-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlibrtlsdr.c
3294 lines (2733 loc) · 93.6 KB
/
librtlsdr.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
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* Copyright (C) 2012-2014 by Steve Markgraf <[email protected]>
* Copyright (C) 2012 by Dimitri Stolnikov <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* Copied from the original librtlsdr.c at:
* https://github.com/old-dab/rtlsdr/blob/master/src/librtlsdr.c
*
* and:
* 1. Removed all non-WIN32 code.
* 2. Faking some Pthread calls using Win-SDK function.
* 3. Changed the code-style using Astyle.
*
* Hence nothing here depends on the libusb library.
*/
#include <windows.h>
#include <winusb.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <process.h>
#include <malloc.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include "rtl-sdr.h"
#include "tuner_e4k.h"
#include "tuner_fc001x.h"
#include "tuner_fc2580.h"
#include "tuner_r82xx.h"
#include "version.h"
#include "trace.h"
#define WINUSB_REQUEST_TYPE_VENDOR (0x02 << 5)
#define WINUSB_ENDPOINT_IN 0x80
#define WINUSB_ENDPOINT_OUT 0x00
#define CTRL_IN (WINUSB_REQUEST_TYPE_VENDOR | WINUSB_ENDPOINT_IN)
#define CTRL_OUT (WINUSB_REQUEST_TYPE_VENDOR | WINUSB_ENDPOINT_OUT)
/* two raised to the power of n
*/
#define TWO_POW(n) ((double)(1ULL<<(n)))
#define EP_RX 0x81
#define RTLSDR_READ_ARRAY(dev, index, addr, array, len) \
usb_control_transfer (dev, CTRL_IN, addr, index, array, len, __LINE__)
#define RTLSDR_WRITE_ARRAY(dev, index, addr, array, len) \
usb_control_transfer (dev, CTRL_OUT, addr, index | 0x10, array, len, __LINE__)
typedef struct rtlsdr_tuner_iface {
/* tuner interface */
int (*init) (void *);
int (*exit) (void *);
int (*set_freq) (void *, uint32_t freq /* Hz */);
int (*set_bw) (void *dev,
int bw /* Hz */,
uint32_t *applied_bw /* configured bw in Hz */,
int apply /* 1 == configure it!, 0 == deliver applied_bw */);
int (*set_gain_index) (void *, unsigned int index);
int (*set_if_gain) (void *, int stage, int gain /* tenth dB */);
int (*set_gain_mode) (void *, int manual);
int (*set_i2c_register) (void *,
unsigned i2c_register,
unsigned data /* byte */,
unsigned mask /* byte */);
int (*get_i2c_register) (void *, uint8_t *data, int *len, int *strength);
int (*set_sideband) (void *, int sideband);
const int * (*get_gains) (int *len);
} rtlsdr_tuner_iface_t;
enum rtlsdr_async_status {
RTLSDR_INACTIVE = 0,
RTLSDR_CANCELING,
RTLSDR_RUNNING
};
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define FIR_LEN 16
/*
* FIR coefficients.
*
* The filter is running at XTal frequency. It is symmetric filter with 32
* coefficients. Only first 16 coefficients are specified, the other 16
* use the same values but in reversed order. The first coefficient in
* the array is the outer one, the last is the inner one.
* First 8 coefficients are 8 bit signed integers, the next 8 coefficients
* are 12 bit signed integers. All coefficients have the same weight.
*
* Default FIR coefficients used for DAB/FM by the Windows driver,
* the DVB driver uses different ones
*/
static const int fir_default [][FIR_LEN] = {
/* 1.2 MHz */
{
-54, -36, -41, -40, -32, -14, 14, 53, /* 8 bit signed */
101, 156, 215, 273, 327, 372, 404, 421 /* 12 bit signed */
},
/* 770 kHz */
{
-44, -30, -12, 10, 35, 62, 91, 121,
151, 181, 208, 232, 252, 268, 279, 285
},
};
static const int fir_bw [] = { 2400, 1500, 300 };
static int cal_imr = 0;
static bool got_device_usb_product [10];
enum softagc_mode {
SOFTAGC_OFF = 0, /* off */
SOFTAGC_ON /* operate full time - attenuate and gain */
};
struct softagc_state {
uintptr_t command_thread;
volatile int exit_command_thread;
volatile int command_newGain;
volatile int command_changeGain;
enum softagc_mode softAgcMode;
};
struct rtlsdr_dev {
WINUSB_INTERFACE_HANDLE usbHandle;
HANDLE deviceHandle;
enum rtlsdr_async_status async_status;
int async_cancel;
uint32_t index;
/* rtl demod context */
uint32_t rate; /* Hz */
uint32_t rtl_xtal; /* Hz */
int fir;
int direct_sampling;
/* tuner context */
enum rtlsdr_tuner tuner_type;
rtlsdr_tuner_iface_t *tuner;
uint32_t tun_xtal; /* Hz */
uint32_t freq; /* Hz */
uint32_t bw;
uint32_t offs_freq; /* Hz */
int corr; /* ppb */
int gain; /* tenth dB */
unsigned int gain_index;
unsigned int gain_count;
int gain_mode;
int gains [24];
enum rtlsdr_ds_mode direct_sampling_mode;
uint32_t direct_sampling_threshold; /* Hz */
struct e4k_state e4k_s;
struct r82xx_config r82xx_c;
struct r82xx_priv r82xx_p;
enum rtlsdr_demod slave_demod;
struct softagc_state softagc;
/* Concurrent lock for the periodic reading of I2C registers
*/
CRITICAL_SECTION cs_mutex;
/* status */
int rc_active;
int opening;
int verbose;
int agc_mode;
char manufact [256];
char product [256];
/* transfer buffers */
uint8_t **xfer_buf;
OVERLAPPED **overlapped;
uint32_t num_xfer_buf;
};
static int rtlsdr_update_ds (rtlsdr_dev_t *dev, uint32_t freq);
static int rtlsdr_set_spectrum_inversion (rtlsdr_dev_t *dev, int sideband);
static void softagc_init (rtlsdr_dev_t *dev);
static void softagc_close (rtlsdr_dev_t *dev);
static void softagc (rtlsdr_dev_t *dev, uint8_t *buf, int len);
static const char *async_status_name (enum rtlsdr_async_status status)
{
return (status == RTLSDR_INACTIVE ? "RTLSDR_INACTIVE" :
status == RTLSDR_CANCELING ? "RTLSDR_CANCELING" :
status == RTLSDR_RUNNING ? "RTLSDR_RUNNING" : "?");
}
/* generic tuner interface functions, shall be moved to the tuner implementations
*/
int e4000_init (void *dev)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
devt->e4k_s.i2c_addr = E4K_I2C_ADDR;
rtlsdr_get_xtal_freq (devt, NULL, &devt->e4k_s.vco.fosc);
devt->e4k_s.rtl_dev = dev;
return e4k_init (&devt->e4k_s);
}
int e4000_exit (void *dev)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_standby (&devt->e4k_s, 1);
}
int e4000_set_freq (void *dev, uint32_t freq)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_tune_freq (&devt->e4k_s, freq);
}
int e4000_set_bw (void *dev, int bw, uint32_t *applied_bw, int apply)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
if (!apply)
return (0);
return e4k_set_bandwidth (&devt->e4k_s, bw, applied_bw, apply);
}
int e4000_set_gain_index (void *dev, unsigned int index)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_set_gain_index (&devt->e4k_s, index);
}
int e4000_set_if_gain (void *dev, int stage, int gain)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_if_gain_set (&devt->e4k_s, (uint8_t) stage, (int8_t) (gain / 10));
}
int e4000_set_gain_mode (void *dev, int manual)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_enable_manual_gain (&devt->e4k_s, manual);
}
int e4000_set_i2c_register (void *dev, unsigned i2c_register, unsigned data, unsigned mask)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_set_i2c_register (&devt->e4k_s, i2c_register, data, mask);
}
int e4000_get_i2c_register (void *dev, uint8_t *data, int *len, int *strength)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return e4k_get_i2c_register (&devt->e4k_s, data, len, strength);
}
int r820t_init (void *dev)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
devt->r82xx_p.rtl_dev = dev;
if (devt->tuner_type == RTLSDR_TUNER_R828D)
{
devt->r82xx_c.i2c_addr = R828D_I2C_ADDR;
devt->r82xx_c.rafael_chip = CHIP_R828D;
}
else
{
devt->r82xx_c.i2c_addr = R820T_I2C_ADDR;
devt->r82xx_c.rafael_chip = CHIP_R820T;
}
rtlsdr_get_xtal_freq (devt, NULL, &devt->r82xx_c.xtal);
devt->r82xx_c.use_predetect = 0;
devt->r82xx_c.cal_imr = cal_imr;
devt->r82xx_p.cfg = &devt->r82xx_c;
return r82xx_init (&devt->r82xx_p);
}
int r820t_exit (void *dev)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_standby (&devt->r82xx_p);
}
int r820t_set_freq (void *dev, uint32_t freq)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_set_freq (&devt->r82xx_p, freq);
}
int r820t_set_bw (void *dev, int bw, uint32_t *applied_bw, int apply)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
int r = r82xx_set_bandwidth (&devt->r82xx_p, bw, applied_bw, apply);
if (!apply)
return (0);
if (r < 0)
return (r);
r = rtlsdr_set_if_freq (devt, r);
if (r)
return (r);
return rtlsdr_set_center_freq (devt, devt->freq);
}
int r820t_set_gain_index (void *dev, unsigned int index)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_set_gain_index (&devt->r82xx_p, index);
}
int r820t_set_gain_mode (void *dev, int manual)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_set_gain_mode (&devt->r82xx_p, manual);
}
int r820t_set_i2c_register (void *dev, unsigned i2c_register, unsigned data, unsigned mask)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_set_i2c_register (&devt->r82xx_p, i2c_register, data, mask);
}
int r820t_get_i2c_register (void *dev, uint8_t *data, int *len, int *strength)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
return r82xx_get_i2c_register (&devt->r82xx_p, data, len, strength);
}
int r820t_set_sideband (void *dev, int sideband)
{
rtlsdr_dev_t *devt = (rtlsdr_dev_t*) dev;
int r = r82xx_set_sideband (&devt->r82xx_p, sideband);
if (r < 0)
return (r);
r = rtlsdr_set_spectrum_inversion (devt, sideband);
if (r)
return (r);
return rtlsdr_set_center_freq (devt, devt->freq);
}
/* definition order must match enum rtlsdr_tuner
*/
static rtlsdr_tuner_iface_t tuners [] = {
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* dummy for unknown tuners */
},
{
e4000_init, e4000_exit,
e4000_set_freq, e4000_set_bw, e4000_set_gain_index, e4000_set_if_gain,
e4000_set_gain_mode, e4000_set_i2c_register,
e4000_get_i2c_register, NULL, e4k_get_gains
},
{
fc0012_init, fc0012_exit,
fc0012_set_freq, fc001x_set_bw, fc0012_set_gain_index, NULL,
fc001x_set_gain_mode, fc001x_set_i2c_register,
fc0012_get_i2c_register, NULL, fc001x_get_gains
},
{
fc0013_init, fc0013_exit,
fc0013_set_freq, fc001x_set_bw, fc0013_set_gain_index, NULL,
fc001x_set_gain_mode, fc001x_set_i2c_register,
fc0013_get_i2c_register, NULL, fc001x_get_gains
},
{
fc2580_init, fc2580_exit,
fc2580_set_freq, fc2580_set_bw, fc2580_set_gain_index, NULL,
fc2580_set_gain_mode, fc2580_set_i2c_register,
fc2580_get_i2c_register, NULL, fc2580_get_gains
},
{
r820t_init, r820t_exit,
r820t_set_freq, r820t_set_bw, r820t_set_gain_index, NULL,
r820t_set_gain_mode, r820t_set_i2c_register,
r820t_get_i2c_register, r820t_set_sideband, r82xx_get_gains
},
{
r820t_init, r820t_exit,
r820t_set_freq, r820t_set_bw, r820t_set_gain_index, NULL,
r820t_set_gain_mode, r820t_set_i2c_register,
r820t_get_i2c_register, r820t_set_sideband, r82xx_get_gains
},
};
typedef struct rtlsdr_dongle {
const uint16_t vid;
const uint16_t pid;
const char *name;
} rtlsdr_dongle_t;
typedef struct found_device {
uint16_t vid;
uint16_t pid;
char serial [80];
char mfg [80];
char DevicePath [256];
} found_device;
/*
* Please add your device here and send a patch to [email protected]
*/
static const rtlsdr_dongle_t known_devices [] =
{
{ 0x0bda, 0x2832, "Generic RTL2832U" },
{ 0x0bda, 0x2838, "Generic RTL2832U OEM" },
{ 0x0413, 0x6680, "DigitalNow Quad DVB-T PCI-E card" },
{ 0x0413, 0x6f0f, "Leadtek WinFast DTV Dongle mini D" },
{ 0x0458, 0x707f, "Genius TVGo DVB-T03 USB dongle (Ver. B)" },
{ 0x0ccd, 0x00a9, "Terratec Cinergy T Stick Black (rev 1)" },
{ 0x0ccd, 0x00b3, "Terratec NOXON DAB/DAB+ USB dongle (rev 1)" },
{ 0x0ccd, 0x00b4, "Terratec Deutschlandradio DAB Stick" },
{ 0x0ccd, 0x00b5, "Terratec NOXON DAB Stick - Radio Energy" },
{ 0x0ccd, 0x00b7, "Terratec Media Broadcast DAB Stick" },
{ 0x0ccd, 0x00b8, "Terratec BR DAB Stick" },
{ 0x0ccd, 0x00b9, "Terratec WDR DAB Stick" },
{ 0x0ccd, 0x00c0, "Terratec MuellerVerlag DAB Stick" },
{ 0x0ccd, 0x00c6, "Terratec Fraunhofer DAB Stick" },
{ 0x0ccd, 0x00d3, "Terratec Cinergy T Stick RC (Rev.3)" },
{ 0x0ccd, 0x00d7, "Terratec T Stick PLUS" },
{ 0x0ccd, 0x00e0, "Terratec NOXON DAB/DAB+ USB dongle (rev 2)" },
{ 0x1209, 0x2832, "Generic RTL2832U" },
{ 0x1554, 0x5020, "PixelView PV-DT235U(RN)" },
{ 0x15f4, 0x0131, "Astrometa DVB-T/DVB-T2" },
{ 0x15f4, 0x0133, "HanfTek DAB+FM+DVB-T" },
{ 0x185b, 0x0620, "Compro Videomate U620F"},
{ 0x185b, 0x0650, "Compro Videomate U650F"},
{ 0x185b, 0x0680, "Compro Videomate U680F"},
{ 0x1b80, 0xd393, "GIGABYTE GT-U7300" },
{ 0x1b80, 0xd394, "DIKOM USB-DVBT HD" },
{ 0x1b80, 0xd395, "Peak 102569AGPK" },
{ 0x1b80, 0xd397, "KWorld KW-UB450-T USB DVB-T Pico TV" },
{ 0x1b80, 0xd398, "Zaapa ZT-MINDVBZP" },
{ 0x1b80, 0xd39d, "SVEON STV20 DVB-T USB & FM" },
{ 0x1b80, 0xd3a4, "Twintech UT-40" },
{ 0x1b80, 0xd3a8, "ASUS U3100MINI_PLUS_V2" },
{ 0x1b80, 0xd3af, "SVEON STV27 DVB-T USB & FM" },
{ 0x1b80, 0xd3b0, "SVEON STV21 DVB-T USB & FM" },
{ 0x1d19, 0x1101, "Dexatek DK DVB-T Dongle (Logilink VG0002A)" },
{ 0x1d19, 0x1102, "Dexatek DK DVB-T Dongle (MSI DigiVox mini II V3.0)" },
{ 0x1d19, 0x1103, "Dexatek Technology Ltd. DK 5217 DVB-T Dongle" },
{ 0x1d19, 0x1104, "MSI DigiVox Micro HD" },
{ 0x1f4d, 0xa803, "Sweex DVB-T USB" },
{ 0x1f4d, 0xb803, "GTek T803" },
{ 0x1f4d, 0xc803, "Lifeview LV5TDeluxe" },
{ 0x1f4d, 0xd286, "MyGica TD312" },
{ 0x1f4d, 0xd803, "PROlectrix DV107669" },
};
#define DEFAULT_BUF_NUMBER 15
#define DEFAULT_BUF_LENGTH (64 * 512)
/* buf_len:
* must be multiple of 512 - else it will be overwritten
* in rtlsdr_read_async() in librtlsdr.c with DEFAULT_BUF_LENGTH (= 16*32 *512 = 512 *512)
*
* -> 512*512 -> 1048 ms @ 250 kS or 81.92 ms @ 3.2 MS (internal default)
* -> 32*512 -> 65 ms @ 250 kS or 5.12 ms @ 3.2 MS (new default)
*/
#define DEF_RTL_XTAL_FREQ 28800000
#define MIN_RTL_XTAL_FREQ (DEF_RTL_XTAL_FREQ - 1000)
#define MAX_RTL_XTAL_FREQ (DEF_RTL_XTAL_FREQ + 1000)
#define EEPROM_ADDR 0xa0
#define RTL2832_DEMOD_ADDR 0x20
#define DUMMY_PAGE 0x0a
#define DUMMY_ADDR 0x01
/*
* memory map
*
* 0x0000 DEMOD : demodulator
* 0x2000 USB : SIE, USB endpoint, debug, DMA
* 0x3000 SYS : system
* 0xfc00 RC : remote controller (not RTL2831U)
*/
enum usb_reg {
/* SIE Control Registers */
USB_SYSCTL = 0x2000, /* USB system control */
USB_IRQSTAT = 0x2008, /* SIE interrupt status */
USB_IRQEN = 0x200C, /* SIE interrupt enable */
USB_CTRL = 0x2010, /* USB control */
USB_STAT = 0x2014, /* USB status */
USB_DEVADDR = 0x2018, /* USB device address */
USB_TEST = 0x201C, /* USB test mode */
USB_FRAME_NUMBER = 0x2020, /* frame number */
USB_FIFO_ADDR = 0x2028, /* address of SIE FIFO RAM */
USB_FIFO_CMD = 0x202A, /* SIE FIFO RAM access command */
USB_FIFO_DATA = 0x2030, /* SIE FIFO RAM data */
/* Endpoint Registers */
EP0_SETUPA = 0x20F8, /* EP 0 setup packet lower byte */
EP0_SETUPB = 0x20FC, /* EP 0 setup packet higher byte */
USB_EP0_CFG = 0x2104, /* EP 0 configure */
USB_EP0_CTL = 0x2108, /* EP 0 control */
USB_EP0_STAT = 0x210C, /* EP 0 status */
USB_EP0_IRQSTAT = 0x2110, /* EP 0 interrupt status */
USB_EP0_IRQEN = 0x2114, /* EP 0 interrupt enable */
USB_EP0_MAXPKT = 0x2118, /* EP 0 max packet size */
USB_EP0_BC = 0x2120, /* EP 0 FIFO byte counter */
USB_EPA_CFG = 0x2144, /* EP A configure */
USB_EPA_CTL = 0x2148, /* EP A control */
USB_EPA_STAT = 0x214C, /* EP A status */
USB_EPA_IRQSTAT = 0x2150, /* EP A interrupt status */
USB_EPA_IRQEN = 0x2154, /* EP A interrupt enable */
USB_EPA_MAXPKT = 0x2158, /* EP A max packet size */
USB_EPA_FIFO_CFG = 0x2160, /* EP A FIFO configure */
/* Debug Registers */
USB_PHYTSTDIS = 0x2F04, /* PHY test disable */
USB_TOUT_VAL = 0x2F08, /* USB time-out time */
USB_VDRCTRL = 0x2F10, /* UTMI vendor signal control */
USB_VSTAIN = 0x2F14, /* UTMI vendor signal status in */
USB_VLOADM = 0x2F18, /* UTMI load vendor signal status in */
USB_VSTAOUT = 0x2F1C, /* UTMI vendor signal status out */
USB_UTMI_TST = 0x2F80, /* UTMI test */
USB_UTMI_STATUS = 0x2F84, /* UTMI status */
USB_TSTCTL = 0x2F88, /* test control */
USB_TSTCTL2 = 0x2F8C, /* test control 2 */
USB_PID_FORCE = 0x2F90, /* force PID */
USB_PKTERR_CNT = 0x2F94, /* packet error counter */
USB_RXERR_CNT = 0x2F98, /* RX error counter */
USB_MEM_BIST = 0x2F9C, /* MEM BIST test */
USB_SLBBIST = 0x2FA0, /* self-loop-back BIST */
USB_CNTTEST = 0x2FA4, /* counter test */
USB_PHYTST = 0x2FC0, /* USB PHY test */
USB_DBGIDX = 0x2FF0, /* select individual block debug signal */
USB_DBGMUX = 0x2FF4 /* debug signal module mux */
};
enum sys_reg {
/* demod control registers */
DEMOD_CTL = 0x3000, /* control register for DVB-T demodulator */
GPO = 0x3001, /* output value of GPIO */
GPI = 0x3002, /* input value of GPIO */
GPOE = 0x3003, /* output enable of GPIO */
GPD = 0x3004, /* direction control for GPIO */
SYSINTE = 0x3005, /* system interrupt enable */
SYSINTS = 0x3006, /* system interrupt status */
GP_CFG0 = 0x3007, /* PAD configuration for GPIO0-GPIO3 */
GP_CFG1 = 0x3008, /* PAD configuration for GPIO4 */
SYSINTE_1 = 0x3009,
SYSINTS_1 = 0x300A,
DEMOD_CTL1 = 0x300B,
IR_SUSPEND = 0x300C,
/* I2C master registers */
I2CCR = 0x3040, /* I2C clock */
I2CMCR = 0x3044, /* I2C master control */
I2CMSTR = 0x3048, /* I2C master SCL timing */
I2CMSR = 0x304C, /* I2C master status */
I2CMFR = 0x3050 /* I2C master FIFO */
};
enum ir_reg {
/* IR registers */
IR_RX_BUF = 0xFC00,
IR_RX_IE = 0xFD00,
IR_RX_IF = 0xFD01,
IR_RX_CTRL = 0xFD02,
IR_RX_CFG = 0xFD03,
IR_MAX_DURATION0 = 0xFD04,
IR_MAX_DURATION1 = 0xFD05,
IR_IDLE_LEN0 = 0xFD06,
IR_IDLE_LEN1 = 0xFD07,
IR_GLITCH_LEN = 0xFD08,
IR_RX_BUF_CTRL = 0xFD09,
IR_RX_BUF_DATA = 0xFD0A,
IR_RX_BC = 0xFD0B,
IR_RX_CLK = 0xFD0C,
IR_RX_C_COUNT_L = 0xFD0D,
IR_RX_C_COUNT_H = 0xFD0E,
IR_SUSPEND_CTRL = 0xFD10,
IR_ERR_TOL_CTRL = 0xFD11,
IR_UNIT_LEN = 0xFD12,
IR_ERR_TOL_LEN = 0xFD13,
IR_MAX_H_TOL_LEN = 0xFD14,
IR_MAX_L_TOL_LEN = 0xFD15,
IR_MASK_CTRL = 0xFD16,
IR_MASK_DATA = 0xFD17,
IR_RES_MASK_ADDR = 0xFD18,
IR_RES_MASK_T_LEN = 0xFD19
};
enum blocks {
DEMODB = 0x0000,
USBB = 0x0100,
SYSB = 0x0200,
IRB = 0x0201,
TUNB = 0x0300,
ROMB = 0x0400,
IICB = 0x0600
};
static uint32_t last_error;
uint32_t rtlsdr_last_error (void)
{
return (last_error);
}
/*
* Some demodulator registers, not described in datasheet:
*
* Page Reg Bitmap Description
* ---------------------------------------------------------------
* 0 0x09 Gain before the ADC
* [1:0] I, in steps of 2.5 dB
* [3:2] Q, in steps of 2.5 dB
* [6:4] I and Q, in steps of 0.7 dB
* ----------------------------------------------------------------
* 0 0x17 Gain, when DAGC is on. 0x01=min, 0xff=max
* ---------------------------------------------------------------
* 0 0x18 ADC gain, when DAGC is off. 0x00=min, 0x7f=max
* ----------------------------------------------------------------
* 0 0x19 [0] SDR mode 0: off, 1: on
* 0 0x19 [1] Test mode 0: off, 1: on
* 0 0x19 [2] 3rd FIR switch 0: on, 1: off
* 0 0x19 [4:3] DAGC speed 00 = slowest, 11 = fastest
* 0 0x19 [5] DAGC 0: on, 1: off
* ----------------------------------------------------------------
* 0 0x1a Coefficient 6 of 3rd FIR
* 0 0x1b Coefficient 5 of 3rd FIR
* 0 0x1c Coefficient 4 of 3rd FIR
* 0 0x1d Coefficient 3 of 3rd FIR
* 0 0x1e Coefficient 2 of 3rd FIR
* 0 0x1f Coefficient 1 of 3rd FIR
* ----------------------------------------------------------------
* 1 0x01 [2] Demodulator software reset 0: off, 1: on
* ----------------------------------------------------------------
* 3 0x01 RSSI
* ---------------------------------------------------------------
*/
static const rtlsdr_dongle_t *find_known_device (uint16_t vid, uint16_t pid, unsigned called_from)
{
const rtlsdr_dongle_t *device = NULL;
size_t i;
for (i = 0; i < ARRAY_SIZE(known_devices); i++)
{
if (known_devices[i].vid == vid && known_devices[i].pid == pid)
{
RTL_TRACE (1, "Found VID: 0x%04X PID: 0x%04X -> \"%s\" (line: %u)\n",
vid, pid, known_devices[i].name, called_from);
device = (known_devices + i);
break;
}
}
return (device);
}
static int List_Devices (int index, found_device *found)
{
SP_DEVINFO_DATA DeviceInfoData;
char DeviceID [256];
char DeviceID2 [256];
char devInterfaceGuidArray [256];
char Mfg [80];
char Service [32]; /* Driver service name. */
HKEY hkeyDevInfo;
DWORD length;
int vid, pid;
int DeviceIndex = 0;
int count = 0;
HDEVINFO DeviceInfoSet;
if (found)
memset (found, '\0', sizeof(*found));
DeviceInfoSet = SetupDiGetClassDevsA (NULL, "USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
last_error = GetLastError();
RTL_TRACE (1, "SetupDiGetClassDevs() failed: %s\n", trace_strerror(last_error));
return (-1);
}
memset (&DeviceInfoData, '\0', sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
while (SetupDiEnumDeviceInfo(DeviceInfoSet, DeviceIndex, &DeviceInfoData))
{
/* Get the Device Instance ID
*/
if (!SetupDiGetDeviceInstanceIdA(DeviceInfoSet, &DeviceInfoData, DeviceID, sizeof(DeviceID), NULL))
{
last_error = GetLastError();
RTL_TRACE_WINUSB ("SetupDiGetDeviceInstanceId", last_error);
DeviceIndex++;
continue;
}
RTL_TRACE (2, "%d: Found device: '%s'\n", DeviceIndex, DeviceID);
DeviceIndex++;
/* We are only interested in *usb device* instances; not root hubs (or anything else)
*/
if (_strnicmp(DeviceID, "USB\\VID_", 8))
continue;
if (!_strnicmp(DeviceID + 22, "MI_01", 5))
continue;
sscanf (DeviceID + 8, "%04X", &vid);
sscanf (DeviceID + 17, "%04X", &pid);
if (!find_known_device(vid, pid, __LINE__))
continue;
if (!_strnicmp(DeviceID + 22, "MI_", 3))
{
/* This is a composite device.
* The 'SerialNumber' will come from the parent device.
*/
DWORD hParentInst;
if (CM_Get_Parent(&hParentInst, DeviceInfoData.DevInst, 0) == ERROR_SUCCESS)
CM_Get_Device_IDA (hParentInst, DeviceID2, sizeof(DeviceID2) - 1, 0);
}
/* Get SPDRP_SERVICE */
if (!SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &DeviceInfoData, SPDRP_SERVICE, NULL, (BYTE*)Service, sizeof(Service) - 1, NULL))
{
last_error = GetLastError();
RTL_TRACE_WINUSB ("SetupDiGetDeviceRegistryProperty", last_error);
continue;
}
if (_strnicmp(Service, "WinUSB", 6))
continue;
if (!SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &DeviceInfoData, SPDRP_MFG, NULL, (BYTE*)Mfg, sizeof(Mfg) - 1, NULL))
{
last_error = GetLastError();
RTL_TRACE_WINUSB ("SetupDiGetDeviceRegistryProperty", last_error);
continue;
}
if (found)
strncpy (found->mfg, Mfg, sizeof(found->mfg));
hkeyDevInfo = SetupDiOpenDevRegKey (DeviceInfoSet, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
if (hkeyDevInfo == INVALID_HANDLE_VALUE)
{
last_error = GetLastError();
RTL_TRACE_WINUSB ("SetupDiOpenDevRegKey", last_error);
continue;
}
length = sizeof(devInterfaceGuidArray);
memset (devInterfaceGuidArray, '\0', sizeof(devInterfaceGuidArray));
if (RegQueryValueExA(hkeyDevInfo, "DeviceInterfaceGUIDs", NULL, NULL, (BYTE*)devInterfaceGuidArray, &length) != ERROR_SUCCESS)
{
last_error = GetLastError();
RTL_TRACE_WINUSB ("RegQueryValueExA", last_error);
RegCloseKey (hkeyDevInfo);
continue;
}
RegCloseKey (hkeyDevInfo);
RTL_TRACE (2, "%d: %04X:%04X %s %s\n", count, vid, pid, Mfg, &devInterfaceGuidArray[0]);
if (found && index == count) /* not when 'index == -1' */
{
char DevicePath [256];
char *backslash;
found->vid = vid;
found->pid = pid;
strcpy (DevicePath, "\\\\?\\");
strcat (DevicePath, DeviceID);
strcat (DevicePath, "#");
strcat (DevicePath, &devInterfaceGuidArray[0]);
DevicePath[7] = '#';
backslash = strchr (DevicePath + 8, '\\');
if (backslash)
*backslash = '#';
strcpy (found->DevicePath, DevicePath);
RTL_TRACE (2, "count: %d, found->mfg = %s\n"
" DevicePath = %s\n", count, found->mfg, DevicePath);
break;
}
count++;
}
if (DeviceInfoSet)
SetupDiDestroyDeviceInfoList (DeviceInfoSet);
return (count);
}
static void Close_Device (rtlsdr_dev_t *dev)
{
RTL_TRACE (2, "Calling 'WinUsb_Free (0x%p)'\n", dev->usbHandle);
if (dev->usbHandle && dev->usbHandle != INVALID_HANDLE_VALUE)
WinUsb_Free (dev->usbHandle);
if (dev->deviceHandle && dev->deviceHandle != INVALID_HANDLE_VALUE)
CloseHandle (dev->deviceHandle);
dev->usbHandle = INVALID_HANDLE_VALUE;
dev->deviceHandle = INVALID_HANDLE_VALUE;
}
static BOOL Open_Device (rtlsdr_dev_t *dev, const char *DevicePath, int *err)
{
BOOL rc = TRUE;
RTL_TRACE (2, "Calling 'CreateFileA (\"%s\")'\n", DevicePath);
dev->usbHandle = INVALID_HANDLE_VALUE;
dev->deviceHandle = CreateFileA (DevicePath, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (dev->deviceHandle == INVALID_HANDLE_VALUE)
{
last_error = GetLastError();
*err = -1 * last_error;
RTL_TRACE (1, "CreateFileA(\"%s\") failed: %s\n", DevicePath, trace_strerror(last_error));
rc = FALSE;
}
else if (!WinUsb_Initialize(dev->deviceHandle, &dev->usbHandle))
{
last_error = GetLastError();
*err = -1 * last_error;
RTL_TRACE_WINUSB ("WinUsb_Initialize", last_error);
Close_Device (dev);
rc = FALSE;
}
RTL_TRACE (1, "dev->deviceHandle: 0x%p, dev->usbHandle: 0x%p\n", dev->deviceHandle, dev->usbHandle);
*err = 0;
return (rc);
}
static int usb_control_transfer (rtlsdr_dev_t *dev, /* the active device */
uint8_t type, /* CTRL_IN or CTRL_OUT */
uint16_t wValue, /* register value */
uint16_t wIndex, /* register block-index */
uint8_t *data, /* read or write control-data */
uint16_t wLength, /* length of control-data */
unsigned line) /* called from line */
{
WINUSB_SETUP_PACKET setupPacket;
ULONG written;
BOOL rc;
if (!dev)
{
last_error = ERROR_INVALID_PARAMETER;
RTL_TRACE (0, "FATAL: %s() called from %u with 'dev == NULL'!\n", __FUNCTION__, line);
return (-1);
}
if (!dev->usbHandle)
{
last_error = ERROR_INVALID_PARAMETER;
RTL_TRACE (0, "FATAL: %s() called from %u with 'dev->usbHandle == NULL'!\n", __FUNCTION__, line);
return (-1);
}
/* Setup packets are always 8 bytes (64 bits)
*/
*(__int64*) &setupPacket = 0;
/* Fill the setup packet
*/
setupPacket.RequestType = type;
setupPacket.Value = wValue;
setupPacket.Index = wIndex;
setupPacket.Length = wLength;
RTL_TRACE (2, "%u: type: %s data: 0x%p, wLength: %u\n",
line,
type == CTRL_IN ? "CTRL_IN, " :
type == CTRL_OUT ? "CTRL_OUT," : "?,",
data, wLength);
written = 0;
rc = WinUsb_ControlTransfer (dev->usbHandle, setupPacket, data, wLength, &written, NULL);
if (!rc || written != wLength)
{
last_error = GetLastError();
RTL_TRACE (1, "%u: WinUsb_ControlTransfer() %s only %lu bytes: %s\n",
line, type == CTRL_IN ? "read" : "wrote",
written, trace_strerror(last_error));
return (-1);
}
return (int)written;
}
static uint8_t rtlsdr_read_reg (rtlsdr_dev_t *dev, uint16_t index, uint16_t addr)
{
uint8_t data;
int r = RTLSDR_READ_ARRAY (dev, index, addr, &data, 1);
if (r != 1)
RTL_TRACE (1, "%s failed with %d\n", __FUNCTION__, r);
return data;
}
static int rtlsdr_write_reg (rtlsdr_dev_t *dev, uint16_t index, uint16_t addr, uint16_t val, uint8_t len)
{
uint8_t data [2];
int r;
assert (len == 1 || len == 2);
if (len == 1)
data[0] = val & 0xff;
else
{
data[0] = val >> 8;
data[1] = val & 0xff;
}
r = RTLSDR_WRITE_ARRAY (dev, index, addr, data, len);
if (r < 0)
RTL_TRACE (1, "%s failed with %d\n", __FUNCTION__, r);
return (r);
}
static int rtlsdr_write_reg_mask (rtlsdr_dev_t *dev, uint16_t index, uint16_t addr, uint8_t val, uint8_t mask)
{
uint8_t tmp = rtlsdr_read_reg (dev, index, addr);
val = (tmp & ~mask) | (val & mask);
if (tmp == val)
return (0);
return rtlsdr_write_reg (dev, index, addr, (uint16_t)val, 1);
}
static uint8_t check_tuner (rtlsdr_dev_t *dev, uint8_t i2c_addr, uint8_t reg)
{
uint8_t data = 0;
if (RTLSDR_READ_ARRAY(dev, TUNB, reg << 8 | i2c_addr, &data, 1) != 1)
return (0xFF); /* signal a read-error since the address is unsupported for this tuner */
return (data);
}
int rtlsdr_i2c_write_fn (void *dev, uint8_t addr, uint8_t reg, uint8_t *buf, int len)
{
int wr_len;
if (!dev)
return (-1);
wr_len = RTLSDR_WRITE_ARRAY ((rtlsdr_dev_t*)dev, TUNB, reg << 8 | addr, buf, len);
RTL_TRACE (2, "I2C-bus addr: 0x%02X, reg: 0x%02X, wr_len: %d\n", addr, reg, wr_len);
return (wr_len);
}
int rtlsdr_i2c_read_fn (void *dev, uint8_t addr, uint8_t reg, uint8_t *buf, int len)
{
int rd_len;
if (!dev)
return (-1);