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>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <media/v4l2-ioctl.h>
40 #define DEF_NURBS 2 /* default number of URBs */
42 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 2, 0)
48 static int video_nr = -1;
51 int gspca_debug = D_ERR | D_PROBE;
52 EXPORT_SYMBOL(gspca_debug);
54 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
61 (pixfmt >> 16) & 0xff,
65 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
72 #define PDEBUG_MODE(txt, pixfmt, w, h)
75 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
76 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77 #define GSPCA_MEMORY_READ 7
79 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
84 static void gspca_vm_open(struct vm_area_struct *vma)
86 struct gspca_frame *frame = vma->vm_private_data;
88 frame->vma_use_count++;
89 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92 static void gspca_vm_close(struct vm_area_struct *vma)
94 struct gspca_frame *frame = vma->vm_private_data;
96 if (--frame->vma_use_count <= 0)
97 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100 static struct vm_operations_struct gspca_vm_ops = {
101 .open = gspca_vm_open,
102 .close = gspca_vm_close,
106 * fill a video frame from an URB and resubmit
108 static void fill_frame(struct gspca_dev *gspca_dev,
111 struct gspca_frame *frame;
112 __u8 *data; /* address of data in the iso message */
116 if (urb->status != 0) {
117 if (!gspca_dev->frozen)
118 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
119 return; /* disconnection ? */
121 pkt_scan = gspca_dev->sd_desc->pkt_scan;
122 for (i = 0; i < urb->number_of_packets; i++) {
124 /* check the availability of the frame buffer */
126 j = gspca_dev->fr_queue[j];
127 frame = &gspca_dev->frame[j];
128 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
129 != V4L2_BUF_FLAG_QUEUED) {
130 gspca_dev->last_packet_type = DISCARD_PACKET;
134 /* check the packet status and length */
135 len = urb->iso_frame_desc[i].actual_length;
138 st = urb->iso_frame_desc[i].status;
141 "ISOC data error: [%d] len=%d, status=%d",
143 gspca_dev->last_packet_type = DISCARD_PACKET;
147 /* let the packet be analyzed by the subdriver */
148 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
149 i, urb->iso_frame_desc[i].offset, len);
150 data = (__u8 *) urb->transfer_buffer
151 + urb->iso_frame_desc[i].offset;
152 pkt_scan(gspca_dev, frame, data, len);
155 /* resubmit the URB */
157 st = usb_submit_urb(urb, GFP_ATOMIC);
159 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
163 * ISOC message interrupt from the USB device
165 * Analyse each packet and call the subdriver for copy to the frame buffer.
167 static void isoc_irq(struct urb *urb
170 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
172 PDEBUG(D_PACK, "isoc irq");
173 if (!gspca_dev->streaming)
175 fill_frame(gspca_dev, urb);
179 * add data to the current frame
181 * This function is called by the subdrivers at interrupt level.
183 * To build a frame, these ones must add
185 * - 0 or many INTER_PACKETs
187 * DISCARD_PACKET invalidates the whole frame.
188 * On LAST_PACKET, a new frame is returned.
190 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
192 struct gspca_frame *frame,
198 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
200 /* when start of a new frame, if the current frame buffer
201 * is not queued, discard the whole frame */
202 if (packet_type == FIRST_PACKET) {
203 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
204 != V4L2_BUF_FLAG_QUEUED) {
205 gspca_dev->last_packet_type = DISCARD_PACKET;
208 frame->data_end = frame->data;
209 jiffies_to_timeval(get_jiffies_64(),
210 &frame->v4l2_buf.timestamp);
211 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
212 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
213 if (packet_type == LAST_PACKET)
214 gspca_dev->last_packet_type = packet_type;
218 /* append the packet to the frame buffer */
220 if (frame->data_end - frame->data + len
221 > frame->v4l2_buf.length) {
222 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
223 frame->data_end - frame->data + len,
224 frame->v4l2_buf.length);
225 packet_type = DISCARD_PACKET;
227 memcpy(frame->data_end, data, len);
228 frame->data_end += len;
231 gspca_dev->last_packet_type = packet_type;
233 /* if last packet, wake the application and advance in the queue */
234 if (packet_type == LAST_PACKET) {
235 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
236 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
237 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
238 atomic_inc(&gspca_dev->nevent);
239 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
240 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
242 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
243 frame->v4l2_buf.bytesused,
247 j = gspca_dev->fr_queue[i];
248 frame = &gspca_dev->frame[j];
252 EXPORT_SYMBOL(gspca_frame_add);
254 static int gspca_is_compressed(__u32 format)
257 case V4L2_PIX_FMT_MJPEG:
258 case V4L2_PIX_FMT_JPEG:
259 case V4L2_PIX_FMT_SPCA561:
260 case V4L2_PIX_FMT_PAC207:
266 static void *rvmalloc(unsigned long size)
271 /* size = PAGE_ALIGN(size); (already done) */
272 mem = vmalloc_32(size);
274 adr = (unsigned long) mem;
275 while ((long) size > 0) {
276 SetPageReserved(vmalloc_to_page((void *) adr));
284 static void rvfree(void *mem, long size)
288 adr = (unsigned long) mem;
290 ClearPageReserved(vmalloc_to_page((void *) adr));
297 static int frame_alloc(struct gspca_dev *gspca_dev,
300 struct gspca_frame *frame;
304 i = gspca_dev->curr_mode;
305 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
306 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
307 frsz = PAGE_ALIGN(frsz);
308 gspca_dev->frsz = frsz;
309 if (count > GSPCA_MAX_FRAMES)
310 count = GSPCA_MAX_FRAMES;
311 gspca_dev->frbuf = rvmalloc(frsz * count);
312 if (!gspca_dev->frbuf) {
313 err("frame alloc failed");
316 gspca_dev->nframes = count;
317 for (i = 0; i < count; i++) {
318 frame = &gspca_dev->frame[i];
319 frame->v4l2_buf.index = i;
320 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
321 frame->v4l2_buf.flags = 0;
322 frame->v4l2_buf.field = V4L2_FIELD_NONE;
323 frame->v4l2_buf.length = frsz;
324 frame->v4l2_buf.memory = gspca_dev->memory;
325 frame->v4l2_buf.sequence = 0;
326 frame->data = frame->data_end =
327 gspca_dev->frbuf + i * frsz;
328 frame->v4l2_buf.m.offset = i * frsz;
330 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
331 gspca_dev->last_packet_type = DISCARD_PACKET;
332 gspca_dev->sequence = 0;
333 atomic_set(&gspca_dev->nevent, 0);
337 static void frame_free(struct gspca_dev *gspca_dev)
341 PDEBUG(D_STREAM, "frame free");
342 if (gspca_dev->frbuf != NULL) {
343 rvfree(gspca_dev->frbuf,
344 gspca_dev->nframes * gspca_dev->frsz);
345 gspca_dev->frbuf = NULL;
346 for (i = 0; i < gspca_dev->nframes; i++)
347 gspca_dev->frame[i].data = NULL;
349 gspca_dev->nframes = 0;
352 static void destroy_urbs(struct gspca_dev *gspca_dev)
357 PDEBUG(D_STREAM, "kill transfer");
358 for (i = 0; i < MAX_NURBS; ++i) {
359 urb = gspca_dev->urb[i];
363 gspca_dev->urb[i] = NULL;
365 if (urb->transfer_buffer != NULL)
366 usb_buffer_free(gspca_dev->dev,
367 urb->transfer_buffer_length,
368 urb->transfer_buffer,
375 * search an input isochronous endpoint in an alternate setting
377 static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
380 struct usb_host_endpoint *ep;
383 epaddr |= USB_DIR_IN;
384 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
385 ep = &alt->endpoint[i];
386 if (ep->desc.bEndpointAddress == epaddr) {
387 attr = ep->desc.bmAttributes
388 & USB_ENDPOINT_XFERTYPE_MASK;
389 if (attr == USB_ENDPOINT_XFER_ISOC)
398 * search an input isochronous endpoint
400 * The endpoint is defined by the subdriver.
401 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
402 * This routine may be called many times when the bandwidth is too small
403 * (the bandwidth is checked on urb submit).
405 static struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
407 struct usb_interface *intf;
408 struct usb_host_endpoint *ep;
411 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
413 i = gspca_dev->alt; /* previous alt setting */
414 while (--i > 0) { /* alt 0 is unusable */
415 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
420 err("no ISOC endpoint found");
423 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
424 i, ep->desc.bEndpointAddress);
425 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
427 err("set interface err %d", ret);
430 gspca_dev->alt = i; /* memorize the current alt setting */
435 * create the isochronous URBs
437 static int create_urbs(struct gspca_dev *gspca_dev,
438 struct usb_host_endpoint *ep)
441 int n, nurbs, i, psize, npkt, bsize;
443 /* calculate the packet size and the number of packets */
444 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
446 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
447 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
448 npkt = ISO_MAX_SIZE / psize;
449 if (npkt > ISO_MAX_PKT)
451 bsize = psize * npkt;
453 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
455 gspca_dev->nurbs = nurbs;
456 for (n = 0; n < nurbs; n++) {
457 urb = usb_alloc_urb(npkt, GFP_KERNEL);
459 err("usb_alloc_urb failed");
462 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
467 if (urb->transfer_buffer == NULL) {
469 destroy_urbs(gspca_dev);
470 err("usb_buffer_urb failed");
473 gspca_dev->urb[n] = urb;
474 urb->dev = gspca_dev->dev;
475 urb->context = gspca_dev;
476 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
477 ep->desc.bEndpointAddress);
478 urb->transfer_flags = URB_ISO_ASAP
479 | URB_NO_TRANSFER_DMA_MAP;
480 urb->interval = ep->desc.bInterval;
481 urb->complete = isoc_irq;
482 urb->number_of_packets = npkt;
483 urb->transfer_buffer_length = bsize;
484 for (i = 0; i < npkt; i++) {
485 urb->iso_frame_desc[i].length = psize;
486 urb->iso_frame_desc[i].offset = psize * i;
493 * start the USB transfer
495 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
497 struct usb_host_endpoint *ep;
500 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
503 /* set the higher alternate setting and
504 * loop until urb submit succeeds */
505 gspca_dev->alt = gspca_dev->nbalt;
507 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
508 ep = get_isoc_ep(gspca_dev);
513 ret = create_urbs(gspca_dev, ep);
518 gspca_dev->sd_desc->start(gspca_dev);
519 gspca_dev->streaming = 1;
520 atomic_set(&gspca_dev->nevent, 0);
522 /* submit the URBs */
523 for (n = 0; n < gspca_dev->nurbs; n++) {
524 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
526 PDEBUG(D_ERR|D_STREAM,
527 "usb_submit_urb [%d] err %d", n, ret);
528 gspca_dev->streaming = 0;
529 destroy_urbs(gspca_dev);
531 break; /* try the previous alt */
539 mutex_unlock(&gspca_dev->usb_lock);
543 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
547 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
549 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
553 /* Note both the queue and the usb lock should be hold when calling this */
554 static void gspca_stream_off(struct gspca_dev *gspca_dev)
556 gspca_dev->streaming = 0;
557 atomic_set(&gspca_dev->nevent, 0);
558 if (gspca_dev->present) {
559 gspca_dev->sd_desc->stopN(gspca_dev);
560 destroy_urbs(gspca_dev);
561 gspca_set_alt0(gspca_dev);
562 gspca_dev->sd_desc->stop0(gspca_dev);
563 PDEBUG(D_STREAM, "stream off OK");
567 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
571 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
572 gspca_dev->curr_mode = i;
573 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
574 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
575 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
578 static int wxh_to_mode(struct gspca_dev *gspca_dev,
579 int width, int height)
583 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
584 if (width >= gspca_dev->cam.cam_mode[i].width
585 && height >= gspca_dev->cam.cam_mode[i].height)
592 * search a mode with the right pixel format
594 static int gspca_get_mode(struct gspca_dev *gspca_dev,
600 modeU = modeD = mode;
601 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
603 if (gspca_dev->cam.cam_mode[modeD].pixelformat
607 if (++modeU < gspca_dev->cam.nmodes) {
608 if (gspca_dev->cam.cam_mode[modeU].pixelformat
616 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
617 struct v4l2_fmtdesc *fmtdesc)
619 struct gspca_dev *gspca_dev = priv;
623 /* give an index to each format */
626 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
627 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
630 if (fmt_tb[j] == fmt_tb[index])
635 if (fmtdesc->index == index)
636 break; /* new format */
638 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
643 return -EINVAL; /* no more format */
645 fmtdesc->pixelformat = fmt_tb[index];
646 if (gspca_is_compressed(fmt_tb[index]))
647 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
648 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
649 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
650 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
651 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
652 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
653 fmtdesc->description[4] = '\0';
657 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
658 struct v4l2_format *fmt)
660 struct gspca_dev *gspca_dev = priv;
663 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
665 mode = gspca_dev->curr_mode;
666 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
667 sizeof fmt->fmt.pix);
671 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
672 struct v4l2_format *fmt)
674 int w, h, mode, mode2;
676 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
678 w = fmt->fmt.pix.width;
679 h = fmt->fmt.pix.height;
682 if (gspca_debug & D_CONF)
683 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
685 /* search the closest mode for width and height */
686 mode = wxh_to_mode(gspca_dev, w, h);
688 /* OK if right palette */
689 if (gspca_dev->cam.cam_mode[mode].pixelformat
690 != fmt->fmt.pix.pixelformat) {
692 /* else, search the closest mode with the same pixel format */
693 mode2 = gspca_get_mode(gspca_dev, mode,
694 fmt->fmt.pix.pixelformat);
698 ; * no chance, return this mode */
700 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
701 sizeof fmt->fmt.pix);
702 return mode; /* used when s_fmt */
705 static int vidioc_try_fmt_vid_cap(struct file *file,
707 struct v4l2_format *fmt)
709 struct gspca_dev *gspca_dev = priv;
712 ret = try_fmt_vid_cap(gspca_dev, fmt);
718 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
719 struct v4l2_format *fmt)
721 struct gspca_dev *gspca_dev = priv;
724 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
727 ret = try_fmt_vid_cap(gspca_dev, fmt);
731 if (gspca_dev->nframes != 0
732 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
737 if (ret == gspca_dev->curr_mode) {
739 goto out; /* same mode */
742 if (gspca_dev->streaming) {
746 gspca_dev->width = fmt->fmt.pix.width;
747 gspca_dev->height = fmt->fmt.pix.height;
748 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
749 gspca_dev->curr_mode = ret;
753 mutex_unlock(&gspca_dev->queue_lock);
757 static int dev_open(struct inode *inode, struct file *file)
759 struct gspca_dev *gspca_dev;
762 PDEBUG(D_STREAM, "%s open", current->comm);
763 gspca_dev = (struct gspca_dev *) video_devdata(file);
764 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
766 if (!gspca_dev->present) {
771 /* if not done yet, initialize the sensor */
772 if (gspca_dev->users == 0) {
773 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
777 ret = gspca_dev->sd_desc->open(gspca_dev);
778 mutex_unlock(&gspca_dev->usb_lock);
780 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
783 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
788 file->private_data = gspca_dev;
790 /* activate the v4l2 debug */
791 if (gspca_debug & D_V4L2)
792 gspca_dev->vdev.debug |= 3;
794 gspca_dev->vdev.debug &= ~3;
797 mutex_unlock(&gspca_dev->queue_lock);
799 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
801 PDEBUG(D_STREAM, "open done");
805 static int dev_close(struct inode *inode, struct file *file)
807 struct gspca_dev *gspca_dev = file->private_data;
809 PDEBUG(D_STREAM, "%s close", current->comm);
810 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
814 /* if the file did the capture, free the streaming resources */
815 if (gspca_dev->capt_file == file) {
816 mutex_lock(&gspca_dev->usb_lock);
817 if (gspca_dev->streaming)
818 gspca_stream_off(gspca_dev);
819 gspca_dev->sd_desc->close(gspca_dev);
820 mutex_unlock(&gspca_dev->usb_lock);
821 frame_free(gspca_dev);
822 gspca_dev->capt_file = NULL;
823 gspca_dev->memory = GSPCA_MEMORY_NO;
825 file->private_data = NULL;
826 mutex_unlock(&gspca_dev->queue_lock);
827 PDEBUG(D_STREAM, "close done");
831 static int vidioc_querycap(struct file *file, void *priv,
832 struct v4l2_capability *cap)
834 struct gspca_dev *gspca_dev = priv;
836 memset(cap, 0, sizeof *cap);
837 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
838 /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
839 if (gspca_dev->dev->product != NULL) {
840 strncpy(cap->card, gspca_dev->dev->product,
843 snprintf(cap->card, sizeof cap->card,
844 "USB Camera (%04x:%04x)",
845 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
846 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
848 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
849 sizeof cap->bus_info);
850 cap->version = DRIVER_VERSION_NUMBER;
851 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
853 | V4L2_CAP_READWRITE;
857 /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
858 static int vidioc_queryctrl(struct file *file, void *priv,
859 struct v4l2_queryctrl *q_ctrl)
861 struct gspca_dev *gspca_dev = priv;
866 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
867 id &= V4L2_CTRL_ID_MASK;
869 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
870 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
872 &gspca_dev->sd_desc->ctrls[i].qctrl,
879 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
880 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
882 &gspca_dev->sd_desc->ctrls[i].qctrl,
890 static int vidioc_s_ctrl(struct file *file, void *priv,
891 struct v4l2_control *ctrl)
893 struct gspca_dev *gspca_dev = priv;
894 const struct ctrl *ctrls;
897 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
898 i < gspca_dev->sd_desc->nctrls;
900 if (ctrl->id != ctrls->qctrl.id)
902 if (ctrl->value < ctrls->qctrl.minimum
903 || ctrl->value > ctrls->qctrl.maximum)
905 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
906 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
908 ret = ctrls->set(gspca_dev, ctrl->value);
909 mutex_unlock(&gspca_dev->usb_lock);
915 static int vidioc_g_ctrl(struct file *file, void *priv,
916 struct v4l2_control *ctrl)
918 struct gspca_dev *gspca_dev = priv;
920 const struct ctrl *ctrls;
923 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
924 i < gspca_dev->sd_desc->nctrls;
926 if (ctrl->id != ctrls->qctrl.id)
928 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
930 ret = ctrls->get(gspca_dev, &ctrl->value);
931 mutex_unlock(&gspca_dev->usb_lock);
937 static int vidioc_querymenu(struct file *file, void *priv,
938 struct v4l2_querymenu *qmenu)
940 struct gspca_dev *gspca_dev = priv;
942 if (!gspca_dev->sd_desc->querymenu)
944 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
947 static int vidioc_enum_input(struct file *file, void *priv,
948 struct v4l2_input *input)
950 struct gspca_dev *gspca_dev = priv;
952 if (input->index != 0)
954 memset(input, 0, sizeof *input);
955 input->type = V4L2_INPUT_TYPE_CAMERA;
956 strncpy(input->name, gspca_dev->sd_desc->name,
961 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
967 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
974 static int vidioc_reqbufs(struct file *file, void *priv,
975 struct v4l2_requestbuffers *rb)
977 struct gspca_dev *gspca_dev = priv;
980 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
982 switch (rb->memory) {
983 case GSPCA_MEMORY_READ: /* (internal call) */
984 case V4L2_MEMORY_MMAP:
985 case V4L2_MEMORY_USERPTR:
990 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
993 if (gspca_dev->memory != GSPCA_MEMORY_NO
994 && gspca_dev->memory != rb->memory) {
999 /* only one file may do the capture */
1000 if (gspca_dev->capt_file != NULL
1001 && gspca_dev->capt_file != file) {
1006 /* if allocated, the buffers must not be mapped */
1007 for (i = 0; i < gspca_dev->nframes; i++) {
1008 if (gspca_dev->frame[i].vma_use_count) {
1014 /* stop streaming */
1015 if (gspca_dev->streaming) {
1016 mutex_lock(&gspca_dev->usb_lock);
1017 gspca_stream_off(gspca_dev);
1018 mutex_unlock(&gspca_dev->usb_lock);
1021 /* free the previous allocated buffers, if any */
1022 if (gspca_dev->nframes != 0) {
1023 frame_free(gspca_dev);
1024 gspca_dev->capt_file = NULL;
1026 if (rb->count == 0) /* unrequest */
1028 gspca_dev->memory = rb->memory;
1029 ret = frame_alloc(gspca_dev, rb->count);
1031 rb->count = gspca_dev->nframes;
1032 gspca_dev->capt_file = file;
1035 mutex_unlock(&gspca_dev->queue_lock);
1036 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1040 static int vidioc_querybuf(struct file *file, void *priv,
1041 struct v4l2_buffer *v4l2_buf)
1043 struct gspca_dev *gspca_dev = priv;
1044 struct gspca_frame *frame;
1046 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1047 || v4l2_buf->index < 0
1048 || v4l2_buf->index >= gspca_dev->nframes)
1051 frame = &gspca_dev->frame[v4l2_buf->index];
1052 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1056 static int vidioc_streamon(struct file *file, void *priv,
1057 enum v4l2_buf_type buf_type)
1059 struct gspca_dev *gspca_dev = priv;
1062 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1064 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1065 return -ERESTARTSYS;
1066 if (!gspca_dev->present) {
1070 if (gspca_dev->nframes == 0) {
1074 if (!gspca_dev->streaming) {
1075 ret = gspca_init_transfer(gspca_dev);
1080 if (gspca_debug & D_STREAM) {
1081 PDEBUG_MODE("stream on OK",
1089 mutex_unlock(&gspca_dev->queue_lock);
1093 static int vidioc_streamoff(struct file *file, void *priv,
1094 enum v4l2_buf_type buf_type)
1096 struct gspca_dev *gspca_dev = priv;
1099 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1101 if (!gspca_dev->streaming)
1103 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1104 return -ERESTARTSYS;
1106 /* stop streaming */
1107 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1111 gspca_stream_off(gspca_dev);
1112 mutex_unlock(&gspca_dev->usb_lock);
1114 /* empty the application queues */
1115 for (i = 0; i < gspca_dev->nframes; i++)
1116 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1117 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1118 gspca_dev->last_packet_type = DISCARD_PACKET;
1119 gspca_dev->sequence = 0;
1120 atomic_set(&gspca_dev->nevent, 0);
1123 mutex_unlock(&gspca_dev->queue_lock);
1127 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1128 struct v4l2_jpegcompression *jpegcomp)
1130 struct gspca_dev *gspca_dev = priv;
1133 if (!gspca_dev->sd_desc->get_jcomp)
1135 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1136 return -ERESTARTSYS;
1137 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1138 mutex_unlock(&gspca_dev->usb_lock);
1142 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1143 struct v4l2_jpegcompression *jpegcomp)
1145 struct gspca_dev *gspca_dev = priv;
1148 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1149 return -ERESTARTSYS;
1150 if (!gspca_dev->sd_desc->set_jcomp)
1152 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1153 mutex_unlock(&gspca_dev->usb_lock);
1157 static int vidioc_g_parm(struct file *filp, void *priv,
1158 struct v4l2_streamparm *parm)
1160 struct gspca_dev *gspca_dev = priv;
1162 memset(parm, 0, sizeof *parm);
1163 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1164 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1168 static int vidioc_s_parm(struct file *filp, void *priv,
1169 struct v4l2_streamparm *parm)
1171 struct gspca_dev *gspca_dev = priv;
1174 n = parm->parm.capture.readbuffers;
1175 if (n == 0 || n > GSPCA_MAX_FRAMES)
1176 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1178 gspca_dev->nbufread = n;
1182 static int vidioc_s_std(struct file *filp, void *priv,
1188 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1189 static int vidiocgmbuf(struct file *file, void *priv,
1190 struct video_mbuf *mbuf)
1192 struct gspca_dev *gspca_dev = file->private_data;
1195 PDEBUG(D_STREAM, "cgmbuf");
1196 if (gspca_dev->nframes == 0) {
1200 struct v4l2_format fmt;
1202 memset(&fmt, 0, sizeof fmt);
1203 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1204 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1205 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1206 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1207 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1208 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1213 struct v4l2_requestbuffers rb;
1215 memset(&rb, 0, sizeof rb);
1217 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1218 rb.memory = V4L2_MEMORY_MMAP;
1219 ret = vidioc_reqbufs(file, priv, &rb);
1224 mbuf->frames = gspca_dev->nframes;
1225 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1226 for (i = 0; i < mbuf->frames; i++)
1227 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1232 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1234 struct gspca_dev *gspca_dev = file->private_data;
1235 struct gspca_frame *frame;
1237 unsigned long addr, start, size;
1240 start = vma->vm_start;
1241 size = vma->vm_end - vma->vm_start;
1242 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1244 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1245 return -ERESTARTSYS;
1246 if (!gspca_dev->present) {
1250 if (gspca_dev->capt_file != file) {
1256 for (i = 0; i < gspca_dev->nframes; ++i) {
1257 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1258 PDEBUG(D_STREAM, "mmap bad memory type");
1261 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1263 frame = &gspca_dev->frame[i];
1267 if (frame == NULL) {
1268 PDEBUG(D_STREAM, "mmap no frame buffer found");
1272 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1273 /* v4l1 maps all the buffers */
1275 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1277 if (size != frame->v4l2_buf.length) {
1278 PDEBUG(D_STREAM, "mmap bad size");
1284 * - VM_IO marks the area as being a mmaped region for I/O to a
1285 * device. It also prevents the region from being core dumped.
1287 vma->vm_flags |= VM_IO;
1289 addr = (unsigned long) frame->data;
1291 page = vmalloc_to_page((void *) addr);
1292 ret = vm_insert_page(vma, start, page);
1300 vma->vm_ops = &gspca_vm_ops;
1301 vma->vm_private_data = frame;
1305 mutex_unlock(&gspca_dev->queue_lock);
1310 * wait for a video frame
1312 * If a frame is ready, its index is returned.
1314 static int frame_wait(struct gspca_dev *gspca_dev,
1317 struct gspca_frame *frame;
1320 /* check if a frame is ready */
1321 i = gspca_dev->fr_o;
1322 j = gspca_dev->fr_queue[i];
1323 frame = &gspca_dev->frame[j];
1324 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1325 atomic_dec(&gspca_dev->nevent);
1328 if (nonblock_ing) /* no frame yet */
1331 /* wait till a frame is ready */
1333 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1334 atomic_read(&gspca_dev->nevent) > 0,
1335 msecs_to_jiffies(3000));
1338 return ret; /* interrupt */
1339 return -EIO; /* timeout */
1341 atomic_dec(&gspca_dev->nevent);
1342 if (!gspca_dev->streaming || !gspca_dev->present)
1344 i = gspca_dev->fr_o;
1345 j = gspca_dev->fr_queue[i];
1346 frame = &gspca_dev->frame[j];
1347 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1351 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1352 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1357 if (gspca_dev->sd_desc->dq_callback) {
1358 mutex_lock(&gspca_dev->usb_lock);
1359 gspca_dev->sd_desc->dq_callback(gspca_dev);
1360 mutex_unlock(&gspca_dev->usb_lock);
1366 * dequeue a video buffer
1368 * If nonblock_ing is false, block until a buffer is available.
1370 static int vidioc_dqbuf(struct file *file, void *priv,
1371 struct v4l2_buffer *v4l2_buf)
1373 struct gspca_dev *gspca_dev = priv;
1374 struct gspca_frame *frame;
1377 PDEBUG(D_FRAM, "dqbuf");
1378 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1380 if (v4l2_buf->memory != gspca_dev->memory)
1383 /* if not streaming, be sure the application will not loop forever */
1384 if (!(file->f_flags & O_NONBLOCK)
1385 && !gspca_dev->streaming && gspca_dev->users == 1)
1388 /* only the capturing file may dequeue */
1389 if (gspca_dev->capt_file != file)
1392 /* only one dequeue / read at a time */
1393 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1394 return -ERESTARTSYS;
1396 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1399 i = ret; /* frame index */
1400 frame = &gspca_dev->frame[i];
1401 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1402 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1404 frame->v4l2_buf.bytesused)) {
1405 PDEBUG(D_ERR|D_STREAM,
1406 "dqbuf cp to user failed");
1411 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1412 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1413 PDEBUG(D_FRAM, "dqbuf %d", i);
1416 mutex_unlock(&gspca_dev->read_lock);
1421 * queue a video buffer
1423 * Attempting to queue a buffer that has already been
1424 * queued will return -EINVAL.
1426 static int vidioc_qbuf(struct file *file, void *priv,
1427 struct v4l2_buffer *v4l2_buf)
1429 struct gspca_dev *gspca_dev = priv;
1430 struct gspca_frame *frame;
1433 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1434 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1437 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1438 return -ERESTARTSYS;
1440 index = v4l2_buf->index;
1441 if ((unsigned) index >= gspca_dev->nframes) {
1443 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1447 if (v4l2_buf->memory != gspca_dev->memory) {
1448 PDEBUG(D_FRAM, "qbuf bad memory type");
1453 frame = &gspca_dev->frame[index];
1454 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1455 PDEBUG(D_FRAM, "qbuf bad state");
1460 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1461 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1463 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1464 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1465 frame->v4l2_buf.length = v4l2_buf->length;
1468 /* put the buffer in the 'queued' queue */
1469 i = gspca_dev->fr_q;
1470 gspca_dev->fr_queue[i] = index;
1471 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1472 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1477 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1478 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1481 mutex_unlock(&gspca_dev->queue_lock);
1486 * allocate the resources for read()
1488 static int read_alloc(struct gspca_dev *gspca_dev,
1491 struct v4l2_buffer v4l2_buf;
1494 PDEBUG(D_STREAM, "read alloc");
1495 if (gspca_dev->nframes == 0) {
1496 struct v4l2_requestbuffers rb;
1498 memset(&rb, 0, sizeof rb);
1499 rb.count = gspca_dev->nbufread;
1500 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1501 rb.memory = GSPCA_MEMORY_READ;
1502 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1504 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1507 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1508 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1509 v4l2_buf.memory = GSPCA_MEMORY_READ;
1510 for (i = 0; i < gspca_dev->nbufread; i++) {
1512 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1514 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1518 gspca_dev->memory = GSPCA_MEMORY_READ;
1521 /* start streaming */
1522 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1524 PDEBUG(D_STREAM, "read streamon err %d", ret);
1528 static unsigned int dev_poll(struct file *file, poll_table *wait)
1530 struct gspca_dev *gspca_dev = file->private_data;
1533 PDEBUG(D_FRAM, "poll");
1535 poll_wait(file, &gspca_dev->wq, wait);
1536 if (!gspca_dev->present)
1539 /* if reqbufs is not done, the user would use read() */
1540 if (gspca_dev->nframes == 0) {
1541 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1542 return POLLERR; /* not the 1st time */
1543 ret = read_alloc(gspca_dev, file);
1548 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1550 if (!gspca_dev->present) {
1555 /* check the next incoming buffer */
1556 i = gspca_dev->fr_o;
1557 i = gspca_dev->fr_queue[i];
1558 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1559 ret = POLLIN | POLLRDNORM; /* something to read */
1563 mutex_unlock(&gspca_dev->queue_lock);
1567 static ssize_t dev_read(struct file *file, char __user *data,
1568 size_t count, loff_t *ppos)
1570 struct gspca_dev *gspca_dev = file->private_data;
1571 struct gspca_frame *frame;
1572 struct v4l2_buffer v4l2_buf;
1573 struct timeval timestamp;
1576 PDEBUG(D_FRAM, "read (%zd)", count);
1577 if (!gspca_dev->present)
1579 switch (gspca_dev->memory) {
1580 case GSPCA_MEMORY_NO: /* first time */
1581 ret = read_alloc(gspca_dev, file);
1585 case GSPCA_MEMORY_READ:
1586 if (gspca_dev->capt_file == file)
1594 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1598 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1599 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1600 v4l2_buf.memory = GSPCA_MEMORY_READ;
1601 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1603 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1607 /* if the process slept for more than 1 second,
1608 * get anewer frame */
1609 frame = &gspca_dev->frame[v4l2_buf.index];
1611 break; /* avoid infinite loop */
1612 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1614 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1616 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1621 /* copy the frame */
1622 if (count > frame->v4l2_buf.bytesused)
1623 count = frame->v4l2_buf.bytesused;
1624 ret = copy_to_user(data, frame->data, count);
1626 PDEBUG(D_ERR|D_STREAM,
1627 "read cp to user lack %d / %zd", ret, count);
1633 /* in each case, requeue the buffer */
1634 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1640 static void dev_release(struct video_device *vfd)
1645 static struct file_operations dev_fops = {
1646 .owner = THIS_MODULE,
1648 .release = dev_close,
1651 .ioctl = video_ioctl2,
1652 #ifdef CONFIG_COMPAT
1653 .compat_ioctl = v4l_compat_ioctl32,
1655 .llseek = no_llseek,
1659 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1660 .vidioc_querycap = vidioc_querycap,
1661 .vidioc_dqbuf = vidioc_dqbuf,
1662 .vidioc_qbuf = vidioc_qbuf,
1663 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1664 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1665 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1666 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1667 .vidioc_streamon = vidioc_streamon,
1668 .vidioc_queryctrl = vidioc_queryctrl,
1669 .vidioc_g_ctrl = vidioc_g_ctrl,
1670 .vidioc_s_ctrl = vidioc_s_ctrl,
1671 .vidioc_querymenu = vidioc_querymenu,
1672 .vidioc_enum_input = vidioc_enum_input,
1673 .vidioc_g_input = vidioc_g_input,
1674 .vidioc_s_input = vidioc_s_input,
1675 .vidioc_reqbufs = vidioc_reqbufs,
1676 .vidioc_querybuf = vidioc_querybuf,
1677 .vidioc_streamoff = vidioc_streamoff,
1678 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1679 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1680 .vidioc_g_parm = vidioc_g_parm,
1681 .vidioc_s_parm = vidioc_s_parm,
1682 .vidioc_s_std = vidioc_s_std,
1683 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1684 .vidiocgmbuf = vidiocgmbuf,
1688 static struct video_device gspca_template = {
1689 .name = "gspca main driver",
1691 .ioctl_ops = &dev_ioctl_ops,
1692 .release = dev_release, /* mandatory */
1697 * probe and create a new gspca device
1699 * This function must be called by the sub-driver when it is
1700 * called for probing a new device.
1702 int gspca_dev_probe(struct usb_interface *intf,
1703 const struct usb_device_id *id,
1704 const struct sd_desc *sd_desc,
1706 struct module *module)
1708 struct usb_interface_descriptor *interface;
1709 struct gspca_dev *gspca_dev;
1710 struct usb_device *dev = interface_to_usbdev(intf);
1713 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1715 /* we don't handle multi-config cameras */
1716 if (dev->descriptor.bNumConfigurations != 1)
1718 interface = &intf->cur_altsetting->desc;
1719 if (interface->bInterfaceNumber > 0)
1722 /* create the device */
1723 if (dev_size < sizeof *gspca_dev)
1724 dev_size = sizeof *gspca_dev;
1725 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1726 if (gspca_dev == NULL) {
1727 err("couldn't kzalloc gspca struct");
1730 gspca_dev->dev = dev;
1731 gspca_dev->iface = interface->bInterfaceNumber;
1732 gspca_dev->nbalt = intf->num_altsetting;
1733 gspca_dev->sd_desc = sd_desc;
1734 /* gspca_dev->users = 0; (done by kzalloc) */
1735 gspca_dev->nbufread = 2;
1737 /* configure the subdriver */
1738 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1741 ret = gspca_set_alt0(gspca_dev);
1744 gspca_set_default_mode(gspca_dev);
1746 mutex_init(&gspca_dev->usb_lock);
1747 mutex_init(&gspca_dev->read_lock);
1748 mutex_init(&gspca_dev->queue_lock);
1749 init_waitqueue_head(&gspca_dev->wq);
1751 /* init video stuff */
1752 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1753 gspca_dev->vdev.parent = &dev->dev;
1754 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1755 gspca_dev->vdev.fops = &gspca_dev->fops;
1756 gspca_dev->fops.owner = module; /* module protection */
1757 gspca_dev->present = 1;
1758 ret = video_register_device(&gspca_dev->vdev,
1762 err("video_register_device err %d", ret);
1766 usb_set_intfdata(intf, gspca_dev);
1767 PDEBUG(D_PROBE, "probe ok");
1773 EXPORT_SYMBOL(gspca_dev_probe);
1778 * This function must be called by the sub-driver
1779 * when the device disconnects, after the specific resources are freed.
1781 void gspca_disconnect(struct usb_interface *intf)
1783 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1787 gspca_dev->present = 0;
1788 mutex_lock(&gspca_dev->queue_lock);
1789 mutex_lock(&gspca_dev->usb_lock);
1790 gspca_dev->streaming = 0;
1791 destroy_urbs(gspca_dev);
1792 mutex_unlock(&gspca_dev->usb_lock);
1793 mutex_unlock(&gspca_dev->queue_lock);
1794 while (gspca_dev->users != 0) { /* wait until fully closed */
1795 atomic_inc(&gspca_dev->nevent);
1796 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1799 /* We don't want people trying to open up the device */
1800 video_unregister_device(&gspca_dev->vdev);
1801 /* Free the memory */
1803 PDEBUG(D_PROBE, "disconnect complete");
1805 EXPORT_SYMBOL(gspca_disconnect);
1808 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1810 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1812 if (!gspca_dev->streaming)
1814 gspca_dev->frozen = 1; /* avoid urb error messages */
1815 gspca_dev->sd_desc->stopN(gspca_dev);
1816 destroy_urbs(gspca_dev);
1817 gspca_set_alt0(gspca_dev);
1818 gspca_dev->sd_desc->stop0(gspca_dev);
1821 EXPORT_SYMBOL(gspca_suspend);
1823 int gspca_resume(struct usb_interface *intf)
1825 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1827 gspca_dev->frozen = 0;
1828 if (!gspca_dev->streaming)
1830 return gspca_init_transfer(gspca_dev);
1832 EXPORT_SYMBOL(gspca_resume);
1834 /* -- cam driver utility functions -- */
1836 /* auto gain and exposure algorithm based on the knee algorithm described here:
1837 http://ytse.tricolour.net/docs/LowLightOptimization.html
1839 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1841 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1842 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1844 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1845 const struct ctrl *gain_ctrl = NULL;
1846 const struct ctrl *exposure_ctrl = NULL;
1847 const struct ctrl *autogain_ctrl = NULL;
1850 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1851 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1852 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1853 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1854 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1855 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1856 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1858 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1859 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1860 "on cam without (auto)gain/exposure");
1864 if (gain_ctrl->get(gspca_dev, &gain) ||
1865 exposure_ctrl->get(gspca_dev, &exposure) ||
1866 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1870 orig_exposure = exposure;
1872 /* If we are of a multiple of deadzone, do multiple steps to reach the
1873 desired lumination fast (with the risc of a slight overshoot) */
1874 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1876 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1877 avg_lum, desired_avg_lum, steps);
1879 for (i = 0; i < steps; i++) {
1880 if (avg_lum > desired_avg_lum) {
1881 if (gain > gain_knee)
1883 else if (exposure > exposure_knee)
1885 else if (gain > gain_ctrl->qctrl.default_value)
1887 else if (exposure > exposure_ctrl->qctrl.minimum)
1889 else if (gain > gain_ctrl->qctrl.minimum)
1894 if (gain < gain_ctrl->qctrl.default_value)
1896 else if (exposure < exposure_knee)
1898 else if (gain < gain_knee)
1900 else if (exposure < exposure_ctrl->qctrl.maximum)
1902 else if (gain < gain_ctrl->qctrl.maximum)
1909 if (gain != orig_gain) {
1910 gain_ctrl->set(gspca_dev, gain);
1913 if (exposure != orig_exposure) {
1914 exposure_ctrl->set(gspca_dev, exposure);
1920 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1922 /* -- module insert / remove -- */
1923 static int __init gspca_init(void)
1925 info("main v%d.%d.%d registered",
1926 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1927 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1928 DRIVER_VERSION_NUMBER & 0xff);
1931 static void __exit gspca_exit(void)
1933 info("main deregistered");
1936 module_init(gspca_init);
1937 module_exit(gspca_exit);
1940 module_param_named(debug, gspca_debug, int, 0644);
1941 MODULE_PARM_DESC(debug,
1942 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
1943 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"