Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / media / video / gspca / gspca.c
blob5da4879f47f2c3e01342479add75a04332e48fa8
1 /*
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
17 * for more details.
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 MODULE_NAME "gspca"
26 #include <linux/init.h>
27 #include <linux/fs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/pagemap.h>
34 #include <linux/io.h>
35 #include <asm/page.h>
36 #include <linux/uaccess.h>
37 #include <linux/ktime.h>
38 #include <media/v4l2-ioctl.h>
40 #include "gspca.h"
42 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
43 #include <linux/input.h>
44 #include <linux/usb/input.h>
45 #endif
47 /* global values */
48 #define DEF_NURBS 3 /* default number of URBs */
49 #if DEF_NURBS > MAX_NURBS
50 #error "DEF_NURBS too big"
51 #endif
53 #define DRIVER_VERSION_NUMBER "2.13.0"
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(DRIVER_VERSION_NUMBER);
60 #ifdef GSPCA_DEBUG
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",
68 txt,
69 pixfmt & 0xff,
70 (pixfmt >> 8) & 0xff,
71 (pixfmt >> 16) & 0xff,
72 pixfmt >> 24,
73 w, h);
74 } else {
75 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
76 txt,
77 pixfmt,
78 w, h);
81 #else
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
83 #endif
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)
92 * VMA operations.
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;
122 int ret;
124 ret = urb->status;
125 switch (ret) {
126 case 0:
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");
131 break;
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
140 break;
142 default:
143 PDEBUG(D_ERR, "URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
151 err("Resubmit URB failed with error %i", ret);
155 static int gspca_input_connect(struct gspca_dev *dev)
157 struct input_dev *input_dev;
158 int err = 0;
160 dev->input_dev = NULL;
161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
162 input_dev = input_allocate_device();
163 if (!input_dev)
164 return -ENOMEM;
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);
179 if (err) {
180 err("Input device registration failed with error %i",
181 err);
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
189 return err;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
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);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
216 buffer = usb_alloc_coherent(dev, buffer_len,
217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
229 PDEBUG(D_ERR, "submit int URB failed with error %i", ret);
230 goto error_submit;
232 gspca_dev->int_urb = urb;
233 return ret;
235 error_submit:
236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
239 urb->transfer_dma);
240 error_buffer:
241 usb_free_urb(urb);
242 error:
243 return ret;
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;
251 int i;
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);
262 break;
268 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
270 struct urb *urb;
272 urb = gspca_dev->int_urb;
273 if (urb) {
274 gspca_dev->int_urb = NULL;
275 usb_kill_urb(urb);
276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
279 urb->transfer_dma);
280 usb_free_urb(urb);
283 #else
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)
294 return 0;
296 #endif
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev *gspca_dev,
302 struct urb *urb)
304 u8 *data; /* address of data in the iso message */
305 int i, len, st;
306 cam_pkt_op pkt_scan;
308 if (urb->status != 0) {
309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
311 #ifdef CONFIG_PM
312 if (gspca_dev->frozen)
313 return;
314 #endif
315 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
316 urb->status = 0;
317 goto resubmit;
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;
325 if (st) {
326 err("ISOC data error: [%d] len=%d, status=%d",
327 i, len, st);
328 gspca_dev->last_packet_type = DISCARD_PACKET;
329 continue;
331 if (len == 0) {
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
334 continue;
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);
345 resubmit:
346 /* resubmit the URB */
347 st = usb_submit_urb(urb, GFP_ATOMIC);
348 if (st < 0)
349 err("usb_submit_urb() ret %d", 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)
363 return;
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;
373 int st;
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
377 return;
378 switch (urb->status) {
379 case 0:
380 break;
381 case -ESHUTDOWN:
382 return; /* disconnection */
383 default:
384 #ifdef CONFIG_PM
385 if (gspca_dev->frozen)
386 return;
387 #endif
388 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
389 urb->status = 0;
390 goto resubmit;
393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
395 urb->transfer_buffer,
396 urb->actual_length);
398 resubmit:
399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
402 if (st < 0)
403 err("usb_submit_urb() ret %d", 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
413 * - one FIRST_PACKET
414 * - 0 or many INTER_PACKETs
415 * - one LAST_PACKET
416 * DISCARD_PACKET invalidates the whole frame.
418 void gspca_frame_add(struct gspca_dev *gspca_dev,
419 enum gspca_packet_type packet_type,
420 const u8 *data,
421 int len)
423 struct gspca_frame *frame;
424 int i, j;
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++;
435 return;
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;
443 } else {
444 switch (gspca_dev->last_packet_type) {
445 case DISCARD_PACKET:
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;
451 return;
452 case LAST_PACKET:
453 return;
457 /* append the packet to the frame buffer */
458 if (len > 0) {
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,
462 gspca_dev->frsz);
463 packet_type = DISCARD_PACKET;
464 } else {
465 /* !! image is NULL only when last pkt is LAST or DISCARD
466 if (gspca_dev->image == NULL) {
467 err("gspca_frame_add() image == NULL");
468 return;
471 memcpy(gspca_dev->image + gspca_dev->image_len,
472 data, 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
480 * in the queue */
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 gspca_is_compressed(__u32 format)
502 switch (format) {
503 case V4L2_PIX_FMT_MJPEG:
504 case V4L2_PIX_FMT_JPEG:
505 case V4L2_PIX_FMT_SPCA561:
506 case V4L2_PIX_FMT_PAC207:
507 case V4L2_PIX_FMT_MR97310A:
508 return 1;
510 return 0;
513 static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
514 enum v4l2_memory memory, unsigned int count)
516 struct gspca_frame *frame;
517 unsigned int frsz;
518 int i;
520 i = gspca_dev->curr_mode;
521 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
522 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
523 frsz = PAGE_ALIGN(frsz);
524 if (count >= GSPCA_MAX_FRAMES)
525 count = GSPCA_MAX_FRAMES - 1;
526 gspca_dev->frbuf = vmalloc_32(frsz * count);
527 if (!gspca_dev->frbuf) {
528 err("frame alloc failed");
529 return -ENOMEM;
531 gspca_dev->capt_file = file;
532 gspca_dev->memory = memory;
533 gspca_dev->frsz = frsz;
534 gspca_dev->nframes = count;
535 for (i = 0; i < count; i++) {
536 frame = &gspca_dev->frame[i];
537 frame->v4l2_buf.index = i;
538 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
539 frame->v4l2_buf.flags = 0;
540 frame->v4l2_buf.field = V4L2_FIELD_NONE;
541 frame->v4l2_buf.length = frsz;
542 frame->v4l2_buf.memory = memory;
543 frame->v4l2_buf.sequence = 0;
544 frame->data = gspca_dev->frbuf + i * frsz;
545 frame->v4l2_buf.m.offset = i * frsz;
547 atomic_set(&gspca_dev->fr_q, 0);
548 atomic_set(&gspca_dev->fr_i, 0);
549 gspca_dev->fr_o = 0;
550 return 0;
553 static void frame_free(struct gspca_dev *gspca_dev)
555 int i;
557 PDEBUG(D_STREAM, "frame free");
558 if (gspca_dev->frbuf != NULL) {
559 vfree(gspca_dev->frbuf);
560 gspca_dev->frbuf = NULL;
561 for (i = 0; i < gspca_dev->nframes; i++)
562 gspca_dev->frame[i].data = NULL;
564 gspca_dev->nframes = 0;
565 gspca_dev->frsz = 0;
566 gspca_dev->capt_file = NULL;
567 gspca_dev->memory = GSPCA_MEMORY_NO;
570 static void destroy_urbs(struct gspca_dev *gspca_dev)
572 struct urb *urb;
573 unsigned int i;
575 PDEBUG(D_STREAM, "kill transfer");
576 for (i = 0; i < MAX_NURBS; i++) {
577 urb = gspca_dev->urb[i];
578 if (urb == NULL)
579 break;
581 gspca_dev->urb[i] = NULL;
582 usb_kill_urb(urb);
583 if (urb->transfer_buffer != NULL)
584 usb_free_coherent(gspca_dev->dev,
585 urb->transfer_buffer_length,
586 urb->transfer_buffer,
587 urb->transfer_dma);
588 usb_free_urb(urb);
592 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
594 int ret;
596 if (gspca_dev->alt == 0)
597 return 0;
598 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
599 if (ret < 0)
600 err("set alt 0 err %d", ret);
601 return ret;
604 /* Note: both the queue and the usb locks should be held when calling this */
605 static void gspca_stream_off(struct gspca_dev *gspca_dev)
607 gspca_dev->streaming = 0;
608 if (gspca_dev->present) {
609 if (gspca_dev->sd_desc->stopN)
610 gspca_dev->sd_desc->stopN(gspca_dev);
611 destroy_urbs(gspca_dev);
612 gspca_input_destroy_urb(gspca_dev);
613 gspca_set_alt0(gspca_dev);
614 gspca_input_create_urb(gspca_dev);
617 /* always call stop0 to free the subdriver's resources */
618 if (gspca_dev->sd_desc->stop0)
619 gspca_dev->sd_desc->stop0(gspca_dev);
620 PDEBUG(D_STREAM, "stream off OK");
624 * look for an input transfer endpoint in an alternate setting
626 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
627 int xfer)
629 struct usb_host_endpoint *ep;
630 int i, attr;
632 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
633 ep = &alt->endpoint[i];
634 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
635 if (attr == xfer
636 && ep->desc.wMaxPacketSize != 0
637 && usb_endpoint_dir_in(&ep->desc))
638 return ep;
640 return NULL;
644 * look for an input (isoc or bulk) endpoint
646 * The endpoint is defined by the subdriver.
647 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
648 * This routine may be called many times when the bandwidth is too small
649 * (the bandwidth is checked on urb submit).
651 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
653 struct usb_interface *intf;
654 struct usb_host_endpoint *ep;
655 int xfer, i, ret;
657 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
658 ep = NULL;
659 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
660 : USB_ENDPOINT_XFER_ISOC;
661 i = gspca_dev->alt; /* previous alt setting */
662 if (gspca_dev->cam.reverse_alts) {
663 while (++i < gspca_dev->nbalt) {
664 ep = alt_xfer(&intf->altsetting[i], xfer);
665 if (ep)
666 break;
668 } else {
669 while (--i >= 0) {
670 ep = alt_xfer(&intf->altsetting[i], xfer);
671 if (ep)
672 break;
675 if (ep == NULL) {
676 err("no transfer endpoint found");
677 return NULL;
679 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
680 i, ep->desc.bEndpointAddress);
681 gspca_dev->alt = i; /* memorize the current alt setting */
682 if (gspca_dev->nbalt > 1) {
683 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
684 if (ret < 0) {
685 err("set alt %d err %d", i, ret);
686 ep = NULL;
689 return ep;
693 * create the URBs for image transfer
695 static int create_urbs(struct gspca_dev *gspca_dev,
696 struct usb_host_endpoint *ep)
698 struct urb *urb;
699 int n, nurbs, i, psize, npkt, bsize;
701 /* calculate the packet size and the number of packets */
702 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
704 if (!gspca_dev->cam.bulk) { /* isoc */
706 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
707 if (gspca_dev->pkt_size == 0)
708 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
709 else
710 psize = gspca_dev->pkt_size;
711 npkt = gspca_dev->cam.npkt;
712 if (npkt == 0)
713 npkt = 32; /* default value */
714 bsize = psize * npkt;
715 PDEBUG(D_STREAM,
716 "isoc %d pkts size %d = bsize:%d",
717 npkt, psize, bsize);
718 nurbs = DEF_NURBS;
719 } else { /* bulk */
720 npkt = 0;
721 bsize = gspca_dev->cam.bulk_size;
722 if (bsize == 0)
723 bsize = psize;
724 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
725 if (gspca_dev->cam.bulk_nurbs != 0)
726 nurbs = gspca_dev->cam.bulk_nurbs;
727 else
728 nurbs = 1;
731 for (n = 0; n < nurbs; n++) {
732 urb = usb_alloc_urb(npkt, GFP_KERNEL);
733 if (!urb) {
734 err("usb_alloc_urb failed");
735 return -ENOMEM;
737 gspca_dev->urb[n] = urb;
738 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
739 bsize,
740 GFP_KERNEL,
741 &urb->transfer_dma);
743 if (urb->transfer_buffer == NULL) {
744 err("usb_alloc_coherent failed");
745 return -ENOMEM;
747 urb->dev = gspca_dev->dev;
748 urb->context = gspca_dev;
749 urb->transfer_buffer_length = bsize;
750 if (npkt != 0) { /* ISOC */
751 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
752 ep->desc.bEndpointAddress);
753 urb->transfer_flags = URB_ISO_ASAP
754 | URB_NO_TRANSFER_DMA_MAP;
755 urb->interval = ep->desc.bInterval;
756 urb->complete = isoc_irq;
757 urb->number_of_packets = npkt;
758 for (i = 0; i < npkt; i++) {
759 urb->iso_frame_desc[i].length = psize;
760 urb->iso_frame_desc[i].offset = psize * i;
762 } else { /* bulk */
763 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
764 ep->desc.bEndpointAddress);
765 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
766 urb->complete = bulk_irq;
769 return 0;
773 * start the USB transfer
775 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
777 struct usb_host_endpoint *ep;
778 struct urb *urb;
779 int n, ret;
781 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
782 return -ERESTARTSYS;
784 if (!gspca_dev->present) {
785 ret = -ENODEV;
786 goto unlock;
789 /* reset the streaming variables */
790 gspca_dev->image = NULL;
791 gspca_dev->image_len = 0;
792 gspca_dev->last_packet_type = DISCARD_PACKET;
793 gspca_dev->sequence = 0;
795 gspca_dev->usb_err = 0;
797 /* set the higher alternate setting and
798 * loop until urb submit succeeds */
799 if (gspca_dev->cam.reverse_alts)
800 gspca_dev->alt = 0;
801 else
802 gspca_dev->alt = gspca_dev->nbalt;
804 if (gspca_dev->sd_desc->isoc_init) {
805 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
806 if (ret < 0)
807 goto unlock;
810 gspca_input_destroy_urb(gspca_dev);
811 ep = get_ep(gspca_dev);
812 if (ep == NULL) {
813 ret = -EIO;
814 goto out;
816 for (;;) {
817 if (!gspca_dev->cam.no_urb_create) {
818 PDEBUG(D_STREAM, "init transfer alt %d",
819 gspca_dev->alt);
820 ret = create_urbs(gspca_dev, ep);
821 if (ret < 0) {
822 destroy_urbs(gspca_dev);
823 goto out;
827 /* clear the bulk endpoint */
828 if (gspca_dev->cam.bulk)
829 usb_clear_halt(gspca_dev->dev,
830 gspca_dev->urb[0]->pipe);
832 /* start the cam */
833 ret = gspca_dev->sd_desc->start(gspca_dev);
834 if (ret < 0) {
835 destroy_urbs(gspca_dev);
836 goto out;
838 gspca_dev->streaming = 1;
840 /* some bulk transfers are started by the subdriver */
841 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
842 break;
844 /* submit the URBs */
845 for (n = 0; n < MAX_NURBS; n++) {
846 urb = gspca_dev->urb[n];
847 if (urb == NULL)
848 break;
849 ret = usb_submit_urb(urb, GFP_KERNEL);
850 if (ret < 0)
851 break;
853 if (ret >= 0)
854 break;
855 gspca_stream_off(gspca_dev);
856 if (ret != -ENOSPC) {
857 err("usb_submit_urb alt %d err %d",
858 gspca_dev->alt, ret);
859 goto out;
862 /* the bandwidth is not wide enough
863 * negotiate or try a lower alternate setting */
864 PDEBUG(D_ERR|D_STREAM,
865 "bandwidth not wide enough - trying again");
866 msleep(20); /* wait for kill complete */
867 if (gspca_dev->sd_desc->isoc_nego) {
868 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
869 if (ret < 0)
870 goto out;
871 } else {
872 ep = get_ep(gspca_dev);
873 if (ep == NULL) {
874 ret = -EIO;
875 goto out;
879 out:
880 gspca_input_create_urb(gspca_dev);
881 unlock:
882 mutex_unlock(&gspca_dev->usb_lock);
883 return ret;
886 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
888 struct gspca_ctrl *ctrl;
889 int i;
891 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
892 gspca_dev->curr_mode = i;
893 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
894 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
895 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
897 /* set the current control values to their default values
898 * which may have changed in sd_init() */
899 ctrl = gspca_dev->cam.ctrls;
900 if (ctrl != NULL) {
901 for (i = 0;
902 i < gspca_dev->sd_desc->nctrls;
903 i++, ctrl++)
904 ctrl->val = ctrl->def;
908 static int wxh_to_mode(struct gspca_dev *gspca_dev,
909 int width, int height)
911 int i;
913 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
914 if (width >= gspca_dev->cam.cam_mode[i].width
915 && height >= gspca_dev->cam.cam_mode[i].height)
916 break;
918 return i;
922 * search a mode with the right pixel format
924 static int gspca_get_mode(struct gspca_dev *gspca_dev,
925 int mode,
926 int pixfmt)
928 int modeU, modeD;
930 modeU = modeD = mode;
931 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
932 if (--modeD >= 0) {
933 if (gspca_dev->cam.cam_mode[modeD].pixelformat
934 == pixfmt)
935 return modeD;
937 if (++modeU < gspca_dev->cam.nmodes) {
938 if (gspca_dev->cam.cam_mode[modeU].pixelformat
939 == pixfmt)
940 return modeU;
943 return -EINVAL;
946 #ifdef CONFIG_VIDEO_ADV_DEBUG
947 static int vidioc_g_register(struct file *file, void *priv,
948 struct v4l2_dbg_register *reg)
950 int ret;
951 struct gspca_dev *gspca_dev = priv;
953 if (!gspca_dev->sd_desc->get_chip_ident)
954 return -EINVAL;
956 if (!gspca_dev->sd_desc->get_register)
957 return -EINVAL;
959 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
960 return -ERESTARTSYS;
961 gspca_dev->usb_err = 0;
962 if (gspca_dev->present)
963 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
964 else
965 ret = -ENODEV;
966 mutex_unlock(&gspca_dev->usb_lock);
968 return ret;
971 static int vidioc_s_register(struct file *file, void *priv,
972 struct v4l2_dbg_register *reg)
974 int ret;
975 struct gspca_dev *gspca_dev = priv;
977 if (!gspca_dev->sd_desc->get_chip_ident)
978 return -EINVAL;
980 if (!gspca_dev->sd_desc->set_register)
981 return -EINVAL;
983 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
984 return -ERESTARTSYS;
985 gspca_dev->usb_err = 0;
986 if (gspca_dev->present)
987 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
988 else
989 ret = -ENODEV;
990 mutex_unlock(&gspca_dev->usb_lock);
992 return ret;
994 #endif
996 static int vidioc_g_chip_ident(struct file *file, void *priv,
997 struct v4l2_dbg_chip_ident *chip)
999 int ret;
1000 struct gspca_dev *gspca_dev = priv;
1002 if (!gspca_dev->sd_desc->get_chip_ident)
1003 return -EINVAL;
1005 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1006 return -ERESTARTSYS;
1007 gspca_dev->usb_err = 0;
1008 if (gspca_dev->present)
1009 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1010 else
1011 ret = -ENODEV;
1012 mutex_unlock(&gspca_dev->usb_lock);
1014 return ret;
1017 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1018 struct v4l2_fmtdesc *fmtdesc)
1020 struct gspca_dev *gspca_dev = priv;
1021 int i, j, index;
1022 __u32 fmt_tb[8];
1024 /* give an index to each format */
1025 index = 0;
1026 j = 0;
1027 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1028 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1029 j = 0;
1030 for (;;) {
1031 if (fmt_tb[j] == fmt_tb[index])
1032 break;
1033 j++;
1035 if (j == index) {
1036 if (fmtdesc->index == index)
1037 break; /* new format */
1038 index++;
1039 if (index >= ARRAY_SIZE(fmt_tb))
1040 return -EINVAL;
1043 if (i < 0)
1044 return -EINVAL; /* no more format */
1046 fmtdesc->pixelformat = fmt_tb[index];
1047 if (gspca_is_compressed(fmt_tb[index]))
1048 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1049 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1050 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1051 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1052 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1053 fmtdesc->description[4] = '\0';
1054 return 0;
1057 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1058 struct v4l2_format *fmt)
1060 struct gspca_dev *gspca_dev = priv;
1061 int mode;
1063 mode = gspca_dev->curr_mode;
1064 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1065 sizeof fmt->fmt.pix);
1066 return 0;
1069 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1070 struct v4l2_format *fmt)
1072 int w, h, mode, mode2;
1074 w = fmt->fmt.pix.width;
1075 h = fmt->fmt.pix.height;
1077 #ifdef GSPCA_DEBUG
1078 if (gspca_debug & D_CONF)
1079 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1080 #endif
1081 /* search the closest mode for width and height */
1082 mode = wxh_to_mode(gspca_dev, w, h);
1084 /* OK if right palette */
1085 if (gspca_dev->cam.cam_mode[mode].pixelformat
1086 != fmt->fmt.pix.pixelformat) {
1088 /* else, search the closest mode with the same pixel format */
1089 mode2 = gspca_get_mode(gspca_dev, mode,
1090 fmt->fmt.pix.pixelformat);
1091 if (mode2 >= 0)
1092 mode = mode2;
1093 /* else
1094 ; * no chance, return this mode */
1096 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1097 sizeof fmt->fmt.pix);
1098 return mode; /* used when s_fmt */
1101 static int vidioc_try_fmt_vid_cap(struct file *file,
1102 void *priv,
1103 struct v4l2_format *fmt)
1105 struct gspca_dev *gspca_dev = priv;
1106 int ret;
1108 ret = try_fmt_vid_cap(gspca_dev, fmt);
1109 if (ret < 0)
1110 return ret;
1111 return 0;
1114 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1115 struct v4l2_format *fmt)
1117 struct gspca_dev *gspca_dev = priv;
1118 int ret;
1120 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1121 return -ERESTARTSYS;
1123 ret = try_fmt_vid_cap(gspca_dev, fmt);
1124 if (ret < 0)
1125 goto out;
1127 if (gspca_dev->nframes != 0
1128 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1129 ret = -EINVAL;
1130 goto out;
1133 if (ret == gspca_dev->curr_mode) {
1134 ret = 0;
1135 goto out; /* same mode */
1138 if (gspca_dev->streaming) {
1139 ret = -EBUSY;
1140 goto out;
1142 gspca_dev->width = fmt->fmt.pix.width;
1143 gspca_dev->height = fmt->fmt.pix.height;
1144 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1145 gspca_dev->curr_mode = ret;
1147 ret = 0;
1148 out:
1149 mutex_unlock(&gspca_dev->queue_lock);
1150 return ret;
1153 static int vidioc_enum_framesizes(struct file *file, void *priv,
1154 struct v4l2_frmsizeenum *fsize)
1156 struct gspca_dev *gspca_dev = priv;
1157 int i;
1158 __u32 index = 0;
1160 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1161 if (fsize->pixel_format !=
1162 gspca_dev->cam.cam_mode[i].pixelformat)
1163 continue;
1165 if (fsize->index == index) {
1166 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1167 fsize->discrete.width =
1168 gspca_dev->cam.cam_mode[i].width;
1169 fsize->discrete.height =
1170 gspca_dev->cam.cam_mode[i].height;
1171 return 0;
1173 index++;
1176 return -EINVAL;
1179 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1180 struct v4l2_frmivalenum *fival)
1182 struct gspca_dev *gspca_dev = priv;
1183 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1184 __u32 i;
1186 if (gspca_dev->cam.mode_framerates == NULL ||
1187 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1188 return -EINVAL;
1190 if (fival->pixel_format !=
1191 gspca_dev->cam.cam_mode[mode].pixelformat)
1192 return -EINVAL;
1194 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1195 if (fival->index == i) {
1196 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1197 fival->discrete.numerator = 1;
1198 fival->discrete.denominator =
1199 gspca_dev->cam.mode_framerates[mode].rates[i];
1200 return 0;
1204 return -EINVAL;
1207 static void gspca_release(struct video_device *vfd)
1209 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1211 PDEBUG(D_PROBE, "%s released",
1212 video_device_node_name(&gspca_dev->vdev));
1214 kfree(gspca_dev->usb_buf);
1215 kfree(gspca_dev);
1218 static int dev_open(struct file *file)
1220 struct gspca_dev *gspca_dev;
1222 PDEBUG(D_STREAM, "[%s] open", current->comm);
1223 gspca_dev = (struct gspca_dev *) video_devdata(file);
1224 if (!gspca_dev->present)
1225 return -ENODEV;
1227 /* protect the subdriver against rmmod */
1228 if (!try_module_get(gspca_dev->module))
1229 return -ENODEV;
1231 file->private_data = gspca_dev;
1232 #ifdef GSPCA_DEBUG
1233 /* activate the v4l2 debug */
1234 if (gspca_debug & D_V4L2)
1235 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1236 | V4L2_DEBUG_IOCTL_ARG;
1237 else
1238 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1239 | V4L2_DEBUG_IOCTL_ARG);
1240 #endif
1241 return 0;
1244 static int dev_close(struct file *file)
1246 struct gspca_dev *gspca_dev = file->private_data;
1248 PDEBUG(D_STREAM, "[%s] close", current->comm);
1249 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1250 return -ERESTARTSYS;
1252 /* if the file did the capture, free the streaming resources */
1253 if (gspca_dev->capt_file == file) {
1254 if (gspca_dev->streaming) {
1255 mutex_lock(&gspca_dev->usb_lock);
1256 gspca_dev->usb_err = 0;
1257 gspca_stream_off(gspca_dev);
1258 mutex_unlock(&gspca_dev->usb_lock);
1260 frame_free(gspca_dev);
1262 file->private_data = NULL;
1263 module_put(gspca_dev->module);
1264 mutex_unlock(&gspca_dev->queue_lock);
1266 PDEBUG(D_STREAM, "close done");
1268 return 0;
1271 static int vidioc_querycap(struct file *file, void *priv,
1272 struct v4l2_capability *cap)
1274 struct gspca_dev *gspca_dev = priv;
1275 int ret;
1277 /* protect the access to the usb device */
1278 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1279 return -ERESTARTSYS;
1280 if (!gspca_dev->present) {
1281 ret = -ENODEV;
1282 goto out;
1284 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1285 sizeof cap->driver);
1286 if (gspca_dev->dev->product != NULL) {
1287 strlcpy((char *) cap->card, gspca_dev->dev->product,
1288 sizeof cap->card);
1289 } else {
1290 snprintf((char *) cap->card, sizeof cap->card,
1291 "USB Camera (%04x:%04x)",
1292 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1293 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1295 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1296 sizeof(cap->bus_info));
1297 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1298 | V4L2_CAP_STREAMING
1299 | V4L2_CAP_READWRITE;
1300 ret = 0;
1301 out:
1302 mutex_unlock(&gspca_dev->usb_lock);
1303 return ret;
1306 static int get_ctrl(struct gspca_dev *gspca_dev,
1307 int id)
1309 const struct ctrl *ctrls;
1310 int i;
1312 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1313 i < gspca_dev->sd_desc->nctrls;
1314 i++, ctrls++) {
1315 if (gspca_dev->ctrl_dis & (1 << i))
1316 continue;
1317 if (id == ctrls->qctrl.id)
1318 return i;
1320 return -1;
1323 static int vidioc_queryctrl(struct file *file, void *priv,
1324 struct v4l2_queryctrl *q_ctrl)
1326 struct gspca_dev *gspca_dev = priv;
1327 const struct ctrl *ctrls;
1328 struct gspca_ctrl *gspca_ctrl;
1329 int i, idx;
1330 u32 id;
1332 id = q_ctrl->id;
1333 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1334 id &= V4L2_CTRL_ID_MASK;
1335 id++;
1336 idx = -1;
1337 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1338 if (gspca_dev->ctrl_dis & (1 << i))
1339 continue;
1340 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1341 continue;
1342 if (idx >= 0
1343 && gspca_dev->sd_desc->ctrls[i].qctrl.id
1344 > gspca_dev->sd_desc->ctrls[idx].qctrl.id)
1345 continue;
1346 idx = i;
1348 } else {
1349 idx = get_ctrl(gspca_dev, id);
1351 if (idx < 0)
1352 return -EINVAL;
1353 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1354 memcpy(q_ctrl, &ctrls->qctrl, sizeof *q_ctrl);
1355 if (gspca_dev->cam.ctrls != NULL) {
1356 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1357 q_ctrl->default_value = gspca_ctrl->def;
1358 q_ctrl->minimum = gspca_ctrl->min;
1359 q_ctrl->maximum = gspca_ctrl->max;
1361 if (gspca_dev->ctrl_inac & (1 << idx))
1362 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1363 return 0;
1366 static int vidioc_s_ctrl(struct file *file, void *priv,
1367 struct v4l2_control *ctrl)
1369 struct gspca_dev *gspca_dev = priv;
1370 const struct ctrl *ctrls;
1371 struct gspca_ctrl *gspca_ctrl;
1372 int idx, ret;
1374 idx = get_ctrl(gspca_dev, ctrl->id);
1375 if (idx < 0)
1376 return -EINVAL;
1377 if (gspca_dev->ctrl_inac & (1 << idx))
1378 return -EINVAL;
1379 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1380 if (gspca_dev->cam.ctrls != NULL) {
1381 gspca_ctrl = &gspca_dev->cam.ctrls[idx];
1382 if (ctrl->value < gspca_ctrl->min
1383 || ctrl->value > gspca_ctrl->max)
1384 return -ERANGE;
1385 } else {
1386 gspca_ctrl = NULL;
1387 if (ctrl->value < ctrls->qctrl.minimum
1388 || ctrl->value > ctrls->qctrl.maximum)
1389 return -ERANGE;
1391 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1392 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1393 return -ERESTARTSYS;
1394 if (!gspca_dev->present) {
1395 ret = -ENODEV;
1396 goto out;
1398 gspca_dev->usb_err = 0;
1399 if (ctrls->set != NULL) {
1400 ret = ctrls->set(gspca_dev, ctrl->value);
1401 goto out;
1403 if (gspca_ctrl != NULL) {
1404 gspca_ctrl->val = ctrl->value;
1405 if (ctrls->set_control != NULL
1406 && gspca_dev->streaming)
1407 ctrls->set_control(gspca_dev);
1409 ret = gspca_dev->usb_err;
1410 out:
1411 mutex_unlock(&gspca_dev->usb_lock);
1412 return ret;
1415 static int vidioc_g_ctrl(struct file *file, void *priv,
1416 struct v4l2_control *ctrl)
1418 struct gspca_dev *gspca_dev = priv;
1419 const struct ctrl *ctrls;
1420 int idx, ret;
1422 idx = get_ctrl(gspca_dev, ctrl->id);
1423 if (idx < 0)
1424 return -EINVAL;
1425 ctrls = &gspca_dev->sd_desc->ctrls[idx];
1427 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1428 return -ERESTARTSYS;
1429 if (!gspca_dev->present) {
1430 ret = -ENODEV;
1431 goto out;
1433 gspca_dev->usb_err = 0;
1434 if (ctrls->get != NULL) {
1435 ret = ctrls->get(gspca_dev, &ctrl->value);
1436 goto out;
1438 if (gspca_dev->cam.ctrls != NULL)
1439 ctrl->value = gspca_dev->cam.ctrls[idx].val;
1440 ret = 0;
1441 out:
1442 mutex_unlock(&gspca_dev->usb_lock);
1443 return ret;
1446 static int vidioc_querymenu(struct file *file, void *priv,
1447 struct v4l2_querymenu *qmenu)
1449 struct gspca_dev *gspca_dev = priv;
1451 if (!gspca_dev->sd_desc->querymenu)
1452 return -EINVAL;
1453 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1456 static int vidioc_enum_input(struct file *file, void *priv,
1457 struct v4l2_input *input)
1459 struct gspca_dev *gspca_dev = priv;
1461 if (input->index != 0)
1462 return -EINVAL;
1463 input->type = V4L2_INPUT_TYPE_CAMERA;
1464 input->status = gspca_dev->cam.input_flags;
1465 strlcpy(input->name, gspca_dev->sd_desc->name,
1466 sizeof input->name);
1467 return 0;
1470 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1472 *i = 0;
1473 return 0;
1476 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1478 if (i > 0)
1479 return -EINVAL;
1480 return (0);
1483 static int vidioc_reqbufs(struct file *file, void *priv,
1484 struct v4l2_requestbuffers *rb)
1486 struct gspca_dev *gspca_dev = priv;
1487 int i, ret = 0, streaming;
1489 i = rb->memory; /* (avoid compilation warning) */
1490 switch (i) {
1491 case GSPCA_MEMORY_READ: /* (internal call) */
1492 case V4L2_MEMORY_MMAP:
1493 case V4L2_MEMORY_USERPTR:
1494 break;
1495 default:
1496 return -EINVAL;
1498 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1499 return -ERESTARTSYS;
1501 if (gspca_dev->memory != GSPCA_MEMORY_NO
1502 && gspca_dev->memory != GSPCA_MEMORY_READ
1503 && gspca_dev->memory != rb->memory) {
1504 ret = -EBUSY;
1505 goto out;
1508 /* only one file may do the capture */
1509 if (gspca_dev->capt_file != NULL
1510 && gspca_dev->capt_file != file) {
1511 ret = -EBUSY;
1512 goto out;
1515 /* if allocated, the buffers must not be mapped */
1516 for (i = 0; i < gspca_dev->nframes; i++) {
1517 if (gspca_dev->frame[i].vma_use_count) {
1518 ret = -EBUSY;
1519 goto out;
1523 /* stop streaming */
1524 streaming = gspca_dev->streaming;
1525 if (streaming) {
1526 mutex_lock(&gspca_dev->usb_lock);
1527 gspca_dev->usb_err = 0;
1528 gspca_stream_off(gspca_dev);
1529 mutex_unlock(&gspca_dev->usb_lock);
1531 /* Don't restart the stream when switching from read
1532 * to mmap mode */
1533 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1534 streaming = 0;
1537 /* free the previous allocated buffers, if any */
1538 if (gspca_dev->nframes != 0)
1539 frame_free(gspca_dev);
1540 if (rb->count == 0) /* unrequest */
1541 goto out;
1542 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1543 if (ret == 0) {
1544 rb->count = gspca_dev->nframes;
1545 if (streaming)
1546 ret = gspca_init_transfer(gspca_dev);
1548 out:
1549 mutex_unlock(&gspca_dev->queue_lock);
1550 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1551 return ret;
1554 static int vidioc_querybuf(struct file *file, void *priv,
1555 struct v4l2_buffer *v4l2_buf)
1557 struct gspca_dev *gspca_dev = priv;
1558 struct gspca_frame *frame;
1560 if (v4l2_buf->index < 0
1561 || v4l2_buf->index >= gspca_dev->nframes)
1562 return -EINVAL;
1564 frame = &gspca_dev->frame[v4l2_buf->index];
1565 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1566 return 0;
1569 static int vidioc_streamon(struct file *file, void *priv,
1570 enum v4l2_buf_type buf_type)
1572 struct gspca_dev *gspca_dev = priv;
1573 int ret;
1575 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1576 return -EINVAL;
1577 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1578 return -ERESTARTSYS;
1580 /* check the capture file */
1581 if (gspca_dev->capt_file != file) {
1582 ret = -EBUSY;
1583 goto out;
1586 if (gspca_dev->nframes == 0
1587 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1588 ret = -EINVAL;
1589 goto out;
1591 if (!gspca_dev->streaming) {
1592 ret = gspca_init_transfer(gspca_dev);
1593 if (ret < 0)
1594 goto out;
1596 #ifdef GSPCA_DEBUG
1597 if (gspca_debug & D_STREAM) {
1598 PDEBUG_MODE("stream on OK",
1599 gspca_dev->pixfmt,
1600 gspca_dev->width,
1601 gspca_dev->height);
1603 #endif
1604 ret = 0;
1605 out:
1606 mutex_unlock(&gspca_dev->queue_lock);
1607 return ret;
1610 static int vidioc_streamoff(struct file *file, void *priv,
1611 enum v4l2_buf_type buf_type)
1613 struct gspca_dev *gspca_dev = priv;
1614 int ret;
1616 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1617 return -EINVAL;
1619 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1620 return -ERESTARTSYS;
1622 if (!gspca_dev->streaming) {
1623 ret = 0;
1624 goto out;
1627 /* check the capture file */
1628 if (gspca_dev->capt_file != file) {
1629 ret = -EBUSY;
1630 goto out;
1633 /* stop streaming */
1634 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1635 ret = -ERESTARTSYS;
1636 goto out;
1638 gspca_dev->usb_err = 0;
1639 gspca_stream_off(gspca_dev);
1640 mutex_unlock(&gspca_dev->usb_lock);
1641 /* In case another thread is waiting in dqbuf */
1642 wake_up_interruptible(&gspca_dev->wq);
1644 /* empty the transfer queues */
1645 atomic_set(&gspca_dev->fr_q, 0);
1646 atomic_set(&gspca_dev->fr_i, 0);
1647 gspca_dev->fr_o = 0;
1648 ret = 0;
1649 out:
1650 mutex_unlock(&gspca_dev->queue_lock);
1651 return ret;
1654 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1655 struct v4l2_jpegcompression *jpegcomp)
1657 struct gspca_dev *gspca_dev = priv;
1658 int ret;
1660 if (!gspca_dev->sd_desc->get_jcomp)
1661 return -EINVAL;
1662 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1663 return -ERESTARTSYS;
1664 gspca_dev->usb_err = 0;
1665 if (gspca_dev->present)
1666 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1667 else
1668 ret = -ENODEV;
1669 mutex_unlock(&gspca_dev->usb_lock);
1670 return ret;
1673 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1674 struct v4l2_jpegcompression *jpegcomp)
1676 struct gspca_dev *gspca_dev = priv;
1677 int ret;
1679 if (!gspca_dev->sd_desc->set_jcomp)
1680 return -EINVAL;
1681 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1682 return -ERESTARTSYS;
1683 gspca_dev->usb_err = 0;
1684 if (gspca_dev->present)
1685 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1686 else
1687 ret = -ENODEV;
1688 mutex_unlock(&gspca_dev->usb_lock);
1689 return ret;
1692 static int vidioc_g_parm(struct file *filp, void *priv,
1693 struct v4l2_streamparm *parm)
1695 struct gspca_dev *gspca_dev = priv;
1697 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1699 if (gspca_dev->sd_desc->get_streamparm) {
1700 int ret;
1702 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1703 return -ERESTARTSYS;
1704 if (gspca_dev->present) {
1705 gspca_dev->usb_err = 0;
1706 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1707 ret = gspca_dev->usb_err;
1708 } else {
1709 ret = -ENODEV;
1711 mutex_unlock(&gspca_dev->usb_lock);
1712 return ret;
1715 return 0;
1718 static int vidioc_s_parm(struct file *filp, void *priv,
1719 struct v4l2_streamparm *parm)
1721 struct gspca_dev *gspca_dev = priv;
1722 int n;
1724 n = parm->parm.capture.readbuffers;
1725 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1726 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1727 else
1728 gspca_dev->nbufread = n;
1730 if (gspca_dev->sd_desc->set_streamparm) {
1731 int ret;
1733 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1734 return -ERESTARTSYS;
1735 if (gspca_dev->present) {
1736 gspca_dev->usb_err = 0;
1737 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1738 ret = gspca_dev->usb_err;
1739 } else {
1740 ret = -ENODEV;
1742 mutex_unlock(&gspca_dev->usb_lock);
1743 return ret;
1746 return 0;
1749 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1751 struct gspca_dev *gspca_dev = file->private_data;
1752 struct gspca_frame *frame;
1753 struct page *page;
1754 unsigned long addr, start, size;
1755 int i, ret;
1757 start = vma->vm_start;
1758 size = vma->vm_end - vma->vm_start;
1759 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1761 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1762 return -ERESTARTSYS;
1763 if (!gspca_dev->present) {
1764 ret = -ENODEV;
1765 goto out;
1767 if (gspca_dev->capt_file != file) {
1768 ret = -EINVAL;
1769 goto out;
1772 frame = NULL;
1773 for (i = 0; i < gspca_dev->nframes; ++i) {
1774 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1775 PDEBUG(D_STREAM, "mmap bad memory type");
1776 break;
1778 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1779 == vma->vm_pgoff) {
1780 frame = &gspca_dev->frame[i];
1781 break;
1784 if (frame == NULL) {
1785 PDEBUG(D_STREAM, "mmap no frame buffer found");
1786 ret = -EINVAL;
1787 goto out;
1789 if (size != frame->v4l2_buf.length) {
1790 PDEBUG(D_STREAM, "mmap bad size");
1791 ret = -EINVAL;
1792 goto out;
1796 * - VM_IO marks the area as being a mmaped region for I/O to a
1797 * device. It also prevents the region from being core dumped.
1799 vma->vm_flags |= VM_IO;
1801 addr = (unsigned long) frame->data;
1802 while (size > 0) {
1803 page = vmalloc_to_page((void *) addr);
1804 ret = vm_insert_page(vma, start, page);
1805 if (ret < 0)
1806 goto out;
1807 start += PAGE_SIZE;
1808 addr += PAGE_SIZE;
1809 size -= PAGE_SIZE;
1812 vma->vm_ops = &gspca_vm_ops;
1813 vma->vm_private_data = frame;
1814 gspca_vm_open(vma);
1815 ret = 0;
1816 out:
1817 mutex_unlock(&gspca_dev->queue_lock);
1818 return ret;
1821 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1822 enum v4l2_memory memory)
1824 if (!gspca_dev->present)
1825 return -ENODEV;
1826 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1827 !gspca_dev->streaming)
1828 return -EINVAL;
1830 /* check if a frame is ready */
1831 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1834 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1835 enum v4l2_memory memory)
1837 int ret;
1839 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1840 return -ERESTARTSYS;
1841 ret = frame_ready_nolock(gspca_dev, file, memory);
1842 mutex_unlock(&gspca_dev->queue_lock);
1843 return ret;
1847 * dequeue a video buffer
1849 * If nonblock_ing is false, block until a buffer is available.
1851 static int vidioc_dqbuf(struct file *file, void *priv,
1852 struct v4l2_buffer *v4l2_buf)
1854 struct gspca_dev *gspca_dev = priv;
1855 struct gspca_frame *frame;
1856 int i, j, ret;
1858 PDEBUG(D_FRAM, "dqbuf");
1860 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1861 return -ERESTARTSYS;
1863 for (;;) {
1864 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1865 if (ret < 0)
1866 goto out;
1867 if (ret > 0)
1868 break;
1870 mutex_unlock(&gspca_dev->queue_lock);
1872 if (file->f_flags & O_NONBLOCK)
1873 return -EAGAIN;
1875 /* wait till a frame is ready */
1876 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1877 frame_ready(gspca_dev, file, v4l2_buf->memory),
1878 msecs_to_jiffies(3000));
1879 if (ret < 0)
1880 return ret;
1881 if (ret == 0)
1882 return -EIO;
1884 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1885 return -ERESTARTSYS;
1888 i = gspca_dev->fr_o;
1889 j = gspca_dev->fr_queue[i];
1890 frame = &gspca_dev->frame[j];
1892 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1894 if (gspca_dev->sd_desc->dq_callback) {
1895 mutex_lock(&gspca_dev->usb_lock);
1896 gspca_dev->usb_err = 0;
1897 if (gspca_dev->present)
1898 gspca_dev->sd_desc->dq_callback(gspca_dev);
1899 mutex_unlock(&gspca_dev->usb_lock);
1902 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1903 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1904 PDEBUG(D_FRAM, "dqbuf %d", j);
1905 ret = 0;
1907 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1908 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1909 frame->data,
1910 frame->v4l2_buf.bytesused)) {
1911 PDEBUG(D_ERR|D_STREAM,
1912 "dqbuf cp to user failed");
1913 ret = -EFAULT;
1916 out:
1917 mutex_unlock(&gspca_dev->queue_lock);
1918 return ret;
1922 * queue a video buffer
1924 * Attempting to queue a buffer that has already been
1925 * queued will return -EINVAL.
1927 static int vidioc_qbuf(struct file *file, void *priv,
1928 struct v4l2_buffer *v4l2_buf)
1930 struct gspca_dev *gspca_dev = priv;
1931 struct gspca_frame *frame;
1932 int i, index, ret;
1934 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1936 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1937 return -ERESTARTSYS;
1939 index = v4l2_buf->index;
1940 if ((unsigned) index >= gspca_dev->nframes) {
1941 PDEBUG(D_FRAM,
1942 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1943 ret = -EINVAL;
1944 goto out;
1946 if (v4l2_buf->memory != gspca_dev->memory) {
1947 PDEBUG(D_FRAM, "qbuf bad memory type");
1948 ret = -EINVAL;
1949 goto out;
1952 frame = &gspca_dev->frame[index];
1953 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1954 PDEBUG(D_FRAM, "qbuf bad state");
1955 ret = -EINVAL;
1956 goto out;
1959 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1961 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1962 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1963 frame->v4l2_buf.length = v4l2_buf->length;
1966 /* put the buffer in the 'queued' queue */
1967 i = atomic_read(&gspca_dev->fr_q);
1968 gspca_dev->fr_queue[i] = index;
1969 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1971 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1972 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1973 ret = 0;
1974 out:
1975 mutex_unlock(&gspca_dev->queue_lock);
1976 return ret;
1980 * allocate the resources for read()
1982 static int read_alloc(struct gspca_dev *gspca_dev,
1983 struct file *file)
1985 struct v4l2_buffer v4l2_buf;
1986 int i, ret;
1988 PDEBUG(D_STREAM, "read alloc");
1989 if (gspca_dev->nframes == 0) {
1990 struct v4l2_requestbuffers rb;
1992 memset(&rb, 0, sizeof rb);
1993 rb.count = gspca_dev->nbufread;
1994 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1995 rb.memory = GSPCA_MEMORY_READ;
1996 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1997 if (ret != 0) {
1998 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1999 return ret;
2001 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2002 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2003 v4l2_buf.memory = GSPCA_MEMORY_READ;
2004 for (i = 0; i < gspca_dev->nbufread; i++) {
2005 v4l2_buf.index = i;
2006 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2007 if (ret != 0) {
2008 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2009 return ret;
2012 gspca_dev->memory = GSPCA_MEMORY_READ;
2015 /* start streaming */
2016 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2017 if (ret != 0)
2018 PDEBUG(D_STREAM, "read streamon err %d", ret);
2019 return ret;
2022 static unsigned int dev_poll(struct file *file, poll_table *wait)
2024 struct gspca_dev *gspca_dev = file->private_data;
2025 int ret;
2027 PDEBUG(D_FRAM, "poll");
2029 poll_wait(file, &gspca_dev->wq, wait);
2031 /* if reqbufs is not done, the user would use read() */
2032 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
2033 ret = read_alloc(gspca_dev, file);
2034 if (ret != 0)
2035 return POLLERR;
2038 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2039 return POLLERR;
2041 /* check if an image has been received */
2042 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
2043 ret = POLLIN | POLLRDNORM; /* yes */
2044 else
2045 ret = 0;
2046 mutex_unlock(&gspca_dev->queue_lock);
2047 if (!gspca_dev->present)
2048 return POLLHUP;
2049 return ret;
2052 static ssize_t dev_read(struct file *file, char __user *data,
2053 size_t count, loff_t *ppos)
2055 struct gspca_dev *gspca_dev = file->private_data;
2056 struct gspca_frame *frame;
2057 struct v4l2_buffer v4l2_buf;
2058 struct timeval timestamp;
2059 int n, ret, ret2;
2061 PDEBUG(D_FRAM, "read (%zd)", count);
2062 if (!gspca_dev->present)
2063 return -ENODEV;
2064 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
2065 ret = read_alloc(gspca_dev, file);
2066 if (ret != 0)
2067 return ret;
2070 /* get a frame */
2071 timestamp = ktime_to_timeval(ktime_get());
2072 timestamp.tv_sec--;
2073 n = 2;
2074 for (;;) {
2075 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2076 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2077 v4l2_buf.memory = GSPCA_MEMORY_READ;
2078 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2079 if (ret != 0) {
2080 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2081 return ret;
2084 /* if the process slept for more than 1 second,
2085 * get a newer frame */
2086 frame = &gspca_dev->frame[v4l2_buf.index];
2087 if (--n < 0)
2088 break; /* avoid infinite loop */
2089 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2090 break;
2091 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2092 if (ret != 0) {
2093 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2094 return ret;
2098 /* copy the frame */
2099 if (count > frame->v4l2_buf.bytesused)
2100 count = frame->v4l2_buf.bytesused;
2101 ret = copy_to_user(data, frame->data, count);
2102 if (ret != 0) {
2103 PDEBUG(D_ERR|D_STREAM,
2104 "read cp to user lack %d / %zd", ret, count);
2105 ret = -EFAULT;
2106 goto out;
2108 ret = count;
2109 out:
2110 /* in each case, requeue the buffer */
2111 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2112 if (ret2 != 0)
2113 return ret2;
2114 return ret;
2117 static struct v4l2_file_operations dev_fops = {
2118 .owner = THIS_MODULE,
2119 .open = dev_open,
2120 .release = dev_close,
2121 .read = dev_read,
2122 .mmap = dev_mmap,
2123 .unlocked_ioctl = video_ioctl2,
2124 .poll = dev_poll,
2127 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2128 .vidioc_querycap = vidioc_querycap,
2129 .vidioc_dqbuf = vidioc_dqbuf,
2130 .vidioc_qbuf = vidioc_qbuf,
2131 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2132 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2133 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2134 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2135 .vidioc_streamon = vidioc_streamon,
2136 .vidioc_queryctrl = vidioc_queryctrl,
2137 .vidioc_g_ctrl = vidioc_g_ctrl,
2138 .vidioc_s_ctrl = vidioc_s_ctrl,
2139 .vidioc_querymenu = vidioc_querymenu,
2140 .vidioc_enum_input = vidioc_enum_input,
2141 .vidioc_g_input = vidioc_g_input,
2142 .vidioc_s_input = vidioc_s_input,
2143 .vidioc_reqbufs = vidioc_reqbufs,
2144 .vidioc_querybuf = vidioc_querybuf,
2145 .vidioc_streamoff = vidioc_streamoff,
2146 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2147 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2148 .vidioc_g_parm = vidioc_g_parm,
2149 .vidioc_s_parm = vidioc_s_parm,
2150 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2151 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2152 #ifdef CONFIG_VIDEO_ADV_DEBUG
2153 .vidioc_g_register = vidioc_g_register,
2154 .vidioc_s_register = vidioc_s_register,
2155 #endif
2156 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2159 static const struct video_device gspca_template = {
2160 .name = "gspca main driver",
2161 .fops = &dev_fops,
2162 .ioctl_ops = &dev_ioctl_ops,
2163 .release = gspca_release,
2166 /* initialize the controls */
2167 static void ctrls_init(struct gspca_dev *gspca_dev)
2169 struct gspca_ctrl *ctrl;
2170 int i;
2172 for (i = 0, ctrl = gspca_dev->cam.ctrls;
2173 i < gspca_dev->sd_desc->nctrls;
2174 i++, ctrl++) {
2175 ctrl->def = gspca_dev->sd_desc->ctrls[i].qctrl.default_value;
2176 ctrl->val = ctrl->def;
2177 ctrl->min = gspca_dev->sd_desc->ctrls[i].qctrl.minimum;
2178 ctrl->max = gspca_dev->sd_desc->ctrls[i].qctrl.maximum;
2183 * probe and create a new gspca device
2185 * This function must be called by the sub-driver when it is
2186 * called for probing a new device.
2188 int gspca_dev_probe2(struct usb_interface *intf,
2189 const struct usb_device_id *id,
2190 const struct sd_desc *sd_desc,
2191 int dev_size,
2192 struct module *module)
2194 struct gspca_dev *gspca_dev;
2195 struct usb_device *dev = interface_to_usbdev(intf);
2196 int ret;
2198 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2200 /* create the device */
2201 if (dev_size < sizeof *gspca_dev)
2202 dev_size = sizeof *gspca_dev;
2203 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2204 if (!gspca_dev) {
2205 err("couldn't kzalloc gspca struct");
2206 return -ENOMEM;
2208 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2209 if (!gspca_dev->usb_buf) {
2210 err("out of memory");
2211 ret = -ENOMEM;
2212 goto out;
2214 gspca_dev->dev = dev;
2215 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2216 gspca_dev->nbalt = intf->num_altsetting;
2218 /* check if any audio device */
2219 if (dev->config->desc.bNumInterfaces != 1) {
2220 int i;
2221 struct usb_interface *intf2;
2223 for (i = 0; i < dev->config->desc.bNumInterfaces; i++) {
2224 intf2 = dev->config->interface[i];
2225 if (intf2 != NULL
2226 && intf2->altsetting != NULL
2227 && intf2->altsetting->desc.bInterfaceClass ==
2228 USB_CLASS_AUDIO) {
2229 gspca_dev->audio = 1;
2230 break;
2235 gspca_dev->sd_desc = sd_desc;
2236 gspca_dev->nbufread = 2;
2237 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2239 /* configure the subdriver and initialize the USB device */
2240 ret = sd_desc->config(gspca_dev, id);
2241 if (ret < 0)
2242 goto out;
2243 if (gspca_dev->cam.ctrls != NULL)
2244 ctrls_init(gspca_dev);
2245 ret = sd_desc->init(gspca_dev);
2246 if (ret < 0)
2247 goto out;
2248 gspca_set_default_mode(gspca_dev);
2250 ret = gspca_input_connect(gspca_dev);
2251 if (ret)
2252 goto out;
2254 mutex_init(&gspca_dev->usb_lock);
2255 mutex_init(&gspca_dev->queue_lock);
2256 init_waitqueue_head(&gspca_dev->wq);
2258 /* init video stuff */
2259 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2260 gspca_dev->vdev.parent = &intf->dev;
2261 gspca_dev->module = module;
2262 gspca_dev->present = 1;
2263 ret = video_register_device(&gspca_dev->vdev,
2264 VFL_TYPE_GRABBER,
2265 -1);
2266 if (ret < 0) {
2267 err("video_register_device err %d", ret);
2268 goto out;
2271 usb_set_intfdata(intf, gspca_dev);
2272 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2274 gspca_input_create_urb(gspca_dev);
2276 return 0;
2277 out:
2278 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2279 if (gspca_dev->input_dev)
2280 input_unregister_device(gspca_dev->input_dev);
2281 #endif
2282 kfree(gspca_dev->usb_buf);
2283 kfree(gspca_dev);
2284 return ret;
2286 EXPORT_SYMBOL(gspca_dev_probe2);
2288 /* same function as the previous one, but check the interface */
2289 int gspca_dev_probe(struct usb_interface *intf,
2290 const struct usb_device_id *id,
2291 const struct sd_desc *sd_desc,
2292 int dev_size,
2293 struct module *module)
2295 struct usb_device *dev = interface_to_usbdev(intf);
2297 /* we don't handle multi-config cameras */
2298 if (dev->descriptor.bNumConfigurations != 1) {
2299 err("%04x:%04x too many config",
2300 id->idVendor, id->idProduct);
2301 return -ENODEV;
2304 /* the USB video interface must be the first one */
2305 if (dev->config->desc.bNumInterfaces != 1
2306 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2307 return -ENODEV;
2309 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2311 EXPORT_SYMBOL(gspca_dev_probe);
2314 * USB disconnection
2316 * This function must be called by the sub-driver
2317 * when the device disconnects, after the specific resources are freed.
2319 void gspca_disconnect(struct usb_interface *intf)
2321 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2322 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2323 struct input_dev *input_dev;
2324 #endif
2326 PDEBUG(D_PROBE, "%s disconnect",
2327 video_device_node_name(&gspca_dev->vdev));
2328 mutex_lock(&gspca_dev->usb_lock);
2330 gspca_dev->present = 0;
2331 wake_up_interruptible(&gspca_dev->wq);
2333 destroy_urbs(gspca_dev);
2335 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2336 gspca_input_destroy_urb(gspca_dev);
2337 input_dev = gspca_dev->input_dev;
2338 if (input_dev) {
2339 gspca_dev->input_dev = NULL;
2340 input_unregister_device(input_dev);
2342 #endif
2344 /* the device is freed at exit of this function */
2345 gspca_dev->dev = NULL;
2346 mutex_unlock(&gspca_dev->usb_lock);
2348 usb_set_intfdata(intf, NULL);
2350 /* release the device */
2351 /* (this will call gspca_release() immediately or on last close) */
2352 video_unregister_device(&gspca_dev->vdev);
2354 /* PDEBUG(D_PROBE, "disconnect complete"); */
2356 EXPORT_SYMBOL(gspca_disconnect);
2358 #ifdef CONFIG_PM
2359 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2361 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2363 if (!gspca_dev->streaming)
2364 return 0;
2365 gspca_dev->frozen = 1; /* avoid urb error messages */
2366 if (gspca_dev->sd_desc->stopN)
2367 gspca_dev->sd_desc->stopN(gspca_dev);
2368 destroy_urbs(gspca_dev);
2369 gspca_input_destroy_urb(gspca_dev);
2370 gspca_set_alt0(gspca_dev);
2371 if (gspca_dev->sd_desc->stop0)
2372 gspca_dev->sd_desc->stop0(gspca_dev);
2373 return 0;
2375 EXPORT_SYMBOL(gspca_suspend);
2377 int gspca_resume(struct usb_interface *intf)
2379 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2381 gspca_dev->frozen = 0;
2382 gspca_dev->sd_desc->init(gspca_dev);
2383 gspca_input_create_urb(gspca_dev);
2384 if (gspca_dev->streaming)
2385 return gspca_init_transfer(gspca_dev);
2386 return 0;
2388 EXPORT_SYMBOL(gspca_resume);
2389 #endif
2390 /* -- cam driver utility functions -- */
2392 /* auto gain and exposure algorithm based on the knee algorithm described here:
2393 http://ytse.tricolour.net/docs/LowLightOptimization.html
2395 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2396 where changed. */
2397 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2398 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2400 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2401 const struct ctrl *gain_ctrl = NULL;
2402 const struct ctrl *exposure_ctrl = NULL;
2403 const struct ctrl *autogain_ctrl = NULL;
2404 int retval = 0;
2406 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2407 if (gspca_dev->ctrl_dis & (1 << i))
2408 continue;
2409 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2410 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2411 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2412 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2413 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2414 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2416 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2417 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2418 "on cam without (auto)gain/exposure");
2419 return 0;
2422 if (gain_ctrl->get(gspca_dev, &gain) ||
2423 exposure_ctrl->get(gspca_dev, &exposure) ||
2424 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2425 return 0;
2427 orig_gain = gain;
2428 orig_exposure = exposure;
2430 /* If we are of a multiple of deadzone, do multiple steps to reach the
2431 desired lumination fast (with the risc of a slight overshoot) */
2432 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2434 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2435 avg_lum, desired_avg_lum, steps);
2437 for (i = 0; i < steps; i++) {
2438 if (avg_lum > desired_avg_lum) {
2439 if (gain > gain_knee)
2440 gain--;
2441 else if (exposure > exposure_knee)
2442 exposure--;
2443 else if (gain > gain_ctrl->qctrl.default_value)
2444 gain--;
2445 else if (exposure > exposure_ctrl->qctrl.minimum)
2446 exposure--;
2447 else if (gain > gain_ctrl->qctrl.minimum)
2448 gain--;
2449 else
2450 break;
2451 } else {
2452 if (gain < gain_ctrl->qctrl.default_value)
2453 gain++;
2454 else if (exposure < exposure_knee)
2455 exposure++;
2456 else if (gain < gain_knee)
2457 gain++;
2458 else if (exposure < exposure_ctrl->qctrl.maximum)
2459 exposure++;
2460 else if (gain < gain_ctrl->qctrl.maximum)
2461 gain++;
2462 else
2463 break;
2467 if (gain != orig_gain) {
2468 gain_ctrl->set(gspca_dev, gain);
2469 retval = 1;
2471 if (exposure != orig_exposure) {
2472 exposure_ctrl->set(gspca_dev, exposure);
2473 retval = 1;
2476 return retval;
2478 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2480 /* -- module insert / remove -- */
2481 static int __init gspca_init(void)
2483 info("v" DRIVER_VERSION_NUMBER " registered");
2484 return 0;
2486 static void __exit gspca_exit(void)
2490 module_init(gspca_init);
2491 module_exit(gspca_exit);
2493 #ifdef GSPCA_DEBUG
2494 module_param_named(debug, gspca_debug, int, 0644);
2495 MODULE_PARM_DESC(debug,
2496 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2497 " 0x08:stream 0x10:frame 0x20:packet"
2498 " 0x0100: v4l2");
2499 #endif