power.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
  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. /*
  26. * ACPI power-managed devices may be controlled in two ways:
  27. * 1. via "Device Specific (D-State) Control"
  28. * 2. via "Power Resource Control".
  29. * This module is used to manage devices relying on Power Resource Control.
  30. *
  31. * An ACPI "power resource object" describes a software controllable power
  32. * plane, clock plane, or other resource used by a power managed device.
  33. * A device may rely on multiple power resources, and a power resource
  34. * may be shared by multiple devices.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #define _COMPONENT ACPI_POWER_COMPONENT
  45. ACPI_MODULE_NAME("power");
  46. #define ACPI_POWER_COMPONENT 0x00800000
  47. #define ACPI_POWER_CLASS "power_resource"
  48. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  49. #define ACPI_POWER_FILE_INFO "info"
  50. #define ACPI_POWER_FILE_STATUS "state"
  51. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  52. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  53. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  54. static int acpi_power_add(struct acpi_device *device);
  55. static int acpi_power_remove(struct acpi_device *device, int type);
  56. static int acpi_power_open_fs(struct inode *inode, struct file *file);
  57. static struct acpi_driver acpi_power_driver = {
  58. .name = "power",
  59. .class = ACPI_POWER_CLASS,
  60. .ids = ACPI_POWER_HID,
  61. .ops = {
  62. .add = acpi_power_add,
  63. .remove = acpi_power_remove,
  64. },
  65. };
  66. struct acpi_power_resource {
  67. struct acpi_device * device;
  68. acpi_bus_id name;
  69. u32 system_level;
  70. u32 order;
  71. int state;
  72. int references;
  73. };
  74. static struct list_head acpi_power_resource_list;
  75. static const struct file_operations acpi_power_fops = {
  76. .open = acpi_power_open_fs,
  77. .read = seq_read,
  78. .llseek = seq_lseek,
  79. .release = single_release,
  80. };
  81. /* --------------------------------------------------------------------------
  82. Power Resource Management
  83. -------------------------------------------------------------------------- */
  84. static int
  85. acpi_power_get_context(acpi_handle handle,
  86. struct acpi_power_resource **resource)
  87. {
  88. int result = 0;
  89. struct acpi_device *device = NULL;
  90. if (!resource)
  91. return -ENODEV;
  92. result = acpi_bus_get_device(handle, &device);
  93. if (result) {
  94. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  95. return result;
  96. }
  97. *resource = acpi_driver_data(device);
  98. if (!resource)
  99. return -ENODEV;
  100. return 0;
  101. }
  102. static int acpi_power_get_state(struct acpi_power_resource *resource)
  103. {
  104. acpi_status status = AE_OK;
  105. unsigned long sta = 0;
  106. if (!resource)
  107. return -EINVAL;
  108. status = acpi_evaluate_integer(resource->device->handle, "_STA", NULL, &sta);
  109. if (ACPI_FAILURE(status))
  110. return -ENODEV;
  111. if (sta & 0x01)
  112. resource->state = ACPI_POWER_RESOURCE_STATE_ON;
  113. else
  114. resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
  115. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  116. resource->name, resource->state ? "on" : "off"));
  117. return 0;
  118. }
  119. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  120. {
  121. int result = 0;
  122. struct acpi_power_resource *resource = NULL;
  123. u32 i = 0;
  124. if (!list || !state)
  125. return -EINVAL;
  126. /* The state of the list is 'on' IFF all resources are 'on'. */
  127. for (i = 0; i < list->count; i++) {
  128. result = acpi_power_get_context(list->handles[i], &resource);
  129. if (result)
  130. return result;
  131. result = acpi_power_get_state(resource);
  132. if (result)
  133. return result;
  134. *state = resource->state;
  135. if (*state != ACPI_POWER_RESOURCE_STATE_ON)
  136. break;
  137. }
  138. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  139. *state ? "on" : "off"));
  140. return result;
  141. }
  142. static int acpi_power_on(acpi_handle handle)
  143. {
  144. int result = 0;
  145. acpi_status status = AE_OK;
  146. struct acpi_device *device = NULL;
  147. struct acpi_power_resource *resource = NULL;
  148. result = acpi_power_get_context(handle, &resource);
  149. if (result)
  150. return result;
  151. resource->references++;
  152. if ((resource->references > 1)
  153. || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
  154. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
  155. resource->name));
  156. return 0;
  157. }
  158. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  159. if (ACPI_FAILURE(status))
  160. return -ENODEV;
  161. result = acpi_power_get_state(resource);
  162. if (result)
  163. return result;
  164. if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
  165. return -ENOEXEC;
  166. /* Update the power resource's _device_ power state */
  167. device = resource->device;
  168. resource->device->power.state = ACPI_STATE_D0;
  169. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
  170. resource->name));
  171. return 0;
  172. }
  173. static int acpi_power_off_device(acpi_handle handle)
  174. {
  175. int result = 0;
  176. acpi_status status = AE_OK;
  177. struct acpi_power_resource *resource = NULL;
  178. result = acpi_power_get_context(handle, &resource);
  179. if (result)
  180. return result;
  181. if (resource->references)
  182. resource->references--;
  183. if (resource->references) {
  184. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  185. "Resource [%s] is still in use, dereferencing\n",
  186. resource->device->pnp.bus_id));
  187. return 0;
  188. }
  189. if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
  190. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
  191. resource->device->pnp.bus_id));
  192. return 0;
  193. }
  194. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  195. if (ACPI_FAILURE(status))
  196. return -ENODEV;
  197. result = acpi_power_get_state(resource);
  198. if (result)
  199. return result;
  200. if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
  201. return -ENOEXEC;
  202. /* Update the power resource's _device_ power state */
  203. resource->device->power.state = ACPI_STATE_D3;
  204. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
  205. resource->name));
  206. return 0;
  207. }
  208. /*
  209. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  210. * 1. Power on the power resources required for the wakeup device
  211. * 2. Enable _PSW (power state wake) for the device if present
  212. */
  213. int acpi_enable_wakeup_device_power(struct acpi_device *dev)
  214. {
  215. union acpi_object arg = { ACPI_TYPE_INTEGER };
  216. struct acpi_object_list arg_list = { 1, &arg };
  217. acpi_status status = AE_OK;
  218. int i;
  219. int ret = 0;
  220. if (!dev || !dev->wakeup.flags.valid)
  221. return -1;
  222. arg.integer.value = 1;
  223. /* Open power resource */
  224. for (i = 0; i < dev->wakeup.resources.count; i++) {
  225. ret = acpi_power_on(dev->wakeup.resources.handles[i]);
  226. if (ret) {
  227. printk(KERN_ERR PREFIX "Transition power state\n");
  228. dev->wakeup.flags.valid = 0;
  229. return -1;
  230. }
  231. }
  232. /* Execute PSW */
  233. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  234. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  235. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  236. dev->wakeup.flags.valid = 0;
  237. ret = -1;
  238. }
  239. return ret;
  240. }
  241. /*
  242. * Shutdown a wakeup device, counterpart of above method
  243. * 1. Disable _PSW (power state wake)
  244. * 2. Shutdown down the power resources
  245. */
  246. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  247. {
  248. union acpi_object arg = { ACPI_TYPE_INTEGER };
  249. struct acpi_object_list arg_list = { 1, &arg };
  250. acpi_status status = AE_OK;
  251. int i;
  252. int ret = 0;
  253. if (!dev || !dev->wakeup.flags.valid)
  254. return -1;
  255. arg.integer.value = 0;
  256. /* Execute PSW */
  257. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  258. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  259. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  260. dev->wakeup.flags.valid = 0;
  261. return -1;
  262. }
  263. /* Close power resource */
  264. for (i = 0; i < dev->wakeup.resources.count; i++) {
  265. ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
  266. if (ret) {
  267. printk(KERN_ERR PREFIX "Transition power state\n");
  268. dev->wakeup.flags.valid = 0;
  269. return -1;
  270. }
  271. }
  272. return ret;
  273. }
  274. /* --------------------------------------------------------------------------
  275. Device Power Management
  276. -------------------------------------------------------------------------- */
  277. int acpi_power_get_inferred_state(struct acpi_device *device)
  278. {
  279. int result = 0;
  280. struct acpi_handle_list *list = NULL;
  281. int list_state = 0;
  282. int i = 0;
  283. if (!device)
  284. return -EINVAL;
  285. device->power.state = ACPI_STATE_UNKNOWN;
  286. /*
  287. * We know a device's inferred power state when all the resources
  288. * required for a given D-state are 'on'.
  289. */
  290. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  291. list = &device->power.states[i].resources;
  292. if (list->count < 1)
  293. continue;
  294. result = acpi_power_get_list_state(list, &list_state);
  295. if (result)
  296. return result;
  297. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  298. device->power.state = i;
  299. return 0;
  300. }
  301. }
  302. device->power.state = ACPI_STATE_D3;
  303. return 0;
  304. }
  305. int acpi_power_transition(struct acpi_device *device, int state)
  306. {
  307. int result = 0;
  308. struct acpi_handle_list *cl = NULL; /* Current Resources */
  309. struct acpi_handle_list *tl = NULL; /* Target Resources */
  310. int i = 0;
  311. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  312. return -EINVAL;
  313. if ((device->power.state < ACPI_STATE_D0)
  314. || (device->power.state > ACPI_STATE_D3))
  315. return -ENODEV;
  316. cl = &device->power.states[device->power.state].resources;
  317. tl = &device->power.states[state].resources;
  318. device->power.state = ACPI_STATE_UNKNOWN;
  319. if (!cl->count && !tl->count) {
  320. result = -ENODEV;
  321. goto end;
  322. }
  323. /* TBD: Resources must be ordered. */
  324. /*
  325. * First we reference all power resources required in the target list
  326. * (e.g. so the device doesn't lose power while transitioning).
  327. */
  328. for (i = 0; i < tl->count; i++) {
  329. result = acpi_power_on(tl->handles[i]);
  330. if (result)
  331. goto end;
  332. }
  333. /*
  334. * Then we dereference all power resources used in the current list.
  335. */
  336. for (i = 0; i < cl->count; i++) {
  337. result = acpi_power_off_device(cl->handles[i]);
  338. if (result)
  339. goto end;
  340. }
  341. /* We shouldn't change the state till all above operations succeed */
  342. device->power.state = state;
  343. end:
  344. if (result)
  345. printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
  346. device->pnp.bus_id, state);
  347. return result;
  348. }
  349. /* --------------------------------------------------------------------------
  350. FS Interface (/proc)
  351. -------------------------------------------------------------------------- */
  352. static struct proc_dir_entry *acpi_power_dir;
  353. static int acpi_power_seq_show(struct seq_file *seq, void *offset)
  354. {
  355. struct acpi_power_resource *resource = NULL;
  356. resource = seq->private;
  357. if (!resource)
  358. goto end;
  359. seq_puts(seq, "state: ");
  360. switch (resource->state) {
  361. case ACPI_POWER_RESOURCE_STATE_ON:
  362. seq_puts(seq, "on\n");
  363. break;
  364. case ACPI_POWER_RESOURCE_STATE_OFF:
  365. seq_puts(seq, "off\n");
  366. break;
  367. default:
  368. seq_puts(seq, "unknown\n");
  369. break;
  370. }
  371. seq_printf(seq, "system level: S%d\n"
  372. "order: %d\n"
  373. "reference count: %d\n",
  374. resource->system_level,
  375. resource->order, resource->references);
  376. end:
  377. return 0;
  378. }
  379. static int acpi_power_open_fs(struct inode *inode, struct file *file)
  380. {
  381. return single_open(file, acpi_power_seq_show, PDE(inode)->data);
  382. }
  383. static int acpi_power_add_fs(struct acpi_device *device)
  384. {
  385. struct proc_dir_entry *entry = NULL;
  386. if (!device)
  387. return -EINVAL;
  388. if (!acpi_device_dir(device)) {
  389. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  390. acpi_power_dir);
  391. if (!acpi_device_dir(device))
  392. return -ENODEV;
  393. }
  394. /* 'status' [R] */
  395. entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
  396. S_IRUGO, acpi_device_dir(device));
  397. if (!entry)
  398. return -EIO;
  399. else {
  400. entry->proc_fops = &acpi_power_fops;
  401. entry->data = acpi_driver_data(device);
  402. }
  403. return 0;
  404. }
  405. static int acpi_power_remove_fs(struct acpi_device *device)
  406. {
  407. if (acpi_device_dir(device)) {
  408. remove_proc_entry(ACPI_POWER_FILE_STATUS,
  409. acpi_device_dir(device));
  410. remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
  411. acpi_device_dir(device) = NULL;
  412. }
  413. return 0;
  414. }
  415. /* --------------------------------------------------------------------------
  416. Driver Interface
  417. -------------------------------------------------------------------------- */
  418. static int acpi_power_add(struct acpi_device *device)
  419. {
  420. int result = 0;
  421. acpi_status status = AE_OK;
  422. struct acpi_power_resource *resource = NULL;
  423. union acpi_object acpi_object;
  424. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  425. if (!device)
  426. return -EINVAL;
  427. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  428. if (!resource)
  429. return -ENOMEM;
  430. resource->device = device;
  431. strcpy(resource->name, device->pnp.bus_id);
  432. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  433. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  434. acpi_driver_data(device) = resource;
  435. /* Evalute the object to get the system level and resource order. */
  436. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  437. if (ACPI_FAILURE(status)) {
  438. result = -ENODEV;
  439. goto end;
  440. }
  441. resource->system_level = acpi_object.power_resource.system_level;
  442. resource->order = acpi_object.power_resource.resource_order;
  443. result = acpi_power_get_state(resource);
  444. if (result)
  445. goto end;
  446. switch (resource->state) {
  447. case ACPI_POWER_RESOURCE_STATE_ON:
  448. device->power.state = ACPI_STATE_D0;
  449. break;
  450. case ACPI_POWER_RESOURCE_STATE_OFF:
  451. device->power.state = ACPI_STATE_D3;
  452. break;
  453. default:
  454. device->power.state = ACPI_STATE_UNKNOWN;
  455. break;
  456. }
  457. result = acpi_power_add_fs(device);
  458. if (result)
  459. goto end;
  460. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  461. acpi_device_bid(device), resource->state ? "on" : "off");
  462. end:
  463. if (result)
  464. kfree(resource);
  465. return result;
  466. }
  467. static int acpi_power_remove(struct acpi_device *device, int type)
  468. {
  469. struct acpi_power_resource *resource = NULL;
  470. if (!device || !acpi_driver_data(device))
  471. return -EINVAL;
  472. resource = acpi_driver_data(device);
  473. acpi_power_remove_fs(device);
  474. kfree(resource);
  475. return 0;
  476. }
  477. static int __init acpi_power_init(void)
  478. {
  479. int result = 0;
  480. if (acpi_disabled)
  481. return 0;
  482. INIT_LIST_HEAD(&acpi_power_resource_list);
  483. acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
  484. if (!acpi_power_dir)
  485. return -ENODEV;
  486. result = acpi_bus_register_driver(&acpi_power_driver);
  487. if (result < 0) {
  488. remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
  489. return -ENODEV;
  490. }
  491. return 0;
  492. }
  493. subsys_initcall(acpi_power_init);