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