power.c 17 KB

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