Commit 69564dfe authored by Viresh Kumar's avatar Viresh Kumar Committed by Greg Kroah-Hartman

greybus: lights: convert to bundle driver

Convert the legacy lights protocol driver to a bundle driver.

This also fixes a potential crash should a (malicious) module have sent
an early request before the private data had been initialised.
Signed-off-by: default avatarViresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: default avatarJohan Hovold <johan@hovoldconsulting.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@google.com>
parent 6ce0eed7
...@@ -245,7 +245,6 @@ static const struct greybus_bundle_id legacy_id_table[] = { ...@@ -245,7 +245,6 @@ static const struct greybus_bundle_id legacy_id_table[] = {
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_PWM) }, { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_PWM) },
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_SPI) }, { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_SPI) },
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_CAMERA) }, { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_CAMERA) },
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
{ } { }
}; };
MODULE_DEVICE_TABLE(greybus, legacy_id_table); MODULE_DEVICE_TABLE(greybus, legacy_id_table);
......
...@@ -1149,7 +1149,7 @@ static int gb_lights_register_all(struct gb_lights *glights) ...@@ -1149,7 +1149,7 @@ static int gb_lights_register_all(struct gb_lights *glights)
return ret; return ret;
} }
static int gb_lights_event_recv(u8 type, struct gb_operation *op) static int gb_lights_request_handler(struct gb_operation *op)
{ {
struct gb_connection *connection = op->connection; struct gb_connection *connection = op->connection;
struct device *dev = &connection->bundle->dev; struct device *dev = &connection->bundle->dev;
...@@ -1161,8 +1161,8 @@ static int gb_lights_event_recv(u8 type, struct gb_operation *op) ...@@ -1161,8 +1161,8 @@ static int gb_lights_event_recv(u8 type, struct gb_operation *op)
u8 light_id; u8 light_id;
u8 event; u8 event;
if (type != GB_LIGHTS_TYPE_EVENT) { if (op->type != GB_LIGHTS_TYPE_EVENT) {
dev_err(dev, "Unsupported unsolicited event: %u\n", type); dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
return -EINVAL; return -EINVAL;
} }
...@@ -1201,57 +1201,95 @@ static int gb_lights_event_recv(u8 type, struct gb_operation *op) ...@@ -1201,57 +1201,95 @@ static int gb_lights_event_recv(u8 type, struct gb_operation *op)
return ret; return ret;
} }
static int gb_lights_connection_init(struct gb_connection *connection) static int gb_lights_probe(struct gb_bundle *bundle,
const struct greybus_bundle_id *id)
{ {
struct greybus_descriptor_cport *cport_desc;
struct gb_connection *connection;
struct gb_lights *glights; struct gb_lights *glights;
int ret; int ret;
if (bundle->num_cports != 1)
return -ENODEV;
cport_desc = &bundle->cport_desc[0];
if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
return -ENODEV;
glights = kzalloc(sizeof(*glights), GFP_KERNEL); glights = kzalloc(sizeof(*glights), GFP_KERNEL);
if (!glights) if (!glights)
return -ENOMEM; return -ENOMEM;
connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
gb_lights_request_handler);
if (IS_ERR(connection)) {
ret = PTR_ERR(connection);
goto out;
}
glights->connection = connection; glights->connection = connection;
connection->private = glights; connection->private = glights;
mutex_init(&glights->lights_lock); mutex_init(&glights->lights_lock);
greybus_set_drvdata(bundle, glights);
/* We aren't ready to receive an incoming request yet */
ret = gb_connection_enable_tx(connection);
if (ret)
goto error_connection_destroy;
/* /*
* Setup all the lights devices over this connection, if anything goes * Setup all the lights devices over this connection, if anything goes
* wrong tear down all lights * wrong tear down all lights
*/ */
ret = gb_lights_create_all(glights); ret = gb_lights_create_all(glights);
if (ret < 0) if (ret < 0)
goto out; goto error_connection_disable;
/* We are ready to receive an incoming request now, enable RX as well */
ret = gb_connection_enable(connection);
if (ret)
goto error_connection_disable;
/* Enable & register lights */ /* Enable & register lights */
ret = gb_lights_register_all(glights); ret = gb_lights_register_all(glights);
if (ret < 0) if (ret < 0)
goto out; goto error_connection_disable;
return 0; return 0;
error_connection_disable:
gb_connection_disable(connection);
error_connection_destroy:
gb_connection_destroy(connection);
out: out:
gb_lights_release(glights); gb_lights_release(glights);
return ret; return ret;
} }
static void gb_lights_connection_exit(struct gb_connection *connection) static void gb_lights_disconnect(struct gb_bundle *bundle)
{ {
struct gb_lights *glights = connection->private; struct gb_lights *glights = greybus_get_drvdata(bundle);
gb_connection_disable(glights->connection);
gb_connection_destroy(glights->connection);
gb_lights_release(glights); gb_lights_release(glights);
} }
static struct gb_protocol lights_protocol = { static const struct greybus_bundle_id gb_lights_id_table[] = {
.name = "lights", { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
.id = GREYBUS_PROTOCOL_LIGHTS, { }
.major = GB_LIGHTS_VERSION_MAJOR,
.minor = GB_LIGHTS_VERSION_MINOR,
.connection_init = gb_lights_connection_init,
.connection_exit = gb_lights_connection_exit,
.request_recv = gb_lights_event_recv,
}; };
MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
gb_protocol_driver(&lights_protocol); static struct greybus_driver gb_lights_driver = {
.name = "lights",
.probe = gb_lights_probe,
.disconnect = gb_lights_disconnect,
.id_table = gb_lights_id_table,
};
module_greybus_driver(gb_lights_driver);
MODULE_LICENSE("GPL v2"); MODULE_LICENSE("GPL v2");
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