scan.c 56.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/*
 * scan.c - support for transforming the ACPI namespace into individual objects
 */

#include <linux/module.h>
#include <linux/init.h>
7
#include <linux/slab.h>
8
#include <linux/kernel.h>
Linus Torvalds's avatar
Linus Torvalds committed
9
#include <linux/acpi.h>
10
#include <linux/acpi_iort.h>
11 12
#include <linux/signal.h>
#include <linux/kthread.h>
13
#include <linux/dmi.h>
14
#include <linux/nls.h>
15
#include <linux/dma-mapping.h>
16
#include <linux/platform_data/x86/apple.h>
Linus Torvalds's avatar
Linus Torvalds committed
17

18 19
#include <asm/pgtable.h>

20 21
#include "internal.h"

Linus Torvalds's avatar
Linus Torvalds committed
22
#define _COMPONENT		ACPI_BUS_COMPONENT
23
ACPI_MODULE_NAME("scan");
Len Brown's avatar
Len Brown committed
24
extern struct acpi_device *acpi_root;
Linus Torvalds's avatar
Linus Torvalds committed
25 26

#define ACPI_BUS_CLASS			"system_bus"
27
#define ACPI_BUS_HID			"LNXSYBUS"
Linus Torvalds's avatar
Linus Torvalds committed
28 29
#define ACPI_BUS_DEVICE_NAME		"System Bus"

30 31
#define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)

32 33
#define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)

34
static const char *dummy_hid = "device";
35

36 37
static LIST_HEAD(acpi_dep_list);
static DEFINE_MUTEX(acpi_dep_list_lock);
38
LIST_HEAD(acpi_bus_id_list);
39
static DEFINE_MUTEX(acpi_scan_lock);
40
static LIST_HEAD(acpi_scan_handlers_list);
41
DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
42
LIST_HEAD(acpi_wakeup_device_list);
43
static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds's avatar
Linus Torvalds committed
44

45 46 47 48 49 50 51
/*
 * The UART device described by the SPCR table is the only object which needs
 * special-casing. Everything else is covered by ACPI namespace paths in STAO
 * table.
 */
static u64 spcr_uart_addr;

52 53 54 55 56 57
struct acpi_dep_data {
	struct list_head node;
	acpi_handle master;
	acpi_handle slave;
};

58 59 60 61 62 63 64 65 66 67 68 69
void acpi_scan_lock_acquire(void)
{
	mutex_lock(&acpi_scan_lock);
}
EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);

void acpi_scan_lock_release(void)
{
	mutex_unlock(&acpi_scan_lock);
}
EXPORT_SYMBOL_GPL(acpi_scan_lock_release);

70 71 72 73 74 75 76 77 78 79
void acpi_lock_hp_context(void)
{
	mutex_lock(&acpi_hp_context_lock);
}

void acpi_unlock_hp_context(void)
{
	mutex_unlock(&acpi_hp_context_lock);
}

80 81 82 83 84 85
void 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))
{
	acpi_lock_hp_context();
86 87 88
	hp->notify = notify;
	hp->uevent = uevent;
	acpi_set_hp_context(adev, hp);
89 90 91 92
	acpi_unlock_hp_context();
}
EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);

93 94
int acpi_scan_add_handler(struct acpi_scan_handler *handler)
{
95
	if (!handler)
96 97 98 99 100 101
		return -EINVAL;

	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
	return 0;
}

102 103 104 105 106 107 108 109 110 111 112 113 114
int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
				       const char *hotplug_profile_name)
{
	int error;

	error = acpi_scan_add_handler(handler);
	if (error)
		return error;

	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
	return 0;
}

115
bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
116 117 118 119
{
	struct acpi_device_physical_node *pn;
	bool offline = true;

120 121 122 123 124
	/*
	 * acpi_container_offline() calls this for all of the container's
	 * children under the container's physical_node_lock lock.
	 */
	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
125 126 127

	list_for_each_entry(pn, &adev->physical_node_list, node)
		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
128 129 130
			if (uevent)
				kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);

131 132 133 134 135 136 137 138
			offline = false;
			break;
		}

	mutex_unlock(&adev->physical_node_lock);
	return offline;
}

139 140
static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
				    void **ret_p)
