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