[PATCH v2.1 1/2] V4L: Add memory-to-memory device helper framework for V4L2.

2009-12-23 Thread Pawel Osciak
A mem-to-mem device is a device that uses memory buffers passed by
userspace applications for both source and destination data. This is
different from existing drivers, which use memory buffers for only one
of those at once.

In terms of V4L2 such a device would be both of OUTPUT and CAPTURE type.
Although no such devices are present in the V4L2 framework, a demand for such
a model exists, e.g. for 'resizer devices'.

This patch also adds a separate kconfig submenu for mem-to-mem V4L devices.

Signed-off-by: Pawel Osciak p.osc...@samsung.com
Signed-off-by: Marek Szyprowski m.szyprow...@samsung.com
Reviewed-by: Kyungmin Park kyungmin.p...@samsung.com
---
 drivers/media/video/Kconfig|   14 +
 drivers/media/video/Makefile   |2 +
 drivers/media/video/v4l2-mem2mem.c |  671 
 include/media/v4l2-mem2mem.h   |  153 
 4 files changed, 840 insertions(+), 0 deletions(-)
 create mode 100644 drivers/media/video/v4l2-mem2mem.c
 create mode 100644 include/media/v4l2-mem2mem.h

diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index 2f83be7..4e97dcf 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -45,6 +45,10 @@ config VIDEO_TUNER
tristate
depends on MEDIA_TUNER
 
+config V4L2_MEM2MEM_DEV
+   tristate
+   depends on VIDEOBUF_GEN
+
 #
 # Multimedia Video device configuration
 #
@@ -1075,3 +1079,13 @@ config USB_S2255
 
 endif # V4L_USB_DRIVERS
 endif # VIDEO_CAPTURE_DRIVERS
+
+menuconfig V4L_MEM2MEM_DRIVERS
+   bool Memory-to-memory multimedia devices
+   depends on VIDEO_V4L2
+   default n
+   ---help---
+ Say Y here to enable selecting drivers for V4L devices that
+ use system memory for both source and destination buffers, as opposed
+ to capture and output drivers, which use memory buffers for just
+ one of those.
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index 2af68ee..9fe7d40 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -115,6 +115,8 @@ obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o
 obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o
 obj-$(CONFIG_VIDEO_BTCX)  += btcx-risc.o
 
+obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
+
 obj-$(CONFIG_VIDEO_M32R_AR_M64278) += arv.o
 
 obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o
diff --git a/drivers/media/video/v4l2-mem2mem.c 
b/drivers/media/video/v4l2-mem2mem.c
new file mode 100644
index 000..417ee2c
--- /dev/null
+++ b/drivers/media/video/v4l2-mem2mem.c
@@ -0,0 +1,671 @@
+/*
+ * Memory-to-memory device framework for Video for Linux 2.
+ *
+ * Helper functions for devices that use memory buffers for both source
+ * and destination.
+ *
+ * Copyright (c) 2009 Samsung Electronics Co., Ltd.
+ * Pawel Osciak, p.osc...@samsung.com
+ * Marek Szyprowski, m.szyprow...@samsung.com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version
+ */
+
+#include linux/module.h
+#include linux/sched.h
+#include media/videobuf-core.h
+#include media/v4l2-mem2mem.h
+
+MODULE_DESCRIPTION(Mem to mem device framework for V4L2);
+MODULE_AUTHOR(Pawel Osciak, p.osc...@samsung.com);
+MODULE_LICENSE(GPL);
+
+static int debug;
+module_param(debug, int, 0644);
+
+#define dprintk(fmt, arg...) do {\
+   if (debug = 1)\
+   printk(KERN_DEBUG %s:  fmt, __func__, ## arg); } while (0)
+
+
+/* The instance is already queued on the jobqueue */
+#define TRANS_QUEUED   (1  0)
+/* The instance is currently running in hardware */
+#define TRANS_RUNNING  (1  1)
+
+
+/* Offset base for buffers on the destination queue - used to distinguish
+ * between source and destination buffers when mmapping - they receive the same
+ * offsets but for different queues */
+#define DST_QUEUE_OFF_BASE (TASK_SIZE / 2)
+
+
+struct v4l2_m2m_dev {
+   /* Currently running instance */
+   struct v4l2_m2m_ctx *curr_ctx;
+   /* Instances queued to run */
+   struct list_headjobqueue;
+   spinlock_t  job_spinlock;
+
+   struct v4l2_m2m_ops *m2m_ops;
+};
+
+static inline
+struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
+enum v4l2_buf_type type)
+{
+   switch (type) {
+   case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+   return m2m_ctx-cap_q_ctx;
+   case V4L2_BUF_TYPE_VIDEO_OUTPUT:
+   return m2m_ctx-out_q_ctx;
+   default:
+   printk(KERN_ERR Invalid buffer type\n);
+   return NULL;
+   }
+}
+
+/**
+ * v4l2_m2m_get_vq() - return videobuf_queue for the given type
+ */
+struct videobuf_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
+  enum v4l2_buf_type 

Re: [PATCH v2.1 1/2] V4L: Add memory-to-memory device helper framework for V4L2.

