property.c 31.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * ACPI device specific properties support.
 *
 * Copyright (C) 2014, Intel Corporation
 * All rights reserved.
 *
 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
 *          Darren Hart <dvhart@linux.intel.com>
 *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/export.h>

#include "internal.h"

22
static int acpi_data_get_property_array(const struct acpi_device_data *data,
23 24 25 26
					const char *name,
					acpi_object_type type,
					const union acpi_object **obj);

27 28 29 30 31
/* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
static const u8 prp_uuid[16] = {
	0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
	0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
};
32 33 34 35 36 37 38 39
/* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
static const u8 ads_uuid[16] = {
	0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
	0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
};

static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
					   const union acpi_object *desc,
40 41
					   struct acpi_device_data *data,
					   struct fwnode_handle *parent);
42 43 44
static bool acpi_extract_properties(const union acpi_object *desc,
				    struct acpi_device_data *data);

45 46 47
static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
					acpi_handle handle,
					const union acpi_object *link,
48 49
					struct list_head *list,
					struct fwnode_handle *parent)
50 51
{
	struct acpi_data_node *dn;
52
	bool result;
53 54 55 56 57 58

	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
	if (!dn)
		return false;

	dn->name = link->package.elements[0].string.pointer;
59
	dn->fwnode.ops = &acpi_data_fwnode_ops;
60
	dn->parent = parent;
61 62
	INIT_LIST_HEAD(&dn->data.subnodes);

63
	result = acpi_extract_properties(desc, &dn->data);
64

65 66 67
	if (handle) {
		acpi_handle scope;
		acpi_status status;
68

69 70 71 72 73 74 75 76
		/*
		 * The scope for the subnode object lookup is the one of the
		 * namespace node (device) containing the object that has
		 * returned the package.  That is, it's the scope of that
		 * object's parent.
		 */
		status = acpi_get_parent(handle, &scope);
		if (ACPI_SUCCESS(status)
77 78
		    && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
						      &dn->fwnode))
79
			result = true;
80 81
	} else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
						  &dn->fwnode)) {
82 83
		result = true;
	}
84

85
	if (result) {
86
		dn->handle = handle;
87
		dn->data.pointer = desc;
88 89 90 91
		list_add_tail(&dn->sibling, list);
		return true;
	}

92
	kfree(dn);
93
	acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
94 95 96 97 98
	return false;
}

static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
					const union acpi_object *link,
99 100
					struct list_head *list,
					struct fwnode_handle *parent)
101 102 103 104 105 106 107 108 109
{
	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
	acpi_status status;

	status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
					    ACPI_TYPE_PACKAGE);
	if (ACPI_FAILURE(status))
		return false;

110 111
	if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
					parent))
112
		return true;
113 114 115 116 117

	ACPI_FREE(buf.pointer);
	return false;
}

118 119
static bool acpi_nondev_subnode_ok(acpi_handle scope,
				   const union acpi_object *link,
120 121
				   struct list_head *list,
				   struct fwnode_handle *parent)
122 123 124 125 126 127 128 129 130 131 132 133
{
	acpi_handle handle;
	acpi_status status;

	if (!scope)
		return false;

	status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
				 &handle);
	if (ACPI_FAILURE(status))
		return false;

134
	return acpi_nondev_subnode_data_ok(handle, link, list, parent);
135 136
}

137 138
static int acpi_add_nondev_subnodes(acpi_handle scope,
				    const union acpi_object *links,
139 140
				    struct list_head *list,
				    struct fwnode_handle *parent)
141 142 143 144 145
{
	bool ret = false;
	int i;

	for (i = 0; i < links->package.count; i++) {
146 147 148
		const union acpi_object *link, *desc;
		acpi_handle handle;
		bool result;
149 150

		link = &links->package.elements[i];
151 152 153 154 155 156 157 158 159 160 161
		/* Only two elements allowed. */
		if (link->package.count != 2)
			continue;

		/* The first one must be a string. */
		if (link->package.elements[0].type != ACPI_TYPE_STRING)
			continue;

		/* The second one may be a string, a reference or a package. */
		switch (link->package.elements[1].type) {
		case ACPI_TYPE_STRING:
162 163
			result = acpi_nondev_subnode_ok(scope, link, list,
							 parent);
164 165 166
			break;
		case ACPI_TYPE_LOCAL_REFERENCE:
			handle = link->package.elements[1].reference.handle;
167 168
			result = acpi_nondev_subnode_data_ok(handle, link, list,
							     parent);
169 170 171
			break;
		case ACPI_TYPE_PACKAGE:
			desc = &link->package.elements[1];
172 173
			result = acpi_nondev_subnode_extract(desc, NULL, link,
							     list, parent);
174 175 176 177 178 179
			break;
		default:
			result = false;
			break;
		}
		ret = ret || result;
180 181 182 183 184 185 186
	}

	return ret;
}