141 142 143
{
	struct acpi_device *device = NULL;
	struct acpi_device_physical_node *pn;
144
	bool second_pass = (bool)data;
145 146 147 148 149
	acpi_status status = AE_OK;

	if (acpi_bus_get_device(handle, &device))
		return AE_OK;

150 151 152 153 154
	if (device->handler && !device->handler->hotplug.enabled) {
		*ret_p = &device->dev;
		return AE_SUPPORT;
	}

155 156 157 158 159
	mutex_lock(&device->physical_node_lock);

	list_for_each_entry(pn, &device->physical_node_list, node) {
		int ret;

160 161 162 163 164 165 166
		if (second_pass) {
			/* Skip devices offlined by the first pass. */
			if (pn->put_online)
				continue;
		} else {
			pn->put_online = false;
		}
167
		ret = device_offline(pn->dev);
168 169 170 171 172 173 174 175
		if (ret >= 0) {
			pn->put_online = !ret;
		} else {
			*ret_p = pn->dev;
			if (second_pass) {
				status = AE_ERROR;
				break;
			}
176 177 178 179 180 181 182 183
		}
	}

	mutex_unlock(&device->physical_node_lock);

	return status;
}

184 185
static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
				   void **ret_p)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
	struct acpi_device *device = NULL;
	struct acpi_device_physical_node *pn;

	if (acpi_bus_get_device(handle, &device))
		return AE_OK;

	mutex_lock(&device->physical_node_lock);

	list_for_each_entry(pn, &device->physical_node_list, node)
		if (pn->put_online) {
			device_online(pn->dev);
			pn->put_online = false;
		}

	mutex_unlock(&device->physical_node_lock);

	return AE_OK;
}

206
static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
207
{
208
	acpi_handle handle = device->handle;
209
	struct device *errdev = NULL;
210
	acpi_status status;
211

212 213 214 215 216 217 218 219 220
	/*
	 * Carry out two passes here and ignore errors in the first pass,
	 * because if the devices in question are memory blocks and
	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
	 * that the other blocks depend on, but it is not known in advance which
	 * block holds them.
	 *
	 * If the first pass is successful, the second one isn't needed, though.
	 */
221 222 223 224 225 226 227 228 229 230
	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
				     NULL, acpi_bus_offline, (void *)false,
				     (void **)&errdev);
	if (status == AE_SUPPORT) {
		dev_warn(errdev, "Offline disabled.\n");
		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
				    acpi_bus_online, NULL, NULL, NULL);
		return -EPERM;
	}
	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
231 232
	if (errdev) {
		errdev = NULL;
233
		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
234 235
				    NULL, acpi_bus_offline, (void *)true,
				    (void **)&errdev);
236
		if (!errdev)
237 238
			acpi_bus_offline(handle, 0, (void *)true,
					 (void **)&errdev);
239

240
		if (errdev) {
241
			dev_warn(errdev, "Offline failed.\n");
242
			acpi_bus_online(handle, 0, NULL, NULL);
243
			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
244 245
					    ACPI_UINT32_MAX, acpi_bus_online,
					    NULL, NULL, NULL);
246 247
			return -EBUSY;
		}
248
	}
249 250 251 252 253 254 255 256 257
	return 0;
}

static int acpi_scan_hot_remove(struct acpi_device *device)
{
	acpi_handle handle = device->handle;
	unsigned long long sta;
	acpi_status status;

258
	if (device->handler && device->handler->hotplug.demand_offline) {
259
		if (!acpi_scan_is_offline(device, true))
260 261 262 263 264 265
			return -EBUSY;
	} else {
		int error = acpi_scan_try_to_offline(device);
		if (error)
			return error;
	}
266

267
	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
268
		"Hot-removing device %s...\n", dev_name(&device->dev)));
269

270
	acpi_bus_trim(device);
271

272
	acpi_evaluate_lck(handle, 0);
Linus Torvalds's avatar
Linus Torvalds committed
273 274 275
	/*
	 * TBD: _EJD support.
	 */
276 277 278 279 280
	status = acpi_evaluate_ej0(handle);
	if (status == AE_NOT_FOUND)
		return -ENODEV;
	else if (ACPI_FAILURE(status))
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
281

282 283 284 285 286 287 288 289 290 291 292 293 294
	/*
	 * Verify if eject was indeed successful.  If not, log an error
	 * message.  No need to call _OST since _EJ0 call was made OK.
	 */
	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
	if (ACPI_FAILURE(status)) {
		acpi_handle_warn(handle,
			"Status check after eject failed (0x%x)\n", status);
	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
		acpi_handle_warn(handle,
			"Eject incomplete - status 0x%llx\n", sta);
	}

295 296
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
297

298 299 300 301 302 303 304 305 306 307
static int acpi_scan_device_not_present(struct acpi_device *adev)
{
	if (!acpi_device_enumerated(adev)) {
		dev_warn(&adev->dev, "Still not present\n");
		return -EALREADY;
	}
	acpi_bus_trim(adev);
	return 0;
}

