Linux 3.12.49
[linux/fpc-iii.git] / drivers / acpi / scan.c
blob332b126ee3c4c408a04d9e3e8da55aed2503dc0d
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)
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
34 bool acpi_force_hot_remove;
36 static const char *dummy_hid = "device";
38 static LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
44 struct acpi_device_bus_id{
45 char bus_id[15];
46 unsigned int instance_no;
47 struct list_head node;
50 void acpi_scan_lock_acquire(void)
52 mutex_lock(&acpi_scan_lock);
54 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56 void acpi_scan_lock_release(void)
58 mutex_unlock(&acpi_scan_lock);
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
64 if (!handler || !handler->attach)
65 return -EINVAL;
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 return 0;
71 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
74 int error;
76 error = acpi_scan_add_handler(handler);
77 if (error)
78 return error;
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 return 0;
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
89 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90 int size)
92 int len;
93 int count;
94 struct acpi_hardware_id *id;
96 if (list_empty(&acpi_dev->pnp.ids))
97 return 0;
99 len = snprintf(modalias, size, "acpi:");
100 size -= len;
102 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
103 count = snprintf(&modalias[len], size, "%s:", id->id);
104 if (count < 0 || count >= size)
105 return -EINVAL;
106 len += count;
107 size -= count;
110 modalias[len] = '\0';
111 return len;
114 static ssize_t
115 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
116 struct acpi_device *acpi_dev = to_acpi_device(dev);
117 int len;
119 /* Device has no HID and no CID or string is >1024 */
120 len = create_modalias(acpi_dev, buf, 1024);
121 if (len <= 0)
122 return 0;
123 buf[len++] = '\n';
124 return len;
126 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
128 static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129 void *data, void **ret_p)
131 struct acpi_device *device = NULL;
132 struct acpi_device_physical_node *pn;
133 bool second_pass = (bool)data;
134 acpi_status status = AE_OK;
136 if (acpi_bus_get_device(handle, &device))
137 return AE_OK;
139 mutex_lock(&device->physical_node_lock);
141 list_for_each_entry(pn, &device->physical_node_list, node) {
142 int ret;
144 if (second_pass) {
145 /* Skip devices offlined by the first pass. */
146 if (pn->put_online)
147 continue;
148 } else {
149 pn->put_online = false;
151 ret = device_offline(pn->dev);
152 if (acpi_force_hot_remove)
153 continue;
155 if (ret >= 0) {
156 pn->put_online = !ret;
157 } else {
158 *ret_p = pn->dev;
159 if (second_pass) {
160 status = AE_ERROR;
161 break;
166 mutex_unlock(&device->physical_node_lock);
168 return status;
171 static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172 void *data, void **ret_p)
174 struct acpi_device *device = NULL;
175 struct acpi_device_physical_node *pn;
177 if (acpi_bus_get_device(handle, &device))
178 return AE_OK;
180 mutex_lock(&device->physical_node_lock);
182 list_for_each_entry(pn, &device->physical_node_list, node)
183 if (pn->put_online) {
184 device_online(pn->dev);
185 pn->put_online = false;
188 mutex_unlock(&device->physical_node_lock);
190 return AE_OK;
193 static int acpi_scan_hot_remove(struct acpi_device *device)
195 acpi_handle handle = device->handle;
196 struct device *errdev;
197 acpi_status status;
198 unsigned long long sta;
200 /* If there is no handle, the device node has been unregistered. */
201 if (!handle) {
202 dev_dbg(&device->dev, "ACPI handle missing\n");
203 put_device(&device->dev);
204 return -EINVAL;
208 * Carry out two passes here and ignore errors in the first pass,
209 * because if the devices in question are memory blocks and
210 * CONFIG_MEMCG is set, one of the blocks may hold data structures
211 * that the other blocks depend on, but it is not known in advance which
212 * block holds them.
214 * If the first pass is successful, the second one isn't needed, though.
216 errdev = NULL;
217 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
218 NULL, acpi_bus_offline_companions,
219 (void *)false, (void **)&errdev);
220 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
221 if (errdev) {
222 errdev = NULL;
223 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
224 NULL, acpi_bus_offline_companions,
225 (void *)true , (void **)&errdev);
226 if (!errdev || acpi_force_hot_remove)
227 acpi_bus_offline_companions(handle, 0, (void *)true,
228 (void **)&errdev);
230 if (errdev && !acpi_force_hot_remove) {
231 dev_warn(errdev, "Offline failed.\n");
232 acpi_bus_online_companions(handle, 0, NULL, NULL);
233 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
234 ACPI_UINT32_MAX,
235 acpi_bus_online_companions, NULL,
236 NULL, NULL);
237 put_device(&device->dev);
238 return -EBUSY;
242 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
243 "Hot-removing device %s...\n", dev_name(&device->dev)));
245 acpi_bus_trim(device);
247 /* Device node has been unregistered. */
248 put_device(&device->dev);
249 device = NULL;
251 acpi_evaluate_lck(handle, 0);
253 * TBD: _EJD support.
255 status = acpi_evaluate_ej0(handle);
256 if (status == AE_NOT_FOUND)
257 return -ENODEV;
258 else if (ACPI_FAILURE(status))
259 return -EIO;
262 * Verify if eject was indeed successful. If not, log an error
263 * message. No need to call _OST since _EJ0 call was made OK.
265 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
266 if (ACPI_FAILURE(status)) {
267 acpi_handle_warn(handle,
268 "Status check after eject failed (0x%x)\n", status);
269 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
270 acpi_handle_warn(handle,
271 "Eject incomplete - status 0x%llx\n", sta);
274 return 0;
277 static void acpi_bus_device_eject(void *context)
279 acpi_handle handle = context;
280 struct acpi_device *device = NULL;
281 struct acpi_scan_handler *handler;
282 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
283 int error;
285 lock_device_hotplug();
286 mutex_lock(&acpi_scan_lock);
288 acpi_bus_get_device(handle, &device);
289 if (!device)
290 goto err_out;
292 handler = device->handler;
293 if (!handler || !handler->hotplug.enabled) {
294 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
295 goto err_out;
297 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
298 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
299 if (handler->hotplug.mode == AHM_CONTAINER)
300 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
302 get_device(&device->dev);
303 error = acpi_scan_hot_remove(device);
304 if (error)
305 goto err_out;
307 out:
308 mutex_unlock(&acpi_scan_lock);
309 unlock_device_hotplug();
310 return;
312 err_out:
313 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
314 NULL);
315 goto out;
318 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
320 struct acpi_device *device = NULL;
321 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
322 int error;
324 lock_device_hotplug();
325 mutex_lock(&acpi_scan_lock);
327 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
328 acpi_bus_get_device(handle, &device);
329 if (device) {
330 dev_warn(&device->dev, "Attempt to re-insert\n");
331 goto out;
334 error = acpi_bus_scan(handle);
335 if (error) {
336 acpi_handle_warn(handle, "Namespace scan failure\n");
337 goto out;
339 error = acpi_bus_get_device(handle, &device);
340 if (error) {
341 acpi_handle_warn(handle, "Missing device node object\n");
342 goto out;
344 ost_code = ACPI_OST_SC_SUCCESS;
345 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
346 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
348 out:
349 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
350 mutex_unlock(&acpi_scan_lock);
351 unlock_device_hotplug();
354 static void acpi_scan_bus_check(void *context)
356 acpi_scan_bus_device_check((acpi_handle)context,
357 ACPI_NOTIFY_BUS_CHECK);
360 static void acpi_scan_device_check(void *context)
362 acpi_scan_bus_device_check((acpi_handle)context,
363 ACPI_NOTIFY_DEVICE_CHECK);
366 static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
368 u32 ost_status;
370 switch (type) {
371 case ACPI_NOTIFY_BUS_CHECK:
372 acpi_handle_debug(handle,
373 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
374 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
375 break;
376 case ACPI_NOTIFY_DEVICE_CHECK:
377 acpi_handle_debug(handle,
378 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
379 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
380 break;
381 case ACPI_NOTIFY_EJECT_REQUEST:
382 acpi_handle_debug(handle,
383 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
384 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
385 break;
386 default:
387 /* non-hotplug event; possibly handled by other handler */
388 return;
391 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
394 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
396 acpi_osd_exec_callback callback;
397 struct acpi_scan_handler *handler = data;
398 acpi_status status;
400 if (!handler->hotplug.enabled)
401 return acpi_hotplug_unsupported(handle, type);
403 switch (type) {
404 case ACPI_NOTIFY_BUS_CHECK:
405 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
406 callback = acpi_scan_bus_check;
407 break;
408 case ACPI_NOTIFY_DEVICE_CHECK:
409 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
410 callback = acpi_scan_device_check;
411 break;
412 case ACPI_NOTIFY_EJECT_REQUEST:
413 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
414 callback = acpi_bus_device_eject;
415 break;
416 default:
417 /* non-hotplug event; possibly handled by other handler */
418 return;
420 status = acpi_os_hotplug_execute(callback, handle);
421 if (ACPI_FAILURE(status))
422 acpi_evaluate_hotplug_ost(handle, type,
423 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
424 NULL);
428 * acpi_bus_hot_remove_device: hot-remove a device and its children
429 * @context: struct acpi_eject_event pointer (freed in this func)
431 * Hot-remove a device and its children. This function frees up the
432 * memory space passed by arg context, so that the caller may call
433 * this function asynchronously through acpi_os_hotplug_execute().
435 void acpi_bus_hot_remove_device(void *context)
437 struct acpi_eject_event *ej_event = context;
438 struct acpi_device *device = ej_event->device;
439 acpi_handle handle = device->handle;
440 int error;
442 lock_device_hotplug();
443 mutex_lock(&acpi_scan_lock);
445 error = acpi_scan_hot_remove(device);
446 if (error && handle)
447 acpi_evaluate_hotplug_ost(handle, ej_event->event,
448 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
449 NULL);
451 mutex_unlock(&acpi_scan_lock);
452 unlock_device_hotplug();
453 kfree(context);
455 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
457 static ssize_t real_power_state_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 struct acpi_device *adev = to_acpi_device(dev);
461 int state;
462 int ret;
464 ret = acpi_device_get_power(adev, &state);
465 if (ret)
466 return ret;
468 return sprintf(buf, "%s\n", acpi_power_state_string(state));
471 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
473 static ssize_t power_state_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct acpi_device *adev = to_acpi_device(dev);
478 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
481 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
483 static ssize_t
484 acpi_eject_store(struct device *d, struct device_attribute *attr,
485 const char *buf, size_t count)
487 struct acpi_device *acpi_device = to_acpi_device(d);
488 struct acpi_eject_event *ej_event;
489 acpi_object_type not_used;
490 acpi_status status;
491 int ret;
493 if (!count || buf[0] != '1')
494 return -EINVAL;
496 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
497 && !acpi_device->driver)
498 return -ENODEV;
500 status = acpi_get_type(acpi_device->handle, &not_used);
501 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
502 return -ENODEV;
504 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
505 if (!ej_event) {
506 ret = -ENOMEM;
507 goto err_out;
509 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
510 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
511 ej_event->device = acpi_device;
512 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
513 get_device(&acpi_device->dev);
514 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
515 if (ACPI_SUCCESS(status))
516 return count;
518 put_device(&acpi_device->dev);
519 kfree(ej_event);
520 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
522 err_out:
523 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
524 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
525 return ret;
528 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
530 static ssize_t
531 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
532 struct acpi_device *acpi_dev = to_acpi_device(dev);
534 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
536 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
538 static ssize_t acpi_device_uid_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
541 struct acpi_device *acpi_dev = to_acpi_device(dev);
543 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
545 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
547 static ssize_t acpi_device_adr_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
550 struct acpi_device *acpi_dev = to_acpi_device(dev);
552 return sprintf(buf, "0x%08x\n",
553 (unsigned int)(acpi_dev->pnp.bus_address));
555 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
557 static ssize_t
558 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
559 struct acpi_device *acpi_dev = to_acpi_device(dev);
560 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
561 int result;
563 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
564 if (result)
565 goto end;
567 result = sprintf(buf, "%s\n", (char*)path.pointer);
568 kfree(path.pointer);
569 end:
570 return result;
572 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
574 /* sysfs file that shows description text from the ACPI _STR method */
575 static ssize_t description_show(struct device *dev,
576 struct device_attribute *attr,
577 char *buf) {
578 struct acpi_device *acpi_dev = to_acpi_device(dev);
579 int result;
581 if (acpi_dev->pnp.str_obj == NULL)
582 return 0;
585 * The _STR object contains a Unicode identifier for a device.
586 * We need to convert to utf-8 so it can be displayed.
588 result = utf16s_to_utf8s(
589 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
590 acpi_dev->pnp.str_obj->buffer.length,
591 UTF16_LITTLE_ENDIAN, buf,
592 PAGE_SIZE);
594 buf[result++] = '\n';
596 return result;
598 static DEVICE_ATTR(description, 0444, description_show, NULL);
600 static ssize_t
601 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
602 char *buf) {
603 struct acpi_device *acpi_dev = to_acpi_device(dev);
605 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
607 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
609 static int acpi_device_setup_files(struct acpi_device *dev)
611 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
612 acpi_status status;
613 unsigned long long sun;
614 int result = 0;
617 * Devices gotten from FADT don't have a "path" attribute
619 if (dev->handle) {
620 result = device_create_file(&dev->dev, &dev_attr_path);
621 if (result)
622 goto end;
625 if (!list_empty(&dev->pnp.ids)) {
626 result = device_create_file(&dev->dev, &dev_attr_hid);
627 if (result)
628 goto end;
630 result = device_create_file(&dev->dev, &dev_attr_modalias);
631 if (result)
632 goto end;
636 * If device has _STR, 'description' file is created
638 if (acpi_has_method(dev->handle, "_STR")) {
639 status = acpi_evaluate_object(dev->handle, "_STR",
640 NULL, &buffer);
641 if (ACPI_FAILURE(status))
642 buffer.pointer = NULL;
643 dev->pnp.str_obj = buffer.pointer;
644 result = device_create_file(&dev->dev, &dev_attr_description);
645 if (result)
646 goto end;
649 if (dev->pnp.type.bus_address)
650 result = device_create_file(&dev->dev, &dev_attr_adr);
651 if (dev->pnp.unique_id)
652 result = device_create_file(&dev->dev, &dev_attr_uid);
654 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
655 if (ACPI_SUCCESS(status)) {
656 dev->pnp.sun = (unsigned long)sun;
657 result = device_create_file(&dev->dev, &dev_attr_sun);
658 if (result)
659 goto end;
660 } else {
661 dev->pnp.sun = (unsigned long)-1;
665 * If device has _EJ0, 'eject' file is created that is used to trigger
666 * hot-removal function from userland.
668 if (acpi_has_method(dev->handle, "_EJ0")) {
669 result = device_create_file(&dev->dev, &dev_attr_eject);
670 if (result)
671 return result;
674 if (dev->flags.power_manageable) {
675 result = device_create_file(&dev->dev, &dev_attr_power_state);
676 if (result)
677 return result;
679 if (dev->power.flags.power_resources)
680 result = device_create_file(&dev->dev,
681 &dev_attr_real_power_state);
684 end:
685 return result;
688 static void acpi_device_remove_files(struct acpi_device *dev)
690 if (dev->flags.power_manageable) {
691 device_remove_file(&dev->dev, &dev_attr_power_state);
692 if (dev->power.flags.power_resources)
693 device_remove_file(&dev->dev,
694 &dev_attr_real_power_state);
698 * If device has _STR, remove 'description' file
700 if (acpi_has_method(dev->handle, "_STR")) {
701 kfree(dev->pnp.str_obj);
702 device_remove_file(&dev->dev, &dev_attr_description);
705 * If device has _EJ0, remove 'eject' file.
707 if (acpi_has_method(dev->handle, "_EJ0"))
708 device_remove_file(&dev->dev, &dev_attr_eject);
710 if (acpi_has_method(dev->handle, "_SUN"))
711 device_remove_file(&dev->dev, &dev_attr_sun);
713 if (dev->pnp.unique_id)
714 device_remove_file(&dev->dev, &dev_attr_uid);
715 if (dev->pnp.type.bus_address)
716 device_remove_file(&dev->dev, &dev_attr_adr);
717 device_remove_file(&dev->dev, &dev_attr_modalias);
718 device_remove_file(&dev->dev, &dev_attr_hid);
719 if (dev->handle)
720 device_remove_file(&dev->dev, &dev_attr_path);
722 /* --------------------------------------------------------------------------
723 ACPI Bus operations
724 -------------------------------------------------------------------------- */
726 static const struct acpi_device_id *__acpi_match_device(
727 struct acpi_device *device, const struct acpi_device_id *ids)
729 const struct acpi_device_id *id;
730 struct acpi_hardware_id *hwid;
733 * If the device is not present, it is unnecessary to load device
734 * driver for it.
736 if (!device->status.present)
737 return NULL;
739 for (id = ids; id->id[0]; id++)
740 list_for_each_entry(hwid, &device->pnp.ids, list)
741 if (!strcmp((char *) id->id, hwid->id))
742 return id;
744 return NULL;
748 * acpi_match_device - Match a struct device against a given list of ACPI IDs
749 * @ids: Array of struct acpi_device_id object to match against.
750 * @dev: The device structure to match.
752 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
753 * object for that handle and use that object to match against a given list of
754 * device IDs.
756 * Return a pointer to the first matching ID on success or %NULL on failure.
758 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
759 const struct device *dev)
761 struct acpi_device *adev;
762 acpi_handle handle = ACPI_HANDLE(dev);
764 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
765 return NULL;
767 return __acpi_match_device(adev, ids);
769 EXPORT_SYMBOL_GPL(acpi_match_device);
771 int acpi_match_device_ids(struct acpi_device *device,
772 const struct acpi_device_id *ids)
774 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
776 EXPORT_SYMBOL(acpi_match_device_ids);
778 static void acpi_free_power_resources_lists(struct acpi_device *device)
780 int i;
782 if (device->wakeup.flags.valid)
783 acpi_power_resources_list_free(&device->wakeup.resources);
785 if (!device->flags.power_manageable)
786 return;
788 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
789 struct acpi_device_power_state *ps = &device->power.states[i];
790 acpi_power_resources_list_free(&ps->resources);
794 static void acpi_device_release(struct device *dev)
796 struct acpi_device *acpi_dev = to_acpi_device(dev);
798 acpi_free_pnp_ids(&acpi_dev->pnp);
799 acpi_free_power_resources_lists(acpi_dev);
800 kfree(acpi_dev);
803 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
805 struct acpi_device *acpi_dev = to_acpi_device(dev);
806 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
808 return acpi_dev->flags.match_driver
809 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
812 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
814 struct acpi_device *acpi_dev = to_acpi_device(dev);
815 int len;
817 if (list_empty(&acpi_dev->pnp.ids))
818 return 0;
820 if (add_uevent_var(env, "MODALIAS="))
821 return -ENOMEM;
822 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
823 sizeof(env->buf) - env->buflen);
824 if (len >= (sizeof(env->buf) - env->buflen))
825 return -ENOMEM;
826 env->buflen += len;
827 return 0;
830 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
832 struct acpi_device *device = data;
834 device->driver->ops.notify(device, event);
837 static void acpi_device_notify_fixed(void *data)
839 struct acpi_device *device = data;
841 /* Fixed hardware devices have no handles */
842 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
845 static acpi_status acpi_device_fixed_event(void *data)
847 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
848 return AE_OK;
851 static int acpi_device_install_notify_handler(struct acpi_device *device)
853 acpi_status status;
855 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
856 status =
857 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
858 acpi_device_fixed_event,
859 device);
860 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
861 status =
862 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
863 acpi_device_fixed_event,
864 device);
865 else
866 status = acpi_install_notify_handler(device->handle,
867 ACPI_DEVICE_NOTIFY,
868 acpi_device_notify,
869 device);
871 if (ACPI_FAILURE(status))
872 return -EINVAL;
873 return 0;
876 static void acpi_device_remove_notify_handler(struct acpi_device *device)
878 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
879 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
880 acpi_device_fixed_event);
881 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
882 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
883 acpi_device_fixed_event);
884 else
885 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
886 acpi_device_notify);
889 static int acpi_device_probe(struct device *dev)
891 struct acpi_device *acpi_dev = to_acpi_device(dev);
892 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
893 int ret;
895 if (acpi_dev->handler)
896 return -EINVAL;
898 if (!acpi_drv->ops.add)
899 return -ENOSYS;
901 ret = acpi_drv->ops.add(acpi_dev);
902 if (ret)
903 return ret;
905 acpi_dev->driver = acpi_drv;
906 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
907 "Driver [%s] successfully bound to device [%s]\n",
908 acpi_drv->name, acpi_dev->pnp.bus_id));
910 if (acpi_drv->ops.notify) {
911 ret = acpi_device_install_notify_handler(acpi_dev);
912 if (ret) {
913 if (acpi_drv->ops.remove)
914 acpi_drv->ops.remove(acpi_dev);
916 acpi_dev->driver = NULL;
917 acpi_dev->driver_data = NULL;
918 return ret;
922 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
923 acpi_drv->name, acpi_dev->pnp.bus_id));
924 get_device(dev);
925 return 0;
928 static int acpi_device_remove(struct device * dev)
930 struct acpi_device *acpi_dev = to_acpi_device(dev);
931 struct acpi_driver *acpi_drv = acpi_dev->driver;
933 if (acpi_drv) {
934 if (acpi_drv->ops.notify)
935 acpi_device_remove_notify_handler(acpi_dev);
936 if (acpi_drv->ops.remove)
937 acpi_drv->ops.remove(acpi_dev);
939 acpi_dev->driver = NULL;
940 acpi_dev->driver_data = NULL;
942 put_device(dev);
943 return 0;
946 struct bus_type acpi_bus_type = {
947 .name = "acpi",
948 .match = acpi_bus_match,
949 .probe = acpi_device_probe,
950 .remove = acpi_device_remove,
951 .uevent = acpi_device_uevent,
954 static void acpi_bus_data_handler(acpi_handle handle, void *context)
956 /* Intentionally empty. */
959 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
961 acpi_status status;
963 if (!device)
964 return -EINVAL;
966 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
967 if (ACPI_FAILURE(status) || !*device) {
968 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
969 handle));
970 return -ENODEV;
972 return 0;
974 EXPORT_SYMBOL(acpi_bus_get_device);
976 int acpi_device_add(struct acpi_device *device,
977 void (*release)(struct device *))
979 int result;
980 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
981 int found = 0;
983 if (device->handle) {
984 acpi_status status;
986 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
987 device);
988 if (ACPI_FAILURE(status)) {
989 acpi_handle_err(device->handle,
990 "Unable to attach device data\n");
991 return -ENODEV;
996 * Linkage
997 * -------
998 * Link this device to its parent and siblings.
1000 INIT_LIST_HEAD(&device->children);
1001 INIT_LIST_HEAD(&device->node);
1002 INIT_LIST_HEAD(&device->wakeup_list);
1003 INIT_LIST_HEAD(&device->physical_node_list);
1004 mutex_init(&device->physical_node_lock);
1006 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1007 if (!new_bus_id) {
1008 pr_err(PREFIX "Memory allocation error\n");
1009 result = -ENOMEM;
1010 goto err_detach;
1013 mutex_lock(&acpi_device_lock);
1015 * Find suitable bus_id and instance number in acpi_bus_id_list
1016 * If failed, create one and link it into acpi_bus_id_list
1018 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1019 if (!strcmp(acpi_device_bus_id->bus_id,
1020 acpi_device_hid(device))) {
1021 acpi_device_bus_id->instance_no++;
1022 found = 1;
1023 kfree(new_bus_id);
1024 break;
1027 if (!found) {
1028 acpi_device_bus_id = new_bus_id;
1029 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1030 acpi_device_bus_id->instance_no = 0;
1031 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1033 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1035 if (device->parent)
1036 list_add_tail(&device->node, &device->parent->children);
1038 if (device->wakeup.flags.valid)
1039 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1040 mutex_unlock(&acpi_device_lock);
1042 if (device->parent)
1043 device->dev.parent = &device->parent->dev;
1044 device->dev.bus = &acpi_bus_type;
1045 device->dev.release = release;
1046 result = device_add(&device->dev);
1047 if (result) {
1048 dev_err(&device->dev, "Error registering device\n");
1049 goto err;
1052 result = acpi_device_setup_files(device);
1053 if (result)
1054 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1055 dev_name(&device->dev));
1057 return 0;
1059 err:
1060 mutex_lock(&acpi_device_lock);
1061 if (device->parent)
1062 list_del(&device->node);
1063 list_del(&device->wakeup_list);
1064 mutex_unlock(&acpi_device_lock);
1066 err_detach:
1067 acpi_detach_data(device->handle, acpi_bus_data_handler);
1068 return result;
1071 static void acpi_device_unregister(struct acpi_device *device)
1073 mutex_lock(&acpi_device_lock);
1074 if (device->parent)
1075 list_del(&device->node);
1077 list_del(&device->wakeup_list);
1078 mutex_unlock(&acpi_device_lock);
1080 acpi_detach_data(device->handle, acpi_bus_data_handler);
1082 acpi_power_add_remove_device(device, false);
1083 acpi_device_remove_files(device);
1084 if (device->remove)
1085 device->remove(device);
1087 device_del(&device->dev);
1089 * Transition the device to D3cold to drop the reference counts of all
1090 * power resources the device depends on and turn off the ones that have
1091 * no more references.
1093 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
1094 device->handle = NULL;
1095 put_device(&device->dev);
1098 /* --------------------------------------------------------------------------
1099 Driver Management
1100 -------------------------------------------------------------------------- */
1102 * acpi_bus_register_driver - register a driver with the ACPI bus
1103 * @driver: driver being registered
1105 * Registers a driver with the ACPI bus. Searches the namespace for all
1106 * devices that match the driver's criteria and binds. Returns zero for
1107 * success or a negative error status for failure.
1109 int acpi_bus_register_driver(struct acpi_driver *driver)
1111 int ret;
1113 if (acpi_disabled)
1114 return -ENODEV;
1115 driver->drv.name = driver->name;
1116 driver->drv.bus = &acpi_bus_type;
1117 driver->drv.owner = driver->owner;
1119 ret = driver_register(&driver->drv);
1120 return ret;
1123 EXPORT_SYMBOL(acpi_bus_register_driver);
1126 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1127 * @driver: driver to unregister
1129 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1130 * devices that match the driver's criteria and unbinds.
1132 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1134 driver_unregister(&driver->drv);
1137 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1139 /* --------------------------------------------------------------------------
1140 Device Enumeration
1141 -------------------------------------------------------------------------- */
1142 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1144 struct acpi_device *device = NULL;
1145 acpi_status status;
1148 * Fixed hardware devices do not appear in the namespace and do not
1149 * have handles, but we fabricate acpi_devices for them, so we have
1150 * to deal with them specially.
1152 if (!handle)
1153 return acpi_root;
1155 do {
1156 status = acpi_get_parent(handle, &handle);
1157 if (ACPI_FAILURE(status))
1158 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1159 } while (acpi_bus_get_device(handle, &device));
1160 return device;
1163 acpi_status
1164 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1166 acpi_status status;
1167 acpi_handle tmp;
1168 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1169 union acpi_object *obj;
1171 status = acpi_get_handle(handle, "_EJD", &tmp);
1172 if (ACPI_FAILURE(status))
1173 return status;
1175 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1176 if (ACPI_SUCCESS(status)) {
1177 obj = buffer.pointer;
1178 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1179 ejd);
1180 kfree(buffer.pointer);
1182 return status;
1184 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1186 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1187 struct acpi_device_wakeup *wakeup)
1189 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1190 union acpi_object *package = NULL;
1191 union acpi_object *element = NULL;
1192 acpi_status status;
1193 int err = -ENODATA;
1195 if (!wakeup)
1196 return -EINVAL;
1198 INIT_LIST_HEAD(&wakeup->resources);
1200 /* _PRW */
1201 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1202 if (ACPI_FAILURE(status)) {
1203 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1204 return err;
1207 package = (union acpi_object *)buffer.pointer;
1209 if (!package || package->package.count < 2)
1210 goto out;
1212 element = &(package->package.elements[0]);
1213 if (!element)
1214 goto out;
1216 if (element->type == ACPI_TYPE_PACKAGE) {
1217 if ((element->package.count < 2) ||
1218 (element->package.elements[0].type !=
1219 ACPI_TYPE_LOCAL_REFERENCE)
1220 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1221 goto out;
1223 wakeup->gpe_device =
1224 element->package.elements[0].reference.handle;
1225 wakeup->gpe_number =
1226 (u32) element->package.elements[1].integer.value;
1227 } else if (element->type == ACPI_TYPE_INTEGER) {
1228 wakeup->gpe_device = NULL;
1229 wakeup->gpe_number = element->integer.value;
1230 } else {
1231 goto out;
1234 element = &(package->package.elements[1]);
1235 if (element->type != ACPI_TYPE_INTEGER)
1236 goto out;
1238 wakeup->sleep_state = element->integer.value;
1240 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1241 if (err)
1242 goto out;
1244 if (!list_empty(&wakeup->resources)) {
1245 int sleep_state;
1247 err = acpi_power_wakeup_list_init(&wakeup->resources,
1248 &sleep_state);
1249 if (err) {
1250 acpi_handle_warn(handle, "Retrieving current states "
1251 "of wakeup power resources failed\n");
1252 acpi_power_resources_list_free(&wakeup->resources);
1253 goto out;
1255 if (sleep_state < wakeup->sleep_state) {
1256 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1257 "(S%d) by S%d from power resources\n",
1258 (int)wakeup->sleep_state, sleep_state);
1259 wakeup->sleep_state = sleep_state;
1262 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1264 out:
1265 kfree(buffer.pointer);
1266 return err;
1269 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1271 struct acpi_device_id button_device_ids[] = {
1272 {"PNP0C0C", 0},
1273 {"PNP0C0D", 0},
1274 {"PNP0C0E", 0},
1275 {"", 0},
1277 acpi_status status;
1278 acpi_event_status event_status;
1280 device->wakeup.flags.notifier_present = 0;
1282 /* Power button, Lid switch always enable wakeup */
1283 if (!acpi_match_device_ids(device, button_device_ids)) {
1284 device->wakeup.flags.run_wake = 1;
1285 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1286 /* Do not use Lid/sleep button for S5 wakeup */
1287 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1288 device->wakeup.sleep_state = ACPI_STATE_S4;
1290 device_set_wakeup_capable(&device->dev, true);
1291 return;
1294 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1295 device->wakeup.gpe_number,
1296 &event_status);
1297 if (status == AE_OK)
1298 device->wakeup.flags.run_wake =
1299 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1302 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1304 int err;
1306 /* Presence of _PRW indicates wake capable */
1307 if (!acpi_has_method(device->handle, "_PRW"))
1308 return;
1310 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1311 &device->wakeup);
1312 if (err) {
1313 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1314 return;
1317 device->wakeup.flags.valid = 1;
1318 device->wakeup.prepare_count = 0;
1319 acpi_bus_set_run_wake_flags(device);
1320 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1321 * system for the ACPI device with the _PRW object.
1322 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1323 * So it is necessary to call _DSW object first. Only when it is not
1324 * present will the _PSW object used.
1326 err = acpi_device_sleep_wake(device, 0, 0, 0);
1327 if (err)
1328 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1329 "error in _DSW or _PSW evaluation\n"));
1332 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1334 struct acpi_device_power_state *ps = &device->power.states[state];
1335 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1336 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1337 acpi_status status;
1339 INIT_LIST_HEAD(&ps->resources);
1341 /* Evaluate "_PRx" to get referenced power resources */
1342 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1343 if (ACPI_SUCCESS(status)) {
1344 union acpi_object *package = buffer.pointer;
1346 if (buffer.length && package
1347 && package->type == ACPI_TYPE_PACKAGE
1348 && package->package.count) {
1349 int err = acpi_extract_power_resources(package, 0,
1350 &ps->resources);
1351 if (!err)
1352 device->power.flags.power_resources = 1;
1354 ACPI_FREE(buffer.pointer);
1357 /* Evaluate "_PSx" to see if we can do explicit sets */
1358 pathname[2] = 'S';
1359 if (acpi_has_method(device->handle, pathname))
1360 ps->flags.explicit_set = 1;
1363 * State is valid if there are means to put the device into it.
1364 * D3hot is only valid if _PR3 present.
1366 if (!list_empty(&ps->resources)
1367 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1368 ps->flags.valid = 1;
1369 ps->flags.os_accessible = 1;
1372 ps->power = -1; /* Unknown - driver assigned */
1373 ps->latency = -1; /* Unknown - driver assigned */
1376 static void acpi_bus_get_power_flags(struct acpi_device *device)
1378 u32 i;
1380 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1381 if (!acpi_has_method(device->handle, "_PS0") &&
1382 !acpi_has_method(device->handle, "_PR0"))
1383 return;
1385 device->flags.power_manageable = 1;
1388 * Power Management Flags
1390 if (acpi_has_method(device->handle, "_PSC"))
1391 device->power.flags.explicit_get = 1;
1392 if (acpi_has_method(device->handle, "_IRC"))
1393 device->power.flags.inrush_current = 1;
1396 * Enumerate supported power management states
1398 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1399 acpi_bus_init_power_state(device, i);
1401 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1403 /* Set defaults for D0 and D3 states (always valid) */
1404 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1405 device->power.states[ACPI_STATE_D0].power = 100;
1406 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1407 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1409 /* Set D3cold's explicit_set flag if _PS3 exists. */
1410 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1411 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1413 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1414 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1415 device->power.flags.power_resources)
1416 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1418 if (acpi_bus_init_power(device)) {
1419 acpi_free_power_resources_lists(device);
1420 device->flags.power_manageable = 0;
1424 static void acpi_bus_get_flags(struct acpi_device *device)
1426 /* Presence of _STA indicates 'dynamic_status' */
1427 if (acpi_has_method(device->handle, "_STA"))
1428 device->flags.dynamic_status = 1;
1430 /* Presence of _RMV indicates 'removable' */
1431 if (acpi_has_method(device->handle, "_RMV"))
1432 device->flags.removable = 1;
1434 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1435 if (acpi_has_method(device->handle, "_EJD") ||
1436 acpi_has_method(device->handle, "_EJ0"))
1437 device->flags.ejectable = 1;
1440 static void acpi_device_get_busid(struct acpi_device *device)
1442 char bus_id[5] = { '?', 0 };
1443 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1444 int i = 0;
1447 * Bus ID
1448 * ------
1449 * The device's Bus ID is simply the object name.
1450 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1452 if (ACPI_IS_ROOT_DEVICE(device)) {
1453 strcpy(device->pnp.bus_id, "ACPI");
1454 return;
1457 switch (device->device_type) {
1458 case ACPI_BUS_TYPE_POWER_BUTTON:
1459 strcpy(device->pnp.bus_id, "PWRF");
1460 break;
1461 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1462 strcpy(device->pnp.bus_id, "SLPF");
1463 break;
1464 default:
1465 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1466 /* Clean up trailing underscores (if any) */
1467 for (i = 3; i > 1; i--) {
1468 if (bus_id[i] == '_')
1469 bus_id[i] = '\0';
1470 else
1471 break;
1473 strcpy(device->pnp.bus_id, bus_id);
1474 break;
1479 * acpi_ata_match - see if an acpi object is an ATA device
1481 * If an acpi object has one of the ACPI ATA methods defined,
1482 * then we can safely call it an ATA device.
1484 bool acpi_ata_match(acpi_handle handle)
1486 return acpi_has_method(handle, "_GTF") ||
1487 acpi_has_method(handle, "_GTM") ||
1488 acpi_has_method(handle, "_STM") ||
1489 acpi_has_method(handle, "_SDD");
1493 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1495 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1496 * then we can safely call it an ejectable drive bay
1498 bool acpi_bay_match(acpi_handle handle)
1500 acpi_handle phandle;
1502 if (!acpi_has_method(handle, "_EJ0"))
1503 return false;
1504 if (acpi_ata_match(handle))
1505 return true;
1506 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1507 return false;
1509 return acpi_ata_match(phandle);
1513 * acpi_dock_match - see if an acpi object has a _DCK method
1515 bool acpi_dock_match(acpi_handle handle)
1517 return acpi_has_method(handle, "_DCK");
1520 const char *acpi_device_hid(struct acpi_device *device)
1522 struct acpi_hardware_id *hid;
1524 if (list_empty(&device->pnp.ids))
1525 return dummy_hid;
1527 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1528 return hid->id;
1530 EXPORT_SYMBOL(acpi_device_hid);
1532 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1534 struct acpi_hardware_id *id;
1536 id = kmalloc(sizeof(*id), GFP_KERNEL);
1537 if (!id)
1538 return;
1540 id->id = kstrdup(dev_id, GFP_KERNEL);
1541 if (!id->id) {
1542 kfree(id);
1543 return;
1546 list_add_tail(&id->list, &pnp->ids);
1547 pnp->type.hardware_id = 1;
1551 * Old IBM workstations have a DSDT bug wherein the SMBus object
1552 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1553 * prefix. Work around this.
1555 static bool acpi_ibm_smbus_match(acpi_handle handle)
1557 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1558 struct acpi_buffer path = { sizeof(node_name), node_name };
1560 if (!dmi_name_in_vendors("IBM"))
1561 return false;
1563 /* Look for SMBS object */
1564 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1565 strcmp("SMBS", path.pointer))
1566 return false;
1568 /* Does it have the necessary (but misnamed) methods? */
1569 if (acpi_has_method(handle, "SBI") &&
1570 acpi_has_method(handle, "SBR") &&
1571 acpi_has_method(handle, "SBW"))
1572 return true;
1574 return false;
1577 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1578 int device_type)
1580 acpi_status status;
1581 struct acpi_device_info *info;
1582 struct acpi_pnp_device_id_list *cid_list;
1583 int i;
1585 switch (device_type) {
1586 case ACPI_BUS_TYPE_DEVICE:
1587 if (handle == ACPI_ROOT_OBJECT) {
1588 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1589 break;
1592 status = acpi_get_object_info(handle, &info);
1593 if (ACPI_FAILURE(status)) {
1594 pr_err(PREFIX "%s: Error reading device info\n",
1595 __func__);
1596 return;
1599 if (info->valid & ACPI_VALID_HID)
1600 acpi_add_id(pnp, info->hardware_id.string);
1601 if (info->valid & ACPI_VALID_CID) {
1602 cid_list = &info->compatible_id_list;
1603 for (i = 0; i < cid_list->count; i++)
1604 acpi_add_id(pnp, cid_list->ids[i].string);
1606 if (info->valid & ACPI_VALID_ADR) {
1607 pnp->bus_address = info->address;
1608 pnp->type.bus_address = 1;
1610 if (info->valid & ACPI_VALID_UID)
1611 pnp->unique_id = kstrdup(info->unique_id.string,
1612 GFP_KERNEL);
1614 kfree(info);
1617 * Some devices don't reliably have _HIDs & _CIDs, so add
1618 * synthetic HIDs to make sure drivers can find them.
1620 if (acpi_is_video_device(handle))
1621 acpi_add_id(pnp, ACPI_VIDEO_HID);
1622 else if (acpi_bay_match(handle))
1623 acpi_add_id(pnp, ACPI_BAY_HID);
1624 else if (acpi_dock_match(handle))
1625 acpi_add_id(pnp, ACPI_DOCK_HID);
1626 else if (acpi_ibm_smbus_match(handle))
1627 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1628 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1629 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1630 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1631 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1634 break;
1635 case ACPI_BUS_TYPE_POWER:
1636 acpi_add_id(pnp, ACPI_POWER_HID);
1637 break;
1638 case ACPI_BUS_TYPE_PROCESSOR:
1639 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1640 break;
1641 case ACPI_BUS_TYPE_THERMAL:
1642 acpi_add_id(pnp, ACPI_THERMAL_HID);
1643 break;
1644 case ACPI_BUS_TYPE_POWER_BUTTON:
1645 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1646 break;
1647 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1648 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1649 break;
1653 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1655 struct acpi_hardware_id *id, *tmp;
1657 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1658 kfree(id->id);
1659 kfree(id);
1661 kfree(pnp->unique_id);
1664 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1665 int type, unsigned long long sta)
1667 INIT_LIST_HEAD(&device->pnp.ids);
1668 device->device_type = type;
1669 device->handle = handle;
1670 device->parent = acpi_bus_get_parent(handle);
1671 STRUCT_TO_INT(device->status) = sta;
1672 acpi_device_get_busid(device);
1673 acpi_set_pnp_ids(handle, &device->pnp, type);
1674 acpi_bus_get_flags(device);
1675 device->flags.match_driver = false;
1676 device_initialize(&device->dev);
1677 dev_set_uevent_suppress(&device->dev, true);
1680 void acpi_device_add_finalize(struct acpi_device *device)
1682 device->flags.match_driver = true;
1683 dev_set_uevent_suppress(&device->dev, false);
1684 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1687 static int acpi_add_single_object(struct acpi_device **child,
1688 acpi_handle handle, int type,
1689 unsigned long long sta)
1691 int result;
1692 struct acpi_device *device;
1693 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1695 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1696 if (!device) {
1697 printk(KERN_ERR PREFIX "Memory allocation error\n");
1698 return -ENOMEM;
1701 acpi_init_device_object(device, handle, type, sta);
1702 acpi_bus_get_power_flags(device);
1703 acpi_bus_get_wakeup_device_flags(device);
1705 result = acpi_device_add(device, acpi_device_release);
1706 if (result) {
1707 acpi_device_release(&device->dev);
1708 return result;
1711 acpi_power_add_remove_device(device, true);
1712 acpi_device_add_finalize(device);
1713 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1714 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1715 dev_name(&device->dev), (char *) buffer.pointer,
1716 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1717 kfree(buffer.pointer);
1718 *child = device;
1719 return 0;
1722 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1723 unsigned long long *sta)
1725 acpi_status status;
1726 acpi_object_type acpi_type;
1728 status = acpi_get_type(handle, &acpi_type);
1729 if (ACPI_FAILURE(status))
1730 return -ENODEV;
1732 switch (acpi_type) {
1733 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1734 case ACPI_TYPE_DEVICE:
1735 *type = ACPI_BUS_TYPE_DEVICE;
1736 status = acpi_bus_get_status_handle(handle, sta);
1737 if (ACPI_FAILURE(status))
1738 return -ENODEV;
1739 break;
1740 case ACPI_TYPE_PROCESSOR:
1741 *type = ACPI_BUS_TYPE_PROCESSOR;
1742 status = acpi_bus_get_status_handle(handle, sta);
1743 if (ACPI_FAILURE(status))
1744 return -ENODEV;
1745 break;
1746 case ACPI_TYPE_THERMAL:
1747 *type = ACPI_BUS_TYPE_THERMAL;
1748 *sta = ACPI_STA_DEFAULT;
1749 break;
1750 case ACPI_TYPE_POWER:
1751 *type = ACPI_BUS_TYPE_POWER;
1752 *sta = ACPI_STA_DEFAULT;
1753 break;
1754 default:
1755 return -ENODEV;
1758 return 0;
1761 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1762 char *idstr,
1763 const struct acpi_device_id **matchid)
1765 const struct acpi_device_id *devid;
1767 for (devid = handler->ids; devid->id[0]; devid++)
1768 if (!strcmp((char *)devid->id, idstr)) {
1769 if (matchid)
1770 *matchid = devid;
1772 return true;
1775 return false;
1778 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1779 const struct acpi_device_id **matchid)
1781 struct acpi_scan_handler *handler;
1783 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1784 if (acpi_scan_handler_matching(handler, idstr, matchid))
1785 return handler;
1787 return NULL;
1790 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1792 if (!!hotplug->enabled == !!val)
1793 return;
1795 mutex_lock(&acpi_scan_lock);
1797 hotplug->enabled = val;
1799 mutex_unlock(&acpi_scan_lock);
1802 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1804 struct acpi_device_pnp pnp = {};
1805 struct acpi_hardware_id *hwid;
1806 struct acpi_scan_handler *handler;
1808 INIT_LIST_HEAD(&pnp.ids);
1809 acpi_set_pnp_ids(handle, &pnp, type);
1811 if (!pnp.type.hardware_id)
1812 goto out;
1815 * This relies on the fact that acpi_install_notify_handler() will not
1816 * install the same notify handler routine twice for the same handle.
1818 list_for_each_entry(hwid, &pnp.ids, list) {
1819 handler = acpi_scan_match_handler(hwid->id, NULL);
1820 if (handler && !handler->hotplug.ignore) {
1821 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1822 acpi_hotplug_notify_cb, handler);
1823 break;
1827 out:
1828 acpi_free_pnp_ids(&pnp);
1831 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1832 void *not_used, void **return_value)
1834 struct acpi_device *device = NULL;
1835 int type;
1836 unsigned long long sta;
1837 int result;
1839 acpi_bus_get_device(handle, &device);
1840 if (device)
1841 goto out;
1843 result = acpi_bus_type_and_status(handle, &type, &sta);
1844 if (result)
1845 return AE_OK;
1847 if (type == ACPI_BUS_TYPE_POWER) {
1848 acpi_add_power_resource(handle);
1849 return AE_OK;
1852 acpi_scan_init_hotplug(handle, type);
1854 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1855 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1856 struct acpi_device_wakeup wakeup;
1858 if (acpi_has_method(handle, "_PRW")) {
1859 acpi_bus_extract_wakeup_device_power_package(handle,
1860 &wakeup);
1861 acpi_power_resources_list_free(&wakeup.resources);
1863 return AE_CTRL_DEPTH;
1866 acpi_add_single_object(&device, handle, type, sta);
1867 if (!device)
1868 return AE_CTRL_DEPTH;
1870 out:
1871 if (!*return_value)
1872 *return_value = device;
1874 return AE_OK;
1877 static int acpi_scan_attach_handler(struct acpi_device *device)
1879 struct acpi_hardware_id *hwid;
1880 int ret = 0;
1882 list_for_each_entry(hwid, &device->pnp.ids, list) {
1883 const struct acpi_device_id *devid;
1884 struct acpi_scan_handler *handler;
1886 handler = acpi_scan_match_handler(hwid->id, &devid);
1887 if (handler) {
1888 ret = handler->attach(device, devid);
1889 if (ret > 0) {
1890 device->handler = handler;
1891 break;
1892 } else if (ret < 0) {
1893 break;
1897 return ret;
1900 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1901 void *not_used, void **ret_not_used)
1903 struct acpi_device *device;
1904 unsigned long long sta_not_used;
1905 int ret;
1908 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1909 * namespace walks prematurely.
1911 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1912 return AE_OK;
1914 if (acpi_bus_get_device(handle, &device))
1915 return AE_CTRL_DEPTH;
1917 if (device->handler)
1918 return AE_OK;
1920 ret = acpi_scan_attach_handler(device);
1921 if (ret)
1922 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1924 ret = device_attach(&device->dev);
1925 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1929 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1930 * @handle: Root of the namespace scope to scan.
1932 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1933 * found devices.
1935 * If no devices were found, -ENODEV is returned, but it does not mean that
1936 * there has been a real error. There just have been no suitable ACPI objects
1937 * in the table trunk from which the kernel could create a device and add an
1938 * appropriate driver.
1940 * Must be called under acpi_scan_lock.
1942 int acpi_bus_scan(acpi_handle handle)
1944 void *device = NULL;
1945 int error = 0;
1947 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1948 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1949 acpi_bus_check_add, NULL, NULL, &device);
1951 if (!device)
1952 error = -ENODEV;
1953 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1954 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1955 acpi_bus_device_attach, NULL, NULL, NULL);
1957 return error;
1959 EXPORT_SYMBOL(acpi_bus_scan);
1961 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1962 void *not_used, void **ret_not_used)
1964 struct acpi_device *device = NULL;
1966 if (!acpi_bus_get_device(handle, &device)) {
1967 struct acpi_scan_handler *dev_handler = device->handler;
1969 if (dev_handler) {
1970 if (dev_handler->detach)
1971 dev_handler->detach(device);
1973 device->handler = NULL;
1974 } else {
1975 device_release_driver(&device->dev);
1978 return AE_OK;
1981 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1982 void *not_used, void **ret_not_used)
1984 struct acpi_device *device = NULL;
1986 if (!acpi_bus_get_device(handle, &device))
1987 acpi_device_unregister(device);
1989 return AE_OK;
1993 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1994 * @start: Root of the ACPI device nodes subtree to remove.
1996 * Must be called under acpi_scan_lock.
1998 void acpi_bus_trim(struct acpi_device *start)
2001 * Execute acpi_bus_device_detach() as a post-order callback to detach
2002 * all ACPI drivers from the device nodes being removed.
2004 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2005 acpi_bus_device_detach, NULL, NULL);
2006 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2008 * Execute acpi_bus_remove() as a post-order callback to remove device
2009 * nodes in the given namespace scope.
2011 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2012 acpi_bus_remove, NULL, NULL);
2013 acpi_bus_remove(start->handle, 0, NULL, NULL);
2015 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2017 static int acpi_bus_scan_fixed(void)
2019 int result = 0;
2022 * Enumerate all fixed-feature devices.
2024 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2025 struct acpi_device *device = NULL;
2027 result = acpi_add_single_object(&device, NULL,
2028 ACPI_BUS_TYPE_POWER_BUTTON,
2029 ACPI_STA_DEFAULT);
2030 if (result)
2031 return result;
2033 result = device_attach(&device->dev);
2034 if (result < 0)
2035 return result;
2037 device_init_wakeup(&device->dev, true);
2040 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2041 struct acpi_device *device = NULL;
2043 result = acpi_add_single_object(&device, NULL,
2044 ACPI_BUS_TYPE_SLEEP_BUTTON,
2045 ACPI_STA_DEFAULT);
2046 if (result)
2047 return result;
2049 result = device_attach(&device->dev);
2052 return result < 0 ? result : 0;
2055 int __init acpi_scan_init(void)
2057 int result;
2059 result = bus_register(&acpi_bus_type);
2060 if (result) {
2061 /* We don't want to quit even if we failed to add suspend/resume */
2062 printk(KERN_ERR PREFIX "Could not register bus type\n");
2065 acpi_pci_root_init();
2066 acpi_pci_link_init();
2067 acpi_processor_init();
2068 acpi_platform_init();
2069 acpi_lpss_init();
2070 acpi_cmos_rtc_init();
2071 acpi_container_init();
2072 acpi_memory_hotplug_init();
2073 acpi_dock_init();
2075 mutex_lock(&acpi_scan_lock);
2077 * Enumerate devices in the ACPI namespace.
2079 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2080 if (result)
2081 goto out;
2083 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2084 if (result)
2085 goto out;
2087 result = acpi_bus_scan_fixed();
2088 if (result) {
2089 acpi_device_unregister(acpi_root);
2090 goto out;
2093 acpi_update_all_gpes();
2095 acpi_pci_root_hp_init();
2097 out:
2098 mutex_unlock(&acpi_scan_lock);
2099 return result;