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