xref: /freebsd/sys/contrib/dev/broadcom/brcm80211/brcmfmac/btcoex.c (revision b4c3e9b5b09c829b4135aff738bd2893ed052377)
1*b4c3e9b5SBjoern A. Zeeb // SPDX-License-Identifier: ISC
2*b4c3e9b5SBjoern A. Zeeb /*
3*b4c3e9b5SBjoern A. Zeeb  * Copyright (c) 2013 Broadcom Corporation
4*b4c3e9b5SBjoern A. Zeeb  */
5*b4c3e9b5SBjoern A. Zeeb #include <linux/slab.h>
6*b4c3e9b5SBjoern A. Zeeb #include <linux/netdevice.h>
7*b4c3e9b5SBjoern A. Zeeb #include <net/cfg80211.h>
8*b4c3e9b5SBjoern A. Zeeb 
9*b4c3e9b5SBjoern A. Zeeb #include <brcmu_wifi.h>
10*b4c3e9b5SBjoern A. Zeeb #include <brcmu_utils.h>
11*b4c3e9b5SBjoern A. Zeeb #include <defs.h>
12*b4c3e9b5SBjoern A. Zeeb #include "core.h"
13*b4c3e9b5SBjoern A. Zeeb #include "debug.h"
14*b4c3e9b5SBjoern A. Zeeb #include "fwil.h"
15*b4c3e9b5SBjoern A. Zeeb #include "fwil_types.h"
16*b4c3e9b5SBjoern A. Zeeb #include "btcoex.h"
17*b4c3e9b5SBjoern A. Zeeb #include "p2p.h"
18*b4c3e9b5SBjoern A. Zeeb #include "cfg80211.h"
19*b4c3e9b5SBjoern A. Zeeb 
20*b4c3e9b5SBjoern A. Zeeb /* T1 start SCO/eSCO priority suppression */
21*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BTCOEX_OPPR_WIN_TIME   msecs_to_jiffies(2000)
22*b4c3e9b5SBjoern A. Zeeb 
23*b4c3e9b5SBjoern A. Zeeb /* BT registers values during DHCP */
24*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG50 0x8022
25*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG51 0
26*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG64 0
27*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG65 0
28*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG71 0
29*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG66 0x2710
30*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG41 0x33
31*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_DHCP_REG68 0x190
32*b4c3e9b5SBjoern A. Zeeb 
33*b4c3e9b5SBjoern A. Zeeb /* number of samples for SCO detection */
34*b4c3e9b5SBjoern A. Zeeb #define BRCMF_BT_SCO_SAMPLES 12
35*b4c3e9b5SBjoern A. Zeeb 
36*b4c3e9b5SBjoern A. Zeeb /**
37*b4c3e9b5SBjoern A. Zeeb * enum brcmf_btcoex_state - BT coex DHCP state machine states
38*b4c3e9b5SBjoern A. Zeeb * @BRCMF_BT_DHCP_IDLE: DCHP is idle
39*b4c3e9b5SBjoern A. Zeeb * @BRCMF_BT_DHCP_START: DHCP started, wait before
40*b4c3e9b5SBjoern A. Zeeb *	boosting wifi priority
41*b4c3e9b5SBjoern A. Zeeb * @BRCMF_BT_DHCP_OPPR_WIN: graceful DHCP opportunity ended,
42*b4c3e9b5SBjoern A. Zeeb *	boost wifi priority
43*b4c3e9b5SBjoern A. Zeeb * @BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT: wifi priority boost end,
44*b4c3e9b5SBjoern A. Zeeb *	restore defaults
45*b4c3e9b5SBjoern A. Zeeb */
46*b4c3e9b5SBjoern A. Zeeb enum brcmf_btcoex_state {
47*b4c3e9b5SBjoern A. Zeeb 	BRCMF_BT_DHCP_IDLE,
48*b4c3e9b5SBjoern A. Zeeb 	BRCMF_BT_DHCP_START,
49*b4c3e9b5SBjoern A. Zeeb 	BRCMF_BT_DHCP_OPPR_WIN,
50*b4c3e9b5SBjoern A. Zeeb 	BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
51*b4c3e9b5SBjoern A. Zeeb };
52*b4c3e9b5SBjoern A. Zeeb 
53*b4c3e9b5SBjoern A. Zeeb /**
54*b4c3e9b5SBjoern A. Zeeb  * struct brcmf_btcoex_info - BT coex related information
55*b4c3e9b5SBjoern A. Zeeb  * @vif: interface for which request was done.
56*b4c3e9b5SBjoern A. Zeeb  * @timer: timer for DHCP state machine
57*b4c3e9b5SBjoern A. Zeeb  * @timeout: configured timeout.
58*b4c3e9b5SBjoern A. Zeeb  * @timer_on:  DHCP timer active
59*b4c3e9b5SBjoern A. Zeeb  * @dhcp_done: DHCP finished before T1/T2 timer expiration
60*b4c3e9b5SBjoern A. Zeeb  * @bt_state: DHCP state machine state
61*b4c3e9b5SBjoern A. Zeeb  * @work: DHCP state machine work
62*b4c3e9b5SBjoern A. Zeeb  * @cfg: driver private data for cfg80211 interface
63*b4c3e9b5SBjoern A. Zeeb  * @reg66: saved value of btc_params 66
64*b4c3e9b5SBjoern A. Zeeb  * @reg41: saved value of btc_params 41
65*b4c3e9b5SBjoern A. Zeeb  * @reg68: saved value of btc_params 68
66*b4c3e9b5SBjoern A. Zeeb  * @saved_regs_part1: flag indicating regs 66,41,68
67*b4c3e9b5SBjoern A. Zeeb  *	have been saved
68*b4c3e9b5SBjoern A. Zeeb  * @reg50: saved value of btc_params 50
69*b4c3e9b5SBjoern A. Zeeb  * @reg51: saved value of btc_params 51
70*b4c3e9b5SBjoern A. Zeeb  * @reg64: saved value of btc_params 64
71*b4c3e9b5SBjoern A. Zeeb  * @reg65: saved value of btc_params 65
72*b4c3e9b5SBjoern A. Zeeb  * @reg71: saved value of btc_params 71
73*b4c3e9b5SBjoern A. Zeeb  * @saved_regs_part2: flag indicating regs 50,51,64,65,71
74*b4c3e9b5SBjoern A. Zeeb  *	have been saved
75*b4c3e9b5SBjoern A. Zeeb  */
76*b4c3e9b5SBjoern A. Zeeb struct brcmf_btcoex_info {
77*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_cfg80211_vif *vif;
78*b4c3e9b5SBjoern A. Zeeb 	struct timer_list timer;
79*b4c3e9b5SBjoern A. Zeeb 	u16 timeout;
80*b4c3e9b5SBjoern A. Zeeb 	bool timer_on;
81*b4c3e9b5SBjoern A. Zeeb 	bool dhcp_done;
82*b4c3e9b5SBjoern A. Zeeb 	enum brcmf_btcoex_state bt_state;
83*b4c3e9b5SBjoern A. Zeeb 	struct work_struct work;
84*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_cfg80211_info *cfg;
85*b4c3e9b5SBjoern A. Zeeb 	u32 reg66;
86*b4c3e9b5SBjoern A. Zeeb 	u32 reg41;
87*b4c3e9b5SBjoern A. Zeeb 	u32 reg68;
88*b4c3e9b5SBjoern A. Zeeb 	bool saved_regs_part1;
89*b4c3e9b5SBjoern A. Zeeb 	u32 reg50;
90*b4c3e9b5SBjoern A. Zeeb 	u32 reg51;
91*b4c3e9b5SBjoern A. Zeeb 	u32 reg64;
92*b4c3e9b5SBjoern A. Zeeb 	u32 reg65;
93*b4c3e9b5SBjoern A. Zeeb 	u32 reg71;
94*b4c3e9b5SBjoern A. Zeeb 	bool saved_regs_part2;
95*b4c3e9b5SBjoern A. Zeeb };
96*b4c3e9b5SBjoern A. Zeeb 
97*b4c3e9b5SBjoern A. Zeeb /**
98*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_params_write() - write btc_params firmware variable
99*b4c3e9b5SBjoern A. Zeeb  * @ifp: interface
100*b4c3e9b5SBjoern A. Zeeb  * @addr: btc_params register number
101*b4c3e9b5SBjoern A. Zeeb  * @data: data to write
102*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_params_write(struct brcmf_if * ifp,u32 addr,u32 data)103*b4c3e9b5SBjoern A. Zeeb static s32 brcmf_btcoex_params_write(struct brcmf_if *ifp, u32 addr, u32 data)
104*b4c3e9b5SBjoern A. Zeeb {
105*b4c3e9b5SBjoern A. Zeeb 	struct {
106*b4c3e9b5SBjoern A. Zeeb 		__le32 addr;
107*b4c3e9b5SBjoern A. Zeeb 		__le32 data;
108*b4c3e9b5SBjoern A. Zeeb 	} reg_write;
109*b4c3e9b5SBjoern A. Zeeb 
110*b4c3e9b5SBjoern A. Zeeb 	reg_write.addr = cpu_to_le32(addr);
111*b4c3e9b5SBjoern A. Zeeb 	reg_write.data = cpu_to_le32(data);
112*b4c3e9b5SBjoern A. Zeeb 	return brcmf_fil_iovar_data_set(ifp, "btc_params",
113*b4c3e9b5SBjoern A. Zeeb 					&reg_write, sizeof(reg_write));
114*b4c3e9b5SBjoern A. Zeeb }
115*b4c3e9b5SBjoern A. Zeeb 
116*b4c3e9b5SBjoern A. Zeeb /**
117*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_params_read() - read btc_params firmware variable
118*b4c3e9b5SBjoern A. Zeeb  * @ifp: interface
119*b4c3e9b5SBjoern A. Zeeb  * @addr: btc_params register number
120*b4c3e9b5SBjoern A. Zeeb  * @data: read data
121*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_params_read(struct brcmf_if * ifp,u32 addr,u32 * data)122*b4c3e9b5SBjoern A. Zeeb static s32 brcmf_btcoex_params_read(struct brcmf_if *ifp, u32 addr, u32 *data)
123*b4c3e9b5SBjoern A. Zeeb {
124*b4c3e9b5SBjoern A. Zeeb 	*data = addr;
125*b4c3e9b5SBjoern A. Zeeb 
126*b4c3e9b5SBjoern A. Zeeb 	return brcmf_fil_iovar_int_query(ifp, "btc_params", data);
127*b4c3e9b5SBjoern A. Zeeb }
128*b4c3e9b5SBjoern A. Zeeb 
129*b4c3e9b5SBjoern A. Zeeb /**
130*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_boost_wifi() - control BT SCO/eSCO parameters
131*b4c3e9b5SBjoern A. Zeeb  * @btci: BT coex info
132*b4c3e9b5SBjoern A. Zeeb  * @trump_sco:
133*b4c3e9b5SBjoern A. Zeeb  *	true - set SCO/eSCO parameters for compatibility
134*b4c3e9b5SBjoern A. Zeeb  *		during DHCP window
135*b4c3e9b5SBjoern A. Zeeb  *	false - restore saved parameter values
136*b4c3e9b5SBjoern A. Zeeb  *
137*b4c3e9b5SBjoern A. Zeeb  * Enhanced BT COEX settings for eSCO compatibility during DHCP window
138*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_boost_wifi(struct brcmf_btcoex_info * btci,bool trump_sco)139*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_boost_wifi(struct brcmf_btcoex_info *btci,
140*b4c3e9b5SBjoern A. Zeeb 				    bool trump_sco)
141*b4c3e9b5SBjoern A. Zeeb {
142*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp = brcmf_get_ifp(btci->cfg->pub, 0);
143*b4c3e9b5SBjoern A. Zeeb 
144*b4c3e9b5SBjoern A. Zeeb 	if (trump_sco && !btci->saved_regs_part2) {
145*b4c3e9b5SBjoern A. Zeeb 		/* this should reduce eSCO agressive
146*b4c3e9b5SBjoern A. Zeeb 		 * retransmit w/o breaking it
147*b4c3e9b5SBjoern A. Zeeb 		 */
148*b4c3e9b5SBjoern A. Zeeb 
149*b4c3e9b5SBjoern A. Zeeb 		/* save current */
150*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "new SCO/eSCO coex algo {save & override}\n");
151*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 50, &btci->reg50);
152*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 51, &btci->reg51);
153*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 64, &btci->reg64);
154*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 65, &btci->reg65);
155*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 71, &btci->reg71);
156*b4c3e9b5SBjoern A. Zeeb 
157*b4c3e9b5SBjoern A. Zeeb 		btci->saved_regs_part2 = true;
158*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO,
159*b4c3e9b5SBjoern A. Zeeb 			  "saved bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
160*b4c3e9b5SBjoern A. Zeeb 			  btci->reg50, btci->reg51, btci->reg64,
161*b4c3e9b5SBjoern A. Zeeb 			  btci->reg65, btci->reg71);
162*b4c3e9b5SBjoern A. Zeeb 
163*b4c3e9b5SBjoern A. Zeeb 		/* pacify the eSco   */
164*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 50, BRCMF_BT_DHCP_REG50);
165*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 51, BRCMF_BT_DHCP_REG51);
166*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 64, BRCMF_BT_DHCP_REG64);
167*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 65, BRCMF_BT_DHCP_REG65);
168*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 71, BRCMF_BT_DHCP_REG71);
169*b4c3e9b5SBjoern A. Zeeb 
170*b4c3e9b5SBjoern A. Zeeb 	} else if (btci->saved_regs_part2) {
171*b4c3e9b5SBjoern A. Zeeb 		/* restore previously saved bt params */
172*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "Do new SCO/eSCO coex algo {restore}\n");
173*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 50, btci->reg50);
174*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 51, btci->reg51);
175*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 64, btci->reg64);
176*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 65, btci->reg65);
177*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 71, btci->reg71);
178*b4c3e9b5SBjoern A. Zeeb 
179*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO,
180*b4c3e9b5SBjoern A. Zeeb 			  "restored bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
181*b4c3e9b5SBjoern A. Zeeb 			  btci->reg50, btci->reg51, btci->reg64,
182*b4c3e9b5SBjoern A. Zeeb 			  btci->reg65, btci->reg71);
183*b4c3e9b5SBjoern A. Zeeb 
184*b4c3e9b5SBjoern A. Zeeb 		btci->saved_regs_part2 = false;
185*b4c3e9b5SBjoern A. Zeeb 	} else {
186*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "attempted to restore not saved BTCOEX params\n");
187*b4c3e9b5SBjoern A. Zeeb 	}
188*b4c3e9b5SBjoern A. Zeeb }
189*b4c3e9b5SBjoern A. Zeeb 
190*b4c3e9b5SBjoern A. Zeeb /**
191*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_is_sco_active() - check if SCO/eSCO is active
192*b4c3e9b5SBjoern A. Zeeb  * @ifp: interface
193*b4c3e9b5SBjoern A. Zeeb  *
194*b4c3e9b5SBjoern A. Zeeb  * return: true if SCO/eSCO session is active
195*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_is_sco_active(struct brcmf_if * ifp)196*b4c3e9b5SBjoern A. Zeeb static bool brcmf_btcoex_is_sco_active(struct brcmf_if *ifp)
197*b4c3e9b5SBjoern A. Zeeb {
198*b4c3e9b5SBjoern A. Zeeb 	int ioc_res = 0;
199*b4c3e9b5SBjoern A. Zeeb 	bool res = false;
200*b4c3e9b5SBjoern A. Zeeb 	int sco_id_cnt = 0;
201*b4c3e9b5SBjoern A. Zeeb 	u32 param27;
202*b4c3e9b5SBjoern A. Zeeb 	int i;
203*b4c3e9b5SBjoern A. Zeeb 
204*b4c3e9b5SBjoern A. Zeeb 	for (i = 0; i < BRCMF_BT_SCO_SAMPLES; i++) {
205*b4c3e9b5SBjoern A. Zeeb 		ioc_res = brcmf_btcoex_params_read(ifp, 27, &param27);
206*b4c3e9b5SBjoern A. Zeeb 
207*b4c3e9b5SBjoern A. Zeeb 		if (ioc_res < 0) {
208*b4c3e9b5SBjoern A. Zeeb 			brcmf_err("ioc read btc params error\n");
209*b4c3e9b5SBjoern A. Zeeb 			break;
210*b4c3e9b5SBjoern A. Zeeb 		}
211*b4c3e9b5SBjoern A. Zeeb 
212*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "sample[%d], btc_params 27:%x\n", i, param27);
213*b4c3e9b5SBjoern A. Zeeb 
214*b4c3e9b5SBjoern A. Zeeb 		if ((param27 & 0x6) == 2) { /* count both sco & esco  */
215*b4c3e9b5SBjoern A. Zeeb 			sco_id_cnt++;
216*b4c3e9b5SBjoern A. Zeeb 		}
217*b4c3e9b5SBjoern A. Zeeb 
218*b4c3e9b5SBjoern A. Zeeb 		if (sco_id_cnt > 2) {
219*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO,
220*b4c3e9b5SBjoern A. Zeeb 				  "sco/esco detected, pkt id_cnt:%d samples:%d\n",
221*b4c3e9b5SBjoern A. Zeeb 				  sco_id_cnt, i);
222*b4c3e9b5SBjoern A. Zeeb 			res = true;
223*b4c3e9b5SBjoern A. Zeeb 			break;
224*b4c3e9b5SBjoern A. Zeeb 		}
225*b4c3e9b5SBjoern A. Zeeb 	}
226*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "exit: result=%d\n", res);
227*b4c3e9b5SBjoern A. Zeeb 	return res;
228*b4c3e9b5SBjoern A. Zeeb }
229*b4c3e9b5SBjoern A. Zeeb 
230*b4c3e9b5SBjoern A. Zeeb /*
231*b4c3e9b5SBjoern A. Zeeb  * btcmf_btcoex_save_part1() - save first step parameters.
232*b4c3e9b5SBjoern A. Zeeb  */
btcmf_btcoex_save_part1(struct brcmf_btcoex_info * btci)233*b4c3e9b5SBjoern A. Zeeb static void btcmf_btcoex_save_part1(struct brcmf_btcoex_info *btci)
234*b4c3e9b5SBjoern A. Zeeb {
235*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp = btci->vif->ifp;
236*b4c3e9b5SBjoern A. Zeeb 
237*b4c3e9b5SBjoern A. Zeeb 	if (!btci->saved_regs_part1) {
238*b4c3e9b5SBjoern A. Zeeb 		/* Retrieve and save original reg value */
239*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 66, &btci->reg66);
240*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 41, &btci->reg41);
241*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_read(ifp, 68, &btci->reg68);
242*b4c3e9b5SBjoern A. Zeeb 		btci->saved_regs_part1 = true;
243*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO,
244*b4c3e9b5SBjoern A. Zeeb 			  "saved btc_params regs (66,41,68) 0x%x 0x%x 0x%x\n",
245*b4c3e9b5SBjoern A. Zeeb 			  btci->reg66, btci->reg41,
246*b4c3e9b5SBjoern A. Zeeb 			  btci->reg68);
247*b4c3e9b5SBjoern A. Zeeb 	}
248*b4c3e9b5SBjoern A. Zeeb }
249*b4c3e9b5SBjoern A. Zeeb 
250*b4c3e9b5SBjoern A. Zeeb /*
251*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_restore_part1() - restore first step parameters.
252*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_restore_part1(struct brcmf_btcoex_info * btci)253*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_restore_part1(struct brcmf_btcoex_info *btci)
254*b4c3e9b5SBjoern A. Zeeb {
255*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp;
256*b4c3e9b5SBjoern A. Zeeb 
257*b4c3e9b5SBjoern A. Zeeb 	if (btci->saved_regs_part1) {
258*b4c3e9b5SBjoern A. Zeeb 		btci->saved_regs_part1 = false;
259*b4c3e9b5SBjoern A. Zeeb 		ifp = btci->vif->ifp;
260*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 66, btci->reg66);
261*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 41, btci->reg41);
262*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_params_write(ifp, 68, btci->reg68);
263*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO,
264*b4c3e9b5SBjoern A. Zeeb 			  "restored btc_params regs {66,41,68} 0x%x 0x%x 0x%x\n",
265*b4c3e9b5SBjoern A. Zeeb 			  btci->reg66, btci->reg41,
266*b4c3e9b5SBjoern A. Zeeb 			  btci->reg68);
267*b4c3e9b5SBjoern A. Zeeb 	}
268*b4c3e9b5SBjoern A. Zeeb }
269*b4c3e9b5SBjoern A. Zeeb 
270*b4c3e9b5SBjoern A. Zeeb /*
271*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_timerfunc() - BT coex timer callback
272*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_timerfunc(struct timer_list * t)273*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_timerfunc(struct timer_list *t)
274*b4c3e9b5SBjoern A. Zeeb {
275*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_btcoex_info *bt_local = timer_container_of(bt_local, t,
276*b4c3e9b5SBjoern A. Zeeb 								timer);
277*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "enter\n");
278*b4c3e9b5SBjoern A. Zeeb 
279*b4c3e9b5SBjoern A. Zeeb 	bt_local->timer_on = false;
280*b4c3e9b5SBjoern A. Zeeb 	schedule_work(&bt_local->work);
281*b4c3e9b5SBjoern A. Zeeb }
282*b4c3e9b5SBjoern A. Zeeb 
283*b4c3e9b5SBjoern A. Zeeb /**
284*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_handler() - BT coex state machine work handler
285*b4c3e9b5SBjoern A. Zeeb  * @work: work
286*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_handler(struct work_struct * work)287*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_handler(struct work_struct *work)
288*b4c3e9b5SBjoern A. Zeeb {
289*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_btcoex_info *btci;
290*b4c3e9b5SBjoern A. Zeeb 	btci = container_of(work, struct brcmf_btcoex_info, work);
291*b4c3e9b5SBjoern A. Zeeb 	if (btci->timer_on) {
292*b4c3e9b5SBjoern A. Zeeb 		btci->timer_on = false;
293*b4c3e9b5SBjoern A. Zeeb 		timer_delete_sync(&btci->timer);
294*b4c3e9b5SBjoern A. Zeeb 	}
295*b4c3e9b5SBjoern A. Zeeb 
296*b4c3e9b5SBjoern A. Zeeb 	switch (btci->bt_state) {
297*b4c3e9b5SBjoern A. Zeeb 	case BRCMF_BT_DHCP_START:
298*b4c3e9b5SBjoern A. Zeeb 		/* DHCP started provide OPPORTUNITY window
299*b4c3e9b5SBjoern A. Zeeb 		   to get DHCP address
300*b4c3e9b5SBjoern A. Zeeb 		*/
301*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "DHCP started\n");
302*b4c3e9b5SBjoern A. Zeeb 		btci->bt_state = BRCMF_BT_DHCP_OPPR_WIN;
303*b4c3e9b5SBjoern A. Zeeb 		if (btci->timeout < BRCMF_BTCOEX_OPPR_WIN_TIME) {
304*b4c3e9b5SBjoern A. Zeeb 			mod_timer(&btci->timer, btci->timer.expires);
305*b4c3e9b5SBjoern A. Zeeb 		} else {
306*b4c3e9b5SBjoern A. Zeeb 			btci->timeout -= BRCMF_BTCOEX_OPPR_WIN_TIME;
307*b4c3e9b5SBjoern A. Zeeb 			mod_timer(&btci->timer,
308*b4c3e9b5SBjoern A. Zeeb 				  jiffies + BRCMF_BTCOEX_OPPR_WIN_TIME);
309*b4c3e9b5SBjoern A. Zeeb 		}
310*b4c3e9b5SBjoern A. Zeeb 		btci->timer_on = true;
311*b4c3e9b5SBjoern A. Zeeb 		break;
312*b4c3e9b5SBjoern A. Zeeb 
313*b4c3e9b5SBjoern A. Zeeb 	case BRCMF_BT_DHCP_OPPR_WIN:
314*b4c3e9b5SBjoern A. Zeeb 		if (btci->dhcp_done) {
315*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO, "DHCP done before T1 expiration\n");
316*b4c3e9b5SBjoern A. Zeeb 			goto idle;
317*b4c3e9b5SBjoern A. Zeeb 		}
318*b4c3e9b5SBjoern A. Zeeb 
319*b4c3e9b5SBjoern A. Zeeb 		/* DHCP is not over yet, start lowering BT priority */
320*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "DHCP T1:%d expired\n",
321*b4c3e9b5SBjoern A. Zeeb 			  jiffies_to_msecs(BRCMF_BTCOEX_OPPR_WIN_TIME));
322*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_boost_wifi(btci, true);
323*b4c3e9b5SBjoern A. Zeeb 
324*b4c3e9b5SBjoern A. Zeeb 		btci->bt_state = BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT;
325*b4c3e9b5SBjoern A. Zeeb 		mod_timer(&btci->timer, jiffies + btci->timeout);
326*b4c3e9b5SBjoern A. Zeeb 		btci->timer_on = true;
327*b4c3e9b5SBjoern A. Zeeb 		break;
328*b4c3e9b5SBjoern A. Zeeb 
329*b4c3e9b5SBjoern A. Zeeb 	case BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT:
330*b4c3e9b5SBjoern A. Zeeb 		if (btci->dhcp_done)
331*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO, "DHCP done before T2 expiration\n");
332*b4c3e9b5SBjoern A. Zeeb 		else
333*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO, "DHCP T2:%d expired\n",
334*b4c3e9b5SBjoern A. Zeeb 				  BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT);
335*b4c3e9b5SBjoern A. Zeeb 
336*b4c3e9b5SBjoern A. Zeeb 		goto idle;
337*b4c3e9b5SBjoern A. Zeeb 
338*b4c3e9b5SBjoern A. Zeeb 	default:
339*b4c3e9b5SBjoern A. Zeeb 		brcmf_err("invalid state=%d !!!\n", btci->bt_state);
340*b4c3e9b5SBjoern A. Zeeb 		goto idle;
341*b4c3e9b5SBjoern A. Zeeb 	}
342*b4c3e9b5SBjoern A. Zeeb 
343*b4c3e9b5SBjoern A. Zeeb 	return;
344*b4c3e9b5SBjoern A. Zeeb 
345*b4c3e9b5SBjoern A. Zeeb idle:
346*b4c3e9b5SBjoern A. Zeeb 	btci->bt_state = BRCMF_BT_DHCP_IDLE;
347*b4c3e9b5SBjoern A. Zeeb 	btci->timer_on = false;
348*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_boost_wifi(btci, false);
349*b4c3e9b5SBjoern A. Zeeb 	cfg80211_crit_proto_stopped(&btci->vif->wdev, GFP_KERNEL);
350*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_restore_part1(btci);
351*b4c3e9b5SBjoern A. Zeeb 	btci->vif = NULL;
352*b4c3e9b5SBjoern A. Zeeb }
353*b4c3e9b5SBjoern A. Zeeb 
354*b4c3e9b5SBjoern A. Zeeb /**
355*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_attach() - initialize BT coex data
356*b4c3e9b5SBjoern A. Zeeb  * @cfg: driver private cfg80211 data
357*b4c3e9b5SBjoern A. Zeeb  *
358*b4c3e9b5SBjoern A. Zeeb  * return: 0 on success
359*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_attach(struct brcmf_cfg80211_info * cfg)360*b4c3e9b5SBjoern A. Zeeb int brcmf_btcoex_attach(struct brcmf_cfg80211_info *cfg)
361*b4c3e9b5SBjoern A. Zeeb {
362*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_btcoex_info *btci;
363*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "enter\n");
364*b4c3e9b5SBjoern A. Zeeb 
365*b4c3e9b5SBjoern A. Zeeb 	btci = kmalloc(sizeof(*btci), GFP_KERNEL);
366*b4c3e9b5SBjoern A. Zeeb 	if (!btci)
367*b4c3e9b5SBjoern A. Zeeb 		return -ENOMEM;
368*b4c3e9b5SBjoern A. Zeeb 
369*b4c3e9b5SBjoern A. Zeeb 	btci->bt_state = BRCMF_BT_DHCP_IDLE;
370*b4c3e9b5SBjoern A. Zeeb 
371*b4c3e9b5SBjoern A. Zeeb 	/* Set up timer for BT  */
372*b4c3e9b5SBjoern A. Zeeb 	btci->timer_on = false;
373*b4c3e9b5SBjoern A. Zeeb 	btci->timeout = BRCMF_BTCOEX_OPPR_WIN_TIME;
374*b4c3e9b5SBjoern A. Zeeb 	timer_setup(&btci->timer, brcmf_btcoex_timerfunc, 0);
375*b4c3e9b5SBjoern A. Zeeb 	btci->cfg = cfg;
376*b4c3e9b5SBjoern A. Zeeb 	btci->saved_regs_part1 = false;
377*b4c3e9b5SBjoern A. Zeeb 	btci->saved_regs_part2 = false;
378*b4c3e9b5SBjoern A. Zeeb 
379*b4c3e9b5SBjoern A. Zeeb 	INIT_WORK(&btci->work, brcmf_btcoex_handler);
380*b4c3e9b5SBjoern A. Zeeb 
381*b4c3e9b5SBjoern A. Zeeb 	cfg->btcoex = btci;
382*b4c3e9b5SBjoern A. Zeeb 	return 0;
383*b4c3e9b5SBjoern A. Zeeb }
384*b4c3e9b5SBjoern A. Zeeb 
385*b4c3e9b5SBjoern A. Zeeb /**
386*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_detach - clean BT coex data
387*b4c3e9b5SBjoern A. Zeeb  * @cfg: driver private cfg80211 data
388*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_detach(struct brcmf_cfg80211_info * cfg)389*b4c3e9b5SBjoern A. Zeeb void brcmf_btcoex_detach(struct brcmf_cfg80211_info *cfg)
390*b4c3e9b5SBjoern A. Zeeb {
391*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "enter\n");
392*b4c3e9b5SBjoern A. Zeeb 
393*b4c3e9b5SBjoern A. Zeeb 	if (!cfg->btcoex)
394*b4c3e9b5SBjoern A. Zeeb 		return;
395*b4c3e9b5SBjoern A. Zeeb 
396*b4c3e9b5SBjoern A. Zeeb 	timer_shutdown_sync(&cfg->btcoex->timer);
397*b4c3e9b5SBjoern A. Zeeb 	cfg->btcoex->timer_on = false;
398*b4c3e9b5SBjoern A. Zeeb 
399*b4c3e9b5SBjoern A. Zeeb 	cancel_work_sync(&cfg->btcoex->work);
400*b4c3e9b5SBjoern A. Zeeb 
401*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_boost_wifi(cfg->btcoex, false);
402*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_restore_part1(cfg->btcoex);
403*b4c3e9b5SBjoern A. Zeeb 
404*b4c3e9b5SBjoern A. Zeeb 	kfree(cfg->btcoex);
405*b4c3e9b5SBjoern A. Zeeb 	cfg->btcoex = NULL;
406*b4c3e9b5SBjoern A. Zeeb }
407*b4c3e9b5SBjoern A. Zeeb 
brcmf_btcoex_dhcp_start(struct brcmf_btcoex_info * btci)408*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_dhcp_start(struct brcmf_btcoex_info *btci)
409*b4c3e9b5SBjoern A. Zeeb {
410*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp = btci->vif->ifp;
411*b4c3e9b5SBjoern A. Zeeb 
412*b4c3e9b5SBjoern A. Zeeb 	btcmf_btcoex_save_part1(btci);
413*b4c3e9b5SBjoern A. Zeeb 	/* set new regs values */
414*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_params_write(ifp, 66, BRCMF_BT_DHCP_REG66);
415*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_params_write(ifp, 41, BRCMF_BT_DHCP_REG41);
416*b4c3e9b5SBjoern A. Zeeb 	brcmf_btcoex_params_write(ifp, 68, BRCMF_BT_DHCP_REG68);
417*b4c3e9b5SBjoern A. Zeeb 	btci->dhcp_done = false;
418*b4c3e9b5SBjoern A. Zeeb 	btci->bt_state = BRCMF_BT_DHCP_START;
419*b4c3e9b5SBjoern A. Zeeb 	schedule_work(&btci->work);
420*b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "enable BT DHCP Timer\n");
421*b4c3e9b5SBjoern A. Zeeb }
422*b4c3e9b5SBjoern A. Zeeb 
brcmf_btcoex_dhcp_end(struct brcmf_btcoex_info * btci)423*b4c3e9b5SBjoern A. Zeeb static void brcmf_btcoex_dhcp_end(struct brcmf_btcoex_info *btci)
424*b4c3e9b5SBjoern A. Zeeb {
425*b4c3e9b5SBjoern A. Zeeb 	/* Stop any bt timer because DHCP session is done */
426*b4c3e9b5SBjoern A. Zeeb 	btci->dhcp_done = true;
427*b4c3e9b5SBjoern A. Zeeb 	if (btci->timer_on) {
428*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "disable BT DHCP Timer\n");
429*b4c3e9b5SBjoern A. Zeeb 		btci->timer_on = false;
430*b4c3e9b5SBjoern A. Zeeb 		timer_delete_sync(&btci->timer);
431*b4c3e9b5SBjoern A. Zeeb 
432*b4c3e9b5SBjoern A. Zeeb 		/* schedule worker if transition to IDLE is needed */
433*b4c3e9b5SBjoern A. Zeeb 		if (btci->bt_state != BRCMF_BT_DHCP_IDLE) {
434*b4c3e9b5SBjoern A. Zeeb 			brcmf_dbg(INFO, "bt_state:%d\n",
435*b4c3e9b5SBjoern A. Zeeb 				  btci->bt_state);
436*b4c3e9b5SBjoern A. Zeeb 			schedule_work(&btci->work);
437*b4c3e9b5SBjoern A. Zeeb 		}
438*b4c3e9b5SBjoern A. Zeeb 	} else {
439*b4c3e9b5SBjoern A. Zeeb 		/* Restore original values */
440*b4c3e9b5SBjoern A. Zeeb 		brcmf_btcoex_restore_part1(btci);
441*b4c3e9b5SBjoern A. Zeeb 	}
442*b4c3e9b5SBjoern A. Zeeb }
443*b4c3e9b5SBjoern A. Zeeb 
444*b4c3e9b5SBjoern A. Zeeb /*
445*b4c3e9b5SBjoern A. Zeeb  * brcmf_btcoex_set_mode - set BT coex mode
446*b4c3e9b5SBjoern A. Zeeb  * @mode: Wifi-Bluetooth coexistence mode
447*b4c3e9b5SBjoern A. Zeeb  *
448*b4c3e9b5SBjoern A. Zeeb  * return: 0 on success
449*b4c3e9b5SBjoern A. Zeeb  */
brcmf_btcoex_set_mode(struct brcmf_cfg80211_vif * vif,enum brcmf_btcoex_mode mode,u16 duration)450*b4c3e9b5SBjoern A. Zeeb int brcmf_btcoex_set_mode(struct brcmf_cfg80211_vif *vif,
451*b4c3e9b5SBjoern A. Zeeb 			  enum brcmf_btcoex_mode mode, u16 duration)
452*b4c3e9b5SBjoern A. Zeeb {
453*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
454*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_btcoex_info *btci = cfg->btcoex;
455*b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
456*b4c3e9b5SBjoern A. Zeeb 
457*b4c3e9b5SBjoern A. Zeeb 	switch (mode) {
458*b4c3e9b5SBjoern A. Zeeb 	case BRCMF_BTCOEX_DISABLED:
459*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "DHCP session starts\n");
460*b4c3e9b5SBjoern A. Zeeb 		if (btci->bt_state != BRCMF_BT_DHCP_IDLE)
461*b4c3e9b5SBjoern A. Zeeb 			return -EBUSY;
462*b4c3e9b5SBjoern A. Zeeb 		/* Start BT timer only for SCO connection */
463*b4c3e9b5SBjoern A. Zeeb 		if (brcmf_btcoex_is_sco_active(ifp)) {
464*b4c3e9b5SBjoern A. Zeeb 			btci->timeout = msecs_to_jiffies(duration);
465*b4c3e9b5SBjoern A. Zeeb 			btci->vif = vif;
466*b4c3e9b5SBjoern A. Zeeb 			brcmf_btcoex_dhcp_start(btci);
467*b4c3e9b5SBjoern A. Zeeb 		}
468*b4c3e9b5SBjoern A. Zeeb 		break;
469*b4c3e9b5SBjoern A. Zeeb 
470*b4c3e9b5SBjoern A. Zeeb 	case BRCMF_BTCOEX_ENABLED:
471*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "DHCP session ends\n");
472*b4c3e9b5SBjoern A. Zeeb 		if (btci->bt_state != BRCMF_BT_DHCP_IDLE &&
473*b4c3e9b5SBjoern A. Zeeb 		    vif == btci->vif) {
474*b4c3e9b5SBjoern A. Zeeb 			brcmf_btcoex_dhcp_end(btci);
475*b4c3e9b5SBjoern A. Zeeb 		}
476*b4c3e9b5SBjoern A. Zeeb 		break;
477*b4c3e9b5SBjoern A. Zeeb 	default:
478*b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(INFO, "Unknown mode, ignored\n");
479*b4c3e9b5SBjoern A. Zeeb 	}
480*b4c3e9b5SBjoern A. Zeeb 	return 0;
481*b4c3e9b5SBjoern A. Zeeb }
482