提交 786856b6 编写于 作者: L Linus Torvalds

Merge tag 'device-properties-4.11-rc1' of...

Merge tag 'device-properties-4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm

Pull device property updates from Rafael J. Wysocki:
 "Generic device properties framework updates for v4.11-rc1

  Allow built-in (static) device properties to be declared as constant,
  make it possible to save memory by discarding alternative (but unused)
  built-in (static) property sets and add support for automatic handling
  of built-in properties to the I2C code"

* tag 'device-properties-4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm:
  i2c: allow specify device properties in i2c_board_info
  device property: export code duplicating array of property entries
  device property: constify property arrays values
  device property: allow to constify properties
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
struct property_set { struct property_set {
struct fwnode_handle fwnode; struct fwnode_handle fwnode;
struct property_entry *properties; const struct property_entry *properties;
}; };
static inline bool is_pset_node(struct fwnode_handle *fwnode) static inline bool is_pset_node(struct fwnode_handle *fwnode)
...@@ -35,10 +35,10 @@ static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode) ...@@ -35,10 +35,10 @@ static inline struct property_set *to_pset_node(struct fwnode_handle *fwnode)
container_of(fwnode, struct property_set, fwnode) : NULL; container_of(fwnode, struct property_set, fwnode) : NULL;
} }
static struct property_entry *pset_prop_get(struct property_set *pset, static const struct property_entry *pset_prop_get(struct property_set *pset,
const char *name) const char *name)
{ {
struct property_entry *prop; const struct property_entry *prop;
if (!pset || !pset->properties) if (!pset || !pset->properties)
return NULL; return NULL;
...@@ -50,11 +50,11 @@ static struct property_entry *pset_prop_get(struct property_set *pset, ...@@ -50,11 +50,11 @@ static struct property_entry *pset_prop_get(struct property_set *pset,
return NULL; return NULL;
} }
static void *pset_prop_find(struct property_set *pset, const char *propname, static const void *pset_prop_find(struct property_set *pset,
size_t length) const char *propname, size_t length)
{ {
struct property_entry *prop; const struct property_entry *prop;
void *pointer; const void *pointer;
prop = pset_prop_get(pset, propname); prop = pset_prop_get(pset, propname);
if (!prop) if (!prop)
...@@ -74,7 +74,7 @@ static int pset_prop_read_u8_array(struct property_set *pset, ...@@ -74,7 +74,7 @@ static int pset_prop_read_u8_array(struct property_set *pset,
const char *propname, const char *propname,
u8 *values, size_t nval) u8 *values, size_t nval)
{ {
void *pointer; const void *pointer;
size_t length = nval * sizeof(*values); size_t length = nval * sizeof(*values);
pointer = pset_prop_find(pset, propname, length); pointer = pset_prop_find(pset, propname, length);
...@@ -89,7 +89,7 @@ static int pset_prop_read_u16_array(struct property_set *pset, ...@@ -89,7 +89,7 @@ static int pset_prop_read_u16_array(struct property_set *pset,
const char *propname, const char *propname,
u16 *values, size_t nval) u16 *values, size_t nval)
{ {
void *pointer; const void *pointer;
size_t length = nval * sizeof(*values); size_t length = nval * sizeof(*values);
pointer = pset_prop_find(pset, propname, length); pointer = pset_prop_find(pset, propname, length);
...@@ -104,7 +104,7 @@ static int pset_prop_read_u32_array(struct property_set *pset, ...@@ -104,7 +104,7 @@ static int pset_prop_read_u32_array(struct property_set *pset,
const char *propname, const char *propname,
u32 *values, size_t nval) u32 *values, size_t nval)
{ {
void *pointer; const void *pointer;
size_t length = nval * sizeof(*values); size_t length = nval * sizeof(*values);
pointer = pset_prop_find(pset, propname, length); pointer = pset_prop_find(pset, propname, length);
...@@ -119,7 +119,7 @@ static int pset_prop_read_u64_array(struct property_set *pset, ...@@ -119,7 +119,7 @@ static int pset_prop_read_u64_array(struct property_set *pset,
const char *propname, const char *propname,
u64 *values, size_t nval) u64 *values, size_t nval)
{ {
void *pointer; const void *pointer;
size_t length = nval * sizeof(*values); size_t length = nval * sizeof(*values);
pointer = pset_prop_find(pset, propname, length); pointer = pset_prop_find(pset, propname, length);
...@@ -133,7 +133,7 @@ static int pset_prop_read_u64_array(struct property_set *pset, ...@@ -133,7 +133,7 @@ static int pset_prop_read_u64_array(struct property_set *pset,
static int pset_prop_count_elems_of_size(struct property_set *pset, static int pset_prop_count_elems_of_size(struct property_set *pset,
const char *propname, size_t length) const char *propname, size_t length)
{ {
struct property_entry *prop; const struct property_entry *prop;
prop = pset_prop_get(pset, propname); prop = pset_prop_get(pset, propname);
if (!prop) if (!prop)
...@@ -146,7 +146,7 @@ static int pset_prop_read_string_array(struct property_set *pset, ...@@ -146,7 +146,7 @@ static int pset_prop_read_string_array(struct property_set *pset,
const char *propname, const char *propname,
const char **strings, size_t nval) const char **strings, size_t nval)
{ {
void *pointer; const void *pointer;
size_t length = nval * sizeof(*strings); size_t length = nval * sizeof(*strings);
pointer = pset_prop_find(pset, propname, length); pointer = pset_prop_find(pset, propname, length);
...@@ -160,8 +160,8 @@ static int pset_prop_read_string_array(struct property_set *pset, ...@@ -160,8 +160,8 @@ static int pset_prop_read_string_array(struct property_set *pset,
static int pset_prop_read_string(struct property_set *pset, static int pset_prop_read_string(struct property_set *pset,
const char *propname, const char **strings) const char *propname, const char **strings)
{ {
struct property_entry *prop; const struct property_entry *prop;
const char **pointer; const char * const *pointer;
prop = pset_prop_get(pset, propname); prop = pset_prop_get(pset, propname);
if (!prop) if (!prop)
...@@ -682,77 +682,64 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode, ...@@ -682,77 +682,64 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode,
} }
EXPORT_SYMBOL_GPL(fwnode_property_match_string); EXPORT_SYMBOL_GPL(fwnode_property_match_string);
/** static int property_copy_string_array(struct property_entry *dst,
* pset_free_set - releases memory allocated for copied property set const struct property_entry *src)
* @pset: Property set to release
*
* Function takes previously copied property set and releases all the
* memory allocated to it.
*/
static void pset_free_set(struct property_set *pset)
{ {
const struct property_entry *prop; char **d;
size_t i, nval; size_t nval = src->length / sizeof(*d);
int i;
if (!pset) d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
return; if (!d)
return -ENOMEM;
for (prop = pset->properties; prop->name; prop++) { for (i = 0; i < nval; i++) {
if (prop->is_array) { d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
if (prop->is_string && prop->pointer.str) { if (!d[i] && src->pointer.str[i]) {
nval = prop->length / sizeof(const char *); while (--i >= 0)
for (i = 0; i < nval; i++) kfree(d[i]);
kfree(prop->pointer.str[i]); kfree(d);
} return -ENOMEM;
kfree(prop->pointer.raw_data);
} else if (prop->is_string) {
kfree(prop->value.str);
} }
kfree(prop->name);
} }
kfree(pset->properties); dst->pointer.raw_data = d;
kfree(pset); return 0;
} }
static int pset_copy_entry(struct property_entry *dst, static int property_entry_copy_data(struct property_entry *dst,
const struct property_entry *src) const struct property_entry *src)
{ {
const char **d, **s; int error;
size_t i, nval;
dst->name = kstrdup(src->name, GFP_KERNEL); dst->name = kstrdup(src->name, GFP_KERNEL);
if (!dst->name) if (!dst->name)
return -ENOMEM; return -ENOMEM;
if (src->is_array) { if (src->is_array) {
if (!src->length) if (!src->length) {
return -ENODATA; error = -ENODATA;
goto out_free_name;
}
if (src->is_string) { if (src->is_string) {
nval = src->length / sizeof(const char *); error = property_copy_string_array(dst, src);
dst->pointer.str = kcalloc(nval, sizeof(const char *), if (error)
GFP_KERNEL); goto out_free_name;
if (!dst->pointer.str)
return -ENOMEM;
d = dst->pointer.str;
s = src->pointer.str;
for (i = 0; i < nval; i++) {
d[i] = kstrdup(s[i], GFP_KERNEL);
if (!d[i] && s[i])
return -ENOMEM;
}
} else { } else {
dst->pointer.raw_data = kmemdup(src->pointer.raw_data, dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
src->length, GFP_KERNEL); src->length, GFP_KERNEL);
if (!dst->pointer.raw_data) if (!dst->pointer.raw_data) {
return -ENOMEM; error = -ENOMEM;
goto out_free_name;
}
} }
} else if (src->is_string) { } else if (src->is_string) {
dst->value.str = kstrdup(src->value.str, GFP_KERNEL); dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
if (!dst->value.str && src->value.str) if (!dst->value.str && src->value.str) {
return -ENOMEM; error = -ENOMEM;
goto out_free_name;
}
} else { } else {
dst->value.raw_data = src->value.raw_data; dst->value.raw_data = src->value.raw_data;
} }
...@@ -762,6 +749,95 @@ static int pset_copy_entry(struct property_entry *dst, ...@@ -762,6 +749,95 @@ static int pset_copy_entry(struct property_entry *dst,
dst->is_string = src->is_string; dst->is_string = src->is_string;
return 0; return 0;
out_free_name:
kfree(dst->name);
return error;
}
static void property_entry_free_data(const struct property_entry *p)
{
size_t i, nval;
if (p->is_array) {
if (p->is_string && p->pointer.str) {
nval = p->length / sizeof(const char *);
for (i = 0; i < nval; i++)
kfree(p->pointer.str[i]);
}
kfree(p->pointer.raw_data);
} else if (p->is_string) {
kfree(p->value.str);
}
kfree(p->name);
}
/**
* property_entries_dup - duplicate array of properties
* @properties: array of properties to copy
*
* This function creates a deep copy of the given NULL-terminated array
* of property entries.
*/
struct property_entry *
property_entries_dup(const struct property_entry *properties)
{
struct property_entry *p;
int i, n = 0;
while (properties[n].name)
n++;
p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
if (!p)
return ERR_PTR(-ENOMEM);
for (i = 0; i < n; i++) {
int ret = property_entry_copy_data(&p[i], &properties[i]);
if (ret) {
while (--i >= 0)
property_entry_free_data(&p[i]);
kfree(p);
return ERR_PTR(ret);
}
}
return p;
}
EXPORT_SYMBOL_GPL(property_entries_dup);
/**
* property_entries_free - free previously allocated array of properties
* @properties: array of properties to destroy
*
* This function frees given NULL-terminated array of property entries,
* along with their data.
*/
void property_entries_free(const struct property_entry *properties)
{
const struct property_entry *p;
for (p = properties; p->name; p++)
property_entry_free_data(p);
kfree(properties);
}
EXPORT_SYMBOL_GPL(property_entries_free);
/**
* pset_free_set - releases memory allocated for copied property set
* @pset: Property set to release
*
* Function takes previously copied property set and releases all the
* memory allocated to it.
*/
static void pset_free_set(struct property_set *pset)
{
if (!pset)
return;
property_entries_free(pset->properties);
kfree(pset);
} }
/** /**
...@@ -776,32 +852,20 @@ static int pset_copy_entry(struct property_entry *dst, ...@@ -776,32 +852,20 @@ static int pset_copy_entry(struct property_entry *dst,
*/ */
static struct property_set *pset_copy_set(const struct property_set *pset) static struct property_set *pset_copy_set(const struct property_set *pset)
{ {
const struct property_entry *entry; struct property_entry *properties;
struct property_set *p; struct property_set *p;
size_t i, n = 0;
p = kzalloc(sizeof(*p), GFP_KERNEL); p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p) if (!p)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
while (pset->properties[n].name) properties = property_entries_dup(pset->properties);
n++; if (IS_ERR(properties)) {
p->properties = kcalloc(n + 1, sizeof(*entry), GFP_KERNEL);
if (!p->properties) {
kfree(p); kfree(p);
return ERR_PTR(-ENOMEM); return ERR_CAST(properties);
}
for (i = 0; i < n; i++) {
int ret = pset_copy_entry(&p->properties[i],
&pset->properties[i]);
if (ret) {
pset_free_set(p);
return ERR_PTR(ret);
}
} }
p->properties = properties;
return p; return p;
} }
...@@ -847,7 +911,8 @@ EXPORT_SYMBOL_GPL(device_remove_properties); ...@@ -847,7 +911,8 @@ EXPORT_SYMBOL_GPL(device_remove_properties);
* @dev as its secondary firmware node. The function takes a copy of * @dev as its secondary firmware node. The function takes a copy of
* @properties. * @properties.
*/ */
int device_add_properties(struct device *dev, struct property_entry *properties) int device_add_properties(struct device *dev,
const struct property_entry *properties)
{ {
struct property_set *p, pset; struct property_set *p, pset;
......
...@@ -1336,15 +1336,29 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) ...@@ -1336,15 +1336,29 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
client->dev.fwnode = info->fwnode; client->dev.fwnode = info->fwnode;
i2c_dev_set_name(adap, client); i2c_dev_set_name(adap, client);
if (info->properties) {
status = device_add_properties(&client->dev, info->properties);
if (status) {
dev_err(&adap->dev,
"Failed to add properties to client %s: %d\n",
client->name, status);
goto out_err;
}
}
status = device_register(&client->dev); status = device_register(&client->dev);
if (status) if (status)
goto out_err; goto out_free_props;
dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
client->name, dev_name(&client->dev)); client->name, dev_name(&client->dev));
return client; return client;
out_free_props:
if (info->properties)
device_remove_properties(&client->dev);
out_err: out_err:
dev_err(&adap->dev, dev_err(&adap->dev,
"Failed to register i2c client %s at 0x%02x (%d)\n", "Failed to register i2c client %s at 0x%02x (%d)\n",
......
...@@ -51,6 +51,7 @@ enum i2c_slave_event; ...@@ -51,6 +51,7 @@ enum i2c_slave_event;
typedef int (*i2c_slave_cb_t)(struct i2c_client *, enum i2c_slave_event, u8 *); typedef int (*i2c_slave_cb_t)(struct i2c_client *, enum i2c_slave_event, u8 *);
struct module; struct module;
struct property_entry;
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
/* /*
...@@ -299,6 +300,7 @@ static inline int i2c_slave_event(struct i2c_client *client, ...@@ -299,6 +300,7 @@ static inline int i2c_slave_event(struct i2c_client *client,
* @archdata: copied into i2c_client.dev.archdata * @archdata: copied into i2c_client.dev.archdata
* @of_node: pointer to OpenFirmware device node * @of_node: pointer to OpenFirmware device node
* @fwnode: device node supplied by the platform firmware * @fwnode: device node supplied by the platform firmware
* @properties: additional device properties for the device
* @irq: stored in i2c_client.irq * @irq: stored in i2c_client.irq
* *
* I2C doesn't actually support hardware probing, although controllers and * I2C doesn't actually support hardware probing, although controllers and
...@@ -320,6 +322,7 @@ struct i2c_board_info { ...@@ -320,6 +322,7 @@ struct i2c_board_info {
struct dev_archdata *archdata; struct dev_archdata *archdata;
struct device_node *of_node; struct device_node *of_node;
struct fwnode_handle *fwnode; struct fwnode_handle *fwnode;
const struct property_entry *properties;
int irq; int irq;
}; };
......
...@@ -160,12 +160,12 @@ struct property_entry { ...@@ -160,12 +160,12 @@ struct property_entry {
bool is_string; bool is_string;
union { union {
union { union {
void *raw_data; const void *raw_data;
u8 *u8_data; const u8 *u8_data;
u16 *u16_data; const u16 *u16_data;
u32 *u32_data; const u32 *u32_data;
u64 *u64_data; const u64 *u64_data;
const char **str; const char * const *str;
} pointer; } pointer;
union { union {
unsigned long long raw_data; unsigned long long raw_data;
...@@ -241,8 +241,13 @@ struct property_entry { ...@@ -241,8 +241,13 @@ struct property_entry {
.name = _name_, \ .name = _name_, \
} }
struct property_entry *
property_entries_dup(const struct property_entry *properties);
void property_entries_free(const struct property_entry *properties);
int device_add_properties(struct device *dev, int device_add_properties(struct device *dev,
struct property_entry *properties); const struct property_entry *properties);
void device_remove_properties(struct device *dev); void device_remove_properties(struct device *dev);
bool device_dma_supported(struct device *dev); bool device_dma_supported(struct device *dev);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册