Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /******************************************************************************
0003  * rtl8712_cmd.c
0004  *
0005  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
0006  * Linux device driver for RTL8192SU
0007  *
0008  * Modifications for inclusion into the Linux staging tree are
0009  * Copyright(c) 2010 Larry Finger. All rights reserved.
0010  *
0011  * Contact information:
0012  * WLAN FAE <wlanfae@realtek.com>.
0013  * Larry Finger <Larry.Finger@lwfinger.net>
0014  *
0015  ******************************************************************************/
0016 
0017 #define _RTL8712_CMD_C_
0018 
0019 #include <linux/compiler.h>
0020 #include <linux/kernel.h>
0021 #include <linux/errno.h>
0022 #include <linux/slab.h>
0023 #include <linux/sched/signal.h>
0024 #include <linux/module.h>
0025 #include <linux/kref.h>
0026 #include <linux/netdevice.h>
0027 #include <linux/skbuff.h>
0028 #include <linux/usb.h>
0029 #include <linux/usb/ch9.h>
0030 #include <linux/circ_buf.h>
0031 #include <linux/uaccess.h>
0032 #include <asm/byteorder.h>
0033 #include <linux/atomic.h>
0034 #include <linux/semaphore.h>
0035 #include <linux/rtnetlink.h>
0036 
0037 #include "osdep_service.h"
0038 #include "drv_types.h"
0039 #include "recv_osdep.h"
0040 #include "mlme_osdep.h"
0041 #include "rtl871x_ioctl_set.h"
0042 
0043 static void check_hw_pbc(struct _adapter *padapter)
0044 {
0045     u8  tmp1byte;
0046 
0047     r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
0048     tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
0049     tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
0050     r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
0051     tmp1byte = r8712_read8(padapter, GPIO_CTRL);
0052     if (tmp1byte == 0xff)
0053         return;
0054     if (tmp1byte & HAL_8192S_HW_GPIO_WPS_BIT) {
0055         /* Here we only set bPbcPressed to true
0056          * After trigger PBC, the variable will be set to false
0057          */
0058         netdev_dbg(padapter->pnetdev, "CheckPbcGPIO - PBC is pressed !!!!\n");
0059         /* 0 is the default value and it means the application monitors
0060          * the HW PBC doesn't provide its pid to driver.
0061          */
0062         if (padapter->pid == 0)
0063             return;
0064         kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
0065     }
0066 }
0067 
0068 /* query rx phy status from fw.
0069  * Adhoc mode: beacon.
0070  * Infrastructure mode: beacon , data.
0071  */
0072 static void query_fw_rx_phy_status(struct _adapter *padapter)
0073 {
0074     u32 val32 = 0;
0075     int pollingcnts = 50;
0076 
0077     if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
0078         r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
0079         msleep(100);
0080         /* Wait FW complete IO Cmd */
0081         while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
0082                (pollingcnts > 0)) {
0083             pollingcnts--;
0084             msleep(20);
0085         }
0086         if (pollingcnts != 0)
0087             val32 = r8712_read32(padapter, IOCMD_DATA_REG);
0088         else /* time out */
0089             val32 = 0;
0090         val32 >>= 4;
0091         padapter->recvpriv.fw_rssi =
0092              (u8)r8712_signal_scale_mapping(val32);
0093     }
0094 }
0095 
0096 /* check mlme, hw, phy, or dynamic algorithm status. */
0097 static void StatusWatchdogCallback(struct _adapter *padapter)
0098 {
0099     check_hw_pbc(padapter);
0100     query_fw_rx_phy_status(padapter);
0101 }
0102 
0103 static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
0104 {
0105     struct drvint_cmd_parm *pdrvcmd;
0106 
0107     if (!pbuf)
0108         return;
0109     pdrvcmd = (struct drvint_cmd_parm *)pbuf;
0110     switch (pdrvcmd->i_cid) {
0111     case WDG_WK_CID:
0112         StatusWatchdogCallback(padapter);
0113         break;
0114     default:
0115         break;
0116     }
0117     kfree(pdrvcmd->pbuf);
0118 }
0119 
0120 static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
0121 {
0122     struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
0123 
0124     r8712_free_cmd_obj(pcmd);
0125     return H2C_SUCCESS;
0126 }
0127 
0128 static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
0129 {
0130     void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
0131     struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
0132 
0133     pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
0134     if (!pcmd_callback)
0135         r8712_free_cmd_obj(pcmd);
0136     else
0137         pcmd_callback(padapter, pcmd);
0138     return H2C_SUCCESS;
0139 }
0140 
0141 static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
0142 {
0143     u32 val;
0144     void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
0145     struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
0146 
0147     if (pcmd->rsp && pcmd->rspsz > 0)
0148         memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
0149     pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
0150     if (!pcmd_callback)
0151         r8712_free_cmd_obj(pcmd);
0152     else
0153         pcmd_callback(padapter, pcmd);
0154     return H2C_SUCCESS;
0155 }
0156 
0157 static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
0158 {
0159     void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
0160     struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
0161 
0162     pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
0163     if (!pcmd_callback)
0164         r8712_free_cmd_obj(pcmd);
0165     else
0166         pcmd_callback(padapter, pcmd);
0167     return H2C_SUCCESS;
0168 }
0169 
0170 static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
0171 {
0172     struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
0173 
0174     r8712_free_cmd_obj(pcmd);
0175     return H2C_SUCCESS;
0176 }
0177 
0178 static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
0179                       struct cmd_obj *pcmd)
0180 {
0181     struct cmd_obj *pcmd_r;
0182 
0183     if (!pcmd)
0184         return pcmd;
0185     pcmd_r = NULL;
0186 
0187     switch (pcmd->cmdcode) {
0188     case GEN_CMD_CODE(_Read_BBREG):
0189         read_bbreg_hdl(padapter, (u8 *)pcmd);
0190         break;
0191     case GEN_CMD_CODE(_Write_BBREG):
0192         write_bbreg_hdl(padapter, (u8 *)pcmd);
0193         break;
0194     case GEN_CMD_CODE(_Read_RFREG):
0195         read_rfreg_hdl(padapter, (u8 *)pcmd);
0196         break;
0197     case GEN_CMD_CODE(_Write_RFREG):
0198         write_rfreg_hdl(padapter, (u8 *)pcmd);
0199         break;
0200     case GEN_CMD_CODE(_SetUsbSuspend):
0201         sys_suspend_hdl(padapter, (u8 *)pcmd);
0202         break;
0203     case GEN_CMD_CODE(_JoinBss):
0204         r8712_joinbss_reset(padapter);
0205         /* Before set JoinBss_CMD to FW, driver must ensure FW is in
0206          * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
0207          * new pwr_mode to Driver, instead of use workitem to change
0208          * state.
0209          */
0210         if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
0211             padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
0212             mutex_lock(&padapter->pwrctrlpriv.mutex_lock);
0213             r8712_set_rpwm(padapter, PS_STATE_S4);
0214             mutex_unlock(&padapter->pwrctrlpriv.mutex_lock);
0215         }
0216         pcmd_r = pcmd;
0217         break;
0218     case _DRV_INT_CMD_:
0219         r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
0220         r8712_free_cmd_obj(pcmd);
0221         pcmd_r = NULL;
0222         break;
0223     default:
0224         pcmd_r = pcmd;
0225         break;
0226     }
0227     return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
0228 }
0229 
0230 u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
0231 {
0232     int pollingcnts = 50;
0233 
0234     r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
0235     msleep(100);
0236     while ((r8712_read32(pAdapter, IOCMD_CTRL_REG != 0)) &&
0237            (pollingcnts > 0)) {
0238         pollingcnts--;
0239         msleep(20);
0240     }
0241     if (pollingcnts == 0)
0242         return false;
0243     return true;
0244 }
0245 
0246 void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
0247 {
0248     if (flag == 0)  /* set */
0249         r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
0250     else        /* query */
0251         *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
0252 }
0253 
0254 int r8712_cmd_thread(void *context)
0255 {
0256     struct cmd_obj *pcmd;
0257     unsigned int cmdsz, wr_sz;
0258     __le32 *pcmdbuf;
0259     struct tx_desc *pdesc;
0260     void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
0261     struct _adapter *padapter = context;
0262     struct  cmd_priv *pcmdpriv = &padapter->cmdpriv;
0263     struct completion *cmd_queue_comp =
0264         &pcmdpriv->cmd_queue_comp;
0265     struct mutex *pwctrl_lock = &padapter->pwrctrlpriv.mutex_lock;
0266 
0267     allow_signal(SIGTERM);
0268     while (1) {
0269         if (wait_for_completion_interruptible(cmd_queue_comp))
0270             break;
0271         if (padapter->driver_stopped || padapter->surprise_removed)
0272             break;
0273         if (r8712_register_cmd_alive(padapter))
0274             continue;
0275 _next:
0276         pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
0277         if (!(pcmd)) {
0278             r8712_unregister_cmd_alive(padapter);
0279             continue;
0280         }
0281         pcmdbuf = (__le32 *)pcmdpriv->cmd_buf;
0282         pdesc = (struct tx_desc *)pcmdbuf;
0283         memset(pdesc, 0, TXDESC_SIZE);
0284         pcmd = cmd_hdl_filter(padapter, pcmd);
0285         if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
0286             struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
0287             u8 blnPending = 0;
0288             u16 cmdcode = pcmd->cmdcode;
0289 
0290             pcmdpriv->cmd_issued_cnt++;
0291             cmdsz = round_up(pcmd->cmdsz, 8);
0292             wr_sz = TXDESC_SIZE + 8 + cmdsz;
0293             pdesc->txdw0 |= cpu_to_le32((wr_sz - TXDESC_SIZE) &
0294                              0x0000ffff);
0295             if (pdvobj->ishighspeed) {
0296                 if ((wr_sz % 512) == 0)
0297                     blnPending = 1;
0298             } else {
0299                 if ((wr_sz % 64) == 0)
0300                     blnPending = 1;
0301             }
0302             if (blnPending) { /* 32 bytes for TX Desc - 8 offset */
0303                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
0304                         OFFSET_SZ + 8) << OFFSET_SHT) &
0305                         0x00ff0000);
0306             } else {
0307                 pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
0308                                   OFFSET_SZ) <<
0309                                   OFFSET_SHT) &
0310                                   0x00ff0000);
0311             }
0312             pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
0313             pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
0314                             0x00001f00);
0315             pcmdbuf += (TXDESC_SIZE >> 2);
0316             *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
0317                            (pcmd->cmdcode << 16) |
0318                            (pcmdpriv->cmd_seq << 24));
0319             pcmdbuf += 2; /* 8 bytes alignment */
0320             memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
0321             if (blnPending)
0322                 wr_sz += 8;   /* Append 8 bytes */
0323             r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
0324                     (u8 *)pdesc);
0325             pcmdpriv->cmd_seq++;
0326             if (cmdcode == GEN_CMD_CODE(_CreateBss)) {
0327                 pcmd->res = H2C_SUCCESS;
0328                 pcmd_callback = cmd_callback[cmdcode].callback;
0329                 if (pcmd_callback)
0330                     pcmd_callback(padapter, pcmd);
0331                 continue;
0332             }
0333             if (cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
0334                 if (padapter->pwrctrlpriv.bSleep) {
0335                     mutex_lock(pwctrl_lock);
0336                     r8712_set_rpwm(padapter, PS_STATE_S2);
0337                     mutex_unlock(pwctrl_lock);
0338                 }
0339             }
0340             r8712_free_cmd_obj(pcmd);
0341             if (list_empty(&pcmdpriv->cmd_queue.queue)) {
0342                 r8712_unregister_cmd_alive(padapter);
0343                 continue;
0344             } else {
0345                 goto _next;
0346             }
0347         } else {
0348             goto _next;
0349         }
0350         flush_signals_thread();
0351     }
0352     /* free all cmd_obj resources */
0353     do {
0354         pcmd = r8712_dequeue_cmd(&pcmdpriv->cmd_queue);
0355         if (!pcmd)
0356             break;
0357         r8712_free_cmd_obj(pcmd);
0358     } while (1);
0359     complete(&pcmdpriv->terminate_cmdthread_comp);
0360     return 0;
0361 }
0362 
0363 void r8712_event_handle(struct _adapter *padapter, __le32 *peventbuf)
0364 {
0365     u8 evt_code, evt_seq;
0366     u16 evt_sz;
0367     void (*event_callback)(struct _adapter *dev, u8 *pbuf);
0368     struct  evt_priv *pevt_priv = &padapter->evtpriv;
0369 
0370     if (!peventbuf)
0371         goto _abort_event_;
0372     evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
0373     evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
0374     evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
0375     /* checking event sequence... */
0376     if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
0377         pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
0378         goto _abort_event_;
0379     }
0380     /* checking if event code is valid */
0381     if (evt_code >= MAX_C2HEVT) {
0382         pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
0383         goto _abort_event_;
0384     } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
0385            (evt_sz > sizeof(struct wlan_bssid_ex))) {
0386         pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
0387         goto _abort_event_;
0388     }
0389     /* checking if event size match the event parm size */
0390     if ((wlanevents[evt_code].parmsize) &&
0391         (wlanevents[evt_code].parmsize != evt_sz)) {
0392         pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
0393         goto _abort_event_;
0394     } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
0395         pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
0396         goto _abort_event_;
0397     }
0398     pevt_priv->event_seq++; /* update evt_seq */
0399     if (pevt_priv->event_seq > 127)
0400         pevt_priv->event_seq = 0;
0401     /* move to event content, 8 bytes alignment */
0402     peventbuf = peventbuf + 2;
0403     event_callback = wlanevents[evt_code].event_callback;
0404     if (event_callback)
0405         event_callback(padapter, (u8 *)peventbuf);
0406     pevt_priv->evt_done_cnt++;
0407 _abort_event_:
0408     return;
0409 }