308
static int acpi_scan_device_check(struct acpi_device *adev)
309
{
310
	int error;
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	acpi_bus_get_status(adev);
	if (adev->status.present || adev->status.functional) {
		/*
		 * This function is only called for device objects for which
		 * matching scan handlers exist.  The only situation in which
		 * the scan handler is not attached to this device object yet
		 * is when the device has just appeared (either it wasn't
		 * present at all before or it was removed and then added
		 * again).
		 */
		if (adev->handler) {
			dev_warn(&adev->dev, "Already enumerated\n");
			return -EALREADY;
		}
		error = acpi_bus_scan(adev->handle);
		if (error) {
			dev_warn(&adev->dev, "Namespace scan failure\n");
			return error;
		}
		if (!adev->handler) {
			dev_warn(&adev->dev, "Enumeration failure\n");
			error = -ENODEV;
		}
	} else {
		error = acpi_scan_device_not_present(adev);
	}
	return error;
}

static int acpi_scan_bus_check(struct acpi_device *adev)
{
	struct acpi_scan_handler *handler = adev->handler;
	struct acpi_device *child;
	int error;

	acpi_bus_get_status(adev);
	if (!(adev->status.present || adev->status.functional)) {
		acpi_scan_device_not_present(adev);
		return 0;
	}
	if (handler && handler->hotplug.scan_dependent)
		return handler->hotplug.scan_dependent(adev);

355 356 357 358 359
	error = acpi_bus_scan(adev->handle);
	if (error) {
		dev_warn(&adev->dev, "Namespace scan failure\n");
		return error;
	}
360 361 362 363
	list_for_each_entry(child, &adev->children, node) {
		error = acpi_scan_bus_check(child);
		if (error)
			return error;
364 365
	}
	return 0;
366 367
}

368 369 370 371 372 373 374 375 376
static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
{
	switch (type) {
	case ACPI_NOTIFY_BUS_CHECK:
		return acpi_scan_bus_check(adev);
	case ACPI_NOTIFY_DEVICE_CHECK:
		return acpi_scan_device_check(adev);
	case ACPI_NOTIFY_EJECT_REQUEST:
	case ACPI_OST_EC_OSPM_EJECT:
377 378 379 380
		if (adev->handler && !adev->handler->hotplug.enabled) {
			dev_info(&adev->dev, "Eject disabled\n");
			return -EPERM;
		}
381 382
		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
383 384 385 386 387
		return acpi_scan_hot_remove(adev);
	}
	return -EINVAL;
}

388
void acpi_device_hotplug(struct acpi_device *adev, u32 src)
389 390
{
	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
391
	int error = -ENODEV;
392

393
	lock_device_hotplug();
394
	mutex_lock(&acpi_scan_lock);
395

396 397
	/*
	 * The device object's ACPI handle cannot become invalid as long as we
398
	 * are holding acpi_scan_lock, but it might have become invalid before
399 400 401
	 * that lock was acquired.
	 */
	if (adev->handle == INVALID_ACPI_HANDLE)
402
		goto err_out;
403

404 405 406
	if (adev->flags.is_dock_station) {
		error = dock_notify(adev, src);
	} else if (adev->flags.hotplug_notify) {
407 408
		error = acpi_generic_hotplug_event(adev, src);
	} else {
409
		int (*notify)(struct acpi_device *, u32);
410 411

		acpi_lock_hp_context();
412
		notify = adev->hp ? adev->hp->notify : NULL;
413 414 415 416 417
		acpi_unlock_hp_context();
		/*
		 * There may be additional notify handlers for device objects
		 * without the .event() callback, so ignore them here.
		 */
418 419
		if (notify)
			error = notify(adev, src);
420 421
		else
			goto out;
422
	}
423 424
	switch (error) {
	case 0:
425
		ost_code = ACPI_OST_SC_SUCCESS;
426 427 428 429 430 431 432 433 434 435 436
		break;
	case -EPERM:
		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
		break;
	case -EBUSY:
		ost_code = ACPI_OST_SC_DEVICE_BUSY;
		break;
	default:
		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
		break;
	}
437

438
 err_out:
439
	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
440 441

 out:
442
	acpi_bus_put_acpi_device(adev);
443
	mutex_unlock(&acpi_scan_lock);
444
	unlock_device_hotplug();
445 446
}

447 448 449 450
static void acpi_free_power_resources_lists(struct acpi_device *device)
{
	int i;

451 452 453
	if (device->wakeup.flags.valid)
		acpi_power_resources_list_free(&device->wakeup.resources);

454
	if (!device->power.flags.power_resources)
455 456 457 458 459 460
		return;

	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
		struct acpi_device_power_state *ps = &device->power.states[i];
		acpi_power_resources_list_free(&ps->resources);
	}
461 462
}

