power.c 16 KB

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