V4L/DVB (8201): gspca: v4l2_pix_format in each subdriver.
[linux-2.6/kvm.git] / drivers / media / video / gspca / gspca.c
blobcb0aeb0c0a45476b92e435c2c20788146089fc08
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>
36 #include "gspca.h"
38 /* global values */
39 #define DEF_NURBS 2 /* default number of URBs (mmap) */
40 #define USR_NURBS 5 /* default number of URBs (userptr) */
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, 1, 5)
47 static const char version[] = "2.1.5";
49 static int video_nr = -1;
51 #ifdef CONFIG_VIDEO_ADV_DEBUG
52 int gspca_debug = D_ERR | D_PROBE;
53 EXPORT_SYMBOL(gspca_debug);
55 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
57 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
58 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
59 txt,
60 pixfmt & 0xff,
61 (pixfmt >> 8) & 0xff,
62 (pixfmt >> 16) & 0xff,
63 pixfmt >> 24,
64 w, h);
65 } else {
66 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
67 txt,
68 pixfmt,
69 w, h);
72 #else
73 #define PDEBUG_MODE(txt, pixfmt, w, h)
74 #endif
76 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
77 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
78 #define GSPCA_MEMORY_READ 7
80 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
83 * VMA operations.
85 static void gspca_vm_open(struct vm_area_struct *vma)
87 struct gspca_frame *frame = vma->vm_private_data;
89 frame->vma_use_count++;
90 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
93 static void gspca_vm_close(struct vm_area_struct *vma)
95 struct gspca_frame *frame = vma->vm_private_data;
97 if (--frame->vma_use_count <= 0)
98 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
101 static struct vm_operations_struct gspca_vm_ops = {
102 .open = gspca_vm_open,
103 .close = gspca_vm_close,
107 * fill a video frame from an URB and resubmit
109 static void fill_frame(struct gspca_dev *gspca_dev,
110 struct urb *urb)
112 struct gspca_frame *frame;
113 __u8 *data; /* address of data in the iso message */
114 int i, j, len, st;
115 cam_pkt_op pkt_scan;
117 if (urb->status != 0) {
118 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
119 return; /* disconnection ? */
121 pkt_scan = gspca_dev->sd_desc->pkt_scan;
122 for (i = 0; i < urb->number_of_packets; i++) {
124 /* check the availability of the frame buffer */
125 j = gspca_dev->fr_i;
126 j = gspca_dev->fr_queue[j];
127 frame = &gspca_dev->frame[j];
128 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
129 != V4L2_BUF_FLAG_QUEUED) {
130 gspca_dev->last_packet_type = DISCARD_PACKET;
131 break;
134 /* check the packet status and length */
135 len = urb->iso_frame_desc[i].actual_length;
136 if (len == 0)
137 continue;
138 st = urb->iso_frame_desc[i].status;
139 if (st) {
140 PDEBUG(D_ERR,
141 "ISOC data error: [%d] len=%d, status=%d",
142 i, len, st);
143 gspca_dev->last_packet_type = DISCARD_PACKET;
144 continue;
147 /* let the packet be analyzed by the subdriver */
148 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
149 i, urb->iso_frame_desc[i].offset, len);
150 data = (__u8 *) urb->transfer_buffer
151 + urb->iso_frame_desc[i].offset;
152 pkt_scan(gspca_dev, frame, data, len);
155 /* resubmit the URB */
156 /*fixme: don't do that when userptr and too many URBs sent*/
157 urb->status = 0;
158 st = usb_submit_urb(urb, GFP_ATOMIC);
159 if (st < 0)
160 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
164 * ISOC message interrupt from the USB device
166 * Analyse each packet and call the subdriver for copy
167 * to the frame buffer.
169 * There are 2 functions:
170 * - the first one (isoc_irq_mmap) is used when the application
171 * buffers are mapped. The frame detection and copy is done
172 * at interrupt level.
173 * - the second one (isoc_irq_user) is used when the application
174 * buffers are in user space (userptr). The frame detection
175 * and copy is done by the application.
177 static void isoc_irq_mmap(struct urb *urb
180 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
182 PDEBUG(D_PACK, "isoc irq mmap");
183 if (!gspca_dev->streaming)
184 return;
185 fill_frame(gspca_dev, urb);
188 static void isoc_irq_user(struct urb *urb
191 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
192 int i;
194 PDEBUG(D_PACK, "isoc irq user");
195 if (!gspca_dev->streaming)
196 return;
198 i = gspca_dev->urb_in % gspca_dev->nurbs;
199 if (urb != gspca_dev->urb[i]) {
200 PDEBUG(D_ERR|D_PACK, "urb out of sequence");
201 return; /* should never occur */
204 gspca_dev->urb_in++;
205 atomic_inc(&gspca_dev->nevent); /* new event */
206 wake_up_interruptible(&gspca_dev->wq);
207 /*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
211 * treat the isoc messages
213 * This routine is called by the application (case userptr).
215 static void isoc_transfer(struct gspca_dev *gspca_dev)
217 struct urb *urb;
218 int i;
220 for (;;) {
221 i = gspca_dev->urb_out;
222 PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
223 if (i == gspca_dev->urb_in) /* isoc message to read */
224 break; /* no (more) message */
225 atomic_dec(&gspca_dev->nevent);
226 /*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
227 gspca_dev->urb_out = i + 1; /* message treated */
228 urb = gspca_dev->urb[i % gspca_dev->nurbs];
229 fill_frame(gspca_dev, urb);
234 * add data to the current frame
236 * This function is called by the subdrivers at interrupt level
237 * or user level.
238 * To build a frame, these ones must add
239 * - one FIRST_PACKET
240 * - 0 or many INTER_PACKETs
241 * - one LAST_PACKET
242 * DISCARD_PACKET invalidates the whole frame.
243 * On LAST_PACKET, a new frame is returned.
245 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
246 int packet_type,
247 struct gspca_frame *frame,
248 const __u8 *data,
249 int len)
251 int i, j;
253 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
255 /* when start of a new frame, if the current frame buffer
256 * is not queued, discard the whole frame */
257 if (packet_type == FIRST_PACKET) {
258 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
259 != V4L2_BUF_FLAG_QUEUED) {
260 gspca_dev->last_packet_type = DISCARD_PACKET;
261 return frame;
263 frame->data_end = frame->data;
264 jiffies_to_timeval(get_jiffies_64(),
265 &frame->v4l2_buf.timestamp);
266 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
267 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
268 return frame;
271 /* append the packet to the frame buffer */
272 if (len > 0) {
273 if (frame->data_end - frame->data + len
274 > frame->v4l2_buf.length) {
275 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
276 frame->data_end - frame->data + len,
277 frame->v4l2_buf.length);
278 packet_type = DISCARD_PACKET;
279 } else {
280 if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR) {
281 memcpy(frame->data_end, data, len);
282 } else {
283 if (copy_to_user(frame->data_end,
284 data, len) != 0) {
285 PDEBUG(D_ERR|D_PACK,
286 "copy to user failed");
287 packet_type = DISCARD_PACKET;
290 frame->data_end += len;
293 gspca_dev->last_packet_type = packet_type;
295 /* if last packet, wake the application and advance in the queue */
296 if (packet_type == LAST_PACKET) {
297 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
298 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
299 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
300 atomic_inc(&gspca_dev->nevent);
301 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
302 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
303 gspca_dev->fr_i = i;
304 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
305 frame->v4l2_buf.bytesused,
306 gspca_dev->fr_q,
308 gspca_dev->fr_o);
309 j = gspca_dev->fr_queue[i];
310 frame = &gspca_dev->frame[j];
312 return frame;
314 EXPORT_SYMBOL(gspca_frame_add);
316 static int gspca_is_compressed(__u32 format)
318 switch (format) {
319 case V4L2_PIX_FMT_MJPEG:
320 case V4L2_PIX_FMT_JPEG:
321 case V4L2_PIX_FMT_SPCA561:
322 case V4L2_PIX_FMT_PAC207:
323 return 1;
325 return 0;
328 static void *rvmalloc(unsigned long size)
330 void *mem;
331 unsigned long adr;
333 /* size = PAGE_ALIGN(size); (already done) */
334 mem = vmalloc_32(size);
335 if (mem != NULL) {
336 memset(mem, 0, size);
337 adr = (unsigned long) mem;
338 while ((long) size > 0) {
339 SetPageReserved(vmalloc_to_page((void *) adr));
340 adr += PAGE_SIZE;
341 size -= PAGE_SIZE;
344 return mem;
347 static void rvfree(void *mem, unsigned long size)
349 unsigned long adr;
351 if (!mem)
352 return;
353 adr = (unsigned long) mem;
354 while ((long) size > 0) {
355 ClearPageReserved(vmalloc_to_page((void *) adr));
356 adr += PAGE_SIZE;
357 size -= PAGE_SIZE;
359 vfree(mem);
362 static int frame_alloc(struct gspca_dev *gspca_dev,
363 unsigned int count)
365 struct gspca_frame *frame;
366 unsigned int frsz;
367 int i;
369 i = gspca_dev->curr_mode;
370 frsz = gspca_dev->cam.cam_mode[i].sizeimage;
371 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
372 frsz = PAGE_ALIGN(frsz);
373 PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
374 gspca_dev->frsz = frsz;
375 if (count > GSPCA_MAX_FRAMES)
376 count = GSPCA_MAX_FRAMES;
377 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
378 gspca_dev->frbuf = rvmalloc(frsz * count);
379 if (!gspca_dev->frbuf) {
380 err("frame alloc failed");
381 return -ENOMEM;
384 gspca_dev->nframes = count;
385 for (i = 0; i < count; i++) {
386 frame = &gspca_dev->frame[i];
387 frame->v4l2_buf.index = i;
388 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
389 frame->v4l2_buf.flags = 0;
390 frame->v4l2_buf.field = V4L2_FIELD_NONE;
391 frame->v4l2_buf.length = frsz;
392 frame->v4l2_buf.memory = gspca_dev->memory;
393 frame->v4l2_buf.sequence = 0;
394 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
395 frame->data = frame->data_end =
396 gspca_dev->frbuf + i * frsz;
397 frame->v4l2_buf.m.offset = i * frsz;
400 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
401 gspca_dev->last_packet_type = DISCARD_PACKET;
402 gspca_dev->sequence = 0;
403 atomic_set(&gspca_dev->nevent, 0);
404 return 0;
407 static void frame_free(struct gspca_dev *gspca_dev)
409 int i;
411 PDEBUG(D_STREAM, "frame free");
412 if (gspca_dev->frbuf != NULL) {
413 rvfree(gspca_dev->frbuf,
414 gspca_dev->nframes * gspca_dev->frsz);
415 gspca_dev->frbuf = NULL;
416 for (i = 0; i < gspca_dev->nframes; i++)
417 gspca_dev->frame[i].data = NULL;
419 gspca_dev->nframes = 0;
422 static void destroy_urbs(struct gspca_dev *gspca_dev)
424 struct urb *urb;
425 unsigned int i;
427 PDEBUG(D_STREAM, "kill transfer");
428 for (i = 0; i < MAX_NURBS; ++i) {
429 urb = gspca_dev->urb[i];
430 if (urb == NULL)
431 break;
433 gspca_dev->urb[i] = NULL;
434 usb_kill_urb(urb);
435 if (urb->transfer_buffer != NULL)
436 usb_buffer_free(gspca_dev->dev,
437 urb->transfer_buffer_length,
438 urb->transfer_buffer,
439 urb->transfer_dma);
440 usb_free_urb(urb);
445 * search an input isochronous endpoint in an alternate setting
447 static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
448 __u8 epaddr)
450 struct usb_host_endpoint *ep;
451 int i, attr;
453 epaddr |= USB_DIR_IN;
454 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
455 ep = &alt->endpoint[i];
456 if (ep->desc.bEndpointAddress == epaddr) {
457 attr = ep->desc.bmAttributes
458 & USB_ENDPOINT_XFERTYPE_MASK;
459 if (attr == USB_ENDPOINT_XFER_ISOC)
460 return ep;
461 break;
464 return NULL;
468 * search an input isochronous endpoint
470 * The endpoint is defined by the subdriver.
471 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
472 * This routine may be called many times when the bandwidth is too small
473 * (the bandwidth is checked on urb submit).
475 struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
477 struct usb_interface *intf;
478 struct usb_host_endpoint *ep;
479 int i, ret;
481 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
482 ep = NULL;
483 i = gspca_dev->alt; /* previous alt setting */
484 while (--i > 0) { /* alt 0 is unusable */
485 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
486 if (ep)
487 break;
489 if (ep == NULL) {
490 err("no ISOC endpoint found");
491 return NULL;
493 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
494 i, ep->desc.bEndpointAddress);
495 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
496 if (ret < 0) {
497 err("set interface err %d", ret);
498 return NULL;
500 gspca_dev->alt = i; /* memorize the current alt setting */
501 return ep;
505 * create the isochronous URBs
507 static int create_urbs(struct gspca_dev *gspca_dev,
508 struct usb_host_endpoint *ep)
510 struct urb *urb;
511 int n, nurbs, i, psize, npkt, bsize;
512 usb_complete_t usb_complete;
514 /* calculate the packet size and the number of packets */
515 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
517 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
518 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
519 npkt = ISO_MAX_SIZE / psize;
520 if (npkt > ISO_MAX_PKT)
521 npkt = ISO_MAX_PKT;
522 bsize = psize * npkt;
523 PDEBUG(D_STREAM,
524 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
525 /*fixme:don't submit all URBs when userptr*/
526 if (gspca_dev->memory != V4L2_MEMORY_USERPTR) {
527 usb_complete = isoc_irq_mmap;
528 nurbs = DEF_NURBS;
529 } else {
530 usb_complete = isoc_irq_user;
531 nurbs = USR_NURBS;
533 gspca_dev->nurbs = nurbs;
534 for (n = 0; n < nurbs; n++) {
535 urb = usb_alloc_urb(npkt, GFP_KERNEL);
536 if (!urb) {
537 err("usb_alloc_urb failed");
538 return -ENOMEM;
540 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
541 bsize,
542 GFP_KERNEL,
543 &urb->transfer_dma);
545 if (urb->transfer_buffer == NULL) {
546 usb_free_urb(urb);
547 destroy_urbs(gspca_dev);
548 err("usb_buffer_urb failed");
549 return -ENOMEM;
551 gspca_dev->urb[n] = urb;
552 urb->dev = gspca_dev->dev;
553 urb->context = gspca_dev;
554 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
555 ep->desc.bEndpointAddress);
556 urb->transfer_flags = URB_ISO_ASAP
557 | URB_NO_TRANSFER_DMA_MAP;
558 urb->interval = ep->desc.bInterval;
559 urb->complete = usb_complete;
560 urb->number_of_packets = npkt;
561 urb->transfer_buffer_length = bsize;
562 for (i = 0; i < npkt; i++) {
563 urb->iso_frame_desc[i].length = psize;
564 urb->iso_frame_desc[i].offset = psize * i;
567 gspca_dev->urb_in = gspca_dev->urb_out = 0;
568 return 0;
572 * start the USB transfer
574 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
576 struct usb_host_endpoint *ep;
577 int n, ret;
579 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
580 return -ERESTARTSYS;
582 /* set the higher alternate setting and
583 * loop until urb submit succeeds */
584 gspca_dev->alt = gspca_dev->nbalt;
585 for (;;) {
586 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
587 ep = get_isoc_ep(gspca_dev);
588 if (ep == NULL) {
589 ret = -EIO;
590 goto out;
592 ret = create_urbs(gspca_dev, ep);
593 if (ret < 0)
594 goto out;
596 /* start the cam */
597 gspca_dev->sd_desc->start(gspca_dev);
598 gspca_dev->streaming = 1;
599 atomic_set(&gspca_dev->nevent, 0);
601 /* submit the URBs */
602 for (n = 0; n < gspca_dev->nurbs; n++) {
603 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
604 if (ret < 0) {
605 PDEBUG(D_ERR|D_STREAM,
606 "usb_submit_urb [%d] err %d", n, ret);
607 gspca_dev->streaming = 0;
608 destroy_urbs(gspca_dev);
609 if (ret == -ENOSPC)
610 break; /* try the previous alt */
611 goto out;
614 if (ret >= 0)
615 break;
617 out:
618 mutex_unlock(&gspca_dev->usb_lock);
619 return ret;
622 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
624 int ret;
626 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
627 if (ret < 0)
628 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
629 return ret;
632 /* Note both the queue and the usb lock should be hold when calling this */
633 static void gspca_stream_off(struct gspca_dev *gspca_dev)
635 gspca_dev->streaming = 0;
636 atomic_set(&gspca_dev->nevent, 0);
637 if (gspca_dev->present) {
638 gspca_dev->sd_desc->stopN(gspca_dev);
639 destroy_urbs(gspca_dev);
640 gspca_set_alt0(gspca_dev);
641 gspca_dev->sd_desc->stop0(gspca_dev);
642 PDEBUG(D_STREAM, "stream off OK");
643 } else {
644 destroy_urbs(gspca_dev);
645 atomic_inc(&gspca_dev->nevent);
646 wake_up_interruptible(&gspca_dev->wq);
647 PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
651 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
653 int i;
655 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
656 gspca_dev->curr_mode = i;
657 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
658 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
659 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;
662 static int wxh_to_mode(struct gspca_dev *gspca_dev,
663 int width, int height)
665 int i;
667 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
668 if (width >= gspca_dev->cam.cam_mode[i].width
669 && height >= gspca_dev->cam.cam_mode[i].height)
670 break;
672 return i;
676 * search a mode with the right pixel format
678 static int gspca_get_mode(struct gspca_dev *gspca_dev,
679 int mode,
680 int pixfmt)
682 int modeU, modeD;
684 modeU = modeD = mode;
685 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
686 if (--modeD >= 0) {
687 if (gspca_dev->cam.cam_mode[modeD].pixelformat
688 == pixfmt)
689 return modeD;
691 if (++modeU < gspca_dev->cam.nmodes) {
692 if (gspca_dev->cam.cam_mode[modeU].pixelformat
693 == pixfmt)
694 return modeU;
697 return -EINVAL;
700 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
701 struct v4l2_fmtdesc *fmtdesc)
703 struct gspca_dev *gspca_dev = priv;
704 int i, j, index;
705 __u32 fmt_tb[8];
707 /* give an index to each format */
708 index = 0;
709 j = 0;
710 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
711 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
712 j = 0;
713 for (;;) {
714 if (fmt_tb[j] == fmt_tb[index])
715 break;
716 j++;
718 if (j == index) {
719 if (fmtdesc->index == index)
720 break; /* new format */
721 index++;
722 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
723 return -EINVAL;
726 if (i < 0)
727 return -EINVAL; /* no more format */
729 fmtdesc->pixelformat = fmt_tb[index];
730 if (gspca_is_compressed(fmt_tb[index]))
731 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
732 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
733 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
734 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
735 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
736 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
737 fmtdesc->description[4] = '\0';
738 return 0;
741 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
742 struct v4l2_format *fmt)
744 struct gspca_dev *gspca_dev = priv;
745 int mode;
747 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
748 return -EINVAL;
749 mode = gspca_dev->curr_mode;
750 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
751 sizeof fmt->fmt.pix);
752 return 0;
755 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
756 struct v4l2_format *fmt)
758 int w, h, mode, mode2;
760 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
761 return -EINVAL;
762 w = fmt->fmt.pix.width;
763 h = fmt->fmt.pix.height;
765 /* (luvcview problem) */
766 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
767 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
768 #ifdef CONFIG_VIDEO_ADV_DEBUG
769 if (gspca_debug & D_CONF)
770 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
771 #endif
772 /* search the closest mode for width and height */
773 mode = wxh_to_mode(gspca_dev, w, h);
775 /* OK if right palette */
776 if (gspca_dev->cam.cam_mode[mode].pixelformat
777 != fmt->fmt.pix.pixelformat) {
779 /* else, search the closest mode with the same pixel format */
780 mode2 = gspca_get_mode(gspca_dev, mode,
781 fmt->fmt.pix.pixelformat);
782 if (mode2 >= 0)
783 mode = mode2;
784 /* else
785 ; * no chance, return this mode */
787 memcpy(&fmt->fmt.pix, &gspca_dev->cam.cam_mode[mode],
788 sizeof fmt->fmt.pix);
789 return mode; /* used when s_fmt */
792 static int vidioc_try_fmt_vid_cap(struct file *file,
793 void *priv,
794 struct v4l2_format *fmt)
796 struct gspca_dev *gspca_dev = priv;
797 int ret;
799 ret = try_fmt_vid_cap(gspca_dev, fmt);
800 if (ret < 0)
801 return ret;
802 return 0;
805 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
806 struct v4l2_format *fmt)
808 struct gspca_dev *gspca_dev = priv;
809 int ret;
811 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
812 return -ERESTARTSYS;
814 ret = try_fmt_vid_cap(gspca_dev, fmt);
815 if (ret < 0)
816 goto out;
818 if (gspca_dev->nframes != 0
819 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
820 ret = -EINVAL;
821 goto out;
824 if (ret == gspca_dev->curr_mode) {
825 ret = 0;
826 goto out; /* same mode */
829 if (gspca_dev->streaming) {
830 ret = -EBUSY;
831 goto out;
833 gspca_dev->width = fmt->fmt.pix.width;
834 gspca_dev->height = fmt->fmt.pix.height;
835 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
836 gspca_dev->curr_mode = ret;
838 ret = 0;
839 out:
840 mutex_unlock(&gspca_dev->queue_lock);
841 return ret;
844 static int dev_open(struct inode *inode, struct file *file)
846 struct gspca_dev *gspca_dev;
847 int ret;
849 PDEBUG(D_STREAM, "%s open", current->comm);
850 gspca_dev = (struct gspca_dev *) video_devdata(file);
851 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
852 return -ERESTARTSYS;
853 if (!gspca_dev->present) {
854 ret = -ENODEV;
855 goto out;
858 /* if not done yet, initialize the sensor */
859 if (gspca_dev->users == 0) {
860 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
861 ret = -ERESTARTSYS;
862 goto out;
864 ret = gspca_dev->sd_desc->open(gspca_dev);
865 mutex_unlock(&gspca_dev->usb_lock);
866 if (ret != 0) {
867 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
868 goto out;
870 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
871 ret = -EBUSY;
872 goto out;
874 gspca_dev->users++;
875 file->private_data = gspca_dev;
876 #ifdef CONFIG_VIDEO_ADV_DEBUG
877 /* activate the v4l2 debug */
878 if (gspca_debug & D_V4L2)
879 gspca_dev->vdev.debug |= 3;
880 else
881 gspca_dev->vdev.debug &= ~3;
882 #endif
883 out:
884 mutex_unlock(&gspca_dev->queue_lock);
885 if (ret != 0)
886 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
887 else
888 PDEBUG(D_STREAM, "open done");
889 return ret;
892 static int dev_close(struct inode *inode, struct file *file)
894 struct gspca_dev *gspca_dev = file->private_data;
896 PDEBUG(D_STREAM, "%s close", current->comm);
897 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
898 return -ERESTARTSYS;
899 gspca_dev->users--;
901 /* if the file did capture, free the streaming resources */
902 if (gspca_dev->capt_file == file) {
903 mutex_lock(&gspca_dev->usb_lock);
904 if (gspca_dev->streaming)
905 gspca_stream_off(gspca_dev);
906 gspca_dev->sd_desc->close(gspca_dev);
907 mutex_unlock(&gspca_dev->usb_lock);
908 frame_free(gspca_dev);
909 gspca_dev->capt_file = NULL;
910 gspca_dev->memory = GSPCA_MEMORY_NO;
912 file->private_data = NULL;
913 mutex_unlock(&gspca_dev->queue_lock);
914 PDEBUG(D_STREAM, "close done");
915 return 0;
918 static int vidioc_querycap(struct file *file, void *priv,
919 struct v4l2_capability *cap)
921 struct gspca_dev *gspca_dev = priv;
923 memset(cap, 0, sizeof *cap);
924 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
925 strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
926 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
927 sizeof cap->bus_info);
928 cap->version = DRIVER_VERSION_NUMBER;
929 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
930 | V4L2_CAP_STREAMING
931 | V4L2_CAP_READWRITE;
932 return 0;
935 /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
936 static int vidioc_queryctrl(struct file *file, void *priv,
937 struct v4l2_queryctrl *q_ctrl)
939 struct gspca_dev *gspca_dev = priv;
940 int i;
941 u32 id;
943 id = q_ctrl->id;
944 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
945 id &= V4L2_CTRL_ID_MASK;
946 id++;
947 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
948 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
949 memcpy(q_ctrl,
950 &gspca_dev->sd_desc->ctrls[i].qctrl,
951 sizeof *q_ctrl);
952 return 0;
955 return -EINVAL;
957 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
958 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
959 memcpy(q_ctrl,
960 &gspca_dev->sd_desc->ctrls[i].qctrl,
961 sizeof *q_ctrl);
962 return 0;
965 if (id >= V4L2_CID_BASE
966 && id <= V4L2_CID_LASTP1) {
967 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
968 return 0;
970 return -EINVAL;
973 static int vidioc_s_ctrl(struct file *file, void *priv,
974 struct v4l2_control *ctrl)
976 struct gspca_dev *gspca_dev = priv;
977 const struct ctrl *ctrls;
978 int i, ret;
980 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
981 i < gspca_dev->sd_desc->nctrls;
982 i++, ctrls++) {
983 if (ctrl->id != ctrls->qctrl.id)
984 continue;
985 if (ctrl->value < ctrls->qctrl.minimum
986 && ctrl->value > ctrls->qctrl.maximum)
987 return -ERANGE;
988 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
989 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
990 return -ERESTARTSYS;
991 ret = ctrls->set(gspca_dev, ctrl->value);
992 mutex_unlock(&gspca_dev->usb_lock);
993 return ret;
995 return -EINVAL;
998 static int vidioc_g_ctrl(struct file *file, void *priv,
999 struct v4l2_control *ctrl)
1001 struct gspca_dev *gspca_dev = priv;
1003 const struct ctrl *ctrls;
1004 int i, ret;
1006 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1007 i < gspca_dev->sd_desc->nctrls;
1008 i++, ctrls++) {
1009 if (ctrl->id != ctrls->qctrl.id)
1010 continue;
1011 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1012 return -ERESTARTSYS;
1013 ret = ctrls->get(gspca_dev, &ctrl->value);
1014 mutex_unlock(&gspca_dev->usb_lock);
1015 return ret;
1017 return -EINVAL;
1020 static int vidioc_querymenu(struct file *file, void *priv,
1021 struct v4l2_querymenu *qmenu)
1023 struct gspca_dev *gspca_dev = priv;
1025 if (!gspca_dev->sd_desc->querymenu)
1026 return -EINVAL;
1027 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1030 static int vidioc_enum_input(struct file *file, void *priv,
1031 struct v4l2_input *input)
1033 struct gspca_dev *gspca_dev = priv;
1035 if (input->index != 0)
1036 return -EINVAL;
1037 memset(input, 0, sizeof *input);
1038 input->type = V4L2_INPUT_TYPE_CAMERA;
1039 strncpy(input->name, gspca_dev->sd_desc->name,
1040 sizeof input->name);
1041 return 0;
1044 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1046 *i = 0;
1047 return 0;
1050 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1052 if (i > 0)
1053 return -EINVAL;
1054 return (0);
1057 static int vidioc_reqbufs(struct file *file, void *priv,
1058 struct v4l2_requestbuffers *rb)
1060 struct gspca_dev *gspca_dev = priv;
1061 int i, ret = 0;
1063 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1064 return -EINVAL;
1065 switch (rb->memory) {
1066 case V4L2_MEMORY_MMAP:
1067 case V4L2_MEMORY_USERPTR:
1068 break;
1069 default:
1070 return -EINVAL;
1072 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1073 return -ERESTARTSYS;
1075 for (i = 0; i < gspca_dev->nframes; i++) {
1076 if (gspca_dev->frame[i].vma_use_count) {
1077 ret = -EBUSY;
1078 goto out;
1082 /* only one file may do capture */
1083 if ((gspca_dev->capt_file != NULL && gspca_dev->capt_file != file)
1084 || gspca_dev->streaming) {
1085 ret = -EBUSY;
1086 goto out;
1089 if (rb->count == 0) { /* unrequest? */
1090 frame_free(gspca_dev);
1091 gspca_dev->capt_file = NULL;
1092 } else {
1093 gspca_dev->memory = rb->memory;
1094 ret = frame_alloc(gspca_dev, rb->count);
1095 if (ret == 0) {
1096 rb->count = gspca_dev->nframes;
1097 gspca_dev->capt_file = file;
1100 out:
1101 mutex_unlock(&gspca_dev->queue_lock);
1102 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1103 return ret;
1106 static int vidioc_querybuf(struct file *file, void *priv,
1107 struct v4l2_buffer *v4l2_buf)
1109 struct gspca_dev *gspca_dev = priv;
1110 struct gspca_frame *frame;
1112 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1113 || v4l2_buf->index < 0
1114 || v4l2_buf->index >= gspca_dev->nframes)
1115 return -EINVAL;
1117 frame = &gspca_dev->frame[v4l2_buf->index];
1118 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1119 return 0;
1122 static int vidioc_streamon(struct file *file, void *priv,
1123 enum v4l2_buf_type buf_type)
1125 struct gspca_dev *gspca_dev = priv;
1126 int ret;
1128 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1129 return -EINVAL;
1130 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1131 return -ERESTARTSYS;
1132 if (!gspca_dev->present) {
1133 ret = -ENODEV;
1134 goto out;
1136 if (gspca_dev->nframes == 0) {
1137 ret = -EINVAL;
1138 goto out;
1140 if (gspca_dev->capt_file != file) {
1141 ret = -EINVAL;
1142 goto out;
1144 if (!gspca_dev->streaming) {
1145 ret = gspca_init_transfer(gspca_dev);
1146 if (ret < 0)
1147 goto out;
1149 #ifdef CONFIG_VIDEO_ADV_DEBUG
1150 if (gspca_debug & D_STREAM) {
1151 PDEBUG_MODE("stream on OK",
1152 gspca_dev->pixfmt,
1153 gspca_dev->width,
1154 gspca_dev->height);
1156 #endif
1157 ret = 0;
1158 out:
1159 mutex_unlock(&gspca_dev->queue_lock);
1160 return ret;
1163 static int vidioc_streamoff(struct file *file, void *priv,
1164 enum v4l2_buf_type buf_type)
1166 struct gspca_dev *gspca_dev = priv;
1167 int ret;
1169 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1170 return -EINVAL;
1171 if (!gspca_dev->streaming)
1172 return 0;
1173 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1174 return -ERESTARTSYS;
1175 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1176 ret = -ERESTARTSYS;
1177 goto out;
1179 if (gspca_dev->capt_file != file) {
1180 ret = -EINVAL;
1181 goto out2;
1183 gspca_stream_off(gspca_dev);
1184 ret = 0;
1185 out2:
1186 mutex_unlock(&gspca_dev->usb_lock);
1187 out:
1188 mutex_unlock(&gspca_dev->queue_lock);
1189 return ret;
1192 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1193 struct v4l2_jpegcompression *jpegcomp)
1195 struct gspca_dev *gspca_dev = priv;
1196 int ret;
1198 if (!gspca_dev->sd_desc->get_jcomp)
1199 return -EINVAL;
1200 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1201 return -ERESTARTSYS;
1202 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1203 mutex_unlock(&gspca_dev->usb_lock);
1204 return ret;
1207 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1208 struct v4l2_jpegcompression *jpegcomp)
1210 struct gspca_dev *gspca_dev = priv;
1211 int ret;
1213 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1214 return -ERESTARTSYS;
1215 if (!gspca_dev->sd_desc->set_jcomp)
1216 return -EINVAL;
1217 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1218 mutex_unlock(&gspca_dev->usb_lock);
1219 return ret;
1222 static int vidioc_g_parm(struct file *filp, void *priv,
1223 struct v4l2_streamparm *parm)
1225 struct gspca_dev *gspca_dev = priv;
1227 memset(parm, 0, sizeof *parm);
1228 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1229 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1230 return 0;
1233 static int vidioc_s_parm(struct file *filp, void *priv,
1234 struct v4l2_streamparm *parm)
1236 struct gspca_dev *gspca_dev = priv;
1237 int n;
1239 n = parm->parm.capture.readbuffers;
1240 if (n == 0 || n > GSPCA_MAX_FRAMES)
1241 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1242 else
1243 gspca_dev->nbufread = n;
1244 return 0;
1247 static int vidioc_s_std(struct file *filp, void *priv,
1248 v4l2_std_id *parm)
1250 return 0;
1253 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1254 static int vidiocgmbuf(struct file *file, void *priv,
1255 struct video_mbuf *mbuf)
1257 struct gspca_dev *gspca_dev = file->private_data;
1258 int i;
1260 PDEBUG(D_STREAM, "cgmbuf");
1261 if (gspca_dev->nframes == 0) {
1262 int ret;
1265 struct v4l2_format fmt;
1267 memset(&fmt, 0, sizeof fmt);
1268 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1270 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1271 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1272 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1273 ret = vidioc_s_fmt_vid_cap(file, priv, &fmt);
1274 if (ret != 0)
1275 return ret;
1278 struct v4l2_requestbuffers rb;
1280 memset(&rb, 0, sizeof rb);
1281 rb.count = 4;
1282 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1283 rb.memory = V4L2_MEMORY_MMAP;
1284 ret = vidioc_reqbufs(file, priv, &rb);
1285 if (ret != 0)
1286 return ret;
1289 mbuf->frames = gspca_dev->nframes;
1290 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1291 for (i = 0; i < mbuf->frames; i++)
1292 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1293 return 0;
1295 #endif
1297 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1299 struct gspca_dev *gspca_dev = file->private_data;
1300 struct gspca_frame *frame;
1301 struct page *page;
1302 unsigned long addr, start, size;
1303 int i, ret;
1304 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1305 int compat = 0;
1306 #endif
1308 start = vma->vm_start;
1309 size = vma->vm_end - vma->vm_start;
1310 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1312 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1313 return -ERESTARTSYS;
1314 if (!gspca_dev->present) {
1315 ret = -ENODEV;
1316 goto out;
1318 if (gspca_dev->capt_file != file) {
1319 ret = -EINVAL;
1320 goto out;
1323 frame = NULL;
1324 for (i = 0; i < gspca_dev->nframes; ++i) {
1325 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1326 PDEBUG(D_STREAM, "mmap bad memory type");
1327 break;
1329 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1330 == vma->vm_pgoff) {
1331 frame = &gspca_dev->frame[i];
1332 break;
1335 if (frame == NULL) {
1336 PDEBUG(D_STREAM, "mmap no frame buffer found");
1337 ret = -EINVAL;
1338 goto out;
1340 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1341 if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes)
1342 compat = 1;
1343 else
1344 #endif
1345 if (size != frame->v4l2_buf.length) {
1346 PDEBUG(D_STREAM, "mmap bad size");
1347 ret = -EINVAL;
1348 goto out;
1352 * - VM_IO marks the area as being a mmaped region for I/O to a
1353 * device. It also prevents the region from being core dumped.
1355 vma->vm_flags |= VM_IO;
1357 addr = (unsigned long) frame->data;
1358 while (size > 0) {
1359 page = vmalloc_to_page((void *) addr);
1360 ret = vm_insert_page(vma, start, page);
1361 if (ret < 0)
1362 goto out;
1363 start += PAGE_SIZE;
1364 addr += PAGE_SIZE;
1365 size -= PAGE_SIZE;
1368 vma->vm_ops = &gspca_vm_ops;
1369 vma->vm_private_data = frame;
1370 gspca_vm_open(vma);
1371 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1372 if (compat) {
1373 /*fixme: ugly*/
1374 for (i = 1; i < gspca_dev->nframes; ++i)
1375 gspca_dev->frame[i].v4l2_buf.flags |=
1376 V4L2_BUF_FLAG_MAPPED;
1378 #endif
1379 ret = 0;
1380 out:
1381 mutex_unlock(&gspca_dev->queue_lock);
1382 return ret;
1386 * wait for a video frame
1388 * If a frame is ready, its index is returned.
1390 static int frame_wait(struct gspca_dev *gspca_dev,
1391 int nonblock_ing)
1393 struct gspca_frame *frame;
1394 int i, j, ret;
1396 /* if userptr, treat the awaiting URBs */
1397 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1398 isoc_transfer(gspca_dev);
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 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1425 isoc_transfer(gspca_dev);
1426 i = gspca_dev->fr_o;
1427 j = gspca_dev->fr_queue[i];
1428 frame = &gspca_dev->frame[j];
1429 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1430 break;
1433 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1434 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1435 gspca_dev->fr_q,
1436 gspca_dev->fr_i,
1437 gspca_dev->fr_o);
1439 if (gspca_dev->sd_desc->dq_callback)
1440 gspca_dev->sd_desc->dq_callback(gspca_dev);
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 || (v4l2_buf->memory != V4L2_MEMORY_MMAP
1460 && v4l2_buf->memory != V4L2_MEMORY_USERPTR))
1461 return -EINVAL;
1462 if (!gspca_dev->streaming)
1463 return -EINVAL;
1464 if (gspca_dev->capt_file != file) {
1465 ret = -EINVAL;
1466 goto out;
1469 /* only one read */
1470 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1471 return -ERESTARTSYS;
1473 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1474 if (ret < 0)
1475 goto out;
1476 i = ret; /* frame index */
1477 frame = &gspca_dev->frame[i];
1478 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1479 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1480 PDEBUG(D_FRAM, "dqbuf %d", i);
1481 ret = 0;
1482 out:
1483 mutex_unlock(&gspca_dev->read_lock);
1484 return ret;
1488 * queue a video buffer
1490 * Attempting to queue a buffer that has already been
1491 * queued will return -EINVAL.
1493 static int vidioc_qbuf(struct file *file, void *priv,
1494 struct v4l2_buffer *v4l2_buf)
1496 struct gspca_dev *gspca_dev = priv;
1497 struct gspca_frame *frame;
1498 int i, index, ret;
1500 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1501 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1502 return -EINVAL;
1504 index = v4l2_buf->index;
1505 if ((unsigned) index >= gspca_dev->nframes) {
1506 PDEBUG(D_FRAM,
1507 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1508 return -EINVAL;
1510 frame = &gspca_dev->frame[index];
1512 if (v4l2_buf->memory != frame->v4l2_buf.memory) {
1513 PDEBUG(D_FRAM, "qbuf bad memory type");
1514 return -EINVAL;
1516 if (gspca_dev->capt_file != file)
1517 return -EINVAL;
1519 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1520 return -ERESTARTSYS;
1522 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1523 PDEBUG(D_FRAM, "qbuf bad state");
1524 ret = -EINVAL;
1525 goto out;
1528 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1529 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1531 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1532 frame->data = frame->data_end =
1533 (__u8 *) v4l2_buf->m.userptr;
1534 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1535 frame->v4l2_buf.length = v4l2_buf->length;
1538 /* put the buffer in the 'queued' queue */
1539 i = gspca_dev->fr_q;
1540 gspca_dev->fr_queue[i] = index;
1541 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1542 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1543 gspca_dev->fr_q,
1544 gspca_dev->fr_i,
1545 gspca_dev->fr_o);
1547 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1548 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1549 ret = 0;
1550 out:
1551 mutex_unlock(&gspca_dev->queue_lock);
1552 return ret;
1556 * allocate the resources for read()
1558 static int read_alloc(struct gspca_dev *gspca_dev,
1559 struct file *file)
1561 struct v4l2_buffer v4l2_buf;
1562 int i, ret;
1564 PDEBUG(D_STREAM, "read alloc");
1565 if (gspca_dev->nframes == 0) {
1566 struct v4l2_requestbuffers rb;
1568 memset(&rb, 0, sizeof rb);
1569 rb.count = gspca_dev->nbufread;
1570 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1571 rb.memory = V4L2_MEMORY_MMAP;
1572 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1573 if (ret != 0) {
1574 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1575 return ret;
1577 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1578 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1579 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1580 for (i = 0; i < gspca_dev->nbufread; i++) {
1581 v4l2_buf.index = i;
1582 /*fixme: ugly!*/
1583 gspca_dev->frame[i].v4l2_buf.flags |=
1584 V4L2_BUF_FLAG_MAPPED;
1585 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1586 if (ret != 0) {
1587 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1588 return ret;
1591 gspca_dev->memory = GSPCA_MEMORY_READ;
1594 /* start streaming */
1595 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1596 if (ret != 0)
1597 PDEBUG(D_STREAM, "read streamon err %d", ret);
1598 return ret;
1601 static unsigned int dev_poll(struct file *file, poll_table *wait)
1603 struct gspca_dev *gspca_dev = file->private_data;
1604 int i, ret;
1606 PDEBUG(D_FRAM, "poll");
1608 poll_wait(file, &gspca_dev->wq, wait);
1609 if (!gspca_dev->present)
1610 return POLLERR;
1612 /* if not streaming, the user would use read() */
1613 if (!gspca_dev->streaming) {
1614 if (gspca_dev->memory != GSPCA_MEMORY_NO) {
1615 ret = POLLERR; /* not the 1st time */
1616 goto out;
1618 ret = read_alloc(gspca_dev, file);
1619 if (ret != 0) {
1620 ret = POLLERR;
1621 goto out;
1625 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1626 return POLLERR;
1627 if (!gspca_dev->present) {
1628 ret = POLLERR;
1629 goto out;
1632 /* if userptr, treat the awaiting URBs */
1633 if (gspca_dev->memory == V4L2_MEMORY_USERPTR
1634 && gspca_dev->capt_file == file)
1635 isoc_transfer(gspca_dev);
1637 i = gspca_dev->fr_o;
1638 i = gspca_dev->fr_queue[i];
1639 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1640 ret = POLLIN | POLLRDNORM; /* something to read */
1641 else
1642 ret = 0;
1643 out:
1644 mutex_unlock(&gspca_dev->queue_lock);
1645 return ret;
1648 static ssize_t dev_read(struct file *file, char __user *data,
1649 size_t count, loff_t *ppos)
1651 struct gspca_dev *gspca_dev = file->private_data;
1652 struct gspca_frame *frame;
1653 struct v4l2_buffer v4l2_buf;
1654 struct timeval timestamp;
1655 int n, ret, ret2;
1657 PDEBUG(D_FRAM, "read (%d)", count);
1658 if (!gspca_dev->present)
1659 return -ENODEV;
1660 switch (gspca_dev->memory) {
1661 case GSPCA_MEMORY_NO: /* first time */
1662 ret = read_alloc(gspca_dev, file);
1663 if (ret != 0)
1664 return ret;
1665 break;
1666 case GSPCA_MEMORY_READ:
1667 if (gspca_dev->capt_file == file)
1668 break;
1669 /* fall thru */
1670 default:
1671 return -EINVAL;
1674 /* get a frame */
1675 jiffies_to_timeval(get_jiffies_64(), &timestamp);
1676 timestamp.tv_sec--;
1677 n = 2;
1678 for (;;) {
1679 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1680 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1681 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1682 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1683 if (ret != 0) {
1684 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1685 return ret;
1688 /* if the process slept for more than 1 second,
1689 * get anewer frame */
1690 frame = &gspca_dev->frame[v4l2_buf.index];
1691 if (--n < 0)
1692 break; /* avoid infinite loop */
1693 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1694 break;
1695 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1696 if (ret != 0) {
1697 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1698 return ret;
1702 /* copy the frame */
1703 if (count < frame->v4l2_buf.bytesused) {
1704 PDEBUG(D_STREAM, "read bad count: %d < %d",
1705 count, frame->v4l2_buf.bytesused);
1706 /*fixme: special errno?*/
1707 ret = -EINVAL;
1708 goto out;
1710 count = frame->v4l2_buf.bytesused;
1711 ret = copy_to_user(data, frame->data, count);
1712 if (ret != 0) {
1713 PDEBUG(D_ERR|D_STREAM,
1714 "read cp to user lack %d / %d", ret, count);
1715 ret = -EFAULT;
1716 goto out;
1718 ret = count;
1719 out:
1720 /* in each case, requeue the buffer */
1721 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1722 if (ret2 != 0)
1723 return ret2;
1724 return ret;
1727 static void dev_release(struct video_device *vfd)
1729 /* nothing */
1732 static struct file_operations dev_fops = {
1733 .owner = THIS_MODULE,
1734 .open = dev_open,
1735 .release = dev_close,
1736 .read = dev_read,
1737 .mmap = dev_mmap,
1738 .ioctl = video_ioctl2,
1739 #ifdef CONFIG_COMPAT
1740 .compat_ioctl = v4l_compat_ioctl32,
1741 #endif
1742 .llseek = no_llseek,
1743 .poll = dev_poll,
1746 static struct video_device gspca_template = {
1747 .name = "gspca main driver",
1748 .type = VID_TYPE_CAPTURE,
1749 .fops = &dev_fops,
1750 .release = dev_release, /* mandatory */
1751 .minor = -1,
1752 .vidioc_querycap = vidioc_querycap,
1753 .vidioc_dqbuf = vidioc_dqbuf,
1754 .vidioc_qbuf = vidioc_qbuf,
1755 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1756 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1757 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1758 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1759 .vidioc_streamon = vidioc_streamon,
1760 .vidioc_queryctrl = vidioc_queryctrl,
1761 .vidioc_g_ctrl = vidioc_g_ctrl,
1762 .vidioc_s_ctrl = vidioc_s_ctrl,
1763 .vidioc_querymenu = vidioc_querymenu,
1764 .vidioc_enum_input = vidioc_enum_input,
1765 .vidioc_g_input = vidioc_g_input,
1766 .vidioc_s_input = vidioc_s_input,
1767 .vidioc_reqbufs = vidioc_reqbufs,
1768 .vidioc_querybuf = vidioc_querybuf,
1769 .vidioc_streamoff = vidioc_streamoff,
1770 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1771 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1772 .vidioc_g_parm = vidioc_g_parm,
1773 .vidioc_s_parm = vidioc_s_parm,
1774 .vidioc_s_std = vidioc_s_std,
1775 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1776 .vidiocgmbuf = vidiocgmbuf,
1777 #endif
1781 * probe and create a new gspca device
1783 * This function must be called by the sub-driver when it is
1784 * called for probing a new device.
1786 int gspca_dev_probe(struct usb_interface *intf,
1787 const struct usb_device_id *id,
1788 const struct sd_desc *sd_desc,
1789 int dev_size,
1790 struct module *module)
1792 struct usb_interface_descriptor *interface;
1793 struct gspca_dev *gspca_dev;
1794 struct usb_device *dev = interface_to_usbdev(intf);
1795 int ret;
1797 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1799 /* we don't handle multi-config cameras */
1800 if (dev->descriptor.bNumConfigurations != 1)
1801 return -ENODEV;
1802 interface = &intf->cur_altsetting->desc;
1803 if (interface->bInterfaceNumber > 0)
1804 return -ENODEV;
1806 /* create the device */
1807 if (dev_size < sizeof *gspca_dev)
1808 dev_size = sizeof *gspca_dev;
1809 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1810 if (gspca_dev == NULL) {
1811 err("couldn't kzalloc gspca struct");
1812 return -EIO;
1814 gspca_dev->dev = dev;
1815 gspca_dev->iface = interface->bInterfaceNumber;
1816 gspca_dev->nbalt = intf->num_altsetting;
1817 gspca_dev->sd_desc = sd_desc;
1818 /* gspca_dev->users = 0; (done by kzalloc) */
1819 gspca_dev->nbufread = 2;
1821 /* configure the subdriver */
1822 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1823 if (ret < 0)
1824 goto out;
1825 ret = gspca_set_alt0(gspca_dev);
1826 if (ret < 0)
1827 goto out;
1828 gspca_set_default_mode(gspca_dev);
1830 mutex_init(&gspca_dev->usb_lock);
1831 mutex_init(&gspca_dev->read_lock);
1832 mutex_init(&gspca_dev->queue_lock);
1833 init_waitqueue_head(&gspca_dev->wq);
1835 /* init video stuff */
1836 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1837 gspca_dev->vdev.dev = &dev->dev;
1838 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1839 gspca_dev->vdev.fops = &gspca_dev->fops;
1840 gspca_dev->fops.owner = module; /* module protection */
1841 ret = video_register_device(&gspca_dev->vdev,
1842 VFL_TYPE_GRABBER,
1843 video_nr);
1844 if (ret < 0) {
1845 err("video_register_device err %d", ret);
1846 goto out;
1849 gspca_dev->present = 1;
1850 usb_set_intfdata(intf, gspca_dev);
1851 PDEBUG(D_PROBE, "probe ok");
1852 return 0;
1853 out:
1854 kfree(gspca_dev);
1855 return ret;
1857 EXPORT_SYMBOL(gspca_dev_probe);
1860 * USB disconnection
1862 * This function must be called by the sub-driver
1863 * when the device disconnects, after the specific resources are freed.
1865 void gspca_disconnect(struct usb_interface *intf)
1867 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1869 if (!gspca_dev)
1870 return;
1871 gspca_dev->present = 0;
1872 mutex_lock(&gspca_dev->queue_lock);
1873 mutex_lock(&gspca_dev->usb_lock);
1874 gspca_dev->streaming = 0;
1875 destroy_urbs(gspca_dev);
1876 mutex_unlock(&gspca_dev->usb_lock);
1877 mutex_unlock(&gspca_dev->queue_lock);
1878 while (gspca_dev->users != 0) { /* wait until fully closed */
1879 atomic_inc(&gspca_dev->nevent);
1880 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1881 schedule();
1883 /* We don't want people trying to open up the device */
1884 video_unregister_device(&gspca_dev->vdev);
1885 /* Free the memory */
1886 kfree(gspca_dev);
1887 PDEBUG(D_PROBE, "disconnect complete");
1889 EXPORT_SYMBOL(gspca_disconnect);
1891 /* -- module insert / remove -- */
1892 static int __init gspca_init(void)
1894 info("main v%s registered", version);
1895 return 0;
1897 static void __exit gspca_exit(void)
1899 info("main deregistered");
1902 module_init(gspca_init);
1903 module_exit(gspca_exit);
1905 #ifdef CONFIG_VIDEO_ADV_DEBUG
1906 module_param_named(debug, gspca_debug, int, 0644);
1907 MODULE_PARM_DESC(debug,
1908 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
1909 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
1910 " 0x0100: v4l2");
1911 #endif