Commit 17ca6770 authored by Evgeniy Borisov's avatar Evgeniy Borisov Committed by Greg Kroah-Hartman

greybus: camera-gb: Implement camera module reference counting as subject.

In explanation:

The main idea for implementing reference counting is to not block exit
until any other modules are in use. Camera responsibility is to handle
properly any additional calls after camera exit and that what this
patch is doing:

1. Free camera module when reference count is zero.
2. After camera cleanup handle properly any additional ongoing
   transaction. Return error if connection is destroyed.
Signed-off-by: default avatarEvgeniy Borisov <eborisov@mm-sol.com>
Reviewed-by: default avatarGjorgji Rosikopulos <grosikopulos@mm-sol.com>
Reviewed-by: default avatarLaurent Pinchart <laurent.pinchart@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@google.com>
parent 6c5ce637
...@@ -35,7 +35,9 @@ struct gb_camera_debugfs_buffer { ...@@ -35,7 +35,9 @@ struct gb_camera_debugfs_buffer {
/** /**
* struct gb_camera - A Greybus Camera Device * struct gb_camera - A Greybus Camera Device
* @connection: the greybus connection for camera control * @connection: the greybus connection for camera management
* @data_connection: the greybus connection for camera data
* @mutex: protects the connection field
* @debugfs: debugfs entries for camera protocol operations testing * @debugfs: debugfs entries for camera protocol operations testing
* @module: Greybus camera module registered to HOST processor. * @module: Greybus camera module registered to HOST processor.
*/ */
...@@ -43,6 +45,7 @@ struct gb_camera { ...@@ -43,6 +45,7 @@ struct gb_camera {
struct gb_bundle *bundle; struct gb_bundle *bundle;
struct gb_connection *connection; struct gb_connection *connection;
struct gb_connection *data_connection; struct gb_connection *data_connection;
struct mutex mutex;
struct { struct {
struct dentry *root; struct dentry *root;
...@@ -163,15 +166,24 @@ static int gb_camera_set_power_mode(struct gb_camera *gcam, bool hs) ...@@ -163,15 +166,24 @@ static int gb_camera_set_power_mode(struct gb_camera *gcam, bool hs)
static int gb_camera_capabilities(struct gb_camera *gcam, static int gb_camera_capabilities(struct gb_camera *gcam,
u8 *capabilities, size_t *size) u8 *capabilities, size_t *size)
{ {
struct gb_operation *op; struct gb_operation *op = NULL;
int ret; int ret;
mutex_lock(&gcam->mutex);
if (!gcam->connection) {
ret = -EINVAL;
goto done;
}
op = gb_operation_create_flags(gcam->connection, op = gb_operation_create_flags(gcam->connection,
GB_CAMERA_TYPE_CAPABILITIES, 0, *size, GB_CAMERA_TYPE_CAPABILITIES, 0, *size,
GB_OPERATION_FLAG_SHORT_RESPONSE, GB_OPERATION_FLAG_SHORT_RESPONSE,
GFP_KERNEL); GFP_KERNEL);
if (!op) if (!op) {
return -ENOMEM; ret = -ENOMEM;
goto done;
}
ret = gb_operation_request_send_sync(op); ret = gb_operation_request_send_sync(op);
if (ret) { if (ret) {
...@@ -183,6 +195,8 @@ static int gb_camera_capabilities(struct gb_camera *gcam, ...@@ -183,6 +195,8 @@ static int gb_camera_capabilities(struct gb_camera *gcam,
*size = op->response->payload_size; *size = op->response->payload_size;
done: done:
mutex_unlock(&gcam->mutex);
if (op)
gb_operation_put(op); gb_operation_put(op);
return ret; return ret;
} }
...@@ -222,8 +236,9 @@ static int gb_camera_configure_streams(struct gb_camera *gcam, ...@@ -222,8 +236,9 @@ static int gb_camera_configure_streams(struct gb_camera *gcam,
req = kmalloc(req_size, GFP_KERNEL); req = kmalloc(req_size, GFP_KERNEL);
resp = kmalloc(resp_size, GFP_KERNEL); resp = kmalloc(resp_size, GFP_KERNEL);
if (!req || !resp) { if (!req || !resp) {
ret = -ENOMEM; kfree(req);
goto done; kfree(resp);
return -ENOMEM;
} }
req->num_streams = nstreams; req->num_streams = nstreams;
...@@ -239,6 +254,13 @@ static int gb_camera_configure_streams(struct gb_camera *gcam, ...@@ -239,6 +254,13 @@ static int gb_camera_configure_streams(struct gb_camera *gcam,
cfg->padding = 0; cfg->padding = 0;
} }
mutex_lock(&gcam->mutex);
if (!gcam->connection) {
ret = -EINVAL;
goto done;
}
ret = gb_operation_sync(gcam->connection, ret = gb_operation_sync(gcam->connection,
GB_CAMERA_TYPE_CONFIGURE_STREAMS, GB_CAMERA_TYPE_CONFIGURE_STREAMS,
req, req_size, resp, resp_size); req, req_size, resp, resp_size);
...@@ -329,6 +351,7 @@ static int gb_camera_configure_streams(struct gb_camera *gcam, ...@@ -329,6 +351,7 @@ static int gb_camera_configure_streams(struct gb_camera *gcam,
ret = 0; ret = 0;
done: done:
mutex_unlock(&gcam->mutex);
kfree(req); kfree(req);
kfree(resp); kfree(resp);
return ret; return ret;
...@@ -356,8 +379,17 @@ static int gb_camera_capture(struct gb_camera *gcam, u32 request_id, ...@@ -356,8 +379,17 @@ static int gb_camera_capture(struct gb_camera *gcam, u32 request_id,
req->num_frames = cpu_to_le16(num_frames); req->num_frames = cpu_to_le16(num_frames);
memcpy(req->settings, settings, settings_size); memcpy(req->settings, settings, settings_size);
mutex_lock(&gcam->mutex);
if (!gcam->connection) {
ret = -EINVAL;
goto done;
}
ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_CAPTURE, ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_CAPTURE,
req, req_size, NULL, 0); req, req_size, NULL, 0);
done:
mutex_unlock(&gcam->mutex);
kfree(req); kfree(req);
...@@ -369,15 +401,26 @@ static int gb_camera_flush(struct gb_camera *gcam, u32 *request_id) ...@@ -369,15 +401,26 @@ static int gb_camera_flush(struct gb_camera *gcam, u32 *request_id)
struct gb_camera_flush_response resp; struct gb_camera_flush_response resp;
int ret; int ret;
mutex_lock(&gcam->mutex);
if (!gcam->connection) {
ret = -EINVAL;
goto done;
}
ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_FLUSH, NULL, 0, ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_FLUSH, NULL, 0,
&resp, sizeof(resp)); &resp, sizeof(resp));
if (ret < 0) if (ret < 0)
return ret; goto done;
if (request_id) if (request_id)
*request_id = le32_to_cpu(resp.request_id); *request_id = le32_to_cpu(resp.request_id);
return 0; done:
mutex_unlock(&gcam->mutex);
return ret;
} }
static int gb_camera_request_handler(struct gb_operation *op) static int gb_camera_request_handler(struct gb_operation *op)
...@@ -527,18 +570,6 @@ static const struct gb_camera_ops gb_cam_ops = { ...@@ -527,18 +570,6 @@ static const struct gb_camera_ops gb_cam_ops = {
.flush = gb_camera_op_flush, .flush = gb_camera_op_flush,
}; };
static int gb_camera_register_intf_ops(struct gb_camera *gcam)
{
gcam->module.priv = gcam;
gcam->module.ops = &gb_cam_ops;
return gb_camera_register(&gcam->module);
}
static int gb_camera_unregister_intf_ops(struct gb_camera *gcam)
{
return gb_camera_unregister(&gcam->module);
}
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* DebugFS * DebugFS
*/ */
...@@ -899,14 +930,23 @@ static void gb_camera_cleanup(struct gb_camera *gcam) ...@@ -899,14 +930,23 @@ static void gb_camera_cleanup(struct gb_camera *gcam)
if (gcam->data_connection) { if (gcam->data_connection) {
gb_connection_disable(gcam->data_connection); gb_connection_disable(gcam->data_connection);
gb_connection_destroy(gcam->data_connection); gb_connection_destroy(gcam->data_connection);
gcam->data_connection = NULL;
} }
mutex_lock(&gcam->mutex);
if (gcam->connection) { if (gcam->connection) {
gb_connection_disable(gcam->connection); gb_connection_disable(gcam->connection);
gb_connection_destroy(gcam->connection); gb_connection_destroy(gcam->connection);
gcam->connection = NULL;
} }
mutex_unlock(&gcam->mutex);
}
kfree(gcam); static void gb_camera_release_module(struct kref *ref)
{
struct gb_camera_module *cam_mod =
container_of(ref, struct gb_camera_module, refcount);
kfree(cam_mod->priv);
} }
static int gb_camera_probe(struct gb_bundle *bundle, static int gb_camera_probe(struct gb_bundle *bundle,
...@@ -964,6 +1004,8 @@ static int gb_camera_probe(struct gb_bundle *bundle, ...@@ -964,6 +1004,8 @@ static int gb_camera_probe(struct gb_bundle *bundle,
if (ret) if (ret)
goto error; goto error;
mutex_init(&gcam->mutex);
/* /*
* Create the data connection between the camera module data CPort and * Create the data connection between the camera module data CPort and
* APB CDSI1. The CDSI1 CPort ID is hardcoded by the ES2 bridge. * APB CDSI1. The CDSI1 CPort ID is hardcoded by the ES2 bridge.
...@@ -986,7 +1028,11 @@ static int gb_camera_probe(struct gb_bundle *bundle, ...@@ -986,7 +1028,11 @@ static int gb_camera_probe(struct gb_bundle *bundle,
if (ret < 0) if (ret < 0)
goto error; goto error;
ret = gb_camera_register_intf_ops(gcam); gcam->module.priv = gcam;
gcam->module.ops = &gb_cam_ops;
gcam->module.interface_id = gcam->connection->intf->interface_id;
gcam->module.release = gb_camera_release_module;
ret = gb_camera_register(&gcam->module);
if (ret < 0) if (ret < 0)
goto error; goto error;
...@@ -996,6 +1042,7 @@ static int gb_camera_probe(struct gb_bundle *bundle, ...@@ -996,6 +1042,7 @@ static int gb_camera_probe(struct gb_bundle *bundle,
error: error:
gb_camera_cleanup(gcam); gb_camera_cleanup(gcam);
kfree(gcam);
return ret; return ret;
} }
...@@ -1003,8 +1050,8 @@ static void gb_camera_disconnect(struct gb_bundle *bundle) ...@@ -1003,8 +1050,8 @@ static void gb_camera_disconnect(struct gb_bundle *bundle)
{ {
struct gb_camera *gcam = greybus_get_drvdata(bundle); struct gb_camera *gcam = greybus_get_drvdata(bundle);
gb_camera_unregister_intf_ops(gcam);
gb_camera_cleanup(gcam); gb_camera_cleanup(gcam);
gb_camera_unregister(&gcam->module);
} }
static const struct greybus_bundle_id gb_camera_id_table[] = { static const struct greybus_bundle_id gb_camera_id_table[] = {
......
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