2 * Main USB camera driver
4 * Copyright (C) 2008-2009 Jean-Francois Moine (http://moinejf.free.fr)
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
24 #include <linux/version.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/pagemap.h>
34 #include <linux/uaccess.h>
35 #include <linux/jiffies.h>
36 #include <media/v4l2-ioctl.h>
41 #define DEF_NURBS 3 /* default number of URBs */
42 #if DEF_NURBS > MAX_NURBS
43 #error "DEF_NURBS too big"
46 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
47 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
48 MODULE_LICENSE("GPL");
50 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 8, 0)
53 int gspca_debug = D_ERR | D_PROBE;
54 EXPORT_SYMBOL(gspca_debug);
56 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
58 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
59 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
63 (pixfmt >> 16) & 0xff,
67 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
74 #define PDEBUG_MODE(txt, pixfmt, w, h)
77 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
78 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79 #define GSPCA_MEMORY_READ 7
81 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
86 static void gspca_vm_open(struct vm_area_struct *vma)
88 struct gspca_frame *frame = vma->vm_private_data;
90 frame->vma_use_count++;
91 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
94 static void gspca_vm_close(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 if (--frame->vma_use_count <= 0)
99 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
102 static const struct vm_operations_struct gspca_vm_ops = {
103 .open = gspca_vm_open,
104 .close = gspca_vm_close,
107 /* get the current input frame buffer */
108 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
110 struct gspca_frame *frame;
112 frame = gspca_dev->cur_frame;
113 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
114 != V4L2_BUF_FLAG_QUEUED)
118 EXPORT_SYMBOL(gspca_get_i_frame);
121 * fill a video frame from an URB and resubmit
123 static void fill_frame(struct gspca_dev *gspca_dev,
126 u8 *data; /* address of data in the iso message */
130 if (urb->status != 0) {
131 if (urb->status == -ESHUTDOWN)
132 return; /* disconnection */
134 if (gspca_dev->frozen)
137 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
141 pkt_scan = gspca_dev->sd_desc->pkt_scan;
142 for (i = 0; i < urb->number_of_packets; i++) {
144 /* check the packet status and length */
145 len = urb->iso_frame_desc[i].actual_length;
147 if (gspca_dev->empty_packet == 0)
148 gspca_dev->empty_packet = 1;
151 st = urb->iso_frame_desc[i].status;
154 "ISOC data error: [%d] len=%d, status=%d",
156 gspca_dev->last_packet_type = DISCARD_PACKET;
160 /* let the packet be analyzed by the subdriver */
161 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
162 i, urb->iso_frame_desc[i].offset, len);
163 data = (u8 *) urb->transfer_buffer
164 + urb->iso_frame_desc[i].offset;
165 pkt_scan(gspca_dev, data, len);
169 /* resubmit the URB */
170 st = usb_submit_urb(urb, GFP_ATOMIC);
172 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
176 * ISOC message interrupt from the USB device
178 * Analyse each packet and call the subdriver for copy to the frame buffer.
180 static void isoc_irq(struct urb *urb)
182 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
184 PDEBUG(D_PACK, "isoc irq");
185 if (!gspca_dev->streaming)
187 fill_frame(gspca_dev, urb);
191 * bulk message interrupt from the USB device
193 static void bulk_irq(struct urb *urb)
195 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
198 PDEBUG(D_PACK, "bulk irq");
199 if (!gspca_dev->streaming)
201 switch (urb->status) {
205 return; /* disconnection */
208 if (gspca_dev->frozen)
211 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
216 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
217 gspca_dev->sd_desc->pkt_scan(gspca_dev,
218 urb->transfer_buffer,
222 /* resubmit the URB */
223 if (gspca_dev->cam.bulk_nurbs != 0) {
224 st = usb_submit_urb(urb, GFP_ATOMIC);
226 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
231 * add data to the current frame
233 * This function is called by the subdrivers at interrupt level.
235 * To build a frame, these ones must add
237 * - 0 or many INTER_PACKETs
239 * DISCARD_PACKET invalidates the whole frame.
240 * On LAST_PACKET, a new frame is returned.
242 void gspca_frame_add(struct gspca_dev *gspca_dev,
243 enum gspca_packet_type packet_type,
247 struct gspca_frame *frame;
250 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
252 /* check the availability of the frame buffer */
253 frame = gspca_dev->cur_frame;
254 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
255 != V4L2_BUF_FLAG_QUEUED) {
256 gspca_dev->last_packet_type = DISCARD_PACKET;
260 /* when start of a new frame, if the current frame buffer
261 * is not queued, discard the whole frame */
262 if (packet_type == FIRST_PACKET) {
263 frame->data_end = frame->data;
264 jiffies_to_timeval(get_jiffies_64(),
265 &frame->v4l2_buf.timestamp);
266 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
267 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
268 if (packet_type == LAST_PACKET)
269 gspca_dev->last_packet_type = packet_type;
273 /* append the packet to the frame buffer */
275 if (frame->data_end - frame->data + len
276 > frame->v4l2_buf.length) {
277 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
278 frame->data_end - frame->data + len,
279 frame->v4l2_buf.length);
280 packet_type = DISCARD_PACKET;
282 memcpy(frame->data_end, data, len);
283 frame->data_end += len;
286 gspca_dev->last_packet_type = packet_type;
288 /* if last packet, wake up the application and advance in the queue */
289 if (packet_type == LAST_PACKET) {
290 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
291 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
292 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
293 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
294 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
296 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
297 frame->v4l2_buf.bytesused,
301 j = gspca_dev->fr_queue[i];
302 gspca_dev->cur_frame = &gspca_dev->frame[j];
305 EXPORT_SYMBOL(gspca_frame_add);
307 static int gspca_is_compressed(__u32 format)
310 case V4L2_PIX_FMT_MJPEG:
311 case V4L2_PIX_FMT_JPEG:
312 case V4L2_PIX_FMT_SPCA561:
313 case V4L2_PIX_FMT_PAC207:
314 case V4L2_PIX_FMT_MR97310A:
320 static void *rvmalloc(long size)
325 mem = vmalloc_32(size);
327 adr = (unsigned long) mem;
329 SetPageReserved(vmalloc_to_page((void *) adr));
337 static void rvfree(void *mem, long size)
341 adr = (unsigned long) mem;
343 ClearPageReserved(vmalloc_to_page((void *) adr));
350 static int frame_alloc(struct gspca_dev *gspca_dev,
353 struct gspca_frame *frame;
357 i = gspca_dev->curr_mode;
358 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
359 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
360 frsz = PAGE_ALIGN(frsz);
361 gspca_dev->frsz = frsz;
362 if (count > GSPCA_MAX_FRAMES)
363 count = GSPCA_MAX_FRAMES;
364 gspca_dev->frbuf = rvmalloc(frsz * count);
365 if (!gspca_dev->frbuf) {
366 err("frame alloc failed");
369 gspca_dev->nframes = count;
370 for (i = 0; i < count; i++) {
371 frame = &gspca_dev->frame[i];
372 frame->v4l2_buf.index = i;
373 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
374 frame->v4l2_buf.flags = 0;
375 frame->v4l2_buf.field = V4L2_FIELD_NONE;
376 frame->v4l2_buf.length = frsz;
377 frame->v4l2_buf.memory = gspca_dev->memory;
378 frame->v4l2_buf.sequence = 0;
379 frame->data = frame->data_end =
380 gspca_dev->frbuf + i * frsz;
381 frame->v4l2_buf.m.offset = i * frsz;
383 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
384 gspca_dev->cur_frame = &gspca_dev->frame[0];
385 gspca_dev->last_packet_type = DISCARD_PACKET;
386 gspca_dev->sequence = 0;
390 static void frame_free(struct gspca_dev *gspca_dev)
394 PDEBUG(D_STREAM, "frame free");
395 if (gspca_dev->frbuf != NULL) {
396 rvfree(gspca_dev->frbuf,
397 gspca_dev->nframes * gspca_dev->frsz);
398 gspca_dev->frbuf = NULL;
399 for (i = 0; i < gspca_dev->nframes; i++)
400 gspca_dev->frame[i].data = NULL;
402 gspca_dev->nframes = 0;
405 static void destroy_urbs(struct gspca_dev *gspca_dev)
410 PDEBUG(D_STREAM, "kill transfer");
411 for (i = 0; i < MAX_NURBS; i++) {
412 urb = gspca_dev->urb[i];
416 gspca_dev->urb[i] = NULL;
418 if (urb->transfer_buffer != NULL)
419 usb_buffer_free(gspca_dev->dev,
420 urb->transfer_buffer_length,
421 urb->transfer_buffer,
428 * look for an input transfer endpoint in an alternate setting
430 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
433 struct usb_host_endpoint *ep;
436 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
437 ep = &alt->endpoint[i];
438 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
440 && ep->desc.wMaxPacketSize != 0)
447 * look for an input (isoc or bulk) endpoint
449 * The endpoint is defined by the subdriver.
450 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
451 * This routine may be called many times when the bandwidth is too small
452 * (the bandwidth is checked on urb submit).
454 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
456 struct usb_interface *intf;
457 struct usb_host_endpoint *ep;
460 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
462 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
463 : USB_ENDPOINT_XFER_ISOC;
464 i = gspca_dev->alt; /* previous alt setting */
465 if (gspca_dev->cam.reverse_alts) {
466 while (++i < gspca_dev->nbalt) {
467 ep = alt_xfer(&intf->altsetting[i], xfer);
473 ep = alt_xfer(&intf->altsetting[i], xfer);
479 err("no transfer endpoint found");
482 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
483 i, ep->desc.bEndpointAddress);
484 gspca_dev->alt = i; /* memorize the current alt setting */
485 if (gspca_dev->nbalt > 1) {
486 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
488 err("set alt %d err %d", i, ret);
496 * create the URBs for image transfer
498 static int create_urbs(struct gspca_dev *gspca_dev,
499 struct usb_host_endpoint *ep)
502 int n, nurbs, i, psize, npkt, bsize;
504 /* calculate the packet size and the number of packets */
505 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
507 if (!gspca_dev->cam.bulk) { /* isoc */
509 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
510 if (gspca_dev->pkt_size == 0)
511 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
513 psize = gspca_dev->pkt_size;
514 npkt = gspca_dev->cam.npkt;
516 npkt = 32; /* default value */
517 bsize = psize * npkt;
519 "isoc %d pkts size %d = bsize:%d",
524 bsize = gspca_dev->cam.bulk_size;
527 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
528 if (gspca_dev->cam.bulk_nurbs != 0)
529 nurbs = gspca_dev->cam.bulk_nurbs;
534 for (n = 0; n < nurbs; n++) {
535 urb = usb_alloc_urb(npkt, GFP_KERNEL);
537 err("usb_alloc_urb failed");
540 gspca_dev->urb[n] = urb;
541 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
546 if (urb->transfer_buffer == NULL) {
547 err("usb_buffer_alloc failed");
550 urb->dev = gspca_dev->dev;
551 urb->context = gspca_dev;
552 urb->transfer_buffer_length = bsize;
553 if (npkt != 0) { /* ISOC */
554 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
555 ep->desc.bEndpointAddress);
556 urb->transfer_flags = URB_ISO_ASAP
557 | URB_NO_TRANSFER_DMA_MAP;
558 urb->interval = ep->desc.bInterval;
559 urb->complete = isoc_irq;
560 urb->number_of_packets = npkt;
561 for (i = 0; i < npkt; i++) {
562 urb->iso_frame_desc[i].length = psize;
563 urb->iso_frame_desc[i].offset = psize * i;
566 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
567 ep->desc.bEndpointAddress),
568 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
569 urb->complete = bulk_irq;
576 * start the USB transfer
578 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
580 struct usb_host_endpoint *ep;
584 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
587 if (!gspca_dev->present) {
592 /* set the higher alternate setting and
593 * loop until urb submit succeeds */
594 if (gspca_dev->cam.reverse_alts)
597 gspca_dev->alt = gspca_dev->nbalt;
599 if (gspca_dev->sd_desc->isoc_init) {
600 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
604 ep = get_ep(gspca_dev);
610 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
611 ret = create_urbs(gspca_dev, ep);
613 destroy_urbs(gspca_dev);
617 /* clear the bulk endpoint */
618 if (gspca_dev->cam.bulk)
619 usb_clear_halt(gspca_dev->dev,
620 gspca_dev->urb[0]->pipe);
623 ret = gspca_dev->sd_desc->start(gspca_dev);
625 destroy_urbs(gspca_dev);
628 gspca_dev->streaming = 1;
630 /* some bulk transfers are started by the subdriver */
631 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
634 /* submit the URBs */
635 for (n = 0; n < MAX_NURBS; n++) {
636 urb = gspca_dev->urb[n];
639 ret = usb_submit_urb(urb, GFP_KERNEL);
645 gspca_dev->streaming = 0;
646 destroy_urbs(gspca_dev);
647 if (ret != -ENOSPC) {
648 PDEBUG(D_ERR|D_STREAM,
649 "usb_submit_urb alt %d err %d",
650 gspca_dev->alt, ret);
654 /* the bandwidth is not wide enough
655 * negociate or try a lower alternate setting */
656 PDEBUG(D_ERR|D_STREAM,
657 "bandwidth not wide enough - trying again");
658 msleep(20); /* wait for kill complete */
659 if (gspca_dev->sd_desc->isoc_nego) {
660 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
664 ep = get_ep(gspca_dev);
672 mutex_unlock(&gspca_dev->usb_lock);
676 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
680 if (gspca_dev->alt == 0)
682 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
684 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
688 /* Note: both the queue and the usb locks should be held when calling this */
689 static void gspca_stream_off(struct gspca_dev *gspca_dev)
691 gspca_dev->streaming = 0;
692 if (gspca_dev->present) {
693 if (gspca_dev->sd_desc->stopN)
694 gspca_dev->sd_desc->stopN(gspca_dev);
695 destroy_urbs(gspca_dev);
696 gspca_set_alt0(gspca_dev);
699 /* always call stop0 to free the subdriver's resources */
700 if (gspca_dev->sd_desc->stop0)
701 gspca_dev->sd_desc->stop0(gspca_dev);
702 PDEBUG(D_STREAM, "stream off OK");
705 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
709 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
710 gspca_dev->curr_mode = i;
711 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
712 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
713 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
716 static int wxh_to_mode(struct gspca_dev *gspca_dev,
717 int width, int height)
721 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
722 if (width >= gspca_dev->cam.cam_mode[i].width
723 && height >= gspca_dev->cam.cam_mode[i].height)
730 * search a mode with the right pixel format
732 static int gspca_get_mode(struct gspca_dev *gspca_dev,
738 modeU = modeD = mode;
739 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
741 if (gspca_dev->cam.cam_mode[modeD].pixelformat
745 if (++modeU < gspca_dev->cam.nmodes) {
746 if (gspca_dev->cam.cam_mode[modeU].pixelformat
754 #ifdef CONFIG_VIDEO_ADV_DEBUG
755 static int vidioc_g_register(struct file *file, void *priv,
756 struct v4l2_dbg_register *reg)
759 struct gspca_dev *gspca_dev = priv;
761 if (!gspca_dev->sd_desc->get_chip_ident)
764 if (!gspca_dev->sd_desc->get_register)
767 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
769 gspca_dev->usb_err = 0;
770 if (gspca_dev->present)
771 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
774 mutex_unlock(&gspca_dev->usb_lock);
779 static int vidioc_s_register(struct file *file, void *priv,
780 struct v4l2_dbg_register *reg)
783 struct gspca_dev *gspca_dev = priv;
785 if (!gspca_dev->sd_desc->get_chip_ident)
788 if (!gspca_dev->sd_desc->set_register)
791 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
793 gspca_dev->usb_err = 0;
794 if (gspca_dev->present)
795 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
798 mutex_unlock(&gspca_dev->usb_lock);
804 static int vidioc_g_chip_ident(struct file *file, void *priv,
805 struct v4l2_dbg_chip_ident *chip)
808 struct gspca_dev *gspca_dev = priv;
810 if (!gspca_dev->sd_desc->get_chip_ident)
813 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
815 gspca_dev->usb_err = 0;
816 if (gspca_dev->present)
817 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
820 mutex_unlock(&gspca_dev->usb_lock);
825 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
826 struct v4l2_fmtdesc *fmtdesc)
828 struct gspca_dev *gspca_dev = priv;
832 /* give an index to each format */
835 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
836 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
839 if (fmt_tb[j] == fmt_tb[index])
844 if (fmtdesc->index == index)
845 break; /* new format */
847 if (index >= ARRAY_SIZE(fmt_tb))
852 return -EINVAL; /* no more format */
854 fmtdesc->pixelformat = fmt_tb[index];
855 if (gspca_is_compressed(fmt_tb[index]))
856 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
857 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
858 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
859 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
860 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
861 fmtdesc->description[4] = '\0';
865 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
866 struct v4l2_format *fmt)
868 struct gspca_dev *gspca_dev = priv;
871 mode = gspca_dev->curr_mode;
872 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
873 sizeof fmt->fmt.pix);
877 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
878 struct v4l2_format *fmt)
880 int w, h, mode, mode2;
882 w = fmt->fmt.pix.width;
883 h = fmt->fmt.pix.height;
886 if (gspca_debug & D_CONF)
887 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
889 /* search the closest mode for width and height */
890 mode = wxh_to_mode(gspca_dev, w, h);
892 /* OK if right palette */
893 if (gspca_dev->cam.cam_mode[mode].pixelformat
894 != fmt->fmt.pix.pixelformat) {
896 /* else, search the closest mode with the same pixel format */
897 mode2 = gspca_get_mode(gspca_dev, mode,
898 fmt->fmt.pix.pixelformat);
902 ; * no chance, return this mode */
904 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
905 sizeof fmt->fmt.pix);
906 return mode; /* used when s_fmt */
909 static int vidioc_try_fmt_vid_cap(struct file *file,
911 struct v4l2_format *fmt)
913 struct gspca_dev *gspca_dev = priv;
916 ret = try_fmt_vid_cap(gspca_dev, fmt);
922 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
923 struct v4l2_format *fmt)
925 struct gspca_dev *gspca_dev = priv;
928 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
931 ret = try_fmt_vid_cap(gspca_dev, fmt);
935 if (gspca_dev->nframes != 0
936 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
941 if (ret == gspca_dev->curr_mode) {
943 goto out; /* same mode */
946 if (gspca_dev->streaming) {
950 gspca_dev->width = fmt->fmt.pix.width;
951 gspca_dev->height = fmt->fmt.pix.height;
952 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
953 gspca_dev->curr_mode = ret;
957 mutex_unlock(&gspca_dev->queue_lock);
961 static int vidioc_enum_framesizes(struct file *file, void *priv,
962 struct v4l2_frmsizeenum *fsize)
964 struct gspca_dev *gspca_dev = priv;
968 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
969 if (fsize->pixel_format !=
970 gspca_dev->cam.cam_mode[i].pixelformat)
973 if (fsize->index == index) {
974 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
975 fsize->discrete.width =
976 gspca_dev->cam.cam_mode[i].width;
977 fsize->discrete.height =
978 gspca_dev->cam.cam_mode[i].height;
987 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
988 struct v4l2_frmivalenum *fival)
990 struct gspca_dev *gspca_dev = priv;
991 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
994 if (gspca_dev->cam.mode_framerates == NULL ||
995 gspca_dev->cam.mode_framerates[mode].nrates == 0)
998 if (fival->pixel_format !=
999 gspca_dev->cam.cam_mode[mode].pixelformat)
1002 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1003 if (fival->index == i) {
1004 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1005 fival->discrete.numerator = 1;
1006 fival->discrete.denominator =
1007 gspca_dev->cam.mode_framerates[mode].rates[i];
1015 static void gspca_release(struct video_device *vfd)
1017 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1019 PDEBUG(D_PROBE, "%s released",
1020 video_device_node_name(&gspca_dev->vdev));
1022 kfree(gspca_dev->usb_buf);
1026 static int dev_open(struct file *file)
1028 struct gspca_dev *gspca_dev;
1031 PDEBUG(D_STREAM, "[%s] open", current->comm);
1032 gspca_dev = (struct gspca_dev *) video_devdata(file);
1033 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1034 return -ERESTARTSYS;
1035 if (!gspca_dev->present) {
1040 if (gspca_dev->users > 4) { /* (arbitrary value) */
1045 /* protect the subdriver against rmmod */
1046 if (!try_module_get(gspca_dev->module)) {
1053 file->private_data = gspca_dev;
1055 /* activate the v4l2 debug */
1056 if (gspca_debug & D_V4L2)
1057 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1058 | V4L2_DEBUG_IOCTL_ARG;
1060 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1061 | V4L2_DEBUG_IOCTL_ARG);
1065 mutex_unlock(&gspca_dev->queue_lock);
1067 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1069 PDEBUG(D_STREAM, "open done");
1073 static int dev_close(struct file *file)
1075 struct gspca_dev *gspca_dev = file->private_data;
1077 PDEBUG(D_STREAM, "[%s] close", current->comm);
1078 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1079 return -ERESTARTSYS;
1082 /* if the file did the capture, free the streaming resources */
1083 if (gspca_dev->capt_file == file) {
1084 if (gspca_dev->streaming) {
1085 mutex_lock(&gspca_dev->usb_lock);
1086 gspca_dev->usb_err = 0;
1087 gspca_stream_off(gspca_dev);
1088 mutex_unlock(&gspca_dev->usb_lock);
1090 frame_free(gspca_dev);
1091 gspca_dev->capt_file = NULL;
1092 gspca_dev->memory = GSPCA_MEMORY_NO;
1094 file->private_data = NULL;
1095 module_put(gspca_dev->module);
1096 mutex_unlock(&gspca_dev->queue_lock);
1098 PDEBUG(D_STREAM, "close done");
1103 static int vidioc_querycap(struct file *file, void *priv,
1104 struct v4l2_capability *cap)
1106 struct gspca_dev *gspca_dev = priv;
1109 /* protect the access to the usb device */
1110 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1111 return -ERESTARTSYS;
1112 if (!gspca_dev->present) {
1116 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1117 if (gspca_dev->dev->product != NULL) {
1118 strncpy(cap->card, gspca_dev->dev->product,
1121 snprintf(cap->card, sizeof cap->card,
1122 "USB Camera (%04x:%04x)",
1123 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1124 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1126 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1127 cap->version = DRIVER_VERSION_NUMBER;
1128 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1129 | V4L2_CAP_STREAMING
1130 | V4L2_CAP_READWRITE;
1133 mutex_unlock(&gspca_dev->usb_lock);
1137 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1140 const struct ctrl *ctrls;
1143 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1144 i < gspca_dev->sd_desc->nctrls;
1146 if (gspca_dev->ctrl_dis & (1 << i))
1148 if (id == ctrls->qctrl.id)
1154 static int vidioc_queryctrl(struct file *file, void *priv,
1155 struct v4l2_queryctrl *q_ctrl)
1157 struct gspca_dev *gspca_dev = priv;
1158 const struct ctrl *ctrls;
1164 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1165 id &= V4L2_CTRL_ID_MASK;
1167 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1168 if (gspca_dev->ctrl_dis & (1 << i))
1170 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1172 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1175 ctrls = &gspca_dev->sd_desc->ctrls[i];
1180 ctrls = get_ctrl(gspca_dev, id);
1183 i = ctrls - gspca_dev->sd_desc->ctrls;
1185 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1186 if (gspca_dev->ctrl_inac & (1 << i))
1187 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1191 static int vidioc_s_ctrl(struct file *file, void *priv,
1192 struct v4l2_control *ctrl)
1194 struct gspca_dev *gspca_dev = priv;
1195 const struct ctrl *ctrls;
1198 ctrls = get_ctrl(gspca_dev, ctrl->id);
1202 if (ctrl->value < ctrls->qctrl.minimum
1203 || ctrl->value > ctrls->qctrl.maximum)
1205 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1206 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1207 return -ERESTARTSYS;
1208 gspca_dev->usb_err = 0;
1209 if (gspca_dev->present)
1210 ret = ctrls->set(gspca_dev, ctrl->value);
1213 mutex_unlock(&gspca_dev->usb_lock);
1217 static int vidioc_g_ctrl(struct file *file, void *priv,
1218 struct v4l2_control *ctrl)
1220 struct gspca_dev *gspca_dev = priv;
1221 const struct ctrl *ctrls;
1224 ctrls = get_ctrl(gspca_dev, ctrl->id);
1228 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1229 return -ERESTARTSYS;
1230 gspca_dev->usb_err = 0;
1231 if (gspca_dev->present)
1232 ret = ctrls->get(gspca_dev, &ctrl->value);
1235 mutex_unlock(&gspca_dev->usb_lock);
1239 /*fixme: have an audio flag in gspca_dev?*/
1240 static int vidioc_s_audio(struct file *file, void *priv,
1241 struct v4l2_audio *audio)
1243 if (audio->index != 0)
1248 static int vidioc_g_audio(struct file *file, void *priv,
1249 struct v4l2_audio *audio)
1251 strcpy(audio->name, "Microphone");
1255 static int vidioc_enumaudio(struct file *file, void *priv,
1256 struct v4l2_audio *audio)
1258 if (audio->index != 0)
1261 strcpy(audio->name, "Microphone");
1262 audio->capability = 0;
1267 static int vidioc_querymenu(struct file *file, void *priv,
1268 struct v4l2_querymenu *qmenu)
1270 struct gspca_dev *gspca_dev = priv;
1272 if (!gspca_dev->sd_desc->querymenu)
1274 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1277 static int vidioc_enum_input(struct file *file, void *priv,
1278 struct v4l2_input *input)
1280 struct gspca_dev *gspca_dev = priv;
1282 if (input->index != 0)
1284 input->type = V4L2_INPUT_TYPE_CAMERA;
1285 input->status = gspca_dev->cam.input_flags;
1286 strncpy(input->name, gspca_dev->sd_desc->name,
1287 sizeof input->name);
1291 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1297 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1304 static int vidioc_reqbufs(struct file *file, void *priv,
1305 struct v4l2_requestbuffers *rb)
1307 struct gspca_dev *gspca_dev = priv;
1310 switch (rb->memory) {
1311 case GSPCA_MEMORY_READ: /* (internal call) */
1312 case V4L2_MEMORY_MMAP:
1313 case V4L2_MEMORY_USERPTR:
1318 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1319 return -ERESTARTSYS;
1321 if (gspca_dev->memory != GSPCA_MEMORY_NO
1322 && gspca_dev->memory != rb->memory) {
1327 /* only one file may do the capture */
1328 if (gspca_dev->capt_file != NULL
1329 && gspca_dev->capt_file != file) {
1334 /* if allocated, the buffers must not be mapped */
1335 for (i = 0; i < gspca_dev->nframes; i++) {
1336 if (gspca_dev->frame[i].vma_use_count) {
1342 /* stop streaming */
1343 if (gspca_dev->streaming) {
1344 mutex_lock(&gspca_dev->usb_lock);
1345 gspca_dev->usb_err = 0;
1346 gspca_stream_off(gspca_dev);
1347 mutex_unlock(&gspca_dev->usb_lock);
1350 /* free the previous allocated buffers, if any */
1351 if (gspca_dev->nframes != 0) {
1352 frame_free(gspca_dev);
1353 gspca_dev->capt_file = NULL;
1355 if (rb->count == 0) /* unrequest */
1357 gspca_dev->memory = rb->memory;
1358 ret = frame_alloc(gspca_dev, rb->count);
1360 rb->count = gspca_dev->nframes;
1361 gspca_dev->capt_file = file;
1364 mutex_unlock(&gspca_dev->queue_lock);
1365 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1369 static int vidioc_querybuf(struct file *file, void *priv,
1370 struct v4l2_buffer *v4l2_buf)
1372 struct gspca_dev *gspca_dev = priv;
1373 struct gspca_frame *frame;
1375 if (v4l2_buf->index < 0
1376 || v4l2_buf->index >= gspca_dev->nframes)
1379 frame = &gspca_dev->frame[v4l2_buf->index];
1380 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1384 static int vidioc_streamon(struct file *file, void *priv,
1385 enum v4l2_buf_type buf_type)
1387 struct gspca_dev *gspca_dev = priv;
1390 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1392 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1393 return -ERESTARTSYS;
1395 if (gspca_dev->nframes == 0
1396 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1400 if (!gspca_dev->streaming) {
1401 ret = gspca_init_transfer(gspca_dev);
1406 if (gspca_debug & D_STREAM) {
1407 PDEBUG_MODE("stream on OK",
1415 mutex_unlock(&gspca_dev->queue_lock);
1419 static int vidioc_streamoff(struct file *file, void *priv,
1420 enum v4l2_buf_type buf_type)
1422 struct gspca_dev *gspca_dev = priv;
1425 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1427 if (!gspca_dev->streaming)
1429 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1430 return -ERESTARTSYS;
1432 /* stop streaming */
1433 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1437 gspca_dev->usb_err = 0;
1438 gspca_stream_off(gspca_dev);
1439 mutex_unlock(&gspca_dev->usb_lock);
1441 /* empty the application queues */
1442 for (i = 0; i < gspca_dev->nframes; i++)
1443 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1444 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1445 gspca_dev->last_packet_type = DISCARD_PACKET;
1446 gspca_dev->sequence = 0;
1449 mutex_unlock(&gspca_dev->queue_lock);
1453 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1454 struct v4l2_jpegcompression *jpegcomp)
1456 struct gspca_dev *gspca_dev = priv;
1459 if (!gspca_dev->sd_desc->get_jcomp)
1461 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1462 return -ERESTARTSYS;
1463 gspca_dev->usb_err = 0;
1464 if (gspca_dev->present)
1465 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1468 mutex_unlock(&gspca_dev->usb_lock);
1472 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1473 struct v4l2_jpegcompression *jpegcomp)
1475 struct gspca_dev *gspca_dev = priv;
1478 if (!gspca_dev->sd_desc->set_jcomp)
1480 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1481 return -ERESTARTSYS;
1482 gspca_dev->usb_err = 0;
1483 if (gspca_dev->present)
1484 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1487 mutex_unlock(&gspca_dev->usb_lock);
1491 static int vidioc_g_parm(struct file *filp, void *priv,
1492 struct v4l2_streamparm *parm)
1494 struct gspca_dev *gspca_dev = priv;
1496 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1498 if (gspca_dev->sd_desc->get_streamparm) {
1501 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1502 return -ERESTARTSYS;
1503 gspca_dev->usb_err = 0;
1504 if (gspca_dev->present)
1505 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1509 mutex_unlock(&gspca_dev->usb_lock);
1516 static int vidioc_s_parm(struct file *filp, void *priv,
1517 struct v4l2_streamparm *parm)
1519 struct gspca_dev *gspca_dev = priv;
1522 n = parm->parm.capture.readbuffers;
1523 if (n == 0 || n > GSPCA_MAX_FRAMES)
1524 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1526 gspca_dev->nbufread = n;
1528 if (gspca_dev->sd_desc->set_streamparm) {
1531 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1532 return -ERESTARTSYS;
1533 gspca_dev->usb_err = 0;
1534 if (gspca_dev->present)
1535 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1539 mutex_unlock(&gspca_dev->usb_lock);
1546 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1547 static int vidiocgmbuf(struct file *file, void *priv,
1548 struct video_mbuf *mbuf)
1550 struct gspca_dev *gspca_dev = file->private_data;
1553 PDEBUG(D_STREAM, "cgmbuf");
1554 if (gspca_dev->nframes == 0) {
1558 struct v4l2_format fmt;
1560 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1561 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1562 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1563 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1564 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1565 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1570 struct v4l2_requestbuffers rb;
1572 memset(&rb, 0, sizeof rb);
1574 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1575 rb.memory = V4L2_MEMORY_MMAP;
1576 ret = vidioc_reqbufs(file, priv, &rb);
1581 mbuf->frames = gspca_dev->nframes;
1582 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1583 for (i = 0; i < mbuf->frames; i++)
1584 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1589 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1591 struct gspca_dev *gspca_dev = file->private_data;
1592 struct gspca_frame *frame;
1594 unsigned long addr, start, size;
1597 start = vma->vm_start;
1598 size = vma->vm_end - vma->vm_start;
1599 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1601 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1602 return -ERESTARTSYS;
1603 if (!gspca_dev->present) {
1607 if (gspca_dev->capt_file != file) {
1613 for (i = 0; i < gspca_dev->nframes; ++i) {
1614 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1615 PDEBUG(D_STREAM, "mmap bad memory type");
1618 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1620 frame = &gspca_dev->frame[i];
1624 if (frame == NULL) {
1625 PDEBUG(D_STREAM, "mmap no frame buffer found");
1629 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1630 /* v4l1 maps all the buffers */
1632 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1634 if (size != frame->v4l2_buf.length) {
1635 PDEBUG(D_STREAM, "mmap bad size");
1641 * - VM_IO marks the area as being a mmaped region for I/O to a
1642 * device. It also prevents the region from being core dumped.
1644 vma->vm_flags |= VM_IO;
1646 addr = (unsigned long) frame->data;
1648 page = vmalloc_to_page((void *) addr);
1649 ret = vm_insert_page(vma, start, page);
1657 vma->vm_ops = &gspca_vm_ops;
1658 vma->vm_private_data = frame;
1662 mutex_unlock(&gspca_dev->queue_lock);
1667 * wait for a video frame
1669 * If a frame is ready, its index is returned.
1671 static int frame_wait(struct gspca_dev *gspca_dev,
1674 struct gspca_frame *frame;
1677 /* check if a frame is ready */
1678 i = gspca_dev->fr_o;
1679 j = gspca_dev->fr_queue[i];
1680 frame = &gspca_dev->frame[j];
1682 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1686 /* wait till a frame is ready */
1687 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1688 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1689 !gspca_dev->streaming || !gspca_dev->present,
1690 msecs_to_jiffies(3000));
1693 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1697 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1698 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1703 if (gspca_dev->sd_desc->dq_callback) {
1704 mutex_lock(&gspca_dev->usb_lock);
1705 gspca_dev->usb_err = 0;
1706 if (gspca_dev->present)
1707 gspca_dev->sd_desc->dq_callback(gspca_dev);
1708 mutex_unlock(&gspca_dev->usb_lock);
1714 * dequeue a video buffer
1716 * If nonblock_ing is false, block until a buffer is available.
1718 static int vidioc_dqbuf(struct file *file, void *priv,
1719 struct v4l2_buffer *v4l2_buf)
1721 struct gspca_dev *gspca_dev = priv;
1722 struct gspca_frame *frame;
1725 PDEBUG(D_FRAM, "dqbuf");
1726 if (v4l2_buf->memory != gspca_dev->memory)
1729 if (!gspca_dev->present)
1732 /* if not streaming, be sure the application will not loop forever */
1733 if (!(file->f_flags & O_NONBLOCK)
1734 && !gspca_dev->streaming && gspca_dev->users == 1)
1737 /* only the capturing file may dequeue */
1738 if (gspca_dev->capt_file != file)
1741 /* only one dequeue / read at a time */
1742 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1743 return -ERESTARTSYS;
1745 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1748 i = ret; /* frame index */
1749 frame = &gspca_dev->frame[i];
1750 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1751 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1753 frame->v4l2_buf.bytesused)) {
1754 PDEBUG(D_ERR|D_STREAM,
1755 "dqbuf cp to user failed");
1760 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1761 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1762 PDEBUG(D_FRAM, "dqbuf %d", i);
1765 mutex_unlock(&gspca_dev->read_lock);
1770 * queue a video buffer
1772 * Attempting to queue a buffer that has already been
1773 * queued will return -EINVAL.
1775 static int vidioc_qbuf(struct file *file, void *priv,
1776 struct v4l2_buffer *v4l2_buf)
1778 struct gspca_dev *gspca_dev = priv;
1779 struct gspca_frame *frame;
1782 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1784 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1785 return -ERESTARTSYS;
1787 index = v4l2_buf->index;
1788 if ((unsigned) index >= gspca_dev->nframes) {
1790 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1794 if (v4l2_buf->memory != gspca_dev->memory) {
1795 PDEBUG(D_FRAM, "qbuf bad memory type");
1800 frame = &gspca_dev->frame[index];
1801 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1802 PDEBUG(D_FRAM, "qbuf bad state");
1807 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1809 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1810 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1811 frame->v4l2_buf.length = v4l2_buf->length;
1814 /* put the buffer in the 'queued' queue */
1815 i = gspca_dev->fr_q;
1816 gspca_dev->fr_queue[i] = index;
1817 if (gspca_dev->fr_i == i)
1818 gspca_dev->cur_frame = frame;
1819 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1820 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1825 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1826 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1829 mutex_unlock(&gspca_dev->queue_lock);
1834 * allocate the resources for read()
1836 static int read_alloc(struct gspca_dev *gspca_dev,
1839 struct v4l2_buffer v4l2_buf;
1842 PDEBUG(D_STREAM, "read alloc");
1843 if (gspca_dev->nframes == 0) {
1844 struct v4l2_requestbuffers rb;
1846 memset(&rb, 0, sizeof rb);
1847 rb.count = gspca_dev->nbufread;
1848 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1849 rb.memory = GSPCA_MEMORY_READ;
1850 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1852 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1855 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1856 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1857 v4l2_buf.memory = GSPCA_MEMORY_READ;
1858 for (i = 0; i < gspca_dev->nbufread; i++) {
1860 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1862 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1866 gspca_dev->memory = GSPCA_MEMORY_READ;
1869 /* start streaming */
1870 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1872 PDEBUG(D_STREAM, "read streamon err %d", ret);
1876 static unsigned int dev_poll(struct file *file, poll_table *wait)
1878 struct gspca_dev *gspca_dev = file->private_data;
1881 PDEBUG(D_FRAM, "poll");
1883 poll_wait(file, &gspca_dev->wq, wait);
1885 /* if reqbufs is not done, the user would use read() */
1886 if (gspca_dev->nframes == 0) {
1887 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1888 return POLLERR; /* not the 1st time */
1889 ret = read_alloc(gspca_dev, file);
1894 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1897 /* check the next incoming buffer */
1898 i = gspca_dev->fr_o;
1899 i = gspca_dev->fr_queue[i];
1900 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1901 ret = POLLIN | POLLRDNORM; /* something to read */
1904 mutex_unlock(&gspca_dev->queue_lock);
1905 if (!gspca_dev->present)
1910 static ssize_t dev_read(struct file *file, char __user *data,
1911 size_t count, loff_t *ppos)
1913 struct gspca_dev *gspca_dev = file->private_data;
1914 struct gspca_frame *frame;
1915 struct v4l2_buffer v4l2_buf;
1916 struct timeval timestamp;
1919 PDEBUG(D_FRAM, "read (%zd)", count);
1920 if (!gspca_dev->present)
1922 switch (gspca_dev->memory) {
1923 case GSPCA_MEMORY_NO: /* first time */
1924 ret = read_alloc(gspca_dev, file);
1928 case GSPCA_MEMORY_READ:
1929 if (gspca_dev->capt_file == file)
1937 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1941 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1942 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1943 v4l2_buf.memory = GSPCA_MEMORY_READ;
1944 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1946 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1950 /* if the process slept for more than 1 second,
1951 * get a newer frame */
1952 frame = &gspca_dev->frame[v4l2_buf.index];
1954 break; /* avoid infinite loop */
1955 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1957 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1959 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1964 /* copy the frame */
1965 if (count > frame->v4l2_buf.bytesused)
1966 count = frame->v4l2_buf.bytesused;
1967 ret = copy_to_user(data, frame->data, count);
1969 PDEBUG(D_ERR|D_STREAM,
1970 "read cp to user lack %d / %zd", ret, count);
1976 /* in each case, requeue the buffer */
1977 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1983 static struct v4l2_file_operations dev_fops = {
1984 .owner = THIS_MODULE,
1986 .release = dev_close,
1989 .unlocked_ioctl = video_ioctl2,
1993 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1994 .vidioc_querycap = vidioc_querycap,
1995 .vidioc_dqbuf = vidioc_dqbuf,
1996 .vidioc_qbuf = vidioc_qbuf,
1997 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1998 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1999 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2000 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2001 .vidioc_streamon = vidioc_streamon,
2002 .vidioc_queryctrl = vidioc_queryctrl,
2003 .vidioc_g_ctrl = vidioc_g_ctrl,
2004 .vidioc_s_ctrl = vidioc_s_ctrl,
2005 .vidioc_g_audio = vidioc_g_audio,
2006 .vidioc_s_audio = vidioc_s_audio,
2007 .vidioc_enumaudio = vidioc_enumaudio,
2008 .vidioc_querymenu = vidioc_querymenu,
2009 .vidioc_enum_input = vidioc_enum_input,
2010 .vidioc_g_input = vidioc_g_input,
2011 .vidioc_s_input = vidioc_s_input,
2012 .vidioc_reqbufs = vidioc_reqbufs,
2013 .vidioc_querybuf = vidioc_querybuf,
2014 .vidioc_streamoff = vidioc_streamoff,
2015 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2016 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2017 .vidioc_g_parm = vidioc_g_parm,
2018 .vidioc_s_parm = vidioc_s_parm,
2019 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2020 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2021 #ifdef CONFIG_VIDEO_ADV_DEBUG
2022 .vidioc_g_register = vidioc_g_register,
2023 .vidioc_s_register = vidioc_s_register,
2025 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2026 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2027 .vidiocgmbuf = vidiocgmbuf,
2031 static struct video_device gspca_template = {
2032 .name = "gspca main driver",
2034 .ioctl_ops = &dev_ioctl_ops,
2035 .release = gspca_release,
2039 * probe and create a new gspca device
2041 * This function must be called by the sub-driver when it is
2042 * called for probing a new device.
2044 int gspca_dev_probe(struct usb_interface *intf,
2045 const struct usb_device_id *id,
2046 const struct sd_desc *sd_desc,
2048 struct module *module)
2050 struct usb_interface_descriptor *interface;
2051 struct gspca_dev *gspca_dev;
2052 struct usb_device *dev = interface_to_usbdev(intf);
2055 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2057 /* we don't handle multi-config cameras */
2058 if (dev->descriptor.bNumConfigurations != 1) {
2059 PDEBUG(D_ERR, "Too many config");
2062 interface = &intf->cur_altsetting->desc;
2063 if (interface->bInterfaceNumber > 0) {
2064 PDEBUG(D_ERR, "intf != 0");
2068 /* create the device */
2069 if (dev_size < sizeof *gspca_dev)
2070 dev_size = sizeof *gspca_dev;
2071 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2073 err("couldn't kzalloc gspca struct");
2076 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2077 if (!gspca_dev->usb_buf) {
2078 err("out of memory");
2082 gspca_dev->dev = dev;
2083 gspca_dev->iface = interface->bInterfaceNumber;
2084 gspca_dev->nbalt = intf->num_altsetting;
2085 gspca_dev->sd_desc = sd_desc;
2086 gspca_dev->nbufread = 2;
2087 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2089 /* configure the subdriver and initialize the USB device */
2090 ret = sd_desc->config(gspca_dev, id);
2093 ret = sd_desc->init(gspca_dev);
2096 gspca_set_default_mode(gspca_dev);
2098 mutex_init(&gspca_dev->usb_lock);
2099 mutex_init(&gspca_dev->read_lock);
2100 mutex_init(&gspca_dev->queue_lock);
2101 init_waitqueue_head(&gspca_dev->wq);
2103 /* init video stuff */
2104 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2105 gspca_dev->vdev.parent = &intf->dev;
2106 gspca_dev->module = module;
2107 gspca_dev->present = 1;
2108 ret = video_register_device(&gspca_dev->vdev,
2112 err("video_register_device err %d", ret);
2116 usb_set_intfdata(intf, gspca_dev);
2117 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2120 kfree(gspca_dev->usb_buf);
2124 EXPORT_SYMBOL(gspca_dev_probe);
2129 * This function must be called by the sub-driver
2130 * when the device disconnects, after the specific resources are freed.
2132 void gspca_disconnect(struct usb_interface *intf)
2134 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2136 PDEBUG(D_PROBE, "%s disconnect",
2137 video_device_node_name(&gspca_dev->vdev));
2138 mutex_lock(&gspca_dev->usb_lock);
2139 gspca_dev->present = 0;
2141 if (gspca_dev->streaming) {
2142 destroy_urbs(gspca_dev);
2143 wake_up_interruptible(&gspca_dev->wq);
2146 /* the device is freed at exit of this function */
2147 gspca_dev->dev = NULL;
2148 mutex_unlock(&gspca_dev->usb_lock);
2150 usb_set_intfdata(intf, NULL);
2152 /* release the device */
2153 /* (this will call gspca_release() immediatly or on last close) */
2154 video_unregister_device(&gspca_dev->vdev);
2156 /* PDEBUG(D_PROBE, "disconnect complete"); */
2158 EXPORT_SYMBOL(gspca_disconnect);
2161 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2163 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2165 if (!gspca_dev->streaming)
2167 gspca_dev->frozen = 1; /* avoid urb error messages */
2168 if (gspca_dev->sd_desc->stopN)
2169 gspca_dev->sd_desc->stopN(gspca_dev);
2170 destroy_urbs(gspca_dev);
2171 gspca_set_alt0(gspca_dev);
2172 if (gspca_dev->sd_desc->stop0)
2173 gspca_dev->sd_desc->stop0(gspca_dev);
2176 EXPORT_SYMBOL(gspca_suspend);
2178 int gspca_resume(struct usb_interface *intf)
2180 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2182 gspca_dev->frozen = 0;
2183 gspca_dev->sd_desc->init(gspca_dev);
2184 if (gspca_dev->streaming)
2185 return gspca_init_transfer(gspca_dev);
2188 EXPORT_SYMBOL(gspca_resume);
2190 /* -- cam driver utility functions -- */
2192 /* auto gain and exposure algorithm based on the knee algorithm described here:
2193 http://ytse.tricolour.net/docs/LowLightOptimization.html
2195 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2197 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2198 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2200 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2201 const struct ctrl *gain_ctrl = NULL;
2202 const struct ctrl *exposure_ctrl = NULL;
2203 const struct ctrl *autogain_ctrl = NULL;
2206 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2207 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2208 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2209 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2210 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2211 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2212 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2214 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2215 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2216 "on cam without (auto)gain/exposure");
2220 if (gain_ctrl->get(gspca_dev, &gain) ||
2221 exposure_ctrl->get(gspca_dev, &exposure) ||
2222 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2226 orig_exposure = exposure;
2228 /* If we are of a multiple of deadzone, do multiple steps to reach the
2229 desired lumination fast (with the risc of a slight overshoot) */
2230 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2232 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2233 avg_lum, desired_avg_lum, steps);
2235 for (i = 0; i < steps; i++) {
2236 if (avg_lum > desired_avg_lum) {
2237 if (gain > gain_knee)
2239 else if (exposure > exposure_knee)
2241 else if (gain > gain_ctrl->qctrl.default_value)
2243 else if (exposure > exposure_ctrl->qctrl.minimum)
2245 else if (gain > gain_ctrl->qctrl.minimum)
2250 if (gain < gain_ctrl->qctrl.default_value)
2252 else if (exposure < exposure_knee)
2254 else if (gain < gain_knee)
2256 else if (exposure < exposure_ctrl->qctrl.maximum)
2258 else if (gain < gain_ctrl->qctrl.maximum)
2265 if (gain != orig_gain) {
2266 gain_ctrl->set(gspca_dev, gain);
2269 if (exposure != orig_exposure) {
2270 exposure_ctrl->set(gspca_dev, exposure);
2276 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2278 /* -- module insert / remove -- */
2279 static int __init gspca_init(void)
2281 info("main v%d.%d.%d registered",
2282 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2283 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2284 DRIVER_VERSION_NUMBER & 0xff);
2287 static void __exit gspca_exit(void)
2289 info("main deregistered");
2292 module_init(gspca_init);
2293 module_exit(gspca_exit);
2296 module_param_named(debug, gspca_debug, int, 0644);
2297 MODULE_PARM_DESC(debug,
2298 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2299 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"