ppc: Clean up scanlog
[linux-2.6.git] / drivers / acpi / scan.c
blob5e7e991717d76f97981fcff3d0a221879b47fcea
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/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
15 #include <acpi/acpi_drivers.h>
17 #include "internal.h"
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 #define STRUCT_TO_INT(s) (*((int*)&s))
22 extern struct acpi_device *acpi_root;
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30 static const char *dummy_hid = "device";
32 static LIST_HEAD(acpi_device_list);
33 static LIST_HEAD(acpi_bus_id_list);
34 static DEFINE_MUTEX(acpi_scan_lock);
35 static LIST_HEAD(acpi_scan_handlers_list);
36 DEFINE_MUTEX(acpi_device_lock);
37 LIST_HEAD(acpi_wakeup_device_list);
39 struct acpi_device_bus_id{
40 char bus_id[15];
41 unsigned int instance_no;
42 struct list_head node;
45 void acpi_scan_lock_acquire(void)
47 mutex_lock(&acpi_scan_lock);
49 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
51 void acpi_scan_lock_release(void)
53 mutex_unlock(&acpi_scan_lock);
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
57 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
59 if (!handler || !handler->attach)
60 return -EINVAL;
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
63 return 0;
67 * Creates hid/cid(s) string needed for modalias and uevent
68 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
69 * char *modalias: "acpi:IBM0001:ACPI0001"
71 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
72 int size)
74 int len;
75 int count;
76 struct acpi_hardware_id *id;
78 if (list_empty(&acpi_dev->pnp.ids))
79 return 0;
81 len = snprintf(modalias, size, "acpi:");
82 size -= len;
84 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
85 count = snprintf(&modalias[len], size, "%s:", id->id);
86 if (count < 0 || count >= size)
87 return -EINVAL;
88 len += count;
89 size -= count;
92 modalias[len] = '\0';
93 return len;
96 static ssize_t
97 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
98 struct acpi_device *acpi_dev = to_acpi_device(dev);
99 int len;
101 /* Device has no HID and no CID or string is >1024 */
102 len = create_modalias(acpi_dev, buf, 1024);
103 if (len <= 0)
104 return 0;
105 buf[len++] = '\n';
106 return len;
108 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
111 * acpi_bus_hot_remove_device: hot-remove a device and its children
112 * @context: struct acpi_eject_event pointer (freed in this func)
114 * Hot-remove a device and its children. This function frees up the
115 * memory space passed by arg context, so that the caller may call
116 * this function asynchronously through acpi_os_hotplug_execute().
118 void acpi_bus_hot_remove_device(void *context)
120 struct acpi_eject_event *ej_event = context;
121 struct acpi_device *device = ej_event->device;
122 acpi_handle handle = device->handle;
123 acpi_handle temp;
124 struct acpi_object_list arg_list;
125 union acpi_object arg;
126 acpi_status status = AE_OK;
127 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
129 mutex_lock(&acpi_scan_lock);
131 /* If there is no handle, the device node has been unregistered. */
132 if (!device->handle) {
133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
135 goto out;
138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
139 "Hot-removing device %s...\n", dev_name(&device->dev)));
141 acpi_bus_trim(device);
142 /* Device node has been unregistered. */
143 put_device(&device->dev);
144 device = NULL;
146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
147 arg_list.count = 1;
148 arg_list.pointer = &arg;
149 arg.type = ACPI_TYPE_INTEGER;
150 arg.integer.value = 0;
151 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
154 arg_list.count = 1;
155 arg_list.pointer = &arg;
156 arg.type = ACPI_TYPE_INTEGER;
157 arg.integer.value = 1;
160 * TBD: _EJD support.
162 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
163 if (ACPI_FAILURE(status)) {
164 if (status != AE_NOT_FOUND)
165 acpi_handle_warn(handle, "Eject failed\n");
167 /* Tell the firmware the hot-remove operation has failed. */
168 acpi_evaluate_hotplug_ost(handle, ej_event->event,
169 ost_code, NULL);
172 out:
173 mutex_unlock(&acpi_scan_lock);
174 kfree(context);
175 return;
177 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
179 static ssize_t real_power_state_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct acpi_device *adev = to_acpi_device(dev);
183 int state;
184 int ret;
186 ret = acpi_device_get_power(adev, &state);
187 if (ret)
188 return ret;
190 return sprintf(buf, "%s\n", acpi_power_state_string(state));
193 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
195 static ssize_t power_state_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
198 struct acpi_device *adev = to_acpi_device(dev);
200 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
203 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
205 static ssize_t
206 acpi_eject_store(struct device *d, struct device_attribute *attr,
207 const char *buf, size_t count)
209 int ret = count;
210 acpi_status status;
211 acpi_object_type type = 0;
212 struct acpi_device *acpi_device = to_acpi_device(d);
213 struct acpi_eject_event *ej_event;
215 if ((!count) || (buf[0] != '1')) {
216 return -EINVAL;
218 if (!acpi_device->driver && !acpi_device->handler) {
219 ret = -ENODEV;
220 goto err;
222 status = acpi_get_type(acpi_device->handle, &type);
223 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
224 ret = -ENODEV;
225 goto err;
228 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
229 if (!ej_event) {
230 ret = -ENOMEM;
231 goto err;
234 get_device(&acpi_device->dev);
235 ej_event->device = acpi_device;
236 if (acpi_device->flags.eject_pending) {
237 /* event originated from ACPI eject notification */
238 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
239 acpi_device->flags.eject_pending = 0;
240 } else {
241 /* event originated from user */
242 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
243 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
244 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
247 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
248 if (ACPI_FAILURE(status)) {
249 put_device(&acpi_device->dev);
250 kfree(ej_event);
252 err:
253 return ret;
256 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
258 static ssize_t
259 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
260 struct acpi_device *acpi_dev = to_acpi_device(dev);
262 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
264 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
266 static ssize_t acpi_device_uid_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct acpi_device *acpi_dev = to_acpi_device(dev);
271 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
273 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
275 static ssize_t acpi_device_adr_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct acpi_device *acpi_dev = to_acpi_device(dev);
280 return sprintf(buf, "0x%08x\n",
281 (unsigned int)(acpi_dev->pnp.bus_address));
283 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
285 static ssize_t
286 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
289 int result;
291 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
292 if (result)
293 goto end;
295 result = sprintf(buf, "%s\n", (char*)path.pointer);
296 kfree(path.pointer);
297 end:
298 return result;
300 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
302 /* sysfs file that shows description text from the ACPI _STR method */
303 static ssize_t description_show(struct device *dev,
304 struct device_attribute *attr,
305 char *buf) {
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 int result;
309 if (acpi_dev->pnp.str_obj == NULL)
310 return 0;
313 * The _STR object contains a Unicode identifier for a device.
314 * We need to convert to utf-8 so it can be displayed.
316 result = utf16s_to_utf8s(
317 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
318 acpi_dev->pnp.str_obj->buffer.length,
319 UTF16_LITTLE_ENDIAN, buf,
320 PAGE_SIZE);
322 buf[result++] = '\n';
324 return result;
326 static DEVICE_ATTR(description, 0444, description_show, NULL);
328 static ssize_t
329 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
330 char *buf) {
331 struct acpi_device *acpi_dev = to_acpi_device(dev);
333 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
335 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
337 static int acpi_device_setup_files(struct acpi_device *dev)
339 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
340 acpi_status status;
341 acpi_handle temp;
342 unsigned long long sun;
343 int result = 0;
346 * Devices gotten from FADT don't have a "path" attribute
348 if (dev->handle) {
349 result = device_create_file(&dev->dev, &dev_attr_path);
350 if (result)
351 goto end;
354 if (!list_empty(&dev->pnp.ids)) {
355 result = device_create_file(&dev->dev, &dev_attr_hid);
356 if (result)
357 goto end;
359 result = device_create_file(&dev->dev, &dev_attr_modalias);
360 if (result)
361 goto end;
365 * If device has _STR, 'description' file is created
367 status = acpi_get_handle(dev->handle, "_STR", &temp);
368 if (ACPI_SUCCESS(status)) {
369 status = acpi_evaluate_object(dev->handle, "_STR",
370 NULL, &buffer);
371 if (ACPI_FAILURE(status))
372 buffer.pointer = NULL;
373 dev->pnp.str_obj = buffer.pointer;
374 result = device_create_file(&dev->dev, &dev_attr_description);
375 if (result)
376 goto end;
379 if (dev->flags.bus_address)
380 result = device_create_file(&dev->dev, &dev_attr_adr);
381 if (dev->pnp.unique_id)
382 result = device_create_file(&dev->dev, &dev_attr_uid);
384 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
385 if (ACPI_SUCCESS(status)) {
386 dev->pnp.sun = (unsigned long)sun;
387 result = device_create_file(&dev->dev, &dev_attr_sun);
388 if (result)
389 goto end;
390 } else {
391 dev->pnp.sun = (unsigned long)-1;
395 * If device has _EJ0, 'eject' file is created that is used to trigger
396 * hot-removal function from userland.
398 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
399 if (ACPI_SUCCESS(status)) {
400 result = device_create_file(&dev->dev, &dev_attr_eject);
401 if (result)
402 return result;
405 if (dev->flags.power_manageable) {
406 result = device_create_file(&dev->dev, &dev_attr_power_state);
407 if (result)
408 return result;
410 if (dev->power.flags.power_resources)
411 result = device_create_file(&dev->dev,
412 &dev_attr_real_power_state);
415 end:
416 return result;
419 static void acpi_device_remove_files(struct acpi_device *dev)
421 acpi_status status;
422 acpi_handle temp;
424 if (dev->flags.power_manageable) {
425 device_remove_file(&dev->dev, &dev_attr_power_state);
426 if (dev->power.flags.power_resources)
427 device_remove_file(&dev->dev,
428 &dev_attr_real_power_state);
432 * If device has _STR, remove 'description' file
434 status = acpi_get_handle(dev->handle, "_STR", &temp);
435 if (ACPI_SUCCESS(status)) {
436 kfree(dev->pnp.str_obj);
437 device_remove_file(&dev->dev, &dev_attr_description);
440 * If device has _EJ0, remove 'eject' file.
442 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
443 if (ACPI_SUCCESS(status))
444 device_remove_file(&dev->dev, &dev_attr_eject);
446 status = acpi_get_handle(dev->handle, "_SUN", &temp);
447 if (ACPI_SUCCESS(status))
448 device_remove_file(&dev->dev, &dev_attr_sun);
450 if (dev->pnp.unique_id)
451 device_remove_file(&dev->dev, &dev_attr_uid);
452 if (dev->flags.bus_address)
453 device_remove_file(&dev->dev, &dev_attr_adr);
454 device_remove_file(&dev->dev, &dev_attr_modalias);
455 device_remove_file(&dev->dev, &dev_attr_hid);
456 if (dev->handle)
457 device_remove_file(&dev->dev, &dev_attr_path);
459 /* --------------------------------------------------------------------------
460 ACPI Bus operations
461 -------------------------------------------------------------------------- */
463 static const struct acpi_device_id *__acpi_match_device(
464 struct acpi_device *device, const struct acpi_device_id *ids)
466 const struct acpi_device_id *id;
467 struct acpi_hardware_id *hwid;
470 * If the device is not present, it is unnecessary to load device
471 * driver for it.
473 if (!device->status.present)
474 return NULL;
476 for (id = ids; id->id[0]; id++)
477 list_for_each_entry(hwid, &device->pnp.ids, list)
478 if (!strcmp((char *) id->id, hwid->id))
479 return id;
481 return NULL;
485 * acpi_match_device - Match a struct device against a given list of ACPI IDs
486 * @ids: Array of struct acpi_device_id object to match against.
487 * @dev: The device structure to match.
489 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
490 * object for that handle and use that object to match against a given list of
491 * device IDs.
493 * Return a pointer to the first matching ID on success or %NULL on failure.
495 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
496 const struct device *dev)
498 struct acpi_device *adev;
499 acpi_handle handle = ACPI_HANDLE(dev);
501 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
502 return NULL;
504 return __acpi_match_device(adev, ids);
506 EXPORT_SYMBOL_GPL(acpi_match_device);
508 int acpi_match_device_ids(struct acpi_device *device,
509 const struct acpi_device_id *ids)
511 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
513 EXPORT_SYMBOL(acpi_match_device_ids);
515 void acpi_free_ids(struct acpi_device *device)
517 struct acpi_hardware_id *id, *tmp;
519 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
520 kfree(id->id);
521 kfree(id);
523 kfree(device->pnp.unique_id);
526 static void acpi_free_power_resources_lists(struct acpi_device *device)
528 int i;
530 if (device->wakeup.flags.valid)
531 acpi_power_resources_list_free(&device->wakeup.resources);
533 if (!device->flags.power_manageable)
534 return;
536 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
537 struct acpi_device_power_state *ps = &device->power.states[i];
538 acpi_power_resources_list_free(&ps->resources);
542 static void acpi_device_release(struct device *dev)
544 struct acpi_device *acpi_dev = to_acpi_device(dev);
546 acpi_free_ids(acpi_dev);
547 acpi_free_power_resources_lists(acpi_dev);
548 kfree(acpi_dev);
551 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
553 struct acpi_device *acpi_dev = to_acpi_device(dev);
554 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
556 return acpi_dev->flags.match_driver
557 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
560 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
562 struct acpi_device *acpi_dev = to_acpi_device(dev);
563 int len;
565 if (list_empty(&acpi_dev->pnp.ids))
566 return 0;
568 if (add_uevent_var(env, "MODALIAS="))
569 return -ENOMEM;
570 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
571 sizeof(env->buf) - env->buflen);
572 if (len >= (sizeof(env->buf) - env->buflen))
573 return -ENOMEM;
574 env->buflen += len;
575 return 0;
578 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
580 struct acpi_device *device = data;
582 device->driver->ops.notify(device, event);
585 static acpi_status acpi_device_notify_fixed(void *data)
587 struct acpi_device *device = data;
589 /* Fixed hardware devices have no handles */
590 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
591 return AE_OK;
594 static int acpi_device_install_notify_handler(struct acpi_device *device)
596 acpi_status status;
598 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
599 status =
600 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
601 acpi_device_notify_fixed,
602 device);
603 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
604 status =
605 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
606 acpi_device_notify_fixed,
607 device);
608 else
609 status = acpi_install_notify_handler(device->handle,
610 ACPI_DEVICE_NOTIFY,
611 acpi_device_notify,
612 device);
614 if (ACPI_FAILURE(status))
615 return -EINVAL;
616 return 0;
619 static void acpi_device_remove_notify_handler(struct acpi_device *device)
621 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
622 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
623 acpi_device_notify_fixed);
624 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
625 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
626 acpi_device_notify_fixed);
627 else
628 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
629 acpi_device_notify);
632 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
633 static int acpi_device_probe(struct device * dev)
635 struct acpi_device *acpi_dev = to_acpi_device(dev);
636 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
637 int ret;
639 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
640 if (!ret) {
641 if (acpi_drv->ops.notify) {
642 ret = acpi_device_install_notify_handler(acpi_dev);
643 if (ret) {
644 if (acpi_drv->ops.remove)
645 acpi_drv->ops.remove(acpi_dev);
646 acpi_dev->driver = NULL;
647 acpi_dev->driver_data = NULL;
648 return ret;
652 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
653 "Found driver [%s] for device [%s]\n",
654 acpi_drv->name, acpi_dev->pnp.bus_id));
655 get_device(dev);
657 return ret;
660 static int acpi_device_remove(struct device * dev)
662 struct acpi_device *acpi_dev = to_acpi_device(dev);
663 struct acpi_driver *acpi_drv = acpi_dev->driver;
665 if (acpi_drv) {
666 if (acpi_drv->ops.notify)
667 acpi_device_remove_notify_handler(acpi_dev);
668 if (acpi_drv->ops.remove)
669 acpi_drv->ops.remove(acpi_dev);
671 acpi_dev->driver = NULL;
672 acpi_dev->driver_data = NULL;
674 put_device(dev);
675 return 0;
678 struct bus_type acpi_bus_type = {
679 .name = "acpi",
680 .match = acpi_bus_match,
681 .probe = acpi_device_probe,
682 .remove = acpi_device_remove,
683 .uevent = acpi_device_uevent,
686 int acpi_device_add(struct acpi_device *device,
687 void (*release)(struct device *))
689 int result;
690 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
691 int found = 0;
693 if (device->handle) {
694 acpi_status status;
696 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
697 device);
698 if (ACPI_FAILURE(status)) {
699 acpi_handle_err(device->handle,
700 "Unable to attach device data\n");
701 return -ENODEV;
706 * Linkage
707 * -------
708 * Link this device to its parent and siblings.
710 INIT_LIST_HEAD(&device->children);
711 INIT_LIST_HEAD(&device->node);
712 INIT_LIST_HEAD(&device->wakeup_list);
713 INIT_LIST_HEAD(&device->physical_node_list);
714 mutex_init(&device->physical_node_lock);
715 INIT_LIST_HEAD(&device->power_dependent);
717 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
718 if (!new_bus_id) {
719 pr_err(PREFIX "Memory allocation error\n");
720 result = -ENOMEM;
721 goto err_detach;
724 mutex_lock(&acpi_device_lock);
726 * Find suitable bus_id and instance number in acpi_bus_id_list
727 * If failed, create one and link it into acpi_bus_id_list
729 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
730 if (!strcmp(acpi_device_bus_id->bus_id,
731 acpi_device_hid(device))) {
732 acpi_device_bus_id->instance_no++;
733 found = 1;
734 kfree(new_bus_id);
735 break;
738 if (!found) {
739 acpi_device_bus_id = new_bus_id;
740 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
741 acpi_device_bus_id->instance_no = 0;
742 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
744 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
746 if (device->parent)
747 list_add_tail(&device->node, &device->parent->children);
749 if (device->wakeup.flags.valid)
750 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
751 mutex_unlock(&acpi_device_lock);
753 if (device->parent)
754 device->dev.parent = &device->parent->dev;
755 device->dev.bus = &acpi_bus_type;
756 device->dev.release = release;
757 result = device_add(&device->dev);
758 if (result) {
759 dev_err(&device->dev, "Error registering device\n");
760 goto err;
763 result = acpi_device_setup_files(device);
764 if (result)
765 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
766 dev_name(&device->dev));
768 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
769 return 0;
771 err:
772 mutex_lock(&acpi_device_lock);
773 if (device->parent)
774 list_del(&device->node);
775 list_del(&device->wakeup_list);
776 mutex_unlock(&acpi_device_lock);
778 err_detach:
779 acpi_detach_data(device->handle, acpi_bus_data_handler);
780 return result;
783 static void acpi_device_unregister(struct acpi_device *device)
785 mutex_lock(&acpi_device_lock);
786 if (device->parent)
787 list_del(&device->node);
789 list_del(&device->wakeup_list);
790 mutex_unlock(&acpi_device_lock);
792 acpi_detach_data(device->handle, acpi_bus_data_handler);
794 acpi_power_add_remove_device(device, false);
795 acpi_device_remove_files(device);
796 if (device->remove)
797 device->remove(device);
799 device_del(&device->dev);
801 * Transition the device to D3cold to drop the reference counts of all
802 * power resources the device depends on and turn off the ones that have
803 * no more references.
805 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
806 device->handle = NULL;
807 put_device(&device->dev);
810 /* --------------------------------------------------------------------------
811 Driver Management
812 -------------------------------------------------------------------------- */
814 * acpi_bus_driver_init - add a device to a driver
815 * @device: the device to add and initialize
816 * @driver: driver for the device
818 * Used to initialize a device via its device driver. Called whenever a
819 * driver is bound to a device. Invokes the driver's add() ops.
821 static int
822 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
824 int result = 0;
826 if (!device || !driver)
827 return -EINVAL;
829 if (!driver->ops.add)
830 return -ENOSYS;
832 result = driver->ops.add(device);
833 if (result) {
834 device->driver = NULL;
835 device->driver_data = NULL;
836 return result;
839 device->driver = driver;
842 * TBD - Configuration Management: Assign resources to device based
843 * upon possible configuration and currently allocated resources.
846 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
847 "Driver successfully bound to device\n"));
848 return 0;
852 * acpi_bus_register_driver - register a driver with the ACPI bus
853 * @driver: driver being registered
855 * Registers a driver with the ACPI bus. Searches the namespace for all
856 * devices that match the driver's criteria and binds. Returns zero for
857 * success or a negative error status for failure.
859 int acpi_bus_register_driver(struct acpi_driver *driver)
861 int ret;
863 if (acpi_disabled)
864 return -ENODEV;
865 driver->drv.name = driver->name;
866 driver->drv.bus = &acpi_bus_type;
867 driver->drv.owner = driver->owner;
869 ret = driver_register(&driver->drv);
870 return ret;
873 EXPORT_SYMBOL(acpi_bus_register_driver);
876 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
877 * @driver: driver to unregister
879 * Unregisters a driver with the ACPI bus. Searches the namespace for all
880 * devices that match the driver's criteria and unbinds.
882 void acpi_bus_unregister_driver(struct acpi_driver *driver)
884 driver_unregister(&driver->drv);
887 EXPORT_SYMBOL(acpi_bus_unregister_driver);
889 /* --------------------------------------------------------------------------
890 Device Enumeration
891 -------------------------------------------------------------------------- */
892 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
894 struct acpi_device *device = NULL;
895 acpi_status status;
898 * Fixed hardware devices do not appear in the namespace and do not
899 * have handles, but we fabricate acpi_devices for them, so we have
900 * to deal with them specially.
902 if (!handle)
903 return acpi_root;
905 do {
906 status = acpi_get_parent(handle, &handle);
907 if (ACPI_FAILURE(status))
908 return status == AE_NULL_ENTRY ? NULL : acpi_root;
909 } while (acpi_bus_get_device(handle, &device));
910 return device;
913 acpi_status
914 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
916 acpi_status status;
917 acpi_handle tmp;
918 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
919 union acpi_object *obj;
921 status = acpi_get_handle(handle, "_EJD", &tmp);
922 if (ACPI_FAILURE(status))
923 return status;
925 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
926 if (ACPI_SUCCESS(status)) {
927 obj = buffer.pointer;
928 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
929 ejd);
930 kfree(buffer.pointer);
932 return status;
934 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
936 void acpi_bus_data_handler(acpi_handle handle, void *context)
939 /* TBD */
941 return;
944 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
945 struct acpi_device_wakeup *wakeup)
947 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
948 union acpi_object *package = NULL;
949 union acpi_object *element = NULL;
950 acpi_status status;
951 int err = -ENODATA;
953 if (!wakeup)
954 return -EINVAL;
956 INIT_LIST_HEAD(&wakeup->resources);
958 /* _PRW */
959 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
960 if (ACPI_FAILURE(status)) {
961 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
962 return err;
965 package = (union acpi_object *)buffer.pointer;
967 if (!package || package->package.count < 2)
968 goto out;
970 element = &(package->package.elements[0]);
971 if (!element)
972 goto out;
974 if (element->type == ACPI_TYPE_PACKAGE) {
975 if ((element->package.count < 2) ||
976 (element->package.elements[0].type !=
977 ACPI_TYPE_LOCAL_REFERENCE)
978 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
979 goto out;
981 wakeup->gpe_device =
982 element->package.elements[0].reference.handle;
983 wakeup->gpe_number =
984 (u32) element->package.elements[1].integer.value;
985 } else if (element->type == ACPI_TYPE_INTEGER) {
986 wakeup->gpe_device = NULL;
987 wakeup->gpe_number = element->integer.value;
988 } else {
989 goto out;
992 element = &(package->package.elements[1]);
993 if (element->type != ACPI_TYPE_INTEGER)
994 goto out;
996 wakeup->sleep_state = element->integer.value;
998 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
999 if (err)
1000 goto out;
1002 if (!list_empty(&wakeup->resources)) {
1003 int sleep_state;
1005 err = acpi_power_wakeup_list_init(&wakeup->resources,
1006 &sleep_state);
1007 if (err) {
1008 acpi_handle_warn(handle, "Retrieving current states "
1009 "of wakeup power resources failed\n");
1010 acpi_power_resources_list_free(&wakeup->resources);
1011 goto out;
1013 if (sleep_state < wakeup->sleep_state) {
1014 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1015 "(S%d) by S%d from power resources\n",
1016 (int)wakeup->sleep_state, sleep_state);
1017 wakeup->sleep_state = sleep_state;
1020 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1022 out:
1023 kfree(buffer.pointer);
1024 return err;
1027 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1029 struct acpi_device_id button_device_ids[] = {
1030 {"PNP0C0C", 0},
1031 {"PNP0C0D", 0},
1032 {"PNP0C0E", 0},
1033 {"", 0},
1035 acpi_status status;
1036 acpi_event_status event_status;
1038 device->wakeup.flags.notifier_present = 0;
1040 /* Power button, Lid switch always enable wakeup */
1041 if (!acpi_match_device_ids(device, button_device_ids)) {
1042 device->wakeup.flags.run_wake = 1;
1043 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1044 /* Do not use Lid/sleep button for S5 wakeup */
1045 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1046 device->wakeup.sleep_state = ACPI_STATE_S4;
1048 device_set_wakeup_capable(&device->dev, true);
1049 return;
1052 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1053 device->wakeup.gpe_number,
1054 &event_status);
1055 if (status == AE_OK)
1056 device->wakeup.flags.run_wake =
1057 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1060 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1062 acpi_handle temp;
1063 acpi_status status = 0;
1064 int err;
1066 /* Presence of _PRW indicates wake capable */
1067 status = acpi_get_handle(device->handle, "_PRW", &temp);
1068 if (ACPI_FAILURE(status))
1069 return;
1071 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1072 &device->wakeup);
1073 if (err) {
1074 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1075 return;
1078 device->wakeup.flags.valid = 1;
1079 device->wakeup.prepare_count = 0;
1080 acpi_bus_set_run_wake_flags(device);
1081 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1082 * system for the ACPI device with the _PRW object.
1083 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1084 * So it is necessary to call _DSW object first. Only when it is not
1085 * present will the _PSW object used.
1087 err = acpi_device_sleep_wake(device, 0, 0, 0);
1088 if (err)
1089 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1090 "error in _DSW or _PSW evaluation\n"));
1093 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1095 struct acpi_device_power_state *ps = &device->power.states[state];
1096 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1097 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1098 acpi_handle handle;
1099 acpi_status status;
1101 INIT_LIST_HEAD(&ps->resources);
1103 /* Evaluate "_PRx" to get referenced power resources */
1104 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1105 if (ACPI_SUCCESS(status)) {
1106 union acpi_object *package = buffer.pointer;
1108 if (buffer.length && package
1109 && package->type == ACPI_TYPE_PACKAGE
1110 && package->package.count) {
1111 int err = acpi_extract_power_resources(package, 0,
1112 &ps->resources);
1113 if (!err)
1114 device->power.flags.power_resources = 1;
1116 ACPI_FREE(buffer.pointer);
1119 /* Evaluate "_PSx" to see if we can do explicit sets */
1120 pathname[2] = 'S';
1121 status = acpi_get_handle(device->handle, pathname, &handle);
1122 if (ACPI_SUCCESS(status))
1123 ps->flags.explicit_set = 1;
1126 * State is valid if there are means to put the device into it.
1127 * D3hot is only valid if _PR3 present.
1129 if (!list_empty(&ps->resources)
1130 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1131 ps->flags.valid = 1;
1132 ps->flags.os_accessible = 1;
1135 ps->power = -1; /* Unknown - driver assigned */
1136 ps->latency = -1; /* Unknown - driver assigned */
1139 static void acpi_bus_get_power_flags(struct acpi_device *device)
1141 acpi_status status;
1142 acpi_handle handle;
1143 u32 i;
1145 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1146 status = acpi_get_handle(device->handle, "_PS0", &handle);
1147 if (ACPI_FAILURE(status)) {
1148 status = acpi_get_handle(device->handle, "_PR0", &handle);
1149 if (ACPI_FAILURE(status))
1150 return;
1153 device->flags.power_manageable = 1;
1156 * Power Management Flags
1158 status = acpi_get_handle(device->handle, "_PSC", &handle);
1159 if (ACPI_SUCCESS(status))
1160 device->power.flags.explicit_get = 1;
1161 status = acpi_get_handle(device->handle, "_IRC", &handle);
1162 if (ACPI_SUCCESS(status))
1163 device->power.flags.inrush_current = 1;
1166 * Enumerate supported power management states
1168 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1169 acpi_bus_init_power_state(device, i);
1171 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1173 /* Set defaults for D0 and D3 states (always valid) */
1174 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1175 device->power.states[ACPI_STATE_D0].power = 100;
1176 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1177 device->power.states[ACPI_STATE_D3].power = 0;
1179 /* Set D3cold's explicit_set flag if _PS3 exists. */
1180 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1181 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1183 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1184 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1185 device->power.flags.power_resources)
1186 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1188 if (acpi_bus_init_power(device)) {
1189 acpi_free_power_resources_lists(device);
1190 device->flags.power_manageable = 0;
1194 static void acpi_bus_get_flags(struct acpi_device *device)
1196 acpi_status status = AE_OK;
1197 acpi_handle temp = NULL;
1199 /* Presence of _STA indicates 'dynamic_status' */
1200 status = acpi_get_handle(device->handle, "_STA", &temp);
1201 if (ACPI_SUCCESS(status))
1202 device->flags.dynamic_status = 1;
1204 /* Presence of _RMV indicates 'removable' */
1205 status = acpi_get_handle(device->handle, "_RMV", &temp);
1206 if (ACPI_SUCCESS(status))
1207 device->flags.removable = 1;
1209 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1210 status = acpi_get_handle(device->handle, "_EJD", &temp);
1211 if (ACPI_SUCCESS(status))
1212 device->flags.ejectable = 1;
1213 else {
1214 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1215 if (ACPI_SUCCESS(status))
1216 device->flags.ejectable = 1;
1220 static void acpi_device_get_busid(struct acpi_device *device)
1222 char bus_id[5] = { '?', 0 };
1223 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1224 int i = 0;
1227 * Bus ID
1228 * ------
1229 * The device's Bus ID is simply the object name.
1230 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1232 if (ACPI_IS_ROOT_DEVICE(device)) {
1233 strcpy(device->pnp.bus_id, "ACPI");
1234 return;
1237 switch (device->device_type) {
1238 case ACPI_BUS_TYPE_POWER_BUTTON:
1239 strcpy(device->pnp.bus_id, "PWRF");
1240 break;
1241 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1242 strcpy(device->pnp.bus_id, "SLPF");
1243 break;
1244 default:
1245 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1246 /* Clean up trailing underscores (if any) */
1247 for (i = 3; i > 1; i--) {
1248 if (bus_id[i] == '_')
1249 bus_id[i] = '\0';
1250 else
1251 break;
1253 strcpy(device->pnp.bus_id, bus_id);
1254 break;
1259 * acpi_bay_match - see if a device is an ejectable driver bay
1261 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1262 * then we can safely call it an ejectable drive bay
1264 static int acpi_bay_match(struct acpi_device *device){
1265 acpi_status status;
1266 acpi_handle handle;
1267 acpi_handle tmp;
1268 acpi_handle phandle;
1270 handle = device->handle;
1272 status = acpi_get_handle(handle, "_EJ0", &tmp);
1273 if (ACPI_FAILURE(status))
1274 return -ENODEV;
1276 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1277 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1278 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1279 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1280 return 0;
1282 if (acpi_get_parent(handle, &phandle))
1283 return -ENODEV;
1285 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1286 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1287 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1288 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1289 return 0;
1291 return -ENODEV;
1295 * acpi_dock_match - see if a device has a _DCK method
1297 static int acpi_dock_match(struct acpi_device *device)
1299 acpi_handle tmp;
1300 return acpi_get_handle(device->handle, "_DCK", &tmp);
1303 const char *acpi_device_hid(struct acpi_device *device)
1305 struct acpi_hardware_id *hid;
1307 if (list_empty(&device->pnp.ids))
1308 return dummy_hid;
1310 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1311 return hid->id;
1313 EXPORT_SYMBOL(acpi_device_hid);
1315 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1317 struct acpi_hardware_id *id;
1319 id = kmalloc(sizeof(*id), GFP_KERNEL);
1320 if (!id)
1321 return;
1323 id->id = kstrdup(dev_id, GFP_KERNEL);
1324 if (!id->id) {
1325 kfree(id);
1326 return;
1329 list_add_tail(&id->list, &device->pnp.ids);
1333 * Old IBM workstations have a DSDT bug wherein the SMBus object
1334 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1335 * prefix. Work around this.
1337 static int acpi_ibm_smbus_match(struct acpi_device *device)
1339 acpi_handle h_dummy;
1340 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1341 int result;
1343 if (!dmi_name_in_vendors("IBM"))
1344 return -ENODEV;
1346 /* Look for SMBS object */
1347 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1348 if (result)
1349 return result;
1351 if (strcmp("SMBS", path.pointer)) {
1352 result = -ENODEV;
1353 goto out;
1356 /* Does it have the necessary (but misnamed) methods? */
1357 result = -ENODEV;
1358 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1359 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1360 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1361 result = 0;
1362 out:
1363 kfree(path.pointer);
1364 return result;
1367 static void acpi_device_set_id(struct acpi_device *device)
1369 acpi_status status;
1370 struct acpi_device_info *info;
1371 struct acpi_pnp_device_id_list *cid_list;
1372 int i;
1374 switch (device->device_type) {
1375 case ACPI_BUS_TYPE_DEVICE:
1376 if (ACPI_IS_ROOT_DEVICE(device)) {
1377 acpi_add_id(device, ACPI_SYSTEM_HID);
1378 break;
1381 status = acpi_get_object_info(device->handle, &info);
1382 if (ACPI_FAILURE(status)) {
1383 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1384 return;
1387 if (info->valid & ACPI_VALID_HID)
1388 acpi_add_id(device, info->hardware_id.string);
1389 if (info->valid & ACPI_VALID_CID) {
1390 cid_list = &info->compatible_id_list;
1391 for (i = 0; i < cid_list->count; i++)
1392 acpi_add_id(device, cid_list->ids[i].string);
1394 if (info->valid & ACPI_VALID_ADR) {
1395 device->pnp.bus_address = info->address;
1396 device->flags.bus_address = 1;
1398 if (info->valid & ACPI_VALID_UID)
1399 device->pnp.unique_id = kstrdup(info->unique_id.string,
1400 GFP_KERNEL);
1402 kfree(info);
1405 * Some devices don't reliably have _HIDs & _CIDs, so add
1406 * synthetic HIDs to make sure drivers can find them.
1408 if (acpi_is_video_device(device))
1409 acpi_add_id(device, ACPI_VIDEO_HID);
1410 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1411 acpi_add_id(device, ACPI_BAY_HID);
1412 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1413 acpi_add_id(device, ACPI_DOCK_HID);
1414 else if (!acpi_ibm_smbus_match(device))
1415 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1416 else if (list_empty(&device->pnp.ids) &&
1417 ACPI_IS_ROOT_DEVICE(device->parent)) {
1418 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1419 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1420 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1423 break;
1424 case ACPI_BUS_TYPE_POWER:
1425 acpi_add_id(device, ACPI_POWER_HID);
1426 break;
1427 case ACPI_BUS_TYPE_PROCESSOR:
1428 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1429 break;
1430 case ACPI_BUS_TYPE_THERMAL:
1431 acpi_add_id(device, ACPI_THERMAL_HID);
1432 break;
1433 case ACPI_BUS_TYPE_POWER_BUTTON:
1434 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1435 break;
1436 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1437 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1438 break;
1442 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1443 int type, unsigned long long sta)
1445 INIT_LIST_HEAD(&device->pnp.ids);
1446 device->device_type = type;
1447 device->handle = handle;
1448 device->parent = acpi_bus_get_parent(handle);
1449 STRUCT_TO_INT(device->status) = sta;
1450 acpi_device_get_busid(device);
1451 acpi_device_set_id(device);
1452 acpi_bus_get_flags(device);
1453 device->flags.match_driver = false;
1454 device_initialize(&device->dev);
1455 dev_set_uevent_suppress(&device->dev, true);
1458 void acpi_device_add_finalize(struct acpi_device *device)
1460 device->flags.match_driver = true;
1461 dev_set_uevent_suppress(&device->dev, false);
1462 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1465 static int acpi_add_single_object(struct acpi_device **child,
1466 acpi_handle handle, int type,
1467 unsigned long long sta)
1469 int result;
1470 struct acpi_device *device;
1471 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1473 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1474 if (!device) {
1475 printk(KERN_ERR PREFIX "Memory allocation error\n");
1476 return -ENOMEM;
1479 acpi_init_device_object(device, handle, type, sta);
1480 acpi_bus_get_power_flags(device);
1481 acpi_bus_get_wakeup_device_flags(device);
1483 result = acpi_device_add(device, acpi_device_release);
1484 if (result) {
1485 acpi_device_release(&device->dev);
1486 return result;
1489 acpi_power_add_remove_device(device, true);
1490 acpi_device_add_finalize(device);
1491 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1492 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1493 dev_name(&device->dev), (char *) buffer.pointer,
1494 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1495 kfree(buffer.pointer);
1496 *child = device;
1497 return 0;
1500 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1501 unsigned long long *sta)
1503 acpi_status status;
1504 acpi_object_type acpi_type;
1506 status = acpi_get_type(handle, &acpi_type);
1507 if (ACPI_FAILURE(status))
1508 return -ENODEV;
1510 switch (acpi_type) {
1511 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1512 case ACPI_TYPE_DEVICE:
1513 *type = ACPI_BUS_TYPE_DEVICE;
1514 status = acpi_bus_get_status_handle(handle, sta);
1515 if (ACPI_FAILURE(status))
1516 return -ENODEV;
1517 break;
1518 case ACPI_TYPE_PROCESSOR:
1519 *type = ACPI_BUS_TYPE_PROCESSOR;
1520 status = acpi_bus_get_status_handle(handle, sta);
1521 if (ACPI_FAILURE(status))
1522 return -ENODEV;
1523 break;
1524 case ACPI_TYPE_THERMAL:
1525 *type = ACPI_BUS_TYPE_THERMAL;
1526 *sta = ACPI_STA_DEFAULT;
1527 break;
1528 case ACPI_TYPE_POWER:
1529 *type = ACPI_BUS_TYPE_POWER;
1530 *sta = ACPI_STA_DEFAULT;
1531 break;
1532 default:
1533 return -ENODEV;
1536 return 0;
1539 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1540 void *not_used, void **return_value)
1542 struct acpi_device *device = NULL;
1543 int type;
1544 unsigned long long sta;
1545 acpi_status status;
1546 int result;
1548 acpi_bus_get_device(handle, &device);
1549 if (device)
1550 goto out;
1552 result = acpi_bus_type_and_status(handle, &type, &sta);
1553 if (result)
1554 return AE_OK;
1556 if (type == ACPI_BUS_TYPE_POWER) {
1557 acpi_add_power_resource(handle);
1558 return AE_OK;
1561 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1562 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1563 struct acpi_device_wakeup wakeup;
1564 acpi_handle temp;
1566 status = acpi_get_handle(handle, "_PRW", &temp);
1567 if (ACPI_SUCCESS(status)) {
1568 acpi_bus_extract_wakeup_device_power_package(handle,
1569 &wakeup);
1570 acpi_power_resources_list_free(&wakeup.resources);
1572 return AE_CTRL_DEPTH;
1575 acpi_add_single_object(&device, handle, type, sta);
1576 if (!device)
1577 return AE_CTRL_DEPTH;
1579 out:
1580 if (!*return_value)
1581 *return_value = device;
1583 return AE_OK;
1586 static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id)
1588 struct acpi_scan_handler *handler;
1590 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1591 const struct acpi_device_id *devid;
1593 for (devid = handler->ids; devid->id[0]; devid++) {
1594 int ret;
1596 if (strcmp((char *)devid->id, id))
1597 continue;
1599 ret = handler->attach(device, devid);
1600 if (ret > 0) {
1601 device->handler = handler;
1602 return ret;
1603 } else if (ret < 0) {
1604 return ret;
1608 return 0;
1611 static int acpi_scan_attach_handler(struct acpi_device *device)
1613 struct acpi_hardware_id *hwid;
1614 int ret = 0;
1616 list_for_each_entry(hwid, &device->pnp.ids, list) {
1617 ret = acpi_scan_do_attach_handler(device, hwid->id);
1618 if (ret)
1619 break;
1622 return ret;
1625 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1626 void *not_used, void **ret_not_used)
1628 struct acpi_device *device;
1629 unsigned long long sta_not_used;
1630 int ret;
1633 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1634 * namespace walks prematurely.
1636 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1637 return AE_OK;
1639 if (acpi_bus_get_device(handle, &device))
1640 return AE_CTRL_DEPTH;
1642 ret = acpi_scan_attach_handler(device);
1643 if (ret)
1644 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1646 ret = device_attach(&device->dev);
1647 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1651 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1652 * @handle: Root of the namespace scope to scan.
1654 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1655 * found devices.
1657 * If no devices were found, -ENODEV is returned, but it does not mean that
1658 * there has been a real error. There just have been no suitable ACPI objects
1659 * in the table trunk from which the kernel could create a device and add an
1660 * appropriate driver.
1662 * Must be called under acpi_scan_lock.
1664 int acpi_bus_scan(acpi_handle handle)
1666 void *device = NULL;
1667 int error = 0;
1669 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1670 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1671 acpi_bus_check_add, NULL, NULL, &device);
1673 if (!device)
1674 error = -ENODEV;
1675 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1676 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1677 acpi_bus_device_attach, NULL, NULL, NULL);
1679 return error;
1681 EXPORT_SYMBOL(acpi_bus_scan);
1683 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1684 void *not_used, void **ret_not_used)
1686 struct acpi_device *device = NULL;
1688 if (!acpi_bus_get_device(handle, &device)) {
1689 struct acpi_scan_handler *dev_handler = device->handler;
1691 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1692 if (dev_handler) {
1693 if (dev_handler->detach)
1694 dev_handler->detach(device);
1696 device->handler = NULL;
1697 } else {
1698 device_release_driver(&device->dev);
1701 return AE_OK;
1704 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1705 void *not_used, void **ret_not_used)
1707 struct acpi_device *device = NULL;
1709 if (!acpi_bus_get_device(handle, &device))
1710 acpi_device_unregister(device);
1712 return AE_OK;
1716 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1717 * @start: Root of the ACPI device nodes subtree to remove.
1719 * Must be called under acpi_scan_lock.
1721 void acpi_bus_trim(struct acpi_device *start)
1724 * Execute acpi_bus_device_detach() as a post-order callback to detach
1725 * all ACPI drivers from the device nodes being removed.
1727 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1728 acpi_bus_device_detach, NULL, NULL);
1729 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1731 * Execute acpi_bus_remove() as a post-order callback to remove device
1732 * nodes in the given namespace scope.
1734 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1735 acpi_bus_remove, NULL, NULL);
1736 acpi_bus_remove(start->handle, 0, NULL, NULL);
1738 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1740 static int acpi_bus_scan_fixed(void)
1742 int result = 0;
1745 * Enumerate all fixed-feature devices.
1747 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1748 struct acpi_device *device = NULL;
1750 result = acpi_add_single_object(&device, NULL,
1751 ACPI_BUS_TYPE_POWER_BUTTON,
1752 ACPI_STA_DEFAULT);
1753 if (result)
1754 return result;
1756 result = device_attach(&device->dev);
1757 if (result < 0)
1758 return result;
1760 device_init_wakeup(&device->dev, true);
1763 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1764 struct acpi_device *device = NULL;
1766 result = acpi_add_single_object(&device, NULL,
1767 ACPI_BUS_TYPE_SLEEP_BUTTON,
1768 ACPI_STA_DEFAULT);
1769 if (result)
1770 return result;
1772 result = device_attach(&device->dev);
1775 return result < 0 ? result : 0;
1778 int __init acpi_scan_init(void)
1780 int result;
1782 result = bus_register(&acpi_bus_type);
1783 if (result) {
1784 /* We don't want to quit even if we failed to add suspend/resume */
1785 printk(KERN_ERR PREFIX "Could not register bus type\n");
1788 acpi_pci_root_init();
1789 acpi_pci_link_init();
1790 acpi_platform_init();
1791 acpi_csrt_init();
1792 acpi_container_init();
1793 acpi_pci_slot_init();
1795 mutex_lock(&acpi_scan_lock);
1797 * Enumerate devices in the ACPI namespace.
1799 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1800 if (result)
1801 goto out;
1803 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1804 if (result)
1805 goto out;
1807 result = acpi_bus_scan_fixed();
1808 if (result) {
1809 acpi_device_unregister(acpi_root);
1810 goto out;
1813 acpi_update_all_gpes();
1815 acpi_pci_root_hp_init();
1817 out:
1818 mutex_unlock(&acpi_scan_lock);
1819 return result;