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
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 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
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 i = gspca_dev->curr_mode;
508 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
509 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
510 frsz = PAGE_ALIGN(frsz);
511 if (count >= GSPCA_MAX_FRAMES)
512 count = GSPCA_MAX_FRAMES - 1;
513 gspca_dev->frbuf = vmalloc_32(frsz * count);
514 if (!gspca_dev->frbuf) {
515 pr_err("frame alloc failed\n");
518 gspca_dev->capt_file = file;
519 gspca_dev->memory = memory;
520 gspca_dev->frsz = frsz;
521 gspca_dev->nframes = count;
522 for (i = 0; i < count; i++) {
523 frame = &gspca_dev->frame[i];
524 frame->v4l2_buf.index = i;
525 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
526 frame->v4l2_buf.flags = 0;
527 frame->v4l2_buf.field = V4L2_FIELD_NONE;
528 frame->v4l2_buf.length = frsz;
529 frame->v4l2_buf.memory = memory;
530 frame->v4l2_buf.sequence = 0;
531 frame->data = gspca_dev->frbuf + i * frsz;
532 frame->v4l2_buf.m.offset = i * frsz;
534 atomic_set(&gspca_dev->fr_q, 0);
535 atomic_set(&gspca_dev->fr_i, 0);
540 static void frame_free(struct gspca_dev *gspca_dev)
544 PDEBUG(D_STREAM, "frame free");
545 if (gspca_dev->frbuf != NULL) {
546 vfree(gspca_dev->frbuf);
547 gspca_dev->frbuf = NULL;
548 for (i = 0; i < gspca_dev->nframes; i++)
549 gspca_dev->frame[i].data = NULL;
551 gspca_dev->nframes = 0;
553 gspca_dev->capt_file = NULL;
554 gspca_dev->memory = GSPCA_MEMORY_NO;
557 static void destroy_urbs(struct gspca_dev *gspca_dev)
562 PDEBUG(D_STREAM, "kill transfer");
563 for (i = 0; i < MAX_NURBS; i++) {
564 urb = gspca_dev->urb[i];
568 gspca_dev->urb[i] = NULL;
570 usb_free_coherent(gspca_dev->dev,
571 urb->transfer_buffer_length,
572 urb->transfer_buffer,
578 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
582 if (gspca_dev->alt == 0)
584 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
586 pr_err("set alt 0 err %d\n", ret);
590 /* Note: both the queue and the usb locks should be held when calling this */
591 static void gspca_stream_off(struct gspca_dev *gspca_dev)
593 gspca_dev->streaming = 0;
594 gspca_dev->usb_err = 0;
595 if (gspca_dev->sd_desc->stopN)
596 gspca_dev->sd_desc->stopN(gspca_dev);
597 destroy_urbs(gspca_dev);
598 gspca_input_destroy_urb(gspca_dev);
599 gspca_set_alt0(gspca_dev);
600 gspca_input_create_urb(gspca_dev);
601 if (gspca_dev->sd_desc->stop0)
602 gspca_dev->sd_desc->stop0(gspca_dev);
603 PDEBUG(D_STREAM, "stream off OK");
607 * look for an input transfer endpoint in an alternate setting
609 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
612 struct usb_host_endpoint *ep;
615 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
616 ep = &alt->endpoint[i];
617 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
619 && ep->desc.wMaxPacketSize != 0
620 && usb_endpoint_dir_in(&ep->desc))
626 /* compute the minimum bandwidth for the current transfer */
627 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
632 /* get the (max) image size */
633 i = gspca_dev->curr_mode;
634 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
636 /* if the image is compressed, estimate its mean size */
637 if (!gspca_dev->cam.needs_full_bandwidth &&
638 bandwidth < gspca_dev->cam.cam_mode[i].width *
639 gspca_dev->cam.cam_mode[i].height)
640 bandwidth = bandwidth * 3 / 8; /* 0.375 */
642 /* estimate the frame rate */
643 if (gspca_dev->sd_desc->get_streamparm) {
644 struct v4l2_streamparm parm;
646 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
647 bandwidth *= parm.parm.capture.timeperframe.denominator;
648 bandwidth /= parm.parm.capture.timeperframe.numerator;
651 /* don't hope more than 15 fps with USB 1.1 and
652 * image resolution >= 640x480 */
653 if (gspca_dev->width >= 640
654 && gspca_dev->dev->speed == USB_SPEED_FULL)
655 bandwidth *= 15; /* 15 fps */
657 bandwidth *= 30; /* 30 fps */
660 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
672 * build the table of the endpoints
673 * and compute the minimum bandwidth for the image transfer
675 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
676 struct usb_interface *intf,
677 struct ep_tb_s *ep_tb)
679 struct usb_host_endpoint *ep;
680 int i, j, nbalt, psize, found;
681 u32 bandwidth, last_bw;
683 nbalt = intf->num_altsetting;
685 nbalt = MAX_ALT; /* fixme: should warn */
687 /* build the endpoint table */
691 ep_tb->bandwidth = 2000 * 2000 * 120;
693 for (j = 0; j < nbalt; j++) {
694 ep = alt_xfer(&intf->altsetting[j],
695 USB_ENDPOINT_XFER_ISOC);
698 if (ep->desc.bInterval == 0) {
699 pr_err("alt %d iso endp with 0 interval\n", j);
702 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
703 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
704 bandwidth = psize * 1000;
705 if (gspca_dev->dev->speed == USB_SPEED_HIGH
706 || gspca_dev->dev->speed == USB_SPEED_SUPER)
708 bandwidth /= 1 << (ep->desc.bInterval - 1);
709 if (bandwidth <= last_bw)
711 if (bandwidth < ep_tb->bandwidth) {
712 ep_tb->bandwidth = bandwidth;
719 PDEBUG(D_STREAM, "alt %d bandwidth %d",
720 ep_tb->alt, ep_tb->bandwidth);
721 last_bw = ep_tb->bandwidth;
728 * has a usb audio class interface (a built in usb mic); and
729 * is a usb 1 full speed device; and
730 * uses the max full speed iso bandwidth; and
731 * and has more than 1 alt setting
732 * then skip the highest alt setting to spare bandwidth for the mic
734 if (gspca_dev->audio &&
735 gspca_dev->dev->speed == USB_SPEED_FULL &&
736 last_bw >= 1000000 &&
738 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
743 /* get the requested bandwidth and start at the highest atlsetting */
744 bandwidth = which_bandwidth(gspca_dev);
748 if (ep_tb->bandwidth < bandwidth)
756 * create the URBs for image transfer
758 static int create_urbs(struct gspca_dev *gspca_dev,
759 struct usb_host_endpoint *ep)
762 int n, nurbs, i, psize, npkt, bsize;
764 /* calculate the packet size and the number of packets */
765 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
767 if (!gspca_dev->cam.bulk) { /* isoc */
769 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
770 if (gspca_dev->pkt_size == 0)
771 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
773 psize = gspca_dev->pkt_size;
774 npkt = gspca_dev->cam.npkt;
776 npkt = 32; /* default value */
777 bsize = psize * npkt;
779 "isoc %d pkts size %d = bsize:%d",
784 bsize = gspca_dev->cam.bulk_size;
787 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
788 if (gspca_dev->cam.bulk_nurbs != 0)
789 nurbs = gspca_dev->cam.bulk_nurbs;
794 for (n = 0; n < nurbs; n++) {
795 urb = usb_alloc_urb(npkt, GFP_KERNEL);
797 pr_err("usb_alloc_urb failed\n");
800 gspca_dev->urb[n] = urb;
801 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
806 if (urb->transfer_buffer == NULL) {
807 pr_err("usb_alloc_coherent failed\n");
810 urb->dev = gspca_dev->dev;
811 urb->context = gspca_dev;
812 urb->transfer_buffer_length = bsize;
813 if (npkt != 0) { /* ISOC */
814 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
815 ep->desc.bEndpointAddress);
816 urb->transfer_flags = URB_ISO_ASAP
817 | URB_NO_TRANSFER_DMA_MAP;
818 urb->interval = 1 << (ep->desc.bInterval - 1);
819 urb->complete = isoc_irq;
820 urb->number_of_packets = npkt;
821 for (i = 0; i < npkt; i++) {
822 urb->iso_frame_desc[i].length = psize;
823 urb->iso_frame_desc[i].offset = psize * i;
826 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
827 ep->desc.bEndpointAddress);
828 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
829 urb->complete = bulk_irq;
836 * start the USB transfer
838 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
840 struct usb_interface *intf;
841 struct usb_host_endpoint *ep;
843 struct ep_tb_s ep_tb[MAX_ALT];
844 int n, ret, xfer, alt, alt_idx;
846 /* reset the streaming variables */
847 gspca_dev->image = NULL;
848 gspca_dev->image_len = 0;
849 gspca_dev->last_packet_type = DISCARD_PACKET;
850 gspca_dev->sequence = 0;
852 gspca_dev->usb_err = 0;
854 /* do the specific subdriver stuff before endpoint selection */
855 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
856 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
857 if (gspca_dev->sd_desc->isoc_init) {
858 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
862 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
863 : USB_ENDPOINT_XFER_ISOC;
865 /* if bulk or the subdriver forced an altsetting, get the endpoint */
866 if (gspca_dev->alt != 0) {
867 gspca_dev->alt--; /* (previous version compatibility) */
868 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
870 pr_err("bad altsetting %d\n", gspca_dev->alt);
873 ep_tb[0].alt = gspca_dev->alt;
877 /* else, compute the minimum bandwidth
878 * and build the endpoint table */
879 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
881 pr_err("no transfer endpoint found\n");
886 /* set the highest alternate setting and
887 * loop until urb submit succeeds */
888 gspca_input_destroy_urb(gspca_dev);
890 gspca_dev->alt = ep_tb[--alt_idx].alt;
893 if (alt != gspca_dev->alt) {
894 alt = gspca_dev->alt;
895 if (intf->num_altsetting > 1) {
896 ret = usb_set_interface(gspca_dev->dev,
901 goto retry; /*fixme: ugly*/
902 pr_err("set alt %d err %d\n", alt, ret);
907 if (!gspca_dev->cam.no_urb_create) {
908 PDEBUG(D_STREAM, "init transfer alt %d", alt);
909 ret = create_urbs(gspca_dev,
910 alt_xfer(&intf->altsetting[alt], xfer));
912 destroy_urbs(gspca_dev);
917 /* clear the bulk endpoint */
918 if (gspca_dev->cam.bulk)
919 usb_clear_halt(gspca_dev->dev,
920 gspca_dev->urb[0]->pipe);
923 ret = gspca_dev->sd_desc->start(gspca_dev);
925 destroy_urbs(gspca_dev);
928 gspca_dev->streaming = 1;
929 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
931 /* some bulk transfers are started by the subdriver */
932 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
935 /* submit the URBs */
936 for (n = 0; n < MAX_NURBS; n++) {
937 urb = gspca_dev->urb[n];
940 ret = usb_submit_urb(urb, GFP_KERNEL);
945 break; /* transfer is started */
947 /* something when wrong
948 * stop the webcam and free the transfer resources */
949 gspca_stream_off(gspca_dev);
950 if (ret != -ENOSPC) {
951 pr_err("usb_submit_urb alt %d err %d\n",
952 gspca_dev->alt, ret);
956 /* the bandwidth is not wide enough
957 * negotiate or try a lower alternate setting */
959 PERR("alt %d - bandwidth not wide enough, trying again", alt);
960 msleep(20); /* wait for kill complete */
961 if (gspca_dev->sd_desc->isoc_nego) {
962 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
967 pr_err("no transfer endpoint found\n");
971 gspca_dev->alt = ep_tb[--alt_idx].alt;
975 gspca_input_create_urb(gspca_dev);
979 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
983 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
984 gspca_dev->curr_mode = i;
985 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
986 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
987 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
989 /* does nothing if ctrl_handler == NULL */
990 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
993 static int wxh_to_mode(struct gspca_dev *gspca_dev,
994 int width, int height)
998 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
999 if (width >= gspca_dev->cam.cam_mode[i].width
1000 && height >= gspca_dev->cam.cam_mode[i].height)
1007 * search a mode with the right pixel format
1009 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1015 modeU = modeD = mode;
1016 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1018 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1022 if (++modeU < gspca_dev->cam.nmodes) {
1023 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1031 #ifdef CONFIG_VIDEO_ADV_DEBUG
1032 static int vidioc_g_chip_info(struct file *file, void *priv,
1033 struct v4l2_dbg_chip_info *chip)
1035 struct gspca_dev *gspca_dev = video_drvdata(file);
1037 gspca_dev->usb_err = 0;
1038 if (gspca_dev->sd_desc->get_chip_info)
1039 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
1040 return chip->match.addr ? -EINVAL : 0;
1043 static int vidioc_g_register(struct file *file, void *priv,
1044 struct v4l2_dbg_register *reg)
1046 struct gspca_dev *gspca_dev = video_drvdata(file);
1048 gspca_dev->usb_err = 0;
1049 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
1052 static int vidioc_s_register(struct file *file, void *priv,
1053 const struct v4l2_dbg_register *reg)
1055 struct gspca_dev *gspca_dev = video_drvdata(file);
1057 gspca_dev->usb_err = 0;
1058 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
1062 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1063 struct v4l2_fmtdesc *fmtdesc)
1065 struct gspca_dev *gspca_dev = video_drvdata(file);
1069 /* give an index to each format */
1072 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1073 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1076 if (fmt_tb[j] == fmt_tb[index])
1081 if (fmtdesc->index == index)
1082 break; /* new format */
1084 if (index >= ARRAY_SIZE(fmt_tb))
1089 return -EINVAL; /* no more format */
1091 fmtdesc->pixelformat = fmt_tb[index];
1092 if (gspca_dev->cam.cam_mode[i].sizeimage <
1093 gspca_dev->cam.cam_mode[i].width *
1094 gspca_dev->cam.cam_mode[i].height)
1095 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1096 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1097 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1098 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1099 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1100 fmtdesc->description[4] = '\0';
1104 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1105 struct v4l2_format *fmt)
1107 struct gspca_dev *gspca_dev = video_drvdata(file);
1110 mode = gspca_dev->curr_mode;
1111 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1112 /* some drivers use priv internally, zero it before giving it to
1114 fmt->fmt.pix.priv = 0;
1118 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1119 struct v4l2_format *fmt)
1121 int w, h, mode, mode2;
1123 w = fmt->fmt.pix.width;
1124 h = fmt->fmt.pix.height;
1126 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1127 fmt->fmt.pix.pixelformat, w, h);
1129 /* search the closest mode for width and height */
1130 mode = wxh_to_mode(gspca_dev, w, h);
1132 /* OK if right palette */
1133 if (gspca_dev->cam.cam_mode[mode].pixelformat
1134 != fmt->fmt.pix.pixelformat) {
1136 /* else, search the closest mode with the same pixel format */
1137 mode2 = gspca_get_mode(gspca_dev, mode,
1138 fmt->fmt.pix.pixelformat);
1142 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1143 /* some drivers use priv internally, zero it before giving it to
1145 fmt->fmt.pix.priv = 0;
1146 return mode; /* used when s_fmt */
1149 static int vidioc_try_fmt_vid_cap(struct file *file,
1151 struct v4l2_format *fmt)
1153 struct gspca_dev *gspca_dev = video_drvdata(file);
1156 ret = try_fmt_vid_cap(gspca_dev, fmt);
1162 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1163 struct v4l2_format *fmt)
1165 struct gspca_dev *gspca_dev = video_drvdata(file);
1168 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1169 return -ERESTARTSYS;
1171 ret = try_fmt_vid_cap(gspca_dev, fmt);
1175 if (gspca_dev->nframes != 0
1176 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1181 if (ret == gspca_dev->curr_mode) {
1183 goto out; /* same mode */
1186 if (gspca_dev->streaming) {
1190 gspca_dev->width = fmt->fmt.pix.width;
1191 gspca_dev->height = fmt->fmt.pix.height;
1192 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1193 gspca_dev->curr_mode = ret;
1197 mutex_unlock(&gspca_dev->queue_lock);
1201 static int vidioc_enum_framesizes(struct file *file, void *priv,
1202 struct v4l2_frmsizeenum *fsize)
1204 struct gspca_dev *gspca_dev = video_drvdata(file);
1208 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1209 if (fsize->pixel_format !=
1210 gspca_dev->cam.cam_mode[i].pixelformat)
1213 if (fsize->index == index) {
1214 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1215 fsize->discrete.width =
1216 gspca_dev->cam.cam_mode[i].width;
1217 fsize->discrete.height =
1218 gspca_dev->cam.cam_mode[i].height;
1227 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1228 struct v4l2_frmivalenum *fival)
1230 struct gspca_dev *gspca_dev = video_drvdata(filp);
1231 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1234 if (gspca_dev->cam.mode_framerates == NULL ||
1235 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1238 if (fival->pixel_format !=
1239 gspca_dev->cam.cam_mode[mode].pixelformat)
1242 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1243 if (fival->index == i) {
1244 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1245 fival->discrete.numerator = 1;
1246 fival->discrete.denominator =
1247 gspca_dev->cam.mode_framerates[mode].rates[i];
1255 static void gspca_release(struct v4l2_device *v4l2_device)
1257 struct gspca_dev *gspca_dev =
1258 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1260 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1261 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1262 kfree(gspca_dev->usb_buf);
1266 static int dev_open(struct file *file)
1268 struct gspca_dev *gspca_dev = video_drvdata(file);
1270 PDEBUG(D_STREAM, "[%s] open", current->comm);
1272 /* protect the subdriver against rmmod */
1273 if (!try_module_get(gspca_dev->module))
1276 return v4l2_fh_open(file);
1279 static int dev_close(struct file *file)
1281 struct gspca_dev *gspca_dev = video_drvdata(file);
1283 PDEBUG(D_STREAM, "[%s] close", current->comm);
1285 /* Needed for gspca_stream_off, always lock before queue_lock! */
1286 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1287 return -ERESTARTSYS;
1289 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
1290 mutex_unlock(&gspca_dev->usb_lock);
1291 return -ERESTARTSYS;
1294 /* if the file did the capture, free the streaming resources */
1295 if (gspca_dev->capt_file == file) {
1296 if (gspca_dev->streaming)
1297 gspca_stream_off(gspca_dev);
1298 frame_free(gspca_dev);
1300 module_put(gspca_dev->module);
1301 mutex_unlock(&gspca_dev->queue_lock);
1302 mutex_unlock(&gspca_dev->usb_lock);
1304 PDEBUG(D_STREAM, "close done");
1306 return v4l2_fh_release(file);
1309 static int vidioc_querycap(struct file *file, void *priv,
1310 struct v4l2_capability *cap)
1312 struct gspca_dev *gspca_dev = video_drvdata(file);
1314 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1315 sizeof cap->driver);
1316 if (gspca_dev->dev->product != NULL) {
1317 strlcpy((char *) cap->card, gspca_dev->dev->product,
1320 snprintf((char *) cap->card, sizeof cap->card,
1321 "USB Camera (%04x:%04x)",
1322 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1323 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1325 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1326 sizeof(cap->bus_info));
1327 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
1328 | V4L2_CAP_STREAMING
1329 | V4L2_CAP_READWRITE;
1330 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1334 static int vidioc_enum_input(struct file *file, void *priv,
1335 struct v4l2_input *input)
1337 struct gspca_dev *gspca_dev = video_drvdata(file);
1339 if (input->index != 0)
1341 input->type = V4L2_INPUT_TYPE_CAMERA;
1342 input->status = gspca_dev->cam.input_flags;
1343 strlcpy(input->name, gspca_dev->sd_desc->name,
1344 sizeof input->name);
1348 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1354 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1361 static int vidioc_reqbufs(struct file *file, void *priv,
1362 struct v4l2_requestbuffers *rb)
1364 struct gspca_dev *gspca_dev = video_drvdata(file);
1365 int i, ret = 0, streaming;
1367 i = rb->memory; /* (avoid compilation warning) */
1369 case GSPCA_MEMORY_READ: /* (internal call) */
1370 case V4L2_MEMORY_MMAP:
1371 case V4L2_MEMORY_USERPTR:
1376 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1377 return -ERESTARTSYS;
1379 if (gspca_dev->memory != GSPCA_MEMORY_NO
1380 && gspca_dev->memory != GSPCA_MEMORY_READ
1381 && gspca_dev->memory != rb->memory) {
1386 /* only one file may do the capture */
1387 if (gspca_dev->capt_file != NULL
1388 && gspca_dev->capt_file != file) {
1393 /* if allocated, the buffers must not be mapped */
1394 for (i = 0; i < gspca_dev->nframes; i++) {
1395 if (gspca_dev->frame[i].vma_use_count) {
1401 /* stop streaming */
1402 streaming = gspca_dev->streaming;
1404 gspca_stream_off(gspca_dev);
1406 /* Don't restart the stream when switching from read
1408 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1412 /* free the previous allocated buffers, if any */
1413 if (gspca_dev->nframes != 0)
1414 frame_free(gspca_dev);
1415 if (rb->count == 0) /* unrequest */
1417 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1419 rb->count = gspca_dev->nframes;
1421 ret = gspca_init_transfer(gspca_dev);
1424 mutex_unlock(&gspca_dev->queue_lock);
1425 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1429 static int vidioc_querybuf(struct file *file, void *priv,
1430 struct v4l2_buffer *v4l2_buf)
1432 struct gspca_dev *gspca_dev = video_drvdata(file);
1433 struct gspca_frame *frame;
1435 if (v4l2_buf->index >= gspca_dev->nframes)
1438 frame = &gspca_dev->frame[v4l2_buf->index];
1439 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1443 static int vidioc_streamon(struct file *file, void *priv,
1444 enum v4l2_buf_type buf_type)
1446 struct gspca_dev *gspca_dev = video_drvdata(file);
1449 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1451 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1452 return -ERESTARTSYS;
1454 /* check the capture file */
1455 if (gspca_dev->capt_file != file) {
1460 if (gspca_dev->nframes == 0
1461 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1465 if (!gspca_dev->streaming) {
1466 ret = gspca_init_transfer(gspca_dev);
1470 PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK", gspca_dev->pixfmt,
1471 gspca_dev->width, gspca_dev->height);
1474 mutex_unlock(&gspca_dev->queue_lock);
1478 static int vidioc_streamoff(struct file *file, void *priv,
1479 enum v4l2_buf_type buf_type)
1481 struct gspca_dev *gspca_dev = video_drvdata(file);
1484 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1487 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1488 return -ERESTARTSYS;
1490 if (!gspca_dev->streaming) {
1495 /* check the capture file */
1496 if (gspca_dev->capt_file != file) {
1501 /* stop streaming */
1502 gspca_stream_off(gspca_dev);
1503 /* In case another thread is waiting in dqbuf */
1504 wake_up_interruptible(&gspca_dev->wq);
1506 /* empty the transfer queues */
1507 for (i = 0; i < gspca_dev->nframes; i++)
1508 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1509 atomic_set(&gspca_dev->fr_q, 0);
1510 atomic_set(&gspca_dev->fr_i, 0);
1511 gspca_dev->fr_o = 0;
1514 mutex_unlock(&gspca_dev->queue_lock);
1518 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1519 struct v4l2_jpegcompression *jpegcomp)
1521 struct gspca_dev *gspca_dev = video_drvdata(file);
1523 gspca_dev->usb_err = 0;
1524 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1527 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1528 const struct v4l2_jpegcompression *jpegcomp)
1530 struct gspca_dev *gspca_dev = video_drvdata(file);
1532 gspca_dev->usb_err = 0;
1533 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1536 static int vidioc_g_parm(struct file *filp, void *priv,
1537 struct v4l2_streamparm *parm)
1539 struct gspca_dev *gspca_dev = video_drvdata(filp);
1541 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1543 if (gspca_dev->sd_desc->get_streamparm) {
1544 gspca_dev->usb_err = 0;
1545 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1546 return gspca_dev->usb_err;
1551 static int vidioc_s_parm(struct file *filp, void *priv,
1552 struct v4l2_streamparm *parm)
1554 struct gspca_dev *gspca_dev = video_drvdata(filp);
1557 n = parm->parm.capture.readbuffers;
1558 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1559 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1561 gspca_dev->nbufread = n;
1563 if (gspca_dev->sd_desc->set_streamparm) {
1564 gspca_dev->usb_err = 0;
1565 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1566 return gspca_dev->usb_err;
1572 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1574 struct gspca_dev *gspca_dev = video_drvdata(file);
1575 struct gspca_frame *frame;
1577 unsigned long addr, start, size;
1580 start = vma->vm_start;
1581 size = vma->vm_end - vma->vm_start;
1582 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1584 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1585 return -ERESTARTSYS;
1586 if (gspca_dev->capt_file != file) {
1592 for (i = 0; i < gspca_dev->nframes; ++i) {
1593 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1594 PDEBUG(D_STREAM, "mmap bad memory type");
1597 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1599 frame = &gspca_dev->frame[i];
1603 if (frame == NULL) {
1604 PDEBUG(D_STREAM, "mmap no frame buffer found");
1608 if (size != frame->v4l2_buf.length) {
1609 PDEBUG(D_STREAM, "mmap bad size");
1615 * - VM_IO marks the area as being a mmaped region for I/O to a
1616 * device. It also prevents the region from being core dumped.
1618 vma->vm_flags |= VM_IO;
1620 addr = (unsigned long) frame->data;
1622 page = vmalloc_to_page((void *) addr);
1623 ret = vm_insert_page(vma, start, page);
1631 vma->vm_ops = &gspca_vm_ops;
1632 vma->vm_private_data = frame;
1636 mutex_unlock(&gspca_dev->queue_lock);
1640 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1641 enum v4l2_memory memory)
1643 if (!gspca_dev->present)
1645 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1646 !gspca_dev->streaming)
1649 /* check if a frame is ready */
1650 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1653 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1654 enum v4l2_memory memory)
1658 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1659 return -ERESTARTSYS;
1660 ret = frame_ready_nolock(gspca_dev, file, memory);
1661 mutex_unlock(&gspca_dev->queue_lock);
1666 * dequeue a video buffer
1668 * If nonblock_ing is false, block until a buffer is available.
1670 static int vidioc_dqbuf(struct file *file, void *priv,
1671 struct v4l2_buffer *v4l2_buf)
1673 struct gspca_dev *gspca_dev = video_drvdata(file);
1674 struct gspca_frame *frame;
1677 PDEBUG(D_FRAM, "dqbuf");
1679 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1680 return -ERESTARTSYS;
1683 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1689 mutex_unlock(&gspca_dev->queue_lock);
1691 if (file->f_flags & O_NONBLOCK)
1694 /* wait till a frame is ready */
1695 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1696 frame_ready(gspca_dev, file, v4l2_buf->memory),
1697 msecs_to_jiffies(3000));
1703 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1704 return -ERESTARTSYS;
1707 i = gspca_dev->fr_o;
1708 j = gspca_dev->fr_queue[i];
1709 frame = &gspca_dev->frame[j];
1711 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1713 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1714 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1715 PDEBUG(D_FRAM, "dqbuf %d", j);
1718 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1719 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1721 frame->v4l2_buf.bytesused)) {
1722 PERR("dqbuf cp to user failed");
1727 mutex_unlock(&gspca_dev->queue_lock);
1729 if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
1730 mutex_lock(&gspca_dev->usb_lock);
1731 gspca_dev->usb_err = 0;
1732 if (gspca_dev->present)
1733 gspca_dev->sd_desc->dq_callback(gspca_dev);
1734 mutex_unlock(&gspca_dev->usb_lock);
1741 * queue a video buffer
1743 * Attempting to queue a buffer that has already been
1744 * queued will return -EINVAL.
1746 static int vidioc_qbuf(struct file *file, void *priv,
1747 struct v4l2_buffer *v4l2_buf)
1749 struct gspca_dev *gspca_dev = video_drvdata(file);
1750 struct gspca_frame *frame;
1753 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1755 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1756 return -ERESTARTSYS;
1758 index = v4l2_buf->index;
1759 if ((unsigned) index >= gspca_dev->nframes) {
1761 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1765 if (v4l2_buf->memory != gspca_dev->memory) {
1766 PDEBUG(D_FRAM, "qbuf bad memory type");
1771 frame = &gspca_dev->frame[index];
1772 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1773 PDEBUG(D_FRAM, "qbuf bad state");
1778 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1780 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1781 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1782 frame->v4l2_buf.length = v4l2_buf->length;
1785 /* put the buffer in the 'queued' queue */
1786 i = atomic_read(&gspca_dev->fr_q);
1787 gspca_dev->fr_queue[i] = index;
1788 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1790 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1791 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1794 mutex_unlock(&gspca_dev->queue_lock);
1799 * allocate the resources for read()
1801 static int read_alloc(struct gspca_dev *gspca_dev,
1804 struct v4l2_buffer v4l2_buf;
1807 PDEBUG(D_STREAM, "read alloc");
1809 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1810 return -ERESTARTSYS;
1812 if (gspca_dev->nframes == 0) {
1813 struct v4l2_requestbuffers rb;
1815 memset(&rb, 0, sizeof rb);
1816 rb.count = gspca_dev->nbufread;
1817 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1818 rb.memory = GSPCA_MEMORY_READ;
1819 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1821 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1824 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1825 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1826 v4l2_buf.memory = GSPCA_MEMORY_READ;
1827 for (i = 0; i < gspca_dev->nbufread; i++) {
1829 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1831 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1837 /* start streaming */
1838 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1840 PDEBUG(D_STREAM, "read streamon err %d", ret);
1842 mutex_unlock(&gspca_dev->usb_lock);
1846 static unsigned int dev_poll(struct file *file, poll_table *wait)
1848 struct gspca_dev *gspca_dev = video_drvdata(file);
1849 unsigned long req_events = poll_requested_events(wait);
1852 PDEBUG(D_FRAM, "poll");
1854 if (req_events & POLLPRI)
1855 ret |= v4l2_ctrl_poll(file, wait);
1857 if (req_events & (POLLIN | POLLRDNORM)) {
1858 /* if reqbufs is not done, the user would use read() */
1859 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
1860 if (read_alloc(gspca_dev, file) != 0) {
1866 poll_wait(file, &gspca_dev->wq, wait);
1868 /* check if an image has been received */
1869 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
1873 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
1874 ret |= POLLIN | POLLRDNORM;
1875 mutex_unlock(&gspca_dev->queue_lock);
1879 if (!gspca_dev->present)
1885 static ssize_t dev_read(struct file *file, char __user *data,
1886 size_t count, loff_t *ppos)
1888 struct gspca_dev *gspca_dev = video_drvdata(file);
1889 struct gspca_frame *frame;
1890 struct v4l2_buffer v4l2_buf;
1891 struct timeval timestamp;
1894 PDEBUG(D_FRAM, "read (%zd)", count);
1895 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
1896 ret = read_alloc(gspca_dev, file);
1902 timestamp = ktime_to_timeval(ktime_get());
1906 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1907 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1908 v4l2_buf.memory = GSPCA_MEMORY_READ;
1909 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1911 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1915 /* if the process slept for more than 1 second,
1916 * get a newer frame */
1917 frame = &gspca_dev->frame[v4l2_buf.index];
1919 break; /* avoid infinite loop */
1920 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1922 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1924 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1929 /* copy the frame */
1930 if (count > frame->v4l2_buf.bytesused)
1931 count = frame->v4l2_buf.bytesused;
1932 ret = copy_to_user(data, frame->data, count);
1934 PERR("read cp to user lack %d / %zd", ret, count);
1940 /* in each case, requeue the buffer */
1941 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1947 static struct v4l2_file_operations dev_fops = {
1948 .owner = THIS_MODULE,
1950 .release = dev_close,
1953 .unlocked_ioctl = video_ioctl2,
1957 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1958 .vidioc_querycap = vidioc_querycap,
1959 .vidioc_dqbuf = vidioc_dqbuf,
1960 .vidioc_qbuf = vidioc_qbuf,
1961 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1962 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1963 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1964 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1965 .vidioc_streamon = vidioc_streamon,
1966 .vidioc_enum_input = vidioc_enum_input,
1967 .vidioc_g_input = vidioc_g_input,
1968 .vidioc_s_input = vidioc_s_input,
1969 .vidioc_reqbufs = vidioc_reqbufs,
1970 .vidioc_querybuf = vidioc_querybuf,
1971 .vidioc_streamoff = vidioc_streamoff,
1972 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1973 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1974 .vidioc_g_parm = vidioc_g_parm,
1975 .vidioc_s_parm = vidioc_s_parm,
1976 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1977 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1978 #ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_chip_info = vidioc_g_chip_info,
1980 .vidioc_g_register = vidioc_g_register,
1981 .vidioc_s_register = vidioc_s_register,
1983 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1984 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1987 static const struct video_device gspca_template = {
1988 .name = "gspca main driver",
1990 .ioctl_ops = &dev_ioctl_ops,
1991 .release = video_device_release_empty, /* We use v4l2_dev.release */
1995 * probe and create a new gspca device
1997 * This function must be called by the sub-driver when it is
1998 * called for probing a new device.
2000 int gspca_dev_probe2(struct usb_interface *intf,
2001 const struct usb_device_id *id,
2002 const struct sd_desc *sd_desc,
2004 struct module *module)
2006 struct gspca_dev *gspca_dev;
2007 struct usb_device *dev = interface_to_usbdev(intf);
2010 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2011 sd_desc->name, id->idVendor, id->idProduct);
2013 /* create the device */
2014 if (dev_size < sizeof *gspca_dev)
2015 dev_size = sizeof *gspca_dev;
2016 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2018 pr_err("couldn't kzalloc gspca struct\n");
2021 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2022 if (!gspca_dev->usb_buf) {
2023 pr_err("out of memory\n");
2027 gspca_dev->dev = dev;
2028 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2030 /* check if any audio device */
2031 if (dev->actconfig->desc.bNumInterfaces != 1) {
2033 struct usb_interface *intf2;
2035 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
2036 intf2 = dev->actconfig->interface[i];
2038 && intf2->altsetting != NULL
2039 && intf2->altsetting->desc.bInterfaceClass ==
2041 gspca_dev->audio = 1;
2047 gspca_dev->v4l2_dev.release = gspca_release;
2048 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
2051 gspca_dev->sd_desc = sd_desc;
2052 gspca_dev->nbufread = 2;
2053 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2054 gspca_dev->vdev = gspca_template;
2055 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
2056 video_set_drvdata(&gspca_dev->vdev, gspca_dev);
2057 set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
2058 gspca_dev->module = module;
2059 gspca_dev->present = 1;
2061 mutex_init(&gspca_dev->usb_lock);
2062 gspca_dev->vdev.lock = &gspca_dev->usb_lock;
2063 mutex_init(&gspca_dev->queue_lock);
2064 init_waitqueue_head(&gspca_dev->wq);
2066 /* configure the subdriver and initialize the USB device */
2067 ret = sd_desc->config(gspca_dev, id);
2070 ret = sd_desc->init(gspca_dev);
2073 if (sd_desc->init_controls)
2074 ret = sd_desc->init_controls(gspca_dev);
2077 gspca_set_default_mode(gspca_dev);
2079 ret = gspca_input_connect(gspca_dev);
2084 * Don't take usb_lock for these ioctls. This improves latency if
2085 * usb_lock is taken for a long time, e.g. when changing a control
2086 * value, and a new frame is ready to be dequeued.
2088 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
2089 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
2090 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
2091 #ifdef CONFIG_VIDEO_ADV_DEBUG
2092 if (!gspca_dev->sd_desc->get_register)
2093 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
2094 if (!gspca_dev->sd_desc->set_register)
2095 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
2097 if (!gspca_dev->sd_desc->get_jcomp)
2098 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
2099 if (!gspca_dev->sd_desc->set_jcomp)
2100 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
2102 /* init video stuff */
2103 ret = video_register_device(&gspca_dev->vdev,
2107 pr_err("video_register_device err %d\n", ret);
2111 usb_set_intfdata(intf, gspca_dev);
2112 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2114 gspca_input_create_urb(gspca_dev);
2118 #if IS_ENABLED(CONFIG_INPUT)
2119 if (gspca_dev->input_dev)
2120 input_unregister_device(gspca_dev->input_dev);
2122 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
2123 kfree(gspca_dev->usb_buf);
2127 EXPORT_SYMBOL(gspca_dev_probe2);
2129 /* same function as the previous one, but check the interface */
2130 int gspca_dev_probe(struct usb_interface *intf,
2131 const struct usb_device_id *id,
2132 const struct sd_desc *sd_desc,
2134 struct module *module)
2136 struct usb_device *dev = interface_to_usbdev(intf);
2138 /* we don't handle multi-config cameras */
2139 if (dev->descriptor.bNumConfigurations != 1) {
2140 pr_err("%04x:%04x too many config\n",
2141 id->idVendor, id->idProduct);
2145 /* the USB video interface must be the first one */
2146 if (dev->actconfig->desc.bNumInterfaces != 1
2147 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2150 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2152 EXPORT_SYMBOL(gspca_dev_probe);
2157 * This function must be called by the sub-driver
2158 * when the device disconnects, after the specific resources are freed.
2160 void gspca_disconnect(struct usb_interface *intf)
2162 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2163 #if IS_ENABLED(CONFIG_INPUT)
2164 struct input_dev *input_dev;
2167 PDEBUG(D_PROBE, "%s disconnect",
2168 video_device_node_name(&gspca_dev->vdev));
2170 mutex_lock(&gspca_dev->usb_lock);
2172 gspca_dev->present = 0;
2173 destroy_urbs(gspca_dev);
2175 #if IS_ENABLED(CONFIG_INPUT)
2176 gspca_input_destroy_urb(gspca_dev);
2177 input_dev = gspca_dev->input_dev;
2179 gspca_dev->input_dev = NULL;
2180 input_unregister_device(input_dev);
2183 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2184 if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
2185 gspca_dev->sd_desc->stop0(gspca_dev);
2186 gspca_dev->streaming = 0;
2187 gspca_dev->dev = NULL;
2188 wake_up_interruptible(&gspca_dev->wq);
2190 v4l2_device_disconnect(&gspca_dev->v4l2_dev);
2191 video_unregister_device(&gspca_dev->vdev);
2193 mutex_unlock(&gspca_dev->usb_lock);
2195 /* (this will call gspca_release() immediately or on last close) */
2196 v4l2_device_put(&gspca_dev->v4l2_dev);
2198 EXPORT_SYMBOL(gspca_disconnect);
2201 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2203 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2205 gspca_input_destroy_urb(gspca_dev);
2207 if (!gspca_dev->streaming)
2210 mutex_lock(&gspca_dev->usb_lock);
2211 gspca_dev->frozen = 1; /* avoid urb error messages */
2212 gspca_dev->usb_err = 0;
2213 if (gspca_dev->sd_desc->stopN)
2214 gspca_dev->sd_desc->stopN(gspca_dev);
2215 destroy_urbs(gspca_dev);
2216 gspca_set_alt0(gspca_dev);
2217 if (gspca_dev->sd_desc->stop0)
2218 gspca_dev->sd_desc->stop0(gspca_dev);
2219 mutex_unlock(&gspca_dev->usb_lock);
2223 EXPORT_SYMBOL(gspca_suspend);
2225 int gspca_resume(struct usb_interface *intf)
2227 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2228 int streaming, ret = 0;
2230 mutex_lock(&gspca_dev->usb_lock);
2231 gspca_dev->frozen = 0;
2232 gspca_dev->usb_err = 0;
2233 gspca_dev->sd_desc->init(gspca_dev);
2235 * Most subdrivers send all ctrl values on sd_start and thus
2236 * only write to the device registers on s_ctrl when streaming ->
2237 * Clear streaming to avoid setting all ctrls twice.
2239 streaming = gspca_dev->streaming;
2240 gspca_dev->streaming = 0;
2242 ret = gspca_init_transfer(gspca_dev);
2244 gspca_input_create_urb(gspca_dev);
2245 mutex_unlock(&gspca_dev->usb_lock);
2249 EXPORT_SYMBOL(gspca_resume);
2252 /* -- module insert / remove -- */
2253 static int __init gspca_init(void)
2255 pr_info("v" GSPCA_VERSION " registered\n");
2258 static void __exit gspca_exit(void)
2262 module_init(gspca_init);
2263 module_exit(gspca_exit);
2265 module_param_named(debug, gspca_debug, int, 0644);
2266 MODULE_PARM_DESC(debug,
2267 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");