-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms1xxx-usb.c
1298 lines (1104 loc) · 39 KB
/
sms1xxx-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
/* SMS1XXX - Siano DVB-T USB driver for FreeBSD 8.0 and higher:
*
* Copyright (C) 2008-2014, Ganaël Laplanche, http://contribs.martymac.org
*
* This driver contains code taken from the FreeBSD dvbusb driver:
*
* Copyright (C) 2006-2007, Raaf
* Copyright (C) 2004-2006, Patrick Boettcher
*
* This driver contains code taken from the Linux siano driver:
*
* Siano Mobile Silicon, Inc.
* MDTV receiver kernel modules.
* Copyright (C) 2006-2009, Uri Shkolnik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
*
* 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.
*
* * * *
* USB communications handling
* * * *
*/
/* sleep(9) stuff */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
/* mutex(9) stuff */
#include <sys/lock.h>
#include <sys/mutex.h>
#include "sms1xxx.h"
#include "sms1xxx-usb.h"
#include "sms1xxx-demux.h"
#include "sms1xxx-firmware.h"
#include "sms1xxx-endian.h"
#include "sms1xxx-gpio.h"
#include "sms1xxx-frontend.h"
static void sms1xxx_usb_tx_cb(struct usb_xfer *, usb_error_t);
static void sms1xxx_usb_rx_cb(struct usb_xfer *, usb_error_t);
static const struct usb_config sms1xxx_config[SMS1XXX_N_TRANSFER] = {
[SMS1XXX_BULK_TX] = {
.type = UE_BULK,
.endpoint = USB_SIANO_ENDP_CTRL,
.direction = UE_DIR_OUT,
.bufsize = SMS1XXX_BULK_TX_BUFS_SIZE,
.timeout = 250,
.callback = &sms1xxx_usb_tx_cb,
},
[SMS1XXX_BULK_RX] = {
.type = UE_BULK,
.endpoint = USB_SIANO_ENDP_RCV,
.direction = UE_DIR_IN,
.bufsize = SMS1XXX_BULK_RX_BUFS_SIZE,
.timeout = FRONTEND_TIMEOUT + 500,
.callback = &sms1xxx_usb_rx_cb,
.flags = {
.pipe_bof = 1,
.short_xfer_ok = 1,
},
},
};
/*******************
* Stats functions *
*******************/
static u32 sms_to_guard_interval_table[] = {
[0] = GUARD_INTERVAL_1_32,
[1] = GUARD_INTERVAL_1_16,
[2] = GUARD_INTERVAL_1_8,
[3] = GUARD_INTERVAL_1_4,
};
static u32 sms_to_code_rate_table[] = {
[0] = FEC_1_2,
[1] = FEC_2_3,
[2] = FEC_3_4,
[3] = FEC_5_6,
[4] = FEC_7_8,
};
static u32 sms_to_hierarchy_table[] = {
[0] = HIERARCHY_NONE,
[1] = HIERARCHY_1,
[2] = HIERARCHY_2,
[3] = HIERARCHY_4,
};
static u32 sms_to_modulation_table[] = {
[0] = QPSK,
[1] = QAM_16,
[2] = QAM_64,
[3] = DQPSK,
};
static inline int
sms_to_mode(u32 mode)
{
switch (mode) {
case 2:
return (TRANSMISSION_MODE_2K);
case 4:
return (TRANSMISSION_MODE_4K);
case 8:
return (TRANSMISSION_MODE_8K);
}
return (TRANSMISSION_MODE_AUTO);
}
static inline int
sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
{
TRACE(TRACE_USB,"\n");
if (is_demod_locked)
return (FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
FE_HAS_SYNC | FE_HAS_LOCK);
if (is_rf_locked)
return (FE_HAS_SIGNAL | FE_HAS_CARRIER);
return (0);
}
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define convert_from_table(value, table, defval) ({ \
u32 __ret; \
if (value < ARRAY_SIZE(table)) \
__ret = table[value]; \
else \
__ret = defval; \
__ret; \
})
static inline u32
sms_to_bw(u32 value)
{
return (value * 1000000);
}
#define sms_to_guard_interval(value) \
convert_from_table(value, sms_to_guard_interval_table, \
GUARD_INTERVAL_AUTO);
#define sms_to_code_rate(value) \
convert_from_table(value, sms_to_code_rate_table, \
FEC_NONE);
#define sms_to_hierarchy(value) \
convert_from_table(value, sms_to_hierarchy_table, \
FEC_NONE);
#define sms_to_modulation(value) \
convert_from_table(value, sms_to_modulation_table, \
FEC_NONE);
static void
sms1xxx_update_tx_params(struct sms1xxx_softc *sc,
struct sms_tx_stats *p)
{
struct dtv_frontend_properties *c = &sc->dtv_property_cache;
TRACE(TRACE_USB,"\n");
c->frequency = p->frequency;
sc->fe_status = sms_to_status(p->is_demod_locked, 0);
c->bandwidth_hz = sms_to_bw(p->bandwidth);
c->transmission_mode = sms_to_mode(p->transmission_mode);
c->guard_interval = sms_to_guard_interval(p->guard_interval);
c->code_rate_HP = sms_to_code_rate(p->code_rate);
c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
c->hierarchy = sms_to_hierarchy(p->hierarchy);
c->modulation = sms_to_modulation(p->constellation);
return;
}
static void
sms1xxx_update_per_slices(struct sms1xxx_softc *sc,
struct RECEPTION_STATISTICS_PER_SLICES_S *p)
{
struct dtv_frontend_properties *c = &sc->dtv_property_cache;
TRACE(TRACE_USB,"\n");
sc->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
c->modulation = sms_to_modulation(p->constellation);
/* signal Strength, in DBm */
c->strength.stat[0].uvalue = p->in_band_power * 1000;
/* Carrier to noise ratio, in DB */
c->cnr.stat[0].svalue = p->snr * 1000;
/* PER/BER requires demod lock */
if (!p->is_demod_locked)
return;
/* TS PER */
sc->last_per = c->block_error.stat[0].uvalue;
c->block_error.stat[0].scale = FE_SCALE_COUNTER;
c->block_count.stat[0].scale = FE_SCALE_COUNTER;
c->block_error.stat[0].uvalue += p->ets_packets;
c->block_count.stat[0].uvalue += p->ets_packets + p->ts_packets;
/* ber */
c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
c->post_bit_error.stat[0].uvalue += p->ber_error_count;
c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
/* Legacy PER/BER */
u64 tmp = p->ets_packets * 65535;
if (p->ts_packets + p->ets_packets)
tmp /= (p->ts_packets + p->ets_packets);
sc->legacy_per = tmp;
return;
}
static void
sms1xxx_update_dvb_stats(struct sms1xxx_softc *sc,
struct sms_stats *p)
{
struct dtv_frontend_properties *c = &sc->dtv_property_cache;
TRACE(TRACE_USB,"\n");
/* Update DVB modulation parameters */
c->frequency = p->frequency;
sc->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
c->bandwidth_hz = sms_to_bw(p->bandwidth);
c->transmission_mode = sms_to_mode(p->transmission_mode);
c->guard_interval = sms_to_guard_interval(p->guard_interval);
c->code_rate_HP = sms_to_code_rate(p->code_rate);
c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
c->hierarchy = sms_to_hierarchy(p->hierarchy);
c->modulation = sms_to_modulation(p->constellation);
/* update reception data */
c->lna = p->is_external_lna_on ? 1 : 0;
/* Carrier to noise ratio, in DB */
c->cnr.stat[0].svalue = p->SNR * 1000;
/* signal Strength, in DBm */
c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
/* PER/BER requires demod lock */
if (!p->is_demod_locked)
return;
/* TS PER */
sc->last_per = c->block_error.stat[0].uvalue;
c->block_error.stat[0].scale = FE_SCALE_COUNTER;
c->block_count.stat[0].scale = FE_SCALE_COUNTER;
c->block_error.stat[0].uvalue += p->error_ts_packets;
c->block_count.stat[0].uvalue += p->total_ts_packets;
/* ber */
c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
c->post_bit_error.stat[0].uvalue += p->ber_error_count;
c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
/* Legacy PER/BER */
sc->legacy_ber = p->ber;
return;
};
/****************************
* Initialization functions *
****************************/
/* Initialize USB input/output pipes and xfers */
int
sms1xxx_usb_init(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB, "sc=%p init=%d\n", sc, sc->usbinit);
if(sc->usbinit < 0) {
ERR("USB (Un)Initialisation in progress\n");
return (EBUSY);
}
sc->usbinit = -1;
/* Setup USB xfers */
if(usbd_transfer_setup(sc->udev, &sc->sc_iface_index, sc->sc_xfer,
sms1xxx_config, SMS1XXX_N_TRANSFER, sc, &sc->sc_mtx)) {
ERR("could not setup xfers\n");
sc->usbinit = 0;
return (EIO);
}
sc->usbinit = 1;
return (0);
}
/* Un-initialize USB input/output pipes and xfers */
int
sms1xxx_usb_exit(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB,"sc=%p init=%d\n",sc,sc->usbinit);
if(sc->usbinit < 0) {
ERR("USB (Un)Initialisation in progress\n");
return (EBUSY);
}
sc->usbinit = -1;
/* Unsetup USB xfers */
usbd_transfer_unsetup(sc->sc_xfer, SMS1XXX_N_TRANSFER);
sc->usbinit = 0;
return (0);
}
/* Called on each open() of a demux device */
int
sms1xxx_usb_ref(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB,"refcnt %d -> %d\n",sc->usbrefs,sc->usbrefs + 1);
sc->usbrefs++;
return (0);
}
/* Called on each close() of a demux device */
void
sms1xxx_usb_deref(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB,"refcnt %d -> %d\n",sc->usbrefs,sc->usbrefs - 1);
sc->usbrefs--;
}
/**********************
* Callback functions *
**********************/
/* Analyze packets received from the device */
static void
sms1xxx_usb_get_packets(struct sms1xxx_softc *sc, u8 *packet, u32 bytes)
{
u32 offset = 0;
int stats_updated = 0;
if (bytes == 0) {
ERR("null message\n");
return;
}
struct sms_msg_hdr *phdr = (struct sms_msg_hdr *)packet;
sms1xxx_endian_handle_message_header(phdr);
TRACE(TRACE_USB_FLOW,"got %d bytes "
"(msg_length=%d, msg_type=%d, msg_flags=%d)\n",
bytes, phdr->msg_length, phdr->msg_type, phdr->msg_flags);
if (bytes < phdr->msg_length) {
ERR("invalid message : msg_length=%d, received only %d bytes\n",
phdr->msg_length, bytes);
return;
}
if ((phdr->msg_flags & MSG_HDR_FLAG_SPLIT_MSG) &&
(bytes > phdr->msg_length)) {
/* Compute start offset */
offset = bytes - phdr->msg_length;
if(offset < sizeof(struct sms_msg_hdr)) {
ERR("invalid message : offset too small\n");
return;
}
/* Copy header to its new location */
memcpy((u8 *)phdr + offset, phdr, sizeof(struct sms_msg_hdr));
TRACE(TRACE_USB_FLOW, "split message detected, offset=%d, "
"header=%p, new=%p\n", offset, phdr, (u8 *)phdr + offset);
/* Move pointer to its new location */
phdr = (struct sms_msg_hdr *)((u8 *)phdr + offset);
}
bytes = phdr->msg_length;
/* Do we need to re-route ? */
if ((phdr->msg_type == MSG_SMS_HO_PER_SLICES_IND) ||
(phdr->msg_type == MSG_SMS_TRANSMISSION_IND)) {
if (sc->mode == DEVICE_MODE_DVBT_BDA)
phdr->msg_dst_id = DVBT_BDA_CONTROL_MSG_ID;
}
sms1xxx_endian_handle_rx_message(phdr);
switch (phdr->msg_type) {
case MSG_SMS_DVBT_BDA_DATA:
{
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_DVBT_BDA_DATA\n");
/* Send data to demuxer using chunks of PACKET_SIZE bytes */
/* Number of bytes remaining */
u32 remaining = bytes - sizeof(struct sms_msg_hdr);
/* Number of chunks sent */
u32 sent = 0;
while (remaining >= PACKET_SIZE) {
TRACE(TRACE_USB_FLOW,"sending packet %d to demuxer, "
"addr = %p\n",sent,
((u8*)(phdr + 1))+(sent*PACKET_SIZE));
sms1xxx_demux_put_packet(sc,
((u8*)(phdr + 1))+(sent*PACKET_SIZE));
sent++;
remaining -= PACKET_SIZE;
}
TRACE(TRACE_USB_FLOW,"sent %d packets to demuxer, "
"%d bytes remaining\n",sent,remaining);
if (remaining > 0) {
WARN("junk data received, %d bytes skipped\n",
remaining);
}
break;
}
case MSG_SMS_RF_TUNE_RES:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_RF_TUNE_RES\n");
sms1xxx_demux_pesbuf_reset(sc, 1, "channel zap");
sc->fe_status = 0;
stats_updated = 1;
DLG_COMPLETE(sc->dlg_status, DLG_TUNE_DONE);
break;
/* SMS11xx statistics messages */
case MSG_SMS_SIGNAL_DETECTED_IND:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_SIGNAL_DETECTED_IND\n");
sc->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
stats_updated = 1;
break;
case MSG_SMS_NO_SIGNAL_IND:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_NO_SIGNAL_IND\n");
sc->fe_status = 0;
stats_updated = 1;
break;
case MSG_SMS_TRANSMISSION_IND:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_TRANSMISSION_IND\n");
sms1xxx_update_tx_params(sc, (struct sms_tx_stats *)(phdr + 1));
stats_updated = 1;
break;
case MSG_SMS_HO_PER_SLICES_IND:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_HO_PER_SLICES_IND\n");
sms1xxx_update_per_slices(sc, (struct RECEPTION_STATISTICS_PER_SLICES_S *)(phdr + 1));
stats_updated = 1;
break;
/* SMS1000 - triggered - statistics messages */
case MSG_SMS_GET_STATISTICS_RES:
TRACE(TRACE_USB_FLOW,"handling MSG_SMS_GET_STATISTICS_RES\n");
sms1xxx_update_dvb_stats(sc,
&(((struct sms_msg_statistics_info *)(phdr + 1))->stat));
stats_updated = 1;
DLG_COMPLETE(sc->dlg_status, DLG_STAT_DONE);
break;
case MSG_SMS_GET_VERSION_EX_RES:
{
#ifdef SMS1XXX_DEBUG
if(sms1xxxdbg & TRACE_USB_FLOW) {
struct sms_version_res *ver =
(struct sms_version_res *) phdr;
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_GET_VERSION_EX_RES\n");
TRACE(TRACE_USB_FLOW,
"dumping version information :\n");
char label[MSG_VER_LABEL_SIZE + 1];
strncpy(label, ver->TextLabel, sizeof(label));
printf("+ label: %s\n", label);
printf("+ chipset: 0x%x\n", ver->chip_model);
printf("+ firmware id: %d\n", ver->firmware_id);
printf("+ supp. protos: 0x%x\n", ver->supported_protocols);
printf("+ version: %d.%d\n",
ver->version_major, ver->version_minor);
printf("+ romversion: %d.%d\n",
ver->rom_ver_major, ver->rom_ver_minor);
}
#endif
break;
}
case MSG_SMS_GET_PID_FILTER_LIST_RES:
/* 'REQ' instead of 'RES' : FAMILY1 firmware bug ? */
case MSG_SMS_GET_PID_FILTER_LIST_REQ:
{
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_GET_PID_FILTER_LIST_RES\n");
#ifdef SMS1XXX_DEBUG
struct sms_msg_data *pdata =
(struct sms_msg_data *)((u8 *) packet);
u32 nfilt = pdata->msg_data[1];
u32 *filterList = &(pdata->msg_data[2]);
TRACE(TRACE_FILTERS, "found %d filters in stack\n", nfilt);
for(int i = 0; i < nfilt; ++i) {
TRACE(TRACE_FILTERS, "filter %u(0x%x) set\n",
filterList[i], filterList[i]);
}
#endif
DLG_COMPLETE(sc->dlg_status, DLG_PID_DONE);
break;
}
case MSG_SMS_ADD_PID_FILTER_RES:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_ADD_PID_FILTER_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_PID_DONE);
break;
case MSG_SMS_REMOVE_PID_FILTER_RES:
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_REMOVE_PID_FILTER_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_PID_DONE);
break;
case MSG_SMS_INIT_DEVICE_RES:
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_INIT_DEVICE_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_INIT_DONE);
break;
case MSG_SW_RELOAD_START_RES:
TRACE(TRACE_USB_FLOW,
"handling MSG_SW_RELOAD_START_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_RELOAD_START_DONE);
break;
case MSG_SMS_DATA_DOWNLOAD_RES:
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_DATA_DOWNLOAD_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_DATA_DOWNLOAD_DONE);
break;
case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
TRACE(TRACE_USB_FLOW,
"handling MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_SWDOWNLOAD_TRIGGER_DONE);
break;
case MSG_SMS_START_IR_RES:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_START_IR_RES\n");
INFO("IR module started\n");
DLG_COMPLETE(sc->dlg_status, DLG_IR_DONE);
break;
case MSG_SMS_IR_SAMPLES_IND:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_IR_SAMPLES_IND\n");
sms1xxx_ir_put_packet(sc,
(const u8 *)((u8 *)phdr + sizeof(struct sms_msg_hdr)),
(u32)phdr->msg_length - sizeof(struct sms_msg_hdr));
break;
case MSG_SMS_GPIO_CONFIG_RES:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_GPIO_CONFIG_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_GPIO_CONFIG_DONE);
break;
case MSG_SMS_GPIO_CONFIG_EX_RES:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_GPIO_CONFIG_EX_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_GPIO_CONFIG_DONE);
break;
case MSG_SMS_GPIO_SET_LEVEL_RES:
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_GPIO_SET_LEVEL_RES\n");
DLG_COMPLETE(sc->dlg_status, DLG_GPIO_SET_DONE);
break;
case MSG_SMS_GPIO_GET_LEVEL_RES:
{
TRACE(TRACE_USB_FLOW, "handling MSG_SMS_GPIO_GET_LEVEL_RES\n");
u32 *msgdata = (u32 *)phdr;
sc->gpio.get_res = msgdata[1];
DLG_COMPLETE(sc->dlg_status, DLG_GPIO_GET_DONE);
break;
}
default:
TRACE(TRACE_USB_FLOW,"unhandled msg type (%d)\n", phdr->msg_type);
#ifdef SMS1XXX_DEBUG
/* Dump raw data */
TRACE(TRACE_USB_DUMP,"dumping %d bytes :\n", bytes);
if(sms1xxxdbg & TRACE_USB_DUMP)
sms1xxx_dump_data((const u8*)phdr, bytes, '+');
#endif
break;
}
/* Update frontend status given received stats */
if (stats_updated) {
if (!(sc->fe_status & FE_HAS_LOCK)) {
sms1xxx_frontend_stats_not_ready(sc);
}
sc->fe_event = 1;
}
}
static void
sms1xxx_usb_tx_cb(struct usb_xfer *xfer, usb_error_t error)
{
struct sms1xxx_softc *sc = usbd_xfer_softc(xfer);
struct usb_page_cache *pc;
struct sms1xxx_data *data;
mtx_assert(&sc->sc_mtx, MA_OWNED);
if(sc->sc_dying)
return;
switch(USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
data = STAILQ_FIRST(&sc->sc_tx_active);
if (data == NULL)
goto tx_setup;
STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
/* FALLTHROUGH */
case USB_ST_SETUP:
tx_setup:
data = STAILQ_FIRST(&sc->sc_tx_pending);
if (data == NULL)
return;
STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
pc = usbd_xfer_get_frame(xfer, 0);
usbd_copy_in(pc, 0, data->buf, data->buflen);
usbd_xfer_set_frame_len(xfer, 0, data->buflen);
STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
usbd_transfer_submit(xfer);
break;
default:
break;
}
return;
}
/* Main callback function, calls sms1xxx_usb_get_packets */
static void
sms1xxx_usb_rx_cb(struct usb_xfer *xfer, usb_error_t error)
{
struct sms1xxx_softc *sc = usbd_xfer_softc(xfer);
mtx_assert(&sc->sc_mtx, MA_OWNED);
if(sc->sc_dying)
return;
switch(USB_GET_STATE(xfer)) {
case USB_ST_TRANSFERRED:
usbd_xfer_frame_data(xfer, 0, (void*)&sc->sc_rx_data.buf,
&sc->sc_rx_data.buflen);
#ifdef SMS1XXX_DIAGNOSTIC
sc->stats.interrupts++;
sc->stats.bytes += sc->sc_rx_data.buflen;
#endif
sms1xxx_usb_get_packets(sc, sc->sc_rx_data.buf, sc->sc_rx_data.buflen);
/* FALLTHROUGH */
case USB_ST_SETUP:
rx_setup:
usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
usbd_transfer_submit(xfer);
break;
default:
if (error != USB_ERR_CANCELLED) {
/* try to clear stall first */
usbd_xfer_set_stall(xfer);
goto rx_setup;
}
break;
}
return;
}
int
sms1xxx_usb_xfers_start(struct sms1xxx_softc *sc)
{
int i;
TRACE(TRACE_USB,"sc=%p init=%d\n",sc,sc->usbinit);
if(sc->usbinit <= 0) {
ERR("usb not initialized\n");
return (ENXIO);
}
/* Output : initialize TX STAILQs */
STAILQ_INIT(&sc->sc_tx_inactive);
STAILQ_INIT(&sc->sc_tx_pending);
STAILQ_INIT(&sc->sc_tx_active);
for (i = 0; i < SMS1XXX_BULK_TX_BUFS; i++) {
struct sms1xxx_data *data = &sc->sc_tx_data[i];
data->buf =
malloc(SMS1XXX_BULK_TX_BUFS_SIZE, M_USBDEV, M_NOWAIT | M_ZERO);
if (data->buf == NULL) {
TRACE(TRACE_USB,"could not allocate TX buffer!\n");
return (ENOMEM);
}
STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
}
/* Input : RX transfer */
mtx_lock(&sc->sc_mtx);
usbd_transfer_start(sc->sc_xfer[SMS1XXX_BULK_RX]);
mtx_unlock(&sc->sc_mtx);
return (0);
}
int
sms1xxx_usb_xfers_stop(struct sms1xxx_softc *sc)
{
int i;
TRACE(TRACE_USB,"sc=%p init=%d\n",sc,sc->usbinit);
if(sc->usbinit <= 0) {
ERR("usb not initialized\n");
return (ENXIO);
}
mtx_lock(&sc->sc_mtx);
usbd_transfer_stop(sc->sc_xfer[SMS1XXX_BULK_RX]);
mtx_unlock(&sc->sc_mtx);
usbd_transfer_drain(sc->sc_xfer[SMS1XXX_BULK_RX]);
/* Input : RX transfer */
mtx_lock(&sc->sc_mtx);
usbd_transfer_stop(sc->sc_xfer[SMS1XXX_BULK_TX]);
mtx_unlock(&sc->sc_mtx);
usbd_transfer_drain(sc->sc_xfer[SMS1XXX_BULK_TX]);
/* Output : un-initialize TX STAILQs */
for (i = 0; i < SMS1XXX_BULK_TX_BUFS; i++) {
struct sms1xxx_data *data = &sc->sc_tx_data[i];
free(data->buf, M_USBDEV);
}
return (0);
}
/*******************
* Write functions *
*******************/
/* Write data to the device */
int
sms1xxx_usb_rawwrite(struct sms1xxx_softc *sc, const u8 *wbuf, u32 wlen)
{
int offset, bsize;
struct sms1xxx_data *data;
TRACE(TRACE_USB_FLOW,"wlen=%d\n",wlen);
if(sc->usbinit <= 0) {
ERR("usb not initialized\n");
return (ENXIO);
}
for (offset = 0; offset < wlen; offset += bsize) {
if ((wlen - offset) > SMS1XXX_BULK_TX_BUFS_SIZE)
bsize = SMS1XXX_BULK_TX_BUFS_SIZE;
else
bsize = wlen - offset;
mtx_lock(&sc->sc_mtx);
data = STAILQ_FIRST(&sc->sc_tx_inactive);
if (data == NULL) {
/* Wait a few ms for free buffers */
usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(5));
data = STAILQ_FIRST(&sc->sc_tx_inactive);
if (data == NULL) {
mtx_unlock(&sc->sc_mtx);
return (ENOBUFS);
}
}
STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
memcpy(data->buf, wbuf + offset, bsize);
data->buflen = bsize;
STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
usbd_transfer_start(sc->sc_xfer[SMS1XXX_BULK_TX]);
mtx_unlock(&sc->sc_mtx);
}
TRACE(TRACE_USB_FLOW,"sent %d bytes\n",offset);
return (0);
}
/* Write data to the device, handling endianness conversions */
int
sms1xxx_usb_write(struct sms1xxx_softc *sc, u8 *wbuf, u32 wlen)
{
TRACE(TRACE_USB_FLOW,"\n");
sms1xxx_endian_handle_message_header(wbuf);
return (sms1xxx_usb_rawwrite(sc, (const u8 *)wbuf, wlen));
}
/* Wait for a response from the device
XXX To be improved */
static int
sms1xxx_usb_wait(struct sms1xxx_softc *sc,
u16 completion, unsigned int delay_ms)
{
struct timeval tv_start;
struct timeval tv;
int ms;
microtime(&tv_start);
do {
pause("pause",5);
microtime(&tv);
timevalsub(&tv,&tv_start);
ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
if(sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ECANCELED);
}
if (DLG_ISCOMPLETE(sc->dlg_status, completion))
return (0);
}
while (ms < delay_ms);
return (ETIMEDOUT);
}
/* Write data to the device and wait for a response */
int
sms1xxx_usb_rawwrite_and_wait(struct sms1xxx_softc *sc, const u8 *wbuf,
u32 wlen, u16 completion, unsigned int delay_ms)
{
int err = 0;
TRACE(TRACE_USB_FLOW,"completion=%u(0x%x),delay_ms=%d\n",completion,
completion,delay_ms);
DLG_INIT(sc->dlg_status, completion);
err = sms1xxx_usb_rawwrite(sc, wbuf, wlen);
if (err == 0) {
err = sms1xxx_usb_wait(sc, completion, delay_ms);
}
TRACE(TRACE_USB_FLOW,"done,completion=%u(0x%x),err=%d\n",completion,
completion,err);
return (err);
}
/* Write data to the device and wait for a response,
handling endianness conversions */
int
sms1xxx_usb_write_and_wait(struct sms1xxx_softc *sc, u8 *wbuf,
u32 wlen, u16 completion, unsigned int delay_ms)
{
TRACE(TRACE_USB_FLOW,"\n");
sms1xxx_endian_handle_message_header(wbuf);
return (sms1xxx_usb_rawwrite_and_wait(sc, (const u8 *)wbuf,
wlen, completion, delay_ms));
}
/***************************
* Higher-level operations *
***************************/
/* Set requested mode and ask device to reattach */
int
sms1xxx_usb_setmode(struct sms1xxx_softc *sc, int mode)
{
TRACE(TRACE_USB,"mode=%d\n",mode);
struct sms_msg_hdr Msg = {
MSG_SW_RELOAD_REQ,
0,
HIF_TASK,
sizeof(struct sms_msg_hdr),
0
};
if (sms1xxx_firmware_name(sc->sc_type, mode) == NULL) {
ERR("invalid mode specified %d\n", mode);
return (EINVAL);
}
/* Set requested mode and ask device to re-attach */
sc->mode = DEVICE_MODE_NONE ;
sc->device->requested_mode = mode ;
sc->device->fw_loading = FALSE ;
INFO("asking device to reset, target mode=%d\n",mode);
return (sms1xxx_usb_write(sc, (u8*)&Msg, sizeof(Msg)));
}
/* Initialize the device after firmware loading */
int
sms1xxx_usb_initdevice(struct sms1xxx_softc *sc, int mode)
{
TRACE(TRACE_USB,"mode=%d\n", mode);
struct sms_msg_data InitMsg;
if ((sms1xxx_firmware_name(sc->sc_type, mode) == NULL) ||
(mode != sc->device->requested_mode)) {
ERR("invalid mode specified %d\n", mode);
return (EINVAL);
}
InitMsg.x_msg_header.msg_type = MSG_SMS_INIT_DEVICE_REQ;
InitMsg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
InitMsg.x_msg_header.msg_dst_id = HIF_TASK;
InitMsg.x_msg_header.msg_length = sizeof(InitMsg);
InitMsg.x_msg_header.msg_flags = 0;
InitMsg.msg_data[0] = mode;
sms1xxx_endian_handle_tx_message(&InitMsg);
return (sms1xxx_usb_write_and_wait(sc, (u8*)&InitMsg, sizeof(InitMsg),
DLG_INIT_DONE, FRONTEND_TIMEOUT));
}
/* Prepare a *warm* device to firmware upload */
int
sms1xxx_usb_reloadstart(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB,"\n");
struct sms_msg_hdr Msg = {
MSG_SW_RELOAD_START_REQ,
0,
HIF_TASK,
sizeof(struct sms_msg_hdr),
0
};
return (sms1xxx_usb_write_and_wait(sc, (u8*)&Msg, sizeof(Msg),
DLG_RELOAD_START_DONE, FRONTEND_TIMEOUT));
}
/* Execute firmware on a *warm* device after firmware upload */
/* XXX Does not seem to work : alignment pb ? */
int
sms1xxx_usb_reloadexec(struct sms1xxx_softc *sc)
{
TRACE(TRACE_USB,"\n");
struct sms_msg_hdr Msg = {
MSG_SW_RELOAD_EXEC_REQ,
0,
HIF_TASK,
sizeof(struct sms_msg_hdr),
0
};
return (sms1xxx_usb_write(sc, (u8*)&Msg, sizeof(Msg)));
}
/* Execute firmware on a *cold* device after firmware upload */
int
sms1xxx_usb_swdtrigger(struct sms1xxx_softc *sc, u32 start_address)
{
int err = 0;
TRACE(TRACE_USB,"start_address=%d\n", start_address);
/* sms_msg_data with extra data */
int msgSize = sizeof(struct sms_msg_data) + (sizeof(u32) * 4);
struct sms_msg_data *TriggerMsg = malloc(msgSize, M_USBDEV, M_WAITOK);
if(TriggerMsg == NULL) {
ERR("could not allocate msg buffer\n");
return (ENOMEM);
}
TriggerMsg->x_msg_header.msg_type = MSG_SMS_SWDOWNLOAD_TRIGGER_REQ;
TriggerMsg->x_msg_header.msg_src_id = 0;
TriggerMsg->x_msg_header.msg_dst_id = HIF_TASK;
TriggerMsg->x_msg_header.msg_length = msgSize;
TriggerMsg->x_msg_header.msg_flags = 0;
TriggerMsg->msg_data[0] = start_address; /* Entry point */
TriggerMsg->msg_data[1] = 5; /* Priority */
TriggerMsg->msg_data[2] = 0x200; /* Stack size */
TriggerMsg->msg_data[3] = 0; /* Parameter */
TriggerMsg->msg_data[4] = 4; /* Task ID */
sms1xxx_endian_handle_tx_message(TriggerMsg);
/* XXX honour SMS_ROM_NO_RESPONSE and use sms1xxx_usb_write() ? */
err = sms1xxx_usb_write_and_wait(sc, (u8*)TriggerMsg,
msgSize, DLG_SWDOWNLOAD_TRIGGER_DONE, FRONTEND_TIMEOUT);
free(TriggerMsg, M_USBDEV);
return (err);
}
/* Get device version information
The result is only used for information purpose ;
it is not (yet ?) used by the driver
XXX Seems to work on FAMILY2 devices only */
int