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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
36 #define _COMPONENT ACPI_POWER_COMPONENT
37 ACPI_MODULE_NAME ("acpi_power")
39 #define ACPI_POWER_COMPONENT 0x00800000
40 #define ACPI_POWER_CLASS "power_resource"
41 #define ACPI_POWER_DRIVER_NAME "ACPI Power Resource Driver"
42 #define ACPI_POWER_DEVICE_NAME "Power Resource"
43 #define ACPI_POWER_FILE_INFO "info"
44 #define ACPI_POWER_FILE_STATUS "state"
45 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
46 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
47 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
49 int acpi_power_add (struct acpi_device *device);
50 int acpi_power_remove (struct acpi_device *device, int type);
51 static int acpi_power_open_fs(struct inode *inode, struct file *file);
53 static struct acpi_driver acpi_power_driver = {
54 .name = ACPI_POWER_DRIVER_NAME,
55 .class = ACPI_POWER_CLASS,
56 .ids = ACPI_POWER_HID,
58 .add = acpi_power_add,
59 .remove = acpi_power_remove,
63 struct acpi_power_resource
73 static struct list_head acpi_power_resource_list;
75 static struct file_operations acpi_power_fops = {
76 .open = acpi_power_open_fs,
79 .release = single_release,
82 /* --------------------------------------------------------------------------
83 Power Resource Management
84 -------------------------------------------------------------------------- */
87 acpi_power_get_context (
89 struct acpi_power_resource **resource)
92 struct acpi_device *device = NULL;
94 ACPI_FUNCTION_TRACE("acpi_power_get_context");
97 return_VALUE(-ENODEV);
99 result = acpi_bus_get_device(handle, &device);
101 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error getting context [%p]\n",
103 return_VALUE(result);
106 *resource = (struct acpi_power_resource *) acpi_driver_data(device);
108 return_VALUE(-ENODEV);
115 acpi_power_get_state (
116 struct acpi_power_resource *resource)
118 acpi_status status = AE_OK;
119 unsigned long sta = 0;
121 ACPI_FUNCTION_TRACE("acpi_power_get_state");
124 return_VALUE(-EINVAL);
126 status = acpi_evaluate_integer(resource->handle, "_STA", NULL, &sta);
127 if (ACPI_FAILURE(status))
128 return_VALUE(-ENODEV);
131 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
133 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
135 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
136 resource->name, resource->state?"on":"off"));
143 acpi_power_get_list_state (
144 struct acpi_handle_list *list,
148 struct acpi_power_resource *resource = NULL;
151 ACPI_FUNCTION_TRACE("acpi_power_get_list_state");
154 return_VALUE(-EINVAL);
156 /* The state of the list is 'on' IFF all resources are 'on'. */
158 for (i=0; i<list->count; i++) {
159 result = acpi_power_get_context(list->handles[i], &resource);
161 return_VALUE(result);
162 result = acpi_power_get_state(resource);
164 return_VALUE(result);
166 *state = resource->state;
168 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
172 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
175 return_VALUE(result);
184 acpi_status status = AE_OK;
185 struct acpi_device *device = NULL;
186 struct acpi_power_resource *resource = NULL;
188 ACPI_FUNCTION_TRACE("acpi_power_on");
190 result = acpi_power_get_context(handle, &resource);
192 return_VALUE(result);
194 resource->references++;
196 if ((resource->references > 1)
197 || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
198 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
203 status = acpi_evaluate_object(resource->handle, "_ON", NULL, NULL);
204 if (ACPI_FAILURE(status))
205 return_VALUE(-ENODEV);
207 result = acpi_power_get_state(resource);
209 return_VALUE(result);
210 if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
211 return_VALUE(-ENOEXEC);
213 /* Update the power resource's _device_ power state */
214 result = acpi_bus_get_device(resource->handle, &device);
216 return_VALUE(result);
217 device->power.state = ACPI_STATE_D0;
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
227 acpi_power_off_device (
231 acpi_status status = AE_OK;
232 struct acpi_device *device = NULL;
233 struct acpi_power_resource *resource = NULL;
235 ACPI_FUNCTION_TRACE("acpi_power_off_device");
237 result = acpi_power_get_context(handle, &resource);
239 return_VALUE(result);
241 if (resource->references)
242 resource->references--;
244 if (resource->references) {
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
246 "Resource [%s] is still in use, dereferencing\n",
247 device->pnp.bus_id));
251 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
252 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
253 device->pnp.bus_id));
257 status = acpi_evaluate_object(resource->handle, "_OFF", NULL, NULL);
258 if (ACPI_FAILURE(status))
259 return_VALUE(-ENODEV);
261 result = acpi_power_get_state(resource);
263 return_VALUE(result);
264 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
265 return_VALUE(-ENOEXEC);
267 /* Update the power resource's _device_ power state */
268 result = acpi_bus_get_device(resource->handle, &device);
270 return_VALUE(result);
271 device->power.state = ACPI_STATE_D3;
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
280 /* --------------------------------------------------------------------------
281 Device Power Management
282 -------------------------------------------------------------------------- */
285 acpi_power_get_inferred_state (
286 struct acpi_device *device)
289 struct acpi_handle_list *list = NULL;
293 ACPI_FUNCTION_TRACE("acpi_power_get_inferred_state");
296 return_VALUE(-EINVAL);
298 device->power.state = ACPI_STATE_UNKNOWN;
301 * We know a device's inferred power state when all the resources
302 * required for a given D-state are 'on'.
304 for (i=ACPI_STATE_D0; i<ACPI_STATE_D3; i++) {
305 list = &device->power.states[i].resources;
309 result = acpi_power_get_list_state(list, &list_state);
311 return_VALUE(result);
313 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
314 device->power.state = i;
319 device->power.state = ACPI_STATE_D3;
326 acpi_power_transition (
327 struct acpi_device *device,
331 struct acpi_handle_list *cl = NULL; /* Current Resources */
332 struct acpi_handle_list *tl = NULL; /* Target Resources */
335 ACPI_FUNCTION_TRACE("acpi_power_transition");
337 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
338 return_VALUE(-EINVAL);
340 if ((device->power.state < ACPI_STATE_D0) || (device->power.state > ACPI_STATE_D3))
341 return_VALUE(-ENODEV);
343 cl = &device->power.states[device->power.state].resources;
344 tl = &device->power.states[state].resources;
346 device->power.state = ACPI_STATE_UNKNOWN;
348 if (!cl->count && !tl->count) {
353 /* TBD: Resources must be ordered. */
356 * First we reference all power resources required in the target list
357 * (e.g. so the device doesn't lose power while transitioning).
359 for (i=0; i<tl->count; i++) {
360 result = acpi_power_on(tl->handles[i]);
366 * Then we dereference all power resources used in the current list.
368 for (i=0; i<cl->count; i++) {
369 result = acpi_power_off_device(cl->handles[i]);
374 /* We shouldn't change the state till all above operations succeed */
375 device->power.state = state;
378 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
379 "Error transitioning device [%s] to D%d\n",
380 device->pnp.bus_id, state));
382 return_VALUE(result);
386 /* --------------------------------------------------------------------------
388 -------------------------------------------------------------------------- */
390 struct proc_dir_entry *acpi_power_dir;
392 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
394 struct acpi_power_resource *resource = NULL;
396 ACPI_FUNCTION_TRACE("acpi_power_seq_show");
398 resource = (struct acpi_power_resource *)seq->private;
403 seq_puts(seq, "state: ");
404 switch (resource->state) {
405 case ACPI_POWER_RESOURCE_STATE_ON:
406 seq_puts(seq, "on\n");
408 case ACPI_POWER_RESOURCE_STATE_OFF:
409 seq_puts(seq, "off\n");
412 seq_puts(seq, "unknown\n");
416 seq_printf(seq, "system level: S%d\n"
418 "reference count: %d\n",
419 resource->system_level,
421 resource->references);
427 static int acpi_power_open_fs(struct inode *inode, struct file *file)
429 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
434 struct acpi_device *device)
436 struct proc_dir_entry *entry = NULL;
438 ACPI_FUNCTION_TRACE("acpi_power_add_fs");
441 return_VALUE(-EINVAL);
443 if (!acpi_device_dir(device)) {
444 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
446 if (!acpi_device_dir(device))
447 return_VALUE(-ENODEV);
451 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
452 S_IRUGO, acpi_device_dir(device));
454 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
455 "Unable to create '%s' fs entry\n",
456 ACPI_POWER_FILE_STATUS));
458 entry->proc_fops = &acpi_power_fops;
459 entry->data = acpi_driver_data(device);
467 acpi_power_remove_fs (
468 struct acpi_device *device)
470 ACPI_FUNCTION_TRACE("acpi_power_remove_fs");
472 if (acpi_device_dir(device)) {
473 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
474 acpi_device_dir(device) = NULL;
481 /* --------------------------------------------------------------------------
483 -------------------------------------------------------------------------- */
487 struct acpi_device *device)
490 acpi_status status = AE_OK;
491 struct acpi_power_resource *resource = NULL;
492 union acpi_object acpi_object;
493 struct acpi_buffer buffer = {sizeof(acpi_object), &acpi_object};
495 ACPI_FUNCTION_TRACE("acpi_power_add");
498 return_VALUE(-EINVAL);
500 resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
502 return_VALUE(-ENOMEM);
503 memset(resource, 0, sizeof(struct acpi_power_resource));
505 resource->handle = device->handle;
506 sprintf(resource->name, "%s", device->pnp.bus_id);
507 sprintf(acpi_device_name(device), "%s", ACPI_POWER_DEVICE_NAME);
508 sprintf(acpi_device_class(device), "%s", ACPI_POWER_CLASS);
509 acpi_driver_data(device) = resource;
511 /* Evalute the object to get the system level and resource order. */
512 status = acpi_evaluate_object(resource->handle, NULL, NULL, &buffer);
513 if (ACPI_FAILURE(status)) {
517 resource->system_level = acpi_object.power_resource.system_level;
518 resource->order = acpi_object.power_resource.resource_order;
520 result = acpi_power_get_state(resource);
524 switch (resource->state) {
525 case ACPI_POWER_RESOURCE_STATE_ON:
526 device->power.state = ACPI_STATE_D0;
528 case ACPI_POWER_RESOURCE_STATE_OFF:
529 device->power.state = ACPI_STATE_D3;
532 device->power.state = ACPI_STATE_UNKNOWN;
536 result = acpi_power_add_fs(device);
540 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
541 acpi_device_bid(device), resource->state?"on":"off");
547 return_VALUE(result);
553 struct acpi_device *device,
556 struct acpi_power_resource *resource = NULL;
558 ACPI_FUNCTION_TRACE("acpi_power_remove");
560 if (!device || !acpi_driver_data(device))
561 return_VALUE(-EINVAL);
563 resource = (struct acpi_power_resource *) acpi_driver_data(device);
565 acpi_power_remove_fs(device);
573 static int __init acpi_power_init (void)
577 ACPI_FUNCTION_TRACE("acpi_power_init");
582 INIT_LIST_HEAD(&acpi_power_resource_list);
584 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
586 return_VALUE(-ENODEV);
588 result = acpi_bus_register_driver(&acpi_power_driver);
590 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
591 return_VALUE(-ENODEV);
597 subsys_initcall(acpi_power_init);