linux-sh.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/07] soc_camera: SuperH Mobile CEU support
@ 2008-07-01 12:46 Magnus Damm
  2008-07-01 12:46 ` [PATCH 01/07] soc_camera: Remove default spinlock operations Magnus Damm
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:46 UTC (permalink / raw)
  To: video4linux-list; +Cc: akpm, lethal, mchehab, linux-sh

These patches add support for the SuperH Mobile CEU interface.

[PATCH 01/07] soc_camera: Remove default spinlock operations
[PATCH 02/07] soc_camera: Let the host select videobuf_queue type
[PATCH 03/07] soc_camera: Remove vbq_ops and msize
[PATCH 04/07] soc_camera: Remove unused file lock pointer
[PATCH 05/07] soc_camera: Add 16-bit bus width support
[PATCH 06/07] videobuf: Add physically contiguous queue code
[PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver

The patches can be divided in 3 groups:
 - soc_camera - make videobuf_queue host specific + minor changes
 - videobuf - add support for physically contiguous memory buffers
 - new driver - add the SuperH Mobile CEU driver 

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/Kconfig                |   16 
 drivers/media/video/Makefile               |    2 
 drivers/media/video/pxa_camera.c           |   21 
 drivers/media/video/sh_mobile_ceu_camera.c |  622 ++++++++++++++++++++++++++++
 drivers/media/video/soc_camera.c           |   52 --
 drivers/media/video/videobuf-dma-contig.c  |  413 ++++++++++++++++++
 include/asm-sh/sh_mobile_ceu.h             |   10 
 include/media/soc_camera.h                 |   16 
 include/media/videobuf-dma-contig.h        |   39 +
 9 files changed, 1129 insertions(+), 62 deletions(-)


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 01/07] soc_camera: Remove default spinlock operations
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
@ 2008-07-01 12:46 ` Magnus Damm
  2008-07-01 12:46 ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:46 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh, akpm, lethal, mchehab

This patch removes the default spinlock_alloc() and spinlock_free()
functions. The pxa_camera.c driver is providing it's own spinlock
callbacks anyway. With this patch spinlock callbacks are required
when registering the host.

This is ground work for the next per-host videobuf queue patch.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/soc_camera.c |   23 ++---------------------
 1 file changed, 2 insertions(+), 21 deletions(-)

--- 0001/drivers/media/video/soc_camera.c
+++ work/drivers/media/video/soc_camera.c	2008-06-12 14:05:36.000000000 +0900
@@ -776,27 +776,13 @@ static void dummy_release(struct device 
 {
 }
 
-static spinlock_t *spinlock_alloc(struct soc_camera_file *icf)
-{
-	spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
-
-	if (lock)
-		spin_lock_init(lock);
-
-	return lock;
-}
-
-static void spinlock_free(spinlock_t *lock)
-{
-	kfree(lock);
-}
-
 int soc_camera_host_register(struct soc_camera_host *ici)
 {
 	int ret;
 	struct soc_camera_host *ix;
 
-	if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove)
+	if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove
+	    || !ici->ops->spinlock_alloc)
 		return -EINVAL;
 
 	/* Number might be equal to the platform device ID */
@@ -821,11 +807,6 @@ int soc_camera_host_register(struct soc_
 	if (ret)
 		goto edevr;
 
-	if (!ici->ops->spinlock_alloc) {
-		ici->ops->spinlock_alloc = spinlock_alloc;
-		ici->ops->spinlock_free = spinlock_free;
-	}
-
 	scan_add_host(ici);
 
 	return 0;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 02/07] soc_camera: Let the host select videobuf_queue type
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
  2008-07-01 12:46 ` [PATCH 01/07] soc_camera: Remove default spinlock operations Magnus Damm
@ 2008-07-01 12:46 ` Magnus Damm
  2008-07-01 20:02   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Guennadi Liakhovetski
  2008-07-02  7:27   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Paulius Zaleckas
  2008-07-01 12:47 ` [PATCH 03/07] soc_camera: Remove vbq_ops and msize Magnus Damm
                   ` (4 subsequent siblings)
  6 siblings, 2 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:46 UTC (permalink / raw)
  To: video4linux-list; +Cc: akpm, lethal, mchehab, linux-sh

This patch makes it possible for hosts (soc_camera drivers for the soc)
to select a different videobuf queue than VIDEOBUF_DMA_SG. This is needed
by the SuperH Mobile CEU hardware which requires physically contiguous
buffers. While at it, rename the spinlock callbacks to file callbacks.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/Kconfig      |    4 ++--
 drivers/media/video/pxa_camera.c |   15 ++++++++++++---
 drivers/media/video/soc_camera.c |   27 +++++++--------------------
 include/media/soc_camera.h       |    6 +++---
 4 files changed, 24 insertions(+), 28 deletions(-)

--- 0001/drivers/media/video/Kconfig
+++ work/drivers/media/video/Kconfig	2008-07-01 13:05:48.000000000 +0900
@@ -901,8 +901,7 @@ endif # V4L_USB_DRIVERS
 
 config SOC_CAMERA
 	tristate "SoC camera support"
-	depends on VIDEO_V4L2 && HAS_DMA
-	select VIDEOBUF_DMA_SG
+	depends on VIDEO_V4L2
 	help
 	  SoC Camera is a common API to several cameras, not connecting
 	  over a bus like PCI or USB. For example some i2c camera connected
@@ -941,6 +940,7 @@ config VIDEO_PXA27x
 	tristate "PXA27x Quick Capture Interface driver"
 	depends on VIDEO_DEV && PXA27x
 	select SOC_CAMERA
+	select VIDEOBUF_DMA_SG
 	---help---
 	  This is a v4l2 driver for the PXA27x Quick Capture Interface
 
--- 0001/drivers/media/video/pxa_camera.c
+++ work/drivers/media/video/pxa_camera.c	2008-07-01 13:03:56.000000000 +0900
@@ -31,6 +31,7 @@
 #include <media/v4l2-common.h>
 #include <media/v4l2-dev.h>
 #include <media/soc_camera.h>
+#include <media/videobuf-dma-sg.h>
 
 #include <linux/videodev2.h>
 
@@ -983,13 +984,21 @@ static int pxa_camera_querycap(struct so
 	return 0;
 }
 
-static spinlock_t *pxa_camera_spinlock_alloc(struct soc_camera_file *icf)
+static int pxa_camera_file_alloc(struct soc_camera_file *icf)
 {
 	struct soc_camera_host *ici  		to_soc_camera_host(icf->icd->dev.parent);
 	struct pxa_camera_dev *pcdev = ici->priv;
 
-	return &pcdev->lock;
+	icf->lock = &pcdev->lock;
+
+	/* We must pass NULL as dev pointer, then all pci_* dma operations
+	 * transform to normal dma_* ones. */
+	videobuf_queue_sg_init(&icf->vb_vidq, &pxa_videobuf_ops,
+			       NULL, icf->lock,
+			       V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
+			       sizeof(struct pxa_buffer), icf->icd);
+	return 0;
 }
 
 static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
@@ -1003,7 +1012,7 @@ static struct soc_camera_host_ops pxa_so
 	.querycap	= pxa_camera_querycap,
 	.try_bus_param	= pxa_camera_try_bus_param,
 	.set_bus_param	= pxa_camera_set_bus_param,
-	.spinlock_alloc	= pxa_camera_spinlock_alloc,
+	.file_alloc	= pxa_camera_file_alloc,
 };
 
 /* Should be allocated dynamically too, but we have only one. */
