Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   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
  22static inline bool is_pset_node(struct fwnode_handle *fwnode)
  23{
  24	return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_PDATA;
  25}
  26
  27static 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
  33static 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
  48static 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
  68static 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
  83static 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
  98static 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
 113static 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
 128static 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
 140static 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
 155static 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
 180static 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 */
 193bool device_property_present(struct device *dev, const char *propname)
 194{
 195	return fwnode_property_present(dev_fwnode(dev), propname);
 196}
 197EXPORT_SYMBOL_GPL(device_property_present);
 198
 199static 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 */
 216bool 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 && !IS_ERR_OR_NULL(fwnode) &&
 222	    !IS_ERR_OR_NULL(fwnode->secondary))
 223		ret = __fwnode_property_present(fwnode->secondary, propname);
 224	return ret;
 225}
 226EXPORT_SYMBOL_GPL(fwnode_property_present);
 227
 228/**
 229 * device_property_read_u8_array - return a u8 array property of a device
 230 * @dev: Device to get the property of
 231 * @propname: Name of the property
 232 * @val: The values are stored here or %NULL to return the number of values
 233 * @nval: Size of the @val array
 234 *
 235 * Function reads an array of u8 properties with @propname from the device
 236 * firmware description and stores them to @val if found.
 237 *
 238 * Return: number of values if @val was %NULL,
 239 *         %0 if the property was found (success),
 240 *	   %-EINVAL if given arguments are not valid,
 241 *	   %-ENODATA if the property does not have a value,
 242 *	   %-EPROTO if the property is not an array of numbers,
 243 *	   %-EOVERFLOW if the size of the property is not as expected.
 244 *	   %-ENXIO if no suitable firmware interface is present.
 245 */
 246int device_property_read_u8_array(struct device *dev, const char *propname,
 247				  u8 *val, size_t nval)
 248{
 249	return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
 250}
 251EXPORT_SYMBOL_GPL(device_property_read_u8_array);
 252
 253/**
 254 * device_property_read_u16_array - return a u16 array property of a device
 255 * @dev: Device to get the property of
 256 * @propname: Name of the property
 257 * @val: The values are stored here or %NULL to return the number of values
 258 * @nval: Size of the @val array
 259 *
 260 * Function reads an array of u16 properties with @propname from the device
 261 * firmware description and stores them to @val if found.
 262 *
 263 * Return: number of values if @val was %NULL,
 264 *         %0 if the property was found (success),
 265 *	   %-EINVAL if given arguments are not valid,
 266 *	   %-ENODATA if the property does not have a value,
 267 *	   %-EPROTO if the property is not an array of numbers,
 268 *	   %-EOVERFLOW if the size of the property is not as expected.
 269 *	   %-ENXIO if no suitable firmware interface is present.
 270 */
 271int device_property_read_u16_array(struct device *dev, const char *propname,
 272				   u16 *val, size_t nval)
 273{
 274	return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
 275}
 276EXPORT_SYMBOL_GPL(device_property_read_u16_array);
 277
 278/**
 279 * device_property_read_u32_array - return a u32 array property of a device
 280 * @dev: Device to get the property of
 281 * @propname: Name of the property
 282 * @val: The values are stored here or %NULL to return the number of values
 283 * @nval: Size of the @val array
 284 *
 285 * Function reads an array of u32 properties with @propname from the device
 286 * firmware description and stores them to @val if found.
 287 *
 288 * Return: number of values if @val was %NULL,
 289 *         %0 if the property was found (success),
 290 *	   %-EINVAL if given arguments are not valid,
 291 *	   %-ENODATA if the property does not have a value,
 292 *	   %-EPROTO if the property is not an array of numbers,
 293 *	   %-EOVERFLOW if the size of the property is not as expected.
 294 *	   %-ENXIO if no suitable firmware interface is present.
 295 */
 296int device_property_read_u32_array(struct device *dev, const char *propname,
 297				   u32 *val, size_t nval)
 298{
 299	return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
 300}
 301EXPORT_SYMBOL_GPL(device_property_read_u32_array);
 302
 303/**
 304 * device_property_read_u64_array - return a u64 array property of a device
 305 * @dev: Device to get the property of
 306 * @propname: Name of the property
 307 * @val: The values are stored here or %NULL to return the number of values
 308 * @nval: Size of the @val array
 309 *
 310 * Function reads an array of u64 properties with @propname from the device
 311 * firmware description and stores them to @val if found.
 312 *
 313 * Return: number of values if @val was %NULL,
 314 *         %0 if the property was found (success),
 315 *	   %-EINVAL if given arguments are not valid,
 316 *	   %-ENODATA if the property does not have a value,
 317 *	   %-EPROTO if the property is not an array of numbers,
 318 *	   %-EOVERFLOW if the size of the property is not as expected.
 319 *	   %-ENXIO if no suitable firmware interface is present.
 320 */
 321int device_property_read_u64_array(struct device *dev, const char *propname,
 322				   u64 *val, size_t nval)
 323{
 324	return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
 325}
 326EXPORT_SYMBOL_GPL(device_property_read_u64_array);
 327
 328/**
 329 * device_property_read_string_array - return a string array property of device
 330 * @dev: Device to get the property of
 331 * @propname: Name of the property
 332 * @val: The values are stored here or %NULL to return the number of values
 333 * @nval: Size of the @val array
 334 *
 335 * Function reads an array of string properties with @propname from the device
 336 * firmware description and stores them to @val if found.
 337 *
 338 * Return: number of values if @val was %NULL,
 339 *         %0 if the property was found (success),
 340 *	   %-EINVAL if given arguments are not valid,
 341 *	   %-ENODATA if the property does not have a value,
 342 *	   %-EPROTO or %-EILSEQ if the property is not an array of strings,
 343 *	   %-EOVERFLOW if the size of the property is not as expected.
 344 *	   %-ENXIO if no suitable firmware interface is present.
 345 */
 346int device_property_read_string_array(struct device *dev, const char *propname,
 347				      const char **val, size_t nval)
 348{
 349	return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
 350}
 351EXPORT_SYMBOL_GPL(device_property_read_string_array);
 352
 353/**
 354 * device_property_read_string - return a string property of a device
 355 * @dev: Device to get the property of
 356 * @propname: Name of the property
 357 * @val: The value is stored here
 358 *
 359 * Function reads property @propname from the device firmware description and
 360 * stores the value into @val if found. The value is checked to be a string.
 361 *
 362 * Return: %0 if the property was found (success),
 363 *	   %-EINVAL if given arguments are not valid,
 364 *	   %-ENODATA if the property does not have a value,
 365 *	   %-EPROTO or %-EILSEQ if the property type is not a string.
 366 *	   %-ENXIO if no suitable firmware interface is present.
 367 */
 368int device_property_read_string(struct device *dev, const char *propname,
 369				const char **val)
 370{
 371	return fwnode_property_read_string(dev_fwnode(dev), propname, val);
 372}
 373EXPORT_SYMBOL_GPL(device_property_read_string);
 374
 375/**
 376 * device_property_match_string - find a string in an array and return index
 377 * @dev: Device to get the property of
 378 * @propname: Name of the property holding the array
 379 * @string: String to look for
 380 *
 381 * Find a given string in a string array and if it is found return the
 382 * index back.
 383 *
 384 * Return: %0 if the property was found (success),
 385 *	   %-EINVAL if given arguments are not valid,
 386 *	   %-ENODATA if the property does not have a value,
 387 *	   %-EPROTO if the property is not an array of strings,
 388 *	   %-ENXIO if no suitable firmware interface is present.
 389 */
 390int device_property_match_string(struct device *dev, const char *propname,
 391				 const char *string)
 392{
 393	return fwnode_property_match_string(dev_fwnode(dev), propname, string);
 394}
 395EXPORT_SYMBOL_GPL(device_property_match_string);
 396
 397#define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval)				\
 398	(val) ? of_property_read_##type##_array((node), (propname), (val), (nval))	\
 399	      : of_property_count_elems_of_size((node), (propname), sizeof(type))
 400
 401#define PSET_PROP_READ_ARRAY(node, propname, type, val, nval)				\
 402	(val) ? pset_prop_read_##type##_array((node), (propname), (val), (nval))	\
 403	      : pset_prop_count_elems_of_size((node), (propname), sizeof(type))
 404
 405#define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_)	\
 406({											\
 407	int _ret_;									\
 408	if (is_of_node(_fwnode_))							\
 409		_ret_ = OF_DEV_PROP_READ_ARRAY(to_of_node(_fwnode_), _propname_,	\
 410					       _type_, _val_, _nval_);			\
 411	else if (is_acpi_node(_fwnode_))						\
 412		_ret_ = acpi_node_prop_read(_fwnode_, _propname_, _proptype_,		\
 413					    _val_, _nval_);				\
 414	else if (is_pset_node(_fwnode_)) 						\
 415		_ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_,	\
 416					     _type_, _val_, _nval_);			\
 417	else										\
 418		_ret_ = -ENXIO;								\
 419	_ret_;										\
 420})
 421
 422#define FWNODE_PROP_READ_ARRAY(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_)	\
 423({											\
 424	int _ret_;									\
 425	_ret_ = FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_,		\
 426				 _val_, _nval_);					\
 427	if (_ret_ == -EINVAL && !IS_ERR_OR_NULL(_fwnode_) &&				\
 428	    !IS_ERR_OR_NULL(_fwnode_->secondary))					\
 429		_ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_,	\
 430				_proptype_, _val_, _nval_);				\
 431	_ret_;										\
 432})
 433
 434/**
 435 * fwnode_property_read_u8_array - return a u8 array property of firmware node
 436 * @fwnode: Firmware node to get the property of
 437 * @propname: Name of the property
 438 * @val: The values are stored here or %NULL to return the number of values
 439 * @nval: Size of the @val array
 440 *
 441 * Read an array of u8 properties with @propname from @fwnode and stores them to
 442 * @val if found.
 443 *
 444 * Return: number of values if @val was %NULL,
 445 *         %0 if the property was found (success),
 446 *	   %-EINVAL if given arguments are not valid,
 447 *	   %-ENODATA if the property does not have a value,
 448 *	   %-EPROTO if the property is not an array of numbers,
 449 *	   %-EOVERFLOW if the size of the property is not as expected,
 450 *	   %-ENXIO if no suitable firmware interface is present.
 451 */
 452int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
 453				  const char *propname, u8 *val, size_t nval)
 454{
 455	return FWNODE_PROP_READ_ARRAY(fwnode, propname, u8, DEV_PROP_U8,
 456				      val, nval);
 457}
 458EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
 459
 460/**
 461 * fwnode_property_read_u16_array - return a u16 array property of firmware node
 462 * @fwnode: Firmware node to get the property of
 463 * @propname: Name of the property
 464 * @val: The values are stored here or %NULL to return the number of values
 465 * @nval: Size of the @val array
 466 *
 467 * Read an array of u16 properties with @propname from @fwnode and store them to
 468 * @val if found.
 469 *
 470 * Return: number of values if @val was %NULL,
 471 *         %0 if the property was found (success),
 472 *	   %-EINVAL if given arguments are not valid,
 473 *	   %-ENODATA if the property does not have a value,
 474 *	   %-EPROTO if the property is not an array of numbers,
 475 *	   %-EOVERFLOW if the size of the property is not as expected,
 476 *	   %-ENXIO if no suitable firmware interface is present.
 477 */
 478int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
 479				   const char *propname, u16 *val, size_t nval)
 480{
 481	return FWNODE_PROP_READ_ARRAY(fwnode, propname, u16, DEV_PROP_U16,
 482				      val, nval);
 483}
 484EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
 485
 486/**
 487 * fwnode_property_read_u32_array - return a u32 array property of firmware node
 488 * @fwnode: Firmware node to get the property of
 489 * @propname: Name of the property
 490 * @val: The values are stored here or %NULL to return the number of values
 491 * @nval: Size of the @val array
 492 *
 493 * Read an array of u32 properties with @propname from @fwnode store them to
 494 * @val if found.
 495 *
 496 * Return: number of values if @val was %NULL,
 497 *         %0 if the property was found (success),
 498 *	   %-EINVAL if given arguments are not valid,
 499 *	   %-ENODATA if the property does not have a value,
 500 *	   %-EPROTO if the property is not an array of numbers,
 501 *	   %-EOVERFLOW if the size of the property is not as expected,
 502 *	   %-ENXIO if no suitable firmware interface is present.
 503 */
 504int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
 505				   const char *propname, u32 *val, size_t nval)
 506{
 507	return FWNODE_PROP_READ_ARRAY(fwnode, propname, u32, DEV_PROP_U32,
 508				      val, nval);
 509}
 510EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
 511
 512/**
 513 * fwnode_property_read_u64_array - return a u64 array property firmware node
 514 * @fwnode: Firmware node to get the property of
 515 * @propname: Name of the property
 516 * @val: The values are stored here or %NULL to return the number of values
 517 * @nval: Size of the @val array
 518 *
 519 * Read an array of u64 properties with @propname from @fwnode and store them to
 520 * @val if found.
 521 *
 522 * Return: number of values if @val was %NULL,
 523 *         %0 if the property was found (success),
 524 *	   %-EINVAL if given arguments are not valid,
 525 *	   %-ENODATA if the property does not have a value,
 526 *	   %-EPROTO if the property is not an array of numbers,
 527 *	   %-EOVERFLOW if the size of the property is not as expected,
 528 *	   %-ENXIO if no suitable firmware interface is present.
 529 */
 530int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
 531				   const char *propname, u64 *val, size_t nval)
 532{
 533	return FWNODE_PROP_READ_ARRAY(fwnode, propname, u64, DEV_PROP_U64,
 534				      val, nval);
 535}
 536EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
 537
 538static int __fwnode_property_read_string_array(struct fwnode_handle *fwnode,
 539					       const char *propname,
 540					       const char **val, size_t nval)
 541{
 542	if (is_of_node(fwnode))
 543		return val ?
 544			of_property_read_string_array(to_of_node(fwnode),
 545						      propname, val, nval) :
 546			of_property_count_strings(to_of_node(fwnode), propname);
 547	else if (is_acpi_node(fwnode))
 548		return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
 549					   val, nval);
 550	else if (is_pset_node(fwnode))
 551		return val ?
 552			pset_prop_read_string_array(to_pset_node(fwnode),
 553						    propname, val, nval) :
 554			pset_prop_count_elems_of_size(to_pset_node(fwnode),
 555						      propname,
 556						      sizeof(const char *));
 557	return -ENXIO;
 558}
 559
 560static int __fwnode_property_read_string(struct fwnode_handle *fwnode,
 561					 const char *propname, const char **val)
 562{
 563	if (is_of_node(fwnode))
 564		return of_property_read_string(to_of_node(fwnode), propname, val);
 565	else if (is_acpi_node(fwnode))
 566		return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
 567					   val, 1);
 568	else if (is_pset_node(fwnode))
 569		return pset_prop_read_string(to_pset_node(fwnode), propname, val);
 570	return -ENXIO;
 571}
 572
 573/**
 574 * fwnode_property_read_string_array - return string array property of a node
 575 * @fwnode: Firmware node to get the property of
 576 * @propname: Name of the property
 577 * @val: The values are stored here or %NULL to return the number of values
 578 * @nval: Size of the @val array
 579 *
 580 * Read an string list property @propname from the given firmware node and store
 581 * them to @val if found.
 582 *
 583 * Return: number of values if @val was %NULL,
 584 *         %0 if the property was found (success),
 585 *	   %-EINVAL if given arguments are not valid,
 586 *	   %-ENODATA if the property does not have a value,
 587 *	   %-EPROTO if the property is not an array of strings,
 588 *	   %-EOVERFLOW if the size of the property is not as expected,
 589 *	   %-ENXIO if no suitable firmware interface is present.
 590 */
 591int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
 592				      const char *propname, const char **val,
 593				      size_t nval)
 594{
 595	int ret;
 596
 597	ret = __fwnode_property_read_string_array(fwnode, propname, val, nval);
 598	if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
 599	    !IS_ERR_OR_NULL(fwnode->secondary))
 600		ret = __fwnode_property_read_string_array(fwnode->secondary,
 601							  propname, val, nval);
 602	return ret;
 603}
 604EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
 605
 606/**
 607 * fwnode_property_read_string - return a string property of a firmware node
 608 * @fwnode: Firmware node to get the property of
 609 * @propname: Name of the property
 610 * @val: The value is stored here
 611 *
 612 * Read property @propname from the given firmware node and store the value into
 613 * @val if found.  The value is checked to be a string.
 614 *
 615 * Return: %0 if the property was found (success),
 616 *	   %-EINVAL if given arguments are not valid,
 617 *	   %-ENODATA if the property does not have a value,
 618 *	   %-EPROTO or %-EILSEQ if the property is not a string,
 619 *	   %-ENXIO if no suitable firmware interface is present.
 620 */
 621int fwnode_property_read_string(struct fwnode_handle *fwnode,
 622				const char *propname, const char **val)
 623{
 624	int ret;
 625
 626	ret = __fwnode_property_read_string(fwnode, propname, val);
 627	if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
 628	    !IS_ERR_OR_NULL(fwnode->secondary))
 629		ret = __fwnode_property_read_string(fwnode->secondary,
 630						    propname, val);
 631	return ret;
 632}
 633EXPORT_SYMBOL_GPL(fwnode_property_read_string);
 634
 635/**
 636 * fwnode_property_match_string - find a string in an array and return index
 637 * @fwnode: Firmware node to get the property of
 638 * @propname: Name of the property holding the array
 639 * @string: String to look for
 640 *
 641 * Find a given string in a string array and if it is found return the
 642 * index back.
 643 *
 644 * Return: %0 if the property was found (success),
 645 *	   %-EINVAL if given arguments are not valid,
 646 *	   %-ENODATA if the property does not have a value,
 647 *	   %-EPROTO if the property is not an array of strings,
 648 *	   %-ENXIO if no suitable firmware interface is present.
 649 */
 650int fwnode_property_match_string(struct fwnode_handle *fwnode,
 651	const char *propname, const char *string)
 652{
 653	const char **values;
 654	int nval, ret;
 655
 656	nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
 657	if (nval < 0)
 658		return nval;
 659
 660	if (nval == 0)
 661		return -ENODATA;
 662
 663	values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
 664	if (!values)
 665		return -ENOMEM;
 666
 667	ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
 668	if (ret < 0)
 669		goto out;
 670
 671	ret = match_string(values, nval, string);
 672	if (ret < 0)
 673		ret = -ENODATA;
 674out:
 675	kfree(values);
 676	return ret;
 677}
 678EXPORT_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 */
 687static 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
 713static 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 */
 772static 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 */
 811void 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		set_primary_fwnode(dev, NULL);
 825		pset_free_set(to_pset_node(fwnode));
 826	} else {
 827		fwnode = fwnode->secondary;
 828		if (!IS_ERR(fwnode) && is_pset_node(fwnode)) {
 829			set_secondary_fwnode(dev, NULL);
 830			pset_free_set(to_pset_node(fwnode));
 831		}
 832	}
 833}
 834EXPORT_SYMBOL_GPL(device_remove_property_set);
 835
 836/**
 837 * device_add_property_set - Add a collection of properties to a device object.
 838 * @dev: Device to add properties to.
 839 * @pset: Collection of properties to add.
 840 *
 841 * Associate a collection of device properties represented by @pset with @dev
 842 * as its secondary firmware node. The function takes a copy of @pset.
 843 */
 844int device_add_property_set(struct device *dev, const struct property_set *pset)
 845{
 846	struct property_set *p;
 847
 848	if (!pset)
 849		return -EINVAL;
 850
 851	p = pset_copy_set(pset);
 852	if (IS_ERR(p))
 853		return PTR_ERR(p);
 854
 855	p->fwnode.type = FWNODE_PDATA;
 856	set_secondary_fwnode(dev, &p->fwnode);
 857	return 0;
 858}
 859EXPORT_SYMBOL_GPL(device_add_property_set);
 860
 861/**
 862 * device_get_next_child_node - Return the next child node handle for a device
 863 * @dev: Device to find the next child node for.
 864 * @child: Handle to one of the device's child nodes or a null handle.
 865 */
 866struct fwnode_handle *device_get_next_child_node(struct device *dev,
 867						 struct fwnode_handle *child)
 868{
 869	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
 870		struct device_node *node;
 871
 872		node = of_get_next_available_child(dev->of_node, to_of_node(child));
 873		if (node)
 874			return &node->fwnode;
 875	} else if (IS_ENABLED(CONFIG_ACPI)) {
 876		return acpi_get_next_subnode(dev, child);
 877	}
 878	return NULL;
 879}
 880EXPORT_SYMBOL_GPL(device_get_next_child_node);
 881
 882/**
 883 * fwnode_handle_put - Drop reference to a device node
 884 * @fwnode: Pointer to the device node to drop the reference to.
 885 *
 886 * This has to be used when terminating device_for_each_child_node() iteration
 887 * with break or return to prevent stale device node references from being left
 888 * behind.
 889 */
 890void fwnode_handle_put(struct fwnode_handle *fwnode)
 891{
 892	if (is_of_node(fwnode))
 893		of_node_put(to_of_node(fwnode));
 894}
 895EXPORT_SYMBOL_GPL(fwnode_handle_put);
 896
 897/**
 898 * device_get_child_node_count - return the number of child nodes for device
 899 * @dev: Device to cound the child nodes for
 900 */
 901unsigned int device_get_child_node_count(struct device *dev)
 902{
 903	struct fwnode_handle *child;
 904	unsigned int count = 0;
 905
 906	device_for_each_child_node(dev, child)
 907		count++;
 908
 909	return count;
 910}
 911EXPORT_SYMBOL_GPL(device_get_child_node_count);
 912
 913bool device_dma_supported(struct device *dev)
 914{
 915	/* For DT, this is always supported.
 916	 * For ACPI, this depends on CCA, which
 917	 * is determined by the acpi_dma_supported().
 918	 */
 919	if (IS_ENABLED(CONFIG_OF) && dev->of_node)
 920		return true;
 921
 922	return acpi_dma_supported(ACPI_COMPANION(dev));
 923}
 924EXPORT_SYMBOL_GPL(device_dma_supported);
 925
 926enum dev_dma_attr device_get_dma_attr(struct device *dev)
 927{
 928	enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
 929
 930	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
 931		if (of_dma_is_coherent(dev->of_node))
 932			attr = DEV_DMA_COHERENT;
 933		else
 934			attr = DEV_DMA_NON_COHERENT;
 935	} else
 936		attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
 937
 938	return attr;
 939}
 940EXPORT_SYMBOL_GPL(device_get_dma_attr);
 941
 942/**
 943 * device_get_phy_mode - Get phy mode for given device
 944 * @dev:	Pointer to the given device
 945 *
 946 * The function gets phy interface string from property 'phy-mode' or
 947 * 'phy-connection-type', and return its index in phy_modes table, or errno in
 948 * error case.
 949 */
 950int device_get_phy_mode(struct device *dev)
 951{
 952	const char *pm;
 953	int err, i;
 954
 955	err = device_property_read_string(dev, "phy-mode", &pm);
 956	if (err < 0)
 957		err = device_property_read_string(dev,
 958						  "phy-connection-type", &pm);
 959	if (err < 0)
 960		return err;
 961
 962	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
 963		if (!strcasecmp(pm, phy_modes(i)))
 964			return i;
 965
 966	return -ENODEV;
 967}
 968EXPORT_SYMBOL_GPL(device_get_phy_mode);
 969
 970static void *device_get_mac_addr(struct device *dev,
 971				 const char *name, char *addr,
 972				 int alen)
 973{
 974	int ret = device_property_read_u8_array(dev, name, addr, alen);
 975
 976	if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
 977		return addr;
 978	return NULL;
 979}
 980
 981/**
 982 * device_get_mac_address - Get the MAC for a given device
 983 * @dev:	Pointer to the device
 984 * @addr:	Address of buffer to store the MAC in
 985 * @alen:	Length of the buffer pointed to by addr, should be ETH_ALEN
 986 *
 987 * Search the firmware node for the best MAC address to use.  'mac-address' is
 988 * checked first, because that is supposed to contain to "most recent" MAC
 989 * address. If that isn't set, then 'local-mac-address' is checked next,
 990 * because that is the default address.  If that isn't set, then the obsolete
 991 * 'address' is checked, just in case we're using an old device tree.
 992 *
 993 * Note that the 'address' property is supposed to contain a virtual address of
 994 * the register set, but some DTS files have redefined that property to be the
 995 * MAC address.
 996 *
 997 * All-zero MAC addresses are rejected, because those could be properties that
 998 * exist in the firmware tables, but were not updated by the firmware.  For
 999 * example, the DTS could define 'mac-address' and 'local-mac-address', with
1000 * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
1001 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
1002 * exists but is all zeros.
1003*/
1004void *device_get_mac_address(struct device *dev, char *addr, int alen)
1005{
1006	char *res;
1007
1008	res = device_get_mac_addr(dev, "mac-address", addr, alen);
1009	if (res)
1010		return res;
1011
1012	res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
1013	if (res)
1014		return res;
1015
1016	return device_get_mac_addr(dev, "address", addr, alen);
1017}
1018EXPORT_SYMBOL(device_get_mac_address);