thermal_core.c 50 KB

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