Full support for Ginger Console
[linux-ginger.git] / drivers / media / video / gspca / gspca.c
blob23d3fb7769186f921c0b90eccdf9cd4471301075
1 /*
2 * Main USB camera driver
4 * Copyright (C) 2008-2009 Jean-Francois Moine (http://moinejf.free.fr)
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.
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.
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.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
24 #include <linux/version.h>
25 #include <linux/fs.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/string.h>
31 #include <linux/pagemap.h>
32 #include <linux/io.h>
33 #include <asm/page.h>
34 #include <linux/uaccess.h>
35 #include <linux/jiffies.h>
36 #include <media/v4l2-ioctl.h>
38 #include "gspca.h"
40 /* global values */
41 #define DEF_NURBS 3 /* default number of URBs */
42 #if DEF_NURBS > MAX_NURBS
43 #error "DEF_NURBS too big"
44 #endif
46 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
47 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
48 MODULE_LICENSE("GPL");
50 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 7, 0)
52 #ifdef GSPCA_DEBUG
53 int gspca_debug = D_ERR | D_PROBE;
54 EXPORT_SYMBOL(gspca_debug);
56 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 txt,
61 pixfmt & 0xff,
62 (pixfmt >> 8) & 0xff,
63 (pixfmt >> 16) & 0xff,
64 pixfmt >> 24,
65 w, h);
66 } else {
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
68 txt,
69 pixfmt,
70 w, h);
73 #else
74 #define PDEBUG_MODE(txt, pixfmt, w, h)
75 #endif
77 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
78 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79 #define GSPCA_MEMORY_READ 7
81 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
84 * VMA operations.
86 static void gspca_vm_open(struct vm_area_struct *vma)
88 struct gspca_frame *frame = vma->vm_private_data;
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
94 static void gspca_vm_close(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
102 static const struct vm_operations_struct gspca_vm_ops = {
103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
107 /* get the current input frame buffer */
108 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
110 struct gspca_frame *frame;
111 int i;
113 i = gspca_dev->fr_i;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
118 return NULL;
119 return frame;
121 EXPORT_SYMBOL(gspca_get_i_frame);
124 * fill a video frame from an URB and resubmit
126 static void fill_frame(struct gspca_dev *gspca_dev,
127 struct urb *urb)
129 struct gspca_frame *frame;
130 u8 *data; /* address of data in the iso message */
131 int i, len, st;
132 cam_pkt_op pkt_scan;
134 if (urb->status != 0) {
135 if (urb->status == -ESHUTDOWN)
136 return; /* disconnection */
137 #ifdef CONFIG_PM
138 if (!gspca_dev->frozen)
139 #endif
140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
141 return;
143 pkt_scan = gspca_dev->sd_desc->pkt_scan;
144 for (i = 0; i < urb->number_of_packets; i++) {
146 /* check the availability of the frame buffer */
147 frame = gspca_get_i_frame(gspca_dev);
148 if (!frame) {
149 gspca_dev->last_packet_type = DISCARD_PACKET;
150 break;
153 /* check the packet status and length */
154 len = urb->iso_frame_desc[i].actual_length;
155 if (len == 0) {
156 if (gspca_dev->empty_packet == 0)
157 gspca_dev->empty_packet = 1;
158 continue;
160 st = urb->iso_frame_desc[i].status;
161 if (st) {
162 PDEBUG(D_ERR,
163 "ISOC data error: [%d] len=%d, status=%d",
164 i, len, st);
165 gspca_dev->last_packet_type = DISCARD_PACKET;
166 continue;
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
171 i, urb->iso_frame_desc[i].offset, len);
172 data = (u8 *) urb->transfer_buffer
173 + urb->iso_frame_desc[i].offset;
174 pkt_scan(gspca_dev, frame, data, len);
177 /* resubmit the URB */
178 st = usb_submit_urb(urb, GFP_ATOMIC);
179 if (st < 0)
180 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
184 * ISOC message interrupt from the USB device
186 * Analyse each packet and call the subdriver for copy to the frame buffer.
188 static void isoc_irq(struct urb *urb)
190 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
192 PDEBUG(D_PACK, "isoc irq");
193 if (!gspca_dev->streaming)
194 return;
195 fill_frame(gspca_dev, urb);
199 * bulk message interrupt from the USB device
201 static void bulk_irq(struct urb *urb)
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
205 int st;
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
209 return;
210 switch (urb->status) {
211 case 0:
212 break;
213 case -ESHUTDOWN:
214 return; /* disconnection */
215 case -ECONNRESET:
216 urb->status = 0;
217 break;
218 default:
219 #ifdef CONFIG_PM
220 if (!gspca_dev->frozen)
221 #endif
222 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
223 return;
226 /* check the availability of the frame buffer */
227 frame = gspca_get_i_frame(gspca_dev);
228 if (!frame) {
229 gspca_dev->last_packet_type = DISCARD_PACKET;
230 } else {
231 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
232 gspca_dev->sd_desc->pkt_scan(gspca_dev,
233 frame,
234 urb->transfer_buffer,
235 urb->actual_length);
238 /* resubmit the URB */
239 if (gspca_dev->cam.bulk_nurbs != 0) {
240 st = usb_submit_urb(urb, GFP_ATOMIC);
241 if (st < 0)
242 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
247 * add data to the current frame
249 * This function is called by the subdrivers at interrupt level.
251 * To build a frame, these ones must add
252 * - one FIRST_PACKET
253 * - 0 or many INTER_PACKETs
254 * - one LAST_PACKET
255 * DISCARD_PACKET invalidates the whole frame.
256 * On LAST_PACKET, a new frame is returned.
258 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
259 enum gspca_packet_type packet_type,
260 struct gspca_frame *frame,
261 const __u8 *data,
262 int len)
264 int i, j;
266 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
268 /* when start of a new frame, if the current frame buffer
269 * is not queued, discard the whole frame */
270 if (packet_type == FIRST_PACKET) {
271 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
272 != V4L2_BUF_FLAG_QUEUED) {
273 gspca_dev->last_packet_type = DISCARD_PACKET;
274 return frame;
276 frame->data_end = frame->data;
277 jiffies_to_timeval(get_jiffies_64(),
278 &frame->v4l2_buf.timestamp);
279 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
280 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
281 if (packet_type == LAST_PACKET)
282 gspca_dev->last_packet_type = packet_type;
283 return frame;
286 /* append the packet to the frame buffer */
287 if (len > 0) {
288 if (frame->data_end - frame->data + len
289 > frame->v4l2_buf.length) {
290 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
291 frame->data_end - frame->data + len,
292 frame->v4l2_buf.length);
293 packet_type = DISCARD_PACKET;
294 } else {
295 memcpy(frame->data_end, data, len);
296 frame->data_end += len;
299 gspca_dev->last_packet_type = packet_type;
301 /* if last packet, wake up the application and advance in the queue */
302 if (packet_type == LAST_PACKET) {
303 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
304 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
305 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
306 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
307 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
308 gspca_dev->fr_i = i;
309 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
310 frame->v4l2_buf.bytesused,
311 gspca_dev->fr_q,
313 gspca_dev->fr_o);
314 j = gspca_dev->fr_queue[i];
315 frame = &gspca_dev->frame[j];
317 return frame;
319 EXPORT_SYMBOL(gspca_frame_add);
321 static int gspca_is_compressed(__u32 format)
323 switch (format) {
324 case V4L2_PIX_FMT_MJPEG:
325 case V4L2_PIX_FMT_JPEG:
326 case V4L2_PIX_FMT_SPCA561:
327 case V4L2_PIX_FMT_PAC207:
328 case V4L2_PIX_FMT_MR97310A:
329 return 1;
331 return 0;
334 static void *rvmalloc(unsigned long size)
336 void *mem;
337 unsigned long adr;
339 mem = vmalloc_32(size);
340 if (mem != NULL) {
341 adr = (unsigned long) mem;
342 while ((long) size > 0) {
343 SetPageReserved(vmalloc_to_page((void *) adr));
344 adr += PAGE_SIZE;
345 size -= PAGE_SIZE;
348 return mem;
351 static void rvfree(void *mem, long size)
353 unsigned long adr;
355 adr = (unsigned long) mem;
356 while (size > 0) {
357 ClearPageReserved(vmalloc_to_page((void *) adr));
358 adr += PAGE_SIZE;
359 size -= PAGE_SIZE;
361 vfree(mem);
364 static int frame_alloc(struct gspca_dev *gspca_dev,
365 unsigned int count)
367 struct gspca_frame *frame;
368 unsigned int frsz;
369 int i;
371 i = gspca_dev->curr_mode;
372 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
373 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
374 frsz = PAGE_ALIGN(frsz);
375 gspca_dev->frsz = frsz;
376 if (count > GSPCA_MAX_FRAMES)
377 count = GSPCA_MAX_FRAMES;
378 gspca_dev->frbuf = rvmalloc(frsz * count);
379 if (!gspca_dev->frbuf) {
380 err("frame alloc failed");
381 return -ENOMEM;
383 gspca_dev->nframes = count;
384 for (i = 0; i < count; i++) {
385 frame = &gspca_dev->frame[i];
386 frame->v4l2_buf.index = i;
387 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
388 frame->v4l2_buf.flags = 0;
389 frame->v4l2_buf.field = V4L2_FIELD_NONE;
390 frame->v4l2_buf.length = frsz;
391 frame->v4l2_buf.memory = gspca_dev->memory;
392 frame->v4l2_buf.sequence = 0;
393 frame->data = frame->data_end =
394 gspca_dev->frbuf + i * frsz;
395 frame->v4l2_buf.m.offset = i * frsz;
397 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
398 gspca_dev->last_packet_type = DISCARD_PACKET;
399 gspca_dev->sequence = 0;
400 return 0;
403 static void frame_free(struct gspca_dev *gspca_dev)
405 int i;
407 PDEBUG(D_STREAM, "frame free");
408 if (gspca_dev->frbuf != NULL) {
409 rvfree(gspca_dev->frbuf,
410 gspca_dev->nframes * gspca_dev->frsz);
411 gspca_dev->frbuf = NULL;
412 for (i = 0; i < gspca_dev->nframes; i++)
413 gspca_dev->frame[i].data = NULL;
415 gspca_dev->nframes = 0;
418 static void destroy_urbs(struct gspca_dev *gspca_dev)
420 struct urb *urb;
421 unsigned int i;
423 PDEBUG(D_STREAM, "kill transfer");
424 for (i = 0; i < MAX_NURBS; i++) {
425 urb = gspca_dev->urb[i];
426 if (urb == NULL)
427 break;
429 gspca_dev->urb[i] = NULL;
430 usb_kill_urb(urb);
431 if (urb->transfer_buffer != NULL)
432 usb_buffer_free(gspca_dev->dev,
433 urb->transfer_buffer_length,
434 urb->transfer_buffer,
435 urb->transfer_dma);
436 usb_free_urb(urb);
441 * look for an input transfer endpoint in an alternate setting
443 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
444 int xfer)
446 struct usb_host_endpoint *ep;
447 int i, attr;
449 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
450 ep = &alt->endpoint[i];
451 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
452 if (attr == xfer
453 && ep->desc.wMaxPacketSize != 0)
454 return ep;
456 return NULL;
460 * look for an input (isoc or bulk) endpoint
462 * The endpoint is defined by the subdriver.
463 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
464 * This routine may be called many times when the bandwidth is too small
465 * (the bandwidth is checked on urb submit).
467 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
469 struct usb_interface *intf;
470 struct usb_host_endpoint *ep;
471 int xfer, i, ret;
473 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
474 ep = NULL;
475 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
476 : USB_ENDPOINT_XFER_ISOC;
477 i = gspca_dev->alt; /* previous alt setting */
478 while (--i >= 0) {
479 ep = alt_xfer(&intf->altsetting[i], xfer);
480 if (ep)
481 break;
483 if (ep == NULL) {
484 err("no transfer endpoint found");
485 return NULL;
487 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
488 i, ep->desc.bEndpointAddress);
489 gspca_dev->alt = i; /* memorize the current alt setting */
490 if (gspca_dev->nbalt > 1) {
491 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
492 if (ret < 0) {
493 err("set alt %d err %d", i, ret);
494 return NULL;
497 return ep;
501 * create the URBs for image transfer
503 static int create_urbs(struct gspca_dev *gspca_dev,
504 struct usb_host_endpoint *ep)
506 struct urb *urb;
507 int n, nurbs, i, psize, npkt, bsize;
509 /* calculate the packet size and the number of packets */
510 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
512 if (!gspca_dev->cam.bulk) { /* isoc */
514 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
515 if (gspca_dev->pkt_size == 0)
516 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
517 else
518 psize = gspca_dev->pkt_size;
519 npkt = gspca_dev->cam.npkt;
520 if (npkt == 0)
521 npkt = 32; /* default value */
522 bsize = psize * npkt;
523 PDEBUG(D_STREAM,
524 "isoc %d pkts size %d = bsize:%d",
525 npkt, psize, bsize);
526 nurbs = DEF_NURBS;
527 } else { /* bulk */
528 npkt = 0;
529 bsize = gspca_dev->cam.bulk_size;
530 if (bsize == 0)
531 bsize = psize;
532 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
533 if (gspca_dev->cam.bulk_nurbs != 0)
534 nurbs = gspca_dev->cam.bulk_nurbs;
535 else
536 nurbs = 1;
539 gspca_dev->nurbs = nurbs;
540 for (n = 0; n < nurbs; n++) {
541 urb = usb_alloc_urb(npkt, GFP_KERNEL);
542 if (!urb) {
543 err("usb_alloc_urb failed");
544 destroy_urbs(gspca_dev);
545 return -ENOMEM;
547 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
548 bsize,
549 GFP_KERNEL,
550 &urb->transfer_dma);
552 if (urb->transfer_buffer == NULL) {
553 usb_free_urb(urb);
554 err("usb_buffer_urb failed");
555 destroy_urbs(gspca_dev);
556 return -ENOMEM;
558 gspca_dev->urb[n] = urb;
559 urb->dev = gspca_dev->dev;
560 urb->context = gspca_dev;
561 urb->transfer_buffer_length = bsize;
562 if (npkt != 0) { /* ISOC */
563 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
564 ep->desc.bEndpointAddress);
565 urb->transfer_flags = URB_ISO_ASAP
566 | URB_NO_TRANSFER_DMA_MAP;
567 urb->interval = ep->desc.bInterval;
568 urb->complete = isoc_irq;
569 urb->number_of_packets = npkt;
570 for (i = 0; i < npkt; i++) {
571 urb->iso_frame_desc[i].length = psize;
572 urb->iso_frame_desc[i].offset = psize * i;
574 } else { /* bulk */
575 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
576 ep->desc.bEndpointAddress),
577 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
578 urb->complete = bulk_irq;
581 return 0;
585 * start the USB transfer
587 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
589 struct usb_host_endpoint *ep;
590 int n, ret;
592 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
593 return -ERESTARTSYS;
595 if (!gspca_dev->present) {
596 ret = -ENODEV;
597 goto out;
600 /* set the higher alternate setting and
601 * loop until urb submit succeeds */
602 gspca_dev->alt = gspca_dev->nbalt;
603 if (gspca_dev->sd_desc->isoc_init) {
604 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
605 if (ret < 0)
606 goto out;
608 ep = get_ep(gspca_dev);
609 if (ep == NULL) {
610 ret = -EIO;
611 goto out;
613 for (;;) {
614 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
615 ret = create_urbs(gspca_dev, ep);
616 if (ret < 0)
617 goto out;
619 /* clear the bulk endpoint */
620 if (gspca_dev->cam.bulk)
621 usb_clear_halt(gspca_dev->dev,
622 gspca_dev->urb[0]->pipe);
624 /* start the cam */
625 ret = gspca_dev->sd_desc->start(gspca_dev);
626 if (ret < 0) {
627 destroy_urbs(gspca_dev);
628 goto out;
630 gspca_dev->streaming = 1;
632 /* some bulk transfers are started by the subdriver */
633 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
634 break;
636 /* submit the URBs */
637 for (n = 0; n < gspca_dev->nurbs; n++) {
638 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
639 if (ret < 0)
640 break;
642 if (ret >= 0)
643 break;
644 PDEBUG(D_ERR|D_STREAM,
645 "usb_submit_urb alt %d err %d", gspca_dev->alt, ret);
646 gspca_dev->streaming = 0;
647 destroy_urbs(gspca_dev);
648 if (ret != -ENOSPC)
649 goto out;
651 /* the bandwidth is not wide enough
652 * negociate or try a lower alternate setting */
653 msleep(20); /* wait for kill complete */
654 if (gspca_dev->sd_desc->isoc_nego) {
655 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
656 if (ret < 0)
657 goto out;
658 } else {
659 ep = get_ep(gspca_dev);
660 if (ep == NULL) {
661 ret = -EIO;
662 goto out;
666 out:
667 mutex_unlock(&gspca_dev->usb_lock);
668 return ret;
671 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
673 int ret;
675 if (gspca_dev->alt == 0)
676 return 0;
677 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
678 if (ret < 0)
679 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
680 return ret;
683 /* Note: both the queue and the usb locks should be held when calling this */
684 static void gspca_stream_off(struct gspca_dev *gspca_dev)
686 gspca_dev->streaming = 0;
687 if (gspca_dev->present) {
688 if (gspca_dev->sd_desc->stopN)
689 gspca_dev->sd_desc->stopN(gspca_dev);
690 destroy_urbs(gspca_dev);
691 gspca_set_alt0(gspca_dev);
694 /* always call stop0 to free the subdriver's resources */
695 if (gspca_dev->sd_desc->stop0)
696 gspca_dev->sd_desc->stop0(gspca_dev);
697 PDEBUG(D_STREAM, "stream off OK");
700 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
702 int i;
704 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
705 gspca_dev->curr_mode = i;
706 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
707 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
708 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
711 static int wxh_to_mode(struct gspca_dev *gspca_dev,
712 int width, int height)
714 int i;
716 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
717 if (width >= gspca_dev->cam.cam_mode[i].width
718 && height >= gspca_dev->cam.cam_mode[i].height)
719 break;
721 return i;
725 * search a mode with the right pixel format
727 static int gspca_get_mode(struct gspca_dev *gspca_dev,
728 int mode,
729 int pixfmt)
731 int modeU, modeD;
733 modeU = modeD = mode;
734 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
735 if (--modeD >= 0) {
736 if (gspca_dev->cam.cam_mode[modeD].pixelformat
737 == pixfmt)
738 return modeD;
740 if (++modeU < gspca_dev->cam.nmodes) {
741 if (gspca_dev->cam.cam_mode[modeU].pixelformat
742 == pixfmt)
743 return modeU;
746 return -EINVAL;
749 #ifdef CONFIG_VIDEO_ADV_DEBUG
750 static int vidioc_g_register(struct file *file, void *priv,
751 struct v4l2_dbg_register *reg)
753 int ret;
754 struct gspca_dev *gspca_dev = priv;
756 if (!gspca_dev->sd_desc->get_chip_ident)
757 return -EINVAL;
759 if (!gspca_dev->sd_desc->get_register)
760 return -EINVAL;
762 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
763 return -ERESTARTSYS;
764 if (gspca_dev->present)
765 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
766 else
767 ret = -ENODEV;
768 mutex_unlock(&gspca_dev->usb_lock);
770 return ret;
773 static int vidioc_s_register(struct file *file, void *priv,
774 struct v4l2_dbg_register *reg)
776 int ret;
777 struct gspca_dev *gspca_dev = priv;
779 if (!gspca_dev->sd_desc->get_chip_ident)
780 return -EINVAL;
782 if (!gspca_dev->sd_desc->set_register)
783 return -EINVAL;
785 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
786 return -ERESTARTSYS;
787 if (gspca_dev->present)
788 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
789 else
790 ret = -ENODEV;
791 mutex_unlock(&gspca_dev->usb_lock);
793 return ret;
795 #endif
797 static int vidioc_g_chip_ident(struct file *file, void *priv,
798 struct v4l2_dbg_chip_ident *chip)
800 int ret;
801 struct gspca_dev *gspca_dev = priv;
803 if (!gspca_dev->sd_desc->get_chip_ident)
804 return -EINVAL;
806 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
807 return -ERESTARTSYS;
808 if (gspca_dev->present)
809 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
810 else
811 ret = -ENODEV;
812 mutex_unlock(&gspca_dev->usb_lock);
814 return ret;
817 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
818 struct v4l2_fmtdesc *fmtdesc)
820 struct gspca_dev *gspca_dev = priv;
821 int i, j, index;
822 __u32 fmt_tb[8];
824 /* give an index to each format */
825 index = 0;
826 j = 0;
827 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
828 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
829 j = 0;
830 for (;;) {
831 if (fmt_tb[j] == fmt_tb[index])
832 break;
833 j++;
835 if (j == index) {
836 if (fmtdesc->index == index)
837 break; /* new format */
838 index++;
839 if (index >= ARRAY_SIZE(fmt_tb))
840 return -EINVAL;
843 if (i < 0)
844 return -EINVAL; /* no more format */
846 fmtdesc->pixelformat = fmt_tb[index];
847 if (gspca_is_compressed(fmt_tb[index]))
848 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
849 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
850 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
851 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
852 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
853 fmtdesc->description[4] = '\0';
854 return 0;
857 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
858 struct v4l2_format *fmt)
860 struct gspca_dev *gspca_dev = priv;
861 int mode;
863 mode = gspca_dev->curr_mode;
864 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
865 sizeof fmt->fmt.pix);
866 return 0;
869 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
870 struct v4l2_format *fmt)
872 int w, h, mode, mode2;
874 w = fmt->fmt.pix.width;
875 h = fmt->fmt.pix.height;
877 #ifdef GSPCA_DEBUG
878 if (gspca_debug & D_CONF)
879 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
880 #endif
881 /* search the closest mode for width and height */
882 mode = wxh_to_mode(gspca_dev, w, h);
884 /* OK if right palette */
885 if (gspca_dev->cam.cam_mode[mode].pixelformat
886 != fmt->fmt.pix.pixelformat) {
888 /* else, search the closest mode with the same pixel format */
889 mode2 = gspca_get_mode(gspca_dev, mode,
890 fmt->fmt.pix.pixelformat);
891 if (mode2 >= 0)
892 mode = mode2;
893 /* else
894 ; * no chance, return this mode */
896 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
897 sizeof fmt->fmt.pix);
898 return mode; /* used when s_fmt */
901 static int vidioc_try_fmt_vid_cap(struct file *file,
902 void *priv,
903 struct v4l2_format *fmt)
905 struct gspca_dev *gspca_dev = priv;
906 int ret;
908 ret = try_fmt_vid_cap(gspca_dev, fmt);
909 if (ret < 0)
910 return ret;
911 return 0;
914 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
915 struct v4l2_format *fmt)
917 struct gspca_dev *gspca_dev = priv;
918 int ret;
920 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
921 return -ERESTARTSYS;
923 ret = try_fmt_vid_cap(gspca_dev, fmt);
924 if (ret < 0)
925 goto out;
927 if (gspca_dev->nframes != 0
928 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
929 ret = -EINVAL;
930 goto out;
933 if (ret == gspca_dev->curr_mode) {
934 ret = 0;
935 goto out; /* same mode */
938 if (gspca_dev->streaming) {
939 ret = -EBUSY;
940 goto out;
942 gspca_dev->width = fmt->fmt.pix.width;
943 gspca_dev->height = fmt->fmt.pix.height;
944 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
945 gspca_dev->curr_mode = ret;
947 ret = 0;
948 out:
949 mutex_unlock(&gspca_dev->queue_lock);
950 return ret;
953 static int vidioc_enum_framesizes(struct file *file, void *priv,
954 struct v4l2_frmsizeenum *fsize)
956 struct gspca_dev *gspca_dev = priv;
957 int i;
958 __u32 index = 0;
960 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
961 if (fsize->pixel_format !=
962 gspca_dev->cam.cam_mode[i].pixelformat)
963 continue;
965 if (fsize->index == index) {
966 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
967 fsize->discrete.width =
968 gspca_dev->cam.cam_mode[i].width;
969 fsize->discrete.height =
970 gspca_dev->cam.cam_mode[i].height;
971 return 0;
973 index++;
976 return -EINVAL;
979 static void gspca_release(struct video_device *vfd)
981 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
983 PDEBUG(D_STREAM, "device released");
985 kfree(gspca_dev->usb_buf);
986 kfree(gspca_dev);
989 static int dev_open(struct file *file)
991 struct gspca_dev *gspca_dev;
992 int ret;
994 PDEBUG(D_STREAM, "%s open", current->comm);
995 gspca_dev = (struct gspca_dev *) video_devdata(file);
996 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
997 return -ERESTARTSYS;
998 if (!gspca_dev->present) {
999 ret = -ENODEV;
1000 goto out;
1003 if (gspca_dev->users > 4) { /* (arbitrary value) */
1004 ret = -EBUSY;
1005 goto out;
1008 /* protect the subdriver against rmmod */
1009 if (!try_module_get(gspca_dev->module)) {
1010 ret = -ENODEV;
1011 goto out;
1014 gspca_dev->users++;
1016 file->private_data = gspca_dev;
1017 #ifdef GSPCA_DEBUG
1018 /* activate the v4l2 debug */
1019 if (gspca_debug & D_V4L2)
1020 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1021 | V4L2_DEBUG_IOCTL_ARG;
1022 else
1023 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1024 | V4L2_DEBUG_IOCTL_ARG);
1025 #endif
1026 ret = 0;
1027 out:
1028 mutex_unlock(&gspca_dev->queue_lock);
1029 if (ret != 0)
1030 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1031 else
1032 PDEBUG(D_STREAM, "open done");
1033 return ret;
1036 static int dev_close(struct file *file)
1038 struct gspca_dev *gspca_dev = file->private_data;
1040 PDEBUG(D_STREAM, "%s close", current->comm);
1041 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1042 return -ERESTARTSYS;
1043 gspca_dev->users--;
1045 /* if the file did the capture, free the streaming resources */
1046 if (gspca_dev->capt_file == file) {
1047 if (gspca_dev->streaming) {
1048 mutex_lock(&gspca_dev->usb_lock);
1049 gspca_stream_off(gspca_dev);
1050 mutex_unlock(&gspca_dev->usb_lock);
1052 frame_free(gspca_dev);
1053 gspca_dev->capt_file = NULL;
1054 gspca_dev->memory = GSPCA_MEMORY_NO;
1056 file->private_data = NULL;
1057 module_put(gspca_dev->module);
1058 mutex_unlock(&gspca_dev->queue_lock);
1060 PDEBUG(D_STREAM, "close done");
1062 return 0;
1065 static int vidioc_querycap(struct file *file, void *priv,
1066 struct v4l2_capability *cap)
1068 struct gspca_dev *gspca_dev = priv;
1069 int ret;
1071 /* protect the access to the usb device */
1072 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1073 return -ERESTARTSYS;
1074 if (!gspca_dev->present) {
1075 ret = -ENODEV;
1076 goto out;
1078 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1079 if (gspca_dev->dev->product != NULL) {
1080 strncpy(cap->card, gspca_dev->dev->product,
1081 sizeof cap->card);
1082 } else {
1083 snprintf(cap->card, sizeof cap->card,
1084 "USB Camera (%04x:%04x)",
1085 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1086 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1088 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1089 cap->version = DRIVER_VERSION_NUMBER;
1090 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1091 | V4L2_CAP_STREAMING
1092 | V4L2_CAP_READWRITE;
1093 ret = 0;
1094 out:
1095 mutex_unlock(&gspca_dev->usb_lock);
1096 return ret;
1099 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1100 int id)
1102 const struct ctrl *ctrls;
1103 int i;
1105 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1106 i < gspca_dev->sd_desc->nctrls;
1107 i++, ctrls++) {
1108 if (gspca_dev->ctrl_dis & (1 << i))
1109 continue;
1110 if (id == ctrls->qctrl.id)
1111 return ctrls;
1113 return NULL;
1116 static int vidioc_queryctrl(struct file *file, void *priv,
1117 struct v4l2_queryctrl *q_ctrl)
1119 struct gspca_dev *gspca_dev = priv;
1120 const struct ctrl *ctrls;
1121 int i;
1122 u32 id;
1124 ctrls = NULL;
1125 id = q_ctrl->id;
1126 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1127 id &= V4L2_CTRL_ID_MASK;
1128 id++;
1129 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1130 if (gspca_dev->ctrl_dis & (1 << i))
1131 continue;
1132 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1133 continue;
1134 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1135 > ctrls->qctrl.id)
1136 continue;
1137 ctrls = &gspca_dev->sd_desc->ctrls[i];
1139 } else {
1140 ctrls = get_ctrl(gspca_dev, id);
1142 if (ctrls == NULL)
1143 return -EINVAL;
1144 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1145 return 0;
1148 static int vidioc_s_ctrl(struct file *file, void *priv,
1149 struct v4l2_control *ctrl)
1151 struct gspca_dev *gspca_dev = priv;
1152 const struct ctrl *ctrls;
1153 int ret;
1155 ctrls = get_ctrl(gspca_dev, ctrl->id);
1156 if (ctrls == NULL)
1157 return -EINVAL;
1159 if (ctrl->value < ctrls->qctrl.minimum
1160 || ctrl->value > ctrls->qctrl.maximum)
1161 return -ERANGE;
1162 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1163 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1164 return -ERESTARTSYS;
1165 if (gspca_dev->present)
1166 ret = ctrls->set(gspca_dev, ctrl->value);
1167 else
1168 ret = -ENODEV;
1169 mutex_unlock(&gspca_dev->usb_lock);
1170 return ret;
1173 static int vidioc_g_ctrl(struct file *file, void *priv,
1174 struct v4l2_control *ctrl)
1176 struct gspca_dev *gspca_dev = priv;
1177 const struct ctrl *ctrls;
1178 int ret;
1180 ctrls = get_ctrl(gspca_dev, ctrl->id);
1181 if (ctrls == NULL)
1182 return -EINVAL;
1184 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1185 return -ERESTARTSYS;
1186 if (gspca_dev->present)
1187 ret = ctrls->get(gspca_dev, &ctrl->value);
1188 else
1189 ret = -ENODEV;
1190 mutex_unlock(&gspca_dev->usb_lock);
1191 return ret;
1194 /*fixme: have an audio flag in gspca_dev?*/
1195 static int vidioc_s_audio(struct file *file, void *priv,
1196 struct v4l2_audio *audio)
1198 if (audio->index != 0)
1199 return -EINVAL;
1200 return 0;
1203 static int vidioc_g_audio(struct file *file, void *priv,
1204 struct v4l2_audio *audio)
1206 strcpy(audio->name, "Microphone");
1207 return 0;
1210 static int vidioc_enumaudio(struct file *file, void *priv,
1211 struct v4l2_audio *audio)
1213 if (audio->index != 0)
1214 return -EINVAL;
1216 strcpy(audio->name, "Microphone");
1217 audio->capability = 0;
1218 audio->mode = 0;
1219 return 0;
1222 static int vidioc_querymenu(struct file *file, void *priv,
1223 struct v4l2_querymenu *qmenu)
1225 struct gspca_dev *gspca_dev = priv;
1227 if (!gspca_dev->sd_desc->querymenu)
1228 return -EINVAL;
1229 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1232 static int vidioc_enum_input(struct file *file, void *priv,
1233 struct v4l2_input *input)
1235 struct gspca_dev *gspca_dev = priv;
1237 if (input->index != 0)
1238 return -EINVAL;
1239 input->type = V4L2_INPUT_TYPE_CAMERA;
1240 input->status = gspca_dev->cam.input_flags;
1241 strncpy(input->name, gspca_dev->sd_desc->name,
1242 sizeof input->name);
1243 return 0;
1246 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1248 *i = 0;
1249 return 0;
1252 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1254 if (i > 0)
1255 return -EINVAL;
1256 return (0);
1259 static int vidioc_reqbufs(struct file *file, void *priv,
1260 struct v4l2_requestbuffers *rb)
1262 struct gspca_dev *gspca_dev = priv;
1263 int i, ret = 0;
1265 switch (rb->memory) {
1266 case GSPCA_MEMORY_READ: /* (internal call) */
1267 case V4L2_MEMORY_MMAP:
1268 case V4L2_MEMORY_USERPTR:
1269 break;
1270 default:
1271 return -EINVAL;
1273 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1274 return -ERESTARTSYS;
1276 if (gspca_dev->memory != GSPCA_MEMORY_NO
1277 && gspca_dev->memory != rb->memory) {
1278 ret = -EBUSY;
1279 goto out;
1282 /* only one file may do the capture */
1283 if (gspca_dev->capt_file != NULL
1284 && gspca_dev->capt_file != file) {
1285 ret = -EBUSY;
1286 goto out;
1289 /* if allocated, the buffers must not be mapped */
1290 for (i = 0; i < gspca_dev->nframes; i++) {
1291 if (gspca_dev->frame[i].vma_use_count) {
1292 ret = -EBUSY;
1293 goto out;
1297 /* stop streaming */
1298 if (gspca_dev->streaming) {
1299 mutex_lock(&gspca_dev->usb_lock);
1300 gspca_stream_off(gspca_dev);
1301 mutex_unlock(&gspca_dev->usb_lock);
1304 /* free the previous allocated buffers, if any */
1305 if (gspca_dev->nframes != 0) {
1306 frame_free(gspca_dev);
1307 gspca_dev->capt_file = NULL;
1309 if (rb->count == 0) /* unrequest */
1310 goto out;
1311 gspca_dev->memory = rb->memory;
1312 ret = frame_alloc(gspca_dev, rb->count);
1313 if (ret == 0) {
1314 rb->count = gspca_dev->nframes;
1315 gspca_dev->capt_file = file;
1317 out:
1318 mutex_unlock(&gspca_dev->queue_lock);
1319 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1320 return ret;
1323 static int vidioc_querybuf(struct file *file, void *priv,
1324 struct v4l2_buffer *v4l2_buf)
1326 struct gspca_dev *gspca_dev = priv;
1327 struct gspca_frame *frame;
1329 if (v4l2_buf->index < 0
1330 || v4l2_buf->index >= gspca_dev->nframes)
1331 return -EINVAL;
1333 frame = &gspca_dev->frame[v4l2_buf->index];
1334 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1335 return 0;
1338 static int vidioc_streamon(struct file *file, void *priv,
1339 enum v4l2_buf_type buf_type)
1341 struct gspca_dev *gspca_dev = priv;
1342 int ret;
1344 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1345 return -EINVAL;
1346 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1347 return -ERESTARTSYS;
1349 if (gspca_dev->nframes == 0
1350 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1351 ret = -EINVAL;
1352 goto out;
1354 if (!gspca_dev->streaming) {
1355 ret = gspca_init_transfer(gspca_dev);
1356 if (ret < 0)
1357 goto out;
1359 #ifdef GSPCA_DEBUG
1360 if (gspca_debug & D_STREAM) {
1361 PDEBUG_MODE("stream on OK",
1362 gspca_dev->pixfmt,
1363 gspca_dev->width,
1364 gspca_dev->height);
1366 #endif
1367 ret = 0;
1368 out:
1369 mutex_unlock(&gspca_dev->queue_lock);
1370 return ret;
1373 static int vidioc_streamoff(struct file *file, void *priv,
1374 enum v4l2_buf_type buf_type)
1376 struct gspca_dev *gspca_dev = priv;
1377 int i, ret;
1379 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1380 return -EINVAL;
1381 if (!gspca_dev->streaming)
1382 return 0;
1383 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1384 return -ERESTARTSYS;
1386 /* stop streaming */
1387 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1388 ret = -ERESTARTSYS;
1389 goto out;
1391 gspca_stream_off(gspca_dev);
1392 mutex_unlock(&gspca_dev->usb_lock);
1394 /* empty the application queues */
1395 for (i = 0; i < gspca_dev->nframes; i++)
1396 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1397 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1398 gspca_dev->last_packet_type = DISCARD_PACKET;
1399 gspca_dev->sequence = 0;
1400 ret = 0;
1401 out:
1402 mutex_unlock(&gspca_dev->queue_lock);
1403 return ret;
1406 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1407 struct v4l2_jpegcompression *jpegcomp)
1409 struct gspca_dev *gspca_dev = priv;
1410 int ret;
1412 if (!gspca_dev->sd_desc->get_jcomp)
1413 return -EINVAL;
1414 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1415 return -ERESTARTSYS;
1416 if (gspca_dev->present)
1417 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1418 else
1419 ret = -ENODEV;
1420 mutex_unlock(&gspca_dev->usb_lock);
1421 return ret;
1424 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1425 struct v4l2_jpegcompression *jpegcomp)
1427 struct gspca_dev *gspca_dev = priv;
1428 int ret;
1430 if (!gspca_dev->sd_desc->set_jcomp)
1431 return -EINVAL;
1432 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1433 return -ERESTARTSYS;
1434 if (gspca_dev->present)
1435 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1436 else
1437 ret = -ENODEV;
1438 mutex_unlock(&gspca_dev->usb_lock);
1439 return ret;
1442 static int vidioc_g_parm(struct file *filp, void *priv,
1443 struct v4l2_streamparm *parm)
1445 struct gspca_dev *gspca_dev = priv;
1447 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1449 if (gspca_dev->sd_desc->get_streamparm) {
1450 int ret;
1452 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1453 return -ERESTARTSYS;
1454 if (gspca_dev->present)
1455 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1456 parm);
1457 else
1458 ret = -ENODEV;
1459 mutex_unlock(&gspca_dev->usb_lock);
1460 return ret;
1463 return 0;
1466 static int vidioc_s_parm(struct file *filp, void *priv,
1467 struct v4l2_streamparm *parm)
1469 struct gspca_dev *gspca_dev = priv;
1470 int n;
1472 n = parm->parm.capture.readbuffers;
1473 if (n == 0 || n > GSPCA_MAX_FRAMES)
1474 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1475 else
1476 gspca_dev->nbufread = n;
1478 if (gspca_dev->sd_desc->set_streamparm) {
1479 int ret;
1481 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1482 return -ERESTARTSYS;
1483 if (gspca_dev->present)
1484 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1485 parm);
1486 else
1487 ret = -ENODEV;
1488 mutex_unlock(&gspca_dev->usb_lock);
1489 return ret;
1492 return 0;
1495 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1496 static int vidiocgmbuf(struct file *file, void *priv,
1497 struct video_mbuf *mbuf)
1499 struct gspca_dev *gspca_dev = file->private_data;
1500 int i;
1502 PDEBUG(D_STREAM, "cgmbuf");
1503 if (gspca_dev->nframes == 0) {
1504 int ret;
1507 struct v4l2_format fmt;
1509 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1510 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1511 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1512 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1513 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1514 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1515 if (ret != 0)
1516 return ret;
1519 struct v4l2_requestbuffers rb;
1521 memset(&rb, 0, sizeof rb);
1522 rb.count = 4;
1523 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1524 rb.memory = V4L2_MEMORY_MMAP;
1525 ret = vidioc_reqbufs(file, priv, &rb);
1526 if (ret != 0)
1527 return ret;
1530 mbuf->frames = gspca_dev->nframes;
1531 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1532 for (i = 0; i < mbuf->frames; i++)
1533 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1534 return 0;
1536 #endif
1538 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1540 struct gspca_dev *gspca_dev = file->private_data;
1541 struct gspca_frame *frame;
1542 struct page *page;
1543 unsigned long addr, start, size;
1544 int i, ret;
1546 start = vma->vm_start;
1547 size = vma->vm_end - vma->vm_start;
1548 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1550 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1551 return -ERESTARTSYS;
1552 if (!gspca_dev->present) {
1553 ret = -ENODEV;
1554 goto out;
1556 if (gspca_dev->capt_file != file) {
1557 ret = -EINVAL;
1558 goto out;
1561 frame = NULL;
1562 for (i = 0; i < gspca_dev->nframes; ++i) {
1563 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1564 PDEBUG(D_STREAM, "mmap bad memory type");
1565 break;
1567 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1568 == vma->vm_pgoff) {
1569 frame = &gspca_dev->frame[i];
1570 break;
1573 if (frame == NULL) {
1574 PDEBUG(D_STREAM, "mmap no frame buffer found");
1575 ret = -EINVAL;
1576 goto out;
1578 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1579 /* v4l1 maps all the buffers */
1580 if (i != 0
1581 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1582 #endif
1583 if (size != frame->v4l2_buf.length) {
1584 PDEBUG(D_STREAM, "mmap bad size");
1585 ret = -EINVAL;
1586 goto out;
1590 * - VM_IO marks the area as being a mmaped region for I/O to a
1591 * device. It also prevents the region from being core dumped.
1593 vma->vm_flags |= VM_IO;
1595 addr = (unsigned long) frame->data;
1596 while (size > 0) {
1597 page = vmalloc_to_page((void *) addr);
1598 ret = vm_insert_page(vma, start, page);
1599 if (ret < 0)
1600 goto out;
1601 start += PAGE_SIZE;
1602 addr += PAGE_SIZE;
1603 size -= PAGE_SIZE;
1606 vma->vm_ops = &gspca_vm_ops;
1607 vma->vm_private_data = frame;
1608 gspca_vm_open(vma);
1609 ret = 0;
1610 out:
1611 mutex_unlock(&gspca_dev->queue_lock);
1612 return ret;
1616 * wait for a video frame
1618 * If a frame is ready, its index is returned.
1620 static int frame_wait(struct gspca_dev *gspca_dev,
1621 int nonblock_ing)
1623 struct gspca_frame *frame;
1624 int i, j, ret;
1626 /* check if a frame is ready */
1627 i = gspca_dev->fr_o;
1628 j = gspca_dev->fr_queue[i];
1629 frame = &gspca_dev->frame[j];
1631 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1632 if (nonblock_ing)
1633 return -EAGAIN;
1635 /* wait till a frame is ready */
1636 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1637 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1638 !gspca_dev->streaming || !gspca_dev->present,
1639 msecs_to_jiffies(3000));
1640 if (ret < 0)
1641 return ret;
1642 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1643 return -EIO;
1646 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1647 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1648 gspca_dev->fr_q,
1649 gspca_dev->fr_i,
1650 gspca_dev->fr_o);
1652 if (gspca_dev->sd_desc->dq_callback) {
1653 mutex_lock(&gspca_dev->usb_lock);
1654 if (gspca_dev->present)
1655 gspca_dev->sd_desc->dq_callback(gspca_dev);
1656 mutex_unlock(&gspca_dev->usb_lock);
1658 return j;
1662 * dequeue a video buffer
1664 * If nonblock_ing is false, block until a buffer is available.
1666 static int vidioc_dqbuf(struct file *file, void *priv,
1667 struct v4l2_buffer *v4l2_buf)
1669 struct gspca_dev *gspca_dev = priv;
1670 struct gspca_frame *frame;
1671 int i, ret;
1673 PDEBUG(D_FRAM, "dqbuf");
1674 if (v4l2_buf->memory != gspca_dev->memory)
1675 return -EINVAL;
1677 if (!gspca_dev->present)
1678 return -ENODEV;
1680 /* if not streaming, be sure the application will not loop forever */
1681 if (!(file->f_flags & O_NONBLOCK)
1682 && !gspca_dev->streaming && gspca_dev->users == 1)
1683 return -EINVAL;
1685 /* only the capturing file may dequeue */
1686 if (gspca_dev->capt_file != file)
1687 return -EINVAL;
1689 /* only one dequeue / read at a time */
1690 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1691 return -ERESTARTSYS;
1693 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1694 if (ret < 0)
1695 goto out;
1696 i = ret; /* frame index */
1697 frame = &gspca_dev->frame[i];
1698 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1699 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1700 frame->data,
1701 frame->v4l2_buf.bytesused)) {
1702 PDEBUG(D_ERR|D_STREAM,
1703 "dqbuf cp to user failed");
1704 ret = -EFAULT;
1705 goto out;
1708 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1709 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1710 PDEBUG(D_FRAM, "dqbuf %d", i);
1711 ret = 0;
1712 out:
1713 mutex_unlock(&gspca_dev->read_lock);
1714 return ret;
1718 * queue a video buffer
1720 * Attempting to queue a buffer that has already been
1721 * queued will return -EINVAL.
1723 static int vidioc_qbuf(struct file *file, void *priv,
1724 struct v4l2_buffer *v4l2_buf)
1726 struct gspca_dev *gspca_dev = priv;
1727 struct gspca_frame *frame;
1728 int i, index, ret;
1730 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1732 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1733 return -ERESTARTSYS;
1735 index = v4l2_buf->index;
1736 if ((unsigned) index >= gspca_dev->nframes) {
1737 PDEBUG(D_FRAM,
1738 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1739 ret = -EINVAL;
1740 goto out;
1742 if (v4l2_buf->memory != gspca_dev->memory) {
1743 PDEBUG(D_FRAM, "qbuf bad memory type");
1744 ret = -EINVAL;
1745 goto out;
1748 frame = &gspca_dev->frame[index];
1749 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1750 PDEBUG(D_FRAM, "qbuf bad state");
1751 ret = -EINVAL;
1752 goto out;
1755 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1757 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1758 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1759 frame->v4l2_buf.length = v4l2_buf->length;
1762 /* put the buffer in the 'queued' queue */
1763 i = gspca_dev->fr_q;
1764 gspca_dev->fr_queue[i] = index;
1765 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1766 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1767 gspca_dev->fr_q,
1768 gspca_dev->fr_i,
1769 gspca_dev->fr_o);
1771 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1772 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1773 ret = 0;
1774 out:
1775 mutex_unlock(&gspca_dev->queue_lock);
1776 return ret;
1780 * allocate the resources for read()
1782 static int read_alloc(struct gspca_dev *gspca_dev,
1783 struct file *file)
1785 struct v4l2_buffer v4l2_buf;
1786 int i, ret;
1788 PDEBUG(D_STREAM, "read alloc");
1789 if (gspca_dev->nframes == 0) {
1790 struct v4l2_requestbuffers rb;
1792 memset(&rb, 0, sizeof rb);
1793 rb.count = gspca_dev->nbufread;
1794 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1795 rb.memory = GSPCA_MEMORY_READ;
1796 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1797 if (ret != 0) {
1798 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1799 return ret;
1801 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1802 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1803 v4l2_buf.memory = GSPCA_MEMORY_READ;
1804 for (i = 0; i < gspca_dev->nbufread; i++) {
1805 v4l2_buf.index = i;
1806 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1807 if (ret != 0) {
1808 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1809 return ret;
1812 gspca_dev->memory = GSPCA_MEMORY_READ;
1815 /* start streaming */
1816 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1817 if (ret != 0)
1818 PDEBUG(D_STREAM, "read streamon err %d", ret);
1819 return ret;
1822 static unsigned int dev_poll(struct file *file, poll_table *wait)
1824 struct gspca_dev *gspca_dev = file->private_data;
1825 int i, ret;
1827 PDEBUG(D_FRAM, "poll");
1829 poll_wait(file, &gspca_dev->wq, wait);
1831 /* if reqbufs is not done, the user would use read() */
1832 if (gspca_dev->nframes == 0) {
1833 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1834 return POLLERR; /* not the 1st time */
1835 ret = read_alloc(gspca_dev, file);
1836 if (ret != 0)
1837 return POLLERR;
1840 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1841 return POLLERR;
1843 /* check the next incoming buffer */
1844 i = gspca_dev->fr_o;
1845 i = gspca_dev->fr_queue[i];
1846 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1847 ret = POLLIN | POLLRDNORM; /* something to read */
1848 else
1849 ret = 0;
1850 mutex_unlock(&gspca_dev->queue_lock);
1851 if (!gspca_dev->present)
1852 return POLLHUP;
1853 return ret;
1856 static ssize_t dev_read(struct file *file, char __user *data,
1857 size_t count, loff_t *ppos)
1859 struct gspca_dev *gspca_dev = file->private_data;
1860 struct gspca_frame *frame;
1861 struct v4l2_buffer v4l2_buf;
1862 struct timeval timestamp;
1863 int n, ret, ret2;
1865 PDEBUG(D_FRAM, "read (%zd)", count);
1866 if (!gspca_dev->present)
1867 return -ENODEV;
1868 switch (gspca_dev->memory) {
1869 case GSPCA_MEMORY_NO: /* first time */
1870 ret = read_alloc(gspca_dev, file);
1871 if (ret != 0)
1872 return ret;
1873 break;
1874 case GSPCA_MEMORY_READ:
1875 if (gspca_dev->capt_file == file)
1876 break;
1877 /* fall thru */
1878 default:
1879 return -EINVAL;
1882 /* get a frame */
1883 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1884 timestamp.tv_sec--;
1885 n = 2;
1886 for (;;) {
1887 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1888 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1889 v4l2_buf.memory = GSPCA_MEMORY_READ;
1890 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1891 if (ret != 0) {
1892 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1893 return ret;
1896 /* if the process slept for more than 1 second,
1897 * get a newer frame */
1898 frame = &gspca_dev->frame[v4l2_buf.index];
1899 if (--n < 0)
1900 break; /* avoid infinite loop */
1901 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1902 break;
1903 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1904 if (ret != 0) {
1905 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1906 return ret;
1910 /* copy the frame */
1911 if (count > frame->v4l2_buf.bytesused)
1912 count = frame->v4l2_buf.bytesused;
1913 ret = copy_to_user(data, frame->data, count);
1914 if (ret != 0) {
1915 PDEBUG(D_ERR|D_STREAM,
1916 "read cp to user lack %d / %zd", ret, count);
1917 ret = -EFAULT;
1918 goto out;
1920 ret = count;
1921 out:
1922 /* in each case, requeue the buffer */
1923 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1924 if (ret2 != 0)
1925 return ret2;
1926 return ret;
1929 static struct v4l2_file_operations dev_fops = {
1930 .owner = THIS_MODULE,
1931 .open = dev_open,
1932 .release = dev_close,
1933 .read = dev_read,
1934 .mmap = dev_mmap,
1935 .unlocked_ioctl = video_ioctl2,
1936 .poll = dev_poll,
1939 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1940 .vidioc_querycap = vidioc_querycap,
1941 .vidioc_dqbuf = vidioc_dqbuf,
1942 .vidioc_qbuf = vidioc_qbuf,
1943 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1944 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1945 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1946 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1947 .vidioc_streamon = vidioc_streamon,
1948 .vidioc_queryctrl = vidioc_queryctrl,
1949 .vidioc_g_ctrl = vidioc_g_ctrl,
1950 .vidioc_s_ctrl = vidioc_s_ctrl,
1951 .vidioc_g_audio = vidioc_g_audio,
1952 .vidioc_s_audio = vidioc_s_audio,
1953 .vidioc_enumaudio = vidioc_enumaudio,
1954 .vidioc_querymenu = vidioc_querymenu,
1955 .vidioc_enum_input = vidioc_enum_input,
1956 .vidioc_g_input = vidioc_g_input,
1957 .vidioc_s_input = vidioc_s_input,
1958 .vidioc_reqbufs = vidioc_reqbufs,
1959 .vidioc_querybuf = vidioc_querybuf,
1960 .vidioc_streamoff = vidioc_streamoff,
1961 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1962 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1963 .vidioc_g_parm = vidioc_g_parm,
1964 .vidioc_s_parm = vidioc_s_parm,
1965 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1966 #ifdef CONFIG_VIDEO_ADV_DEBUG
1967 .vidioc_g_register = vidioc_g_register,
1968 .vidioc_s_register = vidioc_s_register,
1969 #endif
1970 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1971 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1972 .vidiocgmbuf = vidiocgmbuf,
1973 #endif
1976 static struct video_device gspca_template = {
1977 .name = "gspca main driver",
1978 .fops = &dev_fops,
1979 .ioctl_ops = &dev_ioctl_ops,
1980 .release = gspca_release,
1981 .minor = -1,
1985 * probe and create a new gspca device
1987 * This function must be called by the sub-driver when it is
1988 * called for probing a new device.
1990 int gspca_dev_probe(struct usb_interface *intf,
1991 const struct usb_device_id *id,
1992 const struct sd_desc *sd_desc,
1993 int dev_size,
1994 struct module *module)
1996 struct usb_interface_descriptor *interface;
1997 struct gspca_dev *gspca_dev;
1998 struct usb_device *dev = interface_to_usbdev(intf);
1999 int ret;
2001 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2003 /* we don't handle multi-config cameras */
2004 if (dev->descriptor.bNumConfigurations != 1)
2005 return -ENODEV;
2006 interface = &intf->cur_altsetting->desc;
2007 if (interface->bInterfaceNumber > 0)
2008 return -ENODEV;
2010 /* create the device */
2011 if (dev_size < sizeof *gspca_dev)
2012 dev_size = sizeof *gspca_dev;
2013 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2014 if (!gspca_dev) {
2015 err("couldn't kzalloc gspca struct");
2016 return -ENOMEM;
2018 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2019 if (!gspca_dev->usb_buf) {
2020 err("out of memory");
2021 ret = -ENOMEM;
2022 goto out;
2024 gspca_dev->dev = dev;
2025 gspca_dev->iface = interface->bInterfaceNumber;
2026 gspca_dev->nbalt = intf->num_altsetting;
2027 gspca_dev->sd_desc = sd_desc;
2028 gspca_dev->nbufread = 2;
2029 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2031 /* configure the subdriver and initialize the USB device */
2032 ret = sd_desc->config(gspca_dev, id);
2033 if (ret < 0)
2034 goto out;
2035 ret = sd_desc->init(gspca_dev);
2036 if (ret < 0)
2037 goto out;
2038 ret = gspca_set_alt0(gspca_dev);
2039 if (ret < 0)
2040 goto out;
2041 gspca_set_default_mode(gspca_dev);
2043 mutex_init(&gspca_dev->usb_lock);
2044 mutex_init(&gspca_dev->read_lock);
2045 mutex_init(&gspca_dev->queue_lock);
2046 init_waitqueue_head(&gspca_dev->wq);
2048 /* init video stuff */
2049 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2050 gspca_dev->vdev.parent = &intf->dev;
2051 gspca_dev->module = module;
2052 gspca_dev->present = 1;
2053 ret = video_register_device(&gspca_dev->vdev,
2054 VFL_TYPE_GRABBER,
2055 -1);
2056 if (ret < 0) {
2057 err("video_register_device err %d", ret);
2058 goto out;
2061 usb_set_intfdata(intf, gspca_dev);
2062 PDEBUG(D_PROBE, "probe ok");
2063 return 0;
2064 out:
2065 kfree(gspca_dev->usb_buf);
2066 kfree(gspca_dev);
2067 return ret;
2069 EXPORT_SYMBOL(gspca_dev_probe);
2072 * USB disconnection
2074 * This function must be called by the sub-driver
2075 * when the device disconnects, after the specific resources are freed.
2077 void gspca_disconnect(struct usb_interface *intf)
2079 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2081 mutex_lock(&gspca_dev->usb_lock);
2082 gspca_dev->present = 0;
2084 if (gspca_dev->streaming) {
2085 destroy_urbs(gspca_dev);
2086 wake_up_interruptible(&gspca_dev->wq);
2089 /* the device is freed at exit of this function */
2090 gspca_dev->dev = NULL;
2091 mutex_unlock(&gspca_dev->usb_lock);
2093 usb_set_intfdata(intf, NULL);
2095 /* release the device */
2096 /* (this will call gspca_release() immediatly or on last close) */
2097 video_unregister_device(&gspca_dev->vdev);
2099 PDEBUG(D_PROBE, "disconnect complete");
2101 EXPORT_SYMBOL(gspca_disconnect);
2103 #ifdef CONFIG_PM
2104 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2106 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2108 if (!gspca_dev->streaming)
2109 return 0;
2110 gspca_dev->frozen = 1; /* avoid urb error messages */
2111 if (gspca_dev->sd_desc->stopN)
2112 gspca_dev->sd_desc->stopN(gspca_dev);
2113 destroy_urbs(gspca_dev);
2114 gspca_set_alt0(gspca_dev);
2115 if (gspca_dev->sd_desc->stop0)
2116 gspca_dev->sd_desc->stop0(gspca_dev);
2117 return 0;
2119 EXPORT_SYMBOL(gspca_suspend);
2121 int gspca_resume(struct usb_interface *intf)
2123 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2125 gspca_dev->frozen = 0;
2126 gspca_dev->sd_desc->init(gspca_dev);
2127 if (gspca_dev->streaming)
2128 return gspca_init_transfer(gspca_dev);
2129 return 0;
2131 EXPORT_SYMBOL(gspca_resume);
2132 #endif
2133 /* -- cam driver utility functions -- */
2135 /* auto gain and exposure algorithm based on the knee algorithm described here:
2136 http://ytse.tricolour.net/docs/LowLightOptimization.html
2138 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2139 where changed. */
2140 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2141 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2143 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2144 const struct ctrl *gain_ctrl = NULL;
2145 const struct ctrl *exposure_ctrl = NULL;
2146 const struct ctrl *autogain_ctrl = NULL;
2147 int retval = 0;
2149 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2150 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2151 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2152 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2153 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2154 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2155 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2157 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2158 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2159 "on cam without (auto)gain/exposure");
2160 return 0;
2163 if (gain_ctrl->get(gspca_dev, &gain) ||
2164 exposure_ctrl->get(gspca_dev, &exposure) ||
2165 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2166 return 0;
2168 orig_gain = gain;
2169 orig_exposure = exposure;
2171 /* If we are of a multiple of deadzone, do multiple steps to reach the
2172 desired lumination fast (with the risc of a slight overshoot) */
2173 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2175 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2176 avg_lum, desired_avg_lum, steps);
2178 for (i = 0; i < steps; i++) {
2179 if (avg_lum > desired_avg_lum) {
2180 if (gain > gain_knee)
2181 gain--;
2182 else if (exposure > exposure_knee)
2183 exposure--;
2184 else if (gain > gain_ctrl->qctrl.default_value)
2185 gain--;
2186 else if (exposure > exposure_ctrl->qctrl.minimum)
2187 exposure--;
2188 else if (gain > gain_ctrl->qctrl.minimum)
2189 gain--;
2190 else
2191 break;
2192 } else {
2193 if (gain < gain_ctrl->qctrl.default_value)
2194 gain++;
2195 else if (exposure < exposure_knee)
2196 exposure++;
2197 else if (gain < gain_knee)
2198 gain++;
2199 else if (exposure < exposure_ctrl->qctrl.maximum)
2200 exposure++;
2201 else if (gain < gain_ctrl->qctrl.maximum)
2202 gain++;
2203 else
2204 break;
2208 if (gain != orig_gain) {
2209 gain_ctrl->set(gspca_dev, gain);
2210 retval = 1;
2212 if (exposure != orig_exposure) {
2213 exposure_ctrl->set(gspca_dev, exposure);
2214 retval = 1;
2217 return retval;
2219 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2221 /* -- module insert / remove -- */
2222 static int __init gspca_init(void)
2224 info("main v%d.%d.%d registered",
2225 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2226 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2227 DRIVER_VERSION_NUMBER & 0xff);
2228 return 0;
2230 static void __exit gspca_exit(void)
2232 info("main deregistered");
2235 module_init(gspca_init);
2236 module_exit(gspca_exit);
2238 #ifdef GSPCA_DEBUG
2239 module_param_named(debug, gspca_debug, int, 0644);
2240 MODULE_PARM_DESC(debug,
2241 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2242 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2243 " 0x0100: v4l2");
2244 #endif