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) {
118 if (!gspca_dev->frozen)
120 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
121 return; /* disconnection ? */
123 pkt_scan = gspca_dev->sd_desc->pkt_scan;
124 for (i = 0; i < urb->number_of_packets; i++) {
126 /* check the availability of the frame buffer */
128 j = gspca_dev->fr_queue[j];
129 frame = &gspca_dev->frame[j];
130 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
131 != V4L2_BUF_FLAG_QUEUED) {
132 gspca_dev->last_packet_type = DISCARD_PACKET;
136 /* check the packet status and length */
137 len = urb->iso_frame_desc[i].actual_length;
140 st = urb->iso_frame_desc[i].status;
143 "ISOC data error: [%d] len=%d, status=%d",
145 gspca_dev->last_packet_type = DISCARD_PACKET;
149 /* let the packet be analyzed by the subdriver */
150 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
151 i, urb->iso_frame_desc[i].offset, len);
152 data = (__u8 *) urb->transfer_buffer
153 + urb->iso_frame_desc[i].offset;
154 pkt_scan(gspca_dev, frame, data, len);
157 /* resubmit the URB */
159 st = usb_submit_urb(urb, GFP_ATOMIC);
161 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
165 * ISOC message interrupt from the USB device
167 * Analyse each packet and call the subdriver for copy to the frame buffer.
169 static void isoc_irq(struct urb *urb
172 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
174 PDEBUG(D_PACK, "isoc irq");
175 if (!gspca_dev->streaming)
177 fill_frame(gspca_dev, urb);
181 * add data to the current frame
183 * This function is called by the subdrivers at interrupt level.
185 * To build a frame, these ones must add
187 * - 0 or many INTER_PACKETs
189 * DISCARD_PACKET invalidates the whole frame.
190 * On LAST_PACKET, a new frame is returned.
192 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
194 struct gspca_frame *frame,
200 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
202 /* when start of a new frame, if the current frame buffer
203 * is not queued, discard the whole frame */
204 if (packet_type == FIRST_PACKET) {
205 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
206 != V4L2_BUF_FLAG_QUEUED) {
207 gspca_dev->last_packet_type = DISCARD_PACKET;
210 frame->data_end = frame->data;
211 jiffies_to_timeval(get_jiffies_64(),
212 &frame->v4l2_buf.timestamp);
213 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
214 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
215 if (packet_type == LAST_PACKET)
216 gspca_dev->last_packet_type = packet_type;
220 /* append the packet to the frame buffer */
222 if (frame->data_end - frame->data + len
223 > frame->v4l2_buf.length) {
224 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
225 frame->data_end - frame->data + len,
226 frame->v4l2_buf.length);
227 packet_type = DISCARD_PACKET;
229 memcpy(frame->data_end, data, len);
230 frame->data_end += len;
233 gspca_dev->last_packet_type = packet_type;
235 /* if last packet, wake the application and advance in the queue */
236 if (packet_type == LAST_PACKET) {
237 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
238 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
239 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
240 atomic_inc(&gspca_dev->nevent);
241 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
242 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
244 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
245 frame->v4l2_buf.bytesused,
249 j = gspca_dev->fr_queue[i];
250 frame = &gspca_dev->frame[j];
254 EXPORT_SYMBOL(gspca_frame_add);
256 static int gspca_is_compressed(__u32 format)
259 case V4L2_PIX_FMT_MJPEG:
260 case V4L2_PIX_FMT_JPEG:
261 case V4L2_PIX_FMT_SPCA561:
262 case V4L2_PIX_FMT_PAC207:
268 static void *rvmalloc(unsigned long size)
273 /* size = PAGE_ALIGN(size); (already done) */
274 mem = vmalloc_32(size);
276 adr = (unsigned long) mem;
277 while ((long) size > 0) {
278 SetPageReserved(vmalloc_to_page((void *) adr));
286 static void rvfree(void *mem, long size)
290 adr = (unsigned long) mem;
292 ClearPageReserved(vmalloc_to_page((void *) adr));
299 static int frame_alloc(struct gspca_dev *gspca_dev,
302 struct gspca_frame *frame;
306 i = gspca_dev->curr_mode;
307 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
308 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
309 frsz = PAGE_ALIGN(frsz);
310 gspca_dev->frsz = frsz;
311 if (count > GSPCA_MAX_FRAMES)
312 count = GSPCA_MAX_FRAMES;
313 gspca_dev->frbuf = rvmalloc(frsz * count);
314 if (!gspca_dev->frbuf) {
315 err("frame alloc failed");
318 gspca_dev->nframes = count;
319 for (i = 0; i < count; i++) {
320 frame = &gspca_dev->frame[i];
321 frame->v4l2_buf.index = i;
322 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323 frame->v4l2_buf.flags = 0;
324 frame->v4l2_buf.field = V4L2_FIELD_NONE;
325 frame->v4l2_buf.length = frsz;
326 frame->v4l2_buf.memory = gspca_dev->memory;
327 frame->v4l2_buf.sequence = 0;
328 frame->data = frame->data_end =
329 gspca_dev->frbuf + i * frsz;
330 frame->v4l2_buf.m.offset = i * frsz;
332 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
333 gspca_dev->last_packet_type = DISCARD_PACKET;
334 gspca_dev->sequence = 0;
335 atomic_set(&gspca_dev->nevent, 0);
339 static void frame_free(struct gspca_dev *gspca_dev)
343 PDEBUG(D_STREAM, "frame free");
344 if (gspca_dev->frbuf != NULL) {
345 rvfree(gspca_dev->frbuf,
346 gspca_dev->nframes * gspca_dev->frsz);
347 gspca_dev->frbuf = NULL;
348 for (i = 0; i < gspca_dev->nframes; i++)
349 gspca_dev->frame[i].data = NULL;
351 gspca_dev->nframes = 0;
354 static void destroy_urbs(struct gspca_dev *gspca_dev)
359 PDEBUG(D_STREAM, "kill transfer");
360 for (i = 0; i < MAX_NURBS; ++i) {
361 urb = gspca_dev->urb[i];
365 gspca_dev->urb[i] = NULL;
367 if (urb->transfer_buffer != NULL)
368 usb_buffer_free(gspca_dev->dev,
369 urb->transfer_buffer_length,
370 urb->transfer_buffer,
377 * search an input isochronous endpoint in an alternate setting
379 static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
382 struct usb_host_endpoint *ep;
385 epaddr |= USB_DIR_IN;
386 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
387 ep = &alt->endpoint[i];
388 if (ep->desc.bEndpointAddress == epaddr) {
389 attr = ep->desc.bmAttributes
390 & USB_ENDPOINT_XFERTYPE_MASK;
391 if (attr == USB_ENDPOINT_XFER_ISOC)
400 * search an input isochronous endpoint
402 * The endpoint is defined by the subdriver.
403 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
404 * This routine may be called many times when the bandwidth is too small
405 * (the bandwidth is checked on urb submit).
407 static struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
409 struct usb_interface *intf;
410 struct usb_host_endpoint *ep;
413 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
415 i = gspca_dev->alt; /* previous alt setting */
416 while (--i > 0) { /* alt 0 is unusable */
417 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
422 err("no ISOC endpoint found");
425 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
426 i, ep->desc.bEndpointAddress);
427 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
429 err("set interface err %d", ret);
432 gspca_dev->alt = i; /* memorize the current alt setting */
437 * create the isochronous URBs
439 static int create_urbs(struct gspca_dev *gspca_dev,
440 struct usb_host_endpoint *ep)
443 int n, nurbs, i, psize, npkt, bsize;
445 /* calculate the packet size and the number of packets */
446 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
448 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
449 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
450 npkt = ISO_MAX_SIZE / psize;
451 if (npkt > ISO_MAX_PKT)
453 bsize = psize * npkt;
455 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
457 gspca_dev->nurbs = nurbs;
458 for (n = 0; n < nurbs; n++) {
459 urb = usb_alloc_urb(npkt, GFP_KERNEL);
461 err("usb_alloc_urb failed");
462 destroy_urbs(gspca_dev);
465 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
470 if (urb->transfer_buffer == NULL) {
472 err("usb_buffer_urb failed");
473 destroy_urbs(gspca_dev);
476 gspca_dev->urb[n] = urb;
477 urb->dev = gspca_dev->dev;
478 urb->context = gspca_dev;
479 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
480 ep->desc.bEndpointAddress);
481 urb->transfer_flags = URB_ISO_ASAP
482 | URB_NO_TRANSFER_DMA_MAP;
483 urb->interval = ep->desc.bInterval;
484 urb->complete = isoc_irq;
485 urb->number_of_packets = npkt;
486 urb->transfer_buffer_length = bsize;
487 for (i = 0; i < npkt; i++) {
488 urb->iso_frame_desc[i].length = psize;
489 urb->iso_frame_desc[i].offset = psize * i;
496 * start the USB transfer
498 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
500 struct usb_host_endpoint *ep;
503 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
506 /* set the higher alternate setting and
507 * loop until urb submit succeeds */
508 gspca_dev->alt = gspca_dev->nbalt;
510 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
511 ep = get_isoc_ep(gspca_dev);
516 ret = create_urbs(gspca_dev, ep);
521 gspca_dev->sd_desc->start(gspca_dev);
522 gspca_dev->streaming = 1;
523 atomic_set(&gspca_dev->nevent, 0);
525 /* submit the URBs */
526 for (n = 0; n < gspca_dev->nurbs; n++) {
527 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
529 PDEBUG(D_ERR|D_STREAM,
530 "usb_submit_urb [%d] err %d", n, ret);
531 gspca_dev->streaming = 0;
532 destroy_urbs(gspca_dev);
534 break; /* try the previous alt */
542 mutex_unlock(&gspca_dev->usb_lock);
546 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
550 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
552 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
556 /* Note both the queue and the usb lock should be hold when calling this */
557 static void gspca_stream_off(struct gspca_dev *gspca_dev)
559 gspca_dev->streaming = 0;
560 atomic_set(&gspca_dev->nevent, 0);
561 if (gspca_dev->present) {
562 if (gspca_dev->sd_desc->stopN)
563 gspca_dev->sd_desc->stopN(gspca_dev);
564 destroy_urbs(gspca_dev);
565 gspca_set_alt0(gspca_dev);
566 if (gspca_dev->sd_desc->stop0)
567 gspca_dev->sd_desc->stop0(gspca_dev);
568 PDEBUG(D_STREAM, "stream off OK");
572 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
576 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
577 gspca_dev->curr_mode = i;
578 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
579 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
580 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
583 static int wxh_to_mode(struct gspca_dev *gspca_dev,
584 int width, int height)
588 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
589 if (width >= gspca_dev->cam.cam_mode[i].width
590 && height >= gspca_dev->cam.cam_mode[i].height)
597 * search a mode with the right pixel format
599 static int gspca_get_mode(struct gspca_dev *gspca_dev,
605 modeU = modeD = mode;
606 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
608 if (gspca_dev->cam.cam_mode[modeD].pixelformat
612 if (++modeU < gspca_dev->cam.nmodes) {
613 if (gspca_dev->cam.cam_mode[modeU].pixelformat
621 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
622 struct v4l2_fmtdesc *fmtdesc)
624 struct gspca_dev *gspca_dev = priv;
628 /* give an index to each format */
631 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
632 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
635 if (fmt_tb[j] == fmt_tb[index])
640 if (fmtdesc->index == index)
641 break; /* new format */
643 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
648 return -EINVAL; /* no more format */
650 fmtdesc->pixelformat = fmt_tb[index];
651 if (gspca_is_compressed(fmt_tb[index]))
652 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
653 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
654 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
655 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
656 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
657 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
658 fmtdesc->description[4] = '\0';
662 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
663 struct v4l2_format *fmt)
665 struct gspca_dev *gspca_dev = priv;
668 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
670 mode = gspca_dev->curr_mode;
671 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
672 sizeof fmt->fmt.pix);
676 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
677 struct v4l2_format *fmt)
679 int w, h, mode, mode2;
681 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
683 w = fmt->fmt.pix.width;
684 h = fmt->fmt.pix.height;
687 if (gspca_debug & D_CONF)
688 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
690 /* search the closest mode for width and height */
691 mode = wxh_to_mode(gspca_dev, w, h);
693 /* OK if right palette */
694 if (gspca_dev->cam.cam_mode[mode].pixelformat
695 != fmt->fmt.pix.pixelformat) {
697 /* else, search the closest mode with the same pixel format */
698 mode2 = gspca_get_mode(gspca_dev, mode,
699 fmt->fmt.pix.pixelformat);
703 ; * no chance, return this mode */
705 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
706 sizeof fmt->fmt.pix);
707 return mode; /* used when s_fmt */
710 static int vidioc_try_fmt_vid_cap(struct file *file,
712 struct v4l2_format *fmt)
714 struct gspca_dev *gspca_dev = priv;
717 ret = try_fmt_vid_cap(gspca_dev, fmt);
723 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
724 struct v4l2_format *fmt)
726 struct gspca_dev *gspca_dev = priv;
729 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
732 ret = try_fmt_vid_cap(gspca_dev, fmt);
736 if (gspca_dev->nframes != 0
737 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
742 if (ret == gspca_dev->curr_mode) {
744 goto out; /* same mode */
747 if (gspca_dev->streaming) {
751 gspca_dev->width = fmt->fmt.pix.width;
752 gspca_dev->height = fmt->fmt.pix.height;
753 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
754 gspca_dev->curr_mode = ret;
758 mutex_unlock(&gspca_dev->queue_lock);
762 static int dev_open(struct inode *inode, struct file *file)
764 struct gspca_dev *gspca_dev;
767 PDEBUG(D_STREAM, "%s open", current->comm);
768 gspca_dev = (struct gspca_dev *) video_devdata(file);
769 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
771 if (!gspca_dev->present) {
776 if (gspca_dev->users > 4) { /* (arbitrary value) */
781 file->private_data = gspca_dev;
783 /* activate the v4l2 debug */
784 if (gspca_debug & D_V4L2)
785 gspca_dev->vdev.debug |= 3;
787 gspca_dev->vdev.debug &= ~3;
791 mutex_unlock(&gspca_dev->queue_lock);
793 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
795 PDEBUG(D_STREAM, "open done");
799 static int dev_close(struct inode *inode, struct file *file)
801 struct gspca_dev *gspca_dev = file->private_data;
803 PDEBUG(D_STREAM, "%s close", current->comm);
804 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
808 /* if the file did the capture, free the streaming resources */
809 if (gspca_dev->capt_file == file) {
810 if (gspca_dev->streaming) {
811 mutex_lock(&gspca_dev->usb_lock);
812 gspca_stream_off(gspca_dev);
813 mutex_unlock(&gspca_dev->usb_lock);
815 frame_free(gspca_dev);
816 gspca_dev->capt_file = NULL;
817 gspca_dev->memory = GSPCA_MEMORY_NO;
819 file->private_data = NULL;
820 mutex_unlock(&gspca_dev->queue_lock);
821 PDEBUG(D_STREAM, "close done");
825 static int vidioc_querycap(struct file *file, void *priv,
826 struct v4l2_capability *cap)
828 struct gspca_dev *gspca_dev = priv;
830 memset(cap, 0, sizeof *cap);
831 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
832 /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
833 if (gspca_dev->dev->product != NULL) {
834 strncpy(cap->card, gspca_dev->dev->product,
837 snprintf(cap->card, sizeof cap->card,
838 "USB Camera (%04x:%04x)",
839 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
840 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
842 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
843 sizeof cap->bus_info);
844 cap->version = DRIVER_VERSION_NUMBER;
845 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
847 | V4L2_CAP_READWRITE;
851 static int vidioc_queryctrl(struct file *file, void *priv,
852 struct v4l2_queryctrl *q_ctrl)
854 struct gspca_dev *gspca_dev = priv;
860 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
861 id &= V4L2_CTRL_ID_MASK;
863 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
864 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
870 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
871 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
876 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
877 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
884 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
886 if (gspca_dev->ctrl_dis & (1 << ix))
887 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
891 static int vidioc_s_ctrl(struct file *file, void *priv,
892 struct v4l2_control *ctrl)
894 struct gspca_dev *gspca_dev = priv;
895 const struct ctrl *ctrls;
898 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
899 i < gspca_dev->sd_desc->nctrls;
901 if (ctrl->id != ctrls->qctrl.id)
903 if (gspca_dev->ctrl_dis & (1 << i))
905 if (ctrl->value < ctrls->qctrl.minimum
906 || ctrl->value > ctrls->qctrl.maximum)
908 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
909 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
911 ret = ctrls->set(gspca_dev, ctrl->value);
912 mutex_unlock(&gspca_dev->usb_lock);
918 static int vidioc_g_ctrl(struct file *file, void *priv,
919 struct v4l2_control *ctrl)
921 struct gspca_dev *gspca_dev = priv;
923 const struct ctrl *ctrls;
926 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
927 i < gspca_dev->sd_desc->nctrls;
929 if (ctrl->id != ctrls->qctrl.id)
931 if (gspca_dev->ctrl_dis & (1 << i))
933 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
935 ret = ctrls->get(gspca_dev, &ctrl->value);
936 mutex_unlock(&gspca_dev->usb_lock);
942 static int vidioc_querymenu(struct file *file, void *priv,
943 struct v4l2_querymenu *qmenu)
945 struct gspca_dev *gspca_dev = priv;
947 if (!gspca_dev->sd_desc->querymenu)
949 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
952 static int vidioc_enum_input(struct file *file, void *priv,
953 struct v4l2_input *input)
955 struct gspca_dev *gspca_dev = priv;
957 if (input->index != 0)
959 memset(input, 0, sizeof *input);
960 input->type = V4L2_INPUT_TYPE_CAMERA;
961 strncpy(input->name, gspca_dev->sd_desc->name,
966 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
972 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
979 static int vidioc_reqbufs(struct file *file, void *priv,
980 struct v4l2_requestbuffers *rb)
982 struct gspca_dev *gspca_dev = priv;
985 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
987 switch (rb->memory) {
988 case GSPCA_MEMORY_READ: /* (internal call) */
989 case V4L2_MEMORY_MMAP:
990 case V4L2_MEMORY_USERPTR:
995 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
998 if (gspca_dev->memory != GSPCA_MEMORY_NO
999 && gspca_dev->memory != rb->memory) {
1004 /* only one file may do the capture */
1005 if (gspca_dev->capt_file != NULL
1006 && gspca_dev->capt_file != file) {
1011 /* if allocated, the buffers must not be mapped */
1012 for (i = 0; i < gspca_dev->nframes; i++) {
1013 if (gspca_dev->frame[i].vma_use_count) {
1019 /* stop streaming */
1020 if (gspca_dev->streaming) {
1021 mutex_lock(&gspca_dev->usb_lock);
1022 gspca_stream_off(gspca_dev);
1023 mutex_unlock(&gspca_dev->usb_lock);
1026 /* free the previous allocated buffers, if any */
1027 if (gspca_dev->nframes != 0) {
1028 frame_free(gspca_dev);
1029 gspca_dev->capt_file = NULL;
1031 if (rb->count == 0) /* unrequest */
1033 gspca_dev->memory = rb->memory;
1034 ret = frame_alloc(gspca_dev, rb->count);
1036 rb->count = gspca_dev->nframes;
1037 gspca_dev->capt_file = file;
1040 mutex_unlock(&gspca_dev->queue_lock);
1041 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1045 static int vidioc_querybuf(struct file *file, void *priv,
1046 struct v4l2_buffer *v4l2_buf)
1048 struct gspca_dev *gspca_dev = priv;
1049 struct gspca_frame *frame;
1051 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1052 || v4l2_buf->index < 0
1053 || v4l2_buf->index >= gspca_dev->nframes)
1056 frame = &gspca_dev->frame[v4l2_buf->index];
1057 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1061 static int vidioc_streamon(struct file *file, void *priv,
1062 enum v4l2_buf_type buf_type)
1064 struct gspca_dev *gspca_dev = priv;
1067 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1069 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1070 return -ERESTARTSYS;
1071 if (!gspca_dev->present) {
1075 if (gspca_dev->nframes == 0) {
1079 if (!gspca_dev->streaming) {
1080 ret = gspca_init_transfer(gspca_dev);
1085 if (gspca_debug & D_STREAM) {
1086 PDEBUG_MODE("stream on OK",
1094 mutex_unlock(&gspca_dev->queue_lock);
1098 static int vidioc_streamoff(struct file *file, void *priv,
1099 enum v4l2_buf_type buf_type)
1101 struct gspca_dev *gspca_dev = priv;
1104 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1106 if (!gspca_dev->streaming)
1108 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1109 return -ERESTARTSYS;
1111 /* stop streaming */
1112 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1116 gspca_stream_off(gspca_dev);
1117 mutex_unlock(&gspca_dev->usb_lock);
1119 /* empty the application queues */
1120 for (i = 0; i < gspca_dev->nframes; i++)
1121 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1122 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1123 gspca_dev->last_packet_type = DISCARD_PACKET;
1124 gspca_dev->sequence = 0;
1125 atomic_set(&gspca_dev->nevent, 0);
1128 mutex_unlock(&gspca_dev->queue_lock);
1132 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1133 struct v4l2_jpegcompression *jpegcomp)
1135 struct gspca_dev *gspca_dev = priv;
1138 if (!gspca_dev->sd_desc->get_jcomp)
1140 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1141 return -ERESTARTSYS;
1142 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1143 mutex_unlock(&gspca_dev->usb_lock);
1147 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1148 struct v4l2_jpegcompression *jpegcomp)
1150 struct gspca_dev *gspca_dev = priv;
1153 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1154 return -ERESTARTSYS;
1155 if (!gspca_dev->sd_desc->set_jcomp)
1157 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1158 mutex_unlock(&gspca_dev->usb_lock);
1162 static int vidioc_g_parm(struct file *filp, void *priv,
1163 struct v4l2_streamparm *parm)
1165 struct gspca_dev *gspca_dev = priv;
1167 memset(parm, 0, sizeof *parm);
1168 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1169 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1173 static int vidioc_s_parm(struct file *filp, void *priv,
1174 struct v4l2_streamparm *parm)
1176 struct gspca_dev *gspca_dev = priv;
1179 n = parm->parm.capture.readbuffers;
1180 if (n == 0 || n > GSPCA_MAX_FRAMES)
1181 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1183 gspca_dev->nbufread = n;
1187 static int vidioc_s_std(struct file *filp, void *priv,
1193 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1194 static int vidiocgmbuf(struct file *file, void *priv,
1195 struct video_mbuf *mbuf)
1197 struct gspca_dev *gspca_dev = file->private_data;
1200 PDEBUG(D_STREAM, "cgmbuf");
1201 if (gspca_dev->nframes == 0) {
1205 struct v4l2_format fmt;
1207 memset(&fmt, 0, sizeof fmt);
1208 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1209 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1210 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1211 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1212 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1213 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1218 struct v4l2_requestbuffers rb;
1220 memset(&rb, 0, sizeof rb);
1222 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1223 rb.memory = V4L2_MEMORY_MMAP;
1224 ret = vidioc_reqbufs(file, priv, &rb);
1229 mbuf->frames = gspca_dev->nframes;
1230 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1231 for (i = 0; i < mbuf->frames; i++)
1232 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1237 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1239 struct gspca_dev *gspca_dev = file->private_data;
1240 struct gspca_frame *frame;
1242 unsigned long addr, start, size;
1245 start = vma->vm_start;
1246 size = vma->vm_end - vma->vm_start;
1247 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1249 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1250 return -ERESTARTSYS;
1251 if (!gspca_dev->present) {
1255 if (gspca_dev->capt_file != file) {
1261 for (i = 0; i < gspca_dev->nframes; ++i) {
1262 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1263 PDEBUG(D_STREAM, "mmap bad memory type");
1266 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1268 frame = &gspca_dev->frame[i];
1272 if (frame == NULL) {
1273 PDEBUG(D_STREAM, "mmap no frame buffer found");
1277 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1278 /* v4l1 maps all the buffers */
1280 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1282 if (size != frame->v4l2_buf.length) {
1283 PDEBUG(D_STREAM, "mmap bad size");
1289 * - VM_IO marks the area as being a mmaped region for I/O to a
1290 * device. It also prevents the region from being core dumped.
1292 vma->vm_flags |= VM_IO;
1294 addr = (unsigned long) frame->data;
1296 page = vmalloc_to_page((void *) addr);
1297 ret = vm_insert_page(vma, start, page);
1305 vma->vm_ops = &gspca_vm_ops;
1306 vma->vm_private_data = frame;
1310 mutex_unlock(&gspca_dev->queue_lock);
1315 * wait for a video frame
1317 * If a frame is ready, its index is returned.
1319 static int frame_wait(struct gspca_dev *gspca_dev,
1322 struct gspca_frame *frame;
1325 /* check if a frame is ready */
1326 i = gspca_dev->fr_o;
1327 j = gspca_dev->fr_queue[i];
1328 frame = &gspca_dev->frame[j];
1329 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1330 atomic_dec(&gspca_dev->nevent);
1333 if (nonblock_ing) /* no frame yet */
1336 /* wait till a frame is ready */
1338 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1339 atomic_read(&gspca_dev->nevent) > 0,
1340 msecs_to_jiffies(3000));
1343 return ret; /* interrupt */
1344 return -EIO; /* timeout */
1346 atomic_dec(&gspca_dev->nevent);
1347 if (!gspca_dev->streaming || !gspca_dev->present)
1349 i = gspca_dev->fr_o;
1350 j = gspca_dev->fr_queue[i];
1351 frame = &gspca_dev->frame[j];
1352 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1356 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1357 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1362 if (gspca_dev->sd_desc->dq_callback) {
1363 mutex_lock(&gspca_dev->usb_lock);
1364 gspca_dev->sd_desc->dq_callback(gspca_dev);
1365 mutex_unlock(&gspca_dev->usb_lock);
1371 * dequeue a video buffer
1373 * If nonblock_ing is false, block until a buffer is available.
1375 static int vidioc_dqbuf(struct file *file, void *priv,
1376 struct v4l2_buffer *v4l2_buf)
1378 struct gspca_dev *gspca_dev = priv;
1379 struct gspca_frame *frame;
1382 PDEBUG(D_FRAM, "dqbuf");
1383 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1385 if (v4l2_buf->memory != gspca_dev->memory)
1388 /* if not streaming, be sure the application will not loop forever */
1389 if (!(file->f_flags & O_NONBLOCK)
1390 && !gspca_dev->streaming && gspca_dev->users == 1)
1393 /* only the capturing file may dequeue */
1394 if (gspca_dev->capt_file != file)
1397 /* only one dequeue / read at a time */
1398 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1399 return -ERESTARTSYS;
1401 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1404 i = ret; /* frame index */
1405 frame = &gspca_dev->frame[i];
1406 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1407 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1409 frame->v4l2_buf.bytesused)) {
1410 PDEBUG(D_ERR|D_STREAM,
1411 "dqbuf cp to user failed");
1416 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1417 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1418 PDEBUG(D_FRAM, "dqbuf %d", i);
1421 mutex_unlock(&gspca_dev->read_lock);
1426 * queue a video buffer
1428 * Attempting to queue a buffer that has already been
1429 * queued will return -EINVAL.
1431 static int vidioc_qbuf(struct file *file, void *priv,
1432 struct v4l2_buffer *v4l2_buf)
1434 struct gspca_dev *gspca_dev = priv;
1435 struct gspca_frame *frame;
1438 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1439 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1442 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1443 return -ERESTARTSYS;
1445 index = v4l2_buf->index;
1446 if ((unsigned) index >= gspca_dev->nframes) {
1448 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1452 if (v4l2_buf->memory != gspca_dev->memory) {
1453 PDEBUG(D_FRAM, "qbuf bad memory type");
1458 frame = &gspca_dev->frame[index];
1459 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1460 PDEBUG(D_FRAM, "qbuf bad state");
1465 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1466 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1468 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1469 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1470 frame->v4l2_buf.length = v4l2_buf->length;
1473 /* put the buffer in the 'queued' queue */
1474 i = gspca_dev->fr_q;
1475 gspca_dev->fr_queue[i] = index;
1476 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1477 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1482 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1483 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1486 mutex_unlock(&gspca_dev->queue_lock);
1491 * allocate the resources for read()
1493 static int read_alloc(struct gspca_dev *gspca_dev,
1496 struct v4l2_buffer v4l2_buf;
1499 PDEBUG(D_STREAM, "read alloc");
1500 if (gspca_dev->nframes == 0) {
1501 struct v4l2_requestbuffers rb;
1503 memset(&rb, 0, sizeof rb);
1504 rb.count = gspca_dev->nbufread;
1505 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1506 rb.memory = GSPCA_MEMORY_READ;
1507 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1509 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1512 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1513 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1514 v4l2_buf.memory = GSPCA_MEMORY_READ;
1515 for (i = 0; i < gspca_dev->nbufread; i++) {
1517 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1519 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1523 gspca_dev->memory = GSPCA_MEMORY_READ;
1526 /* start streaming */
1527 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1529 PDEBUG(D_STREAM, "read streamon err %d", ret);
1533 static unsigned int dev_poll(struct file *file, poll_table *wait)
1535 struct gspca_dev *gspca_dev = file->private_data;
1538 PDEBUG(D_FRAM, "poll");
1540 poll_wait(file, &gspca_dev->wq, wait);
1541 if (!gspca_dev->present)
1544 /* if reqbufs is not done, the user would use read() */
1545 if (gspca_dev->nframes == 0) {
1546 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1547 return POLLERR; /* not the 1st time */
1548 ret = read_alloc(gspca_dev, file);
1553 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1555 if (!gspca_dev->present) {
1560 /* check the next incoming buffer */
1561 i = gspca_dev->fr_o;
1562 i = gspca_dev->fr_queue[i];
1563 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1564 ret = POLLIN | POLLRDNORM; /* something to read */
1568 mutex_unlock(&gspca_dev->queue_lock);
1572 static ssize_t dev_read(struct file *file, char __user *data,
1573 size_t count, loff_t *ppos)
1575 struct gspca_dev *gspca_dev = file->private_data;
1576 struct gspca_frame *frame;
1577 struct v4l2_buffer v4l2_buf;
1578 struct timeval timestamp;
1581 PDEBUG(D_FRAM, "read (%zd)", count);
1582 if (!gspca_dev->present)
1584 switch (gspca_dev->memory) {
1585 case GSPCA_MEMORY_NO: /* first time */
1586 ret = read_alloc(gspca_dev, file);
1590 case GSPCA_MEMORY_READ:
1591 if (gspca_dev->capt_file == file)
1599 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1603 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1604 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1605 v4l2_buf.memory = GSPCA_MEMORY_READ;
1606 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1608 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1612 /* if the process slept for more than 1 second,
1613 * get anewer frame */
1614 frame = &gspca_dev->frame[v4l2_buf.index];
1616 break; /* avoid infinite loop */
1617 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1619 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1621 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1626 /* copy the frame */
1627 if (count > frame->v4l2_buf.bytesused)
1628 count = frame->v4l2_buf.bytesused;
1629 ret = copy_to_user(data, frame->data, count);
1631 PDEBUG(D_ERR|D_STREAM,
1632 "read cp to user lack %d / %zd", ret, count);
1638 /* in each case, requeue the buffer */
1639 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1645 static void dev_release(struct video_device *vfd)
1650 static struct file_operations dev_fops = {
1651 .owner = THIS_MODULE,
1653 .release = dev_close,
1656 .ioctl = video_ioctl2,
1657 #ifdef CONFIG_COMPAT
1658 .compat_ioctl = v4l_compat_ioctl32,
1660 .llseek = no_llseek,
1664 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1665 .vidioc_querycap = vidioc_querycap,
1666 .vidioc_dqbuf = vidioc_dqbuf,
1667 .vidioc_qbuf = vidioc_qbuf,
1668 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1669 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1670 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1671 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1672 .vidioc_streamon = vidioc_streamon,
1673 .vidioc_queryctrl = vidioc_queryctrl,
1674 .vidioc_g_ctrl = vidioc_g_ctrl,
1675 .vidioc_s_ctrl = vidioc_s_ctrl,
1676 .vidioc_querymenu = vidioc_querymenu,
1677 .vidioc_enum_input = vidioc_enum_input,
1678 .vidioc_g_input = vidioc_g_input,
1679 .vidioc_s_input = vidioc_s_input,
1680 .vidioc_reqbufs = vidioc_reqbufs,
1681 .vidioc_querybuf = vidioc_querybuf,
1682 .vidioc_streamoff = vidioc_streamoff,
1683 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1684 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1685 .vidioc_g_parm = vidioc_g_parm,
1686 .vidioc_s_parm = vidioc_s_parm,
1687 .vidioc_s_std = vidioc_s_std,
1688 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1689 .vidiocgmbuf = vidiocgmbuf,
1693 static struct video_device gspca_template = {
1694 .name = "gspca main driver",
1696 .ioctl_ops = &dev_ioctl_ops,
1697 .release = dev_release, /* mandatory */
1702 * probe and create a new gspca device
1704 * This function must be called by the sub-driver when it is
1705 * called for probing a new device.
1707 int gspca_dev_probe(struct usb_interface *intf,
1708 const struct usb_device_id *id,
1709 const struct sd_desc *sd_desc,
1711 struct module *module)
1713 struct usb_interface_descriptor *interface;
1714 struct gspca_dev *gspca_dev;
1715 struct usb_device *dev = interface_to_usbdev(intf);
1718 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1720 /* we don't handle multi-config cameras */
1721 if (dev->descriptor.bNumConfigurations != 1)
1723 interface = &intf->cur_altsetting->desc;
1724 if (interface->bInterfaceNumber > 0)
1727 /* create the device */
1728 if (dev_size < sizeof *gspca_dev)
1729 dev_size = sizeof *gspca_dev;
1730 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1731 if (gspca_dev == NULL) {
1732 err("couldn't kzalloc gspca struct");
1735 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1736 if (!gspca_dev->usb_buf) {
1737 err("out of memory");
1741 gspca_dev->dev = dev;
1742 gspca_dev->iface = interface->bInterfaceNumber;
1743 gspca_dev->nbalt = intf->num_altsetting;
1744 gspca_dev->sd_desc = sd_desc;
1745 /* gspca_dev->users = 0; (done by kzalloc) */
1746 gspca_dev->nbufread = 2;
1748 /* configure the subdriver and initialize the USB device */
1749 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1752 ret = gspca_dev->sd_desc->init(gspca_dev);
1755 ret = gspca_set_alt0(gspca_dev);
1758 gspca_set_default_mode(gspca_dev);
1760 mutex_init(&gspca_dev->usb_lock);
1761 mutex_init(&gspca_dev->read_lock);
1762 mutex_init(&gspca_dev->queue_lock);
1763 init_waitqueue_head(&gspca_dev->wq);
1765 /* init video stuff */
1766 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1767 gspca_dev->vdev.parent = &dev->dev;
1768 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1769 gspca_dev->vdev.fops = &gspca_dev->fops;
1770 gspca_dev->fops.owner = module; /* module protection */
1771 gspca_dev->present = 1;
1772 ret = video_register_device(&gspca_dev->vdev,
1776 err("video_register_device err %d", ret);
1780 usb_set_intfdata(intf, gspca_dev);
1781 PDEBUG(D_PROBE, "probe ok");
1784 kfree(gspca_dev->usb_buf);
1788 EXPORT_SYMBOL(gspca_dev_probe);
1793 * This function must be called by the sub-driver
1794 * when the device disconnects, after the specific resources are freed.
1796 void gspca_disconnect(struct usb_interface *intf)
1798 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1802 gspca_dev->present = 0;
1803 mutex_lock(&gspca_dev->queue_lock);
1804 mutex_lock(&gspca_dev->usb_lock);
1805 gspca_dev->streaming = 0;
1806 destroy_urbs(gspca_dev);
1807 mutex_unlock(&gspca_dev->usb_lock);
1808 mutex_unlock(&gspca_dev->queue_lock);
1809 while (gspca_dev->users != 0) { /* wait until fully closed */
1810 atomic_inc(&gspca_dev->nevent);
1811 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1814 /* We don't want people trying to open up the device */
1815 video_unregister_device(&gspca_dev->vdev);
1816 /* Free the memory */
1817 kfree(gspca_dev->usb_buf);
1819 PDEBUG(D_PROBE, "disconnect complete");
1821 EXPORT_SYMBOL(gspca_disconnect);
1824 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1826 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1828 if (!gspca_dev->streaming)
1830 gspca_dev->frozen = 1; /* avoid urb error messages */
1831 if (gspca_dev->sd_desc->stopN)
1832 gspca_dev->sd_desc->stopN(gspca_dev);
1833 destroy_urbs(gspca_dev);
1834 gspca_set_alt0(gspca_dev);
1835 if (gspca_dev->sd_desc->stop0)
1836 gspca_dev->sd_desc->stop0(gspca_dev);
1839 EXPORT_SYMBOL(gspca_suspend);
1841 int gspca_resume(struct usb_interface *intf)
1843 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1845 gspca_dev->frozen = 0;
1846 gspca_dev->sd_desc->init(gspca_dev);
1847 if (gspca_dev->streaming)
1848 return gspca_init_transfer(gspca_dev);
1851 EXPORT_SYMBOL(gspca_resume);
1853 /* -- cam driver utility functions -- */
1855 /* auto gain and exposure algorithm based on the knee algorithm described here:
1856 http://ytse.tricolour.net/docs/LowLightOptimization.html
1858 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1860 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1861 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1863 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1864 const struct ctrl *gain_ctrl = NULL;
1865 const struct ctrl *exposure_ctrl = NULL;
1866 const struct ctrl *autogain_ctrl = NULL;
1869 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1870 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1871 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1872 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1873 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1874 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1875 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1877 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1878 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1879 "on cam without (auto)gain/exposure");
1883 if (gain_ctrl->get(gspca_dev, &gain) ||
1884 exposure_ctrl->get(gspca_dev, &exposure) ||
1885 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1889 orig_exposure = exposure;
1891 /* If we are of a multiple of deadzone, do multiple steps to reach the
1892 desired lumination fast (with the risc of a slight overshoot) */
1893 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1895 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1896 avg_lum, desired_avg_lum, steps);
1898 for (i = 0; i < steps; i++) {
1899 if (avg_lum > desired_avg_lum) {
1900 if (gain > gain_knee)
1902 else if (exposure > exposure_knee)
1904 else if (gain > gain_ctrl->qctrl.default_value)
1906 else if (exposure > exposure_ctrl->qctrl.minimum)
1908 else if (gain > gain_ctrl->qctrl.minimum)
1913 if (gain < gain_ctrl->qctrl.default_value)
1915 else if (exposure < exposure_knee)
1917 else if (gain < gain_knee)
1919 else if (exposure < exposure_ctrl->qctrl.maximum)
1921 else if (gain < gain_ctrl->qctrl.maximum)
1928 if (gain != orig_gain) {
1929 gain_ctrl->set(gspca_dev, gain);
1932 if (exposure != orig_exposure) {
1933 exposure_ctrl->set(gspca_dev, exposure);
1939 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1941 /* -- module insert / remove -- */
1942 static int __init gspca_init(void)
1944 info("main v%d.%d.%d registered",
1945 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1946 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1947 DRIVER_VERSION_NUMBER & 0xff);
1950 static void __exit gspca_exit(void)
1952 info("main deregistered");
1955 module_init(gspca_init);
1956 module_exit(gspca_exit);
1959 module_param_named(debug, gspca_debug, int, 0644);
1960 MODULE_PARM_DESC(debug,
1961 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
1962 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"