power.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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("power");
  46. #define ACPI_POWER_CLASS "power_resource"
  47. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  48. #define ACPI_POWER_FILE_INFO "info"
  49. #define ACPI_POWER_FILE_STATUS "state"
  50. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  51. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  52. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  53. int acpi_power_nocheck;
  54. module_param_named(power_nocheck, acpi_power_nocheck, bool, 000);
  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_resume(struct acpi_device *device);
  58. static int acpi_power_open_fs(struct inode *inode, struct file *file);
  59. static struct acpi_device_id power_device_ids[] = {
  60. {ACPI_POWER_HID, 0},
  61. {"", 0},
  62. };
  63. MODULE_DEVICE_TABLE(acpi, power_device_ids);
  64. static struct acpi_driver acpi_power_driver = {
  65. .name = "power",
  66. .class = ACPI_POWER_CLASS,
  67. .ids = power_device_ids,
  68. .ops = {
  69. .add = acpi_power_add,
  70. .remove = acpi_power_remove,
  71. .resume = acpi_power_resume,
  72. },
  73. };
  74. struct acpi_power_reference {
  75. struct list_head node;
  76. struct acpi_device *device;
  77. };
  78. struct acpi_power_resource {
  79. struct acpi_device * device;
  80. acpi_bus_id name;
  81. u32 system_level;
  82. u32 order;
  83. struct mutex resource_lock;
  84. struct list_head reference;
  85. };
  86. static struct list_head acpi_power_resource_list;
  87. static const struct file_operations acpi_power_fops = {
  88. .owner = THIS_MODULE,
  89. .open = acpi_power_open_fs,
  90. .read = seq_read,
  91. .llseek = seq_lseek,
  92. .release = single_release,
  93. };
  94. /* --------------------------------------------------------------------------
  95. Power Resource Management
  96. -------------------------------------------------------------------------- */
  97. static int
  98. acpi_power_get_context(acpi_handle handle,
  99. struct acpi_power_resource **resource)
  100. {
  101. int result = 0;
  102. struct acpi_device *device = NULL;
  103. if (!resource)
  104. return -ENODEV;
  105. result = acpi_bus_get_device(handle, &device);
  106. if (result) {
  107. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  108. return result;
  109. }
  110. *resource = acpi_driver_data(device);
  111. if (!*resource)
  112. return -ENODEV;
  113. return 0;
  114. }
  115. static int acpi_power_get_state(acpi_handle handle, int *state)
  116. {
  117. acpi_status status = AE_OK;
  118. unsigned long long sta = 0;
  119. char node_name[5];
  120. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  121. if (!handle || !state)
  122. return -EINVAL;
  123. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  124. if (ACPI_FAILURE(status))
  125. return -ENODEV;
  126. *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
  127. ACPI_POWER_RESOURCE_STATE_OFF;
  128. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  129. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  130. node_name,
  131. *state ? "on" : "off"));
  132. return 0;
  133. }
  134. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  135. {
  136. int result = 0, state1;
  137. u32 i = 0;
  138. if (!list || !state)
  139. return -EINVAL;
  140. /* The state of the list is 'on' IFF all resources are 'on'. */
  141. /* */
  142. for (i = 0; i < list->count; i++) {
  143. /*
  144. * The state of the power resource can be obtained by
  145. * using the ACPI handle. In such case it is unnecessary to
  146. * get the Power resource first and then get its state again.
  147. */
  148. result = acpi_power_get_state(list->handles[i], &state1);
  149. if (result)
  150. return result;
  151. *state = state1;
  152. if (*state != ACPI_POWER_RESOURCE_STATE_ON)
  153. break;
  154. }
  155. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  156. *state ? "on" : "off"));
  157. return result;
  158. }
  159. static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
  160. {
  161. int result = 0, state;
  162. int found = 0;
  163. acpi_status status = AE_OK;
  164. struct acpi_power_resource *resource = NULL;
  165. struct list_head *node, *next;
  166. struct acpi_power_reference *ref;
  167. result = acpi_power_get_context(handle, &resource);
  168. if (result)
  169. return result;
  170. mutex_lock(&resource->resource_lock);
  171. list_for_each_safe(node, next, &resource->reference) {
  172. ref = container_of(node, struct acpi_power_reference, node);
  173. if (dev->handle == ref->device->handle) {
  174. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
  175. dev->pnp.bus_id, resource->name));
  176. found = 1;
  177. break;
  178. }
  179. }
  180. if (!found) {
  181. ref = kmalloc(sizeof (struct acpi_power_reference),
  182. irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
  183. if (!ref) {
  184. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
  185. mutex_unlock(&resource->resource_lock);
  186. return -ENOMEM;
  187. }
  188. list_add_tail(&ref->node, &resource->reference);
  189. ref->device = dev;
  190. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
  191. dev->pnp.bus_id, resource->name));
  192. }
  193. mutex_unlock(&resource->resource_lock);
  194. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  195. if (ACPI_FAILURE(status))
  196. return -ENODEV;
  197. if (!acpi_power_nocheck) {
  198. /*
  199. * If acpi_power_nocheck is set, it is unnecessary to check
  200. * the power state after power transition.
  201. */
  202. result = acpi_power_get_state(resource->device->handle,
  203. &state);
  204. if (result)
  205. return result;
  206. if (state != ACPI_POWER_RESOURCE_STATE_ON)
  207. return -ENOEXEC;
  208. }
  209. /* Update the power resource's _device_ power state */
  210. resource->device->power.state = ACPI_STATE_D0;
  211. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
  212. resource->name));
  213. return 0;
  214. }
  215. static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
  216. {
  217. int result = 0, state;
  218. acpi_status status = AE_OK;
  219. struct acpi_power_resource *resource = NULL;
  220. struct list_head *node, *next;
  221. struct acpi_power_reference *ref;
  222. result = acpi_power_get_context(handle, &resource);
  223. if (result)
  224. return result;
  225. mutex_lock(&resource->resource_lock);
  226. list_for_each_safe(node, next, &resource->reference) {
  227. ref = container_of(node, struct acpi_power_reference, node);
  228. if (dev->handle == ref->device->handle) {
  229. list_del(&ref->node);
  230. kfree(ref);
  231. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
  232. dev->pnp.bus_id, resource->name));
  233. break;
  234. }
  235. }
  236. if (!list_empty(&resource->reference)) {
  237. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
  238. resource->name));
  239. mutex_unlock(&resource->resource_lock);
  240. return 0;
  241. }
  242. mutex_unlock(&resource->resource_lock);
  243. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  244. if (ACPI_FAILURE(status))
  245. return -ENODEV;
  246. if (!acpi_power_nocheck) {
  247. /*
  248. * If acpi_power_nocheck is set, it is unnecessary to check
  249. * the power state after power transition.
  250. */
  251. result = acpi_power_get_state(handle, &state);
  252. if (result)
  253. return result;
  254. if (state != ACPI_POWER_RESOURCE_STATE_OFF)
  255. return -ENOEXEC;
  256. }
  257. /* Update the power resource's _device_ power state */
  258. resource->device->power.state = ACPI_STATE_D3;
  259. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
  260. resource->name));
  261. return 0;
  262. }
  263. /**
  264. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  265. * ACPI 3.0) _PSW (Power State Wake)
  266. * @dev: Device to handle.
  267. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  268. * @sleep_state: Target sleep state of the system.
  269. * @dev_state: Target power state of the device.
  270. *
  271. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  272. * State Wake) for the device, if present. On failure reset the device's
  273. * wakeup.flags.valid flag.
  274. *
  275. * RETURN VALUE:
  276. * 0 if either _DSW or _PSW has been successfully executed
  277. * 0 if neither _DSW nor _PSW has been found
  278. * -ENODEV if the execution of either _DSW or _PSW has failed
  279. */
  280. int acpi_device_sleep_wake(struct acpi_device *dev,
  281. int enable, int sleep_state, int dev_state)
  282. {
  283. union acpi_object in_arg[3];
  284. struct acpi_object_list arg_list = { 3, in_arg };
  285. acpi_status status = AE_OK;
  286. /*
  287. * Try to execute _DSW first.
  288. *
  289. * Three agruments are needed for the _DSW object:
  290. * Argument 0: enable/disable the wake capabilities
  291. * Argument 1: target system state
  292. * Argument 2: target device state
  293. * When _DSW object is called to disable the wake capabilities, maybe
  294. * the first argument is filled. The values of the other two agruments
  295. * are meaningless.
  296. */
  297. in_arg[0].type = ACPI_TYPE_INTEGER;
  298. in_arg[0].integer.value = enable;
  299. in_arg[1].type = ACPI_TYPE_INTEGER;
  300. in_arg[1].integer.value = sleep_state;
  301. in_arg[2].type = ACPI_TYPE_INTEGER;
  302. in_arg[2].integer.value = dev_state;
  303. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  304. if (ACPI_SUCCESS(status)) {
  305. return 0;
  306. } else if (status != AE_NOT_FOUND) {
  307. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  308. dev->wakeup.flags.valid = 0;
  309. return -ENODEV;
  310. }
  311. /* Execute _PSW */
  312. arg_list.count = 1;
  313. in_arg[0].integer.value = enable;
  314. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  315. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  316. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  317. dev->wakeup.flags.valid = 0;
  318. return -ENODEV;
  319. }
  320. return 0;
  321. }
  322. /*
  323. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  324. * 1. Power on the power resources required for the wakeup device
  325. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  326. * State Wake) for the device, if present
  327. */
  328. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  329. {
  330. int i, err;
  331. if (!dev || !dev->wakeup.flags.valid)
  332. return -EINVAL;
  333. /*
  334. * Do not execute the code below twice in a row without calling
  335. * acpi_disable_wakeup_device_power() in between for the same device
  336. */
  337. if (dev->wakeup.flags.prepared)
  338. return 0;
  339. /* Open power resource */
  340. for (i = 0; i < dev->wakeup.resources.count; i++) {
  341. int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
  342. if (ret) {
  343. printk(KERN_ERR PREFIX "Transition power state\n");
  344. dev->wakeup.flags.valid = 0;
  345. return -ENODEV;
  346. }
  347. }
  348. /*
  349. * Passing 3 as the third argument below means the device may be placed
  350. * in arbitrary power state afterwards.
  351. */
  352. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  353. if (!err)
  354. dev->wakeup.flags.prepared = 1;
  355. return err;
  356. }
  357. /*
  358. * Shutdown a wakeup device, counterpart of above method
  359. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  360. * State Wake) for the device, if present
  361. * 2. Shutdown down the power resources
  362. */
  363. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  364. {
  365. int i, ret;
  366. if (!dev || !dev->wakeup.flags.valid)
  367. return -EINVAL;
  368. /*
  369. * Do not execute the code below twice in a row without calling
  370. * acpi_enable_wakeup_device_power() in between for the same device
  371. */
  372. if (!dev->wakeup.flags.prepared)
  373. return 0;
  374. dev->wakeup.flags.prepared = 0;
  375. ret = acpi_device_sleep_wake(dev, 0, 0, 0);
  376. if (ret)
  377. return ret;
  378. /* Close power resource */
  379. for (i = 0; i < dev->wakeup.resources.count; i++) {
  380. ret = acpi_power_off_device(dev->wakeup.resources.handles[i], dev);
  381. if (ret) {
  382. printk(KERN_ERR PREFIX "Transition power state\n");
  383. dev->wakeup.flags.valid = 0;
  384. return -ENODEV;
  385. }
  386. }
  387. return ret;
  388. }
  389. /* --------------------------------------------------------------------------
  390. Device Power Management
  391. -------------------------------------------------------------------------- */
  392. int acpi_power_get_inferred_state(struct acpi_device *device)
  393. {
  394. int result = 0;
  395. struct acpi_handle_list *list = NULL;
  396. int list_state = 0;
  397. int i = 0;
  398. if (!device)
  399. return -EINVAL;
  400. device->power.state = ACPI_STATE_UNKNOWN;
  401. /*
  402. * We know a device's inferred power state when all the resources
  403. * required for a given D-state are 'on'.
  404. */
  405. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  406. list = &device->power.states[i].resources;
  407. if (list->count < 1)
  408. continue;
  409. result = acpi_power_get_list_state(list, &list_state);
  410. if (result)
  411. return result;
  412. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  413. device->power.state = i;
  414. return 0;
  415. }
  416. }
  417. device->power.state = ACPI_STATE_D3;
  418. return 0;
  419. }
  420. int acpi_power_transition(struct acpi_device *device, int state)
  421. {
  422. int result = 0;
  423. struct acpi_handle_list *cl = NULL; /* Current Resources */
  424. struct acpi_handle_list *tl = NULL; /* Target Resources */
  425. int i = 0;
  426. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  427. return -EINVAL;
  428. if ((device->power.state < ACPI_STATE_D0)
  429. || (device->power.state > ACPI_STATE_D3))
  430. return -ENODEV;
  431. cl = &device->power.states[device->power.state].resources;
  432. tl = &device->power.states[state].resources;
  433. /* TBD: Resources must be ordered. */
  434. /*
  435. * First we reference all power resources required in the target list
  436. * (e.g. so the device doesn't lose power while transitioning).
  437. */
  438. for (i = 0; i < tl->count; i++) {
  439. result = acpi_power_on(tl->handles[i], device);
  440. if (result)
  441. goto end;
  442. }
  443. if (device->power.state == state) {
  444. goto end;
  445. }
  446. /*
  447. * Then we dereference all power resources used in the current list.
  448. */
  449. for (i = 0; i < cl->count; i++) {
  450. result = acpi_power_off_device(cl->handles[i], device);
  451. if (result)
  452. goto end;
  453. }
  454. end:
  455. if (result)
  456. device->power.state = ACPI_STATE_UNKNOWN;
  457. else {
  458. /* We shouldn't change the state till all above operations succeed */
  459. device->power.state = state;
  460. }
  461. return result;
  462. }
  463. /* --------------------------------------------------------------------------
  464. FS Interface (/proc)
  465. -------------------------------------------------------------------------- */
  466. static struct proc_dir_entry *acpi_power_dir;
  467. static int acpi_power_seq_show(struct seq_file *seq, void *offset)
  468. {
  469. int count = 0;
  470. int result = 0, state;
  471. struct acpi_power_resource *resource = NULL;
  472. struct list_head *node, *next;
  473. struct acpi_power_reference *ref;
  474. resource = seq->private;
  475. if (!resource)
  476. goto end;
  477. result = acpi_power_get_state(resource->device->handle, &state);
  478. if (result)
  479. goto end;
  480. seq_puts(seq, "state: ");
  481. switch (state) {
  482. case ACPI_POWER_RESOURCE_STATE_ON:
  483. seq_puts(seq, "on\n");
  484. break;
  485. case ACPI_POWER_RESOURCE_STATE_OFF:
  486. seq_puts(seq, "off\n");
  487. break;
  488. default:
  489. seq_puts(seq, "unknown\n");
  490. break;
  491. }
  492. mutex_lock(&resource->resource_lock);
  493. list_for_each_safe(node, next, &resource->reference) {
  494. ref = container_of(node, struct acpi_power_reference, node);
  495. count++;
  496. }
  497. mutex_unlock(&resource->resource_lock);
  498. seq_printf(seq, "system level: S%d\n"
  499. "order: %d\n"
  500. "reference count: %d\n",
  501. resource->system_level,
  502. resource->order, count);
  503. end:
  504. return 0;
  505. }
  506. static int acpi_power_open_fs(struct inode *inode, struct file *file)
  507. {
  508. return single_open(file, acpi_power_seq_show, PDE(inode)->data);
  509. }
  510. static int acpi_power_add_fs(struct acpi_device *device)
  511. {
  512. struct proc_dir_entry *entry = NULL;
  513. if (!device)
  514. return -EINVAL;
  515. if (!acpi_device_dir(device)) {
  516. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  517. acpi_power_dir);
  518. if (!acpi_device_dir(device))
  519. return -ENODEV;
  520. }
  521. /* 'status' [R] */
  522. entry = proc_create_data(ACPI_POWER_FILE_STATUS,
  523. S_IRUGO, acpi_device_dir(device),
  524. &acpi_power_fops, acpi_driver_data(device));
  525. if (!entry)
  526. return -EIO;
  527. return 0;
  528. }
  529. static int acpi_power_remove_fs(struct acpi_device *device)
  530. {
  531. if (acpi_device_dir(device)) {
  532. remove_proc_entry(ACPI_POWER_FILE_STATUS,
  533. acpi_device_dir(device));
  534. remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
  535. acpi_device_dir(device) = NULL;
  536. }
  537. return 0;
  538. }
  539. /* --------------------------------------------------------------------------
  540. Driver Interface
  541. -------------------------------------------------------------------------- */
  542. static int acpi_power_add(struct acpi_device *device)
  543. {
  544. int result = 0, state;
  545. acpi_status status = AE_OK;
  546. struct acpi_power_resource *resource = NULL;
  547. union acpi_object acpi_object;
  548. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  549. if (!device)
  550. return -EINVAL;
  551. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  552. if (!resource)
  553. return -ENOMEM;
  554. resource->device = device;
  555. mutex_init(&resource->resource_lock);
  556. INIT_LIST_HEAD(&resource->reference);
  557. strcpy(resource->name, device->pnp.bus_id);
  558. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  559. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  560. device->driver_data = resource;
  561. /* Evalute the object to get the system level and resource order. */
  562. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  563. if (ACPI_FAILURE(status)) {
  564. result = -ENODEV;
  565. goto end;
  566. }
  567. resource->system_level = acpi_object.power_resource.system_level;
  568. resource->order = acpi_object.power_resource.resource_order;
  569. result = acpi_power_get_state(device->handle, &state);
  570. if (result)
  571. goto end;
  572. switch (state) {
  573. case ACPI_POWER_RESOURCE_STATE_ON:
  574. device->power.state = ACPI_STATE_D0;
  575. break;
  576. case ACPI_POWER_RESOURCE_STATE_OFF:
  577. device->power.state = ACPI_STATE_D3;
  578. break;
  579. default:
  580. device->power.state = ACPI_STATE_UNKNOWN;
  581. break;
  582. }
  583. result = acpi_power_add_fs(device);
  584. if (result)
  585. goto end;
  586. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  587. acpi_device_bid(device), state ? "on" : "off");
  588. end:
  589. if (result)
  590. kfree(resource);
  591. return result;
  592. }
  593. static int acpi_power_remove(struct acpi_device *device, int type)
  594. {
  595. struct acpi_power_resource *resource = NULL;
  596. struct list_head *node, *next;
  597. if (!device || !acpi_driver_data(device))
  598. return -EINVAL;
  599. resource = acpi_driver_data(device);
  600. acpi_power_remove_fs(device);
  601. mutex_lock(&resource->resource_lock);
  602. list_for_each_safe(node, next, &resource->reference) {
  603. struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
  604. list_del(&ref->node);
  605. kfree(ref);
  606. }
  607. mutex_unlock(&resource->resource_lock);
  608. kfree(resource);
  609. return 0;
  610. }
  611. static int acpi_power_resume(struct acpi_device *device)
  612. {
  613. int result = 0, state;
  614. struct acpi_power_resource *resource = NULL;
  615. struct acpi_power_reference *ref;
  616. if (!device || !acpi_driver_data(device))
  617. return -EINVAL;
  618. resource = acpi_driver_data(device);
  619. result = acpi_power_get_state(device->handle, &state);
  620. if (result)
  621. return result;
  622. mutex_lock(&resource->resource_lock);
  623. if (state == ACPI_POWER_RESOURCE_STATE_OFF &&
  624. !list_empty(&resource->reference)) {
  625. ref = container_of(resource->reference.next, struct acpi_power_reference, node);
  626. mutex_unlock(&resource->resource_lock);
  627. result = acpi_power_on(device->handle, ref->device);
  628. return result;
  629. }
  630. mutex_unlock(&resource->resource_lock);
  631. return 0;
  632. }
  633. int __init acpi_power_init(void)
  634. {
  635. int result = 0;
  636. INIT_LIST_HEAD(&acpi_power_resource_list);
  637. acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
  638. if (!acpi_power_dir)
  639. return -ENODEV;
  640. result = acpi_bus_register_driver(&acpi_power_driver);
  641. if (result < 0) {
  642. remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
  643. return -ENODEV;
  644. }
  645. return 0;
  646. }