static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
					   const union acpi_object *desc,
187 188
					   struct acpi_device_data *data,
					   struct fwnode_handle *parent)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
	int i;

	/* Look for the ACPI data subnodes UUID. */
	for (i = 0; i < desc->package.count; i += 2) {
		const union acpi_object *uuid, *links;

		uuid = &desc->package.elements[i];
		links = &desc->package.elements[i + 1];

		/*
		 * The first element must be a UUID and the second one must be
		 * a package.
		 */
		if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
		    || links->type != ACPI_TYPE_PACKAGE)
			break;

		if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid)))
			continue;

210 211
		return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
						parent);
212 213 214 215
	}

	return false;
}
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

static bool acpi_property_value_ok(const union acpi_object *value)
{
	int j;

	/*
	 * The value must be an integer, a string, a reference, or a package
	 * whose every element must be an integer, a string, or a reference.
	 */
	switch (value->type) {
	case ACPI_TYPE_INTEGER:
	case ACPI_TYPE_STRING:
	case ACPI_TYPE_LOCAL_REFERENCE:
		return true;

	case ACPI_TYPE_PACKAGE:
		for (j = 0; j < value->package.count; j++)
			switch (value->package.elements[j].type) {
			case ACPI_TYPE_INTEGER:
			case ACPI_TYPE_STRING:
			case ACPI_TYPE_LOCAL_REFERENCE:
				continue;

			default:
				return false;
			}

		return true;
	}
	return false;
}

static bool acpi_properties_format_valid(const union acpi_object *properties)
{
	int i;

	for (i = 0; i < properties->package.count; i++) {
		const union acpi_object *property;

		property = &properties->package.elements[i];
		/*
		 * Only two elements allowed, the first one must be a string and
		 * the second one has to satisfy certain conditions.
		 */
		if (property->package.count != 2
		    || property->package.elements[0].type != ACPI_TYPE_STRING
		    || !acpi_property_value_ok(&property->package.elements[1]))
			return false;
	}
	return true;
}

268 269 270 271 272
static void acpi_init_of_compatible(struct acpi_device *adev)
{
	const union acpi_object *of_compatible;
	int ret;

273 274
	ret = acpi_data_get_property_array(&adev->data, "compatible",
					   ACPI_TYPE_STRING, &of_compatible);
275 276 277 278
	if (ret) {
		ret = acpi_dev_get_property(adev, "compatible",
					    ACPI_TYPE_STRING, &of_compatible);
		if (ret) {
279 280 281 282
			if (adev->parent
			    && adev->parent->flags.of_compatible_ok)
				goto out;

283 284 285 286
			return;
		}
	}
	adev->data.of_compatible = of_compatible;
287 288 289

 out:
	adev->flags.of_compatible_ok = 1;
290 291
}

292 293
static bool acpi_extract_properties(const union acpi_object *desc,
				    struct acpi_device_data *data)
294 295 296 297
{
	int i;

	if (desc->package.count % 2)
298
		return false;
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

	/* Look for the device properties UUID. */
	for (i = 0; i < desc->package.count; i += 2) {
		const union acpi_object *uuid, *properties;

		uuid = &desc->package.elements[i];
		properties = &desc->package.elements[i + 1];

		/*
		 * The first element must be a UUID and the second one must be
		 * a package.
		 */
		if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
		    || properties->type != ACPI_TYPE_PACKAGE)
			break;

		if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
			continue;

		/*
		 * We found the matching UUID. Now validate the format of the
		 * package immediately following it.
		 */
		if (!acpi_properties_format_valid(properties))
			break;

325 326 327
		data->properties = properties;
		return true;
	}
328

329 330
	return false;
}
331

