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