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/property.h> 19 #include <linux/etherdevice.h> 20 #include <linux/phy.h> 21 22 static inline bool is_pset_node(struct fwnode_handle *fwnode) 23 { 24 return fwnode && fwnode->type == FWNODE_PDATA; 25 } 26 27 static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode) 28 { 29 return is_pset_node(fwnode) ? 30 container_of(fwnode, struct property_set, fwnode) : NULL; 31 } 32 33 static struct property_entry *pset_prop_get(struct property_set *pset, 34 const char *name) 35 { 36 struct property_entry *prop; 37 38 if (!pset || !pset->properties) 39 return NULL; 40 41 for (prop = pset->properties; prop->name; prop++) 42 if (!strcmp(name, prop->name)) 43 return prop; 44 45 return NULL; 46 } 47 48 static void *pset_prop_find(struct property_set *pset, const char *propname, 49 size_t length) 50 { 51 struct property_entry *prop; 52 void *pointer; 53 54 prop = pset_prop_get(pset, propname); 55 if (!prop) 56 return ERR_PTR(-EINVAL); 57 if (prop->is_array) 58 pointer = prop->pointer.raw_data; 59 else 60 pointer = &prop->value.raw_data; 61 if (!pointer) 62 return ERR_PTR(-ENODATA); 63 if (length > prop->length) 64 return ERR_PTR(-EOVERFLOW); 65 return pointer; 66 } 67 68 static int pset_prop_read_u8_array(struct property_set *pset, 69 const char *propname, 70 u8 *values, size_t nval) 71 { 72 void *pointer; 73 size_t length = nval * sizeof(*values); 74 75 pointer = pset_prop_find(pset, propname, length); 76 if (IS_ERR(pointer)) 77 return PTR_ERR(pointer); 78 79 memcpy(values, pointer, length); 80 return 0; 81 } 82 83 static int pset_prop_read_u16_array(struct property_set *pset, 84 const char *propname, 85 u16 *values, size_t nval) 86 { 87 void *pointer; 88 size_t length = nval * sizeof(*values); 89 90 pointer = pset_prop_find(pset, propname, length); 91 if (IS_ERR(pointer)) 92 return PTR_ERR(pointer); 93 94 memcpy(values, pointer, length); 95 return 0; 96 } 97 98 static int pset_prop_read_u32_array(struct property_set *pset, 99 const char *propname, 100 u32 *values, size_t nval) 101 { 102 void *pointer; 103 size_t length = nval * sizeof(*values); 104 105 pointer = pset_prop_find(pset, propname, length); 106 if (IS_ERR(pointer)) 107 return PTR_ERR(pointer); 108 109 memcpy(values, pointer, length); 110 return 0; 111 } 112 113 static int pset_prop_read_u64_array(struct property_set *pset, 114 const char *propname, 115 u64 *values, size_t nval) 116 { 117 void *pointer; 118 size_t length = nval * sizeof(*values); 119 120 pointer = pset_prop_find(pset, propname, length); 121 if (IS_ERR(pointer)) 122 return PTR_ERR(pointer); 123 124 memcpy(values, pointer, length); 125 return 0; 126 } 127 128 static int pset_prop_count_elems_of_size(struct property_set *pset, 129 const char *propname, size_t length) 130 { 131 struct property_entry *prop; 132 133 prop = pset_prop_get(pset, propname); 134 if (!prop) 135 return -EINVAL; 136 137 return prop->length / length; 138 } 139 140 static int pset_prop_read_string_array(struct property_set *pset, 141 const char *propname, 142 const char **strings, size_t nval) 143 { 144 void *pointer; 145 size_t length = nval * sizeof(*strings); 146 147 pointer = pset_prop_find(pset, propname, length); 148 if (IS_ERR(pointer)) 149 return PTR_ERR(pointer); 150 151 memcpy(strings, pointer, length); 152 return 0; 153 } 154 155 static int pset_prop_read_string(struct property_set *pset, 156 const char *propname, const char **strings) 157 { 158 struct property_entry *prop; 159 const char **pointer; 160 161 prop = pset_prop_get(pset, propname); 162 if (!prop) 163 return -EINVAL; 164 if (!prop->is_string) 165 return -EILSEQ; 166 if (prop->is_array) { 167 pointer = prop->pointer.str; 168 if (!pointer) 169 return -ENODATA; 170 } else { 171 pointer = &prop->value.str; 172 if (*pointer && strnlen(*pointer, prop->length) >= prop->length) 173 return -EILSEQ; 174 } 175 176 *strings = *pointer; 177 return 0; 178 } 179 180 static inline struct fwnode_handle *dev_fwnode(struct device *dev) 181 { 182 return IS_ENABLED(CONFIG_OF) && dev->of_node ? 183 &dev->of_node->fwnode : dev->fwnode; 184 } 185 186 /** 187 * device_property_present - check if a property of a device is present 188 * @dev: Device whose property is being checked 189 * @propname: Name of the property 190 * 191 * Check if property @propname is present in the device firmware description. 192 */ 193 bool device_property_present(struct device *dev, const char *propname) 194 { 195 return fwnode_property_present(dev_fwnode(dev), propname); 196 } 197 EXPORT_SYMBOL_GPL(device_property_present); 198 199 static bool __fwnode_property_present(struct fwnode_handle *fwnode, 200 const char *propname) 201 { 202 if (is_of_node(fwnode)) 203 return of_property_read_bool(to_of_node(fwnode), propname); 204 else if (is_acpi_node(fwnode)) 205 return !acpi_node_prop_get(fwnode, propname, NULL); 206 else if (is_pset_node(fwnode)) 207 return !!pset_prop_get(to_pset_node(fwnode), propname); 208 return false; 209 } 210 211 /** 212 * fwnode_property_present - check if a property of a firmware node is present 213 * @fwnode: Firmware node whose property to check 214 * @propname: Name of the property 215 */ 216 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname) 217 { 218 bool ret; 219 220 ret = __fwnode_property_present(fwnode, propname); 221 if (ret == false && fwnode && fwnode->secondary) 222 ret = __fwnode_property_present(fwnode->secondary, propname); 223 return ret; 224 } 225 EXPORT_SYMBOL_GPL(fwnode_property_present); 226 227 /** 228 * device_property_read_u8_array - return a u8 array property of a device 229 * @dev: Device to get the property of 230 * @propname: Name of the property 231 * @val: The values are stored here or %NULL to return the number of values 232 * @nval: Size of the @val array 233 * 234 * Function reads an array of u8 properties with @propname from the device 235 * firmware description and stores them to @val if found. 236 * 237 * Return: number of values if @val was %NULL, 238 * %0 if the property was found (success), 239 * %-EINVAL if given arguments are not valid, 240 * %-ENODATA if the property does not have a value, 241 * %-EPROTO if the property is not an array of numbers, 242 * %-EOVERFLOW if the size of the property is not as expected. 243 * %-ENXIO if no suitable firmware interface is present. 244 */ 245 int device_property_read_u8_array(struct device *dev, const char *propname, 246 u8 *val, size_t nval) 247 { 248 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); 249 } 250 EXPORT_SYMBOL_GPL(device_property_read_u8_array); 251 252 /** 253 * device_property_read_u16_array - return a u16 array property of a device 254 * @dev: Device to get the property of 255 * @propname: Name of the property 256 * @val: The values are stored here or %NULL to return the number of values 257 * @nval: Size of the @val array 258 * 259 * Function reads an array of u16 properties with @propname from the device 260 * firmware description and stores them to @val if found. 261 * 262 * Return: number of values if @val was %NULL, 263 * %0 if the property was found (success), 264 * %-EINVAL if given arguments are not valid, 265 * %-ENODATA if the property does not have a value, 266 * %-EPROTO if the property is not an array of numbers, 267 * %-EOVERFLOW if the size of the property is not as expected. 268 * %-ENXIO if no suitable firmware interface is present. 269 */ 270 int device_property_read_u16_array(struct device *dev, const char *propname, 271 u16 *val, size_t nval) 272 { 273 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); 274 } 275 EXPORT_SYMBOL_GPL(device_property_read_u16_array); 276 277 /** 278 * device_property_read_u32_array - return a u32 array property of a device 279 * @dev: Device to get the property of 280 * @propname: Name of the property 281 * @val: The values are stored here or %NULL to return the number of values 282 * @nval: Size of the @val array 283 * 284 * Function reads an array of u32 properties with @propname from the device 285 * firmware description and stores them to @val if found. 286 * 287 * Return: number of values if @val was %NULL, 288 * %0 if the property was found (success), 289 * %-EINVAL if given arguments are not valid, 290 * %-ENODATA if the property does not have a value, 291 * %-EPROTO if the property is not an array of numbers, 292 * %-EOVERFLOW if the size of the property is not as expected. 293 * %-ENXIO if no suitable firmware interface is present. 294 */ 295 int device_property_read_u32_array(struct device *dev, const char *propname, 296 u32 *val, size_t nval) 297 { 298 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); 299 } 300 EXPORT_SYMBOL_GPL(device_property_read_u32_array); 301 302 /** 303 * device_property_read_u64_array - return a u64 array property of a device 304 * @dev: Device to get the property of 305 * @propname: Name of the property 306 * @val: The values are stored here or %NULL to return the number of values 307 * @nval: Size of the @val array 308 * 309 * Function reads an array of u64 properties with @propname from the device 310 * firmware description and stores them to @val if found. 311 * 312 * Return: number of values if @val was %NULL, 313 * %0 if the property was found (success), 314 * %-EINVAL if given arguments are not valid, 315 * %-ENODATA if the property does not have a value, 316 * %-EPROTO if the property is not an array of numbers, 317 * %-EOVERFLOW if the size of the property is not as expected. 318 * %-ENXIO if no suitable firmware interface is present. 319 */ 320 int device_property_read_u64_array(struct device *dev, const char *propname, 321 u64 *val, size_t nval) 322 { 323 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); 324 } 325 EXPORT_SYMBOL_GPL(device_property_read_u64_array); 326 327 /** 328 * device_property_read_string_array - return a string array property of device 329 * @dev: Device to get the property of 330 * @propname: Name of the property 331 * @val: The values are stored here or %NULL to return the number of values 332 * @nval: Size of the @val array 333 * 334 * Function reads an array of string properties with @propname from the device 335 * firmware description and stores them to @val if found. 336 * 337 * Return: number of values if @val was %NULL, 338 * %0 if the property was found (success), 339 * %-EINVAL if given arguments are not valid, 340 * %-ENODATA if the property does not have a value, 341 * %-EPROTO or %-EILSEQ if the property is not an array of strings, 342 * %-EOVERFLOW if the size of the property is not as expected. 343 * %-ENXIO if no suitable firmware interface is present. 344 */ 345 int device_property_read_string_array(struct device *dev, const char *propname, 346 const char **val, size_t nval) 347 { 348 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); 349 } 350 EXPORT_SYMBOL_GPL(device_property_read_string_array); 351 352 /** 353 * device_property_read_string - return a string property of a device 354 * @dev: Device to get the property of 355 * @propname: Name of the property 356 * @val: The value is stored here 357 * 358 * Function reads property @propname from the device firmware description and 359 * stores the value into @val if found. The value is checked to be a string. 360 * 361 * Return: %0 if the property was found (success), 362 * %-EINVAL if given arguments are not valid, 363 * %-ENODATA if the property does not have a value, 364 * %-EPROTO or %-EILSEQ if the property type is not a string. 365 * %-ENXIO if no suitable firmware interface is present. 366 */ 367 int device_property_read_string(struct device *dev, const char *propname, 368 const char **val) 369 { 370 return fwnode_property_read_string(dev_fwnode(dev), propname, val); 371 } 372 EXPORT_SYMBOL_GPL(device_property_read_string); 373 374 /** 375 * device_property_match_string - find a string in an array and return index 376 * @dev: Device to get the property of 377 * @propname: Name of the property holding the array 378 * @string: String to look for 379 * 380 * Find a given string in a string array and if it is found return the 381 * index back. 382 * 383 * Return: %0 if the property was found (success), 384 * %-EINVAL if given arguments are not valid, 385 * %-ENODATA if the property does not have a value, 386 * %-EPROTO if the property is not an array of strings, 387 * %-ENXIO if no suitable firmware interface is present. 388 */ 389 int device_property_match_string(struct device *dev, const char *propname, 390 const char *string) 391 { 392 return fwnode_property_match_string(dev_fwnode(dev), propname, string); 393 } 394 EXPORT_SYMBOL_GPL(device_property_match_string); 395 396 #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \ 397 (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \ 398 : of_property_count_elems_of_size((node), (propname), sizeof(type)) 399 400 #define PSET_PROP_READ_ARRAY(node, propname, type, val, nval) \ 401 (val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval)) \ 402 : pset_prop_count_elems_of_size((node), (propname), sizeof(type)) 403 404 #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \ 405 ({ \ 406 int _ret_; \ 407 if (is_of_node(_fwnode_)) \ 408 _ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_, \ 409 _type_, _val_, _nval_); \ 410 else if (is_acpi_node(_fwnode_)) \ 411 _ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_, \ 412 _val_, _nval_); \ 413 else if (is_pset_node(_fwnode_)) \ 414 _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \ 415 _type_, _val_, _nval_); \ 416 else \ 417 _ret_ = -ENXIO; \ 418 _ret_; \ 419 }) 420 421 #define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \ 422 ({ \ 423 int _ret_; \ 424 _ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, \ 425 _val_, _nval_); \ 426 if (_ret_ == -EINVAL && _fwnode_ && _fwnode_->secondary) \ 427 _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \ 428 _proptype_, _val_, _nval_); \ 429 _ret_; \ 430 }) 431 432 /** 433 * fwnode_property_read_u8_array - return a u8 array property of firmware node 434 * @fwnode: Firmware node to get the property of 435 * @propname: Name of the property 436 * @val: The values are stored here or %NULL to return the number of values 437 * @nval: Size of the @val array 438 * 439 * Read an array of u8 properties with @propname from @fwnode and stores them to 440 * @val if found. 441 * 442 * Return: number of values if @val was %NULL, 443 * %0 if the property was found (success), 444 * %-EINVAL if given arguments are not valid, 445 * %-ENODATA if the property does not have a value, 446 * %-EPROTO if the property is not an array of numbers, 447 * %-EOVERFLOW if the size of the property is not as expected, 448 * %-ENXIO if no suitable firmware interface is present. 449 */ 450 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode, 451 const char *propname, u8 *val, size_t nval) 452 { 453 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8, 454 val, nval); 455 } 456 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array); 457 458 /** 459 * fwnode_property_read_u16_array - return a u16 array property of firmware node 460 * @fwnode: Firmware node to get the property of 461 * @propname: Name of the property 462 * @val: The values are stored here or %NULL to return the number of values 463 * @nval: Size of the @val array 464 * 465 * Read an array of u16 properties with @propname from @fwnode and store them to 466 * @val if found. 467 * 468 * Return: number of values if @val was %NULL, 469 * %0 if the property was found (success), 470 * %-EINVAL if given arguments are not valid, 471 * %-ENODATA if the property does not have a value, 472 * %-EPROTO if the property is not an array of numbers, 473 * %-EOVERFLOW if the size of the property is not as expected, 474 * %-ENXIO if no suitable firmware interface is present. 475 */ 476 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode, 477 const char *propname, u16 *val, size_t nval) 478 { 479 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16, 480 val, nval); 481 } 482 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array); 483 484 /** 485 * fwnode_property_read_u32_array - return a u32 array property of firmware node 486 * @fwnode: Firmware node to get the property of 487 * @propname: Name of the property 488 * @val: The values are stored here or %NULL to return the number of values 489 * @nval: Size of the @val array 490 * 491 * Read an array of u32 properties with @propname from @fwnode store them to 492 * @val if found. 493 * 494 * Return: number of values if @val was %NULL, 495 * %0 if the property was found (success), 496 * %-EINVAL if given arguments are not valid, 497 * %-ENODATA if the property does not have a value, 498 * %-EPROTO if the property is not an array of numbers, 499 * %-EOVERFLOW if the size of the property is not as expected, 500 * %-ENXIO if no suitable firmware interface is present. 501 */ 502 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode, 503 const char *propname, u32 *val, size_t nval) 504 { 505 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32, 506 val, nval); 507 } 508 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array); 509 510 /** 511 * fwnode_property_read_u64_array - return a u64 array property firmware node 512 * @fwnode: Firmware node to get the property of 513 * @propname: Name of the property 514 * @val: The values are stored here or %NULL to return the number of values 515 * @nval: Size of the @val array 516 * 517 * Read an array of u64 properties with @propname from @fwnode and store them to 518 * @val if found. 519 * 520 * Return: number of values if @val was %NULL, 521 * %0 if the property was found (success), 522 * %-EINVAL if given arguments are not valid, 523 * %-ENODATA if the property does not have a value, 524 * %-EPROTO if the property is not an array of numbers, 525 * %-EOVERFLOW if the size of the property is not as expected, 526 * %-ENXIO if no suitable firmware interface is present. 527 */ 528 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode, 529 const char *propname, u64 *val, size_t nval) 530 { 531 return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64, 532 val, nval); 533 } 534 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array); 535 536 static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode, 537 const char *propname, 538 const char **val, size_t nval) 539 { 540 if (is_of_node(fwnode)) 541 return val ? 542 of_property_read_string_array(to_of_node(fwnode), 543 propname, val, nval) : 544 of_property_count_strings(to_of_node(fwnode), propname); 545 else if (is_acpi_node(fwnode)) 546 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING, 547 val, nval); 548 else if (is_pset_node(fwnode)) 549 return val ? 550 pset_prop_read_string_array(to_pset_node(fwnode), 551 propname, val, nval) : 552 pset_prop_count_elems_of_size(to_pset_node(fwnode), 553 propname, 554 sizeof(const char *)); 555 return -ENXIO; 556 } 557 558 static int __fwnode_property_read_string(struct fwnode_handle *fwnode, 559 const char *propname, const char **val) 560 { 561 if (is_of_node(fwnode)) 562 return of_property_read_string(to_of_node(fwnode), propname, val); 563 else if (is_acpi_node(fwnode)) 564 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING, 565 val, 1); 566 else if (is_pset_node(fwnode)) 567 return pset_prop_read_string(to_pset_node(fwnode), propname, val); 568 return -ENXIO; 569 } 570 571 /** 572 * fwnode_property_read_string_array - return string array property of a node 573 * @fwnode: Firmware node to get the property of 574 * @propname: Name of the property 575 * @val: The values are stored here or %NULL to return the number of values 576 * @nval: Size of the @val array 577 * 578 * Read an string list property @propname from the given firmware node and store 579 * them to @val if found. 580 * 581 * Return: number of values if @val was %NULL, 582 * %0 if the property was found (success), 583 * %-EINVAL if given arguments are not valid, 584 * %-ENODATA if the property does not have a value, 585 * %-EPROTO if the property is not an array of strings, 586 * %-EOVERFLOW if the size of the property is not as expected, 587 * %-ENXIO if no suitable firmware interface is present. 588 */ 589 int fwnode_property_read_string_array(struct fwnode_handle *fwnode, 590 const char *propname, const char **val, 591 size_t nval) 592 { 593 int ret; 594 595 ret = __fwnode_property_read_string_array(fwnode, propname, val, nval); 596 if (ret == -EINVAL && fwnode && fwnode->secondary) 597 ret = __fwnode_property_read_string_array(fwnode->secondary, 598 propname, val, nval); 599 return ret; 600 } 601 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array); 602 603 /** 604 * fwnode_property_read_string - return a string property of a firmware node 605 * @fwnode: Firmware node to get the property of 606 * @propname: Name of the property 607 * @val: The value is stored here 608 * 609 * Read property @propname from the given firmware node and store the value into 610 * @val if found. The value is checked to be a string. 611 * 612 * Return: %0 if the property was found (success), 613 * %-EINVAL if given arguments are not valid, 614 * %-ENODATA if the property does not have a value, 615 * %-EPROTO or %-EILSEQ if the property is not a string, 616 * %-ENXIO if no suitable firmware interface is present. 617 */ 618 int fwnode_property_read_string(struct fwnode_handle *fwnode, 619 const char *propname, const char **val) 620 { 621 int ret; 622 623 ret = __fwnode_property_read_string(fwnode, propname, val); 624 if (ret == -EINVAL && fwnode && fwnode->secondary) 625 ret = __fwnode_property_read_string(fwnode->secondary, 626 propname, val); 627 return ret; 628 } 629 EXPORT_SYMBOL_GPL(fwnode_property_read_string); 630 631 /** 632 * fwnode_property_match_string - find a string in an array and return index 633 * @fwnode: Firmware node to get the property of 634 * @propname: Name of the property holding the array 635 * @string: String to look for 636 * 637 * Find a given string in a string array and if it is found return the 638 * index back. 639 * 640 * Return: %0 if the property was found (success), 641 * %-EINVAL if given arguments are not valid, 642 * %-ENODATA if the property does not have a value, 643 * %-EPROTO if the property is not an array of strings, 644 * %-ENXIO if no suitable firmware interface is present. 645 */ 646 int fwnode_property_match_string(struct fwnode_handle *fwnode, 647 const char *propname, const char *string) 648 { 649 const char **values; 650 int nval, ret, i; 651 652 nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0); 653 if (nval < 0) 654 return nval; 655 656 if (nval == 0) 657 return -ENODATA; 658 659 values = kcalloc(nval, sizeof(*values), GFP_KERNEL); 660 if (!values) 661 return -ENOMEM; 662 663 ret = fwnode_property_read_string_array(fwnode, propname, values, nval); 664 if (ret < 0) 665 goto out; 666 667 ret = -ENODATA; 668 for (i = 0; i < nval; i++) { 669 if (!strcmp(values[i], string)) { 670 ret = i; 671 break; 672 } 673 } 674 out: 675 kfree(values); 676 return ret; 677 } 678 EXPORT_SYMBOL_GPL(fwnode_property_match_string); 679 680 /** 681 * pset_free_set - releases memory allocated for copied property set 682 * @pset: Property set to release 683 * 684 * Function takes previously copied property set and releases all the 685 * memory allocated to it. 686 */ 687 static void pset_free_set(struct property_set *pset) 688 { 689 const struct property_entry *prop; 690 size_t i, nval; 691 692 if (!pset) 693 return; 694 695 for (prop = pset->properties; prop->name; prop++) { 696 if (prop->is_array) { 697 if (prop->is_string && prop->pointer.str) { 698 nval = prop->length / sizeof(const char *); 699 for (i = 0; i < nval; i++) 700 kfree(prop->pointer.str[i]); 701 } 702 kfree(prop->pointer.raw_data); 703 } else if (prop->is_string) { 704 kfree(prop->value.str); 705 } 706 kfree(prop->name); 707 } 708 709 kfree(pset->properties); 710 kfree(pset); 711 } 712 713 static int pset_copy_entry(struct property_entry *dst, 714 const struct property_entry *src) 715 { 716 const char **d, **s; 717 size_t i, nval; 718 719 dst->name = kstrdup(src->name, GFP_KERNEL); 720 if (!dst->name) 721 return -ENOMEM; 722 723 if (src->is_array) { 724 if (!src->length) 725 return -ENODATA; 726 727 if (src->is_string) { 728 nval = src->length / sizeof(const char *); 729 dst->pointer.str = kcalloc(nval, sizeof(const char *), 730 GFP_KERNEL); 731 if (!dst->pointer.str) 732 return -ENOMEM; 733 734 d = dst->pointer.str; 735 s = src->pointer.str; 736 for (i = 0; i < nval; i++) { 737 d[i] = kstrdup(s[i], GFP_KERNEL); 738 if (!d[i] && s[i]) 739 return -ENOMEM; 740 } 741 } else { 742 dst->pointer.raw_data = kmemdup(src->pointer.raw_data, 743 src->length, GFP_KERNEL); 744 if (!dst->pointer.raw_data) 745 return -ENOMEM; 746 } 747 } else if (src->is_string) { 748 dst->value.str = kstrdup(src->value.str, GFP_KERNEL); 749 if (!dst->value.str && src->value.str) 750 return -ENOMEM; 751 } else { 752 dst->value.raw_data = src->value.raw_data; 753 } 754 755 dst->length = src->length; 756 dst->is_array = src->is_array; 757 dst->is_string = src->is_string; 758 759 return 0; 760 } 761 762 /** 763 * pset_copy_set - copies property set 764 * @pset: Property set to copy 765 * 766 * This function takes a deep copy of the given property set and returns 767 * pointer to the copy. Call device_free_property_set() to free resources 768 * allocated in this function. 769 * 770 * Return: Pointer to the new property set or error pointer. 771 */ 772 static struct property_set *pset_copy_set(const struct property_set *pset) 773 { 774 const struct property_entry *entry; 775 struct property_set *p; 776 size_t i, n = 0; 777 778 p = kzalloc(sizeof(*p), GFP_KERNEL); 779 if (!p) 780 return ERR_PTR(-ENOMEM); 781 782 while (pset->properties[n].name) 783 n++; 784 785 p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL); 786 if (!p->properties) { 787 kfree(p); 788 return ERR_PTR(-ENOMEM); 789 } 790 791 for (i = 0; i < n; i++) { 792 int ret = pset_copy_entry(&p->properties[i], 793 &pset->properties[i]); 794 if (ret) { 795 pset_free_set(p); 796 return ERR_PTR(ret); 797 } 798 } 799 800 return p; 801 } 802 803 /** 804 * device_remove_property_set - Remove properties from a device object. 805 * @dev: Device whose properties to remove. 806 * 807 * The function removes properties previously associated to the device 808 * secondary firmware node with device_add_property_set(). Memory allocated 809 * to the properties will also be released. 810 */ 811 void device_remove_property_set(struct device *dev) 812 { 813 struct fwnode_handle *fwnode; 814 815 fwnode = dev_fwnode(dev); 816 if (!fwnode) 817 return; 818 /* 819 * Pick either primary or secondary node depending which one holds 820 * the pset. If there is no real firmware node (ACPI/DT) primary 821 * will hold the pset. 822 */ 823 if (!is_pset_node(fwnode)) 824 fwnode = fwnode->secondary; 825 if (!IS_ERR(fwnode) && is_pset_node(fwnode)) 826 pset_free_set(to_pset_node(fwnode)); 827 set_secondary_fwnode(dev, NULL); 828 } 829 EXPORT_SYMBOL_GPL(device_remove_property_set); 830 831 /** 832 * device_add_property_set - Add a collection of properties to a device object. 833 * @dev: Device to add properties to. 834 * @pset: Collection of properties to add. 835 * 836 * Associate a collection of device properties represented by @pset with @dev 837 * as its secondary firmware node. The function takes a copy of @pset. 838 */ 839 int device_add_property_set(struct device *dev, const struct property_set *pset) 840 { 841 struct property_set *p; 842 843 if (!pset) 844 return -EINVAL; 845 846 p = pset_copy_set(pset); 847 if (IS_ERR(p)) 848 return PTR_ERR(p); 849 850 p->fwnode.type = FWNODE_PDATA; 851 set_secondary_fwnode(dev, &p->fwnode); 852 return 0; 853 } 854 EXPORT_SYMBOL_GPL(device_add_property_set); 855 856 /** 857 * device_get_next_child_node - Return the next child node handle for a device 858 * @dev: Device to find the next child node for. 859 * @child: Handle to one of the device's child nodes or a null handle. 860 */ 861 struct fwnode_handle *device_get_next_child_node(struct device *dev, 862 struct fwnode_handle *child) 863 { 864 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { 865 struct device_node *node; 866 867 node = of_get_next_available_child(dev->of_node, to_of_node(child)); 868 if (node) 869 return &node->fwnode; 870 } else if (IS_ENABLED(CONFIG_ACPI)) { 871 return acpi_get_next_subnode(dev, child); 872 } 873 return NULL; 874 } 875 EXPORT_SYMBOL_GPL(device_get_next_child_node); 876 877 /** 878 * fwnode_handle_put - Drop reference to a device node 879 * @fwnode: Pointer to the device node to drop the reference to. 880 * 881 * This has to be used when terminating device_for_each_child_node() iteration 882 * with break or return to prevent stale device node references from being left 883 * behind. 884 */ 885 void fwnode_handle_put(struct fwnode_handle *fwnode) 886 { 887 if (is_of_node(fwnode)) 888 of_node_put(to_of_node(fwnode)); 889 } 890 EXPORT_SYMBOL_GPL(fwnode_handle_put); 891 892 /** 893 * device_get_child_node_count - return the number of child nodes for device 894 * @dev: Device to cound the child nodes for 895 */ 896 unsigned int device_get_child_node_count(struct device *dev) 897 { 898 struct fwnode_handle *child; 899 unsigned int count = 0; 900 901 device_for_each_child_node(dev, child) 902 count++; 903 904 return count; 905 } 906 EXPORT_SYMBOL_GPL(device_get_child_node_count); 907 908 bool device_dma_supported(struct device *dev) 909 { 910 /* For DT, this is always supported. 911 * For ACPI, this depends on CCA, which 912 * is determined by the acpi_dma_supported(). 913 */ 914 if (IS_ENABLED(CONFIG_OF) && dev->of_node) 915 return true; 916 917 return acpi_dma_supported(ACPI_COMPANION(dev)); 918 } 919 EXPORT_SYMBOL_GPL(device_dma_supported); 920 921 enum dev_dma_attr device_get_dma_attr(struct device *dev) 922 { 923 enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED; 924 925 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { 926 if (of_dma_is_coherent(dev->of_node)) 927 attr = DEV_DMA_COHERENT; 928 else 929 attr = DEV_DMA_NON_COHERENT; 930 } else 931 attr = acpi_get_dma_attr(ACPI_COMPANION(dev)); 932 933 return attr; 934 } 935 EXPORT_SYMBOL_GPL(device_get_dma_attr); 936 937 /** 938 * device_get_phy_mode - Get phy mode for given device 939 * @dev: Pointer to the given device 940 * 941 * The function gets phy interface string from property 'phy-mode' or 942 * 'phy-connection-type', and return its index in phy_modes table, or errno in 943 * error case. 944 */ 945 int device_get_phy_mode(struct device *dev) 946 { 947 const char *pm; 948 int err, i; 949 950 err = device_property_read_string(dev, "phy-mode", &pm); 951 if (err < 0) 952 err = device_property_read_string(dev, 953 "phy-connection-type", &pm); 954 if (err < 0) 955 return err; 956 957 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) 958 if (!strcasecmp(pm, phy_modes(i))) 959 return i; 960 961 return -ENODEV; 962 } 963 EXPORT_SYMBOL_GPL(device_get_phy_mode); 964 965 static void *device_get_mac_addr(struct device *dev, 966 const char *name, char *addr, 967 int alen) 968 { 969 int ret = device_property_read_u8_array(dev, name, addr, alen); 970 971 if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr)) 972 return addr; 973 return NULL; 974 } 975 976 /** 977 * device_get_mac_address - Get the MAC for a given device 978 * @dev: Pointer to the device 979 * @addr: Address of buffer to store the MAC in 980 * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN 981 * 982 * Search the firmware node for the best MAC address to use. 'mac-address' is 983 * checked first, because that is supposed to contain to "most recent" MAC 984 * address. If that isn't set, then 'local-mac-address' is checked next, 985 * because that is the default address. If that isn't set, then the obsolete 986 * 'address' is checked, just in case we're using an old device tree. 987 * 988 * Note that the 'address' property is supposed to contain a virtual address of 989 * the register set, but some DTS files have redefined that property to be the 990 * MAC address. 991 * 992 * All-zero MAC addresses are rejected, because those could be properties that 993 * exist in the firmware tables, but were not updated by the firmware. For 994 * example, the DTS could define 'mac-address' and 'local-mac-address', with 995 * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'. 996 * In this case, the real MAC is in 'local-mac-address', and 'mac-address' 997 * exists but is all zeros. 998 */ 999 void *device_get_mac_address(struct device *dev, char *addr, int alen) 1000 { 1001 char *res; 1002 1003 res = device_get_mac_addr(dev, "mac-address", addr, alen); 1004 if (res) 1005 return res; 1006 1007 res = device_get_mac_addr(dev, "local-mac-address", addr, alen); 1008 if (res) 1009 return res; 1010 1011 return device_get_mac_addr(dev, "address", addr, alen); 1012 } 1013 EXPORT_SYMBOL(device_get_mac_address); 1014