media: delete unused proc_fs.h include
[sfrench/cifs-2.6.git] / drivers / media / usb / zr364xx / zr364xx.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Zoran 364xx based USB webcam module version 0.73
4  *
5  * Allows you to use your USB webcam with V4L2 applications
6  * This is still in heavy development !
7  *
8  * Copyright (C) 2004  Antoine Jacquet <royale@zerezo.com>
9  * http://royale.zerezo.com/zr364xx/
10  *
11  * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
12  * V4L2 version inspired by meye.c driver
13  *
14  * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
15  */
16
17
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/usb.h>
21 #include <linux/vmalloc.h>
22 #include <linux/slab.h>
23 #include <linux/highmem.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/videobuf-vmalloc.h>
31
32
33 /* Version Information */
34 #define DRIVER_VERSION "0.7.4"
35 #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
36 #define DRIVER_DESC "Zoran 364xx"
37
38
39 /* Camera */
40 #define FRAMES 1
41 #define MAX_FRAME_SIZE 200000
42 #define BUFFER_SIZE 0x1000
43 #define CTRL_TIMEOUT 500
44
45 #define ZR364XX_DEF_BUFS        4
46 #define ZR364XX_READ_IDLE       0
47 #define ZR364XX_READ_FRAME      1
48
49 /* Debug macro */
50 #define DBG(fmt, args...) \
51         do { \
52                 if (debug) { \
53                         printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
54                 } \
55         } while (0)
56
57 /*#define FULL_DEBUG 1*/
58 #ifdef FULL_DEBUG
59 #define _DBG DBG
60 #else
61 #define _DBG(fmt, args...)
62 #endif
63
64 /* Init methods, need to find nicer names for these
65  * the exact names of the chipsets would be the best if someone finds it */
66 #define METHOD0 0
67 #define METHOD1 1
68 #define METHOD2 2
69 #define METHOD3 3
70
71
72 /* Module parameters */
73 static int debug;
74 static int mode;
75
76
77 /* Module parameters interface */
78 module_param(debug, int, 0644);
79 MODULE_PARM_DESC(debug, "Debug level");
80 module_param(mode, int, 0644);
81 MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
82
83
84 /* Devices supported by this driver
85  * .driver_info contains the init method used by the camera */
86 static const struct usb_device_id device_table[] = {
87         {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
88         {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
89         {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
90         {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
91         {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
92         {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
93         {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
94         {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
95         {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
96         {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
97         {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
98         {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
99         {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
100         {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
101         {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
102         {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
103         {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
104         {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
105         {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
106         {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD3 },
107         {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
108         {}                      /* Terminating entry */
109 };
110
111 MODULE_DEVICE_TABLE(usb, device_table);
112
113 /* frame structure */
114 struct zr364xx_framei {
115         unsigned long ulState;  /* ulState:ZR364XX_READ_IDLE,
116                                            ZR364XX_READ_FRAME */
117         void *lpvbits;          /* image data */
118         unsigned long cur_size; /* current data copied to it */
119 };
120
121 /* image buffer structure */
122 struct zr364xx_bufferi {
123         unsigned long dwFrames;                 /* number of frames in buffer */
124         struct zr364xx_framei frame[FRAMES];    /* array of FRAME structures */
125 };
126
127 struct zr364xx_dmaqueue {
128         struct list_head        active;
129         struct zr364xx_camera   *cam;
130 };
131
132 struct zr364xx_pipeinfo {
133         u32 transfer_size;
134         u8 *transfer_buffer;
135         u32 state;
136         void *stream_urb;
137         void *cam;      /* back pointer to zr364xx_camera struct */
138         u32 err_count;
139         u32 idx;
140 };
141
142 struct zr364xx_fmt {
143         u32 fourcc;
144         int depth;
145 };
146
147 /* image formats.  */
148 static const struct zr364xx_fmt formats[] = {
149         {
150                 .fourcc = V4L2_PIX_FMT_JPEG,
151                 .depth = 24
152         }
153 };
154
155 /* Camera stuff */
156 struct zr364xx_camera {
157         struct usb_device *udev;        /* save off the usb device pointer */
158         struct usb_interface *interface;/* the interface for this device */
159         struct v4l2_device v4l2_dev;
160         struct v4l2_ctrl_handler ctrl_handler;
161         struct video_device vdev;       /* v4l video device */
162         struct v4l2_fh *owner;          /* owns the streaming */
163         int nb;
164         struct zr364xx_bufferi          buffer;
165         int skip;
166         int width;
167         int height;
168         int method;
169         struct mutex lock;
170
171         spinlock_t              slock;
172         struct zr364xx_dmaqueue vidq;
173         int                     last_frame;
174         int                     cur_frame;
175         unsigned long           frame_count;
176         int                     b_acquire;
177         struct zr364xx_pipeinfo pipe[1];
178
179         u8                      read_endpoint;
180
181         const struct zr364xx_fmt *fmt;
182         struct videobuf_queue   vb_vidq;
183         bool was_streaming;
184 };
185
186 /* buffer for one video frame */
187 struct zr364xx_buffer {
188         /* common v4l buffer stuff -- must be first */
189         struct videobuf_buffer vb;
190         const struct zr364xx_fmt *fmt;
191 };
192
193 /* function used to send initialisation commands to the camera */
194 static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
195                             u16 index, unsigned char *cp, u16 size)
196 {
197         int status;
198
199         unsigned char *transfer_buffer = kmemdup(cp, size, GFP_KERNEL);
200         if (!transfer_buffer)
201                 return -ENOMEM;
202
203         status = usb_control_msg(udev,
204                                  usb_sndctrlpipe(udev, 0),
205                                  request,
206                                  USB_DIR_OUT | USB_TYPE_VENDOR |
207                                  USB_RECIP_DEVICE, value, index,
208                                  transfer_buffer, size, CTRL_TIMEOUT);
209
210         kfree(transfer_buffer);
211         return status;
212 }
213
214
215 /* Control messages sent to the camera to initialize it
216  * and launch the capture */
217 typedef struct {
218         unsigned int value;
219         unsigned int size;
220         unsigned char *bytes;
221 } message;
222
223 /* method 0 */
224 static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
225 static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
226 static unsigned char m0d3[] = { 0, 0 };
227 static message m0[] = {
228         {0x1f30, 0, NULL},
229         {0xd000, 0, NULL},
230         {0x3370, sizeof(m0d1), m0d1},
231         {0x2000, 0, NULL},
232         {0x2f0f, 0, NULL},
233         {0x2610, sizeof(m0d2), m0d2},
234         {0xe107, 0, NULL},
235         {0x2502, 0, NULL},
236         {0x1f70, 0, NULL},
237         {0xd000, 0, NULL},
238         {0x9a01, sizeof(m0d3), m0d3},
239         {-1, -1, NULL}
240 };
241
242 /* method 1 */
243 static unsigned char m1d1[] = { 0xff, 0xff };
244 static unsigned char m1d2[] = { 0x00, 0x00 };
245 static message m1[] = {
246         {0x1f30, 0, NULL},
247         {0xd000, 0, NULL},
248         {0xf000, 0, NULL},
249         {0x2000, 0, NULL},
250         {0x2f0f, 0, NULL},
251         {0x2650, 0, NULL},
252         {0xe107, 0, NULL},
253         {0x2502, sizeof(m1d1), m1d1},
254         {0x1f70, 0, NULL},
255         {0xd000, 0, NULL},
256         {0xd000, 0, NULL},
257         {0xd000, 0, NULL},
258         {0x9a01, sizeof(m1d2), m1d2},
259         {-1, -1, NULL}
260 };
261
262 /* method 2 */
263 static unsigned char m2d1[] = { 0xff, 0xff };
264 static message m2[] = {
265         {0x1f30, 0, NULL},
266         {0xf000, 0, NULL},
267         {0x2000, 0, NULL},
268         {0x2f0f, 0, NULL},
269         {0x2650, 0, NULL},
270         {0xe107, 0, NULL},
271         {0x2502, sizeof(m2d1), m2d1},
272         {0x1f70, 0, NULL},
273         {-1, -1, NULL}
274 };
275
276 /* init table */
277 static message *init[4] = { m0, m1, m2, m2 };
278
279
280 /* JPEG static data in header (Huffman table, etc) */
281 static unsigned char header1[] = {
282         0xFF, 0xD8,
283         /*
284         0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
285         0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
286         */
287         0xFF, 0xDB, 0x00, 0x84
288 };
289 static unsigned char header2[] = {
290         0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
291         0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
293         0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
294         0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
295         0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
296         0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
297         0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
298         0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
299         0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
300         0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
301         0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
302         0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
303         0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
304         0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
305         0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
306         0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
307         0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
308         0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
309         0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
310         0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
311         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
312         0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
313         0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
314         0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
315         0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
316         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
317         0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
318         0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
319         0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
320         0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
321         0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
322         0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
323         0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
324         0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
325         0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
326         0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
327         0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
328         0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
329         0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
330         0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
331         0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
332         0x00, 0x3F, 0x00
333 };
334 static unsigned char header3;
335
336 /* ------------------------------------------------------------------
337    Videobuf operations
338    ------------------------------------------------------------------*/
339
340 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
341                         unsigned int *size)
342 {
343         struct zr364xx_camera *cam = vq->priv_data;
344
345         *size = cam->width * cam->height * (cam->fmt->depth >> 3);
346
347         if (*count == 0)
348                 *count = ZR364XX_DEF_BUFS;
349
350         if (*size * *count > ZR364XX_DEF_BUFS * 1024 * 1024)
351                 *count = (ZR364XX_DEF_BUFS * 1024 * 1024) / *size;
352
353         return 0;
354 }
355
356 static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
357 {
358         _DBG("%s\n", __func__);
359
360         BUG_ON(in_interrupt());
361
362         videobuf_vmalloc_free(&buf->vb);
363         buf->vb.state = VIDEOBUF_NEEDS_INIT;
364 }
365
366 static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
367                           enum v4l2_field field)
368 {
369         struct zr364xx_camera *cam = vq->priv_data;
370         struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
371                                                   vb);
372         int rc;
373
374         DBG("%s, field=%d\n", __func__, field);
375         if (!cam->fmt)
376                 return -EINVAL;
377
378         buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
379
380         if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
381                 DBG("invalid buffer prepare\n");
382                 return -EINVAL;
383         }
384
385         buf->fmt = cam->fmt;
386         buf->vb.width = cam->width;
387         buf->vb.height = cam->height;
388         buf->vb.field = field;
389
390         if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
391                 rc = videobuf_iolock(vq, &buf->vb, NULL);
392                 if (rc < 0)
393                         goto fail;
394         }
395
396         buf->vb.state = VIDEOBUF_PREPARED;
397         return 0;
398 fail:
399         free_buffer(vq, buf);
400         return rc;
401 }
402
403 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
404 {
405         struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
406                                                   vb);
407         struct zr364xx_camera *cam = vq->priv_data;
408
409         _DBG("%s\n", __func__);
410
411         buf->vb.state = VIDEOBUF_QUEUED;
412         list_add_tail(&buf->vb.queue, &cam->vidq.active);
413 }
414
415 static void buffer_release(struct videobuf_queue *vq,
416                            struct videobuf_buffer *vb)
417 {
418         struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
419                                                   vb);
420
421         _DBG("%s\n", __func__);
422         free_buffer(vq, buf);
423 }
424
425 static const struct videobuf_queue_ops zr364xx_video_qops = {
426         .buf_setup = buffer_setup,
427         .buf_prepare = buffer_prepare,
428         .buf_queue = buffer_queue,
429         .buf_release = buffer_release,
430 };
431
432 /********************/
433 /* V4L2 integration */
434 /********************/
435 static int zr364xx_vidioc_streamon(struct file *file, void *priv,
436                                    enum v4l2_buf_type type);
437
438 static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
439                             loff_t * ppos)
440 {
441         struct zr364xx_camera *cam = video_drvdata(file);
442         int err = 0;
443
444         _DBG("%s\n", __func__);
445
446         if (!buf)
447                 return -EINVAL;
448
449         if (!count)
450                 return -EINVAL;
451
452         if (mutex_lock_interruptible(&cam->lock))
453                 return -ERESTARTSYS;
454
455         err = zr364xx_vidioc_streamon(file, file->private_data,
456                                 V4L2_BUF_TYPE_VIDEO_CAPTURE);
457         if (err == 0) {
458                 DBG("%s: reading %d bytes at pos %d.\n", __func__,
459                                 (int) count, (int) *ppos);
460
461                 /* NoMan Sux ! */
462                 err = videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
463                                         file->f_flags & O_NONBLOCK);
464         }
465         mutex_unlock(&cam->lock);
466         return err;
467 }
468
469 /* video buffer vmalloc implementation based partly on VIVI driver which is
470  *          Copyright (c) 2006 by
471  *                  Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
472  *                  Ted Walther <ted--a.t--enumera.com>
473  *                  John Sokol <sokol--a.t--videotechnology.com>
474  *                  http://v4l.videotechnology.com/
475  *
476  */
477 static void zr364xx_fillbuff(struct zr364xx_camera *cam,
478                              struct zr364xx_buffer *buf,
479                              int jpgsize)
480 {
481         int pos = 0;
482         const char *tmpbuf;
483         char *vbuf = videobuf_to_vmalloc(&buf->vb);
484         unsigned long last_frame;
485
486         if (!vbuf)
487                 return;
488
489         last_frame = cam->last_frame;
490         if (last_frame != -1) {
491                 tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
492                 switch (buf->fmt->fourcc) {
493                 case V4L2_PIX_FMT_JPEG:
494                         buf->vb.size = jpgsize;
495                         memcpy(vbuf, tmpbuf, buf->vb.size);
496                         break;
497                 default:
498                         printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
499                 }
500                 cam->last_frame = -1;
501         } else {
502                 printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
503                 return;
504         }
505         DBG("%s: Buffer %p size= %d\n", __func__, vbuf, pos);
506         /* tell v4l buffer was filled */
507
508         buf->vb.field_count = cam->frame_count * 2;
509         buf->vb.ts = ktime_get_ns();
510         buf->vb.state = VIDEOBUF_DONE;
511 }
512
513 static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
514 {
515         struct zr364xx_dmaqueue *dma_q = &cam->vidq;
516         struct zr364xx_buffer *buf;
517         unsigned long flags = 0;
518         int rc = 0;
519
520         DBG("wakeup: %p\n", &dma_q);
521         spin_lock_irqsave(&cam->slock, flags);
522
523         if (list_empty(&dma_q->active)) {
524                 DBG("No active queue to serve\n");
525                 rc = -1;
526                 goto unlock;
527         }
528         buf = list_entry(dma_q->active.next,
529                          struct zr364xx_buffer, vb.queue);
530
531         if (!waitqueue_active(&buf->vb.done)) {
532                 /* no one active */
533                 rc = -1;
534                 goto unlock;
535         }
536         list_del(&buf->vb.queue);
537         buf->vb.ts = ktime_get_ns();
538         DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
539         zr364xx_fillbuff(cam, buf, jpgsize);
540         wake_up(&buf->vb.done);
541         DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
542 unlock:
543         spin_unlock_irqrestore(&cam->slock, flags);
544         return rc;
545 }
546
547 /* this function moves the usb stream read pipe data
548  * into the system buffers.
549  * returns 0 on success, EAGAIN if more data to process (call this
550  * function again).
551  */
552 static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
553                                         struct zr364xx_pipeinfo *pipe_info,
554                                         struct urb *purb)
555 {
556         unsigned char *pdest;
557         unsigned char *psrc;
558         s32 idx = -1;
559         struct zr364xx_framei *frm;
560         int i = 0;
561         unsigned char *ptr = NULL;
562
563         _DBG("buffer to user\n");
564         idx = cam->cur_frame;
565         frm = &cam->buffer.frame[idx];
566
567         /* swap bytes if camera needs it */
568         if (cam->method == METHOD0) {
569                 u16 *buf = (u16 *)pipe_info->transfer_buffer;
570                 for (i = 0; i < purb->actual_length/2; i++)
571                         swab16s(buf + i);
572         }
573
574         /* search done.  now find out if should be acquiring */
575         if (!cam->b_acquire) {
576                 /* we found a frame, but this channel is turned off */
577                 frm->ulState = ZR364XX_READ_IDLE;
578                 return -EINVAL;
579         }
580
581         psrc = (u8 *)pipe_info->transfer_buffer;
582         ptr = pdest = frm->lpvbits;
583
584         if (frm->ulState == ZR364XX_READ_IDLE) {
585                 if (purb->actual_length < 128) {
586                         /* header incomplete */
587                         dev_info(&cam->udev->dev,
588                                  "%s: buffer (%d bytes) too small to hold jpeg header. Discarding.\n",
589                                  __func__, purb->actual_length);
590                         return -EINVAL;
591                 }
592
593                 frm->ulState = ZR364XX_READ_FRAME;
594                 frm->cur_size = 0;
595
596                 _DBG("jpeg header, ");
597                 memcpy(ptr, header1, sizeof(header1));
598                 ptr += sizeof(header1);
599                 header3 = 0;
600                 memcpy(ptr, &header3, 1);
601                 ptr++;
602                 memcpy(ptr, psrc, 64);
603                 ptr += 64;
604                 header3 = 1;
605                 memcpy(ptr, &header3, 1);
606                 ptr++;
607                 memcpy(ptr, psrc + 64, 64);
608                 ptr += 64;
609                 memcpy(ptr, header2, sizeof(header2));
610                 ptr += sizeof(header2);
611                 memcpy(ptr, psrc + 128,
612                        purb->actual_length - 128);
613                 ptr += purb->actual_length - 128;
614                 _DBG("header : %d %d %d %d %d %d %d %d %d\n",
615                     psrc[0], psrc[1], psrc[2],
616                     psrc[3], psrc[4], psrc[5],
617                     psrc[6], psrc[7], psrc[8]);
618                 frm->cur_size = ptr - pdest;
619         } else {
620                 if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
621                         dev_info(&cam->udev->dev,
622                                  "%s: buffer (%d bytes) too small to hold frame data. Discarding frame data.\n",
623                                  __func__, MAX_FRAME_SIZE);
624                 } else {
625                         pdest += frm->cur_size;
626                         memcpy(pdest, psrc, purb->actual_length);
627                         frm->cur_size += purb->actual_length;
628                 }
629         }
630         /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
631                 purb->actual_length);*/
632
633         if (purb->actual_length < pipe_info->transfer_size) {
634                 _DBG("****************Buffer[%d]full*************\n", idx);
635                 cam->last_frame = cam->cur_frame;
636                 cam->cur_frame++;
637                 /* end of system frame ring buffer, start at zero */
638                 if (cam->cur_frame == cam->buffer.dwFrames)
639                         cam->cur_frame = 0;
640
641                 /* frame ready */
642                 /* go back to find the JPEG EOI marker */
643                 ptr = pdest = frm->lpvbits;
644                 ptr += frm->cur_size - 2;
645                 while (ptr > pdest) {
646                         if (*ptr == 0xFF && *(ptr + 1) == 0xD9
647                             && *(ptr + 2) == 0xFF)
648                                 break;
649                         ptr--;
650                 }
651                 if (ptr == pdest)
652                         DBG("No EOI marker\n");
653
654                 /* Sometimes there is junk data in the middle of the picture,
655                  * we want to skip this bogus frames */
656                 while (ptr > pdest) {
657                         if (*ptr == 0xFF && *(ptr + 1) == 0xFF
658                             && *(ptr + 2) == 0xFF)
659                                 break;
660                         ptr--;
661                 }
662                 if (ptr != pdest) {
663                         DBG("Bogus frame ? %d\n", ++(cam->nb));
664                 } else if (cam->b_acquire) {
665                         /* we skip the 2 first frames which are usually buggy */
666                         if (cam->skip)
667                                 cam->skip--;
668                         else {
669                                 _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
670                                     frm->cur_size,
671                                     pdest[0], pdest[1], pdest[2], pdest[3],
672                                     pdest[4], pdest[5], pdest[6], pdest[7]);
673
674                                 zr364xx_got_frame(cam, frm->cur_size);
675                         }
676                 }
677                 cam->frame_count++;
678                 frm->ulState = ZR364XX_READ_IDLE;
679                 frm->cur_size = 0;
680         }
681         /* done successfully */
682         return 0;
683 }
684
685 static int zr364xx_vidioc_querycap(struct file *file, void *priv,
686                                    struct v4l2_capability *cap)
687 {
688         struct zr364xx_camera *cam = video_drvdata(file);
689
690         strscpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
691         if (cam->udev->product)
692                 strscpy(cap->card, cam->udev->product, sizeof(cap->card));
693         strscpy(cap->bus_info, dev_name(&cam->udev->dev),
694                 sizeof(cap->bus_info));
695         return 0;
696 }
697
698 static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
699                                      struct v4l2_input *i)
700 {
701         if (i->index != 0)
702                 return -EINVAL;
703         strscpy(i->name, DRIVER_DESC " Camera", sizeof(i->name));
704         i->type = V4L2_INPUT_TYPE_CAMERA;
705         return 0;
706 }
707
708 static int zr364xx_vidioc_g_input(struct file *file, void *priv,
709                                   unsigned int *i)
710 {
711         *i = 0;
712         return 0;
713 }
714
715 static int zr364xx_vidioc_s_input(struct file *file, void *priv,
716                                   unsigned int i)
717 {
718         if (i != 0)
719                 return -EINVAL;
720         return 0;
721 }
722
723 static int zr364xx_s_ctrl(struct v4l2_ctrl *ctrl)
724 {
725         struct zr364xx_camera *cam =
726                 container_of(ctrl->handler, struct zr364xx_camera, ctrl_handler);
727         int temp;
728
729         switch (ctrl->id) {
730         case V4L2_CID_BRIGHTNESS:
731                 /* hardware brightness */
732                 send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
733                 temp = (0x60 << 8) + 127 - ctrl->val;
734                 send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
735                 break;
736         default:
737                 return -EINVAL;
738         }
739
740         return 0;
741 }
742
743 static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
744                                        void *priv, struct v4l2_fmtdesc *f)
745 {
746         if (f->index > 0)
747                 return -EINVAL;
748         f->pixelformat = formats[0].fourcc;
749         return 0;
750 }
751
752 static char *decode_fourcc(__u32 pixelformat, char *buf)
753 {
754         buf[0] = pixelformat & 0xff;
755         buf[1] = (pixelformat >> 8) & 0xff;
756         buf[2] = (pixelformat >> 16) & 0xff;
757         buf[3] = (pixelformat >> 24) & 0xff;
758         buf[4] = '\0';
759         return buf;
760 }
761
762 static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
763                                       struct v4l2_format *f)
764 {
765         struct zr364xx_camera *cam = video_drvdata(file);
766         char pixelformat_name[5];
767
768         if (!cam)
769                 return -ENODEV;
770
771         if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
772                 DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
773                     decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
774                 return -EINVAL;
775         }
776
777         if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
778             !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
779                 f->fmt.pix.width = 320;
780                 f->fmt.pix.height = 240;
781         }
782
783         f->fmt.pix.field = V4L2_FIELD_NONE;
784         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
785         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
786         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
787         DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
788             decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
789             f->fmt.pix.field);
790         return 0;
791 }
792
793 static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
794                                     struct v4l2_format *f)
795 {
796         struct zr364xx_camera *cam;
797
798         if (!file)
799                 return -ENODEV;
800         cam = video_drvdata(file);
801
802         f->fmt.pix.pixelformat = formats[0].fourcc;
803         f->fmt.pix.field = V4L2_FIELD_NONE;
804         f->fmt.pix.width = cam->width;
805         f->fmt.pix.height = cam->height;
806         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
807         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
808         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
809         return 0;
810 }
811
812 static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
813                                     struct v4l2_format *f)
814 {
815         struct zr364xx_camera *cam = video_drvdata(file);
816         struct videobuf_queue *q = &cam->vb_vidq;
817         char pixelformat_name[5];
818         int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
819         int i;
820
821         if (ret < 0)
822                 return ret;
823
824         mutex_lock(&q->vb_lock);
825
826         if (videobuf_queue_is_busy(&cam->vb_vidq)) {
827                 DBG("%s queue busy\n", __func__);
828                 ret = -EBUSY;
829                 goto out;
830         }
831
832         if (cam->owner) {
833                 DBG("%s can't change format after started\n", __func__);
834                 ret = -EBUSY;
835                 goto out;
836         }
837
838         cam->width = f->fmt.pix.width;
839         cam->height = f->fmt.pix.height;
840         DBG("%s: %dx%d mode selected\n", __func__,
841                  cam->width, cam->height);
842         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
843         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
844         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
845         cam->vb_vidq.field = f->fmt.pix.field;
846
847         if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
848                 mode = 1;
849         else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
850                 mode = 2;
851         else
852                 mode = 0;
853
854         m0d1[0] = mode;
855         m1[2].value = 0xf000 + mode;
856         m2[1].value = 0xf000 + mode;
857
858         /* special case for METHOD3, the modes are different */
859         if (cam->method == METHOD3) {
860                 switch (mode) {
861                 case 1:
862                         m2[1].value = 0xf000 + 4;
863                         break;
864                 case 2:
865                         m2[1].value = 0xf000 + 0;
866                         break;
867                 default:
868                         m2[1].value = 0xf000 + 1;
869                         break;
870                 }
871         }
872
873         header2[437] = cam->height / 256;
874         header2[438] = cam->height % 256;
875         header2[439] = cam->width / 256;
876         header2[440] = cam->width % 256;
877
878         for (i = 0; init[cam->method][i].size != -1; i++) {
879                 ret =
880                     send_control_msg(cam->udev, 1, init[cam->method][i].value,
881                                      0, init[cam->method][i].bytes,
882                                      init[cam->method][i].size);
883                 if (ret < 0) {
884                         dev_err(&cam->udev->dev,
885                            "error during resolution change sequence: %d\n", i);
886                         goto out;
887                 }
888         }
889
890         /* Added some delay here, since opening/closing the camera quickly,
891          * like Ekiga does during its startup, can crash the webcam
892          */
893         mdelay(100);
894         cam->skip = 2;
895         ret = 0;
896
897 out:
898         mutex_unlock(&q->vb_lock);
899
900         DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
901             decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
902             f->fmt.pix.field);
903         return ret;
904 }
905
906 static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
907                           struct v4l2_requestbuffers *p)
908 {
909         struct zr364xx_camera *cam = video_drvdata(file);
910
911         if (cam->owner && cam->owner != priv)
912                 return -EBUSY;
913         return videobuf_reqbufs(&cam->vb_vidq, p);
914 }
915
916 static int zr364xx_vidioc_querybuf(struct file *file,
917                                 void *priv,
918                                 struct v4l2_buffer *p)
919 {
920         int rc;
921         struct zr364xx_camera *cam = video_drvdata(file);
922         rc = videobuf_querybuf(&cam->vb_vidq, p);
923         return rc;
924 }
925
926 static int zr364xx_vidioc_qbuf(struct file *file,
927                                 void *priv,
928                                 struct v4l2_buffer *p)
929 {
930         int rc;
931         struct zr364xx_camera *cam = video_drvdata(file);
932         _DBG("%s\n", __func__);
933         if (cam->owner && cam->owner != priv)
934                 return -EBUSY;
935         rc = videobuf_qbuf(&cam->vb_vidq, p);
936         return rc;
937 }
938
939 static int zr364xx_vidioc_dqbuf(struct file *file,
940                                 void *priv,
941                                 struct v4l2_buffer *p)
942 {
943         int rc;
944         struct zr364xx_camera *cam = video_drvdata(file);
945         _DBG("%s\n", __func__);
946         if (cam->owner && cam->owner != priv)
947                 return -EBUSY;
948         rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
949         return rc;
950 }
951
952 static void read_pipe_completion(struct urb *purb)
953 {
954         struct zr364xx_pipeinfo *pipe_info;
955         struct zr364xx_camera *cam;
956         int pipe;
957
958         pipe_info = purb->context;
959         _DBG("%s %p, status %d\n", __func__, purb, purb->status);
960         if (!pipe_info) {
961                 printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
962                 return;
963         }
964
965         cam = pipe_info->cam;
966         if (!cam) {
967                 printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
968                 return;
969         }
970
971         /* if shutting down, do not resubmit, exit immediately */
972         if (purb->status == -ESHUTDOWN) {
973                 DBG("%s, err shutdown\n", __func__);
974                 pipe_info->err_count++;
975                 return;
976         }
977
978         if (pipe_info->state == 0) {
979                 DBG("exiting USB pipe\n");
980                 return;
981         }
982
983         if (purb->actual_length > pipe_info->transfer_size) {
984                 dev_err(&cam->udev->dev, "wrong number of bytes\n");
985                 return;
986         }
987
988         if (purb->status == 0)
989                 zr364xx_read_video_callback(cam, pipe_info, purb);
990         else {
991                 pipe_info->err_count++;
992                 DBG("%s: failed URB %d\n", __func__, purb->status);
993         }
994
995         pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
996
997         /* reuse urb */
998         usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
999                           pipe,
1000                           pipe_info->transfer_buffer,
1001                           pipe_info->transfer_size,
1002                           read_pipe_completion, pipe_info);
1003
1004         if (pipe_info->state != 0) {
1005                 purb->status = usb_submit_urb(pipe_info->stream_urb,
1006                                               GFP_ATOMIC);
1007
1008                 if (purb->status)
1009                         dev_err(&cam->udev->dev,
1010                                 "error submitting urb (error=%i)\n",
1011                                 purb->status);
1012         } else
1013                 DBG("read pipe complete state 0\n");
1014 }
1015
1016 static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
1017 {
1018         int pipe;
1019         int retval;
1020         struct zr364xx_pipeinfo *pipe_info = cam->pipe;
1021         pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
1022         DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
1023
1024         pipe_info->state = 1;
1025         pipe_info->err_count = 0;
1026         pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
1027         if (!pipe_info->stream_urb)
1028                 return -ENOMEM;
1029         /* transfer buffer allocated in board_init */
1030         usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
1031                           pipe,
1032                           pipe_info->transfer_buffer,
1033                           pipe_info->transfer_size,
1034                           read_pipe_completion, pipe_info);
1035
1036         DBG("submitting URB %p\n", pipe_info->stream_urb);
1037         retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
1038         if (retval) {
1039                 printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
1040                 return retval;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
1047 {
1048         struct zr364xx_pipeinfo *pipe_info;
1049
1050         if (!cam) {
1051                 printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
1052                 return;
1053         }
1054         DBG("stop read pipe\n");
1055         pipe_info = cam->pipe;
1056         if (pipe_info) {
1057                 if (pipe_info->state != 0)
1058                         pipe_info->state = 0;
1059
1060                 if (pipe_info->stream_urb) {
1061                         /* cancel urb */
1062                         usb_kill_urb(pipe_info->stream_urb);
1063                         usb_free_urb(pipe_info->stream_urb);
1064                         pipe_info->stream_urb = NULL;
1065                 }
1066         }
1067         return;
1068 }
1069
1070 /* starts acquisition process */
1071 static int zr364xx_start_acquire(struct zr364xx_camera *cam)
1072 {
1073         int j;
1074
1075         DBG("start acquire\n");
1076
1077         cam->last_frame = -1;
1078         cam->cur_frame = 0;
1079         for (j = 0; j < FRAMES; j++) {
1080                 cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
1081                 cam->buffer.frame[j].cur_size = 0;
1082         }
1083         cam->b_acquire = 1;
1084         return 0;
1085 }
1086
1087 static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
1088 {
1089         cam->b_acquire = 0;
1090         return 0;
1091 }
1092
1093 static int zr364xx_prepare(struct zr364xx_camera *cam)
1094 {
1095         int res;
1096         int i, j;
1097
1098         for (i = 0; init[cam->method][i].size != -1; i++) {
1099                 res = send_control_msg(cam->udev, 1, init[cam->method][i].value,
1100                                      0, init[cam->method][i].bytes,
1101                                      init[cam->method][i].size);
1102                 if (res < 0) {
1103                         dev_err(&cam->udev->dev,
1104                                 "error during open sequence: %d\n", i);
1105                         return res;
1106                 }
1107         }
1108
1109         cam->skip = 2;
1110         cam->last_frame = -1;
1111         cam->cur_frame = 0;
1112         cam->frame_count = 0;
1113         for (j = 0; j < FRAMES; j++) {
1114                 cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
1115                 cam->buffer.frame[j].cur_size = 0;
1116         }
1117         v4l2_ctrl_handler_setup(&cam->ctrl_handler);
1118         return 0;
1119 }
1120
1121 static int zr364xx_vidioc_streamon(struct file *file, void *priv,
1122                                    enum v4l2_buf_type type)
1123 {
1124         struct zr364xx_camera *cam = video_drvdata(file);
1125         int res;
1126
1127         DBG("%s\n", __func__);
1128
1129         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1130                 return -EINVAL;
1131
1132         if (cam->owner && cam->owner != priv)
1133                 return -EBUSY;
1134
1135         res = zr364xx_prepare(cam);
1136         if (res)
1137                 return res;
1138         res = videobuf_streamon(&cam->vb_vidq);
1139         if (res == 0) {
1140                 zr364xx_start_acquire(cam);
1141                 cam->owner = file->private_data;
1142         }
1143         return res;
1144 }
1145
1146 static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
1147                                     enum v4l2_buf_type type)
1148 {
1149         struct zr364xx_camera *cam = video_drvdata(file);
1150
1151         DBG("%s\n", __func__);
1152         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1153                 return -EINVAL;
1154         if (cam->owner && cam->owner != priv)
1155                 return -EBUSY;
1156         zr364xx_stop_acquire(cam);
1157         return videobuf_streamoff(&cam->vb_vidq);
1158 }
1159
1160
1161 /* open the camera */
1162 static int zr364xx_open(struct file *file)
1163 {
1164         struct zr364xx_camera *cam = video_drvdata(file);
1165         int err;
1166
1167         DBG("%s\n", __func__);
1168
1169         if (mutex_lock_interruptible(&cam->lock))
1170                 return -ERESTARTSYS;
1171
1172         err = v4l2_fh_open(file);
1173         if (err)
1174                 goto out;
1175
1176         /* Added some delay here, since opening/closing the camera quickly,
1177          * like Ekiga does during its startup, can crash the webcam
1178          */
1179         mdelay(100);
1180         err = 0;
1181
1182 out:
1183         mutex_unlock(&cam->lock);
1184         DBG("%s: %d\n", __func__, err);
1185         return err;
1186 }
1187
1188 static void zr364xx_release(struct v4l2_device *v4l2_dev)
1189 {
1190         struct zr364xx_camera *cam =
1191                 container_of(v4l2_dev, struct zr364xx_camera, v4l2_dev);
1192         unsigned long i;
1193
1194         v4l2_device_unregister(&cam->v4l2_dev);
1195
1196         videobuf_mmap_free(&cam->vb_vidq);
1197
1198         /* release sys buffers */
1199         for (i = 0; i < FRAMES; i++) {
1200                 if (cam->buffer.frame[i].lpvbits) {
1201                         DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
1202                         vfree(cam->buffer.frame[i].lpvbits);
1203                 }
1204                 cam->buffer.frame[i].lpvbits = NULL;
1205         }
1206
1207         v4l2_ctrl_handler_free(&cam->ctrl_handler);
1208         /* release transfer buffer */
1209         kfree(cam->pipe->transfer_buffer);
1210         kfree(cam);
1211 }
1212
1213 /* release the camera */
1214 static int zr364xx_close(struct file *file)
1215 {
1216         struct zr364xx_camera *cam;
1217         struct usb_device *udev;
1218         int i;
1219
1220         DBG("%s\n", __func__);
1221         cam = video_drvdata(file);
1222
1223         mutex_lock(&cam->lock);
1224         udev = cam->udev;
1225
1226         if (file->private_data == cam->owner) {
1227                 /* turn off stream */
1228                 if (cam->b_acquire)
1229                         zr364xx_stop_acquire(cam);
1230                 videobuf_streamoff(&cam->vb_vidq);
1231
1232                 for (i = 0; i < 2; i++) {
1233                         send_control_msg(udev, 1, init[cam->method][i].value,
1234                                         0, init[cam->method][i].bytes,
1235                                         init[cam->method][i].size);
1236                 }
1237                 cam->owner = NULL;
1238         }
1239
1240         /* Added some delay here, since opening/closing the camera quickly,
1241          * like Ekiga does during its startup, can crash the webcam
1242          */
1243         mdelay(100);
1244         mutex_unlock(&cam->lock);
1245         return v4l2_fh_release(file);
1246 }
1247
1248
1249 static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
1250 {
1251         struct zr364xx_camera *cam = video_drvdata(file);
1252         int ret;
1253
1254         if (!cam) {
1255                 DBG("%s: cam == NULL\n", __func__);
1256                 return -ENODEV;
1257         }
1258         DBG("mmap called, vma=%p\n", vma);
1259
1260         ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
1261
1262         DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
1263                 (unsigned long)vma->vm_start,
1264                 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
1265         return ret;
1266 }
1267
1268 static __poll_t zr364xx_poll(struct file *file,
1269                                struct poll_table_struct *wait)
1270 {
1271         struct zr364xx_camera *cam = video_drvdata(file);
1272         struct videobuf_queue *q = &cam->vb_vidq;
1273         __poll_t res = v4l2_ctrl_poll(file, wait);
1274
1275         _DBG("%s\n", __func__);
1276
1277         return res | videobuf_poll_stream(file, q, wait);
1278 }
1279
1280 static const struct v4l2_ctrl_ops zr364xx_ctrl_ops = {
1281         .s_ctrl = zr364xx_s_ctrl,
1282 };
1283
1284 static const struct v4l2_file_operations zr364xx_fops = {
1285         .owner = THIS_MODULE,
1286         .open = zr364xx_open,
1287         .release = zr364xx_close,
1288         .read = zr364xx_read,
1289         .mmap = zr364xx_mmap,
1290         .unlocked_ioctl = video_ioctl2,
1291         .poll = zr364xx_poll,
1292 };
1293
1294 static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
1295         .vidioc_querycap        = zr364xx_vidioc_querycap,
1296         .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
1297         .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
1298         .vidioc_s_fmt_vid_cap   = zr364xx_vidioc_s_fmt_vid_cap,
1299         .vidioc_g_fmt_vid_cap   = zr364xx_vidioc_g_fmt_vid_cap,
1300         .vidioc_enum_input      = zr364xx_vidioc_enum_input,
1301         .vidioc_g_input         = zr364xx_vidioc_g_input,
1302         .vidioc_s_input         = zr364xx_vidioc_s_input,
1303         .vidioc_streamon        = zr364xx_vidioc_streamon,
1304         .vidioc_streamoff       = zr364xx_vidioc_streamoff,
1305         .vidioc_reqbufs         = zr364xx_vidioc_reqbufs,
1306         .vidioc_querybuf        = zr364xx_vidioc_querybuf,
1307         .vidioc_qbuf            = zr364xx_vidioc_qbuf,
1308         .vidioc_dqbuf           = zr364xx_vidioc_dqbuf,
1309         .vidioc_log_status      = v4l2_ctrl_log_status,
1310         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1311         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1312 };
1313
1314 static const struct video_device zr364xx_template = {
1315         .name = DRIVER_DESC,
1316         .fops = &zr364xx_fops,
1317         .ioctl_ops = &zr364xx_ioctl_ops,
1318         .release = video_device_release_empty,
1319         .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1320                        V4L2_CAP_STREAMING,
1321 };
1322
1323
1324
1325 /*******************/
1326 /* USB integration */
1327 /*******************/
1328 static int zr364xx_board_init(struct zr364xx_camera *cam)
1329 {
1330         struct zr364xx_pipeinfo *pipe = cam->pipe;
1331         unsigned long i;
1332
1333         DBG("board init: %p\n", cam);
1334         memset(pipe, 0, sizeof(*pipe));
1335         pipe->cam = cam;
1336         pipe->transfer_size = BUFFER_SIZE;
1337
1338         pipe->transfer_buffer = kzalloc(pipe->transfer_size,
1339                                         GFP_KERNEL);
1340         if (!pipe->transfer_buffer) {
1341                 DBG("out of memory!\n");
1342                 return -ENOMEM;
1343         }
1344
1345         cam->b_acquire = 0;
1346         cam->frame_count = 0;
1347
1348         /*** start create system buffers ***/
1349         for (i = 0; i < FRAMES; i++) {
1350                 /* always allocate maximum size for system buffers */
1351                 cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
1352
1353                 DBG("valloc %p, idx %lu, pdata %p\n",
1354                         &cam->buffer.frame[i], i,
1355                         cam->buffer.frame[i].lpvbits);
1356                 if (!cam->buffer.frame[i].lpvbits) {
1357                         printk(KERN_INFO KBUILD_MODNAME ": out of memory. Using less frames\n");
1358                         break;
1359                 }
1360         }
1361
1362         if (i == 0) {
1363                 printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
1364                 kfree(cam->pipe->transfer_buffer);
1365                 cam->pipe->transfer_buffer = NULL;
1366                 return -ENOMEM;
1367         } else
1368                 cam->buffer.dwFrames = i;
1369
1370         /* make sure internal states are set */
1371         for (i = 0; i < FRAMES; i++) {
1372                 cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
1373                 cam->buffer.frame[i].cur_size = 0;
1374         }
1375
1376         cam->cur_frame = 0;
1377         cam->last_frame = -1;
1378         /*** end create system buffers ***/
1379
1380         /* start read pipe */
1381         zr364xx_start_readpipe(cam);
1382         DBG(": board initialized\n");
1383         return 0;
1384 }
1385
1386 static int zr364xx_probe(struct usb_interface *intf,
1387                          const struct usb_device_id *id)
1388 {
1389         struct usb_device *udev = interface_to_usbdev(intf);
1390         struct zr364xx_camera *cam = NULL;
1391         struct usb_host_interface *iface_desc;
1392         struct usb_endpoint_descriptor *endpoint;
1393         struct v4l2_ctrl_handler *hdl;
1394         int err;
1395         int i;
1396
1397         DBG("probing...\n");
1398
1399         dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
1400         dev_info(&intf->dev, "model %04x:%04x detected\n",
1401                  le16_to_cpu(udev->descriptor.idVendor),
1402                  le16_to_cpu(udev->descriptor.idProduct));
1403
1404         cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1405         if (!cam)
1406                 return -ENOMEM;
1407
1408         cam->v4l2_dev.release = zr364xx_release;
1409         err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
1410         if (err < 0) {
1411                 dev_err(&udev->dev, "couldn't register v4l2_device\n");
1412                 kfree(cam);
1413                 return err;
1414         }
1415         hdl = &cam->ctrl_handler;
1416         v4l2_ctrl_handler_init(hdl, 1);
1417         v4l2_ctrl_new_std(hdl, &zr364xx_ctrl_ops,
1418                           V4L2_CID_BRIGHTNESS, 0, 127, 1, 64);
1419         if (hdl->error) {
1420                 err = hdl->error;
1421                 dev_err(&udev->dev, "couldn't register control\n");
1422                 goto fail;
1423         }
1424         /* save the init method used by this camera */
1425         cam->method = id->driver_info;
1426         mutex_init(&cam->lock);
1427         cam->vdev = zr364xx_template;
1428         cam->vdev.lock = &cam->lock;
1429         cam->vdev.v4l2_dev = &cam->v4l2_dev;
1430         cam->vdev.ctrl_handler = &cam->ctrl_handler;
1431         video_set_drvdata(&cam->vdev, cam);
1432
1433         cam->udev = udev;
1434
1435         switch (mode) {
1436         case 1:
1437                 dev_info(&udev->dev, "160x120 mode selected\n");
1438                 cam->width = 160;
1439                 cam->height = 120;
1440                 break;
1441         case 2:
1442                 dev_info(&udev->dev, "640x480 mode selected\n");
1443                 cam->width = 640;
1444                 cam->height = 480;
1445                 break;
1446         default:
1447                 dev_info(&udev->dev, "320x240 mode selected\n");
1448                 cam->width = 320;
1449                 cam->height = 240;
1450                 break;
1451         }
1452
1453         m0d1[0] = mode;
1454         m1[2].value = 0xf000 + mode;
1455         m2[1].value = 0xf000 + mode;
1456
1457         /* special case for METHOD3, the modes are different */
1458         if (cam->method == METHOD3) {
1459                 switch (mode) {
1460                 case 1:
1461                         m2[1].value = 0xf000 + 4;
1462                         break;
1463                 case 2:
1464                         m2[1].value = 0xf000 + 0;
1465                         break;
1466                 default:
1467                         m2[1].value = 0xf000 + 1;
1468                         break;
1469                 }
1470         }
1471
1472         header2[437] = cam->height / 256;
1473         header2[438] = cam->height % 256;
1474         header2[439] = cam->width / 256;
1475         header2[440] = cam->width % 256;
1476
1477         cam->nb = 0;
1478
1479         DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
1480
1481         /* set up the endpoint information  */
1482         iface_desc = intf->cur_altsetting;
1483         DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
1484         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1485                 endpoint = &iface_desc->endpoint[i].desc;
1486                 if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1487                         /* we found the bulk in endpoint */
1488                         cam->read_endpoint = endpoint->bEndpointAddress;
1489                 }
1490         }
1491
1492         if (!cam->read_endpoint) {
1493                 err = -ENOMEM;
1494                 dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
1495                 goto fail;
1496         }
1497
1498         /* v4l */
1499         INIT_LIST_HEAD(&cam->vidq.active);
1500         cam->vidq.cam = cam;
1501
1502         usb_set_intfdata(intf, cam);
1503
1504         /* load zr364xx board specific */
1505         err = zr364xx_board_init(cam);
1506         if (!err)
1507                 err = v4l2_ctrl_handler_setup(hdl);
1508         if (err)
1509                 goto fail;
1510
1511         spin_lock_init(&cam->slock);
1512
1513         cam->fmt = formats;
1514
1515         videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
1516                                     NULL, &cam->slock,
1517                                     V4L2_BUF_TYPE_VIDEO_CAPTURE,
1518                                     V4L2_FIELD_NONE,
1519                                     sizeof(struct zr364xx_buffer), cam, &cam->lock);
1520
1521         err = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
1522         if (err) {
1523                 dev_err(&udev->dev, "video_register_device failed\n");
1524                 goto fail;
1525         }
1526
1527         dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
1528                  video_device_node_name(&cam->vdev));
1529         return 0;
1530
1531 fail:
1532         v4l2_ctrl_handler_free(hdl);
1533         v4l2_device_unregister(&cam->v4l2_dev);
1534         kfree(cam);
1535         return err;
1536 }
1537
1538
1539 static void zr364xx_disconnect(struct usb_interface *intf)
1540 {
1541         struct zr364xx_camera *cam = usb_get_intfdata(intf);
1542
1543         mutex_lock(&cam->lock);
1544         usb_set_intfdata(intf, NULL);
1545         dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
1546         video_unregister_device(&cam->vdev);
1547         v4l2_device_disconnect(&cam->v4l2_dev);
1548
1549         /* stops the read pipe if it is running */
1550         if (cam->b_acquire)
1551                 zr364xx_stop_acquire(cam);
1552
1553         zr364xx_stop_readpipe(cam);
1554         mutex_unlock(&cam->lock);
1555         v4l2_device_put(&cam->v4l2_dev);
1556 }
1557
1558
1559 #ifdef CONFIG_PM
1560 static int zr364xx_suspend(struct usb_interface *intf, pm_message_t message)
1561 {
1562         struct zr364xx_camera *cam = usb_get_intfdata(intf);
1563
1564         cam->was_streaming = cam->b_acquire;
1565         if (!cam->was_streaming)
1566                 return 0;
1567         zr364xx_stop_acquire(cam);
1568         zr364xx_stop_readpipe(cam);
1569         return 0;
1570 }
1571
1572 static int zr364xx_resume(struct usb_interface *intf)
1573 {
1574         struct zr364xx_camera *cam = usb_get_intfdata(intf);
1575         int res;
1576
1577         if (!cam->was_streaming)
1578                 return 0;
1579
1580         zr364xx_start_readpipe(cam);
1581         res = zr364xx_prepare(cam);
1582         if (!res)
1583                 zr364xx_start_acquire(cam);
1584         return res;
1585 }
1586 #endif
1587
1588 /**********************/
1589 /* Module integration */
1590 /**********************/
1591
1592 static struct usb_driver zr364xx_driver = {
1593         .name = "zr364xx",
1594         .probe = zr364xx_probe,
1595         .disconnect = zr364xx_disconnect,
1596 #ifdef CONFIG_PM
1597         .suspend = zr364xx_suspend,
1598         .resume = zr364xx_resume,
1599         .reset_resume = zr364xx_resume,
1600 #endif
1601         .id_table = device_table
1602 };
1603
1604 module_usb_driver(zr364xx_driver);
1605
1606 MODULE_AUTHOR(DRIVER_AUTHOR);
1607 MODULE_DESCRIPTION(DRIVER_DESC);
1608 MODULE_LICENSE("GPL");
1609 MODULE_VERSION(DRIVER_VERSION);