power.c 17 KB

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