2 * Main USB camera driver
4 * Copyright (C) 2008-2010 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 MODULE_NAME "gspca"
26 #include <linux/init.h>
27 #include <linux/version.h>
29 #include <linux/vmalloc.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
37 #include <linux/uaccess.h>
38 #include <linux/ktime.h>
39 #include <media/v4l2-ioctl.h>
43 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
44 #include <linux/input.h>
45 #include <linux/usb/input.h>
49 #define DEF_NURBS 3 /* default number of URBs */
50 #if DEF_NURBS > MAX_NURBS
51 #error "DEF_NURBS too big"
54 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 12, 0)
61 int gspca_debug
= D_ERR
| D_PROBE
;
62 EXPORT_SYMBOL(gspca_debug
);
64 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
66 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
67 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
71 (pixfmt
>> 16) & 0xff,
75 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct
*vma
)
96 struct gspca_frame
*frame
= vma
->vm_private_data
;
98 frame
->vma_use_count
++;
99 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
102 static void gspca_vm_close(struct vm_area_struct
*vma
)
104 struct gspca_frame
*frame
= vma
->vm_private_data
;
106 if (--frame
->vma_use_count
<= 0)
107 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
110 static const struct vm_operations_struct gspca_vm_ops
= {
111 .open
= gspca_vm_open
,
112 .close
= gspca_vm_close
,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb
*urb
)
121 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
127 if (gspca_dev
->sd_desc
->int_pkt_scan(gspca_dev
,
128 urb
->transfer_buffer
, urb
->actual_length
) < 0) {
129 PDEBUG(D_ERR
, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR
, "URB error %i, resubmitting", urb
->status
);
149 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
151 err("Resubmit URB failed with error %i", ret
);
155 static int gspca_input_connect(struct gspca_dev
*dev
)
157 struct input_dev
*input_dev
;
160 dev
->input_dev
= NULL
;
161 if (dev
->sd_desc
->int_pkt_scan
|| dev
->sd_desc
->other_input
) {
162 input_dev
= input_allocate_device();
166 usb_make_path(dev
->dev
, dev
->phys
, sizeof(dev
->phys
));
167 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
169 input_dev
->name
= dev
->sd_desc
->name
;
170 input_dev
->phys
= dev
->phys
;
172 usb_to_input_id(dev
->dev
, &input_dev
->id
);
174 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
175 input_dev
->keybit
[BIT_WORD(KEY_CAMERA
)] = BIT_MASK(KEY_CAMERA
);
176 input_dev
->dev
.parent
= &dev
->dev
->dev
;
178 err
= input_register_device(input_dev
);
180 err("Input device registration failed with error %i",
182 input_dev
->dev
.parent
= NULL
;
183 input_free_device(input_dev
);
185 dev
->input_dev
= input_dev
;
192 static int alloc_and_submit_int_urb(struct gspca_dev
*gspca_dev
,
193 struct usb_endpoint_descriptor
*ep
)
195 unsigned int buffer_len
;
198 struct usb_device
*dev
;
202 buffer_len
= le16_to_cpu(ep
->wMaxPacketSize
);
203 interval
= ep
->bInterval
;
204 PDEBUG(D_CONF
, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep
->bEndpointAddress
, buffer_len
, interval
);
208 dev
= gspca_dev
->dev
;
210 urb
= usb_alloc_urb(0, GFP_KERNEL
);
216 buffer
= usb_alloc_coherent(dev
, buffer_len
,
217 GFP_KERNEL
, &urb
->transfer_dma
);
222 usb_fill_int_urb(urb
, dev
,
223 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
225 int_irq
, (void *)gspca_dev
, interval
);
226 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
227 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
229 PDEBUG(D_ERR
, "submit int URB failed with error %i", ret
);
232 gspca_dev
->int_urb
= urb
;
236 usb_free_coherent(dev
,
237 urb
->transfer_buffer_length
,
238 urb
->transfer_buffer
,
246 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
248 struct usb_interface
*intf
;
249 struct usb_host_interface
*intf_desc
;
250 struct usb_endpoint_descriptor
*ep
;
253 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
254 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
255 intf_desc
= intf
->cur_altsetting
;
256 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
257 ep
= &intf_desc
->endpoint
[i
].desc
;
258 if (usb_endpoint_dir_in(ep
) &&
259 usb_endpoint_xfer_int(ep
)) {
261 alloc_and_submit_int_urb(gspca_dev
, ep
);
268 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
272 urb
= gspca_dev
->int_urb
;
274 gspca_dev
->int_urb
= NULL
;
276 usb_free_coherent(gspca_dev
->dev
,
277 urb
->transfer_buffer_length
,
278 urb
->transfer_buffer
,
284 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
288 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
292 static inline int gspca_input_connect(struct gspca_dev
*dev
)
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev
*gspca_dev
,
304 u8
*data
; /* address of data in the iso message */
308 if (urb
->status
!= 0) {
309 if (urb
->status
== -ESHUTDOWN
)
310 return; /* disconnection */
312 if (gspca_dev
->frozen
)
315 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
319 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
320 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
321 len
= urb
->iso_frame_desc
[i
].actual_length
;
323 /* check the packet status and length */
324 st
= urb
->iso_frame_desc
[i
].status
;
326 err("ISOC data error: [%d] len=%d, status=%d",
328 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
332 if (gspca_dev
->empty_packet
== 0)
333 gspca_dev
->empty_packet
= 1;
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
339 i
, urb
->iso_frame_desc
[i
].offset
, len
);
340 data
= (u8
*) urb
->transfer_buffer
341 + urb
->iso_frame_desc
[i
].offset
;
342 pkt_scan(gspca_dev
, data
, len
);
346 /* resubmit the URB */
347 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
349 err("usb_submit_urb() ret %d", st
);
353 * ISOC message interrupt from the USB device
355 * Analyse each packet and call the subdriver for copy to the frame buffer.
357 static void isoc_irq(struct urb
*urb
)
359 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
361 PDEBUG(D_PACK
, "isoc irq");
362 if (!gspca_dev
->streaming
)
364 fill_frame(gspca_dev
, urb
);
368 * bulk message interrupt from the USB device
370 static void bulk_irq(struct urb
*urb
)
372 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
375 PDEBUG(D_PACK
, "bulk irq");
376 if (!gspca_dev
->streaming
)
378 switch (urb
->status
) {
382 return; /* disconnection */
385 if (gspca_dev
->frozen
)
388 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
393 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
394 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
395 urb
->transfer_buffer
,
399 /* resubmit the URB */
400 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
401 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
403 err("usb_submit_urb() ret %d", st
);
408 * add data to the current frame
410 * This function is called by the subdrivers at interrupt level.
412 * To build a frame, these ones must add
414 * - 0 or many INTER_PACKETs
416 * DISCARD_PACKET invalidates the whole frame.
417 * On LAST_PACKET, a new frame is returned.
419 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
420 enum gspca_packet_type packet_type
,
424 struct gspca_frame
*frame
;
427 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
429 if (packet_type
== FIRST_PACKET
) {
430 i
= atomic_read(&gspca_dev
->fr_i
);
432 /* if there are no queued buffer, discard the whole frame */
433 if (i
== atomic_read(&gspca_dev
->fr_q
)) {
434 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
435 gspca_dev
->sequence
++;
438 j
= gspca_dev
->fr_queue
[i
];
439 frame
= &gspca_dev
->frame
[j
];
440 frame
->v4l2_buf
.timestamp
= ktime_to_timeval(ktime_get());
441 frame
->v4l2_buf
.sequence
= gspca_dev
->sequence
++;
442 gspca_dev
->image
= frame
->data
;
443 gspca_dev
->image_len
= 0;
445 switch (gspca_dev
->last_packet_type
) {
447 if (packet_type
== LAST_PACKET
)
448 gspca_dev
->last_packet_type
= packet_type
;
455 /* append the packet to the frame buffer */
457 if (gspca_dev
->image_len
+ len
> gspca_dev
->frsz
) {
458 PDEBUG(D_ERR
|D_PACK
, "frame overflow %d > %d",
459 gspca_dev
->image_len
+ len
,
461 packet_type
= DISCARD_PACKET
;
463 /* !! image is NULL only when last pkt is LAST or DISCARD
464 if (gspca_dev->image == NULL) {
465 err("gspca_frame_add() image == NULL");
469 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
471 gspca_dev
->image_len
+= len
;
474 gspca_dev
->last_packet_type
= packet_type
;
476 /* if last packet, invalidate packet concatenation until
477 * next first packet, wake up the application and advance
479 if (packet_type
== LAST_PACKET
) {
480 i
= atomic_read(&gspca_dev
->fr_i
);
481 j
= gspca_dev
->fr_queue
[i
];
482 frame
= &gspca_dev
->frame
[j
];
483 frame
->v4l2_buf
.bytesused
= gspca_dev
->image_len
;
484 frame
->v4l2_buf
.flags
= (frame
->v4l2_buf
.flags
485 | V4L2_BUF_FLAG_DONE
)
486 & ~V4L2_BUF_FLAG_QUEUED
;
487 i
= (i
+ 1) % GSPCA_MAX_FRAMES
;
488 atomic_set(&gspca_dev
->fr_i
, i
);
489 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
490 PDEBUG(D_FRAM
, "frame complete len:%d",
491 frame
->v4l2_buf
.bytesused
);
492 gspca_dev
->image
= NULL
;
493 gspca_dev
->image_len
= 0;
496 EXPORT_SYMBOL(gspca_frame_add
);
498 static int gspca_is_compressed(__u32 format
)
501 case V4L2_PIX_FMT_MJPEG
:
502 case V4L2_PIX_FMT_JPEG
:
503 case V4L2_PIX_FMT_SPCA561
:
504 case V4L2_PIX_FMT_PAC207
:
505 case V4L2_PIX_FMT_MR97310A
:
511 static int frame_alloc(struct gspca_dev
*gspca_dev
, struct file
*file
,
512 enum v4l2_memory memory
, unsigned int count
)
514 struct gspca_frame
*frame
;
518 i
= gspca_dev
->curr_mode
;
519 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
520 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
521 frsz
= PAGE_ALIGN(frsz
);
522 if (count
>= GSPCA_MAX_FRAMES
)
523 count
= GSPCA_MAX_FRAMES
- 1;
524 gspca_dev
->frbuf
= vmalloc_32(frsz
* count
);
525 if (!gspca_dev
->frbuf
) {
526 err("frame alloc failed");
529 gspca_dev
->capt_file
= file
;
530 gspca_dev
->memory
= memory
;
531 gspca_dev
->frsz
= frsz
;
532 gspca_dev
->nframes
= count
;
533 for (i
= 0; i
< count
; i
++) {
534 frame
= &gspca_dev
->frame
[i
];
535 frame
->v4l2_buf
.index
= i
;
536 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
537 frame
->v4l2_buf
.flags
= 0;
538 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
539 frame
->v4l2_buf
.length
= frsz
;
540 frame
->v4l2_buf
.memory
= memory
;
541 frame
->v4l2_buf
.sequence
= 0;
542 frame
->data
= gspca_dev
->frbuf
+ i
* frsz
;
543 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
545 atomic_set(&gspca_dev
->fr_q
, 0);
546 atomic_set(&gspca_dev
->fr_i
, 0);
551 static void frame_free(struct gspca_dev
*gspca_dev
)
555 PDEBUG(D_STREAM
, "frame free");
556 if (gspca_dev
->frbuf
!= NULL
) {
557 vfree(gspca_dev
->frbuf
);
558 gspca_dev
->frbuf
= NULL
;
559 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
560 gspca_dev
->frame
[i
].data
= NULL
;
562 gspca_dev
->nframes
= 0;
564 gspca_dev
->capt_file
= NULL
;
565 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
568 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
573 PDEBUG(D_STREAM
, "kill transfer");
574 for (i
= 0; i
< MAX_NURBS
; i
++) {
575 urb
= gspca_dev
->urb
[i
];
579 gspca_dev
->urb
[i
] = NULL
;
581 if (urb
->transfer_buffer
!= NULL
)
582 usb_free_coherent(gspca_dev
->dev
,
583 urb
->transfer_buffer_length
,
584 urb
->transfer_buffer
,
590 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
594 if (gspca_dev
->alt
== 0)
596 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
598 err("set alt 0 err %d", ret
);
602 /* Note: both the queue and the usb locks should be held when calling this */
603 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
605 gspca_dev
->streaming
= 0;
606 if (gspca_dev
->present
) {
607 if (gspca_dev
->sd_desc
->stopN
)
608 gspca_dev
->sd_desc
->stopN(gspca_dev
);
609 destroy_urbs(gspca_dev
);
610 gspca_input_destroy_urb(gspca_dev
);
611 gspca_set_alt0(gspca_dev
);
612 gspca_input_create_urb(gspca_dev
);
615 /* always call stop0 to free the subdriver's resources */
616 if (gspca_dev
->sd_desc
->stop0
)
617 gspca_dev
->sd_desc
->stop0(gspca_dev
);
618 PDEBUG(D_STREAM
, "stream off OK");
622 * look for an input transfer endpoint in an alternate setting
624 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
627 struct usb_host_endpoint
*ep
;
630 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
631 ep
= &alt
->endpoint
[i
];
632 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
634 && ep
->desc
.wMaxPacketSize
!= 0)
641 * look for an input (isoc or bulk) endpoint
643 * The endpoint is defined by the subdriver.
644 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
645 * This routine may be called many times when the bandwidth is too small
646 * (the bandwidth is checked on urb submit).
648 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
650 struct usb_interface
*intf
;
651 struct usb_host_endpoint
*ep
;
654 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
656 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
657 : USB_ENDPOINT_XFER_ISOC
;
658 i
= gspca_dev
->alt
; /* previous alt setting */
659 if (gspca_dev
->cam
.reverse_alts
) {
660 while (++i
< gspca_dev
->nbalt
) {
661 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
667 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
673 err("no transfer endpoint found");
676 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
677 i
, ep
->desc
.bEndpointAddress
);
678 gspca_dev
->alt
= i
; /* memorize the current alt setting */
679 if (gspca_dev
->nbalt
> 1) {
680 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
682 err("set alt %d err %d", i
, ret
);
690 * create the URBs for image transfer
692 static int create_urbs(struct gspca_dev
*gspca_dev
,
693 struct usb_host_endpoint
*ep
)
696 int n
, nurbs
, i
, psize
, npkt
, bsize
;
698 /* calculate the packet size and the number of packets */
699 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
701 if (!gspca_dev
->cam
.bulk
) { /* isoc */
703 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
704 if (gspca_dev
->pkt_size
== 0)
705 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
707 psize
= gspca_dev
->pkt_size
;
708 npkt
= gspca_dev
->cam
.npkt
;
710 npkt
= 32; /* default value */
711 bsize
= psize
* npkt
;
713 "isoc %d pkts size %d = bsize:%d",
718 bsize
= gspca_dev
->cam
.bulk_size
;
721 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
722 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
723 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
728 for (n
= 0; n
< nurbs
; n
++) {
729 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
731 err("usb_alloc_urb failed");
734 gspca_dev
->urb
[n
] = urb
;
735 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
740 if (urb
->transfer_buffer
== NULL
) {
741 err("usb_alloc_coherent failed");
744 urb
->dev
= gspca_dev
->dev
;
745 urb
->context
= gspca_dev
;
746 urb
->transfer_buffer_length
= bsize
;
747 if (npkt
!= 0) { /* ISOC */
748 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
749 ep
->desc
.bEndpointAddress
);
750 urb
->transfer_flags
= URB_ISO_ASAP
751 | URB_NO_TRANSFER_DMA_MAP
;
752 urb
->interval
= ep
->desc
.bInterval
;
753 urb
->complete
= isoc_irq
;
754 urb
->number_of_packets
= npkt
;
755 for (i
= 0; i
< npkt
; i
++) {
756 urb
->iso_frame_desc
[i
].length
= psize
;
757 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
760 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
761 ep
->desc
.bEndpointAddress
);
762 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
763 urb
->complete
= bulk_irq
;
770 * start the USB transfer
772 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
774 struct usb_host_endpoint
*ep
;
778 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
781 if (!gspca_dev
->present
) {
786 /* reset the streaming variables */
787 gspca_dev
->image
= NULL
;
788 gspca_dev
->image_len
= 0;
789 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
790 gspca_dev
->sequence
= 0;
792 gspca_dev
->usb_err
= 0;
794 /* set the higher alternate setting and
795 * loop until urb submit succeeds */
796 if (gspca_dev
->cam
.reverse_alts
)
799 gspca_dev
->alt
= gspca_dev
->nbalt
;
801 if (gspca_dev
->sd_desc
->isoc_init
) {
802 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
807 gspca_input_destroy_urb(gspca_dev
);
808 ep
= get_ep(gspca_dev
);
814 if (!gspca_dev
->cam
.no_urb_create
) {
815 PDEBUG(D_STREAM
, "init transfer alt %d",
817 ret
= create_urbs(gspca_dev
, ep
);
819 destroy_urbs(gspca_dev
);
824 /* clear the bulk endpoint */
825 if (gspca_dev
->cam
.bulk
)
826 usb_clear_halt(gspca_dev
->dev
,
827 gspca_dev
->urb
[0]->pipe
);
830 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
832 destroy_urbs(gspca_dev
);
835 gspca_dev
->streaming
= 1;
837 /* some bulk transfers are started by the subdriver */
838 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
841 /* submit the URBs */
842 for (n
= 0; n
< MAX_NURBS
; n
++) {
843 urb
= gspca_dev
->urb
[n
];
846 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
852 gspca_stream_off(gspca_dev
);
853 if (ret
!= -ENOSPC
) {
854 err("usb_submit_urb alt %d err %d",
855 gspca_dev
->alt
, ret
);
859 /* the bandwidth is not wide enough
860 * negociate or try a lower alternate setting */
861 PDEBUG(D_ERR
|D_STREAM
,
862 "bandwidth not wide enough - trying again");
863 msleep(20); /* wait for kill complete */
864 if (gspca_dev
->sd_desc
->isoc_nego
) {
865 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
869 ep
= get_ep(gspca_dev
);
877 gspca_input_create_urb(gspca_dev
);
879 mutex_unlock(&gspca_dev
->usb_lock
);
883 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
885 struct gspca_ctrl
*ctrl
;
888 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
889 gspca_dev
->curr_mode
= i
;
890 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
891 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
892 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
894 /* set the current control values to their default values
895 * which may have changed in sd_init() */
896 ctrl
= gspca_dev
->cam
.ctrls
;
899 i
< gspca_dev
->sd_desc
->nctrls
;
901 ctrl
->val
= ctrl
->def
;
905 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
906 int width
, int height
)
910 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
911 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
912 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
919 * search a mode with the right pixel format
921 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
927 modeU
= modeD
= mode
;
928 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
930 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
934 if (++modeU
< gspca_dev
->cam
.nmodes
) {
935 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
943 #ifdef CONFIG_VIDEO_ADV_DEBUG
944 static int vidioc_g_register(struct file
*file
, void *priv
,
945 struct v4l2_dbg_register
*reg
)
948 struct gspca_dev
*gspca_dev
= priv
;
950 if (!gspca_dev
->sd_desc
->get_chip_ident
)
953 if (!gspca_dev
->sd_desc
->get_register
)
956 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
958 gspca_dev
->usb_err
= 0;
959 if (gspca_dev
->present
)
960 ret
= gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
963 mutex_unlock(&gspca_dev
->usb_lock
);
968 static int vidioc_s_register(struct file
*file
, void *priv
,
969 struct v4l2_dbg_register
*reg
)
972 struct gspca_dev
*gspca_dev
= priv
;
974 if (!gspca_dev
->sd_desc
->get_chip_ident
)
977 if (!gspca_dev
->sd_desc
->set_register
)
980 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
982 gspca_dev
->usb_err
= 0;
983 if (gspca_dev
->present
)
984 ret
= gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
987 mutex_unlock(&gspca_dev
->usb_lock
);
993 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
994 struct v4l2_dbg_chip_ident
*chip
)
997 struct gspca_dev
*gspca_dev
= priv
;
999 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1002 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1003 return -ERESTARTSYS
;
1004 gspca_dev
->usb_err
= 0;
1005 if (gspca_dev
->present
)
1006 ret
= gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
1009 mutex_unlock(&gspca_dev
->usb_lock
);
1014 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1015 struct v4l2_fmtdesc
*fmtdesc
)
1017 struct gspca_dev
*gspca_dev
= priv
;
1021 /* give an index to each format */
1024 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1025 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1028 if (fmt_tb
[j
] == fmt_tb
[index
])
1033 if (fmtdesc
->index
== index
)
1034 break; /* new format */
1036 if (index
>= ARRAY_SIZE(fmt_tb
))
1041 return -EINVAL
; /* no more format */
1043 fmtdesc
->pixelformat
= fmt_tb
[index
];
1044 if (gspca_is_compressed(fmt_tb
[index
]))
1045 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1046 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1047 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1048 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1049 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1050 fmtdesc
->description
[4] = '\0';
1054 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1055 struct v4l2_format
*fmt
)
1057 struct gspca_dev
*gspca_dev
= priv
;
1060 mode
= gspca_dev
->curr_mode
;
1061 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1062 sizeof fmt
->fmt
.pix
);
1066 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1067 struct v4l2_format
*fmt
)
1069 int w
, h
, mode
, mode2
;
1071 w
= fmt
->fmt
.pix
.width
;
1072 h
= fmt
->fmt
.pix
.height
;
1075 if (gspca_debug
& D_CONF
)
1076 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
1078 /* search the closest mode for width and height */
1079 mode
= wxh_to_mode(gspca_dev
, w
, h
);
1081 /* OK if right palette */
1082 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1083 != fmt
->fmt
.pix
.pixelformat
) {
1085 /* else, search the closest mode with the same pixel format */
1086 mode2
= gspca_get_mode(gspca_dev
, mode
,
1087 fmt
->fmt
.pix
.pixelformat
);
1091 ; * no chance, return this mode */
1093 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1094 sizeof fmt
->fmt
.pix
);
1095 return mode
; /* used when s_fmt */
1098 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1100 struct v4l2_format
*fmt
)
1102 struct gspca_dev
*gspca_dev
= priv
;
1105 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1111 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1112 struct v4l2_format
*fmt
)
1114 struct gspca_dev
*gspca_dev
= priv
;
1117 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1118 return -ERESTARTSYS
;
1120 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1124 if (gspca_dev
->nframes
!= 0
1125 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1130 if (ret
== gspca_dev
->curr_mode
) {
1132 goto out
; /* same mode */
1135 if (gspca_dev
->streaming
) {
1139 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
1140 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
1141 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
1142 gspca_dev
->curr_mode
= ret
;
1146 mutex_unlock(&gspca_dev
->queue_lock
);
1150 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1151 struct v4l2_frmsizeenum
*fsize
)
1153 struct gspca_dev
*gspca_dev
= priv
;
1157 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1158 if (fsize
->pixel_format
!=
1159 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1162 if (fsize
->index
== index
) {
1163 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1164 fsize
->discrete
.width
=
1165 gspca_dev
->cam
.cam_mode
[i
].width
;
1166 fsize
->discrete
.height
=
1167 gspca_dev
->cam
.cam_mode
[i
].height
;
1176 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1177 struct v4l2_frmivalenum
*fival
)
1179 struct gspca_dev
*gspca_dev
= priv
;
1180 int mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1183 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1184 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1187 if (fival
->pixel_format
!=
1188 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1191 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1192 if (fival
->index
== i
) {
1193 fival
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1194 fival
->discrete
.numerator
= 1;
1195 fival
->discrete
.denominator
=
1196 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1204 static void gspca_release(struct video_device
*vfd
)
1206 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
1208 PDEBUG(D_PROBE
, "%s released",
1209 video_device_node_name(&gspca_dev
->vdev
));
1211 kfree(gspca_dev
->usb_buf
);
1215 static int dev_open(struct file
*file
)
1217 struct gspca_dev
*gspca_dev
;
1219 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1220 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
1221 if (!gspca_dev
->present
)
1224 /* protect the subdriver against rmmod */
1225 if (!try_module_get(gspca_dev
->module
))
1228 file
->private_data
= gspca_dev
;
1230 /* activate the v4l2 debug */
1231 if (gspca_debug
& D_V4L2
)
1232 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1233 | V4L2_DEBUG_IOCTL_ARG
;
1235 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1236 | V4L2_DEBUG_IOCTL_ARG
);
1241 static int dev_close(struct file
*file
)
1243 struct gspca_dev
*gspca_dev
= file
->private_data
;
1245 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1246 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1247 return -ERESTARTSYS
;
1249 /* if the file did the capture, free the streaming resources */
1250 if (gspca_dev
->capt_file
== file
) {
1251 if (gspca_dev
->streaming
) {
1252 mutex_lock(&gspca_dev
->usb_lock
);
1253 gspca_dev
->usb_err
= 0;
1254 gspca_stream_off(gspca_dev
);
1255 mutex_unlock(&gspca_dev
->usb_lock
);
1257 frame_free(gspca_dev
);
1259 file
->private_data
= NULL
;
1260 module_put(gspca_dev
->module
);
1261 mutex_unlock(&gspca_dev
->queue_lock
);
1263 PDEBUG(D_STREAM
, "close done");
1268 static int vidioc_querycap(struct file
*file
, void *priv
,
1269 struct v4l2_capability
*cap
)
1271 struct gspca_dev
*gspca_dev
= priv
;
1274 /* protect the access to the usb device */
1275 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1276 return -ERESTARTSYS
;
1277 if (!gspca_dev
->present
) {
1281 strncpy((char *) cap
->driver
, gspca_dev
->sd_desc
->name
,
1282 sizeof cap
->driver
);
1283 if (gspca_dev
->dev
->product
!= NULL
) {
1284 strncpy((char *) cap
->card
, gspca_dev
->dev
->product
,
1287 snprintf((char *) cap
->card
, sizeof cap
->card
,
1288 "USB Camera (%04x:%04x)",
1289 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1290 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1292 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1293 sizeof(cap
->bus_info
));
1294 cap
->version
= DRIVER_VERSION_NUMBER
;
1295 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
1296 | V4L2_CAP_STREAMING
1297 | V4L2_CAP_READWRITE
;
1300 mutex_unlock(&gspca_dev
->usb_lock
);
1304 static int get_ctrl(struct gspca_dev
*gspca_dev
,
1307 const struct ctrl
*ctrls
;
1310 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1311 i
< gspca_dev
->sd_desc
->nctrls
;
1313 if (gspca_dev
->ctrl_dis
& (1 << i
))
1315 if (id
== ctrls
->qctrl
.id
)
1321 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1322 struct v4l2_queryctrl
*q_ctrl
)
1324 struct gspca_dev
*gspca_dev
= priv
;
1325 const struct ctrl
*ctrls
;
1326 struct gspca_ctrl
*gspca_ctrl
;
1331 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1332 id
&= V4L2_CTRL_ID_MASK
;
1335 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1336 if (gspca_dev
->ctrl_dis
& (1 << i
))
1338 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1341 && gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1342 > gspca_dev
->sd_desc
->ctrls
[idx
].qctrl
.id
)
1347 idx
= get_ctrl(gspca_dev
, id
);
1351 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1352 memcpy(q_ctrl
, &ctrls
->qctrl
, sizeof *q_ctrl
);
1353 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1354 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1355 q_ctrl
->default_value
= gspca_ctrl
->def
;
1356 q_ctrl
->minimum
= gspca_ctrl
->min
;
1357 q_ctrl
->maximum
= gspca_ctrl
->max
;
1359 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1360 q_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1364 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1365 struct v4l2_control
*ctrl
)
1367 struct gspca_dev
*gspca_dev
= priv
;
1368 const struct ctrl
*ctrls
;
1369 struct gspca_ctrl
*gspca_ctrl
;
1372 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1375 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1377 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1378 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1379 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1380 if (ctrl
->value
< gspca_ctrl
->min
1381 || ctrl
->value
> gspca_ctrl
->max
)
1385 if (ctrl
->value
< ctrls
->qctrl
.minimum
1386 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1389 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1390 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1391 return -ERESTARTSYS
;
1392 if (!gspca_dev
->present
) {
1396 gspca_dev
->usb_err
= 0;
1397 if (ctrls
->set
!= NULL
) {
1398 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1401 if (gspca_ctrl
!= NULL
) {
1402 gspca_ctrl
->val
= ctrl
->value
;
1403 if (ctrls
->set_control
!= NULL
1404 && gspca_dev
->streaming
)
1405 ctrls
->set_control(gspca_dev
);
1407 ret
= gspca_dev
->usb_err
;
1409 mutex_unlock(&gspca_dev
->usb_lock
);
1413 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1414 struct v4l2_control
*ctrl
)
1416 struct gspca_dev
*gspca_dev
= priv
;
1417 const struct ctrl
*ctrls
;
1420 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1423 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1425 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1426 return -ERESTARTSYS
;
1427 if (!gspca_dev
->present
) {
1431 gspca_dev
->usb_err
= 0;
1432 if (ctrls
->get
!= NULL
) {
1433 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1436 if (gspca_dev
->cam
.ctrls
!= NULL
)
1437 ctrl
->value
= gspca_dev
->cam
.ctrls
[idx
].val
;
1440 mutex_unlock(&gspca_dev
->usb_lock
);
1444 static int vidioc_querymenu(struct file
*file
, void *priv
,
1445 struct v4l2_querymenu
*qmenu
)
1447 struct gspca_dev
*gspca_dev
= priv
;
1449 if (!gspca_dev
->sd_desc
->querymenu
)
1451 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1454 static int vidioc_enum_input(struct file
*file
, void *priv
,
1455 struct v4l2_input
*input
)
1457 struct gspca_dev
*gspca_dev
= priv
;
1459 if (input
->index
!= 0)
1461 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1462 input
->status
= gspca_dev
->cam
.input_flags
;
1463 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1464 sizeof input
->name
);
1468 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1474 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1481 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1482 struct v4l2_requestbuffers
*rb
)
1484 struct gspca_dev
*gspca_dev
= priv
;
1485 int i
, ret
= 0, streaming
;
1487 i
= rb
->memory
; /* (avoid compilation warning) */
1489 case GSPCA_MEMORY_READ
: /* (internal call) */
1490 case V4L2_MEMORY_MMAP
:
1491 case V4L2_MEMORY_USERPTR
:
1496 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1497 return -ERESTARTSYS
;
1499 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1500 && gspca_dev
->memory
!= GSPCA_MEMORY_READ
1501 && gspca_dev
->memory
!= rb
->memory
) {
1506 /* only one file may do the capture */
1507 if (gspca_dev
->capt_file
!= NULL
1508 && gspca_dev
->capt_file
!= file
) {
1513 /* if allocated, the buffers must not be mapped */
1514 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1515 if (gspca_dev
->frame
[i
].vma_use_count
) {
1521 /* stop streaming */
1522 streaming
= gspca_dev
->streaming
;
1524 mutex_lock(&gspca_dev
->usb_lock
);
1525 gspca_dev
->usb_err
= 0;
1526 gspca_stream_off(gspca_dev
);
1527 mutex_unlock(&gspca_dev
->usb_lock
);
1529 /* Don't restart the stream when switching from read to mmap mode */
1530 if (gspca_dev
->memory
== GSPCA_MEMORY_READ
)
1533 /* free the previous allocated buffers, if any */
1534 if (gspca_dev
->nframes
!= 0)
1535 frame_free(gspca_dev
);
1536 if (rb
->count
== 0) /* unrequest */
1538 ret
= frame_alloc(gspca_dev
, file
, rb
->memory
, rb
->count
);
1540 rb
->count
= gspca_dev
->nframes
;
1542 ret
= gspca_init_transfer(gspca_dev
);
1545 mutex_unlock(&gspca_dev
->queue_lock
);
1546 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1550 static int vidioc_querybuf(struct file
*file
, void *priv
,
1551 struct v4l2_buffer
*v4l2_buf
)
1553 struct gspca_dev
*gspca_dev
= priv
;
1554 struct gspca_frame
*frame
;
1556 if (v4l2_buf
->index
< 0
1557 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1560 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1561 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1565 static int vidioc_streamon(struct file
*file
, void *priv
,
1566 enum v4l2_buf_type buf_type
)
1568 struct gspca_dev
*gspca_dev
= priv
;
1571 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1573 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1574 return -ERESTARTSYS
;
1576 /* check the capture file */
1577 if (gspca_dev
->capt_file
!= file
) {
1582 if (gspca_dev
->nframes
== 0
1583 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1587 if (!gspca_dev
->streaming
) {
1588 ret
= gspca_init_transfer(gspca_dev
);
1593 if (gspca_debug
& D_STREAM
) {
1594 PDEBUG_MODE("stream on OK",
1602 mutex_unlock(&gspca_dev
->queue_lock
);
1606 static int vidioc_streamoff(struct file
*file
, void *priv
,
1607 enum v4l2_buf_type buf_type
)
1609 struct gspca_dev
*gspca_dev
= priv
;
1612 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1615 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1616 return -ERESTARTSYS
;
1618 if (!gspca_dev
->streaming
) {
1623 /* check the capture file */
1624 if (gspca_dev
->capt_file
!= file
) {
1629 /* stop streaming */
1630 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1634 gspca_dev
->usb_err
= 0;
1635 gspca_stream_off(gspca_dev
);
1636 mutex_unlock(&gspca_dev
->usb_lock
);
1637 /* In case another thread is waiting in dqbuf */
1638 wake_up_interruptible(&gspca_dev
->wq
);
1640 /* empty the transfer queues */
1641 atomic_set(&gspca_dev
->fr_q
, 0);
1642 atomic_set(&gspca_dev
->fr_i
, 0);
1643 gspca_dev
->fr_o
= 0;
1646 mutex_unlock(&gspca_dev
->queue_lock
);
1650 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1651 struct v4l2_jpegcompression
*jpegcomp
)
1653 struct gspca_dev
*gspca_dev
= priv
;
1656 if (!gspca_dev
->sd_desc
->get_jcomp
)
1658 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1659 return -ERESTARTSYS
;
1660 gspca_dev
->usb_err
= 0;
1661 if (gspca_dev
->present
)
1662 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1665 mutex_unlock(&gspca_dev
->usb_lock
);
1669 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1670 struct v4l2_jpegcompression
*jpegcomp
)
1672 struct gspca_dev
*gspca_dev
= priv
;
1675 if (!gspca_dev
->sd_desc
->set_jcomp
)
1677 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1678 return -ERESTARTSYS
;
1679 gspca_dev
->usb_err
= 0;
1680 if (gspca_dev
->present
)
1681 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1684 mutex_unlock(&gspca_dev
->usb_lock
);
1688 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1689 struct v4l2_streamparm
*parm
)
1691 struct gspca_dev
*gspca_dev
= priv
;
1693 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1695 if (gspca_dev
->sd_desc
->get_streamparm
) {
1698 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1699 return -ERESTARTSYS
;
1700 if (gspca_dev
->present
) {
1701 gspca_dev
->usb_err
= 0;
1702 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1703 ret
= gspca_dev
->usb_err
;
1707 mutex_unlock(&gspca_dev
->usb_lock
);
1714 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1715 struct v4l2_streamparm
*parm
)
1717 struct gspca_dev
*gspca_dev
= priv
;
1720 n
= parm
->parm
.capture
.readbuffers
;
1721 if (n
== 0 || n
>= GSPCA_MAX_FRAMES
)
1722 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1724 gspca_dev
->nbufread
= n
;
1726 if (gspca_dev
->sd_desc
->set_streamparm
) {
1729 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1730 return -ERESTARTSYS
;
1731 if (gspca_dev
->present
) {
1732 gspca_dev
->usb_err
= 0;
1733 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1734 ret
= gspca_dev
->usb_err
;
1738 mutex_unlock(&gspca_dev
->usb_lock
);
1745 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1747 struct gspca_dev
*gspca_dev
= file
->private_data
;
1748 struct gspca_frame
*frame
;
1750 unsigned long addr
, start
, size
;
1753 start
= vma
->vm_start
;
1754 size
= vma
->vm_end
- vma
->vm_start
;
1755 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1757 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1758 return -ERESTARTSYS
;
1759 if (!gspca_dev
->present
) {
1763 if (gspca_dev
->capt_file
!= file
) {
1769 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1770 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1771 PDEBUG(D_STREAM
, "mmap bad memory type");
1774 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1776 frame
= &gspca_dev
->frame
[i
];
1780 if (frame
== NULL
) {
1781 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1785 if (size
!= frame
->v4l2_buf
.length
) {
1786 PDEBUG(D_STREAM
, "mmap bad size");
1792 * - VM_IO marks the area as being a mmaped region for I/O to a
1793 * device. It also prevents the region from being core dumped.
1795 vma
->vm_flags
|= VM_IO
;
1797 addr
= (unsigned long) frame
->data
;
1799 page
= vmalloc_to_page((void *) addr
);
1800 ret
= vm_insert_page(vma
, start
, page
);
1808 vma
->vm_ops
= &gspca_vm_ops
;
1809 vma
->vm_private_data
= frame
;
1813 mutex_unlock(&gspca_dev
->queue_lock
);
1817 static int frame_ready_nolock(struct gspca_dev
*gspca_dev
, struct file
*file
,
1818 enum v4l2_memory memory
)
1820 if (!gspca_dev
->present
)
1822 if (gspca_dev
->capt_file
!= file
|| gspca_dev
->memory
!= memory
||
1823 !gspca_dev
->streaming
)
1826 /* check if a frame is ready */
1827 return gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
);
1830 static int frame_ready(struct gspca_dev
*gspca_dev
, struct file
*file
,
1831 enum v4l2_memory memory
)
1835 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1836 return -ERESTARTSYS
;
1837 ret
= frame_ready_nolock(gspca_dev
, file
, memory
);
1838 mutex_unlock(&gspca_dev
->queue_lock
);
1843 * dequeue a video buffer
1845 * If nonblock_ing is false, block until a buffer is available.
1847 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1848 struct v4l2_buffer
*v4l2_buf
)
1850 struct gspca_dev
*gspca_dev
= priv
;
1851 struct gspca_frame
*frame
;
1854 PDEBUG(D_FRAM
, "dqbuf");
1856 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1857 return -ERESTARTSYS
;
1860 ret
= frame_ready_nolock(gspca_dev
, file
, v4l2_buf
->memory
);
1866 mutex_unlock(&gspca_dev
->queue_lock
);
1868 if (file
->f_flags
& O_NONBLOCK
)
1871 /* wait till a frame is ready */
1872 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1873 frame_ready(gspca_dev
, file
, v4l2_buf
->memory
),
1874 msecs_to_jiffies(3000));
1880 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1881 return -ERESTARTSYS
;
1884 i
= gspca_dev
->fr_o
;
1885 j
= gspca_dev
->fr_queue
[i
];
1886 frame
= &gspca_dev
->frame
[j
];
1888 gspca_dev
->fr_o
= (i
+ 1) % GSPCA_MAX_FRAMES
;
1890 if (gspca_dev
->sd_desc
->dq_callback
) {
1891 mutex_lock(&gspca_dev
->usb_lock
);
1892 gspca_dev
->usb_err
= 0;
1893 if (gspca_dev
->present
)
1894 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1895 mutex_unlock(&gspca_dev
->usb_lock
);
1898 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1899 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1900 PDEBUG(D_FRAM
, "dqbuf %d", j
);
1903 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1904 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1906 frame
->v4l2_buf
.bytesused
)) {
1907 PDEBUG(D_ERR
|D_STREAM
,
1908 "dqbuf cp to user failed");
1913 mutex_unlock(&gspca_dev
->queue_lock
);
1918 * queue a video buffer
1920 * Attempting to queue a buffer that has already been
1921 * queued will return -EINVAL.
1923 static int vidioc_qbuf(struct file
*file
, void *priv
,
1924 struct v4l2_buffer
*v4l2_buf
)
1926 struct gspca_dev
*gspca_dev
= priv
;
1927 struct gspca_frame
*frame
;
1930 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1932 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1933 return -ERESTARTSYS
;
1935 index
= v4l2_buf
->index
;
1936 if ((unsigned) index
>= gspca_dev
->nframes
) {
1938 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1942 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1943 PDEBUG(D_FRAM
, "qbuf bad memory type");
1948 frame
= &gspca_dev
->frame
[index
];
1949 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1950 PDEBUG(D_FRAM
, "qbuf bad state");
1955 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1957 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1958 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1959 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1962 /* put the buffer in the 'queued' queue */
1963 i
= atomic_read(&gspca_dev
->fr_q
);
1964 gspca_dev
->fr_queue
[i
] = index
;
1965 atomic_set(&gspca_dev
->fr_q
, (i
+ 1) % GSPCA_MAX_FRAMES
);
1967 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1968 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1971 mutex_unlock(&gspca_dev
->queue_lock
);
1976 * allocate the resources for read()
1978 static int read_alloc(struct gspca_dev
*gspca_dev
,
1981 struct v4l2_buffer v4l2_buf
;
1984 PDEBUG(D_STREAM
, "read alloc");
1985 if (gspca_dev
->nframes
== 0) {
1986 struct v4l2_requestbuffers rb
;
1988 memset(&rb
, 0, sizeof rb
);
1989 rb
.count
= gspca_dev
->nbufread
;
1990 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1991 rb
.memory
= GSPCA_MEMORY_READ
;
1992 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1994 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1997 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1998 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1999 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2000 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
2002 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2004 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
2008 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
2011 /* start streaming */
2012 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2014 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
2018 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
2020 struct gspca_dev
*gspca_dev
= file
->private_data
;
2023 PDEBUG(D_FRAM
, "poll");
2025 poll_wait(file
, &gspca_dev
->wq
, wait
);
2027 /* if reqbufs is not done, the user would use read() */
2028 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) {
2029 ret
= read_alloc(gspca_dev
, file
);
2034 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
2037 /* check if an image has been received */
2038 if (gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
))
2039 ret
= POLLIN
| POLLRDNORM
; /* yes */
2042 mutex_unlock(&gspca_dev
->queue_lock
);
2043 if (!gspca_dev
->present
)
2048 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
2049 size_t count
, loff_t
*ppos
)
2051 struct gspca_dev
*gspca_dev
= file
->private_data
;
2052 struct gspca_frame
*frame
;
2053 struct v4l2_buffer v4l2_buf
;
2054 struct timeval timestamp
;
2057 PDEBUG(D_FRAM
, "read (%zd)", count
);
2058 if (!gspca_dev
->present
)
2060 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) { /* first time ? */
2061 ret
= read_alloc(gspca_dev
, file
);
2067 timestamp
= ktime_to_timeval(ktime_get());
2071 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2072 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2073 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2074 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
2076 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
2080 /* if the process slept for more than 1 second,
2081 * get a newer frame */
2082 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
2084 break; /* avoid infinite loop */
2085 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
2087 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2089 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
2094 /* copy the frame */
2095 if (count
> frame
->v4l2_buf
.bytesused
)
2096 count
= frame
->v4l2_buf
.bytesused
;
2097 ret
= copy_to_user(data
, frame
->data
, count
);
2099 PDEBUG(D_ERR
|D_STREAM
,
2100 "read cp to user lack %d / %zd", ret
, count
);
2106 /* in each case, requeue the buffer */
2107 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2113 static struct v4l2_file_operations dev_fops
= {
2114 .owner
= THIS_MODULE
,
2116 .release
= dev_close
,
2119 .unlocked_ioctl
= video_ioctl2
,
2123 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
2124 .vidioc_querycap
= vidioc_querycap
,
2125 .vidioc_dqbuf
= vidioc_dqbuf
,
2126 .vidioc_qbuf
= vidioc_qbuf
,
2127 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2128 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2129 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2130 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2131 .vidioc_streamon
= vidioc_streamon
,
2132 .vidioc_queryctrl
= vidioc_queryctrl
,
2133 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2134 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2135 .vidioc_querymenu
= vidioc_querymenu
,
2136 .vidioc_enum_input
= vidioc_enum_input
,
2137 .vidioc_g_input
= vidioc_g_input
,
2138 .vidioc_s_input
= vidioc_s_input
,
2139 .vidioc_reqbufs
= vidioc_reqbufs
,
2140 .vidioc_querybuf
= vidioc_querybuf
,
2141 .vidioc_streamoff
= vidioc_streamoff
,
2142 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
2143 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
2144 .vidioc_g_parm
= vidioc_g_parm
,
2145 .vidioc_s_parm
= vidioc_s_parm
,
2146 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2147 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2148 #ifdef CONFIG_VIDEO_ADV_DEBUG
2149 .vidioc_g_register
= vidioc_g_register
,
2150 .vidioc_s_register
= vidioc_s_register
,
2152 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2155 static struct video_device gspca_template
= {
2156 .name
= "gspca main driver",
2158 .ioctl_ops
= &dev_ioctl_ops
,
2159 .release
= gspca_release
,
2162 /* initialize the controls */
2163 static void ctrls_init(struct gspca_dev
*gspca_dev
)
2165 struct gspca_ctrl
*ctrl
;
2168 for (i
= 0, ctrl
= gspca_dev
->cam
.ctrls
;
2169 i
< gspca_dev
->sd_desc
->nctrls
;
2171 ctrl
->def
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.default_value
;
2172 ctrl
->val
= ctrl
->def
;
2173 ctrl
->min
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.minimum
;
2174 ctrl
->max
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.maximum
;
2179 * probe and create a new gspca device
2181 * This function must be called by the sub-driver when it is
2182 * called for probing a new device.
2184 int gspca_dev_probe2(struct usb_interface
*intf
,
2185 const struct usb_device_id
*id
,
2186 const struct sd_desc
*sd_desc
,
2188 struct module
*module
)
2190 struct gspca_dev
*gspca_dev
;
2191 struct usb_device
*dev
= interface_to_usbdev(intf
);
2194 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
2196 /* create the device */
2197 if (dev_size
< sizeof *gspca_dev
)
2198 dev_size
= sizeof *gspca_dev
;
2199 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2201 err("couldn't kzalloc gspca struct");
2204 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2205 if (!gspca_dev
->usb_buf
) {
2206 err("out of memory");
2210 gspca_dev
->dev
= dev
;
2211 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2212 gspca_dev
->nbalt
= intf
->num_altsetting
;
2214 /* check if any audio device */
2215 if (dev
->config
->desc
.bNumInterfaces
!= 1) {
2217 struct usb_interface
*intf2
;
2219 for (i
= 0; i
< dev
->config
->desc
.bNumInterfaces
; i
++) {
2220 intf2
= dev
->config
->interface
[i
];
2222 && intf2
->altsetting
!= NULL
2223 && intf2
->altsetting
->desc
.bInterfaceClass
==
2225 gspca_dev
->audio
= 1;
2231 gspca_dev
->sd_desc
= sd_desc
;
2232 gspca_dev
->nbufread
= 2;
2233 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2235 /* configure the subdriver and initialize the USB device */
2236 ret
= sd_desc
->config(gspca_dev
, id
);
2239 if (gspca_dev
->cam
.ctrls
!= NULL
)
2240 ctrls_init(gspca_dev
);
2241 ret
= sd_desc
->init(gspca_dev
);
2244 gspca_set_default_mode(gspca_dev
);
2246 ret
= gspca_input_connect(gspca_dev
);
2250 mutex_init(&gspca_dev
->usb_lock
);
2251 mutex_init(&gspca_dev
->queue_lock
);
2252 init_waitqueue_head(&gspca_dev
->wq
);
2254 /* init video stuff */
2255 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
2256 gspca_dev
->vdev
.parent
= &intf
->dev
;
2257 gspca_dev
->module
= module
;
2258 gspca_dev
->present
= 1;
2259 ret
= video_register_device(&gspca_dev
->vdev
,
2263 err("video_register_device err %d", ret
);
2267 usb_set_intfdata(intf
, gspca_dev
);
2268 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2270 gspca_input_create_urb(gspca_dev
);
2274 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2275 if (gspca_dev
->input_dev
)
2276 input_unregister_device(gspca_dev
->input_dev
);
2278 kfree(gspca_dev
->usb_buf
);
2282 EXPORT_SYMBOL(gspca_dev_probe2
);
2284 /* same function as the previous one, but check the interface */
2285 int gspca_dev_probe(struct usb_interface
*intf
,
2286 const struct usb_device_id
*id
,
2287 const struct sd_desc
*sd_desc
,
2289 struct module
*module
)
2291 struct usb_device
*dev
= interface_to_usbdev(intf
);
2293 /* we don't handle multi-config cameras */
2294 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2295 err("%04x:%04x too many config",
2296 id
->idVendor
, id
->idProduct
);
2300 /* the USB video interface must be the first one */
2301 if (dev
->config
->desc
.bNumInterfaces
!= 1
2302 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2305 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
2307 EXPORT_SYMBOL(gspca_dev_probe
);
2312 * This function must be called by the sub-driver
2313 * when the device disconnects, after the specific resources are freed.
2315 void gspca_disconnect(struct usb_interface
*intf
)
2317 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2318 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2319 struct input_dev
*input_dev
;
2322 PDEBUG(D_PROBE
, "%s disconnect",
2323 video_device_node_name(&gspca_dev
->vdev
));
2324 mutex_lock(&gspca_dev
->usb_lock
);
2326 gspca_dev
->present
= 0;
2327 wake_up_interruptible(&gspca_dev
->wq
);
2329 destroy_urbs(gspca_dev
);
2331 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2332 gspca_input_destroy_urb(gspca_dev
);
2333 input_dev
= gspca_dev
->input_dev
;
2335 gspca_dev
->input_dev
= NULL
;
2336 input_unregister_device(input_dev
);
2340 /* the device is freed at exit of this function */
2341 gspca_dev
->dev
= NULL
;
2342 mutex_unlock(&gspca_dev
->usb_lock
);
2344 usb_set_intfdata(intf
, NULL
);
2346 /* release the device */
2347 /* (this will call gspca_release() immediatly or on last close) */
2348 video_unregister_device(&gspca_dev
->vdev
);
2350 /* PDEBUG(D_PROBE, "disconnect complete"); */
2352 EXPORT_SYMBOL(gspca_disconnect
);
2355 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2357 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2359 if (!gspca_dev
->streaming
)
2361 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2362 if (gspca_dev
->sd_desc
->stopN
)
2363 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2364 destroy_urbs(gspca_dev
);
2365 gspca_input_destroy_urb(gspca_dev
);
2366 gspca_set_alt0(gspca_dev
);
2367 if (gspca_dev
->sd_desc
->stop0
)
2368 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2371 EXPORT_SYMBOL(gspca_suspend
);
2373 int gspca_resume(struct usb_interface
*intf
)
2375 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2377 gspca_dev
->frozen
= 0;
2378 gspca_dev
->sd_desc
->init(gspca_dev
);
2379 gspca_input_create_urb(gspca_dev
);
2380 if (gspca_dev
->streaming
)
2381 return gspca_init_transfer(gspca_dev
);
2384 EXPORT_SYMBOL(gspca_resume
);
2386 /* -- cam driver utility functions -- */
2388 /* auto gain and exposure algorithm based on the knee algorithm described here:
2389 http://ytse.tricolour.net/docs/LowLightOptimization.html
2391 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2393 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2394 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2396 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2397 const struct ctrl
*gain_ctrl
= NULL
;
2398 const struct ctrl
*exposure_ctrl
= NULL
;
2399 const struct ctrl
*autogain_ctrl
= NULL
;
2402 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2403 if (gspca_dev
->ctrl_dis
& (1 << i
))
2405 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2406 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2407 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2408 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2409 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2410 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2412 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2413 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2414 "on cam without (auto)gain/exposure");
2418 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2419 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2420 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2424 orig_exposure
= exposure
;
2426 /* If we are of a multiple of deadzone, do multiple steps to reach the
2427 desired lumination fast (with the risc of a slight overshoot) */
2428 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2430 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2431 avg_lum
, desired_avg_lum
, steps
);
2433 for (i
= 0; i
< steps
; i
++) {
2434 if (avg_lum
> desired_avg_lum
) {
2435 if (gain
> gain_knee
)
2437 else if (exposure
> exposure_knee
)
2439 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2441 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2443 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2448 if (gain
< gain_ctrl
->qctrl
.default_value
)
2450 else if (exposure
< exposure_knee
)
2452 else if (gain
< gain_knee
)
2454 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2456 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2463 if (gain
!= orig_gain
) {
2464 gain_ctrl
->set(gspca_dev
, gain
);
2467 if (exposure
!= orig_exposure
) {
2468 exposure_ctrl
->set(gspca_dev
, exposure
);
2474 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2476 /* -- module insert / remove -- */
2477 static int __init
gspca_init(void)
2479 info("v%d.%d.%d registered",
2480 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2481 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2482 DRIVER_VERSION_NUMBER
& 0xff);
2485 static void __exit
gspca_exit(void)
2489 module_init(gspca_init
);
2490 module_exit(gspca_exit
);
2493 module_param_named(debug
, gspca_debug
, int, 0644);
2494 MODULE_PARM_DESC(debug
,
2495 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2496 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"