--- 0005/drivers/media/video/soc_camera.c
+++ work/drivers/media/video/soc_camera.c	2008-07-01 13:03:56.000000000 +0900
@@ -182,7 +182,6 @@ static int soc_camera_open(struct inode 
 	struct soc_camera_device *icd;
 	struct soc_camera_host *ici;
 	struct soc_camera_file *icf;
-	spinlock_t *lock;
 	int ret;
 
 	icf = vmalloc(sizeof(*icf));
@@ -210,11 +209,9 @@ static int soc_camera_open(struct inode 
 
 	icf->icd = icd;
 
-	icf->lock = ici->ops->spinlock_alloc(icf);
-	if (!icf->lock) {
-		ret = -ENOMEM;
+	ret = ici->ops->file_alloc(icf);
+	if (ret)
 		goto esla;
-	}
 
 	icd->use_count++;
 
@@ -232,21 +229,13 @@ static int soc_camera_open(struct inode 
 
 	file->private_data = icf;
 	dev_dbg(&icd->dev, "camera device open\n");
-
-	/* We must pass NULL as dev pointer, then all pci_* dma operations
-	 * transform to normal dma_* ones. */
-	videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, icf->lock,
-				V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
-				ici->msize, icd);
-
 	return 0;
 
 	/* All errors are entered with the video_lock held */
 eiciadd:
-	lock = icf->lock;
+	if (ici->ops->file_free)
+		ici->ops->file_free(icf);
 	icf->lock = NULL;
-	if (ici->ops->spinlock_free)
-		ici->ops->spinlock_free(lock);
 esla:
 	module_put(ici->ops->owner);
 emgi:
@@ -263,15 +252,14 @@ static int soc_camera_close(struct inode
 	struct soc_camera_device *icd = icf->icd;
 	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
 	struct video_device *vdev = icd->vdev;
-	spinlock_t *lock = icf->lock;
 
 	mutex_lock(&video_lock);
 	icd->use_count--;
 	if (!icd->use_count)
 		ici->ops->remove(icd);
+	if (ici->ops->file_free)
+		ici->ops->file_free(icf);
 	icf->lock = NULL;
-	if (ici->ops->spinlock_free)
-		ici->ops->spinlock_free(lock);
 	module_put(icd->ops->owner);
 	module_put(ici->ops->owner);
 	mutex_unlock(&video_lock);
@@ -772,8 +760,7 @@ int soc_camera_host_register(struct soc_
 	int ret;
 	struct soc_camera_host *ix;
 
-	if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove
-	    || !ici->ops->spinlock_alloc)
+	if (!ici->ops->add || !ici->ops->remove || !ici->ops->file_alloc)
 		return -EINVAL;
 
 	/* Number might be equal to the platform device ID */
--- 0001/include/media/soc_camera.h
+++ work/include/media/soc_camera.h	2008-07-01 13:03:56.000000000 +0900
@@ -13,7 +13,7 @@
 #define SOC_CAMERA_H
 
 #include <linux/videodev2.h>
-#include <media/videobuf-dma-sg.h>
+#include <media/videobuf-core.h>
 
 struct soc_camera_device {
 	struct list_head list;
@@ -74,8 +74,8 @@ struct soc_camera_host_ops {
 	int (*try_bus_param)(struct soc_camera_device *, __u32);
 	int (*set_bus_param)(struct soc_camera_device *, __u32);
 	unsigned int (*poll)(struct file *, poll_table *);
-	spinlock_t* (*spinlock_alloc)(struct soc_camera_file *);
-	void (*spinlock_free)(spinlock_t *);
+	int (*file_alloc)(struct soc_camera_file *);
+	void (*file_free)(struct soc_camera_file *);
 };
 
 struct soc_camera_link {

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 03/07] soc_camera: Remove vbq_ops and msize
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
  2008-07-01 12:46 ` [PATCH 01/07] soc_camera: Remove default spinlock operations Magnus Damm
  2008-07-01 12:46 ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
@ 2008-07-01 12:47 ` Magnus Damm
  2008-07-01 12:47 ` [PATCH 04/07] soc_camera: Remove unused file lock pointer Magnus Damm
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:47 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh, akpm, lethal, mchehab

The struct soc_camera_host members vbq_ops and msize are not needed
anymore since we now let the soc_camera host manage the videobuf queue.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/pxa_camera.c |    2 --
 include/media/soc_camera.h       |    2 --
 2 files changed, 4 deletions(-)

--- 0007/drivers/media/video/pxa_camera.c
+++ work/drivers/media/video/pxa_camera.c	2008-06-12 14:30:25.000000000 +0900
@@ -1018,8 +1018,6 @@ static struct soc_camera_host_ops pxa_so
 /* Should be allocated dynamically too, but we have only one. */
 static struct soc_camera_host pxa_soc_camera_host = {
 	.drv_name		= PXA_CAM_DRV_NAME,
-	.vbq_ops		= &pxa_videobuf_ops,
-	.msize			= sizeof(struct pxa_buffer),
 	.ops			= &pxa_soc_camera_host_ops,
 };
 
--- 0007/include/media/soc_camera.h
+++ work/include/media/soc_camera.h	2008-06-12 14:30:18.000000000 +0900
@@ -55,8 +55,6 @@ struct soc_camera_host {
 	struct list_head list;
 	struct device dev;
 	unsigned char nr;				/* Host number */
-	size_t msize;
-	struct videobuf_queue_ops *vbq_ops;
 	void *priv;
 	char *drv_name;
 	struct soc_camera_host_ops *ops;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 04/07] soc_camera: Remove unused file lock pointer
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
                   ` (2 preceding siblings ...)
  2008-07-01 12:47 ` [PATCH 03/07] soc_camera: Remove vbq_ops and msize Magnus Damm
@ 2008-07-01 12:47 ` Magnus Damm
  2008-07-01 12:47 ` [PATCH 05/07] soc_camera: Add 16-bit bus width support Magnus Damm
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:47 UTC (permalink / raw)
  To: video4linux-list; +Cc: akpm, lethal, mchehab, linux-sh

This icf->lock pointer is not needed anymore since we now let the
soc_camera host setup the videobuf queue.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/pxa_camera.c |    4 +---
 drivers/media/video/soc_camera.c |    2 --
 include/media/soc_camera.h       |    1 -
 3 files changed, 1 insertion(+), 6 deletions(-)

--- 0008/drivers/media/video/pxa_camera.c
+++ work/drivers/media/video/pxa_camera.c	2008-06-12 14:36:07.000000000 +0900
@@ -990,12 +990,10 @@ static int pxa_camera_file_alloc(struct 
 		to_soc_camera_host(icf->icd->dev.parent);
 	struct pxa_camera_dev *pcdev = ici->priv;
 
-	icf->lock = &pcdev->lock;
-
 	/* We must pass NULL as dev pointer, then all pci_* dma operations
 	 * transform to normal dma_* ones. */
 	videobuf_queue_sg_init(&icf->vb_vidq, &pxa_videobuf_ops,
-			       NULL, icf->lock,
+			       NULL, &pcdev->lock,
 			       V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
 			       sizeof(struct pxa_buffer), icf->icd);
 	return 0;
--- 0007/drivers/media/video/soc_camera.c
+++ work/drivers/media/video/soc_camera.c	2008-06-12 14:37:33.000000000 +0900
@@ -236,7 +236,6 @@ static int soc_camera_open(struct inode 
 eiciadd:
 	if (ici->ops->file_free)
 		ici->ops->file_free(icf);
-	icf->lock = NULL;
 esla:
 	module_put(ici->ops->owner);
 emgi:
@@ -260,7 +259,6 @@ static int soc_camera_close(struct inode
 		ici->ops->remove(icd);
 	if (ici->ops->file_free)
 		ici->ops->file_free(icf);
-	icf->lock = NULL;
 	module_put(icd->ops->owner);
 	module_put(ici->ops->owner);
 	mutex_unlock(&video_lock);
--- 0008/include/media/soc_camera.h
+++ work/include/media/soc_camera.h	2008-06-12 14:35:21.000000000 +0900
@@ -48,7 +48,6 @@ struct soc_camera_device {
 struct soc_camera_file {
 	struct soc_camera_device *icd;
 	struct videobuf_queue vb_vidq;
-	spinlock_t *lock;
 };
 
 struct soc_camera_host {

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 05/07] soc_camera: Add 16-bit bus width support
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
                   ` (3 preceding siblings ...)
  2008-07-01 12:47 ` [PATCH 04/07] soc_camera: Remove unused file lock pointer Magnus Damm
@ 2008-07-01 12:47 ` Magnus Damm
  2008-07-01 12:47 ` [PATCH 06/07] videobuf: Add physically contiguous queue code Magnus Damm
  2008-07-01 12:47 ` [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver Magnus Damm
  6 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:47 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh, akpm, lethal, mchehab

The SuperH Mobile CEU hardware supports 16-bit width bus,
so extend the soc_camera code with SOCAM_DATAWIDTH_16.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 include/media/soc_camera.h |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

--- 0008/include/media/soc_camera.h
+++ work/include/media/soc_camera.h	2008-07-01 14:38:34.000000000 +0900
@@ -153,11 +153,12 @@ static inline struct v4l2_queryctrl cons
 #define SOCAM_DATAWIDTH_8		(1 << 6)
 #define SOCAM_DATAWIDTH_9		(1 << 7)
 #define SOCAM_DATAWIDTH_10		(1 << 8)
-#define SOCAM_PCLK_SAMPLE_RISING	(1 << 9)
-#define SOCAM_PCLK_SAMPLE_FALLING	(1 << 10)
+#define SOCAM_DATAWIDTH_16		(1 << 9)
+#define SOCAM_PCLK_SAMPLE_RISING	(1 << 10)
+#define SOCAM_PCLK_SAMPLE_FALLING	(1 << 11)
 
 #define SOCAM_DATAWIDTH_MASK (SOCAM_DATAWIDTH_8 | SOCAM_DATAWIDTH_9 | \
-			      SOCAM_DATAWIDTH_10)
+			      SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_16)
 
 static inline unsigned long soc_camera_bus_param_compatible(
 			unsigned long camera_flags, unsigned long bus_flags)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 06/07] videobuf: Add physically contiguous queue code
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
                   ` (4 preceding siblings ...)
  2008-07-01 12:47 ` [PATCH 05/07] soc_camera: Add 16-bit bus width support Magnus Damm
@ 2008-07-01 12:47 ` Magnus Damm
  2008-07-02  7:43   ` Paulius Zaleckas
  2008-07-04  9:08   ` Paulius Zaleckas
  2008-07-01 12:47 ` [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver Magnus Damm
  6 siblings, 2 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:47 UTC (permalink / raw)
  To: video4linux-list; +Cc: akpm, lethal, mchehab, linux-sh

This patch adds support for videobuf queues made from physically
contiguous memory. Useful for hardware such as the SuperH Mobile CEU
which doesn't support scatter gatter bus mastering.

Since it may be difficult to allocate large chunks of physically
contiguous memory after some uptime due to fragmentation, this code
allocates memory using dma_alloc_coherent(). Architectures supporting
dma_declare_coherent_memory() can easily avoid fragmentation issues
by using dma_declare_coherent_memory() to force dma_alloc_coherent()
to allocate from a certain pre-allocated memory area.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/Kconfig               |    4 
 drivers/media/video/Makefile              |    1 
 drivers/media/video/videobuf-dma-contig.c |  413 +++++++++++++++++++++++++++++
 include/media/videobuf-dma-contig.h       |   39 ++
 4 files changed, 457 insertions(+)

--- 0006/drivers/media/video/Kconfig
+++ work/drivers/media/video/Kconfig	2008-06-30 15:30:35.000000000 +0900
@@ -24,6 +24,10 @@ config VIDEOBUF_VMALLOC
 	select VIDEOBUF_GEN
 	tristate
 
+config VIDEOBUF_DMA_CONTIG
+	select VIDEOBUF_GEN
+	tristate
+
 config VIDEOBUF_DVB
 	tristate
 	select VIDEOBUF_GEN
--- 0001/drivers/media/video/Makefile
+++ work/drivers/media/video/Makefile	2008-06-30 15:30:35.000000000 +0900
@@ -88,6 +88,7 @@ obj-$(CONFIG_VIDEO_TUNER) += tuner.o
 
 obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o
 obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o
+obj-$(CONFIG_VIDEOBUF_DMA_CONTIG) += videobuf-dma-contig.o
 obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o
 obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o
 obj-$(CONFIG_VIDEO_BTCX)  += btcx-risc.o
--- /dev/null
+++ work/drivers/media/video/videobuf-dma-contig.c	2008-06-30 15:36:13.000000000 +0900
@@ -0,0 +1,413 @@
+/*
+ * helper functions for physically contiguous capture buffers
+ *
+ * The functions support hardware lacking scatter gatter support
+ * (i.e. the buffers must be linear in physical memory)
+ *
+ * Copyright (c) 2008 Magnus Damm
+ *
+ * Based on videobuf-vmalloc.c,
+ * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
+ *
+ * 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
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/dma-mapping.h>
+#include <media/videobuf-dma-contig.h>
+
+#define MAGIC_DC_MEM 0x0733ac61
+#define MAGIC_CHECK(is, should)						\
+	if (unlikely((is) != (should)))	{				\
+		pr_err("magic mismatch: %x expected %x\n", is, should); \
+		BUG();							\
+	}
+
+static void
+videobuf_vm_open(struct vm_area_struct *vma)
+{
+	struct videobuf_mapping *map = vma->vm_private_data;
+
+	dev_dbg(map->q->dev, "vm_open %p [count=%u,vma=%08lx-%08lx]\n",
+		map, map->count, vma->vm_start, vma->vm_end);
+
+	map->count++;
+}
+
+static void videobuf_vm_close(struct vm_area_struct *vma)
+{
+	struct videobuf_mapping *map = vma->vm_private_data;
+	struct videobuf_queue *q = map->q;
+	int i, datasize;
+
+	dev_dbg(map->q->dev, "vm_close %p [count=%u,vma=%08lx-%08lx]\n",
+		map, map->count, vma->vm_start, vma->vm_end);
+
+	map->count--;
+	if (0 = map->count) {
+		struct videobuf_dma_contig_memory *mem;
+
+		dev_dbg(map->q->dev, "munmap %p q=%p\n", map, q);
+		mutex_lock(&q->vb_lock);
+
+		/* We need first to cancel streams, before unmapping */
+		if (q->streaming)
+			videobuf_queue_cancel(q);
+
+		for (i = 0; i < VIDEO_MAX_FRAME; i++) {
+			if (NULL = q->bufs[i])
+				continue;
+
+			if (q->bufs[i]->map != map)
+				continue;
+
+			mem = q->bufs[i]->priv;
+			if (mem) {
+				/* This callback is called only if kernel has
+				   allocated memory and this memory is mmapped.
+				   In this case, memory should be freed,
+				   in order to do memory unmap.
+				 */
+
+				MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+				/* vfree is not atomic - can't be
+				   called with IRQ's disabled
+				 */
+				dev_dbg(map->q->dev, "buf[%d] freeing %p\n",
+					i, mem->vaddr);
+
+				datasize = PAGE_ALIGN(q->bufs[i]->size);
+				dma_free_coherent(q->dev, datasize,
+						  mem->vaddr, mem->dma_handle);
+				mem->vaddr = NULL;
+			}
+
+			q->bufs[i]->map   = NULL;
+			q->bufs[i]->baddr = 0;
+		}
+
+		kfree(map);
+
+		mutex_unlock(&q->vb_lock);
+	}
+
+	return;
+}
+
+static struct vm_operations_struct videobuf_vm_ops = {
+	.open     = videobuf_vm_open,
+	.close    = videobuf_vm_close,
+};
+
+static void *__videobuf_alloc(size_t size)
+{
+	struct videobuf_dma_contig_memory *mem;
+	struct videobuf_buffer *vb;
+
+	vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
+	if (vb) {
+		mem = vb->priv = ((char *)vb)+size;
+		mem->magic = MAGIC_DC_MEM;
+	}
+
+	return vb;
+}
+
+static void *__videobuf_to_vmalloc(struct videobuf_buffer *buf)
+{
+	struct videobuf_dma_contig_memory *mem = buf->priv;
+
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	return mem->vaddr;
+}
+
+static int __videobuf_iolock(struct videobuf_queue *q,
+			     struct videobuf_buffer *vb,
+			     struct v4l2_framebuffer *fbuf)
+{
+	struct videobuf_dma_contig_memory *mem = vb->priv;
+	int datasize;
+
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	switch (vb->memory) {
+	case V4L2_MEMORY_MMAP:
+		dev_dbg(q->dev, "%s memory method MMAP\n", __func__);
+
+		/* All handling should be done by __videobuf_mmap_mapper() */
+		if (!mem->vaddr) {
+			pr_err("memory is not alloced/mmapped.\n");
+			return -EINVAL;
+		}
+		break;
+	case V4L2_MEMORY_USERPTR:
+		dev_dbg(q->dev, "%s memory method USERPTR\n", __func__);
+
+		/* The only USERPTR currently supported is the one needed for
+		   read() method.
+		 */
+		if (vb->baddr)
+			return -EINVAL;
+
+		datasize = PAGE_ALIGN(vb->size);
+		mem->vaddr = dma_alloc_coherent(q->dev, datasize,
+						&mem->dma_handle, GFP_KERNEL);
+		if (!mem->vaddr) {
+			pr_err("dma_alloc_coherent %d failed\n", datasize);
+			return -ENOMEM;
+		}
+
+		dev_dbg(q->dev, "dma_alloc_coherent data is at %p (%d)\n",
+			mem->vaddr, datasize);
+		break;
+	case V4L2_MEMORY_OVERLAY:
+	default:
+		dev_dbg(q->dev, "%s memory method OVERLAY/unknown\n",
+			__func__);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int __videobuf_sync(struct videobuf_queue *q,
+			   struct videobuf_buffer *buf)
+{
+	return 0;
+}
+
+static int __videobuf_mmap_free(struct videobuf_queue *q)
+{
+	unsigned int i;
+
+	dev_dbg(q->dev, "%s\n", __func__);
+	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
+		if (q->bufs[i]) {
+			if (q->bufs[i]->map)
+				return -EBUSY;
+		}
+	}
+
+	return 0;
+}
+
+static int __videobuf_mmap_mapper(struct videobuf_queue *q,
+				  struct vm_area_struct *vma)
+{
+	struct videobuf_dma_contig_memory *mem;
+	struct videobuf_mapping *map;
+	unsigned int first;
+	int retval, datasize;
+	unsigned long size, offset = vma->vm_pgoff << PAGE_SHIFT;
+
+	dev_dbg(q->dev, "%s\n", __func__);
+	if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
+		return -EINVAL;
+
+	/* look for first buffer to map */
+	for (first = 0; first < VIDEO_MAX_FRAME; first++) {
+		if (NULL = q->bufs[first])
+			continue;
+
+		if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
+			continue;
+		if (q->bufs[first]->boff = offset)
+			break;
+	}
+	if (VIDEO_MAX_FRAME = first) {
+		dev_dbg(q->dev, "invalid user space offset [offset=0x%lx]\n",
+			(vma->vm_pgoff << PAGE_SHIFT));
+		return -EINVAL;
+	}
+
+	/* create mapping + update buffer list */
+	map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
+	if (NULL = map)
+		return -ENOMEM;
+
+	q->bufs[first]->map = map;
+	map->start = vma->vm_start;
+	map->end = vma->vm_end;
+	map->q = q;
+
+	q->bufs[first]->baddr = vma->vm_start;
+
+	mem = q->bufs[first]->priv;
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	datasize = PAGE_ALIGN(q->bufs[first]->bsize);
+	mem->vaddr = dma_alloc_coherent(q->dev, datasize,
+					&mem->dma_handle, GFP_KERNEL);
+	if (!mem->vaddr) {
+		pr_err("dma_alloc_coherent size %d failed\n", datasize);
+		goto error;
+	}
+	dev_dbg(q->dev, "dma_alloc_coherent data is at addr %p (size %d)\n",
+		mem->vaddr, datasize);
+
+	/* Try to remap memory */
+
+	size = vma->vm_end - vma->vm_start;
+	size = (size < datasize) ? size : datasize;
+
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+	retval = remap_pfn_range(vma, vma->vm_start,
+				 __pa(mem->vaddr) >> PAGE_SHIFT,
+				 size, vma->vm_page_prot);
+	if (retval < 0) {
+		pr_err("mmap: remap failed with error %d. ", retval);
+		dma_free_coherent(q->dev, datasize,
+				  mem->vaddr, mem->dma_handle);
+		goto error;
+	}
+
+	vma->vm_ops          = &videobuf_vm_ops;
+	vma->vm_flags       |= VM_DONTEXPAND;
+	vma->vm_private_data = map;
+
+	dev_dbg(q->dev, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
+		map, q, vma->vm_start, vma->vm_end,
+		(long int) q->bufs[first]->bsize,
+		vma->vm_pgoff, first);
+
+	videobuf_vm_open(vma);
+
+	return 0;
+
+error:
+	kfree(map);
+	return -ENOMEM;
+}
+
+static int __videobuf_copy_to_user(struct videobuf_queue *q,
+				   char __user *data, size_t count,
+				   int nonblocking)
+{
+	struct videobuf_dma_contig_memory *mem = q->read_buf->priv;
+	void *vaddr;
+
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+	BUG_ON(!mem->vaddr);
+
+	/* copy to userspace */
+	if (count > q->read_buf->size - q->read_off)
+		count = q->read_buf->size - q->read_off;
+
+	vaddr = mem->vaddr;
+
+	if (copy_to_user(data, vaddr + q->read_off, count))
+		return -EFAULT;
+
+	return count;
+}
+
+static int __videobuf_copy_stream(struct videobuf_queue *q,
+				  char __user *data, size_t count, size_t pos,
+				  int vbihack, int nonblocking)
+{
+	unsigned int  *fc;
+	struct videobuf_dma_contig_memory *mem = q->read_buf->priv;
+
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	if (vbihack) {
+		/* dirty, undocumented hack -- pass the frame counter
+			* within the last four bytes of each vbi data block.
+			* We need that one to maintain backward compatibility
+			* to all vbi decoding software out there ... */
+		fc = (unsigned int *)mem->vaddr;
+		fc += (q->read_buf->size >> 2) - 1;
+		*fc = q->read_buf->field_count >> 1;
+		dev_dbg(q->dev, "vbihack: %d\n", *fc);
+	}
+
+	/* copy stuff using the common method */
+	count = __videobuf_copy_to_user(q, data, count, nonblocking);
+
+	if ((count = -EFAULT) && (pos = 0))
+		return -EFAULT;
+
+	return count;
+}
+
+static struct videobuf_qtype_ops qops = {
+	.magic        = MAGIC_QTYPE_OPS,
+
+	.alloc        = __videobuf_alloc,
+	.iolock       = __videobuf_iolock,
+	.sync         = __videobuf_sync,
+	.mmap_free    = __videobuf_mmap_free,
+	.mmap_mapper  = __videobuf_mmap_mapper,
+	.video_copy_to_user = __videobuf_copy_to_user,
+	.copy_stream  = __videobuf_copy_stream,
+	.vmalloc      = __videobuf_to_vmalloc,
+};
+
+void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
+				    struct videobuf_queue_ops *ops,
+				    struct device *dev,
+				    spinlock_t *irqlock,
+				    enum v4l2_buf_type type,
+				    enum v4l2_field field,
+				    unsigned int msize,
+				    void *priv)
+{
+	videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
+				 priv, &qops);
+}
+EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init);
+
+struct videobuf_dma_contig_memory *
+videobuf_to_dma_contig(struct videobuf_buffer *buf)
+{
+	struct videobuf_dma_contig_memory *mem = buf->priv;
+
+	BUG_ON(!mem);
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	return mem;
+}
+EXPORT_SYMBOL_GPL(videobuf_to_dma_contig);
+
+void videobuf_dma_contig_free(struct videobuf_queue *q,
+			      struct videobuf_buffer *buf)
+{
+	struct videobuf_dma_contig_memory *mem = buf->priv;
+	int datasize;
+
+	/* mmapped memory can't be freed here, otherwise mmapped region
+	   would be released, while still needed. In this case, the memory
+	   release should happen inside videobuf_vm_close().
+	   So, it should free memory only if the memory were allocated for
+	   read() operation.
+	 */
+	if ((buf->memory != V4L2_MEMORY_USERPTR) || (buf->baddr = 0))
+		return;
+
+	if (!mem)
+		return;
+
+	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
+
+	datasize = PAGE_ALIGN(buf->size);
+	dma_free_coherent(q->dev, datasize, mem->vaddr, mem->dma_handle);
+	mem->vaddr = NULL;
+
+	return;
+}
+EXPORT_SYMBOL_GPL(videobuf_dma_contig_free);
+
+MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers");
+MODULE_AUTHOR("Magnus Damm");
+MODULE_LICENSE("GPL");
+
--- /dev/null
+++ work/include/media/videobuf-dma-contig.h	2008-06-30 15:30:35.000000000 +0900
@@ -0,0 +1,39 @@
+/*
+ * helper functions for physically contiguous capture buffers
+ *
+ * The functions support hardware lacking scatter gatter support
+ * (i.e. the buffers must be linear in physical memory)
+ *
+ * Copyright (c) 2008 Magnus Damm
+ *
+ * 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
+ */
+#ifndef _VIDEOBUF_DMA_CONTIG_H
+#define _VIDEOBUF_DMA_CONTIG_H
+
+#include <linux/dma-mapping.h>
+#include <media/videobuf-core.h>
+
+struct videobuf_dma_contig_memory {
+	u32 magic;
+	void *vaddr;
+	dma_addr_t dma_handle;
+};
+
+void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
+				    struct videobuf_queue_ops *ops,
+				    struct device *dev,
+				    spinlock_t *irqlock,
+				    enum v4l2_buf_type type,
+				    enum v4l2_field field,
+				    unsigned int msize,
+				    void *priv);
+
+struct videobuf_dma_contig_memory *
+videobuf_to_dma_contig(struct videobuf_buffer *buf);
+void videobuf_dma_contig_free(struct videobuf_queue *q,
+			      struct videobuf_buffer *buf);
+
+#endif /* _VIDEOBUF_DMA_CONTIG_H */

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver
  2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
                   ` (5 preceding siblings ...)
  2008-07-01 12:47 ` [PATCH 06/07] videobuf: Add physically contiguous queue code Magnus Damm
