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