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