thermal_core.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/idr.h>
  32. #include <linux/thermal.h>
  33. #include <linux/reboot.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include "thermal_core.h"
  37. MODULE_AUTHOR("Zhang Rui");
  38. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  39. MODULE_LICENSE("GPL v2");
  40. static DEFINE_IDR(thermal_tz_idr);
  41. static DEFINE_IDR(thermal_cdev_idr);
  42. static DEFINE_MUTEX(thermal_idr_lock);
  43. static LIST_HEAD(thermal_tz_list);
  44. static LIST_HEAD(thermal_cdev_list);
  45. static LIST_HEAD(thermal_governor_list);
  46. static DEFINE_MUTEX(thermal_list_lock);
  47. static DEFINE_MUTEX(thermal_governor_lock);
  48. static struct thermal_governor *__find_governor(const char *name)
  49. {
  50. struct thermal_governor *pos;
  51. list_for_each_entry(pos, &thermal_governor_list, governor_list)
  52. if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
  53. return pos;
  54. return NULL;
  55. }
  56. int thermal_register_governor(struct thermal_governor *governor)
  57. {
  58. int err;
  59. const char *name;
  60. struct thermal_zone_device *pos;
  61. if (!governor)
  62. return -EINVAL;
  63. mutex_lock(&thermal_governor_lock);
  64. err = -EBUSY;
  65. if (__find_governor(governor->name) == NULL) {
  66. err = 0;
  67. list_add(&governor->governor_list, &thermal_governor_list);
  68. }
  69. mutex_lock(&thermal_list_lock);
  70. list_for_each_entry(pos, &thermal_tz_list, node) {
  71. if (pos->governor)
  72. continue;
  73. if (pos->tzp)
  74. name = pos->tzp->governor_name;
  75. else
  76. name = DEFAULT_THERMAL_GOVERNOR;
  77. if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
  78. pos->governor = governor;
  79. }
  80. mutex_unlock(&thermal_list_lock);
  81. mutex_unlock(&thermal_governor_lock);
  82. return err;
  83. }
  84. void thermal_unregister_governor(struct thermal_governor *governor)
  85. {
  86. struct thermal_zone_device *pos;
  87. if (!governor)
  88. return;
  89. mutex_lock(&thermal_governor_lock);
  90. if (__find_governor(governor->name) == NULL)
  91. goto exit;
  92. mutex_lock(&thermal_list_lock);
  93. list_for_each_entry(pos, &thermal_tz_list, node) {
  94. if (!strnicmp(pos->governor->name, governor->name,
  95. THERMAL_NAME_LENGTH))
  96. pos->governor = NULL;
  97. }
  98. mutex_unlock(&thermal_list_lock);
  99. list_del(&governor->governor_list);
  100. exit:
  101. mutex_unlock(&thermal_governor_lock);
  102. return;
  103. }
  104. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  105. {
  106. int ret;
  107. if (lock)
  108. mutex_lock(lock);
  109. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  110. if (lock)
  111. mutex_unlock(lock);
  112. if (unlikely(ret < 0))
  113. return ret;
  114. *id = ret;
  115. return 0;
  116. }
  117. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  118. {
  119. if (lock)
  120. mutex_lock(lock);
  121. idr_remove(idr, id);
  122. if (lock)
  123. mutex_unlock(lock);
  124. }
  125. int get_tz_trend(struct thermal_zone_device *tz, int trip)
  126. {
  127. enum thermal_trend trend;
  128. if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
  129. if (tz->temperature > tz->last_temperature)
  130. trend = THERMAL_TREND_RAISING;
  131. else if (tz->temperature < tz->last_temperature)
  132. trend = THERMAL_TREND_DROPPING;
  133. else
  134. trend = THERMAL_TREND_STABLE;
  135. }
  136. return trend;
  137. }
  138. EXPORT_SYMBOL(get_tz_trend);
  139. struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
  140. struct thermal_cooling_device *cdev, int trip)
  141. {
  142. struct thermal_instance *pos = NULL;
  143. struct thermal_instance *target_instance = NULL;
  144. mutex_lock(&tz->lock);
  145. mutex_lock(&cdev->lock);
  146. list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
  147. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  148. target_instance = pos;
  149. break;
  150. }
  151. }
  152. mutex_unlock(&cdev->lock);
  153. mutex_unlock(&tz->lock);
  154. return target_instance;
  155. }
  156. EXPORT_SYMBOL(get_thermal_instance);
  157. static void print_bind_err_msg(struct thermal_zone_device *tz,
  158. struct thermal_cooling_device *cdev, int ret)
  159. {
  160. dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
  161. tz->type, cdev->type, ret);
  162. }
  163. static void __bind(struct thermal_zone_device *tz, int mask,
  164. struct thermal_cooling_device *cdev)
  165. {
  166. int i, ret;
  167. for (i = 0; i < tz->trips; i++) {
  168. if (mask & (1 << i)) {
  169. ret = thermal_zone_bind_cooling_device(tz, i, cdev,
  170. THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
  171. if (ret)
  172. print_bind_err_msg(tz, cdev, ret);
  173. }
  174. }
  175. }
  176. static void __unbind(struct thermal_zone_device *tz, int mask,
  177. struct thermal_cooling_device *cdev)
  178. {
  179. int i;
  180. for (i = 0; i < tz->trips; i++)
  181. if (mask & (1 << i))
  182. thermal_zone_unbind_cooling_device(tz, i, cdev);
  183. }
  184. static void bind_cdev(struct thermal_cooling_device *cdev)
  185. {
  186. int i, ret;
  187. const struct thermal_zone_params *tzp;
  188. struct thermal_zone_device *pos = NULL;
  189. mutex_lock(&thermal_list_lock);
  190. list_for_each_entry(pos, &thermal_tz_list, node) {
  191. if (!pos->tzp && !pos->ops->bind)
  192. continue;
  193. if (!pos->tzp && pos->ops->bind) {
  194. ret = pos->ops->bind(pos, cdev);
  195. if (ret)
  196. print_bind_err_msg(pos, cdev, ret);
  197. }
  198. tzp = pos->tzp;
  199. if (!tzp || !tzp->tbp)
  200. continue;
  201. for (i = 0; i < tzp->num_tbps; i++) {
  202. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  203. continue;
  204. if (tzp->tbp[i].match(pos, cdev))
  205. continue;
  206. tzp->tbp[i].cdev = cdev;
  207. __bind(pos, tzp->tbp[i].trip_mask, cdev);
  208. }
  209. }
  210. mutex_unlock(&thermal_list_lock);
  211. }
  212. static void bind_tz(struct thermal_zone_device *tz)
  213. {
  214. int i, ret;
  215. struct thermal_cooling_device *pos = NULL;
  216. const struct thermal_zone_params *tzp = tz->tzp;
  217. if (!tzp && !tz->ops->bind)
  218. return;
  219. mutex_lock(&thermal_list_lock);
  220. /* If there is no platform data, try to use ops->bind */
  221. if (!tzp && tz->ops->bind) {
  222. list_for_each_entry(pos, &thermal_cdev_list, node) {
  223. ret = tz->ops->bind(tz, pos);
  224. if (ret)
  225. print_bind_err_msg(tz, pos, ret);
  226. }
  227. goto exit;
  228. }
  229. if (!tzp || !tzp->tbp)
  230. goto exit;
  231. list_for_each_entry(pos, &thermal_cdev_list, node) {
  232. for (i = 0; i < tzp->num_tbps; i++) {
  233. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  234. continue;
  235. if (tzp->tbp[i].match(tz, pos))
  236. continue;
  237. tzp->tbp[i].cdev = pos;
  238. __bind(tz, tzp->tbp[i].trip_mask, pos);
  239. }
  240. }
  241. exit:
  242. mutex_unlock(&thermal_list_lock);
  243. }
  244. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  245. int delay)
  246. {
  247. if (delay > 1000)
  248. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  249. round_jiffies(msecs_to_jiffies(delay)));
  250. else if (delay)
  251. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  252. msecs_to_jiffies(delay));
  253. else
  254. cancel_delayed_work(&tz->poll_queue);
  255. }
  256. static void monitor_thermal_zone(struct thermal_zone_device *tz)
  257. {
  258. mutex_lock(&tz->lock);
  259. if (tz->passive)
  260. thermal_zone_device_set_polling(tz, tz->passive_delay);
  261. else if (tz->polling_delay)
  262. thermal_zone_device_set_polling(tz, tz->polling_delay);
  263. else
  264. thermal_zone_device_set_polling(tz, 0);
  265. mutex_unlock(&tz->lock);
  266. }
  267. static void handle_non_critical_trips(struct thermal_zone_device *tz,
  268. int trip, enum thermal_trip_type trip_type)
  269. {
  270. if (tz->governor)
  271. tz->governor->throttle(tz, trip);
  272. }
  273. static void handle_critical_trips(struct thermal_zone_device *tz,
  274. int trip, enum thermal_trip_type trip_type)
  275. {
  276. long trip_temp;
  277. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  278. /* If we have not crossed the trip_temp, we do not care. */
  279. if (tz->temperature < trip_temp)
  280. return;
  281. if (tz->ops->notify)
  282. tz->ops->notify(tz, trip, trip_type);
  283. if (trip_type == THERMAL_TRIP_CRITICAL) {
  284. dev_emerg(&tz->device,
  285. "critical temperature reached(%d C),shutting down\n",
  286. tz->temperature / 1000);
  287. orderly_poweroff(true);
  288. }
  289. }
  290. static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
  291. {
  292. enum thermal_trip_type type;
  293. tz->ops->get_trip_type(tz, trip, &type);
  294. if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
  295. handle_critical_trips(tz, trip, type);
  296. else
  297. handle_non_critical_trips(tz, trip, type);
  298. /*
  299. * Alright, we handled this trip successfully.
  300. * So, start monitoring again.
  301. */
  302. monitor_thermal_zone(tz);
  303. }
  304. /**
  305. * thermal_zone_get_temp() - returns its the temperature of thermal zone
  306. * @tz: a valid pointer to a struct thermal_zone_device
  307. * @temp: a valid pointer to where to store the resulting temperature.
  308. *
  309. * When a valid thermal zone reference is passed, it will fetch its
  310. * temperature and fill @temp.
  311. *
  312. * Return: On success returns 0, an error code otherwise
  313. */
  314. int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
  315. {
  316. int ret = -EINVAL;
  317. #ifdef CONFIG_THERMAL_EMULATION
  318. int count;
  319. unsigned long crit_temp = -1UL;
  320. enum thermal_trip_type type;
  321. #endif
  322. if (!tz || IS_ERR(tz))
  323. goto exit;
  324. mutex_lock(&tz->lock);
  325. ret = tz->ops->get_temp(tz, temp);
  326. #ifdef CONFIG_THERMAL_EMULATION
  327. if (!tz->emul_temperature)
  328. goto skip_emul;
  329. for (count = 0; count < tz->trips; count++) {
  330. ret = tz->ops->get_trip_type(tz, count, &type);
  331. if (!ret && type == THERMAL_TRIP_CRITICAL) {
  332. ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
  333. break;
  334. }
  335. }
  336. if (ret)
  337. goto skip_emul;
  338. if (*temp < crit_temp)
  339. *temp = tz->emul_temperature;
  340. skip_emul:
  341. #endif
  342. mutex_unlock(&tz->lock);
  343. exit:
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  347. static void update_temperature(struct thermal_zone_device *tz)
  348. {
  349. long temp;
  350. int ret;
  351. ret = thermal_zone_get_temp(tz, &temp);
  352. if (ret) {
  353. dev_warn(&tz->device, "failed to read out thermal zone %d\n",
  354. tz->id);
  355. return;
  356. }
  357. mutex_lock(&tz->lock);
  358. tz->last_temperature = tz->temperature;
  359. tz->temperature = temp;
  360. mutex_unlock(&tz->lock);
  361. }
  362. void thermal_zone_device_update(struct thermal_zone_device *tz)
  363. {
  364. int count;
  365. update_temperature(tz);
  366. for (count = 0; count < tz->trips; count++)
  367. handle_thermal_trip(tz, count);
  368. }
  369. EXPORT_SYMBOL_GPL(thermal_zone_device_update);
  370. static void thermal_zone_device_check(struct work_struct *work)
  371. {
  372. struct thermal_zone_device *tz = container_of(work, struct
  373. thermal_zone_device,
  374. poll_queue.work);
  375. thermal_zone_device_update(tz);
  376. }
  377. /* sys I/F for thermal zone */
  378. #define to_thermal_zone(_dev) \
  379. container_of(_dev, struct thermal_zone_device, device)
  380. static ssize_t
  381. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  382. {
  383. struct thermal_zone_device *tz = to_thermal_zone(dev);
  384. return sprintf(buf, "%s\n", tz->type);
  385. }
  386. static ssize_t
  387. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  388. {
  389. struct thermal_zone_device *tz = to_thermal_zone(dev);
  390. long temperature;
  391. int ret;
  392. ret = thermal_zone_get_temp(tz, &temperature);
  393. if (ret)
  394. return ret;
  395. return sprintf(buf, "%ld\n", temperature);
  396. }
  397. static ssize_t
  398. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  399. {
  400. struct thermal_zone_device *tz = to_thermal_zone(dev);
  401. enum thermal_device_mode mode;
  402. int result;
  403. if (!tz->ops->get_mode)
  404. return -EPERM;
  405. result = tz->ops->get_mode(tz, &mode);
  406. if (result)
  407. return result;
  408. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  409. : "disabled");
  410. }
  411. static ssize_t
  412. mode_store(struct device *dev, struct device_attribute *attr,
  413. const char *buf, size_t count)
  414. {
  415. struct thermal_zone_device *tz = to_thermal_zone(dev);
  416. int result;
  417. if (!tz->ops->set_mode)
  418. return -EPERM;
  419. if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
  420. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  421. else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
  422. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  423. else
  424. result = -EINVAL;
  425. if (result)
  426. return result;
  427. return count;
  428. }
  429. static ssize_t
  430. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  431. char *buf)
  432. {
  433. struct thermal_zone_device *tz = to_thermal_zone(dev);
  434. enum thermal_trip_type type;
  435. int trip, result;
  436. if (!tz->ops->get_trip_type)
  437. return -EPERM;
  438. if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
  439. return -EINVAL;
  440. result = tz->ops->get_trip_type(tz, trip, &type);
  441. if (result)
  442. return result;
  443. switch (type) {
  444. case THERMAL_TRIP_CRITICAL:
  445. return sprintf(buf, "critical\n");
  446. case THERMAL_TRIP_HOT:
  447. return sprintf(buf, "hot\n");
  448. case THERMAL_TRIP_PASSIVE:
  449. return sprintf(buf, "passive\n");
  450. case THERMAL_TRIP_ACTIVE:
  451. return sprintf(buf, "active\n");
  452. default:
  453. return sprintf(buf, "unknown\n");
  454. }
  455. }
  456. static ssize_t
  457. trip_point_temp_store(struct device *dev, struct device_attribute *attr,
  458. const char *buf, size_t count)
  459. {
  460. struct thermal_zone_device *tz = to_thermal_zone(dev);
  461. int trip, ret;
  462. unsigned long temperature;
  463. if (!tz->ops->set_trip_temp)
  464. return -EPERM;
  465. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  466. return -EINVAL;
  467. if (kstrtoul(buf, 10, &temperature))
  468. return -EINVAL;
  469. ret = tz->ops->set_trip_temp(tz, trip, temperature);
  470. return ret ? ret : count;
  471. }
  472. static ssize_t
  473. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  474. char *buf)
  475. {
  476. struct thermal_zone_device *tz = to_thermal_zone(dev);
  477. int trip, ret;
  478. long temperature;
  479. if (!tz->ops->get_trip_temp)
  480. return -EPERM;
  481. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  482. return -EINVAL;
  483. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  484. if (ret)
  485. return ret;
  486. return sprintf(buf, "%ld\n", temperature);
  487. }
  488. static ssize_t
  489. trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
  490. const char *buf, size_t count)
  491. {
  492. struct thermal_zone_device *tz = to_thermal_zone(dev);
  493. int trip, ret;
  494. unsigned long temperature;
  495. if (!tz->ops->set_trip_hyst)
  496. return -EPERM;
  497. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  498. return -EINVAL;
  499. if (kstrtoul(buf, 10, &temperature))
  500. return -EINVAL;
  501. /*
  502. * We are not doing any check on the 'temperature' value
  503. * here. The driver implementing 'set_trip_hyst' has to
  504. * take care of this.
  505. */
  506. ret = tz->ops->set_trip_hyst(tz, trip, temperature);
  507. return ret ? ret : count;
  508. }
  509. static ssize_t
  510. trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
  511. char *buf)
  512. {
  513. struct thermal_zone_device *tz = to_thermal_zone(dev);
  514. int trip, ret;
  515. unsigned long temperature;
  516. if (!tz->ops->get_trip_hyst)
  517. return -EPERM;
  518. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  519. return -EINVAL;
  520. ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
  521. return ret ? ret : sprintf(buf, "%ld\n", temperature);
  522. }
  523. static ssize_t
  524. passive_store(struct device *dev, struct device_attribute *attr,
  525. const char *buf, size_t count)
  526. {
  527. struct thermal_zone_device *tz = to_thermal_zone(dev);
  528. struct thermal_cooling_device *cdev = NULL;
  529. int state;
  530. if (!sscanf(buf, "%d\n", &state))
  531. return -EINVAL;
  532. /* sanity check: values below 1000 millicelcius don't make sense
  533. * and can cause the system to go into a thermal heart attack
  534. */
  535. if (state && state < 1000)
  536. return -EINVAL;
  537. if (state && !tz->forced_passive) {
  538. mutex_lock(&thermal_list_lock);
  539. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  540. if (!strncmp("Processor", cdev->type,
  541. sizeof("Processor")))
  542. thermal_zone_bind_cooling_device(tz,
  543. THERMAL_TRIPS_NONE, cdev,
  544. THERMAL_NO_LIMIT,
  545. THERMAL_NO_LIMIT);
  546. }
  547. mutex_unlock(&thermal_list_lock);
  548. if (!tz->passive_delay)
  549. tz->passive_delay = 1000;
  550. } else if (!state && tz->forced_passive) {
  551. mutex_lock(&thermal_list_lock);
  552. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  553. if (!strncmp("Processor", cdev->type,
  554. sizeof("Processor")))
  555. thermal_zone_unbind_cooling_device(tz,
  556. THERMAL_TRIPS_NONE,
  557. cdev);
  558. }
  559. mutex_unlock(&thermal_list_lock);
  560. tz->passive_delay = 0;
  561. }
  562. tz->forced_passive = state;
  563. thermal_zone_device_update(tz);
  564. return count;
  565. }
  566. static ssize_t
  567. passive_show(struct device *dev, struct device_attribute *attr,
  568. char *buf)
  569. {
  570. struct thermal_zone_device *tz = to_thermal_zone(dev);
  571. return sprintf(buf, "%d\n", tz->forced_passive);
  572. }
  573. static ssize_t
  574. policy_store(struct device *dev, struct device_attribute *attr,
  575. const char *buf, size_t count)
  576. {
  577. int ret = -EINVAL;
  578. struct thermal_zone_device *tz = to_thermal_zone(dev);
  579. struct thermal_governor *gov;
  580. mutex_lock(&thermal_governor_lock);
  581. gov = __find_governor(buf);
  582. if (!gov)
  583. goto exit;
  584. tz->governor = gov;
  585. ret = count;
  586. exit:
  587. mutex_unlock(&thermal_governor_lock);
  588. return ret;
  589. }
  590. static ssize_t
  591. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  592. {
  593. struct thermal_zone_device *tz = to_thermal_zone(dev);
  594. return sprintf(buf, "%s\n", tz->governor->name);
  595. }
  596. #ifdef CONFIG_THERMAL_EMULATION
  597. static ssize_t
  598. emul_temp_store(struct device *dev, struct device_attribute *attr,
  599. const char *buf, size_t count)
  600. {
  601. struct thermal_zone_device *tz = to_thermal_zone(dev);
  602. int ret = 0;
  603. unsigned long temperature;
  604. if (kstrtoul(buf, 10, &temperature))
  605. return -EINVAL;
  606. if (!tz->ops->set_emul_temp) {
  607. mutex_lock(&tz->lock);
  608. tz->emul_temperature = temperature;
  609. mutex_unlock(&tz->lock);
  610. } else {
  611. ret = tz->ops->set_emul_temp(tz, temperature);
  612. }
  613. return ret ? ret : count;
  614. }
  615. static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
  616. #endif/*CONFIG_THERMAL_EMULATION*/
  617. static DEVICE_ATTR(type, 0444, type_show, NULL);
  618. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  619. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  620. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
  621. static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
  622. /* sys I/F for cooling device */
  623. #define to_cooling_device(_dev) \
  624. container_of(_dev, struct thermal_cooling_device, device)
  625. static ssize_t
  626. thermal_cooling_device_type_show(struct device *dev,
  627. struct device_attribute *attr, char *buf)
  628. {
  629. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  630. return sprintf(buf, "%s\n", cdev->type);
  631. }
  632. static ssize_t
  633. thermal_cooling_device_max_state_show(struct device *dev,
  634. struct device_attribute *attr, char *buf)
  635. {
  636. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  637. unsigned long state;
  638. int ret;
  639. ret = cdev->ops->get_max_state(cdev, &state);
  640. if (ret)
  641. return ret;
  642. return sprintf(buf, "%ld\n", state);
  643. }
  644. static ssize_t
  645. thermal_cooling_device_cur_state_show(struct device *dev,
  646. struct device_attribute *attr, char *buf)
  647. {
  648. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  649. unsigned long state;
  650. int ret;
  651. ret = cdev->ops->get_cur_state(cdev, &state);
  652. if (ret)
  653. return ret;
  654. return sprintf(buf, "%ld\n", state);
  655. }
  656. static ssize_t
  657. thermal_cooling_device_cur_state_store(struct device *dev,
  658. struct device_attribute *attr,
  659. const char *buf, size_t count)
  660. {
  661. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  662. unsigned long state;
  663. int result;
  664. if (!sscanf(buf, "%ld\n", &state))
  665. return -EINVAL;
  666. if ((long)state < 0)
  667. return -EINVAL;
  668. result = cdev->ops->set_cur_state(cdev, state);
  669. if (result)
  670. return result;
  671. return count;
  672. }
  673. static struct device_attribute dev_attr_cdev_type =
  674. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  675. static DEVICE_ATTR(max_state, 0444,
  676. thermal_cooling_device_max_state_show, NULL);
  677. static DEVICE_ATTR(cur_state, 0644,
  678. thermal_cooling_device_cur_state_show,
  679. thermal_cooling_device_cur_state_store);
  680. static ssize_t
  681. thermal_cooling_device_trip_point_show(struct device *dev,
  682. struct device_attribute *attr, char *buf)
  683. {
  684. struct thermal_instance *instance;
  685. instance =
  686. container_of(attr, struct thermal_instance, attr);
  687. if (instance->trip == THERMAL_TRIPS_NONE)
  688. return sprintf(buf, "-1\n");
  689. else
  690. return sprintf(buf, "%d\n", instance->trip);
  691. }
  692. /* Device management */
  693. #if defined(CONFIG_THERMAL_HWMON)
  694. /* hwmon sys I/F */
  695. #include <linux/hwmon.h>
  696. /* thermal zone devices with the same type share one hwmon device */
  697. struct thermal_hwmon_device {
  698. char type[THERMAL_NAME_LENGTH];
  699. struct device *device;
  700. int count;
  701. struct list_head tz_list;
  702. struct list_head node;
  703. };
  704. struct thermal_hwmon_attr {
  705. struct device_attribute attr;
  706. char name[16];
  707. };
  708. /* one temperature input for each thermal zone */
  709. struct thermal_hwmon_temp {
  710. struct list_head hwmon_node;
  711. struct thermal_zone_device *tz;
  712. struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
  713. struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
  714. };
  715. static LIST_HEAD(thermal_hwmon_list);
  716. static ssize_t
  717. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  718. {
  719. struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
  720. return sprintf(buf, "%s\n", hwmon->type);
  721. }
  722. static DEVICE_ATTR(name, 0444, name_show, NULL);
  723. static ssize_t
  724. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  725. {
  726. long temperature;
  727. int ret;
  728. struct thermal_hwmon_attr *hwmon_attr
  729. = container_of(attr, struct thermal_hwmon_attr, attr);
  730. struct thermal_hwmon_temp *temp
  731. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  732. temp_input);
  733. struct thermal_zone_device *tz = temp->tz;
  734. ret = thermal_zone_get_temp(tz, &temperature);
  735. if (ret)
  736. return ret;
  737. return sprintf(buf, "%ld\n", temperature);
  738. }
  739. static ssize_t
  740. temp_crit_show(struct device *dev, struct device_attribute *attr,
  741. char *buf)
  742. {
  743. struct thermal_hwmon_attr *hwmon_attr
  744. = container_of(attr, struct thermal_hwmon_attr, attr);
  745. struct thermal_hwmon_temp *temp
  746. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  747. temp_crit);
  748. struct thermal_zone_device *tz = temp->tz;
  749. long temperature;
  750. int ret;
  751. ret = tz->ops->get_trip_temp(tz, 0, &temperature);
  752. if (ret)
  753. return ret;
  754. return sprintf(buf, "%ld\n", temperature);
  755. }
  756. static struct thermal_hwmon_device *
  757. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  758. {
  759. struct thermal_hwmon_device *hwmon;
  760. mutex_lock(&thermal_list_lock);
  761. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  762. if (!strcmp(hwmon->type, tz->type)) {
  763. mutex_unlock(&thermal_list_lock);
  764. return hwmon;
  765. }
  766. mutex_unlock(&thermal_list_lock);
  767. return NULL;
  768. }
  769. /* Find the temperature input matching a given thermal zone */
  770. static struct thermal_hwmon_temp *
  771. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  772. const struct thermal_zone_device *tz)
  773. {
  774. struct thermal_hwmon_temp *temp;
  775. mutex_lock(&thermal_list_lock);
  776. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  777. if (temp->tz == tz) {
  778. mutex_unlock(&thermal_list_lock);
  779. return temp;
  780. }
  781. mutex_unlock(&thermal_list_lock);
  782. return NULL;
  783. }
  784. static int
  785. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  786. {
  787. struct thermal_hwmon_device *hwmon;
  788. struct thermal_hwmon_temp *temp;
  789. int new_hwmon_device = 1;
  790. int result;
  791. hwmon = thermal_hwmon_lookup_by_type(tz);
  792. if (hwmon) {
  793. new_hwmon_device = 0;
  794. goto register_sys_interface;
  795. }
  796. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  797. if (!hwmon)
  798. return -ENOMEM;
  799. INIT_LIST_HEAD(&hwmon->tz_list);
  800. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  801. hwmon->device = hwmon_device_register(NULL);
  802. if (IS_ERR(hwmon->device)) {
  803. result = PTR_ERR(hwmon->device);
  804. goto free_mem;
  805. }
  806. dev_set_drvdata(hwmon->device, hwmon);
  807. result = device_create_file(hwmon->device, &dev_attr_name);
  808. if (result)
  809. goto free_mem;
  810. register_sys_interface:
  811. temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
  812. if (!temp) {
  813. result = -ENOMEM;
  814. goto unregister_name;
  815. }
  816. temp->tz = tz;
  817. hwmon->count++;
  818. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  819. "temp%d_input", hwmon->count);
  820. temp->temp_input.attr.attr.name = temp->temp_input.name;
  821. temp->temp_input.attr.attr.mode = 0444;
  822. temp->temp_input.attr.show = temp_input_show;
  823. sysfs_attr_init(&temp->temp_input.attr.attr);
  824. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  825. if (result)
  826. goto free_temp_mem;
  827. if (tz->ops->get_crit_temp) {
  828. unsigned long temperature;
  829. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  830. snprintf(temp->temp_crit.name,
  831. sizeof(temp->temp_crit.name),
  832. "temp%d_crit", hwmon->count);
  833. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  834. temp->temp_crit.attr.attr.mode = 0444;
  835. temp->temp_crit.attr.show = temp_crit_show;
  836. sysfs_attr_init(&temp->temp_crit.attr.attr);
  837. result = device_create_file(hwmon->device,
  838. &temp->temp_crit.attr);
  839. if (result)
  840. goto unregister_input;
  841. }
  842. }
  843. mutex_lock(&thermal_list_lock);
  844. if (new_hwmon_device)
  845. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  846. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  847. mutex_unlock(&thermal_list_lock);
  848. return 0;
  849. unregister_input:
  850. device_remove_file(hwmon->device, &temp->temp_input.attr);
  851. free_temp_mem:
  852. kfree(temp);
  853. unregister_name:
  854. if (new_hwmon_device) {
  855. device_remove_file(hwmon->device, &dev_attr_name);
  856. hwmon_device_unregister(hwmon->device);
  857. }
  858. free_mem:
  859. if (new_hwmon_device)
  860. kfree(hwmon);
  861. return result;
  862. }
  863. static void
  864. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  865. {
  866. struct thermal_hwmon_device *hwmon;
  867. struct thermal_hwmon_temp *temp;
  868. hwmon = thermal_hwmon_lookup_by_type(tz);
  869. if (unlikely(!hwmon)) {
  870. /* Should never happen... */
  871. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  872. return;
  873. }
  874. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  875. if (unlikely(!temp)) {
  876. /* Should never happen... */
  877. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  878. return;
  879. }
  880. device_remove_file(hwmon->device, &temp->temp_input.attr);
  881. if (tz->ops->get_crit_temp)
  882. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  883. mutex_lock(&thermal_list_lock);
  884. list_del(&temp->hwmon_node);
  885. kfree(temp);
  886. if (!list_empty(&hwmon->tz_list)) {
  887. mutex_unlock(&thermal_list_lock);
  888. return;
  889. }
  890. list_del(&hwmon->node);
  891. mutex_unlock(&thermal_list_lock);
  892. device_remove_file(hwmon->device, &dev_attr_name);
  893. hwmon_device_unregister(hwmon->device);
  894. kfree(hwmon);
  895. }
  896. #else
  897. static int
  898. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  899. {
  900. return 0;
  901. }
  902. static void
  903. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  904. {
  905. }
  906. #endif
  907. /**
  908. * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
  909. * @tz: pointer to struct thermal_zone_device
  910. * @trip: indicates which trip point the cooling devices is
  911. * associated with in this thermal zone.
  912. * @cdev: pointer to struct thermal_cooling_device
  913. * @upper: the Maximum cooling state for this trip point.
  914. * THERMAL_NO_LIMIT means no upper limit,
  915. * and the cooling device can be in max_state.
  916. * @lower: the Minimum cooling state can be used for this trip point.
  917. * THERMAL_NO_LIMIT means no lower limit,
  918. * and the cooling device can be in cooling state 0.
  919. *
  920. * This interface function bind a thermal cooling device to the certain trip
  921. * point of a thermal zone device.
  922. * This function is usually called in the thermal zone device .bind callback.
  923. *
  924. * Return: 0 on success, the proper error value otherwise.
  925. */
  926. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  927. int trip,
  928. struct thermal_cooling_device *cdev,
  929. unsigned long upper, unsigned long lower)
  930. {
  931. struct thermal_instance *dev;
  932. struct thermal_instance *pos;
  933. struct thermal_zone_device *pos1;
  934. struct thermal_cooling_device *pos2;
  935. unsigned long max_state;
  936. int result;
  937. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  938. return -EINVAL;
  939. list_for_each_entry(pos1, &thermal_tz_list, node) {
  940. if (pos1 == tz)
  941. break;
  942. }
  943. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  944. if (pos2 == cdev)
  945. break;
  946. }
  947. if (tz != pos1 || cdev != pos2)
  948. return -EINVAL;
  949. cdev->ops->get_max_state(cdev, &max_state);
  950. /* lower default 0, upper default max_state */
  951. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  952. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  953. if (lower > upper || upper > max_state)
  954. return -EINVAL;
  955. dev =
  956. kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
  957. if (!dev)
  958. return -ENOMEM;
  959. dev->tz = tz;
  960. dev->cdev = cdev;
  961. dev->trip = trip;
  962. dev->upper = upper;
  963. dev->lower = lower;
  964. dev->target = THERMAL_NO_TARGET;
  965. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  966. if (result)
  967. goto free_mem;
  968. sprintf(dev->name, "cdev%d", dev->id);
  969. result =
  970. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  971. if (result)
  972. goto release_idr;
  973. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  974. sysfs_attr_init(&dev->attr.attr);
  975. dev->attr.attr.name = dev->attr_name;
  976. dev->attr.attr.mode = 0444;
  977. dev->attr.show = thermal_cooling_device_trip_point_show;
  978. result = device_create_file(&tz->device, &dev->attr);
  979. if (result)
  980. goto remove_symbol_link;
  981. mutex_lock(&tz->lock);
  982. mutex_lock(&cdev->lock);
  983. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  984. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  985. result = -EEXIST;
  986. break;
  987. }
  988. if (!result) {
  989. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  990. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  991. }
  992. mutex_unlock(&cdev->lock);
  993. mutex_unlock(&tz->lock);
  994. if (!result)
  995. return 0;
  996. device_remove_file(&tz->device, &dev->attr);
  997. remove_symbol_link:
  998. sysfs_remove_link(&tz->device.kobj, dev->name);
  999. release_idr:
  1000. release_idr(&tz->idr, &tz->lock, dev->id);
  1001. free_mem:
  1002. kfree(dev);
  1003. return result;
  1004. }
  1005. EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
  1006. /**
  1007. * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
  1008. * thermal zone.
  1009. * @tz: pointer to a struct thermal_zone_device.
  1010. * @trip: indicates which trip point the cooling devices is
  1011. * associated with in this thermal zone.
  1012. * @cdev: pointer to a struct thermal_cooling_device.
  1013. *
  1014. * This interface function unbind a thermal cooling device from the certain
  1015. * trip point of a thermal zone device.
  1016. * This function is usually called in the thermal zone device .unbind callback.
  1017. *
  1018. * Return: 0 on success, the proper error value otherwise.
  1019. */
  1020. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  1021. int trip,
  1022. struct thermal_cooling_device *cdev)
  1023. {
  1024. struct thermal_instance *pos, *next;
  1025. mutex_lock(&tz->lock);
  1026. mutex_lock(&cdev->lock);
  1027. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  1028. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  1029. list_del(&pos->tz_node);
  1030. list_del(&pos->cdev_node);
  1031. mutex_unlock(&cdev->lock);
  1032. mutex_unlock(&tz->lock);
  1033. goto unbind;
  1034. }
  1035. }
  1036. mutex_unlock(&cdev->lock);
  1037. mutex_unlock(&tz->lock);
  1038. return -ENODEV;
  1039. unbind:
  1040. device_remove_file(&tz->device, &pos->attr);
  1041. sysfs_remove_link(&tz->device.kobj, pos->name);
  1042. release_idr(&tz->idr, &tz->lock, pos->id);
  1043. kfree(pos);
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
  1047. static void thermal_release(struct device *dev)
  1048. {
  1049. struct thermal_zone_device *tz;
  1050. struct thermal_cooling_device *cdev;
  1051. if (!strncmp(dev_name(dev), "thermal_zone",
  1052. sizeof("thermal_zone") - 1)) {
  1053. tz = to_thermal_zone(dev);
  1054. kfree(tz);
  1055. } else {
  1056. cdev = to_cooling_device(dev);
  1057. kfree(cdev);
  1058. }
  1059. }
  1060. static struct class thermal_class = {
  1061. .name = "thermal",
  1062. .dev_release = thermal_release,
  1063. };
  1064. /**
  1065. * thermal_cooling_device_register() - register a new thermal cooling device
  1066. * @type: the thermal cooling device type.
  1067. * @devdata: device private data.
  1068. * @ops: standard thermal cooling devices callbacks.
  1069. *
  1070. * This interface function adds a new thermal cooling device (fan/processor/...)
  1071. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  1072. * to all the thermal zone devices registered at the same time.
  1073. *
  1074. * Return: a pointer to the created struct thermal_cooling_device or an
  1075. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  1076. */
  1077. struct thermal_cooling_device *
  1078. thermal_cooling_device_register(char *type, void *devdata,
  1079. const struct thermal_cooling_device_ops *ops)
  1080. {
  1081. struct thermal_cooling_device *cdev;
  1082. int result;
  1083. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1084. return ERR_PTR(-EINVAL);
  1085. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  1086. !ops->set_cur_state)
  1087. return ERR_PTR(-EINVAL);
  1088. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  1089. if (!cdev)
  1090. return ERR_PTR(-ENOMEM);
  1091. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  1092. if (result) {
  1093. kfree(cdev);
  1094. return ERR_PTR(result);
  1095. }
  1096. strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
  1097. mutex_init(&cdev->lock);
  1098. INIT_LIST_HEAD(&cdev->thermal_instances);
  1099. cdev->ops = ops;
  1100. cdev->updated = true;
  1101. cdev->device.class = &thermal_class;
  1102. cdev->devdata = devdata;
  1103. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  1104. result = device_register(&cdev->device);
  1105. if (result) {
  1106. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1107. kfree(cdev);
  1108. return ERR_PTR(result);
  1109. }
  1110. /* sys I/F */
  1111. if (type) {
  1112. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  1113. if (result)
  1114. goto unregister;
  1115. }
  1116. result = device_create_file(&cdev->device, &dev_attr_max_state);
  1117. if (result)
  1118. goto unregister;
  1119. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  1120. if (result)
  1121. goto unregister;
  1122. /* Add 'this' new cdev to the global cdev list */
  1123. mutex_lock(&thermal_list_lock);
  1124. list_add(&cdev->node, &thermal_cdev_list);
  1125. mutex_unlock(&thermal_list_lock);
  1126. /* Update binding information for 'this' new cdev */
  1127. bind_cdev(cdev);
  1128. return cdev;
  1129. unregister:
  1130. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1131. device_unregister(&cdev->device);
  1132. return ERR_PTR(result);
  1133. }
  1134. EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
  1135. /**
  1136. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  1137. * @cdev: the thermal cooling device to remove.
  1138. *
  1139. * thermal_cooling_device_unregister() must be called when the device is no
  1140. * longer needed.
  1141. */
  1142. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  1143. {
  1144. int i;
  1145. const struct thermal_zone_params *tzp;
  1146. struct thermal_zone_device *tz;
  1147. struct thermal_cooling_device *pos = NULL;
  1148. if (!cdev)
  1149. return;
  1150. mutex_lock(&thermal_list_lock);
  1151. list_for_each_entry(pos, &thermal_cdev_list, node)
  1152. if (pos == cdev)
  1153. break;
  1154. if (pos != cdev) {
  1155. /* thermal cooling device not found */
  1156. mutex_unlock(&thermal_list_lock);
  1157. return;
  1158. }
  1159. list_del(&cdev->node);
  1160. /* Unbind all thermal zones associated with 'this' cdev */
  1161. list_for_each_entry(tz, &thermal_tz_list, node) {
  1162. if (tz->ops->unbind) {
  1163. tz->ops->unbind(tz, cdev);
  1164. continue;
  1165. }
  1166. if (!tz->tzp || !tz->tzp->tbp)
  1167. continue;
  1168. tzp = tz->tzp;
  1169. for (i = 0; i < tzp->num_tbps; i++) {
  1170. if (tzp->tbp[i].cdev == cdev) {
  1171. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1172. tzp->tbp[i].cdev = NULL;
  1173. }
  1174. }
  1175. }
  1176. mutex_unlock(&thermal_list_lock);
  1177. if (cdev->type[0])
  1178. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  1179. device_remove_file(&cdev->device, &dev_attr_max_state);
  1180. device_remove_file(&cdev->device, &dev_attr_cur_state);
  1181. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1182. device_unregister(&cdev->device);
  1183. return;
  1184. }
  1185. EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
  1186. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  1187. {
  1188. struct thermal_instance *instance;
  1189. unsigned long target = 0;
  1190. /* cooling device is updated*/
  1191. if (cdev->updated)
  1192. return;
  1193. mutex_lock(&cdev->lock);
  1194. /* Make sure cdev enters the deepest cooling state */
  1195. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  1196. if (instance->target == THERMAL_NO_TARGET)
  1197. continue;
  1198. if (instance->target > target)
  1199. target = instance->target;
  1200. }
  1201. mutex_unlock(&cdev->lock);
  1202. cdev->ops->set_cur_state(cdev, target);
  1203. cdev->updated = true;
  1204. }
  1205. EXPORT_SYMBOL(thermal_cdev_update);
  1206. /**
  1207. * thermal_notify_framework - Sensor drivers use this API to notify framework
  1208. * @tz: thermal zone device
  1209. * @trip: indicates which trip point has been crossed
  1210. *
  1211. * This function handles the trip events from sensor drivers. It starts
  1212. * throttling the cooling devices according to the policy configured.
  1213. * For CRITICAL and HOT trip points, this notifies the respective drivers,
  1214. * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
  1215. * The throttling policy is based on the configured platform data; if no
  1216. * platform data is provided, this uses the step_wise throttling policy.
  1217. */
  1218. void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
  1219. {
  1220. handle_thermal_trip(tz, trip);
  1221. }
  1222. EXPORT_SYMBOL_GPL(thermal_notify_framework);
  1223. /**
  1224. * create_trip_attrs() - create attributes for trip points
  1225. * @tz: the thermal zone device
  1226. * @mask: Writeable trip point bitmap.
  1227. *
  1228. * helper function to instantiate sysfs entries for every trip
  1229. * point and its properties of a struct thermal_zone_device.
  1230. *
  1231. * Return: 0 on success, the proper error value otherwise.
  1232. */
  1233. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  1234. {
  1235. int indx;
  1236. int size = sizeof(struct thermal_attr) * tz->trips;
  1237. tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
  1238. if (!tz->trip_type_attrs)
  1239. return -ENOMEM;
  1240. tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
  1241. if (!tz->trip_temp_attrs) {
  1242. kfree(tz->trip_type_attrs);
  1243. return -ENOMEM;
  1244. }
  1245. if (tz->ops->get_trip_hyst) {
  1246. tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
  1247. if (!tz->trip_hyst_attrs) {
  1248. kfree(tz->trip_type_attrs);
  1249. kfree(tz->trip_temp_attrs);
  1250. return -ENOMEM;
  1251. }
  1252. }
  1253. for (indx = 0; indx < tz->trips; indx++) {
  1254. /* create trip type attribute */
  1255. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  1256. "trip_point_%d_type", indx);
  1257. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  1258. tz->trip_type_attrs[indx].attr.attr.name =
  1259. tz->trip_type_attrs[indx].name;
  1260. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  1261. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  1262. device_create_file(&tz->device,
  1263. &tz->trip_type_attrs[indx].attr);
  1264. /* create trip temp attribute */
  1265. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  1266. "trip_point_%d_temp", indx);
  1267. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  1268. tz->trip_temp_attrs[indx].attr.attr.name =
  1269. tz->trip_temp_attrs[indx].name;
  1270. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  1271. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  1272. if (mask & (1 << indx)) {
  1273. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  1274. tz->trip_temp_attrs[indx].attr.store =
  1275. trip_point_temp_store;
  1276. }
  1277. device_create_file(&tz->device,
  1278. &tz->trip_temp_attrs[indx].attr);
  1279. /* create Optional trip hyst attribute */
  1280. if (!tz->ops->get_trip_hyst)
  1281. continue;
  1282. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  1283. "trip_point_%d_hyst", indx);
  1284. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  1285. tz->trip_hyst_attrs[indx].attr.attr.name =
  1286. tz->trip_hyst_attrs[indx].name;
  1287. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  1288. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  1289. if (tz->ops->set_trip_hyst) {
  1290. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  1291. tz->trip_hyst_attrs[indx].attr.store =
  1292. trip_point_hyst_store;
  1293. }
  1294. device_create_file(&tz->device,
  1295. &tz->trip_hyst_attrs[indx].attr);
  1296. }
  1297. return 0;
  1298. }
  1299. static void remove_trip_attrs(struct thermal_zone_device *tz)
  1300. {
  1301. int indx;
  1302. for (indx = 0; indx < tz->trips; indx++) {
  1303. device_remove_file(&tz->device,
  1304. &tz->trip_type_attrs[indx].attr);
  1305. device_remove_file(&tz->device,
  1306. &tz->trip_temp_attrs[indx].attr);
  1307. if (tz->ops->get_trip_hyst)
  1308. device_remove_file(&tz->device,
  1309. &tz->trip_hyst_attrs[indx].attr);
  1310. }
  1311. kfree(tz->trip_type_attrs);
  1312. kfree(tz->trip_temp_attrs);
  1313. kfree(tz->trip_hyst_attrs);
  1314. }
  1315. /**
  1316. * thermal_zone_device_register() - register a new thermal zone device
  1317. * @type: the thermal zone device type
  1318. * @trips: the number of trip points the thermal zone support
  1319. * @mask: a bit string indicating the writeablility of trip points
  1320. * @devdata: private device data
  1321. * @ops: standard thermal zone device callbacks
  1322. * @tzp: thermal zone platform parameters
  1323. * @passive_delay: number of milliseconds to wait between polls when
  1324. * performing passive cooling
  1325. * @polling_delay: number of milliseconds to wait between polls when checking
  1326. * whether trip points have been crossed (0 for interrupt
  1327. * driven systems)
  1328. *
  1329. * This interface function adds a new thermal zone device (sensor) to
  1330. * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
  1331. * thermal cooling devices registered at the same time.
  1332. * thermal_zone_device_unregister() must be called when the device is no
  1333. * longer needed. The passive cooling depends on the .get_trend() return value.
  1334. *
  1335. * Return: a pointer to the created struct thermal_zone_device or an
  1336. * in case of error, an ERR_PTR. Caller must check return value with
  1337. * IS_ERR*() helpers.
  1338. */
  1339. struct thermal_zone_device *thermal_zone_device_register(const char *type,
  1340. int trips, int mask, void *devdata,
  1341. const struct thermal_zone_device_ops *ops,
  1342. const struct thermal_zone_params *tzp,
  1343. int passive_delay, int polling_delay)
  1344. {
  1345. struct thermal_zone_device *tz;
  1346. enum thermal_trip_type trip_type;
  1347. int result;
  1348. int count;
  1349. int passive = 0;
  1350. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1351. return ERR_PTR(-EINVAL);
  1352. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  1353. return ERR_PTR(-EINVAL);
  1354. if (!ops || !ops->get_temp)
  1355. return ERR_PTR(-EINVAL);
  1356. if (trips > 0 && !ops->get_trip_type)
  1357. return ERR_PTR(-EINVAL);
  1358. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  1359. if (!tz)
  1360. return ERR_PTR(-ENOMEM);
  1361. INIT_LIST_HEAD(&tz->thermal_instances);
  1362. idr_init(&tz->idr);
  1363. mutex_init(&tz->lock);
  1364. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  1365. if (result) {
  1366. kfree(tz);
  1367. return ERR_PTR(result);
  1368. }
  1369. strlcpy(tz->type, type ? : "", sizeof(tz->type));
  1370. tz->ops = ops;
  1371. tz->tzp = tzp;
  1372. tz->device.class = &thermal_class;
  1373. tz->devdata = devdata;
  1374. tz->trips = trips;
  1375. tz->passive_delay = passive_delay;
  1376. tz->polling_delay = polling_delay;
  1377. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1378. result = device_register(&tz->device);
  1379. if (result) {
  1380. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1381. kfree(tz);
  1382. return ERR_PTR(result);
  1383. }
  1384. /* sys I/F */
  1385. if (type) {
  1386. result = device_create_file(&tz->device, &dev_attr_type);
  1387. if (result)
  1388. goto unregister;
  1389. }
  1390. result = device_create_file(&tz->device, &dev_attr_temp);
  1391. if (result)
  1392. goto unregister;
  1393. if (ops->get_mode) {
  1394. result = device_create_file(&tz->device, &dev_attr_mode);
  1395. if (result)
  1396. goto unregister;
  1397. }
  1398. result = create_trip_attrs(tz, mask);
  1399. if (result)
  1400. goto unregister;
  1401. for (count = 0; count < trips; count++) {
  1402. tz->ops->get_trip_type(tz, count, &trip_type);
  1403. if (trip_type == THERMAL_TRIP_PASSIVE)
  1404. passive = 1;
  1405. }
  1406. if (!passive) {
  1407. result = device_create_file(&tz->device, &dev_attr_passive);
  1408. if (result)
  1409. goto unregister;
  1410. }
  1411. #ifdef CONFIG_THERMAL_EMULATION
  1412. result = device_create_file(&tz->device, &dev_attr_emul_temp);
  1413. if (result)
  1414. goto unregister;
  1415. #endif
  1416. /* Create policy attribute */
  1417. result = device_create_file(&tz->device, &dev_attr_policy);
  1418. if (result)
  1419. goto unregister;
  1420. /* Update 'this' zone's governor information */
  1421. mutex_lock(&thermal_governor_lock);
  1422. if (tz->tzp)
  1423. tz->governor = __find_governor(tz->tzp->governor_name);
  1424. else
  1425. tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
  1426. mutex_unlock(&thermal_governor_lock);
  1427. result = thermal_add_hwmon_sysfs(tz);
  1428. if (result)
  1429. goto unregister;
  1430. mutex_lock(&thermal_list_lock);
  1431. list_add_tail(&tz->node, &thermal_tz_list);
  1432. mutex_unlock(&thermal_list_lock);
  1433. /* Bind cooling devices for this zone */
  1434. bind_tz(tz);
  1435. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  1436. thermal_zone_device_update(tz);
  1437. if (!result)
  1438. return tz;
  1439. unregister:
  1440. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1441. device_unregister(&tz->device);
  1442. return ERR_PTR(result);
  1443. }
  1444. EXPORT_SYMBOL_GPL(thermal_zone_device_register);
  1445. /**
  1446. * thermal_device_unregister - removes the registered thermal zone device
  1447. * @tz: the thermal zone device to remove
  1448. */
  1449. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1450. {
  1451. int i;
  1452. const struct thermal_zone_params *tzp;
  1453. struct thermal_cooling_device *cdev;
  1454. struct thermal_zone_device *pos = NULL;
  1455. if (!tz)
  1456. return;
  1457. tzp = tz->tzp;
  1458. mutex_lock(&thermal_list_lock);
  1459. list_for_each_entry(pos, &thermal_tz_list, node)
  1460. if (pos == tz)
  1461. break;
  1462. if (pos != tz) {
  1463. /* thermal zone device not found */
  1464. mutex_unlock(&thermal_list_lock);
  1465. return;
  1466. }
  1467. list_del(&tz->node);
  1468. /* Unbind all cdevs associated with 'this' thermal zone */
  1469. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  1470. if (tz->ops->unbind) {
  1471. tz->ops->unbind(tz, cdev);
  1472. continue;
  1473. }
  1474. if (!tzp || !tzp->tbp)
  1475. break;
  1476. for (i = 0; i < tzp->num_tbps; i++) {
  1477. if (tzp->tbp[i].cdev == cdev) {
  1478. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1479. tzp->tbp[i].cdev = NULL;
  1480. }
  1481. }
  1482. }
  1483. mutex_unlock(&thermal_list_lock);
  1484. thermal_zone_device_set_polling(tz, 0);
  1485. if (tz->type[0])
  1486. device_remove_file(&tz->device, &dev_attr_type);
  1487. device_remove_file(&tz->device, &dev_attr_temp);
  1488. if (tz->ops->get_mode)
  1489. device_remove_file(&tz->device, &dev_attr_mode);
  1490. device_remove_file(&tz->device, &dev_attr_policy);
  1491. remove_trip_attrs(tz);
  1492. tz->governor = NULL;
  1493. thermal_remove_hwmon_sysfs(tz);
  1494. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1495. idr_destroy(&tz->idr);
  1496. mutex_destroy(&tz->lock);
  1497. device_unregister(&tz->device);
  1498. return;
  1499. }
  1500. EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
  1501. /**
  1502. * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
  1503. * @name: thermal zone name to fetch the temperature
  1504. *
  1505. * When only one zone is found with the passed name, returns a reference to it.
  1506. *
  1507. * Return: On success returns a reference to an unique thermal zone with
  1508. * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
  1509. * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
  1510. */
  1511. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
  1512. {
  1513. struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
  1514. unsigned int found = 0;
  1515. if (!name)
  1516. goto exit;
  1517. mutex_lock(&thermal_list_lock);
  1518. list_for_each_entry(pos, &thermal_tz_list, node)
  1519. if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
  1520. found++;
  1521. ref = pos;
  1522. }
  1523. mutex_unlock(&thermal_list_lock);
  1524. /* nothing has been found, thus an error code for it */
  1525. if (found == 0)
  1526. ref = ERR_PTR(-ENODEV);
  1527. else if (found > 1)
  1528. /* Success only when an unique zone is found */
  1529. ref = ERR_PTR(-EEXIST);
  1530. exit:
  1531. return ref;
  1532. }
  1533. EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
  1534. #ifdef CONFIG_NET
  1535. static struct genl_family thermal_event_genl_family = {
  1536. .id = GENL_ID_GENERATE,
  1537. .name = THERMAL_GENL_FAMILY_NAME,
  1538. .version = THERMAL_GENL_VERSION,
  1539. .maxattr = THERMAL_GENL_ATTR_MAX,
  1540. };
  1541. static struct genl_multicast_group thermal_event_mcgrp = {
  1542. .name = THERMAL_GENL_MCAST_GROUP_NAME,
  1543. };
  1544. int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  1545. enum events event)
  1546. {
  1547. struct sk_buff *skb;
  1548. struct nlattr *attr;
  1549. struct thermal_genl_event *thermal_event;
  1550. void *msg_header;
  1551. int size;
  1552. int result;
  1553. static unsigned int thermal_event_seqnum;
  1554. if (!tz)
  1555. return -EINVAL;
  1556. /* allocate memory */
  1557. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1558. nla_total_size(0);
  1559. skb = genlmsg_new(size, GFP_ATOMIC);
  1560. if (!skb)
  1561. return -ENOMEM;
  1562. /* add the genetlink message header */
  1563. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1564. &thermal_event_genl_family, 0,
  1565. THERMAL_GENL_CMD_EVENT);
  1566. if (!msg_header) {
  1567. nlmsg_free(skb);
  1568. return -ENOMEM;
  1569. }
  1570. /* fill the data */
  1571. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1572. sizeof(struct thermal_genl_event));
  1573. if (!attr) {
  1574. nlmsg_free(skb);
  1575. return -EINVAL;
  1576. }
  1577. thermal_event = nla_data(attr);
  1578. if (!thermal_event) {
  1579. nlmsg_free(skb);
  1580. return -EINVAL;
  1581. }
  1582. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1583. thermal_event->orig = tz->id;
  1584. thermal_event->event = event;
  1585. /* send multicast genetlink message */
  1586. result = genlmsg_end(skb, msg_header);
  1587. if (result < 0) {
  1588. nlmsg_free(skb);
  1589. return result;
  1590. }
  1591. result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
  1592. if (result)
  1593. dev_err(&tz->device, "Failed to send netlink event:%d", result);
  1594. return result;
  1595. }
  1596. EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
  1597. static int genetlink_init(void)
  1598. {
  1599. int result;
  1600. result = genl_register_family(&thermal_event_genl_family);
  1601. if (result)
  1602. return result;
  1603. result = genl_register_mc_group(&thermal_event_genl_family,
  1604. &thermal_event_mcgrp);
  1605. if (result)
  1606. genl_unregister_family(&thermal_event_genl_family);
  1607. return result;
  1608. }
  1609. static void genetlink_exit(void)
  1610. {
  1611. genl_unregister_family(&thermal_event_genl_family);
  1612. }
  1613. #else /* !CONFIG_NET */
  1614. static inline int genetlink_init(void) { return 0; }
  1615. static inline void genetlink_exit(void) {}
  1616. #endif /* !CONFIG_NET */
  1617. static int __init thermal_register_governors(void)
  1618. {
  1619. int result;
  1620. result = thermal_gov_step_wise_register();
  1621. if (result)
  1622. return result;
  1623. result = thermal_gov_fair_share_register();
  1624. if (result)
  1625. return result;
  1626. return thermal_gov_user_space_register();
  1627. }
  1628. static void thermal_unregister_governors(void)
  1629. {
  1630. thermal_gov_step_wise_unregister();
  1631. thermal_gov_fair_share_unregister();
  1632. thermal_gov_user_space_unregister();
  1633. }
  1634. static int __init thermal_init(void)
  1635. {
  1636. int result;
  1637. result = thermal_register_governors();
  1638. if (result)
  1639. goto error;
  1640. result = class_register(&thermal_class);
  1641. if (result)
  1642. goto unregister_governors;
  1643. result = genetlink_init();
  1644. if (result)
  1645. goto unregister_class;
  1646. return 0;
  1647. unregister_governors:
  1648. thermal_unregister_governors();
  1649. unregister_class:
  1650. class_unregister(&thermal_class);
  1651. error:
  1652. idr_destroy(&thermal_tz_idr);
  1653. idr_destroy(&thermal_cdev_idr);
  1654. mutex_destroy(&thermal_idr_lock);
  1655. mutex_destroy(&thermal_list_lock);
  1656. mutex_destroy(&thermal_governor_lock);
  1657. return result;
  1658. }
  1659. static void __exit thermal_exit(void)
  1660. {
  1661. genetlink_exit();
  1662. class_unregister(&thermal_class);
  1663. thermal_unregister_governors();
  1664. idr_destroy(&thermal_tz_idr);
  1665. idr_destroy(&thermal_cdev_idr);
  1666. mutex_destroy(&thermal_idr_lock);
  1667. mutex_destroy(&thermal_list_lock);
  1668. mutex_destroy(&thermal_governor_lock);
  1669. }
  1670. fs_initcall(thermal_init);
  1671. module_exit(thermal_exit);