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