- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / drivers / acpi / thermal.c
1 /*
2  *  acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  *
25  *  This driver fully implements the ACPI thermal policy as described in the
26  *  ACPI 2.0 Specification.
27  *
28  *  TBD: 1. Implement passive cooling hysteresis.
29  *       2. Enhance passive cooling (CPU) states/limit interface to support
30  *          concepts of 'multiple limiters', upper/lower limits, etc.
31  *
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/dmi.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
39 #include <linux/proc_fs.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/seq_file.h>
43 #include <linux/reboot.h>
44 #include <linux/device.h>
45 #include <linux/dmi.h>
46 #include <asm/uaccess.h>
47 #include <linux/thermal.h>
48 #include <acpi/acpi_bus.h>
49 #include <acpi/acpi_drivers.h>
50
51 #define PREFIX "ACPI: "
52
53 #define ACPI_THERMAL_CLASS              "thermal_zone"
54 #define ACPI_THERMAL_DEVICE_NAME        "Thermal Zone"
55 #define ACPI_THERMAL_FILE_STATE         "state"
56 #define ACPI_THERMAL_FILE_TEMPERATURE   "temperature"
57 #define ACPI_THERMAL_FILE_TRIP_POINTS   "trip_points"
58 #define ACPI_THERMAL_FILE_COOLING_MODE  "cooling_mode"
59 #define ACPI_THERMAL_FILE_POLLING_FREQ  "polling_frequency"
60 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
61 #define ACPI_THERMAL_NOTIFY_THRESHOLDS  0x81
62 #define ACPI_THERMAL_NOTIFY_DEVICES     0x82
63 #define ACPI_THERMAL_NOTIFY_CRITICAL    0xF0
64 #define ACPI_THERMAL_NOTIFY_HOT         0xF1
65 #define ACPI_THERMAL_MODE_ACTIVE        0x00
66
67 #define ACPI_THERMAL_MAX_ACTIVE 10
68 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
69
70 #define _COMPONENT              ACPI_THERMAL_COMPONENT
71 ACPI_MODULE_NAME("thermal");
72
73 MODULE_AUTHOR("Paul Diefenbaugh");
74 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
75 MODULE_LICENSE("GPL");
76
77 static int act;
78 module_param(act, int, 0644);
79 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
80
81 static int crt;
82 module_param(crt, int, 0644);
83 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
84
85 static int tzp;
86 module_param(tzp, int, 0444);
87 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
88
89 static int nocrt;
90 module_param(nocrt, int, 0);
91 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
92
93 static int off;
94 module_param(off, int, 0);
95 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
96
97 static int psv;
98 module_param(psv, int, 0644);
99 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
100
101 static int acpi_thermal_add(struct acpi_device *device);
102 static int acpi_thermal_remove(struct acpi_device *device, int type);
103 static int acpi_thermal_resume(struct acpi_device *device);
104 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
105 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
106 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
107 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
108 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
109 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
110                                                const char __user *, size_t,
111                                                loff_t *);
112 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
113 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
114                                           size_t, loff_t *);
115
116 static const struct acpi_device_id  thermal_device_ids[] = {
117         {ACPI_THERMAL_HID, 0},
118         {"", 0},
119 };
120 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
121
122 static struct acpi_driver acpi_thermal_driver = {
123         .name = "thermal",
124         .class = ACPI_THERMAL_CLASS,
125         .ids = thermal_device_ids,
126         .ops = {
127                 .add = acpi_thermal_add,
128                 .remove = acpi_thermal_remove,
129                 .resume = acpi_thermal_resume,
130                 .notify = acpi_thermal_notify,
131                 },
132 };
133
134 struct acpi_thermal_state {
135         u8 critical:1;
136         u8 hot:1;
137         u8 passive:1;
138         u8 active:1;
139         u8 reserved:4;
140         int active_index;
141 };
142
143 struct acpi_thermal_state_flags {
144         u8 valid:1;
145         u8 enabled:1;
146         u8 reserved:6;
147 };
148
149 struct acpi_thermal_critical {
150         struct acpi_thermal_state_flags flags;
151         unsigned long temperature;
152 };
153
154 struct acpi_thermal_hot {
155         struct acpi_thermal_state_flags flags;
156         unsigned long temperature;
157 };
158
159 struct acpi_thermal_passive {
160         struct acpi_thermal_state_flags flags;
161         unsigned long temperature;
162         unsigned long tc1;
163         unsigned long tc2;
164         unsigned long tsp;
165         struct acpi_handle_list devices;
166 };
167
168 struct acpi_thermal_active {
169         struct acpi_thermal_state_flags flags;
170         unsigned long temperature;
171         struct acpi_handle_list devices;
172 };
173
174 struct acpi_thermal_trips {
175         struct acpi_thermal_critical critical;
176         struct acpi_thermal_hot hot;
177         struct acpi_thermal_passive passive;
178         struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
179 };
180
181 struct acpi_thermal_flags {
182         u8 cooling_mode:1;      /* _SCP */
183         u8 devices:1;           /* _TZD */
184         u8 reserved:6;
185 };
186
187 struct acpi_thermal {
188         struct acpi_device * device;
189         acpi_bus_id name;
190         unsigned long temperature;
191         unsigned long last_temperature;
192         unsigned long polling_frequency;
193         volatile u8 zombie;
194         struct acpi_thermal_flags flags;
195         struct acpi_thermal_state state;
196         struct acpi_thermal_trips trips;
197         struct acpi_handle_list devices;
198         struct thermal_zone_device *thermal_zone;
199         int tz_enabled;
200         int kelvin_offset;
201         struct mutex lock;
202 };
203
204 static const struct file_operations acpi_thermal_state_fops = {
205         .owner = THIS_MODULE,
206         .open = acpi_thermal_state_open_fs,
207         .read = seq_read,
208         .llseek = seq_lseek,
209         .release = single_release,
210 };
211
212 static const struct file_operations acpi_thermal_temp_fops = {
213         .owner = THIS_MODULE,
214         .open = acpi_thermal_temp_open_fs,
215         .read = seq_read,
216         .llseek = seq_lseek,
217         .release = single_release,
218 };
219
220 static const struct file_operations acpi_thermal_trip_fops = {
221         .owner = THIS_MODULE,
222         .open = acpi_thermal_trip_open_fs,
223         .read = seq_read,
224         .llseek = seq_lseek,
225         .release = single_release,
226 };
227
228 static const struct file_operations acpi_thermal_cooling_fops = {
229         .owner = THIS_MODULE,
230         .open = acpi_thermal_cooling_open_fs,
231         .read = seq_read,
232         .write = acpi_thermal_write_cooling_mode,
233         .llseek = seq_lseek,
234         .release = single_release,
235 };
236
237 static const struct file_operations acpi_thermal_polling_fops = {
238         .owner = THIS_MODULE,
239         .open = acpi_thermal_polling_open_fs,
240         .read = seq_read,
241         .write = acpi_thermal_write_polling,
242         .llseek = seq_lseek,
243         .release = single_release,
244 };
245
246 /* --------------------------------------------------------------------------
247                              Thermal Zone Management
248    -------------------------------------------------------------------------- */
249
250 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
251 {
252         acpi_status status = AE_OK;
253         unsigned long long tmp;
254
255         if (!tz)
256                 return -EINVAL;
257
258         tz->last_temperature = tz->temperature;
259
260         status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
261         if (ACPI_FAILURE(status))
262                 return -ENODEV;
263
264         tz->temperature = tmp;
265         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
266                           tz->temperature));
267
268         return 0;
269 }
270
271 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
272 {
273         acpi_status status = AE_OK;
274         unsigned long long tmp;
275
276         if (!tz)
277                 return -EINVAL;
278
279         status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
280         if (ACPI_FAILURE(status))
281                 return -ENODEV;
282
283         tz->polling_frequency = tmp;
284         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
285                           tz->polling_frequency));
286
287         return 0;
288 }
289
290 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
291 {
292
293         if (!tz)
294                 return -EINVAL;
295
296         tz->polling_frequency = seconds * 10;   /* Convert value to deci-seconds */
297
298         tz->thermal_zone->polling_delay = seconds * 1000;
299
300         if (tz->tz_enabled)
301                 thermal_zone_device_update(tz->thermal_zone);
302
303         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
304                           "Polling frequency set to %lu seconds\n",
305                           tz->polling_frequency/10));
306
307         return 0;
308 }
309
310 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
311 {
312         acpi_status status = AE_OK;
313         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
314         struct acpi_object_list arg_list = { 1, &arg0 };
315         acpi_handle handle = NULL;
316
317
318         if (!tz)
319                 return -EINVAL;
320
321         status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
322         if (ACPI_FAILURE(status)) {
323                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
324                 return -ENODEV;
325         }
326
327         arg0.integer.value = mode;
328
329         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
330         if (ACPI_FAILURE(status))
331                 return -ENODEV;
332
333         return 0;
334 }
335
336 #define ACPI_TRIPS_CRITICAL     0x01
337 #define ACPI_TRIPS_HOT          0x02
338 #define ACPI_TRIPS_PASSIVE      0x04
339 #define ACPI_TRIPS_ACTIVE       0x08
340 #define ACPI_TRIPS_DEVICES      0x10
341
342 #define ACPI_TRIPS_REFRESH_THRESHOLDS   (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
343 #define ACPI_TRIPS_REFRESH_DEVICES      ACPI_TRIPS_DEVICES
344
345 #define ACPI_TRIPS_INIT      (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT |    \
346                               ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE |  \
347                               ACPI_TRIPS_DEVICES)
348
349 /*
350  * This exception is thrown out in two cases:
351  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
352  *   when re-evaluating the AML code.
353  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
354  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
355  */
356 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str)        \
357 do {    \
358         if (flags != ACPI_TRIPS_INIT)   \
359                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,      \
360                 "ACPI thermal trip point %s changed\n"  \
361                 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
362 } while (0)
363
364 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
365 {
366         acpi_status status = AE_OK;
367         unsigned long long tmp;
368         struct acpi_handle_list devices;
369         int valid = 0;
370         int i;
371
372         /* Critical Shutdown (required) */
373         if (flag & ACPI_TRIPS_CRITICAL) {
374                 status = acpi_evaluate_integer(tz->device->handle,
375                                 "_CRT", NULL, &tmp);
376                 tz->trips.critical.temperature = tmp;
377                 /*
378                  * Treat freezing temperatures as invalid as well; some
379                  * BIOSes return really low values and cause reboots at startup.
380                  * Below zero (Celsius) values clearly aren't right for sure..
381                  * ... so lets discard those as invalid.
382                  */
383                 if (ACPI_FAILURE(status) ||
384                                 tz->trips.critical.temperature <= 2732) {
385                         tz->trips.critical.flags.valid = 0;
386                         ACPI_EXCEPTION((AE_INFO, status,
387                                         "No or invalid critical threshold"));
388                         return -ENODEV;
389                 } else {
390                         tz->trips.critical.flags.valid = 1;
391                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
392                                         "Found critical threshold [%lu]\n",
393                                         tz->trips.critical.temperature));
394                 }
395                 if (tz->trips.critical.flags.valid == 1) {
396                         if (crt == -1) {
397                                 tz->trips.critical.flags.valid = 0;
398                         } else if (crt > 0) {
399                                 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
400                                 /*
401                                  * Allow override critical threshold
402                                  */
403                                 if (crt_k > tz->trips.critical.temperature)
404                                         printk(KERN_WARNING PREFIX
405                                                 "Critical threshold %d C\n", crt);
406                                 tz->trips.critical.temperature = crt_k;
407                         }
408                 }
409         }
410
411         /* Critical Sleep (optional) */
412         if (flag & ACPI_TRIPS_HOT) {
413                 status = acpi_evaluate_integer(tz->device->handle,
414                                 "_HOT", NULL, &tmp);
415                 if (ACPI_FAILURE(status)) {
416                         tz->trips.hot.flags.valid = 0;
417                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
418                                         "No hot threshold\n"));
419                 } else {
420                         tz->trips.hot.temperature = tmp;
421                         tz->trips.hot.flags.valid = 1;
422                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
423                                         "Found hot threshold [%lu]\n",
424                                         tz->trips.critical.temperature));
425                 }
426         }
427
428         /* Passive (optional) */
429         if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
430                 (flag == ACPI_TRIPS_INIT)) {
431                 valid = tz->trips.passive.flags.valid;
432                 if (psv == -1) {
433                         status = AE_SUPPORT;
434                 } else if (psv > 0) {
435                         tmp = CELSIUS_TO_KELVIN(psv);
436                         status = AE_OK;
437                 } else {
438                         status = acpi_evaluate_integer(tz->device->handle,
439                                 "_PSV", NULL, &tmp);
440                 }
441
442                 if (ACPI_FAILURE(status))
443                         tz->trips.passive.flags.valid = 0;
444                 else {
445                         tz->trips.passive.temperature = tmp;
446                         tz->trips.passive.flags.valid = 1;
447                         if (flag == ACPI_TRIPS_INIT) {
448                                 status = acpi_evaluate_integer(
449                                                 tz->device->handle, "_TC1",
450                                                 NULL, &tmp);
451                                 if (ACPI_FAILURE(status))
452                                         tz->trips.passive.flags.valid = 0;
453                                 else
454                                         tz->trips.passive.tc1 = tmp;
455                                 status = acpi_evaluate_integer(
456                                                 tz->device->handle, "_TC2",
457                                                 NULL, &tmp);
458                                 if (ACPI_FAILURE(status))
459                                         tz->trips.passive.flags.valid = 0;
460                                 else
461                                         tz->trips.passive.tc2 = tmp;
462                                 status = acpi_evaluate_integer(
463                                                 tz->device->handle, "_TSP",
464                                                 NULL, &tmp);
465                                 if (ACPI_FAILURE(status))
466                                         tz->trips.passive.flags.valid = 0;
467                                 else
468                                         tz->trips.passive.tsp = tmp;
469                         }
470                 }
471         }
472         if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
473                 memset(&devices, 0, sizeof(struct acpi_handle_list));
474                 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
475                                                         NULL, &devices);
476                 if (ACPI_FAILURE(status)) {
477                         printk(KERN_WARNING PREFIX
478                                 "Invalid passive threshold\n");
479                         tz->trips.passive.flags.valid = 0;
480                 }
481                 else
482                         tz->trips.passive.flags.valid = 1;
483
484                 if (memcmp(&tz->trips.passive.devices, &devices,
485                                 sizeof(struct acpi_handle_list))) {
486                         memcpy(&tz->trips.passive.devices, &devices,
487                                 sizeof(struct acpi_handle_list));
488                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
489                 }
490         }
491         if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
492                 if (valid != tz->trips.passive.flags.valid)
493                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
494         }
495
496         /* Active (optional) */
497         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
498                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
499                 valid = tz->trips.active[i].flags.valid;
500
501                 if (act == -1)
502                         break; /* disable all active trip points */
503
504                 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
505                         tz->trips.active[i].flags.valid)) {
506                         status = acpi_evaluate_integer(tz->device->handle,
507                                                         name, NULL, &tmp);
508                         if (ACPI_FAILURE(status)) {
509                                 tz->trips.active[i].flags.valid = 0;
510                                 if (i == 0)
511                                         break;
512                                 if (act <= 0)
513                                         break;
514                                 if (i == 1)
515                                         tz->trips.active[0].temperature =
516                                                 CELSIUS_TO_KELVIN(act);
517                                 else
518                                         /*
519                                          * Don't allow override higher than
520                                          * the next higher trip point
521                                          */
522                                         tz->trips.active[i - 1].temperature =
523                                                 (tz->trips.active[i - 2].temperature <
524                                                 CELSIUS_TO_KELVIN(act) ?
525                                                 tz->trips.active[i - 2].temperature :
526                                                 CELSIUS_TO_KELVIN(act));
527                                 break;
528                         } else {
529                                 tz->trips.active[i].temperature = tmp;
530                                 tz->trips.active[i].flags.valid = 1;
531                         }
532                 }
533
534                 name[2] = 'L';
535                 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
536                         memset(&devices, 0, sizeof(struct acpi_handle_list));
537                         status = acpi_evaluate_reference(tz->device->handle,
538                                                 name, NULL, &devices);
539                         if (ACPI_FAILURE(status)) {
540                                 printk(KERN_WARNING PREFIX
541                                         "Invalid active%d threshold\n", i);
542                                 tz->trips.active[i].flags.valid = 0;
543                         }
544                         else
545                                 tz->trips.active[i].flags.valid = 1;
546
547                         if (memcmp(&tz->trips.active[i].devices, &devices,
548                                         sizeof(struct acpi_handle_list))) {
549                                 memcpy(&tz->trips.active[i].devices, &devices,
550                                         sizeof(struct acpi_handle_list));
551                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
552                         }
553                 }
554                 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
555                         if (valid != tz->trips.active[i].flags.valid)
556                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
557
558                 if (!tz->trips.active[i].flags.valid)
559                         break;
560         }
561
562         if (flag & ACPI_TRIPS_DEVICES) {
563                 memset(&devices, 0, sizeof(struct acpi_handle_list));
564                 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
565                                                 NULL, &devices);
566                 if (memcmp(&tz->devices, &devices,
567                                 sizeof(struct acpi_handle_list))) {
568                         memcpy(&tz->devices, &devices,
569                                 sizeof(struct acpi_handle_list));
570                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
571                 }
572         }
573
574         return 0;
575 }
576
577 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
578 {
579         return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
580 }
581
582 static void acpi_thermal_check(void *data)
583 {
584         struct acpi_thermal *tz = data;
585
586         thermal_zone_device_update(tz->thermal_zone);
587 }
588
589 /* sys I/F for generic thermal sysfs support */
590 #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
591
592 static int thermal_get_temp(struct thermal_zone_device *thermal,
593                             unsigned long *temp)
594 {
595         struct acpi_thermal *tz = thermal->devdata;
596         int result;
597
598         if (!tz)
599                 return -EINVAL;
600
601         result = acpi_thermal_get_temperature(tz);
602         if (result)
603                 return result;
604
605         *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
606         return 0;
607 }
608
609 static const char enabled[] = "kernel";
610 static const char disabled[] = "user";
611 static int thermal_get_mode(struct thermal_zone_device *thermal,
612                                 enum thermal_device_mode *mode)
613 {
614         struct acpi_thermal *tz = thermal->devdata;
615
616         if (!tz)
617                 return -EINVAL;
618
619         *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
620                 THERMAL_DEVICE_DISABLED;
621
622         return 0;
623 }
624
625 static int thermal_set_mode(struct thermal_zone_device *thermal,
626                                 enum thermal_device_mode mode)
627 {
628         struct acpi_thermal *tz = thermal->devdata;
629         int enable;
630
631         if (!tz)
632                 return -EINVAL;
633
634         /*
635          * enable/disable thermal management from ACPI thermal driver
636          */
637         if (mode == THERMAL_DEVICE_ENABLED)
638                 enable = 1;
639         else if (mode == THERMAL_DEVICE_DISABLED)
640                 enable = 0;
641         else
642                 return -EINVAL;
643
644         if (enable != tz->tz_enabled) {
645                 tz->tz_enabled = enable;
646                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
647                         "%s ACPI thermal control\n",
648                         tz->tz_enabled ? enabled : disabled));
649                 acpi_thermal_check(tz);
650         }
651         return 0;
652 }
653
654 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
655                                  int trip, enum thermal_trip_type *type)
656 {
657         struct acpi_thermal *tz = thermal->devdata;
658         int i;
659
660         if (!tz || trip < 0)
661                 return -EINVAL;
662
663         if (tz->trips.critical.flags.valid) {
664                 if (!trip) {
665                         *type = THERMAL_TRIP_CRITICAL;
666                         return 0;
667                 }
668                 trip--;
669         }
670
671         if (tz->trips.hot.flags.valid) {
672                 if (!trip) {
673                         *type = THERMAL_TRIP_HOT;
674                         return 0;
675                 }
676                 trip--;
677         }
678
679         if (tz->trips.passive.flags.valid) {
680                 if (!trip) {
681                         *type = THERMAL_TRIP_PASSIVE;
682                         return 0;
683                 }
684                 trip--;
685         }
686
687         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
688                 tz->trips.active[i].flags.valid; i++) {
689                 if (!trip) {
690                         *type = THERMAL_TRIP_ACTIVE;
691                         return 0;
692                 }
693                 trip--;
694         }
695
696         return -EINVAL;
697 }
698
699 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
700                                  int trip, unsigned long *temp)
701 {
702         struct acpi_thermal *tz = thermal->devdata;
703         int i;
704
705         if (!tz || trip < 0)
706                 return -EINVAL;
707
708         if (tz->trips.critical.flags.valid) {
709                 if (!trip) {
710                         *temp = KELVIN_TO_MILLICELSIUS(
711                                 tz->trips.critical.temperature,
712                                 tz->kelvin_offset);
713                         return 0;
714                 }
715                 trip--;
716         }
717
718         if (tz->trips.hot.flags.valid) {
719                 if (!trip) {
720                         *temp = KELVIN_TO_MILLICELSIUS(
721                                 tz->trips.hot.temperature,
722                                 tz->kelvin_offset);
723                         return 0;
724                 }
725                 trip--;
726         }
727
728         if (tz->trips.passive.flags.valid) {
729                 if (!trip) {
730                         *temp = KELVIN_TO_MILLICELSIUS(
731                                 tz->trips.passive.temperature,
732                                 tz->kelvin_offset);
733                         return 0;
734                 }
735                 trip--;
736         }
737
738         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
739                 tz->trips.active[i].flags.valid; i++) {
740                 if (!trip) {
741                         *temp = KELVIN_TO_MILLICELSIUS(
742                                 tz->trips.active[i].temperature,
743                                 tz->kelvin_offset);
744                         return 0;
745                 }
746                 trip--;
747         }
748
749         return -EINVAL;
750 }
751
752 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
753                                 unsigned long *temperature) {
754         struct acpi_thermal *tz = thermal->devdata;
755
756         if (tz->trips.critical.flags.valid) {
757                 *temperature = KELVIN_TO_MILLICELSIUS(
758                                 tz->trips.critical.temperature,
759                                 tz->kelvin_offset);
760                 return 0;
761         } else
762                 return -EINVAL;
763 }
764
765 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
766                            enum thermal_trip_type trip_type)
767 {
768         u8 type = 0;
769         struct acpi_thermal *tz = thermal->devdata;
770
771         if (trip_type == THERMAL_TRIP_CRITICAL)
772                 type = ACPI_THERMAL_NOTIFY_CRITICAL;
773         else if (trip_type == THERMAL_TRIP_HOT)
774                 type = ACPI_THERMAL_NOTIFY_HOT;
775         else
776                 return 0;
777
778         acpi_bus_generate_proc_event(tz->device, type, 1);
779         acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
780                                         dev_name(&tz->device->dev), type, 1);
781
782         if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
783                 return 1;
784
785         return 0;
786 }
787
788 typedef int (*cb)(struct thermal_zone_device *, int,
789                   struct thermal_cooling_device *);
790 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
791                                         struct thermal_cooling_device *cdev,
792                                         cb action)
793 {
794         struct acpi_device *device = cdev->devdata;
795         struct acpi_thermal *tz = thermal->devdata;
796         struct acpi_device *dev;
797         acpi_status status;
798         acpi_handle handle;
799         int i;
800         int j;
801         int trip = -1;
802         int result = 0;
803
804         if (tz->trips.critical.flags.valid)
805                 trip++;
806
807         if (tz->trips.hot.flags.valid)
808                 trip++;
809
810         if (tz->trips.passive.flags.valid) {
811                 trip++;
812                 for (i = 0; i < tz->trips.passive.devices.count;
813                     i++) {
814                         handle = tz->trips.passive.devices.handles[i];
815                         status = acpi_bus_get_device(handle, &dev);
816                         if (ACPI_SUCCESS(status) && (dev == device)) {
817                                 result = action(thermal, trip, cdev);
818                                 if (result)
819                                         goto failed;
820                         }
821                 }
822         }
823
824         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
825                 if (!tz->trips.active[i].flags.valid)
826                         break;
827                 trip++;
828                 for (j = 0;
829                     j < tz->trips.active[i].devices.count;
830                     j++) {
831                         handle = tz->trips.active[i].devices.handles[j];
832                         status = acpi_bus_get_device(handle, &dev);
833                         if (ACPI_SUCCESS(status) && (dev == device)) {
834                                 result = action(thermal, trip, cdev);
835                                 if (result)
836                                         goto failed;
837                         }
838                 }
839         }
840
841         for (i = 0; i < tz->devices.count; i++) {
842                 handle = tz->devices.handles[i];
843                 status = acpi_bus_get_device(handle, &dev);
844                 if (ACPI_SUCCESS(status) && (dev == device)) {
845                         result = action(thermal, -1, cdev);
846                         if (result)
847                                 goto failed;
848                 }
849         }
850
851 failed:
852         return result;
853 }
854
855 static int
856 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
857                                         struct thermal_cooling_device *cdev)
858 {
859         return acpi_thermal_cooling_device_cb(thermal, cdev,
860                                 thermal_zone_bind_cooling_device);
861 }
862
863 static int
864 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
865                                         struct thermal_cooling_device *cdev)
866 {
867         return acpi_thermal_cooling_device_cb(thermal, cdev,
868                                 thermal_zone_unbind_cooling_device);
869 }
870
871 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
872         .bind = acpi_thermal_bind_cooling_device,
873         .unbind = acpi_thermal_unbind_cooling_device,
874         .get_temp = thermal_get_temp,
875         .get_mode = thermal_get_mode,
876         .set_mode = thermal_set_mode,
877         .get_trip_type = thermal_get_trip_type,
878         .get_trip_temp = thermal_get_trip_temp,
879         .get_crit_temp = thermal_get_crit_temp,
880         .notify = thermal_notify,
881 };
882
883 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
884 {
885         int trips = 0;
886         int result;
887         acpi_status status;
888         int i;
889
890         if (tz->trips.critical.flags.valid)
891                 trips++;
892
893         if (tz->trips.hot.flags.valid)
894                 trips++;
895
896         if (tz->trips.passive.flags.valid)
897                 trips++;
898
899         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
900                         tz->trips.active[i].flags.valid; i++, trips++);
901
902         if (tz->trips.passive.flags.valid)
903                 tz->thermal_zone =
904                         thermal_zone_device_register("acpitz", trips, tz,
905                                                      &acpi_thermal_zone_ops,
906                                                      tz->trips.passive.tc1,
907                                                      tz->trips.passive.tc2,
908                                                      tz->trips.passive.tsp*100,
909                                                      tz->polling_frequency*100);
910         else
911                 tz->thermal_zone =
912                         thermal_zone_device_register("acpitz", trips, tz,
913                                                      &acpi_thermal_zone_ops,
914                                                      0, 0, 0,
915                                                      tz->polling_frequency*100);
916         if (IS_ERR(tz->thermal_zone))
917                 return -ENODEV;
918
919         result = sysfs_create_link(&tz->device->dev.kobj,
920                                    &tz->thermal_zone->device.kobj, "thermal_zone");
921         if (result)
922                 return result;
923
924         result = sysfs_create_link(&tz->thermal_zone->device.kobj,
925                                    &tz->device->dev.kobj, "device");
926         if (result)
927                 return result;
928
929         status = acpi_attach_data(tz->device->handle,
930                                   acpi_bus_private_data_handler,
931                                   tz->thermal_zone);
932         if (ACPI_FAILURE(status)) {
933                 printk(KERN_ERR PREFIX
934                                 "Error attaching device data\n");
935                 return -ENODEV;
936         }
937
938         tz->tz_enabled = 1;
939
940         dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
941                  tz->thermal_zone->id);
942         return 0;
943 }
944
945 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
946 {
947         sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
948         sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
949         thermal_zone_device_unregister(tz->thermal_zone);
950         tz->thermal_zone = NULL;
951         acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
952 }
953
954
955 /* --------------------------------------------------------------------------
956                               FS Interface (/proc)
957    -------------------------------------------------------------------------- */
958
959 static struct proc_dir_entry *acpi_thermal_dir;
960
961 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
962 {
963         struct acpi_thermal *tz = seq->private;
964
965
966         if (!tz)
967                 goto end;
968
969         seq_puts(seq, "state:                   ");
970
971         if (!tz->state.critical && !tz->state.hot && !tz->state.passive
972             && !tz->state.active)
973                 seq_puts(seq, "ok\n");
974         else {
975                 if (tz->state.critical)
976                         seq_puts(seq, "critical ");
977                 if (tz->state.hot)
978                         seq_puts(seq, "hot ");
979                 if (tz->state.passive)
980                         seq_puts(seq, "passive ");
981                 if (tz->state.active)
982                         seq_printf(seq, "active[%d]", tz->state.active_index);
983                 seq_puts(seq, "\n");
984         }
985
986       end:
987         return 0;
988 }
989
990 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
991 {
992         return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
993 }
994
995 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
996 {
997         int result = 0;
998         struct acpi_thermal *tz = seq->private;
999
1000
1001         if (!tz)
1002                 goto end;
1003
1004         result = acpi_thermal_get_temperature(tz);
1005         if (result)
1006                 goto end;
1007
1008         seq_printf(seq, "temperature:             %ld C\n",
1009                    KELVIN_TO_CELSIUS(tz->temperature));
1010
1011       end:
1012         return 0;
1013 }
1014
1015 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1016 {
1017         return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1018 }
1019
1020 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1021 {
1022         struct acpi_thermal *tz = seq->private;
1023         struct acpi_device *device;
1024         acpi_status status;
1025
1026         int i = 0;
1027         int j = 0;
1028
1029
1030         if (!tz)
1031                 goto end;
1032
1033         if (tz->trips.critical.flags.valid)
1034                 seq_printf(seq, "critical (S5):           %ld C%s",
1035                            KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1036                            nocrt ? " <disabled>\n" : "\n");
1037
1038         if (tz->trips.hot.flags.valid)
1039                 seq_printf(seq, "hot (S4):                %ld C%s",
1040                            KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1041                            nocrt ? " <disabled>\n" : "\n");
1042
1043         if (tz->trips.passive.flags.valid) {
1044                 seq_printf(seq,
1045                            "passive:                 %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1046                            KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1047                            tz->trips.passive.tc1, tz->trips.passive.tc2,
1048                            tz->trips.passive.tsp);
1049                 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1050                         status = acpi_bus_get_device(tz->trips.passive.devices.
1051                                                      handles[j], &device);
1052                         seq_printf(seq, "%4.4s ", status ? "" :
1053                                    acpi_device_bid(device));
1054                 }
1055                 seq_puts(seq, "\n");
1056         } else {
1057                 seq_printf(seq, "passive (forced):");
1058                 if (tz->thermal_zone->forced_passive)
1059                         seq_printf(seq, "        %i C\n",
1060                                    tz->thermal_zone->forced_passive / 1000);
1061                 else
1062                         seq_printf(seq, "<not set>\n");
1063         }
1064
1065         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1066                 if (!(tz->trips.active[i].flags.valid))
1067                         break;
1068                 seq_printf(seq, "active[%d]:               %ld C: devices=",
1069                            i,
1070                            KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1071                 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1072                         status = acpi_bus_get_device(tz->trips.active[i].
1073                                                      devices.handles[j],
1074                                                      &device);
1075                         seq_printf(seq, "%4.4s ", status ? "" :
1076                                    acpi_device_bid(device));
1077                 }
1078                 seq_puts(seq, "\n");
1079         }
1080
1081       end:
1082         return 0;
1083 }
1084
1085 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1086 {
1087         return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1088 }
1089
1090 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1091 {
1092         struct acpi_thermal *tz = seq->private;
1093
1094
1095         if (!tz)
1096                 goto end;
1097
1098         if (!tz->flags.cooling_mode)
1099                 seq_puts(seq, "<setting not supported>\n");
1100         else
1101                 seq_puts(seq, "0 - Active; 1 - Passive\n");
1102
1103       end:
1104         return 0;
1105 }
1106
1107 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1108 {
1109         return single_open(file, acpi_thermal_cooling_seq_show,
1110                            PDE(inode)->data);
1111 }
1112
1113 static ssize_t
1114 acpi_thermal_write_cooling_mode(struct file *file,
1115                                 const char __user * buffer,
1116                                 size_t count, loff_t * ppos)
1117 {
1118         struct seq_file *m = file->private_data;
1119         struct acpi_thermal *tz = m->private;
1120         int result = 0;
1121         char mode_string[12] = { '\0' };
1122
1123
1124         if (!tz || (count > sizeof(mode_string) - 1))
1125                 return -EINVAL;
1126
1127         if (!tz->flags.cooling_mode)
1128                 return -ENODEV;
1129
1130         if (copy_from_user(mode_string, buffer, count))
1131                 return -EFAULT;
1132
1133         mode_string[count] = '\0';
1134
1135         result = acpi_thermal_set_cooling_mode(tz,
1136                                                simple_strtoul(mode_string, NULL,
1137                                                               0));
1138         if (result)
1139                 return result;
1140
1141         acpi_thermal_check(tz);
1142
1143         return count;
1144 }
1145
1146 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1147 {
1148         struct acpi_thermal *tz = seq->private;
1149
1150
1151         if (!tz)
1152                 goto end;
1153
1154         if (!tz->thermal_zone->polling_delay) {
1155                 seq_puts(seq, "<polling disabled>\n");
1156                 goto end;
1157         }
1158
1159         seq_printf(seq, "polling frequency:       %d seconds\n",
1160                    (tz->thermal_zone->polling_delay / 1000));
1161
1162       end:
1163         return 0;
1164 }
1165
1166 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1167 {
1168         return single_open(file, acpi_thermal_polling_seq_show,
1169                            PDE(inode)->data);
1170 }
1171
1172 static ssize_t
1173 acpi_thermal_write_polling(struct file *file,
1174                            const char __user * buffer,
1175                            size_t count, loff_t * ppos)
1176 {
1177         struct seq_file *m = file->private_data;
1178         struct acpi_thermal *tz = m->private;
1179         int result = 0;
1180         char polling_string[12] = { '\0' };
1181         int seconds = 0;
1182
1183
1184         if (!tz || (count > sizeof(polling_string) - 1))
1185                 return -EINVAL;
1186
1187         if (copy_from_user(polling_string, buffer, count))
1188                 return -EFAULT;
1189
1190         polling_string[count] = '\0';
1191
1192         seconds = simple_strtoul(polling_string, NULL, 0);
1193
1194         result = acpi_thermal_set_polling(tz, seconds);
1195         if (result)
1196                 return result;
1197
1198         acpi_thermal_check(tz);
1199
1200         return count;
1201 }
1202
1203 static int acpi_thermal_add_fs(struct acpi_device *device)
1204 {
1205         struct proc_dir_entry *entry = NULL;
1206
1207
1208         if (!acpi_device_dir(device)) {
1209                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1210                                                      acpi_thermal_dir);
1211                 if (!acpi_device_dir(device))
1212                         return -ENODEV;
1213         }
1214
1215         /* 'state' [R] */
1216         entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1217                                  S_IRUGO, acpi_device_dir(device),
1218                                  &acpi_thermal_state_fops,
1219                                  acpi_driver_data(device));
1220         if (!entry)
1221                 return -ENODEV;
1222
1223         /* 'temperature' [R] */
1224         entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1225                                  S_IRUGO, acpi_device_dir(device),
1226                                  &acpi_thermal_temp_fops,
1227                                  acpi_driver_data(device));
1228         if (!entry)
1229                 return -ENODEV;
1230
1231         /* 'trip_points' [R] */
1232         entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1233                                  S_IRUGO,
1234                                  acpi_device_dir(device),
1235                                  &acpi_thermal_trip_fops,
1236                                  acpi_driver_data(device));
1237         if (!entry)
1238                 return -ENODEV;
1239
1240         /* 'cooling_mode' [R/W] */
1241         entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1242                                  S_IFREG | S_IRUGO | S_IWUSR,
1243                                  acpi_device_dir(device),
1244                                  &acpi_thermal_cooling_fops,
1245                                  acpi_driver_data(device));
1246         if (!entry)
1247                 return -ENODEV;
1248
1249         /* 'polling_frequency' [R/W] */
1250         entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1251                                  S_IFREG | S_IRUGO | S_IWUSR,
1252                                  acpi_device_dir(device),
1253                                  &acpi_thermal_polling_fops,
1254                                  acpi_driver_data(device));
1255         if (!entry)
1256                 return -ENODEV;
1257         return 0;
1258 }
1259
1260 static int acpi_thermal_remove_fs(struct acpi_device *device)
1261 {
1262
1263         if (acpi_device_dir(device)) {
1264                 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1265                                   acpi_device_dir(device));
1266                 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1267                                   acpi_device_dir(device));
1268                 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1269                                   acpi_device_dir(device));
1270                 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1271                                   acpi_device_dir(device));
1272                 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1273                                   acpi_device_dir(device));
1274                 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1275                 acpi_device_dir(device) = NULL;
1276         }
1277
1278         return 0;
1279 }
1280
1281 /* --------------------------------------------------------------------------
1282                                  Driver Interface
1283    -------------------------------------------------------------------------- */
1284
1285 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
1286 {
1287         struct acpi_thermal *tz = acpi_driver_data(device);
1288
1289
1290         if (!tz)
1291                 return;
1292
1293         switch (event) {
1294         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1295                 acpi_thermal_check(tz);
1296                 break;
1297         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1298                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1299                 acpi_thermal_check(tz);
1300                 acpi_bus_generate_proc_event(device, event, 0);
1301                 acpi_bus_generate_netlink_event(device->pnp.device_class,
1302                                                   dev_name(&device->dev), event, 0);
1303                 break;
1304         case ACPI_THERMAL_NOTIFY_DEVICES:
1305                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1306                 acpi_thermal_check(tz);
1307                 acpi_bus_generate_proc_event(device, event, 0);
1308                 acpi_bus_generate_netlink_event(device->pnp.device_class,
1309                                                   dev_name(&device->dev), event, 0);
1310                 break;
1311         default:
1312                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1313                                   "Unsupported event [0x%x]\n", event));
1314                 break;
1315         }
1316 }
1317
1318 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1319 {
1320         int result = 0;
1321
1322
1323         if (!tz)
1324                 return -EINVAL;
1325
1326         /* Get temperature [_TMP] (required) */
1327         result = acpi_thermal_get_temperature(tz);
1328         if (result)
1329                 return result;
1330
1331         /* Get trip points [_CRT, _PSV, etc.] (required) */
1332         result = acpi_thermal_get_trip_points(tz);
1333         if (result)
1334                 return result;
1335
1336         /* Set the cooling mode [_SCP] to active cooling (default) */
1337         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1338         if (!result)
1339                 tz->flags.cooling_mode = 1;
1340
1341         /* Get default polling frequency [_TZP] (optional) */
1342         if (tzp)
1343                 tz->polling_frequency = tzp;
1344         else
1345                 acpi_thermal_get_polling_frequency(tz);
1346
1347         return 0;
1348 }
1349
1350 /*
1351  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1352  * handles temperature values with a single decimal place. As a consequence,
1353  * some implementations use an offset of 273.1 and others use an offset of
1354  * 273.2. Try to find out which one is being used, to present the most
1355  * accurate and visually appealing number.
1356  *
1357  * The heuristic below should work for all ACPI thermal zones which have a
1358  * critical trip point with a value being a multiple of 0.5 degree Celsius.
1359  */
1360 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1361 {
1362         if (tz->trips.critical.flags.valid &&
1363             (tz->trips.critical.temperature % 5) == 1)
1364                 tz->kelvin_offset = 2731;
1365         else
1366                 tz->kelvin_offset = 2732;
1367 }
1368
1369 static struct dmi_system_id thermal_psv_dmi_table[] = {
1370         {
1371                 .ident = "IBM ThinkPad T41",
1372                 .matches = {
1373                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1374                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T41"),
1375                 },
1376         },
1377         {
1378                 .ident = "IBM ThinkPad T42",
1379                 .matches = {
1380                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1381                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T42"),
1382                 },
1383         },
1384         {
1385                 .ident = "IBM ThinkPad T43",
1386                 .matches = {
1387                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1388                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T43"),
1389                 },
1390         },
1391         {
1392                 .ident = "IBM ThinkPad T41p",
1393                 .matches = {
1394                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1395                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T41p"),
1396                 },
1397         },
1398         {
1399                 .ident = "IBM ThinkPad T42p",
1400                 .matches = {
1401                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1402                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T42p"),
1403                 },
1404         },
1405         {
1406                 .ident = "IBM ThinkPad T43p",
1407                 .matches = {
1408                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1409                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad T43p"),
1410                 },
1411         },
1412         {
1413                 .ident = "IBM ThinkPad R40",
1414                 .matches = {
1415                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1416                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad R40"),
1417                 },
1418         },
1419         {
1420                 .ident = "IBM ThinkPad R50p",
1421                 .matches = {
1422                         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
1423                         DMI_MATCH(DMI_PRODUCT_VERSION,"ThinkPad R50p"),
1424                 },
1425         },
1426         {},
1427 };
1428
1429 static int acpi_thermal_add(struct acpi_device *device)
1430 {
1431         int result = 0;
1432         struct acpi_thermal *tz = NULL;
1433
1434
1435         if (!device)
1436                 return -EINVAL;
1437
1438         tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1439         if (!tz)
1440                 return -ENOMEM;
1441
1442         tz->device = device;
1443         strcpy(tz->name, device->pnp.bus_id);
1444         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1445         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1446         device->driver_data = tz;
1447         mutex_init(&tz->lock);
1448
1449
1450         result = acpi_thermal_get_info(tz);
1451         if (result)
1452                 goto free_memory;
1453
1454         acpi_thermal_guess_offset(tz);
1455
1456         result = acpi_thermal_register_thermal_zone(tz);
1457         if (result)
1458                 goto free_memory;
1459
1460         if (dmi_check_system(thermal_psv_dmi_table)) {
1461                 if (tz->trips.passive.flags.valid &&
1462                     tz->trips.passive.temperature > CELSIUS_TO_KELVIN(85)) {
1463                         printk (KERN_INFO "Adjust passive trip point from %lu"
1464                                 " to %lu\n",
1465                                 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1466                                 KELVIN_TO_CELSIUS(tz->trips.passive.temperature - 150));
1467                         tz->trips.passive.temperature -= 150;
1468                         acpi_thermal_set_polling(tz, 5);
1469                 }
1470         }
1471
1472         result = acpi_thermal_add_fs(device);
1473         if (result)
1474                 goto unregister_thermal_zone;
1475
1476         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1477                acpi_device_name(device), acpi_device_bid(device),
1478                KELVIN_TO_CELSIUS(tz->temperature));
1479         goto end;
1480
1481 unregister_thermal_zone:
1482         thermal_zone_device_unregister(tz->thermal_zone);
1483 free_memory:
1484         kfree(tz);
1485 end:
1486         return result;
1487 }
1488
1489 static int acpi_thermal_remove(struct acpi_device *device, int type)
1490 {
1491         struct acpi_thermal *tz = NULL;
1492
1493         if (!device || !acpi_driver_data(device))
1494                 return -EINVAL;
1495
1496         tz = acpi_driver_data(device);
1497
1498         acpi_thermal_remove_fs(device);
1499         acpi_thermal_unregister_thermal_zone(tz);
1500         mutex_destroy(&tz->lock);
1501         kfree(tz);
1502         return 0;
1503 }
1504
1505 static int acpi_thermal_resume(struct acpi_device *device)
1506 {
1507         struct acpi_thermal *tz = NULL;
1508         int i, j, power_state, result;
1509
1510
1511         if (!device || !acpi_driver_data(device))
1512                 return -EINVAL;
1513
1514         tz = acpi_driver_data(device);
1515
1516         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1517                 if (!(&tz->trips.active[i]))
1518                         break;
1519                 if (!tz->trips.active[i].flags.valid)
1520                         break;
1521                 tz->trips.active[i].flags.enabled = 1;
1522                 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1523                         result = acpi_bus_get_power(tz->trips.active[i].devices.
1524                             handles[j], &power_state);
1525                         if (result || (power_state != ACPI_STATE_D0)) {
1526                                 tz->trips.active[i].flags.enabled = 0;
1527                                 break;
1528                         }
1529                 }
1530                 tz->state.active |= tz->trips.active[i].flags.enabled;
1531         }
1532
1533         acpi_thermal_check(tz);
1534
1535         return AE_OK;
1536 }
1537
1538 static int thermal_act(const struct dmi_system_id *d) {
1539
1540         if (act == 0) {
1541                 printk(KERN_NOTICE "ACPI: %s detected: "
1542                         "disabling all active thermal trip points\n", d->ident);
1543                 act = -1;
1544         }
1545         return 0;
1546 }
1547 static int thermal_nocrt(const struct dmi_system_id *d) {
1548
1549         printk(KERN_NOTICE "ACPI: %s detected: "
1550                 "disabling all critical thermal trip point actions.\n", d->ident);
1551         nocrt = 1;
1552         return 0;
1553 }
1554 static int thermal_tzp(const struct dmi_system_id *d) {
1555
1556         if (tzp == 0) {
1557                 printk(KERN_NOTICE "ACPI: %s detected: "
1558                         "enabling thermal zone polling\n", d->ident);
1559                 tzp = 300;      /* 300 dS = 30 Seconds */
1560         }
1561         return 0;
1562 }
1563 static int thermal_psv(const struct dmi_system_id *d) {
1564
1565         if (psv == 0) {
1566                 printk(KERN_NOTICE "ACPI: %s detected: "
1567                         "disabling all passive thermal trip points\n", d->ident);
1568                 psv = -1;
1569         }
1570         return 0;
1571 }
1572
1573 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1574         /*
1575          * Award BIOS on this AOpen makes thermal control almost worthless.
1576          * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1577          */
1578         {
1579          .callback = thermal_act,
1580          .ident = "AOpen i915GMm-HFS",
1581          .matches = {
1582                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1583                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1584                 },
1585         },
1586         {
1587          .callback = thermal_psv,
1588          .ident = "AOpen i915GMm-HFS",
1589          .matches = {
1590                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1591                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1592                 },
1593         },
1594         {
1595          .callback = thermal_tzp,
1596          .ident = "AOpen i915GMm-HFS",
1597          .matches = {
1598                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1599                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1600                 },
1601         },
1602         {
1603          .callback = thermal_nocrt,
1604          .ident = "Gigabyte GA-7ZX",
1605          .matches = {
1606                 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1607                 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1608                 },
1609         },
1610         {}
1611 };
1612
1613 static int __init acpi_thermal_init(void)
1614 {
1615         int result = 0;
1616
1617         dmi_check_system(thermal_dmi_table);
1618
1619         if (off) {
1620                 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1621                 return -ENODEV;
1622         }
1623         acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1624         if (!acpi_thermal_dir)
1625                 return -ENODEV;
1626
1627         result = acpi_bus_register_driver(&acpi_thermal_driver);
1628         if (result < 0) {
1629                 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1630                 return -ENODEV;
1631         }
1632
1633         return 0;
1634 }
1635
1636 static void __exit acpi_thermal_exit(void)
1637 {
1638
1639         acpi_bus_unregister_driver(&acpi_thermal_driver);
1640
1641         remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1642
1643         return;
1644 }
1645
1646 module_init(acpi_thermal_init);
1647 module_exit(acpi_thermal_exit);