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
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>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
33 #include <asm/uaccess.h>
34 #include <linux/jiffies.h>
38 #undef CONFIG_VIDEO_V4L1_COMPAT
41 #define DEF_NURBS 2 /* default number of URBs (mmap) */
42 #define USR_NURBS 5 /* default number of URBs (userptr) */
44 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46 MODULE_LICENSE("GPL");
48 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 0)
49 static const char version[] = "2.1.0";
51 static int video_nr = -1;
53 static int comp_fac = 30; /* Buffer size ratio when compressed in % */
55 #ifdef VIDEO_ADV_DEBUG
56 int gspca_debug = D_ERR | D_PROBE;
57 EXPORT_SYMBOL(gspca_debug);
59 static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
61 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
62 PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d",
66 (pixfmt >> 16) & 0xff,
70 PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d",
77 #define PDEBUG_MODE(txt, pixfmt, w, h)
80 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
81 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
82 #define GSPCA_MEMORY_READ 7
84 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
89 static void gspca_vm_open(struct vm_area_struct *vma)
91 struct gspca_frame *frame = vma->vm_private_data;
93 frame->vma_use_count++;
94 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
97 static void gspca_vm_close(struct vm_area_struct *vma)
99 struct gspca_frame *frame = vma->vm_private_data;
101 if (--frame->vma_use_count <= 0)
102 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
105 static struct vm_operations_struct gspca_vm_ops = {
106 .open = gspca_vm_open,
107 .close = gspca_vm_close,
111 * fill a video frame from an URB and resubmit
113 static void fill_frame(struct gspca_dev *gspca_dev,
116 struct gspca_frame *frame;
117 __u8 *data; /* address of data in the iso message */
121 if (urb->status != 0) {
122 PDEBUG(D_ERR|D_PACK, "urb status: %d", urb->status);
123 return; /* disconnection ? */
125 pkt_scan = gspca_dev->sd_desc->pkt_scan;
126 for (i = 0; i < urb->number_of_packets; i++) {
128 /* check the availability of the frame buffer */
130 j = gspca_dev->fr_queue[j];
131 frame = &gspca_dev->frame[j];
132 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
133 != V4L2_BUF_FLAG_QUEUED) {
134 gspca_dev->last_packet_type = DISCARD_PACKET;
138 /* check the packet status and length */
139 len = urb->iso_frame_desc[i].actual_length;
142 st = urb->iso_frame_desc[i].status;
145 "ISOC data error: [%d] len=%d, status=%d",
147 gspca_dev->last_packet_type = DISCARD_PACKET;
151 /* let the packet be analyzed by the subdriver */
152 PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
153 i, urb->iso_frame_desc[i].offset, len);
154 data = (__u8 *) urb->transfer_buffer
155 + urb->iso_frame_desc[i].offset;
156 pkt_scan(gspca_dev, frame, data, len);
159 /* resubmit the URB */
160 /*fixme: don't do that when userptr and too many URBs sent*/
162 st = usb_submit_urb(urb, GFP_ATOMIC);
164 PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
168 * ISOC message interrupt from the USB device
170 * Analyse each packet and call the subdriver for copy
171 * to the frame buffer.
173 * There are 2 functions:
174 * - the first one (isoc_irq_mmap) is used when the application
175 * buffers are mapped. The frame detection and copy is done
176 * at interrupt level.
177 * - the second one (isoc_irq_user) is used when the application
178 * buffers are in user space (userptr). The frame detection
179 * and copy is done by the application.
181 static void isoc_irq_mmap(struct urb *urb
184 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
186 PDEBUG(D_PACK, "isoc irq mmap");
187 if (!gspca_dev->streaming)
189 fill_frame(gspca_dev, urb);
192 static void isoc_irq_user(struct urb *urb
195 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
198 PDEBUG(D_PACK, "isoc irq user");
199 if (!gspca_dev->streaming)
202 i = gspca_dev->urb_in % gspca_dev->nurbs;
203 if (urb != gspca_dev->urb[i]) {
204 PDEBUG(D_ERR|D_PACK, "urb out of sequence");
205 return; /* should never occur */
209 atomic_inc(&gspca_dev->nevent); /* new event */
210 wake_up_interruptible(&gspca_dev->wq);
211 /*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
215 * treat the isoc messages
217 * This routine is called by the application (case userptr).
219 static void isoc_transfer(struct gspca_dev *gspca_dev)
225 i = gspca_dev->urb_out;
226 PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
227 if (i == gspca_dev->urb_in) /* isoc message to read */
228 break; /* no (more) message */
229 atomic_dec(&gspca_dev->nevent);
230 /*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
231 gspca_dev->urb_out = i + 1; /* message treated */
232 urb = gspca_dev->urb[i % gspca_dev->nurbs];
233 fill_frame(gspca_dev, urb);
238 * add data to the current frame
240 * This function is called by the subdrivers at interrupt level
242 * To build a frame, these ones must add
244 * - 0 or many INTER_PACKETs
246 * DISCARD_PACKET invalidates the whole frame.
247 * On LAST_PACKET, a new frame is returned.
249 struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
251 struct gspca_frame *frame,
257 PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
259 /* when start of a new frame, if the current frame buffer
260 * is not queued, discard the whole frame */
261 if (packet_type == FIRST_PACKET) {
262 if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
263 != V4L2_BUF_FLAG_QUEUED) {
264 gspca_dev->last_packet_type = DISCARD_PACKET;
267 frame->data_end = frame->data;
268 jiffies_to_timeval(get_jiffies_64(),
269 &frame->v4l2_buf.timestamp);
270 frame->v4l2_buf.sequence = ++gspca_dev->sequence;
271 } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
275 /* append the packet to the frame buffer */
277 if (frame->data_end - frame->data + len
278 > frame->v4l2_buf.length) {
279 PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d",
280 frame->data_end - frame->data + len,
281 frame->v4l2_buf.length);
282 packet_type = DISCARD_PACKET;
284 if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR)
285 memcpy(frame->data_end, data, len);
287 copy_to_user(frame->data_end, data, len);
288 frame->data_end += len;
291 gspca_dev->last_packet_type = packet_type;
293 /* if last packet, wake the application and advance in the queue */
294 if (packet_type == LAST_PACKET) {
295 frame->v4l2_buf.bytesused = frame->data_end - frame->data;
296 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
297 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
298 atomic_inc(&gspca_dev->nevent);
299 wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
300 i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
302 PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
303 frame->v4l2_buf.bytesused,
307 j = gspca_dev->fr_queue[i];
308 frame = &gspca_dev->frame[j];
312 EXPORT_SYMBOL(gspca_frame_add);
314 static int gspca_is_compressed(__u32 format)
317 case V4L2_PIX_FMT_MJPEG:
318 case V4L2_PIX_FMT_JPEG:
319 case V4L2_PIX_FMT_SPCA561:
325 static void *rvmalloc(unsigned long size)
330 /* size = PAGE_ALIGN(size); (already done) */
331 mem = vmalloc_32(size);
333 memset(mem, 0, size);
334 adr = (unsigned long) mem;
335 while ((long) size > 0) {
336 SetPageReserved(vmalloc_to_page((void *) adr));
344 static void rvfree(void *mem, unsigned long size)
350 adr = (unsigned long) mem;
351 while ((long) size > 0) {
352 ClearPageReserved(vmalloc_to_page((void *) adr));
359 static __u32 get_v4l2_depth(__u32 pixfmt)
362 /* case V4L2_PIX_FMT_BGR32:
363 case V4L2_PIX_FMT_RGB32:
365 case V4L2_PIX_FMT_RGB24: /* 'RGB3' */
366 case V4L2_PIX_FMT_BGR24:
368 /* case V4L2_PIX_FMT_RGB565: * 'RGBP' */
369 case V4L2_PIX_FMT_YUYV: /* 'YUYV' packed 4.2.2 */
370 case V4L2_PIX_FMT_YYUV: /* 'YYUV' */
372 case V4L2_PIX_FMT_YUV420: /* 'YU12' planar 4.2.0 */
373 case V4L2_PIX_FMT_SPCA501: /* 'S501' YUYV per line */
375 case V4L2_PIX_FMT_MJPEG:
376 case V4L2_PIX_FMT_JPEG:
377 case V4L2_PIX_FMT_SBGGR8: /* 'BA81' Bayer */
378 case V4L2_PIX_FMT_SN9C10X: /* 'S910' SN9C10x compression */
379 case V4L2_PIX_FMT_SPCA561: /* 'S561' compressed BGGR bayer */
382 PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
384 (pixfmt >> 8) & 0xff,
385 (pixfmt >> 16) & 0xff,
390 static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
394 size = gspca_dev->width * gspca_dev->height
395 * get_v4l2_depth(gspca_dev->pixfmt) / 8;
401 static int frame_alloc(struct gspca_dev *gspca_dev,
404 struct gspca_frame *frame;
408 frsz = gspca_get_buff_size(gspca_dev);
411 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
412 if (count > GSPCA_MAX_FRAMES)
413 count = GSPCA_MAX_FRAMES;
414 /* if compressed (JPEG), reduce the buffer size */
415 if (gspca_is_compressed(gspca_dev->pixfmt))
416 frsz = (frsz * comp_fac) / 100 + 600; /* (+ JPEG header sz) */
417 frsz = PAGE_ALIGN(frsz);
418 PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
419 gspca_dev->frsz = frsz;
420 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
421 gspca_dev->frbuf = rvmalloc(frsz * count);
422 if (!gspca_dev->frbuf) {
423 err("frame alloc failed");
427 gspca_dev->nframes = count;
428 for (i = 0; i < count; i++) {
429 frame = &gspca_dev->frame[i];
430 frame->v4l2_buf.index = i;
431 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
432 frame->v4l2_buf.flags = 0;
433 frame->v4l2_buf.field = V4L2_FIELD_NONE;
434 frame->v4l2_buf.length = frsz;
435 frame->v4l2_buf.memory = gspca_dev->memory;
436 frame->v4l2_buf.sequence = 0;
437 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
438 frame->data = frame->data_end =
439 gspca_dev->frbuf + i * frsz;
440 frame->v4l2_buf.m.offset = i * frsz;
443 gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
444 gspca_dev->last_packet_type = DISCARD_PACKET;
445 gspca_dev->sequence = 0;
446 atomic_set(&gspca_dev->nevent, 0);
450 static void frame_free(struct gspca_dev *gspca_dev)
454 PDEBUG(D_STREAM, "frame free");
455 if (gspca_dev->frbuf != 0) {
456 rvfree(gspca_dev->frbuf,
457 gspca_dev->nframes * gspca_dev->frsz);
458 gspca_dev->frbuf = NULL;
459 for (i = 0; i < gspca_dev->nframes; i++)
460 gspca_dev->frame[i].data = NULL;
462 gspca_dev->nframes = 0;
465 static void destroy_urbs(struct gspca_dev *gspca_dev)
470 PDEBUG(D_STREAM, "kill transfer");
471 for (i = 0; i < MAX_NURBS; ++i) {
472 urb = gspca_dev->urb[i];
476 gspca_dev->urb[i] = NULL;
478 if (urb->transfer_buffer != 0)
479 usb_buffer_free(gspca_dev->dev,
480 urb->transfer_buffer_length,
481 urb->transfer_buffer,
488 * search an input isochronous endpoint in an alternate setting
490 static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt,
493 struct usb_host_endpoint *ep;
496 epaddr |= USB_DIR_IN;
497 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
498 ep = &alt->endpoint[i];
499 if (ep->desc.bEndpointAddress == epaddr) {
500 attr = ep->desc.bmAttributes
501 & USB_ENDPOINT_XFERTYPE_MASK;
502 if (attr == USB_ENDPOINT_XFER_ISOC)
511 * search an input isochronous endpoint
513 * The endpoint is defined by the subdriver.
514 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
515 * This routine may be called many times when the bandwidth is too small
516 * (the bandwidth is checked on urb submit).
518 struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
520 struct usb_interface *intf;
521 struct usb_host_endpoint *ep;
524 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
526 i = gspca_dev->alt; /* previous alt setting */
527 while (--i > 0) { /* alt 0 is unusable */
528 ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr);
533 err("no ISOC endpoint found");
536 PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x",
537 i, ep->desc.bEndpointAddress);
538 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i);
540 err("set interface err %d", ret);
543 gspca_dev->alt = i; /* memorize the current alt setting */
548 * create the isochronous URBs
550 static int create_urbs(struct gspca_dev *gspca_dev,
551 struct usb_host_endpoint *ep)
554 int n, nurbs, i, psize, npkt, bsize;
555 usb_complete_t usb_complete;
557 /* calculate the packet size and the number of packets */
558 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
560 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
561 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
562 npkt = ISO_MAX_SIZE / psize;
563 if (npkt > ISO_MAX_PKT)
565 bsize = psize * npkt;
567 "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
568 /*fixme:change for userptr*/
569 /*fixme:don't submit all URBs when userptr*/
570 if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
571 usb_complete = isoc_irq_mmap;
574 usb_complete = isoc_irq_user;
577 gspca_dev->nurbs = nurbs;
578 for (n = 0; n < nurbs; n++) {
579 urb = usb_alloc_urb(npkt, GFP_KERNEL);
581 err("usb_alloc_urb failed");
584 urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev,
589 if (urb->transfer_buffer == NULL) {
591 destroy_urbs(gspca_dev);
592 err("usb_buffer_urb failed");
595 gspca_dev->urb[n] = urb;
596 urb->dev = gspca_dev->dev;
597 urb->context = gspca_dev;
598 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
599 ep->desc.bEndpointAddress);
600 urb->transfer_flags = URB_ISO_ASAP
601 | URB_NO_TRANSFER_DMA_MAP;
602 urb->interval = ep->desc.bInterval;
603 urb->complete = usb_complete;
604 urb->number_of_packets = npkt;
605 urb->transfer_buffer_length = bsize;
606 for (i = 0; i < npkt; i++) {
607 urb->iso_frame_desc[i].length = psize;
608 urb->iso_frame_desc[i].offset = psize * i;
611 gspca_dev->urb_in = gspca_dev->urb_out = 0;
616 * start the USB transfer
618 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
620 struct usb_host_endpoint *ep;
623 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
626 /* set the higher alternate setting and
627 * loop until urb submit succeeds */
628 gspca_dev->alt = gspca_dev->nbalt;
630 PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
631 ep = get_isoc_ep(gspca_dev);
636 ret = create_urbs(gspca_dev, ep);
641 gspca_dev->sd_desc->start(gspca_dev);
642 gspca_dev->streaming = 1;
643 atomic_set(&gspca_dev->nevent, 0);
645 /* submit the URBs */
646 for (n = 0; n < gspca_dev->nurbs; n++) {
647 ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
649 PDEBUG(D_ERR|D_STREAM,
650 "usb_submit_urb [%d] err %d", n, ret);
651 gspca_dev->streaming = 0;
652 destroy_urbs(gspca_dev);
654 break; /* try the previous alt */
662 mutex_unlock(&gspca_dev->usb_lock);
666 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
670 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
672 PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret);
676 /* Note both the queue and the usb lock should be hold when calling this */
677 static void gspca_stream_off(struct gspca_dev *gspca_dev)
679 gspca_dev->streaming = 0;
680 atomic_set(&gspca_dev->nevent, 0);
681 if (gspca_dev->present) {
682 gspca_dev->sd_desc->stopN(gspca_dev);
683 destroy_urbs(gspca_dev);
684 gspca_set_alt0(gspca_dev);
685 gspca_dev->sd_desc->stop0(gspca_dev);
686 PDEBUG(D_STREAM, "stream off OK");
688 destroy_urbs(gspca_dev);
689 atomic_inc(&gspca_dev->nevent);
690 wake_up_interruptible(&gspca_dev->wq);
691 PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
695 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
699 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
700 gspca_dev->curr_mode = i;
701 gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
702 gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
703 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
706 static int wxh_to_mode(struct gspca_dev *gspca_dev,
707 int width, int height)
711 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
712 if (width >= gspca_dev->cam.cam_mode[i].width
713 && height >= gspca_dev->cam.cam_mode[i].height)
720 * search a mode with the right pixel format
722 static int gspca_get_mode(struct gspca_dev *gspca_dev,
728 modeU = modeD = mode;
729 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
731 if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt)
734 if (++modeU < gspca_dev->cam.nmodes) {
735 if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt)
742 static int vidioc_enum_fmt_cap(struct file *file, void *priv,
743 struct v4l2_fmtdesc *fmtdesc)
745 struct gspca_dev *gspca_dev = priv;
749 PDEBUG(D_CONF, "enum fmt cap");
751 /* give an index to each format */
754 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
755 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt;
758 if (fmt_tb[j] == fmt_tb[index])
763 if (fmtdesc->index == index)
764 break; /* new format */
766 if (index >= sizeof fmt_tb / sizeof fmt_tb[0])
771 return -EINVAL; /* no more format */
773 fmtdesc->pixelformat = fmt_tb[index];
774 if (gspca_is_compressed(fmt_tb[index]))
775 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
776 fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
777 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
778 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
779 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
780 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
781 fmtdesc->description[4] = '\0';
785 static int vidioc_g_fmt_cap(struct file *file, void *priv,
786 struct v4l2_format *fmt)
788 struct gspca_dev *gspca_dev = priv;
790 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
792 fmt->fmt.pix.width = gspca_dev->width;
793 fmt->fmt.pix.height = gspca_dev->height;
794 fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
795 #ifdef VIDEO_ADV_DEBUG
796 if (gspca_debug & D_CONF) {
797 PDEBUG_MODE("get fmt cap",
798 fmt->fmt.pix.pixelformat,
800 fmt->fmt.pix.height);
803 fmt->fmt.pix.field = V4L2_FIELD_NONE;
804 fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
805 * fmt->fmt.pix.width / 8;
806 fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline
807 * fmt->fmt.pix.height;
808 /* (should be in the subdriver) */
809 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
810 fmt->fmt.pix.priv = 0;
814 static int try_fmt_cap(struct gspca_dev *gspca_dev,
815 struct v4l2_format *fmt)
817 int w, h, mode, mode2, frsz;
819 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
821 w = fmt->fmt.pix.width;
822 h = fmt->fmt.pix.height;
824 /* (luvcview problem) */
825 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
826 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
827 #ifdef VIDEO_ADV_DEBUG
828 if (gspca_debug & D_CONF)
829 PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
831 /* search the closest mode for width and height */
832 mode = wxh_to_mode(gspca_dev, w, h);
834 /* OK if right palette */
835 if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) {
837 /* else, search the closest mode with the same pixel format */
838 mode2 = gspca_get_mode(gspca_dev, mode,
839 fmt->fmt.pix.pixelformat);
844 /* no chance, return this mode */
845 fmt->fmt.pix.pixelformat =
846 gspca_dev->cam.cam_mode[mode].pixfmt;
847 #ifdef VIDEO_ADV_DEBUG
848 if (gspca_debug & D_CONF) {
849 PDEBUG_MODE("new format",
850 fmt->fmt.pix.pixelformat,
851 gspca_dev->cam.cam_mode[mode].width,
852 gspca_dev->cam.cam_mode[mode].height);
857 fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width;
858 fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height;
859 fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat)
860 * fmt->fmt.pix.width / 8;
861 frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height;
862 if (gspca_is_compressed(fmt->fmt.pix.pixelformat))
863 frsz = (frsz * comp_fac) / 100;
864 fmt->fmt.pix.sizeimage = frsz;
865 return mode; /* used when s_fmt */
868 static int vidioc_try_fmt_cap(struct file *file,
870 struct v4l2_format *fmt)
872 struct gspca_dev *gspca_dev = priv;
875 ret = try_fmt_cap(gspca_dev, fmt);
881 static int vidioc_s_fmt_cap(struct file *file, void *priv,
882 struct v4l2_format *fmt)
884 struct gspca_dev *gspca_dev = priv;
887 #ifdef CONFIG_VIDEO_V4L1_COMPAT
888 /* if v4l1 got JPEG */
889 if (fmt->fmt.pix.pixelformat == 0
890 && gspca_dev->streaming) {
891 fmt->fmt.pix.width = gspca_dev->width;
892 fmt->fmt.pix.height = gspca_dev->height;
893 fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
897 #ifdef VIDEO_ADV_DEBUG
898 if (gspca_debug & D_CONF) {
899 PDEBUG_MODE("set fmt cap",
900 fmt->fmt.pix.pixelformat,
901 fmt->fmt.pix.width, fmt->fmt.pix.height);
904 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
907 ret = try_fmt_cap(gspca_dev, fmt);
911 if (gspca_dev->nframes != 0
912 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
917 if (ret == gspca_dev->curr_mode) {
919 goto out; /* same mode */
922 if (gspca_dev->streaming) {
926 gspca_dev->width = fmt->fmt.pix.width;
927 gspca_dev->height = fmt->fmt.pix.height;
928 gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
929 gspca_dev->curr_mode = ret;
933 mutex_unlock(&gspca_dev->queue_lock);
937 static int dev_open(struct inode *inode, struct file *file)
939 struct gspca_dev *gspca_dev;
942 PDEBUG(D_STREAM, "%s open", current->comm);
943 gspca_dev = (struct gspca_dev *) video_devdata(file);
944 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
946 if (!gspca_dev->present) {
951 /* if not done yet, initialize the sensor */
952 if (gspca_dev->users == 0) {
953 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
957 ret = gspca_dev->sd_desc->open(gspca_dev);
958 mutex_unlock(&gspca_dev->usb_lock);
960 PDEBUG(D_ERR|D_CONF, "init device failed %d", ret);
963 } else if (gspca_dev->users > 4) { /* (arbitrary value) */
968 file->private_data = gspca_dev;
969 #ifdef VIDEO_ADV_DEBUG
970 /* activate the v4l2 debug */
971 if (gspca_debug & D_V4L2)
972 gspca_dev->vdev.debug |= 3;
974 gspca_dev->vdev.debug &= ~3;
977 mutex_unlock(&gspca_dev->queue_lock);
979 PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
981 PDEBUG(D_STREAM, "open done");
985 static int dev_close(struct inode *inode, struct file *file)
987 struct gspca_dev *gspca_dev = file->private_data;
989 PDEBUG(D_STREAM, "%s close", current->comm);
990 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
994 /* if the file did capture, free the streaming resources */
995 if (gspca_dev->capt_file == file) {
996 mutex_lock(&gspca_dev->usb_lock);
997 if (gspca_dev->streaming)
998 gspca_stream_off(gspca_dev);
999 gspca_dev->sd_desc->close(gspca_dev);
1000 mutex_unlock(&gspca_dev->usb_lock);
1001 frame_free(gspca_dev);
1002 gspca_dev->capt_file = 0;
1003 gspca_dev->memory = GSPCA_MEMORY_NO;
1005 file->private_data = NULL;
1006 mutex_unlock(&gspca_dev->queue_lock);
1007 PDEBUG(D_STREAM, "close done");
1011 static int vidioc_querycap(struct file *file, void *priv,
1012 struct v4l2_capability *cap)
1014 struct gspca_dev *gspca_dev = priv;
1016 PDEBUG(D_CONF, "querycap");
1017 memset(cap, 0, sizeof *cap);
1018 strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver);
1019 strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card);
1020 strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name,
1021 sizeof cap->bus_info);
1022 cap->version = DRIVER_VERSION_NUMBER;
1023 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
1024 | V4L2_CAP_STREAMING
1025 | V4L2_CAP_READWRITE;
1029 /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
1030 static int vidioc_queryctrl(struct file *file, void *priv,
1031 struct v4l2_queryctrl *q_ctrl)
1033 struct gspca_dev *gspca_dev = priv;
1038 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1039 id &= V4L2_CTRL_ID_MASK;
1041 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1042 if (id >= gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1044 &gspca_dev->sd_desc->ctrls[i].qctrl,
1051 for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) {
1052 if (id == gspca_dev->sd_desc->ctrls[i].qctrl.id) {
1054 &gspca_dev->sd_desc->ctrls[i].qctrl,
1059 if (id >= V4L2_CID_BASE
1060 && id <= V4L2_CID_LASTP1) {
1061 q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1067 static int vidioc_s_ctrl(struct file *file, void *priv,
1068 struct v4l2_control *ctrl)
1070 struct gspca_dev *gspca_dev = priv;
1074 PDEBUG(D_CONF, "set ctrl");
1075 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1076 i < gspca_dev->sd_desc->nctrls;
1078 if (ctrl->id != ctrls->qctrl.id)
1080 if (ctrl->value < ctrls->qctrl.minimum
1081 && ctrl->value > ctrls->qctrl.maximum)
1083 PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value);
1084 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1085 return -ERESTARTSYS;
1086 ret = ctrls->set(gspca_dev, ctrl->value);
1087 mutex_unlock(&gspca_dev->usb_lock);
1093 static int vidioc_g_ctrl(struct file *file, void *priv,
1094 struct v4l2_control *ctrl)
1096 struct gspca_dev *gspca_dev = priv;
1101 for (i = 0, ctrls = gspca_dev->sd_desc->ctrls;
1102 i < gspca_dev->sd_desc->nctrls;
1104 if (ctrl->id != ctrls->qctrl.id)
1106 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1107 return -ERESTARTSYS;
1108 ret = ctrls->get(gspca_dev, &ctrl->value);
1109 mutex_unlock(&gspca_dev->usb_lock);
1115 static int vidioc_querymenu(struct file *file, void *priv,
1116 struct v4l2_querymenu *qmenu)
1118 struct gspca_dev *gspca_dev = priv;
1120 if (!gspca_dev->sd_desc->querymenu)
1122 return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu);
1125 static int vidioc_enum_input(struct file *file, void *priv,
1126 struct v4l2_input *input)
1128 struct gspca_dev *gspca_dev = priv;
1130 if (input->index != 0)
1132 memset(input, 0, sizeof *input);
1133 input->type = V4L2_INPUT_TYPE_CAMERA;
1134 strncpy(input->name, gspca_dev->sd_desc->name,
1135 sizeof input->name);
1139 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1145 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1152 static int vidioc_reqbufs(struct file *file, void *priv,
1153 struct v4l2_requestbuffers *rb)
1155 struct gspca_dev *gspca_dev = priv;
1158 PDEBUG(D_STREAM, "reqbufs %d", rb->count);
1159 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1161 switch (rb->memory) {
1162 case V4L2_MEMORY_MMAP:
1163 case V4L2_MEMORY_USERPTR:
1168 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1169 return -ERESTARTSYS;
1171 for (i = 0; i < gspca_dev->nframes; i++) {
1172 if (gspca_dev->frame[i].vma_use_count) {
1178 /* only one file may do capture */
1179 if ((gspca_dev->capt_file != 0 && gspca_dev->capt_file != file)
1180 || gspca_dev->streaming) {
1185 if (rb->count == 0) { /* unrequest? */
1186 frame_free(gspca_dev);
1187 gspca_dev->capt_file = 0;
1189 gspca_dev->memory = rb->memory;
1190 ret = frame_alloc(gspca_dev, rb->count);
1192 rb->count = gspca_dev->nframes;
1193 gspca_dev->capt_file = file;
1197 mutex_unlock(&gspca_dev->queue_lock);
1198 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1202 static int vidioc_querybuf(struct file *file, void *priv,
1203 struct v4l2_buffer *v4l2_buf)
1205 struct gspca_dev *gspca_dev = priv;
1206 struct gspca_frame *frame;
1208 PDEBUG(D_STREAM, "querybuf");
1209 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1210 || v4l2_buf->index < 0
1211 || v4l2_buf->index >= gspca_dev->nframes)
1214 frame = &gspca_dev->frame[v4l2_buf->index];
1215 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1219 static int vidioc_streamon(struct file *file, void *priv,
1220 enum v4l2_buf_type buf_type)
1222 struct gspca_dev *gspca_dev = priv;
1225 PDEBUG(D_STREAM, "stream on");
1226 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1228 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1229 return -ERESTARTSYS;
1230 if (!gspca_dev->present) {
1234 if (gspca_dev->nframes == 0) {
1238 if (gspca_dev->capt_file != file) {
1242 if (!gspca_dev->streaming) {
1243 ret = gspca_init_transfer(gspca_dev);
1247 #ifdef VIDEO_ADV_DEBUG
1248 if (gspca_debug & D_STREAM) {
1249 PDEBUG_MODE("stream on OK",
1257 mutex_unlock(&gspca_dev->queue_lock);
1261 static int vidioc_streamoff(struct file *file, void *priv,
1262 enum v4l2_buf_type buf_type)
1264 struct gspca_dev *gspca_dev = priv;
1267 PDEBUG(D_STREAM, "stream off");
1268 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1270 if (!gspca_dev->streaming)
1272 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1273 return -ERESTARTSYS;
1274 if (mutex_lock_interruptible(&gspca_dev->usb_lock)) {
1278 if (gspca_dev->capt_file != file) {
1282 gspca_stream_off(gspca_dev);
1285 mutex_unlock(&gspca_dev->usb_lock);
1287 mutex_unlock(&gspca_dev->queue_lock);
1291 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1292 struct v4l2_jpegcompression *jpegcomp)
1294 struct gspca_dev *gspca_dev = priv;
1297 if (!gspca_dev->sd_desc->get_jcomp)
1299 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1300 return -ERESTARTSYS;
1301 ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1302 mutex_unlock(&gspca_dev->usb_lock);
1306 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1307 struct v4l2_jpegcompression *jpegcomp)
1309 struct gspca_dev *gspca_dev = priv;
1312 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1313 return -ERESTARTSYS;
1314 if (!gspca_dev->sd_desc->set_jcomp)
1316 ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1317 mutex_unlock(&gspca_dev->usb_lock);
1321 static int vidioc_g_parm(struct file *filp, void *priv,
1322 struct v4l2_streamparm *parm)
1324 struct gspca_dev *gspca_dev = priv;
1326 memset(parm, 0, sizeof *parm);
1327 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1328 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1332 static int vidioc_s_parm(struct file *filp, void *priv,
1333 struct v4l2_streamparm *parm)
1335 struct gspca_dev *gspca_dev = priv;
1338 n = parm->parm.capture.readbuffers;
1339 if (n == 0 || n > GSPCA_MAX_FRAMES)
1340 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1342 gspca_dev->nbufread = n;
1346 static int vidioc_s_std(struct file *filp, void *priv,
1352 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1353 static int vidiocgmbuf(struct file *file, void *priv,
1354 struct video_mbuf *mbuf)
1356 struct gspca_dev *gspca_dev = file->private_data;
1359 PDEBUG(D_STREAM, "cgmbuf");
1360 if (gspca_dev->nframes == 0) {
1364 struct v4l2_format fmt;
1366 memset(&fmt, 0, sizeof fmt);
1367 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1368 i = gspca_dev->cam.nmodes - 1; /* highest mode */
1369 fmt.fmt.pix.width = gspca_dev->cam.cam_mode[i].width;
1370 fmt.fmt.pix.height = gspca_dev->cam.cam_mode[i].height;
1371 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
1372 ret = vidioc_s_fmt_cap(file, priv, &fmt);
1377 struct v4l2_requestbuffers rb;
1379 memset(&rb, 0, sizeof rb);
1381 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1382 rb.memory = V4L2_MEMORY_MMAP;
1383 ret = vidioc_reqbufs(file, priv, &rb);
1388 mbuf->frames = gspca_dev->nframes;
1389 mbuf->size = gspca_dev->frsz * gspca_dev->nframes;
1390 for (i = 0; i < mbuf->frames; i++)
1391 mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset;
1396 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1398 struct gspca_dev *gspca_dev = file->private_data;
1399 struct gspca_frame *frame = 0;
1401 unsigned long addr, start, size;
1403 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1407 start = vma->vm_start;
1408 size = vma->vm_end - vma->vm_start;
1409 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1411 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1412 return -ERESTARTSYS;
1413 if (!gspca_dev->present) {
1417 if (gspca_dev->capt_file != file) {
1422 for (i = 0; i < gspca_dev->nframes; ++i) {
1423 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1424 PDEBUG(D_STREAM, "mmap bad memory type");
1427 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1429 frame = &gspca_dev->frame[i];
1434 PDEBUG(D_STREAM, "mmap no frame buffer found");
1438 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1439 if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes)
1443 if (size != frame->v4l2_buf.length) {
1444 PDEBUG(D_STREAM, "mmap bad size");
1450 * - VM_IO marks the area as being a mmaped region for I/O to a
1451 * device. It also prevents the region from being core dumped.
1453 vma->vm_flags |= VM_IO;
1455 addr = (unsigned long) frame->data;
1457 page = vmalloc_to_page((void *) addr);
1458 ret = vm_insert_page(vma, start, page);
1466 vma->vm_ops = &gspca_vm_ops;
1467 vma->vm_private_data = frame;
1469 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1472 for (i = 1; i < gspca_dev->nframes; ++i)
1473 gspca_dev->frame[i].v4l2_buf.flags |=
1474 V4L2_BUF_FLAG_MAPPED;
1479 mutex_unlock(&gspca_dev->queue_lock);
1484 * wait for a video frame
1486 * If a frame is ready, its index is returned.
1488 static int frame_wait(struct gspca_dev *gspca_dev,
1491 struct gspca_frame *frame;
1494 /* if userptr, treat the awaiting URBs */
1495 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1496 isoc_transfer(gspca_dev);
1498 /* check if a frame is ready */
1499 i = gspca_dev->fr_o;
1500 j = gspca_dev->fr_queue[i];
1501 frame = &gspca_dev->frame[j];
1502 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1504 if (nonblock_ing) /* no frame yet */
1507 /* wait till a frame is ready */
1509 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1510 atomic_read(&gspca_dev->nevent) > 0,
1511 msecs_to_jiffies(3000));
1517 if (!gspca_dev->streaming || !gspca_dev->present)
1519 if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
1520 isoc_transfer(gspca_dev);
1521 i = gspca_dev->fr_o;
1522 j = gspca_dev->fr_queue[i];
1523 frame = &gspca_dev->frame[j];
1524 if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1528 atomic_dec(&gspca_dev->nevent);
1529 gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
1530 PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d",
1535 if (gspca_dev->sd_desc->dq_callback)
1536 gspca_dev->sd_desc->dq_callback(gspca_dev);
1542 * dequeue a video buffer
1544 * If nonblock_ing is false, block until a buffer is available.
1546 static int vidioc_dqbuf(struct file *file, void *priv,
1547 struct v4l2_buffer *v4l2_buf)
1549 struct gspca_dev *gspca_dev = priv;
1550 struct gspca_frame *frame;
1553 PDEBUG(D_FRAM, "dqbuf");
1554 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1555 || (v4l2_buf->memory != V4L2_MEMORY_MMAP
1556 && v4l2_buf->memory != V4L2_MEMORY_USERPTR))
1558 if (!gspca_dev->streaming)
1560 if (gspca_dev->capt_file != file) {
1566 if (mutex_lock_interruptible(&gspca_dev->read_lock))
1567 return -ERESTARTSYS;
1569 ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
1572 i = ret; /* frame index */
1573 frame = &gspca_dev->frame[i];
1574 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1575 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1576 PDEBUG(D_FRAM, "dqbuf %d", i);
1579 mutex_unlock(&gspca_dev->read_lock);
1584 * queue a video buffer
1586 * Attempting to queue a buffer that has already been
1587 * queued will return -EINVAL.
1589 static int vidioc_qbuf(struct file *file, void *priv,
1590 struct v4l2_buffer *v4l2_buf)
1592 struct gspca_dev *gspca_dev = priv;
1593 struct gspca_frame *frame;
1596 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1597 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1600 index = v4l2_buf->index;
1601 if ((unsigned) index >= gspca_dev->nframes) {
1603 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1606 frame = &gspca_dev->frame[index];
1608 if (v4l2_buf->memory != frame->v4l2_buf.memory) {
1609 PDEBUG(D_FRAM, "qbuf bad memory type");
1612 if (gspca_dev->capt_file != file)
1615 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1616 return -ERESTARTSYS;
1618 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1619 PDEBUG(D_FRAM, "qbuf bad state");
1624 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1625 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1627 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1628 frame->data = frame->data_end =
1629 (__u8 *) v4l2_buf->m.userptr;
1630 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1631 frame->v4l2_buf.length = v4l2_buf->length;
1634 /* put the buffer in the 'queued' queue */
1635 i = gspca_dev->fr_q;
1636 gspca_dev->fr_queue[i] = index;
1637 gspca_dev->fr_q = (i + 1) % gspca_dev->nframes;
1638 PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d",
1643 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1644 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1647 mutex_unlock(&gspca_dev->queue_lock);
1652 * allocate the resources for read()
1654 static int read_alloc(struct gspca_dev *gspca_dev,
1657 struct v4l2_buffer v4l2_buf;
1660 PDEBUG(D_STREAM, "read alloc");
1661 if (gspca_dev->nframes == 0) {
1662 struct v4l2_requestbuffers rb;
1664 memset(&rb, 0, sizeof rb);
1665 rb.count = gspca_dev->nbufread;
1666 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1667 rb.memory = V4L2_MEMORY_MMAP;
1668 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1670 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1673 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1674 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1676 for (i = 0; i < gspca_dev->nbufread; i++) {
1679 gspca_dev->frame[i].v4l2_buf.flags |=
1680 V4L2_BUF_FLAG_MAPPED;
1681 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1683 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1687 gspca_dev->memory = GSPCA_MEMORY_READ;
1690 /* start streaming */
1691 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1693 PDEBUG(D_STREAM, "read streamon err %d", ret);
1697 static unsigned int dev_poll(struct file *file, poll_table *wait)
1699 struct gspca_dev *gspca_dev = file->private_data;
1702 PDEBUG(D_FRAM, "poll");
1704 poll_wait(file, &gspca_dev->wq, wait);
1705 if (!gspca_dev->present)
1708 /* if not streaming, the user would use read() */
1709 if (!gspca_dev->streaming) {
1710 if (gspca_dev->memory != GSPCA_MEMORY_NO) {
1711 ret = POLLERR; /* not the 1st time */
1714 ret = read_alloc(gspca_dev, file);
1721 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
1723 if (!gspca_dev->present) {
1728 /* if not mmap, treat the awaiting URBs */
1729 if (gspca_dev->memory == V4L2_MEMORY_USERPTR
1730 && gspca_dev->capt_file == file)
1731 isoc_transfer(gspca_dev);
1733 i = gspca_dev->fr_o;
1734 i = gspca_dev->fr_queue[i];
1735 if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
1736 ret = POLLIN | POLLRDNORM; /* something to read */
1740 mutex_unlock(&gspca_dev->queue_lock);
1744 static ssize_t dev_read(struct file *file, char __user *data,
1745 size_t count, loff_t *ppos)
1747 struct gspca_dev *gspca_dev = file->private_data;
1748 struct gspca_frame *frame;
1749 struct v4l2_buffer v4l2_buf;
1750 struct timeval timestamp;
1753 PDEBUG(D_FRAM, "read (%d)", count);
1754 if (!gspca_dev->present)
1756 switch (gspca_dev->memory) {
1757 case GSPCA_MEMORY_NO: /* first time */
1758 ret = read_alloc(gspca_dev, file);
1762 case GSPCA_MEMORY_READ:
1763 if (gspca_dev->capt_file != file)
1771 jiffies_to_timeval(get_jiffies_64(), ×tamp);
1773 for (i = 0; i < 2; i++) {
1774 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1775 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1776 v4l2_buf.memory = V4L2_MEMORY_MMAP;
1777 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1779 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1783 /* if the process slept for more than 1 second,
1784 * get a brand new frame */
1785 frame = &gspca_dev->frame[v4l2_buf.index];
1786 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1788 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1790 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1795 /* copy the frame */
1796 if (count < frame->v4l2_buf.bytesused) {
1797 PDEBUG(D_STREAM, "read bad count: %d < %d",
1798 count, frame->v4l2_buf.bytesused);
1799 /*fixme: special errno?*/
1803 count = frame->v4l2_buf.bytesused;
1804 ret = copy_to_user(data, frame->data, count);
1806 PDEBUG(D_ERR|D_STREAM,
1807 "read cp to user lack %d / %d", ret, count);
1813 /* in each case, requeue the buffer */
1814 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1820 static void dev_release(struct video_device *vfd)
1825 static struct file_operations dev_fops = {
1826 .owner = THIS_MODULE,
1828 .release = dev_close,
1831 .ioctl = video_ioctl2,
1832 #ifdef CONFIG_COMPAT
1833 .compat_ioctl = v4l_compat_ioctl32,
1835 .llseek = no_llseek,
1839 static struct video_device gspca_template = {
1840 .name = "gspca main driver",
1841 .type = VID_TYPE_CAPTURE,
1843 .release = dev_release, /* mandatory */
1845 .vidioc_querycap = vidioc_querycap,
1846 .vidioc_dqbuf = vidioc_dqbuf,
1847 .vidioc_qbuf = vidioc_qbuf,
1848 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1849 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1850 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1851 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1852 .vidioc_streamon = vidioc_streamon,
1853 .vidioc_queryctrl = vidioc_queryctrl,
1854 .vidioc_g_ctrl = vidioc_g_ctrl,
1855 .vidioc_s_ctrl = vidioc_s_ctrl,
1856 .vidioc_querymenu = vidioc_querymenu,
1857 .vidioc_enum_input = vidioc_enum_input,
1858 .vidioc_g_input = vidioc_g_input,
1859 .vidioc_s_input = vidioc_s_input,
1860 .vidioc_reqbufs = vidioc_reqbufs,
1861 .vidioc_querybuf = vidioc_querybuf,
1862 .vidioc_streamoff = vidioc_streamoff,
1863 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1864 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1865 .vidioc_g_parm = vidioc_g_parm,
1866 .vidioc_s_parm = vidioc_s_parm,
1867 .vidioc_s_std = vidioc_s_std,
1868 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1869 .vidiocgmbuf = vidiocgmbuf,
1874 * probe and create a new gspca device
1876 * This function must be called by the sub-driver when it is
1877 * called for probing a new device.
1879 int gspca_dev_probe(struct usb_interface *intf,
1880 const struct usb_device_id *id,
1881 const struct sd_desc *sd_desc,
1883 struct module *module)
1885 struct usb_interface_descriptor *interface;
1886 struct gspca_dev *gspca_dev;
1887 struct usb_device *dev = interface_to_usbdev(intf);
1890 PDEBUG(D_PROBE, "probing %04x:%04x", id->idVendor, id->idProduct);
1892 /* we don't handle multi-config cameras */
1893 if (dev->descriptor.bNumConfigurations != 1)
1895 interface = &intf->cur_altsetting->desc;
1896 if (interface->bInterfaceNumber > 0)
1899 /* create the device */
1900 if (dev_size < sizeof *gspca_dev)
1901 dev_size = sizeof *gspca_dev;
1902 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1903 if (gspca_dev == NULL) {
1904 err("couldn't kzalloc gspca struct");
1907 gspca_dev->dev = dev;
1908 gspca_dev->iface = interface->bInterfaceNumber;
1909 gspca_dev->nbalt = intf->num_altsetting;
1910 gspca_dev->sd_desc = sd_desc;
1911 /* gspca_dev->users = 0; (done by kzalloc) */
1912 gspca_dev->nbufread = 2;
1914 /* configure the subdriver */
1915 ret = gspca_dev->sd_desc->config(gspca_dev, id);
1918 ret = gspca_set_alt0(gspca_dev);
1921 gspca_set_default_mode(gspca_dev);
1923 mutex_init(&gspca_dev->usb_lock);
1924 mutex_init(&gspca_dev->read_lock);
1925 mutex_init(&gspca_dev->queue_lock);
1926 init_waitqueue_head(&gspca_dev->wq);
1928 /* init video stuff */
1929 memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
1930 gspca_dev->vdev.dev = &dev->dev;
1931 memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
1932 gspca_dev->vdev.fops = &gspca_dev->fops;
1933 gspca_dev->fops.owner = module; /* module protection */
1934 ret = video_register_device(&gspca_dev->vdev,
1938 err("video_register_device err %d", ret);
1942 gspca_dev->present = 1;
1943 usb_set_intfdata(intf, gspca_dev);
1944 PDEBUG(D_PROBE, "probe ok");
1950 EXPORT_SYMBOL(gspca_dev_probe);
1955 * This function must be called by the sub-driver
1956 * when the device disconnects, after the specific resources are freed.
1958 void gspca_disconnect(struct usb_interface *intf)
1960 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1964 gspca_dev->present = 0;
1965 mutex_lock(&gspca_dev->queue_lock);
1966 mutex_lock(&gspca_dev->usb_lock);
1967 gspca_dev->streaming = 0;
1968 destroy_urbs(gspca_dev);
1969 mutex_unlock(&gspca_dev->usb_lock);
1970 mutex_unlock(&gspca_dev->queue_lock);
1971 while (gspca_dev->users != 0) { /* wait until fully closed */
1972 atomic_inc(&gspca_dev->nevent);
1973 wake_up_interruptible(&gspca_dev->wq); /* wake processes */
1976 /* We don't want people trying to open up the device */
1977 video_unregister_device(&gspca_dev->vdev);
1978 /* Free the memory */
1980 PDEBUG(D_PROBE, "disconnect complete");
1982 EXPORT_SYMBOL(gspca_disconnect);
1984 /* -- module insert / remove -- */
1985 static int __init gspca_init(void)
1987 info("main v%s registered", version);
1990 static void __exit gspca_exit(void)
1992 info("main deregistered");
1995 module_init(gspca_init);
1996 module_exit(gspca_exit);
1998 #ifdef VIDEO_ADV_DEBUG
1999 module_param_named(debug, gspca_debug, int, 0644);
2000 MODULE_PARM_DESC(debug,
2001 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2002 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
2005 module_param(comp_fac, int, 0644);
2006 MODULE_PARM_DESC(comp_fac,
2007 "Buffer size ratio when compressed in percent");