Re: [RFCv4 09/21] v4l2: add request API support

2018-02-20 Thread Alexandre Courbot
On Tue, Feb 20, 2018 at 10:25 PM, Hans Verkuil  wrote:
> On 02/20/18 05:44, Alexandre Courbot wrote:
>> Add a v4l2 request entity data structure that takes care of storing the
>> request-related state of a V4L2 device ; in this case, its controls.
>>
>> Signed-off-by: Alexandre Courbot 
>> ---
>>  drivers/media/v4l2-core/Makefile   |   1 +
>>  drivers/media/v4l2-core/v4l2-request.c | 178 +
>>  include/media/v4l2-request.h   | 159 ++
>>  3 files changed, 338 insertions(+)
>>  create mode 100644 drivers/media/v4l2-core/v4l2-request.c
>>  create mode 100644 include/media/v4l2-request.h
>>
>> diff --git a/drivers/media/v4l2-core/Makefile 
>> b/drivers/media/v4l2-core/Makefile
>> index 80de2cb9c476..13d0477535bd 100644
>> --- a/drivers/media/v4l2-core/Makefile
>> +++ b/drivers/media/v4l2-core/Makefile
>> @@ -16,6 +16,7 @@ ifeq ($(CONFIG_TRACEPOINTS),y)
>>videodev-objs += vb2-trace.o v4l2-trace.o
>>  endif
>>  videodev-$(CONFIG_MEDIA_CONTROLLER) += v4l2-mc.o
>> +videodev-$(CONFIG_MEDIA_REQUEST_API) += v4l2-request.o
>>
>>  obj-$(CONFIG_VIDEO_V4L2) += videodev.o
>>  obj-$(CONFIG_VIDEO_V4L2) += v4l2-common.o
>> diff --git a/drivers/media/v4l2-core/v4l2-request.c 
>> b/drivers/media/v4l2-core/v4l2-request.c
>> new file mode 100644
>> index ..e8ad10e2f525
>> --- /dev/null
>> +++ b/drivers/media/v4l2-core/v4l2-request.c
>> @@ -0,0 +1,178 @@
>> +/*
>> + * Media requests support for V4L2
>> + *
>> + * Copyright (C) 2018, The Chromium OS Authors.  All rights reserved.
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License version 2 as
>> + * published by the Free Software Foundation.
>> + *
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> + */
>> +
>> +#include 
>> +
>> +#include 
>> +#include 
>> +#include 
>> +
>> +void v4l2_request_entity_init(struct v4l2_request_entity *entity,
>> +   const struct media_request_entity_ops *ops,
>> +   struct video_device *vdev)
>> +{
>> + media_request_entity_init(&entity->base, 
>> MEDIA_REQUEST_ENTITY_TYPE_V4L2, ops);
>> + entity->vdev = vdev;
>> +}
>> +EXPORT_SYMBOL_GPL(v4l2_request_entity_init);
>> +
>> +struct media_request_entity_data *
>> +v4l2_request_entity_data_alloc(struct media_request *req,
>> +struct v4l2_ctrl_handler *hdl)
>> +{
>> + struct v4l2_request_entity_data *data;
>> + int ret;
>> +
>> + data = kzalloc(sizeof(*data), GFP_KERNEL);
>> + if (!data)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + ret = v4l2_ctrl_request_init(&data->ctrls);
>> + if (ret) {
>> + kfree(data);
>> + return ERR_PTR(ret);
>> + }
>> + ret = v4l2_ctrl_request_clone(&data->ctrls, hdl, NULL);
>> + if (ret) {
>> + kfree(data);
>> + return ERR_PTR(ret);
>> + }
>> +
>> + INIT_LIST_HEAD(&data->queued_buffers);
>> +
>> + return &data->base;
>> +}
>> +EXPORT_SYMBOL_GPL(v4l2_request_entity_data_alloc);
>> +
>> +void v4l2_request_entity_data_free(struct media_request_entity_data *_data)
>> +{
>> + struct v4l2_request_entity_data *data;
>> + struct v4l2_vb2_request_buffer *qb, *n;
>> +
>> + data = to_v4l2_entity_data(_data);
>> +
>> + list_for_each_entry_safe(qb, n, &data->queued_buffers, node) {
>> + struct vb2_buffer *buf;
>> + dev_warn(_data->request->mgr->dev,
>> +  "entity data freed while buffer still queued!\n");
>> +
>> + /* give buffer back to user-space */
>> + buf = qb->queue->bufs[qb->v4l2_buf.index];
>> + buf->state = qb->pre_req_state;
>> + buf->request = NULL;
>> +
>> + kfree(qb);
>> + }
>> +
>> + v4l2_ctrl_handler_free(&data->ctrls);
>> + kfree(data);
>> +}
>> +EXPORT_SYMBOL_GPL(v4l2_request_entity_data_free);
>> +
>> +
>> +
>> +
>> +
>> +static struct media_request *v4l2_request_alloc(struct media_request_mgr 
>> *mgr)
>> +{
>> + struct media_request *req;
>> +
>> + req = kzalloc(sizeof(*req), GFP_KERNEL);
>> + if (!req)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + req->mgr = mgr;
>> + req->state = MEDIA_REQUEST_STATE_IDLE;
>> + INIT_LIST_HEAD(&req->data);
>> + init_waitqueue_head(&req->complete_wait);
>> + mutex_init(&req->lock);
>> +
>> + mutex_lock(&mgr->mutex);
>> + list_add_tail(&req->list, &mgr->requests);
>> + mutex_unlock(&mgr->mutex);
>> +
>> + return req;
>> +}
>> +
>> +static void v4l2_request_free(struct media_request *req)
>> +{
>> + struct media_request_mgr *mgr = req->mgr;
>> + struct media_request_entity_data *data, *next;
>> +

Re: [RFCv4 09/21] v4l2: add request API support

2018-02-20 Thread Hans Verkuil
On 02/20/18 05:44, Alexandre Courbot wrote:
> Add a v4l2 request entity data structure that takes care of storing the
> request-related state of a V4L2 device ; in this case, its controls.
> 
> Signed-off-by: Alexandre Courbot 
> ---
>  drivers/media/v4l2-core/Makefile   |   1 +
>  drivers/media/v4l2-core/v4l2-request.c | 178 +
>  include/media/v4l2-request.h   | 159 ++
>  3 files changed, 338 insertions(+)
>  create mode 100644 drivers/media/v4l2-core/v4l2-request.c
>  create mode 100644 include/media/v4l2-request.h
> 
> diff --git a/drivers/media/v4l2-core/Makefile 
> b/drivers/media/v4l2-core/Makefile
> index 80de2cb9c476..13d0477535bd 100644
> --- a/drivers/media/v4l2-core/Makefile
> +++ b/drivers/media/v4l2-core/Makefile
> @@ -16,6 +16,7 @@ ifeq ($(CONFIG_TRACEPOINTS),y)
>videodev-objs += vb2-trace.o v4l2-trace.o
>  endif
>  videodev-$(CONFIG_MEDIA_CONTROLLER) += v4l2-mc.o
> +videodev-$(CONFIG_MEDIA_REQUEST_API) += v4l2-request.o
>  
>  obj-$(CONFIG_VIDEO_V4L2) += videodev.o
>  obj-$(CONFIG_VIDEO_V4L2) += v4l2-common.o
> diff --git a/drivers/media/v4l2-core/v4l2-request.c 
> b/drivers/media/v4l2-core/v4l2-request.c
> new file mode 100644
> index ..e8ad10e2f525
> --- /dev/null
> +++ b/drivers/media/v4l2-core/v4l2-request.c
> @@ -0,0 +1,178 @@
> +/*
> + * Media requests support for V4L2
> + *
> + * Copyright (C) 2018, The Chromium OS Authors.  All rights reserved.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + */
> +
> +#include 
> +
> +#include 
> +#include 
> +#include 
> +
> +void v4l2_request_entity_init(struct v4l2_request_entity *entity,
> +   const struct media_request_entity_ops *ops,
> +   struct video_device *vdev)
> +{
> + media_request_entity_init(&entity->base, 
> MEDIA_REQUEST_ENTITY_TYPE_V4L2, ops);
> + entity->vdev = vdev;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_request_entity_init);
> +
> +struct media_request_entity_data *
> +v4l2_request_entity_data_alloc(struct media_request *req,
> +struct v4l2_ctrl_handler *hdl)
> +{
> + struct v4l2_request_entity_data *data;
> + int ret;
> +
> + data = kzalloc(sizeof(*data), GFP_KERNEL);
> + if (!data)
> + return ERR_PTR(-ENOMEM);
> +
> + ret = v4l2_ctrl_request_init(&data->ctrls);
> + if (ret) {
> + kfree(data);
> + return ERR_PTR(ret);
> + }
> + ret = v4l2_ctrl_request_clone(&data->ctrls, hdl, NULL);
> + if (ret) {
> + kfree(data);
> + return ERR_PTR(ret);
> + }
> +
> + INIT_LIST_HEAD(&data->queued_buffers);
> +
> + return &data->base;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_request_entity_data_alloc);
> +
> +void v4l2_request_entity_data_free(struct media_request_entity_data *_data)
> +{
> + struct v4l2_request_entity_data *data;
> + struct v4l2_vb2_request_buffer *qb, *n;
> +
> + data = to_v4l2_entity_data(_data);
> +
> + list_for_each_entry_safe(qb, n, &data->queued_buffers, node) {
> + struct vb2_buffer *buf;
> + dev_warn(_data->request->mgr->dev,
> +  "entity data freed while buffer still queued!\n");
> +
> + /* give buffer back to user-space */
> + buf = qb->queue->bufs[qb->v4l2_buf.index];
> + buf->state = qb->pre_req_state;
> + buf->request = NULL;
> +
> + kfree(qb);
> + }
> +
> + v4l2_ctrl_handler_free(&data->ctrls);
> + kfree(data);
> +}
> +EXPORT_SYMBOL_GPL(v4l2_request_entity_data_free);
> +
> +
> +
> +
> +
> +static struct media_request *v4l2_request_alloc(struct media_request_mgr 
> *mgr)
> +{
> + struct media_request *req;
> +
> + req = kzalloc(sizeof(*req), GFP_KERNEL);
> + if (!req)
> + return ERR_PTR(-ENOMEM);
> +
> + req->mgr = mgr;
> + req->state = MEDIA_REQUEST_STATE_IDLE;
> + INIT_LIST_HEAD(&req->data);
> + init_waitqueue_head(&req->complete_wait);
> + mutex_init(&req->lock);
> +
> + mutex_lock(&mgr->mutex);
> + list_add_tail(&req->list, &mgr->requests);
> + mutex_unlock(&mgr->mutex);
> +
> + return req;
> +}
> +
> +static void v4l2_request_free(struct media_request *req)
> +{
> + struct media_request_mgr *mgr = req->mgr;
> + struct media_request_entity_data *data, *next;
> +
> + mutex_lock(&mgr->mutex);
> + list_del(&req->list);
> + mutex_unlock(&mgr->mutex);
> +
> + list_for_each_entry_safe(data, next, &req->data, list) {
> + list_del(&data->

Re: [RFCv4 09/21] v4l2: add request API support

2018-02-20 Thread Alexandre Courbot
Hi Philippe,

On Tue, Feb 20, 2018 at 4:36 PM, Philippe Ombredanne
 wrote:
> On Tue, Feb 20, 2018 at 5:44 AM, Alexandre Courbot
>  wrote:
>> Add a v4l2 request entity data structure that takes care of storing the
>> request-related state of a V4L2 device ; in this case, its controls.
>>
>> Signed-off-by: Alexandre Courbot 
>
> 
>
>> --- /dev/null
>> +++ b/drivers/media/v4l2-core/v4l2-request.c
>> @@ -0,0 +1,178 @@
>> +/*
>> + * Media requests support for V4L2
>> + *
>> + * Copyright (C) 2018, The Chromium OS Authors.  All rights reserved.
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License version 2 as
>> + * published by the Free Software Foundation.
>> + *
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> + */
>
> Do you mind using SPDX tags per [1] rather that this fine but long
> legalese. (Here and in the whole patch series)
>
> [1] 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/license-rules.rst

I need to check what is the stance of Chromium OS regarding these, but
will gladly comply if possible.


Re: [RFCv4 09/21] v4l2: add request API support

2018-02-19 Thread Philippe Ombredanne
On Tue, Feb 20, 2018 at 5:44 AM, Alexandre Courbot
 wrote:
> Add a v4l2 request entity data structure that takes care of storing the
> request-related state of a V4L2 device ; in this case, its controls.
>
> Signed-off-by: Alexandre Courbot 



> --- /dev/null
> +++ b/drivers/media/v4l2-core/v4l2-request.c
> @@ -0,0 +1,178 @@
> +/*
> + * Media requests support for V4L2
> + *
> + * Copyright (C) 2018, The Chromium OS Authors.  All rights reserved.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + */

Do you mind using SPDX tags per [1] rather that this fine but long
legalese. (Here and in the whole patch series)

[1] 
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/license-rules.rst
-- 
Cordially
Philippe Ombredanne


[RFCv4 09/21] v4l2: add request API support

2018-02-19 Thread Alexandre Courbot
Add a v4l2 request entity data structure that takes care of storing the
request-related state of a V4L2 device ; in this case, its controls.

Signed-off-by: Alexandre Courbot 
---
 drivers/media/v4l2-core/Makefile   |   1 +
 drivers/media/v4l2-core/v4l2-request.c | 178 +
 include/media/v4l2-request.h   | 159 ++
 3 files changed, 338 insertions(+)
 create mode 100644 drivers/media/v4l2-core/v4l2-request.c
 create mode 100644 include/media/v4l2-request.h

diff --git a/drivers/media/v4l2-core/Makefile b/drivers/media/v4l2-core/Makefile
index 80de2cb9c476..13d0477535bd 100644
--- a/drivers/media/v4l2-core/Makefile
+++ b/drivers/media/v4l2-core/Makefile
@@ -16,6 +16,7 @@ ifeq ($(CONFIG_TRACEPOINTS),y)
   videodev-objs += vb2-trace.o v4l2-trace.o
 endif
 videodev-$(CONFIG_MEDIA_CONTROLLER) += v4l2-mc.o
+videodev-$(CONFIG_MEDIA_REQUEST_API) += v4l2-request.o
 
 obj-$(CONFIG_VIDEO_V4L2) += videodev.o
 obj-$(CONFIG_VIDEO_V4L2) += v4l2-common.o
diff --git a/drivers/media/v4l2-core/v4l2-request.c 
b/drivers/media/v4l2-core/v4l2-request.c
new file mode 100644
index ..e8ad10e2f525
--- /dev/null
+++ b/drivers/media/v4l2-core/v4l2-request.c
@@ -0,0 +1,178 @@
+/*
+ * Media requests support for V4L2
+ *
+ * Copyright (C) 2018, The Chromium OS Authors.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+
+void v4l2_request_entity_init(struct v4l2_request_entity *entity,
+ const struct media_request_entity_ops *ops,
+ struct video_device *vdev)
+{
+   media_request_entity_init(&entity->base, 
MEDIA_REQUEST_ENTITY_TYPE_V4L2, ops);
+   entity->vdev = vdev;
+}
+EXPORT_SYMBOL_GPL(v4l2_request_entity_init);
+
+struct media_request_entity_data *
+v4l2_request_entity_data_alloc(struct media_request *req,
+  struct v4l2_ctrl_handler *hdl)
+{
+   struct v4l2_request_entity_data *data;
+   int ret;
+
+   data = kzalloc(sizeof(*data), GFP_KERNEL);
+   if (!data)
+   return ERR_PTR(-ENOMEM);
+
+   ret = v4l2_ctrl_request_init(&data->ctrls);
+   if (ret) {
+   kfree(data);
+   return ERR_PTR(ret);
+   }
+   ret = v4l2_ctrl_request_clone(&data->ctrls, hdl, NULL);
+   if (ret) {
+   kfree(data);
+   return ERR_PTR(ret);
+   }
+
+   INIT_LIST_HEAD(&data->queued_buffers);
+
+   return &data->base;
+}
+EXPORT_SYMBOL_GPL(v4l2_request_entity_data_alloc);
+
+void v4l2_request_entity_data_free(struct media_request_entity_data *_data)
+{
+   struct v4l2_request_entity_data *data;
+   struct v4l2_vb2_request_buffer *qb, *n;
+
+   data = to_v4l2_entity_data(_data);
+
+   list_for_each_entry_safe(qb, n, &data->queued_buffers, node) {
+   struct vb2_buffer *buf;
+   dev_warn(_data->request->mgr->dev,
+"entity data freed while buffer still queued!\n");
+
+   /* give buffer back to user-space */
+   buf = qb->queue->bufs[qb->v4l2_buf.index];
+   buf->state = qb->pre_req_state;
+   buf->request = NULL;
+
+   kfree(qb);
+   }
+
+   v4l2_ctrl_handler_free(&data->ctrls);
+   kfree(data);
+}
+EXPORT_SYMBOL_GPL(v4l2_request_entity_data_free);
+
+
+
+
+
+static struct media_request *v4l2_request_alloc(struct media_request_mgr *mgr)
+{
+   struct media_request *req;
+
+   req = kzalloc(sizeof(*req), GFP_KERNEL);
+   if (!req)
+   return ERR_PTR(-ENOMEM);
+
+   req->mgr = mgr;
+   req->state = MEDIA_REQUEST_STATE_IDLE;
+   INIT_LIST_HEAD(&req->data);
+   init_waitqueue_head(&req->complete_wait);
+   mutex_init(&req->lock);
+
+   mutex_lock(&mgr->mutex);
+   list_add_tail(&req->list, &mgr->requests);
+   mutex_unlock(&mgr->mutex);
+
+   return req;
+}
+
+static void v4l2_request_free(struct media_request *req)
+{
+   struct media_request_mgr *mgr = req->mgr;
+   struct media_request_entity_data *data, *next;
+
+   mutex_lock(&mgr->mutex);
+   list_del(&req->list);
+   mutex_unlock(&mgr->mutex);
+
+   list_for_each_entry_safe(data, next, &req->data, list) {
+   list_del(&data->list);
+   data->entity->ops->data_free(data);
+   }
+
+   kfree(req);
+}
+
+static bool v4l2_entity_valid(const struct media_request *req,
+ const struct media_request_entity *_entity)
+{
+