@ 2008-07-01 12:47 ` Magnus Damm
  2008-07-01 20:07   ` Guennadi Liakhovetski
  6 siblings, 1 reply; 16+ messages in thread
From: Magnus Damm @ 2008-07-01 12:47 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh, akpm, lethal, mchehab

This patch adds support for the SuperH Mobile CEU interface.

The hardware is configured in a transparent data fetch mode,
and frames are captured from the attached camera and written
to physically contiguous memory buffers provided by the
videobuf-dma-contig queue.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/media/video/Kconfig                |    8 
 drivers/media/video/Makefile               |    1 
 drivers/media/video/sh_mobile_ceu_camera.c |  622 ++++++++++++++++++++++++++++
 include/asm-sh/sh_mobile_ceu.h             |   10 
 4 files changed, 641 insertions(+)

--- 0009/drivers/media/video/Kconfig
+++ work/drivers/media/video/Kconfig	2008-07-01 18:28:21.000000000 +0900
@@ -948,4 +948,12 @@ config VIDEO_PXA27x
 	---help---
 	  This is a v4l2 driver for the PXA27x Quick Capture Interface
 
+config VIDEO_SH_MOBILE_CEU
+	tristate "SuperH Mobile CEU Interface driver"
+	depends on VIDEO_DEV && SUPERH
+	select SOC_CAMERA
+	select VIDEOBUF_DMA_CONTIG
+	---help---
+	  This is a v4l2 driver for the SuperH Mobile CEU Interface
+
 endif # VIDEO_CAPTURE_DRIVERS
--- 0009/drivers/media/video/Makefile
+++ work/drivers/media/video/Makefile	2008-07-01 18:28:21.000000000 +0900
@@ -131,6 +131,7 @@ obj-$(CONFIG_VIDEO_VIVI) += vivi.o
 obj-$(CONFIG_VIDEO_CX23885) += cx23885/
 
 obj-$(CONFIG_VIDEO_PXA27x)	+= pxa_camera.o
+obj-$(CONFIG_VIDEO_SH_MOBILE_CEU)	+= sh_mobile_ceu_camera.o
 obj-$(CONFIG_SOC_CAMERA)	+= soc_camera.o
 obj-$(CONFIG_SOC_CAMERA_MT9M001)	+= mt9m001.o
 obj-$(CONFIG_SOC_CAMERA_MT9V022)	+= mt9v022.o
