sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / media / usb / gspca / gspca.c
blobf3a7ace0fac9cc8bd4e2b2c8b82038dc85aa7068
1 /*
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
17 * for more details.
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>
29 #include <linux/fs.h>
30 #include <linux/vmalloc.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/pagemap.h>
36 #include <linux/io.h>
37 #include <asm/page.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>
45 #include "gspca.h"
47 #if IS_ENABLED(CONFIG_INPUT)
48 #include <linux/input.h>
49 #include <linux/usb/input.h>
50 #endif
52 /* global values */
53 #define DEF_NURBS 3 /* default number of URBs */
54 #if DEF_NURBS > MAX_NURBS
55 #error "DEF_NURBS too big"
56 #endif
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);
63 int gspca_debug;
64 EXPORT_SYMBOL(gspca_debug);
66 static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
67 __u32 pixfmt, int w, int h)
69 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
70 PDEBUG(debug, "%s %c%c%c%c %dx%d",
71 txt,
72 pixfmt & 0xff,
73 (pixfmt >> 8) & 0xff,
74 (pixfmt >> 16) & 0xff,
75 pixfmt >> 24,
76 w, h);
77 } else {
78 PDEBUG(debug, "%s 0x%08x %dx%d",
79 txt,
80 pixfmt,
81 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)
92 * VMA operations.
94 static void gspca_vm_open(struct vm_area_struct *vma)
96 struct gspca_frame *frame = vma->vm_private_data;
98 frame->vma_use_count++;
99 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
102 static void gspca_vm_close(struct vm_area_struct *vma)
104 struct gspca_frame *frame = vma->vm_private_data;
106 if (--frame->vma_use_count <= 0)
107 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
110 static const struct vm_operations_struct gspca_vm_ops = {
111 .open = gspca_vm_open,
112 .close = gspca_vm_close,
116 * Input and interrupt endpoint handling functions
118 #if IS_ENABLED(CONFIG_INPUT)
119 static void int_irq(struct urb *urb)
121 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
122 int ret;
124 ret = urb->status;
125 switch (ret) {
126 case 0:
127 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
128 urb->transfer_buffer, urb->actual_length) < 0) {
129 PERR("Unknown packet received");
131 break;
133 case -ENOENT:
134 case -ECONNRESET:
135 case -ENODEV:
136 case -ESHUTDOWN:
137 /* Stop is requested either by software or hardware is gone,
138 * keep the ret value non-zero and don't resubmit later.
140 break;
142 default:
143 PERR("URB error %i, resubmitting", urb->status);
144 urb->status = 0;
145 ret = 0;
148 if (ret == 0) {
149 ret = usb_submit_urb(urb, GFP_ATOMIC);
150 if (ret < 0)
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;
158 int err = 0;
160 dev->input_dev = NULL;
161 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
162 input_dev = input_allocate_device();
163 if (!input_dev)
164 return -ENOMEM;
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);
179 if (err) {
180 pr_err("Input device registration failed with error %i\n",
181 err);
182 input_dev->dev.parent = NULL;
183 input_free_device(input_dev);
184 } else {
185 dev->input_dev = input_dev;
189 return err;
192 static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
193 struct usb_endpoint_descriptor *ep)
195 unsigned int buffer_len;
196 int interval;
197 struct urb *urb;
198 struct usb_device *dev;
199 void *buffer = NULL;
200 int ret = -EINVAL;
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);
211 if (!urb) {
212 ret = -ENOMEM;
213 goto error;
216 buffer = usb_alloc_coherent(dev, buffer_len,
217 GFP_KERNEL, &urb->transfer_dma);
218 if (!buffer) {
219 ret = -ENOMEM;
220 goto error_buffer;
222 usb_fill_int_urb(urb, dev,
223 usb_rcvintpipe(dev, ep->bEndpointAddress),
224 buffer, buffer_len,
225 int_irq, (void *)gspca_dev, interval);
226 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
227 ret = usb_submit_urb(urb, GFP_KERNEL);
228 if (ret < 0) {
229 PERR("submit int URB failed with error %i", ret);
230 goto error_submit;
232 gspca_dev->int_urb = urb;
233 return ret;
235 error_submit:
236 usb_free_coherent(dev,
237 urb->transfer_buffer_length,
238 urb->transfer_buffer,
239 urb->transfer_dma);
240 error_buffer:
241 usb_free_urb(urb);
242 error:
243 return ret;
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;
251 int i;
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);
262 break;
268 static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
270 struct urb *urb;
272 urb = gspca_dev->int_urb;
273 if (urb) {
274 gspca_dev->int_urb = NULL;
275 usb_kill_urb(urb);
276 usb_free_coherent(gspca_dev->dev,
277 urb->transfer_buffer_length,
278 urb->transfer_buffer,
279 urb->transfer_dma);
280 usb_free_urb(urb);
283 #else
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)
294 return 0;
296 #endif
299 * fill a video frame from an URB and resubmit
301 static void fill_frame(struct gspca_dev *gspca_dev,
302 struct urb *urb)
304 u8 *data; /* address of data in the iso message */
305 int i, len, st;
306 cam_pkt_op pkt_scan;
308 if (urb->status != 0) {
309 if (urb->status == -ESHUTDOWN)
310 return; /* disconnection */
311 #ifdef CONFIG_PM
312 if (gspca_dev->frozen)
313 return;
314 #endif
315 PERR("urb status: %d", urb->status);
316 urb->status = 0;
317 goto resubmit;
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;
325 if (st) {
326 pr_err("ISOC data error: [%d] len=%d, status=%d\n",
327 i, len, st);
328 gspca_dev->last_packet_type = DISCARD_PACKET;
329 continue;
331 if (len == 0) {
332 if (gspca_dev->empty_packet == 0)
333 gspca_dev->empty_packet = 1;
334 continue;
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);
345 resubmit:
346 /* resubmit the URB */
347 st = usb_submit_urb(urb, GFP_ATOMIC);
348 if (st < 0)
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)
363 return;
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;
373 int st;
375 PDEBUG(D_PACK, "bulk irq");
376 if (!gspca_dev->streaming)
377 return;
378 switch (urb->status) {
379 case 0:
380 break;
381 case -ESHUTDOWN:
382 return; /* disconnection */
383 default:
384 #ifdef CONFIG_PM
385 if (gspca_dev->frozen)
386 return;
387 #endif
388 PERR("urb status: %d", urb->status);
389 urb->status = 0;
390 goto resubmit;
393 PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
394 gspca_dev->sd_desc->pkt_scan(gspca_dev,
395 urb->transfer_buffer,
396 urb->actual_length);
398 resubmit:
399 /* resubmit the URB */
400 if (gspca_dev->cam.bulk_nurbs != 0) {
401 st = usb_submit_urb(urb, GFP_ATOMIC);
402 if (st < 0)
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
413 * - one FIRST_PACKET
414 * - 0 or many INTER_PACKETs
415 * - one LAST_PACKET
416 * DISCARD_PACKET invalidates the whole frame.
418 void gspca_frame_add(struct gspca_dev *gspca_dev,
419 enum gspca_packet_type packet_type,
420 const u8 *data,
421 int len)
423 struct gspca_frame *frame;
424 int i, j;
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++;
435 return;
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;
443 } else {
444 switch (gspca_dev->last_packet_type) {
445 case DISCARD_PACKET:
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;
451 return;
452 case LAST_PACKET:
453 return;
457 /* append the packet to the frame buffer */
458 if (len > 0) {
459 if (gspca_dev->image_len + len > gspca_dev->frsz) {
460 PERR("frame overflow %d > %d",
461 gspca_dev->image_len + len,
462 gspca_dev->frsz);
463 packet_type = DISCARD_PACKET;
464 } else {
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");
468 return;
471 memcpy(gspca_dev->image + gspca_dev->image_len,
472 data, 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
480 * in the queue */
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;
504 unsigned int frsz;
505 int i;
507 frsz = gspca_dev->pixfmt.sizeimage;
508 PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
509 frsz = PAGE_ALIGN(frsz);
510 if (count >= GSPCA_MAX_FRAMES)
511 count = GSPCA_MAX_FRAMES - 1;
512 gspca_dev->frbuf = vmalloc_32(frsz * count);
513 if (!gspca_dev->frbuf) {
514 pr_err("frame alloc failed\n");
515 return -ENOMEM;
517 gspca_dev->capt_file = file;
518 gspca_dev->memory = memory;
519 gspca_dev->frsz = frsz;
520 gspca_dev->nframes = count;
521 for (i = 0; i < count; i++) {
522 frame = &gspca_dev->frame[i];
523 frame->v4l2_buf.index = i;
524 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
525 frame->v4l2_buf.flags = 0;
526 frame->v4l2_buf.field = V4L2_FIELD_NONE;
527 frame->v4l2_buf.length = frsz;
528 frame->v4l2_buf.memory = memory;
529 frame->v4l2_buf.sequence = 0;
530 frame->data = gspca_dev->frbuf + i * frsz;
531 frame->v4l2_buf.m.offset = i * frsz;
533 atomic_set(&gspca_dev->fr_q, 0);
534 atomic_set(&gspca_dev->fr_i, 0);
535 gspca_dev->fr_o = 0;
536 return 0;
539 static void frame_free(struct gspca_dev *gspca_dev)
541 int i;
543 PDEBUG(D_STREAM, "frame free");
544 if (gspca_dev->frbuf != NULL) {
545 vfree(gspca_dev->frbuf);
546 gspca_dev->frbuf = NULL;
547 for (i = 0; i < gspca_dev->nframes; i++)
548 gspca_dev->frame[i].data = NULL;
550 gspca_dev->nframes = 0;
551 gspca_dev->frsz = 0;
552 gspca_dev->capt_file = NULL;
553 gspca_dev->memory = GSPCA_MEMORY_NO;
556 static void destroy_urbs(struct gspca_dev *gspca_dev)
558 struct urb *urb;
559 unsigned int i;
561 PDEBUG(D_STREAM, "kill transfer");
562 for (i = 0; i < MAX_NURBS; i++) {
563 urb = gspca_dev->urb[i];
564 if (urb == NULL)
565 break;
567 gspca_dev->urb[i] = NULL;
568 usb_kill_urb(urb);
569 usb_free_coherent(gspca_dev->dev,
570 urb->transfer_buffer_length,
571 urb->transfer_buffer,
572 urb->transfer_dma);
573 usb_free_urb(urb);
577 static int gspca_set_alt0(struct gspca_dev *gspca_dev)
579 int ret;
581 if (gspca_dev->alt == 0)
582 return 0;
583 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
584 if (ret < 0)
585 pr_err("set alt 0 err %d\n", ret);
586 return ret;
589 /* Note: both the queue and the usb locks should be held when calling this */
590 static void gspca_stream_off(struct gspca_dev *gspca_dev)
592 gspca_dev->streaming = 0;
593 gspca_dev->usb_err = 0;
594 if (gspca_dev->sd_desc->stopN)
595 gspca_dev->sd_desc->stopN(gspca_dev);
596 destroy_urbs(gspca_dev);
597 gspca_input_destroy_urb(gspca_dev);
598 gspca_set_alt0(gspca_dev);
599 gspca_input_create_urb(gspca_dev);
600 if (gspca_dev->sd_desc->stop0)
601 gspca_dev->sd_desc->stop0(gspca_dev);
602 PDEBUG(D_STREAM, "stream off OK");
606 * look for an input transfer endpoint in an alternate setting
608 static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
609 int xfer)
611 struct usb_host_endpoint *ep;
612 int i, attr;
614 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
615 ep = &alt->endpoint[i];
616 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
617 if (attr == xfer
618 && ep->desc.wMaxPacketSize != 0
619 && usb_endpoint_dir_in(&ep->desc))
620 return ep;
622 return NULL;
625 /* compute the minimum bandwidth for the current transfer */
626 static u32 which_bandwidth(struct gspca_dev *gspca_dev)
628 u32 bandwidth;
630 /* get the (max) image size */
631 bandwidth = gspca_dev->pixfmt.sizeimage;
633 /* if the image is compressed, estimate its mean size */
634 if (!gspca_dev->cam.needs_full_bandwidth &&
635 bandwidth < gspca_dev->pixfmt.width *
636 gspca_dev->pixfmt.height)
637 bandwidth = bandwidth * 3 / 8; /* 0.375 */
639 /* estimate the frame rate */
640 if (gspca_dev->sd_desc->get_streamparm) {
641 struct v4l2_streamparm parm;
643 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
644 bandwidth *= parm.parm.capture.timeperframe.denominator;
645 bandwidth /= parm.parm.capture.timeperframe.numerator;
646 } else {
648 /* don't hope more than 15 fps with USB 1.1 and
649 * image resolution >= 640x480 */
650 if (gspca_dev->pixfmt.width >= 640
651 && gspca_dev->dev->speed == USB_SPEED_FULL)
652 bandwidth *= 15; /* 15 fps */
653 else
654 bandwidth *= 30; /* 30 fps */
657 PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
658 return bandwidth;
661 /* endpoint table */
662 #define MAX_ALT 16
663 struct ep_tb_s {
664 u32 alt;
665 u32 bandwidth;
669 * build the table of the endpoints
670 * and compute the minimum bandwidth for the image transfer
672 static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
673 struct usb_interface *intf,
674 struct ep_tb_s *ep_tb)
676 struct usb_host_endpoint *ep;
677 int i, j, nbalt, psize, found;
678 u32 bandwidth, last_bw;
680 nbalt = intf->num_altsetting;
681 if (nbalt > MAX_ALT)
682 nbalt = MAX_ALT; /* fixme: should warn */
684 /* build the endpoint table */
685 i = 0;
686 last_bw = 0;
687 for (;;) {
688 ep_tb->bandwidth = 2000 * 2000 * 120;
689 found = 0;
690 for (j = 0; j < nbalt; j++) {
691 ep = alt_xfer(&intf->altsetting[j],
692 USB_ENDPOINT_XFER_ISOC);
693 if (ep == NULL)
694 continue;
695 if (ep->desc.bInterval == 0) {
696 pr_err("alt %d iso endp with 0 interval\n", j);
697 continue;
699 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
700 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
701 bandwidth = psize * 1000;
702 if (gspca_dev->dev->speed == USB_SPEED_HIGH
703 || gspca_dev->dev->speed == USB_SPEED_SUPER)
704 bandwidth *= 8;
705 bandwidth /= 1 << (ep->desc.bInterval - 1);
706 if (bandwidth <= last_bw)
707 continue;
708 if (bandwidth < ep_tb->bandwidth) {
709 ep_tb->bandwidth = bandwidth;
710 ep_tb->alt = j;
711 found = 1;
714 if (!found)
715 break;
716 PDEBUG(D_STREAM, "alt %d bandwidth %d",
717 ep_tb->alt, ep_tb->bandwidth);
718 last_bw = ep_tb->bandwidth;
719 i++;
720 ep_tb++;
724 * If the camera:
725 * has a usb audio class interface (a built in usb mic); and
726 * is a usb 1 full speed device; and
727 * uses the max full speed iso bandwidth; and
728 * and has more than 1 alt setting
729 * then skip the highest alt setting to spare bandwidth for the mic
731 if (gspca_dev->audio &&
732 gspca_dev->dev->speed == USB_SPEED_FULL &&
733 last_bw >= 1000000 &&
734 i > 1) {
735 PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
736 i--;
737 ep_tb--;
740 /* get the requested bandwidth and start at the highest atlsetting */
741 bandwidth = which_bandwidth(gspca_dev);
742 ep_tb--;
743 while (i > 1) {
744 ep_tb--;
745 if (ep_tb->bandwidth < bandwidth)
746 break;
747 i--;
749 return i;
753 * create the URBs for image transfer
755 static int create_urbs(struct gspca_dev *gspca_dev,
756 struct usb_host_endpoint *ep)
758 struct urb *urb;
759 int n, nurbs, i, psize, npkt, bsize;
761 /* calculate the packet size and the number of packets */
762 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
764 if (!gspca_dev->cam.bulk) { /* isoc */
766 /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
767 if (gspca_dev->pkt_size == 0)
768 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
769 else
770 psize = gspca_dev->pkt_size;
771 npkt = gspca_dev->cam.npkt;
772 if (npkt == 0)
773 npkt = 32; /* default value */
774 bsize = psize * npkt;
775 PDEBUG(D_STREAM,
776 "isoc %d pkts size %d = bsize:%d",
777 npkt, psize, bsize);
778 nurbs = DEF_NURBS;
779 } else { /* bulk */
780 npkt = 0;
781 bsize = gspca_dev->cam.bulk_size;
782 if (bsize == 0)
783 bsize = psize;
784 PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
785 if (gspca_dev->cam.bulk_nurbs != 0)
786 nurbs = gspca_dev->cam.bulk_nurbs;
787 else
788 nurbs = 1;
791 for (n = 0; n < nurbs; n++) {
792 urb = usb_alloc_urb(npkt, GFP_KERNEL);
793 if (!urb) {
794 pr_err("usb_alloc_urb failed\n");
795 return -ENOMEM;
797 gspca_dev->urb[n] = urb;
798 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
799 bsize,
800 GFP_KERNEL,
801 &urb->transfer_dma);
803 if (urb->transfer_buffer == NULL) {
804 pr_err("usb_alloc_coherent failed\n");
805 return -ENOMEM;
807 urb->dev = gspca_dev->dev;
808 urb->context = gspca_dev;
809 urb->transfer_buffer_length = bsize;
810 if (npkt != 0) { /* ISOC */
811 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
812 ep->desc.bEndpointAddress);
813 urb->transfer_flags = URB_ISO_ASAP
814 | URB_NO_TRANSFER_DMA_MAP;
815 urb->interval = 1 << (ep->desc.bInterval - 1);
816 urb->complete = isoc_irq;
817 urb->number_of_packets = npkt;
818 for (i = 0; i < npkt; i++) {
819 urb->iso_frame_desc[i].length = psize;
820 urb->iso_frame_desc[i].offset = psize * i;
822 } else { /* bulk */
823 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
824 ep->desc.bEndpointAddress);
825 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
826 urb->complete = bulk_irq;
829 return 0;
833 * start the USB transfer
835 static int gspca_init_transfer(struct gspca_dev *gspca_dev)
837 struct usb_interface *intf;
838 struct usb_host_endpoint *ep;
839 struct urb *urb;
840 struct ep_tb_s ep_tb[MAX_ALT];
841 int n, ret, xfer, alt, alt_idx;
843 /* reset the streaming variables */
844 gspca_dev->image = NULL;
845 gspca_dev->image_len = 0;
846 gspca_dev->last_packet_type = DISCARD_PACKET;
847 gspca_dev->sequence = 0;
849 gspca_dev->usb_err = 0;
851 /* do the specific subdriver stuff before endpoint selection */
852 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
853 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
854 if (gspca_dev->sd_desc->isoc_init) {
855 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
856 if (ret < 0)
857 return ret;
859 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
860 : USB_ENDPOINT_XFER_ISOC;
862 /* if bulk or the subdriver forced an altsetting, get the endpoint */
863 if (gspca_dev->alt != 0) {
864 gspca_dev->alt--; /* (previous version compatibility) */
865 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
866 if (ep == NULL) {
867 pr_err("bad altsetting %d\n", gspca_dev->alt);
868 return -EIO;
870 ep_tb[0].alt = gspca_dev->alt;
871 alt_idx = 1;
872 } else {
874 /* else, compute the minimum bandwidth
875 * and build the endpoint table */
876 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
877 if (alt_idx <= 0) {
878 pr_err("no transfer endpoint found\n");
879 return -EIO;
883 /* set the highest alternate setting and
884 * loop until urb submit succeeds */
885 gspca_input_destroy_urb(gspca_dev);
887 gspca_dev->alt = ep_tb[--alt_idx].alt;
888 alt = -1;
889 for (;;) {
890 if (alt != gspca_dev->alt) {
891 alt = gspca_dev->alt;
892 if (intf->num_altsetting > 1) {
893 ret = usb_set_interface(gspca_dev->dev,
894 gspca_dev->iface,
895 alt);
896 if (ret < 0) {
897 if (ret == -ENOSPC)
898 goto retry; /*fixme: ugly*/
899 pr_err("set alt %d err %d\n", alt, ret);
900 goto out;
904 if (!gspca_dev->cam.no_urb_create) {
905 PDEBUG(D_STREAM, "init transfer alt %d", alt);
906 ret = create_urbs(gspca_dev,
907 alt_xfer(&intf->altsetting[alt], xfer));
908 if (ret < 0) {
909 destroy_urbs(gspca_dev);
910 goto out;
914 /* clear the bulk endpoint */
915 if (gspca_dev->cam.bulk)
916 usb_clear_halt(gspca_dev->dev,
917 gspca_dev->urb[0]->pipe);
919 /* start the cam */
920 ret = gspca_dev->sd_desc->start(gspca_dev);
921 if (ret < 0) {
922 destroy_urbs(gspca_dev);
923 goto out;
925 gspca_dev->streaming = 1;
926 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
928 /* some bulk transfers are started by the subdriver */
929 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
930 break;
932 /* submit the URBs */
933 for (n = 0; n < MAX_NURBS; n++) {
934 urb = gspca_dev->urb[n];
935 if (urb == NULL)
936 break;
937 ret = usb_submit_urb(urb, GFP_KERNEL);
938 if (ret < 0)
939 break;
941 if (ret >= 0)
942 break; /* transfer is started */
944 /* something when wrong
945 * stop the webcam and free the transfer resources */
946 gspca_stream_off(gspca_dev);
947 if (ret != -ENOSPC) {
948 pr_err("usb_submit_urb alt %d err %d\n",
949 gspca_dev->alt, ret);
950 goto out;
953 /* the bandwidth is not wide enough
954 * negotiate or try a lower alternate setting */
955 retry:
956 PERR("alt %d - bandwidth not wide enough, trying again", alt);
957 msleep(20); /* wait for kill complete */
958 if (gspca_dev->sd_desc->isoc_nego) {
959 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
960 if (ret < 0)
961 goto out;
962 } else {
963 if (alt_idx <= 0) {
964 pr_err("no transfer endpoint found\n");
965 ret = -EIO;
966 goto out;
968 gspca_dev->alt = ep_tb[--alt_idx].alt;
971 out:
972 gspca_input_create_urb(gspca_dev);
973 return ret;
976 static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
978 int i;
980 i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
981 gspca_dev->curr_mode = i;
982 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
984 /* does nothing if ctrl_handler == NULL */
985 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
988 static int wxh_to_mode(struct gspca_dev *gspca_dev,
989 int width, int height)
991 int i;
993 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
994 if (width >= gspca_dev->cam.cam_mode[i].width
995 && height >= gspca_dev->cam.cam_mode[i].height)
996 break;
998 return i;
1002 * search a mode with the right pixel format
1004 static int gspca_get_mode(struct gspca_dev *gspca_dev,
1005 int mode,
1006 int pixfmt)
1008 int modeU, modeD;
1010 modeU = modeD = mode;
1011 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
1012 if (--modeD >= 0) {
1013 if (gspca_dev->cam.cam_mode[modeD].pixelformat
1014 == pixfmt)
1015 return modeD;
1017 if (++modeU < gspca_dev->cam.nmodes) {
1018 if (gspca_dev->cam.cam_mode[modeU].pixelformat
1019 == pixfmt)
1020 return modeU;
1023 return -EINVAL;
1026 #ifdef CONFIG_VIDEO_ADV_DEBUG
1027 static int vidioc_g_chip_info(struct file *file, void *priv,
1028 struct v4l2_dbg_chip_info *chip)
1030 struct gspca_dev *gspca_dev = video_drvdata(file);
1032 gspca_dev->usb_err = 0;
1033 if (gspca_dev->sd_desc->get_chip_info)
1034 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
1035 return chip->match.addr ? -EINVAL : 0;
1038 static int vidioc_g_register(struct file *file, void *priv,
1039 struct v4l2_dbg_register *reg)
1041 struct gspca_dev *gspca_dev = video_drvdata(file);
1043 gspca_dev->usb_err = 0;
1044 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
1047 static int vidioc_s_register(struct file *file, void *priv,
1048 const struct v4l2_dbg_register *reg)
1050 struct gspca_dev *gspca_dev = video_drvdata(file);
1052 gspca_dev->usb_err = 0;
1053 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
1055 #endif
1057 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1058 struct v4l2_fmtdesc *fmtdesc)
1060 struct gspca_dev *gspca_dev = video_drvdata(file);
1061 int i, j, index;
1062 __u32 fmt_tb[8];
1064 /* give an index to each format */
1065 index = 0;
1066 j = 0;
1067 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1068 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1069 j = 0;
1070 for (;;) {
1071 if (fmt_tb[j] == fmt_tb[index])
1072 break;
1073 j++;
1075 if (j == index) {
1076 if (fmtdesc->index == index)
1077 break; /* new format */
1078 index++;
1079 if (index >= ARRAY_SIZE(fmt_tb))
1080 return -EINVAL;
1083 if (i < 0)
1084 return -EINVAL; /* no more format */
1086 fmtdesc->pixelformat = fmt_tb[index];
1087 if (gspca_dev->cam.cam_mode[i].sizeimage <
1088 gspca_dev->cam.cam_mode[i].width *
1089 gspca_dev->cam.cam_mode[i].height)
1090 fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
1091 fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
1092 fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
1093 fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
1094 fmtdesc->description[3] = fmtdesc->pixelformat >> 24;
1095 fmtdesc->description[4] = '\0';
1096 return 0;
1099 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1100 struct v4l2_format *fmt)
1102 struct gspca_dev *gspca_dev = video_drvdata(file);
1104 fmt->fmt.pix = gspca_dev->pixfmt;
1105 /* some drivers use priv internally, zero it before giving it to
1106 userspace */
1107 fmt->fmt.pix.priv = 0;
1108 return 0;
1111 static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1112 struct v4l2_format *fmt)
1114 int w, h, mode, mode2;
1116 w = fmt->fmt.pix.width;
1117 h = fmt->fmt.pix.height;
1119 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1120 fmt->fmt.pix.pixelformat, w, h);
1122 /* search the closest mode for width and height */
1123 mode = wxh_to_mode(gspca_dev, w, h);
1125 /* OK if right palette */
1126 if (gspca_dev->cam.cam_mode[mode].pixelformat
1127 != fmt->fmt.pix.pixelformat) {
1129 /* else, search the closest mode with the same pixel format */
1130 mode2 = gspca_get_mode(gspca_dev, mode,
1131 fmt->fmt.pix.pixelformat);
1132 if (mode2 >= 0)
1133 mode = mode2;
1135 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1136 if (gspca_dev->sd_desc->try_fmt) {
1137 /* pass original resolution to subdriver try_fmt */
1138 fmt->fmt.pix.width = w;
1139 fmt->fmt.pix.height = h;
1140 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
1142 /* some drivers use priv internally, zero it before giving it to
1143 userspace */
1144 fmt->fmt.pix.priv = 0;
1145 return mode; /* used when s_fmt */
1148 static int vidioc_try_fmt_vid_cap(struct file *file,
1149 void *priv,
1150 struct v4l2_format *fmt)
1152 struct gspca_dev *gspca_dev = video_drvdata(file);
1153 int ret;
1155 ret = try_fmt_vid_cap(gspca_dev, fmt);
1156 if (ret < 0)
1157 return ret;
1158 return 0;
1161 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1162 struct v4l2_format *fmt)
1164 struct gspca_dev *gspca_dev = video_drvdata(file);
1165 int ret;
1167 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1168 return -ERESTARTSYS;
1170 ret = try_fmt_vid_cap(gspca_dev, fmt);
1171 if (ret < 0)
1172 goto out;
1174 if (gspca_dev->nframes != 0
1175 && fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
1176 ret = -EINVAL;
1177 goto out;
1180 if (gspca_dev->streaming) {
1181 ret = -EBUSY;
1182 goto out;
1184 gspca_dev->curr_mode = ret;
1185 if (gspca_dev->sd_desc->try_fmt)
1186 /* subdriver try_fmt can modify format parameters */
1187 gspca_dev->pixfmt = fmt->fmt.pix;
1188 else
1189 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[ret];
1191 ret = 0;
1192 out:
1193 mutex_unlock(&gspca_dev->queue_lock);
1194 return ret;
1197 static int vidioc_enum_framesizes(struct file *file, void *priv,
1198 struct v4l2_frmsizeenum *fsize)
1200 struct gspca_dev *gspca_dev = video_drvdata(file);
1201 int i;
1202 __u32 index = 0;
1204 if (gspca_dev->sd_desc->enum_framesizes)
1205 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
1207 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1208 if (fsize->pixel_format !=
1209 gspca_dev->cam.cam_mode[i].pixelformat)
1210 continue;
1212 if (fsize->index == index) {
1213 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1214 fsize->discrete.width =
1215 gspca_dev->cam.cam_mode[i].width;
1216 fsize->discrete.height =
1217 gspca_dev->cam.cam_mode[i].height;
1218 return 0;
1220 index++;
1223 return -EINVAL;
1226 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1227 struct v4l2_frmivalenum *fival)
1229 struct gspca_dev *gspca_dev = video_drvdata(filp);
1230 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1231 __u32 i;
1233 if (gspca_dev->cam.mode_framerates == NULL ||
1234 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1235 return -EINVAL;
1237 if (fival->pixel_format !=
1238 gspca_dev->cam.cam_mode[mode].pixelformat)
1239 return -EINVAL;
1241 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1242 if (fival->index == i) {
1243 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1244 fival->discrete.numerator = 1;
1245 fival->discrete.denominator =
1246 gspca_dev->cam.mode_framerates[mode].rates[i];
1247 return 0;
1251 return -EINVAL;
1254 static void gspca_release(struct v4l2_device *v4l2_device)
1256 struct gspca_dev *gspca_dev =
1257 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1259 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1260 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1261 kfree(gspca_dev->usb_buf);
1262 kfree(gspca_dev);
1265 static int dev_open(struct file *file)
1267 struct gspca_dev *gspca_dev = video_drvdata(file);
1268 int ret;
1270 PDEBUG(D_STREAM, "[%s] open", current->comm);
1272 /* protect the subdriver against rmmod */
1273 if (!try_module_get(gspca_dev->module))
1274 return -ENODEV;
1276 ret = v4l2_fh_open(file);
1277 if (ret)
1278 module_put(gspca_dev->module);
1279 return ret;
1282 static int dev_close(struct file *file)
1284 struct gspca_dev *gspca_dev = video_drvdata(file);
1286 PDEBUG(D_STREAM, "[%s] close", current->comm);
1288 /* Needed for gspca_stream_off, always lock before queue_lock! */
1289 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1290 return -ERESTARTSYS;
1292 if (mutex_lock_interruptible(&gspca_dev->queue_lock)) {
1293 mutex_unlock(&gspca_dev->usb_lock);
1294 return -ERESTARTSYS;
1297 /* if the file did the capture, free the streaming resources */
1298 if (gspca_dev->capt_file == file) {
1299 if (gspca_dev->streaming)
1300 gspca_stream_off(gspca_dev);
1301 frame_free(gspca_dev);
1303 module_put(gspca_dev->module);
1304 mutex_unlock(&gspca_dev->queue_lock);
1305 mutex_unlock(&gspca_dev->usb_lock);
1307 PDEBUG(D_STREAM, "close done");
1309 return v4l2_fh_release(file);
1312 static int vidioc_querycap(struct file *file, void *priv,
1313 struct v4l2_capability *cap)
1315 struct gspca_dev *gspca_dev = video_drvdata(file);
1317 strlcpy((char *) cap->driver, gspca_dev->sd_desc->name,
1318 sizeof cap->driver);
1319 if (gspca_dev->dev->product != NULL) {
1320 strlcpy((char *) cap->card, gspca_dev->dev->product,
1321 sizeof cap->card);
1322 } else {
1323 snprintf((char *) cap->card, sizeof cap->card,
1324 "USB Camera (%04x:%04x)",
1325 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1326 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1328 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1329 sizeof(cap->bus_info));
1330 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
1331 | V4L2_CAP_STREAMING
1332 | V4L2_CAP_READWRITE;
1333 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1334 return 0;
1337 static int vidioc_enum_input(struct file *file, void *priv,
1338 struct v4l2_input *input)
1340 struct gspca_dev *gspca_dev = video_drvdata(file);
1342 if (input->index != 0)
1343 return -EINVAL;
1344 input->type = V4L2_INPUT_TYPE_CAMERA;
1345 input->status = gspca_dev->cam.input_flags;
1346 strlcpy(input->name, gspca_dev->sd_desc->name,
1347 sizeof input->name);
1348 return 0;
1351 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1353 *i = 0;
1354 return 0;
1357 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1359 if (i > 0)
1360 return -EINVAL;
1361 return (0);
1364 static int vidioc_reqbufs(struct file *file, void *priv,
1365 struct v4l2_requestbuffers *rb)
1367 struct gspca_dev *gspca_dev = video_drvdata(file);
1368 int i, ret = 0, streaming;
1370 i = rb->memory; /* (avoid compilation warning) */
1371 switch (i) {
1372 case GSPCA_MEMORY_READ: /* (internal call) */
1373 case V4L2_MEMORY_MMAP:
1374 case V4L2_MEMORY_USERPTR:
1375 break;
1376 default:
1377 return -EINVAL;
1379 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1380 return -ERESTARTSYS;
1382 if (gspca_dev->memory != GSPCA_MEMORY_NO
1383 && gspca_dev->memory != GSPCA_MEMORY_READ
1384 && gspca_dev->memory != rb->memory) {
1385 ret = -EBUSY;
1386 goto out;
1389 /* only one file may do the capture */
1390 if (gspca_dev->capt_file != NULL
1391 && gspca_dev->capt_file != file) {
1392 ret = -EBUSY;
1393 goto out;
1396 /* if allocated, the buffers must not be mapped */
1397 for (i = 0; i < gspca_dev->nframes; i++) {
1398 if (gspca_dev->frame[i].vma_use_count) {
1399 ret = -EBUSY;
1400 goto out;
1404 /* stop streaming */
1405 streaming = gspca_dev->streaming;
1406 if (streaming) {
1407 gspca_stream_off(gspca_dev);
1409 /* Don't restart the stream when switching from read
1410 * to mmap mode */
1411 if (gspca_dev->memory == GSPCA_MEMORY_READ)
1412 streaming = 0;
1415 /* free the previous allocated buffers, if any */
1416 if (gspca_dev->nframes != 0)
1417 frame_free(gspca_dev);
1418 if (rb->count == 0) /* unrequest */
1419 goto out;
1420 ret = frame_alloc(gspca_dev, file, rb->memory, rb->count);
1421 if (ret == 0) {
1422 rb->count = gspca_dev->nframes;
1423 if (streaming)
1424 ret = gspca_init_transfer(gspca_dev);
1426 out:
1427 mutex_unlock(&gspca_dev->queue_lock);
1428 PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count);
1429 return ret;
1432 static int vidioc_querybuf(struct file *file, void *priv,
1433 struct v4l2_buffer *v4l2_buf)
1435 struct gspca_dev *gspca_dev = video_drvdata(file);
1436 struct gspca_frame *frame;
1438 if (v4l2_buf->index >= gspca_dev->nframes)
1439 return -EINVAL;
1441 frame = &gspca_dev->frame[v4l2_buf->index];
1442 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1443 return 0;
1446 static int vidioc_streamon(struct file *file, void *priv,
1447 enum v4l2_buf_type buf_type)
1449 struct gspca_dev *gspca_dev = video_drvdata(file);
1450 int ret;
1452 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1453 return -EINVAL;
1454 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1455 return -ERESTARTSYS;
1457 /* check the capture file */
1458 if (gspca_dev->capt_file != file) {
1459 ret = -EBUSY;
1460 goto out;
1463 if (gspca_dev->nframes == 0
1464 || !(gspca_dev->frame[0].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)) {
1465 ret = -EINVAL;
1466 goto out;
1468 if (!gspca_dev->streaming) {
1469 ret = gspca_init_transfer(gspca_dev);
1470 if (ret < 0)
1471 goto out;
1473 PDEBUG_MODE(gspca_dev, D_STREAM, "stream on OK",
1474 gspca_dev->pixfmt.pixelformat,
1475 gspca_dev->pixfmt.width, gspca_dev->pixfmt.height);
1476 ret = 0;
1477 out:
1478 mutex_unlock(&gspca_dev->queue_lock);
1479 return ret;
1482 static int vidioc_streamoff(struct file *file, void *priv,
1483 enum v4l2_buf_type buf_type)
1485 struct gspca_dev *gspca_dev = video_drvdata(file);
1486 int i, ret;
1488 if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1489 return -EINVAL;
1491 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1492 return -ERESTARTSYS;
1494 if (!gspca_dev->streaming) {
1495 ret = 0;
1496 goto out;
1499 /* check the capture file */
1500 if (gspca_dev->capt_file != file) {
1501 ret = -EBUSY;
1502 goto out;
1505 /* stop streaming */
1506 gspca_stream_off(gspca_dev);
1507 /* In case another thread is waiting in dqbuf */
1508 wake_up_interruptible(&gspca_dev->wq);
1510 /* empty the transfer queues */
1511 for (i = 0; i < gspca_dev->nframes; i++)
1512 gspca_dev->frame[i].v4l2_buf.flags &= ~BUF_ALL_FLAGS;
1513 atomic_set(&gspca_dev->fr_q, 0);
1514 atomic_set(&gspca_dev->fr_i, 0);
1515 gspca_dev->fr_o = 0;
1516 ret = 0;
1517 out:
1518 mutex_unlock(&gspca_dev->queue_lock);
1519 return ret;
1522 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1523 struct v4l2_jpegcompression *jpegcomp)
1525 struct gspca_dev *gspca_dev = video_drvdata(file);
1527 gspca_dev->usb_err = 0;
1528 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1531 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1532 const struct v4l2_jpegcompression *jpegcomp)
1534 struct gspca_dev *gspca_dev = video_drvdata(file);
1536 gspca_dev->usb_err = 0;
1537 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1540 static int vidioc_g_parm(struct file *filp, void *priv,
1541 struct v4l2_streamparm *parm)
1543 struct gspca_dev *gspca_dev = video_drvdata(filp);
1545 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1547 if (gspca_dev->sd_desc->get_streamparm) {
1548 gspca_dev->usb_err = 0;
1549 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1550 return gspca_dev->usb_err;
1552 return 0;
1555 static int vidioc_s_parm(struct file *filp, void *priv,
1556 struct v4l2_streamparm *parm)
1558 struct gspca_dev *gspca_dev = video_drvdata(filp);
1559 int n;
1561 n = parm->parm.capture.readbuffers;
1562 if (n == 0 || n >= GSPCA_MAX_FRAMES)
1563 parm->parm.capture.readbuffers = gspca_dev->nbufread;
1564 else
1565 gspca_dev->nbufread = n;
1567 if (gspca_dev->sd_desc->set_streamparm) {
1568 gspca_dev->usb_err = 0;
1569 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1570 return gspca_dev->usb_err;
1573 return 0;
1576 static int dev_mmap(struct file *file, struct vm_area_struct *vma)
1578 struct gspca_dev *gspca_dev = video_drvdata(file);
1579 struct gspca_frame *frame;
1580 struct page *page;
1581 unsigned long addr, start, size;
1582 int i, ret;
1584 start = vma->vm_start;
1585 size = vma->vm_end - vma->vm_start;
1586 PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size);
1588 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1589 return -ERESTARTSYS;
1590 if (gspca_dev->capt_file != file) {
1591 ret = -EINVAL;
1592 goto out;
1595 frame = NULL;
1596 for (i = 0; i < gspca_dev->nframes; ++i) {
1597 if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) {
1598 PDEBUG(D_STREAM, "mmap bad memory type");
1599 break;
1601 if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT)
1602 == vma->vm_pgoff) {
1603 frame = &gspca_dev->frame[i];
1604 break;
1607 if (frame == NULL) {
1608 PDEBUG(D_STREAM, "mmap no frame buffer found");
1609 ret = -EINVAL;
1610 goto out;
1612 if (size != frame->v4l2_buf.length) {
1613 PDEBUG(D_STREAM, "mmap bad size");
1614 ret = -EINVAL;
1615 goto out;
1619 * - VM_IO marks the area as being a mmaped region for I/O to a
1620 * device. It also prevents the region from being core dumped.
1622 vma->vm_flags |= VM_IO;
1624 addr = (unsigned long) frame->data;
1625 while (size > 0) {
1626 page = vmalloc_to_page((void *) addr);
1627 ret = vm_insert_page(vma, start, page);
1628 if (ret < 0)
1629 goto out;
1630 start += PAGE_SIZE;
1631 addr += PAGE_SIZE;
1632 size -= PAGE_SIZE;
1635 vma->vm_ops = &gspca_vm_ops;
1636 vma->vm_private_data = frame;
1637 gspca_vm_open(vma);
1638 ret = 0;
1639 out:
1640 mutex_unlock(&gspca_dev->queue_lock);
1641 return ret;
1644 static int frame_ready_nolock(struct gspca_dev *gspca_dev, struct file *file,
1645 enum v4l2_memory memory)
1647 if (!gspca_dev->present)
1648 return -ENODEV;
1649 if (gspca_dev->capt_file != file || gspca_dev->memory != memory ||
1650 !gspca_dev->streaming)
1651 return -EINVAL;
1653 /* check if a frame is ready */
1654 return gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i);
1657 static int frame_ready(struct gspca_dev *gspca_dev, struct file *file,
1658 enum v4l2_memory memory)
1660 int ret;
1662 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1663 return -ERESTARTSYS;
1664 ret = frame_ready_nolock(gspca_dev, file, memory);
1665 mutex_unlock(&gspca_dev->queue_lock);
1666 return ret;
1670 * dequeue a video buffer
1672 * If nonblock_ing is false, block until a buffer is available.
1674 static int vidioc_dqbuf(struct file *file, void *priv,
1675 struct v4l2_buffer *v4l2_buf)
1677 struct gspca_dev *gspca_dev = video_drvdata(file);
1678 struct gspca_frame *frame;
1679 int i, j, ret;
1681 PDEBUG(D_FRAM, "dqbuf");
1683 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1684 return -ERESTARTSYS;
1686 for (;;) {
1687 ret = frame_ready_nolock(gspca_dev, file, v4l2_buf->memory);
1688 if (ret < 0)
1689 goto out;
1690 if (ret > 0)
1691 break;
1693 mutex_unlock(&gspca_dev->queue_lock);
1695 if (file->f_flags & O_NONBLOCK)
1696 return -EAGAIN;
1698 /* wait till a frame is ready */
1699 ret = wait_event_interruptible_timeout(gspca_dev->wq,
1700 frame_ready(gspca_dev, file, v4l2_buf->memory),
1701 msecs_to_jiffies(3000));
1702 if (ret < 0)
1703 return ret;
1704 if (ret == 0)
1705 return -EIO;
1707 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1708 return -ERESTARTSYS;
1711 i = gspca_dev->fr_o;
1712 j = gspca_dev->fr_queue[i];
1713 frame = &gspca_dev->frame[j];
1715 gspca_dev->fr_o = (i + 1) % GSPCA_MAX_FRAMES;
1717 frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
1718 memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf);
1719 PDEBUG(D_FRAM, "dqbuf %d", j);
1720 ret = 0;
1722 if (gspca_dev->memory == V4L2_MEMORY_USERPTR) {
1723 if (copy_to_user((__u8 __user *) frame->v4l2_buf.m.userptr,
1724 frame->data,
1725 frame->v4l2_buf.bytesused)) {
1726 PERR("dqbuf cp to user failed");
1727 ret = -EFAULT;
1730 out:
1731 mutex_unlock(&gspca_dev->queue_lock);
1733 if (ret == 0 && gspca_dev->sd_desc->dq_callback) {
1734 mutex_lock(&gspca_dev->usb_lock);
1735 gspca_dev->usb_err = 0;
1736 if (gspca_dev->present)
1737 gspca_dev->sd_desc->dq_callback(gspca_dev);
1738 mutex_unlock(&gspca_dev->usb_lock);
1741 return ret;
1745 * queue a video buffer
1747 * Attempting to queue a buffer that has already been
1748 * queued will return -EINVAL.
1750 static int vidioc_qbuf(struct file *file, void *priv,
1751 struct v4l2_buffer *v4l2_buf)
1753 struct gspca_dev *gspca_dev = video_drvdata(file);
1754 struct gspca_frame *frame;
1755 int i, index, ret;
1757 PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index);
1759 if (mutex_lock_interruptible(&gspca_dev->queue_lock))
1760 return -ERESTARTSYS;
1762 index = v4l2_buf->index;
1763 if ((unsigned) index >= gspca_dev->nframes) {
1764 PDEBUG(D_FRAM,
1765 "qbuf idx %d >= %d", index, gspca_dev->nframes);
1766 ret = -EINVAL;
1767 goto out;
1769 if (v4l2_buf->memory != gspca_dev->memory) {
1770 PDEBUG(D_FRAM, "qbuf bad memory type");
1771 ret = -EINVAL;
1772 goto out;
1775 frame = &gspca_dev->frame[index];
1776 if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
1777 PDEBUG(D_FRAM, "qbuf bad state");
1778 ret = -EINVAL;
1779 goto out;
1782 frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
1784 if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
1785 frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
1786 frame->v4l2_buf.length = v4l2_buf->length;
1789 /* put the buffer in the 'queued' queue */
1790 i = atomic_read(&gspca_dev->fr_q);
1791 gspca_dev->fr_queue[i] = index;
1792 atomic_set(&gspca_dev->fr_q, (i + 1) % GSPCA_MAX_FRAMES);
1794 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1795 v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1796 ret = 0;
1797 out:
1798 mutex_unlock(&gspca_dev->queue_lock);
1799 return ret;
1803 * allocate the resources for read()
1805 static int read_alloc(struct gspca_dev *gspca_dev,
1806 struct file *file)
1808 struct v4l2_buffer v4l2_buf;
1809 int i, ret;
1811 PDEBUG(D_STREAM, "read alloc");
1813 if (mutex_lock_interruptible(&gspca_dev->usb_lock))
1814 return -ERESTARTSYS;
1816 if (gspca_dev->nframes == 0) {
1817 struct v4l2_requestbuffers rb;
1819 memset(&rb, 0, sizeof rb);
1820 rb.count = gspca_dev->nbufread;
1821 rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1822 rb.memory = GSPCA_MEMORY_READ;
1823 ret = vidioc_reqbufs(file, gspca_dev, &rb);
1824 if (ret != 0) {
1825 PDEBUG(D_STREAM, "read reqbuf err %d", ret);
1826 goto out;
1828 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1829 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1830 v4l2_buf.memory = GSPCA_MEMORY_READ;
1831 for (i = 0; i < gspca_dev->nbufread; i++) {
1832 v4l2_buf.index = i;
1833 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1834 if (ret != 0) {
1835 PDEBUG(D_STREAM, "read qbuf err: %d", ret);
1836 goto out;
1841 /* start streaming */
1842 ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1843 if (ret != 0)
1844 PDEBUG(D_STREAM, "read streamon err %d", ret);
1845 out:
1846 mutex_unlock(&gspca_dev->usb_lock);
1847 return ret;
1850 static unsigned int dev_poll(struct file *file, poll_table *wait)
1852 struct gspca_dev *gspca_dev = video_drvdata(file);
1853 unsigned long req_events = poll_requested_events(wait);
1854 int ret = 0;
1856 PDEBUG(D_FRAM, "poll");
1858 if (req_events & POLLPRI)
1859 ret |= v4l2_ctrl_poll(file, wait);
1861 if (req_events & (POLLIN | POLLRDNORM)) {
1862 /* if reqbufs is not done, the user would use read() */
1863 if (gspca_dev->memory == GSPCA_MEMORY_NO) {
1864 if (read_alloc(gspca_dev, file) != 0) {
1865 ret |= POLLERR;
1866 goto out;
1870 poll_wait(file, &gspca_dev->wq, wait);
1872 /* check if an image has been received */
1873 if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) {
1874 ret |= POLLERR;
1875 goto out;
1877 if (gspca_dev->fr_o != atomic_read(&gspca_dev->fr_i))
1878 ret |= POLLIN | POLLRDNORM;
1879 mutex_unlock(&gspca_dev->queue_lock);
1882 out:
1883 if (!gspca_dev->present)
1884 ret |= POLLHUP;
1886 return ret;
1889 static ssize_t dev_read(struct file *file, char __user *data,
1890 size_t count, loff_t *ppos)
1892 struct gspca_dev *gspca_dev = video_drvdata(file);
1893 struct gspca_frame *frame;
1894 struct v4l2_buffer v4l2_buf;
1895 struct timeval timestamp;
1896 int n, ret, ret2;
1898 PDEBUG(D_FRAM, "read (%zd)", count);
1899 if (gspca_dev->memory == GSPCA_MEMORY_NO) { /* first time ? */
1900 ret = read_alloc(gspca_dev, file);
1901 if (ret != 0)
1902 return ret;
1905 /* get a frame */
1906 timestamp = ktime_to_timeval(ktime_get());
1907 timestamp.tv_sec--;
1908 n = 2;
1909 for (;;) {
1910 memset(&v4l2_buf, 0, sizeof v4l2_buf);
1911 v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1912 v4l2_buf.memory = GSPCA_MEMORY_READ;
1913 ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf);
1914 if (ret != 0) {
1915 PDEBUG(D_STREAM, "read dqbuf err %d", ret);
1916 return ret;
1919 /* if the process slept for more than 1 second,
1920 * get a newer frame */
1921 frame = &gspca_dev->frame[v4l2_buf.index];
1922 if (--n < 0)
1923 break; /* avoid infinite loop */
1924 if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec)
1925 break;
1926 ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1927 if (ret != 0) {
1928 PDEBUG(D_STREAM, "read qbuf err %d", ret);
1929 return ret;
1933 /* copy the frame */
1934 if (count > frame->v4l2_buf.bytesused)
1935 count = frame->v4l2_buf.bytesused;
1936 ret = copy_to_user(data, frame->data, count);
1937 if (ret != 0) {
1938 PERR("read cp to user lack %d / %zd", ret, count);
1939 ret = -EFAULT;
1940 goto out;
1942 ret = count;
1943 out:
1944 /* in each case, requeue the buffer */
1945 ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf);
1946 if (ret2 != 0)
1947 return ret2;
1948 return ret;
1951 static struct v4l2_file_operations dev_fops = {
1952 .owner = THIS_MODULE,
1953 .open = dev_open,
1954 .release = dev_close,
1955 .read = dev_read,
1956 .mmap = dev_mmap,
1957 .unlocked_ioctl = video_ioctl2,
1958 .poll = dev_poll,
1961 static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1962 .vidioc_querycap = vidioc_querycap,
1963 .vidioc_dqbuf = vidioc_dqbuf,
1964 .vidioc_qbuf = vidioc_qbuf,
1965 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1966 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1967 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1968 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1969 .vidioc_streamon = vidioc_streamon,
1970 .vidioc_enum_input = vidioc_enum_input,
1971 .vidioc_g_input = vidioc_g_input,
1972 .vidioc_s_input = vidioc_s_input,
1973 .vidioc_reqbufs = vidioc_reqbufs,
1974 .vidioc_querybuf = vidioc_querybuf,
1975 .vidioc_streamoff = vidioc_streamoff,
1976 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1977 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1978 .vidioc_g_parm = vidioc_g_parm,
1979 .vidioc_s_parm = vidioc_s_parm,
1980 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1981 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1982 #ifdef CONFIG_VIDEO_ADV_DEBUG
1983 .vidioc_g_chip_info = vidioc_g_chip_info,
1984 .vidioc_g_register = vidioc_g_register,
1985 .vidioc_s_register = vidioc_s_register,
1986 #endif
1987 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1988 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1991 static const struct video_device gspca_template = {
1992 .name = "gspca main driver",
1993 .fops = &dev_fops,
1994 .ioctl_ops = &dev_ioctl_ops,
1995 .release = video_device_release_empty, /* We use v4l2_dev.release */
1999 * probe and create a new gspca device
2001 * This function must be called by the sub-driver when it is
2002 * called for probing a new device.
2004 int gspca_dev_probe2(struct usb_interface *intf,
2005 const struct usb_device_id *id,
2006 const struct sd_desc *sd_desc,
2007 int dev_size,
2008 struct module *module)
2010 struct gspca_dev *gspca_dev;
2011 struct usb_device *dev = interface_to_usbdev(intf);
2012 int ret;
2014 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
2015 sd_desc->name, id->idVendor, id->idProduct);
2017 /* create the device */
2018 if (dev_size < sizeof *gspca_dev)
2019 dev_size = sizeof *gspca_dev;
2020 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
2021 if (!gspca_dev) {
2022 pr_err("couldn't kzalloc gspca struct\n");
2023 return -ENOMEM;
2025 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
2026 if (!gspca_dev->usb_buf) {
2027 pr_err("out of memory\n");
2028 ret = -ENOMEM;
2029 goto out;
2031 gspca_dev->dev = dev;
2032 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
2034 /* check if any audio device */
2035 if (dev->actconfig->desc.bNumInterfaces != 1) {
2036 int i;
2037 struct usb_interface *intf2;
2039 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
2040 intf2 = dev->actconfig->interface[i];
2041 if (intf2 != NULL
2042 && intf2->altsetting != NULL
2043 && intf2->altsetting->desc.bInterfaceClass ==
2044 USB_CLASS_AUDIO) {
2045 gspca_dev->audio = 1;
2046 break;
2051 gspca_dev->v4l2_dev.release = gspca_release;
2052 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
2053 if (ret)
2054 goto out;
2055 gspca_dev->sd_desc = sd_desc;
2056 gspca_dev->nbufread = 2;
2057 gspca_dev->empty_packet = -1; /* don't check the empty packets */
2058 gspca_dev->vdev = gspca_template;
2059 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
2060 video_set_drvdata(&gspca_dev->vdev, gspca_dev);
2061 set_bit(V4L2_FL_USE_FH_PRIO, &gspca_dev->vdev.flags);
2062 gspca_dev->module = module;
2063 gspca_dev->present = 1;
2065 mutex_init(&gspca_dev->usb_lock);
2066 gspca_dev->vdev.lock = &gspca_dev->usb_lock;
2067 mutex_init(&gspca_dev->queue_lock);
2068 init_waitqueue_head(&gspca_dev->wq);
2070 /* configure the subdriver and initialize the USB device */
2071 ret = sd_desc->config(gspca_dev, id);
2072 if (ret < 0)
2073 goto out;
2074 ret = sd_desc->init(gspca_dev);
2075 if (ret < 0)
2076 goto out;
2077 if (sd_desc->init_controls)
2078 ret = sd_desc->init_controls(gspca_dev);
2079 if (ret < 0)
2080 goto out;
2081 gspca_set_default_mode(gspca_dev);
2083 ret = gspca_input_connect(gspca_dev);
2084 if (ret)
2085 goto out;
2088 * Don't take usb_lock for these ioctls. This improves latency if
2089 * usb_lock is taken for a long time, e.g. when changing a control
2090 * value, and a new frame is ready to be dequeued.
2092 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_DQBUF);
2093 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QBUF);
2094 v4l2_disable_ioctl_locking(&gspca_dev->vdev, VIDIOC_QUERYBUF);
2095 #ifdef CONFIG_VIDEO_ADV_DEBUG
2096 if (!gspca_dev->sd_desc->get_register)
2097 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
2098 if (!gspca_dev->sd_desc->set_register)
2099 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
2100 #endif
2101 if (!gspca_dev->sd_desc->get_jcomp)
2102 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
2103 if (!gspca_dev->sd_desc->set_jcomp)
2104 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
2106 /* init video stuff */
2107 ret = video_register_device(&gspca_dev->vdev,
2108 VFL_TYPE_GRABBER,
2109 -1);
2110 if (ret < 0) {
2111 pr_err("video_register_device err %d\n", ret);
2112 goto out;
2115 usb_set_intfdata(intf, gspca_dev);
2116 PDEBUG(D_PROBE, "%s created", video_device_node_name(&gspca_dev->vdev));
2118 gspca_input_create_urb(gspca_dev);
2120 return 0;
2121 out:
2122 #if IS_ENABLED(CONFIG_INPUT)
2123 if (gspca_dev->input_dev)
2124 input_unregister_device(gspca_dev->input_dev);
2125 #endif
2126 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
2127 kfree(gspca_dev->usb_buf);
2128 kfree(gspca_dev);
2129 return ret;
2131 EXPORT_SYMBOL(gspca_dev_probe2);
2133 /* same function as the previous one, but check the interface */
2134 int gspca_dev_probe(struct usb_interface *intf,
2135 const struct usb_device_id *id,
2136 const struct sd_desc *sd_desc,
2137 int dev_size,
2138 struct module *module)
2140 struct usb_device *dev = interface_to_usbdev(intf);
2142 /* we don't handle multi-config cameras */
2143 if (dev->descriptor.bNumConfigurations != 1) {
2144 pr_err("%04x:%04x too many config\n",
2145 id->idVendor, id->idProduct);
2146 return -ENODEV;
2149 /* the USB video interface must be the first one */
2150 if (dev->actconfig->desc.bNumInterfaces != 1
2151 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
2152 return -ENODEV;
2154 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
2156 EXPORT_SYMBOL(gspca_dev_probe);
2159 * USB disconnection
2161 * This function must be called by the sub-driver
2162 * when the device disconnects, after the specific resources are freed.
2164 void gspca_disconnect(struct usb_interface *intf)
2166 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2167 #if IS_ENABLED(CONFIG_INPUT)
2168 struct input_dev *input_dev;
2169 #endif
2171 PDEBUG(D_PROBE, "%s disconnect",
2172 video_device_node_name(&gspca_dev->vdev));
2174 mutex_lock(&gspca_dev->usb_lock);
2176 gspca_dev->present = 0;
2177 destroy_urbs(gspca_dev);
2179 #if IS_ENABLED(CONFIG_INPUT)
2180 gspca_input_destroy_urb(gspca_dev);
2181 input_dev = gspca_dev->input_dev;
2182 if (input_dev) {
2183 gspca_dev->input_dev = NULL;
2184 input_unregister_device(input_dev);
2186 #endif
2187 /* Free subdriver's streaming resources / stop sd workqueue(s) */
2188 if (gspca_dev->sd_desc->stop0 && gspca_dev->streaming)
2189 gspca_dev->sd_desc->stop0(gspca_dev);
2190 gspca_dev->streaming = 0;
2191 gspca_dev->dev = NULL;
2192 wake_up_interruptible(&gspca_dev->wq);
2194 v4l2_device_disconnect(&gspca_dev->v4l2_dev);
2195 video_unregister_device(&gspca_dev->vdev);
2197 mutex_unlock(&gspca_dev->usb_lock);
2199 /* (this will call gspca_release() immediately or on last close) */
2200 v4l2_device_put(&gspca_dev->v4l2_dev);
2202 EXPORT_SYMBOL(gspca_disconnect);
2204 #ifdef CONFIG_PM
2205 int gspca_suspend(struct usb_interface *intf, pm_message_t message)
2207 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2209 gspca_input_destroy_urb(gspca_dev);
2211 if (!gspca_dev->streaming)
2212 return 0;
2214 mutex_lock(&gspca_dev->usb_lock);
2215 gspca_dev->frozen = 1; /* avoid urb error messages */
2216 gspca_dev->usb_err = 0;
2217 if (gspca_dev->sd_desc->stopN)
2218 gspca_dev->sd_desc->stopN(gspca_dev);
2219 destroy_urbs(gspca_dev);
2220 gspca_set_alt0(gspca_dev);
2221 if (gspca_dev->sd_desc->stop0)
2222 gspca_dev->sd_desc->stop0(gspca_dev);
2223 mutex_unlock(&gspca_dev->usb_lock);
2225 return 0;
2227 EXPORT_SYMBOL(gspca_suspend);
2229 int gspca_resume(struct usb_interface *intf)
2231 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
2232 int streaming, ret = 0;
2234 mutex_lock(&gspca_dev->usb_lock);
2235 gspca_dev->frozen = 0;
2236 gspca_dev->usb_err = 0;
2237 gspca_dev->sd_desc->init(gspca_dev);
2239 * Most subdrivers send all ctrl values on sd_start and thus
2240 * only write to the device registers on s_ctrl when streaming ->
2241 * Clear streaming to avoid setting all ctrls twice.
2243 streaming = gspca_dev->streaming;
2244 gspca_dev->streaming = 0;
2245 if (streaming)
2246 ret = gspca_init_transfer(gspca_dev);
2247 else
2248 gspca_input_create_urb(gspca_dev);
2249 mutex_unlock(&gspca_dev->usb_lock);
2251 return ret;
2253 EXPORT_SYMBOL(gspca_resume);
2254 #endif
2256 /* -- module insert / remove -- */
2257 static int __init gspca_init(void)
2259 pr_info("v" GSPCA_VERSION " registered\n");
2260 return 0;
2262 static void __exit gspca_exit(void)
2266 module_init(gspca_init);
2267 module_exit(gspca_exit);
2269 module_param_named(debug, gspca_debug, int, 0644);
2270 MODULE_PARM_DESC(debug,
2271 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");