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