1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2013 Broadcom Corporation
5 #include <linux/slab.h>
6 #include <linux/netdevice.h>
7 #include <net/cfg80211.h>
9 #include <brcmu_wifi.h>
10 #include <brcmu_utils.h>
15 #include "fwil_types.h"
20 /* T1 start SCO/eSCO priority suppression */
21 #define BRCMF_BTCOEX_OPPR_WIN_TIME msecs_to_jiffies(2000)
23 /* BT registers values during DHCP */
24 #define BRCMF_BT_DHCP_REG50 0x8022
25 #define BRCMF_BT_DHCP_REG51 0
26 #define BRCMF_BT_DHCP_REG64 0
27 #define BRCMF_BT_DHCP_REG65 0
28 #define BRCMF_BT_DHCP_REG71 0
29 #define BRCMF_BT_DHCP_REG66 0x2710
30 #define BRCMF_BT_DHCP_REG41 0x33
31 #define BRCMF_BT_DHCP_REG68 0x190
33 /* number of samples for SCO detection */
34 #define BRCMF_BT_SCO_SAMPLES 12
37 * enum brcmf_btcoex_state - BT coex DHCP state machine states
38 * @BRCMF_BT_DHCP_IDLE: DCHP is idle
39 * @BRCMF_BT_DHCP_START: DHCP started, wait before
40 * boosting wifi priority
41 * @BRCMF_BT_DHCP_OPPR_WIN: graceful DHCP opportunity ended,
43 * @BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT: wifi priority boost end,
46 enum brcmf_btcoex_state
{
49 BRCMF_BT_DHCP_OPPR_WIN
,
50 BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
54 * struct brcmf_btcoex_info - BT coex related information
55 * @vif: interface for which request was done.
56 * @timer: timer for DHCP state machine
57 * @timeout: configured timeout.
58 * @timer_on: DHCP timer active
59 * @dhcp_done: DHCP finished before T1/T2 timer expiration
60 * @bt_state: DHCP state machine state
61 * @work: DHCP state machine work
62 * @cfg: driver private data for cfg80211 interface
63 * @reg66: saved value of btc_params 66
64 * @reg41: saved value of btc_params 41
65 * @reg68: saved value of btc_params 68
66 * @saved_regs_part1: flag indicating regs 66,41,68
68 * @reg50: saved value of btc_params 50
69 * @reg51: saved value of btc_params 51
70 * @reg64: saved value of btc_params 64
71 * @reg65: saved value of btc_params 65
72 * @reg71: saved value of btc_params 71
73 * @saved_regs_part2: flag indicating regs 50,51,64,65,71
76 struct brcmf_btcoex_info
{
77 struct brcmf_cfg80211_vif
*vif
;
78 struct timer_list timer
;
82 enum brcmf_btcoex_state bt_state
;
83 struct work_struct work
;
84 struct brcmf_cfg80211_info
*cfg
;
88 bool saved_regs_part1
;
94 bool saved_regs_part2
;
98 * brcmf_btcoex_params_write() - write btc_params firmware variable
100 * @addr: btc_params register number
101 * @data: data to write
103 static s32
brcmf_btcoex_params_write(struct brcmf_if
*ifp
, u32 addr
, u32 data
)
110 reg_write
.addr
= cpu_to_le32(addr
);
111 reg_write
.data
= cpu_to_le32(data
);
112 return brcmf_fil_iovar_data_set(ifp
, "btc_params",
113 ®_write
, sizeof(reg_write
));
117 * brcmf_btcoex_params_read() - read btc_params firmware variable
119 * @addr: btc_params register number
122 static s32
brcmf_btcoex_params_read(struct brcmf_if
*ifp
, u32 addr
, u32
*data
)
126 return brcmf_fil_iovar_int_get(ifp
, "btc_params", data
);
130 * brcmf_btcoex_boost_wifi() - control BT SCO/eSCO parameters
131 * @btci: BT coex info
133 * true - set SCO/eSCO parameters for compatibility
135 * false - restore saved parameter values
137 * Enhanced BT COEX settings for eSCO compatibility during DHCP window
139 static void brcmf_btcoex_boost_wifi(struct brcmf_btcoex_info
*btci
,
142 struct brcmf_if
*ifp
= brcmf_get_ifp(btci
->cfg
->pub
, 0);
144 if (trump_sco
&& !btci
->saved_regs_part2
) {
145 /* this should reduce eSCO agressive
146 * retransmit w/o breaking it
150 brcmf_dbg(INFO
, "new SCO/eSCO coex algo {save & override}\n");
151 brcmf_btcoex_params_read(ifp
, 50, &btci
->reg50
);
152 brcmf_btcoex_params_read(ifp
, 51, &btci
->reg51
);
153 brcmf_btcoex_params_read(ifp
, 64, &btci
->reg64
);
154 brcmf_btcoex_params_read(ifp
, 65, &btci
->reg65
);
155 brcmf_btcoex_params_read(ifp
, 71, &btci
->reg71
);
157 btci
->saved_regs_part2
= true;
159 "saved bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
160 btci
->reg50
, btci
->reg51
, btci
->reg64
,
161 btci
->reg65
, btci
->reg71
);
163 /* pacify the eSco */
164 brcmf_btcoex_params_write(ifp
, 50, BRCMF_BT_DHCP_REG50
);
165 brcmf_btcoex_params_write(ifp
, 51, BRCMF_BT_DHCP_REG51
);
166 brcmf_btcoex_params_write(ifp
, 64, BRCMF_BT_DHCP_REG64
);
167 brcmf_btcoex_params_write(ifp
, 65, BRCMF_BT_DHCP_REG65
);
168 brcmf_btcoex_params_write(ifp
, 71, BRCMF_BT_DHCP_REG71
);
170 } else if (btci
->saved_regs_part2
) {
171 /* restore previously saved bt params */
172 brcmf_dbg(INFO
, "Do new SCO/eSCO coex algo {restore}\n");
173 brcmf_btcoex_params_write(ifp
, 50, btci
->reg50
);
174 brcmf_btcoex_params_write(ifp
, 51, btci
->reg51
);
175 brcmf_btcoex_params_write(ifp
, 64, btci
->reg64
);
176 brcmf_btcoex_params_write(ifp
, 65, btci
->reg65
);
177 brcmf_btcoex_params_write(ifp
, 71, btci
->reg71
);
180 "restored bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
181 btci
->reg50
, btci
->reg51
, btci
->reg64
,
182 btci
->reg65
, btci
->reg71
);
184 btci
->saved_regs_part2
= false;
186 brcmf_dbg(INFO
, "attempted to restore not saved BTCOEX params\n");
191 * brcmf_btcoex_is_sco_active() - check if SCO/eSCO is active
194 * return: true if SCO/eSCO session is active
196 static bool brcmf_btcoex_is_sco_active(struct brcmf_if
*ifp
)
204 for (i
= 0; i
< BRCMF_BT_SCO_SAMPLES
; i
++) {
205 ioc_res
= brcmf_btcoex_params_read(ifp
, 27, ¶m27
);
208 brcmf_err("ioc read btc params error\n");
212 brcmf_dbg(INFO
, "sample[%d], btc_params 27:%x\n", i
, param27
);
214 if ((param27
& 0x6) == 2) { /* count both sco & esco */
218 if (sco_id_cnt
> 2) {
220 "sco/esco detected, pkt id_cnt:%d samples:%d\n",
226 brcmf_dbg(TRACE
, "exit: result=%d\n", res
);
231 * btcmf_btcoex_save_part1() - save first step parameters.
233 static void btcmf_btcoex_save_part1(struct brcmf_btcoex_info
*btci
)
235 struct brcmf_if
*ifp
= btci
->vif
->ifp
;
237 if (!btci
->saved_regs_part1
) {
238 /* Retrieve and save original reg value */
239 brcmf_btcoex_params_read(ifp
, 66, &btci
->reg66
);
240 brcmf_btcoex_params_read(ifp
, 41, &btci
->reg41
);
241 brcmf_btcoex_params_read(ifp
, 68, &btci
->reg68
);
242 btci
->saved_regs_part1
= true;
244 "saved btc_params regs (66,41,68) 0x%x 0x%x 0x%x\n",
245 btci
->reg66
, btci
->reg41
,
251 * brcmf_btcoex_restore_part1() - restore first step parameters.
253 static void brcmf_btcoex_restore_part1(struct brcmf_btcoex_info
*btci
)
255 struct brcmf_if
*ifp
;
257 if (btci
->saved_regs_part1
) {
258 btci
->saved_regs_part1
= false;
259 ifp
= btci
->vif
->ifp
;
260 brcmf_btcoex_params_write(ifp
, 66, btci
->reg66
);
261 brcmf_btcoex_params_write(ifp
, 41, btci
->reg41
);
262 brcmf_btcoex_params_write(ifp
, 68, btci
->reg68
);
264 "restored btc_params regs {66,41,68} 0x%x 0x%x 0x%x\n",
265 btci
->reg66
, btci
->reg41
,
271 * brcmf_btcoex_timerfunc() - BT coex timer callback
273 static void brcmf_btcoex_timerfunc(struct timer_list
*t
)
275 struct brcmf_btcoex_info
*bt_local
= from_timer(bt_local
, t
, timer
);
276 brcmf_dbg(TRACE
, "enter\n");
278 bt_local
->timer_on
= false;
279 schedule_work(&bt_local
->work
);
283 * brcmf_btcoex_handler() - BT coex state machine work handler
286 static void brcmf_btcoex_handler(struct work_struct
*work
)
288 struct brcmf_btcoex_info
*btci
;
289 btci
= container_of(work
, struct brcmf_btcoex_info
, work
);
290 if (btci
->timer_on
) {
291 btci
->timer_on
= false;
292 del_timer_sync(&btci
->timer
);
295 switch (btci
->bt_state
) {
296 case BRCMF_BT_DHCP_START
:
297 /* DHCP started provide OPPORTUNITY window
300 brcmf_dbg(INFO
, "DHCP started\n");
301 btci
->bt_state
= BRCMF_BT_DHCP_OPPR_WIN
;
302 if (btci
->timeout
< BRCMF_BTCOEX_OPPR_WIN_TIME
) {
303 mod_timer(&btci
->timer
, btci
->timer
.expires
);
305 btci
->timeout
-= BRCMF_BTCOEX_OPPR_WIN_TIME
;
306 mod_timer(&btci
->timer
,
307 jiffies
+ BRCMF_BTCOEX_OPPR_WIN_TIME
);
309 btci
->timer_on
= true;
312 case BRCMF_BT_DHCP_OPPR_WIN
:
313 if (btci
->dhcp_done
) {
314 brcmf_dbg(INFO
, "DHCP done before T1 expiration\n");
318 /* DHCP is not over yet, start lowering BT priority */
319 brcmf_dbg(INFO
, "DHCP T1:%d expired\n",
320 jiffies_to_msecs(BRCMF_BTCOEX_OPPR_WIN_TIME
));
321 brcmf_btcoex_boost_wifi(btci
, true);
323 btci
->bt_state
= BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
;
324 mod_timer(&btci
->timer
, jiffies
+ btci
->timeout
);
325 btci
->timer_on
= true;
328 case BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
:
330 brcmf_dbg(INFO
, "DHCP done before T2 expiration\n");
332 brcmf_dbg(INFO
, "DHCP T2:%d expired\n",
333 BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
);
338 brcmf_err("invalid state=%d !!!\n", btci
->bt_state
);
345 btci
->bt_state
= BRCMF_BT_DHCP_IDLE
;
346 btci
->timer_on
= false;
347 brcmf_btcoex_boost_wifi(btci
, false);
348 cfg80211_crit_proto_stopped(&btci
->vif
->wdev
, GFP_KERNEL
);
349 brcmf_btcoex_restore_part1(btci
);
354 * brcmf_btcoex_attach() - initialize BT coex data
355 * @cfg: driver private cfg80211 data
357 * return: 0 on success
359 int brcmf_btcoex_attach(struct brcmf_cfg80211_info
*cfg
)
361 struct brcmf_btcoex_info
*btci
= NULL
;
362 brcmf_dbg(TRACE
, "enter\n");
364 btci
= kmalloc(sizeof(struct brcmf_btcoex_info
), GFP_KERNEL
);
368 btci
->bt_state
= BRCMF_BT_DHCP_IDLE
;
370 /* Set up timer for BT */
371 btci
->timer_on
= false;
372 btci
->timeout
= BRCMF_BTCOEX_OPPR_WIN_TIME
;
373 timer_setup(&btci
->timer
, brcmf_btcoex_timerfunc
, 0);
375 btci
->saved_regs_part1
= false;
376 btci
->saved_regs_part2
= false;
378 INIT_WORK(&btci
->work
, brcmf_btcoex_handler
);
385 * brcmf_btcoex_detach - clean BT coex data
386 * @cfg: driver private cfg80211 data
388 void brcmf_btcoex_detach(struct brcmf_cfg80211_info
*cfg
)
390 brcmf_dbg(TRACE
, "enter\n");
395 if (cfg
->btcoex
->timer_on
) {
396 cfg
->btcoex
->timer_on
= false;
397 del_timer_sync(&cfg
->btcoex
->timer
);
400 cancel_work_sync(&cfg
->btcoex
->work
);
402 brcmf_btcoex_boost_wifi(cfg
->btcoex
, false);
403 brcmf_btcoex_restore_part1(cfg
->btcoex
);
409 static void brcmf_btcoex_dhcp_start(struct brcmf_btcoex_info
*btci
)
411 struct brcmf_if
*ifp
= btci
->vif
->ifp
;
413 btcmf_btcoex_save_part1(btci
);
414 /* set new regs values */
415 brcmf_btcoex_params_write(ifp
, 66, BRCMF_BT_DHCP_REG66
);
416 brcmf_btcoex_params_write(ifp
, 41, BRCMF_BT_DHCP_REG41
);
417 brcmf_btcoex_params_write(ifp
, 68, BRCMF_BT_DHCP_REG68
);
418 btci
->dhcp_done
= false;
419 btci
->bt_state
= BRCMF_BT_DHCP_START
;
420 schedule_work(&btci
->work
);
421 brcmf_dbg(TRACE
, "enable BT DHCP Timer\n");
424 static void brcmf_btcoex_dhcp_end(struct brcmf_btcoex_info
*btci
)
426 /* Stop any bt timer because DHCP session is done */
427 btci
->dhcp_done
= true;
428 if (btci
->timer_on
) {
429 brcmf_dbg(INFO
, "disable BT DHCP Timer\n");
430 btci
->timer_on
= false;
431 del_timer_sync(&btci
->timer
);
433 /* schedule worker if transition to IDLE is needed */
434 if (btci
->bt_state
!= BRCMF_BT_DHCP_IDLE
) {
435 brcmf_dbg(INFO
, "bt_state:%d\n",
437 schedule_work(&btci
->work
);
440 /* Restore original values */
441 brcmf_btcoex_restore_part1(btci
);
446 * brcmf_btcoex_set_mode - set BT coex mode
447 * @mode: Wifi-Bluetooth coexistence mode
449 * return: 0 on success
451 int brcmf_btcoex_set_mode(struct brcmf_cfg80211_vif
*vif
,
452 enum brcmf_btcoex_mode mode
, u16 duration
)
454 struct brcmf_cfg80211_info
*cfg
= wiphy_to_cfg(vif
->wdev
.wiphy
);
455 struct brcmf_btcoex_info
*btci
= cfg
->btcoex
;
456 struct brcmf_if
*ifp
= brcmf_get_ifp(cfg
->pub
, 0);
459 case BRCMF_BTCOEX_DISABLED
:
460 brcmf_dbg(INFO
, "DHCP session starts\n");
461 if (btci
->bt_state
!= BRCMF_BT_DHCP_IDLE
)
463 /* Start BT timer only for SCO connection */
464 if (brcmf_btcoex_is_sco_active(ifp
)) {
465 btci
->timeout
= msecs_to_jiffies(duration
);
467 brcmf_btcoex_dhcp_start(btci
);
471 case BRCMF_BTCOEX_ENABLED
:
472 brcmf_dbg(INFO
, "DHCP session ends\n");
473 if (btci
->bt_state
!= BRCMF_BT_DHCP_IDLE
&&
475 brcmf_btcoex_dhcp_end(btci
);
479 brcmf_dbg(INFO
, "Unknown mode, ignored\n");