thermal_sys.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 struct thermal_hwmon_device *
  390. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  391. {
  392. struct thermal_hwmon_device *hwmon;
  393. mutex_lock(&thermal_list_lock);
  394. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  395. if (!strcmp(hwmon->type, tz->type)) {
  396. mutex_unlock(&thermal_list_lock);
  397. return hwmon;
  398. }
  399. mutex_unlock(&thermal_list_lock);
  400. return NULL;
  401. }
  402. static int
  403. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  404. {
  405. struct thermal_hwmon_device *hwmon;
  406. int new_hwmon_device = 1;
  407. int result;
  408. hwmon = thermal_hwmon_lookup_by_type(tz);
  409. if (hwmon) {
  410. new_hwmon_device = 0;
  411. goto register_sys_interface;
  412. }
  413. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  414. if (!hwmon)
  415. return -ENOMEM;
  416. INIT_LIST_HEAD(&hwmon->tz_list);
  417. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  418. hwmon->device = hwmon_device_register(NULL);
  419. if (IS_ERR(hwmon->device)) {
  420. result = PTR_ERR(hwmon->device);
  421. goto free_mem;
  422. }
  423. dev_set_drvdata(hwmon->device, hwmon);
  424. result = device_create_file(hwmon->device, &dev_attr_name);
  425. if (result)
  426. goto free_mem;
  427. register_sys_interface:
  428. tz->hwmon = hwmon;
  429. hwmon->count++;
  430. snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
  431. "temp%d_input", hwmon->count);
  432. tz->temp_input.attr.attr.name = tz->temp_input.name;
  433. tz->temp_input.attr.attr.mode = 0444;
  434. tz->temp_input.attr.show = temp_input_show;
  435. sysfs_attr_init(&tz->temp_input.attr.attr);
  436. result = device_create_file(hwmon->device, &tz->temp_input.attr);
  437. if (result)
  438. goto unregister_name;
  439. if (tz->ops->get_crit_temp) {
  440. unsigned long temperature;
  441. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  442. snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
  443. "temp%d_crit", hwmon->count);
  444. tz->temp_crit.attr.attr.name = tz->temp_crit.name;
  445. tz->temp_crit.attr.attr.mode = 0444;
  446. tz->temp_crit.attr.show = temp_crit_show;
  447. sysfs_attr_init(&tz->temp_crit.attr.attr);
  448. result = device_create_file(hwmon->device,
  449. &tz->temp_crit.attr);
  450. if (result)
  451. goto unregister_input;
  452. }
  453. }
  454. mutex_lock(&thermal_list_lock);
  455. if (new_hwmon_device)
  456. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  457. list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
  458. mutex_unlock(&thermal_list_lock);
  459. return 0;
  460. unregister_input:
  461. device_remove_file(hwmon->device, &tz->temp_input.attr);
  462. unregister_name:
  463. if (new_hwmon_device) {
  464. device_remove_file(hwmon->device, &dev_attr_name);
  465. hwmon_device_unregister(hwmon->device);
  466. }
  467. free_mem:
  468. if (new_hwmon_device)
  469. kfree(hwmon);
  470. return result;
  471. }
  472. static void
  473. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  474. {
  475. struct thermal_hwmon_device *hwmon = tz->hwmon;
  476. tz->hwmon = NULL;
  477. device_remove_file(hwmon->device, &tz->temp_input.attr);
  478. if (tz->ops->get_crit_temp)
  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(
  715. char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
  716. {
  717. struct thermal_cooling_device *cdev;
  718. struct thermal_zone_device *pos;
  719. int result;
  720. if (strlen(type) >= THERMAL_NAME_LENGTH)
  721. return ERR_PTR(-EINVAL);
  722. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  723. !ops->set_cur_state)
  724. return ERR_PTR(-EINVAL);
  725. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  726. if (!cdev)
  727. return ERR_PTR(-ENOMEM);
  728. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  729. if (result) {
  730. kfree(cdev);
  731. return ERR_PTR(result);
  732. }
  733. strcpy(cdev->type, type);
  734. cdev->ops = ops;
  735. cdev->device.class = &thermal_class;
  736. cdev->devdata = devdata;
  737. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  738. result = device_register(&cdev->device);
  739. if (result) {
  740. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  741. kfree(cdev);
  742. return ERR_PTR(result);
  743. }
  744. /* sys I/F */
  745. if (type) {
  746. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  747. if (result)
  748. goto unregister;
  749. }
  750. result = device_create_file(&cdev->device, &dev_attr_max_state);
  751. if (result)
  752. goto unregister;
  753. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  754. if (result)
  755. goto unregister;
  756. mutex_lock(&thermal_list_lock);
  757. list_add(&cdev->node, &thermal_cdev_list);
  758. list_for_each_entry(pos, &thermal_tz_list, node) {
  759. if (!pos->ops->bind)
  760. continue;
  761. result = pos->ops->bind(pos, cdev);
  762. if (result)
  763. break;
  764. }
  765. mutex_unlock(&thermal_list_lock);
  766. if (!result)
  767. return cdev;
  768. unregister:
  769. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  770. device_unregister(&cdev->device);
  771. return ERR_PTR(result);
  772. }
  773. EXPORT_SYMBOL(thermal_cooling_device_register);
  774. /**
  775. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  776. * @cdev: the thermal cooling device to remove.
  777. *
  778. * thermal_cooling_device_unregister() must be called when the device is no
  779. * longer needed.
  780. */
  781. void thermal_cooling_device_unregister(struct
  782. thermal_cooling_device
  783. *cdev)
  784. {
  785. struct thermal_zone_device *tz;
  786. struct thermal_cooling_device *pos = NULL;
  787. if (!cdev)
  788. return;
  789. mutex_lock(&thermal_list_lock);
  790. list_for_each_entry(pos, &thermal_cdev_list, node)
  791. if (pos == cdev)
  792. break;
  793. if (pos != cdev) {
  794. /* thermal cooling device not found */
  795. mutex_unlock(&thermal_list_lock);
  796. return;
  797. }
  798. list_del(&cdev->node);
  799. list_for_each_entry(tz, &thermal_tz_list, node) {
  800. if (!tz->ops->unbind)
  801. continue;
  802. tz->ops->unbind(tz, cdev);
  803. }
  804. mutex_unlock(&thermal_list_lock);
  805. if (cdev->type[0])
  806. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  807. device_remove_file(&cdev->device, &dev_attr_max_state);
  808. device_remove_file(&cdev->device, &dev_attr_cur_state);
  809. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  810. device_unregister(&cdev->device);
  811. return;
  812. }
  813. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  814. /**
  815. * thermal_zone_device_update - force an update of a thermal zone's state
  816. * @ttz: the thermal zone to update
  817. */
  818. void thermal_zone_device_update(struct thermal_zone_device *tz)
  819. {
  820. int count, ret = 0;
  821. long temp, trip_temp;
  822. enum thermal_trip_type trip_type;
  823. struct thermal_cooling_device_instance *instance;
  824. struct thermal_cooling_device *cdev;
  825. mutex_lock(&tz->lock);
  826. if (tz->ops->get_temp(tz, &temp)) {
  827. /* get_temp failed - retry it later */
  828. printk(KERN_WARNING PREFIX "failed to read out thermal zone "
  829. "%d\n", tz->id);
  830. goto leave;
  831. }
  832. for (count = 0; count < tz->trips; count++) {
  833. tz->ops->get_trip_type(tz, count, &trip_type);
  834. tz->ops->get_trip_temp(tz, count, &trip_temp);
  835. switch (trip_type) {
  836. case THERMAL_TRIP_CRITICAL:
  837. if (temp >= trip_temp) {
  838. if (tz->ops->notify)
  839. ret = tz->ops->notify(tz, count,
  840. trip_type);
  841. if (!ret) {
  842. printk(KERN_EMERG
  843. "Critical temperature reached (%ld C), shutting down.\n",
  844. temp/1000);
  845. orderly_poweroff(true);
  846. }
  847. }
  848. break;
  849. case THERMAL_TRIP_HOT:
  850. if (temp >= trip_temp)
  851. if (tz->ops->notify)
  852. tz->ops->notify(tz, count, trip_type);
  853. break;
  854. case THERMAL_TRIP_ACTIVE:
  855. list_for_each_entry(instance, &tz->cooling_devices,
  856. node) {
  857. if (instance->trip != count)
  858. continue;
  859. cdev = instance->cdev;
  860. if (temp >= trip_temp)
  861. cdev->ops->set_cur_state(cdev, 1);
  862. else
  863. cdev->ops->set_cur_state(cdev, 0);
  864. }
  865. break;
  866. case THERMAL_TRIP_PASSIVE:
  867. if (temp >= trip_temp || tz->passive)
  868. thermal_zone_device_passive(tz, temp,
  869. trip_temp, count);
  870. break;
  871. }
  872. }
  873. if (tz->forced_passive)
  874. thermal_zone_device_passive(tz, temp, tz->forced_passive,
  875. THERMAL_TRIPS_NONE);
  876. tz->last_temperature = temp;
  877. leave:
  878. if (tz->passive)
  879. thermal_zone_device_set_polling(tz, tz->passive_delay);
  880. else if (tz->polling_delay)
  881. thermal_zone_device_set_polling(tz, tz->polling_delay);
  882. else
  883. thermal_zone_device_set_polling(tz, 0);
  884. mutex_unlock(&tz->lock);
  885. }
  886. EXPORT_SYMBOL(thermal_zone_device_update);
  887. /**
  888. * thermal_zone_device_register - register a new thermal zone device
  889. * @type: the thermal zone device type
  890. * @trips: the number of trip points the thermal zone support
  891. * @devdata: private device data
  892. * @ops: standard thermal zone device callbacks
  893. * @tc1: thermal coefficient 1 for passive calculations
  894. * @tc2: thermal coefficient 2 for passive calculations
  895. * @passive_delay: number of milliseconds to wait between polls when
  896. * performing passive cooling
  897. * @polling_delay: number of milliseconds to wait between polls when checking
  898. * whether trip points have been crossed (0 for interrupt
  899. * driven systems)
  900. *
  901. * thermal_zone_device_unregister() must be called when the device is no
  902. * longer needed. The passive cooling formula uses tc1 and tc2 as described in
  903. * section 11.1.5.1 of the ACPI specification 3.0.
  904. */
  905. struct thermal_zone_device *thermal_zone_device_register(char *type,
  906. int trips, void *devdata,
  907. const struct thermal_zone_device_ops *ops,
  908. int tc1, int tc2, int passive_delay, int polling_delay)
  909. {
  910. struct thermal_zone_device *tz;
  911. struct thermal_cooling_device *pos;
  912. enum thermal_trip_type trip_type;
  913. int result;
  914. int count;
  915. int passive = 0;
  916. if (strlen(type) >= THERMAL_NAME_LENGTH)
  917. return ERR_PTR(-EINVAL);
  918. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  919. return ERR_PTR(-EINVAL);
  920. if (!ops || !ops->get_temp)
  921. return ERR_PTR(-EINVAL);
  922. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  923. if (!tz)
  924. return ERR_PTR(-ENOMEM);
  925. INIT_LIST_HEAD(&tz->cooling_devices);
  926. idr_init(&tz->idr);
  927. mutex_init(&tz->lock);
  928. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  929. if (result) {
  930. kfree(tz);
  931. return ERR_PTR(result);
  932. }
  933. strcpy(tz->type, type);
  934. tz->ops = ops;
  935. tz->device.class = &thermal_class;
  936. tz->devdata = devdata;
  937. tz->trips = trips;
  938. tz->tc1 = tc1;
  939. tz->tc2 = tc2;
  940. tz->passive_delay = passive_delay;
  941. tz->polling_delay = polling_delay;
  942. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  943. result = device_register(&tz->device);
  944. if (result) {
  945. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  946. kfree(tz);
  947. return ERR_PTR(result);
  948. }
  949. /* sys I/F */
  950. if (type) {
  951. result = device_create_file(&tz->device, &dev_attr_type);
  952. if (result)
  953. goto unregister;
  954. }
  955. result = device_create_file(&tz->device, &dev_attr_temp);
  956. if (result)
  957. goto unregister;
  958. if (ops->get_mode) {
  959. result = device_create_file(&tz->device, &dev_attr_mode);
  960. if (result)
  961. goto unregister;
  962. }
  963. for (count = 0; count < trips; count++) {
  964. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  965. if (result)
  966. goto unregister;
  967. tz->ops->get_trip_type(tz, count, &trip_type);
  968. if (trip_type == THERMAL_TRIP_PASSIVE)
  969. passive = 1;
  970. }
  971. if (!passive)
  972. result = device_create_file(&tz->device,
  973. &dev_attr_passive);
  974. if (result)
  975. goto unregister;
  976. result = thermal_add_hwmon_sysfs(tz);
  977. if (result)
  978. goto unregister;
  979. mutex_lock(&thermal_list_lock);
  980. list_add_tail(&tz->node, &thermal_tz_list);
  981. if (ops->bind)
  982. list_for_each_entry(pos, &thermal_cdev_list, node) {
  983. result = ops->bind(tz, pos);
  984. if (result)
  985. break;
  986. }
  987. mutex_unlock(&thermal_list_lock);
  988. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  989. thermal_zone_device_update(tz);
  990. if (!result)
  991. return tz;
  992. unregister:
  993. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  994. device_unregister(&tz->device);
  995. return ERR_PTR(result);
  996. }
  997. EXPORT_SYMBOL(thermal_zone_device_register);
  998. /**
  999. * thermal_device_unregister - removes the registered thermal zone device
  1000. * @tz: the thermal zone device to remove
  1001. */
  1002. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1003. {
  1004. struct thermal_cooling_device *cdev;
  1005. struct thermal_zone_device *pos = NULL;
  1006. int count;
  1007. if (!tz)
  1008. return;
  1009. mutex_lock(&thermal_list_lock);
  1010. list_for_each_entry(pos, &thermal_tz_list, node)
  1011. if (pos == tz)
  1012. break;
  1013. if (pos != tz) {
  1014. /* thermal zone device not found */
  1015. mutex_unlock(&thermal_list_lock);
  1016. return;
  1017. }
  1018. list_del(&tz->node);
  1019. if (tz->ops->unbind)
  1020. list_for_each_entry(cdev, &thermal_cdev_list, node)
  1021. tz->ops->unbind(tz, cdev);
  1022. mutex_unlock(&thermal_list_lock);
  1023. thermal_zone_device_set_polling(tz, 0);
  1024. if (tz->type[0])
  1025. device_remove_file(&tz->device, &dev_attr_type);
  1026. device_remove_file(&tz->device, &dev_attr_temp);
  1027. if (tz->ops->get_mode)
  1028. device_remove_file(&tz->device, &dev_attr_mode);
  1029. for (count = 0; count < tz->trips; count++)
  1030. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  1031. thermal_remove_hwmon_sysfs(tz);
  1032. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1033. idr_destroy(&tz->idr);
  1034. mutex_destroy(&tz->lock);
  1035. device_unregister(&tz->device);
  1036. return;
  1037. }
  1038. EXPORT_SYMBOL(thermal_zone_device_unregister);
  1039. #ifdef CONFIG_NET
  1040. static struct genl_family thermal_event_genl_family = {
  1041. .id = GENL_ID_GENERATE,
  1042. .name = THERMAL_GENL_FAMILY_NAME,
  1043. .version = THERMAL_GENL_VERSION,
  1044. .maxattr = THERMAL_GENL_ATTR_MAX,
  1045. };
  1046. static struct genl_multicast_group thermal_event_mcgrp = {
  1047. .name = THERMAL_GENL_MCAST_GROUP_NAME,
  1048. };
  1049. int generate_netlink_event(u32 orig, enum events event)
  1050. {
  1051. struct sk_buff *skb;
  1052. struct nlattr *attr;
  1053. struct thermal_genl_event *thermal_event;
  1054. void *msg_header;
  1055. int size;
  1056. int result;
  1057. /* allocate memory */
  1058. size = nla_total_size(sizeof(struct thermal_genl_event)) + \
  1059. nla_total_size(0);
  1060. skb = genlmsg_new(size, GFP_ATOMIC);
  1061. if (!skb)
  1062. return -ENOMEM;
  1063. /* add the genetlink message header */
  1064. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1065. &thermal_event_genl_family, 0,
  1066. THERMAL_GENL_CMD_EVENT);
  1067. if (!msg_header) {
  1068. nlmsg_free(skb);
  1069. return -ENOMEM;
  1070. }
  1071. /* fill the data */
  1072. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \
  1073. sizeof(struct thermal_genl_event));
  1074. if (!attr) {
  1075. nlmsg_free(skb);
  1076. return -EINVAL;
  1077. }
  1078. thermal_event = nla_data(attr);
  1079. if (!thermal_event) {
  1080. nlmsg_free(skb);
  1081. return -EINVAL;
  1082. }
  1083. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1084. thermal_event->orig = orig;
  1085. thermal_event->event = event;
  1086. /* send multicast genetlink message */
  1087. result = genlmsg_end(skb, msg_header);
  1088. if (result < 0) {
  1089. nlmsg_free(skb);
  1090. return result;
  1091. }
  1092. result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
  1093. if (result)
  1094. printk(KERN_INFO "failed to send netlink event:%d", result);
  1095. return result;
  1096. }
  1097. EXPORT_SYMBOL(generate_netlink_event);
  1098. static int genetlink_init(void)
  1099. {
  1100. int result;
  1101. result = genl_register_family(&thermal_event_genl_family);
  1102. if (result)
  1103. return result;
  1104. result = genl_register_mc_group(&thermal_event_genl_family,
  1105. &thermal_event_mcgrp);
  1106. if (result)
  1107. genl_unregister_family(&thermal_event_genl_family);
  1108. return result;
  1109. }
  1110. static void genetlink_exit(void)
  1111. {
  1112. genl_unregister_family(&thermal_event_genl_family);
  1113. }
  1114. #else /* !CONFIG_NET */
  1115. static inline int genetlink_init(void) { return 0; }
  1116. static inline void genetlink_exit(void) {}
  1117. #endif /* !CONFIG_NET */
  1118. static int __init thermal_init(void)
  1119. {
  1120. int result = 0;
  1121. result = class_register(&thermal_class);
  1122. if (result) {
  1123. idr_destroy(&thermal_tz_idr);
  1124. idr_destroy(&thermal_cdev_idr);
  1125. mutex_destroy(&thermal_idr_lock);
  1126. mutex_destroy(&thermal_list_lock);
  1127. }
  1128. result = genetlink_init();
  1129. return result;
  1130. }
  1131. static void __exit thermal_exit(void)
  1132. {
  1133. class_unregister(&thermal_class);
  1134. idr_destroy(&thermal_tz_idr);
  1135. idr_destroy(&thermal_cdev_idr);
  1136. mutex_destroy(&thermal_idr_lock);
  1137. mutex_destroy(&thermal_list_lock);
  1138. genetlink_exit();
  1139. }
  1140. fs_initcall(thermal_init);
  1141. module_exit(thermal_exit);