463
static void acpi_device_release(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
464
{
465
	struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds's avatar
Linus Torvalds committed
466

467
	acpi_free_properties(acpi_dev);
468
	acpi_free_pnp_ids(&acpi_dev->pnp);
469
	acpi_free_power_resources_lists(acpi_dev);
470
	kfree(acpi_dev);
Linus Torvalds's avatar
Linus Torvalds committed
471 472
}

473 474
static void acpi_device_del(struct acpi_device *device)
{
475 476
	struct acpi_device_bus_id *acpi_device_bus_id;

477 478 479 480
	mutex_lock(&acpi_device_lock);
	if (device->parent)
		list_del(&device->node);

481 482 483 484 485 486 487 488 489 490 491 492
	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
		if (!strcmp(acpi_device_bus_id->bus_id,
			    acpi_device_hid(device))) {
			if (acpi_device_bus_id->instance_no > 0)
				acpi_device_bus_id->instance_no--;
			else {
				list_del(&acpi_device_bus_id->node);
				kfree(acpi_device_bus_id);
			}
			break;
		}

493 494 495 496 497 498 499 500 501 502 503
	list_del(&device->wakeup_list);
	mutex_unlock(&acpi_device_lock);

	acpi_power_add_remove_device(device, false);
	acpi_device_remove_files(device);
	if (device->remove)
		device->remove(device);

	device_del(&device->dev);
}

504 505
static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static LIST_HEAD(acpi_device_del_list);
static DEFINE_MUTEX(acpi_device_del_lock);

static void acpi_device_del_work_fn(struct work_struct *work_not_used)
{
	for (;;) {
		struct acpi_device *adev;

		mutex_lock(&acpi_device_del_lock);

		if (list_empty(&acpi_device_del_list)) {
			mutex_unlock(&acpi_device_del_lock);
			break;
		}
		adev = list_first_entry(&acpi_device_del_list,
					struct acpi_device, del_list);
		list_del(&adev->del_list);

		mutex_unlock(&acpi_device_del_lock);

526 527 528
		blocking_notifier_call_chain(&acpi_reconfig_chain,
					     ACPI_RECONFIG_DEVICE_REMOVE, adev);

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
		acpi_device_del(adev);
		/*
		 * Drop references to all power resources that might have been
		 * used by the device.
		 */
		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
		put_device(&adev->dev);
	}
}

/**
 * acpi_scan_drop_device - Drop an ACPI device object.
 * @handle: Handle of an ACPI namespace node, not used.
 * @context: Address of the ACPI device object to drop.
 *
 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
 * namespace node the device object pointed to by @context is attached to.
 *
 * The unregistration is carried out asynchronously to avoid running
 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
 * ensure the correct ordering (the device objects must be unregistered in the
 * same order in which the corresponding namespace nodes are deleted).
 */
static void acpi_scan_drop_device(acpi_handle handle, void *context)
553
{
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	static DECLARE_WORK(work, acpi_device_del_work_fn);
	struct acpi_device *adev = context;

	mutex_lock(&acpi_device_del_lock);

	/*
	 * Use the ACPI hotplug workqueue which is ordered, so this work item
	 * won't run after any hotplug work items submitted subsequently.  That
	 * prevents attempts to register device objects identical to those being
	 * deleted from happening concurrently (such attempts result from
	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
	 * run after all of the work items submitted previosuly, which helps
	 * those work items to ensure that they are not accessing stale device
	 * objects.
	 */
	if (list_empty(&acpi_device_del_list))
		acpi_queue_hotplug_work(&work);

	list_add_tail(&adev->del_list, &acpi_device_del_list);
	/* Make acpi_ns_validate_handle() return NULL for this handle. */
	adev->handle = INVALID_ACPI_HANDLE;

	mutex_unlock(&acpi_device_del_lock);
577 578
}

579 580
static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
				void (*callback)(void *))
581 582 583 584 585 586
{
	acpi_status status;

	if (!device)
		return -EINVAL;

587 588
	status = acpi_get_data_full(handle, acpi_scan_drop_device,
				    (void **)device, callback);
589 590 591 592 593 594 595
	if (ACPI_FAILURE(status) || !*device) {
		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
				  handle));
		return -ENODEV;
	}
	return 0;
}
596 597 598 599 600

int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
{
	return acpi_get_device_data(handle, device, NULL);
}
601
EXPORT_SYMBOL(acpi_bus_get_device);
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
static void get_acpi_device(void *dev)
{
	if (dev)
		get_device(&((struct acpi_device *)dev)->dev);
}

struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
{
	struct acpi_device *adev = NULL;

	acpi_get_device_data(handle, &adev, get_acpi_device);
	return adev;
}

void acpi_bus_put_acpi_device(struct acpi_device *adev)
{
	put_device(&adev->dev);
}

622 623
int acpi_device_add(struct acpi_device *device,
		    void (*release)(struct device *))
Linus Torvalds's avatar
Linus Torvalds committed
624
{
Len Brown's avatar
Len Brown committed
625
	int result;
626 627
	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
	int found = 0;
628

629 630 631
	if (device->handle) {
		acpi_status status;

632
		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
633 634 635 636 637 638 639 640
					  device);
		if (ACPI_FAILURE(status)) {
			acpi_handle_err(device->handle,
					"Unable to attach device data\n");
			return -ENODEV;
		}
	}