332 333 334 335 336 337 338
void acpi_init_properties(struct acpi_device *adev)
{
	struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
	struct acpi_hardware_id *hwid;
	acpi_status status;
	bool acpi_of = false;

339 340
	INIT_LIST_HEAD(&adev->data.subnodes);

341 342 343 344 345 346 347 348 349
	/*
	 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
	 * Device Tree compatible properties for this device.
	 */
	list_for_each_entry(hwid, &adev->pnp.ids, list) {
		if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
			acpi_of = true;
			break;
		}
350 351
	}

352 353 354 355 356 357 358 359 360
	status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
					    ACPI_TYPE_PACKAGE);
	if (ACPI_FAILURE(status))
		goto out;

	if (acpi_extract_properties(buf.pointer, &adev->data)) {
		adev->data.pointer = buf.pointer;
		if (acpi_of)
			acpi_init_of_compatible(adev);
361
	}
362 363
	if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
					&adev->data, acpi_fwnode_handle(adev)))
364 365 366
		adev->data.pointer = buf.pointer;

	if (!adev->data.pointer) {
367 368 369
		acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
		ACPI_FREE(buf.pointer);
	}
370 371 372 373

 out:
	if (acpi_of && !adev->flags.of_compatible_ok)
		acpi_handle_info(adev->handle,
374
			 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
375 376
}

377 378 379 380 381 382 383 384 385
static void acpi_destroy_nondev_subnodes(struct list_head *list)
{
	struct acpi_data_node *dn, *next;

	if (list_empty(list))
		return;

	list_for_each_entry_safe_reverse(dn, next, list, sibling) {
		acpi_destroy_nondev_subnodes(&dn->data.subnodes);
386
		wait_for_completion(&dn->kobj_done);
387 388 389 390 391 392
		list_del(&dn->sibling);
		ACPI_FREE((void *)dn->data.pointer);
		kfree(dn);
	}
}

393 394
void acpi_free_properties(struct acpi_device *adev)
{
395
	acpi_destroy_nondev_subnodes(&adev->data.subnodes);
396
	ACPI_FREE((void *)adev->data.pointer);
397
	adev->data.of_compatible = NULL;
398 399 400 401 402
	adev->data.pointer = NULL;
	adev->data.properties = NULL;
}

/**
403 404
 * acpi_data_get_property - return an ACPI property with given name
 * @data: ACPI device deta object to get the property from
405 406 407 408 409 410 411 412
 * @name: Name of the property
 * @type: Expected property type
 * @obj: Location to store the property value (if not %NULL)
 *
 * Look up a property with @name and store a pointer to the resulting ACPI
 * object at the location pointed to by @obj if found.
 *
 * Callers must not attempt to free the returned objects.  These objects will be
413
 * freed by the ACPI core automatically during the removal of @data.
414 415 416
 *
 * Return: %0 if property with @name has been found (success),
 *         %-EINVAL if the arguments are invalid,
417
 *         %-EINVAL if the property doesn't exist,
418 419
 *         %-EPROTO if the property value type doesn't match @type.
 */
420
static int acpi_data_get_property(const struct acpi_device_data *data,
421 422
				  const char *name, acpi_object_type type,
				  const union acpi_object **obj)
423 424 425 426
{
	const union acpi_object *properties;
	int i;

427
	if (!data || !name)
428 429
		return -EINVAL;

430
	if (!data->pointer || !data->properties)
431
		return -EINVAL;
432

433
	properties = data->properties;
434 435 436 437 438 439 440 441 442 443 444 445
	for (i = 0; i < properties->package.count; i++) {
		const union acpi_object *propname, *propvalue;
		const union acpi_object *property;

		property = &properties->package.elements[i];

		propname = &property->package.elements[0];
		propvalue = &property->package.elements[1];

		if (!strcmp(name, propname->string.pointer)) {
			if (type != ACPI_TYPE_ANY && propvalue->type != type)
				return -EPROTO;
446
			if (obj)
447 448 449 450 451
				*obj = propvalue;

			return 0;
		}
	}
452
	return -EINVAL;
453
}
454 455 456 457 458 459 460 461

/**
 * acpi_dev_get_property - return an ACPI property with given name.
 * @adev: ACPI device to get the property from.
 * @name: Name of the property.
 * @type: Expected property type.
 * @obj: Location to store the property value (if not %NULL).
 */
462
int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
463 464 465 466
			  acpi_object_type type, const union acpi_object **obj)
{
	return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
}
467 468
EXPORT_SYMBOL_GPL(acpi_dev_get_property);

