thermal_sys.c 36 KB

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