thermal_sys.c 34 KB

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