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>
44 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
45 #include <linux/input.h>
46 #include <linux/usb/input.h>
50 #define DEF_NURBS 3 /* default number of URBs */
51 #if DEF_NURBS > MAX_NURBS
52 #error "DEF_NURBS too big"
55 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
56 MODULE_DESCRIPTION("GSPCA USB Camera Driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(GSPCA_VERSION
);
61 int gspca_debug
= D_ERR
| D_PROBE
;
62 EXPORT_SYMBOL(gspca_debug
);
64 static void PDEBUG_MODE(char *txt
, __u32 pixfmt
, int w
, int h
)
66 if ((pixfmt
>> 24) >= '0' && (pixfmt
>> 24) <= 'z') {
67 PDEBUG(D_CONF
|D_STREAM
, "%s %c%c%c%c %dx%d",
71 (pixfmt
>> 16) & 0xff,
75 PDEBUG(D_CONF
|D_STREAM
, "%s 0x%08x %dx%d",
82 #define PDEBUG_MODE(txt, pixfmt, w, h)
85 /* specific memory types - !! should be different from V4L2_MEMORY_xxx */
86 #define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
87 #define GSPCA_MEMORY_READ 7
89 #define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
94 static void gspca_vm_open(struct vm_area_struct
*vma
)
96 struct gspca_frame
*frame
= vma
->vm_private_data
;
98 frame
->vma_use_count
++;
99 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_MAPPED
;
102 static void gspca_vm_close(struct vm_area_struct
*vma
)
104 struct gspca_frame
*frame
= vma
->vm_private_data
;
106 if (--frame
->vma_use_count
<= 0)
107 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_MAPPED
;
110 static const struct vm_operations_struct gspca_vm_ops
= {
111 .open
= gspca_vm_open
,
112 .close
= gspca_vm_close
,
116 * Input and interrupt endpoint handling functions
118 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
119 static void int_irq(struct urb
*urb
)
121 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
127 if (gspca_dev
->sd_desc
->int_pkt_scan(gspca_dev
,
128 urb
->transfer_buffer
, urb
->actual_length
) < 0) {
129 PDEBUG(D_ERR
, "Unknown packet received");
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
143 PDEBUG(D_ERR
, "URB error %i, resubmitting", urb
->status
);
149 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
151 pr_err("Resubmit URB failed with error %i\n", ret
);
155 static int gspca_input_connect(struct gspca_dev
*dev
)
157 struct input_dev
*input_dev
;
160 dev
->input_dev
= NULL
;
161 if (dev
->sd_desc
->int_pkt_scan
|| dev
->sd_desc
->other_input
) {
162 input_dev
= input_allocate_device();
166 usb_make_path(dev
->dev
, dev
->phys
, sizeof(dev
->phys
));
167 strlcat(dev
->phys
, "/input0", sizeof(dev
->phys
));
169 input_dev
->name
= dev
->sd_desc
->name
;
170 input_dev
->phys
= dev
->phys
;
172 usb_to_input_id(dev
->dev
, &input_dev
->id
);
174 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
175 input_dev
->keybit
[BIT_WORD(KEY_CAMERA
)] = BIT_MASK(KEY_CAMERA
);
176 input_dev
->dev
.parent
= &dev
->dev
->dev
;
178 err
= input_register_device(input_dev
);
180 pr_err("Input device registration failed with error %i\n",
182 input_dev
->dev
.parent
= NULL
;
183 input_free_device(input_dev
);
185 dev
->input_dev
= input_dev
;
192 static int alloc_and_submit_int_urb(struct gspca_dev
*gspca_dev
,
193 struct usb_endpoint_descriptor
*ep
)
195 unsigned int buffer_len
;
198 struct usb_device
*dev
;
202 buffer_len
= le16_to_cpu(ep
->wMaxPacketSize
);
203 interval
= ep
->bInterval
;
204 PDEBUG(D_CONF
, "found int in endpoint: 0x%x, "
205 "buffer_len=%u, interval=%u",
206 ep
->bEndpointAddress
, buffer_len
, interval
);
208 dev
= gspca_dev
->dev
;
210 urb
= usb_alloc_urb(0, GFP_KERNEL
);
216 buffer
= usb_alloc_coherent(dev
, buffer_len
,
217 GFP_KERNEL
, &urb
->transfer_dma
);
222 usb_fill_int_urb(urb
, dev
,
223 usb_rcvintpipe(dev
, ep
->bEndpointAddress
),
225 int_irq
, (void *)gspca_dev
, interval
);
226 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
227 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
229 PDEBUG(D_ERR
, "submit int URB failed with error %i", ret
);
232 gspca_dev
->int_urb
= urb
;
236 usb_free_coherent(dev
,
237 urb
->transfer_buffer_length
,
238 urb
->transfer_buffer
,
246 static void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
248 struct usb_interface
*intf
;
249 struct usb_host_interface
*intf_desc
;
250 struct usb_endpoint_descriptor
*ep
;
253 if (gspca_dev
->sd_desc
->int_pkt_scan
) {
254 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
255 intf_desc
= intf
->cur_altsetting
;
256 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
257 ep
= &intf_desc
->endpoint
[i
].desc
;
258 if (usb_endpoint_dir_in(ep
) &&
259 usb_endpoint_xfer_int(ep
)) {
261 alloc_and_submit_int_urb(gspca_dev
, ep
);
268 static void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
272 urb
= gspca_dev
->int_urb
;
274 gspca_dev
->int_urb
= NULL
;
276 usb_free_coherent(gspca_dev
->dev
,
277 urb
->transfer_buffer_length
,
278 urb
->transfer_buffer
,
284 static inline void gspca_input_destroy_urb(struct gspca_dev
*gspca_dev
)
288 static inline void gspca_input_create_urb(struct gspca_dev
*gspca_dev
)
292 static inline int gspca_input_connect(struct gspca_dev
*dev
)
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev
*gspca_dev
,
304 u8
*data
; /* address of data in the iso message */
308 if (urb
->status
!= 0) {
309 if (urb
->status
== -ESHUTDOWN
)
310 return; /* disconnection */
312 if (gspca_dev
->frozen
)
315 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
319 pkt_scan
= gspca_dev
->sd_desc
->pkt_scan
;
320 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
321 len
= urb
->iso_frame_desc
[i
].actual_length
;
323 /* check the packet status and length */
324 st
= urb
->iso_frame_desc
[i
].status
;
326 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
328 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
332 if (gspca_dev
->empty_packet
== 0)
333 gspca_dev
->empty_packet
= 1;
337 /* let the packet be analyzed by the subdriver */
338 PDEBUG(D_PACK
, "packet [%d] o:%d l:%d",
339 i
, urb
->iso_frame_desc
[i
].offset
, len
);
340 data
= (u8
*) urb
->transfer_buffer
341 + urb
->iso_frame_desc
[i
].offset
;
342 pkt_scan(gspca_dev
, data
, len
);
346 /* resubmit the URB */
347 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
349 pr_err("usb_submit_urb() ret %d\n", st
);
353 * ISOC message interrupt from the USB device
355 * Analyse each packet and call the subdriver for copy to the frame buffer.
357 static void isoc_irq(struct urb
*urb
)
359 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
361 PDEBUG(D_PACK
, "isoc irq");
362 if (!gspca_dev
->streaming
)
364 fill_frame(gspca_dev
, urb
);
368 * bulk message interrupt from the USB device
370 static void bulk_irq(struct urb
*urb
)
372 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*) urb
->context
;
375 PDEBUG(D_PACK
, "bulk irq");
376 if (!gspca_dev
->streaming
)
378 switch (urb
->status
) {
382 return; /* disconnection */
385 if (gspca_dev
->frozen
)
388 PDEBUG(D_ERR
|D_PACK
, "urb status: %d", urb
->status
);
393 PDEBUG(D_PACK
, "packet l:%d", urb
->actual_length
);
394 gspca_dev
->sd_desc
->pkt_scan(gspca_dev
,
395 urb
->transfer_buffer
,
399 /* resubmit the URB */
400 if (gspca_dev
->cam
.bulk_nurbs
!= 0) {
401 st
= usb_submit_urb(urb
, GFP_ATOMIC
);
403 pr_err("usb_submit_urb() ret %d\n", st
);
408 * add data to the current frame
410 * This function is called by the subdrivers at interrupt level.
412 * To build a frame, these ones must add
414 * - 0 or many INTER_PACKETs
416 * DISCARD_PACKET invalidates the whole frame.
418 void gspca_frame_add(struct gspca_dev
*gspca_dev
,
419 enum gspca_packet_type packet_type
,
423 struct gspca_frame
*frame
;
426 PDEBUG(D_PACK
, "add t:%d l:%d", packet_type
, len
);
428 if (packet_type
== FIRST_PACKET
) {
429 i
= atomic_read(&gspca_dev
->fr_i
);
431 /* if there are no queued buffer, discard the whole frame */
432 if (i
== atomic_read(&gspca_dev
->fr_q
)) {
433 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
434 gspca_dev
->sequence
++;
437 j
= gspca_dev
->fr_queue
[i
];
438 frame
= &gspca_dev
->frame
[j
];
439 frame
->v4l2_buf
.timestamp
= ktime_to_timeval(ktime_get());
440 frame
->v4l2_buf
.sequence
= gspca_dev
->sequence
++;
441 gspca_dev
->image
= frame
->data
;
442 gspca_dev
->image_len
= 0;
444 switch (gspca_dev
->last_packet_type
) {
446 if (packet_type
== LAST_PACKET
) {
447 gspca_dev
->last_packet_type
= packet_type
;
448 gspca_dev
->image
= NULL
;
449 gspca_dev
->image_len
= 0;
457 /* append the packet to the frame buffer */
459 if (gspca_dev
->image_len
+ len
> gspca_dev
->frsz
) {
460 PDEBUG(D_ERR
|D_PACK
, "frame overflow %d > %d",
461 gspca_dev
->image_len
+ len
,
463 packet_type
= DISCARD_PACKET
;
465 /* !! image is NULL only when last pkt is LAST or DISCARD
466 if (gspca_dev->image == NULL) {
467 pr_err("gspca_frame_add() image == NULL\n");
471 memcpy(gspca_dev
->image
+ gspca_dev
->image_len
,
473 gspca_dev
->image_len
+= len
;
476 gspca_dev
->last_packet_type
= packet_type
;
478 /* if last packet, invalidate packet concatenation until
479 * next first packet, wake up the application and advance
481 if (packet_type
== LAST_PACKET
) {
482 i
= atomic_read(&gspca_dev
->fr_i
);
483 j
= gspca_dev
->fr_queue
[i
];
484 frame
= &gspca_dev
->frame
[j
];
485 frame
->v4l2_buf
.bytesused
= gspca_dev
->image_len
;
486 frame
->v4l2_buf
.flags
= (frame
->v4l2_buf
.flags
487 | V4L2_BUF_FLAG_DONE
)
488 & ~V4L2_BUF_FLAG_QUEUED
;
489 i
= (i
+ 1) % GSPCA_MAX_FRAMES
;
490 atomic_set(&gspca_dev
->fr_i
, i
);
491 wake_up_interruptible(&gspca_dev
->wq
); /* event = new frame */
492 PDEBUG(D_FRAM
, "frame complete len:%d",
493 frame
->v4l2_buf
.bytesused
);
494 gspca_dev
->image
= NULL
;
495 gspca_dev
->image_len
= 0;
498 EXPORT_SYMBOL(gspca_frame_add
);
500 static int frame_alloc(struct gspca_dev
*gspca_dev
, struct file
*file
,
501 enum v4l2_memory memory
, unsigned int count
)
503 struct gspca_frame
*frame
;
507 i
= gspca_dev
->curr_mode
;
508 frsz
= gspca_dev
->cam
.cam_mode
[i
].sizeimage
;
509 PDEBUG(D_STREAM
, "frame alloc frsz: %d", frsz
);
510 frsz
= PAGE_ALIGN(frsz
);
511 if (count
>= GSPCA_MAX_FRAMES
)
512 count
= GSPCA_MAX_FRAMES
- 1;
513 gspca_dev
->frbuf
= vmalloc_32(frsz
* count
);
514 if (!gspca_dev
->frbuf
) {
515 pr_err("frame alloc failed\n");
518 gspca_dev
->capt_file
= file
;
519 gspca_dev
->memory
= memory
;
520 gspca_dev
->frsz
= frsz
;
521 gspca_dev
->nframes
= count
;
522 for (i
= 0; i
< count
; i
++) {
523 frame
= &gspca_dev
->frame
[i
];
524 frame
->v4l2_buf
.index
= i
;
525 frame
->v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
526 frame
->v4l2_buf
.flags
= 0;
527 frame
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
528 frame
->v4l2_buf
.length
= frsz
;
529 frame
->v4l2_buf
.memory
= memory
;
530 frame
->v4l2_buf
.sequence
= 0;
531 frame
->data
= gspca_dev
->frbuf
+ i
* frsz
;
532 frame
->v4l2_buf
.m
.offset
= i
* frsz
;
534 atomic_set(&gspca_dev
->fr_q
, 0);
535 atomic_set(&gspca_dev
->fr_i
, 0);
540 static void frame_free(struct gspca_dev
*gspca_dev
)
544 PDEBUG(D_STREAM
, "frame free");
545 if (gspca_dev
->frbuf
!= NULL
) {
546 vfree(gspca_dev
->frbuf
);
547 gspca_dev
->frbuf
= NULL
;
548 for (i
= 0; i
< gspca_dev
->nframes
; i
++)
549 gspca_dev
->frame
[i
].data
= NULL
;
551 gspca_dev
->nframes
= 0;
553 gspca_dev
->capt_file
= NULL
;
554 gspca_dev
->memory
= GSPCA_MEMORY_NO
;
557 static void destroy_urbs(struct gspca_dev
*gspca_dev
)
562 PDEBUG(D_STREAM
, "kill transfer");
563 for (i
= 0; i
< MAX_NURBS
; i
++) {
564 urb
= gspca_dev
->urb
[i
];
568 gspca_dev
->urb
[i
] = NULL
;
570 if (urb
->transfer_buffer
!= NULL
)
571 usb_free_coherent(gspca_dev
->dev
,
572 urb
->transfer_buffer_length
,
573 urb
->transfer_buffer
,
579 static int gspca_set_alt0(struct gspca_dev
*gspca_dev
)
583 if (gspca_dev
->alt
== 0)
585 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 0);
587 pr_err("set alt 0 err %d\n", ret
);
591 /* Note: both the queue and the usb locks should be held when calling this */
592 static void gspca_stream_off(struct gspca_dev
*gspca_dev
)
594 gspca_dev
->streaming
= 0;
595 if (gspca_dev
->present
) {
596 if (gspca_dev
->sd_desc
->stopN
)
597 gspca_dev
->sd_desc
->stopN(gspca_dev
);
598 destroy_urbs(gspca_dev
);
599 gspca_input_destroy_urb(gspca_dev
);
600 gspca_set_alt0(gspca_dev
);
601 gspca_input_create_urb(gspca_dev
);
604 /* always call stop0 to free the subdriver's resources */
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 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
853 if (!gspca_dev
->present
) {
858 /* reset the streaming variables */
859 gspca_dev
->image
= NULL
;
860 gspca_dev
->image_len
= 0;
861 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
862 gspca_dev
->sequence
= 0;
864 gspca_dev
->usb_err
= 0;
866 /* do the specific subdriver stuff before endpoint selection */
867 intf
= usb_ifnum_to_if(gspca_dev
->dev
, gspca_dev
->iface
);
868 gspca_dev
->alt
= gspca_dev
->cam
.bulk
? intf
->num_altsetting
: 0;
869 if (gspca_dev
->sd_desc
->isoc_init
) {
870 ret
= gspca_dev
->sd_desc
->isoc_init(gspca_dev
);
874 xfer
= gspca_dev
->cam
.bulk
? USB_ENDPOINT_XFER_BULK
875 : USB_ENDPOINT_XFER_ISOC
;
877 /* if bulk or the subdriver forced an altsetting, get the endpoint */
878 if (gspca_dev
->alt
!= 0) {
879 gspca_dev
->alt
--; /* (previous version compatibility) */
880 ep
= alt_xfer(&intf
->altsetting
[gspca_dev
->alt
], xfer
);
882 pr_err("bad altsetting %d\n", gspca_dev
->alt
);
886 ep_tb
[0].alt
= gspca_dev
->alt
;
890 /* else, compute the minimum bandwidth
891 * and build the endpoint table */
892 alt_idx
= build_isoc_ep_tb(gspca_dev
, intf
, ep_tb
);
894 pr_err("no transfer endpoint found\n");
900 /* set the highest alternate setting and
901 * loop until urb submit succeeds */
902 gspca_input_destroy_urb(gspca_dev
);
904 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
907 if (alt
!= gspca_dev
->alt
) {
908 alt
= gspca_dev
->alt
;
909 if (intf
->num_altsetting
> 1) {
910 ret
= usb_set_interface(gspca_dev
->dev
,
915 goto retry
; /*fixme: ugly*/
916 pr_err("set alt %d err %d\n", alt
, ret
);
921 if (!gspca_dev
->cam
.no_urb_create
) {
922 PDEBUG(D_STREAM
, "init transfer alt %d", alt
);
923 ret
= create_urbs(gspca_dev
,
924 alt_xfer(&intf
->altsetting
[alt
], xfer
));
926 destroy_urbs(gspca_dev
);
931 /* clear the bulk endpoint */
932 if (gspca_dev
->cam
.bulk
)
933 usb_clear_halt(gspca_dev
->dev
,
934 gspca_dev
->urb
[0]->pipe
);
937 ret
= gspca_dev
->sd_desc
->start(gspca_dev
);
939 destroy_urbs(gspca_dev
);
942 gspca_dev
->streaming
= 1;
944 /* some bulk transfers are started by the subdriver */
945 if (gspca_dev
->cam
.bulk
&& gspca_dev
->cam
.bulk_nurbs
== 0)
948 /* submit the URBs */
949 for (n
= 0; n
< MAX_NURBS
; n
++) {
950 urb
= gspca_dev
->urb
[n
];
953 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
958 break; /* transfer is started */
960 /* something when wrong
961 * stop the webcam and free the transfer resources */
962 gspca_stream_off(gspca_dev
);
963 if (ret
!= -ENOSPC
) {
964 pr_err("usb_submit_urb alt %d err %d\n",
965 gspca_dev
->alt
, ret
);
969 /* the bandwidth is not wide enough
970 * negotiate or try a lower alternate setting */
972 PDEBUG(D_ERR
|D_STREAM
,
973 "alt %d - bandwidth not wide enough - trying again",
975 msleep(20); /* wait for kill complete */
976 if (gspca_dev
->sd_desc
->isoc_nego
) {
977 ret
= gspca_dev
->sd_desc
->isoc_nego(gspca_dev
);
982 pr_err("no transfer endpoint found\n");
986 gspca_dev
->alt
= ep_tb
[--alt_idx
].alt
;
990 gspca_input_create_urb(gspca_dev
);
992 mutex_unlock(&gspca_dev
->usb_lock
);
996 static void gspca_set_default_mode(struct gspca_dev
*gspca_dev
)
998 struct gspca_ctrl
*ctrl
;
1001 i
= gspca_dev
->cam
.nmodes
- 1; /* take the highest mode */
1002 gspca_dev
->curr_mode
= i
;
1003 gspca_dev
->width
= gspca_dev
->cam
.cam_mode
[i
].width
;
1004 gspca_dev
->height
= gspca_dev
->cam
.cam_mode
[i
].height
;
1005 gspca_dev
->pixfmt
= gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1007 /* set the current control values to their default values
1008 * which may have changed in sd_init() */
1009 ctrl
= gspca_dev
->cam
.ctrls
;
1012 i
< gspca_dev
->sd_desc
->nctrls
;
1014 ctrl
->val
= ctrl
->def
;
1018 static int wxh_to_mode(struct gspca_dev
*gspca_dev
,
1019 int width
, int height
)
1023 for (i
= gspca_dev
->cam
.nmodes
; --i
> 0; ) {
1024 if (width
>= gspca_dev
->cam
.cam_mode
[i
].width
1025 && height
>= gspca_dev
->cam
.cam_mode
[i
].height
)
1032 * search a mode with the right pixel format
1034 static int gspca_get_mode(struct gspca_dev
*gspca_dev
,
1040 modeU
= modeD
= mode
;
1041 while ((modeU
< gspca_dev
->cam
.nmodes
) || modeD
>= 0) {
1043 if (gspca_dev
->cam
.cam_mode
[modeD
].pixelformat
1047 if (++modeU
< gspca_dev
->cam
.nmodes
) {
1048 if (gspca_dev
->cam
.cam_mode
[modeU
].pixelformat
1056 #ifdef CONFIG_VIDEO_ADV_DEBUG
1057 static int vidioc_g_register(struct file
*file
, void *priv
,
1058 struct v4l2_dbg_register
*reg
)
1061 struct gspca_dev
*gspca_dev
= priv
;
1063 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1066 if (!gspca_dev
->sd_desc
->get_register
)
1069 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1070 return -ERESTARTSYS
;
1071 gspca_dev
->usb_err
= 0;
1072 if (gspca_dev
->present
)
1073 ret
= gspca_dev
->sd_desc
->get_register(gspca_dev
, reg
);
1076 mutex_unlock(&gspca_dev
->usb_lock
);
1081 static int vidioc_s_register(struct file
*file
, void *priv
,
1082 struct v4l2_dbg_register
*reg
)
1085 struct gspca_dev
*gspca_dev
= priv
;
1087 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1090 if (!gspca_dev
->sd_desc
->set_register
)
1093 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1094 return -ERESTARTSYS
;
1095 gspca_dev
->usb_err
= 0;
1096 if (gspca_dev
->present
)
1097 ret
= gspca_dev
->sd_desc
->set_register(gspca_dev
, reg
);
1100 mutex_unlock(&gspca_dev
->usb_lock
);
1106 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1107 struct v4l2_dbg_chip_ident
*chip
)
1110 struct gspca_dev
*gspca_dev
= priv
;
1112 if (!gspca_dev
->sd_desc
->get_chip_ident
)
1115 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1116 return -ERESTARTSYS
;
1117 gspca_dev
->usb_err
= 0;
1118 if (gspca_dev
->present
)
1119 ret
= gspca_dev
->sd_desc
->get_chip_ident(gspca_dev
, chip
);
1122 mutex_unlock(&gspca_dev
->usb_lock
);
1127 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1128 struct v4l2_fmtdesc
*fmtdesc
)
1130 struct gspca_dev
*gspca_dev
= priv
;
1134 /* give an index to each format */
1137 for (i
= gspca_dev
->cam
.nmodes
; --i
>= 0; ) {
1138 fmt_tb
[index
] = gspca_dev
->cam
.cam_mode
[i
].pixelformat
;
1141 if (fmt_tb
[j
] == fmt_tb
[index
])
1146 if (fmtdesc
->index
== index
)
1147 break; /* new format */
1149 if (index
>= ARRAY_SIZE(fmt_tb
))
1154 return -EINVAL
; /* no more format */
1156 fmtdesc
->pixelformat
= fmt_tb
[index
];
1157 if (gspca_dev
->cam
.cam_mode
[i
].sizeimage
<
1158 gspca_dev
->cam
.cam_mode
[i
].width
*
1159 gspca_dev
->cam
.cam_mode
[i
].height
)
1160 fmtdesc
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1161 fmtdesc
->description
[0] = fmtdesc
->pixelformat
& 0xff;
1162 fmtdesc
->description
[1] = (fmtdesc
->pixelformat
>> 8) & 0xff;
1163 fmtdesc
->description
[2] = (fmtdesc
->pixelformat
>> 16) & 0xff;
1164 fmtdesc
->description
[3] = fmtdesc
->pixelformat
>> 24;
1165 fmtdesc
->description
[4] = '\0';
1169 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1170 struct v4l2_format
*fmt
)
1172 struct gspca_dev
*gspca_dev
= priv
;
1175 mode
= gspca_dev
->curr_mode
;
1176 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1177 sizeof fmt
->fmt
.pix
);
1181 static int try_fmt_vid_cap(struct gspca_dev
*gspca_dev
,
1182 struct v4l2_format
*fmt
)
1184 int w
, h
, mode
, mode2
;
1186 w
= fmt
->fmt
.pix
.width
;
1187 h
= fmt
->fmt
.pix
.height
;
1190 if (gspca_debug
& D_CONF
)
1191 PDEBUG_MODE("try fmt cap", fmt
->fmt
.pix
.pixelformat
, w
, h
);
1193 /* search the closest mode for width and height */
1194 mode
= wxh_to_mode(gspca_dev
, w
, h
);
1196 /* OK if right palette */
1197 if (gspca_dev
->cam
.cam_mode
[mode
].pixelformat
1198 != fmt
->fmt
.pix
.pixelformat
) {
1200 /* else, search the closest mode with the same pixel format */
1201 mode2
= gspca_get_mode(gspca_dev
, mode
,
1202 fmt
->fmt
.pix
.pixelformat
);
1206 ; * no chance, return this mode */
1208 memcpy(&fmt
->fmt
.pix
, &gspca_dev
->cam
.cam_mode
[mode
],
1209 sizeof fmt
->fmt
.pix
);
1210 return mode
; /* used when s_fmt */
1213 static int vidioc_try_fmt_vid_cap(struct file
*file
,
1215 struct v4l2_format
*fmt
)
1217 struct gspca_dev
*gspca_dev
= priv
;
1220 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1226 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1227 struct v4l2_format
*fmt
)
1229 struct gspca_dev
*gspca_dev
= priv
;
1232 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1233 return -ERESTARTSYS
;
1235 ret
= try_fmt_vid_cap(gspca_dev
, fmt
);
1239 if (gspca_dev
->nframes
!= 0
1240 && fmt
->fmt
.pix
.sizeimage
> gspca_dev
->frsz
) {
1245 if (ret
== gspca_dev
->curr_mode
) {
1247 goto out
; /* same mode */
1250 if (gspca_dev
->streaming
) {
1254 gspca_dev
->width
= fmt
->fmt
.pix
.width
;
1255 gspca_dev
->height
= fmt
->fmt
.pix
.height
;
1256 gspca_dev
->pixfmt
= fmt
->fmt
.pix
.pixelformat
;
1257 gspca_dev
->curr_mode
= ret
;
1261 mutex_unlock(&gspca_dev
->queue_lock
);
1265 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1266 struct v4l2_frmsizeenum
*fsize
)
1268 struct gspca_dev
*gspca_dev
= priv
;
1272 for (i
= 0; i
< gspca_dev
->cam
.nmodes
; i
++) {
1273 if (fsize
->pixel_format
!=
1274 gspca_dev
->cam
.cam_mode
[i
].pixelformat
)
1277 if (fsize
->index
== index
) {
1278 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1279 fsize
->discrete
.width
=
1280 gspca_dev
->cam
.cam_mode
[i
].width
;
1281 fsize
->discrete
.height
=
1282 gspca_dev
->cam
.cam_mode
[i
].height
;
1291 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
1292 struct v4l2_frmivalenum
*fival
)
1294 struct gspca_dev
*gspca_dev
= priv
;
1295 int mode
= wxh_to_mode(gspca_dev
, fival
->width
, fival
->height
);
1298 if (gspca_dev
->cam
.mode_framerates
== NULL
||
1299 gspca_dev
->cam
.mode_framerates
[mode
].nrates
== 0)
1302 if (fival
->pixel_format
!=
1303 gspca_dev
->cam
.cam_mode
[mode
].pixelformat
)
1306 for (i
= 0; i
< gspca_dev
->cam
.mode_framerates
[mode
].nrates
; i
++) {
1307 if (fival
->index
== i
) {
1308 fival
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1309 fival
->discrete
.numerator
= 1;
1310 fival
->discrete
.denominator
=
1311 gspca_dev
->cam
.mode_framerates
[mode
].rates
[i
];
1319 static void gspca_release(struct video_device
*vfd
)
1321 struct gspca_dev
*gspca_dev
= container_of(vfd
, struct gspca_dev
, vdev
);
1323 PDEBUG(D_PROBE
, "%s released",
1324 video_device_node_name(&gspca_dev
->vdev
));
1326 kfree(gspca_dev
->usb_buf
);
1330 static int dev_open(struct file
*file
)
1332 struct gspca_dev
*gspca_dev
;
1334 PDEBUG(D_STREAM
, "[%s] open", current
->comm
);
1335 gspca_dev
= (struct gspca_dev
*) video_devdata(file
);
1336 if (!gspca_dev
->present
)
1339 /* protect the subdriver against rmmod */
1340 if (!try_module_get(gspca_dev
->module
))
1343 file
->private_data
= gspca_dev
;
1345 /* activate the v4l2 debug */
1346 if (gspca_debug
& D_V4L2
)
1347 gspca_dev
->vdev
.debug
|= V4L2_DEBUG_IOCTL
1348 | V4L2_DEBUG_IOCTL_ARG
;
1350 gspca_dev
->vdev
.debug
&= ~(V4L2_DEBUG_IOCTL
1351 | V4L2_DEBUG_IOCTL_ARG
);
1356 static int dev_close(struct file
*file
)
1358 struct gspca_dev
*gspca_dev
= file
->private_data
;
1360 PDEBUG(D_STREAM
, "[%s] close", current
->comm
);
1361 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1362 return -ERESTARTSYS
;
1364 /* if the file did the capture, free the streaming resources */
1365 if (gspca_dev
->capt_file
== file
) {
1366 if (gspca_dev
->streaming
) {
1367 mutex_lock(&gspca_dev
->usb_lock
);
1368 gspca_dev
->usb_err
= 0;
1369 gspca_stream_off(gspca_dev
);
1370 mutex_unlock(&gspca_dev
->usb_lock
);
1372 frame_free(gspca_dev
);
1374 file
->private_data
= NULL
;
1375 module_put(gspca_dev
->module
);
1376 mutex_unlock(&gspca_dev
->queue_lock
);
1378 PDEBUG(D_STREAM
, "close done");
1383 static int vidioc_querycap(struct file
*file
, void *priv
,
1384 struct v4l2_capability
*cap
)
1386 struct gspca_dev
*gspca_dev
= priv
;
1389 /* protect the access to the usb device */
1390 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1391 return -ERESTARTSYS
;
1392 if (!gspca_dev
->present
) {
1396 strlcpy((char *) cap
->driver
, gspca_dev
->sd_desc
->name
,
1397 sizeof cap
->driver
);
1398 if (gspca_dev
->dev
->product
!= NULL
) {
1399 strlcpy((char *) cap
->card
, gspca_dev
->dev
->product
,
1402 snprintf((char *) cap
->card
, sizeof cap
->card
,
1403 "USB Camera (%04x:%04x)",
1404 le16_to_cpu(gspca_dev
->dev
->descriptor
.idVendor
),
1405 le16_to_cpu(gspca_dev
->dev
->descriptor
.idProduct
));
1407 usb_make_path(gspca_dev
->dev
, (char *) cap
->bus_info
,
1408 sizeof(cap
->bus_info
));
1409 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
1410 | V4L2_CAP_STREAMING
1411 | V4L2_CAP_READWRITE
;
1414 mutex_unlock(&gspca_dev
->usb_lock
);
1418 static int get_ctrl(struct gspca_dev
*gspca_dev
,
1421 const struct ctrl
*ctrls
;
1424 for (i
= 0, ctrls
= gspca_dev
->sd_desc
->ctrls
;
1425 i
< gspca_dev
->sd_desc
->nctrls
;
1427 if (gspca_dev
->ctrl_dis
& (1 << i
))
1429 if (id
== ctrls
->qctrl
.id
)
1435 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1436 struct v4l2_queryctrl
*q_ctrl
)
1438 struct gspca_dev
*gspca_dev
= priv
;
1439 const struct ctrl
*ctrls
;
1440 struct gspca_ctrl
*gspca_ctrl
;
1445 if (id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
1446 id
&= V4L2_CTRL_ID_MASK
;
1449 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
1450 if (gspca_dev
->ctrl_dis
& (1 << i
))
1452 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
< id
)
1455 && gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
1456 > gspca_dev
->sd_desc
->ctrls
[idx
].qctrl
.id
)
1461 idx
= get_ctrl(gspca_dev
, id
);
1465 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1466 memcpy(q_ctrl
, &ctrls
->qctrl
, sizeof *q_ctrl
);
1467 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1468 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1469 q_ctrl
->default_value
= gspca_ctrl
->def
;
1470 q_ctrl
->minimum
= gspca_ctrl
->min
;
1471 q_ctrl
->maximum
= gspca_ctrl
->max
;
1473 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1474 q_ctrl
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
1478 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1479 struct v4l2_control
*ctrl
)
1481 struct gspca_dev
*gspca_dev
= priv
;
1482 const struct ctrl
*ctrls
;
1483 struct gspca_ctrl
*gspca_ctrl
;
1486 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1489 if (gspca_dev
->ctrl_inac
& (1 << idx
))
1491 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1492 if (gspca_dev
->cam
.ctrls
!= NULL
) {
1493 gspca_ctrl
= &gspca_dev
->cam
.ctrls
[idx
];
1494 if (ctrl
->value
< gspca_ctrl
->min
1495 || ctrl
->value
> gspca_ctrl
->max
)
1499 if (ctrl
->value
< ctrls
->qctrl
.minimum
1500 || ctrl
->value
> ctrls
->qctrl
.maximum
)
1503 PDEBUG(D_CONF
, "set ctrl [%08x] = %d", ctrl
->id
, ctrl
->value
);
1504 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1505 return -ERESTARTSYS
;
1506 if (!gspca_dev
->present
) {
1510 gspca_dev
->usb_err
= 0;
1511 if (ctrls
->set
!= NULL
) {
1512 ret
= ctrls
->set(gspca_dev
, ctrl
->value
);
1515 if (gspca_ctrl
!= NULL
) {
1516 gspca_ctrl
->val
= ctrl
->value
;
1517 if (ctrls
->set_control
!= NULL
1518 && gspca_dev
->streaming
)
1519 ctrls
->set_control(gspca_dev
);
1521 ret
= gspca_dev
->usb_err
;
1523 mutex_unlock(&gspca_dev
->usb_lock
);
1527 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1528 struct v4l2_control
*ctrl
)
1530 struct gspca_dev
*gspca_dev
= priv
;
1531 const struct ctrl
*ctrls
;
1534 idx
= get_ctrl(gspca_dev
, ctrl
->id
);
1537 ctrls
= &gspca_dev
->sd_desc
->ctrls
[idx
];
1539 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1540 return -ERESTARTSYS
;
1541 if (!gspca_dev
->present
) {
1545 gspca_dev
->usb_err
= 0;
1546 if (ctrls
->get
!= NULL
) {
1547 ret
= ctrls
->get(gspca_dev
, &ctrl
->value
);
1550 if (gspca_dev
->cam
.ctrls
!= NULL
)
1551 ctrl
->value
= gspca_dev
->cam
.ctrls
[idx
].val
;
1554 mutex_unlock(&gspca_dev
->usb_lock
);
1558 static int vidioc_querymenu(struct file
*file
, void *priv
,
1559 struct v4l2_querymenu
*qmenu
)
1561 struct gspca_dev
*gspca_dev
= priv
;
1563 if (!gspca_dev
->sd_desc
->querymenu
)
1565 return gspca_dev
->sd_desc
->querymenu(gspca_dev
, qmenu
);
1568 static int vidioc_enum_input(struct file
*file
, void *priv
,
1569 struct v4l2_input
*input
)
1571 struct gspca_dev
*gspca_dev
= priv
;
1573 if (input
->index
!= 0)
1575 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1576 input
->status
= gspca_dev
->cam
.input_flags
;
1577 strlcpy(input
->name
, gspca_dev
->sd_desc
->name
,
1578 sizeof input
->name
);
1582 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1588 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1595 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1596 struct v4l2_requestbuffers
*rb
)
1598 struct gspca_dev
*gspca_dev
= priv
;
1599 int i
, ret
= 0, streaming
;
1601 i
= rb
->memory
; /* (avoid compilation warning) */
1603 case GSPCA_MEMORY_READ
: /* (internal call) */
1604 case V4L2_MEMORY_MMAP
:
1605 case V4L2_MEMORY_USERPTR
:
1610 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1611 return -ERESTARTSYS
;
1613 if (gspca_dev
->memory
!= GSPCA_MEMORY_NO
1614 && gspca_dev
->memory
!= GSPCA_MEMORY_READ
1615 && gspca_dev
->memory
!= rb
->memory
) {
1620 /* only one file may do the capture */
1621 if (gspca_dev
->capt_file
!= NULL
1622 && gspca_dev
->capt_file
!= file
) {
1627 /* if allocated, the buffers must not be mapped */
1628 for (i
= 0; i
< gspca_dev
->nframes
; i
++) {
1629 if (gspca_dev
->frame
[i
].vma_use_count
) {
1635 /* stop streaming */
1636 streaming
= gspca_dev
->streaming
;
1638 mutex_lock(&gspca_dev
->usb_lock
);
1639 gspca_dev
->usb_err
= 0;
1640 gspca_stream_off(gspca_dev
);
1641 mutex_unlock(&gspca_dev
->usb_lock
);
1643 /* Don't restart the stream when switching from read
1645 if (gspca_dev
->memory
== GSPCA_MEMORY_READ
)
1649 /* free the previous allocated buffers, if any */
1650 if (gspca_dev
->nframes
!= 0)
1651 frame_free(gspca_dev
);
1652 if (rb
->count
== 0) /* unrequest */
1654 ret
= frame_alloc(gspca_dev
, file
, rb
->memory
, rb
->count
);
1656 rb
->count
= gspca_dev
->nframes
;
1658 ret
= gspca_init_transfer(gspca_dev
);
1661 mutex_unlock(&gspca_dev
->queue_lock
);
1662 PDEBUG(D_STREAM
, "reqbufs st:%d c:%d", ret
, rb
->count
);
1666 static int vidioc_querybuf(struct file
*file
, void *priv
,
1667 struct v4l2_buffer
*v4l2_buf
)
1669 struct gspca_dev
*gspca_dev
= priv
;
1670 struct gspca_frame
*frame
;
1672 if (v4l2_buf
->index
< 0
1673 || v4l2_buf
->index
>= gspca_dev
->nframes
)
1676 frame
= &gspca_dev
->frame
[v4l2_buf
->index
];
1677 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
1681 static int vidioc_streamon(struct file
*file
, void *priv
,
1682 enum v4l2_buf_type buf_type
)
1684 struct gspca_dev
*gspca_dev
= priv
;
1687 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1689 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1690 return -ERESTARTSYS
;
1692 /* check the capture file */
1693 if (gspca_dev
->capt_file
!= file
) {
1698 if (gspca_dev
->nframes
== 0
1699 || !(gspca_dev
->frame
[0].v4l2_buf
.flags
& V4L2_BUF_FLAG_QUEUED
)) {
1703 if (!gspca_dev
->streaming
) {
1704 ret
= gspca_init_transfer(gspca_dev
);
1709 if (gspca_debug
& D_STREAM
) {
1710 PDEBUG_MODE("stream on OK",
1718 mutex_unlock(&gspca_dev
->queue_lock
);
1722 static int vidioc_streamoff(struct file
*file
, void *priv
,
1723 enum v4l2_buf_type buf_type
)
1725 struct gspca_dev
*gspca_dev
= priv
;
1728 if (buf_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1731 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1732 return -ERESTARTSYS
;
1734 if (!gspca_dev
->streaming
) {
1739 /* check the capture file */
1740 if (gspca_dev
->capt_file
!= file
) {
1745 /* stop streaming */
1746 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
)) {
1750 gspca_dev
->usb_err
= 0;
1751 gspca_stream_off(gspca_dev
);
1752 mutex_unlock(&gspca_dev
->usb_lock
);
1753 /* In case another thread is waiting in dqbuf */
1754 wake_up_interruptible(&gspca_dev
->wq
);
1756 /* empty the transfer queues */
1757 atomic_set(&gspca_dev
->fr_q
, 0);
1758 atomic_set(&gspca_dev
->fr_i
, 0);
1759 gspca_dev
->fr_o
= 0;
1762 mutex_unlock(&gspca_dev
->queue_lock
);
1766 static int vidioc_g_jpegcomp(struct file
*file
, void *priv
,
1767 struct v4l2_jpegcompression
*jpegcomp
)
1769 struct gspca_dev
*gspca_dev
= priv
;
1772 if (!gspca_dev
->sd_desc
->get_jcomp
)
1774 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1775 return -ERESTARTSYS
;
1776 gspca_dev
->usb_err
= 0;
1777 if (gspca_dev
->present
)
1778 ret
= gspca_dev
->sd_desc
->get_jcomp(gspca_dev
, jpegcomp
);
1781 mutex_unlock(&gspca_dev
->usb_lock
);
1785 static int vidioc_s_jpegcomp(struct file
*file
, void *priv
,
1786 struct v4l2_jpegcompression
*jpegcomp
)
1788 struct gspca_dev
*gspca_dev
= priv
;
1791 if (!gspca_dev
->sd_desc
->set_jcomp
)
1793 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1794 return -ERESTARTSYS
;
1795 gspca_dev
->usb_err
= 0;
1796 if (gspca_dev
->present
)
1797 ret
= gspca_dev
->sd_desc
->set_jcomp(gspca_dev
, jpegcomp
);
1800 mutex_unlock(&gspca_dev
->usb_lock
);
1804 static int vidioc_g_parm(struct file
*filp
, void *priv
,
1805 struct v4l2_streamparm
*parm
)
1807 struct gspca_dev
*gspca_dev
= priv
;
1809 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1811 if (gspca_dev
->sd_desc
->get_streamparm
) {
1814 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1815 return -ERESTARTSYS
;
1816 if (gspca_dev
->present
) {
1817 gspca_dev
->usb_err
= 0;
1818 gspca_dev
->sd_desc
->get_streamparm(gspca_dev
, parm
);
1819 ret
= gspca_dev
->usb_err
;
1823 mutex_unlock(&gspca_dev
->usb_lock
);
1830 static int vidioc_s_parm(struct file
*filp
, void *priv
,
1831 struct v4l2_streamparm
*parm
)
1833 struct gspca_dev
*gspca_dev
= priv
;
1836 n
= parm
->parm
.capture
.readbuffers
;
1837 if (n
== 0 || n
>= GSPCA_MAX_FRAMES
)
1838 parm
->parm
.capture
.readbuffers
= gspca_dev
->nbufread
;
1840 gspca_dev
->nbufread
= n
;
1842 if (gspca_dev
->sd_desc
->set_streamparm
) {
1845 if (mutex_lock_interruptible(&gspca_dev
->usb_lock
))
1846 return -ERESTARTSYS
;
1847 if (gspca_dev
->present
) {
1848 gspca_dev
->usb_err
= 0;
1849 gspca_dev
->sd_desc
->set_streamparm(gspca_dev
, parm
);
1850 ret
= gspca_dev
->usb_err
;
1854 mutex_unlock(&gspca_dev
->usb_lock
);
1861 static int dev_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1863 struct gspca_dev
*gspca_dev
= file
->private_data
;
1864 struct gspca_frame
*frame
;
1866 unsigned long addr
, start
, size
;
1869 start
= vma
->vm_start
;
1870 size
= vma
->vm_end
- vma
->vm_start
;
1871 PDEBUG(D_STREAM
, "mmap start:%08x size:%d", (int) start
, (int) size
);
1873 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1874 return -ERESTARTSYS
;
1875 if (!gspca_dev
->present
) {
1879 if (gspca_dev
->capt_file
!= file
) {
1885 for (i
= 0; i
< gspca_dev
->nframes
; ++i
) {
1886 if (gspca_dev
->frame
[i
].v4l2_buf
.memory
!= V4L2_MEMORY_MMAP
) {
1887 PDEBUG(D_STREAM
, "mmap bad memory type");
1890 if ((gspca_dev
->frame
[i
].v4l2_buf
.m
.offset
>> PAGE_SHIFT
)
1892 frame
= &gspca_dev
->frame
[i
];
1896 if (frame
== NULL
) {
1897 PDEBUG(D_STREAM
, "mmap no frame buffer found");
1901 if (size
!= frame
->v4l2_buf
.length
) {
1902 PDEBUG(D_STREAM
, "mmap bad size");
1908 * - VM_IO marks the area as being a mmaped region for I/O to a
1909 * device. It also prevents the region from being core dumped.
1911 vma
->vm_flags
|= VM_IO
;
1913 addr
= (unsigned long) frame
->data
;
1915 page
= vmalloc_to_page((void *) addr
);
1916 ret
= vm_insert_page(vma
, start
, page
);
1924 vma
->vm_ops
= &gspca_vm_ops
;
1925 vma
->vm_private_data
= frame
;
1929 mutex_unlock(&gspca_dev
->queue_lock
);
1933 static int frame_ready_nolock(struct gspca_dev
*gspca_dev
, struct file
*file
,
1934 enum v4l2_memory memory
)
1936 if (!gspca_dev
->present
)
1938 if (gspca_dev
->capt_file
!= file
|| gspca_dev
->memory
!= memory
||
1939 !gspca_dev
->streaming
)
1942 /* check if a frame is ready */
1943 return gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
);
1946 static int frame_ready(struct gspca_dev
*gspca_dev
, struct file
*file
,
1947 enum v4l2_memory memory
)
1951 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1952 return -ERESTARTSYS
;
1953 ret
= frame_ready_nolock(gspca_dev
, file
, memory
);
1954 mutex_unlock(&gspca_dev
->queue_lock
);
1959 * dequeue a video buffer
1961 * If nonblock_ing is false, block until a buffer is available.
1963 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1964 struct v4l2_buffer
*v4l2_buf
)
1966 struct gspca_dev
*gspca_dev
= priv
;
1967 struct gspca_frame
*frame
;
1970 PDEBUG(D_FRAM
, "dqbuf");
1972 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1973 return -ERESTARTSYS
;
1976 ret
= frame_ready_nolock(gspca_dev
, file
, v4l2_buf
->memory
);
1982 mutex_unlock(&gspca_dev
->queue_lock
);
1984 if (file
->f_flags
& O_NONBLOCK
)
1987 /* wait till a frame is ready */
1988 ret
= wait_event_interruptible_timeout(gspca_dev
->wq
,
1989 frame_ready(gspca_dev
, file
, v4l2_buf
->memory
),
1990 msecs_to_jiffies(3000));
1996 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
1997 return -ERESTARTSYS
;
2000 i
= gspca_dev
->fr_o
;
2001 j
= gspca_dev
->fr_queue
[i
];
2002 frame
= &gspca_dev
->frame
[j
];
2004 gspca_dev
->fr_o
= (i
+ 1) % GSPCA_MAX_FRAMES
;
2006 if (gspca_dev
->sd_desc
->dq_callback
) {
2007 mutex_lock(&gspca_dev
->usb_lock
);
2008 gspca_dev
->usb_err
= 0;
2009 if (gspca_dev
->present
)
2010 gspca_dev
->sd_desc
->dq_callback(gspca_dev
);
2011 mutex_unlock(&gspca_dev
->usb_lock
);
2014 frame
->v4l2_buf
.flags
&= ~V4L2_BUF_FLAG_DONE
;
2015 memcpy(v4l2_buf
, &frame
->v4l2_buf
, sizeof *v4l2_buf
);
2016 PDEBUG(D_FRAM
, "dqbuf %d", j
);
2019 if (gspca_dev
->memory
== V4L2_MEMORY_USERPTR
) {
2020 if (copy_to_user((__u8 __user
*) frame
->v4l2_buf
.m
.userptr
,
2022 frame
->v4l2_buf
.bytesused
)) {
2023 PDEBUG(D_ERR
|D_STREAM
,
2024 "dqbuf cp to user failed");
2029 mutex_unlock(&gspca_dev
->queue_lock
);
2034 * queue a video buffer
2036 * Attempting to queue a buffer that has already been
2037 * queued will return -EINVAL.
2039 static int vidioc_qbuf(struct file
*file
, void *priv
,
2040 struct v4l2_buffer
*v4l2_buf
)
2042 struct gspca_dev
*gspca_dev
= priv
;
2043 struct gspca_frame
*frame
;
2046 PDEBUG(D_FRAM
, "qbuf %d", v4l2_buf
->index
);
2048 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
))
2049 return -ERESTARTSYS
;
2051 index
= v4l2_buf
->index
;
2052 if ((unsigned) index
>= gspca_dev
->nframes
) {
2054 "qbuf idx %d >= %d", index
, gspca_dev
->nframes
);
2058 if (v4l2_buf
->memory
!= gspca_dev
->memory
) {
2059 PDEBUG(D_FRAM
, "qbuf bad memory type");
2064 frame
= &gspca_dev
->frame
[index
];
2065 if (frame
->v4l2_buf
.flags
& BUF_ALL_FLAGS
) {
2066 PDEBUG(D_FRAM
, "qbuf bad state");
2071 frame
->v4l2_buf
.flags
|= V4L2_BUF_FLAG_QUEUED
;
2073 if (frame
->v4l2_buf
.memory
== V4L2_MEMORY_USERPTR
) {
2074 frame
->v4l2_buf
.m
.userptr
= v4l2_buf
->m
.userptr
;
2075 frame
->v4l2_buf
.length
= v4l2_buf
->length
;
2078 /* put the buffer in the 'queued' queue */
2079 i
= atomic_read(&gspca_dev
->fr_q
);
2080 gspca_dev
->fr_queue
[i
] = index
;
2081 atomic_set(&gspca_dev
->fr_q
, (i
+ 1) % GSPCA_MAX_FRAMES
);
2083 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
2084 v4l2_buf
->flags
&= ~V4L2_BUF_FLAG_DONE
;
2087 mutex_unlock(&gspca_dev
->queue_lock
);
2092 * allocate the resources for read()
2094 static int read_alloc(struct gspca_dev
*gspca_dev
,
2097 struct v4l2_buffer v4l2_buf
;
2100 PDEBUG(D_STREAM
, "read alloc");
2101 if (gspca_dev
->nframes
== 0) {
2102 struct v4l2_requestbuffers rb
;
2104 memset(&rb
, 0, sizeof rb
);
2105 rb
.count
= gspca_dev
->nbufread
;
2106 rb
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2107 rb
.memory
= GSPCA_MEMORY_READ
;
2108 ret
= vidioc_reqbufs(file
, gspca_dev
, &rb
);
2110 PDEBUG(D_STREAM
, "read reqbuf err %d", ret
);
2113 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2114 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2115 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2116 for (i
= 0; i
< gspca_dev
->nbufread
; i
++) {
2118 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2120 PDEBUG(D_STREAM
, "read qbuf err: %d", ret
);
2124 gspca_dev
->memory
= GSPCA_MEMORY_READ
;
2127 /* start streaming */
2128 ret
= vidioc_streamon(file
, gspca_dev
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
2130 PDEBUG(D_STREAM
, "read streamon err %d", ret
);
2134 static unsigned int dev_poll(struct file
*file
, poll_table
*wait
)
2136 struct gspca_dev
*gspca_dev
= file
->private_data
;
2139 PDEBUG(D_FRAM
, "poll");
2141 poll_wait(file
, &gspca_dev
->wq
, wait
);
2143 /* if reqbufs is not done, the user would use read() */
2144 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) {
2145 ret
= read_alloc(gspca_dev
, file
);
2150 if (mutex_lock_interruptible(&gspca_dev
->queue_lock
) != 0)
2153 /* check if an image has been received */
2154 if (gspca_dev
->fr_o
!= atomic_read(&gspca_dev
->fr_i
))
2155 ret
= POLLIN
| POLLRDNORM
; /* yes */
2158 mutex_unlock(&gspca_dev
->queue_lock
);
2159 if (!gspca_dev
->present
)
2164 static ssize_t
dev_read(struct file
*file
, char __user
*data
,
2165 size_t count
, loff_t
*ppos
)
2167 struct gspca_dev
*gspca_dev
= file
->private_data
;
2168 struct gspca_frame
*frame
;
2169 struct v4l2_buffer v4l2_buf
;
2170 struct timeval timestamp
;
2173 PDEBUG(D_FRAM
, "read (%zd)", count
);
2174 if (!gspca_dev
->present
)
2176 if (gspca_dev
->memory
== GSPCA_MEMORY_NO
) { /* first time ? */
2177 ret
= read_alloc(gspca_dev
, file
);
2183 timestamp
= ktime_to_timeval(ktime_get());
2187 memset(&v4l2_buf
, 0, sizeof v4l2_buf
);
2188 v4l2_buf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2189 v4l2_buf
.memory
= GSPCA_MEMORY_READ
;
2190 ret
= vidioc_dqbuf(file
, gspca_dev
, &v4l2_buf
);
2192 PDEBUG(D_STREAM
, "read dqbuf err %d", ret
);
2196 /* if the process slept for more than 1 second,
2197 * get a newer frame */
2198 frame
= &gspca_dev
->frame
[v4l2_buf
.index
];
2200 break; /* avoid infinite loop */
2201 if (frame
->v4l2_buf
.timestamp
.tv_sec
>= timestamp
.tv_sec
)
2203 ret
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2205 PDEBUG(D_STREAM
, "read qbuf err %d", ret
);
2210 /* copy the frame */
2211 if (count
> frame
->v4l2_buf
.bytesused
)
2212 count
= frame
->v4l2_buf
.bytesused
;
2213 ret
= copy_to_user(data
, frame
->data
, count
);
2215 PDEBUG(D_ERR
|D_STREAM
,
2216 "read cp to user lack %d / %zd", ret
, count
);
2222 /* in each case, requeue the buffer */
2223 ret2
= vidioc_qbuf(file
, gspca_dev
, &v4l2_buf
);
2229 static struct v4l2_file_operations dev_fops
= {
2230 .owner
= THIS_MODULE
,
2232 .release
= dev_close
,
2235 .unlocked_ioctl
= video_ioctl2
,
2239 static const struct v4l2_ioctl_ops dev_ioctl_ops
= {
2240 .vidioc_querycap
= vidioc_querycap
,
2241 .vidioc_dqbuf
= vidioc_dqbuf
,
2242 .vidioc_qbuf
= vidioc_qbuf
,
2243 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2244 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2245 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2246 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2247 .vidioc_streamon
= vidioc_streamon
,
2248 .vidioc_queryctrl
= vidioc_queryctrl
,
2249 .vidioc_g_ctrl
= vidioc_g_ctrl
,
2250 .vidioc_s_ctrl
= vidioc_s_ctrl
,
2251 .vidioc_querymenu
= vidioc_querymenu
,
2252 .vidioc_enum_input
= vidioc_enum_input
,
2253 .vidioc_g_input
= vidioc_g_input
,
2254 .vidioc_s_input
= vidioc_s_input
,
2255 .vidioc_reqbufs
= vidioc_reqbufs
,
2256 .vidioc_querybuf
= vidioc_querybuf
,
2257 .vidioc_streamoff
= vidioc_streamoff
,
2258 .vidioc_g_jpegcomp
= vidioc_g_jpegcomp
,
2259 .vidioc_s_jpegcomp
= vidioc_s_jpegcomp
,
2260 .vidioc_g_parm
= vidioc_g_parm
,
2261 .vidioc_s_parm
= vidioc_s_parm
,
2262 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2263 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
2264 #ifdef CONFIG_VIDEO_ADV_DEBUG
2265 .vidioc_g_register
= vidioc_g_register
,
2266 .vidioc_s_register
= vidioc_s_register
,
2268 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
2271 static const struct video_device gspca_template
= {
2272 .name
= "gspca main driver",
2274 .ioctl_ops
= &dev_ioctl_ops
,
2275 .release
= gspca_release
,
2278 /* initialize the controls */
2279 static void ctrls_init(struct gspca_dev
*gspca_dev
)
2281 struct gspca_ctrl
*ctrl
;
2284 for (i
= 0, ctrl
= gspca_dev
->cam
.ctrls
;
2285 i
< gspca_dev
->sd_desc
->nctrls
;
2287 ctrl
->def
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.default_value
;
2288 ctrl
->val
= ctrl
->def
;
2289 ctrl
->min
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.minimum
;
2290 ctrl
->max
= gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.maximum
;
2295 * probe and create a new gspca device
2297 * This function must be called by the sub-driver when it is
2298 * called for probing a new device.
2300 int gspca_dev_probe2(struct usb_interface
*intf
,
2301 const struct usb_device_id
*id
,
2302 const struct sd_desc
*sd_desc
,
2304 struct module
*module
)
2306 struct gspca_dev
*gspca_dev
;
2307 struct usb_device
*dev
= interface_to_usbdev(intf
);
2310 pr_info("%s-" GSPCA_VERSION
" probing %04x:%04x\n",
2311 sd_desc
->name
, id
->idVendor
, id
->idProduct
);
2313 /* create the device */
2314 if (dev_size
< sizeof *gspca_dev
)
2315 dev_size
= sizeof *gspca_dev
;
2316 gspca_dev
= kzalloc(dev_size
, GFP_KERNEL
);
2318 pr_err("couldn't kzalloc gspca struct\n");
2321 gspca_dev
->usb_buf
= kmalloc(USB_BUF_SZ
, GFP_KERNEL
);
2322 if (!gspca_dev
->usb_buf
) {
2323 pr_err("out of memory\n");
2327 gspca_dev
->dev
= dev
;
2328 gspca_dev
->iface
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
2330 /* check if any audio device */
2331 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1) {
2333 struct usb_interface
*intf2
;
2335 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
2336 intf2
= dev
->actconfig
->interface
[i
];
2338 && intf2
->altsetting
!= NULL
2339 && intf2
->altsetting
->desc
.bInterfaceClass
==
2341 gspca_dev
->audio
= 1;
2347 gspca_dev
->sd_desc
= sd_desc
;
2348 gspca_dev
->nbufread
= 2;
2349 gspca_dev
->empty_packet
= -1; /* don't check the empty packets */
2351 /* configure the subdriver and initialize the USB device */
2352 ret
= sd_desc
->config(gspca_dev
, id
);
2355 if (gspca_dev
->cam
.ctrls
!= NULL
)
2356 ctrls_init(gspca_dev
);
2357 ret
= sd_desc
->init(gspca_dev
);
2360 gspca_set_default_mode(gspca_dev
);
2362 ret
= gspca_input_connect(gspca_dev
);
2366 mutex_init(&gspca_dev
->usb_lock
);
2367 mutex_init(&gspca_dev
->queue_lock
);
2368 init_waitqueue_head(&gspca_dev
->wq
);
2370 /* init video stuff */
2371 memcpy(&gspca_dev
->vdev
, &gspca_template
, sizeof gspca_template
);
2372 gspca_dev
->vdev
.parent
= &intf
->dev
;
2373 gspca_dev
->module
= module
;
2374 gspca_dev
->present
= 1;
2375 ret
= video_register_device(&gspca_dev
->vdev
,
2379 pr_err("video_register_device err %d\n", ret
);
2383 usb_set_intfdata(intf
, gspca_dev
);
2384 PDEBUG(D_PROBE
, "%s created", video_device_node_name(&gspca_dev
->vdev
));
2386 gspca_input_create_urb(gspca_dev
);
2390 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2391 if (gspca_dev
->input_dev
)
2392 input_unregister_device(gspca_dev
->input_dev
);
2394 kfree(gspca_dev
->usb_buf
);
2398 EXPORT_SYMBOL(gspca_dev_probe2
);
2400 /* same function as the previous one, but check the interface */
2401 int gspca_dev_probe(struct usb_interface
*intf
,
2402 const struct usb_device_id
*id
,
2403 const struct sd_desc
*sd_desc
,
2405 struct module
*module
)
2407 struct usb_device
*dev
= interface_to_usbdev(intf
);
2409 /* we don't handle multi-config cameras */
2410 if (dev
->descriptor
.bNumConfigurations
!= 1) {
2411 pr_err("%04x:%04x too many config\n",
2412 id
->idVendor
, id
->idProduct
);
2416 /* the USB video interface must be the first one */
2417 if (dev
->actconfig
->desc
.bNumInterfaces
!= 1
2418 && intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2421 return gspca_dev_probe2(intf
, id
, sd_desc
, dev_size
, module
);
2423 EXPORT_SYMBOL(gspca_dev_probe
);
2428 * This function must be called by the sub-driver
2429 * when the device disconnects, after the specific resources are freed.
2431 void gspca_disconnect(struct usb_interface
*intf
)
2433 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2434 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2435 struct input_dev
*input_dev
;
2438 PDEBUG(D_PROBE
, "%s disconnect",
2439 video_device_node_name(&gspca_dev
->vdev
));
2440 mutex_lock(&gspca_dev
->usb_lock
);
2442 gspca_dev
->present
= 0;
2443 wake_up_interruptible(&gspca_dev
->wq
);
2445 destroy_urbs(gspca_dev
);
2447 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
2448 gspca_input_destroy_urb(gspca_dev
);
2449 input_dev
= gspca_dev
->input_dev
;
2451 gspca_dev
->input_dev
= NULL
;
2452 input_unregister_device(input_dev
);
2456 /* the device is freed at exit of this function */
2457 gspca_dev
->dev
= NULL
;
2458 mutex_unlock(&gspca_dev
->usb_lock
);
2460 usb_set_intfdata(intf
, NULL
);
2462 /* release the device */
2463 /* (this will call gspca_release() immediately or on last close) */
2464 video_unregister_device(&gspca_dev
->vdev
);
2466 /* PDEBUG(D_PROBE, "disconnect complete"); */
2468 EXPORT_SYMBOL(gspca_disconnect
);
2471 int gspca_suspend(struct usb_interface
*intf
, pm_message_t message
)
2473 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2475 if (!gspca_dev
->streaming
)
2477 gspca_dev
->frozen
= 1; /* avoid urb error messages */
2478 if (gspca_dev
->sd_desc
->stopN
)
2479 gspca_dev
->sd_desc
->stopN(gspca_dev
);
2480 destroy_urbs(gspca_dev
);
2481 gspca_input_destroy_urb(gspca_dev
);
2482 gspca_set_alt0(gspca_dev
);
2483 if (gspca_dev
->sd_desc
->stop0
)
2484 gspca_dev
->sd_desc
->stop0(gspca_dev
);
2487 EXPORT_SYMBOL(gspca_suspend
);
2489 int gspca_resume(struct usb_interface
*intf
)
2491 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
2493 gspca_dev
->frozen
= 0;
2494 gspca_dev
->sd_desc
->init(gspca_dev
);
2495 gspca_input_create_urb(gspca_dev
);
2496 if (gspca_dev
->streaming
)
2497 return gspca_init_transfer(gspca_dev
);
2500 EXPORT_SYMBOL(gspca_resume
);
2502 /* -- cam driver utility functions -- */
2504 /* auto gain and exposure algorithm based on the knee algorithm described here:
2505 http://ytse.tricolour.net/docs/LowLightOptimization.html
2507 Returns 0 if no changes were made, 1 if the gain and or exposure settings
2509 int gspca_auto_gain_n_exposure(struct gspca_dev
*gspca_dev
, int avg_lum
,
2510 int desired_avg_lum
, int deadzone
, int gain_knee
, int exposure_knee
)
2512 int i
, steps
, gain
, orig_gain
, exposure
, orig_exposure
, autogain
;
2513 const struct ctrl
*gain_ctrl
= NULL
;
2514 const struct ctrl
*exposure_ctrl
= NULL
;
2515 const struct ctrl
*autogain_ctrl
= NULL
;
2518 for (i
= 0; i
< gspca_dev
->sd_desc
->nctrls
; i
++) {
2519 if (gspca_dev
->ctrl_dis
& (1 << i
))
2521 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_GAIN
)
2522 gain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2523 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_EXPOSURE
)
2524 exposure_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2525 if (gspca_dev
->sd_desc
->ctrls
[i
].qctrl
.id
== V4L2_CID_AUTOGAIN
)
2526 autogain_ctrl
= &gspca_dev
->sd_desc
->ctrls
[i
];
2528 if (!gain_ctrl
|| !exposure_ctrl
|| !autogain_ctrl
) {
2529 PDEBUG(D_ERR
, "Error: gspca_auto_gain_n_exposure called "
2530 "on cam without (auto)gain/exposure");
2534 if (gain_ctrl
->get(gspca_dev
, &gain
) ||
2535 exposure_ctrl
->get(gspca_dev
, &exposure
) ||
2536 autogain_ctrl
->get(gspca_dev
, &autogain
) || !autogain
)
2540 orig_exposure
= exposure
;
2542 /* If we are of a multiple of deadzone, do multiple steps to reach the
2543 desired lumination fast (with the risc of a slight overshoot) */
2544 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
2546 PDEBUG(D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d",
2547 avg_lum
, desired_avg_lum
, steps
);
2549 for (i
= 0; i
< steps
; i
++) {
2550 if (avg_lum
> desired_avg_lum
) {
2551 if (gain
> gain_knee
)
2553 else if (exposure
> exposure_knee
)
2555 else if (gain
> gain_ctrl
->qctrl
.default_value
)
2557 else if (exposure
> exposure_ctrl
->qctrl
.minimum
)
2559 else if (gain
> gain_ctrl
->qctrl
.minimum
)
2564 if (gain
< gain_ctrl
->qctrl
.default_value
)
2566 else if (exposure
< exposure_knee
)
2568 else if (gain
< gain_knee
)
2570 else if (exposure
< exposure_ctrl
->qctrl
.maximum
)
2572 else if (gain
< gain_ctrl
->qctrl
.maximum
)
2579 if (gain
!= orig_gain
) {
2580 gain_ctrl
->set(gspca_dev
, gain
);
2583 if (exposure
!= orig_exposure
) {
2584 exposure_ctrl
->set(gspca_dev
, exposure
);
2590 EXPORT_SYMBOL(gspca_auto_gain_n_exposure
);
2592 /* -- module insert / remove -- */
2593 static int __init
gspca_init(void)
2595 pr_info("v" GSPCA_VERSION
" registered\n");
2598 static void __exit
gspca_exit(void)
2602 module_init(gspca_init
);
2603 module_exit(gspca_exit
);
2606 module_param_named(debug
, gspca_debug
, int, 0644);
2607 MODULE_PARM_DESC(debug
,
2608 "Debug (bit) 0x01:error 0x02:probe 0x04:config"
2609 " 0x08:stream 0x10:frame 0x20:packet"