thermal_sys.c 36 KB

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