-
Notifications
You must be signed in to change notification settings - Fork 0
/
hciattach_rtk.c
2021 lines (1750 loc) · 46.4 KB
/
hciattach_rtk.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
/*
* Copyright (C) 2013 Realtek Semiconductor Corp.
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <endian.h>
#include <byteswap.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include "rtb_fwc.h"
#include "hciattach.h"
#include "hciattach_h4.h"
#define RTK_VERSION "3.1.0099684.20181218-165126"
#define TIMESTAMP_PR
#define MAX_EVENTS 10
/* #define SERIAL_NONBLOCK_READ */
#ifdef SERIAL_NONBLOCK_READ
#define FD_BLOCK 0
#define FD_NONBLOCK 1
#endif
/* #define RTL_8703A_SUPPORT */
/* #define RTL8723DSH4_UART_HWFLOWC */ /* 8723DS H4 special */
uint8_t DBG_ON = 1;
#define HCI_EVENT_HDR_SIZE 2
/* #define RTK_PATCH_LENGTH_MAX 24576 */ //24*1024
#define RTB_PATCH_LENGTH_MAX (40 * 1024)
#define PATCH_DATA_FIELD_MAX_SIZE 252
#define HCI_CMD_READ_BD_ADDR 0x1009
#define HCI_VENDOR_CHANGE_BAUD 0xfc17
#define HCI_VENDOR_READ_ROM_VER 0xfc6d
#define HCI_CMD_READ_LOCAL_VER 0x1001
#define HCI_VENDOR_READ_CHIP_TYPE 0xfc61
#define HCI_CMD_RESET 0x0c03
/* HCI data types */
#define H5_ACK_PKT 0x00
#define HCI_COMMAND_PKT 0x01
#define HCI_ACLDATA_PKT 0x02
#define HCI_SCODATA_PKT 0x03
#define HCI_EVENT_PKT 0x04
#define H5_VDRSPEC_PKT 0x0E
#define H5_LINK_CTL_PKT 0x0F
#define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
#define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
#define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
#define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
#define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
#define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4))
#define H5_HDR_SIZE 4
struct sk_buff {
uint32_t max_len;
uint32_t data_len;
uint8_t *data;
};
struct hci_ev_cmd_complete {
uint8_t ncmd;
uint16_t opcode;
} __attribute__ ((packed));
#define OP_H5_SYNC 0x01
#define OP_H5_CONFIG 0x02
#define OP_ROM_VER ((1 << 24) | HCI_VENDOR_READ_ROM_VER)
#define OP_LMP_VER ((1 << 24) | HCI_CMD_READ_LOCAL_VER)
#define OP_CHIP_TYPE ((1 << 24) | HCI_VENDOR_READ_CHIP_TYPE)
#define OP_SET_BAUD ((1 << 24) | HCI_VENDOR_CHANGE_BAUD)
#define OP_HCI_RESET ((1 << 24) | HCI_CMD_RESET)
struct rtb_struct rtb_cfg;
/* bite reverse in bytes
* 00000001 -> 10000000
* 00000100 -> 00100000
*/
const uint8_t byte_rev_table[256] = {
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
static __inline uint8_t bit_rev8(uint8_t byte)
{
return byte_rev_table[byte];
}
static __inline uint16_t bit_rev16(uint16_t x)
{
return (bit_rev8(x & 0xff) << 8) | bit_rev8(x >> 8);
}
static const uint16_t crc_table[] = {
0x0000, 0x1081, 0x2102, 0x3183,
0x4204, 0x5285, 0x6306, 0x7387,
0x8408, 0x9489, 0xa50a, 0xb58b,
0xc60c, 0xd68d, 0xe70e, 0xf78f
};
/* Initialise the crc calculator */
#define H5_CRC_INIT(x) x = 0xffff
static __inline struct sk_buff *skb_alloc(unsigned int len)
{
struct sk_buff *skb = NULL;
if ((skb = malloc(len + sizeof(*skb)))) {
skb->max_len = len;
skb->data_len = 0;
skb->data = ((uint8_t *)skb) + sizeof(*skb);
} else {
RS_ERR("Allocate skb fails!");
skb = NULL;
return NULL;
}
memset(skb->data, 0, len);
return skb;
}
static __inline void skb_free(struct sk_buff *skb)
{
free(skb);
return;
}
/*
* Add data to a buffer
* This function extends the used data area of the buffer.
*/
static uint8_t *skb_put(struct sk_buff *skb, uint32_t len)
{
uint32_t old_len = skb->data_len;
if ((skb->data_len + len) > (skb->max_len)) {
RS_ERR("Buffer too small");
exit(EXIT_FAILURE);
}
skb->data_len += len;
return (skb->data + old_len);
}
/*
* Remove end from a buffer
* Cut the length of a buffer down by removing data from the tail
*/
static void skb_trim(struct sk_buff *skb, uint32_t len)
{
if (skb->data_len > len) {
skb->data_len = len;
} else {
RS_ERR("Trim error, data_len %u < len %u", skb->data_len, len);
}
}
/*
* Remove data from the start of a buffer
* This function removes data from the start of a buffer.
* A pointer to the next data in the buffer is returned
*/
static uint8_t *skb_pull(struct sk_buff *skb, uint32_t len)
{
if (len > skb->data_len) {
RS_ERR("Pull error, data_len %u < len %u", skb->data_len, len);
exit(EXIT_FAILURE);
}
skb->data_len -= len;
skb->data += len;
return skb->data;
}
/**
* Add "d" into crc scope, caculate the new crc value
*
* @param crc crc data
* @param d one byte data
*/
static void h5_crc_update(uint16_t * crc, uint8_t d)
{
uint16_t reg = *crc;
reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
*crc = reg;
}
struct __una_u16 {
uint16_t x;
};
static __inline uint16_t __get_unaligned_cpu16(const void *p)
{
const struct __una_u16 *ptr = (const struct __una_u16 *)p;
return ptr->x;
}
static __inline uint16_t get_unaligned_be16(const void *p)
{
return __get_unaligned_cpu16((const uint8_t *)p);
}
/*
* Get crc data.
*/
static uint16_t h5_get_crc(struct rtb_struct * h5)
{
uint16_t crc = 0;
uint8_t *data = h5->rx_skb->data + h5->rx_skb->data_len - 2;
crc = data[1] + (data[0] << 8);
return crc;
/* return get_unaligned_be16(&h5->rx_skb->data[h5->rx_skb->data_len - 2]); */
}
/*
* Add 0xc0 to buffer.
*/
static void h5_slip_msgdelim(struct sk_buff *skb)
{
const char pkt_delim = 0xc0;
memcpy(skb_put(skb, 1), &pkt_delim, 1);
}
/*
* Encode one byte in h5 proto
* 0xc0 -> 0xdb, 0xdc
* 0xdb -> 0xdb, 0xdd
* 0x11 -> 0xdb, 0xde
* 0x13 -> 0xdb, 0xdf
* others will not change
*/
static void h5_slip_one_byte(struct sk_buff *skb, uint8_t c)
{
const uint8_t esc_c0[2] = { 0xdb, 0xdc };
const uint8_t esc_db[2] = { 0xdb, 0xdd };
const uint8_t esc_11[2] = { 0xdb, 0xde };
const uint8_t esc_13[2] = { 0xdb, 0xdf };
switch (c) {
case 0xc0:
memcpy(skb_put(skb, 2), &esc_c0, 2);
break;
case 0xdb:
memcpy(skb_put(skb, 2), &esc_db, 2);
break;
case 0x11:
memcpy(skb_put(skb, 2), &esc_11, 2);
break;
case 0x13:
memcpy(skb_put(skb, 2), &esc_13, 2);
break;
default:
memcpy(skb_put(skb, 1), &c, 1);
break;
}
}
/*
* Decode one byte in h5 proto
* 0xdb, 0xdc -> 0xc0
* 0xdb, 0xdd -> 0xdb
* 0xdb, 0xde -> 0x11
* 0xdb, 0xdf -> 0x13
* others will not change
*/
static void h5_unslip_one_byte(struct rtb_struct * h5, unsigned char byte)
{
const uint8_t c0 = 0xc0, db = 0xdb;
const uint8_t oof1 = 0x11, oof2 = 0x13;
if (H5_ESCSTATE_NOESC == h5->rx_esc_state) {
if (0xdb == byte) {
h5->rx_esc_state = H5_ESCSTATE_ESC;
} else {
memcpy(skb_put(h5->rx_skb, 1), &byte, 1);
/* Check Pkt Header's CRC enable bit */
if ((h5->rx_skb->data[0] & 0x40) != 0 &&
h5->rx_state != H5_W4_CRC) {
h5_crc_update(&h5->message_crc, byte);
}
h5->rx_count--;
}
} else if (H5_ESCSTATE_ESC == h5->rx_esc_state) {
switch (byte) {
case 0xdc:
memcpy(skb_put(h5->rx_skb, 1), &c0, 1);
if ((h5->rx_skb->data[0] & 0x40) != 0 &&
h5->rx_state != H5_W4_CRC)
h5_crc_update(&h5->message_crc, 0xc0);
h5->rx_esc_state = H5_ESCSTATE_NOESC;
h5->rx_count--;
break;
case 0xdd:
memcpy(skb_put(h5->rx_skb, 1), &db, 1);
if ((h5->rx_skb->data[0] & 0x40) != 0 &&
h5->rx_state != H5_W4_CRC)
h5_crc_update(&h5->message_crc, 0xdb);
h5->rx_esc_state = H5_ESCSTATE_NOESC;
h5->rx_count--;
break;
case 0xde:
memcpy(skb_put(h5->rx_skb, 1), &oof1, 1);
if ((h5->rx_skb->data[0] & 0x40) != 0 &&
h5->rx_state != H5_W4_CRC)
h5_crc_update(&h5->message_crc, oof1);
h5->rx_esc_state = H5_ESCSTATE_NOESC;
h5->rx_count--;
break;
case 0xdf:
memcpy(skb_put(h5->rx_skb, 1), &oof2, 1);
if ((h5->rx_skb->data[0] & 0x40) != 0 &&
h5->rx_state != H5_W4_CRC)
h5_crc_update(&h5->message_crc, oof2);
h5->rx_esc_state = H5_ESCSTATE_NOESC;
h5->rx_count--;
break;
default:
RS_ERR("Error: Invalid byte %02x after esc byte", byte);
skb_free(h5->rx_skb);
h5->rx_skb = NULL;
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_count = 0;
break;
}
}
}
/*
* Prepare h5 packet
* Refer to Core Spec Vol 4, Part D
* Three-wire UART Transport Layer: 4 PACKET HEADER
*/
static struct sk_buff *h5_prepare_pkt(struct rtb_struct * h5, uint8_t *data,
int len, int pkt_type)
{
struct sk_buff *nskb;
uint8_t hdr[4];
uint16_t H5_CRC_INIT(h5_txmsg_crc);
int rel, i;
switch (pkt_type) {
case HCI_ACLDATA_PKT:
case HCI_COMMAND_PKT:
case HCI_EVENT_PKT:
rel = 1; /* reliable */
break;
case H5_ACK_PKT:
case H5_VDRSPEC_PKT:
case H5_LINK_CTL_PKT:
rel = 0; /* unreliable */
break;
default:
RS_ERR("Unknown packet type");
return NULL;
}
/* Max len of packet: (len + 4(h5 hdr) + 2(crc))*2
* Because bytes 0xc0 and 0xdb are escaped, worst case is that the
* packet is only made of 0xc0 and 0xdb
* The additional 2-octets are 0xc0 delimiters at start and end of each
* packet.
*/
nskb = skb_alloc((len + 6) * 2 + 2);
if (!nskb)
return NULL;
/* Add SLIP start byte: 0xc0 */
h5_slip_msgdelim(nskb);
/* Set ack number in SLIP header */
hdr[0] = h5->rxseq_txack << 3;
h5->is_txack_req = 0;
/* RS_DBG("Request packet no(%u) to card", h5->rxseq_txack); */
/* RS_DBG("Sending packet with seqno %u and wait %u", h5->msgq_txseq,
* h5->rxseq_txack);
*/
if (rel) {
/* Set reliable bit and seq number */
hdr[0] |= 0x80 + h5->msgq_txseq;
/* RS_DBG("Sending packet with seqno(%u)", h5->msgq_txseq); */
++(h5->msgq_txseq);
h5->msgq_txseq = (h5->msgq_txseq) & 0x07;
}
/* Set DIC Present bit */
if (h5->use_crc)
hdr[0] |= 0x40;
/* Set packet type and payload length */
hdr[1] = ((len << 4) & 0xff) | pkt_type;
hdr[2] = (uint8_t) (len >> 4);
/* Set header checksum */
hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
/* Encode h5 header */
for (i = 0; i < 4; i++) {
h5_slip_one_byte(nskb, hdr[i]);
if (h5->use_crc)
h5_crc_update(&h5_txmsg_crc, hdr[i]);
}
/* Encode payload */
for (i = 0; i < len; i++) {
h5_slip_one_byte(nskb, data[i]);
if (h5->use_crc)
h5_crc_update(&h5_txmsg_crc, data[i]);
}
/* Encode CRC */
if (h5->use_crc) {
h5_txmsg_crc = bit_rev16(h5_txmsg_crc);
h5_slip_one_byte(nskb, (uint8_t) ((h5_txmsg_crc >> 8) & 0x00ff));
h5_slip_one_byte(nskb, (uint8_t) (h5_txmsg_crc & 0x00ff));
}
/* Add 0xc0 at the end of the packet */
h5_slip_msgdelim(nskb);
return nskb;
}
/*
* Remove controller acked packet from host unacked lists
*/
/* static void h5_remove_acked_pkt(struct rtb_struct * h5)
* {
* int pkts_to_be_removed = 0;
* int seqno = 0;
* int i = 0;
*
* seqno = h5->msgq_txseq;
* // pkts_to_be_removed = GetListLength(h5->unacked);
*
* while (pkts_to_be_removed) {
* if (h5->rxack == seqno)
* break;
*
* pkts_to_be_removed--;
* seqno = (seqno - 1) & 0x07;
* }
*
* if (h5->rxack != seqno) {
* RS_DBG("Peer acked invalid packet");
* }
* // skb_queue_walk_safe(&h5->unack, skb, tmp)
* // remove ack'ed packet from h5->unack queue
* for (i = 0; i < 5; ++i) {
* if (i >= pkts_to_be_removed)
* break;
* i++;
* //__skb_unlink(skb, &h5->unack);
* //skb_free(skb);
* }
*
* // if (skb_queue_empty(&h5->unack))
* // del_timer(&h5->th5);
* // spin_unlock_irqrestore(&h5->unack.lock, flags);
*
* if (i != pkts_to_be_removed)
* RS_DBG("Removed only (%u) out of (%u) pkts", i,
* pkts_to_be_removed);
* }
*/
/*
* Send host ack.
*/
static void rtb_send_ack(int fd)
{
int len;
struct sk_buff *nskb = h5_prepare_pkt(&rtb_cfg, NULL, 0, H5_ACK_PKT);
len = write(fd, nskb->data, nskb->data_len);
if (len != nskb->data_len)
RS_ERR("Write pure ack fails");
skb_free(nskb);
return;
}
/*
* Parse hci command complete event in h5 init state.
*/
static void h5_init_hci_cc(struct sk_buff *skb)
{
struct hci_ev_cmd_complete *ev = NULL;
uint16_t opcode = 0;
uint8_t status = 0;
skb_pull(skb, HCI_EVENT_HDR_SIZE);
ev = (struct hci_ev_cmd_complete *)skb->data;
opcode = le16_to_cpu(ev->opcode);
RS_DBG("Receive cmd complete event of command: %04x", opcode);
skb_pull(skb, sizeof(struct hci_ev_cmd_complete));
status = skb->data[0];
if (status) {
RS_ERR("status is %u for cmd %04x", status, opcode);
return;
}
if (rtb_cfg.cmd_state.opcode != opcode) {
RS_ERR("%s: Received unexpected cc for cmd %04x, %04x of cc",
__func__, rtb_cfg.cmd_state.opcode, opcode);
return;
}
rtb_cfg.cmd_state.state = CMD_STATE_SUCCESS;
switch (opcode) {
case HCI_VENDOR_CHANGE_BAUD:
RS_INFO("Received cc of vendor change baud");
break;
case HCI_CMD_READ_BD_ADDR:
RS_INFO("BD Address: %02x:%02x:%02x:%02x:%02x:%02x",
skb->data[5], skb->data[4], skb->data[3],
skb->data[2], skb->data[1], skb->data[0]);
break;
case HCI_CMD_READ_LOCAL_VER:
rtb_cfg.hci_ver = skb->data[1];
rtb_cfg.hci_rev = (skb->data[2] | skb->data[3] << 8);
rtb_cfg.lmp_subver = (skb->data[7] | (skb->data[8] << 8));
RS_INFO("HCI Version 0x%02x", rtb_cfg.hci_ver);
RS_INFO("HCI Revision 0x%04x", rtb_cfg.hci_rev);
RS_INFO("LMP Subversion 0x%04x", rtb_cfg.lmp_subver);
break;
case HCI_VENDOR_READ_ROM_VER:
rtb_cfg.eversion = skb->data[1];
RS_INFO("Read ROM version %02x", rtb_cfg.eversion);
break;
case HCI_VENDOR_READ_CHIP_TYPE:
rtb_cfg.chip_type = (skb->data[1] & 0x0f);
RS_INFO("Read chip type %02x", rtb_cfg.chip_type);
break;
default:
return;
}
/* Count the cmd num for makeing the seq number aligned */
rtb_cfg.num_of_cmd_sent++;
}
/*
* Parse hci command complete event in h5 post state.
*/
static void h5_post_hci_cc(struct sk_buff *skb)
{
struct hci_ev_cmd_complete *ev = NULL;
uint16_t opcode = 0;
uint8_t status = 0;
skb_pull(skb, HCI_EVENT_HDR_SIZE);
ev = (struct hci_ev_cmd_complete *)skb->data;
opcode = le16_to_cpu(ev->opcode);
RS_DBG("Receive cmd complete event of command: %04x", opcode);
skb_pull(skb, sizeof(struct hci_ev_cmd_complete));
status = skb->data[0];
if (status) {
RS_ERR("status is %u for cmd %04x", status, opcode);
return;
}
if (rtb_cfg.cmd_state.opcode != opcode) {
RS_ERR("%s: Received unexpected cc for cmd %04x, %04x of cc",
__func__, rtb_cfg.cmd_state.opcode, opcode);
return;
}
rtb_cfg.cmd_state.state = CMD_STATE_SUCCESS;
switch (opcode) {
case HCI_CMD_RESET:
RS_INFO("Received cc of hci reset cmd");
rtb_cfg.link_estab_state = H5_ACTIVE;
break;
default:
break;
}
}
/*
* Process a hci frame
*/
static void hci_recv_frame(struct sk_buff *skb)
{
if (rtb_cfg.link_estab_state == H5_INIT) {
if (skb->data[0] == 0x0e)
h5_init_hci_cc(skb);
/*
* rtb_send_ack(rtb_cfg.serial_fd);
* usleep(10000);
* rtb_send_ack(rtb_cfg.serial_fd);
*/
} else if (rtb_cfg.link_estab_state == H5_PATCH) {
if (skb->data[0] != 0x0e) {
RS_INFO("Received event 0x%x during download patch",
skb->data[0]);
return;
}
rtb_cfg.rx_index = skb->data[6];
/* RS_INFO("rx_index %d", rtb_cfg.rx_index); */
/* Download fw/config done */
if (rtb_cfg.rx_index & 0x80) {
rtb_cfg.rx_index &= ~0x80;
rtb_cfg.link_estab_state = H5_HCI_RESET;
}
} else if (rtb_cfg.link_estab_state == H5_HCI_RESET) {
if (skb->data[0] == 0x0e)
h5_post_hci_cc(skb);
} else {
RS_ERR("receive packets in active state");
}
}
static void h5_handle_internal_rx(struct sk_buff *skb)
{
int len;
uint8_t sync_req[2] = { 0x01, 0x7E };
uint8_t sync_resp[2] = { 0x02, 0x7D };
uint8_t sync_resp_pkt[0x8] = {
0xc0, 0x00, 0x2F, 0x00, 0xD0, 0x02, 0x7D, 0xc0
};
uint8_t conf_req[2] = { 0x03, 0xFC };
uint8_t conf_resp[2] = { 0x04, 0x7B };
uint8_t conf_resp_pkt[0x8] = {
0xc0, 0x00, 0x2F, 0x00, 0xD0, 0x04, 0x7B, 0xc0
};
if (rtb_cfg.link_estab_state == H5_SYNC) {
if (!memcmp(skb->data, sync_req, 2)) {
RS_INFO("[SYNC] Get SYNC Pkt\n");
len = write(rtb_cfg.serial_fd, sync_resp_pkt, 0x8);
if (len != 0x08)
RS_ERR("Send h5 sync resp error, %s",
strerror(errno));
} else if (!memcmp(skb->data, sync_resp, 2)) {
RS_INFO("[SYNC] Get SYNC Resp Pkt");
rtb_cfg.link_estab_state = H5_CONFIG;
}
} else if (rtb_cfg.link_estab_state == H5_CONFIG) {
if (!memcmp(skb->data, sync_req, 0x2)) {
RS_INFO("[CONFIG] Get SYNC pkt");
len = write(rtb_cfg.serial_fd, sync_resp_pkt, 0x8);
if (len != 0x08)
RS_ERR("Send h5 sync resp error, %s",
strerror(errno));
} else if (!memcmp(skb->data, conf_req, 0x2)) {
RS_INFO("[CONFIG] Get CONFG pkt");
len = write(rtb_cfg.serial_fd, conf_resp_pkt, 0x8);
if (len != 0x08)
RS_ERR("Send h5 sync resp to ctl error, %s",
strerror(errno));
} else if (!memcmp(skb->data, conf_resp, 0x2)) {
RS_INFO("[CONFIG] Get CONFG resp pkt");
/* Change state to H5_INIT after receiving a conf resp
*/
rtb_cfg.link_estab_state = H5_INIT;
if (skb->data_len > 2) {
rtb_cfg.use_crc = ((skb->data[2]) >> 4) & 0x01;
RS_INFO("dic is %u, cfg field 0x%02x",
rtb_cfg.use_crc, skb->data[2]);
}
} else {
RS_WARN("[CONFIG] Get unknown pkt");
rtb_send_ack(rtb_cfg.serial_fd);
}
}
}
/*
* Process the received complete h5 packet
*/
static void h5_complete_rx_pkt(struct rtb_struct *h5)
{
int pass_up = 1;
uint8_t *h5_hdr = NULL;
h5_hdr = (uint8_t *) (h5->rx_skb->data);
if (H5_HDR_RELIABLE(h5_hdr)) {
/* RS_DBG("Received reliable seqno %u from card", h5->rxseq_txack);
*/
h5->rxseq_txack = H5_HDR_SEQ(h5_hdr) + 1;
/* h5->rxseq_txack %= 8; */
h5->rxseq_txack &= 0x07;
h5->is_txack_req = 1;
}
h5->rxack = H5_HDR_ACK(h5_hdr);
switch (H5_HDR_PKT_TYPE(h5_hdr)) {
case HCI_ACLDATA_PKT:
case HCI_EVENT_PKT:
case HCI_COMMAND_PKT:
/* h5_remove_acked_pkt(h5); */
pass_up = 1;
break;
case HCI_SCODATA_PKT:
pass_up = 1;
break;
case H5_LINK_CTL_PKT:
pass_up = 0;
skb_pull(h5->rx_skb, H5_HDR_SIZE);
h5_handle_internal_rx(h5->rx_skb);
break;
default: /* Pure ack or other unexpected pkt */
pass_up = 0;
break;
}
if (pass_up) {
skb_pull(h5->rx_skb, H5_HDR_SIZE);
hci_recv_frame(h5->rx_skb);
}
if (h5->is_txack_req) {
rtb_send_ack(rtb_cfg.serial_fd);
h5->is_txack_req = 0;
}
skb_free(h5->rx_skb);
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_skb = NULL;
}
/*
* Parse the receive data in h5 proto.
*/
static int h5_recv(struct rtb_struct *h5, void *data, int count)
{
unsigned char *ptr;
ptr = (unsigned char *)data;
while (count) {
if (h5->rx_count) {
if (*ptr == 0xc0) {
RS_ERR("Short h5 packet");
skb_free(h5->rx_skb);
h5->rx_state = H5_W4_PKT_START;
h5->rx_count = 0;
} else
h5_unslip_one_byte(h5, *ptr);
ptr++;
count--;
continue;
}
switch (h5->rx_state) {
case H5_W4_HDR:
/* Check header checksum */
if ((0xff & (uint8_t)~(h5->rx_skb->data[0] + h5->rx_skb->data[1] +
h5->rx_skb->data[2])) != h5->rx_skb->data[3]) {
RS_ERR("h5 hdr checksum error");
skb_free(h5->rx_skb);
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_count = 0;
continue;
}
/* The received seq number is unexpected */
if (h5->rx_skb->data[0] & 0x80 &&
(h5->rx_skb->data[0] & 0x07) != h5->rxseq_txack) {
RS_ERR("Out-of-order packet arrived, got(%u)expected(%u)",
h5->rx_skb->data[0] & 0x07,
h5->rxseq_txack);
h5->is_txack_req = 1;
skb_free(h5->rx_skb);
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_count = 0;
/* Depend on whether Controller will reset ack
* number or not
*/
if (rtb_cfg.tx_index == rtb_cfg.total_num)
rtb_cfg.rxseq_txack =
h5->rx_skb->data[0] & 0x07;
continue;
}
h5->rx_state = H5_W4_DATA;
h5->rx_count =
(h5->rx_skb->data[1] >> 4) +
(h5->rx_skb->data[2] << 4);
continue;
case H5_W4_DATA:
/* Packet with crc */
if (h5->rx_skb->data[0] & 0x40) {
h5->rx_state = H5_W4_CRC;
h5->rx_count = 2;
} else {
h5_complete_rx_pkt(h5);
}
continue;
case H5_W4_CRC:
if (bit_rev16(h5->message_crc) != h5_get_crc(h5)) {
RS_ERR("Checksum failed, computed %04x received %04x",
bit_rev16(h5->message_crc),
h5_get_crc(h5));
skb_free(h5->rx_skb);
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_count = 0;
continue;
}
skb_trim(h5->rx_skb, h5->rx_skb->data_len - 2);
h5_complete_rx_pkt(h5);
continue;
case H5_W4_PKT_DELIMITER:
switch (*ptr) {
case 0xc0:
h5->rx_state = H5_W4_PKT_START;
break;
default:
break;
}
ptr++;
count--;
break;
case H5_W4_PKT_START:
switch (*ptr) {
case 0xc0:
ptr++;
count--;
break;
default:
h5->rx_state = H5_W4_HDR;
h5->rx_count = 4;
h5->rx_esc_state = H5_ESCSTATE_NOESC;
H5_CRC_INIT(h5->message_crc);
/* Do not increment ptr or decrement count
* Allocate packet. Max len of a H5 pkt=
* 0xFFF (payload) +4 (header) +2 (crc)
*/
h5->rx_skb = skb_alloc(0x1005);
if (!h5->rx_skb) {
RS_ERR("Can't alloc skb for new pkt");
h5->rx_state = H5_W4_PKT_DELIMITER;
h5->rx_count = 0;
return 0;
}
break;
}
break;
default:
break;
}
}
return count;
}
static const char *op_string(uint32_t op)
{
switch (op) {
case OP_SET_BAUD:
return "OP_SET_BAUD";
case OP_H5_SYNC:
return "OP_H5_SYNC";
case OP_H5_CONFIG:
return "OP_H5_CONFIG";
case OP_HCI_RESET:
return "OP_HCI_RESET";
case OP_CHIP_TYPE:
return "OP_CHIP_TYPE";
case OP_ROM_VER:
return "OP_ROM_VER";
case OP_LMP_VER:
return "OP_LMP_VER";
default:
return "OP_UNKNOWN";
}
}
static int start_transmit_wait(int fd, struct sk_buff *skb,
uint32_t op, unsigned int msec, int retry)
{
unsigned char buf[128];
ssize_t result;
struct iovec iov;
ssize_t ret;
uint8_t *data;
int len;
int op_result = -1;
uint64_t expired;
int n;
struct epoll_event events[MAX_EVENTS];
int nfds;
uint16_t opcode = 0;
if (fd == -1 || !skb) {
RS_ERR("Invalid parameter");
return -1;
}
data = skb->data;
len = skb->data_len;
if (op & (1 << 24)) {
opcode = (op & 0xffff);
if (opcode != rtb_cfg.cmd_state.opcode ||