thermal_sys.c 36 KB

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