power.c 16 KB

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