2 * Main USB camera driver
4 * Copyright (C) 2008-2011 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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define GSPCA_VERSION "2.14.0"
28 #include <linux/init.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
38 #include <linux/uaccess.h>
39 #include <linux/ktime.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-fh.h>
43 #include <media/v4l2-event.h>
47 #if IS_ENABLED(CONFIG_INPUT)
48 #include <linux/input.h>
49 #include <linux/usb/input.h>
53 #define DEF_NURBS 3 /* default number of URBs */
54 #if DEF_NURBS > MAX_NURBS
55 #error "DEF_NURBS too big"
58 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
59 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(GSPCA_VERSION);
64 EXPORT_SYMBOL(gspca_debug);
66 static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
67 __u32 pixfmt, int w, int h)
69 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
70 PDEBUG(debug, "%s %c%c%c%c %dx%d",
74 (pixfmt >> 16) & 0xff,
78 PDEBUG(debug, "%s 0x%08x %dx%d",
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 IS_ENABLED(CONFIG_INPUT)
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 PERR("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 PERR("URB error %i, resubmitting", urb->status);
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
151 pr_err("Resubmit URB failed with error %i\n", 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 pr_err("Input device registration failed with error %i\n",
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 ret = usb_submit_urb(urb, GFP_KERNEL);
229 PERR("submit int URB failed with error %i", ret);
232 gspca_dev->int_urb = urb;
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 PERR("urb status: %d", urb->status);
319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
321 len = urb->iso_frame_desc[i].actual_length;
323 /* check the packet status and length */
324 st = urb->iso_frame_desc[i].status;
326 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
328 gspca_dev->last_packet_type = DISCARD_PACKET;
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
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 pr_err("usb_submit_urb() ret %d\n", 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 PERR("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 pr_err("usb_submit_urb() ret %d\n", 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.
418 void gspca_frame_add(struct gspca_dev *gspca_dev,
419 enum gspca_packet_type packet_type,
423 struct gspca_frame *frame;
426 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
428 if (packet_type == FIRST_PACKET) {
429 i = atomic_read(&gspca_dev->fr_i);
431 /* if there are no queued buffer, discard the whole frame */
432 if (i == atomic_read(&gspca_dev->fr_q)) {
433 gspca_dev->last_packet_type = DISCARD_PACKET;
434 gspca_dev->sequence++;
437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
439 v4l2_get_timestamp(&frame->v4l2_buf.timestamp);
440 frame->v4l2_buf.sequence = gspca_dev->sequence++;
441 gspca_dev->image = frame->data;
442 gspca_dev->image_len = 0;
444 switch (gspca_dev->last_packet_type) {
446 if (packet_type == LAST_PACKET) {
447 gspca_dev->last_packet_type = packet_type;
448 gspca_dev->image = NULL;
449 gspca_dev->image_len = 0;
457 /* append the packet to the frame buffer */
459 if (gspca_dev->image_len + len > gspca_dev->frsz) {
460 PERR("frame overflow %d > %d",
461 gspca_dev->image_len + len,
463 packet_type = DISCARD_PACKET;
465 /* !! image is NULL only when last pkt is LAST or DISCARD
466 if (gspca_dev->image == NULL) {
467 pr_err("gspca_frame_add() image == NULL\n");
471 memcpy(gspca_dev->image + gspca_dev->image_len,
473 gspca_dev->image_len += len;
476 gspca_dev->last_packet_type = packet_type;
478 /* if last packet, invalidate packet concatenation until
479 * next first packet, wake up the application and advance
481 if (packet_type == LAST_PACKET) {
482 i = atomic_read(&gspca_dev->fr_i);
483 j = gspca_dev->fr_queue[i];
484 frame = &gspca_dev->frame[j];
485 frame->v4l2_buf.bytesused = gspca_dev->image_len;
486 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
487 | V4L2_BUF_FLAG_DONE)
488 & ~V4L2_BUF_FLAG_QUEUED;
489 i = (i + 1) % GSPCA_MAX_FRAMES;
490 atomic_set(&gspca_dev->fr_i, i);
491 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
492 PDEBUG(D_FRAM, "frame complete len:%d",
493 frame->v4l2_buf.bytesused);
494 gspca_dev->image = NULL;
495 gspca_dev->image_len = 0;
498 EXPORT_SYMBOL(gspca_frame_add);
500 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
501 enum v4l2_memory memory, unsigned int count)
503 struct gspca_frame *frame;
507 frsz = gspca_dev->pixfmt.sizeimage;
508 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
509 frsz = PAGE_ALIGN(frsz);
510 if (count >= GSPCA_MAX_FRAMES)
511 count = GSPCA_MAX_FRAMES - 1;
512 gspca_dev->frbuf = vmalloc_32(frsz * count);
513 if (!gspca_dev->frbuf) {
514 pr_err("frame alloc failed\n");
517 gspca_dev->capt_file = file;
518 gspca_dev->memory = memory;
519 gspca_dev->frsz = frsz;
520 gspca_dev->nframes = count;
521 for (i = 0; i < count; i++) {
522 frame = &gspca_dev->frame[i];
523 frame->v4l2_buf.index = i;
524 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
525 frame->v4l2_buf.flags = 0;
526 frame->v4l2_buf.field = V4L2_FIELD_NONE;
527 frame->v4l2_buf.length = frsz;
528 frame->v4l2_buf.memory = memory;
529 frame->v4l2_buf.sequence = 0;
530 frame->data = gspca_dev->frbuf + i * frsz;
531 frame->v4l2_buf.m.offset = i * frsz;
533 atomic_set(&gspca_dev->fr_q, 0);
534 atomic_set(&gspca_dev->fr_i, 0);
539 static void frame_free(struct gspca_dev *gspca_dev)
543 PDEBUG(D_STREAM, "frame free");
544 if (gspca_dev->frbuf != NULL) {
545 vfree(gspca_dev->frbuf);
546 gspca_dev->frbuf = NULL;
547 for (i = 0; i < gspca_dev->nframes; i++)
548 gspca_dev->frame[i].data = NULL;
550 gspca_dev->nframes = 0;
552 gspca_dev->capt_file = NULL;
553 gspca_dev->memory = GSPCA_MEMORY_NO;
556 static void destroy_urbs(struct gspca_dev *gspca_dev)
561 PDEBUG(D_STREAM, "kill transfer");
562 for (i = 0; i < MAX_NURBS; i++) {
563 urb = gspca_dev->urb[i];
567 gspca_dev->urb[i] = NULL;
569 usb_free_coherent(gspca_dev->dev,
570 urb->transfer_buffer_length,
571 urb->transfer_buffer,
577 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
581 if (gspca_dev->alt == 0)
583 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
585 pr_err("set alt 0 err %d\n", ret);
589 /* Note: both the queue and the usb locks should be held when calling this */
590 static void gspca_stream_off(struct gspca_dev *gspca_dev)
592 gspca_dev->streaming = 0;
593 gspca_dev->usb_err = 0;
594 if (gspca_dev->sd_desc->stopN)
595 gspca_dev->sd_desc->stopN(gspca_dev);
596 destroy_urbs(gspca_dev);
597 gspca_input_destroy_urb(gspca_dev);
598 gspca_set_alt0(gspca_dev);
599 gspca_input_create_urb(gspca_dev);
600 if (gspca_dev->sd_desc->stop0)
601 gspca_dev->sd_desc->stop0(gspca_dev);
602 PDEBUG(D_STREAM, "stream off OK");
606 * look for an input transfer endpoint in an alternate setting.
608 * If xfer_ep is invalid, return the first valid ep found, otherwise
609 * look for exactly the ep with address equal to xfer_ep.
611 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
612 int xfer, int xfer_ep)
614 struct usb_host_endpoint *ep;
617 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
618 ep = &alt->endpoint[i];
619 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
621 && ep->desc.wMaxPacketSize != 0
622 && usb_endpoint_dir_in(&ep->desc)
623 && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
629 /* compute the minimum bandwidth for the current transfer */
630 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
634 /* get the (max) image size */
635 bandwidth = gspca_dev->pixfmt.sizeimage;
637 /* if the image is compressed, estimate its mean size */
638 if (!gspca_dev->cam.needs_full_bandwidth &&
639 bandwidth < gspca_dev->pixfmt.width *
640 gspca_dev->pixfmt.height)
641 bandwidth = bandwidth * 3 / 8; /* 0.375 */
643 /* estimate the frame rate */
644 if (gspca_dev->sd_desc->get_streamparm) {
645 struct v4l2_streamparm parm;
647 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
648 bandwidth *= parm.parm.capture.timeperframe.denominator;
649 bandwidth /= parm.parm.capture.timeperframe.numerator;
652 /* don't hope more than 15 fps with USB 1.1 and
653 * image resolution >= 640x480 */
654 if (gspca_dev->pixfmt.width >= 640
655 && gspca_dev->dev->speed == USB_SPEED_FULL)
656 bandwidth *= 15; /* 15 fps */
658 bandwidth *= 30; /* 30 fps */
661 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
673 * build the table of the endpoints
674 * and compute the minimum bandwidth for the image transfer
676 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
677 struct usb_interface *intf,
678 struct ep_tb_s *ep_tb)
680 struct usb_host_endpoint *ep;
681 int i, j, nbalt, psize, found;
682 u32 bandwidth, last_bw;
684 nbalt = intf->num_altsetting;
686 nbalt = MAX_ALT; /* fixme: should warn */
688 /* build the endpoint table */
692 ep_tb->bandwidth = 2000 * 2000 * 120;
694 for (j = 0; j < nbalt; j++) {
695 ep = alt_xfer(&intf->altsetting[j],
696 USB_ENDPOINT_XFER_ISOC,
700 if (ep->desc.bInterval == 0) {
701 pr_err("alt %d iso endp with 0 interval\n", j);
704 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
705 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
706 bandwidth = psize * 1000;
707 if (gspca_dev->dev->speed == USB_SPEED_HIGH
708 || gspca_dev->dev->speed == USB_SPEED_SUPER)
710 bandwidth /= 1 << (ep->desc.bInterval - 1);
711 if (bandwidth <= last_bw)
713 if (bandwidth < ep_tb->bandwidth) {
714 ep_tb->bandwidth = bandwidth;
721 PDEBUG(D_STREAM, "alt %d bandwidth %d",
722 ep_tb->alt, ep_tb->bandwidth);
723 last_bw = ep_tb->bandwidth;
730 * has a usb audio class interface (a built in usb mic); and
731 * is a usb 1 full speed device; and
732 * uses the max full speed iso bandwidth; and
733 * and has more than 1 alt setting
734 * then skip the highest alt setting to spare bandwidth for the mic
736 if (gspca_dev->audio &&
737 gspca_dev->dev->speed == USB_SPEED_FULL &&
738 last_bw >= 1000000 &&
740 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
745 /* get the requested bandwidth and start at the highest atlsetting */
746 bandwidth = which_bandwidth(gspca_dev);
750 if (ep_tb->bandwidth < bandwidth)
758 * create the URBs for image transfer
760 static int create_urbs(struct gspca_dev *gspca_dev,
761 struct usb_host_endpoint *ep)
764 int n, nurbs, i, psize, npkt, bsize;
766 /* calculate the packet size and the number of packets */
767 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
769 if (!gspca_dev->cam.bulk) { /* isoc */
771 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
772 if (gspca_dev->pkt_size == 0)
773 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
775 psize = gspca_dev->pkt_size;
776 npkt = gspca_dev->cam.npkt;
778 npkt = 32; /* default value */
779 bsize = psize * npkt;
781 "isoc %d pkts size %d = bsize:%d",
786 bsize = gspca_dev->cam.bulk_size;
789 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
790 if (gspca_dev->cam.bulk_nurbs != 0)
791 nurbs = gspca_dev->cam.bulk_nurbs;
796 for (n = 0; n < nurbs; n++) {
797 urb = usb_alloc_urb(npkt, GFP_KERNEL);
799 pr_err("usb_alloc_urb failed\n");
802 gspca_dev->urb[n] = urb;
803 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
808 if (urb->transfer_buffer == NULL) {
809 pr_err("usb_alloc_coherent failed\n");
812 urb->dev = gspca_dev->dev;
813 urb->context = gspca_dev;
814 urb->transfer_buffer_length = bsize;
815 if (npkt != 0) { /* ISOC */
816 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
817 ep->desc.bEndpointAddress);
818 urb->transfer_flags = URB_ISO_ASAP
819 | URB_NO_TRANSFER_DMA_MAP;
820 urb->interval = 1 << (ep->desc.bInterval - 1);
821 urb->complete = isoc_irq;
822 urb->number_of_packets = npkt;
823 for (i = 0; i < npkt; i++) {
824 urb->iso_frame_desc[i].length = psize;
825 urb->iso_frame_desc[i].offset = psize * i;
828 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
829 ep->desc.bEndpointAddress);
830 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
831 urb->complete = bulk_irq;
838 * start the USB transfer
840 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
842 struct usb_interface *intf;
843 struct usb_host_endpoint *ep;
845 struct ep_tb_s ep_tb[MAX_ALT];
846 int n, ret, xfer, alt, alt_idx;
848 /* reset the streaming variables */
849 gspca_dev->image = NULL;
850 gspca_dev->image_len = 0;
851 gspca_dev->last_packet_type = DISCARD_PACKET;
852 gspca_dev->sequence = 0;
854 gspca_dev->usb_err = 0;
856 /* do the specific subdriver stuff before endpoint selection */
857 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
858 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
859 if (gspca_dev->sd_desc->isoc_init) {
860 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
864 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
865 : USB_ENDPOINT_XFER_ISOC;
867 /* if bulk or the subdriver forced an altsetting, get the endpoint */
868 if (gspca_dev->alt != 0) {
869 gspca_dev->alt--; /* (previous version compatibility) */
870 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
873 pr_err("bad altsetting %d\n", gspca_dev->alt);
876 ep_tb[0].alt = gspca_dev->alt;
879 /* else, compute the minimum bandwidth
880 * and build the endpoint table */
881 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
883 pr_err("no transfer endpoint found\n");
888 /* set the highest alternate setting and
889 * loop until urb submit succeeds */
890 gspca_input_destroy_urb(gspca_dev);
892 gspca_dev->alt = ep_tb[--alt_idx].alt;
895 if (alt != gspca_dev->alt) {
896 alt = gspca_dev->alt;
897 if (intf->num_altsetting > 1) {
898 ret = usb_set_interface(gspca_dev->dev,
903 goto retry; /*fixme: ugly*/
904 pr_err("set alt %d err %d\n", alt, ret);
909 if (!gspca_dev->cam.no_urb_create) {
910 PDEBUG(D_STREAM, "init transfer alt %d", alt);
911 ret = create_urbs(gspca_dev,
912 alt_xfer(&intf->altsetting[alt], xfer,
913 gspca_dev->xfer_ep));
915 destroy_urbs(gspca_dev);
920 /* clear the bulk endpoint */
921 if (gspca_dev->cam.bulk)
922 usb_clear_halt(gspca_dev->dev,
923 gspca_dev->urb[0]->pipe);
926 ret = gspca_dev->sd_desc->start(gspca_dev);
928 destroy_urbs(gspca_dev);
931 gspca_dev->streaming = 1;
932 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
934 /* some bulk transfers are started by the subdriver */
935 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
938 /* submit the URBs */
939 for (n = 0; n < MAX_NURBS; n++) {
940 urb = gspca_dev->urb[n];
943 ret = usb_submit_urb(urb, GFP_KERNEL);
948 break; /* transfer is started */
950 /* something when wrong
951 * stop the webcam and free the transfer resources */
952 gspca_stream_off(gspca_dev);
953 if (ret != -ENOSPC) {
954 pr_err("usb_submit_urb alt %d err %d\n",
955 gspca_dev->alt, ret);
959 /* the bandwidth is not wide enough
960 * negotiate or try a lower alternate setting */
962 PERR("alt %d - bandwidth not wide enough, trying again", alt);
963 msleep(20); /* wait for kill complete */
964 if (gspca_dev->sd_desc->isoc_nego) {
965 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
970 pr_err("no transfer endpoint found\n");
974 gspca_dev->alt = ep_tb[--alt_idx].alt;
978 gspca_input_create_urb(gspca_dev);
982 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
986 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
987 gspca_dev->curr_mode = i;
988 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
990 /* does nothing if ctrl_handler == NULL */
991 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
994 static int wxh_to_mode(struct gspca_dev *gspca_dev,
995 int width, int height)
999 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
1000 if (width >= gspca_dev->cam.cam_mode[i].width
1001 && height >= gspca_dev->cam.cam_mode[i].height)
1008 * search a mode with the right pixel format
1010 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1016 modeU = modeD = mode;
1017 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1019 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1023 if (++modeU < gspca_dev->cam.nmodes) {
1024 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1032 #ifdef CONFIG_VIDEO_ADV_DEBUG
1033 static int vidioc_g_chip_info(struct file *file, void *priv,
1034 struct v4l2_dbg_chip_info *chip)
1036 struct gspca_dev *gspca_dev = video_drvdata(file);
1038 gspca_dev->usb_err = 0;
1039 if (gspca_dev->sd_desc->get_chip_info)
1040 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
1041 return chip->match.addr ? -EINVAL : 0;
1044 static int vidioc_g_register(struct file *file, void *priv,
1045 struct v4l2_dbg_register *reg)
1047 struct gspca_dev *gspca_dev = video_drvdata(file);
1049 gspca_dev->usb_err = 0;
1050 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
1053 static int vidioc_s_register(struct file *file, void *priv,
1054 const struct v4l2_dbg_register *reg)
1056 struct gspca_dev *gspca_dev = video_drvdata(file);
1058 gspca_dev->usb_err = 0;
1059 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
1063 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1064 struct v4l2_fmtdesc *fmtdesc)
1066 struct gspca_dev *gspca_dev = video_drvdata(file);
1070 /* give an index to each format */
1073 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1074 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1077 if (fmt_tb[j] == fmt_tb[index])
1082 if (fmtdesc->index == index)
1083 break; /* new format */
1085 if (index >= ARRAY_SIZE(fmt_tb))
1090 return -EINVAL; /* no more format */
1092 fmtdesc->pixelformat = fmt_tb[index];
1093 if (gspca_dev->cam.cam_mode[i].sizeimage <
1094 gspca_dev->cam.cam_mode[i].width *
1095 gspca_dev->cam.cam_mode[i].height)
1096 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1097 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1098 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1099 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1100 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1101 fmtdesc->description[4] = '\0';
1105 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1106 struct v4l2_format *fmt)
1108 struct gspca_dev *gspca_dev = video_drvdata(file);
1110 fmt->fmt.pix = gspca_dev->pixfmt;
1111 /* some drivers use priv internally, zero it before giving it back to
1113 fmt->fmt.pix.priv = 0;
1117 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1118 struct v4l2_format *fmt)
1120 int w, h, mode, mode2;
1122 w = fmt->fmt.pix.width;
1123 h = fmt->fmt.pix.height;
1125 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1126 fmt->fmt.pix.pixelformat, w, h);
1128 /* search the closest mode for width and height */
1129 mode = wxh_to_mode(gspca_dev, w, h);
1131 /* OK if right palette */
1132 if (gspca_dev->cam.cam_mode[mode].pixelformat
1133 != fmt->fmt.pix.pixelformat) {
1135 /* else, search the closest mode with the same pixel format */
1136 mode2 = gspca_get_mode(gspca_dev, mode,
1137 fmt->fmt.pix.pixelformat);
1141 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1142 if (gspca_dev->sd_desc->try_fmt) {
1143 /* pass original resolution to subdriver try_fmt */
1144 fmt->fmt.pix.width = w;
1145 fmt->fmt.pix.height = h;
1146 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
1148 /* some drivers use priv internally, zero it before giving it back to
1150 fmt->fmt.pix.priv = 0;
1151 return mode; /* used when s_fmt */
1154 static int vidioc_try_fmt_vid_cap(struct file *file,
1156 struct v4l2_format *fmt)
1158 struct gspca_dev *gspca_dev = video_drvdata(file);
1161 ret = try_fmt_vid_cap(gspca_dev, fmt);
1167 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1168 struct v4l2_format *fmt)
1170 struct gspca_dev *gspca_dev = video_drvdata(file);
1173 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1174 return -ERESTARTSYS;
1176 ret = try_fmt_vid_cap(gspca_dev, fmt);
1180 if (gspca_dev->nframes != 0
1181 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1186 if (gspca_dev->streaming) {
1190 gspca_dev->curr_mode = ret;
1191 if (gspca_dev->sd_desc->try_fmt)
1192 /* subdriver try_fmt can modify format parameters */
1193 gspca_dev->pixfmt = fmt->fmt.pix;
1195 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret];
1199 mutex_unlock(&gspca_dev->queue_lock);
1203 static int vidioc_enum_framesizes(struct file *file, void *priv,
1204 struct v4l2_frmsizeenum *fsize)
1206 struct gspca_dev *gspca_dev = video_drvdata(file);
1210 if (gspca_dev->sd_desc->enum_framesizes)
1211 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
1213 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1214 if (fsize->pixel_format !=
1215 gspca_dev->cam.cam_mode[i].pixelformat)
1218 if (fsize->index == index) {
1219 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1220 fsize->discrete.width =
1221 gspca_dev->cam.cam_mode[i].width;
1222 fsize->discrete.height =
1223 gspca_dev->cam.cam_mode[i].height;
1232 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1233 struct v4l2_frmivalenum *fival)
1235 struct gspca_dev *gspca_dev = video_drvdata(filp);
1236 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1239 if (gspca_dev->cam.mode_framerates == NULL ||
1240 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1243 if (fival->pixel_format !=
1244 gspca_dev->cam.cam_mode[mode].pixelformat)
1247 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1248 if (fival->index == i) {
1249 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1250 fival->discrete.numerator = 1;
1251 fival->discrete.denominator =
1252 gspca_dev->cam.mode_framerates[mode].rates[i];
1260 static void gspca_release(struct v4l2_device *v4l2_device)
1262 struct gspca_dev *gspca_dev =
1263 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1265 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1266 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1267 kfree(gspca_dev->usb_buf);
1271 static int dev_open(struct file *file)
1273 struct gspca_dev *gspca_dev = video_drvdata(file);
1276 PDEBUG(D_STREAM, "[%s] open", current->comm);
1278 /* protect the subdriver against rmmod */
1279 if (!try_module_get(gspca_dev->module))
1282 ret = v4l2_fh_open(file);
1284 module_put(gspca_dev->module);
1288 static int dev_close(struct file *file)
1290 struct gspca_dev *gspca_dev = video_drvdata(file);
1292 PDEBUG(D_STREAM, "[%s] close", current->comm);
1294 /* Needed for gspca_stream_off, always lock before queue_lock! */
1295 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1296 return -ERESTARTSYS;
1298 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
1299 mutex_unlock(&gspca_dev->usb_lock);
1300 return -ERESTARTSYS;
1303 /* if the file did the capture, free the streaming resources */
1304 if (gspca_dev->capt_file == file) {
1305 if (gspca_dev->streaming)
1306 gspca_stream_off(gspca_dev);
1307 frame_free(gspca_dev);
1309 module_put(gspca_dev->module);
1310 mutex_unlock(&gspca_dev->queue_lock);
1311 mutex_unlock(&gspca_dev->usb_lock);
1313 PDEBUG(D_STREAM, "close done");
1315 return v4l2_fh_release(file);
1318 static int vidioc_querycap(struct file *file, void *priv,
1319 struct v4l2_capability *cap)
1321 struct gspca_dev *gspca_dev = video_drvdata(file);
1323 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1324 sizeof cap->driver);
1325 if (gspca_dev->dev->product != NULL) {
1326 strlcpy((char *) cap->card, gspca_dev->dev->product,
1329 snprintf((char *) cap->card, sizeof cap->card,
1330 "USB Camera (%04x:%04x)",
1331 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1332 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1334 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1335 sizeof(cap->bus_info));
1336 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
1337 | V4L2_CAP_STREAMING
1338 | V4L2_CAP_READWRITE;
1339 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1343 static int vidioc_enum_input(struct file *file, void *priv,
1344 struct v4l2_input *input)
1346 struct gspca_dev *gspca_dev = video_drvdata(file);
1348 if (input->index != 0)
1350 input->type = V4L2_INPUT_TYPE_CAMERA;
1351 input->status = gspca_dev->cam.input_flags;
1352 strlcpy(input->name, gspca_dev->sd_desc->name,
1353 sizeof input->name);
1357 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1363 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1370 static int vidioc_reqbufs(struct file *file, void *priv,
1371 struct v4l2_requestbuffers *rb)
1373 struct gspca_dev *gspca_dev = video_drvdata(file);
1374 int i, ret = 0, streaming;
1376 i = rb->memory; /* (avoid compilation warning) */
1378 case GSPCA_MEMORY_READ: /* (internal call) */
1379 case V4L2_MEMORY_MMAP:
1380 case V4L2_MEMORY_USERPTR:
1385 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1386 return -ERESTARTSYS;
1388 if (gspca_dev->memory != GSPCA_MEMORY_NO
1389 && gspca_dev->memory != GSPCA_MEMORY_READ
1390 && gspca_dev->memory != rb->memory) {
1395 /* only one file may do the capture */
1396 if (gspca_dev->capt_file != NULL
1397 && gspca_dev->capt_file != file) {
1402 /* if allocated, the buffers must not be mapped */
1403 for (i = 0; i < gspca_dev->nframes; i++) {
1404 if (gspca_dev->frame[i].vma_use_count) {
1410 /* stop streaming */
1411 streaming = gspca_dev->streaming;
1413 gspca_stream_off(gspca_dev);
1415 /* Don't restart the stream when switching from read
1417 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1421 /* free the previous allocated buffers, if any */
1422 if (gspca_dev->nframes != 0)
1423 frame_free(gspca_dev);
1424 if (rb->count == 0) /* unrequest */
1426 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1428 rb->count = gspca_dev->nframes;
1430 ret = gspca_init_transfer(gspca_dev);
1433 mutex_unlock(&gspca_dev->queue_lock);
1434 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1438 static int vidioc_querybuf(struct file *file, void *priv,
1439 struct v4l2_buffer *v4l2_buf)
1441 struct gspca_dev *gspca_dev = video_drvdata(file);
1442 struct gspca_frame *frame;
1444 if (v4l2_buf->index >= gspca_dev->nframes)
1447 frame = &gspca_dev->frame[v4l2_buf->index];
1448 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1452 static int vidioc_streamon(struct file *file, void *priv,
1453 enum v4l2_buf_type buf_type)
1455 struct gspca_dev *gspca_dev = video_drvdata(file);
1458 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1460 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1461 return -ERESTARTSYS;
1463 /* check the capture file */
1464 if (gspca_dev->capt_file != file) {
1469 if (gspca_dev->nframes == 0
1470 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1474 if (!gspca_dev->streaming) {
1475 ret = gspca_init_transfer(gspca_dev);
1479 PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK",
1480 gspca_dev->pixfmt.pixelformat,
1481 gspca_dev->pixfmt.width, gspca_dev->pixfmt.height);
1484 mutex_unlock(&gspca_dev->queue_lock);
1488 static int vidioc_streamoff(struct file *file, void *priv,
1489 enum v4l2_buf_type buf_type)
1491 struct gspca_dev *gspca_dev = video_drvdata(file);
1494 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1497 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1498 return -ERESTARTSYS;
1500 if (!gspca_dev->streaming) {
1505 /* check the capture file */
1506 if (gspca_dev->capt_file != file) {
1511 /* stop streaming */
1512 gspca_stream_off(gspca_dev);
1513 /* In case another thread is waiting in dqbuf */
1514 wake_up_interruptible(&gspca_dev->wq);
1516 /* empty the transfer queues */
1517 for (i = 0; i < gspca_dev->nframes; i++)
1518 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1519 atomic_set(&gspca_dev->fr_q, 0);
1520 atomic_set(&gspca_dev->fr_i, 0);
1521 gspca_dev->fr_o = 0;
1524 mutex_unlock(&gspca_dev->queue_lock);
1528 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1529 struct v4l2_jpegcompression *jpegcomp)
1531 struct gspca_dev *gspca_dev = video_drvdata(file);
1533 gspca_dev->usb_err = 0;
1534 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1537 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1538 const struct v4l2_jpegcompression *jpegcomp)
1540 struct gspca_dev *gspca_dev = video_drvdata(file);
1542 gspca_dev->usb_err = 0;
1543 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1546 static int vidioc_g_parm(struct file *filp, void *priv,
1547 struct v4l2_streamparm *parm)
1549 struct gspca_dev *gspca_dev = video_drvdata(filp);
1551 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1553 if (gspca_dev->sd_desc->get_streamparm) {
1554 gspca_dev->usb_err = 0;
1555 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1556 return gspca_dev->usb_err;
1561 static int vidioc_s_parm(struct file *filp, void *priv,
1562 struct v4l2_streamparm *parm)
1564 struct gspca_dev *gspca_dev = video_drvdata(filp);
1567 n = parm->parm.capture.readbuffers;
1568 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1569 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1571 gspca_dev->nbufread = n;
1573 if (gspca_dev->sd_desc->set_streamparm) {
1574 gspca_dev->usb_err = 0;
1575 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1576 return gspca_dev->usb_err;
1582 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1584 struct gspca_dev *gspca_dev = video_drvdata(file);
1585 struct gspca_frame *frame;
1587 unsigned long addr, start, size;
1590 start = vma->vm_start;
1591 size = vma->vm_end - vma->vm_start;
1592 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1594 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1595 return -ERESTARTSYS;
1596 if (gspca_dev->capt_file != file) {
1602 for (i = 0; i < gspca_dev->nframes; ++i) {
1603 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1604 PDEBUG(D_STREAM, "mmap bad memory type");
1607 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1609 frame = &gspca_dev->frame[i];
1613 if (frame == NULL) {
1614 PDEBUG(D_STREAM, "mmap no frame buffer found");
1618 if (size != frame->v4l2_buf.length) {
1619 PDEBUG(D_STREAM, "mmap bad size");
1625 * - VM_IO marks the area as being a mmaped region for I/O to a
1626 * device. It also prevents the region from being core dumped.
1628 vma->vm_flags |= VM_IO;
1630 addr = (unsigned long) frame->data;
1632 page = vmalloc_to_page((void *) addr);
1633 ret = vm_insert_page(vma, start, page);
1641 vma->vm_ops = &gspca_vm_ops;
1642 vma->vm_private_data = frame;
1646 mutex_unlock(&gspca_dev->queue_lock);
1650 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1651 enum v4l2_memory memory)
1653 if (!gspca_dev->present)
1655 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1656 !gspca_dev->streaming)
1659 /* check if a frame is ready */
1660 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1663 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1664 enum v4l2_memory memory)
1668 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1669 return -ERESTARTSYS;
1670 ret = frame_ready_nolock(gspca_dev, file, memory);
1671 mutex_unlock(&gspca_dev->queue_lock);
1676 * dequeue a video buffer
1678 * If nonblock_ing is false, block until a buffer is available.
1680 static int vidioc_dqbuf(struct file *file, void *priv,
1681 struct v4l2_buffer *v4l2_buf)
1683 struct gspca_dev *gspca_dev = video_drvdata(file);
1684 struct gspca_frame *frame;
1687 PDEBUG(D_FRAM, "dqbuf");
1689 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1690 return -ERESTARTSYS;
1693 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1699 mutex_unlock(&gspca_dev->queue_lock);
1701 if (file->f_flags & O_NONBLOCK)
1704 /* wait till a frame is ready */
1705 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1706 frame_ready(gspca_dev, file, v4l2_buf->memory),
1707 msecs_to_jiffies(3000));
1713 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1714 return -ERESTARTSYS;
1717 i = gspca_dev->fr_o;
1718 j = gspca_dev->fr_queue[i];
1719 frame = &gspca_dev->frame[j];
1721 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1723 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1724 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1725 PDEBUG(D_FRAM, "dqbuf %d", j);
1728 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1729 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1731 frame->v4l2_buf.bytesused)) {
1732 PERR("dqbuf cp to user failed");
1737 mutex_unlock(&gspca_dev->queue_lock);
1739 if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
1740 mutex_lock(&gspca_dev->usb_lock);
1741 gspca_dev->usb_err = 0;
1742 if (gspca_dev->present)
1743 gspca_dev->sd_desc->dq_callback(gspca_dev);
1744 mutex_unlock(&gspca_dev->usb_lock);
1751 * queue a video buffer
1753 * Attempting to queue a buffer that has already been
1754 * queued will return -EINVAL.
1756 static int vidioc_qbuf(struct file *file, void *priv,
1757 struct v4l2_buffer *v4l2_buf)
1759 struct gspca_dev *gspca_dev = video_drvdata(file);
1760 struct gspca_frame *frame;
1763 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1765 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1766 return -ERESTARTSYS;
1768 index = v4l2_buf->index;
1769 if ((unsigned) index >= gspca_dev->nframes) {
1771 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1775 if (v4l2_buf->memory != gspca_dev->memory) {
1776 PDEBUG(D_FRAM, "qbuf bad memory type");
1781 frame = &gspca_dev->frame[index];
1782 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1783 PDEBUG(D_FRAM, "qbuf bad state");
1788 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1790 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1791 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1792 frame->v4l2_buf.length = v4l2_buf->length;
1795 /* put the buffer in the 'queued' queue */
1796 i = atomic_read(&gspca_dev->fr_q);
1797 gspca_dev->fr_queue[i] = index;
1798 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1800 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1801 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1804 mutex_unlock(&gspca_dev->queue_lock);
1809 * allocate the resources for read()
1811 static int read_alloc(struct gspca_dev *gspca_dev,
1814 struct v4l2_buffer v4l2_buf;
1817 PDEBUG(D_STREAM, "read alloc");
1819 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1820 return -ERESTARTSYS;
1822 if (gspca_dev->nframes == 0) {
1823 struct v4l2_requestbuffers rb;
1825 memset(&rb, 0, sizeof rb);
1826 rb.count = gspca_dev->nbufread;
1827 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1828 rb.memory = GSPCA_MEMORY_READ;
1829 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1831 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1834 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1835 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1836 v4l2_buf.memory = GSPCA_MEMORY_READ;
1837 for (i = 0; i < gspca_dev->nbufread; i++) {
1839 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1841 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1847 /* start streaming */
1848 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1850 PDEBUG(D_STREAM, "read streamon err %d", ret);
1852 mutex_unlock(&gspca_dev->usb_lock);
1856 static unsigned int dev_poll(struct file *file, poll_table *wait)
1858 struct gspca_dev *gspca_dev = video_drvdata(file);
1859 unsigned long req_events = poll_requested_events(wait);
1862 PDEBUG(D_FRAM, "poll");
1864 if (req_events & POLLPRI)
1865 ret |= v4l2_ctrl_poll(file, wait);
1867 if (req_events & (POLLIN | POLLRDNORM)) {
1868 /* if reqbufs is not done, the user would use read() */
1869 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
1870 if (read_alloc(gspca_dev, file) != 0) {
1876 poll_wait(file, &gspca_dev->wq, wait);
1878 /* check if an image has been received */
1879 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
1883 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
1884 ret |= POLLIN | POLLRDNORM;
1885 mutex_unlock(&gspca_dev->queue_lock);
1889 if (!gspca_dev->present)
1895 static ssize_t dev_read(struct file *file, char __user *data,
1896 size_t count, loff_t *ppos)
1898 struct gspca_dev *gspca_dev = video_drvdata(file);
1899 struct gspca_frame *frame;
1900 struct v4l2_buffer v4l2_buf;
1901 struct timeval timestamp;
1904 PDEBUG(D_FRAM, "read (%zd)", count);
1905 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
1906 ret = read_alloc(gspca_dev, file);
1912 v4l2_get_timestamp(×tamp);
1916 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1917 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1918 v4l2_buf.memory = GSPCA_MEMORY_READ;
1919 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1921 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1925 /* if the process slept for more than 1 second,
1926 * get a newer frame */
1927 frame = &gspca_dev->frame[v4l2_buf.index];
1929 break; /* avoid infinite loop */
1930 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1932 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1934 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1939 /* copy the frame */
1940 if (count > frame->v4l2_buf.bytesused)
1941 count = frame->v4l2_buf.bytesused;
1942 ret = copy_to_user(data, frame->data, count);
1944 PERR("read cp to user lack %d / %zd", ret, count);
1950 /* in each case, requeue the buffer */
1951 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1957 static struct v4l2_file_operations dev_fops = {
1958 .owner = THIS_MODULE,
1960 .release = dev_close,
1963 .unlocked_ioctl = video_ioctl2,
1967 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1968 .vidioc_querycap = vidioc_querycap,
1969 .vidioc_dqbuf = vidioc_dqbuf,
1970 .vidioc_qbuf = vidioc_qbuf,
1971 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1972 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1973 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1974 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1975 .vidioc_streamon = vidioc_streamon,
1976 .vidioc_enum_input = vidioc_enum_input,
1977 .vidioc_g_input = vidioc_g_input,
1978 .vidioc_s_input = vidioc_s_input,
1979 .vidioc_reqbufs = vidioc_reqbufs,
1980 .vidioc_querybuf = vidioc_querybuf,
1981 .vidioc_streamoff = vidioc_streamoff,
1982 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1983 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1984 .vidioc_g_parm = vidioc_g_parm,
1985 .vidioc_s_parm = vidioc_s_parm,
1986 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1987 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1988 #ifdef CONFIG_VIDEO_ADV_DEBUG
1989 .vidioc_g_chip_info = vidioc_g_chip_info,
1990 .vidioc_g_register = vidioc_g_register,
1991 .vidioc_s_register = vidioc_s_register,
1993 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1994 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1997 static const struct video_device gspca_template = {
1998 .name = "gspca main driver",
2000 .ioctl_ops = &dev_ioctl_ops,
2001 .release = video_device_release_empty, /* We use v4l2_dev.release */
2005 * probe and create a new gspca device
2007 * This function must be called by the sub-driver when it is
2008 * called for probing a new device.
2010 int gspca_dev_probe2(struct usb_interface *intf,
2011 const struct usb_device_id *id,
2012 const struct sd_desc *sd_desc,
2014 struct module *module)
2016 struct gspca_dev *gspca_dev;
2017 struct usb_device *dev = interface_to_usbdev(intf);
2020 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2021 sd_desc->name, id->idVendor, id->idProduct);
2023 /* create the device */
2024 if (dev_size < sizeof *gspca_dev)
2025 dev_size = sizeof *gspca_dev;
2026 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2028 pr_err("couldn't kzalloc gspca struct\n");
2031 gspca_dev->usb_buf = kzalloc(USB_BUF_SZ, GFP_KERNEL);
2032 if (!gspca_dev->usb_buf) {
2033 pr_err("out of memory\n");
2037 gspca_dev->dev = dev;
2038 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2039 gspca_dev->xfer_ep = -1;
2041 /* check if any audio device */
2042 if (dev->actconfig->desc.bNumInterfaces != 1) {
2044 struct usb_interface *intf2;
2046 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
2047 intf2 = dev->actconfig->interface[i];
2049 && intf2->altsetting != NULL
2050 && intf2->altsetting->desc.bInterfaceClass ==
2052 gspca_dev->audio = 1;
2058 gspca_dev->v4l2_dev.release = gspca_release;
2059 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
2062 gspca_dev->sd_desc = sd_desc;
2063 gspca_dev->nbufread = 2;
2064 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2065 gspca_dev->vdev = gspca_template;
2066 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
2067 video_set_drvdata(&gspca_dev->vdev, gspca_dev);
2068 gspca_dev->module = module;
2069 gspca_dev->present = 1;
2071 mutex_init(&gspca_dev->usb_lock);
2072 gspca_dev->vdev.lock = &gspca_dev->usb_lock;
2073 mutex_init(&gspca_dev->queue_lock);
2074 init_waitqueue_head(&gspca_dev->wq);
2076 /* configure the subdriver and initialize the USB device */
2077 ret = sd_desc->config(gspca_dev, id);
2080 ret = sd_desc->init(gspca_dev);
2083 if (sd_desc->init_controls)
2084 ret = sd_desc->init_controls(gspca_dev);
2087 gspca_set_default_mode(gspca_dev);
2089 ret = gspca_input_connect(gspca_dev);
2094 * Don't take usb_lock for these ioctls. This improves latency if
2095 * usb_lock is taken for a long time, e.g. when changing a control
2096 * value, and a new frame is ready to be dequeued.
2098 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
2099 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
2100 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
2101 #ifdef CONFIG_VIDEO_ADV_DEBUG
2102 if (!gspca_dev->sd_desc->get_register)
2103 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
2104 if (!gspca_dev->sd_desc->set_register)
2105 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
2107 if (!gspca_dev->sd_desc->get_jcomp)
2108 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
2109 if (!gspca_dev->sd_desc->set_jcomp)
2110 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
2112 /* init video stuff */
2113 ret = video_register_device(&gspca_dev->vdev,
2117 pr_err("video_register_device err %d\n", ret);
2121 usb_set_intfdata(intf, gspca_dev);
2122 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2124 gspca_input_create_urb(gspca_dev);
2128 #if IS_ENABLED(CONFIG_INPUT)
2129 if (gspca_dev->input_dev)
2130 input_unregister_device(gspca_dev->input_dev);
2132 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
2133 v4l2_device_unregister(&gspca_dev->v4l2_dev);
2134 if (sd_desc->probe_error)
2135 sd_desc->probe_error(gspca_dev);
2136 kfree(gspca_dev->usb_buf);
2140 EXPORT_SYMBOL(gspca_dev_probe2);
2142 /* same function as the previous one, but check the interface */
2143 int gspca_dev_probe(struct usb_interface *intf,
2144 const struct usb_device_id *id,
2145 const struct sd_desc *sd_desc,
2147 struct module *module)
2149 struct usb_device *dev = interface_to_usbdev(intf);
2151 /* we don't handle multi-config cameras */
2152 if (dev->descriptor.bNumConfigurations != 1) {
2153 pr_err("%04x:%04x too many config\n",
2154 id->idVendor, id->idProduct);
2158 /* the USB video interface must be the first one */
2159 if (dev->actconfig->desc.bNumInterfaces != 1
2160 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2163 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2165 EXPORT_SYMBOL(gspca_dev_probe);
2170 * This function must be called by the sub-driver
2171 * when the device disconnects, after the specific resources are freed.
2173 void gspca_disconnect(struct usb_interface *intf)
2175 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2176 #if IS_ENABLED(CONFIG_INPUT)
2177 struct input_dev *input_dev;
2180 PDEBUG(D_PROBE, "%s disconnect",
2181 video_device_node_name(&gspca_dev->vdev));
2183 mutex_lock(&gspca_dev->usb_lock);
2185 gspca_dev->present = 0;
2186 destroy_urbs(gspca_dev);
2188 #if IS_ENABLED(CONFIG_INPUT)
2189 gspca_input_destroy_urb(gspca_dev);
2190 input_dev = gspca_dev->input_dev;
2192 gspca_dev->input_dev = NULL;
2193 input_unregister_device(input_dev);
2196 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2197 if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
2198 gspca_dev->sd_desc->stop0(gspca_dev);
2199 gspca_dev->streaming = 0;
2200 gspca_dev->dev = NULL;
2201 wake_up_interruptible(&gspca_dev->wq);
2203 v4l2_device_disconnect(&gspca_dev->v4l2_dev);
2204 video_unregister_device(&gspca_dev->vdev);
2206 mutex_unlock(&gspca_dev->usb_lock);
2208 /* (this will call gspca_release() immediately or on last close) */
2209 v4l2_device_put(&gspca_dev->v4l2_dev);
2211 EXPORT_SYMBOL(gspca_disconnect);
2214 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2216 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2218 gspca_input_destroy_urb(gspca_dev);
2220 if (!gspca_dev->streaming)
2223 mutex_lock(&gspca_dev->usb_lock);
2224 gspca_dev->frozen = 1; /* avoid urb error messages */
2225 gspca_dev->usb_err = 0;
2226 if (gspca_dev->sd_desc->stopN)
2227 gspca_dev->sd_desc->stopN(gspca_dev);
2228 destroy_urbs(gspca_dev);
2229 gspca_set_alt0(gspca_dev);
2230 if (gspca_dev->sd_desc->stop0)
2231 gspca_dev->sd_desc->stop0(gspca_dev);
2232 mutex_unlock(&gspca_dev->usb_lock);
2236 EXPORT_SYMBOL(gspca_suspend);
2238 int gspca_resume(struct usb_interface *intf)
2240 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2241 int streaming, ret = 0;
2243 mutex_lock(&gspca_dev->usb_lock);
2244 gspca_dev->frozen = 0;
2245 gspca_dev->usb_err = 0;
2246 gspca_dev->sd_desc->init(gspca_dev);
2248 * Most subdrivers send all ctrl values on sd_start and thus
2249 * only write to the device registers on s_ctrl when streaming ->
2250 * Clear streaming to avoid setting all ctrls twice.
2252 streaming = gspca_dev->streaming;
2253 gspca_dev->streaming = 0;
2255 ret = gspca_init_transfer(gspca_dev);
2257 gspca_input_create_urb(gspca_dev);
2258 mutex_unlock(&gspca_dev->usb_lock);
2262 EXPORT_SYMBOL(gspca_resume);
2265 /* -- module insert / remove -- */
2266 static int __init gspca_init(void)
2268 pr_info("v" GSPCA_VERSION " registered\n");
2271 static void __exit gspca_exit(void)
2275 module_init(gspca_init);
2276 module_exit(gspca_exit);
2278 module_param_named(debug, gspca_debug, int, 0644);
2279 MODULE_PARM_DESC(debug,
2280 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");