--- /dev/null
+++ work/drivers/media/video/sh_mobile_ceu_camera.c	2008-07-01 20:32:07.000000000 +0900
@@ -0,0 +1,622 @@
+/*
+ * V4L2 Driver for SuperH Mobile CEU interface
+ *
+ * Copyright (C) 2008 Magnus Damm
+ *
+ * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
+ *
+ * Copyright (C) 2006, Sascha Hauer, Pengutronix
+ * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
+ *
+ * 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/init.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/moduleparam.h>
+#include <linux/time.h>
+#include <linux/version.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/mutex.h>
+
+#include <media/v4l2-common.h>
+#include <media/v4l2-dev.h>
+#include <media/soc_camera.h>
+#include <media/videobuf-dma-contig.h>
+
+#include <linux/videodev2.h>
+
+#include <asm/sh_mobile_ceu.h>
+
+/* register offsets for sh7722 / sh7723 */
+
+#define CAPSR  0x00
+#define CAPCR  0x04
+#define CAMCR  0x08
+#define CMCYR  0x0c
+#define CAMOR  0x10
+#define CAPWR  0x14
+#define CAIFR  0x18
+#define CSTCR  0x20 /* not on sh7723 */
+#define CSECR  0x24 /* not on sh7723 */
+#define CRCNTR 0x28
+#define CRCMPR 0x2c
+#define CFLCR  0x30
+#define CFSZR  0x34
+#define CDWDR  0x38
+#define CDAYR  0x3c
+#define CDACR  0x40
+#define CDBYR  0x44
+#define CDBCR  0x48
+#define CBDSR  0x4c
+#define CFWCR  0x5c
+#define CLFCR  0x60
+#define CDOCR  0x64
+#define CDDCR  0x68
+#define CDDAR  0x6c
+#define CEIER  0x70
+#define CETCR  0x74
+#define CSTSR  0x7c
+#define CSRTR  0x80
+#define CDSSR  0x84
+#define CDAYR2 0x90
+#define CDACR2 0x94
+#define CDBYR2 0x98
+#define CDBCR2 0x9c
+
+static DEFINE_MUTEX(camera_lock);
+
+/* per video frame buffer */
+struct sh_mobile_ceu_buffer {
+	struct videobuf_buffer vb; /* v4l buffer must be first */
+	const struct soc_camera_data_format *fmt;
+};
+
+struct sh_mobile_ceu_dev {
+	struct device *dev;
+	struct soc_camera_host ici;
+	struct soc_camera_device *icd;
+
+	unsigned int irq;
+	void __iomem *base;
+
+	spinlock_t lock;
+	struct list_head capture;
+	struct videobuf_buffer *active;
+
+	struct sh_mobile_ceu_info *pdata;
+};
+
+static unsigned int vid_limit = 4;	/* Video memory limit, in Mb */
+
+static void ceu_write(struct sh_mobile_ceu_dev *priv,
+		      unsigned long reg_offs, unsigned long data)
+{
+	iowrite32(data, priv->base + reg_offs);
+}
+
+static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
+			      unsigned long reg_offs)
+{
+	return ioread32(priv->base + reg_offs);
+}
+
+/*
+ *  Videobuf operations
+ */
+static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
+					unsigned int *count,
+					unsigned int *size)
+{
+	struct soc_camera_device *icd = vq->priv_data;
+	int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
+
+	*size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
+
+	if (0 = *count)
+		*count = 2;
+	while (*size * *count > vid_limit * 1024 * 1024)
+		(*count)--;
+
+	dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
+
+	return 0;
+}
+
+static void free_buffer(struct videobuf_queue *vq,
+			struct sh_mobile_ceu_buffer *buf)
+{
+	struct soc_camera_device *icd = vq->priv_data;
+
+	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
+		&buf->vb, buf->vb.baddr, buf->vb.bsize);
+
+	if (in_interrupt())
+		BUG();
+
+	videobuf_dma_contig_free(vq, &buf->vb);
+	dev_dbg(&icd->dev, "%s freed\n", __func__);
+	buf->vb.state = VIDEOBUF_NEEDS_INIT;
+}
+
+static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
+{
+	struct videobuf_dma_contig_memory *mem;
+
+	ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
+	ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
+	ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
+
+	ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
+
+	ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
+
+	if (pcdev->active) {
+		mem = videobuf_to_dma_contig(pcdev->active);
+
+		ceu_write(pcdev, CDAYR, mem->dma_handle);
+		ceu_write(pcdev, CAPSR, 0x1); /* start capture */
+	}
+}
+
+static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
+					  struct videobuf_buffer *vb,
+					  enum v4l2_field field)
+{
+	struct soc_camera_device *icd = vq->priv_data;
+	struct sh_mobile_ceu_buffer *buf;
+	int ret;
+
+	buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
+
+	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
+		vb, vb->baddr, vb->bsize);
+
+	/* Added list head initialization on alloc */
+	WARN_ON(!list_empty(&vb->queue));
+
+#ifdef DEBUG
+	/* This can be useful if you want to see if we actually fill
+	 * the buffer with something */
+	memset((void *)vb->baddr, 0xaa, vb->bsize);
+#endif
+
+	BUG_ON(NULL = icd->current_fmt);
+
+	if (buf->fmt	!= icd->current_fmt ||
+	    vb->width	!= icd->width ||
+	    vb->height	!= icd->height ||
+	    vb->field	!= field) {
+		buf->fmt	= icd->current_fmt;
+		vb->width	= icd->width;
+		vb->height	= icd->height;
+		vb->field	= field;
+		vb->state	= VIDEOBUF_NEEDS_INIT;
+	}
+
+	vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
+	if (0 != vb->baddr && vb->bsize < vb->size) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (vb->state = VIDEOBUF_NEEDS_INIT) {
+		ret = videobuf_iolock(vq, vb, NULL);
+		if (ret)
+			goto fail;
+		vb->state = VIDEOBUF_PREPARED;
+	}
+
+	return 0;
+fail:
+	free_buffer(vq, buf);
+out:
+	return ret;
+}
+
+static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
+					 struct videobuf_buffer *vb)
+{
+	struct soc_camera_device *icd = vq->priv_data;
+	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+	unsigned long flags;
+
+	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
+		vb, vb->baddr, vb->bsize);
+
+	vb->state = VIDEOBUF_ACTIVE;
+	spin_lock_irqsave(&pcdev->lock, flags);
+	list_add_tail(&vb->queue, &pcdev->capture);
+
+	if (!pcdev->active) {
+		pcdev->active = vb;
+		sh_mobile_ceu_capture(pcdev);
+	}
+
+	spin_unlock_irqrestore(&pcdev->lock, flags);
+}
+
+static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
+					   struct videobuf_buffer *vb)
+{
+	free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
+}
+
+static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
+	.buf_setup      = sh_mobile_ceu_videobuf_setup,
+	.buf_prepare    = sh_mobile_ceu_videobuf_prepare,
+	.buf_queue      = sh_mobile_ceu_videobuf_queue,
+	.buf_release    = sh_mobile_ceu_videobuf_release,
+};
+
+static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
+{
+	struct sh_mobile_ceu_dev *pcdev = data;
+	struct videobuf_buffer *vb;
+	unsigned long flags;
+
+	spin_lock_irqsave(&pcdev->lock, flags);
+
+	vb = pcdev->active;
+	list_del_init(&vb->queue);
+
+	if (!list_empty(&pcdev->capture))
+		pcdev->active = list_entry(pcdev->capture.next,
+					   struct videobuf_buffer, queue);
+	else
+		pcdev->active = NULL;
+
+	sh_mobile_ceu_capture(pcdev);
+
+	vb->state = VIDEOBUF_DONE;
+	do_gettimeofday(&vb->ts);
+	vb->field_count++;
+	wake_up(&vb->done);
+	spin_unlock_irqrestore(&pcdev->lock, flags);
+
+	return IRQ_HANDLED;
+}
+
+static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
+{
+	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+	int ret = -EBUSY;
+
+	mutex_lock(&camera_lock);
+
+	if (pcdev->icd)
+		goto err;
+
+	dev_info(&icd->dev,
+		 "SuperH Mobile CEU driver attached to camera %d\n",
+		 icd->devnum);
+
+	ret = icd->ops->init(icd);
+	if (ret)
+		goto err;
+
+	ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
+	while (ceu_read(pcdev, CSTSR) & 1)
+		msleep(1);
+
+	pcdev->icd = icd;
+err:
+	mutex_unlock(&camera_lock);
+
+	return ret;
+}
+
+static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
+{
+	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+
+	BUG_ON(icd != pcdev->icd);
+
+	dev_info(&icd->dev,
+		 "SuperH Mobile CEU driver detached from camera %d\n",
+		 icd->devnum);
+
+	/* disable capture, disable interrupts */
+	ceu_write(pcdev, CEIER, 0);
+	ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
+	icd->ops->release(icd);
+	pcdev->icd = NULL;
+}
+
+static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
+				       __u32 pixfmt)
+{
+	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+	int ret, buswidth, width, cfszr_width, cdwdr_width;
+	unsigned long camera_flags, common_flags, value;
+
+	camera_flags = icd->ops->query_bus_param(icd);
+	common_flags = soc_camera_bus_param_compatible(camera_flags,
+						       pcdev->pdata->flags);
+	if (!common_flags)
+		return -EINVAL;
+
+	ret = icd->ops->set_bus_param(icd, common_flags);
+	if (ret < 0)
+		return ret;
+
+	switch (common_flags & SOCAM_DATAWIDTH_MASK) {
+	case SOCAM_DATAWIDTH_8: buswidth = 8; break;
+	case SOCAM_DATAWIDTH_16: buswidth = 16; break;
+	default: return -EINVAL;
+	}
+
+	ceu_write(pcdev, CRCNTR, 0);
+	ceu_write(pcdev, CRCMPR, 0);
+
+	value = 0x00000010;
+	value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
+	value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
+	value |= (buswidth = 16) ? (1 << 12) : 0;
+	ceu_write(pcdev, CAMCR, value);
+
+	ceu_write(pcdev, CAPCR, 0x00300000);
+	ceu_write(pcdev, CAIFR, 0);
+
+	mdelay(1);
+
+	width = icd->width * (icd->current_fmt->depth / 8);
+	width = (buswidth = 16) ? width / 2 : width;
+	cfszr_width = (buswidth = 8) ? width / 2 : width;
+	cdwdr_width = (buswidth = 16) ? width * 2 : width;
+
+	ceu_write(pcdev, CAMOR, 0);
+	ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
+	ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */
+	ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
+	ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */
+	ceu_write(pcdev, CDOCR, 0x00000016);
+
+	ceu_write(pcdev, CDWDR, cdwdr_width);
+	ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
+
+	/* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
+	/* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
+
+	return 0;
+}
+
+static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
+				       __u32 pixfmt)
+{
+	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+	unsigned long camera_flags, common_flags;
+
+	camera_flags = icd->ops->query_bus_param(icd);
+	common_flags = soc_camera_bus_param_compatible(camera_flags,
+						       pcdev->pdata->flags);
+	if (!common_flags)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int sh_mobile_ceu_set_fmt_cap(struct soc_camera_device *icd,
+				     __u32 pixfmt, struct v4l2_rect *rect)
+{
+	return icd->ops->set_fmt_cap(icd, pixfmt, rect);
+}
+
+static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device *icd,
+				     struct v4l2_format *f)
+{
+	/* FIXME: calculate using depth and bus width */
+
+	if (f->fmt.pix.height < 4)
+		f->fmt.pix.height = 4;
+	if (f->fmt.pix.height > 1920)
+		f->fmt.pix.height = 1920;
+	if (f->fmt.pix.width < 2)
+		f->fmt.pix.width = 2;
+	if (f->fmt.pix.width > 2560)
+		f->fmt.pix.width = 2560;
+	f->fmt.pix.width &= ~0x01;
+	f->fmt.pix.height &= ~0x03;
+
+	/* limit to sensor capabilities */
+	return icd->ops->try_fmt_cap(icd, f);
+}
+
+static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
+				 struct v4l2_requestbuffers *p)
+{
+	int i;
+
+	/* This is for locking debugging only. I removed spinlocks and now I
+	 * check whether .prepare is ever called on a linked buffer, or whether
+	 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
+	 * it hadn't triggered */
+	for (i = 0; i < p->count; i++) {
+		struct sh_mobile_ceu_buffer *buf;
+
+		buf = container_of(icf->vb_vidq.bufs[i],
+				   struct sh_mobile_ceu_buffer, vb);
+		INIT_LIST_HEAD(&buf->vb.queue);
+	}
+
+	return 0;
+}
+
+static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
+{
+	struct soc_camera_file *icf = file->private_data;
+	struct sh_mobile_ceu_buffer *buf;
+
+	buf = list_entry(icf->vb_vidq.stream.next,
+			 struct sh_mobile_ceu_buffer, vb.stream);
+
+	poll_wait(file, &buf->vb.done, pt);
+
+	if (buf->vb.state = VIDEOBUF_DONE ||
+	    buf->vb.state = VIDEOBUF_ERROR)
+		return POLLIN|POLLRDNORM;
+
+	return 0;
+}
+
+static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
+				  struct v4l2_capability *cap)
+{
+	strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
+	cap->version = KERNEL_VERSION(0, 0, 5);
+	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
+	return 0;
+}
+
+static int sh_mobile_ceu_file_alloc(struct soc_camera_file *icf)
+{
+	struct soc_camera_host *ici = to_soc_camera_host(icf->icd->dev.parent);
+	struct sh_mobile_ceu_dev *pcdev = ici->priv;
+
+	videobuf_queue_dma_contig_init(&icf->vb_vidq,
+				       &sh_mobile_ceu_videobuf_ops,
+				       &ici->dev, &pcdev->lock,
+				       V4L2_BUF_TYPE_VIDEO_CAPTURE,
+				       V4L2_FIELD_NONE,
+				       sizeof(struct sh_mobile_ceu_buffer),
+				       icf->icd);
+	return 0;
+}
+
+static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
+	.owner		= THIS_MODULE,
+	.add		= sh_mobile_ceu_add_device,
+	.remove		= sh_mobile_ceu_remove_device,
+	.set_fmt_cap	= sh_mobile_ceu_set_fmt_cap,
+	.try_fmt_cap	= sh_mobile_ceu_try_fmt_cap,
+	.reqbufs	= sh_mobile_ceu_reqbufs,
+	.poll		= sh_mobile_ceu_poll,
+	.querycap	= sh_mobile_ceu_querycap,
+	.try_bus_param	= sh_mobile_ceu_try_bus_param,
+	.set_bus_param	= sh_mobile_ceu_set_bus_param,
+	.file_alloc	= sh_mobile_ceu_file_alloc,
+};
+
+static int sh_mobile_ceu_probe(struct platform_device *pdev)
+{
+	struct sh_mobile_ceu_dev *pcdev;
+	struct resource *res;
+	void __iomem *base;
+	unsigned int irq;
+	int err = 0;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	irq = platform_get_irq(pdev, 0);
+	if (!res || !irq) {
+		err = -ENODEV;
+		goto exit;
+	}
+
+	pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
+	if (!pcdev) {
+		dev_err(&pdev->dev, "Could not allocate pcdev\n");
+		err = -ENOMEM;
+		goto exit;
+	}
+
+	platform_set_drvdata(pdev, pcdev);
+	INIT_LIST_HEAD(&pcdev->capture);
+	spin_lock_init(&pcdev->lock);
+
+	pcdev->pdata = pdev->dev.platform_data;
+	if (!pcdev->pdata) {
+		err = -EINVAL;
+		goto exit_kfree;
+	}
+
+	base = ioremap_nocache(res->start, res->end - res->start + 1);
+	if (!base) {
+		err = -ENXIO;
+		goto exit_kfree;
+	}
+	pcdev->irq = irq;
+	pcdev->base = base;
+	pcdev->dev = &pdev->dev;
+
+	/* request irq */
+	err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
+			  pdev->dev.bus_id, pcdev);
+	if (err) {
+		dev_err(pcdev->dev, "Camera interrupt register failed \n");
+		goto exit_iounmap;
+	}
+
+	pcdev->ici.priv = pcdev;
+	pcdev->ici.dev.parent = &pdev->dev;
+	pcdev->ici.nr = pdev->id;
+	pcdev->ici.drv_name = pdev->dev.bus_id,
+	pcdev->ici.ops = &sh_mobile_ceu_host_ops,
+
+	err = soc_camera_host_register(&pcdev->ici);
+	if (err)
+		goto exit_free_irq;
+
+	return 0;
+
+exit_free_irq:
+	free_irq(pcdev->irq, pcdev);
+exit_iounmap:
+	iounmap(base);
+exit_kfree:
+	kfree(pcdev);
+exit:
+	return err;
+}
+
+static int sh_mobile_ceu_remove(struct platform_device *pdev)
+{
+	struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
+
+	soc_camera_host_unregister(&pcdev->ici);
+	free_irq(pcdev->irq, pcdev);
+	iounmap(pcdev->base);
+	kfree(pcdev);
+	return 0;
+}
+
+static struct platform_driver sh_mobile_ceu_driver = {
+	.driver 	= {
+		.name	= "sh_mobile_ceu",
+	},
+	.probe		= sh_mobile_ceu_probe,
+	.remove		= sh_mobile_ceu_remove,
+};
+
+static int __init sh_mobile_ceu_init(void)
+{
+	return platform_driver_register(&sh_mobile_ceu_driver);
+}
+
+static void __exit sh_mobile_ceu_exit(void)
+{
+	return platform_driver_unregister(&sh_mobile_ceu_driver);
+}
+
+module_init(sh_mobile_ceu_init);
+module_exit(sh_mobile_ceu_exit);
+
+MODULE_DESCRIPTION("SuperH Mobile CEU driver");
+MODULE_AUTHOR("Magnus Damm");
+MODULE_LICENSE("GPL");
--- /dev/null
+++ work/include/asm-sh/sh_mobile_ceu.h	2008-07-01 18:28:21.000000000 +0900
@@ -0,0 +1,10 @@
+#ifndef __ASM_SH_MOBILE_CEU_H__
+#define __ASM_SH_MOBILE_CEU_H__
+
+#include <media/soc_camera.h>
+
+struct sh_mobile_ceu_info {
+	unsigned long flags; /* SOCAM_... */
+};
+
+#endif /* __ASM_SH_MOBILE_CEU_H__ */

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 02/07] soc_camera: Let the host select videobuf_queue
  2008-07-01 12:46 ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
