thermal.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  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. * This driver fully implements the ACPI thermal policy as described in the
  26. * ACPI 2.0 Specification.
  27. *
  28. * TBD: 1. Implement passive cooling hysteresis.
  29. * 2. Enhance passive cooling (CPU) states/limit interface to support
  30. * concepts of 'multiple limiters', upper/lower limits, etc.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/types.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/sched.h>
  39. #include <linux/kmod.h>
  40. #include <linux/seq_file.h>
  41. #include <asm/uaccess.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #define ACPI_THERMAL_COMPONENT 0x04000000
  45. #define ACPI_THERMAL_CLASS "thermal_zone"
  46. #define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
  47. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  48. #define ACPI_THERMAL_FILE_STATE "state"
  49. #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
  50. #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
  51. #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
  52. #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
  53. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  54. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  55. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  56. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  57. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  58. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  59. #define ACPI_THERMAL_MODE_PASSIVE 0x01
  60. #define ACPI_THERMAL_MODE_CRITICAL 0xff
  61. #define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
  62. #define ACPI_THERMAL_MAX_ACTIVE 10
  63. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  64. #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
  65. #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
  66. #define _COMPONENT ACPI_THERMAL_COMPONENT
  67. ACPI_MODULE_NAME("acpi_thermal")
  68. MODULE_AUTHOR("Paul Diefenbaugh");
  69. MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
  70. MODULE_LICENSE("GPL");
  71. static int tzp;
  72. module_param(tzp, int, 0);
  73. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
  74. static int acpi_thermal_add(struct acpi_device *device);
  75. static int acpi_thermal_remove(struct acpi_device *device, int type);
  76. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
  77. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
  78. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
  79. static ssize_t acpi_thermal_write_trip_points(struct file *,
  80. const char __user *, size_t,
  81. loff_t *);
  82. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
  83. static ssize_t acpi_thermal_write_cooling_mode(struct file *,
  84. const char __user *, size_t,
  85. loff_t *);
  86. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
  87. static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
  88. size_t, loff_t *);
  89. static struct acpi_driver acpi_thermal_driver = {
  90. .name = ACPI_THERMAL_DRIVER_NAME,
  91. .class = ACPI_THERMAL_CLASS,
  92. .ids = ACPI_THERMAL_HID,
  93. .ops = {
  94. .add = acpi_thermal_add,
  95. .remove = acpi_thermal_remove,
  96. },
  97. };
  98. struct acpi_thermal_state {
  99. u8 critical:1;
  100. u8 hot:1;
  101. u8 passive:1;
  102. u8 active:1;
  103. u8 reserved:4;
  104. int active_index;
  105. };
  106. struct acpi_thermal_state_flags {
  107. u8 valid:1;
  108. u8 enabled:1;
  109. u8 reserved:6;
  110. };
  111. struct acpi_thermal_critical {
  112. struct acpi_thermal_state_flags flags;
  113. unsigned long temperature;
  114. };
  115. struct acpi_thermal_hot {
  116. struct acpi_thermal_state_flags flags;
  117. unsigned long temperature;
  118. };
  119. struct acpi_thermal_passive {
  120. struct acpi_thermal_state_flags flags;
  121. unsigned long temperature;
  122. unsigned long tc1;
  123. unsigned long tc2;
  124. unsigned long tsp;
  125. struct acpi_handle_list devices;
  126. };
  127. struct acpi_thermal_active {
  128. struct acpi_thermal_state_flags flags;
  129. unsigned long temperature;
  130. struct acpi_handle_list devices;
  131. };
  132. struct acpi_thermal_trips {
  133. struct acpi_thermal_critical critical;
  134. struct acpi_thermal_hot hot;
  135. struct acpi_thermal_passive passive;
  136. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  137. };
  138. struct acpi_thermal_flags {
  139. u8 cooling_mode:1; /* _SCP */
  140. u8 devices:1; /* _TZD */
  141. u8 reserved:6;
  142. };
  143. struct acpi_thermal {
  144. acpi_handle handle;
  145. acpi_bus_id name;
  146. unsigned long temperature;
  147. unsigned long last_temperature;
  148. unsigned long polling_frequency;
  149. u8 cooling_mode;
  150. volatile u8 zombie;
  151. struct acpi_thermal_flags flags;
  152. struct acpi_thermal_state state;
  153. struct acpi_thermal_trips trips;
  154. struct acpi_handle_list devices;
  155. struct timer_list timer;
  156. };
  157. static struct file_operations acpi_thermal_state_fops = {
  158. .open = acpi_thermal_state_open_fs,
  159. .read = seq_read,
  160. .llseek = seq_lseek,
  161. .release = single_release,
  162. };
  163. static struct file_operations acpi_thermal_temp_fops = {
  164. .open = acpi_thermal_temp_open_fs,
  165. .read = seq_read,
  166. .llseek = seq_lseek,
  167. .release = single_release,
  168. };
  169. static struct file_operations acpi_thermal_trip_fops = {
  170. .open = acpi_thermal_trip_open_fs,
  171. .read = seq_read,
  172. .write = acpi_thermal_write_trip_points,
  173. .llseek = seq_lseek,
  174. .release = single_release,
  175. };
  176. static struct file_operations acpi_thermal_cooling_fops = {
  177. .open = acpi_thermal_cooling_open_fs,
  178. .read = seq_read,
  179. .write = acpi_thermal_write_cooling_mode,
  180. .llseek = seq_lseek,
  181. .release = single_release,
  182. };
  183. static struct file_operations acpi_thermal_polling_fops = {
  184. .open = acpi_thermal_polling_open_fs,
  185. .read = seq_read,
  186. .write = acpi_thermal_write_polling,
  187. .llseek = seq_lseek,
  188. .release = single_release,
  189. };
  190. /* --------------------------------------------------------------------------
  191. Thermal Zone Management
  192. -------------------------------------------------------------------------- */
  193. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  194. {
  195. acpi_status status = AE_OK;
  196. ACPI_FUNCTION_TRACE("acpi_thermal_get_temperature");
  197. if (!tz)
  198. return_VALUE(-EINVAL);
  199. tz->last_temperature = tz->temperature;
  200. status =
  201. acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
  202. if (ACPI_FAILURE(status))
  203. return_VALUE(-ENODEV);
  204. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  205. tz->temperature));
  206. return_VALUE(0);
  207. }
  208. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  209. {
  210. acpi_status status = AE_OK;
  211. ACPI_FUNCTION_TRACE("acpi_thermal_get_polling_frequency");
  212. if (!tz)
  213. return_VALUE(-EINVAL);
  214. status =
  215. acpi_evaluate_integer(tz->handle, "_TZP", NULL,
  216. &tz->polling_frequency);
  217. if (ACPI_FAILURE(status))
  218. return_VALUE(-ENODEV);
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  220. tz->polling_frequency));
  221. return_VALUE(0);
  222. }
  223. static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
  224. {
  225. ACPI_FUNCTION_TRACE("acpi_thermal_set_polling");
  226. if (!tz)
  227. return_VALUE(-EINVAL);
  228. tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
  229. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  230. "Polling frequency set to %lu seconds\n",
  231. tz->polling_frequency));
  232. return_VALUE(0);
  233. }
  234. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  235. {
  236. acpi_status status = AE_OK;
  237. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  238. struct acpi_object_list arg_list = { 1, &arg0 };
  239. acpi_handle handle = NULL;
  240. ACPI_FUNCTION_TRACE("acpi_thermal_set_cooling_mode");
  241. if (!tz)
  242. return_VALUE(-EINVAL);
  243. status = acpi_get_handle(tz->handle, "_SCP", &handle);
  244. if (ACPI_FAILURE(status)) {
  245. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  246. return_VALUE(-ENODEV);
  247. }
  248. arg0.integer.value = mode;
  249. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  250. if (ACPI_FAILURE(status))
  251. return_VALUE(-ENODEV);
  252. tz->cooling_mode = mode;
  253. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
  254. mode ? "passive" : "active"));
  255. return_VALUE(0);
  256. }
  257. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  258. {
  259. acpi_status status = AE_OK;
  260. int i = 0;
  261. ACPI_FUNCTION_TRACE("acpi_thermal_get_trip_points");
  262. if (!tz)
  263. return_VALUE(-EINVAL);
  264. /* Critical Shutdown (required) */
  265. status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
  266. &tz->trips.critical.temperature);
  267. if (ACPI_FAILURE(status)) {
  268. tz->trips.critical.flags.valid = 0;
  269. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical threshold\n"));
  270. return_VALUE(-ENODEV);
  271. } else {
  272. tz->trips.critical.flags.valid = 1;
  273. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  274. "Found critical threshold [%lu]\n",
  275. tz->trips.critical.temperature));
  276. }
  277. /* Critical Sleep (optional) */
  278. status =
  279. acpi_evaluate_integer(tz->handle, "_HOT", NULL,
  280. &tz->trips.hot.temperature);
  281. if (ACPI_FAILURE(status)) {
  282. tz->trips.hot.flags.valid = 0;
  283. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
  284. } else {
  285. tz->trips.hot.flags.valid = 1;
  286. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
  287. tz->trips.hot.temperature));
  288. }
  289. /* Passive: Processors (optional) */
  290. status =
  291. acpi_evaluate_integer(tz->handle, "_PSV", NULL,
  292. &tz->trips.passive.temperature);
  293. if (ACPI_FAILURE(status)) {
  294. tz->trips.passive.flags.valid = 0;
  295. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
  296. } else {
  297. tz->trips.passive.flags.valid = 1;
  298. status =
  299. acpi_evaluate_integer(tz->handle, "_TC1", NULL,
  300. &tz->trips.passive.tc1);
  301. if (ACPI_FAILURE(status))
  302. tz->trips.passive.flags.valid = 0;
  303. status =
  304. acpi_evaluate_integer(tz->handle, "_TC2", NULL,
  305. &tz->trips.passive.tc2);
  306. if (ACPI_FAILURE(status))
  307. tz->trips.passive.flags.valid = 0;
  308. status =
  309. acpi_evaluate_integer(tz->handle, "_TSP", NULL,
  310. &tz->trips.passive.tsp);
  311. if (ACPI_FAILURE(status))
  312. tz->trips.passive.flags.valid = 0;
  313. status =
  314. acpi_evaluate_reference(tz->handle, "_PSL", NULL,
  315. &tz->trips.passive.devices);
  316. if (ACPI_FAILURE(status))
  317. tz->trips.passive.flags.valid = 0;
  318. if (!tz->trips.passive.flags.valid)
  319. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  320. "Invalid passive threshold\n"));
  321. else
  322. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  323. "Found passive threshold [%lu]\n",
  324. tz->trips.passive.temperature));
  325. }
  326. /* Active: Fans, etc. (optional) */
  327. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  328. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  329. status =
  330. acpi_evaluate_integer(tz->handle, name, NULL,
  331. &tz->trips.active[i].temperature);
  332. if (ACPI_FAILURE(status))
  333. break;
  334. name[2] = 'L';
  335. status =
  336. acpi_evaluate_reference(tz->handle, name, NULL,
  337. &tz->trips.active[i].devices);
  338. if (ACPI_SUCCESS(status)) {
  339. tz->trips.active[i].flags.valid = 1;
  340. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  341. "Found active threshold [%d]:[%lu]\n",
  342. i, tz->trips.active[i].temperature));
  343. } else
  344. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  345. "Invalid active threshold [%d]\n",
  346. i));
  347. }
  348. return_VALUE(0);
  349. }
  350. static int acpi_thermal_get_devices(struct acpi_thermal *tz)
  351. {
  352. acpi_status status = AE_OK;
  353. ACPI_FUNCTION_TRACE("acpi_thermal_get_devices");
  354. if (!tz)
  355. return_VALUE(-EINVAL);
  356. status =
  357. acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
  358. if (ACPI_FAILURE(status))
  359. return_VALUE(-ENODEV);
  360. return_VALUE(0);
  361. }
  362. static int acpi_thermal_call_usermode(char *path)
  363. {
  364. char *argv[2] = { NULL, NULL };
  365. char *envp[3] = { NULL, NULL, NULL };
  366. ACPI_FUNCTION_TRACE("acpi_thermal_call_usermode");
  367. if (!path)
  368. return_VALUE(-EINVAL);
  369. argv[0] = path;
  370. /* minimal command environment */
  371. envp[0] = "HOME=/";
  372. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  373. call_usermodehelper(argv[0], argv, envp, 0);
  374. return_VALUE(0);
  375. }
  376. static int acpi_thermal_critical(struct acpi_thermal *tz)
  377. {
  378. int result = 0;
  379. struct acpi_device *device = NULL;
  380. ACPI_FUNCTION_TRACE("acpi_thermal_critical");
  381. if (!tz || !tz->trips.critical.flags.valid)
  382. return_VALUE(-EINVAL);
  383. if (tz->temperature >= tz->trips.critical.temperature) {
  384. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Critical trip point\n"));
  385. tz->trips.critical.flags.enabled = 1;
  386. } else if (tz->trips.critical.flags.enabled)
  387. tz->trips.critical.flags.enabled = 0;
  388. result = acpi_bus_get_device(tz->handle, &device);
  389. if (result)
  390. return_VALUE(result);
  391. printk(KERN_EMERG
  392. "Critical temperature reached (%ld C), shutting down.\n",
  393. KELVIN_TO_CELSIUS(tz->temperature));
  394. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
  395. tz->trips.critical.flags.enabled);
  396. acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
  397. return_VALUE(0);
  398. }
  399. static int acpi_thermal_hot(struct acpi_thermal *tz)
  400. {
  401. int result = 0;
  402. struct acpi_device *device = NULL;
  403. ACPI_FUNCTION_TRACE("acpi_thermal_hot");
  404. if (!tz || !tz->trips.hot.flags.valid)
  405. return_VALUE(-EINVAL);
  406. if (tz->temperature >= tz->trips.hot.temperature) {
  407. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Hot trip point\n"));
  408. tz->trips.hot.flags.enabled = 1;
  409. } else if (tz->trips.hot.flags.enabled)
  410. tz->trips.hot.flags.enabled = 0;
  411. result = acpi_bus_get_device(tz->handle, &device);
  412. if (result)
  413. return_VALUE(result);
  414. acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
  415. tz->trips.hot.flags.enabled);
  416. /* TBD: Call user-mode "sleep(S4)" function */
  417. return_VALUE(0);
  418. }
  419. static int acpi_thermal_passive(struct acpi_thermal *tz)
  420. {
  421. int result = 0;
  422. struct acpi_thermal_passive *passive = NULL;
  423. int trend = 0;
  424. int i = 0;
  425. ACPI_FUNCTION_TRACE("acpi_thermal_passive");
  426. if (!tz || !tz->trips.passive.flags.valid)
  427. return_VALUE(-EINVAL);
  428. passive = &(tz->trips.passive);
  429. /*
  430. * Above Trip?
  431. * -----------
  432. * Calculate the thermal trend (using the passive cooling equation)
  433. * and modify the performance limit for all passive cooling devices
  434. * accordingly. Note that we assume symmetry.
  435. */
  436. if (tz->temperature >= passive->temperature) {
  437. trend =
  438. (passive->tc1 * (tz->temperature - tz->last_temperature)) +
  439. (passive->tc2 * (tz->temperature - passive->temperature));
  440. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  441. "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
  442. trend, passive->tc1, tz->temperature,
  443. tz->last_temperature, passive->tc2,
  444. tz->temperature, passive->temperature));
  445. tz->trips.passive.flags.enabled = 1;
  446. /* Heating up? */
  447. if (trend > 0)
  448. for (i = 0; i < passive->devices.count; i++)
  449. acpi_processor_set_thermal_limit(passive->
  450. devices.
  451. handles[i],
  452. ACPI_PROCESSOR_LIMIT_INCREMENT);
  453. /* Cooling off? */
  454. else if (trend < 0)
  455. for (i = 0; i < passive->devices.count; i++)
  456. acpi_processor_set_thermal_limit(passive->
  457. devices.
  458. handles[i],
  459. ACPI_PROCESSOR_LIMIT_DECREMENT);
  460. }
  461. /*
  462. * Below Trip?
  463. * -----------
  464. * Implement passive cooling hysteresis to slowly increase performance
  465. * and avoid thrashing around the passive trip point. Note that we
  466. * assume symmetry.
  467. */
  468. else if (tz->trips.passive.flags.enabled) {
  469. for (i = 0; i < passive->devices.count; i++)
  470. result =
  471. acpi_processor_set_thermal_limit(passive->devices.
  472. handles[i],
  473. ACPI_PROCESSOR_LIMIT_DECREMENT);
  474. if (result == 1) {
  475. tz->trips.passive.flags.enabled = 0;
  476. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  477. "Disabling passive cooling (zone is cool)\n"));
  478. }
  479. }
  480. return_VALUE(0);
  481. }
  482. static int acpi_thermal_active(struct acpi_thermal *tz)
  483. {
  484. int result = 0;
  485. struct acpi_thermal_active *active = NULL;
  486. int i = 0;
  487. int j = 0;
  488. unsigned long maxtemp = 0;
  489. ACPI_FUNCTION_TRACE("acpi_thermal_active");
  490. if (!tz)
  491. return_VALUE(-EINVAL);
  492. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  493. active = &(tz->trips.active[i]);
  494. if (!active || !active->flags.valid)
  495. break;
  496. /*
  497. * Above Threshold?
  498. * ----------------
  499. * If not already enabled, turn ON all cooling devices
  500. * associated with this active threshold.
  501. */
  502. if (tz->temperature >= active->temperature) {
  503. if (active->temperature > maxtemp)
  504. tz->state.active_index = i, maxtemp =
  505. active->temperature;
  506. if (!active->flags.enabled) {
  507. for (j = 0; j < active->devices.count; j++) {
  508. result =
  509. acpi_bus_set_power(active->devices.
  510. handles[j],
  511. ACPI_STATE_D0);
  512. if (result) {
  513. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  514. "Unable to turn cooling device [%p] 'on'\n",
  515. active->
  516. devices.
  517. handles[j]));
  518. continue;
  519. }
  520. active->flags.enabled = 1;
  521. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  522. "Cooling device [%p] now 'on'\n",
  523. active->devices.
  524. handles[j]));
  525. }
  526. }
  527. }
  528. /*
  529. * Below Threshold?
  530. * ----------------
  531. * Turn OFF all cooling devices associated with this
  532. * threshold.
  533. */
  534. else if (active->flags.enabled) {
  535. for (j = 0; j < active->devices.count; j++) {
  536. result =
  537. acpi_bus_set_power(active->devices.
  538. handles[j],
  539. ACPI_STATE_D3);
  540. if (result) {
  541. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  542. "Unable to turn cooling device [%p] 'off'\n",
  543. active->devices.
  544. handles[j]));
  545. continue;
  546. }
  547. active->flags.enabled = 0;
  548. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  549. "Cooling device [%p] now 'off'\n",
  550. active->devices.handles[j]));
  551. }
  552. }
  553. }
  554. return_VALUE(0);
  555. }
  556. static void acpi_thermal_check(void *context);
  557. static void acpi_thermal_run(unsigned long data)
  558. {
  559. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  560. if (!tz->zombie)
  561. acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
  562. acpi_thermal_check, (void *)data);
  563. }
  564. static void acpi_thermal_check(void *data)
  565. {
  566. int result = 0;
  567. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  568. unsigned long sleep_time = 0;
  569. int i = 0;
  570. struct acpi_thermal_state state;
  571. ACPI_FUNCTION_TRACE("acpi_thermal_check");
  572. if (!tz) {
  573. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
  574. return_VOID;
  575. }
  576. state = tz->state;
  577. result = acpi_thermal_get_temperature(tz);
  578. if (result)
  579. return_VOID;
  580. memset(&tz->state, 0, sizeof(tz->state));
  581. /*
  582. * Check Trip Points
  583. * -----------------
  584. * Compare the current temperature to the trip point values to see
  585. * if we've entered one of the thermal policy states. Note that
  586. * this function determines when a state is entered, but the
  587. * individual policy decides when it is exited (e.g. hysteresis).
  588. */
  589. if (tz->trips.critical.flags.valid)
  590. state.critical |=
  591. (tz->temperature >= tz->trips.critical.temperature);
  592. if (tz->trips.hot.flags.valid)
  593. state.hot |= (tz->temperature >= tz->trips.hot.temperature);
  594. if (tz->trips.passive.flags.valid)
  595. state.passive |=
  596. (tz->temperature >= tz->trips.passive.temperature);
  597. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  598. if (tz->trips.active[i].flags.valid)
  599. state.active |=
  600. (tz->temperature >=
  601. tz->trips.active[i].temperature);
  602. /*
  603. * Invoke Policy
  604. * -------------
  605. * Separated from the above check to allow individual policy to
  606. * determine when to exit a given state.
  607. */
  608. if (state.critical)
  609. acpi_thermal_critical(tz);
  610. if (state.hot)
  611. acpi_thermal_hot(tz);
  612. if (state.passive)
  613. acpi_thermal_passive(tz);
  614. if (state.active)
  615. acpi_thermal_active(tz);
  616. /*
  617. * Calculate State
  618. * ---------------
  619. * Again, separated from the above two to allow independent policy
  620. * decisions.
  621. */
  622. if (tz->trips.critical.flags.enabled)
  623. tz->state.critical = 1;
  624. if (tz->trips.hot.flags.enabled)
  625. tz->state.hot = 1;
  626. if (tz->trips.passive.flags.enabled)
  627. tz->state.passive = 1;
  628. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  629. if (tz->trips.active[i].flags.enabled)
  630. tz->state.active = 1;
  631. /*
  632. * Calculate Sleep Time
  633. * --------------------
  634. * If we're in the passive state, use _TSP's value. Otherwise
  635. * use the default polling frequency (e.g. _TZP). If no polling
  636. * frequency is specified then we'll wait forever (at least until
  637. * a thermal event occurs). Note that _TSP and _TZD values are
  638. * given in 1/10th seconds (we must covert to milliseconds).
  639. */
  640. if (tz->state.passive)
  641. sleep_time = tz->trips.passive.tsp * 100;
  642. else if (tz->polling_frequency > 0)
  643. sleep_time = tz->polling_frequency * 100;
  644. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
  645. tz->name, tz->temperature, sleep_time));
  646. /*
  647. * Schedule Next Poll
  648. * ------------------
  649. */
  650. if (!sleep_time) {
  651. if (timer_pending(&(tz->timer)))
  652. del_timer(&(tz->timer));
  653. } else {
  654. if (timer_pending(&(tz->timer)))
  655. mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
  656. else {
  657. tz->timer.data = (unsigned long)tz;
  658. tz->timer.function = acpi_thermal_run;
  659. tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
  660. add_timer(&(tz->timer));
  661. }
  662. }
  663. return_VOID;
  664. }
  665. /* --------------------------------------------------------------------------
  666. FS Interface (/proc)
  667. -------------------------------------------------------------------------- */
  668. static struct proc_dir_entry *acpi_thermal_dir;
  669. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  670. {
  671. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  672. ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
  673. if (!tz)
  674. goto end;
  675. seq_puts(seq, "state: ");
  676. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  677. && !tz->state.active)
  678. seq_puts(seq, "ok\n");
  679. else {
  680. if (tz->state.critical)
  681. seq_puts(seq, "critical ");
  682. if (tz->state.hot)
  683. seq_puts(seq, "hot ");
  684. if (tz->state.passive)
  685. seq_puts(seq, "passive ");
  686. if (tz->state.active)
  687. seq_printf(seq, "active[%d]", tz->state.active_index);
  688. seq_puts(seq, "\n");
  689. }
  690. end:
  691. return_VALUE(0);
  692. }
  693. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  694. {
  695. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  696. }
  697. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  698. {
  699. int result = 0;
  700. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  701. ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
  702. if (!tz)
  703. goto end;
  704. result = acpi_thermal_get_temperature(tz);
  705. if (result)
  706. goto end;
  707. seq_printf(seq, "temperature: %ld C\n",
  708. KELVIN_TO_CELSIUS(tz->temperature));
  709. end:
  710. return_VALUE(0);
  711. }
  712. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  713. {
  714. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  715. }
  716. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  717. {
  718. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  719. int i = 0;
  720. int j = 0;
  721. ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
  722. if (!tz)
  723. goto end;
  724. if (tz->trips.critical.flags.valid)
  725. seq_printf(seq, "critical (S5): %ld C\n",
  726. KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
  727. if (tz->trips.hot.flags.valid)
  728. seq_printf(seq, "hot (S4): %ld C\n",
  729. KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
  730. if (tz->trips.passive.flags.valid) {
  731. seq_printf(seq,
  732. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  733. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  734. tz->trips.passive.tc1, tz->trips.passive.tc2,
  735. tz->trips.passive.tsp);
  736. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  737. seq_printf(seq, "0x%p ",
  738. tz->trips.passive.devices.handles[j]);
  739. }
  740. seq_puts(seq, "\n");
  741. }
  742. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  743. if (!(tz->trips.active[i].flags.valid))
  744. break;
  745. seq_printf(seq, "active[%d]: %ld C: devices=",
  746. i,
  747. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  748. for (j = 0; j < tz->trips.active[i].devices.count; j++)
  749. seq_printf(seq, "0x%p ",
  750. tz->trips.active[i].devices.handles[j]);
  751. seq_puts(seq, "\n");
  752. }
  753. end:
  754. return_VALUE(0);
  755. }
  756. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  757. {
  758. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  759. }
  760. static ssize_t
  761. acpi_thermal_write_trip_points(struct file *file,
  762. const char __user * buffer,
  763. size_t count, loff_t * ppos)
  764. {
  765. struct seq_file *m = (struct seq_file *)file->private_data;
  766. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  767. char *limit_string;
  768. int num, critical, hot, passive;
  769. int *active;
  770. int i = 0;
  771. ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
  772. limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
  773. if (!limit_string)
  774. return_VALUE(-ENOMEM);
  775. memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
  776. active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
  777. if (!active)
  778. return_VALUE(-ENOMEM);
  779. if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
  780. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  781. count = -EINVAL;
  782. goto end;
  783. }
  784. if (copy_from_user(limit_string, buffer, count)) {
  785. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  786. count = -EFAULT;
  787. goto end;
  788. }
  789. limit_string[count] = '\0';
  790. num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
  791. &critical, &hot, &passive,
  792. &active[0], &active[1], &active[2], &active[3], &active[4],
  793. &active[5], &active[6], &active[7], &active[8],
  794. &active[9]);
  795. if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
  796. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  797. count = -EINVAL;
  798. goto end;
  799. }
  800. tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
  801. tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
  802. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
  803. for (i = 0; i < num - 3; i++) {
  804. if (!(tz->trips.active[i].flags.valid))
  805. break;
  806. tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
  807. }
  808. end:
  809. kfree(active);
  810. kfree(limit_string);
  811. return_VALUE(count);
  812. }
  813. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  814. {
  815. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  816. ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
  817. if (!tz)
  818. goto end;
  819. if (!tz->flags.cooling_mode) {
  820. seq_puts(seq, "<setting not supported>\n");
  821. }
  822. if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
  823. seq_printf(seq, "cooling mode: critical\n");
  824. else
  825. seq_printf(seq, "cooling mode: %s\n",
  826. tz->cooling_mode ? "passive" : "active");
  827. end:
  828. return_VALUE(0);
  829. }
  830. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  831. {
  832. return single_open(file, acpi_thermal_cooling_seq_show,
  833. PDE(inode)->data);
  834. }
  835. static ssize_t
  836. acpi_thermal_write_cooling_mode(struct file *file,
  837. const char __user * buffer,
  838. size_t count, loff_t * ppos)
  839. {
  840. struct seq_file *m = (struct seq_file *)file->private_data;
  841. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  842. int result = 0;
  843. char mode_string[12] = { '\0' };
  844. ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
  845. if (!tz || (count > sizeof(mode_string) - 1))
  846. return_VALUE(-EINVAL);
  847. if (!tz->flags.cooling_mode)
  848. return_VALUE(-ENODEV);
  849. if (copy_from_user(mode_string, buffer, count))
  850. return_VALUE(-EFAULT);
  851. mode_string[count] = '\0';
  852. result = acpi_thermal_set_cooling_mode(tz,
  853. simple_strtoul(mode_string, NULL,
  854. 0));
  855. if (result)
  856. return_VALUE(result);
  857. acpi_thermal_check(tz);
  858. return_VALUE(count);
  859. }
  860. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  861. {
  862. struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
  863. ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
  864. if (!tz)
  865. goto end;
  866. if (!tz->polling_frequency) {
  867. seq_puts(seq, "<polling disabled>\n");
  868. goto end;
  869. }
  870. seq_printf(seq, "polling frequency: %lu seconds\n",
  871. (tz->polling_frequency / 10));
  872. end:
  873. return_VALUE(0);
  874. }
  875. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  876. {
  877. return single_open(file, acpi_thermal_polling_seq_show,
  878. PDE(inode)->data);
  879. }
  880. static ssize_t
  881. acpi_thermal_write_polling(struct file *file,
  882. const char __user * buffer,
  883. size_t count, loff_t * ppos)
  884. {
  885. struct seq_file *m = (struct seq_file *)file->private_data;
  886. struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
  887. int result = 0;
  888. char polling_string[12] = { '\0' };
  889. int seconds = 0;
  890. ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
  891. if (!tz || (count > sizeof(polling_string) - 1))
  892. return_VALUE(-EINVAL);
  893. if (copy_from_user(polling_string, buffer, count))
  894. return_VALUE(-EFAULT);
  895. polling_string[count] = '\0';
  896. seconds = simple_strtoul(polling_string, NULL, 0);
  897. result = acpi_thermal_set_polling(tz, seconds);
  898. if (result)
  899. return_VALUE(result);
  900. acpi_thermal_check(tz);
  901. return_VALUE(count);
  902. }
  903. static int acpi_thermal_add_fs(struct acpi_device *device)
  904. {
  905. struct proc_dir_entry *entry = NULL;
  906. ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
  907. if (!acpi_device_dir(device)) {
  908. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  909. acpi_thermal_dir);
  910. if (!acpi_device_dir(device))
  911. return_VALUE(-ENODEV);
  912. acpi_device_dir(device)->owner = THIS_MODULE;
  913. }
  914. /* 'state' [R] */
  915. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  916. S_IRUGO, acpi_device_dir(device));
  917. if (!entry)
  918. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  919. "Unable to create '%s' fs entry\n",
  920. ACPI_THERMAL_FILE_STATE));
  921. else {
  922. entry->proc_fops = &acpi_thermal_state_fops;
  923. entry->data = acpi_driver_data(device);
  924. entry->owner = THIS_MODULE;
  925. }
  926. /* 'temperature' [R] */
  927. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  928. S_IRUGO, acpi_device_dir(device));
  929. if (!entry)
  930. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  931. "Unable to create '%s' fs entry\n",
  932. ACPI_THERMAL_FILE_TEMPERATURE));
  933. else {
  934. entry->proc_fops = &acpi_thermal_temp_fops;
  935. entry->data = acpi_driver_data(device);
  936. entry->owner = THIS_MODULE;
  937. }
  938. /* 'trip_points' [R/W] */
  939. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  940. S_IFREG | S_IRUGO | S_IWUSR,
  941. acpi_device_dir(device));
  942. if (!entry)
  943. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  944. "Unable to create '%s' fs entry\n",
  945. ACPI_THERMAL_FILE_TRIP_POINTS));
  946. else {
  947. entry->proc_fops = &acpi_thermal_trip_fops;
  948. entry->data = acpi_driver_data(device);
  949. entry->owner = THIS_MODULE;
  950. }
  951. /* 'cooling_mode' [R/W] */
  952. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  953. S_IFREG | S_IRUGO | S_IWUSR,
  954. acpi_device_dir(device));
  955. if (!entry)
  956. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  957. "Unable to create '%s' fs entry\n",
  958. ACPI_THERMAL_FILE_COOLING_MODE));
  959. else {
  960. entry->proc_fops = &acpi_thermal_cooling_fops;
  961. entry->data = acpi_driver_data(device);
  962. entry->owner = THIS_MODULE;
  963. }
  964. /* 'polling_frequency' [R/W] */
  965. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  966. S_IFREG | S_IRUGO | S_IWUSR,
  967. acpi_device_dir(device));
  968. if (!entry)
  969. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  970. "Unable to create '%s' fs entry\n",
  971. ACPI_THERMAL_FILE_POLLING_FREQ));
  972. else {
  973. entry->proc_fops = &acpi_thermal_polling_fops;
  974. entry->data = acpi_driver_data(device);
  975. entry->owner = THIS_MODULE;
  976. }
  977. return_VALUE(0);
  978. }
  979. static int acpi_thermal_remove_fs(struct acpi_device *device)
  980. {
  981. ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
  982. if (acpi_device_dir(device)) {
  983. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  984. acpi_device_dir(device));
  985. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  986. acpi_device_dir(device));
  987. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  988. acpi_device_dir(device));
  989. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  990. acpi_device_dir(device));
  991. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  992. acpi_device_dir(device));
  993. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  994. acpi_device_dir(device) = NULL;
  995. }
  996. return_VALUE(0);
  997. }
  998. /* --------------------------------------------------------------------------
  999. Driver Interface
  1000. -------------------------------------------------------------------------- */
  1001. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  1002. {
  1003. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  1004. struct acpi_device *device = NULL;
  1005. ACPI_FUNCTION_TRACE("acpi_thermal_notify");
  1006. if (!tz)
  1007. return_VOID;
  1008. if (acpi_bus_get_device(tz->handle, &device))
  1009. return_VOID;
  1010. switch (event) {
  1011. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1012. acpi_thermal_check(tz);
  1013. break;
  1014. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1015. acpi_thermal_get_trip_points(tz);
  1016. acpi_thermal_check(tz);
  1017. acpi_bus_generate_event(device, event, 0);
  1018. break;
  1019. case ACPI_THERMAL_NOTIFY_DEVICES:
  1020. if (tz->flags.devices)
  1021. acpi_thermal_get_devices(tz);
  1022. acpi_bus_generate_event(device, event, 0);
  1023. break;
  1024. default:
  1025. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1026. "Unsupported event [0x%x]\n", event));
  1027. break;
  1028. }
  1029. return_VOID;
  1030. }
  1031. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1032. {
  1033. int result = 0;
  1034. ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
  1035. if (!tz)
  1036. return_VALUE(-EINVAL);
  1037. /* Get temperature [_TMP] (required) */
  1038. result = acpi_thermal_get_temperature(tz);
  1039. if (result)
  1040. return_VALUE(result);
  1041. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1042. result = acpi_thermal_get_trip_points(tz);
  1043. if (result)
  1044. return_VALUE(result);
  1045. /* Set the cooling mode [_SCP] to active cooling (default) */
  1046. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1047. if (!result)
  1048. tz->flags.cooling_mode = 1;
  1049. else {
  1050. /* Oh,we have not _SCP method.
  1051. Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
  1052. tz->flags.cooling_mode = 0;
  1053. if (tz->trips.active[0].flags.valid
  1054. && tz->trips.passive.flags.valid) {
  1055. if (tz->trips.passive.temperature >
  1056. tz->trips.active[0].temperature)
  1057. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1058. else
  1059. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1060. } else if (!tz->trips.active[0].flags.valid
  1061. && tz->trips.passive.flags.valid) {
  1062. tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
  1063. } else if (tz->trips.active[0].flags.valid
  1064. && !tz->trips.passive.flags.valid) {
  1065. tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
  1066. } else {
  1067. /* _ACx and _PSV are optional, but _CRT is required */
  1068. tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
  1069. }
  1070. }
  1071. /* Get default polling frequency [_TZP] (optional) */
  1072. if (tzp)
  1073. tz->polling_frequency = tzp;
  1074. else
  1075. acpi_thermal_get_polling_frequency(tz);
  1076. /* Get devices in this thermal zone [_TZD] (optional) */
  1077. result = acpi_thermal_get_devices(tz);
  1078. if (!result)
  1079. tz->flags.devices = 1;
  1080. return_VALUE(0);
  1081. }
  1082. static int acpi_thermal_add(struct acpi_device *device)
  1083. {
  1084. int result = 0;
  1085. acpi_status status = AE_OK;
  1086. struct acpi_thermal *tz = NULL;
  1087. ACPI_FUNCTION_TRACE("acpi_thermal_add");
  1088. if (!device)
  1089. return_VALUE(-EINVAL);
  1090. tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1091. if (!tz)
  1092. return_VALUE(-ENOMEM);
  1093. memset(tz, 0, sizeof(struct acpi_thermal));
  1094. tz->handle = device->handle;
  1095. strcpy(tz->name, device->pnp.bus_id);
  1096. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1097. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1098. acpi_driver_data(device) = tz;
  1099. result = acpi_thermal_get_info(tz);
  1100. if (result)
  1101. goto end;
  1102. result = acpi_thermal_add_fs(device);
  1103. if (result)
  1104. return_VALUE(result);
  1105. init_timer(&tz->timer);
  1106. acpi_thermal_check(tz);
  1107. status = acpi_install_notify_handler(tz->handle,
  1108. ACPI_DEVICE_NOTIFY,
  1109. acpi_thermal_notify, tz);
  1110. if (ACPI_FAILURE(status)) {
  1111. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1112. "Error installing notify handler\n"));
  1113. result = -ENODEV;
  1114. goto end;
  1115. }
  1116. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1117. acpi_device_name(device), acpi_device_bid(device),
  1118. KELVIN_TO_CELSIUS(tz->temperature));
  1119. end:
  1120. if (result) {
  1121. acpi_thermal_remove_fs(device);
  1122. kfree(tz);
  1123. }
  1124. return_VALUE(result);
  1125. }
  1126. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1127. {
  1128. acpi_status status = AE_OK;
  1129. struct acpi_thermal *tz = NULL;
  1130. ACPI_FUNCTION_TRACE("acpi_thermal_remove");
  1131. if (!device || !acpi_driver_data(device))
  1132. return_VALUE(-EINVAL);
  1133. tz = (struct acpi_thermal *)acpi_driver_data(device);
  1134. /* avoid timer adding new defer task */
  1135. tz->zombie = 1;
  1136. /* wait for running timer (on other CPUs) finish */
  1137. del_timer_sync(&(tz->timer));
  1138. /* synchronize deferred task */
  1139. acpi_os_wait_events_complete(NULL);
  1140. /* deferred task may reinsert timer */
  1141. del_timer_sync(&(tz->timer));
  1142. status = acpi_remove_notify_handler(tz->handle,
  1143. ACPI_DEVICE_NOTIFY,
  1144. acpi_thermal_notify);
  1145. if (ACPI_FAILURE(status))
  1146. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  1147. "Error removing notify handler\n"));
  1148. /* Terminate policy */
  1149. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1150. tz->trips.passive.flags.enabled = 0;
  1151. acpi_thermal_passive(tz);
  1152. }
  1153. if (tz->trips.active[0].flags.valid
  1154. && tz->trips.active[0].flags.enabled) {
  1155. tz->trips.active[0].flags.enabled = 0;
  1156. acpi_thermal_active(tz);
  1157. }
  1158. acpi_thermal_remove_fs(device);
  1159. kfree(tz);
  1160. return_VALUE(0);
  1161. }
  1162. static int __init acpi_thermal_init(void)
  1163. {
  1164. int result = 0;
  1165. ACPI_FUNCTION_TRACE("acpi_thermal_init");
  1166. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1167. if (!acpi_thermal_dir)
  1168. return_VALUE(-ENODEV);
  1169. acpi_thermal_dir->owner = THIS_MODULE;
  1170. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1171. if (result < 0) {
  1172. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1173. return_VALUE(-ENODEV);
  1174. }
  1175. return_VALUE(0);
  1176. }
  1177. static void __exit acpi_thermal_exit(void)
  1178. {
  1179. ACPI_FUNCTION_TRACE("acpi_thermal_exit");
  1180. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1181. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1182. return_VOID;
  1183. }
  1184. module_init(acpi_thermal_init);
  1185. module_exit(acpi_thermal_exit);