1 /* 2 * property.c - Unified device property interface. 3 * 4 * Copyright (C) 2014, Intel Corporation 5 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 6 * Mika Westerberg <mika.westerberg@linux.intel.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/acpi.h> 14 #include <linux/export.h> 15 #include <linux/kernel.h> 16 #include <linux/of.h> 17 #include <linux/of_address.h> 18 #include <linux/of_graph.h> 19 #include <linux/property.h> 20 #include <linux/etherdevice.h> 21 #include <linux/phy.h> 22 23 struct property_set { 24 struct fwnode_handle fwnode; 25 const struct property_entry *properties; 26 }; 27 28 static inline bool is_pset_node(struct fwnode_handle *fwnode) 29 { 30 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA; 31 } 32 33 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode) 34 { 35 return is_pset_node(fwnode) ? 36 container_of(fwnode, struct property_set, fwnode) : NULL; 37 } 38 39 static const struct property_entry *pset_prop_get(struct property_set *pset, 40 const char *name) 41 { 42 const struct property_entry *prop; 43 44 if (!pset || !pset->properties) 45 return NULL; 46 47 for (prop = pset->properties; prop->name; prop++) 48 if (!strcmp(name, prop->name)) 49 return prop; 50 51 return NULL; 52 } 53 54 static const void *pset_prop_find(struct property_set *pset, 55 const char *propname, size_t length) 56 { 57 const struct property_entry *prop; 58 const void *pointer; 59 60 prop = pset_prop_get(pset, propname); 61 if (!prop) 62 return ERR_PTR(-EINVAL); 63 if (prop->is_array) 64 pointer = prop->pointer.raw_data; 65 else 66 pointer = &prop->value.raw_data; 67 if (!pointer) 68 return ERR_PTR(-ENODATA); 69 if (length > prop->length) 70 return ERR_PTR(-EOVERFLOW); 71 return pointer; 72 } 73 74 static int pset_prop_read_u8_array(struct property_set *pset, 75 const char *propname, 76 u8 *values, size_t nval) 77 { 78 const void *pointer; 79 size_t length = nval * sizeof(*values); 80 81 pointer = pset_prop_find(pset, propname, length); 82 if (IS_ERR(pointer)) 83 return PTR_ERR(pointer); 84 85 memcpy(values, pointer, length); 86 return 0; 87 } 88 89 static int pset_prop_read_u16_array(struct property_set *pset, 90 const char *propname, 91 u16 *values, size_t nval) 92 { 93 const void *pointer; 94 size_t length = nval * sizeof(*values); 95 96 pointer = pset_prop_find(pset, propname, length); 97 if (IS_ERR(pointer)) 98 return PTR_ERR(pointer); 99 100 memcpy(values, pointer, length); 101 return 0; 102 } 103 104 static int pset_prop_read_u32_array(struct property_set *pset, 105 const char *propname, 106 u32 *values, size_t nval) 107 { 108 const void *pointer; 109 size_t length = nval * sizeof(*values); 110 111 pointer = pset_prop_find(pset, propname, length); 112 if (IS_ERR(pointer)) 113 return PTR_ERR(pointer); 114 115 memcpy(values, pointer, length); 116 return 0; 117 } 118 119 static int pset_prop_read_u64_array(struct property_set *pset, 120 const char *propname, 121 u64 *values, size_t nval) 122 { 123 const void *pointer; 124 size_t length = nval * sizeof(*values); 125 126 pointer = pset_prop_find(pset, propname, length); 127 if (IS_ERR(pointer)) 128 return PTR_ERR(pointer); 129 130 memcpy(values, pointer, length); 131 return 0; 132 } 133 134 static int pset_prop_count_elems_of_size(struct property_set *pset, 135 const char *propname, size_t length) 136 { 137 const struct property_entry *prop; 138 139 prop = pset_prop_get(pset, propname); 140 if (!prop) 141 return -EINVAL; 142 143 return prop->length / length; 144 } 145 146 static int pset_prop_read_string_array(struct property_set *pset, 147 const char *propname, 148 const char **strings, size_t nval) 149 { 150 const struct property_entry *prop; 151 const void *pointer; 152 size_t array_len, length; 153 154 /* Find out the array length. */ 155 prop = pset_prop_get(pset, propname); 156 if (!prop) 157 return -EINVAL; 158 159 if (!prop->is_array) 160 /* The array length for a non-array string property is 1. */ 161 array_len = 1; 162 else 163 /* Find the length of an array. */ 164 array_len = pset_prop_count_elems_of_size(pset, propname, 165 sizeof(const char *)); 166 167 /* Return how many there are if strings is NULL. */ 168 if (!strings) 169 return array_len; 170 171 array_len = min(nval, array_len); 172 length = array_len * sizeof(*strings); 173 174 pointer = pset_prop_find(pset, propname, length); 175 if (IS_ERR(pointer)) 176 return PTR_ERR(pointer); 177 178 memcpy(strings, pointer, length); 179 180 return array_len; 181 } 182 183 struct fwnode_handle *dev_fwnode(struct device *dev) 184 { 185 return IS_ENABLED(CONFIG_OF) && dev->of_node ? 186 &dev->of_node->fwnode : dev->fwnode; 187 } 188 EXPORT_SYMBOL_GPL(dev_fwnode); 189 190 static bool pset_fwnode_property_present(struct fwnode_handle *fwnode, 191 const char *propname) 192 { 193 return !!pset_prop_get(to_pset_node(fwnode), propname); 194 } 195 196 static int pset_fwnode_read_int_array(struct fwnode_handle *fwnode, 197 const char *propname, 198 unsigned int elem_size, void *val, 199 size_t nval) 200 { 201 struct property_set *node = to_pset_node(fwnode); 202 203 if (!val) 204 return pset_prop_count_elems_of_size(node, propname, elem_size); 205 206 switch (elem_size) { 207 case sizeof(u8): 208 return pset_prop_read_u8_array(node, propname, val, nval); 209 case sizeof(u16): 210 return pset_prop_read_u16_array(node, propname, val, nval); 211 case sizeof(u32): 212 return pset_prop_read_u32_array(node, propname, val, nval); 213 case sizeof(u64): 214 return pset_prop_read_u64_array(node, propname, val, nval); 215 } 216 217 return -ENXIO; 218 } 219 220 static int pset_fwnode_property_read_string_array(struct fwnode_handle *fwnode, 221 const char *propname, 222 const char **val, size_t nval) 223 { 224 return pset_prop_read_string_array(to_pset_node(fwnode), propname, 225 val, nval); 226 } 227 228 static const struct fwnode_operations pset_fwnode_ops = { 229 .property_present = pset_fwnode_property_present, 230 .property_read_int_array = pset_fwnode_read_int_array, 231 .property_read_string_array = pset_fwnode_property_read_string_array, 232 }; 233 234 /** 235 * device_property_present - check if a property of a device is present 236 * @dev: Device whose property is being checked 237 * @propname: Name of the property 238 * 239 * Check if property @propname is present in the device firmware description. 240 */ 241 bool device_property_present(struct device *dev, const char *propname) 242 { 243 return fwnode_property_present(dev_fwnode(dev), propname); 244 } 245 EXPORT_SYMBOL_GPL(device_property_present); 246 247 /** 248 * fwnode_property_present - check if a property of a firmware node is present 249 * @fwnode: Firmware node whose property to check 250 * @propname: Name of the property 251 */ 252 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname) 253 { 254 bool ret; 255 256 ret = fwnode_call_bool_op(fwnode, property_present, propname); 257 if (ret == false && !IS_ERR_OR_NULL(fwnode) && 258 !IS_ERR_OR_NULL(fwnode->secondary)) 259 ret = fwnode_call_bool_op(fwnode->secondary, property_present, 260 propname); 261 return ret; 262 } 263 EXPORT_SYMBOL_GPL(fwnode_property_present); 264 265 /** 266 * device_property_read_u8_array - return a u8 array property of a device 267 * @dev: Device to get the property of 268 * @propname: Name of the property 269 * @val: The values are stored here or %NULL to return the number of values 270 * @nval: Size of the @val array 271 * 272 * Function reads an array of u8 properties with @propname from the device 273 * firmware description and stores them to @val if found. 274 * 275 * Return: number of values if @val was %NULL, 276 * %0 if the property was found (success), 277 * %-EINVAL if given arguments are not valid, 278 * %-ENODATA if the property does not have a value, 279 * %-EPROTO if the property is not an array of numbers, 280 * %-EOVERFLOW if the size of the property is not as expected. 281 * %-ENXIO if no suitable firmware interface is present. 282 */ 283 int device_property_read_u8_array(struct device *dev, const char *propname, 284 u8 *val, size_t nval) 285 { 286 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); 287 } 288 EXPORT_SYMBOL_GPL(device_property_read_u8_array); 289 290 /** 291 * device_property_read_u16_array - return a u16 array property of a device 292 * @dev: Device to get the property of 293 * @propname: Name of the property 294 * @val: The values are stored here or %NULL to return the number of values 295 * @nval: Size of the @val array 296 * 297 * Function reads an array of u16 properties with @propname from the device 298 * firmware description and stores them to @val if found. 299 * 300 * Return: number of values if @val was %NULL, 301 * %0 if the property was found (success), 302 * %-EINVAL if given arguments are not valid, 303 * %-ENODATA if the property does not have a value, 304 * %-EPROTO if the property is not an array of numbers, 305 * %-EOVERFLOW if the size of the property is not as expected. 306 * %-ENXIO if no suitable firmware interface is present. 307 */ 308 int device_property_read_u16_array(struct device *dev, const char *propname, 309 u16 *val, size_t nval) 310 { 311 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); 312 } 313 EXPORT_SYMBOL_GPL(device_property_read_u16_array); 314 315 /** 316 * device_property_read_u32_array - return a u32 array property of a device 317 * @dev: Device to get the property of 318 * @propname: Name of the property 319 * @val: The values are stored here or %NULL to return the number of values 320 * @nval: Size of the @val array 321 * 322 * Function reads an array of u32 properties with @propname from the device 323 * firmware description and stores them to @val if found. 324 * 325 * Return: number of values if @val was %NULL, 326 * %0 if the property was found (success), 327 * %-EINVAL if given arguments are not valid, 328 * %-ENODATA if the property does not have a value, 329 * %-EPROTO if the property is not an array of numbers, 330 * %-EOVERFLOW if the size of the property is not as expected. 331 * %-ENXIO if no suitable firmware interface is present. 332 */ 333 int device_property_read_u32_array(struct device *dev, const char *propname, 334 u32 *val, size_t nval) 335 { 336 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); 337 } 338 EXPORT_SYMBOL_GPL(device_property_read_u32_array); 339 340 /** 341 * device_property_read_u64_array - return a u64 array property of a device 342 * @dev: Device to get the property of 343 * @propname: Name of the property 344 * @val: The values are stored here or %NULL to return the number of values 345 * @nval: Size of the @val array 346 * 347 * Function reads an array of u64 properties with @propname from the device 348 * firmware description and stores them to @val if found. 349 * 350 * Return: number of values if @val was %NULL, 351 * %0 if the property was found (success), 352 * %-EINVAL if given arguments are not valid, 353 * %-ENODATA if the property does not have a value, 354 * %-EPROTO if the property is not an array of numbers, 355 * %-EOVERFLOW if the size of the property is not as expected. 356 * %-ENXIO if no suitable firmware interface is present. 357 */ 358 int device_property_read_u64_array(struct device *dev, const char *propname, 359 u64 *val, size_t nval) 360 { 361 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); 362 } 363 EXPORT_SYMBOL_GPL(device_property_read_u64_array); 364 365 /** 366 * device_property_read_string_array - return a string array property of device 367 * @dev: Device to get the property of 368 * @propname: Name of the property 369 * @val: The values are stored here or %NULL to return the number of values 370 * @nval: Size of the @val array 371 * 372 * Function reads an array of string properties with @propname from the device 373 * firmware description and stores them to @val if found. 374 * 375 * Return: number of values read on success if @val is non-NULL, 376 * number of values available on success if @val is NULL, 377 * %-EINVAL if given arguments are not valid, 378 * %-ENODATA if the property does not have a value, 379 * %-EPROTO or %-EILSEQ if the property is not an array of strings, 380 * %-EOVERFLOW if the size of the property is not as expected. 381 * %-ENXIO if no suitable firmware interface is present. 382 */ 383 int device_property_read_string_array(struct device *dev, const char *propname, 384 const char **val, size_t nval) 385 { 386 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); 387 } 388 EXPORT_SYMBOL_GPL(device_property_read_string_array); 389 390 /** 391 * device_property_read_string - return a string property of a device 392 * @dev: Device to get the property of 393 * @propname: Name of the property 394 * @val: The value is stored here 395 * 396 * Function reads property @propname from the device firmware description and 397 * stores the value into @val if found. The value is checked to be a string. 398 * 399 * Return: %0 if the property was found (success), 400 * %-EINVAL if given arguments are not valid, 401 * %-ENODATA if the property does not have a value, 402 * %-EPROTO or %-EILSEQ if the property type is not a string. 403 * %-ENXIO if no suitable firmware interface is present. 404 */ 405 int device_property_read_string(struct device *dev, const char *propname, 406 const char **val) 407 { 408 return fwnode_property_read_string(dev_fwnode(dev), propname, val); 409 } 410 EXPORT_SYMBOL_GPL(device_property_read_string); 411 412 /** 413 * device_property_match_string - find a string in an array and return index 414 * @dev: Device to get the property of 415 * @propname: Name of the property holding the array 416 * @string: String to look for 417 * 418 * Find a given string in a string array and if it is found return the 419 * index back. 420 * 421 * Return: %0 if the property was found (success), 422 * %-EINVAL if given arguments are not valid, 423 * %-ENODATA if the property does not have a value, 424 * %-EPROTO if the property is not an array of strings, 425 * %-ENXIO if no suitable firmware interface is present. 426 */ 427 int device_property_match_string(struct device *dev, const char *propname, 428 const char *string) 429 { 430 return fwnode_property_match_string(dev_fwnode(dev), propname, string); 431 } 432 EXPORT_SYMBOL_GPL(device_property_match_string); 433 434 static int fwnode_property_read_int_array(struct fwnode_handle *fwnode, 435 const char *propname, 436 unsigned int elem_size, void *val, 437 size_t nval) 438 { 439 int ret; 440 441 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname, 442 elem_size, val, nval); 443 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) && 444 !IS_ERR_OR_NULL(fwnode->secondary)) 445 ret = fwnode_call_int_op( 446 fwnode->secondary, property_read_int_array, propname, 447 elem_size, val, nval); 448 449 return ret; 450 } 451 452 /** 453 * fwnode_property_read_u8_array - return a u8 array property of firmware node 454 * @fwnode: Firmware node to get the property of 455 * @propname: Name of the property 456 * @val: The values are stored here or %NULL to return the number of values 457 * @nval: Size of the @val array 458 * 459 * Read an array of u8 properties with @propname from @fwnode and stores them to 460 * @val if found. 461 * 462 * Return: number of values if @val was %NULL, 463 * %0 if the property was found (success), 464 * %-EINVAL if given arguments are not valid, 465 * %-ENODATA if the property does not have a value, 466 * %-EPROTO if the property is not an array of numbers, 467 * %-EOVERFLOW if the size of the property is not as expected, 468 * %-ENXIO if no suitable firmware interface is present. 469 */ 470 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode, 471 const char *propname, u8 *val, size_t nval) 472 { 473 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8), 474 val, nval); 475 } 476 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array); 477 478 /** 479 * fwnode_property_read_u16_array - return a u16 array property of firmware node 480 * @fwnode: Firmware node to get the property of 481 * @propname: Name of the property 482 * @val: The values are stored here or %NULL to return the number of values 483 * @nval: Size of the @val array 484 * 485 * Read an array of u16 properties with @propname from @fwnode and store them to 486 * @val if found. 487 * 488 * Return: number of values if @val was %NULL, 489 * %0 if the property was found (success), 490 * %-EINVAL if given arguments are not valid, 491 * %-ENODATA if the property does not have a value, 492 * %-EPROTO if the property is not an array of numbers, 493 * %-EOVERFLOW if the size of the property is not as expected, 494 * %-ENXIO if no suitable firmware interface is present. 495 */ 496 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode, 497 const char *propname, u16 *val, size_t nval) 498 { 499 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16), 500 val, nval); 501 } 502 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array); 503 504 /** 505 * fwnode_property_read_u32_array - return a u32 array property of firmware node 506 * @fwnode: Firmware node to get the property of 507 * @propname: Name of the property 508 * @val: The values are stored here or %NULL to return the number of values 509 * @nval: Size of the @val array 510 * 511 * Read an array of u32 properties with @propname from @fwnode store them to 512 * @val if found. 513 * 514 * Return: number of values if @val was %NULL, 515 * %0 if the property was found (success), 516 * %-EINVAL if given arguments are not valid, 517 * %-ENODATA if the property does not have a value, 518 * %-EPROTO if the property is not an array of numbers, 519 * %-EOVERFLOW if the size of the property is not as expected, 520 * %-ENXIO if no suitable firmware interface is present. 521 */ 522 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode, 523 const char *propname, u32 *val, size_t nval) 524 { 525 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32), 526 val, nval); 527 } 528 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array); 529 530 /** 531 * fwnode_property_read_u64_array - return a u64 array property firmware node 532 * @fwnode: Firmware node to get the property of 533 * @propname: Name of the property 534 * @val: The values are stored here or %NULL to return the number of values 535 * @nval: Size of the @val array 536 * 537 * Read an array of u64 properties with @propname from @fwnode and store them to 538 * @val if found. 539 * 540 * Return: number of values if @val was %NULL, 541 * %0 if the property was found (success), 542 * %-EINVAL if given arguments are not valid, 543 * %-ENODATA if the property does not have a value, 544 * %-EPROTO if the property is not an array of numbers, 545 * %-EOVERFLOW if the size of the property is not as expected, 546 * %-ENXIO if no suitable firmware interface is present. 547 */ 548 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode, 549 const char *propname, u64 *val, size_t nval) 550 { 551 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64), 552 val, nval); 553 } 554 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array); 555 556 /** 557 * fwnode_property_read_string_array - return string array property of a node 558 * @fwnode: Firmware node to get the property of 559 * @propname: Name of the property 560 * @val: The values are stored here or %NULL to return the number of values 561 * @nval: Size of the @val array 562 * 563 * Read an string list property @propname from the given firmware node and store 564 * them to @val if found. 565 * 566 * Return: number of values read on success if @val is non-NULL, 567 * number of values available on success if @val is NULL, 568 * %-EINVAL if given arguments are not valid, 569 * %-ENODATA if the property does not have a value, 570 * %-EPROTO or %-EILSEQ if the property is not an array of strings, 571 * %-EOVERFLOW if the size of the property is not as expected, 572 * %-ENXIO if no suitable firmware interface is present. 573 */ 574 int fwnode_property_read_string_array(struct fwnode_handle *fwnode, 575 const char *propname, const char **val, 576 size_t nval) 577 { 578 int ret; 579 580 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname, 581 val, nval); 582 if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) && 583 !IS_ERR_OR_NULL(fwnode->secondary)) 584 ret = fwnode_call_int_op(fwnode->secondary, 585 property_read_string_array, propname, 586 val, nval); 587 return ret; 588 } 589 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array); 590 591 /** 592 * fwnode_property_read_string - return a string property of a firmware node 593 * @fwnode: Firmware node to get the property of 594 * @propname: Name of the property 595 * @val: The value is stored here 596 * 597 * Read property @propname from the given firmware node and store the value into 598 * @val if found. The value is checked to be a string. 599 * 600 * Return: %0 if the property was found (success), 601 * %-EINVAL if given arguments are not valid, 602 * %-ENODATA if the property does not have a value, 603 * %-EPROTO or %-EILSEQ if the property is not a string, 604 * %-ENXIO if no suitable firmware interface is present. 605 */ 606 int fwnode_property_read_string(struct fwnode_handle *fwnode, 607 const char *propname, const char **val) 608 { 609 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1); 610 611 return ret < 0 ? ret : 0; 612 } 613 EXPORT_SYMBOL_GPL(fwnode_property_read_string); 614 615 /** 616 * fwnode_property_match_string - find a string in an array and return index 617 * @fwnode: Firmware node to get the property of 618 * @propname: Name of the property holding the array 619 * @string: String to look for 620 * 621 * Find a given string in a string array and if it is found return the 622 * index back. 623 * 624 * Return: %0 if the property was found (success), 625 * %-EINVAL if given arguments are not valid, 626 * %-ENODATA if the property does not have a value, 627 * %-EPROTO if the property is not an array of strings, 628 * %-ENXIO if no suitable firmware interface is present. 629 */ 630 int fwnode_property_match_string(struct fwnode_handle *fwnode, 631 const char *propname, const char *string) 632 { 633 const char **values; 634 int nval, ret; 635 636 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0); 637 if (nval < 0) 638 return nval; 639 640 if (nval == 0) 641 return -ENODATA; 642 643 values = kcalloc(nval, sizeof(*values), GFP_KERNEL); 644 if (!values) 645 return -ENOMEM; 646 647 ret = fwnode_property_read_string_array(fwnode, propname, values, nval); 648 if (ret < 0) 649 goto out; 650 651 ret = match_string(values, nval, string); 652 if (ret < 0) 653 ret = -ENODATA; 654 out: 655 kfree(values); 656 return ret; 657 } 658 EXPORT_SYMBOL_GPL(fwnode_property_match_string); 659 660 static int property_copy_string_array(struct property_entry *dst, 661 const struct property_entry *src) 662 { 663 char **d; 664 size_t nval = src->length / sizeof(*d); 665 int i; 666 667 d = kcalloc(nval, sizeof(*d), GFP_KERNEL); 668 if (!d) 669 return -ENOMEM; 670 671 for (i = 0; i < nval; i++) { 672 d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL); 673 if (!d[i] && src->pointer.str[i]) { 674 while (--i >= 0) 675 kfree(d[i]); 676 kfree(d); 677 return -ENOMEM; 678 } 679 } 680 681 dst->pointer.raw_data = d; 682 return 0; 683 } 684 685 static int property_entry_copy_data(struct property_entry *dst, 686 const struct property_entry *src) 687 { 688 int error; 689 690 dst->name = kstrdup(src->name, GFP_KERNEL); 691 if (!dst->name) 692 return -ENOMEM; 693 694 if (src->is_array) { 695 if (!src->length) { 696 error = -ENODATA; 697 goto out_free_name; 698 } 699 700 if (src->is_string) { 701 error = property_copy_string_array(dst, src); 702 if (error) 703 goto out_free_name; 704 } else { 705 dst->pointer.raw_data = kmemdup(src->pointer.raw_data, 706 src->length, GFP_KERNEL); 707 if (!dst->pointer.raw_data) { 708 error = -ENOMEM; 709 goto out_free_name; 710 } 711 } 712 } else if (src->is_string) { 713 dst->value.str = kstrdup(src->value.str, GFP_KERNEL); 714 if (!dst->value.str && src->value.str) { 715 error = -ENOMEM; 716 goto out_free_name; 717 } 718 } else { 719 dst->value.raw_data = src->value.raw_data; 720 } 721 722 dst->length = src->length; 723 dst->is_array = src->is_array; 724 dst->is_string = src->is_string; 725 726 return 0; 727 728 out_free_name: 729 kfree(dst->name); 730 return error; 731 } 732 733 static void property_entry_free_data(const struct property_entry *p) 734 { 735 size_t i, nval; 736 737 if (p->is_array) { 738 if (p->is_string && p->pointer.str) { 739 nval = p->length / sizeof(const char *); 740 for (i = 0; i < nval; i++) 741 kfree(p->pointer.str[i]); 742 } 743 kfree(p->pointer.raw_data); 744 } else if (p->is_string) { 745 kfree(p->value.str); 746 } 747 kfree(p->name); 748 } 749 750 /** 751 * property_entries_dup - duplicate array of properties 752 * @properties: array of properties to copy 753 * 754 * This function creates a deep copy of the given NULL-terminated array 755 * of property entries. 756 */ 757 struct property_entry * 758 property_entries_dup(const struct property_entry *properties) 759 { 760 struct property_entry *p; 761 int i, n = 0; 762 763 while (properties[n].name) 764 n++; 765 766 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL); 767 if (!p) 768 return ERR_PTR(-ENOMEM); 769 770 for (i = 0; i < n; i++) { 771 int ret = property_entry_copy_data(&p[i], &properties[i]); 772 if (ret) { 773 while (--i >= 0) 774 property_entry_free_data(&p[i]); 775 kfree(p); 776 return ERR_PTR(ret); 777 } 778 } 779 780 return p; 781 } 782 EXPORT_SYMBOL_GPL(property_entries_dup); 783 784 /** 785 * property_entries_free - free previously allocated array of properties 786 * @properties: array of properties to destroy 787 * 788 * This function frees given NULL-terminated array of property entries, 789 * along with their data. 790 */ 791 void property_entries_free(const struct property_entry *properties) 792 { 793 const struct property_entry *p; 794 795 for (p = properties; p->name; p++) 796 property_entry_free_data(p); 797 798 kfree(properties); 799 } 800 EXPORT_SYMBOL_GPL(property_entries_free); 801 802 /** 803 * pset_free_set - releases memory allocated for copied property set 804 * @pset: Property set to release 805 * 806 * Function takes previously copied property set and releases all the 807 * memory allocated to it. 808 */ 809 static void pset_free_set(struct property_set *pset) 810 { 811 if (!pset) 812 return; 813 814 property_entries_free(pset->properties); 815 kfree(pset); 816 } 817 818 /** 819 * pset_copy_set - copies property set 820 * @pset: Property set to copy 821 * 822 * This function takes a deep copy of the given property set and returns 823 * pointer to the copy. Call device_free_property_set() to free resources 824 * allocated in this function. 825 * 826 * Return: Pointer to the new property set or error pointer. 827 */ 828 static struct property_set *pset_copy_set(const struct property_set *pset) 829 { 830 struct property_entry *properties; 831 struct property_set *p; 832 833 p = kzalloc(sizeof(*p), GFP_KERNEL); 834 if (!p) 835 return ERR_PTR(-ENOMEM); 836 837 properties = property_entries_dup(pset->properties); 838 if (IS_ERR(properties)) { 839 kfree(p); 840 return ERR_CAST(properties); 841 } 842 843 p->properties = properties; 844 return p; 845 } 846 847 /** 848 * device_remove_properties - Remove properties from a device object. 849 * @dev: Device whose properties to remove. 850 * 851 * The function removes properties previously associated to the device 852 * secondary firmware node with device_add_properties(). Memory allocated 853 * to the properties will also be released. 854 */ 855 void device_remove_properties(struct device *dev) 856 { 857 struct fwnode_handle *fwnode; 858 859 fwnode = dev_fwnode(dev); 860 if (!fwnode) 861 return; 862 /* 863 * Pick either primary or secondary node depending which one holds 864 * the pset. If there is no real firmware node (ACPI/DT) primary 865 * will hold the pset. 866 */ 867 if (is_pset_node(fwnode)) { 868 set_primary_fwnode(dev, NULL); 869 pset_free_set(to_pset_node(fwnode)); 870 } else { 871 fwnode = fwnode->secondary; 872 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) { 873 set_secondary_fwnode(dev, NULL); 874 pset_free_set(to_pset_node(fwnode)); 875 } 876 } 877 } 878 EXPORT_SYMBOL_GPL(device_remove_properties); 879 880 /** 881 * device_add_properties - Add a collection of properties to a device object. 882 * @dev: Device to add properties to. 883 * @properties: Collection of properties to add. 884 * 885 * Associate a collection of device properties represented by @properties with 886 * @dev as its secondary firmware node. The function takes a copy of 887 * @properties. 888 */ 889 int device_add_properties(struct device *dev, 890 const struct property_entry *properties) 891 { 892 struct property_set *p, pset; 893 894 if (!properties) 895 return -EINVAL; 896 897 pset.properties = properties; 898 899 p = pset_copy_set(&pset); 900 if (IS_ERR(p)) 901 return PTR_ERR(p); 902 903 p->fwnode.type = FWNODE_PDATA; 904 p->fwnode.ops = &pset_fwnode_ops; 905 set_secondary_fwnode(dev, &p->fwnode); 906 return 0; 907 } 908 EXPORT_SYMBOL_GPL(device_add_properties); 909 910 /** 911 * fwnode_get_next_parent - Iterate to the node's parent 912 * @fwnode: Firmware whose parent is retrieved 913 * 914 * This is like fwnode_get_parent() except that it drops the refcount 915 * on the passed node, making it suitable for iterating through a 916 * node's parents. 917 * 918 * Returns a node pointer with refcount incremented, use 919 * fwnode_handle_node() on it when done. 920 */ 921 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode) 922 { 923 struct fwnode_handle *parent = fwnode_get_parent(fwnode); 924 925 fwnode_handle_put(fwnode); 926 927 return parent; 928 } 929 EXPORT_SYMBOL_GPL(fwnode_get_next_parent); 930 931 /** 932 * fwnode_get_parent - Return parent firwmare node 933 * @fwnode: Firmware whose parent is retrieved 934 * 935 * Return parent firmware node of the given node if possible or %NULL if no 936 * parent was available. 937 */ 938 struct fwnode_handle *fwnode_get_parent(struct fwnode_handle *fwnode) 939 { 940 return fwnode_call_ptr_op(fwnode, get_parent); 941 } 942 EXPORT_SYMBOL_GPL(fwnode_get_parent); 943 944 /** 945 * fwnode_get_next_child_node - Return the next child node handle for a node 946 * @fwnode: Firmware node to find the next child node for. 947 * @child: Handle to one of the node's child nodes or a %NULL handle. 948 */ 949 struct fwnode_handle *fwnode_get_next_child_node(struct fwnode_handle *fwnode, 950 struct fwnode_handle *child) 951 { 952 return fwnode_call_ptr_op(fwnode, get_next_child_node, child); 953 } 954 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node); 955 956 /** 957 * device_get_next_child_node - Return the next child node handle for a device 958 * @dev: Device to find the next child node for. 959 * @child: Handle to one of the device's child nodes or a null handle. 960 */ 961 struct fwnode_handle *device_get_next_child_node(struct device *dev, 962 struct fwnode_handle *child) 963 { 964 struct acpi_device *adev = ACPI_COMPANION(dev); 965 struct fwnode_handle *fwnode = NULL; 966 967 if (dev->of_node) 968 fwnode = &dev->of_node->fwnode; 969 else if (adev) 970 fwnode = acpi_fwnode_handle(adev); 971 972 return fwnode_get_next_child_node(fwnode, child); 973 } 974 EXPORT_SYMBOL_GPL(device_get_next_child_node); 975 976 /** 977 * fwnode_get_named_child_node - Return first matching named child node handle 978 * @fwnode: Firmware node to find the named child node for. 979 * @childname: String to match child node name against. 980 */ 981 struct fwnode_handle *fwnode_get_named_child_node(struct fwnode_handle *fwnode, 982 const char *childname) 983 { 984 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname); 985 } 986 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node); 987 988 /** 989 * device_get_named_child_node - Return first matching named child node handle 990 * @dev: Device to find the named child node for. 991 * @childname: String to match child node name against. 992 */ 993 struct fwnode_handle *device_get_named_child_node(struct device *dev, 994 const char *childname) 995 { 996 return fwnode_get_named_child_node(dev_fwnode(dev), childname); 997 } 998 EXPORT_SYMBOL_GPL(device_get_named_child_node); 999 1000 /** 1001 * fwnode_handle_get - Obtain a reference to a device node 1002 * @fwnode: Pointer to the device node to obtain the reference to. 1003 */ 1004 void fwnode_handle_get(struct fwnode_handle *fwnode) 1005 { 1006 fwnode_call_void_op(fwnode, get); 1007 } 1008 EXPORT_SYMBOL_GPL(fwnode_handle_get); 1009 1010 /** 1011 * fwnode_handle_put - Drop reference to a device node 1012 * @fwnode: Pointer to the device node to drop the reference to. 1013 * 1014 * This has to be used when terminating device_for_each_child_node() iteration 1015 * with break or return to prevent stale device node references from being left 1016 * behind. 1017 */ 1018 void fwnode_handle_put(struct fwnode_handle *fwnode) 1019 { 1020 fwnode_call_void_op(fwnode, put); 1021 } 1022 EXPORT_SYMBOL_GPL(fwnode_handle_put); 1023 1024 /** 1025 * fwnode_device_is_available - check if a device is available for use 1026 * @fwnode: Pointer to the fwnode of the device. 1027 */ 1028 bool fwnode_device_is_available(struct fwnode_handle *fwnode) 1029 { 1030 return fwnode_call_bool_op(fwnode, device_is_available); 1031 } 1032 EXPORT_SYMBOL_GPL(fwnode_device_is_available); 1033 1034 /** 1035 * device_get_child_node_count - return the number of child nodes for device 1036 * @dev: Device to cound the child nodes for 1037 */ 1038 unsigned int device_get_child_node_count(struct device *dev) 1039 { 1040 struct fwnode_handle *child; 1041 unsigned int count = 0; 1042 1043 device_for_each_child_node(dev, child) 1044 count++; 1045 1046 return count; 1047 } 1048 EXPORT_SYMBOL_GPL(device_get_child_node_count); 1049 1050 bool device_dma_supported(struct device *dev) 1051 { 1052 /* For DT, this is always supported. 1053 * For ACPI, this depends on CCA, which 1054 * is determined by the acpi_dma_supported(). 1055 */ 1056 if (IS_ENABLED(CONFIG_OF) && dev->of_node) 1057 return true; 1058 1059 return acpi_dma_supported(ACPI_COMPANION(dev)); 1060 } 1061 EXPORT_SYMBOL_GPL(device_dma_supported); 1062 1063 enum dev_dma_attr device_get_dma_attr(struct device *dev) 1064 { 1065 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED; 1066 1067 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { 1068 if (of_dma_is_coherent(dev->of_node)) 1069 attr = DEV_DMA_COHERENT; 1070 else 1071 attr = DEV_DMA_NON_COHERENT; 1072 } else 1073 attr = acpi_get_dma_attr(ACPI_COMPANION(dev)); 1074 1075 return attr; 1076 } 1077 EXPORT_SYMBOL_GPL(device_get_dma_attr); 1078 1079 /** 1080 * device_get_phy_mode - Get phy mode for given device 1081 * @dev: Pointer to the given device 1082 * 1083 * The function gets phy interface string from property 'phy-mode' or 1084 * 'phy-connection-type', and return its index in phy_modes table, or errno in 1085 * error case. 1086 */ 1087 int device_get_phy_mode(struct device *dev) 1088 { 1089 const char *pm; 1090 int err, i; 1091 1092 err = device_property_read_string(dev, "phy-mode", &pm); 1093 if (err < 0) 1094 err = device_property_read_string(dev, 1095 "phy-connection-type", &pm); 1096 if (err < 0) 1097 return err; 1098 1099 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) 1100 if (!strcasecmp(pm, phy_modes(i))) 1101 return i; 1102 1103 return -ENODEV; 1104 } 1105 EXPORT_SYMBOL_GPL(device_get_phy_mode); 1106 1107 static void *device_get_mac_addr(struct device *dev, 1108 const char *name, char *addr, 1109 int alen) 1110 { 1111 int ret = device_property_read_u8_array(dev, name, addr, alen); 1112 1113 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr)) 1114 return addr; 1115 return NULL; 1116 } 1117 1118 /** 1119 * device_get_mac_address - Get the MAC for a given device 1120 * @dev: Pointer to the device 1121 * @addr: Address of buffer to store the MAC in 1122 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN 1123 * 1124 * Search the firmware node for the best MAC address to use. 'mac-address' is 1125 * checked first, because that is supposed to contain to "most recent" MAC 1126 * address. If that isn't set, then 'local-mac-address' is checked next, 1127 * because that is the default address. If that isn't set, then the obsolete 1128 * 'address' is checked, just in case we're using an old device tree. 1129 * 1130 * Note that the 'address' property is supposed to contain a virtual address of 1131 * the register set, but some DTS files have redefined that property to be the 1132 * MAC address. 1133 * 1134 * All-zero MAC addresses are rejected, because those could be properties that 1135 * exist in the firmware tables, but were not updated by the firmware. For 1136 * example, the DTS could define 'mac-address' and 'local-mac-address', with 1137 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'. 1138 * In this case, the real MAC is in 'local-mac-address', and 'mac-address' 1139 * exists but is all zeros. 1140 */ 1141 void *device_get_mac_address(struct device *dev, char *addr, int alen) 1142 { 1143 char *res; 1144 1145 res = device_get_mac_addr(dev, "mac-address", addr, alen); 1146 if (res) 1147 return res; 1148 1149 res = device_get_mac_addr(dev, "local-mac-address", addr, alen); 1150 if (res) 1151 return res; 1152 1153 return device_get_mac_addr(dev, "address", addr, alen); 1154 } 1155 EXPORT_SYMBOL(device_get_mac_address); 1156 1157 /** 1158 * device_graph_get_next_endpoint - Get next endpoint firmware node 1159 * @fwnode: Pointer to the parent firmware node 1160 * @prev: Previous endpoint node or %NULL to get the first 1161 * 1162 * Returns an endpoint firmware node pointer or %NULL if no more endpoints 1163 * are available. 1164 */ 1165 struct fwnode_handle * 1166 fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode, 1167 struct fwnode_handle *prev) 1168 { 1169 return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev); 1170 } 1171 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint); 1172 1173 /** 1174 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint 1175 * @endpoint: Endpoint firmware node of the port 1176 * 1177 * Return: the firmware node of the device the @endpoint belongs to. 1178 */ 1179 struct fwnode_handle * 1180 fwnode_graph_get_port_parent(struct fwnode_handle *endpoint) 1181 { 1182 struct fwnode_handle *port, *parent; 1183 1184 port = fwnode_get_parent(endpoint); 1185 parent = fwnode_call_ptr_op(port, graph_get_port_parent); 1186 1187 fwnode_handle_put(port); 1188 1189 return parent; 1190 } 1191 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent); 1192 1193 /** 1194 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device 1195 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1196 * 1197 * Extracts firmware node of a remote device the @fwnode points to. 1198 */ 1199 struct fwnode_handle * 1200 fwnode_graph_get_remote_port_parent(struct fwnode_handle *fwnode) 1201 { 1202 struct fwnode_handle *endpoint, *parent; 1203 1204 endpoint = fwnode_graph_get_remote_endpoint(fwnode); 1205 parent = fwnode_graph_get_port_parent(endpoint); 1206 1207 fwnode_handle_put(endpoint); 1208 1209 return parent; 1210 } 1211 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent); 1212 1213 /** 1214 * fwnode_graph_get_remote_port - Return fwnode of a remote port 1215 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1216 * 1217 * Extracts firmware node of a remote port the @fwnode points to. 1218 */ 1219 struct fwnode_handle *fwnode_graph_get_remote_port(struct fwnode_handle *fwnode) 1220 { 1221 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode)); 1222 } 1223 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port); 1224 1225 /** 1226 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint 1227 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1228 * 1229 * Extracts firmware node of a remote endpoint the @fwnode points to. 1230 */ 1231 struct fwnode_handle * 1232 fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode) 1233 { 1234 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint); 1235 } 1236 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint); 1237 1238 /** 1239 * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint 1240 * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint 1241 * @port_id: identifier of the parent port node 1242 * @endpoint_id: identifier of the endpoint node 1243 * 1244 * Return: Remote fwnode handle associated with remote endpoint node linked 1245 * to @node. Use fwnode_node_put() on it when done. 1246 */ 1247 struct fwnode_handle *fwnode_graph_get_remote_node(struct fwnode_handle *fwnode, 1248 u32 port_id, u32 endpoint_id) 1249 { 1250 struct fwnode_handle *endpoint = NULL; 1251 1252 while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) { 1253 struct fwnode_endpoint fwnode_ep; 1254 struct fwnode_handle *remote; 1255 int ret; 1256 1257 ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep); 1258 if (ret < 0) 1259 continue; 1260 1261 if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id) 1262 continue; 1263 1264 remote = fwnode_graph_get_remote_port_parent(endpoint); 1265 if (!remote) 1266 return NULL; 1267 1268 return fwnode_device_is_available(remote) ? remote : NULL; 1269 } 1270 1271 return NULL; 1272 } 1273 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node); 1274 1275 /** 1276 * fwnode_graph_parse_endpoint - parse common endpoint node properties 1277 * @fwnode: pointer to endpoint fwnode_handle 1278 * @endpoint: pointer to the fwnode endpoint data structure 1279 * 1280 * Parse @fwnode representing a graph endpoint node and store the 1281 * information in @endpoint. The caller must hold a reference to 1282 * @fwnode. 1283 */ 1284 int fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode, 1285 struct fwnode_endpoint *endpoint) 1286 { 1287 memset(endpoint, 0, sizeof(*endpoint)); 1288 1289 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint); 1290 } 1291 EXPORT_SYMBOL(fwnode_graph_parse_endpoint); 1292