@ 2008-07-01 20:02   ` Guennadi Liakhovetski
  2008-07-02  2:50     ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
  2008-07-02  7:27   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Paulius Zaleckas
  1 sibling, 1 reply; 16+ messages in thread
From: Guennadi Liakhovetski @ 2008-07-01 20:02 UTC (permalink / raw)
  To: Magnus Damm; +Cc: linux-sh, video4linux-list, lethal, akpm, mchehab

On Tue, 1 Jul 2008, Magnus Damm wrote:

> This patch makes it possible for hosts (soc_camera drivers for the soc)
> to select a different videobuf queue than VIDEOBUF_DMA_SG. This is needed
> by the SuperH Mobile CEU hardware which requires physically contiguous
> buffers. While at it, rename the spinlock callbacks to file callbacks.
> 
> Signed-off-by: Magnus Damm <damm@igel.co.jp>

I'm afraid, this patch conflicts with an earlier one by Paulius Zaleckas:
http://marc.info/?l=linux-video&m\x121438688924771&w=2, which I've already 
acked, and which, I think, makes videobuf handling more generic than 
yours. Could you please have a look if you can use it, if yes - rebase 
your patch-set on it (I think, some other your patches will have to be 
changed then too). If there's anything that you cannot use there, we'll 
have to see how we can satisfy all the requirements.

Thanks
Guennadi

> ---
> 
>  drivers/media/video/Kconfig      |    4 ++--
>  drivers/media/video/pxa_camera.c |   15 ++++++++++++---
>  drivers/media/video/soc_camera.c |   27 +++++++--------------------
>  include/media/soc_camera.h       |    6 +++---
>  4 files changed, 24 insertions(+), 28 deletions(-)
> 
> --- 0001/drivers/media/video/Kconfig
> +++ work/drivers/media/video/Kconfig	2008-07-01 13:05:48.000000000 +0900
> @@ -901,8 +901,7 @@ endif # V4L_USB_DRIVERS
>  
>  config SOC_CAMERA
>  	tristate "SoC camera support"
> -	depends on VIDEO_V4L2 && HAS_DMA
> -	select VIDEOBUF_DMA_SG
> +	depends on VIDEO_V4L2
>  	help
>  	  SoC Camera is a common API to several cameras, not connecting
>  	  over a bus like PCI or USB. For example some i2c camera connected
> @@ -941,6 +940,7 @@ config VIDEO_PXA27x
>  	tristate "PXA27x Quick Capture Interface driver"
>  	depends on VIDEO_DEV && PXA27x
>  	select SOC_CAMERA
> +	select VIDEOBUF_DMA_SG
>  	---help---
>  	  This is a v4l2 driver for the PXA27x Quick Capture Interface
>  
> --- 0001/drivers/media/video/pxa_camera.c
> +++ work/drivers/media/video/pxa_camera.c	2008-07-01 13:03:56.000000000 +0900
> @@ -31,6 +31,7 @@
>  #include <media/v4l2-common.h>
>  #include <media/v4l2-dev.h>
>  #include <media/soc_camera.h>
> +#include <media/videobuf-dma-sg.h>
>  
>  #include <linux/videodev2.h>
>  
> @@ -983,13 +984,21 @@ static int pxa_camera_querycap(struct so
>  	return 0;
>  }
>  
> -static spinlock_t *pxa_camera_spinlock_alloc(struct soc_camera_file *icf)
> +static int pxa_camera_file_alloc(struct soc_camera_file *icf)
>  {
>  	struct soc_camera_host *ici >  		to_soc_camera_host(icf->icd->dev.parent);
>  	struct pxa_camera_dev *pcdev = ici->priv;
>  
> -	return &pcdev->lock;
> +	icf->lock = &pcdev->lock;
> +
> +	/* We must pass NULL as dev pointer, then all pci_* dma operations
> +	 * transform to normal dma_* ones. */
> +	videobuf_queue_sg_init(&icf->vb_vidq, &pxa_videobuf_ops,
> +			       NULL, icf->lock,
> +			       V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
> +			       sizeof(struct pxa_buffer), icf->icd);
> +	return 0;
>  }
>  
>  static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
> @@ -1003,7 +1012,7 @@ static struct soc_camera_host_ops pxa_so
>  	.querycap	= pxa_camera_querycap,
>  	.try_bus_param	= pxa_camera_try_bus_param,
>  	.set_bus_param	= pxa_camera_set_bus_param,
> -	.spinlock_alloc	= pxa_camera_spinlock_alloc,
> +	.file_alloc	= pxa_camera_file_alloc,
>  };
>  
>  /* Should be allocated dynamically too, but we have only one. */
> --- 0005/drivers/media/video/soc_camera.c
> +++ work/drivers/media/video/soc_camera.c	2008-07-01 13:03:56.000000000 +0900
> @@ -182,7 +182,6 @@ static int soc_camera_open(struct inode 
>  	struct soc_camera_device *icd;
>  	struct soc_camera_host *ici;
>  	struct soc_camera_file *icf;
> -	spinlock_t *lock;
>  	int ret;
>  
>  	icf = vmalloc(sizeof(*icf));
> @@ -210,11 +209,9 @@ static int soc_camera_open(struct inode 
>  
>  	icf->icd = icd;
>  
> -	icf->lock = ici->ops->spinlock_alloc(icf);
> -	if (!icf->lock) {
> -		ret = -ENOMEM;
> +	ret = ici->ops->file_alloc(icf);
> +	if (ret)
>  		goto esla;
> -	}
>  
>  	icd->use_count++;
>  
> @@ -232,21 +229,13 @@ static int soc_camera_open(struct inode 
>  
>  	file->private_data = icf;
>  	dev_dbg(&icd->dev, "camera device open\n");
> -
> -	/* We must pass NULL as dev pointer, then all pci_* dma operations
> -	 * transform to normal dma_* ones. */
> -	videobuf_queue_sg_init(&icf->vb_vidq, ici->vbq_ops, NULL, icf->lock,
> -				V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
> -				ici->msize, icd);
> -
>  	return 0;
>  
>  	/* All errors are entered with the video_lock held */
>  eiciadd:
> -	lock = icf->lock;
> +	if (ici->ops->file_free)
> +		ici->ops->file_free(icf);
>  	icf->lock = NULL;
> -	if (ici->ops->spinlock_free)
> -		ici->ops->spinlock_free(lock);
>  esla:
>  	module_put(ici->ops->owner);
>  emgi:
> @@ -263,15 +252,14 @@ static int soc_camera_close(struct inode
>  	struct soc_camera_device *icd = icf->icd;
>  	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
>  	struct video_device *vdev = icd->vdev;
> -	spinlock_t *lock = icf->lock;
>  
>  	mutex_lock(&video_lock);
>  	icd->use_count--;
>  	if (!icd->use_count)
>  		ici->ops->remove(icd);
> +	if (ici->ops->file_free)
> +		ici->ops->file_free(icf);
>  	icf->lock = NULL;
> -	if (ici->ops->spinlock_free)
> -		ici->ops->spinlock_free(lock);
>  	module_put(icd->ops->owner);
>  	module_put(ici->ops->owner);
>  	mutex_unlock(&video_lock);
> @@ -772,8 +760,7 @@ int soc_camera_host_register(struct soc_
>  	int ret;
>  	struct soc_camera_host *ix;
>  
> -	if (!ici->vbq_ops || !ici->ops->add || !ici->ops->remove
> -	    || !ici->ops->spinlock_alloc)
> +	if (!ici->ops->add || !ici->ops->remove || !ici->ops->file_alloc)
>  		return -EINVAL;
>  
>  	/* Number might be equal to the platform device ID */
> --- 0001/include/media/soc_camera.h
> +++ work/include/media/soc_camera.h	2008-07-01 13:03:56.000000000 +0900
> @@ -13,7 +13,7 @@
>  #define SOC_CAMERA_H
>  
>  #include <linux/videodev2.h>
> -#include <media/videobuf-dma-sg.h>
> +#include <media/videobuf-core.h>
>  
>  struct soc_camera_device {
>  	struct list_head list;
> @@ -74,8 +74,8 @@ struct soc_camera_host_ops {
>  	int (*try_bus_param)(struct soc_camera_device *, __u32);
>  	int (*set_bus_param)(struct soc_camera_device *, __u32);
>  	unsigned int (*poll)(struct file *, poll_table *);
> -	spinlock_t* (*spinlock_alloc)(struct soc_camera_file *);
> -	void (*spinlock_free)(spinlock_t *);
> +	int (*file_alloc)(struct soc_camera_file *);
> +	void (*file_free)(struct soc_camera_file *);
>  };
>  
>  struct soc_camera_link {
> 
> --
> video4linux-list mailing list
> Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
> https://www.redhat.com/mailman/listinfo/video4linux-list
> 

---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver
  2008-07-01 12:47 ` [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver Magnus Damm
@ 2008-07-01 20:07   ` Guennadi Liakhovetski
  2008-07-02  3:08     ` Magnus Damm
  0 siblings, 1 reply; 16+ messages in thread
From: Guennadi Liakhovetski @ 2008-07-01 20:07 UTC (permalink / raw)
  To: Magnus Damm; +Cc: video4linux-list, lethal, akpm, mchehab, linux-sh

On Tue, 1 Jul 2008, Magnus Damm wrote:

> This patch adds support for the SuperH Mobile CEU interface.
> 
> The hardware is configured in a transparent data fetch mode,
> and frames are captured from the attached camera and written
> to physically contiguous memory buffers provided by the
> videobuf-dma-contig queue.
> 
> Signed-off-by: Magnus Damm <damm@igel.co.jp>

Great to see new soc-camera host drivers! Just out of curiousity - can you 
tell us, with which camera drivers you tested / used it?

Thanks
Guennadi

> ---
> 
>  drivers/media/video/Kconfig                |    8 
>  drivers/media/video/Makefile               |    1 
>  drivers/media/video/sh_mobile_ceu_camera.c |  622 ++++++++++++++++++++++++++++
>  include/asm-sh/sh_mobile_ceu.h             |   10 
>  4 files changed, 641 insertions(+)
> 
> --- 0009/drivers/media/video/Kconfig
> +++ work/drivers/media/video/Kconfig	2008-07-01 18:28:21.000000000 +0900
> @@ -948,4 +948,12 @@ config VIDEO_PXA27x
>  	---help---
>  	  This is a v4l2 driver for the PXA27x Quick Capture Interface
>  
> +config VIDEO_SH_MOBILE_CEU
> +	tristate "SuperH Mobile CEU Interface driver"
> +	depends on VIDEO_DEV && SUPERH
> +	select SOC_CAMERA
> +	select VIDEOBUF_DMA_CONTIG
> +	---help---
> +	  This is a v4l2 driver for the SuperH Mobile CEU Interface
> +
>  endif # VIDEO_CAPTURE_DRIVERS
> --- 0009/drivers/media/video/Makefile
> +++ work/drivers/media/video/Makefile	2008-07-01 18:28:21.000000000 +0900
> @@ -131,6 +131,7 @@ obj-$(CONFIG_VIDEO_VIVI) += vivi.o
>  obj-$(CONFIG_VIDEO_CX23885) += cx23885/
>  
>  obj-$(CONFIG_VIDEO_PXA27x)	+= pxa_camera.o
> +obj-$(CONFIG_VIDEO_SH_MOBILE_CEU)	+= sh_mobile_ceu_camera.o
>  obj-$(CONFIG_SOC_CAMERA)	+= soc_camera.o
>  obj-$(CONFIG_SOC_CAMERA_MT9M001)	+= mt9m001.o
>  obj-$(CONFIG_SOC_CAMERA_MT9V022)	+= mt9v022.o
> --- /dev/null
> +++ work/drivers/media/video/sh_mobile_ceu_camera.c	2008-07-01 20:32:07.000000000 +0900
> @@ -0,0 +1,622 @@
> +/*
> + * V4L2 Driver for SuperH Mobile CEU interface
> + *
> + * Copyright (C) 2008 Magnus Damm
> + *
> + * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
> + *
> + * Copyright (C) 2006, Sascha Hauer, Pengutronix
> + * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
> + *
> + * 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/init.h>
> +#include <linux/module.h>
> +#include <linux/io.h>
> +#include <linux/delay.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/errno.h>
> +#include <linux/fs.h>
> +#include <linux/interrupt.h>
> +#include <linux/kernel.h>
> +#include <linux/mm.h>
> +#include <linux/moduleparam.h>
> +#include <linux/time.h>
> +#include <linux/version.h>
> +#include <linux/device.h>
> +#include <linux/platform_device.h>
> +#include <linux/mutex.h>
> +
> +#include <media/v4l2-common.h>
> +#include <media/v4l2-dev.h>
> +#include <media/soc_camera.h>
> +#include <media/videobuf-dma-contig.h>
> +
> +#include <linux/videodev2.h>
> +
> +#include <asm/sh_mobile_ceu.h>
> +
> +/* register offsets for sh7722 / sh7723 */
> +
> +#define CAPSR  0x00
> +#define CAPCR  0x04
> +#define CAMCR  0x08
> +#define CMCYR  0x0c
> +#define CAMOR  0x10
> +#define CAPWR  0x14
> +#define CAIFR  0x18
> +#define CSTCR  0x20 /* not on sh7723 */
> +#define CSECR  0x24 /* not on sh7723 */
> +#define CRCNTR 0x28
> +#define CRCMPR 0x2c
> +#define CFLCR  0x30
> +#define CFSZR  0x34
> +#define CDWDR  0x38
> +#define CDAYR  0x3c
> +#define CDACR  0x40
> +#define CDBYR  0x44
> +#define CDBCR  0x48
> +#define CBDSR  0x4c
> +#define CFWCR  0x5c
> +#define CLFCR  0x60
> +#define CDOCR  0x64
> +#define CDDCR  0x68
> +#define CDDAR  0x6c
> +#define CEIER  0x70
> +#define CETCR  0x74
> +#define CSTSR  0x7c
> +#define CSRTR  0x80
> +#define CDSSR  0x84
> +#define CDAYR2 0x90
> +#define CDACR2 0x94
> +#define CDBYR2 0x98
> +#define CDBCR2 0x9c
> +
> +static DEFINE_MUTEX(camera_lock);
> +
> +/* per video frame buffer */
> +struct sh_mobile_ceu_buffer {
> +	struct videobuf_buffer vb; /* v4l buffer must be first */
> +	const struct soc_camera_data_format *fmt;
> +};
> +
> +struct sh_mobile_ceu_dev {
> +	struct device *dev;
> +	struct soc_camera_host ici;
> +	struct soc_camera_device *icd;
> +
> +	unsigned int irq;
> +	void __iomem *base;
> +
> +	spinlock_t lock;
> +	struct list_head capture;
> +	struct videobuf_buffer *active;
> +
> +	struct sh_mobile_ceu_info *pdata;
> +};
> +
> +static unsigned int vid_limit = 4;	/* Video memory limit, in Mb */
> +
> +static void ceu_write(struct sh_mobile_ceu_dev *priv,
> +		      unsigned long reg_offs, unsigned long data)
> +{
> +	iowrite32(data, priv->base + reg_offs);
> +}
> +
> +static unsigned long ceu_read(struct sh_mobile_ceu_dev *priv,
> +			      unsigned long reg_offs)
> +{
> +	return ioread32(priv->base + reg_offs);
> +}
> +
> +/*
> + *  Videobuf operations
> + */
> +static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
> +					unsigned int *count,
> +					unsigned int *size)
> +{
> +	struct soc_camera_device *icd = vq->priv_data;
> +	int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
> +
> +	*size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
> +
> +	if (0 = *count)
> +		*count = 2;
> +	while (*size * *count > vid_limit * 1024 * 1024)
> +		(*count)--;
> +
> +	dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
> +
> +	return 0;
> +}
> +
> +static void free_buffer(struct videobuf_queue *vq,
> +			struct sh_mobile_ceu_buffer *buf)
> +{
> +	struct soc_camera_device *icd = vq->priv_data;
> +
> +	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
> +		&buf->vb, buf->vb.baddr, buf->vb.bsize);
> +
> +	if (in_interrupt())
> +		BUG();
> +
> +	videobuf_dma_contig_free(vq, &buf->vb);
> +	dev_dbg(&icd->dev, "%s freed\n", __func__);
> +	buf->vb.state = VIDEOBUF_NEEDS_INIT;
> +}
> +
> +static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
> +{
> +	struct videobuf_dma_contig_memory *mem;
> +
> +	ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~1);
> +	ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & 0x0317f313);
> +	ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | 1);
> +
> +	ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~0x10000);
> +
> +	ceu_write(pcdev, CETCR, 0x0317f313 ^ 0x10);
> +
> +	if (pcdev->active) {
> +		mem = videobuf_to_dma_contig(pcdev->active);
> +
> +		ceu_write(pcdev, CDAYR, mem->dma_handle);
> +		ceu_write(pcdev, CAPSR, 0x1); /* start capture */
> +	}
> +}
> +
> +static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
> +					  struct videobuf_buffer *vb,
> +					  enum v4l2_field field)
> +{
> +	struct soc_camera_device *icd = vq->priv_data;
> +	struct sh_mobile_ceu_buffer *buf;
> +	int ret;
> +
> +	buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
> +
> +	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
> +		vb, vb->baddr, vb->bsize);
> +
> +	/* Added list head initialization on alloc */
> +	WARN_ON(!list_empty(&vb->queue));
> +
> +#ifdef DEBUG
> +	/* This can be useful if you want to see if we actually fill
> +	 * the buffer with something */
> +	memset((void *)vb->baddr, 0xaa, vb->bsize);
> +#endif
> +
> +	BUG_ON(NULL = icd->current_fmt);
> +
> +	if (buf->fmt	!= icd->current_fmt ||
> +	    vb->width	!= icd->width ||
> +	    vb->height	!= icd->height ||
> +	    vb->field	!= field) {
> +		buf->fmt	= icd->current_fmt;
> +		vb->width	= icd->width;
> +		vb->height	= icd->height;
> +		vb->field	= field;
> +		vb->state	= VIDEOBUF_NEEDS_INIT;
> +	}
> +
> +	vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
> +	if (0 != vb->baddr && vb->bsize < vb->size) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
> +
> +	if (vb->state = VIDEOBUF_NEEDS_INIT) {
> +		ret = videobuf_iolock(vq, vb, NULL);
> +		if (ret)
> +			goto fail;
> +		vb->state = VIDEOBUF_PREPARED;
> +	}
> +
> +	return 0;
> +fail:
> +	free_buffer(vq, buf);
> +out:
> +	return ret;
> +}
> +
> +static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
> +					 struct videobuf_buffer *vb)
> +{
> +	struct soc_camera_device *icd = vq->priv_data;
> +	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +	unsigned long flags;
> +
> +	dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
> +		vb, vb->baddr, vb->bsize);
> +
> +	vb->state = VIDEOBUF_ACTIVE;
> +	spin_lock_irqsave(&pcdev->lock, flags);
> +	list_add_tail(&vb->queue, &pcdev->capture);
> +
> +	if (!pcdev->active) {
> +		pcdev->active = vb;
> +		sh_mobile_ceu_capture(pcdev);
> +	}
> +
> +	spin_unlock_irqrestore(&pcdev->lock, flags);
> +}
> +
> +static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
> +					   struct videobuf_buffer *vb)
> +{
> +	free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
> +}
> +
> +static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
> +	.buf_setup      = sh_mobile_ceu_videobuf_setup,
> +	.buf_prepare    = sh_mobile_ceu_videobuf_prepare,
> +	.buf_queue      = sh_mobile_ceu_videobuf_queue,
> +	.buf_release    = sh_mobile_ceu_videobuf_release,
> +};
> +
> +static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
> +{
> +	struct sh_mobile_ceu_dev *pcdev = data;
> +	struct videobuf_buffer *vb;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&pcdev->lock, flags);
> +
> +	vb = pcdev->active;
> +	list_del_init(&vb->queue);
> +
> +	if (!list_empty(&pcdev->capture))
> +		pcdev->active = list_entry(pcdev->capture.next,
> +					   struct videobuf_buffer, queue);
> +	else
> +		pcdev->active = NULL;
> +
> +	sh_mobile_ceu_capture(pcdev);
> +
> +	vb->state = VIDEOBUF_DONE;
> +	do_gettimeofday(&vb->ts);
> +	vb->field_count++;
> +	wake_up(&vb->done);
> +	spin_unlock_irqrestore(&pcdev->lock, flags);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
> +{
> +	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +	int ret = -EBUSY;
> +
> +	mutex_lock(&camera_lock);
> +
> +	if (pcdev->icd)
> +		goto err;
> +
> +	dev_info(&icd->dev,
> +		 "SuperH Mobile CEU driver attached to camera %d\n",
> +		 icd->devnum);
> +
> +	ret = icd->ops->init(icd);
> +	if (ret)
> +		goto err;
> +
> +	ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
> +	while (ceu_read(pcdev, CSTSR) & 1)
> +		msleep(1);
> +
> +	pcdev->icd = icd;
> +err:
> +	mutex_unlock(&camera_lock);
> +
> +	return ret;
> +}
> +
> +static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
> +{
> +	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +
> +	BUG_ON(icd != pcdev->icd);
> +
> +	dev_info(&icd->dev,
> +		 "SuperH Mobile CEU driver detached from camera %d\n",
> +		 icd->devnum);
> +
> +	/* disable capture, disable interrupts */
> +	ceu_write(pcdev, CEIER, 0);
> +	ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
> +	icd->ops->release(icd);
> +	pcdev->icd = NULL;
> +}
> +
> +static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
> +				       __u32 pixfmt)
> +{
> +	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +	int ret, buswidth, width, cfszr_width, cdwdr_width;
> +	unsigned long camera_flags, common_flags, value;
> +
> +	camera_flags = icd->ops->query_bus_param(icd);
> +	common_flags = soc_camera_bus_param_compatible(camera_flags,
> +						       pcdev->pdata->flags);
> +	if (!common_flags)
> +		return -EINVAL;
> +
> +	ret = icd->ops->set_bus_param(icd, common_flags);
> +	if (ret < 0)
> +		return ret;
> +
> +	switch (common_flags & SOCAM_DATAWIDTH_MASK) {
> +	case SOCAM_DATAWIDTH_8: buswidth = 8; break;
> +	case SOCAM_DATAWIDTH_16: buswidth = 16; break;
> +	default: return -EINVAL;
> +	}
> +
> +	ceu_write(pcdev, CRCNTR, 0);
> +	ceu_write(pcdev, CRCMPR, 0);
> +
> +	value = 0x00000010;
> +	value |= (common_flags & SOCAM_VSYNC_ACTIVE_LOW) ? (1 << 1) : 0;
> +	value |= (common_flags & SOCAM_HSYNC_ACTIVE_LOW) ? (1 << 0) : 0;
> +	value |= (buswidth = 16) ? (1 << 12) : 0;
> +	ceu_write(pcdev, CAMCR, value);
> +
> +	ceu_write(pcdev, CAPCR, 0x00300000);
> +	ceu_write(pcdev, CAIFR, 0);
> +
> +	mdelay(1);
> +
> +	width = icd->width * (icd->current_fmt->depth / 8);
> +	width = (buswidth = 16) ? width / 2 : width;
> +	cfszr_width = (buswidth = 8) ? width / 2 : width;
> +	cdwdr_width = (buswidth = 16) ? width * 2 : width;
> +
> +	ceu_write(pcdev, CAMOR, 0);
> +	ceu_write(pcdev, CAPWR, (icd->height << 16) | width);
> +	ceu_write(pcdev, CFLCR, 0); /* data fetch mode - no scaling */
> +	ceu_write(pcdev, CFSZR, (icd->height << 16) | cfszr_width);
> +	ceu_write(pcdev, CLFCR, 0); /* data fetch mode - no lowpass filter */
> +	ceu_write(pcdev, CDOCR, 0x00000016);
> +
> +	ceu_write(pcdev, CDWDR, cdwdr_width);
> +	ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
> +
> +	/* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
> +	/* in data fetch mode: no need for CDACR, CDBYR, CDBCR */
> +
> +	return 0;
> +}
> +
> +static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
> +				       __u32 pixfmt)
> +{
> +	struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +	unsigned long camera_flags, common_flags;
> +
> +	camera_flags = icd->ops->query_bus_param(icd);
> +	common_flags = soc_camera_bus_param_compatible(camera_flags,
> +						       pcdev->pdata->flags);
> +	if (!common_flags)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +static int sh_mobile_ceu_set_fmt_cap(struct soc_camera_device *icd,
> +				     __u32 pixfmt, struct v4l2_rect *rect)
> +{
> +	return icd->ops->set_fmt_cap(icd, pixfmt, rect);
> +}
> +
> +static int sh_mobile_ceu_try_fmt_cap(struct soc_camera_device *icd,
> +				     struct v4l2_format *f)
> +{
> +	/* FIXME: calculate using depth and bus width */
> +
> +	if (f->fmt.pix.height < 4)
> +		f->fmt.pix.height = 4;
> +	if (f->fmt.pix.height > 1920)
> +		f->fmt.pix.height = 1920;
> +	if (f->fmt.pix.width < 2)
> +		f->fmt.pix.width = 2;
> +	if (f->fmt.pix.width > 2560)
> +		f->fmt.pix.width = 2560;
> +	f->fmt.pix.width &= ~0x01;
> +	f->fmt.pix.height &= ~0x03;
> +
> +	/* limit to sensor capabilities */
> +	return icd->ops->try_fmt_cap(icd, f);
> +}
> +
> +static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
> +				 struct v4l2_requestbuffers *p)
> +{
> +	int i;
> +
> +	/* This is for locking debugging only. I removed spinlocks and now I
> +	 * check whether .prepare is ever called on a linked buffer, or whether
> +	 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
> +	 * it hadn't triggered */
> +	for (i = 0; i < p->count; i++) {
> +		struct sh_mobile_ceu_buffer *buf;
> +
> +		buf = container_of(icf->vb_vidq.bufs[i],
> +				   struct sh_mobile_ceu_buffer, vb);
> +		INIT_LIST_HEAD(&buf->vb.queue);
> +	}
> +
> +	return 0;
> +}
> +
> +static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
> +{
> +	struct soc_camera_file *icf = file->private_data;
> +	struct sh_mobile_ceu_buffer *buf;
> +
> +	buf = list_entry(icf->vb_vidq.stream.next,
> +			 struct sh_mobile_ceu_buffer, vb.stream);
> +
> +	poll_wait(file, &buf->vb.done, pt);
> +
> +	if (buf->vb.state = VIDEOBUF_DONE ||
> +	    buf->vb.state = VIDEOBUF_ERROR)
> +		return POLLIN|POLLRDNORM;
> +
> +	return 0;
> +}
> +
> +static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
> +				  struct v4l2_capability *cap)
> +{
> +	strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
> +	cap->version = KERNEL_VERSION(0, 0, 5);
> +	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
> +	return 0;
> +}
> +
> +static int sh_mobile_ceu_file_alloc(struct soc_camera_file *icf)
> +{
> +	struct soc_camera_host *ici = to_soc_camera_host(icf->icd->dev.parent);
> +	struct sh_mobile_ceu_dev *pcdev = ici->priv;
> +
> +	videobuf_queue_dma_contig_init(&icf->vb_vidq,
> +				       &sh_mobile_ceu_videobuf_ops,
> +				       &ici->dev, &pcdev->lock,
> +				       V4L2_BUF_TYPE_VIDEO_CAPTURE,
> +				       V4L2_FIELD_NONE,
> +				       sizeof(struct sh_mobile_ceu_buffer),
> +				       icf->icd);
> +	return 0;
> +}
> +
> +static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
> +	.owner		= THIS_MODULE,
> +	.add		= sh_mobile_ceu_add_device,
> +	.remove		= sh_mobile_ceu_remove_device,
> +	.set_fmt_cap	= sh_mobile_ceu_set_fmt_cap,
> +	.try_fmt_cap	= sh_mobile_ceu_try_fmt_cap,
> +	.reqbufs	= sh_mobile_ceu_reqbufs,
> +	.poll		= sh_mobile_ceu_poll,
> +	.querycap	= sh_mobile_ceu_querycap,
> +	.try_bus_param	= sh_mobile_ceu_try_bus_param,
> +	.set_bus_param	= sh_mobile_ceu_set_bus_param,
> +	.file_alloc	= sh_mobile_ceu_file_alloc,
> +};
> +
> +static int sh_mobile_ceu_probe(struct platform_device *pdev)
> +{
> +	struct sh_mobile_ceu_dev *pcdev;
> +	struct resource *res;
> +	void __iomem *base;
> +	unsigned int irq;
> +	int err = 0;
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	irq = platform_get_irq(pdev, 0);
> +	if (!res || !irq) {
> +		err = -ENODEV;
> +		goto exit;
> +	}
> +
> +	pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
> +	if (!pcdev) {
> +		dev_err(&pdev->dev, "Could not allocate pcdev\n");
> +		err = -ENOMEM;
> +		goto exit;
> +	}
> +
> +	platform_set_drvdata(pdev, pcdev);
> +	INIT_LIST_HEAD(&pcdev->capture);
> +	spin_lock_init(&pcdev->lock);
> +
> +	pcdev->pdata = pdev->dev.platform_data;
> +	if (!pcdev->pdata) {
> +		err = -EINVAL;
> +		goto exit_kfree;
> +	}
> +
> +	base = ioremap_nocache(res->start, res->end - res->start + 1);
> +	if (!base) {
> +		err = -ENXIO;
> +		goto exit_kfree;
> +	}
> +	pcdev->irq = irq;
> +	pcdev->base = base;
> +	pcdev->dev = &pdev->dev;
> +
> +	/* request irq */
> +	err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
> +			  pdev->dev.bus_id, pcdev);
> +	if (err) {
> +		dev_err(pcdev->dev, "Camera interrupt register failed \n");
> +		goto exit_iounmap;
> +	}
> +
> +	pcdev->ici.priv = pcdev;
> +	pcdev->ici.dev.parent = &pdev->dev;
> +	pcdev->ici.nr = pdev->id;
> +	pcdev->ici.drv_name = pdev->dev.bus_id,
> +	pcdev->ici.ops = &sh_mobile_ceu_host_ops,
> +
> +	err = soc_camera_host_register(&pcdev->ici);
> +	if (err)
> +		goto exit_free_irq;
> +
> +	return 0;
> +
> +exit_free_irq:
> +	free_irq(pcdev->irq, pcdev);
> +exit_iounmap:
> +	iounmap(base);
> +exit_kfree:
> +	kfree(pcdev);
> +exit:
> +	return err;
> +}
> +
> +static int sh_mobile_ceu_remove(struct platform_device *pdev)
> +{
> +	struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
> +
> +	soc_camera_host_unregister(&pcdev->ici);
> +	free_irq(pcdev->irq, pcdev);
> +	iounmap(pcdev->base);
> +	kfree(pcdev);
> +	return 0;
> +}
> +
> +static struct platform_driver sh_mobile_ceu_driver = {
> +	.driver 	= {
> +		.name	= "sh_mobile_ceu",
> +	},
> +	.probe		= sh_mobile_ceu_probe,
> +	.remove		= sh_mobile_ceu_remove,
> +};
> +
> +static int __init sh_mobile_ceu_init(void)
> +{
> +	return platform_driver_register(&sh_mobile_ceu_driver);
> +}
> +
> +static void __exit sh_mobile_ceu_exit(void)
> +{
> +	return platform_driver_unregister(&sh_mobile_ceu_driver);
> +}
> +
> +module_init(sh_mobile_ceu_init);
> +module_exit(sh_mobile_ceu_exit);
> +
> +MODULE_DESCRIPTION("SuperH Mobile CEU driver");
> +MODULE_AUTHOR("Magnus Damm");
> +MODULE_LICENSE("GPL");
> --- /dev/null
> +++ work/include/asm-sh/sh_mobile_ceu.h	2008-07-01 18:28:21.000000000 +0900
> @@ -0,0 +1,10 @@
> +#ifndef __ASM_SH_MOBILE_CEU_H__
> +#define __ASM_SH_MOBILE_CEU_H__
> +
> +#include <media/soc_camera.h>
> +
> +struct sh_mobile_ceu_info {
> +	unsigned long flags; /* SOCAM_... */
> +};
> +
> +#endif /* __ASM_SH_MOBILE_CEU_H__ */
> 
> --
> video4linux-list mailing list
> Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
> https://www.redhat.com/mailman/listinfo/video4linux-list
> 

