thermal_sys.c 34 KB

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