xref: /openbmc/linux/drivers/acpi/scan.c (revision ace8238b00eafd493b8dbcc7db813ed72b8b6e87)
1 /*
2  * scan.c - support for transforming the ACPI namespace into individual objects
3  */
4 
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>
14 
15 #include <acpi/acpi_drivers.h>
16 
17 #include "internal.h"
18 
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;
23 
24 #define ACPI_BUS_CLASS			"system_bus"
25 #define ACPI_BUS_HID			"LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME		"System Bus"
27 
28 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
29 
30 /*
31  * If set, devices will be hot-removed even if they cannot be put offline
32  * gracefully (from the kernel's standpoint).
33  */
34 bool acpi_force_hot_remove;
35 
36 static const char *dummy_hid = "device";
37 
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);
43 
44 struct acpi_device_bus_id{
45 	char bus_id[15];
46 	unsigned int instance_no;
47 	struct list_head node;
48 };
49 
50 void acpi_scan_lock_acquire(void)
51 {
52 	mutex_lock(&acpi_scan_lock);
53 }
54 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
55 
56 void acpi_scan_lock_release(void)
57 {
58 	mutex_unlock(&acpi_scan_lock);
59 }
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
61 
62 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63 {
64 	if (!handler || !handler->attach)
65 		return -EINVAL;
66 
67 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 	return 0;
69 }
70 
71 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 				       const char *hotplug_profile_name)
73 {
74 	int error;
75 
76 	error = acpi_scan_add_handler(handler);
77 	if (error)
78 		return error;
79 
80 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 	return 0;
82 }
83 
84 /*
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"
88 */
89 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90 			   int size)
91 {
92 	int len;
93 	int count;
94 	struct acpi_hardware_id *id;
95 
96 	if (list_empty(&acpi_dev->pnp.ids))
97 		return 0;
98 
99 	len = snprintf(modalias, size, "acpi:");
100 	size -= len;
101 
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;
108 	}
109 
110 	modalias[len] = '\0';
111 	return len;
112 }
113 
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;
118 
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;
125 }
126 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
127 
128 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
129 				    void **ret_p)
130 {
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;
135 
136 	if (acpi_bus_get_device(handle, &device))
137 		return AE_OK;
138 
139 	if (device->handler && !device->handler->hotplug.enabled) {
140 		*ret_p = &device->dev;
141 		return AE_SUPPORT;
142 	}
143 
144 	mutex_lock(&device->physical_node_lock);
145 
146 	list_for_each_entry(pn, &device->physical_node_list, node) {
147 		int ret;
148 
149 		if (second_pass) {
150 			/* Skip devices offlined by the first pass. */
151 			if (pn->put_online)
152 				continue;
153 		} else {
154 			pn->put_online = false;
155 		}
156 		ret = device_offline(pn->dev);
157 		if (acpi_force_hot_remove)
158 			continue;
159 
160 		if (ret >= 0) {
161 			pn->put_online = !ret;
162 		} else {
163 			*ret_p = pn->dev;
164 			if (second_pass) {
165 				status = AE_ERROR;
166 				break;
167 			}
168 		}
169 	}
170 
171 	mutex_unlock(&device->physical_node_lock);
172 
173 	return status;
174 }
175 
176 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
177 				   void **ret_p)
178 {
179 	struct acpi_device *device = NULL;
180 	struct acpi_device_physical_node *pn;
181 
182 	if (acpi_bus_get_device(handle, &device))
183 		return AE_OK;
184 
185 	mutex_lock(&device->physical_node_lock);
186 
187 	list_for_each_entry(pn, &device->physical_node_list, node)
188 		if (pn->put_online) {
189 			device_online(pn->dev);
190 			pn->put_online = false;
191 		}
192 
193 	mutex_unlock(&device->physical_node_lock);
194 
195 	return AE_OK;
196 }
197 
198 static int acpi_scan_hot_remove(struct acpi_device *device)
199 {
200 	acpi_handle handle = device->handle;
201 	struct device *errdev;
202 	acpi_status status;
203 	unsigned long long sta;
204 
205 	/* If there is no handle, the device node has been unregistered. */
206 	if (!handle) {
207 		dev_dbg(&device->dev, "ACPI handle missing\n");
208 		put_device(&device->dev);
209 		return -EINVAL;
210 	}
211 
212 	/*
213 	 * Carry out two passes here and ignore errors in the first pass,
214 	 * because if the devices in question are memory blocks and
215 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216 	 * that the other blocks depend on, but it is not known in advance which
217 	 * block holds them.
218 	 *
219 	 * If the first pass is successful, the second one isn't needed, though.
220 	 */
221 	errdev = NULL;
222 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
223 				     NULL, acpi_bus_offline, (void *)false,
224 				     (void **)&errdev);
225 	if (status == AE_SUPPORT) {
226 		dev_warn(errdev, "Offline disabled.\n");
227 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
228 				    acpi_bus_online, NULL, NULL, NULL);
229 		put_device(&device->dev);
230 		return -EPERM;
231 	}
232 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
233 	if (errdev) {
234 		errdev = NULL;
235 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
236 				    NULL, acpi_bus_offline, (void *)true,
237 				    (void **)&errdev);
238 		if (!errdev || acpi_force_hot_remove)
239 			acpi_bus_offline(handle, 0, (void *)true,
240 					 (void **)&errdev);
241 
242 		if (errdev && !acpi_force_hot_remove) {
243 			dev_warn(errdev, "Offline failed.\n");
244 			acpi_bus_online(handle, 0, NULL, NULL);
245 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
246 					    ACPI_UINT32_MAX, acpi_bus_online,
247 					    NULL, NULL, NULL);
248 			put_device(&device->dev);
249 			return -EBUSY;
250 		}
251 	}
252 
253 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
254 		"Hot-removing device %s...\n", dev_name(&device->dev)));
255 
256 	acpi_bus_trim(device);
257 
258 	/* Device node has been unregistered. */
259 	put_device(&device->dev);
260 	device = NULL;
261 
262 	acpi_evaluate_lck(handle, 0);
263 	/*
264 	 * TBD: _EJD support.
265 	 */
266 	status = acpi_evaluate_ej0(handle);
267 	if (status == AE_NOT_FOUND)
268 		return -ENODEV;
269 	else if (ACPI_FAILURE(status))
270 		return -EIO;
271 
272 	/*
273 	 * Verify if eject was indeed successful.  If not, log an error
274 	 * message.  No need to call _OST since _EJ0 call was made OK.
275 	 */
276 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
277 	if (ACPI_FAILURE(status)) {
278 		acpi_handle_warn(handle,
279 			"Status check after eject failed (0x%x)\n", status);
280 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
281 		acpi_handle_warn(handle,
282 			"Eject incomplete - status 0x%llx\n", sta);
283 	}
284 
285 	return 0;
286 }
287 
288 static void acpi_bus_device_eject(void *context)
289 {
290 	acpi_handle handle = context;
291 	struct acpi_device *device = NULL;
292 	struct acpi_scan_handler *handler;
293 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
294 	int error;
295 
296 	lock_device_hotplug();
297 	mutex_lock(&acpi_scan_lock);
298 
299 	acpi_bus_get_device(handle, &device);
300 	if (!device)
301 		goto err_out;
302 
303 	handler = device->handler;
304 	if (!handler || !handler->hotplug.enabled)
305 		goto err_support;
306 
307 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
308 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
309 	if (handler->hotplug.mode == AHM_CONTAINER)
310 		kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
311 
312 	get_device(&device->dev);
313 	error = acpi_scan_hot_remove(device);
314 	if (error == -EPERM) {
315 		goto err_support;
316 	} else if (error) {
317 		goto err_out;
318 	}
319 
320  out:
321 	mutex_unlock(&acpi_scan_lock);
322 	unlock_device_hotplug();
323 	return;
324 
325  err_support:
326 	ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
327  err_out:
328 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
329 				  NULL);
330 	goto out;
331 }
332 
333 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
334 {
335 	struct acpi_device *device = NULL;
336 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
337 	int error;
338 
339 	lock_device_hotplug();
340 	mutex_lock(&acpi_scan_lock);
341 
342 	if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
343 		acpi_bus_get_device(handle, &device);
344 		if (device) {
345 			dev_warn(&device->dev, "Attempt to re-insert\n");
346 			goto out;
347 		}
348 	}
349 	acpi_evaluate_hotplug_ost(handle, ost_source,
350 				  ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
351 	error = acpi_bus_scan(handle);
352 	if (error) {
353 		acpi_handle_warn(handle, "Namespace scan failure\n");
354 		goto out;
355 	}
356 	error = acpi_bus_get_device(handle, &device);
357 	if (error) {
358 		acpi_handle_warn(handle, "Missing device node object\n");
359 		goto out;
360 	}
361 	ost_code = ACPI_OST_SC_SUCCESS;
362 	if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
363 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
364 
365  out:
366 	acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
367 	mutex_unlock(&acpi_scan_lock);
368 	unlock_device_hotplug();
369 }
370 
371 static void acpi_scan_bus_check(void *context)
372 {
373 	acpi_scan_bus_device_check((acpi_handle)context,
374 				   ACPI_NOTIFY_BUS_CHECK);
375 }
376 
377 static void acpi_scan_device_check(void *context)
378 {
379 	acpi_scan_bus_device_check((acpi_handle)context,
380 				   ACPI_NOTIFY_DEVICE_CHECK);
381 }
382 
383 static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
384 {
385 	u32 ost_status;
386 
387 	switch (type) {
388 	case ACPI_NOTIFY_BUS_CHECK:
389 		acpi_handle_debug(handle,
390 			"ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
391 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
392 		break;
393 	case ACPI_NOTIFY_DEVICE_CHECK:
394 		acpi_handle_debug(handle,
395 			"ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
396 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
397 		break;
398 	case ACPI_NOTIFY_EJECT_REQUEST:
399 		acpi_handle_debug(handle,
400 			"ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
401 		ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
402 		break;
403 	default:
404 		/* non-hotplug event; possibly handled by other handler */
405 		return;
406 	}
407 
408 	acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
409 }
410 
411 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
412 {
413 	acpi_osd_exec_callback callback;
414 	struct acpi_scan_handler *handler = data;
415 	acpi_status status;
416 
417 	if (!handler->hotplug.enabled)
418 		return acpi_hotplug_unsupported(handle, type);
419 
420 	switch (type) {
421 	case ACPI_NOTIFY_BUS_CHECK:
422 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
423 		callback = acpi_scan_bus_check;
424 		break;
425 	case ACPI_NOTIFY_DEVICE_CHECK:
426 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
427 		callback = acpi_scan_device_check;
428 		break;
429 	case ACPI_NOTIFY_EJECT_REQUEST:
430 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
431 		callback = acpi_bus_device_eject;
432 		break;
433 	default:
434 		/* non-hotplug event; possibly handled by other handler */
435 		return;
436 	}
437 	status = acpi_os_hotplug_execute(callback, handle);
438 	if (ACPI_FAILURE(status))
439 		acpi_evaluate_hotplug_ost(handle, type,
440 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
441 					  NULL);
442 }
443 
444 void __acpi_bus_hot_remove_device(struct acpi_device *device, u32 ost_src)
445 {
446 	acpi_handle handle = device->handle;
447 	int error;
448 
449 	lock_device_hotplug();
450 	mutex_lock(&acpi_scan_lock);
451 
452 	error = acpi_scan_hot_remove(device);
453 	if (error && handle)
454 		acpi_evaluate_hotplug_ost(handle, ost_src,
455 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
456 					  NULL);
457 
458 	mutex_unlock(&acpi_scan_lock);
459 	unlock_device_hotplug();
460 }
461 
462 /**
463  * acpi_bus_hot_remove_device: Hot-remove a device and its children.
464  * @context: Address of the ACPI device object to hot-remove.
465  */
466 void acpi_bus_hot_remove_device(void *context)
467 {
468 	__acpi_bus_hot_remove_device(context, ACPI_NOTIFY_EJECT_REQUEST);
469 }
470 
471 static ssize_t real_power_state_show(struct device *dev,
472 				     struct device_attribute *attr, char *buf)
473 {
474 	struct acpi_device *adev = to_acpi_device(dev);
475 	int state;
476 	int ret;
477 
478 	ret = acpi_device_get_power(adev, &state);
479 	if (ret)
480 		return ret;
481 
482 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
483 }
484 
485 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
486 
487 static ssize_t power_state_show(struct device *dev,
488 				struct device_attribute *attr, char *buf)
489 {
490 	struct acpi_device *adev = to_acpi_device(dev);
491 
492 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
493 }
494 
495 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
496 
497 static void acpi_eject_store_work(void *context)
498 {
499 	__acpi_bus_hot_remove_device(context, ACPI_OST_EC_OSPM_EJECT);
500 }
501 
502 static ssize_t
503 acpi_eject_store(struct device *d, struct device_attribute *attr,
504 		const char *buf, size_t count)
505 {
506 	struct acpi_device *acpi_device = to_acpi_device(d);
507 	acpi_object_type not_used;
508 	acpi_status status;
509 
510 	if (!count || buf[0] != '1')
511 		return -EINVAL;
512 
513 	if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
514 	    && !acpi_device->driver)
515 		return -ENODEV;
516 
517 	status = acpi_get_type(acpi_device->handle, &not_used);
518 	if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
519 		return -ENODEV;
520 
521 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
522 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
523 	get_device(&acpi_device->dev);
524 	status = acpi_os_hotplug_execute(acpi_eject_store_work, acpi_device);
525 	if (ACPI_SUCCESS(status))
526 		return count;
527 
528 	put_device(&acpi_device->dev);
529 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
530 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
531 	return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
532 }
533 
534 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
535 
536 static ssize_t
537 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
538 	struct acpi_device *acpi_dev = to_acpi_device(dev);
539 
540 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
541 }
542 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
543 
544 static ssize_t acpi_device_uid_show(struct device *dev,
545 				    struct device_attribute *attr, char *buf)
546 {
547 	struct acpi_device *acpi_dev = to_acpi_device(dev);
548 
549 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
550 }
551 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
552 
553 static ssize_t acpi_device_adr_show(struct device *dev,
554 				    struct device_attribute *attr, char *buf)
555 {
556 	struct acpi_device *acpi_dev = to_acpi_device(dev);
557 
558 	return sprintf(buf, "0x%08x\n",
559 		       (unsigned int)(acpi_dev->pnp.bus_address));
560 }
561 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
562 
563 static ssize_t
564 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
565 	struct acpi_device *acpi_dev = to_acpi_device(dev);
566 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
567 	int result;
568 
569 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
570 	if (result)
571 		goto end;
572 
573 	result = sprintf(buf, "%s\n", (char*)path.pointer);
574 	kfree(path.pointer);
575 end:
576 	return result;
577 }
578 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
579 
580 /* sysfs file that shows description text from the ACPI _STR method */
581 static ssize_t description_show(struct device *dev,
582 				struct device_attribute *attr,
583 				char *buf) {
584 	struct acpi_device *acpi_dev = to_acpi_device(dev);
585 	int result;
586 
587 	if (acpi_dev->pnp.str_obj == NULL)
588 		return 0;
589 
590 	/*
591 	 * The _STR object contains a Unicode identifier for a device.
592 	 * We need to convert to utf-8 so it can be displayed.
593 	 */
594 	result = utf16s_to_utf8s(
595 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
596 		acpi_dev->pnp.str_obj->buffer.length,
597 		UTF16_LITTLE_ENDIAN, buf,
598 		PAGE_SIZE);
599 
600 	buf[result++] = '\n';
601 
602 	return result;
603 }
604 static DEVICE_ATTR(description, 0444, description_show, NULL);
605 
606 static ssize_t
607 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
608 		     char *buf) {
609 	struct acpi_device *acpi_dev = to_acpi_device(dev);
610 
611 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
612 }
613 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
614 
615 static int acpi_device_setup_files(struct acpi_device *dev)
616 {
617 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
618 	acpi_status status;
619 	unsigned long long sun;
620 	int result = 0;
621 
622 	/*
623 	 * Devices gotten from FADT don't have a "path" attribute
624 	 */
625 	if (dev->handle) {
626 		result = device_create_file(&dev->dev, &dev_attr_path);
627 		if (result)
628 			goto end;
629 	}
630 
631 	if (!list_empty(&dev->pnp.ids)) {
632 		result = device_create_file(&dev->dev, &dev_attr_hid);
633 		if (result)
634 			goto end;
635 
636 		result = device_create_file(&dev->dev, &dev_attr_modalias);
637 		if (result)
638 			goto end;
639 	}
640 
641 	/*
642 	 * If device has _STR, 'description' file is created
643 	 */
644 	if (acpi_has_method(dev->handle, "_STR")) {
645 		status = acpi_evaluate_object(dev->handle, "_STR",
646 					NULL, &buffer);
647 		if (ACPI_FAILURE(status))
648 			buffer.pointer = NULL;
649 		dev->pnp.str_obj = buffer.pointer;
650 		result = device_create_file(&dev->dev, &dev_attr_description);
651 		if (result)
652 			goto end;
653 	}
654 
655 	if (dev->pnp.type.bus_address)
656 		result = device_create_file(&dev->dev, &dev_attr_adr);
657 	if (dev->pnp.unique_id)
658 		result = device_create_file(&dev->dev, &dev_attr_uid);
659 
660 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
661 	if (ACPI_SUCCESS(status)) {
662 		dev->pnp.sun = (unsigned long)sun;
663 		result = device_create_file(&dev->dev, &dev_attr_sun);
664 		if (result)
665 			goto end;
666 	} else {
667 		dev->pnp.sun = (unsigned long)-1;
668 	}
669 
670         /*
671          * If device has _EJ0, 'eject' file is created that is used to trigger
672          * hot-removal function from userland.
673          */
674 	if (acpi_has_method(dev->handle, "_EJ0")) {
675 		result = device_create_file(&dev->dev, &dev_attr_eject);
676 		if (result)
677 			return result;
678 	}
679 
680 	if (dev->flags.power_manageable) {
681 		result = device_create_file(&dev->dev, &dev_attr_power_state);
682 		if (result)
683 			return result;
684 
685 		if (dev->power.flags.power_resources)
686 			result = device_create_file(&dev->dev,
687 						    &dev_attr_real_power_state);
688 	}
689 
690 end:
691 	return result;
692 }
693 
694 static void acpi_device_remove_files(struct acpi_device *dev)
695 {
696 	if (dev->flags.power_manageable) {
697 		device_remove_file(&dev->dev, &dev_attr_power_state);
698 		if (dev->power.flags.power_resources)
699 			device_remove_file(&dev->dev,
700 					   &dev_attr_real_power_state);
701 	}
702 
703 	/*
704 	 * If device has _STR, remove 'description' file
705 	 */
706 	if (acpi_has_method(dev->handle, "_STR")) {
707 		kfree(dev->pnp.str_obj);
708 		device_remove_file(&dev->dev, &dev_attr_description);
709 	}
710 	/*
711 	 * If device has _EJ0, remove 'eject' file.
712 	 */
713 	if (acpi_has_method(dev->handle, "_EJ0"))
714 		device_remove_file(&dev->dev, &dev_attr_eject);
715 
716 	if (acpi_has_method(dev->handle, "_SUN"))
717 		device_remove_file(&dev->dev, &dev_attr_sun);
718 
719 	if (dev->pnp.unique_id)
720 		device_remove_file(&dev->dev, &dev_attr_uid);
721 	if (dev->pnp.type.bus_address)
722 		device_remove_file(&dev->dev, &dev_attr_adr);
723 	device_remove_file(&dev->dev, &dev_attr_modalias);
724 	device_remove_file(&dev->dev, &dev_attr_hid);
725 	if (dev->handle)
726 		device_remove_file(&dev->dev, &dev_attr_path);
727 }
728 /* --------------------------------------------------------------------------
729 			ACPI Bus operations
730    -------------------------------------------------------------------------- */
731 
732 static const struct acpi_device_id *__acpi_match_device(
733 	struct acpi_device *device, const struct acpi_device_id *ids)
734 {
735 	const struct acpi_device_id *id;
736 	struct acpi_hardware_id *hwid;
737 
738 	/*
739 	 * If the device is not present, it is unnecessary to load device
740 	 * driver for it.
741 	 */
742 	if (!device->status.present)
743 		return NULL;
744 
745 	for (id = ids; id->id[0]; id++)
746 		list_for_each_entry(hwid, &device->pnp.ids, list)
747 			if (!strcmp((char *) id->id, hwid->id))
748 				return id;
749 
750 	return NULL;
751 }
752 
753 /**
754  * acpi_match_device - Match a struct device against a given list of ACPI IDs
755  * @ids: Array of struct acpi_device_id object to match against.
756  * @dev: The device structure to match.
757  *
758  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
759  * object for that handle and use that object to match against a given list of
760  * device IDs.
761  *
762  * Return a pointer to the first matching ID on success or %NULL on failure.
763  */
764 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
765 					       const struct device *dev)
766 {
767 	struct acpi_device *adev;
768 	acpi_handle handle = ACPI_HANDLE(dev);
769 
770 	if (!ids || !handle || acpi_bus_get_device(handle, &adev))
771 		return NULL;
772 
773 	return __acpi_match_device(adev, ids);
774 }
775 EXPORT_SYMBOL_GPL(acpi_match_device);
776 
777 int acpi_match_device_ids(struct acpi_device *device,
778 			  const struct acpi_device_id *ids)
779 {
780 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
781 }
782 EXPORT_SYMBOL(acpi_match_device_ids);
783 
784 static void acpi_free_power_resources_lists(struct acpi_device *device)
785 {
786 	int i;
787 
788 	if (device->wakeup.flags.valid)
789 		acpi_power_resources_list_free(&device->wakeup.resources);
790 
791 	if (!device->flags.power_manageable)
792 		return;
793 
794 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
795 		struct acpi_device_power_state *ps = &device->power.states[i];
796 		acpi_power_resources_list_free(&ps->resources);
797 	}
798 }
799 
800 static void acpi_device_release(struct device *dev)
801 {
802 	struct acpi_device *acpi_dev = to_acpi_device(dev);
803 
804 	acpi_free_pnp_ids(&acpi_dev->pnp);
805 	acpi_free_power_resources_lists(acpi_dev);
806 	kfree(acpi_dev);
807 }
808 
809 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
810 {
811 	struct acpi_device *acpi_dev = to_acpi_device(dev);
812 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
813 
814 	return acpi_dev->flags.match_driver
815 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
816 }
817 
818 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
819 {
820 	struct acpi_device *acpi_dev = to_acpi_device(dev);
821 	int len;
822 
823 	if (list_empty(&acpi_dev->pnp.ids))
824 		return 0;
825 
826 	if (add_uevent_var(env, "MODALIAS="))
827 		return -ENOMEM;
828 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
829 			      sizeof(env->buf) - env->buflen);
830 	if (len >= (sizeof(env->buf) - env->buflen))
831 		return -ENOMEM;
832 	env->buflen += len;
833 	return 0;
834 }
835 
836 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
837 {
838 	struct acpi_device *device = data;
839 
840 	device->driver->ops.notify(device, event);
841 }
842 
843 static acpi_status acpi_device_notify_fixed(void *data)
844 {
845 	struct acpi_device *device = data;
846 
847 	/* Fixed hardware devices have no handles */
848 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
849 	return AE_OK;
850 }
851 
852 static int acpi_device_install_notify_handler(struct acpi_device *device)
853 {
854 	acpi_status status;
855 
856 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
857 		status =
858 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
859 						     acpi_device_notify_fixed,
860 						     device);
861 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
862 		status =
863 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
864 						     acpi_device_notify_fixed,
865 						     device);
866 	else
867 		status = acpi_install_notify_handler(device->handle,
868 						     ACPI_DEVICE_NOTIFY,
869 						     acpi_device_notify,
870 						     device);
871 
872 	if (ACPI_FAILURE(status))
873 		return -EINVAL;
874 	return 0;
875 }
876 
877 static void acpi_device_remove_notify_handler(struct acpi_device *device)
878 {
879 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
880 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
881 						acpi_device_notify_fixed);
882 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
883 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
884 						acpi_device_notify_fixed);
885 	else
886 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
887 					   acpi_device_notify);
888 }
889 
890 static int acpi_device_probe(struct device *dev)
891 {
892 	struct acpi_device *acpi_dev = to_acpi_device(dev);
893 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
894 	int ret;
895 
896 	if (acpi_dev->handler)
897 		return -EINVAL;
898 
899 	if (!acpi_drv->ops.add)
900 		return -ENOSYS;
901 
902 	ret = acpi_drv->ops.add(acpi_dev);
903 	if (ret)
904 		return ret;
905 
906 	acpi_dev->driver = acpi_drv;
907 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
908 			  "Driver [%s] successfully bound to device [%s]\n",
909 			  acpi_drv->name, acpi_dev->pnp.bus_id));
910 
911 	if (acpi_drv->ops.notify) {
912 		ret = acpi_device_install_notify_handler(acpi_dev);
913 		if (ret) {
914 			if (acpi_drv->ops.remove)
915 				acpi_drv->ops.remove(acpi_dev);
916 
917 			acpi_dev->driver = NULL;
918 			acpi_dev->driver_data = NULL;
919 			return ret;
920 		}
921 	}
922 
923 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
924 			  acpi_drv->name, acpi_dev->pnp.bus_id));
925 	get_device(dev);
926 	return 0;
927 }
928 
929 static int acpi_device_remove(struct device * dev)
930 {
931 	struct acpi_device *acpi_dev = to_acpi_device(dev);
932 	struct acpi_driver *acpi_drv = acpi_dev->driver;
933 
934 	if (acpi_drv) {
935 		if (acpi_drv->ops.notify)
936 			acpi_device_remove_notify_handler(acpi_dev);
937 		if (acpi_drv->ops.remove)
938 			acpi_drv->ops.remove(acpi_dev);
939 	}
940 	acpi_dev->driver = NULL;
941 	acpi_dev->driver_data = NULL;
942 
943 	put_device(dev);
944 	return 0;
945 }
946 
947 struct bus_type acpi_bus_type = {
948 	.name		= "acpi",
949 	.match		= acpi_bus_match,
950 	.probe		= acpi_device_probe,
951 	.remove		= acpi_device_remove,
952 	.uevent		= acpi_device_uevent,
953 };
954 
955 static void acpi_bus_data_handler(acpi_handle handle, void *context)
956 {
957 	/* Intentionally empty. */
958 }
959 
960 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
961 {
962 	acpi_status status;
963 
964 	if (!device)
965 		return -EINVAL;
966 
967 	status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
968 	if (ACPI_FAILURE(status) || !*device) {
969 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
970 				  handle));
971 		return -ENODEV;
972 	}
973 	return 0;
974 }
975 EXPORT_SYMBOL_GPL(acpi_bus_get_device);
976 
977 int acpi_device_add(struct acpi_device *device,
978 		    void (*release)(struct device *))
979 {
980 	int result;
981 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
982 	int found = 0;
983 
984 	if (device->handle) {
985 		acpi_status status;
986 
987 		status = acpi_attach_data(device->handle, acpi_bus_data_handler,
988 					  device);
989 		if (ACPI_FAILURE(status)) {
990 			acpi_handle_err(device->handle,
991 					"Unable to attach device data\n");
992 			return -ENODEV;
993 		}
994 	}
995 
996 	/*
997 	 * Linkage
998 	 * -------
999 	 * Link this device to its parent and siblings.
1000 	 */
1001 	INIT_LIST_HEAD(&device->children);
1002 	INIT_LIST_HEAD(&device->node);
1003 	INIT_LIST_HEAD(&device->wakeup_list);
1004 	INIT_LIST_HEAD(&device->physical_node_list);
1005 	mutex_init(&device->physical_node_lock);
1006 	INIT_LIST_HEAD(&device->power_dependent);
1007 
1008 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1009 	if (!new_bus_id) {
1010 		pr_err(PREFIX "Memory allocation error\n");
1011 		result = -ENOMEM;
1012 		goto err_detach;
1013 	}
1014 
1015 	mutex_lock(&acpi_device_lock);
1016 	/*
1017 	 * Find suitable bus_id and instance number in acpi_bus_id_list
1018 	 * If failed, create one and link it into acpi_bus_id_list
1019 	 */
1020 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1021 		if (!strcmp(acpi_device_bus_id->bus_id,
1022 			    acpi_device_hid(device))) {
1023 			acpi_device_bus_id->instance_no++;
1024 			found = 1;
1025 			kfree(new_bus_id);
1026 			break;
1027 		}
1028 	}
1029 	if (!found) {
1030 		acpi_device_bus_id = new_bus_id;
1031 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1032 		acpi_device_bus_id->instance_no = 0;
1033 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1034 	}
1035 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1036 
1037 	if (device->parent)
1038 		list_add_tail(&device->node, &device->parent->children);
1039 
1040 	if (device->wakeup.flags.valid)
1041 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1042 	mutex_unlock(&acpi_device_lock);
1043 
1044 	if (device->parent)
1045 		device->dev.parent = &device->parent->dev;
1046 	device->dev.bus = &acpi_bus_type;
1047 	device->dev.release = release;
1048 	result = device_add(&device->dev);
1049 	if (result) {
1050 		dev_err(&device->dev, "Error registering device\n");
1051 		goto err;
1052 	}
1053 
1054 	result = acpi_device_setup_files(device);
1055 	if (result)
1056 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1057 		       dev_name(&device->dev));
1058 
1059 	return 0;
1060 
1061  err:
1062 	mutex_lock(&acpi_device_lock);
1063 	if (device->parent)
1064 		list_del(&device->node);
1065 	list_del(&device->wakeup_list);
1066 	mutex_unlock(&acpi_device_lock);
1067 
1068  err_detach:
1069 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1070 	return result;
1071 }
1072 
1073 static void acpi_device_unregister(struct acpi_device *device)
1074 {
1075 	mutex_lock(&acpi_device_lock);
1076 	if (device->parent)
1077 		list_del(&device->node);
1078 
1079 	list_del(&device->wakeup_list);
1080 	mutex_unlock(&acpi_device_lock);
1081 
1082 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1083 
1084 	acpi_power_add_remove_device(device, false);
1085 	acpi_device_remove_files(device);
1086 	if (device->remove)
1087 		device->remove(device);
1088 
1089 	device_del(&device->dev);
1090 	/*
1091 	 * Transition the device to D3cold to drop the reference counts of all
1092 	 * power resources the device depends on and turn off the ones that have
1093 	 * no more references.
1094 	 */
1095 	acpi_device_set_power(device, ACPI_STATE_D3_COLD);
1096 	device->handle = NULL;
1097 	put_device(&device->dev);
1098 }
1099 
1100 /* --------------------------------------------------------------------------
1101                                  Driver Management
1102    -------------------------------------------------------------------------- */
1103 /**
1104  * acpi_bus_register_driver - register a driver with the ACPI bus
1105  * @driver: driver being registered
1106  *
1107  * Registers a driver with the ACPI bus.  Searches the namespace for all
1108  * devices that match the driver's criteria and binds.  Returns zero for
1109  * success or a negative error status for failure.
1110  */
1111 int acpi_bus_register_driver(struct acpi_driver *driver)
1112 {
1113 	int ret;
1114 
1115 	if (acpi_disabled)
1116 		return -ENODEV;
1117 	driver->drv.name = driver->name;
1118 	driver->drv.bus = &acpi_bus_type;
1119 	driver->drv.owner = driver->owner;
1120 
1121 	ret = driver_register(&driver->drv);
1122 	return ret;
1123 }
1124 
1125 EXPORT_SYMBOL(acpi_bus_register_driver);
1126 
1127 /**
1128  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1129  * @driver: driver to unregister
1130  *
1131  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
1132  * devices that match the driver's criteria and unbinds.
1133  */
1134 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1135 {
1136 	driver_unregister(&driver->drv);
1137 }
1138 
1139 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1140 
1141 /* --------------------------------------------------------------------------
1142                                  Device Enumeration
1143    -------------------------------------------------------------------------- */
1144 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1145 {
1146 	struct acpi_device *device = NULL;
1147 	acpi_status status;
1148 
1149 	/*
1150 	 * Fixed hardware devices do not appear in the namespace and do not
1151 	 * have handles, but we fabricate acpi_devices for them, so we have
1152 	 * to deal with them specially.
1153 	 */
1154 	if (!handle)
1155 		return acpi_root;
1156 
1157 	do {
1158 		status = acpi_get_parent(handle, &handle);
1159 		if (ACPI_FAILURE(status))
1160 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
1161 	} while (acpi_bus_get_device(handle, &device));
1162 	return device;
1163 }
1164 
1165 acpi_status
1166 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1167 {
1168 	acpi_status status;
1169 	acpi_handle tmp;
1170 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1171 	union acpi_object *obj;
1172 
1173 	status = acpi_get_handle(handle, "_EJD", &tmp);
1174 	if (ACPI_FAILURE(status))
1175 		return status;
1176 
1177 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1178 	if (ACPI_SUCCESS(status)) {
1179 		obj = buffer.pointer;
1180 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1181 					 ejd);
1182 		kfree(buffer.pointer);
1183 	}
1184 	return status;
1185 }
1186 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1187 
1188 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1189 					struct acpi_device_wakeup *wakeup)
1190 {
1191 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1192 	union acpi_object *package = NULL;
1193 	union acpi_object *element = NULL;
1194 	acpi_status status;
1195 	int err = -ENODATA;
1196 
1197 	if (!wakeup)
1198 		return -EINVAL;
1199 
1200 	INIT_LIST_HEAD(&wakeup->resources);
1201 
1202 	/* _PRW */
1203 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1204 	if (ACPI_FAILURE(status)) {
1205 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1206 		return err;
1207 	}
1208 
1209 	package = (union acpi_object *)buffer.pointer;
1210 
1211 	if (!package || package->package.count < 2)
1212 		goto out;
1213 
1214 	element = &(package->package.elements[0]);
1215 	if (!element)
1216 		goto out;
1217 
1218 	if (element->type == ACPI_TYPE_PACKAGE) {
1219 		if ((element->package.count < 2) ||
1220 		    (element->package.elements[0].type !=
1221 		     ACPI_TYPE_LOCAL_REFERENCE)
1222 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1223 			goto out;
1224 
1225 		wakeup->gpe_device =
1226 		    element->package.elements[0].reference.handle;
1227 		wakeup->gpe_number =
1228 		    (u32) element->package.elements[1].integer.value;
1229 	} else if (element->type == ACPI_TYPE_INTEGER) {
1230 		wakeup->gpe_device = NULL;
1231 		wakeup->gpe_number = element->integer.value;
1232 	} else {
1233 		goto out;
1234 	}
1235 
1236 	element = &(package->package.elements[1]);
1237 	if (element->type != ACPI_TYPE_INTEGER)
1238 		goto out;
1239 
1240 	wakeup->sleep_state = element->integer.value;
1241 
1242 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1243 	if (err)
1244 		goto out;
1245 
1246 	if (!list_empty(&wakeup->resources)) {
1247 		int sleep_state;
1248 
1249 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1250 						  &sleep_state);
1251 		if (err) {
1252 			acpi_handle_warn(handle, "Retrieving current states "
1253 					 "of wakeup power resources failed\n");
1254 			acpi_power_resources_list_free(&wakeup->resources);
1255 			goto out;
1256 		}
1257 		if (sleep_state < wakeup->sleep_state) {
1258 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
1259 					 "(S%d) by S%d from power resources\n",
1260 					 (int)wakeup->sleep_state, sleep_state);
1261 			wakeup->sleep_state = sleep_state;
1262 		}
1263 	}
1264 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1265 
1266  out:
1267 	kfree(buffer.pointer);
1268 	return err;
1269 }
1270 
1271 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1272 {
1273 	struct acpi_device_id button_device_ids[] = {
1274 		{"PNP0C0C", 0},
1275 		{"PNP0C0D", 0},
1276 		{"PNP0C0E", 0},
1277 		{"", 0},
1278 	};
1279 	acpi_status status;
1280 	acpi_event_status event_status;
1281 
1282 	device->wakeup.flags.notifier_present = 0;
1283 
1284 	/* Power button, Lid switch always enable wakeup */
1285 	if (!acpi_match_device_ids(device, button_device_ids)) {
1286 		device->wakeup.flags.run_wake = 1;
1287 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1288 			/* Do not use Lid/sleep button for S5 wakeup */
1289 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1290 				device->wakeup.sleep_state = ACPI_STATE_S4;
1291 		}
1292 		device_set_wakeup_capable(&device->dev, true);
1293 		return;
1294 	}
1295 
1296 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1297 					device->wakeup.gpe_number,
1298 						&event_status);
1299 	if (status == AE_OK)
1300 		device->wakeup.flags.run_wake =
1301 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1302 }
1303 
1304 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1305 {
1306 	int err;
1307 
1308 	/* Presence of _PRW indicates wake capable */
1309 	if (!acpi_has_method(device->handle, "_PRW"))
1310 		return;
1311 
1312 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1313 							   &device->wakeup);
1314 	if (err) {
1315 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1316 		return;
1317 	}
1318 
1319 	device->wakeup.flags.valid = 1;
1320 	device->wakeup.prepare_count = 0;
1321 	acpi_bus_set_run_wake_flags(device);
1322 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1323 	 * system for the ACPI device with the _PRW object.
1324 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1325 	 * So it is necessary to call _DSW object first. Only when it is not
1326 	 * present will the _PSW object used.
1327 	 */
1328 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1329 	if (err)
1330 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1331 				"error in _DSW or _PSW evaluation\n"));
1332 }
1333 
1334 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1335 {
1336 	struct acpi_device_power_state *ps = &device->power.states[state];
1337 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1338 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1339 	acpi_status status;
1340 
1341 	INIT_LIST_HEAD(&ps->resources);
1342 
1343 	/* Evaluate "_PRx" to get referenced power resources */
1344 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1345 	if (ACPI_SUCCESS(status)) {
1346 		union acpi_object *package = buffer.pointer;
1347 
1348 		if (buffer.length && package
1349 		    && package->type == ACPI_TYPE_PACKAGE
1350 		    && package->package.count) {
1351 			int err = acpi_extract_power_resources(package, 0,
1352 							       &ps->resources);
1353 			if (!err)
1354 				device->power.flags.power_resources = 1;
1355 		}
1356 		ACPI_FREE(buffer.pointer);
1357 	}
1358 
1359 	/* Evaluate "_PSx" to see if we can do explicit sets */
1360 	pathname[2] = 'S';
1361 	if (acpi_has_method(device->handle, pathname))
1362 		ps->flags.explicit_set = 1;
1363 
1364 	/*
1365 	 * State is valid if there are means to put the device into it.
1366 	 * D3hot is only valid if _PR3 present.
1367 	 */
1368 	if (!list_empty(&ps->resources)
1369 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1370 		ps->flags.valid = 1;
1371 		ps->flags.os_accessible = 1;
1372 	}
1373 
1374 	ps->power = -1;		/* Unknown - driver assigned */
1375 	ps->latency = -1;	/* Unknown - driver assigned */
1376 }
1377 
1378 static void acpi_bus_get_power_flags(struct acpi_device *device)
1379 {
1380 	u32 i;
1381 
1382 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1383 	if (!acpi_has_method(device->handle, "_PS0") &&
1384 	    !acpi_has_method(device->handle, "_PR0"))
1385 		return;
1386 
1387 	device->flags.power_manageable = 1;
1388 
1389 	/*
1390 	 * Power Management Flags
1391 	 */
1392 	if (acpi_has_method(device->handle, "_PSC"))
1393 		device->power.flags.explicit_get = 1;
1394 	if (acpi_has_method(device->handle, "_IRC"))
1395 		device->power.flags.inrush_current = 1;
1396 
1397 	/*
1398 	 * Enumerate supported power management states
1399 	 */
1400 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1401 		acpi_bus_init_power_state(device, i);
1402 
1403 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1404 
1405 	/* Set defaults for D0 and D3 states (always valid) */
1406 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
1407 	device->power.states[ACPI_STATE_D0].power = 100;
1408 	device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1409 	device->power.states[ACPI_STATE_D3_COLD].power = 0;
1410 
1411 	/* Set D3cold's explicit_set flag if _PS3 exists. */
1412 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1413 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1414 
1415 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1416 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1417 			device->power.flags.power_resources)
1418 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1419 
1420 	if (acpi_bus_init_power(device)) {
1421 		acpi_free_power_resources_lists(device);
1422 		device->flags.power_manageable = 0;
1423 	}
1424 }
1425 
1426 static void acpi_bus_get_flags(struct acpi_device *device)
1427 {
1428 	/* Presence of _STA indicates 'dynamic_status' */
1429 	if (acpi_has_method(device->handle, "_STA"))
1430 		device->flags.dynamic_status = 1;
1431 
1432 	/* Presence of _RMV indicates 'removable' */
1433 	if (acpi_has_method(device->handle, "_RMV"))
1434 		device->flags.removable = 1;
1435 
1436 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1437 	if (acpi_has_method(device->handle, "_EJD") ||
1438 	    acpi_has_method(device->handle, "_EJ0"))
1439 		device->flags.ejectable = 1;
1440 }
1441 
1442 static void acpi_device_get_busid(struct acpi_device *device)
1443 {
1444 	char bus_id[5] = { '?', 0 };
1445 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1446 	int i = 0;
1447 
1448 	/*
1449 	 * Bus ID
1450 	 * ------
1451 	 * The device's Bus ID is simply the object name.
1452 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1453 	 */
1454 	if (ACPI_IS_ROOT_DEVICE(device)) {
1455 		strcpy(device->pnp.bus_id, "ACPI");
1456 		return;
1457 	}
1458 
1459 	switch (device->device_type) {
1460 	case ACPI_BUS_TYPE_POWER_BUTTON:
1461 		strcpy(device->pnp.bus_id, "PWRF");
1462 		break;
1463 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1464 		strcpy(device->pnp.bus_id, "SLPF");
1465 		break;
1466 	default:
1467 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1468 		/* Clean up trailing underscores (if any) */
1469 		for (i = 3; i > 1; i--) {
1470 			if (bus_id[i] == '_')
1471 				bus_id[i] = '\0';
1472 			else
1473 				break;
1474 		}
1475 		strcpy(device->pnp.bus_id, bus_id);
1476 		break;
1477 	}
1478 }
1479 
1480 /*
1481  * acpi_ata_match - see if an acpi object is an ATA device
1482  *
1483  * If an acpi object has one of the ACPI ATA methods defined,
1484  * then we can safely call it an ATA device.
1485  */
1486 bool acpi_ata_match(acpi_handle handle)
1487 {
1488 	return acpi_has_method(handle, "_GTF") ||
1489 	       acpi_has_method(handle, "_GTM") ||
1490 	       acpi_has_method(handle, "_STM") ||
1491 	       acpi_has_method(handle, "_SDD");
1492 }
1493 
1494 /*
1495  * acpi_bay_match - see if an acpi object is an ejectable driver bay
1496  *
1497  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1498  * then we can safely call it an ejectable drive bay
1499  */
1500 bool acpi_bay_match(acpi_handle handle)
1501 {
1502 	acpi_handle phandle;
1503 
1504 	if (!acpi_has_method(handle, "_EJ0"))
1505 		return false;
1506 	if (acpi_ata_match(handle))
1507 		return true;
1508 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1509 		return false;
1510 
1511 	return acpi_ata_match(phandle);
1512 }
1513 
1514 /*
1515  * acpi_dock_match - see if an acpi object has a _DCK method
1516  */
1517 bool acpi_dock_match(acpi_handle handle)
1518 {
1519 	return acpi_has_method(handle, "_DCK");
1520 }
1521 
1522 const char *acpi_device_hid(struct acpi_device *device)
1523 {
1524 	struct acpi_hardware_id *hid;
1525 
1526 	if (list_empty(&device->pnp.ids))
1527 		return dummy_hid;
1528 
1529 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1530 	return hid->id;
1531 }
1532 EXPORT_SYMBOL(acpi_device_hid);
1533 
1534 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1535 {
1536 	struct acpi_hardware_id *id;
1537 
1538 	id = kmalloc(sizeof(*id), GFP_KERNEL);
1539 	if (!id)
1540 		return;
1541 
1542 	id->id = kstrdup(dev_id, GFP_KERNEL);
1543 	if (!id->id) {
1544 		kfree(id);
1545 		return;
1546 	}
1547 
1548 	list_add_tail(&id->list, &pnp->ids);
1549 	pnp->type.hardware_id = 1;
1550 }
1551 
1552 /*
1553  * Old IBM workstations have a DSDT bug wherein the SMBus object
1554  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1555  * prefix.  Work around this.
1556  */
1557 static bool acpi_ibm_smbus_match(acpi_handle handle)
1558 {
1559 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1560 	struct acpi_buffer path = { sizeof(node_name), node_name };
1561 
1562 	if (!dmi_name_in_vendors("IBM"))
1563 		return false;
1564 
1565 	/* Look for SMBS object */
1566 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1567 	    strcmp("SMBS", path.pointer))
1568 		return false;
1569 
1570 	/* Does it have the necessary (but misnamed) methods? */
1571 	if (acpi_has_method(handle, "SBI") &&
1572 	    acpi_has_method(handle, "SBR") &&
1573 	    acpi_has_method(handle, "SBW"))
1574 		return true;
1575 
1576 	return false;
1577 }
1578 
1579 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1580 				int device_type)
1581 {
1582 	acpi_status status;
1583 	struct acpi_device_info *info;
1584 	struct acpi_pnp_device_id_list *cid_list;
1585 	int i;
1586 
1587 	switch (device_type) {
1588 	case ACPI_BUS_TYPE_DEVICE:
1589 		if (handle == ACPI_ROOT_OBJECT) {
1590 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1591 			break;
1592 		}
1593 
1594 		status = acpi_get_object_info(handle, &info);
1595 		if (ACPI_FAILURE(status)) {
1596 			pr_err(PREFIX "%s: Error reading device info\n",
1597 					__func__);
1598 			return;
1599 		}
1600 
1601 		if (info->valid & ACPI_VALID_HID)
1602 			acpi_add_id(pnp, info->hardware_id.string);
1603 		if (info->valid & ACPI_VALID_CID) {
1604 			cid_list = &info->compatible_id_list;
1605 			for (i = 0; i < cid_list->count; i++)
1606 				acpi_add_id(pnp, cid_list->ids[i].string);
1607 		}
1608 		if (info->valid & ACPI_VALID_ADR) {
1609 			pnp->bus_address = info->address;
1610 			pnp->type.bus_address = 1;
1611 		}
1612 		if (info->valid & ACPI_VALID_UID)
1613 			pnp->unique_id = kstrdup(info->unique_id.string,
1614 							GFP_KERNEL);
1615 
1616 		kfree(info);
1617 
1618 		/*
1619 		 * Some devices don't reliably have _HIDs & _CIDs, so add
1620 		 * synthetic HIDs to make sure drivers can find them.
1621 		 */
1622 		if (acpi_is_video_device(handle))
1623 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1624 		else if (acpi_bay_match(handle))
1625 			acpi_add_id(pnp, ACPI_BAY_HID);
1626 		else if (acpi_dock_match(handle))
1627 			acpi_add_id(pnp, ACPI_DOCK_HID);
1628 		else if (acpi_ibm_smbus_match(handle))
1629 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1630 		else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1631 			acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1632 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1633 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1634 		}
1635 
1636 		break;
1637 	case ACPI_BUS_TYPE_POWER:
1638 		acpi_add_id(pnp, ACPI_POWER_HID);
1639 		break;
1640 	case ACPI_BUS_TYPE_PROCESSOR:
1641 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1642 		break;
1643 	case ACPI_BUS_TYPE_THERMAL:
1644 		acpi_add_id(pnp, ACPI_THERMAL_HID);
1645 		break;
1646 	case ACPI_BUS_TYPE_POWER_BUTTON:
1647 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1648 		break;
1649 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1650 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1651 		break;
1652 	}
1653 }
1654 
1655 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1656 {
1657 	struct acpi_hardware_id *id, *tmp;
1658 
1659 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1660 		kfree(id->id);
1661 		kfree(id);
1662 	}
1663 	kfree(pnp->unique_id);
1664 }
1665 
1666 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1667 			     int type, unsigned long long sta)
1668 {
1669 	INIT_LIST_HEAD(&device->pnp.ids);
1670 	device->device_type = type;
1671 	device->handle = handle;
1672 	device->parent = acpi_bus_get_parent(handle);
1673 	STRUCT_TO_INT(device->status) = sta;
1674 	acpi_device_get_busid(device);
1675 	acpi_set_pnp_ids(handle, &device->pnp, type);
1676 	acpi_bus_get_flags(device);
1677 	device->flags.match_driver = false;
1678 	device_initialize(&device->dev);
1679 	dev_set_uevent_suppress(&device->dev, true);
1680 }
1681 
1682 void acpi_device_add_finalize(struct acpi_device *device)
1683 {
1684 	dev_set_uevent_suppress(&device->dev, false);
1685 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1686 }
1687 
1688 static int acpi_add_single_object(struct acpi_device **child,
1689 				  acpi_handle handle, int type,
1690 				  unsigned long long sta)
1691 {
1692 	int result;
1693 	struct acpi_device *device;
1694 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1695 
1696 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1697 	if (!device) {
1698 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1699 		return -ENOMEM;
1700 	}
1701 
1702 	acpi_init_device_object(device, handle, type, sta);
1703 	acpi_bus_get_power_flags(device);
1704 	acpi_bus_get_wakeup_device_flags(device);
1705 
1706 	result = acpi_device_add(device, acpi_device_release);
1707 	if (result) {
1708 		acpi_device_release(&device->dev);
1709 		return result;
1710 	}
1711 
1712 	acpi_power_add_remove_device(device, true);
1713 	acpi_device_add_finalize(device);
1714 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1715 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1716 		dev_name(&device->dev), (char *) buffer.pointer,
1717 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
1718 	kfree(buffer.pointer);
1719 	*child = device;
1720 	return 0;
1721 }
1722 
1723 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1724 				    unsigned long long *sta)
1725 {
1726 	acpi_status status;
1727 	acpi_object_type acpi_type;
1728 
1729 	status = acpi_get_type(handle, &acpi_type);
1730 	if (ACPI_FAILURE(status))
1731 		return -ENODEV;
1732 
1733 	switch (acpi_type) {
1734 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1735 	case ACPI_TYPE_DEVICE:
1736 		*type = ACPI_BUS_TYPE_DEVICE;
1737 		status = acpi_bus_get_status_handle(handle, sta);
1738 		if (ACPI_FAILURE(status))
1739 			return -ENODEV;
1740 		break;
1741 	case ACPI_TYPE_PROCESSOR:
1742 		*type = ACPI_BUS_TYPE_PROCESSOR;
1743 		status = acpi_bus_get_status_handle(handle, sta);
1744 		if (ACPI_FAILURE(status))
1745 			return -ENODEV;
1746 		break;
1747 	case ACPI_TYPE_THERMAL:
1748 		*type = ACPI_BUS_TYPE_THERMAL;
1749 		*sta = ACPI_STA_DEFAULT;
1750 		break;
1751 	case ACPI_TYPE_POWER:
1752 		*type = ACPI_BUS_TYPE_POWER;
1753 		*sta = ACPI_STA_DEFAULT;
1754 		break;
1755 	default:
1756 		return -ENODEV;
1757 	}
1758 
1759 	return 0;
1760 }
1761 
1762 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1763 				       char *idstr,
1764 				       const struct acpi_device_id **matchid)
1765 {
1766 	const struct acpi_device_id *devid;
1767 
1768 	for (devid = handler->ids; devid->id[0]; devid++)
1769 		if (!strcmp((char *)devid->id, idstr)) {
1770 			if (matchid)
1771 				*matchid = devid;
1772 
1773 			return true;
1774 		}
1775 
1776 	return false;
1777 }
1778 
1779 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1780 					const struct acpi_device_id **matchid)
1781 {
1782 	struct acpi_scan_handler *handler;
1783 
1784 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1785 		if (acpi_scan_handler_matching(handler, idstr, matchid))
1786 			return handler;
1787 
1788 	return NULL;
1789 }
1790 
1791 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1792 {
1793 	if (!!hotplug->enabled == !!val)
1794 		return;
1795 
1796 	mutex_lock(&acpi_scan_lock);
1797 
1798 	hotplug->enabled = val;
1799 
1800 	mutex_unlock(&acpi_scan_lock);
1801 }
1802 
1803 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1804 {
1805 	struct acpi_device_pnp pnp = {};
1806 	struct acpi_hardware_id *hwid;
1807 	struct acpi_scan_handler *handler;
1808 
1809 	INIT_LIST_HEAD(&pnp.ids);
1810 	acpi_set_pnp_ids(handle, &pnp, type);
1811 
1812 	if (!pnp.type.hardware_id)
1813 		goto out;
1814 
1815 	/*
1816 	 * This relies on the fact that acpi_install_notify_handler() will not
1817 	 * install the same notify handler routine twice for the same handle.
1818 	 */
1819 	list_for_each_entry(hwid, &pnp.ids, list) {
1820 		handler = acpi_scan_match_handler(hwid->id, NULL);
1821 		if (handler) {
1822 			acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1823 					acpi_hotplug_notify_cb, handler);
1824 			break;
1825 		}
1826 	}
1827 
1828 out:
1829 	acpi_free_pnp_ids(&pnp);
1830 }
1831 
1832 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1833 				      void *not_used, void **return_value)
1834 {
1835 	struct acpi_device *device = NULL;
1836 	int type;
1837 	unsigned long long sta;
1838 	int result;
1839 
1840 	acpi_bus_get_device(handle, &device);
1841 	if (device)
1842 		goto out;
1843 
1844 	result = acpi_bus_type_and_status(handle, &type, &sta);
1845 	if (result)
1846 		return AE_OK;
1847 
1848 	if (type == ACPI_BUS_TYPE_POWER) {
1849 		acpi_add_power_resource(handle);
1850 		return AE_OK;
1851 	}
1852 
1853 	acpi_scan_init_hotplug(handle, type);
1854 
1855 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1856 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1857 		struct acpi_device_wakeup wakeup;
1858 
1859 		if (acpi_has_method(handle, "_PRW")) {
1860 			acpi_bus_extract_wakeup_device_power_package(handle,
1861 								     &wakeup);
1862 			acpi_power_resources_list_free(&wakeup.resources);
1863 		}
1864 		return AE_CTRL_DEPTH;
1865 	}
1866 
1867 	acpi_add_single_object(&device, handle, type, sta);
1868 	if (!device)
1869 		return AE_CTRL_DEPTH;
1870 
1871  out:
1872 	if (!*return_value)
1873 		*return_value = device;
1874 
1875 	return AE_OK;
1876 }
1877 
1878 static int acpi_scan_attach_handler(struct acpi_device *device)
1879 {
1880 	struct acpi_hardware_id *hwid;
1881 	int ret = 0;
1882 
1883 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1884 		const struct acpi_device_id *devid;
1885 		struct acpi_scan_handler *handler;
1886 
1887 		handler = acpi_scan_match_handler(hwid->id, &devid);
1888 		if (handler) {
1889 			ret = handler->attach(device, devid);
1890 			if (ret > 0) {
1891 				device->handler = handler;
1892 				break;
1893 			} else if (ret < 0) {
1894 				break;
1895 			}
1896 		}
1897 	}
1898 	return ret;
1899 }
1900 
1901 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1902 					  void *not_used, void **ret_not_used)
1903 {
1904 	struct acpi_device *device;
1905 	unsigned long long sta_not_used;
1906 	int ret;
1907 
1908 	/*
1909 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1910 	 * namespace walks prematurely.
1911 	 */
1912 	if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1913 		return AE_OK;
1914 
1915 	if (acpi_bus_get_device(handle, &device))
1916 		return AE_CTRL_DEPTH;
1917 
1918 	if (device->handler)
1919 		return AE_OK;
1920 
1921 	ret = acpi_scan_attach_handler(device);
1922 	if (ret < 0)
1923 		return AE_CTRL_DEPTH;
1924 
1925 	device->flags.match_driver = true;
1926 	if (ret > 0)
1927 		return AE_OK;
1928 
1929 	ret = device_attach(&device->dev);
1930 	return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1931 }
1932 
1933 /**
1934  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1935  * @handle: Root of the namespace scope to scan.
1936  *
1937  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1938  * found devices.
1939  *
1940  * If no devices were found, -ENODEV is returned, but it does not mean that
1941  * there has been a real error.  There just have been no suitable ACPI objects
1942  * in the table trunk from which the kernel could create a device and add an
1943  * appropriate driver.
1944  *
1945  * Must be called under acpi_scan_lock.
1946  */
1947 int acpi_bus_scan(acpi_handle handle)
1948 {
1949 	void *device = NULL;
1950 	int error = 0;
1951 
1952 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1953 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1954 				    acpi_bus_check_add, NULL, NULL, &device);
1955 
1956 	if (!device)
1957 		error = -ENODEV;
1958 	else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1959 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1960 				    acpi_bus_device_attach, NULL, NULL, NULL);
1961 
1962 	return error;
1963 }
1964 EXPORT_SYMBOL(acpi_bus_scan);
1965 
1966 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1967 					  void *not_used, void **ret_not_used)
1968 {
1969 	struct acpi_device *device = NULL;
1970 
1971 	if (!acpi_bus_get_device(handle, &device)) {
1972 		struct acpi_scan_handler *dev_handler = device->handler;
1973 
1974 		if (dev_handler) {
1975 			if (dev_handler->detach)
1976 				dev_handler->detach(device);
1977 
1978 			device->handler = NULL;
1979 		} else {
1980 			device_release_driver(&device->dev);
1981 		}
1982 	}
1983 	return AE_OK;
1984 }
1985 
1986 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1987 				   void *not_used, void **ret_not_used)
1988 {
1989 	struct acpi_device *device = NULL;
1990 
1991 	if (!acpi_bus_get_device(handle, &device))
1992 		acpi_device_unregister(device);
1993 
1994 	return AE_OK;
1995 }
1996 
1997 /**
1998  * acpi_bus_trim - Remove ACPI device node and all of its descendants
1999  * @start: Root of the ACPI device nodes subtree to remove.
2000  *
2001  * Must be called under acpi_scan_lock.
2002  */
2003 void acpi_bus_trim(struct acpi_device *start)
2004 {
2005 	/*
2006 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
2007 	 * all ACPI drivers from the device nodes being removed.
2008 	 */
2009 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2010 			    acpi_bus_device_detach, NULL, NULL);
2011 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2012 	/*
2013 	 * Execute acpi_bus_remove() as a post-order callback to remove device
2014 	 * nodes in the given namespace scope.
2015 	 */
2016 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2017 			    acpi_bus_remove, NULL, NULL);
2018 	acpi_bus_remove(start->handle, 0, NULL, NULL);
2019 }
2020 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2021 
2022 static int acpi_bus_scan_fixed(void)
2023 {
2024 	int result = 0;
2025 
2026 	/*
2027 	 * Enumerate all fixed-feature devices.
2028 	 */
2029 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2030 		struct acpi_device *device = NULL;
2031 
2032 		result = acpi_add_single_object(&device, NULL,
2033 						ACPI_BUS_TYPE_POWER_BUTTON,
2034 						ACPI_STA_DEFAULT);
2035 		if (result)
2036 			return result;
2037 
2038 		result = device_attach(&device->dev);
2039 		if (result < 0)
2040 			return result;
2041 
2042 		device_init_wakeup(&device->dev, true);
2043 	}
2044 
2045 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2046 		struct acpi_device *device = NULL;
2047 
2048 		result = acpi_add_single_object(&device, NULL,
2049 						ACPI_BUS_TYPE_SLEEP_BUTTON,
2050 						ACPI_STA_DEFAULT);
2051 		if (result)
2052 			return result;
2053 
2054 		result = device_attach(&device->dev);
2055 	}
2056 
2057 	return result < 0 ? result : 0;
2058 }
2059 
2060 int __init acpi_scan_init(void)
2061 {
2062 	int result;
2063 
2064 	result = bus_register(&acpi_bus_type);
2065 	if (result) {
2066 		/* We don't want to quit even if we failed to add suspend/resume */
2067 		printk(KERN_ERR PREFIX "Could not register bus type\n");
2068 	}
2069 
2070 	acpi_pci_root_init();
2071 	acpi_pci_link_init();
2072 	acpi_processor_init();
2073 	acpi_platform_init();
2074 	acpi_lpss_init();
2075 	acpi_cmos_rtc_init();
2076 	acpi_container_init();
2077 	acpi_memory_hotplug_init();
2078 	acpi_dock_init();
2079 
2080 	mutex_lock(&acpi_scan_lock);
2081 	/*
2082 	 * Enumerate devices in the ACPI namespace.
2083 	 */
2084 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2085 	if (result)
2086 		goto out;
2087 
2088 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2089 	if (result)
2090 		goto out;
2091 
2092 	result = acpi_bus_scan_fixed();
2093 	if (result) {
2094 		acpi_device_unregister(acpi_root);
2095 		goto out;
2096 	}
2097 
2098 	acpi_update_all_gpes();
2099 
2100 	acpi_pci_root_hp_init();
2101 
2102  out:
2103 	mutex_unlock(&acpi_scan_lock);
2104 	return result;
2105 }
2106