2 * Main USB camera driver
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <media/v4l2-ioctl.h>
40 #define DEF_NURBS 2 /* default number of URBs */
42 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
48 static int video_nr
= -1;
51 int gspca_debug
= D_ERR
| D_PROBE
;
52 EXPORT_SYMBOL(gspca_debug
);
54 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
56 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
57 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
61 (pixfmt
>> 16) & 0xff,
65 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
72 #define PDEBUG_MODE(txt, pixfmt, w, h)
75 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
76 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
77 #define GSPCA_MEMORY_READ 7
79 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
84 static void gspca_vm_open(struct vm_area_struct
*vma
)
86 struct gspca_frame
*frame
= vma
->vm_private_data
;
88 frame
->vma_use_count
++;
89 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
92 static void gspca_vm_close(struct vm_area_struct
*vma
)
94 struct gspca_frame
*frame
= vma
->vm_private_data
;
96 if (--frame
->vma_use_count
<= 0)
97 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
100 static struct vm_operations_struct gspca_vm_ops
= {
101 .open
= gspca_vm_open
,
102 .close
= gspca_vm_close
,
105 /* get the current input frame buffer */
106 struct gspca_frame
*gspca_get_i_frame(struct gspca_dev
*gspca_dev
)
108 struct gspca_frame
*frame
;
112 i
= gspca_dev
->fr_queue
[i
];
113 frame
= &gspca_dev
->frame
[i
];
114 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
115 != V4L2_BUF_FLAG_QUEUED
)
119 EXPORT_SYMBOL(gspca_get_i_frame
);
122 * fill a video frame from an URB and resubmit
124 static void fill_frame(struct gspca_dev
*gspca_dev
,
127 struct gspca_frame
*frame
;
128 __u8
*data
; /* address of data in the iso message */
132 if (urb
->status
!= 0) {
134 if (!gspca_dev
->frozen
)
136 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
137 return; /* disconnection ? */
139 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
140 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
142 /* check the availability of the frame buffer */
143 frame
= gspca_get_i_frame(gspca_dev
);
145 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
149 /* check the packet status and length */
150 len
= urb
->iso_frame_desc
[i
].actual_length
;
153 st
= urb
->iso_frame_desc
[i
].status
;
156 "ISOC data error: [%d] len=%d, status=%d",
158 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
162 /* let the packet be analyzed by the subdriver */
163 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
164 i
, urb
->iso_frame_desc
[i
].offset
, len
);
165 data
= (__u8
*) urb
->transfer_buffer
166 + urb
->iso_frame_desc
[i
].offset
;
167 pkt_scan(gspca_dev
, frame
, data
, len
);
170 /* resubmit the URB */
172 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
174 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
178 * ISOC message interrupt from the USB device
180 * Analyse each packet and call the subdriver for copy to the frame buffer.
182 static void isoc_irq(struct urb
*urb
185 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
187 PDEBUG(D_PACK
, "isoc irq");
188 if (!gspca_dev
->streaming
)
190 fill_frame(gspca_dev
, urb
);
194 * bulk message interrupt from the USB device
196 static void bulk_irq(struct urb
*urb
199 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
200 struct gspca_frame
*frame
;
202 PDEBUG(D_PACK
, "bulk irq");
203 if (!gspca_dev
->streaming
)
205 if (urb
->status
!= 0 && urb
->status
!= -ECONNRESET
) {
207 if (!gspca_dev
->frozen
)
209 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
210 return; /* disconnection ? */
213 /* check the availability of the frame buffer */
214 frame
= gspca_get_i_frame(gspca_dev
);
216 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
218 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
219 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
221 urb
->transfer_buffer
,
227 * add data to the current frame
229 * This function is called by the subdrivers at interrupt level.
231 * To build a frame, these ones must add
233 * - 0 or many INTER_PACKETs
235 * DISCARD_PACKET invalidates the whole frame.
236 * On LAST_PACKET, a new frame is returned.
238 struct gspca_frame
*gspca_frame_add(struct gspca_dev
*gspca_dev
,
240 struct gspca_frame
*frame
,
246 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
248 /* when start of a new frame, if the current frame buffer
249 * is not queued, discard the whole frame */
250 if (packet_type
== FIRST_PACKET
) {
251 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
252 != V4L2_BUF_FLAG_QUEUED
) {
253 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
256 frame
->data_end
= frame
->data
;
257 jiffies_to_timeval(get_jiffies_64(),
258 &frame
->v4l2_buf
.timestamp
);
259 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
260 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
261 if (packet_type
== LAST_PACKET
)
262 gspca_dev
->last_packet_type
= packet_type
;
266 /* append the packet to the frame buffer */
268 if (frame
->data_end
- frame
->data
+ len
269 > frame
->v4l2_buf
.length
) {
270 PDEBUG(D_ERR
|D_PACK
, "frame overflow %zd > %d",
271 frame
->data_end
- frame
->data
+ len
,
272 frame
->v4l2_buf
.length
);
273 packet_type
= DISCARD_PACKET
;
275 memcpy(frame
->data_end
, data
, len
);
276 frame
->data_end
+= len
;
279 gspca_dev
->last_packet_type
= packet_type
;
281 /* if last packet, wake the application and advance in the queue */
282 if (packet_type
== LAST_PACKET
) {
283 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
284 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
285 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
286 atomic_inc(&gspca_dev
->nevent
);
287 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
288 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
290 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
291 frame
->v4l2_buf
.bytesused
,
295 j
= gspca_dev
->fr_queue
[i
];
296 frame
= &gspca_dev
->frame
[j
];
300 EXPORT_SYMBOL(gspca_frame_add
);
302 static int gspca_is_compressed(__u32 format
)
305 case V4L2_PIX_FMT_MJPEG
:
306 case V4L2_PIX_FMT_JPEG
:
307 case V4L2_PIX_FMT_SPCA561
:
308 case V4L2_PIX_FMT_PAC207
:
314 static void *rvmalloc(unsigned long size
)
319 /* size = PAGE_ALIGN(size); (already done) */
320 mem
= vmalloc_32(size
);
322 adr
= (unsigned long) mem
;
323 while ((long) size
> 0) {
324 SetPageReserved(vmalloc_to_page((void *) adr
));
332 static void rvfree(void *mem
, long size
)
336 adr
= (unsigned long) mem
;
338 ClearPageReserved(vmalloc_to_page((void *) adr
));
345 static int frame_alloc(struct gspca_dev
*gspca_dev
,
348 struct gspca_frame
*frame
;
352 i
= gspca_dev
->curr_mode
;
353 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
354 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
355 frsz
= PAGE_ALIGN(frsz
);
356 gspca_dev
->frsz
= frsz
;
357 if (count
> GSPCA_MAX_FRAMES
)
358 count
= GSPCA_MAX_FRAMES
;
359 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
360 if (!gspca_dev
->frbuf
) {
361 err("frame alloc failed");
364 gspca_dev
->nframes
= count
;
365 for (i
= 0; i
< count
; i
++) {
366 frame
= &gspca_dev
->frame
[i
];
367 frame
->v4l2_buf
.index
= i
;
368 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
369 frame
->v4l2_buf
.flags
= 0;
370 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
371 frame
->v4l2_buf
.length
= frsz
;
372 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
373 frame
->v4l2_buf
.sequence
= 0;
374 frame
->data
= frame
->data_end
=
375 gspca_dev
->frbuf
+ i
* frsz
;
376 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
378 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
379 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
380 gspca_dev
->sequence
= 0;
381 atomic_set(&gspca_dev
->nevent
, 0);
385 static void frame_free(struct gspca_dev
*gspca_dev
)
389 PDEBUG(D_STREAM
, "frame free");
390 if (gspca_dev
->frbuf
!= NULL
) {
391 rvfree(gspca_dev
->frbuf
,
392 gspca_dev
->nframes
* gspca_dev
->frsz
);
393 gspca_dev
->frbuf
= NULL
;
394 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
395 gspca_dev
->frame
[i
].data
= NULL
;
397 gspca_dev
->nframes
= 0;
400 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
405 PDEBUG(D_STREAM
, "kill transfer");
406 for (i
= 0; i
< MAX_NURBS
; ++i
) {
407 urb
= gspca_dev
->urb
[i
];
411 gspca_dev
->urb
[i
] = NULL
;
413 if (urb
->transfer_buffer
!= NULL
)
414 usb_buffer_free(gspca_dev
->dev
,
415 urb
->transfer_buffer_length
,
416 urb
->transfer_buffer
,
423 * search an input transfer endpoint in an alternate setting
425 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
429 struct usb_host_endpoint
*ep
;
432 epaddr
|= USB_DIR_IN
;
433 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
434 ep
= &alt
->endpoint
[i
];
435 if (ep
->desc
.bEndpointAddress
== epaddr
) {
436 attr
= ep
->desc
.bmAttributes
437 & USB_ENDPOINT_XFERTYPE_MASK
;
447 * search an input (isoc or bulk) endpoint
449 * The endpoint is defined by the subdriver.
450 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
451 * This routine may be called many times when the bandwidth is too small
452 * (the bandwidth is checked on urb submit).
454 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
456 struct usb_interface
*intf
;
457 struct usb_host_endpoint
*ep
;
460 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
462 i
= gspca_dev
->alt
; /* previous alt setting */
463 while (--i
> 0) { /* alt 0 is unusable */
464 ep
= alt_xfer(&intf
->altsetting
[i
],
465 gspca_dev
->cam
.epaddr
,
467 ? USB_ENDPOINT_XFER_BULK
468 : USB_ENDPOINT_XFER_ISOC
);
473 err("no transfer endpoint found");
476 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
477 i
, ep
->desc
.bEndpointAddress
);
478 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
480 err("set interface err %d", ret
);
483 gspca_dev
->alt
= i
; /* memorize the current alt setting */
488 * create the URBs for image transfer
490 static int create_urbs(struct gspca_dev
*gspca_dev
,
491 struct usb_host_endpoint
*ep
)
494 int n
, nurbs
, i
, psize
, npkt
, bsize
;
496 /* calculate the packet size and the number of packets */
497 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
499 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
500 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
501 if (!gspca_dev
->bulk
) {
502 npkt
= ISO_MAX_SIZE
/ psize
;
503 if (npkt
> ISO_MAX_PKT
)
505 bsize
= psize
* npkt
;
507 "isoc %d pkts size %d = bsize:%d",
513 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
517 gspca_dev
->nurbs
= nurbs
;
518 for (n
= 0; n
< nurbs
; n
++) {
519 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
521 err("usb_alloc_urb failed");
522 destroy_urbs(gspca_dev
);
525 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
530 if (urb
->transfer_buffer
== NULL
) {
532 err("usb_buffer_urb failed");
533 destroy_urbs(gspca_dev
);
536 gspca_dev
->urb
[n
] = urb
;
537 urb
->dev
= gspca_dev
->dev
;
538 urb
->context
= gspca_dev
;
539 urb
->transfer_buffer_length
= bsize
;
540 if (npkt
!= 0) { /* ISOC */
541 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
542 ep
->desc
.bEndpointAddress
);
543 urb
->transfer_flags
= URB_ISO_ASAP
;
544 urb
->interval
= ep
->desc
.bInterval
;
545 urb
->complete
= isoc_irq
;
546 urb
->number_of_packets
= npkt
;
547 for (i
= 0; i
< npkt
; i
++) {
548 urb
->iso_frame_desc
[i
].length
= psize
;
549 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
552 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
553 ep
->desc
.bEndpointAddress
),
554 urb
->complete
= bulk_irq
;
561 * start the USB transfer
563 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
565 struct usb_host_endpoint
*ep
;
568 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
571 /* set the higher alternate setting and
572 * loop until urb submit succeeds */
573 gspca_dev
->alt
= gspca_dev
->nbalt
;
575 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
576 ep
= get_ep(gspca_dev
);
581 ret
= create_urbs(gspca_dev
, ep
);
586 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
588 destroy_urbs(gspca_dev
);
591 gspca_dev
->streaming
= 1;
592 atomic_set(&gspca_dev
->nevent
, 0);
594 /* start the bulk transfer is done by the subdriver */
598 /* submit the URBs */
599 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
600 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
602 PDEBUG(D_ERR
|D_STREAM
,
603 "usb_submit_urb [%d] err %d", n
, ret
);
604 gspca_dev
->streaming
= 0;
605 destroy_urbs(gspca_dev
);
607 break; /* try the previous alt */
615 mutex_unlock(&gspca_dev
->usb_lock
);
619 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
623 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
625 PDEBUG(D_ERR
|D_STREAM
, "set interface 0 err %d", ret
);
629 /* Note both the queue and the usb lock should be hold when calling this */
630 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
632 gspca_dev
->streaming
= 0;
633 atomic_set(&gspca_dev
->nevent
, 0);
634 if (gspca_dev
->present
) {
635 if (gspca_dev
->sd_desc
->stopN
)
636 gspca_dev
->sd_desc
->stopN(gspca_dev
);
637 destroy_urbs(gspca_dev
);
638 gspca_set_alt0(gspca_dev
);
639 if (gspca_dev
->sd_desc
->stop0
)
640 gspca_dev
->sd_desc
->stop0(gspca_dev
);
641 PDEBUG(D_STREAM
, "stream off OK");
645 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
649 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
650 gspca_dev
->curr_mode
= i
;
651 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
652 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
653 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
656 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
657 int width
, int height
)
661 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
662 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
663 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
670 * search a mode with the right pixel format
672 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
678 modeU
= modeD
= mode
;
679 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
681 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
685 if (++modeU
< gspca_dev
->cam
.nmodes
) {
686 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
694 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
695 struct v4l2_fmtdesc
*fmtdesc
)
697 struct gspca_dev
*gspca_dev
= priv
;
701 /* give an index to each format */
704 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
705 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
708 if (fmt_tb
[j
] == fmt_tb
[index
])
713 if (fmtdesc
->index
== index
)
714 break; /* new format */
716 if (index
>= sizeof fmt_tb
/ sizeof fmt_tb
[0])
721 return -EINVAL
; /* no more format */
723 fmtdesc
->pixelformat
= fmt_tb
[index
];
724 if (gspca_is_compressed(fmt_tb
[index
]))
725 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
726 fmtdesc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
727 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
728 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
729 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
730 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
731 fmtdesc
->description
[4] = '\0';
735 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
736 struct v4l2_format
*fmt
)
738 struct gspca_dev
*gspca_dev
= priv
;
741 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
743 mode
= gspca_dev
->curr_mode
;
744 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
745 sizeof fmt
->fmt
.pix
);
749 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
750 struct v4l2_format
*fmt
)
752 int w
, h
, mode
, mode2
;
754 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
756 w
= fmt
->fmt
.pix
.width
;
757 h
= fmt
->fmt
.pix
.height
;
760 if (gspca_debug
& D_CONF
)
761 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
763 /* search the closest mode for width and height */
764 mode
= wxh_to_mode(gspca_dev
, w
, h
);
766 /* OK if right palette */
767 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
768 != fmt
->fmt
.pix
.pixelformat
) {
770 /* else, search the closest mode with the same pixel format */
771 mode2
= gspca_get_mode(gspca_dev
, mode
,
772 fmt
->fmt
.pix
.pixelformat
);
776 ; * no chance, return this mode */
778 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
779 sizeof fmt
->fmt
.pix
);
780 return mode
; /* used when s_fmt */
783 static int vidioc_try_fmt_vid_cap(struct file
*file
,
785 struct v4l2_format
*fmt
)
787 struct gspca_dev
*gspca_dev
= priv
;
790 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
796 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
797 struct v4l2_format
*fmt
)
799 struct gspca_dev
*gspca_dev
= priv
;
802 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
805 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
809 if (gspca_dev
->nframes
!= 0
810 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
815 if (ret
== gspca_dev
->curr_mode
) {
817 goto out
; /* same mode */
820 if (gspca_dev
->streaming
) {
824 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
825 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
826 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
827 gspca_dev
->curr_mode
= ret
;
831 mutex_unlock(&gspca_dev
->queue_lock
);
835 static int dev_open(struct inode
*inode
, struct file
*file
)
837 struct gspca_dev
*gspca_dev
;
840 PDEBUG(D_STREAM
, "%s open", current
->comm
);
841 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
842 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
844 if (!gspca_dev
->present
) {
849 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
854 file
->private_data
= gspca_dev
;
856 /* activate the v4l2 debug */
857 if (gspca_debug
& D_V4L2
)
858 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
859 | V4L2_DEBUG_IOCTL_ARG
;
861 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
862 | V4L2_DEBUG_IOCTL_ARG
);
866 mutex_unlock(&gspca_dev
->queue_lock
);
868 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
870 PDEBUG(D_STREAM
, "open done");
874 static int dev_close(struct inode
*inode
, struct file
*file
)
876 struct gspca_dev
*gspca_dev
= file
->private_data
;
878 PDEBUG(D_STREAM
, "%s close", current
->comm
);
879 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
883 /* if the file did the capture, free the streaming resources */
884 if (gspca_dev
->capt_file
== file
) {
885 if (gspca_dev
->streaming
) {
886 mutex_lock(&gspca_dev
->usb_lock
);
887 gspca_stream_off(gspca_dev
);
888 mutex_unlock(&gspca_dev
->usb_lock
);
890 frame_free(gspca_dev
);
891 gspca_dev
->capt_file
= NULL
;
892 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
894 file
->private_data
= NULL
;
895 mutex_unlock(&gspca_dev
->queue_lock
);
896 PDEBUG(D_STREAM
, "close done");
900 static int vidioc_querycap(struct file
*file
, void *priv
,
901 struct v4l2_capability
*cap
)
903 struct gspca_dev
*gspca_dev
= priv
;
905 memset(cap
, 0, sizeof *cap
);
906 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
907 /* strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); */
908 if (gspca_dev
->dev
->product
!= NULL
) {
909 strncpy(cap
->card
, gspca_dev
->dev
->product
,
912 snprintf(cap
->card
, sizeof cap
->card
,
913 "USB Camera (%04x:%04x)",
914 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
915 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
917 strncpy(cap
->bus_info
, gspca_dev
->dev
->bus
->bus_name
,
918 sizeof cap
->bus_info
);
919 cap
->version
= DRIVER_VERSION_NUMBER
;
920 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
922 | V4L2_CAP_READWRITE
;
926 static int vidioc_queryctrl(struct file
*file
, void *priv
,
927 struct v4l2_queryctrl
*q_ctrl
)
929 struct gspca_dev
*gspca_dev
= priv
;
935 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
936 id
&= V4L2_CTRL_ID_MASK
;
938 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
939 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
945 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
946 > gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
.id
)
951 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
952 if (id
== gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
959 memcpy(q_ctrl
, &gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
,
961 if (gspca_dev
->ctrl_dis
& (1 << ix
))
962 q_ctrl
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
966 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
967 struct v4l2_control
*ctrl
)
969 struct gspca_dev
*gspca_dev
= priv
;
970 const struct ctrl
*ctrls
;
973 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
974 i
< gspca_dev
->sd_desc
->nctrls
;
976 if (ctrl
->id
!= ctrls
->qctrl
.id
)
978 if (gspca_dev
->ctrl_dis
& (1 << i
))
980 if (ctrl
->value
< ctrls
->qctrl
.minimum
981 || ctrl
->value
> ctrls
->qctrl
.maximum
)
983 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
984 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
986 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
987 mutex_unlock(&gspca_dev
->usb_lock
);
993 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
994 struct v4l2_control
*ctrl
)
996 struct gspca_dev
*gspca_dev
= priv
;
998 const struct ctrl
*ctrls
;
1001 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1002 i
< gspca_dev
->sd_desc
->nctrls
;
1004 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1006 if (gspca_dev
->ctrl_dis
& (1 << i
))
1008 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1009 return -ERESTARTSYS
;
1010 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1011 mutex_unlock(&gspca_dev
->usb_lock
);
1017 static int vidioc_querymenu(struct file
*file
, void *priv
,
1018 struct v4l2_querymenu
*qmenu
)
1020 struct gspca_dev
*gspca_dev
= priv
;
1022 if (!gspca_dev
->sd_desc
->querymenu
)
1024 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1027 static int vidioc_enum_input(struct file
*file
, void *priv
,
1028 struct v4l2_input
*input
)
1030 struct gspca_dev
*gspca_dev
= priv
;
1032 if (input
->index
!= 0)
1034 memset(input
, 0, sizeof *input
);
1035 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1036 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1037 sizeof input
->name
);
1041 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1047 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1054 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1055 struct v4l2_requestbuffers
*rb
)
1057 struct gspca_dev
*gspca_dev
= priv
;
1060 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1062 switch (rb
->memory
) {
1063 case GSPCA_MEMORY_READ
: /* (internal call) */
1064 case V4L2_MEMORY_MMAP
:
1065 case V4L2_MEMORY_USERPTR
:
1070 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1071 return -ERESTARTSYS
;
1073 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1074 && gspca_dev
->memory
!= rb
->memory
) {
1079 /* only one file may do the capture */
1080 if (gspca_dev
->capt_file
!= NULL
1081 && gspca_dev
->capt_file
!= file
) {
1086 /* if allocated, the buffers must not be mapped */
1087 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1088 if (gspca_dev
->frame
[i
].vma_use_count
) {
1094 /* stop streaming */
1095 if (gspca_dev
->streaming
) {
1096 mutex_lock(&gspca_dev
->usb_lock
);
1097 gspca_stream_off(gspca_dev
);
1098 mutex_unlock(&gspca_dev
->usb_lock
);
1101 /* free the previous allocated buffers, if any */
1102 if (gspca_dev
->nframes
!= 0) {
1103 frame_free(gspca_dev
);
1104 gspca_dev
->capt_file
= NULL
;
1106 if (rb
->count
== 0) /* unrequest */
1108 gspca_dev
->memory
= rb
->memory
;
1109 ret
= frame_alloc(gspca_dev
, rb
->count
);
1111 rb
->count
= gspca_dev
->nframes
;
1112 gspca_dev
->capt_file
= file
;
1115 mutex_unlock(&gspca_dev
->queue_lock
);
1116 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1120 static int vidioc_querybuf(struct file
*file
, void *priv
,
1121 struct v4l2_buffer
*v4l2_buf
)
1123 struct gspca_dev
*gspca_dev
= priv
;
1124 struct gspca_frame
*frame
;
1126 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1127 || v4l2_buf
->index
< 0
1128 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1131 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1132 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1136 static int vidioc_streamon(struct file
*file
, void *priv
,
1137 enum v4l2_buf_type buf_type
)
1139 struct gspca_dev
*gspca_dev
= priv
;
1142 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1144 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1145 return -ERESTARTSYS
;
1146 if (!gspca_dev
->present
) {
1150 if (gspca_dev
->nframes
== 0) {
1154 if (!gspca_dev
->streaming
) {
1155 ret
= gspca_init_transfer(gspca_dev
);
1160 if (gspca_debug
& D_STREAM
) {
1161 PDEBUG_MODE("stream on OK",
1169 mutex_unlock(&gspca_dev
->queue_lock
);
1173 static int vidioc_streamoff(struct file
*file
, void *priv
,
1174 enum v4l2_buf_type buf_type
)
1176 struct gspca_dev
*gspca_dev
= priv
;
1179 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1181 if (!gspca_dev
->streaming
)
1183 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1184 return -ERESTARTSYS
;
1186 /* stop streaming */
1187 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1191 gspca_stream_off(gspca_dev
);
1192 mutex_unlock(&gspca_dev
->usb_lock
);
1194 /* empty the application queues */
1195 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1196 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1197 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1198 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1199 gspca_dev
->sequence
= 0;
1200 atomic_set(&gspca_dev
->nevent
, 0);
1203 mutex_unlock(&gspca_dev
->queue_lock
);
1207 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1208 struct v4l2_jpegcompression
*jpegcomp
)
1210 struct gspca_dev
*gspca_dev
= priv
;
1213 if (!gspca_dev
->sd_desc
->get_jcomp
)
1215 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1216 return -ERESTARTSYS
;
1217 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1218 mutex_unlock(&gspca_dev
->usb_lock
);
1222 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1223 struct v4l2_jpegcompression
*jpegcomp
)
1225 struct gspca_dev
*gspca_dev
= priv
;
1228 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1229 return -ERESTARTSYS
;
1230 if (!gspca_dev
->sd_desc
->set_jcomp
)
1232 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1233 mutex_unlock(&gspca_dev
->usb_lock
);
1237 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1238 struct v4l2_streamparm
*parm
)
1240 struct gspca_dev
*gspca_dev
= priv
;
1242 memset(parm
, 0, sizeof *parm
);
1243 parm
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1244 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1248 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1249 struct v4l2_streamparm
*parm
)
1251 struct gspca_dev
*gspca_dev
= priv
;
1254 n
= parm
->parm
.capture
.readbuffers
;
1255 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1256 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1258 gspca_dev
->nbufread
= n
;
1262 static int vidioc_s_std(struct file
*filp
, void *priv
,
1268 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1269 static int vidiocgmbuf(struct file
*file
, void *priv
,
1270 struct video_mbuf
*mbuf
)
1272 struct gspca_dev
*gspca_dev
= file
->private_data
;
1275 PDEBUG(D_STREAM
, "cgmbuf");
1276 if (gspca_dev
->nframes
== 0) {
1280 struct v4l2_format fmt
;
1282 memset(&fmt
, 0, sizeof fmt
);
1283 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1284 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1285 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1286 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1287 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1288 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1293 struct v4l2_requestbuffers rb
;
1295 memset(&rb
, 0, sizeof rb
);
1297 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1298 rb
.memory
= V4L2_MEMORY_MMAP
;
1299 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1304 mbuf
->frames
= gspca_dev
->nframes
;
1305 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1306 for (i
= 0; i
< mbuf
->frames
; i
++)
1307 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1312 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1314 struct gspca_dev
*gspca_dev
= file
->private_data
;
1315 struct gspca_frame
*frame
;
1317 unsigned long addr
, start
, size
;
1320 start
= vma
->vm_start
;
1321 size
= vma
->vm_end
- vma
->vm_start
;
1322 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1324 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1325 return -ERESTARTSYS
;
1326 if (!gspca_dev
->present
) {
1330 if (gspca_dev
->capt_file
!= file
) {
1336 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1337 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1338 PDEBUG(D_STREAM
, "mmap bad memory type");
1341 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1343 frame
= &gspca_dev
->frame
[i
];
1347 if (frame
== NULL
) {
1348 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1352 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1353 /* v4l1 maps all the buffers */
1355 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1357 if (size
!= frame
->v4l2_buf
.length
) {
1358 PDEBUG(D_STREAM
, "mmap bad size");
1364 * - VM_IO marks the area as being a mmaped region for I/O to a
1365 * device. It also prevents the region from being core dumped.
1367 vma
->vm_flags
|= VM_IO
;
1369 addr
= (unsigned long) frame
->data
;
1371 page
= vmalloc_to_page((void *) addr
);
1372 ret
= vm_insert_page(vma
, start
, page
);
1380 vma
->vm_ops
= &gspca_vm_ops
;
1381 vma
->vm_private_data
= frame
;
1385 mutex_unlock(&gspca_dev
->queue_lock
);
1390 * wait for a video frame
1392 * If a frame is ready, its index is returned.
1394 static int frame_wait(struct gspca_dev
*gspca_dev
,
1397 struct gspca_frame
*frame
;
1400 /* check if a frame is ready */
1401 i
= gspca_dev
->fr_o
;
1402 j
= gspca_dev
->fr_queue
[i
];
1403 frame
= &gspca_dev
->frame
[j
];
1404 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) {
1405 atomic_dec(&gspca_dev
->nevent
);
1408 if (nonblock_ing
) /* no frame yet */
1411 /* wait till a frame is ready */
1413 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1414 atomic_read(&gspca_dev
->nevent
) > 0,
1415 msecs_to_jiffies(3000));
1418 return ret
; /* interrupt */
1419 return -EIO
; /* timeout */
1421 atomic_dec(&gspca_dev
->nevent
);
1422 if (!gspca_dev
->streaming
|| !gspca_dev
->present
)
1424 i
= gspca_dev
->fr_o
;
1425 j
= gspca_dev
->fr_queue
[i
];
1426 frame
= &gspca_dev
->frame
[j
];
1427 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1431 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1432 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1437 if (gspca_dev
->sd_desc
->dq_callback
) {
1438 mutex_lock(&gspca_dev
->usb_lock
);
1439 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1440 mutex_unlock(&gspca_dev
->usb_lock
);
1446 * dequeue a video buffer
1448 * If nonblock_ing is false, block until a buffer is available.
1450 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1451 struct v4l2_buffer
*v4l2_buf
)
1453 struct gspca_dev
*gspca_dev
= priv
;
1454 struct gspca_frame
*frame
;
1457 PDEBUG(D_FRAM
, "dqbuf");
1458 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1460 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1463 /* if not streaming, be sure the application will not loop forever */
1464 if (!(file
->f_flags
& O_NONBLOCK
)
1465 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1468 /* only the capturing file may dequeue */
1469 if (gspca_dev
->capt_file
!= file
)
1472 /* only one dequeue / read at a time */
1473 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1474 return -ERESTARTSYS
;
1476 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1479 i
= ret
; /* frame index */
1480 frame
= &gspca_dev
->frame
[i
];
1481 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1482 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1484 frame
->v4l2_buf
.bytesused
)) {
1485 PDEBUG(D_ERR
|D_STREAM
,
1486 "dqbuf cp to user failed");
1491 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1492 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1493 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1496 mutex_unlock(&gspca_dev
->read_lock
);
1501 * queue a video buffer
1503 * Attempting to queue a buffer that has already been
1504 * queued will return -EINVAL.
1506 static int vidioc_qbuf(struct file
*file
, void *priv
,
1507 struct v4l2_buffer
*v4l2_buf
)
1509 struct gspca_dev
*gspca_dev
= priv
;
1510 struct gspca_frame
*frame
;
1513 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1514 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1517 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1518 return -ERESTARTSYS
;
1520 index
= v4l2_buf
->index
;
1521 if ((unsigned) index
>= gspca_dev
->nframes
) {
1523 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1527 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1528 PDEBUG(D_FRAM
, "qbuf bad memory type");
1533 frame
= &gspca_dev
->frame
[index
];
1534 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1535 PDEBUG(D_FRAM
, "qbuf bad state");
1540 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1541 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1543 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1544 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1545 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1548 /* put the buffer in the 'queued' queue */
1549 i
= gspca_dev
->fr_q
;
1550 gspca_dev
->fr_queue
[i
] = index
;
1551 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1552 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1557 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1558 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1561 mutex_unlock(&gspca_dev
->queue_lock
);
1566 * allocate the resources for read()
1568 static int read_alloc(struct gspca_dev
*gspca_dev
,
1571 struct v4l2_buffer v4l2_buf
;
1574 PDEBUG(D_STREAM
, "read alloc");
1575 if (gspca_dev
->nframes
== 0) {
1576 struct v4l2_requestbuffers rb
;
1578 memset(&rb
, 0, sizeof rb
);
1579 rb
.count
= gspca_dev
->nbufread
;
1580 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1581 rb
.memory
= GSPCA_MEMORY_READ
;
1582 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1584 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1587 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1588 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1589 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1590 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1592 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1594 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1598 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1601 /* start streaming */
1602 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1604 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1608 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1610 struct gspca_dev
*gspca_dev
= file
->private_data
;
1613 PDEBUG(D_FRAM
, "poll");
1615 poll_wait(file
, &gspca_dev
->wq
, wait
);
1616 if (!gspca_dev
->present
)
1619 /* if reqbufs is not done, the user would use read() */
1620 if (gspca_dev
->nframes
== 0) {
1621 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
1622 return POLLERR
; /* not the 1st time */
1623 ret
= read_alloc(gspca_dev
, file
);
1628 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1630 if (!gspca_dev
->present
) {
1635 /* check the next incoming buffer */
1636 i
= gspca_dev
->fr_o
;
1637 i
= gspca_dev
->fr_queue
[i
];
1638 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1639 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1643 mutex_unlock(&gspca_dev
->queue_lock
);
1647 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1648 size_t count
, loff_t
*ppos
)
1650 struct gspca_dev
*gspca_dev
= file
->private_data
;
1651 struct gspca_frame
*frame
;
1652 struct v4l2_buffer v4l2_buf
;
1653 struct timeval timestamp
;
1656 PDEBUG(D_FRAM
, "read (%zd)", count
);
1657 if (!gspca_dev
->present
)
1659 switch (gspca_dev
->memory
) {
1660 case GSPCA_MEMORY_NO
: /* first time */
1661 ret
= read_alloc(gspca_dev
, file
);
1665 case GSPCA_MEMORY_READ
:
1666 if (gspca_dev
->capt_file
== file
)
1674 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1678 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1679 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1680 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1681 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1683 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1687 /* if the process slept for more than 1 second,
1688 * get anewer frame */
1689 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1691 break; /* avoid infinite loop */
1692 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1694 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1696 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1701 /* copy the frame */
1702 if (count
> frame
->v4l2_buf
.bytesused
)
1703 count
= frame
->v4l2_buf
.bytesused
;
1704 ret
= copy_to_user(data
, frame
->data
, count
);
1706 PDEBUG(D_ERR
|D_STREAM
,
1707 "read cp to user lack %d / %zd", ret
, count
);
1713 /* in each case, requeue the buffer */
1714 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1720 static void dev_release(struct video_device
*vfd
)
1725 static struct file_operations dev_fops
= {
1726 .owner
= THIS_MODULE
,
1728 .release
= dev_close
,
1731 .ioctl
= video_ioctl2
,
1732 #ifdef CONFIG_COMPAT
1733 .compat_ioctl
= v4l_compat_ioctl32
,
1735 .llseek
= no_llseek
,
1739 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1740 .vidioc_querycap
= vidioc_querycap
,
1741 .vidioc_dqbuf
= vidioc_dqbuf
,
1742 .vidioc_qbuf
= vidioc_qbuf
,
1743 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1744 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1745 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1746 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1747 .vidioc_streamon
= vidioc_streamon
,
1748 .vidioc_queryctrl
= vidioc_queryctrl
,
1749 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1750 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1751 .vidioc_querymenu
= vidioc_querymenu
,
1752 .vidioc_enum_input
= vidioc_enum_input
,
1753 .vidioc_g_input
= vidioc_g_input
,
1754 .vidioc_s_input
= vidioc_s_input
,
1755 .vidioc_reqbufs
= vidioc_reqbufs
,
1756 .vidioc_querybuf
= vidioc_querybuf
,
1757 .vidioc_streamoff
= vidioc_streamoff
,
1758 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1759 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1760 .vidioc_g_parm
= vidioc_g_parm
,
1761 .vidioc_s_parm
= vidioc_s_parm
,
1762 .vidioc_s_std
= vidioc_s_std
,
1763 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1764 .vidiocgmbuf
= vidiocgmbuf
,
1768 static struct video_device gspca_template
= {
1769 .name
= "gspca main driver",
1771 .ioctl_ops
= &dev_ioctl_ops
,
1772 .release
= dev_release
, /* mandatory */
1777 * probe and create a new gspca device
1779 * This function must be called by the sub-driver when it is
1780 * called for probing a new device.
1782 int gspca_dev_probe(struct usb_interface
*intf
,
1783 const struct usb_device_id
*id
,
1784 const struct sd_desc
*sd_desc
,
1786 struct module
*module
)
1788 struct usb_interface_descriptor
*interface
;
1789 struct gspca_dev
*gspca_dev
;
1790 struct usb_device
*dev
= interface_to_usbdev(intf
);
1793 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1795 /* we don't handle multi-config cameras */
1796 if (dev
->descriptor
.bNumConfigurations
!= 1)
1798 interface
= &intf
->cur_altsetting
->desc
;
1799 if (interface
->bInterfaceNumber
> 0)
1802 /* create the device */
1803 if (dev_size
< sizeof *gspca_dev
)
1804 dev_size
= sizeof *gspca_dev
;
1805 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1806 if (gspca_dev
== NULL
) {
1807 err("couldn't kzalloc gspca struct");
1810 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
1811 if (!gspca_dev
->usb_buf
) {
1812 err("out of memory");
1816 gspca_dev
->dev
= dev
;
1817 gspca_dev
->iface
= interface
->bInterfaceNumber
;
1818 gspca_dev
->nbalt
= intf
->num_altsetting
;
1819 gspca_dev
->sd_desc
= sd_desc
;
1820 /* gspca_dev->users = 0; (done by kzalloc) */
1821 gspca_dev
->nbufread
= 2;
1823 /* configure the subdriver and initialize the USB device */
1824 ret
= gspca_dev
->sd_desc
->config(gspca_dev
, id
);
1827 ret
= gspca_dev
->sd_desc
->init(gspca_dev
);
1830 ret
= gspca_set_alt0(gspca_dev
);
1833 gspca_set_default_mode(gspca_dev
);
1835 mutex_init(&gspca_dev
->usb_lock
);
1836 mutex_init(&gspca_dev
->read_lock
);
1837 mutex_init(&gspca_dev
->queue_lock
);
1838 init_waitqueue_head(&gspca_dev
->wq
);
1840 /* init video stuff */
1841 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
1842 gspca_dev
->vdev
.parent
= &dev
->dev
;
1843 memcpy(&gspca_dev
->fops
, &dev_fops
, sizeof gspca_dev
->fops
);
1844 gspca_dev
->vdev
.fops
= &gspca_dev
->fops
;
1845 gspca_dev
->fops
.owner
= module
; /* module protection */
1846 gspca_dev
->present
= 1;
1847 ret
= video_register_device(&gspca_dev
->vdev
,
1851 err("video_register_device err %d", ret
);
1855 usb_set_intfdata(intf
, gspca_dev
);
1856 PDEBUG(D_PROBE
, "probe ok");
1859 kfree(gspca_dev
->usb_buf
);
1863 EXPORT_SYMBOL(gspca_dev_probe
);
1868 * This function must be called by the sub-driver
1869 * when the device disconnects, after the specific resources are freed.
1871 void gspca_disconnect(struct usb_interface
*intf
)
1873 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1877 gspca_dev
->present
= 0;
1878 mutex_lock(&gspca_dev
->queue_lock
);
1879 mutex_lock(&gspca_dev
->usb_lock
);
1880 gspca_dev
->streaming
= 0;
1881 destroy_urbs(gspca_dev
);
1882 mutex_unlock(&gspca_dev
->usb_lock
);
1883 mutex_unlock(&gspca_dev
->queue_lock
);
1884 while (gspca_dev
->users
!= 0) { /* wait until fully closed */
1885 atomic_inc(&gspca_dev
->nevent
);
1886 wake_up_interruptible(&gspca_dev
->wq
); /* wake processes */
1889 /* We don't want people trying to open up the device */
1890 video_unregister_device(&gspca_dev
->vdev
);
1891 /* Free the memory */
1892 kfree(gspca_dev
->usb_buf
);
1894 PDEBUG(D_PROBE
, "disconnect complete");
1896 EXPORT_SYMBOL(gspca_disconnect
);
1899 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1901 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1903 if (!gspca_dev
->streaming
)
1905 gspca_dev
->frozen
= 1; /* avoid urb error messages */
1906 if (gspca_dev
->sd_desc
->stopN
)
1907 gspca_dev
->sd_desc
->stopN(gspca_dev
);
1908 destroy_urbs(gspca_dev
);
1909 gspca_set_alt0(gspca_dev
);
1910 if (gspca_dev
->sd_desc
->stop0
)
1911 gspca_dev
->sd_desc
->stop0(gspca_dev
);
1914 EXPORT_SYMBOL(gspca_suspend
);
1916 int gspca_resume(struct usb_interface
*intf
)
1918 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1920 gspca_dev
->frozen
= 0;
1921 gspca_dev
->sd_desc
->init(gspca_dev
);
1922 if (gspca_dev
->streaming
)
1923 return gspca_init_transfer(gspca_dev
);
1926 EXPORT_SYMBOL(gspca_resume
);
1928 /* -- cam driver utility functions -- */
1930 /* auto gain and exposure algorithm based on the knee algorithm described here:
1931 http://ytse.tricolour.net/docs/LowLightOptimization.html
1933 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1935 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
1936 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
1938 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
1939 const struct ctrl
*gain_ctrl
= NULL
;
1940 const struct ctrl
*exposure_ctrl
= NULL
;
1941 const struct ctrl
*autogain_ctrl
= NULL
;
1944 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1945 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
1946 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1947 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
1948 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1949 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
1950 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1952 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
1953 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
1954 "on cam without (auto)gain/exposure");
1958 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
1959 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
1960 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
1964 orig_exposure
= exposure
;
1966 /* If we are of a multiple of deadzone, do multiple steps to reach the
1967 desired lumination fast (with the risc of a slight overshoot) */
1968 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
1970 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
1971 avg_lum
, desired_avg_lum
, steps
);
1973 for (i
= 0; i
< steps
; i
++) {
1974 if (avg_lum
> desired_avg_lum
) {
1975 if (gain
> gain_knee
)
1977 else if (exposure
> exposure_knee
)
1979 else if (gain
> gain_ctrl
->qctrl
.default_value
)
1981 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
1983 else if (gain
> gain_ctrl
->qctrl
.minimum
)
1988 if (gain
< gain_ctrl
->qctrl
.default_value
)
1990 else if (exposure
< exposure_knee
)
1992 else if (gain
< gain_knee
)
1994 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
1996 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2003 if (gain
!= orig_gain
) {
2004 gain_ctrl
->set(gspca_dev
, gain
);
2007 if (exposure
!= orig_exposure
) {
2008 exposure_ctrl
->set(gspca_dev
, exposure
);
2014 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2016 /* -- module insert / remove -- */
2017 static int __init
gspca_init(void)
2019 info("main v%d.%d.%d registered",
2020 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2021 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2022 DRIVER_VERSION_NUMBER
& 0xff);
2025 static void __exit
gspca_exit(void)
2027 info("main deregistered");
2030 module_init(gspca_init
);
2031 module_exit(gspca_exit
);
2034 module_param_named(debug
, gspca_debug
, int, 0644);
2035 MODULE_PARM_DESC(debug
,
2036 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2037 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"