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;
114 i = gspca_dev->fr_queue[i];
115 frame = &gspca_dev->frame[i];
116 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
117 != V4L2_BUF_FLAG_QUEUED)
121 EXPORT_SYMBOL(gspca_get_i_frame);
124 * fill a video frame from an URB and resubmit
126 static void fill_frame(struct gspca_dev *gspca_dev,
129 u8 *data; /* address of data in the iso message */
133 if (urb->status != 0) {
134 if (urb->status == -ESHUTDOWN)
135 return; /* disconnection */
137 if (gspca_dev->frozen)
140 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
144 pkt_scan = gspca_dev->sd_desc->pkt_scan;
145 for (i = 0; i < urb->number_of_packets; i++) {
147 /* check the packet status and length */
148 len = urb->iso_frame_desc[i].actual_length;
150 if (gspca_dev->empty_packet == 0)
151 gspca_dev->empty_packet = 1;
154 st = urb->iso_frame_desc[i].status;
157 "ISOC data error: [%d] len=%d, status=%d",
159 gspca_dev->last_packet_type = DISCARD_PACKET;
163 /* let the packet be analyzed by the subdriver */
164 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
165 i, urb->iso_frame_desc[i].offset, len);
166 data = (u8 *) urb->transfer_buffer
167 + urb->iso_frame_desc[i].offset;
168 pkt_scan(gspca_dev, gspca_dev->cur_frame, data, len);
172 /* resubmit the URB */
173 st = usb_submit_urb(urb, GFP_ATOMIC);
175 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
179 * ISOC message interrupt from the USB device
181 * Analyse each packet and call the subdriver for copy to the frame buffer.
183 static void isoc_irq(struct urb *urb)
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
187 PDEBUG(D_PACK, "isoc irq");
188 if (!gspca_dev->streaming)
190 fill_frame(gspca_dev, urb);
194 * bulk message interrupt from the USB device
196 static void bulk_irq(struct urb *urb)
198 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
201 PDEBUG(D_PACK, "bulk irq");
202 if (!gspca_dev->streaming)
204 switch (urb->status) {
208 return; /* disconnection */
211 if (gspca_dev->frozen)
214 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
219 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
220 gspca_dev->sd_desc->pkt_scan(gspca_dev,
222 urb->transfer_buffer,
226 /* resubmit the URB */
227 if (gspca_dev->cam.bulk_nurbs != 0) {
228 st = usb_submit_urb(urb, GFP_ATOMIC);
230 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
235 * add data to the current frame
237 * This function is called by the subdrivers at interrupt level.
239 * To build a frame, these ones must add
241 * - 0 or many INTER_PACKETs
243 * DISCARD_PACKET invalidates the whole frame.
244 * On LAST_PACKET, a new frame is returned.
246 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
247 enum gspca_packet_type packet_type,
248 struct gspca_frame *dummy,
252 struct gspca_frame *frame;
255 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
257 /* check the availability of the frame buffer */
258 frame = gspca_dev->cur_frame;
259 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
260 != V4L2_BUF_FLAG_QUEUED) {
261 gspca_dev->last_packet_type = DISCARD_PACKET;
265 /* when start of a new frame, if the current frame buffer
266 * is not queued, discard the whole frame */
267 if (packet_type == FIRST_PACKET) {
268 frame->data_end = frame->data;
269 jiffies_to_timeval(get_jiffies_64(),
270 &frame->v4l2_buf.timestamp);
271 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
272 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
273 if (packet_type == LAST_PACKET)
274 gspca_dev->last_packet_type = packet_type;
278 /* append the packet to the frame buffer */
280 if (frame->data_end - frame->data + len
281 > frame->v4l2_buf.length) {
282 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
283 frame->data_end - frame->data + len,
284 frame->v4l2_buf.length);
285 packet_type = DISCARD_PACKET;
287 memcpy(frame->data_end, data, len);
288 frame->data_end += len;
291 gspca_dev->last_packet_type = packet_type;
293 /* if last packet, wake up the application and advance in the queue */
294 if (packet_type == LAST_PACKET) {
295 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
296 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
297 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
298 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
299 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
301 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
302 frame->v4l2_buf.bytesused,
306 j = gspca_dev->fr_queue[i];
307 gspca_dev->cur_frame = frame = &gspca_dev->frame[j];
311 EXPORT_SYMBOL(gspca_frame_add);
313 static int gspca_is_compressed(__u32 format)
316 case V4L2_PIX_FMT_MJPEG:
317 case V4L2_PIX_FMT_JPEG:
318 case V4L2_PIX_FMT_SPCA561:
319 case V4L2_PIX_FMT_PAC207:
320 case V4L2_PIX_FMT_MR97310A:
326 static void *rvmalloc(unsigned long size)
331 mem = vmalloc_32(size);
333 adr = (unsigned long) mem;
334 while ((long) size > 0) {
335 SetPageReserved(vmalloc_to_page((void *) adr));
343 static void rvfree(void *mem, long size)
347 adr = (unsigned long) mem;
349 ClearPageReserved(vmalloc_to_page((void *) adr));
356 static int frame_alloc(struct gspca_dev *gspca_dev,
359 struct gspca_frame *frame;
363 i = gspca_dev->curr_mode;
364 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
365 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
366 frsz = PAGE_ALIGN(frsz);
367 gspca_dev->frsz = frsz;
368 if (count > GSPCA_MAX_FRAMES)
369 count = GSPCA_MAX_FRAMES;
370 gspca_dev->frbuf = rvmalloc(frsz * count);
371 if (!gspca_dev->frbuf) {
372 err("frame alloc failed");
375 gspca_dev->nframes = count;
376 for (i = 0; i < count; i++) {
377 frame = &gspca_dev->frame[i];
378 frame->v4l2_buf.index = i;
379 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
380 frame->v4l2_buf.flags = 0;
381 frame->v4l2_buf.field = V4L2_FIELD_NONE;
382 frame->v4l2_buf.length = frsz;
383 frame->v4l2_buf.memory = gspca_dev->memory;
384 frame->v4l2_buf.sequence = 0;
385 frame->data = frame->data_end =
386 gspca_dev->frbuf + i * frsz;
387 frame->v4l2_buf.m.offset = i * frsz;
389 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
390 gspca_dev->cur_frame = &gspca_dev->frame[0];
391 gspca_dev->last_packet_type = DISCARD_PACKET;
392 gspca_dev->sequence = 0;
396 static void frame_free(struct gspca_dev *gspca_dev)
400 PDEBUG(D_STREAM, "frame free");
401 if (gspca_dev->frbuf != NULL) {
402 rvfree(gspca_dev->frbuf,
403 gspca_dev->nframes * gspca_dev->frsz);
404 gspca_dev->frbuf = NULL;
405 for (i = 0; i < gspca_dev->nframes; i++)
406 gspca_dev->frame[i].data = NULL;
408 gspca_dev->nframes = 0;
411 static void destroy_urbs(struct gspca_dev *gspca_dev)
416 PDEBUG(D_STREAM, "kill transfer");
417 for (i = 0; i < MAX_NURBS; i++) {
418 urb = gspca_dev->urb[i];
422 gspca_dev->urb[i] = NULL;
424 if (urb->transfer_buffer != NULL)
425 usb_buffer_free(gspca_dev->dev,
426 urb->transfer_buffer_length,
427 urb->transfer_buffer,
434 * look for an input transfer endpoint in an alternate setting
436 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
439 struct usb_host_endpoint *ep;
442 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
443 ep = &alt->endpoint[i];
444 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
446 && ep->desc.wMaxPacketSize != 0)
453 * look for an input (isoc or bulk) endpoint
455 * The endpoint is defined by the subdriver.
456 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
457 * This routine may be called many times when the bandwidth is too small
458 * (the bandwidth is checked on urb submit).
460 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
462 struct usb_interface *intf;
463 struct usb_host_endpoint *ep;
466 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
468 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
469 : USB_ENDPOINT_XFER_ISOC;
470 i = gspca_dev->alt; /* previous alt setting */
471 if (gspca_dev->cam.reverse_alts) {
472 while (++i < gspca_dev->nbalt) {
473 ep = alt_xfer(&intf->altsetting[i], xfer);
479 ep = alt_xfer(&intf->altsetting[i], xfer);
485 err("no transfer endpoint found");
488 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
489 i, ep->desc.bEndpointAddress);
490 gspca_dev->alt = i; /* memorize the current alt setting */
491 if (gspca_dev->nbalt > 1) {
492 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
494 err("set alt %d err %d", i, ret);
502 * create the URBs for image transfer
504 static int create_urbs(struct gspca_dev *gspca_dev,
505 struct usb_host_endpoint *ep)
508 int n, nurbs, i, psize, npkt, bsize;
510 /* calculate the packet size and the number of packets */
511 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
513 if (!gspca_dev->cam.bulk) { /* isoc */
515 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
516 if (gspca_dev->pkt_size == 0)
517 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
519 psize = gspca_dev->pkt_size;
520 npkt = gspca_dev->cam.npkt;
522 npkt = 32; /* default value */
523 bsize = psize * npkt;
525 "isoc %d pkts size %d = bsize:%d",
530 bsize = gspca_dev->cam.bulk_size;
533 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
534 if (gspca_dev->cam.bulk_nurbs != 0)
535 nurbs = gspca_dev->cam.bulk_nurbs;
540 gspca_dev->nurbs = nurbs;
541 for (n = 0; n < nurbs; n++) {
542 urb = usb_alloc_urb(npkt, GFP_KERNEL);
544 err("usb_alloc_urb failed");
545 destroy_urbs(gspca_dev);
548 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
553 if (urb->transfer_buffer == NULL) {
555 err("usb_buffer_urb failed");
556 destroy_urbs(gspca_dev);
559 gspca_dev->urb[n] = urb;
560 urb->dev = gspca_dev->dev;
561 urb->context = gspca_dev;
562 urb->transfer_buffer_length = bsize;
563 if (npkt != 0) { /* ISOC */
564 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
565 ep->desc.bEndpointAddress);
566 urb->transfer_flags = URB_ISO_ASAP
567 | URB_NO_TRANSFER_DMA_MAP;
568 urb->interval = ep->desc.bInterval;
569 urb->complete = isoc_irq;
570 urb->number_of_packets = npkt;
571 for (i = 0; i < npkt; i++) {
572 urb->iso_frame_desc[i].length = psize;
573 urb->iso_frame_desc[i].offset = psize * i;
576 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
577 ep->desc.bEndpointAddress),
578 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
579 urb->complete = bulk_irq;
586 * start the USB transfer
588 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
590 struct usb_host_endpoint *ep;
593 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
596 if (!gspca_dev->present) {
601 /* set the higher alternate setting and
602 * loop until urb submit succeeds */
603 if (gspca_dev->cam.reverse_alts)
606 gspca_dev->alt = gspca_dev->nbalt;
608 if (gspca_dev->sd_desc->isoc_init) {
609 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
613 ep = get_ep(gspca_dev);
619 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
620 ret = create_urbs(gspca_dev, ep);
624 /* clear the bulk endpoint */
625 if (gspca_dev->cam.bulk)
626 usb_clear_halt(gspca_dev->dev,
627 gspca_dev->urb[0]->pipe);
630 ret = gspca_dev->sd_desc->start(gspca_dev);
632 destroy_urbs(gspca_dev);
635 gspca_dev->streaming = 1;
637 /* some bulk transfers are started by the subdriver */
638 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
641 /* submit the URBs */
642 for (n = 0; n < gspca_dev->nurbs; n++) {
643 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
649 gspca_dev->streaming = 0;
650 destroy_urbs(gspca_dev);
651 if (ret != -ENOSPC) {
652 PDEBUG(D_ERR|D_STREAM,
653 "usb_submit_urb alt %d err %d",
654 gspca_dev->alt, ret);
658 /* the bandwidth is not wide enough
659 * negociate or try a lower alternate setting */
660 PDEBUG(D_ERR|D_STREAM,
661 "bandwidth not wide enough - trying again");
662 msleep(20); /* wait for kill complete */
663 if (gspca_dev->sd_desc->isoc_nego) {
664 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
668 ep = get_ep(gspca_dev);
676 mutex_unlock(&gspca_dev->usb_lock);
680 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
684 if (gspca_dev->alt == 0)
686 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
688 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
692 /* Note: both the queue and the usb locks should be held when calling this */
693 static void gspca_stream_off(struct gspca_dev *gspca_dev)
695 gspca_dev->streaming = 0;
696 if (gspca_dev->present) {
697 if (gspca_dev->sd_desc->stopN)
698 gspca_dev->sd_desc->stopN(gspca_dev);
699 destroy_urbs(gspca_dev);
700 gspca_set_alt0(gspca_dev);
703 /* always call stop0 to free the subdriver's resources */
704 if (gspca_dev->sd_desc->stop0)
705 gspca_dev->sd_desc->stop0(gspca_dev);
706 PDEBUG(D_STREAM, "stream off OK");
709 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
713 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
714 gspca_dev->curr_mode = i;
715 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
716 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
717 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
720 static int wxh_to_mode(struct gspca_dev *gspca_dev,
721 int width, int height)
725 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
726 if (width >= gspca_dev->cam.cam_mode[i].width
727 && height >= gspca_dev->cam.cam_mode[i].height)
734 * search a mode with the right pixel format
736 static int gspca_get_mode(struct gspca_dev *gspca_dev,
742 modeU = modeD = mode;
743 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
745 if (gspca_dev->cam.cam_mode[modeD].pixelformat
749 if (++modeU < gspca_dev->cam.nmodes) {
750 if (gspca_dev->cam.cam_mode[modeU].pixelformat
758 #ifdef CONFIG_VIDEO_ADV_DEBUG
759 static int vidioc_g_register(struct file *file, void *priv,
760 struct v4l2_dbg_register *reg)
763 struct gspca_dev *gspca_dev = priv;
765 if (!gspca_dev->sd_desc->get_chip_ident)
768 if (!gspca_dev->sd_desc->get_register)
771 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
773 if (gspca_dev->present)
774 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
777 mutex_unlock(&gspca_dev->usb_lock);
782 static int vidioc_s_register(struct file *file, void *priv,
783 struct v4l2_dbg_register *reg)
786 struct gspca_dev *gspca_dev = priv;
788 if (!gspca_dev->sd_desc->get_chip_ident)
791 if (!gspca_dev->sd_desc->set_register)
794 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
796 if (gspca_dev->present)
797 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
800 mutex_unlock(&gspca_dev->usb_lock);
806 static int vidioc_g_chip_ident(struct file *file, void *priv,
807 struct v4l2_dbg_chip_ident *chip)
810 struct gspca_dev *gspca_dev = priv;
812 if (!gspca_dev->sd_desc->get_chip_ident)
815 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
817 if (gspca_dev->present)
818 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
821 mutex_unlock(&gspca_dev->usb_lock);
826 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
827 struct v4l2_fmtdesc *fmtdesc)
829 struct gspca_dev *gspca_dev = priv;
833 /* give an index to each format */
836 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
837 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
840 if (fmt_tb[j] == fmt_tb[index])
845 if (fmtdesc->index == index)
846 break; /* new format */
848 if (index >= ARRAY_SIZE(fmt_tb))
853 return -EINVAL; /* no more format */
855 fmtdesc->pixelformat = fmt_tb[index];
856 if (gspca_is_compressed(fmt_tb[index]))
857 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
858 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
859 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
860 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
861 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
862 fmtdesc->description[4] = '\0';
866 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
867 struct v4l2_format *fmt)
869 struct gspca_dev *gspca_dev = priv;
872 mode = gspca_dev->curr_mode;
873 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
874 sizeof fmt->fmt.pix);
878 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
879 struct v4l2_format *fmt)
881 int w, h, mode, mode2;
883 w = fmt->fmt.pix.width;
884 h = fmt->fmt.pix.height;
887 if (gspca_debug & D_CONF)
888 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
890 /* search the closest mode for width and height */
891 mode = wxh_to_mode(gspca_dev, w, h);
893 /* OK if right palette */
894 if (gspca_dev->cam.cam_mode[mode].pixelformat
895 != fmt->fmt.pix.pixelformat) {
897 /* else, search the closest mode with the same pixel format */
898 mode2 = gspca_get_mode(gspca_dev, mode,
899 fmt->fmt.pix.pixelformat);
903 ; * no chance, return this mode */
905 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
906 sizeof fmt->fmt.pix);
907 return mode; /* used when s_fmt */
910 static int vidioc_try_fmt_vid_cap(struct file *file,
912 struct v4l2_format *fmt)
914 struct gspca_dev *gspca_dev = priv;
917 ret = try_fmt_vid_cap(gspca_dev, fmt);
923 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
924 struct v4l2_format *fmt)
926 struct gspca_dev *gspca_dev = priv;
929 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
932 ret = try_fmt_vid_cap(gspca_dev, fmt);
936 if (gspca_dev->nframes != 0
937 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
942 if (ret == gspca_dev->curr_mode) {
944 goto out; /* same mode */
947 if (gspca_dev->streaming) {
951 gspca_dev->width = fmt->fmt.pix.width;
952 gspca_dev->height = fmt->fmt.pix.height;
953 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
954 gspca_dev->curr_mode = ret;
958 mutex_unlock(&gspca_dev->queue_lock);
962 static int vidioc_enum_framesizes(struct file *file, void *priv,
963 struct v4l2_frmsizeenum *fsize)
965 struct gspca_dev *gspca_dev = priv;
969 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
970 if (fsize->pixel_format !=
971 gspca_dev->cam.cam_mode[i].pixelformat)
974 if (fsize->index == index) {
975 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
976 fsize->discrete.width =
977 gspca_dev->cam.cam_mode[i].width;
978 fsize->discrete.height =
979 gspca_dev->cam.cam_mode[i].height;
988 static void gspca_release(struct video_device *vfd)
990 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
992 PDEBUG(D_PROBE, "/dev/video%d released", gspca_dev->vdev.num);
994 kfree(gspca_dev->usb_buf);
998 static int dev_open(struct file *file)
1000 struct gspca_dev *gspca_dev;
1003 PDEBUG(D_STREAM, "[%s] open", current->comm);
1004 gspca_dev = (struct gspca_dev *) video_devdata(file);
1005 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1006 return -ERESTARTSYS;
1007 if (!gspca_dev->present) {
1012 if (gspca_dev->users > 4) { /* (arbitrary value) */
1017 /* protect the subdriver against rmmod */
1018 if (!try_module_get(gspca_dev->module)) {
1025 file->private_data = gspca_dev;
1027 /* activate the v4l2 debug */
1028 if (gspca_debug & D_V4L2)
1029 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1030 | V4L2_DEBUG_IOCTL_ARG;
1032 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1033 | V4L2_DEBUG_IOCTL_ARG);
1037 mutex_unlock(&gspca_dev->queue_lock);
1039 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1041 PDEBUG(D_STREAM, "open done");
1045 static int dev_close(struct file *file)
1047 struct gspca_dev *gspca_dev = file->private_data;
1049 PDEBUG(D_STREAM, "[%s] close", current->comm);
1050 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1051 return -ERESTARTSYS;
1054 /* if the file did the capture, free the streaming resources */
1055 if (gspca_dev->capt_file == file) {
1056 if (gspca_dev->streaming) {
1057 mutex_lock(&gspca_dev->usb_lock);
1058 gspca_stream_off(gspca_dev);
1059 mutex_unlock(&gspca_dev->usb_lock);
1061 frame_free(gspca_dev);
1062 gspca_dev->capt_file = NULL;
1063 gspca_dev->memory = GSPCA_MEMORY_NO;
1065 file->private_data = NULL;
1066 module_put(gspca_dev->module);
1067 mutex_unlock(&gspca_dev->queue_lock);
1069 PDEBUG(D_STREAM, "close done");
1074 static int vidioc_querycap(struct file *file, void *priv,
1075 struct v4l2_capability *cap)
1077 struct gspca_dev *gspca_dev = priv;
1080 /* protect the access to the usb device */
1081 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1082 return -ERESTARTSYS;
1083 if (!gspca_dev->present) {
1087 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1088 if (gspca_dev->dev->product != NULL) {
1089 strncpy(cap->card, gspca_dev->dev->product,
1092 snprintf(cap->card, sizeof cap->card,
1093 "USB Camera (%04x:%04x)",
1094 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1095 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1097 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1098 cap->version = DRIVER_VERSION_NUMBER;
1099 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1100 | V4L2_CAP_STREAMING
1101 | V4L2_CAP_READWRITE;
1104 mutex_unlock(&gspca_dev->usb_lock);
1108 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1111 const struct ctrl *ctrls;
1114 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1115 i < gspca_dev->sd_desc->nctrls;
1117 if (gspca_dev->ctrl_dis & (1 << i))
1119 if (id == ctrls->qctrl.id)
1125 static int vidioc_queryctrl(struct file *file, void *priv,
1126 struct v4l2_queryctrl *q_ctrl)
1128 struct gspca_dev *gspca_dev = priv;
1129 const struct ctrl *ctrls;
1135 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1136 id &= V4L2_CTRL_ID_MASK;
1138 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1139 if (gspca_dev->ctrl_dis & (1 << i))
1141 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1143 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1146 ctrls = &gspca_dev->sd_desc->ctrls[i];
1149 ctrls = get_ctrl(gspca_dev, id);
1150 i = ctrls - gspca_dev->sd_desc->ctrls;
1154 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1155 if (gspca_dev->ctrl_inac & (1 << i))
1156 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1160 static int vidioc_s_ctrl(struct file *file, void *priv,
1161 struct v4l2_control *ctrl)
1163 struct gspca_dev *gspca_dev = priv;
1164 const struct ctrl *ctrls;
1167 ctrls = get_ctrl(gspca_dev, ctrl->id);
1171 if (ctrl->value < ctrls->qctrl.minimum
1172 || ctrl->value > ctrls->qctrl.maximum)
1174 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1175 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1176 return -ERESTARTSYS;
1177 if (gspca_dev->present)
1178 ret = ctrls->set(gspca_dev, ctrl->value);
1181 mutex_unlock(&gspca_dev->usb_lock);
1185 static int vidioc_g_ctrl(struct file *file, void *priv,
1186 struct v4l2_control *ctrl)
1188 struct gspca_dev *gspca_dev = priv;
1189 const struct ctrl *ctrls;
1192 ctrls = get_ctrl(gspca_dev, ctrl->id);
1196 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1197 return -ERESTARTSYS;
1198 if (gspca_dev->present)
1199 ret = ctrls->get(gspca_dev, &ctrl->value);
1202 mutex_unlock(&gspca_dev->usb_lock);
1206 /*fixme: have an audio flag in gspca_dev?*/
1207 static int vidioc_s_audio(struct file *file, void *priv,
1208 struct v4l2_audio *audio)
1210 if (audio->index != 0)
1215 static int vidioc_g_audio(struct file *file, void *priv,
1216 struct v4l2_audio *audio)
1218 strcpy(audio->name, "Microphone");
1222 static int vidioc_enumaudio(struct file *file, void *priv,
1223 struct v4l2_audio *audio)
1225 if (audio->index != 0)
1228 strcpy(audio->name, "Microphone");
1229 audio->capability = 0;
1234 static int vidioc_querymenu(struct file *file, void *priv,
1235 struct v4l2_querymenu *qmenu)
1237 struct gspca_dev *gspca_dev = priv;
1239 if (!gspca_dev->sd_desc->querymenu)
1241 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1244 static int vidioc_enum_input(struct file *file, void *priv,
1245 struct v4l2_input *input)
1247 struct gspca_dev *gspca_dev = priv;
1249 if (input->index != 0)
1251 input->type = V4L2_INPUT_TYPE_CAMERA;
1252 input->status = gspca_dev->cam.input_flags;
1253 strncpy(input->name, gspca_dev->sd_desc->name,
1254 sizeof input->name);
1258 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1264 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1271 static int vidioc_reqbufs(struct file *file, void *priv,
1272 struct v4l2_requestbuffers *rb)
1274 struct gspca_dev *gspca_dev = priv;
1277 switch (rb->memory) {
1278 case GSPCA_MEMORY_READ: /* (internal call) */
1279 case V4L2_MEMORY_MMAP:
1280 case V4L2_MEMORY_USERPTR:
1285 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1286 return -ERESTARTSYS;
1288 if (gspca_dev->memory != GSPCA_MEMORY_NO
1289 && gspca_dev->memory != rb->memory) {
1294 /* only one file may do the capture */
1295 if (gspca_dev->capt_file != NULL
1296 && gspca_dev->capt_file != file) {
1301 /* if allocated, the buffers must not be mapped */
1302 for (i = 0; i < gspca_dev->nframes; i++) {
1303 if (gspca_dev->frame[i].vma_use_count) {
1309 /* stop streaming */
1310 if (gspca_dev->streaming) {
1311 mutex_lock(&gspca_dev->usb_lock);
1312 gspca_stream_off(gspca_dev);
1313 mutex_unlock(&gspca_dev->usb_lock);
1316 /* free the previous allocated buffers, if any */
1317 if (gspca_dev->nframes != 0) {
1318 frame_free(gspca_dev);
1319 gspca_dev->capt_file = NULL;
1321 if (rb->count == 0) /* unrequest */
1323 gspca_dev->memory = rb->memory;
1324 ret = frame_alloc(gspca_dev, rb->count);
1326 rb->count = gspca_dev->nframes;
1327 gspca_dev->capt_file = file;
1330 mutex_unlock(&gspca_dev->queue_lock);
1331 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1335 static int vidioc_querybuf(struct file *file, void *priv,
1336 struct v4l2_buffer *v4l2_buf)
1338 struct gspca_dev *gspca_dev = priv;
1339 struct gspca_frame *frame;
1341 if (v4l2_buf->index < 0
1342 || v4l2_buf->index >= gspca_dev->nframes)
1345 frame = &gspca_dev->frame[v4l2_buf->index];
1346 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1350 static int vidioc_streamon(struct file *file, void *priv,
1351 enum v4l2_buf_type buf_type)
1353 struct gspca_dev *gspca_dev = priv;
1356 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1358 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1359 return -ERESTARTSYS;
1361 if (gspca_dev->nframes == 0
1362 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1366 if (!gspca_dev->streaming) {
1367 ret = gspca_init_transfer(gspca_dev);
1372 if (gspca_debug & D_STREAM) {
1373 PDEBUG_MODE("stream on OK",
1381 mutex_unlock(&gspca_dev->queue_lock);
1385 static int vidioc_streamoff(struct file *file, void *priv,
1386 enum v4l2_buf_type buf_type)
1388 struct gspca_dev *gspca_dev = priv;
1391 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1393 if (!gspca_dev->streaming)
1395 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1396 return -ERESTARTSYS;
1398 /* stop streaming */
1399 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1403 gspca_stream_off(gspca_dev);
1404 mutex_unlock(&gspca_dev->usb_lock);
1406 /* empty the application queues */
1407 for (i = 0; i < gspca_dev->nframes; i++)
1408 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1409 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1410 gspca_dev->last_packet_type = DISCARD_PACKET;
1411 gspca_dev->sequence = 0;
1414 mutex_unlock(&gspca_dev->queue_lock);
1418 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1419 struct v4l2_jpegcompression *jpegcomp)
1421 struct gspca_dev *gspca_dev = priv;
1424 if (!gspca_dev->sd_desc->get_jcomp)
1426 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1427 return -ERESTARTSYS;
1428 if (gspca_dev->present)
1429 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1432 mutex_unlock(&gspca_dev->usb_lock);
1436 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1437 struct v4l2_jpegcompression *jpegcomp)
1439 struct gspca_dev *gspca_dev = priv;
1442 if (!gspca_dev->sd_desc->set_jcomp)
1444 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1445 return -ERESTARTSYS;
1446 if (gspca_dev->present)
1447 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1450 mutex_unlock(&gspca_dev->usb_lock);
1454 static int vidioc_g_parm(struct file *filp, void *priv,
1455 struct v4l2_streamparm *parm)
1457 struct gspca_dev *gspca_dev = priv;
1459 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1461 if (gspca_dev->sd_desc->get_streamparm) {
1464 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1465 return -ERESTARTSYS;
1466 if (gspca_dev->present)
1467 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1471 mutex_unlock(&gspca_dev->usb_lock);
1478 static int vidioc_s_parm(struct file *filp, void *priv,
1479 struct v4l2_streamparm *parm)
1481 struct gspca_dev *gspca_dev = priv;
1484 n = parm->parm.capture.readbuffers;
1485 if (n == 0 || n > GSPCA_MAX_FRAMES)
1486 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1488 gspca_dev->nbufread = n;
1490 if (gspca_dev->sd_desc->set_streamparm) {
1493 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1494 return -ERESTARTSYS;
1495 if (gspca_dev->present)
1496 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1500 mutex_unlock(&gspca_dev->usb_lock);
1507 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1508 static int vidiocgmbuf(struct file *file, void *priv,
1509 struct video_mbuf *mbuf)
1511 struct gspca_dev *gspca_dev = file->private_data;
1514 PDEBUG(D_STREAM, "cgmbuf");
1515 if (gspca_dev->nframes == 0) {
1519 struct v4l2_format fmt;
1521 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1522 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1523 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1524 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1525 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1526 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1531 struct v4l2_requestbuffers rb;
1533 memset(&rb, 0, sizeof rb);
1535 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1536 rb.memory = V4L2_MEMORY_MMAP;
1537 ret = vidioc_reqbufs(file, priv, &rb);
1542 mbuf->frames = gspca_dev->nframes;
1543 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1544 for (i = 0; i < mbuf->frames; i++)
1545 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1550 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1552 struct gspca_dev *gspca_dev = file->private_data;
1553 struct gspca_frame *frame;
1555 unsigned long addr, start, size;
1558 start = vma->vm_start;
1559 size = vma->vm_end - vma->vm_start;
1560 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1562 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1563 return -ERESTARTSYS;
1564 if (!gspca_dev->present) {
1568 if (gspca_dev->capt_file != file) {
1574 for (i = 0; i < gspca_dev->nframes; ++i) {
1575 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1576 PDEBUG(D_STREAM, "mmap bad memory type");
1579 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1581 frame = &gspca_dev->frame[i];
1585 if (frame == NULL) {
1586 PDEBUG(D_STREAM, "mmap no frame buffer found");
1590 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1591 /* v4l1 maps all the buffers */
1593 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1595 if (size != frame->v4l2_buf.length) {
1596 PDEBUG(D_STREAM, "mmap bad size");
1602 * - VM_IO marks the area as being a mmaped region for I/O to a
1603 * device. It also prevents the region from being core dumped.
1605 vma->vm_flags |= VM_IO;
1607 addr = (unsigned long) frame->data;
1609 page = vmalloc_to_page((void *) addr);
1610 ret = vm_insert_page(vma, start, page);
1618 vma->vm_ops = (struct vm_operations_struct *) &gspca_vm_ops;
1619 vma->vm_private_data = frame;
1623 mutex_unlock(&gspca_dev->queue_lock);
1628 * wait for a video frame
1630 * If a frame is ready, its index is returned.
1632 static int frame_wait(struct gspca_dev *gspca_dev,
1635 struct gspca_frame *frame;
1638 /* check if a frame is ready */
1639 i = gspca_dev->fr_o;
1640 j = gspca_dev->fr_queue[i];
1641 frame = &gspca_dev->frame[j];
1643 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1647 /* wait till a frame is ready */
1648 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1649 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1650 !gspca_dev->streaming || !gspca_dev->present,
1651 msecs_to_jiffies(3000));
1654 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1658 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1659 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1664 if (gspca_dev->sd_desc->dq_callback) {
1665 mutex_lock(&gspca_dev->usb_lock);
1666 if (gspca_dev->present)
1667 gspca_dev->sd_desc->dq_callback(gspca_dev);
1668 mutex_unlock(&gspca_dev->usb_lock);
1674 * dequeue a video buffer
1676 * If nonblock_ing is false, block until a buffer is available.
1678 static int vidioc_dqbuf(struct file *file, void *priv,
1679 struct v4l2_buffer *v4l2_buf)
1681 struct gspca_dev *gspca_dev = priv;
1682 struct gspca_frame *frame;
1685 PDEBUG(D_FRAM, "dqbuf");
1686 if (v4l2_buf->memory != gspca_dev->memory)
1689 if (!gspca_dev->present)
1692 /* if not streaming, be sure the application will not loop forever */
1693 if (!(file->f_flags & O_NONBLOCK)
1694 && !gspca_dev->streaming && gspca_dev->users == 1)
1697 /* only the capturing file may dequeue */
1698 if (gspca_dev->capt_file != file)
1701 /* only one dequeue / read at a time */
1702 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1703 return -ERESTARTSYS;
1705 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1708 i = ret; /* frame index */
1709 frame = &gspca_dev->frame[i];
1710 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1711 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1713 frame->v4l2_buf.bytesused)) {
1714 PDEBUG(D_ERR|D_STREAM,
1715 "dqbuf cp to user failed");
1720 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1721 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1722 PDEBUG(D_FRAM, "dqbuf %d", i);
1725 mutex_unlock(&gspca_dev->read_lock);
1730 * queue a video buffer
1732 * Attempting to queue a buffer that has already been
1733 * queued will return -EINVAL.
1735 static int vidioc_qbuf(struct file *file, void *priv,
1736 struct v4l2_buffer *v4l2_buf)
1738 struct gspca_dev *gspca_dev = priv;
1739 struct gspca_frame *frame;
1742 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1744 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1745 return -ERESTARTSYS;
1747 index = v4l2_buf->index;
1748 if ((unsigned) index >= gspca_dev->nframes) {
1750 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1754 if (v4l2_buf->memory != gspca_dev->memory) {
1755 PDEBUG(D_FRAM, "qbuf bad memory type");
1760 frame = &gspca_dev->frame[index];
1761 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1762 PDEBUG(D_FRAM, "qbuf bad state");
1767 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1769 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1770 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1771 frame->v4l2_buf.length = v4l2_buf->length;
1774 /* put the buffer in the 'queued' queue */
1775 i = gspca_dev->fr_q;
1776 gspca_dev->fr_queue[i] = index;
1777 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1778 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1783 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1784 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1787 mutex_unlock(&gspca_dev->queue_lock);
1792 * allocate the resources for read()
1794 static int read_alloc(struct gspca_dev *gspca_dev,
1797 struct v4l2_buffer v4l2_buf;
1800 PDEBUG(D_STREAM, "read alloc");
1801 if (gspca_dev->nframes == 0) {
1802 struct v4l2_requestbuffers rb;
1804 memset(&rb, 0, sizeof rb);
1805 rb.count = gspca_dev->nbufread;
1806 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1807 rb.memory = GSPCA_MEMORY_READ;
1808 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1810 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1813 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1814 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1815 v4l2_buf.memory = GSPCA_MEMORY_READ;
1816 for (i = 0; i < gspca_dev->nbufread; i++) {
1818 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1820 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1824 gspca_dev->memory = GSPCA_MEMORY_READ;
1827 /* start streaming */
1828 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1830 PDEBUG(D_STREAM, "read streamon err %d", ret);
1834 static unsigned int dev_poll(struct file *file, poll_table *wait)
1836 struct gspca_dev *gspca_dev = file->private_data;
1839 PDEBUG(D_FRAM, "poll");
1841 poll_wait(file, &gspca_dev->wq, wait);
1843 /* if reqbufs is not done, the user would use read() */
1844 if (gspca_dev->nframes == 0) {
1845 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1846 return POLLERR; /* not the 1st time */
1847 ret = read_alloc(gspca_dev, file);
1852 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1855 /* check the next incoming buffer */
1856 i = gspca_dev->fr_o;
1857 i = gspca_dev->fr_queue[i];
1858 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1859 ret = POLLIN | POLLRDNORM; /* something to read */
1862 mutex_unlock(&gspca_dev->queue_lock);
1863 if (!gspca_dev->present)
1868 static ssize_t dev_read(struct file *file, char __user *data,
1869 size_t count, loff_t *ppos)
1871 struct gspca_dev *gspca_dev = file->private_data;
1872 struct gspca_frame *frame;
1873 struct v4l2_buffer v4l2_buf;
1874 struct timeval timestamp;
1877 PDEBUG(D_FRAM, "read (%zd)", count);
1878 if (!gspca_dev->present)
1880 switch (gspca_dev->memory) {
1881 case GSPCA_MEMORY_NO: /* first time */
1882 ret = read_alloc(gspca_dev, file);
1886 case GSPCA_MEMORY_READ:
1887 if (gspca_dev->capt_file == file)
1895 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1899 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1900 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1901 v4l2_buf.memory = GSPCA_MEMORY_READ;
1902 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1904 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1908 /* if the process slept for more than 1 second,
1909 * get a newer frame */
1910 frame = &gspca_dev->frame[v4l2_buf.index];
1912 break; /* avoid infinite loop */
1913 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1915 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1917 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1922 /* copy the frame */
1923 if (count > frame->v4l2_buf.bytesused)
1924 count = frame->v4l2_buf.bytesused;
1925 ret = copy_to_user(data, frame->data, count);
1927 PDEBUG(D_ERR|D_STREAM,
1928 "read cp to user lack %d / %zd", ret, count);
1934 /* in each case, requeue the buffer */
1935 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1941 static struct v4l2_file_operations dev_fops = {
1942 .owner = THIS_MODULE,
1944 .release = dev_close,
1947 .unlocked_ioctl = video_ioctl2,
1951 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1952 .vidioc_querycap = vidioc_querycap,
1953 .vidioc_dqbuf = vidioc_dqbuf,
1954 .vidioc_qbuf = vidioc_qbuf,
1955 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1956 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1957 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1958 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1959 .vidioc_streamon = vidioc_streamon,
1960 .vidioc_queryctrl = vidioc_queryctrl,
1961 .vidioc_g_ctrl = vidioc_g_ctrl,
1962 .vidioc_s_ctrl = vidioc_s_ctrl,
1963 .vidioc_g_audio = vidioc_g_audio,
1964 .vidioc_s_audio = vidioc_s_audio,
1965 .vidioc_enumaudio = vidioc_enumaudio,
1966 .vidioc_querymenu = vidioc_querymenu,
1967 .vidioc_enum_input = vidioc_enum_input,
1968 .vidioc_g_input = vidioc_g_input,
1969 .vidioc_s_input = vidioc_s_input,
1970 .vidioc_reqbufs = vidioc_reqbufs,
1971 .vidioc_querybuf = vidioc_querybuf,
1972 .vidioc_streamoff = vidioc_streamoff,
1973 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1974 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1975 .vidioc_g_parm = vidioc_g_parm,
1976 .vidioc_s_parm = vidioc_s_parm,
1977 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1978 #ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = vidioc_g_register,
1980 .vidioc_s_register = vidioc_s_register,
1982 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1983 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1984 .vidiocgmbuf = vidiocgmbuf,
1988 static struct video_device gspca_template = {
1989 .name = "gspca main driver",
1991 .ioctl_ops = &dev_ioctl_ops,
1992 .release = gspca_release,
1997 * probe and create a new gspca device
1999 * This function must be called by the sub-driver when it is
2000 * called for probing a new device.
2002 int gspca_dev_probe(struct usb_interface *intf,
2003 const struct usb_device_id *id,
2004 const struct sd_desc *sd_desc,
2006 struct module *module)
2008 struct usb_interface_descriptor *interface;
2009 struct gspca_dev *gspca_dev;
2010 struct usb_device *dev = interface_to_usbdev(intf);
2013 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2015 /* we don't handle multi-config cameras */
2016 if (dev->descriptor.bNumConfigurations != 1) {
2017 PDEBUG(D_ERR, "Too many config");
2020 interface = &intf->cur_altsetting->desc;
2021 if (interface->bInterfaceNumber > 0) {
2022 PDEBUG(D_ERR, "intf != 0");
2026 /* create the device */
2027 if (dev_size < sizeof *gspca_dev)
2028 dev_size = sizeof *gspca_dev;
2029 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2031 err("couldn't kzalloc gspca struct");
2034 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2035 if (!gspca_dev->usb_buf) {
2036 err("out of memory");
2040 gspca_dev->dev = dev;
2041 gspca_dev->iface = interface->bInterfaceNumber;
2042 gspca_dev->nbalt = intf->num_altsetting;
2043 gspca_dev->sd_desc = sd_desc;
2044 gspca_dev->nbufread = 2;
2045 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2047 /* configure the subdriver and initialize the USB device */
2048 ret = sd_desc->config(gspca_dev, id);
2051 ret = sd_desc->init(gspca_dev);
2054 ret = gspca_set_alt0(gspca_dev);
2057 gspca_set_default_mode(gspca_dev);
2059 mutex_init(&gspca_dev->usb_lock);
2060 mutex_init(&gspca_dev->read_lock);
2061 mutex_init(&gspca_dev->queue_lock);
2062 init_waitqueue_head(&gspca_dev->wq);
2064 /* init video stuff */
2065 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2066 gspca_dev->vdev.parent = &intf->dev;
2067 gspca_dev->module = module;
2068 gspca_dev->present = 1;
2069 ret = video_register_device(&gspca_dev->vdev,
2073 err("video_register_device err %d", ret);
2077 usb_set_intfdata(intf, gspca_dev);
2078 PDEBUG(D_PROBE, "/dev/video%d created", gspca_dev->vdev.num);
2081 kfree(gspca_dev->usb_buf);
2085 EXPORT_SYMBOL(gspca_dev_probe);
2090 * This function must be called by the sub-driver
2091 * when the device disconnects, after the specific resources are freed.
2093 void gspca_disconnect(struct usb_interface *intf)
2095 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2097 PDEBUG(D_PROBE, "/dev/video%d disconnect", gspca_dev->vdev.num);
2098 mutex_lock(&gspca_dev->usb_lock);
2099 gspca_dev->present = 0;
2101 if (gspca_dev->streaming) {
2102 destroy_urbs(gspca_dev);
2103 wake_up_interruptible(&gspca_dev->wq);
2106 /* the device is freed at exit of this function */
2107 gspca_dev->dev = NULL;
2108 mutex_unlock(&gspca_dev->usb_lock);
2110 usb_set_intfdata(intf, NULL);
2112 /* release the device */
2113 /* (this will call gspca_release() immediatly or on last close) */
2114 video_unregister_device(&gspca_dev->vdev);
2116 /* PDEBUG(D_PROBE, "disconnect complete"); */
2118 EXPORT_SYMBOL(gspca_disconnect);
2121 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2123 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2125 if (!gspca_dev->streaming)
2127 gspca_dev->frozen = 1; /* avoid urb error messages */
2128 if (gspca_dev->sd_desc->stopN)
2129 gspca_dev->sd_desc->stopN(gspca_dev);
2130 destroy_urbs(gspca_dev);
2131 gspca_set_alt0(gspca_dev);
2132 if (gspca_dev->sd_desc->stop0)
2133 gspca_dev->sd_desc->stop0(gspca_dev);
2136 EXPORT_SYMBOL(gspca_suspend);
2138 int gspca_resume(struct usb_interface *intf)
2140 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2142 gspca_dev->frozen = 0;
2143 gspca_dev->sd_desc->init(gspca_dev);
2144 if (gspca_dev->streaming)
2145 return gspca_init_transfer(gspca_dev);
2148 EXPORT_SYMBOL(gspca_resume);
2150 /* -- cam driver utility functions -- */
2152 /* auto gain and exposure algorithm based on the knee algorithm described here:
2153 http://ytse.tricolour.net/docs/LowLightOptimization.html
2155 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2157 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2158 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2160 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2161 const struct ctrl *gain_ctrl = NULL;
2162 const struct ctrl *exposure_ctrl = NULL;
2163 const struct ctrl *autogain_ctrl = NULL;
2166 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2167 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2168 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2169 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2170 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2171 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2172 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2174 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2175 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2176 "on cam without (auto)gain/exposure");
2180 if (gain_ctrl->get(gspca_dev, &gain) ||
2181 exposure_ctrl->get(gspca_dev, &exposure) ||
2182 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2186 orig_exposure = exposure;
2188 /* If we are of a multiple of deadzone, do multiple steps to reach the
2189 desired lumination fast (with the risc of a slight overshoot) */
2190 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2192 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2193 avg_lum, desired_avg_lum, steps);
2195 for (i = 0; i < steps; i++) {
2196 if (avg_lum > desired_avg_lum) {
2197 if (gain > gain_knee)
2199 else if (exposure > exposure_knee)
2201 else if (gain > gain_ctrl->qctrl.default_value)
2203 else if (exposure > exposure_ctrl->qctrl.minimum)
2205 else if (gain > gain_ctrl->qctrl.minimum)
2210 if (gain < gain_ctrl->qctrl.default_value)
2212 else if (exposure < exposure_knee)
2214 else if (gain < gain_knee)
2216 else if (exposure < exposure_ctrl->qctrl.maximum)
2218 else if (gain < gain_ctrl->qctrl.maximum)
2225 if (gain != orig_gain) {
2226 gain_ctrl->set(gspca_dev, gain);
2229 if (exposure != orig_exposure) {
2230 exposure_ctrl->set(gspca_dev, exposure);
2236 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2238 /* -- module insert / remove -- */
2239 static int __init gspca_init(void)
2241 info("main v%d.%d.%d registered",
2242 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2243 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2244 DRIVER_VERSION_NUMBER & 0xff);
2247 static void __exit gspca_exit(void)
2249 info("main deregistered");
2252 module_init(gspca_init);
2253 module_exit(gspca_exit);
2256 module_param_named(debug, gspca_debug, int, 0644);
2257 MODULE_PARM_DESC(debug,
2258 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2259 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"