forked from hbars/stk11xx-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stk11xx-usb.c
1268 lines (1006 loc) · 31.4 KB
/
stk11xx-usb.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
/**
* @file stk11xx-usb.c
* @author Nicolas VIVIEN
* @date 2006-10-23
* @version v2.2.x
*
* @brief Driver for Syntek USB video camera
*
* @note Copyright (C) Nicolas VIVIEN
*
* @par Licences
*
* 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
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @par SubVersion
* $Date$
* $Revision$
* $Author$
* $HeadURL$
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/kref.h>
#include <linux/mm.h>
#include <linux/usb.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include "stk11xx.h"
/**
* @var default_fps
* Number of frame per second by default
*/
static int default_fps = -1;
/**
* @var default_hflip
* Enable / Disable horizontal flip image
*/
static int default_hflip = -1;
/**
* @var default_vflip
* Enable / Disable vertical flip image
*/
static int default_vflip = -1;
/**
* @var default_brightness
* Set brightness
*/
static int default_brightness = -1;
/**
* @var default_whiteness
* Set whiteness
*/
static int default_whiteness = -1;
/**
* @var default_contrast
* Set contrast
*/
static int default_contrast = -1;
/**
* @var default_colour
* Set colour
*/
static int default_colour = -1;
/**
* @var default_norm
* Set norm
*/
static int default_norm = -1;
/**
* @var stk11xx_table
* Define all the hotplug supported devices by this driver
*/
static struct usb_device_id stk11xx_table[] = {
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_A311_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_A821_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_AA11_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A31_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A33_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A51_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6A54_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK1_VENDOR_ID, USB_STK_6D51_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK2_VENDOR_ID, USB_STK_0408_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK2_VENDOR_ID, USB_STK_0500_PRODUCT_ID) },
{ USB_DEVICE(USB_SYNTEK2_VENDOR_ID, USB_STK_0501_PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE(usb, stk11xx_table); /**< Define the supported devices */
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief Initilize an isochronous pipe.
*
* This function permits to initialize an URB transfert (or isochronous pipe).
*/
int usb_stk11xx_isoc_init(struct usb_stk11xx *dev)
{
int i, j;
int ret = 0;
struct urb *urb;
struct usb_device *udev;
if (dev == NULL)
return -EFAULT;
if (dev->isoc_init_ok)
return 0;
udev = dev->udev;
STK_DEBUG("usb_stk11xx_isoc_init()\n");
// Allocate URB structure
for (i=0; i<MAX_ISO_BUFS; i++) {
urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
if (urb == NULL) {
STK_ERROR("Failed to allocate URB %d\n", i);
ret = -ENOMEM;
break;
}
dev->isobuf[i].urb = urb;
}
if (ret) {
while (i >= 0) {
if (dev->isobuf[i].urb != NULL)
usb_free_urb(dev->isobuf[i].urb);
dev->isobuf[i].urb = NULL;
i--;
}
return ret;
}
// Init URB structure
for (i=0; i<MAX_ISO_BUFS; i++) {
urb = dev->isobuf[i].urb;
urb->interval = 1;
urb->dev = udev;
urb->pipe = usb_rcvisocpipe(udev, dev->isoc_in_endpointAddr);
urb->transfer_flags = URB_ISO_ASAP;
urb->transfer_buffer = dev->isobuf[i].data;
urb->transfer_buffer_length = ISO_BUFFER_SIZE;
urb->complete = usb_stk11xx_isoc_handler;
urb->context = dev;
urb->start_frame = 0;
urb->number_of_packets = ISO_FRAMES_PER_DESC;
for (j=0; j<ISO_FRAMES_PER_DESC; j++) {
urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; //dev->isoc_in_size;
}
}
STK_DEBUG("dev->isoc_in_size = %X\n", dev->isoc_in_size);
STK_DEBUG("dev->isoc_in_endpointAddr = %X\n", dev->isoc_in_endpointAddr);
// Link
for (i=0; i<MAX_ISO_BUFS; i++) {
ret = usb_submit_urb(dev->isobuf[i].urb, GFP_KERNEL);
if (ret)
STK_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
else
STK_DEBUG("URB 0x%p submitted.\n", dev->isobuf[i].urb);
switch (ret) {
case -ENOMEM:
STK_ERROR("ENOMEM\n");
break;
case -ENODEV:
STK_ERROR("ENODEV\n");
break;
case -ENXIO:
STK_ERROR("ENXIO\n");
break;
case -EINVAL:
STK_ERROR("EINVAL\n");
break;
case -EAGAIN:
STK_ERROR("EAGAIN\n");
break;
case -EFBIG:
STK_ERROR("EFBIG\n");
break;
case -EPIPE:
STK_ERROR("EPIPE\n");
break;
case -EMSGSIZE:
STK_ERROR("EMSGSIZE\n");
break;
}
}
// All is done
dev->isoc_init_ok = 1;
return 0;
}
/**
* @param urb URB structure
*
* @brief ISOC handler
*
* This function is called as an URB transfert is complete (Isochronous pipe).
* So, the traitement is done in interrupt time, so it has be fast, not crash,
* ans not stall. Neat.
*/
void usb_stk11xx_isoc_handler(struct urb *urb)
{
int i;
int ret;
int skip;
int awake = 0;
int framestatus;
int framelen;
unsigned char *fill = NULL;
unsigned char *iso_buf = NULL;
struct usb_stk11xx *dev;
struct stk11xx_frame_buf *framebuf;
STK_STREAM("Isoc handler\n");
dev = (struct usb_stk11xx *) urb->context;
if (dev == NULL) {
STK_ERROR("isoc_handler called with NULL device !\n");
return;
}
if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
STK_DEBUG("URB unlinked synchronuously !\n");
return;
}
if (urb->status != -EINPROGRESS && urb->status != 0) {
const char *errmsg;
errmsg = "Unknown";
switch(urb->status) {
case -ENOSR:
errmsg = "Buffer error (overrun)";
break;
case -EPIPE:
errmsg = "Stalled (device not responding)";
break;
case -EOVERFLOW:
errmsg = "Babble (bad cable?)";
break;
case -EPROTO:
errmsg = "Bit-stuff error (bad cable?)";
break;
case -EILSEQ:
errmsg = "CRC/Timeout (could be anything)";
break;
case -ETIMEDOUT:
errmsg = "NAK (device does not respond)";
break;
}
STK_ERROR("isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
dev->visoc_errors++;
wake_up_interruptible(&dev->wait_frame);
urb->dev = dev->udev;
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret != 0) {
STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
}
return;
}
framebuf = dev->fill_frame;
if (framebuf == NULL) {
STK_ERROR("isoc_handler without valid fill frame !\n");
wake_up_interruptible(&dev->wait_frame);
urb->dev = dev->udev;
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret != 0) {
STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
}
return;
}
else {
fill = framebuf->data + framebuf->filled;
}
// Reset ISOC error counter
dev->visoc_errors = 0;
// Compact data
for (i=0; i<urb->number_of_packets; i++) {
framestatus = urb->iso_frame_desc[i].status;
framelen = urb->iso_frame_desc[i].actual_length;
iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
if (framestatus == 0) {
skip = 4;
if (framelen > 4) {
// we found something informational from there
// the isoc frames have to type of headers
// type1: 00 xx 00 00 or 20 xx 00 00
// type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
// xx is a sequencer which has never been seen over 0x3f
//
// imho data written down looks like bayer, i see similarities after
// every 640 bytes
if (*iso_buf & 0x80) {
skip = 8;
}
// Determine if odd or even frame, and set a flag
if (framelen == 8) {
if (*iso_buf & 0x40)
framebuf->odd = true;
else
framebuf->odd = false;
}
// Our buffer is full !!!
if (framelen - skip + framebuf->filled > dev->frame_size) {
STK_ERROR("Frame buffer overflow %d %d %d!\n",
framelen, framelen-skip+framebuf->filled, dev->frame_size);
framebuf->errors++;
}
// All is OK
else {
memcpy(fill, iso_buf + skip, framelen - skip);
fill += framelen - skip;
}
// New size of our buffer
framebuf->filled += framelen - skip;
}
STK_STREAM("URB : Length = %d - Skip = %d - Buffer size = %d\n",
framelen, skip, framebuf->filled);
// Data is always follow by a frame with a length '4'
if (framelen == 4) {
if (framebuf->filled > 0) {
// Our buffer has enough data ?
if (framebuf->filled < dev->frame_size)
framebuf->errors++;
// If there are errors, we skip a frame...
if (framebuf->errors == 0) {
if (stk11xx_next_frame(dev))
dev->vframes_dumped++;
}
else
dev->vframes_error++;
awake = 1;
framebuf = dev->fill_frame;
framebuf->filled = 0;
framebuf->errors = 0;
fill = framebuf->data;
}
}
}
else {
STK_ERROR("Iso frame %d of USB has error %d\n", i, framestatus);
}
}
if (awake == 1)
wake_up_interruptible(&dev->wait_frame);
urb->dev = dev->udev;
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret != 0) {
STK_ERROR("Error (%d) re-submitting urb in stk11xx_isoc_handler.\n", ret);
}
}
/**
* @param dev Device structure
*
* @brief Clean-up all the ISOC buffers
*
* This function permits to clean-up all the ISOC buffers.
*/
void usb_stk11xx_isoc_cleanup(struct usb_stk11xx *dev)
{
int i;
STK_DEBUG("Isoc cleanup\n");
if (dev == NULL)
return;
if (dev->isoc_init_ok == 0)
return;
// Unlinking ISOC buffers
for (i=0; i<MAX_ISO_BUFS; i++) {
struct urb *urb;
urb = dev->isobuf[i].urb;
if (urb != 0) {
if (dev->isoc_init_ok)
usb_kill_urb(urb);
usb_free_urb(urb);
dev->isobuf[i].urb = NULL;
}
}
// All is done
dev->isoc_init_ok = 0;
}
/**
* @param dev Device structure
* @param index Choice of the interface
*
* @returns 0 if all is OK
*
* @brief Send the message SET_FEATURE and choose the interface
*
* This function permits to send the message SET_FEATURE on the USB bus.
*/
int usb_stk11xx_set_feature(struct usb_stk11xx *dev, int index)
{
int result;
struct usb_device *udev = dev->udev;
result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
USB_REQ_SET_FEATURE,
USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
USB_DEVICE_REMOTE_WAKEUP,
index,
NULL,
0,
500);
if (result < 0)
STK_ERROR("SET FEATURE fail !\n");
else
STK_DEBUG("SET FEATURE\n");
return result;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief Send the message SET_CONFIGURATION
*
* This function permits to send the message SET_CONFIGURATION on the USB bus.
*/
int usb_stk11xx_set_configuration(struct usb_stk11xx *dev)
{
int result;
struct usb_device *udev = dev->udev;
result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
USB_REQ_SET_CONFIGURATION,
USB_TYPE_STANDARD | USB_DIR_OUT | USB_RECIP_DEVICE,
0,
udev->config[0].desc.bConfigurationValue,
NULL,
0,
500);
if (result < 0)
STK_ERROR("SET CONFIGURATION fail !\n");
else
STK_DEBUG("SET CONFIGURATION %d\n", udev->config[0].desc.bConfigurationValue);
return result;
}
/**
* @param dev
* @param index
* @param value
*
* @returns 0 if all is OK
*
* @brief Write a 16-bits value to a 16-bits register
*
* This function permits to write a 16-bits value to a 16-bits register on the USB bus.
*/
int usb_stk11xx_write_registry(struct usb_stk11xx *dev, __u16 index, __u16 value)
{
int result;
struct usb_device *udev = dev->udev;
result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0x01,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
value,
index,
NULL,
0,
500);
if (result < 0)
STK_ERROR("Write registry fails %02X = %02X", index, value);
return result;
}
/**
* @param dev
* @param index
* @param value
*
* @returns 0 if all is OK
*
* @brief Read a 16-bits value from a 16-bits register
*
* This function permits to read a 16-bits value from a 16-bits register on the USB bus.
*/
int usb_stk11xx_read_registry(struct usb_stk11xx *dev, __u16 index, int *value)
{
int result;
struct usb_device *udev = dev->udev;
*value = 0;
result = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
0x00,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0x00,
index,
(__u8 *) value,
sizeof(__u8),
500);
if (result < 0)
STK_ERROR("Read registry fails %02X", index);
return result;
}
/**
* @param dev
*
* @returns 0 if all is OK
*
* @brief Set the default value about the video settings.
*
* This function permits to set the video settings for each video camera model.
*
*/
static int usb_stk11xx_default_settings(struct usb_stk11xx *dev)
{
switch (dev->webcam_model) {
case SYNTEK_STK_0408:
dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
dev->vsettings.norm = (default_norm == -1) ? 0 : default_norm;
dev->vsettings.input = 0;
break;
case SYNTEK_STK_M811:
case SYNTEK_STK_A311:
dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
dev->vsettings.vflip = (default_vflip == -1) ? 1 : default_vflip;
dev->vsettings.hflip = (default_hflip == -1) ? 1 : default_hflip;
dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
break;
case SYNTEK_STK_A821:
case SYNTEK_STK_AA11:
dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
break;
case SYNTEK_STK_6A31:
case SYNTEK_STK_6A33:
case SYNTEK_STK_0500:
dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
break;
case SYNTEK_STK_6A51:
case SYNTEK_STK_6D51:
case SYNTEK_STK_6A54:
dev->vsettings.fps = (default_fps == -1) ? 25 : default_fps;
dev->vsettings.vflip = (default_vflip == -1) ? 0 : default_vflip;
dev->vsettings.hflip = (default_hflip == -1) ? 0 : default_hflip;
dev->vsettings.brightness = (default_brightness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_brightness;
dev->vsettings.whiteness = (default_whiteness == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_whiteness;
dev->vsettings.contrast = (default_contrast == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_contrast;
dev->vsettings.colour = (default_colour == -1) ? STK11XX_PERCENT(50, 0xFFFF) : default_colour;
break;
default:
return -1;
}
dev->vsettings.default_brightness = dev->vsettings.brightness;
dev->vsettings.default_whiteness = dev->vsettings.whiteness;
dev->vsettings.default_contrast = dev->vsettings.contrast;
dev->vsettings.default_colour = dev->vsettings.colour;
dev->vsettings.default_hflip = dev->vsettings.hflip;
dev->vsettings.default_vflip = dev->vsettings.vflip;
return 0;
}
/**
* @param interface
* @param id
*
* @returns 0 if all is OK
*
* @brief Load the driver
*
* This function detects the device and allocate the buffers for the device
* and the video interface.
*/
static int usb_stk11xx_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
int i;
int err;
size_t buffer_size;
int vendor_id;
int product_id;
int bNumInterfaces;
int webcam_model;
int webcam_type;
struct usb_stk11xx *dev = NULL;
struct usb_device *udev = interface_to_usbdev(interface);
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint;
// Get USB VendorID and ProductID
vendor_id = le16_to_cpu(udev->descriptor.idVendor);
product_id = le16_to_cpu(udev->descriptor.idProduct);
// Check if we can handle this device
STK_DEBUG("Probe function called with VendorID=%04X, ProductID=%04X and InterfaceNumber=%d\n",
vendor_id, product_id, interface->cur_altsetting->desc.bInterfaceNumber);
// The interface are probed one by one.
// We are interested in the video interface (always the interface '0')
// The interfaces '1' or '2' (if presents) are the audio control.
if (interface->cur_altsetting->desc.bInterfaceNumber > 0)
return -ENODEV;
// Detect device
if (vendor_id == USB_SYNTEK1_VENDOR_ID) {
switch (product_id) {
case USB_STK_A311_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0xA311.\n");
webcam_model = SYNTEK_STK_A311;
webcam_type = STK11XX_SXGA;
break;
case USB_STK_A821_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 VGA WebCam - Product ID 0xA821.\n");
webcam_model = SYNTEK_STK_A821;
webcam_type = STK11XX_VGA;
break;
case USB_STK_AA11_PRODUCT_ID:
STK_INFO("Syntek AVStream USB2.0 VGA WebCam - Product ID 0xAA11.\n");
STK_INFO("Using code for Product ID AxA821\n");
webcam_model = SYNTEK_STK_AA11;
webcam_type = STK11XX_VGA;
break;
case USB_STK_6A31_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A31.\n");
webcam_model = SYNTEK_STK_6A31;
webcam_type = STK11XX_VGA;
break;
case USB_STK_6A33_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A33.\n");
webcam_model = SYNTEK_STK_6A33;
webcam_type = STK11XX_VGA;
break;
case USB_STK_6A51_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A51.\n");
webcam_model = SYNTEK_STK_6A51;
webcam_type = STK11XX_VGA;
break;
case USB_STK_6A54_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6A54.\n");
webcam_model = SYNTEK_STK_6A54;
webcam_type = STK11XX_VGA;
break;
case USB_STK_6D51_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x6D51.\n");
webcam_model = SYNTEK_STK_6D51;
webcam_type = STK11XX_VGA;
break;
default:
STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
le16_to_cpu(udev->descriptor.idProduct));
return -ENODEV;
}
}
else if (vendor_id == USB_SYNTEK2_VENDOR_ID) {
switch (product_id) {
case USB_STK_0408_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1160 based device found.\n");
STK_INFO("Syntek AVStream USB2.0 Video Capture - Product ID 0x0408.\n");
webcam_model = SYNTEK_STK_0408;
webcam_type = STK11XX_PAL;
break;
case USB_STK_0500_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x0500.\n");
webcam_model = SYNTEK_STK_0500;
webcam_type = STK11XX_VGA;
break;
case USB_STK_0501_PRODUCT_ID:
STK_INFO("Syntek USB2.0 - STK-1135 based webcam found.\n");
STK_INFO("Syntek AVStream USB2.0 1.3M WebCam - Product ID 0x0501.\n");
webcam_model = SYNTEK_STK_M811;
webcam_type = STK11XX_SXGA;
break;
default:
STK_ERROR("usb_stk11xx_probe failed ! Camera product 0x%04X is not supported.\n",
le16_to_cpu(udev->descriptor.idProduct));
return -ENODEV;
}
}
else
return -ENODEV;
// Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device
dev = kzalloc(sizeof(struct usb_stk11xx), GFP_KERNEL);
if (dev == NULL) {
STK_ERROR("Out of memory !\n");
return -ENOMEM;
}
// Init mutexes, spinlock, etc.
#ifndef init_MUTEX
sema_init(&dev->mutex,1);
#else
init_MUTEX(&dev->mutex);
#endif
mutex_init(&dev->modlock);
spin_lock_init(&dev->spinlock);
init_waitqueue_head(&dev->wait_frame);
// Save pointers
dev->webcam_model = webcam_model;
dev->webcam_type = webcam_type;
dev->udev = udev;
dev->interface = interface;
// Read the product release
dev->release = le16_to_cpu(udev->descriptor.bcdDevice);
STK_INFO("Release: %04x\n", dev->release);
// How many interfaces (1 or 3) ?
bNumInterfaces = udev->config->desc.bNumInterfaces;
STK_INFO("Number of interfaces : %d\n", bNumInterfaces);
// Constructor
// 2 : enough for webcam
// 3 : for easycap... it's usefull ?
dev->nbuffers = 2;
// dev->nbuffers = 3;
dev->len_per_image = PAGE_ALIGN(STK11XX_FRAME_SIZE);
// Switch on the camera (to detect size of buffers)
dev_stk11xx_camera_on(dev);
// Set up the endpoint information
// use only the first int-in and isoc-in endpoints
// for the current alternate setting
iface_desc = interface->cur_altsetting;
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
if (!dev->int_in_endpointAddr
&& ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
&& ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
// we found an interrupt in endpoint
buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
dev->int_in_size = buffer_size;
dev->int_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
}
if (!dev->isoc_in_endpointAddr
&& ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
&& ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
// we found an isoc in endpoint
buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
dev->isoc_in_size = buffer_size;
dev->isoc_in_endpointAddr = (endpoint->bEndpointAddress & 0xf);
}
}
if (!(dev->int_in_endpointAddr && dev->isoc_in_endpointAddr)) {
STK_ERROR("Could not find both int-in and isoc-in endpoints");
kfree(dev);
return -ENODEV;
}
// Switch off camera
dev_stk11xx_camera_off(dev);
// Initialize the video device
dev->vdev = video_device_alloc();
if (!dev->vdev) {
kfree(dev);
return -ENOMEM;
}
// Initialize the camera
dev_stk11xx_initialize_device(dev);
// Register the video device
err = v4l_stk11xx_register_video_device(dev);
if (err) {
kfree(dev);
return err;
}
// Create the entries in the sys filesystem
stk11xx_create_sysfs_files(dev->vdev);
// Save our data pointer in this interface device
usb_set_intfdata(interface, dev);
// Default settings video device
usb_stk11xx_default_settings(dev);
// Enable power management feature
// usb_autopm_enable(dev->interface);
return 0;
}
/**
* @param interface
*
* @brief This function is called when the device is disconnected
* or when the kernel module is unloaded.
*/
static void usb_stk11xx_disconnect(struct usb_interface *interface)
{
struct usb_stk11xx *dev = usb_get_intfdata(interface);
STK_INFO("Syntek USB2.0 Camera disconnected\n");
// We got unplugged; this is signalled by an EPIPE error code
if (dev->vopen) {
STK_INFO("Disconnected while webcam is in use !\n");
dev->error_status = EPIPE;
}
// Alert waiting processes
wake_up_interruptible(&dev->wait_frame);
// Wait until device is closed
while (dev->vopen)
schedule();
// Remove the entries in the sys filesystem
stk11xx_remove_sysfs_files(dev->vdev);
// Unregister the video device
v4l_stk11xx_unregister_video_device(dev);
usb_set_intfdata(interface, NULL);
kfree(dev);
}