NFS: Fix a double free in nfs_parse_mount_options()
[deliverable/linux.git] / drivers / acpi / thermal.c
CommitLineData
1da177e4
LT
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>
0b5bfa1c 36#include <linux/dmi.h>
1da177e4
LT
37#include <linux/init.h>
38#include <linux/types.h>
39#include <linux/proc_fs.h>
cd354f1a 40#include <linux/jiffies.h>
1da177e4
LT
41#include <linux/kmod.h>
42#include <linux/seq_file.h>
10a0a8d4 43#include <linux/reboot.h>
f6f5c45e 44#include <linux/device.h>
1da177e4 45#include <asm/uaccess.h>
3f655ef8 46#include <linux/thermal.h>
1da177e4
LT
47#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
1da177e4 50#define ACPI_THERMAL_CLASS "thermal_zone"
1da177e4
LT
51#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52#define ACPI_THERMAL_FILE_STATE "state"
53#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61#define ACPI_THERMAL_NOTIFY_HOT 0xF1
62#define ACPI_THERMAL_MODE_ACTIVE 0x00
1da177e4
LT
63
64#define ACPI_THERMAL_MAX_ACTIVE 10
65#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66
1da177e4 67#define _COMPONENT ACPI_THERMAL_COMPONENT
f52fd66d 68ACPI_MODULE_NAME("thermal");
1da177e4 69
1cbf4c56 70MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 71MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1da177e4
LT
72MODULE_LICENSE("GPL");
73
f8707ec9
LB
74static int act;
75module_param(act, int, 0644);
3c1d36da 76MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
f8707ec9 77
c52a7419
LB
78static int crt;
79module_param(crt, int, 0644);
80MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
81
1da177e4 82static int tzp;
730ff34d 83module_param(tzp, int, 0444);
3c1d36da 84MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
1da177e4 85
f5487145
LB
86static int nocrt;
87module_param(nocrt, int, 0);
8c99fdce 88MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
f5487145 89
72b33ef8
LB
90static int off;
91module_param(off, int, 0);
3c1d36da 92MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
72b33ef8 93
a70cdc52
LB
94static int psv;
95module_param(psv, int, 0644);
3c1d36da 96MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
a70cdc52 97
4be44fcd
LB
98static int acpi_thermal_add(struct acpi_device *device);
99static int acpi_thermal_remove(struct acpi_device *device, int type);
5d9464a4 100static int acpi_thermal_resume(struct acpi_device *device);
1da177e4
LT
101static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
102static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
103static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
1da177e4 104static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
4be44fcd
LB
105static ssize_t acpi_thermal_write_cooling_mode(struct file *,
106 const char __user *, size_t,
107 loff_t *);
1da177e4 108static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
4be44fcd
LB
109static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
110 size_t, loff_t *);
1da177e4 111
1ba90e3a
TR
112static const struct acpi_device_id thermal_device_ids[] = {
113 {ACPI_THERMAL_HID, 0},
114 {"", 0},
115};
116MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
117
1da177e4 118static struct acpi_driver acpi_thermal_driver = {
c2b6705b 119 .name = "thermal",
4be44fcd 120 .class = ACPI_THERMAL_CLASS,
1ba90e3a 121 .ids = thermal_device_ids,
4be44fcd
LB
122 .ops = {
123 .add = acpi_thermal_add,
124 .remove = acpi_thermal_remove,
74ce1468 125 .resume = acpi_thermal_resume,
4be44fcd 126 },
1da177e4
LT
127};
128
129struct acpi_thermal_state {
4be44fcd
LB
130 u8 critical:1;
131 u8 hot:1;
132 u8 passive:1;
133 u8 active:1;
134 u8 reserved:4;
135 int active_index;
1da177e4
LT
136};
137
138struct acpi_thermal_state_flags {
4be44fcd
LB
139 u8 valid:1;
140 u8 enabled:1;
141 u8 reserved:6;
1da177e4
LT
142};
143
144struct acpi_thermal_critical {
145 struct acpi_thermal_state_flags flags;
4be44fcd 146 unsigned long temperature;
1da177e4
LT
147};
148
149struct acpi_thermal_hot {
150 struct acpi_thermal_state_flags flags;
4be44fcd 151 unsigned long temperature;
1da177e4
LT
152};
153
154struct acpi_thermal_passive {
155 struct acpi_thermal_state_flags flags;
4be44fcd
LB
156 unsigned long temperature;
157 unsigned long tc1;
158 unsigned long tc2;
159 unsigned long tsp;
160 struct acpi_handle_list devices;
1da177e4
LT
161};
162
163struct acpi_thermal_active {
164 struct acpi_thermal_state_flags flags;
4be44fcd
LB
165 unsigned long temperature;
166 struct acpi_handle_list devices;
1da177e4
LT
167};
168
169struct acpi_thermal_trips {
170 struct acpi_thermal_critical critical;
4be44fcd 171 struct acpi_thermal_hot hot;
1da177e4
LT
172 struct acpi_thermal_passive passive;
173 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
174};
175
176struct acpi_thermal_flags {
4be44fcd
LB
177 u8 cooling_mode:1; /* _SCP */
178 u8 devices:1; /* _TZD */
179 u8 reserved:6;
1da177e4
LT
180};
181
182struct acpi_thermal {
8348e1b1 183 struct acpi_device * device;
4be44fcd
LB
184 acpi_bus_id name;
185 unsigned long temperature;
186 unsigned long last_temperature;
187 unsigned long polling_frequency;
4be44fcd 188 volatile u8 zombie;
1da177e4
LT
189 struct acpi_thermal_flags flags;
190 struct acpi_thermal_state state;
191 struct acpi_thermal_trips trips;
4be44fcd 192 struct acpi_handle_list devices;
3f655ef8
ZR
193 struct thermal_zone_device *thermal_zone;
194 int tz_enabled;
6e215785 195 struct mutex lock;
1da177e4
LT
196};
197
d7508032 198static const struct file_operations acpi_thermal_state_fops = {
cf7acfab 199 .owner = THIS_MODULE,
4be44fcd
LB
200 .open = acpi_thermal_state_open_fs,
201 .read = seq_read,
202 .llseek = seq_lseek,
203 .release = single_release,
1da177e4
LT
204};
205
d7508032 206static const struct file_operations acpi_thermal_temp_fops = {
cf7acfab 207 .owner = THIS_MODULE,
4be44fcd
LB
208 .open = acpi_thermal_temp_open_fs,
209 .read = seq_read,
210 .llseek = seq_lseek,
211 .release = single_release,
1da177e4
LT
212};
213
d7508032 214static const struct file_operations acpi_thermal_trip_fops = {
cf7acfab 215 .owner = THIS_MODULE,
4be44fcd
LB
216 .open = acpi_thermal_trip_open_fs,
217 .read = seq_read,
4be44fcd
LB
218 .llseek = seq_lseek,
219 .release = single_release,
1da177e4
LT
220};
221
d7508032 222static const struct file_operations acpi_thermal_cooling_fops = {
cf7acfab 223 .owner = THIS_MODULE,
4be44fcd
LB
224 .open = acpi_thermal_cooling_open_fs,
225 .read = seq_read,
226 .write = acpi_thermal_write_cooling_mode,
227 .llseek = seq_lseek,
228 .release = single_release,
1da177e4
LT
229};
230
d7508032 231static const struct file_operations acpi_thermal_polling_fops = {
cf7acfab 232 .owner = THIS_MODULE,
4be44fcd
LB
233 .open = acpi_thermal_polling_open_fs,
234 .read = seq_read,
235 .write = acpi_thermal_write_polling,
236 .llseek = seq_lseek,
237 .release = single_release,
1da177e4
LT
238};
239
240/* --------------------------------------------------------------------------
241 Thermal Zone Management
242 -------------------------------------------------------------------------- */
243
4be44fcd 244static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
1da177e4 245{
4be44fcd 246 acpi_status status = AE_OK;
27663c58 247 unsigned long long tmp;
1da177e4
LT
248
249 if (!tz)
d550d98d 250 return -EINVAL;
1da177e4
LT
251
252 tz->last_temperature = tz->temperature;
253
27663c58 254 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
1da177e4 255 if (ACPI_FAILURE(status))
d550d98d 256 return -ENODEV;
1da177e4 257
27663c58 258 tz->temperature = tmp;
4be44fcd
LB
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
260 tz->temperature));
1da177e4 261
d550d98d 262 return 0;
1da177e4
LT
263}
264
4be44fcd 265static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
1da177e4 266{
4be44fcd 267 acpi_status status = AE_OK;
27663c58 268 unsigned long long tmp;
1da177e4
LT
269
270 if (!tz)
d550d98d 271 return -EINVAL;
1da177e4 272
27663c58 273 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
1da177e4 274 if (ACPI_FAILURE(status))
d550d98d 275 return -ENODEV;
1da177e4 276
27663c58 277 tz->polling_frequency = tmp;
4be44fcd
LB
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
279 tz->polling_frequency));
1da177e4 280
d550d98d 281 return 0;
1da177e4
LT
282}
283
4be44fcd 284static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
1da177e4 285{
1da177e4
LT
286
287 if (!tz)
d550d98d 288 return -EINVAL;
1da177e4
LT
289
290 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
291
b1569e99
MG
292 tz->thermal_zone->polling_delay = seconds * 1000;
293
294 if (tz->tz_enabled)
295 thermal_zone_device_update(tz->thermal_zone);
296
4be44fcd
LB
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
298 "Polling frequency set to %lu seconds\n",
636cedf9 299 tz->polling_frequency/10));
1da177e4 300
d550d98d 301 return 0;
1da177e4
LT
302}
303
4be44fcd 304static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
1da177e4 305{
4be44fcd
LB
306 acpi_status status = AE_OK;
307 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
308 struct acpi_object_list arg_list = { 1, &arg0 };
309 acpi_handle handle = NULL;
1da177e4 310
1da177e4
LT
311
312 if (!tz)
d550d98d 313 return -EINVAL;
1da177e4 314
38ba7c9e 315 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
1da177e4
LT
316 if (ACPI_FAILURE(status)) {
317 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
d550d98d 318 return -ENODEV;
1da177e4
LT
319 }
320
321 arg0.integer.value = mode;
322
323 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
324 if (ACPI_FAILURE(status))
d550d98d 325 return -ENODEV;
1da177e4 326
d550d98d 327 return 0;
1da177e4
LT
328}
329
ce44e197
ZR
330#define ACPI_TRIPS_CRITICAL 0x01
331#define ACPI_TRIPS_HOT 0x02
332#define ACPI_TRIPS_PASSIVE 0x04
333#define ACPI_TRIPS_ACTIVE 0x08
334#define ACPI_TRIPS_DEVICES 0x10
1da177e4 335
ce44e197
ZR
336#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
337#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
1da177e4 338
ce44e197
ZR
339#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
340 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
341 ACPI_TRIPS_DEVICES)
1da177e4 342
ce44e197
ZR
343/*
344 * This exception is thrown out in two cases:
345 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
346 * when re-evaluating the AML code.
347 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
348 * We need to re-bind the cooling devices of a thermal zone when this occurs.
349 */
350#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
351do { \
352 if (flags != ACPI_TRIPS_INIT) \
353 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
354 "ACPI thermal trip point %s changed\n" \
355 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
356} while (0)
357
358static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
359{
360 acpi_status status = AE_OK;
27663c58 361 unsigned long long tmp;
ce44e197
ZR
362 struct acpi_handle_list devices;
363 int valid = 0;
364 int i;
1da177e4 365
ce44e197
ZR
366 /* Critical Shutdown (required) */
367 if (flag & ACPI_TRIPS_CRITICAL) {
368 status = acpi_evaluate_integer(tz->device->handle,
27663c58
MW
369 "_CRT", NULL, &tmp);
370 tz->trips.critical.temperature = tmp;
a39a2d7c
AV
371 /*
372 * Treat freezing temperatures as invalid as well; some
373 * BIOSes return really low values and cause reboots at startup.
b731d7b6 374 * Below zero (Celsius) values clearly aren't right for sure..
a39a2d7c
AV
375 * ... so lets discard those as invalid.
376 */
377 if (ACPI_FAILURE(status) ||
378 tz->trips.critical.temperature <= 2732) {
c52a7419 379 tz->trips.critical.flags.valid = 0;
ce44e197 380 ACPI_EXCEPTION((AE_INFO, status,
a39a2d7c 381 "No or invalid critical threshold"));
ce44e197
ZR
382 return -ENODEV;
383 } else {
384 tz->trips.critical.flags.valid = 1;
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
386 "Found critical threshold [%lu]\n",
387 tz->trips.critical.temperature));
388 }
389 if (tz->trips.critical.flags.valid == 1) {
390 if (crt == -1) {
391 tz->trips.critical.flags.valid = 0;
392 } else if (crt > 0) {
393 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
394 /*
22a94d79 395 * Allow override critical threshold
ce44e197 396 */
22a94d79
ZR
397 if (crt_k > tz->trips.critical.temperature)
398 printk(KERN_WARNING PREFIX
399 "Critical threshold %d C\n", crt);
400 tz->trips.critical.temperature = crt_k;
ce44e197 401 }
c52a7419
LB
402 }
403 }
404
1da177e4 405 /* Critical Sleep (optional) */
ce44e197 406 if (flag & ACPI_TRIPS_HOT) {
a70cdc52 407 status = acpi_evaluate_integer(tz->device->handle,
27663c58 408 "_HOT", NULL, &tmp);
ce44e197
ZR
409 if (ACPI_FAILURE(status)) {
410 tz->trips.hot.flags.valid = 0;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412 "No hot threshold\n"));
413 } else {
27663c58 414 tz->trips.hot.temperature = tmp;
ce44e197
ZR
415 tz->trips.hot.flags.valid = 1;
416 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
417 "Found hot threshold [%lu]\n",
418 tz->trips.critical.temperature));
419 }
a70cdc52
LB
420 }
421
ce44e197 422 /* Passive (optional) */
0e4240d9
ZR
423 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
424 (flag == ACPI_TRIPS_INIT)) {
ce44e197
ZR
425 valid = tz->trips.passive.flags.valid;
426 if (psv == -1) {
427 status = AE_SUPPORT;
428 } else if (psv > 0) {
27663c58 429 tmp = CELSIUS_TO_KELVIN(psv);
ce44e197
ZR
430 status = AE_OK;
431 } else {
432 status = acpi_evaluate_integer(tz->device->handle,
27663c58 433 "_PSV", NULL, &tmp);
ce44e197 434 }
1da177e4 435
1da177e4
LT
436 if (ACPI_FAILURE(status))
437 tz->trips.passive.flags.valid = 0;
ce44e197 438 else {
27663c58 439 tz->trips.passive.temperature = tmp;
ce44e197
ZR
440 tz->trips.passive.flags.valid = 1;
441 if (flag == ACPI_TRIPS_INIT) {
442 status = acpi_evaluate_integer(
443 tz->device->handle, "_TC1",
27663c58 444 NULL, &tmp);
ce44e197
ZR
445 if (ACPI_FAILURE(status))
446 tz->trips.passive.flags.valid = 0;
27663c58
MW
447 else
448 tz->trips.passive.tc1 = tmp;
ce44e197
ZR
449 status = acpi_evaluate_integer(
450 tz->device->handle, "_TC2",
27663c58 451 NULL, &tmp);
ce44e197
ZR
452 if (ACPI_FAILURE(status))
453 tz->trips.passive.flags.valid = 0;
27663c58
MW
454 else
455 tz->trips.passive.tc2 = tmp;
ce44e197
ZR
456 status = acpi_evaluate_integer(
457 tz->device->handle, "_TSP",
27663c58 458 NULL, &tmp);
ce44e197
ZR
459 if (ACPI_FAILURE(status))
460 tz->trips.passive.flags.valid = 0;
27663c58
MW
461 else
462 tz->trips.passive.tsp = tmp;
ce44e197
ZR
463 }
464 }
465 }
466 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
467 memset(&devices, 0, sizeof(struct acpi_handle_list));
468 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
469 NULL, &devices);
0e4240d9
ZR
470 if (ACPI_FAILURE(status)) {
471 printk(KERN_WARNING PREFIX
472 "Invalid passive threshold\n");
1da177e4 473 tz->trips.passive.flags.valid = 0;
0e4240d9 474 }
1da177e4 475 else
ce44e197 476 tz->trips.passive.flags.valid = 1;
1da177e4 477
ce44e197
ZR
478 if (memcmp(&tz->trips.passive.devices, &devices,
479 sizeof(struct acpi_handle_list))) {
480 memcpy(&tz->trips.passive.devices, &devices,
481 sizeof(struct acpi_handle_list));
482 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
483 }
484 }
485 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
486 if (valid != tz->trips.passive.flags.valid)
487 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
488 }
1da177e4 489
ce44e197 490 /* Active (optional) */
4be44fcd 491 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
4be44fcd 492 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
ce44e197 493 valid = tz->trips.active[i].flags.valid;
1da177e4 494
f8707ec9 495 if (act == -1)
ce44e197
ZR
496 break; /* disable all active trip points */
497
0e4240d9
ZR
498 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
499 tz->trips.active[i].flags.valid)) {
ce44e197 500 status = acpi_evaluate_integer(tz->device->handle,
27663c58 501 name, NULL, &tmp);
ce44e197
ZR
502 if (ACPI_FAILURE(status)) {
503 tz->trips.active[i].flags.valid = 0;
504 if (i == 0)
505 break;
506 if (act <= 0)
507 break;
508 if (i == 1)
509 tz->trips.active[0].temperature =
510 CELSIUS_TO_KELVIN(act);
511 else
512 /*
513 * Don't allow override higher than
514 * the next higher trip point
515 */
516 tz->trips.active[i - 1].temperature =
517 (tz->trips.active[i - 2].temperature <
518 CELSIUS_TO_KELVIN(act) ?
519 tz->trips.active[i - 2].temperature :
520 CELSIUS_TO_KELVIN(act));
f8707ec9 521 break;
27663c58
MW
522 } else {
523 tz->trips.active[i].temperature = tmp;
ce44e197 524 tz->trips.active[i].flags.valid = 1;
27663c58 525 }
f8707ec9 526 }
1da177e4
LT
527
528 name[2] = 'L';
ce44e197
ZR
529 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
530 memset(&devices, 0, sizeof(struct acpi_handle_list));
531 status = acpi_evaluate_reference(tz->device->handle,
532 name, NULL, &devices);
0e4240d9
ZR
533 if (ACPI_FAILURE(status)) {
534 printk(KERN_WARNING PREFIX
535 "Invalid active%d threshold\n", i);
ce44e197 536 tz->trips.active[i].flags.valid = 0;
0e4240d9 537 }
ce44e197
ZR
538 else
539 tz->trips.active[i].flags.valid = 1;
540
541 if (memcmp(&tz->trips.active[i].devices, &devices,
542 sizeof(struct acpi_handle_list))) {
543 memcpy(&tz->trips.active[i].devices, &devices,
544 sizeof(struct acpi_handle_list));
545 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
546 }
547 }
548 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
549 if (valid != tz->trips.active[i].flags.valid)
550 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
551
552 if (!tz->trips.active[i].flags.valid)
553 break;
554 }
555
556 if (flag & ACPI_TRIPS_DEVICES) {
557 memset(&devices, 0, sizeof(struct acpi_handle_list));
558 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
559 NULL, &devices);
560 if (memcmp(&tz->devices, &devices,
561 sizeof(struct acpi_handle_list))) {
562 memcpy(&tz->devices, &devices,
563 sizeof(struct acpi_handle_list));
564 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
565 }
1da177e4
LT
566 }
567
d550d98d 568 return 0;
1da177e4
LT
569}
570
ce44e197 571static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
1da177e4 572{
ce44e197 573 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
1da177e4
LT
574}
575
4be44fcd 576static void acpi_thermal_check(void *data)
1da177e4 577{
50dd0969 578 struct acpi_thermal *tz = data;
1da177e4 579
b1569e99 580 thermal_zone_device_update(tz->thermal_zone);
1da177e4
LT
581}
582
3f655ef8 583/* sys I/F for generic thermal sysfs support */
5e012760
ZR
584#define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
585
6503e5df
MG
586static int thermal_get_temp(struct thermal_zone_device *thermal,
587 unsigned long *temp)
3f655ef8
ZR
588{
589 struct acpi_thermal *tz = thermal->devdata;
76ecb4f2 590 int result;
3f655ef8
ZR
591
592 if (!tz)
593 return -EINVAL;
594
76ecb4f2
ZR
595 result = acpi_thermal_get_temperature(tz);
596 if (result)
597 return result;
598
6503e5df
MG
599 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature);
600 return 0;
3f655ef8
ZR
601}
602
603static const char enabled[] = "kernel";
604static const char disabled[] = "user";
605static int thermal_get_mode(struct thermal_zone_device *thermal,
6503e5df 606 enum thermal_device_mode *mode)
3f655ef8
ZR
607{
608 struct acpi_thermal *tz = thermal->devdata;
609
610 if (!tz)
611 return -EINVAL;
612
6503e5df
MG
613 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
614 THERMAL_DEVICE_DISABLED;
615
616 return 0;
3f655ef8
ZR
617}
618
619static int thermal_set_mode(struct thermal_zone_device *thermal,
6503e5df 620 enum thermal_device_mode mode)
3f655ef8
ZR
621{
622 struct acpi_thermal *tz = thermal->devdata;
623 int enable;
624
625 if (!tz)
626 return -EINVAL;
627
628 /*
629 * enable/disable thermal management from ACPI thermal driver
630 */
6503e5df 631 if (mode == THERMAL_DEVICE_ENABLED)
3f655ef8 632 enable = 1;
6503e5df 633 else if (mode == THERMAL_DEVICE_DISABLED)
3f655ef8
ZR
634 enable = 0;
635 else
636 return -EINVAL;
637
638 if (enable != tz->tz_enabled) {
639 tz->tz_enabled = enable;
640 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
641 "%s ACPI thermal control\n",
642 tz->tz_enabled ? enabled : disabled));
643 acpi_thermal_check(tz);
644 }
645 return 0;
646}
647
648static int thermal_get_trip_type(struct thermal_zone_device *thermal,
6503e5df 649 int trip, enum thermal_trip_type *type)
3f655ef8
ZR
650{
651 struct acpi_thermal *tz = thermal->devdata;
652 int i;
653
654 if (!tz || trip < 0)
655 return -EINVAL;
656
657 if (tz->trips.critical.flags.valid) {
6503e5df
MG
658 if (!trip) {
659 *type = THERMAL_TRIP_CRITICAL;
660 return 0;
661 }
3f655ef8
ZR
662 trip--;
663 }
664
665 if (tz->trips.hot.flags.valid) {
6503e5df
MG
666 if (!trip) {
667 *type = THERMAL_TRIP_HOT;
668 return 0;
669 }
3f655ef8
ZR
670 trip--;
671 }
672
673 if (tz->trips.passive.flags.valid) {
6503e5df
MG
674 if (!trip) {
675 *type = THERMAL_TRIP_PASSIVE;
676 return 0;
677 }
3f655ef8
ZR
678 trip--;
679 }
680
681 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
682 tz->trips.active[i].flags.valid; i++) {
6503e5df
MG
683 if (!trip) {
684 *type = THERMAL_TRIP_ACTIVE;
685 return 0;
686 }
3f655ef8
ZR
687 trip--;
688 }
689
690 return -EINVAL;
691}
692
693static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
6503e5df 694 int trip, unsigned long *temp)
3f655ef8
ZR
695{
696 struct acpi_thermal *tz = thermal->devdata;
697 int i;
698
699 if (!tz || trip < 0)
700 return -EINVAL;
701
702 if (tz->trips.critical.flags.valid) {
6503e5df
MG
703 if (!trip) {
704 *temp = KELVIN_TO_MILLICELSIUS(
705 tz->trips.critical.temperature);
706 return 0;
707 }
3f655ef8
ZR
708 trip--;
709 }
710
711 if (tz->trips.hot.flags.valid) {
6503e5df
MG
712 if (!trip) {
713 *temp = KELVIN_TO_MILLICELSIUS(
714 tz->trips.hot.temperature);
715 return 0;
716 }
3f655ef8
ZR
717 trip--;
718 }
719
720 if (tz->trips.passive.flags.valid) {
6503e5df
MG
721 if (!trip) {
722 *temp = KELVIN_TO_MILLICELSIUS(
723 tz->trips.passive.temperature);
724 return 0;
725 }
3f655ef8
ZR
726 trip--;
727 }
728
729 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
730 tz->trips.active[i].flags.valid; i++) {
6503e5df
MG
731 if (!trip) {
732 *temp = KELVIN_TO_MILLICELSIUS(
733 tz->trips.active[i].temperature);
734 return 0;
735 }
3f655ef8
ZR
736 trip--;
737 }
738
739 return -EINVAL;
740}
741
9ec732ff
ZR
742static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
743 unsigned long *temperature) {
744 struct acpi_thermal *tz = thermal->devdata;
745
746 if (tz->trips.critical.flags.valid) {
747 *temperature = KELVIN_TO_MILLICELSIUS(
748 tz->trips.critical.temperature);
749 return 0;
750 } else
751 return -EINVAL;
752}
753
b1569e99
MG
754static int thermal_notify(struct thermal_zone_device *thermal, int trip,
755 enum thermal_trip_type trip_type)
756{
757 u8 type = 0;
758 struct acpi_thermal *tz = thermal->devdata;
759
760 if (trip_type == THERMAL_TRIP_CRITICAL)
761 type = ACPI_THERMAL_NOTIFY_CRITICAL;
762 else if (trip_type == THERMAL_TRIP_HOT)
763 type = ACPI_THERMAL_NOTIFY_HOT;
764 else
765 return 0;
766
767 acpi_bus_generate_proc_event(tz->device, type, 1);
768 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
f6f5c45e 769 dev_name(&tz->device->dev), type, 1);
b1569e99
MG
770
771 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
772 return 1;
773
774 return 0;
775}
776
3f655ef8
ZR
777typedef int (*cb)(struct thermal_zone_device *, int,
778 struct thermal_cooling_device *);
779static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
780 struct thermal_cooling_device *cdev,
781 cb action)
782{
783 struct acpi_device *device = cdev->devdata;
784 struct acpi_thermal *tz = thermal->devdata;
653a00c9
ZR
785 struct acpi_device *dev;
786 acpi_status status;
787 acpi_handle handle;
3f655ef8
ZR
788 int i;
789 int j;
790 int trip = -1;
791 int result = 0;
792
793 if (tz->trips.critical.flags.valid)
794 trip++;
795
796 if (tz->trips.hot.flags.valid)
797 trip++;
798
799 if (tz->trips.passive.flags.valid) {
800 trip++;
801 for (i = 0; i < tz->trips.passive.devices.count;
802 i++) {
653a00c9
ZR
803 handle = tz->trips.passive.devices.handles[i];
804 status = acpi_bus_get_device(handle, &dev);
805 if (ACPI_SUCCESS(status) && (dev == device)) {
806 result = action(thermal, trip, cdev);
807 if (result)
808 goto failed;
809 }
3f655ef8
ZR
810 }
811 }
812
813 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
814 if (!tz->trips.active[i].flags.valid)
815 break;
816 trip++;
817 for (j = 0;
818 j < tz->trips.active[i].devices.count;
819 j++) {
653a00c9
ZR
820 handle = tz->trips.active[i].devices.handles[j];
821 status = acpi_bus_get_device(handle, &dev);
822 if (ACPI_SUCCESS(status) && (dev == device)) {
823 result = action(thermal, trip, cdev);
824 if (result)
825 goto failed;
826 }
3f655ef8
ZR
827 }
828 }
829
830 for (i = 0; i < tz->devices.count; i++) {
653a00c9
ZR
831 handle = tz->devices.handles[i];
832 status = acpi_bus_get_device(handle, &dev);
833 if (ACPI_SUCCESS(status) && (dev == device)) {
834 result = action(thermal, -1, cdev);
835 if (result)
836 goto failed;
837 }
3f655ef8
ZR
838 }
839
840failed:
841 return result;
842}
843
844static int
845acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
846 struct thermal_cooling_device *cdev)
847{
848 return acpi_thermal_cooling_device_cb(thermal, cdev,
849 thermal_zone_bind_cooling_device);
850}
851
852static int
853acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
854 struct thermal_cooling_device *cdev)
855{
856 return acpi_thermal_cooling_device_cb(thermal, cdev,
857 thermal_zone_unbind_cooling_device);
858}
859
860static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
861 .bind = acpi_thermal_bind_cooling_device,
862 .unbind = acpi_thermal_unbind_cooling_device,
863 .get_temp = thermal_get_temp,
864 .get_mode = thermal_get_mode,
865 .set_mode = thermal_set_mode,
866 .get_trip_type = thermal_get_trip_type,
867 .get_trip_temp = thermal_get_trip_temp,
9ec732ff 868 .get_crit_temp = thermal_get_crit_temp,
b1569e99 869 .notify = thermal_notify,
3f655ef8
ZR
870};
871
872static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
873{
874 int trips = 0;
875 int result;
20733939 876 acpi_status status;
3f655ef8
ZR
877 int i;
878
879 if (tz->trips.critical.flags.valid)
880 trips++;
881
882 if (tz->trips.hot.flags.valid)
883 trips++;
884
885 if (tz->trips.passive.flags.valid)
886 trips++;
887
888 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
889 tz->trips.active[i].flags.valid; i++, trips++);
b1569e99
MG
890
891 if (tz->trips.passive.flags.valid)
892 tz->thermal_zone =
893 thermal_zone_device_register("acpitz", trips, tz,
894 &acpi_thermal_zone_ops,
895 tz->trips.passive.tc1,
896 tz->trips.passive.tc2,
897 tz->trips.passive.tsp*100,
898 tz->polling_frequency*100);
899 else
900 tz->thermal_zone =
901 thermal_zone_device_register("acpitz", trips, tz,
902 &acpi_thermal_zone_ops,
903 0, 0, 0,
904 tz->polling_frequency);
bb070e43 905 if (IS_ERR(tz->thermal_zone))
3f655ef8
ZR
906 return -ENODEV;
907
908 result = sysfs_create_link(&tz->device->dev.kobj,
909 &tz->thermal_zone->device.kobj, "thermal_zone");
910 if (result)
911 return result;
912
913 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
914 &tz->device->dev.kobj, "device");
915 if (result)
916 return result;
917
20733939
ZR
918 status = acpi_attach_data(tz->device->handle,
919 acpi_bus_private_data_handler,
920 tz->thermal_zone);
921 if (ACPI_FAILURE(status)) {
55ac9a01
LM
922 printk(KERN_ERR PREFIX
923 "Error attaching device data\n");
20733939
ZR
924 return -ENODEV;
925 }
926
3f655ef8
ZR
927 tz->tz_enabled = 1;
928
fc3a8828
GKH
929 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
930 tz->thermal_zone->id);
3f655ef8
ZR
931 return 0;
932}
933
934static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
935{
936 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
937 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
938 thermal_zone_device_unregister(tz->thermal_zone);
939 tz->thermal_zone = NULL;
20733939 940 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
3f655ef8
ZR
941}
942
943
1da177e4
LT
944/* --------------------------------------------------------------------------
945 FS Interface (/proc)
946 -------------------------------------------------------------------------- */
947
4be44fcd 948static struct proc_dir_entry *acpi_thermal_dir;
1da177e4
LT
949
950static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
951{
50dd0969 952 struct acpi_thermal *tz = seq->private;
1da177e4 953
1da177e4
LT
954
955 if (!tz)
956 goto end;
957
958 seq_puts(seq, "state: ");
959
4be44fcd
LB
960 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
961 && !tz->state.active)
1da177e4
LT
962 seq_puts(seq, "ok\n");
963 else {
964 if (tz->state.critical)
965 seq_puts(seq, "critical ");
966 if (tz->state.hot)
967 seq_puts(seq, "hot ");
968 if (tz->state.passive)
969 seq_puts(seq, "passive ");
970 if (tz->state.active)
971 seq_printf(seq, "active[%d]", tz->state.active_index);
972 seq_puts(seq, "\n");
973 }
974
4be44fcd 975 end:
d550d98d 976 return 0;
1da177e4
LT
977}
978
979static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
980{
981 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
982}
983
1da177e4
LT
984static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
985{
4be44fcd 986 int result = 0;
50dd0969 987 struct acpi_thermal *tz = seq->private;
1da177e4 988
1da177e4
LT
989
990 if (!tz)
991 goto end;
992
993 result = acpi_thermal_get_temperature(tz);
994 if (result)
995 goto end;
996
4be44fcd
LB
997 seq_printf(seq, "temperature: %ld C\n",
998 KELVIN_TO_CELSIUS(tz->temperature));
1da177e4 999
4be44fcd 1000 end:
d550d98d 1001 return 0;
1da177e4
LT
1002}
1003
1004static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1005{
1006 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1007}
1008
1da177e4
LT
1009static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1010{
50dd0969 1011 struct acpi_thermal *tz = seq->private;
68ccfaa8 1012 struct acpi_device *device;
e7c746ef
TR
1013 acpi_status status;
1014
4be44fcd
LB
1015 int i = 0;
1016 int j = 0;
1da177e4 1017
1da177e4
LT
1018
1019 if (!tz)
1020 goto end;
1021
1022 if (tz->trips.critical.flags.valid)
f5487145
LB
1023 seq_printf(seq, "critical (S5): %ld C%s",
1024 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1025 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
1026
1027 if (tz->trips.hot.flags.valid)
f5487145
LB
1028 seq_printf(seq, "hot (S4): %ld C%s",
1029 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1030 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
1031
1032 if (tz->trips.passive.flags.valid) {
4be44fcd
LB
1033 seq_printf(seq,
1034 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1035 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1036 tz->trips.passive.tc1, tz->trips.passive.tc2,
1037 tz->trips.passive.tsp);
1038 for (j = 0; j < tz->trips.passive.devices.count; j++) {
e7c746ef
TR
1039 status = acpi_bus_get_device(tz->trips.passive.devices.
1040 handles[j], &device);
1041 seq_printf(seq, "%4.4s ", status ? "" :
1042 acpi_device_bid(device));
1da177e4
LT
1043 }
1044 seq_puts(seq, "\n");
1045 }
1046
1047 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1048 if (!(tz->trips.active[i].flags.valid))
1049 break;
1050 seq_printf(seq, "active[%d]: %ld C: devices=",
4be44fcd
LB
1051 i,
1052 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
68ccfaa8 1053 for (j = 0; j < tz->trips.active[i].devices.count; j++){
e7c746ef
TR
1054 status = acpi_bus_get_device(tz->trips.active[i].
1055 devices.handles[j],
1056 &device);
1057 seq_printf(seq, "%4.4s ", status ? "" :
1058 acpi_device_bid(device));
68ccfaa8 1059 }
1da177e4
LT
1060 seq_puts(seq, "\n");
1061 }
1062
4be44fcd 1063 end:
d550d98d 1064 return 0;
1da177e4
LT
1065}
1066
1067static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1068{
1069 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1070}
1071
1da177e4
LT
1072static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1073{
50dd0969 1074 struct acpi_thermal *tz = seq->private;
1da177e4 1075
1da177e4
LT
1076
1077 if (!tz)
1078 goto end;
1079
eaca2d3f 1080 if (!tz->flags.cooling_mode)
1da177e4 1081 seq_puts(seq, "<setting not supported>\n");
1da177e4 1082 else
eaca2d3f 1083 seq_puts(seq, "0 - Active; 1 - Passive\n");
1da177e4 1084
4be44fcd 1085 end:
d550d98d 1086 return 0;
1da177e4
LT
1087}
1088
1089static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1090{
1091 return single_open(file, acpi_thermal_cooling_seq_show,
4be44fcd 1092 PDE(inode)->data);
1da177e4
LT
1093}
1094
1095static ssize_t
4be44fcd
LB
1096acpi_thermal_write_cooling_mode(struct file *file,
1097 const char __user * buffer,
1098 size_t count, loff_t * ppos)
1da177e4 1099{
50dd0969
JE
1100 struct seq_file *m = file->private_data;
1101 struct acpi_thermal *tz = m->private;
4be44fcd
LB
1102 int result = 0;
1103 char mode_string[12] = { '\0' };
1da177e4 1104
1da177e4
LT
1105
1106 if (!tz || (count > sizeof(mode_string) - 1))
d550d98d 1107 return -EINVAL;
1da177e4
LT
1108
1109 if (!tz->flags.cooling_mode)
d550d98d 1110 return -ENODEV;
1da177e4
LT
1111
1112 if (copy_from_user(mode_string, buffer, count))
d550d98d 1113 return -EFAULT;
4be44fcd 1114
1da177e4 1115 mode_string[count] = '\0';
4be44fcd
LB
1116
1117 result = acpi_thermal_set_cooling_mode(tz,
1118 simple_strtoul(mode_string, NULL,
1119 0));
1da177e4 1120 if (result)
d550d98d 1121 return result;
1da177e4
LT
1122
1123 acpi_thermal_check(tz);
1124
d550d98d 1125 return count;
1da177e4
LT
1126}
1127
1da177e4
LT
1128static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1129{
50dd0969 1130 struct acpi_thermal *tz = seq->private;
1da177e4 1131
1da177e4
LT
1132
1133 if (!tz)
1134 goto end;
1135
b1569e99 1136 if (!tz->thermal_zone->polling_delay) {
1da177e4
LT
1137 seq_puts(seq, "<polling disabled>\n");
1138 goto end;
1139 }
1140
b1569e99
MG
1141 seq_printf(seq, "polling frequency: %d seconds\n",
1142 (tz->thermal_zone->polling_delay / 1000));
1da177e4 1143
4be44fcd 1144 end:
d550d98d 1145 return 0;
1da177e4
LT
1146}
1147
1148static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1149{
1150 return single_open(file, acpi_thermal_polling_seq_show,
4be44fcd 1151 PDE(inode)->data);
1da177e4
LT
1152}
1153
1154static ssize_t
4be44fcd
LB
1155acpi_thermal_write_polling(struct file *file,
1156 const char __user * buffer,
1157 size_t count, loff_t * ppos)
1da177e4 1158{
50dd0969
JE
1159 struct seq_file *m = file->private_data;
1160 struct acpi_thermal *tz = m->private;
4be44fcd
LB
1161 int result = 0;
1162 char polling_string[12] = { '\0' };
1163 int seconds = 0;
1da177e4 1164
1da177e4
LT
1165
1166 if (!tz || (count > sizeof(polling_string) - 1))
d550d98d 1167 return -EINVAL;
4be44fcd 1168
1da177e4 1169 if (copy_from_user(polling_string, buffer, count))
d550d98d 1170 return -EFAULT;
4be44fcd 1171
1da177e4
LT
1172 polling_string[count] = '\0';
1173
1174 seconds = simple_strtoul(polling_string, NULL, 0);
4be44fcd 1175
1da177e4
LT
1176 result = acpi_thermal_set_polling(tz, seconds);
1177 if (result)
d550d98d 1178 return result;
1da177e4
LT
1179
1180 acpi_thermal_check(tz);
1181
d550d98d 1182 return count;
1da177e4
LT
1183}
1184
4be44fcd 1185static int acpi_thermal_add_fs(struct acpi_device *device)
1da177e4 1186{
4be44fcd 1187 struct proc_dir_entry *entry = NULL;
1da177e4 1188
1da177e4
LT
1189
1190 if (!acpi_device_dir(device)) {
1191 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 1192 acpi_thermal_dir);
1da177e4 1193 if (!acpi_device_dir(device))
d550d98d 1194 return -ENODEV;
1da177e4
LT
1195 }
1196
1197 /* 'state' [R] */
cf7acfab
DL
1198 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1199 S_IRUGO, acpi_device_dir(device),
1200 &acpi_thermal_state_fops,
1201 acpi_driver_data(device));
1da177e4 1202 if (!entry)
d550d98d 1203 return -ENODEV;
1da177e4
LT
1204
1205 /* 'temperature' [R] */
cf7acfab
DL
1206 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1207 S_IRUGO, acpi_device_dir(device),
1208 &acpi_thermal_temp_fops,
1209 acpi_driver_data(device));
1da177e4 1210 if (!entry)
d550d98d 1211 return -ENODEV;
1da177e4 1212
2db9ccba 1213 /* 'trip_points' [R] */
cf7acfab
DL
1214 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1215 S_IRUGO,
1216 acpi_device_dir(device),
1217 &acpi_thermal_trip_fops,
1218 acpi_driver_data(device));
1da177e4 1219 if (!entry)
d550d98d 1220 return -ENODEV;
1da177e4
LT
1221
1222 /* 'cooling_mode' [R/W] */
cf7acfab
DL
1223 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1224 S_IFREG | S_IRUGO | S_IWUSR,
1225 acpi_device_dir(device),
1226 &acpi_thermal_cooling_fops,
1227 acpi_driver_data(device));
1da177e4 1228 if (!entry)
d550d98d 1229 return -ENODEV;
1da177e4
LT
1230
1231 /* 'polling_frequency' [R/W] */
cf7acfab
DL
1232 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1233 S_IFREG | S_IRUGO | S_IWUSR,
1234 acpi_device_dir(device),
1235 &acpi_thermal_polling_fops,
1236 acpi_driver_data(device));
1da177e4 1237 if (!entry)
d550d98d 1238 return -ENODEV;
d550d98d 1239 return 0;
1da177e4
LT
1240}
1241
4be44fcd 1242static int acpi_thermal_remove_fs(struct acpi_device *device)
1da177e4 1243{
1da177e4
LT
1244
1245 if (acpi_device_dir(device)) {
1246 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1247 acpi_device_dir(device));
1248 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1249 acpi_device_dir(device));
1250 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1251 acpi_device_dir(device));
1252 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1253 acpi_device_dir(device));
1254 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1255 acpi_device_dir(device));
1256 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1257 acpi_device_dir(device) = NULL;
1258 }
1259
d550d98d 1260 return 0;
1da177e4
LT
1261}
1262
1da177e4
LT
1263/* --------------------------------------------------------------------------
1264 Driver Interface
1265 -------------------------------------------------------------------------- */
1266
4be44fcd 1267static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1da177e4 1268{
50dd0969 1269 struct acpi_thermal *tz = data;
4be44fcd 1270 struct acpi_device *device = NULL;
1da177e4 1271
1da177e4
LT
1272
1273 if (!tz)
d550d98d 1274 return;
1da177e4 1275
8348e1b1 1276 device = tz->device;
1da177e4
LT
1277
1278 switch (event) {
1279 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1280 acpi_thermal_check(tz);
1281 break;
1282 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
ce44e197 1283 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1da177e4 1284 acpi_thermal_check(tz);
14e04fb3 1285 acpi_bus_generate_proc_event(device, event, 0);
962ce8ca 1286 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 1287 dev_name(&device->dev), event, 0);
1da177e4
LT
1288 break;
1289 case ACPI_THERMAL_NOTIFY_DEVICES:
ce44e197
ZR
1290 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1291 acpi_thermal_check(tz);
14e04fb3 1292 acpi_bus_generate_proc_event(device, event, 0);
962ce8ca 1293 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 1294 dev_name(&device->dev), event, 0);
1da177e4
LT
1295 break;
1296 default:
1297 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1298 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1299 break;
1300 }
1301
d550d98d 1302 return;
1da177e4
LT
1303}
1304
4be44fcd 1305static int acpi_thermal_get_info(struct acpi_thermal *tz)
1da177e4 1306{
4be44fcd 1307 int result = 0;
1da177e4 1308
1da177e4
LT
1309
1310 if (!tz)
d550d98d 1311 return -EINVAL;
1da177e4
LT
1312
1313 /* Get temperature [_TMP] (required) */
1314 result = acpi_thermal_get_temperature(tz);
1315 if (result)
d550d98d 1316 return result;
1da177e4
LT
1317
1318 /* Get trip points [_CRT, _PSV, etc.] (required) */
1319 result = acpi_thermal_get_trip_points(tz);
1320 if (result)
d550d98d 1321 return result;
1da177e4
LT
1322
1323 /* Set the cooling mode [_SCP] to active cooling (default) */
1324 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
4be44fcd 1325 if (!result)
1da177e4 1326 tz->flags.cooling_mode = 1;
1da177e4
LT
1327
1328 /* Get default polling frequency [_TZP] (optional) */
1329 if (tzp)
1330 tz->polling_frequency = tzp;
1331 else
1332 acpi_thermal_get_polling_frequency(tz);
1333
d550d98d 1334 return 0;
1da177e4
LT
1335}
1336
4be44fcd 1337static int acpi_thermal_add(struct acpi_device *device)
1da177e4 1338{
4be44fcd
LB
1339 int result = 0;
1340 acpi_status status = AE_OK;
1341 struct acpi_thermal *tz = NULL;
1da177e4 1342
1da177e4
LT
1343
1344 if (!device)
d550d98d 1345 return -EINVAL;
1da177e4 1346
36bcbec7 1347 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1da177e4 1348 if (!tz)
d550d98d 1349 return -ENOMEM;
1da177e4 1350
8348e1b1 1351 tz->device = device;
1da177e4
LT
1352 strcpy(tz->name, device->pnp.bus_id);
1353 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1354 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
db89b4f0 1355 device->driver_data = tz;
6e215785 1356 mutex_init(&tz->lock);
3f655ef8
ZR
1357
1358
1da177e4
LT
1359 result = acpi_thermal_get_info(tz);
1360 if (result)
3f655ef8
ZR
1361 goto free_memory;
1362
1363 result = acpi_thermal_register_thermal_zone(tz);
1364 if (result)
1365 goto free_memory;
1da177e4
LT
1366
1367 result = acpi_thermal_add_fs(device);
1368 if (result)
3f655ef8 1369 goto unregister_thermal_zone;
1da177e4 1370
38ba7c9e 1371 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
1372 ACPI_DEVICE_NOTIFY,
1373 acpi_thermal_notify, tz);
1da177e4 1374 if (ACPI_FAILURE(status)) {
1da177e4 1375 result = -ENODEV;
3f655ef8 1376 goto remove_fs;
1da177e4
LT
1377 }
1378
1379 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
4be44fcd
LB
1380 acpi_device_name(device), acpi_device_bid(device),
1381 KELVIN_TO_CELSIUS(tz->temperature));
3f655ef8 1382 goto end;
1da177e4 1383
3f655ef8
ZR
1384remove_fs:
1385 acpi_thermal_remove_fs(device);
1386unregister_thermal_zone:
1387 thermal_zone_device_unregister(tz->thermal_zone);
1388free_memory:
1389 kfree(tz);
1390end:
d550d98d 1391 return result;
1da177e4
LT
1392}
1393
4be44fcd 1394static int acpi_thermal_remove(struct acpi_device *device, int type)
1da177e4 1395{
4be44fcd
LB
1396 acpi_status status = AE_OK;
1397 struct acpi_thermal *tz = NULL;
1da177e4 1398
1da177e4 1399 if (!device || !acpi_driver_data(device))
d550d98d 1400 return -EINVAL;
1da177e4 1401
50dd0969 1402 tz = acpi_driver_data(device);
1da177e4 1403
38ba7c9e 1404 status = acpi_remove_notify_handler(device->handle,
4be44fcd
LB
1405 ACPI_DEVICE_NOTIFY,
1406 acpi_thermal_notify);
1da177e4 1407
1da177e4 1408 acpi_thermal_remove_fs(device);
3f655ef8 1409 acpi_thermal_unregister_thermal_zone(tz);
6e215785 1410 mutex_destroy(&tz->lock);
1da177e4 1411 kfree(tz);
d550d98d 1412 return 0;
1da177e4
LT
1413}
1414
5d9464a4 1415static int acpi_thermal_resume(struct acpi_device *device)
74ce1468
KK
1416{
1417 struct acpi_thermal *tz = NULL;
b1028c54
KK
1418 int i, j, power_state, result;
1419
74ce1468
KK
1420
1421 if (!device || !acpi_driver_data(device))
d550d98d 1422 return -EINVAL;
74ce1468 1423
50dd0969 1424 tz = acpi_driver_data(device);
74ce1468 1425
bed936f7 1426 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
b1028c54
KK
1427 if (!(&tz->trips.active[i]))
1428 break;
1429 if (!tz->trips.active[i].flags.valid)
1430 break;
1431 tz->trips.active[i].flags.enabled = 1;
1432 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1433 result = acpi_bus_get_power(tz->trips.active[i].devices.
1434 handles[j], &power_state);
1435 if (result || (power_state != ACPI_STATE_D0)) {
1436 tz->trips.active[i].flags.enabled = 0;
1437 break;
1438 }
bed936f7 1439 }
b1028c54 1440 tz->state.active |= tz->trips.active[i].flags.enabled;
bed936f7
KK
1441 }
1442
b1028c54 1443 acpi_thermal_check(tz);
74ce1468
KK
1444
1445 return AE_OK;
1446}
1447
1855256c 1448static int thermal_act(const struct dmi_system_id *d) {
0b5bfa1c
LB
1449
1450 if (act == 0) {
1451 printk(KERN_NOTICE "ACPI: %s detected: "
1452 "disabling all active thermal trip points\n", d->ident);
1453 act = -1;
1454 }
1455 return 0;
1456}
1855256c 1457static int thermal_nocrt(const struct dmi_system_id *d) {
8c99fdce
LB
1458
1459 printk(KERN_NOTICE "ACPI: %s detected: "
1460 "disabling all critical thermal trip point actions.\n", d->ident);
1461 nocrt = 1;
1462 return 0;
1463}
1855256c 1464static int thermal_tzp(const struct dmi_system_id *d) {
0b5bfa1c
LB
1465
1466 if (tzp == 0) {
1467 printk(KERN_NOTICE "ACPI: %s detected: "
1468 "enabling thermal zone polling\n", d->ident);
1469 tzp = 300; /* 300 dS = 30 Seconds */
1470 }
1471 return 0;
1472}
1855256c 1473static int thermal_psv(const struct dmi_system_id *d) {
0b5bfa1c
LB
1474
1475 if (psv == 0) {
1476 printk(KERN_NOTICE "ACPI: %s detected: "
1477 "disabling all passive thermal trip points\n", d->ident);
1478 psv = -1;
1479 }
1480 return 0;
1481}
1482
1483static struct dmi_system_id thermal_dmi_table[] __initdata = {
1484 /*
1485 * Award BIOS on this AOpen makes thermal control almost worthless.
1486 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1487 */
1488 {
1489 .callback = thermal_act,
1490 .ident = "AOpen i915GMm-HFS",
1491 .matches = {
1492 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1493 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1494 },
1495 },
1496 {
1497 .callback = thermal_psv,
1498 .ident = "AOpen i915GMm-HFS",
1499 .matches = {
1500 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1501 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1502 },
1503 },
1504 {
1505 .callback = thermal_tzp,
1506 .ident = "AOpen i915GMm-HFS",
1507 .matches = {
1508 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1509 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1510 },
1511 },
8c99fdce
LB
1512 {
1513 .callback = thermal_nocrt,
1514 .ident = "Gigabyte GA-7ZX",
1515 .matches = {
1516 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1517 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1518 },
1519 },
0b5bfa1c
LB
1520 {}
1521};
0b5bfa1c 1522
4be44fcd 1523static int __init acpi_thermal_init(void)
1da177e4 1524{
4be44fcd 1525 int result = 0;
1da177e4 1526
0b5bfa1c
LB
1527 dmi_check_system(thermal_dmi_table);
1528
72b33ef8
LB
1529 if (off) {
1530 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1531 return -ENODEV;
1532 }
1da177e4
LT
1533 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1534 if (!acpi_thermal_dir)
d550d98d 1535 return -ENODEV;
1da177e4
LT
1536
1537 result = acpi_bus_register_driver(&acpi_thermal_driver);
1538 if (result < 0) {
1539 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
d550d98d 1540 return -ENODEV;
1da177e4
LT
1541 }
1542
d550d98d 1543 return 0;
1da177e4
LT
1544}
1545
4be44fcd 1546static void __exit acpi_thermal_exit(void)
1da177e4 1547{
1da177e4
LT
1548
1549 acpi_bus_unregister_driver(&acpi_thermal_driver);
1550
1551 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1552
d550d98d 1553 return;
1da177e4
LT
1554}
1555
1da177e4
LT
1556module_init(acpi_thermal_init);
1557module_exit(acpi_thermal_exit);
This page took 0.493975 seconds and 5 git commands to generate.