---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 02/07] soc_camera: Let the host select videobuf_queue type
  2008-07-01 20:02   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Guennadi Liakhovetski
@ 2008-07-02  2:50     ` Magnus Damm
  0 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-02  2:50 UTC (permalink / raw)
  To: Guennadi Liakhovetski
  Cc: video4linux-list, linux-sh, lethal, mchehab, paulius.zaleckas,
	akpm

[CC Paulius]

On Wed, Jul 2, 2008 at 5:02 AM, Guennadi Liakhovetski
<g.liakhovetski@gmx.de> wrote:
> On Tue, 1 Jul 2008, Magnus Damm wrote:
>
>> This patch makes it possible for hosts (soc_camera drivers for the soc)
>> to select a different videobuf queue than VIDEOBUF_DMA_SG. This is needed
>> by the SuperH Mobile CEU hardware which requires physically contiguous
>> buffers. While at it, rename the spinlock callbacks to file callbacks.
>>
>> Signed-off-by: Magnus Damm <damm@igel.co.jp>
>
> I'm afraid, this patch conflicts with an earlier one by Paulius Zaleckas:
> http://marc.info/?l=linux-video&m\x121438688924771&w=2, which I've already
> acked, and which, I think, makes videobuf handling more generic than
> yours. Could you please have a look if you can use it, if yes - rebase
> your patch-set on it (I think, some other your patches will have to be
> changed then too). If there's anything that you cannot use there, we'll
> have to see how we can satisfy all the requirements.

My patches are similar to Paulius change above, but I also clean up
the spinlock handling - since the videobuf queue stuff is moved into
the host driver there is no longer any point to keep the spinlock
callbacks and the icf->lock pointer inside soc_camera.c.

If you're interested in cleaning up the spinlock stuff then it's a tad
difficult to do that on top of Paulius code IMO. I'd say my patches
01-04 does the same as Paulis patch, but also removes redundant
spinlock code.

Let me know what you think. I'm more than happy to build on top of
Paulius patch, but I wonder if we really should keep the redundant
spinlock code...

Thanks!

/ magnus

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver
  2008-07-01 20:07   ` Guennadi Liakhovetski
