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
;
459 * look for an input (isoc or bulk) endpoint
461 * The endpoint is defined by the subdriver.
462 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
463 * This routine may be called many times when the bandwidth is too small
464 * (the bandwidth is checked on urb submit).
466 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
468 struct usb_interface
*intf
;
469 struct usb_host_endpoint
*ep
;
472 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
474 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
475 : USB_ENDPOINT_XFER_ISOC
;
476 i
= gspca_dev
->alt
; /* previous alt setting */
478 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
483 err("no transfer endpoint found");
486 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
487 i
, ep
->desc
.bEndpointAddress
);
489 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
491 err("set alt %d err %d", i
, ret
);
495 gspca_dev
->alt
= i
; /* memorize the current alt setting */
500 * create the URBs for image transfer
502 static int create_urbs(struct gspca_dev
*gspca_dev
,
503 struct usb_host_endpoint
*ep
)
506 int n
, nurbs
, i
, psize
, npkt
, bsize
;
508 /* calculate the packet size and the number of packets */
509 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
511 if (!gspca_dev
->cam
.bulk
) { /* isoc */
513 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
514 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
515 npkt
= ISO_MAX_SIZE
/ psize
;
516 if (npkt
> ISO_MAX_PKT
)
518 bsize
= psize
* npkt
;
520 "isoc %d pkts size %d = bsize:%d",
525 bsize
= gspca_dev
->cam
.bulk_size
;
528 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
529 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
530 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
535 gspca_dev
->nurbs
= nurbs
;
536 for (n
= 0; n
< nurbs
; n
++) {
537 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
539 err("usb_alloc_urb failed");
540 destroy_urbs(gspca_dev
);
543 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
548 if (urb
->transfer_buffer
== NULL
) {
550 err("usb_buffer_urb failed");
551 destroy_urbs(gspca_dev
);
554 gspca_dev
->urb
[n
] = urb
;
555 urb
->dev
= gspca_dev
->dev
;
556 urb
->context
= gspca_dev
;
557 urb
->transfer_buffer_length
= bsize
;
558 if (npkt
!= 0) { /* ISOC */
559 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
560 ep
->desc
.bEndpointAddress
);
561 urb
->transfer_flags
= URB_ISO_ASAP
562 | URB_NO_TRANSFER_DMA_MAP
;
563 urb
->interval
= ep
->desc
.bInterval
;
564 urb
->complete
= isoc_irq
;
565 urb
->number_of_packets
= npkt
;
566 for (i
= 0; i
< npkt
; i
++) {
567 urb
->iso_frame_desc
[i
].length
= psize
;
568 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
571 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
572 ep
->desc
.bEndpointAddress
),
573 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
574 urb
->complete
= bulk_irq
;
581 * start the USB transfer
583 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
585 struct usb_host_endpoint
*ep
;
588 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
591 if (!gspca_dev
->present
) {
596 /* set the higher alternate setting and
597 * loop until urb submit succeeds */
598 gspca_dev
->alt
= gspca_dev
->nbalt
;
600 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
601 ep
= get_ep(gspca_dev
);
606 ret
= create_urbs(gspca_dev
, ep
);
610 /* clear the bulk endpoint */
611 if (gspca_dev
->cam
.bulk
)
612 usb_clear_halt(gspca_dev
->dev
,
613 gspca_dev
->urb
[0]->pipe
);
616 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
618 destroy_urbs(gspca_dev
);
621 gspca_dev
->streaming
= 1;
623 /* some bulk transfers are started by the subdriver */
624 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
627 /* submit the URBs */
628 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
629 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
631 PDEBUG(D_ERR
|D_STREAM
,
632 "usb_submit_urb [%d] err %d", n
, ret
);
633 gspca_dev
->streaming
= 0;
634 destroy_urbs(gspca_dev
);
635 if (ret
== -ENOSPC
) {
636 msleep(20); /* wait for kill
638 break; /* try the previous alt */
647 mutex_unlock(&gspca_dev
->usb_lock
);
651 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
655 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
657 PDEBUG(D_ERR
|D_STREAM
, "set alt 0 err %d", ret
);
661 /* Note: both the queue and the usb locks should be held when calling this */
662 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
664 gspca_dev
->streaming
= 0;
665 if (gspca_dev
->present
) {
666 if (gspca_dev
->sd_desc
->stopN
)
667 gspca_dev
->sd_desc
->stopN(gspca_dev
);
668 destroy_urbs(gspca_dev
);
669 gspca_set_alt0(gspca_dev
);
672 /* always call stop0 to free the subdriver's resources */
673 if (gspca_dev
->sd_desc
->stop0
)
674 gspca_dev
->sd_desc
->stop0(gspca_dev
);
675 PDEBUG(D_STREAM
, "stream off OK");
678 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
682 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
683 gspca_dev
->curr_mode
= i
;
684 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
685 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
686 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
689 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
690 int width
, int height
)
694 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
695 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
696 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
703 * search a mode with the right pixel format
705 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
711 modeU
= modeD
= mode
;
712 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
714 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
718 if (++modeU
< gspca_dev
->cam
.nmodes
) {
719 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
727 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
728 struct v4l2_fmtdesc
*fmtdesc
)
730 struct gspca_dev
*gspca_dev
= priv
;
734 /* give an index to each format */
737 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
738 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
741 if (fmt_tb
[j
] == fmt_tb
[index
])
746 if (fmtdesc
->index
== index
)
747 break; /* new format */
749 if (index
>= ARRAY_SIZE(fmt_tb
))
754 return -EINVAL
; /* no more format */
756 fmtdesc
->pixelformat
= fmt_tb
[index
];
757 if (gspca_is_compressed(fmt_tb
[index
]))
758 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
759 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
760 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
761 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
762 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
763 fmtdesc
->description
[4] = '\0';
767 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
768 struct v4l2_format
*fmt
)
770 struct gspca_dev
*gspca_dev
= priv
;
773 mode
= gspca_dev
->curr_mode
;
774 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
775 sizeof fmt
->fmt
.pix
);
779 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
780 struct v4l2_format
*fmt
)
782 int w
, h
, mode
, mode2
;
784 w
= fmt
->fmt
.pix
.width
;
785 h
= fmt
->fmt
.pix
.height
;
788 if (gspca_debug
& D_CONF
)
789 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
791 /* search the closest mode for width and height */
792 mode
= wxh_to_mode(gspca_dev
, w
, h
);
794 /* OK if right palette */
795 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
796 != fmt
->fmt
.pix
.pixelformat
) {
798 /* else, search the closest mode with the same pixel format */
799 mode2
= gspca_get_mode(gspca_dev
, mode
,
800 fmt
->fmt
.pix
.pixelformat
);
804 ; * no chance, return this mode */
806 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
807 sizeof fmt
->fmt
.pix
);
808 return mode
; /* used when s_fmt */
811 static int vidioc_try_fmt_vid_cap(struct file
*file
,
813 struct v4l2_format
*fmt
)
815 struct gspca_dev
*gspca_dev
= priv
;
818 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
824 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
825 struct v4l2_format
*fmt
)
827 struct gspca_dev
*gspca_dev
= priv
;
830 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
833 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
837 if (gspca_dev
->nframes
!= 0
838 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
843 if (ret
== gspca_dev
->curr_mode
) {
845 goto out
; /* same mode */
848 if (gspca_dev
->streaming
) {
852 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
853 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
854 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
855 gspca_dev
->curr_mode
= ret
;
859 mutex_unlock(&gspca_dev
->queue_lock
);
863 static void gspca_release(struct video_device
*vfd
)
865 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
867 PDEBUG(D_STREAM
, "device released");
869 kfree(gspca_dev
->usb_buf
);
873 static int dev_open(struct file
*file
)
875 struct gspca_dev
*gspca_dev
;
878 PDEBUG(D_STREAM
, "%s open", current
->comm
);
879 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
880 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
882 if (!gspca_dev
->present
) {
887 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
892 /* protect the subdriver against rmmod */
893 if (!try_module_get(gspca_dev
->module
)) {
900 file
->private_data
= gspca_dev
;
902 /* activate the v4l2 debug */
903 if (gspca_debug
& D_V4L2
)
904 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
905 | V4L2_DEBUG_IOCTL_ARG
;
907 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
908 | V4L2_DEBUG_IOCTL_ARG
);
912 mutex_unlock(&gspca_dev
->queue_lock
);
914 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
916 PDEBUG(D_STREAM
, "open done");
920 static int dev_close(struct file
*file
)
922 struct gspca_dev
*gspca_dev
= file
->private_data
;
924 PDEBUG(D_STREAM
, "%s close", current
->comm
);
925 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
929 /* if the file did the capture, free the streaming resources */
930 if (gspca_dev
->capt_file
== file
) {
931 if (gspca_dev
->streaming
) {
932 mutex_lock(&gspca_dev
->usb_lock
);
933 gspca_stream_off(gspca_dev
);
934 mutex_unlock(&gspca_dev
->usb_lock
);
936 frame_free(gspca_dev
);
937 gspca_dev
->capt_file
= NULL
;
938 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
940 file
->private_data
= NULL
;
941 module_put(gspca_dev
->module
);
942 mutex_unlock(&gspca_dev
->queue_lock
);
944 PDEBUG(D_STREAM
, "close done");
949 static int vidioc_querycap(struct file
*file
, void *priv
,
950 struct v4l2_capability
*cap
)
952 struct gspca_dev
*gspca_dev
= priv
;
955 /* protect the access to the usb device */
956 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
958 if (!gspca_dev
->present
) {
962 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
963 if (gspca_dev
->dev
->product
!= NULL
) {
964 strncpy(cap
->card
, gspca_dev
->dev
->product
,
967 snprintf(cap
->card
, sizeof cap
->card
,
968 "USB Camera (%04x:%04x)",
969 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
970 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
972 usb_make_path(gspca_dev
->dev
, cap
->bus_info
, sizeof(cap
->bus_info
));
973 cap
->version
= DRIVER_VERSION_NUMBER
;
974 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
976 | V4L2_CAP_READWRITE
;
979 mutex_unlock(&gspca_dev
->usb_lock
);
983 static int vidioc_queryctrl(struct file
*file
, void *priv
,
984 struct v4l2_queryctrl
*q_ctrl
)
986 struct gspca_dev
*gspca_dev
= priv
;
992 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
993 id
&= V4L2_CTRL_ID_MASK
;
995 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
996 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1002 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1003 > gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
.id
)
1008 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1009 if (id
== gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
1016 memcpy(q_ctrl
, &gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
,
1018 if (gspca_dev
->ctrl_dis
& (1 << ix
))
1019 q_ctrl
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1023 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1024 struct v4l2_control
*ctrl
)
1026 struct gspca_dev
*gspca_dev
= priv
;
1027 const struct ctrl
*ctrls
;
1030 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1031 i
< gspca_dev
->sd_desc
->nctrls
;
1033 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1035 if (gspca_dev
->ctrl_dis
& (1 << i
))
1037 if (ctrl
->value
< ctrls
->qctrl
.minimum
1038 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1040 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1041 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1042 return -ERESTARTSYS
;
1043 if (gspca_dev
->present
)
1044 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1047 mutex_unlock(&gspca_dev
->usb_lock
);
1053 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1054 struct v4l2_control
*ctrl
)
1056 struct gspca_dev
*gspca_dev
= priv
;
1058 const struct ctrl
*ctrls
;
1061 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1062 i
< gspca_dev
->sd_desc
->nctrls
;
1064 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1066 if (gspca_dev
->ctrl_dis
& (1 << i
))
1068 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1069 return -ERESTARTSYS
;
1070 if (gspca_dev
->present
)
1071 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1074 mutex_unlock(&gspca_dev
->usb_lock
);
1080 /*fixme: have an audio flag in gspca_dev?*/
1081 static int vidioc_s_audio(struct file
*file
, void *priv
,
1082 struct v4l2_audio
*audio
)
1084 if (audio
->index
!= 0)
1089 static int vidioc_g_audio(struct file
*file
, void *priv
,
1090 struct v4l2_audio
*audio
)
1092 strcpy(audio
->name
, "Microphone");
1096 static int vidioc_enumaudio(struct file
*file
, void *priv
,
1097 struct v4l2_audio
*audio
)
1099 if (audio
->index
!= 0)
1102 strcpy(audio
->name
, "Microphone");
1103 audio
->capability
= 0;
1108 static int vidioc_querymenu(struct file
*file
, void *priv
,
1109 struct v4l2_querymenu
*qmenu
)
1111 struct gspca_dev
*gspca_dev
= priv
;
1113 if (!gspca_dev
->sd_desc
->querymenu
)
1115 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1118 static int vidioc_enum_input(struct file
*file
, void *priv
,
1119 struct v4l2_input
*input
)
1121 struct gspca_dev
*gspca_dev
= priv
;
1123 if (input
->index
!= 0)
1125 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1126 input
->status
= gspca_dev
->cam
.input_flags
;
1127 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1128 sizeof input
->name
);
1132 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1138 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1145 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1146 struct v4l2_requestbuffers
*rb
)
1148 struct gspca_dev
*gspca_dev
= priv
;
1151 switch (rb
->memory
) {
1152 case GSPCA_MEMORY_READ
: /* (internal call) */
1153 case V4L2_MEMORY_MMAP
:
1154 case V4L2_MEMORY_USERPTR
:
1159 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1160 return -ERESTARTSYS
;
1162 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1163 && gspca_dev
->memory
!= rb
->memory
) {
1168 /* only one file may do the capture */
1169 if (gspca_dev
->capt_file
!= NULL
1170 && gspca_dev
->capt_file
!= file
) {
1175 /* if allocated, the buffers must not be mapped */
1176 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1177 if (gspca_dev
->frame
[i
].vma_use_count
) {
1183 /* stop streaming */
1184 if (gspca_dev
->streaming
) {
1185 mutex_lock(&gspca_dev
->usb_lock
);
1186 gspca_stream_off(gspca_dev
);
1187 mutex_unlock(&gspca_dev
->usb_lock
);
1190 /* free the previous allocated buffers, if any */
1191 if (gspca_dev
->nframes
!= 0) {
1192 frame_free(gspca_dev
);
1193 gspca_dev
->capt_file
= NULL
;
1195 if (rb
->count
== 0) /* unrequest */
1197 gspca_dev
->memory
= rb
->memory
;
1198 ret
= frame_alloc(gspca_dev
, rb
->count
);
1200 rb
->count
= gspca_dev
->nframes
;
1201 gspca_dev
->capt_file
= file
;
1204 mutex_unlock(&gspca_dev
->queue_lock
);
1205 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1209 static int vidioc_querybuf(struct file
*file
, void *priv
,
1210 struct v4l2_buffer
*v4l2_buf
)
1212 struct gspca_dev
*gspca_dev
= priv
;
1213 struct gspca_frame
*frame
;
1215 if (v4l2_buf
->index
< 0
1216 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1219 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1220 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1224 static int vidioc_streamon(struct file
*file
, void *priv
,
1225 enum v4l2_buf_type buf_type
)
1227 struct gspca_dev
*gspca_dev
= priv
;
1230 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1232 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1233 return -ERESTARTSYS
;
1235 if (gspca_dev
->nframes
== 0
1236 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1240 if (!gspca_dev
->streaming
) {
1241 ret
= gspca_init_transfer(gspca_dev
);
1246 if (gspca_debug
& D_STREAM
) {
1247 PDEBUG_MODE("stream on OK",
1255 mutex_unlock(&gspca_dev
->queue_lock
);
1259 static int vidioc_streamoff(struct file
*file
, void *priv
,
1260 enum v4l2_buf_type buf_type
)
1262 struct gspca_dev
*gspca_dev
= priv
;
1265 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1267 if (!gspca_dev
->streaming
)
1269 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1270 return -ERESTARTSYS
;
1272 /* stop streaming */
1273 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1277 gspca_stream_off(gspca_dev
);
1278 mutex_unlock(&gspca_dev
->usb_lock
);
1280 /* empty the application queues */
1281 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1282 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1283 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1284 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1285 gspca_dev
->sequence
= 0;
1288 mutex_unlock(&gspca_dev
->queue_lock
);
1292 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1293 struct v4l2_jpegcompression
*jpegcomp
)
1295 struct gspca_dev
*gspca_dev
= priv
;
1298 if (!gspca_dev
->sd_desc
->get_jcomp
)
1300 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1301 return -ERESTARTSYS
;
1302 if (gspca_dev
->present
)
1303 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1306 mutex_unlock(&gspca_dev
->usb_lock
);
1310 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1311 struct v4l2_jpegcompression
*jpegcomp
)
1313 struct gspca_dev
*gspca_dev
= priv
;
1316 if (!gspca_dev
->sd_desc
->set_jcomp
)
1318 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1319 return -ERESTARTSYS
;
1320 if (gspca_dev
->present
)
1321 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1324 mutex_unlock(&gspca_dev
->usb_lock
);
1328 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1329 struct v4l2_streamparm
*parm
)
1331 struct gspca_dev
*gspca_dev
= priv
;
1333 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1335 if (gspca_dev
->sd_desc
->get_streamparm
) {
1338 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1339 return -ERESTARTSYS
;
1340 if (gspca_dev
->present
)
1341 ret
= gspca_dev
->sd_desc
->get_streamparm(gspca_dev
,
1345 mutex_unlock(&gspca_dev
->usb_lock
);
1352 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1353 struct v4l2_streamparm
*parm
)
1355 struct gspca_dev
*gspca_dev
= priv
;
1358 n
= parm
->parm
.capture
.readbuffers
;
1359 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1360 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1362 gspca_dev
->nbufread
= n
;
1364 if (gspca_dev
->sd_desc
->set_streamparm
) {
1367 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1368 return -ERESTARTSYS
;
1369 if (gspca_dev
->present
)
1370 ret
= gspca_dev
->sd_desc
->set_streamparm(gspca_dev
,
1374 mutex_unlock(&gspca_dev
->usb_lock
);
1381 static int vidioc_s_std(struct file
*filp
, void *priv
,
1387 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1388 static int vidiocgmbuf(struct file
*file
, void *priv
,
1389 struct video_mbuf
*mbuf
)
1391 struct gspca_dev
*gspca_dev
= file
->private_data
;
1394 PDEBUG(D_STREAM
, "cgmbuf");
1395 if (gspca_dev
->nframes
== 0) {
1399 struct v4l2_format fmt
;
1401 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1402 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1403 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1404 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1405 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1406 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1411 struct v4l2_requestbuffers rb
;
1413 memset(&rb
, 0, sizeof rb
);
1415 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1416 rb
.memory
= V4L2_MEMORY_MMAP
;
1417 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1422 mbuf
->frames
= gspca_dev
->nframes
;
1423 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1424 for (i
= 0; i
< mbuf
->frames
; i
++)
1425 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1430 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1432 struct gspca_dev
*gspca_dev
= file
->private_data
;
1433 struct gspca_frame
*frame
;
1435 unsigned long addr
, start
, size
;
1438 start
= vma
->vm_start
;
1439 size
= vma
->vm_end
- vma
->vm_start
;
1440 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1442 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1443 return -ERESTARTSYS
;
1444 if (!gspca_dev
->present
) {
1448 if (gspca_dev
->capt_file
!= file
) {
1454 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1455 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1456 PDEBUG(D_STREAM
, "mmap bad memory type");
1459 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1461 frame
= &gspca_dev
->frame
[i
];
1465 if (frame
== NULL
) {
1466 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1470 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1471 /* v4l1 maps all the buffers */
1473 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1475 if (size
!= frame
->v4l2_buf
.length
) {
1476 PDEBUG(D_STREAM
, "mmap bad size");
1482 * - VM_IO marks the area as being a mmaped region for I/O to a
1483 * device. It also prevents the region from being core dumped.
1485 vma
->vm_flags
|= VM_IO
;
1487 addr
= (unsigned long) frame
->data
;
1489 page
= vmalloc_to_page((void *) addr
);
1490 ret
= vm_insert_page(vma
, start
, page
);
1498 vma
->vm_ops
= &gspca_vm_ops
;
1499 vma
->vm_private_data
= frame
;
1503 mutex_unlock(&gspca_dev
->queue_lock
);
1508 * wait for a video frame
1510 * If a frame is ready, its index is returned.
1512 static int frame_wait(struct gspca_dev
*gspca_dev
,
1515 struct gspca_frame
*frame
;
1518 /* check if a frame is ready */
1519 i
= gspca_dev
->fr_o
;
1520 j
= gspca_dev
->fr_queue
[i
];
1521 frame
= &gspca_dev
->frame
[j
];
1523 if (!(frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)) {
1527 /* wait till a frame is ready */
1528 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1529 (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) ||
1530 !gspca_dev
->streaming
|| !gspca_dev
->present
,
1531 msecs_to_jiffies(3000));
1534 if (ret
== 0 || !gspca_dev
->streaming
|| !gspca_dev
->present
)
1538 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1539 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1544 if (gspca_dev
->sd_desc
->dq_callback
) {
1545 mutex_lock(&gspca_dev
->usb_lock
);
1546 if (gspca_dev
->present
)
1547 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1548 mutex_unlock(&gspca_dev
->usb_lock
);
1554 * dequeue a video buffer
1556 * If nonblock_ing is false, block until a buffer is available.
1558 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1559 struct v4l2_buffer
*v4l2_buf
)
1561 struct gspca_dev
*gspca_dev
= priv
;
1562 struct gspca_frame
*frame
;
1565 PDEBUG(D_FRAM
, "dqbuf");
1566 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1569 if (!gspca_dev
->present
)
1572 /* if not streaming, be sure the application will not loop forever */
1573 if (!(file
->f_flags
& O_NONBLOCK
)
1574 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1577 /* only the capturing file may dequeue */
1578 if (gspca_dev
->capt_file
!= file
)
1581 /* only one dequeue / read at a time */
1582 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1583 return -ERESTARTSYS
;
1585 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1588 i
= ret
; /* frame index */
1589 frame
= &gspca_dev
->frame
[i
];
1590 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1591 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1593 frame
->v4l2_buf
.bytesused
)) {
1594 PDEBUG(D_ERR
|D_STREAM
,
1595 "dqbuf cp to user failed");
1600 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1601 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1602 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1605 mutex_unlock(&gspca_dev
->read_lock
);
1610 * queue a video buffer
1612 * Attempting to queue a buffer that has already been
1613 * queued will return -EINVAL.
1615 static int vidioc_qbuf(struct file
*file
, void *priv
,
1616 struct v4l2_buffer
*v4l2_buf
)
1618 struct gspca_dev
*gspca_dev
= priv
;
1619 struct gspca_frame
*frame
;
1622 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1624 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1625 return -ERESTARTSYS
;
1627 index
= v4l2_buf
->index
;
1628 if ((unsigned) index
>= gspca_dev
->nframes
) {
1630 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1634 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1635 PDEBUG(D_FRAM
, "qbuf bad memory type");
1640 frame
= &gspca_dev
->frame
[index
];
1641 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1642 PDEBUG(D_FRAM
, "qbuf bad state");
1647 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1649 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1650 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1651 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1654 /* put the buffer in the 'queued' queue */
1655 i
= gspca_dev
->fr_q
;
1656 gspca_dev
->fr_queue
[i
] = index
;
1657 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1658 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1663 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1664 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1667 mutex_unlock(&gspca_dev
->queue_lock
);
1672 * allocate the resources for read()
1674 static int read_alloc(struct gspca_dev
*gspca_dev
,
1677 struct v4l2_buffer v4l2_buf
;
1680 PDEBUG(D_STREAM
, "read alloc");
1681 if (gspca_dev
->nframes
== 0) {
1682 struct v4l2_requestbuffers rb
;
1684 memset(&rb
, 0, sizeof rb
);
1685 rb
.count
= gspca_dev
->nbufread
;
1686 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1687 rb
.memory
= GSPCA_MEMORY_READ
;
1688 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1690 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1693 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1694 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1695 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1696 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1698 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1700 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1704 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1707 /* start streaming */
1708 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1710 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1714 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1716 struct gspca_dev
*gspca_dev
= file
->private_data
;
1719 PDEBUG(D_FRAM
, "poll");
1721 poll_wait(file
, &gspca_dev
->wq
, wait
);
1723 /* if reqbufs is not done, the user would use read() */
1724 if (gspca_dev
->nframes
== 0) {
1725 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
1726 return POLLERR
; /* not the 1st time */
1727 ret
= read_alloc(gspca_dev
, file
);
1732 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1735 /* check the next incoming buffer */
1736 i
= gspca_dev
->fr_o
;
1737 i
= gspca_dev
->fr_queue
[i
];
1738 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1739 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1742 mutex_unlock(&gspca_dev
->queue_lock
);
1743 if (!gspca_dev
->present
)
1748 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1749 size_t count
, loff_t
*ppos
)
1751 struct gspca_dev
*gspca_dev
= file
->private_data
;
1752 struct gspca_frame
*frame
;
1753 struct v4l2_buffer v4l2_buf
;
1754 struct timeval timestamp
;
1757 PDEBUG(D_FRAM
, "read (%zd)", count
);
1758 if (!gspca_dev
->present
)
1760 switch (gspca_dev
->memory
) {
1761 case GSPCA_MEMORY_NO
: /* first time */
1762 ret
= read_alloc(gspca_dev
, file
);
1766 case GSPCA_MEMORY_READ
:
1767 if (gspca_dev
->capt_file
== file
)
1775 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1779 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1780 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1781 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1782 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1784 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1788 /* if the process slept for more than 1 second,
1789 * get a newer frame */
1790 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1792 break; /* avoid infinite loop */
1793 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1795 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1797 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1802 /* copy the frame */
1803 if (count
> frame
->v4l2_buf
.bytesused
)
1804 count
= frame
->v4l2_buf
.bytesused
;
1805 ret
= copy_to_user(data
, frame
->data
, count
);
1807 PDEBUG(D_ERR
|D_STREAM
,
1808 "read cp to user lack %d / %zd", ret
, count
);
1814 /* in each case, requeue the buffer */
1815 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1821 static struct v4l2_file_operations dev_fops
= {
1822 .owner
= THIS_MODULE
,
1824 .release
= dev_close
,
1827 .unlocked_ioctl
= video_ioctl2
,
1831 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1832 .vidioc_querycap
= vidioc_querycap
,
1833 .vidioc_dqbuf
= vidioc_dqbuf
,
1834 .vidioc_qbuf
= vidioc_qbuf
,
1835 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1836 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1837 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1838 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1839 .vidioc_streamon
= vidioc_streamon
,
1840 .vidioc_queryctrl
= vidioc_queryctrl
,
1841 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1842 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1843 .vidioc_g_audio
= vidioc_g_audio
,
1844 .vidioc_s_audio
= vidioc_s_audio
,
1845 .vidioc_enumaudio
= vidioc_enumaudio
,
1846 .vidioc_querymenu
= vidioc_querymenu
,
1847 .vidioc_enum_input
= vidioc_enum_input
,
1848 .vidioc_g_input
= vidioc_g_input
,
1849 .vidioc_s_input
= vidioc_s_input
,
1850 .vidioc_reqbufs
= vidioc_reqbufs
,
1851 .vidioc_querybuf
= vidioc_querybuf
,
1852 .vidioc_streamoff
= vidioc_streamoff
,
1853 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1854 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1855 .vidioc_g_parm
= vidioc_g_parm
,
1856 .vidioc_s_parm
= vidioc_s_parm
,
1857 .vidioc_s_std
= vidioc_s_std
,
1858 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1859 .vidiocgmbuf
= vidiocgmbuf
,
1863 static struct video_device gspca_template
= {
1864 .name
= "gspca main driver",
1866 .ioctl_ops
= &dev_ioctl_ops
,
1867 .release
= gspca_release
,
1872 * probe and create a new gspca device
1874 * This function must be called by the sub-driver when it is
1875 * called for probing a new device.
1877 int gspca_dev_probe(struct usb_interface
*intf
,
1878 const struct usb_device_id
*id
,
1879 const struct sd_desc
*sd_desc
,
1881 struct module
*module
)
1883 struct usb_interface_descriptor
*interface
;
1884 struct gspca_dev
*gspca_dev
;
1885 struct usb_device
*dev
= interface_to_usbdev(intf
);
1888 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1890 /* we don't handle multi-config cameras */
1891 if (dev
->descriptor
.bNumConfigurations
!= 1)
1893 interface
= &intf
->cur_altsetting
->desc
;
1894 if (interface
->bInterfaceNumber
> 0)
1897 /* create the device */
1898 if (dev_size
< sizeof *gspca_dev
)
1899 dev_size
= sizeof *gspca_dev
;
1900 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1902 err("couldn't kzalloc gspca struct");
1905 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
1906 if (!gspca_dev
->usb_buf
) {
1907 err("out of memory");
1911 gspca_dev
->dev
= dev
;
1912 gspca_dev
->iface
= interface
->bInterfaceNumber
;
1913 gspca_dev
->nbalt
= intf
->num_altsetting
;
1914 gspca_dev
->sd_desc
= sd_desc
;
1915 gspca_dev
->nbufread
= 2;
1916 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
1918 /* configure the subdriver and initialize the USB device */
1919 ret
= sd_desc
->config(gspca_dev
, id
);
1922 ret
= sd_desc
->init(gspca_dev
);
1925 ret
= gspca_set_alt0(gspca_dev
);
1928 gspca_set_default_mode(gspca_dev
);
1930 mutex_init(&gspca_dev
->usb_lock
);
1931 mutex_init(&gspca_dev
->read_lock
);
1932 mutex_init(&gspca_dev
->queue_lock
);
1933 init_waitqueue_head(&gspca_dev
->wq
);
1935 /* init video stuff */
1936 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
1937 gspca_dev
->vdev
.parent
= &intf
->dev
;
1938 gspca_dev
->module
= module
;
1939 gspca_dev
->present
= 1;
1940 ret
= video_register_device(&gspca_dev
->vdev
,
1944 err("video_register_device err %d", ret
);
1948 usb_set_intfdata(intf
, gspca_dev
);
1949 PDEBUG(D_PROBE
, "probe ok");
1952 kfree(gspca_dev
->usb_buf
);
1956 EXPORT_SYMBOL(gspca_dev_probe
);
1961 * This function must be called by the sub-driver
1962 * when the device disconnects, after the specific resources are freed.
1964 void gspca_disconnect(struct usb_interface
*intf
)
1966 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1968 mutex_lock(&gspca_dev
->usb_lock
);
1969 gspca_dev
->present
= 0;
1971 if (gspca_dev
->streaming
) {
1972 destroy_urbs(gspca_dev
);
1973 wake_up_interruptible(&gspca_dev
->wq
);
1976 /* the device is freed at exit of this function */
1977 gspca_dev
->dev
= NULL
;
1978 mutex_unlock(&gspca_dev
->usb_lock
);
1980 usb_set_intfdata(intf
, NULL
);
1982 /* release the device */
1983 /* (this will call gspca_release() immediatly or on last close) */
1984 video_unregister_device(&gspca_dev
->vdev
);
1986 PDEBUG(D_PROBE
, "disconnect complete");
1988 EXPORT_SYMBOL(gspca_disconnect
);
1991 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1993 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1995 if (!gspca_dev
->streaming
)
1997 gspca_dev
->frozen
= 1; /* avoid urb error messages */
1998 if (gspca_dev
->sd_desc
->stopN
)
1999 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2000 destroy_urbs(gspca_dev
);
2001 gspca_set_alt0(gspca_dev
);
2002 if (gspca_dev
->sd_desc
->stop0
)
2003 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2006 EXPORT_SYMBOL(gspca_suspend
);
2008 int gspca_resume(struct usb_interface
*intf
)
2010 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2012 gspca_dev
->frozen
= 0;
2013 gspca_dev
->sd_desc
->init(gspca_dev
);
2014 if (gspca_dev
->streaming
)
2015 return gspca_init_transfer(gspca_dev
);
2018 EXPORT_SYMBOL(gspca_resume
);
2020 /* -- cam driver utility functions -- */
2022 /* auto gain and exposure algorithm based on the knee algorithm described here:
2023 http://ytse.tricolour.net/docs/LowLightOptimization.html
2025 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2027 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2028 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2030 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2031 const struct ctrl
*gain_ctrl
= NULL
;
2032 const struct ctrl
*exposure_ctrl
= NULL
;
2033 const struct ctrl
*autogain_ctrl
= NULL
;
2036 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2037 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2038 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2039 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2040 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2041 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2042 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2044 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2045 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2046 "on cam without (auto)gain/exposure");
2050 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2051 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2052 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2056 orig_exposure
= exposure
;
2058 /* If we are of a multiple of deadzone, do multiple steps to reach the
2059 desired lumination fast (with the risc of a slight overshoot) */
2060 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2062 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2063 avg_lum
, desired_avg_lum
, steps
);
2065 for (i
= 0; i
< steps
; i
++) {
2066 if (avg_lum
> desired_avg_lum
) {
2067 if (gain
> gain_knee
)
2069 else if (exposure
> exposure_knee
)
2071 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2073 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2075 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2080 if (gain
< gain_ctrl
->qctrl
.default_value
)
2082 else if (exposure
< exposure_knee
)
2084 else if (gain
< gain_knee
)
2086 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2088 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2095 if (gain
!= orig_gain
) {
2096 gain_ctrl
->set(gspca_dev
, gain
);
2099 if (exposure
!= orig_exposure
) {
2100 exposure_ctrl
->set(gspca_dev
, exposure
);
2106 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2108 /* -- module insert / remove -- */
2109 static int __init
gspca_init(void)
2111 info("main v%d.%d.%d registered",
2112 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2113 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2114 DRIVER_VERSION_NUMBER
& 0xff);
2117 static void __exit
gspca_exit(void)
2119 info("main deregistered");
2122 module_init(gspca_init
);
2123 module_exit(gspca_exit
);
2126 module_param_named(debug
, gspca_debug
, int, 0644);
2127 MODULE_PARM_DESC(debug
,
2128 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2129 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"