mtk_btif.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #ifndef __MTK_BTIF_H_
  2. #define __MTK_BTIF_H_
  3. #include <linux/version.h>
  4. #include <linux/module.h>
  5. #include <linux/types.h>
  6. #include <linux/mutex.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/atomic.h>
  9. #include <linux/list.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/rt.h>
  14. #include <linux/kthread.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/fs.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/string.h>
  20. #include <linux/time.h> /* gettimeofday */
  21. #include <asm-generic/bug.h>
  22. #include "btif_pub.h"
  23. #include "btif_dma_pub.h"
  24. #include "mtk_btif_exp.h"
  25. #define BTIF_PORT_NR 1
  26. #define BTIF_USER_NAME_MAX_LEN 32
  27. /*-------------Register Defination Start ---------------*/
  28. #if (defined(CONFIG_MTK_GMO_RAM_OPTIMIZE) && !defined(CONFIG_MT_ENG_BUILD))
  29. #define BTIF_RX_BUFFER_SIZE (1024 * 32)
  30. #else
  31. #define BTIF_RX_BUFFER_SIZE (1024 * 64)
  32. #endif
  33. #define BTIF_TX_FIFO_SIZE (1024 * 4)
  34. /*------------Register Defination End ----------------*/
  35. /*------------BTIF Module Clock and Power Control Defination---------------*/
  36. typedef enum _ENUM_BTIF_RX_TYPE_ {
  37. BTIF_IRQ_CTX = 0,
  38. BTIF_TASKLET_CTX = BTIF_IRQ_CTX + 1,
  39. BTIF_THREAD_CTX = BTIF_TASKLET_CTX + 1,
  40. BTIF_WQ_CTX = BTIF_THREAD_CTX + 1,
  41. BTIF_RX_TYPE_MAX,
  42. } ENUM_BTIF_RX_TYPE;
  43. typedef enum _ENUM_BTIF_TX_TYPE_ {
  44. BTIF_TX_USER_CTX = 0,
  45. BTIF_TX_SINGLE_CTX = BTIF_TX_USER_CTX + 1,
  46. BTIF_TX_TYPE_MAX,
  47. } ENUM_BTIF_TX_TYPE;
  48. typedef enum _ENUM_BTIF_STATE_ {
  49. B_S_OFF = 0,
  50. B_S_SUSPEND = B_S_OFF + 1,
  51. B_S_DPIDLE = B_S_SUSPEND + 1,
  52. B_S_ON = B_S_DPIDLE + 1,
  53. B_S_MAX,
  54. } ENUM_BTIF_STATE;
  55. #define ENABLE_BTIF_RX_DMA 1
  56. #define ENABLE_BTIF_TX_DMA 1
  57. #ifdef ENABLE_BTIF_TX_DMA
  58. #define BTIF_TX_MODE BTIF_MODE_DMA
  59. #else
  60. #define BTIF_TX_MODE BTIF_MODE_PIO
  61. #endif
  62. #ifdef ENABLE_BTIF_RX_DMA
  63. #define BTIF_RX_MODE BTIF_MODE_DMA
  64. #else
  65. #define BTIF_RX_MODE BTIF_MODE_PIO
  66. #endif
  67. #define BTIF_RX_BTM_CTX BTIF_THREAD_CTX/*BTIF_WQ_CTX*//* BTIF_TASKLET_CTX */
  68. /*-- cannot be used because ,
  69. mtk_wcn_stp_parser data will call *(stp_if_tx) to send ack,
  70. in which context sleepable lock or usleep operation may be used,
  71. these operation is not allowed in tasklet, may cause schedule_bug*/
  72. #define BTIF_TX_CTX BTIF_TX_USER_CTX /* BTIF_TX_SINGLE_CTX */
  73. #define ENABLE_BTIF_RX_THREAD_RT_SCHED 0
  74. #define MAX_BTIF_RXD_TIME_REC 3
  75. /*Structure Defination*/
  76. /*-----------------BTIF setting--------------*/
  77. typedef struct _mtk_btif_setting_ {
  78. ENUM_BTIF_MODE tx_mode; /*BTIF Tx Mode Setting */
  79. ENUM_BTIF_MODE rx_mode; /*BTIF Tx Mode Setting */
  80. ENUM_BTIF_RX_TYPE rx_type; /*rx handle type */
  81. ENUM_BTIF_TX_TYPE tx_type; /*tx type */
  82. } mtk_btif_setting, *p_mtk_btif_setting;
  83. /*---------------------------------------------------------------------------*/
  84. #if 0
  85. /*---------------------------------------------------------------------------*/
  86. typedef struct _mtk_btif_register_ {
  87. unsigned int iir; /*Interrupt Identification Register */
  88. unsigned int lsr; /*Line Status Register */
  89. unsigned int fake_lcr; /*Fake Lcr Regiseter */
  90. unsigned int fifo_ctrl; /*FIFO Control Register */
  91. unsigned int ier; /*Interrupt Enable Register */
  92. unsigned int sleep_en; /*Sleep Enable Register */
  93. unsigned int rto_counter; /*Rx Timeout Counter Register */
  94. unsigned int dma_en; /*DMA Enalbe Register */
  95. unsigned int tri_lvl; /*Tx/Rx Trigger Level Register */
  96. unsigned int wat_time; /*Async Wait Time Register */
  97. unsigned int handshake; /*New HandShake Mode Register */
  98. unsigned int sleep_wak; /*Sleep Wakeup Reigster */
  99. } mtk_btif_register, *p_mtk_btif_register;
  100. /*---------------------------------------------------------------------------*/
  101. #endif
  102. typedef struct _btif_buf_str_ {
  103. unsigned int size;
  104. unsigned char *p_buf;
  105. /*For Tx: next Tx data pointer to FIFO;
  106. For Rx: next read data pointer from BTIF user */
  107. unsigned int rd_idx;
  108. /*For Tx: next Tx data pointer from BTIF user;
  109. For Rx: next write data(from FIFO) pointer */
  110. unsigned int wr_idx;
  111. } btif_buf_str, *p_btif_buf_str;
  112. /*---------------------------------------------------------------------------*/
  113. typedef struct _mtk_btif_dma_ {
  114. /*p_mtk_btif*/ void *p_btif;
  115. /*BTIF pointer to which DMA belongs */
  116. #if 0
  117. unsigned int channel; /*DMA's channel */
  118. #endif
  119. ENUM_BTIF_DIR dir; /*DMA's direction: */
  120. bool enable; /*DMA enable or disable flag */
  121. P_MTK_DMA_INFO_STR p_dma_info; /*DMA's IRQ information */
  122. #if 0
  123. mtk_dma_register register; /*DMA's register */
  124. #endif
  125. spinlock_t iolock; /*io lock for DMA channel */
  126. atomic_t entry; /* entry count */
  127. } mtk_btif_dma, *p_mtk_btif_dma;
  128. #if (defined(CONFIG_MTK_GMO_RAM_OPTIMIZE) && !defined(CONFIG_MT_ENG_BUILD))
  129. #define BTIF_LOG_ENTRY_NUM 10
  130. #else
  131. #define BTIF_LOG_ENTRY_NUM 30
  132. #endif
  133. #define BTIF_LOG_SZ 1536
  134. typedef void (*MTK_BTIF_RX_NOTIFY) (void);
  135. typedef struct _btif_log_buf_t_ {
  136. unsigned int len;
  137. struct timeval timer;
  138. unsigned char buffer[BTIF_LOG_SZ];
  139. } BTIF_LOG_BUF_T, *P_BTIF_LOG_BUF_T;
  140. typedef struct _btif_log_queue_t_ {
  141. ENUM_BTIF_DIR dir;
  142. bool enable;
  143. bool output_flag;
  144. unsigned int in;
  145. unsigned int out;
  146. unsigned int size;
  147. spinlock_t lock;
  148. P_BTIF_LOG_BUF_T p_queue[BTIF_LOG_ENTRY_NUM];
  149. } BTIF_LOG_QUEUE_T, *P_BTIF_LOG_QUEUE_T;
  150. /*---------------------------------------------------------------------------*/
  151. typedef struct _mtk_btif_ {
  152. unsigned int open_counter; /*open counter */
  153. bool enable; /*BTIF module enable flag */
  154. bool lpbk_flag; /*BTIF module enable flag */
  155. #if 0
  156. unsigned long base; /* BTIF controller base address */
  157. #endif
  158. ENUM_BTIF_STATE state; /*BTIF state mechanism */
  159. struct mutex state_mtx; /*lock to BTIF state mechanism's state change */
  160. struct mutex ops_mtx; /*lock to BTIF's open and close */
  161. #if 0
  162. mtk_btif_register register; /*BTIF registers */
  163. #endif
  164. ENUM_BTIF_MODE tx_mode; /* BTIF Tx channel mode */
  165. ENUM_BTIF_MODE rx_mode; /* BTIF Rx channel mode */
  166. struct mutex tx_mtx; /*lock to BTIF's tx process */
  167. /*rx handling */
  168. ENUM_BTIF_RX_TYPE btm_type; /*BTIF Rx bottom half context */
  169. /*tx handling*/
  170. ENUM_BTIF_TX_TYPE tx_ctx; /*BTIF tx context */
  171. /* unsigned char rx_buf[BTIF_RX_BUFFER_SIZE]; */
  172. btif_buf_str btif_buf;
  173. spinlock_t rx_irq_spinlock; /*lock for rx irq handling */
  174. /*rx workqueue information*/
  175. /*lock to BTIF's rx bottom half when kernel thread is used */
  176. struct mutex rx_mtx;
  177. struct workqueue_struct *p_rx_wq;
  178. struct work_struct rx_work;
  179. struct workqueue_struct *p_tx_wq;
  180. struct work_struct tx_work;
  181. struct kfifo *p_tx_fifo;
  182. /*rx tasklet information*/
  183. struct tasklet_struct rx_tasklet;
  184. /*lock to BTIF's rx bottom half when tasklet is used */
  185. spinlock_t rx_tasklet_spinlock;
  186. /*rx thread information*/
  187. struct task_struct *p_task;
  188. struct completion rx_comp;
  189. mtk_btif_setting *setting; /*BTIF setting */
  190. p_mtk_btif_dma p_tx_dma; /*BTIF Tx channel DMA */
  191. p_mtk_btif_dma p_rx_dma; /*BTIF Rx channel DMA */
  192. MTK_WCN_BTIF_RX_CB rx_cb; /*Rx callback function */
  193. MTK_BTIF_RX_NOTIFY rx_notify;
  194. P_MTK_BTIF_INFO_STR p_btif_info; /*BTIF's information */
  195. /*Log Tx data to buffer*/
  196. BTIF_LOG_QUEUE_T tx_log;
  197. /*Log Rx data to buffer*/
  198. BTIF_LOG_QUEUE_T rx_log;
  199. /* struct list_head *p_user_list; */
  200. struct list_head user_list;
  201. /* get btif dev pointer*/
  202. void *private_data;
  203. } mtk_btif, *p_mtk_btif;
  204. /*---------------------------------------------------------------------------*/
  205. /*---------------------------------------------------------------------------*/
  206. #if 0
  207. /*---------------------------------------------------------------------------*/
  208. typedef struct _mtk_dma_register_ {
  209. unsigned int int_flag; /*Tx offset:0x0 Rx offset:0x0 */
  210. unsigned int int_enable; /*Tx offset:0x4 Rx offset:0x4 */
  211. unsigned int dma_enable; /*Tx offset:0x8 Rx offset:0x8 */
  212. unsigned int dma_reset; /*Tx offset:0xc Rx offset:0xc */
  213. unsigned int dma_stop; /*Tx offset:0x10 Rx offset:0x10 */
  214. unsigned int dma_flush; /*Tx offset:0x14 Rx offset:0x14 */
  215. unsigned int vff_addr; /*Tx offset:0x1c Rx offset:0x1c */
  216. unsigned int vff_len; /*Tx offset:0x24 Rx offset:0x24 */
  217. unsigned int vff_thr; /*Tx offset:0x28 Rx offset:0x28 */
  218. unsigned int vff_wpt; /*Tx offset:0x2c Rx offset:0x2c */
  219. unsigned int vff_rpt; /*Tx offset:0x30 Rx offset:0x30 */
  220. unsigned int rx_fc_thr; /*Tx:No this register Rx offset:0x34 */
  221. unsigned int int_buf_size; /*Tx offset:0x38 Rx offset:0x38 */
  222. unsigned int vff_valid_size; /*Tx offset:0x3c Rx offset:0x3c */
  223. unsigned int vff_left_size; /*Tx offset:0x40 Rx offset:0x40 */
  224. unsigned int debug_status; /*Tx offset:0x50 Rx offset:0x50 */
  225. } mtk_dma_register, *p_mtk_dma_register;
  226. /*---------------------------------------------------------------------------*/
  227. #endif
  228. /*---------------------------------------------------------------------------*/
  229. typedef struct _mtk_btif_user_ {
  230. bool enable; /*register its state */
  231. struct list_head entry; /*btif_user's bi-direction list table */
  232. /*BTIF's user, static allocation */
  233. char u_name[BTIF_USER_NAME_MAX_LEN];
  234. unsigned long u_id;
  235. p_mtk_btif p_btif;
  236. } mtk_btif_user, *p_mtk_btif_user;
  237. /*---------------------------------------------------------------------------*/
  238. #define BBS_PTR(ptr, idx) ((ptr->p_buf) + idx)
  239. #define BBS_SIZE(ptr) ((ptr)->size)
  240. #define BBS_MASK(ptr) (BBS_SIZE(ptr) - 1)
  241. #define BBS_COUNT(ptr) ((ptr)->wr_idx >= (ptr)->rd_idx ? (ptr)->wr_idx - \
  242. (ptr)->rd_idx : BBS_SIZE(ptr) - \
  243. ((ptr)->rd_idx - (ptr)->wr_idx))
  244. #define BBS_COUNT_CUR(ptr, wr_idx) (wr_idx >= (ptr)->rd_idx ? wr_idx - \
  245. (ptr)->rd_idx : BBS_SIZE(ptr) - \
  246. ((ptr)->rd_idx - wr_idx))
  247. #define BBS_LEFT(ptr) (BBS_SIZE(ptr) - BBS_COUNT(ptr))
  248. #define BBS_AVL_SIZE(ptr) (BBS_SIZE(ptr) - BBS_COUNT(ptr))
  249. #define BBS_FULL(ptr) (BBS_COUNT(ptr) - BBS_SIZE(ptr))
  250. #define BBS_EMPTY(ptr) ((ptr)->wr_idx == (ptr)->rd_idx)
  251. #define BBS_WRITE_MOVE_NEXT(ptr) ((ptr)->wr_idx = \
  252. ((ptr)->wr_idx + 1) & BBS_MASK(ptr))
  253. #define BBS_READ_MOVE_NEXT(ptr) ((ptr)->rd_idx = \
  254. ((ptr)->rd_idx + 1) & BBS_MASK(ptr))
  255. #define BBS_INIT(ptr) \
  256. { \
  257. (ptr)->rd_idx = (ptr)->wr_idx = 0; \
  258. (ptr)->size = BTIF_RX_BUFFER_SIZE; \
  259. }
  260. #define BTIF_MUTEX_UNLOCK(x) mutex_unlock(x)
  261. extern mtk_btif g_btif[];
  262. int btif_open(p_mtk_btif p_btif);
  263. int btif_close(p_mtk_btif p_btif);
  264. int btif_send_data(p_mtk_btif p_btif,
  265. const unsigned char *p_buf, unsigned int buf_len);
  266. int btif_enter_dpidle(p_mtk_btif p_btif);
  267. int btif_exit_dpidle(p_mtk_btif p_btif);
  268. int btif_rx_cb_reg(p_mtk_btif p_btif, MTK_WCN_BTIF_RX_CB rx_cb);
  269. /*for test purpose*/
  270. int _btif_suspend(p_mtk_btif p_btif);
  271. int _btif_resume(p_mtk_btif p_btif);
  272. int _btif_restore_noirq(p_mtk_btif p_btif);
  273. int btif_lpbk_ctrl(p_mtk_btif p_btif, bool flag);
  274. int btif_log_buf_dmp_in(P_BTIF_LOG_QUEUE_T p_log_que, const char *p_buf,
  275. int len);
  276. int btif_dump_data(char *p_buf, int len);
  277. int btif_log_buf_dmp_out(P_BTIF_LOG_QUEUE_T p_log_que);
  278. int btif_log_buf_enable(P_BTIF_LOG_QUEUE_T p_log_que);
  279. int btif_log_buf_disable(P_BTIF_LOG_QUEUE_T p_log_que);
  280. int btif_log_output_enable(P_BTIF_LOG_QUEUE_T p_log_que);
  281. int btif_log_output_disable(P_BTIF_LOG_QUEUE_T p_log_que);
  282. int btif_log_buf_reset(P_BTIF_LOG_QUEUE_T p_log_que);
  283. int btif_log_buf_init(p_mtk_btif p_btif);
  284. int btif_dump_reg(p_mtk_btif p_btif);
  285. int btif_rx_notify_reg(p_mtk_btif p_btif, MTK_BTIF_RX_NOTIFY rx_notify);
  286. int btif_raise_wak_signal(p_mtk_btif p_btif);
  287. int btif_clock_ctrl(p_mtk_btif p_btif, int en);
  288. bool btif_parser_wmt_evt(p_mtk_btif p_btif,
  289. const char *sub_str,
  290. unsigned int sub_len);
  291. #endif /*__MTK_BTIF_H_*/