2 * Main USB camera driver
4 * Copyright (C) 2008-2010 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define MODULE_NAME "gspca"
26 #include <linux/init.h>
27 #include <linux/version.h>
29 #include <linux/vmalloc.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
37 #include <linux/uaccess.h>
38 #include <linux/ktime.h>
39 #include <media/v4l2-ioctl.h>
43 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
44 #include <linux/input.h>
45 #include <linux/usb/input.h>
49 #define DEF_NURBS 3 /* default number of URBs */
50 #if DEF_NURBS > MAX_NURBS
51 #error "DEF_NURBS too big"
54 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0)
61 int gspca_debug = D_ERR | D_PROBE;
62 EXPORT_SYMBOL(gspca_debug);
64 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
71 (pixfmt >> 16) & 0xff,
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102 static void gspca_vm_close(struct vm_area_struct *vma)
104 struct gspca_frame *frame = vma->vm_private_data;
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110 static const struct vm_operations_struct gspca_vm_ops = {
111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb *urb)
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
151 PDEBUG(D_ERR, "Resubmit URB failed with error %i", ret);
155 static int gspca_input_connect(struct gspca_dev *dev)
157 struct input_dev *input_dev;
160 dev->input_dev = NULL;
161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
162 input_dev = input_allocate_device();
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
172 usb_to_input_id(dev->dev, &input_dev->id);
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
178 err = input_register_device(input_dev);
180 PDEBUG(D_ERR, "Input device registration failed "
181 "with error %i", err);
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
185 dev->input_dev = input_dev;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
198 struct usb_device *dev;
202 buffer_len = 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 gspca_dev->int_urb = urb;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
235 usb_free_coherent(dev,
236 urb->transfer_buffer_length,
237 urb->transfer_buffer,
245 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
247 struct usb_interface *intf;
248 struct usb_host_interface *intf_desc;
249 struct usb_endpoint_descriptor *ep;
252 if (gspca_dev->sd_desc->int_pkt_scan) {
253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
254 intf_desc = intf->cur_altsetting;
255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
256 ep = &intf_desc->endpoint[i].desc;
257 if (usb_endpoint_dir_in(ep) &&
258 usb_endpoint_xfer_int(ep)) {
260 alloc_and_submit_int_urb(gspca_dev, ep);
267 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
271 urb = gspca_dev->int_urb;
273 gspca_dev->int_urb = NULL;
275 usb_free_coherent(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
283 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
287 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
291 static inline int gspca_input_connect(struct gspca_dev *dev)
298 * fill a video frame from an URB and resubmit
300 static void fill_frame(struct gspca_dev *gspca_dev,
303 u8 *data; /* address of data in the iso message */
307 if (urb->status != 0) {
308 if (urb->status == -ESHUTDOWN)
309 return; /* disconnection */
311 if (gspca_dev->frozen)
314 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
318 pkt_scan = gspca_dev->sd_desc->pkt_scan;
319 for (i = 0; i < urb->number_of_packets; i++) {
321 /* check the packet status and length */
322 len = urb->iso_frame_desc[i].actual_length;
324 if (gspca_dev->empty_packet == 0)
325 gspca_dev->empty_packet = 1;
328 st = urb->iso_frame_desc[i].status;
331 "ISOC data error: [%d] len=%d, status=%d",
333 gspca_dev->last_packet_type = DISCARD_PACKET;
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
340 data = (u8 *) urb->transfer_buffer
341 + urb->iso_frame_desc[i].offset;
342 pkt_scan(gspca_dev, data, len);
346 /* resubmit the URB */
347 st = usb_submit_urb(urb, GFP_ATOMIC);
349 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
353 * ISOC message interrupt from the USB device
355 * Analyse each packet and call the subdriver for copy to the frame buffer.
357 static void isoc_irq(struct urb *urb)
359 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
361 PDEBUG(D_PACK, "isoc irq");
362 if (!gspca_dev->streaming)
364 fill_frame(gspca_dev, urb);
368 * bulk message interrupt from the USB device
370 static void bulk_irq(struct urb *urb)
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
378 switch (urb->status) {
382 return; /* disconnection */
385 if (gspca_dev->frozen)
388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
395 urb->transfer_buffer,
399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
403 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
408 * add data to the current frame
410 * This function is called by the subdrivers at interrupt level.
412 * To build a frame, these ones must add
414 * - 0 or many INTER_PACKETs
416 * DISCARD_PACKET invalidates the whole frame.
417 * On LAST_PACKET, a new frame is returned.
419 void gspca_frame_add(struct gspca_dev *gspca_dev,
420 enum gspca_packet_type packet_type,
424 struct gspca_frame *frame;
427 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
429 if (packet_type == FIRST_PACKET) {
430 i = atomic_read(&gspca_dev->fr_i);
432 /* if there are no queued buffer, discard the whole frame */
433 if (i == atomic_read(&gspca_dev->fr_q)) {
434 gspca_dev->last_packet_type = DISCARD_PACKET;
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;
443 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
444 if (packet_type == LAST_PACKET)
445 gspca_dev->last_packet_type = packet_type;
449 /* append the packet to the frame buffer */
451 if (gspca_dev->image_len + len > gspca_dev->frsz) {
452 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
453 gspca_dev->image_len + len,
455 packet_type = DISCARD_PACKET;
457 memcpy(gspca_dev->image + gspca_dev->image_len,
459 gspca_dev->image_len += len;
462 gspca_dev->last_packet_type = packet_type;
464 /* if last packet, invalidate packet concatenation until
465 * next first packet, wake up the application and advance
467 if (packet_type == LAST_PACKET) {
468 i = atomic_read(&gspca_dev->fr_i);
469 j = gspca_dev->fr_queue[i];
470 frame = &gspca_dev->frame[j];
471 frame->v4l2_buf.bytesused = gspca_dev->image_len;
472 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
473 | V4L2_BUF_FLAG_DONE)
474 & ~V4L2_BUF_FLAG_QUEUED;
475 i = (i + 1) % GSPCA_MAX_FRAMES;
476 atomic_set(&gspca_dev->fr_i, i);
477 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
478 PDEBUG(D_FRAM, "frame complete len:%d",
479 frame->v4l2_buf.bytesused);
480 gspca_dev->image = NULL;
481 gspca_dev->image_len = 0;
484 EXPORT_SYMBOL(gspca_frame_add);
486 static int gspca_is_compressed(__u32 format)
489 case V4L2_PIX_FMT_MJPEG:
490 case V4L2_PIX_FMT_JPEG:
491 case V4L2_PIX_FMT_SPCA561:
492 case V4L2_PIX_FMT_PAC207:
493 case V4L2_PIX_FMT_MR97310A:
499 static int frame_alloc(struct gspca_dev *gspca_dev,
502 struct gspca_frame *frame;
506 i = gspca_dev->curr_mode;
507 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
508 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
509 frsz = PAGE_ALIGN(frsz);
510 gspca_dev->frsz = 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 err("frame alloc failed");
518 gspca_dev->nframes = count;
519 for (i = 0; i < count; i++) {
520 frame = &gspca_dev->frame[i];
521 frame->v4l2_buf.index = i;
522 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
523 frame->v4l2_buf.flags = 0;
524 frame->v4l2_buf.field = V4L2_FIELD_NONE;
525 frame->v4l2_buf.length = frsz;
526 frame->v4l2_buf.memory = gspca_dev->memory;
527 frame->v4l2_buf.sequence = 0;
528 frame->data = gspca_dev->frbuf + i * frsz;
529 frame->v4l2_buf.m.offset = i * frsz;
531 atomic_set(&gspca_dev->fr_q, 0);
532 atomic_set(&gspca_dev->fr_i, 0);
537 static void frame_free(struct gspca_dev *gspca_dev)
541 PDEBUG(D_STREAM, "frame free");
542 if (gspca_dev->frbuf != NULL) {
543 vfree(gspca_dev->frbuf);
544 gspca_dev->frbuf = NULL;
545 for (i = 0; i < gspca_dev->nframes; i++)
546 gspca_dev->frame[i].data = NULL;
548 gspca_dev->nframes = 0;
551 static void destroy_urbs(struct gspca_dev *gspca_dev)
556 PDEBUG(D_STREAM, "kill transfer");
557 for (i = 0; i < MAX_NURBS; i++) {
558 urb = gspca_dev->urb[i];
562 gspca_dev->urb[i] = NULL;
564 if (urb->transfer_buffer != NULL)
565 usb_free_coherent(gspca_dev->dev,
566 urb->transfer_buffer_length,
567 urb->transfer_buffer,
573 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
577 if (gspca_dev->alt == 0)
579 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
581 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
585 /* Note: both the queue and the usb locks should be held when calling this */
586 static void gspca_stream_off(struct gspca_dev *gspca_dev)
588 gspca_dev->streaming = 0;
589 if (gspca_dev->present) {
590 if (gspca_dev->sd_desc->stopN)
591 gspca_dev->sd_desc->stopN(gspca_dev);
592 destroy_urbs(gspca_dev);
593 gspca_input_destroy_urb(gspca_dev);
594 gspca_set_alt0(gspca_dev);
595 gspca_input_create_urb(gspca_dev);
598 /* always call stop0 to free the subdriver's resources */
599 if (gspca_dev->sd_desc->stop0)
600 gspca_dev->sd_desc->stop0(gspca_dev);
601 PDEBUG(D_STREAM, "stream off OK");
605 * look for an input transfer endpoint in an alternate setting
607 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
610 struct usb_host_endpoint *ep;
613 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
614 ep = &alt->endpoint[i];
615 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
617 && ep->desc.wMaxPacketSize != 0)
624 * look for an input (isoc or bulk) endpoint
626 * The endpoint is defined by the subdriver.
627 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
628 * This routine may be called many times when the bandwidth is too small
629 * (the bandwidth is checked on urb submit).
631 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
633 struct usb_interface *intf;
634 struct usb_host_endpoint *ep;
637 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
639 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
640 : USB_ENDPOINT_XFER_ISOC;
641 i = gspca_dev->alt; /* previous alt setting */
642 if (gspca_dev->cam.reverse_alts) {
643 if (gspca_dev->audio)
645 while (++i < gspca_dev->nbalt) {
646 ep = alt_xfer(&intf->altsetting[i], xfer);
651 if (gspca_dev->audio)
654 ep = alt_xfer(&intf->altsetting[i], xfer);
660 err("no transfer endpoint found");
663 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
664 i, ep->desc.bEndpointAddress);
665 gspca_dev->alt = i; /* memorize the current alt setting */
666 if (gspca_dev->nbalt > 1) {
667 gspca_input_destroy_urb(gspca_dev);
668 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
670 err("set alt %d err %d", i, ret);
673 gspca_input_create_urb(gspca_dev);
679 * create the URBs for image transfer
681 static int create_urbs(struct gspca_dev *gspca_dev,
682 struct usb_host_endpoint *ep)
685 int n, nurbs, i, psize, npkt, bsize;
687 /* calculate the packet size and the number of packets */
688 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
690 if (!gspca_dev->cam.bulk) { /* isoc */
692 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
693 if (gspca_dev->pkt_size == 0)
694 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
696 psize = gspca_dev->pkt_size;
697 npkt = gspca_dev->cam.npkt;
699 npkt = 32; /* default value */
700 bsize = psize * npkt;
702 "isoc %d pkts size %d = bsize:%d",
707 bsize = gspca_dev->cam.bulk_size;
710 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
711 if (gspca_dev->cam.bulk_nurbs != 0)
712 nurbs = gspca_dev->cam.bulk_nurbs;
717 for (n = 0; n < nurbs; n++) {
718 urb = usb_alloc_urb(npkt, GFP_KERNEL);
720 err("usb_alloc_urb failed");
723 gspca_dev->urb[n] = urb;
724 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
729 if (urb->transfer_buffer == NULL) {
730 err("usb_alloc_coherent failed");
733 urb->dev = gspca_dev->dev;
734 urb->context = gspca_dev;
735 urb->transfer_buffer_length = bsize;
736 if (npkt != 0) { /* ISOC */
737 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
738 ep->desc.bEndpointAddress);
739 urb->transfer_flags = URB_ISO_ASAP
740 | URB_NO_TRANSFER_DMA_MAP;
741 urb->interval = ep->desc.bInterval;
742 urb->complete = isoc_irq;
743 urb->number_of_packets = npkt;
744 for (i = 0; i < npkt; i++) {
745 urb->iso_frame_desc[i].length = psize;
746 urb->iso_frame_desc[i].offset = psize * i;
749 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
750 ep->desc.bEndpointAddress),
751 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
752 urb->complete = bulk_irq;
759 * start the USB transfer
761 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
763 struct usb_host_endpoint *ep;
767 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
770 if (!gspca_dev->present) {
775 /* reset the streaming variables */
776 gspca_dev->image = NULL;
777 gspca_dev->image_len = 0;
778 gspca_dev->last_packet_type = DISCARD_PACKET;
779 gspca_dev->sequence = 0;
781 gspca_dev->usb_err = 0;
783 /* set the higher alternate setting and
784 * loop until urb submit succeeds */
785 if (gspca_dev->cam.reverse_alts)
788 gspca_dev->alt = gspca_dev->nbalt;
790 if (gspca_dev->sd_desc->isoc_init) {
791 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
795 ep = get_ep(gspca_dev);
801 if (!gspca_dev->cam.no_urb_create) {
802 PDEBUG(D_STREAM, "init transfer alt %d",
804 ret = create_urbs(gspca_dev, ep);
806 destroy_urbs(gspca_dev);
811 /* clear the bulk endpoint */
812 if (gspca_dev->cam.bulk)
813 usb_clear_halt(gspca_dev->dev,
814 gspca_dev->urb[0]->pipe);
817 ret = gspca_dev->sd_desc->start(gspca_dev);
819 destroy_urbs(gspca_dev);
822 gspca_dev->streaming = 1;
824 /* some bulk transfers are started by the subdriver */
825 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
828 /* submit the URBs */
829 for (n = 0; n < MAX_NURBS; n++) {
830 urb = gspca_dev->urb[n];
833 ret = usb_submit_urb(urb, GFP_KERNEL);
839 gspca_stream_off(gspca_dev);
840 if (ret != -ENOSPC) {
841 PDEBUG(D_ERR|D_STREAM,
842 "usb_submit_urb alt %d err %d",
843 gspca_dev->alt, ret);
847 /* the bandwidth is not wide enough
848 * negociate or try a lower alternate setting */
849 PDEBUG(D_ERR|D_STREAM,
850 "bandwidth not wide enough - trying again");
851 msleep(20); /* wait for kill complete */
852 if (gspca_dev->sd_desc->isoc_nego) {
853 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
857 ep = get_ep(gspca_dev);
865 mutex_unlock(&gspca_dev->usb_lock);
869 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
873 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
874 gspca_dev->curr_mode = i;
875 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
876 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
877 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
880 static int wxh_to_mode(struct gspca_dev *gspca_dev,
881 int width, int height)
885 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
886 if (width >= gspca_dev->cam.cam_mode[i].width
887 && height >= gspca_dev->cam.cam_mode[i].height)
894 * search a mode with the right pixel format
896 static int gspca_get_mode(struct gspca_dev *gspca_dev,
902 modeU = modeD = mode;
903 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
905 if (gspca_dev->cam.cam_mode[modeD].pixelformat
909 if (++modeU < gspca_dev->cam.nmodes) {
910 if (gspca_dev->cam.cam_mode[modeU].pixelformat
918 #ifdef CONFIG_VIDEO_ADV_DEBUG
919 static int vidioc_g_register(struct file *file, void *priv,
920 struct v4l2_dbg_register *reg)
923 struct gspca_dev *gspca_dev = priv;
925 if (!gspca_dev->sd_desc->get_chip_ident)
928 if (!gspca_dev->sd_desc->get_register)
931 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
933 gspca_dev->usb_err = 0;
934 if (gspca_dev->present)
935 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
938 mutex_unlock(&gspca_dev->usb_lock);
943 static int vidioc_s_register(struct file *file, void *priv,
944 struct v4l2_dbg_register *reg)
947 struct gspca_dev *gspca_dev = priv;
949 if (!gspca_dev->sd_desc->get_chip_ident)
952 if (!gspca_dev->sd_desc->set_register)
955 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
957 gspca_dev->usb_err = 0;
958 if (gspca_dev->present)
959 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
962 mutex_unlock(&gspca_dev->usb_lock);
968 static int vidioc_g_chip_ident(struct file *file, void *priv,
969 struct v4l2_dbg_chip_ident *chip)
972 struct gspca_dev *gspca_dev = priv;
974 if (!gspca_dev->sd_desc->get_chip_ident)
977 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
979 gspca_dev->usb_err = 0;
980 if (gspca_dev->present)
981 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
984 mutex_unlock(&gspca_dev->usb_lock);
989 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
990 struct v4l2_fmtdesc *fmtdesc)
992 struct gspca_dev *gspca_dev = priv;
996 /* give an index to each format */
999 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1000 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1003 if (fmt_tb[j] == fmt_tb[index])
1008 if (fmtdesc->index == index)
1009 break; /* new format */
1011 if (index >= ARRAY_SIZE(fmt_tb))
1016 return -EINVAL; /* no more format */
1018 fmtdesc->pixelformat = fmt_tb[index];
1019 if (gspca_is_compressed(fmt_tb[index]))
1020 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1021 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1022 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1023 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1024 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1025 fmtdesc->description[4] = '\0';
1029 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1030 struct v4l2_format *fmt)
1032 struct gspca_dev *gspca_dev = priv;
1035 mode = gspca_dev->curr_mode;
1036 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1037 sizeof fmt->fmt.pix);
1041 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1042 struct v4l2_format *fmt)
1044 int w, h, mode, mode2;
1046 w = fmt->fmt.pix.width;
1047 h = fmt->fmt.pix.height;
1050 if (gspca_debug & D_CONF)
1051 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1053 /* search the closest mode for width and height */
1054 mode = wxh_to_mode(gspca_dev, w, h);
1056 /* OK if right palette */
1057 if (gspca_dev->cam.cam_mode[mode].pixelformat
1058 != fmt->fmt.pix.pixelformat) {
1060 /* else, search the closest mode with the same pixel format */
1061 mode2 = gspca_get_mode(gspca_dev, mode,
1062 fmt->fmt.pix.pixelformat);
1066 ; * no chance, return this mode */
1068 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1069 sizeof fmt->fmt.pix);
1070 return mode; /* used when s_fmt */
1073 static int vidioc_try_fmt_vid_cap(struct file *file,
1075 struct v4l2_format *fmt)
1077 struct gspca_dev *gspca_dev = priv;
1080 ret = try_fmt_vid_cap(gspca_dev, fmt);
1086 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1087 struct v4l2_format *fmt)
1089 struct gspca_dev *gspca_dev = priv;
1092 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1093 return -ERESTARTSYS;
1095 ret = try_fmt_vid_cap(gspca_dev, fmt);
1099 if (gspca_dev->nframes != 0
1100 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1105 if (ret == gspca_dev->curr_mode) {
1107 goto out; /* same mode */
1110 if (gspca_dev->streaming) {
1114 gspca_dev->width = fmt->fmt.pix.width;
1115 gspca_dev->height = fmt->fmt.pix.height;
1116 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1117 gspca_dev->curr_mode = ret;
1121 mutex_unlock(&gspca_dev->queue_lock);
1125 static int vidioc_enum_framesizes(struct file *file, void *priv,
1126 struct v4l2_frmsizeenum *fsize)
1128 struct gspca_dev *gspca_dev = priv;
1132 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1133 if (fsize->pixel_format !=
1134 gspca_dev->cam.cam_mode[i].pixelformat)
1137 if (fsize->index == index) {
1138 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1139 fsize->discrete.width =
1140 gspca_dev->cam.cam_mode[i].width;
1141 fsize->discrete.height =
1142 gspca_dev->cam.cam_mode[i].height;
1151 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1152 struct v4l2_frmivalenum *fival)
1154 struct gspca_dev *gspca_dev = priv;
1155 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1158 if (gspca_dev->cam.mode_framerates == NULL ||
1159 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1162 if (fival->pixel_format !=
1163 gspca_dev->cam.cam_mode[mode].pixelformat)
1166 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1167 if (fival->index == i) {
1168 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1169 fival->discrete.numerator = 1;
1170 fival->discrete.denominator =
1171 gspca_dev->cam.mode_framerates[mode].rates[i];
1179 static void gspca_release(struct video_device *vfd)
1181 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1183 PDEBUG(D_PROBE, "%s released",
1184 video_device_node_name(&gspca_dev->vdev));
1186 kfree(gspca_dev->usb_buf);
1190 static int dev_open(struct file *file)
1192 struct gspca_dev *gspca_dev;
1195 PDEBUG(D_STREAM, "[%s] open", current->comm);
1196 gspca_dev = (struct gspca_dev *) video_devdata(file);
1197 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1198 return -ERESTARTSYS;
1199 if (!gspca_dev->present) {
1204 if (gspca_dev->users > 4) { /* (arbitrary value) */
1209 /* protect the subdriver against rmmod */
1210 if (!try_module_get(gspca_dev->module)) {
1217 file->private_data = gspca_dev;
1219 /* activate the v4l2 debug */
1220 if (gspca_debug & D_V4L2)
1221 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1222 | V4L2_DEBUG_IOCTL_ARG;
1224 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1225 | V4L2_DEBUG_IOCTL_ARG);
1229 mutex_unlock(&gspca_dev->queue_lock);
1231 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1233 PDEBUG(D_STREAM, "open done");
1237 static int dev_close(struct file *file)
1239 struct gspca_dev *gspca_dev = file->private_data;
1241 PDEBUG(D_STREAM, "[%s] close", current->comm);
1242 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1243 return -ERESTARTSYS;
1246 /* if the file did the capture, free the streaming resources */
1247 if (gspca_dev->capt_file == file) {
1248 if (gspca_dev->streaming) {
1249 mutex_lock(&gspca_dev->usb_lock);
1250 gspca_dev->usb_err = 0;
1251 gspca_stream_off(gspca_dev);
1252 mutex_unlock(&gspca_dev->usb_lock);
1254 frame_free(gspca_dev);
1255 gspca_dev->capt_file = NULL;
1256 gspca_dev->memory = GSPCA_MEMORY_NO;
1258 file->private_data = NULL;
1259 module_put(gspca_dev->module);
1260 mutex_unlock(&gspca_dev->queue_lock);
1262 PDEBUG(D_STREAM, "close done");
1267 static int vidioc_querycap(struct file *file, void *priv,
1268 struct v4l2_capability *cap)
1270 struct gspca_dev *gspca_dev = priv;
1273 /* protect the access to the usb device */
1274 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1275 return -ERESTARTSYS;
1276 if (!gspca_dev->present) {
1280 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1281 if (gspca_dev->dev->product != NULL) {
1282 strncpy(cap->card, gspca_dev->dev->product,
1285 snprintf(cap->card, sizeof cap->card,
1286 "USB Camera (%04x:%04x)",
1287 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1288 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1290 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1291 cap->version = DRIVER_VERSION_NUMBER;
1292 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1293 | V4L2_CAP_STREAMING
1294 | V4L2_CAP_READWRITE;
1297 mutex_unlock(&gspca_dev->usb_lock);
1301 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1304 const struct ctrl *ctrls;
1307 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1308 i < gspca_dev->sd_desc->nctrls;
1310 if (gspca_dev->ctrl_dis & (1 << i))
1312 if (id == ctrls->qctrl.id)
1318 static int vidioc_queryctrl(struct file *file, void *priv,
1319 struct v4l2_queryctrl *q_ctrl)
1321 struct gspca_dev *gspca_dev = priv;
1322 const struct ctrl *ctrls;
1328 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1329 id &= V4L2_CTRL_ID_MASK;
1331 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1332 if (gspca_dev->ctrl_dis & (1 << i))
1334 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1336 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1339 ctrls = &gspca_dev->sd_desc->ctrls[i];
1344 ctrls = get_ctrl(gspca_dev, id);
1347 i = ctrls - gspca_dev->sd_desc->ctrls;
1349 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1350 if (gspca_dev->ctrl_inac & (1 << i))
1351 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1355 static int vidioc_s_ctrl(struct file *file, void *priv,
1356 struct v4l2_control *ctrl)
1358 struct gspca_dev *gspca_dev = priv;
1359 const struct ctrl *ctrls;
1362 ctrls = get_ctrl(gspca_dev, ctrl->id);
1366 if (ctrl->value < ctrls->qctrl.minimum
1367 || ctrl->value > ctrls->qctrl.maximum)
1369 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1370 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1371 return -ERESTARTSYS;
1372 gspca_dev->usb_err = 0;
1373 if (gspca_dev->present)
1374 ret = ctrls->set(gspca_dev, ctrl->value);
1377 mutex_unlock(&gspca_dev->usb_lock);
1381 static int vidioc_g_ctrl(struct file *file, void *priv,
1382 struct v4l2_control *ctrl)
1384 struct gspca_dev *gspca_dev = priv;
1385 const struct ctrl *ctrls;
1388 ctrls = get_ctrl(gspca_dev, ctrl->id);
1392 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1393 return -ERESTARTSYS;
1394 gspca_dev->usb_err = 0;
1395 if (gspca_dev->present)
1396 ret = ctrls->get(gspca_dev, &ctrl->value);
1399 mutex_unlock(&gspca_dev->usb_lock);
1403 static int vidioc_querymenu(struct file *file, void *priv,
1404 struct v4l2_querymenu *qmenu)
1406 struct gspca_dev *gspca_dev = priv;
1408 if (!gspca_dev->sd_desc->querymenu)
1410 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1413 static int vidioc_enum_input(struct file *file, void *priv,
1414 struct v4l2_input *input)
1416 struct gspca_dev *gspca_dev = priv;
1418 if (input->index != 0)
1420 input->type = V4L2_INPUT_TYPE_CAMERA;
1421 input->status = gspca_dev->cam.input_flags;
1422 strncpy(input->name, gspca_dev->sd_desc->name,
1423 sizeof input->name);
1427 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1433 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1440 static int vidioc_reqbufs(struct file *file, void *priv,
1441 struct v4l2_requestbuffers *rb)
1443 struct gspca_dev *gspca_dev = priv;
1444 int i, ret = 0, streaming;
1446 i = rb->memory; /* (avoid compilation warning) */
1448 case GSPCA_MEMORY_READ: /* (internal call) */
1449 case V4L2_MEMORY_MMAP:
1450 case V4L2_MEMORY_USERPTR:
1455 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1456 return -ERESTARTSYS;
1458 if (gspca_dev->memory != GSPCA_MEMORY_NO
1459 && gspca_dev->memory != rb->memory) {
1464 /* only one file may do the capture */
1465 if (gspca_dev->capt_file != NULL
1466 && gspca_dev->capt_file != file) {
1471 /* if allocated, the buffers must not be mapped */
1472 for (i = 0; i < gspca_dev->nframes; i++) {
1473 if (gspca_dev->frame[i].vma_use_count) {
1479 /* stop streaming */
1480 streaming = gspca_dev->streaming;
1482 mutex_lock(&gspca_dev->usb_lock);
1483 gspca_dev->usb_err = 0;
1484 gspca_stream_off(gspca_dev);
1485 mutex_unlock(&gspca_dev->usb_lock);
1488 /* free the previous allocated buffers, if any */
1489 if (gspca_dev->nframes != 0) {
1490 frame_free(gspca_dev);
1491 gspca_dev->capt_file = NULL;
1493 if (rb->count == 0) /* unrequest */
1495 gspca_dev->memory = rb->memory;
1496 ret = frame_alloc(gspca_dev, rb->count);
1498 rb->count = gspca_dev->nframes;
1499 gspca_dev->capt_file = file;
1501 ret = gspca_init_transfer(gspca_dev);
1504 mutex_unlock(&gspca_dev->queue_lock);
1505 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1509 static int vidioc_querybuf(struct file *file, void *priv,
1510 struct v4l2_buffer *v4l2_buf)
1512 struct gspca_dev *gspca_dev = priv;
1513 struct gspca_frame *frame;
1515 if (v4l2_buf->index < 0
1516 || v4l2_buf->index >= gspca_dev->nframes)
1519 frame = &gspca_dev->frame[v4l2_buf->index];
1520 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1524 static int vidioc_streamon(struct file *file, void *priv,
1525 enum v4l2_buf_type buf_type)
1527 struct gspca_dev *gspca_dev = priv;
1530 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1532 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1533 return -ERESTARTSYS;
1535 /* check the capture file */
1536 if (gspca_dev->capt_file != file) {
1541 if (gspca_dev->nframes == 0
1542 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1546 if (!gspca_dev->streaming) {
1547 ret = gspca_init_transfer(gspca_dev);
1552 if (gspca_debug & D_STREAM) {
1553 PDEBUG_MODE("stream on OK",
1561 mutex_unlock(&gspca_dev->queue_lock);
1565 static int vidioc_streamoff(struct file *file, void *priv,
1566 enum v4l2_buf_type buf_type)
1568 struct gspca_dev *gspca_dev = priv;
1571 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1573 if (!gspca_dev->streaming)
1575 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1576 return -ERESTARTSYS;
1578 /* check the capture file */
1579 if (gspca_dev->capt_file != file) {
1584 /* stop streaming */
1585 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1589 gspca_dev->usb_err = 0;
1590 gspca_stream_off(gspca_dev);
1591 mutex_unlock(&gspca_dev->usb_lock);
1593 /* empty the transfer queues */
1594 atomic_set(&gspca_dev->fr_q, 0);
1595 atomic_set(&gspca_dev->fr_i, 0);
1596 gspca_dev->fr_o = 0;
1599 mutex_unlock(&gspca_dev->queue_lock);
1603 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1604 struct v4l2_jpegcompression *jpegcomp)
1606 struct gspca_dev *gspca_dev = priv;
1609 if (!gspca_dev->sd_desc->get_jcomp)
1611 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1612 return -ERESTARTSYS;
1613 gspca_dev->usb_err = 0;
1614 if (gspca_dev->present)
1615 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1618 mutex_unlock(&gspca_dev->usb_lock);
1622 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1623 struct v4l2_jpegcompression *jpegcomp)
1625 struct gspca_dev *gspca_dev = priv;
1628 if (!gspca_dev->sd_desc->set_jcomp)
1630 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1631 return -ERESTARTSYS;
1632 gspca_dev->usb_err = 0;
1633 if (gspca_dev->present)
1634 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1637 mutex_unlock(&gspca_dev->usb_lock);
1641 static int vidioc_g_parm(struct file *filp, void *priv,
1642 struct v4l2_streamparm *parm)
1644 struct gspca_dev *gspca_dev = priv;
1646 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1648 if (gspca_dev->sd_desc->get_streamparm) {
1651 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1652 return -ERESTARTSYS;
1653 gspca_dev->usb_err = 0;
1654 if (gspca_dev->present)
1655 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1659 mutex_unlock(&gspca_dev->usb_lock);
1666 static int vidioc_s_parm(struct file *filp, void *priv,
1667 struct v4l2_streamparm *parm)
1669 struct gspca_dev *gspca_dev = priv;
1672 n = parm->parm.capture.readbuffers;
1673 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1674 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1676 gspca_dev->nbufread = n;
1678 if (gspca_dev->sd_desc->set_streamparm) {
1681 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1682 return -ERESTARTSYS;
1683 gspca_dev->usb_err = 0;
1684 if (gspca_dev->present)
1685 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1689 mutex_unlock(&gspca_dev->usb_lock);
1696 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1698 struct gspca_dev *gspca_dev = file->private_data;
1699 struct gspca_frame *frame;
1701 unsigned long addr, start, size;
1704 start = vma->vm_start;
1705 size = vma->vm_end - vma->vm_start;
1706 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1708 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1709 return -ERESTARTSYS;
1710 if (!gspca_dev->present) {
1714 if (gspca_dev->capt_file != file) {
1720 for (i = 0; i < gspca_dev->nframes; ++i) {
1721 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1722 PDEBUG(D_STREAM, "mmap bad memory type");
1725 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1727 frame = &gspca_dev->frame[i];
1731 if (frame == NULL) {
1732 PDEBUG(D_STREAM, "mmap no frame buffer found");
1736 if (size != frame->v4l2_buf.length) {
1737 PDEBUG(D_STREAM, "mmap bad size");
1743 * - VM_IO marks the area as being a mmaped region for I/O to a
1744 * device. It also prevents the region from being core dumped.
1746 vma->vm_flags |= VM_IO;
1748 addr = (unsigned long) frame->data;
1750 page = vmalloc_to_page((void *) addr);
1751 ret = vm_insert_page(vma, start, page);
1759 vma->vm_ops = &gspca_vm_ops;
1760 vma->vm_private_data = frame;
1764 mutex_unlock(&gspca_dev->queue_lock);
1769 * wait for a video frame
1771 * If a frame is ready, its index is returned.
1773 static int frame_wait(struct gspca_dev *gspca_dev,
1778 /* check if a frame is ready */
1779 i = gspca_dev->fr_o;
1780 if (i == atomic_read(&gspca_dev->fr_i)) {
1784 /* wait till a frame is ready */
1785 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1786 i != atomic_read(&gspca_dev->fr_i) ||
1787 !gspca_dev->streaming || !gspca_dev->present,
1788 msecs_to_jiffies(3000));
1791 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1795 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1797 if (gspca_dev->sd_desc->dq_callback) {
1798 mutex_lock(&gspca_dev->usb_lock);
1799 gspca_dev->usb_err = 0;
1800 if (gspca_dev->present)
1801 gspca_dev->sd_desc->dq_callback(gspca_dev);
1802 mutex_unlock(&gspca_dev->usb_lock);
1804 return gspca_dev->fr_queue[i];
1808 * dequeue a video buffer
1810 * If nonblock_ing is false, block until a buffer is available.
1812 static int vidioc_dqbuf(struct file *file, void *priv,
1813 struct v4l2_buffer *v4l2_buf)
1815 struct gspca_dev *gspca_dev = priv;
1816 struct gspca_frame *frame;
1819 PDEBUG(D_FRAM, "dqbuf");
1820 if (v4l2_buf->memory != gspca_dev->memory)
1823 if (!gspca_dev->present)
1826 /* if not streaming, be sure the application will not loop forever */
1827 if (!(file->f_flags & O_NONBLOCK)
1828 && !gspca_dev->streaming && gspca_dev->users == 1)
1831 /* only the capturing file may dequeue */
1832 if (gspca_dev->capt_file != file)
1835 /* only one dequeue / read at a time */
1836 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1837 return -ERESTARTSYS;
1839 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1842 i = ret; /* frame index */
1843 frame = &gspca_dev->frame[i];
1844 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1845 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1847 frame->v4l2_buf.bytesused)) {
1848 PDEBUG(D_ERR|D_STREAM,
1849 "dqbuf cp to user failed");
1854 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1855 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1856 PDEBUG(D_FRAM, "dqbuf %d", i);
1859 mutex_unlock(&gspca_dev->read_lock);
1864 * queue a video buffer
1866 * Attempting to queue a buffer that has already been
1867 * queued will return -EINVAL.
1869 static int vidioc_qbuf(struct file *file, void *priv,
1870 struct v4l2_buffer *v4l2_buf)
1872 struct gspca_dev *gspca_dev = priv;
1873 struct gspca_frame *frame;
1876 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1878 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1879 return -ERESTARTSYS;
1881 index = v4l2_buf->index;
1882 if ((unsigned) index >= gspca_dev->nframes) {
1884 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1888 if (v4l2_buf->memory != gspca_dev->memory) {
1889 PDEBUG(D_FRAM, "qbuf bad memory type");
1894 frame = &gspca_dev->frame[index];
1895 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1896 PDEBUG(D_FRAM, "qbuf bad state");
1901 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1903 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1904 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1905 frame->v4l2_buf.length = v4l2_buf->length;
1908 /* put the buffer in the 'queued' queue */
1909 i = atomic_read(&gspca_dev->fr_q);
1910 gspca_dev->fr_queue[i] = index;
1911 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1913 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1914 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1917 mutex_unlock(&gspca_dev->queue_lock);
1922 * allocate the resources for read()
1924 static int read_alloc(struct gspca_dev *gspca_dev,
1927 struct v4l2_buffer v4l2_buf;
1930 PDEBUG(D_STREAM, "read alloc");
1931 if (gspca_dev->nframes == 0) {
1932 struct v4l2_requestbuffers rb;
1934 memset(&rb, 0, sizeof rb);
1935 rb.count = gspca_dev->nbufread;
1936 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 rb.memory = GSPCA_MEMORY_READ;
1938 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1940 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1943 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1944 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1945 v4l2_buf.memory = GSPCA_MEMORY_READ;
1946 for (i = 0; i < gspca_dev->nbufread; i++) {
1948 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1950 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1954 gspca_dev->memory = GSPCA_MEMORY_READ;
1957 /* start streaming */
1958 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1960 PDEBUG(D_STREAM, "read streamon err %d", ret);
1964 static unsigned int dev_poll(struct file *file, poll_table *wait)
1966 struct gspca_dev *gspca_dev = file->private_data;
1969 PDEBUG(D_FRAM, "poll");
1971 poll_wait(file, &gspca_dev->wq, wait);
1973 /* if reqbufs is not done, the user would use read() */
1974 if (gspca_dev->nframes == 0) {
1975 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1976 return POLLERR; /* not the 1st time */
1977 ret = read_alloc(gspca_dev, file);
1982 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1985 /* check if an image has been received */
1986 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
1987 ret = POLLIN | POLLRDNORM; /* yes */
1990 mutex_unlock(&gspca_dev->queue_lock);
1991 if (!gspca_dev->present)
1996 static ssize_t dev_read(struct file *file, char __user *data,
1997 size_t count, loff_t *ppos)
1999 struct gspca_dev *gspca_dev = file->private_data;
2000 struct gspca_frame *frame;
2001 struct v4l2_buffer v4l2_buf;
2002 struct timeval timestamp;
2005 PDEBUG(D_FRAM, "read (%zd)", count);
2006 if (!gspca_dev->present)
2008 switch (gspca_dev->memory) {
2009 case GSPCA_MEMORY_NO: /* first time */
2010 ret = read_alloc(gspca_dev, file);
2014 case GSPCA_MEMORY_READ:
2015 if (gspca_dev->capt_file == file)
2023 timestamp = ktime_to_timeval(ktime_get());
2027 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2028 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2029 v4l2_buf.memory = GSPCA_MEMORY_READ;
2030 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2032 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2036 /* if the process slept for more than 1 second,
2037 * get a newer frame */
2038 frame = &gspca_dev->frame[v4l2_buf.index];
2040 break; /* avoid infinite loop */
2041 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2043 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2045 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2050 /* copy the frame */
2051 if (count > frame->v4l2_buf.bytesused)
2052 count = frame->v4l2_buf.bytesused;
2053 ret = copy_to_user(data, frame->data, count);
2055 PDEBUG(D_ERR|D_STREAM,
2056 "read cp to user lack %d / %zd", ret, count);
2062 /* in each case, requeue the buffer */
2063 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2069 static struct v4l2_file_operations dev_fops = {
2070 .owner = THIS_MODULE,
2072 .release = dev_close,
2075 .unlocked_ioctl = video_ioctl2,
2079 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2080 .vidioc_querycap = vidioc_querycap,
2081 .vidioc_dqbuf = vidioc_dqbuf,
2082 .vidioc_qbuf = vidioc_qbuf,
2083 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2084 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2085 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2086 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2087 .vidioc_streamon = vidioc_streamon,
2088 .vidioc_queryctrl = vidioc_queryctrl,
2089 .vidioc_g_ctrl = vidioc_g_ctrl,
2090 .vidioc_s_ctrl = vidioc_s_ctrl,
2091 .vidioc_querymenu = vidioc_querymenu,
2092 .vidioc_enum_input = vidioc_enum_input,
2093 .vidioc_g_input = vidioc_g_input,
2094 .vidioc_s_input = vidioc_s_input,
2095 .vidioc_reqbufs = vidioc_reqbufs,
2096 .vidioc_querybuf = vidioc_querybuf,
2097 .vidioc_streamoff = vidioc_streamoff,
2098 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2099 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2100 .vidioc_g_parm = vidioc_g_parm,
2101 .vidioc_s_parm = vidioc_s_parm,
2102 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2103 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2104 #ifdef CONFIG_VIDEO_ADV_DEBUG
2105 .vidioc_g_register = vidioc_g_register,
2106 .vidioc_s_register = vidioc_s_register,
2108 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2111 static struct video_device gspca_template = {
2112 .name = "gspca main driver",
2114 .ioctl_ops = &dev_ioctl_ops,
2115 .release = gspca_release,
2119 * probe and create a new gspca device
2121 * This function must be called by the sub-driver when it is
2122 * called for probing a new device.
2124 int gspca_dev_probe2(struct usb_interface *intf,
2125 const struct usb_device_id *id,
2126 const struct sd_desc *sd_desc,
2128 struct module *module)
2130 struct gspca_dev *gspca_dev;
2131 struct usb_device *dev = interface_to_usbdev(intf);
2134 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2136 /* create the device */
2137 if (dev_size < sizeof *gspca_dev)
2138 dev_size = sizeof *gspca_dev;
2139 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2141 err("couldn't kzalloc gspca struct");
2144 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2145 if (!gspca_dev->usb_buf) {
2146 err("out of memory");
2150 gspca_dev->dev = dev;
2151 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2152 gspca_dev->nbalt = intf->num_altsetting;
2154 /* check if any audio device */
2155 if (dev->config->desc.bNumInterfaces != 1) {
2157 struct usb_interface *intf2;
2159 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2160 intf2 = dev->config->interface[i];
2162 && intf2->altsetting != NULL
2163 && intf2->altsetting->desc.bInterfaceClass ==
2165 gspca_dev->audio = 1;
2171 gspca_dev->sd_desc = sd_desc;
2172 gspca_dev->nbufread = 2;
2173 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2175 /* configure the subdriver and initialize the USB device */
2176 ret = sd_desc->config(gspca_dev, id);
2179 ret = sd_desc->init(gspca_dev);
2182 gspca_set_default_mode(gspca_dev);
2184 ret = gspca_input_connect(gspca_dev);
2188 mutex_init(&gspca_dev->usb_lock);
2189 mutex_init(&gspca_dev->read_lock);
2190 mutex_init(&gspca_dev->queue_lock);
2191 init_waitqueue_head(&gspca_dev->wq);
2193 /* init video stuff */
2194 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2195 gspca_dev->vdev.parent = &intf->dev;
2196 gspca_dev->module = module;
2197 gspca_dev->present = 1;
2198 ret = video_register_device(&gspca_dev->vdev,
2202 err("video_register_device err %d", ret);
2206 usb_set_intfdata(intf, gspca_dev);
2207 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2209 gspca_input_create_urb(gspca_dev);
2213 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2214 if (gspca_dev->input_dev)
2215 input_unregister_device(gspca_dev->input_dev);
2217 kfree(gspca_dev->usb_buf);
2221 EXPORT_SYMBOL(gspca_dev_probe2);
2223 /* same function as the previous one, but check the interface */
2224 int gspca_dev_probe(struct usb_interface *intf,
2225 const struct usb_device_id *id,
2226 const struct sd_desc *sd_desc,
2228 struct module *module)
2230 struct usb_device *dev = interface_to_usbdev(intf);
2232 /* we don't handle multi-config cameras */
2233 if (dev->descriptor.bNumConfigurations != 1) {
2234 PDEBUG(D_ERR, "%04x:%04x too many config",
2235 id->idVendor, id->idProduct);
2239 /* the USB video interface must be the first one */
2240 if (dev->config->desc.bNumInterfaces != 1
2241 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2244 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2246 EXPORT_SYMBOL(gspca_dev_probe);
2251 * This function must be called by the sub-driver
2252 * when the device disconnects, after the specific resources are freed.
2254 void gspca_disconnect(struct usb_interface *intf)
2256 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2257 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2258 struct input_dev *input_dev;
2261 PDEBUG(D_PROBE, "%s disconnect",
2262 video_device_node_name(&gspca_dev->vdev));
2263 mutex_lock(&gspca_dev->usb_lock);
2264 gspca_dev->present = 0;
2266 if (gspca_dev->streaming) {
2267 destroy_urbs(gspca_dev);
2268 wake_up_interruptible(&gspca_dev->wq);
2271 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2272 gspca_input_destroy_urb(gspca_dev);
2273 input_dev = gspca_dev->input_dev;
2275 gspca_dev->input_dev = NULL;
2276 input_unregister_device(input_dev);
2280 /* the device is freed at exit of this function */
2281 gspca_dev->dev = NULL;
2282 mutex_unlock(&gspca_dev->usb_lock);
2284 usb_set_intfdata(intf, NULL);
2286 /* release the device */
2287 /* (this will call gspca_release() immediatly or on last close) */
2288 video_unregister_device(&gspca_dev->vdev);
2290 /* PDEBUG(D_PROBE, "disconnect complete"); */
2292 EXPORT_SYMBOL(gspca_disconnect);
2295 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2297 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2299 if (!gspca_dev->streaming)
2301 gspca_dev->frozen = 1; /* avoid urb error messages */
2302 if (gspca_dev->sd_desc->stopN)
2303 gspca_dev->sd_desc->stopN(gspca_dev);
2304 destroy_urbs(gspca_dev);
2305 gspca_input_destroy_urb(gspca_dev);
2306 gspca_set_alt0(gspca_dev);
2307 if (gspca_dev->sd_desc->stop0)
2308 gspca_dev->sd_desc->stop0(gspca_dev);
2311 EXPORT_SYMBOL(gspca_suspend);
2313 int gspca_resume(struct usb_interface *intf)
2315 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2317 gspca_dev->frozen = 0;
2318 gspca_dev->sd_desc->init(gspca_dev);
2319 gspca_input_create_urb(gspca_dev);
2320 if (gspca_dev->streaming)
2321 return gspca_init_transfer(gspca_dev);
2324 EXPORT_SYMBOL(gspca_resume);
2326 /* -- cam driver utility functions -- */
2328 /* auto gain and exposure algorithm based on the knee algorithm described here:
2329 http://ytse.tricolour.net/docs/LowLightOptimization.html
2331 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2333 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2334 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2336 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2337 const struct ctrl *gain_ctrl = NULL;
2338 const struct ctrl *exposure_ctrl = NULL;
2339 const struct ctrl *autogain_ctrl = NULL;
2342 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2343 if (gspca_dev->ctrl_dis & (1 << i))
2345 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2346 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2347 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2348 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2349 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2350 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2352 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2353 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2354 "on cam without (auto)gain/exposure");
2358 if (gain_ctrl->get(gspca_dev, &gain) ||
2359 exposure_ctrl->get(gspca_dev, &exposure) ||
2360 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2364 orig_exposure = exposure;
2366 /* If we are of a multiple of deadzone, do multiple steps to reach the
2367 desired lumination fast (with the risc of a slight overshoot) */
2368 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2370 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2371 avg_lum, desired_avg_lum, steps);
2373 for (i = 0; i < steps; i++) {
2374 if (avg_lum > desired_avg_lum) {
2375 if (gain > gain_knee)
2377 else if (exposure > exposure_knee)
2379 else if (gain > gain_ctrl->qctrl.default_value)
2381 else if (exposure > exposure_ctrl->qctrl.minimum)
2383 else if (gain > gain_ctrl->qctrl.minimum)
2388 if (gain < gain_ctrl->qctrl.default_value)
2390 else if (exposure < exposure_knee)
2392 else if (gain < gain_knee)
2394 else if (exposure < exposure_ctrl->qctrl.maximum)
2396 else if (gain < gain_ctrl->qctrl.maximum)
2403 if (gain != orig_gain) {
2404 gain_ctrl->set(gspca_dev, gain);
2407 if (exposure != orig_exposure) {
2408 exposure_ctrl->set(gspca_dev, exposure);
2414 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2416 /* -- module insert / remove -- */
2417 static int __init gspca_init(void)
2419 info("main v%d.%d.%d registered",
2420 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2421 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2422 DRIVER_VERSION_NUMBER & 0xff);
2425 static void __exit gspca_exit(void)
2427 info("main deregistered");
2430 module_init(gspca_init);
2431 module_exit(gspca_exit);
2434 module_param_named(debug, gspca_debug, int, 0644);
2435 MODULE_PARM_DESC(debug,
2436 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2437 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"