2 * Main USB camera driver
4 * Copyright (C) 2008-2009 Jean-Francois 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/jiffies.h>
39 #include <media/v4l2-ioctl.h>
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-Francois 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
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;
193 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
194 struct usb_endpoint_descriptor *ep)
196 unsigned int buffer_len;
199 struct usb_device *dev;
203 buffer_len = ep->wMaxPacketSize;
204 interval = ep->bInterval;
205 PDEBUG(D_PROBE, "found int in endpoint: 0x%x, "
206 "buffer_len=%u, interval=%u",
207 ep->bEndpointAddress, buffer_len, interval);
209 dev = gspca_dev->dev;
211 urb = usb_alloc_urb(0, GFP_KERNEL);
217 buffer = usb_buffer_alloc(dev, ep->wMaxPacketSize,
218 GFP_KERNEL, &urb->transfer_dma);
223 usb_fill_int_urb(urb, dev,
224 usb_rcvintpipe(dev, ep->bEndpointAddress),
226 int_irq, (void *)gspca_dev, interval);
227 gspca_dev->int_urb = urb;
228 ret = usb_submit_urb(urb, GFP_KERNEL);
230 PDEBUG(D_ERR, "submit URB failed with error %i", ret);
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
246 static int gspca_input_create_urb(struct gspca_dev *gspca_dev)
249 struct usb_interface *intf;
250 struct usb_host_interface *intf_desc;
251 struct usb_endpoint_descriptor *ep;
254 if (gspca_dev->sd_desc->int_pkt_scan) {
255 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
256 intf_desc = intf->cur_altsetting;
257 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
258 ep = &intf_desc->endpoint[i].desc;
259 if (usb_endpoint_dir_in(ep) &&
260 usb_endpoint_xfer_int(ep)) {
262 ret = alloc_and_submit_int_urb(gspca_dev, ep);
270 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
274 urb = gspca_dev->int_urb;
276 gspca_dev->int_urb = NULL;
278 usb_buffer_free(gspca_dev->dev,
279 urb->transfer_buffer_length,
280 urb->transfer_buffer,
286 #define gspca_input_connect(gspca_dev) 0
287 #define gspca_input_create_urb(gspca_dev) 0
288 #define gspca_input_destroy_urb(gspca_dev)
291 /* get the current input frame buffer */
292 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
294 struct gspca_frame *frame;
296 frame = gspca_dev->cur_frame;
297 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
298 != V4L2_BUF_FLAG_QUEUED)
302 EXPORT_SYMBOL(gspca_get_i_frame);
305 * fill a video frame from an URB and resubmit
307 static void fill_frame(struct gspca_dev *gspca_dev,
310 u8 *data; /* address of data in the iso message */
314 if (urb->status != 0) {
315 if (urb->status == -ESHUTDOWN)
316 return; /* disconnection */
318 if (gspca_dev->frozen)
321 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
325 pkt_scan = gspca_dev->sd_desc->pkt_scan;
326 for (i = 0; i < urb->number_of_packets; i++) {
328 /* check the packet status and length */
329 len = urb->iso_frame_desc[i].actual_length;
331 if (gspca_dev->empty_packet == 0)
332 gspca_dev->empty_packet = 1;
335 st = urb->iso_frame_desc[i].status;
338 "ISOC data error: [%d] len=%d, status=%d",
340 gspca_dev->last_packet_type = DISCARD_PACKET;
344 /* let the packet be analyzed by the subdriver */
345 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
346 i, urb->iso_frame_desc[i].offset, len);
347 data = (u8 *) urb->transfer_buffer
348 + urb->iso_frame_desc[i].offset;
349 pkt_scan(gspca_dev, data, len);
353 /* resubmit the URB */
354 st = usb_submit_urb(urb, GFP_ATOMIC);
356 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
360 * ISOC message interrupt from the USB device
362 * Analyse each packet and call the subdriver for copy to the frame buffer.
364 static void isoc_irq(struct urb *urb)
366 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
368 PDEBUG(D_PACK, "isoc irq");
369 if (!gspca_dev->streaming)
371 fill_frame(gspca_dev, urb);
375 * bulk message interrupt from the USB device
377 static void bulk_irq(struct urb *urb)
379 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
382 PDEBUG(D_PACK, "bulk irq");
383 if (!gspca_dev->streaming)
385 switch (urb->status) {
389 return; /* disconnection */
392 if (gspca_dev->frozen)
395 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
400 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
401 gspca_dev->sd_desc->pkt_scan(gspca_dev,
402 urb->transfer_buffer,
406 /* resubmit the URB */
407 if (gspca_dev->cam.bulk_nurbs != 0) {
408 st = usb_submit_urb(urb, GFP_ATOMIC);
410 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
415 * add data to the current frame
417 * This function is called by the subdrivers at interrupt level.
419 * To build a frame, these ones must add
421 * - 0 or many INTER_PACKETs
423 * DISCARD_PACKET invalidates the whole frame.
424 * On LAST_PACKET, a new frame is returned.
426 void gspca_frame_add(struct gspca_dev *gspca_dev,
427 enum gspca_packet_type packet_type,
431 struct gspca_frame *frame;
434 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
436 /* check the availability of the frame buffer */
437 frame = gspca_dev->cur_frame;
438 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
439 != V4L2_BUF_FLAG_QUEUED) {
440 gspca_dev->last_packet_type = DISCARD_PACKET;
444 /* when start of a new frame, if the current frame buffer
445 * is not queued, discard the whole frame */
446 if (packet_type == FIRST_PACKET) {
447 frame->data_end = frame->data;
448 jiffies_to_timeval(get_jiffies_64(),
449 &frame->v4l2_buf.timestamp);
450 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
451 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
452 if (packet_type == LAST_PACKET)
453 gspca_dev->last_packet_type = packet_type;
457 /* append the packet to the frame buffer */
459 if (frame->data_end - frame->data + len
460 > frame->v4l2_buf.length) {
461 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
462 frame->data_end - frame->data + len,
463 frame->v4l2_buf.length);
464 packet_type = DISCARD_PACKET;
466 memcpy(frame->data_end, data, len);
467 frame->data_end += len;
470 gspca_dev->last_packet_type = packet_type;
472 /* if last packet, wake up the application and advance in the queue */
473 if (packet_type == LAST_PACKET) {
474 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
475 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
476 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
477 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
478 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
480 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
481 frame->v4l2_buf.bytesused,
485 j = gspca_dev->fr_queue[i];
486 gspca_dev->cur_frame = &gspca_dev->frame[j];
489 EXPORT_SYMBOL(gspca_frame_add);
491 static int gspca_is_compressed(__u32 format)
494 case V4L2_PIX_FMT_MJPEG:
495 case V4L2_PIX_FMT_JPEG:
496 case V4L2_PIX_FMT_SPCA561:
497 case V4L2_PIX_FMT_PAC207:
498 case V4L2_PIX_FMT_MR97310A:
504 static void *rvmalloc(long size)
509 mem = vmalloc_32(size);
511 adr = (unsigned long) mem;
513 SetPageReserved(vmalloc_to_page((void *) adr));
521 static void rvfree(void *mem, long size)
525 adr = (unsigned long) mem;
527 ClearPageReserved(vmalloc_to_page((void *) adr));
534 static int frame_alloc(struct gspca_dev *gspca_dev,
537 struct gspca_frame *frame;
541 i = gspca_dev->curr_mode;
542 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
543 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
544 frsz = PAGE_ALIGN(frsz);
545 gspca_dev->frsz = frsz;
546 if (count > GSPCA_MAX_FRAMES)
547 count = GSPCA_MAX_FRAMES;
548 gspca_dev->frbuf = rvmalloc(frsz * count);
549 if (!gspca_dev->frbuf) {
550 err("frame alloc failed");
553 gspca_dev->nframes = count;
554 for (i = 0; i < count; i++) {
555 frame = &gspca_dev->frame[i];
556 frame->v4l2_buf.index = i;
557 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
558 frame->v4l2_buf.flags = 0;
559 frame->v4l2_buf.field = V4L2_FIELD_NONE;
560 frame->v4l2_buf.length = frsz;
561 frame->v4l2_buf.memory = gspca_dev->memory;
562 frame->v4l2_buf.sequence = 0;
563 frame->data = frame->data_end =
564 gspca_dev->frbuf + i * frsz;
565 frame->v4l2_buf.m.offset = i * frsz;
567 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
568 gspca_dev->cur_frame = &gspca_dev->frame[0];
569 gspca_dev->last_packet_type = DISCARD_PACKET;
570 gspca_dev->sequence = 0;
574 static void frame_free(struct gspca_dev *gspca_dev)
578 PDEBUG(D_STREAM, "frame free");
579 if (gspca_dev->frbuf != NULL) {
580 rvfree(gspca_dev->frbuf,
581 gspca_dev->nframes * gspca_dev->frsz);
582 gspca_dev->frbuf = NULL;
583 for (i = 0; i < gspca_dev->nframes; i++)
584 gspca_dev->frame[i].data = NULL;
586 gspca_dev->nframes = 0;
589 static void destroy_urbs(struct gspca_dev *gspca_dev)
594 PDEBUG(D_STREAM, "kill transfer");
595 for (i = 0; i < MAX_NURBS; i++) {
596 urb = gspca_dev->urb[i];
600 gspca_dev->urb[i] = NULL;
602 if (urb->transfer_buffer != NULL)
603 usb_buffer_free(gspca_dev->dev,
604 urb->transfer_buffer_length,
605 urb->transfer_buffer,
612 * look for an input transfer endpoint in an alternate setting
614 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
617 struct usb_host_endpoint *ep;
620 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
621 ep = &alt->endpoint[i];
622 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
624 && ep->desc.wMaxPacketSize != 0)
631 * look for an input (isoc or bulk) endpoint
633 * The endpoint is defined by the subdriver.
634 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
635 * This routine may be called many times when the bandwidth is too small
636 * (the bandwidth is checked on urb submit).
638 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
640 struct usb_interface *intf;
641 struct usb_host_endpoint *ep;
644 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
646 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
647 : USB_ENDPOINT_XFER_ISOC;
648 i = gspca_dev->alt; /* previous alt setting */
649 if (gspca_dev->cam.reverse_alts) {
650 while (++i < gspca_dev->nbalt) {
651 ep = alt_xfer(&intf->altsetting[i], xfer);
657 ep = alt_xfer(&intf->altsetting[i], xfer);
663 err("no transfer endpoint found");
666 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
667 i, ep->desc.bEndpointAddress);
668 gspca_dev->alt = i; /* memorize the current alt setting */
669 if (gspca_dev->nbalt > 1) {
670 gspca_input_destroy_urb(gspca_dev);
671 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
673 err("set alt %d err %d", i, ret);
676 gspca_input_create_urb(gspca_dev);
682 * create the URBs for image transfer
684 static int create_urbs(struct gspca_dev *gspca_dev,
685 struct usb_host_endpoint *ep)
688 int n, nurbs, i, psize, npkt, bsize;
690 /* calculate the packet size and the number of packets */
691 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
693 if (!gspca_dev->cam.bulk) { /* isoc */
695 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
696 if (gspca_dev->pkt_size == 0)
697 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
699 psize = gspca_dev->pkt_size;
700 npkt = gspca_dev->cam.npkt;
702 npkt = 32; /* default value */
703 bsize = psize * npkt;
705 "isoc %d pkts size %d = bsize:%d",
710 bsize = gspca_dev->cam.bulk_size;
713 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
714 if (gspca_dev->cam.bulk_nurbs != 0)
715 nurbs = gspca_dev->cam.bulk_nurbs;
720 for (n = 0; n < nurbs; n++) {
721 urb = usb_alloc_urb(npkt, GFP_KERNEL);
723 err("usb_alloc_urb failed");
726 gspca_dev->urb[n] = urb;
727 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
732 if (urb->transfer_buffer == NULL) {
733 err("usb_buffer_alloc failed");
736 urb->dev = gspca_dev->dev;
737 urb->context = gspca_dev;
738 urb->transfer_buffer_length = bsize;
739 if (npkt != 0) { /* ISOC */
740 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
741 ep->desc.bEndpointAddress);
742 urb->transfer_flags = URB_ISO_ASAP
743 | URB_NO_TRANSFER_DMA_MAP;
744 urb->interval = ep->desc.bInterval;
745 urb->complete = isoc_irq;
746 urb->number_of_packets = npkt;
747 for (i = 0; i < npkt; i++) {
748 urb->iso_frame_desc[i].length = psize;
749 urb->iso_frame_desc[i].offset = psize * i;
752 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
753 ep->desc.bEndpointAddress),
754 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
755 urb->complete = bulk_irq;
762 * start the USB transfer
764 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
766 struct usb_host_endpoint *ep;
770 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
773 if (!gspca_dev->present) {
778 gspca_dev->usb_err = 0;
780 /* set the higher alternate setting and
781 * loop until urb submit succeeds */
782 if (gspca_dev->cam.reverse_alts)
785 gspca_dev->alt = gspca_dev->nbalt;
787 if (gspca_dev->sd_desc->isoc_init) {
788 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
792 ep = get_ep(gspca_dev);
798 if (!gspca_dev->cam.no_urb_create) {
799 PDEBUG(D_STREAM, "init transfer alt %d",
801 ret = create_urbs(gspca_dev, ep);
803 destroy_urbs(gspca_dev);
808 /* clear the bulk endpoint */
809 if (gspca_dev->cam.bulk)
810 usb_clear_halt(gspca_dev->dev,
811 gspca_dev->urb[0]->pipe);
814 ret = gspca_dev->sd_desc->start(gspca_dev);
816 destroy_urbs(gspca_dev);
819 gspca_dev->streaming = 1;
821 /* some bulk transfers are started by the subdriver */
822 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
825 /* submit the URBs */
826 for (n = 0; n < MAX_NURBS; n++) {
827 urb = gspca_dev->urb[n];
830 ret = usb_submit_urb(urb, GFP_KERNEL);
836 gspca_dev->streaming = 0;
837 destroy_urbs(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 int gspca_set_alt0(struct gspca_dev *gspca_dev)
871 if (gspca_dev->alt == 0)
873 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
875 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
879 /* Note: both the queue and the usb locks should be held when calling this */
880 static void gspca_stream_off(struct gspca_dev *gspca_dev)
882 gspca_dev->streaming = 0;
883 if (gspca_dev->present) {
884 if (gspca_dev->sd_desc->stopN)
885 gspca_dev->sd_desc->stopN(gspca_dev);
886 destroy_urbs(gspca_dev);
887 gspca_input_destroy_urb(gspca_dev);
888 gspca_set_alt0(gspca_dev);
889 gspca_input_create_urb(gspca_dev);
892 /* always call stop0 to free the subdriver's resources */
893 if (gspca_dev->sd_desc->stop0)
894 gspca_dev->sd_desc->stop0(gspca_dev);
895 PDEBUG(D_STREAM, "stream off OK");
898 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
902 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
903 gspca_dev->curr_mode = i;
904 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
905 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
906 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
909 static int wxh_to_mode(struct gspca_dev *gspca_dev,
910 int width, int height)
914 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
915 if (width >= gspca_dev->cam.cam_mode[i].width
916 && height >= gspca_dev->cam.cam_mode[i].height)
923 * search a mode with the right pixel format
925 static int gspca_get_mode(struct gspca_dev *gspca_dev,
931 modeU = modeD = mode;
932 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
934 if (gspca_dev->cam.cam_mode[modeD].pixelformat
938 if (++modeU < gspca_dev->cam.nmodes) {
939 if (gspca_dev->cam.cam_mode[modeU].pixelformat
947 #ifdef CONFIG_VIDEO_ADV_DEBUG
948 static int vidioc_g_register(struct file *file, void *priv,
949 struct v4l2_dbg_register *reg)
952 struct gspca_dev *gspca_dev = priv;
954 if (!gspca_dev->sd_desc->get_chip_ident)
957 if (!gspca_dev->sd_desc->get_register)
960 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
962 gspca_dev->usb_err = 0;
963 if (gspca_dev->present)
964 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
967 mutex_unlock(&gspca_dev->usb_lock);
972 static int vidioc_s_register(struct file *file, void *priv,
973 struct v4l2_dbg_register *reg)
976 struct gspca_dev *gspca_dev = priv;
978 if (!gspca_dev->sd_desc->get_chip_ident)
981 if (!gspca_dev->sd_desc->set_register)
984 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
986 gspca_dev->usb_err = 0;
987 if (gspca_dev->present)
988 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
991 mutex_unlock(&gspca_dev->usb_lock);
997 static int vidioc_g_chip_ident(struct file *file, void *priv,
998 struct v4l2_dbg_chip_ident *chip)
1001 struct gspca_dev *gspca_dev = priv;
1003 if (!gspca_dev->sd_desc->get_chip_ident)
1006 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1007 return -ERESTARTSYS;
1008 gspca_dev->usb_err = 0;
1009 if (gspca_dev->present)
1010 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1013 mutex_unlock(&gspca_dev->usb_lock);
1018 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1019 struct v4l2_fmtdesc *fmtdesc)
1021 struct gspca_dev *gspca_dev = priv;
1025 /* give an index to each format */
1028 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1029 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1032 if (fmt_tb[j] == fmt_tb[index])
1037 if (fmtdesc->index == index)
1038 break; /* new format */
1040 if (index >= ARRAY_SIZE(fmt_tb))
1045 return -EINVAL; /* no more format */
1047 fmtdesc->pixelformat = fmt_tb[index];
1048 if (gspca_is_compressed(fmt_tb[index]))
1049 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1050 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1051 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1052 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1053 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1054 fmtdesc->description[4] = '\0';
1058 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1059 struct v4l2_format *fmt)
1061 struct gspca_dev *gspca_dev = priv;
1064 mode = gspca_dev->curr_mode;
1065 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1066 sizeof fmt->fmt.pix);
1070 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1071 struct v4l2_format *fmt)
1073 int w, h, mode, mode2;
1075 w = fmt->fmt.pix.width;
1076 h = fmt->fmt.pix.height;
1079 if (gspca_debug & D_CONF)
1080 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1082 /* search the closest mode for width and height */
1083 mode = wxh_to_mode(gspca_dev, w, h);
1085 /* OK if right palette */
1086 if (gspca_dev->cam.cam_mode[mode].pixelformat
1087 != fmt->fmt.pix.pixelformat) {
1089 /* else, search the closest mode with the same pixel format */
1090 mode2 = gspca_get_mode(gspca_dev, mode,
1091 fmt->fmt.pix.pixelformat);
1095 ; * no chance, return this mode */
1097 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1098 sizeof fmt->fmt.pix);
1099 return mode; /* used when s_fmt */
1102 static int vidioc_try_fmt_vid_cap(struct file *file,
1104 struct v4l2_format *fmt)
1106 struct gspca_dev *gspca_dev = priv;
1109 ret = try_fmt_vid_cap(gspca_dev, fmt);
1115 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1116 struct v4l2_format *fmt)
1118 struct gspca_dev *gspca_dev = priv;
1121 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1122 return -ERESTARTSYS;
1124 ret = try_fmt_vid_cap(gspca_dev, fmt);
1128 if (gspca_dev->nframes != 0
1129 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1134 if (ret == gspca_dev->curr_mode) {
1136 goto out; /* same mode */
1139 if (gspca_dev->streaming) {
1143 gspca_dev->width = fmt->fmt.pix.width;
1144 gspca_dev->height = fmt->fmt.pix.height;
1145 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1146 gspca_dev->curr_mode = ret;
1150 mutex_unlock(&gspca_dev->queue_lock);
1154 static int vidioc_enum_framesizes(struct file *file, void *priv,
1155 struct v4l2_frmsizeenum *fsize)
1157 struct gspca_dev *gspca_dev = priv;
1161 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1162 if (fsize->pixel_format !=
1163 gspca_dev->cam.cam_mode[i].pixelformat)
1166 if (fsize->index == index) {
1167 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1168 fsize->discrete.width =
1169 gspca_dev->cam.cam_mode[i].width;
1170 fsize->discrete.height =
1171 gspca_dev->cam.cam_mode[i].height;
1180 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1181 struct v4l2_frmivalenum *fival)
1183 struct gspca_dev *gspca_dev = priv;
1184 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1187 if (gspca_dev->cam.mode_framerates == NULL ||
1188 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1191 if (fival->pixel_format !=
1192 gspca_dev->cam.cam_mode[mode].pixelformat)
1195 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1196 if (fival->index == i) {
1197 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1198 fival->discrete.numerator = 1;
1199 fival->discrete.denominator =
1200 gspca_dev->cam.mode_framerates[mode].rates[i];
1208 static void gspca_release(struct video_device *vfd)
1210 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1212 PDEBUG(D_PROBE, "%s released",
1213 video_device_node_name(&gspca_dev->vdev));
1215 kfree(gspca_dev->usb_buf);
1219 static int dev_open(struct file *file)
1221 struct gspca_dev *gspca_dev;
1224 PDEBUG(D_STREAM, "[%s] open", current->comm);
1225 gspca_dev = (struct gspca_dev *) video_devdata(file);
1226 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1227 return -ERESTARTSYS;
1228 if (!gspca_dev->present) {
1233 if (gspca_dev->users > 4) { /* (arbitrary value) */
1238 /* protect the subdriver against rmmod */
1239 if (!try_module_get(gspca_dev->module)) {
1246 file->private_data = gspca_dev;
1248 /* activate the v4l2 debug */
1249 if (gspca_debug & D_V4L2)
1250 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1251 | V4L2_DEBUG_IOCTL_ARG;
1253 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1254 | V4L2_DEBUG_IOCTL_ARG);
1258 mutex_unlock(&gspca_dev->queue_lock);
1260 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1262 PDEBUG(D_STREAM, "open done");
1266 static int dev_close(struct file *file)
1268 struct gspca_dev *gspca_dev = file->private_data;
1270 PDEBUG(D_STREAM, "[%s] close", current->comm);
1271 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1272 return -ERESTARTSYS;
1275 /* if the file did the capture, free the streaming resources */
1276 if (gspca_dev->capt_file == file) {
1277 if (gspca_dev->streaming) {
1278 mutex_lock(&gspca_dev->usb_lock);
1279 gspca_dev->usb_err = 0;
1280 gspca_stream_off(gspca_dev);
1281 mutex_unlock(&gspca_dev->usb_lock);
1283 frame_free(gspca_dev);
1284 gspca_dev->capt_file = NULL;
1285 gspca_dev->memory = GSPCA_MEMORY_NO;
1287 file->private_data = NULL;
1288 module_put(gspca_dev->module);
1289 mutex_unlock(&gspca_dev->queue_lock);
1291 PDEBUG(D_STREAM, "close done");
1296 static int vidioc_querycap(struct file *file, void *priv,
1297 struct v4l2_capability *cap)
1299 struct gspca_dev *gspca_dev = priv;
1302 /* protect the access to the usb device */
1303 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1304 return -ERESTARTSYS;
1305 if (!gspca_dev->present) {
1309 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1310 if (gspca_dev->dev->product != NULL) {
1311 strncpy(cap->card, gspca_dev->dev->product,
1314 snprintf(cap->card, sizeof cap->card,
1315 "USB Camera (%04x:%04x)",
1316 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1317 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1319 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1320 cap->version = DRIVER_VERSION_NUMBER;
1321 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1322 | V4L2_CAP_STREAMING
1323 | V4L2_CAP_READWRITE;
1326 mutex_unlock(&gspca_dev->usb_lock);
1330 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1333 const struct ctrl *ctrls;
1336 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1337 i < gspca_dev->sd_desc->nctrls;
1339 if (gspca_dev->ctrl_dis & (1 << i))
1341 if (id == ctrls->qctrl.id)
1347 static int vidioc_queryctrl(struct file *file, void *priv,
1348 struct v4l2_queryctrl *q_ctrl)
1350 struct gspca_dev *gspca_dev = priv;
1351 const struct ctrl *ctrls;
1357 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1358 id &= V4L2_CTRL_ID_MASK;
1360 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1361 if (gspca_dev->ctrl_dis & (1 << i))
1363 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1365 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1368 ctrls = &gspca_dev->sd_desc->ctrls[i];
1373 ctrls = get_ctrl(gspca_dev, id);
1376 i = ctrls - gspca_dev->sd_desc->ctrls;
1378 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1379 if (gspca_dev->ctrl_inac & (1 << i))
1380 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1384 static int vidioc_s_ctrl(struct file *file, void *priv,
1385 struct v4l2_control *ctrl)
1387 struct gspca_dev *gspca_dev = priv;
1388 const struct ctrl *ctrls;
1391 ctrls = get_ctrl(gspca_dev, ctrl->id);
1395 if (ctrl->value < ctrls->qctrl.minimum
1396 || ctrl->value > ctrls->qctrl.maximum)
1398 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1399 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1400 return -ERESTARTSYS;
1401 gspca_dev->usb_err = 0;
1402 if (gspca_dev->present)
1403 ret = ctrls->set(gspca_dev, ctrl->value);
1406 mutex_unlock(&gspca_dev->usb_lock);
1410 static int vidioc_g_ctrl(struct file *file, void *priv,
1411 struct v4l2_control *ctrl)
1413 struct gspca_dev *gspca_dev = priv;
1414 const struct ctrl *ctrls;
1417 ctrls = get_ctrl(gspca_dev, ctrl->id);
1421 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1422 return -ERESTARTSYS;
1423 gspca_dev->usb_err = 0;
1424 if (gspca_dev->present)
1425 ret = ctrls->get(gspca_dev, &ctrl->value);
1428 mutex_unlock(&gspca_dev->usb_lock);
1432 /*fixme: have an audio flag in gspca_dev?*/
1433 static int vidioc_s_audio(struct file *file, void *priv,
1434 struct v4l2_audio *audio)
1436 if (audio->index != 0)
1441 static int vidioc_g_audio(struct file *file, void *priv,
1442 struct v4l2_audio *audio)
1444 strcpy(audio->name, "Microphone");
1448 static int vidioc_enumaudio(struct file *file, void *priv,
1449 struct v4l2_audio *audio)
1451 if (audio->index != 0)
1454 strcpy(audio->name, "Microphone");
1455 audio->capability = 0;
1460 static int vidioc_querymenu(struct file *file, void *priv,
1461 struct v4l2_querymenu *qmenu)
1463 struct gspca_dev *gspca_dev = priv;
1465 if (!gspca_dev->sd_desc->querymenu)
1467 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1470 static int vidioc_enum_input(struct file *file, void *priv,
1471 struct v4l2_input *input)
1473 struct gspca_dev *gspca_dev = priv;
1475 if (input->index != 0)
1477 input->type = V4L2_INPUT_TYPE_CAMERA;
1478 input->status = gspca_dev->cam.input_flags;
1479 strncpy(input->name, gspca_dev->sd_desc->name,
1480 sizeof input->name);
1484 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1490 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1497 static int vidioc_reqbufs(struct file *file, void *priv,
1498 struct v4l2_requestbuffers *rb)
1500 struct gspca_dev *gspca_dev = priv;
1503 switch (rb->memory) {
1504 case GSPCA_MEMORY_READ: /* (internal call) */
1505 case V4L2_MEMORY_MMAP:
1506 case V4L2_MEMORY_USERPTR:
1511 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1512 return -ERESTARTSYS;
1514 if (gspca_dev->memory != GSPCA_MEMORY_NO
1515 && gspca_dev->memory != rb->memory) {
1520 /* only one file may do the capture */
1521 if (gspca_dev->capt_file != NULL
1522 && gspca_dev->capt_file != file) {
1527 /* if allocated, the buffers must not be mapped */
1528 for (i = 0; i < gspca_dev->nframes; i++) {
1529 if (gspca_dev->frame[i].vma_use_count) {
1535 /* stop streaming */
1536 if (gspca_dev->streaming) {
1537 mutex_lock(&gspca_dev->usb_lock);
1538 gspca_dev->usb_err = 0;
1539 gspca_stream_off(gspca_dev);
1540 mutex_unlock(&gspca_dev->usb_lock);
1543 /* free the previous allocated buffers, if any */
1544 if (gspca_dev->nframes != 0) {
1545 frame_free(gspca_dev);
1546 gspca_dev->capt_file = NULL;
1548 if (rb->count == 0) /* unrequest */
1550 gspca_dev->memory = rb->memory;
1551 ret = frame_alloc(gspca_dev, rb->count);
1553 rb->count = gspca_dev->nframes;
1554 gspca_dev->capt_file = file;
1557 mutex_unlock(&gspca_dev->queue_lock);
1558 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1562 static int vidioc_querybuf(struct file *file, void *priv,
1563 struct v4l2_buffer *v4l2_buf)
1565 struct gspca_dev *gspca_dev = priv;
1566 struct gspca_frame *frame;
1568 if (v4l2_buf->index < 0
1569 || v4l2_buf->index >= gspca_dev->nframes)
1572 frame = &gspca_dev->frame[v4l2_buf->index];
1573 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1577 static int vidioc_streamon(struct file *file, void *priv,
1578 enum v4l2_buf_type buf_type)
1580 struct gspca_dev *gspca_dev = priv;
1583 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1585 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1586 return -ERESTARTSYS;
1588 if (gspca_dev->nframes == 0
1589 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1593 if (!gspca_dev->streaming) {
1594 ret = gspca_init_transfer(gspca_dev);
1599 if (gspca_debug & D_STREAM) {
1600 PDEBUG_MODE("stream on OK",
1608 mutex_unlock(&gspca_dev->queue_lock);
1612 static int vidioc_streamoff(struct file *file, void *priv,
1613 enum v4l2_buf_type buf_type)
1615 struct gspca_dev *gspca_dev = priv;
1618 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1620 if (!gspca_dev->streaming)
1622 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1623 return -ERESTARTSYS;
1625 /* stop streaming */
1626 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1630 gspca_dev->usb_err = 0;
1631 gspca_stream_off(gspca_dev);
1632 mutex_unlock(&gspca_dev->usb_lock);
1634 /* empty the application queues */
1635 for (i = 0; i < gspca_dev->nframes; i++)
1636 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1637 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1638 gspca_dev->last_packet_type = DISCARD_PACKET;
1639 gspca_dev->sequence = 0;
1642 mutex_unlock(&gspca_dev->queue_lock);
1646 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1647 struct v4l2_jpegcompression *jpegcomp)
1649 struct gspca_dev *gspca_dev = priv;
1652 if (!gspca_dev->sd_desc->get_jcomp)
1654 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1655 return -ERESTARTSYS;
1656 gspca_dev->usb_err = 0;
1657 if (gspca_dev->present)
1658 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1661 mutex_unlock(&gspca_dev->usb_lock);
1665 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1666 struct v4l2_jpegcompression *jpegcomp)
1668 struct gspca_dev *gspca_dev = priv;
1671 if (!gspca_dev->sd_desc->set_jcomp)
1673 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1674 return -ERESTARTSYS;
1675 gspca_dev->usb_err = 0;
1676 if (gspca_dev->present)
1677 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1680 mutex_unlock(&gspca_dev->usb_lock);
1684 static int vidioc_g_parm(struct file *filp, void *priv,
1685 struct v4l2_streamparm *parm)
1687 struct gspca_dev *gspca_dev = priv;
1689 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1691 if (gspca_dev->sd_desc->get_streamparm) {
1694 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1695 return -ERESTARTSYS;
1696 gspca_dev->usb_err = 0;
1697 if (gspca_dev->present)
1698 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1702 mutex_unlock(&gspca_dev->usb_lock);
1709 static int vidioc_s_parm(struct file *filp, void *priv,
1710 struct v4l2_streamparm *parm)
1712 struct gspca_dev *gspca_dev = priv;
1715 n = parm->parm.capture.readbuffers;
1716 if (n == 0 || n > GSPCA_MAX_FRAMES)
1717 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1719 gspca_dev->nbufread = n;
1721 if (gspca_dev->sd_desc->set_streamparm) {
1724 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1725 return -ERESTARTSYS;
1726 gspca_dev->usb_err = 0;
1727 if (gspca_dev->present)
1728 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1732 mutex_unlock(&gspca_dev->usb_lock);
1739 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1740 static int vidiocgmbuf(struct file *file, void *priv,
1741 struct video_mbuf *mbuf)
1743 struct gspca_dev *gspca_dev = file->private_data;
1746 PDEBUG(D_STREAM, "cgmbuf");
1747 if (gspca_dev->nframes == 0) {
1751 struct v4l2_format fmt;
1753 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1754 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1755 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1756 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1757 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1758 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1763 struct v4l2_requestbuffers rb;
1765 memset(&rb, 0, sizeof rb);
1767 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1768 rb.memory = V4L2_MEMORY_MMAP;
1769 ret = vidioc_reqbufs(file, priv, &rb);
1774 mbuf->frames = gspca_dev->nframes;
1775 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1776 for (i = 0; i < mbuf->frames; i++)
1777 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1782 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1784 struct gspca_dev *gspca_dev = file->private_data;
1785 struct gspca_frame *frame;
1787 unsigned long addr, start, size;
1790 start = vma->vm_start;
1791 size = vma->vm_end - vma->vm_start;
1792 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1794 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1795 return -ERESTARTSYS;
1796 if (!gspca_dev->present) {
1800 if (gspca_dev->capt_file != file) {
1806 for (i = 0; i < gspca_dev->nframes; ++i) {
1807 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1808 PDEBUG(D_STREAM, "mmap bad memory type");
1811 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1813 frame = &gspca_dev->frame[i];
1817 if (frame == NULL) {
1818 PDEBUG(D_STREAM, "mmap no frame buffer found");
1822 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1823 /* v4l1 maps all the buffers */
1825 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1827 if (size != frame->v4l2_buf.length) {
1828 PDEBUG(D_STREAM, "mmap bad size");
1834 * - VM_IO marks the area as being a mmaped region for I/O to a
1835 * device. It also prevents the region from being core dumped.
1837 vma->vm_flags |= VM_IO;
1839 addr = (unsigned long) frame->data;
1841 page = vmalloc_to_page((void *) addr);
1842 ret = vm_insert_page(vma, start, page);
1850 vma->vm_ops = &gspca_vm_ops;
1851 vma->vm_private_data = frame;
1855 mutex_unlock(&gspca_dev->queue_lock);
1860 * wait for a video frame
1862 * If a frame is ready, its index is returned.
1864 static int frame_wait(struct gspca_dev *gspca_dev,
1867 struct gspca_frame *frame;
1870 /* check if a frame is ready */
1871 i = gspca_dev->fr_o;
1872 j = gspca_dev->fr_queue[i];
1873 frame = &gspca_dev->frame[j];
1875 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1879 /* wait till a frame is ready */
1880 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1881 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1882 !gspca_dev->streaming || !gspca_dev->present,
1883 msecs_to_jiffies(3000));
1886 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1890 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1891 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1896 if (gspca_dev->sd_desc->dq_callback) {
1897 mutex_lock(&gspca_dev->usb_lock);
1898 gspca_dev->usb_err = 0;
1899 if (gspca_dev->present)
1900 gspca_dev->sd_desc->dq_callback(gspca_dev);
1901 mutex_unlock(&gspca_dev->usb_lock);
1907 * dequeue a video buffer
1909 * If nonblock_ing is false, block until a buffer is available.
1911 static int vidioc_dqbuf(struct file *file, void *priv,
1912 struct v4l2_buffer *v4l2_buf)
1914 struct gspca_dev *gspca_dev = priv;
1915 struct gspca_frame *frame;
1918 PDEBUG(D_FRAM, "dqbuf");
1919 if (v4l2_buf->memory != gspca_dev->memory)
1922 if (!gspca_dev->present)
1925 /* if not streaming, be sure the application will not loop forever */
1926 if (!(file->f_flags & O_NONBLOCK)
1927 && !gspca_dev->streaming && gspca_dev->users == 1)
1930 /* only the capturing file may dequeue */
1931 if (gspca_dev->capt_file != file)
1934 /* only one dequeue / read at a time */
1935 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1936 return -ERESTARTSYS;
1938 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1941 i = ret; /* frame index */
1942 frame = &gspca_dev->frame[i];
1943 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1944 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1946 frame->v4l2_buf.bytesused)) {
1947 PDEBUG(D_ERR|D_STREAM,
1948 "dqbuf cp to user failed");
1953 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1954 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1955 PDEBUG(D_FRAM, "dqbuf %d", i);
1958 mutex_unlock(&gspca_dev->read_lock);
1963 * queue a video buffer
1965 * Attempting to queue a buffer that has already been
1966 * queued will return -EINVAL.
1968 static int vidioc_qbuf(struct file *file, void *priv,
1969 struct v4l2_buffer *v4l2_buf)
1971 struct gspca_dev *gspca_dev = priv;
1972 struct gspca_frame *frame;
1975 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1977 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1978 return -ERESTARTSYS;
1980 index = v4l2_buf->index;
1981 if ((unsigned) index >= gspca_dev->nframes) {
1983 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1987 if (v4l2_buf->memory != gspca_dev->memory) {
1988 PDEBUG(D_FRAM, "qbuf bad memory type");
1993 frame = &gspca_dev->frame[index];
1994 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1995 PDEBUG(D_FRAM, "qbuf bad state");
2000 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
2002 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
2003 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2004 frame->v4l2_buf.length = v4l2_buf->length;
2007 /* put the buffer in the 'queued' queue */
2008 i = gspca_dev->fr_q;
2009 gspca_dev->fr_queue[i] = index;
2010 if (gspca_dev->fr_i == i)
2011 gspca_dev->cur_frame = frame;
2012 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
2013 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
2018 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2019 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2022 mutex_unlock(&gspca_dev->queue_lock);
2027 * allocate the resources for read()
2029 static int read_alloc(struct gspca_dev *gspca_dev,
2032 struct v4l2_buffer v4l2_buf;
2035 PDEBUG(D_STREAM, "read alloc");
2036 if (gspca_dev->nframes == 0) {
2037 struct v4l2_requestbuffers rb;
2039 memset(&rb, 0, sizeof rb);
2040 rb.count = gspca_dev->nbufread;
2041 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2042 rb.memory = GSPCA_MEMORY_READ;
2043 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2045 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2048 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2049 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2050 v4l2_buf.memory = GSPCA_MEMORY_READ;
2051 for (i = 0; i < gspca_dev->nbufread; i++) {
2053 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2055 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2059 gspca_dev->memory = GSPCA_MEMORY_READ;
2062 /* start streaming */
2063 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2065 PDEBUG(D_STREAM, "read streamon err %d", ret);
2069 static unsigned int dev_poll(struct file *file, poll_table *wait)
2071 struct gspca_dev *gspca_dev = file->private_data;
2074 PDEBUG(D_FRAM, "poll");
2076 poll_wait(file, &gspca_dev->wq, wait);
2078 /* if reqbufs is not done, the user would use read() */
2079 if (gspca_dev->nframes == 0) {
2080 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2081 return POLLERR; /* not the 1st time */
2082 ret = read_alloc(gspca_dev, file);
2087 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2090 /* check the next incoming buffer */
2091 i = gspca_dev->fr_o;
2092 i = gspca_dev->fr_queue[i];
2093 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2094 ret = POLLIN | POLLRDNORM; /* something to read */
2097 mutex_unlock(&gspca_dev->queue_lock);
2098 if (!gspca_dev->present)
2103 static ssize_t dev_read(struct file *file, char __user *data,
2104 size_t count, loff_t *ppos)
2106 struct gspca_dev *gspca_dev = file->private_data;
2107 struct gspca_frame *frame;
2108 struct v4l2_buffer v4l2_buf;
2109 struct timeval timestamp;
2112 PDEBUG(D_FRAM, "read (%zd)", count);
2113 if (!gspca_dev->present)
2115 switch (gspca_dev->memory) {
2116 case GSPCA_MEMORY_NO: /* first time */
2117 ret = read_alloc(gspca_dev, file);
2121 case GSPCA_MEMORY_READ:
2122 if (gspca_dev->capt_file == file)
2130 jiffies_to_timeval(get_jiffies_64(), ×tamp);
2134 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2135 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2136 v4l2_buf.memory = GSPCA_MEMORY_READ;
2137 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2139 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2143 /* if the process slept for more than 1 second,
2144 * get a newer frame */
2145 frame = &gspca_dev->frame[v4l2_buf.index];
2147 break; /* avoid infinite loop */
2148 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2150 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2152 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2157 /* copy the frame */
2158 if (count > frame->v4l2_buf.bytesused)
2159 count = frame->v4l2_buf.bytesused;
2160 ret = copy_to_user(data, frame->data, count);
2162 PDEBUG(D_ERR|D_STREAM,
2163 "read cp to user lack %d / %zd", ret, count);
2169 /* in each case, requeue the buffer */
2170 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2176 static struct v4l2_file_operations dev_fops = {
2177 .owner = THIS_MODULE,
2179 .release = dev_close,
2182 .unlocked_ioctl = video_ioctl2,
2186 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2187 .vidioc_querycap = vidioc_querycap,
2188 .vidioc_dqbuf = vidioc_dqbuf,
2189 .vidioc_qbuf = vidioc_qbuf,
2190 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2191 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2192 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2193 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2194 .vidioc_streamon = vidioc_streamon,
2195 .vidioc_queryctrl = vidioc_queryctrl,
2196 .vidioc_g_ctrl = vidioc_g_ctrl,
2197 .vidioc_s_ctrl = vidioc_s_ctrl,
2198 .vidioc_g_audio = vidioc_g_audio,
2199 .vidioc_s_audio = vidioc_s_audio,
2200 .vidioc_enumaudio = vidioc_enumaudio,
2201 .vidioc_querymenu = vidioc_querymenu,
2202 .vidioc_enum_input = vidioc_enum_input,
2203 .vidioc_g_input = vidioc_g_input,
2204 .vidioc_s_input = vidioc_s_input,
2205 .vidioc_reqbufs = vidioc_reqbufs,
2206 .vidioc_querybuf = vidioc_querybuf,
2207 .vidioc_streamoff = vidioc_streamoff,
2208 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2209 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2210 .vidioc_g_parm = vidioc_g_parm,
2211 .vidioc_s_parm = vidioc_s_parm,
2212 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2213 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2214 #ifdef CONFIG_VIDEO_ADV_DEBUG
2215 .vidioc_g_register = vidioc_g_register,
2216 .vidioc_s_register = vidioc_s_register,
2218 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2219 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2220 .vidiocgmbuf = vidiocgmbuf,
2224 static struct video_device gspca_template = {
2225 .name = "gspca main driver",
2227 .ioctl_ops = &dev_ioctl_ops,
2228 .release = gspca_release,
2232 * probe and create a new gspca device
2234 * This function must be called by the sub-driver when it is
2235 * called for probing a new device.
2237 int gspca_dev_probe(struct usb_interface *intf,
2238 const struct usb_device_id *id,
2239 const struct sd_desc *sd_desc,
2241 struct module *module)
2243 struct usb_interface_descriptor *interface;
2244 struct gspca_dev *gspca_dev;
2245 struct usb_device *dev = interface_to_usbdev(intf);
2248 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2250 /* we don't handle multi-config cameras */
2251 if (dev->descriptor.bNumConfigurations != 1) {
2252 PDEBUG(D_ERR, "Too many config");
2256 /* the USB video interface must be the first one */
2257 interface = &intf->cur_altsetting->desc;
2258 if (dev->config->desc.bNumInterfaces != 1 &&
2259 interface->bInterfaceNumber != 0)
2262 /* create the device */
2263 if (dev_size < sizeof *gspca_dev)
2264 dev_size = sizeof *gspca_dev;
2265 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2267 err("couldn't kzalloc gspca struct");
2270 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2271 if (!gspca_dev->usb_buf) {
2272 err("out of memory");
2276 gspca_dev->dev = dev;
2277 gspca_dev->iface = interface->bInterfaceNumber;
2278 gspca_dev->nbalt = intf->num_altsetting;
2279 gspca_dev->sd_desc = sd_desc;
2280 gspca_dev->nbufread = 2;
2281 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2283 /* configure the subdriver and initialize the USB device */
2284 ret = sd_desc->config(gspca_dev, id);
2287 ret = sd_desc->init(gspca_dev);
2290 gspca_set_default_mode(gspca_dev);
2292 mutex_init(&gspca_dev->usb_lock);
2293 mutex_init(&gspca_dev->read_lock);
2294 mutex_init(&gspca_dev->queue_lock);
2295 init_waitqueue_head(&gspca_dev->wq);
2297 /* init video stuff */
2298 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2299 gspca_dev->vdev.parent = &intf->dev;
2300 gspca_dev->module = module;
2301 gspca_dev->present = 1;
2302 ret = video_register_device(&gspca_dev->vdev,
2306 err("video_register_device err %d", ret);
2310 usb_set_intfdata(intf, gspca_dev);
2311 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2313 ret = gspca_input_connect(gspca_dev);
2315 ret = gspca_input_create_urb(gspca_dev);
2319 kfree(gspca_dev->usb_buf);
2323 EXPORT_SYMBOL(gspca_dev_probe);
2328 * This function must be called by the sub-driver
2329 * when the device disconnects, after the specific resources are freed.
2331 void gspca_disconnect(struct usb_interface *intf)
2333 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2335 struct input_dev *input_dev;
2338 PDEBUG(D_PROBE, "%s disconnect",
2339 video_device_node_name(&gspca_dev->vdev));
2340 mutex_lock(&gspca_dev->usb_lock);
2341 gspca_dev->present = 0;
2343 if (gspca_dev->streaming) {
2344 destroy_urbs(gspca_dev);
2345 wake_up_interruptible(&gspca_dev->wq);
2349 gspca_input_destroy_urb(gspca_dev);
2350 input_dev = gspca_dev->input_dev;
2352 gspca_dev->input_dev = NULL;
2353 input_unregister_device(input_dev);
2357 /* the device is freed at exit of this function */
2358 gspca_dev->dev = NULL;
2359 mutex_unlock(&gspca_dev->usb_lock);
2361 usb_set_intfdata(intf, NULL);
2363 /* release the device */
2364 /* (this will call gspca_release() immediatly or on last close) */
2365 video_unregister_device(&gspca_dev->vdev);
2367 /* PDEBUG(D_PROBE, "disconnect complete"); */
2369 EXPORT_SYMBOL(gspca_disconnect);
2372 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2374 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2376 if (!gspca_dev->streaming)
2378 gspca_dev->frozen = 1; /* avoid urb error messages */
2379 if (gspca_dev->sd_desc->stopN)
2380 gspca_dev->sd_desc->stopN(gspca_dev);
2381 destroy_urbs(gspca_dev);
2382 gspca_input_destroy_urb(gspca_dev);
2383 gspca_set_alt0(gspca_dev);
2384 if (gspca_dev->sd_desc->stop0)
2385 gspca_dev->sd_desc->stop0(gspca_dev);
2388 EXPORT_SYMBOL(gspca_suspend);
2390 int gspca_resume(struct usb_interface *intf)
2392 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2394 gspca_dev->frozen = 0;
2395 gspca_dev->sd_desc->init(gspca_dev);
2396 gspca_input_create_urb(gspca_dev);
2397 if (gspca_dev->streaming)
2398 return gspca_init_transfer(gspca_dev);
2401 EXPORT_SYMBOL(gspca_resume);
2403 /* -- cam driver utility functions -- */
2405 /* auto gain and exposure algorithm based on the knee algorithm described here:
2406 http://ytse.tricolour.net/docs/LowLightOptimization.html
2408 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2410 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2411 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2413 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2414 const struct ctrl *gain_ctrl = NULL;
2415 const struct ctrl *exposure_ctrl = NULL;
2416 const struct ctrl *autogain_ctrl = NULL;
2419 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2420 if (gspca_dev->ctrl_dis & (1 << i))
2422 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2423 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2424 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2425 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2426 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2427 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2429 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2430 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2431 "on cam without (auto)gain/exposure");
2435 if (gain_ctrl->get(gspca_dev, &gain) ||
2436 exposure_ctrl->get(gspca_dev, &exposure) ||
2437 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2441 orig_exposure = exposure;
2443 /* If we are of a multiple of deadzone, do multiple steps to reach the
2444 desired lumination fast (with the risc of a slight overshoot) */
2445 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2447 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2448 avg_lum, desired_avg_lum, steps);
2450 for (i = 0; i < steps; i++) {
2451 if (avg_lum > desired_avg_lum) {
2452 if (gain > gain_knee)
2454 else if (exposure > exposure_knee)
2456 else if (gain > gain_ctrl->qctrl.default_value)
2458 else if (exposure > exposure_ctrl->qctrl.minimum)
2460 else if (gain > gain_ctrl->qctrl.minimum)
2465 if (gain < gain_ctrl->qctrl.default_value)
2467 else if (exposure < exposure_knee)
2469 else if (gain < gain_knee)
2471 else if (exposure < exposure_ctrl->qctrl.maximum)
2473 else if (gain < gain_ctrl->qctrl.maximum)
2480 if (gain != orig_gain) {
2481 gain_ctrl->set(gspca_dev, gain);
2484 if (exposure != orig_exposure) {
2485 exposure_ctrl->set(gspca_dev, exposure);
2491 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2493 /* -- module insert / remove -- */
2494 static int __init gspca_init(void)
2496 info("main v%d.%d.%d registered",
2497 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2498 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2499 DRIVER_VERSION_NUMBER & 0xff);
2502 static void __exit gspca_exit(void)
2504 info("main deregistered");
2507 module_init(gspca_init);
2508 module_exit(gspca_exit);
2511 module_param_named(debug, gspca_debug, int, 0644);
2512 MODULE_PARM_DESC(debug,
2513 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2514 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"