469 470
static const struct acpi_device_data *
acpi_device_data_of_node(const struct fwnode_handle *fwnode)
471
{
472
	if (is_acpi_device_node(fwnode)) {
473
		const struct acpi_device *adev = to_acpi_device_node(fwnode);
474
		return &adev->data;
475
	} else if (is_acpi_data_node(fwnode)) {
476
		const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
477 478 479 480 481
		return &dn->data;
	}
	return NULL;
}

482
/**
483 484 485 486 487
 * acpi_node_prop_get - return an ACPI property with given name.
 * @fwnode: Firmware node to get the property from.
 * @propname: Name of the property.
 * @valptr: Location to store a pointer to the property value (if not %NULL).
 */
488 489
int acpi_node_prop_get(const struct fwnode_handle *fwnode,
		       const char *propname, void **valptr)
490 491 492 493 494 495 496 497 498
{
	return acpi_data_get_property(acpi_device_data_of_node(fwnode),
				      propname, ACPI_TYPE_ANY,
				      (const union acpi_object **)valptr);
}

/**
 * acpi_data_get_property_array - return an ACPI array property with given name
 * @adev: ACPI data object to get the property from
499 500 501 502 503 504 505 506
 * @name: Name of the property
 * @type: Expected type of array elements
 * @obj: Location to store a pointer to the property value (if not NULL)
 *
 * Look up an array property with @name and store a pointer to the resulting
 * ACPI object at the location pointed to by @obj if found.
 *
 * Callers must not attempt to free the returned objects.  Those objects will be
507
 * freed by the ACPI core automatically during the removal of @data.
508 509 510
 *
 * Return: %0 if array property (package) with @name has been found (success),
 *         %-EINVAL if the arguments are invalid,
511
 *         %-EINVAL if the property doesn't exist,
512 513 514
 *         %-EPROTO if the property is not a package or the type of its elements
 *           doesn't match @type.
 */
515
static int acpi_data_get_property_array(const struct acpi_device_data *data,
516 517 518
					const char *name,
					acpi_object_type type,
					const union acpi_object **obj)
519 520 521 522
{
	const union acpi_object *prop;
	int ret, i;

523
	ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	if (ret)
		return ret;

	if (type != ACPI_TYPE_ANY) {
		/* Check that all elements are of correct type. */
		for (i = 0; i < prop->package.count; i++)
			if (prop->package.elements[i].type != type)
				return -EPROTO;
	}
	if (obj)
		*obj = prop;

	return 0;
}

/**
540 541
 * __acpi_node_get_property_reference - returns handle to the referenced object
 * @fwnode: Firmware node to get the property from
542
 * @propname: Name of the property
543
 * @index: Index of the reference to return
544
 * @num_args: Maximum number of arguments after each reference
545 546 547 548
 * @args: Location to store the returned reference with optional arguments
 *
 * Find property with @name, verifify that it is a package containing at least
 * one object reference and if so, store the ACPI device object pointer to the
549 550
 * target object in @args->adev.  If the reference includes arguments, store
 * them in the @args->args[] array.
551
 *
552 553
 * If there's more than one reference in the property value package, @index is
 * used to select the one to return.
554
 *
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
 * It is possible to leave holes in the property value set like in the
 * example below:
 *
 * Package () {
 *     "cs-gpios",
 *     Package () {
 *        ^GPIO, 19, 0, 0,
 *        ^GPIO, 20, 0, 0,
 *        0,
 *        ^GPIO, 21, 0, 0,
 *     }
 * }
 *
 * Calling this function with index %2 return %-ENOENT and with index %3
 * returns the last entry. If the property does not contain any more values
 * %-ENODATA is returned. The NULL entry must be single integer and
 * preferably contain value %0.
 *
573 574
 * Return: %0 on success, negative error code on failure.
 */
575
int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
576 577
	const char *propname, size_t index, size_t num_args,
	struct acpi_reference_args *args)
