rtk_coex.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. *
  3. * Realtek Bluetooth USB driver
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <net/bluetooth/hci_core.h>
  22. #include <linux/list.h>
  23. /***********************************
  24. ** Realtek - For coexistence **
  25. ***********************************/
  26. #define BTRTL_HCIUSB 0
  27. #define BTRTL_HCIUART 1
  28. #define BTRTL_HCI_IF BTRTL_HCIUART
  29. #define TRUE 1
  30. #define FALSE 0
  31. #define CONNECT_PORT 30001
  32. #define CONNECT_PORT_WIFI 30000
  33. #define invite_req "INVITE_REQ"
  34. #define invite_rsp "INVITE_RSP"
  35. #define attend_req "ATTEND_REQ"
  36. #define attend_ack "ATTEND_ACK"
  37. #define wifi_leave "WIFI_LEAVE"
  38. #define leave_ack "LEAVE_ACK"
  39. #define bt_leave "BT_LEAVE"
  40. #define HCI_OP_PERIODIC_INQ 0x0403
  41. #define HCI_EV_LE_META 0x3e
  42. #define HCI_EV_LE_CONN_COMPLETE 0x01
  43. #define HCI_EV_LE_CONN_UPDATE_COMPLETE 0x03
  44. #define HCI_EV_LE_ENHANCED_CONN_COMPLETE 0x0a
  45. //vendor cmd to fw
  46. #define HCI_VENDOR_ENABLE_PROFILE_REPORT_COMMAND 0xfc18
  47. #define HCI_VENDOR_SET_PROFILE_REPORT_COMMAND 0xfc19
  48. #define HCI_VENDOR_MAILBOX_CMD 0xfc8f
  49. #define HCI_VENDOR_SET_BITPOOL 0xfc51
  50. //subcmd to fw
  51. #define HCI_VENDOR_SUB_CMD_WIFI_CHANNEL_AND_BANDWIDTH_CMD 0x11
  52. #define HCI_VENDOR_SUB_CMD_WIFI_FORCE_TX_POWER_CMD 0x17
  53. #define HCI_VENDOR_SUB_CMD_BT_ENABLE_IGNORE_WLAN_ACT_CMD 0x1B
  54. #define HCI_VENDOR_SUB_CMD_BT_REPORT_CONN_SCO_INQ_INFO 0x23
  55. #define HCI_VENDOR_SUB_CMD_BT_AUTO_REPORT_STATUS_INFO 0x27
  56. #define HCI_VENDOR_SUB_CMD_BT_AUTO_REPORT_ENABLE 0x28
  57. #define HCI_VENDOR_SUB_CMD_BT_SET_TXRETRY_REPORT_PARAM 0x29
  58. #define HCI_VENDOR_SUB_CMD_BT_SET_PTATABLE 0x2A
  59. #define HCI_VENDOR_SUB_CMD_SET_BT_PSD_MODE 0x31
  60. #define HCI_VENDOR_SUB_CMD_SET_BT_LNA_CONSTRAINT 0x32
  61. #define HCI_VENDOR_SUB_CMD_GET_AFH_MAP_L 0x40
  62. #define HCI_VENDOR_SUB_CMD_GET_AFH_MAP_M 0x41
  63. #define HCI_VENDOR_SUB_CMD_GET_AFH_MAP_H 0x42
  64. #define HCI_VENDOR_SUB_CMD_RD_REG_REQ 0x43
  65. #define HCI_VENDOR_SUB_CMD_WR_REG_REQ 0x44
  66. #define HCI_EV_VENDOR_SPECIFIC 0xff
  67. //sub event from fw start
  68. #define HCI_VENDOR_PTA_REPORT_EVENT 0x24
  69. #define HCI_VENDOR_PTA_AUTO_REPORT_EVENT 0x25
  70. //vendor cmd to wifi driver
  71. #define HCI_GRP_VENDOR_SPECIFIC (0x3f << 10)
  72. #define HCI_OP_HCI_EXTENSION_VERSION_NOTIFY (0x0100 | HCI_GRP_VENDOR_SPECIFIC)
  73. #define HCI_OP_BT_OPERATION_NOTIFY (0x0102 | HCI_GRP_VENDOR_SPECIFIC)
  74. #define HCI_OP_HCI_BT_INFO_NOTIFY (0x0106 | HCI_GRP_VENDOR_SPECIFIC)
  75. #define HCI_OP_HCI_BT_COEX_NOTIFY (0x0107 | HCI_GRP_VENDOR_SPECIFIC)
  76. #define HCI_OP_HCI_BT_PATCH_VER_NOTIFY (0x0108 | HCI_GRP_VENDOR_SPECIFIC)
  77. #define HCI_OP_HCI_BT_AFH_MAP_NOTIFY (0x0109 | HCI_GRP_VENDOR_SPECIFIC)
  78. #define HCI_OP_HCI_BT_REGISTER_VALUE_NOTIFY (0x010a | HCI_GRP_VENDOR_SPECIFIC)
  79. //bt info reason to wifi
  80. #define HOST_RESPONSE 0 //Host response when receive the BT Info Control Event
  81. #define POLLING_RESPONSE 1 //The BT Info response for polling by BT firmware.
  82. #define AUTO_REPORT 2 //BT auto report by BT firmware.
  83. #define STACK_REPORT_WHILE_DEVICE_D2 3 //Stack report when BT firmware is under power save state(ex:D2)
  84. // vendor event from wifi
  85. #define RTK_HS_EXTENSION_EVENT_WIFI_SCAN 0x01
  86. #define RTK_HS_EXTENSION_EVENT_RADIO_STATUS_NOTIFY 0x02
  87. #define RTK_HS_EXTENSION_EVENT_HCI_BT_INFO_CONTROL 0x03
  88. #define RTK_HS_EXTENSION_EVENT_HCI_BT_COEX_CONTROL 0x04
  89. //op code from wifi
  90. #define BT_PATCH_VERSION_QUERY 0x00
  91. #define IGNORE_WLAN_ACTIVE_CONTROL 0x01
  92. #define LNA_CONSTRAIN_CONTROL 0x02
  93. #define BT_POWER_DECREASE_CONTROL 0x03
  94. #define BT_PSD_MODE_CONTROL 0x04
  95. #define WIFI_BW_CHNL_NOTIFY 0x05
  96. #define QUERY_BT_AFH_MAP 0x06
  97. #define BT_REGISTER_ACCESS 0x07
  98. //bt operation to notify
  99. #define BT_OPCODE_NONE 0
  100. #define BT_OPCODE_INQUIRY_START 1
  101. #define BT_OPCODE_INQUIRY_END 2
  102. #define BT_OPCODE_PAGE_START 3
  103. #define BT_OPCODE_PAGE_SUCCESS_END 4
  104. #define BT_OPCODE_PAGE_UNSUCCESS_END 5
  105. #define BT_OPCODE_PAIR_START 6
  106. #define BT_OPCODE_PAIR_END 7
  107. #define BT_OPCODE_ENABLE_BT 8
  108. #define BT_OPCODE_DISABLE_BT 9
  109. #define HCI_EXTENSION_VERSION 0x0004
  110. #define HCI_CMD_PREAMBLE_SIZE 3
  111. #define PAN_PACKET_COUNT 5
  112. #define STREAM_TO_UINT16(u16, p) {u16 = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); (p) += 2;}
  113. #define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);}
  114. #define PSM_SDP 0x0001
  115. #define PSM_RFCOMM 0x0003
  116. #define PSM_PAN 0x000F
  117. #define PSM_HID 0x0011
  118. #define PSM_HID_INT 0x0013
  119. #define PSM_AVCTP 0x0017
  120. #define PSM_AVDTP 0x0019
  121. #define PSM_FTP 0x1001
  122. #define PSM_BIP 0x1003
  123. #define PSM_OPP 0x1015
  124. //--add more if needed--//
  125. enum {
  126. profile_sco = 0,
  127. profile_hid = 1,
  128. profile_a2dp = 2,
  129. profile_pan = 3,
  130. profile_hid_interval = 4,
  131. profile_hogp = 5,
  132. profile_voice = 6,
  133. profile_sink = 7,
  134. profile_max = 8
  135. };
  136. #define A2DP_SIGNAL 0x01
  137. #define A2DP_MEDIA 0x02
  138. //profile info data
  139. typedef struct {
  140. struct list_head list;
  141. uint16_t handle;
  142. uint16_t psm;
  143. uint16_t dcid;
  144. uint16_t scid;
  145. uint8_t profile_index;
  146. uint8_t flags;
  147. } rtk_prof_info, *prtk_prof_info;
  148. //profile info for each connection
  149. typedef struct rtl_hci_conn {
  150. struct list_head list;
  151. uint16_t handle;
  152. uint8_t type; // 0:l2cap, 1:sco/esco, 2:le
  153. uint8_t profile_bitmap;
  154. int8_t profile_refcount[8];
  155. } rtk_conn_prof, *prtk_conn_prof;
  156. #ifdef RTB_SOFTWARE_MAILBOX
  157. struct rtl_btinfo {
  158. u8 cmd;
  159. u8 len;
  160. u8 data[6];
  161. };
  162. #define RTL_BTINFO_LEN (sizeof(struct rtl_btinfo))
  163. /* typedef struct {
  164. * uint8_t cmd_index;
  165. * uint8_t cmd_length;
  166. * uint8_t link_status;
  167. * uint8_t retry_cnt;
  168. * uint8_t rssi;
  169. * uint8_t mailbox_info;
  170. * uint16_t acl_throughput;
  171. * } hci_linkstatus_report; */
  172. typedef struct {
  173. uint8_t type;
  174. uint32_t offset;
  175. uint32_t value;
  176. } hci_mailbox_register;
  177. struct rtl_btinfo_ctl {
  178. uint8_t polling_enable;
  179. uint8_t polling_time;
  180. uint8_t autoreport_enable;
  181. };
  182. #endif /* RTB_SOFTWARE_MAILBOX */
  183. #define MAX_LEN_OF_HCI_EV 32
  184. #define NUM_RTL_HCI_EV 32
  185. struct rtl_hci_ev {
  186. __u8 data[MAX_LEN_OF_HCI_EV];
  187. __u16 len;
  188. struct list_head list;
  189. };
  190. #define L2_MAX_SUBSEC_LEN 128
  191. #define L2_MAX_PKTS 16
  192. struct rtl_l2_buff {
  193. __u8 data[L2_MAX_SUBSEC_LEN];
  194. __u16 len;
  195. __u16 out;
  196. struct list_head list;
  197. };
  198. struct rtl_coex_struct {
  199. struct list_head conn_hash; //hash for connections
  200. struct list_head profile_list; //hash for profile info
  201. struct hci_dev *hdev;
  202. #ifdef RTB_SOFTWARE_MAILBOX
  203. struct socket *udpsock;
  204. struct sockaddr_in addr;
  205. struct sockaddr_in wifi_addr;
  206. struct timer_list polling_timer;
  207. #endif
  208. struct timer_list a2dp_count_timer;
  209. struct timer_list pan_count_timer;
  210. struct timer_list hogp_count_timer;
  211. #ifdef RTB_SOFTWARE_MAILBOX
  212. struct workqueue_struct *sock_wq;
  213. struct delayed_work sock_work;
  214. #endif
  215. struct workqueue_struct *fw_wq;
  216. struct delayed_work fw_work;
  217. struct delayed_work l2_work;
  218. #ifdef RTB_SOFTWARE_MAILBOX
  219. struct sock *sk;
  220. #endif
  221. struct urb *urb;
  222. spinlock_t spin_lock_sock;
  223. spinlock_t spin_lock_profile;
  224. uint32_t a2dp_packet_count;
  225. uint32_t pan_packet_count;
  226. uint32_t hogp_packet_count;
  227. uint32_t voice_packet_count;
  228. uint8_t profile_bitmap;
  229. uint8_t profile_status;
  230. int8_t profile_refcount[8];
  231. uint8_t ispairing;
  232. uint8_t isinquirying;
  233. uint8_t ispaging;
  234. #ifdef RTB_SOFTWARE_MAILBOX
  235. uint8_t wifi_state;
  236. uint8_t autoreport;
  237. uint8_t polling_enable;
  238. uint8_t polling_interval;
  239. uint8_t piconet_id;
  240. uint8_t mode;
  241. uint8_t afh_map[10];
  242. #endif
  243. uint16_t hci_reversion;
  244. uint16_t lmp_subversion;
  245. #ifdef RTB_SOFTWARE_MAILBOX
  246. uint8_t wifi_on;
  247. uint8_t sock_open;
  248. #endif
  249. unsigned long cmd_last_tx;
  250. /* hci ev buff */
  251. struct list_head ev_used_list;
  252. struct list_head ev_free_list;
  253. spinlock_t rxlock;
  254. __u8 pkt_type;
  255. __u16 expect;
  256. __u8 *tbuff;
  257. __u16 elen;
  258. __u8 back_buff[HCI_MAX_EVENT_SIZE];
  259. /* l2cap rx buff */
  260. struct list_head l2_used_list;
  261. struct list_head l2_free_list;
  262. /* buff addr and size */
  263. spinlock_t buff_lock;
  264. unsigned long pages_addr;
  265. unsigned long buff_size;
  266. #define RTL_COEX_RUNNING (1 << 0)
  267. unsigned long flags;
  268. };
  269. #ifdef __LITTLE_ENDIAN
  270. struct sbc_frame_hdr {
  271. uint8_t syncword:8; /* Sync word */
  272. uint8_t subbands:1; /* Subbands */
  273. uint8_t allocation_method:1; /* Allocation method */
  274. uint8_t channel_mode:2; /* Channel mode */
  275. uint8_t blocks:2; /* Blocks */
  276. uint8_t sampling_frequency:2; /* Sampling frequency */
  277. uint8_t bitpool:8; /* Bitpool */
  278. uint8_t crc_check:8; /* CRC check */
  279. } __attribute__ ((packed));
  280. /* NOTE: The code is copied from pa.
  281. * only the bit field in 8-bit is affected by endian, not the 16-bit or 32-bit.
  282. * why?
  283. */
  284. struct rtp_header {
  285. unsigned cc:4;
  286. unsigned x:1;
  287. unsigned p:1;
  288. unsigned v:2;
  289. unsigned pt:7;
  290. unsigned m:1;
  291. uint16_t sequence_number;
  292. uint32_t timestamp;
  293. uint32_t ssrc;
  294. uint32_t csrc[0];
  295. } __attribute__ ((packed));
  296. #else
  297. /* big endian */
  298. struct sbc_frame_hdr {
  299. uint8_t syncword:8; /* Sync word */
  300. uint8_t sampling_frequency:2; /* Sampling frequency */
  301. uint8_t blocks:2; /* Blocks */
  302. uint8_t channel_mode:2; /* Channel mode */
  303. uint8_t allocation_method:1; /* Allocation method */
  304. uint8_t subbands:1; /* Subbands */
  305. uint8_t bitpool:8; /* Bitpool */
  306. uint8_t crc_check:8; /* CRC check */
  307. } __attribute__ ((packed));
  308. struct rtp_header {
  309. unsigned v:2;
  310. unsigned p:1;
  311. unsigned x:1;
  312. unsigned cc:4;
  313. unsigned m:1;
  314. unsigned pt:7;
  315. uint16_t sequence_number;
  316. uint32_t timestamp;
  317. uint32_t ssrc;
  318. uint32_t csrc[0];
  319. } __attribute__ ((packed));
  320. #endif /* __LITTLE_ENDIAN */
  321. void rtk_btcoex_parse_event(uint8_t *buffer, int count);
  322. void rtk_btcoex_parse_cmd(uint8_t *buffer, int count);
  323. void rtk_btcoex_parse_l2cap_data_tx(uint8_t *buffer, int count);
  324. void rtk_btcoex_parse_l2cap_data_rx(uint8_t *buffer, int count);
  325. void rtk_btcoex_open(struct hci_dev *hdev);
  326. void rtk_btcoex_close(void);
  327. void rtk_btcoex_probe(struct hci_dev *hdev);
  328. void rtk_btcoex_init(void);
  329. void rtk_btcoex_exit(void);