1
2
3
4
5
6
7
8
9
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#define GSPCA_VERSION "2.14.0"
14
15#include <linux/init.h>
16#include <linux/fs.h>
17#include <linux/vmalloc.h>
18#include <linux/sched.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/string.h>
22#include <linux/pagemap.h>
23#include <linux/io.h>
24#include <asm/page.h>
25#include <linux/uaccess.h>
26#include <linux/ktime.h>
27#include <media/v4l2-ioctl.h>
28#include <media/v4l2-ctrls.h>
29#include <media/v4l2-fh.h>
30#include <media/v4l2-event.h>
31
32#include "gspca.h"
33
34#if IS_ENABLED(CONFIG_INPUT)
35#include <linux/input.h>
36#include <linux/usb/input.h>
37#endif
38
39
40#define DEF_NURBS 3
41#if DEF_NURBS > MAX_NURBS
42#error "DEF_NURBS too big"
43#endif
44
45MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
46MODULE_DESCRIPTION("GSPCA USB Camera Driver");
47MODULE_LICENSE("GPL");
48MODULE_VERSION(GSPCA_VERSION);
49
50int gspca_debug;
51EXPORT_SYMBOL(gspca_debug);
52
53static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
54 __u32 pixfmt, int w, int h)
55{
56 if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
57 gspca_dbg(gspca_dev, debug, "%s %c%c%c%c %dx%d\n",
58 txt,
59 pixfmt & 0xff,
60 (pixfmt >> 8) & 0xff,
61 (pixfmt >> 16) & 0xff,
62 pixfmt >> 24,
63 w, h);
64 } else {
65 gspca_dbg(gspca_dev, debug, "%s 0x%08x %dx%d\n",
66 txt,
67 pixfmt,
68 w, h);
69 }
70}
71
72
73#define GSPCA_MEMORY_NO 0
74#define GSPCA_MEMORY_READ 7
75
76
77
78
79#if IS_ENABLED(CONFIG_INPUT)
80static void int_irq(struct urb *urb)
81{
82 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
83 int ret;
84
85 ret = urb->status;
86 switch (ret) {
87 case 0:
88 if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
89 urb->transfer_buffer, urb->actual_length) < 0) {
90 gspca_err(gspca_dev, "Unknown packet received\n");
91 }
92 break;
93
94 case -ENOENT:
95 case -ECONNRESET:
96 case -ENODEV:
97 case -ESHUTDOWN:
98
99
100
101 break;
102
103 default:
104 gspca_err(gspca_dev, "URB error %i, resubmitting\n",
105 urb->status);
106 urb->status = 0;
107 ret = 0;
108 }
109
110 if (ret == 0) {
111 ret = usb_submit_urb(urb, GFP_ATOMIC);
112 if (ret < 0)
113 pr_err("Resubmit URB failed with error %i\n", ret);
114 }
115}
116
117static int gspca_input_connect(struct gspca_dev *dev)
118{
119 struct input_dev *input_dev;
120 int err = 0;
121
122 dev->input_dev = NULL;
123 if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
124 input_dev = input_allocate_device();
125 if (!input_dev)
126 return -ENOMEM;
127
128 usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
129 strlcat(dev->phys, "/input0", sizeof(dev->phys));
130
131 input_dev->name = dev->sd_desc->name;
132 input_dev->phys = dev->phys;
133
134 usb_to_input_id(dev->dev, &input_dev->id);
135
136 input_dev->evbit[0] = BIT_MASK(EV_KEY);
137 input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
138 input_dev->dev.parent = &dev->dev->dev;
139
140 err = input_register_device(input_dev);
141 if (err) {
142 pr_err("Input device registration failed with error %i\n",
143 err);
144 input_dev->dev.parent = NULL;
145 input_free_device(input_dev);
146 } else {
147 dev->input_dev = input_dev;
148 }
149 }
150
151 return err;
152}
153
154static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
155 struct usb_endpoint_descriptor *ep)
156{
157 unsigned int buffer_len;
158 int interval;
159 struct urb *urb;
160 struct usb_device *dev;
161 void *buffer = NULL;
162 int ret = -EINVAL;
163
164 buffer_len = le16_to_cpu(ep->wMaxPacketSize);
165 interval = ep->bInterval;
166 gspca_dbg(gspca_dev, D_CONF, "found int in endpoint: 0x%x, buffer_len=%u, interval=%u\n",
167 ep->bEndpointAddress, buffer_len, interval);
168
169 dev = gspca_dev->dev;
170
171 urb = usb_alloc_urb(0, GFP_KERNEL);
172 if (!urb) {
173 ret = -ENOMEM;
174 goto error;
175 }
176
177 buffer = usb_alloc_coherent(dev, buffer_len,
178 GFP_KERNEL, &urb->transfer_dma);
179 if (!buffer) {
180 ret = -ENOMEM;
181 goto error_buffer;
182 }
183 usb_fill_int_urb(urb, dev,
184 usb_rcvintpipe(dev, ep->bEndpointAddress),
185 buffer, buffer_len,
186 int_irq, (void *)gspca_dev, interval);
187 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
188 ret = usb_submit_urb(urb, GFP_KERNEL);
189 if (ret < 0) {
190 gspca_err(gspca_dev, "submit int URB failed with error %i\n",
191 ret);
192 goto error_submit;
193 }
194 gspca_dev->int_urb = urb;
195 return ret;
196
197error_submit:
198 usb_free_coherent(dev,
199 urb->transfer_buffer_length,
200 urb->transfer_buffer,
201 urb->transfer_dma);
202error_buffer:
203 usb_free_urb(urb);
204error:
205 return ret;
206}
207
208static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
209{
210 struct usb_interface *intf;
211 struct usb_host_interface *intf_desc;
212 struct usb_endpoint_descriptor *ep;
213 int i;
214
215 if (gspca_dev->sd_desc->int_pkt_scan) {
216 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
217 intf_desc = intf->cur_altsetting;
218 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
219 ep = &intf_desc->endpoint[i].desc;
220 if (usb_endpoint_dir_in(ep) &&
221 usb_endpoint_xfer_int(ep)) {
222
223 alloc_and_submit_int_urb(gspca_dev, ep);
224 break;
225 }
226 }
227 }
228}
229
230static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
231{
232 struct urb *urb;
233
234 urb = gspca_dev->int_urb;
235 if (urb) {
236 gspca_dev->int_urb = NULL;
237 usb_kill_urb(urb);
238 usb_free_coherent(gspca_dev->dev,
239 urb->transfer_buffer_length,
240 urb->transfer_buffer,
241 urb->transfer_dma);
242 usb_free_urb(urb);
243 }
244}
245#else
246static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
247{
248}
249
250static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
251{
252}
253
254static inline int gspca_input_connect(struct gspca_dev *dev)
255{
256 return 0;
257}
258#endif
259
260
261
262
263static void fill_frame(struct gspca_dev *gspca_dev,
264 struct urb *urb)
265{
266 u8 *data;
267 int i, len, st;
268 cam_pkt_op pkt_scan;
269
270 if (urb->status != 0) {
271 if (urb->status == -ESHUTDOWN)
272 return;
273#ifdef CONFIG_PM
274 if (gspca_dev->frozen)
275 return;
276#endif
277 gspca_err(gspca_dev, "urb status: %d\n", urb->status);
278 urb->status = 0;
279 goto resubmit;
280 }
281 pkt_scan = gspca_dev->sd_desc->pkt_scan;
282 for (i = 0; i < urb->number_of_packets; i++) {
283 len = urb->iso_frame_desc[i].actual_length;
284
285
286 st = urb->iso_frame_desc[i].status;
287 if (st) {
288 gspca_dbg(gspca_dev, D_PACK, "ISOC data error: [%d] len=%d, status=%d\n",
289 i, len, st);
290 gspca_dev->last_packet_type = DISCARD_PACKET;
291 continue;
292 }
293 if (len == 0) {
294 if (gspca_dev->empty_packet == 0)
295 gspca_dev->empty_packet = 1;
296 continue;
297 }
298
299
300 gspca_dbg(gspca_dev, D_PACK, "packet [%d] o:%d l:%d\n",
301 i, urb->iso_frame_desc[i].offset, len);
302 data = (u8 *) urb->transfer_buffer
303 + urb->iso_frame_desc[i].offset;
304 pkt_scan(gspca_dev, data, len);
305 }
306
307resubmit:
308 if (!gspca_dev->streaming)
309 return;
310
311 st = usb_submit_urb(urb, GFP_ATOMIC);
312 if (st < 0)
313 pr_err("usb_submit_urb() ret %d\n", st);
314}
315
316
317
318
319
320
321static void isoc_irq(struct urb *urb)
322{
323 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
324
325 gspca_dbg(gspca_dev, D_PACK, "isoc irq\n");
326 if (!gspca_dev->streaming)
327 return;
328 fill_frame(gspca_dev, urb);
329}
330
331
332
333
334static void bulk_irq(struct urb *urb)
335{
336 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
337 int st;
338
339 gspca_dbg(gspca_dev, D_PACK, "bulk irq\n");
340 if (!gspca_dev->streaming)
341 return;
342 switch (urb->status) {
343 case 0:
344 break;
345 case -ESHUTDOWN:
346 return;
347 default:
348#ifdef CONFIG_PM
349 if (gspca_dev->frozen)
350 return;
351#endif
352 gspca_err(gspca_dev, "urb status: %d\n", urb->status);
353 urb->status = 0;
354 goto resubmit;
355 }
356
357 gspca_dbg(gspca_dev, D_PACK, "packet l:%d\n", urb->actual_length);
358 gspca_dev->sd_desc->pkt_scan(gspca_dev,
359 urb->transfer_buffer,
360 urb->actual_length);
361
362resubmit:
363 if (!gspca_dev->streaming)
364 return;
365
366 if (gspca_dev->cam.bulk_nurbs != 0) {
367 st = usb_submit_urb(urb, GFP_ATOMIC);
368 if (st < 0)
369 pr_err("usb_submit_urb() ret %d\n", st);
370 }
371}
372
373
374
375
376
377
378
379
380
381
382
383
384void gspca_frame_add(struct gspca_dev *gspca_dev,
385 enum gspca_packet_type packet_type,
386 const u8 *data,
387 int len)
388{
389 struct gspca_buffer *buf;
390 unsigned long flags;
391
392 gspca_dbg(gspca_dev, D_PACK, "add t:%d l:%d\n", packet_type, len);
393
394 spin_lock_irqsave(&gspca_dev->qlock, flags);
395 buf = list_first_entry_or_null(&gspca_dev->buf_list,
396 typeof(*buf), list);
397 spin_unlock_irqrestore(&gspca_dev->qlock, flags);
398
399 if (packet_type == FIRST_PACKET) {
400
401 if (!buf) {
402 gspca_dev->last_packet_type = DISCARD_PACKET;
403 gspca_dev->sequence++;
404 return;
405 }
406 gspca_dev->image = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
407 gspca_dev->image_len = 0;
408 } else {
409 switch (gspca_dev->last_packet_type) {
410 case DISCARD_PACKET:
411 if (packet_type == LAST_PACKET) {
412 gspca_dev->last_packet_type = packet_type;
413 gspca_dev->image = NULL;
414 gspca_dev->image_len = 0;
415 }
416 return;
417 case LAST_PACKET:
418 return;
419 }
420 }
421
422
423 if (len > 0) {
424 if (gspca_dev->image_len + len > PAGE_ALIGN(gspca_dev->pixfmt.sizeimage)) {
425 gspca_err(gspca_dev, "frame overflow %d > %d\n",
426 gspca_dev->image_len + len,
427 PAGE_ALIGN(gspca_dev->pixfmt.sizeimage));
428 packet_type = DISCARD_PACKET;
429 } else {
430
431
432
433
434
435
436 memcpy(gspca_dev->image + gspca_dev->image_len,
437 data, len);
438 gspca_dev->image_len += len;
439 }
440 }
441 gspca_dev->last_packet_type = packet_type;
442
443
444
445
446 if (packet_type == LAST_PACKET) {
447 spin_lock_irqsave(&gspca_dev->qlock, flags);
448 list_del(&buf->list);
449 spin_unlock_irqrestore(&gspca_dev->qlock, flags);
450 buf->vb.vb2_buf.timestamp = ktime_get_ns();
451 vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
452 gspca_dev->image_len);
453 buf->vb.sequence = gspca_dev->sequence++;
454 buf->vb.field = V4L2_FIELD_NONE;
455 gspca_dbg(gspca_dev, D_FRAM, "frame complete len:%d\n",
456 gspca_dev->image_len);
457 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
458 gspca_dev->image = NULL;
459 gspca_dev->image_len = 0;
460 }
461}
462EXPORT_SYMBOL(gspca_frame_add);
463
464static void destroy_urbs(struct gspca_dev *gspca_dev)
465{
466 struct urb *urb;
467 unsigned int i;
468
469 gspca_dbg(gspca_dev, D_STREAM, "kill transfer\n");
470
471
472
473
474
475 for (i = 0; i < MAX_NURBS; i++)
476 usb_kill_urb(gspca_dev->urb[i]);
477
478 gspca_dbg(gspca_dev, D_STREAM, "releasing urbs\n");
479 for (i = 0; i < MAX_NURBS; i++) {
480 urb = gspca_dev->urb[i];
481 if (!urb)
482 continue;
483 gspca_dev->urb[i] = NULL;
484 usb_free_coherent(gspca_dev->dev,
485 urb->transfer_buffer_length,
486 urb->transfer_buffer,
487 urb->transfer_dma);
488 usb_free_urb(urb);
489 }
490}
491
492static int gspca_set_alt0(struct gspca_dev *gspca_dev)
493{
494 int ret;
495
496 if (gspca_dev->alt == 0)
497 return 0;
498 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
499 if (ret < 0)
500 pr_err("set alt 0 err %d\n", ret);
501 return ret;
502}
503
504
505
506
507
508
509
510static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
511 int xfer, int xfer_ep)
512{
513 struct usb_host_endpoint *ep;
514 int i, attr;
515
516 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
517 ep = &alt->endpoint[i];
518 attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
519 if (attr == xfer
520 && ep->desc.wMaxPacketSize != 0
521 && usb_endpoint_dir_in(&ep->desc)
522 && (xfer_ep < 0 || ep->desc.bEndpointAddress == xfer_ep))
523 return ep;
524 }
525 return NULL;
526}
527
528
529static u32 which_bandwidth(struct gspca_dev *gspca_dev)
530{
531 u32 bandwidth;
532
533
534 bandwidth = gspca_dev->pixfmt.sizeimage;
535
536
537 if (!gspca_dev->cam.needs_full_bandwidth &&
538 bandwidth < gspca_dev->pixfmt.width *
539 gspca_dev->pixfmt.height)
540 bandwidth = bandwidth * 3 / 8;
541
542
543 if (gspca_dev->sd_desc->get_streamparm) {
544 struct v4l2_streamparm parm;
545
546 gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
547 bandwidth *= parm.parm.capture.timeperframe.denominator;
548 bandwidth /= parm.parm.capture.timeperframe.numerator;
549 } else {
550
551
552
553 if (gspca_dev->pixfmt.width >= 640
554 && gspca_dev->dev->speed == USB_SPEED_FULL)
555 bandwidth *= 15;
556 else
557 bandwidth *= 30;
558 }
559
560 gspca_dbg(gspca_dev, D_STREAM, "min bandwidth: %d\n", bandwidth);
561 return bandwidth;
562}
563
564
565#define MAX_ALT 16
566struct ep_tb_s {
567 u32 alt;
568 u32 bandwidth;
569};
570
571
572
573
574
575static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
576 struct usb_interface *intf,
577 struct ep_tb_s *ep_tb)
578{
579 struct usb_host_endpoint *ep;
580 int i, j, nbalt, psize, found;
581 u32 bandwidth, last_bw;
582
583 nbalt = intf->num_altsetting;
584 if (nbalt > MAX_ALT)
585 nbalt = MAX_ALT;
586
587
588 i = 0;
589 last_bw = 0;
590 for (;;) {
591 ep_tb->bandwidth = 2000 * 2000 * 120;
592 found = 0;
593 for (j = 0; j < nbalt; j++) {
594 ep = alt_xfer(&intf->altsetting[j],
595 USB_ENDPOINT_XFER_ISOC,
596 gspca_dev->xfer_ep);
597 if (ep == NULL)
598 continue;
599 if (ep->desc.bInterval == 0) {
600 pr_err("alt %d iso endp with 0 interval\n", j);
601 continue;
602 }
603 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
604 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
605 bandwidth = psize * 1000;
606 if (gspca_dev->dev->speed == USB_SPEED_HIGH
607 || gspca_dev->dev->speed >= USB_SPEED_SUPER)
608 bandwidth *= 8;
609 bandwidth /= 1 << (ep->desc.bInterval - 1);
610 if (bandwidth <= last_bw)
611 continue;
612 if (bandwidth < ep_tb->bandwidth) {
613 ep_tb->bandwidth = bandwidth;
614 ep_tb->alt = j;
615 found = 1;
616 }
617 }
618 if (!found)
619 break;
620 gspca_dbg(gspca_dev, D_STREAM, "alt %d bandwidth %d\n",
621 ep_tb->alt, ep_tb->bandwidth);
622 last_bw = ep_tb->bandwidth;
623 i++;
624 ep_tb++;
625 }
626
627
628
629
630
631
632
633
634
635 if (gspca_dev->audio &&
636 gspca_dev->dev->speed == USB_SPEED_FULL &&
637 last_bw >= 1000000 &&
638 i > 1) {
639 gspca_dbg(gspca_dev, D_STREAM, "dev has usb audio, skipping highest alt\n");
640 i--;
641 ep_tb--;
642 }
643
644
645 bandwidth = which_bandwidth(gspca_dev);
646 ep_tb--;
647 while (i > 1) {
648 ep_tb--;
649 if (ep_tb->bandwidth < bandwidth)
650 break;
651 i--;
652 }
653 return i;
654}
655
656
657
658
659static int create_urbs(struct gspca_dev *gspca_dev,
660 struct usb_host_endpoint *ep)
661{
662 struct urb *urb;
663 int n, nurbs, i, psize, npkt, bsize;
664
665
666 psize = le16_to_cpu(ep->desc.wMaxPacketSize);
667
668 if (!gspca_dev->cam.bulk) {
669
670
671 if (gspca_dev->pkt_size == 0)
672 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
673 else
674 psize = gspca_dev->pkt_size;
675 npkt = gspca_dev->cam.npkt;
676 if (npkt == 0)
677 npkt = 32;
678 bsize = psize * npkt;
679 gspca_dbg(gspca_dev, D_STREAM,
680 "isoc %d pkts size %d = bsize:%d\n",
681 npkt, psize, bsize);
682 nurbs = DEF_NURBS;
683 } else {
684 npkt = 0;
685 bsize = gspca_dev->cam.bulk_size;
686 if (bsize == 0)
687 bsize = psize;
688 gspca_dbg(gspca_dev, D_STREAM, "bulk bsize:%d\n", bsize);
689 if (gspca_dev->cam.bulk_nurbs != 0)
690 nurbs = gspca_dev->cam.bulk_nurbs;
691 else
692 nurbs = 1;
693 }
694
695 for (n = 0; n < nurbs; n++) {
696 urb = usb_alloc_urb(npkt, GFP_KERNEL);
697 if (!urb)
698 return -ENOMEM;
699 gspca_dev->urb[n] = urb;
700 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
701 bsize,
702 GFP_KERNEL,
703 &urb->transfer_dma);
704
705 if (urb->transfer_buffer == NULL) {
706 pr_err("usb_alloc_coherent failed\n");
707 return -ENOMEM;
708 }
709 urb->dev = gspca_dev->dev;
710 urb->context = gspca_dev;
711 urb->transfer_buffer_length = bsize;
712 if (npkt != 0) {
713 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
714 ep->desc.bEndpointAddress);
715 urb->transfer_flags = URB_ISO_ASAP
716 | URB_NO_TRANSFER_DMA_MAP;
717 urb->interval = 1 << (ep->desc.bInterval - 1);
718 urb->complete = isoc_irq;
719 urb->number_of_packets = npkt;
720 for (i = 0; i < npkt; i++) {
721 urb->iso_frame_desc[i].length = psize;
722 urb->iso_frame_desc[i].offset = psize * i;
723 }
724 } else {
725 urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
726 ep->desc.bEndpointAddress);
727 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
728 urb->complete = bulk_irq;
729 }
730 }
731 return 0;
732}
733
734
735static void gspca_stream_off(struct gspca_dev *gspca_dev)
736{
737 gspca_dev->streaming = false;
738 gspca_dev->usb_err = 0;
739 if (gspca_dev->sd_desc->stopN)
740 gspca_dev->sd_desc->stopN(gspca_dev);
741 destroy_urbs(gspca_dev);
742 gspca_input_destroy_urb(gspca_dev);
743 gspca_set_alt0(gspca_dev);
744 if (gspca_dev->present)
745 gspca_input_create_urb(gspca_dev);
746 if (gspca_dev->sd_desc->stop0)
747 gspca_dev->sd_desc->stop0(gspca_dev);
748 gspca_dbg(gspca_dev, D_STREAM, "stream off OK\n");
749}
750
751
752
753
754static int gspca_init_transfer(struct gspca_dev *gspca_dev)
755{
756 struct usb_interface *intf;
757 struct usb_host_endpoint *ep;
758 struct urb *urb;
759 struct ep_tb_s ep_tb[MAX_ALT];
760 int n, ret, xfer, alt, alt_idx;
761
762
763 gspca_dev->image = NULL;
764 gspca_dev->image_len = 0;
765 gspca_dev->last_packet_type = DISCARD_PACKET;
766
767 gspca_dev->usb_err = 0;
768
769
770 intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
771 gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
772 if (gspca_dev->sd_desc->isoc_init) {
773 ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
774 if (ret < 0)
775 return ret;
776 }
777 xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
778 : USB_ENDPOINT_XFER_ISOC;
779
780
781 if (gspca_dev->alt != 0) {
782 gspca_dev->alt--;
783 ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer,
784 gspca_dev->xfer_ep);
785 if (ep == NULL) {
786 pr_err("bad altsetting %d\n", gspca_dev->alt);
787 return -EIO;
788 }
789 ep_tb[0].alt = gspca_dev->alt;
790 alt_idx = 1;
791 } else {
792
793
794 alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
795 if (alt_idx <= 0) {
796 pr_err("no transfer endpoint found\n");
797 return -EIO;
798 }
799 }
800
801
802
803 gspca_input_destroy_urb(gspca_dev);
804
805 gspca_dev->alt = ep_tb[--alt_idx].alt;
806 alt = -1;
807 for (;;) {
808 if (alt != gspca_dev->alt) {
809 alt = gspca_dev->alt;
810 if (intf->num_altsetting > 1) {
811 ret = usb_set_interface(gspca_dev->dev,
812 gspca_dev->iface,
813 alt);
814 if (ret < 0) {
815 if (ret == -ENOSPC)
816 goto retry;
817 pr_err("set alt %d err %d\n", alt, ret);
818 goto out;
819 }
820 }
821 }
822 if (!gspca_dev->cam.no_urb_create) {
823 gspca_dbg(gspca_dev, D_STREAM, "init transfer alt %d\n",
824 alt);
825 ret = create_urbs(gspca_dev,
826 alt_xfer(&intf->altsetting[alt], xfer,
827 gspca_dev->xfer_ep));
828 if (ret < 0) {
829 destroy_urbs(gspca_dev);
830 goto out;
831 }
832 }
833
834
835 if (gspca_dev->cam.bulk)
836 usb_clear_halt(gspca_dev->dev,
837 gspca_dev->urb[0]->pipe);
838
839
840 ret = gspca_dev->sd_desc->start(gspca_dev);
841 if (ret < 0) {
842 destroy_urbs(gspca_dev);
843 goto out;
844 }
845 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
846 gspca_dev->streaming = true;
847
848
849 if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
850 break;
851
852
853 for (n = 0; n < MAX_NURBS; n++) {
854 urb = gspca_dev->urb[n];
855 if (urb == NULL)
856 break;
857 ret = usb_submit_urb(urb, GFP_KERNEL);
858 if (ret < 0)
859 break;
860 }
861 if (ret >= 0)
862 break;
863
864
865
866 gspca_stream_off(gspca_dev);
867 if (ret != -ENOSPC) {
868 pr_err("usb_submit_urb alt %d err %d\n",
869 gspca_dev->alt, ret);
870 goto out;
871 }
872
873
874
875retry:
876 gspca_err(gspca_dev, "alt %d - bandwidth not wide enough, trying again\n",
877 alt);
878 msleep(20);
879 if (gspca_dev->sd_desc->isoc_nego) {
880 ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
881 if (ret < 0)
882 goto out;
883 } else {
884 if (alt_idx <= 0) {
885 pr_err("no transfer endpoint found\n");
886 ret = -EIO;
887 goto out;
888 }
889 gspca_dev->alt = ep_tb[--alt_idx].alt;
890 }
891 }
892out:
893 gspca_input_create_urb(gspca_dev);
894 return ret;
895}
896
897static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
898{
899 int i;
900
901 i = gspca_dev->cam.nmodes - 1;
902 gspca_dev->curr_mode = i;
903 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i];
904
905
906 v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
907}
908
909static int wxh_to_mode(struct gspca_dev *gspca_dev,
910 int width, int height, u32 pixelformat)
911{
912 int i;
913
914 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
915 if (width == gspca_dev->cam.cam_mode[i].width
916 && height == gspca_dev->cam.cam_mode[i].height
917 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat)
918 return i;
919 }
920 return -EINVAL;
921}
922
923static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
924 int width, int height, u32 pixelformat)
925{
926 int i;
927
928 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
929 if (width >= gspca_dev->cam.cam_mode[i].width
930 && height >= gspca_dev->cam.cam_mode[i].height
931 && pixelformat == gspca_dev->cam.cam_mode[i].pixelformat)
932 return i;
933 }
934 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
935 if (width >= gspca_dev->cam.cam_mode[i].width
936 && height >= gspca_dev->cam.cam_mode[i].height)
937 break;
938 }
939 return i;
940}
941
942
943
944
945static int gspca_get_mode(struct gspca_dev *gspca_dev,
946 int mode,
947 int pixfmt)
948{
949 int modeU, modeD;
950
951 modeU = modeD = mode;
952 while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) {
953 if (--modeD >= 0) {
954 if (gspca_dev->cam.cam_mode[modeD].pixelformat
955 == pixfmt)
956 return modeD;
957 }
958 if (++modeU < gspca_dev->cam.nmodes) {
959 if (gspca_dev->cam.cam_mode[modeU].pixelformat
960 == pixfmt)
961 return modeU;
962 }
963 }
964 return -EINVAL;
965}
966
967#ifdef CONFIG_VIDEO_ADV_DEBUG
968static int vidioc_g_chip_info(struct file *file, void *priv,
969 struct v4l2_dbg_chip_info *chip)
970{
971 struct gspca_dev *gspca_dev = video_drvdata(file);
972
973 gspca_dev->usb_err = 0;
974 if (gspca_dev->sd_desc->get_chip_info)
975 return gspca_dev->sd_desc->get_chip_info(gspca_dev, chip);
976 return chip->match.addr ? -EINVAL : 0;
977}
978
979static int vidioc_g_register(struct file *file, void *priv,
980 struct v4l2_dbg_register *reg)
981{
982 struct gspca_dev *gspca_dev = video_drvdata(file);
983
984 gspca_dev->usb_err = 0;
985 return gspca_dev->sd_desc->get_register(gspca_dev, reg);
986}
987
988static int vidioc_s_register(struct file *file, void *priv,
989 const struct v4l2_dbg_register *reg)
990{
991 struct gspca_dev *gspca_dev = video_drvdata(file);
992
993 gspca_dev->usb_err = 0;
994 return gspca_dev->sd_desc->set_register(gspca_dev, reg);
995}
996#endif
997
998static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
999 struct v4l2_fmtdesc *fmtdesc)
1000{
1001 struct gspca_dev *gspca_dev = video_drvdata(file);
1002 int i, j, index;
1003 __u32 fmt_tb[8];
1004
1005
1006 index = 0;
1007 for (i = gspca_dev->cam.nmodes; --i >= 0; ) {
1008 fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixelformat;
1009 j = 0;
1010 for (;;) {
1011 if (fmt_tb[j] == fmt_tb[index])
1012 break;
1013 j++;
1014 }
1015 if (j == index) {
1016 if (fmtdesc->index == index)
1017 break;
1018 index++;
1019 if (index >= ARRAY_SIZE(fmt_tb))
1020 return -EINVAL;
1021 }
1022 }
1023 if (i < 0)
1024 return -EINVAL;
1025
1026 fmtdesc->pixelformat = fmt_tb[index];
1027 return 0;
1028}
1029
1030static int vidioc_g_fmt_vid_cap(struct file *file, void *_priv,
1031 struct v4l2_format *fmt)
1032{
1033 struct gspca_dev *gspca_dev = video_drvdata(file);
1034 u32 priv = fmt->fmt.pix.priv;
1035
1036 fmt->fmt.pix = gspca_dev->pixfmt;
1037
1038 fmt->fmt.pix.priv = priv;
1039 return 0;
1040}
1041
1042static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1043 struct v4l2_format *fmt)
1044{
1045 int w, h, mode, mode2;
1046
1047 w = fmt->fmt.pix.width;
1048 h = fmt->fmt.pix.height;
1049
1050 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1051 fmt->fmt.pix.pixelformat, w, h);
1052
1053
1054 mode = wxh_to_nearest_mode(gspca_dev, w, h, fmt->fmt.pix.pixelformat);
1055
1056
1057 if (gspca_dev->cam.cam_mode[mode].pixelformat
1058 != fmt->fmt.pix.pixelformat) {
1059
1060
1061 mode2 = gspca_get_mode(gspca_dev, mode,
1062 fmt->fmt.pix.pixelformat);
1063 if (mode2 >= 0)
1064 mode = mode2;
1065 }
1066 fmt->fmt.pix = gspca_dev->cam.cam_mode[mode];
1067 if (gspca_dev->sd_desc->try_fmt) {
1068
1069 fmt->fmt.pix.width = w;
1070 fmt->fmt.pix.height = h;
1071 gspca_dev->sd_desc->try_fmt(gspca_dev, fmt);
1072 }
1073 return mode;
1074}
1075
1076static int vidioc_try_fmt_vid_cap(struct file *file, void *_priv,
1077 struct v4l2_format *fmt)
1078{
1079 struct gspca_dev *gspca_dev = video_drvdata(file);
1080 u32 priv = fmt->fmt.pix.priv;
1081
1082 if (try_fmt_vid_cap(gspca_dev, fmt) < 0)
1083 return -EINVAL;
1084
1085 fmt->fmt.pix.priv = priv;
1086 return 0;
1087}
1088
1089static int vidioc_s_fmt_vid_cap(struct file *file, void *_priv,
1090 struct v4l2_format *fmt)
1091{
1092 struct gspca_dev *gspca_dev = video_drvdata(file);
1093 u32 priv = fmt->fmt.pix.priv;
1094 int mode;
1095
1096 if (vb2_is_busy(&gspca_dev->queue))
1097 return -EBUSY;
1098
1099 mode = try_fmt_vid_cap(gspca_dev, fmt);
1100 if (mode < 0)
1101 return -EINVAL;
1102
1103 gspca_dev->curr_mode = mode;
1104 if (gspca_dev->sd_desc->try_fmt)
1105
1106 gspca_dev->pixfmt = fmt->fmt.pix;
1107 else
1108 gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode];
1109
1110 fmt->fmt.pix.priv = priv;
1111 return 0;
1112}
1113
1114static int vidioc_enum_framesizes(struct file *file, void *priv,
1115 struct v4l2_frmsizeenum *fsize)
1116{
1117 struct gspca_dev *gspca_dev = video_drvdata(file);
1118 int i;
1119 __u32 index = 0;
1120
1121 if (gspca_dev->sd_desc->enum_framesizes)
1122 return gspca_dev->sd_desc->enum_framesizes(gspca_dev, fsize);
1123
1124 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1125 if (fsize->pixel_format !=
1126 gspca_dev->cam.cam_mode[i].pixelformat)
1127 continue;
1128
1129 if (fsize->index == index) {
1130 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1131 fsize->discrete.width =
1132 gspca_dev->cam.cam_mode[i].width;
1133 fsize->discrete.height =
1134 gspca_dev->cam.cam_mode[i].height;
1135 return 0;
1136 }
1137 index++;
1138 }
1139
1140 return -EINVAL;
1141}
1142
1143static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1144 struct v4l2_frmivalenum *fival)
1145{
1146 struct gspca_dev *gspca_dev = video_drvdata(filp);
1147 int mode;
1148 __u32 i;
1149
1150 mode = wxh_to_mode(gspca_dev, fival->width, fival->height,
1151 fival->pixel_format);
1152 if (mode < 0)
1153 return -EINVAL;
1154
1155 if (gspca_dev->cam.mode_framerates == NULL ||
1156 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1157 return -EINVAL;
1158
1159 if (fival->pixel_format !=
1160 gspca_dev->cam.cam_mode[mode].pixelformat)
1161 return -EINVAL;
1162
1163 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1164 if (fival->index == i) {
1165 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1166 fival->discrete.numerator = 1;
1167 fival->discrete.denominator =
1168 gspca_dev->cam.mode_framerates[mode].rates[i];
1169 return 0;
1170 }
1171 }
1172
1173 return -EINVAL;
1174}
1175
1176static void gspca_release(struct v4l2_device *v4l2_device)
1177{
1178 struct gspca_dev *gspca_dev =
1179 container_of(v4l2_device, struct gspca_dev, v4l2_dev);
1180
1181 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1182 v4l2_device_unregister(&gspca_dev->v4l2_dev);
1183 kfree(gspca_dev->usb_buf);
1184 kfree(gspca_dev);
1185}
1186
1187static int vidioc_querycap(struct file *file, void *priv,
1188 struct v4l2_capability *cap)
1189{
1190 struct gspca_dev *gspca_dev = video_drvdata(file);
1191
1192 strscpy((char *)cap->driver, gspca_dev->sd_desc->name,
1193 sizeof(cap->driver));
1194 if (gspca_dev->dev->product != NULL) {
1195 strscpy((char *)cap->card, gspca_dev->dev->product,
1196 sizeof(cap->card));
1197 } else {
1198 snprintf((char *) cap->card, sizeof cap->card,
1199 "USB Camera (%04x:%04x)",
1200 le16_to_cpu(gspca_dev->dev->descriptor.idVendor),
1201 le16_to_cpu(gspca_dev->dev->descriptor.idProduct));
1202 }
1203 usb_make_path(gspca_dev->dev, (char *) cap->bus_info,
1204 sizeof(cap->bus_info));
1205 return 0;
1206}
1207
1208static int vidioc_enum_input(struct file *file, void *priv,
1209 struct v4l2_input *input)
1210{
1211 struct gspca_dev *gspca_dev = video_drvdata(file);
1212
1213 if (input->index != 0)
1214 return -EINVAL;
1215 input->type = V4L2_INPUT_TYPE_CAMERA;
1216 input->status = gspca_dev->cam.input_flags;
1217 strscpy(input->name, gspca_dev->sd_desc->name,
1218 sizeof input->name);
1219 return 0;
1220}
1221
1222static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1223{
1224 *i = 0;
1225 return 0;
1226}
1227
1228static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1229{
1230 if (i > 0)
1231 return -EINVAL;
1232 return 0;
1233}
1234
1235static int vidioc_g_jpegcomp(struct file *file, void *priv,
1236 struct v4l2_jpegcompression *jpegcomp)
1237{
1238 struct gspca_dev *gspca_dev = video_drvdata(file);
1239
1240 gspca_dev->usb_err = 0;
1241 return gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp);
1242}
1243
1244static int vidioc_s_jpegcomp(struct file *file, void *priv,
1245 const struct v4l2_jpegcompression *jpegcomp)
1246{
1247 struct gspca_dev *gspca_dev = video_drvdata(file);
1248
1249 gspca_dev->usb_err = 0;
1250 return gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp);
1251}
1252
1253static int vidioc_g_parm(struct file *filp, void *priv,
1254 struct v4l2_streamparm *parm)
1255{
1256 struct gspca_dev *gspca_dev = video_drvdata(filp);
1257
1258 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
1259
1260 if (!gspca_dev->sd_desc->get_streamparm)
1261 return 0;
1262
1263 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1264 gspca_dev->usb_err = 0;
1265 gspca_dev->sd_desc->get_streamparm(gspca_dev, parm);
1266 return gspca_dev->usb_err;
1267}
1268
1269static int vidioc_s_parm(struct file *filp, void *priv,
1270 struct v4l2_streamparm *parm)
1271{
1272 struct gspca_dev *gspca_dev = video_drvdata(filp);
1273
1274 parm->parm.capture.readbuffers = gspca_dev->queue.min_buffers_needed;
1275
1276 if (!gspca_dev->sd_desc->set_streamparm) {
1277 parm->parm.capture.capability = 0;
1278 return 0;
1279 }
1280
1281 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1282 gspca_dev->usb_err = 0;
1283 gspca_dev->sd_desc->set_streamparm(gspca_dev, parm);
1284 return gspca_dev->usb_err;
1285}
1286
1287static int gspca_queue_setup(struct vb2_queue *vq,
1288 unsigned int *nbuffers, unsigned int *nplanes,
1289 unsigned int sizes[], struct device *alloc_devs[])
1290{
1291 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1292 unsigned int size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage);
1293
1294 if (*nplanes)
1295 return sizes[0] < size ? -EINVAL : 0;
1296 *nplanes = 1;
1297 sizes[0] = size;
1298 return 0;
1299}
1300
1301static int gspca_buffer_prepare(struct vb2_buffer *vb)
1302{
1303 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1304 unsigned long size = PAGE_ALIGN(gspca_dev->pixfmt.sizeimage);
1305
1306 if (vb2_plane_size(vb, 0) < size) {
1307 gspca_err(gspca_dev, "buffer too small (%lu < %lu)\n",
1308 vb2_plane_size(vb, 0), size);
1309 return -EINVAL;
1310 }
1311 return 0;
1312}
1313
1314static void gspca_buffer_finish(struct vb2_buffer *vb)
1315{
1316 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1317
1318 if (!gspca_dev->sd_desc->dq_callback)
1319 return;
1320
1321 gspca_dev->usb_err = 0;
1322 if (gspca_dev->present)
1323 gspca_dev->sd_desc->dq_callback(gspca_dev);
1324}
1325
1326static void gspca_buffer_queue(struct vb2_buffer *vb)
1327{
1328 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vb->vb2_queue);
1329 struct gspca_buffer *buf = to_gspca_buffer(vb);
1330 unsigned long flags;
1331
1332 spin_lock_irqsave(&gspca_dev->qlock, flags);
1333 list_add_tail(&buf->list, &gspca_dev->buf_list);
1334 spin_unlock_irqrestore(&gspca_dev->qlock, flags);
1335}
1336
1337static void gspca_return_all_buffers(struct gspca_dev *gspca_dev,
1338 enum vb2_buffer_state state)
1339{
1340 struct gspca_buffer *buf, *node;
1341 unsigned long flags;
1342
1343 spin_lock_irqsave(&gspca_dev->qlock, flags);
1344 list_for_each_entry_safe(buf, node, &gspca_dev->buf_list, list) {
1345 vb2_buffer_done(&buf->vb.vb2_buf, state);
1346 list_del(&buf->list);
1347 }
1348 spin_unlock_irqrestore(&gspca_dev->qlock, flags);
1349}
1350
1351static int gspca_start_streaming(struct vb2_queue *vq, unsigned int count)
1352{
1353 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1354 int ret;
1355
1356 gspca_dev->sequence = 0;
1357
1358 ret = gspca_init_transfer(gspca_dev);
1359 if (ret)
1360 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_QUEUED);
1361 return ret;
1362}
1363
1364static void gspca_stop_streaming(struct vb2_queue *vq)
1365{
1366 struct gspca_dev *gspca_dev = vb2_get_drv_priv(vq);
1367
1368 gspca_stream_off(gspca_dev);
1369
1370
1371 gspca_return_all_buffers(gspca_dev, VB2_BUF_STATE_ERROR);
1372}
1373
1374static const struct vb2_ops gspca_qops = {
1375 .queue_setup = gspca_queue_setup,
1376 .buf_prepare = gspca_buffer_prepare,
1377 .buf_finish = gspca_buffer_finish,
1378 .buf_queue = gspca_buffer_queue,
1379 .start_streaming = gspca_start_streaming,
1380 .stop_streaming = gspca_stop_streaming,
1381 .wait_prepare = vb2_ops_wait_prepare,
1382 .wait_finish = vb2_ops_wait_finish,
1383};
1384
1385static const struct v4l2_file_operations dev_fops = {
1386 .owner = THIS_MODULE,
1387 .open = v4l2_fh_open,
1388 .release = vb2_fop_release,
1389 .unlocked_ioctl = video_ioctl2,
1390 .read = vb2_fop_read,
1391 .mmap = vb2_fop_mmap,
1392 .poll = vb2_fop_poll,
1393};
1394
1395static const struct v4l2_ioctl_ops dev_ioctl_ops = {
1396 .vidioc_querycap = vidioc_querycap,
1397 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1398 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1399 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1400 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1401 .vidioc_enum_input = vidioc_enum_input,
1402 .vidioc_g_input = vidioc_g_input,
1403 .vidioc_s_input = vidioc_s_input,
1404 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1405 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1406 .vidioc_g_parm = vidioc_g_parm,
1407 .vidioc_s_parm = vidioc_s_parm,
1408 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1409 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1410
1411 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1412 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1413 .vidioc_querybuf = vb2_ioctl_querybuf,
1414 .vidioc_qbuf = vb2_ioctl_qbuf,
1415 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1416 .vidioc_expbuf = vb2_ioctl_expbuf,
1417 .vidioc_streamon = vb2_ioctl_streamon,
1418 .vidioc_streamoff = vb2_ioctl_streamoff,
1419
1420#ifdef CONFIG_VIDEO_ADV_DEBUG
1421 .vidioc_g_chip_info = vidioc_g_chip_info,
1422 .vidioc_g_register = vidioc_g_register,
1423 .vidioc_s_register = vidioc_s_register,
1424#endif
1425 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1426 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1427};
1428
1429static const struct video_device gspca_template = {
1430 .name = "gspca main driver",
1431 .fops = &dev_fops,
1432 .ioctl_ops = &dev_ioctl_ops,
1433 .release = video_device_release_empty,
1434};
1435
1436
1437
1438
1439
1440
1441
1442int gspca_dev_probe2(struct usb_interface *intf,
1443 const struct usb_device_id *id,
1444 const struct sd_desc *sd_desc,
1445 int dev_size,
1446 struct module *module)
1447{
1448 struct gspca_dev *gspca_dev;
1449 struct usb_device *dev = interface_to_usbdev(intf);
1450 struct vb2_queue *q;
1451 int ret;
1452
1453 pr_info("%s-" GSPCA_VERSION " probing %04x:%04x\n",
1454 sd_desc->name, id->idVendor, id->idProduct);
1455
1456
1457 if (dev_size < sizeof *gspca_dev)
1458 dev_size = sizeof *gspca_dev;
1459 gspca_dev = kzalloc(dev_size, GFP_KERNEL);
1460 if (!gspca_dev) {
1461 pr_err("couldn't kzalloc gspca struct\n");
1462 return -ENOMEM;
1463 }
1464 gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
1465 if (!gspca_dev->usb_buf) {
1466 pr_err("out of memory\n");
1467 ret = -ENOMEM;
1468 goto out;
1469 }
1470 gspca_dev->dev = dev;
1471 gspca_dev->iface = intf->cur_altsetting->desc.bInterfaceNumber;
1472 gspca_dev->xfer_ep = -1;
1473
1474
1475 if (dev->actconfig->desc.bNumInterfaces != 1) {
1476 int i;
1477 struct usb_interface *intf2;
1478
1479 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1480 intf2 = dev->actconfig->interface[i];
1481 if (intf2 != NULL
1482 && intf2->altsetting != NULL
1483 && intf2->altsetting->desc.bInterfaceClass ==
1484 USB_CLASS_AUDIO) {
1485 gspca_dev->audio = 1;
1486 break;
1487 }
1488 }
1489 }
1490
1491 gspca_dev->v4l2_dev.release = gspca_release;
1492 ret = v4l2_device_register(&intf->dev, &gspca_dev->v4l2_dev);
1493 if (ret)
1494 goto out;
1495 gspca_dev->present = true;
1496 gspca_dev->sd_desc = sd_desc;
1497 gspca_dev->empty_packet = -1;
1498 gspca_dev->vdev = gspca_template;
1499 gspca_dev->vdev.v4l2_dev = &gspca_dev->v4l2_dev;
1500 gspca_dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1501 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1502 video_set_drvdata(&gspca_dev->vdev, gspca_dev);
1503 gspca_dev->module = module;
1504
1505 mutex_init(&gspca_dev->usb_lock);
1506 gspca_dev->vdev.lock = &gspca_dev->usb_lock;
1507 init_waitqueue_head(&gspca_dev->wq);
1508
1509
1510 q = &gspca_dev->queue;
1511 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1512 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1513 q->drv_priv = gspca_dev;
1514 q->buf_struct_size = sizeof(struct gspca_buffer);
1515 q->ops = &gspca_qops;
1516 q->mem_ops = &vb2_vmalloc_memops;
1517 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1518 q->min_buffers_needed = 2;
1519 q->lock = &gspca_dev->usb_lock;
1520 ret = vb2_queue_init(q);
1521 if (ret)
1522 goto out;
1523 gspca_dev->vdev.queue = q;
1524
1525 INIT_LIST_HEAD(&gspca_dev->buf_list);
1526 spin_lock_init(&gspca_dev->qlock);
1527
1528
1529 ret = sd_desc->config(gspca_dev, id);
1530 if (ret < 0)
1531 goto out;
1532 ret = sd_desc->init(gspca_dev);
1533 if (ret < 0)
1534 goto out;
1535 if (sd_desc->init_controls)
1536 ret = sd_desc->init_controls(gspca_dev);
1537 if (ret < 0)
1538 goto out;
1539 gspca_set_default_mode(gspca_dev);
1540
1541 ret = gspca_input_connect(gspca_dev);
1542 if (ret)
1543 goto out;
1544
1545#ifdef CONFIG_VIDEO_ADV_DEBUG
1546 if (!gspca_dev->sd_desc->get_register)
1547 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_G_REGISTER);
1548 if (!gspca_dev->sd_desc->set_register)
1549 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_DBG_S_REGISTER);
1550#endif
1551 if (!gspca_dev->sd_desc->get_jcomp)
1552 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_G_JPEGCOMP);
1553 if (!gspca_dev->sd_desc->set_jcomp)
1554 v4l2_disable_ioctl(&gspca_dev->vdev, VIDIOC_S_JPEGCOMP);
1555
1556
1557 ret = video_register_device(&gspca_dev->vdev,
1558 VFL_TYPE_GRABBER,
1559 -1);
1560 if (ret < 0) {
1561 pr_err("video_register_device err %d\n", ret);
1562 goto out;
1563 }
1564
1565 usb_set_intfdata(intf, gspca_dev);
1566 gspca_dbg(gspca_dev, D_PROBE, "%s created\n",
1567 video_device_node_name(&gspca_dev->vdev));
1568
1569 gspca_input_create_urb(gspca_dev);
1570
1571 return 0;
1572out:
1573#if IS_ENABLED(CONFIG_INPUT)
1574 if (gspca_dev->input_dev)
1575 input_unregister_device(gspca_dev->input_dev);
1576#endif
1577 v4l2_ctrl_handler_free(gspca_dev->vdev.ctrl_handler);
1578 kfree(gspca_dev->usb_buf);
1579 kfree(gspca_dev);
1580 return ret;
1581}
1582EXPORT_SYMBOL(gspca_dev_probe2);
1583
1584
1585int gspca_dev_probe(struct usb_interface *intf,
1586 const struct usb_device_id *id,
1587 const struct sd_desc *sd_desc,
1588 int dev_size,
1589 struct module *module)
1590{
1591 struct usb_device *dev = interface_to_usbdev(intf);
1592
1593
1594 if (dev->descriptor.bNumConfigurations != 1) {
1595 pr_err("%04x:%04x too many config\n",
1596 id->idVendor, id->idProduct);
1597 return -ENODEV;
1598 }
1599
1600
1601 if (dev->actconfig->desc.bNumInterfaces != 1
1602 && intf->cur_altsetting->desc.bInterfaceNumber != 0)
1603 return -ENODEV;
1604
1605 return gspca_dev_probe2(intf, id, sd_desc, dev_size, module);
1606}
1607EXPORT_SYMBOL(gspca_dev_probe);
1608
1609
1610
1611
1612
1613
1614
1615void gspca_disconnect(struct usb_interface *intf)
1616{
1617 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1618#if IS_ENABLED(CONFIG_INPUT)
1619 struct input_dev *input_dev;
1620#endif
1621
1622 gspca_dbg(gspca_dev, D_PROBE, "%s disconnect\n",
1623 video_device_node_name(&gspca_dev->vdev));
1624
1625 mutex_lock(&gspca_dev->usb_lock);
1626 gspca_dev->present = false;
1627 destroy_urbs(gspca_dev);
1628 gspca_input_destroy_urb(gspca_dev);
1629
1630 vb2_queue_error(&gspca_dev->queue);
1631
1632#if IS_ENABLED(CONFIG_INPUT)
1633 input_dev = gspca_dev->input_dev;
1634 if (input_dev) {
1635 gspca_dev->input_dev = NULL;
1636 input_unregister_device(input_dev);
1637 }
1638#endif
1639
1640 v4l2_device_disconnect(&gspca_dev->v4l2_dev);
1641 video_unregister_device(&gspca_dev->vdev);
1642
1643 mutex_unlock(&gspca_dev->usb_lock);
1644
1645
1646 v4l2_device_put(&gspca_dev->v4l2_dev);
1647}
1648EXPORT_SYMBOL(gspca_disconnect);
1649
1650#ifdef CONFIG_PM
1651int gspca_suspend(struct usb_interface *intf, pm_message_t message)
1652{
1653 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1654
1655 gspca_input_destroy_urb(gspca_dev);
1656
1657 if (!vb2_start_streaming_called(&gspca_dev->queue))
1658 return 0;
1659
1660 mutex_lock(&gspca_dev->usb_lock);
1661 gspca_dev->frozen = 1;
1662 gspca_dev->usb_err = 0;
1663 if (gspca_dev->sd_desc->stopN)
1664 gspca_dev->sd_desc->stopN(gspca_dev);
1665 destroy_urbs(gspca_dev);
1666 gspca_set_alt0(gspca_dev);
1667 if (gspca_dev->sd_desc->stop0)
1668 gspca_dev->sd_desc->stop0(gspca_dev);
1669 mutex_unlock(&gspca_dev->usb_lock);
1670
1671 return 0;
1672}
1673EXPORT_SYMBOL(gspca_suspend);
1674
1675int gspca_resume(struct usb_interface *intf)
1676{
1677 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
1678 int streaming, ret = 0;
1679
1680 mutex_lock(&gspca_dev->usb_lock);
1681 gspca_dev->frozen = 0;
1682 gspca_dev->usb_err = 0;
1683 gspca_dev->sd_desc->init(gspca_dev);
1684
1685
1686
1687
1688
1689 streaming = vb2_start_streaming_called(&gspca_dev->queue);
1690 if (streaming)
1691 ret = gspca_init_transfer(gspca_dev);
1692 else
1693 gspca_input_create_urb(gspca_dev);
1694 mutex_unlock(&gspca_dev->usb_lock);
1695
1696 return ret;
1697}
1698EXPORT_SYMBOL(gspca_resume);
1699#endif
1700
1701
1702static int __init gspca_init(void)
1703{
1704 pr_info("v" GSPCA_VERSION " registered\n");
1705 return 0;
1706}
1707static void __exit gspca_exit(void)
1708{
1709}
1710
1711module_init(gspca_init);
1712module_exit(gspca_exit);
1713
1714module_param_named(debug, gspca_debug, int, 0644);
1715MODULE_PARM_DESC(debug,
1716 "1:probe 2:config 3:stream 4:frame 5:packet 6:usbi 7:usbo");
1717