Commit 3d545326 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

greybus: pass appropriate type to create function

Based on a patch from Alex Elder <elder@linaro.org>.

Alex's original description:

Every descriptor in a manifest is interpreted by greybus_new_module().
We call a function to do initialization based on descriptor's type.

Since we know the type of the descriptor at that point, we can pass
to the called function the actual sub-type it needs (i.e., the union
member associated with the type).  This allows those functions to
be slightly simplified, and more focused.

Also change some size variables to have size_t type, and simplify a
few spots further by using sizeof(object) in place of sizeof(type).
parent e82bef42
...@@ -237,53 +237,50 @@ static const struct greybus_module_id fake_gb_id = { ...@@ -237,53 +237,50 @@ static const struct greybus_module_id fake_gb_id = {
}; };
static int create_function(struct greybus_device *gdev, static int create_function(struct greybus_device *gdev,
struct greybus_descriptor *desc, int desc_size) struct greybus_descriptor_function *function,
size_t desc_size)
{ {
int header_size = sizeof(struct greybus_descriptor_function); if (desc_size != sizeof(*function)) {
dev_err(gdev->dev.parent, "invalid function header size %zu\n",
if (desc_size != header_size) {
dev_err(gdev->dev.parent, "invalid function header size %d\n",
desc_size); desc_size);
return -EINVAL; return -EINVAL;
} }
memcpy(&gdev->function, &desc->function, header_size); memcpy(&gdev->function, function, desc_size);
return 0; return 0;
} }
static int create_module_id(struct greybus_device *gdev, static int create_module_id(struct greybus_device *gdev,
struct greybus_descriptor *desc, int desc_size) struct greybus_descriptor_module_id *module_id,
size_t desc_size)
{ {
int header_size = sizeof(struct greybus_descriptor_module_id); if (desc_size != sizeof(*module_id)) {
dev_err(gdev->dev.parent, "invalid module header size %zu\n",
if (desc_size != header_size) {
dev_err(gdev->dev.parent, "invalid module header size %d\n",
desc_size); desc_size);
return -EINVAL; return -EINVAL;
} }
memcpy(&gdev->module_id, &desc->module_id, header_size); memcpy(&gdev->module_id, module_id, desc_size);
return 0; return 0;
} }
static int create_serial_number(struct greybus_device *gdev, static int create_serial_number(struct greybus_device *gdev,
struct greybus_descriptor *desc, int desc_size) struct greybus_descriptor_serial_number *serial_num,
size_t desc_size)
{ {
int header_size = sizeof(struct greybus_descriptor_serial_number); if (desc_size != sizeof(*serial_num)) {
dev_err(gdev->dev.parent, "invalid serial number header size %zu\n",
if (desc_size != header_size) {
dev_err(gdev->dev.parent, "invalid serial number header size %d\n",
desc_size); desc_size);
return -EINVAL; return -EINVAL;
} }
memcpy(&gdev->serial_number, &desc->serial_number, header_size); memcpy(&gdev->serial_number, serial_num, desc_size);
return 0; return 0;
} }
static int create_string(struct greybus_device *gdev, static int create_string(struct greybus_device *gdev,
struct greybus_descriptor *desc, int desc_size) struct greybus_descriptor_string *string,
size_t desc_size)
{ {
int string_size; int string_size;
struct gdev_string *string; struct gdev_string *gdev_string;
int header_size = sizeof(struct greybus_descriptor_string);
if ((gdev->num_strings + 1) >= MAX_STRINGS_PER_MODULE) { if ((gdev->num_strings + 1) >= MAX_STRINGS_PER_MODULE) {
dev_err(gdev->dev.parent, dev_err(gdev->dev.parent,
...@@ -291,53 +288,53 @@ static int create_string(struct greybus_device *gdev, ...@@ -291,53 +288,53 @@ static int create_string(struct greybus_device *gdev,
return -EINVAL; return -EINVAL;
} }
if (desc_size < header_size) { if (desc_size < sizeof(*string)) {
dev_err(gdev->dev.parent, "invalid string header size %d\n", dev_err(gdev->dev.parent, "invalid string header size %zu\n",
desc_size); desc_size);
return -EINVAL; return -EINVAL;
} }
string_size = le16_to_cpu(desc->string.length); string_size = le16_to_cpu(string->length);
string = kzalloc(sizeof(*string) + string_size + 1, GFP_KERNEL); gdev_string = kzalloc(sizeof(*gdev_string) + string_size + 1, GFP_KERNEL);
if (!string) if (!gdev_string)
return -ENOMEM; return -ENOMEM;
string->length = string_size; gdev_string->length = string_size;
string->id = desc->string.id; gdev_string->id = string->id;
memcpy(&string->string, &desc->string.string, string_size); memcpy(&gdev_string->string, &string->string, string_size);
gdev->string[gdev->num_strings] = string; gdev->string[gdev->num_strings] = gdev_string;
gdev->num_strings++; gdev->num_strings++;
return 0; return 0;
} }
static int create_cport(struct greybus_device *gdev, static int create_cport(struct greybus_device *gdev,
struct greybus_descriptor *desc, int desc_size) struct greybus_descriptor_cport *cport,
size_t desc_size)
{ {
struct gdev_cport *cport; struct gdev_cport *gdev_cport;
int header_size = sizeof(struct greybus_descriptor_cport);
if ((gdev->num_cports + 1) >= MAX_CPORTS_PER_MODULE) { if ((gdev->num_cports + 1) >= MAX_CPORTS_PER_MODULE) {
dev_err(gdev->dev.parent, "too many cports for this module!\n"); dev_err(gdev->dev.parent, "too many cports for this module!\n");
return -EINVAL; return -EINVAL;
} }
if (desc_size != header_size) { if (desc_size != sizeof(*cport)) {
dev_err(gdev->dev.parent, dev_err(gdev->dev.parent,
"invalid serial number header size %d\n", desc_size); "invalid serial number header size %zu\n", desc_size);
return -EINVAL; return -EINVAL;
} }
cport = kzalloc(sizeof(*cport), GFP_KERNEL); gdev_cport = kzalloc(sizeof(*gdev_cport), GFP_KERNEL);
if (!cport) if (!gdev_cport)
return -ENOMEM; return -ENOMEM;
cport->number = le16_to_cpu(desc->cport.number); gdev_cport->number = le16_to_cpu(cport->number);
cport->size = le16_to_cpu(desc->cport.size); gdev_cport->size = le16_to_cpu(cport->size);
cport->speed = desc->cport.speed; gdev_cport->speed = cport->speed;
gdev->cport[gdev->num_cports] = cport; gdev->cport[gdev->num_cports] = gdev_cport;
gdev->num_cports++; gdev->num_cports++;
return 0; return 0;
...@@ -412,23 +409,27 @@ struct greybus_device *greybus_new_module(struct device *parent, ...@@ -412,23 +409,27 @@ struct greybus_device *greybus_new_module(struct device *parent,
switch (le16_to_cpu(desc->header.type)) { switch (le16_to_cpu(desc->header.type)) {
case GREYBUS_TYPE_FUNCTION: case GREYBUS_TYPE_FUNCTION:
retval = create_function(gdev, desc, data_size); retval = create_function(gdev, &desc->function,
data_size);
break; break;
case GREYBUS_TYPE_MODULE_ID: case GREYBUS_TYPE_MODULE_ID:
retval = create_module_id(gdev, desc, data_size); retval = create_module_id(gdev, &desc->module_id,
data_size);
break; break;
case GREYBUS_TYPE_SERIAL_NUMBER: case GREYBUS_TYPE_SERIAL_NUMBER:
retval = create_serial_number(gdev, desc, data_size); retval = create_serial_number(gdev,
&desc->serial_number,
data_size);
break; break;
case GREYBUS_TYPE_STRING: case GREYBUS_TYPE_STRING:
retval = create_string(gdev, desc, data_size); retval = create_string(gdev, &desc->string, data_size);
break; break;
case GREYBUS_TYPE_CPORT: case GREYBUS_TYPE_CPORT:
retval = create_cport(gdev, desc, data_size); retval = create_cport(gdev, &desc->cport, data_size);
break; break;
case GREYBUS_TYPE_INVALID: case GREYBUS_TYPE_INVALID:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment