2 * Main USB camera driver
4 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define MODULE_NAME "gspca"
28 #include <linux/init.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
38 #include <linux/uaccess.h>
39 #include <linux/ktime.h>
40 #include <media/v4l2-ioctl.h>
44 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
45 #include <linux/input.h>
46 #include <linux/usb/input.h>
50 #define DEF_NURBS 3 /* default number of URBs */
51 #if DEF_NURBS > MAX_NURBS
52 #error "DEF_NURBS too big"
55 #define DRIVER_VERSION_NUMBER "2.13.0"
57 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
58 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(DRIVER_VERSION_NUMBER
);
63 int gspca_debug
= D_ERR
| D_PROBE
;
64 EXPORT_SYMBOL(gspca_debug
);
66 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
68 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
69 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
73 (pixfmt
>> 16) & 0xff,
77 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
84 #define PDEBUG_MODE(txt, pixfmt, w, h)
87 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
88 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
89 #define GSPCA_MEMORY_READ 7
91 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
96 static void gspca_vm_open(struct vm_area_struct
*vma
)
98 struct gspca_frame
*frame
= vma
->vm_private_data
;
100 frame
->vma_use_count
++;
101 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
104 static void gspca_vm_close(struct vm_area_struct
*vma
)
106 struct gspca_frame
*frame
= vma
->vm_private_data
;
108 if (--frame
->vma_use_count
<= 0)
109 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
112 static const struct vm_operations_struct gspca_vm_ops
= {
113 .open
= gspca_vm_open
,
114 .close
= gspca_vm_close
,
118 * Input and interrupt endpoint handling functions
120 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
121 static void int_irq(struct urb
*urb
)
123 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
129 if (gspca_dev
->sd_desc
->int_pkt_scan(gspca_dev
,
130 urb
->transfer_buffer
, urb
->actual_length
) < 0) {
131 PDEBUG(D_ERR
, "Unknown packet received");
139 /* Stop is requested either by software or hardware is gone,
140 * keep the ret value non-zero and don't resubmit later.
145 PDEBUG(D_ERR
, "URB error %i, resubmitting", urb
->status
);
151 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
153 pr_err("Resubmit URB failed with error %i\n", ret
);
157 static int gspca_input_connect(struct gspca_dev
*dev
)
159 struct input_dev
*input_dev
;
162 dev
->input_dev
= NULL
;
163 if (dev
->sd_desc
->int_pkt_scan
|| dev
->sd_desc
->other_input
) {
164 input_dev
= input_allocate_device();
168 usb_make_path(dev
->dev
, dev
->phys
, sizeof(dev
->phys
));
169 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
171 input_dev
->name
= dev
->sd_desc
->name
;
172 input_dev
->phys
= dev
->phys
;
174 usb_to_input_id(dev
->dev
, &input_dev
->id
);
176 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
177 input_dev
->keybit
[BIT_WORD(KEY_CAMERA
)] = BIT_MASK(KEY_CAMERA
);
178 input_dev
->dev
.parent
= &dev
->dev
->dev
;
180 err
= input_register_device(input_dev
);
182 pr_err("Input device registration failed with error %i\n",
184 input_dev
->dev
.parent
= NULL
;
185 input_free_device(input_dev
);
187 dev
->input_dev
= input_dev
;
194 static int alloc_and_submit_int_urb(struct gspca_dev
*gspca_dev
,
195 struct usb_endpoint_descriptor
*ep
)
197 unsigned int buffer_len
;
200 struct usb_device
*dev
;
204 buffer_len
= le16_to_cpu(ep
->wMaxPacketSize
);
205 interval
= ep
->bInterval
;
206 PDEBUG(D_CONF
, "found int in endpoint: 0x%x, "
207 "buffer_len=%u, interval=%u",
208 ep
->bEndpointAddress
, buffer_len
, interval
);
210 dev
= gspca_dev
->dev
;
212 urb
= usb_alloc_urb(0, GFP_KERNEL
);
218 buffer
= usb_alloc_coherent(dev
, buffer_len
,
219 GFP_KERNEL
, &urb
->transfer_dma
);
224 usb_fill_int_urb(urb
, dev
,
225 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
227 int_irq
, (void *)gspca_dev
, interval
);
228 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
229 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
231 PDEBUG(D_ERR
, "submit int URB failed with error %i", ret
);
234 gspca_dev
->int_urb
= urb
;
238 usb_free_coherent(dev
,
239 urb
->transfer_buffer_length
,
240 urb
->transfer_buffer
,
248 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
250 struct usb_interface
*intf
;
251 struct usb_host_interface
*intf_desc
;
252 struct usb_endpoint_descriptor
*ep
;
255 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
256 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
257 intf_desc
= intf
->cur_altsetting
;
258 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
259 ep
= &intf_desc
->endpoint
[i
].desc
;
260 if (usb_endpoint_dir_in(ep
) &&
261 usb_endpoint_xfer_int(ep
)) {
263 alloc_and_submit_int_urb(gspca_dev
, ep
);
270 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
274 urb
= gspca_dev
->int_urb
;
276 gspca_dev
->int_urb
= NULL
;
278 usb_free_coherent(gspca_dev
->dev
,
279 urb
->transfer_buffer_length
,
280 urb
->transfer_buffer
,
286 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
290 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
294 static inline int gspca_input_connect(struct gspca_dev
*dev
)
301 * fill a video frame from an URB and resubmit
303 static void fill_frame(struct gspca_dev
*gspca_dev
,
306 u8
*data
; /* address of data in the iso message */
310 if (urb
->status
!= 0) {
311 if (urb
->status
== -ESHUTDOWN
)
312 return; /* disconnection */
314 if (gspca_dev
->frozen
)
317 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
321 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
322 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
323 len
= urb
->iso_frame_desc
[i
].actual_length
;
325 /* check the packet status and length */
326 st
= urb
->iso_frame_desc
[i
].status
;
328 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
330 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
334 if (gspca_dev
->empty_packet
== 0)
335 gspca_dev
->empty_packet
= 1;
339 /* let the packet be analyzed by the subdriver */
340 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
341 i
, urb
->iso_frame_desc
[i
].offset
, len
);
342 data
= (u8
*) urb
->transfer_buffer
343 + urb
->iso_frame_desc
[i
].offset
;
344 pkt_scan(gspca_dev
, data
, len
);
348 /* resubmit the URB */
349 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
351 pr_err("usb_submit_urb() ret %d\n", st
);
355 * ISOC message interrupt from the USB device
357 * Analyse each packet and call the subdriver for copy to the frame buffer.
359 static void isoc_irq(struct urb
*urb
)
361 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
363 PDEBUG(D_PACK
, "isoc irq");
364 if (!gspca_dev
->streaming
)
366 fill_frame(gspca_dev
, urb
);
370 * bulk message interrupt from the USB device
372 static void bulk_irq(struct urb
*urb
)
374 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
377 PDEBUG(D_PACK
, "bulk irq");
378 if (!gspca_dev
->streaming
)
380 switch (urb
->status
) {
384 return; /* disconnection */
387 if (gspca_dev
->frozen
)
390 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
395 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
396 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
397 urb
->transfer_buffer
,
401 /* resubmit the URB */
402 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
403 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
405 pr_err("usb_submit_urb() ret %d\n", st
);
410 * add data to the current frame
412 * This function is called by the subdrivers at interrupt level.
414 * To build a frame, these ones must add
416 * - 0 or many INTER_PACKETs
418 * DISCARD_PACKET invalidates the whole frame.
420 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
421 enum gspca_packet_type packet_type
,
425 struct gspca_frame
*frame
;
428 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
430 if (packet_type
== FIRST_PACKET
) {
431 i
= atomic_read(&gspca_dev
->fr_i
);
433 /* if there are no queued buffer, discard the whole frame */
434 if (i
== atomic_read(&gspca_dev
->fr_q
)) {
435 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
436 gspca_dev
->sequence
++;
439 j
= gspca_dev
->fr_queue
[i
];
440 frame
= &gspca_dev
->frame
[j
];
441 frame
->v4l2_buf
.timestamp
= ktime_to_timeval(ktime_get());
442 frame
->v4l2_buf
.sequence
= gspca_dev
->sequence
++;
443 gspca_dev
->image
= frame
->data
;
444 gspca_dev
->image_len
= 0;
446 switch (gspca_dev
->last_packet_type
) {
448 if (packet_type
== LAST_PACKET
) {
449 gspca_dev
->last_packet_type
= packet_type
;
450 gspca_dev
->image
= NULL
;
451 gspca_dev
->image_len
= 0;
459 /* append the packet to the frame buffer */
461 if (gspca_dev
->image_len
+ len
> gspca_dev
->frsz
) {
462 PDEBUG(D_ERR
|D_PACK
, "frame overflow %d > %d",
463 gspca_dev
->image_len
+ len
,
465 packet_type
= DISCARD_PACKET
;
467 /* !! image is NULL only when last pkt is LAST or DISCARD
468 if (gspca_dev->image == NULL) {
469 pr_err("gspca_frame_add() image == NULL\n");
473 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
475 gspca_dev
->image_len
+= len
;
478 gspca_dev
->last_packet_type
= packet_type
;
480 /* if last packet, invalidate packet concatenation until
481 * next first packet, wake up the application and advance
483 if (packet_type
== LAST_PACKET
) {
484 i
= atomic_read(&gspca_dev
->fr_i
);
485 j
= gspca_dev
->fr_queue
[i
];
486 frame
= &gspca_dev
->frame
[j
];
487 frame
->v4l2_buf
.bytesused
= gspca_dev
->image_len
;
488 frame
->v4l2_buf
.flags
= (frame
->v4l2_buf
.flags
489 | V4L2_BUF_FLAG_DONE
)
490 & ~V4L2_BUF_FLAG_QUEUED
;
491 i
= (i
+ 1) % GSPCA_MAX_FRAMES
;
492 atomic_set(&gspca_dev
->fr_i
, i
);
493 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
494 PDEBUG(D_FRAM
, "frame complete len:%d",
495 frame
->v4l2_buf
.bytesused
);
496 gspca_dev
->image
= NULL
;
497 gspca_dev
->image_len
= 0;
500 EXPORT_SYMBOL(gspca_frame_add
);
502 static int gspca_is_compressed(__u32 format
)
505 case V4L2_PIX_FMT_MJPEG
:
506 case V4L2_PIX_FMT_JPEG
:
507 case V4L2_PIX_FMT_SPCA561
:
508 case V4L2_PIX_FMT_PAC207
:
509 case V4L2_PIX_FMT_MR97310A
:
515 static int frame_alloc(struct gspca_dev
*gspca_dev
, struct file
*file
,
516 enum v4l2_memory memory
, unsigned int count
)
518 struct gspca_frame
*frame
;
522 i
= gspca_dev
->curr_mode
;
523 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
524 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
525 frsz
= PAGE_ALIGN(frsz
);
526 if (count
>= GSPCA_MAX_FRAMES
)
527 count
= GSPCA_MAX_FRAMES
- 1;
528 gspca_dev
->frbuf
= vmalloc_32(frsz
* count
);
529 if (!gspca_dev
->frbuf
) {
530 pr_err("frame alloc failed\n");
533 gspca_dev
->capt_file
= file
;
534 gspca_dev
->memory
= memory
;
535 gspca_dev
->frsz
= frsz
;
536 gspca_dev
->nframes
= count
;
537 for (i
= 0; i
< count
; i
++) {
538 frame
= &gspca_dev
->frame
[i
];
539 frame
->v4l2_buf
.index
= i
;
540 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
541 frame
->v4l2_buf
.flags
= 0;
542 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
543 frame
->v4l2_buf
.length
= frsz
;
544 frame
->v4l2_buf
.memory
= memory
;
545 frame
->v4l2_buf
.sequence
= 0;
546 frame
->data
= gspca_dev
->frbuf
+ i
* frsz
;
547 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
549 atomic_set(&gspca_dev
->fr_q
, 0);
550 atomic_set(&gspca_dev
->fr_i
, 0);
555 static void frame_free(struct gspca_dev
*gspca_dev
)
559 PDEBUG(D_STREAM
, "frame free");
560 if (gspca_dev
->frbuf
!= NULL
) {
561 vfree(gspca_dev
->frbuf
);
562 gspca_dev
->frbuf
= NULL
;
563 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
564 gspca_dev
->frame
[i
].data
= NULL
;
566 gspca_dev
->nframes
= 0;
568 gspca_dev
->capt_file
= NULL
;
569 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
572 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
577 PDEBUG(D_STREAM
, "kill transfer");
578 for (i
= 0; i
< MAX_NURBS
; i
++) {
579 urb
= gspca_dev
->urb
[i
];
583 gspca_dev
->urb
[i
] = NULL
;
585 if (urb
->transfer_buffer
!= NULL
)
586 usb_free_coherent(gspca_dev
->dev
,
587 urb
->transfer_buffer_length
,
588 urb
->transfer_buffer
,
594 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
598 if (gspca_dev
->alt
== 0)
600 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
602 pr_err("set alt 0 err %d\n", ret
);
606 /* Note: both the queue and the usb locks should be held when calling this */
607 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
609 gspca_dev
->streaming
= 0;
610 if (gspca_dev
->present
) {
611 if (gspca_dev
->sd_desc
->stopN
)
612 gspca_dev
->sd_desc
->stopN(gspca_dev
);
613 destroy_urbs(gspca_dev
);
614 gspca_input_destroy_urb(gspca_dev
);
615 gspca_set_alt0(gspca_dev
);
616 gspca_input_create_urb(gspca_dev
);
619 /* always call stop0 to free the subdriver's resources */
620 if (gspca_dev
->sd_desc
->stop0
)
621 gspca_dev
->sd_desc
->stop0(gspca_dev
);
622 PDEBUG(D_STREAM
, "stream off OK");
626 * look for an input transfer endpoint in an alternate setting
628 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
631 struct usb_host_endpoint
*ep
;
634 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
635 ep
= &alt
->endpoint
[i
];
636 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
638 && ep
->desc
.wMaxPacketSize
!= 0
639 && usb_endpoint_dir_in(&ep
->desc
))
646 * look for an input (isoc or bulk) endpoint
648 * The endpoint is defined by the subdriver.
649 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
650 * This routine may be called many times when the bandwidth is too small
651 * (the bandwidth is checked on urb submit).
653 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
655 struct usb_interface
*intf
;
656 struct usb_host_endpoint
*ep
;
659 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
661 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
662 : USB_ENDPOINT_XFER_ISOC
;
663 i
= gspca_dev
->alt
; /* previous alt setting */
664 if (gspca_dev
->cam
.reverse_alts
) {
665 while (++i
< gspca_dev
->nbalt
) {
666 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
672 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
678 pr_err("no transfer endpoint found\n");
681 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
682 i
, ep
->desc
.bEndpointAddress
);
683 gspca_dev
->alt
= i
; /* memorize the current alt setting */
684 if (gspca_dev
->nbalt
> 1) {
685 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
687 pr_err("set alt %d err %d\n", i
, ret
);
695 * create the URBs for image transfer
697 static int create_urbs(struct gspca_dev
*gspca_dev
,
698 struct usb_host_endpoint
*ep
)
701 int n
, nurbs
, i
, psize
, npkt
, bsize
;
703 /* calculate the packet size and the number of packets */
704 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
706 if (!gspca_dev
->cam
.bulk
) { /* isoc */
708 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
709 if (gspca_dev
->pkt_size
== 0)
710 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
712 psize
= gspca_dev
->pkt_size
;
713 npkt
= gspca_dev
->cam
.npkt
;
715 npkt
= 32; /* default value */
716 bsize
= psize
* npkt
;
718 "isoc %d pkts size %d = bsize:%d",
723 bsize
= gspca_dev
->cam
.bulk_size
;
726 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
727 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
728 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
733 for (n
= 0; n
< nurbs
; n
++) {
734 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
736 pr_err("usb_alloc_urb failed\n");
739 gspca_dev
->urb
[n
] = urb
;
740 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
745 if (urb
->transfer_buffer
== NULL
) {
746 pr_err("usb_alloc_coherent failed\n");
749 urb
->dev
= gspca_dev
->dev
;
750 urb
->context
= gspca_dev
;
751 urb
->transfer_buffer_length
= bsize
;
752 if (npkt
!= 0) { /* ISOC */
753 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
754 ep
->desc
.bEndpointAddress
);
755 urb
->transfer_flags
= URB_ISO_ASAP
756 | URB_NO_TRANSFER_DMA_MAP
;
757 urb
->interval
= ep
->desc
.bInterval
;
758 urb
->complete
= isoc_irq
;
759 urb
->number_of_packets
= npkt
;
760 for (i
= 0; i
< npkt
; i
++) {
761 urb
->iso_frame_desc
[i
].length
= psize
;
762 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
765 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
766 ep
->desc
.bEndpointAddress
);
767 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
768 urb
->complete
= bulk_irq
;
775 * start the USB transfer
777 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
779 struct usb_host_endpoint
*ep
;
783 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
786 if (!gspca_dev
->present
) {
791 /* reset the streaming variables */
792 gspca_dev
->image
= NULL
;
793 gspca_dev
->image_len
= 0;
794 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
795 gspca_dev
->sequence
= 0;
797 gspca_dev
->usb_err
= 0;
799 /* set the higher alternate setting and
800 * loop until urb submit succeeds */
801 if (gspca_dev
->cam
.reverse_alts
)
804 gspca_dev
->alt
= gspca_dev
->nbalt
;
806 if (gspca_dev
->sd_desc
->isoc_init
) {
807 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
812 gspca_input_destroy_urb(gspca_dev
);
813 ep
= get_ep(gspca_dev
);
819 if (!gspca_dev
->cam
.no_urb_create
) {
820 PDEBUG(D_STREAM
, "init transfer alt %d",
822 ret
= create_urbs(gspca_dev
, ep
);
824 destroy_urbs(gspca_dev
);
829 /* clear the bulk endpoint */
830 if (gspca_dev
->cam
.bulk
)
831 usb_clear_halt(gspca_dev
->dev
,
832 gspca_dev
->urb
[0]->pipe
);
835 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
837 destroy_urbs(gspca_dev
);
840 gspca_dev
->streaming
= 1;
842 /* some bulk transfers are started by the subdriver */
843 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
846 /* submit the URBs */
847 for (n
= 0; n
< MAX_NURBS
; n
++) {
848 urb
= gspca_dev
->urb
[n
];
851 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
857 gspca_stream_off(gspca_dev
);
858 if (ret
!= -ENOSPC
) {
859 pr_err("usb_submit_urb alt %d err %d\n",
860 gspca_dev
->alt
, ret
);
864 /* the bandwidth is not wide enough
865 * negotiate or try a lower alternate setting */
866 PDEBUG(D_ERR
|D_STREAM
,
867 "bandwidth not wide enough - trying again");
868 msleep(20); /* wait for kill complete */
869 if (gspca_dev
->sd_desc
->isoc_nego
) {
870 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
874 ep
= get_ep(gspca_dev
);
882 gspca_input_create_urb(gspca_dev
);
884 mutex_unlock(&gspca_dev
->usb_lock
);
888 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
890 struct gspca_ctrl
*ctrl
;
893 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
894 gspca_dev
->curr_mode
= i
;
895 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
896 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
897 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
899 /* set the current control values to their default values
900 * which may have changed in sd_init() */
901 ctrl
= gspca_dev
->cam
.ctrls
;
904 i
< gspca_dev
->sd_desc
->nctrls
;
906 ctrl
->val
= ctrl
->def
;
910 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
911 int width
, int height
)
915 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
916 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
917 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
924 * search a mode with the right pixel format
926 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
932 modeU
= modeD
= mode
;
933 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
935 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
939 if (++modeU
< gspca_dev
->cam
.nmodes
) {
940 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
948 #ifdef CONFIG_VIDEO_ADV_DEBUG
949 static int vidioc_g_register(struct file
*file
, void *priv
,
950 struct v4l2_dbg_register
*reg
)
953 struct gspca_dev
*gspca_dev
= priv
;
955 if (!gspca_dev
->sd_desc
->get_chip_ident
)
958 if (!gspca_dev
->sd_desc
->get_register
)
961 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
963 gspca_dev
->usb_err
= 0;
964 if (gspca_dev
->present
)
965 ret
= gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
968 mutex_unlock(&gspca_dev
->usb_lock
);
973 static int vidioc_s_register(struct file
*file
, void *priv
,
974 struct v4l2_dbg_register
*reg
)
977 struct gspca_dev
*gspca_dev
= priv
;
979 if (!gspca_dev
->sd_desc
->get_chip_ident
)
982 if (!gspca_dev
->sd_desc
->set_register
)
985 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
987 gspca_dev
->usb_err
= 0;
988 if (gspca_dev
->present
)
989 ret
= gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
992 mutex_unlock(&gspca_dev
->usb_lock
);
998 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
999 struct v4l2_dbg_chip_ident
*chip
)
1002 struct gspca_dev
*gspca_dev
= priv
;
1004 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1007 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1008 return -ERESTARTSYS
;
1009 gspca_dev
->usb_err
= 0;
1010 if (gspca_dev
->present
)
1011 ret
= gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
1014 mutex_unlock(&gspca_dev
->usb_lock
);
1019 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1020 struct v4l2_fmtdesc
*fmtdesc
)
1022 struct gspca_dev
*gspca_dev
= priv
;
1026 /* give an index to each format */
1029 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1030 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1033 if (fmt_tb
[j
] == fmt_tb
[index
])
1038 if (fmtdesc
->index
== index
)
1039 break; /* new format */
1041 if (index
>= ARRAY_SIZE(fmt_tb
))
1046 return -EINVAL
; /* no more format */
1048 fmtdesc
->pixelformat
= fmt_tb
[index
];
1049 if (gspca_is_compressed(fmt_tb
[index
]))
1050 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1051 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1052 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1053 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1054 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1055 fmtdesc
->description
[4] = '\0';
1059 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1060 struct v4l2_format
*fmt
)
1062 struct gspca_dev
*gspca_dev
= priv
;
1065 mode
= gspca_dev
->curr_mode
;
1066 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1067 sizeof fmt
->fmt
.pix
);
1071 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1072 struct v4l2_format
*fmt
)
1074 int w
, h
, mode
, mode2
;
1076 w
= fmt
->fmt
.pix
.width
;
1077 h
= fmt
->fmt
.pix
.height
;
1080 if (gspca_debug
& D_CONF
)
1081 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
1083 /* search the closest mode for width and height */
1084 mode
= wxh_to_mode(gspca_dev
, w
, h
);
1086 /* OK if right palette */
1087 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1088 != fmt
->fmt
.pix
.pixelformat
) {
1090 /* else, search the closest mode with the same pixel format */
1091 mode2
= gspca_get_mode(gspca_dev
, mode
,
1092 fmt
->fmt
.pix
.pixelformat
);
1096 ; * no chance, return this mode */
1098 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1099 sizeof fmt
->fmt
.pix
);
1100 return mode
; /* used when s_fmt */
1103 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1105 struct v4l2_format
*fmt
)
1107 struct gspca_dev
*gspca_dev
= priv
;
1110 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1116 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1117 struct v4l2_format
*fmt
)
1119 struct gspca_dev
*gspca_dev
= priv
;
1122 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1123 return -ERESTARTSYS
;
1125 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1129 if (gspca_dev
->nframes
!= 0
1130 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1135 if (ret
== gspca_dev
->curr_mode
) {
1137 goto out
; /* same mode */
1140 if (gspca_dev
->streaming
) {
1144 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
1145 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
1146 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
1147 gspca_dev
->curr_mode
= ret
;
1151 mutex_unlock(&gspca_dev
->queue_lock
);
1155 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1156 struct v4l2_frmsizeenum
*fsize
)
1158 struct gspca_dev
*gspca_dev
= priv
;
1162 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1163 if (fsize
->pixel_format
!=
1164 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1167 if (fsize
->index
== index
) {
1168 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1169 fsize
->discrete
.width
=
1170 gspca_dev
->cam
.cam_mode
[i
].width
;
1171 fsize
->discrete
.height
=
1172 gspca_dev
->cam
.cam_mode
[i
].height
;
1181 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1182 struct v4l2_frmivalenum
*fival
)
1184 struct gspca_dev
*gspca_dev
= priv
;
1185 int mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1188 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1189 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1192 if (fival
->pixel_format
!=
1193 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1196 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1197 if (fival
->index
== i
) {
1198 fival
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1199 fival
->discrete
.numerator
= 1;
1200 fival
->discrete
.denominator
=
1201 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1209 static void gspca_release(struct video_device
*vfd
)
1211 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
1213 PDEBUG(D_PROBE
, "%s released",
1214 video_device_node_name(&gspca_dev
->vdev
));
1216 kfree(gspca_dev
->usb_buf
);
1220 static int dev_open(struct file
*file
)
1222 struct gspca_dev
*gspca_dev
;
1224 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1225 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
1226 if (!gspca_dev
->present
)
1229 /* protect the subdriver against rmmod */
1230 if (!try_module_get(gspca_dev
->module
))
1233 file
->private_data
= gspca_dev
;
1235 /* activate the v4l2 debug */
1236 if (gspca_debug
& D_V4L2
)
1237 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1238 | V4L2_DEBUG_IOCTL_ARG
;
1240 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1241 | V4L2_DEBUG_IOCTL_ARG
);
1246 static int dev_close(struct file
*file
)
1248 struct gspca_dev
*gspca_dev
= file
->private_data
;
1250 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1251 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1252 return -ERESTARTSYS
;
1254 /* if the file did the capture, free the streaming resources */
1255 if (gspca_dev
->capt_file
== file
) {
1256 if (gspca_dev
->streaming
) {
1257 mutex_lock(&gspca_dev
->usb_lock
);
1258 gspca_dev
->usb_err
= 0;
1259 gspca_stream_off(gspca_dev
);
1260 mutex_unlock(&gspca_dev
->usb_lock
);
1262 frame_free(gspca_dev
);
1264 file
->private_data
= NULL
;
1265 module_put(gspca_dev
->module
);
1266 mutex_unlock(&gspca_dev
->queue_lock
);
1268 PDEBUG(D_STREAM
, "close done");
1273 static int vidioc_querycap(struct file
*file
, void *priv
,
1274 struct v4l2_capability
*cap
)
1276 struct gspca_dev
*gspca_dev
= priv
;
1279 /* protect the access to the usb device */
1280 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1281 return -ERESTARTSYS
;
1282 if (!gspca_dev
->present
) {
1286 strlcpy((char *) cap
->driver
, gspca_dev
->sd_desc
->name
,
1287 sizeof cap
->driver
);
1288 if (gspca_dev
->dev
->product
!= NULL
) {
1289 strlcpy((char *) cap
->card
, gspca_dev
->dev
->product
,
1292 snprintf((char *) cap
->card
, sizeof cap
->card
,
1293 "USB Camera (%04x:%04x)",
1294 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1295 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1297 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1298 sizeof(cap
->bus_info
));
1299 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
1300 | V4L2_CAP_STREAMING
1301 | V4L2_CAP_READWRITE
;
1304 mutex_unlock(&gspca_dev
->usb_lock
);
1308 static int get_ctrl(struct gspca_dev
*gspca_dev
,
1311 const struct ctrl
*ctrls
;
1314 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1315 i
< gspca_dev
->sd_desc
->nctrls
;
1317 if (gspca_dev
->ctrl_dis
& (1 << i
))
1319 if (id
== ctrls
->qctrl
.id
)
1325 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1326 struct v4l2_queryctrl
*q_ctrl
)
1328 struct gspca_dev
*gspca_dev
= priv
;
1329 const struct ctrl
*ctrls
;
1330 struct gspca_ctrl
*gspca_ctrl
;
1335 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1336 id
&= V4L2_CTRL_ID_MASK
;
1339 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1340 if (gspca_dev
->ctrl_dis
& (1 << i
))
1342 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1345 && gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1346 > gspca_dev
->sd_desc
->ctrls
[idx
].qctrl
.id
)
1351 idx
= get_ctrl(gspca_dev
, id
);
1355 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1356 memcpy(q_ctrl
, &ctrls
->qctrl
, sizeof *q_ctrl
);
1357 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1358 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1359 q_ctrl
->default_value
= gspca_ctrl
->def
;
1360 q_ctrl
->minimum
= gspca_ctrl
->min
;
1361 q_ctrl
->maximum
= gspca_ctrl
->max
;
1363 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1364 q_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1368 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1369 struct v4l2_control
*ctrl
)
1371 struct gspca_dev
*gspca_dev
= priv
;
1372 const struct ctrl
*ctrls
;
1373 struct gspca_ctrl
*gspca_ctrl
;
1376 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1379 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1381 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1382 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1383 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1384 if (ctrl
->value
< gspca_ctrl
->min
1385 || ctrl
->value
> gspca_ctrl
->max
)
1389 if (ctrl
->value
< ctrls
->qctrl
.minimum
1390 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1393 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1394 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1395 return -ERESTARTSYS
;
1396 if (!gspca_dev
->present
) {
1400 gspca_dev
->usb_err
= 0;
1401 if (ctrls
->set
!= NULL
) {
1402 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1405 if (gspca_ctrl
!= NULL
) {
1406 gspca_ctrl
->val
= ctrl
->value
;
1407 if (ctrls
->set_control
!= NULL
1408 && gspca_dev
->streaming
)
1409 ctrls
->set_control(gspca_dev
);
1411 ret
= gspca_dev
->usb_err
;
1413 mutex_unlock(&gspca_dev
->usb_lock
);
1417 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1418 struct v4l2_control
*ctrl
)
1420 struct gspca_dev
*gspca_dev
= priv
;
1421 const struct ctrl
*ctrls
;
1424 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1427 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1429 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1430 return -ERESTARTSYS
;
1431 if (!gspca_dev
->present
) {
1435 gspca_dev
->usb_err
= 0;
1436 if (ctrls
->get
!= NULL
) {
1437 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1440 if (gspca_dev
->cam
.ctrls
!= NULL
)
1441 ctrl
->value
= gspca_dev
->cam
.ctrls
[idx
].val
;
1444 mutex_unlock(&gspca_dev
->usb_lock
);
1448 static int vidioc_querymenu(struct file
*file
, void *priv
,
1449 struct v4l2_querymenu
*qmenu
)
1451 struct gspca_dev
*gspca_dev
= priv
;
1453 if (!gspca_dev
->sd_desc
->querymenu
)
1455 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1458 static int vidioc_enum_input(struct file
*file
, void *priv
,
1459 struct v4l2_input
*input
)
1461 struct gspca_dev
*gspca_dev
= priv
;
1463 if (input
->index
!= 0)
1465 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1466 input
->status
= gspca_dev
->cam
.input_flags
;
1467 strlcpy(input
->name
, gspca_dev
->sd_desc
->name
,
1468 sizeof input
->name
);
1472 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1478 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1485 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1486 struct v4l2_requestbuffers
*rb
)
1488 struct gspca_dev
*gspca_dev
= priv
;
1489 int i
, ret
= 0, streaming
;
1491 i
= rb
->memory
; /* (avoid compilation warning) */
1493 case GSPCA_MEMORY_READ
: /* (internal call) */
1494 case V4L2_MEMORY_MMAP
:
1495 case V4L2_MEMORY_USERPTR
:
1500 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1501 return -ERESTARTSYS
;
1503 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1504 && gspca_dev
->memory
!= GSPCA_MEMORY_READ
1505 && gspca_dev
->memory
!= rb
->memory
) {
1510 /* only one file may do the capture */
1511 if (gspca_dev
->capt_file
!= NULL
1512 && gspca_dev
->capt_file
!= file
) {
1517 /* if allocated, the buffers must not be mapped */
1518 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1519 if (gspca_dev
->frame
[i
].vma_use_count
) {
1525 /* stop streaming */
1526 streaming
= gspca_dev
->streaming
;
1528 mutex_lock(&gspca_dev
->usb_lock
);
1529 gspca_dev
->usb_err
= 0;
1530 gspca_stream_off(gspca_dev
);
1531 mutex_unlock(&gspca_dev
->usb_lock
);
1533 /* Don't restart the stream when switching from read
1535 if (gspca_dev
->memory
== GSPCA_MEMORY_READ
)
1539 /* free the previous allocated buffers, if any */
1540 if (gspca_dev
->nframes
!= 0)
1541 frame_free(gspca_dev
);
1542 if (rb
->count
== 0) /* unrequest */
1544 ret
= frame_alloc(gspca_dev
, file
, rb
->memory
, rb
->count
);
1546 rb
->count
= gspca_dev
->nframes
;
1548 ret
= gspca_init_transfer(gspca_dev
);
1551 mutex_unlock(&gspca_dev
->queue_lock
);
1552 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1556 static int vidioc_querybuf(struct file
*file
, void *priv
,
1557 struct v4l2_buffer
*v4l2_buf
)
1559 struct gspca_dev
*gspca_dev
= priv
;
1560 struct gspca_frame
*frame
;
1562 if (v4l2_buf
->index
< 0
1563 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1566 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1567 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1571 static int vidioc_streamon(struct file
*file
, void *priv
,
1572 enum v4l2_buf_type buf_type
)
1574 struct gspca_dev
*gspca_dev
= priv
;
1577 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1579 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1580 return -ERESTARTSYS
;
1582 /* check the capture file */
1583 if (gspca_dev
->capt_file
!= file
) {
1588 if (gspca_dev
->nframes
== 0
1589 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1593 if (!gspca_dev
->streaming
) {
1594 ret
= gspca_init_transfer(gspca_dev
);
1599 if (gspca_debug
& D_STREAM
) {
1600 PDEBUG_MODE("stream on OK",
1608 mutex_unlock(&gspca_dev
->queue_lock
);
1612 static int vidioc_streamoff(struct file
*file
, void *priv
,
1613 enum v4l2_buf_type buf_type
)
1615 struct gspca_dev
*gspca_dev
= priv
;
1618 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1621 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1622 return -ERESTARTSYS
;
1624 if (!gspca_dev
->streaming
) {
1629 /* check the capture file */
1630 if (gspca_dev
->capt_file
!= file
) {
1635 /* stop streaming */
1636 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1640 gspca_dev
->usb_err
= 0;
1641 gspca_stream_off(gspca_dev
);
1642 mutex_unlock(&gspca_dev
->usb_lock
);
1643 /* In case another thread is waiting in dqbuf */
1644 wake_up_interruptible(&gspca_dev
->wq
);
1646 /* empty the transfer queues */
1647 atomic_set(&gspca_dev
->fr_q
, 0);
1648 atomic_set(&gspca_dev
->fr_i
, 0);
1649 gspca_dev
->fr_o
= 0;
1652 mutex_unlock(&gspca_dev
->queue_lock
);
1656 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1657 struct v4l2_jpegcompression
*jpegcomp
)
1659 struct gspca_dev
*gspca_dev
= priv
;
1662 if (!gspca_dev
->sd_desc
->get_jcomp
)
1664 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1665 return -ERESTARTSYS
;
1666 gspca_dev
->usb_err
= 0;
1667 if (gspca_dev
->present
)
1668 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1671 mutex_unlock(&gspca_dev
->usb_lock
);
1675 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1676 struct v4l2_jpegcompression
*jpegcomp
)
1678 struct gspca_dev
*gspca_dev
= priv
;
1681 if (!gspca_dev
->sd_desc
->set_jcomp
)
1683 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1684 return -ERESTARTSYS
;
1685 gspca_dev
->usb_err
= 0;
1686 if (gspca_dev
->present
)
1687 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1690 mutex_unlock(&gspca_dev
->usb_lock
);
1694 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1695 struct v4l2_streamparm
*parm
)
1697 struct gspca_dev
*gspca_dev
= priv
;
1699 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1701 if (gspca_dev
->sd_desc
->get_streamparm
) {
1704 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1705 return -ERESTARTSYS
;
1706 if (gspca_dev
->present
) {
1707 gspca_dev
->usb_err
= 0;
1708 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1709 ret
= gspca_dev
->usb_err
;
1713 mutex_unlock(&gspca_dev
->usb_lock
);
1720 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1721 struct v4l2_streamparm
*parm
)
1723 struct gspca_dev
*gspca_dev
= priv
;
1726 n
= parm
->parm
.capture
.readbuffers
;
1727 if (n
== 0 || n
>= GSPCA_MAX_FRAMES
)
1728 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1730 gspca_dev
->nbufread
= n
;
1732 if (gspca_dev
->sd_desc
->set_streamparm
) {
1735 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1736 return -ERESTARTSYS
;
1737 if (gspca_dev
->present
) {
1738 gspca_dev
->usb_err
= 0;
1739 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1740 ret
= gspca_dev
->usb_err
;
1744 mutex_unlock(&gspca_dev
->usb_lock
);
1751 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1753 struct gspca_dev
*gspca_dev
= file
->private_data
;
1754 struct gspca_frame
*frame
;
1756 unsigned long addr
, start
, size
;
1759 start
= vma
->vm_start
;
1760 size
= vma
->vm_end
- vma
->vm_start
;
1761 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1763 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1764 return -ERESTARTSYS
;
1765 if (!gspca_dev
->present
) {
1769 if (gspca_dev
->capt_file
!= file
) {
1775 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1776 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1777 PDEBUG(D_STREAM
, "mmap bad memory type");
1780 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1782 frame
= &gspca_dev
->frame
[i
];
1786 if (frame
== NULL
) {
1787 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1791 if (size
!= frame
->v4l2_buf
.length
) {
1792 PDEBUG(D_STREAM
, "mmap bad size");
1798 * - VM_IO marks the area as being a mmaped region for I/O to a
1799 * device. It also prevents the region from being core dumped.
1801 vma
->vm_flags
|= VM_IO
;
1803 addr
= (unsigned long) frame
->data
;
1805 page
= vmalloc_to_page((void *) addr
);
1806 ret
= vm_insert_page(vma
, start
, page
);
1814 vma
->vm_ops
= &gspca_vm_ops
;
1815 vma
->vm_private_data
= frame
;
1819 mutex_unlock(&gspca_dev
->queue_lock
);
1823 static int frame_ready_nolock(struct gspca_dev
*gspca_dev
, struct file
*file
,
1824 enum v4l2_memory memory
)
1826 if (!gspca_dev
->present
)
1828 if (gspca_dev
->capt_file
!= file
|| gspca_dev
->memory
!= memory
||
1829 !gspca_dev
->streaming
)
1832 /* check if a frame is ready */
1833 return gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
);
1836 static int frame_ready(struct gspca_dev
*gspca_dev
, struct file
*file
,
1837 enum v4l2_memory memory
)
1841 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1842 return -ERESTARTSYS
;
1843 ret
= frame_ready_nolock(gspca_dev
, file
, memory
);
1844 mutex_unlock(&gspca_dev
->queue_lock
);
1849 * dequeue a video buffer
1851 * If nonblock_ing is false, block until a buffer is available.
1853 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1854 struct v4l2_buffer
*v4l2_buf
)
1856 struct gspca_dev
*gspca_dev
= priv
;
1857 struct gspca_frame
*frame
;
1860 PDEBUG(D_FRAM
, "dqbuf");
1862 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1863 return -ERESTARTSYS
;
1866 ret
= frame_ready_nolock(gspca_dev
, file
, v4l2_buf
->memory
);
1872 mutex_unlock(&gspca_dev
->queue_lock
);
1874 if (file
->f_flags
& O_NONBLOCK
)
1877 /* wait till a frame is ready */
1878 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1879 frame_ready(gspca_dev
, file
, v4l2_buf
->memory
),
1880 msecs_to_jiffies(3000));
1886 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1887 return -ERESTARTSYS
;
1890 i
= gspca_dev
->fr_o
;
1891 j
= gspca_dev
->fr_queue
[i
];
1892 frame
= &gspca_dev
->frame
[j
];
1894 gspca_dev
->fr_o
= (i
+ 1) % GSPCA_MAX_FRAMES
;
1896 if (gspca_dev
->sd_desc
->dq_callback
) {
1897 mutex_lock(&gspca_dev
->usb_lock
);
1898 gspca_dev
->usb_err
= 0;
1899 if (gspca_dev
->present
)
1900 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1901 mutex_unlock(&gspca_dev
->usb_lock
);
1904 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1905 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1906 PDEBUG(D_FRAM
, "dqbuf %d", j
);
1909 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1910 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1912 frame
->v4l2_buf
.bytesused
)) {
1913 PDEBUG(D_ERR
|D_STREAM
,
1914 "dqbuf cp to user failed");
1919 mutex_unlock(&gspca_dev
->queue_lock
);
1924 * queue a video buffer
1926 * Attempting to queue a buffer that has already been
1927 * queued will return -EINVAL.
1929 static int vidioc_qbuf(struct file
*file
, void *priv
,
1930 struct v4l2_buffer
*v4l2_buf
)
1932 struct gspca_dev
*gspca_dev
= priv
;
1933 struct gspca_frame
*frame
;
1936 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1938 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1939 return -ERESTARTSYS
;
1941 index
= v4l2_buf
->index
;
1942 if ((unsigned) index
>= gspca_dev
->nframes
) {
1944 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1948 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1949 PDEBUG(D_FRAM
, "qbuf bad memory type");
1954 frame
= &gspca_dev
->frame
[index
];
1955 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1956 PDEBUG(D_FRAM
, "qbuf bad state");
1961 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1963 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1964 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1965 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1968 /* put the buffer in the 'queued' queue */
1969 i
= atomic_read(&gspca_dev
->fr_q
);
1970 gspca_dev
->fr_queue
[i
] = index
;
1971 atomic_set(&gspca_dev
->fr_q
, (i
+ 1) % GSPCA_MAX_FRAMES
);
1973 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1974 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1977 mutex_unlock(&gspca_dev
->queue_lock
);
1982 * allocate the resources for read()
1984 static int read_alloc(struct gspca_dev
*gspca_dev
,
1987 struct v4l2_buffer v4l2_buf
;
1990 PDEBUG(D_STREAM
, "read alloc");
1991 if (gspca_dev
->nframes
== 0) {
1992 struct v4l2_requestbuffers rb
;
1994 memset(&rb
, 0, sizeof rb
);
1995 rb
.count
= gspca_dev
->nbufread
;
1996 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1997 rb
.memory
= GSPCA_MEMORY_READ
;
1998 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
2000 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
2003 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2004 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2005 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2006 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
2008 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2010 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
2014 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
2017 /* start streaming */
2018 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2020 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
2024 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
2026 struct gspca_dev
*gspca_dev
= file
->private_data
;
2029 PDEBUG(D_FRAM
, "poll");
2031 poll_wait(file
, &gspca_dev
->wq
, wait
);
2033 /* if reqbufs is not done, the user would use read() */
2034 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) {
2035 ret
= read_alloc(gspca_dev
, file
);
2040 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
2043 /* check if an image has been received */
2044 if (gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
))
2045 ret
= POLLIN
| POLLRDNORM
; /* yes */
2048 mutex_unlock(&gspca_dev
->queue_lock
);
2049 if (!gspca_dev
->present
)
2054 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
2055 size_t count
, loff_t
*ppos
)
2057 struct gspca_dev
*gspca_dev
= file
->private_data
;
2058 struct gspca_frame
*frame
;
2059 struct v4l2_buffer v4l2_buf
;
2060 struct timeval timestamp
;
2063 PDEBUG(D_FRAM
, "read (%zd)", count
);
2064 if (!gspca_dev
->present
)
2066 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) { /* first time ? */
2067 ret
= read_alloc(gspca_dev
, file
);
2073 timestamp
= ktime_to_timeval(ktime_get());
2077 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2078 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2079 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2080 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
2082 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
2086 /* if the process slept for more than 1 second,
2087 * get a newer frame */
2088 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
2090 break; /* avoid infinite loop */
2091 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
2093 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2095 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
2100 /* copy the frame */
2101 if (count
> frame
->v4l2_buf
.bytesused
)
2102 count
= frame
->v4l2_buf
.bytesused
;
2103 ret
= copy_to_user(data
, frame
->data
, count
);
2105 PDEBUG(D_ERR
|D_STREAM
,
2106 "read cp to user lack %d / %zd", ret
, count
);
2112 /* in each case, requeue the buffer */
2113 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2119 static struct v4l2_file_operations dev_fops
= {
2120 .owner
= THIS_MODULE
,
2122 .release
= dev_close
,
2125 .unlocked_ioctl
= video_ioctl2
,
2129 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
2130 .vidioc_querycap
= vidioc_querycap
,
2131 .vidioc_dqbuf
= vidioc_dqbuf
,
2132 .vidioc_qbuf
= vidioc_qbuf
,
2133 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2134 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2135 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2136 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2137 .vidioc_streamon
= vidioc_streamon
,
2138 .vidioc_queryctrl
= vidioc_queryctrl
,
2139 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2140 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2141 .vidioc_querymenu
= vidioc_querymenu
,
2142 .vidioc_enum_input
= vidioc_enum_input
,
2143 .vidioc_g_input
= vidioc_g_input
,
2144 .vidioc_s_input
= vidioc_s_input
,
2145 .vidioc_reqbufs
= vidioc_reqbufs
,
2146 .vidioc_querybuf
= vidioc_querybuf
,
2147 .vidioc_streamoff
= vidioc_streamoff
,
2148 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
2149 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
2150 .vidioc_g_parm
= vidioc_g_parm
,
2151 .vidioc_s_parm
= vidioc_s_parm
,
2152 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2153 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2154 #ifdef CONFIG_VIDEO_ADV_DEBUG
2155 .vidioc_g_register
= vidioc_g_register
,
2156 .vidioc_s_register
= vidioc_s_register
,
2158 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2161 static const struct video_device gspca_template
= {
2162 .name
= "gspca main driver",
2164 .ioctl_ops
= &dev_ioctl_ops
,
2165 .release
= gspca_release
,
2168 /* initialize the controls */
2169 static void ctrls_init(struct gspca_dev
*gspca_dev
)
2171 struct gspca_ctrl
*ctrl
;
2174 for (i
= 0, ctrl
= gspca_dev
->cam
.ctrls
;
2175 i
< gspca_dev
->sd_desc
->nctrls
;
2177 ctrl
->def
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.default_value
;
2178 ctrl
->val
= ctrl
->def
;
2179 ctrl
->min
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.minimum
;
2180 ctrl
->max
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.maximum
;
2185 * probe and create a new gspca device
2187 * This function must be called by the sub-driver when it is
2188 * called for probing a new device.
2190 int gspca_dev_probe2(struct usb_interface
*intf
,
2191 const struct usb_device_id
*id
,
2192 const struct sd_desc
*sd_desc
,
2194 struct module
*module
)
2196 struct gspca_dev
*gspca_dev
;
2197 struct usb_device
*dev
= interface_to_usbdev(intf
);
2200 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
2202 /* create the device */
2203 if (dev_size
< sizeof *gspca_dev
)
2204 dev_size
= sizeof *gspca_dev
;
2205 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2207 pr_err("couldn't kzalloc gspca struct\n");
2210 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2211 if (!gspca_dev
->usb_buf
) {
2212 pr_err("out of memory\n");
2216 gspca_dev
->dev
= dev
;
2217 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2218 gspca_dev
->nbalt
= intf
->num_altsetting
;
2220 /* check if any audio device */
2221 if (dev
->config
->desc
.bNumInterfaces
!= 1) {
2223 struct usb_interface
*intf2
;
2225 for (i
= 0; i
< dev
->config
->desc
.bNumInterfaces
; i
++) {
2226 intf2
= dev
->config
->interface
[i
];
2228 && intf2
->altsetting
!= NULL
2229 && intf2
->altsetting
->desc
.bInterfaceClass
==
2231 gspca_dev
->audio
= 1;
2237 gspca_dev
->sd_desc
= sd_desc
;
2238 gspca_dev
->nbufread
= 2;
2239 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2241 /* configure the subdriver and initialize the USB device */
2242 ret
= sd_desc
->config(gspca_dev
, id
);
2245 if (gspca_dev
->cam
.ctrls
!= NULL
)
2246 ctrls_init(gspca_dev
);
2247 ret
= sd_desc
->init(gspca_dev
);
2250 gspca_set_default_mode(gspca_dev
);
2252 ret
= gspca_input_connect(gspca_dev
);
2256 mutex_init(&gspca_dev
->usb_lock
);
2257 mutex_init(&gspca_dev
->queue_lock
);
2258 init_waitqueue_head(&gspca_dev
->wq
);
2260 /* init video stuff */
2261 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
2262 gspca_dev
->vdev
.parent
= &intf
->dev
;
2263 gspca_dev
->module
= module
;
2264 gspca_dev
->present
= 1;
2265 ret
= video_register_device(&gspca_dev
->vdev
,
2269 pr_err("video_register_device err %d\n", ret
);
2273 usb_set_intfdata(intf
, gspca_dev
);
2274 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2276 gspca_input_create_urb(gspca_dev
);
2280 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2281 if (gspca_dev
->input_dev
)
2282 input_unregister_device(gspca_dev
->input_dev
);
2284 kfree(gspca_dev
->usb_buf
);
2288 EXPORT_SYMBOL(gspca_dev_probe2
);
2290 /* same function as the previous one, but check the interface */
2291 int gspca_dev_probe(struct usb_interface
*intf
,
2292 const struct usb_device_id
*id
,
2293 const struct sd_desc
*sd_desc
,
2295 struct module
*module
)
2297 struct usb_device
*dev
= interface_to_usbdev(intf
);
2299 /* we don't handle multi-config cameras */
2300 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2301 pr_err("%04x:%04x too many config\n",
2302 id
->idVendor
, id
->idProduct
);
2306 /* the USB video interface must be the first one */
2307 if (dev
->config
->desc
.bNumInterfaces
!= 1
2308 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2311 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
2313 EXPORT_SYMBOL(gspca_dev_probe
);
2318 * This function must be called by the sub-driver
2319 * when the device disconnects, after the specific resources are freed.
2321 void gspca_disconnect(struct usb_interface
*intf
)
2323 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2324 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2325 struct input_dev
*input_dev
;
2328 PDEBUG(D_PROBE
, "%s disconnect",
2329 video_device_node_name(&gspca_dev
->vdev
));
2330 mutex_lock(&gspca_dev
->usb_lock
);
2332 gspca_dev
->present
= 0;
2333 wake_up_interruptible(&gspca_dev
->wq
);
2335 destroy_urbs(gspca_dev
);
2337 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2338 gspca_input_destroy_urb(gspca_dev
);
2339 input_dev
= gspca_dev
->input_dev
;
2341 gspca_dev
->input_dev
= NULL
;
2342 input_unregister_device(input_dev
);
2346 /* the device is freed at exit of this function */
2347 gspca_dev
->dev
= NULL
;
2348 mutex_unlock(&gspca_dev
->usb_lock
);
2350 usb_set_intfdata(intf
, NULL
);
2352 /* release the device */
2353 /* (this will call gspca_release() immediately or on last close) */
2354 video_unregister_device(&gspca_dev
->vdev
);
2356 /* PDEBUG(D_PROBE, "disconnect complete"); */
2358 EXPORT_SYMBOL(gspca_disconnect
);
2361 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2363 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2365 if (!gspca_dev
->streaming
)
2367 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2368 if (gspca_dev
->sd_desc
->stopN
)
2369 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2370 destroy_urbs(gspca_dev
);
2371 gspca_input_destroy_urb(gspca_dev
);
2372 gspca_set_alt0(gspca_dev
);
2373 if (gspca_dev
->sd_desc
->stop0
)
2374 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2377 EXPORT_SYMBOL(gspca_suspend
);
2379 int gspca_resume(struct usb_interface
*intf
)
2381 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2383 gspca_dev
->frozen
= 0;
2384 gspca_dev
->sd_desc
->init(gspca_dev
);
2385 gspca_input_create_urb(gspca_dev
);
2386 if (gspca_dev
->streaming
)
2387 return gspca_init_transfer(gspca_dev
);
2390 EXPORT_SYMBOL(gspca_resume
);
2392 /* -- cam driver utility functions -- */
2394 /* auto gain and exposure algorithm based on the knee algorithm described here:
2395 http://ytse.tricolour.net/docs/LowLightOptimization.html
2397 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2399 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2400 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2402 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2403 const struct ctrl
*gain_ctrl
= NULL
;
2404 const struct ctrl
*exposure_ctrl
= NULL
;
2405 const struct ctrl
*autogain_ctrl
= NULL
;
2408 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2409 if (gspca_dev
->ctrl_dis
& (1 << i
))
2411 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2412 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2413 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2414 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2415 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2416 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2418 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2419 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2420 "on cam without (auto)gain/exposure");
2424 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2425 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2426 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2430 orig_exposure
= exposure
;
2432 /* If we are of a multiple of deadzone, do multiple steps to reach the
2433 desired lumination fast (with the risc of a slight overshoot) */
2434 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2436 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2437 avg_lum
, desired_avg_lum
, steps
);
2439 for (i
= 0; i
< steps
; i
++) {
2440 if (avg_lum
> desired_avg_lum
) {
2441 if (gain
> gain_knee
)
2443 else if (exposure
> exposure_knee
)
2445 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2447 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2449 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2454 if (gain
< gain_ctrl
->qctrl
.default_value
)
2456 else if (exposure
< exposure_knee
)
2458 else if (gain
< gain_knee
)
2460 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2462 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2469 if (gain
!= orig_gain
) {
2470 gain_ctrl
->set(gspca_dev
, gain
);
2473 if (exposure
!= orig_exposure
) {
2474 exposure_ctrl
->set(gspca_dev
, exposure
);
2480 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2482 /* -- module insert / remove -- */
2483 static int __init
gspca_init(void)
2485 pr_info("v" DRIVER_VERSION_NUMBER
" registered\n");
2488 static void __exit
gspca_exit(void)
2492 module_init(gspca_init
);
2493 module_exit(gspca_exit
);
2496 module_param_named(debug
, gspca_debug
, int, 0644);
2497 MODULE_PARM_DESC(debug
,
2498 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2499 " 0x08:stream 0x10:frame 0x20:packet"