ACPI: Introduce acpi_device_sleep_wake function
[linux-2.6/mini2440.git] / drivers / acpi / scan.c
blobf276890cfde7924767cd555d584e1d960269cf86
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13 #define _COMPONENT ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME("scan");
15 #define STRUCT_TO_INT(s) (*((int*)&s))
16 extern struct acpi_device *acpi_root;
18 #define ACPI_BUS_CLASS "system_bus"
19 #define ACPI_BUS_HID "LNXSYBUS"
20 #define ACPI_BUS_DEVICE_NAME "System Bus"
22 static LIST_HEAD(acpi_device_list);
23 static LIST_HEAD(acpi_bus_id_list);
24 DEFINE_SPINLOCK(acpi_device_lock);
25 LIST_HEAD(acpi_wakeup_device_list);
27 struct acpi_device_bus_id{
28 char bus_id[15];
29 unsigned int instance_no;
30 struct list_head node;
34 * Creates hid/cid(s) string needed for modalias and uevent
35 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
36 * char *modalias: "acpi:IBM0001:ACPI0001"
38 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
39 int size)
41 int len;
42 int count;
44 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
45 return -ENODEV;
47 len = snprintf(modalias, size, "acpi:");
48 size -= len;
50 if (acpi_dev->flags.hardware_id) {
51 count = snprintf(&modalias[len], size, "%s:",
52 acpi_dev->pnp.hardware_id);
53 if (count < 0 || count >= size)
54 return -EINVAL;
55 len += count;
56 size -= count;
59 if (acpi_dev->flags.compatible_ids) {
60 struct acpi_compatible_id_list *cid_list;
61 int i;
63 cid_list = acpi_dev->pnp.cid_list;
64 for (i = 0; i < cid_list->count; i++) {
65 count = snprintf(&modalias[len], size, "%s:",
66 cid_list->id[i].value);
67 if (count < 0 || count >= size) {
68 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
69 acpi_dev->pnp.device_name, i);
70 break;
72 len += count;
73 size -= count;
77 modalias[len] = '\0';
78 return len;
81 static ssize_t
82 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
83 struct acpi_device *acpi_dev = to_acpi_device(dev);
84 int len;
86 /* Device has no HID and no CID or string is >1024 */
87 len = create_modalias(acpi_dev, buf, 1024);
88 if (len <= 0)
89 return 0;
90 buf[len++] = '\n';
91 return len;
93 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
95 static int acpi_eject_operation(acpi_handle handle, int lockable)
97 struct acpi_object_list arg_list;
98 union acpi_object arg;
99 acpi_status status = AE_OK;
102 * TBD: evaluate _PS3?
105 if (lockable) {
106 arg_list.count = 1;
107 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER;
109 arg.integer.value = 0;
110 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
113 arg_list.count = 1;
114 arg_list.pointer = &arg;
115 arg.type = ACPI_TYPE_INTEGER;
116 arg.integer.value = 1;
119 * TBD: _EJD support.
122 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
123 if (ACPI_FAILURE(status)) {
124 return (-ENODEV);
127 return (0);
130 static ssize_t
131 acpi_eject_store(struct device *d, struct device_attribute *attr,
132 const char *buf, size_t count)
134 int result;
135 int ret = count;
136 int islockable;
137 acpi_status status;
138 acpi_handle handle;
139 acpi_object_type type = 0;
140 struct acpi_device *acpi_device = to_acpi_device(d);
142 if ((!count) || (buf[0] != '1')) {
143 return -EINVAL;
145 #ifndef FORCE_EJECT
146 if (acpi_device->driver == NULL) {
147 ret = -ENODEV;
148 goto err;
150 #endif
151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
153 ret = -ENODEV;
154 goto err;
157 islockable = acpi_device->flags.lockable;
158 handle = acpi_device->handle;
160 result = acpi_bus_trim(acpi_device, 1);
162 if (!result)
163 result = acpi_eject_operation(handle, islockable);
165 if (result) {
166 ret = -EBUSY;
168 err:
169 return ret;
172 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
174 static ssize_t
175 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
180 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
182 static ssize_t
183 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
184 struct acpi_device *acpi_dev = to_acpi_device(dev);
185 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
186 int result;
188 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
189 if(result)
190 goto end;
192 result = sprintf(buf, "%s\n", (char*)path.pointer);
193 kfree(path.pointer);
194 end:
195 return result;
197 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
199 static int acpi_device_setup_files(struct acpi_device *dev)
201 acpi_status status;
202 acpi_handle temp;
203 int result = 0;
206 * Devices gotten from FADT don't have a "path" attribute
208 if(dev->handle) {
209 result = device_create_file(&dev->dev, &dev_attr_path);
210 if(result)
211 goto end;
214 if(dev->flags.hardware_id) {
215 result = device_create_file(&dev->dev, &dev_attr_hid);
216 if(result)
217 goto end;
220 if (dev->flags.hardware_id || dev->flags.compatible_ids){
221 result = device_create_file(&dev->dev, &dev_attr_modalias);
222 if(result)
223 goto end;
227 * If device has _EJ0, 'eject' file is created that is used to trigger
228 * hot-removal function from userland.
230 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
231 if (ACPI_SUCCESS(status))
232 result = device_create_file(&dev->dev, &dev_attr_eject);
233 end:
234 return result;
237 static void acpi_device_remove_files(struct acpi_device *dev)
239 acpi_status status;
240 acpi_handle temp;
243 * If device has _EJ0, 'eject' file is created that is used to trigger
244 * hot-removal function from userland.
246 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
247 if (ACPI_SUCCESS(status))
248 device_remove_file(&dev->dev, &dev_attr_eject);
250 if (dev->flags.hardware_id || dev->flags.compatible_ids)
251 device_remove_file(&dev->dev, &dev_attr_modalias);
253 if(dev->flags.hardware_id)
254 device_remove_file(&dev->dev, &dev_attr_hid);
255 if(dev->handle)
256 device_remove_file(&dev->dev, &dev_attr_path);
258 /* --------------------------------------------------------------------------
259 ACPI Bus operations
260 -------------------------------------------------------------------------- */
262 int acpi_match_device_ids(struct acpi_device *device,
263 const struct acpi_device_id *ids)
265 const struct acpi_device_id *id;
267 if (device->flags.hardware_id) {
268 for (id = ids; id->id[0]; id++) {
269 if (!strcmp((char*)id->id, device->pnp.hardware_id))
270 return 0;
274 if (device->flags.compatible_ids) {
275 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
276 int i;
278 for (id = ids; id->id[0]; id++) {
279 /* compare multiple _CID entries against driver ids */
280 for (i = 0; i < cid_list->count; i++) {
281 if (!strcmp((char*)id->id,
282 cid_list->id[i].value))
283 return 0;
288 return -ENOENT;
290 EXPORT_SYMBOL(acpi_match_device_ids);
292 static void acpi_device_release(struct device *dev)
294 struct acpi_device *acpi_dev = to_acpi_device(dev);
296 kfree(acpi_dev->pnp.cid_list);
297 kfree(acpi_dev);
300 static int acpi_device_suspend(struct device *dev, pm_message_t state)
302 struct acpi_device *acpi_dev = to_acpi_device(dev);
303 struct acpi_driver *acpi_drv = acpi_dev->driver;
305 if (acpi_drv && acpi_drv->ops.suspend)
306 return acpi_drv->ops.suspend(acpi_dev, state);
307 return 0;
310 static int acpi_device_resume(struct device *dev)
312 struct acpi_device *acpi_dev = to_acpi_device(dev);
313 struct acpi_driver *acpi_drv = acpi_dev->driver;
315 if (acpi_drv && acpi_drv->ops.resume)
316 return acpi_drv->ops.resume(acpi_dev);
317 return 0;
320 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
322 struct acpi_device *acpi_dev = to_acpi_device(dev);
323 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
325 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
328 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
330 struct acpi_device *acpi_dev = to_acpi_device(dev);
331 int len;
333 if (add_uevent_var(env, "MODALIAS="))
334 return -ENOMEM;
335 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
336 sizeof(env->buf) - env->buflen);
337 if (len >= (sizeof(env->buf) - env->buflen))
338 return -ENOMEM;
339 env->buflen += len;
340 return 0;
343 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
344 static int acpi_start_single_object(struct acpi_device *);
345 static int acpi_device_probe(struct device * dev)
347 struct acpi_device *acpi_dev = to_acpi_device(dev);
348 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
349 int ret;
351 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
352 if (!ret) {
353 if (acpi_dev->bus_ops.acpi_op_start)
354 acpi_start_single_object(acpi_dev);
355 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
356 "Found driver [%s] for device [%s]\n",
357 acpi_drv->name, acpi_dev->pnp.bus_id));
358 get_device(dev);
360 return ret;
363 static int acpi_device_remove(struct device * dev)
365 struct acpi_device *acpi_dev = to_acpi_device(dev);
366 struct acpi_driver *acpi_drv = acpi_dev->driver;
368 if (acpi_drv) {
369 if (acpi_drv->ops.stop)
370 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
371 if (acpi_drv->ops.remove)
372 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
374 acpi_dev->driver = NULL;
375 acpi_driver_data(dev) = NULL;
377 put_device(dev);
378 return 0;
381 static void acpi_device_shutdown(struct device *dev)
383 struct acpi_device *acpi_dev = to_acpi_device(dev);
384 struct acpi_driver *acpi_drv = acpi_dev->driver;
386 if (acpi_drv && acpi_drv->ops.shutdown)
387 acpi_drv->ops.shutdown(acpi_dev);
389 return ;
392 struct bus_type acpi_bus_type = {
393 .name = "acpi",
394 .suspend = acpi_device_suspend,
395 .resume = acpi_device_resume,
396 .shutdown = acpi_device_shutdown,
397 .match = acpi_bus_match,
398 .probe = acpi_device_probe,
399 .remove = acpi_device_remove,
400 .uevent = acpi_device_uevent,
403 static int acpi_device_register(struct acpi_device *device,
404 struct acpi_device *parent)
406 int result;
407 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
408 int found = 0;
410 * Linkage
411 * -------
412 * Link this device to its parent and siblings.
414 INIT_LIST_HEAD(&device->children);
415 INIT_LIST_HEAD(&device->node);
416 INIT_LIST_HEAD(&device->g_list);
417 INIT_LIST_HEAD(&device->wakeup_list);
419 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
420 if (!new_bus_id) {
421 printk(KERN_ERR PREFIX "Memory allocation error\n");
422 return -ENOMEM;
425 spin_lock(&acpi_device_lock);
427 * Find suitable bus_id and instance number in acpi_bus_id_list
428 * If failed, create one and link it into acpi_bus_id_list
430 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
431 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
432 acpi_device_bus_id->instance_no ++;
433 found = 1;
434 kfree(new_bus_id);
435 break;
438 if(!found) {
439 acpi_device_bus_id = new_bus_id;
440 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
441 acpi_device_bus_id->instance_no = 0;
442 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
444 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
446 if (device->parent) {
447 list_add_tail(&device->node, &device->parent->children);
448 list_add_tail(&device->g_list, &device->parent->g_list);
449 } else
450 list_add_tail(&device->g_list, &acpi_device_list);
451 if (device->wakeup.flags.valid)
452 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
453 spin_unlock(&acpi_device_lock);
455 if (device->parent)
456 device->dev.parent = &parent->dev;
457 device->dev.bus = &acpi_bus_type;
458 device_initialize(&device->dev);
459 device->dev.release = &acpi_device_release;
460 result = device_add(&device->dev);
461 if(result) {
462 printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id);
463 goto end;
466 result = acpi_device_setup_files(device);
467 if(result)
468 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
470 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
471 return 0;
472 end:
473 spin_lock(&acpi_device_lock);
474 if (device->parent) {
475 list_del(&device->node);
476 list_del(&device->g_list);
477 } else
478 list_del(&device->g_list);
479 list_del(&device->wakeup_list);
480 spin_unlock(&acpi_device_lock);
481 return result;
484 static void acpi_device_unregister(struct acpi_device *device, int type)
486 spin_lock(&acpi_device_lock);
487 if (device->parent) {
488 list_del(&device->node);
489 list_del(&device->g_list);
490 } else
491 list_del(&device->g_list);
493 list_del(&device->wakeup_list);
494 spin_unlock(&acpi_device_lock);
496 acpi_detach_data(device->handle, acpi_bus_data_handler);
498 acpi_device_remove_files(device);
499 device_unregister(&device->dev);
502 /* --------------------------------------------------------------------------
503 Driver Management
504 -------------------------------------------------------------------------- */
506 * acpi_bus_driver_init - add a device to a driver
507 * @device: the device to add and initialize
508 * @driver: driver for the device
510 * Used to initialize a device via its device driver. Called whenever a
511 * driver is bound to a device. Invokes the driver's add() ops.
513 static int
514 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
516 int result = 0;
519 if (!device || !driver)
520 return -EINVAL;
522 if (!driver->ops.add)
523 return -ENOSYS;
525 result = driver->ops.add(device);
526 if (result) {
527 device->driver = NULL;
528 acpi_driver_data(device) = NULL;
529 return result;
532 device->driver = driver;
535 * TBD - Configuration Management: Assign resources to device based
536 * upon possible configuration and currently allocated resources.
539 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
540 "Driver successfully bound to device\n"));
541 return 0;
544 static int acpi_start_single_object(struct acpi_device *device)
546 int result = 0;
547 struct acpi_driver *driver;
550 if (!(driver = device->driver))
551 return 0;
553 if (driver->ops.start) {
554 result = driver->ops.start(device);
555 if (result && driver->ops.remove)
556 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
559 return result;
563 * acpi_bus_register_driver - register a driver with the ACPI bus
564 * @driver: driver being registered
566 * Registers a driver with the ACPI bus. Searches the namespace for all
567 * devices that match the driver's criteria and binds. Returns zero for
568 * success or a negative error status for failure.
570 int acpi_bus_register_driver(struct acpi_driver *driver)
572 int ret;
574 if (acpi_disabled)
575 return -ENODEV;
576 driver->drv.name = driver->name;
577 driver->drv.bus = &acpi_bus_type;
578 driver->drv.owner = driver->owner;
580 ret = driver_register(&driver->drv);
581 return ret;
584 EXPORT_SYMBOL(acpi_bus_register_driver);
587 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
588 * @driver: driver to unregister
590 * Unregisters a driver with the ACPI bus. Searches the namespace for all
591 * devices that match the driver's criteria and unbinds.
593 void acpi_bus_unregister_driver(struct acpi_driver *driver)
595 driver_unregister(&driver->drv);
598 EXPORT_SYMBOL(acpi_bus_unregister_driver);
600 /* --------------------------------------------------------------------------
601 Device Enumeration
602 -------------------------------------------------------------------------- */
603 acpi_status
604 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
606 acpi_status status;
607 acpi_handle tmp;
608 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
609 union acpi_object *obj;
611 status = acpi_get_handle(handle, "_EJD", &tmp);
612 if (ACPI_FAILURE(status))
613 return status;
615 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
616 if (ACPI_SUCCESS(status)) {
617 obj = buffer.pointer;
618 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
619 ejd);
620 kfree(buffer.pointer);
622 return status;
624 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
626 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
629 /* TBD */
631 return;
634 static int acpi_bus_get_perf_flags(struct acpi_device *device)
636 device->performance.state = ACPI_STATE_UNKNOWN;
637 return 0;
640 static acpi_status
641 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
642 union acpi_object *package)
644 int i = 0;
645 union acpi_object *element = NULL;
647 if (!device || !package || (package->package.count < 2))
648 return AE_BAD_PARAMETER;
650 element = &(package->package.elements[0]);
651 if (!element)
652 return AE_BAD_PARAMETER;
653 if (element->type == ACPI_TYPE_PACKAGE) {
654 if ((element->package.count < 2) ||
655 (element->package.elements[0].type !=
656 ACPI_TYPE_LOCAL_REFERENCE)
657 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
658 return AE_BAD_DATA;
659 device->wakeup.gpe_device =
660 element->package.elements[0].reference.handle;
661 device->wakeup.gpe_number =
662 (u32) element->package.elements[1].integer.value;
663 } else if (element->type == ACPI_TYPE_INTEGER) {
664 device->wakeup.gpe_number = element->integer.value;
665 } else
666 return AE_BAD_DATA;
668 element = &(package->package.elements[1]);
669 if (element->type != ACPI_TYPE_INTEGER) {
670 return AE_BAD_DATA;
672 device->wakeup.sleep_state = element->integer.value;
674 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
675 return AE_NO_MEMORY;
677 device->wakeup.resources.count = package->package.count - 2;
678 for (i = 0; i < device->wakeup.resources.count; i++) {
679 element = &(package->package.elements[i + 2]);
680 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
681 return AE_BAD_DATA;
683 device->wakeup.resources.handles[i] = element->reference.handle;
686 return AE_OK;
689 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
691 acpi_status status = 0;
692 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
693 union acpi_object *package = NULL;
694 int psw_error;
696 struct acpi_device_id button_device_ids[] = {
697 {"PNP0C0D", 0},
698 {"PNP0C0C", 0},
699 {"PNP0C0E", 0},
700 {"", 0},
703 /* _PRW */
704 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
705 if (ACPI_FAILURE(status)) {
706 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
707 goto end;
710 package = (union acpi_object *)buffer.pointer;
711 status = acpi_bus_extract_wakeup_device_power_package(device, package);
712 if (ACPI_FAILURE(status)) {
713 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
714 goto end;
717 kfree(buffer.pointer);
719 device->wakeup.flags.valid = 1;
720 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
721 * system for the ACPI device with the _PRW object.
722 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
723 * So it is necessary to call _DSW object first. Only when it is not
724 * present will the _PSW object used.
726 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
727 if (psw_error)
728 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
729 "error in _DSW or _PSW evaluation\n"));
731 /* Power button, Lid switch always enable wakeup */
732 if (!acpi_match_device_ids(device, button_device_ids))
733 device->wakeup.flags.run_wake = 1;
735 end:
736 if (ACPI_FAILURE(status))
737 device->flags.wake_capable = 0;
738 return 0;
741 static int acpi_bus_get_power_flags(struct acpi_device *device)
743 acpi_status status = 0;
744 acpi_handle handle = NULL;
745 u32 i = 0;
749 * Power Management Flags
751 status = acpi_get_handle(device->handle, "_PSC", &handle);
752 if (ACPI_SUCCESS(status))
753 device->power.flags.explicit_get = 1;
754 status = acpi_get_handle(device->handle, "_IRC", &handle);
755 if (ACPI_SUCCESS(status))
756 device->power.flags.inrush_current = 1;
759 * Enumerate supported power management states
761 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
762 struct acpi_device_power_state *ps = &device->power.states[i];
763 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
765 /* Evaluate "_PRx" to se if power resources are referenced */
766 acpi_evaluate_reference(device->handle, object_name, NULL,
767 &ps->resources);
768 if (ps->resources.count) {
769 device->power.flags.power_resources = 1;
770 ps->flags.valid = 1;
773 /* Evaluate "_PSx" to see if we can do explicit sets */
774 object_name[2] = 'S';
775 status = acpi_get_handle(device->handle, object_name, &handle);
776 if (ACPI_SUCCESS(status)) {
777 ps->flags.explicit_set = 1;
778 ps->flags.valid = 1;
781 /* State is valid if we have some power control */
782 if (ps->resources.count || ps->flags.explicit_set)
783 ps->flags.valid = 1;
785 ps->power = -1; /* Unknown - driver assigned */
786 ps->latency = -1; /* Unknown - driver assigned */
789 /* Set defaults for D0 and D3 states (always valid) */
790 device->power.states[ACPI_STATE_D0].flags.valid = 1;
791 device->power.states[ACPI_STATE_D0].power = 100;
792 device->power.states[ACPI_STATE_D3].flags.valid = 1;
793 device->power.states[ACPI_STATE_D3].power = 0;
795 /* TBD: System wake support and resource requirements. */
797 device->power.state = ACPI_STATE_UNKNOWN;
799 return 0;
802 static int acpi_bus_get_flags(struct acpi_device *device)
804 acpi_status status = AE_OK;
805 acpi_handle temp = NULL;
808 /* Presence of _STA indicates 'dynamic_status' */
809 status = acpi_get_handle(device->handle, "_STA", &temp);
810 if (ACPI_SUCCESS(status))
811 device->flags.dynamic_status = 1;
813 /* Presence of _CID indicates 'compatible_ids' */
814 status = acpi_get_handle(device->handle, "_CID", &temp);
815 if (ACPI_SUCCESS(status))
816 device->flags.compatible_ids = 1;
818 /* Presence of _RMV indicates 'removable' */
819 status = acpi_get_handle(device->handle, "_RMV", &temp);
820 if (ACPI_SUCCESS(status))
821 device->flags.removable = 1;
823 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
824 status = acpi_get_handle(device->handle, "_EJD", &temp);
825 if (ACPI_SUCCESS(status))
826 device->flags.ejectable = 1;
827 else {
828 status = acpi_get_handle(device->handle, "_EJ0", &temp);
829 if (ACPI_SUCCESS(status))
830 device->flags.ejectable = 1;
833 /* Presence of _LCK indicates 'lockable' */
834 status = acpi_get_handle(device->handle, "_LCK", &temp);
835 if (ACPI_SUCCESS(status))
836 device->flags.lockable = 1;
838 /* Presence of _PS0|_PR0 indicates 'power manageable' */
839 status = acpi_get_handle(device->handle, "_PS0", &temp);
840 if (ACPI_FAILURE(status))
841 status = acpi_get_handle(device->handle, "_PR0", &temp);
842 if (ACPI_SUCCESS(status))
843 device->flags.power_manageable = 1;
845 /* Presence of _PRW indicates wake capable */
846 status = acpi_get_handle(device->handle, "_PRW", &temp);
847 if (ACPI_SUCCESS(status))
848 device->flags.wake_capable = 1;
850 /* TBD: Performance management */
852 return 0;
855 static void acpi_device_get_busid(struct acpi_device *device,
856 acpi_handle handle, int type)
858 char bus_id[5] = { '?', 0 };
859 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
860 int i = 0;
863 * Bus ID
864 * ------
865 * The device's Bus ID is simply the object name.
866 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
868 switch (type) {
869 case ACPI_BUS_TYPE_SYSTEM:
870 strcpy(device->pnp.bus_id, "ACPI");
871 break;
872 case ACPI_BUS_TYPE_POWER_BUTTON:
873 strcpy(device->pnp.bus_id, "PWRF");
874 break;
875 case ACPI_BUS_TYPE_SLEEP_BUTTON:
876 strcpy(device->pnp.bus_id, "SLPF");
877 break;
878 default:
879 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
880 /* Clean up trailing underscores (if any) */
881 for (i = 3; i > 1; i--) {
882 if (bus_id[i] == '_')
883 bus_id[i] = '\0';
884 else
885 break;
887 strcpy(device->pnp.bus_id, bus_id);
888 break;
892 static int
893 acpi_video_bus_match(struct acpi_device *device)
895 acpi_handle h_dummy;
897 if (!device)
898 return -EINVAL;
900 /* Since there is no HID, CID for ACPI Video drivers, we have
901 * to check well known required nodes for each feature we support.
904 /* Does this device able to support video switching ? */
905 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) &&
906 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy)))
907 return 0;
909 /* Does this device able to retrieve a video ROM ? */
910 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy)))
911 return 0;
913 /* Does this device able to configure which video head to be POSTed ? */
914 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) &&
915 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) &&
916 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy)))
917 return 0;
919 return -ENODEV;
923 * acpi_bay_match - see if a device is an ejectable driver bay
925 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
926 * then we can safely call it an ejectable drive bay
928 static int acpi_bay_match(struct acpi_device *device){
929 acpi_status status;
930 acpi_handle handle;
931 acpi_handle tmp;
932 acpi_handle phandle;
934 handle = device->handle;
936 status = acpi_get_handle(handle, "_EJ0", &tmp);
937 if (ACPI_FAILURE(status))
938 return -ENODEV;
940 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
941 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
942 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
943 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
944 return 0;
946 if (acpi_get_parent(handle, &phandle))
947 return -ENODEV;
949 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
950 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
951 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
952 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
953 return 0;
955 return -ENODEV;
959 * acpi_dock_match - see if a device has a _DCK method
961 static int acpi_dock_match(struct acpi_device *device)
963 acpi_handle tmp;
964 return acpi_get_handle(device->handle, "_DCK", &tmp);
967 static void acpi_device_set_id(struct acpi_device *device,
968 struct acpi_device *parent, acpi_handle handle,
969 int type)
971 struct acpi_device_info *info;
972 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
973 char *hid = NULL;
974 char *uid = NULL;
975 struct acpi_compatible_id_list *cid_list = NULL;
976 const char *cid_add = NULL;
977 acpi_status status;
979 switch (type) {
980 case ACPI_BUS_TYPE_DEVICE:
981 status = acpi_get_object_info(handle, &buffer);
982 if (ACPI_FAILURE(status)) {
983 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
984 return;
987 info = buffer.pointer;
988 if (info->valid & ACPI_VALID_HID)
989 hid = info->hardware_id.value;
990 if (info->valid & ACPI_VALID_UID)
991 uid = info->unique_id.value;
992 if (info->valid & ACPI_VALID_CID)
993 cid_list = &info->compatibility_id;
994 if (info->valid & ACPI_VALID_ADR) {
995 device->pnp.bus_address = info->address;
996 device->flags.bus_address = 1;
999 /* If we have a video/bay/dock device, add our selfdefined
1000 HID to the CID list. Like that the video/bay/dock drivers
1001 will get autoloaded and the device might still match
1002 against another driver.
1004 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
1005 cid_add = ACPI_VIDEO_HID;
1006 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1007 cid_add = ACPI_BAY_HID;
1008 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1009 cid_add = ACPI_DOCK_HID;
1011 break;
1012 case ACPI_BUS_TYPE_POWER:
1013 hid = ACPI_POWER_HID;
1014 break;
1015 case ACPI_BUS_TYPE_PROCESSOR:
1016 hid = ACPI_PROCESSOR_HID;
1017 break;
1018 case ACPI_BUS_TYPE_SYSTEM:
1019 hid = ACPI_SYSTEM_HID;
1020 break;
1021 case ACPI_BUS_TYPE_THERMAL:
1022 hid = ACPI_THERMAL_HID;
1023 break;
1024 case ACPI_BUS_TYPE_POWER_BUTTON:
1025 hid = ACPI_BUTTON_HID_POWERF;
1026 break;
1027 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1028 hid = ACPI_BUTTON_HID_SLEEPF;
1029 break;
1033 * \_SB
1034 * ----
1035 * Fix for the system root bus device -- the only root-level device.
1037 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1038 hid = ACPI_BUS_HID;
1039 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1040 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1043 if (hid) {
1044 strcpy(device->pnp.hardware_id, hid);
1045 device->flags.hardware_id = 1;
1047 if (uid) {
1048 strcpy(device->pnp.unique_id, uid);
1049 device->flags.unique_id = 1;
1051 if (cid_list || cid_add) {
1052 struct acpi_compatible_id_list *list;
1053 int size = 0;
1054 int count = 0;
1056 if (cid_list) {
1057 size = cid_list->size;
1058 } else if (cid_add) {
1059 size = sizeof(struct acpi_compatible_id_list);
1060 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1061 if (!cid_list) {
1062 printk(KERN_ERR "Memory allocation error\n");
1063 kfree(buffer.pointer);
1064 return;
1065 } else {
1066 cid_list->count = 0;
1067 cid_list->size = size;
1070 if (cid_add)
1071 size += sizeof(struct acpi_compatible_id);
1072 list = kmalloc(size, GFP_KERNEL);
1074 if (list) {
1075 if (cid_list) {
1076 memcpy(list, cid_list, cid_list->size);
1077 count = cid_list->count;
1079 if (cid_add) {
1080 strncpy(list->id[count].value, cid_add,
1081 ACPI_MAX_CID_LENGTH);
1082 count++;
1083 device->flags.compatible_ids = 1;
1085 list->size = size;
1086 list->count = count;
1087 device->pnp.cid_list = list;
1088 } else
1089 printk(KERN_ERR PREFIX "Memory allocation error\n");
1092 kfree(buffer.pointer);
1095 static int acpi_device_set_context(struct acpi_device *device, int type)
1097 acpi_status status = AE_OK;
1098 int result = 0;
1100 * Context
1101 * -------
1102 * Attach this 'struct acpi_device' to the ACPI object. This makes
1103 * resolutions from handle->device very efficient. Note that we need
1104 * to be careful with fixed-feature devices as they all attach to the
1105 * root object.
1107 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1108 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1109 status = acpi_attach_data(device->handle,
1110 acpi_bus_data_handler, device);
1112 if (ACPI_FAILURE(status)) {
1113 printk(KERN_ERR PREFIX "Error attaching device data\n");
1114 result = -ENODEV;
1117 return result;
1120 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1122 if (!dev)
1123 return -EINVAL;
1125 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1126 device_release_driver(&dev->dev);
1128 if (!rmdevice)
1129 return 0;
1132 * unbind _ADR-Based Devices when hot removal
1134 if (dev->flags.bus_address) {
1135 if ((dev->parent) && (dev->parent->ops.unbind))
1136 dev->parent->ops.unbind(dev);
1138 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1140 return 0;
1143 static int
1144 acpi_is_child_device(struct acpi_device *device,
1145 int (*matcher)(struct acpi_device *))
1147 int result = -ENODEV;
1149 do {
1150 if (ACPI_SUCCESS(matcher(device)))
1151 return AE_OK;
1152 } while ((device = device->parent));
1154 return result;
1157 static int
1158 acpi_add_single_object(struct acpi_device **child,
1159 struct acpi_device *parent, acpi_handle handle, int type,
1160 struct acpi_bus_ops *ops)
1162 int result = 0;
1163 struct acpi_device *device = NULL;
1166 if (!child)
1167 return -EINVAL;
1169 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1170 if (!device) {
1171 printk(KERN_ERR PREFIX "Memory allocation error\n");
1172 return -ENOMEM;
1175 device->handle = handle;
1176 device->parent = parent;
1177 device->bus_ops = *ops; /* workround for not call .start */
1180 acpi_device_get_busid(device, handle, type);
1183 * Flags
1184 * -----
1185 * Get prior to calling acpi_bus_get_status() so we know whether
1186 * or not _STA is present. Note that we only look for object
1187 * handles -- cannot evaluate objects until we know the device is
1188 * present and properly initialized.
1190 result = acpi_bus_get_flags(device);
1191 if (result)
1192 goto end;
1195 * Status
1196 * ------
1197 * See if the device is present. We always assume that non-Device
1198 * and non-Processor objects (e.g. thermal zones, power resources,
1199 * etc.) are present, functioning, etc. (at least when parent object
1200 * is present). Note that _STA has a different meaning for some
1201 * objects (e.g. power resources) so we need to be careful how we use
1202 * it.
1204 switch (type) {
1205 case ACPI_BUS_TYPE_PROCESSOR:
1206 case ACPI_BUS_TYPE_DEVICE:
1207 result = acpi_bus_get_status(device);
1208 if (ACPI_FAILURE(result)) {
1209 result = -ENODEV;
1210 goto end;
1212 if (!device->status.present) {
1213 /* Bay and dock should be handled even if absent */
1214 if (!ACPI_SUCCESS(
1215 acpi_is_child_device(device, acpi_bay_match)) &&
1216 !ACPI_SUCCESS(
1217 acpi_is_child_device(device, acpi_dock_match))) {
1218 result = -ENODEV;
1219 goto end;
1222 break;
1223 default:
1224 STRUCT_TO_INT(device->status) =
1225 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1226 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1227 break;
1231 * Initialize Device
1232 * -----------------
1233 * TBD: Synch with Core's enumeration/initialization process.
1237 * Hardware ID, Unique ID, & Bus Address
1238 * -------------------------------------
1240 acpi_device_set_id(device, parent, handle, type);
1243 * Power Management
1244 * ----------------
1246 if (device->flags.power_manageable) {
1247 result = acpi_bus_get_power_flags(device);
1248 if (result)
1249 goto end;
1253 * Wakeup device management
1254 *-----------------------
1256 if (device->flags.wake_capable) {
1257 result = acpi_bus_get_wakeup_device_flags(device);
1258 if (result)
1259 goto end;
1263 * Performance Management
1264 * ----------------------
1266 if (device->flags.performance_manageable) {
1267 result = acpi_bus_get_perf_flags(device);
1268 if (result)
1269 goto end;
1272 if ((result = acpi_device_set_context(device, type)))
1273 goto end;
1275 result = acpi_device_register(device, parent);
1278 * Bind _ADR-Based Devices when hot add
1280 if (device->flags.bus_address) {
1281 if (device->parent && device->parent->ops.bind)
1282 device->parent->ops.bind(device);
1285 end:
1286 if (!result)
1287 *child = device;
1288 else {
1289 kfree(device->pnp.cid_list);
1290 kfree(device);
1293 return result;
1296 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1298 acpi_status status = AE_OK;
1299 struct acpi_device *parent = NULL;
1300 struct acpi_device *child = NULL;
1301 acpi_handle phandle = NULL;
1302 acpi_handle chandle = NULL;
1303 acpi_object_type type = 0;
1304 u32 level = 1;
1307 if (!start)
1308 return -EINVAL;
1310 parent = start;
1311 phandle = start->handle;
1314 * Parse through the ACPI namespace, identify all 'devices', and
1315 * create a new 'struct acpi_device' for each.
1317 while ((level > 0) && parent) {
1319 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1320 chandle, &chandle);
1323 * If this scope is exhausted then move our way back up.
1325 if (ACPI_FAILURE(status)) {
1326 level--;
1327 chandle = phandle;
1328 acpi_get_parent(phandle, &phandle);
1329 if (parent->parent)
1330 parent = parent->parent;
1331 continue;
1334 status = acpi_get_type(chandle, &type);
1335 if (ACPI_FAILURE(status))
1336 continue;
1339 * If this is a scope object then parse it (depth-first).
1341 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1342 level++;
1343 phandle = chandle;
1344 chandle = NULL;
1345 continue;
1349 * We're only interested in objects that we consider 'devices'.
1351 switch (type) {
1352 case ACPI_TYPE_DEVICE:
1353 type = ACPI_BUS_TYPE_DEVICE;
1354 break;
1355 case ACPI_TYPE_PROCESSOR:
1356 type = ACPI_BUS_TYPE_PROCESSOR;
1357 break;
1358 case ACPI_TYPE_THERMAL:
1359 type = ACPI_BUS_TYPE_THERMAL;
1360 break;
1361 case ACPI_TYPE_POWER:
1362 type = ACPI_BUS_TYPE_POWER;
1363 break;
1364 default:
1365 continue;
1368 if (ops->acpi_op_add)
1369 status = acpi_add_single_object(&child, parent,
1370 chandle, type, ops);
1371 else
1372 status = acpi_bus_get_device(chandle, &child);
1374 if (ACPI_FAILURE(status))
1375 continue;
1377 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1378 status = acpi_start_single_object(child);
1379 if (ACPI_FAILURE(status))
1380 continue;
1384 * If the device is present, enabled, and functioning then
1385 * parse its scope (depth-first). Note that we need to
1386 * represent absent devices to facilitate PnP notifications
1387 * -- but only the subtree head (not all of its children,
1388 * which will be enumerated when the parent is inserted).
1390 * TBD: Need notifications and other detection mechanisms
1391 * in place before we can fully implement this.
1393 if (child->status.present) {
1394 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1395 NULL, NULL);
1396 if (ACPI_SUCCESS(status)) {
1397 level++;
1398 phandle = chandle;
1399 chandle = NULL;
1400 parent = child;
1405 return 0;
1409 acpi_bus_add(struct acpi_device **child,
1410 struct acpi_device *parent, acpi_handle handle, int type)
1412 int result;
1413 struct acpi_bus_ops ops;
1415 memset(&ops, 0, sizeof(ops));
1416 ops.acpi_op_add = 1;
1418 result = acpi_add_single_object(child, parent, handle, type, &ops);
1419 if (!result)
1420 result = acpi_bus_scan(*child, &ops);
1422 return result;
1425 EXPORT_SYMBOL(acpi_bus_add);
1427 int acpi_bus_start(struct acpi_device *device)
1429 int result;
1430 struct acpi_bus_ops ops;
1433 if (!device)
1434 return -EINVAL;
1436 result = acpi_start_single_object(device);
1437 if (!result) {
1438 memset(&ops, 0, sizeof(ops));
1439 ops.acpi_op_start = 1;
1440 result = acpi_bus_scan(device, &ops);
1442 return result;
1445 EXPORT_SYMBOL(acpi_bus_start);
1447 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1449 acpi_status status;
1450 struct acpi_device *parent, *child;
1451 acpi_handle phandle, chandle;
1452 acpi_object_type type;
1453 u32 level = 1;
1454 int err = 0;
1456 parent = start;
1457 phandle = start->handle;
1458 child = chandle = NULL;
1460 while ((level > 0) && parent && (!err)) {
1461 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1462 chandle, &chandle);
1465 * If this scope is exhausted then move our way back up.
1467 if (ACPI_FAILURE(status)) {
1468 level--;
1469 chandle = phandle;
1470 acpi_get_parent(phandle, &phandle);
1471 child = parent;
1472 parent = parent->parent;
1474 if (level == 0)
1475 err = acpi_bus_remove(child, rmdevice);
1476 else
1477 err = acpi_bus_remove(child, 1);
1479 continue;
1482 status = acpi_get_type(chandle, &type);
1483 if (ACPI_FAILURE(status)) {
1484 continue;
1487 * If there is a device corresponding to chandle then
1488 * parse it (depth-first).
1490 if (acpi_bus_get_device(chandle, &child) == 0) {
1491 level++;
1492 phandle = chandle;
1493 chandle = NULL;
1494 parent = child;
1496 continue;
1498 return err;
1500 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1503 static int acpi_bus_scan_fixed(struct acpi_device *root)
1505 int result = 0;
1506 struct acpi_device *device = NULL;
1507 struct acpi_bus_ops ops;
1509 if (!root)
1510 return -ENODEV;
1512 memset(&ops, 0, sizeof(ops));
1513 ops.acpi_op_add = 1;
1514 ops.acpi_op_start = 1;
1517 * Enumerate all fixed-feature devices.
1519 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1520 result = acpi_add_single_object(&device, acpi_root,
1521 NULL,
1522 ACPI_BUS_TYPE_POWER_BUTTON,
1523 &ops);
1526 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1527 result = acpi_add_single_object(&device, acpi_root,
1528 NULL,
1529 ACPI_BUS_TYPE_SLEEP_BUTTON,
1530 &ops);
1533 return result;
1536 int __init acpi_boot_ec_enable(void);
1538 static int __init acpi_scan_init(void)
1540 int result;
1541 struct acpi_bus_ops ops;
1544 if (acpi_disabled)
1545 return 0;
1547 memset(&ops, 0, sizeof(ops));
1548 ops.acpi_op_add = 1;
1549 ops.acpi_op_start = 1;
1551 result = bus_register(&acpi_bus_type);
1552 if (result) {
1553 /* We don't want to quit even if we failed to add suspend/resume */
1554 printk(KERN_ERR PREFIX "Could not register bus type\n");
1558 * Create the root device in the bus's device tree
1560 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1561 ACPI_BUS_TYPE_SYSTEM, &ops);
1562 if (result)
1563 goto Done;
1566 * Enumerate devices in the ACPI namespace.
1568 result = acpi_bus_scan_fixed(acpi_root);
1570 /* EC region might be needed at bus_scan, so enable it now */
1571 acpi_boot_ec_enable();
1573 if (!result)
1574 result = acpi_bus_scan(acpi_root, &ops);
1576 if (result)
1577 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1579 Done:
1580 return result;
1583 subsys_initcall(acpi_scan_init);