0001
0002
0003
0004
0005 #include <linux/slab.h>
0006 #include <linux/netdevice.h>
0007 #include <net/cfg80211.h>
0008
0009 #include <brcmu_wifi.h>
0010 #include <brcmu_utils.h>
0011 #include <defs.h>
0012 #include "core.h"
0013 #include "debug.h"
0014 #include "fwil.h"
0015 #include "fwil_types.h"
0016 #include "btcoex.h"
0017 #include "p2p.h"
0018 #include "cfg80211.h"
0019
0020
0021 #define BRCMF_BTCOEX_OPPR_WIN_TIME msecs_to_jiffies(2000)
0022
0023
0024 #define BRCMF_BT_DHCP_REG50 0x8022
0025 #define BRCMF_BT_DHCP_REG51 0
0026 #define BRCMF_BT_DHCP_REG64 0
0027 #define BRCMF_BT_DHCP_REG65 0
0028 #define BRCMF_BT_DHCP_REG71 0
0029 #define BRCMF_BT_DHCP_REG66 0x2710
0030 #define BRCMF_BT_DHCP_REG41 0x33
0031 #define BRCMF_BT_DHCP_REG68 0x190
0032
0033
0034 #define BRCMF_BT_SCO_SAMPLES 12
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 enum brcmf_btcoex_state {
0047 BRCMF_BT_DHCP_IDLE,
0048 BRCMF_BT_DHCP_START,
0049 BRCMF_BT_DHCP_OPPR_WIN,
0050 BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT
0051 };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 struct brcmf_btcoex_info {
0077 struct brcmf_cfg80211_vif *vif;
0078 struct timer_list timer;
0079 u16 timeout;
0080 bool timer_on;
0081 bool dhcp_done;
0082 enum brcmf_btcoex_state bt_state;
0083 struct work_struct work;
0084 struct brcmf_cfg80211_info *cfg;
0085 u32 reg66;
0086 u32 reg41;
0087 u32 reg68;
0088 bool saved_regs_part1;
0089 u32 reg50;
0090 u32 reg51;
0091 u32 reg64;
0092 u32 reg65;
0093 u32 reg71;
0094 bool saved_regs_part2;
0095 };
0096
0097
0098
0099
0100
0101
0102
0103 static s32 brcmf_btcoex_params_write(struct brcmf_if *ifp, u32 addr, u32 data)
0104 {
0105 struct {
0106 __le32 addr;
0107 __le32 data;
0108 } reg_write;
0109
0110 reg_write.addr = cpu_to_le32(addr);
0111 reg_write.data = cpu_to_le32(data);
0112 return brcmf_fil_iovar_data_set(ifp, "btc_params",
0113 ®_write, sizeof(reg_write));
0114 }
0115
0116
0117
0118
0119
0120
0121
0122 static s32 brcmf_btcoex_params_read(struct brcmf_if *ifp, u32 addr, u32 *data)
0123 {
0124 *data = addr;
0125
0126 return brcmf_fil_iovar_int_get(ifp, "btc_params", data);
0127 }
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 static void brcmf_btcoex_boost_wifi(struct brcmf_btcoex_info *btci,
0140 bool trump_sco)
0141 {
0142 struct brcmf_if *ifp = brcmf_get_ifp(btci->cfg->pub, 0);
0143
0144 if (trump_sco && !btci->saved_regs_part2) {
0145
0146
0147
0148
0149
0150 brcmf_dbg(INFO, "new SCO/eSCO coex algo {save & override}\n");
0151 brcmf_btcoex_params_read(ifp, 50, &btci->reg50);
0152 brcmf_btcoex_params_read(ifp, 51, &btci->reg51);
0153 brcmf_btcoex_params_read(ifp, 64, &btci->reg64);
0154 brcmf_btcoex_params_read(ifp, 65, &btci->reg65);
0155 brcmf_btcoex_params_read(ifp, 71, &btci->reg71);
0156
0157 btci->saved_regs_part2 = true;
0158 brcmf_dbg(INFO,
0159 "saved bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
0160 btci->reg50, btci->reg51, btci->reg64,
0161 btci->reg65, btci->reg71);
0162
0163
0164 brcmf_btcoex_params_write(ifp, 50, BRCMF_BT_DHCP_REG50);
0165 brcmf_btcoex_params_write(ifp, 51, BRCMF_BT_DHCP_REG51);
0166 brcmf_btcoex_params_write(ifp, 64, BRCMF_BT_DHCP_REG64);
0167 brcmf_btcoex_params_write(ifp, 65, BRCMF_BT_DHCP_REG65);
0168 brcmf_btcoex_params_write(ifp, 71, BRCMF_BT_DHCP_REG71);
0169
0170 } else if (btci->saved_regs_part2) {
0171
0172 brcmf_dbg(INFO, "Do new SCO/eSCO coex algo {restore}\n");
0173 brcmf_btcoex_params_write(ifp, 50, btci->reg50);
0174 brcmf_btcoex_params_write(ifp, 51, btci->reg51);
0175 brcmf_btcoex_params_write(ifp, 64, btci->reg64);
0176 brcmf_btcoex_params_write(ifp, 65, btci->reg65);
0177 brcmf_btcoex_params_write(ifp, 71, btci->reg71);
0178
0179 brcmf_dbg(INFO,
0180 "restored bt_params[50,51,64,65,71]: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
0181 btci->reg50, btci->reg51, btci->reg64,
0182 btci->reg65, btci->reg71);
0183
0184 btci->saved_regs_part2 = false;
0185 } else {
0186 brcmf_dbg(INFO, "attempted to restore not saved BTCOEX params\n");
0187 }
0188 }
0189
0190
0191
0192
0193
0194
0195
0196 static bool brcmf_btcoex_is_sco_active(struct brcmf_if *ifp)
0197 {
0198 int ioc_res = 0;
0199 bool res = false;
0200 int sco_id_cnt = 0;
0201 u32 param27;
0202 int i;
0203
0204 for (i = 0; i < BRCMF_BT_SCO_SAMPLES; i++) {
0205 ioc_res = brcmf_btcoex_params_read(ifp, 27, ¶m27);
0206
0207 if (ioc_res < 0) {
0208 brcmf_err("ioc read btc params error\n");
0209 break;
0210 }
0211
0212 brcmf_dbg(INFO, "sample[%d], btc_params 27:%x\n", i, param27);
0213
0214 if ((param27 & 0x6) == 2) {
0215 sco_id_cnt++;
0216 }
0217
0218 if (sco_id_cnt > 2) {
0219 brcmf_dbg(INFO,
0220 "sco/esco detected, pkt id_cnt:%d samples:%d\n",
0221 sco_id_cnt, i);
0222 res = true;
0223 break;
0224 }
0225 }
0226 brcmf_dbg(TRACE, "exit: result=%d\n", res);
0227 return res;
0228 }
0229
0230
0231
0232
0233 static void btcmf_btcoex_save_part1(struct brcmf_btcoex_info *btci)
0234 {
0235 struct brcmf_if *ifp = btci->vif->ifp;
0236
0237 if (!btci->saved_regs_part1) {
0238
0239 brcmf_btcoex_params_read(ifp, 66, &btci->reg66);
0240 brcmf_btcoex_params_read(ifp, 41, &btci->reg41);
0241 brcmf_btcoex_params_read(ifp, 68, &btci->reg68);
0242 btci->saved_regs_part1 = true;
0243 brcmf_dbg(INFO,
0244 "saved btc_params regs (66,41,68) 0x%x 0x%x 0x%x\n",
0245 btci->reg66, btci->reg41,
0246 btci->reg68);
0247 }
0248 }
0249
0250
0251
0252
0253 static void brcmf_btcoex_restore_part1(struct brcmf_btcoex_info *btci)
0254 {
0255 struct brcmf_if *ifp;
0256
0257 if (btci->saved_regs_part1) {
0258 btci->saved_regs_part1 = false;
0259 ifp = btci->vif->ifp;
0260 brcmf_btcoex_params_write(ifp, 66, btci->reg66);
0261 brcmf_btcoex_params_write(ifp, 41, btci->reg41);
0262 brcmf_btcoex_params_write(ifp, 68, btci->reg68);
0263 brcmf_dbg(INFO,
0264 "restored btc_params regs {66,41,68} 0x%x 0x%x 0x%x\n",
0265 btci->reg66, btci->reg41,
0266 btci->reg68);
0267 }
0268 }
0269
0270
0271
0272
0273 static void brcmf_btcoex_timerfunc(struct timer_list *t)
0274 {
0275 struct brcmf_btcoex_info *bt_local = from_timer(bt_local, t, timer);
0276 brcmf_dbg(TRACE, "enter\n");
0277
0278 bt_local->timer_on = false;
0279 schedule_work(&bt_local->work);
0280 }
0281
0282
0283
0284
0285
0286 static void brcmf_btcoex_handler(struct work_struct *work)
0287 {
0288 struct brcmf_btcoex_info *btci;
0289 btci = container_of(work, struct brcmf_btcoex_info, work);
0290 if (btci->timer_on) {
0291 btci->timer_on = false;
0292 del_timer_sync(&btci->timer);
0293 }
0294
0295 switch (btci->bt_state) {
0296 case BRCMF_BT_DHCP_START:
0297
0298
0299
0300 brcmf_dbg(INFO, "DHCP started\n");
0301 btci->bt_state = BRCMF_BT_DHCP_OPPR_WIN;
0302 if (btci->timeout < BRCMF_BTCOEX_OPPR_WIN_TIME) {
0303 mod_timer(&btci->timer, btci->timer.expires);
0304 } else {
0305 btci->timeout -= BRCMF_BTCOEX_OPPR_WIN_TIME;
0306 mod_timer(&btci->timer,
0307 jiffies + BRCMF_BTCOEX_OPPR_WIN_TIME);
0308 }
0309 btci->timer_on = true;
0310 break;
0311
0312 case BRCMF_BT_DHCP_OPPR_WIN:
0313 if (btci->dhcp_done) {
0314 brcmf_dbg(INFO, "DHCP done before T1 expiration\n");
0315 goto idle;
0316 }
0317
0318
0319 brcmf_dbg(INFO, "DHCP T1:%d expired\n",
0320 jiffies_to_msecs(BRCMF_BTCOEX_OPPR_WIN_TIME));
0321 brcmf_btcoex_boost_wifi(btci, true);
0322
0323 btci->bt_state = BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT;
0324 mod_timer(&btci->timer, jiffies + btci->timeout);
0325 btci->timer_on = true;
0326 break;
0327
0328 case BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT:
0329 if (btci->dhcp_done)
0330 brcmf_dbg(INFO, "DHCP done before T2 expiration\n");
0331 else
0332 brcmf_dbg(INFO, "DHCP T2:%d expired\n",
0333 BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT);
0334
0335 goto idle;
0336
0337 default:
0338 brcmf_err("invalid state=%d !!!\n", btci->bt_state);
0339 goto idle;
0340 }
0341
0342 return;
0343
0344 idle:
0345 btci->bt_state = BRCMF_BT_DHCP_IDLE;
0346 btci->timer_on = false;
0347 brcmf_btcoex_boost_wifi(btci, false);
0348 cfg80211_crit_proto_stopped(&btci->vif->wdev, GFP_KERNEL);
0349 brcmf_btcoex_restore_part1(btci);
0350 btci->vif = NULL;
0351 }
0352
0353
0354
0355
0356
0357
0358
0359 int brcmf_btcoex_attach(struct brcmf_cfg80211_info *cfg)
0360 {
0361 struct brcmf_btcoex_info *btci = NULL;
0362 brcmf_dbg(TRACE, "enter\n");
0363
0364 btci = kmalloc(sizeof(struct brcmf_btcoex_info), GFP_KERNEL);
0365 if (!btci)
0366 return -ENOMEM;
0367
0368 btci->bt_state = BRCMF_BT_DHCP_IDLE;
0369
0370
0371 btci->timer_on = false;
0372 btci->timeout = BRCMF_BTCOEX_OPPR_WIN_TIME;
0373 timer_setup(&btci->timer, brcmf_btcoex_timerfunc, 0);
0374 btci->cfg = cfg;
0375 btci->saved_regs_part1 = false;
0376 btci->saved_regs_part2 = false;
0377
0378 INIT_WORK(&btci->work, brcmf_btcoex_handler);
0379
0380 cfg->btcoex = btci;
0381 return 0;
0382 }
0383
0384
0385
0386
0387
0388 void brcmf_btcoex_detach(struct brcmf_cfg80211_info *cfg)
0389 {
0390 brcmf_dbg(TRACE, "enter\n");
0391
0392 if (!cfg->btcoex)
0393 return;
0394
0395 if (cfg->btcoex->timer_on) {
0396 cfg->btcoex->timer_on = false;
0397 del_timer_sync(&cfg->btcoex->timer);
0398 }
0399
0400 cancel_work_sync(&cfg->btcoex->work);
0401
0402 brcmf_btcoex_boost_wifi(cfg->btcoex, false);
0403 brcmf_btcoex_restore_part1(cfg->btcoex);
0404
0405 kfree(cfg->btcoex);
0406 cfg->btcoex = NULL;
0407 }
0408
0409 static void brcmf_btcoex_dhcp_start(struct brcmf_btcoex_info *btci)
0410 {
0411 struct brcmf_if *ifp = btci->vif->ifp;
0412
0413 btcmf_btcoex_save_part1(btci);
0414
0415 brcmf_btcoex_params_write(ifp, 66, BRCMF_BT_DHCP_REG66);
0416 brcmf_btcoex_params_write(ifp, 41, BRCMF_BT_DHCP_REG41);
0417 brcmf_btcoex_params_write(ifp, 68, BRCMF_BT_DHCP_REG68);
0418 btci->dhcp_done = false;
0419 btci->bt_state = BRCMF_BT_DHCP_START;
0420 schedule_work(&btci->work);
0421 brcmf_dbg(TRACE, "enable BT DHCP Timer\n");
0422 }
0423
0424 static void brcmf_btcoex_dhcp_end(struct brcmf_btcoex_info *btci)
0425 {
0426
0427 btci->dhcp_done = true;
0428 if (btci->timer_on) {
0429 brcmf_dbg(INFO, "disable BT DHCP Timer\n");
0430 btci->timer_on = false;
0431 del_timer_sync(&btci->timer);
0432
0433
0434 if (btci->bt_state != BRCMF_BT_DHCP_IDLE) {
0435 brcmf_dbg(INFO, "bt_state:%d\n",
0436 btci->bt_state);
0437 schedule_work(&btci->work);
0438 }
0439 } else {
0440
0441 brcmf_btcoex_restore_part1(btci);
0442 }
0443 }
0444
0445
0446
0447
0448
0449
0450
0451 int brcmf_btcoex_set_mode(struct brcmf_cfg80211_vif *vif,
0452 enum brcmf_btcoex_mode mode, u16 duration)
0453 {
0454 struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
0455 struct brcmf_btcoex_info *btci = cfg->btcoex;
0456 struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
0457
0458 switch (mode) {
0459 case BRCMF_BTCOEX_DISABLED:
0460 brcmf_dbg(INFO, "DHCP session starts\n");
0461 if (btci->bt_state != BRCMF_BT_DHCP_IDLE)
0462 return -EBUSY;
0463
0464 if (brcmf_btcoex_is_sco_active(ifp)) {
0465 btci->timeout = msecs_to_jiffies(duration);
0466 btci->vif = vif;
0467 brcmf_btcoex_dhcp_start(btci);
0468 }
0469 break;
0470
0471 case BRCMF_BTCOEX_ENABLED:
0472 brcmf_dbg(INFO, "DHCP session ends\n");
0473 if (btci->bt_state != BRCMF_BT_DHCP_IDLE &&
0474 vif == btci->vif) {
0475 brcmf_btcoex_dhcp_end(btci);
0476 }
0477 break;
0478 default:
0479 brcmf_dbg(INFO, "Unknown mode, ignored\n");
0480 }
0481 return 0;
0482 }