2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
47 #define _COMPONENT ACPI_POWER_COMPONENT
48 ACPI_MODULE_NAME("acpi_power")
49 #define ACPI_POWER_COMPONENT 0x00800000
50 #define ACPI_POWER_CLASS "power_resource"
51 #define ACPI_POWER_DRIVER_NAME "ACPI Power Resource Driver"
52 #define ACPI_POWER_DEVICE_NAME "Power Resource"
53 #define ACPI_POWER_FILE_INFO "info"
54 #define ACPI_POWER_FILE_STATUS "state"
55 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
56 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
57 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
58 static int acpi_power_add(struct acpi_device
*device
);
59 static int acpi_power_remove(struct acpi_device
*device
, int type
);
60 static int acpi_power_open_fs(struct inode
*inode
, struct file
*file
);
62 static struct acpi_driver acpi_power_driver
= {
63 .name
= ACPI_POWER_DRIVER_NAME
,
64 .class = ACPI_POWER_CLASS
,
65 .ids
= ACPI_POWER_HID
,
67 .add
= acpi_power_add
,
68 .remove
= acpi_power_remove
,
72 struct acpi_power_resource
{
73 struct acpi_device
* device
;
81 static struct list_head acpi_power_resource_list
;
83 static const struct file_operations acpi_power_fops
= {
84 .open
= acpi_power_open_fs
,
87 .release
= single_release
,
90 /* --------------------------------------------------------------------------
91 Power Resource Management
92 -------------------------------------------------------------------------- */
95 acpi_power_get_context(acpi_handle handle
,
96 struct acpi_power_resource
**resource
)
99 struct acpi_device
*device
= NULL
;
105 result
= acpi_bus_get_device(handle
, &device
);
107 printk(KERN_WARNING PREFIX
"Getting context [%p]\n", handle
);
111 *resource
= (struct acpi_power_resource
*)acpi_driver_data(device
);
118 static int acpi_power_get_state(struct acpi_power_resource
*resource
)
120 acpi_status status
= AE_OK
;
121 unsigned long sta
= 0;
127 status
= acpi_evaluate_integer(resource
->device
->handle
, "_STA", NULL
, &sta
);
128 if (ACPI_FAILURE(status
))
132 resource
->state
= ACPI_POWER_RESOURCE_STATE_ON
;
134 resource
->state
= ACPI_POWER_RESOURCE_STATE_OFF
;
136 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] is %s\n",
137 resource
->name
, resource
->state
? "on" : "off"));
142 static int acpi_power_get_list_state(struct acpi_handle_list
*list
, int *state
)
145 struct acpi_power_resource
*resource
= NULL
;
152 /* The state of the list is 'on' IFF all resources are 'on'. */
154 for (i
= 0; i
< list
->count
; i
++) {
155 result
= acpi_power_get_context(list
->handles
[i
], &resource
);
158 result
= acpi_power_get_state(resource
);
162 *state
= resource
->state
;
164 if (*state
!= ACPI_POWER_RESOURCE_STATE_ON
)
168 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource list is %s\n",
169 *state
? "on" : "off"));
174 static int acpi_power_on(acpi_handle handle
)
177 acpi_status status
= AE_OK
;
178 struct acpi_device
*device
= NULL
;
179 struct acpi_power_resource
*resource
= NULL
;
182 result
= acpi_power_get_context(handle
, &resource
);
186 resource
->references
++;
188 if ((resource
->references
> 1)
189 || (resource
->state
== ACPI_POWER_RESOURCE_STATE_ON
)) {
190 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] already on\n",
195 status
= acpi_evaluate_object(resource
->device
->handle
, "_ON", NULL
, NULL
);
196 if (ACPI_FAILURE(status
))
199 result
= acpi_power_get_state(resource
);
202 if (resource
->state
!= ACPI_POWER_RESOURCE_STATE_ON
)
205 /* Update the power resource's _device_ power state */
206 device
= resource
->device
;
207 resource
->device
->power
.state
= ACPI_STATE_D0
;
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] turned on\n",
215 static int acpi_power_off_device(acpi_handle handle
)
218 acpi_status status
= AE_OK
;
219 struct acpi_device
*device
= NULL
;
220 struct acpi_power_resource
*resource
= NULL
;
223 result
= acpi_power_get_context(handle
, &resource
);
227 if (resource
->references
)
228 resource
->references
--;
230 if (resource
->references
) {
231 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
232 "Resource [%s] is still in use, dereferencing\n",
233 device
->pnp
.bus_id
));
237 if (resource
->state
== ACPI_POWER_RESOURCE_STATE_OFF
) {
238 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] already off\n",
239 device
->pnp
.bus_id
));
243 status
= acpi_evaluate_object(resource
->device
->handle
, "_OFF", NULL
, NULL
);
244 if (ACPI_FAILURE(status
))
247 result
= acpi_power_get_state(resource
);
250 if (resource
->state
!= ACPI_POWER_RESOURCE_STATE_OFF
)
253 /* Update the power resource's _device_ power state */
254 device
= resource
->device
;
255 device
->power
.state
= ACPI_STATE_D3
;
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Resource [%s] turned off\n",
264 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
265 * 1. Power on the power resources required for the wakeup device
266 * 2. Enable _PSW (power state wake) for the device if present
268 int acpi_enable_wakeup_device_power(struct acpi_device
*dev
)
270 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
271 struct acpi_object_list arg_list
= { 1, &arg
};
272 acpi_status status
= AE_OK
;
276 if (!dev
|| !dev
->wakeup
.flags
.valid
)
279 arg
.integer
.value
= 1;
280 /* Open power resource */
281 for (i
= 0; i
< dev
->wakeup
.resources
.count
; i
++) {
282 ret
= acpi_power_on(dev
->wakeup
.resources
.handles
[i
]);
284 printk(KERN_ERR PREFIX
"Transition power state\n");
285 dev
->wakeup
.flags
.valid
= 0;
291 status
= acpi_evaluate_object(dev
->handle
, "_PSW", &arg_list
, NULL
);
292 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
293 printk(KERN_ERR PREFIX
"Evaluate _PSW\n");
294 dev
->wakeup
.flags
.valid
= 0;
302 * Shutdown a wakeup device, counterpart of above method
303 * 1. Disable _PSW (power state wake)
304 * 2. Shutdown down the power resources
306 int acpi_disable_wakeup_device_power(struct acpi_device
*dev
)
308 union acpi_object arg
= { ACPI_TYPE_INTEGER
};
309 struct acpi_object_list arg_list
= { 1, &arg
};
310 acpi_status status
= AE_OK
;
315 if (!dev
|| !dev
->wakeup
.flags
.valid
)
318 arg
.integer
.value
= 0;
320 status
= acpi_evaluate_object(dev
->handle
, "_PSW", &arg_list
, NULL
);
321 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
322 printk(KERN_ERR PREFIX
"Evaluate _PSW\n");
323 dev
->wakeup
.flags
.valid
= 0;
327 /* Close power resource */
328 for (i
= 0; i
< dev
->wakeup
.resources
.count
; i
++) {
329 ret
= acpi_power_off_device(dev
->wakeup
.resources
.handles
[i
]);
331 printk(KERN_ERR PREFIX
"Transition power state\n");
332 dev
->wakeup
.flags
.valid
= 0;
340 /* --------------------------------------------------------------------------
341 Device Power Management
342 -------------------------------------------------------------------------- */
344 int acpi_power_get_inferred_state(struct acpi_device
*device
)
347 struct acpi_handle_list
*list
= NULL
;
355 device
->power
.state
= ACPI_STATE_UNKNOWN
;
358 * We know a device's inferred power state when all the resources
359 * required for a given D-state are 'on'.
361 for (i
= ACPI_STATE_D0
; i
< ACPI_STATE_D3
; i
++) {
362 list
= &device
->power
.states
[i
].resources
;
366 result
= acpi_power_get_list_state(list
, &list_state
);
370 if (list_state
== ACPI_POWER_RESOURCE_STATE_ON
) {
371 device
->power
.state
= i
;
376 device
->power
.state
= ACPI_STATE_D3
;
381 int acpi_power_transition(struct acpi_device
*device
, int state
)
384 struct acpi_handle_list
*cl
= NULL
; /* Current Resources */
385 struct acpi_handle_list
*tl
= NULL
; /* Target Resources */
389 if (!device
|| (state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
392 if ((device
->power
.state
< ACPI_STATE_D0
)
393 || (device
->power
.state
> ACPI_STATE_D3
))
396 cl
= &device
->power
.states
[device
->power
.state
].resources
;
397 tl
= &device
->power
.states
[state
].resources
;
399 device
->power
.state
= ACPI_STATE_UNKNOWN
;
401 if (!cl
->count
&& !tl
->count
) {
406 /* TBD: Resources must be ordered. */
409 * First we reference all power resources required in the target list
410 * (e.g. so the device doesn't lose power while transitioning).
412 for (i
= 0; i
< tl
->count
; i
++) {
413 result
= acpi_power_on(tl
->handles
[i
]);
419 * Then we dereference all power resources used in the current list.
421 for (i
= 0; i
< cl
->count
; i
++) {
422 result
= acpi_power_off_device(cl
->handles
[i
]);
427 /* We shouldn't change the state till all above operations succeed */
428 device
->power
.state
= state
;
431 printk(KERN_WARNING PREFIX
"Transitioning device [%s] to D%d\n",
432 device
->pnp
.bus_id
, state
);
437 /* --------------------------------------------------------------------------
439 -------------------------------------------------------------------------- */
441 static struct proc_dir_entry
*acpi_power_dir
;
443 static int acpi_power_seq_show(struct seq_file
*seq
, void *offset
)
445 struct acpi_power_resource
*resource
= NULL
;
448 resource
= (struct acpi_power_resource
*)seq
->private;
453 seq_puts(seq
, "state: ");
454 switch (resource
->state
) {
455 case ACPI_POWER_RESOURCE_STATE_ON
:
456 seq_puts(seq
, "on\n");
458 case ACPI_POWER_RESOURCE_STATE_OFF
:
459 seq_puts(seq
, "off\n");
462 seq_puts(seq
, "unknown\n");
466 seq_printf(seq
, "system level: S%d\n"
468 "reference count: %d\n",
469 resource
->system_level
,
470 resource
->order
, resource
->references
);
476 static int acpi_power_open_fs(struct inode
*inode
, struct file
*file
)
478 return single_open(file
, acpi_power_seq_show
, PDE(inode
)->data
);
481 static int acpi_power_add_fs(struct acpi_device
*device
)
483 struct proc_dir_entry
*entry
= NULL
;
489 if (!acpi_device_dir(device
)) {
490 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
492 if (!acpi_device_dir(device
))
497 entry
= create_proc_entry(ACPI_POWER_FILE_STATUS
,
498 S_IRUGO
, acpi_device_dir(device
));
502 entry
->proc_fops
= &acpi_power_fops
;
503 entry
->data
= acpi_driver_data(device
);
509 static int acpi_power_remove_fs(struct acpi_device
*device
)
512 if (acpi_device_dir(device
)) {
513 remove_proc_entry(ACPI_POWER_FILE_STATUS
,
514 acpi_device_dir(device
));
515 remove_proc_entry(acpi_device_bid(device
), acpi_power_dir
);
516 acpi_device_dir(device
) = NULL
;
522 /* --------------------------------------------------------------------------
524 -------------------------------------------------------------------------- */
526 static int acpi_power_add(struct acpi_device
*device
)
529 acpi_status status
= AE_OK
;
530 struct acpi_power_resource
*resource
= NULL
;
531 union acpi_object acpi_object
;
532 struct acpi_buffer buffer
= { sizeof(acpi_object
), &acpi_object
};
538 resource
= kmalloc(sizeof(struct acpi_power_resource
), GFP_KERNEL
);
541 memset(resource
, 0, sizeof(struct acpi_power_resource
));
543 resource
->device
= device
;
544 strcpy(resource
->name
, device
->pnp
.bus_id
);
545 strcpy(acpi_device_name(device
), ACPI_POWER_DEVICE_NAME
);
546 strcpy(acpi_device_class(device
), ACPI_POWER_CLASS
);
547 acpi_driver_data(device
) = resource
;
549 /* Evalute the object to get the system level and resource order. */
550 status
= acpi_evaluate_object(device
->handle
, NULL
, NULL
, &buffer
);
551 if (ACPI_FAILURE(status
)) {
555 resource
->system_level
= acpi_object
.power_resource
.system_level
;
556 resource
->order
= acpi_object
.power_resource
.resource_order
;
558 result
= acpi_power_get_state(resource
);
562 switch (resource
->state
) {
563 case ACPI_POWER_RESOURCE_STATE_ON
:
564 device
->power
.state
= ACPI_STATE_D0
;
566 case ACPI_POWER_RESOURCE_STATE_OFF
:
567 device
->power
.state
= ACPI_STATE_D3
;
570 device
->power
.state
= ACPI_STATE_UNKNOWN
;
574 result
= acpi_power_add_fs(device
);
578 printk(KERN_INFO PREFIX
"%s [%s] (%s)\n", acpi_device_name(device
),
579 acpi_device_bid(device
), resource
->state
? "on" : "off");
588 static int acpi_power_remove(struct acpi_device
*device
, int type
)
590 struct acpi_power_resource
*resource
= NULL
;
593 if (!device
|| !acpi_driver_data(device
))
596 resource
= (struct acpi_power_resource
*)acpi_driver_data(device
);
598 acpi_power_remove_fs(device
);
605 static int __init
acpi_power_init(void)
613 INIT_LIST_HEAD(&acpi_power_resource_list
);
615 acpi_power_dir
= proc_mkdir(ACPI_POWER_CLASS
, acpi_root_dir
);
619 result
= acpi_bus_register_driver(&acpi_power_driver
);
621 remove_proc_entry(ACPI_POWER_CLASS
, acpi_root_dir
);
628 subsys_initcall(acpi_power_init
);