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 GSPCA_VERSION "2.14.0"
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 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
56 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(GSPCA_VERSION);
61 int gspca_debug = D_ERR | D_PROBE;
62 EXPORT_SYMBOL(gspca_debug);
64 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
66 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
67 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
71 (pixfmt >> 16) & 0xff,
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102 static void gspca_vm_close(struct vm_area_struct *vma)
104 struct gspca_frame *frame = vma->vm_private_data;
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110 static const struct vm_operations_struct gspca_vm_ops = {
111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb *urb)
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PDEBUG(D_ERR, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
151 pr_err("Resubmit URB failed with error %i\n", ret);
155 static int gspca_input_connect(struct gspca_dev *dev)
157 struct input_dev *input_dev;
160 dev->input_dev = NULL;
161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
162 input_dev = input_allocate_device();
166 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
167 strlcat(dev->phys, "/input0", sizeof(dev->phys));
169 input_dev->name = dev->sd_desc->name;
170 input_dev->phys = dev->phys;
172 usb_to_input_id(dev->dev, &input_dev->id);
174 input_dev->evbit[0] = BIT_MASK(EV_KEY);
175 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
176 input_dev->dev.parent = &dev->dev->dev;
178 err = input_register_device(input_dev);
180 pr_err("Input device registration failed with error %i\n",
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
185 dev->input_dev = input_dev;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
198 struct usb_device *dev;
202 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
203 interval = ep->bInterval;
204 PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep->bEndpointAddress, buffer_len, interval);
208 dev = gspca_dev->dev;
210 urb = usb_alloc_urb(0, GFP_KERNEL);
216 buffer = usb_alloc_coherent(dev, buffer_len,
217 GFP_KERNEL, &urb->transfer_dma);
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
225 int_irq, (void *)gspca_dev, interval);
226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
232 gspca_dev->int_urb = urb;
236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
246 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
248 struct usb_interface *intf;
249 struct usb_host_interface *intf_desc;
250 struct usb_endpoint_descriptor *ep;
253 if (gspca_dev->sd_desc->int_pkt_scan) {
254 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
255 intf_desc = intf->cur_altsetting;
256 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
257 ep = &intf_desc->endpoint[i].desc;
258 if (usb_endpoint_dir_in(ep) &&
259 usb_endpoint_xfer_int(ep)) {
261 alloc_and_submit_int_urb(gspca_dev, ep);
268 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
272 urb = gspca_dev->int_urb;
274 gspca_dev->int_urb = NULL;
276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
284 static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
288 static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
292 static inline int gspca_input_connect(struct gspca_dev *dev)
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev *gspca_dev,
304 u8 *data; /* address of data in the iso message */
308 if (urb->status != 0) {
309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
312 if (gspca_dev->frozen)
315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
319 pkt_scan = gspca_dev->sd_desc->pkt_scan;
320 for (i = 0; i < urb->number_of_packets; i++) {
321 len = urb->iso_frame_desc[i].actual_length;
323 /* check the packet status and length */
324 st = urb->iso_frame_desc[i].status;
326 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
328 gspca_dev->last_packet_type = DISCARD_PACKET;
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
339 i, urb->iso_frame_desc[i].offset, len);
340 data = (u8 *) urb->transfer_buffer
341 + urb->iso_frame_desc[i].offset;
342 pkt_scan(gspca_dev, data, len);
346 /* resubmit the URB */
347 st = usb_submit_urb(urb, GFP_ATOMIC);
349 pr_err("usb_submit_urb() ret %d\n", st);
353 * ISOC message interrupt from the USB device
355 * Analyse each packet and call the subdriver for copy to the frame buffer.
357 static void isoc_irq(struct urb *urb)
359 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
361 PDEBUG(D_PACK, "isoc irq");
362 if (!gspca_dev->streaming)
364 fill_frame(gspca_dev, urb);
368 * bulk message interrupt from the USB device
370 static void bulk_irq(struct urb *urb)
372 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
378 switch (urb->status) {
382 return; /* disconnection */
385 if (gspca_dev->frozen)
388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
395 urb->transfer_buffer,
399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
403 pr_err("usb_submit_urb() ret %d\n", st);
408 * add data to the current frame
410 * This function is called by the subdrivers at interrupt level.
412 * To build a frame, these ones must add
414 * - 0 or many INTER_PACKETs
416 * DISCARD_PACKET invalidates the whole frame.
418 void gspca_frame_add(struct gspca_dev *gspca_dev,
419 enum gspca_packet_type packet_type,
423 struct gspca_frame *frame;
426 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
428 if (packet_type == FIRST_PACKET) {
429 i = atomic_read(&gspca_dev->fr_i);
431 /* if there are no queued buffer, discard the whole frame */
432 if (i == atomic_read(&gspca_dev->fr_q)) {
433 gspca_dev->last_packet_type = DISCARD_PACKET;
434 gspca_dev->sequence++;
437 j = gspca_dev->fr_queue[i];
438 frame = &gspca_dev->frame[j];
439 frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
440 frame->v4l2_buf.sequence = gspca_dev->sequence++;
441 gspca_dev->image = frame->data;
442 gspca_dev->image_len = 0;
444 switch (gspca_dev->last_packet_type) {
446 if (packet_type == LAST_PACKET) {
447 gspca_dev->last_packet_type = packet_type;
448 gspca_dev->image = NULL;
449 gspca_dev->image_len = 0;
457 /* append the packet to the frame buffer */
459 if (gspca_dev->image_len + len > gspca_dev->frsz) {
460 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
461 gspca_dev->image_len + len,
463 packet_type = DISCARD_PACKET;
465 /* !! image is NULL only when last pkt is LAST or DISCARD
466 if (gspca_dev->image == NULL) {
467 pr_err("gspca_frame_add() image == NULL\n");
471 memcpy(gspca_dev->image + gspca_dev->image_len,
473 gspca_dev->image_len += len;
476 gspca_dev->last_packet_type = packet_type;
478 /* if last packet, invalidate packet concatenation until
479 * next first packet, wake up the application and advance
481 if (packet_type == LAST_PACKET) {
482 i = atomic_read(&gspca_dev->fr_i);
483 j = gspca_dev->fr_queue[i];
484 frame = &gspca_dev->frame[j];
485 frame->v4l2_buf.bytesused = gspca_dev->image_len;
486 frame->v4l2_buf.flags = (frame->v4l2_buf.flags
487 | V4L2_BUF_FLAG_DONE)
488 & ~V4L2_BUF_FLAG_QUEUED;
489 i = (i + 1) % GSPCA_MAX_FRAMES;
490 atomic_set(&gspca_dev->fr_i, i);
491 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
492 PDEBUG(D_FRAM, "frame complete len:%d",
493 frame->v4l2_buf.bytesused);
494 gspca_dev->image = NULL;
495 gspca_dev->image_len = 0;
498 EXPORT_SYMBOL(gspca_frame_add);
500 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
501 enum v4l2_memory memory, unsigned int count)
503 struct gspca_frame *frame;
507 i = gspca_dev->curr_mode;
508 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
509 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
510 frsz = PAGE_ALIGN(frsz);
511 if (count >= GSPCA_MAX_FRAMES)
512 count = GSPCA_MAX_FRAMES - 1;
513 gspca_dev->frbuf = vmalloc_32(frsz * count);
514 if (!gspca_dev->frbuf) {
515 pr_err("frame alloc failed\n");
518 gspca_dev->capt_file = file;
519 gspca_dev->memory = memory;
520 gspca_dev->frsz = frsz;
521 gspca_dev->nframes = count;
522 for (i = 0; i < count; i++) {
523 frame = &gspca_dev->frame[i];
524 frame->v4l2_buf.index = i;
525 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
526 frame->v4l2_buf.flags = 0;
527 frame->v4l2_buf.field = V4L2_FIELD_NONE;
528 frame->v4l2_buf.length = frsz;
529 frame->v4l2_buf.memory = memory;
530 frame->v4l2_buf.sequence = 0;
531 frame->data = gspca_dev->frbuf + i * frsz;
532 frame->v4l2_buf.m.offset = i * frsz;
534 atomic_set(&gspca_dev->fr_q, 0);
535 atomic_set(&gspca_dev->fr_i, 0);
540 static void frame_free(struct gspca_dev *gspca_dev)
544 PDEBUG(D_STREAM, "frame free");
545 if (gspca_dev->frbuf != NULL) {
546 vfree(gspca_dev->frbuf);
547 gspca_dev->frbuf = NULL;
548 for (i = 0; i < gspca_dev->nframes; i++)
549 gspca_dev->frame[i].data = NULL;
551 gspca_dev->nframes = 0;
553 gspca_dev->capt_file = NULL;
554 gspca_dev->memory = GSPCA_MEMORY_NO;
557 static void destroy_urbs(struct gspca_dev *gspca_dev)
562 PDEBUG(D_STREAM, "kill transfer");
563 for (i = 0; i < MAX_NURBS; i++) {
564 urb = gspca_dev->urb[i];
568 gspca_dev->urb[i] = NULL;
570 if (urb->transfer_buffer != NULL)
571 usb_free_coherent(gspca_dev->dev,
572 urb->transfer_buffer_length,
573 urb->transfer_buffer,
579 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
583 if (gspca_dev->alt == 0)
585 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
587 pr_err("set alt 0 err %d\n", ret);
591 /* Note: both the queue and the usb locks should be held when calling this */
592 static void gspca_stream_off(struct gspca_dev *gspca_dev)
594 gspca_dev->streaming = 0;
595 if (gspca_dev->present) {
596 if (gspca_dev->sd_desc->stopN)
597 gspca_dev->sd_desc->stopN(gspca_dev);
598 destroy_urbs(gspca_dev);
599 gspca_input_destroy_urb(gspca_dev);
600 gspca_set_alt0(gspca_dev);
601 gspca_input_create_urb(gspca_dev);
604 /* always call stop0 to free the subdriver's resources */
605 if (gspca_dev->sd_desc->stop0)
606 gspca_dev->sd_desc->stop0(gspca_dev);
607 PDEBUG(D_STREAM, "stream off OK");
611 * look for an input transfer endpoint in an alternate setting
613 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
616 struct usb_host_endpoint *ep;
619 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
620 ep = &alt->endpoint[i];
621 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
623 && ep->desc.wMaxPacketSize != 0
624 && usb_endpoint_dir_in(&ep->desc))
630 /* compute the minimum bandwidth for the current transfer */
631 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
636 i = gspca_dev->curr_mode;
637 bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
639 /* if the image is compressed, estimate the mean image size */
640 if (bandwidth < gspca_dev->cam.cam_mode[i].width *
641 gspca_dev->cam.cam_mode[i].height)
644 /* estimate the frame rate */
645 if (gspca_dev->sd_desc->get_streamparm) {
646 struct v4l2_streamparm parm;
648 parm.parm.capture.timeperframe.denominator = 15;
649 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
650 bandwidth *= parm.parm.capture.timeperframe.denominator;
652 bandwidth *= 15; /* 15 fps */
655 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
667 * build the table of the endpoints
668 * and compute the minimum bandwidth for the image transfer
670 static int build_ep_tb(struct gspca_dev *gspca_dev,
671 struct usb_interface *intf,
673 struct ep_tb_s *ep_tb)
675 struct usb_host_endpoint *ep;
676 int i, j, nbalt, psize, found;
677 u32 bandwidth, last_bw;
679 nbalt = intf->num_altsetting;
681 nbalt = MAX_ALT; /* fixme: should warn */
683 /* build the endpoint table */
687 ep_tb->bandwidth = 2000 * 2000 * 120;
689 for (j = 0; j < nbalt; j++) {
690 ep = alt_xfer(&intf->altsetting[j], xfer);
693 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
694 if (!gspca_dev->cam.bulk) /* isoc */
695 psize = (psize & 0x07ff) *
696 (1 + ((psize >> 11) & 3));
697 bandwidth = psize * ep->desc.bInterval * 1000;
698 if (gspca_dev->dev->speed == USB_SPEED_HIGH
699 || gspca_dev->dev->speed == USB_SPEED_SUPER)
701 if (bandwidth <= last_bw)
703 if (bandwidth < ep_tb->bandwidth) {
704 ep_tb->bandwidth = bandwidth;
711 PDEBUG(D_STREAM, "alt %d bandwidth %d",
712 ep_tb->alt, ep_tb->bandwidth);
713 last_bw = ep_tb->bandwidth;
718 /* get the requested bandwidth and start at the highest atlsetting */
719 bandwidth = which_bandwidth(gspca_dev);
723 if (ep_tb->bandwidth < bandwidth)
731 * create the URBs for image transfer
733 static int create_urbs(struct gspca_dev *gspca_dev,
734 struct usb_host_endpoint *ep)
737 int n, nurbs, i, psize, npkt, bsize;
739 /* calculate the packet size and the number of packets */
740 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
742 if (!gspca_dev->cam.bulk) { /* isoc */
744 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
745 if (gspca_dev->pkt_size == 0)
746 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
748 psize = gspca_dev->pkt_size;
749 npkt = gspca_dev->cam.npkt;
751 npkt = 32; /* default value */
752 bsize = psize * npkt;
754 "isoc %d pkts size %d = bsize:%d",
759 bsize = gspca_dev->cam.bulk_size;
762 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
763 if (gspca_dev->cam.bulk_nurbs != 0)
764 nurbs = gspca_dev->cam.bulk_nurbs;
769 for (n = 0; n < nurbs; n++) {
770 urb = usb_alloc_urb(npkt, GFP_KERNEL);
772 pr_err("usb_alloc_urb failed\n");
775 gspca_dev->urb[n] = urb;
776 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
781 if (urb->transfer_buffer == NULL) {
782 pr_err("usb_alloc_coherent failed\n");
785 urb->dev = gspca_dev->dev;
786 urb->context = gspca_dev;
787 urb->transfer_buffer_length = bsize;
788 if (npkt != 0) { /* ISOC */
789 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
790 ep->desc.bEndpointAddress);
791 urb->transfer_flags = URB_ISO_ASAP
792 | URB_NO_TRANSFER_DMA_MAP;
793 if (gspca_dev->dev->speed == USB_SPEED_LOW)
794 urb->interval = ep->desc.bInterval;
796 urb->interval = 1 << (ep->desc.bInterval - 1);
797 urb->complete = isoc_irq;
798 urb->number_of_packets = npkt;
799 for (i = 0; i < npkt; i++) {
800 urb->iso_frame_desc[i].length = psize;
801 urb->iso_frame_desc[i].offset = psize * i;
804 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
805 ep->desc.bEndpointAddress);
806 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
807 urb->complete = bulk_irq;
814 * start the USB transfer
816 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
818 struct usb_interface *intf;
819 struct usb_host_endpoint *ep;
821 struct ep_tb_s ep_tb[MAX_ALT];
822 int n, ret, xfer, alt, alt_idx;
824 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
827 if (!gspca_dev->present) {
832 /* reset the streaming variables */
833 gspca_dev->image = NULL;
834 gspca_dev->image_len = 0;
835 gspca_dev->last_packet_type = DISCARD_PACKET;
836 gspca_dev->sequence = 0;
838 gspca_dev->usb_err = 0;
840 /* do the specific subdriver stuff before endpoint selection */
841 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
842 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
843 if (gspca_dev->sd_desc->isoc_init) {
844 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
848 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
849 : USB_ENDPOINT_XFER_ISOC;
851 /* if the subdriver forced an altsetting, get the endpoint */
852 if (gspca_dev->alt != 0) {
853 gspca_dev->alt--; /* (previous version compatibility) */
854 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
856 pr_err("bad altsetting %d\n", gspca_dev->alt);
860 ep_tb[0].alt = gspca_dev->alt;
864 /* else, compute the minimum bandwidth
865 * and build the endpoint table */
866 alt_idx = build_ep_tb(gspca_dev, intf, xfer, ep_tb);
868 pr_err("no transfer endpoint found\n");
874 /* set the highest alternate setting and
875 * loop until urb submit succeeds */
876 gspca_input_destroy_urb(gspca_dev);
878 gspca_dev->alt = ep_tb[--alt_idx].alt;
881 if (alt != gspca_dev->alt) {
882 alt = gspca_dev->alt;
883 if (gspca_dev->nbalt > 1) {
884 ret = usb_set_interface(gspca_dev->dev,
889 goto retry; /*fixme: ugly*/
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 */
946 PDEBUG(D_ERR|D_STREAM,
947 "alt %d - bandwidth not wide enough - trying again",
949 msleep(20); /* wait for kill complete */
950 if (gspca_dev->sd_desc->isoc_nego) {
951 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
956 pr_err("no transfer endpoint found\n");
960 gspca_dev->alt = ep_tb[--alt_idx].alt;
964 gspca_input_create_urb(gspca_dev);
966 mutex_unlock(&gspca_dev->usb_lock);
970 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
972 struct gspca_ctrl *ctrl;
975 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
976 gspca_dev->curr_mode = i;
977 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
978 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
979 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
981 /* set the current control values to their default values
982 * which may have changed in sd_init() */
983 ctrl = gspca_dev->cam.ctrls;
986 i < gspca_dev->sd_desc->nctrls;
988 ctrl->val = ctrl->def;
992 static int wxh_to_mode(struct gspca_dev *gspca_dev,
993 int width, int height)
997 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
998 if (width >= gspca_dev->cam.cam_mode[i].width
999 && height >= gspca_dev->cam.cam_mode[i].height)
1006 * search a mode with the right pixel format
1008 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1014 modeU = modeD = mode;
1015 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1017 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1021 if (++modeU < gspca_dev->cam.nmodes) {
1022 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1030 #ifdef CONFIG_VIDEO_ADV_DEBUG
1031 static int vidioc_g_register(struct file *file, void *priv,
1032 struct v4l2_dbg_register *reg)
1035 struct gspca_dev *gspca_dev = priv;
1037 if (!gspca_dev->sd_desc->get_chip_ident)
1040 if (!gspca_dev->sd_desc->get_register)
1043 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1044 return -ERESTARTSYS;
1045 gspca_dev->usb_err = 0;
1046 if (gspca_dev->present)
1047 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
1050 mutex_unlock(&gspca_dev->usb_lock);
1055 static int vidioc_s_register(struct file *file, void *priv,
1056 struct v4l2_dbg_register *reg)
1059 struct gspca_dev *gspca_dev = priv;
1061 if (!gspca_dev->sd_desc->get_chip_ident)
1064 if (!gspca_dev->sd_desc->set_register)
1067 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1068 return -ERESTARTSYS;
1069 gspca_dev->usb_err = 0;
1070 if (gspca_dev->present)
1071 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
1074 mutex_unlock(&gspca_dev->usb_lock);
1080 static int vidioc_g_chip_ident(struct file *file, void *priv,
1081 struct v4l2_dbg_chip_ident *chip)
1084 struct gspca_dev *gspca_dev = priv;
1086 if (!gspca_dev->sd_desc->get_chip_ident)
1089 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1090 return -ERESTARTSYS;
1091 gspca_dev->usb_err = 0;
1092 if (gspca_dev->present)
1093 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1096 mutex_unlock(&gspca_dev->usb_lock);
1101 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1102 struct v4l2_fmtdesc *fmtdesc)
1104 struct gspca_dev *gspca_dev = priv;
1108 /* give an index to each format */
1111 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1112 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1115 if (fmt_tb[j] == fmt_tb[index])
1120 if (fmtdesc->index == index)
1121 break; /* new format */
1123 if (index >= ARRAY_SIZE(fmt_tb))
1128 return -EINVAL; /* no more format */
1130 fmtdesc->pixelformat = fmt_tb[index];
1131 if (gspca_dev->cam.cam_mode[i].sizeimage <
1132 gspca_dev->cam.cam_mode[i].width *
1133 gspca_dev->cam.cam_mode[i].height)
1134 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1135 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1136 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1137 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1138 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1139 fmtdesc->description[4] = '\0';
1143 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1144 struct v4l2_format *fmt)
1146 struct gspca_dev *gspca_dev = priv;
1149 mode = gspca_dev->curr_mode;
1150 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1151 sizeof fmt->fmt.pix);
1155 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1156 struct v4l2_format *fmt)
1158 int w, h, mode, mode2;
1160 w = fmt->fmt.pix.width;
1161 h = fmt->fmt.pix.height;
1164 if (gspca_debug & D_CONF)
1165 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1167 /* search the closest mode for width and height */
1168 mode = wxh_to_mode(gspca_dev, w, h);
1170 /* OK if right palette */
1171 if (gspca_dev->cam.cam_mode[mode].pixelformat
1172 != fmt->fmt.pix.pixelformat) {
1174 /* else, search the closest mode with the same pixel format */
1175 mode2 = gspca_get_mode(gspca_dev, mode,
1176 fmt->fmt.pix.pixelformat);
1180 ; * no chance, return this mode */
1182 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1183 sizeof fmt->fmt.pix);
1184 return mode; /* used when s_fmt */
1187 static int vidioc_try_fmt_vid_cap(struct file *file,
1189 struct v4l2_format *fmt)
1191 struct gspca_dev *gspca_dev = priv;
1194 ret = try_fmt_vid_cap(gspca_dev, fmt);
1200 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1201 struct v4l2_format *fmt)
1203 struct gspca_dev *gspca_dev = priv;
1206 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1207 return -ERESTARTSYS;
1209 ret = try_fmt_vid_cap(gspca_dev, fmt);
1213 if (gspca_dev->nframes != 0
1214 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1219 if (ret == gspca_dev->curr_mode) {
1221 goto out; /* same mode */
1224 if (gspca_dev->streaming) {
1228 gspca_dev->width = fmt->fmt.pix.width;
1229 gspca_dev->height = fmt->fmt.pix.height;
1230 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1231 gspca_dev->curr_mode = ret;
1235 mutex_unlock(&gspca_dev->queue_lock);
1239 static int vidioc_enum_framesizes(struct file *file, void *priv,
1240 struct v4l2_frmsizeenum *fsize)
1242 struct gspca_dev *gspca_dev = priv;
1246 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1247 if (fsize->pixel_format !=
1248 gspca_dev->cam.cam_mode[i].pixelformat)
1251 if (fsize->index == index) {
1252 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1253 fsize->discrete.width =
1254 gspca_dev->cam.cam_mode[i].width;
1255 fsize->discrete.height =
1256 gspca_dev->cam.cam_mode[i].height;
1265 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1266 struct v4l2_frmivalenum *fival)
1268 struct gspca_dev *gspca_dev = priv;
1269 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1272 if (gspca_dev->cam.mode_framerates == NULL ||
1273 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1276 if (fival->pixel_format !=
1277 gspca_dev->cam.cam_mode[mode].pixelformat)
1280 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1281 if (fival->index == i) {
1282 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1283 fival->discrete.numerator = 1;
1284 fival->discrete.denominator =
1285 gspca_dev->cam.mode_framerates[mode].rates[i];
1293 static void gspca_release(struct video_device *vfd)
1295 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1297 PDEBUG(D_PROBE, "%s released",
1298 video_device_node_name(&gspca_dev->vdev));
1300 kfree(gspca_dev->usb_buf);
1304 static int dev_open(struct file *file)
1306 struct gspca_dev *gspca_dev;
1308 PDEBUG(D_STREAM, "[%s] open", current->comm);
1309 gspca_dev = (struct gspca_dev *) video_devdata(file);
1310 if (!gspca_dev->present)
1313 /* protect the subdriver against rmmod */
1314 if (!try_module_get(gspca_dev->module))
1317 file->private_data = gspca_dev;
1319 /* activate the v4l2 debug */
1320 if (gspca_debug & D_V4L2)
1321 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1322 | V4L2_DEBUG_IOCTL_ARG;
1324 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1325 | V4L2_DEBUG_IOCTL_ARG);
1330 static int dev_close(struct file *file)
1332 struct gspca_dev *gspca_dev = file->private_data;
1334 PDEBUG(D_STREAM, "[%s] close", current->comm);
1335 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1336 return -ERESTARTSYS;
1338 /* if the file did the capture, free the streaming resources */
1339 if (gspca_dev->capt_file == file) {
1340 if (gspca_dev->streaming) {
1341 mutex_lock(&gspca_dev->usb_lock);
1342 gspca_dev->usb_err = 0;
1343 gspca_stream_off(gspca_dev);
1344 mutex_unlock(&gspca_dev->usb_lock);
1346 frame_free(gspca_dev);
1348 file->private_data = NULL;
1349 module_put(gspca_dev->module);
1350 mutex_unlock(&gspca_dev->queue_lock);
1352 PDEBUG(D_STREAM, "close done");
1357 static int vidioc_querycap(struct file *file, void *priv,
1358 struct v4l2_capability *cap)
1360 struct gspca_dev *gspca_dev = priv;
1363 /* protect the access to the usb device */
1364 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1365 return -ERESTARTSYS;
1366 if (!gspca_dev->present) {
1370 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1371 sizeof cap->driver);
1372 if (gspca_dev->dev->product != NULL) {
1373 strlcpy((char *) cap->card, gspca_dev->dev->product,
1376 snprintf((char *) cap->card, sizeof cap->card,
1377 "USB Camera (%04x:%04x)",
1378 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1379 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1381 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1382 sizeof(cap->bus_info));
1383 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1384 | V4L2_CAP_STREAMING
1385 | V4L2_CAP_READWRITE;
1388 mutex_unlock(&gspca_dev->usb_lock);
1392 static int get_ctrl(struct gspca_dev *gspca_dev,
1395 const struct ctrl *ctrls;
1398 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1399 i < gspca_dev->sd_desc->nctrls;
1401 if (gspca_dev->ctrl_dis & (1 << i))
1403 if (id == ctrls->qctrl.id)
1409 static int vidioc_queryctrl(struct file *file, void *priv,
1410 struct v4l2_queryctrl *q_ctrl)
1412 struct gspca_dev *gspca_dev = priv;
1413 const struct ctrl *ctrls;
1414 struct gspca_ctrl *gspca_ctrl;
1419 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1420 id &= V4L2_CTRL_ID_MASK;
1423 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1424 if (gspca_dev->ctrl_dis & (1 << i))
1426 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1429 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1430 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
1435 idx = get_ctrl(gspca_dev, id);
1439 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1440 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1441 if (gspca_dev->cam.ctrls != NULL) {
1442 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1443 q_ctrl->default_value = gspca_ctrl->def;
1444 q_ctrl->minimum = gspca_ctrl->min;
1445 q_ctrl->maximum = gspca_ctrl->max;
1447 if (gspca_dev->ctrl_inac & (1 << idx))
1448 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1452 static int vidioc_s_ctrl(struct file *file, void *priv,
1453 struct v4l2_control *ctrl)
1455 struct gspca_dev *gspca_dev = priv;
1456 const struct ctrl *ctrls;
1457 struct gspca_ctrl *gspca_ctrl;
1460 idx = get_ctrl(gspca_dev, ctrl->id);
1463 if (gspca_dev->ctrl_inac & (1 << idx))
1465 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1466 if (gspca_dev->cam.ctrls != NULL) {
1467 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1468 if (ctrl->value < gspca_ctrl->min
1469 || ctrl->value > gspca_ctrl->max)
1473 if (ctrl->value < ctrls->qctrl.minimum
1474 || ctrl->value > ctrls->qctrl.maximum)
1477 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1478 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1479 return -ERESTARTSYS;
1480 if (!gspca_dev->present) {
1484 gspca_dev->usb_err = 0;
1485 if (ctrls->set != NULL) {
1486 ret = ctrls->set(gspca_dev, ctrl->value);
1489 if (gspca_ctrl != NULL) {
1490 gspca_ctrl->val = ctrl->value;
1491 if (ctrls->set_control != NULL
1492 && gspca_dev->streaming)
1493 ctrls->set_control(gspca_dev);
1495 ret = gspca_dev->usb_err;
1497 mutex_unlock(&gspca_dev->usb_lock);
1501 static int vidioc_g_ctrl(struct file *file, void *priv,
1502 struct v4l2_control *ctrl)
1504 struct gspca_dev *gspca_dev = priv;
1505 const struct ctrl *ctrls;
1508 idx = get_ctrl(gspca_dev, ctrl->id);
1511 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1513 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1514 return -ERESTARTSYS;
1515 if (!gspca_dev->present) {
1519 gspca_dev->usb_err = 0;
1520 if (ctrls->get != NULL) {
1521 ret = ctrls->get(gspca_dev, &ctrl->value);
1524 if (gspca_dev->cam.ctrls != NULL)
1525 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1528 mutex_unlock(&gspca_dev->usb_lock);
1532 static int vidioc_querymenu(struct file *file, void *priv,
1533 struct v4l2_querymenu *qmenu)
1535 struct gspca_dev *gspca_dev = priv;
1537 if (!gspca_dev->sd_desc->querymenu)
1539 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1542 static int vidioc_enum_input(struct file *file, void *priv,
1543 struct v4l2_input *input)
1545 struct gspca_dev *gspca_dev = priv;
1547 if (input->index != 0)
1549 input->type = V4L2_INPUT_TYPE_CAMERA;
1550 input->status = gspca_dev->cam.input_flags;
1551 strlcpy(input->name, gspca_dev->sd_desc->name,
1552 sizeof input->name);
1556 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1562 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1569 static int vidioc_reqbufs(struct file *file, void *priv,
1570 struct v4l2_requestbuffers *rb)
1572 struct gspca_dev *gspca_dev = priv;
1573 int i, ret = 0, streaming;
1575 i = rb->memory; /* (avoid compilation warning) */
1577 case GSPCA_MEMORY_READ: /* (internal call) */
1578 case V4L2_MEMORY_MMAP:
1579 case V4L2_MEMORY_USERPTR:
1584 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1585 return -ERESTARTSYS;
1587 if (gspca_dev->memory != GSPCA_MEMORY_NO
1588 && gspca_dev->memory != GSPCA_MEMORY_READ
1589 && gspca_dev->memory != rb->memory) {
1594 /* only one file may do the capture */
1595 if (gspca_dev->capt_file != NULL
1596 && gspca_dev->capt_file != file) {
1601 /* if allocated, the buffers must not be mapped */
1602 for (i = 0; i < gspca_dev->nframes; i++) {
1603 if (gspca_dev->frame[i].vma_use_count) {
1609 /* stop streaming */
1610 streaming = gspca_dev->streaming;
1612 mutex_lock(&gspca_dev->usb_lock);
1613 gspca_dev->usb_err = 0;
1614 gspca_stream_off(gspca_dev);
1615 mutex_unlock(&gspca_dev->usb_lock);
1617 /* Don't restart the stream when switching from read
1619 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1623 /* free the previous allocated buffers, if any */
1624 if (gspca_dev->nframes != 0)
1625 frame_free(gspca_dev);
1626 if (rb->count == 0) /* unrequest */
1628 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1630 rb->count = gspca_dev->nframes;
1632 ret = gspca_init_transfer(gspca_dev);
1635 mutex_unlock(&gspca_dev->queue_lock);
1636 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1640 static int vidioc_querybuf(struct file *file, void *priv,
1641 struct v4l2_buffer *v4l2_buf)
1643 struct gspca_dev *gspca_dev = priv;
1644 struct gspca_frame *frame;
1646 if (v4l2_buf->index < 0
1647 || v4l2_buf->index >= gspca_dev->nframes)
1650 frame = &gspca_dev->frame[v4l2_buf->index];
1651 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1655 static int vidioc_streamon(struct file *file, void *priv,
1656 enum v4l2_buf_type buf_type)
1658 struct gspca_dev *gspca_dev = priv;
1661 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1663 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1664 return -ERESTARTSYS;
1666 /* check the capture file */
1667 if (gspca_dev->capt_file != file) {
1672 if (gspca_dev->nframes == 0
1673 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1677 if (!gspca_dev->streaming) {
1678 ret = gspca_init_transfer(gspca_dev);
1683 if (gspca_debug & D_STREAM) {
1684 PDEBUG_MODE("stream on OK",
1692 mutex_unlock(&gspca_dev->queue_lock);
1696 static int vidioc_streamoff(struct file *file, void *priv,
1697 enum v4l2_buf_type buf_type)
1699 struct gspca_dev *gspca_dev = priv;
1702 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1705 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1706 return -ERESTARTSYS;
1708 if (!gspca_dev->streaming) {
1713 /* check the capture file */
1714 if (gspca_dev->capt_file != file) {
1719 /* stop streaming */
1720 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1724 gspca_dev->usb_err = 0;
1725 gspca_stream_off(gspca_dev);
1726 mutex_unlock(&gspca_dev->usb_lock);
1727 /* In case another thread is waiting in dqbuf */
1728 wake_up_interruptible(&gspca_dev->wq);
1730 /* empty the transfer queues */
1731 for (i = 0; i < gspca_dev->nframes; i++)
1732 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1733 atomic_set(&gspca_dev->fr_q, 0);
1734 atomic_set(&gspca_dev->fr_i, 0);
1735 gspca_dev->fr_o = 0;
1738 mutex_unlock(&gspca_dev->queue_lock);
1742 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1743 struct v4l2_jpegcompression *jpegcomp)
1745 struct gspca_dev *gspca_dev = priv;
1748 if (!gspca_dev->sd_desc->get_jcomp)
1750 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1751 return -ERESTARTSYS;
1752 gspca_dev->usb_err = 0;
1753 if (gspca_dev->present)
1754 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1757 mutex_unlock(&gspca_dev->usb_lock);
1761 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1762 struct v4l2_jpegcompression *jpegcomp)
1764 struct gspca_dev *gspca_dev = priv;
1767 if (!gspca_dev->sd_desc->set_jcomp)
1769 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1770 return -ERESTARTSYS;
1771 gspca_dev->usb_err = 0;
1772 if (gspca_dev->present)
1773 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1776 mutex_unlock(&gspca_dev->usb_lock);
1780 static int vidioc_g_parm(struct file *filp, void *priv,
1781 struct v4l2_streamparm *parm)
1783 struct gspca_dev *gspca_dev = priv;
1785 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1787 if (gspca_dev->sd_desc->get_streamparm) {
1790 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1791 return -ERESTARTSYS;
1792 if (gspca_dev->present) {
1793 gspca_dev->usb_err = 0;
1794 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1795 ret = gspca_dev->usb_err;
1799 mutex_unlock(&gspca_dev->usb_lock);
1806 static int vidioc_s_parm(struct file *filp, void *priv,
1807 struct v4l2_streamparm *parm)
1809 struct gspca_dev *gspca_dev = priv;
1812 n = parm->parm.capture.readbuffers;
1813 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1814 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1816 gspca_dev->nbufread = n;
1818 if (gspca_dev->sd_desc->set_streamparm) {
1821 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1822 return -ERESTARTSYS;
1823 if (gspca_dev->present) {
1824 gspca_dev->usb_err = 0;
1825 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1826 ret = gspca_dev->usb_err;
1830 mutex_unlock(&gspca_dev->usb_lock);
1837 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1839 struct gspca_dev *gspca_dev = file->private_data;
1840 struct gspca_frame *frame;
1842 unsigned long addr, start, size;
1845 start = vma->vm_start;
1846 size = vma->vm_end - vma->vm_start;
1847 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1849 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1850 return -ERESTARTSYS;
1851 if (!gspca_dev->present) {
1855 if (gspca_dev->capt_file != file) {
1861 for (i = 0; i < gspca_dev->nframes; ++i) {
1862 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1863 PDEBUG(D_STREAM, "mmap bad memory type");
1866 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1868 frame = &gspca_dev->frame[i];
1872 if (frame == NULL) {
1873 PDEBUG(D_STREAM, "mmap no frame buffer found");
1877 if (size != frame->v4l2_buf.length) {
1878 PDEBUG(D_STREAM, "mmap bad size");
1884 * - VM_IO marks the area as being a mmaped region for I/O to a
1885 * device. It also prevents the region from being core dumped.
1887 vma->vm_flags |= VM_IO;
1889 addr = (unsigned long) frame->data;
1891 page = vmalloc_to_page((void *) addr);
1892 ret = vm_insert_page(vma, start, page);
1900 vma->vm_ops = &gspca_vm_ops;
1901 vma->vm_private_data = frame;
1905 mutex_unlock(&gspca_dev->queue_lock);
1909 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1910 enum v4l2_memory memory)
1912 if (!gspca_dev->present)
1914 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1915 !gspca_dev->streaming)
1918 /* check if a frame is ready */
1919 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1922 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1923 enum v4l2_memory memory)
1927 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1928 return -ERESTARTSYS;
1929 ret = frame_ready_nolock(gspca_dev, file, memory);
1930 mutex_unlock(&gspca_dev->queue_lock);
1935 * dequeue a video buffer
1937 * If nonblock_ing is false, block until a buffer is available.
1939 static int vidioc_dqbuf(struct file *file, void *priv,
1940 struct v4l2_buffer *v4l2_buf)
1942 struct gspca_dev *gspca_dev = priv;
1943 struct gspca_frame *frame;
1946 PDEBUG(D_FRAM, "dqbuf");
1948 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1949 return -ERESTARTSYS;
1952 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1958 mutex_unlock(&gspca_dev->queue_lock);
1960 if (file->f_flags & O_NONBLOCK)
1963 /* wait till a frame is ready */
1964 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1965 frame_ready(gspca_dev, file, v4l2_buf->memory),
1966 msecs_to_jiffies(3000));
1972 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1973 return -ERESTARTSYS;
1976 i = gspca_dev->fr_o;
1977 j = gspca_dev->fr_queue[i];
1978 frame = &gspca_dev->frame[j];
1980 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1982 if (gspca_dev->sd_desc->dq_callback) {
1983 mutex_lock(&gspca_dev->usb_lock);
1984 gspca_dev->usb_err = 0;
1985 if (gspca_dev->present)
1986 gspca_dev->sd_desc->dq_callback(gspca_dev);
1987 mutex_unlock(&gspca_dev->usb_lock);
1990 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1991 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1992 PDEBUG(D_FRAM, "dqbuf %d", j);
1995 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1996 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1998 frame->v4l2_buf.bytesused)) {
1999 PDEBUG(D_ERR|D_STREAM,
2000 "dqbuf cp to user failed");
2005 mutex_unlock(&gspca_dev->queue_lock);
2010 * queue a video buffer
2012 * Attempting to queue a buffer that has already been
2013 * queued will return -EINVAL.
2015 static int vidioc_qbuf(struct file *file, void *priv,
2016 struct v4l2_buffer *v4l2_buf)
2018 struct gspca_dev *gspca_dev = priv;
2019 struct gspca_frame *frame;
2022 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
2024 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
2025 return -ERESTARTSYS;
2027 index = v4l2_buf->index;
2028 if ((unsigned) index >= gspca_dev->nframes) {
2030 "qbuf idx %d >= %d", index, gspca_dev->nframes);
2034 if (v4l2_buf->memory != gspca_dev->memory) {
2035 PDEBUG(D_FRAM, "qbuf bad memory type");
2040 frame = &gspca_dev->frame[index];
2041 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
2042 PDEBUG(D_FRAM, "qbuf bad state");
2047 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
2049 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
2050 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2051 frame->v4l2_buf.length = v4l2_buf->length;
2054 /* put the buffer in the 'queued' queue */
2055 i = atomic_read(&gspca_dev->fr_q);
2056 gspca_dev->fr_queue[i] = index;
2057 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
2059 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2060 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2063 mutex_unlock(&gspca_dev->queue_lock);
2068 * allocate the resources for read()
2070 static int read_alloc(struct gspca_dev *gspca_dev,
2073 struct v4l2_buffer v4l2_buf;
2076 PDEBUG(D_STREAM, "read alloc");
2077 if (gspca_dev->nframes == 0) {
2078 struct v4l2_requestbuffers rb;
2080 memset(&rb, 0, sizeof rb);
2081 rb.count = gspca_dev->nbufread;
2082 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2083 rb.memory = GSPCA_MEMORY_READ;
2084 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2086 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2089 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2090 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2091 v4l2_buf.memory = GSPCA_MEMORY_READ;
2092 for (i = 0; i < gspca_dev->nbufread; i++) {
2094 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2096 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2100 gspca_dev->memory = GSPCA_MEMORY_READ;
2103 /* start streaming */
2104 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2106 PDEBUG(D_STREAM, "read streamon err %d", ret);
2110 static unsigned int dev_poll(struct file *file, poll_table *wait)
2112 struct gspca_dev *gspca_dev = file->private_data;
2115 PDEBUG(D_FRAM, "poll");
2117 poll_wait(file, &gspca_dev->wq, wait);
2119 /* if reqbufs is not done, the user would use read() */
2120 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
2121 ret = read_alloc(gspca_dev, file);
2126 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2129 /* check if an image has been received */
2130 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2131 ret = POLLIN | POLLRDNORM; /* yes */
2134 mutex_unlock(&gspca_dev->queue_lock);
2135 if (!gspca_dev->present)
2140 static ssize_t dev_read(struct file *file, char __user *data,
2141 size_t count, loff_t *ppos)
2143 struct gspca_dev *gspca_dev = file->private_data;
2144 struct gspca_frame *frame;
2145 struct v4l2_buffer v4l2_buf;
2146 struct timeval timestamp;
2149 PDEBUG(D_FRAM, "read (%zd)", count);
2150 if (!gspca_dev->present)
2152 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
2153 ret = read_alloc(gspca_dev, file);
2159 timestamp = ktime_to_timeval(ktime_get());
2163 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2164 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2165 v4l2_buf.memory = GSPCA_MEMORY_READ;
2166 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2168 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2172 /* if the process slept for more than 1 second,
2173 * get a newer frame */
2174 frame = &gspca_dev->frame[v4l2_buf.index];
2176 break; /* avoid infinite loop */
2177 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2179 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2181 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2186 /* copy the frame */
2187 if (count > frame->v4l2_buf.bytesused)
2188 count = frame->v4l2_buf.bytesused;
2189 ret = copy_to_user(data, frame->data, count);
2191 PDEBUG(D_ERR|D_STREAM,
2192 "read cp to user lack %d / %zd", ret, count);
2198 /* in each case, requeue the buffer */
2199 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2205 static struct v4l2_file_operations dev_fops = {
2206 .owner = THIS_MODULE,
2208 .release = dev_close,
2211 .unlocked_ioctl = video_ioctl2,
2215 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2216 .vidioc_querycap = vidioc_querycap,
2217 .vidioc_dqbuf = vidioc_dqbuf,
2218 .vidioc_qbuf = vidioc_qbuf,
2219 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2220 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2221 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2222 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2223 .vidioc_streamon = vidioc_streamon,
2224 .vidioc_queryctrl = vidioc_queryctrl,
2225 .vidioc_g_ctrl = vidioc_g_ctrl,
2226 .vidioc_s_ctrl = vidioc_s_ctrl,
2227 .vidioc_querymenu = vidioc_querymenu,
2228 .vidioc_enum_input = vidioc_enum_input,
2229 .vidioc_g_input = vidioc_g_input,
2230 .vidioc_s_input = vidioc_s_input,
2231 .vidioc_reqbufs = vidioc_reqbufs,
2232 .vidioc_querybuf = vidioc_querybuf,
2233 .vidioc_streamoff = vidioc_streamoff,
2234 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2235 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2236 .vidioc_g_parm = vidioc_g_parm,
2237 .vidioc_s_parm = vidioc_s_parm,
2238 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2239 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2240 #ifdef CONFIG_VIDEO_ADV_DEBUG
2241 .vidioc_g_register = vidioc_g_register,
2242 .vidioc_s_register = vidioc_s_register,
2244 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2247 static const struct video_device gspca_template = {
2248 .name = "gspca main driver",
2250 .ioctl_ops = &dev_ioctl_ops,
2251 .release = gspca_release,
2254 /* initialize the controls */
2255 static void ctrls_init(struct gspca_dev *gspca_dev)
2257 struct gspca_ctrl *ctrl;
2260 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2261 i < gspca_dev->sd_desc->nctrls;
2263 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2264 ctrl->val = ctrl->def;
2265 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2266 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2271 * probe and create a new gspca device
2273 * This function must be called by the sub-driver when it is
2274 * called for probing a new device.
2276 int gspca_dev_probe2(struct usb_interface *intf,
2277 const struct usb_device_id *id,
2278 const struct sd_desc *sd_desc,
2280 struct module *module)
2282 struct gspca_dev *gspca_dev;
2283 struct usb_device *dev = interface_to_usbdev(intf);
2286 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2287 sd_desc->name, id->idVendor, id->idProduct);
2289 /* create the device */
2290 if (dev_size < sizeof *gspca_dev)
2291 dev_size = sizeof *gspca_dev;
2292 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2294 pr_err("couldn't kzalloc gspca struct\n");
2297 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2298 if (!gspca_dev->usb_buf) {
2299 pr_err("out of memory\n");
2303 gspca_dev->dev = dev;
2304 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2305 gspca_dev->nbalt = intf->num_altsetting;
2307 /* check if any audio device */
2308 if (dev->config->desc.bNumInterfaces != 1) {
2310 struct usb_interface *intf2;
2312 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2313 intf2 = dev->config->interface[i];
2315 && intf2->altsetting != NULL
2316 && intf2->altsetting->desc.bInterfaceClass ==
2318 gspca_dev->audio = 1;
2324 gspca_dev->sd_desc = sd_desc;
2325 gspca_dev->nbufread = 2;
2326 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2328 /* configure the subdriver and initialize the USB device */
2329 ret = sd_desc->config(gspca_dev, id);
2332 if (gspca_dev->cam.ctrls != NULL)
2333 ctrls_init(gspca_dev);
2334 ret = sd_desc->init(gspca_dev);
2337 gspca_set_default_mode(gspca_dev);
2339 ret = gspca_input_connect(gspca_dev);
2343 mutex_init(&gspca_dev->usb_lock);
2344 mutex_init(&gspca_dev->queue_lock);
2345 init_waitqueue_head(&gspca_dev->wq);
2347 /* init video stuff */
2348 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2349 gspca_dev->vdev.parent = &intf->dev;
2350 gspca_dev->module = module;
2351 gspca_dev->present = 1;
2352 ret = video_register_device(&gspca_dev->vdev,
2356 pr_err("video_register_device err %d\n", ret);
2360 usb_set_intfdata(intf, gspca_dev);
2361 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2363 gspca_input_create_urb(gspca_dev);
2367 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2368 if (gspca_dev->input_dev)
2369 input_unregister_device(gspca_dev->input_dev);
2371 kfree(gspca_dev->usb_buf);
2375 EXPORT_SYMBOL(gspca_dev_probe2);
2377 /* same function as the previous one, but check the interface */
2378 int gspca_dev_probe(struct usb_interface *intf,
2379 const struct usb_device_id *id,
2380 const struct sd_desc *sd_desc,
2382 struct module *module)
2384 struct usb_device *dev = interface_to_usbdev(intf);
2386 /* we don't handle multi-config cameras */
2387 if (dev->descriptor.bNumConfigurations != 1) {
2388 pr_err("%04x:%04x too many config\n",
2389 id->idVendor, id->idProduct);
2393 /* the USB video interface must be the first one */
2394 if (dev->config->desc.bNumInterfaces != 1
2395 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2398 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2400 EXPORT_SYMBOL(gspca_dev_probe);
2405 * This function must be called by the sub-driver
2406 * when the device disconnects, after the specific resources are freed.
2408 void gspca_disconnect(struct usb_interface *intf)
2410 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2411 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2412 struct input_dev *input_dev;
2415 PDEBUG(D_PROBE, "%s disconnect",
2416 video_device_node_name(&gspca_dev->vdev));
2417 mutex_lock(&gspca_dev->usb_lock);
2419 gspca_dev->present = 0;
2420 wake_up_interruptible(&gspca_dev->wq);
2422 destroy_urbs(gspca_dev);
2424 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2425 gspca_input_destroy_urb(gspca_dev);
2426 input_dev = gspca_dev->input_dev;
2428 gspca_dev->input_dev = NULL;
2429 input_unregister_device(input_dev);
2433 /* the device is freed at exit of this function */
2434 gspca_dev->dev = NULL;
2435 mutex_unlock(&gspca_dev->usb_lock);
2437 usb_set_intfdata(intf, NULL);
2439 /* release the device */
2440 /* (this will call gspca_release() immediately or on last close) */
2441 video_unregister_device(&gspca_dev->vdev);
2443 /* PDEBUG(D_PROBE, "disconnect complete"); */
2445 EXPORT_SYMBOL(gspca_disconnect);
2448 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2450 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2452 if (!gspca_dev->streaming)
2454 gspca_dev->frozen = 1; /* avoid urb error messages */
2455 if (gspca_dev->sd_desc->stopN)
2456 gspca_dev->sd_desc->stopN(gspca_dev);
2457 destroy_urbs(gspca_dev);
2458 gspca_input_destroy_urb(gspca_dev);
2459 gspca_set_alt0(gspca_dev);
2460 if (gspca_dev->sd_desc->stop0)
2461 gspca_dev->sd_desc->stop0(gspca_dev);
2464 EXPORT_SYMBOL(gspca_suspend);
2466 int gspca_resume(struct usb_interface *intf)
2468 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2470 gspca_dev->frozen = 0;
2471 gspca_dev->sd_desc->init(gspca_dev);
2472 gspca_input_create_urb(gspca_dev);
2473 if (gspca_dev->streaming)
2474 return gspca_init_transfer(gspca_dev);
2477 EXPORT_SYMBOL(gspca_resume);
2479 /* -- cam driver utility functions -- */
2481 /* auto gain and exposure algorithm based on the knee algorithm described here:
2482 http://ytse.tricolour.net/docs/LowLightOptimization.html
2484 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2486 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2487 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2489 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2490 const struct ctrl *gain_ctrl = NULL;
2491 const struct ctrl *exposure_ctrl = NULL;
2492 const struct ctrl *autogain_ctrl = NULL;
2495 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2496 if (gspca_dev->ctrl_dis & (1 << i))
2498 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2499 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2500 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2501 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2502 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2503 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2505 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2506 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2507 "on cam without (auto)gain/exposure");
2511 if (gain_ctrl->get(gspca_dev, &gain) ||
2512 exposure_ctrl->get(gspca_dev, &exposure) ||
2513 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2517 orig_exposure = exposure;
2519 /* If we are of a multiple of deadzone, do multiple steps to reach the
2520 desired lumination fast (with the risc of a slight overshoot) */
2521 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2523 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2524 avg_lum, desired_avg_lum, steps);
2526 for (i = 0; i < steps; i++) {
2527 if (avg_lum > desired_avg_lum) {
2528 if (gain > gain_knee)
2530 else if (exposure > exposure_knee)
2532 else if (gain > gain_ctrl->qctrl.default_value)
2534 else if (exposure > exposure_ctrl->qctrl.minimum)
2536 else if (gain > gain_ctrl->qctrl.minimum)
2541 if (gain < gain_ctrl->qctrl.default_value)
2543 else if (exposure < exposure_knee)
2545 else if (gain < gain_knee)
2547 else if (exposure < exposure_ctrl->qctrl.maximum)
2549 else if (gain < gain_ctrl->qctrl.maximum)
2556 if (gain != orig_gain) {
2557 gain_ctrl->set(gspca_dev, gain);
2560 if (exposure != orig_exposure) {
2561 exposure_ctrl->set(gspca_dev, exposure);
2567 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2569 /* -- module insert / remove -- */
2570 static int __init gspca_init(void)
2572 pr_info("v" GSPCA_VERSION " registered\n");
2575 static void __exit gspca_exit(void)
2579 module_init(gspca_init);
2580 module_exit(gspca_exit);
2583 module_param_named(debug, gspca_debug, int, 0644);
2584 MODULE_PARM_DESC(debug,
2585 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2586 " 0x08:stream 0x10:frame 0x20:packet"