2 * Main USB camera driver
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define MODULE_NAME "gspca"
23 #include <linux/init.h>
25 #include <linux/vmalloc.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/pagemap.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
39 #define DEF_NURBS 2 /* default number of URBs (mmap) */
40 #define USR_NURBS 5 /* default number of URBs (userptr) */
42 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
43 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 5)
47 static const char version
[] = "2.1.5";
49 static int video_nr
= -1;
51 #ifdef CONFIG_VIDEO_ADV_DEBUG
52 int gspca_debug
= D_ERR
| D_PROBE
;
53 EXPORT_SYMBOL(gspca_debug
);
55 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
57 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
58 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
62 (pixfmt
>> 16) & 0xff,
66 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
73 #define PDEBUG_MODE(txt, pixfmt, w, h)
76 /* specific memory types - !! should different from V4L2_MEMORY_xxx */
77 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
78 #define GSPCA_MEMORY_READ 7
80 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
85 static void gspca_vm_open(struct vm_area_struct
*vma
)
87 struct gspca_frame
*frame
= vma
->vm_private_data
;
89 frame
->vma_use_count
++;
90 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
93 static void gspca_vm_close(struct vm_area_struct
*vma
)
95 struct gspca_frame
*frame
= vma
->vm_private_data
;
97 if (--frame
->vma_use_count
<= 0)
98 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
101 static struct vm_operations_struct gspca_vm_ops
= {
102 .open
= gspca_vm_open
,
103 .close
= gspca_vm_close
,
107 * fill a video frame from an URB and resubmit
109 static void fill_frame(struct gspca_dev
*gspca_dev
,
112 struct gspca_frame
*frame
;
113 __u8
*data
; /* address of data in the iso message */
117 if (urb
->status
!= 0) {
118 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
119 return; /* disconnection ? */
121 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
122 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
124 /* check the availability of the frame buffer */
126 j
= gspca_dev
->fr_queue
[j
];
127 frame
= &gspca_dev
->frame
[j
];
128 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
129 != V4L2_BUF_FLAG_QUEUED
) {
130 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
134 /* check the packet status and length */
135 len
= urb
->iso_frame_desc
[i
].actual_length
;
138 st
= urb
->iso_frame_desc
[i
].status
;
141 "ISOC data error: [%d] len=%d, status=%d",
143 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
147 /* let the packet be analyzed by the subdriver */
148 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
149 i
, urb
->iso_frame_desc
[i
].offset
, len
);
150 data
= (__u8
*) urb
->transfer_buffer
151 + urb
->iso_frame_desc
[i
].offset
;
152 pkt_scan(gspca_dev
, frame
, data
, len
);
155 /* resubmit the URB */
156 /*fixme: don't do that when userptr and too many URBs sent*/
158 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
160 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
164 * ISOC message interrupt from the USB device
166 * Analyse each packet and call the subdriver for copy
167 * to the frame buffer.
169 * There are 2 functions:
170 * - the first one (isoc_irq_mmap) is used when the application
171 * buffers are mapped. The frame detection and copy is done
172 * at interrupt level.
173 * - the second one (isoc_irq_user) is used when the application
174 * buffers are in user space (userptr). The frame detection
175 * and copy is done by the application.
177 static void isoc_irq_mmap(struct urb
*urb
180 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
182 PDEBUG(D_PACK
, "isoc irq mmap");
183 if (!gspca_dev
->streaming
)
185 fill_frame(gspca_dev
, urb
);
188 static void isoc_irq_user(struct urb
*urb
191 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
194 PDEBUG(D_PACK
, "isoc irq user");
195 if (!gspca_dev
->streaming
)
198 i
= gspca_dev
->urb_in
% gspca_dev
->nurbs
;
199 if (urb
!= gspca_dev
->urb
[i
]) {
200 PDEBUG(D_ERR
|D_PACK
, "urb out of sequence");
201 return; /* should never occur */
205 atomic_inc(&gspca_dev
->nevent
); /* new event */
206 wake_up_interruptible(&gspca_dev
->wq
);
207 /*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
211 * treat the isoc messages
213 * This routine is called by the application (case userptr).
215 static void isoc_transfer(struct gspca_dev
*gspca_dev
)
221 i
= gspca_dev
->urb_out
;
222 PDEBUG(D_PACK
, "isoc transf i:%d o:%d", gspca_dev
->urb_in
, i
);
223 if (i
== gspca_dev
->urb_in
) /* isoc message to read */
224 break; /* no (more) message */
225 atomic_dec(&gspca_dev
->nevent
);
226 /*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
227 gspca_dev
->urb_out
= i
+ 1; /* message treated */
228 urb
= gspca_dev
->urb
[i
% gspca_dev
->nurbs
];
229 fill_frame(gspca_dev
, urb
);
234 * add data to the current frame
236 * This function is called by the subdrivers at interrupt level
238 * To build a frame, these ones must add
240 * - 0 or many INTER_PACKETs
242 * DISCARD_PACKET invalidates the whole frame.
243 * On LAST_PACKET, a new frame is returned.
245 struct gspca_frame
*gspca_frame_add(struct gspca_dev
*gspca_dev
,
247 struct gspca_frame
*frame
,
253 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
255 /* when start of a new frame, if the current frame buffer
256 * is not queued, discard the whole frame */
257 if (packet_type
== FIRST_PACKET
) {
258 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
259 != V4L2_BUF_FLAG_QUEUED
) {
260 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
263 frame
->data_end
= frame
->data
;
264 jiffies_to_timeval(get_jiffies_64(),
265 &frame
->v4l2_buf
.timestamp
);
266 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
267 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
271 /* append the packet to the frame buffer */
273 if (frame
->data_end
- frame
->data
+ len
274 > frame
->v4l2_buf
.length
) {
275 PDEBUG(D_ERR
|D_PACK
, "frame overflow %d > %d",
276 frame
->data_end
- frame
->data
+ len
,
277 frame
->v4l2_buf
.length
);
278 packet_type
= DISCARD_PACKET
;
280 if (frame
->v4l2_buf
.memory
!= V4L2_MEMORY_USERPTR
) {
281 memcpy(frame
->data_end
, data
, len
);
283 if (copy_to_user(frame
->data_end
,
286 "copy to user failed");
287 packet_type
= DISCARD_PACKET
;
290 frame
->data_end
+= len
;
293 gspca_dev
->last_packet_type
= packet_type
;
295 /* if last packet, wake the application and advance in the queue */
296 if (packet_type
== LAST_PACKET
) {
297 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
298 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
299 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
300 atomic_inc(&gspca_dev
->nevent
);
301 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
302 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
304 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
305 frame
->v4l2_buf
.bytesused
,
309 j
= gspca_dev
->fr_queue
[i
];
310 frame
= &gspca_dev
->frame
[j
];
314 EXPORT_SYMBOL(gspca_frame_add
);
316 static int gspca_is_compressed(__u32 format
)
319 case V4L2_PIX_FMT_MJPEG
:
320 case V4L2_PIX_FMT_JPEG
:
321 case V4L2_PIX_FMT_SPCA561
:
322 case V4L2_PIX_FMT_PAC207
:
328 static void *rvmalloc(unsigned long size
)
333 /* size = PAGE_ALIGN(size); (already done) */
334 mem
= vmalloc_32(size
);
336 memset(mem
, 0, size
);
337 adr
= (unsigned long) mem
;
338 while ((long) size
> 0) {
339 SetPageReserved(vmalloc_to_page((void *) adr
));
347 static void rvfree(void *mem
, unsigned long size
)
353 adr
= (unsigned long) mem
;
354 while ((long) size
> 0) {
355 ClearPageReserved(vmalloc_to_page((void *) adr
));
362 static int frame_alloc(struct gspca_dev
*gspca_dev
,
365 struct gspca_frame
*frame
;
369 i
= gspca_dev
->curr_mode
;
370 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
371 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
372 frsz
= PAGE_ALIGN(frsz
);
373 PDEBUG(D_STREAM
, "new fr_sz: %d", frsz
);
374 gspca_dev
->frsz
= frsz
;
375 if (count
> GSPCA_MAX_FRAMES
)
376 count
= GSPCA_MAX_FRAMES
;
377 if (gspca_dev
->memory
== V4L2_MEMORY_MMAP
) {
378 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
379 if (!gspca_dev
->frbuf
) {
380 err("frame alloc failed");
384 gspca_dev
->nframes
= count
;
385 for (i
= 0; i
< count
; i
++) {
386 frame
= &gspca_dev
->frame
[i
];
387 frame
->v4l2_buf
.index
= i
;
388 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
389 frame
->v4l2_buf
.flags
= 0;
390 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
391 frame
->v4l2_buf
.length
= frsz
;
392 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
393 frame
->v4l2_buf
.sequence
= 0;
394 if (gspca_dev
->memory
== V4L2_MEMORY_MMAP
) {
395 frame
->data
= frame
->data_end
=
396 gspca_dev
->frbuf
+ i
* frsz
;
397 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
400 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
401 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
402 gspca_dev
->sequence
= 0;
403 atomic_set(&gspca_dev
->nevent
, 0);
407 static void frame_free(struct gspca_dev
*gspca_dev
)
411 PDEBUG(D_STREAM
, "frame free");
412 if (gspca_dev
->frbuf
!= NULL
) {
413 rvfree(gspca_dev
->frbuf
,
414 gspca_dev
->nframes
* gspca_dev
->frsz
);
415 gspca_dev
->frbuf
= NULL
;
416 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
417 gspca_dev
->frame
[i
].data
= NULL
;
419 gspca_dev
->nframes
= 0;
422 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
427 PDEBUG(D_STREAM
, "kill transfer");
428 for (i
= 0; i
< MAX_NURBS
; ++i
) {
429 urb
= gspca_dev
->urb
[i
];
433 gspca_dev
->urb
[i
] = NULL
;
435 if (urb
->transfer_buffer
!= NULL
)
436 usb_buffer_free(gspca_dev
->dev
,
437 urb
->transfer_buffer_length
,
438 urb
->transfer_buffer
,
445 * search an input isochronous endpoint in an alternate setting
447 static struct usb_host_endpoint
*alt_isoc(struct usb_host_interface
*alt
,
450 struct usb_host_endpoint
*ep
;
453 epaddr
|= USB_DIR_IN
;
454 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
455 ep
= &alt
->endpoint
[i
];
456 if (ep
->desc
.bEndpointAddress
== epaddr
) {
457 attr
= ep
->desc
.bmAttributes
458 & USB_ENDPOINT_XFERTYPE_MASK
;
459 if (attr
== USB_ENDPOINT_XFER_ISOC
)
468 * search an input isochronous endpoint
470 * The endpoint is defined by the subdriver.
471 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
472 * This routine may be called many times when the bandwidth is too small
473 * (the bandwidth is checked on urb submit).
475 struct usb_host_endpoint
*get_isoc_ep(struct gspca_dev
*gspca_dev
)
477 struct usb_interface
*intf
;
478 struct usb_host_endpoint
*ep
;
481 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
483 i
= gspca_dev
->alt
; /* previous alt setting */
484 while (--i
> 0) { /* alt 0 is unusable */
485 ep
= alt_isoc(&intf
->altsetting
[i
], gspca_dev
->cam
.epaddr
);
490 err("no ISOC endpoint found");
493 PDEBUG(D_STREAM
, "use ISOC alt %d ep 0x%02x",
494 i
, ep
->desc
.bEndpointAddress
);
495 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
497 err("set interface err %d", ret
);
500 gspca_dev
->alt
= i
; /* memorize the current alt setting */
505 * create the isochronous URBs
507 static int create_urbs(struct gspca_dev
*gspca_dev
,
508 struct usb_host_endpoint
*ep
)
511 int n
, nurbs
, i
, psize
, npkt
, bsize
;
512 usb_complete_t usb_complete
;
514 /* calculate the packet size and the number of packets */
515 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
517 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
518 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
519 npkt
= ISO_MAX_SIZE
/ psize
;
520 if (npkt
> ISO_MAX_PKT
)
522 bsize
= psize
* npkt
;
524 "isoc %d pkts size %d (bsize:%d)", npkt
, psize
, bsize
);
525 /*fixme:don't submit all URBs when userptr*/
526 if (gspca_dev
->memory
!= V4L2_MEMORY_USERPTR
) {
527 usb_complete
= isoc_irq_mmap
;
530 usb_complete
= isoc_irq_user
;
533 gspca_dev
->nurbs
= nurbs
;
534 for (n
= 0; n
< nurbs
; n
++) {
535 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
537 err("usb_alloc_urb failed");
540 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
545 if (urb
->transfer_buffer
== NULL
) {
547 destroy_urbs(gspca_dev
);
548 err("usb_buffer_urb failed");
551 gspca_dev
->urb
[n
] = urb
;
552 urb
->dev
= gspca_dev
->dev
;
553 urb
->context
= gspca_dev
;
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
= usb_complete
;
560 urb
->number_of_packets
= npkt
;
561 urb
->transfer_buffer_length
= bsize
;
562 for (i
= 0; i
< npkt
; i
++) {
563 urb
->iso_frame_desc
[i
].length
= psize
;
564 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
567 gspca_dev
->urb_in
= gspca_dev
->urb_out
= 0;
572 * start the USB transfer
574 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
576 struct usb_host_endpoint
*ep
;
579 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
582 /* set the higher alternate setting and
583 * loop until urb submit succeeds */
584 gspca_dev
->alt
= gspca_dev
->nbalt
;
586 PDEBUG(D_STREAM
, "init transfer alt %d", gspca_dev
->alt
);
587 ep
= get_isoc_ep(gspca_dev
);
592 ret
= create_urbs(gspca_dev
, ep
);
597 gspca_dev
->sd_desc
->start(gspca_dev
);
598 gspca_dev
->streaming
= 1;
599 atomic_set(&gspca_dev
->nevent
, 0);
601 /* submit the URBs */
602 for (n
= 0; n
< gspca_dev
->nurbs
; n
++) {
603 ret
= usb_submit_urb(gspca_dev
->urb
[n
], GFP_KERNEL
);
605 PDEBUG(D_ERR
|D_STREAM
,
606 "usb_submit_urb [%d] err %d", n
, ret
);
607 gspca_dev
->streaming
= 0;
608 destroy_urbs(gspca_dev
);
610 break; /* try the previous alt */
618 mutex_unlock(&gspca_dev
->usb_lock
);
622 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
626 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
628 PDEBUG(D_ERR
|D_STREAM
, "set interface 0 err %d", ret
);
632 /* Note both the queue and the usb lock should be hold when calling this */
633 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
635 gspca_dev
->streaming
= 0;
636 atomic_set(&gspca_dev
->nevent
, 0);
637 if (gspca_dev
->present
) {
638 gspca_dev
->sd_desc
->stopN(gspca_dev
);
639 destroy_urbs(gspca_dev
);
640 gspca_set_alt0(gspca_dev
);
641 gspca_dev
->sd_desc
->stop0(gspca_dev
);
642 PDEBUG(D_STREAM
, "stream off OK");
644 destroy_urbs(gspca_dev
);
645 atomic_inc(&gspca_dev
->nevent
);
646 wake_up_interruptible(&gspca_dev
->wq
);
647 PDEBUG(D_ERR
|D_STREAM
, "stream off no device ??");
651 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
655 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
656 gspca_dev
->curr_mode
= i
;
657 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
658 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
659 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
662 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
663 int width
, int height
)
667 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
668 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
669 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
676 * search a mode with the right pixel format
678 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
684 modeU
= modeD
= mode
;
685 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
687 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
691 if (++modeU
< gspca_dev
->cam
.nmodes
) {
692 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
700 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
701 struct v4l2_fmtdesc
*fmtdesc
)
703 struct gspca_dev
*gspca_dev
= priv
;
707 /* give an index to each format */
710 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
711 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
714 if (fmt_tb
[j
] == fmt_tb
[index
])
719 if (fmtdesc
->index
== index
)
720 break; /* new format */
722 if (index
>= sizeof fmt_tb
/ sizeof fmt_tb
[0])
727 return -EINVAL
; /* no more format */
729 fmtdesc
->pixelformat
= fmt_tb
[index
];
730 if (gspca_is_compressed(fmt_tb
[index
]))
731 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
732 fmtdesc
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
733 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
734 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
735 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
736 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
737 fmtdesc
->description
[4] = '\0';
741 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
742 struct v4l2_format
*fmt
)
744 struct gspca_dev
*gspca_dev
= priv
;
747 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
749 mode
= gspca_dev
->curr_mode
;
750 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
751 sizeof fmt
->fmt
.pix
);
755 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
756 struct v4l2_format
*fmt
)
758 int w
, h
, mode
, mode2
;
760 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
762 w
= fmt
->fmt
.pix
.width
;
763 h
= fmt
->fmt
.pix
.height
;
765 /* (luvcview problem) */
766 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_MJPEG
)
767 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_JPEG
;
768 #ifdef CONFIG_VIDEO_ADV_DEBUG
769 if (gspca_debug
& D_CONF
)
770 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
772 /* search the closest mode for width and height */
773 mode
= wxh_to_mode(gspca_dev
, w
, h
);
775 /* OK if right palette */
776 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
777 != fmt
->fmt
.pix
.pixelformat
) {
779 /* else, search the closest mode with the same pixel format */
780 mode2
= gspca_get_mode(gspca_dev
, mode
,
781 fmt
->fmt
.pix
.pixelformat
);
785 ; * no chance, return this mode */
787 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
788 sizeof fmt
->fmt
.pix
);
789 return mode
; /* used when s_fmt */
792 static int vidioc_try_fmt_vid_cap(struct file
*file
,
794 struct v4l2_format
*fmt
)
796 struct gspca_dev
*gspca_dev
= priv
;
799 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
805 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
806 struct v4l2_format
*fmt
)
808 struct gspca_dev
*gspca_dev
= priv
;
811 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
814 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
818 if (gspca_dev
->nframes
!= 0
819 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
824 if (ret
== gspca_dev
->curr_mode
) {
826 goto out
; /* same mode */
829 if (gspca_dev
->streaming
) {
833 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
834 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
835 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
836 gspca_dev
->curr_mode
= ret
;
840 mutex_unlock(&gspca_dev
->queue_lock
);
844 static int dev_open(struct inode
*inode
, struct file
*file
)
846 struct gspca_dev
*gspca_dev
;
849 PDEBUG(D_STREAM
, "%s open", current
->comm
);
850 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
851 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
853 if (!gspca_dev
->present
) {
858 /* if not done yet, initialize the sensor */
859 if (gspca_dev
->users
== 0) {
860 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
864 ret
= gspca_dev
->sd_desc
->open(gspca_dev
);
865 mutex_unlock(&gspca_dev
->usb_lock
);
867 PDEBUG(D_ERR
|D_CONF
, "init device failed %d", ret
);
870 } else if (gspca_dev
->users
> 4) { /* (arbitrary value) */
875 file
->private_data
= gspca_dev
;
876 #ifdef CONFIG_VIDEO_ADV_DEBUG
877 /* activate the v4l2 debug */
878 if (gspca_debug
& D_V4L2
)
879 gspca_dev
->vdev
.debug
|= 3;
881 gspca_dev
->vdev
.debug
&= ~3;
884 mutex_unlock(&gspca_dev
->queue_lock
);
886 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
888 PDEBUG(D_STREAM
, "open done");
892 static int dev_close(struct inode
*inode
, struct file
*file
)
894 struct gspca_dev
*gspca_dev
= file
->private_data
;
896 PDEBUG(D_STREAM
, "%s close", current
->comm
);
897 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
901 /* if the file did capture, free the streaming resources */
902 if (gspca_dev
->capt_file
== file
) {
903 mutex_lock(&gspca_dev
->usb_lock
);
904 if (gspca_dev
->streaming
)
905 gspca_stream_off(gspca_dev
);
906 gspca_dev
->sd_desc
->close(gspca_dev
);
907 mutex_unlock(&gspca_dev
->usb_lock
);
908 frame_free(gspca_dev
);
909 gspca_dev
->capt_file
= NULL
;
910 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
912 file
->private_data
= NULL
;
913 mutex_unlock(&gspca_dev
->queue_lock
);
914 PDEBUG(D_STREAM
, "close done");
918 static int vidioc_querycap(struct file
*file
, void *priv
,
919 struct v4l2_capability
*cap
)
921 struct gspca_dev
*gspca_dev
= priv
;
923 memset(cap
, 0, sizeof *cap
);
924 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
925 strncpy(cap
->card
, gspca_dev
->cam
.dev_name
, sizeof cap
->card
);
926 strncpy(cap
->bus_info
, gspca_dev
->dev
->bus
->bus_name
,
927 sizeof cap
->bus_info
);
928 cap
->version
= DRIVER_VERSION_NUMBER
;
929 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
931 | V4L2_CAP_READWRITE
;
935 /* the use of V4L2_CTRL_FLAG_NEXT_CTRL asks for the controls to be sorted */
936 static int vidioc_queryctrl(struct file
*file
, void *priv
,
937 struct v4l2_queryctrl
*q_ctrl
)
939 struct gspca_dev
*gspca_dev
= priv
;
944 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
945 id
&= V4L2_CTRL_ID_MASK
;
947 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
948 if (id
>= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
950 &gspca_dev
->sd_desc
->ctrls
[i
].qctrl
,
957 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
958 if (id
== gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
) {
960 &gspca_dev
->sd_desc
->ctrls
[i
].qctrl
,
965 if (id
>= V4L2_CID_BASE
966 && id
<= V4L2_CID_LASTP1
) {
967 q_ctrl
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
973 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
974 struct v4l2_control
*ctrl
)
976 struct gspca_dev
*gspca_dev
= priv
;
977 const struct ctrl
*ctrls
;
980 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
981 i
< gspca_dev
->sd_desc
->nctrls
;
983 if (ctrl
->id
!= ctrls
->qctrl
.id
)
985 if (ctrl
->value
< ctrls
->qctrl
.minimum
986 && ctrl
->value
> ctrls
->qctrl
.maximum
)
988 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
989 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
991 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
992 mutex_unlock(&gspca_dev
->usb_lock
);
998 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
999 struct v4l2_control
*ctrl
)
1001 struct gspca_dev
*gspca_dev
= priv
;
1003 const struct ctrl
*ctrls
;
1006 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1007 i
< gspca_dev
->sd_desc
->nctrls
;
1009 if (ctrl
->id
!= ctrls
->qctrl
.id
)
1011 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1012 return -ERESTARTSYS
;
1013 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1014 mutex_unlock(&gspca_dev
->usb_lock
);
1020 static int vidioc_querymenu(struct file
*file
, void *priv
,
1021 struct v4l2_querymenu
*qmenu
)
1023 struct gspca_dev
*gspca_dev
= priv
;
1025 if (!gspca_dev
->sd_desc
->querymenu
)
1027 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1030 static int vidioc_enum_input(struct file
*file
, void *priv
,
1031 struct v4l2_input
*input
)
1033 struct gspca_dev
*gspca_dev
= priv
;
1035 if (input
->index
!= 0)
1037 memset(input
, 0, sizeof *input
);
1038 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1039 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1040 sizeof input
->name
);
1044 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1050 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1057 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1058 struct v4l2_requestbuffers
*rb
)
1060 struct gspca_dev
*gspca_dev
= priv
;
1063 if (rb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1065 switch (rb
->memory
) {
1066 case V4L2_MEMORY_MMAP
:
1067 case V4L2_MEMORY_USERPTR
:
1072 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1073 return -ERESTARTSYS
;
1075 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1076 if (gspca_dev
->frame
[i
].vma_use_count
) {
1082 /* only one file may do capture */
1083 if ((gspca_dev
->capt_file
!= NULL
&& gspca_dev
->capt_file
!= file
)
1084 || gspca_dev
->streaming
) {
1089 if (rb
->count
== 0) { /* unrequest? */
1090 frame_free(gspca_dev
);
1091 gspca_dev
->capt_file
= NULL
;
1093 gspca_dev
->memory
= rb
->memory
;
1094 ret
= frame_alloc(gspca_dev
, rb
->count
);
1096 rb
->count
= gspca_dev
->nframes
;
1097 gspca_dev
->capt_file
= file
;
1101 mutex_unlock(&gspca_dev
->queue_lock
);
1102 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1106 static int vidioc_querybuf(struct file
*file
, void *priv
,
1107 struct v4l2_buffer
*v4l2_buf
)
1109 struct gspca_dev
*gspca_dev
= priv
;
1110 struct gspca_frame
*frame
;
1112 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1113 || v4l2_buf
->index
< 0
1114 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1117 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1118 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1122 static int vidioc_streamon(struct file
*file
, void *priv
,
1123 enum v4l2_buf_type buf_type
)
1125 struct gspca_dev
*gspca_dev
= priv
;
1128 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1130 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1131 return -ERESTARTSYS
;
1132 if (!gspca_dev
->present
) {
1136 if (gspca_dev
->nframes
== 0) {
1140 if (gspca_dev
->capt_file
!= file
) {
1144 if (!gspca_dev
->streaming
) {
1145 ret
= gspca_init_transfer(gspca_dev
);
1149 #ifdef CONFIG_VIDEO_ADV_DEBUG
1150 if (gspca_debug
& D_STREAM
) {
1151 PDEBUG_MODE("stream on OK",
1159 mutex_unlock(&gspca_dev
->queue_lock
);
1163 static int vidioc_streamoff(struct file
*file
, void *priv
,
1164 enum v4l2_buf_type buf_type
)
1166 struct gspca_dev
*gspca_dev
= priv
;
1169 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1171 if (!gspca_dev
->streaming
)
1173 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1174 return -ERESTARTSYS
;
1175 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1179 if (gspca_dev
->capt_file
!= file
) {
1183 gspca_stream_off(gspca_dev
);
1186 mutex_unlock(&gspca_dev
->usb_lock
);
1188 mutex_unlock(&gspca_dev
->queue_lock
);
1192 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1193 struct v4l2_jpegcompression
*jpegcomp
)
1195 struct gspca_dev
*gspca_dev
= priv
;
1198 if (!gspca_dev
->sd_desc
->get_jcomp
)
1200 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1201 return -ERESTARTSYS
;
1202 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1203 mutex_unlock(&gspca_dev
->usb_lock
);
1207 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1208 struct v4l2_jpegcompression
*jpegcomp
)
1210 struct gspca_dev
*gspca_dev
= priv
;
1213 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1214 return -ERESTARTSYS
;
1215 if (!gspca_dev
->sd_desc
->set_jcomp
)
1217 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1218 mutex_unlock(&gspca_dev
->usb_lock
);
1222 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1223 struct v4l2_streamparm
*parm
)
1225 struct gspca_dev
*gspca_dev
= priv
;
1227 memset(parm
, 0, sizeof *parm
);
1228 parm
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1229 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1233 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1234 struct v4l2_streamparm
*parm
)
1236 struct gspca_dev
*gspca_dev
= priv
;
1239 n
= parm
->parm
.capture
.readbuffers
;
1240 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1241 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1243 gspca_dev
->nbufread
= n
;
1247 static int vidioc_s_std(struct file
*filp
, void *priv
,
1253 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1254 static int vidiocgmbuf(struct file
*file
, void *priv
,
1255 struct video_mbuf
*mbuf
)
1257 struct gspca_dev
*gspca_dev
= file
->private_data
;
1260 PDEBUG(D_STREAM
, "cgmbuf");
1261 if (gspca_dev
->nframes
== 0) {
1265 struct v4l2_format fmt
;
1267 memset(&fmt
, 0, sizeof fmt
);
1268 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1269 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1270 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1271 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1272 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1273 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1278 struct v4l2_requestbuffers rb
;
1280 memset(&rb
, 0, sizeof rb
);
1282 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1283 rb
.memory
= V4L2_MEMORY_MMAP
;
1284 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1289 mbuf
->frames
= gspca_dev
->nframes
;
1290 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1291 for (i
= 0; i
< mbuf
->frames
; i
++)
1292 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1297 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1299 struct gspca_dev
*gspca_dev
= file
->private_data
;
1300 struct gspca_frame
*frame
;
1302 unsigned long addr
, start
, size
;
1304 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1308 start
= vma
->vm_start
;
1309 size
= vma
->vm_end
- vma
->vm_start
;
1310 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1312 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1313 return -ERESTARTSYS
;
1314 if (!gspca_dev
->present
) {
1318 if (gspca_dev
->capt_file
!= file
) {
1324 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1325 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1326 PDEBUG(D_STREAM
, "mmap bad memory type");
1329 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1331 frame
= &gspca_dev
->frame
[i
];
1335 if (frame
== NULL
) {
1336 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1340 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1341 if (i
== 0 && size
== frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1345 if (size
!= frame
->v4l2_buf
.length
) {
1346 PDEBUG(D_STREAM
, "mmap bad size");
1352 * - VM_IO marks the area as being a mmaped region for I/O to a
1353 * device. It also prevents the region from being core dumped.
1355 vma
->vm_flags
|= VM_IO
;
1357 addr
= (unsigned long) frame
->data
;
1359 page
= vmalloc_to_page((void *) addr
);
1360 ret
= vm_insert_page(vma
, start
, page
);
1368 vma
->vm_ops
= &gspca_vm_ops
;
1369 vma
->vm_private_data
= frame
;
1371 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1374 for (i
= 1; i
< gspca_dev
->nframes
; ++i
)
1375 gspca_dev
->frame
[i
].v4l2_buf
.flags
|=
1376 V4L2_BUF_FLAG_MAPPED
;
1381 mutex_unlock(&gspca_dev
->queue_lock
);
1386 * wait for a video frame
1388 * If a frame is ready, its index is returned.
1390 static int frame_wait(struct gspca_dev
*gspca_dev
,
1393 struct gspca_frame
*frame
;
1396 /* if userptr, treat the awaiting URBs */
1397 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
)
1398 isoc_transfer(gspca_dev
);
1400 /* check if a frame is ready */
1401 i
= gspca_dev
->fr_o
;
1402 j
= gspca_dev
->fr_queue
[i
];
1403 frame
= &gspca_dev
->frame
[j
];
1404 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) {
1405 atomic_dec(&gspca_dev
->nevent
);
1408 if (nonblock_ing
) /* no frame yet */
1411 /* wait till a frame is ready */
1413 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1414 atomic_read(&gspca_dev
->nevent
) > 0,
1415 msecs_to_jiffies(3000));
1418 return ret
; /* interrupt */
1419 return -EIO
; /* timeout */
1421 atomic_dec(&gspca_dev
->nevent
);
1422 if (!gspca_dev
->streaming
|| !gspca_dev
->present
)
1424 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
)
1425 isoc_transfer(gspca_dev
);
1426 i
= gspca_dev
->fr_o
;
1427 j
= gspca_dev
->fr_queue
[i
];
1428 frame
= &gspca_dev
->frame
[j
];
1429 if (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1433 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1434 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1439 if (gspca_dev
->sd_desc
->dq_callback
)
1440 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1446 * dequeue a video buffer
1448 * If nonblock_ing is false, block until a buffer is available.
1450 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1451 struct v4l2_buffer
*v4l2_buf
)
1453 struct gspca_dev
*gspca_dev
= priv
;
1454 struct gspca_frame
*frame
;
1457 PDEBUG(D_FRAM
, "dqbuf");
1458 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
1459 || (v4l2_buf
->memory
!= V4L2_MEMORY_MMAP
1460 && v4l2_buf
->memory
!= V4L2_MEMORY_USERPTR
))
1462 if (!gspca_dev
->streaming
)
1464 if (gspca_dev
->capt_file
!= file
) {
1470 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1471 return -ERESTARTSYS
;
1473 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1476 i
= ret
; /* frame index */
1477 frame
= &gspca_dev
->frame
[i
];
1478 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1479 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1480 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1483 mutex_unlock(&gspca_dev
->read_lock
);
1488 * queue a video buffer
1490 * Attempting to queue a buffer that has already been
1491 * queued will return -EINVAL.
1493 static int vidioc_qbuf(struct file
*file
, void *priv
,
1494 struct v4l2_buffer
*v4l2_buf
)
1496 struct gspca_dev
*gspca_dev
= priv
;
1497 struct gspca_frame
*frame
;
1500 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1501 if (v4l2_buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1504 index
= v4l2_buf
->index
;
1505 if ((unsigned) index
>= gspca_dev
->nframes
) {
1507 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1510 frame
= &gspca_dev
->frame
[index
];
1512 if (v4l2_buf
->memory
!= frame
->v4l2_buf
.memory
) {
1513 PDEBUG(D_FRAM
, "qbuf bad memory type");
1516 if (gspca_dev
->capt_file
!= file
)
1519 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1520 return -ERESTARTSYS
;
1522 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1523 PDEBUG(D_FRAM
, "qbuf bad state");
1528 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1529 /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
1531 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1532 frame
->data
= frame
->data_end
=
1533 (__u8
*) v4l2_buf
->m
.userptr
;
1534 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1535 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1538 /* put the buffer in the 'queued' queue */
1539 i
= gspca_dev
->fr_q
;
1540 gspca_dev
->fr_queue
[i
] = index
;
1541 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
1542 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
1547 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1548 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1551 mutex_unlock(&gspca_dev
->queue_lock
);
1556 * allocate the resources for read()
1558 static int read_alloc(struct gspca_dev
*gspca_dev
,
1561 struct v4l2_buffer v4l2_buf
;
1564 PDEBUG(D_STREAM
, "read alloc");
1565 if (gspca_dev
->nframes
== 0) {
1566 struct v4l2_requestbuffers rb
;
1568 memset(&rb
, 0, sizeof rb
);
1569 rb
.count
= gspca_dev
->nbufread
;
1570 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1571 rb
.memory
= V4L2_MEMORY_MMAP
;
1572 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1574 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1577 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1578 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1579 v4l2_buf
.memory
= V4L2_MEMORY_MMAP
;
1580 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1583 gspca_dev
->frame
[i
].v4l2_buf
.flags
|=
1584 V4L2_BUF_FLAG_MAPPED
;
1585 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1587 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1591 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
1594 /* start streaming */
1595 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1597 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1601 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1603 struct gspca_dev
*gspca_dev
= file
->private_data
;
1606 PDEBUG(D_FRAM
, "poll");
1608 poll_wait(file
, &gspca_dev
->wq
, wait
);
1609 if (!gspca_dev
->present
)
1612 /* if not streaming, the user would use read() */
1613 if (!gspca_dev
->streaming
) {
1614 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
) {
1615 ret
= POLLERR
; /* not the 1st time */
1618 ret
= read_alloc(gspca_dev
, file
);
1625 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
1627 if (!gspca_dev
->present
) {
1632 /* if userptr, treat the awaiting URBs */
1633 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
1634 && gspca_dev
->capt_file
== file
)
1635 isoc_transfer(gspca_dev
);
1637 i
= gspca_dev
->fr_o
;
1638 i
= gspca_dev
->fr_queue
[i
];
1639 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
1640 ret
= POLLIN
| POLLRDNORM
; /* something to read */
1644 mutex_unlock(&gspca_dev
->queue_lock
);
1648 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1649 size_t count
, loff_t
*ppos
)
1651 struct gspca_dev
*gspca_dev
= file
->private_data
;
1652 struct gspca_frame
*frame
;
1653 struct v4l2_buffer v4l2_buf
;
1654 struct timeval timestamp
;
1657 PDEBUG(D_FRAM
, "read (%d)", count
);
1658 if (!gspca_dev
->present
)
1660 switch (gspca_dev
->memory
) {
1661 case GSPCA_MEMORY_NO
: /* first time */
1662 ret
= read_alloc(gspca_dev
, file
);
1666 case GSPCA_MEMORY_READ
:
1667 if (gspca_dev
->capt_file
== file
)
1675 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
1679 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1680 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1681 v4l2_buf
.memory
= V4L2_MEMORY_MMAP
;
1682 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1684 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1688 /* if the process slept for more than 1 second,
1689 * get anewer frame */
1690 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1692 break; /* avoid infinite loop */
1693 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1695 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1697 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1702 /* copy the frame */
1703 if (count
< frame
->v4l2_buf
.bytesused
) {
1704 PDEBUG(D_STREAM
, "read bad count: %d < %d",
1705 count
, frame
->v4l2_buf
.bytesused
);
1706 /*fixme: special errno?*/
1710 count
= frame
->v4l2_buf
.bytesused
;
1711 ret
= copy_to_user(data
, frame
->data
, count
);
1713 PDEBUG(D_ERR
|D_STREAM
,
1714 "read cp to user lack %d / %d", ret
, count
);
1720 /* in each case, requeue the buffer */
1721 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1727 static void dev_release(struct video_device
*vfd
)
1732 static struct file_operations dev_fops
= {
1733 .owner
= THIS_MODULE
,
1735 .release
= dev_close
,
1738 .ioctl
= video_ioctl2
,
1739 #ifdef CONFIG_COMPAT
1740 .compat_ioctl
= v4l_compat_ioctl32
,
1742 .llseek
= no_llseek
,
1746 static struct video_device gspca_template
= {
1747 .name
= "gspca main driver",
1748 .type
= VID_TYPE_CAPTURE
,
1750 .release
= dev_release
, /* mandatory */
1752 .vidioc_querycap
= vidioc_querycap
,
1753 .vidioc_dqbuf
= vidioc_dqbuf
,
1754 .vidioc_qbuf
= vidioc_qbuf
,
1755 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1756 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1757 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1758 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1759 .vidioc_streamon
= vidioc_streamon
,
1760 .vidioc_queryctrl
= vidioc_queryctrl
,
1761 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1762 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1763 .vidioc_querymenu
= vidioc_querymenu
,
1764 .vidioc_enum_input
= vidioc_enum_input
,
1765 .vidioc_g_input
= vidioc_g_input
,
1766 .vidioc_s_input
= vidioc_s_input
,
1767 .vidioc_reqbufs
= vidioc_reqbufs
,
1768 .vidioc_querybuf
= vidioc_querybuf
,
1769 .vidioc_streamoff
= vidioc_streamoff
,
1770 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1771 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1772 .vidioc_g_parm
= vidioc_g_parm
,
1773 .vidioc_s_parm
= vidioc_s_parm
,
1774 .vidioc_s_std
= vidioc_s_std
,
1775 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1776 .vidiocgmbuf
= vidiocgmbuf
,
1781 * probe and create a new gspca device
1783 * This function must be called by the sub-driver when it is
1784 * called for probing a new device.
1786 int gspca_dev_probe(struct usb_interface
*intf
,
1787 const struct usb_device_id
*id
,
1788 const struct sd_desc
*sd_desc
,
1790 struct module
*module
)
1792 struct usb_interface_descriptor
*interface
;
1793 struct gspca_dev
*gspca_dev
;
1794 struct usb_device
*dev
= interface_to_usbdev(intf
);
1797 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
1799 /* we don't handle multi-config cameras */
1800 if (dev
->descriptor
.bNumConfigurations
!= 1)
1802 interface
= &intf
->cur_altsetting
->desc
;
1803 if (interface
->bInterfaceNumber
> 0)
1806 /* create the device */
1807 if (dev_size
< sizeof *gspca_dev
)
1808 dev_size
= sizeof *gspca_dev
;
1809 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1810 if (gspca_dev
== NULL
) {
1811 err("couldn't kzalloc gspca struct");
1814 gspca_dev
->dev
= dev
;
1815 gspca_dev
->iface
= interface
->bInterfaceNumber
;
1816 gspca_dev
->nbalt
= intf
->num_altsetting
;
1817 gspca_dev
->sd_desc
= sd_desc
;
1818 /* gspca_dev->users = 0; (done by kzalloc) */
1819 gspca_dev
->nbufread
= 2;
1821 /* configure the subdriver */
1822 ret
= gspca_dev
->sd_desc
->config(gspca_dev
, id
);
1825 ret
= gspca_set_alt0(gspca_dev
);
1828 gspca_set_default_mode(gspca_dev
);
1830 mutex_init(&gspca_dev
->usb_lock
);
1831 mutex_init(&gspca_dev
->read_lock
);
1832 mutex_init(&gspca_dev
->queue_lock
);
1833 init_waitqueue_head(&gspca_dev
->wq
);
1835 /* init video stuff */
1836 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
1837 gspca_dev
->vdev
.dev
= &dev
->dev
;
1838 memcpy(&gspca_dev
->fops
, &dev_fops
, sizeof gspca_dev
->fops
);
1839 gspca_dev
->vdev
.fops
= &gspca_dev
->fops
;
1840 gspca_dev
->fops
.owner
= module
; /* module protection */
1841 ret
= video_register_device(&gspca_dev
->vdev
,
1845 err("video_register_device err %d", ret
);
1849 gspca_dev
->present
= 1;
1850 usb_set_intfdata(intf
, gspca_dev
);
1851 PDEBUG(D_PROBE
, "probe ok");
1857 EXPORT_SYMBOL(gspca_dev_probe
);
1862 * This function must be called by the sub-driver
1863 * when the device disconnects, after the specific resources are freed.
1865 void gspca_disconnect(struct usb_interface
*intf
)
1867 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1871 gspca_dev
->present
= 0;
1872 mutex_lock(&gspca_dev
->queue_lock
);
1873 mutex_lock(&gspca_dev
->usb_lock
);
1874 gspca_dev
->streaming
= 0;
1875 destroy_urbs(gspca_dev
);
1876 mutex_unlock(&gspca_dev
->usb_lock
);
1877 mutex_unlock(&gspca_dev
->queue_lock
);
1878 while (gspca_dev
->users
!= 0) { /* wait until fully closed */
1879 atomic_inc(&gspca_dev
->nevent
);
1880 wake_up_interruptible(&gspca_dev
->wq
); /* wake processes */
1883 /* We don't want people trying to open up the device */
1884 video_unregister_device(&gspca_dev
->vdev
);
1885 /* Free the memory */
1887 PDEBUG(D_PROBE
, "disconnect complete");
1889 EXPORT_SYMBOL(gspca_disconnect
);
1891 /* -- module insert / remove -- */
1892 static int __init
gspca_init(void)
1894 info("main v%s registered", version
);
1897 static void __exit
gspca_exit(void)
1899 info("main deregistered");
1902 module_init(gspca_init
);
1903 module_exit(gspca_exit
);
1905 #ifdef CONFIG_VIDEO_ADV_DEBUG
1906 module_param_named(debug
, gspca_debug
, int, 0644);
1907 MODULE_PARM_DESC(debug
,
1908 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
1909 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"