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