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>
33 #include <linux/kref.h>
35 #include <linux/uaccess.h>
36 #include <linux/jiffies.h>
37 #include <media/v4l2-ioctl.h>
42 #define DEF_NURBS 2 /* default number of URBs */
44 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46 MODULE_LICENSE("GPL");
48 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 4, 0)
50 static int video_nr = -1;
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 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 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) {
136 if (!gspca_dev->frozen)
138 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
139 return; /* disconnection ? */
141 pkt_scan = gspca_dev->sd_desc->pkt_scan;
142 for (i = 0; i < urb->number_of_packets; i++) {
144 /* check the availability of the frame buffer */
145 frame = gspca_get_i_frame(gspca_dev);
147 gspca_dev->last_packet_type = DISCARD_PACKET;
151 /* check the packet status and length */
152 len = urb->iso_frame_desc[i].actual_length;
154 if (gspca_dev->empty_packet == 0)
155 gspca_dev->empty_packet = 1;
158 st = urb->iso_frame_desc[i].status;
161 "ISOC data error: [%d] len=%d, status=%d",
163 gspca_dev->last_packet_type = DISCARD_PACKET;
167 /* let the packet be analyzed by the subdriver */
168 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
169 i, urb->iso_frame_desc[i].offset, len);
170 data = (__u8 *) urb->transfer_buffer
171 + urb->iso_frame_desc[i].offset;
172 pkt_scan(gspca_dev, frame, data, len);
175 /* resubmit the URB */
176 st = usb_submit_urb(urb, GFP_ATOMIC);
178 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
182 * ISOC message interrupt from the USB device
184 * Analyse each packet and call the subdriver for copy to the frame buffer.
186 static void isoc_irq(struct urb *urb
189 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
191 PDEBUG(D_PACK, "isoc irq");
192 if (!gspca_dev->streaming)
194 fill_frame(gspca_dev, urb);
198 * bulk message interrupt from the USB device
200 static void bulk_irq(struct urb *urb
203 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
204 struct gspca_frame *frame;
207 PDEBUG(D_PACK, "bulk irq");
208 if (!gspca_dev->streaming)
210 switch (urb->status) {
218 if (!gspca_dev->frozen)
220 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
221 return; /* disconnection ? */
224 /* check the availability of the frame buffer */
225 frame = gspca_get_i_frame(gspca_dev);
227 gspca_dev->last_packet_type = DISCARD_PACKET;
229 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
230 gspca_dev->sd_desc->pkt_scan(gspca_dev,
232 urb->transfer_buffer,
236 /* resubmit the URB */
237 if (gspca_dev->cam.bulk_nurbs != 0) {
238 st = usb_submit_urb(urb, GFP_ATOMIC);
240 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
245 * add data to the current frame
247 * This function is called by the subdrivers at interrupt level.
249 * To build a frame, these ones must add
251 * - 0 or many INTER_PACKETs
253 * DISCARD_PACKET invalidates the whole frame.
254 * On LAST_PACKET, a new frame is returned.
256 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
257 enum gspca_packet_type packet_type,
258 struct gspca_frame *frame,
264 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
266 /* when start of a new frame, if the current frame buffer
267 * is not queued, discard the whole frame */
268 if (packet_type == FIRST_PACKET) {
269 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
270 != V4L2_BUF_FLAG_QUEUED) {
271 gspca_dev->last_packet_type = DISCARD_PACKET;
274 frame->data_end = frame->data;
275 jiffies_to_timeval(get_jiffies_64(),
276 &frame->v4l2_buf.timestamp);
277 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
278 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
279 if (packet_type == LAST_PACKET)
280 gspca_dev->last_packet_type = packet_type;
284 /* append the packet to the frame buffer */
286 if (frame->data_end - frame->data + len
287 > frame->v4l2_buf.length) {
288 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
289 frame->data_end - frame->data + len,
290 frame->v4l2_buf.length);
291 packet_type = DISCARD_PACKET;
293 memcpy(frame->data_end, data, len);
294 frame->data_end += len;
297 gspca_dev->last_packet_type = packet_type;
299 /* if last packet, wake up the application and advance in the queue */
300 if (packet_type == LAST_PACKET) {
301 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
302 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
303 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
304 atomic_inc(&gspca_dev->nevent);
305 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
306 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
308 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
309 frame->v4l2_buf.bytesused,
313 j = gspca_dev->fr_queue[i];
314 frame = &gspca_dev->frame[j];
318 EXPORT_SYMBOL(gspca_frame_add);
320 static int gspca_is_compressed(__u32 format)
323 case V4L2_PIX_FMT_MJPEG:
324 case V4L2_PIX_FMT_JPEG:
325 case V4L2_PIX_FMT_SPCA561:
326 case V4L2_PIX_FMT_PAC207:
332 static void *rvmalloc(unsigned long size)
337 mem = vmalloc_32(size);
339 adr = (unsigned long) mem;
340 while ((long) size > 0) {
341 SetPageReserved(vmalloc_to_page((void *) adr));
349 static void rvfree(void *mem, long size)
353 adr = (unsigned long) mem;
355 ClearPageReserved(vmalloc_to_page((void *) adr));
362 static int frame_alloc(struct gspca_dev *gspca_dev,
365 struct gspca_frame *frame;
369 i = gspca_dev->curr_mode;
370 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
371 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
372 frsz = PAGE_ALIGN(frsz);
373 gspca_dev->frsz = frsz;
374 if (count > GSPCA_MAX_FRAMES)
375 count = GSPCA_MAX_FRAMES;
376 gspca_dev->frbuf = rvmalloc(frsz * count);
377 if (!gspca_dev->frbuf) {
378 err("frame alloc failed");
381 gspca_dev->nframes = count;
382 for (i = 0; i < count; i++) {
383 frame = &gspca_dev->frame[i];
384 frame->v4l2_buf.index = i;
385 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
386 frame->v4l2_buf.flags = 0;
387 frame->v4l2_buf.field = V4L2_FIELD_NONE;
388 frame->v4l2_buf.length = frsz;
389 frame->v4l2_buf.memory = gspca_dev->memory;
390 frame->v4l2_buf.sequence = 0;
391 frame->data = frame->data_end =
392 gspca_dev->frbuf + i * frsz;
393 frame->v4l2_buf.m.offset = i * frsz;
395 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
396 gspca_dev->last_packet_type = DISCARD_PACKET;
397 gspca_dev->sequence = 0;
398 atomic_set(&gspca_dev->nevent, 0);
402 static void frame_free(struct gspca_dev *gspca_dev)
406 PDEBUG(D_STREAM, "frame free");
407 if (gspca_dev->frbuf != NULL) {
408 rvfree(gspca_dev->frbuf,
409 gspca_dev->nframes * gspca_dev->frsz);
410 gspca_dev->frbuf = NULL;
411 for (i = 0; i < gspca_dev->nframes; i++)
412 gspca_dev->frame[i].data = NULL;
414 gspca_dev->nframes = 0;
417 static void destroy_urbs(struct gspca_dev *gspca_dev)
422 PDEBUG(D_STREAM, "kill transfer");
423 for (i = 0; i < MAX_NURBS; i++) {
424 urb = gspca_dev->urb[i];
428 gspca_dev->urb[i] = NULL;
430 if (urb->transfer_buffer != NULL)
431 usb_buffer_free(gspca_dev->dev,
432 urb->transfer_buffer_length,
433 urb->transfer_buffer,
440 * look for an input transfer endpoint in an alternate setting
442 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
446 struct usb_host_endpoint *ep;
449 epaddr |= USB_DIR_IN;
450 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
451 ep = &alt->endpoint[i];
452 if (ep->desc.bEndpointAddress == epaddr) {
453 attr = ep->desc.bmAttributes
454 & USB_ENDPOINT_XFERTYPE_MASK;
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 i = gspca_dev->alt; /* previous alt setting */
482 while (--i > 0) { /* alt 0 is unusable */
483 ep = alt_xfer(&intf->altsetting[i],
484 gspca_dev->cam.epaddr,
485 USB_ENDPOINT_XFER_ISOC);
490 /* if no isoc, try bulk */
492 ep = alt_xfer(&intf->altsetting[0],
493 gspca_dev->cam.epaddr,
494 USB_ENDPOINT_XFER_BULK);
496 err("no transfer endpoint found");
500 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
501 i, ep->desc.bEndpointAddress);
503 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
505 err("set interface err %d", ret);
509 gspca_dev->alt = i; /* memorize the current alt setting */
514 * create the URBs for image transfer
516 static int create_urbs(struct gspca_dev *gspca_dev,
517 struct usb_host_endpoint *ep)
520 int n, nurbs, i, psize, npkt, bsize;
522 /* calculate the packet size and the number of packets */
523 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
525 if (gspca_dev->alt != 0) { /* isoc */
527 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
528 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
529 npkt = ISO_MAX_SIZE / psize;
530 if (npkt > ISO_MAX_PKT)
532 bsize = psize * npkt;
534 "isoc %d pkts size %d = bsize:%d",
539 bsize = gspca_dev->cam.bulk_size;
542 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
543 if (gspca_dev->cam.bulk_nurbs != 0)
544 nurbs = gspca_dev->cam.bulk_nurbs;
549 gspca_dev->nurbs = nurbs;
550 for (n = 0; n < nurbs; n++) {
551 urb = usb_alloc_urb(npkt, GFP_KERNEL);
553 err("usb_alloc_urb failed");
554 destroy_urbs(gspca_dev);
557 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
562 if (urb->transfer_buffer == NULL) {
564 err("usb_buffer_urb failed");
565 destroy_urbs(gspca_dev);
568 gspca_dev->urb[n] = urb;
569 urb->dev = gspca_dev->dev;
570 urb->context = gspca_dev;
571 urb->transfer_buffer_length = bsize;
572 if (npkt != 0) { /* ISOC */
573 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
574 ep->desc.bEndpointAddress);
575 urb->transfer_flags = URB_ISO_ASAP
576 | URB_NO_TRANSFER_DMA_MAP;
577 urb->interval = ep->desc.bInterval;
578 urb->complete = isoc_irq;
579 urb->number_of_packets = npkt;
580 for (i = 0; i < npkt; i++) {
581 urb->iso_frame_desc[i].length = psize;
582 urb->iso_frame_desc[i].offset = psize * i;
585 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
586 ep->desc.bEndpointAddress),
587 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
588 urb->complete = bulk_irq;
595 * start the USB transfer
597 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
599 struct usb_host_endpoint *ep;
602 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
605 /* set the higher alternate setting and
606 * loop until urb submit succeeds */
607 gspca_dev->alt = gspca_dev->nbalt;
609 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
610 ep = get_ep(gspca_dev);
615 ret = create_urbs(gspca_dev, ep);
619 /* clear the bulk endpoint */
620 if (gspca_dev->alt == 0) /* if bulk transfer */
621 usb_clear_halt(gspca_dev->dev,
622 usb_rcvintpipe(gspca_dev->dev,
623 gspca_dev->cam.epaddr));
626 ret = gspca_dev->sd_desc->start(gspca_dev);
628 destroy_urbs(gspca_dev);
631 gspca_dev->streaming = 1;
632 atomic_set(&gspca_dev->nevent, 0);
634 /* some bulk transfers are started by the subdriver */
635 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
638 /* submit the URBs */
639 for (n = 0; n < gspca_dev->nurbs; n++) {
640 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
642 PDEBUG(D_ERR|D_STREAM,
643 "usb_submit_urb [%d] err %d", n, ret);
644 gspca_dev->streaming = 0;
645 destroy_urbs(gspca_dev);
647 break; /* try the previous alt */
655 mutex_unlock(&gspca_dev->usb_lock);
659 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
663 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
665 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
669 /* Note: both the queue and the usb locks should be held when calling this */
670 static void gspca_stream_off(struct gspca_dev *gspca_dev)
672 gspca_dev->streaming = 0;
673 atomic_set(&gspca_dev->nevent, 0);
674 if (gspca_dev->present
675 && gspca_dev->sd_desc->stopN)
676 gspca_dev->sd_desc->stopN(gspca_dev);
677 destroy_urbs(gspca_dev);
678 gspca_set_alt0(gspca_dev);
679 if (gspca_dev->sd_desc->stop0)
680 gspca_dev->sd_desc->stop0(gspca_dev);
681 PDEBUG(D_STREAM, "stream off OK");
684 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
688 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
689 gspca_dev->curr_mode = i;
690 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
691 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
692 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
695 static int wxh_to_mode(struct gspca_dev *gspca_dev,
696 int width, int height)
700 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
701 if (width >= gspca_dev->cam.cam_mode[i].width
702 && height >= gspca_dev->cam.cam_mode[i].height)
709 * search a mode with the right pixel format
711 static int gspca_get_mode(struct gspca_dev *gspca_dev,
717 modeU = modeD = mode;
718 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
720 if (gspca_dev->cam.cam_mode[modeD].pixelformat
724 if (++modeU < gspca_dev->cam.nmodes) {
725 if (gspca_dev->cam.cam_mode[modeU].pixelformat
733 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
734 struct v4l2_fmtdesc *fmtdesc)
736 struct gspca_dev *gspca_dev = priv;
740 /* give an index to each format */
743 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
744 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
747 if (fmt_tb[j] == fmt_tb[index])
752 if (fmtdesc->index == index)
753 break; /* new format */
755 if (index >= ARRAY_SIZE(fmt_tb))
760 return -EINVAL; /* no more format */
762 fmtdesc->pixelformat = fmt_tb[index];
763 if (gspca_is_compressed(fmt_tb[index]))
764 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
765 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
766 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
767 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
768 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
769 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
770 fmtdesc->description[4] = '\0';
774 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
775 struct v4l2_format *fmt)
777 struct gspca_dev *gspca_dev = priv;
780 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
782 mode = gspca_dev->curr_mode;
783 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
784 sizeof fmt->fmt.pix);
788 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
789 struct v4l2_format *fmt)
791 int w, h, mode, mode2;
793 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
795 w = fmt->fmt.pix.width;
796 h = fmt->fmt.pix.height;
799 if (gspca_debug & D_CONF)
800 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
802 /* search the closest mode for width and height */
803 mode = wxh_to_mode(gspca_dev, w, h);
805 /* OK if right palette */
806 if (gspca_dev->cam.cam_mode[mode].pixelformat
807 != fmt->fmt.pix.pixelformat) {
809 /* else, search the closest mode with the same pixel format */
810 mode2 = gspca_get_mode(gspca_dev, mode,
811 fmt->fmt.pix.pixelformat);
815 ; * no chance, return this mode */
817 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
818 sizeof fmt->fmt.pix);
819 return mode; /* used when s_fmt */
822 static int vidioc_try_fmt_vid_cap(struct file *file,
824 struct v4l2_format *fmt)
826 struct gspca_dev *gspca_dev = priv;
829 ret = try_fmt_vid_cap(gspca_dev, fmt);
835 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
836 struct v4l2_format *fmt)
838 struct gspca_dev *gspca_dev = priv;
841 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
844 ret = try_fmt_vid_cap(gspca_dev, fmt);
848 if (gspca_dev->nframes != 0
849 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
854 if (ret == gspca_dev->curr_mode) {
856 goto out; /* same mode */
859 if (gspca_dev->streaming) {
863 gspca_dev->width = fmt->fmt.pix.width;
864 gspca_dev->height = fmt->fmt.pix.height;
865 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
866 gspca_dev->curr_mode = ret;
870 mutex_unlock(&gspca_dev->queue_lock);
874 static void gspca_delete(struct kref *kref)
876 struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
878 PDEBUG(D_STREAM, "device deleted");
880 kfree(gspca_dev->usb_buf);
884 static int dev_open(struct inode *inode, struct file *file)
886 struct gspca_dev *gspca_dev;
889 PDEBUG(D_STREAM, "%s open", current->comm);
890 gspca_dev = (struct gspca_dev *) video_devdata(file);
891 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
893 if (!gspca_dev->present) {
898 if (gspca_dev->users > 4) { /* (arbitrary value) */
903 /* protect the subdriver against rmmod */
904 if (!try_module_get(gspca_dev->module)) {
912 kref_get(&gspca_dev->kref);
914 file->private_data = gspca_dev;
916 /* activate the v4l2 debug */
917 if (gspca_debug & D_V4L2)
918 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
919 | V4L2_DEBUG_IOCTL_ARG;
921 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
922 | V4L2_DEBUG_IOCTL_ARG);
926 mutex_unlock(&gspca_dev->queue_lock);
928 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
930 PDEBUG(D_STREAM, "open done");
934 static int dev_close(struct inode *inode, struct file *file)
936 struct gspca_dev *gspca_dev = file->private_data;
938 PDEBUG(D_STREAM, "%s close", current->comm);
939 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
943 /* if the file did the capture, free the streaming resources */
944 if (gspca_dev->capt_file == file) {
945 if (gspca_dev->streaming) {
946 mutex_lock(&gspca_dev->usb_lock);
947 gspca_stream_off(gspca_dev);
948 mutex_unlock(&gspca_dev->usb_lock);
950 frame_free(gspca_dev);
951 gspca_dev->capt_file = NULL;
952 gspca_dev->memory = GSPCA_MEMORY_NO;
954 file->private_data = NULL;
955 module_put(gspca_dev->module);
956 mutex_unlock(&gspca_dev->queue_lock);
958 PDEBUG(D_STREAM, "close done");
960 kref_put(&gspca_dev->kref, gspca_delete);
965 static int vidioc_querycap(struct file *file, void *priv,
966 struct v4l2_capability *cap)
968 struct gspca_dev *gspca_dev = priv;
970 memset(cap, 0, sizeof *cap);
971 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
972 if (gspca_dev->dev->product != NULL) {
973 strncpy(cap->card, gspca_dev->dev->product,
976 snprintf(cap->card, sizeof cap->card,
977 "USB Camera (%04x:%04x)",
978 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
979 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
981 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
982 sizeof cap->bus_info);
983 cap->version = DRIVER_VERSION_NUMBER;
984 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
986 | V4L2_CAP_READWRITE;
990 static int vidioc_queryctrl(struct file *file, void *priv,
991 struct v4l2_queryctrl *q_ctrl)
993 struct gspca_dev *gspca_dev = priv;
999 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1000 id &= V4L2_CTRL_ID_MASK;
1002 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1003 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1009 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
1010 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
1015 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1016 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1023 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1025 if (gspca_dev->ctrl_dis & (1 << ix))
1026 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1030 static int vidioc_s_ctrl(struct file *file, void *priv,
1031 struct v4l2_control *ctrl)
1033 struct gspca_dev *gspca_dev = priv;
1034 const struct ctrl *ctrls;
1037 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1038 i < gspca_dev->sd_desc->nctrls;
1040 if (ctrl->id != ctrls->qctrl.id)
1042 if (gspca_dev->ctrl_dis & (1 << i))
1044 if (ctrl->value < ctrls->qctrl.minimum
1045 || ctrl->value > ctrls->qctrl.maximum)
1047 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1048 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1049 return -ERESTARTSYS;
1050 ret = ctrls->set(gspca_dev, ctrl->value);
1051 mutex_unlock(&gspca_dev->usb_lock);
1057 static int vidioc_g_ctrl(struct file *file, void *priv,
1058 struct v4l2_control *ctrl)
1060 struct gspca_dev *gspca_dev = priv;
1062 const struct ctrl *ctrls;
1065 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1066 i < gspca_dev->sd_desc->nctrls;
1068 if (ctrl->id != ctrls->qctrl.id)
1070 if (gspca_dev->ctrl_dis & (1 << i))
1072 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1073 return -ERESTARTSYS;
1074 ret = ctrls->get(gspca_dev, &ctrl->value);
1075 mutex_unlock(&gspca_dev->usb_lock);
1081 static int vidioc_querymenu(struct file *file, void *priv,
1082 struct v4l2_querymenu *qmenu)
1084 struct gspca_dev *gspca_dev = priv;
1086 if (!gspca_dev->sd_desc->querymenu)
1088 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1091 static int vidioc_enum_input(struct file *file, void *priv,
1092 struct v4l2_input *input)
1094 struct gspca_dev *gspca_dev = priv;
1096 if (input->index != 0)
1098 memset(input, 0, sizeof *input);
1099 input->type = V4L2_INPUT_TYPE_CAMERA;
1100 strncpy(input->name, gspca_dev->sd_desc->name,
1101 sizeof input->name);
1105 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1111 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1118 static int vidioc_reqbufs(struct file *file, void *priv,
1119 struct v4l2_requestbuffers *rb)
1121 struct gspca_dev *gspca_dev = priv;
1124 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1126 switch (rb->memory) {
1127 case GSPCA_MEMORY_READ: /* (internal call) */
1128 case V4L2_MEMORY_MMAP:
1129 case V4L2_MEMORY_USERPTR:
1134 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1135 return -ERESTARTSYS;
1137 if (gspca_dev->memory != GSPCA_MEMORY_NO
1138 && gspca_dev->memory != rb->memory) {
1143 /* only one file may do the capture */
1144 if (gspca_dev->capt_file != NULL
1145 && gspca_dev->capt_file != file) {
1150 /* if allocated, the buffers must not be mapped */
1151 for (i = 0; i < gspca_dev->nframes; i++) {
1152 if (gspca_dev->frame[i].vma_use_count) {
1158 /* stop streaming */
1159 if (gspca_dev->streaming) {
1160 mutex_lock(&gspca_dev->usb_lock);
1161 gspca_stream_off(gspca_dev);
1162 mutex_unlock(&gspca_dev->usb_lock);
1165 /* free the previous allocated buffers, if any */
1166 if (gspca_dev->nframes != 0) {
1167 frame_free(gspca_dev);
1168 gspca_dev->capt_file = NULL;
1170 if (rb->count == 0) /* unrequest */
1172 gspca_dev->memory = rb->memory;
1173 ret = frame_alloc(gspca_dev, rb->count);
1175 rb->count = gspca_dev->nframes;
1176 gspca_dev->capt_file = file;
1179 mutex_unlock(&gspca_dev->queue_lock);
1180 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1184 static int vidioc_querybuf(struct file *file, void *priv,
1185 struct v4l2_buffer *v4l2_buf)
1187 struct gspca_dev *gspca_dev = priv;
1188 struct gspca_frame *frame;
1190 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1191 || v4l2_buf->index < 0
1192 || v4l2_buf->index >= gspca_dev->nframes)
1195 frame = &gspca_dev->frame[v4l2_buf->index];
1196 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1200 static int vidioc_streamon(struct file *file, void *priv,
1201 enum v4l2_buf_type buf_type)
1203 struct gspca_dev *gspca_dev = priv;
1206 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1208 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1209 return -ERESTARTSYS;
1210 if (!gspca_dev->present) {
1214 if (gspca_dev->nframes == 0) {
1218 if (!gspca_dev->streaming) {
1219 ret = gspca_init_transfer(gspca_dev);
1224 if (gspca_debug & D_STREAM) {
1225 PDEBUG_MODE("stream on OK",
1233 mutex_unlock(&gspca_dev->queue_lock);
1237 static int vidioc_streamoff(struct file *file, void *priv,
1238 enum v4l2_buf_type buf_type)
1240 struct gspca_dev *gspca_dev = priv;
1243 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1245 if (!gspca_dev->streaming)
1247 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1248 return -ERESTARTSYS;
1250 /* stop streaming */
1251 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1255 gspca_stream_off(gspca_dev);
1256 mutex_unlock(&gspca_dev->usb_lock);
1258 /* empty the application queues */
1259 for (i = 0; i < gspca_dev->nframes; i++)
1260 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1261 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1262 gspca_dev->last_packet_type = DISCARD_PACKET;
1263 gspca_dev->sequence = 0;
1264 atomic_set(&gspca_dev->nevent, 0);
1267 mutex_unlock(&gspca_dev->queue_lock);
1271 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1272 struct v4l2_jpegcompression *jpegcomp)
1274 struct gspca_dev *gspca_dev = priv;
1277 if (!gspca_dev->sd_desc->get_jcomp)
1279 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1280 return -ERESTARTSYS;
1281 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1282 mutex_unlock(&gspca_dev->usb_lock);
1286 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1287 struct v4l2_jpegcompression *jpegcomp)
1289 struct gspca_dev *gspca_dev = priv;
1292 if (!gspca_dev->sd_desc->set_jcomp)
1294 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1295 return -ERESTARTSYS;
1296 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1297 mutex_unlock(&gspca_dev->usb_lock);
1301 static int vidioc_g_parm(struct file *filp, void *priv,
1302 struct v4l2_streamparm *parm)
1304 struct gspca_dev *gspca_dev = priv;
1306 memset(parm, 0, sizeof *parm);
1307 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1308 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1312 static int vidioc_s_parm(struct file *filp, void *priv,
1313 struct v4l2_streamparm *parm)
1315 struct gspca_dev *gspca_dev = priv;
1318 n = parm->parm.capture.readbuffers;
1319 if (n == 0 || n > GSPCA_MAX_FRAMES)
1320 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1322 gspca_dev->nbufread = n;
1326 static int vidioc_s_std(struct file *filp, void *priv,
1332 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1333 static int vidiocgmbuf(struct file *file, void *priv,
1334 struct video_mbuf *mbuf)
1336 struct gspca_dev *gspca_dev = file->private_data;
1339 PDEBUG(D_STREAM, "cgmbuf");
1340 if (gspca_dev->nframes == 0) {
1344 struct v4l2_format fmt;
1346 memset(&fmt, 0, sizeof fmt);
1347 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1348 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1349 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1350 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1351 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1352 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1357 struct v4l2_requestbuffers rb;
1359 memset(&rb, 0, sizeof rb);
1361 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 rb.memory = V4L2_MEMORY_MMAP;
1363 ret = vidioc_reqbufs(file, priv, &rb);
1368 mbuf->frames = gspca_dev->nframes;
1369 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1370 for (i = 0; i < mbuf->frames; i++)
1371 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1376 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1378 struct gspca_dev *gspca_dev = file->private_data;
1379 struct gspca_frame *frame;
1381 unsigned long addr, start, size;
1384 start = vma->vm_start;
1385 size = vma->vm_end - vma->vm_start;
1386 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1388 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1389 return -ERESTARTSYS;
1390 if (!gspca_dev->present) {
1394 if (gspca_dev->capt_file != file) {
1400 for (i = 0; i < gspca_dev->nframes; ++i) {
1401 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1402 PDEBUG(D_STREAM, "mmap bad memory type");
1405 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1407 frame = &gspca_dev->frame[i];
1411 if (frame == NULL) {
1412 PDEBUG(D_STREAM, "mmap no frame buffer found");
1416 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1417 /* v4l1 maps all the buffers */
1419 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1421 if (size != frame->v4l2_buf.length) {
1422 PDEBUG(D_STREAM, "mmap bad size");
1428 * - VM_IO marks the area as being a mmaped region for I/O to a
1429 * device. It also prevents the region from being core dumped.
1431 vma->vm_flags |= VM_IO;
1433 addr = (unsigned long) frame->data;
1435 page = vmalloc_to_page((void *) addr);
1436 ret = vm_insert_page(vma, start, page);
1444 vma->vm_ops = &gspca_vm_ops;
1445 vma->vm_private_data = frame;
1449 mutex_unlock(&gspca_dev->queue_lock);
1454 * wait for a video frame
1456 * If a frame is ready, its index is returned.
1458 static int frame_wait(struct gspca_dev *gspca_dev,
1461 struct gspca_frame *frame;
1464 /* check if a frame is ready */
1465 i = gspca_dev->fr_o;
1466 j = gspca_dev->fr_queue[i];
1467 frame = &gspca_dev->frame[j];
1468 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1469 atomic_dec(&gspca_dev->nevent);
1472 if (nonblock_ing) /* no frame yet */
1475 /* wait till a frame is ready */
1477 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1478 atomic_read(&gspca_dev->nevent) > 0,
1479 msecs_to_jiffies(3000));
1482 return ret; /* interrupt */
1483 return -EIO; /* timeout */
1485 atomic_dec(&gspca_dev->nevent);
1486 if (!gspca_dev->streaming || !gspca_dev->present)
1488 i = gspca_dev->fr_o;
1489 j = gspca_dev->fr_queue[i];
1490 frame = &gspca_dev->frame[j];
1491 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1495 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1496 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1501 if (gspca_dev->sd_desc->dq_callback) {
1502 mutex_lock(&gspca_dev->usb_lock);
1503 gspca_dev->sd_desc->dq_callback(gspca_dev);
1504 mutex_unlock(&gspca_dev->usb_lock);
1510 * dequeue a video buffer
1512 * If nonblock_ing is false, block until a buffer is available.
1514 static int vidioc_dqbuf(struct file *file, void *priv,
1515 struct v4l2_buffer *v4l2_buf)
1517 struct gspca_dev *gspca_dev = priv;
1518 struct gspca_frame *frame;
1521 PDEBUG(D_FRAM, "dqbuf");
1522 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1524 if (v4l2_buf->memory != gspca_dev->memory)
1527 /* if not streaming, be sure the application will not loop forever */
1528 if (!(file->f_flags & O_NONBLOCK)
1529 && !gspca_dev->streaming && gspca_dev->users == 1)
1532 /* only the capturing file may dequeue */
1533 if (gspca_dev->capt_file != file)
1536 /* only one dequeue / read at a time */
1537 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1538 return -ERESTARTSYS;
1540 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1543 i = ret; /* frame index */
1544 frame = &gspca_dev->frame[i];
1545 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1546 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1548 frame->v4l2_buf.bytesused)) {
1549 PDEBUG(D_ERR|D_STREAM,
1550 "dqbuf cp to user failed");
1555 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1556 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1557 PDEBUG(D_FRAM, "dqbuf %d", i);
1560 mutex_unlock(&gspca_dev->read_lock);
1565 * queue a video buffer
1567 * Attempting to queue a buffer that has already been
1568 * queued will return -EINVAL.
1570 static int vidioc_qbuf(struct file *file, void *priv,
1571 struct v4l2_buffer *v4l2_buf)
1573 struct gspca_dev *gspca_dev = priv;
1574 struct gspca_frame *frame;
1577 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1578 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1582 return -ERESTARTSYS;
1584 index = v4l2_buf->index;
1585 if ((unsigned) index >= gspca_dev->nframes) {
1587 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1591 if (v4l2_buf->memory != gspca_dev->memory) {
1592 PDEBUG(D_FRAM, "qbuf bad memory type");
1597 frame = &gspca_dev->frame[index];
1598 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1599 PDEBUG(D_FRAM, "qbuf bad state");
1604 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1606 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1607 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1608 frame->v4l2_buf.length = v4l2_buf->length;
1611 /* put the buffer in the 'queued' queue */
1612 i = gspca_dev->fr_q;
1613 gspca_dev->fr_queue[i] = index;
1614 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1615 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1620 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1621 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1624 mutex_unlock(&gspca_dev->queue_lock);
1629 * allocate the resources for read()
1631 static int read_alloc(struct gspca_dev *gspca_dev,
1634 struct v4l2_buffer v4l2_buf;
1637 PDEBUG(D_STREAM, "read alloc");
1638 if (gspca_dev->nframes == 0) {
1639 struct v4l2_requestbuffers rb;
1641 memset(&rb, 0, sizeof rb);
1642 rb.count = gspca_dev->nbufread;
1643 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1644 rb.memory = GSPCA_MEMORY_READ;
1645 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1647 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1650 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1651 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1652 v4l2_buf.memory = GSPCA_MEMORY_READ;
1653 for (i = 0; i < gspca_dev->nbufread; i++) {
1655 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1657 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1661 gspca_dev->memory = GSPCA_MEMORY_READ;
1664 /* start streaming */
1665 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1667 PDEBUG(D_STREAM, "read streamon err %d", ret);
1671 static unsigned int dev_poll(struct file *file, poll_table *wait)
1673 struct gspca_dev *gspca_dev = file->private_data;
1676 PDEBUG(D_FRAM, "poll");
1678 poll_wait(file, &gspca_dev->wq, wait);
1679 if (!gspca_dev->present)
1682 /* if reqbufs is not done, the user would use read() */
1683 if (gspca_dev->nframes == 0) {
1684 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1685 return POLLERR; /* not the 1st time */
1686 ret = read_alloc(gspca_dev, file);
1691 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1693 if (!gspca_dev->present) {
1698 /* check the next incoming buffer */
1699 i = gspca_dev->fr_o;
1700 i = gspca_dev->fr_queue[i];
1701 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1702 ret = POLLIN | POLLRDNORM; /* something to read */
1706 mutex_unlock(&gspca_dev->queue_lock);
1710 static ssize_t dev_read(struct file *file, char __user *data,
1711 size_t count, loff_t *ppos)
1713 struct gspca_dev *gspca_dev = file->private_data;
1714 struct gspca_frame *frame;
1715 struct v4l2_buffer v4l2_buf;
1716 struct timeval timestamp;
1719 PDEBUG(D_FRAM, "read (%zd)", count);
1720 if (!gspca_dev->present)
1722 switch (gspca_dev->memory) {
1723 case GSPCA_MEMORY_NO: /* first time */
1724 ret = read_alloc(gspca_dev, file);
1728 case GSPCA_MEMORY_READ:
1729 if (gspca_dev->capt_file == file)
1737 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1741 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1742 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1743 v4l2_buf.memory = GSPCA_MEMORY_READ;
1744 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1746 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1750 /* if the process slept for more than 1 second,
1751 * get a newer frame */
1752 frame = &gspca_dev->frame[v4l2_buf.index];
1754 break; /* avoid infinite loop */
1755 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1757 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1759 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1764 /* copy the frame */
1765 if (count > frame->v4l2_buf.bytesused)
1766 count = frame->v4l2_buf.bytesused;
1767 ret = copy_to_user(data, frame->data, count);
1769 PDEBUG(D_ERR|D_STREAM,
1770 "read cp to user lack %d / %zd", ret, count);
1776 /* in each case, requeue the buffer */
1777 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1783 static struct file_operations dev_fops = {
1784 .owner = THIS_MODULE,
1786 .release = dev_close,
1789 .ioctl = video_ioctl2,
1790 #ifdef CONFIG_COMPAT
1791 .compat_ioctl = v4l_compat_ioctl32,
1793 .llseek = no_llseek,
1797 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1798 .vidioc_querycap = vidioc_querycap,
1799 .vidioc_dqbuf = vidioc_dqbuf,
1800 .vidioc_qbuf = vidioc_qbuf,
1801 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1802 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1803 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1804 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1805 .vidioc_streamon = vidioc_streamon,
1806 .vidioc_queryctrl = vidioc_queryctrl,
1807 .vidioc_g_ctrl = vidioc_g_ctrl,
1808 .vidioc_s_ctrl = vidioc_s_ctrl,
1809 .vidioc_querymenu = vidioc_querymenu,
1810 .vidioc_enum_input = vidioc_enum_input,
1811 .vidioc_g_input = vidioc_g_input,
1812 .vidioc_s_input = vidioc_s_input,
1813 .vidioc_reqbufs = vidioc_reqbufs,
1814 .vidioc_querybuf = vidioc_querybuf,
1815 .vidioc_streamoff = vidioc_streamoff,
1816 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1817 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1818 .vidioc_g_parm = vidioc_g_parm,
1819 .vidioc_s_parm = vidioc_s_parm,
1820 .vidioc_s_std = vidioc_s_std,
1821 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1822 .vidiocgmbuf = vidiocgmbuf,
1826 static struct video_device gspca_template = {
1827 .name = "gspca main driver",
1829 .ioctl_ops = &dev_ioctl_ops,
1830 .release = video_device_release,
1835 * probe and create a new gspca device
1837 * This function must be called by the sub-driver when it is
1838 * called for probing a new device.
1840 int gspca_dev_probe(struct usb_interface *intf,
1841 const struct usb_device_id *id,
1842 const struct sd_desc *sd_desc,
1844 struct module *module)
1846 struct usb_interface_descriptor *interface;
1847 struct gspca_dev *gspca_dev;
1848 struct usb_device *dev = interface_to_usbdev(intf);
1851 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1853 /* we don't handle multi-config cameras */
1854 if (dev->descriptor.bNumConfigurations != 1)
1856 interface = &intf->cur_altsetting->desc;
1857 if (interface->bInterfaceNumber > 0)
1860 /* create the device */
1861 if (dev_size < sizeof *gspca_dev)
1862 dev_size = sizeof *gspca_dev;
1863 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1865 err("couldn't kzalloc gspca struct");
1868 kref_init(&gspca_dev->kref);
1869 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1870 if (!gspca_dev->usb_buf) {
1871 err("out of memory");
1875 gspca_dev->dev = dev;
1876 gspca_dev->iface = interface->bInterfaceNumber;
1877 gspca_dev->nbalt = intf->num_altsetting;
1878 gspca_dev->sd_desc = sd_desc;
1879 gspca_dev->nbufread = 2;
1880 gspca_dev->empty_packet = -1; /* don't check the empty packets */
1882 /* configure the subdriver and initialize the USB device */
1883 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1886 ret = gspca_dev->sd_desc->init(gspca_dev);
1889 ret = gspca_set_alt0(gspca_dev);
1892 gspca_set_default_mode(gspca_dev);
1894 mutex_init(&gspca_dev->usb_lock);
1895 mutex_init(&gspca_dev->read_lock);
1896 mutex_init(&gspca_dev->queue_lock);
1897 init_waitqueue_head(&gspca_dev->wq);
1899 /* init video stuff */
1900 gspca_dev->vdev = video_device_alloc();
1901 memcpy(gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1902 gspca_dev->vdev->parent = &dev->dev;
1903 gspca_dev->module = module;
1904 gspca_dev->present = 1;
1905 ret = video_register_device(&gspca_dev->vdev,
1909 err("video_register_device err %d", ret);
1913 usb_set_intfdata(intf, gspca_dev);
1914 PDEBUG(D_PROBE, "probe ok");
1917 kfree(gspca_dev->usb_buf);
1921 EXPORT_SYMBOL(gspca_dev_probe);
1926 * This function must be called by the sub-driver
1927 * when the device disconnects, after the specific resources are freed.
1929 void gspca_disconnect(struct usb_interface *intf)
1931 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1933 usb_set_intfdata(intf, NULL);
1935 /* release the device */
1936 /* (this will call gspca_release() immediatly or on last close) */
1937 video_unregister_device(&gspca_dev->vdev);
1939 kref_put(&gspca_dev->kref, gspca_delete);
1941 PDEBUG(D_PROBE, "disconnect complete");
1943 EXPORT_SYMBOL(gspca_disconnect);
1946 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1948 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1950 if (!gspca_dev->streaming)
1952 gspca_dev->frozen = 1; /* avoid urb error messages */
1953 if (gspca_dev->sd_desc->stopN)
1954 gspca_dev->sd_desc->stopN(gspca_dev);
1955 destroy_urbs(gspca_dev);
1956 gspca_set_alt0(gspca_dev);
1957 if (gspca_dev->sd_desc->stop0)
1958 gspca_dev->sd_desc->stop0(gspca_dev);
1961 EXPORT_SYMBOL(gspca_suspend);
1963 int gspca_resume(struct usb_interface *intf)
1965 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1967 gspca_dev->frozen = 0;
1968 gspca_dev->sd_desc->init(gspca_dev);
1969 if (gspca_dev->streaming)
1970 return gspca_init_transfer(gspca_dev);
1973 EXPORT_SYMBOL(gspca_resume);
1975 /* -- cam driver utility functions -- */
1977 /* auto gain and exposure algorithm based on the knee algorithm described here:
1978 http://ytse.tricolour.net/docs/LowLightOptimization.html
1980 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1982 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1983 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1985 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1986 const struct ctrl *gain_ctrl = NULL;
1987 const struct ctrl *exposure_ctrl = NULL;
1988 const struct ctrl *autogain_ctrl = NULL;
1991 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1992 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1993 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1994 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1995 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1996 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1997 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1999 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2000 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2001 "on cam without (auto)gain/exposure");
2005 if (gain_ctrl->get(gspca_dev, &gain) ||
2006 exposure_ctrl->get(gspca_dev, &exposure) ||
2007 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2011 orig_exposure = exposure;
2013 /* If we are of a multiple of deadzone, do multiple steps to reach the
2014 desired lumination fast (with the risc of a slight overshoot) */
2015 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2017 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2018 avg_lum, desired_avg_lum, steps);
2020 for (i = 0; i < steps; i++) {
2021 if (avg_lum > desired_avg_lum) {
2022 if (gain > gain_knee)
2024 else if (exposure > exposure_knee)
2026 else if (gain > gain_ctrl->qctrl.default_value)
2028 else if (exposure > exposure_ctrl->qctrl.minimum)
2030 else if (gain > gain_ctrl->qctrl.minimum)
2035 if (gain < gain_ctrl->qctrl.default_value)
2037 else if (exposure < exposure_knee)
2039 else if (gain < gain_knee)
2041 else if (exposure < exposure_ctrl->qctrl.maximum)
2043 else if (gain < gain_ctrl->qctrl.maximum)
2050 if (gain != orig_gain) {
2051 gain_ctrl->set(gspca_dev, gain);
2054 if (exposure != orig_exposure) {
2055 exposure_ctrl->set(gspca_dev, exposure);
2061 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2063 /* -- module insert / remove -- */
2064 static int __init gspca_init(void)
2066 info("main v%d.%d.%d registered",
2067 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2068 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2069 DRIVER_VERSION_NUMBER & 0xff);
2072 static void __exit gspca_exit(void)
2074 info("main deregistered");
2077 module_init(gspca_init);
2078 module_exit(gspca_exit);
2081 module_param_named(debug, gspca_debug, int, 0644);
2082 MODULE_PARM_DESC(debug,
2083 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2084 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"