V4L/DVB (8717): gspca: Frame buffer too small for small resolutions (sonixj and t613).
[deliverable/linux.git] / drivers / media / video / gspca / gspca.c
CommitLineData
63eb9546
JFM
1/*
2 * Main USB camera driver
3 *
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#define MODULE_NAME "gspca"
22
23#include <linux/init.h>
24#include <linux/fs.h>
25#include <linux/vmalloc.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/mm.h>
29#include <linux/string.h>
30#include <linux/pagemap.h>
956e42d2 31#include <linux/io.h>
63eb9546 32#include <asm/page.h>
956e42d2 33#include <linux/uaccess.h>
63eb9546 34#include <linux/jiffies.h>
35ea11ff 35#include <media/v4l2-ioctl.h>
63eb9546
JFM
36
37#include "gspca.h"
38
d43fa32f 39/* global values */
ac0d6df6 40#define DEF_NURBS 2 /* default number of URBs */
d43fa32f 41
63eb9546
JFM
42MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44MODULE_LICENSE("GPL");
45
bb64e86c 46#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 2, 0)
63eb9546
JFM
47
48static int video_nr = -1;
49
335b3f88 50#ifdef GSPCA_DEBUG
63eb9546
JFM
51int gspca_debug = D_ERR | D_PROBE;
52EXPORT_SYMBOL(gspca_debug);
53
54static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
55{
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
58 txt,
59 pixfmt & 0xff,
60 (pixfmt >> 8) & 0xff,
61 (pixfmt >> 16) & 0xff,
62 pixfmt >> 24,
63 w, h);
64 } else {
65 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
66 txt,
67 pixfmt,
68 w, h);
69 }
70}
71#else
72#define PDEBUG_MODE(txt, pixfmt, w, h)
73#endif
74
d43fa32f
JFM
75/* specific memory types - !! should different from V4L2_MEMORY_xxx */
76#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77#define GSPCA_MEMORY_READ 7
78
d43fa32f 79#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
d43fa32f 80
63eb9546
JFM
81/*
82 * VMA operations.
83 */
84static void gspca_vm_open(struct vm_area_struct *vma)
85{
86 struct gspca_frame *frame = vma->vm_private_data;
87
88 frame->vma_use_count++;
89 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
90}
91
92static void gspca_vm_close(struct vm_area_struct *vma)
93{
94 struct gspca_frame *frame = vma->vm_private_data;
95
96 if (--frame->vma_use_count <= 0)
97 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
98}
99
100static struct vm_operations_struct gspca_vm_ops = {
101 .open = gspca_vm_open,
102 .close = gspca_vm_close,
103};
104
105/*
d43fa32f 106 * fill a video frame from an URB and resubmit
63eb9546 107 */
d43fa32f
JFM
108static void fill_frame(struct gspca_dev *gspca_dev,
109 struct urb *urb)
63eb9546 110{
63eb9546 111 struct gspca_frame *frame;
6a7eba24 112 __u8 *data; /* address of data in the iso message */
63eb9546
JFM
113 int i, j, len, st;
114 cam_pkt_op pkt_scan;
115
6a7eba24
JFM
116 if (urb->status != 0) {
117 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
118 return; /* disconnection ? */
119 }
63eb9546
JFM
120 pkt_scan = gspca_dev->sd_desc->pkt_scan;
121 for (i = 0; i < urb->number_of_packets; i++) {
122
123 /* check the availability of the frame buffer */
124 j = gspca_dev->fr_i;
125 j = gspca_dev->fr_queue[j];
126 frame = &gspca_dev->frame[j];
d43fa32f 127 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
63eb9546
JFM
128 != V4L2_BUF_FLAG_QUEUED) {
129 gspca_dev->last_packet_type = DISCARD_PACKET;
130 break;
131 }
132
133 /* check the packet status and length */
134 len = urb->iso_frame_desc[i].actual_length;
6a7eba24
JFM
135 if (len == 0)
136 continue;
63eb9546
JFM
137 st = urb->iso_frame_desc[i].status;
138 if (st) {
6a7eba24
JFM
139 PDEBUG(D_ERR,
140 "ISOC data error: [%d] len=%d, status=%d",
63eb9546
JFM
141 i, len, st);
142 gspca_dev->last_packet_type = DISCARD_PACKET;
143 continue;
144 }
63eb9546
JFM
145
146 /* let the packet be analyzed by the subdriver */
147 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
148 i, urb->iso_frame_desc[i].offset, len);
6a7eba24 149 data = (__u8 *) urb->transfer_buffer
63eb9546
JFM
150 + urb->iso_frame_desc[i].offset;
151 pkt_scan(gspca_dev, frame, data, len);
152 }
153
154 /* resubmit the URB */
155 urb->status = 0;
156 st = usb_submit_urb(urb, GFP_ATOMIC);
157 if (st < 0)
158 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
159}
160
d43fa32f
JFM
161/*
162 * ISOC message interrupt from the USB device
163 *
ac0d6df6 164 * Analyse each packet and call the subdriver for copy to the frame buffer.
d43fa32f 165 */
ac0d6df6 166static void isoc_irq(struct urb *urb
6a7eba24 167)
d43fa32f
JFM
168{
169 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
170
3647fea8 171 PDEBUG(D_PACK, "isoc irq");
d43fa32f
JFM
172 if (!gspca_dev->streaming)
173 return;
174 fill_frame(gspca_dev, urb);
175}
176
63eb9546
JFM
177/*
178 * add data to the current frame
179 *
ac0d6df6
JFM
180 * This function is called by the subdrivers at interrupt level.
181 *
63eb9546
JFM
182 * To build a frame, these ones must add
183 * - one FIRST_PACKET
184 * - 0 or many INTER_PACKETs
185 * - one LAST_PACKET
186 * DISCARD_PACKET invalidates the whole frame.
187 * On LAST_PACKET, a new frame is returned.
188 */
189struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
190 int packet_type,
191 struct gspca_frame *frame,
a5ae2062 192 const __u8 *data,
63eb9546
JFM
193 int len)
194{
195 int i, j;
196
e2997a72 197 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
63eb9546
JFM
198
199 /* when start of a new frame, if the current frame buffer
200 * is not queued, discard the whole frame */
201 if (packet_type == FIRST_PACKET) {
d43fa32f
JFM
202 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
203 != V4L2_BUF_FLAG_QUEUED) {
63eb9546
JFM
204 gspca_dev->last_packet_type = DISCARD_PACKET;
205 return frame;
206 }
207 frame->data_end = frame->data;
208 jiffies_to_timeval(get_jiffies_64(),
209 &frame->v4l2_buf.timestamp);
210 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
d43fa32f 211 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
06ca78fa
JFM
212 if (packet_type == LAST_PACKET)
213 gspca_dev->last_packet_type = packet_type;
63eb9546 214 return frame;
d43fa32f 215 }
63eb9546 216
d43fa32f 217 /* append the packet to the frame buffer */
63eb9546
JFM
218 if (len > 0) {
219 if (frame->data_end - frame->data + len
220 > frame->v4l2_buf.length) {
a9e11134 221 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
63eb9546
JFM
222 frame->data_end - frame->data + len,
223 frame->v4l2_buf.length);
224 packet_type = DISCARD_PACKET;
225 } else {
ac0d6df6 226 memcpy(frame->data_end, data, len);
63eb9546
JFM
227 frame->data_end += len;
228 }
229 }
230 gspca_dev->last_packet_type = packet_type;
231
232 /* if last packet, wake the application and advance in the queue */
233 if (packet_type == LAST_PACKET) {
234 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
235 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
236 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
237 atomic_inc(&gspca_dev->nevent);
238 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
d43fa32f
JFM
239 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
240 gspca_dev->fr_i = i;
63eb9546
JFM
241 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
242 frame->v4l2_buf.bytesused,
243 gspca_dev->fr_q,
244 i,
245 gspca_dev->fr_o);
246 j = gspca_dev->fr_queue[i];
247 frame = &gspca_dev->frame[j];
63eb9546
JFM
248 }
249 return frame;
250}
251EXPORT_SYMBOL(gspca_frame_add);
252
253static int gspca_is_compressed(__u32 format)
254{
255 switch (format) {
256 case V4L2_PIX_FMT_MJPEG:
257 case V4L2_PIX_FMT_JPEG:
50a871fe 258 case V4L2_PIX_FMT_SPCA561:
ab8f12cf 259 case V4L2_PIX_FMT_PAC207:
63eb9546
JFM
260 return 1;
261 }
262 return 0;
263}
264
265static void *rvmalloc(unsigned long size)
266{
267 void *mem;
268 unsigned long adr;
269
d43fa32f 270/* size = PAGE_ALIGN(size); (already done) */
63eb9546 271 mem = vmalloc_32(size);
efab8211 272 if (mem != NULL) {
63eb9546
JFM
273 adr = (unsigned long) mem;
274 while ((long) size > 0) {
275 SetPageReserved(vmalloc_to_page((void *) adr));
276 adr += PAGE_SIZE;
277 size -= PAGE_SIZE;
278 }
279 }
280 return mem;
281}
282
ac0d6df6 283static void rvfree(void *mem, long size)
63eb9546
JFM
284{
285 unsigned long adr;
286
63eb9546 287 adr = (unsigned long) mem;
ac0d6df6 288 while (size > 0) {
63eb9546
JFM
289 ClearPageReserved(vmalloc_to_page((void *) adr));
290 adr += PAGE_SIZE;
291 size -= PAGE_SIZE;
292 }
293 vfree(mem);
294}
295
296static int frame_alloc(struct gspca_dev *gspca_dev,
d43fa32f 297 unsigned int count)
63eb9546 298{
d43fa32f
JFM
299 struct gspca_frame *frame;
300 unsigned int frsz;
301 int i;
63eb9546 302
c2446b3e
JFM
303 i = gspca_dev->curr_mode;
304 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
63eb9546 305 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
63eb9546 306 frsz = PAGE_ALIGN(frsz);
63eb9546 307 gspca_dev->frsz = frsz;
c2446b3e
JFM
308 if (count > GSPCA_MAX_FRAMES)
309 count = GSPCA_MAX_FRAMES;
ac0d6df6
JFM
310 gspca_dev->frbuf = rvmalloc(frsz * count);
311 if (!gspca_dev->frbuf) {
312 err("frame alloc failed");
313 return -ENOMEM;
63eb9546
JFM
314 }
315 gspca_dev->nframes = count;
316 for (i = 0; i < count; i++) {
d43fa32f
JFM
317 frame = &gspca_dev->frame[i];
318 frame->v4l2_buf.index = i;
319 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
320 frame->v4l2_buf.flags = 0;
321 frame->v4l2_buf.field = V4L2_FIELD_NONE;
322 frame->v4l2_buf.length = frsz;
323 frame->v4l2_buf.memory = gspca_dev->memory;
324 frame->v4l2_buf.sequence = 0;
ac0d6df6 325 frame->data = frame->data_end =
d43fa32f 326 gspca_dev->frbuf + i * frsz;
ac0d6df6 327 frame->v4l2_buf.m.offset = i * frsz;
63eb9546
JFM
328 }
329 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
330 gspca_dev->last_packet_type = DISCARD_PACKET;
331 gspca_dev->sequence = 0;
332 atomic_set(&gspca_dev->nevent, 0);
d43fa32f 333 return 0;
63eb9546
JFM
334}
335
336static void frame_free(struct gspca_dev *gspca_dev)
337{
338 int i;
339
340 PDEBUG(D_STREAM, "frame free");
efab8211 341 if (gspca_dev->frbuf != NULL) {
63eb9546
JFM
342 rvfree(gspca_dev->frbuf,
343 gspca_dev->nframes * gspca_dev->frsz);
344 gspca_dev->frbuf = NULL;
345 for (i = 0; i < gspca_dev->nframes; i++)
346 gspca_dev->frame[i].data = NULL;
347 }
348 gspca_dev->nframes = 0;
349}
350
d43fa32f 351static void destroy_urbs(struct gspca_dev *gspca_dev)
63eb9546
JFM
352{
353 struct urb *urb;
354 unsigned int i;
355
356 PDEBUG(D_STREAM, "kill transfer");
d43fa32f 357 for (i = 0; i < MAX_NURBS; ++i) {
4aa0d037 358 urb = gspca_dev->urb[i];
63eb9546 359 if (urb == NULL)
d43fa32f 360 break;
63eb9546 361
4aa0d037 362 gspca_dev->urb[i] = NULL;
63eb9546 363 usb_kill_urb(urb);
efab8211 364 if (urb->transfer_buffer != NULL)
63eb9546
JFM
365 usb_buffer_free(gspca_dev->dev,
366 urb->transfer_buffer_length,
4aa0d037 367 urb->transfer_buffer,
63eb9546 368 urb->transfer_dma);
63eb9546
JFM
369 usb_free_urb(urb);
370 }
63eb9546
JFM
371}
372
373/*
374 * search an input isochronous endpoint in an alternate setting
375 */
376static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
377 __u8 epaddr)
378{
379 struct usb_host_endpoint *ep;
380 int i, attr;
381
382 epaddr |= USB_DIR_IN;
383 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
384 ep = &alt->endpoint[i];
385 if (ep->desc.bEndpointAddress == epaddr) {
386 attr = ep->desc.bmAttributes
387 & USB_ENDPOINT_XFERTYPE_MASK;
388 if (attr == USB_ENDPOINT_XFER_ISOC)
389 return ep;
390 break;
391 }
392 }
393 return NULL;
394}
395
396/*
397 * search an input isochronous endpoint
398 *
399 * The endpoint is defined by the subdriver.
400 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
401 * This routine may be called many times when the bandwidth is too small
402 * (the bandwidth is checked on urb submit).
403 */
903e10aa 404static struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
63eb9546
JFM
405{
406 struct usb_interface *intf;
407 struct usb_host_endpoint *ep;
408 int i, ret;
409
410 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
6a7eba24 411 ep = NULL;
63eb9546
JFM
412 i = gspca_dev->alt; /* previous alt setting */
413 while (--i > 0) { /* alt 0 is unusable */
414 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
415 if (ep)
416 break;
417 }
6a7eba24 418 if (ep == NULL) {
63eb9546
JFM
419 err("no ISOC endpoint found");
420 return NULL;
421 }
422 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
423 i, ep->desc.bEndpointAddress);
424 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
425 if (ret < 0) {
426 err("set interface err %d", ret);
427 return NULL;
428 }
d43fa32f 429 gspca_dev->alt = i; /* memorize the current alt setting */
63eb9546
JFM
430 return ep;
431}
432
433/*
434 * create the isochronous URBs
435 */
436static int create_urbs(struct gspca_dev *gspca_dev,
437 struct usb_host_endpoint *ep)
438{
439 struct urb *urb;
d43fa32f 440 int n, nurbs, i, psize, npkt, bsize;
63eb9546
JFM
441
442 /* calculate the packet size and the number of packets */
63eb9546 443 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
d43fa32f 444
63eb9546
JFM
445 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
446 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
447 npkt = ISO_MAX_SIZE / psize;
448 if (npkt > ISO_MAX_PKT)
449 npkt = ISO_MAX_PKT;
450 bsize = psize * npkt;
63eb9546
JFM
451 PDEBUG(D_STREAM,
452 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
ac0d6df6 453 nurbs = DEF_NURBS;
6a7eba24 454 gspca_dev->nurbs = nurbs;
d43fa32f 455 for (n = 0; n < nurbs; n++) {
63eb9546
JFM
456 urb = usb_alloc_urb(npkt, GFP_KERNEL);
457 if (!urb) {
458 err("usb_alloc_urb failed");
459 return -ENOMEM;
460 }
4aa0d037 461 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
63eb9546
JFM
462 bsize,
463 GFP_KERNEL,
464 &urb->transfer_dma);
465
4aa0d037 466 if (urb->transfer_buffer == NULL) {
63eb9546 467 usb_free_urb(urb);
d43fa32f 468 destroy_urbs(gspca_dev);
63eb9546
JFM
469 err("usb_buffer_urb failed");
470 return -ENOMEM;
471 }
4aa0d037 472 gspca_dev->urb[n] = urb;
63eb9546
JFM
473 urb->dev = gspca_dev->dev;
474 urb->context = gspca_dev;
475 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
476 ep->desc.bEndpointAddress);
4aa0d037
JFM
477 urb->transfer_flags = URB_ISO_ASAP
478 | URB_NO_TRANSFER_DMA_MAP;
63eb9546 479 urb->interval = ep->desc.bInterval;
ac0d6df6 480 urb->complete = isoc_irq;
63eb9546
JFM
481 urb->number_of_packets = npkt;
482 urb->transfer_buffer_length = bsize;
483 for (i = 0; i < npkt; i++) {
484 urb->iso_frame_desc[i].length = psize;
485 urb->iso_frame_desc[i].offset = psize * i;
486 }
487 }
488 return 0;
489}
490
491/*
492 * start the USB transfer
493 */
494static int gspca_init_transfer(struct gspca_dev *gspca_dev)
495{
63eb9546
JFM
496 struct usb_host_endpoint *ep;
497 int n, ret;
498
e2997a72
HG
499 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
500 return -ERESTARTSYS;
63eb9546 501
d43fa32f
JFM
502 /* set the higher alternate setting and
503 * loop until urb submit succeeds */
504 gspca_dev->alt = gspca_dev->nbalt;
63eb9546 505 for (;;) {
d43fa32f 506 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
63eb9546
JFM
507 ep = get_isoc_ep(gspca_dev);
508 if (ep == NULL) {
509 ret = -EIO;
510 goto out;
511 }
512 ret = create_urbs(gspca_dev, ep);
513 if (ret < 0)
514 goto out;
515
516 /* start the cam */
517 gspca_dev->sd_desc->start(gspca_dev);
518 gspca_dev->streaming = 1;
519 atomic_set(&gspca_dev->nevent, 0);
520
521 /* submit the URBs */
d43fa32f 522 for (n = 0; n < gspca_dev->nurbs; n++) {
4aa0d037 523 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
63eb9546
JFM
524 if (ret < 0) {
525 PDEBUG(D_ERR|D_STREAM,
526 "usb_submit_urb [%d] err %d", n, ret);
e2997a72 527 gspca_dev->streaming = 0;
d43fa32f 528 destroy_urbs(gspca_dev);
63eb9546
JFM
529 if (ret == -ENOSPC)
530 break; /* try the previous alt */
531 goto out;
532 }
533 }
534 if (ret >= 0)
535 break;
536 }
537out:
538 mutex_unlock(&gspca_dev->usb_lock);
539 return ret;
540}
541
542static int gspca_set_alt0(struct gspca_dev *gspca_dev)
543{
544 int ret;
545
546 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
547 if (ret < 0)
548 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
549 return ret;
550}
551
e2997a72 552/* Note both the queue and the usb lock should be hold when calling this */
63eb9546
JFM
553static void gspca_stream_off(struct gspca_dev *gspca_dev)
554{
63eb9546 555 gspca_dev->streaming = 0;
d43fa32f 556 atomic_set(&gspca_dev->nevent, 0);
63eb9546
JFM
557 if (gspca_dev->present) {
558 gspca_dev->sd_desc->stopN(gspca_dev);
d43fa32f 559 destroy_urbs(gspca_dev);
63eb9546
JFM
560 gspca_set_alt0(gspca_dev);
561 gspca_dev->sd_desc->stop0(gspca_dev);
562 PDEBUG(D_STREAM, "stream off OK");
63eb9546 563 }
63eb9546
JFM
564}
565
d43fa32f 566static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
63eb9546
JFM
567{
568 int i;
569
570 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
571 gspca_dev->curr_mode = i;
572 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
573 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
c2446b3e 574 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
63eb9546
JFM
575}
576
577static int wxh_to_mode(struct gspca_dev *gspca_dev,
578 int width, int height)
579{
580 int i;
581
d43fa32f
JFM
582 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
583 if (width >= gspca_dev->cam.cam_mode[i].width
584 && height >= gspca_dev->cam.cam_mode[i].height)
63eb9546
JFM
585 break;
586 }
63eb9546
JFM
587 return i;
588}
589
63eb9546
JFM
590/*
591 * search a mode with the right pixel format
592 */
593static int gspca_get_mode(struct gspca_dev *gspca_dev,
594 int mode,
595 int pixfmt)
596{
597 int modeU, modeD;
598
599 modeU = modeD = mode;
600 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
601 if (--modeD >= 0) {
c2446b3e
JFM
602 if (gspca_dev->cam.cam_mode[modeD].pixelformat
603 == pixfmt)
63eb9546
JFM
604 return modeD;
605 }
606 if (++modeU < gspca_dev->cam.nmodes) {
c2446b3e
JFM
607 if (gspca_dev->cam.cam_mode[modeU].pixelformat
608 == pixfmt)
63eb9546
JFM
609 return modeU;
610 }
611 }
612 return -EINVAL;
613}
614
e078770a 615static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
63eb9546
JFM
616 struct v4l2_fmtdesc *fmtdesc)
617{
618 struct gspca_dev *gspca_dev = priv;
6a7eba24 619 int i, j, index;
63eb9546
JFM
620 __u32 fmt_tb[8];
621
63eb9546
JFM
622 /* give an index to each format */
623 index = 0;
624 j = 0;
625 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
c2446b3e 626 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
63eb9546
JFM
627 j = 0;
628 for (;;) {
629 if (fmt_tb[j] == fmt_tb[index])
630 break;
631 j++;
632 }
633 if (j == index) {
634 if (fmtdesc->index == index)
635 break; /* new format */
636 index++;
637 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
638 return -EINVAL;
639 }
640 }
641 if (i < 0)
642 return -EINVAL; /* no more format */
643
63eb9546
JFM
644 fmtdesc->pixelformat = fmt_tb[index];
645 if (gspca_is_compressed(fmt_tb[index]))
646 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
d43fa32f 647 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
63eb9546
JFM
648 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
649 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
650 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
651 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
652 fmtdesc->description[4] = '\0';
653 return 0;
654}
655
e078770a 656static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
63eb9546
JFM
657 struct v4l2_format *fmt)
658{
659 struct gspca_dev *gspca_dev = priv;
c2446b3e 660 int mode;
63eb9546 661
6a7eba24
JFM
662 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
663 return -EINVAL;
c2446b3e
JFM
664 mode = gspca_dev->curr_mode;
665 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
666 sizeof fmt->fmt.pix);
63eb9546
JFM
667 return 0;
668}
669
e078770a 670static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
63eb9546
JFM
671 struct v4l2_format *fmt)
672{
24c530b4 673 int w, h, mode, mode2;
63eb9546 674
6a7eba24
JFM
675 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
676 return -EINVAL;
4aa0d037
JFM
677 w = fmt->fmt.pix.width;
678 h = fmt->fmt.pix.height;
d43fa32f 679
335b3f88 680#ifdef GSPCA_DEBUG
63eb9546
JFM
681 if (gspca_debug & D_CONF)
682 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
683#endif
684 /* search the closest mode for width and height */
685 mode = wxh_to_mode(gspca_dev, w, h);
686
687 /* OK if right palette */
c2446b3e
JFM
688 if (gspca_dev->cam.cam_mode[mode].pixelformat
689 != fmt->fmt.pix.pixelformat) {
63eb9546
JFM
690
691 /* else, search the closest mode with the same pixel format */
692 mode2 = gspca_get_mode(gspca_dev, mode,
693 fmt->fmt.pix.pixelformat);
c2446b3e 694 if (mode2 >= 0)
63eb9546 695 mode = mode2;
c2446b3e
JFM
696/* else
697 ; * no chance, return this mode */
63eb9546 698 }
c2446b3e
JFM
699 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
700 sizeof fmt->fmt.pix);
63eb9546
JFM
701 return mode; /* used when s_fmt */
702}
703
e078770a 704static int vidioc_try_fmt_vid_cap(struct file *file,
63eb9546
JFM
705 void *priv,
706 struct v4l2_format *fmt)
707{
4aa0d037 708 struct gspca_dev *gspca_dev = priv;
63eb9546
JFM
709 int ret;
710
e078770a 711 ret = try_fmt_vid_cap(gspca_dev, fmt);
63eb9546
JFM
712 if (ret < 0)
713 return ret;
714 return 0;
715}
716
e078770a 717static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
63eb9546
JFM
718 struct v4l2_format *fmt)
719{
720 struct gspca_dev *gspca_dev = priv;
d43fa32f 721 int ret;
63eb9546 722
e2997a72
HG
723 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
724 return -ERESTARTSYS;
d43fa32f 725
e078770a 726 ret = try_fmt_vid_cap(gspca_dev, fmt);
63eb9546
JFM
727 if (ret < 0)
728 goto out;
729
d43fa32f
JFM
730 if (gspca_dev->nframes != 0
731 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
732 ret = -EINVAL;
733 goto out;
734 }
735
50a871fe
JFM
736 if (ret == gspca_dev->curr_mode) {
737 ret = 0;
63eb9546 738 goto out; /* same mode */
50a871fe 739 }
d43fa32f
JFM
740
741 if (gspca_dev->streaming) {
742 ret = -EBUSY;
743 goto out;
e2997a72 744 }
4aa0d037
JFM
745 gspca_dev->width = fmt->fmt.pix.width;
746 gspca_dev->height = fmt->fmt.pix.height;
63eb9546
JFM
747 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
748 gspca_dev->curr_mode = ret;
d43fa32f 749
d43fa32f 750 ret = 0;
63eb9546
JFM
751out:
752 mutex_unlock(&gspca_dev->queue_lock);
753 return ret;
754}
755
756static int dev_open(struct inode *inode, struct file *file)
757{
758 struct gspca_dev *gspca_dev;
759 int ret;
760
d43fa32f 761 PDEBUG(D_STREAM, "%s open", current->comm);
63eb9546 762 gspca_dev = (struct gspca_dev *) video_devdata(file);
e2997a72
HG
763 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
764 return -ERESTARTSYS;
63eb9546
JFM
765 if (!gspca_dev->present) {
766 ret = -ENODEV;
767 goto out;
768 }
769
770 /* if not done yet, initialize the sensor */
771 if (gspca_dev->users == 0) {
e2997a72
HG
772 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
773 ret = -ERESTARTSYS;
63eb9546 774 goto out;
e2997a72 775 }
63eb9546
JFM
776 ret = gspca_dev->sd_desc->open(gspca_dev);
777 mutex_unlock(&gspca_dev->usb_lock);
778 if (ret != 0) {
779 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
780 goto out;
781 }
e2997a72 782 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
63eb9546
JFM
783 ret = -EBUSY;
784 goto out;
785 }
786 gspca_dev->users++;
787 file->private_data = gspca_dev;
335b3f88 788#ifdef GSPCA_DEBUG
63eb9546 789 /* activate the v4l2 debug */
d43fa32f 790 if (gspca_debug & D_V4L2)
63eb9546
JFM
791 gspca_dev->vdev.debug |= 3;
792 else
793 gspca_dev->vdev.debug &= ~3;
794#endif
795out:
796 mutex_unlock(&gspca_dev->queue_lock);
797 if (ret != 0)
798 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
799 else
d43fa32f 800 PDEBUG(D_STREAM, "open done");
63eb9546
JFM
801 return ret;
802}
803
804static int dev_close(struct inode *inode, struct file *file)
805{
806 struct gspca_dev *gspca_dev = file->private_data;
807
d43fa32f 808 PDEBUG(D_STREAM, "%s close", current->comm);
e2997a72
HG
809 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
810 return -ERESTARTSYS;
811 gspca_dev->users--;
e2997a72 812
5b77ae77 813 /* if the file did the capture, free the streaming resources */
4aa0d037
JFM
814 if (gspca_dev->capt_file == file) {
815 mutex_lock(&gspca_dev->usb_lock);
816 if (gspca_dev->streaming)
817 gspca_stream_off(gspca_dev);
818 gspca_dev->sd_desc->close(gspca_dev);
819 mutex_unlock(&gspca_dev->usb_lock);
820 frame_free(gspca_dev);
efab8211 821 gspca_dev->capt_file = NULL;
d43fa32f 822 gspca_dev->memory = GSPCA_MEMORY_NO;
4aa0d037 823 }
d43fa32f 824 file->private_data = NULL;
63eb9546 825 mutex_unlock(&gspca_dev->queue_lock);
d43fa32f 826 PDEBUG(D_STREAM, "close done");
63eb9546
JFM
827 return 0;
828}
829
830static int vidioc_querycap(struct file *file, void *priv,
831 struct v4l2_capability *cap)
832{
833 struct gspca_dev *gspca_dev = priv;
834
63eb9546
JFM
835 memset(cap, 0, sizeof *cap);
836 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
c6edaf16
JFM
837/* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
838 if (gspca_dev->dev->product != NULL) {
839 strncpy(cap->card, gspca_dev->dev->product,
840 sizeof cap->card);
841 } else {
842 snprintf(cap->card, sizeof cap->card,
843 "USB Camera (%04x:%04x)",
844 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
845 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
846 }
63eb9546
JFM
847 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
848 sizeof cap->bus_info);
849 cap->version = DRIVER_VERSION_NUMBER;
850 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
851 | V4L2_CAP_STREAMING
852 | V4L2_CAP_READWRITE;
853 return 0;
854}
855
6a7eba24 856/* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
63eb9546
JFM
857static int vidioc_queryctrl(struct file *file, void *priv,
858 struct v4l2_queryctrl *q_ctrl)
859{
860 struct gspca_dev *gspca_dev = priv;
861 int i;
6a7eba24
JFM
862 u32 id;
863
864 id = q_ctrl->id;
865 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
866 id &= V4L2_CTRL_ID_MASK;
867 id++;
868 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
869 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
870 memcpy(q_ctrl,
871 &gspca_dev->sd_desc->ctrls[i].qctrl,
872 sizeof *q_ctrl);
873 return 0;
874 }
875 }
876 return -EINVAL;
877 }
63eb9546 878 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
6a7eba24 879 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
63eb9546
JFM
880 memcpy(q_ctrl,
881 &gspca_dev->sd_desc->ctrls[i].qctrl,
882 sizeof *q_ctrl);
883 return 0;
884 }
885 }
6a7eba24
JFM
886 if (id >= V4L2_CID_BASE
887 && id <= V4L2_CID_LASTP1) {
63eb9546
JFM
888 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
889 return 0;
890 }
891 return -EINVAL;
892}
893
894static int vidioc_s_ctrl(struct file *file, void *priv,
895 struct v4l2_control *ctrl)
896{
897 struct gspca_dev *gspca_dev = priv;
a5ae2062 898 const struct ctrl *ctrls;
63eb9546
JFM
899 int i, ret;
900
63eb9546
JFM
901 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
902 i < gspca_dev->sd_desc->nctrls;
903 i++, ctrls++) {
904 if (ctrl->id != ctrls->qctrl.id)
905 continue;
906 if (ctrl->value < ctrls->qctrl.minimum
fcf5cb24 907 || ctrl->value > ctrls->qctrl.maximum)
63eb9546
JFM
908 return -ERANGE;
909 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
e2997a72
HG
910 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
911 return -ERESTARTSYS;
63eb9546
JFM
912 ret = ctrls->set(gspca_dev, ctrl->value);
913 mutex_unlock(&gspca_dev->usb_lock);
914 return ret;
915 }
916 return -EINVAL;
917}
918
919static int vidioc_g_ctrl(struct file *file, void *priv,
920 struct v4l2_control *ctrl)
921{
922 struct gspca_dev *gspca_dev = priv;
923
a5ae2062 924 const struct ctrl *ctrls;
63eb9546
JFM
925 int i, ret;
926
927 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
928 i < gspca_dev->sd_desc->nctrls;
929 i++, ctrls++) {
930 if (ctrl->id != ctrls->qctrl.id)
931 continue;
e2997a72
HG
932 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
933 return -ERESTARTSYS;
63eb9546
JFM
934 ret = ctrls->get(gspca_dev, &ctrl->value);
935 mutex_unlock(&gspca_dev->usb_lock);
936 return ret;
937 }
938 return -EINVAL;
939}
940
941static int vidioc_querymenu(struct file *file, void *priv,
942 struct v4l2_querymenu *qmenu)
943{
944 struct gspca_dev *gspca_dev = priv;
945
946 if (!gspca_dev->sd_desc->querymenu)
947 return -EINVAL;
948 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
949}
950
951static int vidioc_enum_input(struct file *file, void *priv,
952 struct v4l2_input *input)
953{
954 struct gspca_dev *gspca_dev = priv;
955
956 if (input->index != 0)
957 return -EINVAL;
958 memset(input, 0, sizeof *input);
959 input->type = V4L2_INPUT_TYPE_CAMERA;
960 strncpy(input->name, gspca_dev->sd_desc->name,
961 sizeof input->name);
962 return 0;
963}
964
965static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
966{
967 *i = 0;
968 return 0;
969}
970
971static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
972{
973 if (i > 0)
974 return -EINVAL;
975 return (0);
976}
977
978static int vidioc_reqbufs(struct file *file, void *priv,
979 struct v4l2_requestbuffers *rb)
980{
981 struct gspca_dev *gspca_dev = priv;
d43fa32f 982 int i, ret = 0;
63eb9546 983
63eb9546
JFM
984 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
985 return -EINVAL;
d43fa32f 986 switch (rb->memory) {
5b77ae77 987 case GSPCA_MEMORY_READ: /* (internal call) */
d43fa32f 988 case V4L2_MEMORY_MMAP:
d43fa32f 989 case V4L2_MEMORY_USERPTR:
6a7eba24 990 break;
d43fa32f 991 default:
63eb9546 992 return -EINVAL;
d43fa32f 993 }
e2997a72
HG
994 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
995 return -ERESTARTSYS;
d43fa32f 996
5b77ae77
JFM
997 if (gspca_dev->memory != GSPCA_MEMORY_NO
998 && gspca_dev->memory != rb->memory) {
4aa0d037
JFM
999 ret = -EBUSY;
1000 goto out;
1001 }
d43fa32f 1002
5b77ae77
JFM
1003 /* only one file may do the capture */
1004 if (gspca_dev->capt_file != NULL
1005 && gspca_dev->capt_file != file) {
1006 ret = -EBUSY;
1007 goto out;
1008 }
1009
1010 /* if allocated, the buffers must not be mapped */
1011 for (i = 0; i < gspca_dev->nframes; i++) {
1012 if (gspca_dev->frame[i].vma_use_count) {
ac0d6df6
JFM
1013 ret = -EBUSY;
1014 goto out;
1015 }
5b77ae77
JFM
1016 }
1017
1018 /* stop streaming */
1019 if (gspca_dev->streaming) {
1020 mutex_lock(&gspca_dev->usb_lock);
1021 gspca_stream_off(gspca_dev);
1022 mutex_unlock(&gspca_dev->usb_lock);
1023 }
1024
1025 /* free the previous allocated buffers, if any */
1026 if (gspca_dev->nframes != 0) {
1027 frame_free(gspca_dev);
1028 gspca_dev->capt_file = NULL;
1029 }
1030 if (rb->count == 0) /* unrequest */
1031 goto out;
1032 gspca_dev->memory = rb->memory;
1033 ret = frame_alloc(gspca_dev, rb->count);
1034 if (ret == 0) {
1035 rb->count = gspca_dev->nframes;
1036 gspca_dev->capt_file = file;
4aa0d037
JFM
1037 }
1038out:
63eb9546
JFM
1039 mutex_unlock(&gspca_dev->queue_lock);
1040 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1041 return ret;
1042}
1043
1044static int vidioc_querybuf(struct file *file, void *priv,
1045 struct v4l2_buffer *v4l2_buf)
1046{
1047 struct gspca_dev *gspca_dev = priv;
1048 struct gspca_frame *frame;
1049
63eb9546
JFM
1050 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1051 || v4l2_buf->index < 0
1052 || v4l2_buf->index >= gspca_dev->nframes)
1053 return -EINVAL;
1054
1055 frame = &gspca_dev->frame[v4l2_buf->index];
1056 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1057 return 0;
1058}
1059
1060static int vidioc_streamon(struct file *file, void *priv,
1061 enum v4l2_buf_type buf_type)
1062{
1063 struct gspca_dev *gspca_dev = priv;
1064 int ret;
1065
63eb9546
JFM
1066 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1067 return -EINVAL;
e2997a72
HG
1068 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1069 return -ERESTARTSYS;
63eb9546
JFM
1070 if (!gspca_dev->present) {
1071 ret = -ENODEV;
1072 goto out;
1073 }
1074 if (gspca_dev->nframes == 0) {
1075 ret = -EINVAL;
1076 goto out;
1077 }
1078 if (!gspca_dev->streaming) {
1079 ret = gspca_init_transfer(gspca_dev);
1080 if (ret < 0)
1081 goto out;
1082 }
335b3f88 1083#ifdef GSPCA_DEBUG
63eb9546
JFM
1084 if (gspca_debug & D_STREAM) {
1085 PDEBUG_MODE("stream on OK",
1086 gspca_dev->pixfmt,
1087 gspca_dev->width,
1088 gspca_dev->height);
1089 }
1090#endif
e2997a72 1091 ret = 0;
63eb9546
JFM
1092out:
1093 mutex_unlock(&gspca_dev->queue_lock);
1094 return ret;
1095}
1096
1097static int vidioc_streamoff(struct file *file, void *priv,
1098 enum v4l2_buf_type buf_type)
1099{
1100 struct gspca_dev *gspca_dev = priv;
5b77ae77 1101 int i, ret;
63eb9546 1102
63eb9546
JFM
1103 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1104 return -EINVAL;
4aa0d037
JFM
1105 if (!gspca_dev->streaming)
1106 return 0;
1107 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1108 return -ERESTARTSYS;
5b77ae77
JFM
1109
1110 /* stop streaming */
4aa0d037
JFM
1111 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1112 ret = -ERESTARTSYS;
1113 goto out;
63eb9546 1114 }
4aa0d037 1115 gspca_stream_off(gspca_dev);
4aa0d037 1116 mutex_unlock(&gspca_dev->usb_lock);
5b77ae77
JFM
1117
1118 /* empty the application queues */
1119 for (i = 0; i < gspca_dev->nframes; i++)
1120 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1121 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1122 gspca_dev->last_packet_type = DISCARD_PACKET;
1123 gspca_dev->sequence = 0;
1124 atomic_set(&gspca_dev->nevent, 0);
1125 ret = 0;
4aa0d037
JFM
1126out:
1127 mutex_unlock(&gspca_dev->queue_lock);
1128 return ret;
63eb9546
JFM
1129}
1130
1131static int vidioc_g_jpegcomp(struct file *file, void *priv,
1132 struct v4l2_jpegcompression *jpegcomp)
1133{
1134 struct gspca_dev *gspca_dev = priv;
1135 int ret;
1136
1137 if (!gspca_dev->sd_desc->get_jcomp)
1138 return -EINVAL;
e2997a72
HG
1139 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1140 return -ERESTARTSYS;
63eb9546
JFM
1141 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1142 mutex_unlock(&gspca_dev->usb_lock);
1143 return ret;
1144}
1145
1146static int vidioc_s_jpegcomp(struct file *file, void *priv,
1147 struct v4l2_jpegcompression *jpegcomp)
1148{
1149 struct gspca_dev *gspca_dev = priv;
1150 int ret;
1151
e2997a72
HG
1152 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1153 return -ERESTARTSYS;
63eb9546
JFM
1154 if (!gspca_dev->sd_desc->set_jcomp)
1155 return -EINVAL;
1156 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1157 mutex_unlock(&gspca_dev->usb_lock);
1158 return ret;
1159}
1160
1161static int vidioc_g_parm(struct file *filp, void *priv,
1162 struct v4l2_streamparm *parm)
1163{
1164 struct gspca_dev *gspca_dev = priv;
1165
6a7eba24 1166 memset(parm, 0, sizeof *parm);
63eb9546
JFM
1167 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1168 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1169 return 0;
1170}
1171
1172static int vidioc_s_parm(struct file *filp, void *priv,
1173 struct v4l2_streamparm *parm)
1174{
1175 struct gspca_dev *gspca_dev = priv;
1176 int n;
1177
63eb9546
JFM
1178 n = parm->parm.capture.readbuffers;
1179 if (n == 0 || n > GSPCA_MAX_FRAMES)
1180 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1181 else
1182 gspca_dev->nbufread = n;
63eb9546
JFM
1183 return 0;
1184}
1185
6a7eba24
JFM
1186static int vidioc_s_std(struct file *filp, void *priv,
1187 v4l2_std_id *parm)
1188{
1189 return 0;
1190}
1191
63eb9546
JFM
1192#ifdef CONFIG_VIDEO_V4L1_COMPAT
1193static int vidiocgmbuf(struct file *file, void *priv,
1194 struct video_mbuf *mbuf)
1195{
1196 struct gspca_dev *gspca_dev = file->private_data;
1197 int i;
1198
1199 PDEBUG(D_STREAM, "cgmbuf");
1200 if (gspca_dev->nframes == 0) {
63eb9546 1201 int ret;
6a7eba24
JFM
1202
1203 {
1204 struct v4l2_format fmt;
1205
1206 memset(&fmt, 0, sizeof fmt);
1207 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1208 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1209 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1210 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1211 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
e078770a 1212 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
6a7eba24
JFM
1213 if (ret != 0)
1214 return ret;
1215 }
1216 {
1217 struct v4l2_requestbuffers rb;
1218
1219 memset(&rb, 0, sizeof rb);
1220 rb.count = 4;
1221 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1222 rb.memory = V4L2_MEMORY_MMAP;
1223 ret = vidioc_reqbufs(file, priv, &rb);
1224 if (ret != 0)
1225 return ret;
1226 }
63eb9546
JFM
1227 }
1228 mbuf->frames = gspca_dev->nframes;
1229 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1230 for (i = 0; i < mbuf->frames; i++)
1231 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1232 return 0;
1233}
1234#endif
1235
1236static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1237{
1238 struct gspca_dev *gspca_dev = file->private_data;
efab8211 1239 struct gspca_frame *frame;
63eb9546
JFM
1240 struct page *page;
1241 unsigned long addr, start, size;
1242 int i, ret;
63eb9546
JFM
1243
1244 start = vma->vm_start;
1245 size = vma->vm_end - vma->vm_start;
1246 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1247
e2997a72
HG
1248 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1249 return -ERESTARTSYS;
63eb9546
JFM
1250 if (!gspca_dev->present) {
1251 ret = -ENODEV;
4aa0d037
JFM
1252 goto out;
1253 }
1254 if (gspca_dev->capt_file != file) {
1255 ret = -EINVAL;
1256 goto out;
63eb9546
JFM
1257 }
1258
efab8211 1259 frame = NULL;
63eb9546
JFM
1260 for (i = 0; i < gspca_dev->nframes; ++i) {
1261 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1262 PDEBUG(D_STREAM, "mmap bad memory type");
1263 break;
1264 }
1265 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1266 == vma->vm_pgoff) {
1267 frame = &gspca_dev->frame[i];
1268 break;
1269 }
1270 }
efab8211 1271 if (frame == NULL) {
63eb9546
JFM
1272 PDEBUG(D_STREAM, "mmap no frame buffer found");
1273 ret = -EINVAL;
4aa0d037 1274 goto out;
63eb9546
JFM
1275 }
1276#ifdef CONFIG_VIDEO_V4L1_COMPAT
ac0d6df6
JFM
1277 /* v4l1 maps all the buffers */
1278 if (i != 0
1279 || size != frame->v4l2_buf.length * gspca_dev->nframes)
63eb9546 1280#endif
ac0d6df6 1281 if (size != frame->v4l2_buf.length) {
63eb9546
JFM
1282 PDEBUG(D_STREAM, "mmap bad size");
1283 ret = -EINVAL;
4aa0d037 1284 goto out;
63eb9546
JFM
1285 }
1286
1287 /*
1288 * - VM_IO marks the area as being a mmaped region for I/O to a
1289 * device. It also prevents the region from being core dumped.
1290 */
1291 vma->vm_flags |= VM_IO;
1292
1293 addr = (unsigned long) frame->data;
1294 while (size > 0) {
1295 page = vmalloc_to_page((void *) addr);
1296 ret = vm_insert_page(vma, start, page);
1297 if (ret < 0)
4aa0d037 1298 goto out;
63eb9546
JFM
1299 start += PAGE_SIZE;
1300 addr += PAGE_SIZE;
1301 size -= PAGE_SIZE;
1302 }
1303
1304 vma->vm_ops = &gspca_vm_ops;
1305 vma->vm_private_data = frame;
1306 gspca_vm_open(vma);
e2997a72 1307 ret = 0;
4aa0d037 1308out:
63eb9546
JFM
1309 mutex_unlock(&gspca_dev->queue_lock);
1310 return ret;
1311}
1312
63eb9546
JFM
1313/*
1314 * wait for a video frame
1315 *
1316 * If a frame is ready, its index is returned.
1317 */
d43fa32f 1318static int frame_wait(struct gspca_dev *gspca_dev,
63eb9546
JFM
1319 int nonblock_ing)
1320{
1321 struct gspca_frame *frame;
1322 int i, j, ret;
1323
d43fa32f 1324 /* check if a frame is ready */
63eb9546
JFM
1325 i = gspca_dev->fr_o;
1326 j = gspca_dev->fr_queue[i];
1327 frame = &gspca_dev->frame[j];
82643d0e
JFM
1328 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1329 atomic_dec(&gspca_dev->nevent);
63eb9546 1330 goto ok;
82643d0e 1331 }
63eb9546
JFM
1332 if (nonblock_ing) /* no frame yet */
1333 return -EAGAIN;
1334
1335 /* wait till a frame is ready */
1336 for (;;) {
4aa0d037
JFM
1337 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1338 atomic_read(&gspca_dev->nevent) > 0,
1339 msecs_to_jiffies(3000));
1340 if (ret <= 0) {
1341 if (ret < 0)
82643d0e
JFM
1342 return ret; /* interrupt */
1343 return -EIO; /* timeout */
4aa0d037 1344 }
82643d0e 1345 atomic_dec(&gspca_dev->nevent);
e2997a72
HG
1346 if (!gspca_dev->streaming || !gspca_dev->present)
1347 return -EIO;
63eb9546
JFM
1348 i = gspca_dev->fr_o;
1349 j = gspca_dev->fr_queue[i];
1350 frame = &gspca_dev->frame[j];
1351 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1352 break;
1353 }
63eb9546 1354ok:
63eb9546
JFM
1355 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1356 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1357 gspca_dev->fr_q,
1358 gspca_dev->fr_i,
1359 gspca_dev->fr_o);
e2997a72 1360
739570bb
JFM
1361 if (gspca_dev->sd_desc->dq_callback) {
1362 mutex_lock(&gspca_dev->usb_lock);
e2997a72 1363 gspca_dev->sd_desc->dq_callback(gspca_dev);
739570bb
JFM
1364 mutex_unlock(&gspca_dev->usb_lock);
1365 }
63eb9546
JFM
1366 return j;
1367}
1368
1369/*
1370 * dequeue a video buffer
1371 *
1372 * If nonblock_ing is false, block until a buffer is available.
1373 */
1374static int vidioc_dqbuf(struct file *file, void *priv,
1375 struct v4l2_buffer *v4l2_buf)
1376{
1377 struct gspca_dev *gspca_dev = priv;
1378 struct gspca_frame *frame;
1379 int i, ret;
1380
1381 PDEBUG(D_FRAM, "dqbuf");
ac0d6df6
JFM
1382 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1383 return -EINVAL;
1384 if (v4l2_buf->memory != gspca_dev->memory)
63eb9546 1385 return -EINVAL;
5b77ae77
JFM
1386
1387 /* if not streaming, be sure the application will not loop forever */
1388 if (!(file->f_flags & O_NONBLOCK)
1389 && !gspca_dev->streaming && gspca_dev->users == 1)
63eb9546
JFM
1390 return -EINVAL;
1391
5b77ae77
JFM
1392 /* only the capturing file may dequeue */
1393 if (gspca_dev->capt_file != file)
1394 return -EINVAL;
1395
1396 /* only one dequeue / read at a time */
63eb9546
JFM
1397 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1398 return -ERESTARTSYS;
1399
d43fa32f 1400 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
63eb9546 1401 if (ret < 0)
4aa0d037 1402 goto out;
63eb9546
JFM
1403 i = ret; /* frame index */
1404 frame = &gspca_dev->frame[i];
ac0d6df6
JFM
1405 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1406 if (copy_to_user((__u8 *) frame->v4l2_buf.m.userptr,
1407 frame->data,
1408 frame->v4l2_buf.bytesused)) {
1409 PDEBUG(D_ERR|D_STREAM,
1410 "dqbuf cp to user failed");
1411 ret = -EFAULT;
1412 goto out;
1413 }
1414 }
63eb9546
JFM
1415 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1416 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1417 PDEBUG(D_FRAM, "dqbuf %d", i);
1418 ret = 0;
4aa0d037 1419out:
63eb9546
JFM
1420 mutex_unlock(&gspca_dev->read_lock);
1421 return ret;
1422}
1423
1424/*
1425 * queue a video buffer
1426 *
1427 * Attempting to queue a buffer that has already been
1428 * queued will return -EINVAL.
1429 */
1430static int vidioc_qbuf(struct file *file, void *priv,
1431 struct v4l2_buffer *v4l2_buf)
1432{
1433 struct gspca_dev *gspca_dev = priv;
1434 struct gspca_frame *frame;
1435 int i, index, ret;
1436
1437 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1438 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1439 return -EINVAL;
1440
5b77ae77
JFM
1441 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1442 return -ERESTARTSYS;
1443
63eb9546
JFM
1444 index = v4l2_buf->index;
1445 if ((unsigned) index >= gspca_dev->nframes) {
d43fa32f 1446 PDEBUG(D_FRAM,
63eb9546 1447 "qbuf idx %d >= %d", index, gspca_dev->nframes);
5b77ae77
JFM
1448 ret = -EINVAL;
1449 goto out;
63eb9546 1450 }
5b77ae77 1451 if (v4l2_buf->memory != gspca_dev->memory) {
d43fa32f 1452 PDEBUG(D_FRAM, "qbuf bad memory type");
5b77ae77
JFM
1453 ret = -EINVAL;
1454 goto out;
63eb9546 1455 }
e2997a72 1456
5b77ae77 1457 frame = &gspca_dev->frame[index];
d43fa32f
JFM
1458 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1459 PDEBUG(D_FRAM, "qbuf bad state");
63eb9546
JFM
1460 ret = -EINVAL;
1461 goto out;
1462 }
1463
1464 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
d43fa32f 1465/* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
63eb9546 1466
d43fa32f 1467 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
63eb9546
JFM
1468 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1469 frame->v4l2_buf.length = v4l2_buf->length;
1470 }
1471
1472 /* put the buffer in the 'queued' queue */
1473 i = gspca_dev->fr_q;
1474 gspca_dev->fr_queue[i] = index;
1475 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1476 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1477 gspca_dev->fr_q,
1478 gspca_dev->fr_i,
1479 gspca_dev->fr_o);
1480
1481 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1482 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1483 ret = 0;
1484out:
1485 mutex_unlock(&gspca_dev->queue_lock);
1486 return ret;
1487}
1488
d43fa32f
JFM
1489/*
1490 * allocate the resources for read()
1491 */
1492static int read_alloc(struct gspca_dev *gspca_dev,
1493 struct file *file)
63eb9546 1494{
63eb9546 1495 struct v4l2_buffer v4l2_buf;
d43fa32f 1496 int i, ret;
63eb9546 1497
d43fa32f 1498 PDEBUG(D_STREAM, "read alloc");
63eb9546
JFM
1499 if (gspca_dev->nframes == 0) {
1500 struct v4l2_requestbuffers rb;
1501
1502 memset(&rb, 0, sizeof rb);
1503 rb.count = gspca_dev->nbufread;
1504 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ac0d6df6 1505 rb.memory = GSPCA_MEMORY_READ;
63eb9546
JFM
1506 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1507 if (ret != 0) {
d43fa32f 1508 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
63eb9546
JFM
1509 return ret;
1510 }
1511 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1512 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ac0d6df6 1513 v4l2_buf.memory = GSPCA_MEMORY_READ;
63eb9546
JFM
1514 for (i = 0; i < gspca_dev->nbufread; i++) {
1515 v4l2_buf.index = i;
63eb9546
JFM
1516 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1517 if (ret != 0) {
1518 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1519 return ret;
1520 }
1521 }
d43fa32f
JFM
1522 gspca_dev->memory = GSPCA_MEMORY_READ;
1523 }
4aa0d037 1524
d43fa32f
JFM
1525 /* start streaming */
1526 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1527 if (ret != 0)
1528 PDEBUG(D_STREAM, "read streamon err %d", ret);
1529 return ret;
1530}
1531
1532static unsigned int dev_poll(struct file *file, poll_table *wait)
1533{
1534 struct gspca_dev *gspca_dev = file->private_data;
1535 int i, ret;
1536
1537 PDEBUG(D_FRAM, "poll");
1538
1539 poll_wait(file, &gspca_dev->wq, wait);
1540 if (!gspca_dev->present)
1541 return POLLERR;
1542
5b77ae77
JFM
1543 /* if reqbufs is not done, the user would use read() */
1544 if (gspca_dev->nframes == 0) {
1545 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1546 return POLLERR; /* not the 1st time */
d43fa32f 1547 ret = read_alloc(gspca_dev, file);
5b77ae77
JFM
1548 if (ret != 0)
1549 return POLLERR;
63eb9546
JFM
1550 }
1551
d43fa32f
JFM
1552 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1553 return POLLERR;
1554 if (!gspca_dev->present) {
1555 ret = POLLERR;
1556 goto out;
1557 }
1558
5b77ae77 1559 /* check the next incoming buffer */
d43fa32f
JFM
1560 i = gspca_dev->fr_o;
1561 i = gspca_dev->fr_queue[i];
1562 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1563 ret = POLLIN | POLLRDNORM; /* something to read */
1564 else
1565 ret = 0;
1566out:
1567 mutex_unlock(&gspca_dev->queue_lock);
1568 return ret;
1569}
1570
1571static ssize_t dev_read(struct file *file, char __user *data,
1572 size_t count, loff_t *ppos)
1573{
1574 struct gspca_dev *gspca_dev = file->private_data;
1575 struct gspca_frame *frame;
1576 struct v4l2_buffer v4l2_buf;
1577 struct timeval timestamp;
82643d0e 1578 int n, ret, ret2;
d43fa32f 1579
a9e11134 1580 PDEBUG(D_FRAM, "read (%zd)", count);
d43fa32f
JFM
1581 if (!gspca_dev->present)
1582 return -ENODEV;
1583 switch (gspca_dev->memory) {
1584 case GSPCA_MEMORY_NO: /* first time */
1585 ret = read_alloc(gspca_dev, file);
1586 if (ret != 0)
1587 return ret;
1588 break;
1589 case GSPCA_MEMORY_READ:
82643d0e
JFM
1590 if (gspca_dev->capt_file == file)
1591 break;
1592 /* fall thru */
d43fa32f
JFM
1593 default:
1594 return -EINVAL;
1595 }
1596
63eb9546
JFM
1597 /* get a frame */
1598 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1599 timestamp.tv_sec--;
82643d0e
JFM
1600 n = 2;
1601 for (;;) {
63eb9546
JFM
1602 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1603 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ac0d6df6 1604 v4l2_buf.memory = GSPCA_MEMORY_READ;
63eb9546
JFM
1605 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1606 if (ret != 0) {
1607 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1608 return ret;
1609 }
1610
1611 /* if the process slept for more than 1 second,
82643d0e 1612 * get anewer frame */
63eb9546 1613 frame = &gspca_dev->frame[v4l2_buf.index];
82643d0e
JFM
1614 if (--n < 0)
1615 break; /* avoid infinite loop */
63eb9546
JFM
1616 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1617 break;
1618 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1619 if (ret != 0) {
1620 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1621 return ret;
1622 }
1623 }
1624
1625 /* copy the frame */
ac0d6df6
JFM
1626 if (count > frame->v4l2_buf.bytesused)
1627 count = frame->v4l2_buf.bytesused;
63eb9546
JFM
1628 ret = copy_to_user(data, frame->data, count);
1629 if (ret != 0) {
1630 PDEBUG(D_ERR|D_STREAM,
a9e11134 1631 "read cp to user lack %d / %zd", ret, count);
63eb9546
JFM
1632 ret = -EFAULT;
1633 goto out;
1634 }
1635 ret = count;
1636out:
1637 /* in each case, requeue the buffer */
1638 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1639 if (ret2 != 0)
1640 return ret2;
1641 return ret;
1642}
1643
d43fa32f 1644static void dev_release(struct video_device *vfd)
63eb9546
JFM
1645{
1646 /* nothing */
1647}
1648
1649static struct file_operations dev_fops = {
1650 .owner = THIS_MODULE,
1651 .open = dev_open,
1652 .release = dev_close,
1653 .read = dev_read,
1654 .mmap = dev_mmap,
1655 .ioctl = video_ioctl2,
6a7eba24
JFM
1656#ifdef CONFIG_COMPAT
1657 .compat_ioctl = v4l_compat_ioctl32,
1658#endif
63eb9546
JFM
1659 .llseek = no_llseek,
1660 .poll = dev_poll,
1661};
1662
a399810c 1663static const struct v4l2_ioctl_ops dev_ioctl_ops = {
63eb9546
JFM
1664 .vidioc_querycap = vidioc_querycap,
1665 .vidioc_dqbuf = vidioc_dqbuf,
1666 .vidioc_qbuf = vidioc_qbuf,
e078770a
MCC
1667 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1668 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1669 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1670 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
63eb9546
JFM
1671 .vidioc_streamon = vidioc_streamon,
1672 .vidioc_queryctrl = vidioc_queryctrl,
1673 .vidioc_g_ctrl = vidioc_g_ctrl,
1674 .vidioc_s_ctrl = vidioc_s_ctrl,
1675 .vidioc_querymenu = vidioc_querymenu,
1676 .vidioc_enum_input = vidioc_enum_input,
1677 .vidioc_g_input = vidioc_g_input,
1678 .vidioc_s_input = vidioc_s_input,
1679 .vidioc_reqbufs = vidioc_reqbufs,
1680 .vidioc_querybuf = vidioc_querybuf,
1681 .vidioc_streamoff = vidioc_streamoff,
1682 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1683 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1684 .vidioc_g_parm = vidioc_g_parm,
1685 .vidioc_s_parm = vidioc_s_parm,
6a7eba24 1686 .vidioc_s_std = vidioc_s_std,
63eb9546
JFM
1687#ifdef CONFIG_VIDEO_V4L1_COMPAT
1688 .vidiocgmbuf = vidiocgmbuf,
1689#endif
1690};
1691
a399810c
HV
1692static struct video_device gspca_template = {
1693 .name = "gspca main driver",
a399810c
HV
1694 .fops = &dev_fops,
1695 .ioctl_ops = &dev_ioctl_ops,
1696 .release = dev_release, /* mandatory */
1697 .minor = -1,
1698};
1699
63eb9546
JFM
1700/*
1701 * probe and create a new gspca device
1702 *
1703 * This function must be called by the sub-driver when it is
1704 * called for probing a new device.
1705 */
1706int gspca_dev_probe(struct usb_interface *intf,
1707 const struct usb_device_id *id,
1708 const struct sd_desc *sd_desc,
d43fa32f
JFM
1709 int dev_size,
1710 struct module *module)
63eb9546
JFM
1711{
1712 struct usb_interface_descriptor *interface;
1713 struct gspca_dev *gspca_dev;
1714 struct usb_device *dev = interface_to_usbdev(intf);
1715 int ret;
63eb9546 1716
6a7eba24 1717 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
63eb9546
JFM
1718
1719 /* we don't handle multi-config cameras */
1720 if (dev->descriptor.bNumConfigurations != 1)
1721 return -ENODEV;
1722 interface = &intf->cur_altsetting->desc;
1723 if (interface->bInterfaceNumber > 0)
1724 return -ENODEV;
1725
1726 /* create the device */
1727 if (dev_size < sizeof *gspca_dev)
1728 dev_size = sizeof *gspca_dev;
1729 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1730 if (gspca_dev == NULL) {
1731 err("couldn't kzalloc gspca struct");
1732 return -EIO;
1733 }
1734 gspca_dev->dev = dev;
1735 gspca_dev->iface = interface->bInterfaceNumber;
d43fa32f 1736 gspca_dev->nbalt = intf->num_altsetting;
63eb9546
JFM
1737 gspca_dev->sd_desc = sd_desc;
1738/* gspca_dev->users = 0; (done by kzalloc) */
1739 gspca_dev->nbufread = 2;
1740
1741 /* configure the subdriver */
1742 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1743 if (ret < 0)
1744 goto out;
1745 ret = gspca_set_alt0(gspca_dev);
1746 if (ret < 0)
1747 goto out;
1748 gspca_set_default_mode(gspca_dev);
1749
1750 mutex_init(&gspca_dev->usb_lock);
1751 mutex_init(&gspca_dev->read_lock);
1752 mutex_init(&gspca_dev->queue_lock);
1753 init_waitqueue_head(&gspca_dev->wq);
1754
1755 /* init video stuff */
1756 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
5e85e732 1757 gspca_dev->vdev.parent = &dev->dev;
d43fa32f
JFM
1758 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1759 gspca_dev->vdev.fops = &gspca_dev->fops;
1760 gspca_dev->fops.owner = module; /* module protection */
ee56a4d3 1761 gspca_dev->present = 1;
63eb9546
JFM
1762 ret = video_register_device(&gspca_dev->vdev,
1763 VFL_TYPE_GRABBER,
1764 video_nr);
1765 if (ret < 0) {
1766 err("video_register_device err %d", ret);
1767 goto out;
1768 }
1769
63eb9546
JFM
1770 usb_set_intfdata(intf, gspca_dev);
1771 PDEBUG(D_PROBE, "probe ok");
1772 return 0;
1773out:
1774 kfree(gspca_dev);
1775 return ret;
1776}
1777EXPORT_SYMBOL(gspca_dev_probe);
1778
1779/*
1780 * USB disconnection
1781 *
1782 * This function must be called by the sub-driver
1783 * when the device disconnects, after the specific resources are freed.
1784 */
1785void gspca_disconnect(struct usb_interface *intf)
1786{
4aa0d037 1787 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
63eb9546
JFM
1788
1789 if (!gspca_dev)
1790 return;
1791 gspca_dev->present = 0;
e2997a72
HG
1792 mutex_lock(&gspca_dev->queue_lock);
1793 mutex_lock(&gspca_dev->usb_lock);
1794 gspca_dev->streaming = 0;
d43fa32f 1795 destroy_urbs(gspca_dev);
63eb9546 1796 mutex_unlock(&gspca_dev->usb_lock);
e2997a72 1797 mutex_unlock(&gspca_dev->queue_lock);
63eb9546
JFM
1798 while (gspca_dev->users != 0) { /* wait until fully closed */
1799 atomic_inc(&gspca_dev->nevent);
1800 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1801 schedule();
1802 }
1803/* We don't want people trying to open up the device */
1804 video_unregister_device(&gspca_dev->vdev);
1805/* Free the memory */
1806 kfree(gspca_dev);
1807 PDEBUG(D_PROBE, "disconnect complete");
1808}
1809EXPORT_SYMBOL(gspca_disconnect);
1810
dcef3237
HG
1811/* -- cam driver utility functions -- */
1812
1813/* auto gain and exposure algorithm based on the knee algorithm described here:
1814 http://ytse.tricolour.net/docs/LowLightOptimization.html
1815
1816 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1817 where changed. */
1818int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1819 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1820{
1821 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1822 const struct ctrl *gain_ctrl = NULL;
1823 const struct ctrl *exposure_ctrl = NULL;
1824 const struct ctrl *autogain_ctrl = NULL;
1825 int retval = 0;
1826
1827 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1828 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1829 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1830 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1831 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1832 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1833 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1834 }
1835 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1836 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1837 "on cam without (auto)gain/exposure");
1838 return 0;
1839 }
1840
1841 if (gain_ctrl->get(gspca_dev, &gain) ||
1842 exposure_ctrl->get(gspca_dev, &exposure) ||
1843 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1844 return 0;
1845
1846 orig_gain = gain;
1847 orig_exposure = exposure;
1848
1849 /* If we are of a multiple of deadzone, do multiple steps to reach the
1850 desired lumination fast (with the risc of a slight overshoot) */
1851 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1852
1853 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1854 avg_lum, desired_avg_lum, steps);
1855
1856 for (i = 0; i < steps; i++) {
1857 if (avg_lum > desired_avg_lum) {
1858 if (gain > gain_knee)
1859 gain--;
1860 else if (exposure > exposure_knee)
1861 exposure--;
1862 else if (gain > gain_ctrl->qctrl.default_value)
1863 gain--;
1864 else if (exposure > exposure_ctrl->qctrl.minimum)
1865 exposure--;
1866 else if (gain > gain_ctrl->qctrl.minimum)
1867 gain--;
1868 else
1869 break;
1870 } else {
1871 if (gain < gain_ctrl->qctrl.default_value)
1872 gain++;
1873 else if (exposure < exposure_knee)
1874 exposure++;
1875 else if (gain < gain_knee)
1876 gain++;
1877 else if (exposure < exposure_ctrl->qctrl.maximum)
1878 exposure++;
1879 else if (gain < gain_ctrl->qctrl.maximum)
1880 gain++;
1881 else
1882 break;
1883 }
1884 }
1885
1886 if (gain != orig_gain) {
1887 gain_ctrl->set(gspca_dev, gain);
1888 retval = 1;
1889 }
1890 if (exposure != orig_exposure) {
1891 exposure_ctrl->set(gspca_dev, exposure);
1892 retval = 1;
1893 }
1894
1895 return retval;
1896}
1897EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1898
63eb9546
JFM
1899/* -- module insert / remove -- */
1900static int __init gspca_init(void)
1901{
bb64e86c
JFM
1902 info("main v%d.%d.%d registered",
1903 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1904 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1905 DRIVER_VERSION_NUMBER & 0xff);
63eb9546
JFM
1906 return 0;
1907}
1908static void __exit gspca_exit(void)
1909{
1910 info("main deregistered");
1911}
1912
1913module_init(gspca_init);
1914module_exit(gspca_exit);
1915
335b3f88 1916#ifdef GSPCA_DEBUG
63eb9546
JFM
1917module_param_named(debug, gspca_debug, int, 0644);
1918MODULE_PARM_DESC(debug,
1919 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
d43fa32f
JFM
1920 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
1921 " 0x0100: v4l2");
6a7eba24 1922#endif
This page took 0.133774 seconds and 5 git commands to generate.