578 579 580
{
	const union acpi_object *element, *end;
	const union acpi_object *obj;
581
	const struct acpi_device_data *data;
582 583 584
	struct acpi_device *device;
	int ret, idx = 0;

585 586 587 588
	data = acpi_device_data_of_node(fwnode);
	if (!data)
		return -EINVAL;

589
	ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
590 591 592 593 594 595 596 597
	if (ret)
		return ret;

	/*
	 * The simplest case is when the value is a single reference.  Just
	 * return that reference then.
	 */
	if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
598
		if (index)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
			return -EINVAL;

		ret = acpi_bus_get_device(obj->reference.handle, &device);
		if (ret)
			return ret;

		args->adev = device;
		args->nargs = 0;
		return 0;
	}

	/*
	 * If it is not a single reference, then it is a package of
	 * references followed by number of ints as follows:
	 *
	 *  Package () { REF, INT, REF, INT, INT }
	 *
	 * The index argument is then used to determine which reference
	 * the caller wants (along with the arguments).
	 */
	if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
		return -EPROTO;

	element = obj->package.elements;
	end = element + obj->package.count;

	while (element < end) {
		u32 nargs, i;

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
			ret = acpi_bus_get_device(element->reference.handle,
						  &device);
			if (ret)
				return -ENODEV;

			nargs = 0;
			element++;

			/* assume following integer elements are all args */
			for (i = 0; element + i < end && i < num_args; i++) {
				int type = element[i].type;

				if (type == ACPI_TYPE_INTEGER)
					nargs++;
				else if (type == ACPI_TYPE_LOCAL_REFERENCE)
					break;
				else
					return -EPROTO;
			}
648

649
			if (nargs > MAX_ACPI_REFERENCE_ARGS)
650
				return -EPROTO;
651

652 653 654 655 656
			if (idx == index) {
				args->adev = device;
				args->nargs = nargs;
				for (i = 0; i < nargs; i++)
					args->args[i] = element[i].integer.value;
657

658 659 660 661 662 663 664 665 666 667
				return 0;
			}

			element += nargs;
		} else if (element->type == ACPI_TYPE_INTEGER) {
			if (idx == index)
				return -ENOENT;
			element++;
		} else {
			return -EPROTO;
668 669
		}

670
		idx++;
671 672
	}

673
	return -ENODATA;
674
}
675
EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
676

677
static int acpi_data_prop_read_single(const struct acpi_device_data *data,
678 679
				      const char *propname,
				      enum dev_prop_type proptype, void *val)
680 681 682 683 684 685 686 687
{
	const union acpi_object *obj;
	int ret;

	if (!val)
		return -EINVAL;

	if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
688
		ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
		if (ret)
			return ret;

		switch (proptype) {
		case DEV_PROP_U8:
			if (obj->integer.value > U8_MAX)
				return -EOVERFLOW;
			*(u8 *)val = obj->integer.value;
			break;
		case DEV_PROP_U16:
			if (obj->integer.value > U16_MAX)
				return -EOVERFLOW;
			*(u16 *)val = obj->integer.value;
			break;
		case DEV_PROP_U32:
			if (obj->integer.value > U32_MAX)
				return -EOVERFLOW;
			*(u32 *)val = obj->integer.value;
			break;
		default:
			*(u64 *)val = obj->integer.value;
			break;
		}
	} else if (proptype == DEV_PROP_STRING) {
713
		ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
714 715 716 717
		if (ret)
			return ret;

		*(char **)val = obj->string.pointer;
718 719

		return 1;
720 721 722 723 724 725
	} else {
		ret = -EINVAL;
	}
	return ret;
}

726 727 728
int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
			      enum dev_prop_type proptype, void *val)
{
729 730 731 732 733 734 735 736 737
	int ret;

	if (!adev)
		return -EINVAL;

	ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
	if (ret < 0 || proptype != ACPI_TYPE_STRING)
		return ret;
	return 0;
738 739
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
				       size_t nval)
{
	int i;

	for (i = 0; i < nval; i++) {
		if (items[i].type != ACPI_TYPE_INTEGER)
			return -EPROTO;
		if (items[i].integer.value > U8_MAX)
			return -EOVERFLOW;

		val[i] = items[i].integer.value;
	}
	return 0;
}

static int acpi_copy_property_array_u16(const union acpi_object *items,
					u16 *val, size_t nval)
{
	int i;

	for (i = 0; i < nval; i++) {
		if (items[i].type != ACPI_TYPE_INTEGER)
			return -EPROTO;
		if (items[i].integer.value > U16_MAX)
			return -EOVERFLOW;

		val[i] = items[i].integer.value;
	}
	return 0;
}

