• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/media/video/
1/*
2 * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
3 * multifunction chip.  Currently works with the Omnivision OV7670
4 * sensor.
5 *
6 * The data sheet for this device can be found at:
7 *    http://www.marvell.com/products/pcconn/88ALP01.jsp
8 *
9 * Copyright 2006 One Laptop Per Child Association, Inc.
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
11 *
12 * Written by Jonathan Corbet, corbet@lwn.net.
13 *
14 * v4l2_device/v4l2_subdev conversion by:
15 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
16 *
17 * Note: this conversion is untested! Please contact the linux-media
18 * mailinglist if you can test this, together with the test results.
19 *
20 * This file may be distributed under the terms of the GNU General
21 * Public License, version 2.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/fs.h>
28#include <linux/mm.h>
29#include <linux/pci.h>
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
32#include <linux/spinlock.h>
33#include <linux/videodev2.h>
34#include <linux/slab.h>
35#include <media/v4l2-device.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-chip-ident.h>
38#include <linux/device.h>
39#include <linux/wait.h>
40#include <linux/list.h>
41#include <linux/dma-mapping.h>
42#include <linux/delay.h>
43#include <linux/jiffies.h>
44#include <linux/vmalloc.h>
45
46#include <asm/uaccess.h>
47#include <asm/io.h>
48
49#include "cafe_ccic-regs.h"
50
51#define CAFE_VERSION 0x000002
52
53
54/*
55 * Parameters.
56 */
57MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
58MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("Video");
61
62/*
63 * Internal DMA buffer management.  Since the controller cannot do S/G I/O,
64 * we must have physically contiguous buffers to bring frames into.
65 * These parameters control how many buffers we use, whether we
66 * allocate them at load time (better chance of success, but nails down
67 * memory) or when somebody tries to use the camera (riskier), and,
68 * for load-time allocation, how big they should be.
69 *
70 * The controller can cycle through three buffers.  We could use
71 * more by flipping pointers around, but it probably makes little
72 * sense.
73 */
74
75#define MAX_DMA_BUFS 3
76static int alloc_bufs_at_read;
77module_param(alloc_bufs_at_read, bool, 0444);
78MODULE_PARM_DESC(alloc_bufs_at_read,
79		"Non-zero value causes DMA buffers to be allocated when the "
80		"video capture device is read, rather than at module load "
81		"time.  This saves memory, but decreases the chances of "
82		"successfully getting those buffers.");
83
84static int n_dma_bufs = 3;
85module_param(n_dma_bufs, uint, 0644);
86MODULE_PARM_DESC(n_dma_bufs,
87		"The number of DMA buffers to allocate.  Can be either two "
88		"(saves memory, makes timing tighter) or three.");
89
90static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2;  /* Worst case */
91module_param(dma_buf_size, uint, 0444);
92MODULE_PARM_DESC(dma_buf_size,
93		"The size of the allocated DMA buffers.  If actual operating "
94		"parameters require larger buffers, an attempt to reallocate "
95		"will be made.");
96
97static int min_buffers = 1;
98module_param(min_buffers, uint, 0644);
99MODULE_PARM_DESC(min_buffers,
100		"The minimum number of streaming I/O buffers we are willing "
101		"to work with.");
102
103static int max_buffers = 10;
104module_param(max_buffers, uint, 0644);
105MODULE_PARM_DESC(max_buffers,
106		"The maximum number of streaming I/O buffers an application "
107		"will be allowed to allocate.  These buffers are big and live "
108		"in vmalloc space.");
109
110static int flip;
111module_param(flip, bool, 0444);
112MODULE_PARM_DESC(flip,
113		"If set, the sensor will be instructed to flip the image "
114		"vertically.");
115
116
117enum cafe_state {
118	S_NOTREADY,	/* Not yet initialized */
119	S_IDLE,		/* Just hanging around */
120	S_FLAKED,	/* Some sort of problem */
121	S_SINGLEREAD,	/* In read() */
122	S_SPECREAD,   	/* Speculative read (for future read()) */
123	S_STREAMING	/* Streaming data */
124};
125
126/*
127 * Tracking of streaming I/O buffers.
128 */
129struct cafe_sio_buffer {
130	struct list_head list;
131	struct v4l2_buffer v4lbuf;
132	char *buffer;   /* Where it lives in kernel space */
133	int mapcount;
134	struct cafe_camera *cam;
135};
136
137/*
138 * A description of one of our devices.
139 * Locking: controlled by s_mutex.  Certain fields, however, require
140 * 	    the dev_lock spinlock; they are marked as such by comments.
141 *	    dev_lock is also required for access to device registers.
142 */
143struct cafe_camera
144{
145	struct v4l2_device v4l2_dev;
146	enum cafe_state state;
147	unsigned long flags;   		/* Buffer status, mainly (dev_lock) */
148	int users;			/* How many open FDs */
149	struct file *owner;		/* Who has data access (v4l2) */
150
151	/*
152	 * Subsystem structures.
153	 */
154	struct pci_dev *pdev;
155	struct video_device vdev;
156	struct i2c_adapter i2c_adapter;
157	struct v4l2_subdev *sensor;
158	unsigned short sensor_addr;
159
160	unsigned char __iomem *regs;
161	struct list_head dev_list;	/* link to other devices */
162
163	/* DMA buffers */
164	unsigned int nbufs;		/* How many are alloc'd */
165	int next_buf;			/* Next to consume (dev_lock) */
166	unsigned int dma_buf_size;  	/* allocated size */
167	void *dma_bufs[MAX_DMA_BUFS];	/* Internal buffer addresses */
168	dma_addr_t dma_handles[MAX_DMA_BUFS]; /* Buffer bus addresses */
169	unsigned int specframes;	/* Unconsumed spec frames (dev_lock) */
170	unsigned int sequence;		/* Frame sequence number */
171	unsigned int buf_seq[MAX_DMA_BUFS]; /* Sequence for individual buffers */
172
173	/* Streaming buffers */
174	unsigned int n_sbufs;		/* How many we have */
175	struct cafe_sio_buffer *sb_bufs; /* The array of housekeeping structs */
176	struct list_head sb_avail;	/* Available for data (we own) (dev_lock) */
177	struct list_head sb_full;	/* With data (user space owns) (dev_lock) */
178	struct tasklet_struct s_tasklet;
179
180	/* Current operating parameters */
181	u32 sensor_type;		/* Currently ov7670 only */
182	struct v4l2_pix_format pix_format;
183
184	/* Locks */
185	struct mutex s_mutex; /* Access to this structure */
186	spinlock_t dev_lock;  /* Access to device */
187
188	/* Misc */
189	wait_queue_head_t smbus_wait;	/* Waiting on i2c events */
190	wait_queue_head_t iowait;	/* Waiting on frame data */
191};
192
193/*
194 * Status flags.  Always manipulated with bit operations.
195 */
196#define CF_BUF0_VALID	 0	/* Buffers valid - first three */
197#define CF_BUF1_VALID	 1
198#define CF_BUF2_VALID	 2
199#define CF_DMA_ACTIVE	 3	/* A frame is incoming */
200#define CF_CONFIG_NEEDED 4	/* Must configure hardware */
201
202#define sensor_call(cam, o, f, args...) \
203	v4l2_subdev_call(cam->sensor, o, f, ##args)
204
205static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
206{
207	return container_of(dev, struct cafe_camera, v4l2_dev);
208}
209
210
211/*
212 * Start over with DMA buffers - dev_lock needed.
213 */
214static void cafe_reset_buffers(struct cafe_camera *cam)
215{
216	int i;
217
218	cam->next_buf = -1;
219	for (i = 0; i < cam->nbufs; i++)
220		clear_bit(i, &cam->flags);
221	cam->specframes = 0;
222}
223
224static inline int cafe_needs_config(struct cafe_camera *cam)
225{
226	return test_bit(CF_CONFIG_NEEDED, &cam->flags);
227}
228
229static void cafe_set_config_needed(struct cafe_camera *cam, int needed)
230{
231	if (needed)
232		set_bit(CF_CONFIG_NEEDED, &cam->flags);
233	else
234		clear_bit(CF_CONFIG_NEEDED, &cam->flags);
235}
236
237
238
239
240/*
241 * Debugging and related.
242 */
243#define cam_err(cam, fmt, arg...) \
244	dev_err(&(cam)->pdev->dev, fmt, ##arg);
245#define cam_warn(cam, fmt, arg...) \
246	dev_warn(&(cam)->pdev->dev, fmt, ##arg);
247#define cam_dbg(cam, fmt, arg...) \
248	dev_dbg(&(cam)->pdev->dev, fmt, ##arg);
249
250
251/* ---------------------------------------------------------------------*/
252
253/*
254 * Device register I/O
255 */
256static inline void cafe_reg_write(struct cafe_camera *cam, unsigned int reg,
257		unsigned int val)
258{
259	iowrite32(val, cam->regs + reg);
260}
261
262static inline unsigned int cafe_reg_read(struct cafe_camera *cam,
263		unsigned int reg)
264{
265	return ioread32(cam->regs + reg);
266}
267
268
269static inline void cafe_reg_write_mask(struct cafe_camera *cam, unsigned int reg,
270		unsigned int val, unsigned int mask)
271{
272	unsigned int v = cafe_reg_read(cam, reg);
273
274	v = (v & ~mask) | (val & mask);
275	cafe_reg_write(cam, reg, v);
276}
277
278static inline void cafe_reg_clear_bit(struct cafe_camera *cam,
279		unsigned int reg, unsigned int val)
280{
281	cafe_reg_write_mask(cam, reg, 0, val);
282}
283
284static inline void cafe_reg_set_bit(struct cafe_camera *cam,
285		unsigned int reg, unsigned int val)
286{
287	cafe_reg_write_mask(cam, reg, val, val);
288}
289
290
291
292/* -------------------------------------------------------------------- */
293/*
294 * The I2C/SMBUS interface to the camera itself starts here.  The
295 * controller handles SMBUS itself, presenting a relatively simple register
296 * interface; all we have to do is to tell it where to route the data.
297 */
298#define CAFE_SMBUS_TIMEOUT (HZ)  /* generous */
299
300static int cafe_smbus_write_done(struct cafe_camera *cam)
301{
302	unsigned long flags;
303	int c1;
304
305	/*
306	 * We must delay after the interrupt, or the controller gets confused
307	 * and never does give us good status.  Fortunately, we don't do this
308	 * often.
309	 */
310	udelay(20);
311	spin_lock_irqsave(&cam->dev_lock, flags);
312	c1 = cafe_reg_read(cam, REG_TWSIC1);
313	spin_unlock_irqrestore(&cam->dev_lock, flags);
314	return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
315}
316
317static int cafe_smbus_write_data(struct cafe_camera *cam,
318		u16 addr, u8 command, u8 value)
319{
320	unsigned int rval;
321	unsigned long flags;
322	DEFINE_WAIT(the_wait);
323
324	spin_lock_irqsave(&cam->dev_lock, flags);
325	rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
326	rval |= TWSIC0_OVMAGIC;  /* Make OV sensors work */
327	/*
328	 * Marvell sez set clkdiv to all 1's for now.
329	 */
330	rval |= TWSIC0_CLKDIV;
331	cafe_reg_write(cam, REG_TWSIC0, rval);
332	(void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
333	rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
334	cafe_reg_write(cam, REG_TWSIC1, rval);
335	spin_unlock_irqrestore(&cam->dev_lock, flags);
336
337	/*
338	 * Time to wait for the write to complete.  THIS IS A RACY
339	 * WAY TO DO IT, but the sad fact is that reading the TWSIC1
340	 * register too quickly after starting the operation sends
341	 * the device into a place that may be kinder and better, but
342	 * which is absolutely useless for controlling the sensor.  In
343	 * practice we have plenty of time to get into our sleep state
344	 * before the interrupt hits, and the worst case is that we
345	 * time out and then see that things completed, so this seems
346	 * the best way for now.
347	 */
348	do {
349		prepare_to_wait(&cam->smbus_wait, &the_wait,
350				TASK_UNINTERRUPTIBLE);
351		schedule_timeout(1); /* even 1 jiffy is too long */
352		finish_wait(&cam->smbus_wait, &the_wait);
353	} while (!cafe_smbus_write_done(cam));
354
355#ifdef IF_THE_CAFE_HARDWARE_WORKED_RIGHT
356	wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(cam),
357			CAFE_SMBUS_TIMEOUT);
358#endif
359	spin_lock_irqsave(&cam->dev_lock, flags);
360	rval = cafe_reg_read(cam, REG_TWSIC1);
361	spin_unlock_irqrestore(&cam->dev_lock, flags);
362
363	if (rval & TWSIC1_WSTAT) {
364		cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
365				command, value);
366		return -EIO;
367	}
368	if (rval & TWSIC1_ERROR) {
369		cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
370				command, value);
371		return -EIO;
372	}
373	return 0;
374}
375
376
377
378static int cafe_smbus_read_done(struct cafe_camera *cam)
379{
380	unsigned long flags;
381	int c1;
382
383	/*
384	 * We must delay after the interrupt, or the controller gets confused
385	 * and never does give us good status.  Fortunately, we don't do this
386	 * often.
387	 */
388	udelay(20);
389	spin_lock_irqsave(&cam->dev_lock, flags);
390	c1 = cafe_reg_read(cam, REG_TWSIC1);
391	spin_unlock_irqrestore(&cam->dev_lock, flags);
392	return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
393}
394
395
396
397static int cafe_smbus_read_data(struct cafe_camera *cam,
398		u16 addr, u8 command, u8 *value)
399{
400	unsigned int rval;
401	unsigned long flags;
402
403	spin_lock_irqsave(&cam->dev_lock, flags);
404	rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
405	rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
406	/*
407	 * Marvel sez set clkdiv to all 1's for now.
408	 */
409	rval |= TWSIC0_CLKDIV;
410	cafe_reg_write(cam, REG_TWSIC0, rval);
411	(void) cafe_reg_read(cam, REG_TWSIC1); /* force write */
412	rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
413	cafe_reg_write(cam, REG_TWSIC1, rval);
414	spin_unlock_irqrestore(&cam->dev_lock, flags);
415
416	wait_event_timeout(cam->smbus_wait,
417			cafe_smbus_read_done(cam), CAFE_SMBUS_TIMEOUT);
418	spin_lock_irqsave(&cam->dev_lock, flags);
419	rval = cafe_reg_read(cam, REG_TWSIC1);
420	spin_unlock_irqrestore(&cam->dev_lock, flags);
421
422	if (rval & TWSIC1_ERROR) {
423		cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
424		return -EIO;
425	}
426	if (! (rval & TWSIC1_RVALID)) {
427		cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
428				command);
429		return -EIO;
430	}
431	*value = rval & 0xff;
432	return 0;
433}
434
435/*
436 * Perform a transfer over SMBUS.  This thing is called under
437 * the i2c bus lock, so we shouldn't race with ourselves...
438 */
439static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
440		unsigned short flags, char rw, u8 command,
441		int size, union i2c_smbus_data *data)
442{
443	struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
444	struct cafe_camera *cam = to_cam(v4l2_dev);
445	int ret = -EINVAL;
446
447	/*
448	 * This interface would appear to only do byte data ops.  OK
449	 * it can do word too, but the cam chip has no use for that.
450	 */
451	if (size != I2C_SMBUS_BYTE_DATA) {
452		cam_err(cam, "funky xfer size %d\n", size);
453		return -EINVAL;
454	}
455
456	if (rw == I2C_SMBUS_WRITE)
457		ret = cafe_smbus_write_data(cam, addr, command, data->byte);
458	else if (rw == I2C_SMBUS_READ)
459		ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
460	return ret;
461}
462
463
464static void cafe_smbus_enable_irq(struct cafe_camera *cam)
465{
466	unsigned long flags;
467
468	spin_lock_irqsave(&cam->dev_lock, flags);
469	cafe_reg_set_bit(cam, REG_IRQMASK, TWSIIRQS);
470	spin_unlock_irqrestore(&cam->dev_lock, flags);
471}
472
473static u32 cafe_smbus_func(struct i2c_adapter *adapter)
474{
475	return I2C_FUNC_SMBUS_READ_BYTE_DATA  |
476	       I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
477}
478
479static struct i2c_algorithm cafe_smbus_algo = {
480	.smbus_xfer = cafe_smbus_xfer,
481	.functionality = cafe_smbus_func
482};
483
484/* Somebody is on the bus */
485static void cafe_ctlr_stop_dma(struct cafe_camera *cam);
486static void cafe_ctlr_power_down(struct cafe_camera *cam);
487
488static int cafe_smbus_setup(struct cafe_camera *cam)
489{
490	struct i2c_adapter *adap = &cam->i2c_adapter;
491	int ret;
492
493	cafe_smbus_enable_irq(cam);
494	adap->owner = THIS_MODULE;
495	adap->algo = &cafe_smbus_algo;
496	strcpy(adap->name, "cafe_ccic");
497	adap->dev.parent = &cam->pdev->dev;
498	i2c_set_adapdata(adap, &cam->v4l2_dev);
499	ret = i2c_add_adapter(adap);
500	if (ret)
501		printk(KERN_ERR "Unable to register cafe i2c adapter\n");
502	return ret;
503}
504
505static void cafe_smbus_shutdown(struct cafe_camera *cam)
506{
507	i2c_del_adapter(&cam->i2c_adapter);
508}
509
510
511/* ------------------------------------------------------------------- */
512/*
513 * Deal with the controller.
514 */
515
516/*
517 * Do everything we think we need to have the interface operating
518 * according to the desired format.
519 */
520static void cafe_ctlr_dma(struct cafe_camera *cam)
521{
522	/*
523	 * Store the first two Y buffers (we aren't supporting
524	 * planar formats for now, so no UV bufs).  Then either
525	 * set the third if it exists, or tell the controller
526	 * to just use two.
527	 */
528	cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
529	cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
530	if (cam->nbufs > 2) {
531		cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
532		cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
533	}
534	else
535		cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
536	cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
537}
538
539static void cafe_ctlr_image(struct cafe_camera *cam)
540{
541	int imgsz;
542	struct v4l2_pix_format *fmt = &cam->pix_format;
543
544	imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
545		(fmt->bytesperline & IMGSZ_H_MASK);
546	cafe_reg_write(cam, REG_IMGSIZE, imgsz);
547	cafe_reg_write(cam, REG_IMGOFFSET, 0);
548	/* YPITCH just drops the last two bits */
549	cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
550			IMGP_YP_MASK);
551	/*
552	 * Tell the controller about the image format we are using.
553	 */
554	switch (cam->pix_format.pixelformat) {
555	case V4L2_PIX_FMT_YUYV:
556	    cafe_reg_write_mask(cam, REG_CTRL0,
557			    C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
558			    C0_DF_MASK);
559	    break;
560
561	case V4L2_PIX_FMT_RGB444:
562	    cafe_reg_write_mask(cam, REG_CTRL0,
563			    C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
564			    C0_DF_MASK);
565		/* Alpha value? */
566	    break;
567
568	case V4L2_PIX_FMT_RGB565:
569	    cafe_reg_write_mask(cam, REG_CTRL0,
570			    C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
571			    C0_DF_MASK);
572	    break;
573
574	default:
575	    cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
576	    break;
577	}
578	/*
579	 * Make sure it knows we want to use hsync/vsync.
580	 */
581	cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
582			C0_SIFM_MASK);
583}
584
585
586/*
587 * Configure the controller for operation; caller holds the
588 * device mutex.
589 */
590static int cafe_ctlr_configure(struct cafe_camera *cam)
591{
592	unsigned long flags;
593
594	spin_lock_irqsave(&cam->dev_lock, flags);
595	cafe_ctlr_dma(cam);
596	cafe_ctlr_image(cam);
597	cafe_set_config_needed(cam, 0);
598	spin_unlock_irqrestore(&cam->dev_lock, flags);
599	return 0;
600}
601
602static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
603{
604	/*
605	 * Clear any pending interrupts, since we do not
606	 * expect to have I/O active prior to enabling.
607	 */
608	cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
609	cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
610}
611
612static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
613{
614	cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
615}
616
617/*
618 * Make the controller start grabbing images.  Everything must
619 * be set up before doing this.
620 */
621static void cafe_ctlr_start(struct cafe_camera *cam)
622{
623	/* set_bit performs a read, so no other barrier should be
624	   needed here */
625	cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
626}
627
628static void cafe_ctlr_stop(struct cafe_camera *cam)
629{
630	cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
631}
632
633static void cafe_ctlr_init(struct cafe_camera *cam)
634{
635	unsigned long flags;
636
637	spin_lock_irqsave(&cam->dev_lock, flags);
638	/*
639	 * Added magic to bring up the hardware on the B-Test board
640	 */
641	cafe_reg_write(cam, 0x3038, 0x8);
642	cafe_reg_write(cam, 0x315c, 0x80008);
643	/*
644	 * Go through the dance needed to wake the device up.
645	 * Note that these registers are global and shared
646	 * with the NAND and SD devices.  Interaction between the
647	 * three still needs to be examined.
648	 */
649	cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
650	cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
651	cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
652	/*
653	 * Here we must wait a bit for the controller to come around.
654	 */
655	spin_unlock_irqrestore(&cam->dev_lock, flags);
656	msleep(5);
657	spin_lock_irqsave(&cam->dev_lock, flags);
658
659	cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
660	cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
661	/*
662	 * Make sure it's not powered down.
663	 */
664	cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
665	/*
666	 * Turn off the enable bit.  It sure should be off anyway,
667	 * but it's good to be sure.
668	 */
669	cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
670	/*
671	 * Mask all interrupts.
672	 */
673	cafe_reg_write(cam, REG_IRQMASK, 0);
674	/*
675	 * Clock the sensor appropriately.  Controller clock should
676	 * be 48MHz, sensor "typical" value is half that.
677	 */
678	cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
679	spin_unlock_irqrestore(&cam->dev_lock, flags);
680}
681
682
683/*
684 * Stop the controller, and don't return until we're really sure that no
685 * further DMA is going on.
686 */
687static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
688{
689	unsigned long flags;
690
691	/*
692	 * Theory: stop the camera controller (whether it is operating
693	 * or not).  Delay briefly just in case we race with the SOF
694	 * interrupt, then wait until no DMA is active.
695	 */
696	spin_lock_irqsave(&cam->dev_lock, flags);
697	cafe_ctlr_stop(cam);
698	spin_unlock_irqrestore(&cam->dev_lock, flags);
699	mdelay(1);
700	wait_event_timeout(cam->iowait,
701			!test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
702	if (test_bit(CF_DMA_ACTIVE, &cam->flags))
703		cam_err(cam, "Timeout waiting for DMA to end\n");
704		/* This would be bad news - what now? */
705	spin_lock_irqsave(&cam->dev_lock, flags);
706	cam->state = S_IDLE;
707	cafe_ctlr_irq_disable(cam);
708	spin_unlock_irqrestore(&cam->dev_lock, flags);
709}
710
711/*
712 * Power up and down.
713 */
714static void cafe_ctlr_power_up(struct cafe_camera *cam)
715{
716	unsigned long flags;
717
718	spin_lock_irqsave(&cam->dev_lock, flags);
719	cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
720	/*
721	 * Part one of the sensor dance: turn the global
722	 * GPIO signal on.
723	 */
724	cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
725	cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
726	/*
727	 * Put the sensor into operational mode (assumes OLPC-style
728	 * wiring).  Control 0 is reset - set to 1 to operate.
729	 * Control 1 is power down, set to 0 to operate.
730	 */
731	cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
732/*	mdelay(1); */ /* Marvell says 1ms will do it */
733	cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
734/*	mdelay(1); */ /* Enough? */
735	spin_unlock_irqrestore(&cam->dev_lock, flags);
736	msleep(5); /* Just to be sure */
737}
738
739static void cafe_ctlr_power_down(struct cafe_camera *cam)
740{
741	unsigned long flags;
742
743	spin_lock_irqsave(&cam->dev_lock, flags);
744	cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
745	cafe_reg_write(cam, REG_GL_FCR, GFCR_GPIO_ON);
746	cafe_reg_write(cam, REG_GL_GPIOR, GGPIO_OUT);
747	cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
748	spin_unlock_irqrestore(&cam->dev_lock, flags);
749}
750
751/* -------------------------------------------------------------------- */
752/*
753 * Communications with the sensor.
754 */
755
756static int __cafe_cam_reset(struct cafe_camera *cam)
757{
758	return sensor_call(cam, core, reset, 0);
759}
760
761/*
762 * We have found the sensor on the i2c.  Let's try to have a
763 * conversation.
764 */
765static int cafe_cam_init(struct cafe_camera *cam)
766{
767	struct v4l2_dbg_chip_ident chip;
768	int ret;
769
770	mutex_lock(&cam->s_mutex);
771	if (cam->state != S_NOTREADY)
772		cam_warn(cam, "Cam init with device in funky state %d",
773				cam->state);
774	ret = __cafe_cam_reset(cam);
775	if (ret)
776		goto out;
777	chip.ident = V4L2_IDENT_NONE;
778	chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
779	chip.match.addr = cam->sensor_addr;
780	ret = sensor_call(cam, core, g_chip_ident, &chip);
781	if (ret)
782		goto out;
783	cam->sensor_type = chip.ident;
784	if (cam->sensor_type != V4L2_IDENT_OV7670) {
785		cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
786		ret = -EINVAL;
787		goto out;
788	}
789/* Get/set parameters? */
790	ret = 0;
791	cam->state = S_IDLE;
792  out:
793	cafe_ctlr_power_down(cam);
794	mutex_unlock(&cam->s_mutex);
795	return ret;
796}
797
798/*
799 * Configure the sensor to match the parameters we have.  Caller should
800 * hold s_mutex
801 */
802static int cafe_cam_set_flip(struct cafe_camera *cam)
803{
804	struct v4l2_control ctrl;
805
806	memset(&ctrl, 0, sizeof(ctrl));
807	ctrl.id = V4L2_CID_VFLIP;
808	ctrl.value = flip;
809	return sensor_call(cam, core, s_ctrl, &ctrl);
810}
811
812
813static int cafe_cam_configure(struct cafe_camera *cam)
814{
815	struct v4l2_format fmt;
816	int ret;
817
818	if (cam->state != S_IDLE)
819		return -EINVAL;
820	fmt.fmt.pix = cam->pix_format;
821	ret = sensor_call(cam, core, init, 0);
822	if (ret == 0)
823		ret = sensor_call(cam, video, s_fmt, &fmt);
824	/*
825	 * OV7670 does weird things if flip is set *before* format...
826	 */
827	ret += cafe_cam_set_flip(cam);
828	return ret;
829}
830
831/* -------------------------------------------------------------------- */
832/*
833 * DMA buffer management.  These functions need s_mutex held.
834 */
835
836static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
837{
838	int i;
839
840	cafe_set_config_needed(cam, 1);
841	if (loadtime)
842		cam->dma_buf_size = dma_buf_size;
843	else
844		cam->dma_buf_size = cam->pix_format.sizeimage;
845	if (n_dma_bufs > 3)
846		n_dma_bufs = 3;
847
848	cam->nbufs = 0;
849	for (i = 0; i < n_dma_bufs; i++) {
850		cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
851				cam->dma_buf_size, cam->dma_handles + i,
852				GFP_KERNEL);
853		if (cam->dma_bufs[i] == NULL) {
854			cam_warn(cam, "Failed to allocate DMA buffer\n");
855			break;
856		}
857		/* For debug, remove eventually */
858		memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
859		(cam->nbufs)++;
860	}
861
862	switch (cam->nbufs) {
863	case 1:
864	    dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
865			    cam->dma_bufs[0], cam->dma_handles[0]);
866	    cam->nbufs = 0;
867	case 0:
868	    cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
869	    return -ENOMEM;
870
871	case 2:
872	    if (n_dma_bufs > 2)
873		    cam_warn(cam, "Will limp along with only 2 buffers\n");
874	    break;
875	}
876	return 0;
877}
878
879static void cafe_free_dma_bufs(struct cafe_camera *cam)
880{
881	int i;
882
883	for (i = 0; i < cam->nbufs; i++) {
884		dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
885				cam->dma_bufs[i], cam->dma_handles[i]);
886		cam->dma_bufs[i] = NULL;
887	}
888	cam->nbufs = 0;
889}
890
891
892
893
894
895/* ----------------------------------------------------------------------- */
896/*
897 * Here starts the V4L2 interface code.
898 */
899
900/*
901 * Read an image from the device.
902 */
903static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
904		char __user *buffer, size_t len, loff_t *pos)
905{
906	int bufno;
907	unsigned long flags;
908
909	spin_lock_irqsave(&cam->dev_lock, flags);
910	if (cam->next_buf < 0) {
911		cam_err(cam, "deliver_buffer: No next buffer\n");
912		spin_unlock_irqrestore(&cam->dev_lock, flags);
913		return -EIO;
914	}
915	bufno = cam->next_buf;
916	clear_bit(bufno, &cam->flags);
917	if (++(cam->next_buf) >= cam->nbufs)
918		cam->next_buf = 0;
919	if (! test_bit(cam->next_buf, &cam->flags))
920		cam->next_buf = -1;
921	cam->specframes = 0;
922	spin_unlock_irqrestore(&cam->dev_lock, flags);
923
924	if (len > cam->pix_format.sizeimage)
925		len = cam->pix_format.sizeimage;
926	if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
927		return -EFAULT;
928	(*pos) += len;
929	return len;
930}
931
932/*
933 * Get everything ready, and start grabbing frames.
934 */
935static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
936{
937	int ret;
938	unsigned long flags;
939
940	/*
941	 * Configuration.  If we still don't have DMA buffers,
942	 * make one last, desperate attempt.
943	 */
944	if (cam->nbufs == 0)
945		if (cafe_alloc_dma_bufs(cam, 0))
946			return -ENOMEM;
947
948	if (cafe_needs_config(cam)) {
949		cafe_cam_configure(cam);
950		ret = cafe_ctlr_configure(cam);
951		if (ret)
952			return ret;
953	}
954
955	/*
956	 * Turn it loose.
957	 */
958	spin_lock_irqsave(&cam->dev_lock, flags);
959	cafe_reset_buffers(cam);
960	cafe_ctlr_irq_enable(cam);
961	cam->state = state;
962	cafe_ctlr_start(cam);
963	spin_unlock_irqrestore(&cam->dev_lock, flags);
964	return 0;
965}
966
967
968static ssize_t cafe_v4l_read(struct file *filp,
969		char __user *buffer, size_t len, loff_t *pos)
970{
971	struct cafe_camera *cam = filp->private_data;
972	int ret = 0;
973
974	/*
975	 * Perhaps we're in speculative read mode and already
976	 * have data?
977	 */
978	mutex_lock(&cam->s_mutex);
979	if (cam->state == S_SPECREAD) {
980		if (cam->next_buf >= 0) {
981			ret = cafe_deliver_buffer(cam, buffer, len, pos);
982			if (ret != 0)
983				goto out_unlock;
984		}
985	} else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
986		ret = -EIO;
987		goto out_unlock;
988	} else if (cam->state != S_IDLE) {
989		ret = -EBUSY;
990		goto out_unlock;
991	}
992
993	/*
994	 * v4l2: multiple processes can open the device, but only
995	 * one gets to grab data from it.
996	 */
997	if (cam->owner && cam->owner != filp) {
998		ret = -EBUSY;
999		goto out_unlock;
1000	}
1001	cam->owner = filp;
1002
1003	/*
1004	 * Do setup if need be.
1005	 */
1006	if (cam->state != S_SPECREAD) {
1007		ret = cafe_read_setup(cam, S_SINGLEREAD);
1008		if (ret)
1009			goto out_unlock;
1010	}
1011	wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1012	if (cam->next_buf < 0) {
1013		cam_err(cam, "read() operation timed out\n");
1014		cafe_ctlr_stop_dma(cam);
1015		ret = -EIO;
1016		goto out_unlock;
1017	}
1018	/*
1019	 * Give them their data and we should be done.
1020	 */
1021	ret = cafe_deliver_buffer(cam, buffer, len, pos);
1022
1023  out_unlock:
1024	mutex_unlock(&cam->s_mutex);
1025	return ret;
1026}
1027
1028
1029
1030
1031
1032
1033
1034
1035/*
1036 * Streaming I/O support.
1037 */
1038
1039
1040
1041static int cafe_vidioc_streamon(struct file *filp, void *priv,
1042		enum v4l2_buf_type type)
1043{
1044	struct cafe_camera *cam = filp->private_data;
1045	int ret = -EINVAL;
1046
1047	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1048		goto out;
1049	mutex_lock(&cam->s_mutex);
1050	if (cam->state != S_IDLE || cam->n_sbufs == 0)
1051		goto out_unlock;
1052
1053	cam->sequence = 0;
1054	ret = cafe_read_setup(cam, S_STREAMING);
1055
1056  out_unlock:
1057	mutex_unlock(&cam->s_mutex);
1058  out:
1059	return ret;
1060}
1061
1062
1063static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1064		enum v4l2_buf_type type)
1065{
1066	struct cafe_camera *cam = filp->private_data;
1067	int ret = -EINVAL;
1068
1069	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1070		goto out;
1071	mutex_lock(&cam->s_mutex);
1072	if (cam->state != S_STREAMING)
1073		goto out_unlock;
1074
1075	cafe_ctlr_stop_dma(cam);
1076	ret = 0;
1077
1078  out_unlock:
1079	mutex_unlock(&cam->s_mutex);
1080  out:
1081	return ret;
1082}
1083
1084
1085
1086static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1087{
1088	struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1089
1090	INIT_LIST_HEAD(&buf->list);
1091	buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1092	buf->buffer = vmalloc_user(buf->v4lbuf.length);
1093	if (buf->buffer == NULL)
1094		return -ENOMEM;
1095	buf->mapcount = 0;
1096	buf->cam = cam;
1097
1098	buf->v4lbuf.index = index;
1099	buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1100	buf->v4lbuf.field = V4L2_FIELD_NONE;
1101	buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1102	/*
1103	 * Offset: must be 32-bit even on a 64-bit system.  videobuf-dma-sg
1104	 * just uses the length times the index, but the spec warns
1105	 * against doing just that - vma merging problems.  So we
1106	 * leave a gap between each pair of buffers.
1107	 */
1108	buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1109	return 0;
1110}
1111
1112static int cafe_free_sio_buffers(struct cafe_camera *cam)
1113{
1114	int i;
1115
1116	/*
1117	 * If any buffers are mapped, we cannot free them at all.
1118	 */
1119	for (i = 0; i < cam->n_sbufs; i++)
1120		if (cam->sb_bufs[i].mapcount > 0)
1121			return -EBUSY;
1122	/*
1123	 * OK, let's do it.
1124	 */
1125	for (i = 0; i < cam->n_sbufs; i++)
1126		vfree(cam->sb_bufs[i].buffer);
1127	cam->n_sbufs = 0;
1128	kfree(cam->sb_bufs);
1129	cam->sb_bufs = NULL;
1130	INIT_LIST_HEAD(&cam->sb_avail);
1131	INIT_LIST_HEAD(&cam->sb_full);
1132	return 0;
1133}
1134
1135
1136
1137static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1138		struct v4l2_requestbuffers *req)
1139{
1140	struct cafe_camera *cam = filp->private_data;
1141	int ret = 0;  /* Silence warning */
1142
1143	/*
1144	 * Make sure it's something we can do.  User pointers could be
1145	 * implemented without great pain, but that's not been done yet.
1146	 */
1147	if (req->memory != V4L2_MEMORY_MMAP)
1148		return -EINVAL;
1149	/*
1150	 * If they ask for zero buffers, they really want us to stop streaming
1151	 * (if it's happening) and free everything.  Should we check owner?
1152	 */
1153	mutex_lock(&cam->s_mutex);
1154	if (req->count == 0) {
1155		if (cam->state == S_STREAMING)
1156			cafe_ctlr_stop_dma(cam);
1157		ret = cafe_free_sio_buffers (cam);
1158		goto out;
1159	}
1160	/*
1161	 * Device needs to be idle and working.  We *could* try to do the
1162	 * right thing in S_SPECREAD by shutting things down, but it
1163	 * probably doesn't matter.
1164	 */
1165	if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1166		ret = -EBUSY;
1167		goto out;
1168	}
1169	cam->owner = filp;
1170
1171	if (req->count < min_buffers)
1172		req->count = min_buffers;
1173	else if (req->count > max_buffers)
1174		req->count = max_buffers;
1175	if (cam->n_sbufs > 0) {
1176		ret = cafe_free_sio_buffers(cam);
1177		if (ret)
1178			goto out;
1179	}
1180
1181	cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1182			GFP_KERNEL);
1183	if (cam->sb_bufs == NULL) {
1184		ret = -ENOMEM;
1185		goto out;
1186	}
1187	for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1188		ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1189		if (ret)
1190			break;
1191	}
1192
1193	if (cam->n_sbufs == 0)  /* no luck at all - ret already set */
1194		kfree(cam->sb_bufs);
1195	req->count = cam->n_sbufs;  /* In case of partial success */
1196
1197  out:
1198	mutex_unlock(&cam->s_mutex);
1199	return ret;
1200}
1201
1202
1203static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1204		struct v4l2_buffer *buf)
1205{
1206	struct cafe_camera *cam = filp->private_data;
1207	int ret = -EINVAL;
1208
1209	mutex_lock(&cam->s_mutex);
1210	if (buf->index >= cam->n_sbufs)
1211		goto out;
1212	*buf = cam->sb_bufs[buf->index].v4lbuf;
1213	ret = 0;
1214  out:
1215	mutex_unlock(&cam->s_mutex);
1216	return ret;
1217}
1218
1219static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1220		struct v4l2_buffer *buf)
1221{
1222	struct cafe_camera *cam = filp->private_data;
1223	struct cafe_sio_buffer *sbuf;
1224	int ret = -EINVAL;
1225	unsigned long flags;
1226
1227	mutex_lock(&cam->s_mutex);
1228	if (buf->index >= cam->n_sbufs)
1229		goto out;
1230	sbuf = cam->sb_bufs + buf->index;
1231	if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1232		ret = 0; /* Already queued?? */
1233		goto out;
1234	}
1235	if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1236		/* Spec doesn't say anything, seems appropriate tho */
1237		ret = -EBUSY;
1238		goto out;
1239	}
1240	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1241	spin_lock_irqsave(&cam->dev_lock, flags);
1242	list_add(&sbuf->list, &cam->sb_avail);
1243	spin_unlock_irqrestore(&cam->dev_lock, flags);
1244	ret = 0;
1245  out:
1246	mutex_unlock(&cam->s_mutex);
1247	return ret;
1248}
1249
1250static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1251		struct v4l2_buffer *buf)
1252{
1253	struct cafe_camera *cam = filp->private_data;
1254	struct cafe_sio_buffer *sbuf;
1255	int ret = -EINVAL;
1256	unsigned long flags;
1257
1258	mutex_lock(&cam->s_mutex);
1259	if (cam->state != S_STREAMING)
1260		goto out_unlock;
1261	if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1262		ret = -EAGAIN;
1263		goto out_unlock;
1264	}
1265
1266	while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1267		mutex_unlock(&cam->s_mutex);
1268		if (wait_event_interruptible(cam->iowait,
1269						!list_empty(&cam->sb_full))) {
1270			ret = -ERESTARTSYS;
1271			goto out;
1272		}
1273		mutex_lock(&cam->s_mutex);
1274	}
1275
1276	if (cam->state != S_STREAMING)
1277		ret = -EINTR;
1278	else {
1279		spin_lock_irqsave(&cam->dev_lock, flags);
1280		/* Should probably recheck !list_empty() here */
1281		sbuf = list_entry(cam->sb_full.next,
1282				struct cafe_sio_buffer, list);
1283		list_del_init(&sbuf->list);
1284		spin_unlock_irqrestore(&cam->dev_lock, flags);
1285		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1286		*buf = sbuf->v4lbuf;
1287		ret = 0;
1288	}
1289
1290  out_unlock:
1291	mutex_unlock(&cam->s_mutex);
1292  out:
1293	return ret;
1294}
1295
1296
1297
1298static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1299{
1300	struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1301	/*
1302	 * Locking: done under mmap_sem, so we don't need to
1303	 * go back to the camera lock here.
1304	 */
1305	sbuf->mapcount++;
1306}
1307
1308
1309static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1310{
1311	struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1312
1313	mutex_lock(&sbuf->cam->s_mutex);
1314	sbuf->mapcount--;
1315	/* Docs say we should stop I/O too... */
1316	if (sbuf->mapcount == 0)
1317		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1318	mutex_unlock(&sbuf->cam->s_mutex);
1319}
1320
1321static const struct vm_operations_struct cafe_v4l_vm_ops = {
1322	.open = cafe_v4l_vm_open,
1323	.close = cafe_v4l_vm_close
1324};
1325
1326
1327static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1328{
1329	struct cafe_camera *cam = filp->private_data;
1330	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1331	int ret = -EINVAL;
1332	int i;
1333	struct cafe_sio_buffer *sbuf = NULL;
1334
1335	if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1336		return -EINVAL;
1337	/*
1338	 * Find the buffer they are looking for.
1339	 */
1340	mutex_lock(&cam->s_mutex);
1341	for (i = 0; i < cam->n_sbufs; i++)
1342		if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1343			sbuf = cam->sb_bufs + i;
1344			break;
1345		}
1346	if (sbuf == NULL)
1347		goto out;
1348
1349	ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1350	if (ret)
1351		goto out;
1352	vma->vm_flags |= VM_DONTEXPAND;
1353	vma->vm_private_data = sbuf;
1354	vma->vm_ops = &cafe_v4l_vm_ops;
1355	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1356	cafe_v4l_vm_open(vma);
1357	ret = 0;
1358  out:
1359	mutex_unlock(&cam->s_mutex);
1360	return ret;
1361}
1362
1363
1364
1365static int cafe_v4l_open(struct file *filp)
1366{
1367	struct cafe_camera *cam = video_drvdata(filp);
1368
1369	filp->private_data = cam;
1370
1371	mutex_lock(&cam->s_mutex);
1372	if (cam->users == 0) {
1373		cafe_ctlr_power_up(cam);
1374		__cafe_cam_reset(cam);
1375		cafe_set_config_needed(cam, 1);
1376	}
1377	(cam->users)++;
1378	mutex_unlock(&cam->s_mutex);
1379	return 0;
1380}
1381
1382
1383static int cafe_v4l_release(struct file *filp)
1384{
1385	struct cafe_camera *cam = filp->private_data;
1386
1387	mutex_lock(&cam->s_mutex);
1388	(cam->users)--;
1389	if (filp == cam->owner) {
1390		cafe_ctlr_stop_dma(cam);
1391		cafe_free_sio_buffers(cam);
1392		cam->owner = NULL;
1393	}
1394	if (cam->users == 0) {
1395		cafe_ctlr_power_down(cam);
1396		if (alloc_bufs_at_read)
1397			cafe_free_dma_bufs(cam);
1398	}
1399	mutex_unlock(&cam->s_mutex);
1400	return 0;
1401}
1402
1403
1404
1405static unsigned int cafe_v4l_poll(struct file *filp,
1406		struct poll_table_struct *pt)
1407{
1408	struct cafe_camera *cam = filp->private_data;
1409
1410	poll_wait(filp, &cam->iowait, pt);
1411	if (cam->next_buf >= 0)
1412		return POLLIN | POLLRDNORM;
1413	return 0;
1414}
1415
1416
1417
1418static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1419		struct v4l2_queryctrl *qc)
1420{
1421	struct cafe_camera *cam = priv;
1422	int ret;
1423
1424	mutex_lock(&cam->s_mutex);
1425	ret = sensor_call(cam, core, queryctrl, qc);
1426	mutex_unlock(&cam->s_mutex);
1427	return ret;
1428}
1429
1430
1431static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1432		struct v4l2_control *ctrl)
1433{
1434	struct cafe_camera *cam = priv;
1435	int ret;
1436
1437	mutex_lock(&cam->s_mutex);
1438	ret = sensor_call(cam, core, g_ctrl, ctrl);
1439	mutex_unlock(&cam->s_mutex);
1440	return ret;
1441}
1442
1443
1444static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1445		struct v4l2_control *ctrl)
1446{
1447	struct cafe_camera *cam = priv;
1448	int ret;
1449
1450	mutex_lock(&cam->s_mutex);
1451	ret = sensor_call(cam, core, s_ctrl, ctrl);
1452	mutex_unlock(&cam->s_mutex);
1453	return ret;
1454}
1455
1456
1457
1458
1459
1460static int cafe_vidioc_querycap(struct file *file, void *priv,
1461		struct v4l2_capability *cap)
1462{
1463	strcpy(cap->driver, "cafe_ccic");
1464	strcpy(cap->card, "cafe_ccic");
1465	cap->version = CAFE_VERSION;
1466	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1467		V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1468	return 0;
1469}
1470
1471
1472/*
1473 * The default format we use until somebody says otherwise.
1474 */
1475static struct v4l2_pix_format cafe_def_pix_format = {
1476	.width		= VGA_WIDTH,
1477	.height		= VGA_HEIGHT,
1478	.pixelformat	= V4L2_PIX_FMT_YUYV,
1479	.field		= V4L2_FIELD_NONE,
1480	.bytesperline	= VGA_WIDTH*2,
1481	.sizeimage	= VGA_WIDTH*VGA_HEIGHT*2,
1482};
1483
1484static int cafe_vidioc_enum_fmt_vid_cap(struct file *filp,
1485		void *priv, struct v4l2_fmtdesc *fmt)
1486{
1487	struct cafe_camera *cam = priv;
1488	int ret;
1489
1490	mutex_lock(&cam->s_mutex);
1491	ret = sensor_call(cam, video, enum_fmt, fmt);
1492	mutex_unlock(&cam->s_mutex);
1493	return ret;
1494}
1495
1496
1497static int cafe_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1498		struct v4l2_format *fmt)
1499{
1500	struct cafe_camera *cam = priv;
1501	int ret;
1502
1503	mutex_lock(&cam->s_mutex);
1504	ret = sensor_call(cam, video, try_fmt, fmt);
1505	mutex_unlock(&cam->s_mutex);
1506	return ret;
1507}
1508
1509static int cafe_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1510		struct v4l2_format *fmt)
1511{
1512	struct cafe_camera *cam = priv;
1513	int ret;
1514
1515	/*
1516	 * Can't do anything if the device is not idle
1517	 * Also can't if there are streaming buffers in place.
1518	 */
1519	if (cam->state != S_IDLE || cam->n_sbufs > 0)
1520		return -EBUSY;
1521	/*
1522	 * See if the formatting works in principle.
1523	 */
1524	ret = cafe_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1525	if (ret)
1526		return ret;
1527	/*
1528	 * Now we start to change things for real, so let's do it
1529	 * under lock.
1530	 */
1531	mutex_lock(&cam->s_mutex);
1532	cam->pix_format = fmt->fmt.pix;
1533	/*
1534	 * Make sure we have appropriate DMA buffers.
1535	 */
1536	ret = -ENOMEM;
1537	if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1538		cafe_free_dma_bufs(cam);
1539	if (cam->nbufs == 0) {
1540		if (cafe_alloc_dma_bufs(cam, 0))
1541			goto out;
1542	}
1543	/*
1544	 * It looks like this might work, so let's program the sensor.
1545	 */
1546	ret = cafe_cam_configure(cam);
1547	if (! ret)
1548		ret = cafe_ctlr_configure(cam);
1549  out:
1550	mutex_unlock(&cam->s_mutex);
1551	return ret;
1552}
1553
1554/*
1555 * Return our stored notion of how the camera is/should be configured.
1556 * The V4l2 spec wants us to be smarter, and actually get this from
1557 * the camera (and not mess with it at open time).  Someday.
1558 */
1559static int cafe_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1560		struct v4l2_format *f)
1561{
1562	struct cafe_camera *cam = priv;
1563
1564	f->fmt.pix = cam->pix_format;
1565	return 0;
1566}
1567
1568/*
1569 * We only have one input - the sensor - so minimize the nonsense here.
1570 */
1571static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1572		struct v4l2_input *input)
1573{
1574	if (input->index != 0)
1575		return -EINVAL;
1576
1577	input->type = V4L2_INPUT_TYPE_CAMERA;
1578	input->std = V4L2_STD_ALL; /* Not sure what should go here */
1579	strcpy(input->name, "Camera");
1580	return 0;
1581}
1582
1583static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1584{
1585	*i = 0;
1586	return 0;
1587}
1588
1589static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1590{
1591	if (i != 0)
1592		return -EINVAL;
1593	return 0;
1594}
1595
1596/* from vivi.c */
1597static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1598{
1599	return 0;
1600}
1601
1602/*
1603 * G/S_PARM.  Most of this is done by the sensor, but we are
1604 * the level which controls the number of read buffers.
1605 */
1606static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1607		struct v4l2_streamparm *parms)
1608{
1609	struct cafe_camera *cam = priv;
1610	int ret;
1611
1612	mutex_lock(&cam->s_mutex);
1613	ret = sensor_call(cam, video, g_parm, parms);
1614	mutex_unlock(&cam->s_mutex);
1615	parms->parm.capture.readbuffers = n_dma_bufs;
1616	return ret;
1617}
1618
1619static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1620		struct v4l2_streamparm *parms)
1621{
1622	struct cafe_camera *cam = priv;
1623	int ret;
1624
1625	mutex_lock(&cam->s_mutex);
1626	ret = sensor_call(cam, video, s_parm, parms);
1627	mutex_unlock(&cam->s_mutex);
1628	parms->parm.capture.readbuffers = n_dma_bufs;
1629	return ret;
1630}
1631
1632static int cafe_vidioc_g_chip_ident(struct file *file, void *priv,
1633		struct v4l2_dbg_chip_ident *chip)
1634{
1635	struct cafe_camera *cam = priv;
1636
1637	chip->ident = V4L2_IDENT_NONE;
1638	chip->revision = 0;
1639	if (v4l2_chip_match_host(&chip->match)) {
1640		chip->ident = V4L2_IDENT_CAFE;
1641		return 0;
1642	}
1643	return sensor_call(cam, core, g_chip_ident, chip);
1644}
1645
1646#ifdef CONFIG_VIDEO_ADV_DEBUG
1647static int cafe_vidioc_g_register(struct file *file, void *priv,
1648		struct v4l2_dbg_register *reg)
1649{
1650	struct cafe_camera *cam = priv;
1651
1652	if (v4l2_chip_match_host(&reg->match)) {
1653		reg->val = cafe_reg_read(cam, reg->reg);
1654		reg->size = 4;
1655		return 0;
1656	}
1657	return sensor_call(cam, core, g_register, reg);
1658}
1659
1660static int cafe_vidioc_s_register(struct file *file, void *priv,
1661		struct v4l2_dbg_register *reg)
1662{
1663	struct cafe_camera *cam = priv;
1664
1665	if (v4l2_chip_match_host(&reg->match)) {
1666		cafe_reg_write(cam, reg->reg, reg->val);
1667		return 0;
1668	}
1669	return sensor_call(cam, core, s_register, reg);
1670}
1671#endif
1672
1673/*
1674 * This template device holds all of those v4l2 methods; we
1675 * clone it for specific real devices.
1676 */
1677
1678static const struct v4l2_file_operations cafe_v4l_fops = {
1679	.owner = THIS_MODULE,
1680	.open = cafe_v4l_open,
1681	.release = cafe_v4l_release,
1682	.read = cafe_v4l_read,
1683	.poll = cafe_v4l_poll,
1684	.mmap = cafe_v4l_mmap,
1685	.ioctl = video_ioctl2,
1686};
1687
1688static const struct v4l2_ioctl_ops cafe_v4l_ioctl_ops = {
1689	.vidioc_querycap 	= cafe_vidioc_querycap,
1690	.vidioc_enum_fmt_vid_cap = cafe_vidioc_enum_fmt_vid_cap,
1691	.vidioc_try_fmt_vid_cap	= cafe_vidioc_try_fmt_vid_cap,
1692	.vidioc_s_fmt_vid_cap	= cafe_vidioc_s_fmt_vid_cap,
1693	.vidioc_g_fmt_vid_cap	= cafe_vidioc_g_fmt_vid_cap,
1694	.vidioc_enum_input	= cafe_vidioc_enum_input,
1695	.vidioc_g_input		= cafe_vidioc_g_input,
1696	.vidioc_s_input		= cafe_vidioc_s_input,
1697	.vidioc_s_std		= cafe_vidioc_s_std,
1698	.vidioc_reqbufs		= cafe_vidioc_reqbufs,
1699	.vidioc_querybuf	= cafe_vidioc_querybuf,
1700	.vidioc_qbuf		= cafe_vidioc_qbuf,
1701	.vidioc_dqbuf		= cafe_vidioc_dqbuf,
1702	.vidioc_streamon	= cafe_vidioc_streamon,
1703	.vidioc_streamoff	= cafe_vidioc_streamoff,
1704	.vidioc_queryctrl	= cafe_vidioc_queryctrl,
1705	.vidioc_g_ctrl		= cafe_vidioc_g_ctrl,
1706	.vidioc_s_ctrl		= cafe_vidioc_s_ctrl,
1707	.vidioc_g_parm		= cafe_vidioc_g_parm,
1708	.vidioc_s_parm		= cafe_vidioc_s_parm,
1709	.vidioc_g_chip_ident    = cafe_vidioc_g_chip_ident,
1710#ifdef CONFIG_VIDEO_ADV_DEBUG
1711	.vidioc_g_register 	= cafe_vidioc_g_register,
1712	.vidioc_s_register 	= cafe_vidioc_s_register,
1713#endif
1714};
1715
1716static struct video_device cafe_v4l_template = {
1717	.name = "cafe",
1718	.tvnorms = V4L2_STD_NTSC_M,
1719	.current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
1720
1721	.fops = &cafe_v4l_fops,
1722	.ioctl_ops = &cafe_v4l_ioctl_ops,
1723	.release = video_device_release_empty,
1724};
1725
1726
1727/* ---------------------------------------------------------------------- */
1728/*
1729 * Interrupt handler stuff
1730 */
1731
1732
1733
1734static void cafe_frame_tasklet(unsigned long data)
1735{
1736	struct cafe_camera *cam = (struct cafe_camera *) data;
1737	int i;
1738	unsigned long flags;
1739	struct cafe_sio_buffer *sbuf;
1740
1741	spin_lock_irqsave(&cam->dev_lock, flags);
1742	for (i = 0; i < cam->nbufs; i++) {
1743		int bufno = cam->next_buf;
1744		if (bufno < 0) {  /* "will never happen" */
1745			cam_err(cam, "No valid bufs in tasklet!\n");
1746			break;
1747		}
1748		if (++(cam->next_buf) >= cam->nbufs)
1749			cam->next_buf = 0;
1750		if (! test_bit(bufno, &cam->flags))
1751			continue;
1752		if (list_empty(&cam->sb_avail))
1753			break;  /* Leave it valid, hope for better later */
1754		clear_bit(bufno, &cam->flags);
1755		sbuf = list_entry(cam->sb_avail.next,
1756				struct cafe_sio_buffer, list);
1757		/*
1758		 * Drop the lock during the big copy.  This *should* be safe...
1759		 */
1760		spin_unlock_irqrestore(&cam->dev_lock, flags);
1761		memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1762				cam->pix_format.sizeimage);
1763		sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1764		sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1765		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1766		sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1767		spin_lock_irqsave(&cam->dev_lock, flags);
1768		list_move_tail(&sbuf->list, &cam->sb_full);
1769	}
1770	if (! list_empty(&cam->sb_full))
1771		wake_up(&cam->iowait);
1772	spin_unlock_irqrestore(&cam->dev_lock, flags);
1773}
1774
1775
1776
1777static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1778{
1779	/*
1780	 * Basic frame housekeeping.
1781	 */
1782	if (test_bit(frame, &cam->flags) && printk_ratelimit())
1783		cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1784	set_bit(frame, &cam->flags);
1785	clear_bit(CF_DMA_ACTIVE, &cam->flags);
1786	if (cam->next_buf < 0)
1787		cam->next_buf = frame;
1788	cam->buf_seq[frame] = ++(cam->sequence);
1789
1790	switch (cam->state) {
1791	/*
1792	 * If in single read mode, try going speculative.
1793	 */
1794	    case S_SINGLEREAD:
1795		cam->state = S_SPECREAD;
1796		cam->specframes = 0;
1797		wake_up(&cam->iowait);
1798		break;
1799
1800	/*
1801	 * If we are already doing speculative reads, and nobody is
1802	 * reading them, just stop.
1803	 */
1804	    case S_SPECREAD:
1805		if (++(cam->specframes) >= cam->nbufs) {
1806			cafe_ctlr_stop(cam);
1807			cafe_ctlr_irq_disable(cam);
1808			cam->state = S_IDLE;
1809		}
1810		wake_up(&cam->iowait);
1811		break;
1812	    case S_STREAMING:
1813		tasklet_schedule(&cam->s_tasklet);
1814		break;
1815
1816	    default:
1817		cam_err(cam, "Frame interrupt in non-operational state\n");
1818		break;
1819	}
1820}
1821
1822
1823
1824
1825static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1826{
1827	unsigned int frame;
1828
1829	cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1830	/*
1831	 * Handle any frame completions.  There really should
1832	 * not be more than one of these, or we have fallen
1833	 * far behind.
1834	 */
1835	for (frame = 0; frame < cam->nbufs; frame++)
1836		if (irqs & (IRQ_EOF0 << frame))
1837			cafe_frame_complete(cam, frame);
1838	/*
1839	 * If a frame starts, note that we have DMA active.  This
1840	 * code assumes that we won't get multiple frame interrupts
1841	 * at once; may want to rethink that.
1842	 */
1843	if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1844		set_bit(CF_DMA_ACTIVE, &cam->flags);
1845}
1846
1847
1848
1849static irqreturn_t cafe_irq(int irq, void *data)
1850{
1851	struct cafe_camera *cam = data;
1852	unsigned int irqs;
1853
1854	spin_lock(&cam->dev_lock);
1855	irqs = cafe_reg_read(cam, REG_IRQSTAT);
1856	if ((irqs & ALLIRQS) == 0) {
1857		spin_unlock(&cam->dev_lock);
1858		return IRQ_NONE;
1859	}
1860	if (irqs & FRAMEIRQS)
1861		cafe_frame_irq(cam, irqs);
1862	if (irqs & TWSIIRQS) {
1863		cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1864		wake_up(&cam->smbus_wait);
1865	}
1866	spin_unlock(&cam->dev_lock);
1867	return IRQ_HANDLED;
1868}
1869
1870
1871/* -------------------------------------------------------------------------- */
1872/*
1873 * PCI interface stuff.
1874 */
1875
1876static int cafe_pci_probe(struct pci_dev *pdev,
1877		const struct pci_device_id *id)
1878{
1879	int ret;
1880	struct cafe_camera *cam;
1881
1882	/*
1883	 * Start putting together one of our big camera structures.
1884	 */
1885	ret = -ENOMEM;
1886	cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
1887	if (cam == NULL)
1888		goto out;
1889	ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev);
1890	if (ret)
1891		goto out_free;
1892
1893	mutex_init(&cam->s_mutex);
1894	spin_lock_init(&cam->dev_lock);
1895	cam->state = S_NOTREADY;
1896	cafe_set_config_needed(cam, 1);
1897	init_waitqueue_head(&cam->smbus_wait);
1898	init_waitqueue_head(&cam->iowait);
1899	cam->pdev = pdev;
1900	cam->pix_format = cafe_def_pix_format;
1901	INIT_LIST_HEAD(&cam->dev_list);
1902	INIT_LIST_HEAD(&cam->sb_avail);
1903	INIT_LIST_HEAD(&cam->sb_full);
1904	tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
1905	/*
1906	 * Get set up on the PCI bus.
1907	 */
1908	ret = pci_enable_device(pdev);
1909	if (ret)
1910		goto out_unreg;
1911	pci_set_master(pdev);
1912
1913	ret = -EIO;
1914	cam->regs = pci_iomap(pdev, 0, 0);
1915	if (! cam->regs) {
1916		printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
1917		goto out_unreg;
1918	}
1919	ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
1920	if (ret)
1921		goto out_iounmap;
1922	/*
1923	 * Initialize the controller and leave it powered up.  It will
1924	 * stay that way until the sensor driver shows up.
1925	 */
1926	cafe_ctlr_init(cam);
1927	cafe_ctlr_power_up(cam);
1928	/*
1929	 * Set up I2C/SMBUS communications.  We have to drop the mutex here
1930	 * because the sensor could attach in this call chain, leading to
1931	 * unsightly deadlocks.
1932	 */
1933	ret = cafe_smbus_setup(cam);
1934	if (ret)
1935		goto out_freeirq;
1936
1937	cam->sensor_addr = 0x42;
1938	cam->sensor = v4l2_i2c_new_subdev(&cam->v4l2_dev, &cam->i2c_adapter,
1939			"ov7670", "ov7670", cam->sensor_addr, NULL);
1940	if (cam->sensor == NULL) {
1941		ret = -ENODEV;
1942		goto out_smbus;
1943	}
1944	ret = cafe_cam_init(cam);
1945	if (ret)
1946		goto out_smbus;
1947
1948	/*
1949	 * Get the v4l2 setup done.
1950	 */
1951	mutex_lock(&cam->s_mutex);
1952	cam->vdev = cafe_v4l_template;
1953	cam->vdev.debug = 0;
1954/*	cam->vdev.debug = V4L2_DEBUG_IOCTL_ARG;*/
1955	cam->vdev.v4l2_dev = &cam->v4l2_dev;
1956	ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1957	if (ret)
1958		goto out_unlock;
1959	video_set_drvdata(&cam->vdev, cam);
1960
1961	/*
1962	 * If so requested, try to get our DMA buffers now.
1963	 */
1964	if (!alloc_bufs_at_read) {
1965		if (cafe_alloc_dma_bufs(cam, 1))
1966			cam_warn(cam, "Unable to alloc DMA buffers at load"
1967					" will try again later.");
1968	}
1969
1970	mutex_unlock(&cam->s_mutex);
1971	return 0;
1972
1973out_unlock:
1974	mutex_unlock(&cam->s_mutex);
1975out_smbus:
1976	cafe_smbus_shutdown(cam);
1977out_freeirq:
1978	cafe_ctlr_power_down(cam);
1979	free_irq(pdev->irq, cam);
1980out_iounmap:
1981	pci_iounmap(pdev, cam->regs);
1982out_free:
1983	v4l2_device_unregister(&cam->v4l2_dev);
1984out_unreg:
1985	kfree(cam);
1986out:
1987	return ret;
1988}
1989
1990
1991/*
1992 * Shut down an initialized device
1993 */
1994static void cafe_shutdown(struct cafe_camera *cam)
1995{
1996	if (cam->n_sbufs > 0)
1997		/* What if they are still mapped?  Shouldn't be, but... */
1998		cafe_free_sio_buffers(cam);
1999	cafe_ctlr_stop_dma(cam);
2000	cafe_ctlr_power_down(cam);
2001	cafe_smbus_shutdown(cam);
2002	cafe_free_dma_bufs(cam);
2003	free_irq(cam->pdev->irq, cam);
2004	pci_iounmap(cam->pdev, cam->regs);
2005	video_unregister_device(&cam->vdev);
2006}
2007
2008
2009static void cafe_pci_remove(struct pci_dev *pdev)
2010{
2011	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2012	struct cafe_camera *cam = to_cam(v4l2_dev);
2013
2014	if (cam == NULL) {
2015		printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2016		return;
2017	}
2018	mutex_lock(&cam->s_mutex);
2019	if (cam->users > 0)
2020		cam_warn(cam, "Removing a device with users!\n");
2021	cafe_shutdown(cam);
2022	v4l2_device_unregister(&cam->v4l2_dev);
2023	kfree(cam);
2024/* No unlock - it no longer exists */
2025}
2026
2027
2028#ifdef CONFIG_PM
2029/*
2030 * Basic power management.
2031 */
2032static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2033{
2034	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2035	struct cafe_camera *cam = to_cam(v4l2_dev);
2036	int ret;
2037	enum cafe_state cstate;
2038
2039	ret = pci_save_state(pdev);
2040	if (ret)
2041		return ret;
2042	cstate = cam->state; /* HACK - stop_dma sets to idle */
2043	cafe_ctlr_stop_dma(cam);
2044	cafe_ctlr_power_down(cam);
2045	pci_disable_device(pdev);
2046	cam->state = cstate;
2047	return 0;
2048}
2049
2050
2051static int cafe_pci_resume(struct pci_dev *pdev)
2052{
2053	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
2054	struct cafe_camera *cam = to_cam(v4l2_dev);
2055	int ret = 0;
2056
2057	ret = pci_restore_state(pdev);
2058	if (ret)
2059		return ret;
2060	ret = pci_enable_device(pdev);
2061
2062	if (ret) {
2063		cam_warn(cam, "Unable to re-enable device on resume!\n");
2064		return ret;
2065	}
2066	cafe_ctlr_init(cam);
2067	cafe_ctlr_power_down(cam);
2068
2069	mutex_lock(&cam->s_mutex);
2070	if (cam->users > 0) {
2071		cafe_ctlr_power_up(cam);
2072		__cafe_cam_reset(cam);
2073	}
2074	mutex_unlock(&cam->s_mutex);
2075
2076	set_bit(CF_CONFIG_NEEDED, &cam->flags);
2077	if (cam->state == S_SPECREAD)
2078		cam->state = S_IDLE;  /* Don't bother restarting */
2079	else if (cam->state == S_SINGLEREAD || cam->state == S_STREAMING)
2080		ret = cafe_read_setup(cam, cam->state);
2081	return ret;
2082}
2083
2084#endif  /* CONFIG_PM */
2085
2086
2087static struct pci_device_id cafe_ids[] = {
2088	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
2089		     PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
2090	{ 0, }
2091};
2092
2093MODULE_DEVICE_TABLE(pci, cafe_ids);
2094
2095static struct pci_driver cafe_pci_driver = {
2096	.name = "cafe1000-ccic",
2097	.id_table = cafe_ids,
2098	.probe = cafe_pci_probe,
2099	.remove = cafe_pci_remove,
2100#ifdef CONFIG_PM
2101	.suspend = cafe_pci_suspend,
2102	.resume = cafe_pci_resume,
2103#endif
2104};
2105
2106
2107
2108
2109static int __init cafe_init(void)
2110{
2111	int ret;
2112
2113	printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2114			CAFE_VERSION);
2115	ret = pci_register_driver(&cafe_pci_driver);
2116	if (ret) {
2117		printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2118		goto out;
2119	}
2120	ret = 0;
2121
2122  out:
2123	return ret;
2124}
2125
2126
2127static void __exit cafe_exit(void)
2128{
2129	pci_unregister_driver(&cafe_pci_driver);
2130}
2131
2132module_init(cafe_init);
2133module_exit(cafe_exit);
2134