thermal: fix potential out-of-bounds memory access
[linux-2.6.git] / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
39
40 MODULE_AUTHOR("Zhang Rui");
41 MODULE_DESCRIPTION("Generic thermal management sysfs support");
42 MODULE_LICENSE("GPL");
43
44 #define THERMAL_NO_TARGET -1UL
45 /*
46  * This structure is used to describe the behavior of
47  * a certain cooling device on a certain trip point
48  * in a certain thermal zone
49  */
50 struct thermal_instance {
51         int id;
52         char name[THERMAL_NAME_LENGTH];
53         struct thermal_zone_device *tz;
54         struct thermal_cooling_device *cdev;
55         int trip;
56         unsigned long upper;    /* Highest cooling state for this trip point */
57         unsigned long lower;    /* Lowest cooling state for this trip point */
58         unsigned long target;   /* expected cooling state */
59         char attr_name[THERMAL_NAME_LENGTH];
60         struct device_attribute attr;
61         struct list_head tz_node; /* node in tz->thermal_instances */
62         struct list_head cdev_node; /* node in cdev->thermal_instances */
63 };
64
65 static DEFINE_IDR(thermal_tz_idr);
66 static DEFINE_IDR(thermal_cdev_idr);
67 static DEFINE_MUTEX(thermal_idr_lock);
68
69 static LIST_HEAD(thermal_tz_list);
70 static LIST_HEAD(thermal_cdev_list);
71 static DEFINE_MUTEX(thermal_list_lock);
72
73 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
74 {
75         int err;
76
77 again:
78         if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
79                 return -ENOMEM;
80
81         if (lock)
82                 mutex_lock(lock);
83         err = idr_get_new(idr, NULL, id);
84         if (lock)
85                 mutex_unlock(lock);
86         if (unlikely(err == -EAGAIN))
87                 goto again;
88         else if (unlikely(err))
89                 return err;
90
91         *id = *id & MAX_ID_MASK;
92         return 0;
93 }
94
95 static void release_idr(struct idr *idr, struct mutex *lock, int id)
96 {
97         if (lock)
98                 mutex_lock(lock);
99         idr_remove(idr, id);
100         if (lock)
101                 mutex_unlock(lock);
102 }
103
104 /* sys I/F for thermal zone */
105
106 #define to_thermal_zone(_dev) \
107         container_of(_dev, struct thermal_zone_device, device)
108
109 static ssize_t
110 type_show(struct device *dev, struct device_attribute *attr, char *buf)
111 {
112         struct thermal_zone_device *tz = to_thermal_zone(dev);
113
114         return sprintf(buf, "%s\n", tz->type);
115 }
116
117 static ssize_t
118 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
119 {
120         struct thermal_zone_device *tz = to_thermal_zone(dev);
121         long temperature;
122         int ret;
123
124         if (!tz->ops->get_temp)
125                 return -EPERM;
126
127         ret = tz->ops->get_temp(tz, &temperature);
128
129         if (ret)
130                 return ret;
131
132         return sprintf(buf, "%ld\n", temperature);
133 }
134
135 static ssize_t
136 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
137 {
138         struct thermal_zone_device *tz = to_thermal_zone(dev);
139         enum thermal_device_mode mode;
140         int result;
141
142         if (!tz->ops->get_mode)
143                 return -EPERM;
144
145         result = tz->ops->get_mode(tz, &mode);
146         if (result)
147                 return result;
148
149         return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
150                        : "disabled");
151 }
152
153 static ssize_t
154 mode_store(struct device *dev, struct device_attribute *attr,
155            const char *buf, size_t count)
156 {
157         struct thermal_zone_device *tz = to_thermal_zone(dev);
158         int result;
159
160         if (!tz->ops->set_mode)
161                 return -EPERM;
162
163         if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
164                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
165         else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
166                 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
167         else
168                 result = -EINVAL;
169
170         if (result)
171                 return result;
172
173         return count;
174 }
175
176 static ssize_t
177 trip_point_type_show(struct device *dev, struct device_attribute *attr,
178                      char *buf)
179 {
180         struct thermal_zone_device *tz = to_thermal_zone(dev);
181         enum thermal_trip_type type;
182         int trip, result;
183
184         if (!tz->ops->get_trip_type)
185                 return -EPERM;
186
187         if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
188                 return -EINVAL;
189
190         result = tz->ops->get_trip_type(tz, trip, &type);
191         if (result)
192                 return result;
193
194         switch (type) {
195         case THERMAL_TRIP_CRITICAL:
196                 return sprintf(buf, "critical\n");
197         case THERMAL_TRIP_HOT:
198                 return sprintf(buf, "hot\n");
199         case THERMAL_TRIP_PASSIVE:
200                 return sprintf(buf, "passive\n");
201         case THERMAL_TRIP_ACTIVE:
202                 return sprintf(buf, "active\n");
203         default:
204                 return sprintf(buf, "unknown\n");
205         }
206 }
207
208 static ssize_t
209 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
210                      const char *buf, size_t count)
211 {
212         struct thermal_zone_device *tz = to_thermal_zone(dev);
213         int trip, ret;
214         unsigned long temperature;
215
216         if (!tz->ops->set_trip_temp)
217                 return -EPERM;
218
219         if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
220                 return -EINVAL;
221
222         if (kstrtoul(buf, 10, &temperature))
223                 return -EINVAL;
224
225         ret = tz->ops->set_trip_temp(tz, trip, temperature);
226
227         return ret ? ret : count;
228 }
229
230 static ssize_t
231 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
232                      char *buf)
233 {
234         struct thermal_zone_device *tz = to_thermal_zone(dev);
235         int trip, ret;
236         long temperature;
237
238         if (!tz->ops->get_trip_temp)
239                 return -EPERM;
240
241         if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
242                 return -EINVAL;
243
244         ret = tz->ops->get_trip_temp(tz, trip, &temperature);
245
246         if (ret)
247                 return ret;
248
249         return sprintf(buf, "%ld\n", temperature);
250 }
251
252 static ssize_t
253 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
254                         const char *buf, size_t count)
255 {
256         struct thermal_zone_device *tz = to_thermal_zone(dev);
257         int trip, ret;
258         unsigned long temperature;
259
260         if (!tz->ops->set_trip_hyst)
261                 return -EPERM;
262
263         if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
264                 return -EINVAL;
265
266         if (kstrtoul(buf, 10, &temperature))
267                 return -EINVAL;
268
269         /*
270          * We are not doing any check on the 'temperature' value
271          * here. The driver implementing 'set_trip_hyst' has to
272          * take care of this.
273          */
274         ret = tz->ops->set_trip_hyst(tz, trip, temperature);
275
276         return ret ? ret : count;
277 }
278
279 static ssize_t
280 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
281                         char *buf)
282 {
283         struct thermal_zone_device *tz = to_thermal_zone(dev);
284         int trip, ret;
285         unsigned long temperature;
286
287         if (!tz->ops->get_trip_hyst)
288                 return -EPERM;
289
290         if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
291                 return -EINVAL;
292
293         ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
294
295         return ret ? ret : sprintf(buf, "%ld\n", temperature);
296 }
297
298 static ssize_t
299 passive_store(struct device *dev, struct device_attribute *attr,
300                     const char *buf, size_t count)
301 {
302         struct thermal_zone_device *tz = to_thermal_zone(dev);
303         struct thermal_cooling_device *cdev = NULL;
304         int state;
305
306         if (!sscanf(buf, "%d\n", &state))
307                 return -EINVAL;
308
309         /* sanity check: values below 1000 millicelcius don't make sense
310          * and can cause the system to go into a thermal heart attack
311          */
312         if (state && state < 1000)
313                 return -EINVAL;
314
315         if (state && !tz->forced_passive) {
316                 mutex_lock(&thermal_list_lock);
317                 list_for_each_entry(cdev, &thermal_cdev_list, node) {
318                         if (!strncmp("Processor", cdev->type,
319                                      sizeof("Processor")))
320                                 thermal_zone_bind_cooling_device(tz,
321                                                 THERMAL_TRIPS_NONE, cdev,
322                                                 THERMAL_NO_LIMIT,
323                                                 THERMAL_NO_LIMIT);
324                 }
325                 mutex_unlock(&thermal_list_lock);
326                 if (!tz->passive_delay)
327                         tz->passive_delay = 1000;
328         } else if (!state && tz->forced_passive) {
329                 mutex_lock(&thermal_list_lock);
330                 list_for_each_entry(cdev, &thermal_cdev_list, node) {
331                         if (!strncmp("Processor", cdev->type,
332                                      sizeof("Processor")))
333                                 thermal_zone_unbind_cooling_device(tz,
334                                                                    THERMAL_TRIPS_NONE,
335                                                                    cdev);
336                 }
337                 mutex_unlock(&thermal_list_lock);
338                 tz->passive_delay = 0;
339         }
340
341         tz->forced_passive = state;
342
343         thermal_zone_device_update(tz);
344
345         return count;
346 }
347
348 static ssize_t
349 passive_show(struct device *dev, struct device_attribute *attr,
350                    char *buf)
351 {
352         struct thermal_zone_device *tz = to_thermal_zone(dev);
353
354         return sprintf(buf, "%d\n", tz->forced_passive);
355 }
356
357 static DEVICE_ATTR(type, 0444, type_show, NULL);
358 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
359 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
360 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
361
362 /* sys I/F for cooling device */
363 #define to_cooling_device(_dev) \
364         container_of(_dev, struct thermal_cooling_device, device)
365
366 static ssize_t
367 thermal_cooling_device_type_show(struct device *dev,
368                                  struct device_attribute *attr, char *buf)
369 {
370         struct thermal_cooling_device *cdev = to_cooling_device(dev);
371
372         return sprintf(buf, "%s\n", cdev->type);
373 }
374
375 static ssize_t
376 thermal_cooling_device_max_state_show(struct device *dev,
377                                       struct device_attribute *attr, char *buf)
378 {
379         struct thermal_cooling_device *cdev = to_cooling_device(dev);
380         unsigned long state;
381         int ret;
382
383         ret = cdev->ops->get_max_state(cdev, &state);
384         if (ret)
385                 return ret;
386         return sprintf(buf, "%ld\n", state);
387 }
388
389 static ssize_t
390 thermal_cooling_device_cur_state_show(struct device *dev,
391                                       struct device_attribute *attr, char *buf)
392 {
393         struct thermal_cooling_device *cdev = to_cooling_device(dev);
394         unsigned long state;
395         int ret;
396
397         ret = cdev->ops->get_cur_state(cdev, &state);
398         if (ret)
399                 return ret;
400         return sprintf(buf, "%ld\n", state);
401 }
402
403 static ssize_t
404 thermal_cooling_device_cur_state_store(struct device *dev,
405                                        struct device_attribute *attr,
406                                        const char *buf, size_t count)
407 {
408         struct thermal_cooling_device *cdev = to_cooling_device(dev);
409         unsigned long state;
410         int result;
411
412         if (!sscanf(buf, "%ld\n", &state))
413                 return -EINVAL;
414
415         if ((long)state < 0)
416                 return -EINVAL;
417
418         result = cdev->ops->set_cur_state(cdev, state);
419         if (result)
420                 return result;
421         return count;
422 }
423
424 static struct device_attribute dev_attr_cdev_type =
425 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
426 static DEVICE_ATTR(max_state, 0444,
427                    thermal_cooling_device_max_state_show, NULL);
428 static DEVICE_ATTR(cur_state, 0644,
429                    thermal_cooling_device_cur_state_show,
430                    thermal_cooling_device_cur_state_store);
431
432 static ssize_t
433 thermal_cooling_device_trip_point_show(struct device *dev,
434                                        struct device_attribute *attr, char *buf)
435 {
436         struct thermal_instance *instance;
437
438         instance =
439             container_of(attr, struct thermal_instance, attr);
440
441         if (instance->trip == THERMAL_TRIPS_NONE)
442                 return sprintf(buf, "-1\n");
443         else
444                 return sprintf(buf, "%d\n", instance->trip);
445 }
446
447 /* Device management */
448
449 #if defined(CONFIG_THERMAL_HWMON)
450
451 /* hwmon sys I/F */
452 #include <linux/hwmon.h>
453
454 /* thermal zone devices with the same type share one hwmon device */
455 struct thermal_hwmon_device {
456         char type[THERMAL_NAME_LENGTH];
457         struct device *device;
458         int count;
459         struct list_head tz_list;
460         struct list_head node;
461 };
462
463 struct thermal_hwmon_attr {
464         struct device_attribute attr;
465         char name[16];
466 };
467
468 /* one temperature input for each thermal zone */
469 struct thermal_hwmon_temp {
470         struct list_head hwmon_node;
471         struct thermal_zone_device *tz;
472         struct thermal_hwmon_attr temp_input;   /* hwmon sys attr */
473         struct thermal_hwmon_attr temp_crit;    /* hwmon sys attr */
474 };
475
476 static LIST_HEAD(thermal_hwmon_list);
477
478 static ssize_t
479 name_show(struct device *dev, struct device_attribute *attr, char *buf)
480 {
481         struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
482         return sprintf(buf, "%s\n", hwmon->type);
483 }
484 static DEVICE_ATTR(name, 0444, name_show, NULL);
485
486 static ssize_t
487 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
488 {
489         long temperature;
490         int ret;
491         struct thermal_hwmon_attr *hwmon_attr
492                         = container_of(attr, struct thermal_hwmon_attr, attr);
493         struct thermal_hwmon_temp *temp
494                         = container_of(hwmon_attr, struct thermal_hwmon_temp,
495                                        temp_input);
496         struct thermal_zone_device *tz = temp->tz;
497
498         ret = tz->ops->get_temp(tz, &temperature);
499
500         if (ret)
501                 return ret;
502
503         return sprintf(buf, "%ld\n", temperature);
504 }
505
506 static ssize_t
507 temp_crit_show(struct device *dev, struct device_attribute *attr,
508                 char *buf)
509 {
510         struct thermal_hwmon_attr *hwmon_attr
511                         = container_of(attr, struct thermal_hwmon_attr, attr);
512         struct thermal_hwmon_temp *temp
513                         = container_of(hwmon_attr, struct thermal_hwmon_temp,
514                                        temp_crit);
515         struct thermal_zone_device *tz = temp->tz;
516         long temperature;
517         int ret;
518
519         ret = tz->ops->get_trip_temp(tz, 0, &temperature);
520         if (ret)
521                 return ret;
522
523         return sprintf(buf, "%ld\n", temperature);
524 }
525
526
527 static struct thermal_hwmon_device *
528 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
529 {
530         struct thermal_hwmon_device *hwmon;
531
532         mutex_lock(&thermal_list_lock);
533         list_for_each_entry(hwmon, &thermal_hwmon_list, node)
534                 if (!strcmp(hwmon->type, tz->type)) {
535                         mutex_unlock(&thermal_list_lock);
536                         return hwmon;
537                 }
538         mutex_unlock(&thermal_list_lock);
539
540         return NULL;
541 }
542
543 /* Find the temperature input matching a given thermal zone */
544 static struct thermal_hwmon_temp *
545 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
546                           const struct thermal_zone_device *tz)
547 {
548         struct thermal_hwmon_temp *temp;
549
550         mutex_lock(&thermal_list_lock);
551         list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
552                 if (temp->tz == tz) {
553                         mutex_unlock(&thermal_list_lock);
554                         return temp;
555                 }
556         mutex_unlock(&thermal_list_lock);
557
558         return NULL;
559 }
560
561 static int
562 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
563 {
564         struct thermal_hwmon_device *hwmon;
565         struct thermal_hwmon_temp *temp;
566         int new_hwmon_device = 1;
567         int result;
568
569         hwmon = thermal_hwmon_lookup_by_type(tz);
570         if (hwmon) {
571                 new_hwmon_device = 0;
572                 goto register_sys_interface;
573         }
574
575         hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
576         if (!hwmon)
577                 return -ENOMEM;
578
579         INIT_LIST_HEAD(&hwmon->tz_list);
580         strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
581         hwmon->device = hwmon_device_register(NULL);
582         if (IS_ERR(hwmon->device)) {
583                 result = PTR_ERR(hwmon->device);
584                 goto free_mem;
585         }
586         dev_set_drvdata(hwmon->device, hwmon);
587         result = device_create_file(hwmon->device, &dev_attr_name);
588         if (result)
589                 goto free_mem;
590
591  register_sys_interface:
592         temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
593         if (!temp) {
594                 result = -ENOMEM;
595                 goto unregister_name;
596         }
597
598         temp->tz = tz;
599         hwmon->count++;
600
601         snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
602                  "temp%d_input", hwmon->count);
603         temp->temp_input.attr.attr.name = temp->temp_input.name;
604         temp->temp_input.attr.attr.mode = 0444;
605         temp->temp_input.attr.show = temp_input_show;
606         sysfs_attr_init(&temp->temp_input.attr.attr);
607         result = device_create_file(hwmon->device, &temp->temp_input.attr);
608         if (result)
609                 goto free_temp_mem;
610
611         if (tz->ops->get_crit_temp) {
612                 unsigned long temperature;
613                 if (!tz->ops->get_crit_temp(tz, &temperature)) {
614                         snprintf(temp->temp_crit.name,
615                                  sizeof(temp->temp_crit.name),
616                                 "temp%d_crit", hwmon->count);
617                         temp->temp_crit.attr.attr.name = temp->temp_crit.name;
618                         temp->temp_crit.attr.attr.mode = 0444;
619                         temp->temp_crit.attr.show = temp_crit_show;
620                         sysfs_attr_init(&temp->temp_crit.attr.attr);
621                         result = device_create_file(hwmon->device,
622                                                     &temp->temp_crit.attr);
623                         if (result)
624                                 goto unregister_input;
625                 }
626         }
627
628         mutex_lock(&thermal_list_lock);
629         if (new_hwmon_device)
630                 list_add_tail(&hwmon->node, &thermal_hwmon_list);
631         list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
632         mutex_unlock(&thermal_list_lock);
633
634         return 0;
635
636  unregister_input:
637         device_remove_file(hwmon->device, &temp->temp_input.attr);
638  free_temp_mem:
639         kfree(temp);
640  unregister_name:
641         if (new_hwmon_device) {
642                 device_remove_file(hwmon->device, &dev_attr_name);
643                 hwmon_device_unregister(hwmon->device);
644         }
645  free_mem:
646         if (new_hwmon_device)
647                 kfree(hwmon);
648
649         return result;
650 }
651
652 static void
653 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
654 {
655         struct thermal_hwmon_device *hwmon;
656         struct thermal_hwmon_temp *temp;
657
658         hwmon = thermal_hwmon_lookup_by_type(tz);
659         if (unlikely(!hwmon)) {
660                 /* Should never happen... */
661                 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
662                 return;
663         }
664
665         temp = thermal_hwmon_lookup_temp(hwmon, tz);
666         if (unlikely(!temp)) {
667                 /* Should never happen... */
668                 dev_dbg(&tz->device, "temperature input lookup failed!\n");
669                 return;
670         }
671
672         device_remove_file(hwmon->device, &temp->temp_input.attr);
673         if (tz->ops->get_crit_temp)
674                 device_remove_file(hwmon->device, &temp->temp_crit.attr);
675
676         mutex_lock(&thermal_list_lock);
677         list_del(&temp->hwmon_node);
678         kfree(temp);
679         if (!list_empty(&hwmon->tz_list)) {
680                 mutex_unlock(&thermal_list_lock);
681                 return;
682         }
683         list_del(&hwmon->node);
684         mutex_unlock(&thermal_list_lock);
685
686         device_remove_file(hwmon->device, &dev_attr_name);
687         hwmon_device_unregister(hwmon->device);
688         kfree(hwmon);
689 }
690 #else
691 static int
692 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
693 {
694         return 0;
695 }
696
697 static void
698 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
699 {
700 }
701 #endif
702
703 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
704                                             int delay)
705 {
706         cancel_delayed_work(&(tz->poll_queue));
707
708         if (!delay)
709                 return;
710
711         if (delay > 1000)
712                 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
713                                       round_jiffies(msecs_to_jiffies(delay)));
714         else
715                 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
716                                       msecs_to_jiffies(delay));
717 }
718
719 static void thermal_zone_device_check(struct work_struct *work)
720 {
721         struct thermal_zone_device *tz = container_of(work, struct
722                                                       thermal_zone_device,
723                                                       poll_queue.work);
724         thermal_zone_device_update(tz);
725 }
726
727 /**
728  * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
729  * @tz:         thermal zone device
730  * @trip:       indicates which trip point the cooling devices is
731  *              associated with in this thermal zone.
732  * @cdev:       thermal cooling device
733  *
734  * This function is usually called in the thermal zone device .bind callback.
735  */
736 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
737                                      int trip,
738                                      struct thermal_cooling_device *cdev,
739                                      unsigned long upper, unsigned long lower)
740 {
741         struct thermal_instance *dev;
742         struct thermal_instance *pos;
743         struct thermal_zone_device *pos1;
744         struct thermal_cooling_device *pos2;
745         unsigned long max_state;
746         int result;
747
748         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
749                 return -EINVAL;
750
751         list_for_each_entry(pos1, &thermal_tz_list, node) {
752                 if (pos1 == tz)
753                         break;
754         }
755         list_for_each_entry(pos2, &thermal_cdev_list, node) {
756                 if (pos2 == cdev)
757                         break;
758         }
759
760         if (tz != pos1 || cdev != pos2)
761                 return -EINVAL;
762
763         cdev->ops->get_max_state(cdev, &max_state);
764
765         /* lower default 0, upper default max_state */
766         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
767         upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
768
769         if (lower > upper || upper > max_state)
770                 return -EINVAL;
771
772         dev =
773             kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
774         if (!dev)
775                 return -ENOMEM;
776         dev->tz = tz;
777         dev->cdev = cdev;
778         dev->trip = trip;
779         dev->upper = upper;
780         dev->lower = lower;
781         dev->target = THERMAL_NO_TARGET;
782
783         result = get_idr(&tz->idr, &tz->lock, &dev->id);
784         if (result)
785                 goto free_mem;
786
787         sprintf(dev->name, "cdev%d", dev->id);
788         result =
789             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
790         if (result)
791                 goto release_idr;
792
793         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
794         sysfs_attr_init(&dev->attr.attr);
795         dev->attr.attr.name = dev->attr_name;
796         dev->attr.attr.mode = 0444;
797         dev->attr.show = thermal_cooling_device_trip_point_show;
798         result = device_create_file(&tz->device, &dev->attr);
799         if (result)
800                 goto remove_symbol_link;
801
802         mutex_lock(&tz->lock);
803         mutex_lock(&cdev->lock);
804         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
805             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
806                 result = -EEXIST;
807                 break;
808         }
809         if (!result) {
810                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
811                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
812         }
813         mutex_unlock(&cdev->lock);
814         mutex_unlock(&tz->lock);
815
816         if (!result)
817                 return 0;
818
819         device_remove_file(&tz->device, &dev->attr);
820 remove_symbol_link:
821         sysfs_remove_link(&tz->device.kobj, dev->name);
822 release_idr:
823         release_idr(&tz->idr, &tz->lock, dev->id);
824 free_mem:
825         kfree(dev);
826         return result;
827 }
828 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
829
830 /**
831  * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
832  * @tz:         thermal zone device
833  * @trip:       indicates which trip point the cooling devices is
834  *              associated with in this thermal zone.
835  * @cdev:       thermal cooling device
836  *
837  * This function is usually called in the thermal zone device .unbind callback.
838  */
839 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
840                                        int trip,
841                                        struct thermal_cooling_device *cdev)
842 {
843         struct thermal_instance *pos, *next;
844
845         mutex_lock(&tz->lock);
846         mutex_lock(&cdev->lock);
847         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
848                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
849                         list_del(&pos->tz_node);
850                         list_del(&pos->cdev_node);
851                         mutex_unlock(&cdev->lock);
852                         mutex_unlock(&tz->lock);
853                         goto unbind;
854                 }
855         }
856         mutex_unlock(&cdev->lock);
857         mutex_unlock(&tz->lock);
858
859         return -ENODEV;
860
861 unbind:
862         device_remove_file(&tz->device, &pos->attr);
863         sysfs_remove_link(&tz->device.kobj, pos->name);
864         release_idr(&tz->idr, &tz->lock, pos->id);
865         kfree(pos);
866         return 0;
867 }
868 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
869
870 static void thermal_release(struct device *dev)
871 {
872         struct thermal_zone_device *tz;
873         struct thermal_cooling_device *cdev;
874
875         if (!strncmp(dev_name(dev), "thermal_zone",
876                      sizeof("thermal_zone") - 1)) {
877                 tz = to_thermal_zone(dev);
878                 kfree(tz);
879         } else {
880                 cdev = to_cooling_device(dev);
881                 kfree(cdev);
882         }
883 }
884
885 static struct class thermal_class = {
886         .name = "thermal",
887         .dev_release = thermal_release,
888 };
889
890 /**
891  * thermal_cooling_device_register - register a new thermal cooling device
892  * @type:       the thermal cooling device type.
893  * @devdata:    device private data.
894  * @ops:                standard thermal cooling devices callbacks.
895  */
896 struct thermal_cooling_device *
897 thermal_cooling_device_register(char *type, void *devdata,
898                                 const struct thermal_cooling_device_ops *ops)
899 {
900         struct thermal_cooling_device *cdev;
901         struct thermal_zone_device *pos;
902         int result;
903
904         if (strlen(type) >= THERMAL_NAME_LENGTH)
905                 return ERR_PTR(-EINVAL);
906
907         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
908             !ops->set_cur_state)
909                 return ERR_PTR(-EINVAL);
910
911         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
912         if (!cdev)
913                 return ERR_PTR(-ENOMEM);
914
915         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
916         if (result) {
917                 kfree(cdev);
918                 return ERR_PTR(result);
919         }
920
921         strcpy(cdev->type, type);
922         mutex_init(&cdev->lock);
923         INIT_LIST_HEAD(&cdev->thermal_instances);
924         cdev->ops = ops;
925         cdev->updated = true;
926         cdev->device.class = &thermal_class;
927         cdev->devdata = devdata;
928         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
929         result = device_register(&cdev->device);
930         if (result) {
931                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
932                 kfree(cdev);
933                 return ERR_PTR(result);
934         }
935
936         /* sys I/F */
937         if (type) {
938                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
939                 if (result)
940                         goto unregister;
941         }
942
943         result = device_create_file(&cdev->device, &dev_attr_max_state);
944         if (result)
945                 goto unregister;
946
947         result = device_create_file(&cdev->device, &dev_attr_cur_state);
948         if (result)
949                 goto unregister;
950
951         mutex_lock(&thermal_list_lock);
952         list_add(&cdev->node, &thermal_cdev_list);
953         list_for_each_entry(pos, &thermal_tz_list, node) {
954                 if (!pos->ops->bind)
955                         continue;
956                 result = pos->ops->bind(pos, cdev);
957                 if (result)
958                         break;
959
960         }
961         mutex_unlock(&thermal_list_lock);
962
963         if (!result)
964                 return cdev;
965
966 unregister:
967         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
968         device_unregister(&cdev->device);
969         return ERR_PTR(result);
970 }
971 EXPORT_SYMBOL(thermal_cooling_device_register);
972
973 /**
974  * thermal_cooling_device_unregister - removes the registered thermal cooling device
975  * @cdev:       the thermal cooling device to remove.
976  *
977  * thermal_cooling_device_unregister() must be called when the device is no
978  * longer needed.
979  */
980 void thermal_cooling_device_unregister(struct
981                                        thermal_cooling_device
982                                        *cdev)
983 {
984         struct thermal_zone_device *tz;
985         struct thermal_cooling_device *pos = NULL;
986
987         if (!cdev)
988                 return;
989
990         mutex_lock(&thermal_list_lock);
991         list_for_each_entry(pos, &thermal_cdev_list, node)
992             if (pos == cdev)
993                 break;
994         if (pos != cdev) {
995                 /* thermal cooling device not found */
996                 mutex_unlock(&thermal_list_lock);
997                 return;
998         }
999         list_del(&cdev->node);
1000         list_for_each_entry(tz, &thermal_tz_list, node) {
1001                 if (!tz->ops->unbind)
1002                         continue;
1003                 tz->ops->unbind(tz, cdev);
1004         }
1005         mutex_unlock(&thermal_list_lock);
1006         if (cdev->type[0])
1007                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1008         device_remove_file(&cdev->device, &dev_attr_max_state);
1009         device_remove_file(&cdev->device, &dev_attr_cur_state);
1010
1011         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1012         device_unregister(&cdev->device);
1013         return;
1014 }
1015 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1016
1017 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1018 {
1019         struct thermal_instance *instance;
1020         unsigned long target = 0;
1021
1022         /* cooling device is updated*/
1023         if (cdev->updated)
1024                 return;
1025
1026         mutex_lock(&cdev->lock);
1027         /* Make sure cdev enters the deepest cooling state */
1028         list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1029                 if (instance->target == THERMAL_NO_TARGET)
1030                         continue;
1031                 if (instance->target > target)
1032                         target = instance->target;
1033         }
1034         mutex_unlock(&cdev->lock);
1035         cdev->ops->set_cur_state(cdev, target);
1036         cdev->updated = true;
1037 }
1038
1039 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1040 {
1041         struct thermal_instance *instance;
1042
1043         list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1044                 thermal_cdev_do_update(instance->cdev);
1045 }
1046
1047 /*
1048  * Cooling algorithm for both active and passive cooling
1049  *
1050  * 1. if the temperature is higher than a trip point,
1051  *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1052  *       state for this trip point
1053  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1054  *       state for this trip point
1055  *
1056  * 2. if the temperature is lower than a trip point, use lower
1057  *    cooling state for this trip point
1058  *
1059  * Note that this behaves the same as the previous passive cooling
1060  * algorithm.
1061  */
1062
1063 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1064                                      int trip, long temp)
1065 {
1066         struct thermal_instance *instance;
1067         struct thermal_cooling_device *cdev = NULL;
1068         unsigned long cur_state, max_state;
1069         long trip_temp;
1070         enum thermal_trip_type trip_type;
1071         enum thermal_trend trend;
1072
1073         if (trip == THERMAL_TRIPS_NONE) {
1074                 trip_temp = tz->forced_passive;
1075                 trip_type = THERMAL_TRIPS_NONE;
1076         } else {
1077                 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1078                 tz->ops->get_trip_type(tz, trip, &trip_type);
1079         }
1080
1081         if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1082                 /*
1083                  * compare the current temperature and previous temperature
1084                  * to get the thermal trend, if no special requirement
1085                  */
1086                 if (tz->temperature > tz->last_temperature)
1087                         trend = THERMAL_TREND_RAISING;
1088                 else if (tz->temperature < tz->last_temperature)
1089                         trend = THERMAL_TREND_DROPPING;
1090                 else
1091                         trend = THERMAL_TREND_STABLE;
1092         }
1093
1094         if (temp >= trip_temp) {
1095                 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1096                         if (instance->trip != trip)
1097                                 continue;
1098
1099                         cdev = instance->cdev;
1100
1101                         cdev->ops->get_cur_state(cdev, &cur_state);
1102                         cdev->ops->get_max_state(cdev, &max_state);
1103
1104                         if (trend == THERMAL_TREND_RAISING) {
1105                                 cur_state = cur_state < instance->upper ?
1106                                             (cur_state + 1) : instance->upper;
1107                         } else if (trend == THERMAL_TREND_DROPPING) {
1108                                 cur_state = cur_state > instance->lower ?
1109                                     (cur_state - 1) : instance->lower;
1110                         }
1111
1112                         /* activate a passive thermal instance */
1113                         if ((trip_type == THERMAL_TRIP_PASSIVE ||
1114                              trip_type == THERMAL_TRIPS_NONE) &&
1115                              instance->target == THERMAL_NO_TARGET)
1116                                 tz->passive++;
1117
1118                         instance->target = cur_state;
1119                         cdev->updated = false; /* cooling device needs update */
1120                 }
1121         } else {        /* below trip */
1122                 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1123                         if (instance->trip != trip)
1124                                 continue;
1125
1126                         /* Do not use the inactive thermal instance */
1127                         if (instance->target == THERMAL_NO_TARGET)
1128                                 continue;
1129                         cdev = instance->cdev;
1130                         cdev->ops->get_cur_state(cdev, &cur_state);
1131
1132                         cur_state = cur_state > instance->lower ?
1133                                     (cur_state - 1) : THERMAL_NO_TARGET;
1134
1135                         /* deactivate a passive thermal instance */
1136                         if ((trip_type == THERMAL_TRIP_PASSIVE ||
1137                              trip_type == THERMAL_TRIPS_NONE) &&
1138                              cur_state == THERMAL_NO_TARGET)
1139                                 tz->passive--;
1140                         instance->target = cur_state;
1141                         cdev->updated = false; /* cooling device needs update */
1142                 }
1143         }
1144
1145         return;
1146 }
1147 /**
1148  * thermal_zone_device_update - force an update of a thermal zone's state
1149  * @ttz:        the thermal zone to update
1150  */
1151
1152 void thermal_zone_device_update(struct thermal_zone_device *tz)
1153 {
1154         int count, ret = 0;
1155         long temp, trip_temp;
1156         enum thermal_trip_type trip_type;
1157
1158         mutex_lock(&tz->lock);
1159
1160         if (tz->ops->get_temp(tz, &temp)) {
1161                 /* get_temp failed - retry it later */
1162                 pr_warn("failed to read out thermal zone %d\n", tz->id);
1163                 goto leave;
1164         }
1165
1166         tz->last_temperature = tz->temperature;
1167         tz->temperature = temp;
1168
1169         for (count = 0; count < tz->trips; count++) {
1170                 tz->ops->get_trip_type(tz, count, &trip_type);
1171                 tz->ops->get_trip_temp(tz, count, &trip_temp);
1172
1173                 switch (trip_type) {
1174                 case THERMAL_TRIP_CRITICAL:
1175                         if (temp >= trip_temp) {
1176                                 if (tz->ops->notify)
1177                                         ret = tz->ops->notify(tz, count,
1178                                                               trip_type);
1179                                 if (!ret) {
1180                                         pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1181                                                  temp/1000);
1182                                         orderly_poweroff(true);
1183                                 }
1184                         }
1185                         break;
1186                 case THERMAL_TRIP_HOT:
1187                         if (temp >= trip_temp)
1188                                 if (tz->ops->notify)
1189                                         tz->ops->notify(tz, count, trip_type);
1190                         break;
1191                 case THERMAL_TRIP_ACTIVE:
1192                         thermal_zone_trip_update(tz, count, temp);
1193                         break;
1194                 case THERMAL_TRIP_PASSIVE:
1195                         if (temp >= trip_temp || tz->passive)
1196                                 thermal_zone_trip_update(tz, count, temp);
1197                         break;
1198                 }
1199         }
1200
1201         if (tz->forced_passive)
1202                 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1203         thermal_zone_do_update(tz);
1204
1205 leave:
1206         if (tz->passive)
1207                 thermal_zone_device_set_polling(tz, tz->passive_delay);
1208         else if (tz->polling_delay)
1209                 thermal_zone_device_set_polling(tz, tz->polling_delay);
1210         else
1211                 thermal_zone_device_set_polling(tz, 0);
1212         mutex_unlock(&tz->lock);
1213 }
1214 EXPORT_SYMBOL(thermal_zone_device_update);
1215
1216 struct thermal_zone_device *thermal_zone_device_find(void *data,
1217         int (*match)(struct thermal_zone_device *, void *))
1218 {
1219         struct thermal_zone_device *thz;
1220
1221         mutex_lock(&thermal_list_lock);
1222         list_for_each_entry(thz, &thermal_tz_list, node)
1223                 if (match(thz, data)) {
1224                         mutex_unlock(&thermal_list_lock);
1225                         return thz;
1226                 }
1227
1228         mutex_unlock(&thermal_list_lock);
1229         return NULL;
1230 }
1231 EXPORT_SYMBOL(thermal_zone_device_find);
1232
1233 /**
1234  * create_trip_attrs - create attributes for trip points
1235  * @tz:         the thermal zone device
1236  * @mask:       Writeable trip point bitmap.
1237  */
1238 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1239 {
1240         int indx;
1241         int size = sizeof(struct thermal_attr) * tz->trips;
1242
1243         tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1244         if (!tz->trip_type_attrs)
1245                 return -ENOMEM;
1246
1247         tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1248         if (!tz->trip_temp_attrs) {
1249                 kfree(tz->trip_type_attrs);
1250                 return -ENOMEM;
1251         }
1252
1253         if (tz->ops->get_trip_hyst) {
1254                 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1255                 if (!tz->trip_hyst_attrs) {
1256                         kfree(tz->trip_type_attrs);
1257                         kfree(tz->trip_temp_attrs);
1258                         return -ENOMEM;
1259                 }
1260         }
1261
1262
1263         for (indx = 0; indx < tz->trips; indx++) {
1264                 /* create trip type attribute */
1265                 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1266                          "trip_point_%d_type", indx);
1267
1268                 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1269                 tz->trip_type_attrs[indx].attr.attr.name =
1270                                                 tz->trip_type_attrs[indx].name;
1271                 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1272                 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1273
1274                 device_create_file(&tz->device,
1275                                    &tz->trip_type_attrs[indx].attr);
1276
1277                 /* create trip temp attribute */
1278                 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1279                          "trip_point_%d_temp", indx);
1280
1281                 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1282                 tz->trip_temp_attrs[indx].attr.attr.name =
1283                                                 tz->trip_temp_attrs[indx].name;
1284                 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1285                 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1286                 if (mask & (1 << indx)) {
1287                         tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1288                         tz->trip_temp_attrs[indx].attr.store =
1289                                                         trip_point_temp_store;
1290                 }
1291
1292                 device_create_file(&tz->device,
1293                                    &tz->trip_temp_attrs[indx].attr);
1294
1295                 /* create Optional trip hyst attribute */
1296                 if (!tz->ops->get_trip_hyst)
1297                         continue;
1298                 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1299                          "trip_point_%d_hyst", indx);
1300
1301                 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1302                 tz->trip_hyst_attrs[indx].attr.attr.name =
1303                                         tz->trip_hyst_attrs[indx].name;
1304                 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1305                 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1306                 if (tz->ops->set_trip_hyst) {
1307                         tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1308                         tz->trip_hyst_attrs[indx].attr.store =
1309                                         trip_point_hyst_store;
1310                 }
1311
1312                 device_create_file(&tz->device,
1313                                    &tz->trip_hyst_attrs[indx].attr);
1314         }
1315         return 0;
1316 }
1317
1318 static void remove_trip_attrs(struct thermal_zone_device *tz)
1319 {
1320         int indx;
1321
1322         for (indx = 0; indx < tz->trips; indx++) {
1323                 device_remove_file(&tz->device,
1324                                    &tz->trip_type_attrs[indx].attr);
1325                 device_remove_file(&tz->device,
1326                                    &tz->trip_temp_attrs[indx].attr);
1327                 if (tz->ops->get_trip_hyst)
1328                         device_remove_file(&tz->device,
1329                                   &tz->trip_hyst_attrs[indx].attr);
1330         }
1331         kfree(tz->trip_type_attrs);
1332         kfree(tz->trip_temp_attrs);
1333         kfree(tz->trip_hyst_attrs);
1334 }
1335
1336 /**
1337  * thermal_zone_device_register - register a new thermal zone device
1338  * @type:       the thermal zone device type
1339  * @trips:      the number of trip points the thermal zone support
1340  * @mask:       a bit string indicating the writeablility of trip points
1341  * @devdata:    private device data
1342  * @ops:        standard thermal zone device callbacks
1343  * @passive_delay: number of milliseconds to wait between polls when
1344  *                 performing passive cooling
1345  * @polling_delay: number of milliseconds to wait between polls when checking
1346  *                 whether trip points have been crossed (0 for interrupt
1347  *                 driven systems)
1348  *
1349  * thermal_zone_device_unregister() must be called when the device is no
1350  * longer needed. The passive cooling depends on the .get_trend() return value.
1351  */
1352 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1353         int trips, int mask, void *devdata,
1354         const struct thermal_zone_device_ops *ops,
1355         int passive_delay, int polling_delay)
1356 {
1357         struct thermal_zone_device *tz;
1358         struct thermal_cooling_device *pos;
1359         enum thermal_trip_type trip_type;
1360         int result;
1361         int count;
1362         int passive = 0;
1363
1364         if (strlen(type) >= THERMAL_NAME_LENGTH)
1365                 return ERR_PTR(-EINVAL);
1366
1367         if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1368                 return ERR_PTR(-EINVAL);
1369
1370         if (!ops || !ops->get_temp)
1371                 return ERR_PTR(-EINVAL);
1372
1373         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1374         if (!tz)
1375                 return ERR_PTR(-ENOMEM);
1376
1377         INIT_LIST_HEAD(&tz->thermal_instances);
1378         idr_init(&tz->idr);
1379         mutex_init(&tz->lock);
1380         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1381         if (result) {
1382                 kfree(tz);
1383                 return ERR_PTR(result);
1384         }
1385
1386         strcpy(tz->type, type);
1387         tz->ops = ops;
1388         tz->device.class = &thermal_class;
1389         tz->devdata = devdata;
1390         tz->trips = trips;
1391         tz->passive_delay = passive_delay;
1392         tz->polling_delay = polling_delay;
1393
1394         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1395         result = device_register(&tz->device);
1396         if (result) {
1397                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1398                 kfree(tz);
1399                 return ERR_PTR(result);
1400         }
1401
1402         /* sys I/F */
1403         if (type) {
1404                 result = device_create_file(&tz->device, &dev_attr_type);
1405                 if (result)
1406                         goto unregister;
1407         }
1408
1409         result = device_create_file(&tz->device, &dev_attr_temp);
1410         if (result)
1411                 goto unregister;
1412
1413         if (ops->get_mode) {
1414                 result = device_create_file(&tz->device, &dev_attr_mode);
1415                 if (result)
1416                         goto unregister;
1417         }
1418
1419         result = create_trip_attrs(tz, mask);
1420         if (result)
1421                 goto unregister;
1422
1423         for (count = 0; count < trips; count++) {
1424                 tz->ops->get_trip_type(tz, count, &trip_type);
1425                 if (trip_type == THERMAL_TRIP_PASSIVE)
1426                         passive = 1;
1427         }
1428
1429         if (!passive)
1430                 result = device_create_file(&tz->device,
1431                                             &dev_attr_passive);
1432
1433         if (result)
1434                 goto unregister;
1435
1436         result = thermal_add_hwmon_sysfs(tz);
1437         if (result)
1438                 goto unregister;
1439
1440         mutex_lock(&thermal_list_lock);
1441         list_add_tail(&tz->node, &thermal_tz_list);
1442         if (ops->bind)
1443                 list_for_each_entry(pos, &thermal_cdev_list, node) {
1444                 result = ops->bind(tz, pos);
1445                 if (result)
1446                         break;
1447                 }
1448         mutex_unlock(&thermal_list_lock);
1449
1450         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1451
1452         thermal_zone_device_update(tz);
1453
1454         if (!result)
1455                 return tz;
1456
1457 unregister:
1458         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1459         device_unregister(&tz->device);
1460         return ERR_PTR(result);
1461 }
1462 EXPORT_SYMBOL(thermal_zone_device_register);
1463
1464 /**
1465  * thermal_device_unregister - removes the registered thermal zone device
1466  * @tz: the thermal zone device to remove
1467  */
1468 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1469 {
1470         struct thermal_cooling_device *cdev;
1471         struct thermal_zone_device *pos = NULL;
1472
1473         if (!tz)
1474                 return;
1475
1476         mutex_lock(&thermal_list_lock);
1477         list_for_each_entry(pos, &thermal_tz_list, node)
1478             if (pos == tz)
1479                 break;
1480         if (pos != tz) {
1481                 /* thermal zone device not found */
1482                 mutex_unlock(&thermal_list_lock);
1483                 return;
1484         }
1485         list_del(&tz->node);
1486         if (tz->ops->unbind)
1487                 list_for_each_entry(cdev, &thermal_cdev_list, node)
1488                     tz->ops->unbind(tz, cdev);
1489         mutex_unlock(&thermal_list_lock);
1490
1491         thermal_zone_device_set_polling(tz, 0);
1492
1493         if (tz->type[0])
1494                 device_remove_file(&tz->device, &dev_attr_type);
1495         device_remove_file(&tz->device, &dev_attr_temp);
1496         if (tz->ops->get_mode)
1497                 device_remove_file(&tz->device, &dev_attr_mode);
1498         remove_trip_attrs(tz);
1499
1500         thermal_remove_hwmon_sysfs(tz);
1501         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1502         idr_destroy(&tz->idr);
1503         mutex_destroy(&tz->lock);
1504         device_unregister(&tz->device);
1505         return;
1506 }
1507 EXPORT_SYMBOL(thermal_zone_device_unregister);
1508
1509 #ifdef CONFIG_NET
1510 static struct genl_family thermal_event_genl_family = {
1511         .id = GENL_ID_GENERATE,
1512         .name = THERMAL_GENL_FAMILY_NAME,
1513         .version = THERMAL_GENL_VERSION,
1514         .maxattr = THERMAL_GENL_ATTR_MAX,
1515 };
1516
1517 static struct genl_multicast_group thermal_event_mcgrp = {
1518         .name = THERMAL_GENL_MCAST_GROUP_NAME,
1519 };
1520
1521 int thermal_generate_netlink_event(u32 orig, enum events event)
1522 {
1523         struct sk_buff *skb;
1524         struct nlattr *attr;
1525         struct thermal_genl_event *thermal_event;
1526         void *msg_header;
1527         int size;
1528         int result;
1529         static unsigned int thermal_event_seqnum;
1530
1531         /* allocate memory */
1532         size = nla_total_size(sizeof(struct thermal_genl_event)) +
1533                nla_total_size(0);
1534
1535         skb = genlmsg_new(size, GFP_ATOMIC);
1536         if (!skb)
1537                 return -ENOMEM;
1538
1539         /* add the genetlink message header */
1540         msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1541                                  &thermal_event_genl_family, 0,
1542                                  THERMAL_GENL_CMD_EVENT);
1543         if (!msg_header) {
1544                 nlmsg_free(skb);
1545                 return -ENOMEM;
1546         }
1547
1548         /* fill the data */
1549         attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1550                            sizeof(struct thermal_genl_event));
1551
1552         if (!attr) {
1553                 nlmsg_free(skb);
1554                 return -EINVAL;
1555         }
1556
1557         thermal_event = nla_data(attr);
1558         if (!thermal_event) {
1559                 nlmsg_free(skb);
1560                 return -EINVAL;
1561         }
1562
1563         memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1564
1565         thermal_event->orig = orig;
1566         thermal_event->event = event;
1567
1568         /* send multicast genetlink message */
1569         result = genlmsg_end(skb, msg_header);
1570         if (result < 0) {
1571                 nlmsg_free(skb);
1572                 return result;
1573         }
1574
1575         result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1576         if (result)
1577                 pr_info("failed to send netlink event:%d\n", result);
1578
1579         return result;
1580 }
1581 EXPORT_SYMBOL(thermal_generate_netlink_event);
1582
1583 static int genetlink_init(void)
1584 {
1585         int result;
1586
1587         result = genl_register_family(&thermal_event_genl_family);
1588         if (result)
1589                 return result;
1590
1591         result = genl_register_mc_group(&thermal_event_genl_family,
1592                                         &thermal_event_mcgrp);
1593         if (result)
1594                 genl_unregister_family(&thermal_event_genl_family);
1595         return result;
1596 }
1597
1598 static void genetlink_exit(void)
1599 {
1600         genl_unregister_family(&thermal_event_genl_family);
1601 }
1602 #else /* !CONFIG_NET */
1603 static inline int genetlink_init(void) { return 0; }
1604 static inline void genetlink_exit(void) {}
1605 #endif /* !CONFIG_NET */
1606
1607 static int __init thermal_init(void)
1608 {
1609         int result = 0;
1610
1611         result = class_register(&thermal_class);
1612         if (result) {
1613                 idr_destroy(&thermal_tz_idr);
1614                 idr_destroy(&thermal_cdev_idr);
1615                 mutex_destroy(&thermal_idr_lock);
1616                 mutex_destroy(&thermal_list_lock);
1617         }
1618         result = genetlink_init();
1619         return result;
1620 }
1621
1622 static void __exit thermal_exit(void)
1623 {
1624         class_unregister(&thermal_class);
1625         idr_destroy(&thermal_tz_idr);
1626         idr_destroy(&thermal_cdev_idr);
1627         mutex_destroy(&thermal_idr_lock);
1628         mutex_destroy(&thermal_list_lock);
1629         genetlink_exit();
1630 }
1631
1632 fs_initcall(thermal_init);
1633 module_exit(thermal_exit);