root/drivers/net/wireless/broadcom/brcm80211/brcmfmac/btcoex.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. brcmf_btcoex_params_write
  2. brcmf_btcoex_params_read
  3. brcmf_btcoex_boost_wifi
  4. brcmf_btcoex_is_sco_active
  5. btcmf_btcoex_save_part1
  6. brcmf_btcoex_restore_part1
  7. brcmf_btcoex_timerfunc
  8. brcmf_btcoex_handler
  9. brcmf_btcoex_attach
  10. brcmf_btcoex_detach
  11. brcmf_btcoex_dhcp_start
  12. brcmf_btcoex_dhcp_end
  13. brcmf_btcoex_set_mode

   1 // SPDX-License-Identifier: ISC
   2 /*
   3  * Copyright (c) 2013 Broadcom Corporation
   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 /* T1 start SCO/eSCO priority suppression */
  21 #define BRCMF_BTCOEX_OPPR_WIN_TIME   msecs_to_jiffies(2000)
  22 
  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
  32 
  33 /* number of samples for SCO detection */
  34 #define BRCMF_BT_SCO_SAMPLES 12
  35 
  36 /**
  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,
  42 *       boost wifi priority
  43 * @BRCMF_BT_DHCP_FLAG_FORCE_TIMEOUT: wifi priority boost end,
  44 *       restore defaults
  45 */
  46 enum 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  * 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
  67  *      have been saved
  68  * @reg51: saved value of btc_params 51
  69  * @reg64: saved value of btc_params 64
  70  * @reg65: saved value of btc_params 65
  71  * @reg71: saved value of btc_params 71
  72  * @saved_regs_part1: flag indicating regs 50,51,64,65,71
  73  *      have been saved
  74  */
  75 struct 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  * brcmf_btcoex_params_write() - write btc_params firmware variable
  98  * @ifp: interface
  99  * @addr: btc_params register number
 100  * @data: data to write
 101  */
 102 static 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                                         &reg_write, sizeof(reg_write));
 113 }
 114 
 115 /**
 116  * brcmf_btcoex_params_read() - read btc_params firmware variable
 117  * @ifp: interface
 118  * @addr: btc_params register number
 119  * @data: read data
 120  */
 121 static 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  * brcmf_btcoex_boost_wifi() - control BT SCO/eSCO parameters
 130  * @btci: BT coex info
 131  * @trump_sco:
 132  *      true - set SCO/eSCO parameters for compatibility
 133  *              during DHCP window
 134  *      false - restore saved parameter values
 135  *
 136  * Enhanced BT COEX settings for eSCO compatibility during DHCP window
 137  */
 138 static 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                 /* this should reduce eSCO agressive
 145                  * retransmit w/o breaking it
 146                  */
 147 
 148                 /* save current */
 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                 /* pacify the eSco   */
 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                 /* restore previously saved bt params */
 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  * brcmf_btcoex_is_sco_active() - check if SCO/eSCO is active
 191  * @ifp: interface
 192  *
 193  * return: true if SCO/eSCO session is active
 194  */
 195 static 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, &param27);
 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) { /* count both sco & esco  */
 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  * btcmf_btcoex_save_part1() - save first step parameters.
 231  */
 232 static 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                 /* Retrieve and save original reg value */
 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  * brcmf_btcoex_restore_part1() - restore first step parameters.
 251  */
 252 static 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  * brcmf_btcoex_timerfunc() - BT coex timer callback
 271  */
 272 static 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  * brcmf_btcoex_handler() - BT coex state machine work handler
 283  * @work: work
 284  */
 285 static 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                 /* DHCP started provide OPPORTUNITY window
 297                    to get DHCP address
 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                 /* DHCP is not over yet, start lowering BT priority */
 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 
 343 idle:
 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  * brcmf_btcoex_attach() - initialize BT coex data
 354  * @cfg: driver private cfg80211 data
 355  *
 356  * return: 0 on success
 357  */
 358 int 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         /* Set up timer for BT  */
 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  * brcmf_btcoex_detach - clean BT coex data
 385  * @cfg: driver private cfg80211 data
 386  */
 387 void 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 
 408 static 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         /* set new regs values */
 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 
 423 static void brcmf_btcoex_dhcp_end(struct brcmf_btcoex_info *btci)
 424 {
 425         /* Stop any bt timer because DHCP session is done */
 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                 /* schedule worker if transition to IDLE is needed */
 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                 /* Restore original values */
 440                 brcmf_btcoex_restore_part1(btci);
 441         }
 442 }
 443 
 444 /**
 445  * brcmf_btcoex_set_mode - set BT coex mode
 446  * @cfg: driver private cfg80211 data
 447  * @mode: Wifi-Bluetooth coexistence mode
 448  *
 449  * return: 0 on success
 450  */
 451 int 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                 /* Start BT timer only for SCO connection */
 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 }

/* [<][>][^][v][top][bottom][index][help] */