blob: e7fe3a14fdafd46e88851f348657835555e7e484 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/types.h>
38#include <linux/proc_fs.h>
39#include <linux/sched.h>
40#include <linux/kmod.h>
41#include <linux/seq_file.h>
42#include <asm/uaccess.h>
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
47#define ACPI_THERMAL_COMPONENT 0x04000000
48#define ACPI_THERMAL_CLASS "thermal_zone"
49#define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
50#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
51#define ACPI_THERMAL_FILE_STATE "state"
52#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
53#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
54#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
55#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
56#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
58#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
59#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
60#define ACPI_THERMAL_NOTIFY_HOT 0xF1
61#define ACPI_THERMAL_MODE_ACTIVE 0x00
62#define ACPI_THERMAL_MODE_PASSIVE 0x01
63#define ACPI_THERMAL_MODE_CRITICAL 0xff
64#define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
65
66#define ACPI_THERMAL_MAX_ACTIVE 10
67#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
69#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
71
72#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040073ACPI_MODULE_NAME("acpi_thermal")
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040075MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77MODULE_LICENSE("GPL");
78
79static int tzp;
80module_param(tzp, int, 0);
81MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_thermal_add(struct acpi_device *device);
84static int acpi_thermal_remove(struct acpi_device *device, int type);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -040085static int acpi_thermal_resume(struct acpi_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
87static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
88static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040089static ssize_t acpi_thermal_write_trip_points(struct file *,
90 const char __user *, size_t,
91 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040093static ssize_t acpi_thermal_write_cooling_mode(struct file *,
94 const char __user *, size_t,
95 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040097static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
98 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static struct acpi_driver acpi_thermal_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400101 .name = ACPI_THERMAL_DRIVER_NAME,
102 .class = ACPI_THERMAL_CLASS,
103 .ids = ACPI_THERMAL_HID,
104 .ops = {
105 .add = acpi_thermal_add,
106 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400107 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 u8 critical:1;
113 u8 hot:1;
114 u8 passive:1;
115 u8 active:1;
116 u8 reserved:4;
117 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 valid:1;
122 u8 enabled:1;
123 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_thermal_critical {
127 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400128 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_hot {
132 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_thermal_passive {
137 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 unsigned long temperature;
139 unsigned long tc1;
140 unsigned long tc2;
141 unsigned long tsp;
142 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct acpi_thermal_active {
146 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 unsigned long temperature;
148 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct acpi_thermal_trips {
152 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_thermal_passive passive;
155 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
156};
157
158struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 cooling_mode:1; /* _SCP */
160 u8 devices:1; /* _TZD */
161 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_thermal {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 acpi_handle handle;
166 acpi_bus_id name;
167 unsigned long temperature;
168 unsigned long last_temperature;
169 unsigned long polling_frequency;
170 u8 cooling_mode;
171 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct acpi_thermal_flags flags;
173 struct acpi_thermal_state state;
174 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 struct acpi_handle_list devices;
176 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179static struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 .open = acpi_thermal_state_open_fs,
181 .read = seq_read,
182 .llseek = seq_lseek,
183 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186static struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 .open = acpi_thermal_temp_open_fs,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193static struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_thermal_trip_open_fs,
195 .read = seq_read,
196 .write = acpi_thermal_write_trip_points,
197 .llseek = seq_lseek,
198 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201static struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_cooling_open_fs,
203 .read = seq_read,
204 .write = acpi_thermal_write_cooling_mode,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209static struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400210 .open = acpi_thermal_polling_open_fs,
211 .read = seq_read,
212 .write = acpi_thermal_write_polling,
213 .llseek = seq_lseek,
214 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217/* --------------------------------------------------------------------------
218 Thermal Zone Management
219 -------------------------------------------------------------------------- */
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Len Brown4be44fc2005-08-05 00:44:28 -0400223 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 ACPI_FUNCTION_TRACE("acpi_thermal_get_temperature");
226
227 if (!tz)
228 return_VALUE(-EINVAL);
229
230 tz->last_temperature = tz->temperature;
231
Len Brown4be44fc2005-08-05 00:44:28 -0400232 status =
233 acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (ACPI_FAILURE(status))
235 return_VALUE(-ENODEV);
236
Len Brown4be44fc2005-08-05 00:44:28 -0400237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
238 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return_VALUE(0);
241}
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Len Brown4be44fc2005-08-05 00:44:28 -0400245 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 ACPI_FUNCTION_TRACE("acpi_thermal_get_polling_frequency");
248
249 if (!tz)
250 return_VALUE(-EINVAL);
251
Len Brown4be44fc2005-08-05 00:44:28 -0400252 status =
253 acpi_evaluate_integer(tz->handle, "_TZP", NULL,
254 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ACPI_FAILURE(status))
256 return_VALUE(-ENODEV);
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
259 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return_VALUE(0);
262}
263
Len Brown4be44fc2005-08-05 00:44:28 -0400264static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 ACPI_FUNCTION_TRACE("acpi_thermal_set_polling");
267
268 if (!tz)
269 return_VALUE(-EINVAL);
270
271 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "Polling frequency set to %lu seconds\n",
275 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return_VALUE(0);
278}
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Len Brown4be44fc2005-08-05 00:44:28 -0400282 acpi_status status = AE_OK;
283 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
284 struct acpi_object_list arg_list = { 1, &arg0 };
285 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 ACPI_FUNCTION_TRACE("acpi_thermal_set_cooling_mode");
288
289 if (!tz)
290 return_VALUE(-EINVAL);
291
292 status = acpi_get_handle(tz->handle, "_SCP", &handle);
293 if (ACPI_FAILURE(status)) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
295 return_VALUE(-ENODEV);
296 }
297
298 arg0.integer.value = mode;
299
300 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
301 if (ACPI_FAILURE(status))
302 return_VALUE(-ENODEV);
303
304 tz->cooling_mode = mode;
305
Len Brown4be44fc2005-08-05 00:44:28 -0400306 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
307 mode ? "passive" : "active"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return_VALUE(0);
310}
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Len Brown4be44fc2005-08-05 00:44:28 -0400314 acpi_status status = AE_OK;
315 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 ACPI_FUNCTION_TRACE("acpi_thermal_get_trip_points");
318
319 if (!tz)
320 return_VALUE(-EINVAL);
321
322 /* Critical Shutdown (required) */
323
Len Brown4be44fc2005-08-05 00:44:28 -0400324 status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
325 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (ACPI_FAILURE(status)) {
327 tz->trips.critical.flags.valid = 0;
328 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical threshold\n"));
329 return_VALUE(-ENODEV);
Len Brown4be44fc2005-08-05 00:44:28 -0400330 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400332 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
333 "Found critical threshold [%lu]\n",
334 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 /* Critical Sleep (optional) */
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339 status =
340 acpi_evaluate_integer(tz->handle, "_HOT", NULL,
341 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ACPI_FAILURE(status)) {
343 tz->trips.hot.flags.valid = 0;
344 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400345 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
348 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 /* Passive: Processors (optional) */
352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 status =
354 acpi_evaluate_integer(tz->handle, "_PSV", NULL,
355 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (ACPI_FAILURE(status)) {
357 tz->trips.passive.flags.valid = 0;
358 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400359 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 tz->trips.passive.flags.valid = 1;
361
Len Brown4be44fc2005-08-05 00:44:28 -0400362 status =
363 acpi_evaluate_integer(tz->handle, "_TC1", NULL,
364 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (ACPI_FAILURE(status))
366 tz->trips.passive.flags.valid = 0;
367
Len Brown4be44fc2005-08-05 00:44:28 -0400368 status =
369 acpi_evaluate_integer(tz->handle, "_TC2", NULL,
370 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (ACPI_FAILURE(status))
372 tz->trips.passive.flags.valid = 0;
373
Len Brown4be44fc2005-08-05 00:44:28 -0400374 status =
375 acpi_evaluate_integer(tz->handle, "_TSP", NULL,
376 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (ACPI_FAILURE(status))
378 tz->trips.passive.flags.valid = 0;
379
Len Brown4be44fc2005-08-05 00:44:28 -0400380 status =
381 acpi_evaluate_reference(tz->handle, "_PSL", NULL,
382 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ACPI_FAILURE(status))
384 tz->trips.passive.flags.valid = 0;
385
386 if (!tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400387 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
388 "Invalid passive threshold\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 else
Len Brown4be44fc2005-08-05 00:44:28 -0400390 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
391 "Found passive threshold [%lu]\n",
392 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 /* Active: Fans, etc. (optional) */
396
Len Brown4be44fc2005-08-05 00:44:28 -0400397 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Len Brown4be44fc2005-08-05 00:44:28 -0400399 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Len Brown4be44fc2005-08-05 00:44:28 -0400401 status =
402 acpi_evaluate_integer(tz->handle, name, NULL,
403 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ACPI_FAILURE(status))
405 break;
406
407 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400408 status =
409 acpi_evaluate_reference(tz->handle, name, NULL,
410 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (ACPI_SUCCESS(status)) {
412 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400413 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
414 "Found active threshold [%d]:[%lu]\n",
415 i, tz->trips.active[i].temperature));
416 } else
417 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
418 "Invalid active threshold [%d]\n",
419 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 return_VALUE(0);
423}
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Len Brown4be44fc2005-08-05 00:44:28 -0400427 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 ACPI_FUNCTION_TRACE("acpi_thermal_get_devices");
430
431 if (!tz)
432 return_VALUE(-EINVAL);
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 status =
435 acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ACPI_FAILURE(status))
437 return_VALUE(-ENODEV);
438
439 return_VALUE(0);
440}
441
Len Brown4be44fc2005-08-05 00:44:28 -0400442static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Len Brown4be44fc2005-08-05 00:44:28 -0400444 char *argv[2] = { NULL, NULL };
445 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 ACPI_FUNCTION_TRACE("acpi_thermal_call_usermode");
448
449 if (!path)
450 return_VALUE(-EINVAL);
451
452 argv[0] = path;
453
454 /* minimal command environment */
455 envp[0] = "HOME=/";
456 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 call_usermodehelper(argv[0], argv, envp, 0);
459
460 return_VALUE(0);
461}
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Len Brown4be44fc2005-08-05 00:44:28 -0400465 int result = 0;
466 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 ACPI_FUNCTION_TRACE("acpi_thermal_critical");
469
470 if (!tz || !tz->trips.critical.flags.valid)
471 return_VALUE(-EINVAL);
472
473 if (tz->temperature >= tz->trips.critical.temperature) {
474 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Critical trip point\n"));
475 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400476 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 tz->trips.critical.flags.enabled = 0;
478
479 result = acpi_bus_get_device(tz->handle, &device);
480 if (result)
481 return_VALUE(result);
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483 printk(KERN_EMERG
484 "Critical temperature reached (%ld C), shutting down.\n",
485 KELVIN_TO_CELSIUS(tz->temperature));
486 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
487 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
490
491 return_VALUE(0);
492}
493
Len Brown4be44fc2005-08-05 00:44:28 -0400494static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Len Brown4be44fc2005-08-05 00:44:28 -0400496 int result = 0;
497 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 ACPI_FUNCTION_TRACE("acpi_thermal_hot");
500
501 if (!tz || !tz->trips.hot.flags.valid)
502 return_VALUE(-EINVAL);
503
504 if (tz->temperature >= tz->trips.hot.temperature) {
505 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Hot trip point\n"));
506 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400507 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 tz->trips.hot.flags.enabled = 0;
509
510 result = acpi_bus_get_device(tz->handle, &device);
511 if (result)
512 return_VALUE(result);
513
Len Brown4be44fc2005-08-05 00:44:28 -0400514 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
515 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* TBD: Call user-mode "sleep(S4)" function */
518
519 return_VALUE(0);
520}
521
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400522static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400524 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400526 int trend = 0;
527 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 ACPI_FUNCTION_TRACE("acpi_thermal_passive");
530
531 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 passive = &(tz->trips.passive);
535
536 /*
537 * Above Trip?
538 * -----------
539 * Calculate the thermal trend (using the passive cooling equation)
540 * and modify the performance limit for all passive cooling devices
541 * accordingly. Note that we assume symmetry.
542 */
543 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400544 trend =
545 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
546 (passive->tc2 * (tz->temperature - passive->temperature));
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
549 trend, passive->tc1, tz->temperature,
550 tz->last_temperature, passive->tc2,
551 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400552 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Heating up? */
554 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400555 for (i = 0; i < passive->devices.count; i++)
556 acpi_processor_set_thermal_limit(passive->
557 devices.
558 handles[i],
559 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400561 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400562 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400563 /*
564 * assume that we are on highest
565 * freq/lowest thrott and can leave
566 * passive mode, even in error case
567 */
568 if (!acpi_processor_set_thermal_limit
569 (passive->devices.handles[i],
570 ACPI_PROCESSOR_LIMIT_DECREMENT))
571 result = 0;
572 /*
573 * Leave cooling mode, even if the temp might
574 * higher than trip point This is because some
575 * machines might have long thermal polling
576 * frequencies (tsp) defined. We will fall back
577 * into passive mode in next cycle (probably quicker)
578 */
579 if (result) {
580 passive->flags.enabled = 0;
581 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
582 "Disabling passive cooling, still above threshold,"
583 " but we are cooling down\n"));
584 }
585 }
586 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 /*
590 * Below Trip?
591 * -----------
592 * Implement passive cooling hysteresis to slowly increase performance
593 * and avoid thrashing around the passive trip point. Note that we
594 * assume symmetry.
595 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400596 if (!passive->flags.enabled)
597 return;
598 for (i = 0; i < passive->devices.count; i++)
599 if (!acpi_processor_set_thermal_limit
600 (passive->devices.handles[i],
601 ACPI_PROCESSOR_LIMIT_DECREMENT))
602 result = 0;
603 if (result) {
604 passive->flags.enabled = 0;
605 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
606 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400610static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown4be44fc2005-08-05 00:44:28 -0400612 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400614 int i = 0;
615 int j = 0;
616 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 ACPI_FUNCTION_TRACE("acpi_thermal_active");
619
620 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400621 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Len Brown4be44fc2005-08-05 00:44:28 -0400623 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 active = &(tz->trips.active[i]);
625 if (!active || !active->flags.valid)
626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400628 /*
629 * Above Threshold?
630 * ----------------
631 * If not already enabled, turn ON all cooling devices
632 * associated with this active threshold.
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400635 tz->state.active_index = i;
636 maxtemp = active->temperature;
637 if (active->flags.enabled)
638 continue;
639 for (j = 0; j < active->devices.count; j++) {
640 result =
641 acpi_bus_set_power(active->devices.
642 handles[j],
643 ACPI_STATE_D0);
644 if (result) {
645 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
646 "Unable to turn cooling device [%p] 'on'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400647 active->devices.
648 handles[j]));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400649 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400651 active->flags.enabled = 1;
652 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
653 "Cooling device [%p] now 'on'\n",
654 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400656 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400658 if (!active->flags.enabled)
659 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * Below Threshold?
662 * ----------------
663 * Turn OFF all cooling devices associated with this
664 * threshold.
665 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400666 for (j = 0; j < active->devices.count; j++) {
667 result = acpi_bus_set_power(active->devices.handles[j],
668 ACPI_STATE_D3);
669 if (result) {
670 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
671 "Unable to turn cooling device [%p] 'off'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400672 active->devices.handles[j]));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400673 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400675 active->flags.enabled = 0;
676 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
677 "Cooling device [%p] now 'off'\n",
678 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Len Brown4be44fc2005-08-05 00:44:28 -0400683static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Len Brown4be44fc2005-08-05 00:44:28 -0400685static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct acpi_thermal *tz = (struct acpi_thermal *)data;
688 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400689 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Len Brown4be44fc2005-08-05 00:44:28 -0400692static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Len Brown4be44fc2005-08-05 00:44:28 -0400694 int result = 0;
695 struct acpi_thermal *tz = (struct acpi_thermal *)data;
696 unsigned long sleep_time = 0;
697 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct acpi_thermal_state state;
699
700 ACPI_FUNCTION_TRACE("acpi_thermal_check");
701
702 if (!tz) {
703 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
704 return_VOID;
705 }
706
707 state = tz->state;
708
709 result = acpi_thermal_get_temperature(tz);
710 if (result)
711 return_VOID;
Len Brown4be44fc2005-08-05 00:44:28 -0400712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * Check Trip Points
717 * -----------------
718 * Compare the current temperature to the trip point values to see
719 * if we've entered one of the thermal policy states. Note that
720 * this function determines when a state is entered, but the
721 * individual policy decides when it is exited (e.g. hysteresis).
722 */
723 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400724 state.critical |=
725 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (tz->trips.hot.flags.valid)
727 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
728 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400729 state.passive |=
730 (tz->temperature >= tz->trips.passive.temperature);
731 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400733 state.active |=
734 (tz->temperature >=
735 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Invoke Policy
739 * -------------
740 * Separated from the above check to allow individual policy to
741 * determine when to exit a given state.
742 */
743 if (state.critical)
744 acpi_thermal_critical(tz);
745 if (state.hot)
746 acpi_thermal_hot(tz);
747 if (state.passive)
748 acpi_thermal_passive(tz);
749 if (state.active)
750 acpi_thermal_active(tz);
751
752 /*
753 * Calculate State
754 * ---------------
755 * Again, separated from the above two to allow independent policy
756 * decisions.
757 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400758 tz->state.critical = tz->trips.critical.flags.enabled;
759 tz->state.hot = tz->trips.hot.flags.enabled;
760 tz->state.passive = tz->trips.passive.flags.enabled;
761 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400762 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400763 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
766 * Calculate Sleep Time
767 * --------------------
768 * If we're in the passive state, use _TSP's value. Otherwise
769 * use the default polling frequency (e.g. _TZP). If no polling
770 * frequency is specified then we'll wait forever (at least until
771 * a thermal event occurs). Note that _TSP and _TZD values are
772 * given in 1/10th seconds (we must covert to milliseconds).
773 */
774 if (tz->state.passive)
775 sleep_time = tz->trips.passive.tsp * 100;
776 else if (tz->polling_frequency > 0)
777 sleep_time = tz->polling_frequency * 100;
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
780 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
783 * Schedule Next Poll
784 * ------------------
785 */
786 if (!sleep_time) {
787 if (timer_pending(&(tz->timer)))
788 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400789 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (timer_pending(&(tz->timer)))
791 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
792 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400793 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 tz->timer.function = acpi_thermal_run;
795 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
796 add_timer(&(tz->timer));
797 }
798 }
799
800 return_VOID;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/* --------------------------------------------------------------------------
804 FS Interface (/proc)
805 -------------------------------------------------------------------------- */
806
Len Brown4be44fc2005-08-05 00:44:28 -0400807static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
810{
Len Brown4be44fc2005-08-05 00:44:28 -0400811 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
814
815 if (!tz)
816 goto end;
817
818 seq_puts(seq, "state: ");
819
Len Brown4be44fc2005-08-05 00:44:28 -0400820 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
821 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 seq_puts(seq, "ok\n");
823 else {
824 if (tz->state.critical)
825 seq_puts(seq, "critical ");
826 if (tz->state.hot)
827 seq_puts(seq, "hot ");
828 if (tz->state.passive)
829 seq_puts(seq, "passive ");
830 if (tz->state.active)
831 seq_printf(seq, "active[%d]", tz->state.active_index);
832 seq_puts(seq, "\n");
833 }
834
Len Brown4be44fc2005-08-05 00:44:28 -0400835 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return_VALUE(0);
837}
838
839static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
840{
841 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
845{
Len Brown4be44fc2005-08-05 00:44:28 -0400846 int result = 0;
847 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
850
851 if (!tz)
852 goto end;
853
854 result = acpi_thermal_get_temperature(tz);
855 if (result)
856 goto end;
857
Len Brown4be44fc2005-08-05 00:44:28 -0400858 seq_printf(seq, "temperature: %ld C\n",
859 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Len Brown4be44fc2005-08-05 00:44:28 -0400861 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return_VALUE(0);
863}
864
865static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
866{
867 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
871{
Len Brown4be44fc2005-08-05 00:44:28 -0400872 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
873 int i = 0;
874 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
877
878 if (!tz)
879 goto end;
880
881 if (tz->trips.critical.flags.valid)
882 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400883 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (tz->trips.hot.flags.valid)
886 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400887 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400890 seq_printf(seq,
891 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
892 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
893 tz->trips.passive.tc1, tz->trips.passive.tc2,
894 tz->trips.passive.tsp);
895 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Len Brown4be44fc2005-08-05 00:44:28 -0400897 seq_printf(seq, "0x%p ",
898 tz->trips.passive.devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 seq_puts(seq, "\n");
901 }
902
903 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
904 if (!(tz->trips.active[i].flags.valid))
905 break;
906 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400907 i,
908 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
909 for (j = 0; j < tz->trips.active[i].devices.count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 seq_printf(seq, "0x%p ",
Len Brown4be44fc2005-08-05 00:44:28 -0400911 tz->trips.active[i].devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 seq_puts(seq, "\n");
913 }
914
Len Brown4be44fc2005-08-05 00:44:28 -0400915 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return_VALUE(0);
917}
918
919static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
920{
921 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
922}
923
924static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400925acpi_thermal_write_trip_points(struct file *file,
926 const char __user * buffer,
927 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Len Brown4be44fc2005-08-05 00:44:28 -0400929 struct seq_file *m = (struct seq_file *)file->private_data;
930 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Len Brown4be44fc2005-08-05 00:44:28 -0400932 char *limit_string;
933 int num, critical, hot, passive;
934 int *active;
935 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
938
939 limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400940 if (!limit_string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return_VALUE(-ENOMEM);
942
943 memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
944
Len Brown4be44fc2005-08-05 00:44:28 -0400945 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500946 if (!active) {
947 kfree(limit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return_VALUE(-ENOMEM);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
952 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
953 count = -EINVAL;
954 goto end;
955 }
Len Brown4be44fc2005-08-05 00:44:28 -0400956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (copy_from_user(limit_string, buffer, count)) {
958 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
959 count = -EFAULT;
960 goto end;
961 }
Len Brown4be44fc2005-08-05 00:44:28 -0400962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 limit_string[count] = '\0';
964
965 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
Len Brown4be44fc2005-08-05 00:44:28 -0400966 &critical, &hot, &passive,
967 &active[0], &active[1], &active[2], &active[3], &active[4],
968 &active[5], &active[6], &active[7], &active[8],
969 &active[9]);
970 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
972 count = -EINVAL;
973 goto end;
974 }
975
976 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
977 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
978 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
979 for (i = 0; i < num - 3; i++) {
980 if (!(tz->trips.active[i].flags.valid))
981 break;
982 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
983 }
Len Brown4be44fc2005-08-05 00:44:28 -0400984
985 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 kfree(active);
987 kfree(limit_string);
988 return_VALUE(count);
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
992{
Len Brown4be44fc2005-08-05 00:44:28 -0400993 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
996
997 if (!tz)
998 goto end;
999
1000 if (!tz->flags.cooling_mode) {
1001 seq_puts(seq, "<setting not supported>\n");
1002 }
1003
Len Brown4be44fc2005-08-05 00:44:28 -04001004 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 seq_printf(seq, "cooling mode: critical\n");
1006 else
1007 seq_printf(seq, "cooling mode: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001008 tz->cooling_mode ? "passive" : "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Len Brown4be44fc2005-08-05 00:44:28 -04001010 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return_VALUE(0);
1012}
1013
1014static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1015{
1016 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001017 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001021acpi_thermal_write_cooling_mode(struct file *file,
1022 const char __user * buffer,
1023 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Len Brown4be44fc2005-08-05 00:44:28 -04001025 struct seq_file *m = (struct seq_file *)file->private_data;
1026 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1027 int result = 0;
1028 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
1031
1032 if (!tz || (count > sizeof(mode_string) - 1))
1033 return_VALUE(-EINVAL);
1034
1035 if (!tz->flags.cooling_mode)
1036 return_VALUE(-ENODEV);
1037
1038 if (copy_from_user(mode_string, buffer, count))
1039 return_VALUE(-EFAULT);
Len Brown4be44fc2005-08-05 00:44:28 -04001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001042
1043 result = acpi_thermal_set_cooling_mode(tz,
1044 simple_strtoul(mode_string, NULL,
1045 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (result)
1047 return_VALUE(result);
1048
1049 acpi_thermal_check(tz);
1050
1051 return_VALUE(count);
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1055{
Len Brown4be44fc2005-08-05 00:44:28 -04001056 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
1059
1060 if (!tz)
1061 goto end;
1062
1063 if (!tz->polling_frequency) {
1064 seq_puts(seq, "<polling disabled>\n");
1065 goto end;
1066 }
1067
1068 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001069 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Len Brown4be44fc2005-08-05 00:44:28 -04001071 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return_VALUE(0);
1073}
1074
1075static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1076{
1077 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001078 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001082acpi_thermal_write_polling(struct file *file,
1083 const char __user * buffer,
1084 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Len Brown4be44fc2005-08-05 00:44:28 -04001086 struct seq_file *m = (struct seq_file *)file->private_data;
1087 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1088 int result = 0;
1089 char polling_string[12] = { '\0' };
1090 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
1093
1094 if (!tz || (count > sizeof(polling_string) - 1))
1095 return_VALUE(-EINVAL);
Len Brown4be44fc2005-08-05 00:44:28 -04001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (copy_from_user(polling_string, buffer, count))
1098 return_VALUE(-EFAULT);
Len Brown4be44fc2005-08-05 00:44:28 -04001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 polling_string[count] = '\0';
1101
1102 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 result = acpi_thermal_set_polling(tz, seconds);
1105 if (result)
1106 return_VALUE(result);
1107
1108 acpi_thermal_check(tz);
1109
1110 return_VALUE(count);
1111}
1112
Len Brown4be44fc2005-08-05 00:44:28 -04001113static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Len Brown4be44fc2005-08-05 00:44:28 -04001115 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
1118
1119 if (!acpi_device_dir(device)) {
1120 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001121 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!acpi_device_dir(device))
1123 return_VALUE(-ENODEV);
1124 acpi_device_dir(device)->owner = THIS_MODULE;
1125 }
1126
1127 /* 'state' [R] */
1128 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001129 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!entry)
1131 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001132 "Unable to create '%s' fs entry\n",
1133 ACPI_THERMAL_FILE_STATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 else {
1135 entry->proc_fops = &acpi_thermal_state_fops;
1136 entry->data = acpi_driver_data(device);
1137 entry->owner = THIS_MODULE;
1138 }
1139
1140 /* 'temperature' [R] */
1141 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001142 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!entry)
1144 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001145 "Unable to create '%s' fs entry\n",
1146 ACPI_THERMAL_FILE_TEMPERATURE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 else {
1148 entry->proc_fops = &acpi_thermal_temp_fops;
1149 entry->data = acpi_driver_data(device);
1150 entry->owner = THIS_MODULE;
1151 }
1152
1153 /* 'trip_points' [R/W] */
1154 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001155 S_IFREG | S_IRUGO | S_IWUSR,
1156 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!entry)
1158 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001159 "Unable to create '%s' fs entry\n",
1160 ACPI_THERMAL_FILE_TRIP_POINTS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 else {
1162 entry->proc_fops = &acpi_thermal_trip_fops;
1163 entry->data = acpi_driver_data(device);
1164 entry->owner = THIS_MODULE;
1165 }
1166
1167 /* 'cooling_mode' [R/W] */
1168 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001169 S_IFREG | S_IRUGO | S_IWUSR,
1170 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!entry)
1172 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001173 "Unable to create '%s' fs entry\n",
1174 ACPI_THERMAL_FILE_COOLING_MODE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 else {
1176 entry->proc_fops = &acpi_thermal_cooling_fops;
1177 entry->data = acpi_driver_data(device);
1178 entry->owner = THIS_MODULE;
1179 }
1180
1181 /* 'polling_frequency' [R/W] */
1182 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001183 S_IFREG | S_IRUGO | S_IWUSR,
1184 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!entry)
1186 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001187 "Unable to create '%s' fs entry\n",
1188 ACPI_THERMAL_FILE_POLLING_FREQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 else {
1190 entry->proc_fops = &acpi_thermal_polling_fops;
1191 entry->data = acpi_driver_data(device);
1192 entry->owner = THIS_MODULE;
1193 }
1194
1195 return_VALUE(0);
1196}
1197
Len Brown4be44fc2005-08-05 00:44:28 -04001198static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
1201
1202 if (acpi_device_dir(device)) {
1203 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1204 acpi_device_dir(device));
1205 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1206 acpi_device_dir(device));
1207 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1208 acpi_device_dir(device));
1209 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1210 acpi_device_dir(device));
1211 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1212 acpi_device_dir(device));
1213 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1214 acpi_device_dir(device) = NULL;
1215 }
1216
1217 return_VALUE(0);
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/* --------------------------------------------------------------------------
1221 Driver Interface
1222 -------------------------------------------------------------------------- */
1223
Len Brown4be44fc2005-08-05 00:44:28 -04001224static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Len Brown4be44fc2005-08-05 00:44:28 -04001226 struct acpi_thermal *tz = (struct acpi_thermal *)data;
1227 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 ACPI_FUNCTION_TRACE("acpi_thermal_notify");
1230
1231 if (!tz)
1232 return_VOID;
1233
1234 if (acpi_bus_get_device(tz->handle, &device))
1235 return_VOID;
1236
1237 switch (event) {
1238 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1239 acpi_thermal_check(tz);
1240 break;
1241 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1242 acpi_thermal_get_trip_points(tz);
1243 acpi_thermal_check(tz);
1244 acpi_bus_generate_event(device, event, 0);
1245 break;
1246 case ACPI_THERMAL_NOTIFY_DEVICES:
1247 if (tz->flags.devices)
1248 acpi_thermal_get_devices(tz);
1249 acpi_bus_generate_event(device, event, 0);
1250 break;
1251 default:
1252 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001253 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
1255 }
1256
1257 return_VOID;
1258}
1259
Len Brown4be44fc2005-08-05 00:44:28 -04001260static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Len Brown4be44fc2005-08-05 00:44:28 -04001262 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
1265
1266 if (!tz)
1267 return_VALUE(-EINVAL);
1268
1269 /* Get temperature [_TMP] (required) */
1270 result = acpi_thermal_get_temperature(tz);
1271 if (result)
1272 return_VALUE(result);
1273
1274 /* Get trip points [_CRT, _PSV, etc.] (required) */
1275 result = acpi_thermal_get_trip_points(tz);
1276 if (result)
1277 return_VALUE(result);
1278
1279 /* Set the cooling mode [_SCP] to active cooling (default) */
1280 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001281 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 tz->flags.cooling_mode = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001283 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* Oh,we have not _SCP method.
Len Brown4be44fc2005-08-05 00:44:28 -04001285 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 tz->flags.cooling_mode = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001287 if (tz->trips.active[0].flags.valid
1288 && tz->trips.passive.flags.valid) {
1289 if (tz->trips.passive.temperature >
1290 tz->trips.active[0].temperature)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001292 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001294 } else if (!tz->trips.active[0].flags.valid
1295 && tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001297 } else if (tz->trips.active[0].flags.valid
1298 && !tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1300 } else {
1301 /* _ACx and _PSV are optional, but _CRT is required */
1302 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1303 }
1304 }
1305
1306 /* Get default polling frequency [_TZP] (optional) */
1307 if (tzp)
1308 tz->polling_frequency = tzp;
1309 else
1310 acpi_thermal_get_polling_frequency(tz);
1311
1312 /* Get devices in this thermal zone [_TZD] (optional) */
1313 result = acpi_thermal_get_devices(tz);
1314 if (!result)
1315 tz->flags.devices = 1;
1316
1317 return_VALUE(0);
1318}
1319
Len Brown4be44fc2005-08-05 00:44:28 -04001320static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Len Brown4be44fc2005-08-05 00:44:28 -04001322 int result = 0;
1323 acpi_status status = AE_OK;
1324 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 ACPI_FUNCTION_TRACE("acpi_thermal_add");
1327
1328 if (!device)
1329 return_VALUE(-EINVAL);
1330
1331 tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1332 if (!tz)
1333 return_VALUE(-ENOMEM);
1334 memset(tz, 0, sizeof(struct acpi_thermal));
1335
1336 tz->handle = device->handle;
1337 strcpy(tz->name, device->pnp.bus_id);
1338 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1339 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1340 acpi_driver_data(device) = tz;
1341
1342 result = acpi_thermal_get_info(tz);
1343 if (result)
1344 goto end;
1345
1346 result = acpi_thermal_add_fs(device);
1347 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001348 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 init_timer(&tz->timer);
1351
1352 acpi_thermal_check(tz);
1353
1354 status = acpi_install_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001355 ACPI_DEVICE_NOTIFY,
1356 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (ACPI_FAILURE(status)) {
1358 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001359 "Error installing notify handler\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 result = -ENODEV;
1361 goto end;
1362 }
1363
1364 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001365 acpi_device_name(device), acpi_device_bid(device),
1366 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Len Brown4be44fc2005-08-05 00:44:28 -04001368 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (result) {
1370 acpi_thermal_remove_fs(device);
1371 kfree(tz);
1372 }
1373
1374 return_VALUE(result);
1375}
1376
Len Brown4be44fc2005-08-05 00:44:28 -04001377static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Len Brown4be44fc2005-08-05 00:44:28 -04001379 acpi_status status = AE_OK;
1380 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 ACPI_FUNCTION_TRACE("acpi_thermal_remove");
1383
1384 if (!device || !acpi_driver_data(device))
1385 return_VALUE(-EINVAL);
1386
Len Brown4be44fc2005-08-05 00:44:28 -04001387 tz = (struct acpi_thermal *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* avoid timer adding new defer task */
1390 tz->zombie = 1;
1391 /* wait for running timer (on other CPUs) finish */
1392 del_timer_sync(&(tz->timer));
1393 /* synchronize deferred task */
1394 acpi_os_wait_events_complete(NULL);
1395 /* deferred task may reinsert timer */
1396 del_timer_sync(&(tz->timer));
1397
1398 status = acpi_remove_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001399 ACPI_DEVICE_NOTIFY,
1400 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (ACPI_FAILURE(status))
1402 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001403 "Error removing notify handler\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001406 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 tz->trips.passive.flags.enabled = 0;
1408 acpi_thermal_passive(tz);
1409 }
1410 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001411 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 tz->trips.active[0].flags.enabled = 0;
1413 acpi_thermal_active(tz);
1414 }
1415
1416 acpi_thermal_remove_fs(device);
1417
1418 kfree(tz);
1419 return_VALUE(0);
1420}
1421
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001422static int acpi_thermal_resume(struct acpi_device *device, int state)
1423{
1424 struct acpi_thermal *tz = NULL;
1425
1426 if (!device || !acpi_driver_data(device))
1427 return_VALUE(-EINVAL);
1428
1429 tz = (struct acpi_thermal *)acpi_driver_data(device);
1430
1431 acpi_thermal_check(tz);
1432
1433 return AE_OK;
1434}
1435
Len Brown4be44fc2005-08-05 00:44:28 -04001436static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Len Brown4be44fc2005-08-05 00:44:28 -04001438 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 ACPI_FUNCTION_TRACE("acpi_thermal_init");
1441
1442 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1443 if (!acpi_thermal_dir)
1444 return_VALUE(-ENODEV);
1445 acpi_thermal_dir->owner = THIS_MODULE;
1446
1447 result = acpi_bus_register_driver(&acpi_thermal_driver);
1448 if (result < 0) {
1449 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1450 return_VALUE(-ENODEV);
1451 }
1452
1453 return_VALUE(0);
1454}
1455
Len Brown4be44fc2005-08-05 00:44:28 -04001456static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 ACPI_FUNCTION_TRACE("acpi_thermal_exit");
1459
1460 acpi_bus_unregister_driver(&acpi_thermal_driver);
1461
1462 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1463
1464 return_VOID;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467module_init(acpi_thermal_init);
1468module_exit(acpi_thermal_exit);