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