power.c 15 KB

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