2 * Main USB camera driver
4 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define MODULE_NAME "gspca"
26 #include <linux/init.h>
27 #include <linux/version.h>
29 #include <linux/vmalloc.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
37 #include <linux/uaccess.h>
38 #include <linux/ktime.h>
39 #include <media/v4l2-ioctl.h>
43 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
44 #include <linux/input.h>
45 #include <linux/usb/input.h>
49 #define DEF_NURBS 3 /* default number of URBs */
50 #if DEF_NURBS > MAX_NURBS
51 #error "DEF_NURBS too big"
54 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 10, 0)
61 int gspca_debug = D_ERR | D_PROBE;
62 EXPORT_SYMBOL(gspca_debug);
64 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
71 (pixfmt >> 16) & 0xff,
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102 static void gspca_vm_close(struct vm_area_struct *vma)
104 struct gspca_frame *frame = vma->vm_private_data;
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110 static const struct vm_operations_struct gspca_vm_ops = {
111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb *urb)
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
151 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 err("Input device registration failed with error %i",
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
185 dev->input_dev = input_dev;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
198 struct usb_device *dev;
202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
203 interval = ep->bInterval;
204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
208 dev = gspca_dev->dev;
210 urb = usb_alloc_urb(0, GFP_KERNEL);
216 buffer = usb_alloc_coherent(dev, buffer_len,
217 GFP_KERNEL, &urb->transfer_dma);
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
225 int_irq, (void *)gspca_dev, interval);
226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
227 gspca_dev->int_urb = urb;
228 ret = usb_submit_urb(urb, GFP_KERNEL);
230 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
246 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
248 struct usb_interface *intf;
249 struct usb_host_interface *intf_desc;
250 struct usb_endpoint_descriptor *ep;
253 if (gspca_dev->sd_desc->int_pkt_scan) {
254 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
255 intf_desc = intf->cur_altsetting;
256 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
257 ep = &intf_desc->endpoint[i].desc;
258 if (usb_endpoint_dir_in(ep) &&
259 usb_endpoint_xfer_int(ep)) {
261 alloc_and_submit_int_urb(gspca_dev, ep);
268 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
272 urb = gspca_dev->int_urb;
274 gspca_dev->int_urb = NULL;
276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
284 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
288 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
292 static inline int gspca_input_connect(struct gspca_dev *dev)
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev *gspca_dev,
304 u8 *data; /* address of data in the iso message */
308 if (urb->status != 0) {
309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
312 if (gspca_dev->frozen)
315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
322 /* check the packet status and length */
323 len = urb->iso_frame_desc[i].actual_length;
325 if (gspca_dev->empty_packet == 0)
326 gspca_dev->empty_packet = 1;
329 st = urb->iso_frame_desc[i].status;
331 err("ISOC data error: [%d] len=%d, status=%d",
333 gspca_dev->last_packet_type = DISCARD_PACKET;
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
340 data = (u8 *) urb->transfer_buffer
341 + urb->iso_frame_desc[i].offset;
342 pkt_scan(gspca_dev, data, len);
346 /* resubmit the URB */
347 st = usb_submit_urb(urb, GFP_ATOMIC);
349 err("usb_submit_urb() ret %d", st);
353 * ISOC message interrupt from the USB device
355 * Analyse each packet and call the subdriver for copy to the frame buffer.
357 static void isoc_irq(struct urb *urb)
359 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
361 PDEBUG(D_PACK, "isoc irq");
362 if (!gspca_dev->streaming)
364 fill_frame(gspca_dev, urb);
368 * bulk message interrupt from the USB device
370 static void bulk_irq(struct urb *urb)
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
378 switch (urb->status) {
382 return; /* disconnection */
385 if (gspca_dev->frozen)
388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
395 urb->transfer_buffer,
399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
403 err("usb_submit_urb() ret %d", st);
408 * add data to the current frame
410 * This function is called by the subdrivers at interrupt level.
412 * To build a frame, these ones must add
414 * - 0 or many INTER_PACKETs
416 * DISCARD_PACKET invalidates the whole frame.
417 * On LAST_PACKET, a new frame is returned.
419 void gspca_frame_add(struct gspca_dev *gspca_dev,
420 enum gspca_packet_type packet_type,
424 struct gspca_frame *frame;
427 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
429 if (packet_type == FIRST_PACKET) {
430 i = atomic_read(&gspca_dev->fr_i);
432 /* if there are no queued buffer, discard the whole frame */
433 if (i == atomic_read(&gspca_dev->fr_q)) {
434 gspca_dev->last_packet_type = DISCARD_PACKET;
435 gspca_dev->sequence++;
438 j = gspca_dev->fr_queue[i];
439 frame = &gspca_dev->frame[j];
440 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
441 frame->v4l2_buf.sequence = gspca_dev->sequence++;
442 gspca_dev->image = frame->data;
443 gspca_dev->image_len = 0;
445 switch (gspca_dev->last_packet_type) {
447 if (packet_type == LAST_PACKET)
448 gspca_dev->last_packet_type = packet_type;
455 /* append the packet to the frame buffer */
457 if (gspca_dev->image_len + len > gspca_dev->frsz) {
458 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
459 gspca_dev->image_len + len,
461 packet_type = DISCARD_PACKET;
463 /* !! image is NULL only when last pkt is LAST or DISCARD
464 if (gspca_dev->image == NULL) {
465 err("gspca_frame_add() image == NULL");
469 memcpy(gspca_dev->image + gspca_dev->image_len,
471 gspca_dev->image_len += len;
474 gspca_dev->last_packet_type = packet_type;
476 /* if last packet, invalidate packet concatenation until
477 * next first packet, wake up the application and advance
479 if (packet_type == LAST_PACKET) {
480 i = atomic_read(&gspca_dev->fr_i);
481 j = gspca_dev->fr_queue[i];
482 frame = &gspca_dev->frame[j];
483 frame->v4l2_buf.bytesused = gspca_dev->image_len;
484 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
485 | V4L2_BUF_FLAG_DONE)
486 & ~V4L2_BUF_FLAG_QUEUED;
487 i = (i + 1) % GSPCA_MAX_FRAMES;
488 atomic_set(&gspca_dev->fr_i, i);
489 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
490 PDEBUG(D_FRAM, "frame complete len:%d",
491 frame->v4l2_buf.bytesused);
492 gspca_dev->image = NULL;
493 gspca_dev->image_len = 0;
496 EXPORT_SYMBOL(gspca_frame_add);
498 static int gspca_is_compressed(__u32 format)
501 case V4L2_PIX_FMT_MJPEG:
502 case V4L2_PIX_FMT_JPEG:
503 case V4L2_PIX_FMT_SPCA561:
504 case V4L2_PIX_FMT_PAC207:
505 case V4L2_PIX_FMT_MR97310A:
511 static int frame_alloc(struct gspca_dev *gspca_dev,
514 struct gspca_frame *frame;
518 i = gspca_dev->curr_mode;
519 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
520 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
521 frsz = PAGE_ALIGN(frsz);
522 gspca_dev->frsz = frsz;
523 if (count >= GSPCA_MAX_FRAMES)
524 count = GSPCA_MAX_FRAMES - 1;
525 gspca_dev->frbuf = vmalloc_32(frsz * count);
526 if (!gspca_dev->frbuf) {
527 err("frame alloc failed");
530 gspca_dev->nframes = count;
531 for (i = 0; i < count; i++) {
532 frame = &gspca_dev->frame[i];
533 frame->v4l2_buf.index = i;
534 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
535 frame->v4l2_buf.flags = 0;
536 frame->v4l2_buf.field = V4L2_FIELD_NONE;
537 frame->v4l2_buf.length = frsz;
538 frame->v4l2_buf.memory = gspca_dev->memory;
539 frame->v4l2_buf.sequence = 0;
540 frame->data = gspca_dev->frbuf + i * frsz;
541 frame->v4l2_buf.m.offset = i * frsz;
543 atomic_set(&gspca_dev->fr_q, 0);
544 atomic_set(&gspca_dev->fr_i, 0);
549 static void frame_free(struct gspca_dev *gspca_dev)
553 PDEBUG(D_STREAM, "frame free");
554 if (gspca_dev->frbuf != NULL) {
555 vfree(gspca_dev->frbuf);
556 gspca_dev->frbuf = NULL;
557 for (i = 0; i < gspca_dev->nframes; i++)
558 gspca_dev->frame[i].data = NULL;
560 gspca_dev->nframes = 0;
563 static void destroy_urbs(struct gspca_dev *gspca_dev)
568 PDEBUG(D_STREAM, "kill transfer");
569 for (i = 0; i < MAX_NURBS; i++) {
570 urb = gspca_dev->urb[i];
574 gspca_dev->urb[i] = NULL;
576 if (urb->transfer_buffer != NULL)
577 usb_free_coherent(gspca_dev->dev,
578 urb->transfer_buffer_length,
579 urb->transfer_buffer,
585 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
589 if (gspca_dev->alt == 0)
591 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
593 err("set alt 0 err %d", ret);
597 /* Note: both the queue and the usb locks should be held when calling this */
598 static void gspca_stream_off(struct gspca_dev *gspca_dev)
600 gspca_dev->streaming = 0;
601 if (gspca_dev->present) {
602 if (gspca_dev->sd_desc->stopN)
603 gspca_dev->sd_desc->stopN(gspca_dev);
604 destroy_urbs(gspca_dev);
605 gspca_input_destroy_urb(gspca_dev);
606 gspca_set_alt0(gspca_dev);
607 gspca_input_create_urb(gspca_dev);
610 /* always call stop0 to free the subdriver's resources */
611 if (gspca_dev->sd_desc->stop0)
612 gspca_dev->sd_desc->stop0(gspca_dev);
613 PDEBUG(D_STREAM, "stream off OK");
617 * look for an input transfer endpoint in an alternate setting
619 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
622 struct usb_host_endpoint *ep;
625 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
626 ep = &alt->endpoint[i];
627 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
629 && ep->desc.wMaxPacketSize != 0)
636 * look for an input (isoc or bulk) endpoint
638 * The endpoint is defined by the subdriver.
639 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
640 * This routine may be called many times when the bandwidth is too small
641 * (the bandwidth is checked on urb submit).
643 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
645 struct usb_interface *intf;
646 struct usb_host_endpoint *ep;
649 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
651 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
652 : USB_ENDPOINT_XFER_ISOC;
653 i = gspca_dev->alt; /* previous alt setting */
654 if (gspca_dev->cam.reverse_alts) {
655 if (gspca_dev->audio && i < gspca_dev->nbalt - 2)
657 while (++i < gspca_dev->nbalt) {
658 ep = alt_xfer(&intf->altsetting[i], xfer);
663 if (gspca_dev->audio && i > 1)
666 ep = alt_xfer(&intf->altsetting[i], xfer);
672 err("no transfer endpoint found");
675 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
676 i, ep->desc.bEndpointAddress);
677 gspca_dev->alt = i; /* memorize the current alt setting */
678 if (gspca_dev->nbalt > 1) {
679 gspca_input_destroy_urb(gspca_dev);
680 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
682 err("set alt %d err %d", i, ret);
685 gspca_input_create_urb(gspca_dev);
691 * create the URBs for image transfer
693 static int create_urbs(struct gspca_dev *gspca_dev,
694 struct usb_host_endpoint *ep)
697 int n, nurbs, i, psize, npkt, bsize;
699 /* calculate the packet size and the number of packets */
700 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
702 if (!gspca_dev->cam.bulk) { /* isoc */
704 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
705 if (gspca_dev->pkt_size == 0)
706 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
708 psize = gspca_dev->pkt_size;
709 npkt = gspca_dev->cam.npkt;
711 npkt = 32; /* default value */
712 bsize = psize * npkt;
714 "isoc %d pkts size %d = bsize:%d",
719 bsize = gspca_dev->cam.bulk_size;
722 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
723 if (gspca_dev->cam.bulk_nurbs != 0)
724 nurbs = gspca_dev->cam.bulk_nurbs;
729 for (n = 0; n < nurbs; n++) {
730 urb = usb_alloc_urb(npkt, GFP_KERNEL);
732 err("usb_alloc_urb failed");
735 gspca_dev->urb[n] = urb;
736 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
741 if (urb->transfer_buffer == NULL) {
742 err("usb_alloc_coherent failed");
745 urb->dev = gspca_dev->dev;
746 urb->context = gspca_dev;
747 urb->transfer_buffer_length = bsize;
748 if (npkt != 0) { /* ISOC */
749 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
750 ep->desc.bEndpointAddress);
751 urb->transfer_flags = URB_ISO_ASAP
752 | URB_NO_TRANSFER_DMA_MAP;
753 urb->interval = ep->desc.bInterval;
754 urb->complete = isoc_irq;
755 urb->number_of_packets = npkt;
756 for (i = 0; i < npkt; i++) {
757 urb->iso_frame_desc[i].length = psize;
758 urb->iso_frame_desc[i].offset = psize * i;
761 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
762 ep->desc.bEndpointAddress),
763 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
764 urb->complete = bulk_irq;
771 * start the USB transfer
773 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
775 struct usb_host_endpoint *ep;
779 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
782 if (!gspca_dev->present) {
787 /* reset the streaming variables */
788 gspca_dev->image = NULL;
789 gspca_dev->image_len = 0;
790 gspca_dev->last_packet_type = DISCARD_PACKET;
791 gspca_dev->sequence = 0;
793 gspca_dev->usb_err = 0;
795 /* set the higher alternate setting and
796 * loop until urb submit succeeds */
797 if (gspca_dev->cam.reverse_alts)
800 gspca_dev->alt = gspca_dev->nbalt;
802 if (gspca_dev->sd_desc->isoc_init) {
803 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
807 ep = get_ep(gspca_dev);
813 if (!gspca_dev->cam.no_urb_create) {
814 PDEBUG(D_STREAM, "init transfer alt %d",
816 ret = create_urbs(gspca_dev, ep);
818 destroy_urbs(gspca_dev);
823 /* clear the bulk endpoint */
824 if (gspca_dev->cam.bulk)
825 usb_clear_halt(gspca_dev->dev,
826 gspca_dev->urb[0]->pipe);
829 ret = gspca_dev->sd_desc->start(gspca_dev);
831 destroy_urbs(gspca_dev);
834 gspca_dev->streaming = 1;
836 /* some bulk transfers are started by the subdriver */
837 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
840 /* submit the URBs */
841 for (n = 0; n < MAX_NURBS; n++) {
842 urb = gspca_dev->urb[n];
845 ret = usb_submit_urb(urb, GFP_KERNEL);
851 gspca_stream_off(gspca_dev);
852 if (ret != -ENOSPC) {
853 err("usb_submit_urb alt %d err %d",
854 gspca_dev->alt, ret);
858 /* the bandwidth is not wide enough
859 * negociate or try a lower alternate setting */
860 PDEBUG(D_ERR|D_STREAM,
861 "bandwidth not wide enough - trying again");
862 msleep(20); /* wait for kill complete */
863 if (gspca_dev->sd_desc->isoc_nego) {
864 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
868 ep = get_ep(gspca_dev);
876 mutex_unlock(&gspca_dev->usb_lock);
880 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
882 struct gspca_ctrl *ctrl;
885 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
886 gspca_dev->curr_mode = i;
887 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
888 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
889 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
891 /* set the current control values to their default values
892 * which may have changed in sd_init() */
893 ctrl = gspca_dev->cam.ctrls;
896 i < gspca_dev->sd_desc->nctrls;
898 ctrl->val = ctrl->def;
902 static int wxh_to_mode(struct gspca_dev *gspca_dev,
903 int width, int height)
907 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
908 if (width >= gspca_dev->cam.cam_mode[i].width
909 && height >= gspca_dev->cam.cam_mode[i].height)
916 * search a mode with the right pixel format
918 static int gspca_get_mode(struct gspca_dev *gspca_dev,
924 modeU = modeD = mode;
925 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
927 if (gspca_dev->cam.cam_mode[modeD].pixelformat
931 if (++modeU < gspca_dev->cam.nmodes) {
932 if (gspca_dev->cam.cam_mode[modeU].pixelformat
940 #ifdef CONFIG_VIDEO_ADV_DEBUG
941 static int vidioc_g_register(struct file *file, void *priv,
942 struct v4l2_dbg_register *reg)
945 struct gspca_dev *gspca_dev = priv;
947 if (!gspca_dev->sd_desc->get_chip_ident)
950 if (!gspca_dev->sd_desc->get_register)
953 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
955 gspca_dev->usb_err = 0;
956 if (gspca_dev->present)
957 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
960 mutex_unlock(&gspca_dev->usb_lock);
965 static int vidioc_s_register(struct file *file, void *priv,
966 struct v4l2_dbg_register *reg)
969 struct gspca_dev *gspca_dev = priv;
971 if (!gspca_dev->sd_desc->get_chip_ident)
974 if (!gspca_dev->sd_desc->set_register)
977 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
979 gspca_dev->usb_err = 0;
980 if (gspca_dev->present)
981 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
984 mutex_unlock(&gspca_dev->usb_lock);
990 static int vidioc_g_chip_ident(struct file *file, void *priv,
991 struct v4l2_dbg_chip_ident *chip)
994 struct gspca_dev *gspca_dev = priv;
996 if (!gspca_dev->sd_desc->get_chip_ident)
999 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1000 return -ERESTARTSYS;
1001 gspca_dev->usb_err = 0;
1002 if (gspca_dev->present)
1003 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1006 mutex_unlock(&gspca_dev->usb_lock);
1011 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1012 struct v4l2_fmtdesc *fmtdesc)
1014 struct gspca_dev *gspca_dev = priv;
1018 /* give an index to each format */
1021 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1022 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1025 if (fmt_tb[j] == fmt_tb[index])
1030 if (fmtdesc->index == index)
1031 break; /* new format */
1033 if (index >= ARRAY_SIZE(fmt_tb))
1038 return -EINVAL; /* no more format */
1040 fmtdesc->pixelformat = fmt_tb[index];
1041 if (gspca_is_compressed(fmt_tb[index]))
1042 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1043 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1044 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1045 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1046 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1047 fmtdesc->description[4] = '\0';
1051 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1052 struct v4l2_format *fmt)
1054 struct gspca_dev *gspca_dev = priv;
1057 mode = gspca_dev->curr_mode;
1058 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1059 sizeof fmt->fmt.pix);
1063 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1064 struct v4l2_format *fmt)
1066 int w, h, mode, mode2;
1068 w = fmt->fmt.pix.width;
1069 h = fmt->fmt.pix.height;
1072 if (gspca_debug & D_CONF)
1073 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1075 /* search the closest mode for width and height */
1076 mode = wxh_to_mode(gspca_dev, w, h);
1078 /* OK if right palette */
1079 if (gspca_dev->cam.cam_mode[mode].pixelformat
1080 != fmt->fmt.pix.pixelformat) {
1082 /* else, search the closest mode with the same pixel format */
1083 mode2 = gspca_get_mode(gspca_dev, mode,
1084 fmt->fmt.pix.pixelformat);
1088 ; * no chance, return this mode */
1090 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1091 sizeof fmt->fmt.pix);
1092 return mode; /* used when s_fmt */
1095 static int vidioc_try_fmt_vid_cap(struct file *file,
1097 struct v4l2_format *fmt)
1099 struct gspca_dev *gspca_dev = priv;
1102 ret = try_fmt_vid_cap(gspca_dev, fmt);
1108 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1109 struct v4l2_format *fmt)
1111 struct gspca_dev *gspca_dev = priv;
1114 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1115 return -ERESTARTSYS;
1117 ret = try_fmt_vid_cap(gspca_dev, fmt);
1121 if (gspca_dev->nframes != 0
1122 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1127 if (ret == gspca_dev->curr_mode) {
1129 goto out; /* same mode */
1132 if (gspca_dev->streaming) {
1136 gspca_dev->width = fmt->fmt.pix.width;
1137 gspca_dev->height = fmt->fmt.pix.height;
1138 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1139 gspca_dev->curr_mode = ret;
1143 mutex_unlock(&gspca_dev->queue_lock);
1147 static int vidioc_enum_framesizes(struct file *file, void *priv,
1148 struct v4l2_frmsizeenum *fsize)
1150 struct gspca_dev *gspca_dev = priv;
1154 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1155 if (fsize->pixel_format !=
1156 gspca_dev->cam.cam_mode[i].pixelformat)
1159 if (fsize->index == index) {
1160 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1161 fsize->discrete.width =
1162 gspca_dev->cam.cam_mode[i].width;
1163 fsize->discrete.height =
1164 gspca_dev->cam.cam_mode[i].height;
1173 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1174 struct v4l2_frmivalenum *fival)
1176 struct gspca_dev *gspca_dev = priv;
1177 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1180 if (gspca_dev->cam.mode_framerates == NULL ||
1181 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1184 if (fival->pixel_format !=
1185 gspca_dev->cam.cam_mode[mode].pixelformat)
1188 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1189 if (fival->index == i) {
1190 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1191 fival->discrete.numerator = 1;
1192 fival->discrete.denominator =
1193 gspca_dev->cam.mode_framerates[mode].rates[i];
1201 static void gspca_release(struct video_device *vfd)
1203 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1205 PDEBUG(D_PROBE, "%s released",
1206 video_device_node_name(&gspca_dev->vdev));
1208 kfree(gspca_dev->usb_buf);
1212 static int dev_open(struct file *file)
1214 struct gspca_dev *gspca_dev;
1217 PDEBUG(D_STREAM, "[%s] open", current->comm);
1218 gspca_dev = (struct gspca_dev *) video_devdata(file);
1219 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1220 return -ERESTARTSYS;
1221 if (!gspca_dev->present) {
1226 if (gspca_dev->users > 4) { /* (arbitrary value) */
1231 /* protect the subdriver against rmmod */
1232 if (!try_module_get(gspca_dev->module)) {
1239 file->private_data = gspca_dev;
1241 /* activate the v4l2 debug */
1242 if (gspca_debug & D_V4L2)
1243 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1244 | V4L2_DEBUG_IOCTL_ARG;
1246 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1247 | V4L2_DEBUG_IOCTL_ARG);
1251 mutex_unlock(&gspca_dev->queue_lock);
1253 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1255 PDEBUG(D_STREAM, "open done");
1259 static int dev_close(struct file *file)
1261 struct gspca_dev *gspca_dev = file->private_data;
1263 PDEBUG(D_STREAM, "[%s] close", current->comm);
1264 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1265 return -ERESTARTSYS;
1268 /* if the file did the capture, free the streaming resources */
1269 if (gspca_dev->capt_file == file) {
1270 if (gspca_dev->streaming) {
1271 mutex_lock(&gspca_dev->usb_lock);
1272 gspca_dev->usb_err = 0;
1273 gspca_stream_off(gspca_dev);
1274 mutex_unlock(&gspca_dev->usb_lock);
1276 frame_free(gspca_dev);
1277 gspca_dev->capt_file = NULL;
1278 gspca_dev->memory = GSPCA_MEMORY_NO;
1280 file->private_data = NULL;
1281 module_put(gspca_dev->module);
1282 mutex_unlock(&gspca_dev->queue_lock);
1284 PDEBUG(D_STREAM, "close done");
1289 static int vidioc_querycap(struct file *file, void *priv,
1290 struct v4l2_capability *cap)
1292 struct gspca_dev *gspca_dev = priv;
1295 /* protect the access to the usb device */
1296 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1297 return -ERESTARTSYS;
1298 if (!gspca_dev->present) {
1302 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1303 if (gspca_dev->dev->product != NULL) {
1304 strncpy(cap->card, gspca_dev->dev->product,
1307 snprintf(cap->card, sizeof cap->card,
1308 "USB Camera (%04x:%04x)",
1309 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1310 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1312 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1313 cap->version = DRIVER_VERSION_NUMBER;
1314 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1315 | V4L2_CAP_STREAMING
1316 | V4L2_CAP_READWRITE;
1319 mutex_unlock(&gspca_dev->usb_lock);
1323 static int get_ctrl(struct gspca_dev *gspca_dev,
1326 const struct ctrl *ctrls;
1329 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1330 i < gspca_dev->sd_desc->nctrls;
1332 if (gspca_dev->ctrl_dis & (1 << i))
1334 if (id == ctrls->qctrl.id)
1340 static int vidioc_queryctrl(struct file *file, void *priv,
1341 struct v4l2_queryctrl *q_ctrl)
1343 struct gspca_dev *gspca_dev = priv;
1344 const struct ctrl *ctrls;
1345 struct gspca_ctrl *gspca_ctrl;
1350 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1351 id &= V4L2_CTRL_ID_MASK;
1354 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1355 if (gspca_dev->ctrl_dis & (1 << i))
1357 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1360 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1361 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
1366 idx = get_ctrl(gspca_dev, id);
1370 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1371 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1372 if (gspca_dev->cam.ctrls != NULL) {
1373 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1374 q_ctrl->default_value = gspca_ctrl->def;
1375 q_ctrl->minimum = gspca_ctrl->min;
1376 q_ctrl->maximum = gspca_ctrl->max;
1378 if (gspca_dev->ctrl_inac & (1 << idx))
1379 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1383 static int vidioc_s_ctrl(struct file *file, void *priv,
1384 struct v4l2_control *ctrl)
1386 struct gspca_dev *gspca_dev = priv;
1387 const struct ctrl *ctrls;
1388 struct gspca_ctrl *gspca_ctrl;
1391 idx = get_ctrl(gspca_dev, ctrl->id);
1394 if (gspca_dev->ctrl_inac & (1 << idx))
1396 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1397 if (gspca_dev->cam.ctrls != NULL) {
1398 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1399 if (ctrl->value < gspca_ctrl->min
1400 || ctrl->value > gspca_ctrl->max)
1404 if (ctrl->value < ctrls->qctrl.minimum
1405 || ctrl->value > ctrls->qctrl.maximum)
1408 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1409 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1410 return -ERESTARTSYS;
1411 if (!gspca_dev->present) {
1415 gspca_dev->usb_err = 0;
1416 if (ctrls->set != NULL) {
1417 ret = ctrls->set(gspca_dev, ctrl->value);
1420 if (gspca_ctrl != NULL) {
1421 gspca_ctrl->val = ctrl->value;
1422 if (ctrls->set_control != NULL
1423 && gspca_dev->streaming)
1424 ctrls->set_control(gspca_dev);
1426 ret = gspca_dev->usb_err;
1428 mutex_unlock(&gspca_dev->usb_lock);
1432 static int vidioc_g_ctrl(struct file *file, void *priv,
1433 struct v4l2_control *ctrl)
1435 struct gspca_dev *gspca_dev = priv;
1436 const struct ctrl *ctrls;
1439 idx = get_ctrl(gspca_dev, ctrl->id);
1442 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1444 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1445 return -ERESTARTSYS;
1446 if (!gspca_dev->present) {
1450 gspca_dev->usb_err = 0;
1451 if (ctrls->get != NULL) {
1452 ret = ctrls->get(gspca_dev, &ctrl->value);
1455 if (gspca_dev->cam.ctrls != NULL)
1456 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1459 mutex_unlock(&gspca_dev->usb_lock);
1463 static int vidioc_querymenu(struct file *file, void *priv,
1464 struct v4l2_querymenu *qmenu)
1466 struct gspca_dev *gspca_dev = priv;
1468 if (!gspca_dev->sd_desc->querymenu)
1470 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1473 static int vidioc_enum_input(struct file *file, void *priv,
1474 struct v4l2_input *input)
1476 struct gspca_dev *gspca_dev = priv;
1478 if (input->index != 0)
1480 input->type = V4L2_INPUT_TYPE_CAMERA;
1481 input->status = gspca_dev->cam.input_flags;
1482 strncpy(input->name, gspca_dev->sd_desc->name,
1483 sizeof input->name);
1487 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1493 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1500 static int vidioc_reqbufs(struct file *file, void *priv,
1501 struct v4l2_requestbuffers *rb)
1503 struct gspca_dev *gspca_dev = priv;
1504 int i, ret = 0, streaming;
1506 i = rb->memory; /* (avoid compilation warning) */
1508 case GSPCA_MEMORY_READ: /* (internal call) */
1509 case V4L2_MEMORY_MMAP:
1510 case V4L2_MEMORY_USERPTR:
1515 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1516 return -ERESTARTSYS;
1518 if (gspca_dev->memory != GSPCA_MEMORY_NO
1519 && gspca_dev->memory != rb->memory) {
1524 /* only one file may do the capture */
1525 if (gspca_dev->capt_file != NULL
1526 && gspca_dev->capt_file != file) {
1531 /* if allocated, the buffers must not be mapped */
1532 for (i = 0; i < gspca_dev->nframes; i++) {
1533 if (gspca_dev->frame[i].vma_use_count) {
1539 /* stop streaming */
1540 streaming = gspca_dev->streaming;
1542 mutex_lock(&gspca_dev->usb_lock);
1543 gspca_dev->usb_err = 0;
1544 gspca_stream_off(gspca_dev);
1545 mutex_unlock(&gspca_dev->usb_lock);
1548 /* free the previous allocated buffers, if any */
1549 if (gspca_dev->nframes != 0) {
1550 frame_free(gspca_dev);
1551 gspca_dev->capt_file = NULL;
1553 if (rb->count == 0) /* unrequest */
1555 gspca_dev->memory = rb->memory;
1556 ret = frame_alloc(gspca_dev, rb->count);
1558 rb->count = gspca_dev->nframes;
1559 gspca_dev->capt_file = file;
1561 ret = gspca_init_transfer(gspca_dev);
1564 mutex_unlock(&gspca_dev->queue_lock);
1565 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1569 static int vidioc_querybuf(struct file *file, void *priv,
1570 struct v4l2_buffer *v4l2_buf)
1572 struct gspca_dev *gspca_dev = priv;
1573 struct gspca_frame *frame;
1575 if (v4l2_buf->index < 0
1576 || v4l2_buf->index >= gspca_dev->nframes)
1579 frame = &gspca_dev->frame[v4l2_buf->index];
1580 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1584 static int vidioc_streamon(struct file *file, void *priv,
1585 enum v4l2_buf_type buf_type)
1587 struct gspca_dev *gspca_dev = priv;
1590 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1592 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1593 return -ERESTARTSYS;
1595 /* check the capture file */
1596 if (gspca_dev->capt_file != file) {
1601 if (gspca_dev->nframes == 0
1602 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1606 if (!gspca_dev->streaming) {
1607 ret = gspca_init_transfer(gspca_dev);
1612 if (gspca_debug & D_STREAM) {
1613 PDEBUG_MODE("stream on OK",
1621 mutex_unlock(&gspca_dev->queue_lock);
1625 static int vidioc_streamoff(struct file *file, void *priv,
1626 enum v4l2_buf_type buf_type)
1628 struct gspca_dev *gspca_dev = priv;
1631 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1633 if (!gspca_dev->streaming)
1635 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1636 return -ERESTARTSYS;
1638 /* check the capture file */
1639 if (gspca_dev->capt_file != file) {
1644 /* stop streaming */
1645 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1649 gspca_dev->usb_err = 0;
1650 gspca_stream_off(gspca_dev);
1651 mutex_unlock(&gspca_dev->usb_lock);
1653 /* empty the transfer queues */
1654 atomic_set(&gspca_dev->fr_q, 0);
1655 atomic_set(&gspca_dev->fr_i, 0);
1656 gspca_dev->fr_o = 0;
1659 mutex_unlock(&gspca_dev->queue_lock);
1663 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1664 struct v4l2_jpegcompression *jpegcomp)
1666 struct gspca_dev *gspca_dev = priv;
1669 if (!gspca_dev->sd_desc->get_jcomp)
1671 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1672 return -ERESTARTSYS;
1673 gspca_dev->usb_err = 0;
1674 if (gspca_dev->present)
1675 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1678 mutex_unlock(&gspca_dev->usb_lock);
1682 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1683 struct v4l2_jpegcompression *jpegcomp)
1685 struct gspca_dev *gspca_dev = priv;
1688 if (!gspca_dev->sd_desc->set_jcomp)
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->set_jcomp(gspca_dev, jpegcomp);
1697 mutex_unlock(&gspca_dev->usb_lock);
1701 static int vidioc_g_parm(struct file *filp, void *priv,
1702 struct v4l2_streamparm *parm)
1704 struct gspca_dev *gspca_dev = priv;
1706 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1708 if (gspca_dev->sd_desc->get_streamparm) {
1711 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1712 return -ERESTARTSYS;
1713 gspca_dev->usb_err = 0;
1714 if (gspca_dev->present)
1715 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1719 mutex_unlock(&gspca_dev->usb_lock);
1726 static int vidioc_s_parm(struct file *filp, void *priv,
1727 struct v4l2_streamparm *parm)
1729 struct gspca_dev *gspca_dev = priv;
1732 n = parm->parm.capture.readbuffers;
1733 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1734 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1736 gspca_dev->nbufread = n;
1738 if (gspca_dev->sd_desc->set_streamparm) {
1741 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1742 return -ERESTARTSYS;
1743 gspca_dev->usb_err = 0;
1744 if (gspca_dev->present)
1745 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1749 mutex_unlock(&gspca_dev->usb_lock);
1756 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1758 struct gspca_dev *gspca_dev = file->private_data;
1759 struct gspca_frame *frame;
1761 unsigned long addr, start, size;
1764 start = vma->vm_start;
1765 size = vma->vm_end - vma->vm_start;
1766 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1768 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1769 return -ERESTARTSYS;
1770 if (!gspca_dev->present) {
1774 if (gspca_dev->capt_file != file) {
1780 for (i = 0; i < gspca_dev->nframes; ++i) {
1781 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1782 PDEBUG(D_STREAM, "mmap bad memory type");
1785 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1787 frame = &gspca_dev->frame[i];
1791 if (frame == NULL) {
1792 PDEBUG(D_STREAM, "mmap no frame buffer found");
1796 if (size != frame->v4l2_buf.length) {
1797 PDEBUG(D_STREAM, "mmap bad size");
1803 * - VM_IO marks the area as being a mmaped region for I/O to a
1804 * device. It also prevents the region from being core dumped.
1806 vma->vm_flags |= VM_IO;
1808 addr = (unsigned long) frame->data;
1810 page = vmalloc_to_page((void *) addr);
1811 ret = vm_insert_page(vma, start, page);
1819 vma->vm_ops = &gspca_vm_ops;
1820 vma->vm_private_data = frame;
1824 mutex_unlock(&gspca_dev->queue_lock);
1829 * wait for a video frame
1831 * If a frame is ready, its index is returned.
1833 static int frame_wait(struct gspca_dev *gspca_dev,
1838 /* check if a frame is ready */
1839 i = gspca_dev->fr_o;
1840 if (i == atomic_read(&gspca_dev->fr_i)) {
1844 /* wait till a frame is ready */
1845 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1846 i != atomic_read(&gspca_dev->fr_i) ||
1847 !gspca_dev->streaming || !gspca_dev->present,
1848 msecs_to_jiffies(3000));
1851 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1855 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1857 if (gspca_dev->sd_desc->dq_callback) {
1858 mutex_lock(&gspca_dev->usb_lock);
1859 gspca_dev->usb_err = 0;
1860 if (gspca_dev->present)
1861 gspca_dev->sd_desc->dq_callback(gspca_dev);
1862 mutex_unlock(&gspca_dev->usb_lock);
1864 return gspca_dev->fr_queue[i];
1868 * dequeue a video buffer
1870 * If nonblock_ing is false, block until a buffer is available.
1872 static int vidioc_dqbuf(struct file *file, void *priv,
1873 struct v4l2_buffer *v4l2_buf)
1875 struct gspca_dev *gspca_dev = priv;
1876 struct gspca_frame *frame;
1879 PDEBUG(D_FRAM, "dqbuf");
1880 if (v4l2_buf->memory != gspca_dev->memory)
1883 if (!gspca_dev->present)
1886 /* if not streaming, be sure the application will not loop forever */
1887 if (!(file->f_flags & O_NONBLOCK)
1888 && !gspca_dev->streaming && gspca_dev->users == 1)
1891 /* only the capturing file may dequeue */
1892 if (gspca_dev->capt_file != file)
1895 /* only one dequeue / read at a time */
1896 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1897 return -ERESTARTSYS;
1899 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1902 i = ret; /* frame index */
1903 frame = &gspca_dev->frame[i];
1904 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1905 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1907 frame->v4l2_buf.bytesused)) {
1908 PDEBUG(D_ERR|D_STREAM,
1909 "dqbuf cp to user failed");
1914 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1915 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1916 PDEBUG(D_FRAM, "dqbuf %d", i);
1919 mutex_unlock(&gspca_dev->read_lock);
1924 * queue a video buffer
1926 * Attempting to queue a buffer that has already been
1927 * queued will return -EINVAL.
1929 static int vidioc_qbuf(struct file *file, void *priv,
1930 struct v4l2_buffer *v4l2_buf)
1932 struct gspca_dev *gspca_dev = priv;
1933 struct gspca_frame *frame;
1936 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1938 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1939 return -ERESTARTSYS;
1941 index = v4l2_buf->index;
1942 if ((unsigned) index >= gspca_dev->nframes) {
1944 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1948 if (v4l2_buf->memory != gspca_dev->memory) {
1949 PDEBUG(D_FRAM, "qbuf bad memory type");
1954 frame = &gspca_dev->frame[index];
1955 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1956 PDEBUG(D_FRAM, "qbuf bad state");
1961 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1963 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1964 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1965 frame->v4l2_buf.length = v4l2_buf->length;
1968 /* put the buffer in the 'queued' queue */
1969 i = atomic_read(&gspca_dev->fr_q);
1970 gspca_dev->fr_queue[i] = index;
1971 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1973 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1974 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1977 mutex_unlock(&gspca_dev->queue_lock);
1982 * allocate the resources for read()
1984 static int read_alloc(struct gspca_dev *gspca_dev,
1987 struct v4l2_buffer v4l2_buf;
1990 PDEBUG(D_STREAM, "read alloc");
1991 if (gspca_dev->nframes == 0) {
1992 struct v4l2_requestbuffers rb;
1994 memset(&rb, 0, sizeof rb);
1995 rb.count = gspca_dev->nbufread;
1996 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1997 rb.memory = GSPCA_MEMORY_READ;
1998 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2000 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2003 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2004 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2005 v4l2_buf.memory = GSPCA_MEMORY_READ;
2006 for (i = 0; i < gspca_dev->nbufread; i++) {
2008 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2010 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2014 gspca_dev->memory = GSPCA_MEMORY_READ;
2017 /* start streaming */
2018 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2020 PDEBUG(D_STREAM, "read streamon err %d", ret);
2024 static unsigned int dev_poll(struct file *file, poll_table *wait)
2026 struct gspca_dev *gspca_dev = file->private_data;
2029 PDEBUG(D_FRAM, "poll");
2031 poll_wait(file, &gspca_dev->wq, wait);
2033 /* if reqbufs is not done, the user would use read() */
2034 if (gspca_dev->nframes == 0) {
2035 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2036 return POLLERR; /* not the 1st time */
2037 ret = read_alloc(gspca_dev, file);
2042 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2045 /* check if an image has been received */
2046 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2047 ret = POLLIN | POLLRDNORM; /* yes */
2050 mutex_unlock(&gspca_dev->queue_lock);
2051 if (!gspca_dev->present)
2056 static ssize_t dev_read(struct file *file, char __user *data,
2057 size_t count, loff_t *ppos)
2059 struct gspca_dev *gspca_dev = file->private_data;
2060 struct gspca_frame *frame;
2061 struct v4l2_buffer v4l2_buf;
2062 struct timeval timestamp;
2065 PDEBUG(D_FRAM, "read (%zd)", count);
2066 if (!gspca_dev->present)
2068 switch (gspca_dev->memory) {
2069 case GSPCA_MEMORY_NO: /* first time */
2070 ret = read_alloc(gspca_dev, file);
2074 case GSPCA_MEMORY_READ:
2075 if (gspca_dev->capt_file == file)
2083 timestamp = ktime_to_timeval(ktime_get());
2087 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2088 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2089 v4l2_buf.memory = GSPCA_MEMORY_READ;
2090 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2092 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2096 /* if the process slept for more than 1 second,
2097 * get a newer frame */
2098 frame = &gspca_dev->frame[v4l2_buf.index];
2100 break; /* avoid infinite loop */
2101 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2103 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2105 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2110 /* copy the frame */
2111 if (count > frame->v4l2_buf.bytesused)
2112 count = frame->v4l2_buf.bytesused;
2113 ret = copy_to_user(data, frame->data, count);
2115 PDEBUG(D_ERR|D_STREAM,
2116 "read cp to user lack %d / %zd", ret, count);
2122 /* in each case, requeue the buffer */
2123 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2129 static struct v4l2_file_operations dev_fops = {
2130 .owner = THIS_MODULE,
2132 .release = dev_close,
2135 .unlocked_ioctl = video_ioctl2,
2139 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2140 .vidioc_querycap = vidioc_querycap,
2141 .vidioc_dqbuf = vidioc_dqbuf,
2142 .vidioc_qbuf = vidioc_qbuf,
2143 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2144 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2145 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2146 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2147 .vidioc_streamon = vidioc_streamon,
2148 .vidioc_queryctrl = vidioc_queryctrl,
2149 .vidioc_g_ctrl = vidioc_g_ctrl,
2150 .vidioc_s_ctrl = vidioc_s_ctrl,
2151 .vidioc_querymenu = vidioc_querymenu,
2152 .vidioc_enum_input = vidioc_enum_input,
2153 .vidioc_g_input = vidioc_g_input,
2154 .vidioc_s_input = vidioc_s_input,
2155 .vidioc_reqbufs = vidioc_reqbufs,
2156 .vidioc_querybuf = vidioc_querybuf,
2157 .vidioc_streamoff = vidioc_streamoff,
2158 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2159 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2160 .vidioc_g_parm = vidioc_g_parm,
2161 .vidioc_s_parm = vidioc_s_parm,
2162 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2163 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2164 #ifdef CONFIG_VIDEO_ADV_DEBUG
2165 .vidioc_g_register = vidioc_g_register,
2166 .vidioc_s_register = vidioc_s_register,
2168 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2171 static struct video_device gspca_template = {
2172 .name = "gspca main driver",
2174 .ioctl_ops = &dev_ioctl_ops,
2175 .release = gspca_release,
2178 /* initialize the controls */
2179 static void ctrls_init(struct gspca_dev *gspca_dev)
2181 struct gspca_ctrl *ctrl;
2184 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2185 i < gspca_dev->sd_desc->nctrls;
2187 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2188 ctrl->val = ctrl->def;
2189 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2190 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2195 * probe and create a new gspca device
2197 * This function must be called by the sub-driver when it is
2198 * called for probing a new device.
2200 int gspca_dev_probe2(struct usb_interface *intf,
2201 const struct usb_device_id *id,
2202 const struct sd_desc *sd_desc,
2204 struct module *module)
2206 struct gspca_dev *gspca_dev;
2207 struct usb_device *dev = interface_to_usbdev(intf);
2210 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2212 /* create the device */
2213 if (dev_size < sizeof *gspca_dev)
2214 dev_size = sizeof *gspca_dev;
2215 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2217 err("couldn't kzalloc gspca struct");
2220 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2221 if (!gspca_dev->usb_buf) {
2222 err("out of memory");
2226 gspca_dev->dev = dev;
2227 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2228 gspca_dev->nbalt = intf->num_altsetting;
2230 /* check if any audio device */
2231 if (dev->config->desc.bNumInterfaces != 1) {
2233 struct usb_interface *intf2;
2235 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2236 intf2 = dev->config->interface[i];
2238 && intf2->altsetting != NULL
2239 && intf2->altsetting->desc.bInterfaceClass ==
2241 gspca_dev->audio = 1;
2247 gspca_dev->sd_desc = sd_desc;
2248 gspca_dev->nbufread = 2;
2249 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2251 /* configure the subdriver and initialize the USB device */
2252 ret = sd_desc->config(gspca_dev, id);
2255 if (gspca_dev->cam.ctrls != NULL)
2256 ctrls_init(gspca_dev);
2257 ret = sd_desc->init(gspca_dev);
2260 gspca_set_default_mode(gspca_dev);
2262 ret = gspca_input_connect(gspca_dev);
2266 mutex_init(&gspca_dev->usb_lock);
2267 mutex_init(&gspca_dev->read_lock);
2268 mutex_init(&gspca_dev->queue_lock);
2269 init_waitqueue_head(&gspca_dev->wq);
2271 /* init video stuff */
2272 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2273 gspca_dev->vdev.parent = &intf->dev;
2274 gspca_dev->module = module;
2275 gspca_dev->present = 1;
2276 ret = video_register_device(&gspca_dev->vdev,
2280 err("video_register_device err %d", ret);
2284 usb_set_intfdata(intf, gspca_dev);
2285 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2287 gspca_input_create_urb(gspca_dev);
2291 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2292 if (gspca_dev->input_dev)
2293 input_unregister_device(gspca_dev->input_dev);
2295 kfree(gspca_dev->usb_buf);
2299 EXPORT_SYMBOL(gspca_dev_probe2);
2301 /* same function as the previous one, but check the interface */
2302 int gspca_dev_probe(struct usb_interface *intf,
2303 const struct usb_device_id *id,
2304 const struct sd_desc *sd_desc,
2306 struct module *module)
2308 struct usb_device *dev = interface_to_usbdev(intf);
2310 /* we don't handle multi-config cameras */
2311 if (dev->descriptor.bNumConfigurations != 1) {
2312 err("%04x:%04x too many config",
2313 id->idVendor, id->idProduct);
2317 /* the USB video interface must be the first one */
2318 if (dev->config->desc.bNumInterfaces != 1
2319 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2322 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2324 EXPORT_SYMBOL(gspca_dev_probe);
2329 * This function must be called by the sub-driver
2330 * when the device disconnects, after the specific resources are freed.
2332 void gspca_disconnect(struct usb_interface *intf)
2334 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2335 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2336 struct input_dev *input_dev;
2339 PDEBUG(D_PROBE, "%s disconnect",
2340 video_device_node_name(&gspca_dev->vdev));
2341 mutex_lock(&gspca_dev->usb_lock);
2342 gspca_dev->present = 0;
2344 if (gspca_dev->streaming) {
2345 destroy_urbs(gspca_dev);
2346 wake_up_interruptible(&gspca_dev->wq);
2349 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2350 gspca_input_destroy_urb(gspca_dev);
2351 input_dev = gspca_dev->input_dev;
2353 gspca_dev->input_dev = NULL;
2354 input_unregister_device(input_dev);
2358 /* the device is freed at exit of this function */
2359 gspca_dev->dev = NULL;
2360 mutex_unlock(&gspca_dev->usb_lock);
2362 usb_set_intfdata(intf, NULL);
2364 /* release the device */
2365 /* (this will call gspca_release() immediatly or on last close) */
2366 video_unregister_device(&gspca_dev->vdev);
2368 /* PDEBUG(D_PROBE, "disconnect complete"); */
2370 EXPORT_SYMBOL(gspca_disconnect);
2373 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2375 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2377 if (!gspca_dev->streaming)
2379 gspca_dev->frozen = 1; /* avoid urb error messages */
2380 if (gspca_dev->sd_desc->stopN)
2381 gspca_dev->sd_desc->stopN(gspca_dev);
2382 destroy_urbs(gspca_dev);
2383 gspca_input_destroy_urb(gspca_dev);
2384 gspca_set_alt0(gspca_dev);
2385 if (gspca_dev->sd_desc->stop0)
2386 gspca_dev->sd_desc->stop0(gspca_dev);
2389 EXPORT_SYMBOL(gspca_suspend);
2391 int gspca_resume(struct usb_interface *intf)
2393 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2395 gspca_dev->frozen = 0;
2396 gspca_dev->sd_desc->init(gspca_dev);
2397 gspca_input_create_urb(gspca_dev);
2398 if (gspca_dev->streaming)
2399 return gspca_init_transfer(gspca_dev);
2402 EXPORT_SYMBOL(gspca_resume);
2404 /* -- cam driver utility functions -- */
2406 /* auto gain and exposure algorithm based on the knee algorithm described here:
2407 http://ytse.tricolour.net/docs/LowLightOptimization.html
2409 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2411 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2412 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2414 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2415 const struct ctrl *gain_ctrl = NULL;
2416 const struct ctrl *exposure_ctrl = NULL;
2417 const struct ctrl *autogain_ctrl = NULL;
2420 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2421 if (gspca_dev->ctrl_dis & (1 << i))
2423 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2424 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2425 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2426 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2427 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2428 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2430 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2431 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2432 "on cam without (auto)gain/exposure");
2436 if (gain_ctrl->get(gspca_dev, &gain) ||
2437 exposure_ctrl->get(gspca_dev, &exposure) ||
2438 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2442 orig_exposure = exposure;
2444 /* If we are of a multiple of deadzone, do multiple steps to reach the
2445 desired lumination fast (with the risc of a slight overshoot) */
2446 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2448 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2449 avg_lum, desired_avg_lum, steps);
2451 for (i = 0; i < steps; i++) {
2452 if (avg_lum > desired_avg_lum) {
2453 if (gain > gain_knee)
2455 else if (exposure > exposure_knee)
2457 else if (gain > gain_ctrl->qctrl.default_value)
2459 else if (exposure > exposure_ctrl->qctrl.minimum)
2461 else if (gain > gain_ctrl->qctrl.minimum)
2466 if (gain < gain_ctrl->qctrl.default_value)
2468 else if (exposure < exposure_knee)
2470 else if (gain < gain_knee)
2472 else if (exposure < exposure_ctrl->qctrl.maximum)
2474 else if (gain < gain_ctrl->qctrl.maximum)
2481 if (gain != orig_gain) {
2482 gain_ctrl->set(gspca_dev, gain);
2485 if (exposure != orig_exposure) {
2486 exposure_ctrl->set(gspca_dev, exposure);
2492 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2494 /* -- module insert / remove -- */
2495 static int __init gspca_init(void)
2497 info("v%d.%d.%d registered",
2498 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2499 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2500 DRIVER_VERSION_NUMBER & 0xff);
2503 static void __exit gspca_exit(void)
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"