2 * Main USB camera driver
4 * V4L2 by 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 2 /* default number of URBs */
43 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
44 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
45 MODULE_LICENSE("GPL");
47 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 5, 0)
50 int gspca_debug = D_ERR | D_PROBE;
51 EXPORT_SYMBOL(gspca_debug);
53 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
55 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
56 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
60 (pixfmt >> 16) & 0xff,
64 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
71 #define PDEBUG_MODE(txt, pixfmt, w, h)
74 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
75 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
76 #define GSPCA_MEMORY_READ 7
78 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
83 static void gspca_vm_open(struct vm_area_struct *vma)
85 struct gspca_frame *frame = vma->vm_private_data;
87 frame->vma_use_count++;
88 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
91 static void gspca_vm_close(struct vm_area_struct *vma)
93 struct gspca_frame *frame = vma->vm_private_data;
95 if (--frame->vma_use_count <= 0)
96 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
99 static struct vm_operations_struct gspca_vm_ops = {
100 .open = gspca_vm_open,
101 .close = gspca_vm_close,
104 /* get the current input frame buffer */
105 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
107 struct gspca_frame *frame;
111 i = gspca_dev->fr_queue[i];
112 frame = &gspca_dev->frame[i];
113 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
114 != V4L2_BUF_FLAG_QUEUED)
118 EXPORT_SYMBOL(gspca_get_i_frame);
121 * fill a video frame from an URB and resubmit
123 static void fill_frame(struct gspca_dev *gspca_dev,
126 struct gspca_frame *frame;
127 u8 *data; /* address of data in the iso message */
131 if (urb->status != 0) {
133 if (!gspca_dev->frozen)
135 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
136 return; /* disconnection ? */
138 pkt_scan = gspca_dev->sd_desc->pkt_scan;
139 for (i = 0; i < urb->number_of_packets; i++) {
141 /* check the availability of the frame buffer */
142 frame = gspca_get_i_frame(gspca_dev);
144 gspca_dev->last_packet_type = DISCARD_PACKET;
148 /* check the packet status and length */
149 len = urb->iso_frame_desc[i].actual_length;
151 if (gspca_dev->empty_packet == 0)
152 gspca_dev->empty_packet = 1;
155 st = urb->iso_frame_desc[i].status;
158 "ISOC data error: [%d] len=%d, status=%d",
160 gspca_dev->last_packet_type = DISCARD_PACKET;
164 /* let the packet be analyzed by the subdriver */
165 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
166 i, urb->iso_frame_desc[i].offset, len);
167 data = (u8 *) urb->transfer_buffer
168 + urb->iso_frame_desc[i].offset;
169 pkt_scan(gspca_dev, frame, data, len);
172 /* resubmit the URB */
173 st = usb_submit_urb(urb, GFP_ATOMIC);
175 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
179 * ISOC message interrupt from the USB device
181 * Analyse each packet and call the subdriver for copy to the frame buffer.
183 static void isoc_irq(struct urb *urb)
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
187 PDEBUG(D_PACK, "isoc irq");
188 if (!gspca_dev->streaming)
190 fill_frame(gspca_dev, urb);
194 * bulk message interrupt from the USB device
196 static void bulk_irq(struct urb *urb)
198 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
199 struct gspca_frame *frame;
202 PDEBUG(D_PACK, "bulk irq");
203 if (!gspca_dev->streaming)
205 switch (urb->status) {
213 if (!gspca_dev->frozen)
215 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
216 return; /* disconnection ? */
219 /* check the availability of the frame buffer */
220 frame = gspca_get_i_frame(gspca_dev);
222 gspca_dev->last_packet_type = DISCARD_PACKET;
224 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
225 gspca_dev->sd_desc->pkt_scan(gspca_dev,
227 urb->transfer_buffer,
231 /* resubmit the URB */
232 if (gspca_dev->cam.bulk_nurbs != 0) {
233 st = usb_submit_urb(urb, GFP_ATOMIC);
235 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
240 * add data to the current frame
242 * This function is called by the subdrivers at interrupt level.
244 * To build a frame, these ones must add
246 * - 0 or many INTER_PACKETs
248 * DISCARD_PACKET invalidates the whole frame.
249 * On LAST_PACKET, a new frame is returned.
251 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
252 enum gspca_packet_type packet_type,
253 struct gspca_frame *frame,
259 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
261 /* when start of a new frame, if the current frame buffer
262 * is not queued, discard the whole frame */
263 if (packet_type == FIRST_PACKET) {
264 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
265 != V4L2_BUF_FLAG_QUEUED) {
266 gspca_dev->last_packet_type = DISCARD_PACKET;
269 frame->data_end = frame->data;
270 jiffies_to_timeval(get_jiffies_64(),
271 &frame->v4l2_buf.timestamp);
272 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
273 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
274 if (packet_type == LAST_PACKET)
275 gspca_dev->last_packet_type = packet_type;
279 /* append the packet to the frame buffer */
281 if (frame->data_end - frame->data + len
282 > frame->v4l2_buf.length) {
283 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
284 frame->data_end - frame->data + len,
285 frame->v4l2_buf.length);
286 packet_type = DISCARD_PACKET;
288 memcpy(frame->data_end, data, len);
289 frame->data_end += len;
292 gspca_dev->last_packet_type = packet_type;
294 /* if last packet, wake up the application and advance in the queue */
295 if (packet_type == LAST_PACKET) {
296 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
297 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
298 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
299 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
300 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
302 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
303 frame->v4l2_buf.bytesused,
307 j = gspca_dev->fr_queue[i];
308 frame = &gspca_dev->frame[j];
312 EXPORT_SYMBOL(gspca_frame_add);
314 static int gspca_is_compressed(__u32 format)
317 case V4L2_PIX_FMT_MJPEG:
318 case V4L2_PIX_FMT_JPEG:
319 case V4L2_PIX_FMT_SPCA561:
320 case V4L2_PIX_FMT_PAC207:
321 case V4L2_PIX_FMT_MR97310A:
327 static void *rvmalloc(unsigned long size)
332 mem = vmalloc_32(size);
334 adr = (unsigned long) mem;
335 while ((long) size > 0) {
336 SetPageReserved(vmalloc_to_page((void *) adr));
344 static void rvfree(void *mem, long size)
348 adr = (unsigned long) mem;
350 ClearPageReserved(vmalloc_to_page((void *) adr));
357 static int frame_alloc(struct gspca_dev *gspca_dev,
360 struct gspca_frame *frame;
364 i = gspca_dev->curr_mode;
365 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
366 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
367 frsz = PAGE_ALIGN(frsz);
368 gspca_dev->frsz = frsz;
369 if (count > GSPCA_MAX_FRAMES)
370 count = GSPCA_MAX_FRAMES;
371 gspca_dev->frbuf = rvmalloc(frsz * count);
372 if (!gspca_dev->frbuf) {
373 err("frame alloc failed");
376 gspca_dev->nframes = count;
377 for (i = 0; i < count; i++) {
378 frame = &gspca_dev->frame[i];
379 frame->v4l2_buf.index = i;
380 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
381 frame->v4l2_buf.flags = 0;
382 frame->v4l2_buf.field = V4L2_FIELD_NONE;
383 frame->v4l2_buf.length = frsz;
384 frame->v4l2_buf.memory = gspca_dev->memory;
385 frame->v4l2_buf.sequence = 0;
386 frame->data = frame->data_end =
387 gspca_dev->frbuf + i * frsz;
388 frame->v4l2_buf.m.offset = i * frsz;
390 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
391 gspca_dev->last_packet_type = DISCARD_PACKET;
392 gspca_dev->sequence = 0;
396 static void frame_free(struct gspca_dev *gspca_dev)
400 PDEBUG(D_STREAM, "frame free");
401 if (gspca_dev->frbuf != NULL) {
402 rvfree(gspca_dev->frbuf,
403 gspca_dev->nframes * gspca_dev->frsz);
404 gspca_dev->frbuf = NULL;
405 for (i = 0; i < gspca_dev->nframes; i++)
406 gspca_dev->frame[i].data = NULL;
408 gspca_dev->nframes = 0;
411 static void destroy_urbs(struct gspca_dev *gspca_dev)
416 PDEBUG(D_STREAM, "kill transfer");
417 for (i = 0; i < MAX_NURBS; i++) {
418 urb = gspca_dev->urb[i];
422 BUG_ON(!gspca_dev->dev);
423 gspca_dev->urb[i] = NULL;
424 if (!gspca_dev->present)
426 if (urb->transfer_buffer != NULL)
427 usb_buffer_free(gspca_dev->dev,
428 urb->transfer_buffer_length,
429 urb->transfer_buffer,
436 * look for an input transfer endpoint in an alternate setting
438 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
441 struct usb_host_endpoint *ep;
444 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
445 ep = &alt->endpoint[i];
446 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
454 * look for an input (isoc or bulk) endpoint
456 * The endpoint is defined by the subdriver.
457 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
458 * This routine may be called many times when the bandwidth is too small
459 * (the bandwidth is checked on urb submit).
461 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
463 struct usb_interface *intf;
464 struct usb_host_endpoint *ep;
467 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
469 i = gspca_dev->alt; /* previous alt setting */
472 while (--i > 0) { /* alt 0 is unusable */
473 ep = alt_xfer(&intf->altsetting[i],
474 USB_ENDPOINT_XFER_ISOC);
479 /* if no isoc, try bulk */
481 ep = alt_xfer(&intf->altsetting[0],
482 USB_ENDPOINT_XFER_BULK);
484 err("no transfer endpoint found");
488 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
489 i, ep->desc.bEndpointAddress);
491 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
493 err("set interface err %d", ret);
497 gspca_dev->alt = i; /* memorize the current alt setting */
502 * create the URBs for image transfer
504 static int create_urbs(struct gspca_dev *gspca_dev,
505 struct usb_host_endpoint *ep)
508 int n, nurbs, i, psize, npkt, bsize;
510 /* calculate the packet size and the number of packets */
511 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
513 if (gspca_dev->alt != 0) { /* isoc */
515 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
516 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
517 npkt = ISO_MAX_SIZE / psize;
518 if (npkt > ISO_MAX_PKT)
520 bsize = psize * npkt;
522 "isoc %d pkts size %d = bsize:%d",
527 bsize = gspca_dev->cam.bulk_size;
530 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
531 if (gspca_dev->cam.bulk_nurbs != 0)
532 nurbs = gspca_dev->cam.bulk_nurbs;
537 gspca_dev->nurbs = nurbs;
538 for (n = 0; n < nurbs; n++) {
539 urb = usb_alloc_urb(npkt, GFP_KERNEL);
541 err("usb_alloc_urb failed");
542 destroy_urbs(gspca_dev);
545 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
550 if (urb->transfer_buffer == NULL) {
552 err("usb_buffer_urb failed");
553 destroy_urbs(gspca_dev);
556 gspca_dev->urb[n] = urb;
557 urb->dev = gspca_dev->dev;
558 urb->context = gspca_dev;
559 urb->transfer_buffer_length = bsize;
560 if (npkt != 0) { /* ISOC */
561 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
562 ep->desc.bEndpointAddress);
563 urb->transfer_flags = URB_ISO_ASAP
564 | URB_NO_TRANSFER_DMA_MAP;
565 urb->interval = ep->desc.bInterval;
566 urb->complete = isoc_irq;
567 urb->number_of_packets = npkt;
568 for (i = 0; i < npkt; i++) {
569 urb->iso_frame_desc[i].length = psize;
570 urb->iso_frame_desc[i].offset = psize * i;
573 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
574 ep->desc.bEndpointAddress),
575 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
576 urb->complete = bulk_irq;
583 * start the USB transfer
585 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
587 struct usb_host_endpoint *ep;
590 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
593 /* set the higher alternate setting and
594 * loop until urb submit succeeds */
595 gspca_dev->alt = gspca_dev->nbalt;
597 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
598 ep = get_ep(gspca_dev);
603 ret = create_urbs(gspca_dev, ep);
607 /* clear the bulk endpoint */
608 if (gspca_dev->alt == 0) /* if bulk transfer */
609 usb_clear_halt(gspca_dev->dev,
610 gspca_dev->urb[0]->pipe);
613 ret = gspca_dev->sd_desc->start(gspca_dev);
615 destroy_urbs(gspca_dev);
618 gspca_dev->streaming = 1;
620 /* some bulk transfers are started by the subdriver */
621 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
624 /* submit the URBs */
625 for (n = 0; n < gspca_dev->nurbs; n++) {
626 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
628 PDEBUG(D_ERR|D_STREAM,
629 "usb_submit_urb [%d] err %d", n, ret);
630 gspca_dev->streaming = 0;
631 destroy_urbs(gspca_dev);
632 if (ret == -ENOSPC) {
633 msleep(20); /* wait for kill
635 break; /* try the previous alt */
644 mutex_unlock(&gspca_dev->usb_lock);
648 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
652 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
654 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
658 /* Note: both the queue and the usb locks should be held when calling this */
659 static void gspca_stream_off(struct gspca_dev *gspca_dev)
661 gspca_dev->streaming = 0;
662 if (gspca_dev->present
663 && gspca_dev->sd_desc->stopN)
664 gspca_dev->sd_desc->stopN(gspca_dev);
665 destroy_urbs(gspca_dev);
666 gspca_set_alt0(gspca_dev);
667 if (gspca_dev->sd_desc->stop0)
668 gspca_dev->sd_desc->stop0(gspca_dev);
669 PDEBUG(D_STREAM, "stream off OK");
672 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
676 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
677 gspca_dev->curr_mode = i;
678 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
679 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
680 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
683 static int wxh_to_mode(struct gspca_dev *gspca_dev,
684 int width, int height)
688 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
689 if (width >= gspca_dev->cam.cam_mode[i].width
690 && height >= gspca_dev->cam.cam_mode[i].height)
697 * search a mode with the right pixel format
699 static int gspca_get_mode(struct gspca_dev *gspca_dev,
705 modeU = modeD = mode;
706 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
708 if (gspca_dev->cam.cam_mode[modeD].pixelformat
712 if (++modeU < gspca_dev->cam.nmodes) {
713 if (gspca_dev->cam.cam_mode[modeU].pixelformat
721 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
722 struct v4l2_fmtdesc *fmtdesc)
724 struct gspca_dev *gspca_dev = priv;
728 /* give an index to each format */
731 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
732 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
735 if (fmt_tb[j] == fmt_tb[index])
740 if (fmtdesc->index == index)
741 break; /* new format */
743 if (index >= ARRAY_SIZE(fmt_tb))
748 return -EINVAL; /* no more format */
750 fmtdesc->pixelformat = fmt_tb[index];
751 if (gspca_is_compressed(fmt_tb[index]))
752 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
753 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
754 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
755 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
756 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
757 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
758 fmtdesc->description[4] = '\0';
762 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
763 struct v4l2_format *fmt)
765 struct gspca_dev *gspca_dev = priv;
768 mode = gspca_dev->curr_mode;
769 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
770 sizeof fmt->fmt.pix);
774 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
775 struct v4l2_format *fmt)
777 int w, h, mode, mode2;
779 w = fmt->fmt.pix.width;
780 h = fmt->fmt.pix.height;
783 if (gspca_debug & D_CONF)
784 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
786 /* search the closest mode for width and height */
787 mode = wxh_to_mode(gspca_dev, w, h);
789 /* OK if right palette */
790 if (gspca_dev->cam.cam_mode[mode].pixelformat
791 != fmt->fmt.pix.pixelformat) {
793 /* else, search the closest mode with the same pixel format */
794 mode2 = gspca_get_mode(gspca_dev, mode,
795 fmt->fmt.pix.pixelformat);
799 ; * no chance, return this mode */
801 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
802 sizeof fmt->fmt.pix);
803 return mode; /* used when s_fmt */
806 static int vidioc_try_fmt_vid_cap(struct file *file,
808 struct v4l2_format *fmt)
810 struct gspca_dev *gspca_dev = priv;
813 ret = try_fmt_vid_cap(gspca_dev, fmt);
819 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
820 struct v4l2_format *fmt)
822 struct gspca_dev *gspca_dev = priv;
825 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
828 ret = try_fmt_vid_cap(gspca_dev, fmt);
832 if (gspca_dev->nframes != 0
833 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
838 if (ret == gspca_dev->curr_mode) {
840 goto out; /* same mode */
843 if (gspca_dev->streaming) {
847 gspca_dev->width = fmt->fmt.pix.width;
848 gspca_dev->height = fmt->fmt.pix.height;
849 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
850 gspca_dev->curr_mode = ret;
854 mutex_unlock(&gspca_dev->queue_lock);
858 static void gspca_release(struct video_device *vfd)
860 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
862 PDEBUG(D_STREAM, "device released");
864 kfree(gspca_dev->usb_buf);
868 static int dev_open(struct file *file)
870 struct gspca_dev *gspca_dev;
873 PDEBUG(D_STREAM, "%s open", current->comm);
874 gspca_dev = (struct gspca_dev *) video_devdata(file);
875 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
877 if (!gspca_dev->present) {
882 if (gspca_dev->users > 4) { /* (arbitrary value) */
887 /* protect the subdriver against rmmod */
888 if (!try_module_get(gspca_dev->module)) {
895 file->private_data = gspca_dev;
897 /* activate the v4l2 debug */
898 if (gspca_debug & D_V4L2)
899 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
900 | V4L2_DEBUG_IOCTL_ARG;
902 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
903 | V4L2_DEBUG_IOCTL_ARG);
907 mutex_unlock(&gspca_dev->queue_lock);
909 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
911 PDEBUG(D_STREAM, "open done");
915 static int dev_close(struct file *file)
917 struct gspca_dev *gspca_dev = file->private_data;
919 PDEBUG(D_STREAM, "%s close", current->comm);
920 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
924 /* if the file did the capture, free the streaming resources */
925 if (gspca_dev->capt_file == file) {
926 if (gspca_dev->streaming) {
927 mutex_lock(&gspca_dev->usb_lock);
928 gspca_stream_off(gspca_dev);
929 mutex_unlock(&gspca_dev->usb_lock);
931 frame_free(gspca_dev);
932 gspca_dev->capt_file = NULL;
933 gspca_dev->memory = GSPCA_MEMORY_NO;
935 file->private_data = NULL;
936 module_put(gspca_dev->module);
937 mutex_unlock(&gspca_dev->queue_lock);
939 PDEBUG(D_STREAM, "close done");
944 static int vidioc_querycap(struct file *file, void *priv,
945 struct v4l2_capability *cap)
947 struct gspca_dev *gspca_dev = priv;
949 memset(cap, 0, sizeof *cap);
950 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
951 if (gspca_dev->dev->product != NULL) {
952 strncpy(cap->card, gspca_dev->dev->product,
955 snprintf(cap->card, sizeof cap->card,
956 "USB Camera (%04x:%04x)",
957 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
958 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
960 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
961 sizeof cap->bus_info);
962 cap->version = DRIVER_VERSION_NUMBER;
963 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
965 | V4L2_CAP_READWRITE;
969 static int vidioc_queryctrl(struct file *file, void *priv,
970 struct v4l2_queryctrl *q_ctrl)
972 struct gspca_dev *gspca_dev = priv;
978 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
979 id &= V4L2_CTRL_ID_MASK;
981 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
982 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
988 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
989 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
994 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
995 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1002 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1004 if (gspca_dev->ctrl_dis & (1 << ix))
1005 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1009 static int vidioc_s_ctrl(struct file *file, void *priv,
1010 struct v4l2_control *ctrl)
1012 struct gspca_dev *gspca_dev = priv;
1013 const struct ctrl *ctrls;
1016 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1017 i < gspca_dev->sd_desc->nctrls;
1019 if (ctrl->id != ctrls->qctrl.id)
1021 if (gspca_dev->ctrl_dis & (1 << i))
1023 if (ctrl->value < ctrls->qctrl.minimum
1024 || ctrl->value > ctrls->qctrl.maximum)
1026 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1027 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1028 return -ERESTARTSYS;
1029 ret = ctrls->set(gspca_dev, ctrl->value);
1030 mutex_unlock(&gspca_dev->usb_lock);
1036 static int vidioc_g_ctrl(struct file *file, void *priv,
1037 struct v4l2_control *ctrl)
1039 struct gspca_dev *gspca_dev = priv;
1041 const struct ctrl *ctrls;
1044 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1045 i < gspca_dev->sd_desc->nctrls;
1047 if (ctrl->id != ctrls->qctrl.id)
1049 if (gspca_dev->ctrl_dis & (1 << i))
1051 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1052 return -ERESTARTSYS;
1053 ret = ctrls->get(gspca_dev, &ctrl->value);
1054 mutex_unlock(&gspca_dev->usb_lock);
1060 /*fixme: have an audio flag in gspca_dev?*/
1061 static int vidioc_s_audio(struct file *file, void *priv,
1062 struct v4l2_audio *audio)
1064 if (audio->index != 0)
1069 static int vidioc_g_audio(struct file *file, void *priv,
1070 struct v4l2_audio *audio)
1072 memset(audio, 0, sizeof *audio);
1073 strcpy(audio->name, "Microphone");
1077 static int vidioc_enumaudio(struct file *file, void *priv,
1078 struct v4l2_audio *audio)
1080 if (audio->index != 0)
1083 strcpy(audio->name, "Microphone");
1084 audio->capability = 0;
1089 static int vidioc_querymenu(struct file *file, void *priv,
1090 struct v4l2_querymenu *qmenu)
1092 struct gspca_dev *gspca_dev = priv;
1094 if (!gspca_dev->sd_desc->querymenu)
1096 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1099 static int vidioc_enum_input(struct file *file, void *priv,
1100 struct v4l2_input *input)
1102 struct gspca_dev *gspca_dev = priv;
1104 if (input->index != 0)
1106 memset(input, 0, sizeof *input);
1107 input->type = V4L2_INPUT_TYPE_CAMERA;
1108 strncpy(input->name, gspca_dev->sd_desc->name,
1109 sizeof input->name);
1113 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1119 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1126 static int vidioc_reqbufs(struct file *file, void *priv,
1127 struct v4l2_requestbuffers *rb)
1129 struct gspca_dev *gspca_dev = priv;
1132 switch (rb->memory) {
1133 case GSPCA_MEMORY_READ: /* (internal call) */
1134 case V4L2_MEMORY_MMAP:
1135 case V4L2_MEMORY_USERPTR:
1140 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1141 return -ERESTARTSYS;
1143 if (gspca_dev->memory != GSPCA_MEMORY_NO
1144 && gspca_dev->memory != rb->memory) {
1149 /* only one file may do the capture */
1150 if (gspca_dev->capt_file != NULL
1151 && gspca_dev->capt_file != file) {
1156 /* if allocated, the buffers must not be mapped */
1157 for (i = 0; i < gspca_dev->nframes; i++) {
1158 if (gspca_dev->frame[i].vma_use_count) {
1164 /* stop streaming */
1165 if (gspca_dev->streaming) {
1166 mutex_lock(&gspca_dev->usb_lock);
1167 gspca_stream_off(gspca_dev);
1168 mutex_unlock(&gspca_dev->usb_lock);
1171 /* free the previous allocated buffers, if any */
1172 if (gspca_dev->nframes != 0) {
1173 frame_free(gspca_dev);
1174 gspca_dev->capt_file = NULL;
1176 if (rb->count == 0) /* unrequest */
1178 gspca_dev->memory = rb->memory;
1179 ret = frame_alloc(gspca_dev, rb->count);
1181 rb->count = gspca_dev->nframes;
1182 gspca_dev->capt_file = file;
1185 mutex_unlock(&gspca_dev->queue_lock);
1186 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1190 static int vidioc_querybuf(struct file *file, void *priv,
1191 struct v4l2_buffer *v4l2_buf)
1193 struct gspca_dev *gspca_dev = priv;
1194 struct gspca_frame *frame;
1196 if (v4l2_buf->index < 0
1197 || v4l2_buf->index >= gspca_dev->nframes)
1200 frame = &gspca_dev->frame[v4l2_buf->index];
1201 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1205 static int vidioc_streamon(struct file *file, void *priv,
1206 enum v4l2_buf_type buf_type)
1208 struct gspca_dev *gspca_dev = priv;
1211 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1213 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1214 return -ERESTARTSYS;
1215 if (!gspca_dev->present) {
1219 if (gspca_dev->nframes == 0
1220 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1224 if (!gspca_dev->streaming) {
1225 ret = gspca_init_transfer(gspca_dev);
1230 if (gspca_debug & D_STREAM) {
1231 PDEBUG_MODE("stream on OK",
1239 mutex_unlock(&gspca_dev->queue_lock);
1243 static int vidioc_streamoff(struct file *file, void *priv,
1244 enum v4l2_buf_type buf_type)
1246 struct gspca_dev *gspca_dev = priv;
1249 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1251 if (!gspca_dev->streaming)
1253 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1254 return -ERESTARTSYS;
1256 /* stop streaming */
1257 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1261 gspca_stream_off(gspca_dev);
1262 mutex_unlock(&gspca_dev->usb_lock);
1264 /* empty the application queues */
1265 for (i = 0; i < gspca_dev->nframes; i++)
1266 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1267 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1268 gspca_dev->last_packet_type = DISCARD_PACKET;
1269 gspca_dev->sequence = 0;
1272 mutex_unlock(&gspca_dev->queue_lock);
1276 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1277 struct v4l2_jpegcompression *jpegcomp)
1279 struct gspca_dev *gspca_dev = priv;
1282 if (!gspca_dev->sd_desc->get_jcomp)
1284 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1285 return -ERESTARTSYS;
1286 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1287 mutex_unlock(&gspca_dev->usb_lock);
1291 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1292 struct v4l2_jpegcompression *jpegcomp)
1294 struct gspca_dev *gspca_dev = priv;
1297 if (!gspca_dev->sd_desc->set_jcomp)
1299 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1300 return -ERESTARTSYS;
1301 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1302 mutex_unlock(&gspca_dev->usb_lock);
1306 static int vidioc_g_parm(struct file *filp, void *priv,
1307 struct v4l2_streamparm *parm)
1309 struct gspca_dev *gspca_dev = priv;
1311 memset(parm, 0, sizeof *parm);
1312 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1313 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1315 if (gspca_dev->sd_desc->get_streamparm) {
1318 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1319 return -ERESTARTSYS;
1320 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1321 mutex_unlock(&gspca_dev->usb_lock);
1328 static int vidioc_s_parm(struct file *filp, void *priv,
1329 struct v4l2_streamparm *parm)
1331 struct gspca_dev *gspca_dev = priv;
1334 n = parm->parm.capture.readbuffers;
1335 if (n == 0 || n > GSPCA_MAX_FRAMES)
1336 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1338 gspca_dev->nbufread = n;
1340 if (gspca_dev->sd_desc->set_streamparm) {
1343 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1344 return -ERESTARTSYS;
1345 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1346 mutex_unlock(&gspca_dev->usb_lock);
1353 static int vidioc_s_std(struct file *filp, void *priv,
1359 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1360 static int vidiocgmbuf(struct file *file, void *priv,
1361 struct video_mbuf *mbuf)
1363 struct gspca_dev *gspca_dev = file->private_data;
1366 PDEBUG(D_STREAM, "cgmbuf");
1367 if (gspca_dev->nframes == 0) {
1371 struct v4l2_format fmt;
1373 memset(&fmt, 0, sizeof fmt);
1374 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1375 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1376 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1377 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1378 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1379 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1384 struct v4l2_requestbuffers rb;
1386 memset(&rb, 0, sizeof rb);
1388 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1389 rb.memory = V4L2_MEMORY_MMAP;
1390 ret = vidioc_reqbufs(file, priv, &rb);
1395 mbuf->frames = gspca_dev->nframes;
1396 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1397 for (i = 0; i < mbuf->frames; i++)
1398 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1403 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1405 struct gspca_dev *gspca_dev = file->private_data;
1406 struct gspca_frame *frame;
1408 unsigned long addr, start, size;
1411 start = vma->vm_start;
1412 size = vma->vm_end - vma->vm_start;
1413 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1415 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1416 return -ERESTARTSYS;
1417 if (!gspca_dev->present) {
1421 if (gspca_dev->capt_file != file) {
1427 for (i = 0; i < gspca_dev->nframes; ++i) {
1428 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1429 PDEBUG(D_STREAM, "mmap bad memory type");
1432 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1434 frame = &gspca_dev->frame[i];
1438 if (frame == NULL) {
1439 PDEBUG(D_STREAM, "mmap no frame buffer found");
1443 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1444 /* v4l1 maps all the buffers */
1446 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1448 if (size != frame->v4l2_buf.length) {
1449 PDEBUG(D_STREAM, "mmap bad size");
1455 * - VM_IO marks the area as being a mmaped region for I/O to a
1456 * device. It also prevents the region from being core dumped.
1458 vma->vm_flags |= VM_IO;
1460 addr = (unsigned long) frame->data;
1462 page = vmalloc_to_page((void *) addr);
1463 ret = vm_insert_page(vma, start, page);
1471 vma->vm_ops = &gspca_vm_ops;
1472 vma->vm_private_data = frame;
1476 mutex_unlock(&gspca_dev->queue_lock);
1481 * wait for a video frame
1483 * If a frame is ready, its index is returned.
1485 static int frame_wait(struct gspca_dev *gspca_dev,
1488 struct gspca_frame *frame;
1491 /* check if a frame is ready */
1492 i = gspca_dev->fr_o;
1493 j = gspca_dev->fr_queue[i];
1494 frame = &gspca_dev->frame[j];
1496 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1500 /* wait till a frame is ready */
1501 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1502 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1503 !gspca_dev->streaming || !gspca_dev->present,
1504 msecs_to_jiffies(3000));
1507 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1511 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1512 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1517 if (gspca_dev->sd_desc->dq_callback) {
1518 mutex_lock(&gspca_dev->usb_lock);
1519 gspca_dev->sd_desc->dq_callback(gspca_dev);
1520 mutex_unlock(&gspca_dev->usb_lock);
1526 * dequeue a video buffer
1528 * If nonblock_ing is false, block until a buffer is available.
1530 static int vidioc_dqbuf(struct file *file, void *priv,
1531 struct v4l2_buffer *v4l2_buf)
1533 struct gspca_dev *gspca_dev = priv;
1534 struct gspca_frame *frame;
1537 PDEBUG(D_FRAM, "dqbuf");
1538 if (v4l2_buf->memory != gspca_dev->memory)
1541 /* if not streaming, be sure the application will not loop forever */
1542 if (!(file->f_flags & O_NONBLOCK)
1543 && !gspca_dev->streaming && gspca_dev->users == 1)
1546 /* only the capturing file may dequeue */
1547 if (gspca_dev->capt_file != file)
1550 /* only one dequeue / read at a time */
1551 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1552 return -ERESTARTSYS;
1554 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1557 i = ret; /* frame index */
1558 frame = &gspca_dev->frame[i];
1559 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1560 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1562 frame->v4l2_buf.bytesused)) {
1563 PDEBUG(D_ERR|D_STREAM,
1564 "dqbuf cp to user failed");
1569 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1570 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1571 PDEBUG(D_FRAM, "dqbuf %d", i);
1574 mutex_unlock(&gspca_dev->read_lock);
1579 * queue a video buffer
1581 * Attempting to queue a buffer that has already been
1582 * queued will return -EINVAL.
1584 static int vidioc_qbuf(struct file *file, void *priv,
1585 struct v4l2_buffer *v4l2_buf)
1587 struct gspca_dev *gspca_dev = priv;
1588 struct gspca_frame *frame;
1591 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1593 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1594 return -ERESTARTSYS;
1596 index = v4l2_buf->index;
1597 if ((unsigned) index >= gspca_dev->nframes) {
1599 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1603 if (v4l2_buf->memory != gspca_dev->memory) {
1604 PDEBUG(D_FRAM, "qbuf bad memory type");
1609 frame = &gspca_dev->frame[index];
1610 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1611 PDEBUG(D_FRAM, "qbuf bad state");
1616 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1618 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1619 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1620 frame->v4l2_buf.length = v4l2_buf->length;
1623 /* put the buffer in the 'queued' queue */
1624 i = gspca_dev->fr_q;
1625 gspca_dev->fr_queue[i] = index;
1626 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1627 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1632 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1633 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1636 mutex_unlock(&gspca_dev->queue_lock);
1641 * allocate the resources for read()
1643 static int read_alloc(struct gspca_dev *gspca_dev,
1646 struct v4l2_buffer v4l2_buf;
1649 PDEBUG(D_STREAM, "read alloc");
1650 if (gspca_dev->nframes == 0) {
1651 struct v4l2_requestbuffers rb;
1653 memset(&rb, 0, sizeof rb);
1654 rb.count = gspca_dev->nbufread;
1655 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1656 rb.memory = GSPCA_MEMORY_READ;
1657 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1659 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1662 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1663 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1664 v4l2_buf.memory = GSPCA_MEMORY_READ;
1665 for (i = 0; i < gspca_dev->nbufread; i++) {
1667 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1669 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1673 gspca_dev->memory = GSPCA_MEMORY_READ;
1676 /* start streaming */
1677 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1679 PDEBUG(D_STREAM, "read streamon err %d", ret);
1683 static unsigned int dev_poll(struct file *file, poll_table *wait)
1685 struct gspca_dev *gspca_dev = file->private_data;
1688 PDEBUG(D_FRAM, "poll");
1690 poll_wait(file, &gspca_dev->wq, wait);
1691 if (!gspca_dev->present)
1694 /* if reqbufs is not done, the user would use read() */
1695 if (gspca_dev->nframes == 0) {
1696 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1697 return POLLERR; /* not the 1st time */
1698 ret = read_alloc(gspca_dev, file);
1703 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1705 if (!gspca_dev->present) {
1710 /* check the next incoming buffer */
1711 i = gspca_dev->fr_o;
1712 i = gspca_dev->fr_queue[i];
1713 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1714 ret = POLLIN | POLLRDNORM; /* something to read */
1718 mutex_unlock(&gspca_dev->queue_lock);
1722 static ssize_t dev_read(struct file *file, char __user *data,
1723 size_t count, loff_t *ppos)
1725 struct gspca_dev *gspca_dev = file->private_data;
1726 struct gspca_frame *frame;
1727 struct v4l2_buffer v4l2_buf;
1728 struct timeval timestamp;
1731 PDEBUG(D_FRAM, "read (%zd)", count);
1732 if (!gspca_dev->present)
1734 switch (gspca_dev->memory) {
1735 case GSPCA_MEMORY_NO: /* first time */
1736 ret = read_alloc(gspca_dev, file);
1740 case GSPCA_MEMORY_READ:
1741 if (gspca_dev->capt_file == file)
1749 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1753 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1754 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1755 v4l2_buf.memory = GSPCA_MEMORY_READ;
1756 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1758 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1762 /* if the process slept for more than 1 second,
1763 * get a newer frame */
1764 frame = &gspca_dev->frame[v4l2_buf.index];
1766 break; /* avoid infinite loop */
1767 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1769 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1771 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1776 /* copy the frame */
1777 if (count > frame->v4l2_buf.bytesused)
1778 count = frame->v4l2_buf.bytesused;
1779 ret = copy_to_user(data, frame->data, count);
1781 PDEBUG(D_ERR|D_STREAM,
1782 "read cp to user lack %d / %zd", ret, count);
1788 /* in each case, requeue the buffer */
1789 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1795 static struct v4l2_file_operations dev_fops = {
1796 .owner = THIS_MODULE,
1798 .release = dev_close,
1801 .unlocked_ioctl = video_ioctl2,
1805 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1806 .vidioc_querycap = vidioc_querycap,
1807 .vidioc_dqbuf = vidioc_dqbuf,
1808 .vidioc_qbuf = vidioc_qbuf,
1809 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1810 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1811 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1812 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1813 .vidioc_streamon = vidioc_streamon,
1814 .vidioc_queryctrl = vidioc_queryctrl,
1815 .vidioc_g_ctrl = vidioc_g_ctrl,
1816 .vidioc_s_ctrl = vidioc_s_ctrl,
1817 .vidioc_g_audio = vidioc_g_audio,
1818 .vidioc_s_audio = vidioc_s_audio,
1819 .vidioc_enumaudio = vidioc_enumaudio,
1820 .vidioc_querymenu = vidioc_querymenu,
1821 .vidioc_enum_input = vidioc_enum_input,
1822 .vidioc_g_input = vidioc_g_input,
1823 .vidioc_s_input = vidioc_s_input,
1824 .vidioc_reqbufs = vidioc_reqbufs,
1825 .vidioc_querybuf = vidioc_querybuf,
1826 .vidioc_streamoff = vidioc_streamoff,
1827 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1828 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1829 .vidioc_g_parm = vidioc_g_parm,
1830 .vidioc_s_parm = vidioc_s_parm,
1831 .vidioc_s_std = vidioc_s_std,
1832 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1833 .vidiocgmbuf = vidiocgmbuf,
1837 static struct video_device gspca_template = {
1838 .name = "gspca main driver",
1840 .ioctl_ops = &dev_ioctl_ops,
1841 .release = gspca_release,
1846 * probe and create a new gspca device
1848 * This function must be called by the sub-driver when it is
1849 * called for probing a new device.
1851 int gspca_dev_probe(struct usb_interface *intf,
1852 const struct usb_device_id *id,
1853 const struct sd_desc *sd_desc,
1855 struct module *module)
1857 struct usb_interface_descriptor *interface;
1858 struct gspca_dev *gspca_dev;
1859 struct usb_device *dev = interface_to_usbdev(intf);
1862 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1864 /* we don't handle multi-config cameras */
1865 if (dev->descriptor.bNumConfigurations != 1)
1867 interface = &intf->cur_altsetting->desc;
1868 if (interface->bInterfaceNumber > 0)
1871 /* create the device */
1872 if (dev_size < sizeof *gspca_dev)
1873 dev_size = sizeof *gspca_dev;
1874 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1876 err("couldn't kzalloc gspca struct");
1879 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1880 if (!gspca_dev->usb_buf) {
1881 err("out of memory");
1885 gspca_dev->dev = dev;
1886 gspca_dev->iface = interface->bInterfaceNumber;
1887 gspca_dev->nbalt = intf->num_altsetting;
1888 gspca_dev->sd_desc = sd_desc;
1889 gspca_dev->nbufread = 2;
1890 gspca_dev->empty_packet = -1; /* don't check the empty packets */
1892 /* configure the subdriver and initialize the USB device */
1893 ret = sd_desc->config(gspca_dev, id);
1896 ret = sd_desc->init(gspca_dev);
1899 ret = gspca_set_alt0(gspca_dev);
1902 gspca_set_default_mode(gspca_dev);
1904 mutex_init(&gspca_dev->usb_lock);
1905 mutex_init(&gspca_dev->read_lock);
1906 mutex_init(&gspca_dev->queue_lock);
1907 init_waitqueue_head(&gspca_dev->wq);
1909 /* init video stuff */
1910 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1911 gspca_dev->vdev.parent = &dev->dev;
1912 gspca_dev->module = module;
1913 gspca_dev->present = 1;
1914 ret = video_register_device(&gspca_dev->vdev,
1918 err("video_register_device err %d", ret);
1922 usb_set_intfdata(intf, gspca_dev);
1923 PDEBUG(D_PROBE, "probe ok");
1926 kfree(gspca_dev->usb_buf);
1930 EXPORT_SYMBOL(gspca_dev_probe);
1935 * This function must be called by the sub-driver
1936 * when the device disconnects, after the specific resources are freed.
1938 void gspca_disconnect(struct usb_interface *intf)
1940 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1942 mutex_lock(&gspca_dev->usb_lock);
1943 gspca_dev->present = 0;
1944 mutex_unlock(&gspca_dev->usb_lock);
1946 destroy_urbs(gspca_dev);
1947 gspca_dev->dev = NULL;
1948 usb_set_intfdata(intf, NULL);
1950 /* release the device */
1951 /* (this will call gspca_release() immediatly or on last close) */
1952 video_unregister_device(&gspca_dev->vdev);
1954 PDEBUG(D_PROBE, "disconnect complete");
1956 EXPORT_SYMBOL(gspca_disconnect);
1959 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1961 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1963 if (!gspca_dev->streaming)
1965 gspca_dev->frozen = 1; /* avoid urb error messages */
1966 if (gspca_dev->sd_desc->stopN)
1967 gspca_dev->sd_desc->stopN(gspca_dev);
1968 destroy_urbs(gspca_dev);
1969 gspca_set_alt0(gspca_dev);
1970 if (gspca_dev->sd_desc->stop0)
1971 gspca_dev->sd_desc->stop0(gspca_dev);
1974 EXPORT_SYMBOL(gspca_suspend);
1976 int gspca_resume(struct usb_interface *intf)
1978 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1980 gspca_dev->frozen = 0;
1981 gspca_dev->sd_desc->init(gspca_dev);
1982 if (gspca_dev->streaming)
1983 return gspca_init_transfer(gspca_dev);
1986 EXPORT_SYMBOL(gspca_resume);
1988 /* -- cam driver utility functions -- */
1990 /* auto gain and exposure algorithm based on the knee algorithm described here:
1991 http://ytse.tricolour.net/docs/LowLightOptimization.html
1993 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1995 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1996 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1998 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1999 const struct ctrl *gain_ctrl = NULL;
2000 const struct ctrl *exposure_ctrl = NULL;
2001 const struct ctrl *autogain_ctrl = NULL;
2004 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2005 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2006 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2007 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2008 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2009 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2010 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2012 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2013 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2014 "on cam without (auto)gain/exposure");
2018 if (gain_ctrl->get(gspca_dev, &gain) ||
2019 exposure_ctrl->get(gspca_dev, &exposure) ||
2020 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2024 orig_exposure = exposure;
2026 /* If we are of a multiple of deadzone, do multiple steps to reach the
2027 desired lumination fast (with the risc of a slight overshoot) */
2028 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2030 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2031 avg_lum, desired_avg_lum, steps);
2033 for (i = 0; i < steps; i++) {
2034 if (avg_lum > desired_avg_lum) {
2035 if (gain > gain_knee)
2037 else if (exposure > exposure_knee)
2039 else if (gain > gain_ctrl->qctrl.default_value)
2041 else if (exposure > exposure_ctrl->qctrl.minimum)
2043 else if (gain > gain_ctrl->qctrl.minimum)
2048 if (gain < gain_ctrl->qctrl.default_value)
2050 else if (exposure < exposure_knee)
2052 else if (gain < gain_knee)
2054 else if (exposure < exposure_ctrl->qctrl.maximum)
2056 else if (gain < gain_ctrl->qctrl.maximum)
2063 if (gain != orig_gain) {
2064 gain_ctrl->set(gspca_dev, gain);
2067 if (exposure != orig_exposure) {
2068 exposure_ctrl->set(gspca_dev, exposure);
2074 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2076 /* -- module insert / remove -- */
2077 static int __init gspca_init(void)
2079 info("main v%d.%d.%d registered",
2080 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2081 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2082 DRIVER_VERSION_NUMBER & 0xff);
2085 static void __exit gspca_exit(void)
2087 info("main deregistered");
2090 module_init(gspca_init);
2091 module_exit(gspca_exit);
2094 module_param_named(debug, gspca_debug, int, 0644);
2095 MODULE_PARM_DESC(debug,
2096 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2097 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"