2 * Main USB camera driver
4 * Copyright (C) 2008-2009 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 3 /* default number of URBs */
42 #if DEF_NURBS > MAX_NURBS
43 #error "DEF_NURBS too big"
46 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
47 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
48 MODULE_LICENSE("GPL");
50 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 6, 0)
53 int gspca_debug
= D_ERR
| D_PROBE
;
54 EXPORT_SYMBOL(gspca_debug
);
56 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
58 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
59 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
63 (pixfmt
>> 16) & 0xff,
67 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
74 #define PDEBUG_MODE(txt, pixfmt, w, h)
77 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
78 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
79 #define GSPCA_MEMORY_READ 7
81 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
86 static void gspca_vm_open(struct vm_area_struct
*vma
)
88 struct gspca_frame
*frame
= vma
->vm_private_data
;
90 frame
->vma_use_count
++;
91 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
94 static void gspca_vm_close(struct vm_area_struct
*vma
)
96 struct gspca_frame
*frame
= vma
->vm_private_data
;
98 if (--frame
->vma_use_count
<= 0)
99 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
102 static struct vm_operations_struct gspca_vm_ops
= {
103 .open
= gspca_vm_open
,
104 .close
= gspca_vm_close
,
107 /* get the current input frame buffer */
108 struct gspca_frame
*gspca_get_i_frame(struct gspca_dev
*gspca_dev
)
110 struct gspca_frame
*frame
;
114 i
= gspca_dev
->fr_queue
[i
];
115 frame
= &gspca_dev
->frame
[i
];
116 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
117 != V4L2_BUF_FLAG_QUEUED
)
121 EXPORT_SYMBOL(gspca_get_i_frame
);
124 * fill a video frame from an URB and resubmit
126 static void fill_frame(struct gspca_dev
*gspca_dev
,
129 struct gspca_frame
*frame
;
130 u8
*data
; /* address of data in the iso message */
134 if (urb
->status
!= 0) {
135 if (urb
->status
== -ESHUTDOWN
)
136 return; /* disconnection */
138 if (!gspca_dev
->frozen
)
140 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
143 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
144 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
146 /* check the availability of the frame buffer */
147 frame
= gspca_get_i_frame(gspca_dev
);
149 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
153 /* check the packet status and length */
154 len
= urb
->iso_frame_desc
[i
].actual_length
;
156 if (gspca_dev
->empty_packet
== 0)
157 gspca_dev
->empty_packet
= 1;
160 st
= urb
->iso_frame_desc
[i
].status
;
163 "ISOC data error: [%d] len=%d, status=%d",
165 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
169 /* let the packet be analyzed by the subdriver */
170 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
171 i
, urb
->iso_frame_desc
[i
].offset
, len
);
172 data
= (u8
*) urb
->transfer_buffer
173 + urb
->iso_frame_desc
[i
].offset
;
174 pkt_scan(gspca_dev
, frame
, data
, len
);
177 /* resubmit the URB */
178 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
180 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
184 * ISOC message interrupt from the USB device
186 * Analyse each packet and call the subdriver for copy to the frame buffer.
188 static void isoc_irq(struct urb
*urb
)
190 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
192 PDEBUG(D_PACK
, "isoc irq");
193 if (!gspca_dev
->streaming
)
195 fill_frame(gspca_dev
, urb
);
199 * bulk message interrupt from the USB device
201 static void bulk_irq(struct urb
*urb
)
203 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
204 struct gspca_frame
*frame
;
207 PDEBUG(D_PACK
, "bulk irq");
208 if (!gspca_dev
->streaming
)
210 switch (urb
->status
) {
214 return; /* disconnection */
220 if (!gspca_dev
->frozen
)
222 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
226 /* check the availability of the frame buffer */
227 frame
= gspca_get_i_frame(gspca_dev
);
229 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
231 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
232 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
234 urb
->transfer_buffer
,
238 /* resubmit the URB */
239 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
240 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
242 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
247 * add data to the current frame
249 * This function is called by the subdrivers at interrupt level.
251 * To build a frame, these ones must add
253 * - 0 or many INTER_PACKETs
255 * DISCARD_PACKET invalidates the whole frame.
256 * On LAST_PACKET, a new frame is returned.
258 struct gspca_frame
*gspca_frame_add(struct gspca_dev
*gspca_dev
,
259 enum gspca_packet_type packet_type
,
260 struct gspca_frame
*frame
,
266 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
268 /* when start of a new frame, if the current frame buffer
269 * is not queued, discard the whole frame */
270 if (packet_type
== FIRST_PACKET
) {
271 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
272 != V4L2_BUF_FLAG_QUEUED
) {
273 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
276 frame
->data_end
= frame
->data
;
277 jiffies_to_timeval(get_jiffies_64(),
278 &frame
->v4l2_buf
.timestamp
);
279 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
280 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
281 if (packet_type
== LAST_PACKET
)
282 gspca_dev
->last_packet_type
= packet_type
;
286 /* append the packet to the frame buffer */
288 if (frame
->data_end
- frame
->data
+ len
289 > frame
->v4l2_buf
.length
) {
290 PDEBUG(D_ERR
|D_PACK
, "frame overflow %zd > %d",
291 frame
->data_end
- frame
->data
+ len
,
292 frame
->v4l2_buf
.length
);
293 packet_type
= DISCARD_PACKET
;
295 memcpy(frame
->data_end
, data
, len
);
296 frame
->data_end
+= len
;
299 gspca_dev
->last_packet_type
= packet_type
;
301 /* if last packet, wake up the application and advance in the queue */
302 if (packet_type
== LAST_PACKET
) {
303 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
304 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
305 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
306 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
307 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
309 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
310 frame
->v4l2_buf
.bytesused
,
314 j
= gspca_dev
->fr_queue
[i
];
315 frame
= &gspca_dev
->frame
[j
];
319 EXPORT_SYMBOL(gspca_frame_add
);
321 static int gspca_is_compressed(__u32 format
)
324 case V4L2_PIX_FMT_MJPEG
:
325 case V4L2_PIX_FMT_JPEG
:
326 case V4L2_PIX_FMT_SPCA561
:
327 case V4L2_PIX_FMT_PAC207
:
328 case V4L2_PIX_FMT_MR97310A
:
334 static void *rvmalloc(unsigned long size
)
339 mem
= vmalloc_32(size
);
341 adr
= (unsigned long) mem
;
342 while ((long) size
> 0) {
343 SetPageReserved(vmalloc_to_page((void *) adr
));
351 static void rvfree(void *mem
, long size
)
355 adr
= (unsigned long) mem
;
357 ClearPageReserved(vmalloc_to_page((void *) adr
));
364 static int frame_alloc(struct gspca_dev
*gspca_dev
,
367 struct gspca_frame
*frame
;
371 i
= gspca_dev
->curr_mode
;
372 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
373 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
374 frsz
= PAGE_ALIGN(frsz
);
375 gspca_dev
->frsz
= frsz
;
376 if (count
> GSPCA_MAX_FRAMES
)
377 count
= GSPCA_MAX_FRAMES
;
378 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
379 if (!gspca_dev
->frbuf
) {
380 err("frame alloc failed");
383 gspca_dev
->nframes
= count
;
384 for (i
= 0; i
< count
; i
++) {
385 frame
= &gspca_dev
->frame
[i
];
386 frame
->v4l2_buf
.index
= i
;
387 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
388 frame
->v4l2_buf
.flags
= 0;
389 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
390 frame
->v4l2_buf
.length
= frsz
;
391 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
392 frame
->v4l2_buf
.sequence
= 0;
393 frame
->data
= frame
->data_end
=
394 gspca_dev
->frbuf
+ i
* frsz
;
395 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
397 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
398 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
399 gspca_dev
->sequence
= 0;
403 static void frame_free(struct gspca_dev
*gspca_dev
)
407 PDEBUG(D_STREAM
, "frame free");
408 if (gspca_dev
->frbuf
!= NULL
) {
409 rvfree(gspca_dev
->frbuf
,
410 gspca_dev
->nframes
* gspca_dev
->frsz
);
411 gspca_dev
->frbuf
= NULL
;
412 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
413 gspca_dev
->frame
[i
].data
= NULL
;
415 gspca_dev
->nframes
= 0;
418 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
423 PDEBUG(D_STREAM
, "kill transfer");
424 for (i
= 0; i
< MAX_NURBS
; i
++) {
425 urb
= gspca_dev
->urb
[i
];
429 gspca_dev
->urb
[i
] = NULL
;
431 if (urb
->transfer_buffer
!= NULL
)
432 usb_buffer_free(gspca_dev
->dev
,
433 urb
->transfer_buffer_length
,
434 urb
->transfer_buffer
,
441 * look for an input transfer endpoint in an alternate setting
443 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
446 struct usb_host_endpoint
*ep
;
449 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
450 ep
= &alt
->endpoint
[i
];
451 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
453 && ep
->desc
.wMaxPacketSize
!= 0)
460 * look for an input (isoc or bulk) endpoint
462 * The endpoint is defined by the subdriver.
463 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
464 * This routine may be called many times when the bandwidth is too small
465 * (the bandwidth is checked on urb submit).
467 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
469 struct usb_interface
*intf
;
470 struct usb_host_endpoint
*ep
;
473 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
475 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
476 : USB_ENDPOINT_XFER_ISOC
;
477 i
= gspca_dev
->alt
; /* previous alt setting */
479 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
484 err("no transfer endpoint found");
487 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
488 i
, ep
->desc
.bEndpointAddress
);
489 if (gspca_dev
->nbalt
> 1) {
490 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
492 err("set alt %d err %d", i
, ret
);
496 gspca_dev
->alt
= i
; /* memorize the current alt setting */
501 * create the URBs for image transfer
503 static int create_urbs(struct gspca_dev
*gspca_dev
,
504 struct usb_host_endpoint
*ep
)
507 int n
, nurbs
, i
, psize
, npkt
, bsize
;
509 /* calculate the packet size and the number of packets */
510 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
512 if (!gspca_dev
->cam
.bulk
) { /* isoc */
514 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
515 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
516 npkt
= gspca_dev
->cam
.npkt
;
518 npkt
= 32; /* default value */
519 bsize
= psize
* npkt
;
521 "isoc %d pkts size %d = bsize:%d",
526 bsize
= gspca_dev
->cam
.bulk_size
;
529 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
530 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
531 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
536 gspca_dev
->nurbs
= nurbs
;
537 for (n
= 0; n
< nurbs
; n
++) {
538 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
540 err("usb_alloc_urb failed");
541 destroy_urbs(gspca_dev
);
544 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
549 if (urb
->transfer_buffer
== NULL
) {
551 err("usb_buffer_urb failed");
552 destroy_urbs(gspca_dev
);
555 gspca_dev
->urb
[n
] = urb
;
556 urb
->dev
= gspca_dev
->dev
;
557 urb
->context
= gspca_dev
;
558 urb
->transfer_buffer_length
= bsize
;
559 if (npkt
!= 0) { /* ISOC */
560 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
561 ep
->desc
.bEndpointAddress
);
562 urb
->transfer_flags
= URB_ISO_ASAP
563 | URB_NO_TRANSFER_DMA_MAP
;
564 urb
->interval
= ep
->desc
.bInterval
;
565 urb
->complete
= isoc_irq
;
566 urb
->number_of_packets
= npkt
;
567 for (i
= 0; i
< npkt
; i
++) {
568 urb
->iso_frame_desc
[i
].length
= psize
;
569 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
572 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
573 ep
->desc
.bEndpointAddress
),
574 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
575 urb
->complete
= bulk_irq
;
582 * start the USB transfer
584 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
586 struct usb_host_endpoint
*ep
;
589 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
592 if (!gspca_dev
->present
) {
597 /* set the higher alternate setting and
598 * loop until urb submit succeeds */
599 gspca_dev
->alt
= gspca_dev
->nbalt
;
601 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
602 ep
= get_ep(gspca_dev
);
607 ret
= create_urbs(gspca_dev
, ep
);
611 /* clear the bulk endpoint */
612 if (gspca_dev
->cam
.bulk
)
613 usb_clear_halt(gspca_dev
->dev
,
614 gspca_dev
->urb
[0]->pipe
);
617 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
619 destroy_urbs(gspca_dev
);
622 gspca_dev
->streaming
= 1;
624 /* some bulk transfers are started by the subdriver */
625 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
628 /* submit the URBs */
629 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
630 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
632 PDEBUG(D_ERR
|D_STREAM
,
633 "usb_submit_urb [%d] err %d", n
, ret
);
634 gspca_dev
->streaming
= 0;
635 destroy_urbs(gspca_dev
);
636 if (ret
== -ENOSPC
) {
637 msleep(20); /* wait for kill
639 break; /* try the previous alt */
648 mutex_unlock(&gspca_dev
->usb_lock
);
652 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
656 if (gspca_dev
->alt
== 0)
658 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
660 PDEBUG(D_ERR
|D_STREAM
, "set alt 0 err %d", ret
);
664 /* Note: both the queue and the usb locks should be held when calling this */
665 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
667 gspca_dev
->streaming
= 0;
668 if (gspca_dev
->present
) {
669 if (gspca_dev
->sd_desc
->stopN
)
670 gspca_dev
->sd_desc
->stopN(gspca_dev
);
671 destroy_urbs(gspca_dev
);
672 gspca_set_alt0(gspca_dev
);
675 /* always call stop0 to free the subdriver's resources */
676 if (gspca_dev
->sd_desc
->stop0
)
677 gspca_dev
->sd_desc
->stop0(gspca_dev
);
678 PDEBUG(D_STREAM
, "stream off OK");
681 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
685 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
686 gspca_dev
->curr_mode
= i
;
687 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
688 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
689 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
692 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
693 int width
, int height
)
697 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
698 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
699 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
706 * search a mode with the right pixel format
708 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
714 modeU
= modeD
= mode
;
715 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
717 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
721 if (++modeU
< gspca_dev
->cam
.nmodes
) {
722 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
730 #ifdef CONFIG_VIDEO_ADV_DEBUG
731 static int vidioc_g_register(struct file
*file
, void *priv
,
732 struct v4l2_dbg_register
*reg
)
735 struct gspca_dev
*gspca_dev
= priv
;
737 if (!gspca_dev
->sd_desc
->get_chip_ident
)
740 if (!gspca_dev
->sd_desc
->get_register
)
743 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
745 if (gspca_dev
->present
)
746 ret
= gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
749 mutex_unlock(&gspca_dev
->usb_lock
);
754 static int vidioc_s_register(struct file
*file
, void *priv
,
755 struct v4l2_dbg_register
*reg
)
758 struct gspca_dev
*gspca_dev
= priv
;
760 if (!gspca_dev
->sd_desc
->get_chip_ident
)
763 if (!gspca_dev
->sd_desc
->set_register
)
766 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
768 if (gspca_dev
->present
)
769 ret
= gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
772 mutex_unlock(&gspca_dev
->usb_lock
);
778 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
779 struct v4l2_dbg_chip_ident
*chip
)
782 struct gspca_dev
*gspca_dev
= priv
;
784 if (!gspca_dev
->sd_desc
->get_chip_ident
)
787 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
789 if (gspca_dev
->present
)
790 ret
= gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
793 mutex_unlock(&gspca_dev
->usb_lock
);
798 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
799 struct v4l2_fmtdesc
*fmtdesc
)
801 struct gspca_dev
*gspca_dev
= priv
;
805 /* give an index to each format */
808 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
809 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
812 if (fmt_tb
[j
] == fmt_tb
[index
])
817 if (fmtdesc
->index
== index
)
818 break; /* new format */
820 if (index
>= ARRAY_SIZE(fmt_tb
))
825 return -EINVAL
; /* no more format */
827 fmtdesc
->pixelformat
= fmt_tb
[index
];
828 if (gspca_is_compressed(fmt_tb
[index
]))
829 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
830 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
831 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
832 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
833 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
834 fmtdesc
->description
[4] = '\0';
838 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
839 struct v4l2_format
*fmt
)
841 struct gspca_dev
*gspca_dev
= priv
;
844 mode
= gspca_dev
->curr_mode
;
845 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
846 sizeof fmt
->fmt
.pix
);
850 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
851 struct v4l2_format
*fmt
)
853 int w
, h
, mode
, mode2
;
855 w
= fmt
->fmt
.pix
.width
;
856 h
= fmt
->fmt
.pix
.height
;
859 if (gspca_debug
& D_CONF
)
860 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
862 /* search the closest mode for width and height */
863 mode
= wxh_to_mode(gspca_dev
, w
, h
);
865 /* OK if right palette */
866 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
867 != fmt
->fmt
.pix
.pixelformat
) {
869 /* else, search the closest mode with the same pixel format */
870 mode2
= gspca_get_mode(gspca_dev
, mode
,
871 fmt
->fmt
.pix
.pixelformat
);
875 ; * no chance, return this mode */
877 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
878 sizeof fmt
->fmt
.pix
);
879 return mode
; /* used when s_fmt */
882 static int vidioc_try_fmt_vid_cap(struct file
*file
,
884 struct v4l2_format
*fmt
)
886 struct gspca_dev
*gspca_dev
= priv
;
889 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
895 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
896 struct v4l2_format
*fmt
)
898 struct gspca_dev
*gspca_dev
= priv
;
901 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
904 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
908 if (gspca_dev
->nframes
!= 0
909 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
914 if (ret
== gspca_dev
->curr_mode
) {
916 goto out
; /* same mode */
919 if (gspca_dev
->streaming
) {
923 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
924 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
925 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
926 gspca_dev
->curr_mode
= ret
;
930 mutex_unlock(&gspca_dev
->queue_lock
);
934 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
935 struct v4l2_frmsizeenum
*fsize
)
937 struct gspca_dev
*gspca_dev
= priv
;
941 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
942 if (fsize
->pixel_format
!=
943 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
946 if (fsize
->index
== index
) {
947 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
948 fsize
->discrete
.width
=
949 gspca_dev
->cam
.cam_mode
[i
].width
;
950 fsize
->discrete
.height
=
951 gspca_dev
->cam
.cam_mode
[i
].height
;
960 static void gspca_release(struct video_device
*vfd
)
962 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
964 PDEBUG(D_STREAM
, "device released");
966 kfree(gspca_dev
->usb_buf
);
970 static int dev_open(struct file
*file
)
972 struct gspca_dev
*gspca_dev
;
975 PDEBUG(D_STREAM
, "%s open", current
->comm
);
976 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
977 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
979 if (!gspca_dev
->present
) {
984 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
989 /* protect the subdriver against rmmod */
990 if (!try_module_get(gspca_dev
->module
)) {
997 file
->private_data
= gspca_dev
;
999 /* activate the v4l2 debug */
1000 if (gspca_debug
& D_V4L2
)
1001 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1002 | V4L2_DEBUG_IOCTL_ARG
;
1004 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1005 | V4L2_DEBUG_IOCTL_ARG
);
1009 mutex_unlock(&gspca_dev
->queue_lock
);
1011 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
1013 PDEBUG(D_STREAM
, "open done");
1017 static int dev_close(struct file
*file
)
1019 struct gspca_dev
*gspca_dev
= file
->private_data
;
1021 PDEBUG(D_STREAM
, "%s close", current
->comm
);
1022 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1023 return -ERESTARTSYS
;
1026 /* if the file did the capture, free the streaming resources */
1027 if (gspca_dev
->capt_file
== file
) {
1028 if (gspca_dev
->streaming
) {
1029 mutex_lock(&gspca_dev
->usb_lock
);
1030 gspca_stream_off(gspca_dev
);
1031 mutex_unlock(&gspca_dev
->usb_lock
);
1033 frame_free(gspca_dev
);
1034 gspca_dev
->capt_file
= NULL
;
1035 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
1037 file
->private_data
= NULL
;
1038 module_put(gspca_dev
->module
);
1039 mutex_unlock(&gspca_dev
->queue_lock
);
1041 PDEBUG(D_STREAM
, "close done");
1046 static int vidioc_querycap(struct file
*file
, void *priv
,
1047 struct v4l2_capability
*cap
)
1049 struct gspca_dev
*gspca_dev
= priv
;
1052 /* protect the access to the usb device */
1053 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1054 return -ERESTARTSYS
;
1055 if (!gspca_dev
->present
) {
1059 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
1060 if (gspca_dev
->dev
->product
!= NULL
) {
1061 strncpy(cap
->card
, gspca_dev
->dev
->product
,
1064 snprintf(cap
->card
, sizeof cap
->card
,
1065 "USB Camera (%04x:%04x)",
1066 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1067 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1069 usb_make_path(gspca_dev
->dev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1070 cap
->version
= DRIVER_VERSION_NUMBER
;
1071 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
1072 | V4L2_CAP_STREAMING
1073 | V4L2_CAP_READWRITE
;
1076 mutex_unlock(&gspca_dev
->usb_lock
);
1080 static const struct ctrl
*get_ctrl(struct gspca_dev
*gspca_dev
,
1083 const struct ctrl
*ctrls
;
1086 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1087 i
< gspca_dev
->sd_desc
->nctrls
;
1089 if (gspca_dev
->ctrl_dis
& (1 << i
))
1091 if (id
== ctrls
->qctrl
.id
)
1097 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1098 struct v4l2_queryctrl
*q_ctrl
)
1100 struct gspca_dev
*gspca_dev
= priv
;
1101 const struct ctrl
*ctrls
;
1107 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1108 id
&= V4L2_CTRL_ID_MASK
;
1110 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1111 if (gspca_dev
->ctrl_dis
& (1 << i
))
1113 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1115 if (ctrls
&& gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1118 ctrls
= &gspca_dev
->sd_desc
->ctrls
[i
];
1121 ctrls
= get_ctrl(gspca_dev
, id
);
1125 memcpy(q_ctrl
, ctrls
, sizeof *q_ctrl
);
1129 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1130 struct v4l2_control
*ctrl
)
1132 struct gspca_dev
*gspca_dev
= priv
;
1133 const struct ctrl
*ctrls
;
1136 ctrls
= get_ctrl(gspca_dev
, ctrl
->id
);
1140 if (ctrl
->value
< ctrls
->qctrl
.minimum
1141 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1143 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1144 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1145 return -ERESTARTSYS
;
1146 if (gspca_dev
->present
)
1147 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1150 mutex_unlock(&gspca_dev
->usb_lock
);
1154 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1155 struct v4l2_control
*ctrl
)
1157 struct gspca_dev
*gspca_dev
= priv
;
1158 const struct ctrl
*ctrls
;
1161 ctrls
= get_ctrl(gspca_dev
, ctrl
->id
);
1165 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1166 return -ERESTARTSYS
;
1167 if (gspca_dev
->present
)
1168 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1171 mutex_unlock(&gspca_dev
->usb_lock
);
1175 /*fixme: have an audio flag in gspca_dev?*/
1176 static int vidioc_s_audio(struct file
*file
, void *priv
,
1177 struct v4l2_audio
*audio
)
1179 if (audio
->index
!= 0)
1184 static int vidioc_g_audio(struct file
*file
, void *priv
,
1185 struct v4l2_audio
*audio
)
1187 strcpy(audio
->name
, "Microphone");
1191 static int vidioc_enumaudio(struct file
*file
, void *priv
,
1192 struct v4l2_audio
*audio
)
1194 if (audio
->index
!= 0)
1197 strcpy(audio
->name
, "Microphone");
1198 audio
->capability
= 0;
1203 static int vidioc_querymenu(struct file
*file
, void *priv
,
1204 struct v4l2_querymenu
*qmenu
)
1206 struct gspca_dev
*gspca_dev
= priv
;
1208 if (!gspca_dev
->sd_desc
->querymenu
)
1210 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1213 static int vidioc_enum_input(struct file
*file
, void *priv
,
1214 struct v4l2_input
*input
)
1216 struct gspca_dev
*gspca_dev
= priv
;
1218 if (input
->index
!= 0)
1220 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1221 input
->status
= gspca_dev
->cam
.input_flags
;
1222 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1223 sizeof input
->name
);
1227 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1233 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1240 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1241 struct v4l2_requestbuffers
*rb
)
1243 struct gspca_dev
*gspca_dev
= priv
;
1246 switch (rb
->memory
) {
1247 case GSPCA_MEMORY_READ
: /* (internal call) */
1248 case V4L2_MEMORY_MMAP
:
1249 case V4L2_MEMORY_USERPTR
:
1254 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1255 return -ERESTARTSYS
;
1257 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1258 && gspca_dev
->memory
!= rb
->memory
) {
1263 /* only one file may do the capture */
1264 if (gspca_dev
->capt_file
!= NULL
1265 && gspca_dev
->capt_file
!= file
) {
1270 /* if allocated, the buffers must not be mapped */
1271 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1272 if (gspca_dev
->frame
[i
].vma_use_count
) {
1278 /* stop streaming */
1279 if (gspca_dev
->streaming
) {
1280 mutex_lock(&gspca_dev
->usb_lock
);
1281 gspca_stream_off(gspca_dev
);
1282 mutex_unlock(&gspca_dev
->usb_lock
);
1285 /* free the previous allocated buffers, if any */
1286 if (gspca_dev
->nframes
!= 0) {
1287 frame_free(gspca_dev
);
1288 gspca_dev
->capt_file
= NULL
;
1290 if (rb
->count
== 0) /* unrequest */
1292 gspca_dev
->memory
= rb
->memory
;
1293 ret
= frame_alloc(gspca_dev
, rb
->count
);
1295 rb
->count
= gspca_dev
->nframes
;
1296 gspca_dev
->capt_file
= file
;
1299 mutex_unlock(&gspca_dev
->queue_lock
);
1300 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1304 static int vidioc_querybuf(struct file
*file
, void *priv
,
1305 struct v4l2_buffer
*v4l2_buf
)
1307 struct gspca_dev
*gspca_dev
= priv
;
1308 struct gspca_frame
*frame
;
1310 if (v4l2_buf
->index
< 0
1311 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1314 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1315 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1319 static int vidioc_streamon(struct file
*file
, void *priv
,
1320 enum v4l2_buf_type buf_type
)
1322 struct gspca_dev
*gspca_dev
= priv
;
1325 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1327 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1328 return -ERESTARTSYS
;
1330 if (gspca_dev
->nframes
== 0
1331 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1335 if (!gspca_dev
->streaming
) {
1336 ret
= gspca_init_transfer(gspca_dev
);
1341 if (gspca_debug
& D_STREAM
) {
1342 PDEBUG_MODE("stream on OK",
1350 mutex_unlock(&gspca_dev
->queue_lock
);
1354 static int vidioc_streamoff(struct file
*file
, void *priv
,
1355 enum v4l2_buf_type buf_type
)
1357 struct gspca_dev
*gspca_dev
= priv
;
1360 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1362 if (!gspca_dev
->streaming
)
1364 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1365 return -ERESTARTSYS
;
1367 /* stop streaming */
1368 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1372 gspca_stream_off(gspca_dev
);
1373 mutex_unlock(&gspca_dev
->usb_lock
);
1375 /* empty the application queues */
1376 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1377 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1378 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1379 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1380 gspca_dev
->sequence
= 0;
1383 mutex_unlock(&gspca_dev
->queue_lock
);
1387 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1388 struct v4l2_jpegcompression
*jpegcomp
)
1390 struct gspca_dev
*gspca_dev
= priv
;
1393 if (!gspca_dev
->sd_desc
->get_jcomp
)
1395 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1396 return -ERESTARTSYS
;
1397 if (gspca_dev
->present
)
1398 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1401 mutex_unlock(&gspca_dev
->usb_lock
);
1405 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1406 struct v4l2_jpegcompression
*jpegcomp
)
1408 struct gspca_dev
*gspca_dev
= priv
;
1411 if (!gspca_dev
->sd_desc
->set_jcomp
)
1413 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1414 return -ERESTARTSYS
;
1415 if (gspca_dev
->present
)
1416 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1419 mutex_unlock(&gspca_dev
->usb_lock
);
1423 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1424 struct v4l2_streamparm
*parm
)
1426 struct gspca_dev
*gspca_dev
= priv
;
1428 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1430 if (gspca_dev
->sd_desc
->get_streamparm
) {
1433 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1434 return -ERESTARTSYS
;
1435 if (gspca_dev
->present
)
1436 ret
= gspca_dev
->sd_desc
->get_streamparm(gspca_dev
,
1440 mutex_unlock(&gspca_dev
->usb_lock
);
1447 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1448 struct v4l2_streamparm
*parm
)
1450 struct gspca_dev
*gspca_dev
= priv
;
1453 n
= parm
->parm
.capture
.readbuffers
;
1454 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1455 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1457 gspca_dev
->nbufread
= n
;
1459 if (gspca_dev
->sd_desc
->set_streamparm
) {
1462 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1463 return -ERESTARTSYS
;
1464 if (gspca_dev
->present
)
1465 ret
= gspca_dev
->sd_desc
->set_streamparm(gspca_dev
,
1469 mutex_unlock(&gspca_dev
->usb_lock
);
1476 static int vidioc_s_std(struct file
*filp
, void *priv
,
1482 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1483 static int vidiocgmbuf(struct file
*file
, void *priv
,
1484 struct video_mbuf
*mbuf
)
1486 struct gspca_dev
*gspca_dev
= file
->private_data
;
1489 PDEBUG(D_STREAM
, "cgmbuf");
1490 if (gspca_dev
->nframes
== 0) {
1494 struct v4l2_format fmt
;
1496 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1497 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1498 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1499 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1500 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1501 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1506 struct v4l2_requestbuffers rb
;
1508 memset(&rb
, 0, sizeof rb
);
1510 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1511 rb
.memory
= V4L2_MEMORY_MMAP
;
1512 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1517 mbuf
->frames
= gspca_dev
->nframes
;
1518 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1519 for (i
= 0; i
< mbuf
->frames
; i
++)
1520 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1525 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1527 struct gspca_dev
*gspca_dev
= file
->private_data
;
1528 struct gspca_frame
*frame
;
1530 unsigned long addr
, start
, size
;
1533 start
= vma
->vm_start
;
1534 size
= vma
->vm_end
- vma
->vm_start
;
1535 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1537 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1538 return -ERESTARTSYS
;
1539 if (!gspca_dev
->present
) {
1543 if (gspca_dev
->capt_file
!= file
) {
1549 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1550 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1551 PDEBUG(D_STREAM
, "mmap bad memory type");
1554 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1556 frame
= &gspca_dev
->frame
[i
];
1560 if (frame
== NULL
) {
1561 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1565 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1566 /* v4l1 maps all the buffers */
1568 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1570 if (size
!= frame
->v4l2_buf
.length
) {
1571 PDEBUG(D_STREAM
, "mmap bad size");
1577 * - VM_IO marks the area as being a mmaped region for I/O to a
1578 * device. It also prevents the region from being core dumped.
1580 vma
->vm_flags
|= VM_IO
;
1582 addr
= (unsigned long) frame
->data
;
1584 page
= vmalloc_to_page((void *) addr
);
1585 ret
= vm_insert_page(vma
, start
, page
);
1593 vma
->vm_ops
= &gspca_vm_ops
;
1594 vma
->vm_private_data
= frame
;
1598 mutex_unlock(&gspca_dev
->queue_lock
);
1603 * wait for a video frame
1605 * If a frame is ready, its index is returned.
1607 static int frame_wait(struct gspca_dev
*gspca_dev
,
1610 struct gspca_frame
*frame
;
1613 /* check if a frame is ready */
1614 i
= gspca_dev
->fr_o
;
1615 j
= gspca_dev
->fr_queue
[i
];
1616 frame
= &gspca_dev
->frame
[j
];
1618 if (!(frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)) {
1622 /* wait till a frame is ready */
1623 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1624 (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) ||
1625 !gspca_dev
->streaming
|| !gspca_dev
->present
,
1626 msecs_to_jiffies(3000));
1629 if (ret
== 0 || !gspca_dev
->streaming
|| !gspca_dev
->present
)
1633 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1634 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1639 if (gspca_dev
->sd_desc
->dq_callback
) {
1640 mutex_lock(&gspca_dev
->usb_lock
);
1641 if (gspca_dev
->present
)
1642 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1643 mutex_unlock(&gspca_dev
->usb_lock
);
1649 * dequeue a video buffer
1651 * If nonblock_ing is false, block until a buffer is available.
1653 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1654 struct v4l2_buffer
*v4l2_buf
)
1656 struct gspca_dev
*gspca_dev
= priv
;
1657 struct gspca_frame
*frame
;
1660 PDEBUG(D_FRAM
, "dqbuf");
1661 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1664 if (!gspca_dev
->present
)
1667 /* if not streaming, be sure the application will not loop forever */
1668 if (!(file
->f_flags
& O_NONBLOCK
)
1669 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1672 /* only the capturing file may dequeue */
1673 if (gspca_dev
->capt_file
!= file
)
1676 /* only one dequeue / read at a time */
1677 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1678 return -ERESTARTSYS
;
1680 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1683 i
= ret
; /* frame index */
1684 frame
= &gspca_dev
->frame
[i
];
1685 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1686 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1688 frame
->v4l2_buf
.bytesused
)) {
1689 PDEBUG(D_ERR
|D_STREAM
,
1690 "dqbuf cp to user failed");
1695 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1696 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1697 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1700 mutex_unlock(&gspca_dev
->read_lock
);
1705 * queue a video buffer
1707 * Attempting to queue a buffer that has already been
1708 * queued will return -EINVAL.
1710 static int vidioc_qbuf(struct file
*file
, void *priv
,
1711 struct v4l2_buffer
*v4l2_buf
)
1713 struct gspca_dev
*gspca_dev
= priv
;
1714 struct gspca_frame
*frame
;
1717 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1719 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1720 return -ERESTARTSYS
;
1722 index
= v4l2_buf
->index
;
1723 if ((unsigned) index
>= gspca_dev
->nframes
) {
1725 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1729 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1730 PDEBUG(D_FRAM
, "qbuf bad memory type");
1735 frame
= &gspca_dev
->frame
[index
];
1736 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1737 PDEBUG(D_FRAM
, "qbuf bad state");
1742 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1744 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1745 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1746 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1749 /* put the buffer in the 'queued' queue */
1750 i
= gspca_dev
->fr_q
;
1751 gspca_dev
->fr_queue
[i
] = index
;
1752 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1753 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1758 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1759 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1762 mutex_unlock(&gspca_dev
->queue_lock
);
1767 * allocate the resources for read()
1769 static int read_alloc(struct gspca_dev
*gspca_dev
,
1772 struct v4l2_buffer v4l2_buf
;
1775 PDEBUG(D_STREAM
, "read alloc");
1776 if (gspca_dev
->nframes
== 0) {
1777 struct v4l2_requestbuffers rb
;
1779 memset(&rb
, 0, sizeof rb
);
1780 rb
.count
= gspca_dev
->nbufread
;
1781 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1782 rb
.memory
= GSPCA_MEMORY_READ
;
1783 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1785 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1788 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1789 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1790 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1791 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1793 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1795 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1799 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1802 /* start streaming */
1803 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1805 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1809 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1811 struct gspca_dev
*gspca_dev
= file
->private_data
;
1814 PDEBUG(D_FRAM
, "poll");
1816 poll_wait(file
, &gspca_dev
->wq
, wait
);
1818 /* if reqbufs is not done, the user would use read() */
1819 if (gspca_dev
->nframes
== 0) {
1820 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
1821 return POLLERR
; /* not the 1st time */
1822 ret
= read_alloc(gspca_dev
, file
);
1827 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1830 /* check the next incoming buffer */
1831 i
= gspca_dev
->fr_o
;
1832 i
= gspca_dev
->fr_queue
[i
];
1833 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1834 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1837 mutex_unlock(&gspca_dev
->queue_lock
);
1838 if (!gspca_dev
->present
)
1843 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1844 size_t count
, loff_t
*ppos
)
1846 struct gspca_dev
*gspca_dev
= file
->private_data
;
1847 struct gspca_frame
*frame
;
1848 struct v4l2_buffer v4l2_buf
;
1849 struct timeval timestamp
;
1852 PDEBUG(D_FRAM
, "read (%zd)", count
);
1853 if (!gspca_dev
->present
)
1855 switch (gspca_dev
->memory
) {
1856 case GSPCA_MEMORY_NO
: /* first time */
1857 ret
= read_alloc(gspca_dev
, file
);
1861 case GSPCA_MEMORY_READ
:
1862 if (gspca_dev
->capt_file
== file
)
1870 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1874 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1875 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1876 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1877 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1879 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1883 /* if the process slept for more than 1 second,
1884 * get a newer frame */
1885 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1887 break; /* avoid infinite loop */
1888 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1890 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1892 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1897 /* copy the frame */
1898 if (count
> frame
->v4l2_buf
.bytesused
)
1899 count
= frame
->v4l2_buf
.bytesused
;
1900 ret
= copy_to_user(data
, frame
->data
, count
);
1902 PDEBUG(D_ERR
|D_STREAM
,
1903 "read cp to user lack %d / %zd", ret
, count
);
1909 /* in each case, requeue the buffer */
1910 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1916 static struct v4l2_file_operations dev_fops
= {
1917 .owner
= THIS_MODULE
,
1919 .release
= dev_close
,
1922 .unlocked_ioctl
= video_ioctl2
,
1926 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1927 .vidioc_querycap
= vidioc_querycap
,
1928 .vidioc_dqbuf
= vidioc_dqbuf
,
1929 .vidioc_qbuf
= vidioc_qbuf
,
1930 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1931 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1932 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1933 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1934 .vidioc_streamon
= vidioc_streamon
,
1935 .vidioc_queryctrl
= vidioc_queryctrl
,
1936 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1937 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1938 .vidioc_g_audio
= vidioc_g_audio
,
1939 .vidioc_s_audio
= vidioc_s_audio
,
1940 .vidioc_enumaudio
= vidioc_enumaudio
,
1941 .vidioc_querymenu
= vidioc_querymenu
,
1942 .vidioc_enum_input
= vidioc_enum_input
,
1943 .vidioc_g_input
= vidioc_g_input
,
1944 .vidioc_s_input
= vidioc_s_input
,
1945 .vidioc_reqbufs
= vidioc_reqbufs
,
1946 .vidioc_querybuf
= vidioc_querybuf
,
1947 .vidioc_streamoff
= vidioc_streamoff
,
1948 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1949 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1950 .vidioc_g_parm
= vidioc_g_parm
,
1951 .vidioc_s_parm
= vidioc_s_parm
,
1952 .vidioc_s_std
= vidioc_s_std
,
1953 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1954 #ifdef CONFIG_VIDEO_ADV_DEBUG
1955 .vidioc_g_register
= vidioc_g_register
,
1956 .vidioc_s_register
= vidioc_s_register
,
1958 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1959 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1960 .vidiocgmbuf
= vidiocgmbuf
,
1964 static struct video_device gspca_template
= {
1965 .name
= "gspca main driver",
1967 .ioctl_ops
= &dev_ioctl_ops
,
1968 .release
= gspca_release
,
1973 * probe and create a new gspca device
1975 * This function must be called by the sub-driver when it is
1976 * called for probing a new device.
1978 int gspca_dev_probe(struct usb_interface
*intf
,
1979 const struct usb_device_id
*id
,
1980 const struct sd_desc
*sd_desc
,
1982 struct module
*module
)
1984 struct usb_interface_descriptor
*interface
;
1985 struct gspca_dev
*gspca_dev
;
1986 struct usb_device
*dev
= interface_to_usbdev(intf
);
1989 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1991 /* we don't handle multi-config cameras */
1992 if (dev
->descriptor
.bNumConfigurations
!= 1)
1994 interface
= &intf
->cur_altsetting
->desc
;
1995 if (interface
->bInterfaceNumber
> 0)
1998 /* create the device */
1999 if (dev_size
< sizeof *gspca_dev
)
2000 dev_size
= sizeof *gspca_dev
;
2001 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2003 err("couldn't kzalloc gspca struct");
2006 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2007 if (!gspca_dev
->usb_buf
) {
2008 err("out of memory");
2012 gspca_dev
->dev
= dev
;
2013 gspca_dev
->iface
= interface
->bInterfaceNumber
;
2014 gspca_dev
->nbalt
= intf
->num_altsetting
;
2015 gspca_dev
->sd_desc
= sd_desc
;
2016 gspca_dev
->nbufread
= 2;
2017 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2019 /* configure the subdriver and initialize the USB device */
2020 ret
= sd_desc
->config(gspca_dev
, id
);
2023 ret
= sd_desc
->init(gspca_dev
);
2026 ret
= gspca_set_alt0(gspca_dev
);
2029 gspca_set_default_mode(gspca_dev
);
2031 mutex_init(&gspca_dev
->usb_lock
);
2032 mutex_init(&gspca_dev
->read_lock
);
2033 mutex_init(&gspca_dev
->queue_lock
);
2034 init_waitqueue_head(&gspca_dev
->wq
);
2036 /* init video stuff */
2037 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
2038 gspca_dev
->vdev
.parent
= &intf
->dev
;
2039 gspca_dev
->module
= module
;
2040 gspca_dev
->present
= 1;
2041 ret
= video_register_device(&gspca_dev
->vdev
,
2045 err("video_register_device err %d", ret
);
2049 usb_set_intfdata(intf
, gspca_dev
);
2050 PDEBUG(D_PROBE
, "probe ok");
2053 kfree(gspca_dev
->usb_buf
);
2057 EXPORT_SYMBOL(gspca_dev_probe
);
2062 * This function must be called by the sub-driver
2063 * when the device disconnects, after the specific resources are freed.
2065 void gspca_disconnect(struct usb_interface
*intf
)
2067 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2069 mutex_lock(&gspca_dev
->usb_lock
);
2070 gspca_dev
->present
= 0;
2072 if (gspca_dev
->streaming
) {
2073 destroy_urbs(gspca_dev
);
2074 wake_up_interruptible(&gspca_dev
->wq
);
2077 /* the device is freed at exit of this function */
2078 gspca_dev
->dev
= NULL
;
2079 mutex_unlock(&gspca_dev
->usb_lock
);
2081 usb_set_intfdata(intf
, NULL
);
2083 /* release the device */
2084 /* (this will call gspca_release() immediatly or on last close) */
2085 video_unregister_device(&gspca_dev
->vdev
);
2087 PDEBUG(D_PROBE
, "disconnect complete");
2089 EXPORT_SYMBOL(gspca_disconnect
);
2092 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2094 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2096 if (!gspca_dev
->streaming
)
2098 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2099 if (gspca_dev
->sd_desc
->stopN
)
2100 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2101 destroy_urbs(gspca_dev
);
2102 gspca_set_alt0(gspca_dev
);
2103 if (gspca_dev
->sd_desc
->stop0
)
2104 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2107 EXPORT_SYMBOL(gspca_suspend
);
2109 int gspca_resume(struct usb_interface
*intf
)
2111 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2113 gspca_dev
->frozen
= 0;
2114 gspca_dev
->sd_desc
->init(gspca_dev
);
2115 if (gspca_dev
->streaming
)
2116 return gspca_init_transfer(gspca_dev
);
2119 EXPORT_SYMBOL(gspca_resume
);
2121 /* -- cam driver utility functions -- */
2123 /* auto gain and exposure algorithm based on the knee algorithm described here:
2124 http://ytse.tricolour.net/docs/LowLightOptimization.html
2126 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2128 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2129 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2131 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2132 const struct ctrl
*gain_ctrl
= NULL
;
2133 const struct ctrl
*exposure_ctrl
= NULL
;
2134 const struct ctrl
*autogain_ctrl
= NULL
;
2137 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2138 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2139 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2140 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2141 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2142 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2143 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2145 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2146 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2147 "on cam without (auto)gain/exposure");
2151 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2152 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2153 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2157 orig_exposure
= exposure
;
2159 /* If we are of a multiple of deadzone, do multiple steps to reach the
2160 desired lumination fast (with the risc of a slight overshoot) */
2161 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2163 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2164 avg_lum
, desired_avg_lum
, steps
);
2166 for (i
= 0; i
< steps
; i
++) {
2167 if (avg_lum
> desired_avg_lum
) {
2168 if (gain
> gain_knee
)
2170 else if (exposure
> exposure_knee
)
2172 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2174 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2176 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2181 if (gain
< gain_ctrl
->qctrl
.default_value
)
2183 else if (exposure
< exposure_knee
)
2185 else if (gain
< gain_knee
)
2187 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2189 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2196 if (gain
!= orig_gain
) {
2197 gain_ctrl
->set(gspca_dev
, gain
);
2200 if (exposure
!= orig_exposure
) {
2201 exposure_ctrl
->set(gspca_dev
, exposure
);
2207 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2209 /* -- module insert / remove -- */
2210 static int __init
gspca_init(void)
2212 info("main v%d.%d.%d registered",
2213 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2214 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2215 DRIVER_VERSION_NUMBER
& 0xff);
2218 static void __exit
gspca_exit(void)
2220 info("main deregistered");
2223 module_init(gspca_init
);
2224 module_exit(gspca_exit
);
2227 module_param_named(debug
, gspca_debug
, int, 0644);
2228 MODULE_PARM_DESC(debug
,
2229 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2230 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"