]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/acpi/thermal.c
Pull trivial2 into release branch
[linux-2.6.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/init.h>
37 #include <linux/types.h>
38 #include <linux/proc_fs.h>
39 #include <linux/sched.h>
40 #include <linux/kmod.h>
41 #include <linux/seq_file.h>
42 #include <asm/uaccess.h>
43
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
46
47 #define ACPI_THERMAL_COMPONENT          0x04000000
48 #define ACPI_THERMAL_CLASS              "thermal_zone"
49 #define ACPI_THERMAL_DRIVER_NAME        "ACPI Thermal Zone Driver"
50 #define ACPI_THERMAL_DEVICE_NAME        "Thermal Zone"
51 #define ACPI_THERMAL_FILE_STATE         "state"
52 #define ACPI_THERMAL_FILE_TEMPERATURE   "temperature"
53 #define ACPI_THERMAL_FILE_TRIP_POINTS   "trip_points"
54 #define ACPI_THERMAL_FILE_COOLING_MODE  "cooling_mode"
55 #define ACPI_THERMAL_FILE_POLLING_FREQ  "polling_frequency"
56 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57 #define ACPI_THERMAL_NOTIFY_THRESHOLDS  0x81
58 #define ACPI_THERMAL_NOTIFY_DEVICES     0x82
59 #define ACPI_THERMAL_NOTIFY_CRITICAL    0xF0
60 #define ACPI_THERMAL_NOTIFY_HOT         0xF1
61 #define ACPI_THERMAL_MODE_ACTIVE        0x00
62 #define ACPI_THERMAL_MODE_PASSIVE       0x01
63 #define ACPI_THERMAL_MODE_CRITICAL      0xff
64 #define ACPI_THERMAL_PATH_POWEROFF      "/sbin/poweroff"
65
66 #define ACPI_THERMAL_MAX_ACTIVE 10
67 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
69 #define KELVIN_TO_CELSIUS(t)    (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70 #define CELSIUS_TO_KELVIN(t)    ((t+273)*10)
71
72 #define _COMPONENT              ACPI_THERMAL_COMPONENT
73 ACPI_MODULE_NAME("acpi_thermal")
74
75 MODULE_AUTHOR("Paul Diefenbaugh");
76 MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77 MODULE_LICENSE("GPL");
78
79 static int tzp;
80 module_param(tzp, int, 0);
81 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
82
83 static int acpi_thermal_add(struct acpi_device *device);
84 static int acpi_thermal_remove(struct acpi_device *device, int type);
85 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
86 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
87 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
88 static ssize_t acpi_thermal_write_trip_points(struct file *,
89                                               const char __user *, size_t,
90                                               loff_t *);
91 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
92 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
93                                                const char __user *, size_t,
94                                                loff_t *);
95 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
96 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
97                                           size_t, loff_t *);
98
99 static struct acpi_driver acpi_thermal_driver = {
100         .name = ACPI_THERMAL_DRIVER_NAME,
101         .class = ACPI_THERMAL_CLASS,
102         .ids = ACPI_THERMAL_HID,
103         .ops = {
104                 .add = acpi_thermal_add,
105                 .remove = acpi_thermal_remove,
106                 },
107 };
108
109 struct acpi_thermal_state {
110         u8 critical:1;
111         u8 hot:1;
112         u8 passive:1;
113         u8 active:1;
114         u8 reserved:4;
115         int active_index;
116 };
117
118 struct acpi_thermal_state_flags {
119         u8 valid:1;
120         u8 enabled:1;
121         u8 reserved:6;
122 };
123
124 struct acpi_thermal_critical {
125         struct acpi_thermal_state_flags flags;
126         unsigned long temperature;
127 };
128
129 struct acpi_thermal_hot {
130         struct acpi_thermal_state_flags flags;
131         unsigned long temperature;
132 };
133
134 struct acpi_thermal_passive {
135         struct acpi_thermal_state_flags flags;
136         unsigned long temperature;
137         unsigned long tc1;
138         unsigned long tc2;
139         unsigned long tsp;
140         struct acpi_handle_list devices;
141 };
142
143 struct acpi_thermal_active {
144         struct acpi_thermal_state_flags flags;
145         unsigned long temperature;
146         struct acpi_handle_list devices;
147 };
148
149 struct acpi_thermal_trips {
150         struct acpi_thermal_critical critical;
151         struct acpi_thermal_hot hot;
152         struct acpi_thermal_passive passive;
153         struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
154 };
155
156 struct acpi_thermal_flags {
157         u8 cooling_mode:1;      /* _SCP */
158         u8 devices:1;           /* _TZD */
159         u8 reserved:6;
160 };
161
162 struct acpi_thermal {
163         acpi_handle handle;
164         acpi_bus_id name;
165         unsigned long temperature;
166         unsigned long last_temperature;
167         unsigned long polling_frequency;
168         u8 cooling_mode;
169         volatile u8 zombie;
170         struct acpi_thermal_flags flags;
171         struct acpi_thermal_state state;
172         struct acpi_thermal_trips trips;
173         struct acpi_handle_list devices;
174         struct timer_list timer;
175 };
176
177 static struct file_operations acpi_thermal_state_fops = {
178         .open = acpi_thermal_state_open_fs,
179         .read = seq_read,
180         .llseek = seq_lseek,
181         .release = single_release,
182 };
183
184 static struct file_operations acpi_thermal_temp_fops = {
185         .open = acpi_thermal_temp_open_fs,
186         .read = seq_read,
187         .llseek = seq_lseek,
188         .release = single_release,
189 };
190
191 static struct file_operations acpi_thermal_trip_fops = {
192         .open = acpi_thermal_trip_open_fs,
193         .read = seq_read,
194         .write = acpi_thermal_write_trip_points,
195         .llseek = seq_lseek,
196         .release = single_release,
197 };
198
199 static struct file_operations acpi_thermal_cooling_fops = {
200         .open = acpi_thermal_cooling_open_fs,
201         .read = seq_read,
202         .write = acpi_thermal_write_cooling_mode,
203         .llseek = seq_lseek,
204         .release = single_release,
205 };
206
207 static struct file_operations acpi_thermal_polling_fops = {
208         .open = acpi_thermal_polling_open_fs,
209         .read = seq_read,
210         .write = acpi_thermal_write_polling,
211         .llseek = seq_lseek,
212         .release = single_release,
213 };
214
215 /* --------------------------------------------------------------------------
216                              Thermal Zone Management
217    -------------------------------------------------------------------------- */
218
219 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
220 {
221         acpi_status status = AE_OK;
222
223         ACPI_FUNCTION_TRACE("acpi_thermal_get_temperature");
224
225         if (!tz)
226                 return_VALUE(-EINVAL);
227
228         tz->last_temperature = tz->temperature;
229
230         status =
231             acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
232         if (ACPI_FAILURE(status))
233                 return_VALUE(-ENODEV);
234
235         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
236                           tz->temperature));
237
238         return_VALUE(0);
239 }
240
241 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
242 {
243         acpi_status status = AE_OK;
244
245         ACPI_FUNCTION_TRACE("acpi_thermal_get_polling_frequency");
246
247         if (!tz)
248                 return_VALUE(-EINVAL);
249
250         status =
251             acpi_evaluate_integer(tz->handle, "_TZP", NULL,
252                                   &tz->polling_frequency);
253         if (ACPI_FAILURE(status))
254                 return_VALUE(-ENODEV);
255
256         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
257                           tz->polling_frequency));
258
259         return_VALUE(0);
260 }
261
262 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
263 {
264         ACPI_FUNCTION_TRACE("acpi_thermal_set_polling");
265
266         if (!tz)
267                 return_VALUE(-EINVAL);
268
269         tz->polling_frequency = seconds * 10;   /* Convert value to deci-seconds */
270
271         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272                           "Polling frequency set to %lu seconds\n",
273                           tz->polling_frequency));
274
275         return_VALUE(0);
276 }
277
278 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
279 {
280         acpi_status status = AE_OK;
281         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
282         struct acpi_object_list arg_list = { 1, &arg0 };
283         acpi_handle handle = NULL;
284
285         ACPI_FUNCTION_TRACE("acpi_thermal_set_cooling_mode");
286
287         if (!tz)
288                 return_VALUE(-EINVAL);
289
290         status = acpi_get_handle(tz->handle, "_SCP", &handle);
291         if (ACPI_FAILURE(status)) {
292                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
293                 return_VALUE(-ENODEV);
294         }
295
296         arg0.integer.value = mode;
297
298         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
299         if (ACPI_FAILURE(status))
300                 return_VALUE(-ENODEV);
301
302         tz->cooling_mode = mode;
303
304         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
305                           mode ? "passive" : "active"));
306
307         return_VALUE(0);
308 }
309
310 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
311 {
312         acpi_status status = AE_OK;
313         int i = 0;
314
315         ACPI_FUNCTION_TRACE("acpi_thermal_get_trip_points");
316
317         if (!tz)
318                 return_VALUE(-EINVAL);
319
320         /* Critical Shutdown (required) */
321
322         status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
323                                        &tz->trips.critical.temperature);
324         if (ACPI_FAILURE(status)) {
325                 tz->trips.critical.flags.valid = 0;
326                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical threshold\n"));
327                 return_VALUE(-ENODEV);
328         } else {
329                 tz->trips.critical.flags.valid = 1;
330                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
331                                   "Found critical threshold [%lu]\n",
332                                   tz->trips.critical.temperature));
333         }
334
335         /* Critical Sleep (optional) */
336
337         status =
338             acpi_evaluate_integer(tz->handle, "_HOT", NULL,
339                                   &tz->trips.hot.temperature);
340         if (ACPI_FAILURE(status)) {
341                 tz->trips.hot.flags.valid = 0;
342                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
343         } else {
344                 tz->trips.hot.flags.valid = 1;
345                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
346                                   tz->trips.hot.temperature));
347         }
348
349         /* Passive: Processors (optional) */
350
351         status =
352             acpi_evaluate_integer(tz->handle, "_PSV", NULL,
353                                   &tz->trips.passive.temperature);
354         if (ACPI_FAILURE(status)) {
355                 tz->trips.passive.flags.valid = 0;
356                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
357         } else {
358                 tz->trips.passive.flags.valid = 1;
359
360                 status =
361                     acpi_evaluate_integer(tz->handle, "_TC1", NULL,
362                                           &tz->trips.passive.tc1);
363                 if (ACPI_FAILURE(status))
364                         tz->trips.passive.flags.valid = 0;
365
366                 status =
367                     acpi_evaluate_integer(tz->handle, "_TC2", NULL,
368                                           &tz->trips.passive.tc2);
369                 if (ACPI_FAILURE(status))
370                         tz->trips.passive.flags.valid = 0;
371
372                 status =
373                     acpi_evaluate_integer(tz->handle, "_TSP", NULL,
374                                           &tz->trips.passive.tsp);
375                 if (ACPI_FAILURE(status))
376                         tz->trips.passive.flags.valid = 0;
377
378                 status =
379                     acpi_evaluate_reference(tz->handle, "_PSL", NULL,
380                                             &tz->trips.passive.devices);
381                 if (ACPI_FAILURE(status))
382                         tz->trips.passive.flags.valid = 0;
383
384                 if (!tz->trips.passive.flags.valid)
385                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
386                                           "Invalid passive threshold\n"));
387                 else
388                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
389                                           "Found passive threshold [%lu]\n",
390                                           tz->trips.passive.temperature));
391         }
392
393         /* Active: Fans, etc. (optional) */
394
395         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
396
397                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
398
399                 status =
400                     acpi_evaluate_integer(tz->handle, name, NULL,
401                                           &tz->trips.active[i].temperature);
402                 if (ACPI_FAILURE(status))
403                         break;
404
405                 name[2] = 'L';
406                 status =
407                     acpi_evaluate_reference(tz->handle, name, NULL,
408                                             &tz->trips.active[i].devices);
409                 if (ACPI_SUCCESS(status)) {
410                         tz->trips.active[i].flags.valid = 1;
411                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412                                           "Found active threshold [%d]:[%lu]\n",
413                                           i, tz->trips.active[i].temperature));
414                 } else
415                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
416                                           "Invalid active threshold [%d]\n",
417                                           i));
418         }
419
420         return_VALUE(0);
421 }
422
423 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
424 {
425         acpi_status status = AE_OK;
426
427         ACPI_FUNCTION_TRACE("acpi_thermal_get_devices");
428
429         if (!tz)
430                 return_VALUE(-EINVAL);
431
432         status =
433             acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
434         if (ACPI_FAILURE(status))
435                 return_VALUE(-ENODEV);
436
437         return_VALUE(0);
438 }
439
440 static int acpi_thermal_call_usermode(char *path)
441 {
442         char *argv[2] = { NULL, NULL };
443         char *envp[3] = { NULL, NULL, NULL };
444
445         ACPI_FUNCTION_TRACE("acpi_thermal_call_usermode");
446
447         if (!path)
448                 return_VALUE(-EINVAL);
449
450         argv[0] = path;
451
452         /* minimal command environment */
453         envp[0] = "HOME=/";
454         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
455
456         call_usermodehelper(argv[0], argv, envp, 0);
457
458         return_VALUE(0);
459 }
460
461 static int acpi_thermal_critical(struct acpi_thermal *tz)
462 {
463         int result = 0;
464         struct acpi_device *device = NULL;
465
466         ACPI_FUNCTION_TRACE("acpi_thermal_critical");
467
468         if (!tz || !tz->trips.critical.flags.valid)
469                 return_VALUE(-EINVAL);
470
471         if (tz->temperature >= tz->trips.critical.temperature) {
472                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Critical trip point\n"));
473                 tz->trips.critical.flags.enabled = 1;
474         } else if (tz->trips.critical.flags.enabled)
475                 tz->trips.critical.flags.enabled = 0;
476
477         result = acpi_bus_get_device(tz->handle, &device);
478         if (result)
479                 return_VALUE(result);
480
481         printk(KERN_EMERG
482                "Critical temperature reached (%ld C), shutting down.\n",
483                KELVIN_TO_CELSIUS(tz->temperature));
484         acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
485                                 tz->trips.critical.flags.enabled);
486
487         acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
488
489         return_VALUE(0);
490 }
491
492 static int acpi_thermal_hot(struct acpi_thermal *tz)
493 {
494         int result = 0;
495         struct acpi_device *device = NULL;
496
497         ACPI_FUNCTION_TRACE("acpi_thermal_hot");
498
499         if (!tz || !tz->trips.hot.flags.valid)
500                 return_VALUE(-EINVAL);
501
502         if (tz->temperature >= tz->trips.hot.temperature) {
503                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Hot trip point\n"));
504                 tz->trips.hot.flags.enabled = 1;
505         } else if (tz->trips.hot.flags.enabled)
506                 tz->trips.hot.flags.enabled = 0;
507
508         result = acpi_bus_get_device(tz->handle, &device);
509         if (result)
510                 return_VALUE(result);
511
512         acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
513                                 tz->trips.hot.flags.enabled);
514
515         /* TBD: Call user-mode "sleep(S4)" function */
516
517         return_VALUE(0);
518 }
519
520 static void acpi_thermal_passive(struct acpi_thermal *tz)
521 {
522         int result = 1;
523         struct acpi_thermal_passive *passive = NULL;
524         int trend = 0;
525         int i = 0;
526
527         ACPI_FUNCTION_TRACE("acpi_thermal_passive");
528
529         if (!tz || !tz->trips.passive.flags.valid)
530                 return;
531
532         passive = &(tz->trips.passive);
533
534         /*
535          * Above Trip?
536          * -----------
537          * Calculate the thermal trend (using the passive cooling equation)
538          * and modify the performance limit for all passive cooling devices
539          * accordingly.  Note that we assume symmetry.
540          */
541         if (tz->temperature >= passive->temperature) {
542                 trend =
543                     (passive->tc1 * (tz->temperature - tz->last_temperature)) +
544                     (passive->tc2 * (tz->temperature - passive->temperature));
545                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
546                                   "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
547                                   trend, passive->tc1, tz->temperature,
548                                   tz->last_temperature, passive->tc2,
549                                   tz->temperature, passive->temperature));
550                 passive->flags.enabled = 1;
551                 /* Heating up? */
552                 if (trend > 0)
553                         for (i = 0; i < passive->devices.count; i++)
554                                 acpi_processor_set_thermal_limit(passive->
555                                                                  devices.
556                                                                  handles[i],
557                                                                  ACPI_PROCESSOR_LIMIT_INCREMENT);
558                 /* Cooling off? */
559                 else if (trend < 0) {
560                         for (i = 0; i < passive->devices.count; i++)
561                                 /*
562                                  * assume that we are on highest
563                                  * freq/lowest thrott and can leave
564                                  * passive mode, even in error case
565                                  */
566                                 if (!acpi_processor_set_thermal_limit
567                                     (passive->devices.handles[i],
568                                      ACPI_PROCESSOR_LIMIT_DECREMENT))
569                                         result = 0;
570                         /*
571                          * Leave cooling mode, even if the temp might
572                          * higher than trip point This is because some
573                          * machines might have long thermal polling
574                          * frequencies (tsp) defined. We will fall back
575                          * into passive mode in next cycle (probably quicker)
576                          */
577                         if (result) {
578                                 passive->flags.enabled = 0;
579                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
580                                                   "Disabling passive cooling, still above threshold,"
581                                                   " but we are cooling down\n"));
582                         }
583                 }
584                 return;
585         }
586
587         /*
588          * Below Trip?
589          * -----------
590          * Implement passive cooling hysteresis to slowly increase performance
591          * and avoid thrashing around the passive trip point.  Note that we
592          * assume symmetry.
593          */
594         if (!passive->flags.enabled)
595                 return;
596         for (i = 0; i < passive->devices.count; i++)
597                 if (!acpi_processor_set_thermal_limit
598                     (passive->devices.handles[i],
599                      ACPI_PROCESSOR_LIMIT_DECREMENT))
600                         result = 0;
601         if (result) {
602                 passive->flags.enabled = 0;
603                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
604                                   "Disabling passive cooling (zone is cool)\n"));
605         }
606 }
607
608 static void acpi_thermal_active(struct acpi_thermal *tz)
609 {
610         int result = 0;
611         struct acpi_thermal_active *active = NULL;
612         int i = 0;
613         int j = 0;
614         unsigned long maxtemp = 0;
615
616         ACPI_FUNCTION_TRACE("acpi_thermal_active");
617
618         if (!tz)
619                 return;
620
621         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
622                 active = &(tz->trips.active[i]);
623                 if (!active || !active->flags.valid)
624                         break;
625                 if (tz->temperature >= active->temperature) {
626                         /*
627                          * Above Threshold?
628                          * ----------------
629                          * If not already enabled, turn ON all cooling devices
630                          * associated with this active threshold.
631                          */
632                         if (active->temperature > maxtemp)
633                                 tz->state.active_index = i;
634                         maxtemp = active->temperature;
635                         if (active->flags.enabled)
636                                 continue;
637                         for (j = 0; j < active->devices.count; j++) {
638                                 result =
639                                     acpi_bus_set_power(active->devices.
640                                                        handles[j],
641                                                        ACPI_STATE_D0);
642                                 if (result) {
643                                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
644                                                           "Unable to turn cooling device [%p] 'on'\n",
645                                                           active->devices.
646                                                           handles[j]));
647                                         continue;
648                                 }
649                                 active->flags.enabled = 1;
650                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
651                                                   "Cooling device [%p] now 'on'\n",
652                                                   active->devices.handles[j]));
653                         }
654                         continue;
655                 }
656                 if (!active->flags.enabled)
657                         continue;
658                 /*
659                  * Below Threshold?
660                  * ----------------
661                  * Turn OFF all cooling devices associated with this
662                  * threshold.
663                  */
664                 for (j = 0; j < active->devices.count; j++) {
665                         result = acpi_bus_set_power(active->devices.handles[j],
666                                                     ACPI_STATE_D3);
667                         if (result) {
668                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
669                                                   "Unable to turn cooling device [%p] 'off'\n",
670                                                   active->devices.handles[j]));
671                                 continue;
672                         }
673                         active->flags.enabled = 0;
674                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
675                                           "Cooling device [%p] now 'off'\n",
676                                           active->devices.handles[j]));
677                 }
678         }
679 }
680
681 static void acpi_thermal_check(void *context);
682
683 static void acpi_thermal_run(unsigned long data)
684 {
685         struct acpi_thermal *tz = (struct acpi_thermal *)data;
686         if (!tz->zombie)
687                 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
688 }
689
690 static void acpi_thermal_check(void *data)
691 {
692         int result = 0;
693         struct acpi_thermal *tz = (struct acpi_thermal *)data;
694         unsigned long sleep_time = 0;
695         int i = 0;
696         struct acpi_thermal_state state;
697
698         ACPI_FUNCTION_TRACE("acpi_thermal_check");
699
700         if (!tz) {
701                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
702                 return_VOID;
703         }
704
705         state = tz->state;
706
707         result = acpi_thermal_get_temperature(tz);
708         if (result)
709                 return_VOID;
710
711         memset(&tz->state, 0, sizeof(tz->state));
712
713         /*
714          * Check Trip Points
715          * -----------------
716          * Compare the current temperature to the trip point values to see
717          * if we've entered one of the thermal policy states.  Note that
718          * this function determines when a state is entered, but the 
719          * individual policy decides when it is exited (e.g. hysteresis).
720          */
721         if (tz->trips.critical.flags.valid)
722                 state.critical |=
723                     (tz->temperature >= tz->trips.critical.temperature);
724         if (tz->trips.hot.flags.valid)
725                 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
726         if (tz->trips.passive.flags.valid)
727                 state.passive |=
728                     (tz->temperature >= tz->trips.passive.temperature);
729         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
730                 if (tz->trips.active[i].flags.valid)
731                         state.active |=
732                             (tz->temperature >=
733                              tz->trips.active[i].temperature);
734
735         /*
736          * Invoke Policy
737          * -------------
738          * Separated from the above check to allow individual policy to 
739          * determine when to exit a given state.
740          */
741         if (state.critical)
742                 acpi_thermal_critical(tz);
743         if (state.hot)
744                 acpi_thermal_hot(tz);
745         if (state.passive)
746                 acpi_thermal_passive(tz);
747         if (state.active)
748                 acpi_thermal_active(tz);
749
750         /*
751          * Calculate State
752          * ---------------
753          * Again, separated from the above two to allow independent policy
754          * decisions.
755          */
756         tz->state.critical = tz->trips.critical.flags.enabled;
757         tz->state.hot = tz->trips.hot.flags.enabled;
758         tz->state.passive = tz->trips.passive.flags.enabled;
759         tz->state.active = 0;
760         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
761                 tz->state.active |= tz->trips.active[i].flags.enabled;
762
763         /*
764          * Calculate Sleep Time
765          * --------------------
766          * If we're in the passive state, use _TSP's value.  Otherwise
767          * use the default polling frequency (e.g. _TZP).  If no polling
768          * frequency is specified then we'll wait forever (at least until
769          * a thermal event occurs).  Note that _TSP and _TZD values are
770          * given in 1/10th seconds (we must covert to milliseconds).
771          */
772         if (tz->state.passive)
773                 sleep_time = tz->trips.passive.tsp * 100;
774         else if (tz->polling_frequency > 0)
775                 sleep_time = tz->polling_frequency * 100;
776
777         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
778                           tz->name, tz->temperature, sleep_time));
779
780         /*
781          * Schedule Next Poll
782          * ------------------
783          */
784         if (!sleep_time) {
785                 if (timer_pending(&(tz->timer)))
786                         del_timer(&(tz->timer));
787         } else {
788                 if (timer_pending(&(tz->timer)))
789                         mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
790                 else {
791                         tz->timer.data = (unsigned long)tz;
792                         tz->timer.function = acpi_thermal_run;
793                         tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
794                         add_timer(&(tz->timer));
795                 }
796         }
797
798         return_VOID;
799 }
800
801 /* --------------------------------------------------------------------------
802                               FS Interface (/proc)
803    -------------------------------------------------------------------------- */
804
805 static struct proc_dir_entry *acpi_thermal_dir;
806
807 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
808 {
809         struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
810
811         ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
812
813         if (!tz)
814                 goto end;
815
816         seq_puts(seq, "state:                   ");
817
818         if (!tz->state.critical && !tz->state.hot && !tz->state.passive
819             && !tz->state.active)
820                 seq_puts(seq, "ok\n");
821         else {
822                 if (tz->state.critical)
823                         seq_puts(seq, "critical ");
824                 if (tz->state.hot)
825                         seq_puts(seq, "hot ");
826                 if (tz->state.passive)
827                         seq_puts(seq, "passive ");
828                 if (tz->state.active)
829                         seq_printf(seq, "active[%d]", tz->state.active_index);
830                 seq_puts(seq, "\n");
831         }
832
833       end:
834         return_VALUE(0);
835 }
836
837 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
838 {
839         return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
840 }
841
842 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
843 {
844         int result = 0;
845         struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
846
847         ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
848
849         if (!tz)
850                 goto end;
851
852         result = acpi_thermal_get_temperature(tz);
853         if (result)
854                 goto end;
855
856         seq_printf(seq, "temperature:             %ld C\n",
857                    KELVIN_TO_CELSIUS(tz->temperature));
858
859       end:
860         return_VALUE(0);
861 }
862
863 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
864 {
865         return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
866 }
867
868 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
869 {
870         struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
871         int i = 0;
872         int j = 0;
873
874         ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
875
876         if (!tz)
877                 goto end;
878
879         if (tz->trips.critical.flags.valid)
880                 seq_printf(seq, "critical (S5):           %ld C\n",
881                            KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
882
883         if (tz->trips.hot.flags.valid)
884                 seq_printf(seq, "hot (S4):                %ld C\n",
885                            KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
886
887         if (tz->trips.passive.flags.valid) {
888                 seq_printf(seq,
889                            "passive:                 %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
890                            KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
891                            tz->trips.passive.tc1, tz->trips.passive.tc2,
892                            tz->trips.passive.tsp);
893                 for (j = 0; j < tz->trips.passive.devices.count; j++) {
894
895                         seq_printf(seq, "0x%p ",
896                                    tz->trips.passive.devices.handles[j]);
897                 }
898                 seq_puts(seq, "\n");
899         }
900
901         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
902                 if (!(tz->trips.active[i].flags.valid))
903                         break;
904                 seq_printf(seq, "active[%d]:               %ld C: devices=",
905                            i,
906                            KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
907                 for (j = 0; j < tz->trips.active[i].devices.count; j++)
908                         seq_printf(seq, "0x%p ",
909                                    tz->trips.active[i].devices.handles[j]);
910                 seq_puts(seq, "\n");
911         }
912
913       end:
914         return_VALUE(0);
915 }
916
917 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
918 {
919         return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
920 }
921
922 static ssize_t
923 acpi_thermal_write_trip_points(struct file *file,
924                                const char __user * buffer,
925                                size_t count, loff_t * ppos)
926 {
927         struct seq_file *m = (struct seq_file *)file->private_data;
928         struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
929
930         char *limit_string;
931         int num, critical, hot, passive;
932         int *active;
933         int i = 0;
934
935         ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
936
937         limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
938         if (!limit_string)
939                 return_VALUE(-ENOMEM);
940
941         memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
942
943         active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
944         if (!active) {
945                 kfree(limit_string);
946                 return_VALUE(-ENOMEM);
947         }
948
949         if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
950                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
951                 count = -EINVAL;
952                 goto end;
953         }
954
955         if (copy_from_user(limit_string, buffer, count)) {
956                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
957                 count = -EFAULT;
958                 goto end;
959         }
960
961         limit_string[count] = '\0';
962
963         num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
964                      &critical, &hot, &passive,
965                      &active[0], &active[1], &active[2], &active[3], &active[4],
966                      &active[5], &active[6], &active[7], &active[8],
967                      &active[9]);
968         if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
969                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
970                 count = -EINVAL;
971                 goto end;
972         }
973
974         tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
975         tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
976         tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
977         for (i = 0; i < num - 3; i++) {
978                 if (!(tz->trips.active[i].flags.valid))
979                         break;
980                 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
981         }
982
983       end:
984         kfree(active);
985         kfree(limit_string);
986         return_VALUE(count);
987 }
988
989 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
990 {
991         struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
992
993         ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
994
995         if (!tz)
996                 goto end;
997
998         if (!tz->flags.cooling_mode) {
999                 seq_puts(seq, "<setting not supported>\n");
1000         }
1001
1002         if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
1003                 seq_printf(seq, "cooling mode:  critical\n");
1004         else
1005                 seq_printf(seq, "cooling mode:  %s\n",
1006                            tz->cooling_mode ? "passive" : "active");
1007
1008       end:
1009         return_VALUE(0);
1010 }
1011
1012 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1013 {
1014         return single_open(file, acpi_thermal_cooling_seq_show,
1015                            PDE(inode)->data);
1016 }
1017
1018 static ssize_t
1019 acpi_thermal_write_cooling_mode(struct file *file,
1020                                 const char __user * buffer,
1021                                 size_t count, loff_t * ppos)
1022 {
1023         struct seq_file *m = (struct seq_file *)file->private_data;
1024         struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1025         int result = 0;
1026         char mode_string[12] = { '\0' };
1027
1028         ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
1029
1030         if (!tz || (count > sizeof(mode_string) - 1))
1031                 return_VALUE(-EINVAL);
1032
1033         if (!tz->flags.cooling_mode)
1034                 return_VALUE(-ENODEV);
1035
1036         if (copy_from_user(mode_string, buffer, count))
1037                 return_VALUE(-EFAULT);
1038
1039         mode_string[count] = '\0';
1040
1041         result = acpi_thermal_set_cooling_mode(tz,
1042                                                simple_strtoul(mode_string, NULL,
1043                                                               0));
1044         if (result)
1045                 return_VALUE(result);
1046
1047         acpi_thermal_check(tz);
1048
1049         return_VALUE(count);
1050 }
1051
1052 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1053 {
1054         struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
1055
1056         ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
1057
1058         if (!tz)
1059                 goto end;
1060
1061         if (!tz->polling_frequency) {
1062                 seq_puts(seq, "<polling disabled>\n");
1063                 goto end;
1064         }
1065
1066         seq_printf(seq, "polling frequency:       %lu seconds\n",
1067                    (tz->polling_frequency / 10));
1068
1069       end:
1070         return_VALUE(0);
1071 }
1072
1073 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1074 {
1075         return single_open(file, acpi_thermal_polling_seq_show,
1076                            PDE(inode)->data);
1077 }
1078
1079 static ssize_t
1080 acpi_thermal_write_polling(struct file *file,
1081                            const char __user * buffer,
1082                            size_t count, loff_t * ppos)
1083 {
1084         struct seq_file *m = (struct seq_file *)file->private_data;
1085         struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1086         int result = 0;
1087         char polling_string[12] = { '\0' };
1088         int seconds = 0;
1089
1090         ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
1091
1092         if (!tz || (count > sizeof(polling_string) - 1))
1093                 return_VALUE(-EINVAL);
1094
1095         if (copy_from_user(polling_string, buffer, count))
1096                 return_VALUE(-EFAULT);
1097
1098         polling_string[count] = '\0';
1099
1100         seconds = simple_strtoul(polling_string, NULL, 0);
1101
1102         result = acpi_thermal_set_polling(tz, seconds);
1103         if (result)
1104                 return_VALUE(result);
1105
1106         acpi_thermal_check(tz);
1107
1108         return_VALUE(count);
1109 }
1110
1111 static int acpi_thermal_add_fs(struct acpi_device *device)
1112 {
1113         struct proc_dir_entry *entry = NULL;
1114
1115         ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
1116
1117         if (!acpi_device_dir(device)) {
1118                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1119                                                      acpi_thermal_dir);
1120                 if (!acpi_device_dir(device))
1121                         return_VALUE(-ENODEV);
1122                 acpi_device_dir(device)->owner = THIS_MODULE;
1123         }
1124
1125         /* 'state' [R] */
1126         entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1127                                   S_IRUGO, acpi_device_dir(device));
1128         if (!entry)
1129                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1130                                   "Unable to create '%s' fs entry\n",
1131                                   ACPI_THERMAL_FILE_STATE));
1132         else {
1133                 entry->proc_fops = &acpi_thermal_state_fops;
1134                 entry->data = acpi_driver_data(device);
1135                 entry->owner = THIS_MODULE;
1136         }
1137
1138         /* 'temperature' [R] */
1139         entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1140                                   S_IRUGO, acpi_device_dir(device));
1141         if (!entry)
1142                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1143                                   "Unable to create '%s' fs entry\n",
1144                                   ACPI_THERMAL_FILE_TEMPERATURE));
1145         else {
1146                 entry->proc_fops = &acpi_thermal_temp_fops;
1147                 entry->data = acpi_driver_data(device);
1148                 entry->owner = THIS_MODULE;
1149         }
1150
1151         /* 'trip_points' [R/W] */
1152         entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1153                                   S_IFREG | S_IRUGO | S_IWUSR,
1154                                   acpi_device_dir(device));
1155         if (!entry)
1156                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1157                                   "Unable to create '%s' fs entry\n",
1158                                   ACPI_THERMAL_FILE_TRIP_POINTS));
1159         else {
1160                 entry->proc_fops = &acpi_thermal_trip_fops;
1161                 entry->data = acpi_driver_data(device);
1162                 entry->owner = THIS_MODULE;
1163         }
1164
1165         /* 'cooling_mode' [R/W] */
1166         entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1167                                   S_IFREG | S_IRUGO | S_IWUSR,
1168                                   acpi_device_dir(device));
1169         if (!entry)
1170                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1171                                   "Unable to create '%s' fs entry\n",
1172                                   ACPI_THERMAL_FILE_COOLING_MODE));
1173         else {
1174                 entry->proc_fops = &acpi_thermal_cooling_fops;
1175                 entry->data = acpi_driver_data(device);
1176                 entry->owner = THIS_MODULE;
1177         }
1178
1179         /* 'polling_frequency' [R/W] */
1180         entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1181                                   S_IFREG | S_IRUGO | S_IWUSR,
1182                                   acpi_device_dir(device));
1183         if (!entry)
1184                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1185                                   "Unable to create '%s' fs entry\n",
1186                                   ACPI_THERMAL_FILE_POLLING_FREQ));
1187         else {
1188                 entry->proc_fops = &acpi_thermal_polling_fops;
1189                 entry->data = acpi_driver_data(device);
1190                 entry->owner = THIS_MODULE;
1191         }
1192
1193         return_VALUE(0);
1194 }
1195
1196 static int acpi_thermal_remove_fs(struct acpi_device *device)
1197 {
1198         ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
1199
1200         if (acpi_device_dir(device)) {
1201                 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1202                                   acpi_device_dir(device));
1203                 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1204                                   acpi_device_dir(device));
1205                 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1206                                   acpi_device_dir(device));
1207                 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1208                                   acpi_device_dir(device));
1209                 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1210                                   acpi_device_dir(device));
1211                 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1212                 acpi_device_dir(device) = NULL;
1213         }
1214
1215         return_VALUE(0);
1216 }
1217
1218 /* --------------------------------------------------------------------------
1219                                  Driver Interface
1220    -------------------------------------------------------------------------- */
1221
1222 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1223 {
1224         struct acpi_thermal *tz = (struct acpi_thermal *)data;
1225         struct acpi_device *device = NULL;
1226
1227         ACPI_FUNCTION_TRACE("acpi_thermal_notify");
1228
1229         if (!tz)
1230                 return_VOID;
1231
1232         if (acpi_bus_get_device(tz->handle, &device))
1233                 return_VOID;
1234
1235         switch (event) {
1236         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1237                 acpi_thermal_check(tz);
1238                 break;
1239         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1240                 acpi_thermal_get_trip_points(tz);
1241                 acpi_thermal_check(tz);
1242                 acpi_bus_generate_event(device, event, 0);
1243                 break;
1244         case ACPI_THERMAL_NOTIFY_DEVICES:
1245                 if (tz->flags.devices)
1246                         acpi_thermal_get_devices(tz);
1247                 acpi_bus_generate_event(device, event, 0);
1248                 break;
1249         default:
1250                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1251                                   "Unsupported event [0x%x]\n", event));
1252                 break;
1253         }
1254
1255         return_VOID;
1256 }
1257
1258 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1259 {
1260         int result = 0;
1261
1262         ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
1263
1264         if (!tz)
1265                 return_VALUE(-EINVAL);
1266
1267         /* Get temperature [_TMP] (required) */
1268         result = acpi_thermal_get_temperature(tz);
1269         if (result)
1270                 return_VALUE(result);
1271
1272         /* Get trip points [_CRT, _PSV, etc.] (required) */
1273         result = acpi_thermal_get_trip_points(tz);
1274         if (result)
1275                 return_VALUE(result);
1276
1277         /* Set the cooling mode [_SCP] to active cooling (default) */
1278         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1279         if (!result)
1280                 tz->flags.cooling_mode = 1;
1281         else {
1282                 /* Oh,we have not _SCP method.
1283                    Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
1284                 tz->flags.cooling_mode = 0;
1285                 if (tz->trips.active[0].flags.valid
1286                     && tz->trips.passive.flags.valid) {
1287                         if (tz->trips.passive.temperature >
1288                             tz->trips.active[0].temperature)
1289                                 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1290                         else
1291                                 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
1292                 } else if (!tz->trips.active[0].flags.valid
1293                            && tz->trips.passive.flags.valid) {
1294                         tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
1295                 } else if (tz->trips.active[0].flags.valid
1296                            && !tz->trips.passive.flags.valid) {
1297                         tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1298                 } else {
1299                         /* _ACx and _PSV are optional, but _CRT is required */
1300                         tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1301                 }
1302         }
1303
1304         /* Get default polling frequency [_TZP] (optional) */
1305         if (tzp)
1306                 tz->polling_frequency = tzp;
1307         else
1308                 acpi_thermal_get_polling_frequency(tz);
1309
1310         /* Get devices in this thermal zone [_TZD] (optional) */
1311         result = acpi_thermal_get_devices(tz);
1312         if (!result)
1313                 tz->flags.devices = 1;
1314
1315         return_VALUE(0);
1316 }
1317
1318 static int acpi_thermal_add(struct acpi_device *device)
1319 {
1320         int result = 0;
1321         acpi_status status = AE_OK;
1322         struct acpi_thermal *tz = NULL;
1323
1324         ACPI_FUNCTION_TRACE("acpi_thermal_add");
1325
1326         if (!device)
1327                 return_VALUE(-EINVAL);
1328
1329         tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1330         if (!tz)
1331                 return_VALUE(-ENOMEM);
1332         memset(tz, 0, sizeof(struct acpi_thermal));
1333
1334         tz->handle = device->handle;
1335         strcpy(tz->name, device->pnp.bus_id);
1336         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1337         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1338         acpi_driver_data(device) = tz;
1339
1340         result = acpi_thermal_get_info(tz);
1341         if (result)
1342                 goto end;
1343
1344         result = acpi_thermal_add_fs(device);
1345         if (result)
1346                 goto end;
1347
1348         init_timer(&tz->timer);
1349
1350         acpi_thermal_check(tz);
1351
1352         status = acpi_install_notify_handler(tz->handle,
1353                                              ACPI_DEVICE_NOTIFY,
1354                                              acpi_thermal_notify, tz);
1355         if (ACPI_FAILURE(status)) {
1356                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1357                                   "Error installing notify handler\n"));
1358                 result = -ENODEV;
1359                 goto end;
1360         }
1361
1362         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1363                acpi_device_name(device), acpi_device_bid(device),
1364                KELVIN_TO_CELSIUS(tz->temperature));
1365
1366       end:
1367         if (result) {
1368                 acpi_thermal_remove_fs(device);
1369                 kfree(tz);
1370         }
1371
1372         return_VALUE(result);
1373 }
1374
1375 static int acpi_thermal_remove(struct acpi_device *device, int type)
1376 {
1377         acpi_status status = AE_OK;
1378         struct acpi_thermal *tz = NULL;
1379
1380         ACPI_FUNCTION_TRACE("acpi_thermal_remove");
1381
1382         if (!device || !acpi_driver_data(device))
1383                 return_VALUE(-EINVAL);
1384
1385         tz = (struct acpi_thermal *)acpi_driver_data(device);
1386
1387         /* avoid timer adding new defer task */
1388         tz->zombie = 1;
1389         /* wait for running timer (on other CPUs) finish */
1390         del_timer_sync(&(tz->timer));
1391         /* synchronize deferred task */
1392         acpi_os_wait_events_complete(NULL);
1393         /* deferred task may reinsert timer */
1394         del_timer_sync(&(tz->timer));
1395
1396         status = acpi_remove_notify_handler(tz->handle,
1397                                             ACPI_DEVICE_NOTIFY,
1398                                             acpi_thermal_notify);
1399         if (ACPI_FAILURE(status))
1400                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1401                                   "Error removing notify handler\n"));
1402
1403         /* Terminate policy */
1404         if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1405                 tz->trips.passive.flags.enabled = 0;
1406                 acpi_thermal_passive(tz);
1407         }
1408         if (tz->trips.active[0].flags.valid
1409             && tz->trips.active[0].flags.enabled) {
1410                 tz->trips.active[0].flags.enabled = 0;
1411                 acpi_thermal_active(tz);
1412         }
1413
1414         acpi_thermal_remove_fs(device);
1415
1416         kfree(tz);
1417         return_VALUE(0);
1418 }
1419
1420 static int __init acpi_thermal_init(void)
1421 {
1422         int result = 0;
1423
1424         ACPI_FUNCTION_TRACE("acpi_thermal_init");
1425
1426         acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1427         if (!acpi_thermal_dir)
1428                 return_VALUE(-ENODEV);
1429         acpi_thermal_dir->owner = THIS_MODULE;
1430
1431         result = acpi_bus_register_driver(&acpi_thermal_driver);
1432         if (result < 0) {
1433                 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1434                 return_VALUE(-ENODEV);
1435         }
1436
1437         return_VALUE(0);
1438 }
1439
1440 static void __exit acpi_thermal_exit(void)
1441 {
1442         ACPI_FUNCTION_TRACE("acpi_thermal_exit");
1443
1444         acpi_bus_unregister_driver(&acpi_thermal_driver);
1445
1446         remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1447
1448         return_VOID;
1449 }
1450
1451 module_init(acpi_thermal_init);
1452 module_exit(acpi_thermal_exit);