Thermal: Introduce locking for cdev.thermal_instances list.
[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, THERMAL_NAME_LENGTH,
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, THERMAL_NAME_LENGTH,
615                                 "temp%d_crit", hwmon->count);
616                         temp->temp_crit.attr.attr.name = temp->temp_crit.name;
617                         temp->temp_crit.attr.attr.mode = 0444;
618                         temp->temp_crit.attr.show = temp_crit_show;
619                         sysfs_attr_init(&temp->temp_crit.attr.attr);
620                         result = device_create_file(hwmon->device,
621                                                     &temp->temp_crit.attr);
622                         if (result)
623                                 goto unregister_input;
624                 }
625         }
626
627         mutex_lock(&thermal_list_lock);
628         if (new_hwmon_device)
629                 list_add_tail(&hwmon->node, &thermal_hwmon_list);
630         list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
631         mutex_unlock(&thermal_list_lock);
632
633         return 0;
634
635  unregister_input:
636         device_remove_file(hwmon->device, &temp->temp_input.attr);
637  free_temp_mem:
638         kfree(temp);
639  unregister_name:
640         if (new_hwmon_device) {
641                 device_remove_file(hwmon->device, &dev_attr_name);
642                 hwmon_device_unregister(hwmon->device);
643         }
644  free_mem:
645         if (new_hwmon_device)
646                 kfree(hwmon);
647
648         return result;
649 }
650
651 static void
652 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
653 {
654         struct thermal_hwmon_device *hwmon;
655         struct thermal_hwmon_temp *temp;
656
657         hwmon = thermal_hwmon_lookup_by_type(tz);
658         if (unlikely(!hwmon)) {
659                 /* Should never happen... */
660                 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
661                 return;
662         }
663
664         temp = thermal_hwmon_lookup_temp(hwmon, tz);
665         if (unlikely(!temp)) {
666                 /* Should never happen... */
667                 dev_dbg(&tz->device, "temperature input lookup failed!\n");
668                 return;
669         }
670
671         device_remove_file(hwmon->device, &temp->temp_input.attr);
672         if (tz->ops->get_crit_temp)
673                 device_remove_file(hwmon->device, &temp->temp_crit.attr);
674
675         mutex_lock(&thermal_list_lock);
676         list_del(&temp->hwmon_node);
677         kfree(temp);
678         if (!list_empty(&hwmon->tz_list)) {
679                 mutex_unlock(&thermal_list_lock);
680                 return;
681         }
682         list_del(&hwmon->node);
683         mutex_unlock(&thermal_list_lock);
684
685         device_remove_file(hwmon->device, &dev_attr_name);
686         hwmon_device_unregister(hwmon->device);
687         kfree(hwmon);
688 }
689 #else
690 static int
691 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
692 {
693         return 0;
694 }
695
696 static void
697 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
698 {
699 }
700 #endif
701
702 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
703                                             int delay)
704 {
705         cancel_delayed_work(&(tz->poll_queue));
706
707         if (!delay)
708                 return;
709
710         if (delay > 1000)
711                 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
712                                       round_jiffies(msecs_to_jiffies(delay)));
713         else
714                 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
715                                       msecs_to_jiffies(delay));
716 }
717
718 static void thermal_zone_device_check(struct work_struct *work)
719 {
720         struct thermal_zone_device *tz = container_of(work, struct
721                                                       thermal_zone_device,
722                                                       poll_queue.work);
723         thermal_zone_device_update(tz);
724 }
725
726 /**
727  * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
728  * @tz:         thermal zone device
729  * @trip:       indicates which trip point the cooling devices is
730  *              associated with in this thermal zone.
731  * @cdev:       thermal cooling device
732  *
733  * This function is usually called in the thermal zone device .bind callback.
734  */
735 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
736                                      int trip,
737                                      struct thermal_cooling_device *cdev,
738                                      unsigned long upper, unsigned long lower)
739 {
740         struct thermal_instance *dev;
741         struct thermal_instance *pos;
742         struct thermal_zone_device *pos1;
743         struct thermal_cooling_device *pos2;
744         unsigned long max_state;
745         int result;
746
747         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
748                 return -EINVAL;
749
750         list_for_each_entry(pos1, &thermal_tz_list, node) {
751                 if (pos1 == tz)
752                         break;
753         }
754         list_for_each_entry(pos2, &thermal_cdev_list, node) {
755                 if (pos2 == cdev)
756                         break;
757         }
758
759         if (tz != pos1 || cdev != pos2)
760                 return -EINVAL;
761
762         cdev->ops->get_max_state(cdev, &max_state);
763
764         /* lower default 0, upper default max_state */
765         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
766         upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
767
768         if (lower > upper || upper > max_state)
769                 return -EINVAL;
770
771         dev =
772             kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
773         if (!dev)
774                 return -ENOMEM;
775         dev->tz = tz;
776         dev->cdev = cdev;
777         dev->trip = trip;
778         dev->upper = upper;
779         dev->lower = lower;
780         dev->target = THERMAL_NO_TARGET;
781
782         result = get_idr(&tz->idr, &tz->lock, &dev->id);
783         if (result)
784                 goto free_mem;
785
786         sprintf(dev->name, "cdev%d", dev->id);
787         result =
788             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
789         if (result)
790                 goto release_idr;
791
792         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
793         sysfs_attr_init(&dev->attr.attr);
794         dev->attr.attr.name = dev->attr_name;
795         dev->attr.attr.mode = 0444;
796         dev->attr.show = thermal_cooling_device_trip_point_show;
797         result = device_create_file(&tz->device, &dev->attr);
798         if (result)
799                 goto remove_symbol_link;
800
801         mutex_lock(&tz->lock);
802         mutex_lock(&cdev->lock);
803         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
804             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
805                 result = -EEXIST;
806                 break;
807         }
808         if (!result) {
809                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
810                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
811         }
812         mutex_unlock(&cdev->lock);
813         mutex_unlock(&tz->lock);
814
815         if (!result)
816                 return 0;
817
818         device_remove_file(&tz->device, &dev->attr);
819 remove_symbol_link:
820         sysfs_remove_link(&tz->device.kobj, dev->name);
821 release_idr:
822         release_idr(&tz->idr, &tz->lock, dev->id);
823 free_mem:
824         kfree(dev);
825         return result;
826 }
827 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
828
829 /**
830  * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
831  * @tz:         thermal zone device
832  * @trip:       indicates which trip point the cooling devices is
833  *              associated with in this thermal zone.
834  * @cdev:       thermal cooling device
835  *
836  * This function is usually called in the thermal zone device .unbind callback.
837  */
838 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
839                                        int trip,
840                                        struct thermal_cooling_device *cdev)
841 {
842         struct thermal_instance *pos, *next;
843
844         mutex_lock(&tz->lock);
845         mutex_lock(&cdev->lock);
846         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
847                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
848                         list_del(&pos->tz_node);
849                         list_del(&pos->cdev_node);
850                         mutex_unlock(&cdev->lock);
851                         mutex_unlock(&tz->lock);
852                         goto unbind;
853                 }
854         }
855         mutex_unlock(&cdev->lock);
856         mutex_unlock(&tz->lock);
857
858         return -ENODEV;
859
860 unbind:
861         device_remove_file(&tz->device, &pos->attr);
862         sysfs_remove_link(&tz->device.kobj, pos->name);
863         release_idr(&tz->idr, &tz->lock, pos->id);
864         kfree(pos);
865         return 0;
866 }
867 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
868
869 static void thermal_release(struct device *dev)
870 {
871         struct thermal_zone_device *tz;
872         struct thermal_cooling_device *cdev;
873
874         if (!strncmp(dev_name(dev), "thermal_zone",
875                      sizeof("thermal_zone") - 1)) {
876                 tz = to_thermal_zone(dev);
877                 kfree(tz);
878         } else {
879                 cdev = to_cooling_device(dev);
880                 kfree(cdev);
881         }
882 }
883
884 static struct class thermal_class = {
885         .name = "thermal",
886         .dev_release = thermal_release,
887 };
888
889 /**
890  * thermal_cooling_device_register - register a new thermal cooling device
891  * @type:       the thermal cooling device type.
892  * @devdata:    device private data.
893  * @ops:                standard thermal cooling devices callbacks.
894  */
895 struct thermal_cooling_device *
896 thermal_cooling_device_register(char *type, void *devdata,
897                                 const struct thermal_cooling_device_ops *ops)
898 {
899         struct thermal_cooling_device *cdev;
900         struct thermal_zone_device *pos;
901         int result;
902
903         if (strlen(type) >= THERMAL_NAME_LENGTH)
904                 return ERR_PTR(-EINVAL);
905
906         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
907             !ops->set_cur_state)
908                 return ERR_PTR(-EINVAL);
909
910         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
911         if (!cdev)
912                 return ERR_PTR(-ENOMEM);
913
914         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
915         if (result) {
916                 kfree(cdev);
917                 return ERR_PTR(result);
918         }
919
920         strcpy(cdev->type, type);
921         mutex_init(&cdev->lock);
922         INIT_LIST_HEAD(&cdev->thermal_instances);
923         cdev->ops = ops;
924         cdev->updated = true;
925         cdev->device.class = &thermal_class;
926         cdev->devdata = devdata;
927         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
928         result = device_register(&cdev->device);
929         if (result) {
930                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
931                 kfree(cdev);
932                 return ERR_PTR(result);
933         }
934
935         /* sys I/F */
936         if (type) {
937                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
938                 if (result)
939                         goto unregister;
940         }
941
942         result = device_create_file(&cdev->device, &dev_attr_max_state);
943         if (result)
944                 goto unregister;
945
946         result = device_create_file(&cdev->device, &dev_attr_cur_state);
947         if (result)
948                 goto unregister;
949
950         mutex_lock(&thermal_list_lock);
951         list_add(&cdev->node, &thermal_cdev_list);
952         list_for_each_entry(pos, &thermal_tz_list, node) {
953                 if (!pos->ops->bind)
954                         continue;
955                 result = pos->ops->bind(pos, cdev);
956                 if (result)
957                         break;
958
959         }
960         mutex_unlock(&thermal_list_lock);
961
962         if (!result)
963                 return cdev;
964
965 unregister:
966         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
967         device_unregister(&cdev->device);
968         return ERR_PTR(result);
969 }
970 EXPORT_SYMBOL(thermal_cooling_device_register);
971
972 /**
973  * thermal_cooling_device_unregister - removes the registered thermal cooling device
974  * @cdev:       the thermal cooling device to remove.
975  *
976  * thermal_cooling_device_unregister() must be called when the device is no
977  * longer needed.
978  */
979 void thermal_cooling_device_unregister(struct
980                                        thermal_cooling_device
981                                        *cdev)
982 {
983         struct thermal_zone_device *tz;
984         struct thermal_cooling_device *pos = NULL;
985
986         if (!cdev)
987                 return;
988
989         mutex_lock(&thermal_list_lock);
990         list_for_each_entry(pos, &thermal_cdev_list, node)
991             if (pos == cdev)
992                 break;
993         if (pos != cdev) {
994                 /* thermal cooling device not found */
995                 mutex_unlock(&thermal_list_lock);
996                 return;
997         }
998         list_del(&cdev->node);
999         list_for_each_entry(tz, &thermal_tz_list, node) {
1000                 if (!tz->ops->unbind)
1001                         continue;
1002                 tz->ops->unbind(tz, cdev);
1003         }
1004         mutex_unlock(&thermal_list_lock);
1005         if (cdev->type[0])
1006                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1007         device_remove_file(&cdev->device, &dev_attr_max_state);
1008         device_remove_file(&cdev->device, &dev_attr_cur_state);
1009
1010         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1011         device_unregister(&cdev->device);
1012         return;
1013 }
1014 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1015
1016 static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
1017 {
1018         struct thermal_instance *instance;
1019         unsigned long target = 0;
1020
1021         /* cooling device is updated*/
1022         if (cdev->updated)
1023                 return;
1024
1025         mutex_lock(&cdev->lock);
1026         /* Make sure cdev enters the deepest cooling state */
1027         list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1028                 if (instance->target == THERMAL_NO_TARGET)
1029                         continue;
1030                 if (instance->target > target)
1031                         target = instance->target;
1032         }
1033         mutex_unlock(&cdev->lock);
1034         cdev->ops->set_cur_state(cdev, target);
1035         cdev->updated = true;
1036 }
1037
1038 static void thermal_zone_do_update(struct thermal_zone_device *tz)
1039 {
1040         struct thermal_instance *instance;
1041
1042         list_for_each_entry(instance, &tz->thermal_instances, tz_node)
1043                 thermal_cdev_do_update(instance->cdev);
1044 }
1045
1046 /*
1047  * Cooling algorithm for both active and passive cooling
1048  *
1049  * 1. if the temperature is higher than a trip point,
1050  *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
1051  *       state for this trip point
1052  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
1053  *       state for this trip point
1054  *
1055  * 2. if the temperature is lower than a trip point, use lower
1056  *    cooling state for this trip point
1057  *
1058  * Note that this behaves the same as the previous passive cooling
1059  * algorithm.
1060  */
1061
1062 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
1063                                      int trip, long temp)
1064 {
1065         struct thermal_instance *instance;
1066         struct thermal_cooling_device *cdev = NULL;
1067         unsigned long cur_state, max_state;
1068         long trip_temp;
1069         enum thermal_trip_type trip_type;
1070         enum thermal_trend trend;
1071
1072         if (trip == THERMAL_TRIPS_NONE) {
1073                 trip_temp = tz->forced_passive;
1074                 trip_type = THERMAL_TRIPS_NONE;
1075         } else {
1076                 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1077                 tz->ops->get_trip_type(tz, trip, &trip_type);
1078         }
1079
1080         if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1081                 /*
1082                  * compare the current temperature and previous temperature
1083                  * to get the thermal trend, if no special requirement
1084                  */
1085                 if (tz->temperature > tz->last_temperature)
1086                         trend = THERMAL_TREND_RAISING;
1087                 else if (tz->temperature < tz->last_temperature)
1088                         trend = THERMAL_TREND_DROPPING;
1089                 else
1090                         trend = THERMAL_TREND_STABLE;
1091         }
1092
1093         if (temp >= trip_temp) {
1094                 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1095                         if (instance->trip != trip)
1096                                 continue;
1097
1098                         cdev = instance->cdev;
1099
1100                         cdev->ops->get_cur_state(cdev, &cur_state);
1101                         cdev->ops->get_max_state(cdev, &max_state);
1102
1103                         if (trend == THERMAL_TREND_RAISING) {
1104                                 cur_state = cur_state < instance->upper ?
1105                                             (cur_state + 1) : instance->upper;
1106                         } else if (trend == THERMAL_TREND_DROPPING) {
1107                                 cur_state = cur_state > instance->lower ?
1108                                     (cur_state - 1) : instance->lower;
1109                         }
1110
1111                         /* activate a passive thermal instance */
1112                         if ((trip_type == THERMAL_TRIP_PASSIVE ||
1113                              trip_type == THERMAL_TRIPS_NONE) &&
1114                              instance->target == THERMAL_NO_TARGET)
1115                                 tz->passive++;
1116
1117                         instance->target = cur_state;
1118                         cdev->updated = false; /* cooling device needs update */
1119                 }
1120         } else {        /* below trip */
1121                 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
1122                         if (instance->trip != trip)
1123                                 continue;
1124
1125                         /* Do not use the inactive thermal instance */
1126                         if (instance->target == THERMAL_NO_TARGET)
1127                                 continue;
1128                         cdev = instance->cdev;
1129                         cdev->ops->get_cur_state(cdev, &cur_state);
1130
1131                         cur_state = cur_state > instance->lower ?
1132                                     (cur_state - 1) : THERMAL_NO_TARGET;
1133
1134                         /* deactivate a passive thermal instance */
1135                         if ((trip_type == THERMAL_TRIP_PASSIVE ||
1136                              trip_type == THERMAL_TRIPS_NONE) &&
1137                              cur_state == THERMAL_NO_TARGET)
1138                                 tz->passive--;
1139                         instance->target = cur_state;
1140                         cdev->updated = false; /* cooling device needs update */
1141                 }
1142         }
1143
1144         return;
1145 }
1146 /**
1147  * thermal_zone_device_update - force an update of a thermal zone's state
1148  * @ttz:        the thermal zone to update
1149  */
1150
1151 void thermal_zone_device_update(struct thermal_zone_device *tz)
1152 {
1153         int count, ret = 0;
1154         long temp, trip_temp;
1155         enum thermal_trip_type trip_type;
1156
1157         mutex_lock(&tz->lock);
1158
1159         if (tz->ops->get_temp(tz, &temp)) {
1160                 /* get_temp failed - retry it later */
1161                 pr_warn("failed to read out thermal zone %d\n", tz->id);
1162                 goto leave;
1163         }
1164
1165         tz->last_temperature = tz->temperature;
1166         tz->temperature = temp;
1167
1168         for (count = 0; count < tz->trips; count++) {
1169                 tz->ops->get_trip_type(tz, count, &trip_type);
1170                 tz->ops->get_trip_temp(tz, count, &trip_temp);
1171
1172                 switch (trip_type) {
1173                 case THERMAL_TRIP_CRITICAL:
1174                         if (temp >= trip_temp) {
1175                                 if (tz->ops->notify)
1176                                         ret = tz->ops->notify(tz, count,
1177                                                               trip_type);
1178                                 if (!ret) {
1179                                         pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1180                                                  temp/1000);
1181                                         orderly_poweroff(true);
1182                                 }
1183                         }
1184                         break;
1185                 case THERMAL_TRIP_HOT:
1186                         if (temp >= trip_temp)
1187                                 if (tz->ops->notify)
1188                                         tz->ops->notify(tz, count, trip_type);
1189                         break;
1190                 case THERMAL_TRIP_ACTIVE:
1191                         thermal_zone_trip_update(tz, count, temp);
1192                         break;
1193                 case THERMAL_TRIP_PASSIVE:
1194                         if (temp >= trip_temp || tz->passive)
1195                                 thermal_zone_trip_update(tz, count, temp);
1196                         break;
1197                 }
1198         }
1199
1200         if (tz->forced_passive)
1201                 thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
1202         thermal_zone_do_update(tz);
1203
1204 leave:
1205         if (tz->passive)
1206                 thermal_zone_device_set_polling(tz, tz->passive_delay);
1207         else if (tz->polling_delay)
1208                 thermal_zone_device_set_polling(tz, tz->polling_delay);
1209         else
1210                 thermal_zone_device_set_polling(tz, 0);
1211         mutex_unlock(&tz->lock);
1212 }
1213 EXPORT_SYMBOL(thermal_zone_device_update);
1214
1215 struct thermal_zone_device *thermal_zone_device_find(void *data,
1216         int (*match)(struct thermal_zone_device *, void *))
1217 {
1218         struct thermal_zone_device *thz;
1219
1220         mutex_lock(&thermal_list_lock);
1221         list_for_each_entry(thz, &thermal_tz_list, node)
1222                 if (match(thz, data)) {
1223                         mutex_unlock(&thermal_list_lock);
1224                         return thz;
1225                 }
1226
1227         mutex_unlock(&thermal_list_lock);
1228         return NULL;
1229 }
1230 EXPORT_SYMBOL(thermal_zone_device_find);
1231
1232 /**
1233  * create_trip_attrs - create attributes for trip points
1234  * @tz:         the thermal zone device
1235  * @mask:       Writeable trip point bitmap.
1236  */
1237 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1238 {
1239         int indx;
1240         int size = sizeof(struct thermal_attr) * tz->trips;
1241
1242         tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1243         if (!tz->trip_type_attrs)
1244                 return -ENOMEM;
1245
1246         tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1247         if (!tz->trip_temp_attrs) {
1248                 kfree(tz->trip_type_attrs);
1249                 return -ENOMEM;
1250         }
1251
1252         if (tz->ops->get_trip_hyst) {
1253                 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1254                 if (!tz->trip_hyst_attrs) {
1255                         kfree(tz->trip_type_attrs);
1256                         kfree(tz->trip_temp_attrs);
1257                         return -ENOMEM;
1258                 }
1259         }
1260
1261
1262         for (indx = 0; indx < tz->trips; indx++) {
1263                 /* create trip type attribute */
1264                 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1265                          "trip_point_%d_type", indx);
1266
1267                 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1268                 tz->trip_type_attrs[indx].attr.attr.name =
1269                                                 tz->trip_type_attrs[indx].name;
1270                 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1271                 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1272
1273                 device_create_file(&tz->device,
1274                                    &tz->trip_type_attrs[indx].attr);
1275
1276                 /* create trip temp attribute */
1277                 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1278                          "trip_point_%d_temp", indx);
1279
1280                 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1281                 tz->trip_temp_attrs[indx].attr.attr.name =
1282                                                 tz->trip_temp_attrs[indx].name;
1283                 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1284                 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1285                 if (mask & (1 << indx)) {
1286                         tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1287                         tz->trip_temp_attrs[indx].attr.store =
1288                                                         trip_point_temp_store;
1289                 }
1290
1291                 device_create_file(&tz->device,
1292                                    &tz->trip_temp_attrs[indx].attr);
1293
1294                 /* create Optional trip hyst attribute */
1295                 if (!tz->ops->get_trip_hyst)
1296                         continue;
1297                 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1298                          "trip_point_%d_hyst", indx);
1299
1300                 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1301                 tz->trip_hyst_attrs[indx].attr.attr.name =
1302                                         tz->trip_hyst_attrs[indx].name;
1303                 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1304                 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1305                 if (tz->ops->set_trip_hyst) {
1306                         tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1307                         tz->trip_hyst_attrs[indx].attr.store =
1308                                         trip_point_hyst_store;
1309                 }
1310
1311                 device_create_file(&tz->device,
1312                                    &tz->trip_hyst_attrs[indx].attr);
1313         }
1314         return 0;
1315 }
1316
1317 static void remove_trip_attrs(struct thermal_zone_device *tz)
1318 {
1319         int indx;
1320
1321         for (indx = 0; indx < tz->trips; indx++) {
1322                 device_remove_file(&tz->device,
1323                                    &tz->trip_type_attrs[indx].attr);
1324                 device_remove_file(&tz->device,
1325                                    &tz->trip_temp_attrs[indx].attr);
1326                 if (tz->ops->get_trip_hyst)
1327                         device_remove_file(&tz->device,
1328                                   &tz->trip_hyst_attrs[indx].attr);
1329         }
1330         kfree(tz->trip_type_attrs);
1331         kfree(tz->trip_temp_attrs);
1332         kfree(tz->trip_hyst_attrs);
1333 }
1334
1335 /**
1336  * thermal_zone_device_register - register a new thermal zone device
1337  * @type:       the thermal zone device type
1338  * @trips:      the number of trip points the thermal zone support
1339  * @mask:       a bit string indicating the writeablility of trip points
1340  * @devdata:    private device data
1341  * @ops:        standard thermal zone device callbacks
1342  * @passive_delay: number of milliseconds to wait between polls when
1343  *                 performing passive cooling
1344  * @polling_delay: number of milliseconds to wait between polls when checking
1345  *                 whether trip points have been crossed (0 for interrupt
1346  *                 driven systems)
1347  *
1348  * thermal_zone_device_unregister() must be called when the device is no
1349  * longer needed. The passive cooling depends on the .get_trend() return value.
1350  */
1351 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1352         int trips, int mask, void *devdata,
1353         const struct thermal_zone_device_ops *ops,
1354         int passive_delay, int polling_delay)
1355 {
1356         struct thermal_zone_device *tz;
1357         struct thermal_cooling_device *pos;
1358         enum thermal_trip_type trip_type;
1359         int result;
1360         int count;
1361         int passive = 0;
1362
1363         if (strlen(type) >= THERMAL_NAME_LENGTH)
1364                 return ERR_PTR(-EINVAL);
1365
1366         if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1367                 return ERR_PTR(-EINVAL);
1368
1369         if (!ops || !ops->get_temp)
1370                 return ERR_PTR(-EINVAL);
1371
1372         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1373         if (!tz)
1374                 return ERR_PTR(-ENOMEM);
1375
1376         INIT_LIST_HEAD(&tz->thermal_instances);
1377         idr_init(&tz->idr);
1378         mutex_init(&tz->lock);
1379         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1380         if (result) {
1381                 kfree(tz);
1382                 return ERR_PTR(result);
1383         }
1384
1385         strcpy(tz->type, type);
1386         tz->ops = ops;
1387         tz->device.class = &thermal_class;
1388         tz->devdata = devdata;
1389         tz->trips = trips;
1390         tz->passive_delay = passive_delay;
1391         tz->polling_delay = polling_delay;
1392
1393         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1394         result = device_register(&tz->device);
1395         if (result) {
1396                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1397                 kfree(tz);
1398                 return ERR_PTR(result);
1399         }
1400
1401         /* sys I/F */
1402         if (type) {
1403                 result = device_create_file(&tz->device, &dev_attr_type);
1404                 if (result)
1405                         goto unregister;
1406         }
1407
1408         result = device_create_file(&tz->device, &dev_attr_temp);
1409         if (result)
1410                 goto unregister;
1411
1412         if (ops->get_mode) {
1413                 result = device_create_file(&tz->device, &dev_attr_mode);
1414                 if (result)
1415                         goto unregister;
1416         }
1417
1418         result = create_trip_attrs(tz, mask);
1419         if (result)
1420                 goto unregister;
1421
1422         for (count = 0; count < trips; count++) {
1423                 tz->ops->get_trip_type(tz, count, &trip_type);
1424                 if (trip_type == THERMAL_TRIP_PASSIVE)
1425                         passive = 1;
1426         }
1427
1428         if (!passive)
1429                 result = device_create_file(&tz->device,
1430                                             &dev_attr_passive);
1431
1432         if (result)
1433                 goto unregister;
1434
1435         result = thermal_add_hwmon_sysfs(tz);
1436         if (result)
1437                 goto unregister;
1438
1439         mutex_lock(&thermal_list_lock);
1440         list_add_tail(&tz->node, &thermal_tz_list);
1441         if (ops->bind)
1442                 list_for_each_entry(pos, &thermal_cdev_list, node) {
1443                 result = ops->bind(tz, pos);
1444                 if (result)
1445                         break;
1446                 }
1447         mutex_unlock(&thermal_list_lock);
1448
1449         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1450
1451         thermal_zone_device_update(tz);
1452
1453         if (!result)
1454                 return tz;
1455
1456 unregister:
1457         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1458         device_unregister(&tz->device);
1459         return ERR_PTR(result);
1460 }
1461 EXPORT_SYMBOL(thermal_zone_device_register);
1462
1463 /**
1464  * thermal_device_unregister - removes the registered thermal zone device
1465  * @tz: the thermal zone device to remove
1466  */
1467 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1468 {
1469         struct thermal_cooling_device *cdev;
1470         struct thermal_zone_device *pos = NULL;
1471
1472         if (!tz)
1473                 return;
1474
1475         mutex_lock(&thermal_list_lock);
1476         list_for_each_entry(pos, &thermal_tz_list, node)
1477             if (pos == tz)
1478                 break;
1479         if (pos != tz) {
1480                 /* thermal zone device not found */
1481                 mutex_unlock(&thermal_list_lock);
1482                 return;
1483         }
1484         list_del(&tz->node);
1485         if (tz->ops->unbind)
1486                 list_for_each_entry(cdev, &thermal_cdev_list, node)
1487                     tz->ops->unbind(tz, cdev);
1488         mutex_unlock(&thermal_list_lock);
1489
1490         thermal_zone_device_set_polling(tz, 0);
1491
1492         if (tz->type[0])
1493                 device_remove_file(&tz->device, &dev_attr_type);
1494         device_remove_file(&tz->device, &dev_attr_temp);
1495         if (tz->ops->get_mode)
1496                 device_remove_file(&tz->device, &dev_attr_mode);
1497         remove_trip_attrs(tz);
1498
1499         thermal_remove_hwmon_sysfs(tz);
1500         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1501         idr_destroy(&tz->idr);
1502         mutex_destroy(&tz->lock);
1503         device_unregister(&tz->device);
1504         return;
1505 }
1506 EXPORT_SYMBOL(thermal_zone_device_unregister);
1507
1508 #ifdef CONFIG_NET
1509 static struct genl_family thermal_event_genl_family = {
1510         .id = GENL_ID_GENERATE,
1511         .name = THERMAL_GENL_FAMILY_NAME,
1512         .version = THERMAL_GENL_VERSION,
1513         .maxattr = THERMAL_GENL_ATTR_MAX,
1514 };
1515
1516 static struct genl_multicast_group thermal_event_mcgrp = {
1517         .name = THERMAL_GENL_MCAST_GROUP_NAME,
1518 };
1519
1520 int thermal_generate_netlink_event(u32 orig, enum events event)
1521 {
1522         struct sk_buff *skb;
1523         struct nlattr *attr;
1524         struct thermal_genl_event *thermal_event;
1525         void *msg_header;
1526         int size;
1527         int result;
1528         static unsigned int thermal_event_seqnum;
1529
1530         /* allocate memory */
1531         size = nla_total_size(sizeof(struct thermal_genl_event)) +
1532                nla_total_size(0);
1533
1534         skb = genlmsg_new(size, GFP_ATOMIC);
1535         if (!skb)
1536                 return -ENOMEM;
1537
1538         /* add the genetlink message header */
1539         msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1540                                  &thermal_event_genl_family, 0,
1541                                  THERMAL_GENL_CMD_EVENT);
1542         if (!msg_header) {
1543                 nlmsg_free(skb);
1544                 return -ENOMEM;
1545         }
1546
1547         /* fill the data */
1548         attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1549                            sizeof(struct thermal_genl_event));
1550
1551         if (!attr) {
1552                 nlmsg_free(skb);
1553                 return -EINVAL;
1554         }
1555
1556         thermal_event = nla_data(attr);
1557         if (!thermal_event) {
1558                 nlmsg_free(skb);
1559                 return -EINVAL;
1560         }
1561
1562         memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1563
1564         thermal_event->orig = orig;
1565         thermal_event->event = event;
1566
1567         /* send multicast genetlink message */
1568         result = genlmsg_end(skb, msg_header);
1569         if (result < 0) {
1570                 nlmsg_free(skb);
1571                 return result;
1572         }
1573
1574         result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1575         if (result)
1576                 pr_info("failed to send netlink event:%d\n", result);
1577
1578         return result;
1579 }
1580 EXPORT_SYMBOL(thermal_generate_netlink_event);
1581
1582 static int genetlink_init(void)
1583 {
1584         int result;
1585
1586         result = genl_register_family(&thermal_event_genl_family);
1587         if (result)
1588                 return result;
1589
1590         result = genl_register_mc_group(&thermal_event_genl_family,
1591                                         &thermal_event_mcgrp);
1592         if (result)
1593                 genl_unregister_family(&thermal_event_genl_family);
1594         return result;
1595 }
1596
1597 static void genetlink_exit(void)
1598 {
1599         genl_unregister_family(&thermal_event_genl_family);
1600 }
1601 #else /* !CONFIG_NET */
1602 static inline int genetlink_init(void) { return 0; }
1603 static inline void genetlink_exit(void) {}
1604 #endif /* !CONFIG_NET */
1605
1606 static int __init thermal_init(void)
1607 {
1608         int result = 0;
1609
1610         result = class_register(&thermal_class);
1611         if (result) {
1612                 idr_destroy(&thermal_tz_idr);
1613                 idr_destroy(&thermal_cdev_idr);
1614                 mutex_destroy(&thermal_idr_lock);
1615                 mutex_destroy(&thermal_list_lock);
1616         }
1617         result = genetlink_init();
1618         return result;
1619 }
1620
1621 static void __exit thermal_exit(void)
1622 {
1623         class_unregister(&thermal_class);
1624         idr_destroy(&thermal_tz_idr);
1625         idr_destroy(&thermal_cdev_idr);
1626         mutex_destroy(&thermal_idr_lock);
1627         mutex_destroy(&thermal_list_lock);
1628         genetlink_exit();
1629 }
1630
1631 fs_initcall(thermal_init);
1632 module_exit(thermal_exit);