thermal_sys.c 35 KB

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