-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtk_coex.c
3035 lines (2584 loc) · 75.9 KB
/
rtk_coex.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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/dcache.h>
#include <linux/version.h>
#include <net/sock.h>
#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include <net/bluetooth/l2cap.h>
#include "rtk_coex.h"
/* Software coex message can be sent to and receive from WiFi driver by
* UDP socket or exported symbol */
/* #define RTK_COEX_OVER_SYMBOL */
#define HCI_VERSION_CODE LINUX_VERSION_CODE
#define RTK_VERSION "1.2"
#define RTKBT_DBG(fmt, arg...) printk(KERN_INFO "rtk_btcoex: " fmt "\n" , ## arg)
#define RTKBT_INFO(fmt, arg...) printk(KERN_INFO "rtk_btcoex: " fmt "\n" , ## arg)
#define RTKBT_WARN(fmt, arg...) printk(KERN_WARNING "rtk_btcoex: " fmt "\n", ## arg)
#define RTKBT_ERR(fmt, arg...) printk(KERN_WARNING "rtk_btcoex: " fmt "\n", ## arg)
static struct rtl_coex_struct btrtl_coex;
#ifdef RTB_SOFTWARE_MAILBOX
#ifdef RTK_COEX_OVER_SYMBOL
static struct sk_buff_head rtw_q;
static struct workqueue_struct *rtw_wq;
static struct work_struct rtw_work;
static u8 rtw_coex_on;
#endif
#endif
#define is_profile_connected(profile) ((btrtl_coex.profile_bitmap & BIT(profile)) > 0)
#define is_profile_busy(profile) ((btrtl_coex.profile_status & BIT(profile)) > 0)
#ifdef RTB_SOFTWARE_MAILBOX
static void rtk_handle_event_from_wifi(uint8_t * msg);
#endif
static int rtl_alloc_buff(struct rtl_coex_struct *coex)
{
struct rtl_hci_ev *ev;
struct rtl_l2_buff *l2;
int i;
int order;
unsigned long addr;
unsigned long addr2;
int ev_size;
int l2_size;
int n;
spin_lock_init(&coex->buff_lock);
INIT_LIST_HEAD(&coex->ev_used_list);
INIT_LIST_HEAD(&coex->ev_free_list);
INIT_LIST_HEAD(&coex->l2_used_list);
INIT_LIST_HEAD(&coex->l2_free_list);
n = NUM_RTL_HCI_EV * sizeof(struct rtl_hci_ev);
ev_size = ALIGN(n, sizeof(unsigned long));
n = L2_MAX_PKTS * sizeof(struct rtl_l2_buff);
l2_size = ALIGN(n, sizeof(unsigned long));
RTKBT_DBG("alloc buffers %d, %d for ev and l2", ev_size, l2_size);
order = get_order(ev_size + l2_size);
addr = __get_free_pages(GFP_KERNEL, order);
if (!addr) {
RTKBT_ERR("failed to alloc buffers for ev and l2.");
return -ENOMEM;
}
memset((void *)addr, 0, ev_size + l2_size);
coex->pages_addr = addr;
coex->buff_size = ev_size + l2_size;
ev = (struct rtl_hci_ev *)addr;
for (i = 0; i < NUM_RTL_HCI_EV; i++) {
list_add_tail(&ev->list, &coex->ev_free_list);
ev++;
}
addr2 = addr + ev_size;
l2 = (struct rtl_l2_buff *)addr2;
for (i = 0; i < L2_MAX_PKTS; i++) {
list_add_tail(&l2->list, &coex->l2_free_list);
l2++;
}
return 0;
}
static void rtl_free_buff(struct rtl_coex_struct *coex)
{
struct rtl_hci_ev *ev;
struct rtl_l2_buff *l2;
unsigned long flags;
spin_lock_irqsave(&coex->buff_lock, flags);
while (!list_empty(&coex->ev_used_list)) {
ev = list_entry(coex->ev_used_list.next, struct rtl_hci_ev,
list);
list_del(&ev->list);
}
while (!list_empty(&coex->ev_free_list)) {
ev = list_entry(coex->ev_free_list.next, struct rtl_hci_ev,
list);
list_del(&ev->list);
}
while (!list_empty(&coex->l2_used_list)) {
l2 = list_entry(coex->l2_used_list.next, struct rtl_l2_buff,
list);
list_del(&l2->list);
}
while (!list_empty(&coex->l2_free_list)) {
l2 = list_entry(coex->l2_free_list.next, struct rtl_l2_buff,
list);
list_del(&l2->list);
}
spin_unlock_irqrestore(&coex->buff_lock, flags);
if (coex->buff_size > 0) {
free_pages(coex->pages_addr, get_order(coex->buff_size));
coex->pages_addr = 0;
coex->buff_size = 0;
}
}
static struct rtl_hci_ev *rtl_ev_node_get(struct rtl_coex_struct *coex)
{
struct rtl_hci_ev *ev;
unsigned long flags;
if (!coex->buff_size)
return NULL;
spin_lock_irqsave(&coex->buff_lock, flags);
if (!list_empty(&coex->ev_free_list)) {
ev = list_entry(coex->ev_free_list.next, struct rtl_hci_ev,
list);
list_del(&ev->list);
} else
ev = NULL;
spin_unlock_irqrestore(&coex->buff_lock, flags);
return ev;
}
static int rtl_ev_node_to_used(struct rtl_coex_struct *coex,
struct rtl_hci_ev *ev)
{
unsigned long flags;
spin_lock_irqsave(&coex->buff_lock, flags);
list_add_tail(&ev->list, &coex->ev_used_list);
spin_unlock_irqrestore(&coex->buff_lock, flags);
return 0;
}
static struct rtl_l2_buff *rtl_l2_node_get(struct rtl_coex_struct *coex)
{
struct rtl_l2_buff *l2;
unsigned long flags;
if (!coex->buff_size)
return NULL;
spin_lock_irqsave(&coex->buff_lock, flags);
if(!list_empty(&coex->l2_free_list)) {
l2 = list_entry(coex->l2_free_list.next, struct rtl_l2_buff,
list);
list_del(&l2->list);
} else
l2 = NULL;
spin_unlock_irqrestore(&coex->buff_lock, flags);
return l2;
}
static int rtl_l2_node_to_used(struct rtl_coex_struct *coex,
struct rtl_l2_buff *l2)
{
unsigned long flags;
spin_lock_irqsave(&coex->buff_lock, flags);
list_add_tail(&l2->list, &coex->l2_used_list);
spin_unlock_irqrestore(&coex->buff_lock, flags);
return 0;
}
static int8_t psm_to_profile_index(uint16_t psm)
{
switch (psm) {
case PSM_AVCTP:
case PSM_SDP:
return -1; //ignore
case PSM_HID:
case PSM_HID_INT:
return profile_hid;
case PSM_AVDTP:
return profile_a2dp;
case PSM_PAN:
case PSM_OPP:
case PSM_FTP:
case PSM_BIP:
case PSM_RFCOMM:
return profile_pan;
default:
return profile_pan;
}
}
static rtk_prof_info *find_by_psm(u16 psm)
{
struct list_head *head = &btrtl_coex.profile_list;
struct list_head *iter = NULL;
struct list_head *temp = NULL;
rtk_prof_info *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_prof_info, list);
if (desc->psm == psm)
return desc;
}
return NULL;
}
static void rtk_check_setup_timer(int8_t profile_index)
{
if (profile_index == profile_a2dp) {
btrtl_coex.a2dp_packet_count = 0;
btrtl_coex.a2dp_count_timer.expires =
jiffies + msecs_to_jiffies(1000);
mod_timer(&btrtl_coex.a2dp_count_timer,
btrtl_coex.a2dp_count_timer.expires);
}
if (profile_index == profile_pan) {
btrtl_coex.pan_packet_count = 0;
btrtl_coex.pan_count_timer.expires =
jiffies + msecs_to_jiffies(1000);
mod_timer(&btrtl_coex.pan_count_timer,
btrtl_coex.pan_count_timer.expires);
}
/* hogp & voice share one timer now */
if ((profile_index == profile_hogp) || (profile_index == profile_voice)) {
if ((0 == btrtl_coex.profile_refcount[profile_hogp])
&& (0 == btrtl_coex.profile_refcount[profile_voice])) {
btrtl_coex.hogp_packet_count = 0;
btrtl_coex.voice_packet_count = 0;
btrtl_coex.hogp_count_timer.expires =
jiffies + msecs_to_jiffies(1000);
mod_timer(&btrtl_coex.hogp_count_timer,
btrtl_coex.hogp_count_timer.expires);
}
}
}
static void rtk_check_del_timer(int8_t profile_index)
{
if (profile_a2dp == profile_index) {
btrtl_coex.a2dp_packet_count = 0;
del_timer_sync(&btrtl_coex.a2dp_count_timer);
}
if (profile_pan == profile_index) {
btrtl_coex.pan_packet_count = 0;
del_timer_sync(&btrtl_coex.pan_count_timer);
}
if (profile_hogp == profile_index) {
btrtl_coex.hogp_packet_count = 0;
if (btrtl_coex.profile_refcount[profile_voice] == 0) {
del_timer_sync(&btrtl_coex.hogp_count_timer);
}
}
if (profile_voice == profile_index) {
btrtl_coex.voice_packet_count = 0;
if (btrtl_coex.profile_refcount[profile_hogp] == 0) {
del_timer_sync(&btrtl_coex.hogp_count_timer);
}
}
}
static rtk_conn_prof *find_connection_by_handle(struct rtl_coex_struct * coex,
uint16_t handle)
{
struct list_head *head = &coex->conn_hash;
struct list_head *iter = NULL, *temp = NULL;
rtk_conn_prof *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_conn_prof, list);
if ((handle & 0xEFF) == desc->handle) {
return desc;
}
}
return NULL;
}
static rtk_conn_prof *allocate_connection_by_handle(uint16_t handle)
{
rtk_conn_prof *phci_conn = NULL;
phci_conn = kmalloc(sizeof(rtk_conn_prof), GFP_ATOMIC);
if (phci_conn)
phci_conn->handle = handle;
return phci_conn;
}
static void init_connection_hash(struct rtl_coex_struct * coex)
{
struct list_head *head = &coex->conn_hash;
INIT_LIST_HEAD(head);
}
static void add_connection_to_hash(struct rtl_coex_struct * coex,
rtk_conn_prof * desc)
{
struct list_head *head = &coex->conn_hash;
list_add_tail(&desc->list, head);
}
static void delete_connection_from_hash(rtk_conn_prof * desc)
{
if (desc) {
list_del(&desc->list);
kfree(desc);
}
}
static void flush_connection_hash(struct rtl_coex_struct * coex)
{
struct list_head *head = &coex->conn_hash;
struct list_head *iter = NULL, *temp = NULL;
rtk_conn_prof *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_conn_prof, list);
if (desc) {
list_del(&desc->list);
kfree(desc);
}
}
//INIT_LIST_HEAD(head);
}
static void init_profile_hash(struct rtl_coex_struct * coex)
{
struct list_head *head = &coex->profile_list;
INIT_LIST_HEAD(head);
}
static uint8_t list_allocate_add(uint16_t handle, uint16_t psm,
int8_t profile_index, uint16_t dcid,
uint16_t scid)
{
rtk_prof_info *pprof_info = NULL;
if (profile_index < 0) {
RTKBT_ERR("PSM 0x%x do not need parse", psm);
return FALSE;
}
pprof_info = kmalloc(sizeof(rtk_prof_info), GFP_ATOMIC);
if (NULL == pprof_info) {
RTKBT_ERR("list_allocate_add: allocate error");
return FALSE;
}
/* Check if it is the second l2cap connection for a2dp
* a2dp signal channel will be created first than media channel.
*/
if (psm == PSM_AVDTP) {
rtk_prof_info *pinfo = find_by_psm(psm);
if (!pinfo) {
pprof_info->flags = A2DP_SIGNAL;
RTKBT_INFO("%s: Add a2dp signal channel", __func__);
} else {
pprof_info->flags = A2DP_MEDIA;
RTKBT_INFO("%s: Add a2dp media channel", __func__);
}
}
pprof_info->handle = handle;
pprof_info->psm = psm;
pprof_info->scid = scid;
pprof_info->dcid = dcid;
pprof_info->profile_index = profile_index;
list_add_tail(&(pprof_info->list), &(btrtl_coex.profile_list));
return TRUE;
}
static void delete_profile_from_hash(rtk_prof_info * desc)
{
RTKBT_DBG("Delete profile: hndl 0x%04x, psm 0x%04x, dcid 0x%04x, "
"scid 0x%04x", desc->handle, desc->psm, desc->dcid,
desc->scid);
if (desc) {
list_del(&desc->list);
kfree(desc);
desc = NULL;
}
}
static void flush_profile_hash(struct rtl_coex_struct * coex)
{
struct list_head *head = &coex->profile_list;
struct list_head *iter = NULL, *temp = NULL;
rtk_prof_info *desc = NULL;
spin_lock(&btrtl_coex.spin_lock_profile);
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_prof_info, list);
delete_profile_from_hash(desc);
}
//INIT_LIST_HEAD(head);
spin_unlock(&btrtl_coex.spin_lock_profile);
}
static rtk_prof_info *find_profile_by_handle_scid(struct rtl_coex_struct *
coex, uint16_t handle,
uint16_t scid)
{
struct list_head *head = &coex->profile_list;
struct list_head *iter = NULL, *temp = NULL;
rtk_prof_info *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_prof_info, list);
if (((handle & 0xFFF) == desc->handle) && (scid == desc->scid)) {
return desc;
}
}
return NULL;
}
static rtk_prof_info *find_profile_by_handle_dcid(struct rtl_coex_struct *
coex, uint16_t handle,
uint16_t dcid)
{
struct list_head *head = &coex->profile_list;
struct list_head *iter = NULL, *temp = NULL;
rtk_prof_info *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_prof_info, list);
if (((handle & 0xFFF) == desc->handle) && (dcid == desc->dcid)) {
return desc;
}
}
return NULL;
}
static rtk_prof_info *find_profile_by_handle_dcid_scid(struct rtl_coex_struct
* coex, uint16_t handle,
uint16_t dcid,
uint16_t scid)
{
struct list_head *head = &coex->profile_list;
struct list_head *iter = NULL, *temp = NULL;
rtk_prof_info *desc = NULL;
list_for_each_safe(iter, temp, head) {
desc = list_entry(iter, rtk_prof_info, list);
if (((handle & 0xFFF) == desc->handle) && (dcid == desc->dcid)
&& (scid == desc->scid)) {
return desc;
}
}
return NULL;
}
static void rtk_vendor_cmd_to_fw(uint16_t opcode, uint8_t parameter_len,
uint8_t * parameter)
{
int len = HCI_CMD_PREAMBLE_SIZE + parameter_len;
uint8_t *p;
struct sk_buff *skb;
struct hci_dev *hdev = btrtl_coex.hdev;
if (!hdev) {
RTKBT_ERR("No HCI device");
return;
} else if (!test_bit(HCI_UP, &hdev->flags)) {
RTKBT_WARN("HCI device is down");
return;
}
skb = bt_skb_alloc(len, GFP_ATOMIC);
if (!skb) {
RTKBT_DBG("there is no room for cmd 0x%x", opcode);
return;
}
p = (uint8_t *) skb_put(skb, HCI_CMD_PREAMBLE_SIZE);
UINT16_TO_STREAM(p, opcode);
*p++ = parameter_len;
if (parameter_len)
memcpy(skb_put(skb, parameter_len), parameter, parameter_len);
bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
#if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
#if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
bt_cb(skb)->opcode = opcode;
#else
bt_cb(skb)->hci.opcode = opcode;
#endif
#endif
/* Stand-alone HCI commands must be flagged as
* single-command requests.
*/
#if HCI_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
#if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
bt_cb(skb)->req.start = true;
#else
#if HCI_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
bt_cb(skb)->hci.req_start = true;
#else
bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
#endif
#endif /* 4.4.0 */
#endif /* 3.10.0 */
RTKBT_DBG("%s: opcode 0x%x", __func__, opcode);
/* It is harmless if set skb->dev twice. The dev will be used in
* btusb_send_frame() after or equal to kernel/hci 3.13.0,
* the hdev will not come from skb->dev. */
#if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
skb->dev = (void *)btrtl_coex.hdev;
#endif
/* Put the skb to the global hdev->cmd_q */
skb_queue_tail(&hdev->cmd_q, skb);
#if HCI_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
tasklet_schedule(&hdev->cmd_task);
#else
queue_work(hdev->workqueue, &hdev->cmd_work);
#endif
return;
}
static void rtk_notify_profileinfo_to_fw(void)
{
struct list_head *head = NULL;
struct list_head *iter = NULL;
struct list_head *temp = NULL;
rtk_conn_prof *hci_conn = NULL;
uint8_t handle_number = 0;
uint32_t buffer_size = 0;
uint8_t *p_buf = NULL;
uint8_t *p = NULL;
head = &btrtl_coex.conn_hash;
list_for_each_safe(iter, temp, head) {
hci_conn = list_entry(iter, rtk_conn_prof, list);
if (hci_conn && hci_conn->profile_bitmap)
handle_number++;
}
buffer_size = 1 + handle_number * 3 + 1;
p_buf = kmalloc(buffer_size, GFP_ATOMIC);
if (NULL == p_buf) {
RTKBT_ERR("%s: alloc error", __func__);
return;
}
p = p_buf;
RTKBT_DBG("%s: BufferSize %u", __func__, buffer_size);
*p++ = handle_number;
RTKBT_DBG("%s: NumberOfHandles %u", __func__, handle_number);
head = &btrtl_coex.conn_hash;
list_for_each(iter, head) {
hci_conn = list_entry(iter, rtk_conn_prof, list);
if (hci_conn && hci_conn->profile_bitmap) {
UINT16_TO_STREAM(p, hci_conn->handle);
RTKBT_DBG("%s: handle 0x%04x", __func__,
hci_conn->handle);
*p++ = hci_conn->profile_bitmap;
RTKBT_DBG("%s: profile_bitmap 0x%02x", __func__,
hci_conn->profile_bitmap);
handle_number--;
}
if (0 == handle_number)
break;
}
*p++ = btrtl_coex.profile_status;
RTKBT_DBG("%s: profile_status 0x%02x", __func__,
btrtl_coex.profile_status);
rtk_vendor_cmd_to_fw(HCI_VENDOR_SET_PROFILE_REPORT_COMMAND, buffer_size,
p_buf);
kfree(p_buf);
return;
}
static void update_profile_state(uint8_t profile_index, uint8_t is_busy)
{
uint8_t need_update = FALSE;
if ((btrtl_coex.profile_bitmap & BIT(profile_index)) == 0) {
RTKBT_ERR("%s: : ERROR!!! profile(Index: %x) does not exist",
__func__, profile_index);
return;
}
if (is_busy) {
if ((btrtl_coex.profile_status & BIT(profile_index)) == 0) {
need_update = TRUE;
btrtl_coex.profile_status |= BIT(profile_index);
}
} else {
if ((btrtl_coex.profile_status & BIT(profile_index)) > 0) {
need_update = TRUE;
btrtl_coex.profile_status &= ~(BIT(profile_index));
}
}
if (need_update) {
RTKBT_DBG("%s: btrtl_coex.profie_bitmap = %x",
__func__, btrtl_coex.profile_bitmap);
RTKBT_DBG("%s: btrtl_coex.profile_status = %x",
__func__, btrtl_coex.profile_status);
rtk_notify_profileinfo_to_fw();
}
}
static void update_profile_connection(rtk_conn_prof * phci_conn,
int8_t profile_index, uint8_t is_add)
{
uint8_t need_update = FALSE;
uint8_t kk;
RTKBT_DBG("%s: is_add %d, profile_index %x", __func__,
is_add, profile_index);
if (profile_index < 0)
return;
if (is_add) {
if (btrtl_coex.profile_refcount[profile_index] == 0) {
need_update = TRUE;
btrtl_coex.profile_bitmap |= BIT(profile_index);
/* SCO is always busy */
if (profile_index == profile_sco)
btrtl_coex.profile_status |=
BIT(profile_index);
rtk_check_setup_timer(profile_index);
}
btrtl_coex.profile_refcount[profile_index]++;
if (0 == phci_conn->profile_refcount[profile_index]) {
need_update = TRUE;
phci_conn->profile_bitmap |= BIT(profile_index);
}
phci_conn->profile_refcount[profile_index]++;
} else {
if (!btrtl_coex.profile_refcount[profile_index]) {
RTKBT_WARN("profile %u refcount is already zero",
profile_index);
return;
}
btrtl_coex.profile_refcount[profile_index]--;
RTKBT_DBG("%s: btrtl_coex.profile_refcount[%x] = %x",
__func__, profile_index,
btrtl_coex.profile_refcount[profile_index]);
if (btrtl_coex.profile_refcount[profile_index] == 0) {
need_update = TRUE;
btrtl_coex.profile_bitmap &= ~(BIT(profile_index));
/* if profile does not exist, status is meaningless */
btrtl_coex.profile_status &= ~(BIT(profile_index));
rtk_check_del_timer(profile_index);
}
phci_conn->profile_refcount[profile_index]--;
if (0 == phci_conn->profile_refcount[profile_index]) {
need_update = TRUE;
phci_conn->profile_bitmap &= ~(BIT(profile_index));
/* clear profile_hid_interval if need */
if ((profile_hid == profile_index)
&& (phci_conn->
profile_bitmap & (BIT(profile_hid_interval)))) {
phci_conn->profile_bitmap &=
~(BIT(profile_hid_interval));
btrtl_coex.
profile_refcount[profile_hid_interval]--;
}
}
}
RTKBT_DBG("%s: btrtl_coex.profile_bitmap 0x%02x", __func__,
btrtl_coex.profile_bitmap);
for (kk = 0; kk < 8; kk++)
RTKBT_DBG("%s: btrtl_coex.profile_refcount[%d] = %d",
__func__, kk,
btrtl_coex.profile_refcount[kk]);
if (need_update)
rtk_notify_profileinfo_to_fw();
}
static void update_hid_active_state(uint16_t handle, uint16_t interval)
{
uint8_t need_update = 0;
rtk_conn_prof *phci_conn =
find_connection_by_handle(&btrtl_coex, handle);
if (phci_conn == NULL)
return;
RTKBT_DBG("%s: handle 0x%04x, interval %u", __func__, handle, interval);
if (((phci_conn->profile_bitmap) & (BIT(profile_hid))) == 0) {
RTKBT_DBG("HID not connected, nothing to be down");
return;
}
if (interval < 60) {
if ((phci_conn->profile_bitmap & (BIT(profile_hid_interval))) ==
0) {
need_update = 1;
phci_conn->profile_bitmap |= BIT(profile_hid_interval);
btrtl_coex.profile_refcount[profile_hid_interval]++;
if (btrtl_coex.
profile_refcount[profile_hid_interval] == 1)
btrtl_coex.profile_status |=
BIT(profile_hid);
}
} else {
if ((phci_conn->profile_bitmap & (BIT(profile_hid_interval)))) {
need_update = 1;
phci_conn->profile_bitmap &=
~(BIT(profile_hid_interval));
btrtl_coex.profile_refcount[profile_hid_interval]--;
if (btrtl_coex.
profile_refcount[profile_hid_interval] == 0)
btrtl_coex.profile_status &=
~(BIT(profile_hid));
}
}
if (need_update)
rtk_notify_profileinfo_to_fw();
}
static uint8_t handle_l2cap_con_req(uint16_t handle, uint16_t psm,
uint16_t scid, uint8_t direction)
{
uint8_t status = FALSE;
rtk_prof_info *prof_info = NULL;
int8_t profile_index = psm_to_profile_index(psm);
if (profile_index < 0) {
RTKBT_DBG("PSM(0x%04x) do not need parse", psm);
return status;
}
spin_lock(&btrtl_coex.spin_lock_profile);
if (direction) //1: out
prof_info =
find_profile_by_handle_scid(&btrtl_coex, handle, scid);
else // 0:in
prof_info =
find_profile_by_handle_dcid(&btrtl_coex, handle, scid);
if (prof_info) {
RTKBT_DBG("%s: this profile is already exist!", __func__);
spin_unlock(&btrtl_coex.spin_lock_profile);
return status;
}
if (direction) //1: out
status = list_allocate_add(handle, psm, profile_index, 0, scid);
else // 0:in
status = list_allocate_add(handle, psm, profile_index, scid, 0);
spin_unlock(&btrtl_coex.spin_lock_profile);
if (!status)
RTKBT_ERR("%s: list_allocate_add failed!", __func__);
return status;
}
static uint8_t handle_l2cap_con_rsp(uint16_t handle, uint16_t dcid,
uint16_t scid, uint8_t direction,
uint8_t result)
{
rtk_prof_info *prof_info = NULL;
rtk_conn_prof *phci_conn = NULL;
spin_lock(&btrtl_coex.spin_lock_profile);
if (!direction) //0, in
prof_info =
find_profile_by_handle_scid(&btrtl_coex, handle, scid);
else //1, out
prof_info =
find_profile_by_handle_dcid(&btrtl_coex, handle, scid);
if (!prof_info) {
//RTKBT_DBG("handle_l2cap_con_rsp: prof_info Not Find!!");
spin_unlock(&btrtl_coex.spin_lock_profile);
return FALSE;
}
if (!result) { //success
RTKBT_DBG("l2cap connection success, update connection");
if (!direction) //0, in
prof_info->dcid = dcid;
else //1, out
prof_info->scid = dcid;
phci_conn = find_connection_by_handle(&btrtl_coex, handle);
if (phci_conn)
update_profile_connection(phci_conn,
prof_info->profile_index,
TRUE);
}
spin_unlock(&btrtl_coex.spin_lock_profile);
return TRUE;
}
static uint8_t handle_l2cap_discon_req(uint16_t handle, uint16_t dcid,
uint16_t scid, uint8_t direction)
{
rtk_prof_info *prof_info = NULL;
rtk_conn_prof *phci_conn = NULL;
RTKBT_DBG("%s: handle 0x%04x, dcid 0x%04x, scid 0x%04x, dir %u",
__func__, handle, dcid, scid, direction);
spin_lock(&btrtl_coex.spin_lock_profile);
if (!direction) //0: in
prof_info =
find_profile_by_handle_dcid_scid(&btrtl_coex, handle,
scid, dcid);
else //1: out
prof_info =
find_profile_by_handle_dcid_scid(&btrtl_coex, handle,
dcid, scid);
if (!prof_info) {
//LogMsg("handle_l2cap_discon_req: prof_info Not Find!");
spin_unlock(&btrtl_coex.spin_lock_profile);
return 0;
}
phci_conn = find_connection_by_handle(&btrtl_coex, handle);
if (!phci_conn) {
spin_unlock(&btrtl_coex.spin_lock_profile);
return 0;
}
update_profile_connection(phci_conn, prof_info->profile_index, FALSE);
if (prof_info->profile_index == profile_a2dp &&
(phci_conn->profile_bitmap & BIT(profile_sink)))
update_profile_connection(phci_conn, profile_sink, FALSE);
delete_profile_from_hash(prof_info);
spin_unlock(&btrtl_coex.spin_lock_profile);
return 1;
}
static const char sample_freqs[4][8] = {
"16", "32", "44.1", "48"
};
static const uint8_t sbc_blocks[4] = { 4, 8, 12, 16 };
static const char chan_modes[4][16] = {
"MONO", "DUAL_CHANNEL", "STEREO", "JOINT_STEREO"
};
static const char alloc_methods[2][12] = {
"LOUDNESS", "SNR"
};
static const uint8_t subbands[2] = { 4, 8 };
void print_sbc_header(struct sbc_frame_hdr *hdr)
{
RTKBT_DBG("syncword: %02x", hdr->syncword);
RTKBT_DBG("freq %skHz", sample_freqs[hdr->sampling_frequency]);
RTKBT_DBG("blocks %u", sbc_blocks[hdr->blocks]);
RTKBT_DBG("channel mode %s", chan_modes[hdr->channel_mode]);
RTKBT_DBG("allocation method %s",
alloc_methods[hdr->allocation_method]);
RTKBT_DBG("subbands %u", subbands[hdr->subbands]);
}
static void packets_count(uint16_t handle, uint16_t scid, uint16_t length,
uint8_t direction, u8 *user_data)
{
rtk_prof_info *prof_info = NULL;
rtk_conn_prof *hci_conn =
find_connection_by_handle(&btrtl_coex, handle);
if (NULL == hci_conn)
return;
if (0 == hci_conn->type) {
if (!direction) //0: in
prof_info =
find_profile_by_handle_scid(&btrtl_coex, handle,
scid);
else //1: out
prof_info =
find_profile_by_handle_dcid(&btrtl_coex, handle,
scid);
if (!prof_info) {
//RTKBT_DBG("packets_count: prof_info Not Find!");
return;
}
/* avdtp media data */
if (prof_info->profile_index == profile_a2dp &&
prof_info->flags == A2DP_MEDIA) {
if (!is_profile_busy(profile_a2dp)) {
struct sbc_frame_hdr *sbc_header;
struct rtp_header *rtph;
u8 bitpool;
update_profile_state(profile_a2dp, TRUE);
if (!direction) {
if (!(hci_conn->profile_bitmap & BIT(profile_sink))) {
btrtl_coex.profile_bitmap |= BIT(profile_sink);
hci_conn->profile_bitmap |= BIT(profile_sink);
update_profile_connection(hci_conn, profile_sink, 1);
}
update_profile_state(profile_sink, TRUE);
}
/* We assume it is SBC if the packet length
* is bigger than 100 bytes
*/
if (length > 100) {
RTKBT_INFO("Length %u", length);
rtph = (struct rtp_header *)user_data;
RTKBT_DBG("rtp: v %u, cc %u, pt %u",
rtph->v, rtph->cc, rtph->pt);
/* move forward */
user_data += sizeof(struct rtp_header) +
rtph->cc * 4 + 1;
/* point to the sbc frame header */
sbc_header = (struct sbc_frame_hdr *)user_data;
bitpool = sbc_header->bitpool;
print_sbc_header(sbc_header);
RTKBT_DBG("bitpool %u", bitpool);
rtk_vendor_cmd_to_fw(HCI_VENDOR_SET_BITPOOL,
1, &bitpool);
}
}