ACPI / scan: More straightforward preparation of ACPI device objects
[deliverable/linux.git] / drivers / acpi / power.c
CommitLineData
1da177e4
LT
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/*
27 * ACPI power-managed devices may be controlled in two ways:
28 * 1. via "Device Specific (D-State) Control"
29 * 2. via "Power Resource Control".
30 * This module is used to manage devices relying on Power Resource Control.
31 *
32 * An ACPI "power resource object" describes a software controllable power
33 * plane, clock plane, or other resource used by a power managed device.
34 * A device may rely on multiple power resources, and a power resource
35 * may be shared by multiple devices.
36 */
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
5a0e3ad6 42#include <linux/slab.h>
0090def6 43#include <linux/pm_runtime.h>
1da177e4
LT
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
9b83ccd2 46#include "sleep.h"
0090def6 47#include "internal.h"
9b83ccd2 48
a192a958
LB
49#define PREFIX "ACPI: "
50
89595b8f 51#define _COMPONENT ACPI_POWER_COMPONENT
f52fd66d 52ACPI_MODULE_NAME("power");
1da177e4 53#define ACPI_POWER_CLASS "power_resource"
1da177e4
LT
54#define ACPI_POWER_DEVICE_NAME "Power Resource"
55#define ACPI_POWER_FILE_INFO "info"
56#define ACPI_POWER_FILE_STATUS "state"
57#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
58#define ACPI_POWER_RESOURCE_STATE_ON 0x01
59#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
f5adfaa3 60
4be44fcd
LB
61static int acpi_power_add(struct acpi_device *device);
62static int acpi_power_remove(struct acpi_device *device, int type);
1da177e4 63
c97adf9e 64static const struct acpi_device_id power_device_ids[] = {
1ba90e3a
TR
65 {ACPI_POWER_HID, 0},
66 {"", 0},
67};
68MODULE_DEVICE_TABLE(acpi, power_device_ids);
69
90692404 70#ifdef CONFIG_PM_SLEEP
e579e2dd 71static int acpi_power_resume(struct device *dev);
90692404 72#endif
e579e2dd
RW
73static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume);
74
1da177e4 75static struct acpi_driver acpi_power_driver = {
c2b6705b 76 .name = "power",
4be44fcd 77 .class = ACPI_POWER_CLASS,
1ba90e3a 78 .ids = power_device_ids,
4be44fcd
LB
79 .ops = {
80 .add = acpi_power_add,
81 .remove = acpi_power_remove,
82 },
e579e2dd 83 .drv.pm = &acpi_power_pm,
1da177e4
LT
84};
85
bc9b6407
RW
86struct acpi_power_dependent_device {
87 struct list_head node;
88 struct acpi_device *adev;
89 struct work_struct work;
0090def6
LM
90};
91
4be44fcd 92struct acpi_power_resource {
bc9b6407
RW
93 struct acpi_device *device;
94 struct list_head dependent;
4be44fcd
LB
95 acpi_bus_id name;
96 u32 system_level;
97 u32 order;
3e384ee6 98 unsigned int ref_count;
0a613902 99 struct mutex resource_lock;
1da177e4
LT
100};
101
4be44fcd 102static struct list_head acpi_power_resource_list;
1da177e4 103
1da177e4
LT
104/* --------------------------------------------------------------------------
105 Power Resource Management
106 -------------------------------------------------------------------------- */
107
108static int
4be44fcd
LB
109acpi_power_get_context(acpi_handle handle,
110 struct acpi_power_resource **resource)
1da177e4 111{
4be44fcd
LB
112 int result = 0;
113 struct acpi_device *device = NULL;
1da177e4 114
1da177e4
LT
115
116 if (!resource)
d550d98d 117 return -ENODEV;
1da177e4
LT
118
119 result = acpi_bus_get_device(handle, &device);
120 if (result) {
cece9296 121 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
d550d98d 122 return result;
1da177e4
LT
123 }
124
50dd0969 125 *resource = acpi_driver_data(device);
a815ab8b 126 if (!*resource)
d550d98d 127 return -ENODEV;
1da177e4 128
d550d98d 129 return 0;
1da177e4
LT
130}
131
a51e145f 132static int acpi_power_get_state(acpi_handle handle, int *state)
1da177e4 133{
4be44fcd 134 acpi_status status = AE_OK;
27663c58 135 unsigned long long sta = 0;
60a4ce7f
LM
136 char node_name[5];
137 struct acpi_buffer buffer = { sizeof(node_name), node_name };
1da177e4 138
1da177e4 139
a51e145f 140 if (!handle || !state)
d550d98d 141 return -EINVAL;
1da177e4 142
a51e145f 143 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
1da177e4 144 if (ACPI_FAILURE(status))
d550d98d 145 return -ENODEV;
1da177e4 146
c35923bc
AS
147 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
148 ACPI_POWER_RESOURCE_STATE_OFF;
1da177e4 149
60a4ce7f
LM
150 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
151
1da177e4 152 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
60a4ce7f 153 node_name,
b1b57fbe 154 *state ? "on" : "off"));
1da177e4 155
d550d98d 156 return 0;
1da177e4
LT
157}
158
4be44fcd 159static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
1da177e4 160{
d0515d9f
RW
161 int cur_state;
162 int i = 0;
1da177e4
LT
163
164 if (!list || !state)
d550d98d 165 return -EINVAL;
1da177e4
LT
166
167 /* The state of the list is 'on' IFF all resources are 'on'. */
168
4be44fcd 169 for (i = 0; i < list->count; i++) {
d0515d9f
RW
170 struct acpi_power_resource *resource;
171 acpi_handle handle = list->handles[i];
172 int result;
173
174 result = acpi_power_get_context(handle, &resource);
1da177e4 175 if (result)
d550d98d 176 return result;
1da177e4 177
d0515d9f
RW
178 mutex_lock(&resource->resource_lock);
179
180 result = acpi_power_get_state(handle, &cur_state);
1da177e4 181
d0515d9f
RW
182 mutex_unlock(&resource->resource_lock);
183
184 if (result)
185 return result;
186
187 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
1da177e4
LT
188 break;
189 }
190
191 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
d0515d9f 192 cur_state ? "on" : "off"));
1da177e4 193
d0515d9f
RW
194 *state = cur_state;
195
196 return 0;
1da177e4
LT
197}
198
bc9b6407 199static void acpi_power_resume_dependent(struct work_struct *work)
0090def6 200{
bc9b6407
RW
201 struct acpi_power_dependent_device *dep;
202 struct acpi_device_physical_node *pn;
203 struct acpi_device *adev;
0090def6
LM
204 int state;
205
bc9b6407
RW
206 dep = container_of(work, struct acpi_power_dependent_device, work);
207 adev = dep->adev;
208 if (acpi_power_get_inferred_state(adev, &state))
0090def6
LM
209 return;
210
bc9b6407 211 if (state > ACPI_STATE_D0)
0090def6
LM
212 return;
213
bc9b6407
RW
214 mutex_lock(&adev->physical_node_lock);
215
216 list_for_each_entry(pn, &adev->physical_node_list, node)
217 pm_request_resume(pn->dev);
218
219 list_for_each_entry(pn, &adev->power_dependent, node)
220 pm_request_resume(pn->dev);
221
222 mutex_unlock(&adev->physical_node_lock);
0090def6
LM
223}
224
3e384ee6 225static int __acpi_power_on(struct acpi_power_resource *resource)
1da177e4 226{
4be44fcd 227 acpi_status status = AE_OK;
1da177e4 228
3e384ee6
RW
229 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
230 if (ACPI_FAILURE(status))
231 return -ENODEV;
232
233 /* Update the power resource's _device_ power state */
234 resource->device->power.state = ACPI_STATE_D0;
235
236 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
237 resource->name));
238
239 return 0;
240}
241
242static int acpi_power_on(acpi_handle handle)
243{
244 int result = 0;
245 struct acpi_power_resource *resource = NULL;
1da177e4
LT
246
247 result = acpi_power_get_context(handle, &resource);
248 if (result)
d550d98d 249 return result;
1da177e4 250
0a613902 251 mutex_lock(&resource->resource_lock);
0a613902 252
3e384ee6
RW
253 if (resource->ref_count++) {
254 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
255 "Power resource [%s] already on",
256 resource->name));
257 } else {
258 result = __acpi_power_on(resource);
bc9b6407 259 if (result) {
12b3b5af 260 resource->ref_count--;
bc9b6407
RW
261 } else {
262 struct acpi_power_dependent_device *dep;
40bf66ec 263
bc9b6407
RW
264 list_for_each_entry(dep, &resource->dependent, node)
265 schedule_work(&dep->work);
266 }
40bf66ec
LM
267 }
268
bc9b6407 269 mutex_unlock(&resource->resource_lock);
40bf66ec 270
12b3b5af 271 return result;
1da177e4
LT
272}
273
36237fa0 274static int acpi_power_off(acpi_handle handle)
1da177e4 275{
bdf43bbf 276 int result = 0;
4be44fcd 277 acpi_status status = AE_OK;
1da177e4 278 struct acpi_power_resource *resource = NULL;
0a613902 279
1da177e4
LT
280 result = acpi_power_get_context(handle, &resource);
281 if (result)
d550d98d 282 return result;
1da177e4 283
0a613902 284 mutex_lock(&resource->resource_lock);
3e384ee6
RW
285
286 if (!resource->ref_count) {
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "Power resource [%s] already off",
289 resource->name));
290 goto unlock;
0a613902 291 }
1da177e4 292
3e384ee6
RW
293 if (--resource->ref_count) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Power resource [%s] still in use\n",
296 resource->name));
297 goto unlock;
1da177e4
LT
298 }
299
5fbc19ef 300 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
3e384ee6
RW
301 if (ACPI_FAILURE(status)) {
302 result = -ENODEV;
303 } else {
304 /* Update the power resource's _device_ power state */
305 resource->device->power.state = ACPI_STATE_D3;
1da177e4 306
3e384ee6
RW
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "Power resource [%s] turned off\n",
309 resource->name));
310 }
1da177e4 311
3e384ee6
RW
312 unlock:
313 mutex_unlock(&resource->resource_lock);
1da177e4 314
3e384ee6 315 return result;
1da177e4
LT
316}
317
d2ef555b
RW
318static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
319{
320 int i;
321
322 for (i = num_res - 1; i >= 0 ; i--)
36237fa0 323 acpi_power_off(list->handles[i]);
d2ef555b
RW
324}
325
326static void acpi_power_off_list(struct acpi_handle_list *list)
327{
328 __acpi_power_off_list(list, list->count);
329}
330
331static int acpi_power_on_list(struct acpi_handle_list *list)
332{
333 int result = 0;
334 int i;
335
336 for (i = 0; i < list->count; i++) {
337 result = acpi_power_on(list->handles[i]);
338 if (result) {
339 __acpi_power_off_list(list, i);
340 break;
341 }
342 }
343
344 return result;
345}
346
bc9b6407
RW
347static void acpi_power_add_dependent(acpi_handle rhandle,
348 struct acpi_device *adev)
0090def6 349{
bc9b6407
RW
350 struct acpi_power_dependent_device *dep;
351 struct acpi_power_resource *resource;
0090def6 352
bc9b6407 353 if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource))
0090def6
LM
354 return;
355
bc9b6407 356 mutex_lock(&resource->resource_lock);
0090def6 357
bc9b6407
RW
358 list_for_each_entry(dep, &resource->dependent, node)
359 if (dep->adev == adev)
360 goto out;
0090def6 361
bc9b6407
RW
362 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
363 if (!dep)
364 goto out;
0090def6 365
bc9b6407
RW
366 dep->adev = adev;
367 INIT_WORK(&dep->work, acpi_power_resume_dependent);
368 list_add_tail(&dep->node, &resource->dependent);
0090def6 369
bc9b6407
RW
370 out:
371 mutex_unlock(&resource->resource_lock);
0090def6
LM
372}
373
bc9b6407
RW
374static void acpi_power_remove_dependent(acpi_handle rhandle,
375 struct acpi_device *adev)
0090def6 376{
bc9b6407
RW
377 struct acpi_power_dependent_device *dep;
378 struct acpi_power_resource *resource;
379 struct work_struct *work = NULL;
0090def6 380
bc9b6407
RW
381 if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource))
382 return;
0090def6 383
bc9b6407 384 mutex_lock(&resource->resource_lock);
0090def6 385
bc9b6407
RW
386 list_for_each_entry(dep, &resource->dependent, node)
387 if (dep->adev == adev) {
388 list_del(&dep->node);
389 work = &dep->work;
390 break;
391 }
0090def6 392
bc9b6407 393 mutex_unlock(&resource->resource_lock);
0090def6 394
bc9b6407
RW
395 if (work) {
396 cancel_work_sync(work);
397 kfree(dep);
398 }
0090def6
LM
399}
400
bc9b6407 401void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
0090def6 402{
bc9b6407
RW
403 if (adev->power.flags.power_resources) {
404 struct acpi_device_power_state *ps;
405 int j;
0090def6 406
bc9b6407
RW
407 ps = &adev->power.states[ACPI_STATE_D0];
408 for (j = 0; j < ps->resources.count; j++) {
409 acpi_handle rhandle = ps->resources.handles[j];
0090def6 410
bc9b6407
RW
411 if (add)
412 acpi_power_add_dependent(rhandle, adev);
413 else
414 acpi_power_remove_dependent(rhandle, adev);
0090def6
LM
415 }
416 }
0090def6 417}
bc9b6407
RW
418
419/* --------------------------------------------------------------------------
420 Device Power Management
421 -------------------------------------------------------------------------- */
0090def6 422
77e76609
RW
423/**
424 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
425 * ACPI 3.0) _PSW (Power State Wake)
426 * @dev: Device to handle.
427 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
428 * @sleep_state: Target sleep state of the system.
429 * @dev_state: Target power state of the device.
430 *
431 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
432 * State Wake) for the device, if present. On failure reset the device's
433 * wakeup.flags.valid flag.
434 *
435 * RETURN VALUE:
436 * 0 if either _DSW or _PSW has been successfully executed
437 * 0 if neither _DSW nor _PSW has been found
438 * -ENODEV if the execution of either _DSW or _PSW has failed
439 */
440int acpi_device_sleep_wake(struct acpi_device *dev,
441 int enable, int sleep_state, int dev_state)
442{
443 union acpi_object in_arg[3];
444 struct acpi_object_list arg_list = { 3, in_arg };
445 acpi_status status = AE_OK;
446
447 /*
448 * Try to execute _DSW first.
449 *
450 * Three agruments are needed for the _DSW object:
451 * Argument 0: enable/disable the wake capabilities
452 * Argument 1: target system state
453 * Argument 2: target device state
454 * When _DSW object is called to disable the wake capabilities, maybe
455 * the first argument is filled. The values of the other two agruments
456 * are meaningless.
457 */
458 in_arg[0].type = ACPI_TYPE_INTEGER;
459 in_arg[0].integer.value = enable;
460 in_arg[1].type = ACPI_TYPE_INTEGER;
461 in_arg[1].integer.value = sleep_state;
462 in_arg[2].type = ACPI_TYPE_INTEGER;
463 in_arg[2].integer.value = dev_state;
464 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
465 if (ACPI_SUCCESS(status)) {
466 return 0;
467 } else if (status != AE_NOT_FOUND) {
468 printk(KERN_ERR PREFIX "_DSW execution failed\n");
469 dev->wakeup.flags.valid = 0;
470 return -ENODEV;
471 }
472
473 /* Execute _PSW */
474 arg_list.count = 1;
475 in_arg[0].integer.value = enable;
476 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
477 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
478 printk(KERN_ERR PREFIX "_PSW execution failed\n");
479 dev->wakeup.flags.valid = 0;
480 return -ENODEV;
481 }
482
483 return 0;
484}
485
1da177e4
LT
486/*
487 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
488 * 1. Power on the power resources required for the wakeup device
77e76609
RW
489 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
490 * State Wake) for the device, if present
1da177e4 491 */
77e76609 492int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
1da177e4 493{
9b83ccd2 494 int i, err = 0;
1da177e4 495
1da177e4 496 if (!dev || !dev->wakeup.flags.valid)
77e76609 497 return -EINVAL;
1da177e4 498
9b83ccd2
RW
499 mutex_lock(&acpi_device_lock);
500
501 if (dev->wakeup.prepare_count++)
502 goto out;
0af4b8c4 503
1da177e4
LT
504 /* Open power resource */
505 for (i = 0; i < dev->wakeup.resources.count; i++) {
3e384ee6 506 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
1da177e4 507 if (ret) {
6468463a 508 printk(KERN_ERR PREFIX "Transition power state\n");
1da177e4 509 dev->wakeup.flags.valid = 0;
9b83ccd2
RW
510 err = -ENODEV;
511 goto err_out;
1da177e4
LT
512 }
513 }
514
77e76609
RW
515 /*
516 * Passing 3 as the third argument below means the device may be placed
517 * in arbitrary power state afterwards.
518 */
0af4b8c4 519 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
0af4b8c4 520
9b83ccd2
RW
521 err_out:
522 if (err)
523 dev->wakeup.prepare_count = 0;
524
525 out:
526 mutex_unlock(&acpi_device_lock);
0af4b8c4 527 return err;
1da177e4
LT
528}
529
530/*
531 * Shutdown a wakeup device, counterpart of above method
77e76609
RW
532 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
533 * State Wake) for the device, if present
1da177e4
LT
534 * 2. Shutdown down the power resources
535 */
4be44fcd 536int acpi_disable_wakeup_device_power(struct acpi_device *dev)
1da177e4 537{
9b83ccd2 538 int i, err = 0;
1da177e4
LT
539
540 if (!dev || !dev->wakeup.flags.valid)
77e76609 541 return -EINVAL;
1da177e4 542
9b83ccd2
RW
543 mutex_lock(&acpi_device_lock);
544
545 if (--dev->wakeup.prepare_count > 0)
546 goto out;
547
0af4b8c4 548 /*
9b83ccd2
RW
549 * Executing the code below even if prepare_count is already zero when
550 * the function is called may be useful, for example for initialisation.
0af4b8c4 551 */
9b83ccd2
RW
552 if (dev->wakeup.prepare_count < 0)
553 dev->wakeup.prepare_count = 0;
0af4b8c4 554
9b83ccd2
RW
555 err = acpi_device_sleep_wake(dev, 0, 0, 0);
556 if (err)
557 goto out;
1da177e4
LT
558
559 /* Close power resource */
560 for (i = 0; i < dev->wakeup.resources.count; i++) {
36237fa0 561 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
1da177e4 562 if (ret) {
6468463a 563 printk(KERN_ERR PREFIX "Transition power state\n");
1da177e4 564 dev->wakeup.flags.valid = 0;
9b83ccd2
RW
565 err = -ENODEV;
566 goto out;
1da177e4
LT
567 }
568 }
569
9b83ccd2
RW
570 out:
571 mutex_unlock(&acpi_device_lock);
572 return err;
1da177e4
LT
573}
574
32a00d27 575int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
1da177e4 576{
4be44fcd
LB
577 int result = 0;
578 struct acpi_handle_list *list = NULL;
579 int list_state = 0;
580 int i = 0;
1da177e4 581
32a00d27 582 if (!device || !state)
d550d98d 583 return -EINVAL;
1da177e4 584
1da177e4
LT
585 /*
586 * We know a device's inferred power state when all the resources
587 * required for a given D-state are 'on'.
588 */
38c92fff 589 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1da177e4
LT
590 list = &device->power.states[i].resources;
591 if (list->count < 1)
592 continue;
593
594 result = acpi_power_get_list_state(list, &list_state);
595 if (result)
d550d98d 596 return result;
1da177e4
LT
597
598 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
32a00d27 599 *state = i;
d550d98d 600 return 0;
1da177e4
LT
601 }
602 }
603
32a00d27 604 *state = ACPI_STATE_D3;
d550d98d 605 return 0;
1da177e4
LT
606}
607
30d3df41
RW
608int acpi_power_on_resources(struct acpi_device *device, int state)
609{
610 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
611 return -EINVAL;
612
613 return acpi_power_on_list(&device->power.states[state].resources);
614}
615
4be44fcd 616int acpi_power_transition(struct acpi_device *device, int state)
1da177e4 617{
5c7dd710 618 int result = 0;
1da177e4 619
3ebc81b8 620 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
d550d98d 621 return -EINVAL;
1da177e4 622
212967c6
RW
623 if (device->power.state == state)
624 return 0;
625
4be44fcd 626 if ((device->power.state < ACPI_STATE_D0)
3ebc81b8 627 || (device->power.state > ACPI_STATE_D3_COLD))
d550d98d 628 return -ENODEV;
1da177e4 629
1da177e4
LT
630 /* TBD: Resources must be ordered. */
631
632 /*
633 * First we reference all power resources required in the target list
d2ef555b
RW
634 * (e.g. so the device doesn't lose power while transitioning). Then,
635 * we dereference all power resources used in the current list.
1da177e4 636 */
5c7dd710
RW
637 if (state < ACPI_STATE_D3_COLD)
638 result = acpi_power_on_list(
639 &device->power.states[state].resources);
640
641 if (!result && device->power.state < ACPI_STATE_D3_COLD)
d2ef555b
RW
642 acpi_power_off_list(
643 &device->power.states[device->power.state].resources);
1da177e4 644
d2ef555b
RW
645 /* We shouldn't change the state unless the above operations succeed. */
646 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
1da177e4 647
d550d98d 648 return result;
1da177e4
LT
649}
650
1da177e4
LT
651/* --------------------------------------------------------------------------
652 Driver Interface
653 -------------------------------------------------------------------------- */
654
4be44fcd 655static int acpi_power_add(struct acpi_device *device)
1da177e4 656{
c35923bc 657 int result = 0, state;
4be44fcd 658 acpi_status status = AE_OK;
1da177e4 659 struct acpi_power_resource *resource = NULL;
4be44fcd
LB
660 union acpi_object acpi_object;
661 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
1da177e4 662
1da177e4
LT
663
664 if (!device)
d550d98d 665 return -EINVAL;
1da177e4 666
36bcbec7 667 resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
1da177e4 668 if (!resource)
d550d98d 669 return -ENOMEM;
1da177e4 670
41598572 671 resource->device = device;
0a613902 672 mutex_init(&resource->resource_lock);
bc9b6407 673 INIT_LIST_HEAD(&resource->dependent);
1da177e4
LT
674 strcpy(resource->name, device->pnp.bus_id);
675 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
676 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
db89b4f0 677 device->driver_data = resource;
1da177e4
LT
678
679 /* Evalute the object to get the system level and resource order. */
5fbc19ef 680 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
1da177e4
LT
681 if (ACPI_FAILURE(status)) {
682 result = -ENODEV;
683 goto end;
684 }
685 resource->system_level = acpi_object.power_resource.system_level;
686 resource->order = acpi_object.power_resource.resource_order;
687
a51e145f 688 result = acpi_power_get_state(device->handle, &state);
1da177e4
LT
689 if (result)
690 goto end;
691
c35923bc 692 switch (state) {
1da177e4
LT
693 case ACPI_POWER_RESOURCE_STATE_ON:
694 device->power.state = ACPI_STATE_D0;
695 break;
696 case ACPI_POWER_RESOURCE_STATE_OFF:
697 device->power.state = ACPI_STATE_D3;
698 break;
699 default:
700 device->power.state = ACPI_STATE_UNKNOWN;
701 break;
702 }
703
1da177e4 704 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
c35923bc 705 acpi_device_bid(device), state ? "on" : "off");
1da177e4 706
4be44fcd 707 end:
1da177e4
LT
708 if (result)
709 kfree(resource);
4be44fcd 710
d550d98d 711 return result;
1da177e4
LT
712}
713
4be44fcd 714static int acpi_power_remove(struct acpi_device *device, int type)
1da177e4 715{
3e384ee6 716 struct acpi_power_resource *resource;
1da177e4 717
3e384ee6 718 if (!device)
d550d98d 719 return -EINVAL;
1da177e4 720
50dd0969 721 resource = acpi_driver_data(device);
3e384ee6
RW
722 if (!resource)
723 return -EINVAL;
0a613902 724
1da177e4
LT
725 kfree(resource);
726
d550d98d 727 return 0;
1da177e4
LT
728}
729
90692404 730#ifdef CONFIG_PM_SLEEP
e579e2dd 731static int acpi_power_resume(struct device *dev)
0a613902 732{
c35923bc 733 int result = 0, state;
e579e2dd 734 struct acpi_device *device;
3e384ee6 735 struct acpi_power_resource *resource;
0a613902 736
e579e2dd 737 if (!dev)
0a613902
KK
738 return -EINVAL;
739
e579e2dd 740 device = to_acpi_device(dev);
db89b4f0 741 resource = acpi_driver_data(device);
3e384ee6
RW
742 if (!resource)
743 return -EINVAL;
744
745 mutex_lock(&resource->resource_lock);
0a613902 746
a51e145f 747 result = acpi_power_get_state(device->handle, &state);
0a613902 748 if (result)
3e384ee6 749 goto unlock;
0a613902 750
3e384ee6
RW
751 if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count)
752 result = __acpi_power_on(resource);
0a613902 753
3e384ee6 754 unlock:
0a613902 755 mutex_unlock(&resource->resource_lock);
3e384ee6
RW
756
757 return result;
0a613902 758}
90692404 759#endif
0a613902 760
44515374 761int __init acpi_power_init(void)
1da177e4 762{
1da177e4 763 INIT_LIST_HEAD(&acpi_power_resource_list);
06af7eb0 764 return acpi_bus_register_driver(&acpi_power_driver);
1da177e4 765}
This page took 0.660363 seconds and 5 git commands to generate.