1 /* 2 * ACPI device specific properties support. 3 * 4 * Copyright (C) 2014, Intel Corporation 5 * All rights reserved. 6 * 7 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com> 8 * Darren Hart <dvhart@linux.intel.com> 9 * Rafael J. Wysocki <rafael.j.wysocki@intel.com> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 */ 15 16 #include <linux/acpi.h> 17 #include <linux/device.h> 18 #include <linux/export.h> 19 20 #include "internal.h" 21 22 static int acpi_data_get_property_array(const struct acpi_device_data *data, 23 const char *name, 24 acpi_object_type type, 25 const union acpi_object **obj); 26 27 static const guid_t prp_guids[] = { 28 /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */ 29 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c, 30 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01), 31 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */ 32 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3, 33 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4), 34 }; 35 36 static const guid_t ads_guid = 37 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6, 38 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b); 39 40 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, 41 const union acpi_object *desc, 42 struct acpi_device_data *data, 43 struct fwnode_handle *parent); 44 static bool acpi_extract_properties(const union acpi_object *desc, 45 struct acpi_device_data *data); 46 47 static bool acpi_nondev_subnode_extract(const union acpi_object *desc, 48 acpi_handle handle, 49 const union acpi_object *link, 50 struct list_head *list, 51 struct fwnode_handle *parent) 52 { 53 struct acpi_data_node *dn; 54 bool result; 55 56 dn = kzalloc(sizeof(*dn), GFP_KERNEL); 57 if (!dn) 58 return false; 59 60 dn->name = link->package.elements[0].string.pointer; 61 dn->fwnode.ops = &acpi_data_fwnode_ops; 62 dn->parent = parent; 63 INIT_LIST_HEAD(&dn->data.properties); 64 INIT_LIST_HEAD(&dn->data.subnodes); 65 66 result = acpi_extract_properties(desc, &dn->data); 67 68 if (handle) { 69 acpi_handle scope; 70 acpi_status status; 71 72 /* 73 * The scope for the subnode object lookup is the one of the 74 * namespace node (device) containing the object that has 75 * returned the package. That is, it's the scope of that 76 * object's parent. 77 */ 78 status = acpi_get_parent(handle, &scope); 79 if (ACPI_SUCCESS(status) 80 && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data, 81 &dn->fwnode)) 82 result = true; 83 } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data, 84 &dn->fwnode)) { 85 result = true; 86 } 87 88 if (result) { 89 dn->handle = handle; 90 dn->data.pointer = desc; 91 list_add_tail(&dn->sibling, list); 92 return true; 93 } 94 95 kfree(dn); 96 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n"); 97 return false; 98 } 99 100 static bool acpi_nondev_subnode_data_ok(acpi_handle handle, 101 const union acpi_object *link, 102 struct list_head *list, 103 struct fwnode_handle *parent) 104 { 105 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 106 acpi_status status; 107 108 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf, 109 ACPI_TYPE_PACKAGE); 110 if (ACPI_FAILURE(status)) 111 return false; 112 113 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list, 114 parent)) 115 return true; 116 117 ACPI_FREE(buf.pointer); 118 return false; 119 } 120 121 static bool acpi_nondev_subnode_ok(acpi_handle scope, 122 const union acpi_object *link, 123 struct list_head *list, 124 struct fwnode_handle *parent) 125 { 126 acpi_handle handle; 127 acpi_status status; 128 129 if (!scope) 130 return false; 131 132 status = acpi_get_handle(scope, link->package.elements[1].string.pointer, 133 &handle); 134 if (ACPI_FAILURE(status)) 135 return false; 136 137 return acpi_nondev_subnode_data_ok(handle, link, list, parent); 138 } 139 140 static int acpi_add_nondev_subnodes(acpi_handle scope, 141 const union acpi_object *links, 142 struct list_head *list, 143 struct fwnode_handle *parent) 144 { 145 bool ret = false; 146 int i; 147 148 for (i = 0; i < links->package.count; i++) { 149 const union acpi_object *link, *desc; 150 acpi_handle handle; 151 bool result; 152 153 link = &links->package.elements[i]; 154 /* Only two elements allowed. */ 155 if (link->package.count != 2) 156 continue; 157 158 /* The first one must be a string. */ 159 if (link->package.elements[0].type != ACPI_TYPE_STRING) 160 continue; 161 162 /* The second one may be a string, a reference or a package. */ 163 switch (link->package.elements[1].type) { 164 case ACPI_TYPE_STRING: 165 result = acpi_nondev_subnode_ok(scope, link, list, 166 parent); 167 break; 168 case ACPI_TYPE_LOCAL_REFERENCE: 169 handle = link->package.elements[1].reference.handle; 170 result = acpi_nondev_subnode_data_ok(handle, link, list, 171 parent); 172 break; 173 case ACPI_TYPE_PACKAGE: 174 desc = &link->package.elements[1]; 175 result = acpi_nondev_subnode_extract(desc, NULL, link, 176 list, parent); 177 break; 178 default: 179 result = false; 180 break; 181 } 182 ret = ret || result; 183 } 184 185 return ret; 186 } 187 188 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, 189 const union acpi_object *desc, 190 struct acpi_device_data *data, 191 struct fwnode_handle *parent) 192 { 193 int i; 194 195 /* Look for the ACPI data subnodes GUID. */ 196 for (i = 0; i < desc->package.count; i += 2) { 197 const union acpi_object *guid, *links; 198 199 guid = &desc->package.elements[i]; 200 links = &desc->package.elements[i + 1]; 201 202 /* 203 * The first element must be a GUID and the second one must be 204 * a package. 205 */ 206 if (guid->type != ACPI_TYPE_BUFFER || 207 guid->buffer.length != 16 || 208 links->type != ACPI_TYPE_PACKAGE) 209 break; 210 211 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid)) 212 continue; 213 214 return acpi_add_nondev_subnodes(scope, links, &data->subnodes, 215 parent); 216 } 217 218 return false; 219 } 220 221 static bool acpi_property_value_ok(const union acpi_object *value) 222 { 223 int j; 224 225 /* 226 * The value must be an integer, a string, a reference, or a package 227 * whose every element must be an integer, a string, or a reference. 228 */ 229 switch (value->type) { 230 case ACPI_TYPE_INTEGER: 231 case ACPI_TYPE_STRING: 232 case ACPI_TYPE_LOCAL_REFERENCE: 233 return true; 234 235 case ACPI_TYPE_PACKAGE: 236 for (j = 0; j < value->package.count; j++) 237 switch (value->package.elements[j].type) { 238 case ACPI_TYPE_INTEGER: 239 case ACPI_TYPE_STRING: 240 case ACPI_TYPE_LOCAL_REFERENCE: 241 continue; 242 243 default: 244 return false; 245 } 246 247 return true; 248 } 249 return false; 250 } 251 252 static bool acpi_properties_format_valid(const union acpi_object *properties) 253 { 254 int i; 255 256 for (i = 0; i < properties->package.count; i++) { 257 const union acpi_object *property; 258 259 property = &properties->package.elements[i]; 260 /* 261 * Only two elements allowed, the first one must be a string and 262 * the second one has to satisfy certain conditions. 263 */ 264 if (property->package.count != 2 265 || property->package.elements[0].type != ACPI_TYPE_STRING 266 || !acpi_property_value_ok(&property->package.elements[1])) 267 return false; 268 } 269 return true; 270 } 271 272 static void acpi_init_of_compatible(struct acpi_device *adev) 273 { 274 const union acpi_object *of_compatible; 275 int ret; 276 277 ret = acpi_data_get_property_array(&adev->data, "compatible", 278 ACPI_TYPE_STRING, &of_compatible); 279 if (ret) { 280 ret = acpi_dev_get_property(adev, "compatible", 281 ACPI_TYPE_STRING, &of_compatible); 282 if (ret) { 283 if (adev->parent 284 && adev->parent->flags.of_compatible_ok) 285 goto out; 286 287 return; 288 } 289 } 290 adev->data.of_compatible = of_compatible; 291 292 out: 293 adev->flags.of_compatible_ok = 1; 294 } 295 296 static bool acpi_is_property_guid(const guid_t *guid) 297 { 298 int i; 299 300 for (i = 0; i < ARRAY_SIZE(prp_guids); i++) { 301 if (guid_equal(guid, &prp_guids[i])) 302 return true; 303 } 304 305 return false; 306 } 307 308 struct acpi_device_properties * 309 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid, 310 const union acpi_object *properties) 311 { 312 struct acpi_device_properties *props; 313 314 props = kzalloc(sizeof(*props), GFP_KERNEL); 315 if (props) { 316 INIT_LIST_HEAD(&props->list); 317 props->guid = guid; 318 props->properties = properties; 319 list_add_tail(&props->list, &data->properties); 320 } 321 322 return props; 323 } 324 325 static bool acpi_extract_properties(const union acpi_object *desc, 326 struct acpi_device_data *data) 327 { 328 int i; 329 330 if (desc->package.count % 2) 331 return false; 332 333 /* Look for the device properties GUID. */ 334 for (i = 0; i < desc->package.count; i += 2) { 335 const union acpi_object *guid, *properties; 336 337 guid = &desc->package.elements[i]; 338 properties = &desc->package.elements[i + 1]; 339 340 /* 341 * The first element must be a GUID and the second one must be 342 * a package. 343 */ 344 if (guid->type != ACPI_TYPE_BUFFER || 345 guid->buffer.length != 16 || 346 properties->type != ACPI_TYPE_PACKAGE) 347 break; 348 349 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer)) 350 continue; 351 352 /* 353 * We found the matching GUID. Now validate the format of the 354 * package immediately following it. 355 */ 356 if (!acpi_properties_format_valid(properties)) 357 continue; 358 359 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer, 360 properties); 361 } 362 363 return !list_empty(&data->properties); 364 } 365 366 void acpi_init_properties(struct acpi_device *adev) 367 { 368 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 369 struct acpi_hardware_id *hwid; 370 acpi_status status; 371 bool acpi_of = false; 372 373 INIT_LIST_HEAD(&adev->data.properties); 374 INIT_LIST_HEAD(&adev->data.subnodes); 375 376 if (!adev->handle) 377 return; 378 379 /* 380 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in 381 * Device Tree compatible properties for this device. 382 */ 383 list_for_each_entry(hwid, &adev->pnp.ids, list) { 384 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) { 385 acpi_of = true; 386 break; 387 } 388 } 389 390 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf, 391 ACPI_TYPE_PACKAGE); 392 if (ACPI_FAILURE(status)) 393 goto out; 394 395 if (acpi_extract_properties(buf.pointer, &adev->data)) { 396 adev->data.pointer = buf.pointer; 397 if (acpi_of) 398 acpi_init_of_compatible(adev); 399 } 400 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, 401 &adev->data, acpi_fwnode_handle(adev))) 402 adev->data.pointer = buf.pointer; 403 404 if (!adev->data.pointer) { 405 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n"); 406 ACPI_FREE(buf.pointer); 407 } 408 409 out: 410 if (acpi_of && !adev->flags.of_compatible_ok) 411 acpi_handle_info(adev->handle, 412 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n"); 413 414 if (!adev->data.pointer) 415 acpi_extract_apple_properties(adev); 416 } 417 418 static void acpi_destroy_nondev_subnodes(struct list_head *list) 419 { 420 struct acpi_data_node *dn, *next; 421 422 if (list_empty(list)) 423 return; 424 425 list_for_each_entry_safe_reverse(dn, next, list, sibling) { 426 acpi_destroy_nondev_subnodes(&dn->data.subnodes); 427 wait_for_completion(&dn->kobj_done); 428 list_del(&dn->sibling); 429 ACPI_FREE((void *)dn->data.pointer); 430 kfree(dn); 431 } 432 } 433 434 void acpi_free_properties(struct acpi_device *adev) 435 { 436 struct acpi_device_properties *props, *tmp; 437 438 acpi_destroy_nondev_subnodes(&adev->data.subnodes); 439 ACPI_FREE((void *)adev->data.pointer); 440 adev->data.of_compatible = NULL; 441 adev->data.pointer = NULL; 442 list_for_each_entry_safe(props, tmp, &adev->data.properties, list) { 443 list_del(&props->list); 444 kfree(props); 445 } 446 } 447 448 /** 449 * acpi_data_get_property - return an ACPI property with given name 450 * @data: ACPI device deta object to get the property from 451 * @name: Name of the property 452 * @type: Expected property type 453 * @obj: Location to store the property value (if not %NULL) 454 * 455 * Look up a property with @name and store a pointer to the resulting ACPI 456 * object at the location pointed to by @obj if found. 457 * 458 * Callers must not attempt to free the returned objects. These objects will be 459 * freed by the ACPI core automatically during the removal of @data. 460 * 461 * Return: %0 if property with @name has been found (success), 462 * %-EINVAL if the arguments are invalid, 463 * %-EINVAL if the property doesn't exist, 464 * %-EPROTO if the property value type doesn't match @type. 465 */ 466 static int acpi_data_get_property(const struct acpi_device_data *data, 467 const char *name, acpi_object_type type, 468 const union acpi_object **obj) 469 { 470 const struct acpi_device_properties *props; 471 472 if (!data || !name) 473 return -EINVAL; 474 475 if (!data->pointer || list_empty(&data->properties)) 476 return -EINVAL; 477 478 list_for_each_entry(props, &data->properties, list) { 479 const union acpi_object *properties; 480 unsigned int i; 481 482 properties = props->properties; 483 for (i = 0; i < properties->package.count; i++) { 484 const union acpi_object *propname, *propvalue; 485 const union acpi_object *property; 486 487 property = &properties->package.elements[i]; 488 489 propname = &property->package.elements[0]; 490 propvalue = &property->package.elements[1]; 491 492 if (!strcmp(name, propname->string.pointer)) { 493 if (type != ACPI_TYPE_ANY && 494 propvalue->type != type) 495 return -EPROTO; 496 if (obj) 497 *obj = propvalue; 498 499 return 0; 500 } 501 } 502 } 503 return -EINVAL; 504 } 505 506 /** 507 * acpi_dev_get_property - return an ACPI property with given name. 508 * @adev: ACPI device to get the property from. 509 * @name: Name of the property. 510 * @type: Expected property type. 511 * @obj: Location to store the property value (if not %NULL). 512 */ 513 int acpi_dev_get_property(const struct acpi_device *adev, const char *name, 514 acpi_object_type type, const union acpi_object **obj) 515 { 516 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL; 517 } 518 EXPORT_SYMBOL_GPL(acpi_dev_get_property); 519 520 static const struct acpi_device_data * 521 acpi_device_data_of_node(const struct fwnode_handle *fwnode) 522 { 523 if (is_acpi_device_node(fwnode)) { 524 const struct acpi_device *adev = to_acpi_device_node(fwnode); 525 return &adev->data; 526 } else if (is_acpi_data_node(fwnode)) { 527 const struct acpi_data_node *dn = to_acpi_data_node(fwnode); 528 return &dn->data; 529 } 530 return NULL; 531 } 532 533 /** 534 * acpi_node_prop_get - return an ACPI property with given name. 535 * @fwnode: Firmware node to get the property from. 536 * @propname: Name of the property. 537 * @valptr: Location to store a pointer to the property value (if not %NULL). 538 */ 539 int acpi_node_prop_get(const struct fwnode_handle *fwnode, 540 const char *propname, void **valptr) 541 { 542 return acpi_data_get_property(acpi_device_data_of_node(fwnode), 543 propname, ACPI_TYPE_ANY, 544 (const union acpi_object **)valptr); 545 } 546 547 /** 548 * acpi_data_get_property_array - return an ACPI array property with given name 549 * @adev: ACPI data object to get the property from 550 * @name: Name of the property 551 * @type: Expected type of array elements 552 * @obj: Location to store a pointer to the property value (if not NULL) 553 * 554 * Look up an array property with @name and store a pointer to the resulting 555 * ACPI object at the location pointed to by @obj if found. 556 * 557 * Callers must not attempt to free the returned objects. Those objects will be 558 * freed by the ACPI core automatically during the removal of @data. 559 * 560 * Return: %0 if array property (package) with @name has been found (success), 561 * %-EINVAL if the arguments are invalid, 562 * %-EINVAL if the property doesn't exist, 563 * %-EPROTO if the property is not a package or the type of its elements 564 * doesn't match @type. 565 */ 566 static int acpi_data_get_property_array(const struct acpi_device_data *data, 567 const char *name, 568 acpi_object_type type, 569 const union acpi_object **obj) 570 { 571 const union acpi_object *prop; 572 int ret, i; 573 574 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop); 575 if (ret) 576 return ret; 577 578 if (type != ACPI_TYPE_ANY) { 579 /* Check that all elements are of correct type. */ 580 for (i = 0; i < prop->package.count; i++) 581 if (prop->package.elements[i].type != type) 582 return -EPROTO; 583 } 584 if (obj) 585 *obj = prop; 586 587 return 0; 588 } 589 590 static struct fwnode_handle * 591 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 592 const char *childname) 593 { 594 struct fwnode_handle *child; 595 596 /* 597 * Find first matching named child node of this fwnode. 598 * For ACPI this will be a data only sub-node. 599 */ 600 fwnode_for_each_child_node(fwnode, child) 601 if (acpi_data_node_match(child, childname)) 602 return child; 603 604 return NULL; 605 } 606 607 /** 608 * __acpi_node_get_property_reference - returns handle to the referenced object 609 * @fwnode: Firmware node to get the property from 610 * @propname: Name of the property 611 * @index: Index of the reference to return 612 * @num_args: Maximum number of arguments after each reference 613 * @args: Location to store the returned reference with optional arguments 614 * 615 * Find property with @name, verifify that it is a package containing at least 616 * one object reference and if so, store the ACPI device object pointer to the 617 * target object in @args->adev. If the reference includes arguments, store 618 * them in the @args->args[] array. 619 * 620 * If there's more than one reference in the property value package, @index is 621 * used to select the one to return. 622 * 623 * It is possible to leave holes in the property value set like in the 624 * example below: 625 * 626 * Package () { 627 * "cs-gpios", 628 * Package () { 629 * ^GPIO, 19, 0, 0, 630 * ^GPIO, 20, 0, 0, 631 * 0, 632 * ^GPIO, 21, 0, 0, 633 * } 634 * } 635 * 636 * Calling this function with index %2 or index %3 return %-ENOENT. If the 637 * property does not contain any more values %-ENOENT is returned. The NULL 638 * entry must be single integer and preferably contain value %0. 639 * 640 * Return: %0 on success, negative error code on failure. 641 */ 642 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode, 643 const char *propname, size_t index, size_t num_args, 644 struct fwnode_reference_args *args) 645 { 646 const union acpi_object *element, *end; 647 const union acpi_object *obj; 648 const struct acpi_device_data *data; 649 struct acpi_device *device; 650 int ret, idx = 0; 651 652 data = acpi_device_data_of_node(fwnode); 653 if (!data) 654 return -ENOENT; 655 656 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj); 657 if (ret) 658 return ret == -EINVAL ? -ENOENT : -EINVAL; 659 660 /* 661 * The simplest case is when the value is a single reference. Just 662 * return that reference then. 663 */ 664 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) { 665 if (index) 666 return -EINVAL; 667 668 ret = acpi_bus_get_device(obj->reference.handle, &device); 669 if (ret) 670 return ret == -ENODEV ? -EINVAL : ret; 671 672 args->fwnode = acpi_fwnode_handle(device); 673 args->nargs = 0; 674 return 0; 675 } 676 677 /* 678 * If it is not a single reference, then it is a package of 679 * references followed by number of ints as follows: 680 * 681 * Package () { REF, INT, REF, INT, INT } 682 * 683 * The index argument is then used to determine which reference 684 * the caller wants (along with the arguments). 685 */ 686 if (obj->type != ACPI_TYPE_PACKAGE) 687 return -EINVAL; 688 if (index >= obj->package.count) 689 return -ENOENT; 690 691 element = obj->package.elements; 692 end = element + obj->package.count; 693 694 while (element < end) { 695 u32 nargs, i; 696 697 if (element->type == ACPI_TYPE_LOCAL_REFERENCE) { 698 struct fwnode_handle *ref_fwnode; 699 700 ret = acpi_bus_get_device(element->reference.handle, 701 &device); 702 if (ret) 703 return -EINVAL; 704 705 nargs = 0; 706 element++; 707 708 /* 709 * Find the referred data extension node under the 710 * referred device node. 711 */ 712 for (ref_fwnode = acpi_fwnode_handle(device); 713 element < end && element->type == ACPI_TYPE_STRING; 714 element++) { 715 ref_fwnode = acpi_fwnode_get_named_child_node( 716 ref_fwnode, element->string.pointer); 717 if (!ref_fwnode) 718 return -EINVAL; 719 } 720 721 /* assume following integer elements are all args */ 722 for (i = 0; element + i < end && i < num_args; i++) { 723 int type = element[i].type; 724 725 if (type == ACPI_TYPE_INTEGER) 726 nargs++; 727 else if (type == ACPI_TYPE_LOCAL_REFERENCE) 728 break; 729 else 730 return -EINVAL; 731 } 732 733 if (nargs > NR_FWNODE_REFERENCE_ARGS) 734 return -EINVAL; 735 736 if (idx == index) { 737 args->fwnode = ref_fwnode; 738 args->nargs = nargs; 739 for (i = 0; i < nargs; i++) 740 args->args[i] = element[i].integer.value; 741 742 return 0; 743 } 744 745 element += nargs; 746 } else if (element->type == ACPI_TYPE_INTEGER) { 747 if (idx == index) 748 return -ENOENT; 749 element++; 750 } else { 751 return -EINVAL; 752 } 753 754 idx++; 755 } 756 757 return -ENOENT; 758 } 759 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference); 760 761 static int acpi_data_prop_read_single(const struct acpi_device_data *data, 762 const char *propname, 763 enum dev_prop_type proptype, void *val) 764 { 765 const union acpi_object *obj; 766 int ret; 767 768 if (!val) 769 return -EINVAL; 770 771 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) { 772 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj); 773 if (ret) 774 return ret; 775 776 switch (proptype) { 777 case DEV_PROP_U8: 778 if (obj->integer.value > U8_MAX) 779 return -EOVERFLOW; 780 *(u8 *)val = obj->integer.value; 781 break; 782 case DEV_PROP_U16: 783 if (obj->integer.value > U16_MAX) 784 return -EOVERFLOW; 785 *(u16 *)val = obj->integer.value; 786 break; 787 case DEV_PROP_U32: 788 if (obj->integer.value > U32_MAX) 789 return -EOVERFLOW; 790 *(u32 *)val = obj->integer.value; 791 break; 792 default: 793 *(u64 *)val = obj->integer.value; 794 break; 795 } 796 } else if (proptype == DEV_PROP_STRING) { 797 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj); 798 if (ret) 799 return ret; 800 801 *(char **)val = obj->string.pointer; 802 803 return 1; 804 } else { 805 ret = -EINVAL; 806 } 807 return ret; 808 } 809 810 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname, 811 enum dev_prop_type proptype, void *val) 812 { 813 int ret; 814 815 if (!adev) 816 return -EINVAL; 817 818 ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val); 819 if (ret < 0 || proptype != ACPI_TYPE_STRING) 820 return ret; 821 return 0; 822 } 823 824 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val, 825 size_t nval) 826 { 827 int i; 828 829 for (i = 0; i < nval; i++) { 830 if (items[i].type != ACPI_TYPE_INTEGER) 831 return -EPROTO; 832 if (items[i].integer.value > U8_MAX) 833 return -EOVERFLOW; 834 835 val[i] = items[i].integer.value; 836 } 837 return 0; 838 } 839 840 static int acpi_copy_property_array_u16(const union acpi_object *items, 841 u16 *val, size_t nval) 842 { 843 int i; 844 845 for (i = 0; i < nval; i++) { 846 if (items[i].type != ACPI_TYPE_INTEGER) 847 return -EPROTO; 848 if (items[i].integer.value > U16_MAX) 849 return -EOVERFLOW; 850 851 val[i] = items[i].integer.value; 852 } 853 return 0; 854 } 855 856 static int acpi_copy_property_array_u32(const union acpi_object *items, 857 u32 *val, size_t nval) 858 { 859 int i; 860 861 for (i = 0; i < nval; i++) { 862 if (items[i].type != ACPI_TYPE_INTEGER) 863 return -EPROTO; 864 if (items[i].integer.value > U32_MAX) 865 return -EOVERFLOW; 866 867 val[i] = items[i].integer.value; 868 } 869 return 0; 870 } 871 872 static int acpi_copy_property_array_u64(const union acpi_object *items, 873 u64 *val, size_t nval) 874 { 875 int i; 876 877 for (i = 0; i < nval; i++) { 878 if (items[i].type != ACPI_TYPE_INTEGER) 879 return -EPROTO; 880 881 val[i] = items[i].integer.value; 882 } 883 return 0; 884 } 885 886 static int acpi_copy_property_array_string(const union acpi_object *items, 887 char **val, size_t nval) 888 { 889 int i; 890 891 for (i = 0; i < nval; i++) { 892 if (items[i].type != ACPI_TYPE_STRING) 893 return -EPROTO; 894 895 val[i] = items[i].string.pointer; 896 } 897 return nval; 898 } 899 900 static int acpi_data_prop_read(const struct acpi_device_data *data, 901 const char *propname, 902 enum dev_prop_type proptype, 903 void *val, size_t nval) 904 { 905 const union acpi_object *obj; 906 const union acpi_object *items; 907 int ret; 908 909 if (val && nval == 1) { 910 ret = acpi_data_prop_read_single(data, propname, proptype, val); 911 if (ret >= 0) 912 return ret; 913 } 914 915 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj); 916 if (ret) 917 return ret; 918 919 if (!val) 920 return obj->package.count; 921 922 if (proptype != DEV_PROP_STRING && nval > obj->package.count) 923 return -EOVERFLOW; 924 else if (nval <= 0) 925 return -EINVAL; 926 927 items = obj->package.elements; 928 929 switch (proptype) { 930 case DEV_PROP_U8: 931 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval); 932 break; 933 case DEV_PROP_U16: 934 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval); 935 break; 936 case DEV_PROP_U32: 937 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval); 938 break; 939 case DEV_PROP_U64: 940 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval); 941 break; 942 case DEV_PROP_STRING: 943 ret = acpi_copy_property_array_string( 944 items, (char **)val, 945 min_t(u32, nval, obj->package.count)); 946 break; 947 default: 948 ret = -EINVAL; 949 break; 950 } 951 return ret; 952 } 953 954 int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname, 955 enum dev_prop_type proptype, void *val, size_t nval) 956 { 957 return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL; 958 } 959 960 /** 961 * acpi_node_prop_read - retrieve the value of an ACPI property with given name. 962 * @fwnode: Firmware node to get the property from. 963 * @propname: Name of the property. 964 * @proptype: Expected property type. 965 * @val: Location to store the property value (if not %NULL). 966 * @nval: Size of the array pointed to by @val. 967 * 968 * If @val is %NULL, return the number of array elements comprising the value 969 * of the property. Otherwise, read at most @nval values to the array at the 970 * location pointed to by @val. 971 */ 972 int acpi_node_prop_read(const struct fwnode_handle *fwnode, 973 const char *propname, enum dev_prop_type proptype, 974 void *val, size_t nval) 975 { 976 return acpi_data_prop_read(acpi_device_data_of_node(fwnode), 977 propname, proptype, val, nval); 978 } 979 980 /** 981 * acpi_get_next_subnode - Return the next child node handle for a fwnode 982 * @fwnode: Firmware node to find the next child node for. 983 * @child: Handle to one of the device's child nodes or a null handle. 984 */ 985 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode, 986 struct fwnode_handle *child) 987 { 988 const struct acpi_device *adev = to_acpi_device_node(fwnode); 989 const struct list_head *head; 990 struct list_head *next; 991 992 if (!child || is_acpi_device_node(child)) { 993 struct acpi_device *child_adev; 994 995 if (adev) 996 head = &adev->children; 997 else 998 goto nondev; 999 1000 if (list_empty(head)) 1001 goto nondev; 1002 1003 if (child) { 1004 adev = to_acpi_device_node(child); 1005 next = adev->node.next; 1006 if (next == head) { 1007 child = NULL; 1008 goto nondev; 1009 } 1010 child_adev = list_entry(next, struct acpi_device, node); 1011 } else { 1012 child_adev = list_first_entry(head, struct acpi_device, 1013 node); 1014 } 1015 return acpi_fwnode_handle(child_adev); 1016 } 1017 1018 nondev: 1019 if (!child || is_acpi_data_node(child)) { 1020 const struct acpi_data_node *data = to_acpi_data_node(fwnode); 1021 struct acpi_data_node *dn; 1022 1023 if (adev) 1024 head = &adev->data.subnodes; 1025 else if (data) 1026 head = &data->data.subnodes; 1027 else 1028 return NULL; 1029 1030 if (list_empty(head)) 1031 return NULL; 1032 1033 if (child) { 1034 dn = to_acpi_data_node(child); 1035 next = dn->sibling.next; 1036 if (next == head) 1037 return NULL; 1038 1039 dn = list_entry(next, struct acpi_data_node, sibling); 1040 } else { 1041 dn = list_first_entry(head, struct acpi_data_node, sibling); 1042 } 1043 return &dn->fwnode; 1044 } 1045 return NULL; 1046 } 1047 1048 /** 1049 * acpi_node_get_parent - Return parent fwnode of this fwnode 1050 * @fwnode: Firmware node whose parent to get 1051 * 1052 * Returns parent node of an ACPI device or data firmware node or %NULL if 1053 * not available. 1054 */ 1055 struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode) 1056 { 1057 if (is_acpi_data_node(fwnode)) { 1058 /* All data nodes have parent pointer so just return that */ 1059 return to_acpi_data_node(fwnode)->parent; 1060 } else if (is_acpi_device_node(fwnode)) { 1061 acpi_handle handle, parent_handle; 1062 1063 handle = to_acpi_device_node(fwnode)->handle; 1064 if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) { 1065 struct acpi_device *adev; 1066 1067 if (!acpi_bus_get_device(parent_handle, &adev)) 1068 return acpi_fwnode_handle(adev); 1069 } 1070 } 1071 1072 return NULL; 1073 } 1074 1075 /* 1076 * Return true if the node is an ACPI graph node. Called on either ports 1077 * or endpoints. 1078 */ 1079 static bool is_acpi_graph_node(struct fwnode_handle *fwnode, 1080 const char *str) 1081 { 1082 unsigned int len = strlen(str); 1083 const char *name; 1084 1085 if (!len || !is_acpi_data_node(fwnode)) 1086 return false; 1087 1088 name = to_acpi_data_node(fwnode)->name; 1089 1090 return (fwnode_property_present(fwnode, "reg") && 1091 !strncmp(name, str, len) && name[len] == '@') || 1092 fwnode_property_present(fwnode, str); 1093 } 1094 1095 /** 1096 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node 1097 * @fwnode: Pointer to the parent firmware node 1098 * @prev: Previous endpoint node or %NULL to get the first 1099 * 1100 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns 1101 * %NULL if there is no next endpoint or in case of error. In case of success 1102 * the next endpoint is returned. 1103 */ 1104 static struct fwnode_handle *acpi_graph_get_next_endpoint( 1105 const struct fwnode_handle *fwnode, struct fwnode_handle *prev) 1106 { 1107 struct fwnode_handle *port = NULL; 1108 struct fwnode_handle *endpoint; 1109 1110 if (!prev) { 1111 do { 1112 port = fwnode_get_next_child_node(fwnode, port); 1113 /* 1114 * The names of the port nodes begin with "port@" 1115 * followed by the number of the port node and they also 1116 * have a "reg" property that also has the number of the 1117 * port node. For compatibility reasons a node is also 1118 * recognised as a port node from the "port" property. 1119 */ 1120 if (is_acpi_graph_node(port, "port")) 1121 break; 1122 } while (port); 1123 } else { 1124 port = fwnode_get_parent(prev); 1125 } 1126 1127 if (!port) 1128 return NULL; 1129 1130 endpoint = fwnode_get_next_child_node(port, prev); 1131 while (!endpoint) { 1132 port = fwnode_get_next_child_node(fwnode, port); 1133 if (!port) 1134 break; 1135 if (is_acpi_graph_node(port, "port")) 1136 endpoint = fwnode_get_next_child_node(port, NULL); 1137 } 1138 1139 /* 1140 * The names of the endpoint nodes begin with "endpoint@" followed by 1141 * the number of the endpoint node and they also have a "reg" property 1142 * that also has the number of the endpoint node. For compatibility 1143 * reasons a node is also recognised as an endpoint node from the 1144 * "endpoint" property. 1145 */ 1146 if (!is_acpi_graph_node(endpoint, "endpoint")) 1147 return NULL; 1148 1149 return endpoint; 1150 } 1151 1152 /** 1153 * acpi_graph_get_child_prop_value - Return a child with a given property value 1154 * @fwnode: device fwnode 1155 * @prop_name: The name of the property to look for 1156 * @val: the desired property value 1157 * 1158 * Return the port node corresponding to a given port number. Returns 1159 * the child node on success, NULL otherwise. 1160 */ 1161 static struct fwnode_handle *acpi_graph_get_child_prop_value( 1162 const struct fwnode_handle *fwnode, const char *prop_name, 1163 unsigned int val) 1164 { 1165 struct fwnode_handle *child; 1166 1167 fwnode_for_each_child_node(fwnode, child) { 1168 u32 nr; 1169 1170 if (fwnode_property_read_u32(child, prop_name, &nr)) 1171 continue; 1172 1173 if (val == nr) 1174 return child; 1175 } 1176 1177 return NULL; 1178 } 1179 1180 1181 /** 1182 * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint 1183 * @fwnode: Endpoint firmware node pointing to a remote device 1184 * @endpoint: Firmware node of remote endpoint is filled here if not %NULL 1185 * 1186 * Returns the remote endpoint corresponding to @__fwnode. NULL on error. 1187 */ 1188 static struct fwnode_handle * 1189 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode) 1190 { 1191 struct fwnode_handle *fwnode; 1192 unsigned int port_nr, endpoint_nr; 1193 struct fwnode_reference_args args; 1194 int ret; 1195 1196 memset(&args, 0, sizeof(args)); 1197 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0, 1198 &args); 1199 if (ret) 1200 return NULL; 1201 1202 /* Direct endpoint reference? */ 1203 if (!is_acpi_device_node(args.fwnode)) 1204 return args.nargs ? NULL : args.fwnode; 1205 1206 /* 1207 * Always require two arguments with the reference: port and 1208 * endpoint indices. 1209 */ 1210 if (args.nargs != 2) 1211 return NULL; 1212 1213 fwnode = args.fwnode; 1214 port_nr = args.args[0]; 1215 endpoint_nr = args.args[1]; 1216 1217 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr); 1218 1219 return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr); 1220 } 1221 1222 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode) 1223 { 1224 if (!is_acpi_device_node(fwnode)) 1225 return false; 1226 1227 return acpi_device_is_present(to_acpi_device_node(fwnode)); 1228 } 1229 1230 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode, 1231 const char *propname) 1232 { 1233 return !acpi_node_prop_get(fwnode, propname, NULL); 1234 } 1235 1236 static int 1237 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode, 1238 const char *propname, 1239 unsigned int elem_size, void *val, 1240 size_t nval) 1241 { 1242 enum dev_prop_type type; 1243 1244 switch (elem_size) { 1245 case sizeof(u8): 1246 type = DEV_PROP_U8; 1247 break; 1248 case sizeof(u16): 1249 type = DEV_PROP_U16; 1250 break; 1251 case sizeof(u32): 1252 type = DEV_PROP_U32; 1253 break; 1254 case sizeof(u64): 1255 type = DEV_PROP_U64; 1256 break; 1257 default: 1258 return -ENXIO; 1259 } 1260 1261 return acpi_node_prop_read(fwnode, propname, type, val, nval); 1262 } 1263 1264 static int 1265 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 1266 const char *propname, const char **val, 1267 size_t nval) 1268 { 1269 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING, 1270 val, nval); 1271 } 1272 1273 static int 1274 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode, 1275 const char *prop, const char *nargs_prop, 1276 unsigned int args_count, unsigned int index, 1277 struct fwnode_reference_args *args) 1278 { 1279 return __acpi_node_get_property_reference(fwnode, prop, index, 1280 args_count, args); 1281 } 1282 1283 static struct fwnode_handle * 1284 acpi_fwnode_get_parent(struct fwnode_handle *fwnode) 1285 { 1286 return acpi_node_get_parent(fwnode); 1287 } 1288 1289 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, 1290 struct fwnode_endpoint *endpoint) 1291 { 1292 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode); 1293 1294 endpoint->local_fwnode = fwnode; 1295 1296 if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port)) 1297 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port); 1298 if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id)) 1299 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id); 1300 1301 return 0; 1302 } 1303 1304 static const void * 1305 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode, 1306 const struct device *dev) 1307 { 1308 return acpi_device_get_match_data(dev); 1309 } 1310 1311 #define DECLARE_ACPI_FWNODE_OPS(ops) \ 1312 const struct fwnode_operations ops = { \ 1313 .device_is_available = acpi_fwnode_device_is_available, \ 1314 .device_get_match_data = acpi_fwnode_device_get_match_data, \ 1315 .property_present = acpi_fwnode_property_present, \ 1316 .property_read_int_array = \ 1317 acpi_fwnode_property_read_int_array, \ 1318 .property_read_string_array = \ 1319 acpi_fwnode_property_read_string_array, \ 1320 .get_parent = acpi_node_get_parent, \ 1321 .get_next_child_node = acpi_get_next_subnode, \ 1322 .get_named_child_node = acpi_fwnode_get_named_child_node, \ 1323 .get_reference_args = acpi_fwnode_get_reference_args, \ 1324 .graph_get_next_endpoint = \ 1325 acpi_graph_get_next_endpoint, \ 1326 .graph_get_remote_endpoint = \ 1327 acpi_graph_get_remote_endpoint, \ 1328 .graph_get_port_parent = acpi_fwnode_get_parent, \ 1329 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \ 1330 }; \ 1331 EXPORT_SYMBOL_GPL(ops) 1332 1333 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops); 1334 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops); 1335 const struct fwnode_operations acpi_static_fwnode_ops; 1336 1337 bool is_acpi_device_node(const struct fwnode_handle *fwnode) 1338 { 1339 return !IS_ERR_OR_NULL(fwnode) && 1340 fwnode->ops == &acpi_device_fwnode_ops; 1341 } 1342 EXPORT_SYMBOL(is_acpi_device_node); 1343 1344 bool is_acpi_data_node(const struct fwnode_handle *fwnode) 1345 { 1346 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops; 1347 } 1348 EXPORT_SYMBOL(is_acpi_data_node); 1349