]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/zr364xx.c
V4L/DVB: remove unused #include <version.h>
[linux-2.6-omap-h63xx.git] / drivers / media / video / zr364xx.c
1 /*
2  * Zoran 364xx based USB webcam module version 0.72
3  *
4  * Allows you to use your USB webcam with V4L2 applications
5  * This is still in heavy developpement !
6  *
7  * Copyright (C) 2004  Antoine Jacquet <royale@zerezo.com>
8  * http://royale.zerezo.com/zr364xx/
9  *
10  * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
11  * V4L2 version inspired by meye.c driver
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28
29 #include <linux/version.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <linux/slab.h>
35 #include <linux/proc_fs.h>
36 #include <linux/highmem.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39
40
41 /* Version Information */
42 #define DRIVER_VERSION "v0.72"
43 #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
44 #define DRIVER_DESC "Zoran 364xx"
45
46
47 /* Camera */
48 #define FRAMES 2
49 #define MAX_FRAME_SIZE 100000
50 #define BUFFER_SIZE 0x1000
51 #define CTRL_TIMEOUT 500
52
53
54 /* Debug macro */
55 #define DBG(x...) if (debug) info(x)
56
57
58 /* Init methods, need to find nicer names for these
59  * the exact names of the chipsets would be the best if someone finds it */
60 #define METHOD0 0
61 #define METHOD1 1
62 #define METHOD2 2
63
64
65 /* Module parameters */
66 static int debug;
67 static int mode;
68
69
70 /* Module parameters interface */
71 module_param(debug, int, 0644);
72 MODULE_PARM_DESC(debug, "Debug level");
73 module_param(mode, int, 0644);
74 MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
75
76
77 /* Devices supported by this driver
78  * .driver_info contains the init method used by the camera */
79 static struct usb_device_id device_table[] = {
80         {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
81         {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
82         {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
83         {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
84         {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
85         {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
86         {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
87         {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
88         {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
89         {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
90         {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
91         {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
92         {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
93         {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
94         {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
95         {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
96         {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
97         {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
98         {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
99         {}                      /* Terminating entry */
100 };
101
102 MODULE_DEVICE_TABLE(usb, device_table);
103
104
105 /* Camera stuff */
106 struct zr364xx_camera {
107         struct usb_device *udev;        /* save off the usb device pointer */
108         struct usb_interface *interface;/* the interface for this device */
109         struct video_device *vdev;      /* v4l video device */
110         u8 *framebuf;
111         int nb;
112         unsigned char *buffer;
113         int skip;
114         int brightness;
115         int width;
116         int height;
117         int method;
118         struct mutex lock;
119         int users;
120 };
121
122
123 /* function used to send initialisation commands to the camera */
124 static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
125                             u16 index, unsigned char *cp, u16 size)
126 {
127         int status;
128
129         unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
130         if (!transfer_buffer) {
131                 info("kmalloc(%d) failed", size);
132                 return -ENOMEM;
133         }
134
135         memcpy(transfer_buffer, cp, size);
136
137         status = usb_control_msg(udev,
138                                  usb_sndctrlpipe(udev, 0),
139                                  request,
140                                  USB_DIR_OUT | USB_TYPE_VENDOR |
141                                  USB_RECIP_DEVICE, value, index,
142                                  transfer_buffer, size, CTRL_TIMEOUT);
143
144         kfree(transfer_buffer);
145
146         if (status < 0)
147                 info("Failed sending control message, error %d.", status);
148
149         return status;
150 }
151
152
153 /* Control messages sent to the camera to initialize it
154  * and launch the capture */
155 typedef struct {
156         unsigned int value;
157         unsigned int size;
158         unsigned char *bytes;
159 } message;
160
161 /* method 0 */
162 static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
163 static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
164 static unsigned char m0d3[] = { 0, 0 };
165 static message m0[] = {
166         {0x1f30, 0, NULL},
167         {0xd000, 0, NULL},
168         {0x3370, sizeof(m0d1), m0d1},
169         {0x2000, 0, NULL},
170         {0x2f0f, 0, NULL},
171         {0x2610, sizeof(m0d2), m0d2},
172         {0xe107, 0, NULL},
173         {0x2502, 0, NULL},
174         {0x1f70, 0, NULL},
175         {0xd000, 0, NULL},
176         {0x9a01, sizeof(m0d3), m0d3},
177         {-1, -1, NULL}
178 };
179
180 /* method 1 */
181 static unsigned char m1d1[] = { 0xff, 0xff };
182 static unsigned char m1d2[] = { 0x00, 0x00 };
183 static message m1[] = {
184         {0x1f30, 0, NULL},
185         {0xd000, 0, NULL},
186         {0xf000, 0, NULL},
187         {0x2000, 0, NULL},
188         {0x2f0f, 0, NULL},
189         {0x2650, 0, NULL},
190         {0xe107, 0, NULL},
191         {0x2502, sizeof(m1d1), m1d1},
192         {0x1f70, 0, NULL},
193         {0xd000, 0, NULL},
194         {0xd000, 0, NULL},
195         {0xd000, 0, NULL},
196         {0x9a01, sizeof(m1d2), m1d2},
197         {-1, -1, NULL}
198 };
199
200 /* method 2 */
201 static unsigned char m2d1[] = { 0xff, 0xff };
202 static message m2[] = {
203         {0x1f30, 0, NULL},
204         {0xf000, 0, NULL},
205         {0x2000, 0, NULL},
206         {0x2f0f, 0, NULL},
207         {0x2650, 0, NULL},
208         {0xe107, 0, NULL},
209         {0x2502, sizeof(m2d1), m2d1},
210         {0x1f70, 0, NULL},
211         {-1, -1, NULL}
212 };
213
214 /* init table */
215 static message *init[3] = { m0, m1, m2 };
216
217
218 /* JPEG static data in header (Huffman table, etc) */
219 static unsigned char header1[] = {
220         0xFF, 0xD8,
221         /*
222         0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
223         0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
224         */
225         0xFF, 0xDB, 0x00, 0x84
226 };
227 static unsigned char header2[] = {
228         0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
229         0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
231         0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
232         0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
233         0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
234         0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
235         0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
236         0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
237         0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
238         0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
239         0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
240         0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
241         0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
242         0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
243         0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
244         0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
245         0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
246         0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
247         0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
248         0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
249         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
250         0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
251         0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
252         0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
253         0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
254         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
255         0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
256         0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
257         0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
258         0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
259         0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
260         0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
261         0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
262         0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
263         0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
264         0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
265         0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
266         0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
267         0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
268         0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
269         0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
270         0x00, 0x3F, 0x00
271 };
272 static unsigned char header3;
273
274
275
276 /********************/
277 /* V4L2 integration */
278 /********************/
279
280 /* this function reads a full JPEG picture synchronously
281  * TODO: do it asynchronously... */
282 static int read_frame(struct zr364xx_camera *cam, int framenum)
283 {
284         int i, n, temp, head, size, actual_length;
285         unsigned char *ptr = NULL, *jpeg;
286
287       redo:
288         /* hardware brightness */
289         n = send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
290         temp = (0x60 << 8) + 127 - cam->brightness;
291         n = send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
292
293         /* during the first loop we are going to insert JPEG header */
294         head = 0;
295         /* this is the place in memory where we are going to build
296          * the JPEG image */
297         jpeg = cam->framebuf + framenum * MAX_FRAME_SIZE;
298         /* read data... */
299         do {
300                 n = usb_bulk_msg(cam->udev,
301                                  usb_rcvbulkpipe(cam->udev, 0x81),
302                                  cam->buffer, BUFFER_SIZE, &actual_length,
303                                  CTRL_TIMEOUT);
304                 DBG("buffer : %d %d", cam->buffer[0], cam->buffer[1]);
305                 DBG("bulk : n=%d size=%d", n, actual_length);
306                 if (n < 0) {
307                         info("error reading bulk msg");
308                         return 0;
309                 }
310                 if (actual_length < 0 || actual_length > BUFFER_SIZE) {
311                         info("wrong number of bytes");
312                         return 0;
313                 }
314
315                 /* swap bytes if camera needs it */
316                 if (cam->method == METHOD0) {
317                         u16 *buf = (u16*)cam->buffer;
318                         for (i = 0; i < BUFFER_SIZE/2; i++)
319                                 swab16s(buf + i);
320                 }
321
322                 /* write the JPEG header */
323                 if (!head) {
324                         DBG("jpeg header");
325                         ptr = jpeg;
326                         memcpy(ptr, header1, sizeof(header1));
327                         ptr += sizeof(header1);
328                         header3 = 0;
329                         memcpy(ptr, &header3, 1);
330                         ptr++;
331                         memcpy(ptr, cam->buffer, 64);
332                         ptr += 64;
333                         header3 = 1;
334                         memcpy(ptr, &header3, 1);
335                         ptr++;
336                         memcpy(ptr, cam->buffer + 64, 64);
337                         ptr += 64;
338                         memcpy(ptr, header2, sizeof(header2));
339                         ptr += sizeof(header2);
340                         memcpy(ptr, cam->buffer + 128,
341                                actual_length - 128);
342                         ptr += actual_length - 128;
343                         head = 1;
344                         DBG("header : %d %d %d %d %d %d %d %d %d",
345                             cam->buffer[0], cam->buffer[1], cam->buffer[2],
346                             cam->buffer[3], cam->buffer[4], cam->buffer[5],
347                             cam->buffer[6], cam->buffer[7], cam->buffer[8]);
348                 } else {
349                         memcpy(ptr, cam->buffer, actual_length);
350                         ptr += actual_length;
351                 }
352         }
353         /* ... until there is no more */
354         while (actual_length == BUFFER_SIZE);
355
356         /* we skip the 2 first frames which are usually buggy */
357         if (cam->skip) {
358                 cam->skip--;
359                 goto redo;
360         }
361
362         /* go back to find the JPEG EOI marker */
363         size = ptr - jpeg;
364         ptr -= 2;
365         while (ptr > jpeg) {
366                 if (*ptr == 0xFF && *(ptr + 1) == 0xD9
367                     && *(ptr + 2) == 0xFF)
368                         break;
369                 ptr--;
370         }
371         if (ptr == jpeg)
372                 DBG("No EOI marker");
373
374         /* Sometimes there is junk data in the middle of the picture,
375          * we want to skip this bogus frames */
376         while (ptr > jpeg) {
377                 if (*ptr == 0xFF && *(ptr + 1) == 0xFF
378                     && *(ptr + 2) == 0xFF)
379                         break;
380                 ptr--;
381         }
382         if (ptr != jpeg) {
383                 DBG("Bogus frame ? %d", cam->nb);
384                 goto redo;
385         }
386
387         DBG("jpeg : %d %d %d %d %d %d %d %d",
388             jpeg[0], jpeg[1], jpeg[2], jpeg[3],
389             jpeg[4], jpeg[5], jpeg[6], jpeg[7]);
390
391         return size;
392 }
393
394
395 static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t cnt,
396                             loff_t * ppos)
397 {
398         unsigned long count = cnt;
399         struct video_device *vdev = video_devdata(file);
400         struct zr364xx_camera *cam;
401
402         DBG("zr364xx_read: read %d bytes.", (int) count);
403
404         if (vdev == NULL)
405                 return -ENODEV;
406         cam = video_get_drvdata(vdev);
407
408         if (!buf)
409                 return -EINVAL;
410
411         if (!count)
412                 return -EINVAL;
413
414         /* NoMan Sux ! */
415         count = read_frame(cam, 0);
416
417         if (copy_to_user(buf, cam->framebuf, count))
418                 return -EFAULT;
419
420         return count;
421 }
422
423
424 static int zr364xx_vidioc_querycap(struct file *file, void *priv,
425                                    struct v4l2_capability *cap)
426 {
427         memset(cap, 0, sizeof(*cap));
428         strcpy(cap->driver, DRIVER_DESC);
429         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
430         return 0;
431 }
432
433 static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
434                                      struct v4l2_input *i)
435 {
436         if (i->index != 0)
437                 return -EINVAL;
438         memset(i, 0, sizeof(*i));
439         i->index = 0;
440         strcpy(i->name, DRIVER_DESC " Camera");
441         i->type = V4L2_INPUT_TYPE_CAMERA;
442         return 0;
443 }
444
445 static int zr364xx_vidioc_g_input(struct file *file, void *priv,
446                                   unsigned int *i)
447 {
448         *i = 0;
449         return 0;
450 }
451
452 static int zr364xx_vidioc_s_input(struct file *file, void *priv,
453                                   unsigned int i)
454 {
455         if (i != 0)
456                 return -EINVAL;
457         return 0;
458 }
459
460 static int zr364xx_vidioc_queryctrl(struct file *file, void *priv,
461                                     struct v4l2_queryctrl *c)
462 {
463         struct video_device *vdev = video_devdata(file);
464         struct zr364xx_camera *cam;
465
466         if (vdev == NULL)
467                 return -ENODEV;
468         cam = video_get_drvdata(vdev);
469
470         switch (c->id) {
471         case V4L2_CID_BRIGHTNESS:
472                 c->type = V4L2_CTRL_TYPE_INTEGER;
473                 strcpy(c->name, "Brightness");
474                 c->minimum = 0;
475                 c->maximum = 127;
476                 c->step = 1;
477                 c->default_value = cam->brightness;
478                 c->flags = 0;
479                 break;
480         default:
481                 return -EINVAL;
482         }
483         return 0;
484 }
485
486 static int zr364xx_vidioc_s_ctrl(struct file *file, void *priv,
487                                  struct v4l2_control *c)
488 {
489         struct video_device *vdev = video_devdata(file);
490         struct zr364xx_camera *cam;
491
492         if (vdev == NULL)
493                 return -ENODEV;
494         cam = video_get_drvdata(vdev);
495
496         switch (c->id) {
497         case V4L2_CID_BRIGHTNESS:
498                 cam->brightness = c->value;
499                 break;
500         default:
501                 return -EINVAL;
502         }
503         return 0;
504 }
505
506 static int zr364xx_vidioc_g_ctrl(struct file *file, void *priv,
507                                  struct v4l2_control *c)
508 {
509         struct video_device *vdev = video_devdata(file);
510         struct zr364xx_camera *cam;
511
512         if (vdev == NULL)
513                 return -ENODEV;
514         cam = video_get_drvdata(vdev);
515
516         switch (c->id) {
517         case V4L2_CID_BRIGHTNESS:
518                 c->value = cam->brightness;
519                 break;
520         default:
521                 return -EINVAL;
522         }
523         return 0;
524 }
525
526 static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
527                                        void *priv, struct v4l2_fmtdesc *f)
528 {
529         if (f->index > 0)
530                 return -EINVAL;
531         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
532                 return -EINVAL;
533         memset(f, 0, sizeof(*f));
534         f->index = 0;
535         f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
536         f->flags = V4L2_FMT_FLAG_COMPRESSED;
537         strcpy(f->description, "JPEG");
538         f->pixelformat = V4L2_PIX_FMT_JPEG;
539         return 0;
540 }
541
542 static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
543                                       struct v4l2_format *f)
544 {
545         struct video_device *vdev = video_devdata(file);
546         struct zr364xx_camera *cam;
547
548         if (vdev == NULL)
549                 return -ENODEV;
550         cam = video_get_drvdata(vdev);
551
552         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
553                 return -EINVAL;
554         if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
555                 return -EINVAL;
556         if (f->fmt.pix.field != V4L2_FIELD_ANY &&
557             f->fmt.pix.field != V4L2_FIELD_NONE)
558                 return -EINVAL;
559         f->fmt.pix.field = V4L2_FIELD_NONE;
560         f->fmt.pix.width = cam->width;
561         f->fmt.pix.height = cam->height;
562         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
563         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
564         f->fmt.pix.colorspace = 0;
565         f->fmt.pix.priv = 0;
566         return 0;
567 }
568
569 static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
570                                     struct v4l2_format *f)
571 {
572         struct video_device *vdev = video_devdata(file);
573         struct zr364xx_camera *cam;
574
575         if (vdev == NULL)
576                 return -ENODEV;
577         cam = video_get_drvdata(vdev);
578
579         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
580                 return -EINVAL;
581         memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
582         f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
583         f->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
584         f->fmt.pix.field = V4L2_FIELD_NONE;
585         f->fmt.pix.width = cam->width;
586         f->fmt.pix.height = cam->height;
587         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
588         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
589         f->fmt.pix.colorspace = 0;
590         f->fmt.pix.priv = 0;
591         return 0;
592 }
593
594 static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
595                                     struct v4l2_format *f)
596 {
597         struct video_device *vdev = video_devdata(file);
598         struct zr364xx_camera *cam;
599
600         if (vdev == NULL)
601                 return -ENODEV;
602         cam = video_get_drvdata(vdev);
603
604         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
605                 return -EINVAL;
606         if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
607                 return -EINVAL;
608         if (f->fmt.pix.field != V4L2_FIELD_ANY &&
609             f->fmt.pix.field != V4L2_FIELD_NONE)
610                 return -EINVAL;
611         f->fmt.pix.field = V4L2_FIELD_NONE;
612         f->fmt.pix.width = cam->width;
613         f->fmt.pix.height = cam->height;
614         f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
615         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
616         f->fmt.pix.colorspace = 0;
617         f->fmt.pix.priv = 0;
618         DBG("ok!");
619         return 0;
620 }
621
622 static int zr364xx_vidioc_streamon(struct file *file, void *priv,
623                                    enum v4l2_buf_type type)
624 {
625         return 0;
626 }
627
628 static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
629                                     enum v4l2_buf_type type)
630 {
631         return 0;
632 }
633
634
635 /* open the camera */
636 static int zr364xx_open(struct inode *inode, struct file *file)
637 {
638         struct video_device *vdev = video_devdata(file);
639         struct zr364xx_camera *cam = video_get_drvdata(vdev);
640         struct usb_device *udev = cam->udev;
641         int i, err;
642
643         DBG("zr364xx_open");
644
645         mutex_lock(&cam->lock);
646
647         if (cam->users) {
648                 err = -EBUSY;
649                 goto out;
650         }
651
652         if (!cam->framebuf) {
653                 cam->framebuf = vmalloc_32(MAX_FRAME_SIZE * FRAMES);
654                 if (!cam->framebuf) {
655                         info("vmalloc_32 failed!");
656                         err = -ENOMEM;
657                         goto out;
658                 }
659         }
660
661         for (i = 0; init[cam->method][i].size != -1; i++) {
662                 err =
663                     send_control_msg(udev, 1, init[cam->method][i].value,
664                                      0, init[cam->method][i].bytes,
665                                      init[cam->method][i].size);
666                 if (err < 0) {
667                         info("error during open sequence: %d", i);
668                         goto out;
669                 }
670         }
671
672         cam->skip = 2;
673         cam->users++;
674         file->private_data = vdev;
675
676         /* Added some delay here, since opening/closing the camera quickly,
677          * like Ekiga does during its startup, can crash the webcam
678          */
679         mdelay(100);
680         err = 0;
681
682 out:
683         mutex_unlock(&cam->lock);
684         return err;
685 }
686
687
688 /* release the camera */
689 static int zr364xx_release(struct inode *inode, struct file *file)
690 {
691         struct video_device *vdev = video_devdata(file);
692         struct zr364xx_camera *cam;
693         struct usb_device *udev;
694         int i, err;
695
696         DBG("zr364xx_release");
697
698         if (vdev == NULL)
699                 return -ENODEV;
700         cam = video_get_drvdata(vdev);
701
702         udev = cam->udev;
703
704         mutex_lock(&cam->lock);
705
706         cam->users--;
707         file->private_data = NULL;
708
709         for (i = 0; i < 2; i++) {
710                 err =
711                     send_control_msg(udev, 1, init[cam->method][i].value,
712                                      0, init[i][cam->method].bytes,
713                                      init[cam->method][i].size);
714                 if (err < 0) {
715                         info("error during release sequence");
716                         goto out;
717                 }
718         }
719
720         /* Added some delay here, since opening/closing the camera quickly,
721          * like Ekiga does during its startup, can crash the webcam
722          */
723         mdelay(100);
724         err = 0;
725
726 out:
727         mutex_unlock(&cam->lock);
728         return err;
729 }
730
731
732 static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
733 {
734         void *pos;
735         unsigned long start = vma->vm_start;
736         unsigned long size = vma->vm_end - vma->vm_start;
737         struct video_device *vdev = video_devdata(file);
738         struct zr364xx_camera *cam;
739
740         DBG("zr364xx_mmap: %ld\n", size);
741
742         if (vdev == NULL)
743                 return -ENODEV;
744         cam = video_get_drvdata(vdev);
745
746         pos = cam->framebuf;
747         while (size > 0) {
748                 if (vm_insert_page(vma, start, vmalloc_to_page(pos)))
749                         return -EAGAIN;
750                 start += PAGE_SIZE;
751                 pos += PAGE_SIZE;
752                 if (size > PAGE_SIZE)
753                         size -= PAGE_SIZE;
754                 else
755                         size = 0;
756         }
757
758         return 0;
759 }
760
761
762 static const struct file_operations zr364xx_fops = {
763         .owner = THIS_MODULE,
764         .open = zr364xx_open,
765         .release = zr364xx_release,
766         .read = zr364xx_read,
767         .mmap = zr364xx_mmap,
768         .ioctl = video_ioctl2,
769         .llseek = no_llseek,
770 };
771
772 static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
773         .vidioc_querycap        = zr364xx_vidioc_querycap,
774         .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
775         .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
776         .vidioc_s_fmt_vid_cap   = zr364xx_vidioc_s_fmt_vid_cap,
777         .vidioc_g_fmt_vid_cap   = zr364xx_vidioc_g_fmt_vid_cap,
778         .vidioc_enum_input      = zr364xx_vidioc_enum_input,
779         .vidioc_g_input         = zr364xx_vidioc_g_input,
780         .vidioc_s_input         = zr364xx_vidioc_s_input,
781         .vidioc_streamon        = zr364xx_vidioc_streamon,
782         .vidioc_streamoff       = zr364xx_vidioc_streamoff,
783         .vidioc_queryctrl       = zr364xx_vidioc_queryctrl,
784         .vidioc_g_ctrl          = zr364xx_vidioc_g_ctrl,
785         .vidioc_s_ctrl          = zr364xx_vidioc_s_ctrl,
786 };
787
788 static struct video_device zr364xx_template = {
789         .name = DRIVER_DESC,
790         .fops = &zr364xx_fops,
791         .ioctl_ops = &zr364xx_ioctl_ops,
792         .release = video_device_release,
793         .minor = -1,
794 };
795
796
797
798 /*******************/
799 /* USB integration */
800 /*******************/
801
802 static int zr364xx_probe(struct usb_interface *intf,
803                          const struct usb_device_id *id)
804 {
805         struct usb_device *udev = interface_to_usbdev(intf);
806         struct zr364xx_camera *cam = NULL;
807         int err;
808
809         DBG("probing...");
810
811         info(DRIVER_DESC " compatible webcam plugged");
812         info("model %04x:%04x detected", udev->descriptor.idVendor,
813              udev->descriptor.idProduct);
814
815         cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
816         if (cam == NULL) {
817                 info("cam: out of memory !");
818                 return -ENOMEM;
819         }
820         /* save the init method used by this camera */
821         cam->method = id->driver_info;
822
823         cam->vdev = video_device_alloc();
824         if (cam->vdev == NULL) {
825                 info("cam->vdev: out of memory !");
826                 kfree(cam);
827                 return -ENOMEM;
828         }
829         memcpy(cam->vdev, &zr364xx_template, sizeof(zr364xx_template));
830         video_set_drvdata(cam->vdev, cam);
831         if (debug)
832                 cam->vdev->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
833
834         cam->udev = udev;
835
836         if ((cam->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL)) == NULL) {
837                 info("cam->buffer: out of memory !");
838                 video_device_release(cam->vdev);
839                 kfree(cam);
840                 return -ENODEV;
841         }
842
843         switch (mode) {
844         case 1:
845                 info("160x120 mode selected");
846                 cam->width = 160;
847                 cam->height = 120;
848                 break;
849         case 2:
850                 info("640x480 mode selected");
851                 cam->width = 640;
852                 cam->height = 480;
853                 break;
854         default:
855                 info("320x240 mode selected");
856                 cam->width = 320;
857                 cam->height = 240;
858                 break;
859         }
860
861         m0d1[0] = mode;
862         m1[2].value = 0xf000 + mode;
863         m2[1].value = 0xf000 + mode;
864         header2[437] = cam->height / 256;
865         header2[438] = cam->height % 256;
866         header2[439] = cam->width / 256;
867         header2[440] = cam->width % 256;
868
869         cam->nb = 0;
870         cam->brightness = 64;
871         mutex_init(&cam->lock);
872
873         err = video_register_device(cam->vdev, VFL_TYPE_GRABBER, -1);
874         if (err) {
875                 info("video_register_device failed");
876                 video_device_release(cam->vdev);
877                 kfree(cam->buffer);
878                 kfree(cam);
879                 return err;
880         }
881
882         usb_set_intfdata(intf, cam);
883
884         info(DRIVER_DESC " controlling video device %d", cam->vdev->minor);
885         return 0;
886 }
887
888
889 static void zr364xx_disconnect(struct usb_interface *intf)
890 {
891         struct zr364xx_camera *cam = usb_get_intfdata(intf);
892         usb_set_intfdata(intf, NULL);
893         dev_set_drvdata(&intf->dev, NULL);
894         info(DRIVER_DESC " webcam unplugged");
895         if (cam->vdev)
896                 video_unregister_device(cam->vdev);
897         cam->vdev = NULL;
898         kfree(cam->buffer);
899         if (cam->framebuf)
900                 vfree(cam->framebuf);
901         kfree(cam);
902 }
903
904
905
906 /**********************/
907 /* Module integration */
908 /**********************/
909
910 static struct usb_driver zr364xx_driver = {
911         .name = "zr364xx",
912         .probe = zr364xx_probe,
913         .disconnect = zr364xx_disconnect,
914         .id_table = device_table
915 };
916
917
918 static int __init zr364xx_init(void)
919 {
920         int retval;
921         retval = usb_register(&zr364xx_driver);
922         if (retval)
923                 info("usb_register failed!");
924         else
925                 info(DRIVER_DESC " module loaded");
926         return retval;
927 }
928
929
930 static void __exit zr364xx_exit(void)
931 {
932         info(DRIVER_DESC " module unloaded");
933         usb_deregister(&zr364xx_driver);
934 }
935
936
937 module_init(zr364xx_init);
938 module_exit(zr364xx_exit);
939
940 MODULE_AUTHOR(DRIVER_AUTHOR);
941 MODULE_DESCRIPTION(DRIVER_DESC);
942 MODULE_LICENSE("GPL");