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