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>
24 #include <linux/version.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/pagemap.h>
34 #include <linux/uaccess.h>
35 #include <linux/jiffies.h>
36 #include <media/v4l2-ioctl.h>
41 #define DEF_NURBS 2 /* default number of URBs */
43 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
44 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
45 MODULE_LICENSE("GPL");
47 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 4, 0)
49 static int video_nr
= -1;
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",
62 (pixfmt
>> 16) & 0xff,
66 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
73 #define PDEBUG_MODE(txt, pixfmt, w, h)
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)
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
,
106 /* get the current input frame buffer */
107 struct gspca_frame
*gspca_get_i_frame(struct gspca_dev
*gspca_dev
)
109 struct gspca_frame
*frame
;
113 i
= gspca_dev
->fr_queue
[i
];
114 frame
= &gspca_dev
->frame
[i
];
115 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
116 != V4L2_BUF_FLAG_QUEUED
)
120 EXPORT_SYMBOL(gspca_get_i_frame
);
123 * fill a video frame from an URB and resubmit
125 static void fill_frame(struct gspca_dev
*gspca_dev
,
128 struct gspca_frame
*frame
;
129 __u8
*data
; /* address of data in the iso message */
133 if (urb
->status
!= 0) {
135 if (!gspca_dev
->frozen
)
137 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
138 return; /* disconnection ? */
140 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
141 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
143 /* check the availability of the frame buffer */
144 frame
= gspca_get_i_frame(gspca_dev
);
146 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
150 /* check the packet status and length */
151 len
= urb
->iso_frame_desc
[i
].actual_length
;
153 if (gspca_dev
->empty_packet
== 0)
154 gspca_dev
->empty_packet
= 1;
157 st
= urb
->iso_frame_desc
[i
].status
;
160 "ISOC data error: [%d] len=%d, status=%d",
162 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
166 /* let the packet be analyzed by the subdriver */
167 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
168 i
, urb
->iso_frame_desc
[i
].offset
, len
);
169 data
= (__u8
*) urb
->transfer_buffer
170 + urb
->iso_frame_desc
[i
].offset
;
171 pkt_scan(gspca_dev
, frame
, data
, len
);
174 /* resubmit the URB */
175 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
177 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
181 * ISOC message interrupt from the USB device
183 * Analyse each packet and call the subdriver for copy to the frame buffer.
185 static void isoc_irq(struct urb
*urb
188 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
190 PDEBUG(D_PACK
, "isoc irq");
191 if (!gspca_dev
->streaming
)
193 fill_frame(gspca_dev
, urb
);
197 * bulk message interrupt from the USB device
199 static void bulk_irq(struct urb
*urb
202 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
203 struct gspca_frame
*frame
;
206 PDEBUG(D_PACK
, "bulk irq");
207 if (!gspca_dev
->streaming
)
209 switch (urb
->status
) {
217 if (!gspca_dev
->frozen
)
219 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
220 return; /* disconnection ? */
223 /* check the availability of the frame buffer */
224 frame
= gspca_get_i_frame(gspca_dev
);
226 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
228 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
229 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
231 urb
->transfer_buffer
,
235 /* resubmit the URB */
236 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
237 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
239 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
244 * add data to the current frame
246 * This function is called by the subdrivers at interrupt level.
248 * To build a frame, these ones must add
250 * - 0 or many INTER_PACKETs
252 * DISCARD_PACKET invalidates the whole frame.
253 * On LAST_PACKET, a new frame is returned.
255 struct gspca_frame
*gspca_frame_add(struct gspca_dev
*gspca_dev
,
256 enum gspca_packet_type packet_type
,
257 struct gspca_frame
*frame
,
263 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
265 /* when start of a new frame, if the current frame buffer
266 * is not queued, discard the whole frame */
267 if (packet_type
== FIRST_PACKET
) {
268 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
269 != V4L2_BUF_FLAG_QUEUED
) {
270 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
273 frame
->data_end
= frame
->data
;
274 jiffies_to_timeval(get_jiffies_64(),
275 &frame
->v4l2_buf
.timestamp
);
276 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
277 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
278 if (packet_type
== LAST_PACKET
)
279 gspca_dev
->last_packet_type
= packet_type
;
283 /* append the packet to the frame buffer */
285 if (frame
->data_end
- frame
->data
+ len
286 > frame
->v4l2_buf
.length
) {
287 PDEBUG(D_ERR
|D_PACK
, "frame overflow %zd > %d",
288 frame
->data_end
- frame
->data
+ len
,
289 frame
->v4l2_buf
.length
);
290 packet_type
= DISCARD_PACKET
;
292 memcpy(frame
->data_end
, data
, len
);
293 frame
->data_end
+= len
;
296 gspca_dev
->last_packet_type
= packet_type
;
298 /* if last packet, wake up the application and advance in the queue */
299 if (packet_type
== LAST_PACKET
) {
300 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
301 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
302 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
303 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
304 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
306 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
307 frame
->v4l2_buf
.bytesused
,
311 j
= gspca_dev
->fr_queue
[i
];
312 frame
= &gspca_dev
->frame
[j
];
316 EXPORT_SYMBOL(gspca_frame_add
);
318 static int gspca_is_compressed(__u32 format
)
321 case V4L2_PIX_FMT_MJPEG
:
322 case V4L2_PIX_FMT_JPEG
:
323 case V4L2_PIX_FMT_SPCA561
:
324 case V4L2_PIX_FMT_PAC207
:
330 static void *rvmalloc(unsigned long size
)
335 mem
= vmalloc_32(size
);
337 adr
= (unsigned long) mem
;
338 while ((long) size
> 0) {
339 SetPageReserved(vmalloc_to_page((void *) adr
));
347 static void rvfree(void *mem
, long size
)
351 adr
= (unsigned long) mem
;
353 ClearPageReserved(vmalloc_to_page((void *) adr
));
360 static int frame_alloc(struct gspca_dev
*gspca_dev
,
363 struct gspca_frame
*frame
;
367 i
= gspca_dev
->curr_mode
;
368 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
369 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
370 frsz
= PAGE_ALIGN(frsz
);
371 gspca_dev
->frsz
= frsz
;
372 if (count
> GSPCA_MAX_FRAMES
)
373 count
= GSPCA_MAX_FRAMES
;
374 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
375 if (!gspca_dev
->frbuf
) {
376 err("frame alloc failed");
379 gspca_dev
->nframes
= count
;
380 for (i
= 0; i
< count
; i
++) {
381 frame
= &gspca_dev
->frame
[i
];
382 frame
->v4l2_buf
.index
= i
;
383 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
384 frame
->v4l2_buf
.flags
= 0;
385 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
386 frame
->v4l2_buf
.length
= frsz
;
387 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
388 frame
->v4l2_buf
.sequence
= 0;
389 frame
->data
= frame
->data_end
=
390 gspca_dev
->frbuf
+ i
* frsz
;
391 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
393 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
394 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
395 gspca_dev
->sequence
= 0;
399 static void frame_free(struct gspca_dev
*gspca_dev
)
403 PDEBUG(D_STREAM
, "frame free");
404 if (gspca_dev
->frbuf
!= NULL
) {
405 rvfree(gspca_dev
->frbuf
,
406 gspca_dev
->nframes
* gspca_dev
->frsz
);
407 gspca_dev
->frbuf
= NULL
;
408 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
409 gspca_dev
->frame
[i
].data
= NULL
;
411 gspca_dev
->nframes
= 0;
414 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
419 PDEBUG(D_STREAM
, "kill transfer");
420 for (i
= 0; i
< MAX_NURBS
; i
++) {
421 urb
= gspca_dev
->urb
[i
];
425 gspca_dev
->urb
[i
] = NULL
;
427 if (urb
->transfer_buffer
!= NULL
)
428 usb_buffer_free(gspca_dev
->dev
,
429 urb
->transfer_buffer_length
,
430 urb
->transfer_buffer
,
437 * look for an input transfer endpoint in an alternate setting
439 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
443 struct usb_host_endpoint
*ep
;
446 epaddr
|= USB_DIR_IN
;
447 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
448 ep
= &alt
->endpoint
[i
];
449 if (ep
->desc
.bEndpointAddress
== epaddr
) {
450 attr
= ep
->desc
.bmAttributes
451 & USB_ENDPOINT_XFERTYPE_MASK
;
461 * look for an input (isoc or bulk) endpoint
463 * The endpoint is defined by the subdriver.
464 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
465 * This routine may be called many times when the bandwidth is too small
466 * (the bandwidth is checked on urb submit).
468 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
470 struct usb_interface
*intf
;
471 struct usb_host_endpoint
*ep
;
474 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
476 i
= gspca_dev
->alt
; /* previous alt setting */
479 while (--i
> 0) { /* alt 0 is unusable */
480 ep
= alt_xfer(&intf
->altsetting
[i
],
481 gspca_dev
->cam
.epaddr
,
482 USB_ENDPOINT_XFER_ISOC
);
487 /* if no isoc, try bulk */
489 ep
= alt_xfer(&intf
->altsetting
[0],
490 gspca_dev
->cam
.epaddr
,
491 USB_ENDPOINT_XFER_BULK
);
493 err("no transfer endpoint found");
497 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
498 i
, ep
->desc
.bEndpointAddress
);
500 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
502 err("set interface err %d", ret
);
506 gspca_dev
->alt
= i
; /* memorize the current alt setting */
511 * create the URBs for image transfer
513 static int create_urbs(struct gspca_dev
*gspca_dev
,
514 struct usb_host_endpoint
*ep
)
517 int n
, nurbs
, i
, psize
, npkt
, bsize
;
519 /* calculate the packet size and the number of packets */
520 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
522 if (gspca_dev
->alt
!= 0) { /* isoc */
524 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
525 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
526 npkt
= ISO_MAX_SIZE
/ psize
;
527 if (npkt
> ISO_MAX_PKT
)
529 bsize
= psize
* npkt
;
531 "isoc %d pkts size %d = bsize:%d",
536 bsize
= gspca_dev
->cam
.bulk_size
;
539 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
540 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
541 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
546 gspca_dev
->nurbs
= nurbs
;
547 for (n
= 0; n
< nurbs
; n
++) {
548 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
550 err("usb_alloc_urb failed");
551 destroy_urbs(gspca_dev
);
554 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
559 if (urb
->transfer_buffer
== NULL
) {
561 err("usb_buffer_urb failed");
562 destroy_urbs(gspca_dev
);
565 gspca_dev
->urb
[n
] = urb
;
566 urb
->dev
= gspca_dev
->dev
;
567 urb
->context
= gspca_dev
;
568 urb
->transfer_buffer_length
= bsize
;
569 if (npkt
!= 0) { /* ISOC */
570 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
571 ep
->desc
.bEndpointAddress
);
572 urb
->transfer_flags
= URB_ISO_ASAP
573 | URB_NO_TRANSFER_DMA_MAP
;
574 urb
->interval
= ep
->desc
.bInterval
;
575 urb
->complete
= isoc_irq
;
576 urb
->number_of_packets
= npkt
;
577 for (i
= 0; i
< npkt
; i
++) {
578 urb
->iso_frame_desc
[i
].length
= psize
;
579 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
582 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
583 ep
->desc
.bEndpointAddress
),
584 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
585 urb
->complete
= bulk_irq
;
592 * start the USB transfer
594 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
596 struct usb_host_endpoint
*ep
;
599 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
602 /* set the higher alternate setting and
603 * loop until urb submit succeeds */
604 gspca_dev
->alt
= gspca_dev
->nbalt
;
606 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
607 ep
= get_ep(gspca_dev
);
612 ret
= create_urbs(gspca_dev
, ep
);
616 /* clear the bulk endpoint */
617 if (gspca_dev
->alt
== 0) /* if bulk transfer */
618 usb_clear_halt(gspca_dev
->dev
,
619 usb_rcvintpipe(gspca_dev
->dev
,
620 gspca_dev
->cam
.epaddr
));
623 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
625 destroy_urbs(gspca_dev
);
628 gspca_dev
->streaming
= 1;
630 /* some bulk transfers are started by the subdriver */
631 if (gspca_dev
->alt
== 0 && gspca_dev
->cam
.bulk_nurbs
== 0)
634 /* submit the URBs */
635 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
636 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
638 PDEBUG(D_ERR
|D_STREAM
,
639 "usb_submit_urb [%d] err %d", n
, ret
);
640 gspca_dev
->streaming
= 0;
641 destroy_urbs(gspca_dev
);
642 if (ret
== -ENOSPC
) {
643 msleep(20); /* wait for kill
645 break; /* try the previous alt */
654 mutex_unlock(&gspca_dev
->usb_lock
);
658 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
662 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
664 PDEBUG(D_ERR
|D_STREAM
, "set alt 0 err %d", ret
);
668 /* Note: both the queue and the usb locks should be held when calling this */
669 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
671 gspca_dev
->streaming
= 0;
672 if (gspca_dev
->present
673 && gspca_dev
->sd_desc
->stopN
)
674 gspca_dev
->sd_desc
->stopN(gspca_dev
);
675 destroy_urbs(gspca_dev
);
676 gspca_set_alt0(gspca_dev
);
677 if (gspca_dev
->sd_desc
->stop0
)
678 gspca_dev
->sd_desc
->stop0(gspca_dev
);
679 PDEBUG(D_STREAM
, "stream off OK");
682 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
686 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
687 gspca_dev
->curr_mode
= i
;
688 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
689 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
690 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
693 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
694 int width
, int height
)
698 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
699 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
700 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
707 * search a mode with the right pixel format
709 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
715 modeU
= modeD
= mode
;
716 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
718 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
722 if (++modeU
< gspca_dev
->cam
.nmodes
) {
723 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
731 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
732 struct v4l2_fmtdesc
*fmtdesc
)
734 struct gspca_dev
*gspca_dev
= priv
;
738 /* give an index to each format */
741 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
742 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
745 if (fmt_tb
[j
] == fmt_tb
[index
])
750 if (fmtdesc
->index
== index
)
751 break; /* new format */
753 if (index
>= ARRAY_SIZE(fmt_tb
))
758 return -EINVAL
; /* no more format */
760 fmtdesc
->pixelformat
= fmt_tb
[index
];
761 if (gspca_is_compressed(fmt_tb
[index
]))
762 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
763 fmtdesc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
764 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
765 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
766 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
767 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
768 fmtdesc
->description
[4] = '\0';
772 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
773 struct v4l2_format
*fmt
)
775 struct gspca_dev
*gspca_dev
= priv
;
778 mode
= gspca_dev
->curr_mode
;
779 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
780 sizeof fmt
->fmt
.pix
);
784 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
785 struct v4l2_format
*fmt
)
787 int w
, h
, mode
, mode2
;
789 w
= fmt
->fmt
.pix
.width
;
790 h
= fmt
->fmt
.pix
.height
;
793 if (gspca_debug
& D_CONF
)
794 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
796 /* search the closest mode for width and height */
797 mode
= wxh_to_mode(gspca_dev
, w
, h
);
799 /* OK if right palette */
800 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
801 != fmt
->fmt
.pix
.pixelformat
) {
803 /* else, search the closest mode with the same pixel format */
804 mode2
= gspca_get_mode(gspca_dev
, mode
,
805 fmt
->fmt
.pix
.pixelformat
);
809 ; * no chance, return this mode */
811 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
812 sizeof fmt
->fmt
.pix
);
813 return mode
; /* used when s_fmt */
816 static int vidioc_try_fmt_vid_cap(struct file
*file
,
818 struct v4l2_format
*fmt
)
820 struct gspca_dev
*gspca_dev
= priv
;
823 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
829 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
830 struct v4l2_format
*fmt
)
832 struct gspca_dev
*gspca_dev
= priv
;
835 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
838 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
842 if (gspca_dev
->nframes
!= 0
843 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
848 if (ret
== gspca_dev
->curr_mode
) {
850 goto out
; /* same mode */
853 if (gspca_dev
->streaming
) {
857 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
858 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
859 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
860 gspca_dev
->curr_mode
= ret
;
864 mutex_unlock(&gspca_dev
->queue_lock
);
868 static void gspca_release(struct video_device
*vfd
)
870 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
872 PDEBUG(D_STREAM
, "device released");
874 kfree(gspca_dev
->usb_buf
);
878 static int dev_open(struct file
*file
)
880 struct gspca_dev
*gspca_dev
;
883 PDEBUG(D_STREAM
, "%s open", current
->comm
);
884 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
885 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
887 if (!gspca_dev
->present
) {
892 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
897 /* protect the subdriver against rmmod */
898 if (!try_module_get(gspca_dev
->module
)) {
905 file
->private_data
= gspca_dev
;
907 /* activate the v4l2 debug */
908 if (gspca_debug
& D_V4L2
)
909 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
910 | V4L2_DEBUG_IOCTL_ARG
;
912 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
913 | V4L2_DEBUG_IOCTL_ARG
);
917 mutex_unlock(&gspca_dev
->queue_lock
);
919 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
921 PDEBUG(D_STREAM
, "open done");
925 static int dev_close(struct file
*file
)
927 struct gspca_dev
*gspca_dev
= file
->private_data
;
929 PDEBUG(D_STREAM
, "%s close", current
->comm
);
930 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
934 /* if the file did the capture, free the streaming resources */
935 if (gspca_dev
->capt_file
== file
) {
936 if (gspca_dev
->streaming
) {
937 mutex_lock(&gspca_dev
->usb_lock
);
938 gspca_stream_off(gspca_dev
);
939 mutex_unlock(&gspca_dev
->usb_lock
);
941 frame_free(gspca_dev
);
942 gspca_dev
->capt_file
= NULL
;
943 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
945 file
->private_data
= NULL
;
946 module_put(gspca_dev
->module
);
947 mutex_unlock(&gspca_dev
->queue_lock
);
949 PDEBUG(D_STREAM
, "close done");
954 static int vidioc_querycap(struct file
*file
, void *priv
,
955 struct v4l2_capability
*cap
)
957 struct gspca_dev
*gspca_dev
= priv
;
959 memset(cap
, 0, sizeof *cap
);
960 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
961 if (gspca_dev
->dev
->product
!= NULL
) {
962 strncpy(cap
->card
, gspca_dev
->dev
->product
,
965 snprintf(cap
->card
, sizeof cap
->card
,
966 "USB Camera (%04x:%04x)",
967 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
968 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
970 strncpy(cap
->bus_info
, gspca_dev
->dev
->bus
->bus_name
,
971 sizeof cap
->bus_info
);
972 cap
->version
= DRIVER_VERSION_NUMBER
;
973 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
975 | V4L2_CAP_READWRITE
;
979 static int vidioc_queryctrl(struct file
*file
, void *priv
,
980 struct v4l2_queryctrl
*q_ctrl
)
982 struct gspca_dev
*gspca_dev
= priv
;
988 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
989 id
&= V4L2_CTRL_ID_MASK
;
991 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
992 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
998 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
999 > gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
.id
)
1004 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1005 if (id
== gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
1012 memcpy(q_ctrl
, &gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
,
1014 if (gspca_dev
->ctrl_dis
& (1 << ix
))
1015 q_ctrl
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1019 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1020 struct v4l2_control
*ctrl
)
1022 struct gspca_dev
*gspca_dev
= priv
;
1023 const struct ctrl
*ctrls
;
1026 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1027 i
< gspca_dev
->sd_desc
->nctrls
;
1029 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1031 if (gspca_dev
->ctrl_dis
& (1 << i
))
1033 if (ctrl
->value
< ctrls
->qctrl
.minimum
1034 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1036 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1037 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1038 return -ERESTARTSYS
;
1039 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1040 mutex_unlock(&gspca_dev
->usb_lock
);
1046 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1047 struct v4l2_control
*ctrl
)
1049 struct gspca_dev
*gspca_dev
= priv
;
1051 const struct ctrl
*ctrls
;
1054 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1055 i
< gspca_dev
->sd_desc
->nctrls
;
1057 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1059 if (gspca_dev
->ctrl_dis
& (1 << i
))
1061 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1062 return -ERESTARTSYS
;
1063 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1064 mutex_unlock(&gspca_dev
->usb_lock
);
1070 /*fixme: have an audio flag in gspca_dev?*/
1071 static int vidioc_s_audio(struct file
*file
, void *priv
,
1072 struct v4l2_audio
*audio
)
1074 if (audio
->index
!= 0)
1079 static int vidioc_g_audio(struct file
*file
, void *priv
,
1080 struct v4l2_audio
*audio
)
1082 memset(audio
, 0, sizeof *audio
);
1083 strcpy(audio
->name
, "Microphone");
1087 static int vidioc_enumaudio(struct file
*file
, void *priv
,
1088 struct v4l2_audio
*audio
)
1090 if (audio
->index
!= 0)
1093 strcpy(audio
->name
, "Microphone");
1094 audio
->capability
= 0;
1099 static int vidioc_querymenu(struct file
*file
, void *priv
,
1100 struct v4l2_querymenu
*qmenu
)
1102 struct gspca_dev
*gspca_dev
= priv
;
1104 if (!gspca_dev
->sd_desc
->querymenu
)
1106 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1109 static int vidioc_enum_input(struct file
*file
, void *priv
,
1110 struct v4l2_input
*input
)
1112 struct gspca_dev
*gspca_dev
= priv
;
1114 if (input
->index
!= 0)
1116 memset(input
, 0, sizeof *input
);
1117 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1118 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1119 sizeof input
->name
);
1123 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1129 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1136 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1137 struct v4l2_requestbuffers
*rb
)
1139 struct gspca_dev
*gspca_dev
= priv
;
1142 switch (rb
->memory
) {
1143 case GSPCA_MEMORY_READ
: /* (internal call) */
1144 case V4L2_MEMORY_MMAP
:
1145 case V4L2_MEMORY_USERPTR
:
1150 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1151 return -ERESTARTSYS
;
1153 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1154 && gspca_dev
->memory
!= rb
->memory
) {
1159 /* only one file may do the capture */
1160 if (gspca_dev
->capt_file
!= NULL
1161 && gspca_dev
->capt_file
!= file
) {
1166 /* if allocated, the buffers must not be mapped */
1167 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1168 if (gspca_dev
->frame
[i
].vma_use_count
) {
1174 /* stop streaming */
1175 if (gspca_dev
->streaming
) {
1176 mutex_lock(&gspca_dev
->usb_lock
);
1177 gspca_stream_off(gspca_dev
);
1178 mutex_unlock(&gspca_dev
->usb_lock
);
1181 /* free the previous allocated buffers, if any */
1182 if (gspca_dev
->nframes
!= 0) {
1183 frame_free(gspca_dev
);
1184 gspca_dev
->capt_file
= NULL
;
1186 if (rb
->count
== 0) /* unrequest */
1188 gspca_dev
->memory
= rb
->memory
;
1189 ret
= frame_alloc(gspca_dev
, rb
->count
);
1191 rb
->count
= gspca_dev
->nframes
;
1192 gspca_dev
->capt_file
= file
;
1195 mutex_unlock(&gspca_dev
->queue_lock
);
1196 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1200 static int vidioc_querybuf(struct file
*file
, void *priv
,
1201 struct v4l2_buffer
*v4l2_buf
)
1203 struct gspca_dev
*gspca_dev
= priv
;
1204 struct gspca_frame
*frame
;
1206 if (v4l2_buf
->index
< 0
1207 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1210 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1211 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1215 static int vidioc_streamon(struct file
*file
, void *priv
,
1216 enum v4l2_buf_type buf_type
)
1218 struct gspca_dev
*gspca_dev
= priv
;
1221 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1223 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1224 return -ERESTARTSYS
;
1225 if (!gspca_dev
->present
) {
1229 if (gspca_dev
->nframes
== 0
1230 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1234 if (!gspca_dev
->streaming
) {
1235 ret
= gspca_init_transfer(gspca_dev
);
1240 if (gspca_debug
& D_STREAM
) {
1241 PDEBUG_MODE("stream on OK",
1249 mutex_unlock(&gspca_dev
->queue_lock
);
1253 static int vidioc_streamoff(struct file
*file
, void *priv
,
1254 enum v4l2_buf_type buf_type
)
1256 struct gspca_dev
*gspca_dev
= priv
;
1259 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1261 if (!gspca_dev
->streaming
)
1263 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1264 return -ERESTARTSYS
;
1266 /* stop streaming */
1267 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1271 gspca_stream_off(gspca_dev
);
1272 mutex_unlock(&gspca_dev
->usb_lock
);
1274 /* empty the application queues */
1275 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1276 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1277 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1278 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1279 gspca_dev
->sequence
= 0;
1282 mutex_unlock(&gspca_dev
->queue_lock
);
1286 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1287 struct v4l2_jpegcompression
*jpegcomp
)
1289 struct gspca_dev
*gspca_dev
= priv
;
1292 if (!gspca_dev
->sd_desc
->get_jcomp
)
1294 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1295 return -ERESTARTSYS
;
1296 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1297 mutex_unlock(&gspca_dev
->usb_lock
);
1301 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1302 struct v4l2_jpegcompression
*jpegcomp
)
1304 struct gspca_dev
*gspca_dev
= priv
;
1307 if (!gspca_dev
->sd_desc
->set_jcomp
)
1309 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1310 return -ERESTARTSYS
;
1311 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1312 mutex_unlock(&gspca_dev
->usb_lock
);
1316 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1317 struct v4l2_streamparm
*parm
)
1319 struct gspca_dev
*gspca_dev
= priv
;
1321 memset(parm
, 0, sizeof *parm
);
1322 parm
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1323 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1325 if (gspca_dev
->sd_desc
->get_streamparm
) {
1328 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1329 return -ERESTARTSYS
;
1330 ret
= gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1331 mutex_unlock(&gspca_dev
->usb_lock
);
1338 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1339 struct v4l2_streamparm
*parm
)
1341 struct gspca_dev
*gspca_dev
= priv
;
1344 n
= parm
->parm
.capture
.readbuffers
;
1345 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1346 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1348 gspca_dev
->nbufread
= n
;
1350 if (gspca_dev
->sd_desc
->set_streamparm
) {
1353 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1354 return -ERESTARTSYS
;
1355 ret
= gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1356 mutex_unlock(&gspca_dev
->usb_lock
);
1363 static int vidioc_s_std(struct file
*filp
, void *priv
,
1369 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1370 static int vidiocgmbuf(struct file
*file
, void *priv
,
1371 struct video_mbuf
*mbuf
)
1373 struct gspca_dev
*gspca_dev
= file
->private_data
;
1376 PDEBUG(D_STREAM
, "cgmbuf");
1377 if (gspca_dev
->nframes
== 0) {
1381 struct v4l2_format fmt
;
1383 memset(&fmt
, 0, sizeof fmt
);
1384 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1385 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1386 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1387 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1388 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1389 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1394 struct v4l2_requestbuffers rb
;
1396 memset(&rb
, 0, sizeof rb
);
1398 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1399 rb
.memory
= V4L2_MEMORY_MMAP
;
1400 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1405 mbuf
->frames
= gspca_dev
->nframes
;
1406 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1407 for (i
= 0; i
< mbuf
->frames
; i
++)
1408 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1413 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1415 struct gspca_dev
*gspca_dev
= file
->private_data
;
1416 struct gspca_frame
*frame
;
1418 unsigned long addr
, start
, size
;
1421 start
= vma
->vm_start
;
1422 size
= vma
->vm_end
- vma
->vm_start
;
1423 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1425 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1426 return -ERESTARTSYS
;
1427 if (!gspca_dev
->present
) {
1431 if (gspca_dev
->capt_file
!= file
) {
1437 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1438 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1439 PDEBUG(D_STREAM
, "mmap bad memory type");
1442 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1444 frame
= &gspca_dev
->frame
[i
];
1448 if (frame
== NULL
) {
1449 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1453 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1454 /* v4l1 maps all the buffers */
1456 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1458 if (size
!= frame
->v4l2_buf
.length
) {
1459 PDEBUG(D_STREAM
, "mmap bad size");
1465 * - VM_IO marks the area as being a mmaped region for I/O to a
1466 * device. It also prevents the region from being core dumped.
1468 vma
->vm_flags
|= VM_IO
;
1470 addr
= (unsigned long) frame
->data
;
1472 page
= vmalloc_to_page((void *) addr
);
1473 ret
= vm_insert_page(vma
, start
, page
);
1481 vma
->vm_ops
= &gspca_vm_ops
;
1482 vma
->vm_private_data
= frame
;
1486 mutex_unlock(&gspca_dev
->queue_lock
);
1491 * wait for a video frame
1493 * If a frame is ready, its index is returned.
1495 static int frame_wait(struct gspca_dev
*gspca_dev
,
1498 struct gspca_frame
*frame
;
1501 /* check if a frame is ready */
1502 i
= gspca_dev
->fr_o
;
1503 j
= gspca_dev
->fr_queue
[i
];
1504 frame
= &gspca_dev
->frame
[j
];
1506 if (!(frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)) {
1510 /* wait till a frame is ready */
1511 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1512 (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) ||
1513 !gspca_dev
->streaming
|| !gspca_dev
->present
,
1514 msecs_to_jiffies(3000));
1517 if (ret
== 0 || !gspca_dev
->streaming
|| !gspca_dev
->present
)
1521 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1522 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1527 if (gspca_dev
->sd_desc
->dq_callback
) {
1528 mutex_lock(&gspca_dev
->usb_lock
);
1529 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1530 mutex_unlock(&gspca_dev
->usb_lock
);
1536 * dequeue a video buffer
1538 * If nonblock_ing is false, block until a buffer is available.
1540 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1541 struct v4l2_buffer
*v4l2_buf
)
1543 struct gspca_dev
*gspca_dev
= priv
;
1544 struct gspca_frame
*frame
;
1547 PDEBUG(D_FRAM
, "dqbuf");
1548 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1551 /* if not streaming, be sure the application will not loop forever */
1552 if (!(file
->f_flags
& O_NONBLOCK
)
1553 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1556 /* only the capturing file may dequeue */
1557 if (gspca_dev
->capt_file
!= file
)
1560 /* only one dequeue / read at a time */
1561 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1562 return -ERESTARTSYS
;
1564 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1567 i
= ret
; /* frame index */
1568 frame
= &gspca_dev
->frame
[i
];
1569 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1570 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1572 frame
->v4l2_buf
.bytesused
)) {
1573 PDEBUG(D_ERR
|D_STREAM
,
1574 "dqbuf cp to user failed");
1579 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1580 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1581 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1584 mutex_unlock(&gspca_dev
->read_lock
);
1589 * queue a video buffer
1591 * Attempting to queue a buffer that has already been
1592 * queued will return -EINVAL.
1594 static int vidioc_qbuf(struct file
*file
, void *priv
,
1595 struct v4l2_buffer
*v4l2_buf
)
1597 struct gspca_dev
*gspca_dev
= priv
;
1598 struct gspca_frame
*frame
;
1601 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1603 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1604 return -ERESTARTSYS
;
1606 index
= v4l2_buf
->index
;
1607 if ((unsigned) index
>= gspca_dev
->nframes
) {
1609 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1613 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1614 PDEBUG(D_FRAM
, "qbuf bad memory type");
1619 frame
= &gspca_dev
->frame
[index
];
1620 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1621 PDEBUG(D_FRAM
, "qbuf bad state");
1626 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1628 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1629 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1630 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1633 /* put the buffer in the 'queued' queue */
1634 i
= gspca_dev
->fr_q
;
1635 gspca_dev
->fr_queue
[i
] = index
;
1636 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1637 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1642 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1643 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1646 mutex_unlock(&gspca_dev
->queue_lock
);
1651 * allocate the resources for read()
1653 static int read_alloc(struct gspca_dev
*gspca_dev
,
1656 struct v4l2_buffer v4l2_buf
;
1659 PDEBUG(D_STREAM
, "read alloc");
1660 if (gspca_dev
->nframes
== 0) {
1661 struct v4l2_requestbuffers rb
;
1663 memset(&rb
, 0, sizeof rb
);
1664 rb
.count
= gspca_dev
->nbufread
;
1665 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1666 rb
.memory
= GSPCA_MEMORY_READ
;
1667 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1669 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1672 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1673 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1674 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1675 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1677 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1679 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1683 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1686 /* start streaming */
1687 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1689 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1693 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1695 struct gspca_dev
*gspca_dev
= file
->private_data
;
1698 PDEBUG(D_FRAM
, "poll");
1700 poll_wait(file
, &gspca_dev
->wq
, wait
);
1701 if (!gspca_dev
->present
)
1704 /* if reqbufs is not done, the user would use read() */
1705 if (gspca_dev
->nframes
== 0) {
1706 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
1707 return POLLERR
; /* not the 1st time */
1708 ret
= read_alloc(gspca_dev
, file
);
1713 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1715 if (!gspca_dev
->present
) {
1720 /* check the next incoming buffer */
1721 i
= gspca_dev
->fr_o
;
1722 i
= gspca_dev
->fr_queue
[i
];
1723 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1724 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1728 mutex_unlock(&gspca_dev
->queue_lock
);
1732 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1733 size_t count
, loff_t
*ppos
)
1735 struct gspca_dev
*gspca_dev
= file
->private_data
;
1736 struct gspca_frame
*frame
;
1737 struct v4l2_buffer v4l2_buf
;
1738 struct timeval timestamp
;
1741 PDEBUG(D_FRAM
, "read (%zd)", count
);
1742 if (!gspca_dev
->present
)
1744 switch (gspca_dev
->memory
) {
1745 case GSPCA_MEMORY_NO
: /* first time */
1746 ret
= read_alloc(gspca_dev
, file
);
1750 case GSPCA_MEMORY_READ
:
1751 if (gspca_dev
->capt_file
== file
)
1759 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1763 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1764 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1765 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1766 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1768 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1772 /* if the process slept for more than 1 second,
1773 * get a newer frame */
1774 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1776 break; /* avoid infinite loop */
1777 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1779 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1781 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1786 /* copy the frame */
1787 if (count
> frame
->v4l2_buf
.bytesused
)
1788 count
= frame
->v4l2_buf
.bytesused
;
1789 ret
= copy_to_user(data
, frame
->data
, count
);
1791 PDEBUG(D_ERR
|D_STREAM
,
1792 "read cp to user lack %d / %zd", ret
, count
);
1798 /* in each case, requeue the buffer */
1799 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1805 static struct v4l2_file_operations dev_fops
= {
1806 .owner
= THIS_MODULE
,
1808 .release
= dev_close
,
1811 .unlocked_ioctl
= video_ioctl2
,
1815 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1816 .vidioc_querycap
= vidioc_querycap
,
1817 .vidioc_dqbuf
= vidioc_dqbuf
,
1818 .vidioc_qbuf
= vidioc_qbuf
,
1819 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1820 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1821 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1822 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1823 .vidioc_streamon
= vidioc_streamon
,
1824 .vidioc_queryctrl
= vidioc_queryctrl
,
1825 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1826 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1827 .vidioc_g_audio
= vidioc_g_audio
,
1828 .vidioc_s_audio
= vidioc_s_audio
,
1829 .vidioc_enumaudio
= vidioc_enumaudio
,
1830 .vidioc_querymenu
= vidioc_querymenu
,
1831 .vidioc_enum_input
= vidioc_enum_input
,
1832 .vidioc_g_input
= vidioc_g_input
,
1833 .vidioc_s_input
= vidioc_s_input
,
1834 .vidioc_reqbufs
= vidioc_reqbufs
,
1835 .vidioc_querybuf
= vidioc_querybuf
,
1836 .vidioc_streamoff
= vidioc_streamoff
,
1837 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1838 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1839 .vidioc_g_parm
= vidioc_g_parm
,
1840 .vidioc_s_parm
= vidioc_s_parm
,
1841 .vidioc_s_std
= vidioc_s_std
,
1842 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1843 .vidiocgmbuf
= vidiocgmbuf
,
1847 static struct video_device gspca_template
= {
1848 .name
= "gspca main driver",
1850 .ioctl_ops
= &dev_ioctl_ops
,
1851 .release
= gspca_release
,
1856 * probe and create a new gspca device
1858 * This function must be called by the sub-driver when it is
1859 * called for probing a new device.
1861 int gspca_dev_probe(struct usb_interface
*intf
,
1862 const struct usb_device_id
*id
,
1863 const struct sd_desc
*sd_desc
,
1865 struct module
*module
)
1867 struct usb_interface_descriptor
*interface
;
1868 struct gspca_dev
*gspca_dev
;
1869 struct usb_device
*dev
= interface_to_usbdev(intf
);
1872 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1874 /* we don't handle multi-config cameras */
1875 if (dev
->descriptor
.bNumConfigurations
!= 1)
1877 interface
= &intf
->cur_altsetting
->desc
;
1878 if (interface
->bInterfaceNumber
> 0)
1881 /* create the device */
1882 if (dev_size
< sizeof *gspca_dev
)
1883 dev_size
= sizeof *gspca_dev
;
1884 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1886 err("couldn't kzalloc gspca struct");
1889 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
1890 if (!gspca_dev
->usb_buf
) {
1891 err("out of memory");
1895 gspca_dev
->dev
= dev
;
1896 gspca_dev
->iface
= interface
->bInterfaceNumber
;
1897 gspca_dev
->nbalt
= intf
->num_altsetting
;
1898 gspca_dev
->sd_desc
= sd_desc
;
1899 gspca_dev
->nbufread
= 2;
1900 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
1902 /* configure the subdriver and initialize the USB device */
1903 ret
= sd_desc
->config(gspca_dev
, id
);
1906 ret
= sd_desc
->init(gspca_dev
);
1909 ret
= gspca_set_alt0(gspca_dev
);
1912 gspca_set_default_mode(gspca_dev
);
1914 mutex_init(&gspca_dev
->usb_lock
);
1915 mutex_init(&gspca_dev
->read_lock
);
1916 mutex_init(&gspca_dev
->queue_lock
);
1917 init_waitqueue_head(&gspca_dev
->wq
);
1919 /* init video stuff */
1920 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
1921 gspca_dev
->vdev
.parent
= &dev
->dev
;
1922 gspca_dev
->module
= module
;
1923 gspca_dev
->present
= 1;
1924 ret
= video_register_device(&gspca_dev
->vdev
,
1928 err("video_register_device err %d", ret
);
1932 usb_set_intfdata(intf
, gspca_dev
);
1933 PDEBUG(D_PROBE
, "probe ok");
1936 kfree(gspca_dev
->usb_buf
);
1940 EXPORT_SYMBOL(gspca_dev_probe
);
1945 * This function must be called by the sub-driver
1946 * when the device disconnects, after the specific resources are freed.
1948 void gspca_disconnect(struct usb_interface
*intf
)
1950 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1952 gspca_dev
->present
= 0;
1953 gspca_dev
->streaming
= 0;
1955 usb_set_intfdata(intf
, NULL
);
1957 /* release the device */
1958 /* (this will call gspca_release() immediatly or on last close) */
1959 video_unregister_device(&gspca_dev
->vdev
);
1961 PDEBUG(D_PROBE
, "disconnect complete");
1963 EXPORT_SYMBOL(gspca_disconnect
);
1966 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1968 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1970 if (!gspca_dev
->streaming
)
1972 gspca_dev
->frozen
= 1; /* avoid urb error messages */
1973 if (gspca_dev
->sd_desc
->stopN
)
1974 gspca_dev
->sd_desc
->stopN(gspca_dev
);
1975 destroy_urbs(gspca_dev
);
1976 gspca_set_alt0(gspca_dev
);
1977 if (gspca_dev
->sd_desc
->stop0
)
1978 gspca_dev
->sd_desc
->stop0(gspca_dev
);
1981 EXPORT_SYMBOL(gspca_suspend
);
1983 int gspca_resume(struct usb_interface
*intf
)
1985 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1987 gspca_dev
->frozen
= 0;
1988 gspca_dev
->sd_desc
->init(gspca_dev
);
1989 if (gspca_dev
->streaming
)
1990 return gspca_init_transfer(gspca_dev
);
1993 EXPORT_SYMBOL(gspca_resume
);
1995 /* -- cam driver utility functions -- */
1997 /* auto gain and exposure algorithm based on the knee algorithm described here:
1998 http://ytse.tricolour.net/docs/LowLightOptimization.html
2000 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2002 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2003 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2005 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2006 const struct ctrl
*gain_ctrl
= NULL
;
2007 const struct ctrl
*exposure_ctrl
= NULL
;
2008 const struct ctrl
*autogain_ctrl
= NULL
;
2011 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2012 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2013 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2014 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2015 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2016 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2017 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2019 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2020 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2021 "on cam without (auto)gain/exposure");
2025 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2026 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2027 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2031 orig_exposure
= exposure
;
2033 /* If we are of a multiple of deadzone, do multiple steps to reach the
2034 desired lumination fast (with the risc of a slight overshoot) */
2035 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2037 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2038 avg_lum
, desired_avg_lum
, steps
);
2040 for (i
= 0; i
< steps
; i
++) {
2041 if (avg_lum
> desired_avg_lum
) {
2042 if (gain
> gain_knee
)
2044 else if (exposure
> exposure_knee
)
2046 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2048 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2050 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2055 if (gain
< gain_ctrl
->qctrl
.default_value
)
2057 else if (exposure
< exposure_knee
)
2059 else if (gain
< gain_knee
)
2061 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2063 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2070 if (gain
!= orig_gain
) {
2071 gain_ctrl
->set(gspca_dev
, gain
);
2074 if (exposure
!= orig_exposure
) {
2075 exposure_ctrl
->set(gspca_dev
, exposure
);
2081 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2083 /* -- module insert / remove -- */
2084 static int __init
gspca_init(void)
2086 info("main v%d.%d.%d registered",
2087 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2088 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2089 DRIVER_VERSION_NUMBER
& 0xff);
2092 static void __exit
gspca_exit(void)
2094 info("main deregistered");
2097 module_init(gspca_init
);
2098 module_exit(gspca_exit
);
2101 module_param_named(debug
, gspca_debug
, int, 0644);
2102 MODULE_PARM_DESC(debug
,
2103 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2104 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"