static int acpi_copy_property_array_u32(const union acpi_object *items,
					u32 *val, size_t nval)
{
	int i;

	for (i = 0; i < nval; i++) {
		if (items[i].type != ACPI_TYPE_INTEGER)
			return -EPROTO;
		if (items[i].integer.value > U32_MAX)
			return -EOVERFLOW;

		val[i] = items[i].integer.value;
	}
	return 0;
}

static int acpi_copy_property_array_u64(const union acpi_object *items,
					u64 *val, size_t nval)
{
	int i;

	for (i = 0; i < nval; i++) {
		if (items[i].type != ACPI_TYPE_INTEGER)
			return -EPROTO;

		val[i] = items[i].integer.value;
	}
	return 0;
}

static int acpi_copy_property_array_string(const union acpi_object *items,
					   char **val, size_t nval)
{
	int i;

	for (i = 0; i < nval; i++) {
		if (items[i].type != ACPI_TYPE_STRING)
			return -EPROTO;

		val[i] = items[i].string.pointer;
	}
813
	return nval;
814 815
}

816
static int acpi_data_prop_read(const struct acpi_device_data *data,
817 818 819
			       const char *propname,
			       enum dev_prop_type proptype,
			       void *val, size_t nval)
820 821 822 823 824 825
{
	const union acpi_object *obj;
	const union acpi_object *items;
	int ret;

	if (val && nval == 1) {
826
		ret = acpi_data_prop_read_single(data, propname, proptype, val);
827
		if (ret >= 0)
828 829 830
			return ret;
	}

831
	ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
832 833 834 835 836 837
	if (ret)
		return ret;

	if (!val)
		return obj->package.count;

838
	if (proptype != DEV_PROP_STRING && nval > obj->package.count)
839
		return -EOVERFLOW;
840 841
	else if (nval <= 0)
		return -EINVAL;
842 843

	items = obj->package.elements;
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858
	switch (proptype) {
	case DEV_PROP_U8:
		ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
		break;
	case DEV_PROP_U16:
		ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
		break;
	case DEV_PROP_U32:
		ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
		break;
	case DEV_PROP_U64:
		ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
		break;
	case DEV_PROP_STRING:
859 860 861
		ret = acpi_copy_property_array_string(
			items, (char **)val,
			min_t(u32, nval, obj->package.count));
862 863 864 865 866 867 868
		break;
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}
869

870
int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
		       enum dev_prop_type proptype, void *val, size_t nval)
{
	return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
}

/**
 * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
 * @fwnode: Firmware node to get the property from.
 * @propname: Name of the property.
 * @proptype: Expected property type.
 * @val: Location to store the property value (if not %NULL).
 * @nval: Size of the array pointed to by @val.
 *
 * If @val is %NULL, return the number of array elements comprising the value
 * of the property.  Otherwise, read at most @nval values to the array at the
 * location pointed to by @val.
 */
888 889 890
int acpi_node_prop_read(const struct fwnode_handle *fwnode,
			const char *propname, enum dev_prop_type proptype,
			void *val, size_t nval)
891 892 893 894
{
	return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
				   propname, proptype, val, nval);
}
895 896

/**
897 898
 * acpi_get_next_subnode - Return the next child node handle for a fwnode
 * @fwnode: Firmware node to find the next child node for.
899 900
 * @child: Handle to one of the device's child nodes or a null handle.
 */
901
struct fwnode_handle *acpi_get_next_subnode(struct fwnode_handle *fwnode,
902 903
					    struct fwnode_handle *child)
{
904 905 906 907
	const struct acpi_device *adev = to_acpi_device_node(fwnode);
	struct acpi_device *child_adev = NULL;
	const struct list_head *head;
	struct list_head *next;
908

909
	if (!child || is_acpi_device_node(child)) {
910 911 912 913 914
		if (adev)
			head = &adev->children;
		else
			goto nondev;

915 916 917 918
		if (list_empty(head))
			goto nondev;

		if (child) {
919 920
			child_adev = to_acpi_device_node(child);
			next = child_adev->node.next;
921 922 923 924
			if (next == head) {
				child = NULL;
				goto nondev;
			}
925
			child_adev = list_entry(next, struct acpi_device, node);
926
		} else {
927 928
			child_adev = list_first_entry(head, struct acpi_device,
						      node);
929
		}
930
		return acpi_fwnode_handle(child_adev);
931 932 933
	}

