[PATCH/RFC 1/3] The camera controller driver

Jonathan Corbet corbet at lwn.net
Fri Oct 6 12:33:33 EDT 2006


This is the core camera controller patch.

jon

diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/Documentation/video4linux/m88alp01-ccic 19-jc/Documentation/video4linux/m88alp01-ccic
--- /k/t/2.6.19-rc1/Documentation/video4linux/m88alp01-ccic	1969-12-31 17:00:00.000000000 -0700
+++ 19-jc/Documentation/video4linux/m88alp01-ccic	2006-10-05 16:08:00.000000000 -0600
@@ -0,0 +1,44 @@
+This is the initial version of the Cafe camera driver.  Here's a few notes
+to go along with it...
+
+ - You need a 2.6.18+ kernel for this driver.  If you're running
+   2.6.18-vanilla, you'll need a couple of small v4l2 patches 
+   or the driver will not function.
+
+ - The name of the driver (m88alp01-ccic) was taken from the product name
+   found in the draft spec.  If we want to change its name (and Marvell
+   could well change theirs) now is probably the right time to do so.
+
+ - Once patched into the kernel, it should build and load just fine.
+   There's a number of tweakable parameters, but there should be no need to
+   mess with them.
+
+ - It is rock-solid with mplayer.  The command I use is:
+
+     mplayer tv:// -tv driver=v4l2:width=640:height=480 -nosound
+
+   That will give you a 30fps window, VGA resolution, showing whatever the
+   camera sees.  You can tweak contrast and brightness ('1' and '2' for
+   contrast, '3' and '4' for brightness); no other controls are hooked up
+   yet. 
+
+ - It does not behave at all well with gqcam - which appears to be trying
+   to use the read() method.  There is some residual weirdness with read()
+   that I've not yet had a chance to track down.
+
+ - Only VGA YUV video is supported so far.  Other formats will come soon.
+
+ - I'm mostly unashamed to show the code, but there are some remaining
+   issues.  The biggest has to do with the use of the "ovcamchip" layer,
+   which was really meant for a very different sort of device.  Much of
+   stuff which should really be dealt with at the lower layer (so that the
+   Cafe could work with sensors other than the OV7670) is currently "known"
+   at the Cafe level instead.  It is my intent to fix that.
+
+Please do look it over, try it out, and get back to me with any questions
+or comments.
+
+jon
+
+Jonathan Corbet
+corbet at lwn.net
diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/drivers/media/video/Kconfig 19-jc/drivers/media/video/Kconfig
--- /k/t/2.6.19-rc1/drivers/media/video/Kconfig	2006-10-05 16:05:24.000000000 -0600
+++ 19-jc/drivers/media/video/Kconfig	2006-10-05 16:08:00.000000000 -0600
@@ -29,6 +29,25 @@ config VIDEO_HELPER_CHIPS_AUTO
 
 	  In doubt, say Y.
 
+config VIDEO_OVCAMCHIP
+	tristate "OmniVision Camera Chip support"
+	depends on I2C 
+	---help---
+	  Support for the OmniVision OV6xxx and OV7xxx series of camera chips.
+	  This driver is intended to be used with the ov511 and w9968cf USB
+	  camera drivers.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ovcamchip.
+
+config VIDEO_M88ALP01_CCIC
+	tristate "Marvell 88ALP01 CMOS Camera Controller support"
+	depends on I2C && VIDEO_V4L2
+	---help---
+	  This is a video4linux2 driver for the Marvell 88ALP01 integrated
+	  CMOS camera controller.  It currently requires the "ovcamchip"
+	  module.
+
 #
 # Encoder / Decoder module configuration
 #
@@ -683,17 +702,6 @@ source "drivers/media/video/usbvideo/Kco
 
 source "drivers/media/video/et61x251/Kconfig"
 
-config VIDEO_OVCAMCHIP
-	tristate "OmniVision Camera Chip support"
-	depends on I2C && VIDEO_V4L1
-	---help---
-	  Support for the OmniVision OV6xxx and OV7xxx series of camera chips.
-	  This driver is intended to be used with the ov511 and w9968cf USB
-	  camera drivers.
-
-	  To compile this driver as a module, choose M here: the
-	  module will be called ovcamchip.
-
 config USB_W9968CF
 	tristate "USB W996[87]CF JPEG Dual Mode Camera support"
 	depends on USB && VIDEO_V4L1 && I2C
diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/drivers/media/video/m88alp01-ccic.c 19-jc/drivers/media/video/m88alp01-ccic.c
--- /k/t/2.6.19-rc1/drivers/media/video/m88alp01-ccic.c	1969-12-31 17:00:00.000000000 -0700
+++ 19-jc/drivers/media/video/m88alp01-ccic.c	2006-10-06 10:07:06.000000000 -0600
@@ -0,0 +1,2316 @@
+/*
+ * A driver for the CMOS camera controller in the Marvell 88ALP01
+ * multifunction chip.  Currently works with the Omnivision OV7670
+ * sensor.
+ *
+ * Copyright 2006 One Laptop Per Child Association, Inc.
+ *
+ * Written by Jonathan Corbet, corbet at lwn.net.
+ *
+ * This file may be distributed under the terms of the GNU General
+ * Public License, version 2.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/videodev2.h>
+#include <linux/device.h>
+#include <linux/wait.h>
+#include <linux/list.h>
+#include <linux/dma-mapping.h>
+#include <linux/delay.h>
+#include <linux/debugfs.h>
+#include <linux/jiffies.h>
+#include <media/ovcamchip.h>
+
+#include <asm/uaccess.h>
+#include <asm/io.h>
+
+#include "m88alp01-regs.h"
+
+#define DEBUG  /* For the foreseeable future */
+
+#define M88_VERSION 0x000001
+
+
+/*
+ * Parameters.
+ */
+MODULE_AUTHOR("Jonathan Corbet <corbet at lwn.net>");
+MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
+MODULE_LICENSE("GPL");
+MODULE_SUPPORTED_DEVICE("Video");
+
+/*
+ * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
+ * we must have physically contiguous buffers to bring frames into.
+ * These parameters control how many buffers we use, whether we
+ * allocate them at load time (better chance of success, but nails down
+ * memory) or when somebody tries to use the camera (riskier), and,
+ * for load-time allocation, how big they should be.
+ *
+ * The controller can cycle through three buffers.  We could use
+ * more by flipping pointers around, but it probably makes little
+ * sense.
+ */
+
+#define MAX_DMA_BUFS 3
+static int alloc_bufs_at_load = 1;
+module_param(alloc_bufs_at_load, bool, 0444);
+MODULE_PARM_DESC(alloc_bufs_at_load,
+		"Non-zero value causes DMA buffers to be allocated at module "
+		"load time.  This increases the chances of successfully getting "
+		"those buffers, but at the cost of nailing down the memory from "
+		"the outset.");
+
+static int n_dma_bufs = 3; 
+module_param(n_dma_bufs, uint, 0644);
+MODULE_PARM_DESC(n_dma_bufs,
+		"The number of DMA buffers to allocate.  Can be either two "
+		"(saves memory, makes timing tighter) or three.");
+
+static int dma_buf_size = 640 * 480 * 2;  /* Worst case */
+module_param(dma_buf_size, uint, 0444);
+MODULE_PARM_DESC(dma_buf_size,
+		"The size of the allocated DMA buffers.  If actual operating "
+		"parameters require larger buffers, an attempt to reallocate "
+		"will be made.");
+		
+static int min_buffers = 1;
+module_param(min_buffers, uint, 0644);
+MODULE_PARM_DESC(min_buffers,
+		"The minimum number of streaming I/O buffers we are willing "
+		"to work with.");
+
+static int max_buffers = 10;
+module_param(max_buffers, uint, 0644);
+MODULE_PARM_DESC(max_buffers,
+		"The maximum number of streaming I/O buffers an application "
+		"will be allowed to allocate.  These buffers are big and live "
+		"in vmalloc space.");
+
+
+enum m88_state {
+	S_NOTREADY,	/* Not yet initialized */
+	S_IDLE,		/* Just hanging around */
+	S_FLAKED,	/* Some sort of problem */
+	S_SINGLEREAD,	/* In read() */
+	S_SPECREAD,   	/* Speculative read (for future read()) */
+	S_STREAMING	/* Streaming data */
+};
+
+/*
+ * Tracking of streaming I/O buffers.
+ */
+struct m88_sio_buffer {
+	struct list_head list;
+	struct v4l2_buffer v4lbuf;
+	char *buffer;   /* Where it lives in kernel space */
+	int mapcount;
+	struct m88_camera *cam;
+};
+
+/*
+ * A description of one of our devices.
+ * Locking: controlled by s_mutex.  Certain fields, however, require
+ * 	    the dev_lock spinlock; they are marked as such by comments.
+ *	    dev_lock is also required for access to device registers.
+ */
+struct m88_camera
+{
+	enum m88_state state;  
+	unsigned long flags;   		/* Buffer status, mainly (dev_lock) */
+	int users;			/* How many open FDs */
+	struct m88_user *owner; 	/* Who has data access (v4l2) */
+
+	/*
+	 * Subsystem structures.
+	 */
+	struct pci_dev *pdev;
+	struct video_device v4ldev;
+	struct i2c_adapter i2c_adapter;
+	struct i2c_client *sensor;
+
+	unsigned char __iomem *regs;
+	struct list_head dev_list;	/* link to other devices */
+
+	/* DMA buffers */
+	unsigned int nbufs;		/* How many are alloc'd */
+	int next_buf;			/* Next to consume (dev_lock) */
+	unsigned int dma_buf_size;  	/* allocated size */
+	void *dma_bufs[MAX_DMA_BUFS];	/* Internal buffer addresses */
+	dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
+	unsigned int specframes;	/* Unconsumed spec frames (dev_lock) */
+	unsigned int sequence;		/* Frame sequence number */
+	unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
+
+	/* Streaming buffers */
+	unsigned int n_sbufs;		/* How many we have */
+	struct m88_sio_buffer *sb_bufs; /* The array of housekeeping structs */
+	struct list_head sb_avail;	/* Available for data (we own) (dev_lock) */
+	struct list_head sb_full;	/* With data (user space owns) (dev_lock) */
+	struct tasklet_struct s_tasklet; 
+
+	/* Current operating parameters */
+	int sensor_type;		/* Currently ov7670 only */
+	struct v4l2_pix_format pix_format;
+	
+	/* Locks */
+	struct mutex s_mutex; /* Access to this structure */
+	spinlock_t dev_lock;  /* Access to device */
+
+	/* Misc */
+	wait_queue_head_t smbus_wait;	/* Waiting on i2c events */
+	wait_queue_head_t iowait;	/* Waiting on frame data */
+#ifdef DEBUG
+	struct dentry *dfs_regs;
+	struct dentry *dfs_cam_regs;
+#endif
+};
+
+/*
+ * Status flags.  Always manipulated with bit operations.
+ */
+#define CF_BUF0_VALID	 0	/* Buffers valid - first three */
+#define CF_BUF1_VALID	 1
+#define CF_BUF2_VALID	 2
+#define CF_DMA_ACTIVE	 3	/* A frame is incoming */
+#define CF_CONFIG_NEEDED 4	/* Must configure hardware */
+
+
+
+/*
+ * Start over with DMA buffers - dev_lock needed.
+ */
+static void m88_reset_buffers(struct m88_camera *cam)
+{
+	int i;
+	
+	cam->next_buf = -1;
+	for (i = 0; i < cam->nbufs; i++) 
+		clear_bit(i, &cam->flags);
+	cam->specframes = 0;
+}
+
+static inline int m88_needs_config(struct m88_camera *cam)
+{
+	return test_bit(CF_CONFIG_NEEDED, &cam->flags);
+}
+
+static void m88_set_config_needed(struct m88_camera *cam, int needed)
+{
+	if (needed)
+		set_bit(CF_CONFIG_NEEDED, &cam->flags);
+	else
+		clear_bit(CF_CONFIG_NEEDED, &cam->flags);
+}
+
+/*
+ * V4L2 requires us to keep track of each file handle - at least, to
+ * the point of knowing which one is the "owner" which is allowed to
+ * actually map buffers and such.
+ */
+struct m88_user {
+	struct m88_camera *cam;
+/* mapped buffers ... */
+};
+
+
+
+/*
+ * Debugging and related.
+ */
+#define cam_err(cam, fmt, arg...) \
+	dev_err(&(cam)->pdev->dev, fmt, ##arg);
+#define cam_warn(cam, fmt, arg...) \
+	dev_warn(&(cam)->pdev->dev, fmt, ##arg);
+#define cam_dbg(cam, fmt, arg...) \
+	dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
+
+
+/* ---------------------------------------------------------------------*/
+/*
+ * We keep a simple list of known devices to search at open time.
+ */
+static LIST_HEAD(m88_dev_list);
+static DEFINE_MUTEX(m88_dev_list_lock);
+
+static void m88_add_dev(struct m88_camera *cam)
+{
+	mutex_lock(&m88_dev_list_lock);
+	list_add_tail(&cam->dev_list, &m88_dev_list);
+	mutex_unlock(&m88_dev_list_lock);
+}
+
+static void m88_remove_dev(struct m88_camera *cam)
+{
+	mutex_lock(&m88_dev_list_lock);
+	list_del(&cam->dev_list);
+	mutex_unlock(&m88_dev_list_lock);
+}
+
+static struct m88_camera *m88_find_dev(int minor)
+{
+	struct m88_camera *cam;
+	
+	mutex_lock(&m88_dev_list_lock);
+	list_for_each_entry(cam, &m88_dev_list, dev_list) {
+		if (cam->v4ldev.minor == minor)
+			goto done;
+	}
+	cam = NULL;
+  done:
+	mutex_unlock(&m88_dev_list_lock);
+	return cam;
+}
+	
+
+static struct m88_camera *m88_find_by_pdev(struct pci_dev *pdev)
+{
+	struct m88_camera *cam;
+	
+	mutex_lock(&m88_dev_list_lock);
+	list_for_each_entry(cam, &m88_dev_list, dev_list) {
+		if (cam->pdev == pdev)
+			goto done;
+	}
+	cam = NULL;
+  done:
+	mutex_unlock(&m88_dev_list_lock);
+	return cam;
+}
+	
+
+/* ------------------------------------------------------------------------ */
+/*
+ * Device register I/O
+ */
+static inline void m88_reg_write(struct m88_camera *cam, unsigned int reg,
+		unsigned int val)
+{
+	iowrite32(val, cam->regs + reg);
+}
+
+static inline unsigned int m88_reg_read(struct m88_camera *cam,
+		unsigned int reg)
+{
+	return ioread32(cam->regs + reg);
+}
+
+
+static inline void m88_reg_write_mask(struct m88_camera *cam, unsigned int reg,
+		unsigned int val, unsigned int mask)
+{
+	unsigned int v = m88_reg_read(cam, reg);
+
+	v = (v & ~mask) | (val & mask);
+	m88_reg_write(cam, reg, v);
+}
+
+static inline void m88_reg_clear_bit(struct m88_camera *cam,
+		unsigned int reg, unsigned int val)
+{
+	m88_reg_write_mask(cam, reg, 0, val);
+}
+
+static inline void m88_reg_set_bit(struct m88_camera *cam,
+		unsigned int reg, unsigned int val)
+{
+	m88_reg_write_mask(cam, reg, val, val);
+}
+
+
+
+/* -------------------------------------------------------------------- */
+/*
+ * The I2C/SMBUS interface to the camera itself starts here.  The
+ * controller handles SMBUS itself, presenting a relatively simple register
+ * interface; all we have to do is to tell it where to route the data.
+ */
+#define M88_SMBUS_TIMEOUT (HZ)  /* generous */
+
+static int m88_smbus_write_done(struct m88_camera *cam)
+{
+	unsigned long flags;
+	int c1;
+
+	/*
+	 * We must delay after the interrupt, or the controller gets confused
+	 * and never does give us good status.  Fortunately, we don't do this
+	 * often.
+	 */
+	udelay(20);
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	c1 = m88_reg_read(cam, REG_TWSIC1);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
+}
+
+static int m88_smbus_write_data(struct m88_camera *cam,
+		u16 addr, u8 command, u8 value)
+{
+	unsigned int rval;
+	unsigned long flags;
+
+	/*
+	 * For reasons unknown, ovcamchip.h defines the sensor slave IDs
+	 * shifted by one bit.  Shift them back here.
+	 */
+	addr <<= 1;
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
+	rval |= TWSIC0_OVMAGIC;  /* Make OV sensors work */
+	/*
+	 * Marvell sez set clkdiv to all 1's for now.
+	 */
+	rval |= TWSIC0_CLKDIV;
+	m88_reg_write(cam, REG_TWSIC0, rval);
+	(void) m88_reg_read(cam, REG_TWSIC1); /* force write */
+	rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
+	m88_reg_write(cam, REG_TWSIC1, rval);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	mdelay(2); /* Required or things flake */
+
+	wait_event_timeout(cam->smbus_wait, m88_smbus_write_done(cam),
+			M88_SMBUS_TIMEOUT);
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	rval = m88_reg_read(cam, REG_TWSIC1);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	
+	if (rval & TWSIC1_WSTAT) {
+		cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
+				command, value);
+		return -EIO;
+	}
+	if (rval & TWSIC1_ERROR) {
+		cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
+				command, value);
+		return -EIO;
+	}
+	return 0;
+}
+
+
+
+static int m88_smbus_read_done(struct m88_camera *cam)
+{
+	unsigned long flags;
+	int c1;
+
+	/*
+	 * We must delay after the interrupt, or the controller gets confused
+	 * and never does give us good status.  Fortunately, we don't do this
+	 * often.
+	 */
+	udelay(20);
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	c1 = m88_reg_read(cam, REG_TWSIC1);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
+}
+
+
+
+static int m88_smbus_read_data(struct m88_camera *cam,
+		u16 addr, u8 command, u8 *value)
+{
+	unsigned int rval;
+	unsigned long flags;
+
+	/*
+	 * For reasons unknown, ovcamchip.h defines the sensor slave IDs
+	 * shifted by one bit.  Shift them back here.
+	 */
+	addr <<= 1;
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
+	rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
+	/*
+	 * Marvel sez set clkdiv to all 1's for now.
+	 */
+	rval |= TWSIC0_CLKDIV;
+	m88_reg_write(cam, REG_TWSIC0, rval);
+	(void) m88_reg_read(cam, REG_TWSIC1); /* force write */
+	rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
+	m88_reg_write(cam, REG_TWSIC1, rval);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+
+	wait_event_timeout(cam->smbus_wait,
+			m88_smbus_read_done(cam), M88_SMBUS_TIMEOUT);
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	rval = m88_reg_read(cam, REG_TWSIC1);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	
+	if (rval & TWSIC1_ERROR) {
+		cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
+		return -EIO;
+	}
+	if (! (rval & TWSIC1_RVALID)) {
+		cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
+				command);
+		return -EIO;
+	}
+	*value = rval & 0xff;
+	return 0;
+}
+
+/*
+ * Perform a transfer over SMBUS.  This thing is called under
+ * the i2c bus lock, so we shouldn't race with ourselves...
+ */
+static int m88_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
+		unsigned short flags, char rw, u8 command,
+		int size, union i2c_smbus_data *data)
+{
+	struct m88_camera *cam = i2c_get_adapdata(adapter);
+	int ret = -EINVAL;
+	
+	/*
+	 * Refuse to talk to anything but OV cam chips.  We should
+	 * never even see an attempt to do so, but one never knows.
+	 */
+	if (addr != OV7xx0_SID) {
+		cam_err(cam, "funky smbus addr %d\n", addr);
+		return -EINVAL;
+	}
+	/*
+	 * This interface would appear to only do byte data ops.  OK
+	 * it can do word too, but the cam chip has no use for that.
+	 */
+	if (size != I2C_SMBUS_BYTE_DATA) {
+		cam_err(cam, "funky xfer size %d\n", size);
+		return -EINVAL;
+	}
+
+	if (rw == I2C_SMBUS_WRITE)
+		ret = m88_smbus_write_data(cam, addr, command, data->byte);
+	else if (rw == I2C_SMBUS_READ)
+		ret = m88_smbus_read_data(cam, addr, command, &data->byte);
+	return ret;
+}
+
+
+static void m88_smbus_enable_irq(struct m88_camera *cam)
+{
+	unsigned long flags;
+	
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}
+
+static u32 m88_smbus_func(struct i2c_adapter *adapter)
+{
+	return I2C_FUNC_SMBUS_READ_BYTE_DATA  |
+	       I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
+}
+
+static struct i2c_algorithm m88_smbus_algo = {
+	.smbus_xfer = m88_smbus_xfer,
+	.functionality = m88_smbus_func
+};
+
+/* Somebody is on the bus */
+static int m88_cam_init(struct m88_camera *cam);
+
+static int m88_smbus_attach(struct i2c_client *client)
+{
+	struct m88_camera *cam = i2c_get_adapdata(client->adapter);
+
+	/*
+	 * Don't talk to chips we don't recognize.
+	 */
+	if (client->driver->id == I2C_DRIVERID_OVCAMCHIP) {
+		cam->sensor = client;
+		return m88_cam_init(cam);
+	}
+	return -EINVAL;
+}
+
+static int m88_smbus_detach(struct i2c_client *client)
+{
+	struct m88_camera *cam = i2c_get_adapdata(client->adapter);
+
+	if (cam->sensor == client)
+		cam->sensor = NULL;  /* Bummer, no camera */
+	return 0;
+}
+
+static int m88_smbus_setup(struct m88_camera *cam)
+{
+	struct i2c_adapter *adap = &cam->i2c_adapter;
+	int ret;
+
+	m88_smbus_enable_irq(cam);
+	adap->id = I2C_HW_SMBUS_M88ALP01;
+	adap->class = I2C_CLASS_CAM_DIGITAL;
+	adap->owner = THIS_MODULE;
+	adap->client_register = m88_smbus_attach;
+	adap->client_unregister = m88_smbus_detach;
+	adap->algo = &m88_smbus_algo;
+	strcpy(adap->name, "m88alp01-ccic");
+	i2c_set_adapdata(adap, cam);
+	ret = i2c_add_adapter(adap);
+	if (ret)
+		printk(KERN_ERR "Unable to register m88 i2c adapter\n");
+	return ret;
+}
+
+static void m88_smbus_shutdown(struct m88_camera *cam)
+{
+	i2c_del_adapter(&cam->i2c_adapter);
+}
+
+
+/* ------------------------------------------------------------------- */
+/*
+ * Deal with the controller.
+ */
+
+/*
+ * Do everything we think we need to have the interface operating
+ * according to the desired format.
+ */
+static void m88_ctlr_dma(struct m88_camera *cam)
+{
+	/*
+	 * Store the first two Y buffers (we aren't supporting
+	 * planar formats for now, so no UV bufs).  Then either
+	 * set the third if it exists, or tell the controller
+	 * to just use two.
+	 */
+	m88_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
+	m88_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
+	if (cam->nbufs > 2) {
+		m88_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
+		m88_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
+	}
+	else
+		m88_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
+	m88_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
+}
+			
+static void m88_ctlr_image(struct m88_camera *cam)
+{
+	int imgsz;
+	struct v4l2_pix_format *fmt = &cam->pix_format;
+
+	imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
+		(fmt->bytesperline & IMGSZ_H_MASK);
+	m88_reg_write(cam, REG_IMGSIZE, imgsz);
+	m88_reg_write(cam, REG_IMGOFFSET, 0);
+	/* YPITCH just drops the last two bits */
+	m88_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
+			IMGP_YP_MASK);
+	/*
+	 * Tell the controller about the image format we are using.
+	 */
+	switch (cam->pix_format.pixelformat) {
+    	case V4L2_PIX_FMT_YUYV:
+	    m88_reg_write_mask(cam, REG_CTRL0,
+			    C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
+			    C0_DF_MASK);
+	    break;
+
+	default:
+	    cam_err(cam, "Unknown format %x", cam->pix_format.pixelformat);
+	    break;
+	}
+	/*
+	 * Make sure it knows we want to use hsync/vsync.
+	 */
+	m88_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
+			C0_SIFM_MASK);
+}
+
+
+/*
+ * Configure the controller for operation; caller holds the
+ * device mutex.
+ */
+static int m88_ctlr_configure(struct m88_camera *cam)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_ctlr_dma(cam);
+	m88_ctlr_image(cam);
+	m88_set_config_needed(cam, 0);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	return 0;
+}
+
+static void m88_ctlr_irq_enable(struct m88_camera *cam)
+{
+	/*
+	 * Clear any pending interrupts, since we do not
+	 * expect to have I/O active prior to enabling.
+	 */
+	m88_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
+	m88_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
+}
+
+static void m88_ctlr_irq_disable(struct m88_camera *cam)
+{
+	m88_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
+}
+
+/*
+ * Make the controller start grabbing images.  Everything must
+ * be set up before doing this.
+ */
+static void m88_ctlr_start(struct m88_camera *cam)
+{
+	/* set_bit performs a read, so no other barrier should be
+	   needed here */
+	m88_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
+}
+
+static void m88_ctlr_stop(struct m88_camera *cam)
+{
+	m88_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
+}
+
+static void m88_ctlr_init(struct m88_camera *cam)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	/*
+	 * Go through the dance needed to wake the device up.
+	 * Note that these registers are global and shared
+	 * with the NAND and SD devices.  Interaction between the
+	 * three still needs to be examined.
+	 */
+	m88_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
+	m88_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
+	m88_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
+	mdelay(5);
+	m88_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
+	m88_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
+	/*
+	 * Make sure it's not powered down. 
+	 */
+	m88_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
+	/*
+	 * Turn off the enable bit.  It sure should be off anyway,
+	 * but it's good to be sure.
+	 */
+	m88_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
+	/*
+	 * Mask all interrupts.
+	 */
+	m88_reg_write(cam, REG_IRQMASK, 0);
+	/*
+	 * Clock the sensor appropriately.  Controller clock should
+	 * be 48MHz, sensor "typical" value is half that.
+	 */
+	m88_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}
+
+
+/*
+ * Stop the controller, and don't return until we're really sure that no
+ * further DMA is going on.
+ */
+static void m88_ctlr_stop_dma(struct m88_camera *cam)
+{
+	unsigned long flags;
+
+	/*
+	 * Theory: stop the camera controller (whether it is operating
+	 * or not).  Delay briefly just in case we race with the SOF
+	 * interrupt, then wait until no DMA is active.
+	 */
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_ctlr_stop(cam);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	mdelay(1);
+	wait_event_timeout(cam->iowait,
+			!test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
+	if (test_bit(CF_DMA_ACTIVE, &cam->flags)) 
+		cam_err(cam, "Timeout waiting for DMA to end\n");
+		/* This would be bad news - what now? */
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	cam->state = S_IDLE;
+	m88_ctlr_irq_disable(cam);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}
+
+/*
+ * Power up and down.
+ */
+static void m88_ctlr_power_up(struct m88_camera *cam)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
+	/*
+	 * Put the sensor into operational mode (assumes OLPC-style
+	 * wiring).  Control 0 is reset - set to 1 to operate.
+	 * Control 1 is power down, set to 0 to operate.
+	 */
+	m88_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN);
+	mdelay(500); /* FIXME */
+	m88_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
+	mdelay(10); /* Enough? */
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}
+
+static void m88_ctlr_power_down(struct m88_camera *cam)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
+	m88_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}	
+
+/* -------------------------------------------------------------------- */
+/*
+ * Communications with the sensor.
+ */
+
+static int __m88_cam_cmd(struct m88_camera *cam, int cmd, void *arg)
+{
+	struct i2c_client *sc = cam->sensor;
+	int ret;
+
+	if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
+		return -EINVAL;
+	ret = sc->driver->command(sc, cmd, arg);
+	if (ret == -EPERM) /* Unsupported command */
+		return 0;
+	return ret;
+}
+
+static int __m88_cam_reset(struct m88_camera *cam)
+{
+	int zero = 0;
+	return __m88_cam_cmd(cam, OVCAMCHIP_CMD_INITIALIZE, &zero);
+}
+
+/*
+ * We have found the sensor on the i2c.  Let's try to have a
+ * conversation.
+ */
+static int m88_cam_init(struct m88_camera *cam)
+{
+	int ret;
+
+	mutex_lock(&cam->s_mutex);
+	if (cam->state != S_NOTREADY)
+		cam_warn(cam, "Cam init with device in funky state %d",
+				cam->state);
+	ret = __m88_cam_reset(cam);
+	if (ret)
+		goto out;
+	ret = __m88_cam_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE, &cam->sensor_type);
+	if (ret)
+		goto out;
+	if (cam->sensor->addr != OV7xx0_SID) {
+		cam_err(cam, "Unsupported sensor addr %d", cam->sensor->addr);
+		ret = -EINVAL;
+		goto out;
+	}
+/* Get/set parameters? */
+
+	ret = 0;
+	cam->state = S_IDLE;
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+/*
+ * Configure the sensor to match the parameters we have.  Caller should
+ * hold s_mutex
+ */
+static int m88_cam_configure(struct m88_camera *cam)
+{
+	int ret;
+	struct ovcamchip_window win;
+
+	if (cam->state != S_IDLE)
+		return -EINVAL;
+	win.x = win.y = 0;
+	win.width = cam->pix_format.width;
+	win.height = cam->pix_format.height;
+	ret = __m88_cam_cmd(cam, OVCAMCHIP_CMD_S_MODE, &win);
+	return ret;
+}
+
+/* -------------------------------------------------------------------- */
+/*
+ * DMA buffer management.  These functions need s_mutex held.
+ */
+
+/* FIXME: this is inefficient as hell, since dma_alloc_coherent just
+ * does a get_free_pages() call, and we waste a good chunk of an orderN
+ * allocation.  Should try to allocate the whole set in one chunk.
+ */
+static int m88_alloc_dma_bufs(struct m88_camera *cam, int loadtime)
+{
+	int i;
+
+	m88_set_config_needed(cam, 1);
+	if (loadtime) 
+		cam->dma_buf_size = dma_buf_size;
+	else
+		cam->dma_buf_size = cam->pix_format.sizeimage;
+	if (n_dma_bufs > 3)
+		n_dma_bufs = 3;
+
+	cam->nbufs = 0;
+	for (i = 0; i < n_dma_bufs; i++) {
+		cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
+				cam->dma_buf_size, cam->dma_handles + i,
+				GFP_KERNEL);
+		if (cam->dma_bufs[i] == NULL) {
+			cam_warn(cam, "Failed to allocate DMA buffer\n");
+			break;
+		}
+		/* For debug, remove eventually */
+		memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
+		(cam->nbufs)++;
+	}
+
+	switch (cam->nbufs) {
+	case 1:
+	    dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
+			    cam->dma_bufs[0], cam->dma_handles[0]);
+	    cam->nbufs = 0;
+	case 0:
+	    cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
+	    return -ENOMEM;
+
+	case 2:
+	    if (n_dma_bufs > 2)
+		    cam_warn(cam, "Will limp along with only 2 buffers\n");
+	    break;
+	}
+	return 0;
+}
+		
+static void m88_free_dma_bufs(struct m88_camera *cam)
+{
+	int i;
+
+	for (i = 0; i < cam->nbufs; i++) {
+		dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
+				cam->dma_bufs[i], cam->dma_handles[i]);
+		cam->dma_bufs[i] = NULL;
+	}
+	cam->nbufs = 0;
+}
+
+
+
+
+
+/* ----------------------------------------------------------------------- */
+/*
+ * Here starts the V4L2 interface code.
+ */
+
+/*
+ * Read an image from the device. 
+ */
+static ssize_t m88_deliver_buffer(struct m88_camera *cam,
+		char __user *buffer, size_t len, loff_t *pos)
+{
+	int bufno;
+	unsigned long flags;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	if (cam->next_buf < 0) {
+		cam_err(cam, "deliver_buffer: No next buffer\n");
+		spin_unlock_irqrestore(&cam->dev_lock, flags);
+		return -EIO;
+	}
+	bufno = cam->next_buf;
+	clear_bit(bufno, &cam->flags);
+	if (++(cam->next_buf) >= cam->nbufs)
+		cam->next_buf = 0;
+	if (! test_bit(cam->next_buf, &cam->flags))
+		cam->next_buf = -1;
+	cam->specframes = 0;
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+
+	if (len > cam->pix_format.sizeimage)
+		len = cam->pix_format.sizeimage;
+	if (copy_to_user(buffer, cam->dma_bufs[bufno], len)) 
+		return -EFAULT;
+	(*pos) += len;
+	return len;
+}	
+
+/*
+ * Get everything ready, and start grabbing frames.
+ */
+static int m88_read_setup(struct m88_camera *cam, enum m88_state state)
+{
+	int ret;
+	unsigned long flags;
+	
+	/*
+	 * Configuration.  If we still don't have DMA buffers,
+	 * make one last, desperate attempt.
+	 */
+	if (cam->nbufs == 0) 
+		if (m88_alloc_dma_bufs(cam, 0))
+			return -ENOMEM;
+
+	if (m88_needs_config(cam)) {
+		ret = m88_ctlr_configure(cam);
+		if (ret)
+			return ret;
+	}
+
+	/*
+	 * Turn it loose.
+	 */
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	m88_reset_buffers(cam);
+	m88_ctlr_irq_enable(cam);
+	cam->state = state;
+	m88_ctlr_start(cam);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	return 0;
+}
+
+
+static ssize_t m88_v4l_read(struct file *filp,
+		char __user *buffer, size_t len, loff_t *pos)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	int ret;
+	
+	/*
+	 * Perhaps we're in speculative read mode and already
+	 * have data?
+	 */
+	mutex_lock(&cam->s_mutex);
+	if (cam->state == S_SPECREAD) {
+		if (cam->next_buf >= 0) {
+			ret = m88_deliver_buffer(cam, buffer, len, pos);
+			if (ret != 0)
+				goto out_unlock;
+		}
+	} else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
+		ret = -EIO;
+		goto out_unlock;
+	} else if (cam->state != S_IDLE) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
+	/*
+	 * v4l2: multiple processes can open the device, but only
+	 * one gets to grab data from it.
+	 */
+	if (cam->owner && cam->owner != user) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+	cam->owner = user;
+
+	/*
+	 * Do setup if need be.
+	 */
+	if (cam->state != S_SPECREAD) {
+		ret = m88_read_setup(cam, S_SINGLEREAD);
+		if (ret)
+			goto out_unlock;
+	}
+	/*
+	 * Wait for something to happen.  This should probably
+	 * be interruptible (FIXME).
+	 */
+	wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
+	if (cam->next_buf < 0) {
+		cam_err(cam, "read() operation timed out\n");
+		m88_ctlr_stop_dma(cam);
+		ret = -EIO;
+		goto out_unlock;
+	}
+	/*
+	 * Give them their data and we should be done.
+	 */
+	ret = m88_deliver_buffer(cam, buffer, len, pos);
+
+  out_unlock:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+
+
+
+
+
+
+
+/*
+ * Streaming I/O support.
+ */
+
+
+
+static int m88_vidioc_streamon(struct file *filp, void *priv,
+		enum v4l2_buf_type type)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	int ret = -EINVAL;
+
+	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		goto out;
+	mutex_lock(&cam->s_mutex);
+	if (cam->state != S_IDLE || cam->n_sbufs == 0)
+		goto out_unlock;
+
+	cam->sequence = 0;
+	ret = m88_read_setup(cam, S_STREAMING);
+
+  out_unlock:
+	mutex_unlock(&cam->s_mutex);
+  out:
+	return ret;
+}
+
+
+static int m88_vidioc_streamoff(struct file *filp, void *priv,
+		enum v4l2_buf_type type)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	int ret = -EINVAL;
+
+	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		goto out;
+	mutex_lock(&cam->s_mutex);
+	if (cam->state != S_STREAMING)
+		goto out_unlock;
+
+	m88_ctlr_stop_dma(cam);
+	ret = 0;
+
+  out_unlock:
+	mutex_unlock(&cam->s_mutex);
+  out:
+	return ret;
+}
+
+
+
+static int m88_setup_siobuf(struct m88_camera *cam, int index)
+{
+	struct m88_sio_buffer *buf = cam->sb_bufs + index;
+	
+	INIT_LIST_HEAD(&buf->list);
+	buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
+	buf->buffer = vmalloc_user(buf->v4lbuf.length);
+	if (buf->buffer == NULL)
+		return -ENOMEM;
+	buf->mapcount = 0;
+	buf->cam = cam;
+
+	buf->v4lbuf.index = index;
+	buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	buf->v4lbuf.field = V4L2_FIELD_NONE;
+	buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
+	/*
+	 * Offset: must be 32-bit even on a 64-bit system.  video-buf
+	 * just uses the length times the index, but the spec warns
+	 * against doing just that - vma merging problems.  So we
+	 * leave a gap between each pair of buffers.
+	 */
+	buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
+	return 0;
+}
+
+static int m88_free_sio_buffers(struct m88_camera *cam)
+{
+	int i;
+
+	/*
+	 * If any buffers are mapped, we cannot free them at all.
+	 */
+	for (i = 0; i < cam->n_sbufs; i++)
+		if (cam->sb_bufs[i].mapcount > 0)
+			return -EBUSY;
+	/*
+	 * OK, let's do it.
+	 */
+	for (i = 0; i < cam->n_sbufs; i++)
+		vfree(cam->sb_bufs[i].buffer);
+	kfree(cam->sb_bufs);
+	cam->n_sbufs = 0;
+	INIT_LIST_HEAD(&cam->sb_avail);
+	INIT_LIST_HEAD(&cam->sb_full);
+	return 0;
+}
+
+
+
+static int m88_vidioc_reqbufs(struct file *filp, void *priv,
+		struct v4l2_requestbuffers *req)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	int ret;
+
+	/*
+	 * Make sure it's something we can do.  User pointers could be
+	 * implemented without great pain, but that's not been done yet.
+	 */
+	if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+	if (req->memory != V4L2_MEMORY_MMAP)
+		return -EINVAL;
+	/*
+	 * If they ask for zero buffers, they really want us to stop streaming
+	 * (if it's happening) and free everything.  Should we check owner?
+	 */
+	mutex_lock(&cam->s_mutex);
+	if (req->count == 0) {
+		if (cam->state == S_STREAMING)
+			m88_ctlr_stop_dma(cam);
+		ret = m88_free_sio_buffers (cam);
+		goto out;
+	}
+	/*
+	 * Device needs to be idle and working.  We *could* try to do the
+	 * right thing in S_SPECREAD by shutting things down, but it
+	 * probably doesn't matter.
+	 */
+	if (cam->state != S_IDLE || (cam->owner && cam->owner != user)) {
+		ret = -EBUSY;
+		goto out;
+	}
+	cam->owner = user;
+
+	if (req->count < min_buffers)
+		req->count = min_buffers;
+	else if (req->count > max_buffers)
+		req->count = max_buffers;
+	if (cam->n_sbufs > 0) {
+		ret = m88_free_sio_buffers(cam);
+		if (ret)
+			goto out;
+	}
+
+	cam->sb_bufs = kzalloc(req->count*sizeof(struct m88_sio_buffer),
+			GFP_KERNEL);
+	if (cam->sb_bufs == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
+		ret = m88_setup_siobuf(cam, cam->n_sbufs);
+		if (ret)
+			break;
+	}
+
+	if (cam->n_sbufs == 0)  /* no luck at all - ret already set */
+		kfree(cam->sb_bufs);
+	else
+		ret = 0;
+	req->count = cam->n_sbufs;  /* In case of partial success */
+
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+
+static int m88_vidioc_querybuf(struct file *filp, void *priv,
+		struct v4l2_buffer *buf)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	int ret = -EINVAL;
+
+	mutex_lock(&cam->s_mutex);
+	if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		goto out;
+	if (buf->index < 0 || buf->index >= cam->n_sbufs)
+		goto out;
+	*buf = cam->sb_bufs[buf->index].v4lbuf;
+	ret = 0;
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+static int m88_vidioc_qbuf(struct file *filp, void *priv,
+		struct v4l2_buffer *buf)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	struct m88_sio_buffer *sbuf;
+	int ret = -EINVAL;
+	unsigned long flags;
+
+	mutex_lock(&cam->s_mutex);
+	if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		goto out;
+	if (buf->index < 0 || buf->index >= cam->n_sbufs)
+		goto out;
+	sbuf = cam->sb_bufs + buf->index;
+	if (sbuf->v4lbuf.flags & (V4L2_BUF_FLAG_QUEUED|V4L2_BUF_FLAG_DONE)) {
+		/* Spec doesn't say anything, seems appropriate tho */
+		ret = -EBUSY;
+		goto out;
+	}
+	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	list_add(&sbuf->list, &cam->sb_avail);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+	ret = 0;
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+static int m88_vidioc_dqbuf(struct file *filp, void *priv,
+		struct v4l2_buffer *buf)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	struct m88_sio_buffer *sbuf;
+	int ret = -EINVAL;
+	unsigned long flags;
+
+	mutex_lock(&cam->s_mutex);
+	if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		goto out_unlock;
+	if (cam->state != S_STREAMING)
+		goto out_unlock;
+	if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
+		ret = -EAGAIN;
+		goto out_unlock;
+	}
+
+	while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
+		mutex_unlock(&cam->s_mutex);
+		if (wait_event_interruptible(cam->iowait,
+						!list_empty(&cam->sb_full))) {
+			ret = -ERESTARTSYS;
+			goto out_unlock;
+		}
+		mutex_lock(&cam->s_mutex);
+	}
+
+	if (cam->state != S_STREAMING)
+		ret = -EINTR;
+	else {
+		spin_lock_irqsave(&cam->dev_lock, flags);
+		/* Should probably recheck !list_empty() here */
+		sbuf = list_entry(cam->sb_full.next,
+				struct m88_sio_buffer, list);
+		list_del_init(&sbuf->list);
+		spin_unlock_irqrestore(&cam->dev_lock, flags);
+		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
+		*buf = sbuf->v4lbuf;
+		ret = 0;
+	}
+
+  out_unlock:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+
+
+static void m88_v4l_vm_open(struct vm_area_struct *vma)
+{
+	struct m88_sio_buffer *sbuf = vma->vm_private_data;
+	/*
+	 * Locking: done under mmap_sem, so we don't need to
+	 * go back to the camera lock here.
+	 */
+	sbuf->mapcount++;
+}
+
+
+static void m88_v4l_vm_close(struct vm_area_struct *vma)
+{
+	struct m88_sio_buffer *sbuf = vma->vm_private_data;
+
+	mutex_lock(&sbuf->cam->s_mutex);
+	sbuf->mapcount--;
+	/* Docs say we should stop I/O too... */
+	if (sbuf->mapcount == 0)
+		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
+	mutex_unlock(&sbuf->cam->s_mutex);
+}
+
+static struct vm_operations_struct m88_v4l_vm_ops = {
+	.open = m88_v4l_vm_open,
+	.close = m88_v4l_vm_close
+};
+
+
+static int m88_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
+	int ret = -EINVAL;
+	int i;
+	struct m88_sio_buffer *sbuf = NULL;
+
+	if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
+		return -EINVAL;
+	/*
+	 * Find the buffer they are looking for.
+	 */
+	mutex_lock(&cam->s_mutex);
+	for (i = 0; i < cam->n_sbufs; i++)
+		if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
+			sbuf = cam->sb_bufs + i;
+			break;
+		}
+	if (sbuf == NULL)
+		goto out;
+
+	ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
+	if (ret)
+		goto out;
+	vma->vm_flags |= VM_DONTEXPAND;
+	vma->vm_private_data = sbuf;
+	vma->vm_ops = &m88_v4l_vm_ops;
+	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
+	m88_v4l_vm_open(vma);
+	ret = 0;
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+	
+
+
+static int m88_v4l_open(struct inode *inode, struct file *filp)
+{
+	struct m88_camera *cam;
+	struct m88_user *user;
+
+	cam = m88_find_dev(iminor(inode));
+	if (cam == NULL)
+		return -ENODEV;
+	user = kzalloc(sizeof(struct m88_user), GFP_KERNEL);
+	if (! user)
+		return -ENOMEM;
+	user->cam = cam;
+	filp->private_data = user;
+
+	mutex_lock(&cam->s_mutex);
+	if (cam->users == 0) {
+		m88_ctlr_power_up(cam);
+		__m88_cam_reset(cam);
+		m88_set_config_needed(cam, 1);
+	/* FIXME make sure this is complete */
+	}
+	(cam->users)++;
+	mutex_unlock(&cam->s_mutex);
+	return 0;
+}
+
+
+static int m88_v4l_release(struct inode *inode, struct file *filp)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	
+	mutex_lock(&cam->s_mutex);
+	(cam->users)--;
+	if (user == cam->owner) {
+		m88_ctlr_stop_dma(cam);
+		m88_free_sio_buffers(cam);
+		cam->owner = NULL;
+	}
+	if (cam->users == 0)
+		m88_ctlr_power_down(cam);
+	mutex_unlock(&cam->s_mutex);
+	kfree(user);
+	return 0;
+}
+
+
+
+static unsigned int m88_v4l_poll(struct file *filp,
+		struct poll_table_struct *pt)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+
+	poll_wait(filp, &cam->iowait, pt);
+	if (cam->next_buf >= 0)
+		return POLLIN | POLLRDNORM;
+	return 0;
+}
+
+
+/*
+ * Controls.
+ */
+static struct m88_v4l_control {
+	__u32 	c_v4l_id;	/* V4L2 control ID */
+	int	c_ov_id;	/* ovcamchip ID */
+	struct v4l2_queryctrl qc;
+} m88_controls[] =
+{
+	{
+		.c_v4l_id = V4L2_CID_BRIGHTNESS,
+		.c_ov_id = OVCAMCHIP_CID_BRIGHT,
+		.qc = {
+			.id = V4L2_CID_BRIGHTNESS,
+			.type = V4L2_CTRL_TYPE_INTEGER,
+			.name = "Brightness",
+			.minimum = 0,
+			.maximum = 255,
+			.step = 1,
+			.default_value = 0x60,   /* XXX from ovcamchip */
+			.flags = V4L2_CTRL_FLAG_SLIDER
+		}
+	},
+	{
+		.c_v4l_id = V4L2_CID_CONTRAST,
+		.c_ov_id = OVCAMCHIP_CID_CONT,
+		.qc = {
+			.id = V4L2_CID_CONTRAST,
+			.type = V4L2_CTRL_TYPE_INTEGER,
+			.name = "Contrast",
+			.minimum = 0,
+			.maximum = 255,
+			.step = 1,
+			.default_value = 0x40,   /* XXX ov7670 spec */
+			.flags = V4L2_CTRL_FLAG_SLIDER
+		}
+	},
+	{
+		.c_v4l_id = 0,
+	}
+};
+	  
+
+static struct m88_v4l_control *m88_v4l_find_ctrl(__u32 id)
+{
+	int i;
+	
+	for (i = 0; m88_controls[i].c_v4l_id != 0; i++)
+		if (m88_controls[i].c_v4l_id == id)
+			return m88_controls + i;
+	return NULL;
+}
+
+static int m88_vidioc_queryctrl(struct file *filp, void *priv,
+		struct v4l2_queryctrl *qc)
+{
+	struct m88_v4l_control *ctrl = m88_v4l_find_ctrl(qc->id);
+	
+	if (ctrl == NULL)
+		return -EINVAL;
+	*qc = ctrl->qc;
+	return 0;
+}
+
+
+static int m88_vidioc_g_ctrl(struct file *filp, void *priv,
+		struct v4l2_control *ctrl)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	struct m88_v4l_control *mctrl;
+	int ret = -EINVAL;
+	struct ovcamchip_control ovctrl;
+	
+	mctrl = m88_v4l_find_ctrl(ctrl->id);
+	if (mctrl == NULL)
+		return -EINVAL;
+	
+	mutex_lock(&cam->s_mutex);
+	/* FIXME for non-integer values */
+	ovctrl.id = mctrl->c_ov_id;
+	ret = __m88_cam_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ovctrl);
+	if (ret == 0) 
+		ctrl->value = (ovctrl.value >> 8) & 0xff;
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+
+static int m88_vidioc_s_ctrl(struct file *filp, void *priv,
+		struct v4l2_control *ctrl)
+{
+	struct m88_user *user = filp->private_data;
+	struct m88_camera *cam = user->cam;
+	struct m88_v4l_control *mctrl;
+	int ret = -EINVAL;
+	struct ovcamchip_control ovctrl;
+	
+	mctrl = m88_v4l_find_ctrl(ctrl->id);
+	if (mctrl == NULL)
+		return -EINVAL;
+	
+	mutex_lock(&cam->s_mutex);
+	/* FIXME for non-integer values */
+	ovctrl.id = mctrl->c_ov_id;
+	ovctrl.value = ctrl->value << 8;
+	ret = __m88_cam_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ovctrl);
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+
+
+static int m88_vidioc_querycap(struct file *file, void *priv,
+		struct v4l2_capability *cap)
+{
+	strcpy(cap->driver, "m88alp01-ccic");
+	strcpy(cap->card, "m88alp01-ccic");
+	cap->version = M88_VERSION;
+	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
+		V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
+	return 0;
+}
+
+/*
+ * What formats do we support?  More to be added here later,
+ * but this will also have to move into the ovcamchip layer.
+ */
+
+static struct m88_format
+{
+	__u8 *desc;
+	__u32 pixelformat;
+	int bytes_per_pixel;
+} m88_format_list [] = {
+	{
+		.desc		= "YUYV 4:2:2",
+		.pixelformat	= V4L2_PIX_FMT_YUYV,
+		.bytes_per_pixel = 2
+	},
+};
+
+#define M88_N_FORMATS 1
+		
+		
+/*
+ * Image sizes.  For now, stick to the canned sizes offered
+ * by the ov7670.  The sensor is more flexible than this, though.
+ *
+ * This, too, needs to be pushed down into the camera layer.
+ */
+static int m88_width_options[] = { 176, 320, 352, 640 };
+static int m88_height_options[] = { 144, 240, 288, 480 };
+#define N_WINDOW_SIZES 4
+
+/*
+ * The default format we use until somebody says otherwise.
+ */
+static struct v4l2_pix_format m88_def_pix_format = {
+	.width		= 640,
+	.height		= 480,
+	.pixelformat	= V4L2_PIX_FMT_YUYV,
+	.field		= V4L2_FIELD_NONE,
+	.bytesperline	= 640*2,
+	.sizeimage	= 640*480*2,
+};
+
+static int m88_vidioc_enum_fmt_cap(struct file *filp,
+		void *priv, struct v4l2_fmtdesc *fmt)
+{
+	struct m88_format *mfmt;
+	
+	if (fmt->index >= M88_N_FORMATS)
+		return -EINVAL;
+	if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+
+	mfmt = m88_format_list + fmt->index;
+	fmt->flags = 0;
+	strcpy(fmt->description, mfmt->desc);
+	fmt->pixelformat = mfmt->pixelformat;
+	return 0;
+}
+
+
+static int m88_vidioc_try_fmt_cap (struct file *filp, void *priv,
+		struct v4l2_format *fmt)
+{
+	struct m88_user *user = priv;
+	struct m88_camera *cam = user->cam;
+	int index, sizeindex;
+	struct v4l2_pix_format *pix = &fmt->fmt.pix;
+
+	for (index = 0; index < M88_N_FORMATS; index++)
+		if (m88_format_list[index].pixelformat == pix->pixelformat)
+			break;
+	if (index >= M88_N_FORMATS)
+		return -EINVAL;
+	/*
+	 * Fields: the OV devices claim to be progressive.
+	 */
+	if (pix->field == V4L2_FIELD_ANY)
+		pix->field = V4L2_FIELD_NONE;
+	else if (pix->field != V4L2_FIELD_NONE) {
+		cam_warn(cam, "Unsupported field requested");
+		return -EINVAL;
+	}
+	/*
+	 * Round requested image size down to the nearest
+	 * we support, but not below the smallest.
+	 */
+	if (pix->height < m88_height_options[0] ||
+			pix->width < m88_width_options[0])
+		sizeindex = 0;
+	else {
+		for (sizeindex = N_WINDOW_SIZES-1; sizeindex > 0; sizeindex--)
+			if (pix->height >= m88_height_options[sizeindex] &&
+			    pix->width >= m88_width_options[sizeindex])
+				break;
+	}
+	pix->width = m88_width_options[sizeindex];
+	pix->height = m88_height_options[sizeindex];
+	/* Must be a multiple of 4 - will be with std widths */
+	pix->bytesperline = pix->width*m88_format_list[index].bytes_per_pixel;
+	pix->sizeimage = pix->height*pix->bytesperline;
+	return 0;
+}
+
+static int m88_vidioc_s_fmt_cap(struct file *filp, void *priv,
+		struct v4l2_format *fmt)
+{
+	struct m88_user *user = priv;
+	struct m88_camera *cam = user->cam;
+	int ret;
+
+	/*
+	 * Can't do anything if the device is not idle
+	 * Also can't if there are streaming buffers in place.
+	 */
+	if (cam->state != S_IDLE || cam->n_sbufs > 0)
+		return -EBUSY;
+	/*
+	 * See if the formatting works in principle.
+	 */
+	ret = m88_vidioc_try_fmt_cap(filp, priv, fmt);
+	if (ret)
+		return ret;
+	/*
+	 * Now we start to change things for real, so let's do it
+	 * under lock.
+	 */
+	mutex_lock(&cam->s_mutex);
+	cam->pix_format = fmt->fmt.pix;
+	/*
+	 * Make sure we have appropriate DMA buffers.
+	 */
+	ret = -ENOMEM;
+	if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
+		m88_free_dma_bufs(cam);
+	if (cam->nbufs == 0) {
+		if (m88_alloc_dma_bufs(cam, 0))
+			goto out;
+	}
+	/*
+	 * It looks like this might work, so let's program the sensor.
+	 */
+	ret = m88_cam_configure(cam);
+	if (! ret)
+		ret = m88_ctlr_configure(cam);
+  out:
+	mutex_unlock(&cam->s_mutex);
+	return ret;
+}
+
+/*
+ * Return our stored notion of how the camera is/should be configured.
+ * The V4l2 spec wants us to be smarter, and actually get this from
+ * the camera (and not mess with it at open time).  Someday.
+ */
+static int m88_vidioc_g_fmt_cap(struct file *filp, void *priv,
+		struct v4l2_format *f)
+{
+	struct m88_user *user = priv;
+	struct m88_camera *cam = user->cam;
+	
+	f->fmt.pix = cam->pix_format;
+	return 0;
+}
+
+/*
+ * We only have one input - the sensor - so minimize the nonsense here.
+ */
+static int m88_vidioc_enum_input(struct file *filp, void *priv,
+		struct v4l2_input *input)
+{
+	if (input->index != 0)
+		return -EINVAL;
+
+	input->type = V4L2_INPUT_TYPE_CAMERA;
+	input->std = V4L2_STD_ALL; /* Not sure what should go here */
+	strcpy(input->name, "Camera");
+	return 0;
+}
+
+static int m88_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
+{
+	*i = 0;
+	return 0;
+}
+
+static int m88_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
+{
+	if (i != 0)
+		return -EINVAL;
+	return 0;
+}
+
+/* from vivi.c */
+static int m88_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id a)
+{
+	return 0;
+}
+
+
+/*
+ * The TV Norm stuff is weird - we're a camera with little to do with TV,
+ * really.  The following is what vivi does.
+ */
+static struct v4l2_tvnorm m88_tvnorm[] = {
+	{
+		.name      = "NTSC-M",
+		.id        = V4L2_STD_NTSC_M,
+	}
+};
+
+
+void m88_v4l_dev_release(struct video_device *vd)
+{
+	struct m88_camera *cam = container_of(vd, struct m88_camera, v4ldev);
+
+	kfree(cam);
+}
+
+
+/*
+ * This template device holds all of those v4l2 methods; we
+ * clone it for specific real devices.
+ */
+
+static struct file_operations m88_v4l_fops = {
+	.owner = THIS_MODULE,
+	.open = m88_v4l_open,
+	.release = m88_v4l_release,
+	.read = m88_v4l_read,
+	.poll = m88_v4l_poll,
+	.mmap = m88_v4l_mmap,
+	.ioctl = video_ioctl2,
+	.llseek = no_llseek,
+};
+
+static struct video_device m88_v4l_template = {
+	.name = "m88alp01",
+	.type = VFL_TYPE_GRABBER,
+	.type2 = VID_TYPE_CAPTURE,
+	.minor = -1, /* Get one dynamically */
+	.tvnorms = m88_tvnorm,
+	.tvnormsize = 1,
+	.current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
+
+	.fops = &m88_v4l_fops,
+	.release = m88_v4l_dev_release,
+
+	.vidioc_querycap 	= m88_vidioc_querycap,
+	.vidioc_enum_fmt_cap	= m88_vidioc_enum_fmt_cap,
+	.vidioc_try_fmt_cap	= m88_vidioc_try_fmt_cap,
+	.vidioc_s_fmt_cap	= m88_vidioc_s_fmt_cap,
+	.vidioc_g_fmt_cap	= m88_vidioc_g_fmt_cap,
+	.vidioc_enum_input	= m88_vidioc_enum_input,
+	.vidioc_g_input		= m88_vidioc_g_input,
+	.vidioc_s_input		= m88_vidioc_s_input,
+	.vidioc_s_std		= m88_vidioc_s_std,
+	.vidioc_reqbufs		= m88_vidioc_reqbufs,
+	.vidioc_querybuf	= m88_vidioc_querybuf,
+	.vidioc_qbuf		= m88_vidioc_qbuf,
+	.vidioc_dqbuf		= m88_vidioc_dqbuf,
+	.vidioc_streamon	= m88_vidioc_streamon,
+	.vidioc_streamoff	= m88_vidioc_streamoff,
+	.vidioc_queryctrl	= m88_vidioc_queryctrl,
+	.vidioc_g_ctrl		= m88_vidioc_g_ctrl,
+	.vidioc_s_ctrl		= m88_vidioc_s_ctrl,
+	/* Do cropping someday */
+};
+
+
+
+
+
+
+
+/* ---------------------------------------------------------------------- */
+/*
+ * Interrupt handler stuff
+ */
+
+static void m88_frame_tasklet(unsigned long data)
+{
+	struct m88_camera *cam = (struct m88_camera *) data;
+	int i;
+	unsigned long flags;
+	struct m88_sio_buffer *sbuf;
+
+	spin_lock_irqsave(&cam->dev_lock, flags);
+	for (i = 0; i < cam->nbufs; i++) {
+		int bufno = cam->next_buf;
+		if (bufno < 0) {  /* "will never happen" */
+			cam_err(cam, "No valid bufs in tasklet!\n");
+			break;
+		}
+		if (++(cam->next_buf) >= cam->nbufs)
+			cam->next_buf = 0;
+		if (! test_bit(bufno, &cam->flags))
+			continue;
+		if (list_empty(&cam->sb_avail))
+			break;  /* Leave it valid, hope for better later */
+		clear_bit(bufno, &cam->flags);
+		/*
+		 * We could perhaps drop the spinlock during this
+		 * big copy.  Something to consider.
+		 */
+		sbuf = list_entry(cam->sb_avail.next,
+				struct m88_sio_buffer, list);
+		memcpy(sbuf->buffer, cam->dma_bufs[bufno],
+				cam->pix_format.sizeimage);
+		sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
+		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
+		sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
+		list_move_tail(&sbuf->list, &cam->sb_full);
+	}
+	if (! list_empty(&cam->sb_full))
+		wake_up(&cam->iowait);
+	spin_unlock_irqrestore(&cam->dev_lock, flags);
+}
+
+
+
+static void m88_frame_complete(struct m88_camera *cam, int frame)
+{
+	set_bit(frame, &cam->flags);
+	clear_bit(CF_DMA_ACTIVE, &cam->flags);
+	if (cam->next_buf < 0)
+		cam->next_buf = frame;
+	cam->buf_seq[frame] = ++(cam->sequence);
+
+	switch (cam->state) {
+	/*
+	 * If in single read mode, try going speculative.
+	 */
+	    case S_SINGLEREAD:
+		cam->state = S_SPECREAD;
+		cam->specframes = 0;
+		wake_up(&cam->iowait);
+		break;
+		
+	/*
+	 * If we are already doing speculative reads, and nobody is
+	 * reading them, just stop.
+	 */
+	    case S_SPECREAD:
+		if (++(cam->specframes) >= cam->nbufs) {
+			m88_ctlr_stop(cam);
+			m88_ctlr_irq_disable(cam);
+			cam->state = S_IDLE;
+		}
+		wake_up(&cam->iowait);
+		break;
+	/*
+	 * For the streaming case, we defer the real work to the
+	 * camera tasklet.
+	 *
+	 * FIXME: if the application is not consuming the buffers,
+	 * we should eventually put things on hold and restart in
+	 * vidioc_dqbuf().
+	 */
+	    case S_STREAMING:
+		tasklet_schedule(&cam->s_tasklet);
+		break;
+
+	    default:
+		cam_err(cam, "Frame interrupt in non-operational state\n");
+		break;
+	}
+}
+
+
+
+
+static void m88_frame_irq(struct m88_camera *cam, unsigned int irqs)
+{
+	unsigned int frame;
+
+	m88_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
+	/*
+	 * Handle any frame completions.  There really should
+	 * not be more than one of these, or we have fallen
+	 * far behind.
+	 */
+	for (frame = 0; frame < cam->nbufs; frame++)
+		if (irqs & (IRQ_EOF0 << frame))
+			m88_frame_complete(cam, frame);
+	/*
+	 * If a frame starts, note that we have DMA active.  This
+	 * code assumes that we won't get multiple frame interrupts
+	 * at once; may want to rethink that.
+	 */
+	if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
+		set_bit(CF_DMA_ACTIVE, &cam->flags);
+}
+
+
+
+static irqreturn_t m88_irq(int irq, void *data, struct pt_regs *regs)
+{
+	struct m88_camera *cam = data;
+	unsigned int irqs;
+
+	spin_lock(&cam->dev_lock);
+	irqs = m88_reg_read(cam, REG_IRQSTAT);
+	if ((irqs & ALLIRQS) == 0) {
+		spin_unlock(&cam->dev_lock);
+		return IRQ_NONE;
+	}
+	if (irqs & FRAMEIRQS)
+		m88_frame_irq(cam, irqs);
+	if (irqs & TWSIIRQS) {
+		m88_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
+		wake_up(&cam->smbus_wait);
+	}
+	spin_unlock(&cam->dev_lock);
+	return IRQ_HANDLED;
+}
+
+
+/* -------------------------------------------------------------------------- */
+#ifdef DEBUG
+/*
+ * Debugfs stuff.
+ */
+
+static char m88_debug_buf[1024];
+static struct dentry *m88_dfs_root;
+
+static void m88_dfs_setup(void)
+{
+	m88_dfs_root = debugfs_create_dir("m88alp01-ccic", NULL);
+	if (IS_ERR(m88_dfs_root)) {
+		m88_dfs_root = NULL;  /* Never mind */
+		printk(KERN_NOTICE "m88alp01-ccic unable to set up debugfs\n");
+	}
+}
+
+static void m88_dfs_shutdown(void)
+{
+	if (m88_dfs_root)
+		debugfs_remove(m88_dfs_root);
+}
+
+static int m88_dfs_open(struct inode *inode, struct file *file)
+{
+	file->private_data = inode->i_private;
+	return 0;
+}
+
+static ssize_t m88_dfs_read_regs(struct file *file,
+		char __user *buf, size_t count, loff_t *ppos)
+{
+	struct m88_camera *cam = file->private_data;
+	char *s = m88_debug_buf;
+	int offset;
+
+	for (offset = 0; offset < 0x44; offset += 4)
+		s += sprintf(s, "%02x: %08x\n", offset,
+				m88_reg_read(cam, offset));
+	for (offset = 0x88; offset <= 0x90; offset += 4)
+		s += sprintf(s, "%02x: %08x\n", offset,
+				m88_reg_read(cam, offset));
+	for (offset = 0xb4; offset <= 0xbc; offset += 4)
+		s += sprintf(s, "%02x: %08x\n", offset,
+				m88_reg_read(cam, offset));
+	for (offset = 0x3000; offset <= 0x300c; offset += 4)
+		s += sprintf(s, "%04x: %08x\n", offset,
+				m88_reg_read(cam, offset));
+	return simple_read_from_buffer(buf, count, ppos, m88_debug_buf,
+			s - m88_debug_buf);
+}
+		
+static struct file_operations m88_dfs_reg_ops = {
+	.owner = THIS_MODULE,
+	.read = m88_dfs_read_regs,
+	.open = m88_dfs_open
+};
+
+static ssize_t m88_dfs_read_cam(struct file *file,
+		char __user *buf, size_t count, loff_t *ppos)
+{
+	struct m88_camera *cam = file->private_data;
+	char *s = m88_debug_buf;
+	int offset;
+
+	for (offset = 0x0; offset < 0x8a; offset++)
+	{
+		u8 v;
+
+		m88_smbus_read_data(cam, OV7xx0_SID, offset, &v);
+		s += sprintf(s, "%02x: %02x\n", offset, v);
+	}
+	return simple_read_from_buffer(buf, count, ppos, m88_debug_buf,
+			s - m88_debug_buf);
+}
+
+static struct file_operations m88_dfs_cam_ops = {
+	.owner = THIS_MODULE,
+	.read = m88_dfs_read_cam,
+	.open = m88_dfs_open
+};
+
+
+
+static void m88_dfs_cam_setup(struct m88_camera *cam)
+{
+	char fname[40];
+
+	if (!m88_dfs_root)
+		return;
+	sprintf(fname, "regs-%d", cam->v4ldev.minor);
+	cam->dfs_regs = debugfs_create_file(fname, 0444, m88_dfs_root,
+			cam, &m88_dfs_reg_ops);
+	sprintf(fname, "cam-%d", cam->v4ldev.minor);
+	cam->dfs_cam_regs = debugfs_create_file(fname, 0444, m88_dfs_root,
+			cam, &m88_dfs_cam_ops);
+}
+
+
+static void m88_dfs_cam_shutdown(struct m88_camera *cam)
+{
+	if (! IS_ERR(cam->dfs_regs))
+		debugfs_remove(cam->dfs_regs);
+	if (! IS_ERR(cam->dfs_cam_regs))
+		debugfs_remove(cam->dfs_cam_regs);
+}
+
+#else
+
+#define m88_dfs_setup()
+#define m88_dfs_shutdown()
+#define m88_dfs_cam_setup(cam)
+#define m88_dfs_cam_shutdown(cam)
+#endif
+
+
+
+
+/* ------------------------------------------------------------------------*/
+/*
+ * PCI interface stuff.
+ */
+
+static int m88_pci_probe(struct pci_dev *pdev,
+		const struct pci_device_id *id)
+{
+	int ret;
+	u16 classword;
+	struct m88_camera *cam;
+	/*
+	 * Make sure we have a camera here - we'll get calls for
+	 * the other cafe devices as well.
+	 */
+	pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
+	if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
+		return -ENODEV;
+	/*
+	 * Start putting together one of our big camera structures.
+	 */
+	ret = -ENOMEM;
+	cam = kzalloc(sizeof(struct m88_camera), GFP_KERNEL);
+	if (cam == NULL)
+		goto out;
+	mutex_init(&cam->s_mutex);
+	mutex_lock(&cam->s_mutex);
+	spin_lock_init(&cam->dev_lock);
+	cam->state = S_NOTREADY;
+	m88_set_config_needed(cam, 1);
+	init_waitqueue_head(&cam->smbus_wait);
+	init_waitqueue_head(&cam->iowait);
+	cam->pdev = pdev;
+	cam->pix_format = m88_def_pix_format;
+	INIT_LIST_HEAD(&cam->dev_list);
+	INIT_LIST_HEAD(&cam->sb_avail);
+	INIT_LIST_HEAD(&cam->sb_full);
+	tasklet_init(&cam->s_tasklet, m88_frame_tasklet, (unsigned long) cam);
+	/*
+	 * Get set up on the PCI bus.
+	 */
+	ret = pci_enable_device(pdev);
+	if (ret)
+		goto out_free;
+	pci_set_master(pdev);
+
+	ret = -EIO;
+	cam->regs = pci_iomap(pdev, 0, 0);
+	if (! cam->regs) {
+		printk(KERN_ERR "Unable to ioremap m88-ccic regs\n");
+		goto out_free;
+	}
+	ret = request_irq(pdev->irq, m88_irq, IRQF_SHARED, "m88-ccic", cam);
+	if (ret)
+		goto out_iounmap;
+	m88_ctlr_init(cam);
+	m88_ctlr_power_up(cam);
+	/*
+	 * Set up I2C/SMBUS communications
+	 */
+	mutex_unlock(&cam->s_mutex);  /* attach can deadlock */
+	ret = m88_smbus_setup(cam);
+	if (ret)
+		goto out_freeirq;
+	/*
+	 * Get the v4l2 setup done.
+	 */
+	mutex_lock(&cam->s_mutex);
+	cam->v4ldev = m88_v4l_template;
+	cam->v4ldev.debug = 0;
+	ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
+	if (ret)
+		goto out_smbus;
+	/*
+	 * If so requested, try to get our DMA buffers now.
+	 */
+	if (alloc_bufs_at_load) {
+		if (m88_alloc_dma_bufs(cam, 1))
+			cam_warn(cam, "Unable to alloc DMA buffers at load"
+					" will try again later.");
+	}
+
+	m88_dfs_cam_setup(cam);
+	mutex_unlock(&cam->s_mutex);
+	m88_add_dev(cam);
+	return 0;
+
+  out_smbus:
+	m88_smbus_shutdown(cam);
+  out_freeirq:
+	m88_ctlr_power_down(cam);
+	free_irq(pdev->irq, cam);
+  out_iounmap:
+	pci_iounmap(pdev, cam->regs);
+  out_free:
+	kfree(cam);
+  out:
+	return ret;
+}
+
+
+/*
+ * Shut down an initialized device
+ */
+static void m88_shutdown(struct m88_camera *cam)
+{
+/* FIXME: Make sure we take care of everything here */
+	m88_dfs_cam_shutdown(cam);
+	if (cam->n_sbufs > 0)
+		/* What if they are still mapped?  Shouldn't be, but... */
+		m88_free_sio_buffers(cam);
+	m88_remove_dev(cam);
+	m88_ctlr_stop_dma(cam);
+	m88_ctlr_power_down(cam);
+	m88_smbus_shutdown(cam);
+	m88_free_dma_bufs(cam);
+	free_irq(cam->pdev->irq, cam);
+	pci_iounmap(cam->pdev, cam->regs);
+	release_mem_region(pci_resource_start(cam->pdev, 0),
+			pci_resource_len(cam->pdev, 0));
+	video_unregister_device(&cam->v4ldev);
+	/* kfree(cam); done in v4l_release () */
+}
+		
+
+static void m88_pci_remove(struct pci_dev *pdev)
+{
+	struct m88_camera *cam = m88_find_by_pdev(pdev);
+
+	if (cam == NULL) {
+		cam_warn(cam, "pci_remove on unknown pdev %p\n", pdev);
+		return;
+	}
+	mutex_lock(&cam->s_mutex);
+	if (cam->users > 0)
+		cam_warn(cam, "Removing a device with users!\n");
+	m88_shutdown(cam);
+/* No unlock - it no longer exists */
+}
+
+
+
+
+static struct pci_device_id m88_ids[] = {
+	{ PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
+	{ PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
+	{ 0, }
+};
+
+MODULE_DEVICE_TABLE(pci, m88_ids);
+
+static struct pci_driver m88_pci_driver = {
+	.name = "m881000-ccic",
+	.id_table = m88_ids,
+	.probe = m88_pci_probe,
+	.remove = m88_pci_remove,
+};
+
+
+
+
+static int __init m88_init(void)
+{
+	int ret;
+
+	printk(KERN_NOTICE "Marvell m88ALP01 Camera Controller version %d\n",
+			M88_VERSION);
+	m88_dfs_setup();
+	ret = pci_register_driver(&m88_pci_driver);
+	if (ret) {
+		printk(KERN_ERR "Unable to register m881000-ccic driver\n");
+		goto out;
+	}
+	request_module("ovcamchip");
+	ret = 0;
+
+  out:
+	return ret;
+}
+
+
+static void __exit m88_exit(void)
+{
+	m88_dfs_shutdown();
+	pci_unregister_driver(&m88_pci_driver);
+}
+
+module_init(m88_init);
+module_exit(m88_exit);
diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/drivers/media/video/m88alp01-regs.h 19-jc/drivers/media/video/m88alp01-regs.h
--- /k/t/2.6.19-rc1/drivers/media/video/m88alp01-regs.h	1969-12-31 17:00:00.000000000 -0700
+++ 19-jc/drivers/media/video/m88alp01-regs.h	2006-10-05 16:08:00.000000000 -0600
@@ -0,0 +1,151 @@
+/*
+ * Register definitions for the m88alp01 camera interface.  Offsets in bytes
+ * as given in the spec.
+ *
+ * Copyright 2006 One Laptop Per Child Association, Inc.
+ *
+ * Written by Jonathan Corbet, corbet at lwn.net.
+ *
+ * This file may be distributed under the terms of the GNU General
+ * Public License, version 2.
+ */
+#define REG_Y0BAR	0x00
+#define REG_Y1BAR	0x04
+#define REG_Y2BAR	0x08
+/* ... */
+
+#define REG_IMGPITCH	0x24	/* Image pitch register */
+#define   IMGP_YP_SHFT	  2		/* Y pitch params */
+#define   IMGP_YP_MASK	  0x00003ffc	/* Y pitch field */
+#define	  IMGP_UVP_SHFT	  18		/* UV pitch (planar) */
+#define   IMGP_UVP_MASK   0x3ffc0000
+#define REG_IRQSTATRAW	0x28	/* RAW IRQ Status */
+#define   IRQ_EOF0	  0x00000001	/* End of frame 0 */
+#define   IRQ_EOF1	  0x00000002	/* End of frame 1 */
+#define   IRQ_EOF2	  0x00000004	/* End of frame 2 */
+#define   IRQ_SOF0	  0x00000008	/* Start of frame 0 */
+#define   IRQ_SOF1	  0x00000010	/* Start of frame 1 */
+#define   IRQ_SOF2	  0x00000020	/* Start of frame 2 */
+#define   IRQ_OVERFLOW	  0x00000040	/* FIFO overflow */
+#define   IRQ_TWSIW	  0x00010000	/* TWSI (smbus) write */
+#define   IRQ_TWSIR	  0x00020000	/* TWSI read */
+#define   IRQ_TWSIE	  0x00040000	/* TWSI error */
+#define   TWSIIRQS (IRQ_TWSIW|IRQ_TWSIR|IRQ_TWSIE)
+#define   FRAMEIRQS (IRQ_EOF0|IRQ_EOF1|IRQ_EOF2|IRQ_SOF0|IRQ_SOF1|IRQ_SOF2)
+#define   ALLIRQS (TWSIIRQS|FRAMEIRQS|IRQ_OVERFLOW)
+#define REG_IRQMASK	0x2c	/* IRQ mask - same bits as IRQSTAT */
+#define REG_IRQSTAT	0x30	/* IRQ status / clear */
+
+#define REG_IMGSIZE	0x34	/* Image size */
+#define  IMGSZ_V_MASK	  0x1fff0000
+#define  IMGSZ_V_SHIFT	  16
+#define	 IMGSZ_H_MASK	  0x00003fff
+#define REG_IMGOFFSET	0x38	/* IMage offset */
+
+#define REG_CTRL0	0x3c	/* Control 0 */
+#define   C0_ENABLE	  0x00000001	/* Makes the whole thing go */
+#define	  C0_RGBE_5RGGB	  0x00000000	/* RGB565 rrrrrggg gggbbbbb */
+#define   C0_RGBE_5GRBG	  0x00000004	/* RGB565 gggrrrrr bbbbbggg */
+#define   C0_RGBE_5GBRG	  0x00000008	/* RGB565 gggbbbbb rrrrrggg */
+#define   C0_RGBE_5BGGR	  0x0000000c	/* RGB565 bbbbbggg gggrrrrr */
+#define   C0_RGBE_4RGBX	  0x00000000	/* RGB444 rrrrgggg bbbb.... */
+#define   C0_RGBE_4XRGB	  0x00000004	/* RGB444 ....rrrr ggggbbbb */
+#define   C0_RGBE_4BGRX	  0x00000008	/* RGB444 bbbbgggg rrrrxxxx */
+#define   C0_RGBE_4XBGR	  0x0000000c	/* RGB444 ....bbbb ggggrrrr */
+
+/* Mask for all the format bits */
+#define   C0_DF_MASK	  0x00fffffc    /* Bits 2-23 */
+
+/* Spec has two fields for DIN and DOUT, but they must match, so
+   combine them here. */
+#define   C0_DF_YUV	  0x00000000    /* Data is YUV	    */
+#define   C0_DF_RGB	  0x000000a0	/* ... RGB		    */
+#define   C0_DF_BAYER     0x00000140	/* ... Bayer                */
+/* 8-8-8 must be missing from the below - ask */
+#define   C0_RGBF_565	  0x00000000
+#define   C0_RGBF_444	  0x00000800
+#define   C0_RGB_BGR	  0x00001000	/* Blue comes first */
+#define   C0_YUV_PLANAR	  0x00000000	/* YUV 422 planar format */
+#define   C0_YUV_PACKED	  0x00008000	/* YUV 422 packed	*/
+#define   C0_YUV_420PL	  0x0000a000	/* YUV 420 planar	*/
+/* Think that 420 packed must be 111 - ask */
+#define	  C0_YUVE_YUYV	  0x00000000	/* Y1CbY0Cr 		*/
+#define	  C0_YUVE_YVYU	  0x00010000	/* Y1CrY0Cb 		*/
+#define	  C0_YUVE_VYUY	  0x00020000	/* CrY1CbY0 		*/
+#define	  C0_YUVE_UYVY	  0x00030000	/* CbY1CrY0 		*/
+#define   C0_YUVE_XYUV	  0x00000000    /* 420: .YUV		*/
+#define	  C0_YUVE_XYVU	  0x00010000	/* 420: .YVU 		*/
+#define	  C0_YUVE_XUVY	  0x00020000	/* 420: .UVY 		*/
+#define	  C0_YUVE_XVUY	  0x00030000	/* 420: .VUY 		*/
+/* Bayer bits 18,19 if needed */
+#define   C0_HPOL_LOW	  0x01000000	/* HSYNC polarity active low */
+#define   C0_VPOL_LOW	  0x02000000	/* VSYNC polarity active low */
+#define   C0_VCLK_LOW	  0x04000000	/* VCLK on falling edge */
+#define   C0_DOWNSCALE	  0x08000000	/* Enable downscaler */
+#define	  C0_SIFM_MASK	  0xc0000000	/* SIF mode bits */
+#define   C0_SIF_HVSYNC	  0x00000000	/* Use H/VSYNC */
+#define   CO_SOF_NOSYNC	  0x40000000	/* Use inband active signaling */
+
+
+#define REG_CTRL1	0x40	/* Control 1 */
+#define   C1_444ALPHA	  0x00f00000	/* Alpha field in RGB444 */
+#define   C1_ALPHA_SHFT	  20
+#define   C1_DMAB32	  0x00000000	/* 32-byte DMA burst */
+#define   C1_DMAB16	  0x02000000	/* 16-byte DMA burst */
+#define	  C1_DMAB64	  0x04000000	/* 64-byte DMA burst */
+#define	  C1_DMAB_MASK	  0x06000000
+#define   C1_TWOBUFS	  0x08000000	/* Use only two DMA buffers */
+#define   C1_PWRDWN	  0x10000000	/* Power down */
+
+#define REG_CLKCTRL	0x88	/* Clock control */
+#define   CLK_DIV_MASK	  0x0000ffff	/* Upper bits RW "reserved" */
+
+#define REG_GPR		0xb4	/* General purpose register.  This
+				   controls inputs to the power and reset
+				   pins on the OV7670 used with OLPC;
+				   other deployments could differ.  */
+#define   GPR_C1EN	  0x00000020	/* Pad 1 (power down) enable */
+#define   GPR_C0EN	  0x00000010	/* Pad 0 (reset) enable */
+#define	  GPR_C1	  0x00000002	/* Control 1 value */
+/*
+ * Control 0 is wired to reset on OLPC machines.  For ov7x sensors,
+ * it is active low, for 0v6x, instead, it's active high.  What
+ * fun.
+ */
+#define   GPR_C0	  0x00000001	/* Control 0 value */
+
+#define REG_TWSIC0	0xb8	/* TWSI (smbus) control 0 */
+#define   TWSIC0_EN       0x00000001	/* TWSI enable */
+#define   TWSIC0_MODE	  0x00000002	/* 1 = 16-bit, 0 = 8-bit */
+#define   TWSIC0_SID	  0x000003fc	/* Slave ID */
+#define   TWSIC0_SID_SHIFT 2
+#define   TWSIC0_CLKDIV   0x0007fc00	/* Clock divider */
+#define   TWSIC0_MASKACK  0x00400000	/* Mask ack from sensor */
+#define   TWSIC0_OVMAGIC  0x00800000	/* Make it work on OV sensors */
+
+#define REG_TWSIC1	0xbc	/* TWSI control 1 */
+#define   TWSIC1_DATA	  0x0000ffff	/* Data to/from camchip */
+#define   TWSIC1_ADDR	  0x00ff0000	/* Address (register) */
+#define   TWSIC1_ADDR_SHIFT 16
+#define   TWSIC1_READ	  0x01000000	/* Set for read op */
+#define   TWSIC1_WSTAT	  0x02000000	/* Write status */
+#define   TWSIC1_RVALID	  0x04000000	/* Read data valid */
+#define   TWSIC1_ERROR	  0x08000000	/* Something screwed up */
+
+
+#define REG_UBAR	0xc4	/* Upper base address register */
+
+/*
+ * Here's the weird global control registers which are said to live
+ * way up here.
+ */
+#define REG_GL_CSR     0x3004  /* Control/status register */
+#define   GCSR_SRS	 0x00000001	/* SW Reset set */
+#define   GCSR_SRC  	 0x00000002	/* SW Reset clear */
+#define	  GCSR_MRS	 0x00000004	/* Master reset set */
+#define	  GCSR_MRC	 0x00000008	/* HW Reset clear */
+#define   GCSR_CCIC_EN   0x00004000    /* CCIC Clock enable */
+#define REG_GL_IMASK   0x300c  /* Interrupt mask register */
+#define   GIMSK_CCIC_EN          0x00000004    /* CCIC Interrupt enable */
+
+#define REG_LEN                REG_GL_IMASK + 4
diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/drivers/media/video/Makefile 19-jc/drivers/media/video/Makefile
--- /k/t/2.6.19-rc1/drivers/media/video/Makefile	2006-10-05 16:05:24.000000000 -0600
+++ 19-jc/drivers/media/video/Makefile	2006-10-05 16:08:00.000000000 -0600
@@ -92,6 +92,8 @@ obj-$(CONFIG_VIDEO_UPD64031A) += upd6403
 obj-$(CONFIG_VIDEO_UPD64083) += upd64083.o
 obj-$(CONFIG_VIDEO_CX2341X) += cx2341x.o
 
+obj-$(CONFIG_VIDEO_M88ALP01_CCIC) += m88alp01-ccic.o
+
 obj-$(CONFIG_USB_DABUSB)        += dabusb.o
 obj-$(CONFIG_USB_OV511)         += ov511.o
 obj-$(CONFIG_USB_SE401)         += se401.o
diff -ruNp -X 19-jc/Documentation/dontdiff /k/t/2.6.19-rc1/include/linux/i2c-id.h 19-jc/include/linux/i2c-id.h
--- /k/t/2.6.19-rc1/include/linux/i2c-id.h	2006-10-05 16:05:39.000000000 -0600
+++ 19-jc/include/linux/i2c-id.h	2006-10-05 16:08:00.000000000 -0600
@@ -250,6 +250,7 @@
 #define I2C_HW_SMBUS_OV518	0x04000f /* OV518(+) USB 1.1 webcam ICs */
 #define I2C_HW_SMBUS_OV519	0x040010 /* OV519 USB 1.1 webcam IC */
 #define I2C_HW_SMBUS_OVFX2	0x040011 /* Cypress/OmniVision FX2 webcam */
+#define I2C_HW_SMBUS_M88ALP01	0x040012 /* Marvell 88ALP01 cam  */
 
 /* --- ISA pseudo-adapter						*/
 #define I2C_HW_ISA		0x050000



More information about the Devel mailing list