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