thermal_core.c 50 KB

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