@ 2008-07-02  3:08     ` Magnus Damm
  0 siblings, 0 replies; 16+ messages in thread
From: Magnus Damm @ 2008-07-02  3:08 UTC (permalink / raw)
  To: Guennadi Liakhovetski; +Cc: video4linux-list, lethal, akpm, mchehab, linux-sh

On Wed, Jul 2, 2008 at 5:07 AM, Guennadi Liakhovetski
<g.liakhovetski@gmx.de> wrote:
> On Tue, 1 Jul 2008, Magnus Damm wrote:
>
>> This patch adds support for the SuperH Mobile CEU interface.
>>
>> The hardware is configured in a transparent data fetch mode,
>> and frames are captured from the attached camera and written
>> to physically contiguous memory buffers provided by the
>> videobuf-dma-contig queue.
>>
>> Signed-off-by: Magnus Damm <damm@igel.co.jp>
>
> Great to see new soc-camera host drivers! Just out of curiousity - can you
> tell us, with which camera drivers you tested / used it?

Uh, I knew that question would come up... =)

So far I've mainly tried the SuperH Mobile CEU driver on a MigoR QVGA
board, which means a sh7722 processor (which includes the CEU) and a
ov7725 camera. I also have a sh7723 board with a ncm03j camera (i
think, no docs), but that combination isn't up an running yet.

As for camera drivers, I'm sorry to say that I've been using hacked-up
out-of-tree drivers. I'd like to add upstream support for both camera
types, but I'm not sure if I have enough time before the merge window
closes for 2.6.27. So I've prioritized the CEU driver so far since
it's easy to reuse.

Regarding the ov7725 driver - there seem to be quite a few
half-fitting candidates out there already. I'm thinking about the
ovcamchip/ code and the ov7670.c driver. I need something for the
soc_camera framework though. What to do? Any recommendations?

Thanks,

/ magnus

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 02/07] soc_camera: Let the host select videobuf_queue
  2008-07-01 12:46 ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
  2008-07-01 20:02   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Guennadi Liakhovetski
@ 2008-07-02  7:27   ` Paulius Zaleckas
  1 sibling, 0 replies; 16+ messages in thread
From: Paulius Zaleckas @ 2008-07-02  7:27 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh

Magnus Damm wrote:
> This patch makes it possible for hosts (soc_camera drivers for the soc)
> to select a different videobuf queue than VIDEOBUF_DMA_SG. This is needed
> by the SuperH Mobile CEU hardware which requires physically contiguous
> buffers. While at it, rename the spinlock callbacks to file callbacks.
> 
> Signed-off-by: Magnus Damm <damm@igel.co.jp>
> ---
> 
>  drivers/media/video/Kconfig      |    4 ++--
>  drivers/media/video/pxa_camera.c |   15 ++++++++++++---
>  drivers/media/video/soc_camera.c |   27 +++++++--------------------
>  include/media/soc_camera.h       |    6 +++---
>  4 files changed, 24 insertions(+), 28 deletions(-)
> 
> --- 0001/drivers/media/video/Kconfig
> +++ work/drivers/media/video/Kconfig	2008-07-01 13:05:48.000000000 +0900
> @@ -901,8 +901,7 @@ endif # V4L_USB_DRIVERS
>  
>  config SOC_CAMERA
>  	tristate "SoC camera support"
> -	depends on VIDEO_V4L2 && HAS_DMA
> -	select VIDEOBUF_DMA_SG
> +	depends on VIDEO_V4L2

Bug here. You won't be able to compile soc_camera without host driver
or host driver as module. SOC_CAMERA has to select VIDEOBUF_GEN!

>  	help
>  	  SoC Camera is a common API to several cameras, not connecting
>  	  over a bus like PCI or USB. For example some i2c camera connected


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 06/07] videobuf: Add physically contiguous queue code
  2008-07-01 12:47 ` [PATCH 06/07] videobuf: Add physically contiguous queue code Magnus Damm
@ 2008-07-02  7:43   ` Paulius Zaleckas
       [not found]     ` <aec7e5c30807020232j1181ba9s43bc0e6920b18733@mail.gmail.com>
  2008-07-04  9:08   ` Paulius Zaleckas
  1 sibling, 1 reply; 16+ messages in thread
From: Paulius Zaleckas @ 2008-07-02  7:43 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh

Heh. I have written almost identical videobuf driver also :)
You should run checkpatch.pl on this patch to correct some style
problems. Since your version is a little bit more generic than mine:

Acked-by: Paulius Zaleckas <paulius.zaleckas@teltonika.lt>

Magnus Damm wrote:
> This patch adds support for videobuf queues made from physically
> contiguous memory. Useful for hardware such as the SuperH Mobile CEU
> which doesn't support scatter gatter bus mastering.
> 
> Since it may be difficult to allocate large chunks of physically
> contiguous memory after some uptime due to fragmentation, this code
> allocates memory using dma_alloc_coherent(). Architectures supporting
> dma_declare_coherent_memory() can easily avoid fragmentation issues
> by using dma_declare_coherent_memory() to force dma_alloc_coherent()
> to allocate from a certain pre-allocated memory area.
> 
> Signed-off-by: Magnus Damm <damm@igel.co.jp>


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 06/07] videobuf: Add physically contiguous queue code
       [not found]     ` <aec7e5c30807020232j1181ba9s43bc0e6920b18733@mail.gmail.com>
@ 2008-07-02 10:42       ` Paulius Zaleckas
  0 siblings, 0 replies; 16+ messages in thread
From: Paulius Zaleckas @ 2008-07-02 10:42 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh

Magnus Damm wrote:
> On Wed, Jul 2, 2008 at 4:43 PM, Paulius Zaleckas
> <paulius.zaleckas@teltonika.lt> wrote:
>> Heh. I have written almost identical videobuf driver also :)
>> You should run checkpatch.pl on this patch to correct some style
>> problems. Since your version is a little bit more generic than mine:
>>
>> Acked-by: Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
> 
> Thanks for your ack. Just curious, which checkpatch version are you
> using? From the linux-next tree? I've checked my patches with the less
> agressive checkpatch included in the linux-2.6 tree. =)

Strange why checkpatch.pl didn't catch this:

+static void *__videobuf_alloc(size_t size)
+{
+	struct videobuf_dma_contig_memory *mem;
+	struct videobuf_buffer *vb;
+
+	vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
+	if (vb) {
+		mem = vb->priv = ((char *)vb)+size;

Should be	mem = vb->priv = ((char *)vb) + size;

+		mem->magic = MAGIC_DC_MEM;
+	}
+
+	return vb;
+}


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 06/07] videobuf: Add physically contiguous queue code
  2008-07-01 12:47 ` [PATCH 06/07] videobuf: Add physically contiguous queue code Magnus Damm
  2008-07-02  7:43   ` Paulius Zaleckas
