forked from hbars/stk11xx-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stk11xx-dev-0408.c
1173 lines (963 loc) · 27.6 KB
/
stk11xx-dev-0408.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-dev-0408.c
* @author Ivor Hewitt
* @date 2009-01-01
* @version v2.2.x
*
* @brief Driver for Syntek USB video camera
*
* @note Copyright (C) Nicolas VIVIEN
* Copyright (C) Ivor Hewitt
*
* @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$
*/
/*
* note currently only supporting 720x576, 704x576 and 640x480 PAL
* other resolutions should work but aren't
*/
#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/usb.h>
#include <media/v4l2-common.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
#include <media/v4l2-ioctl.h>
#endif
#include "stk11xx.h"
#include "stk11xx-dev.h"
int dev_stk0408_check_device(struct usb_stk11xx *dev);
int dev_stk0408_select_input(struct usb_stk11xx *dev, int input);
int dev_stk0408_write0(struct usb_stk11xx *dev, int mask, int val);
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function initializes the device.
*
* This function must be called at first. It's the start of the
* initialization process. After this process, the device is
* completly initalized and it's ready.
*
* This function is written from the USB log.
*/
int dev_stk0408_initialize_device(struct usb_stk11xx *dev)
{
int i;
int retok;
int value;
STK_INFO("Initialize USB2.0 Syntek Capture device\n");
//what is all this writing to register 2 doing?
usb_stk11xx_write_registry(dev, 0x0002, 0x0000);
usb_stk11xx_write_registry(dev, 0x0000, 0x0000);
usb_stk11xx_write_registry(dev, 0x0002, 0x0000);
usb_stk11xx_write_registry(dev, 0x0003, 0x0000);
usb_stk11xx_write_registry(dev, 0x0002, 0x0007);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value);
dev_stk0408_write0(dev, 7, 4);
dev_stk0408_write0(dev, 7, 4);
dev_stk0408_write0(dev, 7, 6);
dev_stk0408_write0(dev, 7, 7);
dev_stk0408_write0(dev, 7, 6);
dev_stk0408_write0(dev, 7, 4);
dev_stk0408_write0(dev, 7, 5);
for (i=0;i<7;i++)
{
dev_stk0408_write0(dev, 7, 4);
dev_stk0408_write0(dev, 7, 4);
dev_stk0408_write0(dev, 7, 5);
}
/* start set */
usb_stk11xx_write_registry(dev, 0x0002, 0x0007);
usb_stk11xx_write_registry(dev, 0x0000, 0x0001);
dev_stk0408_configure_device(dev,1);
dev_stk0408_configure_device(dev,2);
usb_stk11xx_write_registry(dev, 0x0500, 0x0094);
msleep(10);
dev_stk0408_camera_asleep(dev);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
usb_stk11xx_write_registry(dev, 0x0000, 0x0000);
usb_stk11xx_write_registry(dev, 0x0203, 0x00a0);
usb_stk11xx_read_registry(dev, 0x0003, &value);
usb_stk11xx_write_registry(dev, 0x0003, 0x0000);
usb_stk11xx_read_registry(dev, 0x0002, &value); //78?
usb_stk11xx_write_registry(dev, 0x0002, 0x007f);
usb_stk11xx_read_registry(dev, 0x0002, &value); //7f?
usb_stk11xx_read_registry(dev, 0x0000, &value); //0?
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x007);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x007);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x007);
dev_stk0408_write0(dev, 0x07f, 0x006);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x004);
dev_stk0408_write0(dev, 0x07f, 0x005);
usb_stk11xx_write_registry(dev, 0x0002, 0x007f);
usb_stk11xx_write_registry(dev, 0x0000, 0x0001);
retok = dev_stk11xx_check_device(dev, 500);
usb_stk11xx_set_feature(dev, 1);
// Device is initialized and is ready !!!
STK_INFO("Syntek USB2.0 Capture device is ready\n");
return 0;
}
int dev_stk0408_write0(struct usb_stk11xx *dev, int mask, int val)
{
int value;
usb_stk11xx_write_registry(dev, 0x0002, mask);
usb_stk11xx_write_registry(dev, 0x0000, val);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value);
return 0;
}
int dev_stk0408_write_208(struct usb_stk11xx *dev, int val)
{
int value;
int retok;
usb_stk11xx_read_registry(dev, 0x02ff, &value);
usb_stk11xx_write_registry(dev, 0x02ff, 0x0000);
usb_stk11xx_write_registry(dev, 0x0208, val);
usb_stk11xx_write_registry(dev, 0x0200, 0x0020);
retok = dev_stk0408_check_device(dev);
if (retok != 1) {
return -1;
}
usb_stk11xx_read_registry(dev, 0x0209, &value);
usb_stk11xx_write_registry(dev, 0x02ff, 0x0000);
return 1;
}
int dev_stk0408_write_saa(struct usb_stk11xx *dev, int reg, int val)
{
int value;
int retok;
usb_stk11xx_read_registry(dev, 0x02ff, &value);
usb_stk11xx_write_registry(dev, 0x02ff, 0x0000);
usb_stk11xx_write_registry(dev, 0x0204, reg);
usb_stk11xx_write_registry(dev, 0x0205, val);
usb_stk11xx_write_registry(dev, 0x0200, 0x0001);
retok = dev_stk0408_check_device(dev);
if (retok != 1) {
return -1;
}
usb_stk11xx_write_registry(dev, 0x02ff, 0x0000);
return 1;
}
int dev_stk0408_set_resolution(struct usb_stk11xx *dev)
{
/*
* These registers control the resolution of the capture buffer.
*
* xres = (X - xsub) / 2
* yres = (Y - ysub)
*
*/
int x,y,xsub,ysub;
// RRK, need to return for NTSC ?
if (dev->vsettings.norm == 0)
return 0;
switch (stk11xx_image_sizes[dev->resolution].x)
{
case 720:
x = 0x5a0;
xsub = 0;
break;
case 704:
case 352:
case 176:
x = 0x584;
xsub = 4;
break;
case 640:
case 320:
case 160:
x = 0x508;
xsub = 0x08;
break;
default:
return -1;
}
switch (stk11xx_image_sizes[dev->resolution].y)
{
case 576:
case 288:
case 144:
y = 0x121;
ysub = 0x1;
break;
case 480:
y = 0x110;
ysub= 0x20;
break;
case 120:
case 240:
y = 0x103;
ysub = 0x13;
break;
default:
return -1;
}
usb_stk11xx_write_registry(dev, 0x0110, xsub ); // xsub
usb_stk11xx_write_registry(dev, 0x0111, 0 );
usb_stk11xx_write_registry(dev, 0x0112, ysub ); // ysub
usb_stk11xx_write_registry(dev, 0x0113, 0 );
usb_stk11xx_write_registry(dev, 0x0114, x ); // X
usb_stk11xx_write_registry(dev, 0x0115, 5 );
usb_stk11xx_write_registry(dev, 0x0116, y ); // Y
usb_stk11xx_write_registry(dev, 0x0117, 1 );
return 0;
}
/**
* @param dev Device structure
* @param step The step of configuration [0-6]
*
* @returns 0 if all is OK
*
* @brief This function configures the device.
*
* This is called multiple times through intitialisation and configuration
* there appear to be six distinct steps
*
*/
int dev_stk0408_configure_device(struct usb_stk11xx *dev, int step)
{
int value;
int asize;
int i;
static const int ids[] = {
0x203,0x00d,0x00f,0x103,0x018,0x01b,0x01c,0x01a,0x019,
0x300,0x350,0x351,0x352,0x353,0x300,0x018,0x202,0x110,
0x111,0x112,0x113,0x114,0x115,0x116,0x117
};
const int values[] = {
0x04a,0x000,0x002,0x000,0x000,0x00e,0x046,0x014,0x000,
0x012,0x02d,0x001,0x000,0x000,0x080,0x010,0x00f,
(dev->vsettings.norm ? 0x008 : 0x038),
0x000,
(dev->vsettings.norm ? 0x013 : 0x003),
0x000,
(dev->vsettings.norm ? 0x008 : 0x038),
0x005,
(dev->vsettings.norm ? 0x003 : 0x0f3),
(dev->vsettings.norm ? 0x001 : 0x000)
};
if (step != 1)
{
usb_stk11xx_read_registry(dev, 0x0003, &value);
usb_stk11xx_read_registry(dev, 0x0001, &value);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value);
usb_stk11xx_read_registry(dev, 0x0003, &value);
usb_stk11xx_read_registry(dev, 0x0001, &value);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
usb_stk11xx_write_registry(dev, 0x0000, 0x0000);
usb_stk11xx_write_registry(dev, 0x0003, 0x0080);
usb_stk11xx_write_registry(dev, 0x0001, 0x0003);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
usb_stk11xx_read_registry(dev, 0x0000, &value);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
usb_stk11xx_write_registry(dev, 0x0000, 0x0030);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
}
asize = ARRAY_SIZE(values);
for(i=0; i<asize; i++) {
usb_stk11xx_write_registry(dev, ids[i], values[i]);
}
if (step == 1)
{
usb_stk11xx_read_registry(dev, 0x0100, &value);
usb_stk11xx_write_registry(dev, 0x0100, 0x0000);
}
else
{
usb_stk11xx_read_registry(dev, 0x0100, &value);
usb_stk11xx_write_registry(dev, 0x0100, 0x0033);
}
if (step <=2 )
{
return 0;
}
if (step==3)
{
dev_stk0408_sensor_settings(dev);
}
usb_stk11xx_read_registry(dev, 0x0100, &value);
usb_stk11xx_write_registry(dev, 0x0100, 0x0033);
usb_stk11xx_write_registry(dev, 0x0103, 0x0000);
usb_stk11xx_write_registry(dev, 0x0100, 0x0033);
switch (step)
{
case 3: /* all fine */
usb_stk11xx_write_registry(dev, 0x0104, 0x0000);
usb_stk11xx_write_registry(dev, 0x0105, 0x0000);
usb_stk11xx_write_registry(dev, 0x0106, 0x0000);
dev_stk11xx_camera_off(dev);
usb_stk11xx_write_registry(dev, 0x0500, 0x0094);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0506, 0x0001);
usb_stk11xx_write_registry(dev, 0x0507, 0x0000);
break;
case 5:
/* if ((dev->resolution == STK11XX_320x240)||
(dev->resolution == STK11XX_352x288))
{
usb_stk11xx_write_registry(dev, 0x0104, 0x0000);
usb_stk11xx_write_registry(dev, 0x0105, 0x0000);
} */
usb_stk11xx_write_registry(dev, 0x0106, 0x0000);
dev_stk0408_write_saa(dev, 0x02, 0x80);
dev_stk0408_write_208(dev,0x09);
dev_stk0408_write_saa(dev, 0x09, 0x00);
break;
}
if (step == 3)
{
dev_stk0408_write_saa(dev, 0x02, 0x80);
dev_stk0408_write_208(dev,0x09);
dev_stk0408_write_saa(dev, 0x09, 0x00);
//test and set?
usb_stk11xx_write_registry(dev, 0x0504, 0x0012);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0012);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0080);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x0010);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0010);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0000);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x000e);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x000e);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0000);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x0016);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0016);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0000);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x001a);
usb_stk11xx_write_registry(dev, 0x0502, 0x0004);
usb_stk11xx_write_registry(dev, 0x0503, 0x0004);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0080);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0080);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
dev_stk0408_write_saa(dev, 0x02, 0x80);
dev_stk0408_write_208(dev,0x09);
dev_stk0408_write_saa(dev, 0x09, 0x00);
}
if ((step == 4 )|| (step == 6))
{
dev_stk0408_write_saa(dev, 0x02, 0x80);
dev_stk0408_write_208(dev,0x09);
dev_stk0408_write_saa(dev, 0x09, 0x00);
dev_stk0408_write_208(dev,0x0e);
dev_stk0408_write_saa(dev, 0x0e, 0x01);
dev_stk0408_set_resolution(dev);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
dev_stk0408_set_camera_quality(dev);
}
if (step == 6)
{
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
dev_stk0408_write_208(dev,0x0e);
dev_stk0408_write_saa(dev, 0x0e, 0x01);
dev_stk0408_set_resolution( dev);
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
dev_stk0408_select_input(dev, dev->vsettings.input);
dev_stk0408_start_stream(dev);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0080);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0080);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x0002);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0000);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0500, 0x008b);
usb_stk11xx_write_registry(dev, 0x0504, 0x001c);
usb_stk11xx_write_registry(dev, 0x0502, 0x0000);
usb_stk11xx_write_registry(dev, 0x0503, 0x0000);
usb_stk11xx_write_registry(dev, 0x0500, 0x008c);
dev_stk0408_start_stream(dev);
}
if (step==4)
{
dev_stk11xx_camera_on(dev);
}
return 0;
}
int dev_stk0408_select_input(struct usb_stk11xx *dev, int input)
{
switch (input)
{
case 1:
usb_stk11xx_write_registry(dev, 0x0000, 0x0098);
break;
case 2:
usb_stk11xx_write_registry(dev, 0x0000, 0x0090);
break;
case 3:
usb_stk11xx_write_registry(dev, 0x0000, 0x0088);
break;
case 4:
usb_stk11xx_write_registry(dev, 0x0000, 0x0080);
break;
}
usb_stk11xx_write_registry(dev, 0x0002, 0x0093);
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief Wake-up the camera.
*
* This function permits to wake-up the device.
*/
int dev_stk0408_camera_asleep(struct usb_stk11xx *dev)
{
int value;
int value0;
usb_stk11xx_read_registry(dev, 0x0104, &value);
usb_stk11xx_read_registry(dev, 0x0105, &value);
usb_stk11xx_read_registry(dev, 0x0106, &value);
usb_stk11xx_read_registry(dev, 0x0100, &value);
value = value & 0x7f;
usb_stk11xx_write_registry(dev, 0x0100, value);
usb_stk11xx_write_registry(dev, 0x0116, 0x0000);
usb_stk11xx_write_registry(dev, 0x0117, 0x0000);
usb_stk11xx_write_registry(dev, 0x0018, 0x0000);
usb_stk11xx_read_registry(dev, 0x0002, &value);
usb_stk11xx_read_registry(dev, 0x0000, &value0);
usb_stk11xx_write_registry(dev, 0x0002, value);
usb_stk11xx_read_registry(dev, 0x0000, &value0);
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function initializes the device for the stream.
*
* It's the start. This function has to be called at first, before
* enabling the video stream.
*/
int dev_stk0408_init_camera(struct usb_stk11xx *dev)
{
dev_stk0408_camera_asleep(dev);
dev_stk0408_configure_device(dev, 3);
dev_stk0408_configure_device(dev, 4);
dev_stk0408_configure_device(dev, 5);
dev_stk0408_configure_device(dev, 6);
return 0;
}
int dev_stk0408_check_device(struct usb_stk11xx *dev)
{
int i;
int value;
const int retry=2;
for (i=0; i < retry; i++) {
usb_stk11xx_read_registry(dev, 0x201, &value);
//writes to 204/204 return 4 on success
//writes to 208 return 1 on success
if (value == 0x04 || value == 0x01)
return 1;
if (value != 0x00)
{
STK_ERROR("Check device return error (0x0201 = %02X) !\n", value);
return -1;
}
// msleep(10);
}
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function sets the default sensor settings
*
* We set some registers in using a I2C bus.
* WARNING, the sensor settings can be different following the situation.
*/
int dev_stk0408_sensor_settings(struct usb_stk11xx *dev)
{
int i;
int retok;
int asize;
// PAL registers
static const int registers[] = {
0x01,0x03,0x04,0x05,0x06,0x07,0x08,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,
0x13,0x15,0x16,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,
0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b };
const int values[] = {
0x08,0x33,0x00,0x00,0xe9,0x0d,
(dev->vsettings.norm ? 0x38 : 0x78),
0x80,0x47,0x40,0x00,0x01,0x2a,0x00,0x0c,0xe7,
0x00,0x00,0x00,0x02,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x55,0xff,0xff,0xff,0x40,0x54,
(dev->vsettings.norm ? 0x07 : 0x0a),
0x83 };
asize = ARRAY_SIZE(values);
for(i=0; i<asize; i++) {
retok = dev_stk0408_write_saa(dev, registers[i], values[i]);
if (retok != 1) {
STK_ERROR("Load default sensor settings fail !\n");
return -1;
}
}
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
' * @brief This function permits to modify the settings of the camera.
*
* This functions permits to modify the settings :
* - brightness
* - contrast
* - white balance
* - ...
*/
int dev_stk0408_camera_settings(struct usb_stk11xx *dev)
{
dev_stk0408_set_camera_quality(dev);
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function permits to modify the settings of the camera.
*
*/
int dev_stk0408_set_camera_quality(struct usb_stk11xx *dev)
{
usb_stk11xx_write_registry(dev, 0x0002, 0x0078);
//brightness
dev_stk0408_write_saa(dev, 0x0a, dev->vsettings.brightness >> 8); //80
//contrast
dev_stk0408_write_saa(dev, 0x0b, dev->vsettings.contrast >> 9); //40
//hue
dev_stk0408_write_saa(dev, 0x0d, (dev->vsettings.colour - 32768) >> 8); //00
//saturation
dev_stk0408_write_saa(dev, 0x0c, (dev->vsettings.hue) >> 9); //40
STK_DEBUG("Set colour : %d\n", dev->vsettings.colour);
STK_DEBUG("Set contrast : %d\n", dev->vsettings.contrast);
STK_DEBUG("Set hue : %d\n", dev->vsettings.hue);
STK_DEBUG("Set brightness : %d\n", dev->vsettings.brightness);
return 1;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function permits to modify the settings of the camera.
*
* This functions permits to modify the frame rate per second.
*
*/
int dev_stk0408_set_camera_fps(struct usb_stk11xx *dev)
{
//Unknown, setting FPS seems to have no effect
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function sets the device to start the stream.
*
* After the initialization of the device and the initialization of the video stream,
* this function permits to enable the stream.
*/
int dev_stk0408_start_stream(struct usb_stk11xx *dev)
{
int value;
int value_116, value_117;
usb_stk11xx_read_registry(dev, 0x0116, &value_116);
usb_stk11xx_read_registry(dev, 0x0117, &value_117);
usb_stk11xx_write_registry(dev, 0x0116, 0x0000);
usb_stk11xx_write_registry(dev, 0x0117, 0x0000);
usb_stk11xx_read_registry(dev, 0x0100, &value);
value |= 0x80;
// msleep(0x1f4);
usb_stk11xx_write_registry(dev, 0x0100, value);
// msleep(0x64);
usb_stk11xx_write_registry(dev, 0x0116, value_116);
usb_stk11xx_write_registry(dev, 0x0117, value_117);
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief Reconfigure the camera before the stream.
*
* Before enabling the video stream, you have to reconfigure the device.
*/
int dev_stk0408_reconf_camera(struct usb_stk11xx *dev)
{
dev_stk0408_configure_device(dev, 6);
dev_stk11xx_camera_settings(dev);
return 0;
}
/**
* @param dev Device structure
*
* @returns 0 if all is OK
*
* @brief This function sets the device to stop the stream.
*
* You use the function start_stream to enable the video stream. So you
* have to use the function stop_strem to disable the video stream.
*/
int dev_stk0408_stop_stream(struct usb_stk11xx *dev)
{
int value;
usb_stk11xx_read_registry(dev, 0x0100, &value);
value &= 0x7f;
usb_stk11xx_write_registry(dev, 0x0100, value);
msleep(5);
return 0;
}
/*
* Needs some more work and optimisation!
*/
void stk11xx_copy_uvyv(uint8_t *src, uint8_t *rgb,
struct stk11xx_coord *image,
struct stk11xx_coord *view,
const int hflip, const int vflip,
const int hfactor, const int vfactor,
bool order, bool field)
{
int width = image->x;
int height = image->y;
int x;
int y;
uint8_t *line1 = NULL;
uint8_t *line2 = NULL;
static uint8_t *prev=0;
if (!prev)
prev = rgb;
// printk("copy image %d - %d %d,%d,%d\n", width, height, hfactor, vfactor, field);
// vfactor=1 interlace rows
// vfactor=2 full frame copy, duplicate rows
// vfactor=4 half frame, copy rows
if (field == false) // odd frame
{
prev += width * 2;
}
for ( y=0; y < height/2; y++)
{
if (vfactor == 1)
{
if (field == false) // odd frame
{
line1 = rgb + (y*width*4);
line2 = rgb + (y*width*4) + width*2;
}
else
{
line1 = rgb + (y*width*4) + width*2;
line2 = rgb + (y*width*4);
}
}
else
{
line1 = rgb + (y*width*2);
}
if (order && hfactor == 1) //fast line copy with memcpy
{
memcpy(line1,src,width*2);
src += width*2;
}
else //slow line copy with hscaling or YUV reorder
{
for ( x = 0; x < width*2; x+=4)
{
if (order) //yuv order
{
line1[x] = src[0];
line1[x+1] = src[1];
line1[x+2] = src[2];
line1[x+3] = src[3];
}
else
{
line1[x] = src[1];
line1[x+1] = src[0];
line1[x+2] = src[3];
line1[x+3] = src[2];
}
src += (4 * hfactor);
}
}
if (vfactor == 1) //interlaced copy from previous frame
{
memcpy(line2,prev,width*2);
prev += width*4;
}
else if (vfactor == 2) //1 : 1
{
}
else if (vfactor == 4) // 2 : 1
{
src += (width*2)*2;
}
}
prev = rgb;
}
/*
* needs more work and optimisation!
*
* rgb is horribly slow but just written to check the image is working
* replace with a proper yuv to rgb conversion
*/
#define CLAMP(x) x < 0 ? 0 : x > 255 ? 255 : x
void stk11xx_copy_rgb(uint8_t *src, uint8_t *rgb,
struct stk11xx_coord *image,
struct stk11xx_coord *view,
const int hflip, const int vflip,
const int hfactor, const int vfactor,
bool order, bool four, bool field)
{
int width = image->x;
int height = image->y;
int x;
int y;
int step;
bool off = false;
uint8_t *line1 = NULL;
uint8_t *line2 = NULL;
static uint8_t *prev=0;
if (!prev)
prev = rgb;
step = four?4:3;