Zhang Rui's avatar
Zhang Rui committed
641 642 643 644 645 646 647 648
	/*
	 * Linkage
	 * -------
	 * Link this device to its parent and siblings.
	 */
	INIT_LIST_HEAD(&device->children);
	INIT_LIST_HEAD(&device->node);
	INIT_LIST_HEAD(&device->wakeup_list);
649
	INIT_LIST_HEAD(&device->physical_node_list);
650
	INIT_LIST_HEAD(&device->del_list);
651
	mutex_init(&device->physical_node_lock);
Linus Torvalds's avatar
Linus Torvalds committed
652

653 654
	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
	if (!new_bus_id) {
655 656 657
		pr_err(PREFIX "Memory allocation error\n");
		result = -ENOMEM;
		goto err_detach;
Linus Torvalds's avatar
Linus Torvalds committed
658
	}
659

660
	mutex_lock(&acpi_device_lock);
661 662 663 664 665
	/*
	 * Find suitable bus_id and instance number in acpi_bus_id_list
	 * If failed, create one and link it into acpi_bus_id_list
	 */
	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
666 667 668
		if (!strcmp(acpi_device_bus_id->bus_id,
			    acpi_device_hid(device))) {
			acpi_device_bus_id->instance_no++;
669 670 671 672
			found = 1;
			kfree(new_bus_id);
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
673
	}
674
	if (!found) {
675
		acpi_device_bus_id = new_bus_id;
676
		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
677 678
		acpi_device_bus_id->instance_no = 0;
		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
Linus Torvalds's avatar
Linus Torvalds committed
679
	}
680
	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Linus Torvalds's avatar
Linus Torvalds committed
681

Len Brown's avatar
Len Brown committed
682
	if (device->parent)
Zhang Rui's avatar
Zhang Rui committed
683
		list_add_tail(&device->node, &device->parent->children);
Len Brown's avatar
Len Brown committed
684

Zhang Rui's avatar
Zhang Rui committed
685 686
	if (device->wakeup.flags.valid)
		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
687
	mutex_unlock(&acpi_device_lock);
Linus Torvalds's avatar
Linus Torvalds committed
688

689
	if (device->parent)
690
		device->dev.parent = &device->parent->dev;
691
	device->dev.bus = &acpi_bus_type;
692
	device->dev.release = release;
693
	result = device_add(&device->dev);
694
	if (result) {
695
		dev_err(&device->dev, "Error registering device\n");
696
		goto err;
Linus Torvalds's avatar
Linus Torvalds committed
697 698
	}

699
	result = acpi_device_setup_files(device);
700
	if (result)
701 702
		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
		       dev_name(&device->dev));
Linus Torvalds's avatar
Linus Torvalds committed
703 704

	return 0;
705 706

 err:
707
	mutex_lock(&acpi_device_lock);
Len Brown's avatar
Len Brown committed
708
	if (device->parent)
709 710
		list_del(&device->node);
	list_del(&device->wakeup_list);
711
	mutex_unlock(&acpi_device_lock);
712 713

 err_detach:
714
	acpi_detach_data(device->handle, acpi_scan_drop_device);
715
	return result;
Linus Torvalds's avatar
Linus Torvalds committed
716 717
}

Zhang Rui's avatar
Zhang Rui committed
718 719 720
/* --------------------------------------------------------------------------
                                 Device Enumeration
   -------------------------------------------------------------------------- */
721 722
static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
{
723
	struct acpi_device *device = NULL;
724 725 726 727 728 729 730
	acpi_status status;

	/*
	 * Fixed hardware devices do not appear in the namespace and do not
	 * have handles, but we fabricate acpi_devices for them, so we have
	 * to deal with them specially.
	 */
731
	if (!handle)
732 733 734 735 736
		return acpi_root;

	do {
		status = acpi_get_parent(handle, &handle);
		if (ACPI_FAILURE(status))
737 738 739
			return status == AE_NULL_ENTRY ? NULL : acpi_root;
	} while (acpi_bus_get_device(handle, &device));
	return device;
740 741
}

Zhang Rui's avatar
Zhang Rui committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
acpi_status
acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
{
	acpi_status status;
	acpi_handle tmp;
	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
	union acpi_object *obj;

	status = acpi_get_handle(handle, "_EJD", &tmp);
	if (ACPI_FAILURE(status))
		return status;

	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
	if (ACPI_SUCCESS(status)) {
		obj = buffer.pointer;
757 758
		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
					 ejd);
Zhang Rui's avatar
Zhang Rui committed
759 760 761 762 763 764
		kfree(buffer.pointer);
	}
	return status;
}
EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);