 nondev:
934
	if (!child || is_acpi_data_node(child)) {
935
		const struct acpi_data_node *data = to_acpi_data_node(fwnode);
936 937
		struct acpi_data_node *dn;

938 939
		if (child_adev)
			head = &child_adev->data.subnodes;
940 941 942 943 944
		else if (data)
			head = &data->data.subnodes;
		else
			return NULL;

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
		if (list_empty(head))
			return NULL;

		if (child) {
			dn = to_acpi_data_node(child);
			next = dn->sibling.next;
			if (next == head)
				return NULL;

			dn = list_entry(next, struct acpi_data_node, sibling);
		} else {
			dn = list_first_entry(head, struct acpi_data_node, sibling);
		}
		return &dn->fwnode;
	}
	return NULL;
}
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

/**
 * acpi_node_get_parent - Return parent fwnode of this fwnode
 * @fwnode: Firmware node whose parent to get
 *
 * Returns parent node of an ACPI device or data firmware node or %NULL if
 * not available.
 */
struct fwnode_handle *acpi_node_get_parent(struct fwnode_handle *fwnode)
{
	if (is_acpi_data_node(fwnode)) {
		/* All data nodes have parent pointer so just return that */
		return to_acpi_data_node(fwnode)->parent;
	} else if (is_acpi_device_node(fwnode)) {
		acpi_handle handle, parent_handle;

		handle = to_acpi_device_node(fwnode)->handle;
		if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
			struct acpi_device *adev;

			if (!acpi_bus_get_device(parent_handle, &adev))
				return acpi_fwnode_handle(adev);
		}
	}

	return NULL;
}
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

/**
 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
 * @fwnode: Pointer to the parent firmware node
 * @prev: Previous endpoint node or %NULL to get the first
 *
 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
 * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
 * of success the next endpoint is returned.
 */
struct fwnode_handle *acpi_graph_get_next_endpoint(struct fwnode_handle *fwnode,
						   struct fwnode_handle *prev)
{
	struct fwnode_handle *port = NULL;
	struct fwnode_handle *endpoint;

	if (!prev) {
		do {
			port = fwnode_get_next_child_node(fwnode, port);
			/* Ports must have port property */
			if (fwnode_property_present(port, "port"))
				break;
		} while (port);
	} else {
		port = fwnode_get_parent(prev);
	}

	if (!port)
		return NULL;

	endpoint = fwnode_get_next_child_node(port, prev);
	while (!endpoint) {
		port = fwnode_get_next_child_node(fwnode, port);
		if (!port)
			break;
		if (fwnode_property_present(port, "port"))
			endpoint = fwnode_get_next_child_node(port, NULL);
	}

	if (endpoint) {
		/* Endpoints must have "endpoint" property */
		if (!fwnode_property_present(endpoint, "endpoint"))
			return ERR_PTR(-EPROTO);
	}

	return endpoint;
}

/**
 * acpi_graph_get_child_prop_value - Return a child with a given property value
 * @fwnode: device fwnode
 * @prop_name: The name of the property to look for
 * @val: the desired property value
 *
 * Return the port node corresponding to a given port number. Returns
 * the child node on success, NULL otherwise.
 */
static struct fwnode_handle *acpi_graph_get_child_prop_value(
	struct fwnode_handle *fwnode, const char *prop_name, unsigned int val)
{
	struct fwnode_handle *child;

	fwnode_for_each_child_node(fwnode, child) {
		u32 nr;

		if (!fwnode_property_read_u32(fwnode, prop_name, &nr))
			continue;

		if (val == nr)
			return child;
	}

	return NULL;
}


/**
 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
 * @fwnode: Endpoint firmware node pointing to a remote device
 * @parent: Firmware node of remote port parent is filled here if not %NULL
 * @port: Firmware node of remote port is filled here if not %NULL
 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
 *
 * Function parses remote end of ACPI firmware remote endpoint and fills in
 * fields requested by the caller. Returns %0 in case of success and
 * negative errno otherwise.
 */
int acpi_graph_get_remote_endpoint(struct fwnode_handle *fwnode,
				   struct fwnode_handle **parent,
				   struct fwnode_handle **port,
				   struct fwnode_handle **endpoint)
{
	unsigned int port_nr, endpoint_nr;
	struct acpi_reference_args args;
	int ret;