2009-12-23 Thread Andy Walls
On Wed, 2009-12-23 at 14:17 +0100, Pawel Osciak wrote:
 A mem-to-mem device is a device that uses memory buffers passed by
 userspace applications for both source and destination data. This is
 different from existing drivers, which use memory buffers for only one
 of those at once.
 
 In terms of V4L2 such a device would be both of OUTPUT and CAPTURE type.
 Although no such devices are present in the V4L2 framework, a demand for such
 a model exists, e.g. for 'resizer devices'.
 
 This patch also adds a separate kconfig submenu for mem-to-mem V4L devices.
 
 Signed-off-by: Pawel Osciak p.osc...@samsung.com
 Signed-off-by: Marek Szyprowski m.szyprow...@samsung.com
 Reviewed-by: Kyungmin Park kyungmin.p...@samsung.com

Pawel,

I did find a few things that I want to mention.  (If you think I'm wrong
on something feel free to say so, I was interrupted several times when
lpoking things over.)


 ---
  drivers/media/video/Kconfig|   14 +
  drivers/media/video/Makefile   |2 +
  drivers/media/video/v4l2-mem2mem.c |  671 
 
  include/media/v4l2-mem2mem.h   |  153 
  4 files changed, 840 insertions(+), 0 deletions(-)
  create mode 100644 drivers/media/video/v4l2-mem2mem.c
  create mode 100644 include/media/v4l2-mem2mem.h
 
 diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
 index 2f83be7..4e97dcf 100644
 --- a/drivers/media/video/Kconfig
 +++ b/drivers/media/video/Kconfig
 @@ -45,6 +45,10 @@ config VIDEO_TUNER
   tristate
   depends on MEDIA_TUNER
  
 +config V4L2_MEM2MEM_DEV
 + tristate
 + depends on VIDEOBUF_GEN
 +
  #
  # Multimedia Video device configuration
  #
 @@ -1075,3 +1079,13 @@ config USB_S2255
  
  endif # V4L_USB_DRIVERS
  endif # VIDEO_CAPTURE_DRIVERS
 +
 +menuconfig V4L_MEM2MEM_DRIVERS
 + bool Memory-to-memory multimedia devices
 + depends on VIDEO_V4L2
 + default n
 + ---help---
 +   Say Y here to enable selecting drivers for V4L devices that
 +   use system memory for both source and destination buffers, as opposed
 +   to capture and output drivers, which use memory buffers for just
 +   one of those.
 diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
 index 2af68ee..9fe7d40 100644
 --- a/drivers/media/video/Makefile
 +++ b/drivers/media/video/Makefile
 @@ -115,6 +115,8 @@ obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o
  obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o
  obj-$(CONFIG_VIDEO_BTCX)  += btcx-risc.o
  
 +obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
 +
  obj-$(CONFIG_VIDEO_M32R_AR_M64278) += arv.o
  
  obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o
 diff --git a/drivers/media/video/v4l2-mem2mem.c 
 b/drivers/media/video/v4l2-mem2mem.c
 new file mode 100644
 index 000..417ee2c
 --- /dev/null
 +++ b/drivers/media/video/v4l2-mem2mem.c
 @@ -0,0 +1,671 @@
 +/*
 + * Memory-to-memory device framework for Video for Linux 2.
 + *
 + * Helper functions for devices that use memory buffers for both source
 + * and destination.
 + *
 + * Copyright (c) 2009 Samsung Electronics Co., Ltd.
 + * Pawel Osciak, p.osc...@samsung.com
 + * Marek Szyprowski, m.szyprow...@samsung.com
 + *
 + * This program is free software; you can redistribute it and/or modify
 + * it under the terms of the GNU General Public License as published by the
 + * Free Software Foundation; either version 2 of the
 + * License, or (at your option) any later version
 + */
 +
 +#include linux/module.h
 +#include linux/sched.h
 +#include media/videobuf-core.h
 +#include media/v4l2-mem2mem.h
 +
 +MODULE_DESCRIPTION(Mem to mem device framework for V4L2);
 +MODULE_AUTHOR(Pawel Osciak, p.osc...@samsung.com);
 +MODULE_LICENSE(GPL);
 +
 +static int debug;
 +module_param(debug, int, 0644);
 +
 +#define dprintk(fmt, arg...) do {\
 + if (debug = 1)\
 + printk(KERN_DEBUG %s:  fmt, __func__, ## arg); } while (0)
 +
 +
 +/* The instance is already queued on the jobqueue */
 +#define TRANS_QUEUED (1  0)
 +/* The instance is currently running in hardware */
 +#define TRANS_RUNNING(1  1)
 +
 +
 +/* Offset base for buffers on the destination queue - used to distinguish
 + * between source and destination buffers when mmapping - they receive the 
 same
 + * offsets but for different queues */
 +#define DST_QUEUE_OFF_BASE   (TASK_SIZE / 2)
 +
 +
 +struct v4l2_m2m_dev {
 + /* Currently running instance */
 + struct v4l2_m2m_ctx *curr_ctx;
 + /* Instances queued to run */
 + struct list_headjobqueue;
 + spinlock_t  job_spinlock;
 +
 + struct v4l2_m2m_ops *m2m_ops;
 +};
 +
 +static inline
 +struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
 +  enum v4l2_buf_type type)
 +{
 + switch (type) {
 + case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 + return m2m_ctx-cap_q_ctx;
 + case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 + return