blob: fc976f42f4328d70bbccb9a65a1d4bd0b5173cf5 [file] [log] [blame]
Antoine Jacquetb7eee612007-04-27 12:30:59 -03001/*
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
Antoine Jacquetb7eee612007-04-27 12:30:59 -030029#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/usb.h>
32#include <linux/vmalloc.h>
33#include <linux/slab.h>
34#include <linux/proc_fs.h>
Antoine Jacquet2575f842007-03-05 06:32:29 -030035#include <linux/highmem.h>
Antoine Jacquetb7eee612007-04-27 12:30:59 -030036#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Antoine Jacquetb7eee612007-04-27 12:30:59 -030038
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 */
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -030054#define DBG(x...) if (debug) printk(KERN_INFO KBUILD_MODNAME x)
Antoine Jacquetb7eee612007-04-27 12:30:59 -030055
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 */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030065static int debug;
66static int mode;
Antoine Jacquetb7eee612007-04-27 12:30:59 -030067
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 },
Antoine Jacquetbebeaea2007-06-25 16:00:34 -030095 {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
Antoine Jacquet71c04472008-01-25 22:01:53 -030096 {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
Antoine Jacquetc0e0aff2008-01-25 22:03:10 -030097 {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
Antoine Jacquetc76b6382009-01-17 22:49:08 -030098 {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD2 },
Antoine Jacquetb7eee612007-04-27 12:30:59 -030099 {} /* Terminating entry */
100};
101
102MODULE_DEVICE_TABLE(usb, device_table);
103
104
105/* Camera stuff */
106struct 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;
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300119 int users;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300120};
121
122
123/* function used to send initialisation commands to the camera */
124static 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) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300131 dev_err(&udev->dev, "kmalloc(%d) failed\n", size);
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300132 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)
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300147 dev_err(&udev->dev,
148 "Failed sending control message, error %d.\n", status);
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300149
150 return status;
151}
152
153
154/* Control messages sent to the camera to initialize it
155 * and launch the capture */
156typedef struct {
157 unsigned int value;
158 unsigned int size;
159 unsigned char *bytes;
160} message;
161
162/* method 0 */
163static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
164static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
165static unsigned char m0d3[] = { 0, 0 };
166static message m0[] = {
167 {0x1f30, 0, NULL},
168 {0xd000, 0, NULL},
169 {0x3370, sizeof(m0d1), m0d1},
170 {0x2000, 0, NULL},
171 {0x2f0f, 0, NULL},
172 {0x2610, sizeof(m0d2), m0d2},
173 {0xe107, 0, NULL},
174 {0x2502, 0, NULL},
175 {0x1f70, 0, NULL},
176 {0xd000, 0, NULL},
177 {0x9a01, sizeof(m0d3), m0d3},
178 {-1, -1, NULL}
179};
180
181/* method 1 */
182static unsigned char m1d1[] = { 0xff, 0xff };
183static unsigned char m1d2[] = { 0x00, 0x00 };
184static message m1[] = {
185 {0x1f30, 0, NULL},
186 {0xd000, 0, NULL},
187 {0xf000, 0, NULL},
188 {0x2000, 0, NULL},
189 {0x2f0f, 0, NULL},
190 {0x2650, 0, NULL},
191 {0xe107, 0, NULL},
192 {0x2502, sizeof(m1d1), m1d1},
193 {0x1f70, 0, NULL},
194 {0xd000, 0, NULL},
195 {0xd000, 0, NULL},
196 {0xd000, 0, NULL},
197 {0x9a01, sizeof(m1d2), m1d2},
198 {-1, -1, NULL}
199};
200
201/* method 2 */
202static unsigned char m2d1[] = { 0xff, 0xff };
203static message m2[] = {
204 {0x1f30, 0, NULL},
205 {0xf000, 0, NULL},
206 {0x2000, 0, NULL},
207 {0x2f0f, 0, NULL},
208 {0x2650, 0, NULL},
209 {0xe107, 0, NULL},
210 {0x2502, sizeof(m2d1), m2d1},
211 {0x1f70, 0, NULL},
212 {-1, -1, NULL}
213};
214
215/* init table */
216static message *init[3] = { m0, m1, m2 };
217
218
219/* JPEG static data in header (Huffman table, etc) */
220static unsigned char header1[] = {
221 0xFF, 0xD8,
222 /*
223 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
224 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
225 */
226 0xFF, 0xDB, 0x00, 0x84
227};
228static unsigned char header2[] = {
229 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
230 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
232 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
233 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
234 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
235 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
236 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
237 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
238 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
239 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
240 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
241 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
242 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
243 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
244 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
245 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
246 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
247 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
248 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
249 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
250 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
251 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
252 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
253 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
254 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
255 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
256 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
257 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
258 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
259 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
260 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
261 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
262 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
263 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
264 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
265 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
266 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
267 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
268 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
269 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
270 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
271 0x00, 0x3F, 0x00
272};
273static unsigned char header3;
274
275
276
277/********************/
278/* V4L2 integration */
279/********************/
280
281/* this function reads a full JPEG picture synchronously
282 * TODO: do it asynchronously... */
283static int read_frame(struct zr364xx_camera *cam, int framenum)
284{
285 int i, n, temp, head, size, actual_length;
Trent Piepho93566ad2007-03-07 18:19:49 -0300286 unsigned char *ptr = NULL, *jpeg;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300287
288 redo:
289 /* hardware brightness */
290 n = send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
291 temp = (0x60 << 8) + 127 - cam->brightness;
292 n = send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
293
294 /* during the first loop we are going to insert JPEG header */
295 head = 0;
296 /* this is the place in memory where we are going to build
297 * the JPEG image */
298 jpeg = cam->framebuf + framenum * MAX_FRAME_SIZE;
299 /* read data... */
300 do {
301 n = usb_bulk_msg(cam->udev,
302 usb_rcvbulkpipe(cam->udev, 0x81),
303 cam->buffer, BUFFER_SIZE, &actual_length,
304 CTRL_TIMEOUT);
305 DBG("buffer : %d %d", cam->buffer[0], cam->buffer[1]);
306 DBG("bulk : n=%d size=%d", n, actual_length);
307 if (n < 0) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300308 dev_err(&cam->udev->dev, "error reading bulk msg\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300309 return 0;
310 }
311 if (actual_length < 0 || actual_length > BUFFER_SIZE) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300312 dev_err(&cam->udev->dev, "wrong number of bytes\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300313 return 0;
314 }
315
316 /* swap bytes if camera needs it */
Trent Piepho93566ad2007-03-07 18:19:49 -0300317 if (cam->method == METHOD0) {
318 u16 *buf = (u16*)cam->buffer;
319 for (i = 0; i < BUFFER_SIZE/2; i++)
320 swab16s(buf + i);
321 }
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300322
323 /* write the JPEG header */
324 if (!head) {
325 DBG("jpeg header");
326 ptr = jpeg;
327 memcpy(ptr, header1, sizeof(header1));
328 ptr += sizeof(header1);
329 header3 = 0;
330 memcpy(ptr, &header3, 1);
331 ptr++;
332 memcpy(ptr, cam->buffer, 64);
333 ptr += 64;
334 header3 = 1;
335 memcpy(ptr, &header3, 1);
336 ptr++;
337 memcpy(ptr, cam->buffer + 64, 64);
338 ptr += 64;
339 memcpy(ptr, header2, sizeof(header2));
340 ptr += sizeof(header2);
341 memcpy(ptr, cam->buffer + 128,
342 actual_length - 128);
343 ptr += actual_length - 128;
344 head = 1;
345 DBG("header : %d %d %d %d %d %d %d %d %d",
346 cam->buffer[0], cam->buffer[1], cam->buffer[2],
347 cam->buffer[3], cam->buffer[4], cam->buffer[5],
348 cam->buffer[6], cam->buffer[7], cam->buffer[8]);
349 } else {
350 memcpy(ptr, cam->buffer, actual_length);
351 ptr += actual_length;
352 }
353 }
354 /* ... until there is no more */
355 while (actual_length == BUFFER_SIZE);
356
357 /* we skip the 2 first frames which are usually buggy */
358 if (cam->skip) {
359 cam->skip--;
360 goto redo;
361 }
362
363 /* go back to find the JPEG EOI marker */
364 size = ptr - jpeg;
365 ptr -= 2;
366 while (ptr > jpeg) {
367 if (*ptr == 0xFF && *(ptr + 1) == 0xD9
368 && *(ptr + 2) == 0xFF)
369 break;
370 ptr--;
371 }
372 if (ptr == jpeg)
373 DBG("No EOI marker");
374
375 /* Sometimes there is junk data in the middle of the picture,
376 * we want to skip this bogus frames */
377 while (ptr > jpeg) {
378 if (*ptr == 0xFF && *(ptr + 1) == 0xFF
379 && *(ptr + 2) == 0xFF)
380 break;
381 ptr--;
382 }
383 if (ptr != jpeg) {
384 DBG("Bogus frame ? %d", cam->nb);
385 goto redo;
386 }
387
388 DBG("jpeg : %d %d %d %d %d %d %d %d",
389 jpeg[0], jpeg[1], jpeg[2], jpeg[3],
390 jpeg[4], jpeg[5], jpeg[6], jpeg[7]);
391
392 return size;
393}
394
395
Al Viro97cf0102008-03-29 03:10:48 +0000396static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t cnt,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300397 loff_t * ppos)
398{
399 unsigned long count = cnt;
400 struct video_device *vdev = video_devdata(file);
401 struct zr364xx_camera *cam;
402
403 DBG("zr364xx_read: read %d bytes.", (int) count);
404
405 if (vdev == NULL)
406 return -ENODEV;
407 cam = video_get_drvdata(vdev);
408
409 if (!buf)
410 return -EINVAL;
411
412 if (!count)
413 return -EINVAL;
414
415 /* NoMan Sux ! */
416 count = read_frame(cam, 0);
417
418 if (copy_to_user(buf, cam->framebuf, count))
419 return -EFAULT;
420
421 return count;
422}
423
424
425static int zr364xx_vidioc_querycap(struct file *file, void *priv,
426 struct v4l2_capability *cap)
427{
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300428 strcpy(cap->driver, DRIVER_DESC);
429 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
430 return 0;
431}
432
433static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
434 struct v4l2_input *i)
435{
436 if (i->index != 0)
437 return -EINVAL;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300438 strcpy(i->name, DRIVER_DESC " Camera");
439 i->type = V4L2_INPUT_TYPE_CAMERA;
440 return 0;
441}
442
443static int zr364xx_vidioc_g_input(struct file *file, void *priv,
444 unsigned int *i)
445{
446 *i = 0;
447 return 0;
448}
449
450static int zr364xx_vidioc_s_input(struct file *file, void *priv,
451 unsigned int i)
452{
453 if (i != 0)
454 return -EINVAL;
455 return 0;
456}
457
458static int zr364xx_vidioc_queryctrl(struct file *file, void *priv,
459 struct v4l2_queryctrl *c)
460{
461 struct video_device *vdev = video_devdata(file);
462 struct zr364xx_camera *cam;
463
464 if (vdev == NULL)
465 return -ENODEV;
466 cam = video_get_drvdata(vdev);
467
468 switch (c->id) {
469 case V4L2_CID_BRIGHTNESS:
470 c->type = V4L2_CTRL_TYPE_INTEGER;
471 strcpy(c->name, "Brightness");
472 c->minimum = 0;
473 c->maximum = 127;
474 c->step = 1;
475 c->default_value = cam->brightness;
476 c->flags = 0;
477 break;
478 default:
479 return -EINVAL;
480 }
481 return 0;
482}
483
484static int zr364xx_vidioc_s_ctrl(struct file *file, void *priv,
485 struct v4l2_control *c)
486{
487 struct video_device *vdev = video_devdata(file);
488 struct zr364xx_camera *cam;
489
490 if (vdev == NULL)
491 return -ENODEV;
492 cam = video_get_drvdata(vdev);
493
494 switch (c->id) {
495 case V4L2_CID_BRIGHTNESS:
496 cam->brightness = c->value;
497 break;
498 default:
499 return -EINVAL;
500 }
501 return 0;
502}
503
504static int zr364xx_vidioc_g_ctrl(struct file *file, void *priv,
505 struct v4l2_control *c)
506{
507 struct video_device *vdev = video_devdata(file);
508 struct zr364xx_camera *cam;
509
510 if (vdev == NULL)
511 return -ENODEV;
512 cam = video_get_drvdata(vdev);
513
514 switch (c->id) {
515 case V4L2_CID_BRIGHTNESS:
516 c->value = cam->brightness;
517 break;
518 default:
519 return -EINVAL;
520 }
521 return 0;
522}
523
Hans Verkuil78b526a2008-05-28 12:16:41 -0300524static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300525 void *priv, struct v4l2_fmtdesc *f)
526{
527 if (f->index > 0)
528 return -EINVAL;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300529 f->flags = V4L2_FMT_FLAG_COMPRESSED;
530 strcpy(f->description, "JPEG");
531 f->pixelformat = V4L2_PIX_FMT_JPEG;
532 return 0;
533}
534
Hans Verkuil78b526a2008-05-28 12:16:41 -0300535static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300536 struct v4l2_format *f)
537{
538 struct video_device *vdev = video_devdata(file);
539 struct zr364xx_camera *cam;
540
541 if (vdev == NULL)
542 return -ENODEV;
543 cam = video_get_drvdata(vdev);
544
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300545 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
546 return -EINVAL;
547 if (f->fmt.pix.field != V4L2_FIELD_ANY &&
548 f->fmt.pix.field != V4L2_FIELD_NONE)
549 return -EINVAL;
550 f->fmt.pix.field = V4L2_FIELD_NONE;
551 f->fmt.pix.width = cam->width;
552 f->fmt.pix.height = cam->height;
553 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
554 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
555 f->fmt.pix.colorspace = 0;
556 f->fmt.pix.priv = 0;
557 return 0;
558}
559
Hans Verkuil78b526a2008-05-28 12:16:41 -0300560static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300561 struct v4l2_format *f)
562{
563 struct video_device *vdev = video_devdata(file);
564 struct zr364xx_camera *cam;
565
566 if (vdev == NULL)
567 return -ENODEV;
568 cam = video_get_drvdata(vdev);
569
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300570 f->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
571 f->fmt.pix.field = V4L2_FIELD_NONE;
572 f->fmt.pix.width = cam->width;
573 f->fmt.pix.height = cam->height;
574 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
575 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
576 f->fmt.pix.colorspace = 0;
577 f->fmt.pix.priv = 0;
578 return 0;
579}
580
Hans Verkuil78b526a2008-05-28 12:16:41 -0300581static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300582 struct v4l2_format *f)
583{
584 struct video_device *vdev = video_devdata(file);
585 struct zr364xx_camera *cam;
586
587 if (vdev == NULL)
588 return -ENODEV;
589 cam = video_get_drvdata(vdev);
590
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300591 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
592 return -EINVAL;
593 if (f->fmt.pix.field != V4L2_FIELD_ANY &&
594 f->fmt.pix.field != V4L2_FIELD_NONE)
595 return -EINVAL;
596 f->fmt.pix.field = V4L2_FIELD_NONE;
597 f->fmt.pix.width = cam->width;
598 f->fmt.pix.height = cam->height;
599 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
600 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
601 f->fmt.pix.colorspace = 0;
602 f->fmt.pix.priv = 0;
603 DBG("ok!");
604 return 0;
605}
606
607static int zr364xx_vidioc_streamon(struct file *file, void *priv,
608 enum v4l2_buf_type type)
609{
610 return 0;
611}
612
613static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
614 enum v4l2_buf_type type)
615{
616 return 0;
617}
618
619
620/* open the camera */
Hans Verkuilbec43662008-12-30 06:58:20 -0300621static int zr364xx_open(struct file *file)
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300622{
623 struct video_device *vdev = video_devdata(file);
624 struct zr364xx_camera *cam = video_get_drvdata(vdev);
625 struct usb_device *udev = cam->udev;
626 int i, err;
627
628 DBG("zr364xx_open");
629
Antoine Jacquet69025c92008-08-18 17:09:53 -0300630 mutex_lock(&cam->lock);
631
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300632 if (cam->users) {
633 err = -EBUSY;
Antoine Jacquet69025c92008-08-18 17:09:53 -0300634 goto out;
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300635 }
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300636
637 if (!cam->framebuf) {
638 cam->framebuf = vmalloc_32(MAX_FRAME_SIZE * FRAMES);
639 if (!cam->framebuf) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300640 dev_err(&cam->udev->dev, "vmalloc_32 failed!\n");
Antoine Jacquet69025c92008-08-18 17:09:53 -0300641 err = -ENOMEM;
642 goto out;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300643 }
644 }
645
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300646 for (i = 0; init[cam->method][i].size != -1; i++) {
647 err =
648 send_control_msg(udev, 1, init[cam->method][i].value,
649 0, init[cam->method][i].bytes,
650 init[cam->method][i].size);
651 if (err < 0) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300652 dev_err(&cam->udev->dev,
653 "error during open sequence: %d\n", i);
Antoine Jacquet69025c92008-08-18 17:09:53 -0300654 goto out;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300655 }
656 }
657
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300658 cam->skip = 2;
659 cam->users++;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300660 file->private_data = vdev;
661
662 /* Added some delay here, since opening/closing the camera quickly,
663 * like Ekiga does during its startup, can crash the webcam
664 */
665 mdelay(100);
Antoine Jacquet69025c92008-08-18 17:09:53 -0300666 err = 0;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300667
Antoine Jacquet69025c92008-08-18 17:09:53 -0300668out:
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300669 mutex_unlock(&cam->lock);
Antoine Jacquet69025c92008-08-18 17:09:53 -0300670 return err;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300671}
672
673
674/* release the camera */
Hans Verkuilbec43662008-12-30 06:58:20 -0300675static int zr364xx_release(struct file *file)
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300676{
677 struct video_device *vdev = video_devdata(file);
678 struct zr364xx_camera *cam;
679 struct usb_device *udev;
680 int i, err;
681
682 DBG("zr364xx_release");
683
684 if (vdev == NULL)
685 return -ENODEV;
686 cam = video_get_drvdata(vdev);
687
688 udev = cam->udev;
689
690 mutex_lock(&cam->lock);
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300691
692 cam->users--;
693 file->private_data = NULL;
694
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300695 for (i = 0; i < 2; i++) {
696 err =
697 send_control_msg(udev, 1, init[cam->method][i].value,
698 0, init[i][cam->method].bytes,
699 init[cam->method][i].size);
700 if (err < 0) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300701 dev_err(&udev->dev, "error during release sequence\n");
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300702 goto out;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300703 }
704 }
705
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300706 /* Added some delay here, since opening/closing the camera quickly,
707 * like Ekiga does during its startup, can crash the webcam
708 */
709 mdelay(100);
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300710 err = 0;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300711
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300712out:
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300713 mutex_unlock(&cam->lock);
Antoine Jacquet33d27a42008-08-18 17:14:30 -0300714 return err;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300715}
716
717
718static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
719{
720 void *pos;
721 unsigned long start = vma->vm_start;
722 unsigned long size = vma->vm_end - vma->vm_start;
723 struct video_device *vdev = video_devdata(file);
724 struct zr364xx_camera *cam;
725
726 DBG("zr364xx_mmap: %ld\n", size);
727
728 if (vdev == NULL)
729 return -ENODEV;
730 cam = video_get_drvdata(vdev);
731
732 pos = cam->framebuf;
733 while (size > 0) {
734 if (vm_insert_page(vma, start, vmalloc_to_page(pos)))
735 return -EAGAIN;
736 start += PAGE_SIZE;
737 pos += PAGE_SIZE;
738 if (size > PAGE_SIZE)
739 size -= PAGE_SIZE;
740 else
741 size = 0;
742 }
743
744 return 0;
745}
746
747
Hans Verkuilbec43662008-12-30 06:58:20 -0300748static const struct v4l2_file_operations zr364xx_fops = {
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300749 .owner = THIS_MODULE,
750 .open = zr364xx_open,
751 .release = zr364xx_release,
752 .read = zr364xx_read,
753 .mmap = zr364xx_mmap,
754 .ioctl = video_ioctl2,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300755};
756
Hans Verkuila3998102008-07-21 02:57:38 -0300757static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300758 .vidioc_querycap = zr364xx_vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -0300759 .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
760 .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
761 .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
762 .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300763 .vidioc_enum_input = zr364xx_vidioc_enum_input,
764 .vidioc_g_input = zr364xx_vidioc_g_input,
765 .vidioc_s_input = zr364xx_vidioc_s_input,
766 .vidioc_streamon = zr364xx_vidioc_streamon,
767 .vidioc_streamoff = zr364xx_vidioc_streamoff,
768 .vidioc_queryctrl = zr364xx_vidioc_queryctrl,
769 .vidioc_g_ctrl = zr364xx_vidioc_g_ctrl,
770 .vidioc_s_ctrl = zr364xx_vidioc_s_ctrl,
771};
772
Hans Verkuila3998102008-07-21 02:57:38 -0300773static struct video_device zr364xx_template = {
Hans Verkuila3998102008-07-21 02:57:38 -0300774 .name = DRIVER_DESC,
Hans Verkuila3998102008-07-21 02:57:38 -0300775 .fops = &zr364xx_fops,
776 .ioctl_ops = &zr364xx_ioctl_ops,
777 .release = video_device_release,
778 .minor = -1,
779};
780
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300781
782
783/*******************/
784/* USB integration */
785/*******************/
786
787static int zr364xx_probe(struct usb_interface *intf,
788 const struct usb_device_id *id)
789{
790 struct usb_device *udev = interface_to_usbdev(intf);
791 struct zr364xx_camera *cam = NULL;
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300792 int err;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300793
794 DBG("probing...");
795
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300796 dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
797 dev_info(&intf->dev, "model %04x:%04x detected\n",
798 le16_to_cpu(udev->descriptor.idVendor),
799 le16_to_cpu(udev->descriptor.idProduct));
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300800
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300801 cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
802 if (cam == NULL) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300803 dev_err(&udev->dev, "cam: out of memory !\n");
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300804 return -ENOMEM;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300805 }
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300806 /* save the init method used by this camera */
807 cam->method = id->driver_info;
808
809 cam->vdev = video_device_alloc();
810 if (cam->vdev == NULL) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300811 dev_err(&udev->dev, "cam->vdev: out of memory !\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300812 kfree(cam);
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300813 return -ENOMEM;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300814 }
815 memcpy(cam->vdev, &zr364xx_template, sizeof(zr364xx_template));
816 video_set_drvdata(cam->vdev, cam);
817 if (debug)
818 cam->vdev->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
819
820 cam->udev = udev;
821
822 if ((cam->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL)) == NULL) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300823 dev_info(&udev->dev, "cam->buffer: out of memory !\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300824 video_device_release(cam->vdev);
825 kfree(cam);
826 return -ENODEV;
827 }
828
829 switch (mode) {
830 case 1:
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300831 dev_info(&udev->dev, "160x120 mode selected\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300832 cam->width = 160;
833 cam->height = 120;
834 break;
835 case 2:
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300836 dev_info(&udev->dev, "640x480 mode selected\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300837 cam->width = 640;
838 cam->height = 480;
839 break;
840 default:
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300841 dev_info(&udev->dev, "320x240 mode selected\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300842 cam->width = 320;
843 cam->height = 240;
844 break;
845 }
846
847 m0d1[0] = mode;
848 m1[2].value = 0xf000 + mode;
849 m2[1].value = 0xf000 + mode;
850 header2[437] = cam->height / 256;
851 header2[438] = cam->height % 256;
852 header2[439] = cam->width / 256;
853 header2[440] = cam->width % 256;
854
855 cam->nb = 0;
856 cam->brightness = 64;
857 mutex_init(&cam->lock);
858
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300859 err = video_register_device(cam->vdev, VFL_TYPE_GRABBER, -1);
860 if (err) {
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300861 dev_err(&udev->dev, "video_register_device failed\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300862 video_device_release(cam->vdev);
863 kfree(cam->buffer);
864 kfree(cam);
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300865 return err;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300866 }
867
868 usb_set_intfdata(intf, cam);
869
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300870 dev_info(&udev->dev, DRIVER_DESC " controlling video device %d\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -0300871 cam->vdev->num);
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300872 return 0;
873}
874
875
876static void zr364xx_disconnect(struct usb_interface *intf)
877{
878 struct zr364xx_camera *cam = usb_get_intfdata(intf);
879 usb_set_intfdata(intf, NULL);
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300880 dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300881 if (cam->vdev)
882 video_unregister_device(cam->vdev);
883 cam->vdev = NULL;
884 kfree(cam->buffer);
Figo.zhangde99d762009-06-06 06:16:21 -0300885 cam->buffer = NULL;
886 vfree(cam->framebuf);
887 cam->framebuf = NULL;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300888 kfree(cam);
Figo.zhangde99d762009-06-06 06:16:21 -0300889 cam = NULL;
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300890}
891
892
893
894/**********************/
895/* Module integration */
896/**********************/
897
898static struct usb_driver zr364xx_driver = {
899 .name = "zr364xx",
900 .probe = zr364xx_probe,
901 .disconnect = zr364xx_disconnect,
902 .id_table = device_table
903};
904
905
906static int __init zr364xx_init(void)
907{
908 int retval;
Akinobu Mita783aa8f2007-05-20 09:12:10 -0300909 retval = usb_register(&zr364xx_driver);
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300910 if (retval)
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300911 printk(KERN_ERR KBUILD_MODNAME ": usb_register failed!\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300912 else
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300913 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300914 return retval;
915}
916
917
918static void __exit zr364xx_exit(void)
919{
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -0300920 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC " module unloaded\n");
Antoine Jacquetb7eee612007-04-27 12:30:59 -0300921 usb_deregister(&zr364xx_driver);
922}
923
924
925module_init(zr364xx_init);
926module_exit(zr364xx_exit);
927
928MODULE_AUTHOR(DRIVER_AUTHOR);
929MODULE_DESCRIPTION(DRIVER_DESC);
930MODULE_LICENSE("GPL");