i2c: Declare more i2c_adapter parent devices
[safe/jmp/linux-2.6] / 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         adap->dev.parent = &cam->pdev->dev;
553         i2c_set_adapdata(adap, cam);
554         ret = i2c_add_adapter(adap);
555         if (ret)
556                 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
557         return ret;
558 }
559
560 static void cafe_smbus_shutdown(struct cafe_camera *cam)
561 {
562         i2c_del_adapter(&cam->i2c_adapter);
563 }
564
565
566 /* ------------------------------------------------------------------- */
567 /*
568  * Deal with the controller.
569  */
570
571 /*
572  * Do everything we think we need to have the interface operating
573  * according to the desired format.
574  */
575 static void cafe_ctlr_dma(struct cafe_camera *cam)
576 {
577         /*
578          * Store the first two Y buffers (we aren't supporting
579          * planar formats for now, so no UV bufs).  Then either
580          * set the third if it exists, or tell the controller
581          * to just use two.
582          */
583         cafe_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
584         cafe_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
585         if (cam->nbufs > 2) {
586                 cafe_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
587                 cafe_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
588         }
589         else
590                 cafe_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
591         cafe_reg_write(cam, REG_UBAR, 0); /* 32 bits only for now */
592 }
593
594 static void cafe_ctlr_image(struct cafe_camera *cam)
595 {
596         int imgsz;
597         struct v4l2_pix_format *fmt = &cam->pix_format;
598
599         imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
600                 (fmt->bytesperline & IMGSZ_H_MASK);
601         cafe_reg_write(cam, REG_IMGSIZE, imgsz);
602         cafe_reg_write(cam, REG_IMGOFFSET, 0);
603         /* YPITCH just drops the last two bits */
604         cafe_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
605                         IMGP_YP_MASK);
606         /*
607          * Tell the controller about the image format we are using.
608          */
609         switch (cam->pix_format.pixelformat) {
610         case V4L2_PIX_FMT_YUYV:
611             cafe_reg_write_mask(cam, REG_CTRL0,
612                             C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
613                             C0_DF_MASK);
614             break;
615
616         case V4L2_PIX_FMT_RGB444:
617             cafe_reg_write_mask(cam, REG_CTRL0,
618                             C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
619                             C0_DF_MASK);
620                 /* Alpha value? */
621             break;
622
623         case V4L2_PIX_FMT_RGB565:
624             cafe_reg_write_mask(cam, REG_CTRL0,
625                             C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
626                             C0_DF_MASK);
627             break;
628
629         default:
630             cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
631             break;
632         }
633         /*
634          * Make sure it knows we want to use hsync/vsync.
635          */
636         cafe_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
637                         C0_SIFM_MASK);
638 }
639
640
641 /*
642  * Configure the controller for operation; caller holds the
643  * device mutex.
644  */
645 static int cafe_ctlr_configure(struct cafe_camera *cam)
646 {
647         unsigned long flags;
648
649         spin_lock_irqsave(&cam->dev_lock, flags);
650         cafe_ctlr_dma(cam);
651         cafe_ctlr_image(cam);
652         cafe_set_config_needed(cam, 0);
653         spin_unlock_irqrestore(&cam->dev_lock, flags);
654         return 0;
655 }
656
657 static void cafe_ctlr_irq_enable(struct cafe_camera *cam)
658 {
659         /*
660          * Clear any pending interrupts, since we do not
661          * expect to have I/O active prior to enabling.
662          */
663         cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
664         cafe_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
665 }
666
667 static void cafe_ctlr_irq_disable(struct cafe_camera *cam)
668 {
669         cafe_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
670 }
671
672 /*
673  * Make the controller start grabbing images.  Everything must
674  * be set up before doing this.
675  */
676 static void cafe_ctlr_start(struct cafe_camera *cam)
677 {
678         /* set_bit performs a read, so no other barrier should be
679            needed here */
680         cafe_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
681 }
682
683 static void cafe_ctlr_stop(struct cafe_camera *cam)
684 {
685         cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
686 }
687
688 static void cafe_ctlr_init(struct cafe_camera *cam)
689 {
690         unsigned long flags;
691
692         spin_lock_irqsave(&cam->dev_lock, flags);
693         /*
694          * Added magic to bring up the hardware on the B-Test board
695          */
696         cafe_reg_write(cam, 0x3038, 0x8);
697         cafe_reg_write(cam, 0x315c, 0x80008);
698         /*
699          * Go through the dance needed to wake the device up.
700          * Note that these registers are global and shared
701          * with the NAND and SD devices.  Interaction between the
702          * three still needs to be examined.
703          */
704         cafe_reg_write(cam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
705         cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
706         cafe_reg_write(cam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
707         mdelay(5);      /* FIXME revisit this */
708         cafe_reg_write(cam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
709         cafe_reg_set_bit(cam, REG_GL_IMASK, GIMSK_CCIC_EN);
710         /*
711          * Make sure it's not powered down.
712          */
713         cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
714         /*
715          * Turn off the enable bit.  It sure should be off anyway,
716          * but it's good to be sure.
717          */
718         cafe_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
719         /*
720          * Mask all interrupts.
721          */
722         cafe_reg_write(cam, REG_IRQMASK, 0);
723         /*
724          * Clock the sensor appropriately.  Controller clock should
725          * be 48MHz, sensor "typical" value is half that.
726          */
727         cafe_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
728         spin_unlock_irqrestore(&cam->dev_lock, flags);
729 }
730
731
732 /*
733  * Stop the controller, and don't return until we're really sure that no
734  * further DMA is going on.
735  */
736 static void cafe_ctlr_stop_dma(struct cafe_camera *cam)
737 {
738         unsigned long flags;
739
740         /*
741          * Theory: stop the camera controller (whether it is operating
742          * or not).  Delay briefly just in case we race with the SOF
743          * interrupt, then wait until no DMA is active.
744          */
745         spin_lock_irqsave(&cam->dev_lock, flags);
746         cafe_ctlr_stop(cam);
747         spin_unlock_irqrestore(&cam->dev_lock, flags);
748         mdelay(1);
749         wait_event_timeout(cam->iowait,
750                         !test_bit(CF_DMA_ACTIVE, &cam->flags), HZ);
751         if (test_bit(CF_DMA_ACTIVE, &cam->flags))
752                 cam_err(cam, "Timeout waiting for DMA to end\n");
753                 /* This would be bad news - what now? */
754         spin_lock_irqsave(&cam->dev_lock, flags);
755         cam->state = S_IDLE;
756         cafe_ctlr_irq_disable(cam);
757         spin_unlock_irqrestore(&cam->dev_lock, flags);
758 }
759
760 /*
761  * Power up and down.
762  */
763 static void cafe_ctlr_power_up(struct cafe_camera *cam)
764 {
765         unsigned long flags;
766
767         spin_lock_irqsave(&cam->dev_lock, flags);
768         cafe_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
769         /*
770          * Put the sensor into operational mode (assumes OLPC-style
771          * wiring).  Control 0 is reset - set to 1 to operate.
772          * Control 1 is power down, set to 0 to operate.
773          */
774         cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
775         mdelay(1); /* Marvell says 1ms will do it */
776         cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
777         mdelay(1); /* Enough? */
778         spin_unlock_irqrestore(&cam->dev_lock, flags);
779 }
780
781 static void cafe_ctlr_power_down(struct cafe_camera *cam)
782 {
783         unsigned long flags;
784
785         spin_lock_irqsave(&cam->dev_lock, flags);
786         cafe_reg_write(cam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
787         cafe_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
788         spin_unlock_irqrestore(&cam->dev_lock, flags);
789 }
790
791 /* -------------------------------------------------------------------- */
792 /*
793  * Communications with the sensor.
794  */
795
796 static int __cafe_cam_cmd(struct cafe_camera *cam, int cmd, void *arg)
797 {
798         struct i2c_client *sc = cam->sensor;
799         int ret;
800
801         if (sc == NULL || sc->driver == NULL || sc->driver->command == NULL)
802                 return -EINVAL;
803         ret = sc->driver->command(sc, cmd, arg);
804         if (ret == -EPERM) /* Unsupported command */
805                 return 0;
806         return ret;
807 }
808
809 static int __cafe_cam_reset(struct cafe_camera *cam)
810 {
811         int zero = 0;
812         return __cafe_cam_cmd(cam, VIDIOC_INT_RESET, &zero);
813 }
814
815 /*
816  * We have found the sensor on the i2c.  Let's try to have a
817  * conversation.
818  */
819 static int cafe_cam_init(struct cafe_camera *cam)
820 {
821         int ret;
822
823         mutex_lock(&cam->s_mutex);
824         if (cam->state != S_NOTREADY)
825                 cam_warn(cam, "Cam init with device in funky state %d",
826                                 cam->state);
827         ret = __cafe_cam_reset(cam);
828         if (ret)
829                 goto out;
830         ret = __cafe_cam_cmd(cam, VIDIOC_INT_G_CHIP_IDENT, &cam->sensor_type);
831         if (ret)
832                 goto out;
833 //      if (cam->sensor->addr != OV7xx0_SID) {
834         if (cam->sensor_type != V4L2_IDENT_OV7670) {
835                 cam_err(cam, "Unsupported sensor type %d", cam->sensor->addr);
836                 ret = -EINVAL;
837                 goto out;
838         }
839 /* Get/set parameters? */
840         ret = 0;
841         cam->state = S_IDLE;
842   out:
843         mutex_unlock(&cam->s_mutex);
844         return ret;
845 }
846
847 /*
848  * Configure the sensor to match the parameters we have.  Caller should
849  * hold s_mutex
850  */
851 static int cafe_cam_set_flip(struct cafe_camera *cam)
852 {
853         struct v4l2_control ctrl;
854
855         memset(&ctrl, 0, sizeof(ctrl));
856         ctrl.id = V4L2_CID_VFLIP;
857         ctrl.value = flip;
858         return __cafe_cam_cmd(cam, VIDIOC_S_CTRL, &ctrl);
859 }
860
861
862 static int cafe_cam_configure(struct cafe_camera *cam)
863 {
864         struct v4l2_format fmt;
865         int ret, zero = 0;
866
867         if (cam->state != S_IDLE)
868                 return -EINVAL;
869         fmt.fmt.pix = cam->pix_format;
870         ret = __cafe_cam_cmd(cam, VIDIOC_INT_INIT, &zero);
871         if (ret == 0)
872                 ret = __cafe_cam_cmd(cam, VIDIOC_S_FMT, &fmt);
873         /*
874          * OV7670 does weird things if flip is set *before* format...
875          */
876         ret += cafe_cam_set_flip(cam);
877         return ret;
878 }
879
880 /* -------------------------------------------------------------------- */
881 /*
882  * DMA buffer management.  These functions need s_mutex held.
883  */
884
885 /* FIXME: this is inefficient as hell, since dma_alloc_coherent just
886  * does a get_free_pages() call, and we waste a good chunk of an orderN
887  * allocation.  Should try to allocate the whole set in one chunk.
888  */
889 static int cafe_alloc_dma_bufs(struct cafe_camera *cam, int loadtime)
890 {
891         int i;
892
893         cafe_set_config_needed(cam, 1);
894         if (loadtime)
895                 cam->dma_buf_size = dma_buf_size;
896         else
897                 cam->dma_buf_size = cam->pix_format.sizeimage;
898         if (n_dma_bufs > 3)
899                 n_dma_bufs = 3;
900
901         cam->nbufs = 0;
902         for (i = 0; i < n_dma_bufs; i++) {
903                 cam->dma_bufs[i] = dma_alloc_coherent(&cam->pdev->dev,
904                                 cam->dma_buf_size, cam->dma_handles + i,
905                                 GFP_KERNEL);
906                 if (cam->dma_bufs[i] == NULL) {
907                         cam_warn(cam, "Failed to allocate DMA buffer\n");
908                         break;
909                 }
910                 /* For debug, remove eventually */
911                 memset(cam->dma_bufs[i], 0xcc, cam->dma_buf_size);
912                 (cam->nbufs)++;
913         }
914
915         switch (cam->nbufs) {
916         case 1:
917             dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
918                             cam->dma_bufs[0], cam->dma_handles[0]);
919             cam->nbufs = 0;
920         case 0:
921             cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
922             return -ENOMEM;
923
924         case 2:
925             if (n_dma_bufs > 2)
926                     cam_warn(cam, "Will limp along with only 2 buffers\n");
927             break;
928         }
929         return 0;
930 }
931
932 static void cafe_free_dma_bufs(struct cafe_camera *cam)
933 {
934         int i;
935
936         for (i = 0; i < cam->nbufs; i++) {
937                 dma_free_coherent(&cam->pdev->dev, cam->dma_buf_size,
938                                 cam->dma_bufs[i], cam->dma_handles[i]);
939                 cam->dma_bufs[i] = NULL;
940         }
941         cam->nbufs = 0;
942 }
943
944
945
946
947
948 /* ----------------------------------------------------------------------- */
949 /*
950  * Here starts the V4L2 interface code.
951  */
952
953 /*
954  * Read an image from the device.
955  */
956 static ssize_t cafe_deliver_buffer(struct cafe_camera *cam,
957                 char __user *buffer, size_t len, loff_t *pos)
958 {
959         int bufno;
960         unsigned long flags;
961
962         spin_lock_irqsave(&cam->dev_lock, flags);
963         if (cam->next_buf < 0) {
964                 cam_err(cam, "deliver_buffer: No next buffer\n");
965                 spin_unlock_irqrestore(&cam->dev_lock, flags);
966                 return -EIO;
967         }
968         bufno = cam->next_buf;
969         clear_bit(bufno, &cam->flags);
970         if (++(cam->next_buf) >= cam->nbufs)
971                 cam->next_buf = 0;
972         if (! test_bit(cam->next_buf, &cam->flags))
973                 cam->next_buf = -1;
974         cam->specframes = 0;
975         spin_unlock_irqrestore(&cam->dev_lock, flags);
976
977         if (len > cam->pix_format.sizeimage)
978                 len = cam->pix_format.sizeimage;
979         if (copy_to_user(buffer, cam->dma_bufs[bufno], len))
980                 return -EFAULT;
981         (*pos) += len;
982         return len;
983 }
984
985 /*
986  * Get everything ready, and start grabbing frames.
987  */
988 static int cafe_read_setup(struct cafe_camera *cam, enum cafe_state state)
989 {
990         int ret;
991         unsigned long flags;
992
993         /*
994          * Configuration.  If we still don't have DMA buffers,
995          * make one last, desperate attempt.
996          */
997         if (cam->nbufs == 0)
998                 if (cafe_alloc_dma_bufs(cam, 0))
999                         return -ENOMEM;
1000
1001         if (cafe_needs_config(cam)) {
1002                 cafe_cam_configure(cam);
1003                 ret = cafe_ctlr_configure(cam);
1004                 if (ret)
1005                         return ret;
1006         }
1007
1008         /*
1009          * Turn it loose.
1010          */
1011         spin_lock_irqsave(&cam->dev_lock, flags);
1012         cafe_reset_buffers(cam);
1013         cafe_ctlr_irq_enable(cam);
1014         cam->state = state;
1015         cafe_ctlr_start(cam);
1016         spin_unlock_irqrestore(&cam->dev_lock, flags);
1017         return 0;
1018 }
1019
1020
1021 static ssize_t cafe_v4l_read(struct file *filp,
1022                 char __user *buffer, size_t len, loff_t *pos)
1023 {
1024         struct cafe_camera *cam = filp->private_data;
1025         int ret;
1026
1027         /*
1028          * Perhaps we're in speculative read mode and already
1029          * have data?
1030          */
1031         mutex_lock(&cam->s_mutex);
1032         if (cam->state == S_SPECREAD) {
1033                 if (cam->next_buf >= 0) {
1034                         ret = cafe_deliver_buffer(cam, buffer, len, pos);
1035                         if (ret != 0)
1036                                 goto out_unlock;
1037                 }
1038         } else if (cam->state == S_FLAKED || cam->state == S_NOTREADY) {
1039                 ret = -EIO;
1040                 goto out_unlock;
1041         } else if (cam->state != S_IDLE) {
1042                 ret = -EBUSY;
1043                 goto out_unlock;
1044         }
1045
1046         /*
1047          * v4l2: multiple processes can open the device, but only
1048          * one gets to grab data from it.
1049          */
1050         if (cam->owner && cam->owner != filp) {
1051                 ret = -EBUSY;
1052                 goto out_unlock;
1053         }
1054         cam->owner = filp;
1055
1056         /*
1057          * Do setup if need be.
1058          */
1059         if (cam->state != S_SPECREAD) {
1060                 ret = cafe_read_setup(cam, S_SINGLEREAD);
1061                 if (ret)
1062                         goto out_unlock;
1063         }
1064         /*
1065          * Wait for something to happen.  This should probably
1066          * be interruptible (FIXME).
1067          */
1068         wait_event_timeout(cam->iowait, cam->next_buf >= 0, HZ);
1069         if (cam->next_buf < 0) {
1070                 cam_err(cam, "read() operation timed out\n");
1071                 cafe_ctlr_stop_dma(cam);
1072                 ret = -EIO;
1073                 goto out_unlock;
1074         }
1075         /*
1076          * Give them their data and we should be done.
1077          */
1078         ret = cafe_deliver_buffer(cam, buffer, len, pos);
1079
1080   out_unlock:
1081         mutex_unlock(&cam->s_mutex);
1082         return ret;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092 /*
1093  * Streaming I/O support.
1094  */
1095
1096
1097
1098 static int cafe_vidioc_streamon(struct file *filp, void *priv,
1099                 enum v4l2_buf_type type)
1100 {
1101         struct cafe_camera *cam = filp->private_data;
1102         int ret = -EINVAL;
1103
1104         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1105                 goto out;
1106         mutex_lock(&cam->s_mutex);
1107         if (cam->state != S_IDLE || cam->n_sbufs == 0)
1108                 goto out_unlock;
1109
1110         cam->sequence = 0;
1111         ret = cafe_read_setup(cam, S_STREAMING);
1112
1113   out_unlock:
1114         mutex_unlock(&cam->s_mutex);
1115   out:
1116         return ret;
1117 }
1118
1119
1120 static int cafe_vidioc_streamoff(struct file *filp, void *priv,
1121                 enum v4l2_buf_type type)
1122 {
1123         struct cafe_camera *cam = filp->private_data;
1124         int ret = -EINVAL;
1125
1126         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1127                 goto out;
1128         mutex_lock(&cam->s_mutex);
1129         if (cam->state != S_STREAMING)
1130                 goto out_unlock;
1131
1132         cafe_ctlr_stop_dma(cam);
1133         ret = 0;
1134
1135   out_unlock:
1136         mutex_unlock(&cam->s_mutex);
1137   out:
1138         return ret;
1139 }
1140
1141
1142
1143 static int cafe_setup_siobuf(struct cafe_camera *cam, int index)
1144 {
1145         struct cafe_sio_buffer *buf = cam->sb_bufs + index;
1146
1147         INIT_LIST_HEAD(&buf->list);
1148         buf->v4lbuf.length = PAGE_ALIGN(cam->pix_format.sizeimage);
1149         buf->buffer = vmalloc_user(buf->v4lbuf.length);
1150         if (buf->buffer == NULL)
1151                 return -ENOMEM;
1152         buf->mapcount = 0;
1153         buf->cam = cam;
1154
1155         buf->v4lbuf.index = index;
1156         buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1157         buf->v4lbuf.field = V4L2_FIELD_NONE;
1158         buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
1159         /*
1160          * Offset: must be 32-bit even on a 64-bit system.  video-buf
1161          * just uses the length times the index, but the spec warns
1162          * against doing just that - vma merging problems.  So we
1163          * leave a gap between each pair of buffers.
1164          */
1165         buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
1166         return 0;
1167 }
1168
1169 static int cafe_free_sio_buffers(struct cafe_camera *cam)
1170 {
1171         int i;
1172
1173         /*
1174          * If any buffers are mapped, we cannot free them at all.
1175          */
1176         for (i = 0; i < cam->n_sbufs; i++)
1177                 if (cam->sb_bufs[i].mapcount > 0)
1178                         return -EBUSY;
1179         /*
1180          * OK, let's do it.
1181          */
1182         for (i = 0; i < cam->n_sbufs; i++)
1183                 vfree(cam->sb_bufs[i].buffer);
1184         cam->n_sbufs = 0;
1185         kfree(cam->sb_bufs);
1186         cam->sb_bufs = NULL;
1187         INIT_LIST_HEAD(&cam->sb_avail);
1188         INIT_LIST_HEAD(&cam->sb_full);
1189         return 0;
1190 }
1191
1192
1193
1194 static int cafe_vidioc_reqbufs(struct file *filp, void *priv,
1195                 struct v4l2_requestbuffers *req)
1196 {
1197         struct cafe_camera *cam = filp->private_data;
1198         int ret;
1199
1200         /*
1201          * Make sure it's something we can do.  User pointers could be
1202          * implemented without great pain, but that's not been done yet.
1203          */
1204         if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205                 return -EINVAL;
1206         if (req->memory != V4L2_MEMORY_MMAP)
1207                 return -EINVAL;
1208         /*
1209          * If they ask for zero buffers, they really want us to stop streaming
1210          * (if it's happening) and free everything.  Should we check owner?
1211          */
1212         mutex_lock(&cam->s_mutex);
1213         if (req->count == 0) {
1214                 if (cam->state == S_STREAMING)
1215                         cafe_ctlr_stop_dma(cam);
1216                 ret = cafe_free_sio_buffers (cam);
1217                 goto out;
1218         }
1219         /*
1220          * Device needs to be idle and working.  We *could* try to do the
1221          * right thing in S_SPECREAD by shutting things down, but it
1222          * probably doesn't matter.
1223          */
1224         if (cam->state != S_IDLE || (cam->owner && cam->owner != filp)) {
1225                 ret = -EBUSY;
1226                 goto out;
1227         }
1228         cam->owner = filp;
1229
1230         if (req->count < min_buffers)
1231                 req->count = min_buffers;
1232         else if (req->count > max_buffers)
1233                 req->count = max_buffers;
1234         if (cam->n_sbufs > 0) {
1235                 ret = cafe_free_sio_buffers(cam);
1236                 if (ret)
1237                         goto out;
1238         }
1239
1240         cam->sb_bufs = kzalloc(req->count*sizeof(struct cafe_sio_buffer),
1241                         GFP_KERNEL);
1242         if (cam->sb_bufs == NULL) {
1243                 ret = -ENOMEM;
1244                 goto out;
1245         }
1246         for (cam->n_sbufs = 0; cam->n_sbufs < req->count; (cam->n_sbufs++)) {
1247                 ret = cafe_setup_siobuf(cam, cam->n_sbufs);
1248                 if (ret)
1249                         break;
1250         }
1251
1252         if (cam->n_sbufs == 0)  /* no luck at all - ret already set */
1253                 kfree(cam->sb_bufs);
1254         else
1255                 ret = 0;
1256         req->count = cam->n_sbufs;  /* In case of partial success */
1257
1258   out:
1259         mutex_unlock(&cam->s_mutex);
1260         return ret;
1261 }
1262
1263
1264 static int cafe_vidioc_querybuf(struct file *filp, void *priv,
1265                 struct v4l2_buffer *buf)
1266 {
1267         struct cafe_camera *cam = filp->private_data;
1268         int ret = -EINVAL;
1269
1270         mutex_lock(&cam->s_mutex);
1271         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1272                 goto out;
1273         if (buf->index < 0 || buf->index >= cam->n_sbufs)
1274                 goto out;
1275         *buf = cam->sb_bufs[buf->index].v4lbuf;
1276         ret = 0;
1277   out:
1278         mutex_unlock(&cam->s_mutex);
1279         return ret;
1280 }
1281
1282 static int cafe_vidioc_qbuf(struct file *filp, void *priv,
1283                 struct v4l2_buffer *buf)
1284 {
1285         struct cafe_camera *cam = filp->private_data;
1286         struct cafe_sio_buffer *sbuf;
1287         int ret = -EINVAL;
1288         unsigned long flags;
1289
1290         mutex_lock(&cam->s_mutex);
1291         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1292                 goto out;
1293         if (buf->index < 0 || buf->index >= cam->n_sbufs)
1294                 goto out;
1295         sbuf = cam->sb_bufs + buf->index;
1296         if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED) {
1297                 ret = 0; /* Already queued?? */
1298                 goto out;
1299         }
1300         if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_DONE) {
1301                 /* Spec doesn't say anything, seems appropriate tho */
1302                 ret = -EBUSY;
1303                 goto out;
1304         }
1305         sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1306         spin_lock_irqsave(&cam->dev_lock, flags);
1307         list_add(&sbuf->list, &cam->sb_avail);
1308         spin_unlock_irqrestore(&cam->dev_lock, flags);
1309         ret = 0;
1310   out:
1311         mutex_unlock(&cam->s_mutex);
1312         return ret;
1313 }
1314
1315 static int cafe_vidioc_dqbuf(struct file *filp, void *priv,
1316                 struct v4l2_buffer *buf)
1317 {
1318         struct cafe_camera *cam = filp->private_data;
1319         struct cafe_sio_buffer *sbuf;
1320         int ret = -EINVAL;
1321         unsigned long flags;
1322
1323         mutex_lock(&cam->s_mutex);
1324         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1325                 goto out_unlock;
1326         if (cam->state != S_STREAMING)
1327                 goto out_unlock;
1328         if (list_empty(&cam->sb_full) && filp->f_flags & O_NONBLOCK) {
1329                 ret = -EAGAIN;
1330                 goto out_unlock;
1331         }
1332
1333         while (list_empty(&cam->sb_full) && cam->state == S_STREAMING) {
1334                 mutex_unlock(&cam->s_mutex);
1335                 if (wait_event_interruptible(cam->iowait,
1336                                                 !list_empty(&cam->sb_full))) {
1337                         ret = -ERESTARTSYS;
1338                         goto out;
1339                 }
1340                 mutex_lock(&cam->s_mutex);
1341         }
1342
1343         if (cam->state != S_STREAMING)
1344                 ret = -EINTR;
1345         else {
1346                 spin_lock_irqsave(&cam->dev_lock, flags);
1347                 /* Should probably recheck !list_empty() here */
1348                 sbuf = list_entry(cam->sb_full.next,
1349                                 struct cafe_sio_buffer, list);
1350                 list_del_init(&sbuf->list);
1351                 spin_unlock_irqrestore(&cam->dev_lock, flags);
1352                 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1353                 *buf = sbuf->v4lbuf;
1354                 ret = 0;
1355         }
1356
1357   out_unlock:
1358         mutex_unlock(&cam->s_mutex);
1359   out:
1360         return ret;
1361 }
1362
1363
1364
1365 static void cafe_v4l_vm_open(struct vm_area_struct *vma)
1366 {
1367         struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1368         /*
1369          * Locking: done under mmap_sem, so we don't need to
1370          * go back to the camera lock here.
1371          */
1372         sbuf->mapcount++;
1373 }
1374
1375
1376 static void cafe_v4l_vm_close(struct vm_area_struct *vma)
1377 {
1378         struct cafe_sio_buffer *sbuf = vma->vm_private_data;
1379
1380         mutex_lock(&sbuf->cam->s_mutex);
1381         sbuf->mapcount--;
1382         /* Docs say we should stop I/O too... */
1383         if (sbuf->mapcount == 0)
1384                 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
1385         mutex_unlock(&sbuf->cam->s_mutex);
1386 }
1387
1388 static struct vm_operations_struct cafe_v4l_vm_ops = {
1389         .open = cafe_v4l_vm_open,
1390         .close = cafe_v4l_vm_close
1391 };
1392
1393
1394 static int cafe_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
1395 {
1396         struct cafe_camera *cam = filp->private_data;
1397         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1398         int ret = -EINVAL;
1399         int i;
1400         struct cafe_sio_buffer *sbuf = NULL;
1401
1402         if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
1403                 return -EINVAL;
1404         /*
1405          * Find the buffer they are looking for.
1406          */
1407         mutex_lock(&cam->s_mutex);
1408         for (i = 0; i < cam->n_sbufs; i++)
1409                 if (cam->sb_bufs[i].v4lbuf.m.offset == offset) {
1410                         sbuf = cam->sb_bufs + i;
1411                         break;
1412                 }
1413         if (sbuf == NULL)
1414                 goto out;
1415
1416         ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
1417         if (ret)
1418                 goto out;
1419         vma->vm_flags |= VM_DONTEXPAND;
1420         vma->vm_private_data = sbuf;
1421         vma->vm_ops = &cafe_v4l_vm_ops;
1422         sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
1423         cafe_v4l_vm_open(vma);
1424         ret = 0;
1425   out:
1426         mutex_unlock(&cam->s_mutex);
1427         return ret;
1428 }
1429
1430
1431
1432 static int cafe_v4l_open(struct inode *inode, struct file *filp)
1433 {
1434         struct cafe_camera *cam;
1435
1436         cam = cafe_find_dev(iminor(inode));
1437         if (cam == NULL)
1438                 return -ENODEV;
1439         filp->private_data = cam;
1440
1441         mutex_lock(&cam->s_mutex);
1442         if (cam->users == 0) {
1443                 cafe_ctlr_power_up(cam);
1444                 __cafe_cam_reset(cam);
1445                 cafe_set_config_needed(cam, 1);
1446         /* FIXME make sure this is complete */
1447         }
1448         (cam->users)++;
1449         mutex_unlock(&cam->s_mutex);
1450         return 0;
1451 }
1452
1453
1454 static int cafe_v4l_release(struct inode *inode, struct file *filp)
1455 {
1456         struct cafe_camera *cam = filp->private_data;
1457
1458         mutex_lock(&cam->s_mutex);
1459         (cam->users)--;
1460         if (filp == cam->owner) {
1461                 cafe_ctlr_stop_dma(cam);
1462                 cafe_free_sio_buffers(cam);
1463                 cam->owner = NULL;
1464         }
1465         if (cam->users == 0) {
1466                 cafe_ctlr_power_down(cam);
1467                 if (! alloc_bufs_at_load)
1468                         cafe_free_dma_bufs(cam);
1469         }
1470         mutex_unlock(&cam->s_mutex);
1471         return 0;
1472 }
1473
1474
1475
1476 static unsigned int cafe_v4l_poll(struct file *filp,
1477                 struct poll_table_struct *pt)
1478 {
1479         struct cafe_camera *cam = filp->private_data;
1480
1481         poll_wait(filp, &cam->iowait, pt);
1482         if (cam->next_buf >= 0)
1483                 return POLLIN | POLLRDNORM;
1484         return 0;
1485 }
1486
1487
1488
1489 static int cafe_vidioc_queryctrl(struct file *filp, void *priv,
1490                 struct v4l2_queryctrl *qc)
1491 {
1492         struct cafe_camera *cam = filp->private_data;
1493         int ret;
1494
1495         mutex_lock(&cam->s_mutex);
1496         ret = __cafe_cam_cmd(cam, VIDIOC_QUERYCTRL, qc);
1497         mutex_unlock(&cam->s_mutex);
1498         return ret;
1499 }
1500
1501
1502 static int cafe_vidioc_g_ctrl(struct file *filp, void *priv,
1503                 struct v4l2_control *ctrl)
1504 {
1505         struct cafe_camera *cam = filp->private_data;
1506         int ret;
1507
1508         mutex_lock(&cam->s_mutex);
1509         ret = __cafe_cam_cmd(cam, VIDIOC_G_CTRL, ctrl);
1510         mutex_unlock(&cam->s_mutex);
1511         return ret;
1512 }
1513
1514
1515 static int cafe_vidioc_s_ctrl(struct file *filp, void *priv,
1516                 struct v4l2_control *ctrl)
1517 {
1518         struct cafe_camera *cam = filp->private_data;
1519         int ret;
1520
1521         mutex_lock(&cam->s_mutex);
1522         ret = __cafe_cam_cmd(cam, VIDIOC_S_CTRL, ctrl);
1523         mutex_unlock(&cam->s_mutex);
1524         return ret;
1525 }
1526
1527
1528
1529
1530
1531 static int cafe_vidioc_querycap(struct file *file, void *priv,
1532                 struct v4l2_capability *cap)
1533 {
1534         strcpy(cap->driver, "cafe_ccic");
1535         strcpy(cap->card, "cafe_ccic");
1536         cap->version = CAFE_VERSION;
1537         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1538                 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1539         return 0;
1540 }
1541
1542
1543 /*
1544  * The default format we use until somebody says otherwise.
1545  */
1546 static struct v4l2_pix_format cafe_def_pix_format = {
1547         .width          = VGA_WIDTH,
1548         .height         = VGA_HEIGHT,
1549         .pixelformat    = V4L2_PIX_FMT_YUYV,
1550         .field          = V4L2_FIELD_NONE,
1551         .bytesperline   = VGA_WIDTH*2,
1552         .sizeimage      = VGA_WIDTH*VGA_HEIGHT*2,
1553 };
1554
1555 static int cafe_vidioc_enum_fmt_cap(struct file *filp,
1556                 void *priv, struct v4l2_fmtdesc *fmt)
1557 {
1558         struct cafe_camera *cam = priv;
1559         int ret;
1560
1561         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1562                 return -EINVAL;
1563         mutex_lock(&cam->s_mutex);
1564         ret = __cafe_cam_cmd(cam, VIDIOC_ENUM_FMT, fmt);
1565         mutex_unlock(&cam->s_mutex);
1566         return ret;
1567 }
1568
1569
1570 static int cafe_vidioc_try_fmt_cap (struct file *filp, void *priv,
1571                 struct v4l2_format *fmt)
1572 {
1573         struct cafe_camera *cam = priv;
1574         int ret;
1575
1576         mutex_lock(&cam->s_mutex);
1577         ret = __cafe_cam_cmd(cam, VIDIOC_TRY_FMT, fmt);
1578         mutex_unlock(&cam->s_mutex);
1579         return ret;
1580 }
1581
1582 static int cafe_vidioc_s_fmt_cap(struct file *filp, void *priv,
1583                 struct v4l2_format *fmt)
1584 {
1585         struct cafe_camera *cam = priv;
1586         int ret;
1587
1588         /*
1589          * Can't do anything if the device is not idle
1590          * Also can't if there are streaming buffers in place.
1591          */
1592         if (cam->state != S_IDLE || cam->n_sbufs > 0)
1593                 return -EBUSY;
1594         /*
1595          * See if the formatting works in principle.
1596          */
1597         ret = cafe_vidioc_try_fmt_cap(filp, priv, fmt);
1598         if (ret)
1599                 return ret;
1600         /*
1601          * Now we start to change things for real, so let's do it
1602          * under lock.
1603          */
1604         mutex_lock(&cam->s_mutex);
1605         cam->pix_format = fmt->fmt.pix;
1606         /*
1607          * Make sure we have appropriate DMA buffers.
1608          */
1609         ret = -ENOMEM;
1610         if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
1611                 cafe_free_dma_bufs(cam);
1612         if (cam->nbufs == 0) {
1613                 if (cafe_alloc_dma_bufs(cam, 0))
1614                         goto out;
1615         }
1616         /*
1617          * It looks like this might work, so let's program the sensor.
1618          */
1619         ret = cafe_cam_configure(cam);
1620         if (! ret)
1621                 ret = cafe_ctlr_configure(cam);
1622   out:
1623         mutex_unlock(&cam->s_mutex);
1624         return ret;
1625 }
1626
1627 /*
1628  * Return our stored notion of how the camera is/should be configured.
1629  * The V4l2 spec wants us to be smarter, and actually get this from
1630  * the camera (and not mess with it at open time).  Someday.
1631  */
1632 static int cafe_vidioc_g_fmt_cap(struct file *filp, void *priv,
1633                 struct v4l2_format *f)
1634 {
1635         struct cafe_camera *cam = priv;
1636
1637         f->fmt.pix = cam->pix_format;
1638         return 0;
1639 }
1640
1641 /*
1642  * We only have one input - the sensor - so minimize the nonsense here.
1643  */
1644 static int cafe_vidioc_enum_input(struct file *filp, void *priv,
1645                 struct v4l2_input *input)
1646 {
1647         if (input->index != 0)
1648                 return -EINVAL;
1649
1650         input->type = V4L2_INPUT_TYPE_CAMERA;
1651         input->std = V4L2_STD_ALL; /* Not sure what should go here */
1652         strcpy(input->name, "Camera");
1653         return 0;
1654 }
1655
1656 static int cafe_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1657 {
1658         *i = 0;
1659         return 0;
1660 }
1661
1662 static int cafe_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1663 {
1664         if (i != 0)
1665                 return -EINVAL;
1666         return 0;
1667 }
1668
1669 /* from vivi.c */
1670 static int cafe_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
1671 {
1672         return 0;
1673 }
1674
1675 /*
1676  * G/S_PARM.  Most of this is done by the sensor, but we are
1677  * the level which controls the number of read buffers.
1678  */
1679 static int cafe_vidioc_g_parm(struct file *filp, void *priv,
1680                 struct v4l2_streamparm *parms)
1681 {
1682         struct cafe_camera *cam = priv;
1683         int ret;
1684
1685         mutex_lock(&cam->s_mutex);
1686         ret = __cafe_cam_cmd(cam, VIDIOC_G_PARM, parms);
1687         mutex_unlock(&cam->s_mutex);
1688         parms->parm.capture.readbuffers = n_dma_bufs;
1689         return ret;
1690 }
1691
1692 static int cafe_vidioc_s_parm(struct file *filp, void *priv,
1693                 struct v4l2_streamparm *parms)
1694 {
1695         struct cafe_camera *cam = priv;
1696         int ret;
1697
1698         mutex_lock(&cam->s_mutex);
1699         ret = __cafe_cam_cmd(cam, VIDIOC_S_PARM, parms);
1700         mutex_unlock(&cam->s_mutex);
1701         parms->parm.capture.readbuffers = n_dma_bufs;
1702         return ret;
1703 }
1704
1705
1706 static void cafe_v4l_dev_release(struct video_device *vd)
1707 {
1708         struct cafe_camera *cam = container_of(vd, struct cafe_camera, v4ldev);
1709
1710         kfree(cam);
1711 }
1712
1713
1714 /*
1715  * This template device holds all of those v4l2 methods; we
1716  * clone it for specific real devices.
1717  */
1718
1719 static const struct file_operations cafe_v4l_fops = {
1720         .owner = THIS_MODULE,
1721         .open = cafe_v4l_open,
1722         .release = cafe_v4l_release,
1723         .read = cafe_v4l_read,
1724         .poll = cafe_v4l_poll,
1725         .mmap = cafe_v4l_mmap,
1726         .ioctl = video_ioctl2,
1727         .llseek = no_llseek,
1728 };
1729
1730 static struct video_device cafe_v4l_template = {
1731         .name = "cafe",
1732         .type = VFL_TYPE_GRABBER,
1733         .type2 = VID_TYPE_CAPTURE,
1734         .minor = -1, /* Get one dynamically */
1735         .tvnorms = V4L2_STD_NTSC_M,
1736         .current_norm = V4L2_STD_NTSC_M,  /* make mplayer happy */
1737
1738         .fops = &cafe_v4l_fops,
1739         .release = cafe_v4l_dev_release,
1740
1741         .vidioc_querycap        = cafe_vidioc_querycap,
1742         .vidioc_enum_fmt_cap    = cafe_vidioc_enum_fmt_cap,
1743         .vidioc_try_fmt_cap     = cafe_vidioc_try_fmt_cap,
1744         .vidioc_s_fmt_cap       = cafe_vidioc_s_fmt_cap,
1745         .vidioc_g_fmt_cap       = cafe_vidioc_g_fmt_cap,
1746         .vidioc_enum_input      = cafe_vidioc_enum_input,
1747         .vidioc_g_input         = cafe_vidioc_g_input,
1748         .vidioc_s_input         = cafe_vidioc_s_input,
1749         .vidioc_s_std           = cafe_vidioc_s_std,
1750         .vidioc_reqbufs         = cafe_vidioc_reqbufs,
1751         .vidioc_querybuf        = cafe_vidioc_querybuf,
1752         .vidioc_qbuf            = cafe_vidioc_qbuf,
1753         .vidioc_dqbuf           = cafe_vidioc_dqbuf,
1754         .vidioc_streamon        = cafe_vidioc_streamon,
1755         .vidioc_streamoff       = cafe_vidioc_streamoff,
1756         .vidioc_queryctrl       = cafe_vidioc_queryctrl,
1757         .vidioc_g_ctrl          = cafe_vidioc_g_ctrl,
1758         .vidioc_s_ctrl          = cafe_vidioc_s_ctrl,
1759         .vidioc_g_parm          = cafe_vidioc_g_parm,
1760         .vidioc_s_parm          = cafe_vidioc_s_parm,
1761 };
1762
1763
1764
1765
1766
1767
1768
1769 /* ---------------------------------------------------------------------- */
1770 /*
1771  * Interrupt handler stuff
1772  */
1773
1774
1775
1776 static void cafe_frame_tasklet(unsigned long data)
1777 {
1778         struct cafe_camera *cam = (struct cafe_camera *) data;
1779         int i;
1780         unsigned long flags;
1781         struct cafe_sio_buffer *sbuf;
1782
1783         spin_lock_irqsave(&cam->dev_lock, flags);
1784         for (i = 0; i < cam->nbufs; i++) {
1785                 int bufno = cam->next_buf;
1786                 if (bufno < 0) {  /* "will never happen" */
1787                         cam_err(cam, "No valid bufs in tasklet!\n");
1788                         break;
1789                 }
1790                 if (++(cam->next_buf) >= cam->nbufs)
1791                         cam->next_buf = 0;
1792                 if (! test_bit(bufno, &cam->flags))
1793                         continue;
1794                 if (list_empty(&cam->sb_avail))
1795                         break;  /* Leave it valid, hope for better later */
1796                 clear_bit(bufno, &cam->flags);
1797                 /*
1798                  * We could perhaps drop the spinlock during this
1799                  * big copy.  Something to consider.
1800                  */
1801                 sbuf = list_entry(cam->sb_avail.next,
1802                                 struct cafe_sio_buffer, list);
1803                 memcpy(sbuf->buffer, cam->dma_bufs[bufno],
1804                                 cam->pix_format.sizeimage);
1805                 sbuf->v4lbuf.bytesused = cam->pix_format.sizeimage;
1806                 sbuf->v4lbuf.sequence = cam->buf_seq[bufno];
1807                 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1808                 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1809                 list_move_tail(&sbuf->list, &cam->sb_full);
1810         }
1811         if (! list_empty(&cam->sb_full))
1812                 wake_up(&cam->iowait);
1813         spin_unlock_irqrestore(&cam->dev_lock, flags);
1814 }
1815
1816
1817
1818 static void cafe_frame_complete(struct cafe_camera *cam, int frame)
1819 {
1820         /*
1821          * Basic frame housekeeping.
1822          */
1823         if (test_bit(frame, &cam->flags) && printk_ratelimit())
1824                 cam_err(cam, "Frame overrun on %d, frames lost\n", frame);
1825         set_bit(frame, &cam->flags);
1826         clear_bit(CF_DMA_ACTIVE, &cam->flags);
1827         if (cam->next_buf < 0)
1828                 cam->next_buf = frame;
1829         cam->buf_seq[frame] = ++(cam->sequence);
1830
1831         switch (cam->state) {
1832         /*
1833          * If in single read mode, try going speculative.
1834          */
1835             case S_SINGLEREAD:
1836                 cam->state = S_SPECREAD;
1837                 cam->specframes = 0;
1838                 wake_up(&cam->iowait);
1839                 break;
1840
1841         /*
1842          * If we are already doing speculative reads, and nobody is
1843          * reading them, just stop.
1844          */
1845             case S_SPECREAD:
1846                 if (++(cam->specframes) >= cam->nbufs) {
1847                         cafe_ctlr_stop(cam);
1848                         cafe_ctlr_irq_disable(cam);
1849                         cam->state = S_IDLE;
1850                 }
1851                 wake_up(&cam->iowait);
1852                 break;
1853         /*
1854          * For the streaming case, we defer the real work to the
1855          * camera tasklet.
1856          *
1857          * FIXME: if the application is not consuming the buffers,
1858          * we should eventually put things on hold and restart in
1859          * vidioc_dqbuf().
1860          */
1861             case S_STREAMING:
1862                 tasklet_schedule(&cam->s_tasklet);
1863                 break;
1864
1865             default:
1866                 cam_err(cam, "Frame interrupt in non-operational state\n");
1867                 break;
1868         }
1869 }
1870
1871
1872
1873
1874 static void cafe_frame_irq(struct cafe_camera *cam, unsigned int irqs)
1875 {
1876         unsigned int frame;
1877
1878         cafe_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1879         /*
1880          * Handle any frame completions.  There really should
1881          * not be more than one of these, or we have fallen
1882          * far behind.
1883          */
1884         for (frame = 0; frame < cam->nbufs; frame++)
1885                 if (irqs & (IRQ_EOF0 << frame))
1886                         cafe_frame_complete(cam, frame);
1887         /*
1888          * If a frame starts, note that we have DMA active.  This
1889          * code assumes that we won't get multiple frame interrupts
1890          * at once; may want to rethink that.
1891          */
1892         if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2))
1893                 set_bit(CF_DMA_ACTIVE, &cam->flags);
1894 }
1895
1896
1897
1898 static irqreturn_t cafe_irq(int irq, void *data)
1899 {
1900         struct cafe_camera *cam = data;
1901         unsigned int irqs;
1902
1903         spin_lock(&cam->dev_lock);
1904         irqs = cafe_reg_read(cam, REG_IRQSTAT);
1905         if ((irqs & ALLIRQS) == 0) {
1906                 spin_unlock(&cam->dev_lock);
1907                 return IRQ_NONE;
1908         }
1909         if (irqs & FRAMEIRQS)
1910                 cafe_frame_irq(cam, irqs);
1911         if (irqs & TWSIIRQS) {
1912                 cafe_reg_write(cam, REG_IRQSTAT, TWSIIRQS);
1913                 wake_up(&cam->smbus_wait);
1914         }
1915         spin_unlock(&cam->dev_lock);
1916         return IRQ_HANDLED;
1917 }
1918
1919
1920 /* -------------------------------------------------------------------------- */
1921 #ifdef CONFIG_VIDEO_ADV_DEBUG
1922 /*
1923  * Debugfs stuff.
1924  */
1925
1926 static char cafe_debug_buf[1024];
1927 static struct dentry *cafe_dfs_root;
1928
1929 static void cafe_dfs_setup(void)
1930 {
1931         cafe_dfs_root = debugfs_create_dir("cafe_ccic", NULL);
1932         if (IS_ERR(cafe_dfs_root)) {
1933                 cafe_dfs_root = NULL;  /* Never mind */
1934                 printk(KERN_NOTICE "cafe_ccic unable to set up debugfs\n");
1935         }
1936 }
1937
1938 static void cafe_dfs_shutdown(void)
1939 {
1940         if (cafe_dfs_root)
1941                 debugfs_remove(cafe_dfs_root);
1942 }
1943
1944 static int cafe_dfs_open(struct inode *inode, struct file *file)
1945 {
1946         file->private_data = inode->i_private;
1947         return 0;
1948 }
1949
1950 static ssize_t cafe_dfs_read_regs(struct file *file,
1951                 char __user *buf, size_t count, loff_t *ppos)
1952 {
1953         struct cafe_camera *cam = file->private_data;
1954         char *s = cafe_debug_buf;
1955         int offset;
1956
1957         for (offset = 0; offset < 0x44; offset += 4)
1958                 s += sprintf(s, "%02x: %08x\n", offset,
1959                                 cafe_reg_read(cam, offset));
1960         for (offset = 0x88; offset <= 0x90; offset += 4)
1961                 s += sprintf(s, "%02x: %08x\n", offset,
1962                                 cafe_reg_read(cam, offset));
1963         for (offset = 0xb4; offset <= 0xbc; offset += 4)
1964                 s += sprintf(s, "%02x: %08x\n", offset,
1965                                 cafe_reg_read(cam, offset));
1966         for (offset = 0x3000; offset <= 0x300c; offset += 4)
1967                 s += sprintf(s, "%04x: %08x\n", offset,
1968                                 cafe_reg_read(cam, offset));
1969         return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1970                         s - cafe_debug_buf);
1971 }
1972
1973 static const struct file_operations cafe_dfs_reg_ops = {
1974         .owner = THIS_MODULE,
1975         .read = cafe_dfs_read_regs,
1976         .open = cafe_dfs_open
1977 };
1978
1979 static ssize_t cafe_dfs_read_cam(struct file *file,
1980                 char __user *buf, size_t count, loff_t *ppos)
1981 {
1982         struct cafe_camera *cam = file->private_data;
1983         char *s = cafe_debug_buf;
1984         int offset;
1985
1986         if (! cam->sensor)
1987                 return -EINVAL;
1988         for (offset = 0x0; offset < 0x8a; offset++)
1989         {
1990                 u8 v;
1991
1992                 cafe_smbus_read_data(cam, cam->sensor->addr, offset, &v);
1993                 s += sprintf(s, "%02x: %02x\n", offset, v);
1994         }
1995         return simple_read_from_buffer(buf, count, ppos, cafe_debug_buf,
1996                         s - cafe_debug_buf);
1997 }
1998
1999 static const struct file_operations cafe_dfs_cam_ops = {
2000         .owner = THIS_MODULE,
2001         .read = cafe_dfs_read_cam,
2002         .open = cafe_dfs_open
2003 };
2004
2005
2006
2007 static void cafe_dfs_cam_setup(struct cafe_camera *cam)
2008 {
2009         char fname[40];
2010
2011         if (!cafe_dfs_root)
2012                 return;
2013         sprintf(fname, "regs-%d", cam->v4ldev.minor);
2014         cam->dfs_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2015                         cam, &cafe_dfs_reg_ops);
2016         sprintf(fname, "cam-%d", cam->v4ldev.minor);
2017         cam->dfs_cam_regs = debugfs_create_file(fname, 0444, cafe_dfs_root,
2018                         cam, &cafe_dfs_cam_ops);
2019 }
2020
2021
2022 static void cafe_dfs_cam_shutdown(struct cafe_camera *cam)
2023 {
2024         if (! IS_ERR(cam->dfs_regs))
2025                 debugfs_remove(cam->dfs_regs);
2026         if (! IS_ERR(cam->dfs_cam_regs))
2027                 debugfs_remove(cam->dfs_cam_regs);
2028 }
2029
2030 #else
2031
2032 #define cafe_dfs_setup()
2033 #define cafe_dfs_shutdown()
2034 #define cafe_dfs_cam_setup(cam)
2035 #define cafe_dfs_cam_shutdown(cam)
2036 #endif    /* CONFIG_VIDEO_ADV_DEBUG */
2037
2038
2039
2040
2041 /* ------------------------------------------------------------------------*/
2042 /*
2043  * PCI interface stuff.
2044  */
2045
2046 static int cafe_pci_probe(struct pci_dev *pdev,
2047                 const struct pci_device_id *id)
2048 {
2049         int ret;
2050         u16 classword;
2051         struct cafe_camera *cam;
2052         /*
2053          * Make sure we have a camera here - we'll get calls for
2054          * the other cafe devices as well.
2055          */
2056         pci_read_config_word(pdev, PCI_CLASS_DEVICE, &classword);
2057         if (classword != PCI_CLASS_MULTIMEDIA_VIDEO)
2058                 return -ENODEV;
2059         /*
2060          * Start putting together one of our big camera structures.
2061          */
2062         ret = -ENOMEM;
2063         cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
2064         if (cam == NULL)
2065                 goto out;
2066         mutex_init(&cam->s_mutex);
2067         mutex_lock(&cam->s_mutex);
2068         spin_lock_init(&cam->dev_lock);
2069         cam->state = S_NOTREADY;
2070         cafe_set_config_needed(cam, 1);
2071         init_waitqueue_head(&cam->smbus_wait);
2072         init_waitqueue_head(&cam->iowait);
2073         cam->pdev = pdev;
2074         cam->pix_format = cafe_def_pix_format;
2075         INIT_LIST_HEAD(&cam->dev_list);
2076         INIT_LIST_HEAD(&cam->sb_avail);
2077         INIT_LIST_HEAD(&cam->sb_full);
2078         tasklet_init(&cam->s_tasklet, cafe_frame_tasklet, (unsigned long) cam);
2079         /*
2080          * Get set up on the PCI bus.
2081          */
2082         ret = pci_enable_device(pdev);
2083         if (ret)
2084                 goto out_free;
2085         pci_set_master(pdev);
2086
2087         ret = -EIO;
2088         cam->regs = pci_iomap(pdev, 0, 0);
2089         if (! cam->regs) {
2090                 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
2091                 goto out_free;
2092         }
2093         ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
2094         if (ret)
2095                 goto out_iounmap;
2096         cafe_ctlr_init(cam);
2097         cafe_ctlr_power_up(cam);
2098         /*
2099          * Set up I2C/SMBUS communications
2100          */
2101         mutex_unlock(&cam->s_mutex);  /* attach can deadlock */
2102         ret = cafe_smbus_setup(cam);
2103         if (ret)
2104                 goto out_freeirq;
2105         /*
2106          * Get the v4l2 setup done.
2107          */
2108         mutex_lock(&cam->s_mutex);
2109         cam->v4ldev = cafe_v4l_template;
2110         cam->v4ldev.debug = 0;
2111 //      cam->v4ldev.debug = V4L2_DEBUG_IOCTL_ARG;
2112         ret = video_register_device(&cam->v4ldev, VFL_TYPE_GRABBER, -1);
2113         if (ret)
2114                 goto out_smbus;
2115         /*
2116          * If so requested, try to get our DMA buffers now.
2117          */
2118         if (alloc_bufs_at_load) {
2119                 if (cafe_alloc_dma_bufs(cam, 1))
2120                         cam_warn(cam, "Unable to alloc DMA buffers at load"
2121                                         " will try again later.");
2122         }
2123
2124         cafe_dfs_cam_setup(cam);
2125         mutex_unlock(&cam->s_mutex);
2126         cafe_add_dev(cam);
2127         return 0;
2128
2129   out_smbus:
2130         cafe_smbus_shutdown(cam);
2131   out_freeirq:
2132         cafe_ctlr_power_down(cam);
2133         free_irq(pdev->irq, cam);
2134   out_iounmap:
2135         pci_iounmap(pdev, cam->regs);
2136   out_free:
2137         kfree(cam);
2138   out:
2139         return ret;
2140 }
2141
2142
2143 /*
2144  * Shut down an initialized device
2145  */
2146 static void cafe_shutdown(struct cafe_camera *cam)
2147 {
2148 /* FIXME: Make sure we take care of everything here */
2149         cafe_dfs_cam_shutdown(cam);
2150         if (cam->n_sbufs > 0)
2151                 /* What if they are still mapped?  Shouldn't be, but... */
2152                 cafe_free_sio_buffers(cam);
2153         cafe_remove_dev(cam);
2154         cafe_ctlr_stop_dma(cam);
2155         cafe_ctlr_power_down(cam);
2156         cafe_smbus_shutdown(cam);
2157         cafe_free_dma_bufs(cam);
2158         free_irq(cam->pdev->irq, cam);
2159         pci_iounmap(cam->pdev, cam->regs);
2160         video_unregister_device(&cam->v4ldev);
2161         /* kfree(cam); done in v4l_release () */
2162 }
2163
2164
2165 static void cafe_pci_remove(struct pci_dev *pdev)
2166 {
2167         struct cafe_camera *cam = cafe_find_by_pdev(pdev);
2168
2169         if (cam == NULL) {
2170                 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
2171                 return;
2172         }
2173         mutex_lock(&cam->s_mutex);
2174         if (cam->users > 0)
2175                 cam_warn(cam, "Removing a device with users!\n");
2176         cafe_shutdown(cam);
2177 /* No unlock - it no longer exists */
2178 }
2179
2180
2181
2182
2183 static struct pci_device_id cafe_ids[] = {
2184         { PCI_DEVICE(0x1148, 0x4340) }, /* Temporary ID on devel board */
2185         { PCI_DEVICE(0x11ab, 0x4100) }, /* Eventual real ID */
2186         { PCI_DEVICE(0x11ab, 0x4102) }, /* Really eventual real ID */
2187         { 0, }
2188 };
2189
2190 MODULE_DEVICE_TABLE(pci, cafe_ids);
2191
2192 static struct pci_driver cafe_pci_driver = {
2193         .name = "cafe1000-ccic",
2194         .id_table = cafe_ids,
2195         .probe = cafe_pci_probe,
2196         .remove = cafe_pci_remove,
2197 };
2198
2199
2200
2201
2202 static int __init cafe_init(void)
2203 {
2204         int ret;
2205
2206         printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
2207                         CAFE_VERSION);
2208         cafe_dfs_setup();
2209         ret = pci_register_driver(&cafe_pci_driver);
2210         if (ret) {
2211                 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
2212                 goto out;
2213         }
2214         request_module("ov7670");  /* FIXME want something more general */
2215         ret = 0;
2216
2217   out:
2218         return ret;
2219 }
2220
2221
2222 static void __exit cafe_exit(void)
2223 {
2224         pci_unregister_driver(&cafe_pci_driver);
2225         cafe_dfs_shutdown();
2226 }
2227
2228 module_init(cafe_init);
2229 module_exit(cafe_exit);