thermal_sys.c 34 KB

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