Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / gspca / gspca.c
blob222af479150b4baa2fea9444eeeb40010b95c20b
1 /*
2 * Main USB camera driver
4 * Copyright (C) 2008-2009 Jean-Francois 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/version.h>
28 #include <linux/fs.h>
29 #include <linux/vmalloc.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
35 #include <linux/io.h>
36 #include <asm/page.h>
37 #include <linux/uaccess.h>
38 #include <linux/jiffies.h>
39 #include <media/v4l2-ioctl.h>
41 #include "gspca.h"
43 #ifdef CONFIG_INPUT
44 #include <linux/input.h>
45 #include <linux/usb/input.h>
46 #endif
48 /* global values */
49 #define DEF_NURBS 3 /* default number of URBs */
50 #if DEF_NURBS > MAX_NURBS
51 #error "DEF_NURBS too big"
52 #endif
54 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 0)
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 #ifdef CONFIG_INPUT
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 PDEBUG(D_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 PDEBUG(D_ERR, "Input device registration failed "
181 "with error %i", 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 = ep->wMaxPacketSize;
203 interval = ep->bInterval;
204 PDEBUG(D_PROBE, "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_buffer_alloc(dev, ep->wMaxPacketSize,
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 gspca_dev->int_urb = urb;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
229 PDEBUG(D_ERR, "submit URB failed with error %i", ret);
230 goto error_submit;
232 return ret;
234 error_submit:
235 usb_buffer_free(dev,
236 urb->transfer_buffer_length,
237 urb->transfer_buffer,
238 urb->transfer_dma);
239 error_buffer:
240 usb_free_urb(urb);
241 error:
242 return ret;
245 static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
247 struct usb_interface *intf;
248 struct usb_host_interface *intf_desc;
249 struct usb_endpoint_descriptor *ep;
250 int i;
252 if (gspca_dev->sd_desc->int_pkt_scan) {
253 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
254 intf_desc = intf->cur_altsetting;
255 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
256 ep = &intf_desc->endpoint[i].desc;
257 if (usb_endpoint_dir_in(ep) &&
258 usb_endpoint_xfer_int(ep)) {
260 alloc_and_submit_int_urb(gspca_dev, ep);
261 break;
267 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
269 struct urb *urb;
271 urb = gspca_dev->int_urb;
272 if (urb) {
273 gspca_dev->int_urb = NULL;
274 usb_kill_urb(urb);
275 usb_buffer_free(gspca_dev->dev,
276 urb->transfer_buffer_length,
277 urb->transfer_buffer,
278 urb->transfer_dma);
279 usb_free_urb(urb);
282 #else
283 #define gspca_input_connect(gspca_dev) 0
284 #define gspca_input_create_urb(gspca_dev)
285 #define gspca_input_destroy_urb(gspca_dev)
286 #endif
288 /* get the current input frame buffer */
289 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
291 struct gspca_frame *frame;
293 frame = gspca_dev->cur_frame;
294 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
295 != V4L2_BUF_FLAG_QUEUED)
296 return NULL;
297 return frame;
299 EXPORT_SYMBOL(gspca_get_i_frame);
302 * fill a video frame from an URB and resubmit
304 static void fill_frame(struct gspca_dev *gspca_dev,
305 struct urb *urb)
307 u8 *data; /* address of data in the iso message */
308 int i, len, st;
309 cam_pkt_op pkt_scan;
311 if (urb->status != 0) {
312 if (urb->status == -ESHUTDOWN)
313 return; /* disconnection */
314 #ifdef CONFIG_PM
315 if (gspca_dev->frozen)
316 return;
317 #endif
318 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
319 urb->status = 0;
320 goto resubmit;
322 pkt_scan = gspca_dev->sd_desc->pkt_scan;
323 for (i = 0; i < urb->number_of_packets; i++) {
325 /* check the packet status and length */
326 len = urb->iso_frame_desc[i].actual_length;
327 if (len == 0) {
328 if (gspca_dev->empty_packet == 0)
329 gspca_dev->empty_packet = 1;
330 continue;
332 st = urb->iso_frame_desc[i].status;
333 if (st) {
334 PDEBUG(D_ERR,
335 "ISOC data error: [%d] len=%d, status=%d",
336 i, len, st);
337 gspca_dev->last_packet_type = DISCARD_PACKET;
338 continue;
341 /* let the packet be analyzed by the subdriver */
342 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
343 i, urb->iso_frame_desc[i].offset, len);
344 data = (u8 *) urb->transfer_buffer
345 + urb->iso_frame_desc[i].offset;
346 pkt_scan(gspca_dev, data, len);
349 resubmit:
350 /* resubmit the URB */
351 st = usb_submit_urb(urb, GFP_ATOMIC);
352 if (st < 0)
353 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
357 * ISOC message interrupt from the USB device
359 * Analyse each packet and call the subdriver for copy to the frame buffer.
361 static void isoc_irq(struct urb *urb)
363 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
365 PDEBUG(D_PACK, "isoc irq");
366 if (!gspca_dev->streaming)
367 return;
368 fill_frame(gspca_dev, urb);
372 * bulk message interrupt from the USB device
374 static void bulk_irq(struct urb *urb)
376 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
377 int st;
379 PDEBUG(D_PACK, "bulk irq");
380 if (!gspca_dev->streaming)
381 return;
382 switch (urb->status) {
383 case 0:
384 break;
385 case -ESHUTDOWN:
386 return; /* disconnection */
387 default:
388 #ifdef CONFIG_PM
389 if (gspca_dev->frozen)
390 return;
391 #endif
392 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
393 urb->status = 0;
394 goto resubmit;
397 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
398 gspca_dev->sd_desc->pkt_scan(gspca_dev,
399 urb->transfer_buffer,
400 urb->actual_length);
402 resubmit:
403 /* resubmit the URB */
404 if (gspca_dev->cam.bulk_nurbs != 0) {
405 st = usb_submit_urb(urb, GFP_ATOMIC);
406 if (st < 0)
407 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
412 * add data to the current frame
414 * This function is called by the subdrivers at interrupt level.
416 * To build a frame, these ones must add
417 * - one FIRST_PACKET
418 * - 0 or many INTER_PACKETs
419 * - one LAST_PACKET
420 * DISCARD_PACKET invalidates the whole frame.
421 * On LAST_PACKET, a new frame is returned.
423 void gspca_frame_add(struct gspca_dev *gspca_dev,
424 enum gspca_packet_type packet_type,
425 const u8 *data,
426 int len)
428 struct gspca_frame *frame;
429 int i, j;
431 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
433 /* check the availability of the frame buffer */
434 frame = gspca_dev->cur_frame;
435 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
436 != V4L2_BUF_FLAG_QUEUED) {
437 gspca_dev->last_packet_type = DISCARD_PACKET;
438 return;
441 /* when start of a new frame, if the current frame buffer
442 * is not queued, discard the whole frame */
443 if (packet_type == FIRST_PACKET) {
444 frame->data_end = frame->data;
445 jiffies_to_timeval(get_jiffies_64(),
446 &frame->v4l2_buf.timestamp);
447 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
448 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
449 if (packet_type == LAST_PACKET)
450 gspca_dev->last_packet_type = packet_type;
451 return;
454 /* append the packet to the frame buffer */
455 if (len > 0) {
456 if (frame->data_end - frame->data + len
457 > frame->v4l2_buf.length) {
458 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
459 frame->data_end - frame->data + len,
460 frame->v4l2_buf.length);
461 packet_type = DISCARD_PACKET;
462 } else {
463 memcpy(frame->data_end, data, len);
464 frame->data_end += len;
467 gspca_dev->last_packet_type = packet_type;
469 /* if last packet, wake up the application and advance in the queue */
470 if (packet_type == LAST_PACKET) {
471 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
472 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
473 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
474 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
475 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
476 gspca_dev->fr_i = i;
477 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
478 frame->v4l2_buf.bytesused,
479 gspca_dev->fr_q,
481 gspca_dev->fr_o);
482 j = gspca_dev->fr_queue[i];
483 gspca_dev->cur_frame = &gspca_dev->frame[j];
486 EXPORT_SYMBOL(gspca_frame_add);
488 static int gspca_is_compressed(__u32 format)
490 switch (format) {
491 case V4L2_PIX_FMT_MJPEG:
492 case V4L2_PIX_FMT_JPEG:
493 case V4L2_PIX_FMT_SPCA561:
494 case V4L2_PIX_FMT_PAC207:
495 case V4L2_PIX_FMT_MR97310A:
496 return 1;
498 return 0;
501 static void *rvmalloc(long size)
503 void *mem;
504 unsigned long adr;
506 mem = vmalloc_32(size);
507 if (mem != NULL) {
508 adr = (unsigned long) mem;
509 while (size > 0) {
510 SetPageReserved(vmalloc_to_page((void *) adr));
511 adr += PAGE_SIZE;
512 size -= PAGE_SIZE;
515 return mem;
518 static void rvfree(void *mem, long size)
520 unsigned long adr;
522 adr = (unsigned long) mem;
523 while (size > 0) {
524 ClearPageReserved(vmalloc_to_page((void *) adr));
525 adr += PAGE_SIZE;
526 size -= PAGE_SIZE;
528 vfree(mem);
531 static int frame_alloc(struct gspca_dev *gspca_dev,
532 unsigned int count)
534 struct gspca_frame *frame;
535 unsigned int frsz;
536 int i;
538 i = gspca_dev->curr_mode;
539 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
540 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
541 frsz = PAGE_ALIGN(frsz);
542 gspca_dev->frsz = frsz;
543 if (count > GSPCA_MAX_FRAMES)
544 count = GSPCA_MAX_FRAMES;
545 gspca_dev->frbuf = rvmalloc(frsz * count);
546 if (!gspca_dev->frbuf) {
547 err("frame alloc failed");
548 return -ENOMEM;
550 gspca_dev->nframes = count;
551 for (i = 0; i < count; i++) {
552 frame = &gspca_dev->frame[i];
553 frame->v4l2_buf.index = i;
554 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
555 frame->v4l2_buf.flags = 0;
556 frame->v4l2_buf.field = V4L2_FIELD_NONE;
557 frame->v4l2_buf.length = frsz;
558 frame->v4l2_buf.memory = gspca_dev->memory;
559 frame->v4l2_buf.sequence = 0;
560 frame->data = frame->data_end =
561 gspca_dev->frbuf + i * frsz;
562 frame->v4l2_buf.m.offset = i * frsz;
564 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
565 gspca_dev->cur_frame = &gspca_dev->frame[0];
566 gspca_dev->last_packet_type = DISCARD_PACKET;
567 gspca_dev->sequence = 0;
568 return 0;
571 static void frame_free(struct gspca_dev *gspca_dev)
573 int i;
575 PDEBUG(D_STREAM, "frame free");
576 if (gspca_dev->frbuf != NULL) {
577 rvfree(gspca_dev->frbuf,
578 gspca_dev->nframes * gspca_dev->frsz);
579 gspca_dev->frbuf = NULL;
580 for (i = 0; i < gspca_dev->nframes; i++)
581 gspca_dev->frame[i].data = NULL;
583 gspca_dev->nframes = 0;
586 static void destroy_urbs(struct gspca_dev *gspca_dev)
588 struct urb *urb;
589 unsigned int i;
591 PDEBUG(D_STREAM, "kill transfer");
592 for (i = 0; i < MAX_NURBS; i++) {
593 urb = gspca_dev->urb[i];
594 if (urb == NULL)
595 break;
597 gspca_dev->urb[i] = NULL;
598 usb_kill_urb(urb);
599 if (urb->transfer_buffer != NULL)
600 usb_buffer_free(gspca_dev->dev,
601 urb->transfer_buffer_length,
602 urb->transfer_buffer,
603 urb->transfer_dma);
604 usb_free_urb(urb);
609 * look for an input transfer endpoint in an alternate setting
611 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
612 int xfer)
614 struct usb_host_endpoint *ep;
615 int i, attr;
617 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
618 ep = &alt->endpoint[i];
619 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
620 if (attr == xfer
621 && ep->desc.wMaxPacketSize != 0)
622 return ep;
624 return NULL;
628 * look for an input (isoc or bulk) endpoint
630 * The endpoint is defined by the subdriver.
631 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
632 * This routine may be called many times when the bandwidth is too small
633 * (the bandwidth is checked on urb submit).
635 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
637 struct usb_interface *intf;
638 struct usb_host_endpoint *ep;
639 int xfer, i, ret;
641 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
642 ep = NULL;
643 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
644 : USB_ENDPOINT_XFER_ISOC;
645 i = gspca_dev->alt; /* previous alt setting */
646 if (gspca_dev->cam.reverse_alts) {
647 while (++i < gspca_dev->nbalt) {
648 ep = alt_xfer(&intf->altsetting[i], xfer);
649 if (ep)
650 break;
652 } else {
653 while (--i >= 0) {
654 ep = alt_xfer(&intf->altsetting[i], xfer);
655 if (ep)
656 break;
659 if (ep == NULL) {
660 err("no transfer endpoint found");
661 return NULL;
663 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
664 i, ep->desc.bEndpointAddress);
665 gspca_dev->alt = i; /* memorize the current alt setting */
666 if (gspca_dev->nbalt > 1) {
667 gspca_input_destroy_urb(gspca_dev);
668 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
669 if (ret < 0) {
670 err("set alt %d err %d", i, ret);
671 ep = NULL;
673 gspca_input_create_urb(gspca_dev);
675 return ep;
679 * create the URBs for image transfer
681 static int create_urbs(struct gspca_dev *gspca_dev,
682 struct usb_host_endpoint *ep)
684 struct urb *urb;
685 int n, nurbs, i, psize, npkt, bsize;
687 /* calculate the packet size and the number of packets */
688 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
690 if (!gspca_dev->cam.bulk) { /* isoc */
692 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
693 if (gspca_dev->pkt_size == 0)
694 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
695 else
696 psize = gspca_dev->pkt_size;
697 npkt = gspca_dev->cam.npkt;
698 if (npkt == 0)
699 npkt = 32; /* default value */
700 bsize = psize * npkt;
701 PDEBUG(D_STREAM,
702 "isoc %d pkts size %d = bsize:%d",
703 npkt, psize, bsize);
704 nurbs = DEF_NURBS;
705 } else { /* bulk */
706 npkt = 0;
707 bsize = gspca_dev->cam.bulk_size;
708 if (bsize == 0)
709 bsize = psize;
710 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
711 if (gspca_dev->cam.bulk_nurbs != 0)
712 nurbs = gspca_dev->cam.bulk_nurbs;
713 else
714 nurbs = 1;
717 for (n = 0; n < nurbs; n++) {
718 urb = usb_alloc_urb(npkt, GFP_KERNEL);
719 if (!urb) {
720 err("usb_alloc_urb failed");
721 return -ENOMEM;
723 gspca_dev->urb[n] = urb;
724 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
725 bsize,
726 GFP_KERNEL,
727 &urb->transfer_dma);
729 if (urb->transfer_buffer == NULL) {
730 err("usb_buffer_alloc failed");
731 return -ENOMEM;
733 urb->dev = gspca_dev->dev;
734 urb->context = gspca_dev;
735 urb->transfer_buffer_length = bsize;
736 if (npkt != 0) { /* ISOC */
737 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
738 ep->desc.bEndpointAddress);
739 urb->transfer_flags = URB_ISO_ASAP
740 | URB_NO_TRANSFER_DMA_MAP;
741 urb->interval = ep->desc.bInterval;
742 urb->complete = isoc_irq;
743 urb->number_of_packets = npkt;
744 for (i = 0; i < npkt; i++) {
745 urb->iso_frame_desc[i].length = psize;
746 urb->iso_frame_desc[i].offset = psize * i;
748 } else { /* bulk */
749 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
750 ep->desc.bEndpointAddress),
751 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
752 urb->complete = bulk_irq;
755 return 0;
759 * start the USB transfer
761 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
763 struct usb_host_endpoint *ep;
764 struct urb *urb;
765 int n, ret;
767 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
768 return -ERESTARTSYS;
770 if (!gspca_dev->present) {
771 ret = -ENODEV;
772 goto out;
775 gspca_dev->usb_err = 0;
777 /* set the higher alternate setting and
778 * loop until urb submit succeeds */
779 if (gspca_dev->cam.reverse_alts)
780 gspca_dev->alt = 0;
781 else
782 gspca_dev->alt = gspca_dev->nbalt;
784 if (gspca_dev->sd_desc->isoc_init) {
785 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
786 if (ret < 0)
787 goto out;
789 ep = get_ep(gspca_dev);
790 if (ep == NULL) {
791 ret = -EIO;
792 goto out;
794 for (;;) {
795 if (!gspca_dev->cam.no_urb_create) {
796 PDEBUG(D_STREAM, "init transfer alt %d",
797 gspca_dev->alt);
798 ret = create_urbs(gspca_dev, ep);
799 if (ret < 0) {
800 destroy_urbs(gspca_dev);
801 goto out;
805 /* clear the bulk endpoint */
806 if (gspca_dev->cam.bulk)
807 usb_clear_halt(gspca_dev->dev,
808 gspca_dev->urb[0]->pipe);
810 /* start the cam */
811 ret = gspca_dev->sd_desc->start(gspca_dev);
812 if (ret < 0) {
813 destroy_urbs(gspca_dev);
814 goto out;
816 gspca_dev->streaming = 1;
818 /* some bulk transfers are started by the subdriver */
819 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
820 break;
822 /* submit the URBs */
823 for (n = 0; n < MAX_NURBS; n++) {
824 urb = gspca_dev->urb[n];
825 if (urb == NULL)
826 break;
827 ret = usb_submit_urb(urb, GFP_KERNEL);
828 if (ret < 0)
829 break;
831 if (ret >= 0)
832 break;
833 gspca_dev->streaming = 0;
834 destroy_urbs(gspca_dev);
835 if (ret != -ENOSPC) {
836 PDEBUG(D_ERR|D_STREAM,
837 "usb_submit_urb alt %d err %d",
838 gspca_dev->alt, ret);
839 goto out;
842 /* the bandwidth is not wide enough
843 * negociate or try a lower alternate setting */
844 PDEBUG(D_ERR|D_STREAM,
845 "bandwidth not wide enough - trying again");
846 msleep(20); /* wait for kill complete */
847 if (gspca_dev->sd_desc->isoc_nego) {
848 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
849 if (ret < 0)
850 goto out;
851 } else {
852 ep = get_ep(gspca_dev);
853 if (ep == NULL) {
854 ret = -EIO;
855 goto out;
859 out:
860 mutex_unlock(&gspca_dev->usb_lock);
861 return ret;
864 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
866 int ret;
868 if (gspca_dev->alt == 0)
869 return 0;
870 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
871 if (ret < 0)
872 PDEBUG(D_ERR|D_STREAM, "set alt 0 err %d", ret);
873 return ret;
876 /* Note: both the queue and the usb locks should be held when calling this */
877 static void gspca_stream_off(struct gspca_dev *gspca_dev)
879 gspca_dev->streaming = 0;
880 if (gspca_dev->present) {
881 if (gspca_dev->sd_desc->stopN)
882 gspca_dev->sd_desc->stopN(gspca_dev);
883 destroy_urbs(gspca_dev);
884 gspca_input_destroy_urb(gspca_dev);
885 gspca_set_alt0(gspca_dev);
886 gspca_input_create_urb(gspca_dev);
889 /* always call stop0 to free the subdriver's resources */
890 if (gspca_dev->sd_desc->stop0)
891 gspca_dev->sd_desc->stop0(gspca_dev);
892 PDEBUG(D_STREAM, "stream off OK");
895 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
897 int i;
899 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
900 gspca_dev->curr_mode = i;
901 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
902 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
903 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
906 static int wxh_to_mode(struct gspca_dev *gspca_dev,
907 int width, int height)
909 int i;
911 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
912 if (width >= gspca_dev->cam.cam_mode[i].width
913 && height >= gspca_dev->cam.cam_mode[i].height)
914 break;
916 return i;
920 * search a mode with the right pixel format
922 static int gspca_get_mode(struct gspca_dev *gspca_dev,
923 int mode,
924 int pixfmt)
926 int modeU, modeD;
928 modeU = modeD = mode;
929 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
930 if (--modeD >= 0) {
931 if (gspca_dev->cam.cam_mode[modeD].pixelformat
932 == pixfmt)
933 return modeD;
935 if (++modeU < gspca_dev->cam.nmodes) {
936 if (gspca_dev->cam.cam_mode[modeU].pixelformat
937 == pixfmt)
938 return modeU;
941 return -EINVAL;
944 #ifdef CONFIG_VIDEO_ADV_DEBUG
945 static int vidioc_g_register(struct file *file, void *priv,
946 struct v4l2_dbg_register *reg)
948 int ret;
949 struct gspca_dev *gspca_dev = priv;
951 if (!gspca_dev->sd_desc->get_chip_ident)
952 return -EINVAL;
954 if (!gspca_dev->sd_desc->get_register)
955 return -EINVAL;
957 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
958 return -ERESTARTSYS;
959 gspca_dev->usb_err = 0;
960 if (gspca_dev->present)
961 ret = gspca_dev->sd_desc->get_register(gspca_dev, reg);
962 else
963 ret = -ENODEV;
964 mutex_unlock(&gspca_dev->usb_lock);
966 return ret;
969 static int vidioc_s_register(struct file *file, void *priv,
970 struct v4l2_dbg_register *reg)
972 int ret;
973 struct gspca_dev *gspca_dev = priv;
975 if (!gspca_dev->sd_desc->get_chip_ident)
976 return -EINVAL;
978 if (!gspca_dev->sd_desc->set_register)
979 return -EINVAL;
981 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
982 return -ERESTARTSYS;
983 gspca_dev->usb_err = 0;
984 if (gspca_dev->present)
985 ret = gspca_dev->sd_desc->set_register(gspca_dev, reg);
986 else
987 ret = -ENODEV;
988 mutex_unlock(&gspca_dev->usb_lock);
990 return ret;
992 #endif
994 static int vidioc_g_chip_ident(struct file *file, void *priv,
995 struct v4l2_dbg_chip_ident *chip)
997 int ret;
998 struct gspca_dev *gspca_dev = priv;
1000 if (!gspca_dev->sd_desc->get_chip_ident)
1001 return -EINVAL;
1003 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1004 return -ERESTARTSYS;
1005 gspca_dev->usb_err = 0;
1006 if (gspca_dev->present)
1007 ret = gspca_dev->sd_desc->get_chip_ident(gspca_dev, chip);
1008 else
1009 ret = -ENODEV;
1010 mutex_unlock(&gspca_dev->usb_lock);
1012 return ret;
1015 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1016 struct v4l2_fmtdesc *fmtdesc)
1018 struct gspca_dev *gspca_dev = priv;
1019 int i, j, index;
1020 __u32 fmt_tb[8];
1022 /* give an index to each format */
1023 index = 0;
1024 j = 0;
1025 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1026 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1027 j = 0;
1028 for (;;) {
1029 if (fmt_tb[j] == fmt_tb[index])
1030 break;
1031 j++;
1033 if (j == index) {
1034 if (fmtdesc->index == index)
1035 break; /* new format */
1036 index++;
1037 if (index >= ARRAY_SIZE(fmt_tb))
1038 return -EINVAL;
1041 if (i < 0)
1042 return -EINVAL; /* no more format */
1044 fmtdesc->pixelformat = fmt_tb[index];
1045 if (gspca_is_compressed(fmt_tb[index]))
1046 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1047 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1048 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1049 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1050 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1051 fmtdesc->description[4] = '\0';
1052 return 0;
1055 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1056 struct v4l2_format *fmt)
1058 struct gspca_dev *gspca_dev = priv;
1059 int mode;
1061 mode = gspca_dev->curr_mode;
1062 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1063 sizeof fmt->fmt.pix);
1064 return 0;
1067 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1068 struct v4l2_format *fmt)
1070 int w, h, mode, mode2;
1072 w = fmt->fmt.pix.width;
1073 h = fmt->fmt.pix.height;
1075 #ifdef GSPCA_DEBUG
1076 if (gspca_debug & D_CONF)
1077 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
1078 #endif
1079 /* search the closest mode for width and height */
1080 mode = wxh_to_mode(gspca_dev, w, h);
1082 /* OK if right palette */
1083 if (gspca_dev->cam.cam_mode[mode].pixelformat
1084 != fmt->fmt.pix.pixelformat) {
1086 /* else, search the closest mode with the same pixel format */
1087 mode2 = gspca_get_mode(gspca_dev, mode,
1088 fmt->fmt.pix.pixelformat);
1089 if (mode2 >= 0)
1090 mode = mode2;
1091 /* else
1092 ; * no chance, return this mode */
1094 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
1095 sizeof fmt->fmt.pix);
1096 return mode; /* used when s_fmt */
1099 static int vidioc_try_fmt_vid_cap(struct file *file,
1100 void *priv,
1101 struct v4l2_format *fmt)
1103 struct gspca_dev *gspca_dev = priv;
1104 int ret;
1106 ret = try_fmt_vid_cap(gspca_dev, fmt);
1107 if (ret < 0)
1108 return ret;
1109 return 0;
1112 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1113 struct v4l2_format *fmt)
1115 struct gspca_dev *gspca_dev = priv;
1116 int ret;
1118 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1119 return -ERESTARTSYS;
1121 ret = try_fmt_vid_cap(gspca_dev, fmt);
1122 if (ret < 0)
1123 goto out;
1125 if (gspca_dev->nframes != 0
1126 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1127 ret = -EINVAL;
1128 goto out;
1131 if (ret == gspca_dev->curr_mode) {
1132 ret = 0;
1133 goto out; /* same mode */
1136 if (gspca_dev->streaming) {
1137 ret = -EBUSY;
1138 goto out;
1140 gspca_dev->width = fmt->fmt.pix.width;
1141 gspca_dev->height = fmt->fmt.pix.height;
1142 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
1143 gspca_dev->curr_mode = ret;
1145 ret = 0;
1146 out:
1147 mutex_unlock(&gspca_dev->queue_lock);
1148 return ret;
1151 static int vidioc_enum_framesizes(struct file *file, void *priv,
1152 struct v4l2_frmsizeenum *fsize)
1154 struct gspca_dev *gspca_dev = priv;
1155 int i;
1156 __u32 index = 0;
1158 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1159 if (fsize->pixel_format !=
1160 gspca_dev->cam.cam_mode[i].pixelformat)
1161 continue;
1163 if (fsize->index == index) {
1164 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1165 fsize->discrete.width =
1166 gspca_dev->cam.cam_mode[i].width;
1167 fsize->discrete.height =
1168 gspca_dev->cam.cam_mode[i].height;
1169 return 0;
1171 index++;
1174 return -EINVAL;
1177 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1178 struct v4l2_frmivalenum *fival)
1180 struct gspca_dev *gspca_dev = priv;
1181 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1182 __u32 i;
1184 if (gspca_dev->cam.mode_framerates == NULL ||
1185 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1186 return -EINVAL;
1188 if (fival->pixel_format !=
1189 gspca_dev->cam.cam_mode[mode].pixelformat)
1190 return -EINVAL;
1192 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1193 if (fival->index == i) {
1194 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1195 fival->discrete.numerator = 1;
1196 fival->discrete.denominator =
1197 gspca_dev->cam.mode_framerates[mode].rates[i];
1198 return 0;
1202 return -EINVAL;
1205 static void gspca_release(struct video_device *vfd)
1207 struct gspca_dev *gspca_dev = container_of(vfd, struct gspca_dev, vdev);
1209 PDEBUG(D_PROBE, "%s released",
1210 video_device_node_name(&gspca_dev->vdev));
1212 kfree(gspca_dev->usb_buf);
1213 kfree(gspca_dev);
1216 static int dev_open(struct file *file)
1218 struct gspca_dev *gspca_dev;
1219 int ret;
1221 PDEBUG(D_STREAM, "[%s] open", current->comm);
1222 gspca_dev = (struct gspca_dev *) video_devdata(file);
1223 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1224 return -ERESTARTSYS;
1225 if (!gspca_dev->present) {
1226 ret = -ENODEV;
1227 goto out;
1230 if (gspca_dev->users > 4) { /* (arbitrary value) */
1231 ret = -EBUSY;
1232 goto out;
1235 /* protect the subdriver against rmmod */
1236 if (!try_module_get(gspca_dev->module)) {
1237 ret = -ENODEV;
1238 goto out;
1241 gspca_dev->users++;
1243 file->private_data = gspca_dev;
1244 #ifdef GSPCA_DEBUG
1245 /* activate the v4l2 debug */
1246 if (gspca_debug & D_V4L2)
1247 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
1248 | V4L2_DEBUG_IOCTL_ARG;
1249 else
1250 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
1251 | V4L2_DEBUG_IOCTL_ARG);
1252 #endif
1253 ret = 0;
1254 out:
1255 mutex_unlock(&gspca_dev->queue_lock);
1256 if (ret != 0)
1257 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
1258 else
1259 PDEBUG(D_STREAM, "open done");
1260 return ret;
1263 static int dev_close(struct file *file)
1265 struct gspca_dev *gspca_dev = file->private_data;
1267 PDEBUG(D_STREAM, "[%s] close", current->comm);
1268 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1269 return -ERESTARTSYS;
1270 gspca_dev->users--;
1272 /* if the file did the capture, free the streaming resources */
1273 if (gspca_dev->capt_file == file) {
1274 if (gspca_dev->streaming) {
1275 mutex_lock(&gspca_dev->usb_lock);
1276 gspca_dev->usb_err = 0;
1277 gspca_stream_off(gspca_dev);
1278 mutex_unlock(&gspca_dev->usb_lock);
1280 frame_free(gspca_dev);
1281 gspca_dev->capt_file = NULL;
1282 gspca_dev->memory = GSPCA_MEMORY_NO;
1284 file->private_data = NULL;
1285 module_put(gspca_dev->module);
1286 mutex_unlock(&gspca_dev->queue_lock);
1288 PDEBUG(D_STREAM, "close done");
1290 return 0;
1293 static int vidioc_querycap(struct file *file, void *priv,
1294 struct v4l2_capability *cap)
1296 struct gspca_dev *gspca_dev = priv;
1297 int ret;
1299 /* protect the access to the usb device */
1300 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1301 return -ERESTARTSYS;
1302 if (!gspca_dev->present) {
1303 ret = -ENODEV;
1304 goto out;
1306 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1307 if (gspca_dev->dev->product != NULL) {
1308 strncpy(cap->card, gspca_dev->dev->product,
1309 sizeof cap->card);
1310 } else {
1311 snprintf(cap->card, sizeof cap->card,
1312 "USB Camera (%04x:%04x)",
1313 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1314 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1316 usb_make_path(gspca_dev->dev, cap->bus_info, sizeof(cap->bus_info));
1317 cap->version = DRIVER_VERSION_NUMBER;
1318 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1319 | V4L2_CAP_STREAMING
1320 | V4L2_CAP_READWRITE;
1321 ret = 0;
1322 out:
1323 mutex_unlock(&gspca_dev->usb_lock);
1324 return ret;
1327 static const struct ctrl *get_ctrl(struct gspca_dev *gspca_dev,
1328 int id)
1330 const struct ctrl *ctrls;
1331 int i;
1333 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1334 i < gspca_dev->sd_desc->nctrls;
1335 i++, ctrls++) {
1336 if (gspca_dev->ctrl_dis & (1 << i))
1337 continue;
1338 if (id == ctrls->qctrl.id)
1339 return ctrls;
1341 return NULL;
1344 static int vidioc_queryctrl(struct file *file, void *priv,
1345 struct v4l2_queryctrl *q_ctrl)
1347 struct gspca_dev *gspca_dev = priv;
1348 const struct ctrl *ctrls;
1349 int i;
1350 u32 id;
1352 ctrls = NULL;
1353 id = q_ctrl->id;
1354 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1355 id &= V4L2_CTRL_ID_MASK;
1356 id++;
1357 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1358 if (gspca_dev->ctrl_dis & (1 << i))
1359 continue;
1360 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
1361 continue;
1362 if (ctrls && gspca_dev->sd_desc->ctrls[i].qctrl.id
1363 > ctrls->qctrl.id)
1364 continue;
1365 ctrls = &gspca_dev->sd_desc->ctrls[i];
1367 if (ctrls == NULL)
1368 return -EINVAL;
1369 } else {
1370 ctrls = get_ctrl(gspca_dev, id);
1371 if (ctrls == NULL)
1372 return -EINVAL;
1373 i = ctrls - gspca_dev->sd_desc->ctrls;
1375 memcpy(q_ctrl, ctrls, sizeof *q_ctrl);
1376 if (gspca_dev->ctrl_inac & (1 << i))
1377 q_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
1378 return 0;
1381 static int vidioc_s_ctrl(struct file *file, void *priv,
1382 struct v4l2_control *ctrl)
1384 struct gspca_dev *gspca_dev = priv;
1385 const struct ctrl *ctrls;
1386 int ret;
1388 ctrls = get_ctrl(gspca_dev, ctrl->id);
1389 if (ctrls == NULL)
1390 return -EINVAL;
1392 if (ctrl->value < ctrls->qctrl.minimum
1393 || ctrl->value > ctrls->qctrl.maximum)
1394 return -ERANGE;
1395 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1396 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1397 return -ERESTARTSYS;
1398 gspca_dev->usb_err = 0;
1399 if (gspca_dev->present)
1400 ret = ctrls->set(gspca_dev, ctrl->value);
1401 else
1402 ret = -ENODEV;
1403 mutex_unlock(&gspca_dev->usb_lock);
1404 return ret;
1407 static int vidioc_g_ctrl(struct file *file, void *priv,
1408 struct v4l2_control *ctrl)
1410 struct gspca_dev *gspca_dev = priv;
1411 const struct ctrl *ctrls;
1412 int ret;
1414 ctrls = get_ctrl(gspca_dev, ctrl->id);
1415 if (ctrls == NULL)
1416 return -EINVAL;
1418 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1419 return -ERESTARTSYS;
1420 gspca_dev->usb_err = 0;
1421 if (gspca_dev->present)
1422 ret = ctrls->get(gspca_dev, &ctrl->value);
1423 else
1424 ret = -ENODEV;
1425 mutex_unlock(&gspca_dev->usb_lock);
1426 return ret;
1429 /*fixme: have an audio flag in gspca_dev?*/
1430 static int vidioc_s_audio(struct file *file, void *priv,
1431 struct v4l2_audio *audio)
1433 if (audio->index != 0)
1434 return -EINVAL;
1435 return 0;
1438 static int vidioc_g_audio(struct file *file, void *priv,
1439 struct v4l2_audio *audio)
1441 strcpy(audio->name, "Microphone");
1442 return 0;
1445 static int vidioc_enumaudio(struct file *file, void *priv,
1446 struct v4l2_audio *audio)
1448 if (audio->index != 0)
1449 return -EINVAL;
1451 strcpy(audio->name, "Microphone");
1452 audio->capability = 0;
1453 audio->mode = 0;
1454 return 0;
1457 static int vidioc_querymenu(struct file *file, void *priv,
1458 struct v4l2_querymenu *qmenu)
1460 struct gspca_dev *gspca_dev = priv;
1462 if (!gspca_dev->sd_desc->querymenu)
1463 return -EINVAL;
1464 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1467 static int vidioc_enum_input(struct file *file, void *priv,
1468 struct v4l2_input *input)
1470 struct gspca_dev *gspca_dev = priv;
1472 if (input->index != 0)
1473 return -EINVAL;
1474 input->type = V4L2_INPUT_TYPE_CAMERA;
1475 input->status = gspca_dev->cam.input_flags;
1476 strncpy(input->name, gspca_dev->sd_desc->name,
1477 sizeof input->name);
1478 return 0;
1481 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1483 *i = 0;
1484 return 0;
1487 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1489 if (i > 0)
1490 return -EINVAL;
1491 return (0);
1494 static int vidioc_reqbufs(struct file *file, void *priv,
1495 struct v4l2_requestbuffers *rb)
1497 struct gspca_dev *gspca_dev = priv;
1498 int i, ret = 0;
1500 switch (rb->memory) {
1501 case GSPCA_MEMORY_READ: /* (internal call) */
1502 case V4L2_MEMORY_MMAP:
1503 case V4L2_MEMORY_USERPTR:
1504 break;
1505 default:
1506 return -EINVAL;
1508 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1509 return -ERESTARTSYS;
1511 if (gspca_dev->memory != GSPCA_MEMORY_NO
1512 && gspca_dev->memory != rb->memory) {
1513 ret = -EBUSY;
1514 goto out;
1517 /* only one file may do the capture */
1518 if (gspca_dev->capt_file != NULL
1519 && gspca_dev->capt_file != file) {
1520 ret = -EBUSY;
1521 goto out;
1524 /* if allocated, the buffers must not be mapped */
1525 for (i = 0; i < gspca_dev->nframes; i++) {
1526 if (gspca_dev->frame[i].vma_use_count) {
1527 ret = -EBUSY;
1528 goto out;
1532 /* stop streaming */
1533 if (gspca_dev->streaming) {
1534 mutex_lock(&gspca_dev->usb_lock);
1535 gspca_dev->usb_err = 0;
1536 gspca_stream_off(gspca_dev);
1537 mutex_unlock(&gspca_dev->usb_lock);
1540 /* free the previous allocated buffers, if any */
1541 if (gspca_dev->nframes != 0) {
1542 frame_free(gspca_dev);
1543 gspca_dev->capt_file = NULL;
1545 if (rb->count == 0) /* unrequest */
1546 goto out;
1547 gspca_dev->memory = rb->memory;
1548 ret = frame_alloc(gspca_dev, rb->count);
1549 if (ret == 0) {
1550 rb->count = gspca_dev->nframes;
1551 gspca_dev->capt_file = file;
1553 out:
1554 mutex_unlock(&gspca_dev->queue_lock);
1555 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1556 return ret;
1559 static int vidioc_querybuf(struct file *file, void *priv,
1560 struct v4l2_buffer *v4l2_buf)
1562 struct gspca_dev *gspca_dev = priv;
1563 struct gspca_frame *frame;
1565 if (v4l2_buf->index < 0
1566 || v4l2_buf->index >= gspca_dev->nframes)
1567 return -EINVAL;
1569 frame = &gspca_dev->frame[v4l2_buf->index];
1570 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1571 return 0;
1574 static int vidioc_streamon(struct file *file, void *priv,
1575 enum v4l2_buf_type buf_type)
1577 struct gspca_dev *gspca_dev = priv;
1578 int ret;
1580 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1581 return -EINVAL;
1582 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1583 return -ERESTARTSYS;
1585 if (gspca_dev->nframes == 0
1586 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1587 ret = -EINVAL;
1588 goto out;
1590 if (!gspca_dev->streaming) {
1591 ret = gspca_init_transfer(gspca_dev);
1592 if (ret < 0)
1593 goto out;
1595 #ifdef GSPCA_DEBUG
1596 if (gspca_debug & D_STREAM) {
1597 PDEBUG_MODE("stream on OK",
1598 gspca_dev->pixfmt,
1599 gspca_dev->width,
1600 gspca_dev->height);
1602 #endif
1603 ret = 0;
1604 out:
1605 mutex_unlock(&gspca_dev->queue_lock);
1606 return ret;
1609 static int vidioc_streamoff(struct file *file, void *priv,
1610 enum v4l2_buf_type buf_type)
1612 struct gspca_dev *gspca_dev = priv;
1613 int i, ret;
1615 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1616 return -EINVAL;
1617 if (!gspca_dev->streaming)
1618 return 0;
1619 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1620 return -ERESTARTSYS;
1622 /* stop streaming */
1623 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1624 ret = -ERESTARTSYS;
1625 goto out;
1627 gspca_dev->usb_err = 0;
1628 gspca_stream_off(gspca_dev);
1629 mutex_unlock(&gspca_dev->usb_lock);
1631 /* empty the application queues */
1632 for (i = 0; i < gspca_dev->nframes; i++)
1633 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1634 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1635 gspca_dev->last_packet_type = DISCARD_PACKET;
1636 gspca_dev->sequence = 0;
1637 ret = 0;
1638 out:
1639 mutex_unlock(&gspca_dev->queue_lock);
1640 return ret;
1643 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1644 struct v4l2_jpegcompression *jpegcomp)
1646 struct gspca_dev *gspca_dev = priv;
1647 int ret;
1649 if (!gspca_dev->sd_desc->get_jcomp)
1650 return -EINVAL;
1651 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1652 return -ERESTARTSYS;
1653 gspca_dev->usb_err = 0;
1654 if (gspca_dev->present)
1655 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1656 else
1657 ret = -ENODEV;
1658 mutex_unlock(&gspca_dev->usb_lock);
1659 return ret;
1662 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1663 struct v4l2_jpegcompression *jpegcomp)
1665 struct gspca_dev *gspca_dev = priv;
1666 int ret;
1668 if (!gspca_dev->sd_desc->set_jcomp)
1669 return -EINVAL;
1670 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1671 return -ERESTARTSYS;
1672 gspca_dev->usb_err = 0;
1673 if (gspca_dev->present)
1674 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1675 else
1676 ret = -ENODEV;
1677 mutex_unlock(&gspca_dev->usb_lock);
1678 return ret;
1681 static int vidioc_g_parm(struct file *filp, void *priv,
1682 struct v4l2_streamparm *parm)
1684 struct gspca_dev *gspca_dev = priv;
1686 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1688 if (gspca_dev->sd_desc->get_streamparm) {
1689 int ret;
1691 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1692 return -ERESTARTSYS;
1693 gspca_dev->usb_err = 0;
1694 if (gspca_dev->present)
1695 ret = gspca_dev->sd_desc->get_streamparm(gspca_dev,
1696 parm);
1697 else
1698 ret = -ENODEV;
1699 mutex_unlock(&gspca_dev->usb_lock);
1700 return ret;
1703 return 0;
1706 static int vidioc_s_parm(struct file *filp, void *priv,
1707 struct v4l2_streamparm *parm)
1709 struct gspca_dev *gspca_dev = priv;
1710 int n;
1712 n = parm->parm.capture.readbuffers;
1713 if (n == 0 || n > GSPCA_MAX_FRAMES)
1714 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1715 else
1716 gspca_dev->nbufread = n;
1718 if (gspca_dev->sd_desc->set_streamparm) {
1719 int ret;
1721 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1722 return -ERESTARTSYS;
1723 gspca_dev->usb_err = 0;
1724 if (gspca_dev->present)
1725 ret = gspca_dev->sd_desc->set_streamparm(gspca_dev,
1726 parm);
1727 else
1728 ret = -ENODEV;
1729 mutex_unlock(&gspca_dev->usb_lock);
1730 return ret;
1733 return 0;
1736 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1737 static int vidiocgmbuf(struct file *file, void *priv,
1738 struct video_mbuf *mbuf)
1740 struct gspca_dev *gspca_dev = file->private_data;
1741 int i;
1743 PDEBUG(D_STREAM, "cgmbuf");
1744 if (gspca_dev->nframes == 0) {
1745 int ret;
1748 struct v4l2_format fmt;
1750 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1751 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1752 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1753 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1754 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1755 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1756 if (ret != 0)
1757 return ret;
1760 struct v4l2_requestbuffers rb;
1762 memset(&rb, 0, sizeof rb);
1763 rb.count = 4;
1764 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1765 rb.memory = V4L2_MEMORY_MMAP;
1766 ret = vidioc_reqbufs(file, priv, &rb);
1767 if (ret != 0)
1768 return ret;
1771 mbuf->frames = gspca_dev->nframes;
1772 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1773 for (i = 0; i < mbuf->frames; i++)
1774 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1775 return 0;
1777 #endif
1779 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1781 struct gspca_dev *gspca_dev = file->private_data;
1782 struct gspca_frame *frame;
1783 struct page *page;
1784 unsigned long addr, start, size;
1785 int i, ret;
1787 start = vma->vm_start;
1788 size = vma->vm_end - vma->vm_start;
1789 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1791 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1792 return -ERESTARTSYS;
1793 if (!gspca_dev->present) {
1794 ret = -ENODEV;
1795 goto out;
1797 if (gspca_dev->capt_file != file) {
1798 ret = -EINVAL;
1799 goto out;
1802 frame = NULL;
1803 for (i = 0; i < gspca_dev->nframes; ++i) {
1804 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1805 PDEBUG(D_STREAM, "mmap bad memory type");
1806 break;
1808 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1809 == vma->vm_pgoff) {
1810 frame = &gspca_dev->frame[i];
1811 break;
1814 if (frame == NULL) {
1815 PDEBUG(D_STREAM, "mmap no frame buffer found");
1816 ret = -EINVAL;
1817 goto out;
1819 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1820 /* v4l1 maps all the buffers */
1821 if (i != 0
1822 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1823 #endif
1824 if (size != frame->v4l2_buf.length) {
1825 PDEBUG(D_STREAM, "mmap bad size");
1826 ret = -EINVAL;
1827 goto out;
1831 * - VM_IO marks the area as being a mmaped region for I/O to a
1832 * device. It also prevents the region from being core dumped.
1834 vma->vm_flags |= VM_IO;
1836 addr = (unsigned long) frame->data;
1837 while (size > 0) {
1838 page = vmalloc_to_page((void *) addr);
1839 ret = vm_insert_page(vma, start, page);
1840 if (ret < 0)
1841 goto out;
1842 start += PAGE_SIZE;
1843 addr += PAGE_SIZE;
1844 size -= PAGE_SIZE;
1847 vma->vm_ops = &gspca_vm_ops;
1848 vma->vm_private_data = frame;
1849 gspca_vm_open(vma);
1850 ret = 0;
1851 out:
1852 mutex_unlock(&gspca_dev->queue_lock);
1853 return ret;
1857 * wait for a video frame
1859 * If a frame is ready, its index is returned.
1861 static int frame_wait(struct gspca_dev *gspca_dev,
1862 int nonblock_ing)
1864 struct gspca_frame *frame;
1865 int i, j, ret;
1867 /* check if a frame is ready */
1868 i = gspca_dev->fr_o;
1869 j = gspca_dev->fr_queue[i];
1870 frame = &gspca_dev->frame[j];
1872 if (!(frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)) {
1873 if (nonblock_ing)
1874 return -EAGAIN;
1876 /* wait till a frame is ready */
1877 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1878 (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) ||
1879 !gspca_dev->streaming || !gspca_dev->present,
1880 msecs_to_jiffies(3000));
1881 if (ret < 0)
1882 return ret;
1883 if (ret == 0 || !gspca_dev->streaming || !gspca_dev->present)
1884 return -EIO;
1887 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1888 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1889 gspca_dev->fr_q,
1890 gspca_dev->fr_i,
1891 gspca_dev->fr_o);
1893 if (gspca_dev->sd_desc->dq_callback) {
1894 mutex_lock(&gspca_dev->usb_lock);
1895 gspca_dev->usb_err = 0;
1896 if (gspca_dev->present)
1897 gspca_dev->sd_desc->dq_callback(gspca_dev);
1898 mutex_unlock(&gspca_dev->usb_lock);
1900 return j;
1904 * dequeue a video buffer
1906 * If nonblock_ing is false, block until a buffer is available.
1908 static int vidioc_dqbuf(struct file *file, void *priv,
1909 struct v4l2_buffer *v4l2_buf)
1911 struct gspca_dev *gspca_dev = priv;
1912 struct gspca_frame *frame;
1913 int i, ret;
1915 PDEBUG(D_FRAM, "dqbuf");
1916 if (v4l2_buf->memory != gspca_dev->memory)
1917 return -EINVAL;
1919 if (!gspca_dev->present)
1920 return -ENODEV;
1922 /* if not streaming, be sure the application will not loop forever */
1923 if (!(file->f_flags & O_NONBLOCK)
1924 && !gspca_dev->streaming && gspca_dev->users == 1)
1925 return -EINVAL;
1927 /* only the capturing file may dequeue */
1928 if (gspca_dev->capt_file != file)
1929 return -EINVAL;
1931 /* only one dequeue / read at a time */
1932 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1933 return -ERESTARTSYS;
1935 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1936 if (ret < 0)
1937 goto out;
1938 i = ret; /* frame index */
1939 frame = &gspca_dev->frame[i];
1940 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1941 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1942 frame->data,
1943 frame->v4l2_buf.bytesused)) {
1944 PDEBUG(D_ERR|D_STREAM,
1945 "dqbuf cp to user failed");
1946 ret = -EFAULT;
1947 goto out;
1950 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1951 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1952 PDEBUG(D_FRAM, "dqbuf %d", i);
1953 ret = 0;
1954 out:
1955 mutex_unlock(&gspca_dev->read_lock);
1956 return ret;
1960 * queue a video buffer
1962 * Attempting to queue a buffer that has already been
1963 * queued will return -EINVAL.
1965 static int vidioc_qbuf(struct file *file, void *priv,
1966 struct v4l2_buffer *v4l2_buf)
1968 struct gspca_dev *gspca_dev = priv;
1969 struct gspca_frame *frame;
1970 int i, index, ret;
1972 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1974 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1975 return -ERESTARTSYS;
1977 index = v4l2_buf->index;
1978 if ((unsigned) index >= gspca_dev->nframes) {
1979 PDEBUG(D_FRAM,
1980 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1981 ret = -EINVAL;
1982 goto out;
1984 if (v4l2_buf->memory != gspca_dev->memory) {
1985 PDEBUG(D_FRAM, "qbuf bad memory type");
1986 ret = -EINVAL;
1987 goto out;
1990 frame = &gspca_dev->frame[index];
1991 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1992 PDEBUG(D_FRAM, "qbuf bad state");
1993 ret = -EINVAL;
1994 goto out;
1997 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1999 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
2000 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
2001 frame->v4l2_buf.length = v4l2_buf->length;
2004 /* put the buffer in the 'queued' queue */
2005 i = gspca_dev->fr_q;
2006 gspca_dev->fr_queue[i] = index;
2007 if (gspca_dev->fr_i == i)
2008 gspca_dev->cur_frame = frame;
2009 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
2010 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
2011 gspca_dev->fr_q,
2012 gspca_dev->fr_i,
2013 gspca_dev->fr_o);
2015 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
2016 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
2017 ret = 0;
2018 out:
2019 mutex_unlock(&gspca_dev->queue_lock);
2020 return ret;
2024 * allocate the resources for read()
2026 static int read_alloc(struct gspca_dev *gspca_dev,
2027 struct file *file)
2029 struct v4l2_buffer v4l2_buf;
2030 int i, ret;
2032 PDEBUG(D_STREAM, "read alloc");
2033 if (gspca_dev->nframes == 0) {
2034 struct v4l2_requestbuffers rb;
2036 memset(&rb, 0, sizeof rb);
2037 rb.count = gspca_dev->nbufread;
2038 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2039 rb.memory = GSPCA_MEMORY_READ;
2040 ret = vidioc_reqbufs(file, gspca_dev, &rb);
2041 if (ret != 0) {
2042 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
2043 return ret;
2045 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2046 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2047 v4l2_buf.memory = GSPCA_MEMORY_READ;
2048 for (i = 0; i < gspca_dev->nbufread; i++) {
2049 v4l2_buf.index = i;
2050 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2051 if (ret != 0) {
2052 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
2053 return ret;
2056 gspca_dev->memory = GSPCA_MEMORY_READ;
2059 /* start streaming */
2060 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2061 if (ret != 0)
2062 PDEBUG(D_STREAM, "read streamon err %d", ret);
2063 return ret;
2066 static unsigned int dev_poll(struct file *file, poll_table *wait)
2068 struct gspca_dev *gspca_dev = file->private_data;
2069 int i, ret;
2071 PDEBUG(D_FRAM, "poll");
2073 poll_wait(file, &gspca_dev->wq, wait);
2075 /* if reqbufs is not done, the user would use read() */
2076 if (gspca_dev->nframes == 0) {
2077 if (gspca_dev->memory != GSPCA_MEMORY_NO)
2078 return POLLERR; /* not the 1st time */
2079 ret = read_alloc(gspca_dev, file);
2080 if (ret != 0)
2081 return POLLERR;
2084 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
2085 return POLLERR;
2087 /* check the next incoming buffer */
2088 i = gspca_dev->fr_o;
2089 i = gspca_dev->fr_queue[i];
2090 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
2091 ret = POLLIN | POLLRDNORM; /* something to read */
2092 else
2093 ret = 0;
2094 mutex_unlock(&gspca_dev->queue_lock);
2095 if (!gspca_dev->present)
2096 return POLLHUP;
2097 return ret;
2100 static ssize_t dev_read(struct file *file, char __user *data,
2101 size_t count, loff_t *ppos)
2103 struct gspca_dev *gspca_dev = file->private_data;
2104 struct gspca_frame *frame;
2105 struct v4l2_buffer v4l2_buf;
2106 struct timeval timestamp;
2107 int n, ret, ret2;
2109 PDEBUG(D_FRAM, "read (%zd)", count);
2110 if (!gspca_dev->present)
2111 return -ENODEV;
2112 switch (gspca_dev->memory) {
2113 case GSPCA_MEMORY_NO: /* first time */
2114 ret = read_alloc(gspca_dev, file);
2115 if (ret != 0)
2116 return ret;
2117 break;
2118 case GSPCA_MEMORY_READ:
2119 if (gspca_dev->capt_file == file)
2120 break;
2121 /* fall thru */
2122 default:
2123 return -EINVAL;
2126 /* get a frame */
2127 jiffies_to_timeval(get_jiffies_64(), &timestamp);
2128 timestamp.tv_sec--;
2129 n = 2;
2130 for (;;) {
2131 memset(&v4l2_buf, 0, sizeof v4l2_buf);
2132 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2133 v4l2_buf.memory = GSPCA_MEMORY_READ;
2134 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
2135 if (ret != 0) {
2136 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
2137 return ret;
2140 /* if the process slept for more than 1 second,
2141 * get a newer frame */
2142 frame = &gspca_dev->frame[v4l2_buf.index];
2143 if (--n < 0)
2144 break; /* avoid infinite loop */
2145 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
2146 break;
2147 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2148 if (ret != 0) {
2149 PDEBUG(D_STREAM, "read qbuf err %d", ret);
2150 return ret;
2154 /* copy the frame */
2155 if (count > frame->v4l2_buf.bytesused)
2156 count = frame->v4l2_buf.bytesused;
2157 ret = copy_to_user(data, frame->data, count);
2158 if (ret != 0) {
2159 PDEBUG(D_ERR|D_STREAM,
2160 "read cp to user lack %d / %zd", ret, count);
2161 ret = -EFAULT;
2162 goto out;
2164 ret = count;
2165 out:
2166 /* in each case, requeue the buffer */
2167 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
2168 if (ret2 != 0)
2169 return ret2;
2170 return ret;
2173 static struct v4l2_file_operations dev_fops = {
2174 .owner = THIS_MODULE,
2175 .open = dev_open,
2176 .release = dev_close,
2177 .read = dev_read,
2178 .mmap = dev_mmap,
2179 .unlocked_ioctl = video_ioctl2,
2180 .poll = dev_poll,
2183 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
2184 .vidioc_querycap = vidioc_querycap,
2185 .vidioc_dqbuf = vidioc_dqbuf,
2186 .vidioc_qbuf = vidioc_qbuf,
2187 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2188 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2189 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2190 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2191 .vidioc_streamon = vidioc_streamon,
2192 .vidioc_queryctrl = vidioc_queryctrl,
2193 .vidioc_g_ctrl = vidioc_g_ctrl,
2194 .vidioc_s_ctrl = vidioc_s_ctrl,
2195 .vidioc_g_audio = vidioc_g_audio,
2196 .vidioc_s_audio = vidioc_s_audio,
2197 .vidioc_enumaudio = vidioc_enumaudio,
2198 .vidioc_querymenu = vidioc_querymenu,
2199 .vidioc_enum_input = vidioc_enum_input,
2200 .vidioc_g_input = vidioc_g_input,
2201 .vidioc_s_input = vidioc_s_input,
2202 .vidioc_reqbufs = vidioc_reqbufs,
2203 .vidioc_querybuf = vidioc_querybuf,
2204 .vidioc_streamoff = vidioc_streamoff,
2205 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
2206 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
2207 .vidioc_g_parm = vidioc_g_parm,
2208 .vidioc_s_parm = vidioc_s_parm,
2209 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2210 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2211 #ifdef CONFIG_VIDEO_ADV_DEBUG
2212 .vidioc_g_register = vidioc_g_register,
2213 .vidioc_s_register = vidioc_s_register,
2214 #endif
2215 .vidioc_g_chip_ident = vidioc_g_chip_ident,
2216 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2217 .vidiocgmbuf = vidiocgmbuf,
2218 #endif
2221 static struct video_device gspca_template = {
2222 .name = "gspca main driver",
2223 .fops = &dev_fops,
2224 .ioctl_ops = &dev_ioctl_ops,
2225 .release = gspca_release,
2229 * probe and create a new gspca device
2231 * This function must be called by the sub-driver when it is
2232 * called for probing a new device.
2234 int gspca_dev_probe(struct usb_interface *intf,
2235 const struct usb_device_id *id,
2236 const struct sd_desc *sd_desc,
2237 int dev_size,
2238 struct module *module)
2240 struct usb_interface_descriptor *interface;
2241 struct gspca_dev *gspca_dev;
2242 struct usb_device *dev = interface_to_usbdev(intf);
2243 int ret;
2245 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
2247 /* we don't handle multi-config cameras */
2248 if (dev->descriptor.bNumConfigurations != 1) {
2249 PDEBUG(D_ERR, "Too many config");
2250 return -ENODEV;
2253 /* the USB video interface must be the first one */
2254 interface = &intf->cur_altsetting->desc;
2255 if (dev->config->desc.bNumInterfaces != 1 &&
2256 interface->bInterfaceNumber != 0)
2257 return -ENODEV;
2259 /* create the device */
2260 if (dev_size < sizeof *gspca_dev)
2261 dev_size = sizeof *gspca_dev;
2262 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2263 if (!gspca_dev) {
2264 err("couldn't kzalloc gspca struct");
2265 return -ENOMEM;
2267 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2268 if (!gspca_dev->usb_buf) {
2269 err("out of memory");
2270 ret = -ENOMEM;
2271 goto out;
2273 gspca_dev->dev = dev;
2274 gspca_dev->iface = interface->bInterfaceNumber;
2275 gspca_dev->nbalt = intf->num_altsetting;
2276 gspca_dev->sd_desc = sd_desc;
2277 gspca_dev->nbufread = 2;
2278 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2280 /* configure the subdriver and initialize the USB device */
2281 ret = sd_desc->config(gspca_dev, id);
2282 if (ret < 0)
2283 goto out;
2284 ret = sd_desc->init(gspca_dev);
2285 if (ret < 0)
2286 goto out;
2287 gspca_set_default_mode(gspca_dev);
2289 ret = gspca_input_connect(gspca_dev);
2290 if (ret)
2291 goto out;
2293 mutex_init(&gspca_dev->usb_lock);
2294 mutex_init(&gspca_dev->read_lock);
2295 mutex_init(&gspca_dev->queue_lock);
2296 init_waitqueue_head(&gspca_dev->wq);
2298 /* init video stuff */
2299 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
2300 gspca_dev->vdev.parent = &intf->dev;
2301 gspca_dev->module = module;
2302 gspca_dev->present = 1;
2303 ret = video_register_device(&gspca_dev->vdev,
2304 VFL_TYPE_GRABBER,
2305 -1);
2306 if (ret < 0) {
2307 err("video_register_device err %d", ret);
2308 goto out;
2311 usb_set_intfdata(intf, gspca_dev);
2312 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2314 gspca_input_create_urb(gspca_dev);
2316 return 0;
2317 out:
2318 #ifdef CONFIG_INPUT
2319 if (gspca_dev->input_dev)
2320 input_unregister_device(gspca_dev->input_dev);
2321 #endif
2322 kfree(gspca_dev->usb_buf);
2323 kfree(gspca_dev);
2324 return ret;
2326 EXPORT_SYMBOL(gspca_dev_probe);
2329 * USB disconnection
2331 * This function must be called by the sub-driver
2332 * when the device disconnects, after the specific resources are freed.
2334 void gspca_disconnect(struct usb_interface *intf)
2336 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2337 #ifdef CONFIG_INPUT
2338 struct input_dev *input_dev;
2339 #endif
2341 PDEBUG(D_PROBE, "%s disconnect",
2342 video_device_node_name(&gspca_dev->vdev));
2343 mutex_lock(&gspca_dev->usb_lock);
2344 gspca_dev->present = 0;
2346 if (gspca_dev->streaming) {
2347 destroy_urbs(gspca_dev);
2348 wake_up_interruptible(&gspca_dev->wq);
2351 #ifdef CONFIG_INPUT
2352 gspca_input_destroy_urb(gspca_dev);
2353 input_dev = gspca_dev->input_dev;
2354 if (input_dev) {
2355 gspca_dev->input_dev = NULL;
2356 input_unregister_device(input_dev);
2358 #endif
2360 /* the device is freed at exit of this function */
2361 gspca_dev->dev = NULL;
2362 mutex_unlock(&gspca_dev->usb_lock);
2364 usb_set_intfdata(intf, NULL);
2366 /* release the device */
2367 /* (this will call gspca_release() immediatly or on last close) */
2368 video_unregister_device(&gspca_dev->vdev);
2370 /* PDEBUG(D_PROBE, "disconnect complete"); */
2372 EXPORT_SYMBOL(gspca_disconnect);
2374 #ifdef CONFIG_PM
2375 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2377 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2379 if (!gspca_dev->streaming)
2380 return 0;
2381 gspca_dev->frozen = 1; /* avoid urb error messages */
2382 if (gspca_dev->sd_desc->stopN)
2383 gspca_dev->sd_desc->stopN(gspca_dev);
2384 destroy_urbs(gspca_dev);
2385 gspca_input_destroy_urb(gspca_dev);
2386 gspca_set_alt0(gspca_dev);
2387 if (gspca_dev->sd_desc->stop0)
2388 gspca_dev->sd_desc->stop0(gspca_dev);
2389 return 0;
2391 EXPORT_SYMBOL(gspca_suspend);
2393 int gspca_resume(struct usb_interface *intf)
2395 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2397 gspca_dev->frozen = 0;
2398 gspca_dev->sd_desc->init(gspca_dev);
2399 gspca_input_create_urb(gspca_dev);
2400 if (gspca_dev->streaming)
2401 return gspca_init_transfer(gspca_dev);
2402 return 0;
2404 EXPORT_SYMBOL(gspca_resume);
2405 #endif
2406 /* -- cam driver utility functions -- */
2408 /* auto gain and exposure algorithm based on the knee algorithm described here:
2409 http://ytse.tricolour.net/docs/LowLightOptimization.html
2411 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2412 where changed. */
2413 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
2414 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
2416 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
2417 const struct ctrl *gain_ctrl = NULL;
2418 const struct ctrl *exposure_ctrl = NULL;
2419 const struct ctrl *autogain_ctrl = NULL;
2420 int retval = 0;
2422 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
2423 if (gspca_dev->ctrl_dis & (1 << i))
2424 continue;
2425 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
2426 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2427 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
2428 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
2429 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
2430 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
2432 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
2433 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
2434 "on cam without (auto)gain/exposure");
2435 return 0;
2438 if (gain_ctrl->get(gspca_dev, &gain) ||
2439 exposure_ctrl->get(gspca_dev, &exposure) ||
2440 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
2441 return 0;
2443 orig_gain = gain;
2444 orig_exposure = exposure;
2446 /* If we are of a multiple of deadzone, do multiple steps to reach the
2447 desired lumination fast (with the risc of a slight overshoot) */
2448 steps = abs(desired_avg_lum - avg_lum) / deadzone;
2450 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
2451 avg_lum, desired_avg_lum, steps);
2453 for (i = 0; i < steps; i++) {
2454 if (avg_lum > desired_avg_lum) {
2455 if (gain > gain_knee)
2456 gain--;
2457 else if (exposure > exposure_knee)
2458 exposure--;
2459 else if (gain > gain_ctrl->qctrl.default_value)
2460 gain--;
2461 else if (exposure > exposure_ctrl->qctrl.minimum)
2462 exposure--;
2463 else if (gain > gain_ctrl->qctrl.minimum)
2464 gain--;
2465 else
2466 break;
2467 } else {
2468 if (gain < gain_ctrl->qctrl.default_value)
2469 gain++;
2470 else if (exposure < exposure_knee)
2471 exposure++;
2472 else if (gain < gain_knee)
2473 gain++;
2474 else if (exposure < exposure_ctrl->qctrl.maximum)
2475 exposure++;
2476 else if (gain < gain_ctrl->qctrl.maximum)
2477 gain++;
2478 else
2479 break;
2483 if (gain != orig_gain) {
2484 gain_ctrl->set(gspca_dev, gain);
2485 retval = 1;
2487 if (exposure != orig_exposure) {
2488 exposure_ctrl->set(gspca_dev, exposure);
2489 retval = 1;
2492 return retval;
2494 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2496 /* -- module insert / remove -- */
2497 static int __init gspca_init(void)
2499 info("main v%d.%d.%d registered",
2500 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2501 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2502 DRIVER_VERSION_NUMBER & 0xff);
2503 return 0;
2505 static void __exit gspca_exit(void)
2507 info("main deregistered");
2510 module_init(gspca_init);
2511 module_exit(gspca_exit);
2513 #ifdef GSPCA_DEBUG
2514 module_param_named(debug, gspca_debug, int, 0644);
2515 MODULE_PARM_DESC(debug,
2516 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2517 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2518 " 0x0100: v4l2");
2519 #endif