765 766
static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
					struct acpi_device_wakeup *wakeup)
Zhang Rui's avatar
Zhang Rui committed
767
{
768 769
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *package = NULL;
Zhang Rui's avatar
Zhang Rui committed
770
	union acpi_object *element = NULL;
771
	acpi_status status;
772
	int err = -ENODATA;
Zhang Rui's avatar
Zhang Rui committed
773

774
	if (!wakeup)
775
		return -EINVAL;
Zhang Rui's avatar
Zhang Rui committed
776

777
	INIT_LIST_HEAD(&wakeup->resources);
Zhang Rui's avatar
Zhang Rui committed
778

779 780 781 782
	/* _PRW */
	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
	if (ACPI_FAILURE(status)) {
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
783
		return err;
784 785 786 787
	}

	package = (union acpi_object *)buffer.pointer;

788
	if (!package || package->package.count < 2)
789 790
		goto out;

Zhang Rui's avatar
Zhang Rui committed
791
	element = &(package->package.elements[0]);
792
	if (!element)
793
		goto out;
794

Zhang Rui's avatar
Zhang Rui committed
795 796 797 798
	if (element->type == ACPI_TYPE_PACKAGE) {
		if ((element->package.count < 2) ||
		    (element->package.elements[0].type !=
		     ACPI_TYPE_LOCAL_REFERENCE)
799
		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
800
			goto out;
801

802
		wakeup->gpe_device =
Zhang Rui's avatar
Zhang Rui committed
803
		    element->package.elements[0].reference.handle;
804
		wakeup->gpe_number =
Zhang Rui's avatar
Zhang Rui committed
805 806
		    (u32) element->package.elements[1].integer.value;
	} else if (element->type == ACPI_TYPE_INTEGER) {
807 808 809 810 811
		wakeup->gpe_device = NULL;
		wakeup->gpe_number = element->integer.value;
	} else {
		goto out;
	}
Zhang Rui's avatar
Zhang Rui committed
812 813

	element = &(package->package.elements[1]);
814
	if (element->type != ACPI_TYPE_INTEGER)
815
		goto out;
816

817
	wakeup->sleep_state = element->integer.value;
Zhang Rui's avatar
Zhang Rui committed
818

819 820
	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
	if (err)
821
		goto out;
Zhang Rui's avatar
Zhang Rui committed
822

823 824
	if (!list_empty(&wakeup->resources)) {
		int sleep_state;
Zhang Rui's avatar
Zhang Rui committed
825

826 827 828 829 830 831 832 833
		err = acpi_power_wakeup_list_init(&wakeup->resources,
						  &sleep_state);
		if (err) {
			acpi_handle_warn(handle, "Retrieving current states "
					 "of wakeup power resources failed\n");
			acpi_power_resources_list_free(&wakeup->resources);
			goto out;
		}
834 835 836 837 838 839 840
		if (sleep_state < wakeup->sleep_state) {
			acpi_handle_warn(handle, "Overriding _PRW sleep state "
					 "(S%d) by S%d from power resources\n",
					 (int)wakeup->sleep_state, sleep_state);
			wakeup->sleep_state = sleep_state;
		}
	}
841

842 843
 out:
	kfree(buffer.pointer);
844
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
845 846
}

847
static bool acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
848
{
849
	static const struct acpi_device_id button_device_ids[] = {
850
		{"PNP0C0C", 0},
851
		{"PNP0C0D", 0},
852 853 854
		{"PNP0C0E", 0},
		{"", 0},
	};
855
	struct acpi_device_wakeup *wakeup = &device->wakeup;
856 857
	acpi_status status;

858
	wakeup->flags.notifier_present = 0;
859 860 861

	/* Power button, Lid switch always enable wakeup */
	if (!acpi_match_device_ids(device, button_device_ids)) {
862 863
		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
			/* Do not use Lid/sleep button for S5 wakeup */
864 865
			if (wakeup->sleep_state == ACPI_STATE_S5)
				wakeup->sleep_state = ACPI_STATE_S4;
866
		}
867
		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
868
		device_set_wakeup_capable(&device->dev, true);
869
		return true;
870 871
	}

872 873 874
	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
					 wakeup->gpe_number);
	return ACPI_SUCCESS(status);
875 876
}

877
static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
878
{
879
	int err;
880

881
	/* Presence of _PRW indicates wake capable */
882
	if (!acpi_has_method(device->handle, "_PRW"))
883 884
		return;

885 886 887 888
	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
							   &device->wakeup);
	if (err) {
		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
889
		return;
Zhang Rui's avatar
Zhang Rui committed
890
	}
Linus Torvalds's avatar
Linus Torvalds committed
891

892
	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
893
	device->wakeup.prepare_count = 0;
