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/ktime.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;
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 = 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_buffer_alloc(dev, ep->wMaxPacketSize,
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);
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_buffer_free(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
283 #define gspca_input_connect(gspca_dev) 0
284 #define gspca_input_create_urb(gspca_dev)
285 #define gspca_input_destroy_urb(gspca_dev)
288 /* get the current input frame buffer */
289 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
291 struct gspca_frame *frame;
293 frame = gspca_dev->cur_frame;
294 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
295 != V4L2_BUF_FLAG_QUEUED)
299 EXPORT_SYMBOL(gspca_get_i_frame);
302 * fill a video frame from an URB and resubmit
304 static void fill_frame(struct gspca_dev *gspca_dev,
307 u8 *data; /* address of data in the iso message */
311 if (urb->status != 0) {
312 if (urb->status == -ESHUTDOWN)
313 return; /* disconnection */
315 if (gspca_dev->frozen)
318 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
322 pkt_scan = gspca_dev->sd_desc->pkt_scan;
323 for (i = 0; i < urb->number_of_packets; i++) {
325 /* check the packet status and length */
326 len = urb->iso_frame_desc[i].actual_length;
328 if (gspca_dev->empty_packet == 0)
329 gspca_dev->empty_packet = 1;
332 st = urb->iso_frame_desc[i].status;
335 "ISOC data error: [%d] len=%d, status=%d",
337 gspca_dev->last_packet_type = DISCARD_PACKET;
341 /* let the packet be analyzed by the subdriver */
342 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
343 i, urb->iso_frame_desc[i].offset, len);
344 data = (u8 *) urb->transfer_buffer
345 + urb->iso_frame_desc[i].offset;
346 pkt_scan(gspca_dev, data, len);
350 /* resubmit the URB */
351 st = usb_submit_urb(urb, GFP_ATOMIC);
353 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
357 * ISOC message interrupt from the USB device
359 * Analyse each packet and call the subdriver for copy to the frame buffer.
361 static void isoc_irq(struct urb *urb)
363 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
365 PDEBUG(D_PACK, "isoc irq");
366 if (!gspca_dev->streaming)
368 fill_frame(gspca_dev, urb);
372 * bulk message interrupt from the USB device
374 static void bulk_irq(struct urb *urb)
376 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
379 PDEBUG(D_PACK, "bulk irq");
380 if (!gspca_dev->streaming)
382 switch (urb->status) {
386 return; /* disconnection */
389 if (gspca_dev->frozen)
392 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
397 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
398 gspca_dev->sd_desc->pkt_scan(gspca_dev,
399 urb->transfer_buffer,
403 /* resubmit the URB */
404 if (gspca_dev->cam.bulk_nurbs != 0) {
405 st = usb_submit_urb(urb, GFP_ATOMIC);
407 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
412 * add data to the current frame
414 * This function is called by the subdrivers at interrupt level.
416 * To build a frame, these ones must add
418 * - 0 or many INTER_PACKETs
420 * DISCARD_PACKET invalidates the whole frame.
421 * On LAST_PACKET, a new frame is returned.
423 void gspca_frame_add(struct gspca_dev *gspca_dev,
424 enum gspca_packet_type packet_type,
428 struct gspca_frame *frame;
431 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
433 /* check the availability of the frame buffer */
434 frame = gspca_dev->cur_frame;
435 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
436 != V4L2_BUF_FLAG_QUEUED) {
437 gspca_dev->last_packet_type = DISCARD_PACKET;
441 /* when start of a new frame, if the current frame buffer
442 * is not queued, discard the whole frame */
443 if (packet_type == FIRST_PACKET) {
444 frame->data_end = frame->data;
445 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
446 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
447 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
448 if (packet_type == LAST_PACKET)
449 gspca_dev->last_packet_type = packet_type;
453 /* append the packet to the frame buffer */
455 if (frame->data_end - frame->data + len
456 > frame->v4l2_buf.length) {
457 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
458 frame->data_end - frame->data + len,
459 frame->v4l2_buf.length);
460 packet_type = DISCARD_PACKET;
462 memcpy(frame->data_end, data, len);
463 frame->data_end += len;
466 gspca_dev->last_packet_type = packet_type;
468 /* if last packet, wake up the application and advance in the queue */
469 if (packet_type == LAST_PACKET) {
470 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
471 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
472 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
473 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
474 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
476 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
477 frame->v4l2_buf.bytesused,
481 j = gspca_dev->fr_queue[i];
482 gspca_dev->cur_frame = &gspca_dev->frame[j];
485 EXPORT_SYMBOL(gspca_frame_add);
487 static int gspca_is_compressed(__u32 format)
490 case V4L2_PIX_FMT_MJPEG:
491 case V4L2_PIX_FMT_JPEG:
492 case V4L2_PIX_FMT_SPCA561:
493 case V4L2_PIX_FMT_PAC207:
494 case V4L2_PIX_FMT_MR97310A:
500 static void *rvmalloc(long size)
505 mem = vmalloc_32(size);
507 adr = (unsigned long) mem;
509 SetPageReserved(vmalloc_to_page((void *) adr));
517 static void rvfree(void *mem, long size)
521 adr = (unsigned long) mem;
523 ClearPageReserved(vmalloc_to_page((void *) adr));
530 static int frame_alloc(struct gspca_dev *gspca_dev,
533 struct gspca_frame *frame;
537 i = gspca_dev->curr_mode;
538 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
539 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
540 frsz = PAGE_ALIGN(frsz);
541 gspca_dev->frsz = frsz;
542 if (count > GSPCA_MAX_FRAMES)
543 count = GSPCA_MAX_FRAMES;
544 gspca_dev->frbuf = rvmalloc(frsz * count);
545 if (!gspca_dev->frbuf) {
546 err("frame alloc failed");
549 gspca_dev->nframes = count;
550 for (i = 0; i < count; i++) {
551 frame = &gspca_dev->frame[i];
552 frame->v4l2_buf.index = i;
553 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
554 frame->v4l2_buf.flags = 0;
555 frame->v4l2_buf.field = V4L2_FIELD_NONE;
556 frame->v4l2_buf.length = frsz;
557 frame->v4l2_buf.memory = gspca_dev->memory;
558 frame->v4l2_buf.sequence = 0;
559 frame->data = frame->data_end =
560 gspca_dev->frbuf + i * frsz;
561 frame->v4l2_buf.m.offset = i * frsz;
563 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
564 gspca_dev->cur_frame = &gspca_dev->frame[0];
565 gspca_dev->last_packet_type = DISCARD_PACKET;
566 gspca_dev->sequence = 0;
570 static void frame_free(struct gspca_dev *gspca_dev)
574 PDEBUG(D_STREAM, "frame free");
575 if (gspca_dev->frbuf != NULL) {
576 rvfree(gspca_dev->frbuf,
577 gspca_dev->nframes * gspca_dev->frsz);
578 gspca_dev->frbuf = NULL;
579 for (i = 0; i < gspca_dev->nframes; i++)
580 gspca_dev->frame[i].data = NULL;
582 gspca_dev->nframes = 0;
585 static void destroy_urbs(struct gspca_dev *gspca_dev)
590 PDEBUG(D_STREAM, "kill transfer");
591 for (i = 0; i < MAX_NURBS; i++) {
592 urb = gspca_dev->urb[i];
596 gspca_dev->urb[i] = NULL;
598 if (urb->transfer_buffer != NULL)
599 usb_buffer_free(gspca_dev->dev,
600 urb->transfer_buffer_length,
601 urb->transfer_buffer,
608 * look for an input transfer endpoint in an alternate setting
610 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
613 struct usb_host_endpoint *ep;
616 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
617 ep = &alt->endpoint[i];
618 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
620 && ep->desc.wMaxPacketSize != 0)
627 * look for an input (isoc or bulk) endpoint
629 * The endpoint is defined by the subdriver.
630 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
631 * This routine may be called many times when the bandwidth is too small
632 * (the bandwidth is checked on urb submit).
634 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
636 struct usb_interface *intf;
637 struct usb_host_endpoint *ep;
640 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
642 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
643 : USB_ENDPOINT_XFER_ISOC;
644 i = gspca_dev->alt; /* previous alt setting */
645 if (gspca_dev->cam.reverse_alts) {
646 while (++i < gspca_dev->nbalt) {
647 ep = alt_xfer(&intf->altsetting[i], xfer);
653 ep = alt_xfer(&intf->altsetting[i], xfer);
659 err("no transfer endpoint found");
662 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
663 i, ep->desc.bEndpointAddress);
664 gspca_dev->alt = i; /* memorize the current alt setting */
665 if (gspca_dev->nbalt > 1) {
666 gspca_input_destroy_urb(gspca_dev);
667 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
669 err("set alt %d err %d", i, ret);
672 gspca_input_create_urb(gspca_dev);
678 * create the URBs for image transfer
680 static int create_urbs(struct gspca_dev *gspca_dev,
681 struct usb_host_endpoint *ep)
684 int n, nurbs, i, psize, npkt, bsize;
686 /* calculate the packet size and the number of packets */
687 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
689 if (!gspca_dev->cam.bulk) { /* isoc */
691 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
692 if (gspca_dev->pkt_size == 0)
693 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
695 psize = gspca_dev->pkt_size;
696 npkt = gspca_dev->cam.npkt;
698 npkt = 32; /* default value */
699 bsize = psize * npkt;
701 "isoc %d pkts size %d = bsize:%d",
706 bsize = gspca_dev->cam.bulk_size;
709 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
710 if (gspca_dev->cam.bulk_nurbs != 0)
711 nurbs = gspca_dev->cam.bulk_nurbs;
716 for (n = 0; n < nurbs; n++) {
717 urb = usb_alloc_urb(npkt, GFP_KERNEL);
719 err("usb_alloc_urb failed");
722 gspca_dev->urb[n] = urb;
723 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
728 if (urb->transfer_buffer == NULL) {
729 err("usb_buffer_alloc failed");
732 urb->dev = gspca_dev->dev;
733 urb->context = gspca_dev;
734 urb->transfer_buffer_length = bsize;
735 if (npkt != 0) { /* ISOC */
736 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
737 ep->desc.bEndpointAddress);
738 urb->transfer_flags = URB_ISO_ASAP
739 | URB_NO_TRANSFER_DMA_MAP;
740 urb->interval = ep->desc.bInterval;
741 urb->complete = isoc_irq;
742 urb->number_of_packets = npkt;
743 for (i = 0; i < npkt; i++) {
744 urb->iso_frame_desc[i].length = psize;
745 urb->iso_frame_desc[i].offset = psize * i;
748 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
749 ep->desc.bEndpointAddress),
750 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
751 urb->complete = bulk_irq;
758 * start the USB transfer
760 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
762 struct usb_host_endpoint *ep;
766 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
769 if (!gspca_dev->present) {
774 gspca_dev->usb_err = 0;
776 /* set the higher alternate setting and
777 * loop until urb submit succeeds */
778 if (gspca_dev->cam.reverse_alts)
781 gspca_dev->alt = gspca_dev->nbalt;
783 if (gspca_dev->sd_desc->isoc_init) {
784 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
788 ep = get_ep(gspca_dev);
794 if (!gspca_dev->cam.no_urb_create) {
795 PDEBUG(D_STREAM, "init transfer alt %d",
797 ret = create_urbs(gspca_dev, ep);
799 destroy_urbs(gspca_dev);
804 /* clear the bulk endpoint */
805 if (gspca_dev->cam.bulk)
806 usb_clear_halt(gspca_dev->dev,
807 gspca_dev->urb[0]->pipe);
810 ret = gspca_dev->sd_desc->start(gspca_dev);
812 destroy_urbs(gspca_dev);
815 gspca_dev->streaming = 1;
817 /* some bulk transfers are started by the subdriver */
818 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
821 /* submit the URBs */
822 for (n = 0; n < MAX_NURBS; n++) {
823 urb = gspca_dev->urb[n];
826 ret = usb_submit_urb(urb, GFP_KERNEL);
832 gspca_dev->streaming = 0;
833 destroy_urbs(gspca_dev);
834 if (ret != -ENOSPC) {
835 PDEBUG(D_ERR|D_STREAM,
836 "usb_submit_urb alt %d err %d",
837 gspca_dev->alt, ret);
841 /* the bandwidth is not wide enough
842 * negociate or try a lower alternate setting */
843 PDEBUG(D_ERR|D_STREAM,
844 "bandwidth not wide enough - trying again");
845 msleep(20); /* wait for kill complete */
846 if (gspca_dev->sd_desc->isoc_nego) {
847 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
851 ep = get_ep(gspca_dev);
859 mutex_unlock(&gspca_dev->usb_lock);
863 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
867 if (gspca_dev->alt == 0)
869 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
871 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
875 /* Note: both the queue and the usb locks should be held when calling this */
876 static void gspca_stream_off(struct gspca_dev *gspca_dev)
878 gspca_dev->streaming = 0;
879 if (gspca_dev->present) {
880 if (gspca_dev->sd_desc->stopN)
881 gspca_dev->sd_desc->stopN(gspca_dev);
882 destroy_urbs(gspca_dev);
883 gspca_input_destroy_urb(gspca_dev);
884 gspca_set_alt0(gspca_dev);
885 gspca_input_create_urb(gspca_dev);
888 /* always call stop0 to free the subdriver's resources */
889 if (gspca_dev->sd_desc->stop0)
890 gspca_dev->sd_desc->stop0(gspca_dev);
891 PDEBUG(D_STREAM, "stream off OK");
894 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
898 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
899 gspca_dev->curr_mode = i;
900 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
901 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
902 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
905 static int wxh_to_mode(struct gspca_dev *gspca_dev,
906 int width, int height)
910 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
911 if (width >= gspca_dev->cam.cam_mode[i].width
912 && height >= gspca_dev->cam.cam_mode[i].height)
919 * search a mode with the right pixel format
921 static int gspca_get_mode(struct gspca_dev *gspca_dev,
927 modeU = modeD = mode;
928 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
930 if (gspca_dev->cam.cam_mode[modeD].pixelformat
934 if (++modeU < gspca_dev->cam.nmodes) {
935 if (gspca_dev->cam.cam_mode[modeU].pixelformat
943 #ifdef CONFIG_VIDEO_ADV_DEBUG
944 static int vidioc_g_register(struct file *file, void *priv,
945 struct v4l2_dbg_register *reg)
948 struct gspca_dev *gspca_dev = priv;
950 if (!gspca_dev->sd_desc->get_chip_ident)
953 if (!gspca_dev->sd_desc->get_register)
956 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
958 gspca_dev->usb_err = 0;
959 if (gspca_dev->present)
960 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
963 mutex_unlock(&gspca_dev->usb_lock);
968 static int vidioc_s_register(struct file *file, void *priv,
969 struct v4l2_dbg_register *reg)
972 struct gspca_dev *gspca_dev = priv;
974 if (!gspca_dev->sd_desc->get_chip_ident)
977 if (!gspca_dev->sd_desc->set_register)
980 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
982 gspca_dev->usb_err = 0;
983 if (gspca_dev->present)
984 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
987 mutex_unlock(&gspca_dev->usb_lock);
993 static int vidioc_g_chip_ident(struct file *file, void *priv,
994 struct v4l2_dbg_chip_ident *chip)
997 struct gspca_dev *gspca_dev = priv;
999 if (!gspca_dev->sd_desc->get_chip_ident)
1002 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1003 return -ERESTARTSYS;
1004 gspca_dev->usb_err = 0;
1005 if (gspca_dev->present)
1006 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1009 mutex_unlock(&gspca_dev->usb_lock);
1014 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1015 struct v4l2_fmtdesc *fmtdesc)
1017 struct gspca_dev *gspca_dev = priv;
1021 /* give an index to each format */
1024 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1025 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1028 if (fmt_tb[j] == fmt_tb[index])
1033 if (fmtdesc->index == index)
1034 break; /* new format */
1036 if (index >= ARRAY_SIZE(fmt_tb))
1041 return -EINVAL; /* no more format */
1043 fmtdesc->pixelformat = fmt_tb[index];
1044 if (gspca_is_compressed(fmt_tb[index]))
1045 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1046 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1047 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1048 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1049 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1050 fmtdesc->description[4] = '\0';
1054 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1055 struct v4l2_format *fmt)
1057 struct gspca_dev *gspca_dev = priv;
1060 mode = gspca_dev->curr_mode;
1061 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1062 sizeof fmt->fmt.pix);
1066 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1067 struct v4l2_format *fmt)
1069 int w, h, mode, mode2;
1071 w = fmt->fmt.pix.width;
1072 h = fmt->fmt.pix.height;
1075 if (gspca_debug & D_CONF)
1076 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1078 /* search the closest mode for width and height */
1079 mode = wxh_to_mode(gspca_dev, w, h);
1081 /* OK if right palette */
1082 if (gspca_dev->cam.cam_mode[mode].pixelformat
1083 != fmt->fmt.pix.pixelformat) {
1085 /* else, search the closest mode with the same pixel format */
1086 mode2 = gspca_get_mode(gspca_dev, mode,
1087 fmt->fmt.pix.pixelformat);
1091 ; * no chance, return this mode */
1093 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1094 sizeof fmt->fmt.pix);
1095 return mode; /* used when s_fmt */
1098 static int vidioc_try_fmt_vid_cap(struct file *file,
1100 struct v4l2_format *fmt)
1102 struct gspca_dev *gspca_dev = priv;
1105 ret = try_fmt_vid_cap(gspca_dev, fmt);
1111 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1112 struct v4l2_format *fmt)
1114 struct gspca_dev *gspca_dev = priv;
1117 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1118 return -ERESTARTSYS;
1120 ret = try_fmt_vid_cap(gspca_dev, fmt);
1124 if (gspca_dev->nframes != 0
1125 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1130 if (ret == gspca_dev->curr_mode) {
1132 goto out; /* same mode */
1135 if (gspca_dev->streaming) {
1139 gspca_dev->width = fmt->fmt.pix.width;
1140 gspca_dev->height = fmt->fmt.pix.height;
1141 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1142 gspca_dev->curr_mode = ret;
1146 mutex_unlock(&gspca_dev->queue_lock);
1150 static int vidioc_enum_framesizes(struct file *file, void *priv,
1151 struct v4l2_frmsizeenum *fsize)
1153 struct gspca_dev *gspca_dev = priv;
1157 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1158 if (fsize->pixel_format !=
1159 gspca_dev->cam.cam_mode[i].pixelformat)
1162 if (fsize->index == index) {
1163 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1164 fsize->discrete.width =
1165 gspca_dev->cam.cam_mode[i].width;
1166 fsize->discrete.height =
1167 gspca_dev->cam.cam_mode[i].height;
1176 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1177 struct v4l2_frmivalenum *fival)
1179 struct gspca_dev *gspca_dev = priv;
1180 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1183 if (gspca_dev->cam.mode_framerates == NULL ||
1184 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1187 if (fival->pixel_format !=
1188 gspca_dev->cam.cam_mode[mode].pixelformat)
1191 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1192 if (fival->index == i) {
1193 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1194 fival->discrete.numerator = 1;
1195 fival->discrete.denominator =
1196 gspca_dev->cam.mode_framerates[mode].rates[i];
1204 static void gspca_release(struct video_device *vfd)
1206 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1208 PDEBUG(D_PROBE, "%s released",
1209 video_device_node_name(&gspca_dev->vdev));
1211 kfree(gspca_dev->usb_buf);
1215 static int dev_open(struct file *file)
1217 struct gspca_dev *gspca_dev;
1220 PDEBUG(D_STREAM, "[%s] open", current->comm);
1221 gspca_dev = (struct gspca_dev *) video_devdata(file);
1222 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1223 return -ERESTARTSYS;
1224 if (!gspca_dev->present) {
1229 if (gspca_dev->users > 4) { /* (arbitrary value) */
1234 /* protect the subdriver against rmmod */
1235 if (!try_module_get(gspca_dev->module)) {
1242 file->private_data = gspca_dev;
1244 /* activate the v4l2 debug */
1245 if (gspca_debug & D_V4L2)
1246 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1247 | V4L2_DEBUG_IOCTL_ARG;
1249 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1250 | V4L2_DEBUG_IOCTL_ARG);
1254 mutex_unlock(&gspca_dev->queue_lock);
1256 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1258 PDEBUG(D_STREAM, "open done");
1262 static int dev_close(struct file *file)
1264 struct gspca_dev *gspca_dev = file->private_data;
1266 PDEBUG(D_STREAM, "[%s] close", current->comm);
1267 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1268 return -ERESTARTSYS;
1271 /* if the file did the capture, free the streaming resources */
1272 if (gspca_dev->capt_file == file) {
1273 if (gspca_dev->streaming) {
1274 mutex_lock(&gspca_dev->usb_lock);
1275 gspca_dev->usb_err = 0;
1276 gspca_stream_off(gspca_dev);
1277 mutex_unlock(&gspca_dev->usb_lock);
1279 frame_free(gspca_dev);
1280 gspca_dev->capt_file = NULL;
1281 gspca_dev->memory = GSPCA_MEMORY_NO;
1283 file->private_data = NULL;
1284 module_put(gspca_dev->module);
1285 mutex_unlock(&gspca_dev->queue_lock);
1287 PDEBUG(D_STREAM, "close done");
1292 static int vidioc_querycap(struct file *file, void *priv,
1293 struct v4l2_capability *cap)
1295 struct gspca_dev *gspca_dev = priv;
1298 /* protect the access to the usb device */
1299 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1300 return -ERESTARTSYS;
1301 if (!gspca_dev->present) {
1305 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1306 if (gspca_dev->dev->product != NULL) {
1307 strncpy(cap->card, gspca_dev->dev->product,
1310 snprintf(cap->card, sizeof cap->card,
1311 "USB Camera (%04x:%04x)",
1312 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1313 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1315 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1316 cap->version = DRIVER_VERSION_NUMBER;
1317 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1318 | V4L2_CAP_STREAMING
1319 | V4L2_CAP_READWRITE;
1322 mutex_unlock(&gspca_dev->usb_lock);
1326 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1329 const struct ctrl *ctrls;
1332 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1333 i < gspca_dev->sd_desc->nctrls;
1335 if (gspca_dev->ctrl_dis & (1 << i))
1337 if (id == ctrls->qctrl.id)
1343 static int vidioc_queryctrl(struct file *file, void *priv,
1344 struct v4l2_queryctrl *q_ctrl)
1346 struct gspca_dev *gspca_dev = priv;
1347 const struct ctrl *ctrls;
1353 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1354 id &= V4L2_CTRL_ID_MASK;
1356 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1357 if (gspca_dev->ctrl_dis & (1 << i))
1359 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1361 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1364 ctrls = &gspca_dev->sd_desc->ctrls[i];
1369 ctrls = get_ctrl(gspca_dev, id);
1372 i = ctrls - gspca_dev->sd_desc->ctrls;
1374 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1375 if (gspca_dev->ctrl_inac & (1 << i))
1376 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1380 static int vidioc_s_ctrl(struct file *file, void *priv,
1381 struct v4l2_control *ctrl)
1383 struct gspca_dev *gspca_dev = priv;
1384 const struct ctrl *ctrls;
1387 ctrls = get_ctrl(gspca_dev, ctrl->id);
1391 if (ctrl->value < ctrls->qctrl.minimum
1392 || ctrl->value > ctrls->qctrl.maximum)
1394 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1395 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1396 return -ERESTARTSYS;
1397 gspca_dev->usb_err = 0;
1398 if (gspca_dev->present)
1399 ret = ctrls->set(gspca_dev, ctrl->value);
1402 mutex_unlock(&gspca_dev->usb_lock);
1406 static int vidioc_g_ctrl(struct file *file, void *priv,
1407 struct v4l2_control *ctrl)
1409 struct gspca_dev *gspca_dev = priv;
1410 const struct ctrl *ctrls;
1413 ctrls = get_ctrl(gspca_dev, ctrl->id);
1417 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1418 return -ERESTARTSYS;
1419 gspca_dev->usb_err = 0;
1420 if (gspca_dev->present)
1421 ret = ctrls->get(gspca_dev, &ctrl->value);
1424 mutex_unlock(&gspca_dev->usb_lock);
1428 /*fixme: have an audio flag in gspca_dev?*/
1429 static int vidioc_s_audio(struct file *file, void *priv,
1430 struct v4l2_audio *audio)
1432 if (audio->index != 0)
1437 static int vidioc_g_audio(struct file *file, void *priv,
1438 struct v4l2_audio *audio)
1440 strcpy(audio->name, "Microphone");
1444 static int vidioc_enumaudio(struct file *file, void *priv,
1445 struct v4l2_audio *audio)
1447 if (audio->index != 0)
1450 strcpy(audio->name, "Microphone");
1451 audio->capability = 0;
1456 static int vidioc_querymenu(struct file *file, void *priv,
1457 struct v4l2_querymenu *qmenu)
1459 struct gspca_dev *gspca_dev = priv;
1461 if (!gspca_dev->sd_desc->querymenu)
1463 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1466 static int vidioc_enum_input(struct file *file, void *priv,
1467 struct v4l2_input *input)
1469 struct gspca_dev *gspca_dev = priv;
1471 if (input->index != 0)
1473 input->type = V4L2_INPUT_TYPE_CAMERA;
1474 input->status = gspca_dev->cam.input_flags;
1475 strncpy(input->name, gspca_dev->sd_desc->name,
1476 sizeof input->name);
1480 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1486 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1493 static int vidioc_reqbufs(struct file *file, void *priv,
1494 struct v4l2_requestbuffers *rb)
1496 struct gspca_dev *gspca_dev = priv;
1499 switch (rb->memory) {
1500 case GSPCA_MEMORY_READ: /* (internal call) */
1501 case V4L2_MEMORY_MMAP:
1502 case V4L2_MEMORY_USERPTR:
1507 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1508 return -ERESTARTSYS;
1510 if (gspca_dev->memory != GSPCA_MEMORY_NO
1511 && gspca_dev->memory != rb->memory) {
1516 /* only one file may do the capture */
1517 if (gspca_dev->capt_file != NULL
1518 && gspca_dev->capt_file != file) {
1523 /* if allocated, the buffers must not be mapped */
1524 for (i = 0; i < gspca_dev->nframes; i++) {
1525 if (gspca_dev->frame[i].vma_use_count) {
1531 /* stop streaming */
1532 if (gspca_dev->streaming) {
1533 mutex_lock(&gspca_dev->usb_lock);
1534 gspca_dev->usb_err = 0;
1535 gspca_stream_off(gspca_dev);
1536 mutex_unlock(&gspca_dev->usb_lock);
1539 /* free the previous allocated buffers, if any */
1540 if (gspca_dev->nframes != 0) {
1541 frame_free(gspca_dev);
1542 gspca_dev->capt_file = NULL;
1544 if (rb->count == 0) /* unrequest */
1546 gspca_dev->memory = rb->memory;
1547 ret = frame_alloc(gspca_dev, rb->count);
1549 rb->count = gspca_dev->nframes;
1550 gspca_dev->capt_file = file;
1553 mutex_unlock(&gspca_dev->queue_lock);
1554 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1558 static int vidioc_querybuf(struct file *file, void *priv,
1559 struct v4l2_buffer *v4l2_buf)
1561 struct gspca_dev *gspca_dev = priv;
1562 struct gspca_frame *frame;
1564 if (v4l2_buf->index < 0
1565 || v4l2_buf->index >= gspca_dev->nframes)
1568 frame = &gspca_dev->frame[v4l2_buf->index];
1569 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1573 static int vidioc_streamon(struct file *file, void *priv,
1574 enum v4l2_buf_type buf_type)
1576 struct gspca_dev *gspca_dev = priv;
1579 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1582 return -ERESTARTSYS;
1584 if (gspca_dev->nframes == 0
1585 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1589 if (!gspca_dev->streaming) {
1590 ret = gspca_init_transfer(gspca_dev);
1595 if (gspca_debug & D_STREAM) {
1596 PDEBUG_MODE("stream on OK",
1604 mutex_unlock(&gspca_dev->queue_lock);
1608 static int vidioc_streamoff(struct file *file, void *priv,
1609 enum v4l2_buf_type buf_type)
1611 struct gspca_dev *gspca_dev = priv;
1614 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1616 if (!gspca_dev->streaming)
1618 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1619 return -ERESTARTSYS;
1621 /* stop streaming */
1622 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1626 gspca_dev->usb_err = 0;
1627 gspca_stream_off(gspca_dev);
1628 mutex_unlock(&gspca_dev->usb_lock);
1630 /* empty the application queues */
1631 for (i = 0; i < gspca_dev->nframes; i++)
1632 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1633 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1634 gspca_dev->last_packet_type = DISCARD_PACKET;
1635 gspca_dev->sequence = 0;
1638 mutex_unlock(&gspca_dev->queue_lock);
1642 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1643 struct v4l2_jpegcompression *jpegcomp)
1645 struct gspca_dev *gspca_dev = priv;
1648 if (!gspca_dev->sd_desc->get_jcomp)
1650 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1651 return -ERESTARTSYS;
1652 gspca_dev->usb_err = 0;
1653 if (gspca_dev->present)
1654 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1657 mutex_unlock(&gspca_dev->usb_lock);
1661 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1662 struct v4l2_jpegcompression *jpegcomp)
1664 struct gspca_dev *gspca_dev = priv;
1667 if (!gspca_dev->sd_desc->set_jcomp)
1669 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1670 return -ERESTARTSYS;
1671 gspca_dev->usb_err = 0;
1672 if (gspca_dev->present)
1673 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1676 mutex_unlock(&gspca_dev->usb_lock);
1680 static int vidioc_g_parm(struct file *filp, void *priv,
1681 struct v4l2_streamparm *parm)
1683 struct gspca_dev *gspca_dev = priv;
1685 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1687 if (gspca_dev->sd_desc->get_streamparm) {
1690 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1691 return -ERESTARTSYS;
1692 gspca_dev->usb_err = 0;
1693 if (gspca_dev->present)
1694 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1698 mutex_unlock(&gspca_dev->usb_lock);
1705 static int vidioc_s_parm(struct file *filp, void *priv,
1706 struct v4l2_streamparm *parm)
1708 struct gspca_dev *gspca_dev = priv;
1711 n = parm->parm.capture.readbuffers;
1712 if (n == 0 || n > GSPCA_MAX_FRAMES)
1713 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1715 gspca_dev->nbufread = n;
1717 if (gspca_dev->sd_desc->set_streamparm) {
1720 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1721 return -ERESTARTSYS;
1722 gspca_dev->usb_err = 0;
1723 if (gspca_dev->present)
1724 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1728 mutex_unlock(&gspca_dev->usb_lock);
1735 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1736 static int vidiocgmbuf(struct file *file, void *priv,
1737 struct video_mbuf *mbuf)
1739 struct gspca_dev *gspca_dev = file->private_data;
1742 PDEBUG(D_STREAM, "cgmbuf");
1743 if (gspca_dev->nframes == 0) {
1747 struct v4l2_format fmt;
1749 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1750 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1751 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1752 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1753 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1754 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1759 struct v4l2_requestbuffers rb;
1761 memset(&rb, 0, sizeof rb);
1763 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1764 rb.memory = V4L2_MEMORY_MMAP;
1765 ret = vidioc_reqbufs(file, priv, &rb);
1770 mbuf->frames = gspca_dev->nframes;
1771 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1772 for (i = 0; i < mbuf->frames; i++)
1773 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1778 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1780 struct gspca_dev *gspca_dev = file->private_data;
1781 struct gspca_frame *frame;
1783 unsigned long addr, start, size;
1786 start = vma->vm_start;
1787 size = vma->vm_end - vma->vm_start;
1788 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1790 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1791 return -ERESTARTSYS;
1792 if (!gspca_dev->present) {
1796 if (gspca_dev->capt_file != file) {
1802 for (i = 0; i < gspca_dev->nframes; ++i) {
1803 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1804 PDEBUG(D_STREAM, "mmap bad memory type");
1807 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1809 frame = &gspca_dev->frame[i];
1813 if (frame == NULL) {
1814 PDEBUG(D_STREAM, "mmap no frame buffer found");
1818 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1819 /* v4l1 maps all the buffers */
1821 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1823 if (size != frame->v4l2_buf.length) {
1824 PDEBUG(D_STREAM, "mmap bad size");
1830 * - VM_IO marks the area as being a mmaped region for I/O to a
1831 * device. It also prevents the region from being core dumped.
1833 vma->vm_flags |= VM_IO;
1835 addr = (unsigned long) frame->data;
1837 page = vmalloc_to_page((void *) addr);
1838 ret = vm_insert_page(vma, start, page);
1846 vma->vm_ops = &gspca_vm_ops;
1847 vma->vm_private_data = frame;
1851 mutex_unlock(&gspca_dev->queue_lock);
1856 * wait for a video frame
1858 * If a frame is ready, its index is returned.
1860 static int frame_wait(struct gspca_dev *gspca_dev,
1863 struct gspca_frame *frame;
1866 /* check if a frame is ready */
1867 i = gspca_dev->fr_o;
1868 j = gspca_dev->fr_queue[i];
1869 frame = &gspca_dev->frame[j];
1871 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1875 /* wait till a frame is ready */
1876 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1877 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1878 !gspca_dev->streaming || !gspca_dev->present,
1879 msecs_to_jiffies(3000));
1882 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1886 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1887 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1892 if (gspca_dev->sd_desc->dq_callback) {
1893 mutex_lock(&gspca_dev->usb_lock);
1894 gspca_dev->usb_err = 0;
1895 if (gspca_dev->present)
1896 gspca_dev->sd_desc->dq_callback(gspca_dev);
1897 mutex_unlock(&gspca_dev->usb_lock);
1903 * dequeue a video buffer
1905 * If nonblock_ing is false, block until a buffer is available.
1907 static int vidioc_dqbuf(struct file *file, void *priv,
1908 struct v4l2_buffer *v4l2_buf)
1910 struct gspca_dev *gspca_dev = priv;
1911 struct gspca_frame *frame;
1914 PDEBUG(D_FRAM, "dqbuf");
1915 if (v4l2_buf->memory != gspca_dev->memory)
1918 if (!gspca_dev->present)
1921 /* if not streaming, be sure the application will not loop forever */
1922 if (!(file->f_flags & O_NONBLOCK)
1923 && !gspca_dev->streaming && gspca_dev->users == 1)
1926 /* only the capturing file may dequeue */
1927 if (gspca_dev->capt_file != file)
1930 /* only one dequeue / read at a time */
1931 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1932 return -ERESTARTSYS;
1934 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1937 i = ret; /* frame index */
1938 frame = &gspca_dev->frame[i];
1939 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1940 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1942 frame->v4l2_buf.bytesused)) {
1943 PDEBUG(D_ERR|D_STREAM,
1944 "dqbuf cp to user failed");
1949 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1950 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1951 PDEBUG(D_FRAM, "dqbuf %d", i);
1954 mutex_unlock(&gspca_dev->read_lock);
1959 * queue a video buffer
1961 * Attempting to queue a buffer that has already been
1962 * queued will return -EINVAL.
1964 static int vidioc_qbuf(struct file *file, void *priv,
1965 struct v4l2_buffer *v4l2_buf)
1967 struct gspca_dev *gspca_dev = priv;
1968 struct gspca_frame *frame;
1971 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1973 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1974 return -ERESTARTSYS;
1976 index = v4l2_buf->index;
1977 if ((unsigned) index >= gspca_dev->nframes) {
1979 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1983 if (v4l2_buf->memory != gspca_dev->memory) {
1984 PDEBUG(D_FRAM, "qbuf bad memory type");
1989 frame = &gspca_dev->frame[index];
1990 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1991 PDEBUG(D_FRAM, "qbuf bad state");
1996 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1998 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1999 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2000 frame->v4l2_buf.length = v4l2_buf->length;
2003 /* put the buffer in the 'queued' queue */
2004 i = gspca_dev->fr_q;
2005 gspca_dev->fr_queue[i] = index;
2006 if (gspca_dev->fr_i == i)
2007 gspca_dev->cur_frame = frame;
2008 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
2009 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
2014 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2015 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2018 mutex_unlock(&gspca_dev->queue_lock);
2023 * allocate the resources for read()
2025 static int read_alloc(struct gspca_dev *gspca_dev,
2028 struct v4l2_buffer v4l2_buf;
2031 PDEBUG(D_STREAM, "read alloc");
2032 if (gspca_dev->nframes == 0) {
2033 struct v4l2_requestbuffers rb;
2035 memset(&rb, 0, sizeof rb);
2036 rb.count = gspca_dev->nbufread;
2037 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2038 rb.memory = GSPCA_MEMORY_READ;
2039 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2041 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2044 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2045 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2046 v4l2_buf.memory = GSPCA_MEMORY_READ;
2047 for (i = 0; i < gspca_dev->nbufread; i++) {
2049 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2051 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2055 gspca_dev->memory = GSPCA_MEMORY_READ;
2058 /* start streaming */
2059 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2061 PDEBUG(D_STREAM, "read streamon err %d", ret);
2065 static unsigned int dev_poll(struct file *file, poll_table *wait)
2067 struct gspca_dev *gspca_dev = file->private_data;
2070 PDEBUG(D_FRAM, "poll");
2072 poll_wait(file, &gspca_dev->wq, wait);
2074 /* if reqbufs is not done, the user would use read() */
2075 if (gspca_dev->nframes == 0) {
2076 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2077 return POLLERR; /* not the 1st time */
2078 ret = read_alloc(gspca_dev, file);
2083 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2086 /* check the next incoming buffer */
2087 i = gspca_dev->fr_o;
2088 i = gspca_dev->fr_queue[i];
2089 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2090 ret = POLLIN | POLLRDNORM; /* something to read */
2093 mutex_unlock(&gspca_dev->queue_lock);
2094 if (!gspca_dev->present)
2099 static ssize_t dev_read(struct file *file, char __user *data,
2100 size_t count, loff_t *ppos)
2102 struct gspca_dev *gspca_dev = file->private_data;
2103 struct gspca_frame *frame;
2104 struct v4l2_buffer v4l2_buf;
2105 struct timeval timestamp;
2108 PDEBUG(D_FRAM, "read (%zd)", count);
2109 if (!gspca_dev->present)
2111 switch (gspca_dev->memory) {
2112 case GSPCA_MEMORY_NO: /* first time */
2113 ret = read_alloc(gspca_dev, file);
2117 case GSPCA_MEMORY_READ:
2118 if (gspca_dev->capt_file == file)
2126 timestamp = ktime_to_timeval(ktime_get());
2130 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2131 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2132 v4l2_buf.memory = GSPCA_MEMORY_READ;
2133 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2135 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2139 /* if the process slept for more than 1 second,
2140 * get a newer frame */
2141 frame = &gspca_dev->frame[v4l2_buf.index];
2143 break; /* avoid infinite loop */
2144 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2146 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2148 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2153 /* copy the frame */
2154 if (count > frame->v4l2_buf.bytesused)
2155 count = frame->v4l2_buf.bytesused;
2156 ret = copy_to_user(data, frame->data, count);
2158 PDEBUG(D_ERR|D_STREAM,
2159 "read cp to user lack %d / %zd", ret, count);
2165 /* in each case, requeue the buffer */
2166 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2172 static struct v4l2_file_operations dev_fops = {
2173 .owner = THIS_MODULE,
2175 .release = dev_close,
2178 .unlocked_ioctl = video_ioctl2,
2182 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2183 .vidioc_querycap = vidioc_querycap,
2184 .vidioc_dqbuf = vidioc_dqbuf,
2185 .vidioc_qbuf = vidioc_qbuf,
2186 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2187 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2188 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2189 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2190 .vidioc_streamon = vidioc_streamon,
2191 .vidioc_queryctrl = vidioc_queryctrl,
2192 .vidioc_g_ctrl = vidioc_g_ctrl,
2193 .vidioc_s_ctrl = vidioc_s_ctrl,
2194 .vidioc_g_audio = vidioc_g_audio,
2195 .vidioc_s_audio = vidioc_s_audio,
2196 .vidioc_enumaudio = vidioc_enumaudio,
2197 .vidioc_querymenu = vidioc_querymenu,
2198 .vidioc_enum_input = vidioc_enum_input,
2199 .vidioc_g_input = vidioc_g_input,
2200 .vidioc_s_input = vidioc_s_input,
2201 .vidioc_reqbufs = vidioc_reqbufs,
2202 .vidioc_querybuf = vidioc_querybuf,
2203 .vidioc_streamoff = vidioc_streamoff,
2204 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2205 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2206 .vidioc_g_parm = vidioc_g_parm,
2207 .vidioc_s_parm = vidioc_s_parm,
2208 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2209 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2210 #ifdef CONFIG_VIDEO_ADV_DEBUG
2211 .vidioc_g_register = vidioc_g_register,
2212 .vidioc_s_register = vidioc_s_register,
2214 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2215 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2216 .vidiocgmbuf = vidiocgmbuf,
2220 static struct video_device gspca_template = {
2221 .name = "gspca main driver",
2223 .ioctl_ops = &dev_ioctl_ops,
2224 .release = gspca_release,
2228 * probe and create a new gspca device
2230 * This function must be called by the sub-driver when it is
2231 * called for probing a new device.
2233 int gspca_dev_probe(struct usb_interface *intf,
2234 const struct usb_device_id *id,
2235 const struct sd_desc *sd_desc,
2237 struct module *module)
2239 struct usb_interface_descriptor *interface;
2240 struct gspca_dev *gspca_dev;
2241 struct usb_device *dev = interface_to_usbdev(intf);
2244 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2246 /* we don't handle multi-config cameras */
2247 if (dev->descriptor.bNumConfigurations != 1) {
2248 PDEBUG(D_ERR, "Too many config");
2252 /* the USB video interface must be the first one */
2253 interface = &intf->cur_altsetting->desc;
2254 if (dev->config->desc.bNumInterfaces != 1 &&
2255 interface->bInterfaceNumber != 0)
2258 /* create the device */
2259 if (dev_size < sizeof *gspca_dev)
2260 dev_size = sizeof *gspca_dev;
2261 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2263 err("couldn't kzalloc gspca struct");
2266 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2267 if (!gspca_dev->usb_buf) {
2268 err("out of memory");
2272 gspca_dev->dev = dev;
2273 gspca_dev->iface = interface->bInterfaceNumber;
2274 gspca_dev->nbalt = intf->num_altsetting;
2275 gspca_dev->sd_desc = sd_desc;
2276 gspca_dev->nbufread = 2;
2277 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2279 /* configure the subdriver and initialize the USB device */
2280 ret = sd_desc->config(gspca_dev, id);
2283 ret = sd_desc->init(gspca_dev);
2286 gspca_set_default_mode(gspca_dev);
2288 ret = gspca_input_connect(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 gspca_input_create_urb(gspca_dev);
2318 if (gspca_dev->input_dev)
2319 input_unregister_device(gspca_dev->input_dev);
2321 kfree(gspca_dev->usb_buf);
2325 EXPORT_SYMBOL(gspca_dev_probe);
2330 * This function must be called by the sub-driver
2331 * when the device disconnects, after the specific resources are freed.
2333 void gspca_disconnect(struct usb_interface *intf)
2335 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2337 struct input_dev *input_dev;
2340 PDEBUG(D_PROBE, "%s disconnect",
2341 video_device_node_name(&gspca_dev->vdev));
2342 mutex_lock(&gspca_dev->usb_lock);
2343 gspca_dev->present = 0;
2345 if (gspca_dev->streaming) {
2346 destroy_urbs(gspca_dev);
2347 wake_up_interruptible(&gspca_dev->wq);
2351 gspca_input_destroy_urb(gspca_dev);
2352 input_dev = gspca_dev->input_dev;
2354 gspca_dev->input_dev = NULL;
2355 input_unregister_device(input_dev);
2359 /* the device is freed at exit of this function */
2360 gspca_dev->dev = NULL;
2361 mutex_unlock(&gspca_dev->usb_lock);
2363 usb_set_intfdata(intf, NULL);
2365 /* release the device */
2366 /* (this will call gspca_release() immediatly or on last close) */
2367 video_unregister_device(&gspca_dev->vdev);
2369 /* PDEBUG(D_PROBE, "disconnect complete"); */
2371 EXPORT_SYMBOL(gspca_disconnect);
2374 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2376 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2378 if (!gspca_dev->streaming)
2380 gspca_dev->frozen = 1; /* avoid urb error messages */
2381 if (gspca_dev->sd_desc->stopN)
2382 gspca_dev->sd_desc->stopN(gspca_dev);
2383 destroy_urbs(gspca_dev);
2384 gspca_input_destroy_urb(gspca_dev);
2385 gspca_set_alt0(gspca_dev);
2386 if (gspca_dev->sd_desc->stop0)
2387 gspca_dev->sd_desc->stop0(gspca_dev);
2390 EXPORT_SYMBOL(gspca_suspend);
2392 int gspca_resume(struct usb_interface *intf)
2394 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2396 gspca_dev->frozen = 0;
2397 gspca_dev->sd_desc->init(gspca_dev);
2398 gspca_input_create_urb(gspca_dev);
2399 if (gspca_dev->streaming)
2400 return gspca_init_transfer(gspca_dev);
2403 EXPORT_SYMBOL(gspca_resume);
2405 /* -- cam driver utility functions -- */
2407 /* auto gain and exposure algorithm based on the knee algorithm described here:
2408 http://ytse.tricolour.net/docs/LowLightOptimization.html
2410 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2412 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2413 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2415 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2416 const struct ctrl *gain_ctrl = NULL;
2417 const struct ctrl *exposure_ctrl = NULL;
2418 const struct ctrl *autogain_ctrl = NULL;
2421 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2422 if (gspca_dev->ctrl_dis & (1 << i))
2424 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2425 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2426 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2427 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2428 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2429 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2431 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2432 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2433 "on cam without (auto)gain/exposure");
2437 if (gain_ctrl->get(gspca_dev, &gain) ||
2438 exposure_ctrl->get(gspca_dev, &exposure) ||
2439 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2443 orig_exposure = exposure;
2445 /* If we are of a multiple of deadzone, do multiple steps to reach the
2446 desired lumination fast (with the risc of a slight overshoot) */
2447 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2449 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2450 avg_lum, desired_avg_lum, steps);
2452 for (i = 0; i < steps; i++) {
2453 if (avg_lum > desired_avg_lum) {
2454 if (gain > gain_knee)
2456 else if (exposure > exposure_knee)
2458 else if (gain > gain_ctrl->qctrl.default_value)
2460 else if (exposure > exposure_ctrl->qctrl.minimum)
2462 else if (gain > gain_ctrl->qctrl.minimum)
2467 if (gain < gain_ctrl->qctrl.default_value)
2469 else if (exposure < exposure_knee)
2471 else if (gain < gain_knee)
2473 else if (exposure < exposure_ctrl->qctrl.maximum)
2475 else if (gain < gain_ctrl->qctrl.maximum)
2482 if (gain != orig_gain) {
2483 gain_ctrl->set(gspca_dev, gain);
2486 if (exposure != orig_exposure) {
2487 exposure_ctrl->set(gspca_dev, exposure);
2493 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2495 /* -- module insert / remove -- */
2496 static int __init gspca_init(void)
2498 info("main v%d.%d.%d registered",
2499 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2500 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2501 DRIVER_VERSION_NUMBER & 0xff);
2504 static void __exit gspca_exit(void)
2506 info("main deregistered");
2509 module_init(gspca_init);
2510 module_exit(gspca_exit);
2513 module_param_named(debug, gspca_debug, int, 0644);
2514 MODULE_PARM_DESC(debug,
2515 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2516 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"