Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 13 | #include <linux/acpi.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 14 | #include <linux/export.h> |
| 15 | #include <linux/kernel.h> |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 16 | #include <linux/of.h> |
Suthikulpanit, Suravee | 05ca556 | 2015-06-10 11:08:54 -0500 | [diff] [blame] | 17 | #include <linux/of_address.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 18 | #include <linux/property.h> |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/phy.h> |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 21 | |
Andy Shevchenko | 61f5e29 | 2015-11-30 17:11:30 +0200 | [diff] [blame] | 22 | static inline bool is_pset_node(struct fwnode_handle *fwnode) |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 23 | { |
| 24 | return fwnode && fwnode->type == FWNODE_PDATA; |
| 25 | } |
| 26 | |
Andy Shevchenko | 61f5e29 | 2015-11-30 17:11:30 +0200 | [diff] [blame] | 27 | static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode) |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 28 | { |
Andy Shevchenko | 61f5e29 | 2015-11-30 17:11:30 +0200 | [diff] [blame] | 29 | return is_pset_node(fwnode) ? |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 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 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 48 | static void *pset_prop_find(struct property_set *pset, const char *propname, |
| 49 | size_t length) |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 50 | { |
| 51 | struct property_entry *prop; |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 52 | void *pointer; |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 53 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 54 | prop = pset_prop_get(pset, propname); |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 55 | if (!prop) |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 56 | return ERR_PTR(-EINVAL); |
Andy Shevchenko | 66586ba | 2015-11-30 17:11:32 +0200 | [diff] [blame] | 57 | if (prop->is_array) |
| 58 | pointer = prop->pointer.raw_data; |
| 59 | else |
| 60 | pointer = &prop->value.raw_data; |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 61 | if (!pointer) |
| 62 | return ERR_PTR(-ENODATA); |
| 63 | if (length > prop->length) |
| 64 | return ERR_PTR(-EOVERFLOW); |
| 65 | return pointer; |
| 66 | } |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 67 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 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); |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 74 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 75 | pointer = pset_prop_find(pset, propname, length); |
| 76 | if (IS_ERR(pointer)) |
| 77 | return PTR_ERR(pointer); |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 78 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 79 | memcpy(values, pointer, length); |
| 80 | return 0; |
| 81 | } |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 82 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 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) |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 135 | return -EINVAL; |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 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); |
Rafael J. Wysocki | 16ba08d | 2015-04-03 16:05:11 +0200 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 154 | |
Andy Shevchenko | 66586ba | 2015-11-30 17:11:32 +0200 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 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 | } |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 195 | return fwnode_property_present(dev_fwnode(dev), propname); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 196 | } |
| 197 | EXPORT_SYMBOL_GPL(device_property_present); |
| 198 | |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 199 | static bool __fwnode_property_present(struct fwnode_handle *fwnode, |
| 200 | const char *propname) |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 201 | { |
| 202 | if (is_of_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 203 | return of_property_read_bool(to_of_node(fwnode), propname); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 204 | else if (is_acpi_node(fwnode)) |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 205 | return !acpi_node_prop_get(fwnode, propname, NULL); |
Andy Shevchenko | 61f5e29 | 2015-11-30 17:11:30 +0200 | [diff] [blame] | 206 | else if (is_pset_node(fwnode)) |
| 207 | return !!pset_prop_get(to_pset_node(fwnode), propname); |
Andy Shevchenko | e3f9e29 | 2015-11-30 17:11:29 +0200 | [diff] [blame] | 208 | return false; |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 209 | } |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 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->secondary) |
| 222 | ret = __fwnode_property_present(fwnode->secondary, propname); |
| 223 | return ret; |
| 224 | } |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(fwnode_property_present); |
| 226 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 231 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 237 | * Return: number of values if @val was %NULL, |
| 238 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 243 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 244 | */ |
| 245 | int device_property_read_u8_array(struct device *dev, const char *propname, |
| 246 | u8 *val, size_t nval) |
| 247 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 248 | return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 256 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 262 | * Return: number of values if @val was %NULL, |
| 263 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 268 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 269 | */ |
| 270 | int device_property_read_u16_array(struct device *dev, const char *propname, |
| 271 | u16 *val, size_t nval) |
| 272 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 273 | return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 281 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 287 | * Return: number of values if @val was %NULL, |
| 288 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 293 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 294 | */ |
| 295 | int device_property_read_u32_array(struct device *dev, const char *propname, |
| 296 | u32 *val, size_t nval) |
| 297 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 298 | return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 306 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 312 | * Return: number of values if @val was %NULL, |
| 313 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 318 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 319 | */ |
| 320 | int device_property_read_u64_array(struct device *dev, const char *propname, |
| 321 | u64 *val, size_t nval) |
| 322 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 323 | return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 331 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 337 | * Return: number of values if @val was %NULL, |
| 338 | * %0 if the property was found (success), |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 343 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 344 | */ |
| 345 | int device_property_read_string_array(struct device *dev, const char *propname, |
| 346 | const char **val, size_t nval) |
| 347 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 348 | return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 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. |
Guenter Roeck | 4fa7508e | 2015-08-26 20:27:04 -0700 | [diff] [blame] | 365 | * %-ENXIO if no suitable firmware interface is present. |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 366 | */ |
| 367 | int device_property_read_string(struct device *dev, const char *propname, |
| 368 | const char **val) |
| 369 | { |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 370 | return fwnode_property_read_string(dev_fwnode(dev), propname, val); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 371 | } |
| 372 | EXPORT_SYMBOL_GPL(device_property_read_string); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 373 | |
Mika Westerberg | 3f5c8d3 | 2015-09-14 17:37:35 +0300 | [diff] [blame] | 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 | |
Andy Shevchenko | 1d656fb | 2015-11-30 17:11:34 +0200 | [diff] [blame] | 396 | #define OF_DEV_PROP_READ_ARRAY(node, propname, type, val, nval) \ |
| 397 | (val) ? of_property_read_##type##_array((node), (propname), (val), (nval)) \ |
Rafael J. Wysocki | 9017f25 | 2015-03-24 00:24:16 +0100 | [diff] [blame] | 398 | : of_property_count_elems_of_size((node), (propname), sizeof(type)) |
| 399 | |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 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 | |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 404 | #define FWNODE_PROP_READ(_fwnode_, _propname_, _type_, _proptype_, _val_, _nval_) \ |
Andy Shevchenko | 1d656fb | 2015-11-30 17:11:34 +0200 | [diff] [blame] | 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_); \ |
Andy Shevchenko | 61f5e29 | 2015-11-30 17:11:30 +0200 | [diff] [blame] | 413 | else if (is_pset_node(_fwnode_)) \ |
Andy Shevchenko | 318a1971 | 2015-11-30 17:11:31 +0200 | [diff] [blame] | 414 | _ret_ = PSET_PROP_READ_ARRAY(to_pset_node(_fwnode_), _propname_, \ |
| 415 | _type_, _val_, _nval_); \ |
Andy Shevchenko | 1d656fb | 2015-11-30 17:11:34 +0200 | [diff] [blame] | 416 | else \ |
| 417 | _ret_ = -ENXIO; \ |
| 418 | _ret_; \ |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 419 | }) |
| 420 | |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 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_->secondary) \ |
| 427 | _ret_ = FWNODE_PROP_READ(_fwnode_->secondary, _propname_, _type_, \ |
| 428 | _proptype_, _val_, _nval_); \ |
| 429 | _ret_; \ |
| 430 | }) |
| 431 | |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 436 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 442 | * Return: number of values if @val was %NULL, |
| 443 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 462 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 468 | * Return: number of values if @val was %NULL, |
| 469 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 488 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 494 | * Return: number of values if @val was %NULL, |
| 495 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 514 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 520 | * Return: number of values if @val was %NULL, |
| 521 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 575 | * @val: The values are stored here or %NULL to return the number of values |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | * |
Adrian Hunter | 5c0acf3 | 2015-03-17 09:58:58 +0200 | [diff] [blame] | 581 | * Return: number of values if @val was %NULL, |
| 582 | * %0 if the property was found (success), |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | { |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 593 | int ret; |
| 594 | |
| 595 | ret = __fwnode_property_read_string_array(fwnode, propname, val, nval); |
| 596 | if (ret == -EINVAL && fwnode->secondary) |
| 597 | ret = __fwnode_property_read_string_array(fwnode->secondary, |
| 598 | propname, val, nval); |
| 599 | return ret; |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 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 | { |
Andy Shevchenko | 362c0b3 | 2015-11-30 17:11:36 +0200 | [diff] [blame] | 621 | int ret; |
| 622 | |
| 623 | ret = __fwnode_property_read_string(fwnode, propname, val); |
| 624 | if (ret == -EINVAL && fwnode->secondary) |
| 625 | ret = __fwnode_property_read_string(fwnode->secondary, |
| 626 | propname, val); |
| 627 | return ret; |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 628 | } |
| 629 | EXPORT_SYMBOL_GPL(fwnode_property_read_string); |
| 630 | |
| 631 | /** |
Mika Westerberg | 3f5c8d3 | 2015-09-14 17:37:35 +0300 | [diff] [blame] | 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 | values = kcalloc(nval, sizeof(*values), GFP_KERNEL); |
| 657 | if (!values) |
| 658 | return -ENOMEM; |
| 659 | |
| 660 | ret = fwnode_property_read_string_array(fwnode, propname, values, nval); |
| 661 | if (ret < 0) |
| 662 | goto out; |
| 663 | |
| 664 | ret = -ENODATA; |
| 665 | for (i = 0; i < nval; i++) { |
| 666 | if (!strcmp(values[i], string)) { |
| 667 | ret = i; |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | out: |
| 672 | kfree(values); |
| 673 | return ret; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(fwnode_property_match_string); |
| 676 | |
| 677 | /** |
Mika Westerberg | 13141e1 | 2015-11-30 17:11:37 +0200 | [diff] [blame^] | 678 | * pset_free_set - releases memory allocated for copied property set |
| 679 | * @pset: Property set to release |
| 680 | * |
| 681 | * Function takes previously copied property set and releases all the |
| 682 | * memory allocated to it. |
| 683 | */ |
| 684 | static void pset_free_set(struct property_set *pset) |
| 685 | { |
| 686 | const struct property_entry *prop; |
| 687 | size_t i, nval; |
| 688 | |
| 689 | if (!pset) |
| 690 | return; |
| 691 | |
| 692 | for (prop = pset->properties; prop->name; prop++) { |
| 693 | if (prop->is_array) { |
| 694 | if (prop->is_string && prop->pointer.str) { |
| 695 | nval = prop->length / sizeof(const char *); |
| 696 | for (i = 0; i < nval; i++) |
| 697 | kfree(prop->pointer.str[i]); |
| 698 | } |
| 699 | kfree(prop->pointer.raw_data); |
| 700 | } else if (prop->is_string) { |
| 701 | kfree(prop->value.str); |
| 702 | } |
| 703 | kfree(prop->name); |
| 704 | } |
| 705 | |
| 706 | kfree(pset->properties); |
| 707 | kfree(pset); |
| 708 | } |
| 709 | |
| 710 | static int pset_copy_entry(struct property_entry *dst, |
| 711 | const struct property_entry *src) |
| 712 | { |
| 713 | const char **d, **s; |
| 714 | size_t i, nval; |
| 715 | |
| 716 | dst->name = kstrdup(src->name, GFP_KERNEL); |
| 717 | if (!dst->name) |
| 718 | return -ENOMEM; |
| 719 | |
| 720 | if (src->is_array) { |
| 721 | if (src->is_string) { |
| 722 | nval = src->length / sizeof(const char *); |
| 723 | dst->pointer.str = kcalloc(nval, sizeof(const char *), |
| 724 | GFP_KERNEL); |
| 725 | if (!dst->pointer.str) |
| 726 | return -ENOMEM; |
| 727 | |
| 728 | d = dst->pointer.str; |
| 729 | s = src->pointer.str; |
| 730 | for (i = 0; i < nval; i++) { |
| 731 | d[i] = kstrdup(s[i], GFP_KERNEL); |
| 732 | if (!d[i] && s[i]) |
| 733 | return -ENOMEM; |
| 734 | } |
| 735 | } else { |
| 736 | dst->pointer.raw_data = kmemdup(src->pointer.raw_data, |
| 737 | src->length, GFP_KERNEL); |
| 738 | if (!dst->pointer.raw_data) |
| 739 | return -ENOMEM; |
| 740 | } |
| 741 | } else if (src->is_string) { |
| 742 | dst->value.str = kstrdup(src->value.str, GFP_KERNEL); |
| 743 | if (!dst->value.str && src->value.str) |
| 744 | return -ENOMEM; |
| 745 | } else { |
| 746 | dst->value.raw_data = src->value.raw_data; |
| 747 | } |
| 748 | |
| 749 | dst->length = src->length; |
| 750 | dst->is_array = src->is_array; |
| 751 | dst->is_string = src->is_string; |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * pset_copy_set - copies property set |
| 758 | * @pset: Property set to copy |
| 759 | * |
| 760 | * This function takes a deep copy of the given property set and returns |
| 761 | * pointer to the copy. Call device_free_property_set() to free resources |
| 762 | * allocated in this function. |
| 763 | * |
| 764 | * Return: Pointer to the new property set or error pointer. |
| 765 | */ |
| 766 | static struct property_set *pset_copy_set(const struct property_set *pset) |
| 767 | { |
| 768 | const struct property_entry *entry; |
| 769 | struct property_set *p; |
| 770 | size_t i, n = 0; |
| 771 | |
| 772 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 773 | if (!p) |
| 774 | return ERR_PTR(-ENOMEM); |
| 775 | |
| 776 | while (pset->properties[n].name) |
| 777 | n++; |
| 778 | |
| 779 | p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL); |
| 780 | if (!p->properties) { |
| 781 | kfree(p); |
| 782 | return ERR_PTR(-ENOMEM); |
| 783 | } |
| 784 | |
| 785 | for (i = 0; i < n; i++) { |
| 786 | int ret = pset_copy_entry(&p->properties[i], |
| 787 | &pset->properties[i]); |
| 788 | if (ret) { |
| 789 | pset_free_set(p); |
| 790 | return ERR_PTR(ret); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return p; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * device_remove_property_set - Remove properties from a device object. |
| 799 | * @dev: Device whose properties to remove. |
| 800 | * |
| 801 | * The function removes properties previously associated to the device |
| 802 | * secondary firmware node with device_add_property_set(). Memory allocated |
| 803 | * to the properties will also be released. |
| 804 | */ |
| 805 | void device_remove_property_set(struct device *dev) |
| 806 | { |
| 807 | struct fwnode_handle *fwnode; |
| 808 | |
| 809 | fwnode = dev_fwnode(dev); |
| 810 | if (!fwnode) |
| 811 | return; |
| 812 | /* |
| 813 | * Pick either primary or secondary node depending which one holds |
| 814 | * the pset. If there is no real firmware node (ACPI/DT) primary |
| 815 | * will hold the pset. |
| 816 | */ |
| 817 | if (!is_pset_node(fwnode)) |
| 818 | fwnode = fwnode->secondary; |
| 819 | if (!IS_ERR(fwnode) && is_pset_node(fwnode)) |
| 820 | pset_free_set(to_pset_node(fwnode)); |
| 821 | set_secondary_fwnode(dev, NULL); |
| 822 | } |
| 823 | EXPORT_SYMBOL_GPL(device_remove_property_set); |
| 824 | |
| 825 | /** |
| 826 | * device_add_property_set - Add a collection of properties to a device object. |
| 827 | * @dev: Device to add properties to. |
| 828 | * @pset: Collection of properties to add. |
| 829 | * |
| 830 | * Associate a collection of device properties represented by @pset with @dev |
| 831 | * as its secondary firmware node. The function takes a copy of @pset. |
| 832 | */ |
| 833 | int device_add_property_set(struct device *dev, const struct property_set *pset) |
| 834 | { |
| 835 | struct property_set *p; |
| 836 | |
| 837 | if (!pset) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | p = pset_copy_set(pset); |
| 841 | if (IS_ERR(p)) |
| 842 | return PTR_ERR(p); |
| 843 | |
| 844 | p->fwnode.type = FWNODE_PDATA; |
| 845 | set_secondary_fwnode(dev, &p->fwnode); |
| 846 | return 0; |
| 847 | } |
| 848 | EXPORT_SYMBOL_GPL(device_add_property_set); |
| 849 | |
| 850 | /** |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 851 | * device_get_next_child_node - Return the next child node handle for a device |
| 852 | * @dev: Device to find the next child node for. |
| 853 | * @child: Handle to one of the device's child nodes or a null handle. |
| 854 | */ |
| 855 | struct fwnode_handle *device_get_next_child_node(struct device *dev, |
| 856 | struct fwnode_handle *child) |
| 857 | { |
| 858 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 859 | struct device_node *node; |
| 860 | |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 861 | node = of_get_next_available_child(dev->of_node, to_of_node(child)); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 862 | if (node) |
| 863 | return &node->fwnode; |
| 864 | } else if (IS_ENABLED(CONFIG_ACPI)) { |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame] | 865 | return acpi_get_next_subnode(dev, child); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 866 | } |
| 867 | return NULL; |
| 868 | } |
| 869 | EXPORT_SYMBOL_GPL(device_get_next_child_node); |
| 870 | |
| 871 | /** |
| 872 | * fwnode_handle_put - Drop reference to a device node |
| 873 | * @fwnode: Pointer to the device node to drop the reference to. |
| 874 | * |
| 875 | * This has to be used when terminating device_for_each_child_node() iteration |
| 876 | * with break or return to prevent stale device node references from being left |
| 877 | * behind. |
| 878 | */ |
| 879 | void fwnode_handle_put(struct fwnode_handle *fwnode) |
| 880 | { |
| 881 | if (is_of_node(fwnode)) |
Alexander Sverdlin | c181fb3 | 2015-06-22 22:38:53 +0200 | [diff] [blame] | 882 | of_node_put(to_of_node(fwnode)); |
Rafael J. Wysocki | 8a0662d | 2014-11-04 14:03:59 +0100 | [diff] [blame] | 883 | } |
| 884 | EXPORT_SYMBOL_GPL(fwnode_handle_put); |
| 885 | |
| 886 | /** |
| 887 | * device_get_child_node_count - return the number of child nodes for device |
| 888 | * @dev: Device to cound the child nodes for |
| 889 | */ |
| 890 | unsigned int device_get_child_node_count(struct device *dev) |
| 891 | { |
| 892 | struct fwnode_handle *child; |
| 893 | unsigned int count = 0; |
| 894 | |
| 895 | device_for_each_child_node(dev, child) |
| 896 | count++; |
| 897 | |
| 898 | return count; |
| 899 | } |
| 900 | EXPORT_SYMBOL_GPL(device_get_child_node_count); |
Suthikulpanit, Suravee | 05ca556 | 2015-06-10 11:08:54 -0500 | [diff] [blame] | 901 | |
Suthikulpanit, Suravee | e5e5586 | 2015-10-28 15:50:49 -0700 | [diff] [blame] | 902 | bool device_dma_supported(struct device *dev) |
| 903 | { |
| 904 | /* For DT, this is always supported. |
| 905 | * For ACPI, this depends on CCA, which |
| 906 | * is determined by the acpi_dma_supported(). |
| 907 | */ |
| 908 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) |
| 909 | return true; |
| 910 | |
| 911 | return acpi_dma_supported(ACPI_COMPANION(dev)); |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(device_dma_supported); |
| 914 | |
| 915 | enum dev_dma_attr device_get_dma_attr(struct device *dev) |
| 916 | { |
| 917 | enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED; |
| 918 | |
| 919 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 920 | if (of_dma_is_coherent(dev->of_node)) |
| 921 | attr = DEV_DMA_COHERENT; |
| 922 | else |
| 923 | attr = DEV_DMA_NON_COHERENT; |
| 924 | } else |
| 925 | attr = acpi_get_dma_attr(ACPI_COMPANION(dev)); |
| 926 | |
| 927 | return attr; |
| 928 | } |
| 929 | EXPORT_SYMBOL_GPL(device_get_dma_attr); |
| 930 | |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 931 | /** |
Jeremy Linton | 2f710a3 | 2015-08-19 11:46:42 -0500 | [diff] [blame] | 932 | * device_get_phy_mode - Get phy mode for given device |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 933 | * @dev: Pointer to the given device |
| 934 | * |
| 935 | * The function gets phy interface string from property 'phy-mode' or |
| 936 | * 'phy-connection-type', and return its index in phy_modes table, or errno in |
| 937 | * error case. |
| 938 | */ |
| 939 | int device_get_phy_mode(struct device *dev) |
| 940 | { |
| 941 | const char *pm; |
| 942 | int err, i; |
| 943 | |
| 944 | err = device_property_read_string(dev, "phy-mode", &pm); |
| 945 | if (err < 0) |
| 946 | err = device_property_read_string(dev, |
| 947 | "phy-connection-type", &pm); |
| 948 | if (err < 0) |
| 949 | return err; |
| 950 | |
| 951 | for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) |
| 952 | if (!strcasecmp(pm, phy_modes(i))) |
| 953 | return i; |
| 954 | |
| 955 | return -ENODEV; |
| 956 | } |
| 957 | EXPORT_SYMBOL_GPL(device_get_phy_mode); |
| 958 | |
| 959 | static void *device_get_mac_addr(struct device *dev, |
| 960 | const char *name, char *addr, |
| 961 | int alen) |
| 962 | { |
| 963 | int ret = device_property_read_u8_array(dev, name, addr, alen); |
| 964 | |
Jeremy Linton | 2f710a3 | 2015-08-19 11:46:42 -0500 | [diff] [blame] | 965 | if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr)) |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 966 | return addr; |
| 967 | return NULL; |
| 968 | } |
| 969 | |
| 970 | /** |
Jeremy Linton | 2f710a3 | 2015-08-19 11:46:42 -0500 | [diff] [blame] | 971 | * device_get_mac_address - Get the MAC for a given device |
| 972 | * @dev: Pointer to the device |
| 973 | * @addr: Address of buffer to store the MAC in |
| 974 | * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN |
| 975 | * |
| 976 | * Search the firmware node for the best MAC address to use. 'mac-address' is |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 977 | * checked first, because that is supposed to contain to "most recent" MAC |
| 978 | * address. If that isn't set, then 'local-mac-address' is checked next, |
| 979 | * because that is the default address. If that isn't set, then the obsolete |
| 980 | * 'address' is checked, just in case we're using an old device tree. |
| 981 | * |
| 982 | * Note that the 'address' property is supposed to contain a virtual address of |
| 983 | * the register set, but some DTS files have redefined that property to be the |
| 984 | * MAC address. |
| 985 | * |
| 986 | * All-zero MAC addresses are rejected, because those could be properties that |
Jeremy Linton | 2f710a3 | 2015-08-19 11:46:42 -0500 | [diff] [blame] | 987 | * exist in the firmware tables, but were not updated by the firmware. For |
| 988 | * example, the DTS could define 'mac-address' and 'local-mac-address', with |
| 989 | * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'. |
| 990 | * In this case, the real MAC is in 'local-mac-address', and 'mac-address' |
| 991 | * exists but is all zeros. |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 992 | */ |
| 993 | void *device_get_mac_address(struct device *dev, char *addr, int alen) |
| 994 | { |
Julien Grall | 5b902d6 | 2015-09-03 23:59:50 +0100 | [diff] [blame] | 995 | char *res; |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 996 | |
Julien Grall | 5b902d6 | 2015-09-03 23:59:50 +0100 | [diff] [blame] | 997 | res = device_get_mac_addr(dev, "mac-address", addr, alen); |
| 998 | if (res) |
| 999 | return res; |
| 1000 | |
| 1001 | res = device_get_mac_addr(dev, "local-mac-address", addr, alen); |
| 1002 | if (res) |
| 1003 | return res; |
Jeremy Linton | 4c96b7d | 2015-08-12 17:06:26 -0500 | [diff] [blame] | 1004 | |
| 1005 | return device_get_mac_addr(dev, "address", addr, alen); |
| 1006 | } |
| 1007 | EXPORT_SYMBOL(device_get_mac_address); |