2 * Main USB camera driver
4 * Copyright (C) 2008-2009 Jean-Francois 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/jiffies.h>
39 #include <media/v4l2-ioctl.h>
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-Francois Moine <http://moinejf.free.fr>");
55 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
56 MODULE_LICENSE("GPL");
58 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 9, 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
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 PDEBUG(D_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 PDEBUG(D_ERR
, "Input device registration failed "
181 "with error %i", err
);
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
= ep
->wMaxPacketSize
;
203 interval
= ep
->bInterval
;
204 PDEBUG(D_PROBE
, "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_buffer_alloc(dev
, ep
->wMaxPacketSize
,
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 gspca_dev
->int_urb
= urb
;
227 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
229 PDEBUG(D_ERR
, "submit URB failed with error %i", ret
);
236 urb
->transfer_buffer_length
,
237 urb
->transfer_buffer
,
245 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
247 struct usb_interface
*intf
;
248 struct usb_host_interface
*intf_desc
;
249 struct usb_endpoint_descriptor
*ep
;
252 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
253 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
254 intf_desc
= intf
->cur_altsetting
;
255 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
256 ep
= &intf_desc
->endpoint
[i
].desc
;
257 if (usb_endpoint_dir_in(ep
) &&
258 usb_endpoint_xfer_int(ep
)) {
260 alloc_and_submit_int_urb(gspca_dev
, ep
);
267 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
271 urb
= gspca_dev
->int_urb
;
273 gspca_dev
->int_urb
= NULL
;
275 usb_buffer_free(gspca_dev
->dev
,
276 urb
->transfer_buffer_length
,
277 urb
->transfer_buffer
,
283 #define gspca_input_connect(gspca_dev) 0
284 #define gspca_input_create_urb(gspca_dev)
285 #define gspca_input_destroy_urb(gspca_dev)
288 /* get the current input frame buffer */
289 struct gspca_frame
*gspca_get_i_frame(struct gspca_dev
*gspca_dev
)
291 struct gspca_frame
*frame
;
293 frame
= gspca_dev
->cur_frame
;
294 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
295 != V4L2_BUF_FLAG_QUEUED
)
299 EXPORT_SYMBOL(gspca_get_i_frame
);
302 * fill a video frame from an URB and resubmit
304 static void fill_frame(struct gspca_dev
*gspca_dev
,
307 u8
*data
; /* address of data in the iso message */
311 if (urb
->status
!= 0) {
312 if (urb
->status
== -ESHUTDOWN
)
313 return; /* disconnection */
315 if (gspca_dev
->frozen
)
318 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
322 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
323 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
325 /* check the packet status and length */
326 len
= urb
->iso_frame_desc
[i
].actual_length
;
328 if (gspca_dev
->empty_packet
== 0)
329 gspca_dev
->empty_packet
= 1;
332 st
= urb
->iso_frame_desc
[i
].status
;
335 "ISOC data error: [%d] len=%d, status=%d",
337 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
341 /* let the packet be analyzed by the subdriver */
342 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
343 i
, urb
->iso_frame_desc
[i
].offset
, len
);
344 data
= (u8
*) urb
->transfer_buffer
345 + urb
->iso_frame_desc
[i
].offset
;
346 pkt_scan(gspca_dev
, data
, len
);
350 /* resubmit the URB */
351 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
353 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
357 * ISOC message interrupt from the USB device
359 * Analyse each packet and call the subdriver for copy to the frame buffer.
361 static void isoc_irq(struct urb
*urb
)
363 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
365 PDEBUG(D_PACK
, "isoc irq");
366 if (!gspca_dev
->streaming
)
368 fill_frame(gspca_dev
, urb
);
372 * bulk message interrupt from the USB device
374 static void bulk_irq(struct urb
*urb
)
376 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
379 PDEBUG(D_PACK
, "bulk irq");
380 if (!gspca_dev
->streaming
)
382 switch (urb
->status
) {
386 return; /* disconnection */
389 if (gspca_dev
->frozen
)
392 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
397 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
398 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
399 urb
->transfer_buffer
,
403 /* resubmit the URB */
404 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
405 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
407 PDEBUG(D_ERR
|D_PACK
, "usb_submit_urb() ret %d", st
);
412 * add data to the current frame
414 * This function is called by the subdrivers at interrupt level.
416 * To build a frame, these ones must add
418 * - 0 or many INTER_PACKETs
420 * DISCARD_PACKET invalidates the whole frame.
421 * On LAST_PACKET, a new frame is returned.
423 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
424 enum gspca_packet_type packet_type
,
428 struct gspca_frame
*frame
;
431 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
433 /* check the availability of the frame buffer */
434 frame
= gspca_dev
->cur_frame
;
435 if ((frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
)
436 != V4L2_BUF_FLAG_QUEUED
) {
437 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
441 /* when start of a new frame, if the current frame buffer
442 * is not queued, discard the whole frame */
443 if (packet_type
== FIRST_PACKET
) {
444 frame
->data_end
= frame
->data
;
445 jiffies_to_timeval(get_jiffies_64(),
446 &frame
->v4l2_buf
.timestamp
);
447 frame
->v4l2_buf
.sequence
= ++gspca_dev
->sequence
;
448 } else if (gspca_dev
->last_packet_type
== DISCARD_PACKET
) {
449 if (packet_type
== LAST_PACKET
)
450 gspca_dev
->last_packet_type
= packet_type
;
454 /* append the packet to the frame buffer */
456 if (frame
->data_end
- frame
->data
+ len
457 > frame
->v4l2_buf
.length
) {
458 PDEBUG(D_ERR
|D_PACK
, "frame overflow %zd > %d",
459 frame
->data_end
- frame
->data
+ len
,
460 frame
->v4l2_buf
.length
);
461 packet_type
= DISCARD_PACKET
;
463 memcpy(frame
->data_end
, data
, len
);
464 frame
->data_end
+= len
;
467 gspca_dev
->last_packet_type
= packet_type
;
469 /* if last packet, wake up the application and advance in the queue */
470 if (packet_type
== LAST_PACKET
) {
471 frame
->v4l2_buf
.bytesused
= frame
->data_end
- frame
->data
;
472 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_QUEUED
;
473 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_DONE
;
474 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
475 i
= (gspca_dev
->fr_i
+ 1) % gspca_dev
->nframes
;
477 PDEBUG(D_FRAM
, "frame complete len:%d q:%d i:%d o:%d",
478 frame
->v4l2_buf
.bytesused
,
482 j
= gspca_dev
->fr_queue
[i
];
483 gspca_dev
->cur_frame
= &gspca_dev
->frame
[j
];
486 EXPORT_SYMBOL(gspca_frame_add
);
488 static int gspca_is_compressed(__u32 format
)
491 case V4L2_PIX_FMT_MJPEG
:
492 case V4L2_PIX_FMT_JPEG
:
493 case V4L2_PIX_FMT_SPCA561
:
494 case V4L2_PIX_FMT_PAC207
:
495 case V4L2_PIX_FMT_MR97310A
:
501 static void *rvmalloc(long size
)
506 mem
= vmalloc_32(size
);
508 adr
= (unsigned long) mem
;
510 SetPageReserved(vmalloc_to_page((void *) adr
));
518 static void rvfree(void *mem
, long size
)
522 adr
= (unsigned long) mem
;
524 ClearPageReserved(vmalloc_to_page((void *) adr
));
531 static int frame_alloc(struct gspca_dev
*gspca_dev
,
534 struct gspca_frame
*frame
;
538 i
= gspca_dev
->curr_mode
;
539 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
540 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
541 frsz
= PAGE_ALIGN(frsz
);
542 gspca_dev
->frsz
= frsz
;
543 if (count
> GSPCA_MAX_FRAMES
)
544 count
= GSPCA_MAX_FRAMES
;
545 gspca_dev
->frbuf
= rvmalloc(frsz
* count
);
546 if (!gspca_dev
->frbuf
) {
547 err("frame alloc failed");
550 gspca_dev
->nframes
= count
;
551 for (i
= 0; i
< count
; i
++) {
552 frame
= &gspca_dev
->frame
[i
];
553 frame
->v4l2_buf
.index
= i
;
554 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
555 frame
->v4l2_buf
.flags
= 0;
556 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
557 frame
->v4l2_buf
.length
= frsz
;
558 frame
->v4l2_buf
.memory
= gspca_dev
->memory
;
559 frame
->v4l2_buf
.sequence
= 0;
560 frame
->data
= frame
->data_end
=
561 gspca_dev
->frbuf
+ i
* frsz
;
562 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
564 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
565 gspca_dev
->cur_frame
= &gspca_dev
->frame
[0];
566 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
567 gspca_dev
->sequence
= 0;
571 static void frame_free(struct gspca_dev
*gspca_dev
)
575 PDEBUG(D_STREAM
, "frame free");
576 if (gspca_dev
->frbuf
!= NULL
) {
577 rvfree(gspca_dev
->frbuf
,
578 gspca_dev
->nframes
* gspca_dev
->frsz
);
579 gspca_dev
->frbuf
= NULL
;
580 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
581 gspca_dev
->frame
[i
].data
= NULL
;
583 gspca_dev
->nframes
= 0;
586 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
591 PDEBUG(D_STREAM
, "kill transfer");
592 for (i
= 0; i
< MAX_NURBS
; i
++) {
593 urb
= gspca_dev
->urb
[i
];
597 gspca_dev
->urb
[i
] = NULL
;
599 if (urb
->transfer_buffer
!= NULL
)
600 usb_buffer_free(gspca_dev
->dev
,
601 urb
->transfer_buffer_length
,
602 urb
->transfer_buffer
,
609 * look for an input transfer endpoint in an alternate setting
611 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
614 struct usb_host_endpoint
*ep
;
617 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
618 ep
= &alt
->endpoint
[i
];
619 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
621 && ep
->desc
.wMaxPacketSize
!= 0)
628 * look for an input (isoc or bulk) endpoint
630 * The endpoint is defined by the subdriver.
631 * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep).
632 * This routine may be called many times when the bandwidth is too small
633 * (the bandwidth is checked on urb submit).
635 static struct usb_host_endpoint
*get_ep(struct gspca_dev
*gspca_dev
)
637 struct usb_interface
*intf
;
638 struct usb_host_endpoint
*ep
;
641 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
643 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
644 : USB_ENDPOINT_XFER_ISOC
;
645 i
= gspca_dev
->alt
; /* previous alt setting */
646 if (gspca_dev
->cam
.reverse_alts
) {
647 while (++i
< gspca_dev
->nbalt
) {
648 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
654 ep
= alt_xfer(&intf
->altsetting
[i
], xfer
);
660 err("no transfer endpoint found");
663 PDEBUG(D_STREAM
, "use alt %d ep 0x%02x",
664 i
, ep
->desc
.bEndpointAddress
);
665 gspca_dev
->alt
= i
; /* memorize the current alt setting */
666 if (gspca_dev
->nbalt
> 1) {
667 gspca_input_destroy_urb(gspca_dev
);
668 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, i
);
670 err("set alt %d err %d", i
, ret
);
673 gspca_input_create_urb(gspca_dev
);
679 * create the URBs for image transfer
681 static int create_urbs(struct gspca_dev
*gspca_dev
,
682 struct usb_host_endpoint
*ep
)
685 int n
, nurbs
, i
, psize
, npkt
, bsize
;
687 /* calculate the packet size and the number of packets */
688 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
690 if (!gspca_dev
->cam
.bulk
) { /* isoc */
692 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
693 if (gspca_dev
->pkt_size
== 0)
694 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
696 psize
= gspca_dev
->pkt_size
;
697 npkt
= gspca_dev
->cam
.npkt
;
699 npkt
= 32; /* default value */
700 bsize
= psize
* npkt
;
702 "isoc %d pkts size %d = bsize:%d",
707 bsize
= gspca_dev
->cam
.bulk_size
;
710 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
711 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
712 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
717 for (n
= 0; n
< nurbs
; n
++) {
718 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
720 err("usb_alloc_urb failed");
723 gspca_dev
->urb
[n
] = urb
;
724 urb
->transfer_buffer
= usb_buffer_alloc(gspca_dev
->dev
,
729 if (urb
->transfer_buffer
== NULL
) {
730 err("usb_buffer_alloc failed");
733 urb
->dev
= gspca_dev
->dev
;
734 urb
->context
= gspca_dev
;
735 urb
->transfer_buffer_length
= bsize
;
736 if (npkt
!= 0) { /* ISOC */
737 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
738 ep
->desc
.bEndpointAddress
);
739 urb
->transfer_flags
= URB_ISO_ASAP
740 | URB_NO_TRANSFER_DMA_MAP
;
741 urb
->interval
= ep
->desc
.bInterval
;
742 urb
->complete
= isoc_irq
;
743 urb
->number_of_packets
= npkt
;
744 for (i
= 0; i
< npkt
; i
++) {
745 urb
->iso_frame_desc
[i
].length
= psize
;
746 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
749 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
750 ep
->desc
.bEndpointAddress
),
751 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
752 urb
->complete
= bulk_irq
;
759 * start the USB transfer
761 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
763 struct usb_host_endpoint
*ep
;
767 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
770 if (!gspca_dev
->present
) {
775 gspca_dev
->usb_err
= 0;
777 /* set the higher alternate setting and
778 * loop until urb submit succeeds */
779 if (gspca_dev
->cam
.reverse_alts
)
782 gspca_dev
->alt
= gspca_dev
->nbalt
;
784 if (gspca_dev
->sd_desc
->isoc_init
) {
785 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
789 ep
= get_ep(gspca_dev
);
795 if (!gspca_dev
->cam
.no_urb_create
) {
796 PDEBUG(D_STREAM
, "init transfer alt %d",
798 ret
= create_urbs(gspca_dev
, ep
);
800 destroy_urbs(gspca_dev
);
805 /* clear the bulk endpoint */
806 if (gspca_dev
->cam
.bulk
)
807 usb_clear_halt(gspca_dev
->dev
,
808 gspca_dev
->urb
[0]->pipe
);
811 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
813 destroy_urbs(gspca_dev
);
816 gspca_dev
->streaming
= 1;
818 /* some bulk transfers are started by the subdriver */
819 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
822 /* submit the URBs */
823 for (n
= 0; n
< MAX_NURBS
; n
++) {
824 urb
= gspca_dev
->urb
[n
];
827 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
833 gspca_dev
->streaming
= 0;
834 destroy_urbs(gspca_dev
);
835 if (ret
!= -ENOSPC
) {
836 PDEBUG(D_ERR
|D_STREAM
,
837 "usb_submit_urb alt %d err %d",
838 gspca_dev
->alt
, ret
);
842 /* the bandwidth is not wide enough
843 * negociate or try a lower alternate setting */
844 PDEBUG(D_ERR
|D_STREAM
,
845 "bandwidth not wide enough - trying again");
846 msleep(20); /* wait for kill complete */
847 if (gspca_dev
->sd_desc
->isoc_nego
) {
848 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
852 ep
= get_ep(gspca_dev
);
860 mutex_unlock(&gspca_dev
->usb_lock
);
864 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
868 if (gspca_dev
->alt
== 0)
870 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
872 PDEBUG(D_ERR
|D_STREAM
, "set alt 0 err %d", ret
);
876 /* Note: both the queue and the usb locks should be held when calling this */
877 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
879 gspca_dev
->streaming
= 0;
880 if (gspca_dev
->present
) {
881 if (gspca_dev
->sd_desc
->stopN
)
882 gspca_dev
->sd_desc
->stopN(gspca_dev
);
883 destroy_urbs(gspca_dev
);
884 gspca_input_destroy_urb(gspca_dev
);
885 gspca_set_alt0(gspca_dev
);
886 gspca_input_create_urb(gspca_dev
);
889 /* always call stop0 to free the subdriver's resources */
890 if (gspca_dev
->sd_desc
->stop0
)
891 gspca_dev
->sd_desc
->stop0(gspca_dev
);
892 PDEBUG(D_STREAM
, "stream off OK");
895 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
899 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
900 gspca_dev
->curr_mode
= i
;
901 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
902 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
903 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
906 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
907 int width
, int height
)
911 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
912 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
913 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
920 * search a mode with the right pixel format
922 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
928 modeU
= modeD
= mode
;
929 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
931 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
935 if (++modeU
< gspca_dev
->cam
.nmodes
) {
936 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
944 #ifdef CONFIG_VIDEO_ADV_DEBUG
945 static int vidioc_g_register(struct file
*file
, void *priv
,
946 struct v4l2_dbg_register
*reg
)
949 struct gspca_dev
*gspca_dev
= priv
;
951 if (!gspca_dev
->sd_desc
->get_chip_ident
)
954 if (!gspca_dev
->sd_desc
->get_register
)
957 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
959 gspca_dev
->usb_err
= 0;
960 if (gspca_dev
->present
)
961 ret
= gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
964 mutex_unlock(&gspca_dev
->usb_lock
);
969 static int vidioc_s_register(struct file
*file
, void *priv
,
970 struct v4l2_dbg_register
*reg
)
973 struct gspca_dev
*gspca_dev
= priv
;
975 if (!gspca_dev
->sd_desc
->get_chip_ident
)
978 if (!gspca_dev
->sd_desc
->set_register
)
981 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
983 gspca_dev
->usb_err
= 0;
984 if (gspca_dev
->present
)
985 ret
= gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
988 mutex_unlock(&gspca_dev
->usb_lock
);
994 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
995 struct v4l2_dbg_chip_ident
*chip
)
998 struct gspca_dev
*gspca_dev
= priv
;
1000 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1003 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1004 return -ERESTARTSYS
;
1005 gspca_dev
->usb_err
= 0;
1006 if (gspca_dev
->present
)
1007 ret
= gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
1010 mutex_unlock(&gspca_dev
->usb_lock
);
1015 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1016 struct v4l2_fmtdesc
*fmtdesc
)
1018 struct gspca_dev
*gspca_dev
= priv
;
1022 /* give an index to each format */
1025 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1026 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1029 if (fmt_tb
[j
] == fmt_tb
[index
])
1034 if (fmtdesc
->index
== index
)
1035 break; /* new format */
1037 if (index
>= ARRAY_SIZE(fmt_tb
))
1042 return -EINVAL
; /* no more format */
1044 fmtdesc
->pixelformat
= fmt_tb
[index
];
1045 if (gspca_is_compressed(fmt_tb
[index
]))
1046 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1047 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1048 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1049 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1050 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1051 fmtdesc
->description
[4] = '\0';
1055 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1056 struct v4l2_format
*fmt
)
1058 struct gspca_dev
*gspca_dev
= priv
;
1061 mode
= gspca_dev
->curr_mode
;
1062 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1063 sizeof fmt
->fmt
.pix
);
1067 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1068 struct v4l2_format
*fmt
)
1070 int w
, h
, mode
, mode2
;
1072 w
= fmt
->fmt
.pix
.width
;
1073 h
= fmt
->fmt
.pix
.height
;
1076 if (gspca_debug
& D_CONF
)
1077 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
1079 /* search the closest mode for width and height */
1080 mode
= wxh_to_mode(gspca_dev
, w
, h
);
1082 /* OK if right palette */
1083 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1084 != fmt
->fmt
.pix
.pixelformat
) {
1086 /* else, search the closest mode with the same pixel format */
1087 mode2
= gspca_get_mode(gspca_dev
, mode
,
1088 fmt
->fmt
.pix
.pixelformat
);
1092 ; * no chance, return this mode */
1094 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1095 sizeof fmt
->fmt
.pix
);
1096 return mode
; /* used when s_fmt */
1099 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1101 struct v4l2_format
*fmt
)
1103 struct gspca_dev
*gspca_dev
= priv
;
1106 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1112 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1113 struct v4l2_format
*fmt
)
1115 struct gspca_dev
*gspca_dev
= priv
;
1118 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1119 return -ERESTARTSYS
;
1121 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1125 if (gspca_dev
->nframes
!= 0
1126 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1131 if (ret
== gspca_dev
->curr_mode
) {
1133 goto out
; /* same mode */
1136 if (gspca_dev
->streaming
) {
1140 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
1141 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
1142 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
1143 gspca_dev
->curr_mode
= ret
;
1147 mutex_unlock(&gspca_dev
->queue_lock
);
1151 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1152 struct v4l2_frmsizeenum
*fsize
)
1154 struct gspca_dev
*gspca_dev
= priv
;
1158 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1159 if (fsize
->pixel_format
!=
1160 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1163 if (fsize
->index
== index
) {
1164 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1165 fsize
->discrete
.width
=
1166 gspca_dev
->cam
.cam_mode
[i
].width
;
1167 fsize
->discrete
.height
=
1168 gspca_dev
->cam
.cam_mode
[i
].height
;
1177 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1178 struct v4l2_frmivalenum
*fival
)
1180 struct gspca_dev
*gspca_dev
= priv
;
1181 int mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1184 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1185 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1188 if (fival
->pixel_format
!=
1189 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1192 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1193 if (fival
->index
== i
) {
1194 fival
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1195 fival
->discrete
.numerator
= 1;
1196 fival
->discrete
.denominator
=
1197 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1205 static void gspca_release(struct video_device
*vfd
)
1207 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
1209 PDEBUG(D_PROBE
, "%s released",
1210 video_device_node_name(&gspca_dev
->vdev
));
1212 kfree(gspca_dev
->usb_buf
);
1216 static int dev_open(struct file
*file
)
1218 struct gspca_dev
*gspca_dev
;
1221 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1222 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
1223 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1224 return -ERESTARTSYS
;
1225 if (!gspca_dev
->present
) {
1230 if (gspca_dev
->users
> 4) { /* (arbitrary value) */
1235 /* protect the subdriver against rmmod */
1236 if (!try_module_get(gspca_dev
->module
)) {
1243 file
->private_data
= gspca_dev
;
1245 /* activate the v4l2 debug */
1246 if (gspca_debug
& D_V4L2
)
1247 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1248 | V4L2_DEBUG_IOCTL_ARG
;
1250 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1251 | V4L2_DEBUG_IOCTL_ARG
);
1255 mutex_unlock(&gspca_dev
->queue_lock
);
1257 PDEBUG(D_ERR
|D_STREAM
, "open failed err %d", ret
);
1259 PDEBUG(D_STREAM
, "open done");
1263 static int dev_close(struct file
*file
)
1265 struct gspca_dev
*gspca_dev
= file
->private_data
;
1267 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1268 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1269 return -ERESTARTSYS
;
1272 /* if the file did the capture, free the streaming resources */
1273 if (gspca_dev
->capt_file
== file
) {
1274 if (gspca_dev
->streaming
) {
1275 mutex_lock(&gspca_dev
->usb_lock
);
1276 gspca_dev
->usb_err
= 0;
1277 gspca_stream_off(gspca_dev
);
1278 mutex_unlock(&gspca_dev
->usb_lock
);
1280 frame_free(gspca_dev
);
1281 gspca_dev
->capt_file
= NULL
;
1282 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
1284 file
->private_data
= NULL
;
1285 module_put(gspca_dev
->module
);
1286 mutex_unlock(&gspca_dev
->queue_lock
);
1288 PDEBUG(D_STREAM
, "close done");
1293 static int vidioc_querycap(struct file
*file
, void *priv
,
1294 struct v4l2_capability
*cap
)
1296 struct gspca_dev
*gspca_dev
= priv
;
1299 /* protect the access to the usb device */
1300 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1301 return -ERESTARTSYS
;
1302 if (!gspca_dev
->present
) {
1306 strncpy(cap
->driver
, gspca_dev
->sd_desc
->name
, sizeof cap
->driver
);
1307 if (gspca_dev
->dev
->product
!= NULL
) {
1308 strncpy(cap
->card
, gspca_dev
->dev
->product
,
1311 snprintf(cap
->card
, sizeof cap
->card
,
1312 "USB Camera (%04x:%04x)",
1313 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1314 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1316 usb_make_path(gspca_dev
->dev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1317 cap
->version
= DRIVER_VERSION_NUMBER
;
1318 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
1319 | V4L2_CAP_STREAMING
1320 | V4L2_CAP_READWRITE
;
1323 mutex_unlock(&gspca_dev
->usb_lock
);
1327 static const struct ctrl
*get_ctrl(struct gspca_dev
*gspca_dev
,
1330 const struct ctrl
*ctrls
;
1333 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1334 i
< gspca_dev
->sd_desc
->nctrls
;
1336 if (gspca_dev
->ctrl_dis
& (1 << i
))
1338 if (id
== ctrls
->qctrl
.id
)
1344 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1345 struct v4l2_queryctrl
*q_ctrl
)
1347 struct gspca_dev
*gspca_dev
= priv
;
1348 const struct ctrl
*ctrls
;
1354 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1355 id
&= V4L2_CTRL_ID_MASK
;
1357 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1358 if (gspca_dev
->ctrl_dis
& (1 << i
))
1360 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1362 if (ctrls
&& gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1365 ctrls
= &gspca_dev
->sd_desc
->ctrls
[i
];
1370 ctrls
= get_ctrl(gspca_dev
, id
);
1373 i
= ctrls
- gspca_dev
->sd_desc
->ctrls
;
1375 memcpy(q_ctrl
, ctrls
, sizeof *q_ctrl
);
1376 if (gspca_dev
->ctrl_inac
& (1 << i
))
1377 q_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1381 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1382 struct v4l2_control
*ctrl
)
1384 struct gspca_dev
*gspca_dev
= priv
;
1385 const struct ctrl
*ctrls
;
1388 ctrls
= get_ctrl(gspca_dev
, ctrl
->id
);
1392 if (ctrl
->value
< ctrls
->qctrl
.minimum
1393 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1395 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1396 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1397 return -ERESTARTSYS
;
1398 gspca_dev
->usb_err
= 0;
1399 if (gspca_dev
->present
)
1400 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1403 mutex_unlock(&gspca_dev
->usb_lock
);
1407 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1408 struct v4l2_control
*ctrl
)
1410 struct gspca_dev
*gspca_dev
= priv
;
1411 const struct ctrl
*ctrls
;
1414 ctrls
= get_ctrl(gspca_dev
, ctrl
->id
);
1418 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1419 return -ERESTARTSYS
;
1420 gspca_dev
->usb_err
= 0;
1421 if (gspca_dev
->present
)
1422 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1425 mutex_unlock(&gspca_dev
->usb_lock
);
1429 /*fixme: have an audio flag in gspca_dev?*/
1430 static int vidioc_s_audio(struct file
*file
, void *priv
,
1431 struct v4l2_audio
*audio
)
1433 if (audio
->index
!= 0)
1438 static int vidioc_g_audio(struct file
*file
, void *priv
,
1439 struct v4l2_audio
*audio
)
1441 strcpy(audio
->name
, "Microphone");
1445 static int vidioc_enumaudio(struct file
*file
, void *priv
,
1446 struct v4l2_audio
*audio
)
1448 if (audio
->index
!= 0)
1451 strcpy(audio
->name
, "Microphone");
1452 audio
->capability
= 0;
1457 static int vidioc_querymenu(struct file
*file
, void *priv
,
1458 struct v4l2_querymenu
*qmenu
)
1460 struct gspca_dev
*gspca_dev
= priv
;
1462 if (!gspca_dev
->sd_desc
->querymenu
)
1464 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1467 static int vidioc_enum_input(struct file
*file
, void *priv
,
1468 struct v4l2_input
*input
)
1470 struct gspca_dev
*gspca_dev
= priv
;
1472 if (input
->index
!= 0)
1474 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1475 input
->status
= gspca_dev
->cam
.input_flags
;
1476 strncpy(input
->name
, gspca_dev
->sd_desc
->name
,
1477 sizeof input
->name
);
1481 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1487 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1494 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1495 struct v4l2_requestbuffers
*rb
)
1497 struct gspca_dev
*gspca_dev
= priv
;
1500 switch (rb
->memory
) {
1501 case GSPCA_MEMORY_READ
: /* (internal call) */
1502 case V4L2_MEMORY_MMAP
:
1503 case V4L2_MEMORY_USERPTR
:
1508 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1509 return -ERESTARTSYS
;
1511 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1512 && gspca_dev
->memory
!= rb
->memory
) {
1517 /* only one file may do the capture */
1518 if (gspca_dev
->capt_file
!= NULL
1519 && gspca_dev
->capt_file
!= file
) {
1524 /* if allocated, the buffers must not be mapped */
1525 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1526 if (gspca_dev
->frame
[i
].vma_use_count
) {
1532 /* stop streaming */
1533 if (gspca_dev
->streaming
) {
1534 mutex_lock(&gspca_dev
->usb_lock
);
1535 gspca_dev
->usb_err
= 0;
1536 gspca_stream_off(gspca_dev
);
1537 mutex_unlock(&gspca_dev
->usb_lock
);
1540 /* free the previous allocated buffers, if any */
1541 if (gspca_dev
->nframes
!= 0) {
1542 frame_free(gspca_dev
);
1543 gspca_dev
->capt_file
= NULL
;
1545 if (rb
->count
== 0) /* unrequest */
1547 gspca_dev
->memory
= rb
->memory
;
1548 ret
= frame_alloc(gspca_dev
, rb
->count
);
1550 rb
->count
= gspca_dev
->nframes
;
1551 gspca_dev
->capt_file
= file
;
1554 mutex_unlock(&gspca_dev
->queue_lock
);
1555 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1559 static int vidioc_querybuf(struct file
*file
, void *priv
,
1560 struct v4l2_buffer
*v4l2_buf
)
1562 struct gspca_dev
*gspca_dev
= priv
;
1563 struct gspca_frame
*frame
;
1565 if (v4l2_buf
->index
< 0
1566 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1569 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1570 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1574 static int vidioc_streamon(struct file
*file
, void *priv
,
1575 enum v4l2_buf_type buf_type
)
1577 struct gspca_dev
*gspca_dev
= priv
;
1580 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1582 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1583 return -ERESTARTSYS
;
1585 if (gspca_dev
->nframes
== 0
1586 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1590 if (!gspca_dev
->streaming
) {
1591 ret
= gspca_init_transfer(gspca_dev
);
1596 if (gspca_debug
& D_STREAM
) {
1597 PDEBUG_MODE("stream on OK",
1605 mutex_unlock(&gspca_dev
->queue_lock
);
1609 static int vidioc_streamoff(struct file
*file
, void *priv
,
1610 enum v4l2_buf_type buf_type
)
1612 struct gspca_dev
*gspca_dev
= priv
;
1615 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1617 if (!gspca_dev
->streaming
)
1619 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1620 return -ERESTARTSYS
;
1622 /* stop streaming */
1623 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1627 gspca_dev
->usb_err
= 0;
1628 gspca_stream_off(gspca_dev
);
1629 mutex_unlock(&gspca_dev
->usb_lock
);
1631 /* empty the application queues */
1632 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1633 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1634 gspca_dev
->fr_i
= gspca_dev
->fr_o
= gspca_dev
->fr_q
= 0;
1635 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1636 gspca_dev
->sequence
= 0;
1639 mutex_unlock(&gspca_dev
->queue_lock
);
1643 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1644 struct v4l2_jpegcompression
*jpegcomp
)
1646 struct gspca_dev
*gspca_dev
= priv
;
1649 if (!gspca_dev
->sd_desc
->get_jcomp
)
1651 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1652 return -ERESTARTSYS
;
1653 gspca_dev
->usb_err
= 0;
1654 if (gspca_dev
->present
)
1655 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1658 mutex_unlock(&gspca_dev
->usb_lock
);
1662 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1663 struct v4l2_jpegcompression
*jpegcomp
)
1665 struct gspca_dev
*gspca_dev
= priv
;
1668 if (!gspca_dev
->sd_desc
->set_jcomp
)
1670 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1671 return -ERESTARTSYS
;
1672 gspca_dev
->usb_err
= 0;
1673 if (gspca_dev
->present
)
1674 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1677 mutex_unlock(&gspca_dev
->usb_lock
);
1681 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1682 struct v4l2_streamparm
*parm
)
1684 struct gspca_dev
*gspca_dev
= priv
;
1686 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1688 if (gspca_dev
->sd_desc
->get_streamparm
) {
1691 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1692 return -ERESTARTSYS
;
1693 gspca_dev
->usb_err
= 0;
1694 if (gspca_dev
->present
)
1695 ret
= gspca_dev
->sd_desc
->get_streamparm(gspca_dev
,
1699 mutex_unlock(&gspca_dev
->usb_lock
);
1706 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1707 struct v4l2_streamparm
*parm
)
1709 struct gspca_dev
*gspca_dev
= priv
;
1712 n
= parm
->parm
.capture
.readbuffers
;
1713 if (n
== 0 || n
> GSPCA_MAX_FRAMES
)
1714 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1716 gspca_dev
->nbufread
= n
;
1718 if (gspca_dev
->sd_desc
->set_streamparm
) {
1721 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1722 return -ERESTARTSYS
;
1723 gspca_dev
->usb_err
= 0;
1724 if (gspca_dev
->present
)
1725 ret
= gspca_dev
->sd_desc
->set_streamparm(gspca_dev
,
1729 mutex_unlock(&gspca_dev
->usb_lock
);
1736 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1737 static int vidiocgmbuf(struct file
*file
, void *priv
,
1738 struct video_mbuf
*mbuf
)
1740 struct gspca_dev
*gspca_dev
= file
->private_data
;
1743 PDEBUG(D_STREAM
, "cgmbuf");
1744 if (gspca_dev
->nframes
== 0) {
1748 struct v4l2_format fmt
;
1750 fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1751 i
= gspca_dev
->cam
.nmodes
- 1; /* highest mode */
1752 fmt
.fmt
.pix
.width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1753 fmt
.fmt
.pix
.height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1754 fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_BGR24
;
1755 ret
= vidioc_s_fmt_vid_cap(file
, priv
, &fmt
);
1760 struct v4l2_requestbuffers rb
;
1762 memset(&rb
, 0, sizeof rb
);
1764 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1765 rb
.memory
= V4L2_MEMORY_MMAP
;
1766 ret
= vidioc_reqbufs(file
, priv
, &rb
);
1771 mbuf
->frames
= gspca_dev
->nframes
;
1772 mbuf
->size
= gspca_dev
->frsz
* gspca_dev
->nframes
;
1773 for (i
= 0; i
< mbuf
->frames
; i
++)
1774 mbuf
->offsets
[i
] = gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
;
1779 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1781 struct gspca_dev
*gspca_dev
= file
->private_data
;
1782 struct gspca_frame
*frame
;
1784 unsigned long addr
, start
, size
;
1787 start
= vma
->vm_start
;
1788 size
= vma
->vm_end
- vma
->vm_start
;
1789 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1791 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1792 return -ERESTARTSYS
;
1793 if (!gspca_dev
->present
) {
1797 if (gspca_dev
->capt_file
!= file
) {
1803 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1804 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1805 PDEBUG(D_STREAM
, "mmap bad memory type");
1808 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1810 frame
= &gspca_dev
->frame
[i
];
1814 if (frame
== NULL
) {
1815 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1819 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1820 /* v4l1 maps all the buffers */
1822 || size
!= frame
->v4l2_buf
.length
* gspca_dev
->nframes
)
1824 if (size
!= frame
->v4l2_buf
.length
) {
1825 PDEBUG(D_STREAM
, "mmap bad size");
1831 * - VM_IO marks the area as being a mmaped region for I/O to a
1832 * device. It also prevents the region from being core dumped.
1834 vma
->vm_flags
|= VM_IO
;
1836 addr
= (unsigned long) frame
->data
;
1838 page
= vmalloc_to_page((void *) addr
);
1839 ret
= vm_insert_page(vma
, start
, page
);
1847 vma
->vm_ops
= &gspca_vm_ops
;
1848 vma
->vm_private_data
= frame
;
1852 mutex_unlock(&gspca_dev
->queue_lock
);
1857 * wait for a video frame
1859 * If a frame is ready, its index is returned.
1861 static int frame_wait(struct gspca_dev
*gspca_dev
,
1864 struct gspca_frame
*frame
;
1867 /* check if a frame is ready */
1868 i
= gspca_dev
->fr_o
;
1869 j
= gspca_dev
->fr_queue
[i
];
1870 frame
= &gspca_dev
->frame
[j
];
1872 if (!(frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)) {
1876 /* wait till a frame is ready */
1877 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1878 (frame
->v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
) ||
1879 !gspca_dev
->streaming
|| !gspca_dev
->present
,
1880 msecs_to_jiffies(3000));
1883 if (ret
== 0 || !gspca_dev
->streaming
|| !gspca_dev
->present
)
1887 gspca_dev
->fr_o
= (i
+ 1) % gspca_dev
->nframes
;
1888 PDEBUG(D_FRAM
, "frame wait q:%d i:%d o:%d",
1893 if (gspca_dev
->sd_desc
->dq_callback
) {
1894 mutex_lock(&gspca_dev
->usb_lock
);
1895 gspca_dev
->usb_err
= 0;
1896 if (gspca_dev
->present
)
1897 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1898 mutex_unlock(&gspca_dev
->usb_lock
);
1904 * dequeue a video buffer
1906 * If nonblock_ing is false, block until a buffer is available.
1908 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1909 struct v4l2_buffer
*v4l2_buf
)
1911 struct gspca_dev
*gspca_dev
= priv
;
1912 struct gspca_frame
*frame
;
1915 PDEBUG(D_FRAM
, "dqbuf");
1916 if (v4l2_buf
->memory
!= gspca_dev
->memory
)
1919 if (!gspca_dev
->present
)
1922 /* if not streaming, be sure the application will not loop forever */
1923 if (!(file
->f_flags
& O_NONBLOCK
)
1924 && !gspca_dev
->streaming
&& gspca_dev
->users
== 1)
1927 /* only the capturing file may dequeue */
1928 if (gspca_dev
->capt_file
!= file
)
1931 /* only one dequeue / read at a time */
1932 if (mutex_lock_interruptible(&gspca_dev
->read_lock
))
1933 return -ERESTARTSYS
;
1935 ret
= frame_wait(gspca_dev
, file
->f_flags
& O_NONBLOCK
);
1938 i
= ret
; /* frame index */
1939 frame
= &gspca_dev
->frame
[i
];
1940 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1941 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1943 frame
->v4l2_buf
.bytesused
)) {
1944 PDEBUG(D_ERR
|D_STREAM
,
1945 "dqbuf cp to user failed");
1950 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1951 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1952 PDEBUG(D_FRAM
, "dqbuf %d", i
);
1955 mutex_unlock(&gspca_dev
->read_lock
);
1960 * queue a video buffer
1962 * Attempting to queue a buffer that has already been
1963 * queued will return -EINVAL.
1965 static int vidioc_qbuf(struct file
*file
, void *priv
,
1966 struct v4l2_buffer
*v4l2_buf
)
1968 struct gspca_dev
*gspca_dev
= priv
;
1969 struct gspca_frame
*frame
;
1972 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1974 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1975 return -ERESTARTSYS
;
1977 index
= v4l2_buf
->index
;
1978 if ((unsigned) index
>= gspca_dev
->nframes
) {
1980 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1984 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1985 PDEBUG(D_FRAM
, "qbuf bad memory type");
1990 frame
= &gspca_dev
->frame
[index
];
1991 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1992 PDEBUG(D_FRAM
, "qbuf bad state");
1997 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1999 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
2000 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
2001 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
2004 /* put the buffer in the 'queued' queue */
2005 i
= gspca_dev
->fr_q
;
2006 gspca_dev
->fr_queue
[i
] = index
;
2007 if (gspca_dev
->fr_i
== i
)
2008 gspca_dev
->cur_frame
= frame
;
2009 gspca_dev
->fr_q
= (i
+ 1) % gspca_dev
->nframes
;
2010 PDEBUG(D_FRAM
, "qbuf q:%d i:%d o:%d",
2015 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
2016 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
2019 mutex_unlock(&gspca_dev
->queue_lock
);
2024 * allocate the resources for read()
2026 static int read_alloc(struct gspca_dev
*gspca_dev
,
2029 struct v4l2_buffer v4l2_buf
;
2032 PDEBUG(D_STREAM
, "read alloc");
2033 if (gspca_dev
->nframes
== 0) {
2034 struct v4l2_requestbuffers rb
;
2036 memset(&rb
, 0, sizeof rb
);
2037 rb
.count
= gspca_dev
->nbufread
;
2038 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2039 rb
.memory
= GSPCA_MEMORY_READ
;
2040 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
2042 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
2045 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2046 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2047 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2048 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
2050 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2052 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
2056 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
2059 /* start streaming */
2060 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2062 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
2066 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
2068 struct gspca_dev
*gspca_dev
= file
->private_data
;
2071 PDEBUG(D_FRAM
, "poll");
2073 poll_wait(file
, &gspca_dev
->wq
, wait
);
2075 /* if reqbufs is not done, the user would use read() */
2076 if (gspca_dev
->nframes
== 0) {
2077 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
)
2078 return POLLERR
; /* not the 1st time */
2079 ret
= read_alloc(gspca_dev
, file
);
2084 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
2087 /* check the next incoming buffer */
2088 i
= gspca_dev
->fr_o
;
2089 i
= gspca_dev
->fr_queue
[i
];
2090 if (gspca_dev
->frame
[i
].v4l2_buf
.flags
& V4L2_BUF_FLAG_DONE
)
2091 ret
= POLLIN
| POLLRDNORM
; /* something to read */
2094 mutex_unlock(&gspca_dev
->queue_lock
);
2095 if (!gspca_dev
->present
)
2100 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
2101 size_t count
, loff_t
*ppos
)
2103 struct gspca_dev
*gspca_dev
= file
->private_data
;
2104 struct gspca_frame
*frame
;
2105 struct v4l2_buffer v4l2_buf
;
2106 struct timeval timestamp
;
2109 PDEBUG(D_FRAM
, "read (%zd)", count
);
2110 if (!gspca_dev
->present
)
2112 switch (gspca_dev
->memory
) {
2113 case GSPCA_MEMORY_NO
: /* first time */
2114 ret
= read_alloc(gspca_dev
, file
);
2118 case GSPCA_MEMORY_READ
:
2119 if (gspca_dev
->capt_file
== file
)
2127 jiffies_to_timeval(get_jiffies_64(), ×tamp
);
2131 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2132 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2133 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2134 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
2136 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
2140 /* if the process slept for more than 1 second,
2141 * get a newer frame */
2142 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
2144 break; /* avoid infinite loop */
2145 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
2147 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2149 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
2154 /* copy the frame */
2155 if (count
> frame
->v4l2_buf
.bytesused
)
2156 count
= frame
->v4l2_buf
.bytesused
;
2157 ret
= copy_to_user(data
, frame
->data
, count
);
2159 PDEBUG(D_ERR
|D_STREAM
,
2160 "read cp to user lack %d / %zd", ret
, count
);
2166 /* in each case, requeue the buffer */
2167 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2173 static struct v4l2_file_operations dev_fops
= {
2174 .owner
= THIS_MODULE
,
2176 .release
= dev_close
,
2179 .unlocked_ioctl
= video_ioctl2
,
2183 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
2184 .vidioc_querycap
= vidioc_querycap
,
2185 .vidioc_dqbuf
= vidioc_dqbuf
,
2186 .vidioc_qbuf
= vidioc_qbuf
,
2187 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2188 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2189 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2190 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2191 .vidioc_streamon
= vidioc_streamon
,
2192 .vidioc_queryctrl
= vidioc_queryctrl
,
2193 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2194 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2195 .vidioc_g_audio
= vidioc_g_audio
,
2196 .vidioc_s_audio
= vidioc_s_audio
,
2197 .vidioc_enumaudio
= vidioc_enumaudio
,
2198 .vidioc_querymenu
= vidioc_querymenu
,
2199 .vidioc_enum_input
= vidioc_enum_input
,
2200 .vidioc_g_input
= vidioc_g_input
,
2201 .vidioc_s_input
= vidioc_s_input
,
2202 .vidioc_reqbufs
= vidioc_reqbufs
,
2203 .vidioc_querybuf
= vidioc_querybuf
,
2204 .vidioc_streamoff
= vidioc_streamoff
,
2205 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
2206 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
2207 .vidioc_g_parm
= vidioc_g_parm
,
2208 .vidioc_s_parm
= vidioc_s_parm
,
2209 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2210 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2211 #ifdef CONFIG_VIDEO_ADV_DEBUG
2212 .vidioc_g_register
= vidioc_g_register
,
2213 .vidioc_s_register
= vidioc_s_register
,
2215 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2216 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2217 .vidiocgmbuf
= vidiocgmbuf
,
2221 static struct video_device gspca_template
= {
2222 .name
= "gspca main driver",
2224 .ioctl_ops
= &dev_ioctl_ops
,
2225 .release
= gspca_release
,
2229 * probe and create a new gspca device
2231 * This function must be called by the sub-driver when it is
2232 * called for probing a new device.
2234 int gspca_dev_probe(struct usb_interface
*intf
,
2235 const struct usb_device_id
*id
,
2236 const struct sd_desc
*sd_desc
,
2238 struct module
*module
)
2240 struct usb_interface_descriptor
*interface
;
2241 struct gspca_dev
*gspca_dev
;
2242 struct usb_device
*dev
= interface_to_usbdev(intf
);
2245 PDEBUG(D_PROBE
, "probing %04x:%04x", id
->idVendor
, id
->idProduct
);
2247 /* we don't handle multi-config cameras */
2248 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2249 PDEBUG(D_ERR
, "Too many config");
2253 /* the USB video interface must be the first one */
2254 interface
= &intf
->cur_altsetting
->desc
;
2255 if (dev
->config
->desc
.bNumInterfaces
!= 1 &&
2256 interface
->bInterfaceNumber
!= 0)
2259 /* create the device */
2260 if (dev_size
< sizeof *gspca_dev
)
2261 dev_size
= sizeof *gspca_dev
;
2262 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2264 err("couldn't kzalloc gspca struct");
2267 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2268 if (!gspca_dev
->usb_buf
) {
2269 err("out of memory");
2273 gspca_dev
->dev
= dev
;
2274 gspca_dev
->iface
= interface
->bInterfaceNumber
;
2275 gspca_dev
->nbalt
= intf
->num_altsetting
;
2276 gspca_dev
->sd_desc
= sd_desc
;
2277 gspca_dev
->nbufread
= 2;
2278 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2280 /* configure the subdriver and initialize the USB device */
2281 ret
= sd_desc
->config(gspca_dev
, id
);
2284 ret
= sd_desc
->init(gspca_dev
);
2287 gspca_set_default_mode(gspca_dev
);
2289 ret
= gspca_input_connect(gspca_dev
);
2293 mutex_init(&gspca_dev
->usb_lock
);
2294 mutex_init(&gspca_dev
->read_lock
);
2295 mutex_init(&gspca_dev
->queue_lock
);
2296 init_waitqueue_head(&gspca_dev
->wq
);
2298 /* init video stuff */
2299 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
2300 gspca_dev
->vdev
.parent
= &intf
->dev
;
2301 gspca_dev
->module
= module
;
2302 gspca_dev
->present
= 1;
2303 ret
= video_register_device(&gspca_dev
->vdev
,
2307 err("video_register_device err %d", ret
);
2311 usb_set_intfdata(intf
, gspca_dev
);
2312 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2314 gspca_input_create_urb(gspca_dev
);
2319 if (gspca_dev
->input_dev
)
2320 input_unregister_device(gspca_dev
->input_dev
);
2322 kfree(gspca_dev
->usb_buf
);
2326 EXPORT_SYMBOL(gspca_dev_probe
);
2331 * This function must be called by the sub-driver
2332 * when the device disconnects, after the specific resources are freed.
2334 void gspca_disconnect(struct usb_interface
*intf
)
2336 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2338 struct input_dev
*input_dev
;
2341 PDEBUG(D_PROBE
, "%s disconnect",
2342 video_device_node_name(&gspca_dev
->vdev
));
2343 mutex_lock(&gspca_dev
->usb_lock
);
2344 gspca_dev
->present
= 0;
2346 if (gspca_dev
->streaming
) {
2347 destroy_urbs(gspca_dev
);
2348 wake_up_interruptible(&gspca_dev
->wq
);
2352 gspca_input_destroy_urb(gspca_dev
);
2353 input_dev
= gspca_dev
->input_dev
;
2355 gspca_dev
->input_dev
= NULL
;
2356 input_unregister_device(input_dev
);
2360 /* the device is freed at exit of this function */
2361 gspca_dev
->dev
= NULL
;
2362 mutex_unlock(&gspca_dev
->usb_lock
);
2364 usb_set_intfdata(intf
, NULL
);
2366 /* release the device */
2367 /* (this will call gspca_release() immediatly or on last close) */
2368 video_unregister_device(&gspca_dev
->vdev
);
2370 /* PDEBUG(D_PROBE, "disconnect complete"); */
2372 EXPORT_SYMBOL(gspca_disconnect
);
2375 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2377 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2379 if (!gspca_dev
->streaming
)
2381 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2382 if (gspca_dev
->sd_desc
->stopN
)
2383 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2384 destroy_urbs(gspca_dev
);
2385 gspca_input_destroy_urb(gspca_dev
);
2386 gspca_set_alt0(gspca_dev
);
2387 if (gspca_dev
->sd_desc
->stop0
)
2388 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2391 EXPORT_SYMBOL(gspca_suspend
);
2393 int gspca_resume(struct usb_interface
*intf
)
2395 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2397 gspca_dev
->frozen
= 0;
2398 gspca_dev
->sd_desc
->init(gspca_dev
);
2399 gspca_input_create_urb(gspca_dev
);
2400 if (gspca_dev
->streaming
)
2401 return gspca_init_transfer(gspca_dev
);
2404 EXPORT_SYMBOL(gspca_resume
);
2406 /* -- cam driver utility functions -- */
2408 /* auto gain and exposure algorithm based on the knee algorithm described here:
2409 http://ytse.tricolour.net/docs/LowLightOptimization.html
2411 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2413 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2414 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2416 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2417 const struct ctrl
*gain_ctrl
= NULL
;
2418 const struct ctrl
*exposure_ctrl
= NULL
;
2419 const struct ctrl
*autogain_ctrl
= NULL
;
2422 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2423 if (gspca_dev
->ctrl_dis
& (1 << i
))
2425 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2426 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2427 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2428 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2429 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2430 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2432 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2433 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2434 "on cam without (auto)gain/exposure");
2438 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2439 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2440 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2444 orig_exposure
= exposure
;
2446 /* If we are of a multiple of deadzone, do multiple steps to reach the
2447 desired lumination fast (with the risc of a slight overshoot) */
2448 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2450 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2451 avg_lum
, desired_avg_lum
, steps
);
2453 for (i
= 0; i
< steps
; i
++) {
2454 if (avg_lum
> desired_avg_lum
) {
2455 if (gain
> gain_knee
)
2457 else if (exposure
> exposure_knee
)
2459 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2461 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2463 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2468 if (gain
< gain_ctrl
->qctrl
.default_value
)
2470 else if (exposure
< exposure_knee
)
2472 else if (gain
< gain_knee
)
2474 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2476 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2483 if (gain
!= orig_gain
) {
2484 gain_ctrl
->set(gspca_dev
, gain
);
2487 if (exposure
!= orig_exposure
) {
2488 exposure_ctrl
->set(gspca_dev
, exposure
);
2494 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2496 /* -- module insert / remove -- */
2497 static int __init
gspca_init(void)
2499 info("main v%d.%d.%d registered",
2500 (DRIVER_VERSION_NUMBER
>> 16) & 0xff,
2501 (DRIVER_VERSION_NUMBER
>> 8) & 0xff,
2502 DRIVER_VERSION_NUMBER
& 0xff);
2505 static void __exit
gspca_exit(void)
2507 info("main deregistered");
2510 module_init(gspca_init
);
2511 module_exit(gspca_exit
);
2514 module_param_named(debug
, gspca_debug
, int, 0644);
2515 MODULE_PARM_DESC(debug
,
2516 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2517 " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"