thermal_sys.c 36 KB

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