@ 2008-07-04  9:08   ` Paulius Zaleckas
  1 sibling, 0 replies; 16+ messages in thread
From: Paulius Zaleckas @ 2008-07-04  9:08 UTC (permalink / raw)
  To: video4linux-list; +Cc: linux-sh

Here goes my full review:

Magnus Damm wrote:
> This patch adds support for videobuf queues made from physically
> contiguous memory. Useful for hardware such as the SuperH Mobile CEU
> which doesn't support scatter gatter bus mastering.
> 
> Since it may be difficult to allocate large chunks of physically
> contiguous memory after some uptime due to fragmentation, this code
> allocates memory using dma_alloc_coherent(). Architectures supporting
> dma_declare_coherent_memory() can easily avoid fragmentation issues
> by using dma_declare_coherent_memory() to force dma_alloc_coherent()
> to allocate from a certain pre-allocated memory area.
> 
> Signed-off-by: Magnus Damm <damm@igel.co.jp>
> ---
> 
>  drivers/media/video/Kconfig               |    4 
>  drivers/media/video/Makefile              |    1 
>  drivers/media/video/videobuf-dma-contig.c |  413 +++++++++++++++++++++++++++++
>  include/media/videobuf-dma-contig.h       |   39 ++
>  4 files changed, 457 insertions(+)
> 
> --- 0006/drivers/media/video/Kconfig
> +++ work/drivers/media/video/Kconfig	2008-06-30 15:30:35.000000000 +0900
> @@ -24,6 +24,10 @@ config VIDEOBUF_VMALLOC
>  	select VIDEOBUF_GEN
>  	tristate
>  
> +config VIDEOBUF_DMA_CONTIG
> +	select VIDEOBUF_GEN
> +	tristate
> +

Maybe "depends on HAS_DMA" is needed here?

>  config VIDEOBUF_DVB
>  	tristate
>  	select VIDEOBUF_GEN
> --- 0001/drivers/media/video/Makefile
> +++ work/drivers/media/video/Makefile	2008-06-30 15:30:35.000000000 +0900
> @@ -88,6 +88,7 @@ obj-$(CONFIG_VIDEO_TUNER) += tuner.o
>  
>  obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o
>  obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o
> +obj-$(CONFIG_VIDEOBUF_DMA_CONTIG) += videobuf-dma-contig.o
>  obj-$(CONFIG_VIDEOBUF_VMALLOC) += videobuf-vmalloc.o
>  obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o
>  obj-$(CONFIG_VIDEO_BTCX)  += btcx-risc.o
> --- /dev/null
> +++ work/drivers/media/video/videobuf-dma-contig.c	2008-06-30 15:36:13.000000000 +0900
> @@ -0,0 +1,413 @@
> +/*
> + * helper functions for physically contiguous capture buffers
> + *
> + * The functions support hardware lacking scatter gatter support
> + * (i.e. the buffers must be linear in physical memory)
> + *
> + * Copyright (c) 2008 Magnus Damm
> + *
> + * Based on videobuf-vmalloc.c,
> + * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
> + *
> + * 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
> + */
> +
> +#include <linux/init.h>
> +#include <linux/module.h>
> +#include <linux/dma-mapping.h>
> +#include <media/videobuf-dma-contig.h>
> +
> +#define MAGIC_DC_MEM 0x0733ac61
> +#define MAGIC_CHECK(is, should)						\
> +	if (unlikely((is) != (should)))	{				\
> +		pr_err("magic mismatch: %x expected %x\n", is, should); \
> +		BUG();							\
> +	}
> +
> +static void
> +videobuf_vm_open(struct vm_area_struct *vma)
> +{
> +	struct videobuf_mapping *map = vma->vm_private_data;
> +
> +	dev_dbg(map->q->dev, "vm_open %p [count=%u,vma=%08lx-%08lx]\n",
> +		map, map->count, vma->vm_start, vma->vm_end);
> +
> +	map->count++;
> +}
> +
> +static void videobuf_vm_close(struct vm_area_struct *vma)
> +{
> +	struct videobuf_mapping *map = vma->vm_private_data;
> +	struct videobuf_queue *q = map->q;
> +	int i, datasize;
> +
> +	dev_dbg(map->q->dev, "vm_close %p [count=%u,vma=%08lx-%08lx]\n",
> +		map, map->count, vma->vm_start, vma->vm_end);
> +
> +	map->count--;
> +	if (0 = map->count) {
> +		struct videobuf_dma_contig_memory *mem;
> +
> +		dev_dbg(map->q->dev, "munmap %p q=%p\n", map, q);
> +		mutex_lock(&q->vb_lock);
> +
> +		/* We need first to cancel streams, before unmapping */
> +		if (q->streaming)
> +			videobuf_queue_cancel(q);
> +
> +		for (i = 0; i < VIDEO_MAX_FRAME; i++) {
> +			if (NULL = q->bufs[i])

			if (!q->bufs[i])

> +				continue;
> +
> +			if (q->bufs[i]->map != map)
> +				continue;
> +
> +			mem = q->bufs[i]->priv;
> +			if (mem) {
> +				/* This callback is called only if kernel has
> +				   allocated memory and this memory is mmapped.
> +				   In this case, memory should be freed,
> +				   in order to do memory unmap.
> +				 */
> +
> +				MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +				/* vfree is not atomic - can't be
> +				   called with IRQ's disabled
> +				 */
> +				dev_dbg(map->q->dev, "buf[%d] freeing %p\n",
> +					i, mem->vaddr);
> +
> +				datasize = PAGE_ALIGN(q->bufs[i]->size);
> +				dma_free_coherent(q->dev, datasize,
> +						  mem->vaddr, mem->dma_handle);
> +				mem->vaddr = NULL;
> +			}
> +
> +			q->bufs[i]->map   = NULL;
> +			q->bufs[i]->baddr = 0;
> +		}
> +
> +		kfree(map);
> +
> +		mutex_unlock(&q->vb_lock);
> +	}
> +
> +	return;

return not needed here.

> +}
> +
> +static struct vm_operations_struct videobuf_vm_ops = {
> +	.open     = videobuf_vm_open,
> +	.close    = videobuf_vm_close,
> +};
> +
> +static void *__videobuf_alloc(size_t size)
> +{
> +	struct videobuf_dma_contig_memory *mem;
> +	struct videobuf_buffer *vb;
> +
> +	vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
> +	if (vb) {
> +		mem = vb->priv = ((char *)vb)+size;

		mem = vb->priv = ((char *)vb) + size;

> +		mem->magic = MAGIC_DC_MEM;
> +	}
> +
> +	return vb;
> +}
> +
> +static void *__videobuf_to_vmalloc(struct videobuf_buffer *buf)
> +{
> +	struct videobuf_dma_contig_memory *mem = buf->priv;
> +
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	return mem->vaddr;
> +}
> +
> +static int __videobuf_iolock(struct videobuf_queue *q,
> +			     struct videobuf_buffer *vb,
> +			     struct v4l2_framebuffer *fbuf)
> +{
> +	struct videobuf_dma_contig_memory *mem = vb->priv;
> +	int datasize;
> +
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	switch (vb->memory) {
> +	case V4L2_MEMORY_MMAP:
> +		dev_dbg(q->dev, "%s memory method MMAP\n", __func__);
> +
> +		/* All handling should be done by __videobuf_mmap_mapper() */
> +		if (!mem->vaddr) {
> +			pr_err("memory is not alloced/mmapped.\n");

			why not dev_err?

> +			return -EINVAL;
> +		}
> +		break;
> +	case V4L2_MEMORY_USERPTR:
> +		dev_dbg(q->dev, "%s memory method USERPTR\n", __func__);
> +
> +		/* The only USERPTR currently supported is the one needed for
> +		   read() method.
> +		 */
> +		if (vb->baddr)
> +			return -EINVAL;
> +
> +		datasize = PAGE_ALIGN(vb->size);
> +		mem->vaddr = dma_alloc_coherent(q->dev, datasize,
> +						&mem->dma_handle, GFP_KERNEL);
> +		if (!mem->vaddr) {
> +			pr_err("dma_alloc_coherent %d failed\n", datasize);

			dev_err

> +			return -ENOMEM;
> +		}
> +
> +		dev_dbg(q->dev, "dma_alloc_coherent data is at %p (%d)\n",
> +			mem->vaddr, datasize);
> +		break;
> +	case V4L2_MEMORY_OVERLAY:
> +	default:
> +		dev_dbg(q->dev, "%s memory method OVERLAY/unknown\n",
> +			__func__);
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int __videobuf_sync(struct videobuf_queue *q,
> +			   struct videobuf_buffer *buf)
> +{

Maybe dma_sync_single_for_cpu should be called here?

> +	return 0;
> +}
> +
> +static int __videobuf_mmap_free(struct videobuf_queue *q)
> +{
> +	unsigned int i;
> +
> +	dev_dbg(q->dev, "%s\n", __func__);
> +	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
> +		if (q->bufs[i]) {
> +			if (q->bufs[i]->map)

		if (q->bufs[i] && q->bufs[i]->map)

> +				return -EBUSY;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static int __videobuf_mmap_mapper(struct videobuf_queue *q,
> +				  struct vm_area_struct *vma)
> +{
> +	struct videobuf_dma_contig_memory *mem;
> +	struct videobuf_mapping *map;
> +	unsigned int first;
> +	int retval, datasize;
> +	unsigned long size, offset = vma->vm_pgoff << PAGE_SHIFT;
> +
> +	dev_dbg(q->dev, "%s\n", __func__);
> +	if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
> +		return -EINVAL;
> +
> +	/* look for first buffer to map */
> +	for (first = 0; first < VIDEO_MAX_FRAME; first++) {
> +		if (NULL = q->bufs[first])

		if (!q->bufs[first])

> +			continue;
> +
> +		if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
> +			continue;
> +		if (q->bufs[first]->boff = offset)
> +			break;
> +	}
> +	if (VIDEO_MAX_FRAME = first) {
> +		dev_dbg(q->dev, "invalid user space offset [offset=0x%lx]\n",
> +			(vma->vm_pgoff << PAGE_SHIFT));

			offset = vma->vm_pgoff << PAGE_SHIFT;
			Use offset, don't recalculate this value again.

> +		return -EINVAL;
> +	}
> +
> +	/* create mapping + update buffer list */
> +	map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
> +	if (NULL = map)

	if (!map)

> +		return -ENOMEM;
> +
> +	q->bufs[first]->map = map;
> +	map->start = vma->vm_start;
> +	map->end = vma->vm_end;
> +	map->q = q;
> +
> +	q->bufs[first]->baddr = vma->vm_start;
> +
> +	mem = q->bufs[first]->priv;
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	datasize = PAGE_ALIGN(q->bufs[first]->bsize);
> +	mem->vaddr = dma_alloc_coherent(q->dev, datasize,
> +					&mem->dma_handle, GFP_KERNEL);
> +	if (!mem->vaddr) {
> +		pr_err("dma_alloc_coherent size %d failed\n", datasize);

		dev_err?

> +		goto error;
> +	}
> +	dev_dbg(q->dev, "dma_alloc_coherent data is at addr %p (size %d)\n",
> +		mem->vaddr, datasize);
> +
> +	/* Try to remap memory */
> +
> +	size = vma->vm_end - vma->vm_start;
> +	size = (size < datasize) ? size : datasize;
> +
> +	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
> +	retval = remap_pfn_range(vma, vma->vm_start,
> +				 __pa(mem->vaddr) >> PAGE_SHIFT,
> +				 size, vma->vm_page_prot);
> +	if (retval < 0) {

	if (retval)
	because remap_pfn_range returns 0 on success else it is error.

> +		pr_err("mmap: remap failed with error %d. ", retval);

		dev_err ?

> +		dma_free_coherent(q->dev, datasize,
> +				  mem->vaddr, mem->dma_handle);
> +		goto error;
> +	}
> +
> +	vma->vm_ops          = &videobuf_vm_ops;
> +	vma->vm_flags       |= VM_DONTEXPAND;
> +	vma->vm_private_data = map;
> +
> +	dev_dbg(q->dev, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
> +		map, q, vma->vm_start, vma->vm_end,
> +		(long int) q->bufs[first]->bsize,
> +		vma->vm_pgoff, first);
> +
> +	videobuf_vm_open(vma);
> +
> +	return 0;
> +
> +error:
> +	kfree(map);
> +	return -ENOMEM;
> +}
> +
> +static int __videobuf_copy_to_user(struct videobuf_queue *q,
> +				   char __user *data, size_t count,
> +				   int nonblocking)
> +{
> +	struct videobuf_dma_contig_memory *mem = q->read_buf->priv;
> +	void *vaddr;
> +
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +	BUG_ON(!mem->vaddr);
> +
> +	/* copy to userspace */
> +	if (count > q->read_buf->size - q->read_off)
> +		count = q->read_buf->size - q->read_off;
> +
> +	vaddr = mem->vaddr;
> +
> +	if (copy_to_user(data, vaddr + q->read_off, count))
> +		return -EFAULT;
> +
> +	return count;
> +}
> +
> +static int __videobuf_copy_stream(struct videobuf_queue *q,
> +				  char __user *data, size_t count, size_t pos,
> +				  int vbihack, int nonblocking)
> +{
> +	unsigned int  *fc;
> +	struct videobuf_dma_contig_memory *mem = q->read_buf->priv;
> +
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	if (vbihack) {
> +		/* dirty, undocumented hack -- pass the frame counter
> +			* within the last four bytes of each vbi data block.
> +			* We need that one to maintain backward compatibility
> +			* to all vbi decoding software out there ... */
> +		fc = (unsigned int *)mem->vaddr;
> +		fc += (q->read_buf->size >> 2) - 1;
> +		*fc = q->read_buf->field_count >> 1;
> +		dev_dbg(q->dev, "vbihack: %d\n", *fc);
> +	}
> +
> +	/* copy stuff using the common method */
> +	count = __videobuf_copy_to_user(q, data, count, nonblocking);
> +
> +	if ((count = -EFAULT) && (pos = 0))

	if ((count = -EFAULT) && (!pos))

> +		return -EFAULT;
> +
> +	return count;
> +}
> +
> +static struct videobuf_qtype_ops qops = {
> +	.magic        = MAGIC_QTYPE_OPS,
> +
> +	.alloc        = __videobuf_alloc,
> +	.iolock       = __videobuf_iolock,
> +	.sync         = __videobuf_sync,
> +	.mmap_free    = __videobuf_mmap_free,
> +	.mmap_mapper  = __videobuf_mmap_mapper,
> +	.video_copy_to_user = __videobuf_copy_to_user,
> +	.copy_stream  = __videobuf_copy_stream,
> +	.vmalloc      = __videobuf_to_vmalloc,
> +};
> +
> +void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
> +				    struct videobuf_queue_ops *ops,
> +				    struct device *dev,
> +				    spinlock_t *irqlock,
> +				    enum v4l2_buf_type type,
> +				    enum v4l2_field field,
> +				    unsigned int msize,
> +				    void *priv)
> +{
> +	videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
> +				 priv, &qops);
> +}
> +EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init);
> +
> +struct videobuf_dma_contig_memory *
> +videobuf_to_dma_contig(struct videobuf_buffer *buf)
> +{
> +	struct videobuf_dma_contig_memory *mem = buf->priv;
> +
> +	BUG_ON(!mem);
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	return mem;

I think it is enough to return mem->dma_handle
You will have to change function type also...

> +}
> +EXPORT_SYMBOL_GPL(videobuf_to_dma_contig);
> +
> +void videobuf_dma_contig_free(struct videobuf_queue *q,
> +			      struct videobuf_buffer *buf)
> +{
> +	struct videobuf_dma_contig_memory *mem = buf->priv;
> +	int datasize;
> +
> +	/* mmapped memory can't be freed here, otherwise mmapped region
> +	   would be released, while still needed. In this case, the memory
> +	   release should happen inside videobuf_vm_close().
> +	   So, it should free memory only if the memory were allocated for
> +	   read() operation.
> +	 */
> +	if ((buf->memory != V4L2_MEMORY_USERPTR) || (buf->baddr = 0))

	if ((buf->memory != V4L2_MEMORY_USERPTR) || (!buf->baddr))

> +		return;
> +
> +	if (!mem)
> +		return;
> +
> +	MAGIC_CHECK(mem->magic, MAGIC_DC_MEM);
> +
> +	datasize = PAGE_ALIGN(buf->size);
> +	dma_free_coherent(q->dev, datasize, mem->vaddr, mem->dma_handle);
> +	mem->vaddr = NULL;

Maybe you should mem->dma_handle = NULL also?
I don't have strong opinion about this...

> +
> +	return;

return not needed here.

> +}
> +EXPORT_SYMBOL_GPL(videobuf_dma_contig_free);
> +
> +MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers");
> +MODULE_AUTHOR("Magnus Damm");
> +MODULE_LICENSE("GPL");
> +

blank line not needed here

> --- /dev/null
> +++ work/include/media/videobuf-dma-contig.h	2008-06-30 15:30:35.000000000 +0900
> @@ -0,0 +1,39 @@
> +/*
> + * helper functions for physically contiguous capture buffers
> + *
> + * The functions support hardware lacking scatter gatter support
> + * (i.e. the buffers must be linear in physical memory)
> + *
> + * Copyright (c) 2008 Magnus Damm
> + *
> + * 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
> + */
> +#ifndef _VIDEOBUF_DMA_CONTIG_H
> +#define _VIDEOBUF_DMA_CONTIG_H
> +
> +#include <linux/dma-mapping.h>

I think this include is not needed here...

> +#include <media/videobuf-core.h>
> +
> +struct videobuf_dma_contig_memory {
> +	u32 magic;
> +	void *vaddr;
> +	dma_addr_t dma_handle;
> +};
> +
> +void videobuf_queue_dma_contig_init(struct videobuf_queue *q,
> +				    struct videobuf_queue_ops *ops,
> +				    struct device *dev,
> +				    spinlock_t *irqlock,
> +				    enum v4l2_buf_type type,
> +				    enum v4l2_field field,
> +				    unsigned int msize,
> +				    void *priv);
> +
> +struct videobuf_dma_contig_memory *
> +videobuf_to_dma_contig(struct videobuf_buffer *buf);
> +void videobuf_dma_contig_free(struct videobuf_queue *q,
> +			      struct videobuf_buffer *buf);
> +
> +#endif /* _VIDEOBUF_DMA_CONTIG_H */


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2008-07-04  9:08 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-01 12:46 [PATCH 00/07] soc_camera: SuperH Mobile CEU support Magnus Damm
2008-07-01 12:46 ` [PATCH 01/07] soc_camera: Remove default spinlock operations Magnus Damm
2008-07-01 12:46 ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
2008-07-01 20:02   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Guennadi Liakhovetski
2008-07-02  2:50     ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue type Magnus Damm
2008-07-02  7:27   ` [PATCH 02/07] soc_camera: Let the host select videobuf_queue Paulius Zaleckas
2008-07-01 12:47 ` [PATCH 03/07] soc_camera: Remove vbq_ops and msize Magnus Damm
2008-07-01 12:47 ` [PATCH 04/07] soc_camera: Remove unused file lock pointer Magnus Damm
2008-07-01 12:47 ` [PATCH 05/07] soc_camera: Add 16-bit bus width support Magnus Damm
2008-07-01 12:47 ` [PATCH 06/07] videobuf: Add physically contiguous queue code Magnus Damm
2008-07-02  7:43   ` Paulius Zaleckas
     [not found]     ` <aec7e5c30807020232j1181ba9s43bc0e6920b18733@mail.gmail.com>
2008-07-02 10:42       ` Paulius Zaleckas
2008-07-04  9:08   ` Paulius Zaleckas
2008-07-01 12:47 ` [PATCH 07/07] sh_mobile_ceu_camera: Add SuperH Mobile CEU driver Magnus Damm
2008-07-01 20:07   ` Guennadi Liakhovetski
2008-07-02  3:08     ` Magnus Damm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).