ACPI: thermal fixup
[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/dmi.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
39 #include <linux/proc_fs.h>
40 #include <linux/timer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kmod.h>
43 #include <linux/seq_file.h>
44 #include <linux/reboot.h>
45 #include <asm/uaccess.h>
46 #include <linux/thermal.h>
47 #include <acpi/acpi_bus.h>
48 #include <acpi/acpi_drivers.h>
49
50 #define ACPI_THERMAL_COMPONENT          0x04000000
51 #define ACPI_THERMAL_CLASS              "thermal_zone"
52 #define ACPI_THERMAL_DEVICE_NAME        "Thermal Zone"
53 #define ACPI_THERMAL_FILE_STATE         "state"
54 #define ACPI_THERMAL_FILE_TEMPERATURE   "temperature"
55 #define ACPI_THERMAL_FILE_TRIP_POINTS   "trip_points"
56 #define ACPI_THERMAL_FILE_COOLING_MODE  "cooling_mode"
57 #define ACPI_THERMAL_FILE_POLLING_FREQ  "polling_frequency"
58 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
59 #define ACPI_THERMAL_NOTIFY_THRESHOLDS  0x81
60 #define ACPI_THERMAL_NOTIFY_DEVICES     0x82
61 #define ACPI_THERMAL_NOTIFY_CRITICAL    0xF0
62 #define ACPI_THERMAL_NOTIFY_HOT         0xF1
63 #define ACPI_THERMAL_MODE_ACTIVE        0x00
64
65 #define ACPI_THERMAL_MAX_ACTIVE 10
66 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
67
68 #define _COMPONENT              ACPI_THERMAL_COMPONENT
69 ACPI_MODULE_NAME("thermal");
70
71 MODULE_AUTHOR("Paul Diefenbaugh");
72 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
73 MODULE_LICENSE("GPL");
74
75 static int act;
76 module_param(act, int, 0644);
77 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
78
79 static int crt;
80 module_param(crt, int, 0644);
81 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
82
83 static int tzp;
84 module_param(tzp, int, 0444);
85 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
86
87 static int nocrt;
88 module_param(nocrt, int, 0);
89 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
90
91 static int off;
92 module_param(off, int, 0);
93 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
94
95 static int psv;
96 module_param(psv, int, 0644);
97 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
98
99 static int acpi_thermal_add(struct acpi_device *device);
100 static int acpi_thermal_remove(struct acpi_device *device, int type);
101 static int acpi_thermal_resume(struct acpi_device *device);
102 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
103 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
104 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
105 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
106 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
107                                                const char __user *, size_t,
108                                                loff_t *);
109 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
110 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
111                                           size_t, loff_t *);
112
113 static const struct acpi_device_id  thermal_device_ids[] = {
114         {ACPI_THERMAL_HID, 0},
115         {"", 0},
116 };
117 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
118
119 static struct acpi_driver acpi_thermal_driver = {
120         .name = "thermal",
121         .class = ACPI_THERMAL_CLASS,
122         .ids = thermal_device_ids,
123         .ops = {
124                 .add = acpi_thermal_add,
125                 .remove = acpi_thermal_remove,
126                 .resume = acpi_thermal_resume,
127                 },
128 };
129
130 struct acpi_thermal_state {
131         u8 critical:1;
132         u8 hot:1;
133         u8 passive:1;
134         u8 active:1;
135         u8 reserved:4;
136         int active_index;
137 };
138
139 struct acpi_thermal_state_flags {
140         u8 valid:1;
141         u8 enabled:1;
142         u8 reserved:6;
143 };
144
145 struct acpi_thermal_critical {
146         struct acpi_thermal_state_flags flags;
147         unsigned long temperature;
148 };
149
150 struct acpi_thermal_hot {
151         struct acpi_thermal_state_flags flags;
152         unsigned long temperature;
153 };
154
155 struct acpi_thermal_passive {
156         struct acpi_thermal_state_flags flags;
157         unsigned long temperature;
158         unsigned long tc1;
159         unsigned long tc2;
160         unsigned long tsp;
161         struct acpi_handle_list devices;
162 };
163
164 struct acpi_thermal_active {
165         struct acpi_thermal_state_flags flags;
166         unsigned long temperature;
167         struct acpi_handle_list devices;
168 };
169
170 struct acpi_thermal_trips {
171         struct acpi_thermal_critical critical;
172         struct acpi_thermal_hot hot;
173         struct acpi_thermal_passive passive;
174         struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
175 };
176
177 struct acpi_thermal_flags {
178         u8 cooling_mode:1;      /* _SCP */
179         u8 devices:1;           /* _TZD */
180         u8 reserved:6;
181 };
182
183 struct acpi_thermal {
184         struct acpi_device * device;
185         acpi_bus_id name;
186         unsigned long temperature;
187         unsigned long last_temperature;
188         unsigned long polling_frequency;
189         volatile u8 zombie;
190         struct acpi_thermal_flags flags;
191         struct acpi_thermal_state state;
192         struct acpi_thermal_trips trips;
193         struct acpi_handle_list devices;
194         struct timer_list timer;
195         struct thermal_zone_device *thermal_zone;
196         int tz_enabled;
197         struct mutex lock;
198 };
199
200 static const struct file_operations acpi_thermal_state_fops = {
201         .open = acpi_thermal_state_open_fs,
202         .read = seq_read,
203         .llseek = seq_lseek,
204         .release = single_release,
205 };
206
207 static const struct file_operations acpi_thermal_temp_fops = {
208         .open = acpi_thermal_temp_open_fs,
209         .read = seq_read,
210         .llseek = seq_lseek,
211         .release = single_release,
212 };
213
214 static const struct file_operations acpi_thermal_trip_fops = {
215         .open = acpi_thermal_trip_open_fs,
216         .read = seq_read,
217         .llseek = seq_lseek,
218         .release = single_release,
219 };
220
221 static const struct file_operations acpi_thermal_cooling_fops = {
222         .open = acpi_thermal_cooling_open_fs,
223         .read = seq_read,
224         .write = acpi_thermal_write_cooling_mode,
225         .llseek = seq_lseek,
226         .release = single_release,
227 };
228
229 static const struct file_operations acpi_thermal_polling_fops = {
230         .open = acpi_thermal_polling_open_fs,
231         .read = seq_read,
232         .write = acpi_thermal_write_polling,
233         .llseek = seq_lseek,
234         .release = single_release,
235 };
236
237 /* --------------------------------------------------------------------------
238                              Thermal Zone Management
239    -------------------------------------------------------------------------- */
240
241 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
242 {
243         acpi_status status = AE_OK;
244
245
246         if (!tz)
247                 return -EINVAL;
248
249         tz->last_temperature = tz->temperature;
250
251         status =
252             acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
253         if (ACPI_FAILURE(status))
254                 return -ENODEV;
255
256         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
257                           tz->temperature));
258
259         return 0;
260 }
261
262 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
263 {
264         acpi_status status = AE_OK;
265
266
267         if (!tz)
268                 return -EINVAL;
269
270         status =
271             acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
272                                   &tz->polling_frequency);
273         if (ACPI_FAILURE(status))
274                 return -ENODEV;
275
276         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
277                           tz->polling_frequency));
278
279         return 0;
280 }
281
282 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
283 {
284
285         if (!tz)
286                 return -EINVAL;
287
288         tz->polling_frequency = seconds * 10;   /* Convert value to deci-seconds */
289
290         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
291                           "Polling frequency set to %lu seconds\n",
292                           tz->polling_frequency/10));
293
294         return 0;
295 }
296
297 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
298 {
299         acpi_status status = AE_OK;
300         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
301         struct acpi_object_list arg_list = { 1, &arg0 };
302         acpi_handle handle = NULL;
303
304
305         if (!tz)
306                 return -EINVAL;
307
308         status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
309         if (ACPI_FAILURE(status)) {
310                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
311                 return -ENODEV;
312         }
313
314         arg0.integer.value = mode;
315
316         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
317         if (ACPI_FAILURE(status))
318                 return -ENODEV;
319
320         return 0;
321 }
322
323 #define ACPI_TRIPS_CRITICAL     0x01
324 #define ACPI_TRIPS_HOT          0x02
325 #define ACPI_TRIPS_PASSIVE      0x04
326 #define ACPI_TRIPS_ACTIVE       0x08
327 #define ACPI_TRIPS_DEVICES      0x10
328
329 #define ACPI_TRIPS_REFRESH_THRESHOLDS   (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
330 #define ACPI_TRIPS_REFRESH_DEVICES      ACPI_TRIPS_DEVICES
331
332 #define ACPI_TRIPS_INIT      (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT |    \
333                               ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE |  \
334                               ACPI_TRIPS_DEVICES)
335
336 /*
337  * This exception is thrown out in two cases:
338  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
339  *   when re-evaluating the AML code.
340  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
341  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
342  */
343 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str)        \
344 do {    \
345         if (flags != ACPI_TRIPS_INIT)   \
346                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,      \
347                 "ACPI thermal trip point %s changed\n"  \
348                 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
349 } while (0)
350
351 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
352 {
353         acpi_status status = AE_OK;
354         struct acpi_handle_list devices;
355         int valid = 0;
356         int i;
357
358         /* Critical Shutdown (required) */
359         if (flag & ACPI_TRIPS_CRITICAL) {
360                 status = acpi_evaluate_integer(tz->device->handle,
361                                 "_CRT", NULL, &tz->trips.critical.temperature);
362                 if (ACPI_FAILURE(status)) {
363                         tz->trips.critical.flags.valid = 0;
364                         ACPI_EXCEPTION((AE_INFO, status,
365                                         "No critical threshold"));
366                         return -ENODEV;
367                 } else {
368                         tz->trips.critical.flags.valid = 1;
369                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
370                                         "Found critical threshold [%lu]\n",
371                                         tz->trips.critical.temperature));
372                 }
373                 if (tz->trips.critical.flags.valid == 1) {
374                         if (crt == -1) {
375                                 tz->trips.critical.flags.valid = 0;
376                         } else if (crt > 0) {
377                                 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
378                                 /*
379                                  * Allow override to lower critical threshold
380                                  */
381                                 if (crt_k < tz->trips.critical.temperature)
382                                         tz->trips.critical.temperature = crt_k;
383                         }
384                 }
385         }
386
387         /* Critical Sleep (optional) */
388         if (flag & ACPI_TRIPS_HOT) {
389                 status = acpi_evaluate_integer(tz->device->handle,
390                                 "_HOT", NULL, &tz->trips.hot.temperature);
391                 if (ACPI_FAILURE(status)) {
392                         tz->trips.hot.flags.valid = 0;
393                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
394                                         "No hot threshold\n"));
395                 } else {
396                         tz->trips.hot.flags.valid = 1;
397                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
398                                         "Found hot threshold [%lu]\n",
399                                         tz->trips.critical.temperature));
400                 }
401         }
402
403         /* Passive (optional) */
404         if (flag & ACPI_TRIPS_PASSIVE) {
405                 valid = tz->trips.passive.flags.valid;
406                 if (psv == -1) {
407                         status = AE_SUPPORT;
408                 } else if (psv > 0) {
409                         tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
410                         status = AE_OK;
411                 } else {
412                         status = acpi_evaluate_integer(tz->device->handle,
413                                 "_PSV", NULL, &tz->trips.passive.temperature);
414                 }
415
416                 if (ACPI_FAILURE(status))
417                         tz->trips.passive.flags.valid = 0;
418                 else {
419                         tz->trips.passive.flags.valid = 1;
420                         if (flag == ACPI_TRIPS_INIT) {
421                                 status = acpi_evaluate_integer(
422                                                 tz->device->handle, "_TC1",
423                                                 NULL, &tz->trips.passive.tc1);
424                                 if (ACPI_FAILURE(status))
425                                         tz->trips.passive.flags.valid = 0;
426                                 status = acpi_evaluate_integer(
427                                                 tz->device->handle, "_TC2",
428                                                 NULL, &tz->trips.passive.tc2);
429                                 if (ACPI_FAILURE(status))
430                                         tz->trips.passive.flags.valid = 0;
431                                 status = acpi_evaluate_integer(
432                                                 tz->device->handle, "_TSP",
433                                                 NULL, &tz->trips.passive.tsp);
434                                 if (ACPI_FAILURE(status))
435                                         tz->trips.passive.flags.valid = 0;
436                         }
437                 }
438         }
439         if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
440                 memset(&devices, 0, sizeof(struct acpi_handle_list));
441                 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
442                                                         NULL, &devices);
443                 if (ACPI_FAILURE(status))
444                         tz->trips.passive.flags.valid = 0;
445                 else
446                         tz->trips.passive.flags.valid = 1;
447
448                 if (memcmp(&tz->trips.passive.devices, &devices,
449                                 sizeof(struct acpi_handle_list))) {
450                         memcpy(&tz->trips.passive.devices, &devices,
451                                 sizeof(struct acpi_handle_list));
452                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
453                 }
454         }
455         if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
456                 if (valid != tz->trips.passive.flags.valid)
457                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
458         }
459
460         /* Active (optional) */
461         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
462                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
463                 valid = tz->trips.active[i].flags.valid;
464
465                 if (act == -1)
466                         break; /* disable all active trip points */
467
468                 if (flag & ACPI_TRIPS_ACTIVE) {
469                         status = acpi_evaluate_integer(tz->device->handle,
470                                 name, NULL, &tz->trips.active[i].temperature);
471                         if (ACPI_FAILURE(status)) {
472                                 tz->trips.active[i].flags.valid = 0;
473                                 if (i == 0)
474                                         break;
475                                 if (act <= 0)
476                                         break;
477                                 if (i == 1)
478                                         tz->trips.active[0].temperature =
479                                                 CELSIUS_TO_KELVIN(act);
480                                 else
481                                         /*
482                                          * Don't allow override higher than
483                                          * the next higher trip point
484                                          */
485                                         tz->trips.active[i - 1].temperature =
486                                                 (tz->trips.active[i - 2].temperature <
487                                                 CELSIUS_TO_KELVIN(act) ?
488                                                 tz->trips.active[i - 2].temperature :
489                                                 CELSIUS_TO_KELVIN(act));
490                                 break;
491                         } else
492                                 tz->trips.active[i].flags.valid = 1;
493                 }
494
495                 name[2] = 'L';
496                 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
497                         memset(&devices, 0, sizeof(struct acpi_handle_list));
498                         status = acpi_evaluate_reference(tz->device->handle,
499                                                 name, NULL, &devices);
500                         if (ACPI_FAILURE(status))
501                                 tz->trips.active[i].flags.valid = 0;
502                         else
503                                 tz->trips.active[i].flags.valid = 1;
504
505                         if (memcmp(&tz->trips.active[i].devices, &devices,
506                                         sizeof(struct acpi_handle_list))) {
507                                 memcpy(&tz->trips.active[i].devices, &devices,
508                                         sizeof(struct acpi_handle_list));
509                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
510                         }
511                 }
512                 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
513                         if (valid != tz->trips.active[i].flags.valid)
514                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
515
516                 if (!tz->trips.active[i].flags.valid)
517                         break;
518         }
519
520         if (flag & ACPI_TRIPS_DEVICES) {
521                 memset(&devices, 0, sizeof(struct acpi_handle_list));
522                 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
523                                                 NULL, &devices);
524                 if (memcmp(&tz->devices, &devices,
525                                 sizeof(struct acpi_handle_list))) {
526                         memcpy(&tz->devices, &devices,
527                                 sizeof(struct acpi_handle_list));
528                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
529                 }
530         }
531
532         return 0;
533 }
534
535 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
536 {
537         return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
538 }
539
540 static int acpi_thermal_critical(struct acpi_thermal *tz)
541 {
542         if (!tz || !tz->trips.critical.flags.valid || nocrt)
543                 return -EINVAL;
544
545         if (tz->temperature >= tz->trips.critical.temperature) {
546                 printk(KERN_WARNING PREFIX "Critical trip point\n");
547                 tz->trips.critical.flags.enabled = 1;
548         } else if (tz->trips.critical.flags.enabled)
549                 tz->trips.critical.flags.enabled = 0;
550
551         printk(KERN_EMERG
552                "Critical temperature reached (%ld C), shutting down.\n",
553                KELVIN_TO_CELSIUS(tz->temperature));
554         acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
555                                 tz->trips.critical.flags.enabled);
556         acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
557                                           tz->device->dev.bus_id,
558                                           ACPI_THERMAL_NOTIFY_CRITICAL,
559                                           tz->trips.critical.flags.enabled);
560
561         orderly_poweroff(true);
562
563         return 0;
564 }
565
566 static int acpi_thermal_hot(struct acpi_thermal *tz)
567 {
568         if (!tz || !tz->trips.hot.flags.valid || nocrt)
569                 return -EINVAL;
570
571         if (tz->temperature >= tz->trips.hot.temperature) {
572                 printk(KERN_WARNING PREFIX "Hot trip point\n");
573                 tz->trips.hot.flags.enabled = 1;
574         } else if (tz->trips.hot.flags.enabled)
575                 tz->trips.hot.flags.enabled = 0;
576
577         acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
578                                 tz->trips.hot.flags.enabled);
579         acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
580                                           tz->device->dev.bus_id,
581                                           ACPI_THERMAL_NOTIFY_HOT,
582                                           tz->trips.hot.flags.enabled);
583
584         /* TBD: Call user-mode "sleep(S4)" function */
585
586         return 0;
587 }
588
589 static void acpi_thermal_passive(struct acpi_thermal *tz)
590 {
591         int result = 1;
592         struct acpi_thermal_passive *passive = NULL;
593         int trend = 0;
594         int i = 0;
595
596
597         if (!tz || !tz->trips.passive.flags.valid)
598                 return;
599
600         passive = &(tz->trips.passive);
601
602         /*
603          * Above Trip?
604          * -----------
605          * Calculate the thermal trend (using the passive cooling equation)
606          * and modify the performance limit for all passive cooling devices
607          * accordingly.  Note that we assume symmetry.
608          */
609         if (tz->temperature >= passive->temperature) {
610                 trend =
611                     (passive->tc1 * (tz->temperature - tz->last_temperature)) +
612                     (passive->tc2 * (tz->temperature - passive->temperature));
613                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
614                                   "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
615                                   trend, passive->tc1, tz->temperature,
616                                   tz->last_temperature, passive->tc2,
617                                   tz->temperature, passive->temperature));
618                 passive->flags.enabled = 1;
619                 /* Heating up? */
620                 if (trend > 0)
621                         for (i = 0; i < passive->devices.count; i++)
622                                 acpi_processor_set_thermal_limit(passive->
623                                                                  devices.
624                                                                  handles[i],
625                                                                  ACPI_PROCESSOR_LIMIT_INCREMENT);
626                 /* Cooling off? */
627                 else if (trend < 0) {
628                         for (i = 0; i < passive->devices.count; i++)
629                                 /*
630                                  * assume that we are on highest
631                                  * freq/lowest thrott and can leave
632                                  * passive mode, even in error case
633                                  */
634                                 if (!acpi_processor_set_thermal_limit
635                                     (passive->devices.handles[i],
636                                      ACPI_PROCESSOR_LIMIT_DECREMENT))
637                                         result = 0;
638                         /*
639                          * Leave cooling mode, even if the temp might
640                          * higher than trip point This is because some
641                          * machines might have long thermal polling
642                          * frequencies (tsp) defined. We will fall back
643                          * into passive mode in next cycle (probably quicker)
644                          */
645                         if (result) {
646                                 passive->flags.enabled = 0;
647                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
648                                                   "Disabling passive cooling, still above threshold,"
649                                                   " but we are cooling down\n"));
650                         }
651                 }
652                 return;
653         }
654
655         /*
656          * Below Trip?
657          * -----------
658          * Implement passive cooling hysteresis to slowly increase performance
659          * and avoid thrashing around the passive trip point.  Note that we
660          * assume symmetry.
661          */
662         if (!passive->flags.enabled)
663                 return;
664         for (i = 0; i < passive->devices.count; i++)
665                 if (!acpi_processor_set_thermal_limit
666                     (passive->devices.handles[i],
667                      ACPI_PROCESSOR_LIMIT_DECREMENT))
668                         result = 0;
669         if (result) {
670                 passive->flags.enabled = 0;
671                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
672                                   "Disabling passive cooling (zone is cool)\n"));
673         }
674 }
675
676 static void acpi_thermal_active(struct acpi_thermal *tz)
677 {
678         int result = 0;
679         struct acpi_thermal_active *active = NULL;
680         int i = 0;
681         int j = 0;
682         unsigned long maxtemp = 0;
683
684
685         if (!tz)
686                 return;
687
688         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
689                 active = &(tz->trips.active[i]);
690                 if (!active || !active->flags.valid)
691                         break;
692                 if (tz->temperature >= active->temperature) {
693                         /*
694                          * Above Threshold?
695                          * ----------------
696                          * If not already enabled, turn ON all cooling devices
697                          * associated with this active threshold.
698                          */
699                         if (active->temperature > maxtemp)
700                                 tz->state.active_index = i;
701                         maxtemp = active->temperature;
702                         if (active->flags.enabled)
703                                 continue;
704                         for (j = 0; j < active->devices.count; j++) {
705                                 result =
706                                     acpi_bus_set_power(active->devices.
707                                                        handles[j],
708                                                        ACPI_STATE_D0);
709                                 if (result) {
710                                         printk(KERN_WARNING PREFIX
711                                                       "Unable to turn cooling device [%p] 'on'\n",
712                                                       active->devices.
713                                                       handles[j]);
714                                         continue;
715                                 }
716                                 active->flags.enabled = 1;
717                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
718                                                   "Cooling device [%p] now 'on'\n",
719                                                   active->devices.handles[j]));
720                         }
721                         continue;
722                 }
723                 if (!active->flags.enabled)
724                         continue;
725                 /*
726                  * Below Threshold?
727                  * ----------------
728                  * Turn OFF all cooling devices associated with this
729                  * threshold.
730                  */
731                 for (j = 0; j < active->devices.count; j++) {
732                         result = acpi_bus_set_power(active->devices.handles[j],
733                                                     ACPI_STATE_D3);
734                         if (result) {
735                                 printk(KERN_WARNING PREFIX
736                                               "Unable to turn cooling device [%p] 'off'\n",
737                                               active->devices.handles[j]);
738                                 continue;
739                         }
740                         active->flags.enabled = 0;
741                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
742                                           "Cooling device [%p] now 'off'\n",
743                                           active->devices.handles[j]));
744                 }
745         }
746 }
747
748 static void acpi_thermal_check(void *context);
749
750 static void acpi_thermal_run(unsigned long data)
751 {
752         struct acpi_thermal *tz = (struct acpi_thermal *)data;
753         if (!tz->zombie)
754                 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
755 }
756
757 static void acpi_thermal_check(void *data)
758 {
759         int result = 0;
760         struct acpi_thermal *tz = data;
761         unsigned long sleep_time = 0;
762         unsigned long timeout_jiffies = 0;
763         int i = 0;
764         struct acpi_thermal_state state;
765
766
767         if (!tz) {
768                 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
769                 return;
770         }
771
772         /* Check if someone else is already running */
773         if (!mutex_trylock(&tz->lock))
774                 return;
775
776         state = tz->state;
777
778         result = acpi_thermal_get_temperature(tz);
779         if (result)
780                 goto unlock;
781
782         if (!tz->tz_enabled)
783                 goto unlock;
784
785         memset(&tz->state, 0, sizeof(tz->state));
786
787         /*
788          * Check Trip Points
789          * -----------------
790          * Compare the current temperature to the trip point values to see
791          * if we've entered one of the thermal policy states.  Note that
792          * this function determines when a state is entered, but the 
793          * individual policy decides when it is exited (e.g. hysteresis).
794          */
795         if (tz->trips.critical.flags.valid)
796                 state.critical |=
797                     (tz->temperature >= tz->trips.critical.temperature);
798         if (tz->trips.hot.flags.valid)
799                 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
800         if (tz->trips.passive.flags.valid)
801                 state.passive |=
802                     (tz->temperature >= tz->trips.passive.temperature);
803         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
804                 if (tz->trips.active[i].flags.valid)
805                         state.active |=
806                             (tz->temperature >=
807                              tz->trips.active[i].temperature);
808
809         /*
810          * Invoke Policy
811          * -------------
812          * Separated from the above check to allow individual policy to 
813          * determine when to exit a given state.
814          */
815         if (state.critical)
816                 acpi_thermal_critical(tz);
817         if (state.hot)
818                 acpi_thermal_hot(tz);
819         if (state.passive)
820                 acpi_thermal_passive(tz);
821         if (state.active)
822                 acpi_thermal_active(tz);
823
824         /*
825          * Calculate State
826          * ---------------
827          * Again, separated from the above two to allow independent policy
828          * decisions.
829          */
830         tz->state.critical = tz->trips.critical.flags.enabled;
831         tz->state.hot = tz->trips.hot.flags.enabled;
832         tz->state.passive = tz->trips.passive.flags.enabled;
833         tz->state.active = 0;
834         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
835                 tz->state.active |= tz->trips.active[i].flags.enabled;
836
837         /*
838          * Calculate Sleep Time
839          * --------------------
840          * If we're in the passive state, use _TSP's value.  Otherwise
841          * use the default polling frequency (e.g. _TZP).  If no polling
842          * frequency is specified then we'll wait forever (at least until
843          * a thermal event occurs).  Note that _TSP and _TZD values are
844          * given in 1/10th seconds (we must covert to milliseconds).
845          */
846         if (tz->state.passive) {
847                 sleep_time = tz->trips.passive.tsp * 100;
848                 timeout_jiffies =  jiffies + (HZ * sleep_time) / 1000;
849         } else if (tz->polling_frequency > 0) {
850                 sleep_time = tz->polling_frequency * 100;
851                 timeout_jiffies =  round_jiffies(jiffies + (HZ * sleep_time) / 1000);
852         }
853
854         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
855                           tz->name, tz->temperature, sleep_time));
856
857         /*
858          * Schedule Next Poll
859          * ------------------
860          */
861         if (!sleep_time) {
862                 if (timer_pending(&(tz->timer)))
863                         del_timer(&(tz->timer));
864         } else {
865                 if (timer_pending(&(tz->timer)))
866                         mod_timer(&(tz->timer), timeout_jiffies);
867                 else {
868                         tz->timer.data = (unsigned long)tz;
869                         tz->timer.function = acpi_thermal_run;
870                         tz->timer.expires = timeout_jiffies;
871                         add_timer(&(tz->timer));
872                 }
873         }
874       unlock:
875         mutex_unlock(&tz->lock);
876 }
877
878 /* sys I/F for generic thermal sysfs support */
879 static int thermal_get_temp(struct thermal_zone_device *thermal, char *buf)
880 {
881         struct acpi_thermal *tz = thermal->devdata;
882
883         if (!tz)
884                 return -EINVAL;
885
886         return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(tz->temperature));
887 }
888
889 static const char enabled[] = "kernel";
890 static const char disabled[] = "user";
891 static int thermal_get_mode(struct thermal_zone_device *thermal,
892                                 char *buf)
893 {
894         struct acpi_thermal *tz = thermal->devdata;
895
896         if (!tz)
897                 return -EINVAL;
898
899         return sprintf(buf, "%s\n", tz->tz_enabled ?
900                         enabled : disabled);
901 }
902
903 static int thermal_set_mode(struct thermal_zone_device *thermal,
904                                 const char *buf)
905 {
906         struct acpi_thermal *tz = thermal->devdata;
907         int enable;
908
909         if (!tz)
910                 return -EINVAL;
911
912         /*
913          * enable/disable thermal management from ACPI thermal driver
914          */
915         if (!strncmp(buf, enabled, sizeof enabled - 1))
916                 enable = 1;
917         else if (!strncmp(buf, disabled, sizeof disabled - 1))
918                 enable = 0;
919         else
920                 return -EINVAL;
921
922         if (enable != tz->tz_enabled) {
923                 tz->tz_enabled = enable;
924                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
925                         "%s ACPI thermal control\n",
926                         tz->tz_enabled ? enabled : disabled));
927                 acpi_thermal_check(tz);
928         }
929         return 0;
930 }
931
932 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
933                                  int trip, char *buf)
934 {
935         struct acpi_thermal *tz = thermal->devdata;
936         int i;
937
938         if (!tz || trip < 0)
939                 return -EINVAL;
940
941         if (tz->trips.critical.flags.valid) {
942                 if (!trip)
943                         return sprintf(buf, "critical\n");
944                 trip--;
945         }
946
947         if (tz->trips.hot.flags.valid) {
948                 if (!trip)
949                         return sprintf(buf, "hot\n");
950                 trip--;
951         }
952
953         if (tz->trips.passive.flags.valid) {
954                 if (!trip)
955                         return sprintf(buf, "passive\n");
956                 trip--;
957         }
958
959         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
960                 tz->trips.active[i].flags.valid; i++) {
961                 if (!trip)
962                         return sprintf(buf, "active%d\n", i);
963                 trip--;
964         }
965
966         return -EINVAL;
967 }
968
969 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
970                                  int trip, char *buf)
971 {
972         struct acpi_thermal *tz = thermal->devdata;
973         int i;
974
975         if (!tz || trip < 0)
976                 return -EINVAL;
977
978         if (tz->trips.critical.flags.valid) {
979                 if (!trip)
980                         return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
981                                 tz->trips.critical.temperature));
982                 trip--;
983         }
984
985         if (tz->trips.hot.flags.valid) {
986                 if (!trip)
987                         return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
988                                         tz->trips.hot.temperature));
989                 trip--;
990         }
991
992         if (tz->trips.passive.flags.valid) {
993                 if (!trip)
994                         return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
995                                         tz->trips.passive.temperature));
996                 trip--;
997         }
998
999         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1000                 tz->trips.active[i].flags.valid; i++) {
1001                 if (!trip)
1002                         return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
1003                                         tz->trips.active[i].temperature));
1004                 trip--;
1005         }
1006
1007         return -EINVAL;
1008 }
1009
1010 typedef int (*cb)(struct thermal_zone_device *, int,
1011                   struct thermal_cooling_device *);
1012 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
1013                                         struct thermal_cooling_device *cdev,
1014                                         cb action)
1015 {
1016         struct acpi_device *device = cdev->devdata;
1017         struct acpi_thermal *tz = thermal->devdata;
1018         struct acpi_device *dev;
1019         acpi_status status;
1020         acpi_handle handle;
1021         int i;
1022         int j;
1023         int trip = -1;
1024         int result = 0;
1025
1026         if (tz->trips.critical.flags.valid)
1027                 trip++;
1028
1029         if (tz->trips.hot.flags.valid)
1030                 trip++;
1031
1032         if (tz->trips.passive.flags.valid) {
1033                 trip++;
1034                 for (i = 0; i < tz->trips.passive.devices.count;
1035                     i++) {
1036                         handle = tz->trips.passive.devices.handles[i];
1037                         status = acpi_bus_get_device(handle, &dev);
1038                         if (ACPI_SUCCESS(status) && (dev == device)) {
1039                                 result = action(thermal, trip, cdev);
1040                                 if (result)
1041                                         goto failed;
1042                         }
1043                 }
1044         }
1045
1046         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1047                 if (!tz->trips.active[i].flags.valid)
1048                         break;
1049                 trip++;
1050                 for (j = 0;
1051                     j < tz->trips.active[i].devices.count;
1052                     j++) {
1053                         handle = tz->trips.active[i].devices.handles[j];
1054                         status = acpi_bus_get_device(handle, &dev);
1055                         if (ACPI_SUCCESS(status) && (dev == device)) {
1056                                 result = action(thermal, trip, cdev);
1057                                 if (result)
1058                                         goto failed;
1059                         }
1060                 }
1061         }
1062
1063         for (i = 0; i < tz->devices.count; i++) {
1064                 handle = tz->devices.handles[i];
1065                 status = acpi_bus_get_device(handle, &dev);
1066                 if (ACPI_SUCCESS(status) && (dev == device)) {
1067                         result = action(thermal, -1, cdev);
1068                         if (result)
1069                                 goto failed;
1070                 }
1071         }
1072
1073 failed:
1074         return result;
1075 }
1076
1077 static int
1078 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
1079                                         struct thermal_cooling_device *cdev)
1080 {
1081         return acpi_thermal_cooling_device_cb(thermal, cdev,
1082                                 thermal_zone_bind_cooling_device);
1083 }
1084
1085 static int
1086 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
1087                                         struct thermal_cooling_device *cdev)
1088 {
1089         return acpi_thermal_cooling_device_cb(thermal, cdev,
1090                                 thermal_zone_unbind_cooling_device);
1091 }
1092
1093 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
1094         .bind = acpi_thermal_bind_cooling_device,
1095         .unbind = acpi_thermal_unbind_cooling_device,
1096         .get_temp = thermal_get_temp,
1097         .get_mode = thermal_get_mode,
1098         .set_mode = thermal_set_mode,
1099         .get_trip_type = thermal_get_trip_type,
1100         .get_trip_temp = thermal_get_trip_temp,
1101 };
1102
1103 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
1104 {
1105         int trips = 0;
1106         int result;
1107         acpi_status status;
1108         int i;
1109
1110         if (tz->trips.critical.flags.valid)
1111                 trips++;
1112
1113         if (tz->trips.hot.flags.valid)
1114                 trips++;
1115
1116         if (tz->trips.passive.flags.valid)
1117                 trips++;
1118
1119         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1120                         tz->trips.active[i].flags.valid; i++, trips++);
1121         tz->thermal_zone = thermal_zone_device_register("ACPI thermal zone",
1122                                         trips, tz, &acpi_thermal_zone_ops);
1123         if (!tz->thermal_zone)
1124                 return -ENODEV;
1125
1126         result = sysfs_create_link(&tz->device->dev.kobj,
1127                                    &tz->thermal_zone->device.kobj, "thermal_zone");
1128         if (result)
1129                 return result;
1130
1131         result = sysfs_create_link(&tz->thermal_zone->device.kobj,
1132                                    &tz->device->dev.kobj, "device");
1133         if (result)
1134                 return result;
1135
1136         status = acpi_attach_data(tz->device->handle,
1137                                   acpi_bus_private_data_handler,
1138                                   tz->thermal_zone);
1139         if (ACPI_FAILURE(status)) {
1140                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1141                                 "Error attaching device data\n"));
1142                 return -ENODEV;
1143         }
1144
1145         tz->tz_enabled = 1;
1146
1147         printk(KERN_INFO PREFIX "%s is registered as thermal_zone%d\n",
1148                         tz->device->dev.bus_id, tz->thermal_zone->id);
1149         return 0;
1150 }
1151
1152 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
1153 {
1154         sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
1155         sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
1156         thermal_zone_device_unregister(tz->thermal_zone);
1157         tz->thermal_zone = NULL;
1158         acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
1159 }
1160
1161
1162 /* --------------------------------------------------------------------------
1163                               FS Interface (/proc)
1164    -------------------------------------------------------------------------- */
1165
1166 static struct proc_dir_entry *acpi_thermal_dir;
1167
1168 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
1169 {
1170         struct acpi_thermal *tz = seq->private;
1171
1172
1173         if (!tz)
1174                 goto end;
1175
1176         seq_puts(seq, "state:                   ");
1177
1178         if (!tz->state.critical && !tz->state.hot && !tz->state.passive
1179             && !tz->state.active)
1180                 seq_puts(seq, "ok\n");
1181         else {
1182                 if (tz->state.critical)
1183                         seq_puts(seq, "critical ");
1184                 if (tz->state.hot)
1185                         seq_puts(seq, "hot ");
1186                 if (tz->state.passive)
1187                         seq_puts(seq, "passive ");
1188                 if (tz->state.active)
1189                         seq_printf(seq, "active[%d]", tz->state.active_index);
1190                 seq_puts(seq, "\n");
1191         }
1192
1193       end:
1194         return 0;
1195 }
1196
1197 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1198 {
1199         return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1200 }
1201
1202 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1203 {
1204         int result = 0;
1205         struct acpi_thermal *tz = seq->private;
1206
1207
1208         if (!tz)
1209                 goto end;
1210
1211         result = acpi_thermal_get_temperature(tz);
1212         if (result)
1213                 goto end;
1214
1215         seq_printf(seq, "temperature:             %ld C\n",
1216                    KELVIN_TO_CELSIUS(tz->temperature));
1217
1218       end:
1219         return 0;
1220 }
1221
1222 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1223 {
1224         return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1225 }
1226
1227 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1228 {
1229         struct acpi_thermal *tz = seq->private;
1230         struct acpi_device *device;
1231         acpi_status status;
1232
1233         int i = 0;
1234         int j = 0;
1235
1236
1237         if (!tz)
1238                 goto end;
1239
1240         if (tz->trips.critical.flags.valid)
1241                 seq_printf(seq, "critical (S5):           %ld C%s",
1242                            KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1243                            nocrt ? " <disabled>\n" : "\n");
1244
1245         if (tz->trips.hot.flags.valid)
1246                 seq_printf(seq, "hot (S4):                %ld C%s",
1247                            KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1248                            nocrt ? " <disabled>\n" : "\n");
1249
1250         if (tz->trips.passive.flags.valid) {
1251                 seq_printf(seq,
1252                            "passive:                 %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1253                            KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1254                            tz->trips.passive.tc1, tz->trips.passive.tc2,
1255                            tz->trips.passive.tsp);
1256                 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1257                         status = acpi_bus_get_device(tz->trips.passive.devices.
1258                                                      handles[j], &device);
1259                         seq_printf(seq, "%4.4s ", status ? "" :
1260                                    acpi_device_bid(device));
1261                 }
1262                 seq_puts(seq, "\n");
1263         }
1264
1265         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1266                 if (!(tz->trips.active[i].flags.valid))
1267                         break;
1268                 seq_printf(seq, "active[%d]:               %ld C: devices=",
1269                            i,
1270                            KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1271                 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1272                         status = acpi_bus_get_device(tz->trips.active[i].
1273                                                      devices.handles[j],
1274                                                      &device);
1275                         seq_printf(seq, "%4.4s ", status ? "" :
1276                                    acpi_device_bid(device));
1277                 }
1278                 seq_puts(seq, "\n");
1279         }
1280
1281       end:
1282         return 0;
1283 }
1284
1285 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1286 {
1287         return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1288 }
1289
1290 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1291 {
1292         struct acpi_thermal *tz = seq->private;
1293
1294
1295         if (!tz)
1296                 goto end;
1297
1298         if (!tz->flags.cooling_mode)
1299                 seq_puts(seq, "<setting not supported>\n");
1300         else
1301                 seq_puts(seq, "0 - Active; 1 - Passive\n");
1302
1303       end:
1304         return 0;
1305 }
1306
1307 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1308 {
1309         return single_open(file, acpi_thermal_cooling_seq_show,
1310                            PDE(inode)->data);
1311 }
1312
1313 static ssize_t
1314 acpi_thermal_write_cooling_mode(struct file *file,
1315                                 const char __user * buffer,
1316                                 size_t count, loff_t * ppos)
1317 {
1318         struct seq_file *m = file->private_data;
1319         struct acpi_thermal *tz = m->private;
1320         int result = 0;
1321         char mode_string[12] = { '\0' };
1322
1323
1324         if (!tz || (count > sizeof(mode_string) - 1))
1325                 return -EINVAL;
1326
1327         if (!tz->flags.cooling_mode)
1328                 return -ENODEV;
1329
1330         if (copy_from_user(mode_string, buffer, count))
1331                 return -EFAULT;
1332
1333         mode_string[count] = '\0';
1334
1335         result = acpi_thermal_set_cooling_mode(tz,
1336                                                simple_strtoul(mode_string, NULL,
1337                                                               0));
1338         if (result)
1339                 return result;
1340
1341         acpi_thermal_check(tz);
1342
1343         return count;
1344 }
1345
1346 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1347 {
1348         struct acpi_thermal *tz = seq->private;
1349
1350
1351         if (!tz)
1352                 goto end;
1353
1354         if (!tz->polling_frequency) {
1355                 seq_puts(seq, "<polling disabled>\n");
1356                 goto end;
1357         }
1358
1359         seq_printf(seq, "polling frequency:       %lu seconds\n",
1360                    (tz->polling_frequency / 10));
1361
1362       end:
1363         return 0;
1364 }
1365
1366 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1367 {
1368         return single_open(file, acpi_thermal_polling_seq_show,
1369                            PDE(inode)->data);
1370 }
1371
1372 static ssize_t
1373 acpi_thermal_write_polling(struct file *file,
1374                            const char __user * buffer,
1375                            size_t count, loff_t * ppos)
1376 {
1377         struct seq_file *m = file->private_data;
1378         struct acpi_thermal *tz = m->private;
1379         int result = 0;
1380         char polling_string[12] = { '\0' };
1381         int seconds = 0;
1382
1383
1384         if (!tz || (count > sizeof(polling_string) - 1))
1385                 return -EINVAL;
1386
1387         if (copy_from_user(polling_string, buffer, count))
1388                 return -EFAULT;
1389
1390         polling_string[count] = '\0';
1391
1392         seconds = simple_strtoul(polling_string, NULL, 0);
1393
1394         result = acpi_thermal_set_polling(tz, seconds);
1395         if (result)
1396                 return result;
1397
1398         acpi_thermal_check(tz);
1399
1400         return count;
1401 }
1402
1403 static int acpi_thermal_add_fs(struct acpi_device *device)
1404 {
1405         struct proc_dir_entry *entry = NULL;
1406
1407
1408         if (!acpi_device_dir(device)) {
1409                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1410                                                      acpi_thermal_dir);
1411                 if (!acpi_device_dir(device))
1412                         return -ENODEV;
1413                 acpi_device_dir(device)->owner = THIS_MODULE;
1414         }
1415
1416         /* 'state' [R] */
1417         entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1418                                   S_IRUGO, acpi_device_dir(device));
1419         if (!entry)
1420                 return -ENODEV;
1421         else {
1422                 entry->proc_fops = &acpi_thermal_state_fops;
1423                 entry->data = acpi_driver_data(device);
1424                 entry->owner = THIS_MODULE;
1425         }
1426
1427         /* 'temperature' [R] */
1428         entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1429                                   S_IRUGO, acpi_device_dir(device));
1430         if (!entry)
1431                 return -ENODEV;
1432         else {
1433                 entry->proc_fops = &acpi_thermal_temp_fops;
1434                 entry->data = acpi_driver_data(device);
1435                 entry->owner = THIS_MODULE;
1436         }
1437
1438         /* 'trip_points' [R] */
1439         entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1440                                   S_IRUGO,
1441                                   acpi_device_dir(device));
1442         if (!entry)
1443                 return -ENODEV;
1444         else {
1445                 entry->proc_fops = &acpi_thermal_trip_fops;
1446                 entry->data = acpi_driver_data(device);
1447                 entry->owner = THIS_MODULE;
1448         }
1449
1450         /* 'cooling_mode' [R/W] */
1451         entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1452                                   S_IFREG | S_IRUGO | S_IWUSR,
1453                                   acpi_device_dir(device));
1454         if (!entry)
1455                 return -ENODEV;
1456         else {
1457                 entry->proc_fops = &acpi_thermal_cooling_fops;
1458                 entry->data = acpi_driver_data(device);
1459                 entry->owner = THIS_MODULE;
1460         }
1461
1462         /* 'polling_frequency' [R/W] */
1463         entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1464                                   S_IFREG | S_IRUGO | S_IWUSR,
1465                                   acpi_device_dir(device));
1466         if (!entry)
1467                 return -ENODEV;
1468         else {
1469                 entry->proc_fops = &acpi_thermal_polling_fops;
1470                 entry->data = acpi_driver_data(device);
1471                 entry->owner = THIS_MODULE;
1472         }
1473
1474         return 0;
1475 }
1476
1477 static int acpi_thermal_remove_fs(struct acpi_device *device)
1478 {
1479
1480         if (acpi_device_dir(device)) {
1481                 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1482                                   acpi_device_dir(device));
1483                 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1484                                   acpi_device_dir(device));
1485                 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1486                                   acpi_device_dir(device));
1487                 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1488                                   acpi_device_dir(device));
1489                 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1490                                   acpi_device_dir(device));
1491                 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1492                 acpi_device_dir(device) = NULL;
1493         }
1494
1495         return 0;
1496 }
1497
1498 /* --------------------------------------------------------------------------
1499                                  Driver Interface
1500    -------------------------------------------------------------------------- */
1501
1502 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1503 {
1504         struct acpi_thermal *tz = data;
1505         struct acpi_device *device = NULL;
1506
1507
1508         if (!tz)
1509                 return;
1510
1511         device = tz->device;
1512
1513         switch (event) {
1514         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1515                 acpi_thermal_check(tz);
1516                 break;
1517         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1518                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1519                 acpi_thermal_check(tz);
1520                 acpi_bus_generate_proc_event(device, event, 0);
1521                 acpi_bus_generate_netlink_event(device->pnp.device_class,
1522                                                   device->dev.bus_id, event, 0);
1523                 break;
1524         case ACPI_THERMAL_NOTIFY_DEVICES:
1525                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1526                 acpi_thermal_check(tz);
1527                 acpi_bus_generate_proc_event(device, event, 0);
1528                 acpi_bus_generate_netlink_event(device->pnp.device_class,
1529                                                   device->dev.bus_id, event, 0);
1530                 break;
1531         default:
1532                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1533                                   "Unsupported event [0x%x]\n", event));
1534                 break;
1535         }
1536
1537         return;
1538 }
1539
1540 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1541 {
1542         int result = 0;
1543
1544
1545         if (!tz)
1546                 return -EINVAL;
1547
1548         /* Get temperature [_TMP] (required) */
1549         result = acpi_thermal_get_temperature(tz);
1550         if (result)
1551                 return result;
1552
1553         /* Get trip points [_CRT, _PSV, etc.] (required) */
1554         result = acpi_thermal_get_trip_points(tz);
1555         if (result)
1556                 return result;
1557
1558         /* Set the cooling mode [_SCP] to active cooling (default) */
1559         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1560         if (!result)
1561                 tz->flags.cooling_mode = 1;
1562
1563         /* Get default polling frequency [_TZP] (optional) */
1564         if (tzp)
1565                 tz->polling_frequency = tzp;
1566         else
1567                 acpi_thermal_get_polling_frequency(tz);
1568
1569         return 0;
1570 }
1571
1572 static int acpi_thermal_add(struct acpi_device *device)
1573 {
1574         int result = 0;
1575         acpi_status status = AE_OK;
1576         struct acpi_thermal *tz = NULL;
1577
1578
1579         if (!device)
1580                 return -EINVAL;
1581
1582         tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1583         if (!tz)
1584                 return -ENOMEM;
1585
1586         tz->device = device;
1587         strcpy(tz->name, device->pnp.bus_id);
1588         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1589         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1590         acpi_driver_data(device) = tz;
1591         mutex_init(&tz->lock);
1592
1593
1594         result = acpi_thermal_get_info(tz);
1595         if (result)
1596                 goto free_memory;
1597
1598         result = acpi_thermal_register_thermal_zone(tz);
1599         if (result)
1600                 goto free_memory;
1601
1602         result = acpi_thermal_add_fs(device);
1603         if (result)
1604                 goto unregister_thermal_zone;
1605
1606         init_timer(&tz->timer);
1607
1608         acpi_thermal_check(tz);
1609
1610         status = acpi_install_notify_handler(device->handle,
1611                                              ACPI_DEVICE_NOTIFY,
1612                                              acpi_thermal_notify, tz);
1613         if (ACPI_FAILURE(status)) {
1614                 result = -ENODEV;
1615                 goto remove_fs;
1616         }
1617
1618         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1619                acpi_device_name(device), acpi_device_bid(device),
1620                KELVIN_TO_CELSIUS(tz->temperature));
1621         goto end;
1622
1623 remove_fs:
1624         acpi_thermal_remove_fs(device);
1625 unregister_thermal_zone:
1626         thermal_zone_device_unregister(tz->thermal_zone);
1627 free_memory:
1628         kfree(tz);
1629 end:
1630         return result;
1631 }
1632
1633 static int acpi_thermal_remove(struct acpi_device *device, int type)
1634 {
1635         acpi_status status = AE_OK;
1636         struct acpi_thermal *tz = NULL;
1637
1638
1639         if (!device || !acpi_driver_data(device))
1640                 return -EINVAL;
1641
1642         tz = acpi_driver_data(device);
1643
1644         /* avoid timer adding new defer task */
1645         tz->zombie = 1;
1646         /* wait for running timer (on other CPUs) finish */
1647         del_timer_sync(&(tz->timer));
1648         /* synchronize deferred task */
1649         acpi_os_wait_events_complete(NULL);
1650         /* deferred task may reinsert timer */
1651         del_timer_sync(&(tz->timer));
1652
1653         status = acpi_remove_notify_handler(device->handle,
1654                                             ACPI_DEVICE_NOTIFY,
1655                                             acpi_thermal_notify);
1656
1657         /* Terminate policy */
1658         if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1659                 tz->trips.passive.flags.enabled = 0;
1660                 acpi_thermal_passive(tz);
1661         }
1662         if (tz->trips.active[0].flags.valid
1663             && tz->trips.active[0].flags.enabled) {
1664                 tz->trips.active[0].flags.enabled = 0;
1665                 acpi_thermal_active(tz);
1666         }
1667
1668         acpi_thermal_remove_fs(device);
1669         acpi_thermal_unregister_thermal_zone(tz);
1670         mutex_destroy(&tz->lock);
1671         kfree(tz);
1672         return 0;
1673 }
1674
1675 static int acpi_thermal_resume(struct acpi_device *device)
1676 {
1677         struct acpi_thermal *tz = NULL;
1678         int i, j, power_state, result;
1679
1680
1681         if (!device || !acpi_driver_data(device))
1682                 return -EINVAL;
1683
1684         tz = acpi_driver_data(device);
1685
1686         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1687                 if (!(&tz->trips.active[i]))
1688                         break;
1689                 if (!tz->trips.active[i].flags.valid)
1690                         break;
1691                 tz->trips.active[i].flags.enabled = 1;
1692                 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1693                         result = acpi_bus_get_power(tz->trips.active[i].devices.
1694                             handles[j], &power_state);
1695                         if (result || (power_state != ACPI_STATE_D0)) {
1696                                 tz->trips.active[i].flags.enabled = 0;
1697                                 break;
1698                         }
1699                 }
1700                 tz->state.active |= tz->trips.active[i].flags.enabled;
1701         }
1702
1703         acpi_thermal_check(tz);
1704
1705         return AE_OK;
1706 }
1707
1708 #ifdef CONFIG_DMI
1709 static int thermal_act(const struct dmi_system_id *d) {
1710
1711         if (act == 0) {
1712                 printk(KERN_NOTICE "ACPI: %s detected: "
1713                         "disabling all active thermal trip points\n", d->ident);
1714                 act = -1;
1715         }
1716         return 0;
1717 }
1718 static int thermal_nocrt(const struct dmi_system_id *d) {
1719
1720         printk(KERN_NOTICE "ACPI: %s detected: "
1721                 "disabling all critical thermal trip point actions.\n", d->ident);
1722         nocrt = 1;
1723         return 0;
1724 }
1725 static int thermal_tzp(const struct dmi_system_id *d) {
1726
1727         if (tzp == 0) {
1728                 printk(KERN_NOTICE "ACPI: %s detected: "
1729                         "enabling thermal zone polling\n", d->ident);
1730                 tzp = 300;      /* 300 dS = 30 Seconds */
1731         }
1732         return 0;
1733 }
1734 static int thermal_psv(const struct dmi_system_id *d) {
1735
1736         if (psv == 0) {
1737                 printk(KERN_NOTICE "ACPI: %s detected: "
1738                         "disabling all passive thermal trip points\n", d->ident);
1739                 psv = -1;
1740         }
1741         return 0;
1742 }
1743
1744 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1745         /*
1746          * Award BIOS on this AOpen makes thermal control almost worthless.
1747          * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1748          */
1749         {
1750          .callback = thermal_act,
1751          .ident = "AOpen i915GMm-HFS",
1752          .matches = {
1753                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1754                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1755                 },
1756         },
1757         {
1758          .callback = thermal_psv,
1759          .ident = "AOpen i915GMm-HFS",
1760          .matches = {
1761                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1762                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1763                 },
1764         },
1765         {
1766          .callback = thermal_tzp,
1767          .ident = "AOpen i915GMm-HFS",
1768          .matches = {
1769                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1770                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1771                 },
1772         },
1773         {
1774          .callback = thermal_nocrt,
1775          .ident = "Gigabyte GA-7ZX",
1776          .matches = {
1777                 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1778                 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1779                 },
1780         },
1781         {}
1782 };
1783 #endif /* CONFIG_DMI */
1784
1785 static int __init acpi_thermal_init(void)
1786 {
1787         int result = 0;
1788
1789         dmi_check_system(thermal_dmi_table);
1790
1791         if (off) {
1792                 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1793                 return -ENODEV;
1794         }
1795         acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1796         if (!acpi_thermal_dir)
1797                 return -ENODEV;
1798         acpi_thermal_dir->owner = THIS_MODULE;
1799
1800         result = acpi_bus_register_driver(&acpi_thermal_driver);
1801         if (result < 0) {
1802                 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1803                 return -ENODEV;
1804         }
1805
1806         return 0;
1807 }
1808
1809 static void __exit acpi_thermal_exit(void)
1810 {
1811
1812         acpi_bus_unregister_driver(&acpi_thermal_driver);
1813
1814         remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1815
1816         return;
1817 }
1818
1819 module_init(acpi_thermal_init);
1820 module_exit(acpi_thermal_exit);