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 struct gspca_frame *frame;
130 u8 *data; /* address of data in the iso message */
134 if (urb->status != 0) {
135 if (urb->status == -ESHUTDOWN)
136 return; /* disconnection */
138 if (gspca_dev->frozen)
141 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
145 pkt_scan = gspca_dev->sd_desc->pkt_scan;
146 for (i = 0; i < urb->number_of_packets; i++) {
148 /* check the availability of the frame buffer */
149 frame = gspca_get_i_frame(gspca_dev);
151 gspca_dev->last_packet_type = DISCARD_PACKET;
155 /* check the packet status and length */
156 len = urb->iso_frame_desc[i].actual_length;
158 if (gspca_dev->empty_packet == 0)
159 gspca_dev->empty_packet = 1;
162 st = urb->iso_frame_desc[i].status;
165 "ISOC data error: [%d] len=%d, status=%d",
167 gspca_dev->last_packet_type = DISCARD_PACKET;
171 /* let the packet be analyzed by the subdriver */
172 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
173 i, urb->iso_frame_desc[i].offset, len);
174 data = (u8 *) urb->transfer_buffer
175 + urb->iso_frame_desc[i].offset;
176 pkt_scan(gspca_dev, frame, data, len);
180 /* resubmit the URB */
181 st = usb_submit_urb(urb, GFP_ATOMIC);
183 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
187 * ISOC message interrupt from the USB device
189 * Analyse each packet and call the subdriver for copy to the frame buffer.
191 static void isoc_irq(struct urb *urb)
193 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
195 PDEBUG(D_PACK, "isoc irq");
196 if (!gspca_dev->streaming)
198 fill_frame(gspca_dev, urb);
202 * bulk message interrupt from the USB device
204 static void bulk_irq(struct urb *urb)
206 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
207 struct gspca_frame *frame;
210 PDEBUG(D_PACK, "bulk irq");
211 if (!gspca_dev->streaming)
213 switch (urb->status) {
217 return; /* disconnection */
220 if (gspca_dev->frozen)
223 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
228 /* check the availability of the frame buffer */
229 frame = gspca_get_i_frame(gspca_dev);
231 gspca_dev->last_packet_type = DISCARD_PACKET;
233 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
234 gspca_dev->sd_desc->pkt_scan(gspca_dev,
236 urb->transfer_buffer,
241 /* resubmit the URB */
242 if (gspca_dev->cam.bulk_nurbs != 0) {
243 st = usb_submit_urb(urb, GFP_ATOMIC);
245 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
250 * add data to the current frame
252 * This function is called by the subdrivers at interrupt level.
254 * To build a frame, these ones must add
256 * - 0 or many INTER_PACKETs
258 * DISCARD_PACKET invalidates the whole frame.
259 * On LAST_PACKET, a new frame is returned.
261 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
262 enum gspca_packet_type packet_type,
263 struct gspca_frame *frame,
269 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
271 /* when start of a new frame, if the current frame buffer
272 * is not queued, discard the whole frame */
273 if (packet_type == FIRST_PACKET) {
274 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
275 != V4L2_BUF_FLAG_QUEUED) {
276 gspca_dev->last_packet_type = DISCARD_PACKET;
279 frame->data_end = frame->data;
280 jiffies_to_timeval(get_jiffies_64(),
281 &frame->v4l2_buf.timestamp);
282 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
283 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
284 if (packet_type == LAST_PACKET)
285 gspca_dev->last_packet_type = packet_type;
289 /* append the packet to the frame buffer */
291 if (frame->data_end - frame->data + len
292 > frame->v4l2_buf.length) {
293 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
294 frame->data_end - frame->data + len,
295 frame->v4l2_buf.length);
296 packet_type = DISCARD_PACKET;
298 memcpy(frame->data_end, data, len);
299 frame->data_end += len;
302 gspca_dev->last_packet_type = packet_type;
304 /* if last packet, wake up the application and advance in the queue */
305 if (packet_type == LAST_PACKET) {
306 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
307 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
308 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
309 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
310 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
312 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
313 frame->v4l2_buf.bytesused,
317 j = gspca_dev->fr_queue[i];
318 frame = &gspca_dev->frame[j];
322 EXPORT_SYMBOL(gspca_frame_add);
324 static int gspca_is_compressed(__u32 format)
327 case V4L2_PIX_FMT_MJPEG:
328 case V4L2_PIX_FMT_JPEG:
329 case V4L2_PIX_FMT_SPCA561:
330 case V4L2_PIX_FMT_PAC207:
331 case V4L2_PIX_FMT_MR97310A:
337 static void *rvmalloc(unsigned long size)
342 mem = vmalloc_32(size);
344 adr = (unsigned long) mem;
345 while ((long) size > 0) {
346 SetPageReserved(vmalloc_to_page((void *) adr));
354 static void rvfree(void *mem, long size)
358 adr = (unsigned long) mem;
360 ClearPageReserved(vmalloc_to_page((void *) adr));
367 static int frame_alloc(struct gspca_dev *gspca_dev,
370 struct gspca_frame *frame;
374 i = gspca_dev->curr_mode;
375 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
376 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
377 frsz = PAGE_ALIGN(frsz);
378 gspca_dev->frsz = frsz;
379 if (count > GSPCA_MAX_FRAMES)
380 count = GSPCA_MAX_FRAMES;
381 gspca_dev->frbuf = rvmalloc(frsz * count);
382 if (!gspca_dev->frbuf) {
383 err("frame alloc failed");
386 gspca_dev->nframes = count;
387 for (i = 0; i < count; i++) {
388 frame = &gspca_dev->frame[i];
389 frame->v4l2_buf.index = i;
390 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
391 frame->v4l2_buf.flags = 0;
392 frame->v4l2_buf.field = V4L2_FIELD_NONE;
393 frame->v4l2_buf.length = frsz;
394 frame->v4l2_buf.memory = gspca_dev->memory;
395 frame->v4l2_buf.sequence = 0;
396 frame->data = frame->data_end =
397 gspca_dev->frbuf + i * frsz;
398 frame->v4l2_buf.m.offset = i * frsz;
400 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
401 gspca_dev->last_packet_type = DISCARD_PACKET;
402 gspca_dev->sequence = 0;
406 static void frame_free(struct gspca_dev *gspca_dev)
410 PDEBUG(D_STREAM, "frame free");
411 if (gspca_dev->frbuf != NULL) {
412 rvfree(gspca_dev->frbuf,
413 gspca_dev->nframes * gspca_dev->frsz);
414 gspca_dev->frbuf = NULL;
415 for (i = 0; i < gspca_dev->nframes; i++)
416 gspca_dev->frame[i].data = NULL;
418 gspca_dev->nframes = 0;
421 static void destroy_urbs(struct gspca_dev *gspca_dev)
426 PDEBUG(D_STREAM, "kill transfer");
427 for (i = 0; i < MAX_NURBS; i++) {
428 urb = gspca_dev->urb[i];
432 gspca_dev->urb[i] = NULL;
434 if (urb->transfer_buffer != NULL)
435 usb_buffer_free(gspca_dev->dev,
436 urb->transfer_buffer_length,
437 urb->transfer_buffer,
444 * look for an input transfer endpoint in an alternate setting
446 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
449 struct usb_host_endpoint *ep;
452 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
453 ep = &alt->endpoint[i];
454 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
456 && ep->desc.wMaxPacketSize != 0)
463 * look for an input (isoc or bulk) endpoint
465 * The endpoint is defined by the subdriver.
466 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
467 * This routine may be called many times when the bandwidth is too small
468 * (the bandwidth is checked on urb submit).
470 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
472 struct usb_interface *intf;
473 struct usb_host_endpoint *ep;
476 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
478 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
479 : USB_ENDPOINT_XFER_ISOC;
480 i = gspca_dev->alt; /* previous alt setting */
481 if (gspca_dev->cam.reverse_alts) {
482 while (++i < gspca_dev->nbalt) {
483 ep = alt_xfer(&intf->altsetting[i], xfer);
489 ep = alt_xfer(&intf->altsetting[i], xfer);
495 err("no transfer endpoint found");
498 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
499 i, ep->desc.bEndpointAddress);
500 gspca_dev->alt = i; /* memorize the current alt setting */
501 if (gspca_dev->nbalt > 1) {
502 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
504 err("set alt %d err %d", i, ret);
512 * create the URBs for image transfer
514 static int create_urbs(struct gspca_dev *gspca_dev,
515 struct usb_host_endpoint *ep)
518 int n, nurbs, i, psize, npkt, bsize;
520 /* calculate the packet size and the number of packets */
521 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
523 if (!gspca_dev->cam.bulk) { /* isoc */
525 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
526 if (gspca_dev->pkt_size == 0)
527 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
529 psize = gspca_dev->pkt_size;
530 npkt = gspca_dev->cam.npkt;
532 npkt = 32; /* default value */
533 bsize = psize * npkt;
535 "isoc %d pkts size %d = bsize:%d",
540 bsize = gspca_dev->cam.bulk_size;
543 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
544 if (gspca_dev->cam.bulk_nurbs != 0)
545 nurbs = gspca_dev->cam.bulk_nurbs;
550 gspca_dev->nurbs = nurbs;
551 for (n = 0; n < nurbs; n++) {
552 urb = usb_alloc_urb(npkt, GFP_KERNEL);
554 err("usb_alloc_urb failed");
555 destroy_urbs(gspca_dev);
558 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
563 if (urb->transfer_buffer == NULL) {
565 err("usb_buffer_urb failed");
566 destroy_urbs(gspca_dev);
569 gspca_dev->urb[n] = urb;
570 urb->dev = gspca_dev->dev;
571 urb->context = gspca_dev;
572 urb->transfer_buffer_length = bsize;
573 if (npkt != 0) { /* ISOC */
574 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
575 ep->desc.bEndpointAddress);
576 urb->transfer_flags = URB_ISO_ASAP
577 | URB_NO_TRANSFER_DMA_MAP;
578 urb->interval = ep->desc.bInterval;
579 urb->complete = isoc_irq;
580 urb->number_of_packets = npkt;
581 for (i = 0; i < npkt; i++) {
582 urb->iso_frame_desc[i].length = psize;
583 urb->iso_frame_desc[i].offset = psize * i;
586 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
587 ep->desc.bEndpointAddress),
588 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
589 urb->complete = bulk_irq;
596 * start the USB transfer
598 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
600 struct usb_host_endpoint *ep;
603 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
606 if (!gspca_dev->present) {
611 /* set the higher alternate setting and
612 * loop until urb submit succeeds */
613 if (gspca_dev->cam.reverse_alts)
616 gspca_dev->alt = gspca_dev->nbalt;
618 if (gspca_dev->sd_desc->isoc_init) {
619 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
623 ep = get_ep(gspca_dev);
629 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
630 ret = create_urbs(gspca_dev, ep);
634 /* clear the bulk endpoint */
635 if (gspca_dev->cam.bulk)
636 usb_clear_halt(gspca_dev->dev,
637 gspca_dev->urb[0]->pipe);
640 ret = gspca_dev->sd_desc->start(gspca_dev);
642 destroy_urbs(gspca_dev);
645 gspca_dev->streaming = 1;
647 /* some bulk transfers are started by the subdriver */
648 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
651 /* submit the URBs */
652 for (n = 0; n < gspca_dev->nurbs; n++) {
653 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
659 gspca_dev->streaming = 0;
660 destroy_urbs(gspca_dev);
661 if (ret != -ENOSPC) {
662 PDEBUG(D_ERR|D_STREAM,
663 "usb_submit_urb alt %d err %d",
664 gspca_dev->alt, ret);
668 /* the bandwidth is not wide enough
669 * negociate or try a lower alternate setting */
670 PDEBUG(D_ERR|D_STREAM,
671 "bandwidth not wide enough - trying again");
672 msleep(20); /* wait for kill complete */
673 if (gspca_dev->sd_desc->isoc_nego) {
674 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
678 ep = get_ep(gspca_dev);
686 mutex_unlock(&gspca_dev->usb_lock);
690 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
694 if (gspca_dev->alt == 0)
696 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
698 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
702 /* Note: both the queue and the usb locks should be held when calling this */
703 static void gspca_stream_off(struct gspca_dev *gspca_dev)
705 gspca_dev->streaming = 0;
706 if (gspca_dev->present) {
707 if (gspca_dev->sd_desc->stopN)
708 gspca_dev->sd_desc->stopN(gspca_dev);
709 destroy_urbs(gspca_dev);
710 gspca_set_alt0(gspca_dev);
713 /* always call stop0 to free the subdriver's resources */
714 if (gspca_dev->sd_desc->stop0)
715 gspca_dev->sd_desc->stop0(gspca_dev);
716 PDEBUG(D_STREAM, "stream off OK");
719 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
723 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
724 gspca_dev->curr_mode = i;
725 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
726 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
727 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
730 static int wxh_to_mode(struct gspca_dev *gspca_dev,
731 int width, int height)
735 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
736 if (width >= gspca_dev->cam.cam_mode[i].width
737 && height >= gspca_dev->cam.cam_mode[i].height)
744 * search a mode with the right pixel format
746 static int gspca_get_mode(struct gspca_dev *gspca_dev,
752 modeU = modeD = mode;
753 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
755 if (gspca_dev->cam.cam_mode[modeD].pixelformat
759 if (++modeU < gspca_dev->cam.nmodes) {
760 if (gspca_dev->cam.cam_mode[modeU].pixelformat
768 #ifdef CONFIG_VIDEO_ADV_DEBUG
769 static int vidioc_g_register(struct file *file, void *priv,
770 struct v4l2_dbg_register *reg)
773 struct gspca_dev *gspca_dev = priv;
775 if (!gspca_dev->sd_desc->get_chip_ident)
778 if (!gspca_dev->sd_desc->get_register)
781 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
783 if (gspca_dev->present)
784 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
787 mutex_unlock(&gspca_dev->usb_lock);
792 static int vidioc_s_register(struct file *file, void *priv,
793 struct v4l2_dbg_register *reg)
796 struct gspca_dev *gspca_dev = priv;
798 if (!gspca_dev->sd_desc->get_chip_ident)
801 if (!gspca_dev->sd_desc->set_register)
804 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
806 if (gspca_dev->present)
807 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
810 mutex_unlock(&gspca_dev->usb_lock);
816 static int vidioc_g_chip_ident(struct file *file, void *priv,
817 struct v4l2_dbg_chip_ident *chip)
820 struct gspca_dev *gspca_dev = priv;
822 if (!gspca_dev->sd_desc->get_chip_ident)
825 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
827 if (gspca_dev->present)
828 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
831 mutex_unlock(&gspca_dev->usb_lock);
836 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
837 struct v4l2_fmtdesc *fmtdesc)
839 struct gspca_dev *gspca_dev = priv;
843 /* give an index to each format */
846 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
847 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
850 if (fmt_tb[j] == fmt_tb[index])
855 if (fmtdesc->index == index)
856 break; /* new format */
858 if (index >= ARRAY_SIZE(fmt_tb))
863 return -EINVAL; /* no more format */
865 fmtdesc->pixelformat = fmt_tb[index];
866 if (gspca_is_compressed(fmt_tb[index]))
867 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
868 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
869 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
870 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
871 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
872 fmtdesc->description[4] = '\0';
876 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
877 struct v4l2_format *fmt)
879 struct gspca_dev *gspca_dev = priv;
882 mode = gspca_dev->curr_mode;
883 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
884 sizeof fmt->fmt.pix);
888 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
889 struct v4l2_format *fmt)
891 int w, h, mode, mode2;
893 w = fmt->fmt.pix.width;
894 h = fmt->fmt.pix.height;
897 if (gspca_debug & D_CONF)
898 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
900 /* search the closest mode for width and height */
901 mode = wxh_to_mode(gspca_dev, w, h);
903 /* OK if right palette */
904 if (gspca_dev->cam.cam_mode[mode].pixelformat
905 != fmt->fmt.pix.pixelformat) {
907 /* else, search the closest mode with the same pixel format */
908 mode2 = gspca_get_mode(gspca_dev, mode,
909 fmt->fmt.pix.pixelformat);
913 ; * no chance, return this mode */
915 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
916 sizeof fmt->fmt.pix);
917 return mode; /* used when s_fmt */
920 static int vidioc_try_fmt_vid_cap(struct file *file,
922 struct v4l2_format *fmt)
924 struct gspca_dev *gspca_dev = priv;
927 ret = try_fmt_vid_cap(gspca_dev, fmt);
933 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
934 struct v4l2_format *fmt)
936 struct gspca_dev *gspca_dev = priv;
939 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
942 ret = try_fmt_vid_cap(gspca_dev, fmt);
946 if (gspca_dev->nframes != 0
947 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
952 if (ret == gspca_dev->curr_mode) {
954 goto out; /* same mode */
957 if (gspca_dev->streaming) {
961 gspca_dev->width = fmt->fmt.pix.width;
962 gspca_dev->height = fmt->fmt.pix.height;
963 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
964 gspca_dev->curr_mode = ret;
968 mutex_unlock(&gspca_dev->queue_lock);
972 static int vidioc_enum_framesizes(struct file *file, void *priv,
973 struct v4l2_frmsizeenum *fsize)
975 struct gspca_dev *gspca_dev = priv;
979 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
980 if (fsize->pixel_format !=
981 gspca_dev->cam.cam_mode[i].pixelformat)
984 if (fsize->index == index) {
985 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
986 fsize->discrete.width =
987 gspca_dev->cam.cam_mode[i].width;
988 fsize->discrete.height =
989 gspca_dev->cam.cam_mode[i].height;
998 static void gspca_release(struct video_device *vfd)
1000 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1002 PDEBUG(D_PROBE, "/dev/video%d released", gspca_dev->vdev.num);
1004 kfree(gspca_dev->usb_buf);
1008 static int dev_open(struct file *file)
1010 struct gspca_dev *gspca_dev;
1013 PDEBUG(D_STREAM, "[%s] open", current->comm);
1014 gspca_dev = (struct gspca_dev *) video_devdata(file);
1015 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1016 return -ERESTARTSYS;
1017 if (!gspca_dev->present) {
1022 if (gspca_dev->users > 4) { /* (arbitrary value) */
1027 /* protect the subdriver against rmmod */
1028 if (!try_module_get(gspca_dev->module)) {
1035 file->private_data = gspca_dev;
1037 /* activate the v4l2 debug */
1038 if (gspca_debug & D_V4L2)
1039 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1040 | V4L2_DEBUG_IOCTL_ARG;
1042 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1043 | V4L2_DEBUG_IOCTL_ARG);
1047 mutex_unlock(&gspca_dev->queue_lock);
1049 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1051 PDEBUG(D_STREAM, "open done");
1055 static int dev_close(struct file *file)
1057 struct gspca_dev *gspca_dev = file->private_data;
1059 PDEBUG(D_STREAM, "[%s] close", current->comm);
1060 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1061 return -ERESTARTSYS;
1064 /* if the file did the capture, free the streaming resources */
1065 if (gspca_dev->capt_file == file) {
1066 if (gspca_dev->streaming) {
1067 mutex_lock(&gspca_dev->usb_lock);
1068 gspca_stream_off(gspca_dev);
1069 mutex_unlock(&gspca_dev->usb_lock);
1071 frame_free(gspca_dev);
1072 gspca_dev->capt_file = NULL;
1073 gspca_dev->memory = GSPCA_MEMORY_NO;
1075 file->private_data = NULL;
1076 module_put(gspca_dev->module);
1077 mutex_unlock(&gspca_dev->queue_lock);
1079 PDEBUG(D_STREAM, "close done");
1084 static int vidioc_querycap(struct file *file, void *priv,
1085 struct v4l2_capability *cap)
1087 struct gspca_dev *gspca_dev = priv;
1090 /* protect the access to the usb device */
1091 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1092 return -ERESTARTSYS;
1093 if (!gspca_dev->present) {
1097 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1098 if (gspca_dev->dev->product != NULL) {
1099 strncpy(cap->card, gspca_dev->dev->product,
1102 snprintf(cap->card, sizeof cap->card,
1103 "USB Camera (%04x:%04x)",
1104 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1105 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1107 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1108 cap->version = DRIVER_VERSION_NUMBER;
1109 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1110 | V4L2_CAP_STREAMING
1111 | V4L2_CAP_READWRITE;
1114 mutex_unlock(&gspca_dev->usb_lock);
1118 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1121 const struct ctrl *ctrls;
1124 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1125 i < gspca_dev->sd_desc->nctrls;
1127 if (gspca_dev->ctrl_dis & (1 << i))
1129 if (id == ctrls->qctrl.id)
1135 static int vidioc_queryctrl(struct file *file, void *priv,
1136 struct v4l2_queryctrl *q_ctrl)
1138 struct gspca_dev *gspca_dev = priv;
1139 const struct ctrl *ctrls;
1145 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1146 id &= V4L2_CTRL_ID_MASK;
1148 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1149 if (gspca_dev->ctrl_dis & (1 << i))
1151 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1153 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1156 ctrls = &gspca_dev->sd_desc->ctrls[i];
1159 ctrls = get_ctrl(gspca_dev, id);
1160 i = ctrls - gspca_dev->sd_desc->ctrls;
1164 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1165 if (gspca_dev->ctrl_inac & (1 << i))
1166 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1170 static int vidioc_s_ctrl(struct file *file, void *priv,
1171 struct v4l2_control *ctrl)
1173 struct gspca_dev *gspca_dev = priv;
1174 const struct ctrl *ctrls;
1177 ctrls = get_ctrl(gspca_dev, ctrl->id);
1181 if (ctrl->value < ctrls->qctrl.minimum
1182 || ctrl->value > ctrls->qctrl.maximum)
1184 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1185 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1186 return -ERESTARTSYS;
1187 if (gspca_dev->present)
1188 ret = ctrls->set(gspca_dev, ctrl->value);
1191 mutex_unlock(&gspca_dev->usb_lock);
1195 static int vidioc_g_ctrl(struct file *file, void *priv,
1196 struct v4l2_control *ctrl)
1198 struct gspca_dev *gspca_dev = priv;
1199 const struct ctrl *ctrls;
1202 ctrls = get_ctrl(gspca_dev, ctrl->id);
1206 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1207 return -ERESTARTSYS;
1208 if (gspca_dev->present)
1209 ret = ctrls->get(gspca_dev, &ctrl->value);
1212 mutex_unlock(&gspca_dev->usb_lock);
1216 /*fixme: have an audio flag in gspca_dev?*/
1217 static int vidioc_s_audio(struct file *file, void *priv,
1218 struct v4l2_audio *audio)
1220 if (audio->index != 0)
1225 static int vidioc_g_audio(struct file *file, void *priv,
1226 struct v4l2_audio *audio)
1228 strcpy(audio->name, "Microphone");
1232 static int vidioc_enumaudio(struct file *file, void *priv,
1233 struct v4l2_audio *audio)
1235 if (audio->index != 0)
1238 strcpy(audio->name, "Microphone");
1239 audio->capability = 0;
1244 static int vidioc_querymenu(struct file *file, void *priv,
1245 struct v4l2_querymenu *qmenu)
1247 struct gspca_dev *gspca_dev = priv;
1249 if (!gspca_dev->sd_desc->querymenu)
1251 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1254 static int vidioc_enum_input(struct file *file, void *priv,
1255 struct v4l2_input *input)
1257 struct gspca_dev *gspca_dev = priv;
1259 if (input->index != 0)
1261 input->type = V4L2_INPUT_TYPE_CAMERA;
1262 input->status = gspca_dev->cam.input_flags;
1263 strncpy(input->name, gspca_dev->sd_desc->name,
1264 sizeof input->name);
1268 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1274 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1281 static int vidioc_reqbufs(struct file *file, void *priv,
1282 struct v4l2_requestbuffers *rb)
1284 struct gspca_dev *gspca_dev = priv;
1287 switch (rb->memory) {
1288 case GSPCA_MEMORY_READ: /* (internal call) */
1289 case V4L2_MEMORY_MMAP:
1290 case V4L2_MEMORY_USERPTR:
1295 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1296 return -ERESTARTSYS;
1298 if (gspca_dev->memory != GSPCA_MEMORY_NO
1299 && gspca_dev->memory != rb->memory) {
1304 /* only one file may do the capture */
1305 if (gspca_dev->capt_file != NULL
1306 && gspca_dev->capt_file != file) {
1311 /* if allocated, the buffers must not be mapped */
1312 for (i = 0; i < gspca_dev->nframes; i++) {
1313 if (gspca_dev->frame[i].vma_use_count) {
1319 /* stop streaming */
1320 if (gspca_dev->streaming) {
1321 mutex_lock(&gspca_dev->usb_lock);
1322 gspca_stream_off(gspca_dev);
1323 mutex_unlock(&gspca_dev->usb_lock);
1326 /* free the previous allocated buffers, if any */
1327 if (gspca_dev->nframes != 0) {
1328 frame_free(gspca_dev);
1329 gspca_dev->capt_file = NULL;
1331 if (rb->count == 0) /* unrequest */
1333 gspca_dev->memory = rb->memory;
1334 ret = frame_alloc(gspca_dev, rb->count);
1336 rb->count = gspca_dev->nframes;
1337 gspca_dev->capt_file = file;
1340 mutex_unlock(&gspca_dev->queue_lock);
1341 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1345 static int vidioc_querybuf(struct file *file, void *priv,
1346 struct v4l2_buffer *v4l2_buf)
1348 struct gspca_dev *gspca_dev = priv;
1349 struct gspca_frame *frame;
1351 if (v4l2_buf->index < 0
1352 || v4l2_buf->index >= gspca_dev->nframes)
1355 frame = &gspca_dev->frame[v4l2_buf->index];
1356 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1360 static int vidioc_streamon(struct file *file, void *priv,
1361 enum v4l2_buf_type buf_type)
1363 struct gspca_dev *gspca_dev = priv;
1366 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1368 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1369 return -ERESTARTSYS;
1371 if (gspca_dev->nframes == 0
1372 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1376 if (!gspca_dev->streaming) {
1377 ret = gspca_init_transfer(gspca_dev);
1382 if (gspca_debug & D_STREAM) {
1383 PDEBUG_MODE("stream on OK",
1391 mutex_unlock(&gspca_dev->queue_lock);
1395 static int vidioc_streamoff(struct file *file, void *priv,
1396 enum v4l2_buf_type buf_type)
1398 struct gspca_dev *gspca_dev = priv;
1401 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1403 if (!gspca_dev->streaming)
1405 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1406 return -ERESTARTSYS;
1408 /* stop streaming */
1409 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1413 gspca_stream_off(gspca_dev);
1414 mutex_unlock(&gspca_dev->usb_lock);
1416 /* empty the application queues */
1417 for (i = 0; i < gspca_dev->nframes; i++)
1418 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1419 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1420 gspca_dev->last_packet_type = DISCARD_PACKET;
1421 gspca_dev->sequence = 0;
1424 mutex_unlock(&gspca_dev->queue_lock);
1428 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1429 struct v4l2_jpegcompression *jpegcomp)
1431 struct gspca_dev *gspca_dev = priv;
1434 if (!gspca_dev->sd_desc->get_jcomp)
1436 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1437 return -ERESTARTSYS;
1438 if (gspca_dev->present)
1439 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1442 mutex_unlock(&gspca_dev->usb_lock);
1446 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1447 struct v4l2_jpegcompression *jpegcomp)
1449 struct gspca_dev *gspca_dev = priv;
1452 if (!gspca_dev->sd_desc->set_jcomp)
1454 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1455 return -ERESTARTSYS;
1456 if (gspca_dev->present)
1457 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1460 mutex_unlock(&gspca_dev->usb_lock);
1464 static int vidioc_g_parm(struct file *filp, void *priv,
1465 struct v4l2_streamparm *parm)
1467 struct gspca_dev *gspca_dev = priv;
1469 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1471 if (gspca_dev->sd_desc->get_streamparm) {
1474 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1475 return -ERESTARTSYS;
1476 if (gspca_dev->present)
1477 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1481 mutex_unlock(&gspca_dev->usb_lock);
1488 static int vidioc_s_parm(struct file *filp, void *priv,
1489 struct v4l2_streamparm *parm)
1491 struct gspca_dev *gspca_dev = priv;
1494 n = parm->parm.capture.readbuffers;
1495 if (n == 0 || n > GSPCA_MAX_FRAMES)
1496 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1498 gspca_dev->nbufread = n;
1500 if (gspca_dev->sd_desc->set_streamparm) {
1503 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1504 return -ERESTARTSYS;
1505 if (gspca_dev->present)
1506 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1510 mutex_unlock(&gspca_dev->usb_lock);
1517 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1518 static int vidiocgmbuf(struct file *file, void *priv,
1519 struct video_mbuf *mbuf)
1521 struct gspca_dev *gspca_dev = file->private_data;
1524 PDEBUG(D_STREAM, "cgmbuf");
1525 if (gspca_dev->nframes == 0) {
1529 struct v4l2_format fmt;
1531 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1532 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1533 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1534 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1535 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1536 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1541 struct v4l2_requestbuffers rb;
1543 memset(&rb, 0, sizeof rb);
1545 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1546 rb.memory = V4L2_MEMORY_MMAP;
1547 ret = vidioc_reqbufs(file, priv, &rb);
1552 mbuf->frames = gspca_dev->nframes;
1553 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1554 for (i = 0; i < mbuf->frames; i++)
1555 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1560 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1562 struct gspca_dev *gspca_dev = file->private_data;
1563 struct gspca_frame *frame;
1565 unsigned long addr, start, size;
1568 start = vma->vm_start;
1569 size = vma->vm_end - vma->vm_start;
1570 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1572 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1573 return -ERESTARTSYS;
1574 if (!gspca_dev->present) {
1578 if (gspca_dev->capt_file != file) {
1584 for (i = 0; i < gspca_dev->nframes; ++i) {
1585 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1586 PDEBUG(D_STREAM, "mmap bad memory type");
1589 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1591 frame = &gspca_dev->frame[i];
1595 if (frame == NULL) {
1596 PDEBUG(D_STREAM, "mmap no frame buffer found");
1600 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1601 /* v4l1 maps all the buffers */
1603 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1605 if (size != frame->v4l2_buf.length) {
1606 PDEBUG(D_STREAM, "mmap bad size");
1612 * - VM_IO marks the area as being a mmaped region for I/O to a
1613 * device. It also prevents the region from being core dumped.
1615 vma->vm_flags |= VM_IO;
1617 addr = (unsigned long) frame->data;
1619 page = vmalloc_to_page((void *) addr);
1620 ret = vm_insert_page(vma, start, page);
1628 vma->vm_ops = (struct vm_operations_struct *) &gspca_vm_ops;
1629 vma->vm_private_data = frame;
1633 mutex_unlock(&gspca_dev->queue_lock);
1638 * wait for a video frame
1640 * If a frame is ready, its index is returned.
1642 static int frame_wait(struct gspca_dev *gspca_dev,
1645 struct gspca_frame *frame;
1648 /* check if a frame is ready */
1649 i = gspca_dev->fr_o;
1650 j = gspca_dev->fr_queue[i];
1651 frame = &gspca_dev->frame[j];
1653 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1657 /* wait till a frame is ready */
1658 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1659 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1660 !gspca_dev->streaming || !gspca_dev->present,
1661 msecs_to_jiffies(3000));
1664 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1668 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1669 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1674 if (gspca_dev->sd_desc->dq_callback) {
1675 mutex_lock(&gspca_dev->usb_lock);
1676 if (gspca_dev->present)
1677 gspca_dev->sd_desc->dq_callback(gspca_dev);
1678 mutex_unlock(&gspca_dev->usb_lock);
1684 * dequeue a video buffer
1686 * If nonblock_ing is false, block until a buffer is available.
1688 static int vidioc_dqbuf(struct file *file, void *priv,
1689 struct v4l2_buffer *v4l2_buf)
1691 struct gspca_dev *gspca_dev = priv;
1692 struct gspca_frame *frame;
1695 PDEBUG(D_FRAM, "dqbuf");
1696 if (v4l2_buf->memory != gspca_dev->memory)
1699 if (!gspca_dev->present)
1702 /* if not streaming, be sure the application will not loop forever */
1703 if (!(file->f_flags & O_NONBLOCK)
1704 && !gspca_dev->streaming && gspca_dev->users == 1)
1707 /* only the capturing file may dequeue */
1708 if (gspca_dev->capt_file != file)
1711 /* only one dequeue / read at a time */
1712 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1713 return -ERESTARTSYS;
1715 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1718 i = ret; /* frame index */
1719 frame = &gspca_dev->frame[i];
1720 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1721 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1723 frame->v4l2_buf.bytesused)) {
1724 PDEBUG(D_ERR|D_STREAM,
1725 "dqbuf cp to user failed");
1730 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1731 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1732 PDEBUG(D_FRAM, "dqbuf %d", i);
1735 mutex_unlock(&gspca_dev->read_lock);
1740 * queue a video buffer
1742 * Attempting to queue a buffer that has already been
1743 * queued will return -EINVAL.
1745 static int vidioc_qbuf(struct file *file, void *priv,
1746 struct v4l2_buffer *v4l2_buf)
1748 struct gspca_dev *gspca_dev = priv;
1749 struct gspca_frame *frame;
1752 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1754 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1755 return -ERESTARTSYS;
1757 index = v4l2_buf->index;
1758 if ((unsigned) index >= gspca_dev->nframes) {
1760 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1764 if (v4l2_buf->memory != gspca_dev->memory) {
1765 PDEBUG(D_FRAM, "qbuf bad memory type");
1770 frame = &gspca_dev->frame[index];
1771 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1772 PDEBUG(D_FRAM, "qbuf bad state");
1777 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1779 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1780 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1781 frame->v4l2_buf.length = v4l2_buf->length;
1784 /* put the buffer in the 'queued' queue */
1785 i = gspca_dev->fr_q;
1786 gspca_dev->fr_queue[i] = index;
1787 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1788 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1793 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1794 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1797 mutex_unlock(&gspca_dev->queue_lock);
1802 * allocate the resources for read()
1804 static int read_alloc(struct gspca_dev *gspca_dev,
1807 struct v4l2_buffer v4l2_buf;
1810 PDEBUG(D_STREAM, "read alloc");
1811 if (gspca_dev->nframes == 0) {
1812 struct v4l2_requestbuffers rb;
1814 memset(&rb, 0, sizeof rb);
1815 rb.count = gspca_dev->nbufread;
1816 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1817 rb.memory = GSPCA_MEMORY_READ;
1818 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1820 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1823 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1824 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1825 v4l2_buf.memory = GSPCA_MEMORY_READ;
1826 for (i = 0; i < gspca_dev->nbufread; i++) {
1828 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1830 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1834 gspca_dev->memory = GSPCA_MEMORY_READ;
1837 /* start streaming */
1838 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1840 PDEBUG(D_STREAM, "read streamon err %d", ret);
1844 static unsigned int dev_poll(struct file *file, poll_table *wait)
1846 struct gspca_dev *gspca_dev = file->private_data;
1849 PDEBUG(D_FRAM, "poll");
1851 poll_wait(file, &gspca_dev->wq, wait);
1853 /* if reqbufs is not done, the user would use read() */
1854 if (gspca_dev->nframes == 0) {
1855 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1856 return POLLERR; /* not the 1st time */
1857 ret = read_alloc(gspca_dev, file);
1862 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1865 /* check the next incoming buffer */
1866 i = gspca_dev->fr_o;
1867 i = gspca_dev->fr_queue[i];
1868 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1869 ret = POLLIN | POLLRDNORM; /* something to read */
1872 mutex_unlock(&gspca_dev->queue_lock);
1873 if (!gspca_dev->present)
1878 static ssize_t dev_read(struct file *file, char __user *data,
1879 size_t count, loff_t *ppos)
1881 struct gspca_dev *gspca_dev = file->private_data;
1882 struct gspca_frame *frame;
1883 struct v4l2_buffer v4l2_buf;
1884 struct timeval timestamp;
1887 PDEBUG(D_FRAM, "read (%zd)", count);
1888 if (!gspca_dev->present)
1890 switch (gspca_dev->memory) {
1891 case GSPCA_MEMORY_NO: /* first time */
1892 ret = read_alloc(gspca_dev, file);
1896 case GSPCA_MEMORY_READ:
1897 if (gspca_dev->capt_file == file)
1905 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1909 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1910 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1911 v4l2_buf.memory = GSPCA_MEMORY_READ;
1912 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1914 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1918 /* if the process slept for more than 1 second,
1919 * get a newer frame */
1920 frame = &gspca_dev->frame[v4l2_buf.index];
1922 break; /* avoid infinite loop */
1923 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1925 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1927 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1932 /* copy the frame */
1933 if (count > frame->v4l2_buf.bytesused)
1934 count = frame->v4l2_buf.bytesused;
1935 ret = copy_to_user(data, frame->data, count);
1937 PDEBUG(D_ERR|D_STREAM,
1938 "read cp to user lack %d / %zd", ret, count);
1944 /* in each case, requeue the buffer */
1945 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1951 static struct v4l2_file_operations dev_fops = {
1952 .owner = THIS_MODULE,
1954 .release = dev_close,
1957 .unlocked_ioctl = video_ioctl2,
1961 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1962 .vidioc_querycap = vidioc_querycap,
1963 .vidioc_dqbuf = vidioc_dqbuf,
1964 .vidioc_qbuf = vidioc_qbuf,
1965 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1966 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1967 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1968 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1969 .vidioc_streamon = vidioc_streamon,
1970 .vidioc_queryctrl = vidioc_queryctrl,
1971 .vidioc_g_ctrl = vidioc_g_ctrl,
1972 .vidioc_s_ctrl = vidioc_s_ctrl,
1973 .vidioc_g_audio = vidioc_g_audio,
1974 .vidioc_s_audio = vidioc_s_audio,
1975 .vidioc_enumaudio = vidioc_enumaudio,
1976 .vidioc_querymenu = vidioc_querymenu,
1977 .vidioc_enum_input = vidioc_enum_input,
1978 .vidioc_g_input = vidioc_g_input,
1979 .vidioc_s_input = vidioc_s_input,
1980 .vidioc_reqbufs = vidioc_reqbufs,
1981 .vidioc_querybuf = vidioc_querybuf,
1982 .vidioc_streamoff = vidioc_streamoff,
1983 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1984 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1985 .vidioc_g_parm = vidioc_g_parm,
1986 .vidioc_s_parm = vidioc_s_parm,
1987 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1988 #ifdef CONFIG_VIDEO_ADV_DEBUG
1989 .vidioc_g_register = vidioc_g_register,
1990 .vidioc_s_register = vidioc_s_register,
1992 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1993 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1994 .vidiocgmbuf = vidiocgmbuf,
1998 static struct video_device gspca_template = {
1999 .name = "gspca main driver",
2001 .ioctl_ops = &dev_ioctl_ops,
2002 .release = gspca_release,
2007 * probe and create a new gspca device
2009 * This function must be called by the sub-driver when it is
2010 * called for probing a new device.
2012 int gspca_dev_probe(struct usb_interface *intf,
2013 const struct usb_device_id *id,
2014 const struct sd_desc *sd_desc,
2016 struct module *module)
2018 struct usb_interface_descriptor *interface;
2019 struct gspca_dev *gspca_dev;
2020 struct usb_device *dev = interface_to_usbdev(intf);
2023 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2025 /* we don't handle multi-config cameras */
2026 if (dev->descriptor.bNumConfigurations != 1) {
2027 PDEBUG(D_ERR, "Too many config");
2030 interface = &intf->cur_altsetting->desc;
2031 if (interface->bInterfaceNumber > 0) {
2032 PDEBUG(D_ERR, "intf != 0");
2036 /* create the device */
2037 if (dev_size < sizeof *gspca_dev)
2038 dev_size = sizeof *gspca_dev;
2039 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2041 err("couldn't kzalloc gspca struct");
2044 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2045 if (!gspca_dev->usb_buf) {
2046 err("out of memory");
2050 gspca_dev->dev = dev;
2051 gspca_dev->iface = interface->bInterfaceNumber;
2052 gspca_dev->nbalt = intf->num_altsetting;
2053 gspca_dev->sd_desc = sd_desc;
2054 gspca_dev->nbufread = 2;
2055 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2057 /* configure the subdriver and initialize the USB device */
2058 ret = sd_desc->config(gspca_dev, id);
2061 ret = sd_desc->init(gspca_dev);
2064 ret = gspca_set_alt0(gspca_dev);
2067 gspca_set_default_mode(gspca_dev);
2069 mutex_init(&gspca_dev->usb_lock);
2070 mutex_init(&gspca_dev->read_lock);
2071 mutex_init(&gspca_dev->queue_lock);
2072 init_waitqueue_head(&gspca_dev->wq);
2074 /* init video stuff */
2075 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2076 gspca_dev->vdev.parent = &intf->dev;
2077 gspca_dev->module = module;
2078 gspca_dev->present = 1;
2079 ret = video_register_device(&gspca_dev->vdev,
2083 err("video_register_device err %d", ret);
2087 usb_set_intfdata(intf, gspca_dev);
2088 PDEBUG(D_PROBE, "/dev/video%d created", gspca_dev->vdev.num);
2091 kfree(gspca_dev->usb_buf);
2095 EXPORT_SYMBOL(gspca_dev_probe);
2100 * This function must be called by the sub-driver
2101 * when the device disconnects, after the specific resources are freed.
2103 void gspca_disconnect(struct usb_interface *intf)
2105 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2107 PDEBUG(D_PROBE, "/dev/video%d disconnect", gspca_dev->vdev.num);
2108 mutex_lock(&gspca_dev->usb_lock);
2109 gspca_dev->present = 0;
2111 if (gspca_dev->streaming) {
2112 destroy_urbs(gspca_dev);
2113 wake_up_interruptible(&gspca_dev->wq);
2116 /* the device is freed at exit of this function */
2117 gspca_dev->dev = NULL;
2118 mutex_unlock(&gspca_dev->usb_lock);
2120 usb_set_intfdata(intf, NULL);
2122 /* release the device */
2123 /* (this will call gspca_release() immediatly or on last close) */
2124 video_unregister_device(&gspca_dev->vdev);
2126 /* PDEBUG(D_PROBE, "disconnect complete"); */
2128 EXPORT_SYMBOL(gspca_disconnect);
2131 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2133 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2135 if (!gspca_dev->streaming)
2137 gspca_dev->frozen = 1; /* avoid urb error messages */
2138 if (gspca_dev->sd_desc->stopN)
2139 gspca_dev->sd_desc->stopN(gspca_dev);
2140 destroy_urbs(gspca_dev);
2141 gspca_set_alt0(gspca_dev);
2142 if (gspca_dev->sd_desc->stop0)
2143 gspca_dev->sd_desc->stop0(gspca_dev);
2146 EXPORT_SYMBOL(gspca_suspend);
2148 int gspca_resume(struct usb_interface *intf)
2150 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2152 gspca_dev->frozen = 0;
2153 gspca_dev->sd_desc->init(gspca_dev);
2154 if (gspca_dev->streaming)
2155 return gspca_init_transfer(gspca_dev);
2158 EXPORT_SYMBOL(gspca_resume);
2160 /* -- cam driver utility functions -- */
2162 /* auto gain and exposure algorithm based on the knee algorithm described here:
2163 http://ytse.tricolour.net/docs/LowLightOptimization.html
2165 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2167 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2168 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2170 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2171 const struct ctrl *gain_ctrl = NULL;
2172 const struct ctrl *exposure_ctrl = NULL;
2173 const struct ctrl *autogain_ctrl = NULL;
2176 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2177 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2178 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2179 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2180 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2181 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2182 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2184 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2185 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2186 "on cam without (auto)gain/exposure");
2190 if (gain_ctrl->get(gspca_dev, &gain) ||
2191 exposure_ctrl->get(gspca_dev, &exposure) ||
2192 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2196 orig_exposure = exposure;
2198 /* If we are of a multiple of deadzone, do multiple steps to reach the
2199 desired lumination fast (with the risc of a slight overshoot) */
2200 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2202 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2203 avg_lum, desired_avg_lum, steps);
2205 for (i = 0; i < steps; i++) {
2206 if (avg_lum > desired_avg_lum) {
2207 if (gain > gain_knee)
2209 else if (exposure > exposure_knee)
2211 else if (gain > gain_ctrl->qctrl.default_value)
2213 else if (exposure > exposure_ctrl->qctrl.minimum)
2215 else if (gain > gain_ctrl->qctrl.minimum)
2220 if (gain < gain_ctrl->qctrl.default_value)
2222 else if (exposure < exposure_knee)
2224 else if (gain < gain_knee)
2226 else if (exposure < exposure_ctrl->qctrl.maximum)
2228 else if (gain < gain_ctrl->qctrl.maximum)
2235 if (gain != orig_gain) {
2236 gain_ctrl->set(gspca_dev, gain);
2239 if (exposure != orig_exposure) {
2240 exposure_ctrl->set(gspca_dev, exposure);
2246 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2248 /* -- module insert / remove -- */
2249 static int __init gspca_init(void)
2251 info("main v%d.%d.%d registered",
2252 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2253 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2254 DRIVER_VERSION_NUMBER & 0xff);
2257 static void __exit gspca_exit(void)
2259 info("main deregistered");
2262 module_init(gspca_init);
2263 module_exit(gspca_exit);
2266 module_param_named(debug, gspca_debug, int, 0644);
2267 MODULE_PARM_DESC(debug,
2268 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2269 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"