ieee1394: sbp2: fix sbp2_remove_device for error cases
[usb.git] / drivers / acpi / scan.c
blobbe74347d135435e5c5b4508b8144528bc58a0459
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 int create_modalias(struct acpi_device *acpi_dev, char *modalias, int size){
40 int len;
42 if (!acpi_dev->flags.hardware_id)
43 return -ENODEV;
45 len = snprintf(modalias, size, "acpi:%s:",
46 acpi_dev->pnp.hardware_id);
47 if (len < 0 || len >= size)
48 return -EINVAL;
49 size -= len;
51 if (acpi_dev->flags.compatible_ids) {
52 struct acpi_compatible_id_list *cid_list;
53 int i;
54 int count;
56 cid_list = acpi_dev->pnp.cid_list;
57 for (i = 0; i < cid_list->count; i++) {
58 count = snprintf(&modalias[len], size, "%s:",
59 cid_list->id[i].value);
60 if (count < 0 || count >= size) {
61 printk(KERN_ERR "acpi: %s cid[%i] exceeds event buffer size",
62 acpi_dev->pnp.device_name, i);
63 break;
65 len += count;
66 size -= count;
70 modalias[len] = '\0';
71 return len;
74 static ssize_t
75 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
76 struct acpi_device *acpi_dev = to_acpi_device(dev);
77 int len;
79 /* Device has no HID and no CID or string is >1024 */
80 len = create_modalias(acpi_dev, buf, 1024);
81 if (len <= 0)
82 return 0;
83 buf[len++] = '\n';
84 return len;
86 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
88 static int acpi_eject_operation(acpi_handle handle, int lockable)
90 struct acpi_object_list arg_list;
91 union acpi_object arg;
92 acpi_status status = AE_OK;
95 * TBD: evaluate _PS3?
98 if (lockable) {
99 arg_list.count = 1;
100 arg_list.pointer = &arg;
101 arg.type = ACPI_TYPE_INTEGER;
102 arg.integer.value = 0;
103 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
106 arg_list.count = 1;
107 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER;
109 arg.integer.value = 1;
112 * TBD: _EJD support.
115 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
116 if (ACPI_FAILURE(status)) {
117 return (-ENODEV);
120 return (0);
123 static ssize_t
124 acpi_eject_store(struct device *d, struct device_attribute *attr,
125 const char *buf, size_t count)
127 int result;
128 int ret = count;
129 int islockable;
130 acpi_status status;
131 acpi_handle handle;
132 acpi_object_type type = 0;
133 struct acpi_device *acpi_device = to_acpi_device(d);
135 if ((!count) || (buf[0] != '1')) {
136 return -EINVAL;
138 #ifndef FORCE_EJECT
139 if (acpi_device->driver == NULL) {
140 ret = -ENODEV;
141 goto err;
143 #endif
144 status = acpi_get_type(acpi_device->handle, &type);
145 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
146 ret = -ENODEV;
147 goto err;
150 islockable = acpi_device->flags.lockable;
151 handle = acpi_device->handle;
153 result = acpi_bus_trim(acpi_device, 1);
155 if (!result)
156 result = acpi_eject_operation(handle, islockable);
158 if (result) {
159 ret = -EBUSY;
161 err:
162 return ret;
165 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
167 static ssize_t
168 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
169 struct acpi_device *acpi_dev = to_acpi_device(dev);
171 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
173 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
175 static ssize_t
176 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
177 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
179 int result;
181 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
182 if(result)
183 goto end;
185 result = sprintf(buf, "%s\n", (char*)path.pointer);
186 kfree(path.pointer);
187 end:
188 return result;
190 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
192 static int acpi_device_setup_files(struct acpi_device *dev)
194 acpi_status status;
195 acpi_handle temp;
196 int result = 0;
199 * Devices gotten from FADT don't have a "path" attribute
201 if(dev->handle) {
202 result = device_create_file(&dev->dev, &dev_attr_path);
203 if(result)
204 goto end;
207 if(dev->flags.hardware_id) {
208 result = device_create_file(&dev->dev, &dev_attr_hid);
209 if(result)
210 goto end;
213 if (dev->flags.hardware_id || dev->flags.compatible_ids){
214 result = device_create_file(&dev->dev, &dev_attr_modalias);
215 if(result)
216 goto end;
220 * If device has _EJ0, 'eject' file is created that is used to trigger
221 * hot-removal function from userland.
223 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
224 if (ACPI_SUCCESS(status))
225 result = device_create_file(&dev->dev, &dev_attr_eject);
226 end:
227 return result;
230 static void acpi_device_remove_files(struct acpi_device *dev)
232 acpi_status status;
233 acpi_handle temp;
236 * If device has _EJ0, 'eject' file is created that is used to trigger
237 * hot-removal function from userland.
239 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
240 if (ACPI_SUCCESS(status))
241 device_remove_file(&dev->dev, &dev_attr_eject);
243 if (dev->flags.hardware_id || dev->flags.compatible_ids)
244 device_remove_file(&dev->dev, &dev_attr_modalias);
246 if(dev->flags.hardware_id)
247 device_remove_file(&dev->dev, &dev_attr_hid);
248 if(dev->handle)
249 device_remove_file(&dev->dev, &dev_attr_path);
251 /* --------------------------------------------------------------------------
252 ACPI Bus operations
253 -------------------------------------------------------------------------- */
255 int acpi_match_device_ids(struct acpi_device *device,
256 const struct acpi_device_id *ids)
258 const struct acpi_device_id *id;
260 if (device->flags.hardware_id) {
261 for (id = ids; id->id[0]; id++) {
262 if (!strcmp((char*)id->id, device->pnp.hardware_id))
263 return 0;
267 if (device->flags.compatible_ids) {
268 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
269 int i;
271 for (id = ids; id->id[0]; id++) {
272 /* compare multiple _CID entries against driver ids */
273 for (i = 0; i < cid_list->count; i++) {
274 if (!strcmp((char*)id->id,
275 cid_list->id[i].value))
276 return 0;
281 return -ENOENT;
283 EXPORT_SYMBOL(acpi_match_device_ids);
285 static void acpi_device_release(struct device *dev)
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
289 kfree(acpi_dev->pnp.cid_list);
290 kfree(acpi_dev);
293 static int acpi_device_suspend(struct device *dev, pm_message_t state)
295 struct acpi_device *acpi_dev = to_acpi_device(dev);
296 struct acpi_driver *acpi_drv = acpi_dev->driver;
298 if (acpi_drv && acpi_drv->ops.suspend)
299 return acpi_drv->ops.suspend(acpi_dev, state);
300 return 0;
303 static int acpi_device_resume(struct device *dev)
305 struct acpi_device *acpi_dev = to_acpi_device(dev);
306 struct acpi_driver *acpi_drv = acpi_dev->driver;
308 if (acpi_drv && acpi_drv->ops.resume)
309 return acpi_drv->ops.resume(acpi_dev);
310 return 0;
313 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
316 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
318 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
321 static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
322 char *buffer, int buffer_size)
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
326 strcpy(buffer, "MODALIAS=");
327 if (create_modalias(acpi_dev, buffer + 9, buffer_size - 9) > 0) {
328 envp[0] = buffer;
329 envp[1] = NULL;
331 return 0;
334 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
335 static int acpi_start_single_object(struct acpi_device *);
336 static int acpi_device_probe(struct device * dev)
338 struct acpi_device *acpi_dev = to_acpi_device(dev);
339 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
340 int ret;
342 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
343 if (!ret) {
344 if (acpi_dev->bus_ops.acpi_op_start)
345 acpi_start_single_object(acpi_dev);
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
347 "Found driver [%s] for device [%s]\n",
348 acpi_drv->name, acpi_dev->pnp.bus_id));
349 get_device(dev);
351 return ret;
354 static int acpi_device_remove(struct device * dev)
356 struct acpi_device *acpi_dev = to_acpi_device(dev);
357 struct acpi_driver *acpi_drv = acpi_dev->driver;
359 if (acpi_drv) {
360 if (acpi_drv->ops.stop)
361 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
362 if (acpi_drv->ops.remove)
363 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
365 acpi_dev->driver = NULL;
366 acpi_driver_data(dev) = NULL;
368 put_device(dev);
369 return 0;
372 static void acpi_device_shutdown(struct device *dev)
374 struct acpi_device *acpi_dev = to_acpi_device(dev);
375 struct acpi_driver *acpi_drv = acpi_dev->driver;
377 if (acpi_drv && acpi_drv->ops.shutdown)
378 acpi_drv->ops.shutdown(acpi_dev);
380 return ;
383 struct bus_type acpi_bus_type = {
384 .name = "acpi",
385 .suspend = acpi_device_suspend,
386 .resume = acpi_device_resume,
387 .shutdown = acpi_device_shutdown,
388 .match = acpi_bus_match,
389 .probe = acpi_device_probe,
390 .remove = acpi_device_remove,
391 .uevent = acpi_device_uevent,
394 static int acpi_device_register(struct acpi_device *device,
395 struct acpi_device *parent)
397 int result;
398 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
399 int found = 0;
401 * Linkage
402 * -------
403 * Link this device to its parent and siblings.
405 INIT_LIST_HEAD(&device->children);
406 INIT_LIST_HEAD(&device->node);
407 INIT_LIST_HEAD(&device->g_list);
408 INIT_LIST_HEAD(&device->wakeup_list);
410 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
411 if (!new_bus_id) {
412 printk(KERN_ERR PREFIX "Memory allocation error\n");
413 return -ENOMEM;
416 spin_lock(&acpi_device_lock);
418 * Find suitable bus_id and instance number in acpi_bus_id_list
419 * If failed, create one and link it into acpi_bus_id_list
421 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
422 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
423 acpi_device_bus_id->instance_no ++;
424 found = 1;
425 kfree(new_bus_id);
426 break;
429 if(!found) {
430 acpi_device_bus_id = new_bus_id;
431 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
432 acpi_device_bus_id->instance_no = 0;
433 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
435 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
437 if (device->parent) {
438 list_add_tail(&device->node, &device->parent->children);
439 list_add_tail(&device->g_list, &device->parent->g_list);
440 } else
441 list_add_tail(&device->g_list, &acpi_device_list);
442 if (device->wakeup.flags.valid)
443 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
444 spin_unlock(&acpi_device_lock);
446 if (device->parent)
447 device->dev.parent = &parent->dev;
448 device->dev.bus = &acpi_bus_type;
449 device_initialize(&device->dev);
450 device->dev.release = &acpi_device_release;
451 result = device_add(&device->dev);
452 if(result) {
453 printk("Error adding device %s", device->dev.bus_id);
454 goto end;
457 result = acpi_device_setup_files(device);
458 if(result)
459 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
461 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
462 return 0;
463 end:
464 spin_lock(&acpi_device_lock);
465 if (device->parent) {
466 list_del(&device->node);
467 list_del(&device->g_list);
468 } else
469 list_del(&device->g_list);
470 list_del(&device->wakeup_list);
471 spin_unlock(&acpi_device_lock);
472 return result;
475 static void acpi_device_unregister(struct acpi_device *device, int type)
477 spin_lock(&acpi_device_lock);
478 if (device->parent) {
479 list_del(&device->node);
480 list_del(&device->g_list);
481 } else
482 list_del(&device->g_list);
484 list_del(&device->wakeup_list);
485 spin_unlock(&acpi_device_lock);
487 acpi_detach_data(device->handle, acpi_bus_data_handler);
489 acpi_device_remove_files(device);
490 device_unregister(&device->dev);
493 /* --------------------------------------------------------------------------
494 Driver Management
495 -------------------------------------------------------------------------- */
497 * acpi_bus_driver_init - add a device to a driver
498 * @device: the device to add and initialize
499 * @driver: driver for the device
501 * Used to initialize a device via its device driver. Called whenever a
502 * driver is bound to a device. Invokes the driver's add() ops.
504 static int
505 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
507 int result = 0;
510 if (!device || !driver)
511 return -EINVAL;
513 if (!driver->ops.add)
514 return -ENOSYS;
516 result = driver->ops.add(device);
517 if (result) {
518 device->driver = NULL;
519 acpi_driver_data(device) = NULL;
520 return result;
523 device->driver = driver;
526 * TBD - Configuration Management: Assign resources to device based
527 * upon possible configuration and currently allocated resources.
530 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
531 "Driver successfully bound to device\n"));
532 return 0;
535 static int acpi_start_single_object(struct acpi_device *device)
537 int result = 0;
538 struct acpi_driver *driver;
541 if (!(driver = device->driver))
542 return 0;
544 if (driver->ops.start) {
545 result = driver->ops.start(device);
546 if (result && driver->ops.remove)
547 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
550 return result;
554 * acpi_bus_register_driver - register a driver with the ACPI bus
555 * @driver: driver being registered
557 * Registers a driver with the ACPI bus. Searches the namespace for all
558 * devices that match the driver's criteria and binds. Returns zero for
559 * success or a negative error status for failure.
561 int acpi_bus_register_driver(struct acpi_driver *driver)
563 int ret;
565 if (acpi_disabled)
566 return -ENODEV;
567 driver->drv.name = driver->name;
568 driver->drv.bus = &acpi_bus_type;
569 driver->drv.owner = driver->owner;
571 ret = driver_register(&driver->drv);
572 return ret;
575 EXPORT_SYMBOL(acpi_bus_register_driver);
578 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
579 * @driver: driver to unregister
581 * Unregisters a driver with the ACPI bus. Searches the namespace for all
582 * devices that match the driver's criteria and unbinds.
584 void acpi_bus_unregister_driver(struct acpi_driver *driver)
586 driver_unregister(&driver->drv);
589 EXPORT_SYMBOL(acpi_bus_unregister_driver);
591 /* --------------------------------------------------------------------------
592 Device Enumeration
593 -------------------------------------------------------------------------- */
594 acpi_status
595 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
597 acpi_status status;
598 acpi_handle tmp;
599 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
600 union acpi_object *obj;
602 status = acpi_get_handle(handle, "_EJD", &tmp);
603 if (ACPI_FAILURE(status))
604 return status;
606 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
607 if (ACPI_SUCCESS(status)) {
608 obj = buffer.pointer;
609 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
610 kfree(buffer.pointer);
612 return status;
614 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
616 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
619 /* TBD */
621 return;
624 static int acpi_bus_get_perf_flags(struct acpi_device *device)
626 device->performance.state = ACPI_STATE_UNKNOWN;
627 return 0;
630 static acpi_status
631 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
632 union acpi_object *package)
634 int i = 0;
635 union acpi_object *element = NULL;
637 if (!device || !package || (package->package.count < 2))
638 return AE_BAD_PARAMETER;
640 element = &(package->package.elements[0]);
641 if (!element)
642 return AE_BAD_PARAMETER;
643 if (element->type == ACPI_TYPE_PACKAGE) {
644 if ((element->package.count < 2) ||
645 (element->package.elements[0].type !=
646 ACPI_TYPE_LOCAL_REFERENCE)
647 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
648 return AE_BAD_DATA;
649 device->wakeup.gpe_device =
650 element->package.elements[0].reference.handle;
651 device->wakeup.gpe_number =
652 (u32) element->package.elements[1].integer.value;
653 } else if (element->type == ACPI_TYPE_INTEGER) {
654 device->wakeup.gpe_number = element->integer.value;
655 } else
656 return AE_BAD_DATA;
658 element = &(package->package.elements[1]);
659 if (element->type != ACPI_TYPE_INTEGER) {
660 return AE_BAD_DATA;
662 device->wakeup.sleep_state = element->integer.value;
664 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
665 return AE_NO_MEMORY;
667 device->wakeup.resources.count = package->package.count - 2;
668 for (i = 0; i < device->wakeup.resources.count; i++) {
669 element = &(package->package.elements[i + 2]);
670 if (element->type != ACPI_TYPE_ANY) {
671 return AE_BAD_DATA;
674 device->wakeup.resources.handles[i] = element->reference.handle;
677 return AE_OK;
680 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
682 acpi_status status = 0;
683 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
684 union acpi_object *package = NULL;
686 struct acpi_device_id button_device_ids[] = {
687 {"PNP0C0D", 0},
688 {"PNP0C0C", 0},
689 {"PNP0C0E", 0},
690 {"", 0},
694 /* _PRW */
695 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
696 if (ACPI_FAILURE(status)) {
697 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
698 goto end;
701 package = (union acpi_object *)buffer.pointer;
702 status = acpi_bus_extract_wakeup_device_power_package(device, package);
703 if (ACPI_FAILURE(status)) {
704 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
705 goto end;
708 kfree(buffer.pointer);
710 device->wakeup.flags.valid = 1;
711 /* Power button, Lid switch always enable wakeup */
712 if (!acpi_match_device_ids(device, button_device_ids))
713 device->wakeup.flags.run_wake = 1;
715 end:
716 if (ACPI_FAILURE(status))
717 device->flags.wake_capable = 0;
718 return 0;
721 static int acpi_bus_get_power_flags(struct acpi_device *device)
723 acpi_status status = 0;
724 acpi_handle handle = NULL;
725 u32 i = 0;
729 * Power Management Flags
731 status = acpi_get_handle(device->handle, "_PSC", &handle);
732 if (ACPI_SUCCESS(status))
733 device->power.flags.explicit_get = 1;
734 status = acpi_get_handle(device->handle, "_IRC", &handle);
735 if (ACPI_SUCCESS(status))
736 device->power.flags.inrush_current = 1;
739 * Enumerate supported power management states
741 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
742 struct acpi_device_power_state *ps = &device->power.states[i];
743 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
745 /* Evaluate "_PRx" to se if power resources are referenced */
746 acpi_evaluate_reference(device->handle, object_name, NULL,
747 &ps->resources);
748 if (ps->resources.count) {
749 device->power.flags.power_resources = 1;
750 ps->flags.valid = 1;
753 /* Evaluate "_PSx" to see if we can do explicit sets */
754 object_name[2] = 'S';
755 status = acpi_get_handle(device->handle, object_name, &handle);
756 if (ACPI_SUCCESS(status)) {
757 ps->flags.explicit_set = 1;
758 ps->flags.valid = 1;
761 /* State is valid if we have some power control */
762 if (ps->resources.count || ps->flags.explicit_set)
763 ps->flags.valid = 1;
765 ps->power = -1; /* Unknown - driver assigned */
766 ps->latency = -1; /* Unknown - driver assigned */
769 /* Set defaults for D0 and D3 states (always valid) */
770 device->power.states[ACPI_STATE_D0].flags.valid = 1;
771 device->power.states[ACPI_STATE_D0].power = 100;
772 device->power.states[ACPI_STATE_D3].flags.valid = 1;
773 device->power.states[ACPI_STATE_D3].power = 0;
775 /* TBD: System wake support and resource requirements. */
777 device->power.state = ACPI_STATE_UNKNOWN;
779 return 0;
782 static int acpi_bus_get_flags(struct acpi_device *device)
784 acpi_status status = AE_OK;
785 acpi_handle temp = NULL;
788 /* Presence of _STA indicates 'dynamic_status' */
789 status = acpi_get_handle(device->handle, "_STA", &temp);
790 if (ACPI_SUCCESS(status))
791 device->flags.dynamic_status = 1;
793 /* Presence of _CID indicates 'compatible_ids' */
794 status = acpi_get_handle(device->handle, "_CID", &temp);
795 if (ACPI_SUCCESS(status))
796 device->flags.compatible_ids = 1;
798 /* Presence of _RMV indicates 'removable' */
799 status = acpi_get_handle(device->handle, "_RMV", &temp);
800 if (ACPI_SUCCESS(status))
801 device->flags.removable = 1;
803 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
804 status = acpi_get_handle(device->handle, "_EJD", &temp);
805 if (ACPI_SUCCESS(status))
806 device->flags.ejectable = 1;
807 else {
808 status = acpi_get_handle(device->handle, "_EJ0", &temp);
809 if (ACPI_SUCCESS(status))
810 device->flags.ejectable = 1;
813 /* Presence of _LCK indicates 'lockable' */
814 status = acpi_get_handle(device->handle, "_LCK", &temp);
815 if (ACPI_SUCCESS(status))
816 device->flags.lockable = 1;
818 /* Presence of _PS0|_PR0 indicates 'power manageable' */
819 status = acpi_get_handle(device->handle, "_PS0", &temp);
820 if (ACPI_FAILURE(status))
821 status = acpi_get_handle(device->handle, "_PR0", &temp);
822 if (ACPI_SUCCESS(status))
823 device->flags.power_manageable = 1;
825 /* Presence of _PRW indicates wake capable */
826 status = acpi_get_handle(device->handle, "_PRW", &temp);
827 if (ACPI_SUCCESS(status))
828 device->flags.wake_capable = 1;
830 /* TBD: Peformance management */
832 return 0;
835 static void acpi_device_get_busid(struct acpi_device *device,
836 acpi_handle handle, int type)
838 char bus_id[5] = { '?', 0 };
839 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
840 int i = 0;
843 * Bus ID
844 * ------
845 * The device's Bus ID is simply the object name.
846 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
848 switch (type) {
849 case ACPI_BUS_TYPE_SYSTEM:
850 strcpy(device->pnp.bus_id, "ACPI");
851 break;
852 case ACPI_BUS_TYPE_POWER_BUTTON:
853 strcpy(device->pnp.bus_id, "PWRF");
854 break;
855 case ACPI_BUS_TYPE_SLEEP_BUTTON:
856 strcpy(device->pnp.bus_id, "SLPF");
857 break;
858 default:
859 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
860 /* Clean up trailing underscores (if any) */
861 for (i = 3; i > 1; i--) {
862 if (bus_id[i] == '_')
863 bus_id[i] = '\0';
864 else
865 break;
867 strcpy(device->pnp.bus_id, bus_id);
868 break;
872 static int
873 acpi_video_bus_match(struct acpi_device *device)
875 acpi_handle h_dummy1;
876 acpi_handle h_dummy2;
877 acpi_handle h_dummy3;
880 if (!device)
881 return -EINVAL;
883 /* Since there is no HID, CID for ACPI Video drivers, we have
884 * to check well known required nodes for each feature we support.
887 /* Does this device able to support video switching ? */
888 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
889 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
890 return 0;
892 /* Does this device able to retrieve a video ROM ? */
893 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
894 return 0;
896 /* Does this device able to configure which video head to be POSTed ? */
897 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
898 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
899 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
900 return 0;
902 return -ENODEV;
906 * acpi_bay_match - see if a device is an ejectable driver bay
908 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
909 * then we can safely call it an ejectable drive bay
911 static int acpi_bay_match(struct acpi_device *device){
912 acpi_status status;
913 acpi_handle handle;
914 acpi_handle tmp;
915 acpi_handle phandle;
917 handle = device->handle;
919 status = acpi_get_handle(handle, "_EJ0", &tmp);
920 if (ACPI_FAILURE(status))
921 return -ENODEV;
923 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
924 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
925 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
926 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
927 return 0;
929 if (acpi_get_parent(handle, &phandle))
930 return -ENODEV;
932 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
933 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
934 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
935 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
936 return 0;
938 return -ENODEV;
941 static void acpi_device_set_id(struct acpi_device *device,
942 struct acpi_device *parent, acpi_handle handle,
943 int type)
945 struct acpi_device_info *info;
946 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
947 char *hid = NULL;
948 char *uid = NULL;
949 struct acpi_compatible_id_list *cid_list = NULL;
950 acpi_status status;
952 switch (type) {
953 case ACPI_BUS_TYPE_DEVICE:
954 status = acpi_get_object_info(handle, &buffer);
955 if (ACPI_FAILURE(status)) {
956 printk("%s: Error reading device info\n", __FUNCTION__);
957 return;
960 info = buffer.pointer;
961 if (info->valid & ACPI_VALID_HID)
962 hid = info->hardware_id.value;
963 if (info->valid & ACPI_VALID_UID)
964 uid = info->unique_id.value;
965 if (info->valid & ACPI_VALID_CID)
966 cid_list = &info->compatibility_id;
967 if (info->valid & ACPI_VALID_ADR) {
968 device->pnp.bus_address = info->address;
969 device->flags.bus_address = 1;
972 if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){
973 status = acpi_video_bus_match(device);
974 if(ACPI_SUCCESS(status))
975 hid = ACPI_VIDEO_HID;
977 status = acpi_bay_match(device);
978 if (ACPI_SUCCESS(status))
979 hid = ACPI_BAY_HID;
981 break;
982 case ACPI_BUS_TYPE_POWER:
983 hid = ACPI_POWER_HID;
984 break;
985 case ACPI_BUS_TYPE_PROCESSOR:
986 hid = ACPI_PROCESSOR_HID;
987 break;
988 case ACPI_BUS_TYPE_SYSTEM:
989 hid = ACPI_SYSTEM_HID;
990 break;
991 case ACPI_BUS_TYPE_THERMAL:
992 hid = ACPI_THERMAL_HID;
993 break;
994 case ACPI_BUS_TYPE_POWER_BUTTON:
995 hid = ACPI_BUTTON_HID_POWERF;
996 break;
997 case ACPI_BUS_TYPE_SLEEP_BUTTON:
998 hid = ACPI_BUTTON_HID_SLEEPF;
999 break;
1003 * \_SB
1004 * ----
1005 * Fix for the system root bus device -- the only root-level device.
1007 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1008 hid = ACPI_BUS_HID;
1009 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1010 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1013 if (hid) {
1014 strcpy(device->pnp.hardware_id, hid);
1015 device->flags.hardware_id = 1;
1017 if (uid) {
1018 strcpy(device->pnp.unique_id, uid);
1019 device->flags.unique_id = 1;
1021 if (cid_list) {
1022 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
1023 if (device->pnp.cid_list)
1024 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
1025 else
1026 printk(KERN_ERR "Memory allocation error\n");
1029 kfree(buffer.pointer);
1032 static int acpi_device_set_context(struct acpi_device *device, int type)
1034 acpi_status status = AE_OK;
1035 int result = 0;
1037 * Context
1038 * -------
1039 * Attach this 'struct acpi_device' to the ACPI object. This makes
1040 * resolutions from handle->device very efficient. Note that we need
1041 * to be careful with fixed-feature devices as they all attach to the
1042 * root object.
1044 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1045 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1046 status = acpi_attach_data(device->handle,
1047 acpi_bus_data_handler, device);
1049 if (ACPI_FAILURE(status)) {
1050 printk("Error attaching device data\n");
1051 result = -ENODEV;
1054 return result;
1057 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1059 if (!dev)
1060 return -EINVAL;
1062 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1063 device_release_driver(&dev->dev);
1065 if (!rmdevice)
1066 return 0;
1069 * unbind _ADR-Based Devices when hot removal
1071 if (dev->flags.bus_address) {
1072 if ((dev->parent) && (dev->parent->ops.unbind))
1073 dev->parent->ops.unbind(dev);
1075 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1077 return 0;
1080 static int
1081 acpi_add_single_object(struct acpi_device **child,
1082 struct acpi_device *parent, acpi_handle handle, int type,
1083 struct acpi_bus_ops *ops)
1085 int result = 0;
1086 struct acpi_device *device = NULL;
1089 if (!child)
1090 return -EINVAL;
1092 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1093 if (!device) {
1094 printk(KERN_ERR PREFIX "Memory allocation error\n");
1095 return -ENOMEM;
1098 device->handle = handle;
1099 device->parent = parent;
1100 device->bus_ops = *ops; /* workround for not call .start */
1103 acpi_device_get_busid(device, handle, type);
1106 * Flags
1107 * -----
1108 * Get prior to calling acpi_bus_get_status() so we know whether
1109 * or not _STA is present. Note that we only look for object
1110 * handles -- cannot evaluate objects until we know the device is
1111 * present and properly initialized.
1113 result = acpi_bus_get_flags(device);
1114 if (result)
1115 goto end;
1118 * Status
1119 * ------
1120 * See if the device is present. We always assume that non-Device
1121 * and non-Processor objects (e.g. thermal zones, power resources,
1122 * etc.) are present, functioning, etc. (at least when parent object
1123 * is present). Note that _STA has a different meaning for some
1124 * objects (e.g. power resources) so we need to be careful how we use
1125 * it.
1127 switch (type) {
1128 case ACPI_BUS_TYPE_PROCESSOR:
1129 case ACPI_BUS_TYPE_DEVICE:
1130 result = acpi_bus_get_status(device);
1131 if (ACPI_FAILURE(result) || !device->status.present) {
1132 result = -ENOENT;
1133 goto end;
1135 break;
1136 default:
1137 STRUCT_TO_INT(device->status) =
1138 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1139 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1140 break;
1144 * Initialize Device
1145 * -----------------
1146 * TBD: Synch with Core's enumeration/initialization process.
1150 * Hardware ID, Unique ID, & Bus Address
1151 * -------------------------------------
1153 acpi_device_set_id(device, parent, handle, type);
1156 * Power Management
1157 * ----------------
1159 if (device->flags.power_manageable) {
1160 result = acpi_bus_get_power_flags(device);
1161 if (result)
1162 goto end;
1166 * Wakeup device management
1167 *-----------------------
1169 if (device->flags.wake_capable) {
1170 result = acpi_bus_get_wakeup_device_flags(device);
1171 if (result)
1172 goto end;
1176 * Performance Management
1177 * ----------------------
1179 if (device->flags.performance_manageable) {
1180 result = acpi_bus_get_perf_flags(device);
1181 if (result)
1182 goto end;
1185 if ((result = acpi_device_set_context(device, type)))
1186 goto end;
1188 result = acpi_device_register(device, parent);
1191 * Bind _ADR-Based Devices when hot add
1193 if (device->flags.bus_address) {
1194 if (device->parent && device->parent->ops.bind)
1195 device->parent->ops.bind(device);
1198 end:
1199 if (!result)
1200 *child = device;
1201 else {
1202 kfree(device->pnp.cid_list);
1203 kfree(device);
1206 return result;
1209 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1211 acpi_status status = AE_OK;
1212 struct acpi_device *parent = NULL;
1213 struct acpi_device *child = NULL;
1214 acpi_handle phandle = NULL;
1215 acpi_handle chandle = NULL;
1216 acpi_object_type type = 0;
1217 u32 level = 1;
1220 if (!start)
1221 return -EINVAL;
1223 parent = start;
1224 phandle = start->handle;
1227 * Parse through the ACPI namespace, identify all 'devices', and
1228 * create a new 'struct acpi_device' for each.
1230 while ((level > 0) && parent) {
1232 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1233 chandle, &chandle);
1236 * If this scope is exhausted then move our way back up.
1238 if (ACPI_FAILURE(status)) {
1239 level--;
1240 chandle = phandle;
1241 acpi_get_parent(phandle, &phandle);
1242 if (parent->parent)
1243 parent = parent->parent;
1244 continue;
1247 status = acpi_get_type(chandle, &type);
1248 if (ACPI_FAILURE(status))
1249 continue;
1252 * If this is a scope object then parse it (depth-first).
1254 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1255 level++;
1256 phandle = chandle;
1257 chandle = NULL;
1258 continue;
1262 * We're only interested in objects that we consider 'devices'.
1264 switch (type) {
1265 case ACPI_TYPE_DEVICE:
1266 type = ACPI_BUS_TYPE_DEVICE;
1267 break;
1268 case ACPI_TYPE_PROCESSOR:
1269 type = ACPI_BUS_TYPE_PROCESSOR;
1270 break;
1271 case ACPI_TYPE_THERMAL:
1272 type = ACPI_BUS_TYPE_THERMAL;
1273 break;
1274 case ACPI_TYPE_POWER:
1275 type = ACPI_BUS_TYPE_POWER;
1276 break;
1277 default:
1278 continue;
1281 if (ops->acpi_op_add)
1282 status = acpi_add_single_object(&child, parent,
1283 chandle, type, ops);
1284 else
1285 status = acpi_bus_get_device(chandle, &child);
1287 if (ACPI_FAILURE(status))
1288 continue;
1290 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1291 status = acpi_start_single_object(child);
1292 if (ACPI_FAILURE(status))
1293 continue;
1297 * If the device is present, enabled, and functioning then
1298 * parse its scope (depth-first). Note that we need to
1299 * represent absent devices to facilitate PnP notifications
1300 * -- but only the subtree head (not all of its children,
1301 * which will be enumerated when the parent is inserted).
1303 * TBD: Need notifications and other detection mechanisms
1304 * in place before we can fully implement this.
1306 if (child->status.present) {
1307 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1308 NULL, NULL);
1309 if (ACPI_SUCCESS(status)) {
1310 level++;
1311 phandle = chandle;
1312 chandle = NULL;
1313 parent = child;
1318 return 0;
1322 acpi_bus_add(struct acpi_device **child,
1323 struct acpi_device *parent, acpi_handle handle, int type)
1325 int result;
1326 struct acpi_bus_ops ops;
1328 memset(&ops, 0, sizeof(ops));
1329 ops.acpi_op_add = 1;
1331 result = acpi_add_single_object(child, parent, handle, type, &ops);
1332 if (!result)
1333 result = acpi_bus_scan(*child, &ops);
1335 return result;
1338 EXPORT_SYMBOL(acpi_bus_add);
1340 int acpi_bus_start(struct acpi_device *device)
1342 int result;
1343 struct acpi_bus_ops ops;
1346 if (!device)
1347 return -EINVAL;
1349 result = acpi_start_single_object(device);
1350 if (!result) {
1351 memset(&ops, 0, sizeof(ops));
1352 ops.acpi_op_start = 1;
1353 result = acpi_bus_scan(device, &ops);
1355 return result;
1358 EXPORT_SYMBOL(acpi_bus_start);
1360 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1362 acpi_status status;
1363 struct acpi_device *parent, *child;
1364 acpi_handle phandle, chandle;
1365 acpi_object_type type;
1366 u32 level = 1;
1367 int err = 0;
1369 parent = start;
1370 phandle = start->handle;
1371 child = chandle = NULL;
1373 while ((level > 0) && parent && (!err)) {
1374 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1375 chandle, &chandle);
1378 * If this scope is exhausted then move our way back up.
1380 if (ACPI_FAILURE(status)) {
1381 level--;
1382 chandle = phandle;
1383 acpi_get_parent(phandle, &phandle);
1384 child = parent;
1385 parent = parent->parent;
1387 if (level == 0)
1388 err = acpi_bus_remove(child, rmdevice);
1389 else
1390 err = acpi_bus_remove(child, 1);
1392 continue;
1395 status = acpi_get_type(chandle, &type);
1396 if (ACPI_FAILURE(status)) {
1397 continue;
1400 * If there is a device corresponding to chandle then
1401 * parse it (depth-first).
1403 if (acpi_bus_get_device(chandle, &child) == 0) {
1404 level++;
1405 phandle = chandle;
1406 chandle = NULL;
1407 parent = child;
1409 continue;
1411 return err;
1413 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1416 static int acpi_bus_scan_fixed(struct acpi_device *root)
1418 int result = 0;
1419 struct acpi_device *device = NULL;
1420 struct acpi_bus_ops ops;
1422 if (!root)
1423 return -ENODEV;
1425 memset(&ops, 0, sizeof(ops));
1426 ops.acpi_op_add = 1;
1427 ops.acpi_op_start = 1;
1430 * Enumerate all fixed-feature devices.
1432 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1433 result = acpi_add_single_object(&device, acpi_root,
1434 NULL,
1435 ACPI_BUS_TYPE_POWER_BUTTON,
1436 &ops);
1439 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1440 result = acpi_add_single_object(&device, acpi_root,
1441 NULL,
1442 ACPI_BUS_TYPE_SLEEP_BUTTON,
1443 &ops);
1446 return result;
1449 static int __init acpi_scan_init(void)
1451 int result;
1452 struct acpi_bus_ops ops;
1455 if (acpi_disabled)
1456 return 0;
1458 memset(&ops, 0, sizeof(ops));
1459 ops.acpi_op_add = 1;
1460 ops.acpi_op_start = 1;
1462 result = bus_register(&acpi_bus_type);
1463 if (result) {
1464 /* We don't want to quit even if we failed to add suspend/resume */
1465 printk(KERN_ERR PREFIX "Could not register bus type\n");
1469 * Create the root device in the bus's device tree
1471 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1472 ACPI_BUS_TYPE_SYSTEM, &ops);
1473 if (result)
1474 goto Done;
1477 * Enumerate devices in the ACPI namespace.
1479 result = acpi_bus_scan_fixed(acpi_root);
1480 if (!result)
1481 result = acpi_bus_scan(acpi_root, &ops);
1483 if (result)
1484 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1486 Done:
1487 return result;
1490 subsys_initcall(acpi_scan_init);