2 * thermal.c - Generic Thermal Management Sysfs support.
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/reboot.h>
36 #include <net/netlink.h>
37 #include <net/genetlink.h>
39 #include "thermal_core.h"
41 MODULE_AUTHOR("Zhang Rui");
42 MODULE_DESCRIPTION("Generic thermal management sysfs support");
43 MODULE_LICENSE("GPL");
45 static DEFINE_IDR(thermal_tz_idr);
46 static DEFINE_IDR(thermal_cdev_idr);
47 static DEFINE_MUTEX(thermal_idr_lock);
49 static LIST_HEAD(thermal_tz_list);
50 static LIST_HEAD(thermal_cdev_list);
51 static LIST_HEAD(thermal_governor_list);
53 static DEFINE_MUTEX(thermal_list_lock);
54 static DEFINE_MUTEX(thermal_governor_lock);
56 static struct thermal_governor *__find_governor(const char *name)
58 struct thermal_governor *pos;
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
67 int thermal_register_governor(struct thermal_governor *governor)
71 struct thermal_zone_device *pos;
76 mutex_lock(&thermal_governor_lock);
79 if (__find_governor(governor->name) == NULL) {
81 list_add(&governor->governor_list, &thermal_governor_list);
84 mutex_lock(&thermal_list_lock);
86 list_for_each_entry(pos, &thermal_tz_list, node) {
90 name = pos->tzp->governor_name;
92 name = DEFAULT_THERMAL_GOVERNOR;
93 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
94 pos->governor = governor;
97 mutex_unlock(&thermal_list_lock);
98 mutex_unlock(&thermal_governor_lock);
102 EXPORT_SYMBOL_GPL(thermal_register_governor);
104 void thermal_unregister_governor(struct thermal_governor *governor)
106 struct thermal_zone_device *pos;
111 mutex_lock(&thermal_governor_lock);
113 if (__find_governor(governor->name) == NULL)
116 mutex_lock(&thermal_list_lock);
118 list_for_each_entry(pos, &thermal_tz_list, node) {
119 if (!strnicmp(pos->governor->name, governor->name,
120 THERMAL_NAME_LENGTH))
121 pos->governor = NULL;
124 mutex_unlock(&thermal_list_lock);
125 list_del(&governor->governor_list);
127 mutex_unlock(&thermal_governor_lock);
130 EXPORT_SYMBOL_GPL(thermal_unregister_governor);
132 static int get_idr(struct idr *idr, struct mutex *lock, int *id)
138 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
141 if (unlikely(ret < 0))
147 static void release_idr(struct idr *idr, struct mutex *lock, int id)
156 int get_tz_trend(struct thermal_zone_device *tz, int trip)
158 enum thermal_trend trend;
160 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
161 if (tz->temperature > tz->last_temperature)
162 trend = THERMAL_TREND_RAISING;
163 else if (tz->temperature < tz->last_temperature)
164 trend = THERMAL_TREND_DROPPING;
166 trend = THERMAL_TREND_STABLE;
171 EXPORT_SYMBOL(get_tz_trend);
173 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
174 struct thermal_cooling_device *cdev, int trip)
176 struct thermal_instance *pos = NULL;
177 struct thermal_instance *target_instance = NULL;
179 mutex_lock(&tz->lock);
180 mutex_lock(&cdev->lock);
182 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
183 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
184 target_instance = pos;
189 mutex_unlock(&cdev->lock);
190 mutex_unlock(&tz->lock);
192 return target_instance;
194 EXPORT_SYMBOL(get_thermal_instance);
196 static void print_bind_err_msg(struct thermal_zone_device *tz,
197 struct thermal_cooling_device *cdev, int ret)
199 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
200 tz->type, cdev->type, ret);
203 static void __bind(struct thermal_zone_device *tz, int mask,
204 struct thermal_cooling_device *cdev)
208 for (i = 0; i < tz->trips; i++) {
209 if (mask & (1 << i)) {
210 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
211 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
213 print_bind_err_msg(tz, cdev, ret);
218 static void __unbind(struct thermal_zone_device *tz, int mask,
219 struct thermal_cooling_device *cdev)
223 for (i = 0; i < tz->trips; i++)
225 thermal_zone_unbind_cooling_device(tz, i, cdev);
228 static void bind_cdev(struct thermal_cooling_device *cdev)
231 const struct thermal_zone_params *tzp;
232 struct thermal_zone_device *pos = NULL;
234 mutex_lock(&thermal_list_lock);
236 list_for_each_entry(pos, &thermal_tz_list, node) {
237 if (!pos->tzp && !pos->ops->bind)
240 if (!pos->tzp && pos->ops->bind) {
241 ret = pos->ops->bind(pos, cdev);
243 print_bind_err_msg(pos, cdev, ret);
247 if (!tzp || !tzp->tbp)
250 for (i = 0; i < tzp->num_tbps; i++) {
251 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
253 if (tzp->tbp[i].match(pos, cdev))
255 tzp->tbp[i].cdev = cdev;
256 __bind(pos, tzp->tbp[i].trip_mask, cdev);
260 mutex_unlock(&thermal_list_lock);
263 static void bind_tz(struct thermal_zone_device *tz)
266 struct thermal_cooling_device *pos = NULL;
267 const struct thermal_zone_params *tzp = tz->tzp;
269 if (!tzp && !tz->ops->bind)
272 mutex_lock(&thermal_list_lock);
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp && tz->ops->bind) {
276 list_for_each_entry(pos, &thermal_cdev_list, node) {
277 ret = tz->ops->bind(tz, pos);
279 print_bind_err_msg(tz, pos, ret);
284 if (!tzp || !tzp->tbp)
287 list_for_each_entry(pos, &thermal_cdev_list, node) {
288 for (i = 0; i < tzp->num_tbps; i++) {
289 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
291 if (tzp->tbp[i].match(tz, pos))
293 tzp->tbp[i].cdev = pos;
294 __bind(tz, tzp->tbp[i].trip_mask, pos);
298 mutex_unlock(&thermal_list_lock);
301 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
305 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
306 round_jiffies(msecs_to_jiffies(delay)));
308 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
309 msecs_to_jiffies(delay));
311 cancel_delayed_work(&tz->poll_queue);
314 static void monitor_thermal_zone(struct thermal_zone_device *tz)
316 mutex_lock(&tz->lock);
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
323 thermal_zone_device_set_polling(tz, 0);
325 mutex_unlock(&tz->lock);
328 static void handle_non_critical_trips(struct thermal_zone_device *tz,
329 int trip, enum thermal_trip_type trip_type)
332 tz->governor->throttle(tz, trip);
335 static void handle_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
340 tz->ops->get_trip_temp(tz, trip, &trip_temp);
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz->temperature < trip_temp)
347 tz->ops->notify(tz, trip, trip_type);
349 if (trip_type == THERMAL_TRIP_CRITICAL) {
350 dev_emerg(&tz->device,
351 "critical temperature reached(%d C),shutting down\n",
352 tz->temperature / 1000);
353 orderly_poweroff(true);
357 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
359 enum thermal_trip_type type;
361 tz->ops->get_trip_type(tz, trip, &type);
363 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
364 handle_critical_trips(tz, trip, type);
366 handle_non_critical_trips(tz, trip, type);
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
371 monitor_thermal_zone(tz);
374 static int thermal_zone_get_temp(struct thermal_zone_device *tz,
378 #ifdef CONFIG_THERMAL_EMULATION
380 unsigned long crit_temp = -1UL;
381 enum thermal_trip_type type;
384 mutex_lock(&tz->lock);
386 ret = tz->ops->get_temp(tz, temp);
387 #ifdef CONFIG_THERMAL_EMULATION
388 if (!tz->emul_temperature)
391 for (count = 0; count < tz->trips; count++) {
392 ret = tz->ops->get_trip_type(tz, count, &type);
393 if (!ret && type == THERMAL_TRIP_CRITICAL) {
394 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
402 if (*temp < crit_temp)
403 *temp = tz->emul_temperature;
406 mutex_unlock(&tz->lock);
410 static void update_temperature(struct thermal_zone_device *tz)
415 ret = thermal_zone_get_temp(tz, &temp);
417 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
422 mutex_lock(&tz->lock);
423 tz->last_temperature = tz->temperature;
424 tz->temperature = temp;
425 mutex_unlock(&tz->lock);
428 void thermal_zone_device_update(struct thermal_zone_device *tz)
432 update_temperature(tz);
434 for (count = 0; count < tz->trips; count++)
435 handle_thermal_trip(tz, count);
437 EXPORT_SYMBOL(thermal_zone_device_update);
439 static void thermal_zone_device_check(struct work_struct *work)
441 struct thermal_zone_device *tz = container_of(work, struct
444 thermal_zone_device_update(tz);
447 /* sys I/F for thermal zone */
449 #define to_thermal_zone(_dev) \
450 container_of(_dev, struct thermal_zone_device, device)
453 type_show(struct device *dev, struct device_attribute *attr, char *buf)
455 struct thermal_zone_device *tz = to_thermal_zone(dev);
457 return sprintf(buf, "%s\n", tz->type);
461 temp_show(struct device *dev, struct device_attribute *attr, char *buf)
463 struct thermal_zone_device *tz = to_thermal_zone(dev);
467 ret = thermal_zone_get_temp(tz, &temperature);
472 return sprintf(buf, "%ld\n", temperature);
476 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
478 struct thermal_zone_device *tz = to_thermal_zone(dev);
479 enum thermal_device_mode mode;
482 if (!tz->ops->get_mode)
485 result = tz->ops->get_mode(tz, &mode);
489 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
494 mode_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
497 struct thermal_zone_device *tz = to_thermal_zone(dev);
500 if (!tz->ops->set_mode)
503 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
504 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
505 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
506 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
517 trip_point_type_show(struct device *dev, struct device_attribute *attr,
520 struct thermal_zone_device *tz = to_thermal_zone(dev);
521 enum thermal_trip_type type;
524 if (!tz->ops->get_trip_type)
527 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
530 result = tz->ops->get_trip_type(tz, trip, &type);
535 case THERMAL_TRIP_CRITICAL:
536 return sprintf(buf, "critical\n");
537 case THERMAL_TRIP_HOT:
538 return sprintf(buf, "hot\n");
539 case THERMAL_TRIP_PASSIVE:
540 return sprintf(buf, "passive\n");
541 case THERMAL_TRIP_ACTIVE:
542 return sprintf(buf, "active\n");
544 return sprintf(buf, "unknown\n");
549 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct thermal_zone_device *tz = to_thermal_zone(dev);
554 unsigned long temperature;
556 if (!tz->ops->set_trip_temp)
559 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
562 if (kstrtoul(buf, 10, &temperature))
565 ret = tz->ops->set_trip_temp(tz, trip, temperature);
567 return ret ? ret : count;
571 trip_point_temp_show(struct device *dev, struct device_attribute *attr,
574 struct thermal_zone_device *tz = to_thermal_zone(dev);
578 if (!tz->ops->get_trip_temp)
581 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
584 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
589 return sprintf(buf, "%ld\n", temperature);
593 trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
596 struct thermal_zone_device *tz = to_thermal_zone(dev);
598 unsigned long temperature;
600 if (!tz->ops->set_trip_hyst)
603 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
606 if (kstrtoul(buf, 10, &temperature))
610 * We are not doing any check on the 'temperature' value
611 * here. The driver implementing 'set_trip_hyst' has to
614 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
616 return ret ? ret : count;
620 trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
625 unsigned long temperature;
627 if (!tz->ops->get_trip_hyst)
630 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
633 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
635 return ret ? ret : sprintf(buf, "%ld\n", temperature);
639 passive_store(struct device *dev, struct device_attribute *attr,
640 const char *buf, size_t count)
642 struct thermal_zone_device *tz = to_thermal_zone(dev);
643 struct thermal_cooling_device *cdev = NULL;
646 if (!sscanf(buf, "%d\n", &state))
649 /* sanity check: values below 1000 millicelcius don't make sense
650 * and can cause the system to go into a thermal heart attack
652 if (state && state < 1000)
655 if (state && !tz->forced_passive) {
656 mutex_lock(&thermal_list_lock);
657 list_for_each_entry(cdev, &thermal_cdev_list, node) {
658 if (!strncmp("Processor", cdev->type,
659 sizeof("Processor")))
660 thermal_zone_bind_cooling_device(tz,
661 THERMAL_TRIPS_NONE, cdev,
665 mutex_unlock(&thermal_list_lock);
666 if (!tz->passive_delay)
667 tz->passive_delay = 1000;
668 } else if (!state && tz->forced_passive) {
669 mutex_lock(&thermal_list_lock);
670 list_for_each_entry(cdev, &thermal_cdev_list, node) {
671 if (!strncmp("Processor", cdev->type,
672 sizeof("Processor")))
673 thermal_zone_unbind_cooling_device(tz,
677 mutex_unlock(&thermal_list_lock);
678 tz->passive_delay = 0;
681 tz->forced_passive = state;
683 thermal_zone_device_update(tz);
689 passive_show(struct device *dev, struct device_attribute *attr,
692 struct thermal_zone_device *tz = to_thermal_zone(dev);
694 return sprintf(buf, "%d\n", tz->forced_passive);
698 policy_store(struct device *dev, struct device_attribute *attr,
699 const char *buf, size_t count)
702 struct thermal_zone_device *tz = to_thermal_zone(dev);
703 struct thermal_governor *gov;
705 mutex_lock(&thermal_governor_lock);
707 gov = __find_governor(buf);
715 mutex_unlock(&thermal_governor_lock);
720 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
722 struct thermal_zone_device *tz = to_thermal_zone(dev);
724 return sprintf(buf, "%s\n", tz->governor->name);
727 #ifdef CONFIG_THERMAL_EMULATION
729 emul_temp_store(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
732 struct thermal_zone_device *tz = to_thermal_zone(dev);
734 unsigned long temperature;
736 if (kstrtoul(buf, 10, &temperature))
739 if (!tz->ops->set_emul_temp) {
740 mutex_lock(&tz->lock);
741 tz->emul_temperature = temperature;
742 mutex_unlock(&tz->lock);
744 ret = tz->ops->set_emul_temp(tz, temperature);
747 return ret ? ret : count;
749 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
750 #endif/*CONFIG_THERMAL_EMULATION*/
752 static DEVICE_ATTR(type, 0444, type_show, NULL);
753 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
754 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
755 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
756 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
758 /* sys I/F for cooling device */
759 #define to_cooling_device(_dev) \
760 container_of(_dev, struct thermal_cooling_device, device)
763 thermal_cooling_device_type_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
766 struct thermal_cooling_device *cdev = to_cooling_device(dev);
768 return sprintf(buf, "%s\n", cdev->type);
772 thermal_cooling_device_max_state_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
775 struct thermal_cooling_device *cdev = to_cooling_device(dev);
779 ret = cdev->ops->get_max_state(cdev, &state);
782 return sprintf(buf, "%ld\n", state);
786 thermal_cooling_device_cur_state_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
789 struct thermal_cooling_device *cdev = to_cooling_device(dev);
793 ret = cdev->ops->get_cur_state(cdev, &state);
796 return sprintf(buf, "%ld\n", state);
800 thermal_cooling_device_cur_state_store(struct device *dev,
801 struct device_attribute *attr,
802 const char *buf, size_t count)
804 struct thermal_cooling_device *cdev = to_cooling_device(dev);
808 if (!sscanf(buf, "%ld\n", &state))
814 result = cdev->ops->set_cur_state(cdev, state);
820 static struct device_attribute dev_attr_cdev_type =
821 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
822 static DEVICE_ATTR(max_state, 0444,
823 thermal_cooling_device_max_state_show, NULL);
824 static DEVICE_ATTR(cur_state, 0644,
825 thermal_cooling_device_cur_state_show,
826 thermal_cooling_device_cur_state_store);
829 thermal_cooling_device_trip_point_show(struct device *dev,
830 struct device_attribute *attr, char *buf)
832 struct thermal_instance *instance;
835 container_of(attr, struct thermal_instance, attr);
837 if (instance->trip == THERMAL_TRIPS_NONE)
838 return sprintf(buf, "-1\n");
840 return sprintf(buf, "%d\n", instance->trip);
843 /* Device management */
845 #if defined(CONFIG_THERMAL_HWMON)
848 #include <linux/hwmon.h>
850 /* thermal zone devices with the same type share one hwmon device */
851 struct thermal_hwmon_device {
852 char type[THERMAL_NAME_LENGTH];
853 struct device *device;
855 struct list_head tz_list;
856 struct list_head node;
859 struct thermal_hwmon_attr {
860 struct device_attribute attr;
864 /* one temperature input for each thermal zone */
865 struct thermal_hwmon_temp {
866 struct list_head hwmon_node;
867 struct thermal_zone_device *tz;
868 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
869 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
872 static LIST_HEAD(thermal_hwmon_list);
875 name_show(struct device *dev, struct device_attribute *attr, char *buf)
877 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
878 return sprintf(buf, "%s\n", hwmon->type);
880 static DEVICE_ATTR(name, 0444, name_show, NULL);
883 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
887 struct thermal_hwmon_attr *hwmon_attr
888 = container_of(attr, struct thermal_hwmon_attr, attr);
889 struct thermal_hwmon_temp *temp
890 = container_of(hwmon_attr, struct thermal_hwmon_temp,
892 struct thermal_zone_device *tz = temp->tz;
894 ret = thermal_zone_get_temp(tz, &temperature);
899 return sprintf(buf, "%ld\n", temperature);
903 temp_crit_show(struct device *dev, struct device_attribute *attr,
906 struct thermal_hwmon_attr *hwmon_attr
907 = container_of(attr, struct thermal_hwmon_attr, attr);
908 struct thermal_hwmon_temp *temp
909 = container_of(hwmon_attr, struct thermal_hwmon_temp,
911 struct thermal_zone_device *tz = temp->tz;
915 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
919 return sprintf(buf, "%ld\n", temperature);
923 static struct thermal_hwmon_device *
924 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
926 struct thermal_hwmon_device *hwmon;
928 mutex_lock(&thermal_list_lock);
929 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
930 if (!strcmp(hwmon->type, tz->type)) {
931 mutex_unlock(&thermal_list_lock);
934 mutex_unlock(&thermal_list_lock);
939 /* Find the temperature input matching a given thermal zone */
940 static struct thermal_hwmon_temp *
941 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
942 const struct thermal_zone_device *tz)
944 struct thermal_hwmon_temp *temp;
946 mutex_lock(&thermal_list_lock);
947 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
948 if (temp->tz == tz) {
949 mutex_unlock(&thermal_list_lock);
952 mutex_unlock(&thermal_list_lock);
958 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
960 struct thermal_hwmon_device *hwmon;
961 struct thermal_hwmon_temp *temp;
962 int new_hwmon_device = 1;
965 hwmon = thermal_hwmon_lookup_by_type(tz);
967 new_hwmon_device = 0;
968 goto register_sys_interface;
971 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
975 INIT_LIST_HEAD(&hwmon->tz_list);
976 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
977 hwmon->device = hwmon_device_register(NULL);
978 if (IS_ERR(hwmon->device)) {
979 result = PTR_ERR(hwmon->device);
982 dev_set_drvdata(hwmon->device, hwmon);
983 result = device_create_file(hwmon->device, &dev_attr_name);
987 register_sys_interface:
988 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
991 goto unregister_name;
997 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
998 "temp%d_input", hwmon->count);
999 temp->temp_input.attr.attr.name = temp->temp_input.name;
1000 temp->temp_input.attr.attr.mode = 0444;
1001 temp->temp_input.attr.show = temp_input_show;
1002 sysfs_attr_init(&temp->temp_input.attr.attr);
1003 result = device_create_file(hwmon->device, &temp->temp_input.attr);
1007 if (tz->ops->get_crit_temp) {
1008 unsigned long temperature;
1009 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1010 snprintf(temp->temp_crit.name,
1011 sizeof(temp->temp_crit.name),
1012 "temp%d_crit", hwmon->count);
1013 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1014 temp->temp_crit.attr.attr.mode = 0444;
1015 temp->temp_crit.attr.show = temp_crit_show;
1016 sysfs_attr_init(&temp->temp_crit.attr.attr);
1017 result = device_create_file(hwmon->device,
1018 &temp->temp_crit.attr);
1020 goto unregister_input;
1024 mutex_lock(&thermal_list_lock);
1025 if (new_hwmon_device)
1026 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1027 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1028 mutex_unlock(&thermal_list_lock);
1033 device_remove_file(hwmon->device, &temp->temp_input.attr);
1037 if (new_hwmon_device) {
1038 device_remove_file(hwmon->device, &dev_attr_name);
1039 hwmon_device_unregister(hwmon->device);
1042 if (new_hwmon_device)
1049 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1051 struct thermal_hwmon_device *hwmon;
1052 struct thermal_hwmon_temp *temp;
1054 hwmon = thermal_hwmon_lookup_by_type(tz);
1055 if (unlikely(!hwmon)) {
1056 /* Should never happen... */
1057 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1061 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1062 if (unlikely(!temp)) {
1063 /* Should never happen... */
1064 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1068 device_remove_file(hwmon->device, &temp->temp_input.attr);
1069 if (tz->ops->get_crit_temp)
1070 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1072 mutex_lock(&thermal_list_lock);
1073 list_del(&temp->hwmon_node);
1075 if (!list_empty(&hwmon->tz_list)) {
1076 mutex_unlock(&thermal_list_lock);
1079 list_del(&hwmon->node);
1080 mutex_unlock(&thermal_list_lock);
1082 device_remove_file(hwmon->device, &dev_attr_name);
1083 hwmon_device_unregister(hwmon->device);
1088 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1094 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1100 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
1101 * @tz: thermal zone device
1102 * @trip: indicates which trip point the cooling devices is
1103 * associated with in this thermal zone.
1104 * @cdev: thermal cooling device
1106 * This function is usually called in the thermal zone device .bind callback.
1108 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1110 struct thermal_cooling_device *cdev,
1111 unsigned long upper, unsigned long lower)
1113 struct thermal_instance *dev;
1114 struct thermal_instance *pos;
1115 struct thermal_zone_device *pos1;
1116 struct thermal_cooling_device *pos2;
1117 unsigned long max_state;
1120 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1123 list_for_each_entry(pos1, &thermal_tz_list, node) {
1127 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1132 if (tz != pos1 || cdev != pos2)
1135 cdev->ops->get_max_state(cdev, &max_state);
1137 /* lower default 0, upper default max_state */
1138 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1139 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1141 if (lower > upper || upper > max_state)
1145 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1153 dev->target = THERMAL_NO_TARGET;
1155 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1159 sprintf(dev->name, "cdev%d", dev->id);
1161 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1165 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1166 sysfs_attr_init(&dev->attr.attr);
1167 dev->attr.attr.name = dev->attr_name;
1168 dev->attr.attr.mode = 0444;
1169 dev->attr.show = thermal_cooling_device_trip_point_show;
1170 result = device_create_file(&tz->device, &dev->attr);
1172 goto remove_symbol_link;
1174 mutex_lock(&tz->lock);
1175 mutex_lock(&cdev->lock);
1176 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1177 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1182 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1183 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1185 mutex_unlock(&cdev->lock);
1186 mutex_unlock(&tz->lock);
1191 device_remove_file(&tz->device, &dev->attr);
1193 sysfs_remove_link(&tz->device.kobj, dev->name);
1195 release_idr(&tz->idr, &tz->lock, dev->id);
1200 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1203 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1204 * @tz: thermal zone device
1205 * @trip: indicates which trip point the cooling devices is
1206 * associated with in this thermal zone.
1207 * @cdev: thermal cooling device
1209 * This function is usually called in the thermal zone device .unbind callback.
1211 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1213 struct thermal_cooling_device *cdev)
1215 struct thermal_instance *pos, *next;
1217 mutex_lock(&tz->lock);
1218 mutex_lock(&cdev->lock);
1219 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1220 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1221 list_del(&pos->tz_node);
1222 list_del(&pos->cdev_node);
1223 mutex_unlock(&cdev->lock);
1224 mutex_unlock(&tz->lock);
1228 mutex_unlock(&cdev->lock);
1229 mutex_unlock(&tz->lock);
1234 device_remove_file(&tz->device, &pos->attr);
1235 sysfs_remove_link(&tz->device.kobj, pos->name);
1236 release_idr(&tz->idr, &tz->lock, pos->id);
1240 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1242 static void thermal_release(struct device *dev)
1244 struct thermal_zone_device *tz;
1245 struct thermal_cooling_device *cdev;
1247 if (!strncmp(dev_name(dev), "thermal_zone",
1248 sizeof("thermal_zone") - 1)) {
1249 tz = to_thermal_zone(dev);
1252 cdev = to_cooling_device(dev);
1257 static struct class thermal_class = {
1259 .dev_release = thermal_release,
1263 * thermal_cooling_device_register - register a new thermal cooling device
1264 * @type: the thermal cooling device type.
1265 * @devdata: device private data.
1266 * @ops: standard thermal cooling devices callbacks.
1268 struct thermal_cooling_device *
1269 thermal_cooling_device_register(char *type, void *devdata,
1270 const struct thermal_cooling_device_ops *ops)
1272 struct thermal_cooling_device *cdev;
1275 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1276 return ERR_PTR(-EINVAL);
1278 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1279 !ops->set_cur_state)
1280 return ERR_PTR(-EINVAL);
1282 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1284 return ERR_PTR(-ENOMEM);
1286 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1289 return ERR_PTR(result);
1292 strcpy(cdev->type, type ? : "");
1293 mutex_init(&cdev->lock);
1294 INIT_LIST_HEAD(&cdev->thermal_instances);
1296 cdev->updated = true;
1297 cdev->device.class = &thermal_class;
1298 cdev->devdata = devdata;
1299 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1300 result = device_register(&cdev->device);
1302 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1304 return ERR_PTR(result);
1309 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1314 result = device_create_file(&cdev->device, &dev_attr_max_state);
1318 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1322 /* Add 'this' new cdev to the global cdev list */
1323 mutex_lock(&thermal_list_lock);
1324 list_add(&cdev->node, &thermal_cdev_list);
1325 mutex_unlock(&thermal_list_lock);
1327 /* Update binding information for 'this' new cdev */
1333 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1334 device_unregister(&cdev->device);
1335 return ERR_PTR(result);
1337 EXPORT_SYMBOL(thermal_cooling_device_register);
1340 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1341 * @cdev: the thermal cooling device to remove.
1343 * thermal_cooling_device_unregister() must be called when the device is no
1346 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1349 const struct thermal_zone_params *tzp;
1350 struct thermal_zone_device *tz;
1351 struct thermal_cooling_device *pos = NULL;
1356 mutex_lock(&thermal_list_lock);
1357 list_for_each_entry(pos, &thermal_cdev_list, node)
1361 /* thermal cooling device not found */
1362 mutex_unlock(&thermal_list_lock);
1365 list_del(&cdev->node);
1367 /* Unbind all thermal zones associated with 'this' cdev */
1368 list_for_each_entry(tz, &thermal_tz_list, node) {
1369 if (tz->ops->unbind) {
1370 tz->ops->unbind(tz, cdev);
1374 if (!tz->tzp || !tz->tzp->tbp)
1378 for (i = 0; i < tzp->num_tbps; i++) {
1379 if (tzp->tbp[i].cdev == cdev) {
1380 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1381 tzp->tbp[i].cdev = NULL;
1386 mutex_unlock(&thermal_list_lock);
1389 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1390 device_remove_file(&cdev->device, &dev_attr_max_state);
1391 device_remove_file(&cdev->device, &dev_attr_cur_state);
1393 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1394 device_unregister(&cdev->device);
1397 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1399 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1401 struct thermal_instance *instance;
1402 unsigned long target = 0;
1404 /* cooling device is updated*/
1408 mutex_lock(&cdev->lock);
1409 /* Make sure cdev enters the deepest cooling state */
1410 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1411 if (instance->target == THERMAL_NO_TARGET)
1413 if (instance->target > target)
1414 target = instance->target;
1416 mutex_unlock(&cdev->lock);
1417 cdev->ops->set_cur_state(cdev, target);
1418 cdev->updated = true;
1420 EXPORT_SYMBOL(thermal_cdev_update);
1423 * notify_thermal_framework - Sensor drivers use this API to notify framework
1424 * @tz: thermal zone device
1425 * @trip: indicates which trip point has been crossed
1427 * This function handles the trip events from sensor drivers. It starts
1428 * throttling the cooling devices according to the policy configured.
1429 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1430 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1431 * The throttling policy is based on the configured platform data; if no
1432 * platform data is provided, this uses the step_wise throttling policy.
1434 void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1436 handle_thermal_trip(tz, trip);
1438 EXPORT_SYMBOL(notify_thermal_framework);
1441 * create_trip_attrs - create attributes for trip points
1442 * @tz: the thermal zone device
1443 * @mask: Writeable trip point bitmap.
1445 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1448 int size = sizeof(struct thermal_attr) * tz->trips;
1450 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1451 if (!tz->trip_type_attrs)
1454 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1455 if (!tz->trip_temp_attrs) {
1456 kfree(tz->trip_type_attrs);
1460 if (tz->ops->get_trip_hyst) {
1461 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1462 if (!tz->trip_hyst_attrs) {
1463 kfree(tz->trip_type_attrs);
1464 kfree(tz->trip_temp_attrs);
1470 for (indx = 0; indx < tz->trips; indx++) {
1471 /* create trip type attribute */
1472 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1473 "trip_point_%d_type", indx);
1475 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1476 tz->trip_type_attrs[indx].attr.attr.name =
1477 tz->trip_type_attrs[indx].name;
1478 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1479 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1481 device_create_file(&tz->device,
1482 &tz->trip_type_attrs[indx].attr);
1484 /* create trip temp attribute */
1485 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1486 "trip_point_%d_temp", indx);
1488 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1489 tz->trip_temp_attrs[indx].attr.attr.name =
1490 tz->trip_temp_attrs[indx].name;
1491 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1492 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1493 if (mask & (1 << indx)) {
1494 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1495 tz->trip_temp_attrs[indx].attr.store =
1496 trip_point_temp_store;
1499 device_create_file(&tz->device,
1500 &tz->trip_temp_attrs[indx].attr);
1502 /* create Optional trip hyst attribute */
1503 if (!tz->ops->get_trip_hyst)
1505 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1506 "trip_point_%d_hyst", indx);
1508 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1509 tz->trip_hyst_attrs[indx].attr.attr.name =
1510 tz->trip_hyst_attrs[indx].name;
1511 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1512 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1513 if (tz->ops->set_trip_hyst) {
1514 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1515 tz->trip_hyst_attrs[indx].attr.store =
1516 trip_point_hyst_store;
1519 device_create_file(&tz->device,
1520 &tz->trip_hyst_attrs[indx].attr);
1525 static void remove_trip_attrs(struct thermal_zone_device *tz)
1529 for (indx = 0; indx < tz->trips; indx++) {
1530 device_remove_file(&tz->device,
1531 &tz->trip_type_attrs[indx].attr);
1532 device_remove_file(&tz->device,
1533 &tz->trip_temp_attrs[indx].attr);
1534 if (tz->ops->get_trip_hyst)
1535 device_remove_file(&tz->device,
1536 &tz->trip_hyst_attrs[indx].attr);
1538 kfree(tz->trip_type_attrs);
1539 kfree(tz->trip_temp_attrs);
1540 kfree(tz->trip_hyst_attrs);
1544 * thermal_zone_device_register - register a new thermal zone device
1545 * @type: the thermal zone device type
1546 * @trips: the number of trip points the thermal zone support
1547 * @mask: a bit string indicating the writeablility of trip points
1548 * @devdata: private device data
1549 * @ops: standard thermal zone device callbacks
1550 * @tzp: thermal zone platform parameters
1551 * @passive_delay: number of milliseconds to wait between polls when
1552 * performing passive cooling
1553 * @polling_delay: number of milliseconds to wait between polls when checking
1554 * whether trip points have been crossed (0 for interrupt
1557 * thermal_zone_device_unregister() must be called when the device is no
1558 * longer needed. The passive cooling depends on the .get_trend() return value.
1560 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1561 int trips, int mask, void *devdata,
1562 const struct thermal_zone_device_ops *ops,
1563 const struct thermal_zone_params *tzp,
1564 int passive_delay, int polling_delay)
1566 struct thermal_zone_device *tz;
1567 enum thermal_trip_type trip_type;
1572 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1573 return ERR_PTR(-EINVAL);
1575 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1576 return ERR_PTR(-EINVAL);
1578 if (!ops || !ops->get_temp)
1579 return ERR_PTR(-EINVAL);
1581 if (trips > 0 && !ops->get_trip_type)
1582 return ERR_PTR(-EINVAL);
1584 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1586 return ERR_PTR(-ENOMEM);
1588 INIT_LIST_HEAD(&tz->thermal_instances);
1590 mutex_init(&tz->lock);
1591 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1594 return ERR_PTR(result);
1597 strcpy(tz->type, type ? : "");
1600 tz->device.class = &thermal_class;
1601 tz->devdata = devdata;
1603 tz->passive_delay = passive_delay;
1604 tz->polling_delay = polling_delay;
1606 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1607 result = device_register(&tz->device);
1609 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1611 return ERR_PTR(result);
1616 result = device_create_file(&tz->device, &dev_attr_type);
1621 result = device_create_file(&tz->device, &dev_attr_temp);
1625 if (ops->get_mode) {
1626 result = device_create_file(&tz->device, &dev_attr_mode);
1631 result = create_trip_attrs(tz, mask);
1635 for (count = 0; count < trips; count++) {
1636 tz->ops->get_trip_type(tz, count, &trip_type);
1637 if (trip_type == THERMAL_TRIP_PASSIVE)
1642 result = device_create_file(&tz->device, &dev_attr_passive);
1647 #ifdef CONFIG_THERMAL_EMULATION
1648 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1652 /* Create policy attribute */
1653 result = device_create_file(&tz->device, &dev_attr_policy);
1657 /* Update 'this' zone's governor information */
1658 mutex_lock(&thermal_governor_lock);
1661 tz->governor = __find_governor(tz->tzp->governor_name);
1663 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1665 mutex_unlock(&thermal_governor_lock);
1667 result = thermal_add_hwmon_sysfs(tz);
1671 mutex_lock(&thermal_list_lock);
1672 list_add_tail(&tz->node, &thermal_tz_list);
1673 mutex_unlock(&thermal_list_lock);
1675 /* Bind cooling devices for this zone */
1678 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1680 thermal_zone_device_update(tz);
1686 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1687 device_unregister(&tz->device);
1688 return ERR_PTR(result);
1690 EXPORT_SYMBOL(thermal_zone_device_register);
1693 * thermal_device_unregister - removes the registered thermal zone device
1694 * @tz: the thermal zone device to remove
1696 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1699 const struct thermal_zone_params *tzp;
1700 struct thermal_cooling_device *cdev;
1701 struct thermal_zone_device *pos = NULL;
1708 mutex_lock(&thermal_list_lock);
1709 list_for_each_entry(pos, &thermal_tz_list, node)
1713 /* thermal zone device not found */
1714 mutex_unlock(&thermal_list_lock);
1717 list_del(&tz->node);
1719 /* Unbind all cdevs associated with 'this' thermal zone */
1720 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1721 if (tz->ops->unbind) {
1722 tz->ops->unbind(tz, cdev);
1726 if (!tzp || !tzp->tbp)
1729 for (i = 0; i < tzp->num_tbps; i++) {
1730 if (tzp->tbp[i].cdev == cdev) {
1731 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1732 tzp->tbp[i].cdev = NULL;
1737 mutex_unlock(&thermal_list_lock);
1739 thermal_zone_device_set_polling(tz, 0);
1742 device_remove_file(&tz->device, &dev_attr_type);
1743 device_remove_file(&tz->device, &dev_attr_temp);
1744 if (tz->ops->get_mode)
1745 device_remove_file(&tz->device, &dev_attr_mode);
1746 device_remove_file(&tz->device, &dev_attr_policy);
1747 remove_trip_attrs(tz);
1748 tz->governor = NULL;
1750 thermal_remove_hwmon_sysfs(tz);
1751 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1752 idr_destroy(&tz->idr);
1753 mutex_destroy(&tz->lock);
1754 device_unregister(&tz->device);
1757 EXPORT_SYMBOL(thermal_zone_device_unregister);
1760 static struct genl_family thermal_event_genl_family = {
1761 .id = GENL_ID_GENERATE,
1762 .name = THERMAL_GENL_FAMILY_NAME,
1763 .version = THERMAL_GENL_VERSION,
1764 .maxattr = THERMAL_GENL_ATTR_MAX,
1767 static struct genl_multicast_group thermal_event_mcgrp = {
1768 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1771 int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1774 struct sk_buff *skb;
1775 struct nlattr *attr;
1776 struct thermal_genl_event *thermal_event;
1780 static unsigned int thermal_event_seqnum;
1785 /* allocate memory */
1786 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1789 skb = genlmsg_new(size, GFP_ATOMIC);
1793 /* add the genetlink message header */
1794 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1795 &thermal_event_genl_family, 0,
1796 THERMAL_GENL_CMD_EVENT);
1803 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1804 sizeof(struct thermal_genl_event));
1811 thermal_event = nla_data(attr);
1812 if (!thermal_event) {
1817 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1819 thermal_event->orig = tz->id;
1820 thermal_event->event = event;
1822 /* send multicast genetlink message */
1823 result = genlmsg_end(skb, msg_header);
1829 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1831 dev_err(&tz->device, "Failed to send netlink event:%d", result);
1835 EXPORT_SYMBOL(thermal_generate_netlink_event);
1837 static int genetlink_init(void)
1841 result = genl_register_family(&thermal_event_genl_family);
1845 result = genl_register_mc_group(&thermal_event_genl_family,
1846 &thermal_event_mcgrp);
1848 genl_unregister_family(&thermal_event_genl_family);
1852 static void genetlink_exit(void)
1854 genl_unregister_family(&thermal_event_genl_family);
1856 #else /* !CONFIG_NET */
1857 static inline int genetlink_init(void) { return 0; }
1858 static inline void genetlink_exit(void) {}
1859 #endif /* !CONFIG_NET */
1861 static int __init thermal_init(void)
1865 result = class_register(&thermal_class);
1867 idr_destroy(&thermal_tz_idr);
1868 idr_destroy(&thermal_cdev_idr);
1869 mutex_destroy(&thermal_idr_lock);
1870 mutex_destroy(&thermal_list_lock);
1873 result = genetlink_init();
1877 static void __exit thermal_exit(void)
1879 class_unregister(&thermal_class);
1880 idr_destroy(&thermal_tz_idr);
1881 idr_destroy(&thermal_cdev_idr);
1882 mutex_destroy(&thermal_idr_lock);
1883 mutex_destroy(&thermal_list_lock);
1887 fs_initcall(thermal_init);
1888 module_exit(thermal_exit);