V4L/DVB (9083): gspca: URB_NO_TRANSFER_DMA_MAP is not useful for isoc transfers.
[linux-2.6/mini2440.git] / drivers / media / video / gspca / gspca.c
blobf130bb77f7055637938ca9d24a92a1de93ed2b47
1 /*
2 * Main USB camera driver
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
24 #include <linux/fs.h>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
31 #include <linux/io.h>
32 #include <asm/page.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <media/v4l2-ioctl.h>
37 #include "gspca.h"
39 /* global values */
40 #define DEF_NURBS 2 /* default number of URBs */
42 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
48 static int video_nr = -1;
50 #ifdef GSPCA_DEBUG
51 int gspca_debug = D_ERR | D_PROBE;
52 EXPORT_SYMBOL(gspca_debug);
54 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
58 txt,
59 pixfmt & 0xff,
60 (pixfmt >> 8) & 0xff,
61 (pixfmt >> 16) & 0xff,
62 pixfmt >> 24,
63 w, h);
64 } else {
65 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
66 txt,
67 pixfmt,
68 w, h);
71 #else
72 #define PDEBUG_MODE(txt, pixfmt, w, h)
73 #endif
75 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
76 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77 #define GSPCA_MEMORY_READ 7
79 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
82 * VMA operations.
84 static void gspca_vm_open(struct vm_area_struct *vma)
86 struct gspca_frame *frame = vma->vm_private_data;
88 frame->vma_use_count++;
89 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
92 static void gspca_vm_close(struct vm_area_struct *vma)
94 struct gspca_frame *frame = vma->vm_private_data;
96 if (--frame->vma_use_count <= 0)
97 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
100 static struct vm_operations_struct gspca_vm_ops = {
101 .open = gspca_vm_open,
102 .close = gspca_vm_close,
105 /* get the current input frame buffer */
106 struct gspca_frame *gspca_get_i_frame(struct gspca_dev *gspca_dev)
108 struct gspca_frame *frame;
109 int i;
111 i = gspca_dev->fr_i;
112 i = gspca_dev->fr_queue[i];
113 frame = &gspca_dev->frame[i];
114 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
115 != V4L2_BUF_FLAG_QUEUED)
116 return NULL;
117 return frame;
119 EXPORT_SYMBOL(gspca_get_i_frame);
122 * fill a video frame from an URB and resubmit
124 static void fill_frame(struct gspca_dev *gspca_dev,
125 struct urb *urb)
127 struct gspca_frame *frame;
128 __u8 *data; /* address of data in the iso message */
129 int i, len, st;
130 cam_pkt_op pkt_scan;
132 if (urb->status != 0) {
133 #ifdef CONFIG_PM
134 if (!gspca_dev->frozen)
135 #endif
136 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
137 return; /* disconnection ? */
139 pkt_scan = gspca_dev->sd_desc->pkt_scan;
140 for (i = 0; i < urb->number_of_packets; i++) {
142 /* check the availability of the frame buffer */
143 frame = gspca_get_i_frame(gspca_dev);
144 if (!frame) {
145 gspca_dev->last_packet_type = DISCARD_PACKET;
146 break;
149 /* check the packet status and length */
150 len = urb->iso_frame_desc[i].actual_length;
151 if (len == 0)
152 continue;
153 st = urb->iso_frame_desc[i].status;
154 if (st) {
155 PDEBUG(D_ERR,
156 "ISOC data error: [%d] len=%d, status=%d",
157 i, len, st);
158 gspca_dev->last_packet_type = DISCARD_PACKET;
159 continue;
162 /* let the packet be analyzed by the subdriver */
163 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
164 i, urb->iso_frame_desc[i].offset, len);
165 data = (__u8 *) urb->transfer_buffer
166 + urb->iso_frame_desc[i].offset;
167 pkt_scan(gspca_dev, frame, data, len);
170 /* resubmit the URB */
171 urb->status = 0;
172 st = usb_submit_urb(urb, GFP_ATOMIC);
173 if (st < 0)
174 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
178 * ISOC message interrupt from the USB device
180 * Analyse each packet and call the subdriver for copy to the frame buffer.
182 static void isoc_irq(struct urb *urb
185 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
187 PDEBUG(D_PACK, "isoc irq");
188 if (!gspca_dev->streaming)
189 return;
190 fill_frame(gspca_dev, urb);
194 * bulk message interrupt from the USB device
196 static void bulk_irq(struct urb *urb
199 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
200 struct gspca_frame *frame;
202 PDEBUG(D_PACK, "bulk irq");
203 if (!gspca_dev->streaming)
204 return;
205 if (urb->status != 0 && urb->status != -ECONNRESET) {
206 #ifdef CONFIG_PM
207 if (!gspca_dev->frozen)
208 #endif
209 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
210 return; /* disconnection ? */
213 /* check the availability of the frame buffer */
214 frame = gspca_get_i_frame(gspca_dev);
215 if (!frame) {
216 gspca_dev->last_packet_type = DISCARD_PACKET;
217 } else {
218 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
219 gspca_dev->sd_desc->pkt_scan(gspca_dev,
220 frame,
221 urb->transfer_buffer,
222 urb->actual_length);
227 * add data to the current frame
229 * This function is called by the subdrivers at interrupt level.
231 * To build a frame, these ones must add
232 * - one FIRST_PACKET
233 * - 0 or many INTER_PACKETs
234 * - one LAST_PACKET
235 * DISCARD_PACKET invalidates the whole frame.
236 * On LAST_PACKET, a new frame is returned.
238 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
239 int packet_type,
240 struct gspca_frame *frame,
241 const __u8 *data,
242 int len)
244 int i, j;
246 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
248 /* when start of a new frame, if the current frame buffer
249 * is not queued, discard the whole frame */
250 if (packet_type == FIRST_PACKET) {
251 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
252 != V4L2_BUF_FLAG_QUEUED) {
253 gspca_dev->last_packet_type = DISCARD_PACKET;
254 return frame;
256 frame->data_end = frame->data;
257 jiffies_to_timeval(get_jiffies_64(),
258 &frame->v4l2_buf.timestamp);
259 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
260 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
261 if (packet_type == LAST_PACKET)
262 gspca_dev->last_packet_type = packet_type;
263 return frame;
266 /* append the packet to the frame buffer */
267 if (len > 0) {
268 if (frame->data_end - frame->data + len
269 > frame->v4l2_buf.length) {
270 PDEBUG(D_ERR|D_PACK, "frame overflow %zd > %d",
271 frame->data_end - frame->data + len,
272 frame->v4l2_buf.length);
273 packet_type = DISCARD_PACKET;
274 } else {
275 memcpy(frame->data_end, data, len);
276 frame->data_end += len;
279 gspca_dev->last_packet_type = packet_type;
281 /* if last packet, wake the application and advance in the queue */
282 if (packet_type == LAST_PACKET) {
283 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
284 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
285 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
286 atomic_inc(&gspca_dev->nevent);
287 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
288 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
289 gspca_dev->fr_i = i;
290 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
291 frame->v4l2_buf.bytesused,
292 gspca_dev->fr_q,
294 gspca_dev->fr_o);
295 j = gspca_dev->fr_queue[i];
296 frame = &gspca_dev->frame[j];
298 return frame;
300 EXPORT_SYMBOL(gspca_frame_add);
302 static int gspca_is_compressed(__u32 format)
304 switch (format) {
305 case V4L2_PIX_FMT_MJPEG:
306 case V4L2_PIX_FMT_JPEG:
307 case V4L2_PIX_FMT_SPCA561:
308 case V4L2_PIX_FMT_PAC207:
309 return 1;
311 return 0;
314 static void *rvmalloc(unsigned long size)
316 void *mem;
317 unsigned long adr;
319 /* size = PAGE_ALIGN(size); (already done) */
320 mem = vmalloc_32(size);
321 if (mem != NULL) {
322 adr = (unsigned long) mem;
323 while ((long) size > 0) {
324 SetPageReserved(vmalloc_to_page((void *) adr));
325 adr += PAGE_SIZE;
326 size -= PAGE_SIZE;
329 return mem;
332 static void rvfree(void *mem, long size)
334 unsigned long adr;
336 adr = (unsigned long) mem;
337 while (size > 0) {
338 ClearPageReserved(vmalloc_to_page((void *) adr));
339 adr += PAGE_SIZE;
340 size -= PAGE_SIZE;
342 vfree(mem);
345 static int frame_alloc(struct gspca_dev *gspca_dev,
346 unsigned int count)
348 struct gspca_frame *frame;
349 unsigned int frsz;
350 int i;
352 i = gspca_dev->curr_mode;
353 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
354 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
355 frsz = PAGE_ALIGN(frsz);
356 gspca_dev->frsz = frsz;
357 if (count > GSPCA_MAX_FRAMES)
358 count = GSPCA_MAX_FRAMES;
359 gspca_dev->frbuf = rvmalloc(frsz * count);
360 if (!gspca_dev->frbuf) {
361 err("frame alloc failed");
362 return -ENOMEM;
364 gspca_dev->nframes = count;
365 for (i = 0; i < count; i++) {
366 frame = &gspca_dev->frame[i];
367 frame->v4l2_buf.index = i;
368 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
369 frame->v4l2_buf.flags = 0;
370 frame->v4l2_buf.field = V4L2_FIELD_NONE;
371 frame->v4l2_buf.length = frsz;
372 frame->v4l2_buf.memory = gspca_dev->memory;
373 frame->v4l2_buf.sequence = 0;
374 frame->data = frame->data_end =
375 gspca_dev->frbuf + i * frsz;
376 frame->v4l2_buf.m.offset = i * frsz;
378 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
379 gspca_dev->last_packet_type = DISCARD_PACKET;
380 gspca_dev->sequence = 0;
381 atomic_set(&gspca_dev->nevent, 0);
382 return 0;
385 static void frame_free(struct gspca_dev *gspca_dev)
387 int i;
389 PDEBUG(D_STREAM, "frame free");
390 if (gspca_dev->frbuf != NULL) {
391 rvfree(gspca_dev->frbuf,
392 gspca_dev->nframes * gspca_dev->frsz);
393 gspca_dev->frbuf = NULL;
394 for (i = 0; i < gspca_dev->nframes; i++)
395 gspca_dev->frame[i].data = NULL;
397 gspca_dev->nframes = 0;
400 static void destroy_urbs(struct gspca_dev *gspca_dev)
402 struct urb *urb;
403 unsigned int i;
405 PDEBUG(D_STREAM, "kill transfer");
406 for (i = 0; i < MAX_NURBS; ++i) {
407 urb = gspca_dev->urb[i];
408 if (urb == NULL)
409 break;
411 gspca_dev->urb[i] = NULL;
412 usb_kill_urb(urb);
413 if (urb->transfer_buffer != NULL)
414 usb_buffer_free(gspca_dev->dev,
415 urb->transfer_buffer_length,
416 urb->transfer_buffer,
417 urb->transfer_dma);
418 usb_free_urb(urb);
423 * search an input transfer endpoint in an alternate setting
425 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
426 __u8 epaddr,
427 __u8 xfer)
429 struct usb_host_endpoint *ep;
430 int i, attr;
432 epaddr |= USB_DIR_IN;
433 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
434 ep = &alt->endpoint[i];
435 if (ep->desc.bEndpointAddress == epaddr) {
436 attr = ep->desc.bmAttributes
437 & USB_ENDPOINT_XFERTYPE_MASK;
438 if (attr == xfer)
439 return ep;
440 break;
443 return NULL;
447 * search an input (isoc or bulk) endpoint
449 * The endpoint is defined by the subdriver.
450 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
451 * This routine may be called many times when the bandwidth is too small
452 * (the bandwidth is checked on urb submit).
454 static struct usb_host_endpoint *get_ep(struct gspca_dev *gspca_dev)
456 struct usb_interface *intf;
457 struct usb_host_endpoint *ep;
458 int i, ret;
460 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
461 ep = NULL;
462 i = gspca_dev->alt; /* previous alt setting */
463 while (--i > 0) { /* alt 0 is unusable */
464 ep = alt_xfer(&intf->altsetting[i],
465 gspca_dev->cam.epaddr,
466 gspca_dev->bulk
467 ? USB_ENDPOINT_XFER_BULK
468 : USB_ENDPOINT_XFER_ISOC);
469 if (ep)
470 break;
472 if (ep == NULL) {
473 err("no transfer endpoint found");
474 return NULL;
476 PDEBUG(D_STREAM, "use alt %d ep 0x%02x",
477 i, ep->desc.bEndpointAddress);
478 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
479 if (ret < 0) {
480 err("set interface err %d", ret);
481 return NULL;
483 gspca_dev->alt = i; /* memorize the current alt setting */
484 return ep;
488 * create the URBs for image transfer
490 static int create_urbs(struct gspca_dev *gspca_dev,
491 struct usb_host_endpoint *ep)
493 struct urb *urb;
494 int n, nurbs, i, psize, npkt, bsize;
496 /* calculate the packet size and the number of packets */
497 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
499 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
500 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
501 if (!gspca_dev->bulk) {
502 npkt = ISO_MAX_SIZE / psize;
503 if (npkt > ISO_MAX_PKT)
504 npkt = ISO_MAX_PKT;
505 bsize = psize * npkt;
506 PDEBUG(D_STREAM,
507 "isoc %d pkts size %d = bsize:%d",
508 npkt, psize, bsize);
509 nurbs = DEF_NURBS;
510 } else {
511 npkt = 0;
512 bsize = psize;
513 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
514 nurbs = 1;
517 gspca_dev->nurbs = nurbs;
518 for (n = 0; n < nurbs; n++) {
519 urb = usb_alloc_urb(npkt, GFP_KERNEL);
520 if (!urb) {
521 err("usb_alloc_urb failed");
522 destroy_urbs(gspca_dev);
523 return -ENOMEM;
525 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
526 bsize,
527 GFP_KERNEL,
528 &urb->transfer_dma);
530 if (urb->transfer_buffer == NULL) {
531 usb_free_urb(urb);
532 err("usb_buffer_urb failed");
533 destroy_urbs(gspca_dev);
534 return -ENOMEM;
536 gspca_dev->urb[n] = urb;
537 urb->dev = gspca_dev->dev;
538 urb->context = gspca_dev;
539 urb->transfer_buffer_length = bsize;
540 if (npkt != 0) { /* ISOC */
541 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
542 ep->desc.bEndpointAddress);
543 urb->transfer_flags = URB_ISO_ASAP;
544 urb->interval = ep->desc.bInterval;
545 urb->complete = isoc_irq;
546 urb->number_of_packets = npkt;
547 for (i = 0; i < npkt; i++) {
548 urb->iso_frame_desc[i].length = psize;
549 urb->iso_frame_desc[i].offset = psize * i;
551 } else { /* bulk */
552 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
553 ep->desc.bEndpointAddress),
554 urb->complete = bulk_irq;
557 return 0;
561 * start the USB transfer
563 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
565 struct usb_host_endpoint *ep;
566 int n, ret;
568 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
569 return -ERESTARTSYS;
571 /* set the higher alternate setting and
572 * loop until urb submit succeeds */
573 gspca_dev->alt = gspca_dev->nbalt;
574 for (;;) {
575 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
576 ep = get_ep(gspca_dev);
577 if (ep == NULL) {
578 ret = -EIO;
579 goto out;
581 ret = create_urbs(gspca_dev, ep);
582 if (ret < 0)
583 goto out;
585 /* start the cam */
586 ret = gspca_dev->sd_desc->start(gspca_dev);
587 if (ret < 0) {
588 destroy_urbs(gspca_dev);
589 goto out;
591 gspca_dev->streaming = 1;
592 atomic_set(&gspca_dev->nevent, 0);
594 /* start the bulk transfer is done by the subdriver */
595 if (gspca_dev->bulk)
596 break;
598 /* submit the URBs */
599 for (n = 0; n < gspca_dev->nurbs; n++) {
600 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
601 if (ret < 0) {
602 PDEBUG(D_ERR|D_STREAM,
603 "usb_submit_urb [%d] err %d", n, ret);
604 gspca_dev->streaming = 0;
605 destroy_urbs(gspca_dev);
606 if (ret == -ENOSPC)
607 break; /* try the previous alt */
608 goto out;
611 if (ret >= 0)
612 break;
614 out:
615 mutex_unlock(&gspca_dev->usb_lock);
616 return ret;
619 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
621 int ret;
623 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
624 if (ret < 0)
625 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
626 return ret;
629 /* Note both the queue and the usb lock should be hold when calling this */
630 static void gspca_stream_off(struct gspca_dev *gspca_dev)
632 gspca_dev->streaming = 0;
633 atomic_set(&gspca_dev->nevent, 0);
634 if (gspca_dev->present) {
635 if (gspca_dev->sd_desc->stopN)
636 gspca_dev->sd_desc->stopN(gspca_dev);
637 destroy_urbs(gspca_dev);
638 gspca_set_alt0(gspca_dev);
639 if (gspca_dev->sd_desc->stop0)
640 gspca_dev->sd_desc->stop0(gspca_dev);
641 PDEBUG(D_STREAM, "stream off OK");
645 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
647 int i;
649 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
650 gspca_dev->curr_mode = i;
651 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
652 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
653 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
656 static int wxh_to_mode(struct gspca_dev *gspca_dev,
657 int width, int height)
659 int i;
661 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
662 if (width >= gspca_dev->cam.cam_mode[i].width
663 && height >= gspca_dev->cam.cam_mode[i].height)
664 break;
666 return i;
670 * search a mode with the right pixel format
672 static int gspca_get_mode(struct gspca_dev *gspca_dev,
673 int mode,
674 int pixfmt)
676 int modeU, modeD;
678 modeU = modeD = mode;
679 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
680 if (--modeD >= 0) {
681 if (gspca_dev->cam.cam_mode[modeD].pixelformat
682 == pixfmt)
683 return modeD;
685 if (++modeU < gspca_dev->cam.nmodes) {
686 if (gspca_dev->cam.cam_mode[modeU].pixelformat
687 == pixfmt)
688 return modeU;
691 return -EINVAL;
694 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
695 struct v4l2_fmtdesc *fmtdesc)
697 struct gspca_dev *gspca_dev = priv;
698 int i, j, index;
699 __u32 fmt_tb[8];
701 /* give an index to each format */
702 index = 0;
703 j = 0;
704 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
705 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
706 j = 0;
707 for (;;) {
708 if (fmt_tb[j] == fmt_tb[index])
709 break;
710 j++;
712 if (j == index) {
713 if (fmtdesc->index == index)
714 break; /* new format */
715 index++;
716 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
717 return -EINVAL;
720 if (i < 0)
721 return -EINVAL; /* no more format */
723 fmtdesc->pixelformat = fmt_tb[index];
724 if (gspca_is_compressed(fmt_tb[index]))
725 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
726 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
727 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
728 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
729 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
730 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
731 fmtdesc->description[4] = '\0';
732 return 0;
735 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
736 struct v4l2_format *fmt)
738 struct gspca_dev *gspca_dev = priv;
739 int mode;
741 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
742 return -EINVAL;
743 mode = gspca_dev->curr_mode;
744 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
745 sizeof fmt->fmt.pix);
746 return 0;
749 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
750 struct v4l2_format *fmt)
752 int w, h, mode, mode2;
754 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
755 return -EINVAL;
756 w = fmt->fmt.pix.width;
757 h = fmt->fmt.pix.height;
759 #ifdef GSPCA_DEBUG
760 if (gspca_debug & D_CONF)
761 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
762 #endif
763 /* search the closest mode for width and height */
764 mode = wxh_to_mode(gspca_dev, w, h);
766 /* OK if right palette */
767 if (gspca_dev->cam.cam_mode[mode].pixelformat
768 != fmt->fmt.pix.pixelformat) {
770 /* else, search the closest mode with the same pixel format */
771 mode2 = gspca_get_mode(gspca_dev, mode,
772 fmt->fmt.pix.pixelformat);
773 if (mode2 >= 0)
774 mode = mode2;
775 /* else
776 ; * no chance, return this mode */
778 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
779 sizeof fmt->fmt.pix);
780 return mode; /* used when s_fmt */
783 static int vidioc_try_fmt_vid_cap(struct file *file,
784 void *priv,
785 struct v4l2_format *fmt)
787 struct gspca_dev *gspca_dev = priv;
788 int ret;
790 ret = try_fmt_vid_cap(gspca_dev, fmt);
791 if (ret < 0)
792 return ret;
793 return 0;
796 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
797 struct v4l2_format *fmt)
799 struct gspca_dev *gspca_dev = priv;
800 int ret;
802 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
803 return -ERESTARTSYS;
805 ret = try_fmt_vid_cap(gspca_dev, fmt);
806 if (ret < 0)
807 goto out;
809 if (gspca_dev->nframes != 0
810 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
811 ret = -EINVAL;
812 goto out;
815 if (ret == gspca_dev->curr_mode) {
816 ret = 0;
817 goto out; /* same mode */
820 if (gspca_dev->streaming) {
821 ret = -EBUSY;
822 goto out;
824 gspca_dev->width = fmt->fmt.pix.width;
825 gspca_dev->height = fmt->fmt.pix.height;
826 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
827 gspca_dev->curr_mode = ret;
829 ret = 0;
830 out:
831 mutex_unlock(&gspca_dev->queue_lock);
832 return ret;
835 static int dev_open(struct inode *inode, struct file *file)
837 struct gspca_dev *gspca_dev;
838 int ret;
840 PDEBUG(D_STREAM, "%s open", current->comm);
841 gspca_dev = (struct gspca_dev *) video_devdata(file);
842 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
843 return -ERESTARTSYS;
844 if (!gspca_dev->present) {
845 ret = -ENODEV;
846 goto out;
849 if (gspca_dev->users > 4) { /* (arbitrary value) */
850 ret = -EBUSY;
851 goto out;
853 gspca_dev->users++;
854 file->private_data = gspca_dev;
855 #ifdef GSPCA_DEBUG
856 /* activate the v4l2 debug */
857 if (gspca_debug & D_V4L2)
858 gspca_dev->vdev.debug |= V4L2_DEBUG_IOCTL
859 | V4L2_DEBUG_IOCTL_ARG;
860 else
861 gspca_dev->vdev.debug &= ~(V4L2_DEBUG_IOCTL
862 | V4L2_DEBUG_IOCTL_ARG);
863 #endif
864 ret = 0;
865 out:
866 mutex_unlock(&gspca_dev->queue_lock);
867 if (ret != 0)
868 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
869 else
870 PDEBUG(D_STREAM, "open done");
871 return ret;
874 static int dev_close(struct inode *inode, struct file *file)
876 struct gspca_dev *gspca_dev = file->private_data;
878 PDEBUG(D_STREAM, "%s close", current->comm);
879 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
880 return -ERESTARTSYS;
881 gspca_dev->users--;
883 /* if the file did the capture, free the streaming resources */
884 if (gspca_dev->capt_file == file) {
885 if (gspca_dev->streaming) {
886 mutex_lock(&gspca_dev->usb_lock);
887 gspca_stream_off(gspca_dev);
888 mutex_unlock(&gspca_dev->usb_lock);
890 frame_free(gspca_dev);
891 gspca_dev->capt_file = NULL;
892 gspca_dev->memory = GSPCA_MEMORY_NO;
894 file->private_data = NULL;
895 mutex_unlock(&gspca_dev->queue_lock);
896 PDEBUG(D_STREAM, "close done");
897 return 0;
900 static int vidioc_querycap(struct file *file, void *priv,
901 struct v4l2_capability *cap)
903 struct gspca_dev *gspca_dev = priv;
905 memset(cap, 0, sizeof *cap);
906 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
907 /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
908 if (gspca_dev->dev->product != NULL) {
909 strncpy(cap->card, gspca_dev->dev->product,
910 sizeof cap->card);
911 } else {
912 snprintf(cap->card, sizeof cap->card,
913 "USB Camera (%04x:%04x)",
914 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
915 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
917 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
918 sizeof cap->bus_info);
919 cap->version = DRIVER_VERSION_NUMBER;
920 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
921 | V4L2_CAP_STREAMING
922 | V4L2_CAP_READWRITE;
923 return 0;
926 static int vidioc_queryctrl(struct file *file, void *priv,
927 struct v4l2_queryctrl *q_ctrl)
929 struct gspca_dev *gspca_dev = priv;
930 int i, ix;
931 u32 id;
933 ix = -1;
934 id = q_ctrl->id;
935 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
936 id &= V4L2_CTRL_ID_MASK;
937 id++;
938 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
939 if (gspca_dev->sd_desc->ctrls[i].qctrl.id < id)
940 continue;
941 if (ix < 0) {
942 ix = i;
943 continue;
945 if (gspca_dev->sd_desc->ctrls[i].qctrl.id
946 > gspca_dev->sd_desc->ctrls[ix].qctrl.id)
947 continue;
948 ix = i;
951 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
952 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
953 ix = i;
954 break;
957 if (ix < 0)
958 return -EINVAL;
959 memcpy(q_ctrl, &gspca_dev->sd_desc->ctrls[ix].qctrl,
960 sizeof *q_ctrl);
961 if (gspca_dev->ctrl_dis & (1 << ix))
962 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
963 return 0;
966 static int vidioc_s_ctrl(struct file *file, void *priv,
967 struct v4l2_control *ctrl)
969 struct gspca_dev *gspca_dev = priv;
970 const struct ctrl *ctrls;
971 int i, ret;
973 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
974 i < gspca_dev->sd_desc->nctrls;
975 i++, ctrls++) {
976 if (ctrl->id != ctrls->qctrl.id)
977 continue;
978 if (gspca_dev->ctrl_dis & (1 << i))
979 return -EINVAL;
980 if (ctrl->value < ctrls->qctrl.minimum
981 || ctrl->value > ctrls->qctrl.maximum)
982 return -ERANGE;
983 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
984 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
985 return -ERESTARTSYS;
986 ret = ctrls->set(gspca_dev, ctrl->value);
987 mutex_unlock(&gspca_dev->usb_lock);
988 return ret;
990 return -EINVAL;
993 static int vidioc_g_ctrl(struct file *file, void *priv,
994 struct v4l2_control *ctrl)
996 struct gspca_dev *gspca_dev = priv;
998 const struct ctrl *ctrls;
999 int i, ret;
1001 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1002 i < gspca_dev->sd_desc->nctrls;
1003 i++, ctrls++) {
1004 if (ctrl->id != ctrls->qctrl.id)
1005 continue;
1006 if (gspca_dev->ctrl_dis & (1 << i))
1007 return -EINVAL;
1008 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1009 return -ERESTARTSYS;
1010 ret = ctrls->get(gspca_dev, &ctrl->value);
1011 mutex_unlock(&gspca_dev->usb_lock);
1012 return ret;
1014 return -EINVAL;
1017 static int vidioc_querymenu(struct file *file, void *priv,
1018 struct v4l2_querymenu *qmenu)
1020 struct gspca_dev *gspca_dev = priv;
1022 if (!gspca_dev->sd_desc->querymenu)
1023 return -EINVAL;
1024 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1027 static int vidioc_enum_input(struct file *file, void *priv,
1028 struct v4l2_input *input)
1030 struct gspca_dev *gspca_dev = priv;
1032 if (input->index != 0)
1033 return -EINVAL;
1034 memset(input, 0, sizeof *input);
1035 input->type = V4L2_INPUT_TYPE_CAMERA;
1036 strncpy(input->name, gspca_dev->sd_desc->name,
1037 sizeof input->name);
1038 return 0;
1041 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1043 *i = 0;
1044 return 0;
1047 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1049 if (i > 0)
1050 return -EINVAL;
1051 return (0);
1054 static int vidioc_reqbufs(struct file *file, void *priv,
1055 struct v4l2_requestbuffers *rb)
1057 struct gspca_dev *gspca_dev = priv;
1058 int i, ret = 0;
1060 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1061 return -EINVAL;
1062 switch (rb->memory) {
1063 case GSPCA_MEMORY_READ: /* (internal call) */
1064 case V4L2_MEMORY_MMAP:
1065 case V4L2_MEMORY_USERPTR:
1066 break;
1067 default:
1068 return -EINVAL;
1070 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1071 return -ERESTARTSYS;
1073 if (gspca_dev->memory != GSPCA_MEMORY_NO
1074 && gspca_dev->memory != rb->memory) {
1075 ret = -EBUSY;
1076 goto out;
1079 /* only one file may do the capture */
1080 if (gspca_dev->capt_file != NULL
1081 && gspca_dev->capt_file != file) {
1082 ret = -EBUSY;
1083 goto out;
1086 /* if allocated, the buffers must not be mapped */
1087 for (i = 0; i < gspca_dev->nframes; i++) {
1088 if (gspca_dev->frame[i].vma_use_count) {
1089 ret = -EBUSY;
1090 goto out;
1094 /* stop streaming */
1095 if (gspca_dev->streaming) {
1096 mutex_lock(&gspca_dev->usb_lock);
1097 gspca_stream_off(gspca_dev);
1098 mutex_unlock(&gspca_dev->usb_lock);
1101 /* free the previous allocated buffers, if any */
1102 if (gspca_dev->nframes != 0) {
1103 frame_free(gspca_dev);
1104 gspca_dev->capt_file = NULL;
1106 if (rb->count == 0) /* unrequest */
1107 goto out;
1108 gspca_dev->memory = rb->memory;
1109 ret = frame_alloc(gspca_dev, rb->count);
1110 if (ret == 0) {
1111 rb->count = gspca_dev->nframes;
1112 gspca_dev->capt_file = file;
1114 out:
1115 mutex_unlock(&gspca_dev->queue_lock);
1116 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1117 return ret;
1120 static int vidioc_querybuf(struct file *file, void *priv,
1121 struct v4l2_buffer *v4l2_buf)
1123 struct gspca_dev *gspca_dev = priv;
1124 struct gspca_frame *frame;
1126 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1127 || v4l2_buf->index < 0
1128 || v4l2_buf->index >= gspca_dev->nframes)
1129 return -EINVAL;
1131 frame = &gspca_dev->frame[v4l2_buf->index];
1132 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1133 return 0;
1136 static int vidioc_streamon(struct file *file, void *priv,
1137 enum v4l2_buf_type buf_type)
1139 struct gspca_dev *gspca_dev = priv;
1140 int ret;
1142 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1143 return -EINVAL;
1144 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1145 return -ERESTARTSYS;
1146 if (!gspca_dev->present) {
1147 ret = -ENODEV;
1148 goto out;
1150 if (gspca_dev->nframes == 0) {
1151 ret = -EINVAL;
1152 goto out;
1154 if (!gspca_dev->streaming) {
1155 ret = gspca_init_transfer(gspca_dev);
1156 if (ret < 0)
1157 goto out;
1159 #ifdef GSPCA_DEBUG
1160 if (gspca_debug & D_STREAM) {
1161 PDEBUG_MODE("stream on OK",
1162 gspca_dev->pixfmt,
1163 gspca_dev->width,
1164 gspca_dev->height);
1166 #endif
1167 ret = 0;
1168 out:
1169 mutex_unlock(&gspca_dev->queue_lock);
1170 return ret;
1173 static int vidioc_streamoff(struct file *file, void *priv,
1174 enum v4l2_buf_type buf_type)
1176 struct gspca_dev *gspca_dev = priv;
1177 int i, ret;
1179 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1180 return -EINVAL;
1181 if (!gspca_dev->streaming)
1182 return 0;
1183 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1184 return -ERESTARTSYS;
1186 /* stop streaming */
1187 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1188 ret = -ERESTARTSYS;
1189 goto out;
1191 gspca_stream_off(gspca_dev);
1192 mutex_unlock(&gspca_dev->usb_lock);
1194 /* empty the application queues */
1195 for (i = 0; i < gspca_dev->nframes; i++)
1196 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1197 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
1198 gspca_dev->last_packet_type = DISCARD_PACKET;
1199 gspca_dev->sequence = 0;
1200 atomic_set(&gspca_dev->nevent, 0);
1201 ret = 0;
1202 out:
1203 mutex_unlock(&gspca_dev->queue_lock);
1204 return ret;
1207 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1208 struct v4l2_jpegcompression *jpegcomp)
1210 struct gspca_dev *gspca_dev = priv;
1211 int ret;
1213 if (!gspca_dev->sd_desc->get_jcomp)
1214 return -EINVAL;
1215 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1216 return -ERESTARTSYS;
1217 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1218 mutex_unlock(&gspca_dev->usb_lock);
1219 return ret;
1222 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1223 struct v4l2_jpegcompression *jpegcomp)
1225 struct gspca_dev *gspca_dev = priv;
1226 int ret;
1228 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1229 return -ERESTARTSYS;
1230 if (!gspca_dev->sd_desc->set_jcomp)
1231 return -EINVAL;
1232 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1233 mutex_unlock(&gspca_dev->usb_lock);
1234 return ret;
1237 static int vidioc_g_parm(struct file *filp, void *priv,
1238 struct v4l2_streamparm *parm)
1240 struct gspca_dev *gspca_dev = priv;
1242 memset(parm, 0, sizeof *parm);
1243 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1244 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1245 return 0;
1248 static int vidioc_s_parm(struct file *filp, void *priv,
1249 struct v4l2_streamparm *parm)
1251 struct gspca_dev *gspca_dev = priv;
1252 int n;
1254 n = parm->parm.capture.readbuffers;
1255 if (n == 0 || n > GSPCA_MAX_FRAMES)
1256 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1257 else
1258 gspca_dev->nbufread = n;
1259 return 0;
1262 static int vidioc_s_std(struct file *filp, void *priv,
1263 v4l2_std_id *parm)
1265 return 0;
1268 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1269 static int vidiocgmbuf(struct file *file, void *priv,
1270 struct video_mbuf *mbuf)
1272 struct gspca_dev *gspca_dev = file->private_data;
1273 int i;
1275 PDEBUG(D_STREAM, "cgmbuf");
1276 if (gspca_dev->nframes == 0) {
1277 int ret;
1280 struct v4l2_format fmt;
1282 memset(&fmt, 0, sizeof fmt);
1283 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1284 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1285 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1286 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1287 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1288 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1289 if (ret != 0)
1290 return ret;
1293 struct v4l2_requestbuffers rb;
1295 memset(&rb, 0, sizeof rb);
1296 rb.count = 4;
1297 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1298 rb.memory = V4L2_MEMORY_MMAP;
1299 ret = vidioc_reqbufs(file, priv, &rb);
1300 if (ret != 0)
1301 return ret;
1304 mbuf->frames = gspca_dev->nframes;
1305 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1306 for (i = 0; i < mbuf->frames; i++)
1307 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1308 return 0;
1310 #endif
1312 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1314 struct gspca_dev *gspca_dev = file->private_data;
1315 struct gspca_frame *frame;
1316 struct page *page;
1317 unsigned long addr, start, size;
1318 int i, ret;
1320 start = vma->vm_start;
1321 size = vma->vm_end - vma->vm_start;
1322 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1324 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1325 return -ERESTARTSYS;
1326 if (!gspca_dev->present) {
1327 ret = -ENODEV;
1328 goto out;
1330 if (gspca_dev->capt_file != file) {
1331 ret = -EINVAL;
1332 goto out;
1335 frame = NULL;
1336 for (i = 0; i < gspca_dev->nframes; ++i) {
1337 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1338 PDEBUG(D_STREAM, "mmap bad memory type");
1339 break;
1341 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1342 == vma->vm_pgoff) {
1343 frame = &gspca_dev->frame[i];
1344 break;
1347 if (frame == NULL) {
1348 PDEBUG(D_STREAM, "mmap no frame buffer found");
1349 ret = -EINVAL;
1350 goto out;
1352 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1353 /* v4l1 maps all the buffers */
1354 if (i != 0
1355 || size != frame->v4l2_buf.length * gspca_dev->nframes)
1356 #endif
1357 if (size != frame->v4l2_buf.length) {
1358 PDEBUG(D_STREAM, "mmap bad size");
1359 ret = -EINVAL;
1360 goto out;
1364 * - VM_IO marks the area as being a mmaped region for I/O to a
1365 * device. It also prevents the region from being core dumped.
1367 vma->vm_flags |= VM_IO;
1369 addr = (unsigned long) frame->data;
1370 while (size > 0) {
1371 page = vmalloc_to_page((void *) addr);
1372 ret = vm_insert_page(vma, start, page);
1373 if (ret < 0)
1374 goto out;
1375 start += PAGE_SIZE;
1376 addr += PAGE_SIZE;
1377 size -= PAGE_SIZE;
1380 vma->vm_ops = &gspca_vm_ops;
1381 vma->vm_private_data = frame;
1382 gspca_vm_open(vma);
1383 ret = 0;
1384 out:
1385 mutex_unlock(&gspca_dev->queue_lock);
1386 return ret;
1390 * wait for a video frame
1392 * If a frame is ready, its index is returned.
1394 static int frame_wait(struct gspca_dev *gspca_dev,
1395 int nonblock_ing)
1397 struct gspca_frame *frame;
1398 int i, j, ret;
1400 /* check if a frame is ready */
1401 i = gspca_dev->fr_o;
1402 j = gspca_dev->fr_queue[i];
1403 frame = &gspca_dev->frame[j];
1404 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) {
1405 atomic_dec(&gspca_dev->nevent);
1406 goto ok;
1408 if (nonblock_ing) /* no frame yet */
1409 return -EAGAIN;
1411 /* wait till a frame is ready */
1412 for (;;) {
1413 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1414 atomic_read(&gspca_dev->nevent) > 0,
1415 msecs_to_jiffies(3000));
1416 if (ret <= 0) {
1417 if (ret < 0)
1418 return ret; /* interrupt */
1419 return -EIO; /* timeout */
1421 atomic_dec(&gspca_dev->nevent);
1422 if (!gspca_dev->streaming || !gspca_dev->present)
1423 return -EIO;
1424 i = gspca_dev->fr_o;
1425 j = gspca_dev->fr_queue[i];
1426 frame = &gspca_dev->frame[j];
1427 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1428 break;
1431 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1432 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1433 gspca_dev->fr_q,
1434 gspca_dev->fr_i,
1435 gspca_dev->fr_o);
1437 if (gspca_dev->sd_desc->dq_callback) {
1438 mutex_lock(&gspca_dev->usb_lock);
1439 gspca_dev->sd_desc->dq_callback(gspca_dev);
1440 mutex_unlock(&gspca_dev->usb_lock);
1442 return j;
1446 * dequeue a video buffer
1448 * If nonblock_ing is false, block until a buffer is available.
1450 static int vidioc_dqbuf(struct file *file, void *priv,
1451 struct v4l2_buffer *v4l2_buf)
1453 struct gspca_dev *gspca_dev = priv;
1454 struct gspca_frame *frame;
1455 int i, ret;
1457 PDEBUG(D_FRAM, "dqbuf");
1458 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1459 return -EINVAL;
1460 if (v4l2_buf->memory != gspca_dev->memory)
1461 return -EINVAL;
1463 /* if not streaming, be sure the application will not loop forever */
1464 if (!(file->f_flags & O_NONBLOCK)
1465 && !gspca_dev->streaming && gspca_dev->users == 1)
1466 return -EINVAL;
1468 /* only the capturing file may dequeue */
1469 if (gspca_dev->capt_file != file)
1470 return -EINVAL;
1472 /* only one dequeue / read at a time */
1473 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1474 return -ERESTARTSYS;
1476 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1477 if (ret < 0)
1478 goto out;
1479 i = ret; /* frame index */
1480 frame = &gspca_dev->frame[i];
1481 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1482 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1483 frame->data,
1484 frame->v4l2_buf.bytesused)) {
1485 PDEBUG(D_ERR|D_STREAM,
1486 "dqbuf cp to user failed");
1487 ret = -EFAULT;
1488 goto out;
1491 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1492 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1493 PDEBUG(D_FRAM, "dqbuf %d", i);
1494 ret = 0;
1495 out:
1496 mutex_unlock(&gspca_dev->read_lock);
1497 return ret;
1501 * queue a video buffer
1503 * Attempting to queue a buffer that has already been
1504 * queued will return -EINVAL.
1506 static int vidioc_qbuf(struct file *file, void *priv,
1507 struct v4l2_buffer *v4l2_buf)
1509 struct gspca_dev *gspca_dev = priv;
1510 struct gspca_frame *frame;
1511 int i, index, ret;
1513 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1514 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1515 return -EINVAL;
1517 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1518 return -ERESTARTSYS;
1520 index = v4l2_buf->index;
1521 if ((unsigned) index >= gspca_dev->nframes) {
1522 PDEBUG(D_FRAM,
1523 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1524 ret = -EINVAL;
1525 goto out;
1527 if (v4l2_buf->memory != gspca_dev->memory) {
1528 PDEBUG(D_FRAM, "qbuf bad memory type");
1529 ret = -EINVAL;
1530 goto out;
1533 frame = &gspca_dev->frame[index];
1534 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1535 PDEBUG(D_FRAM, "qbuf bad state");
1536 ret = -EINVAL;
1537 goto out;
1540 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1541 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1543 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1544 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1545 frame->v4l2_buf.length = v4l2_buf->length;
1548 /* put the buffer in the 'queued' queue */
1549 i = gspca_dev->fr_q;
1550 gspca_dev->fr_queue[i] = index;
1551 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1552 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1553 gspca_dev->fr_q,
1554 gspca_dev->fr_i,
1555 gspca_dev->fr_o);
1557 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1558 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1559 ret = 0;
1560 out:
1561 mutex_unlock(&gspca_dev->queue_lock);
1562 return ret;
1566 * allocate the resources for read()
1568 static int read_alloc(struct gspca_dev *gspca_dev,
1569 struct file *file)
1571 struct v4l2_buffer v4l2_buf;
1572 int i, ret;
1574 PDEBUG(D_STREAM, "read alloc");
1575 if (gspca_dev->nframes == 0) {
1576 struct v4l2_requestbuffers rb;
1578 memset(&rb, 0, sizeof rb);
1579 rb.count = gspca_dev->nbufread;
1580 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1581 rb.memory = GSPCA_MEMORY_READ;
1582 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1583 if (ret != 0) {
1584 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1585 return ret;
1587 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1588 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1589 v4l2_buf.memory = GSPCA_MEMORY_READ;
1590 for (i = 0; i < gspca_dev->nbufread; i++) {
1591 v4l2_buf.index = i;
1592 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1593 if (ret != 0) {
1594 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1595 return ret;
1598 gspca_dev->memory = GSPCA_MEMORY_READ;
1601 /* start streaming */
1602 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1603 if (ret != 0)
1604 PDEBUG(D_STREAM, "read streamon err %d", ret);
1605 return ret;
1608 static unsigned int dev_poll(struct file *file, poll_table *wait)
1610 struct gspca_dev *gspca_dev = file->private_data;
1611 int i, ret;
1613 PDEBUG(D_FRAM, "poll");
1615 poll_wait(file, &gspca_dev->wq, wait);
1616 if (!gspca_dev->present)
1617 return POLLERR;
1619 /* if reqbufs is not done, the user would use read() */
1620 if (gspca_dev->nframes == 0) {
1621 if (gspca_dev->memory != GSPCA_MEMORY_NO)
1622 return POLLERR; /* not the 1st time */
1623 ret = read_alloc(gspca_dev, file);
1624 if (ret != 0)
1625 return POLLERR;
1628 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1629 return POLLERR;
1630 if (!gspca_dev->present) {
1631 ret = POLLERR;
1632 goto out;
1635 /* check the next incoming buffer */
1636 i = gspca_dev->fr_o;
1637 i = gspca_dev->fr_queue[i];
1638 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1639 ret = POLLIN | POLLRDNORM; /* something to read */
1640 else
1641 ret = 0;
1642 out:
1643 mutex_unlock(&gspca_dev->queue_lock);
1644 return ret;
1647 static ssize_t dev_read(struct file *file, char __user *data,
1648 size_t count, loff_t *ppos)
1650 struct gspca_dev *gspca_dev = file->private_data;
1651 struct gspca_frame *frame;
1652 struct v4l2_buffer v4l2_buf;
1653 struct timeval timestamp;
1654 int n, ret, ret2;
1656 PDEBUG(D_FRAM, "read (%zd)", count);
1657 if (!gspca_dev->present)
1658 return -ENODEV;
1659 switch (gspca_dev->memory) {
1660 case GSPCA_MEMORY_NO: /* first time */
1661 ret = read_alloc(gspca_dev, file);
1662 if (ret != 0)
1663 return ret;
1664 break;
1665 case GSPCA_MEMORY_READ:
1666 if (gspca_dev->capt_file == file)
1667 break;
1668 /* fall thru */
1669 default:
1670 return -EINVAL;
1673 /* get a frame */
1674 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1675 timestamp.tv_sec--;
1676 n = 2;
1677 for (;;) {
1678 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1679 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1680 v4l2_buf.memory = GSPCA_MEMORY_READ;
1681 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1682 if (ret != 0) {
1683 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1684 return ret;
1687 /* if the process slept for more than 1 second,
1688 * get anewer frame */
1689 frame = &gspca_dev->frame[v4l2_buf.index];
1690 if (--n < 0)
1691 break; /* avoid infinite loop */
1692 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1693 break;
1694 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1695 if (ret != 0) {
1696 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1697 return ret;
1701 /* copy the frame */
1702 if (count > frame->v4l2_buf.bytesused)
1703 count = frame->v4l2_buf.bytesused;
1704 ret = copy_to_user(data, frame->data, count);
1705 if (ret != 0) {
1706 PDEBUG(D_ERR|D_STREAM,
1707 "read cp to user lack %d / %zd", ret, count);
1708 ret = -EFAULT;
1709 goto out;
1711 ret = count;
1712 out:
1713 /* in each case, requeue the buffer */
1714 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1715 if (ret2 != 0)
1716 return ret2;
1717 return ret;
1720 static void dev_release(struct video_device *vfd)
1722 /* nothing */
1725 static struct file_operations dev_fops = {
1726 .owner = THIS_MODULE,
1727 .open = dev_open,
1728 .release = dev_close,
1729 .read = dev_read,
1730 .mmap = dev_mmap,
1731 .ioctl = video_ioctl2,
1732 #ifdef CONFIG_COMPAT
1733 .compat_ioctl = v4l_compat_ioctl32,
1734 #endif
1735 .llseek = no_llseek,
1736 .poll = dev_poll,
1739 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1740 .vidioc_querycap = vidioc_querycap,
1741 .vidioc_dqbuf = vidioc_dqbuf,
1742 .vidioc_qbuf = vidioc_qbuf,
1743 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1744 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1745 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1746 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1747 .vidioc_streamon = vidioc_streamon,
1748 .vidioc_queryctrl = vidioc_queryctrl,
1749 .vidioc_g_ctrl = vidioc_g_ctrl,
1750 .vidioc_s_ctrl = vidioc_s_ctrl,
1751 .vidioc_querymenu = vidioc_querymenu,
1752 .vidioc_enum_input = vidioc_enum_input,
1753 .vidioc_g_input = vidioc_g_input,
1754 .vidioc_s_input = vidioc_s_input,
1755 .vidioc_reqbufs = vidioc_reqbufs,
1756 .vidioc_querybuf = vidioc_querybuf,
1757 .vidioc_streamoff = vidioc_streamoff,
1758 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1759 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1760 .vidioc_g_parm = vidioc_g_parm,
1761 .vidioc_s_parm = vidioc_s_parm,
1762 .vidioc_s_std = vidioc_s_std,
1763 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1764 .vidiocgmbuf = vidiocgmbuf,
1765 #endif
1768 static struct video_device gspca_template = {
1769 .name = "gspca main driver",
1770 .fops = &dev_fops,
1771 .ioctl_ops = &dev_ioctl_ops,
1772 .release = dev_release, /* mandatory */
1773 .minor = -1,
1777 * probe and create a new gspca device
1779 * This function must be called by the sub-driver when it is
1780 * called for probing a new device.
1782 int gspca_dev_probe(struct usb_interface *intf,
1783 const struct usb_device_id *id,
1784 const struct sd_desc *sd_desc,
1785 int dev_size,
1786 struct module *module)
1788 struct usb_interface_descriptor *interface;
1789 struct gspca_dev *gspca_dev;
1790 struct usb_device *dev = interface_to_usbdev(intf);
1791 int ret;
1793 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1795 /* we don't handle multi-config cameras */
1796 if (dev->descriptor.bNumConfigurations != 1)
1797 return -ENODEV;
1798 interface = &intf->cur_altsetting->desc;
1799 if (interface->bInterfaceNumber > 0)
1800 return -ENODEV;
1802 /* create the device */
1803 if (dev_size < sizeof *gspca_dev)
1804 dev_size = sizeof *gspca_dev;
1805 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1806 if (gspca_dev == NULL) {
1807 err("couldn't kzalloc gspca struct");
1808 return -EIO;
1810 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1811 if (!gspca_dev->usb_buf) {
1812 err("out of memory");
1813 ret = -EIO;
1814 goto out;
1816 gspca_dev->dev = dev;
1817 gspca_dev->iface = interface->bInterfaceNumber;
1818 gspca_dev->nbalt = intf->num_altsetting;
1819 gspca_dev->sd_desc = sd_desc;
1820 /* gspca_dev->users = 0; (done by kzalloc) */
1821 gspca_dev->nbufread = 2;
1823 /* configure the subdriver and initialize the USB device */
1824 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1825 if (ret < 0)
1826 goto out;
1827 ret = gspca_dev->sd_desc->init(gspca_dev);
1828 if (ret < 0)
1829 goto out;
1830 ret = gspca_set_alt0(gspca_dev);
1831 if (ret < 0)
1832 goto out;
1833 gspca_set_default_mode(gspca_dev);
1835 mutex_init(&gspca_dev->usb_lock);
1836 mutex_init(&gspca_dev->read_lock);
1837 mutex_init(&gspca_dev->queue_lock);
1838 init_waitqueue_head(&gspca_dev->wq);
1840 /* init video stuff */
1841 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1842 gspca_dev->vdev.parent = &dev->dev;
1843 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1844 gspca_dev->vdev.fops = &gspca_dev->fops;
1845 gspca_dev->fops.owner = module; /* module protection */
1846 gspca_dev->present = 1;
1847 ret = video_register_device(&gspca_dev->vdev,
1848 VFL_TYPE_GRABBER,
1849 video_nr);
1850 if (ret < 0) {
1851 err("video_register_device err %d", ret);
1852 goto out;
1855 usb_set_intfdata(intf, gspca_dev);
1856 PDEBUG(D_PROBE, "probe ok");
1857 return 0;
1858 out:
1859 kfree(gspca_dev->usb_buf);
1860 kfree(gspca_dev);
1861 return ret;
1863 EXPORT_SYMBOL(gspca_dev_probe);
1866 * USB disconnection
1868 * This function must be called by the sub-driver
1869 * when the device disconnects, after the specific resources are freed.
1871 void gspca_disconnect(struct usb_interface *intf)
1873 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1875 if (!gspca_dev)
1876 return;
1877 gspca_dev->present = 0;
1878 mutex_lock(&gspca_dev->queue_lock);
1879 mutex_lock(&gspca_dev->usb_lock);
1880 gspca_dev->streaming = 0;
1881 destroy_urbs(gspca_dev);
1882 mutex_unlock(&gspca_dev->usb_lock);
1883 mutex_unlock(&gspca_dev->queue_lock);
1884 while (gspca_dev->users != 0) { /* wait until fully closed */
1885 atomic_inc(&gspca_dev->nevent);
1886 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1887 schedule();
1889 /* We don't want people trying to open up the device */
1890 video_unregister_device(&gspca_dev->vdev);
1891 /* Free the memory */
1892 kfree(gspca_dev->usb_buf);
1893 kfree(gspca_dev);
1894 PDEBUG(D_PROBE, "disconnect complete");
1896 EXPORT_SYMBOL(gspca_disconnect);
1898 #ifdef CONFIG_PM
1899 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1901 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1903 if (!gspca_dev->streaming)
1904 return 0;
1905 gspca_dev->frozen = 1; /* avoid urb error messages */
1906 if (gspca_dev->sd_desc->stopN)
1907 gspca_dev->sd_desc->stopN(gspca_dev);
1908 destroy_urbs(gspca_dev);
1909 gspca_set_alt0(gspca_dev);
1910 if (gspca_dev->sd_desc->stop0)
1911 gspca_dev->sd_desc->stop0(gspca_dev);
1912 return 0;
1914 EXPORT_SYMBOL(gspca_suspend);
1916 int gspca_resume(struct usb_interface *intf)
1918 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1920 gspca_dev->frozen = 0;
1921 gspca_dev->sd_desc->init(gspca_dev);
1922 if (gspca_dev->streaming)
1923 return gspca_init_transfer(gspca_dev);
1924 return 0;
1926 EXPORT_SYMBOL(gspca_resume);
1927 #endif
1928 /* -- cam driver utility functions -- */
1930 /* auto gain and exposure algorithm based on the knee algorithm described here:
1931 http://ytse.tricolour.net/docs/LowLightOptimization.html
1933 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1934 where changed. */
1935 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
1936 int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee)
1938 int i, steps, gain, orig_gain, exposure, orig_exposure, autogain;
1939 const struct ctrl *gain_ctrl = NULL;
1940 const struct ctrl *exposure_ctrl = NULL;
1941 const struct ctrl *autogain_ctrl = NULL;
1942 int retval = 0;
1944 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1945 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_GAIN)
1946 gain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1947 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_EXPOSURE)
1948 exposure_ctrl = &gspca_dev->sd_desc->ctrls[i];
1949 if (gspca_dev->sd_desc->ctrls[i].qctrl.id == V4L2_CID_AUTOGAIN)
1950 autogain_ctrl = &gspca_dev->sd_desc->ctrls[i];
1952 if (!gain_ctrl || !exposure_ctrl || !autogain_ctrl) {
1953 PDEBUG(D_ERR, "Error: gspca_auto_gain_n_exposure called "
1954 "on cam without (auto)gain/exposure");
1955 return 0;
1958 if (gain_ctrl->get(gspca_dev, &gain) ||
1959 exposure_ctrl->get(gspca_dev, &exposure) ||
1960 autogain_ctrl->get(gspca_dev, &autogain) || !autogain)
1961 return 0;
1963 orig_gain = gain;
1964 orig_exposure = exposure;
1966 /* If we are of a multiple of deadzone, do multiple steps to reach the
1967 desired lumination fast (with the risc of a slight overshoot) */
1968 steps = abs(desired_avg_lum - avg_lum) / deadzone;
1970 PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
1971 avg_lum, desired_avg_lum, steps);
1973 for (i = 0; i < steps; i++) {
1974 if (avg_lum > desired_avg_lum) {
1975 if (gain > gain_knee)
1976 gain--;
1977 else if (exposure > exposure_knee)
1978 exposure--;
1979 else if (gain > gain_ctrl->qctrl.default_value)
1980 gain--;
1981 else if (exposure > exposure_ctrl->qctrl.minimum)
1982 exposure--;
1983 else if (gain > gain_ctrl->qctrl.minimum)
1984 gain--;
1985 else
1986 break;
1987 } else {
1988 if (gain < gain_ctrl->qctrl.default_value)
1989 gain++;
1990 else if (exposure < exposure_knee)
1991 exposure++;
1992 else if (gain < gain_knee)
1993 gain++;
1994 else if (exposure < exposure_ctrl->qctrl.maximum)
1995 exposure++;
1996 else if (gain < gain_ctrl->qctrl.maximum)
1997 gain++;
1998 else
1999 break;
2003 if (gain != orig_gain) {
2004 gain_ctrl->set(gspca_dev, gain);
2005 retval = 1;
2007 if (exposure != orig_exposure) {
2008 exposure_ctrl->set(gspca_dev, exposure);
2009 retval = 1;
2012 return retval;
2014 EXPORT_SYMBOL(gspca_auto_gain_n_exposure);
2016 /* -- module insert / remove -- */
2017 static int __init gspca_init(void)
2019 info("main v%d.%d.%d registered",
2020 (DRIVER_VERSION_NUMBER >> 16) & 0xff,
2021 (DRIVER_VERSION_NUMBER >> 8) & 0xff,
2022 DRIVER_VERSION_NUMBER & 0xff);
2023 return 0;
2025 static void __exit gspca_exit(void)
2027 info("main deregistered");
2030 module_init(gspca_init);
2031 module_exit(gspca_exit);
2033 #ifdef GSPCA_DEBUG
2034 module_param_named(debug, gspca_debug, int, 0644);
2035 MODULE_PARM_DESC(debug,
2036 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2037 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2038 " 0x0100: v4l2");
2039 #endif