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