2 * Main USB camera driver
4 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define MODULE_NAME "gspca"
26 #include <linux/init.h>
27 #include <linux/version.h>
29 #include <linux/vmalloc.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
37 #include <linux/uaccess.h>
38 #include <linux/ktime.h>
39 #include <media/v4l2-ioctl.h>
43 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
44 #include <linux/input.h>
45 #include <linux/usb/input.h>
49 #define DEF_NURBS 3 /* default number of URBs */
50 #if DEF_NURBS > MAX_NURBS
51 #error "DEF_NURBS too big"
54 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0)
61 int gspca_debug = D_ERR | D_PROBE;
62 EXPORT_SYMBOL(gspca_debug);
64 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
71 (pixfmt >> 16) & 0xff,
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102 static void gspca_vm_close(struct vm_area_struct *vma)
104 struct gspca_frame *frame = vma->vm_private_data;
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110 static const struct vm_operations_struct gspca_vm_ops = {
111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb *urb)
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
151 PDEBUG(D_ERR, "Resubmit URB failed with error %i", ret);
155 static int gspca_input_connect(struct gspca_dev *dev)
157 struct input_dev *input_dev;
160 dev->input_dev = NULL;
161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
162 input_dev = input_allocate_device();
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
172 usb_to_input_id(dev->dev, &input_dev->id);
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
178 err = input_register_device(input_dev);
180 PDEBUG(D_ERR, "Input device registration failed "
181 "with error %i", err);
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
185 dev->input_dev = input_dev;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
198 struct usb_device *dev;
202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
203 interval = ep->bInterval;
204 PDEBUG(D_PROBE, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
208 dev = gspca_dev->dev;
210 urb = usb_alloc_urb(0, GFP_KERNEL);
216 buffer = usb_alloc_coherent(dev, buffer_len,
217 GFP_KERNEL, &urb->transfer_dma);
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
225 int_irq, (void *)gspca_dev, interval);
226 gspca_dev->int_urb = urb;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
229 PDEBUG(D_ERR, "submit URB failed with error %i", ret);
235 usb_free_coherent(dev,
236 urb->transfer_buffer_length,
237 urb->transfer_buffer,
245 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
247 struct usb_interface *intf;
248 struct usb_host_interface *intf_desc;
249 struct usb_endpoint_descriptor *ep;
252 if (gspca_dev->sd_desc->int_pkt_scan) {
253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
254 intf_desc = intf->cur_altsetting;
255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
256 ep = &intf_desc->endpoint[i].desc;
257 if (usb_endpoint_dir_in(ep) &&
258 usb_endpoint_xfer_int(ep)) {
260 alloc_and_submit_int_urb(gspca_dev, ep);
267 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
271 urb = gspca_dev->int_urb;
273 gspca_dev->int_urb = NULL;
275 usb_free_coherent(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
283 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
287 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
291 static inline int gspca_input_connect(struct gspca_dev *dev)
298 * fill a video frame from an URB and resubmit
300 static void fill_frame(struct gspca_dev *gspca_dev,
303 u8 *data; /* address of data in the iso message */
307 if (urb->status != 0) {
308 if (urb->status == -ESHUTDOWN)
309 return; /* disconnection */
311 if (gspca_dev->frozen)
314 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
318 if (gspca_dev->image == NULL)
319 gspca_dev->last_packet_type = DISCARD_PACKET;
320 pkt_scan = gspca_dev->sd_desc->pkt_scan;
321 for (i = 0; i < urb->number_of_packets; i++) {
323 /* check the packet status and length */
324 len = urb->iso_frame_desc[i].actual_length;
326 if (gspca_dev->empty_packet == 0)
327 gspca_dev->empty_packet = 1;
330 st = urb->iso_frame_desc[i].status;
333 "ISOC data error: [%d] len=%d, status=%d",
335 gspca_dev->last_packet_type = DISCARD_PACKET;
339 /* let the packet be analyzed by the subdriver */
340 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
341 i, urb->iso_frame_desc[i].offset, len);
342 data = (u8 *) urb->transfer_buffer
343 + urb->iso_frame_desc[i].offset;
344 pkt_scan(gspca_dev, data, len);
348 /* resubmit the URB */
349 st = usb_submit_urb(urb, GFP_ATOMIC);
351 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
355 * ISOC message interrupt from the USB device
357 * Analyse each packet and call the subdriver for copy to the frame buffer.
359 static void isoc_irq(struct urb *urb)
361 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
363 PDEBUG(D_PACK, "isoc irq");
364 if (!gspca_dev->streaming)
366 fill_frame(gspca_dev, urb);
370 * bulk message interrupt from the USB device
372 static void bulk_irq(struct urb *urb)
374 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
377 PDEBUG(D_PACK, "bulk irq");
378 if (!gspca_dev->streaming)
380 switch (urb->status) {
384 return; /* disconnection */
387 if (gspca_dev->frozen)
390 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
395 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
396 gspca_dev->sd_desc->pkt_scan(gspca_dev,
397 urb->transfer_buffer,
401 /* resubmit the URB */
402 if (gspca_dev->cam.bulk_nurbs != 0) {
403 st = usb_submit_urb(urb, GFP_ATOMIC);
405 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
410 * add data to the current frame
412 * This function is called by the subdrivers at interrupt level.
414 * To build a frame, these ones must add
416 * - 0 or many INTER_PACKETs
418 * DISCARD_PACKET invalidates the whole frame.
419 * On LAST_PACKET, a new frame is returned.
421 void gspca_frame_add(struct gspca_dev *gspca_dev,
422 enum gspca_packet_type packet_type,
426 struct gspca_frame *frame;
429 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
431 /* check the availability of the frame buffer */
432 if (gspca_dev->image == NULL)
435 if (packet_type == FIRST_PACKET) {
437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
439 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
440 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
441 gspca_dev->image_len = 0;
442 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
443 if (packet_type == LAST_PACKET)
444 gspca_dev->last_packet_type = packet_type;
448 /* append the packet to the frame buffer */
450 if (gspca_dev->image_len + len > gspca_dev->frsz) {
451 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
452 gspca_dev->image_len + len,
454 packet_type = DISCARD_PACKET;
456 memcpy(gspca_dev->image + gspca_dev->image_len,
458 gspca_dev->image_len += len;
461 gspca_dev->last_packet_type = packet_type;
463 /* if last packet, wake up the application and advance in the queue */
464 if (packet_type == LAST_PACKET) {
466 j = gspca_dev->fr_queue[i];
467 frame = &gspca_dev->frame[j];
468 frame->v4l2_buf.bytesused = gspca_dev->image_len;
469 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
470 | V4L2_BUF_FLAG_DONE)
471 & ~V4L2_BUF_FLAG_QUEUED;
472 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
473 i = (i + 1) % gspca_dev->nframes;
475 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
476 frame->v4l2_buf.bytesused,
480 j = gspca_dev->fr_queue[i];
481 frame = &gspca_dev->frame[j];
482 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
483 == V4L2_BUF_FLAG_QUEUED) {
484 gspca_dev->image = frame->data;
486 gspca_dev->image = NULL;
490 EXPORT_SYMBOL(gspca_frame_add);
492 static int gspca_is_compressed(__u32 format)
495 case V4L2_PIX_FMT_MJPEG:
496 case V4L2_PIX_FMT_JPEG:
497 case V4L2_PIX_FMT_SPCA561:
498 case V4L2_PIX_FMT_PAC207:
499 case V4L2_PIX_FMT_MR97310A:
505 static int frame_alloc(struct gspca_dev *gspca_dev,
508 struct gspca_frame *frame;
512 i = gspca_dev->curr_mode;
513 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
514 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
515 frsz = PAGE_ALIGN(frsz);
516 gspca_dev->frsz = frsz;
517 if (count > GSPCA_MAX_FRAMES)
518 count = GSPCA_MAX_FRAMES;
519 gspca_dev->frbuf = vmalloc_32(frsz * count);
520 if (!gspca_dev->frbuf) {
521 err("frame alloc failed");
524 gspca_dev->nframes = count;
525 for (i = 0; i < count; i++) {
526 frame = &gspca_dev->frame[i];
527 frame->v4l2_buf.index = i;
528 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 frame->v4l2_buf.flags = 0;
530 frame->v4l2_buf.field = V4L2_FIELD_NONE;
531 frame->v4l2_buf.length = frsz;
532 frame->v4l2_buf.memory = gspca_dev->memory;
533 frame->v4l2_buf.sequence = 0;
534 frame->data = gspca_dev->frbuf + i * frsz;
535 frame->v4l2_buf.m.offset = i * frsz;
537 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
538 gspca_dev->image = NULL;
539 gspca_dev->image_len = 0;
540 gspca_dev->last_packet_type = DISCARD_PACKET;
541 gspca_dev->sequence = 0;
545 static void frame_free(struct gspca_dev *gspca_dev)
549 PDEBUG(D_STREAM, "frame free");
550 if (gspca_dev->frbuf != NULL) {
551 vfree(gspca_dev->frbuf);
552 gspca_dev->frbuf = NULL;
553 for (i = 0; i < gspca_dev->nframes; i++)
554 gspca_dev->frame[i].data = NULL;
556 gspca_dev->nframes = 0;
559 static void destroy_urbs(struct gspca_dev *gspca_dev)
564 PDEBUG(D_STREAM, "kill transfer");
565 for (i = 0; i < MAX_NURBS; i++) {
566 urb = gspca_dev->urb[i];
570 gspca_dev->urb[i] = NULL;
572 if (urb->transfer_buffer != NULL)
573 usb_free_coherent(gspca_dev->dev,
574 urb->transfer_buffer_length,
575 urb->transfer_buffer,
581 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
585 if (gspca_dev->alt == 0)
587 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
589 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
593 /* Note: both the queue and the usb locks should be held when calling this */
594 static void gspca_stream_off(struct gspca_dev *gspca_dev)
596 gspca_dev->streaming = 0;
597 if (gspca_dev->present) {
598 if (gspca_dev->sd_desc->stopN)
599 gspca_dev->sd_desc->stopN(gspca_dev);
600 destroy_urbs(gspca_dev);
601 gspca_input_destroy_urb(gspca_dev);
602 gspca_set_alt0(gspca_dev);
603 gspca_input_create_urb(gspca_dev);
606 /* always call stop0 to free the subdriver's resources */
607 if (gspca_dev->sd_desc->stop0)
608 gspca_dev->sd_desc->stop0(gspca_dev);
609 PDEBUG(D_STREAM, "stream off OK");
613 * look for an input transfer endpoint in an alternate setting
615 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
618 struct usb_host_endpoint *ep;
621 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
622 ep = &alt->endpoint[i];
623 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
625 && ep->desc.wMaxPacketSize != 0)
632 * look for an input (isoc or bulk) endpoint
634 * The endpoint is defined by the subdriver.
635 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
636 * This routine may be called many times when the bandwidth is too small
637 * (the bandwidth is checked on urb submit).
639 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
641 struct usb_interface *intf;
642 struct usb_host_endpoint *ep;
645 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
647 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
648 : USB_ENDPOINT_XFER_ISOC;
649 i = gspca_dev->alt; /* previous alt setting */
650 if (gspca_dev->cam.reverse_alts) {
651 while (++i < gspca_dev->nbalt) {
652 ep = alt_xfer(&intf->altsetting[i], xfer);
658 ep = alt_xfer(&intf->altsetting[i], xfer);
664 err("no transfer endpoint found");
667 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
668 i, ep->desc.bEndpointAddress);
669 gspca_dev->alt = i; /* memorize the current alt setting */
670 if (gspca_dev->nbalt > 1) {
671 gspca_input_destroy_urb(gspca_dev);
672 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
674 err("set alt %d err %d", i, ret);
677 gspca_input_create_urb(gspca_dev);
683 * create the URBs for image transfer
685 static int create_urbs(struct gspca_dev *gspca_dev,
686 struct usb_host_endpoint *ep)
689 int n, nurbs, i, psize, npkt, bsize;
691 /* calculate the packet size and the number of packets */
692 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
694 if (!gspca_dev->cam.bulk) { /* isoc */
696 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
697 if (gspca_dev->pkt_size == 0)
698 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
700 psize = gspca_dev->pkt_size;
701 npkt = gspca_dev->cam.npkt;
703 npkt = 32; /* default value */
704 bsize = psize * npkt;
706 "isoc %d pkts size %d = bsize:%d",
711 bsize = gspca_dev->cam.bulk_size;
714 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
715 if (gspca_dev->cam.bulk_nurbs != 0)
716 nurbs = gspca_dev->cam.bulk_nurbs;
721 for (n = 0; n < nurbs; n++) {
722 urb = usb_alloc_urb(npkt, GFP_KERNEL);
724 err("usb_alloc_urb failed");
727 gspca_dev->urb[n] = urb;
728 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
733 if (urb->transfer_buffer == NULL) {
734 err("usb_alloc_coherent failed");
737 urb->dev = gspca_dev->dev;
738 urb->context = gspca_dev;
739 urb->transfer_buffer_length = bsize;
740 if (npkt != 0) { /* ISOC */
741 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
742 ep->desc.bEndpointAddress);
743 urb->transfer_flags = URB_ISO_ASAP
744 | URB_NO_TRANSFER_DMA_MAP;
745 urb->interval = ep->desc.bInterval;
746 urb->complete = isoc_irq;
747 urb->number_of_packets = npkt;
748 for (i = 0; i < npkt; i++) {
749 urb->iso_frame_desc[i].length = psize;
750 urb->iso_frame_desc[i].offset = psize * i;
753 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
754 ep->desc.bEndpointAddress),
755 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
756 urb->complete = bulk_irq;
763 * start the USB transfer
765 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
767 struct usb_host_endpoint *ep;
771 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
774 if (!gspca_dev->present) {
779 gspca_dev->usb_err = 0;
781 /* set the higher alternate setting and
782 * loop until urb submit succeeds */
783 if (gspca_dev->cam.reverse_alts)
786 gspca_dev->alt = gspca_dev->nbalt;
788 if (gspca_dev->sd_desc->isoc_init) {
789 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
793 ep = get_ep(gspca_dev);
799 if (!gspca_dev->cam.no_urb_create) {
800 PDEBUG(D_STREAM, "init transfer alt %d",
802 ret = create_urbs(gspca_dev, ep);
804 destroy_urbs(gspca_dev);
809 /* clear the bulk endpoint */
810 if (gspca_dev->cam.bulk)
811 usb_clear_halt(gspca_dev->dev,
812 gspca_dev->urb[0]->pipe);
815 ret = gspca_dev->sd_desc->start(gspca_dev);
817 destroy_urbs(gspca_dev);
820 gspca_dev->streaming = 1;
822 /* some bulk transfers are started by the subdriver */
823 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
826 /* submit the URBs */
827 for (n = 0; n < MAX_NURBS; n++) {
828 urb = gspca_dev->urb[n];
831 ret = usb_submit_urb(urb, GFP_KERNEL);
837 gspca_stream_off(gspca_dev);
838 if (ret != -ENOSPC) {
839 PDEBUG(D_ERR|D_STREAM,
840 "usb_submit_urb alt %d err %d",
841 gspca_dev->alt, ret);
845 /* the bandwidth is not wide enough
846 * negociate or try a lower alternate setting */
847 PDEBUG(D_ERR|D_STREAM,
848 "bandwidth not wide enough - trying again");
849 msleep(20); /* wait for kill complete */
850 if (gspca_dev->sd_desc->isoc_nego) {
851 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
855 ep = get_ep(gspca_dev);
863 mutex_unlock(&gspca_dev->usb_lock);
867 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
871 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
872 gspca_dev->curr_mode = i;
873 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
874 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
875 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
878 static int wxh_to_mode(struct gspca_dev *gspca_dev,
879 int width, int height)
883 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
884 if (width >= gspca_dev->cam.cam_mode[i].width
885 && height >= gspca_dev->cam.cam_mode[i].height)
892 * search a mode with the right pixel format
894 static int gspca_get_mode(struct gspca_dev *gspca_dev,
900 modeU = modeD = mode;
901 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
903 if (gspca_dev->cam.cam_mode[modeD].pixelformat
907 if (++modeU < gspca_dev->cam.nmodes) {
908 if (gspca_dev->cam.cam_mode[modeU].pixelformat
916 #ifdef CONFIG_VIDEO_ADV_DEBUG
917 static int vidioc_g_register(struct file *file, void *priv,
918 struct v4l2_dbg_register *reg)
921 struct gspca_dev *gspca_dev = priv;
923 if (!gspca_dev->sd_desc->get_chip_ident)
926 if (!gspca_dev->sd_desc->get_register)
929 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
931 gspca_dev->usb_err = 0;
932 if (gspca_dev->present)
933 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
936 mutex_unlock(&gspca_dev->usb_lock);
941 static int vidioc_s_register(struct file *file, void *priv,
942 struct v4l2_dbg_register *reg)
945 struct gspca_dev *gspca_dev = priv;
947 if (!gspca_dev->sd_desc->get_chip_ident)
950 if (!gspca_dev->sd_desc->set_register)
953 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
955 gspca_dev->usb_err = 0;
956 if (gspca_dev->present)
957 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
960 mutex_unlock(&gspca_dev->usb_lock);
966 static int vidioc_g_chip_ident(struct file *file, void *priv,
967 struct v4l2_dbg_chip_ident *chip)
970 struct gspca_dev *gspca_dev = priv;
972 if (!gspca_dev->sd_desc->get_chip_ident)
975 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
977 gspca_dev->usb_err = 0;
978 if (gspca_dev->present)
979 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
982 mutex_unlock(&gspca_dev->usb_lock);
987 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
988 struct v4l2_fmtdesc *fmtdesc)
990 struct gspca_dev *gspca_dev = priv;
994 /* give an index to each format */
997 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
998 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1001 if (fmt_tb[j] == fmt_tb[index])
1006 if (fmtdesc->index == index)
1007 break; /* new format */
1009 if (index >= ARRAY_SIZE(fmt_tb))
1014 return -EINVAL; /* no more format */
1016 fmtdesc->pixelformat = fmt_tb[index];
1017 if (gspca_is_compressed(fmt_tb[index]))
1018 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1019 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1020 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1021 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1022 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1023 fmtdesc->description[4] = '\0';
1027 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1028 struct v4l2_format *fmt)
1030 struct gspca_dev *gspca_dev = priv;
1033 mode = gspca_dev->curr_mode;
1034 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1035 sizeof fmt->fmt.pix);
1039 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1040 struct v4l2_format *fmt)
1042 int w, h, mode, mode2;
1044 w = fmt->fmt.pix.width;
1045 h = fmt->fmt.pix.height;
1048 if (gspca_debug & D_CONF)
1049 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1051 /* search the closest mode for width and height */
1052 mode = wxh_to_mode(gspca_dev, w, h);
1054 /* OK if right palette */
1055 if (gspca_dev->cam.cam_mode[mode].pixelformat
1056 != fmt->fmt.pix.pixelformat) {
1058 /* else, search the closest mode with the same pixel format */
1059 mode2 = gspca_get_mode(gspca_dev, mode,
1060 fmt->fmt.pix.pixelformat);
1064 ; * no chance, return this mode */
1066 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1067 sizeof fmt->fmt.pix);
1068 return mode; /* used when s_fmt */
1071 static int vidioc_try_fmt_vid_cap(struct file *file,
1073 struct v4l2_format *fmt)
1075 struct gspca_dev *gspca_dev = priv;
1078 ret = try_fmt_vid_cap(gspca_dev, fmt);
1084 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1085 struct v4l2_format *fmt)
1087 struct gspca_dev *gspca_dev = priv;
1090 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1091 return -ERESTARTSYS;
1093 ret = try_fmt_vid_cap(gspca_dev, fmt);
1097 if (gspca_dev->nframes != 0
1098 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1103 if (ret == gspca_dev->curr_mode) {
1105 goto out; /* same mode */
1108 if (gspca_dev->streaming) {
1112 gspca_dev->width = fmt->fmt.pix.width;
1113 gspca_dev->height = fmt->fmt.pix.height;
1114 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1115 gspca_dev->curr_mode = ret;
1119 mutex_unlock(&gspca_dev->queue_lock);
1123 static int vidioc_enum_framesizes(struct file *file, void *priv,
1124 struct v4l2_frmsizeenum *fsize)
1126 struct gspca_dev *gspca_dev = priv;
1130 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1131 if (fsize->pixel_format !=
1132 gspca_dev->cam.cam_mode[i].pixelformat)
1135 if (fsize->index == index) {
1136 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1137 fsize->discrete.width =
1138 gspca_dev->cam.cam_mode[i].width;
1139 fsize->discrete.height =
1140 gspca_dev->cam.cam_mode[i].height;
1149 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1150 struct v4l2_frmivalenum *fival)
1152 struct gspca_dev *gspca_dev = priv;
1153 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1156 if (gspca_dev->cam.mode_framerates == NULL ||
1157 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1160 if (fival->pixel_format !=
1161 gspca_dev->cam.cam_mode[mode].pixelformat)
1164 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1165 if (fival->index == i) {
1166 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1167 fival->discrete.numerator = 1;
1168 fival->discrete.denominator =
1169 gspca_dev->cam.mode_framerates[mode].rates[i];
1177 static void gspca_release(struct video_device *vfd)
1179 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1181 PDEBUG(D_PROBE, "%s released",
1182 video_device_node_name(&gspca_dev->vdev));
1184 kfree(gspca_dev->usb_buf);
1188 static int dev_open(struct file *file)
1190 struct gspca_dev *gspca_dev;
1193 PDEBUG(D_STREAM, "[%s] open", current->comm);
1194 gspca_dev = (struct gspca_dev *) video_devdata(file);
1195 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1196 return -ERESTARTSYS;
1197 if (!gspca_dev->present) {
1202 if (gspca_dev->users > 4) { /* (arbitrary value) */
1207 /* protect the subdriver against rmmod */
1208 if (!try_module_get(gspca_dev->module)) {
1215 file->private_data = gspca_dev;
1217 /* activate the v4l2 debug */
1218 if (gspca_debug & D_V4L2)
1219 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1220 | V4L2_DEBUG_IOCTL_ARG;
1222 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1223 | V4L2_DEBUG_IOCTL_ARG);
1227 mutex_unlock(&gspca_dev->queue_lock);
1229 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1231 PDEBUG(D_STREAM, "open done");
1235 static int dev_close(struct file *file)
1237 struct gspca_dev *gspca_dev = file->private_data;
1239 PDEBUG(D_STREAM, "[%s] close", current->comm);
1240 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1241 return -ERESTARTSYS;
1244 /* if the file did the capture, free the streaming resources */
1245 if (gspca_dev->capt_file == file) {
1246 if (gspca_dev->streaming) {
1247 mutex_lock(&gspca_dev->usb_lock);
1248 gspca_dev->usb_err = 0;
1249 gspca_stream_off(gspca_dev);
1250 mutex_unlock(&gspca_dev->usb_lock);
1252 frame_free(gspca_dev);
1253 gspca_dev->capt_file = NULL;
1254 gspca_dev->memory = GSPCA_MEMORY_NO;
1256 file->private_data = NULL;
1257 module_put(gspca_dev->module);
1258 mutex_unlock(&gspca_dev->queue_lock);
1260 PDEBUG(D_STREAM, "close done");
1265 static int vidioc_querycap(struct file *file, void *priv,
1266 struct v4l2_capability *cap)
1268 struct gspca_dev *gspca_dev = priv;
1271 /* protect the access to the usb device */
1272 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1273 return -ERESTARTSYS;
1274 if (!gspca_dev->present) {
1278 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1279 if (gspca_dev->dev->product != NULL) {
1280 strncpy(cap->card, gspca_dev->dev->product,
1283 snprintf(cap->card, sizeof cap->card,
1284 "USB Camera (%04x:%04x)",
1285 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1286 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1288 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1289 cap->version = DRIVER_VERSION_NUMBER;
1290 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1291 | V4L2_CAP_STREAMING
1292 | V4L2_CAP_READWRITE;
1295 mutex_unlock(&gspca_dev->usb_lock);
1299 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1302 const struct ctrl *ctrls;
1305 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1306 i < gspca_dev->sd_desc->nctrls;
1308 if (gspca_dev->ctrl_dis & (1 << i))
1310 if (id == ctrls->qctrl.id)
1316 static int vidioc_queryctrl(struct file *file, void *priv,
1317 struct v4l2_queryctrl *q_ctrl)
1319 struct gspca_dev *gspca_dev = priv;
1320 const struct ctrl *ctrls;
1326 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1327 id &= V4L2_CTRL_ID_MASK;
1329 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1330 if (gspca_dev->ctrl_dis & (1 << i))
1332 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1334 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1337 ctrls = &gspca_dev->sd_desc->ctrls[i];
1342 ctrls = get_ctrl(gspca_dev, id);
1345 i = ctrls - gspca_dev->sd_desc->ctrls;
1347 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1348 if (gspca_dev->ctrl_inac & (1 << i))
1349 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1353 static int vidioc_s_ctrl(struct file *file, void *priv,
1354 struct v4l2_control *ctrl)
1356 struct gspca_dev *gspca_dev = priv;
1357 const struct ctrl *ctrls;
1360 ctrls = get_ctrl(gspca_dev, ctrl->id);
1364 if (ctrl->value < ctrls->qctrl.minimum
1365 || ctrl->value > ctrls->qctrl.maximum)
1367 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1368 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1369 return -ERESTARTSYS;
1370 gspca_dev->usb_err = 0;
1371 if (gspca_dev->present)
1372 ret = ctrls->set(gspca_dev, ctrl->value);
1375 mutex_unlock(&gspca_dev->usb_lock);
1379 static int vidioc_g_ctrl(struct file *file, void *priv,
1380 struct v4l2_control *ctrl)
1382 struct gspca_dev *gspca_dev = priv;
1383 const struct ctrl *ctrls;
1386 ctrls = get_ctrl(gspca_dev, ctrl->id);
1390 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1391 return -ERESTARTSYS;
1392 gspca_dev->usb_err = 0;
1393 if (gspca_dev->present)
1394 ret = ctrls->get(gspca_dev, &ctrl->value);
1397 mutex_unlock(&gspca_dev->usb_lock);
1401 /*fixme: have an audio flag in gspca_dev?*/
1402 static int vidioc_s_audio(struct file *file, void *priv,
1403 struct v4l2_audio *audio)
1405 if (audio->index != 0)
1410 static int vidioc_g_audio(struct file *file, void *priv,
1411 struct v4l2_audio *audio)
1413 strcpy(audio->name, "Microphone");
1417 static int vidioc_enumaudio(struct file *file, void *priv,
1418 struct v4l2_audio *audio)
1420 if (audio->index != 0)
1423 strcpy(audio->name, "Microphone");
1424 audio->capability = 0;
1429 static int vidioc_querymenu(struct file *file, void *priv,
1430 struct v4l2_querymenu *qmenu)
1432 struct gspca_dev *gspca_dev = priv;
1434 if (!gspca_dev->sd_desc->querymenu)
1436 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1439 static int vidioc_enum_input(struct file *file, void *priv,
1440 struct v4l2_input *input)
1442 struct gspca_dev *gspca_dev = priv;
1444 if (input->index != 0)
1446 input->type = V4L2_INPUT_TYPE_CAMERA;
1447 input->status = gspca_dev->cam.input_flags;
1448 strncpy(input->name, gspca_dev->sd_desc->name,
1449 sizeof input->name);
1453 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1459 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1466 static int vidioc_reqbufs(struct file *file, void *priv,
1467 struct v4l2_requestbuffers *rb)
1469 struct gspca_dev *gspca_dev = priv;
1470 int i, ret = 0, streaming;
1472 switch (rb->memory) {
1473 case GSPCA_MEMORY_READ: /* (internal call) */
1474 case V4L2_MEMORY_MMAP:
1475 case V4L2_MEMORY_USERPTR:
1480 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1481 return -ERESTARTSYS;
1483 if (gspca_dev->memory != GSPCA_MEMORY_NO
1484 && gspca_dev->memory != rb->memory) {
1489 /* only one file may do the capture */
1490 if (gspca_dev->capt_file != NULL
1491 && gspca_dev->capt_file != file) {
1496 /* if allocated, the buffers must not be mapped */
1497 for (i = 0; i < gspca_dev->nframes; i++) {
1498 if (gspca_dev->frame[i].vma_use_count) {
1504 /* stop streaming */
1505 streaming = gspca_dev->streaming;
1507 mutex_lock(&gspca_dev->usb_lock);
1508 gspca_dev->usb_err = 0;
1509 gspca_stream_off(gspca_dev);
1510 mutex_unlock(&gspca_dev->usb_lock);
1513 /* free the previous allocated buffers, if any */
1514 if (gspca_dev->nframes != 0) {
1515 frame_free(gspca_dev);
1516 gspca_dev->capt_file = NULL;
1518 if (rb->count == 0) /* unrequest */
1520 gspca_dev->memory = rb->memory;
1521 ret = frame_alloc(gspca_dev, rb->count);
1523 rb->count = gspca_dev->nframes;
1524 gspca_dev->capt_file = file;
1526 ret = gspca_init_transfer(gspca_dev);
1529 mutex_unlock(&gspca_dev->queue_lock);
1530 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1534 static int vidioc_querybuf(struct file *file, void *priv,
1535 struct v4l2_buffer *v4l2_buf)
1537 struct gspca_dev *gspca_dev = priv;
1538 struct gspca_frame *frame;
1540 if (v4l2_buf->index < 0
1541 || v4l2_buf->index >= gspca_dev->nframes)
1544 frame = &gspca_dev->frame[v4l2_buf->index];
1545 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1549 static int vidioc_streamon(struct file *file, void *priv,
1550 enum v4l2_buf_type buf_type)
1552 struct gspca_dev *gspca_dev = priv;
1555 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1557 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1558 return -ERESTARTSYS;
1560 /* check the capture file */
1561 if (gspca_dev->capt_file != file) {
1566 if (gspca_dev->nframes == 0
1567 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1571 if (!gspca_dev->streaming) {
1572 ret = gspca_init_transfer(gspca_dev);
1577 if (gspca_debug & D_STREAM) {
1578 PDEBUG_MODE("stream on OK",
1586 mutex_unlock(&gspca_dev->queue_lock);
1590 static int vidioc_streamoff(struct file *file, void *priv,
1591 enum v4l2_buf_type buf_type)
1593 struct gspca_dev *gspca_dev = priv;
1596 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1598 if (!gspca_dev->streaming)
1600 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1601 return -ERESTARTSYS;
1603 /* check the capture file */
1604 if (gspca_dev->capt_file != file) {
1609 /* stop streaming */
1610 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1614 gspca_dev->usb_err = 0;
1615 gspca_stream_off(gspca_dev);
1616 mutex_unlock(&gspca_dev->usb_lock);
1618 /* empty the application queues */
1619 for (i = 0; i < gspca_dev->nframes; i++)
1620 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1621 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1622 gspca_dev->last_packet_type = DISCARD_PACKET;
1623 gspca_dev->sequence = 0;
1626 mutex_unlock(&gspca_dev->queue_lock);
1630 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1631 struct v4l2_jpegcompression *jpegcomp)
1633 struct gspca_dev *gspca_dev = priv;
1636 if (!gspca_dev->sd_desc->get_jcomp)
1638 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1639 return -ERESTARTSYS;
1640 gspca_dev->usb_err = 0;
1641 if (gspca_dev->present)
1642 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1645 mutex_unlock(&gspca_dev->usb_lock);
1649 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1650 struct v4l2_jpegcompression *jpegcomp)
1652 struct gspca_dev *gspca_dev = priv;
1655 if (!gspca_dev->sd_desc->set_jcomp)
1657 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1658 return -ERESTARTSYS;
1659 gspca_dev->usb_err = 0;
1660 if (gspca_dev->present)
1661 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1664 mutex_unlock(&gspca_dev->usb_lock);
1668 static int vidioc_g_parm(struct file *filp, void *priv,
1669 struct v4l2_streamparm *parm)
1671 struct gspca_dev *gspca_dev = priv;
1673 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1675 if (gspca_dev->sd_desc->get_streamparm) {
1678 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1679 return -ERESTARTSYS;
1680 gspca_dev->usb_err = 0;
1681 if (gspca_dev->present)
1682 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1686 mutex_unlock(&gspca_dev->usb_lock);
1693 static int vidioc_s_parm(struct file *filp, void *priv,
1694 struct v4l2_streamparm *parm)
1696 struct gspca_dev *gspca_dev = priv;
1699 n = parm->parm.capture.readbuffers;
1700 if (n == 0 || n > GSPCA_MAX_FRAMES)
1701 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1703 gspca_dev->nbufread = n;
1705 if (gspca_dev->sd_desc->set_streamparm) {
1708 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1709 return -ERESTARTSYS;
1710 gspca_dev->usb_err = 0;
1711 if (gspca_dev->present)
1712 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1716 mutex_unlock(&gspca_dev->usb_lock);
1723 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1725 struct gspca_dev *gspca_dev = file->private_data;
1726 struct gspca_frame *frame;
1728 unsigned long addr, start, size;
1731 start = vma->vm_start;
1732 size = vma->vm_end - vma->vm_start;
1733 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1735 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1736 return -ERESTARTSYS;
1737 if (!gspca_dev->present) {
1741 if (gspca_dev->capt_file != file) {
1747 for (i = 0; i < gspca_dev->nframes; ++i) {
1748 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1749 PDEBUG(D_STREAM, "mmap bad memory type");
1752 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1754 frame = &gspca_dev->frame[i];
1758 if (frame == NULL) {
1759 PDEBUG(D_STREAM, "mmap no frame buffer found");
1763 if (size != frame->v4l2_buf.length) {
1764 PDEBUG(D_STREAM, "mmap bad size");
1770 * - VM_IO marks the area as being a mmaped region for I/O to a
1771 * device. It also prevents the region from being core dumped.
1773 vma->vm_flags |= VM_IO;
1775 addr = (unsigned long) frame->data;
1777 page = vmalloc_to_page((void *) addr);
1778 ret = vm_insert_page(vma, start, page);
1786 vma->vm_ops = &gspca_vm_ops;
1787 vma->vm_private_data = frame;
1791 mutex_unlock(&gspca_dev->queue_lock);
1796 * wait for a video frame
1798 * If a frame is ready, its index is returned.
1800 static int frame_wait(struct gspca_dev *gspca_dev,
1803 struct gspca_frame *frame;
1806 /* check if a frame is ready */
1807 i = gspca_dev->fr_o;
1808 j = gspca_dev->fr_queue[i];
1809 frame = &gspca_dev->frame[j];
1811 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1815 /* wait till a frame is ready */
1816 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1817 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1818 !gspca_dev->streaming || !gspca_dev->present,
1819 msecs_to_jiffies(3000));
1822 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1826 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1827 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1832 if (gspca_dev->sd_desc->dq_callback) {
1833 mutex_lock(&gspca_dev->usb_lock);
1834 gspca_dev->usb_err = 0;
1835 if (gspca_dev->present)
1836 gspca_dev->sd_desc->dq_callback(gspca_dev);
1837 mutex_unlock(&gspca_dev->usb_lock);
1843 * dequeue a video buffer
1845 * If nonblock_ing is false, block until a buffer is available.
1847 static int vidioc_dqbuf(struct file *file, void *priv,
1848 struct v4l2_buffer *v4l2_buf)
1850 struct gspca_dev *gspca_dev = priv;
1851 struct gspca_frame *frame;
1854 PDEBUG(D_FRAM, "dqbuf");
1855 if (v4l2_buf->memory != gspca_dev->memory)
1858 if (!gspca_dev->present)
1861 /* if not streaming, be sure the application will not loop forever */
1862 if (!(file->f_flags & O_NONBLOCK)
1863 && !gspca_dev->streaming && gspca_dev->users == 1)
1866 /* only the capturing file may dequeue */
1867 if (gspca_dev->capt_file != file)
1870 /* only one dequeue / read at a time */
1871 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1872 return -ERESTARTSYS;
1874 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1877 i = ret; /* frame index */
1878 frame = &gspca_dev->frame[i];
1879 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1880 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1882 frame->v4l2_buf.bytesused)) {
1883 PDEBUG(D_ERR|D_STREAM,
1884 "dqbuf cp to user failed");
1889 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1890 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1891 PDEBUG(D_FRAM, "dqbuf %d", i);
1894 mutex_unlock(&gspca_dev->read_lock);
1899 * queue a video buffer
1901 * Attempting to queue a buffer that has already been
1902 * queued will return -EINVAL.
1904 static int vidioc_qbuf(struct file *file, void *priv,
1905 struct v4l2_buffer *v4l2_buf)
1907 struct gspca_dev *gspca_dev = priv;
1908 struct gspca_frame *frame;
1911 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1913 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1914 return -ERESTARTSYS;
1916 index = v4l2_buf->index;
1917 if ((unsigned) index >= gspca_dev->nframes) {
1919 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1923 if (v4l2_buf->memory != gspca_dev->memory) {
1924 PDEBUG(D_FRAM, "qbuf bad memory type");
1929 frame = &gspca_dev->frame[index];
1930 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1931 PDEBUG(D_FRAM, "qbuf bad state");
1936 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1938 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1939 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1940 frame->v4l2_buf.length = v4l2_buf->length;
1943 /* put the buffer in the 'queued' queue */
1944 i = gspca_dev->fr_q;
1945 gspca_dev->fr_queue[i] = index;
1946 if (gspca_dev->fr_i == i)
1947 gspca_dev->image = frame->data;
1948 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1949 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1954 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1955 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1958 mutex_unlock(&gspca_dev->queue_lock);
1963 * allocate the resources for read()
1965 static int read_alloc(struct gspca_dev *gspca_dev,
1968 struct v4l2_buffer v4l2_buf;
1971 PDEBUG(D_STREAM, "read alloc");
1972 if (gspca_dev->nframes == 0) {
1973 struct v4l2_requestbuffers rb;
1975 memset(&rb, 0, sizeof rb);
1976 rb.count = gspca_dev->nbufread;
1977 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1978 rb.memory = GSPCA_MEMORY_READ;
1979 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1981 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1984 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1985 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1986 v4l2_buf.memory = GSPCA_MEMORY_READ;
1987 for (i = 0; i < gspca_dev->nbufread; i++) {
1989 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1991 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1995 gspca_dev->memory = GSPCA_MEMORY_READ;
1998 /* start streaming */
1999 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2001 PDEBUG(D_STREAM, "read streamon err %d", ret);
2005 static unsigned int dev_poll(struct file *file, poll_table *wait)
2007 struct gspca_dev *gspca_dev = file->private_data;
2010 PDEBUG(D_FRAM, "poll");
2012 poll_wait(file, &gspca_dev->wq, wait);
2014 /* if reqbufs is not done, the user would use read() */
2015 if (gspca_dev->nframes == 0) {
2016 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2017 return POLLERR; /* not the 1st time */
2018 ret = read_alloc(gspca_dev, file);
2023 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2026 /* check the next incoming buffer */
2027 i = gspca_dev->fr_o;
2028 i = gspca_dev->fr_queue[i];
2029 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2030 ret = POLLIN | POLLRDNORM; /* something to read */
2033 mutex_unlock(&gspca_dev->queue_lock);
2034 if (!gspca_dev->present)
2039 static ssize_t dev_read(struct file *file, char __user *data,
2040 size_t count, loff_t *ppos)
2042 struct gspca_dev *gspca_dev = file->private_data;
2043 struct gspca_frame *frame;
2044 struct v4l2_buffer v4l2_buf;
2045 struct timeval timestamp;
2048 PDEBUG(D_FRAM, "read (%zd)", count);
2049 if (!gspca_dev->present)
2051 switch (gspca_dev->memory) {
2052 case GSPCA_MEMORY_NO: /* first time */
2053 ret = read_alloc(gspca_dev, file);
2057 case GSPCA_MEMORY_READ:
2058 if (gspca_dev->capt_file == file)
2066 timestamp = ktime_to_timeval(ktime_get());
2070 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2071 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2072 v4l2_buf.memory = GSPCA_MEMORY_READ;
2073 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2075 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2079 /* if the process slept for more than 1 second,
2080 * get a newer frame */
2081 frame = &gspca_dev->frame[v4l2_buf.index];
2083 break; /* avoid infinite loop */
2084 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2086 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2088 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2093 /* copy the frame */
2094 if (count > frame->v4l2_buf.bytesused)
2095 count = frame->v4l2_buf.bytesused;
2096 ret = copy_to_user(data, frame->data, count);
2098 PDEBUG(D_ERR|D_STREAM,
2099 "read cp to user lack %d / %zd", ret, count);
2105 /* in each case, requeue the buffer */
2106 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2112 static struct v4l2_file_operations dev_fops = {
2113 .owner = THIS_MODULE,
2115 .release = dev_close,
2118 .unlocked_ioctl = video_ioctl2,
2122 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2123 .vidioc_querycap = vidioc_querycap,
2124 .vidioc_dqbuf = vidioc_dqbuf,
2125 .vidioc_qbuf = vidioc_qbuf,
2126 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2127 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2128 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2129 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2130 .vidioc_streamon = vidioc_streamon,
2131 .vidioc_queryctrl = vidioc_queryctrl,
2132 .vidioc_g_ctrl = vidioc_g_ctrl,
2133 .vidioc_s_ctrl = vidioc_s_ctrl,
2134 .vidioc_g_audio = vidioc_g_audio,
2135 .vidioc_s_audio = vidioc_s_audio,
2136 .vidioc_enumaudio = vidioc_enumaudio,
2137 .vidioc_querymenu = vidioc_querymenu,
2138 .vidioc_enum_input = vidioc_enum_input,
2139 .vidioc_g_input = vidioc_g_input,
2140 .vidioc_s_input = vidioc_s_input,
2141 .vidioc_reqbufs = vidioc_reqbufs,
2142 .vidioc_querybuf = vidioc_querybuf,
2143 .vidioc_streamoff = vidioc_streamoff,
2144 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2145 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2146 .vidioc_g_parm = vidioc_g_parm,
2147 .vidioc_s_parm = vidioc_s_parm,
2148 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2149 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2150 #ifdef CONFIG_VIDEO_ADV_DEBUG
2151 .vidioc_g_register = vidioc_g_register,
2152 .vidioc_s_register = vidioc_s_register,
2154 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2157 static struct video_device gspca_template = {
2158 .name = "gspca main driver",
2160 .ioctl_ops = &dev_ioctl_ops,
2161 .release = gspca_release,
2165 * probe and create a new gspca device
2167 * This function must be called by the sub-driver when it is
2168 * called for probing a new device.
2170 int gspca_dev_probe2(struct usb_interface *intf,
2171 const struct usb_device_id *id,
2172 const struct sd_desc *sd_desc,
2174 struct module *module)
2176 struct gspca_dev *gspca_dev;
2177 struct usb_device *dev = interface_to_usbdev(intf);
2180 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2182 /* create the device */
2183 if (dev_size < sizeof *gspca_dev)
2184 dev_size = sizeof *gspca_dev;
2185 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2187 err("couldn't kzalloc gspca struct");
2190 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2191 if (!gspca_dev->usb_buf) {
2192 err("out of memory");
2196 gspca_dev->dev = dev;
2197 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2198 gspca_dev->nbalt = intf->num_altsetting;
2199 gspca_dev->sd_desc = sd_desc;
2200 gspca_dev->nbufread = 2;
2201 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2203 /* configure the subdriver and initialize the USB device */
2204 ret = sd_desc->config(gspca_dev, id);
2207 ret = sd_desc->init(gspca_dev);
2210 gspca_set_default_mode(gspca_dev);
2212 ret = gspca_input_connect(gspca_dev);
2216 mutex_init(&gspca_dev->usb_lock);
2217 mutex_init(&gspca_dev->read_lock);
2218 mutex_init(&gspca_dev->queue_lock);
2219 init_waitqueue_head(&gspca_dev->wq);
2221 /* init video stuff */
2222 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2223 gspca_dev->vdev.parent = &intf->dev;
2224 gspca_dev->module = module;
2225 gspca_dev->present = 1;
2226 ret = video_register_device(&gspca_dev->vdev,
2230 err("video_register_device err %d", ret);
2234 usb_set_intfdata(intf, gspca_dev);
2235 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2237 gspca_input_create_urb(gspca_dev);
2241 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2242 if (gspca_dev->input_dev)
2243 input_unregister_device(gspca_dev->input_dev);
2245 kfree(gspca_dev->usb_buf);
2249 EXPORT_SYMBOL(gspca_dev_probe2);
2251 /* same function as the previous one, but check the interface */
2252 int gspca_dev_probe(struct usb_interface *intf,
2253 const struct usb_device_id *id,
2254 const struct sd_desc *sd_desc,
2256 struct module *module)
2258 struct usb_device *dev = interface_to_usbdev(intf);
2260 /* we don't handle multi-config cameras */
2261 if (dev->descriptor.bNumConfigurations != 1) {
2262 PDEBUG(D_ERR, "%04x:%04x too many config",
2263 id->idVendor, id->idProduct);
2267 /* the USB video interface must be the first one */
2268 if (dev->config->desc.bNumInterfaces != 1
2269 && intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2270 PDEBUG(D_ERR, "%04x:%04x bad interface %d",
2271 id->idVendor, id->idProduct,
2272 intf->cur_altsetting->desc.bInterfaceNumber);
2276 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2278 EXPORT_SYMBOL(gspca_dev_probe);
2283 * This function must be called by the sub-driver
2284 * when the device disconnects, after the specific resources are freed.
2286 void gspca_disconnect(struct usb_interface *intf)
2288 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2289 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2290 struct input_dev *input_dev;
2293 PDEBUG(D_PROBE, "%s disconnect",
2294 video_device_node_name(&gspca_dev->vdev));
2295 mutex_lock(&gspca_dev->usb_lock);
2296 gspca_dev->present = 0;
2298 if (gspca_dev->streaming) {
2299 destroy_urbs(gspca_dev);
2300 wake_up_interruptible(&gspca_dev->wq);
2303 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2304 gspca_input_destroy_urb(gspca_dev);
2305 input_dev = gspca_dev->input_dev;
2307 gspca_dev->input_dev = NULL;
2308 input_unregister_device(input_dev);
2312 /* the device is freed at exit of this function */
2313 gspca_dev->dev = NULL;
2314 mutex_unlock(&gspca_dev->usb_lock);
2316 usb_set_intfdata(intf, NULL);
2318 /* release the device */
2319 /* (this will call gspca_release() immediatly or on last close) */
2320 video_unregister_device(&gspca_dev->vdev);
2322 /* PDEBUG(D_PROBE, "disconnect complete"); */
2324 EXPORT_SYMBOL(gspca_disconnect);
2327 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2329 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2331 if (!gspca_dev->streaming)
2333 gspca_dev->frozen = 1; /* avoid urb error messages */
2334 if (gspca_dev->sd_desc->stopN)
2335 gspca_dev->sd_desc->stopN(gspca_dev);
2336 destroy_urbs(gspca_dev);
2337 gspca_input_destroy_urb(gspca_dev);
2338 gspca_set_alt0(gspca_dev);
2339 if (gspca_dev->sd_desc->stop0)
2340 gspca_dev->sd_desc->stop0(gspca_dev);
2343 EXPORT_SYMBOL(gspca_suspend);
2345 int gspca_resume(struct usb_interface *intf)
2347 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2349 gspca_dev->frozen = 0;
2350 gspca_dev->sd_desc->init(gspca_dev);
2351 gspca_input_create_urb(gspca_dev);
2352 if (gspca_dev->streaming)
2353 return gspca_init_transfer(gspca_dev);
2356 EXPORT_SYMBOL(gspca_resume);
2358 /* -- cam driver utility functions -- */
2360 /* auto gain and exposure algorithm based on the knee algorithm described here:
2361 http://ytse.tricolour.net/docs/LowLightOptimization.html
2363 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2365 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2366 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2368 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2369 const struct ctrl *gain_ctrl = NULL;
2370 const struct ctrl *exposure_ctrl = NULL;
2371 const struct ctrl *autogain_ctrl = NULL;
2374 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2375 if (gspca_dev->ctrl_dis & (1 << i))
2377 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2378 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2379 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2380 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2381 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2382 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2384 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2385 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2386 "on cam without (auto)gain/exposure");
2390 if (gain_ctrl->get(gspca_dev, &gain) ||
2391 exposure_ctrl->get(gspca_dev, &exposure) ||
2392 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2396 orig_exposure = exposure;
2398 /* If we are of a multiple of deadzone, do multiple steps to reach the
2399 desired lumination fast (with the risc of a slight overshoot) */
2400 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2402 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2403 avg_lum, desired_avg_lum, steps);
2405 for (i = 0; i < steps; i++) {
2406 if (avg_lum > desired_avg_lum) {
2407 if (gain > gain_knee)
2409 else if (exposure > exposure_knee)
2411 else if (gain > gain_ctrl->qctrl.default_value)
2413 else if (exposure > exposure_ctrl->qctrl.minimum)
2415 else if (gain > gain_ctrl->qctrl.minimum)
2420 if (gain < gain_ctrl->qctrl.default_value)
2422 else if (exposure < exposure_knee)
2424 else if (gain < gain_knee)
2426 else if (exposure < exposure_ctrl->qctrl.maximum)
2428 else if (gain < gain_ctrl->qctrl.maximum)
2435 if (gain != orig_gain) {
2436 gain_ctrl->set(gspca_dev, gain);
2439 if (exposure != orig_exposure) {
2440 exposure_ctrl->set(gspca_dev, exposure);
2446 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2448 /* -- module insert / remove -- */
2449 static int __init gspca_init(void)
2451 info("main v%d.%d.%d registered",
2452 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2453 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2454 DRIVER_VERSION_NUMBER & 0xff);
2457 static void __exit gspca_exit(void)
2459 info("main deregistered");
2462 module_init(gspca_init);
2463 module_exit(gspca_exit);
2466 module_param_named(debug, gspca_debug, int, 0644);
2467 MODULE_PARM_DESC(debug,
2468 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2469 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"