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);
144 pkt_scan = gspca_dev->sd_desc->pkt_scan;
145 for (i = 0; i < urb->number_of_packets; i++) {
147 /* check the availability of the frame buffer */
148 frame = gspca_get_i_frame(gspca_dev);
150 gspca_dev->last_packet_type = DISCARD_PACKET;
154 /* check the packet status and length */
155 len = urb->iso_frame_desc[i].actual_length;
157 if (gspca_dev->empty_packet == 0)
158 gspca_dev->empty_packet = 1;
161 st = urb->iso_frame_desc[i].status;
164 "ISOC data error: [%d] len=%d, status=%d",
166 gspca_dev->last_packet_type = DISCARD_PACKET;
170 /* let the packet be analyzed by the subdriver */
171 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
172 i, urb->iso_frame_desc[i].offset, len);
173 data = (u8 *) urb->transfer_buffer
174 + urb->iso_frame_desc[i].offset;
175 pkt_scan(gspca_dev, frame, data, len);
179 /* resubmit the URB */
180 st = usb_submit_urb(urb, GFP_ATOMIC);
182 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
186 * ISOC message interrupt from the USB device
188 * Analyse each packet and call the subdriver for copy to the frame buffer.
190 static void isoc_irq(struct urb *urb)
192 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
194 PDEBUG(D_PACK, "isoc irq");
195 if (!gspca_dev->streaming)
197 fill_frame(gspca_dev, urb);
201 * bulk message interrupt from the USB device
203 static void bulk_irq(struct urb *urb)
205 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
206 struct gspca_frame *frame;
209 PDEBUG(D_PACK, "bulk irq");
210 if (!gspca_dev->streaming)
212 switch (urb->status) {
216 return; /* disconnection */
222 if (gspca_dev->frozen)
225 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
229 /* check the availability of the frame buffer */
230 frame = gspca_get_i_frame(gspca_dev);
232 gspca_dev->last_packet_type = DISCARD_PACKET;
234 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
235 gspca_dev->sd_desc->pkt_scan(gspca_dev,
237 urb->transfer_buffer,
242 /* resubmit the URB */
243 if (gspca_dev->cam.bulk_nurbs != 0) {
244 st = usb_submit_urb(urb, GFP_ATOMIC);
246 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
251 * add data to the current frame
253 * This function is called by the subdrivers at interrupt level.
255 * To build a frame, these ones must add
257 * - 0 or many INTER_PACKETs
259 * DISCARD_PACKET invalidates the whole frame.
260 * On LAST_PACKET, a new frame is returned.
262 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
263 enum gspca_packet_type packet_type,
264 struct gspca_frame *frame,
270 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
272 /* when start of a new frame, if the current frame buffer
273 * is not queued, discard the whole frame */
274 if (packet_type == FIRST_PACKET) {
275 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
276 != V4L2_BUF_FLAG_QUEUED) {
277 gspca_dev->last_packet_type = DISCARD_PACKET;
280 frame->data_end = frame->data;
281 jiffies_to_timeval(get_jiffies_64(),
282 &frame->v4l2_buf.timestamp);
283 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
284 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
285 if (packet_type == LAST_PACKET)
286 gspca_dev->last_packet_type = packet_type;
290 /* append the packet to the frame buffer */
292 if (frame->data_end - frame->data + len
293 > frame->v4l2_buf.length) {
294 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
295 frame->data_end - frame->data + len,
296 frame->v4l2_buf.length);
297 packet_type = DISCARD_PACKET;
299 memcpy(frame->data_end, data, len);
300 frame->data_end += len;
303 gspca_dev->last_packet_type = packet_type;
305 /* if last packet, wake up the application and advance in the queue */
306 if (packet_type == LAST_PACKET) {
307 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
308 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
309 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
310 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
311 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
313 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
314 frame->v4l2_buf.bytesused,
318 j = gspca_dev->fr_queue[i];
319 frame = &gspca_dev->frame[j];
323 EXPORT_SYMBOL(gspca_frame_add);
325 static int gspca_is_compressed(__u32 format)
328 case V4L2_PIX_FMT_MJPEG:
329 case V4L2_PIX_FMT_JPEG:
330 case V4L2_PIX_FMT_SPCA561:
331 case V4L2_PIX_FMT_PAC207:
332 case V4L2_PIX_FMT_MR97310A:
338 static void *rvmalloc(unsigned long size)
343 mem = vmalloc_32(size);
345 adr = (unsigned long) mem;
346 while ((long) size > 0) {
347 SetPageReserved(vmalloc_to_page((void *) adr));
355 static void rvfree(void *mem, long size)
359 adr = (unsigned long) mem;
361 ClearPageReserved(vmalloc_to_page((void *) adr));
368 static int frame_alloc(struct gspca_dev *gspca_dev,
371 struct gspca_frame *frame;
375 i = gspca_dev->curr_mode;
376 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
377 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
378 frsz = PAGE_ALIGN(frsz);
379 gspca_dev->frsz = frsz;
380 if (count > GSPCA_MAX_FRAMES)
381 count = GSPCA_MAX_FRAMES;
382 gspca_dev->frbuf = rvmalloc(frsz * count);
383 if (!gspca_dev->frbuf) {
384 err("frame alloc failed");
387 gspca_dev->nframes = count;
388 for (i = 0; i < count; i++) {
389 frame = &gspca_dev->frame[i];
390 frame->v4l2_buf.index = i;
391 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
392 frame->v4l2_buf.flags = 0;
393 frame->v4l2_buf.field = V4L2_FIELD_NONE;
394 frame->v4l2_buf.length = frsz;
395 frame->v4l2_buf.memory = gspca_dev->memory;
396 frame->v4l2_buf.sequence = 0;
397 frame->data = frame->data_end =
398 gspca_dev->frbuf + i * frsz;
399 frame->v4l2_buf.m.offset = i * frsz;
401 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
402 gspca_dev->last_packet_type = DISCARD_PACKET;
403 gspca_dev->sequence = 0;
407 static void frame_free(struct gspca_dev *gspca_dev)
411 PDEBUG(D_STREAM, "frame free");
412 if (gspca_dev->frbuf != NULL) {
413 rvfree(gspca_dev->frbuf,
414 gspca_dev->nframes * gspca_dev->frsz);
415 gspca_dev->frbuf = NULL;
416 for (i = 0; i < gspca_dev->nframes; i++)
417 gspca_dev->frame[i].data = NULL;
419 gspca_dev->nframes = 0;
422 static void destroy_urbs(struct gspca_dev *gspca_dev)
427 PDEBUG(D_STREAM, "kill transfer");
428 for (i = 0; i < MAX_NURBS; i++) {
429 urb = gspca_dev->urb[i];
433 gspca_dev->urb[i] = NULL;
435 if (urb->transfer_buffer != NULL)
436 usb_buffer_free(gspca_dev->dev,
437 urb->transfer_buffer_length,
438 urb->transfer_buffer,
445 * look for an input transfer endpoint in an alternate setting
447 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
450 struct usb_host_endpoint *ep;
453 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
454 ep = &alt->endpoint[i];
455 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
457 && ep->desc.wMaxPacketSize != 0)
464 * look for an input (isoc or bulk) endpoint
466 * The endpoint is defined by the subdriver.
467 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
468 * This routine may be called many times when the bandwidth is too small
469 * (the bandwidth is checked on urb submit).
471 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
473 struct usb_interface *intf;
474 struct usb_host_endpoint *ep;
477 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
479 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
480 : USB_ENDPOINT_XFER_ISOC;
481 i = gspca_dev->alt; /* previous alt setting */
482 if (gspca_dev->cam.reverse_alts) {
483 while (++i < gspca_dev->nbalt) {
484 ep = alt_xfer(&intf->altsetting[i], xfer);
490 ep = alt_xfer(&intf->altsetting[i], xfer);
496 err("no transfer endpoint found");
499 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
500 i, ep->desc.bEndpointAddress);
501 gspca_dev->alt = i; /* memorize the current alt setting */
502 if (gspca_dev->nbalt > 1) {
503 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
505 err("set alt %d err %d", i, ret);
513 * create the URBs for image transfer
515 static int create_urbs(struct gspca_dev *gspca_dev,
516 struct usb_host_endpoint *ep)
519 int n, nurbs, i, psize, npkt, bsize;
521 /* calculate the packet size and the number of packets */
522 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
524 if (!gspca_dev->cam.bulk) { /* isoc */
526 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
527 if (gspca_dev->pkt_size == 0)
528 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
530 psize = gspca_dev->pkt_size;
531 npkt = gspca_dev->cam.npkt;
533 npkt = 32; /* default value */
534 bsize = psize * npkt;
536 "isoc %d pkts size %d = bsize:%d",
541 bsize = gspca_dev->cam.bulk_size;
544 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
545 if (gspca_dev->cam.bulk_nurbs != 0)
546 nurbs = gspca_dev->cam.bulk_nurbs;
551 gspca_dev->nurbs = nurbs;
552 for (n = 0; n < nurbs; n++) {
553 urb = usb_alloc_urb(npkt, GFP_KERNEL);
555 err("usb_alloc_urb failed");
556 destroy_urbs(gspca_dev);
559 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
564 if (urb->transfer_buffer == NULL) {
566 err("usb_buffer_urb failed");
567 destroy_urbs(gspca_dev);
570 gspca_dev->urb[n] = urb;
571 urb->dev = gspca_dev->dev;
572 urb->context = gspca_dev;
573 urb->transfer_buffer_length = bsize;
574 if (npkt != 0) { /* ISOC */
575 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
576 ep->desc.bEndpointAddress);
577 urb->transfer_flags = URB_ISO_ASAP
578 | URB_NO_TRANSFER_DMA_MAP;
579 urb->interval = ep->desc.bInterval;
580 urb->complete = isoc_irq;
581 urb->number_of_packets = npkt;
582 for (i = 0; i < npkt; i++) {
583 urb->iso_frame_desc[i].length = psize;
584 urb->iso_frame_desc[i].offset = psize * i;
587 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
588 ep->desc.bEndpointAddress),
589 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
590 urb->complete = bulk_irq;
597 * start the USB transfer
599 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
601 struct usb_host_endpoint *ep;
604 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
607 if (!gspca_dev->present) {
612 /* set the higher alternate setting and
613 * loop until urb submit succeeds */
614 if (gspca_dev->cam.reverse_alts)
617 gspca_dev->alt = gspca_dev->nbalt;
619 if (gspca_dev->sd_desc->isoc_init) {
620 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
624 ep = get_ep(gspca_dev);
630 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
631 ret = create_urbs(gspca_dev, ep);
635 /* clear the bulk endpoint */
636 if (gspca_dev->cam.bulk)
637 usb_clear_halt(gspca_dev->dev,
638 gspca_dev->urb[0]->pipe);
641 ret = gspca_dev->sd_desc->start(gspca_dev);
643 destroy_urbs(gspca_dev);
646 gspca_dev->streaming = 1;
648 /* some bulk transfers are started by the subdriver */
649 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
652 /* submit the URBs */
653 for (n = 0; n < gspca_dev->nurbs; n++) {
654 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
660 gspca_dev->streaming = 0;
661 destroy_urbs(gspca_dev);
662 if (ret != -ENOSPC) {
663 PDEBUG(D_ERR|D_STREAM,
664 "usb_submit_urb alt %d err %d",
665 gspca_dev->alt, ret);
669 /* the bandwidth is not wide enough
670 * negociate or try a lower alternate setting */
671 PDEBUG(D_ERR|D_STREAM,
672 "bandwidth not wide enough - trying again");
673 msleep(20); /* wait for kill complete */
674 if (gspca_dev->sd_desc->isoc_nego) {
675 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
679 ep = get_ep(gspca_dev);
687 mutex_unlock(&gspca_dev->usb_lock);
691 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
695 if (gspca_dev->alt == 0)
697 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
699 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
703 /* Note: both the queue and the usb locks should be held when calling this */
704 static void gspca_stream_off(struct gspca_dev *gspca_dev)
706 gspca_dev->streaming = 0;
707 if (gspca_dev->present) {
708 if (gspca_dev->sd_desc->stopN)
709 gspca_dev->sd_desc->stopN(gspca_dev);
710 destroy_urbs(gspca_dev);
711 gspca_set_alt0(gspca_dev);
714 /* always call stop0 to free the subdriver's resources */
715 if (gspca_dev->sd_desc->stop0)
716 gspca_dev->sd_desc->stop0(gspca_dev);
717 PDEBUG(D_STREAM, "stream off OK");
720 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
724 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
725 gspca_dev->curr_mode = i;
726 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
727 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
728 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
731 static int wxh_to_mode(struct gspca_dev *gspca_dev,
732 int width, int height)
736 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
737 if (width >= gspca_dev->cam.cam_mode[i].width
738 && height >= gspca_dev->cam.cam_mode[i].height)
745 * search a mode with the right pixel format
747 static int gspca_get_mode(struct gspca_dev *gspca_dev,
753 modeU = modeD = mode;
754 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
756 if (gspca_dev->cam.cam_mode[modeD].pixelformat
760 if (++modeU < gspca_dev->cam.nmodes) {
761 if (gspca_dev->cam.cam_mode[modeU].pixelformat
769 #ifdef CONFIG_VIDEO_ADV_DEBUG
770 static int vidioc_g_register(struct file *file, void *priv,
771 struct v4l2_dbg_register *reg)
774 struct gspca_dev *gspca_dev = priv;
776 if (!gspca_dev->sd_desc->get_chip_ident)
779 if (!gspca_dev->sd_desc->get_register)
782 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
784 if (gspca_dev->present)
785 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
788 mutex_unlock(&gspca_dev->usb_lock);
793 static int vidioc_s_register(struct file *file, void *priv,
794 struct v4l2_dbg_register *reg)
797 struct gspca_dev *gspca_dev = priv;
799 if (!gspca_dev->sd_desc->get_chip_ident)
802 if (!gspca_dev->sd_desc->set_register)
805 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
807 if (gspca_dev->present)
808 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
811 mutex_unlock(&gspca_dev->usb_lock);
817 static int vidioc_g_chip_ident(struct file *file, void *priv,
818 struct v4l2_dbg_chip_ident *chip)
821 struct gspca_dev *gspca_dev = priv;
823 if (!gspca_dev->sd_desc->get_chip_ident)
826 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
828 if (gspca_dev->present)
829 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
832 mutex_unlock(&gspca_dev->usb_lock);
837 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
838 struct v4l2_fmtdesc *fmtdesc)
840 struct gspca_dev *gspca_dev = priv;
844 /* give an index to each format */
847 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
848 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
851 if (fmt_tb[j] == fmt_tb[index])
856 if (fmtdesc->index == index)
857 break; /* new format */
859 if (index >= ARRAY_SIZE(fmt_tb))
864 return -EINVAL; /* no more format */
866 fmtdesc->pixelformat = fmt_tb[index];
867 if (gspca_is_compressed(fmt_tb[index]))
868 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
869 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
870 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
871 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
872 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
873 fmtdesc->description[4] = '\0';
877 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_format *fmt)
880 struct gspca_dev *gspca_dev = priv;
883 mode = gspca_dev->curr_mode;
884 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
885 sizeof fmt->fmt.pix);
889 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
890 struct v4l2_format *fmt)
892 int w, h, mode, mode2;
894 w = fmt->fmt.pix.width;
895 h = fmt->fmt.pix.height;
898 if (gspca_debug & D_CONF)
899 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
901 /* search the closest mode for width and height */
902 mode = wxh_to_mode(gspca_dev, w, h);
904 /* OK if right palette */
905 if (gspca_dev->cam.cam_mode[mode].pixelformat
906 != fmt->fmt.pix.pixelformat) {
908 /* else, search the closest mode with the same pixel format */
909 mode2 = gspca_get_mode(gspca_dev, mode,
910 fmt->fmt.pix.pixelformat);
914 ; * no chance, return this mode */
916 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
917 sizeof fmt->fmt.pix);
918 return mode; /* used when s_fmt */
921 static int vidioc_try_fmt_vid_cap(struct file *file,
923 struct v4l2_format *fmt)
925 struct gspca_dev *gspca_dev = priv;
928 ret = try_fmt_vid_cap(gspca_dev, fmt);
934 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
935 struct v4l2_format *fmt)
937 struct gspca_dev *gspca_dev = priv;
940 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
943 ret = try_fmt_vid_cap(gspca_dev, fmt);
947 if (gspca_dev->nframes != 0
948 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
953 if (ret == gspca_dev->curr_mode) {
955 goto out; /* same mode */
958 if (gspca_dev->streaming) {
962 gspca_dev->width = fmt->fmt.pix.width;
963 gspca_dev->height = fmt->fmt.pix.height;
964 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
965 gspca_dev->curr_mode = ret;
969 mutex_unlock(&gspca_dev->queue_lock);
973 static int vidioc_enum_framesizes(struct file *file, void *priv,
974 struct v4l2_frmsizeenum *fsize)
976 struct gspca_dev *gspca_dev = priv;
980 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
981 if (fsize->pixel_format !=
982 gspca_dev->cam.cam_mode[i].pixelformat)
985 if (fsize->index == index) {
986 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
987 fsize->discrete.width =
988 gspca_dev->cam.cam_mode[i].width;
989 fsize->discrete.height =
990 gspca_dev->cam.cam_mode[i].height;
999 static void gspca_release(struct video_device *vfd)
1001 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1003 PDEBUG(D_PROBE, "/dev/video%d released", gspca_dev->vdev.num);
1005 kfree(gspca_dev->usb_buf);
1009 static int dev_open(struct file *file)
1011 struct gspca_dev *gspca_dev;
1014 PDEBUG(D_STREAM, "[%s] open", current->comm);
1015 gspca_dev = (struct gspca_dev *) video_devdata(file);
1016 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1017 return -ERESTARTSYS;
1018 if (!gspca_dev->present) {
1023 if (gspca_dev->users > 4) { /* (arbitrary value) */
1028 /* protect the subdriver against rmmod */
1029 if (!try_module_get(gspca_dev->module)) {
1036 file->private_data = gspca_dev;
1038 /* activate the v4l2 debug */
1039 if (gspca_debug & D_V4L2)
1040 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1041 | V4L2_DEBUG_IOCTL_ARG;
1043 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1044 | V4L2_DEBUG_IOCTL_ARG);
1048 mutex_unlock(&gspca_dev->queue_lock);
1050 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1052 PDEBUG(D_STREAM, "open done");
1056 static int dev_close(struct file *file)
1058 struct gspca_dev *gspca_dev = file->private_data;
1060 PDEBUG(D_STREAM, "[%s] close", current->comm);
1061 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1062 return -ERESTARTSYS;
1065 /* if the file did the capture, free the streaming resources */
1066 if (gspca_dev->capt_file == file) {
1067 if (gspca_dev->streaming) {
1068 mutex_lock(&gspca_dev->usb_lock);
1069 gspca_stream_off(gspca_dev);
1070 mutex_unlock(&gspca_dev->usb_lock);
1072 frame_free(gspca_dev);
1073 gspca_dev->capt_file = NULL;
1074 gspca_dev->memory = GSPCA_MEMORY_NO;
1076 file->private_data = NULL;
1077 module_put(gspca_dev->module);
1078 mutex_unlock(&gspca_dev->queue_lock);
1080 PDEBUG(D_STREAM, "close done");
1085 static int vidioc_querycap(struct file *file, void *priv,
1086 struct v4l2_capability *cap)
1088 struct gspca_dev *gspca_dev = priv;
1091 /* protect the access to the usb device */
1092 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1093 return -ERESTARTSYS;
1094 if (!gspca_dev->present) {
1098 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1099 if (gspca_dev->dev->product != NULL) {
1100 strncpy(cap->card, gspca_dev->dev->product,
1103 snprintf(cap->card, sizeof cap->card,
1104 "USB Camera (%04x:%04x)",
1105 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1106 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1108 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1109 cap->version = DRIVER_VERSION_NUMBER;
1110 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1111 | V4L2_CAP_STREAMING
1112 | V4L2_CAP_READWRITE;
1115 mutex_unlock(&gspca_dev->usb_lock);
1119 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1122 const struct ctrl *ctrls;
1125 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1126 i < gspca_dev->sd_desc->nctrls;
1128 if (gspca_dev->ctrl_dis & (1 << i))
1130 if (id == ctrls->qctrl.id)
1136 static int vidioc_queryctrl(struct file *file, void *priv,
1137 struct v4l2_queryctrl *q_ctrl)
1139 struct gspca_dev *gspca_dev = priv;
1140 const struct ctrl *ctrls;
1146 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1147 id &= V4L2_CTRL_ID_MASK;
1149 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1150 if (gspca_dev->ctrl_dis & (1 << i))
1152 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1154 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1157 ctrls = &gspca_dev->sd_desc->ctrls[i];
1160 ctrls = get_ctrl(gspca_dev, id);
1161 i = ctrls - gspca_dev->sd_desc->ctrls;
1165 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1166 if (gspca_dev->ctrl_inac & (1 << i))
1167 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1171 static int vidioc_s_ctrl(struct file *file, void *priv,
1172 struct v4l2_control *ctrl)
1174 struct gspca_dev *gspca_dev = priv;
1175 const struct ctrl *ctrls;
1178 ctrls = get_ctrl(gspca_dev, ctrl->id);
1182 if (ctrl->value < ctrls->qctrl.minimum
1183 || ctrl->value > ctrls->qctrl.maximum)
1185 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1186 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1187 return -ERESTARTSYS;
1188 if (gspca_dev->present)
1189 ret = ctrls->set(gspca_dev, ctrl->value);
1192 mutex_unlock(&gspca_dev->usb_lock);
1196 static int vidioc_g_ctrl(struct file *file, void *priv,
1197 struct v4l2_control *ctrl)
1199 struct gspca_dev *gspca_dev = priv;
1200 const struct ctrl *ctrls;
1203 ctrls = get_ctrl(gspca_dev, ctrl->id);
1207 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1208 return -ERESTARTSYS;
1209 if (gspca_dev->present)
1210 ret = ctrls->get(gspca_dev, &ctrl->value);
1213 mutex_unlock(&gspca_dev->usb_lock);
1217 /*fixme: have an audio flag in gspca_dev?*/
1218 static int vidioc_s_audio(struct file *file, void *priv,
1219 struct v4l2_audio *audio)
1221 if (audio->index != 0)
1226 static int vidioc_g_audio(struct file *file, void *priv,
1227 struct v4l2_audio *audio)
1229 strcpy(audio->name, "Microphone");
1233 static int vidioc_enumaudio(struct file *file, void *priv,
1234 struct v4l2_audio *audio)
1236 if (audio->index != 0)
1239 strcpy(audio->name, "Microphone");
1240 audio->capability = 0;
1245 static int vidioc_querymenu(struct file *file, void *priv,
1246 struct v4l2_querymenu *qmenu)
1248 struct gspca_dev *gspca_dev = priv;
1250 if (!gspca_dev->sd_desc->querymenu)
1252 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1255 static int vidioc_enum_input(struct file *file, void *priv,
1256 struct v4l2_input *input)
1258 struct gspca_dev *gspca_dev = priv;
1260 if (input->index != 0)
1262 input->type = V4L2_INPUT_TYPE_CAMERA;
1263 input->status = gspca_dev->cam.input_flags;
1264 strncpy(input->name, gspca_dev->sd_desc->name,
1265 sizeof input->name);
1269 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1275 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1282 static int vidioc_reqbufs(struct file *file, void *priv,
1283 struct v4l2_requestbuffers *rb)
1285 struct gspca_dev *gspca_dev = priv;
1288 switch (rb->memory) {
1289 case GSPCA_MEMORY_READ: /* (internal call) */
1290 case V4L2_MEMORY_MMAP:
1291 case V4L2_MEMORY_USERPTR:
1296 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1297 return -ERESTARTSYS;
1299 if (gspca_dev->memory != GSPCA_MEMORY_NO
1300 && gspca_dev->memory != rb->memory) {
1305 /* only one file may do the capture */
1306 if (gspca_dev->capt_file != NULL
1307 && gspca_dev->capt_file != file) {
1312 /* if allocated, the buffers must not be mapped */
1313 for (i = 0; i < gspca_dev->nframes; i++) {
1314 if (gspca_dev->frame[i].vma_use_count) {
1320 /* stop streaming */
1321 if (gspca_dev->streaming) {
1322 mutex_lock(&gspca_dev->usb_lock);
1323 gspca_stream_off(gspca_dev);
1324 mutex_unlock(&gspca_dev->usb_lock);
1327 /* free the previous allocated buffers, if any */
1328 if (gspca_dev->nframes != 0) {
1329 frame_free(gspca_dev);
1330 gspca_dev->capt_file = NULL;
1332 if (rb->count == 0) /* unrequest */
1334 gspca_dev->memory = rb->memory;
1335 ret = frame_alloc(gspca_dev, rb->count);
1337 rb->count = gspca_dev->nframes;
1338 gspca_dev->capt_file = file;
1341 mutex_unlock(&gspca_dev->queue_lock);
1342 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1346 static int vidioc_querybuf(struct file *file, void *priv,
1347 struct v4l2_buffer *v4l2_buf)
1349 struct gspca_dev *gspca_dev = priv;
1350 struct gspca_frame *frame;
1352 if (v4l2_buf->index < 0
1353 || v4l2_buf->index >= gspca_dev->nframes)
1356 frame = &gspca_dev->frame[v4l2_buf->index];
1357 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1361 static int vidioc_streamon(struct file *file, void *priv,
1362 enum v4l2_buf_type buf_type)
1364 struct gspca_dev *gspca_dev = priv;
1367 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1369 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1370 return -ERESTARTSYS;
1372 if (gspca_dev->nframes == 0
1373 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1377 if (!gspca_dev->streaming) {
1378 ret = gspca_init_transfer(gspca_dev);
1383 if (gspca_debug & D_STREAM) {
1384 PDEBUG_MODE("stream on OK",
1392 mutex_unlock(&gspca_dev->queue_lock);
1396 static int vidioc_streamoff(struct file *file, void *priv,
1397 enum v4l2_buf_type buf_type)
1399 struct gspca_dev *gspca_dev = priv;
1402 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1404 if (!gspca_dev->streaming)
1406 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1407 return -ERESTARTSYS;
1409 /* stop streaming */
1410 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1414 gspca_stream_off(gspca_dev);
1415 mutex_unlock(&gspca_dev->usb_lock);
1417 /* empty the application queues */
1418 for (i = 0; i < gspca_dev->nframes; i++)
1419 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1420 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1421 gspca_dev->last_packet_type = DISCARD_PACKET;
1422 gspca_dev->sequence = 0;
1425 mutex_unlock(&gspca_dev->queue_lock);
1429 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1430 struct v4l2_jpegcompression *jpegcomp)
1432 struct gspca_dev *gspca_dev = priv;
1435 if (!gspca_dev->sd_desc->get_jcomp)
1437 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1438 return -ERESTARTSYS;
1439 if (gspca_dev->present)
1440 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1443 mutex_unlock(&gspca_dev->usb_lock);
1447 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1448 struct v4l2_jpegcompression *jpegcomp)
1450 struct gspca_dev *gspca_dev = priv;
1453 if (!gspca_dev->sd_desc->set_jcomp)
1455 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1456 return -ERESTARTSYS;
1457 if (gspca_dev->present)
1458 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1461 mutex_unlock(&gspca_dev->usb_lock);
1465 static int vidioc_g_parm(struct file *filp, void *priv,
1466 struct v4l2_streamparm *parm)
1468 struct gspca_dev *gspca_dev = priv;
1470 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1472 if (gspca_dev->sd_desc->get_streamparm) {
1475 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1476 return -ERESTARTSYS;
1477 if (gspca_dev->present)
1478 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1482 mutex_unlock(&gspca_dev->usb_lock);
1489 static int vidioc_s_parm(struct file *filp, void *priv,
1490 struct v4l2_streamparm *parm)
1492 struct gspca_dev *gspca_dev = priv;
1495 n = parm->parm.capture.readbuffers;
1496 if (n == 0 || n > GSPCA_MAX_FRAMES)
1497 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1499 gspca_dev->nbufread = n;
1501 if (gspca_dev->sd_desc->set_streamparm) {
1504 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1505 return -ERESTARTSYS;
1506 if (gspca_dev->present)
1507 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1511 mutex_unlock(&gspca_dev->usb_lock);
1518 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1519 static int vidiocgmbuf(struct file *file, void *priv,
1520 struct video_mbuf *mbuf)
1522 struct gspca_dev *gspca_dev = file->private_data;
1525 PDEBUG(D_STREAM, "cgmbuf");
1526 if (gspca_dev->nframes == 0) {
1530 struct v4l2_format fmt;
1532 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1533 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1534 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1535 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1536 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1537 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1542 struct v4l2_requestbuffers rb;
1544 memset(&rb, 0, sizeof rb);
1546 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1547 rb.memory = V4L2_MEMORY_MMAP;
1548 ret = vidioc_reqbufs(file, priv, &rb);
1553 mbuf->frames = gspca_dev->nframes;
1554 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1555 for (i = 0; i < mbuf->frames; i++)
1556 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1561 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1563 struct gspca_dev *gspca_dev = file->private_data;
1564 struct gspca_frame *frame;
1566 unsigned long addr, start, size;
1569 start = vma->vm_start;
1570 size = vma->vm_end - vma->vm_start;
1571 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1573 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1574 return -ERESTARTSYS;
1575 if (!gspca_dev->present) {
1579 if (gspca_dev->capt_file != file) {
1585 for (i = 0; i < gspca_dev->nframes; ++i) {
1586 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1587 PDEBUG(D_STREAM, "mmap bad memory type");
1590 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1592 frame = &gspca_dev->frame[i];
1596 if (frame == NULL) {
1597 PDEBUG(D_STREAM, "mmap no frame buffer found");
1601 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1602 /* v4l1 maps all the buffers */
1604 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1606 if (size != frame->v4l2_buf.length) {
1607 PDEBUG(D_STREAM, "mmap bad size");
1613 * - VM_IO marks the area as being a mmaped region for I/O to a
1614 * device. It also prevents the region from being core dumped.
1616 vma->vm_flags |= VM_IO;
1618 addr = (unsigned long) frame->data;
1620 page = vmalloc_to_page((void *) addr);
1621 ret = vm_insert_page(vma, start, page);
1629 vma->vm_ops = (struct vm_operations_struct *) &gspca_vm_ops;
1630 vma->vm_private_data = frame;
1634 mutex_unlock(&gspca_dev->queue_lock);
1639 * wait for a video frame
1641 * If a frame is ready, its index is returned.
1643 static int frame_wait(struct gspca_dev *gspca_dev,
1646 struct gspca_frame *frame;
1649 /* check if a frame is ready */
1650 i = gspca_dev->fr_o;
1651 j = gspca_dev->fr_queue[i];
1652 frame = &gspca_dev->frame[j];
1654 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1658 /* wait till a frame is ready */
1659 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1660 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1661 !gspca_dev->streaming || !gspca_dev->present,
1662 msecs_to_jiffies(3000));
1665 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1669 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1670 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1675 if (gspca_dev->sd_desc->dq_callback) {
1676 mutex_lock(&gspca_dev->usb_lock);
1677 if (gspca_dev->present)
1678 gspca_dev->sd_desc->dq_callback(gspca_dev);
1679 mutex_unlock(&gspca_dev->usb_lock);
1685 * dequeue a video buffer
1687 * If nonblock_ing is false, block until a buffer is available.
1689 static int vidioc_dqbuf(struct file *file, void *priv,
1690 struct v4l2_buffer *v4l2_buf)
1692 struct gspca_dev *gspca_dev = priv;
1693 struct gspca_frame *frame;
1696 PDEBUG(D_FRAM, "dqbuf");
1697 if (v4l2_buf->memory != gspca_dev->memory)
1700 if (!gspca_dev->present)
1703 /* if not streaming, be sure the application will not loop forever */
1704 if (!(file->f_flags & O_NONBLOCK)
1705 && !gspca_dev->streaming && gspca_dev->users == 1)
1708 /* only the capturing file may dequeue */
1709 if (gspca_dev->capt_file != file)
1712 /* only one dequeue / read at a time */
1713 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1714 return -ERESTARTSYS;
1716 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1719 i = ret; /* frame index */
1720 frame = &gspca_dev->frame[i];
1721 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1722 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1724 frame->v4l2_buf.bytesused)) {
1725 PDEBUG(D_ERR|D_STREAM,
1726 "dqbuf cp to user failed");
1731 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1732 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1733 PDEBUG(D_FRAM, "dqbuf %d", i);
1736 mutex_unlock(&gspca_dev->read_lock);
1741 * queue a video buffer
1743 * Attempting to queue a buffer that has already been
1744 * queued will return -EINVAL.
1746 static int vidioc_qbuf(struct file *file, void *priv,
1747 struct v4l2_buffer *v4l2_buf)
1749 struct gspca_dev *gspca_dev = priv;
1750 struct gspca_frame *frame;
1753 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1755 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1756 return -ERESTARTSYS;
1758 index = v4l2_buf->index;
1759 if ((unsigned) index >= gspca_dev->nframes) {
1761 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1765 if (v4l2_buf->memory != gspca_dev->memory) {
1766 PDEBUG(D_FRAM, "qbuf bad memory type");
1771 frame = &gspca_dev->frame[index];
1772 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1773 PDEBUG(D_FRAM, "qbuf bad state");
1778 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1780 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1781 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1782 frame->v4l2_buf.length = v4l2_buf->length;
1785 /* put the buffer in the 'queued' queue */
1786 i = gspca_dev->fr_q;
1787 gspca_dev->fr_queue[i] = index;
1788 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1789 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1794 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1795 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1798 mutex_unlock(&gspca_dev->queue_lock);
1803 * allocate the resources for read()
1805 static int read_alloc(struct gspca_dev *gspca_dev,
1808 struct v4l2_buffer v4l2_buf;
1811 PDEBUG(D_STREAM, "read alloc");
1812 if (gspca_dev->nframes == 0) {
1813 struct v4l2_requestbuffers rb;
1815 memset(&rb, 0, sizeof rb);
1816 rb.count = gspca_dev->nbufread;
1817 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1818 rb.memory = GSPCA_MEMORY_READ;
1819 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1821 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1824 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1825 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1826 v4l2_buf.memory = GSPCA_MEMORY_READ;
1827 for (i = 0; i < gspca_dev->nbufread; i++) {
1829 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1831 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1835 gspca_dev->memory = GSPCA_MEMORY_READ;
1838 /* start streaming */
1839 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1841 PDEBUG(D_STREAM, "read streamon err %d", ret);
1845 static unsigned int dev_poll(struct file *file, poll_table *wait)
1847 struct gspca_dev *gspca_dev = file->private_data;
1850 PDEBUG(D_FRAM, "poll");
1852 poll_wait(file, &gspca_dev->wq, wait);
1854 /* if reqbufs is not done, the user would use read() */
1855 if (gspca_dev->nframes == 0) {
1856 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1857 return POLLERR; /* not the 1st time */
1858 ret = read_alloc(gspca_dev, file);
1863 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1866 /* check the next incoming buffer */
1867 i = gspca_dev->fr_o;
1868 i = gspca_dev->fr_queue[i];
1869 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1870 ret = POLLIN | POLLRDNORM; /* something to read */
1873 mutex_unlock(&gspca_dev->queue_lock);
1874 if (!gspca_dev->present)
1879 static ssize_t dev_read(struct file *file, char __user *data,
1880 size_t count, loff_t *ppos)
1882 struct gspca_dev *gspca_dev = file->private_data;
1883 struct gspca_frame *frame;
1884 struct v4l2_buffer v4l2_buf;
1885 struct timeval timestamp;
1888 PDEBUG(D_FRAM, "read (%zd)", count);
1889 if (!gspca_dev->present)
1891 switch (gspca_dev->memory) {
1892 case GSPCA_MEMORY_NO: /* first time */
1893 ret = read_alloc(gspca_dev, file);
1897 case GSPCA_MEMORY_READ:
1898 if (gspca_dev->capt_file == file)
1906 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1910 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1911 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1912 v4l2_buf.memory = GSPCA_MEMORY_READ;
1913 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1915 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1919 /* if the process slept for more than 1 second,
1920 * get a newer frame */
1921 frame = &gspca_dev->frame[v4l2_buf.index];
1923 break; /* avoid infinite loop */
1924 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1926 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1928 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1933 /* copy the frame */
1934 if (count > frame->v4l2_buf.bytesused)
1935 count = frame->v4l2_buf.bytesused;
1936 ret = copy_to_user(data, frame->data, count);
1938 PDEBUG(D_ERR|D_STREAM,
1939 "read cp to user lack %d / %zd", ret, count);
1945 /* in each case, requeue the buffer */
1946 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1952 static struct v4l2_file_operations dev_fops = {
1953 .owner = THIS_MODULE,
1955 .release = dev_close,
1958 .unlocked_ioctl = video_ioctl2,
1962 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1963 .vidioc_querycap = vidioc_querycap,
1964 .vidioc_dqbuf = vidioc_dqbuf,
1965 .vidioc_qbuf = vidioc_qbuf,
1966 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1967 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1968 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1969 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1970 .vidioc_streamon = vidioc_streamon,
1971 .vidioc_queryctrl = vidioc_queryctrl,
1972 .vidioc_g_ctrl = vidioc_g_ctrl,
1973 .vidioc_s_ctrl = vidioc_s_ctrl,
1974 .vidioc_g_audio = vidioc_g_audio,
1975 .vidioc_s_audio = vidioc_s_audio,
1976 .vidioc_enumaudio = vidioc_enumaudio,
1977 .vidioc_querymenu = vidioc_querymenu,
1978 .vidioc_enum_input = vidioc_enum_input,
1979 .vidioc_g_input = vidioc_g_input,
1980 .vidioc_s_input = vidioc_s_input,
1981 .vidioc_reqbufs = vidioc_reqbufs,
1982 .vidioc_querybuf = vidioc_querybuf,
1983 .vidioc_streamoff = vidioc_streamoff,
1984 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1985 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1986 .vidioc_g_parm = vidioc_g_parm,
1987 .vidioc_s_parm = vidioc_s_parm,
1988 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1989 #ifdef CONFIG_VIDEO_ADV_DEBUG
1990 .vidioc_g_register = vidioc_g_register,
1991 .vidioc_s_register = vidioc_s_register,
1993 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1994 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1995 .vidiocgmbuf = vidiocgmbuf,
1999 static struct video_device gspca_template = {
2000 .name = "gspca main driver",
2002 .ioctl_ops = &dev_ioctl_ops,
2003 .release = gspca_release,
2008 * probe and create a new gspca device
2010 * This function must be called by the sub-driver when it is
2011 * called for probing a new device.
2013 int gspca_dev_probe(struct usb_interface *intf,
2014 const struct usb_device_id *id,
2015 const struct sd_desc *sd_desc,
2017 struct module *module)
2019 struct usb_interface_descriptor *interface;
2020 struct gspca_dev *gspca_dev;
2021 struct usb_device *dev = interface_to_usbdev(intf);
2024 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2026 /* we don't handle multi-config cameras */
2027 if (dev->descriptor.bNumConfigurations != 1) {
2028 PDEBUG(D_ERR, "Too many config");
2031 interface = &intf->cur_altsetting->desc;
2032 if (interface->bInterfaceNumber > 0) {
2033 PDEBUG(D_ERR, "intf != 0");
2037 /* create the device */
2038 if (dev_size < sizeof *gspca_dev)
2039 dev_size = sizeof *gspca_dev;
2040 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2042 err("couldn't kzalloc gspca struct");
2045 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2046 if (!gspca_dev->usb_buf) {
2047 err("out of memory");
2051 gspca_dev->dev = dev;
2052 gspca_dev->iface = interface->bInterfaceNumber;
2053 gspca_dev->nbalt = intf->num_altsetting;
2054 gspca_dev->sd_desc = sd_desc;
2055 gspca_dev->nbufread = 2;
2056 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2058 /* configure the subdriver and initialize the USB device */
2059 ret = sd_desc->config(gspca_dev, id);
2062 ret = sd_desc->init(gspca_dev);
2065 ret = gspca_set_alt0(gspca_dev);
2068 gspca_set_default_mode(gspca_dev);
2070 mutex_init(&gspca_dev->usb_lock);
2071 mutex_init(&gspca_dev->read_lock);
2072 mutex_init(&gspca_dev->queue_lock);
2073 init_waitqueue_head(&gspca_dev->wq);
2075 /* init video stuff */
2076 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2077 gspca_dev->vdev.parent = &intf->dev;
2078 gspca_dev->module = module;
2079 gspca_dev->present = 1;
2080 ret = video_register_device(&gspca_dev->vdev,
2084 err("video_register_device err %d", ret);
2088 usb_set_intfdata(intf, gspca_dev);
2089 PDEBUG(D_PROBE, "/dev/video%d created", gspca_dev->vdev.num);
2092 kfree(gspca_dev->usb_buf);
2096 EXPORT_SYMBOL(gspca_dev_probe);
2101 * This function must be called by the sub-driver
2102 * when the device disconnects, after the specific resources are freed.
2104 void gspca_disconnect(struct usb_interface *intf)
2106 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2108 PDEBUG(D_PROBE, "/dev/video%d disconnect", gspca_dev->vdev.num);
2109 mutex_lock(&gspca_dev->usb_lock);
2110 gspca_dev->present = 0;
2112 if (gspca_dev->streaming) {
2113 destroy_urbs(gspca_dev);
2114 wake_up_interruptible(&gspca_dev->wq);
2117 /* the device is freed at exit of this function */
2118 gspca_dev->dev = NULL;
2119 mutex_unlock(&gspca_dev->usb_lock);
2121 usb_set_intfdata(intf, NULL);
2123 /* release the device */
2124 /* (this will call gspca_release() immediatly or on last close) */
2125 video_unregister_device(&gspca_dev->vdev);
2127 /* PDEBUG(D_PROBE, "disconnect complete"); */
2129 EXPORT_SYMBOL(gspca_disconnect);
2132 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2134 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2136 if (!gspca_dev->streaming)
2138 gspca_dev->frozen = 1; /* avoid urb error messages */
2139 if (gspca_dev->sd_desc->stopN)
2140 gspca_dev->sd_desc->stopN(gspca_dev);
2141 destroy_urbs(gspca_dev);
2142 gspca_set_alt0(gspca_dev);
2143 if (gspca_dev->sd_desc->stop0)
2144 gspca_dev->sd_desc->stop0(gspca_dev);
2147 EXPORT_SYMBOL(gspca_suspend);
2149 int gspca_resume(struct usb_interface *intf)
2151 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2153 gspca_dev->frozen = 0;
2154 gspca_dev->sd_desc->init(gspca_dev);
2155 if (gspca_dev->streaming)
2156 return gspca_init_transfer(gspca_dev);
2159 EXPORT_SYMBOL(gspca_resume);
2161 /* -- cam driver utility functions -- */
2163 /* auto gain and exposure algorithm based on the knee algorithm described here:
2164 http://ytse.tricolour.net/docs/LowLightOptimization.html
2166 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2168 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2169 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2171 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2172 const struct ctrl *gain_ctrl = NULL;
2173 const struct ctrl *exposure_ctrl = NULL;
2174 const struct ctrl *autogain_ctrl = NULL;
2177 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2178 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2179 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2180 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2181 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2182 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2183 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2185 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2186 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2187 "on cam without (auto)gain/exposure");
2191 if (gain_ctrl->get(gspca_dev, &gain) ||
2192 exposure_ctrl->get(gspca_dev, &exposure) ||
2193 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2197 orig_exposure = exposure;
2199 /* If we are of a multiple of deadzone, do multiple steps to reach the
2200 desired lumination fast (with the risc of a slight overshoot) */
2201 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2203 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2204 avg_lum, desired_avg_lum, steps);
2206 for (i = 0; i < steps; i++) {
2207 if (avg_lum > desired_avg_lum) {
2208 if (gain > gain_knee)
2210 else if (exposure > exposure_knee)
2212 else if (gain > gain_ctrl->qctrl.default_value)
2214 else if (exposure > exposure_ctrl->qctrl.minimum)
2216 else if (gain > gain_ctrl->qctrl.minimum)
2221 if (gain < gain_ctrl->qctrl.default_value)
2223 else if (exposure < exposure_knee)
2225 else if (gain < gain_knee)
2227 else if (exposure < exposure_ctrl->qctrl.maximum)
2229 else if (gain < gain_ctrl->qctrl.maximum)
2236 if (gain != orig_gain) {
2237 gain_ctrl->set(gspca_dev, gain);
2240 if (exposure != orig_exposure) {
2241 exposure_ctrl->set(gspca_dev, exposure);
2247 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2249 /* -- module insert / remove -- */
2250 static int __init gspca_init(void)
2252 info("main v%d.%d.%d registered",
2253 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2254 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2255 DRIVER_VERSION_NUMBER & 0xff);
2258 static void __exit gspca_exit(void)
2260 info("main deregistered");
2263 module_init(gspca_init);
2264 module_exit(gspca_exit);
2267 module_param_named(debug, gspca_debug, int, 0644);
2268 MODULE_PARM_DESC(debug,
2269 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2270 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"