894 895
	/*
	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
896 897 898 899 900
	 * system for the ACPI device with the _PRW object.
	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
	 * So it is necessary to call _DSW object first. Only when it is not
	 * present will the _PSW object used.
	 */
901 902
	err = acpi_device_sleep_wake(device, 0, 0, 0);
	if (err)
903 904
		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
				"error in _DSW or _PSW evaluation\n"));
Linus Torvalds's avatar
Linus Torvalds committed
905 906
}

907 908 909
static void acpi_bus_init_power_state(struct acpi_device *device, int state)
{
	struct acpi_device_power_state *ps = &device->power.states[state];
910 911
	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
912
	acpi_status status;
913

914 915
	INIT_LIST_HEAD(&ps->resources);

916 917 918 919 920 921 922 923
	/* Evaluate "_PRx" to get referenced power resources */
	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
	if (ACPI_SUCCESS(status)) {
		union acpi_object *package = buffer.pointer;

		if (buffer.length && package
		    && package->type == ACPI_TYPE_PACKAGE
		    && package->package.count) {
924 925 926
			int err = acpi_extract_power_resources(package, 0,
							       &ps->resources);
			if (!err)
927
				device->power.flags.power_resources = 1;
928
		}
929
		ACPI_FREE(buffer.pointer);
930 931 932
	}

	/* Evaluate "_PSx" to see if we can do explicit sets */
933
	pathname[2] = 'S';
934
	if (acpi_has_method(device->handle, pathname))
935 936
		ps->flags.explicit_set = 1;

937 938
	/* State is valid if there are means to put the device into it. */
	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
939 940 941 942 943 944
		ps->flags.valid = 1;

	ps->power = -1;		/* Unknown - driver assigned */
	ps->latency = -1;	/* Unknown - driver assigned */
}

945
static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
947
	u32 i;
Bjorn Helgaas's avatar
Bjorn Helgaas committed
948

949
	/* Presence of _PS0|_PR0 indicates 'power manageable' */
950 951 952
	if (!acpi_has_method(device->handle, "_PS0") &&
	    !acpi_has_method(device->handle, "_PR0"))
		return;
Bjorn Helgaas's avatar
Bjorn Helgaas committed
953

954
	device->flags.power_manageable = 1;
Len Brown's avatar
Len Brown committed
955

Zhang Rui's avatar
Zhang Rui committed
956 957 958
	/*
	 * Power Management Flags
	 */
959
	if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui's avatar
Zhang Rui committed
960
		device->power.flags.explicit_get = 1;
961

962
	if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui's avatar
Zhang Rui committed
963
		device->power.flags.inrush_current = 1;
Linus Torvalds's avatar
Linus Torvalds committed
964

965 966 967
	if (acpi_has_method(device->handle, "_DSW"))
		device->power.flags.dsw_present = 1;

Zhang Rui's avatar
Zhang Rui committed
968 969 970
	/*
	 * Enumerate supported power management states
	 */
971 972
	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
		acpi_bus_init_power_state(device, i);
973

974
	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
975 976
	if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
		device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
Linus Torvalds's avatar
Linus Torvalds committed
977

978
	/* Set defaults for D0 and D3hot states (always valid) */
Zhang Rui's avatar
Zhang Rui committed
979 980
	device->power.states[ACPI_STATE_D0].flags.valid = 1;
	device->power.states[ACPI_STATE_D0].power = 100;
981
	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
982

983
	if (acpi_bus_init_power(device))
984
		device->flags.power_manageable = 0;
Zhang Rui's avatar
Zhang Rui committed
985
}
986

987
static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
988 989
{
	/* Presence of _STA indicates 'dynamic_status' */
990
	if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993
		device->flags.dynamic_status = 1;

	/* Presence of _RMV indicates 'removable' */
994
	if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds's avatar
Linus Torvalds committed
995 996 997
		device->flags.removable = 1;

	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
998 999
	if (acpi_has_method(device->handle, "_EJD") ||
	    acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds's avatar
Linus Torvalds committed
1000 1001 1002
		device->flags.ejectable = 1;
}

1003
static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
1004
{
Len Brown's avatar
Len Brown committed
1005 1006 1007
	char bus_id[5] = { '?', 0 };
	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
	int i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009 1010 1011 1012 1013 1014

	/*
	 * Bus ID
	 * ------
	 * The device's Bus ID is simply the object name.
	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
	 */
1015
	if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds's avatar
Linus Torvalds committed
1016
		strcpy(device->pnp.bus_id, "ACPI");
1017 1018 1019 1020
		return;
	}

	switch (device->device_type) {
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024 1025 1026
	case ACPI_BUS_TYPE_POWER_BUTTON:
		strcpy(device->pnp.bus_id, "PWRF");
		break;
	case ACPI_BUS_TYPE_SLEEP_BUTTON:
		strcpy(device->pnp.bus_id, "SLPF");
		break;
1027 1028 1029
	case ACPI_BUS_TYPE_ECDT_EC:
		strcpy(device->pnp.bus_id, "ECDT");
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1030
	default:
1031
		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		/* Clean up trailing underscores (if any) */
		for (i = 3; i > 1; i--) {
			if (bus_id[i] == '_')
				bus_id[i] = '\0';
			else
				break;
		}
		strcpy(device->pnp.bus_id, bus_id);
		break;
	}
}

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
/*
 * acpi_ata_match - see if an acpi object is an ATA device
 *
 * If an acpi object has one of the ACPI ATA methods defined,
 * then we can safely call it an ATA device.
 */
