2 * Main USB camera driver
4 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define MODULE_NAME "gspca"
28 #include <linux/init.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
38 #include <linux/uaccess.h>
39 #include <linux/ktime.h>
40 #include <media/v4l2-ioctl.h>
44 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
45 #include <linux/input.h>
46 #include <linux/usb/input.h>
50 #define DEF_NURBS 3 /* default number of URBs */
51 #if DEF_NURBS > MAX_NURBS
52 #error "DEF_NURBS too big"
55 #define DRIVER_VERSION_NUMBER "2.13.0"
57 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
58 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(DRIVER_VERSION_NUMBER);
63 int gspca_debug = D_ERR | D_PROBE;
64 EXPORT_SYMBOL(gspca_debug);
66 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
68 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
69 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
73 (pixfmt >> 16) & 0xff,
77 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
84 #define PDEBUG_MODE(txt, pixfmt, w, h)
87 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
88 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
89 #define GSPCA_MEMORY_READ 7
91 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
96 static void gspca_vm_open(struct vm_area_struct *vma)
98 struct gspca_frame *frame = vma->vm_private_data;
100 frame->vma_use_count++;
101 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
104 static void gspca_vm_close(struct vm_area_struct *vma)
106 struct gspca_frame *frame = vma->vm_private_data;
108 if (--frame->vma_use_count <= 0)
109 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
112 static const struct vm_operations_struct gspca_vm_ops = {
113 .open = gspca_vm_open,
114 .close = gspca_vm_close,
118 * Input and interrupt endpoint handling functions
120 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
121 static void int_irq(struct urb *urb)
123 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
129 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
130 urb->transfer_buffer, urb->actual_length) < 0) {
131 PDEBUG(D_ERR, "Unknown packet received");
139 /* Stop is requested either by software or hardware is gone,
140 * keep the ret value non-zero and don't resubmit later.
145 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
151 ret = usb_submit_urb(urb, GFP_ATOMIC);
153 pr_err("Resubmit URB failed with error %i\n", ret);
157 static int gspca_input_connect(struct gspca_dev *dev)
159 struct input_dev *input_dev;
162 dev->input_dev = NULL;
163 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
164 input_dev = input_allocate_device();
168 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
169 strlcat(dev->phys, "/input0", sizeof(dev->phys));
171 input_dev->name = dev->sd_desc->name;
172 input_dev->phys = dev->phys;
174 usb_to_input_id(dev->dev, &input_dev->id);
176 input_dev->evbit[0] = BIT_MASK(EV_KEY);
177 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
178 input_dev->dev.parent = &dev->dev->dev;
180 err = input_register_device(input_dev);
182 pr_err("Input device registration failed with error %i\n",
184 input_dev->dev.parent = NULL;
185 input_free_device(input_dev);
187 dev->input_dev = input_dev;
194 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
195 struct usb_endpoint_descriptor *ep)
197 unsigned int buffer_len;
200 struct usb_device *dev;
204 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
205 interval = ep->bInterval;
206 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
207 "buffer_len=%u, interval=%u",
208 ep->bEndpointAddress, buffer_len, interval);
210 dev = gspca_dev->dev;
212 urb = usb_alloc_urb(0, GFP_KERNEL);
218 buffer = usb_alloc_coherent(dev, buffer_len,
219 GFP_KERNEL, &urb->transfer_dma);
224 usb_fill_int_urb(urb, dev,
225 usb_rcvintpipe(dev, ep->bEndpointAddress),
227 int_irq, (void *)gspca_dev, interval);
228 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
229 ret = usb_submit_urb(urb, GFP_KERNEL);
231 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
234 gspca_dev->int_urb = urb;
238 usb_free_coherent(dev,
239 urb->transfer_buffer_length,
240 urb->transfer_buffer,
248 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
250 struct usb_interface *intf;
251 struct usb_host_interface *intf_desc;
252 struct usb_endpoint_descriptor *ep;
255 if (gspca_dev->sd_desc->int_pkt_scan) {
256 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
257 intf_desc = intf->cur_altsetting;
258 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
259 ep = &intf_desc->endpoint[i].desc;
260 if (usb_endpoint_dir_in(ep) &&
261 usb_endpoint_xfer_int(ep)) {
263 alloc_and_submit_int_urb(gspca_dev, ep);
270 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
274 urb = gspca_dev->int_urb;
276 gspca_dev->int_urb = NULL;
278 usb_free_coherent(gspca_dev->dev,
279 urb->transfer_buffer_length,
280 urb->transfer_buffer,
286 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
290 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
294 static inline int gspca_input_connect(struct gspca_dev *dev)
301 * fill a video frame from an URB and resubmit
303 static void fill_frame(struct gspca_dev *gspca_dev,
306 u8 *data; /* address of data in the iso message */
310 if (urb->status != 0) {
311 if (urb->status == -ESHUTDOWN)
312 return; /* disconnection */
314 if (gspca_dev->frozen)
317 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
321 pkt_scan = gspca_dev->sd_desc->pkt_scan;
322 for (i = 0; i < urb->number_of_packets; i++) {
323 len = urb->iso_frame_desc[i].actual_length;
325 /* check the packet status and length */
326 st = urb->iso_frame_desc[i].status;
328 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
330 gspca_dev->last_packet_type = DISCARD_PACKET;
334 if (gspca_dev->empty_packet == 0)
335 gspca_dev->empty_packet = 1;
339 /* let the packet be analyzed by the subdriver */
340 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
341 i, urb->iso_frame_desc[i].offset, len);
342 data = (u8 *) urb->transfer_buffer
343 + urb->iso_frame_desc[i].offset;
344 pkt_scan(gspca_dev, data, len);
348 /* resubmit the URB */
349 st = usb_submit_urb(urb, GFP_ATOMIC);
351 pr_err("usb_submit_urb() ret %d\n", st);
355 * ISOC message interrupt from the USB device
357 * Analyse each packet and call the subdriver for copy to the frame buffer.
359 static void isoc_irq(struct urb *urb)
361 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
363 PDEBUG(D_PACK, "isoc irq");
364 if (!gspca_dev->streaming)
366 fill_frame(gspca_dev, urb);
370 * bulk message interrupt from the USB device
372 static void bulk_irq(struct urb *urb)
374 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
377 PDEBUG(D_PACK, "bulk irq");
378 if (!gspca_dev->streaming)
380 switch (urb->status) {
384 return; /* disconnection */
387 if (gspca_dev->frozen)
390 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
395 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
396 gspca_dev->sd_desc->pkt_scan(gspca_dev,
397 urb->transfer_buffer,
401 /* resubmit the URB */
402 if (gspca_dev->cam.bulk_nurbs != 0) {
403 st = usb_submit_urb(urb, GFP_ATOMIC);
405 pr_err("usb_submit_urb() ret %d\n", st);
410 * add data to the current frame
412 * This function is called by the subdrivers at interrupt level.
414 * To build a frame, these ones must add
416 * - 0 or many INTER_PACKETs
418 * DISCARD_PACKET invalidates the whole frame.
420 void gspca_frame_add(struct gspca_dev *gspca_dev,
421 enum gspca_packet_type packet_type,
425 struct gspca_frame *frame;
428 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
430 if (packet_type == FIRST_PACKET) {
431 i = atomic_read(&gspca_dev->fr_i);
433 /* if there are no queued buffer, discard the whole frame */
434 if (i == atomic_read(&gspca_dev->fr_q)) {
435 gspca_dev->last_packet_type = DISCARD_PACKET;
436 gspca_dev->sequence++;
439 j = gspca_dev->fr_queue[i];
440 frame = &gspca_dev->frame[j];
441 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
442 frame->v4l2_buf.sequence = gspca_dev->sequence++;
443 gspca_dev->image = frame->data;
444 gspca_dev->image_len = 0;
446 switch (gspca_dev->last_packet_type) {
448 if (packet_type == LAST_PACKET) {
449 gspca_dev->last_packet_type = packet_type;
450 gspca_dev->image = NULL;
451 gspca_dev->image_len = 0;
459 /* append the packet to the frame buffer */
461 if (gspca_dev->image_len + len > gspca_dev->frsz) {
462 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
463 gspca_dev->image_len + len,
465 packet_type = DISCARD_PACKET;
467 /* !! image is NULL only when last pkt is LAST or DISCARD
468 if (gspca_dev->image == NULL) {
469 pr_err("gspca_frame_add() image == NULL\n");
473 memcpy(gspca_dev->image + gspca_dev->image_len,
475 gspca_dev->image_len += len;
478 gspca_dev->last_packet_type = packet_type;
480 /* if last packet, invalidate packet concatenation until
481 * next first packet, wake up the application and advance
483 if (packet_type == LAST_PACKET) {
484 i = atomic_read(&gspca_dev->fr_i);
485 j = gspca_dev->fr_queue[i];
486 frame = &gspca_dev->frame[j];
487 frame->v4l2_buf.bytesused = gspca_dev->image_len;
488 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
489 | V4L2_BUF_FLAG_DONE)
490 & ~V4L2_BUF_FLAG_QUEUED;
491 i = (i + 1) % GSPCA_MAX_FRAMES;
492 atomic_set(&gspca_dev->fr_i, i);
493 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
494 PDEBUG(D_FRAM, "frame complete len:%d",
495 frame->v4l2_buf.bytesused);
496 gspca_dev->image = NULL;
497 gspca_dev->image_len = 0;
500 EXPORT_SYMBOL(gspca_frame_add);
502 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
503 enum v4l2_memory memory, unsigned int count)
505 struct gspca_frame *frame;
509 i = gspca_dev->curr_mode;
510 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
511 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
512 frsz = PAGE_ALIGN(frsz);
513 if (count >= GSPCA_MAX_FRAMES)
514 count = GSPCA_MAX_FRAMES - 1;
515 gspca_dev->frbuf = vmalloc_32(frsz * count);
516 if (!gspca_dev->frbuf) {
517 pr_err("frame alloc failed\n");
520 gspca_dev->capt_file = file;
521 gspca_dev->memory = memory;
522 gspca_dev->frsz = frsz;
523 gspca_dev->nframes = count;
524 for (i = 0; i < count; i++) {
525 frame = &gspca_dev->frame[i];
526 frame->v4l2_buf.index = i;
527 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
528 frame->v4l2_buf.flags = 0;
529 frame->v4l2_buf.field = V4L2_FIELD_NONE;
530 frame->v4l2_buf.length = frsz;
531 frame->v4l2_buf.memory = memory;
532 frame->v4l2_buf.sequence = 0;
533 frame->data = gspca_dev->frbuf + i * frsz;
534 frame->v4l2_buf.m.offset = i * frsz;
536 atomic_set(&gspca_dev->fr_q, 0);
537 atomic_set(&gspca_dev->fr_i, 0);
542 static void frame_free(struct gspca_dev *gspca_dev)
546 PDEBUG(D_STREAM, "frame free");
547 if (gspca_dev->frbuf != NULL) {
548 vfree(gspca_dev->frbuf);
549 gspca_dev->frbuf = NULL;
550 for (i = 0; i < gspca_dev->nframes; i++)
551 gspca_dev->frame[i].data = NULL;
553 gspca_dev->nframes = 0;
555 gspca_dev->capt_file = NULL;
556 gspca_dev->memory = GSPCA_MEMORY_NO;
559 static void destroy_urbs(struct gspca_dev *gspca_dev)
564 PDEBUG(D_STREAM, "kill transfer");
565 for (i = 0; i < MAX_NURBS; i++) {
566 urb = gspca_dev->urb[i];
570 gspca_dev->urb[i] = NULL;
572 if (urb->transfer_buffer != NULL)
573 usb_free_coherent(gspca_dev->dev,
574 urb->transfer_buffer_length,
575 urb->transfer_buffer,
581 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
585 if (gspca_dev->alt == 0)
587 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
589 pr_err("set alt 0 err %d\n", ret);
593 /* Note: both the queue and the usb locks should be held when calling this */
594 static void gspca_stream_off(struct gspca_dev *gspca_dev)
596 gspca_dev->streaming = 0;
597 if (gspca_dev->present) {
598 if (gspca_dev->sd_desc->stopN)
599 gspca_dev->sd_desc->stopN(gspca_dev);
600 destroy_urbs(gspca_dev);
601 gspca_input_destroy_urb(gspca_dev);
602 gspca_set_alt0(gspca_dev);
603 gspca_input_create_urb(gspca_dev);
606 /* always call stop0 to free the subdriver's resources */
607 if (gspca_dev->sd_desc->stop0)
608 gspca_dev->sd_desc->stop0(gspca_dev);
609 PDEBUG(D_STREAM, "stream off OK");
613 * look for an input transfer endpoint in an alternate setting
615 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
618 struct usb_host_endpoint *ep;
621 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
622 ep = &alt->endpoint[i];
623 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
625 && ep->desc.wMaxPacketSize != 0
626 && usb_endpoint_dir_in(&ep->desc))
632 /* compute the minimum bandwidth for the current transfer */
633 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
638 i = gspca_dev->curr_mode;
639 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
641 /* if the image is compressed, estimate the mean image size */
642 if (bandwidth < gspca_dev->cam.cam_mode[i].width *
643 gspca_dev->cam.cam_mode[i].height)
646 /* estimate the frame rate */
647 if (gspca_dev->sd_desc->get_streamparm) {
648 struct v4l2_streamparm parm;
650 parm.parm.capture.timeperframe.denominator = 15;
651 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
652 bandwidth *= parm.parm.capture.timeperframe.denominator;
654 bandwidth *= 15; /* 15 fps */
657 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
669 * build the table of the endpoints
670 * and compute the minimum bandwidth for the image transfer
672 static int build_ep_tb(struct gspca_dev *gspca_dev,
673 struct usb_interface *intf,
675 struct ep_tb_s *ep_tb)
677 struct usb_host_endpoint *ep;
678 int i, j, nbalt, psize, found;
679 u32 bandwidth, last_bw;
681 nbalt = intf->num_altsetting;
683 nbalt = MAX_ALT; /* fixme: should warn */
685 /* build the endpoint table */
689 ep_tb->bandwidth = 2000 * 2000 * 120;
691 for (j = 0; j < nbalt; j++) {
692 ep = alt_xfer(&intf->altsetting[j], xfer);
695 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
696 if (!gspca_dev->cam.bulk) /* isoc */
697 psize = (psize & 0x07ff) *
698 (1 + ((psize >> 11) & 3));
699 bandwidth = psize * ep->desc.bInterval * 1000;
700 if (gspca_dev->dev->speed == USB_SPEED_HIGH
701 || gspca_dev->dev->speed == USB_SPEED_SUPER)
703 if (bandwidth <= last_bw)
705 if (bandwidth < ep_tb->bandwidth) {
706 ep_tb->bandwidth = bandwidth;
713 PDEBUG(D_STREAM, "alt %d bandwidth %d",
714 ep_tb->alt, ep_tb->bandwidth);
715 last_bw = ep_tb->bandwidth;
720 /* get the requested bandwidth and start at the highest atlsetting */
721 bandwidth = which_bandwidth(gspca_dev);
725 if (ep_tb->bandwidth < bandwidth)
733 * create the URBs for image transfer
735 static int create_urbs(struct gspca_dev *gspca_dev,
736 struct usb_host_endpoint *ep)
739 int n, nurbs, i, psize, npkt, bsize;
741 /* calculate the packet size and the number of packets */
742 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
744 if (!gspca_dev->cam.bulk) { /* isoc */
746 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
747 if (gspca_dev->pkt_size == 0)
748 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
750 psize = gspca_dev->pkt_size;
751 npkt = gspca_dev->cam.npkt;
753 npkt = 32; /* default value */
754 bsize = psize * npkt;
756 "isoc %d pkts size %d = bsize:%d",
761 bsize = gspca_dev->cam.bulk_size;
764 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
765 if (gspca_dev->cam.bulk_nurbs != 0)
766 nurbs = gspca_dev->cam.bulk_nurbs;
771 for (n = 0; n < nurbs; n++) {
772 urb = usb_alloc_urb(npkt, GFP_KERNEL);
774 pr_err("usb_alloc_urb failed\n");
777 gspca_dev->urb[n] = urb;
778 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
783 if (urb->transfer_buffer == NULL) {
784 pr_err("usb_alloc_coherent failed\n");
787 urb->dev = gspca_dev->dev;
788 urb->context = gspca_dev;
789 urb->transfer_buffer_length = bsize;
790 if (npkt != 0) { /* ISOC */
791 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
792 ep->desc.bEndpointAddress);
793 urb->transfer_flags = URB_ISO_ASAP
794 | URB_NO_TRANSFER_DMA_MAP;
795 if (gspca_dev->dev->speed == USB_SPEED_LOW)
796 urb->interval = ep->desc.bInterval;
798 urb->interval = 1 << (ep->desc.bInterval - 1);
799 urb->complete = isoc_irq;
800 urb->number_of_packets = npkt;
801 for (i = 0; i < npkt; i++) {
802 urb->iso_frame_desc[i].length = psize;
803 urb->iso_frame_desc[i].offset = psize * i;
806 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
807 ep->desc.bEndpointAddress);
808 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
809 urb->complete = bulk_irq;
816 * start the USB transfer
818 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
820 struct usb_interface *intf;
821 struct usb_host_endpoint *ep;
823 struct ep_tb_s ep_tb[MAX_ALT];
824 int n, ret, xfer, alt, alt_idx;
826 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
829 if (!gspca_dev->present) {
834 /* reset the streaming variables */
835 gspca_dev->image = NULL;
836 gspca_dev->image_len = 0;
837 gspca_dev->last_packet_type = DISCARD_PACKET;
838 gspca_dev->sequence = 0;
840 gspca_dev->usb_err = 0;
842 /* do the specific subdriver stuff before endpoint selection */
844 if (gspca_dev->sd_desc->isoc_init) {
845 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
849 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
850 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
851 : USB_ENDPOINT_XFER_ISOC;
853 /* if the subdriver forced an altsetting, get the endpoint */
854 if (gspca_dev->alt != 0) {
855 gspca_dev->alt--; /* (previous version compatibility) */
856 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
858 pr_err("bad altsetting %d\n", gspca_dev->alt);
862 ep_tb[0].alt = gspca_dev->alt;
866 /* else, compute the minimum bandwidth
867 * and build the endpoint table */
868 alt_idx = build_ep_tb(gspca_dev, intf, xfer, ep_tb);
870 pr_err("no transfer endpoint found\n");
876 /* set the highest alternate setting and
877 * loop until urb submit succeeds */
878 gspca_input_destroy_urb(gspca_dev);
880 gspca_dev->alt = ep_tb[--alt_idx].alt;
883 if (alt != gspca_dev->alt) {
884 alt = gspca_dev->alt;
885 if (gspca_dev->nbalt > 1) {
886 ret = usb_set_interface(gspca_dev->dev,
890 pr_err("set alt %d err %d\n", alt, ret);
895 if (!gspca_dev->cam.no_urb_create) {
896 PDEBUG(D_STREAM, "init transfer alt %d", alt);
897 ret = create_urbs(gspca_dev,
898 alt_xfer(&intf->altsetting[alt], xfer));
900 destroy_urbs(gspca_dev);
905 /* clear the bulk endpoint */
906 if (gspca_dev->cam.bulk)
907 usb_clear_halt(gspca_dev->dev,
908 gspca_dev->urb[0]->pipe);
911 ret = gspca_dev->sd_desc->start(gspca_dev);
913 destroy_urbs(gspca_dev);
916 gspca_dev->streaming = 1;
918 /* some bulk transfers are started by the subdriver */
919 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
922 /* submit the URBs */
923 for (n = 0; n < MAX_NURBS; n++) {
924 urb = gspca_dev->urb[n];
927 ret = usb_submit_urb(urb, GFP_KERNEL);
932 break; /* transfer is started */
934 /* something when wrong
935 * stop the webcam and free the transfer resources */
936 gspca_stream_off(gspca_dev);
937 if (ret != -ENOSPC) {
938 pr_err("usb_submit_urb alt %d err %d\n",
939 gspca_dev->alt, ret);
943 /* the bandwidth is not wide enough
944 * negotiate or try a lower alternate setting */
945 PDEBUG(D_ERR|D_STREAM,
946 "alt %d - bandwidth not wide enough - trying again",
948 msleep(20); /* wait for kill complete */
949 if (gspca_dev->sd_desc->isoc_nego) {
950 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
955 pr_err("no transfer endpoint found\n");
959 alt = ep_tb[--alt_idx].alt;
963 gspca_input_create_urb(gspca_dev);
965 mutex_unlock(&gspca_dev->usb_lock);
969 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
971 struct gspca_ctrl *ctrl;
974 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
975 gspca_dev->curr_mode = i;
976 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
977 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
978 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
980 /* set the current control values to their default values
981 * which may have changed in sd_init() */
982 ctrl = gspca_dev->cam.ctrls;
985 i < gspca_dev->sd_desc->nctrls;
987 ctrl->val = ctrl->def;
991 static int wxh_to_mode(struct gspca_dev *gspca_dev,
992 int width, int height)
996 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
997 if (width >= gspca_dev->cam.cam_mode[i].width
998 && height >= gspca_dev->cam.cam_mode[i].height)
1005 * search a mode with the right pixel format
1007 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1013 modeU = modeD = mode;
1014 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1016 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1020 if (++modeU < gspca_dev->cam.nmodes) {
1021 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1029 #ifdef CONFIG_VIDEO_ADV_DEBUG
1030 static int vidioc_g_register(struct file *file, void *priv,
1031 struct v4l2_dbg_register *reg)
1034 struct gspca_dev *gspca_dev = priv;
1036 if (!gspca_dev->sd_desc->get_chip_ident)
1039 if (!gspca_dev->sd_desc->get_register)
1042 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1043 return -ERESTARTSYS;
1044 gspca_dev->usb_err = 0;
1045 if (gspca_dev->present)
1046 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
1049 mutex_unlock(&gspca_dev->usb_lock);
1054 static int vidioc_s_register(struct file *file, void *priv,
1055 struct v4l2_dbg_register *reg)
1058 struct gspca_dev *gspca_dev = priv;
1060 if (!gspca_dev->sd_desc->get_chip_ident)
1063 if (!gspca_dev->sd_desc->set_register)
1066 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1067 return -ERESTARTSYS;
1068 gspca_dev->usb_err = 0;
1069 if (gspca_dev->present)
1070 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
1073 mutex_unlock(&gspca_dev->usb_lock);
1079 static int vidioc_g_chip_ident(struct file *file, void *priv,
1080 struct v4l2_dbg_chip_ident *chip)
1083 struct gspca_dev *gspca_dev = priv;
1085 if (!gspca_dev->sd_desc->get_chip_ident)
1088 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1089 return -ERESTARTSYS;
1090 gspca_dev->usb_err = 0;
1091 if (gspca_dev->present)
1092 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1095 mutex_unlock(&gspca_dev->usb_lock);
1100 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1101 struct v4l2_fmtdesc *fmtdesc)
1103 struct gspca_dev *gspca_dev = priv;
1107 /* give an index to each format */
1110 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1111 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1114 if (fmt_tb[j] == fmt_tb[index])
1119 if (fmtdesc->index == index)
1120 break; /* new format */
1122 if (index >= ARRAY_SIZE(fmt_tb))
1127 return -EINVAL; /* no more format */
1129 fmtdesc->pixelformat = fmt_tb[index];
1130 if (gspca_dev->cam.cam_mode[i].sizeimage <
1131 gspca_dev->cam.cam_mode[i].width *
1132 gspca_dev->cam.cam_mode[i].height)
1133 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1134 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1135 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1136 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1137 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1138 fmtdesc->description[4] = '\0';
1142 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1143 struct v4l2_format *fmt)
1145 struct gspca_dev *gspca_dev = priv;
1148 mode = gspca_dev->curr_mode;
1149 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1150 sizeof fmt->fmt.pix);
1154 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1155 struct v4l2_format *fmt)
1157 int w, h, mode, mode2;
1159 w = fmt->fmt.pix.width;
1160 h = fmt->fmt.pix.height;
1163 if (gspca_debug & D_CONF)
1164 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1166 /* search the closest mode for width and height */
1167 mode = wxh_to_mode(gspca_dev, w, h);
1169 /* OK if right palette */
1170 if (gspca_dev->cam.cam_mode[mode].pixelformat
1171 != fmt->fmt.pix.pixelformat) {
1173 /* else, search the closest mode with the same pixel format */
1174 mode2 = gspca_get_mode(gspca_dev, mode,
1175 fmt->fmt.pix.pixelformat);
1179 ; * no chance, return this mode */
1181 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1182 sizeof fmt->fmt.pix);
1183 return mode; /* used when s_fmt */
1186 static int vidioc_try_fmt_vid_cap(struct file *file,
1188 struct v4l2_format *fmt)
1190 struct gspca_dev *gspca_dev = priv;
1193 ret = try_fmt_vid_cap(gspca_dev, fmt);
1199 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1200 struct v4l2_format *fmt)
1202 struct gspca_dev *gspca_dev = priv;
1205 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1206 return -ERESTARTSYS;
1208 ret = try_fmt_vid_cap(gspca_dev, fmt);
1212 if (gspca_dev->nframes != 0
1213 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1218 if (ret == gspca_dev->curr_mode) {
1220 goto out; /* same mode */
1223 if (gspca_dev->streaming) {
1227 gspca_dev->width = fmt->fmt.pix.width;
1228 gspca_dev->height = fmt->fmt.pix.height;
1229 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1230 gspca_dev->curr_mode = ret;
1234 mutex_unlock(&gspca_dev->queue_lock);
1238 static int vidioc_enum_framesizes(struct file *file, void *priv,
1239 struct v4l2_frmsizeenum *fsize)
1241 struct gspca_dev *gspca_dev = priv;
1245 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1246 if (fsize->pixel_format !=
1247 gspca_dev->cam.cam_mode[i].pixelformat)
1250 if (fsize->index == index) {
1251 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1252 fsize->discrete.width =
1253 gspca_dev->cam.cam_mode[i].width;
1254 fsize->discrete.height =
1255 gspca_dev->cam.cam_mode[i].height;
1264 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1265 struct v4l2_frmivalenum *fival)
1267 struct gspca_dev *gspca_dev = priv;
1268 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1271 if (gspca_dev->cam.mode_framerates == NULL ||
1272 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1275 if (fival->pixel_format !=
1276 gspca_dev->cam.cam_mode[mode].pixelformat)
1279 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1280 if (fival->index == i) {
1281 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1282 fival->discrete.numerator = 1;
1283 fival->discrete.denominator =
1284 gspca_dev->cam.mode_framerates[mode].rates[i];
1292 static void gspca_release(struct video_device *vfd)
1294 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1296 PDEBUG(D_PROBE, "%s released",
1297 video_device_node_name(&gspca_dev->vdev));
1299 kfree(gspca_dev->usb_buf);
1303 static int dev_open(struct file *file)
1305 struct gspca_dev *gspca_dev;
1307 PDEBUG(D_STREAM, "[%s] open", current->comm);
1308 gspca_dev = (struct gspca_dev *) video_devdata(file);
1309 if (!gspca_dev->present)
1312 /* protect the subdriver against rmmod */
1313 if (!try_module_get(gspca_dev->module))
1316 file->private_data = gspca_dev;
1318 /* activate the v4l2 debug */
1319 if (gspca_debug & D_V4L2)
1320 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1321 | V4L2_DEBUG_IOCTL_ARG;
1323 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1324 | V4L2_DEBUG_IOCTL_ARG);
1329 static int dev_close(struct file *file)
1331 struct gspca_dev *gspca_dev = file->private_data;
1333 PDEBUG(D_STREAM, "[%s] close", current->comm);
1334 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1335 return -ERESTARTSYS;
1337 /* if the file did the capture, free the streaming resources */
1338 if (gspca_dev->capt_file == file) {
1339 if (gspca_dev->streaming) {
1340 mutex_lock(&gspca_dev->usb_lock);
1341 gspca_dev->usb_err = 0;
1342 gspca_stream_off(gspca_dev);
1343 mutex_unlock(&gspca_dev->usb_lock);
1345 frame_free(gspca_dev);
1347 file->private_data = NULL;
1348 module_put(gspca_dev->module);
1349 mutex_unlock(&gspca_dev->queue_lock);
1351 PDEBUG(D_STREAM, "close done");
1356 static int vidioc_querycap(struct file *file, void *priv,
1357 struct v4l2_capability *cap)
1359 struct gspca_dev *gspca_dev = priv;
1362 /* protect the access to the usb device */
1363 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1364 return -ERESTARTSYS;
1365 if (!gspca_dev->present) {
1369 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1370 sizeof cap->driver);
1371 if (gspca_dev->dev->product != NULL) {
1372 strlcpy((char *) cap->card, gspca_dev->dev->product,
1375 snprintf((char *) cap->card, sizeof cap->card,
1376 "USB Camera (%04x:%04x)",
1377 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1378 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1380 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1381 sizeof(cap->bus_info));
1382 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1383 | V4L2_CAP_STREAMING
1384 | V4L2_CAP_READWRITE;
1387 mutex_unlock(&gspca_dev->usb_lock);
1391 static int get_ctrl(struct gspca_dev *gspca_dev,
1394 const struct ctrl *ctrls;
1397 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1398 i < gspca_dev->sd_desc->nctrls;
1400 if (gspca_dev->ctrl_dis & (1 << i))
1402 if (id == ctrls->qctrl.id)
1408 static int vidioc_queryctrl(struct file *file, void *priv,
1409 struct v4l2_queryctrl *q_ctrl)
1411 struct gspca_dev *gspca_dev = priv;
1412 const struct ctrl *ctrls;
1413 struct gspca_ctrl *gspca_ctrl;
1418 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1419 id &= V4L2_CTRL_ID_MASK;
1422 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1423 if (gspca_dev->ctrl_dis & (1 << i))
1425 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1428 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1429 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
1434 idx = get_ctrl(gspca_dev, id);
1438 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1439 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1440 if (gspca_dev->cam.ctrls != NULL) {
1441 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1442 q_ctrl->default_value = gspca_ctrl->def;
1443 q_ctrl->minimum = gspca_ctrl->min;
1444 q_ctrl->maximum = gspca_ctrl->max;
1446 if (gspca_dev->ctrl_inac & (1 << idx))
1447 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1451 static int vidioc_s_ctrl(struct file *file, void *priv,
1452 struct v4l2_control *ctrl)
1454 struct gspca_dev *gspca_dev = priv;
1455 const struct ctrl *ctrls;
1456 struct gspca_ctrl *gspca_ctrl;
1459 idx = get_ctrl(gspca_dev, ctrl->id);
1462 if (gspca_dev->ctrl_inac & (1 << idx))
1464 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1465 if (gspca_dev->cam.ctrls != NULL) {
1466 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1467 if (ctrl->value < gspca_ctrl->min
1468 || ctrl->value > gspca_ctrl->max)
1472 if (ctrl->value < ctrls->qctrl.minimum
1473 || ctrl->value > ctrls->qctrl.maximum)
1476 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1477 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1478 return -ERESTARTSYS;
1479 if (!gspca_dev->present) {
1483 gspca_dev->usb_err = 0;
1484 if (ctrls->set != NULL) {
1485 ret = ctrls->set(gspca_dev, ctrl->value);
1488 if (gspca_ctrl != NULL) {
1489 gspca_ctrl->val = ctrl->value;
1490 if (ctrls->set_control != NULL
1491 && gspca_dev->streaming)
1492 ctrls->set_control(gspca_dev);
1494 ret = gspca_dev->usb_err;
1496 mutex_unlock(&gspca_dev->usb_lock);
1500 static int vidioc_g_ctrl(struct file *file, void *priv,
1501 struct v4l2_control *ctrl)
1503 struct gspca_dev *gspca_dev = priv;
1504 const struct ctrl *ctrls;
1507 idx = get_ctrl(gspca_dev, ctrl->id);
1510 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1512 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1513 return -ERESTARTSYS;
1514 if (!gspca_dev->present) {
1518 gspca_dev->usb_err = 0;
1519 if (ctrls->get != NULL) {
1520 ret = ctrls->get(gspca_dev, &ctrl->value);
1523 if (gspca_dev->cam.ctrls != NULL)
1524 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1527 mutex_unlock(&gspca_dev->usb_lock);
1531 static int vidioc_querymenu(struct file *file, void *priv,
1532 struct v4l2_querymenu *qmenu)
1534 struct gspca_dev *gspca_dev = priv;
1536 if (!gspca_dev->sd_desc->querymenu)
1538 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1541 static int vidioc_enum_input(struct file *file, void *priv,
1542 struct v4l2_input *input)
1544 struct gspca_dev *gspca_dev = priv;
1546 if (input->index != 0)
1548 input->type = V4L2_INPUT_TYPE_CAMERA;
1549 input->status = gspca_dev->cam.input_flags;
1550 strlcpy(input->name, gspca_dev->sd_desc->name,
1551 sizeof input->name);
1555 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1561 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1568 static int vidioc_reqbufs(struct file *file, void *priv,
1569 struct v4l2_requestbuffers *rb)
1571 struct gspca_dev *gspca_dev = priv;
1572 int i, ret = 0, streaming;
1574 i = rb->memory; /* (avoid compilation warning) */
1576 case GSPCA_MEMORY_READ: /* (internal call) */
1577 case V4L2_MEMORY_MMAP:
1578 case V4L2_MEMORY_USERPTR:
1583 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1584 return -ERESTARTSYS;
1586 if (gspca_dev->memory != GSPCA_MEMORY_NO
1587 && gspca_dev->memory != GSPCA_MEMORY_READ
1588 && gspca_dev->memory != rb->memory) {
1593 /* only one file may do the capture */
1594 if (gspca_dev->capt_file != NULL
1595 && gspca_dev->capt_file != file) {
1600 /* if allocated, the buffers must not be mapped */
1601 for (i = 0; i < gspca_dev->nframes; i++) {
1602 if (gspca_dev->frame[i].vma_use_count) {
1608 /* stop streaming */
1609 streaming = gspca_dev->streaming;
1611 mutex_lock(&gspca_dev->usb_lock);
1612 gspca_dev->usb_err = 0;
1613 gspca_stream_off(gspca_dev);
1614 mutex_unlock(&gspca_dev->usb_lock);
1616 /* Don't restart the stream when switching from read
1618 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1622 /* free the previous allocated buffers, if any */
1623 if (gspca_dev->nframes != 0)
1624 frame_free(gspca_dev);
1625 if (rb->count == 0) /* unrequest */
1627 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1629 rb->count = gspca_dev->nframes;
1631 ret = gspca_init_transfer(gspca_dev);
1634 mutex_unlock(&gspca_dev->queue_lock);
1635 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1639 static int vidioc_querybuf(struct file *file, void *priv,
1640 struct v4l2_buffer *v4l2_buf)
1642 struct gspca_dev *gspca_dev = priv;
1643 struct gspca_frame *frame;
1645 if (v4l2_buf->index < 0
1646 || v4l2_buf->index >= gspca_dev->nframes)
1649 frame = &gspca_dev->frame[v4l2_buf->index];
1650 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1654 static int vidioc_streamon(struct file *file, void *priv,
1655 enum v4l2_buf_type buf_type)
1657 struct gspca_dev *gspca_dev = priv;
1660 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1662 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1663 return -ERESTARTSYS;
1665 /* check the capture file */
1666 if (gspca_dev->capt_file != file) {
1671 if (gspca_dev->nframes == 0
1672 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1676 if (!gspca_dev->streaming) {
1677 ret = gspca_init_transfer(gspca_dev);
1682 if (gspca_debug & D_STREAM) {
1683 PDEBUG_MODE("stream on OK",
1691 mutex_unlock(&gspca_dev->queue_lock);
1695 static int vidioc_streamoff(struct file *file, void *priv,
1696 enum v4l2_buf_type buf_type)
1698 struct gspca_dev *gspca_dev = priv;
1701 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1704 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1705 return -ERESTARTSYS;
1707 if (!gspca_dev->streaming) {
1712 /* check the capture file */
1713 if (gspca_dev->capt_file != file) {
1718 /* stop streaming */
1719 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1723 gspca_dev->usb_err = 0;
1724 gspca_stream_off(gspca_dev);
1725 mutex_unlock(&gspca_dev->usb_lock);
1726 /* In case another thread is waiting in dqbuf */
1727 wake_up_interruptible(&gspca_dev->wq);
1729 /* empty the transfer queues */
1730 atomic_set(&gspca_dev->fr_q, 0);
1731 atomic_set(&gspca_dev->fr_i, 0);
1732 gspca_dev->fr_o = 0;
1735 mutex_unlock(&gspca_dev->queue_lock);
1739 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1740 struct v4l2_jpegcompression *jpegcomp)
1742 struct gspca_dev *gspca_dev = priv;
1745 if (!gspca_dev->sd_desc->get_jcomp)
1747 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1748 return -ERESTARTSYS;
1749 gspca_dev->usb_err = 0;
1750 if (gspca_dev->present)
1751 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1754 mutex_unlock(&gspca_dev->usb_lock);
1758 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1759 struct v4l2_jpegcompression *jpegcomp)
1761 struct gspca_dev *gspca_dev = priv;
1764 if (!gspca_dev->sd_desc->set_jcomp)
1766 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1767 return -ERESTARTSYS;
1768 gspca_dev->usb_err = 0;
1769 if (gspca_dev->present)
1770 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1773 mutex_unlock(&gspca_dev->usb_lock);
1777 static int vidioc_g_parm(struct file *filp, void *priv,
1778 struct v4l2_streamparm *parm)
1780 struct gspca_dev *gspca_dev = priv;
1782 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1784 if (gspca_dev->sd_desc->get_streamparm) {
1787 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1788 return -ERESTARTSYS;
1789 if (gspca_dev->present) {
1790 gspca_dev->usb_err = 0;
1791 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1792 ret = gspca_dev->usb_err;
1796 mutex_unlock(&gspca_dev->usb_lock);
1803 static int vidioc_s_parm(struct file *filp, void *priv,
1804 struct v4l2_streamparm *parm)
1806 struct gspca_dev *gspca_dev = priv;
1809 n = parm->parm.capture.readbuffers;
1810 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1811 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1813 gspca_dev->nbufread = n;
1815 if (gspca_dev->sd_desc->set_streamparm) {
1818 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1819 return -ERESTARTSYS;
1820 if (gspca_dev->present) {
1821 gspca_dev->usb_err = 0;
1822 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1823 ret = gspca_dev->usb_err;
1827 mutex_unlock(&gspca_dev->usb_lock);
1834 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1836 struct gspca_dev *gspca_dev = file->private_data;
1837 struct gspca_frame *frame;
1839 unsigned long addr, start, size;
1842 start = vma->vm_start;
1843 size = vma->vm_end - vma->vm_start;
1844 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1846 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1847 return -ERESTARTSYS;
1848 if (!gspca_dev->present) {
1852 if (gspca_dev->capt_file != file) {
1858 for (i = 0; i < gspca_dev->nframes; ++i) {
1859 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1860 PDEBUG(D_STREAM, "mmap bad memory type");
1863 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1865 frame = &gspca_dev->frame[i];
1869 if (frame == NULL) {
1870 PDEBUG(D_STREAM, "mmap no frame buffer found");
1874 if (size != frame->v4l2_buf.length) {
1875 PDEBUG(D_STREAM, "mmap bad size");
1881 * - VM_IO marks the area as being a mmaped region for I/O to a
1882 * device. It also prevents the region from being core dumped.
1884 vma->vm_flags |= VM_IO;
1886 addr = (unsigned long) frame->data;
1888 page = vmalloc_to_page((void *) addr);
1889 ret = vm_insert_page(vma, start, page);
1897 vma->vm_ops = &gspca_vm_ops;
1898 vma->vm_private_data = frame;
1902 mutex_unlock(&gspca_dev->queue_lock);
1906 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1907 enum v4l2_memory memory)
1909 if (!gspca_dev->present)
1911 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1912 !gspca_dev->streaming)
1915 /* check if a frame is ready */
1916 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1919 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1920 enum v4l2_memory memory)
1924 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1925 return -ERESTARTSYS;
1926 ret = frame_ready_nolock(gspca_dev, file, memory);
1927 mutex_unlock(&gspca_dev->queue_lock);
1932 * dequeue a video buffer
1934 * If nonblock_ing is false, block until a buffer is available.
1936 static int vidioc_dqbuf(struct file *file, void *priv,
1937 struct v4l2_buffer *v4l2_buf)
1939 struct gspca_dev *gspca_dev = priv;
1940 struct gspca_frame *frame;
1943 PDEBUG(D_FRAM, "dqbuf");
1945 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1946 return -ERESTARTSYS;
1949 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1955 mutex_unlock(&gspca_dev->queue_lock);
1957 if (file->f_flags & O_NONBLOCK)
1960 /* wait till a frame is ready */
1961 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1962 frame_ready(gspca_dev, file, v4l2_buf->memory),
1963 msecs_to_jiffies(3000));
1969 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1970 return -ERESTARTSYS;
1973 i = gspca_dev->fr_o;
1974 j = gspca_dev->fr_queue[i];
1975 frame = &gspca_dev->frame[j];
1977 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1979 if (gspca_dev->sd_desc->dq_callback) {
1980 mutex_lock(&gspca_dev->usb_lock);
1981 gspca_dev->usb_err = 0;
1982 if (gspca_dev->present)
1983 gspca_dev->sd_desc->dq_callback(gspca_dev);
1984 mutex_unlock(&gspca_dev->usb_lock);
1987 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1988 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1989 PDEBUG(D_FRAM, "dqbuf %d", j);
1992 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1993 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1995 frame->v4l2_buf.bytesused)) {
1996 PDEBUG(D_ERR|D_STREAM,
1997 "dqbuf cp to user failed");
2002 mutex_unlock(&gspca_dev->queue_lock);
2007 * queue a video buffer
2009 * Attempting to queue a buffer that has already been
2010 * queued will return -EINVAL.
2012 static int vidioc_qbuf(struct file *file, void *priv,
2013 struct v4l2_buffer *v4l2_buf)
2015 struct gspca_dev *gspca_dev = priv;
2016 struct gspca_frame *frame;
2019 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
2021 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
2022 return -ERESTARTSYS;
2024 index = v4l2_buf->index;
2025 if ((unsigned) index >= gspca_dev->nframes) {
2027 "qbuf idx %d >= %d", index, gspca_dev->nframes);
2031 if (v4l2_buf->memory != gspca_dev->memory) {
2032 PDEBUG(D_FRAM, "qbuf bad memory type");
2037 frame = &gspca_dev->frame[index];
2038 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2039 PDEBUG(D_FRAM, "qbuf bad state");
2044 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
2046 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
2047 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2048 frame->v4l2_buf.length = v4l2_buf->length;
2051 /* put the buffer in the 'queued' queue */
2052 i = atomic_read(&gspca_dev->fr_q);
2053 gspca_dev->fr_queue[i] = index;
2054 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
2056 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2057 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2060 mutex_unlock(&gspca_dev->queue_lock);
2065 * allocate the resources for read()
2067 static int read_alloc(struct gspca_dev *gspca_dev,
2070 struct v4l2_buffer v4l2_buf;
2073 PDEBUG(D_STREAM, "read alloc");
2074 if (gspca_dev->nframes == 0) {
2075 struct v4l2_requestbuffers rb;
2077 memset(&rb, 0, sizeof rb);
2078 rb.count = gspca_dev->nbufread;
2079 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2080 rb.memory = GSPCA_MEMORY_READ;
2081 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2083 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2086 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2087 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2088 v4l2_buf.memory = GSPCA_MEMORY_READ;
2089 for (i = 0; i < gspca_dev->nbufread; i++) {
2091 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2093 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2097 gspca_dev->memory = GSPCA_MEMORY_READ;
2100 /* start streaming */
2101 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2103 PDEBUG(D_STREAM, "read streamon err %d", ret);
2107 static unsigned int dev_poll(struct file *file, poll_table *wait)
2109 struct gspca_dev *gspca_dev = file->private_data;
2112 PDEBUG(D_FRAM, "poll");
2114 poll_wait(file, &gspca_dev->wq, wait);
2116 /* if reqbufs is not done, the user would use read() */
2117 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
2118 ret = read_alloc(gspca_dev, file);
2123 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2126 /* check if an image has been received */
2127 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2128 ret = POLLIN | POLLRDNORM; /* yes */
2131 mutex_unlock(&gspca_dev->queue_lock);
2132 if (!gspca_dev->present)
2137 static ssize_t dev_read(struct file *file, char __user *data,
2138 size_t count, loff_t *ppos)
2140 struct gspca_dev *gspca_dev = file->private_data;
2141 struct gspca_frame *frame;
2142 struct v4l2_buffer v4l2_buf;
2143 struct timeval timestamp;
2146 PDEBUG(D_FRAM, "read (%zd)", count);
2147 if (!gspca_dev->present)
2149 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
2150 ret = read_alloc(gspca_dev, file);
2156 timestamp = ktime_to_timeval(ktime_get());
2160 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2161 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2162 v4l2_buf.memory = GSPCA_MEMORY_READ;
2163 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2165 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2169 /* if the process slept for more than 1 second,
2170 * get a newer frame */
2171 frame = &gspca_dev->frame[v4l2_buf.index];
2173 break; /* avoid infinite loop */
2174 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2176 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2178 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2183 /* copy the frame */
2184 if (count > frame->v4l2_buf.bytesused)
2185 count = frame->v4l2_buf.bytesused;
2186 ret = copy_to_user(data, frame->data, count);
2188 PDEBUG(D_ERR|D_STREAM,
2189 "read cp to user lack %d / %zd", ret, count);
2195 /* in each case, requeue the buffer */
2196 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2202 static struct v4l2_file_operations dev_fops = {
2203 .owner = THIS_MODULE,
2205 .release = dev_close,
2208 .unlocked_ioctl = video_ioctl2,
2212 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2213 .vidioc_querycap = vidioc_querycap,
2214 .vidioc_dqbuf = vidioc_dqbuf,
2215 .vidioc_qbuf = vidioc_qbuf,
2216 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2217 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2218 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2219 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2220 .vidioc_streamon = vidioc_streamon,
2221 .vidioc_queryctrl = vidioc_queryctrl,
2222 .vidioc_g_ctrl = vidioc_g_ctrl,
2223 .vidioc_s_ctrl = vidioc_s_ctrl,
2224 .vidioc_querymenu = vidioc_querymenu,
2225 .vidioc_enum_input = vidioc_enum_input,
2226 .vidioc_g_input = vidioc_g_input,
2227 .vidioc_s_input = vidioc_s_input,
2228 .vidioc_reqbufs = vidioc_reqbufs,
2229 .vidioc_querybuf = vidioc_querybuf,
2230 .vidioc_streamoff = vidioc_streamoff,
2231 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2232 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2233 .vidioc_g_parm = vidioc_g_parm,
2234 .vidioc_s_parm = vidioc_s_parm,
2235 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2236 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2237 #ifdef CONFIG_VIDEO_ADV_DEBUG
2238 .vidioc_g_register = vidioc_g_register,
2239 .vidioc_s_register = vidioc_s_register,
2241 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2244 static const struct video_device gspca_template = {
2245 .name = "gspca main driver",
2247 .ioctl_ops = &dev_ioctl_ops,
2248 .release = gspca_release,
2251 /* initialize the controls */
2252 static void ctrls_init(struct gspca_dev *gspca_dev)
2254 struct gspca_ctrl *ctrl;
2257 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2258 i < gspca_dev->sd_desc->nctrls;
2260 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2261 ctrl->val = ctrl->def;
2262 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2263 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2268 * probe and create a new gspca device
2270 * This function must be called by the sub-driver when it is
2271 * called for probing a new device.
2273 int gspca_dev_probe2(struct usb_interface *intf,
2274 const struct usb_device_id *id,
2275 const struct sd_desc *sd_desc,
2277 struct module *module)
2279 struct gspca_dev *gspca_dev;
2280 struct usb_device *dev = interface_to_usbdev(intf);
2283 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2285 /* create the device */
2286 if (dev_size < sizeof *gspca_dev)
2287 dev_size = sizeof *gspca_dev;
2288 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2290 pr_err("couldn't kzalloc gspca struct\n");
2293 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2294 if (!gspca_dev->usb_buf) {
2295 pr_err("out of memory\n");
2299 gspca_dev->dev = dev;
2300 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2301 gspca_dev->nbalt = intf->num_altsetting;
2303 /* check if any audio device */
2304 if (dev->config->desc.bNumInterfaces != 1) {
2306 struct usb_interface *intf2;
2308 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2309 intf2 = dev->config->interface[i];
2311 && intf2->altsetting != NULL
2312 && intf2->altsetting->desc.bInterfaceClass ==
2314 gspca_dev->audio = 1;
2320 gspca_dev->sd_desc = sd_desc;
2321 gspca_dev->nbufread = 2;
2322 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2324 /* configure the subdriver and initialize the USB device */
2325 ret = sd_desc->config(gspca_dev, id);
2328 if (gspca_dev->cam.ctrls != NULL)
2329 ctrls_init(gspca_dev);
2330 ret = sd_desc->init(gspca_dev);
2333 gspca_set_default_mode(gspca_dev);
2335 ret = gspca_input_connect(gspca_dev);
2339 mutex_init(&gspca_dev->usb_lock);
2340 mutex_init(&gspca_dev->queue_lock);
2341 init_waitqueue_head(&gspca_dev->wq);
2343 /* init video stuff */
2344 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2345 gspca_dev->vdev.parent = &intf->dev;
2346 gspca_dev->module = module;
2347 gspca_dev->present = 1;
2348 ret = video_register_device(&gspca_dev->vdev,
2352 pr_err("video_register_device err %d\n", ret);
2356 usb_set_intfdata(intf, gspca_dev);
2357 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2359 gspca_input_create_urb(gspca_dev);
2363 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2364 if (gspca_dev->input_dev)
2365 input_unregister_device(gspca_dev->input_dev);
2367 kfree(gspca_dev->usb_buf);
2371 EXPORT_SYMBOL(gspca_dev_probe2);
2373 /* same function as the previous one, but check the interface */
2374 int gspca_dev_probe(struct usb_interface *intf,
2375 const struct usb_device_id *id,
2376 const struct sd_desc *sd_desc,
2378 struct module *module)
2380 struct usb_device *dev = interface_to_usbdev(intf);
2382 /* we don't handle multi-config cameras */
2383 if (dev->descriptor.bNumConfigurations != 1) {
2384 pr_err("%04x:%04x too many config\n",
2385 id->idVendor, id->idProduct);
2389 /* the USB video interface must be the first one */
2390 if (dev->config->desc.bNumInterfaces != 1
2391 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2394 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2396 EXPORT_SYMBOL(gspca_dev_probe);
2401 * This function must be called by the sub-driver
2402 * when the device disconnects, after the specific resources are freed.
2404 void gspca_disconnect(struct usb_interface *intf)
2406 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2407 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2408 struct input_dev *input_dev;
2411 PDEBUG(D_PROBE, "%s disconnect",
2412 video_device_node_name(&gspca_dev->vdev));
2413 mutex_lock(&gspca_dev->usb_lock);
2415 gspca_dev->present = 0;
2416 wake_up_interruptible(&gspca_dev->wq);
2418 destroy_urbs(gspca_dev);
2420 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2421 gspca_input_destroy_urb(gspca_dev);
2422 input_dev = gspca_dev->input_dev;
2424 gspca_dev->input_dev = NULL;
2425 input_unregister_device(input_dev);
2429 /* the device is freed at exit of this function */
2430 gspca_dev->dev = NULL;
2431 mutex_unlock(&gspca_dev->usb_lock);
2433 usb_set_intfdata(intf, NULL);
2435 /* release the device */
2436 /* (this will call gspca_release() immediately or on last close) */
2437 video_unregister_device(&gspca_dev->vdev);
2439 /* PDEBUG(D_PROBE, "disconnect complete"); */
2441 EXPORT_SYMBOL(gspca_disconnect);
2444 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2446 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2448 if (!gspca_dev->streaming)
2450 gspca_dev->frozen = 1; /* avoid urb error messages */
2451 if (gspca_dev->sd_desc->stopN)
2452 gspca_dev->sd_desc->stopN(gspca_dev);
2453 destroy_urbs(gspca_dev);
2454 gspca_input_destroy_urb(gspca_dev);
2455 gspca_set_alt0(gspca_dev);
2456 if (gspca_dev->sd_desc->stop0)
2457 gspca_dev->sd_desc->stop0(gspca_dev);
2460 EXPORT_SYMBOL(gspca_suspend);
2462 int gspca_resume(struct usb_interface *intf)
2464 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2466 gspca_dev->frozen = 0;
2467 gspca_dev->sd_desc->init(gspca_dev);
2468 gspca_input_create_urb(gspca_dev);
2469 if (gspca_dev->streaming)
2470 return gspca_init_transfer(gspca_dev);
2473 EXPORT_SYMBOL(gspca_resume);
2475 /* -- cam driver utility functions -- */
2477 /* auto gain and exposure algorithm based on the knee algorithm described here:
2478 http://ytse.tricolour.net/docs/LowLightOptimization.html
2480 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2482 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2483 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2485 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2486 const struct ctrl *gain_ctrl = NULL;
2487 const struct ctrl *exposure_ctrl = NULL;
2488 const struct ctrl *autogain_ctrl = NULL;
2491 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2492 if (gspca_dev->ctrl_dis & (1 << i))
2494 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2495 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2496 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2497 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2498 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2499 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2501 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2502 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2503 "on cam without (auto)gain/exposure");
2507 if (gain_ctrl->get(gspca_dev, &gain) ||
2508 exposure_ctrl->get(gspca_dev, &exposure) ||
2509 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2513 orig_exposure = exposure;
2515 /* If we are of a multiple of deadzone, do multiple steps to reach the
2516 desired lumination fast (with the risc of a slight overshoot) */
2517 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2519 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2520 avg_lum, desired_avg_lum, steps);
2522 for (i = 0; i < steps; i++) {
2523 if (avg_lum > desired_avg_lum) {
2524 if (gain > gain_knee)
2526 else if (exposure > exposure_knee)
2528 else if (gain > gain_ctrl->qctrl.default_value)
2530 else if (exposure > exposure_ctrl->qctrl.minimum)
2532 else if (gain > gain_ctrl->qctrl.minimum)
2537 if (gain < gain_ctrl->qctrl.default_value)
2539 else if (exposure < exposure_knee)
2541 else if (gain < gain_knee)
2543 else if (exposure < exposure_ctrl->qctrl.maximum)
2545 else if (gain < gain_ctrl->qctrl.maximum)
2552 if (gain != orig_gain) {
2553 gain_ctrl->set(gspca_dev, gain);
2556 if (exposure != orig_exposure) {
2557 exposure_ctrl->set(gspca_dev, exposure);
2563 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2565 /* -- module insert / remove -- */
2566 static int __init gspca_init(void)
2568 pr_info("v" DRIVER_VERSION_NUMBER " registered\n");
2571 static void __exit gspca_exit(void)
2575 module_init(gspca_init);
2576 module_exit(gspca_exit);
2579 module_param_named(debug, gspca_debug, int, 0644);
2580 MODULE_PARM_DESC(debug,
2581 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2582 " 0x08:stream 0x10:frame 0x20:packet"