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 defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
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 int gspca_debug
= D_ERR
| D_PROBE
;
65 EXPORT_SYMBOL(gspca_debug
);
67 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
69 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
70 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
74 (pixfmt
>> 16) & 0xff,
78 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
85 #define PDEBUG_MODE(txt, pixfmt, w, h)
88 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
89 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
90 #define GSPCA_MEMORY_READ 7
92 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
97 static void gspca_vm_open(struct vm_area_struct
*vma
)
99 struct gspca_frame
*frame
= vma
->vm_private_data
;
101 frame
->vma_use_count
++;
102 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
105 static void gspca_vm_close(struct vm_area_struct
*vma
)
107 struct gspca_frame
*frame
= vma
->vm_private_data
;
109 if (--frame
->vma_use_count
<= 0)
110 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
113 static const struct vm_operations_struct gspca_vm_ops
= {
114 .open
= gspca_vm_open
,
115 .close
= gspca_vm_close
,
119 * Input and interrupt endpoint handling functions
121 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
122 static void int_irq(struct urb
*urb
)
124 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
130 if (gspca_dev
->sd_desc
->int_pkt_scan(gspca_dev
,
131 urb
->transfer_buffer
, urb
->actual_length
) < 0) {
132 PDEBUG(D_ERR
, "Unknown packet received");
140 /* Stop is requested either by software or hardware is gone,
141 * keep the ret value non-zero and don't resubmit later.
146 PDEBUG(D_ERR
, "URB error %i, resubmitting", urb
->status
);
152 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
154 pr_err("Resubmit URB failed with error %i\n", ret
);
158 static int gspca_input_connect(struct gspca_dev
*dev
)
160 struct input_dev
*input_dev
;
163 dev
->input_dev
= NULL
;
164 if (dev
->sd_desc
->int_pkt_scan
|| dev
->sd_desc
->other_input
) {
165 input_dev
= input_allocate_device();
169 usb_make_path(dev
->dev
, dev
->phys
, sizeof(dev
->phys
));
170 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
172 input_dev
->name
= dev
->sd_desc
->name
;
173 input_dev
->phys
= dev
->phys
;
175 usb_to_input_id(dev
->dev
, &input_dev
->id
);
177 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
178 input_dev
->keybit
[BIT_WORD(KEY_CAMERA
)] = BIT_MASK(KEY_CAMERA
);
179 input_dev
->dev
.parent
= &dev
->dev
->dev
;
181 err
= input_register_device(input_dev
);
183 pr_err("Input device registration failed with error %i\n",
185 input_dev
->dev
.parent
= NULL
;
186 input_free_device(input_dev
);
188 dev
->input_dev
= input_dev
;
195 static int alloc_and_submit_int_urb(struct gspca_dev
*gspca_dev
,
196 struct usb_endpoint_descriptor
*ep
)
198 unsigned int buffer_len
;
201 struct usb_device
*dev
;
205 buffer_len
= le16_to_cpu(ep
->wMaxPacketSize
);
206 interval
= ep
->bInterval
;
207 PDEBUG(D_CONF
, "found int in endpoint: 0x%x, "
208 "buffer_len=%u, interval=%u",
209 ep
->bEndpointAddress
, buffer_len
, interval
);
211 dev
= gspca_dev
->dev
;
213 urb
= usb_alloc_urb(0, GFP_KERNEL
);
219 buffer
= usb_alloc_coherent(dev
, buffer_len
,
220 GFP_KERNEL
, &urb
->transfer_dma
);
225 usb_fill_int_urb(urb
, dev
,
226 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
228 int_irq
, (void *)gspca_dev
, interval
);
229 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
230 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
232 PDEBUG(D_ERR
, "submit int URB failed with error %i", ret
);
235 gspca_dev
->int_urb
= urb
;
239 usb_free_coherent(dev
,
240 urb
->transfer_buffer_length
,
241 urb
->transfer_buffer
,
249 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
251 struct usb_interface
*intf
;
252 struct usb_host_interface
*intf_desc
;
253 struct usb_endpoint_descriptor
*ep
;
256 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
257 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
258 intf_desc
= intf
->cur_altsetting
;
259 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
260 ep
= &intf_desc
->endpoint
[i
].desc
;
261 if (usb_endpoint_dir_in(ep
) &&
262 usb_endpoint_xfer_int(ep
)) {
264 alloc_and_submit_int_urb(gspca_dev
, ep
);
271 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
275 urb
= gspca_dev
->int_urb
;
277 gspca_dev
->int_urb
= NULL
;
279 usb_free_coherent(gspca_dev
->dev
,
280 urb
->transfer_buffer_length
,
281 urb
->transfer_buffer
,
287 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
291 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
295 static inline int gspca_input_connect(struct gspca_dev
*dev
)
302 * fill a video frame from an URB and resubmit
304 static void fill_frame(struct gspca_dev
*gspca_dev
,
307 u8
*data
; /* address of data in the iso message */
311 if (urb
->status
!= 0) {
312 if (urb
->status
== -ESHUTDOWN
)
313 return; /* disconnection */
315 if (gspca_dev
->frozen
)
318 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
322 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
323 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
324 len
= urb
->iso_frame_desc
[i
].actual_length
;
326 /* check the packet status and length */
327 st
= urb
->iso_frame_desc
[i
].status
;
329 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
331 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
335 if (gspca_dev
->empty_packet
== 0)
336 gspca_dev
->empty_packet
= 1;
340 /* let the packet be analyzed by the subdriver */
341 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
342 i
, urb
->iso_frame_desc
[i
].offset
, len
);
343 data
= (u8
*) urb
->transfer_buffer
344 + urb
->iso_frame_desc
[i
].offset
;
345 pkt_scan(gspca_dev
, data
, len
);
349 /* resubmit the URB */
350 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
352 pr_err("usb_submit_urb() ret %d\n", st
);
356 * ISOC message interrupt from the USB device
358 * Analyse each packet and call the subdriver for copy to the frame buffer.
360 static void isoc_irq(struct urb
*urb
)
362 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
364 PDEBUG(D_PACK
, "isoc irq");
365 if (!gspca_dev
->streaming
)
367 fill_frame(gspca_dev
, urb
);
371 * bulk message interrupt from the USB device
373 static void bulk_irq(struct urb
*urb
)
375 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
378 PDEBUG(D_PACK
, "bulk irq");
379 if (!gspca_dev
->streaming
)
381 switch (urb
->status
) {
385 return; /* disconnection */
388 if (gspca_dev
->frozen
)
391 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
396 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
397 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
398 urb
->transfer_buffer
,
402 /* resubmit the URB */
403 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
404 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
406 pr_err("usb_submit_urb() ret %d\n", st
);
411 * add data to the current frame
413 * This function is called by the subdrivers at interrupt level.
415 * To build a frame, these ones must add
417 * - 0 or many INTER_PACKETs
419 * DISCARD_PACKET invalidates the whole frame.
421 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
422 enum gspca_packet_type packet_type
,
426 struct gspca_frame
*frame
;
429 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
431 if (packet_type
== FIRST_PACKET
) {
432 i
= atomic_read(&gspca_dev
->fr_i
);
434 /* if there are no queued buffer, discard the whole frame */
435 if (i
== atomic_read(&gspca_dev
->fr_q
)) {
436 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
437 gspca_dev
->sequence
++;
440 j
= gspca_dev
->fr_queue
[i
];
441 frame
= &gspca_dev
->frame
[j
];
442 frame
->v4l2_buf
.timestamp
= ktime_to_timeval(ktime_get());
443 frame
->v4l2_buf
.sequence
= gspca_dev
->sequence
++;
444 gspca_dev
->image
= frame
->data
;
445 gspca_dev
->image_len
= 0;
447 switch (gspca_dev
->last_packet_type
) {
449 if (packet_type
== LAST_PACKET
) {
450 gspca_dev
->last_packet_type
= packet_type
;
451 gspca_dev
->image
= NULL
;
452 gspca_dev
->image_len
= 0;
460 /* append the packet to the frame buffer */
462 if (gspca_dev
->image_len
+ len
> gspca_dev
->frsz
) {
463 PDEBUG(D_ERR
|D_PACK
, "frame overflow %d > %d",
464 gspca_dev
->image_len
+ len
,
466 packet_type
= DISCARD_PACKET
;
468 /* !! image is NULL only when last pkt is LAST or DISCARD
469 if (gspca_dev->image == NULL) {
470 pr_err("gspca_frame_add() image == NULL\n");
474 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
476 gspca_dev
->image_len
+= len
;
479 gspca_dev
->last_packet_type
= packet_type
;
481 /* if last packet, invalidate packet concatenation until
482 * next first packet, wake up the application and advance
484 if (packet_type
== LAST_PACKET
) {
485 i
= atomic_read(&gspca_dev
->fr_i
);
486 j
= gspca_dev
->fr_queue
[i
];
487 frame
= &gspca_dev
->frame
[j
];
488 frame
->v4l2_buf
.bytesused
= gspca_dev
->image_len
;
489 frame
->v4l2_buf
.flags
= (frame
->v4l2_buf
.flags
490 | V4L2_BUF_FLAG_DONE
)
491 & ~V4L2_BUF_FLAG_QUEUED
;
492 i
= (i
+ 1) % GSPCA_MAX_FRAMES
;
493 atomic_set(&gspca_dev
->fr_i
, i
);
494 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
495 PDEBUG(D_FRAM
, "frame complete len:%d",
496 frame
->v4l2_buf
.bytesused
);
497 gspca_dev
->image
= NULL
;
498 gspca_dev
->image_len
= 0;
501 EXPORT_SYMBOL(gspca_frame_add
);
503 static int frame_alloc(struct gspca_dev
*gspca_dev
, struct file
*file
,
504 enum v4l2_memory memory
, unsigned int count
)
506 struct gspca_frame
*frame
;
510 i
= gspca_dev
->curr_mode
;
511 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
512 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
513 frsz
= PAGE_ALIGN(frsz
);
514 if (count
>= GSPCA_MAX_FRAMES
)
515 count
= GSPCA_MAX_FRAMES
- 1;
516 gspca_dev
->frbuf
= vmalloc_32(frsz
* count
);
517 if (!gspca_dev
->frbuf
) {
518 pr_err("frame alloc failed\n");
521 gspca_dev
->capt_file
= file
;
522 gspca_dev
->memory
= memory
;
523 gspca_dev
->frsz
= frsz
;
524 gspca_dev
->nframes
= count
;
525 for (i
= 0; i
< count
; i
++) {
526 frame
= &gspca_dev
->frame
[i
];
527 frame
->v4l2_buf
.index
= i
;
528 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
529 frame
->v4l2_buf
.flags
= 0;
530 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
531 frame
->v4l2_buf
.length
= frsz
;
532 frame
->v4l2_buf
.memory
= memory
;
533 frame
->v4l2_buf
.sequence
= 0;
534 frame
->data
= gspca_dev
->frbuf
+ i
* frsz
;
535 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
537 atomic_set(&gspca_dev
->fr_q
, 0);
538 atomic_set(&gspca_dev
->fr_i
, 0);
543 static void frame_free(struct gspca_dev
*gspca_dev
)
547 PDEBUG(D_STREAM
, "frame free");
548 if (gspca_dev
->frbuf
!= NULL
) {
549 vfree(gspca_dev
->frbuf
);
550 gspca_dev
->frbuf
= NULL
;
551 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
552 gspca_dev
->frame
[i
].data
= NULL
;
554 gspca_dev
->nframes
= 0;
556 gspca_dev
->capt_file
= NULL
;
557 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
560 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
565 PDEBUG(D_STREAM
, "kill transfer");
566 for (i
= 0; i
< MAX_NURBS
; i
++) {
567 urb
= gspca_dev
->urb
[i
];
571 gspca_dev
->urb
[i
] = NULL
;
573 if (urb
->transfer_buffer
!= NULL
)
574 usb_free_coherent(gspca_dev
->dev
,
575 urb
->transfer_buffer_length
,
576 urb
->transfer_buffer
,
582 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
586 if (gspca_dev
->alt
== 0)
588 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
590 pr_err("set alt 0 err %d\n", ret
);
594 /* Note: both the queue and the usb locks should be held when calling this */
595 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
597 gspca_dev
->streaming
= 0;
598 gspca_dev
->usb_err
= 0;
599 if (gspca_dev
->sd_desc
->stopN
)
600 gspca_dev
->sd_desc
->stopN(gspca_dev
);
601 destroy_urbs(gspca_dev
);
602 gspca_input_destroy_urb(gspca_dev
);
603 gspca_set_alt0(gspca_dev
);
604 gspca_input_create_urb(gspca_dev
);
605 if (gspca_dev
->sd_desc
->stop0
)
606 gspca_dev
->sd_desc
->stop0(gspca_dev
);
607 PDEBUG(D_STREAM
, "stream off OK");
611 * look for an input transfer endpoint in an alternate setting
613 static struct usb_host_endpoint
*alt_xfer(struct usb_host_interface
*alt
,
616 struct usb_host_endpoint
*ep
;
619 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
620 ep
= &alt
->endpoint
[i
];
621 attr
= ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
623 && ep
->desc
.wMaxPacketSize
!= 0
624 && usb_endpoint_dir_in(&ep
->desc
))
630 /* compute the minimum bandwidth for the current transfer */
631 static u32
which_bandwidth(struct gspca_dev
*gspca_dev
)
636 /* get the (max) image size */
637 i
= gspca_dev
->curr_mode
;
638 bandwidth
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
640 /* if the image is compressed, estimate its mean size */
641 if (!gspca_dev
->cam
.needs_full_bandwidth
&&
642 bandwidth
< gspca_dev
->cam
.cam_mode
[i
].width
*
643 gspca_dev
->cam
.cam_mode
[i
].height
)
644 bandwidth
= bandwidth
* 3 / 8; /* 0.375 */
646 /* estimate the frame rate */
647 if (gspca_dev
->sd_desc
->get_streamparm
) {
648 struct v4l2_streamparm parm
;
650 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, &parm
);
651 bandwidth
*= parm
.parm
.capture
.timeperframe
.denominator
;
652 bandwidth
/= parm
.parm
.capture
.timeperframe
.numerator
;
655 /* don't hope more than 15 fps with USB 1.1 and
656 * image resolution >= 640x480 */
657 if (gspca_dev
->width
>= 640
658 && gspca_dev
->dev
->speed
== USB_SPEED_FULL
)
659 bandwidth
*= 15; /* 15 fps */
661 bandwidth
*= 30; /* 30 fps */
664 PDEBUG(D_STREAM
, "min bandwidth: %d", bandwidth
);
676 * build the table of the endpoints
677 * and compute the minimum bandwidth for the image transfer
679 static int build_isoc_ep_tb(struct gspca_dev
*gspca_dev
,
680 struct usb_interface
*intf
,
681 struct ep_tb_s
*ep_tb
)
683 struct usb_host_endpoint
*ep
;
684 int i
, j
, nbalt
, psize
, found
;
685 u32 bandwidth
, last_bw
;
687 nbalt
= intf
->num_altsetting
;
689 nbalt
= MAX_ALT
; /* fixme: should warn */
691 /* build the endpoint table */
695 ep_tb
->bandwidth
= 2000 * 2000 * 120;
697 for (j
= 0; j
< nbalt
; j
++) {
698 ep
= alt_xfer(&intf
->altsetting
[j
],
699 USB_ENDPOINT_XFER_ISOC
);
702 if (ep
->desc
.bInterval
== 0) {
703 pr_err("alt %d iso endp with 0 interval\n", j
);
706 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
707 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
708 bandwidth
= psize
* 1000;
709 if (gspca_dev
->dev
->speed
== USB_SPEED_HIGH
710 || gspca_dev
->dev
->speed
== USB_SPEED_SUPER
)
712 bandwidth
/= 1 << (ep
->desc
.bInterval
- 1);
713 if (bandwidth
<= last_bw
)
715 if (bandwidth
< ep_tb
->bandwidth
) {
716 ep_tb
->bandwidth
= bandwidth
;
723 PDEBUG(D_STREAM
, "alt %d bandwidth %d",
724 ep_tb
->alt
, ep_tb
->bandwidth
);
725 last_bw
= ep_tb
->bandwidth
;
732 * has a usb audio class interface (a built in usb mic); and
733 * is a usb 1 full speed device; and
734 * uses the max full speed iso bandwidth; and
735 * and has more than 1 alt setting
736 * then skip the highest alt setting to spare bandwidth for the mic
738 if (gspca_dev
->audio
&&
739 gspca_dev
->dev
->speed
== USB_SPEED_FULL
&&
740 last_bw
>= 1000000 &&
742 PDEBUG(D_STREAM
, "dev has usb audio, skipping highest alt");
747 /* get the requested bandwidth and start at the highest atlsetting */
748 bandwidth
= which_bandwidth(gspca_dev
);
752 if (ep_tb
->bandwidth
< bandwidth
)
760 * create the URBs for image transfer
762 static int create_urbs(struct gspca_dev
*gspca_dev
,
763 struct usb_host_endpoint
*ep
)
766 int n
, nurbs
, i
, psize
, npkt
, bsize
;
768 /* calculate the packet size and the number of packets */
769 psize
= le16_to_cpu(ep
->desc
.wMaxPacketSize
);
771 if (!gspca_dev
->cam
.bulk
) { /* isoc */
773 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
774 if (gspca_dev
->pkt_size
== 0)
775 psize
= (psize
& 0x07ff) * (1 + ((psize
>> 11) & 3));
777 psize
= gspca_dev
->pkt_size
;
778 npkt
= gspca_dev
->cam
.npkt
;
780 npkt
= 32; /* default value */
781 bsize
= psize
* npkt
;
783 "isoc %d pkts size %d = bsize:%d",
788 bsize
= gspca_dev
->cam
.bulk_size
;
791 PDEBUG(D_STREAM
, "bulk bsize:%d", bsize
);
792 if (gspca_dev
->cam
.bulk_nurbs
!= 0)
793 nurbs
= gspca_dev
->cam
.bulk_nurbs
;
798 for (n
= 0; n
< nurbs
; n
++) {
799 urb
= usb_alloc_urb(npkt
, GFP_KERNEL
);
801 pr_err("usb_alloc_urb failed\n");
804 gspca_dev
->urb
[n
] = urb
;
805 urb
->transfer_buffer
= usb_alloc_coherent(gspca_dev
->dev
,
810 if (urb
->transfer_buffer
== NULL
) {
811 pr_err("usb_alloc_coherent failed\n");
814 urb
->dev
= gspca_dev
->dev
;
815 urb
->context
= gspca_dev
;
816 urb
->transfer_buffer_length
= bsize
;
817 if (npkt
!= 0) { /* ISOC */
818 urb
->pipe
= usb_rcvisocpipe(gspca_dev
->dev
,
819 ep
->desc
.bEndpointAddress
);
820 urb
->transfer_flags
= URB_ISO_ASAP
821 | URB_NO_TRANSFER_DMA_MAP
;
822 urb
->interval
= 1 << (ep
->desc
.bInterval
- 1);
823 urb
->complete
= isoc_irq
;
824 urb
->number_of_packets
= npkt
;
825 for (i
= 0; i
< npkt
; i
++) {
826 urb
->iso_frame_desc
[i
].length
= psize
;
827 urb
->iso_frame_desc
[i
].offset
= psize
* i
;
830 urb
->pipe
= usb_rcvbulkpipe(gspca_dev
->dev
,
831 ep
->desc
.bEndpointAddress
);
832 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
833 urb
->complete
= bulk_irq
;
840 * start the USB transfer
842 static int gspca_init_transfer(struct gspca_dev
*gspca_dev
)
844 struct usb_interface
*intf
;
845 struct usb_host_endpoint
*ep
;
847 struct ep_tb_s ep_tb
[MAX_ALT
];
848 int n
, ret
, xfer
, alt
, alt_idx
;
850 /* reset the streaming variables */
851 gspca_dev
->image
= NULL
;
852 gspca_dev
->image_len
= 0;
853 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
854 gspca_dev
->sequence
= 0;
856 gspca_dev
->usb_err
= 0;
858 /* do the specific subdriver stuff before endpoint selection */
859 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
860 gspca_dev
->alt
= gspca_dev
->cam
.bulk
? intf
->num_altsetting
: 0;
861 if (gspca_dev
->sd_desc
->isoc_init
) {
862 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
866 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
867 : USB_ENDPOINT_XFER_ISOC
;
869 /* if bulk or the subdriver forced an altsetting, get the endpoint */
870 if (gspca_dev
->alt
!= 0) {
871 gspca_dev
->alt
--; /* (previous version compatibility) */
872 ep
= alt_xfer(&intf
->altsetting
[gspca_dev
->alt
], xfer
);
874 pr_err("bad altsetting %d\n", gspca_dev
->alt
);
877 ep_tb
[0].alt
= gspca_dev
->alt
;
881 /* else, compute the minimum bandwidth
882 * and build the endpoint table */
883 alt_idx
= build_isoc_ep_tb(gspca_dev
, intf
, ep_tb
);
885 pr_err("no transfer endpoint found\n");
890 /* set the highest alternate setting and
891 * loop until urb submit succeeds */
892 gspca_input_destroy_urb(gspca_dev
);
894 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
897 if (alt
!= gspca_dev
->alt
) {
898 alt
= gspca_dev
->alt
;
899 if (intf
->num_altsetting
> 1) {
900 ret
= usb_set_interface(gspca_dev
->dev
,
905 goto retry
; /*fixme: ugly*/
906 pr_err("set alt %d err %d\n", alt
, ret
);
911 if (!gspca_dev
->cam
.no_urb_create
) {
912 PDEBUG(D_STREAM
, "init transfer alt %d", alt
);
913 ret
= create_urbs(gspca_dev
,
914 alt_xfer(&intf
->altsetting
[alt
], xfer
));
916 destroy_urbs(gspca_dev
);
921 /* clear the bulk endpoint */
922 if (gspca_dev
->cam
.bulk
)
923 usb_clear_halt(gspca_dev
->dev
,
924 gspca_dev
->urb
[0]->pipe
);
927 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
929 destroy_urbs(gspca_dev
);
932 gspca_dev
->streaming
= 1;
934 /* some bulk transfers are started by the subdriver */
935 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
938 /* submit the URBs */
939 for (n
= 0; n
< MAX_NURBS
; n
++) {
940 urb
= gspca_dev
->urb
[n
];
943 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
948 break; /* transfer is started */
950 /* something when wrong
951 * stop the webcam and free the transfer resources */
952 gspca_stream_off(gspca_dev
);
953 if (ret
!= -ENOSPC
) {
954 pr_err("usb_submit_urb alt %d err %d\n",
955 gspca_dev
->alt
, ret
);
959 /* the bandwidth is not wide enough
960 * negotiate or try a lower alternate setting */
962 PDEBUG(D_ERR
|D_STREAM
,
963 "alt %d - bandwidth not wide enough - trying again",
965 msleep(20); /* wait for kill complete */
966 if (gspca_dev
->sd_desc
->isoc_nego
) {
967 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
972 pr_err("no transfer endpoint found\n");
976 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
980 gspca_input_create_urb(gspca_dev
);
984 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
986 struct gspca_ctrl
*ctrl
;
989 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
990 gspca_dev
->curr_mode
= i
;
991 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
992 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
993 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
995 /* set the current control values to their default values
996 * which may have changed in sd_init() */
997 /* does nothing if ctrl_handler == NULL */
998 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
999 ctrl
= gspca_dev
->cam
.ctrls
;
1002 i
< gspca_dev
->sd_desc
->nctrls
;
1004 ctrl
->val
= ctrl
->def
;
1008 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
1009 int width
, int height
)
1013 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
1014 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
1015 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
1022 * search a mode with the right pixel format
1024 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
1030 modeU
= modeD
= mode
;
1031 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
1033 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
1037 if (++modeU
< gspca_dev
->cam
.nmodes
) {
1038 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
1046 #ifdef CONFIG_VIDEO_ADV_DEBUG
1047 static int vidioc_g_register(struct file
*file
, void *priv
,
1048 struct v4l2_dbg_register
*reg
)
1050 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1052 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1055 if (!gspca_dev
->sd_desc
->get_register
)
1058 gspca_dev
->usb_err
= 0;
1059 return gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
1062 static int vidioc_s_register(struct file
*file
, void *priv
,
1063 struct v4l2_dbg_register
*reg
)
1065 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1067 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1070 if (!gspca_dev
->sd_desc
->set_register
)
1073 gspca_dev
->usb_err
= 0;
1074 return gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
1078 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1079 struct v4l2_dbg_chip_ident
*chip
)
1081 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1083 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1086 gspca_dev
->usb_err
= 0;
1087 return gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
1090 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1091 struct v4l2_fmtdesc
*fmtdesc
)
1093 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1097 /* give an index to each format */
1100 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1101 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1104 if (fmt_tb
[j
] == fmt_tb
[index
])
1109 if (fmtdesc
->index
== index
)
1110 break; /* new format */
1112 if (index
>= ARRAY_SIZE(fmt_tb
))
1117 return -EINVAL
; /* no more format */
1119 fmtdesc
->pixelformat
= fmt_tb
[index
];
1120 if (gspca_dev
->cam
.cam_mode
[i
].sizeimage
<
1121 gspca_dev
->cam
.cam_mode
[i
].width
*
1122 gspca_dev
->cam
.cam_mode
[i
].height
)
1123 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1124 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1125 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1126 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1127 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1128 fmtdesc
->description
[4] = '\0';
1132 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1133 struct v4l2_format
*fmt
)
1135 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1138 mode
= gspca_dev
->curr_mode
;
1139 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1140 sizeof fmt
->fmt
.pix
);
1144 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1145 struct v4l2_format
*fmt
)
1147 int w
, h
, mode
, mode2
;
1149 w
= fmt
->fmt
.pix
.width
;
1150 h
= fmt
->fmt
.pix
.height
;
1153 if (gspca_debug
& D_CONF
)
1154 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
1156 /* search the closest mode for width and height */
1157 mode
= wxh_to_mode(gspca_dev
, w
, h
);
1159 /* OK if right palette */
1160 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1161 != fmt
->fmt
.pix
.pixelformat
) {
1163 /* else, search the closest mode with the same pixel format */
1164 mode2
= gspca_get_mode(gspca_dev
, mode
,
1165 fmt
->fmt
.pix
.pixelformat
);
1169 ; * no chance, return this mode */
1171 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1172 sizeof fmt
->fmt
.pix
);
1173 return mode
; /* used when s_fmt */
1176 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1178 struct v4l2_format
*fmt
)
1180 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1183 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1189 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1190 struct v4l2_format
*fmt
)
1192 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1195 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1196 return -ERESTARTSYS
;
1198 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1202 if (gspca_dev
->nframes
!= 0
1203 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1208 if (ret
== gspca_dev
->curr_mode
) {
1210 goto out
; /* same mode */
1213 if (gspca_dev
->streaming
) {
1217 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
1218 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
1219 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
1220 gspca_dev
->curr_mode
= ret
;
1224 mutex_unlock(&gspca_dev
->queue_lock
);
1228 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1229 struct v4l2_frmsizeenum
*fsize
)
1231 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1235 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1236 if (fsize
->pixel_format
!=
1237 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1240 if (fsize
->index
== index
) {
1241 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1242 fsize
->discrete
.width
=
1243 gspca_dev
->cam
.cam_mode
[i
].width
;
1244 fsize
->discrete
.height
=
1245 gspca_dev
->cam
.cam_mode
[i
].height
;
1254 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1255 struct v4l2_frmivalenum
*fival
)
1257 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1258 int mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1261 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1262 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1265 if (fival
->pixel_format
!=
1266 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1269 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1270 if (fival
->index
== i
) {
1271 fival
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1272 fival
->discrete
.numerator
= 1;
1273 fival
->discrete
.denominator
=
1274 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1282 static void gspca_release(struct v4l2_device
*v4l2_device
)
1284 struct gspca_dev
*gspca_dev
=
1285 container_of(v4l2_device
, struct gspca_dev
, v4l2_dev
);
1287 PDEBUG(D_PROBE
, "%s released",
1288 video_device_node_name(&gspca_dev
->vdev
));
1290 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
1291 v4l2_device_unregister(&gspca_dev
->v4l2_dev
);
1292 kfree(gspca_dev
->usb_buf
);
1296 static int dev_open(struct file
*file
)
1298 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1300 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1302 /* protect the subdriver against rmmod */
1303 if (!try_module_get(gspca_dev
->module
))
1307 /* activate the v4l2 debug */
1308 if (gspca_debug
& D_V4L2
)
1309 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1310 | V4L2_DEBUG_IOCTL_ARG
;
1312 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1313 | V4L2_DEBUG_IOCTL_ARG
);
1315 return v4l2_fh_open(file
);
1318 static int dev_close(struct file
*file
)
1320 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1322 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1324 /* Needed for gspca_stream_off, always lock before queue_lock! */
1325 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1326 return -ERESTARTSYS
;
1328 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
)) {
1329 mutex_unlock(&gspca_dev
->usb_lock
);
1330 return -ERESTARTSYS
;
1333 /* if the file did the capture, free the streaming resources */
1334 if (gspca_dev
->capt_file
== file
) {
1335 if (gspca_dev
->streaming
)
1336 gspca_stream_off(gspca_dev
);
1337 frame_free(gspca_dev
);
1339 module_put(gspca_dev
->module
);
1340 mutex_unlock(&gspca_dev
->queue_lock
);
1341 mutex_unlock(&gspca_dev
->usb_lock
);
1343 PDEBUG(D_STREAM
, "close done");
1345 return v4l2_fh_release(file
);
1348 static int vidioc_querycap(struct file
*file
, void *priv
,
1349 struct v4l2_capability
*cap
)
1351 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1353 strlcpy((char *) cap
->driver
, gspca_dev
->sd_desc
->name
,
1354 sizeof cap
->driver
);
1355 if (gspca_dev
->dev
->product
!= NULL
) {
1356 strlcpy((char *) cap
->card
, gspca_dev
->dev
->product
,
1359 snprintf((char *) cap
->card
, sizeof cap
->card
,
1360 "USB Camera (%04x:%04x)",
1361 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1362 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1364 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1365 sizeof(cap
->bus_info
));
1366 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
1367 | V4L2_CAP_STREAMING
1368 | V4L2_CAP_READWRITE
;
1369 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1373 static int get_ctrl(struct gspca_dev
*gspca_dev
,
1376 const struct ctrl
*ctrls
;
1379 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1380 i
< gspca_dev
->sd_desc
->nctrls
;
1382 if (gspca_dev
->ctrl_dis
& (1 << i
))
1384 if (id
== ctrls
->qctrl
.id
)
1390 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1391 struct v4l2_queryctrl
*q_ctrl
)
1393 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1394 const struct ctrl
*ctrls
;
1395 struct gspca_ctrl
*gspca_ctrl
;
1400 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1401 id
&= V4L2_CTRL_ID_MASK
;
1404 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1405 if (gspca_dev
->ctrl_dis
& (1 << i
))
1407 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1410 && gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1411 > gspca_dev
->sd_desc
->ctrls
[idx
].qctrl
.id
)
1416 idx
= get_ctrl(gspca_dev
, id
);
1420 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1421 memcpy(q_ctrl
, &ctrls
->qctrl
, sizeof *q_ctrl
);
1422 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1423 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1424 q_ctrl
->default_value
= gspca_ctrl
->def
;
1425 q_ctrl
->minimum
= gspca_ctrl
->min
;
1426 q_ctrl
->maximum
= gspca_ctrl
->max
;
1428 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1429 q_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1433 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1434 struct v4l2_control
*ctrl
)
1436 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1437 const struct ctrl
*ctrls
;
1438 struct gspca_ctrl
*gspca_ctrl
;
1441 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1444 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1446 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1447 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1448 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1449 if (ctrl
->value
< gspca_ctrl
->min
1450 || ctrl
->value
> gspca_ctrl
->max
)
1454 if (ctrl
->value
< ctrls
->qctrl
.minimum
1455 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1458 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1459 gspca_dev
->usb_err
= 0;
1460 if (ctrls
->set
!= NULL
)
1461 return ctrls
->set(gspca_dev
, ctrl
->value
);
1462 if (gspca_ctrl
!= NULL
) {
1463 gspca_ctrl
->val
= ctrl
->value
;
1464 if (ctrls
->set_control
!= NULL
1465 && gspca_dev
->streaming
)
1466 ctrls
->set_control(gspca_dev
);
1468 return gspca_dev
->usb_err
;
1471 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1472 struct v4l2_control
*ctrl
)
1474 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1475 const struct ctrl
*ctrls
;
1478 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1481 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1483 gspca_dev
->usb_err
= 0;
1484 if (ctrls
->get
!= NULL
)
1485 return ctrls
->get(gspca_dev
, &ctrl
->value
);
1486 if (gspca_dev
->cam
.ctrls
!= NULL
)
1487 ctrl
->value
= gspca_dev
->cam
.ctrls
[idx
].val
;
1491 static int vidioc_querymenu(struct file
*file
, void *priv
,
1492 struct v4l2_querymenu
*qmenu
)
1494 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1496 if (!gspca_dev
->sd_desc
->querymenu
)
1498 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1501 static int vidioc_enum_input(struct file
*file
, void *priv
,
1502 struct v4l2_input
*input
)
1504 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1506 if (input
->index
!= 0)
1508 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1509 input
->status
= gspca_dev
->cam
.input_flags
;
1510 strlcpy(input
->name
, gspca_dev
->sd_desc
->name
,
1511 sizeof input
->name
);
1515 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1521 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1528 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1529 struct v4l2_requestbuffers
*rb
)
1531 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1532 int i
, ret
= 0, streaming
;
1534 i
= rb
->memory
; /* (avoid compilation warning) */
1536 case GSPCA_MEMORY_READ
: /* (internal call) */
1537 case V4L2_MEMORY_MMAP
:
1538 case V4L2_MEMORY_USERPTR
:
1543 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1544 return -ERESTARTSYS
;
1546 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1547 && gspca_dev
->memory
!= GSPCA_MEMORY_READ
1548 && gspca_dev
->memory
!= rb
->memory
) {
1553 /* only one file may do the capture */
1554 if (gspca_dev
->capt_file
!= NULL
1555 && gspca_dev
->capt_file
!= file
) {
1560 /* if allocated, the buffers must not be mapped */
1561 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1562 if (gspca_dev
->frame
[i
].vma_use_count
) {
1568 /* stop streaming */
1569 streaming
= gspca_dev
->streaming
;
1571 gspca_stream_off(gspca_dev
);
1573 /* Don't restart the stream when switching from read
1575 if (gspca_dev
->memory
== GSPCA_MEMORY_READ
)
1579 /* free the previous allocated buffers, if any */
1580 if (gspca_dev
->nframes
!= 0)
1581 frame_free(gspca_dev
);
1582 if (rb
->count
== 0) /* unrequest */
1584 ret
= frame_alloc(gspca_dev
, file
, rb
->memory
, rb
->count
);
1586 rb
->count
= gspca_dev
->nframes
;
1588 ret
= gspca_init_transfer(gspca_dev
);
1591 mutex_unlock(&gspca_dev
->queue_lock
);
1592 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1596 static int vidioc_querybuf(struct file
*file
, void *priv
,
1597 struct v4l2_buffer
*v4l2_buf
)
1599 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1600 struct gspca_frame
*frame
;
1602 if (v4l2_buf
->index
< 0
1603 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1606 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1607 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1611 static int vidioc_streamon(struct file
*file
, void *priv
,
1612 enum v4l2_buf_type buf_type
)
1614 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1617 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1619 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1620 return -ERESTARTSYS
;
1622 /* check the capture file */
1623 if (gspca_dev
->capt_file
!= file
) {
1628 if (gspca_dev
->nframes
== 0
1629 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1633 if (!gspca_dev
->streaming
) {
1634 ret
= gspca_init_transfer(gspca_dev
);
1639 if (gspca_debug
& D_STREAM
) {
1640 PDEBUG_MODE("stream on OK",
1648 mutex_unlock(&gspca_dev
->queue_lock
);
1652 static int vidioc_streamoff(struct file
*file
, void *priv
,
1653 enum v4l2_buf_type buf_type
)
1655 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1658 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1661 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1662 return -ERESTARTSYS
;
1664 if (!gspca_dev
->streaming
) {
1669 /* check the capture file */
1670 if (gspca_dev
->capt_file
!= file
) {
1675 /* stop streaming */
1676 gspca_stream_off(gspca_dev
);
1677 /* In case another thread is waiting in dqbuf */
1678 wake_up_interruptible(&gspca_dev
->wq
);
1680 /* empty the transfer queues */
1681 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
1682 gspca_dev
->frame
[i
].v4l2_buf
.flags
&= ~BUF_ALL_FLAGS
;
1683 atomic_set(&gspca_dev
->fr_q
, 0);
1684 atomic_set(&gspca_dev
->fr_i
, 0);
1685 gspca_dev
->fr_o
= 0;
1688 mutex_unlock(&gspca_dev
->queue_lock
);
1692 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1693 struct v4l2_jpegcompression
*jpegcomp
)
1695 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1697 if (!gspca_dev
->sd_desc
->get_jcomp
)
1699 gspca_dev
->usb_err
= 0;
1700 return gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1703 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1704 struct v4l2_jpegcompression
*jpegcomp
)
1706 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1708 if (!gspca_dev
->sd_desc
->set_jcomp
)
1710 gspca_dev
->usb_err
= 0;
1711 return gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1714 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1715 struct v4l2_streamparm
*parm
)
1717 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1719 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1721 if (gspca_dev
->sd_desc
->get_streamparm
) {
1722 gspca_dev
->usb_err
= 0;
1723 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1724 return gspca_dev
->usb_err
;
1729 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1730 struct v4l2_streamparm
*parm
)
1732 struct gspca_dev
*gspca_dev
= video_drvdata(filp
);
1735 n
= parm
->parm
.capture
.readbuffers
;
1736 if (n
== 0 || n
>= GSPCA_MAX_FRAMES
)
1737 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1739 gspca_dev
->nbufread
= n
;
1741 if (gspca_dev
->sd_desc
->set_streamparm
) {
1742 gspca_dev
->usb_err
= 0;
1743 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1744 return gspca_dev
->usb_err
;
1750 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1752 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1753 struct gspca_frame
*frame
;
1755 unsigned long addr
, start
, size
;
1758 start
= vma
->vm_start
;
1759 size
= vma
->vm_end
- vma
->vm_start
;
1760 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1762 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1763 return -ERESTARTSYS
;
1764 if (gspca_dev
->capt_file
!= file
) {
1770 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1771 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1772 PDEBUG(D_STREAM
, "mmap bad memory type");
1775 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1777 frame
= &gspca_dev
->frame
[i
];
1781 if (frame
== NULL
) {
1782 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1786 if (size
!= frame
->v4l2_buf
.length
) {
1787 PDEBUG(D_STREAM
, "mmap bad size");
1793 * - VM_IO marks the area as being a mmaped region for I/O to a
1794 * device. It also prevents the region from being core dumped.
1796 vma
->vm_flags
|= VM_IO
;
1798 addr
= (unsigned long) frame
->data
;
1800 page
= vmalloc_to_page((void *) addr
);
1801 ret
= vm_insert_page(vma
, start
, page
);
1809 vma
->vm_ops
= &gspca_vm_ops
;
1810 vma
->vm_private_data
= frame
;
1814 mutex_unlock(&gspca_dev
->queue_lock
);
1818 static int frame_ready_nolock(struct gspca_dev
*gspca_dev
, struct file
*file
,
1819 enum v4l2_memory memory
)
1821 if (!gspca_dev
->present
)
1823 if (gspca_dev
->capt_file
!= file
|| gspca_dev
->memory
!= memory
||
1824 !gspca_dev
->streaming
)
1827 /* check if a frame is ready */
1828 return gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
);
1831 static int frame_ready(struct gspca_dev
*gspca_dev
, struct file
*file
,
1832 enum v4l2_memory memory
)
1836 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1837 return -ERESTARTSYS
;
1838 ret
= frame_ready_nolock(gspca_dev
, file
, memory
);
1839 mutex_unlock(&gspca_dev
->queue_lock
);
1844 * dequeue a video buffer
1846 * If nonblock_ing is false, block until a buffer is available.
1848 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1849 struct v4l2_buffer
*v4l2_buf
)
1851 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1852 struct gspca_frame
*frame
;
1855 PDEBUG(D_FRAM
, "dqbuf");
1857 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1858 return -ERESTARTSYS
;
1861 ret
= frame_ready_nolock(gspca_dev
, file
, v4l2_buf
->memory
);
1867 mutex_unlock(&gspca_dev
->queue_lock
);
1869 if (file
->f_flags
& O_NONBLOCK
)
1872 /* wait till a frame is ready */
1873 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1874 frame_ready(gspca_dev
, file
, v4l2_buf
->memory
),
1875 msecs_to_jiffies(3000));
1881 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1882 return -ERESTARTSYS
;
1885 i
= gspca_dev
->fr_o
;
1886 j
= gspca_dev
->fr_queue
[i
];
1887 frame
= &gspca_dev
->frame
[j
];
1889 gspca_dev
->fr_o
= (i
+ 1) % GSPCA_MAX_FRAMES
;
1891 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
1892 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1893 PDEBUG(D_FRAM
, "dqbuf %d", j
);
1896 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
1897 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
1899 frame
->v4l2_buf
.bytesused
)) {
1900 PDEBUG(D_ERR
|D_STREAM
,
1901 "dqbuf cp to user failed");
1906 mutex_unlock(&gspca_dev
->queue_lock
);
1908 if (ret
== 0 && gspca_dev
->sd_desc
->dq_callback
) {
1909 mutex_lock(&gspca_dev
->usb_lock
);
1910 gspca_dev
->usb_err
= 0;
1911 if (gspca_dev
->present
)
1912 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
1913 mutex_unlock(&gspca_dev
->usb_lock
);
1920 * queue a video buffer
1922 * Attempting to queue a buffer that has already been
1923 * queued will return -EINVAL.
1925 static int vidioc_qbuf(struct file
*file
, void *priv
,
1926 struct v4l2_buffer
*v4l2_buf
)
1928 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
1929 struct gspca_frame
*frame
;
1932 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
1934 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1935 return -ERESTARTSYS
;
1937 index
= v4l2_buf
->index
;
1938 if ((unsigned) index
>= gspca_dev
->nframes
) {
1940 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
1944 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
1945 PDEBUG(D_FRAM
, "qbuf bad memory type");
1950 frame
= &gspca_dev
->frame
[index
];
1951 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
1952 PDEBUG(D_FRAM
, "qbuf bad state");
1957 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
1959 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
1960 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
1961 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
1964 /* put the buffer in the 'queued' queue */
1965 i
= atomic_read(&gspca_dev
->fr_q
);
1966 gspca_dev
->fr_queue
[i
] = index
;
1967 atomic_set(&gspca_dev
->fr_q
, (i
+ 1) % GSPCA_MAX_FRAMES
);
1969 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1970 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
1973 mutex_unlock(&gspca_dev
->queue_lock
);
1978 * allocate the resources for read()
1980 static int read_alloc(struct gspca_dev
*gspca_dev
,
1983 struct v4l2_buffer v4l2_buf
;
1986 PDEBUG(D_STREAM
, "read alloc");
1988 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1989 return -ERESTARTSYS
;
1991 if (gspca_dev
->nframes
== 0) {
1992 struct v4l2_requestbuffers rb
;
1994 memset(&rb
, 0, sizeof rb
);
1995 rb
.count
= gspca_dev
->nbufread
;
1996 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1997 rb
.memory
= GSPCA_MEMORY_READ
;
1998 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
2000 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
2003 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2004 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2005 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2006 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
2008 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2010 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
2016 /* start streaming */
2017 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2019 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
2021 mutex_unlock(&gspca_dev
->usb_lock
);
2025 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
2027 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
2028 unsigned long req_events
= poll_requested_events(wait
);
2031 PDEBUG(D_FRAM
, "poll");
2033 if (req_events
& POLLPRI
)
2034 ret
|= v4l2_ctrl_poll(file
, wait
);
2036 if (req_events
& (POLLIN
| POLLRDNORM
)) {
2037 /* if reqbufs is not done, the user would use read() */
2038 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) {
2039 if (read_alloc(gspca_dev
, file
) != 0) {
2045 poll_wait(file
, &gspca_dev
->wq
, wait
);
2047 /* check if an image has been received */
2048 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0) {
2052 if (gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
))
2053 ret
|= POLLIN
| POLLRDNORM
;
2054 mutex_unlock(&gspca_dev
->queue_lock
);
2058 if (!gspca_dev
->present
)
2064 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
2065 size_t count
, loff_t
*ppos
)
2067 struct gspca_dev
*gspca_dev
= video_drvdata(file
);
2068 struct gspca_frame
*frame
;
2069 struct v4l2_buffer v4l2_buf
;
2070 struct timeval timestamp
;
2073 PDEBUG(D_FRAM
, "read (%zd)", count
);
2074 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) { /* first time ? */
2075 ret
= read_alloc(gspca_dev
, file
);
2081 timestamp
= ktime_to_timeval(ktime_get());
2085 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2086 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2087 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2088 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
2090 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
2094 /* if the process slept for more than 1 second,
2095 * get a newer frame */
2096 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
2098 break; /* avoid infinite loop */
2099 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
2101 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2103 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
2108 /* copy the frame */
2109 if (count
> frame
->v4l2_buf
.bytesused
)
2110 count
= frame
->v4l2_buf
.bytesused
;
2111 ret
= copy_to_user(data
, frame
->data
, count
);
2113 PDEBUG(D_ERR
|D_STREAM
,
2114 "read cp to user lack %d / %zd", ret
, count
);
2120 /* in each case, requeue the buffer */
2121 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2127 static struct v4l2_file_operations dev_fops
= {
2128 .owner
= THIS_MODULE
,
2130 .release
= dev_close
,
2133 .unlocked_ioctl
= video_ioctl2
,
2137 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
2138 .vidioc_querycap
= vidioc_querycap
,
2139 .vidioc_dqbuf
= vidioc_dqbuf
,
2140 .vidioc_qbuf
= vidioc_qbuf
,
2141 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2142 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2143 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2144 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2145 .vidioc_streamon
= vidioc_streamon
,
2146 .vidioc_queryctrl
= vidioc_queryctrl
,
2147 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2148 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2149 .vidioc_querymenu
= vidioc_querymenu
,
2150 .vidioc_enum_input
= vidioc_enum_input
,
2151 .vidioc_g_input
= vidioc_g_input
,
2152 .vidioc_s_input
= vidioc_s_input
,
2153 .vidioc_reqbufs
= vidioc_reqbufs
,
2154 .vidioc_querybuf
= vidioc_querybuf
,
2155 .vidioc_streamoff
= vidioc_streamoff
,
2156 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
2157 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
2158 .vidioc_g_parm
= vidioc_g_parm
,
2159 .vidioc_s_parm
= vidioc_s_parm
,
2160 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2161 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2162 #ifdef CONFIG_VIDEO_ADV_DEBUG
2163 .vidioc_g_register
= vidioc_g_register
,
2164 .vidioc_s_register
= vidioc_s_register
,
2166 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2167 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2168 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2171 static const struct video_device gspca_template
= {
2172 .name
= "gspca main driver",
2174 .ioctl_ops
= &dev_ioctl_ops
,
2175 .release
= video_device_release_empty
, /* We use v4l2_dev.release */
2178 /* initialize the controls */
2179 static void ctrls_init(struct gspca_dev
*gspca_dev
)
2181 struct gspca_ctrl
*ctrl
;
2184 for (i
= 0, ctrl
= gspca_dev
->cam
.ctrls
;
2185 i
< gspca_dev
->sd_desc
->nctrls
;
2187 ctrl
->def
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.default_value
;
2188 ctrl
->val
= ctrl
->def
;
2189 ctrl
->min
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.minimum
;
2190 ctrl
->max
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.maximum
;
2195 * probe and create a new gspca device
2197 * This function must be called by the sub-driver when it is
2198 * called for probing a new device.
2200 int gspca_dev_probe2(struct usb_interface
*intf
,
2201 const struct usb_device_id
*id
,
2202 const struct sd_desc
*sd_desc
,
2204 struct module
*module
)
2206 struct gspca_dev
*gspca_dev
;
2207 struct usb_device
*dev
= interface_to_usbdev(intf
);
2210 pr_info("%s-" GSPCA_VERSION
" probing %04x:%04x\n",
2211 sd_desc
->name
, id
->idVendor
, id
->idProduct
);
2213 /* create the device */
2214 if (dev_size
< sizeof *gspca_dev
)
2215 dev_size
= sizeof *gspca_dev
;
2216 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2218 pr_err("couldn't kzalloc gspca struct\n");
2221 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2222 if (!gspca_dev
->usb_buf
) {
2223 pr_err("out of memory\n");
2227 gspca_dev
->dev
= dev
;
2228 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2230 /* check if any audio device */
2231 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
2233 struct usb_interface
*intf2
;
2235 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
2236 intf2
= dev
->actconfig
->interface
[i
];
2238 && intf2
->altsetting
!= NULL
2239 && intf2
->altsetting
->desc
.bInterfaceClass
==
2241 gspca_dev
->audio
= 1;
2247 gspca_dev
->v4l2_dev
.release
= gspca_release
;
2248 ret
= v4l2_device_register(&intf
->dev
, &gspca_dev
->v4l2_dev
);
2251 gspca_dev
->sd_desc
= sd_desc
;
2252 gspca_dev
->nbufread
= 2;
2253 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2254 gspca_dev
->vdev
= gspca_template
;
2255 gspca_dev
->vdev
.v4l2_dev
= &gspca_dev
->v4l2_dev
;
2256 video_set_drvdata(&gspca_dev
->vdev
, gspca_dev
);
2257 set_bit(V4L2_FL_USE_FH_PRIO
, &gspca_dev
->vdev
.flags
);
2258 gspca_dev
->module
= module
;
2259 gspca_dev
->present
= 1;
2261 mutex_init(&gspca_dev
->usb_lock
);
2262 gspca_dev
->vdev
.lock
= &gspca_dev
->usb_lock
;
2263 mutex_init(&gspca_dev
->queue_lock
);
2264 init_waitqueue_head(&gspca_dev
->wq
);
2266 /* configure the subdriver and initialize the USB device */
2267 ret
= sd_desc
->config(gspca_dev
, id
);
2270 if (gspca_dev
->cam
.ctrls
!= NULL
)
2271 ctrls_init(gspca_dev
);
2272 ret
= sd_desc
->init(gspca_dev
);
2275 if (sd_desc
->init_controls
)
2276 ret
= sd_desc
->init_controls(gspca_dev
);
2279 gspca_set_default_mode(gspca_dev
);
2281 ret
= gspca_input_connect(gspca_dev
);
2286 * Don't take usb_lock for these ioctls. This improves latency if
2287 * usb_lock is taken for a long time, e.g. when changing a control
2288 * value, and a new frame is ready to be dequeued.
2290 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_DQBUF
);
2291 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_QBUF
);
2292 v4l2_disable_ioctl_locking(&gspca_dev
->vdev
, VIDIOC_QUERYBUF
);
2294 /* init video stuff */
2295 ret
= video_register_device(&gspca_dev
->vdev
,
2299 pr_err("video_register_device err %d\n", ret
);
2303 usb_set_intfdata(intf
, gspca_dev
);
2304 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2306 gspca_input_create_urb(gspca_dev
);
2310 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2311 if (gspca_dev
->input_dev
)
2312 input_unregister_device(gspca_dev
->input_dev
);
2314 v4l2_ctrl_handler_free(gspca_dev
->vdev
.ctrl_handler
);
2315 kfree(gspca_dev
->usb_buf
);
2319 EXPORT_SYMBOL(gspca_dev_probe2
);
2321 /* same function as the previous one, but check the interface */
2322 int gspca_dev_probe(struct usb_interface
*intf
,
2323 const struct usb_device_id
*id
,
2324 const struct sd_desc
*sd_desc
,
2326 struct module
*module
)
2328 struct usb_device
*dev
= interface_to_usbdev(intf
);
2330 /* we don't handle multi-config cameras */
2331 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2332 pr_err("%04x:%04x too many config\n",
2333 id
->idVendor
, id
->idProduct
);
2337 /* the USB video interface must be the first one */
2338 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1
2339 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2342 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
2344 EXPORT_SYMBOL(gspca_dev_probe
);
2349 * This function must be called by the sub-driver
2350 * when the device disconnects, after the specific resources are freed.
2352 void gspca_disconnect(struct usb_interface
*intf
)
2354 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2355 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2356 struct input_dev
*input_dev
;
2359 PDEBUG(D_PROBE
, "%s disconnect",
2360 video_device_node_name(&gspca_dev
->vdev
));
2362 mutex_lock(&gspca_dev
->usb_lock
);
2364 usb_set_intfdata(intf
, NULL
);
2365 gspca_dev
->dev
= NULL
;
2366 gspca_dev
->present
= 0;
2367 destroy_urbs(gspca_dev
);
2369 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2370 gspca_input_destroy_urb(gspca_dev
);
2371 input_dev
= gspca_dev
->input_dev
;
2373 gspca_dev
->input_dev
= NULL
;
2374 input_unregister_device(input_dev
);
2377 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2378 if (gspca_dev
->sd_desc
->stop0
&& gspca_dev
->streaming
)
2379 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2380 gspca_dev
->streaming
= 0;
2381 wake_up_interruptible(&gspca_dev
->wq
);
2383 v4l2_device_disconnect(&gspca_dev
->v4l2_dev
);
2384 video_unregister_device(&gspca_dev
->vdev
);
2386 mutex_unlock(&gspca_dev
->usb_lock
);
2388 /* (this will call gspca_release() immediately or on last close) */
2389 v4l2_device_put(&gspca_dev
->v4l2_dev
);
2391 EXPORT_SYMBOL(gspca_disconnect
);
2394 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2396 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2398 if (!gspca_dev
->streaming
)
2400 mutex_lock(&gspca_dev
->usb_lock
);
2401 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2402 gspca_dev
->usb_err
= 0;
2403 if (gspca_dev
->sd_desc
->stopN
)
2404 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2405 destroy_urbs(gspca_dev
);
2406 gspca_input_destroy_urb(gspca_dev
);
2407 gspca_set_alt0(gspca_dev
);
2408 if (gspca_dev
->sd_desc
->stop0
)
2409 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2410 mutex_unlock(&gspca_dev
->usb_lock
);
2413 EXPORT_SYMBOL(gspca_suspend
);
2415 int gspca_resume(struct usb_interface
*intf
)
2417 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2418 int streaming
, ret
= 0;
2420 mutex_lock(&gspca_dev
->usb_lock
);
2421 gspca_dev
->frozen
= 0;
2422 gspca_dev
->usb_err
= 0;
2423 gspca_dev
->sd_desc
->init(gspca_dev
);
2424 gspca_input_create_urb(gspca_dev
);
2426 * Most subdrivers send all ctrl values on sd_start and thus
2427 * only write to the device registers on s_ctrl when streaming ->
2428 * Clear streaming to avoid setting all ctrls twice.
2430 streaming
= gspca_dev
->streaming
;
2431 gspca_dev
->streaming
= 0;
2432 v4l2_ctrl_handler_setup(gspca_dev
->vdev
.ctrl_handler
);
2434 ret
= gspca_init_transfer(gspca_dev
);
2435 mutex_unlock(&gspca_dev
->usb_lock
);
2438 EXPORT_SYMBOL(gspca_resume
);
2441 /* -- module insert / remove -- */
2442 static int __init
gspca_init(void)
2444 pr_info("v" GSPCA_VERSION
" registered\n");
2447 static void __exit
gspca_exit(void)
2451 module_init(gspca_init
);
2452 module_exit(gspca_exit
);
2455 module_param_named(debug
, gspca_debug
, int, 0644);
2456 MODULE_PARM_DESC(debug
,
2457 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2458 " 0x08:stream 0x10:frame 0x20:packet"