	memset(&args, 0, sizeof(args));
	ret = acpi_node_get_property_reference(fwnode, "remote-endpoint", 0,
					       &args);
	if (ret)
		return ret;

	/*
	 * Always require two arguments with the reference: port and
	 * endpoint indices.
	 */
	if (args.nargs != 2)
		return -EPROTO;

	fwnode = acpi_fwnode_handle(args.adev);
	port_nr = args.args[0];
	endpoint_nr = args.args[1];

	if (parent)
		*parent = fwnode;

	if (!port && !endpoint)
		return 0;

	fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
	if (!fwnode)
		return -EPROTO;

	if (port)
		*port = fwnode;

	if (!endpoint)
		return 0;

	fwnode = acpi_graph_get_child_prop_value(fwnode, "endpoint",
						 endpoint_nr);
	if (!fwnode)
		return -EPROTO;

	*endpoint = fwnode;

	return 0;
}
1127

1128 1129 1130 1131 1132 1133 1134 1135
static bool acpi_fwnode_device_is_available(struct fwnode_handle *fwnode)
{
	if (!is_acpi_device_node(fwnode))
		return false;

	return acpi_device_is_present(to_acpi_device_node(fwnode));
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
static bool acpi_fwnode_property_present(struct fwnode_handle *fwnode,
					 const char *propname)
{
	return !acpi_node_prop_get(fwnode, propname, NULL);
}

static int acpi_fwnode_property_read_int_array(struct fwnode_handle *fwnode,
					       const char *propname,
					       unsigned int elem_size,
					       void *val, size_t nval)
{
	enum dev_prop_type type;

	switch (elem_size) {
	case sizeof(u8):
		type = DEV_PROP_U8;
		break;
	case sizeof(u16):
		type = DEV_PROP_U16;
		break;
	case sizeof(u32):
		type = DEV_PROP_U32;
		break;
	case sizeof(u64):
		type = DEV_PROP_U64;
		break;
	default:
		return -ENXIO;
	}

	return acpi_node_prop_read(fwnode, propname, type, val, nval);
}

static int acpi_fwnode_property_read_string_array(struct fwnode_handle *fwnode,
						  const char *propname,
						  const char **val, size_t nval)
{
	return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
				   val, nval);
}

static struct fwnode_handle *
acpi_fwnode_get_named_child_node(struct fwnode_handle *fwnode,
				 const char *childname)
{
	struct fwnode_handle *child;

	/*
	 * Find first matching named child node of this fwnode.
	 * For ACPI this will be a data only sub-node.
	 */
	fwnode_for_each_child_node(fwnode, child)
		if (acpi_data_node_match(child, childname))
			return child;

	return NULL;
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
static struct fwnode_handle *
acpi_fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode,
				    struct fwnode_handle *prev)
{
	struct fwnode_handle *endpoint;

	endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
	if (IS_ERR(endpoint))
		return NULL;

	return endpoint;
}

static struct fwnode_handle *
acpi_fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode)
{
	struct fwnode_handle *endpoint = NULL;

	acpi_graph_get_remote_endpoint(fwnode, NULL, NULL, &endpoint);

	return endpoint;
}

static int acpi_fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode,
					    struct fwnode_endpoint *endpoint)
{
	struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);

	endpoint->local_fwnode = fwnode;

	fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
	fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);

	return 0;
}

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
#define DECLARE_ACPI_FWNODE_OPS(ops) \
	const struct fwnode_operations ops = {				\
		.device_is_available = acpi_fwnode_device_is_available, \
		.property_present = acpi_fwnode_property_present,	\
		.property_read_int_array =				\
			acpi_fwnode_property_read_int_array,		\
		.property_read_string_array =				\
			acpi_fwnode_property_read_string_array,		\
		.get_parent = acpi_node_get_parent,			\
		.get_next_child_node = acpi_get_next_subnode,		\
		.get_named_child_node = acpi_fwnode_get_named_child_node, \
		.graph_get_next_endpoint =				\
			acpi_fwnode_graph_get_next_endpoint,		\
		.graph_get_remote_endpoint =				\
			acpi_fwnode_graph_get_remote_endpoint,		\
		.graph_get_port_parent = acpi_node_get_parent,		\
		.graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
	};								\
	EXPORT_SYMBOL_GPL(ops)

DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
const struct fwnode_operations acpi_static_fwnode_ops;