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