bool acpi_ata_match(acpi_handle handle)
{
	return acpi_has_method(handle, "_GTF") ||
	       acpi_has_method(handle, "_GTM") ||
	       acpi_has_method(handle, "_STM") ||
	       acpi_has_method(handle, "_SDD");
}

1058
/*
1059
 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1060 1061 1062 1063
 *
 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
 * then we can safely call it an ejectable drive bay
 */
1064
bool acpi_bay_match(acpi_handle handle)
1065
{
1066 1067
	acpi_handle phandle;

1068
	if (!acpi_has_method(handle, "_EJ0"))
1069 1070 1071 1072 1073
		return false;
	if (acpi_ata_match(handle))
		return true;
	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
		return false;
1074

1075
	return acpi_ata_match(phandle);
1076 1077
}

1078
bool acpi_device_is_battery(struct acpi_device *adev)
1079
{
1080
	struct acpi_hardware_id *hwid;
1081

1082 1083 1084
	list_for_each_entry(hwid, &adev->pnp.ids, list)
		if (!strcmp("PNP0C0A", hwid->id))
			return true;
1085

1086
	return false;
1087 1088
}

1089
static bool is_ejectable_bay(struct acpi_device *adev)
1090
{
1091 1092 1093
	acpi_handle handle = adev->handle;

	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1094 1095 1096 1097 1098
		return true;

	return acpi_bay_match(handle);
}

1099
/*
1100
 * acpi_dock_match - see if an acpi object has a _DCK method
1101
 */
1102
bool acpi_dock_match(acpi_handle handle)
1103
{
1104
	return acpi_has_method(handle, "_DCK");
1105 1106
}

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static acpi_status
acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
			  void **return_value)
{
	long *cap = context;

	if (acpi_has_method(handle, "_BCM") &&
	    acpi_has_method(handle, "_BCL")) {
		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
				  "support\n"));
		*cap |= ACPI_VIDEO_BACKLIGHT;
		/* We have backlight support, no need to scan further */
		return AE_CTRL_TERMINATE;
	}
	return 0;
}

/* Returns true if the ACPI object is a video device which can be
 * handled by video.ko.
 * The device will get a Linux specific CID added in scan.c to
 * identify the device as an ACPI graphics device
 * Be aware that the graphics device may not be physically present
 * Use acpi_video_get_capabilities() to detect general ACPI video
 * capabilities of present cards
 */
long acpi_is_video_device(acpi_handle handle)
{
	long video_caps = 0;

	/* Is this device able to support video switching ? */
	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;

	/* Is this device able to retrieve a video ROM ? */
	if (acpi_has_method(handle, "_ROM"))
		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;

	/* Is this device able to configure which video head to be POSTed ? */
	if (acpi_has_method(handle, "_VPO") &&
	    acpi_has_method(handle, "_GPD") &&
	    acpi_has_method(handle, "_SPD"))
		video_caps |= ACPI_VIDEO_DEVICE_POSTING;

	/* Only check for backlight functionality if one of the above hit. */
	if (video_caps)
		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
				    &video_caps, NULL);

	return video_caps;
}
EXPORT_SYMBOL(acpi_is_video_device);

1160
const char *acpi_device_hid(struct acpi_device *device)
1161
{
1162
	struct acpi_hardware_id *hid;
1163

1164 1165 1166
	if (list_empty(&device->pnp.ids))
		return dummy_hid;

1167 1168 1169 1170
	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
	return hid->id;
}
EXPORT_SYMBOL(acpi_device_hid);
1171

1172
static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1173 1174
{
	struct acpi_hardware_id *id;
1175

1176 1177 1178
	id = kmalloc(sizeof(*id), GFP_KERNEL);
	if (!id)
		return;
1179

1180
	id->id = kstrdup_const(dev_id, GFP_KERNEL);
1181 1182 1183
	if (!id->id) {
		kfree(id);
		return;
1184 1185
	}

1186 1187
	list_add_tail(&id->list, &pnp->ids);
	pnp->type.hardware_id = 1;