Merge branches 'release' and 'autoload' into release
[linux-2.6/libata-dev.git] / drivers / acpi / scan.c
blobc7b0aa52dd230205f25ae2e3c74f469d6ef7c8d1
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;
43 if (!acpi_dev->flags.hardware_id)
44 return -ENODEV;
46 len = snprintf(modalias, size, "acpi:%s:",
47 acpi_dev->pnp.hardware_id);
48 if (len < 0 || len >= size)
49 return -EINVAL;
50 size -= len;
52 if (acpi_dev->flags.compatible_ids) {
53 struct acpi_compatible_id_list *cid_list;
54 int i;
55 int count;
57 cid_list = acpi_dev->pnp.cid_list;
58 for (i = 0; i < cid_list->count; i++) {
59 count = snprintf(&modalias[len], size, "%s:",
60 cid_list->id[i].value);
61 if (count < 0 || count >= size) {
62 printk(KERN_ERR "acpi: %s cid[%i] exceeds event buffer size",
63 acpi_dev->pnp.device_name, i);
64 break;
66 len += count;
67 size -= count;
71 modalias[len] = '\0';
72 return len;
75 static ssize_t
76 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
77 struct acpi_device *acpi_dev = to_acpi_device(dev);
78 int len;
80 /* Device has no HID and no CID or string is >1024 */
81 len = create_modalias(acpi_dev, buf, 1024);
82 if (len <= 0)
83 return 0;
84 buf[len++] = '\n';
85 return len;
87 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
89 static int acpi_eject_operation(acpi_handle handle, int lockable)
91 struct acpi_object_list arg_list;
92 union acpi_object arg;
93 acpi_status status = AE_OK;
96 * TBD: evaluate _PS3?
99 if (lockable) {
100 arg_list.count = 1;
101 arg_list.pointer = &arg;
102 arg.type = ACPI_TYPE_INTEGER;
103 arg.integer.value = 0;
104 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
107 arg_list.count = 1;
108 arg_list.pointer = &arg;
109 arg.type = ACPI_TYPE_INTEGER;
110 arg.integer.value = 1;
113 * TBD: _EJD support.
116 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
117 if (ACPI_FAILURE(status)) {
118 return (-ENODEV);
121 return (0);
124 static ssize_t
125 acpi_eject_store(struct device *d, struct device_attribute *attr,
126 const char *buf, size_t count)
128 int result;
129 int ret = count;
130 int islockable;
131 acpi_status status;
132 acpi_handle handle;
133 acpi_object_type type = 0;
134 struct acpi_device *acpi_device = to_acpi_device(d);
136 if ((!count) || (buf[0] != '1')) {
137 return -EINVAL;
139 #ifndef FORCE_EJECT
140 if (acpi_device->driver == NULL) {
141 ret = -ENODEV;
142 goto err;
144 #endif
145 status = acpi_get_type(acpi_device->handle, &type);
146 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
147 ret = -ENODEV;
148 goto err;
151 islockable = acpi_device->flags.lockable;
152 handle = acpi_device->handle;
154 result = acpi_bus_trim(acpi_device, 1);
156 if (!result)
157 result = acpi_eject_operation(handle, islockable);
159 if (result) {
160 ret = -EBUSY;
162 err:
163 return ret;
166 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
168 static ssize_t
169 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
170 struct acpi_device *acpi_dev = to_acpi_device(dev);
172 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
174 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
176 static ssize_t
177 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
178 struct acpi_device *acpi_dev = to_acpi_device(dev);
179 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
180 int result;
182 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
183 if(result)
184 goto end;
186 result = sprintf(buf, "%s\n", (char*)path.pointer);
187 kfree(path.pointer);
188 end:
189 return result;
191 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
193 static int acpi_device_setup_files(struct acpi_device *dev)
195 acpi_status status;
196 acpi_handle temp;
197 int result = 0;
200 * Devices gotten from FADT don't have a "path" attribute
202 if(dev->handle) {
203 result = device_create_file(&dev->dev, &dev_attr_path);
204 if(result)
205 goto end;
208 if(dev->flags.hardware_id) {
209 result = device_create_file(&dev->dev, &dev_attr_hid);
210 if(result)
211 goto end;
214 if (dev->flags.hardware_id || dev->flags.compatible_ids){
215 result = device_create_file(&dev->dev, &dev_attr_modalias);
216 if(result)
217 goto end;
221 * If device has _EJ0, 'eject' file is created that is used to trigger
222 * hot-removal function from userland.
224 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
225 if (ACPI_SUCCESS(status))
226 result = device_create_file(&dev->dev, &dev_attr_eject);
227 end:
228 return result;
231 static void acpi_device_remove_files(struct acpi_device *dev)
233 acpi_status status;
234 acpi_handle temp;
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
242 device_remove_file(&dev->dev, &dev_attr_eject);
244 if (dev->flags.hardware_id || dev->flags.compatible_ids)
245 device_remove_file(&dev->dev, &dev_attr_modalias);
247 if(dev->flags.hardware_id)
248 device_remove_file(&dev->dev, &dev_attr_hid);
249 if(dev->handle)
250 device_remove_file(&dev->dev, &dev_attr_path);
252 /* --------------------------------------------------------------------------
253 ACPI Bus operations
254 -------------------------------------------------------------------------- */
256 int acpi_match_device_ids(struct acpi_device *device,
257 const struct acpi_device_id *ids)
259 const struct acpi_device_id *id;
261 if (device->flags.hardware_id) {
262 for (id = ids; id->id[0]; id++) {
263 if (!strcmp((char*)id->id, device->pnp.hardware_id))
264 return 0;
268 if (device->flags.compatible_ids) {
269 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
270 int i;
272 for (id = ids; id->id[0]; id++) {
273 /* compare multiple _CID entries against driver ids */
274 for (i = 0; i < cid_list->count; i++) {
275 if (!strcmp((char*)id->id,
276 cid_list->id[i].value))
277 return 0;
282 return -ENOENT;
284 EXPORT_SYMBOL(acpi_match_device_ids);
286 static void acpi_device_release(struct device *dev)
288 struct acpi_device *acpi_dev = to_acpi_device(dev);
290 kfree(acpi_dev->pnp.cid_list);
291 kfree(acpi_dev);
294 static int acpi_device_suspend(struct device *dev, pm_message_t state)
296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
299 if (acpi_drv && acpi_drv->ops.suspend)
300 return acpi_drv->ops.suspend(acpi_dev, state);
301 return 0;
304 static int acpi_device_resume(struct device *dev)
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = acpi_dev->driver;
309 if (acpi_drv && acpi_drv->ops.resume)
310 return acpi_drv->ops.resume(acpi_dev);
311 return 0;
314 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
319 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
322 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
325 int len;
327 if (add_uevent_var(env, "MODALIAS="))
328 return -ENOMEM;
329 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
330 sizeof(env->buf) - env->buflen);
331 if (len >= (sizeof(env->buf) - env->buflen))
332 return -ENOMEM;
333 env->buflen += len;
334 return 0;
337 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
338 static int acpi_start_single_object(struct acpi_device *);
339 static int acpi_device_probe(struct device * dev)
341 struct acpi_device *acpi_dev = to_acpi_device(dev);
342 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
343 int ret;
345 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
346 if (!ret) {
347 if (acpi_dev->bus_ops.acpi_op_start)
348 acpi_start_single_object(acpi_dev);
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
350 "Found driver [%s] for device [%s]\n",
351 acpi_drv->name, acpi_dev->pnp.bus_id));
352 get_device(dev);
354 return ret;
357 static int acpi_device_remove(struct device * dev)
359 struct acpi_device *acpi_dev = to_acpi_device(dev);
360 struct acpi_driver *acpi_drv = acpi_dev->driver;
362 if (acpi_drv) {
363 if (acpi_drv->ops.stop)
364 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
365 if (acpi_drv->ops.remove)
366 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
368 acpi_dev->driver = NULL;
369 acpi_driver_data(dev) = NULL;
371 put_device(dev);
372 return 0;
375 static void acpi_device_shutdown(struct device *dev)
377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378 struct acpi_driver *acpi_drv = acpi_dev->driver;
380 if (acpi_drv && acpi_drv->ops.shutdown)
381 acpi_drv->ops.shutdown(acpi_dev);
383 return ;
386 struct bus_type acpi_bus_type = {
387 .name = "acpi",
388 .suspend = acpi_device_suspend,
389 .resume = acpi_device_resume,
390 .shutdown = acpi_device_shutdown,
391 .match = acpi_bus_match,
392 .probe = acpi_device_probe,
393 .remove = acpi_device_remove,
394 .uevent = acpi_device_uevent,
397 static int acpi_device_register(struct acpi_device *device,
398 struct acpi_device *parent)
400 int result;
401 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
402 int found = 0;
404 * Linkage
405 * -------
406 * Link this device to its parent and siblings.
408 INIT_LIST_HEAD(&device->children);
409 INIT_LIST_HEAD(&device->node);
410 INIT_LIST_HEAD(&device->g_list);
411 INIT_LIST_HEAD(&device->wakeup_list);
413 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
414 if (!new_bus_id) {
415 printk(KERN_ERR PREFIX "Memory allocation error\n");
416 return -ENOMEM;
419 spin_lock(&acpi_device_lock);
421 * Find suitable bus_id and instance number in acpi_bus_id_list
422 * If failed, create one and link it into acpi_bus_id_list
424 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
425 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
426 acpi_device_bus_id->instance_no ++;
427 found = 1;
428 kfree(new_bus_id);
429 break;
432 if(!found) {
433 acpi_device_bus_id = new_bus_id;
434 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
435 acpi_device_bus_id->instance_no = 0;
436 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
438 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
440 if (device->parent) {
441 list_add_tail(&device->node, &device->parent->children);
442 list_add_tail(&device->g_list, &device->parent->g_list);
443 } else
444 list_add_tail(&device->g_list, &acpi_device_list);
445 if (device->wakeup.flags.valid)
446 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
447 spin_unlock(&acpi_device_lock);
449 if (device->parent)
450 device->dev.parent = &parent->dev;
451 device->dev.bus = &acpi_bus_type;
452 device_initialize(&device->dev);
453 device->dev.release = &acpi_device_release;
454 result = device_add(&device->dev);
455 if(result) {
456 printk("Error adding device %s", device->dev.bus_id);
457 goto end;
460 result = acpi_device_setup_files(device);
461 if(result)
462 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
464 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
465 return 0;
466 end:
467 spin_lock(&acpi_device_lock);
468 if (device->parent) {
469 list_del(&device->node);
470 list_del(&device->g_list);
471 } else
472 list_del(&device->g_list);
473 list_del(&device->wakeup_list);
474 spin_unlock(&acpi_device_lock);
475 return result;
478 static void acpi_device_unregister(struct acpi_device *device, int type)
480 spin_lock(&acpi_device_lock);
481 if (device->parent) {
482 list_del(&device->node);
483 list_del(&device->g_list);
484 } else
485 list_del(&device->g_list);
487 list_del(&device->wakeup_list);
488 spin_unlock(&acpi_device_lock);
490 acpi_detach_data(device->handle, acpi_bus_data_handler);
492 acpi_device_remove_files(device);
493 device_unregister(&device->dev);
496 /* --------------------------------------------------------------------------
497 Driver Management
498 -------------------------------------------------------------------------- */
500 * acpi_bus_driver_init - add a device to a driver
501 * @device: the device to add and initialize
502 * @driver: driver for the device
504 * Used to initialize a device via its device driver. Called whenever a
505 * driver is bound to a device. Invokes the driver's add() ops.
507 static int
508 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
510 int result = 0;
513 if (!device || !driver)
514 return -EINVAL;
516 if (!driver->ops.add)
517 return -ENOSYS;
519 result = driver->ops.add(device);
520 if (result) {
521 device->driver = NULL;
522 acpi_driver_data(device) = NULL;
523 return result;
526 device->driver = driver;
529 * TBD - Configuration Management: Assign resources to device based
530 * upon possible configuration and currently allocated resources.
533 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
534 "Driver successfully bound to device\n"));
535 return 0;
538 static int acpi_start_single_object(struct acpi_device *device)
540 int result = 0;
541 struct acpi_driver *driver;
544 if (!(driver = device->driver))
545 return 0;
547 if (driver->ops.start) {
548 result = driver->ops.start(device);
549 if (result && driver->ops.remove)
550 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
553 return result;
557 * acpi_bus_register_driver - register a driver with the ACPI bus
558 * @driver: driver being registered
560 * Registers a driver with the ACPI bus. Searches the namespace for all
561 * devices that match the driver's criteria and binds. Returns zero for
562 * success or a negative error status for failure.
564 int acpi_bus_register_driver(struct acpi_driver *driver)
566 int ret;
568 if (acpi_disabled)
569 return -ENODEV;
570 driver->drv.name = driver->name;
571 driver->drv.bus = &acpi_bus_type;
572 driver->drv.owner = driver->owner;
574 ret = driver_register(&driver->drv);
575 return ret;
578 EXPORT_SYMBOL(acpi_bus_register_driver);
581 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
582 * @driver: driver to unregister
584 * Unregisters a driver with the ACPI bus. Searches the namespace for all
585 * devices that match the driver's criteria and unbinds.
587 void acpi_bus_unregister_driver(struct acpi_driver *driver)
589 driver_unregister(&driver->drv);
592 EXPORT_SYMBOL(acpi_bus_unregister_driver);
594 /* --------------------------------------------------------------------------
595 Device Enumeration
596 -------------------------------------------------------------------------- */
597 acpi_status
598 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
600 acpi_status status;
601 acpi_handle tmp;
602 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
603 union acpi_object *obj;
605 status = acpi_get_handle(handle, "_EJD", &tmp);
606 if (ACPI_FAILURE(status))
607 return status;
609 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
610 if (ACPI_SUCCESS(status)) {
611 obj = buffer.pointer;
612 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
613 kfree(buffer.pointer);
615 return status;
617 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
619 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
622 /* TBD */
624 return;
627 static int acpi_bus_get_perf_flags(struct acpi_device *device)
629 device->performance.state = ACPI_STATE_UNKNOWN;
630 return 0;
633 static acpi_status
634 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
635 union acpi_object *package)
637 int i = 0;
638 union acpi_object *element = NULL;
640 if (!device || !package || (package->package.count < 2))
641 return AE_BAD_PARAMETER;
643 element = &(package->package.elements[0]);
644 if (!element)
645 return AE_BAD_PARAMETER;
646 if (element->type == ACPI_TYPE_PACKAGE) {
647 if ((element->package.count < 2) ||
648 (element->package.elements[0].type !=
649 ACPI_TYPE_LOCAL_REFERENCE)
650 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
651 return AE_BAD_DATA;
652 device->wakeup.gpe_device =
653 element->package.elements[0].reference.handle;
654 device->wakeup.gpe_number =
655 (u32) element->package.elements[1].integer.value;
656 } else if (element->type == ACPI_TYPE_INTEGER) {
657 device->wakeup.gpe_number = element->integer.value;
658 } else
659 return AE_BAD_DATA;
661 element = &(package->package.elements[1]);
662 if (element->type != ACPI_TYPE_INTEGER) {
663 return AE_BAD_DATA;
665 device->wakeup.sleep_state = element->integer.value;
667 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
668 return AE_NO_MEMORY;
670 device->wakeup.resources.count = package->package.count - 2;
671 for (i = 0; i < device->wakeup.resources.count; i++) {
672 element = &(package->package.elements[i + 2]);
673 if (element->type != ACPI_TYPE_ANY) {
674 return AE_BAD_DATA;
677 device->wakeup.resources.handles[i] = element->reference.handle;
680 return AE_OK;
683 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
685 acpi_status status = 0;
686 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
687 union acpi_object *package = NULL;
689 struct acpi_device_id button_device_ids[] = {
690 {"PNP0C0D", 0},
691 {"PNP0C0C", 0},
692 {"PNP0C0E", 0},
693 {"", 0},
697 /* _PRW */
698 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
699 if (ACPI_FAILURE(status)) {
700 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
701 goto end;
704 package = (union acpi_object *)buffer.pointer;
705 status = acpi_bus_extract_wakeup_device_power_package(device, package);
706 if (ACPI_FAILURE(status)) {
707 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
708 goto end;
711 kfree(buffer.pointer);
713 device->wakeup.flags.valid = 1;
714 /* Power button, Lid switch always enable wakeup */
715 if (!acpi_match_device_ids(device, button_device_ids))
716 device->wakeup.flags.run_wake = 1;
718 end:
719 if (ACPI_FAILURE(status))
720 device->flags.wake_capable = 0;
721 return 0;
724 static int acpi_bus_get_power_flags(struct acpi_device *device)
726 acpi_status status = 0;
727 acpi_handle handle = NULL;
728 u32 i = 0;
732 * Power Management Flags
734 status = acpi_get_handle(device->handle, "_PSC", &handle);
735 if (ACPI_SUCCESS(status))
736 device->power.flags.explicit_get = 1;
737 status = acpi_get_handle(device->handle, "_IRC", &handle);
738 if (ACPI_SUCCESS(status))
739 device->power.flags.inrush_current = 1;
742 * Enumerate supported power management states
744 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
745 struct acpi_device_power_state *ps = &device->power.states[i];
746 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
748 /* Evaluate "_PRx" to se if power resources are referenced */
749 acpi_evaluate_reference(device->handle, object_name, NULL,
750 &ps->resources);
751 if (ps->resources.count) {
752 device->power.flags.power_resources = 1;
753 ps->flags.valid = 1;
756 /* Evaluate "_PSx" to see if we can do explicit sets */
757 object_name[2] = 'S';
758 status = acpi_get_handle(device->handle, object_name, &handle);
759 if (ACPI_SUCCESS(status)) {
760 ps->flags.explicit_set = 1;
761 ps->flags.valid = 1;
764 /* State is valid if we have some power control */
765 if (ps->resources.count || ps->flags.explicit_set)
766 ps->flags.valid = 1;
768 ps->power = -1; /* Unknown - driver assigned */
769 ps->latency = -1; /* Unknown - driver assigned */
772 /* Set defaults for D0 and D3 states (always valid) */
773 device->power.states[ACPI_STATE_D0].flags.valid = 1;
774 device->power.states[ACPI_STATE_D0].power = 100;
775 device->power.states[ACPI_STATE_D3].flags.valid = 1;
776 device->power.states[ACPI_STATE_D3].power = 0;
778 /* TBD: System wake support and resource requirements. */
780 device->power.state = ACPI_STATE_UNKNOWN;
782 return 0;
785 static int acpi_bus_get_flags(struct acpi_device *device)
787 acpi_status status = AE_OK;
788 acpi_handle temp = NULL;
791 /* Presence of _STA indicates 'dynamic_status' */
792 status = acpi_get_handle(device->handle, "_STA", &temp);
793 if (ACPI_SUCCESS(status))
794 device->flags.dynamic_status = 1;
796 /* Presence of _CID indicates 'compatible_ids' */
797 status = acpi_get_handle(device->handle, "_CID", &temp);
798 if (ACPI_SUCCESS(status))
799 device->flags.compatible_ids = 1;
801 /* Presence of _RMV indicates 'removable' */
802 status = acpi_get_handle(device->handle, "_RMV", &temp);
803 if (ACPI_SUCCESS(status))
804 device->flags.removable = 1;
806 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
807 status = acpi_get_handle(device->handle, "_EJD", &temp);
808 if (ACPI_SUCCESS(status))
809 device->flags.ejectable = 1;
810 else {
811 status = acpi_get_handle(device->handle, "_EJ0", &temp);
812 if (ACPI_SUCCESS(status))
813 device->flags.ejectable = 1;
816 /* Presence of _LCK indicates 'lockable' */
817 status = acpi_get_handle(device->handle, "_LCK", &temp);
818 if (ACPI_SUCCESS(status))
819 device->flags.lockable = 1;
821 /* Presence of _PS0|_PR0 indicates 'power manageable' */
822 status = acpi_get_handle(device->handle, "_PS0", &temp);
823 if (ACPI_FAILURE(status))
824 status = acpi_get_handle(device->handle, "_PR0", &temp);
825 if (ACPI_SUCCESS(status))
826 device->flags.power_manageable = 1;
828 /* Presence of _PRW indicates wake capable */
829 status = acpi_get_handle(device->handle, "_PRW", &temp);
830 if (ACPI_SUCCESS(status))
831 device->flags.wake_capable = 1;
833 /* TBD: Performance management */
835 return 0;
838 static void acpi_device_get_busid(struct acpi_device *device,
839 acpi_handle handle, int type)
841 char bus_id[5] = { '?', 0 };
842 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
843 int i = 0;
846 * Bus ID
847 * ------
848 * The device's Bus ID is simply the object name.
849 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
851 switch (type) {
852 case ACPI_BUS_TYPE_SYSTEM:
853 strcpy(device->pnp.bus_id, "ACPI");
854 break;
855 case ACPI_BUS_TYPE_POWER_BUTTON:
856 strcpy(device->pnp.bus_id, "PWRF");
857 break;
858 case ACPI_BUS_TYPE_SLEEP_BUTTON:
859 strcpy(device->pnp.bus_id, "SLPF");
860 break;
861 default:
862 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
863 /* Clean up trailing underscores (if any) */
864 for (i = 3; i > 1; i--) {
865 if (bus_id[i] == '_')
866 bus_id[i] = '\0';
867 else
868 break;
870 strcpy(device->pnp.bus_id, bus_id);
871 break;
875 static int
876 acpi_video_bus_match(struct acpi_device *device)
878 acpi_handle h_dummy1;
879 acpi_handle h_dummy2;
880 acpi_handle h_dummy3;
883 if (!device)
884 return -EINVAL;
886 /* Since there is no HID, CID for ACPI Video drivers, we have
887 * to check well known required nodes for each feature we support.
890 /* Does this device able to support video switching ? */
891 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
892 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
893 return 0;
895 /* Does this device able to retrieve a video ROM ? */
896 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
897 return 0;
899 /* Does this device able to configure which video head to be POSTed ? */
900 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
901 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
902 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
903 return 0;
905 return -ENODEV;
909 * acpi_bay_match - see if a device is an ejectable driver bay
911 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
912 * then we can safely call it an ejectable drive bay
914 static int acpi_bay_match(struct acpi_device *device){
915 acpi_status status;
916 acpi_handle handle;
917 acpi_handle tmp;
918 acpi_handle phandle;
920 handle = device->handle;
922 status = acpi_get_handle(handle, "_EJ0", &tmp);
923 if (ACPI_FAILURE(status))
924 return -ENODEV;
926 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
927 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
928 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
929 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
930 return 0;
932 if (acpi_get_parent(handle, &phandle))
933 return -ENODEV;
935 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
936 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
937 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
938 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
939 return 0;
941 return -ENODEV;
945 * acpi_dock_match - see if a device has a _DCK method
947 static int acpi_dock_match(struct acpi_device *device)
949 acpi_handle tmp;
950 return acpi_get_handle(device->handle, "_DCK", &tmp);
953 static void acpi_device_set_id(struct acpi_device *device,
954 struct acpi_device *parent, acpi_handle handle,
955 int type)
957 struct acpi_device_info *info;
958 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
959 char *hid = NULL;
960 char *uid = NULL;
961 struct acpi_compatible_id_list *cid_list = NULL;
962 const char *cid_add = NULL;
963 acpi_status status;
965 switch (type) {
966 case ACPI_BUS_TYPE_DEVICE:
967 status = acpi_get_object_info(handle, &buffer);
968 if (ACPI_FAILURE(status)) {
969 printk("%s: Error reading device info\n", __FUNCTION__);
970 return;
973 info = buffer.pointer;
974 if (info->valid & ACPI_VALID_HID)
975 hid = info->hardware_id.value;
976 if (info->valid & ACPI_VALID_UID)
977 uid = info->unique_id.value;
978 if (info->valid & ACPI_VALID_CID)
979 cid_list = &info->compatibility_id;
980 if (info->valid & ACPI_VALID_ADR) {
981 device->pnp.bus_address = info->address;
982 device->flags.bus_address = 1;
985 /* If we have a video/bay/dock device, add our selfdefined
986 HID to the CID list. Like that the video/bay/dock drivers
987 will get autoloaded and the device might still match
988 against another driver.
990 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
991 cid_add = ACPI_VIDEO_HID;
992 else if (ACPI_SUCCESS(acpi_bay_match(device)))
993 cid_add = ACPI_BAY_HID;
994 else if (ACPI_SUCCESS(acpi_dock_match(device)))
995 cid_add = ACPI_DOCK_HID;
997 break;
998 case ACPI_BUS_TYPE_POWER:
999 hid = ACPI_POWER_HID;
1000 break;
1001 case ACPI_BUS_TYPE_PROCESSOR:
1002 hid = ACPI_PROCESSOR_HID;
1003 break;
1004 case ACPI_BUS_TYPE_SYSTEM:
1005 hid = ACPI_SYSTEM_HID;
1006 break;
1007 case ACPI_BUS_TYPE_THERMAL:
1008 hid = ACPI_THERMAL_HID;
1009 break;
1010 case ACPI_BUS_TYPE_POWER_BUTTON:
1011 hid = ACPI_BUTTON_HID_POWERF;
1012 break;
1013 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1014 hid = ACPI_BUTTON_HID_SLEEPF;
1015 break;
1019 * \_SB
1020 * ----
1021 * Fix for the system root bus device -- the only root-level device.
1023 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1024 hid = ACPI_BUS_HID;
1025 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1026 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1029 if (hid) {
1030 strcpy(device->pnp.hardware_id, hid);
1031 device->flags.hardware_id = 1;
1033 if (uid) {
1034 strcpy(device->pnp.unique_id, uid);
1035 device->flags.unique_id = 1;
1037 if (cid_list || cid_add) {
1038 struct acpi_compatible_id_list *list;
1039 int size = 0;
1040 int count = 0;
1042 if (cid_list) {
1043 size = cid_list->size;
1044 } else if (cid_add) {
1045 size = sizeof(struct acpi_compatible_id_list);
1046 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1047 if (!cid_list) {
1048 printk(KERN_ERR "Memory allocation error\n");
1049 kfree(buffer.pointer);
1050 return;
1051 } else {
1052 cid_list->count = 0;
1053 cid_list->size = size;
1056 if (cid_add)
1057 size += sizeof(struct acpi_compatible_id);
1058 list = kmalloc(size, GFP_KERNEL);
1060 if (list) {
1061 if (cid_list) {
1062 memcpy(list, cid_list, cid_list->size);
1063 count = cid_list->count;
1065 if (cid_add) {
1066 strncpy(list->id[count].value, cid_add,
1067 ACPI_MAX_CID_LENGTH);
1068 count++;
1069 device->flags.compatible_ids = 1;
1071 list->size = size;
1072 list->count = count;
1073 device->pnp.cid_list = list;
1074 } else
1075 printk(KERN_ERR "Memory allocation error\n");
1078 kfree(buffer.pointer);
1081 static int acpi_device_set_context(struct acpi_device *device, int type)
1083 acpi_status status = AE_OK;
1084 int result = 0;
1086 * Context
1087 * -------
1088 * Attach this 'struct acpi_device' to the ACPI object. This makes
1089 * resolutions from handle->device very efficient. Note that we need
1090 * to be careful with fixed-feature devices as they all attach to the
1091 * root object.
1093 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1094 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1095 status = acpi_attach_data(device->handle,
1096 acpi_bus_data_handler, device);
1098 if (ACPI_FAILURE(status)) {
1099 printk("Error attaching device data\n");
1100 result = -ENODEV;
1103 return result;
1106 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1108 if (!dev)
1109 return -EINVAL;
1111 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1112 device_release_driver(&dev->dev);
1114 if (!rmdevice)
1115 return 0;
1118 * unbind _ADR-Based Devices when hot removal
1120 if (dev->flags.bus_address) {
1121 if ((dev->parent) && (dev->parent->ops.unbind))
1122 dev->parent->ops.unbind(dev);
1124 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1126 return 0;
1129 static int
1130 acpi_is_child_device(struct acpi_device *device,
1131 int (*matcher)(struct acpi_device *))
1133 int result = -ENODEV;
1135 do {
1136 if (ACPI_SUCCESS(matcher(device)))
1137 return AE_OK;
1138 } while ((device = device->parent));
1140 return result;
1143 static int
1144 acpi_add_single_object(struct acpi_device **child,
1145 struct acpi_device *parent, acpi_handle handle, int type,
1146 struct acpi_bus_ops *ops)
1148 int result = 0;
1149 struct acpi_device *device = NULL;
1152 if (!child)
1153 return -EINVAL;
1155 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1156 if (!device) {
1157 printk(KERN_ERR PREFIX "Memory allocation error\n");
1158 return -ENOMEM;
1161 device->handle = handle;
1162 device->parent = parent;
1163 device->bus_ops = *ops; /* workround for not call .start */
1166 acpi_device_get_busid(device, handle, type);
1169 * Flags
1170 * -----
1171 * Get prior to calling acpi_bus_get_status() so we know whether
1172 * or not _STA is present. Note that we only look for object
1173 * handles -- cannot evaluate objects until we know the device is
1174 * present and properly initialized.
1176 result = acpi_bus_get_flags(device);
1177 if (result)
1178 goto end;
1181 * Status
1182 * ------
1183 * See if the device is present. We always assume that non-Device
1184 * and non-Processor objects (e.g. thermal zones, power resources,
1185 * etc.) are present, functioning, etc. (at least when parent object
1186 * is present). Note that _STA has a different meaning for some
1187 * objects (e.g. power resources) so we need to be careful how we use
1188 * it.
1190 switch (type) {
1191 case ACPI_BUS_TYPE_PROCESSOR:
1192 case ACPI_BUS_TYPE_DEVICE:
1193 result = acpi_bus_get_status(device);
1194 if (ACPI_FAILURE(result)) {
1195 result = -ENODEV;
1196 goto end;
1198 if (!device->status.present) {
1199 /* Bay and dock should be handled even if absent */
1200 if (!ACPI_SUCCESS(
1201 acpi_is_child_device(device, acpi_bay_match)) &&
1202 !ACPI_SUCCESS(
1203 acpi_is_child_device(device, acpi_dock_match))) {
1204 result = -ENODEV;
1205 goto end;
1208 break;
1209 default:
1210 STRUCT_TO_INT(device->status) =
1211 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1212 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1213 break;
1217 * Initialize Device
1218 * -----------------
1219 * TBD: Synch with Core's enumeration/initialization process.
1223 * Hardware ID, Unique ID, & Bus Address
1224 * -------------------------------------
1226 acpi_device_set_id(device, parent, handle, type);
1229 * Power Management
1230 * ----------------
1232 if (device->flags.power_manageable) {
1233 result = acpi_bus_get_power_flags(device);
1234 if (result)
1235 goto end;
1239 * Wakeup device management
1240 *-----------------------
1242 if (device->flags.wake_capable) {
1243 result = acpi_bus_get_wakeup_device_flags(device);
1244 if (result)
1245 goto end;
1249 * Performance Management
1250 * ----------------------
1252 if (device->flags.performance_manageable) {
1253 result = acpi_bus_get_perf_flags(device);
1254 if (result)
1255 goto end;
1258 if ((result = acpi_device_set_context(device, type)))
1259 goto end;
1261 result = acpi_device_register(device, parent);
1264 * Bind _ADR-Based Devices when hot add
1266 if (device->flags.bus_address) {
1267 if (device->parent && device->parent->ops.bind)
1268 device->parent->ops.bind(device);
1271 end:
1272 if (!result)
1273 *child = device;
1274 else {
1275 kfree(device->pnp.cid_list);
1276 kfree(device);
1279 return result;
1282 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1284 acpi_status status = AE_OK;
1285 struct acpi_device *parent = NULL;
1286 struct acpi_device *child = NULL;
1287 acpi_handle phandle = NULL;
1288 acpi_handle chandle = NULL;
1289 acpi_object_type type = 0;
1290 u32 level = 1;
1293 if (!start)
1294 return -EINVAL;
1296 parent = start;
1297 phandle = start->handle;
1300 * Parse through the ACPI namespace, identify all 'devices', and
1301 * create a new 'struct acpi_device' for each.
1303 while ((level > 0) && parent) {
1305 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1306 chandle, &chandle);
1309 * If this scope is exhausted then move our way back up.
1311 if (ACPI_FAILURE(status)) {
1312 level--;
1313 chandle = phandle;
1314 acpi_get_parent(phandle, &phandle);
1315 if (parent->parent)
1316 parent = parent->parent;
1317 continue;
1320 status = acpi_get_type(chandle, &type);
1321 if (ACPI_FAILURE(status))
1322 continue;
1325 * If this is a scope object then parse it (depth-first).
1327 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1328 level++;
1329 phandle = chandle;
1330 chandle = NULL;
1331 continue;
1335 * We're only interested in objects that we consider 'devices'.
1337 switch (type) {
1338 case ACPI_TYPE_DEVICE:
1339 type = ACPI_BUS_TYPE_DEVICE;
1340 break;
1341 case ACPI_TYPE_PROCESSOR:
1342 type = ACPI_BUS_TYPE_PROCESSOR;
1343 break;
1344 case ACPI_TYPE_THERMAL:
1345 type = ACPI_BUS_TYPE_THERMAL;
1346 break;
1347 case ACPI_TYPE_POWER:
1348 type = ACPI_BUS_TYPE_POWER;
1349 break;
1350 default:
1351 continue;
1354 if (ops->acpi_op_add)
1355 status = acpi_add_single_object(&child, parent,
1356 chandle, type, ops);
1357 else
1358 status = acpi_bus_get_device(chandle, &child);
1360 if (ACPI_FAILURE(status))
1361 continue;
1363 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1364 status = acpi_start_single_object(child);
1365 if (ACPI_FAILURE(status))
1366 continue;
1370 * If the device is present, enabled, and functioning then
1371 * parse its scope (depth-first). Note that we need to
1372 * represent absent devices to facilitate PnP notifications
1373 * -- but only the subtree head (not all of its children,
1374 * which will be enumerated when the parent is inserted).
1376 * TBD: Need notifications and other detection mechanisms
1377 * in place before we can fully implement this.
1379 if (child->status.present) {
1380 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1381 NULL, NULL);
1382 if (ACPI_SUCCESS(status)) {
1383 level++;
1384 phandle = chandle;
1385 chandle = NULL;
1386 parent = child;
1391 return 0;
1395 acpi_bus_add(struct acpi_device **child,
1396 struct acpi_device *parent, acpi_handle handle, int type)
1398 int result;
1399 struct acpi_bus_ops ops;
1401 memset(&ops, 0, sizeof(ops));
1402 ops.acpi_op_add = 1;
1404 result = acpi_add_single_object(child, parent, handle, type, &ops);
1405 if (!result)
1406 result = acpi_bus_scan(*child, &ops);
1408 return result;
1411 EXPORT_SYMBOL(acpi_bus_add);
1413 int acpi_bus_start(struct acpi_device *device)
1415 int result;
1416 struct acpi_bus_ops ops;
1419 if (!device)
1420 return -EINVAL;
1422 result = acpi_start_single_object(device);
1423 if (!result) {
1424 memset(&ops, 0, sizeof(ops));
1425 ops.acpi_op_start = 1;
1426 result = acpi_bus_scan(device, &ops);
1428 return result;
1431 EXPORT_SYMBOL(acpi_bus_start);
1433 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1435 acpi_status status;
1436 struct acpi_device *parent, *child;
1437 acpi_handle phandle, chandle;
1438 acpi_object_type type;
1439 u32 level = 1;
1440 int err = 0;
1442 parent = start;
1443 phandle = start->handle;
1444 child = chandle = NULL;
1446 while ((level > 0) && parent && (!err)) {
1447 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1448 chandle, &chandle);
1451 * If this scope is exhausted then move our way back up.
1453 if (ACPI_FAILURE(status)) {
1454 level--;
1455 chandle = phandle;
1456 acpi_get_parent(phandle, &phandle);
1457 child = parent;
1458 parent = parent->parent;
1460 if (level == 0)
1461 err = acpi_bus_remove(child, rmdevice);
1462 else
1463 err = acpi_bus_remove(child, 1);
1465 continue;
1468 status = acpi_get_type(chandle, &type);
1469 if (ACPI_FAILURE(status)) {
1470 continue;
1473 * If there is a device corresponding to chandle then
1474 * parse it (depth-first).
1476 if (acpi_bus_get_device(chandle, &child) == 0) {
1477 level++;
1478 phandle = chandle;
1479 chandle = NULL;
1480 parent = child;
1482 continue;
1484 return err;
1486 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1489 static int acpi_bus_scan_fixed(struct acpi_device *root)
1491 int result = 0;
1492 struct acpi_device *device = NULL;
1493 struct acpi_bus_ops ops;
1495 if (!root)
1496 return -ENODEV;
1498 memset(&ops, 0, sizeof(ops));
1499 ops.acpi_op_add = 1;
1500 ops.acpi_op_start = 1;
1503 * Enumerate all fixed-feature devices.
1505 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1506 result = acpi_add_single_object(&device, acpi_root,
1507 NULL,
1508 ACPI_BUS_TYPE_POWER_BUTTON,
1509 &ops);
1512 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1513 result = acpi_add_single_object(&device, acpi_root,
1514 NULL,
1515 ACPI_BUS_TYPE_SLEEP_BUTTON,
1516 &ops);
1519 return result;
1522 int __init acpi_boot_ec_enable(void);
1524 static int __init acpi_scan_init(void)
1526 int result;
1527 struct acpi_bus_ops ops;
1530 if (acpi_disabled)
1531 return 0;
1533 memset(&ops, 0, sizeof(ops));
1534 ops.acpi_op_add = 1;
1535 ops.acpi_op_start = 1;
1537 result = bus_register(&acpi_bus_type);
1538 if (result) {
1539 /* We don't want to quit even if we failed to add suspend/resume */
1540 printk(KERN_ERR PREFIX "Could not register bus type\n");
1544 * Create the root device in the bus's device tree
1546 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1547 ACPI_BUS_TYPE_SYSTEM, &ops);
1548 if (result)
1549 goto Done;
1552 * Enumerate devices in the ACPI namespace.
1554 result = acpi_bus_scan_fixed(acpi_root);
1556 /* EC region might be needed at bus_scan, so enable it now */
1557 acpi_boot_ec_enable();
1559 if (!result)
1560 result = acpi_bus_scan(acpi_root, &ops);
1562 if (result)
1563 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1565 Done:
1566 return result;
1569 subsys_initcall(acpi_scan_init);