]>
Commit | Line | Data |
---|---|---|
203d3d4a ZR |
1 | /* |
2 | * thermal.c - Generic Thermal Management Sysfs support. | |
3 | * | |
4 | * Copyright (C) 2008 Intel Corp | |
5 | * Copyright (C) 2008 Zhang Rui <[email protected]> | |
6 | * Copyright (C) 2008 Sujith Thomas <[email protected]> | |
7 | * | |
8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; version 2 of the License. | |
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 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/device.h> | |
28 | #include <linux/err.h> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
203d3d4a ZR |
30 | #include <linux/kdev_t.h> |
31 | #include <linux/idr.h> | |
32 | #include <linux/thermal.h> | |
33 | #include <linux/spinlock.h> | |
b1569e99 | 34 | #include <linux/reboot.h> |
4cb18728 D |
35 | #include <net/netlink.h> |
36 | #include <net/genetlink.h> | |
203d3d4a | 37 | |
63c4ec90 | 38 | MODULE_AUTHOR("Zhang Rui"); |
203d3d4a ZR |
39 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
40 | MODULE_LICENSE("GPL"); | |
41 | ||
42 | #define PREFIX "Thermal: " | |
43 | ||
44 | struct thermal_cooling_device_instance { | |
45 | int id; | |
46 | char name[THERMAL_NAME_LENGTH]; | |
47 | struct thermal_zone_device *tz; | |
48 | struct thermal_cooling_device *cdev; | |
49 | int trip; | |
50 | char attr_name[THERMAL_NAME_LENGTH]; | |
51 | struct device_attribute attr; | |
52 | struct list_head node; | |
53 | }; | |
54 | ||
55 | static DEFINE_IDR(thermal_tz_idr); | |
56 | static DEFINE_IDR(thermal_cdev_idr); | |
57 | static DEFINE_MUTEX(thermal_idr_lock); | |
58 | ||
59 | static LIST_HEAD(thermal_tz_list); | |
60 | static LIST_HEAD(thermal_cdev_list); | |
61 | static DEFINE_MUTEX(thermal_list_lock); | |
62 | ||
4cb18728 D |
63 | static unsigned int thermal_event_seqnum; |
64 | ||
203d3d4a ZR |
65 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
66 | { | |
67 | int err; | |
68 | ||
69 | again: | |
70 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) | |
71 | return -ENOMEM; | |
72 | ||
73 | if (lock) | |
74 | mutex_lock(lock); | |
75 | err = idr_get_new(idr, NULL, id); | |
76 | if (lock) | |
77 | mutex_unlock(lock); | |
78 | if (unlikely(err == -EAGAIN)) | |
79 | goto again; | |
80 | else if (unlikely(err)) | |
81 | return err; | |
82 | ||
83 | *id = *id & MAX_ID_MASK; | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static void release_idr(struct idr *idr, struct mutex *lock, int id) | |
88 | { | |
89 | if (lock) | |
90 | mutex_lock(lock); | |
91 | idr_remove(idr, id); | |
92 | if (lock) | |
93 | mutex_unlock(lock); | |
94 | } | |
95 | ||
96 | /* sys I/F for thermal zone */ | |
97 | ||
98 | #define to_thermal_zone(_dev) \ | |
99 | container_of(_dev, struct thermal_zone_device, device) | |
100 | ||
101 | static ssize_t | |
102 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
103 | { | |
104 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
105 | ||
106 | return sprintf(buf, "%s\n", tz->type); | |
107 | } | |
108 | ||
109 | static ssize_t | |
110 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
111 | { | |
112 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
113 | long temperature; |
114 | int ret; | |
203d3d4a ZR |
115 | |
116 | if (!tz->ops->get_temp) | |
117 | return -EPERM; | |
118 | ||
6503e5df MG |
119 | ret = tz->ops->get_temp(tz, &temperature); |
120 | ||
121 | if (ret) | |
122 | return ret; | |
123 | ||
124 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
125 | } |
126 | ||
127 | static ssize_t | |
128 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
129 | { | |
130 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
131 | enum thermal_device_mode mode; |
132 | int result; | |
203d3d4a ZR |
133 | |
134 | if (!tz->ops->get_mode) | |
135 | return -EPERM; | |
136 | ||
6503e5df MG |
137 | result = tz->ops->get_mode(tz, &mode); |
138 | if (result) | |
139 | return result; | |
140 | ||
141 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" | |
142 | : "disabled"); | |
203d3d4a ZR |
143 | } |
144 | ||
145 | static ssize_t | |
146 | mode_store(struct device *dev, struct device_attribute *attr, | |
147 | const char *buf, size_t count) | |
148 | { | |
149 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
150 | int result; | |
151 | ||
152 | if (!tz->ops->set_mode) | |
153 | return -EPERM; | |
154 | ||
6503e5df MG |
155 | if (!strncmp(buf, "enabled", sizeof("enabled"))) |
156 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); | |
157 | else if (!strncmp(buf, "disabled", sizeof("disabled"))) | |
158 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); | |
159 | else | |
160 | result = -EINVAL; | |
161 | ||
203d3d4a ZR |
162 | if (result) |
163 | return result; | |
164 | ||
165 | return count; | |
166 | } | |
167 | ||
168 | static ssize_t | |
169 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
170 | char *buf) | |
171 | { | |
172 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
173 | enum thermal_trip_type type; |
174 | int trip, result; | |
203d3d4a ZR |
175 | |
176 | if (!tz->ops->get_trip_type) | |
177 | return -EPERM; | |
178 | ||
179 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) | |
180 | return -EINVAL; | |
181 | ||
6503e5df MG |
182 | result = tz->ops->get_trip_type(tz, trip, &type); |
183 | if (result) | |
184 | return result; | |
185 | ||
186 | switch (type) { | |
187 | case THERMAL_TRIP_CRITICAL: | |
625120a4 | 188 | return sprintf(buf, "critical\n"); |
6503e5df | 189 | case THERMAL_TRIP_HOT: |
625120a4 | 190 | return sprintf(buf, "hot\n"); |
6503e5df | 191 | case THERMAL_TRIP_PASSIVE: |
625120a4 | 192 | return sprintf(buf, "passive\n"); |
6503e5df | 193 | case THERMAL_TRIP_ACTIVE: |
625120a4 | 194 | return sprintf(buf, "active\n"); |
6503e5df | 195 | default: |
625120a4 | 196 | return sprintf(buf, "unknown\n"); |
6503e5df | 197 | } |
203d3d4a ZR |
198 | } |
199 | ||
200 | static ssize_t | |
201 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
202 | char *buf) | |
203 | { | |
204 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
6503e5df MG |
205 | int trip, ret; |
206 | long temperature; | |
203d3d4a ZR |
207 | |
208 | if (!tz->ops->get_trip_temp) | |
209 | return -EPERM; | |
210 | ||
211 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) | |
212 | return -EINVAL; | |
213 | ||
6503e5df MG |
214 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
215 | ||
216 | if (ret) | |
217 | return ret; | |
218 | ||
219 | return sprintf(buf, "%ld\n", temperature); | |
203d3d4a ZR |
220 | } |
221 | ||
03a971a2 MG |
222 | static ssize_t |
223 | passive_store(struct device *dev, struct device_attribute *attr, | |
224 | const char *buf, size_t count) | |
225 | { | |
226 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
227 | struct thermal_cooling_device *cdev = NULL; | |
228 | int state; | |
229 | ||
230 | if (!sscanf(buf, "%d\n", &state)) | |
231 | return -EINVAL; | |
232 | ||
3d8e3ad8 FP |
233 | /* sanity check: values below 1000 millicelcius don't make sense |
234 | * and can cause the system to go into a thermal heart attack | |
235 | */ | |
236 | if (state && state < 1000) | |
237 | return -EINVAL; | |
238 | ||
03a971a2 MG |
239 | if (state && !tz->forced_passive) { |
240 | mutex_lock(&thermal_list_lock); | |
241 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
242 | if (!strncmp("Processor", cdev->type, | |
243 | sizeof("Processor"))) | |
244 | thermal_zone_bind_cooling_device(tz, | |
245 | THERMAL_TRIPS_NONE, | |
246 | cdev); | |
247 | } | |
248 | mutex_unlock(&thermal_list_lock); | |
e4143b03 FP |
249 | if (!tz->passive_delay) |
250 | tz->passive_delay = 1000; | |
03a971a2 MG |
251 | } else if (!state && tz->forced_passive) { |
252 | mutex_lock(&thermal_list_lock); | |
253 | list_for_each_entry(cdev, &thermal_cdev_list, node) { | |
254 | if (!strncmp("Processor", cdev->type, | |
255 | sizeof("Processor"))) | |
256 | thermal_zone_unbind_cooling_device(tz, | |
257 | THERMAL_TRIPS_NONE, | |
258 | cdev); | |
259 | } | |
260 | mutex_unlock(&thermal_list_lock); | |
e4143b03 | 261 | tz->passive_delay = 0; |
03a971a2 MG |
262 | } |
263 | ||
264 | tz->tc1 = 1; | |
265 | tz->tc2 = 1; | |
266 | ||
03a971a2 MG |
267 | tz->forced_passive = state; |
268 | ||
269 | thermal_zone_device_update(tz); | |
270 | ||
271 | return count; | |
272 | } | |
273 | ||
274 | static ssize_t | |
275 | passive_show(struct device *dev, struct device_attribute *attr, | |
276 | char *buf) | |
277 | { | |
278 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
279 | ||
280 | return sprintf(buf, "%d\n", tz->forced_passive); | |
281 | } | |
282 | ||
203d3d4a ZR |
283 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
284 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); | |
285 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); | |
03a971a2 MG |
286 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \ |
287 | passive_store); | |
203d3d4a ZR |
288 | |
289 | static struct device_attribute trip_point_attrs[] = { | |
290 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), | |
291 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), | |
292 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), | |
293 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), | |
294 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), | |
295 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), | |
296 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), | |
297 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), | |
298 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), | |
299 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), | |
300 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), | |
301 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), | |
302 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), | |
303 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), | |
304 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), | |
305 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), | |
306 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), | |
307 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), | |
308 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), | |
309 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), | |
5f1a3f2a KH |
310 | __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL), |
311 | __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL), | |
312 | __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL), | |
313 | __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL), | |
203d3d4a ZR |
314 | }; |
315 | ||
316 | #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \ | |
317 | do { \ | |
318 | result = device_create_file(_dev, \ | |
319 | &trip_point_attrs[_index * 2]); \ | |
320 | if (result) \ | |
321 | break; \ | |
322 | result = device_create_file(_dev, \ | |
323 | &trip_point_attrs[_index * 2 + 1]); \ | |
324 | } while (0) | |
325 | ||
326 | #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \ | |
327 | do { \ | |
328 | device_remove_file(_dev, &trip_point_attrs[_index * 2]); \ | |
329 | device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \ | |
330 | } while (0) | |
331 | ||
332 | /* sys I/F for cooling device */ | |
333 | #define to_cooling_device(_dev) \ | |
334 | container_of(_dev, struct thermal_cooling_device, device) | |
335 | ||
336 | static ssize_t | |
337 | thermal_cooling_device_type_show(struct device *dev, | |
338 | struct device_attribute *attr, char *buf) | |
339 | { | |
340 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
341 | ||
342 | return sprintf(buf, "%s\n", cdev->type); | |
343 | } | |
344 | ||
345 | static ssize_t | |
346 | thermal_cooling_device_max_state_show(struct device *dev, | |
347 | struct device_attribute *attr, char *buf) | |
348 | { | |
349 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
350 | unsigned long state; |
351 | int ret; | |
203d3d4a | 352 | |
6503e5df MG |
353 | ret = cdev->ops->get_max_state(cdev, &state); |
354 | if (ret) | |
355 | return ret; | |
356 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
357 | } |
358 | ||
359 | static ssize_t | |
360 | thermal_cooling_device_cur_state_show(struct device *dev, | |
361 | struct device_attribute *attr, char *buf) | |
362 | { | |
363 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df MG |
364 | unsigned long state; |
365 | int ret; | |
203d3d4a | 366 | |
6503e5df MG |
367 | ret = cdev->ops->get_cur_state(cdev, &state); |
368 | if (ret) | |
369 | return ret; | |
370 | return sprintf(buf, "%ld\n", state); | |
203d3d4a ZR |
371 | } |
372 | ||
373 | static ssize_t | |
374 | thermal_cooling_device_cur_state_store(struct device *dev, | |
375 | struct device_attribute *attr, | |
376 | const char *buf, size_t count) | |
377 | { | |
378 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
6503e5df | 379 | unsigned long state; |
203d3d4a ZR |
380 | int result; |
381 | ||
6503e5df | 382 | if (!sscanf(buf, "%ld\n", &state)) |
203d3d4a ZR |
383 | return -EINVAL; |
384 | ||
edb94918 | 385 | if ((long)state < 0) |
203d3d4a ZR |
386 | return -EINVAL; |
387 | ||
388 | result = cdev->ops->set_cur_state(cdev, state); | |
389 | if (result) | |
390 | return result; | |
391 | return count; | |
392 | } | |
393 | ||
394 | static struct device_attribute dev_attr_cdev_type = | |
543a9561 | 395 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
203d3d4a ZR |
396 | static DEVICE_ATTR(max_state, 0444, |
397 | thermal_cooling_device_max_state_show, NULL); | |
398 | static DEVICE_ATTR(cur_state, 0644, | |
399 | thermal_cooling_device_cur_state_show, | |
400 | thermal_cooling_device_cur_state_store); | |
401 | ||
402 | static ssize_t | |
403 | thermal_cooling_device_trip_point_show(struct device *dev, | |
543a9561 | 404 | struct device_attribute *attr, char *buf) |
203d3d4a ZR |
405 | { |
406 | struct thermal_cooling_device_instance *instance; | |
407 | ||
408 | instance = | |
409 | container_of(attr, struct thermal_cooling_device_instance, attr); | |
410 | ||
411 | if (instance->trip == THERMAL_TRIPS_NONE) | |
412 | return sprintf(buf, "-1\n"); | |
413 | else | |
414 | return sprintf(buf, "%d\n", instance->trip); | |
415 | } | |
416 | ||
417 | /* Device management */ | |
418 | ||
16d75239 RH |
419 | #if defined(CONFIG_THERMAL_HWMON) |
420 | ||
e68b16ab ZR |
421 | /* hwmon sys I/F */ |
422 | #include <linux/hwmon.h> | |
423 | static LIST_HEAD(thermal_hwmon_list); | |
424 | ||
425 | static ssize_t | |
426 | name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
427 | { | |
0e968a3b | 428 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
e68b16ab ZR |
429 | return sprintf(buf, "%s\n", hwmon->type); |
430 | } | |
431 | static DEVICE_ATTR(name, 0444, name_show, NULL); | |
432 | ||
433 | static ssize_t | |
434 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) | |
435 | { | |
6503e5df MG |
436 | long temperature; |
437 | int ret; | |
e68b16ab ZR |
438 | struct thermal_hwmon_attr *hwmon_attr |
439 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
440 | struct thermal_zone_device *tz | |
441 | = container_of(hwmon_attr, struct thermal_zone_device, | |
442 | temp_input); | |
443 | ||
6503e5df MG |
444 | ret = tz->ops->get_temp(tz, &temperature); |
445 | ||
446 | if (ret) | |
447 | return ret; | |
448 | ||
449 | return sprintf(buf, "%ld\n", temperature); | |
e68b16ab ZR |
450 | } |
451 | ||
452 | static ssize_t | |
453 | temp_crit_show(struct device *dev, struct device_attribute *attr, | |
454 | char *buf) | |
455 | { | |
456 | struct thermal_hwmon_attr *hwmon_attr | |
457 | = container_of(attr, struct thermal_hwmon_attr, attr); | |
458 | struct thermal_zone_device *tz | |
459 | = container_of(hwmon_attr, struct thermal_zone_device, | |
460 | temp_crit); | |
6503e5df MG |
461 | long temperature; |
462 | int ret; | |
463 | ||
464 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); | |
465 | if (ret) | |
466 | return ret; | |
e68b16ab | 467 | |
6503e5df | 468 | return sprintf(buf, "%ld\n", temperature); |
e68b16ab ZR |
469 | } |
470 | ||
471 | ||
472 | static int | |
473 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
474 | { | |
475 | struct thermal_hwmon_device *hwmon; | |
476 | int new_hwmon_device = 1; | |
477 | int result; | |
478 | ||
479 | mutex_lock(&thermal_list_lock); | |
480 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) | |
481 | if (!strcmp(hwmon->type, tz->type)) { | |
482 | new_hwmon_device = 0; | |
483 | mutex_unlock(&thermal_list_lock); | |
484 | goto register_sys_interface; | |
485 | } | |
486 | mutex_unlock(&thermal_list_lock); | |
487 | ||
488 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); | |
489 | if (!hwmon) | |
490 | return -ENOMEM; | |
491 | ||
492 | INIT_LIST_HEAD(&hwmon->tz_list); | |
493 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); | |
494 | hwmon->device = hwmon_device_register(NULL); | |
495 | if (IS_ERR(hwmon->device)) { | |
496 | result = PTR_ERR(hwmon->device); | |
497 | goto free_mem; | |
498 | } | |
0e968a3b | 499 | dev_set_drvdata(hwmon->device, hwmon); |
e68b16ab ZR |
500 | result = device_create_file(hwmon->device, &dev_attr_name); |
501 | if (result) | |
502 | goto unregister_hwmon_device; | |
503 | ||
504 | register_sys_interface: | |
505 | tz->hwmon = hwmon; | |
506 | hwmon->count++; | |
507 | ||
508 | snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH, | |
509 | "temp%d_input", hwmon->count); | |
510 | tz->temp_input.attr.attr.name = tz->temp_input.name; | |
511 | tz->temp_input.attr.attr.mode = 0444; | |
512 | tz->temp_input.attr.show = temp_input_show; | |
975f8c56 | 513 | sysfs_attr_init(&tz->temp_input.attr.attr); |
e68b16ab ZR |
514 | result = device_create_file(hwmon->device, &tz->temp_input.attr); |
515 | if (result) | |
516 | goto unregister_hwmon_device; | |
517 | ||
518 | if (tz->ops->get_crit_temp) { | |
519 | unsigned long temperature; | |
520 | if (!tz->ops->get_crit_temp(tz, &temperature)) { | |
521 | snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH, | |
522 | "temp%d_crit", hwmon->count); | |
523 | tz->temp_crit.attr.attr.name = tz->temp_crit.name; | |
524 | tz->temp_crit.attr.attr.mode = 0444; | |
525 | tz->temp_crit.attr.show = temp_crit_show; | |
975f8c56 | 526 | sysfs_attr_init(&tz->temp_crit.attr.attr); |
e68b16ab ZR |
527 | result = device_create_file(hwmon->device, |
528 | &tz->temp_crit.attr); | |
529 | if (result) | |
530 | goto unregister_hwmon_device; | |
531 | } | |
532 | } | |
533 | ||
534 | mutex_lock(&thermal_list_lock); | |
535 | if (new_hwmon_device) | |
536 | list_add_tail(&hwmon->node, &thermal_hwmon_list); | |
537 | list_add_tail(&tz->hwmon_node, &hwmon->tz_list); | |
538 | mutex_unlock(&thermal_list_lock); | |
539 | ||
540 | return 0; | |
541 | ||
542 | unregister_hwmon_device: | |
543 | device_remove_file(hwmon->device, &tz->temp_crit.attr); | |
544 | device_remove_file(hwmon->device, &tz->temp_input.attr); | |
545 | if (new_hwmon_device) { | |
546 | device_remove_file(hwmon->device, &dev_attr_name); | |
547 | hwmon_device_unregister(hwmon->device); | |
548 | } | |
549 | free_mem: | |
550 | if (new_hwmon_device) | |
551 | kfree(hwmon); | |
552 | ||
553 | return result; | |
554 | } | |
555 | ||
556 | static void | |
557 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
558 | { | |
559 | struct thermal_hwmon_device *hwmon = tz->hwmon; | |
560 | ||
561 | tz->hwmon = NULL; | |
562 | device_remove_file(hwmon->device, &tz->temp_input.attr); | |
4fdfbe85 D |
563 | if (tz->ops->get_crit_temp) |
564 | device_remove_file(hwmon->device, &tz->temp_crit.attr); | |
e68b16ab ZR |
565 | |
566 | mutex_lock(&thermal_list_lock); | |
567 | list_del(&tz->hwmon_node); | |
568 | if (!list_empty(&hwmon->tz_list)) { | |
569 | mutex_unlock(&thermal_list_lock); | |
570 | return; | |
571 | } | |
572 | list_del(&hwmon->node); | |
573 | mutex_unlock(&thermal_list_lock); | |
574 | ||
575 | device_remove_file(hwmon->device, &dev_attr_name); | |
576 | hwmon_device_unregister(hwmon->device); | |
577 | kfree(hwmon); | |
578 | } | |
579 | #else | |
580 | static int | |
581 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) | |
582 | { | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static void | |
587 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) | |
588 | { | |
589 | } | |
590 | #endif | |
591 | ||
b1569e99 MG |
592 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
593 | int delay) | |
594 | { | |
595 | cancel_delayed_work(&(tz->poll_queue)); | |
596 | ||
597 | if (!delay) | |
598 | return; | |
599 | ||
600 | if (delay > 1000) | |
601 | schedule_delayed_work(&(tz->poll_queue), | |
602 | round_jiffies(msecs_to_jiffies(delay))); | |
603 | else | |
604 | schedule_delayed_work(&(tz->poll_queue), | |
605 | msecs_to_jiffies(delay)); | |
606 | } | |
607 | ||
608 | static void thermal_zone_device_passive(struct thermal_zone_device *tz, | |
609 | int temp, int trip_temp, int trip) | |
610 | { | |
611 | int trend = 0; | |
612 | struct thermal_cooling_device_instance *instance; | |
613 | struct thermal_cooling_device *cdev; | |
614 | long state, max_state; | |
615 | ||
616 | /* | |
617 | * Above Trip? | |
618 | * ----------- | |
619 | * Calculate the thermal trend (using the passive cooling equation) | |
620 | * and modify the performance limit for all passive cooling devices | |
621 | * accordingly. Note that we assume symmetry. | |
622 | */ | |
623 | if (temp >= trip_temp) { | |
624 | tz->passive = true; | |
625 | ||
626 | trend = (tz->tc1 * (temp - tz->last_temperature)) + | |
627 | (tz->tc2 * (temp - trip_temp)); | |
628 | ||
629 | /* Heating up? */ | |
630 | if (trend > 0) { | |
631 | list_for_each_entry(instance, &tz->cooling_devices, | |
632 | node) { | |
633 | if (instance->trip != trip) | |
634 | continue; | |
635 | cdev = instance->cdev; | |
636 | cdev->ops->get_cur_state(cdev, &state); | |
637 | cdev->ops->get_max_state(cdev, &max_state); | |
638 | if (state++ < max_state) | |
639 | cdev->ops->set_cur_state(cdev, state); | |
640 | } | |
641 | } else if (trend < 0) { /* Cooling off? */ | |
642 | list_for_each_entry(instance, &tz->cooling_devices, | |
643 | node) { | |
644 | if (instance->trip != trip) | |
645 | continue; | |
646 | cdev = instance->cdev; | |
647 | cdev->ops->get_cur_state(cdev, &state); | |
648 | cdev->ops->get_max_state(cdev, &max_state); | |
649 | if (state > 0) | |
650 | cdev->ops->set_cur_state(cdev, --state); | |
651 | } | |
652 | } | |
653 | return; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Below Trip? | |
658 | * ----------- | |
659 | * Implement passive cooling hysteresis to slowly increase performance | |
660 | * and avoid thrashing around the passive trip point. Note that we | |
661 | * assume symmetry. | |
662 | */ | |
663 | list_for_each_entry(instance, &tz->cooling_devices, node) { | |
664 | if (instance->trip != trip) | |
665 | continue; | |
666 | cdev = instance->cdev; | |
667 | cdev->ops->get_cur_state(cdev, &state); | |
668 | cdev->ops->get_max_state(cdev, &max_state); | |
669 | if (state > 0) | |
670 | cdev->ops->set_cur_state(cdev, --state); | |
671 | if (state == 0) | |
672 | tz->passive = false; | |
673 | } | |
674 | } | |
675 | ||
676 | static void thermal_zone_device_check(struct work_struct *work) | |
677 | { | |
678 | struct thermal_zone_device *tz = container_of(work, struct | |
679 | thermal_zone_device, | |
680 | poll_queue.work); | |
681 | thermal_zone_device_update(tz); | |
682 | } | |
e68b16ab | 683 | |
203d3d4a ZR |
684 | /** |
685 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone | |
203d3d4a ZR |
686 | * @tz: thermal zone device |
687 | * @trip: indicates which trip point the cooling devices is | |
688 | * associated with in this thermal zone. | |
689 | * @cdev: thermal cooling device | |
543a9561 LB |
690 | * |
691 | * This function is usually called in the thermal zone device .bind callback. | |
203d3d4a ZR |
692 | */ |
693 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, | |
694 | int trip, | |
695 | struct thermal_cooling_device *cdev) | |
696 | { | |
697 | struct thermal_cooling_device_instance *dev; | |
698 | struct thermal_cooling_device_instance *pos; | |
c7516709 TS |
699 | struct thermal_zone_device *pos1; |
700 | struct thermal_cooling_device *pos2; | |
203d3d4a ZR |
701 | int result; |
702 | ||
543a9561 | 703 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
203d3d4a ZR |
704 | return -EINVAL; |
705 | ||
c7516709 TS |
706 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
707 | if (pos1 == tz) | |
708 | break; | |
709 | } | |
710 | list_for_each_entry(pos2, &thermal_cdev_list, node) { | |
711 | if (pos2 == cdev) | |
712 | break; | |
713 | } | |
714 | ||
715 | if (tz != pos1 || cdev != pos2) | |
203d3d4a ZR |
716 | return -EINVAL; |
717 | ||
718 | dev = | |
719 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); | |
720 | if (!dev) | |
721 | return -ENOMEM; | |
722 | dev->tz = tz; | |
723 | dev->cdev = cdev; | |
724 | dev->trip = trip; | |
725 | result = get_idr(&tz->idr, &tz->lock, &dev->id); | |
726 | if (result) | |
727 | goto free_mem; | |
728 | ||
729 | sprintf(dev->name, "cdev%d", dev->id); | |
730 | result = | |
731 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); | |
732 | if (result) | |
733 | goto release_idr; | |
734 | ||
735 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); | |
975f8c56 | 736 | sysfs_attr_init(&dev->attr.attr); |
203d3d4a ZR |
737 | dev->attr.attr.name = dev->attr_name; |
738 | dev->attr.attr.mode = 0444; | |
739 | dev->attr.show = thermal_cooling_device_trip_point_show; | |
740 | result = device_create_file(&tz->device, &dev->attr); | |
741 | if (result) | |
742 | goto remove_symbol_link; | |
743 | ||
744 | mutex_lock(&tz->lock); | |
745 | list_for_each_entry(pos, &tz->cooling_devices, node) | |
746 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { | |
747 | result = -EEXIST; | |
748 | break; | |
749 | } | |
750 | if (!result) | |
751 | list_add_tail(&dev->node, &tz->cooling_devices); | |
752 | mutex_unlock(&tz->lock); | |
753 | ||
754 | if (!result) | |
755 | return 0; | |
756 | ||
757 | device_remove_file(&tz->device, &dev->attr); | |
758 | remove_symbol_link: | |
759 | sysfs_remove_link(&tz->device.kobj, dev->name); | |
760 | release_idr: | |
761 | release_idr(&tz->idr, &tz->lock, dev->id); | |
762 | free_mem: | |
763 | kfree(dev); | |
764 | return result; | |
765 | } | |
543a9561 | 766 | |
203d3d4a ZR |
767 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
768 | ||
769 | /** | |
770 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone | |
203d3d4a ZR |
771 | * @tz: thermal zone device |
772 | * @trip: indicates which trip point the cooling devices is | |
773 | * associated with in this thermal zone. | |
774 | * @cdev: thermal cooling device | |
543a9561 LB |
775 | * |
776 | * This function is usually called in the thermal zone device .unbind callback. | |
203d3d4a ZR |
777 | */ |
778 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, | |
779 | int trip, | |
780 | struct thermal_cooling_device *cdev) | |
781 | { | |
782 | struct thermal_cooling_device_instance *pos, *next; | |
783 | ||
784 | mutex_lock(&tz->lock); | |
785 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { | |
543a9561 | 786 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
203d3d4a ZR |
787 | list_del(&pos->node); |
788 | mutex_unlock(&tz->lock); | |
789 | goto unbind; | |
790 | } | |
791 | } | |
792 | mutex_unlock(&tz->lock); | |
793 | ||
794 | return -ENODEV; | |
795 | ||
796 | unbind: | |
797 | device_remove_file(&tz->device, &pos->attr); | |
798 | sysfs_remove_link(&tz->device.kobj, pos->name); | |
799 | release_idr(&tz->idr, &tz->lock, pos->id); | |
800 | kfree(pos); | |
801 | return 0; | |
802 | } | |
543a9561 | 803 | |
203d3d4a ZR |
804 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
805 | ||
806 | static void thermal_release(struct device *dev) | |
807 | { | |
808 | struct thermal_zone_device *tz; | |
809 | struct thermal_cooling_device *cdev; | |
810 | ||
354655ea | 811 | if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) { |
203d3d4a ZR |
812 | tz = to_thermal_zone(dev); |
813 | kfree(tz); | |
814 | } else { | |
815 | cdev = to_cooling_device(dev); | |
816 | kfree(cdev); | |
817 | } | |
818 | } | |
819 | ||
820 | static struct class thermal_class = { | |
821 | .name = "thermal", | |
822 | .dev_release = thermal_release, | |
823 | }; | |
824 | ||
825 | /** | |
826 | * thermal_cooling_device_register - register a new thermal cooling device | |
827 | * @type: the thermal cooling device type. | |
828 | * @devdata: device private data. | |
829 | * @ops: standard thermal cooling devices callbacks. | |
830 | */ | |
5b275ce2 AC |
831 | struct thermal_cooling_device *thermal_cooling_device_register( |
832 | char *type, void *devdata, const struct thermal_cooling_device_ops *ops) | |
203d3d4a ZR |
833 | { |
834 | struct thermal_cooling_device *cdev; | |
835 | struct thermal_zone_device *pos; | |
836 | int result; | |
837 | ||
838 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 839 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
840 | |
841 | if (!ops || !ops->get_max_state || !ops->get_cur_state || | |
543a9561 | 842 | !ops->set_cur_state) |
3e6fda5c | 843 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
844 | |
845 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); | |
846 | if (!cdev) | |
3e6fda5c | 847 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
848 | |
849 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); | |
850 | if (result) { | |
851 | kfree(cdev); | |
3e6fda5c | 852 | return ERR_PTR(result); |
203d3d4a ZR |
853 | } |
854 | ||
855 | strcpy(cdev->type, type); | |
856 | cdev->ops = ops; | |
857 | cdev->device.class = &thermal_class; | |
858 | cdev->devdata = devdata; | |
354655ea | 859 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
203d3d4a ZR |
860 | result = device_register(&cdev->device); |
861 | if (result) { | |
862 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
863 | kfree(cdev); | |
3e6fda5c | 864 | return ERR_PTR(result); |
203d3d4a ZR |
865 | } |
866 | ||
867 | /* sys I/F */ | |
868 | if (type) { | |
543a9561 | 869 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
870 | if (result) |
871 | goto unregister; | |
872 | } | |
873 | ||
874 | result = device_create_file(&cdev->device, &dev_attr_max_state); | |
875 | if (result) | |
876 | goto unregister; | |
877 | ||
878 | result = device_create_file(&cdev->device, &dev_attr_cur_state); | |
879 | if (result) | |
880 | goto unregister; | |
881 | ||
882 | mutex_lock(&thermal_list_lock); | |
883 | list_add(&cdev->node, &thermal_cdev_list); | |
884 | list_for_each_entry(pos, &thermal_tz_list, node) { | |
885 | if (!pos->ops->bind) | |
886 | continue; | |
887 | result = pos->ops->bind(pos, cdev); | |
888 | if (result) | |
889 | break; | |
890 | ||
891 | } | |
892 | mutex_unlock(&thermal_list_lock); | |
893 | ||
894 | if (!result) | |
895 | return cdev; | |
896 | ||
897 | unregister: | |
898 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
899 | device_unregister(&cdev->device); | |
3e6fda5c | 900 | return ERR_PTR(result); |
203d3d4a | 901 | } |
543a9561 | 902 | |
203d3d4a ZR |
903 | EXPORT_SYMBOL(thermal_cooling_device_register); |
904 | ||
905 | /** | |
906 | * thermal_cooling_device_unregister - removes the registered thermal cooling device | |
203d3d4a ZR |
907 | * @cdev: the thermal cooling device to remove. |
908 | * | |
909 | * thermal_cooling_device_unregister() must be called when the device is no | |
910 | * longer needed. | |
911 | */ | |
912 | void thermal_cooling_device_unregister(struct | |
913 | thermal_cooling_device | |
914 | *cdev) | |
915 | { | |
916 | struct thermal_zone_device *tz; | |
917 | struct thermal_cooling_device *pos = NULL; | |
918 | ||
919 | if (!cdev) | |
920 | return; | |
921 | ||
922 | mutex_lock(&thermal_list_lock); | |
923 | list_for_each_entry(pos, &thermal_cdev_list, node) | |
924 | if (pos == cdev) | |
925 | break; | |
926 | if (pos != cdev) { | |
927 | /* thermal cooling device not found */ | |
928 | mutex_unlock(&thermal_list_lock); | |
929 | return; | |
930 | } | |
931 | list_del(&cdev->node); | |
932 | list_for_each_entry(tz, &thermal_tz_list, node) { | |
933 | if (!tz->ops->unbind) | |
934 | continue; | |
935 | tz->ops->unbind(tz, cdev); | |
936 | } | |
937 | mutex_unlock(&thermal_list_lock); | |
938 | if (cdev->type[0]) | |
543a9561 | 939 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
203d3d4a ZR |
940 | device_remove_file(&cdev->device, &dev_attr_max_state); |
941 | device_remove_file(&cdev->device, &dev_attr_cur_state); | |
942 | ||
943 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); | |
944 | device_unregister(&cdev->device); | |
945 | return; | |
946 | } | |
543a9561 | 947 | |
203d3d4a ZR |
948 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
949 | ||
b1569e99 MG |
950 | /** |
951 | * thermal_zone_device_update - force an update of a thermal zone's state | |
952 | * @ttz: the thermal zone to update | |
953 | */ | |
954 | ||
955 | void thermal_zone_device_update(struct thermal_zone_device *tz) | |
956 | { | |
957 | int count, ret = 0; | |
958 | long temp, trip_temp; | |
959 | enum thermal_trip_type trip_type; | |
960 | struct thermal_cooling_device_instance *instance; | |
961 | struct thermal_cooling_device *cdev; | |
962 | ||
963 | mutex_lock(&tz->lock); | |
964 | ||
0d288162 MB |
965 | if (tz->ops->get_temp(tz, &temp)) { |
966 | /* get_temp failed - retry it later */ | |
967 | printk(KERN_WARNING PREFIX "failed to read out thermal zone " | |
968 | "%d\n", tz->id); | |
969 | goto leave; | |
970 | } | |
b1569e99 MG |
971 | |
972 | for (count = 0; count < tz->trips; count++) { | |
973 | tz->ops->get_trip_type(tz, count, &trip_type); | |
974 | tz->ops->get_trip_temp(tz, count, &trip_temp); | |
975 | ||
976 | switch (trip_type) { | |
977 | case THERMAL_TRIP_CRITICAL: | |
29321357 | 978 | if (temp >= trip_temp) { |
b1569e99 MG |
979 | if (tz->ops->notify) |
980 | ret = tz->ops->notify(tz, count, | |
981 | trip_type); | |
982 | if (!ret) { | |
983 | printk(KERN_EMERG | |
984 | "Critical temperature reached (%ld C), shutting down.\n", | |
985 | temp/1000); | |
986 | orderly_poweroff(true); | |
987 | } | |
988 | } | |
989 | break; | |
990 | case THERMAL_TRIP_HOT: | |
29321357 | 991 | if (temp >= trip_temp) |
b1569e99 MG |
992 | if (tz->ops->notify) |
993 | tz->ops->notify(tz, count, trip_type); | |
994 | break; | |
995 | case THERMAL_TRIP_ACTIVE: | |
996 | list_for_each_entry(instance, &tz->cooling_devices, | |
997 | node) { | |
998 | if (instance->trip != count) | |
999 | continue; | |
1000 | ||
1001 | cdev = instance->cdev; | |
1002 | ||
29321357 | 1003 | if (temp >= trip_temp) |
b1569e99 MG |
1004 | cdev->ops->set_cur_state(cdev, 1); |
1005 | else | |
1006 | cdev->ops->set_cur_state(cdev, 0); | |
1007 | } | |
1008 | break; | |
1009 | case THERMAL_TRIP_PASSIVE: | |
29321357 | 1010 | if (temp >= trip_temp || tz->passive) |
b1569e99 MG |
1011 | thermal_zone_device_passive(tz, temp, |
1012 | trip_temp, count); | |
1013 | break; | |
1014 | } | |
1015 | } | |
03a971a2 MG |
1016 | |
1017 | if (tz->forced_passive) | |
1018 | thermal_zone_device_passive(tz, temp, tz->forced_passive, | |
1019 | THERMAL_TRIPS_NONE); | |
1020 | ||
b1569e99 | 1021 | tz->last_temperature = temp; |
0d288162 MB |
1022 | |
1023 | leave: | |
b1569e99 MG |
1024 | if (tz->passive) |
1025 | thermal_zone_device_set_polling(tz, tz->passive_delay); | |
1026 | else if (tz->polling_delay) | |
1027 | thermal_zone_device_set_polling(tz, tz->polling_delay); | |
3767cb54 FP |
1028 | else |
1029 | thermal_zone_device_set_polling(tz, 0); | |
b1569e99 MG |
1030 | mutex_unlock(&tz->lock); |
1031 | } | |
1032 | EXPORT_SYMBOL(thermal_zone_device_update); | |
1033 | ||
203d3d4a ZR |
1034 | /** |
1035 | * thermal_zone_device_register - register a new thermal zone device | |
1036 | * @type: the thermal zone device type | |
1037 | * @trips: the number of trip points the thermal zone support | |
1038 | * @devdata: private device data | |
1039 | * @ops: standard thermal zone device callbacks | |
b1569e99 MG |
1040 | * @tc1: thermal coefficient 1 for passive calculations |
1041 | * @tc2: thermal coefficient 2 for passive calculations | |
1042 | * @passive_delay: number of milliseconds to wait between polls when | |
1043 | * performing passive cooling | |
1044 | * @polling_delay: number of milliseconds to wait between polls when checking | |
1045 | * whether trip points have been crossed (0 for interrupt | |
1046 | * driven systems) | |
203d3d4a ZR |
1047 | * |
1048 | * thermal_zone_device_unregister() must be called when the device is no | |
b1569e99 MG |
1049 | * longer needed. The passive cooling formula uses tc1 and tc2 as described in |
1050 | * section 11.1.5.1 of the ACPI specification 3.0. | |
203d3d4a ZR |
1051 | */ |
1052 | struct thermal_zone_device *thermal_zone_device_register(char *type, | |
5b275ce2 AC |
1053 | int trips, void *devdata, |
1054 | const struct thermal_zone_device_ops *ops, | |
1055 | int tc1, int tc2, int passive_delay, int polling_delay) | |
203d3d4a ZR |
1056 | { |
1057 | struct thermal_zone_device *tz; | |
1058 | struct thermal_cooling_device *pos; | |
03a971a2 | 1059 | enum thermal_trip_type trip_type; |
203d3d4a ZR |
1060 | int result; |
1061 | int count; | |
03a971a2 | 1062 | int passive = 0; |
203d3d4a ZR |
1063 | |
1064 | if (strlen(type) >= THERMAL_NAME_LENGTH) | |
3e6fda5c | 1065 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1066 | |
1067 | if (trips > THERMAL_MAX_TRIPS || trips < 0) | |
3e6fda5c | 1068 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1069 | |
1070 | if (!ops || !ops->get_temp) | |
3e6fda5c | 1071 | return ERR_PTR(-EINVAL); |
203d3d4a ZR |
1072 | |
1073 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); | |
1074 | if (!tz) | |
3e6fda5c | 1075 | return ERR_PTR(-ENOMEM); |
203d3d4a ZR |
1076 | |
1077 | INIT_LIST_HEAD(&tz->cooling_devices); | |
1078 | idr_init(&tz->idr); | |
1079 | mutex_init(&tz->lock); | |
1080 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); | |
1081 | if (result) { | |
1082 | kfree(tz); | |
3e6fda5c | 1083 | return ERR_PTR(result); |
203d3d4a ZR |
1084 | } |
1085 | ||
1086 | strcpy(tz->type, type); | |
1087 | tz->ops = ops; | |
1088 | tz->device.class = &thermal_class; | |
1089 | tz->devdata = devdata; | |
1090 | tz->trips = trips; | |
b1569e99 MG |
1091 | tz->tc1 = tc1; |
1092 | tz->tc2 = tc2; | |
1093 | tz->passive_delay = passive_delay; | |
1094 | tz->polling_delay = polling_delay; | |
1095 | ||
354655ea | 1096 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
203d3d4a ZR |
1097 | result = device_register(&tz->device); |
1098 | if (result) { | |
1099 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1100 | kfree(tz); | |
3e6fda5c | 1101 | return ERR_PTR(result); |
203d3d4a ZR |
1102 | } |
1103 | ||
1104 | /* sys I/F */ | |
1105 | if (type) { | |
1106 | result = device_create_file(&tz->device, &dev_attr_type); | |
1107 | if (result) | |
1108 | goto unregister; | |
1109 | } | |
1110 | ||
1111 | result = device_create_file(&tz->device, &dev_attr_temp); | |
1112 | if (result) | |
1113 | goto unregister; | |
1114 | ||
1115 | if (ops->get_mode) { | |
1116 | result = device_create_file(&tz->device, &dev_attr_mode); | |
1117 | if (result) | |
1118 | goto unregister; | |
1119 | } | |
1120 | ||
1121 | for (count = 0; count < trips; count++) { | |
1122 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); | |
1123 | if (result) | |
1124 | goto unregister; | |
03a971a2 MG |
1125 | tz->ops->get_trip_type(tz, count, &trip_type); |
1126 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
1127 | passive = 1; | |
203d3d4a ZR |
1128 | } |
1129 | ||
03a971a2 MG |
1130 | if (!passive) |
1131 | result = device_create_file(&tz->device, | |
1132 | &dev_attr_passive); | |
1133 | ||
1134 | if (result) | |
1135 | goto unregister; | |
1136 | ||
e68b16ab ZR |
1137 | result = thermal_add_hwmon_sysfs(tz); |
1138 | if (result) | |
1139 | goto unregister; | |
1140 | ||
203d3d4a ZR |
1141 | mutex_lock(&thermal_list_lock); |
1142 | list_add_tail(&tz->node, &thermal_tz_list); | |
1143 | if (ops->bind) | |
1144 | list_for_each_entry(pos, &thermal_cdev_list, node) { | |
543a9561 LB |
1145 | result = ops->bind(tz, pos); |
1146 | if (result) | |
1147 | break; | |
203d3d4a ZR |
1148 | } |
1149 | mutex_unlock(&thermal_list_lock); | |
1150 | ||
b1569e99 MG |
1151 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
1152 | ||
1153 | thermal_zone_device_update(tz); | |
1154 | ||
203d3d4a ZR |
1155 | if (!result) |
1156 | return tz; | |
1157 | ||
1158 | unregister: | |
1159 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); | |
1160 | device_unregister(&tz->device); | |
3e6fda5c | 1161 | return ERR_PTR(result); |
203d3d4a | 1162 | } |
543a9561 | 1163 | |
203d3d4a ZR |
1164 | EXPORT_SYMBOL(thermal_zone_device_register); |
1165 | ||
1166 | /** | |
1167 | * thermal_device_unregister - removes the registered thermal zone device | |
203d3d4a ZR |
1168 | * @tz: the thermal zone device to remove |
1169 | */ | |
1170 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) | |
1171 | { | |
1172 | struct thermal_cooling_device *cdev; | |
1173 | struct thermal_zone_device *pos = NULL; | |
1174 | int count; | |
1175 | ||
1176 | if (!tz) | |
1177 | return; | |
1178 | ||
1179 | mutex_lock(&thermal_list_lock); | |
1180 | list_for_each_entry(pos, &thermal_tz_list, node) | |
1181 | if (pos == tz) | |
1182 | break; | |
1183 | if (pos != tz) { | |
1184 | /* thermal zone device not found */ | |
1185 | mutex_unlock(&thermal_list_lock); | |
1186 | return; | |
1187 | } | |
1188 | list_del(&tz->node); | |
1189 | if (tz->ops->unbind) | |
1190 | list_for_each_entry(cdev, &thermal_cdev_list, node) | |
1191 | tz->ops->unbind(tz, cdev); | |
1192 | mutex_unlock(&thermal_list_lock); | |
1193 | ||
b1569e99 MG |
1194 | thermal_zone_device_set_polling(tz, 0); |
1195 | ||
203d3d4a ZR |
1196 | if (tz->type[0]) |
1197 | device_remove_file(&tz->device, &dev_attr_type); | |
1198 | device_remove_file(&tz->device, &dev_attr_temp); | |
1199 | if (tz->ops->get_mode) | |
1200 | device_remove_file(&tz->device, &dev_attr_mode); | |
1201 | ||
1202 | for (count = 0; count < tz->trips; count++) | |
1203 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); | |
1204 | ||
e68b16ab | 1205 | thermal_remove_hwmon_sysfs(tz); |
203d3d4a ZR |
1206 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
1207 | idr_destroy(&tz->idr); | |
1208 | mutex_destroy(&tz->lock); | |
1209 | device_unregister(&tz->device); | |
1210 | return; | |
1211 | } | |
543a9561 | 1212 | |
203d3d4a ZR |
1213 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
1214 | ||
af06216a RW |
1215 | #ifdef CONFIG_NET |
1216 | static struct genl_family thermal_event_genl_family = { | |
1217 | .id = GENL_ID_GENERATE, | |
1218 | .name = THERMAL_GENL_FAMILY_NAME, | |
1219 | .version = THERMAL_GENL_VERSION, | |
1220 | .maxattr = THERMAL_GENL_ATTR_MAX, | |
1221 | }; | |
1222 | ||
1223 | static struct genl_multicast_group thermal_event_mcgrp = { | |
1224 | .name = THERMAL_GENL_MCAST_GROUP_NAME, | |
1225 | }; | |
1226 | ||
4cb18728 D |
1227 | int generate_netlink_event(u32 orig, enum events event) |
1228 | { | |
1229 | struct sk_buff *skb; | |
1230 | struct nlattr *attr; | |
1231 | struct thermal_genl_event *thermal_event; | |
1232 | void *msg_header; | |
1233 | int size; | |
1234 | int result; | |
1235 | ||
1236 | /* allocate memory */ | |
1237 | size = nla_total_size(sizeof(struct thermal_genl_event)) + \ | |
1238 | nla_total_size(0); | |
1239 | ||
1240 | skb = genlmsg_new(size, GFP_ATOMIC); | |
1241 | if (!skb) | |
1242 | return -ENOMEM; | |
1243 | ||
1244 | /* add the genetlink message header */ | |
1245 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, | |
1246 | &thermal_event_genl_family, 0, | |
1247 | THERMAL_GENL_CMD_EVENT); | |
1248 | if (!msg_header) { | |
1249 | nlmsg_free(skb); | |
1250 | return -ENOMEM; | |
1251 | } | |
1252 | ||
1253 | /* fill the data */ | |
1254 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \ | |
1255 | sizeof(struct thermal_genl_event)); | |
1256 | ||
1257 | if (!attr) { | |
1258 | nlmsg_free(skb); | |
1259 | return -EINVAL; | |
1260 | } | |
1261 | ||
1262 | thermal_event = nla_data(attr); | |
1263 | if (!thermal_event) { | |
1264 | nlmsg_free(skb); | |
1265 | return -EINVAL; | |
1266 | } | |
1267 | ||
1268 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); | |
1269 | ||
1270 | thermal_event->orig = orig; | |
1271 | thermal_event->event = event; | |
1272 | ||
1273 | /* send multicast genetlink message */ | |
1274 | result = genlmsg_end(skb, msg_header); | |
1275 | if (result < 0) { | |
1276 | nlmsg_free(skb); | |
1277 | return result; | |
1278 | } | |
1279 | ||
1280 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); | |
1281 | if (result) | |
1282 | printk(KERN_INFO "failed to send netlink event:%d", result); | |
1283 | ||
1284 | return result; | |
1285 | } | |
1286 | EXPORT_SYMBOL(generate_netlink_event); | |
1287 | ||
1288 | static int genetlink_init(void) | |
1289 | { | |
1290 | int result; | |
1291 | ||
1292 | result = genl_register_family(&thermal_event_genl_family); | |
1293 | if (result) | |
1294 | return result; | |
1295 | ||
1296 | result = genl_register_mc_group(&thermal_event_genl_family, | |
1297 | &thermal_event_mcgrp); | |
1298 | if (result) | |
1299 | genl_unregister_family(&thermal_event_genl_family); | |
1300 | return result; | |
1301 | } | |
1302 | ||
af06216a RW |
1303 | static void genetlink_exit(void) |
1304 | { | |
1305 | genl_unregister_family(&thermal_event_genl_family); | |
1306 | } | |
1307 | #else /* !CONFIG_NET */ | |
1308 | static inline int genetlink_init(void) { return 0; } | |
1309 | static inline void genetlink_exit(void) {} | |
1310 | #endif /* !CONFIG_NET */ | |
1311 | ||
203d3d4a ZR |
1312 | static int __init thermal_init(void) |
1313 | { | |
1314 | int result = 0; | |
1315 | ||
1316 | result = class_register(&thermal_class); | |
1317 | if (result) { | |
1318 | idr_destroy(&thermal_tz_idr); | |
1319 | idr_destroy(&thermal_cdev_idr); | |
1320 | mutex_destroy(&thermal_idr_lock); | |
1321 | mutex_destroy(&thermal_list_lock); | |
1322 | } | |
4cb18728 | 1323 | result = genetlink_init(); |
203d3d4a ZR |
1324 | return result; |
1325 | } | |
1326 | ||
1327 | static void __exit thermal_exit(void) | |
1328 | { | |
1329 | class_unregister(&thermal_class); | |
1330 | idr_destroy(&thermal_tz_idr); | |
1331 | idr_destroy(&thermal_cdev_idr); | |
1332 | mutex_destroy(&thermal_idr_lock); | |
1333 | mutex_destroy(&thermal_list_lock); | |
4cb18728 | 1334 | genetlink_exit(); |
203d3d4a ZR |
1335 | } |
1336 | ||
4cb18728 | 1337 | fs_initcall(thermal_init); |
203d3d4a | 1338 | module_exit(thermal_exit); |