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