2 * Main USB camera driver
4 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
6 * Camera button input handling by Márton Németh
7 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define GSPCA_VERSION "2.14.0"
28 #include <linux/init.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
38 #include <linux/uaccess.h>
39 #include <linux/ktime.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-fh.h>
43 #include <media/v4l2-event.h>
47 #if IS_ENABLED(CONFIG_INPUT)
48 #include <linux/input.h>
49 #include <linux/usb/input.h>
53 #define DEF_NURBS 3 /* default number of URBs */
54 #if DEF_NURBS > MAX_NURBS
55 #error "DEF_NURBS too big"
58 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
59 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(GSPCA_VERSION
);
64 EXPORT_SYMBOL(gspca_debug
);
66 static void PDEBUG_MODE(struct gspca_dev
*gspca_dev
, int debug
, char *txt
,
67 __u32 pixfmt
, int w
, int h
)
69 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
70 PDEBUG(debug
, "%s %c%c%c%c %dx%d",
74 (pixfmt
>> 16) & 0xff,
78 PDEBUG(debug
, "%s 0x%08x %dx%d",
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 IS_ENABLED(CONFIG_INPUT)
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 PERR("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 PERR("URB error %i, resubmitting", urb
->status
);
149 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
151 pr_err("Resubmit URB failed with error %i\n", 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 pr_err("Input device registration failed with error %i\n",
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, buffer_len=%u, interval=%u",
205 ep
->bEndpointAddress
, buffer_len
, interval
);
207 dev
= gspca_dev
->dev
;
209 urb
= usb_alloc_urb(0, GFP_KERNEL
);
215 buffer
= usb_alloc_coherent(dev
, buffer_len
,
216 GFP_KERNEL
, &urb
->transfer_dma
);
221 usb_fill_int_urb(urb
, dev
,
222 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
224 int_irq
, (void *)gspca_dev
, interval
);
225 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
226 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
228 PERR("submit int URB failed with error %i", ret
);
231 gspca_dev
->int_urb
= urb
;
235 usb_free_coherent(dev
,
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_free_coherent(gspca_dev
->dev
,
276 urb
->transfer_buffer_length
,
277 urb
->transfer_buffer
,
283 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
287 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
291 static inline int gspca_input_connect(struct gspca_dev
*dev
)
298 * fill a video frame from an URB and resubmit
300 static void fill_frame(struct gspca_dev
*gspca_dev
,
303 u8
*data
; /* address of data in the iso message */
307 if (urb
->status
!= 0) {
308 if (urb
->status
== -ESHUTDOWN
)
309 return; /* disconnection */
311 if (gspca_dev
->frozen
)
314 PERR("urb status: %d", urb
->status
);
318 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
319 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
320 len
= urb
->iso_frame_desc
[i
].actual_length
;
322 /* check the packet status and length */
323 st
= urb
->iso_frame_desc
[i
].status
;
325 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
327 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
331 if (gspca_dev
->empty_packet
== 0)
332 gspca_dev
->empty_packet
= 1;
336 /* let the packet be analyzed by the subdriver */
337 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
338 i
, urb
->iso_frame_desc
[i
].offset
, len
);
339 data
= (u8
*) urb
->transfer_buffer
340 + urb
->iso_frame_desc
[i
].offset
;
341 pkt_scan(gspca_dev
, data
, len
);
345 /* resubmit the URB */
346 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
348 pr_err("usb_submit_urb() ret %d\n", st
);
352 * ISOC message interrupt from the USB device
354 * Analyse each packet and call the subdriver for copy to the frame buffer.
356 static void isoc_irq(struct urb
*urb
)
358 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
360 PDEBUG(D_PACK
, "isoc irq");
361 if (!gspca_dev
->streaming
)
363 fill_frame(gspca_dev
, urb
);
367 * bulk message interrupt from the USB device
369 static void bulk_irq(struct urb
*urb
)
371 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
374 PDEBUG(D_PACK
, "bulk irq");
375 if (!gspca_dev
->streaming
)
377 switch (urb
->status
) {
381 return; /* disconnection */
384 if (gspca_dev
->frozen
)
387 PERR("urb status: %d", urb
->status
);
392 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
393 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
394 urb
->transfer_buffer
,
398 /* resubmit the URB */
399 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
400 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
402 pr_err("usb_submit_urb() ret %d\n", st
);
407 * add data to the current frame
409 * This function is called by the subdrivers at interrupt level.
411 * To build a frame, these ones must add
413 * - 0 or many INTER_PACKETs
415 * DISCARD_PACKET invalidates the whole frame.
417 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
418 enum gspca_packet_type packet_type
,
422 struct gspca_frame
*frame
;
425 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
427 if (packet_type
== FIRST_PACKET
) {
428 i
= atomic_read(&gspca_dev
->fr_i
);
430 /* if there are no queued buffer, discard the whole frame */
431 if (i
== atomic_read(&gspca_dev
->fr_q
)) {
432 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
433 gspca_dev
->sequence
++;
436 j
= gspca_dev
->fr_queue
[i
];
437 frame
= &gspca_dev
->frame
[j
];
438 v4l2_get_timestamp(&frame
->v4l2_buf
.timestamp
);
439 frame
->v4l2_buf
.sequence
= gspca_dev
->sequence
++;
440 gspca_dev
->image
= frame
->data
;
441 gspca_dev
->image_len
= 0;
443 switch (gspca_dev
->last_packet_type
) {
445 if (packet_type
== LAST_PACKET
) {
446 gspca_dev
->last_packet_type
= packet_type
;
447 gspca_dev
->image
= NULL
;
448 gspca_dev
->image_len
= 0;
456 /* append the packet to the frame buffer */
458 if (gspca_dev
->image_len
+ len
> gspca_dev
->frsz
) {
459 PERR("frame overflow %d > %d",
460 gspca_dev
->image_len
+ len
,
462 packet_type
= DISCARD_PACKET
;
464 /* !! image is NULL only when last pkt is LAST or DISCARD
465 if (gspca_dev->image == NULL) {
466 pr_err("gspca_frame_add() image == NULL\n");
470 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
472 gspca_dev
->image_len
+= len
;
475 gspca_dev
->last_packet_type
= packet_type
;
477 /* if last packet, invalidate packet concatenation until
478 * next first packet, wake up the application and advance
480 if (packet_type
== LAST_PACKET
) {
481 i
= atomic_read(&gspca_dev
->fr_i
);
482 j
= gspca_dev
->fr_queue
[i
];
483 frame
= &gspca_dev
->frame
[j
];
484 frame
->v4l2_buf
.bytesused
= gspca_dev
->image_len
;
485 frame
->v4l2_buf
.flags
= (frame
->v4l2_buf
.flags
486 | V4L2_BUF_FLAG_DONE
)
487 & ~V4L2_BUF_FLAG_QUEUED
;
488 i
= (i
+ 1) % GSPCA_MAX_FRAMES
;
489 atomic_set(&gspca_dev
->fr_i
, i
);
490 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
491 PDEBUG(D_FRAM
, "frame complete len:%d",
492 frame
->v4l2_buf
.bytesused
);
493 gspca_dev
->image
= NULL
;
494 gspca_dev
->image_len
= 0;
497 EXPORT_SYMBOL(gspca_frame_add
);
499 static int frame_alloc(struct gspca_dev
*gspca_dev
, struct file
*file
,
500 enum v4l2_memory memory
, unsigned int count
)
502 struct gspca_frame
*frame
;
506 frsz
= gspca_dev
->pixfmt
.sizeimage
;
507 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
508 frsz
= PAGE_ALIGN(frsz
);
509 if (count
>= GSPCA_MAX_FRAMES
)
510 count
= GSPCA_MAX_FRAMES
- 1;
511 gspca_dev
->frbuf
= vmalloc_32(frsz
* count
);
512 if (!gspca_dev
->frbuf
) {
513 pr_err("frame alloc failed\n");
516 gspca_dev
->capt_file
= file
;
517 gspca_dev
->memory
= memory
;
518 gspca_dev
->frsz
= frsz
;
519 gspca_dev
->nframes
= count
;
520 for (i
= 0; i
< count
; i
++) {
521 frame
= &gspca_dev
->frame
[i
];
522 frame
->v4l2_buf
.index
= i
;
523 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
524 frame
->v4l2_buf
.flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
525 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
526 frame
->v4l2_buf
.length
= frsz
;
527 frame
->v4l2_buf
.memory
= memory
;
528 frame
->v4l2_buf
.sequence
= 0;
529 frame
->data
= gspca_dev
->frbuf
+ i
* frsz
;
530 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
532 atomic_set(&gspca_dev
->fr_q
, 0);
533 atomic_set(&gspca_dev
->fr_i
, 0);
538 static void frame_free(struct gspca_dev
*gspca_dev
)
542 PDEBUG(D_STREAM
, "frame free");
543 if (gspca_dev
->frbuf
!= NULL
) {
544 vfree(gspca_dev
->frbuf
);
545 gspca_dev
->frbuf
= NULL
;
546 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
547 gspca_dev
->frame
[i
].data
= NULL
;
549 gspca_dev
->nframes
= 0;
551 gspca_dev
->capt_file
= NULL
;
552 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
555 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
560 PDEBUG(D_STREAM
, "kill transfer");
561 for (i
= 0; i
< MAX_NURBS
; i
++) {
562 urb
= gspca_dev
->urb
[i
];
566 gspca_dev
->urb
[i
] = NULL
;
568 usb_free_coherent(gspca_dev
->dev
,
569 urb
->transfer_buffer_length
,
570 urb
->transfer_buffer
,
576 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
580 if (gspca_dev
->alt
== 0)
582 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
584 pr_err("set alt 0 err %d\n", ret
);
588 /* Note: both the queue and the usb locks should be held when calling this */
589 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
591 gspca_dev
->streaming
= 0;
592 gspca_dev
->usb_err
= 0;
593 if (gspca_dev
->sd_desc
->stopN
)
594 gspca_dev
->sd_desc
->stopN(gspca_dev
);
595 destroy_urbs(gspca_dev
);
596 gspca_input_destroy_urb(gspca_dev
);
597 gspca_set_alt0(gspca_dev
);
598 gspca_input_create_urb(gspca_dev
);
599 if (gspca_dev
->sd_desc
->stop0
)
600 gspca_dev
->sd_desc
->stop0(gspca_dev
);
601 PDEBUG(D_STREAM
, "stream off OK");
605 * look for an input transfer endpoint in an alternate setting.
607 * If xfer_ep is invalid, return the first valid ep found, otherwise
608 * look for exactly the ep with address equal to xfer_ep.
610 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
611 int xfer
, int xfer_ep
)
613 struct usb_host_endpoint
*ep
;
616 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
617 ep
= &alt
->endpoint
[i
];
618 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
620 && ep
->desc
.wMaxPacketSize
!= 0
621 && usb_endpoint_dir_in(&ep
->desc
)
622 && (xfer_ep
< 0 || ep
->desc
.bEndpointAddress
== xfer_ep
))
628 /* compute the minimum bandwidth for the current transfer */
629 static u32
which_bandwidth(struct gspca_dev
*gspca_dev
)
633 /* get the (max) image size */
634 bandwidth
= gspca_dev
->pixfmt
.sizeimage
;
636 /* if the image is compressed, estimate its mean size */
637 if (!gspca_dev
->cam
.needs_full_bandwidth
&&
638 bandwidth
< gspca_dev
->pixfmt
.width
*
639 gspca_dev
->pixfmt
.height
)
640 bandwidth
= bandwidth
* 3 / 8; /* 0.375 */
642 /* estimate the frame rate */
643 if (gspca_dev
->sd_desc
->get_streamparm
) {
644 struct v4l2_streamparm parm
;
646 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, &parm
);
647 bandwidth
*= parm
.parm
.capture
.timeperframe
.denominator
;
648 bandwidth
/= parm
.parm
.capture
.timeperframe
.numerator
;
651 /* don't hope more than 15 fps with USB 1.1 and
652 * image resolution >= 640x480 */
653 if (gspca_dev
->pixfmt
.width
>= 640
654 && gspca_dev
->dev
->speed
== USB_SPEED_FULL
)
655 bandwidth
*= 15; /* 15 fps */
657 bandwidth
*= 30; /* 30 fps */
660 PDEBUG(D_STREAM
, "min bandwidth: %d", bandwidth
);
672 * build the table of the endpoints
673 * and compute the minimum bandwidth for the image transfer
675 static int build_isoc_ep_tb(struct gspca_dev
*gspca_dev
,
676 struct usb_interface
*intf
,
677 struct ep_tb_s
*ep_tb
)
679 struct usb_host_endpoint
*ep
;
680 int i
, j
, nbalt
, psize
, found
;
681 u32 bandwidth
, last_bw
;
683 nbalt
= intf
->num_altsetting
;
685 nbalt
= MAX_ALT
; /* fixme: should warn */
687 /* build the endpoint table */
691 ep_tb
->bandwidth
= 2000 * 2000 * 120;
693 for (j
= 0; j
< nbalt
; j
++) {
694 ep
= alt_xfer(&intf
->altsetting
[j
],
695 USB_ENDPOINT_XFER_ISOC
,
699 if (ep
->desc
.bInterval
== 0) {
700 pr_err("alt %d iso endp with 0 interval\n", j
);
703 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
704 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
705 bandwidth
= psize
* 1000;
706 if (gspca_dev
->dev
->speed
== USB_SPEED_HIGH
707 || gspca_dev
->dev
->speed
>= USB_SPEED_SUPER
)
709 bandwidth
/= 1 << (ep
->desc
.bInterval
- 1);
710 if (bandwidth
<= last_bw
)
712 if (bandwidth
< ep_tb
->bandwidth
) {
713 ep_tb
->bandwidth
= bandwidth
;
720 PDEBUG(D_STREAM
, "alt %d bandwidth %d",
721 ep_tb
->alt
, ep_tb
->bandwidth
);
722 last_bw
= ep_tb
->bandwidth
;
729 * has a usb audio class interface (a built in usb mic); and
730 * is a usb 1 full speed device; and
731 * uses the max full speed iso bandwidth; and
732 * and has more than 1 alt setting
733 * then skip the highest alt setting to spare bandwidth for the mic
735 if (gspca_dev
->audio
&&
736 gspca_dev
->dev
->speed
== USB_SPEED_FULL
&&
737 last_bw
>= 1000000 &&
739 PDEBUG(D_STREAM
, "dev has usb audio, skipping highest alt");
744 /* get the requested bandwidth and start at the highest atlsetting */
745 bandwidth
= which_bandwidth(gspca_dev
);
749 if (ep_tb
->bandwidth
< bandwidth
)
757 * create the URBs for image transfer
759 static int create_urbs(struct gspca_dev
*gspca_dev
,
760 struct usb_host_endpoint
*ep
)
763 int n
, nurbs
, i
, psize
, npkt
, bsize
;
765 /* calculate the packet size and the number of packets */
766 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
768 if (!gspca_dev
->cam
.bulk
) { /* isoc */
770 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
771 if (gspca_dev
->pkt_size
== 0)
772 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
774 psize
= gspca_dev
->pkt_size
;
775 npkt
= gspca_dev
->cam
.npkt
;
777 npkt
= 32; /* default value */
778 bsize
= psize
* npkt
;
780 "isoc %d pkts size %d = bsize:%d",
785 bsize
= gspca_dev
->cam
.bulk_size
;
788 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
789 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
790 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
795 for (n
= 0; n
< nurbs
; n
++) {
796 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
799 gspca_dev
->urb
[n
] = urb
;
800 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
805 if (urb
->transfer_buffer
== NULL
) {
806 pr_err("usb_alloc_coherent failed\n");
809 urb
->dev
= gspca_dev
->dev
;
810 urb
->context
= gspca_dev
;
811 urb
->transfer_buffer_length
= bsize
;
812 if (npkt
!= 0) { /* ISOC */
813 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
814 ep
->desc
.bEndpointAddress
);
815 urb
->transfer_flags
= URB_ISO_ASAP
816 | URB_NO_TRANSFER_DMA_MAP
;
817 urb
->interval
= 1 << (ep
->desc
.bInterval
- 1);
818 urb
->complete
= isoc_irq
;
819 urb
->number_of_packets
= npkt
;
820 for (i
= 0; i
< npkt
; i
++) {
821 urb
->iso_frame_desc
[i
].length
= psize
;
822 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
825 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
826 ep
->desc
.bEndpointAddress
);
827 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
828 urb
->complete
= bulk_irq
;
835 * start the USB transfer
837 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
839 struct usb_interface
*intf
;
840 struct usb_host_endpoint
*ep
;
842 struct ep_tb_s ep_tb
[MAX_ALT
];
843 int n
, ret
, xfer
, alt
, alt_idx
;
845 /* reset the streaming variables */
846 gspca_dev
->image
= NULL
;
847 gspca_dev
->image_len
= 0;
848 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
849 gspca_dev
->sequence
= 0;
851 gspca_dev
->usb_err
= 0;
853 /* do the specific subdriver stuff before endpoint selection */
854 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
855 gspca_dev
->alt
= gspca_dev
->cam
.bulk
? intf
->num_altsetting
: 0;
856 if (gspca_dev
->sd_desc
->isoc_init
) {
857 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
861 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
862 : USB_ENDPOINT_XFER_ISOC
;
864 /* if bulk or the subdriver forced an altsetting, get the endpoint */
865 if (gspca_dev
->alt
!= 0) {
866 gspca_dev
->alt
--; /* (previous version compatibility) */
867 ep
= alt_xfer(&intf
->altsetting
[gspca_dev
->alt
], xfer
,
870 pr_err("bad altsetting %d\n", gspca_dev
->alt
);
873 ep_tb
[0].alt
= gspca_dev
->alt
;
876 /* else, compute the minimum bandwidth
877 * and build the endpoint table */
878 alt_idx
= build_isoc_ep_tb(gspca_dev
, intf
, ep_tb
);
880 pr_err("no transfer endpoint found\n");
885 /* set the highest alternate setting and
886 * loop until urb submit succeeds */
887 gspca_input_destroy_urb(gspca_dev
);
889 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
892 if (alt
!= gspca_dev
->alt
) {
893 alt
= gspca_dev
->alt
;
894 if (intf
->num_altsetting
> 1) {
895 ret
= usb_set_interface(gspca_dev
->dev
,
900 goto retry
; /*fixme: ugly*/
901 pr_err("set alt %d err %d\n", alt
, ret
);
906 if (!gspca_dev
->cam
.no_urb_create
) {
907 PDEBUG(D_STREAM
, "init transfer alt %d", alt
);
908 ret
= create_urbs(gspca_dev
,
909 alt_xfer(&intf
->altsetting
[alt
], xfer
,
910 gspca_dev
->xfer_ep
));
912 destroy_urbs(gspca_dev
);
917 /* clear the bulk endpoint */
918 if (gspca_dev
->cam
.bulk
)
919 usb_clear_halt(gspca_dev
->dev
,
920 gspca_dev
->urb
[0]->pipe
);
923 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
925 destroy_urbs(gspca_dev
);
928 gspca_dev
->streaming
= 1;
929 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
931 /* some bulk transfers are started by the subdriver */
932 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
935 /* submit the URBs */
936 for (n
= 0; n
< MAX_NURBS
; n
++) {
937 urb
= gspca_dev
->urb
[n
];
940 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
945 break; /* transfer is started */
947 /* something when wrong
948 * stop the webcam and free the transfer resources */
949 gspca_stream_off(gspca_dev
);
950 if (ret
!= -ENOSPC
) {
951 pr_err("usb_submit_urb alt %d err %d\n",
952 gspca_dev
->alt
, ret
);
956 /* the bandwidth is not wide enough
957 * negotiate or try a lower alternate setting */
959 PERR("alt %d - bandwidth not wide enough, trying again", alt
);
960 msleep(20); /* wait for kill complete */
961 if (gspca_dev
->sd_desc
->isoc_nego
) {
962 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
967 pr_err("no transfer endpoint found\n");
971 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
975 gspca_input_create_urb(gspca_dev
);
979 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
983 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
984 gspca_dev
->curr_mode
= i
;
985 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
];
987 /* does nothing if ctrl_handler == NULL */
988 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
991 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
992 int width
, int height
)
996 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
997 if (width
== gspca_dev
->cam
.cam_mode
[i
].width
998 && height
== gspca_dev
->cam
.cam_mode
[i
].height
)
1004 static int wxh_to_nearest_mode(struct gspca_dev
*gspca_dev
,
1005 int width
, int height
)
1009 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
1010 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
1011 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
1018 * search a mode with the right pixel format
1020 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
1026 modeU
= modeD
= mode
;
1027 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
1029 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
1033 if (++modeU
< gspca_dev
->cam
.nmodes
) {
1034 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
1042 #ifdef CONFIG_VIDEO_ADV_DEBUG
1043 static int vidioc_g_chip_info(struct file
*file
, void *priv
,
1044 struct v4l2_dbg_chip_info
*chip
)
1046 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1048 gspca_dev
->usb_err
= 0;
1049 if (gspca_dev
->sd_desc
->get_chip_info
)
1050 return gspca_dev
->sd_desc
->get_chip_info(gspca_dev
, chip
);
1051 return chip
->match
.addr
? -EINVAL
: 0;
1054 static int vidioc_g_register(struct file
*file
, void *priv
,
1055 struct v4l2_dbg_register
*reg
)
1057 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1059 gspca_dev
->usb_err
= 0;
1060 return gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
1063 static int vidioc_s_register(struct file
*file
, void *priv
,
1064 const struct v4l2_dbg_register
*reg
)
1066 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1068 gspca_dev
->usb_err
= 0;
1069 return gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
1073 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1074 struct v4l2_fmtdesc
*fmtdesc
)
1076 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1080 /* give an index to each format */
1083 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1084 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1087 if (fmt_tb
[j
] == fmt_tb
[index
])
1092 if (fmtdesc
->index
== index
)
1093 break; /* new format */
1095 if (index
>= ARRAY_SIZE(fmt_tb
))
1100 return -EINVAL
; /* no more format */
1102 fmtdesc
->pixelformat
= fmt_tb
[index
];
1103 if (gspca_dev
->cam
.cam_mode
[i
].sizeimage
<
1104 gspca_dev
->cam
.cam_mode
[i
].width
*
1105 gspca_dev
->cam
.cam_mode
[i
].height
)
1106 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1107 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1108 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1109 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1110 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1111 fmtdesc
->description
[4] = '\0';
1115 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1116 struct v4l2_format
*fmt
)
1118 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1120 fmt
->fmt
.pix
= gspca_dev
->pixfmt
;
1121 /* some drivers use priv internally, zero it before giving it back to
1123 fmt
->fmt
.pix
.priv
= 0;
1127 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1128 struct v4l2_format
*fmt
)
1130 int w
, h
, mode
, mode2
;
1132 w
= fmt
->fmt
.pix
.width
;
1133 h
= fmt
->fmt
.pix
.height
;
1135 PDEBUG_MODE(gspca_dev
, D_CONF
, "try fmt cap",
1136 fmt
->fmt
.pix
.pixelformat
, w
, h
);
1138 /* search the nearest mode for width and height */
1139 mode
= wxh_to_nearest_mode(gspca_dev
, w
, h
);
1141 /* OK if right palette */
1142 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1143 != fmt
->fmt
.pix
.pixelformat
) {
1145 /* else, search the closest mode with the same pixel format */
1146 mode2
= gspca_get_mode(gspca_dev
, mode
,
1147 fmt
->fmt
.pix
.pixelformat
);
1151 fmt
->fmt
.pix
= gspca_dev
->cam
.cam_mode
[mode
];
1152 if (gspca_dev
->sd_desc
->try_fmt
) {
1153 /* pass original resolution to subdriver try_fmt */
1154 fmt
->fmt
.pix
.width
= w
;
1155 fmt
->fmt
.pix
.height
= h
;
1156 gspca_dev
->sd_desc
->try_fmt(gspca_dev
, fmt
);
1158 /* some drivers use priv internally, zero it before giving it back to
1160 fmt
->fmt
.pix
.priv
= 0;
1161 return mode
; /* used when s_fmt */
1164 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1166 struct v4l2_format
*fmt
)
1168 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1171 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1177 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1178 struct v4l2_format
*fmt
)
1180 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1183 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1184 return -ERESTARTSYS
;
1186 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1190 if (gspca_dev
->nframes
!= 0
1191 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1196 if (gspca_dev
->streaming
) {
1200 gspca_dev
->curr_mode
= ret
;
1201 if (gspca_dev
->sd_desc
->try_fmt
)
1202 /* subdriver try_fmt can modify format parameters */
1203 gspca_dev
->pixfmt
= fmt
->fmt
.pix
;
1205 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[ret
];
1209 mutex_unlock(&gspca_dev
->queue_lock
);
1213 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1214 struct v4l2_frmsizeenum
*fsize
)
1216 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1220 if (gspca_dev
->sd_desc
->enum_framesizes
)
1221 return gspca_dev
->sd_desc
->enum_framesizes(gspca_dev
, fsize
);
1223 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1224 if (fsize
->pixel_format
!=
1225 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1228 if (fsize
->index
== index
) {
1229 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1230 fsize
->discrete
.width
=
1231 gspca_dev
->cam
.cam_mode
[i
].width
;
1232 fsize
->discrete
.height
=
1233 gspca_dev
->cam
.cam_mode
[i
].height
;
1242 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1243 struct v4l2_frmivalenum
*fival
)
1245 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1249 mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1253 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1254 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1257 if (fival
->pixel_format
!=
1258 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1261 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1262 if (fival
->index
== i
) {
1263 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1264 fival
->discrete
.numerator
= 1;
1265 fival
->discrete
.denominator
=
1266 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1274 static void gspca_release(struct v4l2_device
*v4l2_device
)
1276 struct gspca_dev
*gspca_dev
=
1277 container_of(v4l2_device
, struct gspca_dev
, v4l2_dev
);
1279 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
1280 v4l2_device_unregister(&gspca_dev
->v4l2_dev
);
1281 kfree(gspca_dev
->usb_buf
);
1285 static int dev_open(struct file
*file
)
1287 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1290 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1292 /* protect the subdriver against rmmod */
1293 if (!try_module_get(gspca_dev
->module
))
1296 ret
= v4l2_fh_open(file
);
1298 module_put(gspca_dev
->module
);
1302 static int dev_close(struct file
*file
)
1304 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1306 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1308 /* Needed for gspca_stream_off, always lock before queue_lock! */
1309 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1310 return -ERESTARTSYS
;
1312 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
)) {
1313 mutex_unlock(&gspca_dev
->usb_lock
);
1314 return -ERESTARTSYS
;
1317 /* if the file did the capture, free the streaming resources */
1318 if (gspca_dev
->capt_file
== file
) {
1319 if (gspca_dev
->streaming
)
1320 gspca_stream_off(gspca_dev
);
1321 frame_free(gspca_dev
);
1323 module_put(gspca_dev
->module
);
1324 mutex_unlock(&gspca_dev
->queue_lock
);
1325 mutex_unlock(&gspca_dev
->usb_lock
);
1327 PDEBUG(D_STREAM
, "close done");
1329 return v4l2_fh_release(file
);
1332 static int vidioc_querycap(struct file
*file
, void *priv
,
1333 struct v4l2_capability
*cap
)
1335 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1337 strlcpy((char *) cap
->driver
, gspca_dev
->sd_desc
->name
,
1338 sizeof cap
->driver
);
1339 if (gspca_dev
->dev
->product
!= NULL
) {
1340 strlcpy((char *) cap
->card
, gspca_dev
->dev
->product
,
1343 snprintf((char *) cap
->card
, sizeof cap
->card
,
1344 "USB Camera (%04x:%04x)",
1345 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1346 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1348 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1349 sizeof(cap
->bus_info
));
1350 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
1351 | V4L2_CAP_STREAMING
1352 | V4L2_CAP_READWRITE
;
1353 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1357 static int vidioc_enum_input(struct file
*file
, void *priv
,
1358 struct v4l2_input
*input
)
1360 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1362 if (input
->index
!= 0)
1364 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1365 input
->status
= gspca_dev
->cam
.input_flags
;
1366 strlcpy(input
->name
, gspca_dev
->sd_desc
->name
,
1367 sizeof input
->name
);
1371 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1377 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1384 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1385 struct v4l2_requestbuffers
*rb
)
1387 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1388 int i
, ret
= 0, streaming
;
1390 i
= rb
->memory
; /* (avoid compilation warning) */
1392 case GSPCA_MEMORY_READ
: /* (internal call) */
1393 case V4L2_MEMORY_MMAP
:
1394 case V4L2_MEMORY_USERPTR
:
1399 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1400 return -ERESTARTSYS
;
1402 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1403 && gspca_dev
->memory
!= GSPCA_MEMORY_READ
1404 && gspca_dev
->memory
!= rb
->memory
) {
1409 /* only one file may do the capture */
1410 if (gspca_dev
->capt_file
!= NULL
1411 && gspca_dev
->capt_file
!= file
) {
1416 /* if allocated, the buffers must not be mapped */
1417 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1418 if (gspca_dev
->frame
[i
].vma_use_count
) {
1424 /* stop streaming */
1425 streaming
= gspca_dev
->streaming
;
1427 gspca_stream_off(gspca_dev
);
1429 /* Don't restart the stream when switching from read
1431 if (gspca_dev
->memory
== GSPCA_MEMORY_READ
)
1435 /* free the previous allocated buffers, if any */
1436 if (gspca_dev
->nframes
!= 0)
1437 frame_free(gspca_dev
);
1438 if (rb
->count
== 0) /* unrequest */
1440 ret
= frame_alloc(gspca_dev
, file
, rb
->memory
, rb
->count
);
1442 rb
->count
= gspca_dev
->nframes
;
1444 ret
= gspca_init_transfer(gspca_dev
);
1447 mutex_unlock(&gspca_dev
->queue_lock
);
1448 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1452 static int vidioc_querybuf(struct file
*file
, void *priv
,
1453 struct v4l2_buffer
*v4l2_buf
)
1455 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1456 struct gspca_frame
*frame
;
1458 if (v4l2_buf
->index
>= gspca_dev
->nframes
)
1461 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1462 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1466 static int vidioc_streamon(struct file
*file
, void *priv
,
1467 enum v4l2_buf_type buf_type
)
1469 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1472 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1474 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1475 return -ERESTARTSYS
;
1477 /* check the capture file */
1478 if (gspca_dev
->capt_file
!= file
) {
1483 if (gspca_dev
->nframes
== 0
1484 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1488 if (!gspca_dev
->streaming
) {
1489 ret
= gspca_init_transfer(gspca_dev
);
1493 PDEBUG_MODE(gspca_dev
, D_STREAM
, "stream on OK",
1494 gspca_dev
->pixfmt
.pixelformat
,
1495 gspca_dev
->pixfmt
.width
, gspca_dev
->pixfmt
.height
);
1498 mutex_unlock(&gspca_dev
->queue_lock
);
1502 static int vidioc_streamoff(struct file
*file
, void *priv
,
1503 enum v4l2_buf_type buf_type
)
1505 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1508 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1511 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1512 return -ERESTARTSYS
;
1514 if (!gspca_dev
->streaming
) {
1519 /* check the capture file */
1520 if (gspca_dev
->capt_file
!= file
) {
1525 /* stop streaming */
1526 gspca_stream_off(gspca_dev
);
1527 /* In case another thread is waiting in dqbuf */
1528 wake_up_interruptible(&gspca_dev
->wq
);
1530 /* empty the transfer queues */
1531 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1532 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1533 atomic_set(&gspca_dev
->fr_q
, 0);
1534 atomic_set(&gspca_dev
->fr_i
, 0);
1535 gspca_dev
->fr_o
= 0;
1538 mutex_unlock(&gspca_dev
->queue_lock
);
1542 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1543 struct v4l2_jpegcompression
*jpegcomp
)
1545 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1547 gspca_dev
->usb_err
= 0;
1548 return gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1551 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1552 const struct v4l2_jpegcompression
*jpegcomp
)
1554 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1556 gspca_dev
->usb_err
= 0;
1557 return gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1560 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1561 struct v4l2_streamparm
*parm
)
1563 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1565 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1567 if (gspca_dev
->sd_desc
->get_streamparm
) {
1568 gspca_dev
->usb_err
= 0;
1569 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1570 return gspca_dev
->usb_err
;
1575 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1576 struct v4l2_streamparm
*parm
)
1578 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1581 n
= parm
->parm
.capture
.readbuffers
;
1582 if (n
== 0 || n
>= GSPCA_MAX_FRAMES
)
1583 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1585 gspca_dev
->nbufread
= n
;
1587 if (gspca_dev
->sd_desc
->set_streamparm
) {
1588 gspca_dev
->usb_err
= 0;
1589 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1590 return gspca_dev
->usb_err
;
1596 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1598 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1599 struct gspca_frame
*frame
;
1601 unsigned long addr
, start
, size
;
1604 start
= vma
->vm_start
;
1605 size
= vma
->vm_end
- vma
->vm_start
;
1606 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1608 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1609 return -ERESTARTSYS
;
1610 if (gspca_dev
->capt_file
!= file
) {
1616 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1617 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1618 PDEBUG(D_STREAM
, "mmap bad memory type");
1621 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1623 frame
= &gspca_dev
->frame
[i
];
1627 if (frame
== NULL
) {
1628 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1632 if (size
!= frame
->v4l2_buf
.length
) {
1633 PDEBUG(D_STREAM
, "mmap bad size");
1639 * - VM_IO marks the area as being a mmaped region for I/O to a
1640 * device. It also prevents the region from being core dumped.
1642 vma
->vm_flags
|= VM_IO
;
1644 addr
= (unsigned long) frame
->data
;
1646 page
= vmalloc_to_page((void *) addr
);
1647 ret
= vm_insert_page(vma
, start
, page
);
1655 vma
->vm_ops
= &gspca_vm_ops
;
1656 vma
->vm_private_data
= frame
;
1660 mutex_unlock(&gspca_dev
->queue_lock
);
1664 static int frame_ready_nolock(struct gspca_dev
*gspca_dev
, struct file
*file
,
1665 enum v4l2_memory memory
)
1667 if (!gspca_dev
->present
)
1669 if (gspca_dev
->capt_file
!= file
|| gspca_dev
->memory
!= memory
||
1670 !gspca_dev
->streaming
)
1673 /* check if a frame is ready */
1674 return gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
);
1677 static int frame_ready(struct gspca_dev
*gspca_dev
, struct file
*file
,
1678 enum v4l2_memory memory
)
1682 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1683 return -ERESTARTSYS
;
1684 ret
= frame_ready_nolock(gspca_dev
, file
, memory
);
1685 mutex_unlock(&gspca_dev
->queue_lock
);
1690 * dequeue a video buffer
1692 * If nonblock_ing is false, block until a buffer is available.
1694 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1695 struct v4l2_buffer
*v4l2_buf
)
1697 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1698 struct gspca_frame
*frame
;
1701 PDEBUG(D_FRAM
, "dqbuf");
1703 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1704 return -ERESTARTSYS
;
1707 ret
= frame_ready_nolock(gspca_dev
, file
, v4l2_buf
->memory
);
1713 mutex_unlock(&gspca_dev
->queue_lock
);
1715 if (file
->f_flags
& O_NONBLOCK
)
1718 /* wait till a frame is ready */
1719 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1720 frame_ready(gspca_dev
, file
, v4l2_buf
->memory
),
1721 msecs_to_jiffies(3000));
1727 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1728 return -ERESTARTSYS
;
1731 i
= gspca_dev
->fr_o
;
1732 j
= gspca_dev
->fr_queue
[i
];
1733 frame
= &gspca_dev
->frame
[j
];
1735 gspca_dev
->fr_o
= (i
+ 1) % GSPCA_MAX_FRAMES
;
1737 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1738 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1739 PDEBUG(D_FRAM
, "dqbuf %d", j
);
1742 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1743 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1745 frame
->v4l2_buf
.bytesused
)) {
1746 PERR("dqbuf cp to user failed");
1751 mutex_unlock(&gspca_dev
->queue_lock
);
1753 if (ret
== 0 && gspca_dev
->sd_desc
->dq_callback
) {
1754 mutex_lock(&gspca_dev
->usb_lock
);
1755 gspca_dev
->usb_err
= 0;
1756 if (gspca_dev
->present
)
1757 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1758 mutex_unlock(&gspca_dev
->usb_lock
);
1765 * queue a video buffer
1767 * Attempting to queue a buffer that has already been
1768 * queued will return -EINVAL.
1770 static int vidioc_qbuf(struct file
*file
, void *priv
,
1771 struct v4l2_buffer
*v4l2_buf
)
1773 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1774 struct gspca_frame
*frame
;
1777 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1779 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1780 return -ERESTARTSYS
;
1782 index
= v4l2_buf
->index
;
1783 if ((unsigned) index
>= gspca_dev
->nframes
) {
1785 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1789 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1790 PDEBUG(D_FRAM
, "qbuf bad memory type");
1795 frame
= &gspca_dev
->frame
[index
];
1796 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1797 PDEBUG(D_FRAM
, "qbuf bad state");
1802 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1804 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1805 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1806 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1809 /* put the buffer in the 'queued' queue */
1810 i
= atomic_read(&gspca_dev
->fr_q
);
1811 gspca_dev
->fr_queue
[i
] = index
;
1812 atomic_set(&gspca_dev
->fr_q
, (i
+ 1) % GSPCA_MAX_FRAMES
);
1814 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1815 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1818 mutex_unlock(&gspca_dev
->queue_lock
);
1823 * allocate the resources for read()
1825 static int read_alloc(struct gspca_dev
*gspca_dev
,
1828 struct v4l2_buffer v4l2_buf
;
1831 PDEBUG(D_STREAM
, "read alloc");
1833 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1834 return -ERESTARTSYS
;
1836 if (gspca_dev
->nframes
== 0) {
1837 struct v4l2_requestbuffers rb
;
1839 memset(&rb
, 0, sizeof rb
);
1840 rb
.count
= gspca_dev
->nbufread
;
1841 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1842 rb
.memory
= GSPCA_MEMORY_READ
;
1843 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
1845 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
1848 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1849 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1850 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1851 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
1853 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1855 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
1861 /* start streaming */
1862 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
1864 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
1866 mutex_unlock(&gspca_dev
->usb_lock
);
1870 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
1872 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1873 unsigned long req_events
= poll_requested_events(wait
);
1876 PDEBUG(D_FRAM
, "poll");
1878 if (req_events
& POLLPRI
)
1879 ret
|= v4l2_ctrl_poll(file
, wait
);
1881 if (req_events
& (POLLIN
| POLLRDNORM
)) {
1882 /* if reqbufs is not done, the user would use read() */
1883 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) {
1884 if (read_alloc(gspca_dev
, file
) != 0) {
1890 poll_wait(file
, &gspca_dev
->wq
, wait
);
1892 /* check if an image has been received */
1893 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0) {
1897 if (gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
))
1898 ret
|= POLLIN
| POLLRDNORM
;
1899 mutex_unlock(&gspca_dev
->queue_lock
);
1903 if (!gspca_dev
->present
)
1909 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
1910 size_t count
, loff_t
*ppos
)
1912 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1913 struct gspca_frame
*frame
;
1914 struct v4l2_buffer v4l2_buf
;
1915 struct timeval timestamp
;
1918 PDEBUG(D_FRAM
, "read (%zd)", count
);
1919 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) { /* first time ? */
1920 ret
= read_alloc(gspca_dev
, file
);
1926 v4l2_get_timestamp(×tamp
);
1930 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
1931 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1932 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
1933 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
1935 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
1939 /* if the process slept for more than 1 second,
1940 * get a newer frame */
1941 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
1943 break; /* avoid infinite loop */
1944 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
1946 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1948 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
1953 /* copy the frame */
1954 if (count
> frame
->v4l2_buf
.bytesused
)
1955 count
= frame
->v4l2_buf
.bytesused
;
1956 ret
= copy_to_user(data
, frame
->data
, count
);
1958 PERR("read cp to user lack %d / %zd", ret
, count
);
1964 /* in each case, requeue the buffer */
1965 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
1971 static struct v4l2_file_operations dev_fops
= {
1972 .owner
= THIS_MODULE
,
1974 .release
= dev_close
,
1977 .unlocked_ioctl
= video_ioctl2
,
1981 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1982 .vidioc_querycap
= vidioc_querycap
,
1983 .vidioc_dqbuf
= vidioc_dqbuf
,
1984 .vidioc_qbuf
= vidioc_qbuf
,
1985 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1986 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1987 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1988 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1989 .vidioc_streamon
= vidioc_streamon
,
1990 .vidioc_enum_input
= vidioc_enum_input
,
1991 .vidioc_g_input
= vidioc_g_input
,
1992 .vidioc_s_input
= vidioc_s_input
,
1993 .vidioc_reqbufs
= vidioc_reqbufs
,
1994 .vidioc_querybuf
= vidioc_querybuf
,
1995 .vidioc_streamoff
= vidioc_streamoff
,
1996 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1997 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1998 .vidioc_g_parm
= vidioc_g_parm
,
1999 .vidioc_s_parm
= vidioc_s_parm
,
2000 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2001 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2002 #ifdef CONFIG_VIDEO_ADV_DEBUG
2003 .vidioc_g_chip_info
= vidioc_g_chip_info
,
2004 .vidioc_g_register
= vidioc_g_register
,
2005 .vidioc_s_register
= vidioc_s_register
,
2007 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2008 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2011 static const struct video_device gspca_template
= {
2012 .name
= "gspca main driver",
2014 .ioctl_ops
= &dev_ioctl_ops
,
2015 .release
= video_device_release_empty
, /* We use v4l2_dev.release */
2019 * probe and create a new gspca device
2021 * This function must be called by the sub-driver when it is
2022 * called for probing a new device.
2024 int gspca_dev_probe2(struct usb_interface
*intf
,
2025 const struct usb_device_id
*id
,
2026 const struct sd_desc
*sd_desc
,
2028 struct module
*module
)
2030 struct gspca_dev
*gspca_dev
;
2031 struct usb_device
*dev
= interface_to_usbdev(intf
);
2034 pr_info("%s-" GSPCA_VERSION
" probing %04x:%04x\n",
2035 sd_desc
->name
, id
->idVendor
, id
->idProduct
);
2037 /* create the device */
2038 if (dev_size
< sizeof *gspca_dev
)
2039 dev_size
= sizeof *gspca_dev
;
2040 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2042 pr_err("couldn't kzalloc gspca struct\n");
2045 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2046 if (!gspca_dev
->usb_buf
) {
2047 pr_err("out of memory\n");
2051 gspca_dev
->dev
= dev
;
2052 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2053 gspca_dev
->xfer_ep
= -1;
2055 /* check if any audio device */
2056 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
2058 struct usb_interface
*intf2
;
2060 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
2061 intf2
= dev
->actconfig
->interface
[i
];
2063 && intf2
->altsetting
!= NULL
2064 && intf2
->altsetting
->desc
.bInterfaceClass
==
2066 gspca_dev
->audio
= 1;
2072 gspca_dev
->v4l2_dev
.release
= gspca_release
;
2073 ret
= v4l2_device_register(&intf
->dev
, &gspca_dev
->v4l2_dev
);
2076 gspca_dev
->sd_desc
= sd_desc
;
2077 gspca_dev
->nbufread
= 2;
2078 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2079 gspca_dev
->vdev
= gspca_template
;
2080 gspca_dev
->vdev
.v4l2_dev
= &gspca_dev
->v4l2_dev
;
2081 video_set_drvdata(&gspca_dev
->vdev
, gspca_dev
);
2082 gspca_dev
->module
= module
;
2083 gspca_dev
->present
= 1;
2085 mutex_init(&gspca_dev
->usb_lock
);
2086 gspca_dev
->vdev
.lock
= &gspca_dev
->usb_lock
;
2087 mutex_init(&gspca_dev
->queue_lock
);
2088 init_waitqueue_head(&gspca_dev
->wq
);
2090 /* configure the subdriver and initialize the USB device */
2091 ret
= sd_desc
->config(gspca_dev
, id
);
2094 ret
= sd_desc
->init(gspca_dev
);
2097 if (sd_desc
->init_controls
)
2098 ret
= sd_desc
->init_controls(gspca_dev
);
2101 gspca_set_default_mode(gspca_dev
);
2103 ret
= gspca_input_connect(gspca_dev
);
2108 * Don't take usb_lock for these ioctls. This improves latency if
2109 * usb_lock is taken for a long time, e.g. when changing a control
2110 * value, and a new frame is ready to be dequeued.
2112 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_DQBUF
);
2113 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_QBUF
);
2114 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_QUERYBUF
);
2115 #ifdef CONFIG_VIDEO_ADV_DEBUG
2116 if (!gspca_dev
->sd_desc
->get_register
)
2117 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_DBG_G_REGISTER
);
2118 if (!gspca_dev
->sd_desc
->set_register
)
2119 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_DBG_S_REGISTER
);
2121 if (!gspca_dev
->sd_desc
->get_jcomp
)
2122 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_G_JPEGCOMP
);
2123 if (!gspca_dev
->sd_desc
->set_jcomp
)
2124 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_S_JPEGCOMP
);
2126 /* init video stuff */
2127 ret
= video_register_device(&gspca_dev
->vdev
,
2131 pr_err("video_register_device err %d\n", ret
);
2135 usb_set_intfdata(intf
, gspca_dev
);
2136 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2138 gspca_input_create_urb(gspca_dev
);
2142 #if IS_ENABLED(CONFIG_INPUT)
2143 if (gspca_dev
->input_dev
)
2144 input_unregister_device(gspca_dev
->input_dev
);
2146 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
2147 kfree(gspca_dev
->usb_buf
);
2151 EXPORT_SYMBOL(gspca_dev_probe2
);
2153 /* same function as the previous one, but check the interface */
2154 int gspca_dev_probe(struct usb_interface
*intf
,
2155 const struct usb_device_id
*id
,
2156 const struct sd_desc
*sd_desc
,
2158 struct module
*module
)
2160 struct usb_device
*dev
= interface_to_usbdev(intf
);
2162 /* we don't handle multi-config cameras */
2163 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2164 pr_err("%04x:%04x too many config\n",
2165 id
->idVendor
, id
->idProduct
);
2169 /* the USB video interface must be the first one */
2170 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1
2171 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2174 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
2176 EXPORT_SYMBOL(gspca_dev_probe
);
2181 * This function must be called by the sub-driver
2182 * when the device disconnects, after the specific resources are freed.
2184 void gspca_disconnect(struct usb_interface
*intf
)
2186 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2187 #if IS_ENABLED(CONFIG_INPUT)
2188 struct input_dev
*input_dev
;
2191 PDEBUG(D_PROBE
, "%s disconnect",
2192 video_device_node_name(&gspca_dev
->vdev
));
2194 mutex_lock(&gspca_dev
->usb_lock
);
2196 gspca_dev
->present
= 0;
2197 destroy_urbs(gspca_dev
);
2199 #if IS_ENABLED(CONFIG_INPUT)
2200 gspca_input_destroy_urb(gspca_dev
);
2201 input_dev
= gspca_dev
->input_dev
;
2203 gspca_dev
->input_dev
= NULL
;
2204 input_unregister_device(input_dev
);
2207 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2208 if (gspca_dev
->sd_desc
->stop0
&& gspca_dev
->streaming
)
2209 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2210 gspca_dev
->streaming
= 0;
2211 gspca_dev
->dev
= NULL
;
2212 wake_up_interruptible(&gspca_dev
->wq
);
2214 v4l2_device_disconnect(&gspca_dev
->v4l2_dev
);
2215 video_unregister_device(&gspca_dev
->vdev
);
2217 mutex_unlock(&gspca_dev
->usb_lock
);
2219 /* (this will call gspca_release() immediately or on last close) */
2220 v4l2_device_put(&gspca_dev
->v4l2_dev
);
2222 EXPORT_SYMBOL(gspca_disconnect
);
2225 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2227 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2229 gspca_input_destroy_urb(gspca_dev
);
2231 if (!gspca_dev
->streaming
)
2234 mutex_lock(&gspca_dev
->usb_lock
);
2235 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2236 gspca_dev
->usb_err
= 0;
2237 if (gspca_dev
->sd_desc
->stopN
)
2238 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2239 destroy_urbs(gspca_dev
);
2240 gspca_set_alt0(gspca_dev
);
2241 if (gspca_dev
->sd_desc
->stop0
)
2242 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2243 mutex_unlock(&gspca_dev
->usb_lock
);
2247 EXPORT_SYMBOL(gspca_suspend
);
2249 int gspca_resume(struct usb_interface
*intf
)
2251 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2252 int streaming
, ret
= 0;
2254 mutex_lock(&gspca_dev
->usb_lock
);
2255 gspca_dev
->frozen
= 0;
2256 gspca_dev
->usb_err
= 0;
2257 gspca_dev
->sd_desc
->init(gspca_dev
);
2259 * Most subdrivers send all ctrl values on sd_start and thus
2260 * only write to the device registers on s_ctrl when streaming ->
2261 * Clear streaming to avoid setting all ctrls twice.
2263 streaming
= gspca_dev
->streaming
;
2264 gspca_dev
->streaming
= 0;
2266 ret
= gspca_init_transfer(gspca_dev
);
2268 gspca_input_create_urb(gspca_dev
);
2269 mutex_unlock(&gspca_dev
->usb_lock
);
2273 EXPORT_SYMBOL(gspca_resume
);
2276 /* -- module insert / remove -- */
2277 static int __init
gspca_init(void)
2279 pr_info("v" GSPCA_VERSION
" registered\n");
2282 static void __exit
gspca_exit(void)
2286 module_init(gspca_init
);
2287 module_exit(gspca_exit
);
2289 module_param_named(debug
, gspca_debug
, int, 0644);
2290 MODULE_PARM_DESC(debug
,
2291 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");