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 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, 5, 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 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)
188 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
190 PDEBUG(D_PACK, "isoc irq");
191 if (!gspca_dev->streaming)
193 fill_frame(gspca_dev, urb);
197 * bulk message interrupt from the USB device
199 static void bulk_irq(struct urb *urb)
201 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
202 struct gspca_frame *frame;
205 PDEBUG(D_PACK, "bulk irq");
206 if (!gspca_dev->streaming)
208 switch (urb->status) {
216 if (!gspca_dev->frozen)
218 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
219 return; /* disconnection ? */
222 /* check the availability of the frame buffer */
223 frame = gspca_get_i_frame(gspca_dev);
225 gspca_dev->last_packet_type = DISCARD_PACKET;
227 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
228 gspca_dev->sd_desc->pkt_scan(gspca_dev,
230 urb->transfer_buffer,
234 /* resubmit the URB */
235 if (gspca_dev->cam.bulk_nurbs != 0) {
236 st = usb_submit_urb(urb, GFP_ATOMIC);
238 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
243 * add data to the current frame
245 * This function is called by the subdrivers at interrupt level.
247 * To build a frame, these ones must add
249 * - 0 or many INTER_PACKETs
251 * DISCARD_PACKET invalidates the whole frame.
252 * On LAST_PACKET, a new frame is returned.
254 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
255 enum gspca_packet_type packet_type,
256 struct gspca_frame *frame,
262 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
264 /* when start of a new frame, if the current frame buffer
265 * is not queued, discard the whole frame */
266 if (packet_type == FIRST_PACKET) {
267 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
268 != V4L2_BUF_FLAG_QUEUED) {
269 gspca_dev->last_packet_type = DISCARD_PACKET;
272 frame->data_end = frame->data;
273 jiffies_to_timeval(get_jiffies_64(),
274 &frame->v4l2_buf.timestamp);
275 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
276 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
277 if (packet_type == LAST_PACKET)
278 gspca_dev->last_packet_type = packet_type;
282 /* append the packet to the frame buffer */
284 if (frame->data_end - frame->data + len
285 > frame->v4l2_buf.length) {
286 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
287 frame->data_end - frame->data + len,
288 frame->v4l2_buf.length);
289 packet_type = DISCARD_PACKET;
291 memcpy(frame->data_end, data, len);
292 frame->data_end += len;
295 gspca_dev->last_packet_type = packet_type;
297 /* if last packet, wake up the application and advance in the queue */
298 if (packet_type == LAST_PACKET) {
299 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
300 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
301 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
302 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
303 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
305 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
306 frame->v4l2_buf.bytesused,
310 j = gspca_dev->fr_queue[i];
311 frame = &gspca_dev->frame[j];
315 EXPORT_SYMBOL(gspca_frame_add);
317 static int gspca_is_compressed(__u32 format)
320 case V4L2_PIX_FMT_MJPEG:
321 case V4L2_PIX_FMT_JPEG:
322 case V4L2_PIX_FMT_SPCA561:
323 case V4L2_PIX_FMT_PAC207:
324 case V4L2_PIX_FMT_MR97310A:
330 static void *rvmalloc(unsigned long size)
335 mem = vmalloc_32(size);
337 adr = (unsigned long) mem;
338 while ((long) size > 0) {
339 SetPageReserved(vmalloc_to_page((void *) adr));
347 static void rvfree(void *mem, long size)
351 adr = (unsigned long) mem;
353 ClearPageReserved(vmalloc_to_page((void *) adr));
360 static int frame_alloc(struct gspca_dev *gspca_dev,
363 struct gspca_frame *frame;
367 i = gspca_dev->curr_mode;
368 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
369 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
370 frsz = PAGE_ALIGN(frsz);
371 gspca_dev->frsz = frsz;
372 if (count > GSPCA_MAX_FRAMES)
373 count = GSPCA_MAX_FRAMES;
374 gspca_dev->frbuf = rvmalloc(frsz * count);
375 if (!gspca_dev->frbuf) {
376 err("frame alloc failed");
379 gspca_dev->nframes = count;
380 for (i = 0; i < count; i++) {
381 frame = &gspca_dev->frame[i];
382 frame->v4l2_buf.index = i;
383 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
384 frame->v4l2_buf.flags = 0;
385 frame->v4l2_buf.field = V4L2_FIELD_NONE;
386 frame->v4l2_buf.length = frsz;
387 frame->v4l2_buf.memory = gspca_dev->memory;
388 frame->v4l2_buf.sequence = 0;
389 frame->data = frame->data_end =
390 gspca_dev->frbuf + i * frsz;
391 frame->v4l2_buf.m.offset = i * frsz;
393 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
394 gspca_dev->last_packet_type = DISCARD_PACKET;
395 gspca_dev->sequence = 0;
399 static void frame_free(struct gspca_dev *gspca_dev)
403 PDEBUG(D_STREAM, "frame free");
404 if (gspca_dev->frbuf != NULL) {
405 rvfree(gspca_dev->frbuf,
406 gspca_dev->nframes * gspca_dev->frsz);
407 gspca_dev->frbuf = NULL;
408 for (i = 0; i < gspca_dev->nframes; i++)
409 gspca_dev->frame[i].data = NULL;
411 gspca_dev->nframes = 0;
414 static void destroy_urbs(struct gspca_dev *gspca_dev)
419 PDEBUG(D_STREAM, "kill transfer");
420 for (i = 0; i < MAX_NURBS; i++) {
421 urb = gspca_dev->urb[i];
425 BUG_ON(!gspca_dev->dev);
426 gspca_dev->urb[i] = NULL;
427 if (!gspca_dev->present)
429 if (urb->transfer_buffer != NULL)
430 usb_buffer_free(gspca_dev->dev,
431 urb->transfer_buffer_length,
432 urb->transfer_buffer,
439 * look for an input transfer endpoint in an alternate setting
441 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
444 struct usb_host_endpoint *ep;
447 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
448 ep = &alt->endpoint[i];
449 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
457 * look for an input (isoc or bulk) endpoint
459 * The endpoint is defined by the subdriver.
460 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
461 * This routine may be called many times when the bandwidth is too small
462 * (the bandwidth is checked on urb submit).
464 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
466 struct usb_interface *intf;
467 struct usb_host_endpoint *ep;
470 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
472 i = gspca_dev->alt; /* previous alt setting */
475 while (--i > 0) { /* alt 0 is unusable */
476 ep = alt_xfer(&intf->altsetting[i],
477 USB_ENDPOINT_XFER_ISOC);
482 /* if no isoc, try bulk */
484 ep = alt_xfer(&intf->altsetting[0],
485 USB_ENDPOINT_XFER_BULK);
487 err("no transfer endpoint found");
491 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
492 i, ep->desc.bEndpointAddress);
494 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
496 err("set interface err %d", ret);
500 gspca_dev->alt = i; /* memorize the current alt setting */
505 * create the URBs for image transfer
507 static int create_urbs(struct gspca_dev *gspca_dev,
508 struct usb_host_endpoint *ep)
511 int n, nurbs, i, psize, npkt, bsize;
513 /* calculate the packet size and the number of packets */
514 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
516 if (gspca_dev->alt != 0) { /* isoc */
518 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
519 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
520 npkt = ISO_MAX_SIZE / psize;
521 if (npkt > ISO_MAX_PKT)
523 bsize = psize * npkt;
525 "isoc %d pkts size %d = bsize:%d",
530 bsize = gspca_dev->cam.bulk_size;
533 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
534 if (gspca_dev->cam.bulk_nurbs != 0)
535 nurbs = gspca_dev->cam.bulk_nurbs;
540 gspca_dev->nurbs = nurbs;
541 for (n = 0; n < nurbs; n++) {
542 urb = usb_alloc_urb(npkt, GFP_KERNEL);
544 err("usb_alloc_urb failed");
545 destroy_urbs(gspca_dev);
548 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
553 if (urb->transfer_buffer == NULL) {
555 err("usb_buffer_urb failed");
556 destroy_urbs(gspca_dev);
559 gspca_dev->urb[n] = urb;
560 urb->dev = gspca_dev->dev;
561 urb->context = gspca_dev;
562 urb->transfer_buffer_length = bsize;
563 if (npkt != 0) { /* ISOC */
564 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
565 ep->desc.bEndpointAddress);
566 urb->transfer_flags = URB_ISO_ASAP
567 | URB_NO_TRANSFER_DMA_MAP;
568 urb->interval = ep->desc.bInterval;
569 urb->complete = isoc_irq;
570 urb->number_of_packets = npkt;
571 for (i = 0; i < npkt; i++) {
572 urb->iso_frame_desc[i].length = psize;
573 urb->iso_frame_desc[i].offset = psize * i;
576 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
577 ep->desc.bEndpointAddress),
578 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
579 urb->complete = bulk_irq;
586 * start the USB transfer
588 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
590 struct usb_host_endpoint *ep;
593 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
596 /* set the higher alternate setting and
597 * loop until urb submit succeeds */
598 gspca_dev->alt = gspca_dev->nbalt;
600 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
601 ep = get_ep(gspca_dev);
606 ret = create_urbs(gspca_dev, ep);
610 /* clear the bulk endpoint */
611 if (gspca_dev->alt == 0) /* if bulk transfer */
612 usb_clear_halt(gspca_dev->dev,
613 gspca_dev->urb[0]->pipe);
616 ret = gspca_dev->sd_desc->start(gspca_dev);
618 destroy_urbs(gspca_dev);
621 gspca_dev->streaming = 1;
623 /* some bulk transfers are started by the subdriver */
624 if (gspca_dev->alt == 0 && gspca_dev->cam.bulk_nurbs == 0)
627 /* submit the URBs */
628 for (n = 0; n < gspca_dev->nurbs; n++) {
629 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
631 PDEBUG(D_ERR|D_STREAM,
632 "usb_submit_urb [%d] err %d", n, ret);
633 gspca_dev->streaming = 0;
634 destroy_urbs(gspca_dev);
635 if (ret == -ENOSPC) {
636 msleep(20); /* wait for kill
638 break; /* try the previous alt */
647 mutex_unlock(&gspca_dev->usb_lock);
651 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
655 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
657 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
661 /* Note: both the queue and the usb locks should be held when calling this */
662 static void gspca_stream_off(struct gspca_dev *gspca_dev)
664 gspca_dev->streaming = 0;
665 if (gspca_dev->present
666 && gspca_dev->sd_desc->stopN)
667 gspca_dev->sd_desc->stopN(gspca_dev);
668 destroy_urbs(gspca_dev);
669 if (gspca_dev->present)
670 gspca_set_alt0(gspca_dev);
671 if (gspca_dev->sd_desc->stop0)
672 gspca_dev->sd_desc->stop0(gspca_dev);
673 PDEBUG(D_STREAM, "stream off OK");
676 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
680 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
681 gspca_dev->curr_mode = i;
682 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
683 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
684 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
687 static int wxh_to_mode(struct gspca_dev *gspca_dev,
688 int width, int height)
692 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
693 if (width >= gspca_dev->cam.cam_mode[i].width
694 && height >= gspca_dev->cam.cam_mode[i].height)
701 * search a mode with the right pixel format
703 static int gspca_get_mode(struct gspca_dev *gspca_dev,
709 modeU = modeD = mode;
710 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
712 if (gspca_dev->cam.cam_mode[modeD].pixelformat
716 if (++modeU < gspca_dev->cam.nmodes) {
717 if (gspca_dev->cam.cam_mode[modeU].pixelformat
725 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
726 struct v4l2_fmtdesc *fmtdesc)
728 struct gspca_dev *gspca_dev = priv;
732 /* give an index to each format */
735 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
736 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
739 if (fmt_tb[j] == fmt_tb[index])
744 if (fmtdesc->index == index)
745 break; /* new format */
747 if (index >= ARRAY_SIZE(fmt_tb))
752 return -EINVAL; /* no more format */
754 fmtdesc->pixelformat = fmt_tb[index];
755 if (gspca_is_compressed(fmt_tb[index]))
756 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
757 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
758 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
759 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
760 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
761 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
762 fmtdesc->description[4] = '\0';
766 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
767 struct v4l2_format *fmt)
769 struct gspca_dev *gspca_dev = priv;
772 mode = gspca_dev->curr_mode;
773 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
774 sizeof fmt->fmt.pix);
778 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
779 struct v4l2_format *fmt)
781 int w, h, mode, mode2;
783 w = fmt->fmt.pix.width;
784 h = fmt->fmt.pix.height;
787 if (gspca_debug & D_CONF)
788 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
790 /* search the closest mode for width and height */
791 mode = wxh_to_mode(gspca_dev, w, h);
793 /* OK if right palette */
794 if (gspca_dev->cam.cam_mode[mode].pixelformat
795 != fmt->fmt.pix.pixelformat) {
797 /* else, search the closest mode with the same pixel format */
798 mode2 = gspca_get_mode(gspca_dev, mode,
799 fmt->fmt.pix.pixelformat);
803 ; * no chance, return this mode */
805 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
806 sizeof fmt->fmt.pix);
807 return mode; /* used when s_fmt */
810 static int vidioc_try_fmt_vid_cap(struct file *file,
812 struct v4l2_format *fmt)
814 struct gspca_dev *gspca_dev = priv;
817 ret = try_fmt_vid_cap(gspca_dev, fmt);
823 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
824 struct v4l2_format *fmt)
826 struct gspca_dev *gspca_dev = priv;
829 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
832 ret = try_fmt_vid_cap(gspca_dev, fmt);
836 if (gspca_dev->nframes != 0
837 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
842 if (ret == gspca_dev->curr_mode) {
844 goto out; /* same mode */
847 if (gspca_dev->streaming) {
851 gspca_dev->width = fmt->fmt.pix.width;
852 gspca_dev->height = fmt->fmt.pix.height;
853 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
854 gspca_dev->curr_mode = ret;
858 mutex_unlock(&gspca_dev->queue_lock);
862 static void gspca_release(struct video_device *vfd)
864 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
866 PDEBUG(D_STREAM, "device released");
868 kfree(gspca_dev->usb_buf);
872 static int dev_open(struct file *file)
874 struct gspca_dev *gspca_dev;
877 PDEBUG(D_STREAM, "%s open", current->comm);
878 gspca_dev = (struct gspca_dev *) video_devdata(file);
879 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
881 if (!gspca_dev->present) {
886 if (gspca_dev->users > 4) { /* (arbitrary value) */
891 /* protect the subdriver against rmmod */
892 if (!try_module_get(gspca_dev->module)) {
899 file->private_data = gspca_dev;
901 /* activate the v4l2 debug */
902 if (gspca_debug & D_V4L2)
903 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
904 | V4L2_DEBUG_IOCTL_ARG;
906 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
907 | V4L2_DEBUG_IOCTL_ARG);
911 mutex_unlock(&gspca_dev->queue_lock);
913 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
915 PDEBUG(D_STREAM, "open done");
919 static int dev_close(struct file *file)
921 struct gspca_dev *gspca_dev = file->private_data;
923 PDEBUG(D_STREAM, "%s close", current->comm);
924 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
928 /* if the file did the capture, free the streaming resources */
929 if (gspca_dev->capt_file == file) {
930 if (gspca_dev->streaming) {
931 mutex_lock(&gspca_dev->usb_lock);
932 gspca_stream_off(gspca_dev);
933 mutex_unlock(&gspca_dev->usb_lock);
935 frame_free(gspca_dev);
936 gspca_dev->capt_file = NULL;
937 gspca_dev->memory = GSPCA_MEMORY_NO;
939 file->private_data = NULL;
940 module_put(gspca_dev->module);
941 mutex_unlock(&gspca_dev->queue_lock);
943 PDEBUG(D_STREAM, "close done");
948 static int vidioc_querycap(struct file *file, void *priv,
949 struct v4l2_capability *cap)
951 struct gspca_dev *gspca_dev = priv;
953 memset(cap, 0, sizeof *cap);
954 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
955 if (gspca_dev->dev->product != NULL) {
956 strncpy(cap->card, gspca_dev->dev->product,
959 snprintf(cap->card, sizeof cap->card,
960 "USB Camera (%04x:%04x)",
961 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
962 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
964 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
965 sizeof cap->bus_info);
966 cap->version = DRIVER_VERSION_NUMBER;
967 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
969 | V4L2_CAP_READWRITE;
973 static int vidioc_queryctrl(struct file *file, void *priv,
974 struct v4l2_queryctrl *q_ctrl)
976 struct gspca_dev *gspca_dev = priv;
982 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
983 id &= V4L2_CTRL_ID_MASK;
985 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
986 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
992 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
993 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
998 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
999 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1006 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
1008 if (gspca_dev->ctrl_dis & (1 << ix))
1009 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1013 static int vidioc_s_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1016 struct gspca_dev *gspca_dev = priv;
1017 const struct ctrl *ctrls;
1020 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1021 i < gspca_dev->sd_desc->nctrls;
1023 if (ctrl->id != ctrls->qctrl.id)
1025 if (gspca_dev->ctrl_dis & (1 << i))
1027 if (ctrl->value < ctrls->qctrl.minimum
1028 || ctrl->value > ctrls->qctrl.maximum)
1030 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1031 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1032 return -ERESTARTSYS;
1033 ret = ctrls->set(gspca_dev, ctrl->value);
1034 mutex_unlock(&gspca_dev->usb_lock);
1040 static int vidioc_g_ctrl(struct file *file, void *priv,
1041 struct v4l2_control *ctrl)
1043 struct gspca_dev *gspca_dev = priv;
1045 const struct ctrl *ctrls;
1048 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1049 i < gspca_dev->sd_desc->nctrls;
1051 if (ctrl->id != ctrls->qctrl.id)
1053 if (gspca_dev->ctrl_dis & (1 << i))
1055 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1056 return -ERESTARTSYS;
1057 ret = ctrls->get(gspca_dev, &ctrl->value);
1058 mutex_unlock(&gspca_dev->usb_lock);
1064 /*fixme: have an audio flag in gspca_dev?*/
1065 static int vidioc_s_audio(struct file *file, void *priv,
1066 struct v4l2_audio *audio)
1068 if (audio->index != 0)
1073 static int vidioc_g_audio(struct file *file, void *priv,
1074 struct v4l2_audio *audio)
1076 memset(audio, 0, sizeof *audio);
1077 strcpy(audio->name, "Microphone");
1081 static int vidioc_enumaudio(struct file *file, void *priv,
1082 struct v4l2_audio *audio)
1084 if (audio->index != 0)
1087 strcpy(audio->name, "Microphone");
1088 audio->capability = 0;
1093 static int vidioc_querymenu(struct file *file, void *priv,
1094 struct v4l2_querymenu *qmenu)
1096 struct gspca_dev *gspca_dev = priv;
1098 if (!gspca_dev->sd_desc->querymenu)
1100 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1103 static int vidioc_enum_input(struct file *file, void *priv,
1104 struct v4l2_input *input)
1106 struct gspca_dev *gspca_dev = priv;
1108 if (input->index != 0)
1110 memset(input, 0, sizeof *input);
1111 input->type = V4L2_INPUT_TYPE_CAMERA;
1112 strncpy(input->name, gspca_dev->sd_desc->name,
1113 sizeof input->name);
1117 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1123 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1130 static int vidioc_reqbufs(struct file *file, void *priv,
1131 struct v4l2_requestbuffers *rb)
1133 struct gspca_dev *gspca_dev = priv;
1136 switch (rb->memory) {
1137 case GSPCA_MEMORY_READ: /* (internal call) */
1138 case V4L2_MEMORY_MMAP:
1139 case V4L2_MEMORY_USERPTR:
1144 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1145 return -ERESTARTSYS;
1147 if (gspca_dev->memory != GSPCA_MEMORY_NO
1148 && gspca_dev->memory != rb->memory) {
1153 /* only one file may do the capture */
1154 if (gspca_dev->capt_file != NULL
1155 && gspca_dev->capt_file != file) {
1160 /* if allocated, the buffers must not be mapped */
1161 for (i = 0; i < gspca_dev->nframes; i++) {
1162 if (gspca_dev->frame[i].vma_use_count) {
1168 /* stop streaming */
1169 if (gspca_dev->streaming) {
1170 mutex_lock(&gspca_dev->usb_lock);
1171 gspca_stream_off(gspca_dev);
1172 mutex_unlock(&gspca_dev->usb_lock);
1175 /* free the previous allocated buffers, if any */
1176 if (gspca_dev->nframes != 0) {
1177 frame_free(gspca_dev);
1178 gspca_dev->capt_file = NULL;
1180 if (rb->count == 0) /* unrequest */
1182 gspca_dev->memory = rb->memory;
1183 ret = frame_alloc(gspca_dev, rb->count);
1185 rb->count = gspca_dev->nframes;
1186 gspca_dev->capt_file = file;
1189 mutex_unlock(&gspca_dev->queue_lock);
1190 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1194 static int vidioc_querybuf(struct file *file, void *priv,
1195 struct v4l2_buffer *v4l2_buf)
1197 struct gspca_dev *gspca_dev = priv;
1198 struct gspca_frame *frame;
1200 if (v4l2_buf->index < 0
1201 || v4l2_buf->index >= gspca_dev->nframes)
1204 frame = &gspca_dev->frame[v4l2_buf->index];
1205 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1209 static int vidioc_streamon(struct file *file, void *priv,
1210 enum v4l2_buf_type buf_type)
1212 struct gspca_dev *gspca_dev = priv;
1215 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1218 return -ERESTARTSYS;
1219 if (!gspca_dev->present) {
1223 if (gspca_dev->nframes == 0
1224 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1228 if (!gspca_dev->streaming) {
1229 ret = gspca_init_transfer(gspca_dev);
1234 if (gspca_debug & D_STREAM) {
1235 PDEBUG_MODE("stream on OK",
1243 mutex_unlock(&gspca_dev->queue_lock);
1247 static int vidioc_streamoff(struct file *file, void *priv,
1248 enum v4l2_buf_type buf_type)
1250 struct gspca_dev *gspca_dev = priv;
1253 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1255 if (!gspca_dev->streaming)
1257 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1258 return -ERESTARTSYS;
1260 /* stop streaming */
1261 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1265 gspca_stream_off(gspca_dev);
1266 mutex_unlock(&gspca_dev->usb_lock);
1268 /* empty the application queues */
1269 for (i = 0; i < gspca_dev->nframes; i++)
1270 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1271 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1272 gspca_dev->last_packet_type = DISCARD_PACKET;
1273 gspca_dev->sequence = 0;
1276 mutex_unlock(&gspca_dev->queue_lock);
1280 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1281 struct v4l2_jpegcompression *jpegcomp)
1283 struct gspca_dev *gspca_dev = priv;
1286 if (!gspca_dev->sd_desc->get_jcomp)
1288 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1289 return -ERESTARTSYS;
1290 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1291 mutex_unlock(&gspca_dev->usb_lock);
1295 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1296 struct v4l2_jpegcompression *jpegcomp)
1298 struct gspca_dev *gspca_dev = priv;
1301 if (!gspca_dev->sd_desc->set_jcomp)
1303 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1304 return -ERESTARTSYS;
1305 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1306 mutex_unlock(&gspca_dev->usb_lock);
1310 static int vidioc_g_parm(struct file *filp, void *priv,
1311 struct v4l2_streamparm *parm)
1313 struct gspca_dev *gspca_dev = priv;
1315 memset(parm, 0, sizeof *parm);
1316 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1317 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1319 if (gspca_dev->sd_desc->get_streamparm) {
1322 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1323 return -ERESTARTSYS;
1324 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1325 mutex_unlock(&gspca_dev->usb_lock);
1332 static int vidioc_s_parm(struct file *filp, void *priv,
1333 struct v4l2_streamparm *parm)
1335 struct gspca_dev *gspca_dev = priv;
1338 n = parm->parm.capture.readbuffers;
1339 if (n == 0 || n > GSPCA_MAX_FRAMES)
1340 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1342 gspca_dev->nbufread = n;
1344 if (gspca_dev->sd_desc->set_streamparm) {
1347 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1348 return -ERESTARTSYS;
1349 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1350 mutex_unlock(&gspca_dev->usb_lock);
1357 static int vidioc_s_std(struct file *filp, void *priv,
1363 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1364 static int vidiocgmbuf(struct file *file, void *priv,
1365 struct video_mbuf *mbuf)
1367 struct gspca_dev *gspca_dev = file->private_data;
1370 PDEBUG(D_STREAM, "cgmbuf");
1371 if (gspca_dev->nframes == 0) {
1375 struct v4l2_format fmt;
1377 memset(&fmt, 0, sizeof fmt);
1378 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1379 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1380 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1381 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1382 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1383 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1388 struct v4l2_requestbuffers rb;
1390 memset(&rb, 0, sizeof rb);
1392 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1393 rb.memory = V4L2_MEMORY_MMAP;
1394 ret = vidioc_reqbufs(file, priv, &rb);
1399 mbuf->frames = gspca_dev->nframes;
1400 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1401 for (i = 0; i < mbuf->frames; i++)
1402 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1407 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1409 struct gspca_dev *gspca_dev = file->private_data;
1410 struct gspca_frame *frame;
1412 unsigned long addr, start, size;
1415 start = vma->vm_start;
1416 size = vma->vm_end - vma->vm_start;
1417 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1419 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1420 return -ERESTARTSYS;
1421 if (!gspca_dev->present) {
1425 if (gspca_dev->capt_file != file) {
1431 for (i = 0; i < gspca_dev->nframes; ++i) {
1432 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1433 PDEBUG(D_STREAM, "mmap bad memory type");
1436 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1438 frame = &gspca_dev->frame[i];
1442 if (frame == NULL) {
1443 PDEBUG(D_STREAM, "mmap no frame buffer found");
1447 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1448 /* v4l1 maps all the buffers */
1450 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1452 if (size != frame->v4l2_buf.length) {
1453 PDEBUG(D_STREAM, "mmap bad size");
1459 * - VM_IO marks the area as being a mmaped region for I/O to a
1460 * device. It also prevents the region from being core dumped.
1462 vma->vm_flags |= VM_IO;
1464 addr = (unsigned long) frame->data;
1466 page = vmalloc_to_page((void *) addr);
1467 ret = vm_insert_page(vma, start, page);
1475 vma->vm_ops = &gspca_vm_ops;
1476 vma->vm_private_data = frame;
1480 mutex_unlock(&gspca_dev->queue_lock);
1485 * wait for a video frame
1487 * If a frame is ready, its index is returned.
1489 static int frame_wait(struct gspca_dev *gspca_dev,
1492 struct gspca_frame *frame;
1495 /* check if a frame is ready */
1496 i = gspca_dev->fr_o;
1497 j = gspca_dev->fr_queue[i];
1498 frame = &gspca_dev->frame[j];
1500 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1504 /* wait till a frame is ready */
1505 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1506 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1507 !gspca_dev->streaming || !gspca_dev->present,
1508 msecs_to_jiffies(3000));
1511 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1515 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1516 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1521 if (gspca_dev->sd_desc->dq_callback) {
1522 mutex_lock(&gspca_dev->usb_lock);
1523 gspca_dev->sd_desc->dq_callback(gspca_dev);
1524 mutex_unlock(&gspca_dev->usb_lock);
1530 * dequeue a video buffer
1532 * If nonblock_ing is false, block until a buffer is available.
1534 static int vidioc_dqbuf(struct file *file, void *priv,
1535 struct v4l2_buffer *v4l2_buf)
1537 struct gspca_dev *gspca_dev = priv;
1538 struct gspca_frame *frame;
1541 PDEBUG(D_FRAM, "dqbuf");
1542 if (v4l2_buf->memory != gspca_dev->memory)
1545 /* if not streaming, be sure the application will not loop forever */
1546 if (!(file->f_flags & O_NONBLOCK)
1547 && !gspca_dev->streaming && gspca_dev->users == 1)
1550 /* only the capturing file may dequeue */
1551 if (gspca_dev->capt_file != file)
1554 /* only one dequeue / read at a time */
1555 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1556 return -ERESTARTSYS;
1558 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1561 i = ret; /* frame index */
1562 frame = &gspca_dev->frame[i];
1563 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1564 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1566 frame->v4l2_buf.bytesused)) {
1567 PDEBUG(D_ERR|D_STREAM,
1568 "dqbuf cp to user failed");
1573 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1574 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1575 PDEBUG(D_FRAM, "dqbuf %d", i);
1578 mutex_unlock(&gspca_dev->read_lock);
1583 * queue a video buffer
1585 * Attempting to queue a buffer that has already been
1586 * queued will return -EINVAL.
1588 static int vidioc_qbuf(struct file *file, void *priv,
1589 struct v4l2_buffer *v4l2_buf)
1591 struct gspca_dev *gspca_dev = priv;
1592 struct gspca_frame *frame;
1595 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1597 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1598 return -ERESTARTSYS;
1600 index = v4l2_buf->index;
1601 if ((unsigned) index >= gspca_dev->nframes) {
1603 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1607 if (v4l2_buf->memory != gspca_dev->memory) {
1608 PDEBUG(D_FRAM, "qbuf bad memory type");
1613 frame = &gspca_dev->frame[index];
1614 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1615 PDEBUG(D_FRAM, "qbuf bad state");
1620 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1622 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1623 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1624 frame->v4l2_buf.length = v4l2_buf->length;
1627 /* put the buffer in the 'queued' queue */
1628 i = gspca_dev->fr_q;
1629 gspca_dev->fr_queue[i] = index;
1630 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1631 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1636 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1637 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1640 mutex_unlock(&gspca_dev->queue_lock);
1645 * allocate the resources for read()
1647 static int read_alloc(struct gspca_dev *gspca_dev,
1650 struct v4l2_buffer v4l2_buf;
1653 PDEBUG(D_STREAM, "read alloc");
1654 if (gspca_dev->nframes == 0) {
1655 struct v4l2_requestbuffers rb;
1657 memset(&rb, 0, sizeof rb);
1658 rb.count = gspca_dev->nbufread;
1659 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1660 rb.memory = GSPCA_MEMORY_READ;
1661 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1663 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1666 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1667 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1668 v4l2_buf.memory = GSPCA_MEMORY_READ;
1669 for (i = 0; i < gspca_dev->nbufread; i++) {
1671 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1673 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1677 gspca_dev->memory = GSPCA_MEMORY_READ;
1680 /* start streaming */
1681 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1683 PDEBUG(D_STREAM, "read streamon err %d", ret);
1687 static unsigned int dev_poll(struct file *file, poll_table *wait)
1689 struct gspca_dev *gspca_dev = file->private_data;
1692 PDEBUG(D_FRAM, "poll");
1694 poll_wait(file, &gspca_dev->wq, wait);
1695 if (!gspca_dev->present)
1698 /* if reqbufs is not done, the user would use read() */
1699 if (gspca_dev->nframes == 0) {
1700 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1701 return POLLERR; /* not the 1st time */
1702 ret = read_alloc(gspca_dev, file);
1707 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1709 if (!gspca_dev->present) {
1714 /* check the next incoming buffer */
1715 i = gspca_dev->fr_o;
1716 i = gspca_dev->fr_queue[i];
1717 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1718 ret = POLLIN | POLLRDNORM; /* something to read */
1722 mutex_unlock(&gspca_dev->queue_lock);
1726 static ssize_t dev_read(struct file *file, char __user *data,
1727 size_t count, loff_t *ppos)
1729 struct gspca_dev *gspca_dev = file->private_data;
1730 struct gspca_frame *frame;
1731 struct v4l2_buffer v4l2_buf;
1732 struct timeval timestamp;
1735 PDEBUG(D_FRAM, "read (%zd)", count);
1736 if (!gspca_dev->present)
1738 switch (gspca_dev->memory) {
1739 case GSPCA_MEMORY_NO: /* first time */
1740 ret = read_alloc(gspca_dev, file);
1744 case GSPCA_MEMORY_READ:
1745 if (gspca_dev->capt_file == file)
1753 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1757 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1758 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1759 v4l2_buf.memory = GSPCA_MEMORY_READ;
1760 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1762 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1766 /* if the process slept for more than 1 second,
1767 * get a newer frame */
1768 frame = &gspca_dev->frame[v4l2_buf.index];
1770 break; /* avoid infinite loop */
1771 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1773 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1775 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1780 /* copy the frame */
1781 if (count > frame->v4l2_buf.bytesused)
1782 count = frame->v4l2_buf.bytesused;
1783 ret = copy_to_user(data, frame->data, count);
1785 PDEBUG(D_ERR|D_STREAM,
1786 "read cp to user lack %d / %zd", ret, count);
1792 /* in each case, requeue the buffer */
1793 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1799 static struct v4l2_file_operations dev_fops = {
1800 .owner = THIS_MODULE,
1802 .release = dev_close,
1805 .unlocked_ioctl = video_ioctl2,
1809 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1810 .vidioc_querycap = vidioc_querycap,
1811 .vidioc_dqbuf = vidioc_dqbuf,
1812 .vidioc_qbuf = vidioc_qbuf,
1813 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1814 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1815 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1816 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1817 .vidioc_streamon = vidioc_streamon,
1818 .vidioc_queryctrl = vidioc_queryctrl,
1819 .vidioc_g_ctrl = vidioc_g_ctrl,
1820 .vidioc_s_ctrl = vidioc_s_ctrl,
1821 .vidioc_g_audio = vidioc_g_audio,
1822 .vidioc_s_audio = vidioc_s_audio,
1823 .vidioc_enumaudio = vidioc_enumaudio,
1824 .vidioc_querymenu = vidioc_querymenu,
1825 .vidioc_enum_input = vidioc_enum_input,
1826 .vidioc_g_input = vidioc_g_input,
1827 .vidioc_s_input = vidioc_s_input,
1828 .vidioc_reqbufs = vidioc_reqbufs,
1829 .vidioc_querybuf = vidioc_querybuf,
1830 .vidioc_streamoff = vidioc_streamoff,
1831 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1832 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1833 .vidioc_g_parm = vidioc_g_parm,
1834 .vidioc_s_parm = vidioc_s_parm,
1835 .vidioc_s_std = vidioc_s_std,
1836 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1837 .vidiocgmbuf = vidiocgmbuf,
1841 static struct video_device gspca_template = {
1842 .name = "gspca main driver",
1844 .ioctl_ops = &dev_ioctl_ops,
1845 .release = gspca_release,
1850 * probe and create a new gspca device
1852 * This function must be called by the sub-driver when it is
1853 * called for probing a new device.
1855 int gspca_dev_probe(struct usb_interface *intf,
1856 const struct usb_device_id *id,
1857 const struct sd_desc *sd_desc,
1859 struct module *module)
1861 struct usb_interface_descriptor *interface;
1862 struct gspca_dev *gspca_dev;
1863 struct usb_device *dev = interface_to_usbdev(intf);
1866 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1868 /* we don't handle multi-config cameras */
1869 if (dev->descriptor.bNumConfigurations != 1)
1871 interface = &intf->cur_altsetting->desc;
1872 if (interface->bInterfaceNumber > 0)
1875 /* create the device */
1876 if (dev_size < sizeof *gspca_dev)
1877 dev_size = sizeof *gspca_dev;
1878 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1880 err("couldn't kzalloc gspca struct");
1883 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1884 if (!gspca_dev->usb_buf) {
1885 err("out of memory");
1889 gspca_dev->dev = dev;
1890 gspca_dev->iface = interface->bInterfaceNumber;
1891 gspca_dev->nbalt = intf->num_altsetting;
1892 gspca_dev->sd_desc = sd_desc;
1893 gspca_dev->nbufread = 2;
1894 gspca_dev->empty_packet = -1; /* don't check the empty packets */
1896 /* configure the subdriver and initialize the USB device */
1897 ret = sd_desc->config(gspca_dev, id);
1900 ret = sd_desc->init(gspca_dev);
1903 ret = gspca_set_alt0(gspca_dev);
1906 gspca_set_default_mode(gspca_dev);
1908 mutex_init(&gspca_dev->usb_lock);
1909 mutex_init(&gspca_dev->read_lock);
1910 mutex_init(&gspca_dev->queue_lock);
1911 init_waitqueue_head(&gspca_dev->wq);
1913 /* init video stuff */
1914 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1915 gspca_dev->vdev.parent = &dev->dev;
1916 gspca_dev->module = module;
1917 gspca_dev->present = 1;
1918 ret = video_register_device(&gspca_dev->vdev,
1922 err("video_register_device err %d", ret);
1926 usb_set_intfdata(intf, gspca_dev);
1927 PDEBUG(D_PROBE, "probe ok");
1930 kfree(gspca_dev->usb_buf);
1934 EXPORT_SYMBOL(gspca_dev_probe);
1939 * This function must be called by the sub-driver
1940 * when the device disconnects, after the specific resources are freed.
1942 void gspca_disconnect(struct usb_interface *intf)
1944 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1946 mutex_lock(&gspca_dev->usb_lock);
1947 gspca_dev->present = 0;
1948 mutex_unlock(&gspca_dev->usb_lock);
1950 destroy_urbs(gspca_dev);
1951 gspca_dev->dev = NULL;
1952 usb_set_intfdata(intf, NULL);
1954 /* release the device */
1955 /* (this will call gspca_release() immediatly or on last close) */
1956 video_unregister_device(&gspca_dev->vdev);
1958 PDEBUG(D_PROBE, "disconnect complete");
1960 EXPORT_SYMBOL(gspca_disconnect);
1963 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1965 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1967 if (!gspca_dev->streaming)
1969 gspca_dev->frozen = 1; /* avoid urb error messages */
1970 if (gspca_dev->sd_desc->stopN)
1971 gspca_dev->sd_desc->stopN(gspca_dev);
1972 destroy_urbs(gspca_dev);
1973 gspca_set_alt0(gspca_dev);
1974 if (gspca_dev->sd_desc->stop0)
1975 gspca_dev->sd_desc->stop0(gspca_dev);
1978 EXPORT_SYMBOL(gspca_suspend);
1980 int gspca_resume(struct usb_interface *intf)
1982 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1984 gspca_dev->frozen = 0;
1985 gspca_dev->sd_desc->init(gspca_dev);
1986 if (gspca_dev->streaming)
1987 return gspca_init_transfer(gspca_dev);
1990 EXPORT_SYMBOL(gspca_resume);
1992 /* -- cam driver utility functions -- */
1994 /* auto gain and exposure algorithm based on the knee algorithm described here:
1995 http://ytse.tricolour.net/docs/LowLightOptimization.html
1997 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1999 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2000 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2002 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2003 const struct ctrl *gain_ctrl = NULL;
2004 const struct ctrl *exposure_ctrl = NULL;
2005 const struct ctrl *autogain_ctrl = NULL;
2008 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2009 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2010 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2011 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2012 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2013 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2014 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2016 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2017 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2018 "on cam without (auto)gain/exposure");
2022 if (gain_ctrl->get(gspca_dev, &gain) ||
2023 exposure_ctrl->get(gspca_dev, &exposure) ||
2024 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2028 orig_exposure = exposure;
2030 /* If we are of a multiple of deadzone, do multiple steps to reach the
2031 desired lumination fast (with the risc of a slight overshoot) */
2032 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2034 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2035 avg_lum, desired_avg_lum, steps);
2037 for (i = 0; i < steps; i++) {
2038 if (avg_lum > desired_avg_lum) {
2039 if (gain > gain_knee)
2041 else if (exposure > exposure_knee)
2043 else if (gain > gain_ctrl->qctrl.default_value)
2045 else if (exposure > exposure_ctrl->qctrl.minimum)
2047 else if (gain > gain_ctrl->qctrl.minimum)
2052 if (gain < gain_ctrl->qctrl.default_value)
2054 else if (exposure < exposure_knee)
2056 else if (gain < gain_knee)
2058 else if (exposure < exposure_ctrl->qctrl.maximum)
2060 else if (gain < gain_ctrl->qctrl.maximum)
2067 if (gain != orig_gain) {
2068 gain_ctrl->set(gspca_dev, gain);
2071 if (exposure != orig_exposure) {
2072 exposure_ctrl->set(gspca_dev, exposure);
2078 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2080 /* -- module insert / remove -- */
2081 static int __init gspca_init(void)
2083 info("main v%d.%d.%d registered",
2084 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2085 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2086 DRIVER_VERSION_NUMBER & 0xff);
2089 static void __exit gspca_exit(void)
2091 info("main deregistered");
2094 module_init(gspca_init);
2095 module_exit(gspca_exit);
2098 module_param_named(debug, gspca_debug, int, 0644);
2099 MODULE_PARM_DESC(debug,
2100 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2101 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"