2 * Main USB camera driver
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
24 #include <linux/version.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/pagemap.h>
33 #include <linux/kref.h>
35 #include <linux/uaccess.h>
36 #include <linux/jiffies.h>
37 #include <media/v4l2-ioctl.h>
42 #define DEF_NURBS 2 /* default number of URBs */
44 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
46 MODULE_LICENSE("GPL");
48 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 3, 0)
50 static int video_nr
= -1;
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) {
136 if (!gspca_dev
->frozen
)
138 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
139 return; /* disconnection ? */
141 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
142 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
144 /* check the availability of the frame buffer */
145 frame
= gspca_get_i_frame(gspca_dev
);
147 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
151 /* check the packet status and length */
152 len
= urb
->iso_frame_desc
[i
].actual_length
;
155 st
= urb
->iso_frame_desc
[i
].status
;
158 "ISOC data error: [%d] len=%d, status=%d",
160 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
164 /* let the packet be analyzed by the subdriver */
165 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
166 i
, urb
->iso_frame_desc
[i
].offset
, len
);
167 data
= (__u8
*) urb
->transfer_buffer
168 + urb
->iso_frame_desc
[i
].offset
;
169 pkt_scan(gspca_dev
, frame
, data
, len
);
172 /* resubmit the URB */
174 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
176 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
180 * ISOC message interrupt from the USB device
182 * Analyse each packet and call the subdriver for copy to the frame buffer.
184 static void isoc_irq(struct urb
*urb
187 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
189 PDEBUG(D_PACK
, "isoc irq");
190 if (!gspca_dev
->streaming
)
192 fill_frame(gspca_dev
, urb
);
196 * bulk message interrupt from the USB device
198 static void bulk_irq(struct urb
*urb
201 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
202 struct gspca_frame
*frame
;
204 PDEBUG(D_PACK
, "bulk irq");
205 if (!gspca_dev
->streaming
)
207 if (urb
->status
!= 0 && urb
->status
!= -ECONNRESET
) {
209 if (!gspca_dev
->frozen
)
211 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
212 return; /* disconnection ? */
215 /* check the availability of the frame buffer */
216 frame
= gspca_get_i_frame(gspca_dev
);
218 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
220 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
221 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
223 urb
->transfer_buffer
,
229 * add data to the current frame
231 * This function is called by the subdrivers at interrupt level.
233 * To build a frame, these ones must add
235 * - 0 or many INTER_PACKETs
237 * DISCARD_PACKET invalidates the whole frame.
238 * On LAST_PACKET, a new frame is returned.
240 struct gspca_frame
*gspca_frame_add(struct gspca_dev
*gspca_dev
,
241 enum gspca_packet_type packet_type
,
242 struct gspca_frame
*frame
,
248 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
250 /* when start of a new frame, if the current frame buffer
251 * is not queued, discard the whole frame */
252 if (packet_type
== FIRST_PACKET
) {
253 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
254 != V4L2_BUF_FLAG_QUEUED
) {
255 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
258 frame
->data_end
= frame
->data
;
259 jiffies_to_timeval(get_jiffies_64(),
260 &frame
->v4l2_buf
.timestamp
);
261 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
262 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
263 if (packet_type
== LAST_PACKET
)
264 gspca_dev
->last_packet_type
= packet_type
;
268 /* append the packet to the frame buffer */
270 if (frame
->data_end
- frame
->data
+ len
271 > frame
->v4l2_buf
.length
) {
272 PDEBUG(D_ERR
|D_PACK
, "frame overflow %zd > %d",
273 frame
->data_end
- frame
->data
+ len
,
274 frame
->v4l2_buf
.length
);
275 packet_type
= DISCARD_PACKET
;
277 memcpy(frame
->data_end
, data
, len
);
278 frame
->data_end
+= len
;
281 gspca_dev
->last_packet_type
= packet_type
;
283 /* if last packet, wake up the application and advance in the queue */
284 if (packet_type
== LAST_PACKET
) {
285 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
286 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
287 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
288 atomic_inc(&gspca_dev
->nevent
);
289 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
290 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
292 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
293 frame
->v4l2_buf
.bytesused
,
297 j
= gspca_dev
->fr_queue
[i
];
298 frame
= &gspca_dev
->frame
[j
];
302 EXPORT_SYMBOL(gspca_frame_add
);
304 static int gspca_is_compressed(__u32 format
)
307 case V4L2_PIX_FMT_MJPEG
:
308 case V4L2_PIX_FMT_JPEG
:
309 case V4L2_PIX_FMT_SPCA561
:
310 case V4L2_PIX_FMT_PAC207
:
316 static void *rvmalloc(unsigned long size
)
321 mem
= vmalloc_32(size
);
323 adr
= (unsigned long) mem
;
324 while ((long) size
> 0) {
325 SetPageReserved(vmalloc_to_page((void *) adr
));
333 static void rvfree(void *mem
, long size
)
337 adr
= (unsigned long) mem
;
339 ClearPageReserved(vmalloc_to_page((void *) adr
));
346 static int frame_alloc(struct gspca_dev
*gspca_dev
,
349 struct gspca_frame
*frame
;
353 i
= gspca_dev
->curr_mode
;
354 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
355 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
356 frsz
= PAGE_ALIGN(frsz
);
357 gspca_dev
->frsz
= frsz
;
358 if (count
> GSPCA_MAX_FRAMES
)
359 count
= GSPCA_MAX_FRAMES
;
360 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
361 if (!gspca_dev
->frbuf
) {
362 err("frame alloc failed");
365 gspca_dev
->nframes
= count
;
366 for (i
= 0; i
< count
; i
++) {
367 frame
= &gspca_dev
->frame
[i
];
368 frame
->v4l2_buf
.index
= i
;
369 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
370 frame
->v4l2_buf
.flags
= 0;
371 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
372 frame
->v4l2_buf
.length
= frsz
;
373 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
374 frame
->v4l2_buf
.sequence
= 0;
375 frame
->data
= frame
->data_end
=
376 gspca_dev
->frbuf
+ i
* frsz
;
377 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
379 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
380 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
381 gspca_dev
->sequence
= 0;
382 atomic_set(&gspca_dev
->nevent
, 0);
386 static void frame_free(struct gspca_dev
*gspca_dev
)
390 PDEBUG(D_STREAM
, "frame free");
391 if (gspca_dev
->frbuf
!= NULL
) {
392 rvfree(gspca_dev
->frbuf
,
393 gspca_dev
->nframes
* gspca_dev
->frsz
);
394 gspca_dev
->frbuf
= NULL
;
395 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
396 gspca_dev
->frame
[i
].data
= NULL
;
398 gspca_dev
->nframes
= 0;
401 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
406 PDEBUG(D_STREAM
, "kill transfer");
407 for (i
= 0; i
< MAX_NURBS
; i
++) {
408 urb
= gspca_dev
->urb
[i
];
412 gspca_dev
->urb
[i
] = NULL
;
414 if (urb
->transfer_buffer
!= NULL
)
415 usb_buffer_free(gspca_dev
->dev
,
416 urb
->transfer_buffer_length
,
417 urb
->transfer_buffer
,
424 * look for an input transfer endpoint in an alternate setting
426 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
430 struct usb_host_endpoint
*ep
;
433 epaddr
|= USB_DIR_IN
;
434 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
435 ep
= &alt
->endpoint
[i
];
436 if (ep
->desc
.bEndpointAddress
== epaddr
) {
437 attr
= ep
->desc
.bmAttributes
438 & USB_ENDPOINT_XFERTYPE_MASK
;
448 * look for an input (isoc or bulk) endpoint
450 * The endpoint is defined by the subdriver.
451 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
452 * This routine may be called many times when the bandwidth is too small
453 * (the bandwidth is checked on urb submit).
455 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
457 struct usb_interface
*intf
;
458 struct usb_host_endpoint
*ep
;
461 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
463 i
= gspca_dev
->alt
; /* previous alt setting */
466 while (--i
> 0) { /* alt 0 is unusable */
467 ep
= alt_xfer(&intf
->altsetting
[i
],
468 gspca_dev
->cam
.epaddr
,
469 USB_ENDPOINT_XFER_ISOC
);
474 /* if no isoc, try bulk */
476 ep
= alt_xfer(&intf
->altsetting
[0],
477 gspca_dev
->cam
.epaddr
,
478 USB_ENDPOINT_XFER_BULK
);
480 err("no transfer endpoint found");
484 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
485 i
, ep
->desc
.bEndpointAddress
);
487 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
489 err("set interface err %d", ret
);
493 gspca_dev
->alt
= i
; /* memorize the current alt setting */
498 * create the URBs for image transfer
500 static int create_urbs(struct gspca_dev
*gspca_dev
,
501 struct usb_host_endpoint
*ep
)
504 int n
, nurbs
, i
, psize
, npkt
, bsize
;
506 /* calculate the packet size and the number of packets */
507 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
509 if (gspca_dev
->alt
!= 0) { /* isoc */
511 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
512 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
513 npkt
= ISO_MAX_SIZE
/ psize
;
514 if (npkt
> ISO_MAX_PKT
)
516 bsize
= psize
* npkt
;
518 "isoc %d pkts size %d = bsize:%d",
523 bsize
= gspca_dev
->cam
. bulk_size
;
526 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
530 gspca_dev
->nurbs
= nurbs
;
531 for (n
= 0; n
< nurbs
; n
++) {
532 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
534 err("usb_alloc_urb failed");
535 destroy_urbs(gspca_dev
);
538 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
543 if (urb
->transfer_buffer
== NULL
) {
545 err("usb_buffer_urb failed");
546 destroy_urbs(gspca_dev
);
549 gspca_dev
->urb
[n
] = urb
;
550 urb
->dev
= gspca_dev
->dev
;
551 urb
->context
= gspca_dev
;
552 urb
->transfer_buffer_length
= bsize
;
553 if (npkt
!= 0) { /* ISOC */
554 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
555 ep
->desc
.bEndpointAddress
);
556 urb
->transfer_flags
= URB_ISO_ASAP
557 | URB_NO_TRANSFER_DMA_MAP
;
558 urb
->interval
= ep
->desc
.bInterval
;
559 urb
->complete
= isoc_irq
;
560 urb
->number_of_packets
= npkt
;
561 for (i
= 0; i
< npkt
; i
++) {
562 urb
->iso_frame_desc
[i
].length
= psize
;
563 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
566 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
567 ep
->desc
.bEndpointAddress
),
568 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
569 urb
->complete
= bulk_irq
;
576 * start the USB transfer
578 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
580 struct usb_host_endpoint
*ep
;
583 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
586 /* set the higher alternate setting and
587 * loop until urb submit succeeds */
588 gspca_dev
->alt
= gspca_dev
->nbalt
;
590 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
591 ep
= get_ep(gspca_dev
);
596 ret
= create_urbs(gspca_dev
, ep
);
601 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
603 destroy_urbs(gspca_dev
);
606 gspca_dev
->streaming
= 1;
607 atomic_set(&gspca_dev
->nevent
, 0);
609 /* bulk transfers are started by the subdriver */
610 if (gspca_dev
->alt
== 0)
613 /* submit the URBs */
614 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
615 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
617 PDEBUG(D_ERR
|D_STREAM
,
618 "usb_submit_urb [%d] err %d", n
, ret
);
619 gspca_dev
->streaming
= 0;
620 destroy_urbs(gspca_dev
);
622 break; /* try the previous alt */
630 mutex_unlock(&gspca_dev
->usb_lock
);
634 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
638 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
640 PDEBUG(D_ERR
|D_STREAM
, "set interface 0 err %d", ret
);
644 /* Note: both the queue and the usb locks should be held when calling this */
645 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
647 gspca_dev
->streaming
= 0;
648 atomic_set(&gspca_dev
->nevent
, 0);
649 if (gspca_dev
->present
650 && gspca_dev
->sd_desc
->stopN
)
651 gspca_dev
->sd_desc
->stopN(gspca_dev
);
652 destroy_urbs(gspca_dev
);
653 gspca_set_alt0(gspca_dev
);
654 if (gspca_dev
->sd_desc
->stop0
)
655 gspca_dev
->sd_desc
->stop0(gspca_dev
);
656 PDEBUG(D_STREAM
, "stream off OK");
659 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
663 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
664 gspca_dev
->curr_mode
= i
;
665 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
666 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
667 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
670 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
671 int width
, int height
)
675 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
676 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
677 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
684 * search a mode with the right pixel format
686 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
692 modeU
= modeD
= mode
;
693 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
695 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
699 if (++modeU
< gspca_dev
->cam
.nmodes
) {
700 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
708 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
709 struct v4l2_fmtdesc
*fmtdesc
)
711 struct gspca_dev
*gspca_dev
= priv
;
715 /* give an index to each format */
718 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
719 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
722 if (fmt_tb
[j
] == fmt_tb
[index
])
727 if (fmtdesc
->index
== index
)
728 break; /* new format */
730 if (index
>= sizeof fmt_tb
/ sizeof fmt_tb
[0])
735 return -EINVAL
; /* no more format */
737 fmtdesc
->pixelformat
= fmt_tb
[index
];
738 if (gspca_is_compressed(fmt_tb
[index
]))
739 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
740 fmtdesc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
741 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
742 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
743 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
744 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
745 fmtdesc
->description
[4] = '\0';
749 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
750 struct v4l2_format
*fmt
)
752 struct gspca_dev
*gspca_dev
= priv
;
755 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
757 mode
= gspca_dev
->curr_mode
;
758 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
759 sizeof fmt
->fmt
.pix
);
763 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
764 struct v4l2_format
*fmt
)
766 int w
, h
, mode
, mode2
;
768 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
770 w
= fmt
->fmt
.pix
.width
;
771 h
= fmt
->fmt
.pix
.height
;
774 if (gspca_debug
& D_CONF
)
775 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
777 /* search the closest mode for width and height */
778 mode
= wxh_to_mode(gspca_dev
, w
, h
);
780 /* OK if right palette */
781 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
782 != fmt
->fmt
.pix
.pixelformat
) {
784 /* else, search the closest mode with the same pixel format */
785 mode2
= gspca_get_mode(gspca_dev
, mode
,
786 fmt
->fmt
.pix
.pixelformat
);
790 ; * no chance, return this mode */
792 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
793 sizeof fmt
->fmt
.pix
);
794 return mode
; /* used when s_fmt */
797 static int vidioc_try_fmt_vid_cap(struct file
*file
,
799 struct v4l2_format
*fmt
)
801 struct gspca_dev
*gspca_dev
= priv
;
804 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
810 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
811 struct v4l2_format
*fmt
)
813 struct gspca_dev
*gspca_dev
= priv
;
816 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
819 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
823 if (gspca_dev
->nframes
!= 0
824 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
829 if (ret
== gspca_dev
->curr_mode
) {
831 goto out
; /* same mode */
834 if (gspca_dev
->streaming
) {
838 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
839 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
840 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
841 gspca_dev
->curr_mode
= ret
;
845 mutex_unlock(&gspca_dev
->queue_lock
);
849 static void gspca_delete(struct kref
*kref
)
851 struct gspca_dev
*gspca_dev
= container_of(kref
, struct gspca_dev
, kref
);
853 PDEBUG(D_STREAM
, "device deleted");
855 kfree(gspca_dev
->usb_buf
);
859 static int dev_open(struct inode
*inode
, struct file
*file
)
861 struct gspca_dev
*gspca_dev
;
864 PDEBUG(D_STREAM
, "%s open", current
->comm
);
865 gspca_dev
= video_drvdata(file
);
866 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
868 if (!gspca_dev
->present
) {
873 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
878 /* protect the subdriver against rmmod */
879 if (!try_module_get(gspca_dev
->module
)) {
887 kref_get(&gspca_dev
->kref
);
889 file
->private_data
= gspca_dev
;
891 /* activate the v4l2 debug */
892 if (gspca_debug
& D_V4L2
)
893 gspca_dev
->vdev
->debug
|= V4L2_DEBUG_IOCTL
894 | V4L2_DEBUG_IOCTL_ARG
;
896 gspca_dev
->vdev
->debug
&= ~(V4L2_DEBUG_IOCTL
897 | V4L2_DEBUG_IOCTL_ARG
);
901 mutex_unlock(&gspca_dev
->queue_lock
);
903 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
905 PDEBUG(D_STREAM
, "open done");
909 static int dev_close(struct inode
*inode
, struct file
*file
)
911 struct gspca_dev
*gspca_dev
= file
->private_data
;
913 PDEBUG(D_STREAM
, "%s close", current
->comm
);
914 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
918 /* if the file did the capture, free the streaming resources */
919 if (gspca_dev
->capt_file
== file
) {
920 if (gspca_dev
->streaming
) {
921 mutex_lock(&gspca_dev
->usb_lock
);
922 gspca_stream_off(gspca_dev
);
923 mutex_unlock(&gspca_dev
->usb_lock
);
925 frame_free(gspca_dev
);
926 gspca_dev
->capt_file
= NULL
;
927 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
929 file
->private_data
= NULL
;
930 module_put(gspca_dev
->module
);
931 mutex_unlock(&gspca_dev
->queue_lock
);
933 PDEBUG(D_STREAM
, "close done");
935 kref_put(&gspca_dev
->kref
, gspca_delete
);
940 static int vidioc_querycap(struct file
*file
, void *priv
,
941 struct v4l2_capability
*cap
)
943 struct gspca_dev
*gspca_dev
= priv
;
945 memset(cap
, 0, sizeof *cap
);
946 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
947 if (gspca_dev
->dev
->product
!= NULL
) {
948 strncpy(cap
->card
, gspca_dev
->dev
->product
,
951 snprintf(cap
->card
, sizeof cap
->card
,
952 "USB Camera (%04x:%04x)",
953 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
954 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
956 strncpy(cap
->bus_info
, gspca_dev
->dev
->bus
->bus_name
,
957 sizeof cap
->bus_info
);
958 cap
->version
= DRIVER_VERSION_NUMBER
;
959 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
961 | V4L2_CAP_READWRITE
;
965 static int vidioc_queryctrl(struct file
*file
, void *priv
,
966 struct v4l2_queryctrl
*q_ctrl
)
968 struct gspca_dev
*gspca_dev
= priv
;
974 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
975 id
&= V4L2_CTRL_ID_MASK
;
977 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
978 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
984 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
985 > gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
.id
)
990 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
991 if (id
== gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
998 memcpy(q_ctrl
, &gspca_dev
->sd_desc
->ctrls
[ix
].qctrl
,
1000 if (gspca_dev
->ctrl_dis
& (1 << ix
))
1001 q_ctrl
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1005 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1006 struct v4l2_control
*ctrl
)
1008 struct gspca_dev
*gspca_dev
= priv
;
1009 const struct ctrl
*ctrls
;
1012 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1013 i
< gspca_dev
->sd_desc
->nctrls
;
1015 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1017 if (gspca_dev
->ctrl_dis
& (1 << i
))
1019 if (ctrl
->value
< ctrls
->qctrl
.minimum
1020 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1022 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1023 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1024 return -ERESTARTSYS
;
1025 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1026 mutex_unlock(&gspca_dev
->usb_lock
);
1032 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1033 struct v4l2_control
*ctrl
)
1035 struct gspca_dev
*gspca_dev
= priv
;
1037 const struct ctrl
*ctrls
;
1040 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1041 i
< gspca_dev
->sd_desc
->nctrls
;
1043 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1045 if (gspca_dev
->ctrl_dis
& (1 << i
))
1047 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1048 return -ERESTARTSYS
;
1049 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1050 mutex_unlock(&gspca_dev
->usb_lock
);
1056 static int vidioc_querymenu(struct file
*file
, void *priv
,
1057 struct v4l2_querymenu
*qmenu
)
1059 struct gspca_dev
*gspca_dev
= priv
;
1061 if (!gspca_dev
->sd_desc
->querymenu
)
1063 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1066 static int vidioc_enum_input(struct file
*file
, void *priv
,
1067 struct v4l2_input
*input
)
1069 struct gspca_dev
*gspca_dev
= priv
;
1071 if (input
->index
!= 0)
1073 memset(input
, 0, sizeof *input
);
1074 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1075 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1076 sizeof input
->name
);
1080 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1086 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1093 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1094 struct v4l2_requestbuffers
*rb
)
1096 struct gspca_dev
*gspca_dev
= priv
;
1099 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1101 switch (rb
->memory
) {
1102 case GSPCA_MEMORY_READ
: /* (internal call) */
1103 case V4L2_MEMORY_MMAP
:
1104 case V4L2_MEMORY_USERPTR
:
1109 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1110 return -ERESTARTSYS
;
1112 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1113 && gspca_dev
->memory
!= rb
->memory
) {
1118 /* only one file may do the capture */
1119 if (gspca_dev
->capt_file
!= NULL
1120 && gspca_dev
->capt_file
!= file
) {
1125 /* if allocated, the buffers must not be mapped */
1126 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1127 if (gspca_dev
->frame
[i
].vma_use_count
) {
1133 /* stop streaming */
1134 if (gspca_dev
->streaming
) {
1135 mutex_lock(&gspca_dev
->usb_lock
);
1136 gspca_stream_off(gspca_dev
);
1137 mutex_unlock(&gspca_dev
->usb_lock
);
1140 /* free the previous allocated buffers, if any */
1141 if (gspca_dev
->nframes
!= 0) {
1142 frame_free(gspca_dev
);
1143 gspca_dev
->capt_file
= NULL
;
1145 if (rb
->count
== 0) /* unrequest */
1147 gspca_dev
->memory
= rb
->memory
;
1148 ret
= frame_alloc(gspca_dev
, rb
->count
);
1150 rb
->count
= gspca_dev
->nframes
;
1151 gspca_dev
->capt_file
= file
;
1154 mutex_unlock(&gspca_dev
->queue_lock
);
1155 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1159 static int vidioc_querybuf(struct file
*file
, void *priv
,
1160 struct v4l2_buffer
*v4l2_buf
)
1162 struct gspca_dev
*gspca_dev
= priv
;
1163 struct gspca_frame
*frame
;
1165 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1166 || v4l2_buf
->index
< 0
1167 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1170 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1171 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1175 static int vidioc_streamon(struct file
*file
, void *priv
,
1176 enum v4l2_buf_type buf_type
)
1178 struct gspca_dev
*gspca_dev
= priv
;
1181 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1183 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1184 return -ERESTARTSYS
;
1185 if (!gspca_dev
->present
) {
1189 if (gspca_dev
->nframes
== 0) {
1193 if (!gspca_dev
->streaming
) {
1194 ret
= gspca_init_transfer(gspca_dev
);
1199 if (gspca_debug
& D_STREAM
) {
1200 PDEBUG_MODE("stream on OK",
1208 mutex_unlock(&gspca_dev
->queue_lock
);
1212 static int vidioc_streamoff(struct file
*file
, void *priv
,
1213 enum v4l2_buf_type buf_type
)
1215 struct gspca_dev
*gspca_dev
= priv
;
1218 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1220 if (!gspca_dev
->streaming
)
1222 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1223 return -ERESTARTSYS
;
1225 /* stop streaming */
1226 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1230 gspca_stream_off(gspca_dev
);
1231 mutex_unlock(&gspca_dev
->usb_lock
);
1233 /* empty the application queues */
1234 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1235 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1236 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1237 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1238 gspca_dev
->sequence
= 0;
1239 atomic_set(&gspca_dev
->nevent
, 0);
1242 mutex_unlock(&gspca_dev
->queue_lock
);
1246 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1247 struct v4l2_jpegcompression
*jpegcomp
)
1249 struct gspca_dev
*gspca_dev
= priv
;
1252 if (!gspca_dev
->sd_desc
->get_jcomp
)
1254 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1255 return -ERESTARTSYS
;
1256 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1257 mutex_unlock(&gspca_dev
->usb_lock
);
1261 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1262 struct v4l2_jpegcompression
*jpegcomp
)
1264 struct gspca_dev
*gspca_dev
= priv
;
1267 if (!gspca_dev
->sd_desc
->set_jcomp
)
1269 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1270 return -ERESTARTSYS
;
1271 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1272 mutex_unlock(&gspca_dev
->usb_lock
);
1276 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1277 struct v4l2_streamparm
*parm
)
1279 struct gspca_dev
*gspca_dev
= priv
;
1281 memset(parm
, 0, sizeof *parm
);
1282 parm
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1283 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1287 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1288 struct v4l2_streamparm
*parm
)
1290 struct gspca_dev
*gspca_dev
= priv
;
1293 n
= parm
->parm
.capture
.readbuffers
;
1294 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1295 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1297 gspca_dev
->nbufread
= n
;
1301 static int vidioc_s_std(struct file
*filp
, void *priv
,
1307 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1308 static int vidiocgmbuf(struct file
*file
, void *priv
,
1309 struct video_mbuf
*mbuf
)
1311 struct gspca_dev
*gspca_dev
= file
->private_data
;
1314 PDEBUG(D_STREAM
, "cgmbuf");
1315 if (gspca_dev
->nframes
== 0) {
1319 struct v4l2_format fmt
;
1321 memset(&fmt
, 0, sizeof fmt
);
1322 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1323 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1324 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1325 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1326 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1327 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1332 struct v4l2_requestbuffers rb
;
1334 memset(&rb
, 0, sizeof rb
);
1336 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1337 rb
.memory
= V4L2_MEMORY_MMAP
;
1338 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1343 mbuf
->frames
= gspca_dev
->nframes
;
1344 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1345 for (i
= 0; i
< mbuf
->frames
; i
++)
1346 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1351 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1353 struct gspca_dev
*gspca_dev
= file
->private_data
;
1354 struct gspca_frame
*frame
;
1356 unsigned long addr
, start
, size
;
1359 start
= vma
->vm_start
;
1360 size
= vma
->vm_end
- vma
->vm_start
;
1361 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1363 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1364 return -ERESTARTSYS
;
1365 if (!gspca_dev
->present
) {
1369 if (gspca_dev
->capt_file
!= file
) {
1375 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1376 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1377 PDEBUG(D_STREAM
, "mmap bad memory type");
1380 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1382 frame
= &gspca_dev
->frame
[i
];
1386 if (frame
== NULL
) {
1387 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1391 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1392 /* v4l1 maps all the buffers */
1394 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1396 if (size
!= frame
->v4l2_buf
.length
) {
1397 PDEBUG(D_STREAM
, "mmap bad size");
1403 * - VM_IO marks the area as being a mmaped region for I/O to a
1404 * device. It also prevents the region from being core dumped.
1406 vma
->vm_flags
|= VM_IO
;
1408 addr
= (unsigned long) frame
->data
;
1410 page
= vmalloc_to_page((void *) addr
);
1411 ret
= vm_insert_page(vma
, start
, page
);
1419 vma
->vm_ops
= &gspca_vm_ops
;
1420 vma
->vm_private_data
= frame
;
1424 mutex_unlock(&gspca_dev
->queue_lock
);
1429 * wait for a video frame
1431 * If a frame is ready, its index is returned.
1433 static int frame_wait(struct gspca_dev
*gspca_dev
,
1436 struct gspca_frame
*frame
;
1439 /* check if a frame is ready */
1440 i
= gspca_dev
->fr_o
;
1441 j
= gspca_dev
->fr_queue
[i
];
1442 frame
= &gspca_dev
->frame
[j
];
1443 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) {
1444 atomic_dec(&gspca_dev
->nevent
);
1447 if (nonblock_ing
) /* no frame yet */
1450 /* wait till a frame is ready */
1452 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1453 atomic_read(&gspca_dev
->nevent
) > 0,
1454 msecs_to_jiffies(3000));
1457 return ret
; /* interrupt */
1458 return -EIO
; /* timeout */
1460 atomic_dec(&gspca_dev
->nevent
);
1461 if (!gspca_dev
->streaming
|| !gspca_dev
->present
)
1463 i
= gspca_dev
->fr_o
;
1464 j
= gspca_dev
->fr_queue
[i
];
1465 frame
= &gspca_dev
->frame
[j
];
1466 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1470 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1471 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1476 if (gspca_dev
->sd_desc
->dq_callback
) {
1477 mutex_lock(&gspca_dev
->usb_lock
);
1478 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1479 mutex_unlock(&gspca_dev
->usb_lock
);
1485 * dequeue a video buffer
1487 * If nonblock_ing is false, block until a buffer is available.
1489 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1490 struct v4l2_buffer
*v4l2_buf
)
1492 struct gspca_dev
*gspca_dev
= priv
;
1493 struct gspca_frame
*frame
;
1496 PDEBUG(D_FRAM
, "dqbuf");
1497 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1499 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1502 /* if not streaming, be sure the application will not loop forever */
1503 if (!(file
->f_flags
& O_NONBLOCK
)
1504 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1507 /* only the capturing file may dequeue */
1508 if (gspca_dev
->capt_file
!= file
)
1511 /* only one dequeue / read at a time */
1512 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1513 return -ERESTARTSYS
;
1515 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1518 i
= ret
; /* frame index */
1519 frame
= &gspca_dev
->frame
[i
];
1520 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1521 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1523 frame
->v4l2_buf
.bytesused
)) {
1524 PDEBUG(D_ERR
|D_STREAM
,
1525 "dqbuf cp to user failed");
1530 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1531 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1532 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1535 mutex_unlock(&gspca_dev
->read_lock
);
1540 * queue a video buffer
1542 * Attempting to queue a buffer that has already been
1543 * queued will return -EINVAL.
1545 static int vidioc_qbuf(struct file
*file
, void *priv
,
1546 struct v4l2_buffer
*v4l2_buf
)
1548 struct gspca_dev
*gspca_dev
= priv
;
1549 struct gspca_frame
*frame
;
1552 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1553 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1556 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1557 return -ERESTARTSYS
;
1559 index
= v4l2_buf
->index
;
1560 if ((unsigned) index
>= gspca_dev
->nframes
) {
1562 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1566 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1567 PDEBUG(D_FRAM
, "qbuf bad memory type");
1572 frame
= &gspca_dev
->frame
[index
];
1573 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1574 PDEBUG(D_FRAM
, "qbuf bad state");
1579 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1581 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1582 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1583 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1586 /* put the buffer in the 'queued' queue */
1587 i
= gspca_dev
->fr_q
;
1588 gspca_dev
->fr_queue
[i
] = index
;
1589 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1590 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1595 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1596 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1599 mutex_unlock(&gspca_dev
->queue_lock
);
1604 * allocate the resources for read()
1606 static int read_alloc(struct gspca_dev
*gspca_dev
,
1609 struct v4l2_buffer v4l2_buf
;
1612 PDEBUG(D_STREAM
, "read alloc");
1613 if (gspca_dev
->nframes
== 0) {
1614 struct v4l2_requestbuffers rb
;
1616 memset(&rb
, 0, sizeof rb
);
1617 rb
.count
= gspca_dev
->nbufread
;
1618 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1619 rb
.memory
= GSPCA_MEMORY_READ
;
1620 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1622 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1625 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1626 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1627 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1628 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1630 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1632 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1636 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1639 /* start streaming */
1640 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1642 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1646 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1648 struct gspca_dev
*gspca_dev
= file
->private_data
;
1651 PDEBUG(D_FRAM
, "poll");
1653 poll_wait(file
, &gspca_dev
->wq
, wait
);
1654 if (!gspca_dev
->present
)
1657 /* if reqbufs is not done, the user would use read() */
1658 if (gspca_dev
->nframes
== 0) {
1659 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
1660 return POLLERR
; /* not the 1st time */
1661 ret
= read_alloc(gspca_dev
, file
);
1666 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1668 if (!gspca_dev
->present
) {
1673 /* check the next incoming buffer */
1674 i
= gspca_dev
->fr_o
;
1675 i
= gspca_dev
->fr_queue
[i
];
1676 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1677 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1681 mutex_unlock(&gspca_dev
->queue_lock
);
1685 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1686 size_t count
, loff_t
*ppos
)
1688 struct gspca_dev
*gspca_dev
= file
->private_data
;
1689 struct gspca_frame
*frame
;
1690 struct v4l2_buffer v4l2_buf
;
1691 struct timeval timestamp
;
1694 PDEBUG(D_FRAM
, "read (%zd)", count
);
1695 if (!gspca_dev
->present
)
1697 switch (gspca_dev
->memory
) {
1698 case GSPCA_MEMORY_NO
: /* first time */
1699 ret
= read_alloc(gspca_dev
, file
);
1703 case GSPCA_MEMORY_READ
:
1704 if (gspca_dev
->capt_file
== file
)
1712 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1716 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1717 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1718 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1719 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1721 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1725 /* if the process slept for more than 1 second,
1726 * get a newer frame */
1727 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1729 break; /* avoid infinite loop */
1730 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1732 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1734 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1739 /* copy the frame */
1740 if (count
> frame
->v4l2_buf
.bytesused
)
1741 count
= frame
->v4l2_buf
.bytesused
;
1742 ret
= copy_to_user(data
, frame
->data
, count
);
1744 PDEBUG(D_ERR
|D_STREAM
,
1745 "read cp to user lack %d / %zd", ret
, count
);
1751 /* in each case, requeue the buffer */
1752 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1758 static struct file_operations dev_fops
= {
1759 .owner
= THIS_MODULE
,
1761 .release
= dev_close
,
1764 .ioctl
= video_ioctl2
,
1765 #ifdef CONFIG_COMPAT
1766 .compat_ioctl
= v4l_compat_ioctl32
,
1768 .llseek
= no_llseek
,
1772 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1773 .vidioc_querycap
= vidioc_querycap
,
1774 .vidioc_dqbuf
= vidioc_dqbuf
,
1775 .vidioc_qbuf
= vidioc_qbuf
,
1776 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1777 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1778 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1779 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1780 .vidioc_streamon
= vidioc_streamon
,
1781 .vidioc_queryctrl
= vidioc_queryctrl
,
1782 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1783 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1784 .vidioc_querymenu
= vidioc_querymenu
,
1785 .vidioc_enum_input
= vidioc_enum_input
,
1786 .vidioc_g_input
= vidioc_g_input
,
1787 .vidioc_s_input
= vidioc_s_input
,
1788 .vidioc_reqbufs
= vidioc_reqbufs
,
1789 .vidioc_querybuf
= vidioc_querybuf
,
1790 .vidioc_streamoff
= vidioc_streamoff
,
1791 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1792 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1793 .vidioc_g_parm
= vidioc_g_parm
,
1794 .vidioc_s_parm
= vidioc_s_parm
,
1795 .vidioc_s_std
= vidioc_s_std
,
1796 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1797 .vidiocgmbuf
= vidiocgmbuf
,
1801 static struct video_device gspca_template
= {
1802 .name
= "gspca main driver",
1804 .ioctl_ops
= &dev_ioctl_ops
,
1805 .release
= video_device_release
,
1810 * probe and create a new gspca device
1812 * This function must be called by the sub-driver when it is
1813 * called for probing a new device.
1815 int gspca_dev_probe(struct usb_interface
*intf
,
1816 const struct usb_device_id
*id
,
1817 const struct sd_desc
*sd_desc
,
1819 struct module
*module
)
1821 struct usb_interface_descriptor
*interface
;
1822 struct gspca_dev
*gspca_dev
;
1823 struct usb_device
*dev
= interface_to_usbdev(intf
);
1826 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1828 /* we don't handle multi-config cameras */
1829 if (dev
->descriptor
.bNumConfigurations
!= 1)
1831 interface
= &intf
->cur_altsetting
->desc
;
1832 if (interface
->bInterfaceNumber
> 0)
1835 /* create the device */
1836 if (dev_size
< sizeof *gspca_dev
)
1837 dev_size
= sizeof *gspca_dev
;
1838 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1840 err("couldn't kzalloc gspca struct");
1843 kref_init(&gspca_dev
->kref
);
1844 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
1845 if (!gspca_dev
->usb_buf
) {
1846 err("out of memory");
1850 gspca_dev
->dev
= dev
;
1851 gspca_dev
->iface
= interface
->bInterfaceNumber
;
1852 gspca_dev
->nbalt
= intf
->num_altsetting
;
1853 gspca_dev
->sd_desc
= sd_desc
;
1854 gspca_dev
->nbufread
= 2;
1856 /* configure the subdriver and initialize the USB device */
1857 ret
= gspca_dev
->sd_desc
->config(gspca_dev
, id
);
1860 ret
= gspca_dev
->sd_desc
->init(gspca_dev
);
1863 ret
= gspca_set_alt0(gspca_dev
);
1866 gspca_set_default_mode(gspca_dev
);
1868 mutex_init(&gspca_dev
->usb_lock
);
1869 mutex_init(&gspca_dev
->read_lock
);
1870 mutex_init(&gspca_dev
->queue_lock
);
1871 init_waitqueue_head(&gspca_dev
->wq
);
1873 /* init video stuff */
1874 gspca_dev
->vdev
= video_device_alloc();
1875 memcpy(gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
1876 gspca_dev
->vdev
->parent
= &dev
->dev
;
1877 gspca_dev
->module
= module
;
1878 gspca_dev
->present
= 1;
1879 video_set_drvdata(gspca_dev
->vdev
, gspca_dev
);
1880 ret
= video_register_device(gspca_dev
->vdev
,
1884 err("video_register_device err %d", ret
);
1885 video_device_release(gspca_dev
->vdev
);
1889 usb_set_intfdata(intf
, gspca_dev
);
1890 PDEBUG(D_PROBE
, "probe ok");
1893 kfree(gspca_dev
->usb_buf
);
1897 EXPORT_SYMBOL(gspca_dev_probe
);
1902 * This function must be called by the sub-driver
1903 * when the device disconnects, after the specific resources are freed.
1905 void gspca_disconnect(struct usb_interface
*intf
)
1907 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1909 usb_set_intfdata(intf
, NULL
);
1911 /* We don't want people trying to open up the device */
1912 video_unregister_device(gspca_dev
->vdev
);
1914 gspca_dev
->present
= 0;
1915 gspca_dev
->streaming
= 0;
1917 kref_put(&gspca_dev
->kref
, gspca_delete
);
1919 PDEBUG(D_PROBE
, "disconnect complete");
1921 EXPORT_SYMBOL(gspca_disconnect
);
1924 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1926 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1928 if (!gspca_dev
->streaming
)
1930 gspca_dev
->frozen
= 1; /* avoid urb error messages */
1931 if (gspca_dev
->sd_desc
->stopN
)
1932 gspca_dev
->sd_desc
->stopN(gspca_dev
);
1933 destroy_urbs(gspca_dev
);
1934 gspca_set_alt0(gspca_dev
);
1935 if (gspca_dev
->sd_desc
->stop0
)
1936 gspca_dev
->sd_desc
->stop0(gspca_dev
);
1939 EXPORT_SYMBOL(gspca_suspend
);
1941 int gspca_resume(struct usb_interface
*intf
)
1943 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1945 gspca_dev
->frozen
= 0;
1946 gspca_dev
->sd_desc
->init(gspca_dev
);
1947 if (gspca_dev
->streaming
)
1948 return gspca_init_transfer(gspca_dev
);
1951 EXPORT_SYMBOL(gspca_resume
);
1953 /* -- cam driver utility functions -- */
1955 /* auto gain and exposure algorithm based on the knee algorithm described here:
1956 http://ytse.tricolour.net/docs/LowLightOptimization.html
1958 Returns 0 if no changes were made, 1 if the gain and or exposure settings
1960 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
1961 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
1963 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
1964 const struct ctrl
*gain_ctrl
= NULL
;
1965 const struct ctrl
*exposure_ctrl
= NULL
;
1966 const struct ctrl
*autogain_ctrl
= NULL
;
1969 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1970 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
1971 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1972 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
1973 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1974 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
1975 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
1977 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
1978 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
1979 "on cam without (auto)gain/exposure");
1983 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
1984 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
1985 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
1989 orig_exposure
= exposure
;
1991 /* If we are of a multiple of deadzone, do multiple steps to reach the
1992 desired lumination fast (with the risc of a slight overshoot) */
1993 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
1995 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
1996 avg_lum
, desired_avg_lum
, steps
);
1998 for (i
= 0; i
< steps
; i
++) {
1999 if (avg_lum
> desired_avg_lum
) {
2000 if (gain
> gain_knee
)
2002 else if (exposure
> exposure_knee
)
2004 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2006 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2008 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2013 if (gain
< gain_ctrl
->qctrl
.default_value
)
2015 else if (exposure
< exposure_knee
)
2017 else if (gain
< gain_knee
)
2019 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2021 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2028 if (gain
!= orig_gain
) {
2029 gain_ctrl
->set(gspca_dev
, gain
);
2032 if (exposure
!= orig_exposure
) {
2033 exposure_ctrl
->set(gspca_dev
, exposure
);
2039 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2041 /* -- module insert / remove -- */
2042 static int __init
gspca_init(void)
2044 info("main v%d.%d.%d registered",
2045 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2046 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2047 DRIVER_VERSION_NUMBER
& 0xff);
2050 static void __exit
gspca_exit(void)
2052 info("main deregistered");
2055 module_init(gspca_init
);
2056 module_exit(gspca_exit
);
2059 module_param_named(debug
, gspca_debug
, int, 0644);
2060 MODULE_PARM_DESC(debug
,
2061 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2062 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"