-
Notifications
You must be signed in to change notification settings - Fork 66
/
gspca.c
2280 lines (1987 loc) · 55.7 KB
/
gspca.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Main USB camera driver
*
* Copyright (C) 2008-2011 Jean-François Moine <http://moinejf.free.fr>
*
* Camera button input handling by Márton Németh
* Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* WARNING: THIS IS A PATCHED VERION OF alt_xfer(),
* WICH RETURNS THE SECOND ISOC ENDPOINT, NOT THE FIRST!
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define GSPCA_VERSION "2.14.0"
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/pagemap.h>
#include <linux/io.h>
#include <asm/page.h>
#include <linux/uaccess.h>
#include <linux/ktime.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
#include "gspca.h"
#if IS_ENABLED(CONFIG_INPUT)
#include <linux/input.h>
#include <linux/usb/input.h>
#endif
/* global values */
#define DEF_NURBS 3 /* default number of URBs */
#if DEF_NURBS > MAX_NURBS
#error "DEF_NURBS too big"
#endif
MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
MODULE_DESCRIPTION("GSPCA USB Camera Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(GSPCA_VERSION);
int gspca_debug;
EXPORT_SYMBOL(gspca_debug);
static void PDEBUG_MODE(struct gspca_dev *gspca_dev, int debug, char *txt,
__u32 pixfmt, int w, int h)
{
if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') {
PDEBUG(debug, "%s %c%c%c%c %dx%d",
txt,
pixfmt & 0xff,
(pixfmt >> 8) & 0xff,
(pixfmt >> 16) & 0xff,
pixfmt >> 24,
w, h);
} else {
PDEBUG(debug, "%s 0x%08x %dx%d",
txt,
pixfmt,
w, h);
}
}
/* specific memory types - !! should be different from V4L2_MEMORY_xxx */
#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
#define GSPCA_MEMORY_READ 7
#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
/*
* VMA operations.
*/
static void gspca_vm_open(struct vm_area_struct *vma)
{
struct gspca_frame *frame = vma->vm_private_data;
frame->vma_use_count++;
frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
}
static void gspca_vm_close(struct vm_area_struct *vma)
{
struct gspca_frame *frame = vma->vm_private_data;
if (--frame->vma_use_count <= 0)
frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED;
}
static const struct vm_operations_struct gspca_vm_ops = {
.open = gspca_vm_open,
.close = gspca_vm_close,
};
/*
* Input and interrupt endpoint handling functions
*/
#if IS_ENABLED(CONFIG_INPUT)
static void int_irq(struct urb *urb)
{
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
int ret;
ret = urb->status;
switch (ret) {
case 0:
if (gspca_dev->sd_desc->int_pkt_scan(gspca_dev,
urb->transfer_buffer, urb->actual_length) < 0) {
PERR("Unknown packet received");
}
break;
case -ENOENT:
case -ECONNRESET:
case -ENODEV:
case -ESHUTDOWN:
/* Stop is requested either by software or hardware is gone,
* keep the ret value non-zero and don't resubmit later.
*/
break;
default:
PERR("URB error %i, resubmitting", urb->status);
urb->status = 0;
ret = 0;
}
if (ret == 0) {
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret < 0)
pr_err("Resubmit URB failed with error %i\n", ret);
}
}
static int gspca_input_connect(struct gspca_dev *dev)
{
struct input_dev *input_dev;
int err = 0;
dev->input_dev = NULL;
if (dev->sd_desc->int_pkt_scan || dev->sd_desc->other_input) {
input_dev = input_allocate_device();
if (!input_dev)
return -ENOMEM;
usb_make_path(dev->dev, dev->phys, sizeof(dev->phys));
strlcat(dev->phys, "/input0", sizeof(dev->phys));
input_dev->name = dev->sd_desc->name;
input_dev->phys = dev->phys;
usb_to_input_id(dev->dev, &input_dev->id);
input_dev->evbit[0] = BIT_MASK(EV_KEY);
input_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
input_dev->dev.parent = &dev->dev->dev;
err = input_register_device(input_dev);
if (err) {
pr_err("Input device registration failed with error %i\n",
err);
input_dev->dev.parent = NULL;
input_free_device(input_dev);
} else {
dev->input_dev = input_dev;
}
}
return err;
}
static int alloc_and_submit_int_urb(struct gspca_dev *gspca_dev,
struct usb_endpoint_descriptor *ep)
{
unsigned int buffer_len;
int interval;
struct urb *urb;
struct usb_device *dev;
void *buffer = NULL;
int ret = -EINVAL;
buffer_len = le16_to_cpu(ep->wMaxPacketSize);
interval = ep->bInterval;
PDEBUG(D_CONF, "found int in endpoint: 0x%x, "
"buffer_len=%u, interval=%u",
ep->bEndpointAddress, buffer_len, interval);
dev = gspca_dev->dev;
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
ret = -ENOMEM;
goto error;
}
buffer = usb_alloc_coherent(dev, buffer_len,
GFP_KERNEL, &urb->transfer_dma);
if (!buffer) {
ret = -ENOMEM;
goto error_buffer;
}
usb_fill_int_urb(urb, dev,
usb_rcvintpipe(dev, ep->bEndpointAddress),
buffer, buffer_len,
int_irq, (void *)gspca_dev, interval);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
ret = usb_submit_urb(urb, GFP_KERNEL);
if (ret < 0) {
PERR("submit int URB failed with error %i", ret);
goto error_submit;
}
gspca_dev->int_urb = urb;
return ret;
error_submit:
usb_free_coherent(dev,
urb->transfer_buffer_length,
urb->transfer_buffer,
urb->transfer_dma);
error_buffer:
usb_free_urb(urb);
error:
return ret;
}
static void gspca_input_create_urb(struct gspca_dev *gspca_dev)
{
struct usb_interface *intf;
struct usb_host_interface *intf_desc;
struct usb_endpoint_descriptor *ep;
int i;
if (gspca_dev->sd_desc->int_pkt_scan) {
intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
intf_desc = intf->cur_altsetting;
for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
ep = &intf_desc->endpoint[i].desc;
if (usb_endpoint_dir_in(ep) &&
usb_endpoint_xfer_int(ep)) {
alloc_and_submit_int_urb(gspca_dev, ep);
break;
}
}
}
}
static void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
{
struct urb *urb;
urb = gspca_dev->int_urb;
if (urb) {
gspca_dev->int_urb = NULL;
usb_kill_urb(urb);
usb_free_coherent(gspca_dev->dev,
urb->transfer_buffer_length,
urb->transfer_buffer,
urb->transfer_dma);
usb_free_urb(urb);
}
}
#else
static inline void gspca_input_destroy_urb(struct gspca_dev *gspca_dev)
{
}
static inline void gspca_input_create_urb(struct gspca_dev *gspca_dev)
{
}
static inline int gspca_input_connect(struct gspca_dev *dev)
{
return 0;
}
#endif
/*
* fill a video frame from an URB and resubmit
*/
static void fill_frame(struct gspca_dev *gspca_dev,
struct urb *urb)
{
u8 *data; /* address of data in the iso message */
int i, len, st;
cam_pkt_op pkt_scan;
if (urb->status != 0) {
if (urb->status == -ESHUTDOWN)
return; /* disconnection */
#ifdef CONFIG_PM
if (gspca_dev->frozen)
return;
#endif
PERR("urb status: %d", urb->status);
urb->status = 0;
goto resubmit;
}
pkt_scan = gspca_dev->sd_desc->pkt_scan;
for (i = 0; i < urb->number_of_packets; i++) {
len = urb->iso_frame_desc[i].actual_length;
/* check the packet status and length */
st = urb->iso_frame_desc[i].status;
if (st) {
pr_err("ISOC data error: [%d] len=%d, status=%d\n",
i, len, st);
gspca_dev->last_packet_type = DISCARD_PACKET;
continue;
}
if (len == 0) {
if (gspca_dev->empty_packet == 0)
gspca_dev->empty_packet = 1;
continue;
}
/* let the packet be analyzed by the subdriver */
PDEBUG(D_PACK, "packet [%d] o:%d l:%d",
i, urb->iso_frame_desc[i].offset, len);
data = (u8 *) urb->transfer_buffer
+ urb->iso_frame_desc[i].offset;
pkt_scan(gspca_dev, data, len);
}
resubmit:
/* resubmit the URB */
st = usb_submit_urb(urb, GFP_ATOMIC);
if (st < 0)
pr_err("usb_submit_urb() ret %d\n", st);
}
/*
* ISOC message interrupt from the USB device
*
* Analyse each packet and call the subdriver for copy to the frame buffer.
*/
static void isoc_irq(struct urb *urb)
{
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
PDEBUG(D_PACK, "isoc irq");
if (!gspca_dev->streaming)
return;
fill_frame(gspca_dev, urb);
}
/*
* bulk message interrupt from the USB device
*/
static void bulk_irq(struct urb *urb)
{
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
int st;
PDEBUG(D_PACK, "bulk irq");
if (!gspca_dev->streaming)
return;
switch (urb->status) {
case 0:
break;
case -ESHUTDOWN:
return; /* disconnection */
default:
#ifdef CONFIG_PM
if (gspca_dev->frozen)
return;
#endif
PERR("urb status: %d", urb->status);
urb->status = 0;
goto resubmit;
}
PDEBUG(D_PACK, "packet l:%d", urb->actual_length);
gspca_dev->sd_desc->pkt_scan(gspca_dev,
urb->transfer_buffer,
urb->actual_length);
resubmit:
/* resubmit the URB */
if (gspca_dev->cam.bulk_nurbs != 0) {
st = usb_submit_urb(urb, GFP_ATOMIC);
if (st < 0)
pr_err("usb_submit_urb() ret %d\n", st);
}
}
/*
* add data to the current frame
*
* This function is called by the subdrivers at interrupt level.
*
* To build a frame, these ones must add
* - one FIRST_PACKET
* - 0 or many INTER_PACKETs
* - one LAST_PACKET
* DISCARD_PACKET invalidates the whole frame.
*/
void gspca_frame_add(struct gspca_dev *gspca_dev,
enum gspca_packet_type packet_type,
const u8 *data,
int len)
{
struct gspca_frame *frame;
int i, j;
PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len);
if (packet_type == FIRST_PACKET) {
i = atomic_read(&gspca_dev->fr_i);
/* if there are no queued buffer, discard the whole frame */
if (i == atomic_read(&gspca_dev->fr_q)) {
gspca_dev->last_packet_type = DISCARD_PACKET;
gspca_dev->sequence++;
return;
}
j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j];
frame->v4l2_buf.timestamp = ktime_to_timeval(ktime_get());
frame->v4l2_buf.sequence = gspca_dev->sequence++;
gspca_dev->image = frame->data;
gspca_dev->image_len = 0;
} else {
switch (gspca_dev->last_packet_type) {
case DISCARD_PACKET:
if (packet_type == LAST_PACKET) {
gspca_dev->last_packet_type = packet_type;
gspca_dev->image = NULL;
gspca_dev->image_len = 0;
}
return;
case LAST_PACKET:
return;
}
}
/* append the packet to the frame buffer */
if (len > 0) {
if (gspca_dev->image_len + len > gspca_dev->frsz) {
PERR("frame overflow %d > %d",
gspca_dev->image_len + len,
gspca_dev->frsz);
packet_type = DISCARD_PACKET;
} else {
/* !! image is NULL only when last pkt is LAST or DISCARD
if (gspca_dev->image == NULL) {
pr_err("gspca_frame_add() image == NULL\n");
return;
}
*/
memcpy(gspca_dev->image + gspca_dev->image_len,
data, len);
gspca_dev->image_len += len;
}
}
gspca_dev->last_packet_type = packet_type;
/* if last packet, invalidate packet concatenation until
* next first packet, wake up the application and advance
* in the queue */
if (packet_type == LAST_PACKET) {
i = atomic_read(&gspca_dev->fr_i);
j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j];
frame->v4l2_buf.bytesused = gspca_dev->image_len;
frame->v4l2_buf.flags = (frame->v4l2_buf.flags
| V4L2_BUF_FLAG_DONE)
& ~V4L2_BUF_FLAG_QUEUED;
i = (i + 1) % GSPCA_MAX_FRAMES;
atomic_set(&gspca_dev->fr_i, i);
wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
PDEBUG(D_FRAM, "frame complete len:%d",
frame->v4l2_buf.bytesused);
gspca_dev->image = NULL;
gspca_dev->image_len = 0;
}
}
EXPORT_SYMBOL(gspca_frame_add);
static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
enum v4l2_memory memory, unsigned int count)
{
struct gspca_frame *frame;
unsigned int frsz;
int i;
i = gspca_dev->curr_mode;
frsz = gspca_dev->cam.cam_mode[i].sizeimage;
PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
frsz = PAGE_ALIGN(frsz);
if (count >= GSPCA_MAX_FRAMES)
count = GSPCA_MAX_FRAMES - 1;
gspca_dev->frbuf = vmalloc_32(frsz * count);
if (!gspca_dev->frbuf) {
pr_err("frame alloc failed\n");
return -ENOMEM;
}
gspca_dev->capt_file = file;
gspca_dev->memory = memory;
gspca_dev->frsz = frsz;
gspca_dev->nframes = count;
for (i = 0; i < count; i++) {
frame = &gspca_dev->frame[i];
frame->v4l2_buf.index = i;
frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
frame->v4l2_buf.flags = 0;
frame->v4l2_buf.field = V4L2_FIELD_NONE;
frame->v4l2_buf.length = frsz;
frame->v4l2_buf.memory = memory;
frame->v4l2_buf.sequence = 0;
frame->data = gspca_dev->frbuf + i * frsz;
frame->v4l2_buf.m.offset = i * frsz;
}
atomic_set(&gspca_dev->fr_q, 0);
atomic_set(&gspca_dev->fr_i, 0);
gspca_dev->fr_o = 0;
return 0;
}
static void frame_free(struct gspca_dev *gspca_dev)
{
int i;
PDEBUG(D_STREAM, "frame free");
if (gspca_dev->frbuf != NULL) {
vfree(gspca_dev->frbuf);
gspca_dev->frbuf = NULL;
for (i = 0; i < gspca_dev->nframes; i++)
gspca_dev->frame[i].data = NULL;
}
gspca_dev->nframes = 0;
gspca_dev->frsz = 0;
gspca_dev->capt_file = NULL;
gspca_dev->memory = GSPCA_MEMORY_NO;
}
static void destroy_urbs(struct gspca_dev *gspca_dev)
{
struct urb *urb;
unsigned int i;
PDEBUG(D_STREAM, "kill transfer");
for (i = 0; i < MAX_NURBS; i++) {
urb = gspca_dev->urb[i];
if (urb == NULL)
break;
gspca_dev->urb[i] = NULL;
usb_kill_urb(urb);
usb_free_coherent(gspca_dev->dev,
urb->transfer_buffer_length,
urb->transfer_buffer,
urb->transfer_dma);
usb_free_urb(urb);
}
}
static int gspca_set_alt0(struct gspca_dev *gspca_dev)
{
int ret;
if (gspca_dev->alt == 0)
return 0;
ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0);
if (ret < 0)
pr_err("set alt 0 err %d\n", ret);
return ret;
}
/* Note: both the queue and the usb locks should be held when calling this */
static void gspca_stream_off(struct gspca_dev *gspca_dev)
{
gspca_dev->streaming = 0;
gspca_dev->usb_err = 0;
if (gspca_dev->sd_desc->stopN)
gspca_dev->sd_desc->stopN(gspca_dev);
destroy_urbs(gspca_dev);
gspca_input_destroy_urb(gspca_dev);
gspca_set_alt0(gspca_dev);
gspca_input_create_urb(gspca_dev);
if (gspca_dev->sd_desc->stop0)
gspca_dev->sd_desc->stop0(gspca_dev);
PDEBUG(D_STREAM, "stream off OK");
}
/*
* look for an input transfer endpoint in an alternate setting
* patched to get _only_ the second endpoint
*/
static struct usb_host_endpoint *alt_xfer(struct usb_host_interface *alt,
int xfer)
{
struct usb_host_endpoint *ep;
int i, attr, ep_nr;
ep_nr=0;
for (i = 0; i < alt->desc.bNumEndpoints; i++) {
ep = &alt->endpoint[i];
attr = ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
if (attr == xfer
&& ep->desc.wMaxPacketSize != 0
&& usb_endpoint_dir_in(&ep->desc)){
if(ep_nr > 0){
return ep;
}
else
ep_nr++;
}
}
return NULL;
}
/* compute the minimum bandwidth for the current transfer */
static u32 which_bandwidth(struct gspca_dev *gspca_dev)
{
u32 bandwidth;
int i;
/* get the (max) image size */
i = gspca_dev->curr_mode;
bandwidth = gspca_dev->cam.cam_mode[i].sizeimage;
/* if the image is compressed, estimate its mean size */
if (!gspca_dev->cam.needs_full_bandwidth &&
bandwidth < gspca_dev->cam.cam_mode[i].width *
gspca_dev->cam.cam_mode[i].height)
bandwidth = bandwidth * 3 / 8; /* 0.375 */
/* estimate the frame rate */
if (gspca_dev->sd_desc->get_streamparm) {
struct v4l2_streamparm parm;
gspca_dev->sd_desc->get_streamparm(gspca_dev, &parm);
bandwidth *= parm.parm.capture.timeperframe.denominator;
bandwidth /= parm.parm.capture.timeperframe.numerator;
} else {
/* don't hope more than 15 fps with USB 1.1 and
* image resolution >= 640x480 */
if (gspca_dev->width >= 640
&& gspca_dev->dev->speed == USB_SPEED_FULL)
bandwidth *= 15; /* 15 fps */
else
bandwidth *= 30; /* 30 fps */
}
PDEBUG(D_STREAM, "min bandwidth: %d", bandwidth);
return bandwidth;
}
/* endpoint table */
#define MAX_ALT 16
struct ep_tb_s {
u32 alt;
u32 bandwidth;
};
/*
* build the table of the endpoints
* and compute the minimum bandwidth for the image transfer
*/
static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
struct usb_interface *intf,
struct ep_tb_s *ep_tb)
{
struct usb_host_endpoint *ep;
int i, j, nbalt, psize, found;
u32 bandwidth, last_bw;
nbalt = intf->num_altsetting;
if (nbalt > MAX_ALT)
nbalt = MAX_ALT; /* fixme: should warn */
/* build the endpoint table */
i = 0;
last_bw = 0;
for (;;) {
ep_tb->bandwidth = 2000 * 2000 * 120;
found = 0;
for (j = 0; j < nbalt; j++) {
ep = alt_xfer(&intf->altsetting[j],
USB_ENDPOINT_XFER_ISOC);
if (ep == NULL)
continue;
if (ep->desc.bInterval == 0) {
pr_err("alt %d iso endp with 0 interval\n", j);
continue;
}
psize = le16_to_cpu(ep->desc.wMaxPacketSize);
psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
bandwidth = psize * 1000;
if (gspca_dev->dev->speed == USB_SPEED_HIGH
|| gspca_dev->dev->speed == USB_SPEED_SUPER)
bandwidth *= 8;
bandwidth /= 1 << (ep->desc.bInterval - 1);
if (bandwidth <= last_bw)
continue;
if (bandwidth < ep_tb->bandwidth) {
ep_tb->bandwidth = bandwidth;
ep_tb->alt = j;
found = 1;
}
}
if (!found)
break;
PDEBUG(D_STREAM, "alt %d bandwidth %d",
ep_tb->alt, ep_tb->bandwidth);
last_bw = ep_tb->bandwidth;
i++;
ep_tb++;
}
/*
* If the camera:
* has a usb audio class interface (a built in usb mic); and
* is a usb 1 full speed device; and
* uses the max full speed iso bandwidth; and
* and has more than 1 alt setting
* then skip the highest alt setting to spare bandwidth for the mic
*/
if (gspca_dev->audio &&
gspca_dev->dev->speed == USB_SPEED_FULL &&
last_bw >= 1000000 &&
i > 1) {
PDEBUG(D_STREAM, "dev has usb audio, skipping highest alt");
i--;
ep_tb--;
}
/* get the requested bandwidth and start at the highest atlsetting */
bandwidth = which_bandwidth(gspca_dev);
ep_tb--;
while (i > 1) {
ep_tb--;
if (ep_tb->bandwidth < bandwidth)
break;
i--;
}
return i;
}
/*
* create the URBs for image transfer
*/
static int create_urbs(struct gspca_dev *gspca_dev,
struct usb_host_endpoint *ep)
{
struct urb *urb;
int n, nurbs, i, psize, npkt, bsize;
/* calculate the packet size and the number of packets */
psize = le16_to_cpu(ep->desc.wMaxPacketSize);
if (!gspca_dev->cam.bulk) { /* isoc */
/* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
if (gspca_dev->pkt_size == 0)
psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
else
psize = gspca_dev->pkt_size;
npkt = gspca_dev->cam.npkt;
if (npkt == 0)
npkt = 32; /* default value */
bsize = psize * npkt;
PDEBUG(D_STREAM,
"isoc %d pkts size %d = bsize:%d",
npkt, psize, bsize);
nurbs = DEF_NURBS;
} else { /* bulk */
npkt = 0;
bsize = gspca_dev->cam.bulk_size;
if (bsize == 0)
bsize = psize;
PDEBUG(D_STREAM, "bulk bsize:%d", bsize);
if (gspca_dev->cam.bulk_nurbs != 0)
nurbs = gspca_dev->cam.bulk_nurbs;
else
nurbs = 1;
}
for (n = 0; n < nurbs; n++) {
urb = usb_alloc_urb(npkt, GFP_KERNEL);
if (!urb) {
pr_err("usb_alloc_urb failed\n");
return -ENOMEM;
}
gspca_dev->urb[n] = urb;
urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
bsize,
GFP_KERNEL,
&urb->transfer_dma);
if (urb->transfer_buffer == NULL) {
pr_err("usb_alloc_coherent failed\n");
return -ENOMEM;
}
urb->dev = gspca_dev->dev;
urb->context = gspca_dev;
urb->transfer_buffer_length = bsize;
if (npkt != 0) { /* ISOC */
urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
ep->desc.bEndpointAddress);
urb->transfer_flags = URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP;
urb->interval = 1 << (ep->desc.bInterval - 1);
urb->complete = isoc_irq;
urb->number_of_packets = npkt;
for (i = 0; i < npkt; i++) {
urb->iso_frame_desc[i].length = psize;
urb->iso_frame_desc[i].offset = psize * i;
}
} else { /* bulk */
urb->pipe = usb_rcvbulkpipe(gspca_dev->dev,
ep->desc.bEndpointAddress);
urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
urb->complete = bulk_irq;
}
}
return 0;
}
/*
* start the USB transfer
*/
static int gspca_init_transfer(struct gspca_dev *gspca_dev)
{
struct usb_interface *intf;
struct usb_host_endpoint *ep;
struct urb *urb;
struct ep_tb_s ep_tb[MAX_ALT];
int n, ret, xfer, alt, alt_idx;
/* reset the streaming variables */
gspca_dev->image = NULL;
gspca_dev->image_len = 0;
gspca_dev->last_packet_type = DISCARD_PACKET;
gspca_dev->sequence = 0;
gspca_dev->usb_err = 0;
/* do the specific subdriver stuff before endpoint selection */
intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
gspca_dev->alt = gspca_dev->cam.bulk ? intf->num_altsetting : 0;
if (gspca_dev->sd_desc->isoc_init) {
ret = gspca_dev->sd_desc->isoc_init(gspca_dev);
if (ret < 0)
return ret;
}
xfer = gspca_dev->cam.bulk ? USB_ENDPOINT_XFER_BULK
: USB_ENDPOINT_XFER_ISOC;
/* if bulk or the subdriver forced an altsetting, get the endpoint */
if (gspca_dev->alt != 0) {
gspca_dev->alt--; /* (previous version compatibility) */
ep = alt_xfer(&intf->altsetting[gspca_dev->alt], xfer);
if (ep == NULL) {
pr_err("bad altsetting %d\n", gspca_dev->alt);
return -EIO;
}
ep_tb[0].alt = gspca_dev->alt;
alt_idx = 1;
} else {
/* else, compute the minimum bandwidth
* and build the endpoint table */
alt_idx = build_isoc_ep_tb(gspca_dev, intf, ep_tb);
if (alt_idx <= 0) {
pr_err("no transfer endpoint found\n");
return -EIO;
}
}
/* set the highest alternate setting and
* loop until urb submit succeeds */
gspca_input_destroy_urb(gspca_dev);
gspca_dev->alt = ep_tb[--alt_idx].alt;
alt = -1;
for (;;) {
if (alt != gspca_dev->alt) {
alt = gspca_dev->alt;
if (intf->num_altsetting > 1) {
ret = usb_set_interface(gspca_dev->dev,
gspca_dev->iface,
alt);
if (ret < 0) {
if (ret == -ENOSPC)
goto retry; /*fixme: ugly*/
pr_err("set alt %d err %d\n", alt, ret);
goto out;
}
}
}
if (!gspca_dev->cam.no_urb_create) {
PDEBUG(D_STREAM, "init transfer alt %d", alt);
ret = create_urbs(gspca_dev,
alt_xfer(&intf->altsetting[alt], xfer));
if (ret < 0) {
destroy_urbs(gspca_dev);
goto out;
}
}
/* clear the bulk endpoint */
if (gspca_dev->cam.bulk)
usb_clear_halt(gspca_dev->dev,
gspca_dev->urb[0]->pipe);
/* start the cam */
ret = gspca_dev->sd_desc->start(gspca_dev);
if (ret < 0) {
destroy_urbs(gspca_dev);
goto out;
}
gspca_dev->streaming = 1;
v4l2_ctrl_handler_setup(gspca_dev->vdev.ctrl_handler);
/* some bulk transfers are started by the subdriver */
if (gspca_dev->cam.bulk && gspca_dev->cam.bulk_nurbs == 0)
break;
/* submit the URBs */
for (n = 0; n < MAX_NURBS; n++) {
urb = gspca_dev->urb[n];
if (urb == NULL)
break;
ret = usb_submit_urb(urb, GFP_KERNEL);
if (ret < 0)
break;
}
if (ret >= 0)
break; /* transfer is started */
/* something when wrong
* stop the webcam and free the transfer resources */
gspca_stream_off(gspca_dev);
if (ret != -ENOSPC) {
pr_err("usb_submit_urb alt %d err %d\n",
gspca_dev->alt, ret);
goto out;
}
/* the bandwidth is not wide enough
* negotiate or try a lower alternate setting */
retry:
PERR("alt %d - bandwidth not wide enough, trying again", alt);
msleep(20); /* wait for kill complete */
if (gspca_dev->sd_desc->isoc_nego) {
ret = gspca_dev->sd_desc->isoc_nego(gspca_dev);
if (ret < 0)
goto out;
} else {
if (alt_idx <= 0) {
pr_err("no transfer endpoint found\n");
ret = -EIO;
goto out;
}
gspca_dev->alt = ep_tb[--alt_idx].alt;
}
}
out:
gspca_input_create_urb(gspca_dev);
return ret;
}
static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
{
int i;
i = gspca_dev->cam.nmodes - 1; /* take the highest mode */
gspca_dev->curr_mode = i;
gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixelformat;