thermal_sys.c 34 KB

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