-
Notifications
You must be signed in to change notification settings - Fork 6
/
rtbth_core_us.c
2110 lines (1730 loc) · 61.9 KB
/
rtbth_core_us.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
/*
*************************************************************************
* Ralink Technology Corporation
* 5F., No. 5, Taiyuan 1st St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2012, Ralink Technology Corporation
*
* 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************/
#include "rt_linux.h"
#include "rtbt_ctrl.h"
#include "rtbth_us.h"
#include "hps_bluez.h"
RTBTH_ADAPTER *gpAd = 0;
static int pollm = 1;
static int evt_wq_flag = 0;
static int sync_wq_flag = 0;
static int bind_wq_flag = 0;
static DECLARE_WAIT_QUEUE_HEAD(evt_wq);
static DECLARE_WAIT_QUEUE_HEAD(sync_wq);
//static unsigned int evt; // input buffer of read()
#define RB_SIZE(prb) ((prb)->size)
#define RB_MASK(prb) (RB_SIZE(prb) - 1)
#define RB_COUNT(prb) ((prb)->write - (prb)->read)
#define RB_FULL(prb) (RB_COUNT(prb) >= RB_SIZE(prb))
#define RB_EMPTY(prb) ((prb)->write == (prb)->read)
#define RB_INIT(prb, qsize) \
{ \
(prb)->read = (prb)->write = 0; \
(prb)->size = (qsize); \
}
#define RB_PUT(prb, value) \
{ \
if (!RB_FULL( prb )) { \
(prb)->queue[ (prb)->write & RB_MASK(prb) ] = value; \
++((prb)->write); \
} \
else { \
\
} \
}
#define RB_GET(prb, value) \
{ \
if (!RB_EMPTY(prb)) { \
value = (prb)->queue[ (prb)->read & RB_MASK(prb) ]; \
++((prb)->read); \
if (RB_EMPTY(prb)) { \
(prb)->read = (prb)->write = 0; \
} \
} \
else { \
value = NULL; \
} \
}
#define _osal_inline_ inline
typedef enum e_logical_link_index {
INQUIRY_LOGICAL_LINK = 0,
INQUIRY_SCAN_LOGICAL_LINK = 1,
ASBU_LOGICAL_LINK = 2,
PSBU_LOGICAL_LINK = 3,
PSBC_LOGICAL_LINK = 4,
SYNC_LOGICAL_LINK = 5,
ACLU_LOGICAL_LINK = SYNC_LOGICAL_LINK + 3,
ACLC_LOGICAL_LINK = ACLU_LOGICAL_LINK + 7,
STANDBY_LOGICAL_LINK = ACLC_LOGICAL_LINK + 7,
LE_SCAN_LOGICAL_LINK = STANDBY_LOGICAL_LINK + 1,
LE_ACL_LOGICAL_LINK = LE_SCAN_LOGICAL_LINK + 1,
TOTAL_NUM_OF_LLINKS = LE_ACL_LOGICAL_LINK + 3,
INVALID_LOGICAL_LINK = 0xFF
} LLIDX;
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
UINT32 osal_op_get_id(P_OSAL_OP pOp)
{
return (pOp) ? pOp->op.opId : 0xFFFFFFFF;
}
int osal_unsleepable_lock_init (P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_lock_init(&(pUSL->lock));
return 0;
}
int osal_lock_unsleepable_lock (P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_lock_irqsave(&(pUSL->lock), pUSL->flag);
return 0;
}
int osal_unlock_unsleepable_lock (P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_unlock_irqrestore(&(pUSL->lock), pUSL->flag);
return 0;
}
extern int osal_unsleepable_lock_deinit (P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
return 0;
}
/*
*OSAL layer Thread Opeartion releated APIs
*
*
*/
_osal_inline_ int
osal_thread_create (
P_OSAL_THREAD pThread
)
{
pThread->pThread = kthread_create(pThread->pThreadFunc,
pThread->pThreadData,
pThread->threadName);
if (NULL == pThread->pThread) {
return -1;
}
return 0;
}
_osal_inline_ int
osal_thread_run (
P_OSAL_THREAD pThread
)
{
if (pThread->pThread) {
wake_up_process(pThread->pThread);
return 0;
}
else {
return -1;
}
}
_osal_inline_ int
osal_thread_stop (
P_OSAL_THREAD pThread
)
{
int iRet;
if ( (pThread) && (pThread->pThread) ) {
iRet = kthread_stop(pThread->pThread);
//pThread->pThread = NULL;
return iRet;
}
return -1;
}
_osal_inline_ int
osal_thread_should_stop (
P_OSAL_THREAD pThread
)
{
if ( (pThread) && (pThread->pThread) ) {
return kthread_should_stop();
}
else {
return 1;
}
}
typedef unsigned int (*P_OSAL_EVENT_CHECKER)(P_OSAL_THREAD pThread);
_osal_inline_ int
osal_thread_wait_for_event (
P_OSAL_THREAD pThread,
P_OSAL_EVENT pEvent,
P_OSAL_EVENT_CHECKER pChecker
)
{
if ( (pThread) && (pThread->pThread) && (pEvent) && (pChecker)) {
/* pDevWmt = (P_DEV_WMT)(pThread->pThreadData);*/
return wait_event_interruptible(pEvent->waitQueue,
(/*!RB_EMPTY(&pDevWmt->rActiveOpQ) ||*/ osal_thread_should_stop(pThread) || (*pChecker)(pThread)));
}
return -1;
}
_osal_inline_ int
osal_thread_destroy (
P_OSAL_THREAD pThread
)
{
if (pThread && (pThread->pThread)) {
kthread_stop(pThread->pThread);
pThread->pThread = NULL;
}
return 0;
}
/*
*OSAL layer Signal Opeartion releated APIs
*initialization
*wait for signal
*wait for signal timerout
*raise signal
*destroy a signal
*
*/
_osal_inline_ int
osal_signal_init (
P_OSAL_SIGNAL pSignal
)
{
if (pSignal) {
init_completion(&pSignal->comp);
return 0;
}
else {
return -1;
}
}
_osal_inline_ int
osal_wait_for_signal (
P_OSAL_SIGNAL pSignal
)
{
if (pSignal) {
wait_for_completion_interruptible(&pSignal->comp);
return 0;
}
else {
return -1;
}
}
_osal_inline_ int
osal_wait_for_signal_timeout (
P_OSAL_SIGNAL pSignal
)
{
/* return wait_for_completion_interruptible_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue));*/
/* [ChangeFeature][George] gps driver may be closed by -ERESTARTSYS.
* Avoid using *interruptible" version in order to complete our jobs, such
* as function off gracefully.
*/
return wait_for_completion_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue));
}
_osal_inline_ int
osal_raise_signal (
P_OSAL_SIGNAL pSignal
)
{
// TODO:[FixMe][GeorgeKuo]: DO sanity check here!!!
complete(&pSignal->comp);
return 0;
}
_osal_inline_ int
osal_signal_deinit (
P_OSAL_SIGNAL pSignal
)
{
// TODO:[FixMe][GeorgeKuo]: DO sanity check here!!!
pSignal->timeoutValue = 0;
return 0;
}
/*
*OSAL layer Event Opeartion releated APIs
*initialization
*wait for signal
*wait for signal timerout
*raise signal
*destroy a signal
*
*/
int osal_event_init (
P_OSAL_EVENT pEvent
)
{
init_waitqueue_head(&pEvent->waitQueue);
return 0;
}
int osal_wait_for_event(
P_OSAL_EVENT pEvent,
int (*condition)(PVOID),
void *cond_pa
)
{
return wait_event_interruptible(pEvent->waitQueue, condition(cond_pa));
}
int osal_wait_for_event_timeout(
P_OSAL_EVENT pEvent,
int (*condition)(PVOID),
void *cond_pa
)
{
return wait_event_interruptible_timeout(pEvent->waitQueue, condition(cond_pa), msecs_to_jiffies(pEvent->timeoutValue));
}
int osal_trigger_event(
P_OSAL_EVENT pEvent
)
{
int ret = 0;
wake_up_interruptible(&pEvent->waitQueue);
return ret;
}
int
osal_event_deinit (
P_OSAL_EVENT pEvent
)
{
return 0;
}
int osal_op_is_wait_for_signal(P_OSAL_OP pOp)
{
return (pOp && pOp->signal.timeoutValue) ? 1 : 0;
}
void osal_op_raise_signal(P_OSAL_OP pOp, int result)
{
if (pOp)
{
pOp->result = result;
osal_raise_signal(&pOp->signal);
}
}
static int _rtbth_us_handler(RTBTH_ADAPTER *pAd, P_STP_OP pStpOp)
{
int ret = -1;
RTBTH_EVENT_T evt = 0;
static int in=0;
if (NULL == pStpOp){
return -1;
}
switch(pStpOp->opId) {
case BZ_HCI_EVENT:
case BZ_ACL_EVENT:
case BZ_SCO_EVENT:
case INT_RX_EVENT:
case INT_TX_EVENT:
case INT_MCUCMD_EVENT:
case INT_MCUEVT_EVENT:
case INT_LMPTMR_EVENT:
case INT_LMPERR_EVENT:
case RTBTH_TEST_EVENT:
DebugPrint(TRACE, DBG_INIT,"Event =%d, jiffies = %d, evt_fifo_len = %d, in=%d\n",
pStpOp->opId, jiffies, kfifo_len(pAd->evt_fifo), ++in);
if(kfifo_avail(pAd->evt_fifo) >= sizeof(RTBTH_EVENT_T)){
evt = pStpOp->opId;
kfifo_in(pAd->evt_fifo, &evt , sizeof(RTBTH_EVENT_T));
// if(kfifo_len(pAd->evt_fifo) == sizeof(RTBTH_EVENT_T)){
// if(kfifo_len(pAd->evt_fifo) > 0){
evt_wq_flag = 1;
wake_up_interruptible(&evt_wq);
// }
ret = 0;
} else {
DebugPrint(TRACE, DBG_INIT,"evt_fifo size is not available!\n");
ret = -1;
}
break;
case INIT_COREINIT_EVENT:
case INIT_COREDEINIT_EVENT:
case INIT_CORESTART_EVENT:
case INIT_CORESTOP_EVENT:
case INIT_EPINIT_EVENT:
DebugPrint(LOUD, DBG_INIT,"Event =%d, jiffies = %d, evt_fifo_len = %d, in=%d\n",
pStpOp->opId, jiffies, kfifo_len(pAd->evt_fifo), ++in);
if(kfifo_avail(pAd->evt_fifo) >= sizeof(RTBTH_EVENT_T)){
evt = pStpOp->opId;
kfifo_in(pAd->evt_fifo, &evt , sizeof(RTBTH_EVENT_T));
// if(kfifo_len(pAd->evt_fifo) == sizeof(RTBTH_EVENT_T)){
// if(kfifo_len(pAd->evt_fifo) > 0){
evt_wq_flag = 1;
wake_up_interruptible(&evt_wq);
// }
ret = 0;
} else {
DebugPrint(TRACE, DBG_INIT,"evt_fifo size is not available!\n");
ret = -1;
}
DebugPrint(TRACE, DBG_INIT,"wait for the event complete\n");
// msleep(1000);
sync_wq_flag = 0;
ret = wait_event_interruptible_timeout(sync_wq, sync_wq_flag != 0, HZ);
if(!ret)
DebugPrint(TRACE, DBG_INIT,"wait for sync timeout\n");
break;
case RTBTH_EXIT:
ret = 0;
break;
default:
DebugPrint(TRACE, DBG_INIT,"invalid operation id (%d)\n", pStpOp->opId);
ret = -1;
break;
}
return ret;
}
static P_OSAL_OP _rtbth_us_get_op (
RTBTH_ADAPTER *pAd,
P_OSAL_OP_Q pOpQ
)
{
P_OSAL_OP pOp;
if (!pOpQ)
{
DebugPrint(TRACE, DBG_INIT,"pOpQ == NULL\n");
return NULL;
}
osal_lock_unsleepable_lock(&(pAd->wq_spinlock));
/* acquire lock success */
RB_GET(pOpQ, pOp);
osal_unlock_unsleepable_lock(&(pAd->wq_spinlock));
if (!pOp)
{
DebugPrint(TRACE, DBG_INIT,"RB_GET fail\n");
}
return pOp;
}
static int _rtbth_us_put_op (RTBTH_ADAPTER *pAd, P_OSAL_OP_Q pOpQ, P_OSAL_OP pOp){
int ret;
if (!pOpQ || !pOp){
DebugPrint(TRACE, DBG_INIT,"pOpQ = 0x%p, pLxOp = 0x%p \n", pOpQ, pOp);
return 0;
}
ret = 0;
osal_lock_unsleepable_lock(&(pAd->wq_spinlock));
if (!RB_FULL(pOpQ)){
RB_PUT(pOpQ, pOp);
} else {
ret = -1;
}
osal_unlock_unsleepable_lock(&(pAd->wq_spinlock));
if (ret){
DebugPrint(TRACE, DBG_INIT,"RB_FULL, RB_COUNT=%d , RB_SIZE=%d\n",RB_COUNT(pOpQ), RB_SIZE(pOpQ));
return 0;
} else {
return 1;
}
}
P_OSAL_OP _rtbth_us_get_free_op (RTBTH_ADAPTER *pAd){
P_OSAL_OP pOp;
if (pAd){
pOp = _rtbth_us_get_op(pAd, &pAd->rFreeOpQ);
if (pOp){
memset(&pOp->op, 0, sizeof(pOp->op));
}
return pOp;
} else {
return NULL;
}
}
int _rtbth_us_put_act_op (RTBTH_ADAPTER *pAd, P_OSAL_OP pOp)
{
int bRet = 0;//MTK_WCN_BOOL_FALSE;
int bCleanup = 0;//MTK_WCN_BOOL_FALSE;
int wait_ret = -1;
P_OSAL_SIGNAL pSignal = NULL;
do {
if (!pAd || !pOp) {
DebugPrint(ERROR, DBG_INIT,"pAd = %p, pOp = %p\n", pAd, pOp);
break;
}
pSignal = &pOp->signal;
if (pSignal->timeoutValue){
pOp->result = -9;
osal_signal_init(&pOp->signal);
}
/* put to active Q */
bRet = _rtbth_us_put_op(pAd, &pAd->rActiveOpQ, pOp);
if(0 == bRet){
DebugPrint(TRACE, DBG_INIT,"+++++++++++ Put op Active queue Fail\n");
bCleanup = 1;//MTK_WCN_BOOL_TRUE;
break;
}
/* wake up wmtd */
osal_trigger_event(&pAd->STPd_event);
if (pSignal->timeoutValue == 0) {
bRet = 1;//MTK_WCN_BOOL_TRUE;
/* clean it in wmtd */
break;
}
/* wait result, clean it here */
bCleanup = 1;//MTK_WCN_BOOL_TRUE;
/* check result */
wait_ret = osal_wait_for_signal_timeout(&pOp->signal);
DebugPrint(TRACE, DBG_INIT,"wait completion:%d\n", wait_ret);
if (!wait_ret){
DebugPrint(TRACE, DBG_INIT,"wait completion timeout \n");
// TODO: how to handle it? retry?
} else {
if (pOp->result)
{
DebugPrint(TRACE, DBG_INIT,"op(%d) result:%d\n", pOp->op.opId, pOp->result);
}
/* op completes, check result */
bRet = (pOp->result) ? 0 : 1;
}
} while(0);
if (bCleanup) {
/* put Op back to freeQ */
bRet = _rtbth_us_put_op(pAd, &pAd->rFreeOpQ, pOp);
if(bRet == 0){
DebugPrint(TRACE, DBG_INIT,"+++++++++++ Put op active free fail, maybe disable/enable psm\n");
}
}
return bRet;
}
static int _rtbth_us_wait_for_evt(void *pvData)
{
RTBTH_ADAPTER *pAd = (RTBTH_ADAPTER *)pvData;
DebugPrint(LOUD, DBG_INIT,"%s: pAd->rActiveOpQ = %d\n", __func__, RB_COUNT(&pAd->rActiveOpQ));
return ((!RB_EMPTY(&pAd->rActiveOpQ)) || osal_thread_should_stop(&pAd->PSMd));
}
INT32 _rtbth_us_event_notification(RTBTH_ADAPTER *pAd, RTBTH_EVENT_T evt)
{
P_OSAL_OP pOp;
int bRet;
int retval;
if(!pAd){
return -1;
}
pOp = _rtbth_us_get_free_op(pAd);
if (!pOp)
{
DebugPrint(ERROR, DBG_INIT,"get_free_lxop fail \n");
return -1;
}
pOp->op.opId = (unsigned int) evt;
pOp->signal.timeoutValue = 0;
bRet = _rtbth_us_put_act_op(pAd, pOp);
DebugPrint(LOUD, DBG_INIT,"OPID(%d) type(%d) bRet(%d) \n\n",
pOp->op.opId,
pOp->op.au4OpData[0],
bRet);
retval = (0 == bRet) ? (-1) : 0;
return retval;
}
static int _rtbth_us_proc (void *pvData)
{
RTBTH_ADAPTER *pAd = (RTBTH_ADAPTER *)pvData;
P_OSAL_OP pOp;
unsigned int id;
int ret;
if (!pAd) {
DebugPrint(TRACE, DBG_INIT,"!pAd\n");
return -1;
}
DebugPrint(TRACE, DBG_INIT,"rtbth_us starts running: pWmtDev(0x%p) [pol, rt_pri, n_pri, pri]=[%d, %d, %d, %d] \n",
pAd, current->policy, current->rt_priority, current->normal_prio, current->prio);
for (;;) {
pOp = NULL;
osal_wait_for_event(&pAd->STPd_event,
_rtbth_us_wait_for_evt,
(void *)pAd);
if (osal_thread_should_stop(&pAd->PSMd))
{
DebugPrint(TRACE, DBG_INIT,"should stop now... \n");
// TODO: clean up active opQ
break;
}
/* get Op from activeQ */
pOp = _rtbth_us_get_op(pAd, &pAd->rActiveOpQ);
if (!pOp)
{
DebugPrint(TRACE, DBG_INIT,"+++++++++++ Get op from activeQ fail, maybe disable/enable psm\n");
continue;
}
id = osal_op_get_id(pOp);
if (id >= RTBTH_EVENT_NUM)
{
DebugPrint(TRACE, DBG_INIT,"abnormal opid id: 0x%x \n", id);
ret = -1;
goto handler_done;
}
ret = _rtbth_us_handler(pAd, &pOp->op);
handler_done:
if (ret)
{
// DebugPrint(TRACE, DBG_INIT,"opid id(0x%x)(%s) error(%d)\n", id, (id >= 4)?("???"):(g_psm_op_name[id]), ret);
}
if (osal_op_is_wait_for_signal(pOp))
{
osal_op_raise_signal(pOp, ret);
}
else
{
/* put Op back to freeQ */
if(_rtbth_us_put_op(pAd, &pAd->rFreeOpQ, pOp) == 0)
{
DebugPrint(TRACE, DBG_INIT,"+++++++++++ Put op to FreeOpQ fail, maybe disable/enable psm\n");
}
}
if (RTBTH_EXIT == id)
{
break;
}
}
DebugPrint(TRACE, DBG_INIT,"exits \n");
return 0;
};
static atomic_t rtbth_us_avail = ATOMIC_INIT(1);
static int rtbth_us_open(struct inode *inode, struct file *file){
if(!capable(CAP_SYS_ADMIN))
return -EPERM;
if(!atomic_dec_and_test(&rtbth_us_avail)){
atomic_inc(&rtbth_us_avail);
DebugPrint(WARNING, DBG_INIT,"rtbth occupied: rtbth_us_avail=%d\n",
atomic_read(&rtbth_us_avail)
);
return -EBUSY;
}
DebugPrint(TRACE, DBG_INIT,"%s: major %d minor %d (pid %d)\n", __func__,
imajor(inode),
iminor(inode),
current->pid
);
bind_wq_flag = 1;
#if 0
/* Init the host protocol stack hooking interface */
if (rtbt_hps_iface_init(RAL_INF_PCI, gpAd->os_ctrl->if_dev , gpAd->os_ctrl)){
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_init fail\n");
} else {
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_init ok\n");
}
#endif
/* Link the host protocol stack interface to the protocl stack */
if (rtbt_hps_iface_attach(gpAd->os_ctrl)){
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_attach fail\n");
} else {
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_attach ok\n");
}
return 0;
}
static int rtbth_us_close(struct inode *inode, struct file *file){
if(!capable(CAP_SYS_ADMIN))
return -EPERM;
atomic_inc(&rtbth_us_avail);
DebugPrint(TRACE, DBG_INIT,"%s: major %d minor %d (pid %d)\n", __func__,
imajor(inode),
iminor(inode),
current->pid
);
bind_wq_flag = 0;
if(rtbt_hps_iface_detach(gpAd->os_ctrl)){
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_detach fail\n");
} else {
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_detach ok\n");
}
#if 0
if(rtbt_hps_iface_deinit(RAL_INF_PCI, gpAd->os_ctrl->if_dev, gpAd->os_ctrl)){
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_deinit fail\n");
} else {
DebugPrint(ERROR, DBG_INIT,"rtbt_hps_iface_deinit ok\n");
}
#endif
return 0;
}
#if 1
ssize_t rtbth_us_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos){
ssize_t retval = 0;
RTBTH_EVENT_T evt = 0;
int wait_ret = 0;
// down(&rd_mtx);
if(!capable(CAP_SYS_ADMIN))
return -EPERM;
while(1){
if(kfifo_len(gpAd->evt_fifo) > 0){
kfifo_out(gpAd->evt_fifo, &evt, sizeof(RTBTH_EVENT_T));
DebugPrint(TRACE, DBG_INIT,"%s: event fifo len = %d\n", __func__, kfifo_len(gpAd->evt_fifo));
break;
} else {
if(pollm){
retval = -EAGAIN;
break;
} else {
DebugPrint(TRACE, DBG_INIT,"%s: wait for the event, pending on kernel space\n", __func__);
wait_ret = wait_event_interruptible_timeout(evt_wq, evt_wq_flag != 0, HZ);
if (wait_ret) {
if (-ERESTARTSYS == wait_ret) {
DebugPrint(TRACE, DBG_INIT,"%s: signaled by -ERESTARTSYS(%ld) \n ", __func__, wait_ret);
return -EAGAIN;
}
else {
DebugPrint(TRACE, DBG_INIT,"%s: signaled by %ld \n ", __func__, wait_ret);
}
} else {
DebugPrint(INFO, DBG_INIT,"%s: wait timeout %d\n ", __func__, wait_ret);
retval = -EFAULT;
goto out;
}
evt_wq_flag = 0;
DebugPrint(TRACE, DBG_INIT,"%s: receive the event, return to user space\n", __func__);
}
}
}
if(retval == -EAGAIN)
goto out;
if (copy_to_user(buf, &evt, sizeof(evt)))
{
retval = -EFAULT;
goto out;
}
retval = sizeof(RTBTH_EVENT_T);
out:
// up(&rd_mtx);
return retval;
}
#else
ssize_t rtbth_us_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos){
ssize_t retval = 0;
RTBTH_EVENT_T evt = 0;
int wait_ret = 0;
static int entries = 0;
static int out=0;
entries = 0;
if(!capable(CAP_SYS_ADMIN))
return -EPERM;
while(1){
if(kfifo_len(gpAd->evt_fifo) > 0){
kfifo_out(gpAd->evt_fifo, &evt, sizeof(RTBTH_EVENT_T));
DebugPrint(TRACE, DBG_INIT,"%s: remaining event fifo len = %d, out=%d\n", __func__, kfifo_len(gpAd->evt_fifo), ++out);
break;
} else {
DebugPrint(TRACE, DBG_INIT,"%s: wait for the event, pending on kernel space, %d, kfifo_len(evt)=%d\n",
__func__, entries++, kfifo_len(gpAd->evt_fifo));
// wait_ret = wait_event_interruptible(evt_wq, evt_wq_flag != 0);
wait_ret = wait_event_interruptible_timeout(evt_wq, evt_wq_flag != 0, HZ);
DebugPrint(INFO, DBG_INIT,"%s: wait event returned value %d\n ", __func__, wait_ret);
evt_wq_flag = 0;
if (wait_ret) {
if (-ERESTARTSYS == wait_ret) {
DebugPrint(TRACE, DBG_INIT,"%s: signaled by -ERESTARTSYS(%ld) \n ", __func__, wait_ret);
return -EFAULT;
}
else {
DebugPrint(TRACE, DBG_INIT,"%s: signaled by %ld \n ", __func__, wait_ret);
}
} else {
DebugPrint(INFO, DBG_INIT,"%s: wait timeout %d\n ", __func__, wait_ret);
retval = -EFAULT;
goto out;
}
}
}
if (copy_to_user(buf, &evt, sizeof(RTBTH_EVENT_T)))
{
retval = -EFAULT;
goto out;
}
DebugPrint(TRACE, DBG_INIT,"return type=%d, return to user space\n", evt);
retval = sizeof(RTBTH_EVENT_T);
out:
return retval;
}
#endif
ssize_t rtbth_us_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos){
if(!capable(CAP_SYS_ADMIN))
return -EPERM;
return 0;
}
#if 0
#define RTBTH_US_IOC_MAGIC 'k'
#define RTBTH_IOCPCI32WRITE _IOWR(RTBTH_US_IOC_MAGIC, 1, int)
#define RTBTH_IOCPCI32READ _IOWR(RTBTH_US_IOC_MAGIC, 2, int)
#define RTBTH_IOCMCUWRITE _IOWR(RTBTH_US_IOC_MAGIC, 3, int)
#define RTBTH_IOCMCUREAD _IOWR(RTBTH_US_IOC_MAGIC, 4, int)
#define RTBTH_IOCBBWRITE _IOWR(RTBTH_US_IOC_MAGIC, 5, int)
#define RTBTH_IOCBBREAD _IOWR(RTBTH_US_IOC_MAGIC, 6, int)
#define RTBTH_IOCRFWRITE _IOWR(RTBTH_US_IOC_MAGIC, 7, int)
#define RTBTH_IOCRFREAD _IOWR(RTBTH_US_IOC_MAGIC, 8, int)
#define RTBTH_IOCBZWRITE _IOWR(RTBTH_US_IOC_MAGIC, 9, int)
#define RTBTH_IOCBZREAD _IOWR(RTBTH_US_IOC_MAGIC,10, int)
#define RTBTH_IOCTRCTRL _IOWR(RTBTH_US_IOC_MAGIC,11, int)
#define RTBTH_IOCSYNC _IOWR(RTBTH_US_IOC_MAGIC,12, int)
#define RTBTH_IOCTST _IOWR(RTBTH_US_IOC_MAGIC,13, int)
#define RTBTH_IOCRMODE _IOWR(RTBTH_US_IOC_MAGIC,14, int)
#define RTBTH_IOC_MAXNR 15
#endif
VOID
MCU_Read_Memory(
IN PRTBTH_ADAPTER pAd,
IN UINT32 Address,
OUT PUINT8 pBuffer,
IN UINT16 Length)
{
INT32 i, j, k;
UINT32 Val = 0;
// Only read shared memory at the word boundary
RTBT_ASSERT((Address % 4) == 0);
i = Length / 4;
k = Length % 4;
for (j = 0; j < i; j++)
{
RT_IO_READ32(pAd, Address+(j*4), &Val);
pBuffer[j*4] = (UINT8)Val;
pBuffer[j*4+1] = (UINT8)(Val >> 8);
pBuffer[j*4+2] = (UINT8)(Val >> 16);
pBuffer[j*4+3] = (UINT8)(Val >> 24);
}
if (k > 0)
{
RT_IO_READ32(pAd, Address+(i*4), &Val);
for (j = 0; j < k; j++)
{
pBuffer[(i*4)+j] = (UINT8)(Val >> (8 * j));
}
}
}
VOID
MCU_Write_Memory(
IN PRTBTH_ADAPTER pAd,
IN UINT32 Address,
IN PUINT8 pBuffer,
IN UINT16 Length)
{
INT32 i, j, k;
UINT32 Val;
//DBGPRINT(DBG_NIC_TRACE,( "RtbtWriteMcuMemory: Addr=0x%0x4, Len=%d\n", Address, Length));
// Only write shared memory at the word boundary
RTBT_ASSERT((Address % 4) == 0);
i = Length / 4;
k = Length % 4;
for (j = 0; j < i; j++)
{
Val = ( (UINT32)pBuffer[j*4])
| (((UINT32)pBuffer[(j*4)+1]) << 8)
| (((UINT32)pBuffer[(j*4)+2]) << 16)
| (((UINT32)pBuffer[(j*4)+3]) << 24);
RT_IO_WRITE32(pAd, Address+(j*4), Val);
}
if (k > 0)
{
RT_IO_READ32(pAd, Address+(i*4), &Val);
for (j = 0; j < k; j++)
{
Val &= ~(0xff << (8 * j));
Val += ((UINT32)pBuffer[(i*4)+j]) << (8 * j);
}
RT_IO_WRITE32(pAd, Address+(i*4), Val);
}
}
VOID
PDMA_Transmit_TxRing(
IN PRTBTH_ADAPTER pAd,
IN UINT8 LLIdx,
IN UINT8 RingIdx,
IN UINT8 PktType,
IN UINT8 Llid,
IN UINT8 PFlow,
IN UINT16 Tag,
IN PUINT8 pPacket,
IN UINT16 Length,
IN BOOLEAN AutoFlushable,
IN UINT8 PktSeq,
IN UINT8 ChSel,
IN UINT8 CodeType)
{
PUCHAR pData;
ULONG TxCpuIdx;
PTXD_STRUC pTxD;
PTXBI_STRUC pTxBI;
PRT_TX_RING pTxRing;
NATIVE_BT_CLK_STRUC NativeBtClk;
UINT8 TxRingSize = BthGetTxRingSize(pAd, RingIdx);
NativeBtClk.word = 0;