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