1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Main USB camera driver
5 * Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
7 * Camera button input handling by Márton Németh
8 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #define GSPCA_VERSION "2.14.0"
15 #include <linux/init.h>
17 #include <linux/vmalloc.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/pagemap.h>
25 #include <linux/uaccess.h>
26 #include <linux/ktime.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
34 #if IS_ENABLED(CONFIG_INPUT)
35 #include <linux/input.h>
36 #include <linux/usb/input.h>
40 #define DEF_NURBS 3 /* default number of URBs */
41 #if DEF_NURBS > MAX_NURBS
42 #error "DEF_NURBS too big"
45 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
46 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION(GSPCA_VERSION
);
51 EXPORT_SYMBOL(gspca_debug
);
53 static void PDEBUG_MODE(struct gspca_dev
*gspca_dev
, int debug
, char *txt
,
54 __u32 pixfmt
, int w
, int h
)
56 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
57 gspca_dbg(gspca_dev
, debug
, "%s %c%c%c%c %dx%d\n",
61 (pixfmt
>> 16) & 0xff,
65 gspca_dbg(gspca_dev
, debug
, "%s 0x%08x %dx%d\n",
72 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
73 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
74 #define GSPCA_MEMORY_READ 7
77 * Input and interrupt endpoint handling functions
79 #if IS_ENABLED(CONFIG_INPUT)
80 static void int_irq(struct urb
*urb
)
82 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
88 if (gspca_dev
->sd_desc
->int_pkt_scan(gspca_dev
,
89 urb
->transfer_buffer
, urb
->actual_length
) < 0) {
90 gspca_err(gspca_dev
, "Unknown packet received\n");
98 /* Stop is requested either by software or hardware is gone,
99 * keep the ret value non-zero and don't resubmit later.
104 gspca_err(gspca_dev
, "URB error %i, resubmitting\n",
111 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
113 pr_err("Resubmit URB failed with error %i\n", ret
);
117 static int gspca_input_connect(struct gspca_dev
*dev
)
119 struct input_dev
*input_dev
;
122 dev
->input_dev
= NULL
;
123 if (dev
->sd_desc
->int_pkt_scan
|| dev
->sd_desc
->other_input
) {
124 input_dev
= input_allocate_device();
128 usb_make_path(dev
->dev
, dev
->phys
, sizeof(dev
->phys
));
129 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
131 input_dev
->name
= dev
->sd_desc
->name
;
132 input_dev
->phys
= dev
->phys
;
134 usb_to_input_id(dev
->dev
, &input_dev
->id
);
136 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
137 input_dev
->keybit
[BIT_WORD(KEY_CAMERA
)] = BIT_MASK(KEY_CAMERA
);
138 input_dev
->dev
.parent
= &dev
->dev
->dev
;
140 err
= input_register_device(input_dev
);
142 pr_err("Input device registration failed with error %i\n",
144 input_dev
->dev
.parent
= NULL
;
145 input_free_device(input_dev
);
147 dev
->input_dev
= input_dev
;
154 static int alloc_and_submit_int_urb(struct gspca_dev
*gspca_dev
,
155 struct usb_endpoint_descriptor
*ep
)
157 unsigned int buffer_len
;
160 struct usb_device
*dev
;
164 buffer_len
= le16_to_cpu(ep
->wMaxPacketSize
);
165 interval
= ep
->bInterval
;
166 gspca_dbg(gspca_dev
, D_CONF
, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u\n",
167 ep
->bEndpointAddress
, buffer_len
, interval
);
169 dev
= gspca_dev
->dev
;
171 urb
= usb_alloc_urb(0, GFP_KERNEL
);
177 buffer
= usb_alloc_coherent(dev
, buffer_len
,
178 GFP_KERNEL
, &urb
->transfer_dma
);
183 usb_fill_int_urb(urb
, dev
,
184 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
186 int_irq
, (void *)gspca_dev
, interval
);
187 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
188 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
190 gspca_err(gspca_dev
, "submit int URB failed with error %i\n",
194 gspca_dev
->int_urb
= urb
;
198 usb_free_coherent(dev
,
199 urb
->transfer_buffer_length
,
200 urb
->transfer_buffer
,
208 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
210 struct usb_interface
*intf
;
211 struct usb_host_interface
*intf_desc
;
212 struct usb_endpoint_descriptor
*ep
;
215 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
216 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
217 intf_desc
= intf
->cur_altsetting
;
218 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
219 ep
= &intf_desc
->endpoint
[i
].desc
;
220 if (usb_endpoint_dir_in(ep
) &&
221 usb_endpoint_xfer_int(ep
)) {
223 alloc_and_submit_int_urb(gspca_dev
, ep
);
230 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
234 urb
= gspca_dev
->int_urb
;
236 gspca_dev
->int_urb
= NULL
;
238 usb_free_coherent(gspca_dev
->dev
,
239 urb
->transfer_buffer_length
,
240 urb
->transfer_buffer
,
246 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
250 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
254 static inline int gspca_input_connect(struct gspca_dev
*dev
)
261 * fill a video frame from an URB and resubmit
263 static void fill_frame(struct gspca_dev
*gspca_dev
,
266 u8
*data
; /* address of data in the iso message */
270 if (urb
->status
!= 0) {
271 if (urb
->status
== -ESHUTDOWN
)
272 return; /* disconnection */
274 if (gspca_dev
->frozen
)
277 gspca_err(gspca_dev
, "urb status: %d\n", urb
->status
);
281 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
282 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
283 len
= urb
->iso_frame_desc
[i
].actual_length
;
285 /* check the packet status and length */
286 st
= urb
->iso_frame_desc
[i
].status
;
288 gspca_dbg(gspca_dev
, D_PACK
, "ISOC data error: [%d] len=%d, status=%d\n",
290 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
294 if (gspca_dev
->empty_packet
== 0)
295 gspca_dev
->empty_packet
= 1;
299 /* let the packet be analyzed by the subdriver */
300 gspca_dbg(gspca_dev
, D_PACK
, "packet [%d] o:%d l:%d\n",
301 i
, urb
->iso_frame_desc
[i
].offset
, len
);
302 data
= (u8
*) urb
->transfer_buffer
303 + urb
->iso_frame_desc
[i
].offset
;
304 pkt_scan(gspca_dev
, data
, len
);
308 if (!gspca_dev
->streaming
)
310 /* resubmit the URB */
311 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
313 pr_err("usb_submit_urb() ret %d\n", st
);
317 * ISOC message interrupt from the USB device
319 * Analyse each packet and call the subdriver for copy to the frame buffer.
321 static void isoc_irq(struct urb
*urb
)
323 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
325 gspca_dbg(gspca_dev
, D_PACK
, "isoc irq\n");
326 if (!gspca_dev
->streaming
)
328 fill_frame(gspca_dev
, urb
);
332 * bulk message interrupt from the USB device
334 static void bulk_irq(struct urb
*urb
)
336 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
339 gspca_dbg(gspca_dev
, D_PACK
, "bulk irq\n");
340 if (!gspca_dev
->streaming
)
342 switch (urb
->status
) {
346 return; /* disconnection */
349 if (gspca_dev
->frozen
)
352 gspca_err(gspca_dev
, "urb status: %d\n", urb
->status
);
357 gspca_dbg(gspca_dev
, D_PACK
, "packet l:%d\n", urb
->actual_length
);
358 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
359 urb
->transfer_buffer
,
363 if (!gspca_dev
->streaming
)
365 /* resubmit the URB */
366 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
367 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
369 pr_err("usb_submit_urb() ret %d\n", st
);
374 * add data to the current frame
376 * This function is called by the subdrivers at interrupt level.
378 * To build a frame, these ones must add
380 * - 0 or many INTER_PACKETs
382 * DISCARD_PACKET invalidates the whole frame.
384 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
385 enum gspca_packet_type packet_type
,
389 struct gspca_buffer
*buf
;
392 gspca_dbg(gspca_dev
, D_PACK
, "add t:%d l:%d\n", packet_type
, len
);
394 spin_lock_irqsave(&gspca_dev
->qlock
, flags
);
395 buf
= list_first_entry_or_null(&gspca_dev
->buf_list
,
397 spin_unlock_irqrestore(&gspca_dev
->qlock
, flags
);
399 if (packet_type
== FIRST_PACKET
) {
400 /* if there is no queued buffer, discard the whole frame */
402 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
403 gspca_dev
->sequence
++;
406 gspca_dev
->image
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
407 gspca_dev
->image_len
= 0;
409 switch (gspca_dev
->last_packet_type
) {
411 if (packet_type
== LAST_PACKET
) {
412 gspca_dev
->last_packet_type
= packet_type
;
413 gspca_dev
->image
= NULL
;
414 gspca_dev
->image_len
= 0;
422 /* append the packet to the frame buffer */
424 if (gspca_dev
->image_len
+ len
> PAGE_ALIGN(gspca_dev
->pixfmt
.sizeimage
)) {
425 gspca_err(gspca_dev
, "frame overflow %d > %d\n",
426 gspca_dev
->image_len
+ len
,
427 PAGE_ALIGN(gspca_dev
->pixfmt
.sizeimage
));
428 packet_type
= DISCARD_PACKET
;
430 /* !! image is NULL only when last pkt is LAST or DISCARD
431 if (gspca_dev->image == NULL) {
432 pr_err("gspca_frame_add() image == NULL\n");
436 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
438 gspca_dev
->image_len
+= len
;
441 gspca_dev
->last_packet_type
= packet_type
;
443 /* if last packet, invalidate packet concatenation until
444 * next first packet, wake up the application and advance
446 if (packet_type
== LAST_PACKET
) {
447 spin_lock_irqsave(&gspca_dev
->qlock
, flags
);
448 list_del(&buf
->list
);
449 spin_unlock_irqrestore(&gspca_dev
->qlock
, flags
);
450 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
451 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0,
452 gspca_dev
->image_len
);
453 buf
->vb
.sequence
= gspca_dev
->sequence
++;
454 buf
->vb
.field
= V4L2_FIELD_NONE
;
455 gspca_dbg(gspca_dev
, D_FRAM
, "frame complete len:%d\n",
456 gspca_dev
->image_len
);
457 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
458 gspca_dev
->image
= NULL
;
459 gspca_dev
->image_len
= 0;
462 EXPORT_SYMBOL(gspca_frame_add
);
464 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
469 gspca_dbg(gspca_dev
, D_STREAM
, "kill transfer\n");
471 /* Killing all URBs guarantee that no URB completion
472 * handler is running. Therefore, there shouldn't
473 * be anyone trying to access gspca_dev->urb[i]
475 for (i
= 0; i
< MAX_NURBS
; i
++)
476 usb_kill_urb(gspca_dev
->urb
[i
]);
478 gspca_dbg(gspca_dev
, D_STREAM
, "releasing urbs\n");
479 for (i
= 0; i
< MAX_NURBS
; i
++) {
480 urb
= gspca_dev
->urb
[i
];
483 gspca_dev
->urb
[i
] = NULL
;
484 usb_free_coherent(gspca_dev
->dev
,
485 urb
->transfer_buffer_length
,
486 urb
->transfer_buffer
,
492 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
496 if (gspca_dev
->alt
== 0)
498 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
500 pr_err("set alt 0 err %d\n", ret
);
505 * look for an input transfer endpoint in an alternate setting.
507 * If xfer_ep is invalid, return the first valid ep found, otherwise
508 * look for exactly the ep with address equal to xfer_ep.
510 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
511 int xfer
, int xfer_ep
)
513 struct usb_host_endpoint
*ep
;
516 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
517 ep
= &alt
->endpoint
[i
];
518 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
520 && ep
->desc
.wMaxPacketSize
!= 0
521 && usb_endpoint_dir_in(&ep
->desc
)
522 && (xfer_ep
< 0 || ep
->desc
.bEndpointAddress
== xfer_ep
))
528 /* compute the minimum bandwidth for the current transfer */
529 static u32
which_bandwidth(struct gspca_dev
*gspca_dev
)
533 /* get the (max) image size */
534 bandwidth
= gspca_dev
->pixfmt
.sizeimage
;
536 /* if the image is compressed, estimate its mean size */
537 if (!gspca_dev
->cam
.needs_full_bandwidth
&&
538 bandwidth
< gspca_dev
->pixfmt
.width
*
539 gspca_dev
->pixfmt
.height
)
540 bandwidth
= bandwidth
* 3 / 8; /* 0.375 */
542 /* estimate the frame rate */
543 if (gspca_dev
->sd_desc
->get_streamparm
) {
544 struct v4l2_streamparm parm
;
546 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, &parm
);
547 bandwidth
*= parm
.parm
.capture
.timeperframe
.denominator
;
548 bandwidth
/= parm
.parm
.capture
.timeperframe
.numerator
;
551 /* don't hope more than 15 fps with USB 1.1 and
552 * image resolution >= 640x480 */
553 if (gspca_dev
->pixfmt
.width
>= 640
554 && gspca_dev
->dev
->speed
== USB_SPEED_FULL
)
555 bandwidth
*= 15; /* 15 fps */
557 bandwidth
*= 30; /* 30 fps */
560 gspca_dbg(gspca_dev
, D_STREAM
, "min bandwidth: %d\n", bandwidth
);
572 * build the table of the endpoints
573 * and compute the minimum bandwidth for the image transfer
575 static int build_isoc_ep_tb(struct gspca_dev
*gspca_dev
,
576 struct usb_interface
*intf
,
577 struct ep_tb_s
*ep_tb
)
579 struct usb_host_endpoint
*ep
;
580 int i
, j
, nbalt
, psize
, found
;
581 u32 bandwidth
, last_bw
;
583 nbalt
= intf
->num_altsetting
;
585 nbalt
= MAX_ALT
; /* fixme: should warn */
587 /* build the endpoint table */
591 ep_tb
->bandwidth
= 2000 * 2000 * 120;
593 for (j
= 0; j
< nbalt
; j
++) {
594 ep
= alt_xfer(&intf
->altsetting
[j
],
595 USB_ENDPOINT_XFER_ISOC
,
599 if (ep
->desc
.bInterval
== 0) {
600 pr_err("alt %d iso endp with 0 interval\n", j
);
603 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
604 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
605 bandwidth
= psize
* 1000;
606 if (gspca_dev
->dev
->speed
== USB_SPEED_HIGH
607 || gspca_dev
->dev
->speed
>= USB_SPEED_SUPER
)
609 bandwidth
/= 1 << (ep
->desc
.bInterval
- 1);
610 if (bandwidth
<= last_bw
)
612 if (bandwidth
< ep_tb
->bandwidth
) {
613 ep_tb
->bandwidth
= bandwidth
;
620 gspca_dbg(gspca_dev
, D_STREAM
, "alt %d bandwidth %d\n",
621 ep_tb
->alt
, ep_tb
->bandwidth
);
622 last_bw
= ep_tb
->bandwidth
;
629 * has a usb audio class interface (a built in usb mic); and
630 * is a usb 1 full speed device; and
631 * uses the max full speed iso bandwidth; and
632 * and has more than 1 alt setting
633 * then skip the highest alt setting to spare bandwidth for the mic
635 if (gspca_dev
->audio
&&
636 gspca_dev
->dev
->speed
== USB_SPEED_FULL
&&
637 last_bw
>= 1000000 &&
639 gspca_dbg(gspca_dev
, D_STREAM
, "dev has usb audio, skipping highest alt\n");
644 /* get the requested bandwidth and start at the highest atlsetting */
645 bandwidth
= which_bandwidth(gspca_dev
);
649 if (ep_tb
->bandwidth
< bandwidth
)
657 * create the URBs for image transfer
659 static int create_urbs(struct gspca_dev
*gspca_dev
,
660 struct usb_host_endpoint
*ep
)
663 int n
, nurbs
, i
, psize
, npkt
, bsize
;
665 /* calculate the packet size and the number of packets */
666 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
668 if (!gspca_dev
->cam
.bulk
) { /* isoc */
670 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
671 if (gspca_dev
->pkt_size
== 0)
672 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
674 psize
= gspca_dev
->pkt_size
;
675 npkt
= gspca_dev
->cam
.npkt
;
677 npkt
= 32; /* default value */
678 bsize
= psize
* npkt
;
679 gspca_dbg(gspca_dev
, D_STREAM
,
680 "isoc %d pkts size %d = bsize:%d\n",
685 bsize
= gspca_dev
->cam
.bulk_size
;
688 gspca_dbg(gspca_dev
, D_STREAM
, "bulk bsize:%d\n", bsize
);
689 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
690 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
695 for (n
= 0; n
< nurbs
; n
++) {
696 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
699 gspca_dev
->urb
[n
] = urb
;
700 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
705 if (urb
->transfer_buffer
== NULL
) {
706 pr_err("usb_alloc_coherent failed\n");
709 urb
->dev
= gspca_dev
->dev
;
710 urb
->context
= gspca_dev
;
711 urb
->transfer_buffer_length
= bsize
;
712 if (npkt
!= 0) { /* ISOC */
713 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
714 ep
->desc
.bEndpointAddress
);
715 urb
->transfer_flags
= URB_ISO_ASAP
716 | URB_NO_TRANSFER_DMA_MAP
;
717 urb
->interval
= 1 << (ep
->desc
.bInterval
- 1);
718 urb
->complete
= isoc_irq
;
719 urb
->number_of_packets
= npkt
;
720 for (i
= 0; i
< npkt
; i
++) {
721 urb
->iso_frame_desc
[i
].length
= psize
;
722 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
725 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
726 ep
->desc
.bEndpointAddress
);
727 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
728 urb
->complete
= bulk_irq
;
734 /* Note: both the queue and the usb locks should be held when calling this */
735 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
737 gspca_dev
->streaming
= false;
738 gspca_dev
->usb_err
= 0;
739 if (gspca_dev
->sd_desc
->stopN
)
740 gspca_dev
->sd_desc
->stopN(gspca_dev
);
741 destroy_urbs(gspca_dev
);
742 gspca_input_destroy_urb(gspca_dev
);
743 gspca_set_alt0(gspca_dev
);
744 if (gspca_dev
->present
)
745 gspca_input_create_urb(gspca_dev
);
746 if (gspca_dev
->sd_desc
->stop0
)
747 gspca_dev
->sd_desc
->stop0(gspca_dev
);
748 gspca_dbg(gspca_dev
, D_STREAM
, "stream off OK\n");
752 * start the USB transfer
754 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
756 struct usb_interface
*intf
;
757 struct usb_host_endpoint
*ep
;
759 struct ep_tb_s ep_tb
[MAX_ALT
];
760 int n
, ret
, xfer
, alt
, alt_idx
;
762 /* reset the streaming variables */
763 gspca_dev
->image
= NULL
;
764 gspca_dev
->image_len
= 0;
765 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
767 gspca_dev
->usb_err
= 0;
769 /* do the specific subdriver stuff before endpoint selection */
770 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
771 gspca_dev
->alt
= gspca_dev
->cam
.bulk
? intf
->num_altsetting
: 0;
772 if (gspca_dev
->sd_desc
->isoc_init
) {
773 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
777 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
778 : USB_ENDPOINT_XFER_ISOC
;
780 /* if bulk or the subdriver forced an altsetting, get the endpoint */
781 if (gspca_dev
->alt
!= 0) {
782 gspca_dev
->alt
--; /* (previous version compatibility) */
783 ep
= alt_xfer(&intf
->altsetting
[gspca_dev
->alt
], xfer
,
786 pr_err("bad altsetting %d\n", gspca_dev
->alt
);
789 ep_tb
[0].alt
= gspca_dev
->alt
;
792 /* else, compute the minimum bandwidth
793 * and build the endpoint table */
794 alt_idx
= build_isoc_ep_tb(gspca_dev
, intf
, ep_tb
);
796 pr_err("no transfer endpoint found\n");
801 /* set the highest alternate setting and
802 * loop until urb submit succeeds */
803 gspca_input_destroy_urb(gspca_dev
);
805 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
808 if (alt
!= gspca_dev
->alt
) {
809 alt
= gspca_dev
->alt
;
810 if (intf
->num_altsetting
> 1) {
811 ret
= usb_set_interface(gspca_dev
->dev
,
816 goto retry
; /*fixme: ugly*/
817 pr_err("set alt %d err %d\n", alt
, ret
);
822 if (!gspca_dev
->cam
.no_urb_create
) {
823 gspca_dbg(gspca_dev
, D_STREAM
, "init transfer alt %d\n",
825 ret
= create_urbs(gspca_dev
,
826 alt_xfer(&intf
->altsetting
[alt
], xfer
,
827 gspca_dev
->xfer_ep
));
829 destroy_urbs(gspca_dev
);
834 /* clear the bulk endpoint */
835 if (gspca_dev
->cam
.bulk
)
836 usb_clear_halt(gspca_dev
->dev
,
837 gspca_dev
->urb
[0]->pipe
);
840 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
842 destroy_urbs(gspca_dev
);
845 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
846 gspca_dev
->streaming
= true;
848 /* some bulk transfers are started by the subdriver */
849 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
852 /* submit the URBs */
853 for (n
= 0; n
< MAX_NURBS
; n
++) {
854 urb
= gspca_dev
->urb
[n
];
857 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
862 break; /* transfer is started */
864 /* something when wrong
865 * stop the webcam and free the transfer resources */
866 gspca_stream_off(gspca_dev
);
867 if (ret
!= -ENOSPC
) {
868 pr_err("usb_submit_urb alt %d err %d\n",
869 gspca_dev
->alt
, ret
);
873 /* the bandwidth is not wide enough
874 * negotiate or try a lower alternate setting */
876 gspca_err(gspca_dev
, "alt %d - bandwidth not wide enough, trying again\n",
878 msleep(20); /* wait for kill complete */
879 if (gspca_dev
->sd_desc
->isoc_nego
) {
880 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
885 pr_err("no transfer endpoint found\n");
889 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
893 gspca_input_create_urb(gspca_dev
);
897 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
901 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
902 gspca_dev
->curr_mode
= i
;
903 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
];
905 /* does nothing if ctrl_handler == NULL */
906 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
909 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
910 int width
, int height
, u32 pixelformat
)
914 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
915 if (width
== gspca_dev
->cam
.cam_mode
[i
].width
916 && height
== gspca_dev
->cam
.cam_mode
[i
].height
917 && pixelformat
== gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
923 static int wxh_to_nearest_mode(struct gspca_dev
*gspca_dev
,
924 int width
, int height
, u32 pixelformat
)
928 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
929 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
930 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
931 && pixelformat
== gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
934 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
935 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
936 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
943 * search a mode with the right pixel format
945 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
951 modeU
= modeD
= mode
;
952 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
954 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
958 if (++modeU
< gspca_dev
->cam
.nmodes
) {
959 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
967 #ifdef CONFIG_VIDEO_ADV_DEBUG
968 static int vidioc_g_chip_info(struct file
*file
, void *priv
,
969 struct v4l2_dbg_chip_info
*chip
)
971 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
973 gspca_dev
->usb_err
= 0;
974 if (gspca_dev
->sd_desc
->get_chip_info
)
975 return gspca_dev
->sd_desc
->get_chip_info(gspca_dev
, chip
);
976 return chip
->match
.addr
? -EINVAL
: 0;
979 static int vidioc_g_register(struct file
*file
, void *priv
,
980 struct v4l2_dbg_register
*reg
)
982 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
984 gspca_dev
->usb_err
= 0;
985 return gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
988 static int vidioc_s_register(struct file
*file
, void *priv
,
989 const struct v4l2_dbg_register
*reg
)
991 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
993 gspca_dev
->usb_err
= 0;
994 return gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
998 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
999 struct v4l2_fmtdesc
*fmtdesc
)
1001 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1005 /* give an index to each format */
1007 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1008 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1011 if (fmt_tb
[j
] == fmt_tb
[index
])
1016 if (fmtdesc
->index
== index
)
1017 break; /* new format */
1019 if (index
>= ARRAY_SIZE(fmt_tb
))
1024 return -EINVAL
; /* no more format */
1026 fmtdesc
->pixelformat
= fmt_tb
[index
];
1030 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *_priv
,
1031 struct v4l2_format
*fmt
)
1033 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1034 u32 priv
= fmt
->fmt
.pix
.priv
;
1036 fmt
->fmt
.pix
= gspca_dev
->pixfmt
;
1037 /* some drivers use priv internally, so keep the original value */
1038 fmt
->fmt
.pix
.priv
= priv
;
1042 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1043 struct v4l2_format
*fmt
)
1045 int w
, h
, mode
, mode2
;
1047 w
= fmt
->fmt
.pix
.width
;
1048 h
= fmt
->fmt
.pix
.height
;
1050 PDEBUG_MODE(gspca_dev
, D_CONF
, "try fmt cap",
1051 fmt
->fmt
.pix
.pixelformat
, w
, h
);
1053 /* search the nearest mode for width and height */
1054 mode
= wxh_to_nearest_mode(gspca_dev
, w
, h
, fmt
->fmt
.pix
.pixelformat
);
1056 /* OK if right palette */
1057 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1058 != fmt
->fmt
.pix
.pixelformat
) {
1060 /* else, search the closest mode with the same pixel format */
1061 mode2
= gspca_get_mode(gspca_dev
, mode
,
1062 fmt
->fmt
.pix
.pixelformat
);
1066 fmt
->fmt
.pix
= gspca_dev
->cam
.cam_mode
[mode
];
1067 if (gspca_dev
->sd_desc
->try_fmt
) {
1068 /* pass original resolution to subdriver try_fmt */
1069 fmt
->fmt
.pix
.width
= w
;
1070 fmt
->fmt
.pix
.height
= h
;
1071 gspca_dev
->sd_desc
->try_fmt(gspca_dev
, fmt
);
1073 return mode
; /* used when s_fmt */
1076 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *_priv
,
1077 struct v4l2_format
*fmt
)
1079 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1080 u32 priv
= fmt
->fmt
.pix
.priv
;
1082 if (try_fmt_vid_cap(gspca_dev
, fmt
) < 0)
1084 /* some drivers use priv internally, so keep the original value */
1085 fmt
->fmt
.pix
.priv
= priv
;
1089 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *_priv
,
1090 struct v4l2_format
*fmt
)
1092 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1093 u32 priv
= fmt
->fmt
.pix
.priv
;
1096 if (vb2_is_busy(&gspca_dev
->queue
))
1099 mode
= try_fmt_vid_cap(gspca_dev
, fmt
);
1103 gspca_dev
->curr_mode
= mode
;
1104 if (gspca_dev
->sd_desc
->try_fmt
)
1105 /* subdriver try_fmt can modify format parameters */
1106 gspca_dev
->pixfmt
= fmt
->fmt
.pix
;
1108 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[mode
];
1109 /* some drivers use priv internally, so keep the original value */
1110 fmt
->fmt
.pix
.priv
= priv
;
1114 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1115 struct v4l2_frmsizeenum
*fsize
)
1117 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1121 if (gspca_dev
->sd_desc
->enum_framesizes
)
1122 return gspca_dev
->sd_desc
->enum_framesizes(gspca_dev
, fsize
);
1124 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1125 if (fsize
->pixel_format
!=
1126 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1129 if (fsize
->index
== index
) {
1130 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1131 fsize
->discrete
.width
=
1132 gspca_dev
->cam
.cam_mode
[i
].width
;
1133 fsize
->discrete
.height
=
1134 gspca_dev
->cam
.cam_mode
[i
].height
;
1143 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1144 struct v4l2_frmivalenum
*fival
)
1146 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1150 mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
,
1151 fival
->pixel_format
);
1155 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1156 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1159 if (fival
->pixel_format
!=
1160 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1163 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1164 if (fival
->index
== i
) {
1165 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1166 fival
->discrete
.numerator
= 1;
1167 fival
->discrete
.denominator
=
1168 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1176 static void gspca_release(struct v4l2_device
*v4l2_device
)
1178 struct gspca_dev
*gspca_dev
=
1179 container_of(v4l2_device
, struct gspca_dev
, v4l2_dev
);
1181 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
1182 v4l2_device_unregister(&gspca_dev
->v4l2_dev
);
1183 kfree(gspca_dev
->usb_buf
);
1187 static int vidioc_querycap(struct file
*file
, void *priv
,
1188 struct v4l2_capability
*cap
)
1190 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1192 strscpy((char *)cap
->driver
, gspca_dev
->sd_desc
->name
,
1193 sizeof(cap
->driver
));
1194 if (gspca_dev
->dev
->product
!= NULL
) {
1195 strscpy((char *)cap
->card
, gspca_dev
->dev
->product
,
1198 snprintf((char *) cap
->card
, sizeof cap
->card
,
1199 "USB Camera (%04x:%04x)",
1200 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1201 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1203 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1204 sizeof(cap
->bus_info
));
1208 static int vidioc_enum_input(struct file
*file
, void *priv
,
1209 struct v4l2_input
*input
)
1211 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1213 if (input
->index
!= 0)
1215 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1216 input
->status
= gspca_dev
->cam
.input_flags
;
1217 strscpy(input
->name
, gspca_dev
->sd_desc
->name
,
1218 sizeof input
->name
);
1222 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1228 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1235 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1236 struct v4l2_jpegcompression
*jpegcomp
)
1238 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1240 gspca_dev
->usb_err
= 0;
1241 return gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1244 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1245 const struct v4l2_jpegcompression
*jpegcomp
)
1247 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1249 gspca_dev
->usb_err
= 0;
1250 return gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1253 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1254 struct v4l2_streamparm
*parm
)
1256 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1258 parm
->parm
.capture
.readbuffers
= gspca_dev
->queue
.min_buffers_needed
;
1260 if (!gspca_dev
->sd_desc
->get_streamparm
)
1263 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1264 gspca_dev
->usb_err
= 0;
1265 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1266 return gspca_dev
->usb_err
;
1269 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1270 struct v4l2_streamparm
*parm
)
1272 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1274 parm
->parm
.capture
.readbuffers
= gspca_dev
->queue
.min_buffers_needed
;
1276 if (!gspca_dev
->sd_desc
->set_streamparm
) {
1277 parm
->parm
.capture
.capability
= 0;
1281 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1282 gspca_dev
->usb_err
= 0;
1283 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1284 return gspca_dev
->usb_err
;
1287 static int gspca_queue_setup(struct vb2_queue
*vq
,
1288 unsigned int *nbuffers
, unsigned int *nplanes
,
1289 unsigned int sizes
[], struct device
*alloc_devs
[])
1291 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vq
);
1292 unsigned int size
= PAGE_ALIGN(gspca_dev
->pixfmt
.sizeimage
);
1295 return sizes
[0] < size
? -EINVAL
: 0;
1301 static int gspca_buffer_prepare(struct vb2_buffer
*vb
)
1303 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1304 unsigned long size
= PAGE_ALIGN(gspca_dev
->pixfmt
.sizeimage
);
1306 if (vb2_plane_size(vb
, 0) < size
) {
1307 gspca_err(gspca_dev
, "buffer too small (%lu < %lu)\n",
1308 vb2_plane_size(vb
, 0), size
);
1314 static void gspca_buffer_finish(struct vb2_buffer
*vb
)
1316 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1318 if (!gspca_dev
->sd_desc
->dq_callback
)
1321 gspca_dev
->usb_err
= 0;
1322 if (gspca_dev
->present
)
1323 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1326 static void gspca_buffer_queue(struct vb2_buffer
*vb
)
1328 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1329 struct gspca_buffer
*buf
= to_gspca_buffer(vb
);
1330 unsigned long flags
;
1332 spin_lock_irqsave(&gspca_dev
->qlock
, flags
);
1333 list_add_tail(&buf
->list
, &gspca_dev
->buf_list
);
1334 spin_unlock_irqrestore(&gspca_dev
->qlock
, flags
);
1337 static void gspca_return_all_buffers(struct gspca_dev
*gspca_dev
,
1338 enum vb2_buffer_state state
)
1340 struct gspca_buffer
*buf
, *node
;
1341 unsigned long flags
;
1343 spin_lock_irqsave(&gspca_dev
->qlock
, flags
);
1344 list_for_each_entry_safe(buf
, node
, &gspca_dev
->buf_list
, list
) {
1345 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
1346 list_del(&buf
->list
);
1348 spin_unlock_irqrestore(&gspca_dev
->qlock
, flags
);
1351 static int gspca_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1353 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vq
);
1356 gspca_dev
->sequence
= 0;
1358 ret
= gspca_init_transfer(gspca_dev
);
1360 gspca_return_all_buffers(gspca_dev
, VB2_BUF_STATE_QUEUED
);
1364 static void gspca_stop_streaming(struct vb2_queue
*vq
)
1366 struct gspca_dev
*gspca_dev
= vb2_get_drv_priv(vq
);
1368 gspca_stream_off(gspca_dev
);
1370 /* Release all active buffers */
1371 gspca_return_all_buffers(gspca_dev
, VB2_BUF_STATE_ERROR
);
1374 static const struct vb2_ops gspca_qops
= {
1375 .queue_setup
= gspca_queue_setup
,
1376 .buf_prepare
= gspca_buffer_prepare
,
1377 .buf_finish
= gspca_buffer_finish
,
1378 .buf_queue
= gspca_buffer_queue
,
1379 .start_streaming
= gspca_start_streaming
,
1380 .stop_streaming
= gspca_stop_streaming
,
1381 .wait_prepare
= vb2_ops_wait_prepare
,
1382 .wait_finish
= vb2_ops_wait_finish
,
1385 static const struct v4l2_file_operations dev_fops
= {
1386 .owner
= THIS_MODULE
,
1387 .open
= v4l2_fh_open
,
1388 .release
= vb2_fop_release
,
1389 .unlocked_ioctl
= video_ioctl2
,
1390 .read
= vb2_fop_read
,
1391 .mmap
= vb2_fop_mmap
,
1392 .poll
= vb2_fop_poll
,
1395 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
1396 .vidioc_querycap
= vidioc_querycap
,
1397 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1398 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1399 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1400 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1401 .vidioc_enum_input
= vidioc_enum_input
,
1402 .vidioc_g_input
= vidioc_g_input
,
1403 .vidioc_s_input
= vidioc_s_input
,
1404 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
1405 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
1406 .vidioc_g_parm
= vidioc_g_parm
,
1407 .vidioc_s_parm
= vidioc_s_parm
,
1408 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
1409 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
1411 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1412 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1413 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1414 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1415 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1416 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1417 .vidioc_streamon
= vb2_ioctl_streamon
,
1418 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1420 #ifdef CONFIG_VIDEO_ADV_DEBUG
1421 .vidioc_g_chip_info
= vidioc_g_chip_info
,
1422 .vidioc_g_register
= vidioc_g_register
,
1423 .vidioc_s_register
= vidioc_s_register
,
1425 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1426 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1429 static const struct video_device gspca_template
= {
1430 .name
= "gspca main driver",
1432 .ioctl_ops
= &dev_ioctl_ops
,
1433 .release
= video_device_release_empty
, /* We use v4l2_dev.release */
1437 * probe and create a new gspca device
1439 * This function must be called by the sub-driver when it is
1440 * called for probing a new device.
1442 int gspca_dev_probe2(struct usb_interface
*intf
,
1443 const struct usb_device_id
*id
,
1444 const struct sd_desc
*sd_desc
,
1446 struct module
*module
)
1448 struct gspca_dev
*gspca_dev
;
1449 struct usb_device
*dev
= interface_to_usbdev(intf
);
1450 struct vb2_queue
*q
;
1453 pr_info("%s-" GSPCA_VERSION
" probing %04x:%04x\n",
1454 sd_desc
->name
, id
->idVendor
, id
->idProduct
);
1456 /* create the device */
1457 if (dev_size
< sizeof *gspca_dev
)
1458 dev_size
= sizeof *gspca_dev
;
1459 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
1461 pr_err("couldn't kzalloc gspca struct\n");
1464 gspca_dev
->usb_buf
= kzalloc(USB_BUF_SZ
, GFP_KERNEL
);
1465 if (!gspca_dev
->usb_buf
) {
1466 pr_err("out of memory\n");
1470 gspca_dev
->dev
= dev
;
1471 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
1472 gspca_dev
->xfer_ep
= -1;
1474 /* check if any audio device */
1475 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
1477 struct usb_interface
*intf2
;
1479 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1480 intf2
= dev
->actconfig
->interface
[i
];
1482 && intf2
->altsetting
!= NULL
1483 && intf2
->altsetting
->desc
.bInterfaceClass
==
1485 gspca_dev
->audio
= 1;
1491 gspca_dev
->v4l2_dev
.release
= gspca_release
;
1492 ret
= v4l2_device_register(&intf
->dev
, &gspca_dev
->v4l2_dev
);
1495 gspca_dev
->present
= true;
1496 gspca_dev
->sd_desc
= sd_desc
;
1497 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
1498 gspca_dev
->vdev
= gspca_template
;
1499 gspca_dev
->vdev
.v4l2_dev
= &gspca_dev
->v4l2_dev
;
1500 gspca_dev
->vdev
.device_caps
= V4L2_CAP_VIDEO_CAPTURE
|
1501 V4L2_CAP_STREAMING
| V4L2_CAP_READWRITE
;
1502 video_set_drvdata(&gspca_dev
->vdev
, gspca_dev
);
1503 gspca_dev
->module
= module
;
1505 mutex_init(&gspca_dev
->usb_lock
);
1506 gspca_dev
->vdev
.lock
= &gspca_dev
->usb_lock
;
1507 init_waitqueue_head(&gspca_dev
->wq
);
1509 /* Initialize the vb2 queue */
1510 q
= &gspca_dev
->queue
;
1511 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1512 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1513 q
->drv_priv
= gspca_dev
;
1514 q
->buf_struct_size
= sizeof(struct gspca_buffer
);
1515 q
->ops
= &gspca_qops
;
1516 q
->mem_ops
= &vb2_vmalloc_memops
;
1517 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1518 q
->min_buffers_needed
= 2;
1519 q
->lock
= &gspca_dev
->usb_lock
;
1520 ret
= vb2_queue_init(q
);
1523 gspca_dev
->vdev
.queue
= q
;
1525 INIT_LIST_HEAD(&gspca_dev
->buf_list
);
1526 spin_lock_init(&gspca_dev
->qlock
);
1528 /* configure the subdriver and initialize the USB device */
1529 ret
= sd_desc
->config(gspca_dev
, id
);
1532 ret
= sd_desc
->init(gspca_dev
);
1535 if (sd_desc
->init_controls
)
1536 ret
= sd_desc
->init_controls(gspca_dev
);
1539 gspca_set_default_mode(gspca_dev
);
1541 ret
= gspca_input_connect(gspca_dev
);
1545 #ifdef CONFIG_VIDEO_ADV_DEBUG
1546 if (!gspca_dev
->sd_desc
->get_register
)
1547 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_DBG_G_REGISTER
);
1548 if (!gspca_dev
->sd_desc
->set_register
)
1549 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_DBG_S_REGISTER
);
1551 if (!gspca_dev
->sd_desc
->get_jcomp
)
1552 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_G_JPEGCOMP
);
1553 if (!gspca_dev
->sd_desc
->set_jcomp
)
1554 v4l2_disable_ioctl(&gspca_dev
->vdev
, VIDIOC_S_JPEGCOMP
);
1556 /* init video stuff */
1557 ret
= video_register_device(&gspca_dev
->vdev
,
1561 pr_err("video_register_device err %d\n", ret
);
1565 usb_set_intfdata(intf
, gspca_dev
);
1566 gspca_dbg(gspca_dev
, D_PROBE
, "%s created\n",
1567 video_device_node_name(&gspca_dev
->vdev
));
1569 gspca_input_create_urb(gspca_dev
);
1573 #if IS_ENABLED(CONFIG_INPUT)
1574 if (gspca_dev
->input_dev
)
1575 input_unregister_device(gspca_dev
->input_dev
);
1577 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
1578 v4l2_device_unregister(&gspca_dev
->v4l2_dev
);
1579 kfree(gspca_dev
->usb_buf
);
1583 EXPORT_SYMBOL(gspca_dev_probe2
);
1585 /* same function as the previous one, but check the interface */
1586 int gspca_dev_probe(struct usb_interface
*intf
,
1587 const struct usb_device_id
*id
,
1588 const struct sd_desc
*sd_desc
,
1590 struct module
*module
)
1592 struct usb_device
*dev
= interface_to_usbdev(intf
);
1594 /* we don't handle multi-config cameras */
1595 if (dev
->descriptor
.bNumConfigurations
!= 1) {
1596 pr_err("%04x:%04x too many config\n",
1597 id
->idVendor
, id
->idProduct
);
1601 /* the USB video interface must be the first one */
1602 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1
1603 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
1606 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
1608 EXPORT_SYMBOL(gspca_dev_probe
);
1613 * This function must be called by the sub-driver
1614 * when the device disconnects, after the specific resources are freed.
1616 void gspca_disconnect(struct usb_interface
*intf
)
1618 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1619 #if IS_ENABLED(CONFIG_INPUT)
1620 struct input_dev
*input_dev
;
1623 gspca_dbg(gspca_dev
, D_PROBE
, "%s disconnect\n",
1624 video_device_node_name(&gspca_dev
->vdev
));
1626 mutex_lock(&gspca_dev
->usb_lock
);
1627 gspca_dev
->present
= false;
1628 destroy_urbs(gspca_dev
);
1629 gspca_input_destroy_urb(gspca_dev
);
1631 vb2_queue_error(&gspca_dev
->queue
);
1633 #if IS_ENABLED(CONFIG_INPUT)
1634 input_dev
= gspca_dev
->input_dev
;
1636 gspca_dev
->input_dev
= NULL
;
1637 input_unregister_device(input_dev
);
1641 v4l2_device_disconnect(&gspca_dev
->v4l2_dev
);
1642 video_unregister_device(&gspca_dev
->vdev
);
1644 mutex_unlock(&gspca_dev
->usb_lock
);
1646 /* (this will call gspca_release() immediately or on last close) */
1647 v4l2_device_put(&gspca_dev
->v4l2_dev
);
1649 EXPORT_SYMBOL(gspca_disconnect
);
1652 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
1654 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1656 gspca_input_destroy_urb(gspca_dev
);
1658 if (!vb2_start_streaming_called(&gspca_dev
->queue
))
1661 mutex_lock(&gspca_dev
->usb_lock
);
1662 gspca_dev
->frozen
= 1; /* avoid urb error messages */
1663 gspca_dev
->usb_err
= 0;
1664 if (gspca_dev
->sd_desc
->stopN
)
1665 gspca_dev
->sd_desc
->stopN(gspca_dev
);
1666 destroy_urbs(gspca_dev
);
1667 gspca_set_alt0(gspca_dev
);
1668 if (gspca_dev
->sd_desc
->stop0
)
1669 gspca_dev
->sd_desc
->stop0(gspca_dev
);
1670 mutex_unlock(&gspca_dev
->usb_lock
);
1674 EXPORT_SYMBOL(gspca_suspend
);
1676 int gspca_resume(struct usb_interface
*intf
)
1678 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
1679 int streaming
, ret
= 0;
1681 mutex_lock(&gspca_dev
->usb_lock
);
1682 gspca_dev
->frozen
= 0;
1683 gspca_dev
->usb_err
= 0;
1684 gspca_dev
->sd_desc
->init(gspca_dev
);
1686 * Most subdrivers send all ctrl values on sd_start and thus
1687 * only write to the device registers on s_ctrl when streaming ->
1688 * Clear streaming to avoid setting all ctrls twice.
1690 streaming
= vb2_start_streaming_called(&gspca_dev
->queue
);
1692 ret
= gspca_init_transfer(gspca_dev
);
1694 gspca_input_create_urb(gspca_dev
);
1695 mutex_unlock(&gspca_dev
->usb_lock
);
1699 EXPORT_SYMBOL(gspca_resume
);
1702 /* -- module insert / remove -- */
1703 static int __init
gspca_init(void)
1705 pr_info("v" GSPCA_VERSION
" registered\n");
1708 static void __exit
gspca_exit(void)
1712 module_init(gspca_init
);
1713 module_exit(gspca_exit
);
1715 module_param_named(debug
, gspca_debug
, int, 0644);
1716 MODULE_PARM_DESC(debug
,
1717 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");