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");
464 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
469 if (urb->transfer_buffer == NULL) {
471 destroy_urbs(gspca_dev);
472 err("usb_buffer_urb failed");
475 gspca_dev->urb[n] = urb;
476 urb->dev = gspca_dev->dev;
477 urb->context = gspca_dev;
478 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
479 ep->desc.bEndpointAddress);
480 urb->transfer_flags = URB_ISO_ASAP
481 | URB_NO_TRANSFER_DMA_MAP;
482 urb->interval = ep->desc.bInterval;
483 urb->complete = isoc_irq;
484 urb->number_of_packets = npkt;
485 urb->transfer_buffer_length = bsize;
486 for (i = 0; i < npkt; i++) {
487 urb->iso_frame_desc[i].length = psize;
488 urb->iso_frame_desc[i].offset = psize * i;
495 * start the USB transfer
497 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
499 struct usb_host_endpoint *ep;
502 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
505 /* set the higher alternate setting and
506 * loop until urb submit succeeds */
507 gspca_dev->alt = gspca_dev->nbalt;
509 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
510 ep = get_isoc_ep(gspca_dev);
515 ret = create_urbs(gspca_dev, ep);
520 gspca_dev->sd_desc->start(gspca_dev);
521 gspca_dev->streaming = 1;
522 atomic_set(&gspca_dev->nevent, 0);
524 /* submit the URBs */
525 for (n = 0; n < gspca_dev->nurbs; n++) {
526 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
528 PDEBUG(D_ERR|D_STREAM,
529 "usb_submit_urb [%d] err %d", n, ret);
530 gspca_dev->streaming = 0;
531 destroy_urbs(gspca_dev);
533 break; /* try the previous alt */
541 mutex_unlock(&gspca_dev->usb_lock);
545 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
549 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
551 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
555 /* Note both the queue and the usb lock should be hold when calling this */
556 static void gspca_stream_off(struct gspca_dev *gspca_dev)
558 gspca_dev->streaming = 0;
559 atomic_set(&gspca_dev->nevent, 0);
560 if (gspca_dev->present) {
561 if (gspca_dev->sd_desc->stopN)
562 gspca_dev->sd_desc->stopN(gspca_dev);
563 destroy_urbs(gspca_dev);
564 gspca_set_alt0(gspca_dev);
565 if (gspca_dev->sd_desc->stop0)
566 gspca_dev->sd_desc->stop0(gspca_dev);
567 PDEBUG(D_STREAM, "stream off OK");
571 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
575 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
576 gspca_dev->curr_mode = i;
577 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
578 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
579 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
582 static int wxh_to_mode(struct gspca_dev *gspca_dev,
583 int width, int height)
587 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
588 if (width >= gspca_dev->cam.cam_mode[i].width
589 && height >= gspca_dev->cam.cam_mode[i].height)
596 * search a mode with the right pixel format
598 static int gspca_get_mode(struct gspca_dev *gspca_dev,
604 modeU = modeD = mode;
605 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
607 if (gspca_dev->cam.cam_mode[modeD].pixelformat
611 if (++modeU < gspca_dev->cam.nmodes) {
612 if (gspca_dev->cam.cam_mode[modeU].pixelformat
620 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
621 struct v4l2_fmtdesc *fmtdesc)
623 struct gspca_dev *gspca_dev = priv;
627 /* give an index to each format */
630 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
631 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
634 if (fmt_tb[j] == fmt_tb[index])
639 if (fmtdesc->index == index)
640 break; /* new format */
642 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
647 return -EINVAL; /* no more format */
649 fmtdesc->pixelformat = fmt_tb[index];
650 if (gspca_is_compressed(fmt_tb[index]))
651 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
652 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
653 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
654 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
655 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
656 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
657 fmtdesc->description[4] = '\0';
661 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
662 struct v4l2_format *fmt)
664 struct gspca_dev *gspca_dev = priv;
667 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
669 mode = gspca_dev->curr_mode;
670 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
671 sizeof fmt->fmt.pix);
675 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
676 struct v4l2_format *fmt)
678 int w, h, mode, mode2;
680 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
682 w = fmt->fmt.pix.width;
683 h = fmt->fmt.pix.height;
686 if (gspca_debug & D_CONF)
687 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
689 /* search the closest mode for width and height */
690 mode = wxh_to_mode(gspca_dev, w, h);
692 /* OK if right palette */
693 if (gspca_dev->cam.cam_mode[mode].pixelformat
694 != fmt->fmt.pix.pixelformat) {
696 /* else, search the closest mode with the same pixel format */
697 mode2 = gspca_get_mode(gspca_dev, mode,
698 fmt->fmt.pix.pixelformat);
702 ; * no chance, return this mode */
704 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
705 sizeof fmt->fmt.pix);
706 return mode; /* used when s_fmt */
709 static int vidioc_try_fmt_vid_cap(struct file *file,
711 struct v4l2_format *fmt)
713 struct gspca_dev *gspca_dev = priv;
716 ret = try_fmt_vid_cap(gspca_dev, fmt);
722 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
723 struct v4l2_format *fmt)
725 struct gspca_dev *gspca_dev = priv;
728 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
731 ret = try_fmt_vid_cap(gspca_dev, fmt);
735 if (gspca_dev->nframes != 0
736 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
741 if (ret == gspca_dev->curr_mode) {
743 goto out; /* same mode */
746 if (gspca_dev->streaming) {
750 gspca_dev->width = fmt->fmt.pix.width;
751 gspca_dev->height = fmt->fmt.pix.height;
752 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
753 gspca_dev->curr_mode = ret;
757 mutex_unlock(&gspca_dev->queue_lock);
761 static int dev_open(struct inode *inode, struct file *file)
763 struct gspca_dev *gspca_dev;
766 PDEBUG(D_STREAM, "%s open", current->comm);
767 gspca_dev = (struct gspca_dev *) video_devdata(file);
768 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
770 if (!gspca_dev->present) {
775 if (gspca_dev->users > 4) { /* (arbitrary value) */
780 file->private_data = gspca_dev;
782 /* activate the v4l2 debug */
783 if (gspca_debug & D_V4L2)
784 gspca_dev->vdev.debug |= 3;
786 gspca_dev->vdev.debug &= ~3;
790 mutex_unlock(&gspca_dev->queue_lock);
792 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
794 PDEBUG(D_STREAM, "open done");
798 static int dev_close(struct inode *inode, struct file *file)
800 struct gspca_dev *gspca_dev = file->private_data;
802 PDEBUG(D_STREAM, "%s close", current->comm);
803 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
807 /* if the file did the capture, free the streaming resources */
808 if (gspca_dev->capt_file == file) {
809 frame_free(gspca_dev);
810 gspca_dev->capt_file = NULL;
811 gspca_dev->memory = GSPCA_MEMORY_NO;
813 file->private_data = NULL;
814 mutex_unlock(&gspca_dev->queue_lock);
815 PDEBUG(D_STREAM, "close done");
819 static int vidioc_querycap(struct file *file, void *priv,
820 struct v4l2_capability *cap)
822 struct gspca_dev *gspca_dev = priv;
824 memset(cap, 0, sizeof *cap);
825 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
826 /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
827 if (gspca_dev->dev->product != NULL) {
828 strncpy(cap->card, gspca_dev->dev->product,
831 snprintf(cap->card, sizeof cap->card,
832 "USB Camera (%04x:%04x)",
833 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
834 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
836 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
837 sizeof cap->bus_info);
838 cap->version = DRIVER_VERSION_NUMBER;
839 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
841 | V4L2_CAP_READWRITE;
845 static int vidioc_queryctrl(struct file *file, void *priv,
846 struct v4l2_queryctrl *q_ctrl)
848 struct gspca_dev *gspca_dev = priv;
854 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
855 id &= V4L2_CTRL_ID_MASK;
857 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
858 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
864 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
865 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
870 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
871 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
878 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
880 if (gspca_dev->ctrl_dis & (1 << ix))
881 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
885 static int vidioc_s_ctrl(struct file *file, void *priv,
886 struct v4l2_control *ctrl)
888 struct gspca_dev *gspca_dev = priv;
889 const struct ctrl *ctrls;
892 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
893 i < gspca_dev->sd_desc->nctrls;
895 if (ctrl->id != ctrls->qctrl.id)
897 if (gspca_dev->ctrl_dis & (1 << i))
899 if (ctrl->value < ctrls->qctrl.minimum
900 || ctrl->value > ctrls->qctrl.maximum)
902 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
903 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
905 ret = ctrls->set(gspca_dev, ctrl->value);
906 mutex_unlock(&gspca_dev->usb_lock);
912 static int vidioc_g_ctrl(struct file *file, void *priv,
913 struct v4l2_control *ctrl)
915 struct gspca_dev *gspca_dev = priv;
917 const struct ctrl *ctrls;
920 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
921 i < gspca_dev->sd_desc->nctrls;
923 if (ctrl->id != ctrls->qctrl.id)
925 if (gspca_dev->ctrl_dis & (1 << i))
927 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
929 ret = ctrls->get(gspca_dev, &ctrl->value);
930 mutex_unlock(&gspca_dev->usb_lock);
936 static int vidioc_querymenu(struct file *file, void *priv,
937 struct v4l2_querymenu *qmenu)
939 struct gspca_dev *gspca_dev = priv;
941 if (!gspca_dev->sd_desc->querymenu)
943 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
946 static int vidioc_enum_input(struct file *file, void *priv,
947 struct v4l2_input *input)
949 struct gspca_dev *gspca_dev = priv;
951 if (input->index != 0)
953 memset(input, 0, sizeof *input);
954 input->type = V4L2_INPUT_TYPE_CAMERA;
955 strncpy(input->name, gspca_dev->sd_desc->name,
960 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
966 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
973 static int vidioc_reqbufs(struct file *file, void *priv,
974 struct v4l2_requestbuffers *rb)
976 struct gspca_dev *gspca_dev = priv;
979 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
981 switch (rb->memory) {
982 case GSPCA_MEMORY_READ: /* (internal call) */
983 case V4L2_MEMORY_MMAP:
984 case V4L2_MEMORY_USERPTR:
989 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
992 if (gspca_dev->memory != GSPCA_MEMORY_NO
993 && gspca_dev->memory != rb->memory) {
998 /* only one file may do the capture */
999 if (gspca_dev->capt_file != NULL
1000 && gspca_dev->capt_file != file) {
1005 /* if allocated, the buffers must not be mapped */
1006 for (i = 0; i < gspca_dev->nframes; i++) {
1007 if (gspca_dev->frame[i].vma_use_count) {
1013 /* stop streaming */
1014 if (gspca_dev->streaming) {
1015 mutex_lock(&gspca_dev->usb_lock);
1016 gspca_stream_off(gspca_dev);
1017 mutex_unlock(&gspca_dev->usb_lock);
1020 /* free the previous allocated buffers, if any */
1021 if (gspca_dev->nframes != 0) {
1022 frame_free(gspca_dev);
1023 gspca_dev->capt_file = NULL;
1025 if (rb->count == 0) /* unrequest */
1027 gspca_dev->memory = rb->memory;
1028 ret = frame_alloc(gspca_dev, rb->count);
1030 rb->count = gspca_dev->nframes;
1031 gspca_dev->capt_file = file;
1034 mutex_unlock(&gspca_dev->queue_lock);
1035 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1039 static int vidioc_querybuf(struct file *file, void *priv,
1040 struct v4l2_buffer *v4l2_buf)
1042 struct gspca_dev *gspca_dev = priv;
1043 struct gspca_frame *frame;
1045 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1046 || v4l2_buf->index < 0
1047 || v4l2_buf->index >= gspca_dev->nframes)
1050 frame = &gspca_dev->frame[v4l2_buf->index];
1051 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1055 static int vidioc_streamon(struct file *file, void *priv,
1056 enum v4l2_buf_type buf_type)
1058 struct gspca_dev *gspca_dev = priv;
1061 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1063 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1064 return -ERESTARTSYS;
1065 if (!gspca_dev->present) {
1069 if (gspca_dev->nframes == 0) {
1073 if (!gspca_dev->streaming) {
1074 ret = gspca_init_transfer(gspca_dev);
1079 if (gspca_debug & D_STREAM) {
1080 PDEBUG_MODE("stream on OK",
1088 mutex_unlock(&gspca_dev->queue_lock);
1092 static int vidioc_streamoff(struct file *file, void *priv,
1093 enum v4l2_buf_type buf_type)
1095 struct gspca_dev *gspca_dev = priv;
1098 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1100 if (!gspca_dev->streaming)
1102 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1103 return -ERESTARTSYS;
1105 /* stop streaming */
1106 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1110 gspca_stream_off(gspca_dev);
1111 mutex_unlock(&gspca_dev->usb_lock);
1113 /* empty the application queues */
1114 for (i = 0; i < gspca_dev->nframes; i++)
1115 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1116 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1117 gspca_dev->last_packet_type = DISCARD_PACKET;
1118 gspca_dev->sequence = 0;
1119 atomic_set(&gspca_dev->nevent, 0);
1122 mutex_unlock(&gspca_dev->queue_lock);
1126 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1127 struct v4l2_jpegcompression *jpegcomp)
1129 struct gspca_dev *gspca_dev = priv;
1132 if (!gspca_dev->sd_desc->get_jcomp)
1134 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1135 return -ERESTARTSYS;
1136 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1137 mutex_unlock(&gspca_dev->usb_lock);
1141 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1142 struct v4l2_jpegcompression *jpegcomp)
1144 struct gspca_dev *gspca_dev = priv;
1147 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1148 return -ERESTARTSYS;
1149 if (!gspca_dev->sd_desc->set_jcomp)
1151 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1152 mutex_unlock(&gspca_dev->usb_lock);
1156 static int vidioc_g_parm(struct file *filp, void *priv,
1157 struct v4l2_streamparm *parm)
1159 struct gspca_dev *gspca_dev = priv;
1161 memset(parm, 0, sizeof *parm);
1162 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1163 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1167 static int vidioc_s_parm(struct file *filp, void *priv,
1168 struct v4l2_streamparm *parm)
1170 struct gspca_dev *gspca_dev = priv;
1173 n = parm->parm.capture.readbuffers;
1174 if (n == 0 || n > GSPCA_MAX_FRAMES)
1175 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1177 gspca_dev->nbufread = n;
1181 static int vidioc_s_std(struct file *filp, void *priv,
1187 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1188 static int vidiocgmbuf(struct file *file, void *priv,
1189 struct video_mbuf *mbuf)
1191 struct gspca_dev *gspca_dev = file->private_data;
1194 PDEBUG(D_STREAM, "cgmbuf");
1195 if (gspca_dev->nframes == 0) {
1199 struct v4l2_format fmt;
1201 memset(&fmt, 0, sizeof fmt);
1202 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1203 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1204 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1205 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1206 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1207 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1212 struct v4l2_requestbuffers rb;
1214 memset(&rb, 0, sizeof rb);
1216 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1217 rb.memory = V4L2_MEMORY_MMAP;
1218 ret = vidioc_reqbufs(file, priv, &rb);
1223 mbuf->frames = gspca_dev->nframes;
1224 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1225 for (i = 0; i < mbuf->frames; i++)
1226 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1231 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1233 struct gspca_dev *gspca_dev = file->private_data;
1234 struct gspca_frame *frame;
1236 unsigned long addr, start, size;
1239 start = vma->vm_start;
1240 size = vma->vm_end - vma->vm_start;
1241 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1243 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1244 return -ERESTARTSYS;
1245 if (!gspca_dev->present) {
1249 if (gspca_dev->capt_file != file) {
1255 for (i = 0; i < gspca_dev->nframes; ++i) {
1256 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1257 PDEBUG(D_STREAM, "mmap bad memory type");
1260 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1262 frame = &gspca_dev->frame[i];
1266 if (frame == NULL) {
1267 PDEBUG(D_STREAM, "mmap no frame buffer found");
1271 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1272 /* v4l1 maps all the buffers */
1274 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1276 if (size != frame->v4l2_buf.length) {
1277 PDEBUG(D_STREAM, "mmap bad size");
1283 * - VM_IO marks the area as being a mmaped region for I/O to a
1284 * device. It also prevents the region from being core dumped.
1286 vma->vm_flags |= VM_IO;
1288 addr = (unsigned long) frame->data;
1290 page = vmalloc_to_page((void *) addr);
1291 ret = vm_insert_page(vma, start, page);
1299 vma->vm_ops = &gspca_vm_ops;
1300 vma->vm_private_data = frame;
1304 mutex_unlock(&gspca_dev->queue_lock);
1309 * wait for a video frame
1311 * If a frame is ready, its index is returned.
1313 static int frame_wait(struct gspca_dev *gspca_dev,
1316 struct gspca_frame *frame;
1319 /* check if a frame is ready */
1320 i = gspca_dev->fr_o;
1321 j = gspca_dev->fr_queue[i];
1322 frame = &gspca_dev->frame[j];
1323 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1324 atomic_dec(&gspca_dev->nevent);
1327 if (nonblock_ing) /* no frame yet */
1330 /* wait till a frame is ready */
1332 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1333 atomic_read(&gspca_dev->nevent) > 0,
1334 msecs_to_jiffies(3000));
1337 return ret; /* interrupt */
1338 return -EIO; /* timeout */
1340 atomic_dec(&gspca_dev->nevent);
1341 if (!gspca_dev->streaming || !gspca_dev->present)
1343 i = gspca_dev->fr_o;
1344 j = gspca_dev->fr_queue[i];
1345 frame = &gspca_dev->frame[j];
1346 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1350 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1351 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1356 if (gspca_dev->sd_desc->dq_callback) {
1357 mutex_lock(&gspca_dev->usb_lock);
1358 gspca_dev->sd_desc->dq_callback(gspca_dev);
1359 mutex_unlock(&gspca_dev->usb_lock);
1365 * dequeue a video buffer
1367 * If nonblock_ing is false, block until a buffer is available.
1369 static int vidioc_dqbuf(struct file *file, void *priv,
1370 struct v4l2_buffer *v4l2_buf)
1372 struct gspca_dev *gspca_dev = priv;
1373 struct gspca_frame *frame;
1376 PDEBUG(D_FRAM, "dqbuf");
1377 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1379 if (v4l2_buf->memory != gspca_dev->memory)
1382 /* if not streaming, be sure the application will not loop forever */
1383 if (!(file->f_flags & O_NONBLOCK)
1384 && !gspca_dev->streaming && gspca_dev->users == 1)
1387 /* only the capturing file may dequeue */
1388 if (gspca_dev->capt_file != file)
1391 /* only one dequeue / read at a time */
1392 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1393 return -ERESTARTSYS;
1395 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1398 i = ret; /* frame index */
1399 frame = &gspca_dev->frame[i];
1400 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1401 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1403 frame->v4l2_buf.bytesused)) {
1404 PDEBUG(D_ERR|D_STREAM,
1405 "dqbuf cp to user failed");
1410 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1411 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1412 PDEBUG(D_FRAM, "dqbuf %d", i);
1415 mutex_unlock(&gspca_dev->read_lock);
1420 * queue a video buffer
1422 * Attempting to queue a buffer that has already been
1423 * queued will return -EINVAL.
1425 static int vidioc_qbuf(struct file *file, void *priv,
1426 struct v4l2_buffer *v4l2_buf)
1428 struct gspca_dev *gspca_dev = priv;
1429 struct gspca_frame *frame;
1432 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1433 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1436 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1437 return -ERESTARTSYS;
1439 index = v4l2_buf->index;
1440 if ((unsigned) index >= gspca_dev->nframes) {
1442 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1446 if (v4l2_buf->memory != gspca_dev->memory) {
1447 PDEBUG(D_FRAM, "qbuf bad memory type");
1452 frame = &gspca_dev->frame[index];
1453 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1454 PDEBUG(D_FRAM, "qbuf bad state");
1459 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1460 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1462 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1463 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1464 frame->v4l2_buf.length = v4l2_buf->length;
1467 /* put the buffer in the 'queued' queue */
1468 i = gspca_dev->fr_q;
1469 gspca_dev->fr_queue[i] = index;
1470 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1471 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1476 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1477 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1480 mutex_unlock(&gspca_dev->queue_lock);
1485 * allocate the resources for read()
1487 static int read_alloc(struct gspca_dev *gspca_dev,
1490 struct v4l2_buffer v4l2_buf;
1493 PDEBUG(D_STREAM, "read alloc");
1494 if (gspca_dev->nframes == 0) {
1495 struct v4l2_requestbuffers rb;
1497 memset(&rb, 0, sizeof rb);
1498 rb.count = gspca_dev->nbufread;
1499 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1500 rb.memory = GSPCA_MEMORY_READ;
1501 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1503 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1506 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1507 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1508 v4l2_buf.memory = GSPCA_MEMORY_READ;
1509 for (i = 0; i < gspca_dev->nbufread; i++) {
1511 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1513 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1517 gspca_dev->memory = GSPCA_MEMORY_READ;
1520 /* start streaming */
1521 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1523 PDEBUG(D_STREAM, "read streamon err %d", ret);
1527 static unsigned int dev_poll(struct file *file, poll_table *wait)
1529 struct gspca_dev *gspca_dev = file->private_data;
1532 PDEBUG(D_FRAM, "poll");
1534 poll_wait(file, &gspca_dev->wq, wait);
1535 if (!gspca_dev->present)
1538 /* if reqbufs is not done, the user would use read() */
1539 if (gspca_dev->nframes == 0) {
1540 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1541 return POLLERR; /* not the 1st time */
1542 ret = read_alloc(gspca_dev, file);
1547 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1549 if (!gspca_dev->present) {
1554 /* check the next incoming buffer */
1555 i = gspca_dev->fr_o;
1556 i = gspca_dev->fr_queue[i];
1557 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1558 ret = POLLIN | POLLRDNORM; /* something to read */
1562 mutex_unlock(&gspca_dev->queue_lock);
1566 static ssize_t dev_read(struct file *file, char __user *data,
1567 size_t count, loff_t *ppos)
1569 struct gspca_dev *gspca_dev = file->private_data;
1570 struct gspca_frame *frame;
1571 struct v4l2_buffer v4l2_buf;
1572 struct timeval timestamp;
1575 PDEBUG(D_FRAM, "read (%zd)", count);
1576 if (!gspca_dev->present)
1578 switch (gspca_dev->memory) {
1579 case GSPCA_MEMORY_NO: /* first time */
1580 ret = read_alloc(gspca_dev, file);
1584 case GSPCA_MEMORY_READ:
1585 if (gspca_dev->capt_file == file)
1593 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1597 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1598 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1599 v4l2_buf.memory = GSPCA_MEMORY_READ;
1600 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1602 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1606 /* if the process slept for more than 1 second,
1607 * get anewer frame */
1608 frame = &gspca_dev->frame[v4l2_buf.index];
1610 break; /* avoid infinite loop */
1611 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1613 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1615 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1620 /* copy the frame */
1621 if (count > frame->v4l2_buf.bytesused)
1622 count = frame->v4l2_buf.bytesused;
1623 ret = copy_to_user(data, frame->data, count);
1625 PDEBUG(D_ERR|D_STREAM,
1626 "read cp to user lack %d / %zd", ret, count);
1632 /* in each case, requeue the buffer */
1633 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1639 static void dev_release(struct video_device *vfd)
1644 static struct file_operations dev_fops = {
1645 .owner = THIS_MODULE,
1647 .release = dev_close,
1650 .ioctl = video_ioctl2,
1651 #ifdef CONFIG_COMPAT
1652 .compat_ioctl = v4l_compat_ioctl32,
1654 .llseek = no_llseek,
1658 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1659 .vidioc_querycap = vidioc_querycap,
1660 .vidioc_dqbuf = vidioc_dqbuf,
1661 .vidioc_qbuf = vidioc_qbuf,
1662 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1663 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1664 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1665 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1666 .vidioc_streamon = vidioc_streamon,
1667 .vidioc_queryctrl = vidioc_queryctrl,
1668 .vidioc_g_ctrl = vidioc_g_ctrl,
1669 .vidioc_s_ctrl = vidioc_s_ctrl,
1670 .vidioc_querymenu = vidioc_querymenu,
1671 .vidioc_enum_input = vidioc_enum_input,
1672 .vidioc_g_input = vidioc_g_input,
1673 .vidioc_s_input = vidioc_s_input,
1674 .vidioc_reqbufs = vidioc_reqbufs,
1675 .vidioc_querybuf = vidioc_querybuf,
1676 .vidioc_streamoff = vidioc_streamoff,
1677 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1678 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1679 .vidioc_g_parm = vidioc_g_parm,
1680 .vidioc_s_parm = vidioc_s_parm,
1681 .vidioc_s_std = vidioc_s_std,
1682 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1683 .vidiocgmbuf = vidiocgmbuf,
1687 static struct video_device gspca_template = {
1688 .name = "gspca main driver",
1690 .ioctl_ops = &dev_ioctl_ops,
1691 .release = dev_release, /* mandatory */
1696 * probe and create a new gspca device
1698 * This function must be called by the sub-driver when it is
1699 * called for probing a new device.
1701 int gspca_dev_probe(struct usb_interface *intf,
1702 const struct usb_device_id *id,
1703 const struct sd_desc *sd_desc,
1705 struct module *module)
1707 struct usb_interface_descriptor *interface;
1708 struct gspca_dev *gspca_dev;
1709 struct usb_device *dev = interface_to_usbdev(intf);
1712 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1714 /* we don't handle multi-config cameras */
1715 if (dev->descriptor.bNumConfigurations != 1)
1717 interface = &intf->cur_altsetting->desc;
1718 if (interface->bInterfaceNumber > 0)
1721 /* create the device */
1722 if (dev_size < sizeof *gspca_dev)
1723 dev_size = sizeof *gspca_dev;
1724 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1725 if (gspca_dev == NULL) {
1726 err("couldn't kzalloc gspca struct");
1729 gspca_dev->dev = dev;
1730 gspca_dev->iface = interface->bInterfaceNumber;
1731 gspca_dev->nbalt = intf->num_altsetting;
1732 gspca_dev->sd_desc = sd_desc;
1733 /* gspca_dev->users = 0; (done by kzalloc) */
1734 gspca_dev->nbufread = 2;
1736 /* configure the subdriver and initialize the USB device */
1737 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1740 ret = gspca_dev->sd_desc->init(gspca_dev);
1743 ret = gspca_set_alt0(gspca_dev);
1746 gspca_set_default_mode(gspca_dev);
1748 mutex_init(&gspca_dev->usb_lock);
1749 mutex_init(&gspca_dev->read_lock);
1750 mutex_init(&gspca_dev->queue_lock);
1751 init_waitqueue_head(&gspca_dev->wq);
1753 /* init video stuff */
1754 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1755 gspca_dev->vdev.parent = &dev->dev;
1756 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1757 gspca_dev->vdev.fops = &gspca_dev->fops;
1758 gspca_dev->fops.owner = module; /* module protection */
1759 gspca_dev->present = 1;
1760 ret = video_register_device(&gspca_dev->vdev,
1764 err("video_register_device err %d", ret);
1768 usb_set_intfdata(intf, gspca_dev);
1769 PDEBUG(D_PROBE, "probe ok");
1775 EXPORT_SYMBOL(gspca_dev_probe);
1780 * This function must be called by the sub-driver
1781 * when the device disconnects, after the specific resources are freed.
1783 void gspca_disconnect(struct usb_interface *intf)
1785 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1789 gspca_dev->present = 0;
1790 mutex_lock(&gspca_dev->queue_lock);
1791 mutex_lock(&gspca_dev->usb_lock);
1792 gspca_dev->streaming = 0;
1793 destroy_urbs(gspca_dev);
1794 mutex_unlock(&gspca_dev->usb_lock);
1795 mutex_unlock(&gspca_dev->queue_lock);
1796 while (gspca_dev->users != 0) { /* wait until fully closed */
1797 atomic_inc(&gspca_dev->nevent);
1798 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1801 /* We don't want people trying to open up the device */
1802 video_unregister_device(&gspca_dev->vdev);
1803 /* Free the memory */
1805 PDEBUG(D_PROBE, "disconnect complete");
1807 EXPORT_SYMBOL(gspca_disconnect);
1810 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1812 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1814 if (!gspca_dev->streaming)
1816 gspca_dev->frozen = 1; /* avoid urb error messages */
1817 if (gspca_dev->sd_desc->stopN)
1818 gspca_dev->sd_desc->stopN(gspca_dev);
1819 destroy_urbs(gspca_dev);
1820 gspca_set_alt0(gspca_dev);
1821 if (gspca_dev->sd_desc->stop0)
1822 gspca_dev->sd_desc->stop0(gspca_dev);
1825 EXPORT_SYMBOL(gspca_suspend);
1827 int gspca_resume(struct usb_interface *intf)
1829 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1831 gspca_dev->frozen = 0;
1832 gspca_dev->sd_desc->init(gspca_dev);
1833 if (gspca_dev->streaming)
1834 return gspca_init_transfer(gspca_dev);
1837 EXPORT_SYMBOL(gspca_resume);
1839 /* -- cam driver utility functions -- */
1841 /* auto gain and exposure algorithm based on the knee algorithm described here:
1842 http://ytse.tricolour.net/docs/LowLightOptimization.html
1844 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1846 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1847 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1849 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1850 const struct ctrl *gain_ctrl = NULL;
1851 const struct ctrl *exposure_ctrl = NULL;
1852 const struct ctrl *autogain_ctrl = NULL;
1855 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1856 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1857 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1858 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1859 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1860 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1861 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1863 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1864 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1865 "on cam without (auto)gain/exposure");
1869 if (gain_ctrl->get(gspca_dev, &gain) ||
1870 exposure_ctrl->get(gspca_dev, &exposure) ||
1871 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1875 orig_exposure = exposure;
1877 /* If we are of a multiple of deadzone, do multiple steps to reach the
1878 desired lumination fast (with the risc of a slight overshoot) */
1879 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1881 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1882 avg_lum, desired_avg_lum, steps);
1884 for (i = 0; i < steps; i++) {
1885 if (avg_lum > desired_avg_lum) {
1886 if (gain > gain_knee)
1888 else if (exposure > exposure_knee)
1890 else if (gain > gain_ctrl->qctrl.default_value)
1892 else if (exposure > exposure_ctrl->qctrl.minimum)
1894 else if (gain > gain_ctrl->qctrl.minimum)
1899 if (gain < gain_ctrl->qctrl.default_value)
1901 else if (exposure < exposure_knee)
1903 else if (gain < gain_knee)
1905 else if (exposure < exposure_ctrl->qctrl.maximum)
1907 else if (gain < gain_ctrl->qctrl.maximum)
1914 if (gain != orig_gain) {
1915 gain_ctrl->set(gspca_dev, gain);
1918 if (exposure != orig_exposure) {
1919 exposure_ctrl->set(gspca_dev, exposure);
1925 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
1927 /* -- module insert / remove -- */
1928 static int __init gspca_init(void)
1930 info("main v%d.%d.%d registered",
1931 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
1932 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
1933 DRIVER_VERSION_NUMBER & 0xff);
1936 static void __exit gspca_exit(void)
1938 info("main deregistered");
1941 module_init(gspca_init);
1942 module_exit(gspca_exit);
1945 module_param_named(debug, gspca_debug, int, 0644);
1946 MODULE_PARM_DESC(debug,
1947 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
1948 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"