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