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