linux/drivers/staging/rtl8712/rtl8712_cmd.c
<<
>>
Prefs
   1/******************************************************************************
   2 * rtl8712_cmd.c
   3 *
   4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
   5 * Linux device driver for RTL8192SU
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of version 2 of the GNU General Public License as
   9 * published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along with
  17 * this program; if not, write to the Free Software Foundation, Inc.,
  18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  19 *
  20 * Modifications for inclusion into the Linux staging tree are
  21 * Copyright(c) 2010 Larry Finger. All rights reserved.
  22 *
  23 * Contact information:
  24 * WLAN FAE <wlanfae@realtek.com>.
  25 * Larry Finger <Larry.Finger@lwfinger.net>
  26 *
  27 ******************************************************************************/
  28
  29#define _RTL8712_CMD_C_
  30
  31#include <linux/compiler.h>
  32#include <linux/kernel.h>
  33#include <linux/errno.h>
  34#include <linux/init.h>
  35#include <linux/slab.h>
  36#include <linux/module.h>
  37#include <linux/kref.h>
  38#include <linux/netdevice.h>
  39#include <linux/skbuff.h>
  40#include <linux/usb.h>
  41#include <linux/usb/ch9.h>
  42#include <linux/circ_buf.h>
  43#include <linux/uaccess.h>
  44#include <asm/byteorder.h>
  45#include <linux/atomic.h>
  46#include <linux/semaphore.h>
  47#include <linux/rtnetlink.h>
  48
  49#include "osdep_service.h"
  50#include "drv_types.h"
  51#include "recv_osdep.h"
  52#include "mlme_osdep.h"
  53#include "rtl871x_ioctl_set.h"
  54
  55static void check_hw_pbc(struct _adapter *padapter)
  56{
  57        u8      tmp1byte;
  58
  59        r8712_write8(padapter, MAC_PINMUX_CTRL, (GPIOMUX_EN | GPIOSEL_GPIO));
  60        tmp1byte = r8712_read8(padapter, GPIO_IO_SEL);
  61        tmp1byte &= ~(HAL_8192S_HW_GPIO_WPS_BIT);
  62        r8712_write8(padapter, GPIO_IO_SEL, tmp1byte);
  63        tmp1byte = r8712_read8(padapter, GPIO_CTRL);
  64        if (tmp1byte == 0xff)
  65                return ;
  66        if (tmp1byte&HAL_8192S_HW_GPIO_WPS_BIT) {
  67                /* Here we only set bPbcPressed to true
  68                 * After trigger PBC, the variable will be set to false */
  69                DBG_8712("CheckPbcGPIO - PBC is pressed !!!!\n");
  70                /* 0 is the default value and it means the application monitors
  71                 * the HW PBC doesn't provide its pid to driver. */
  72                if (padapter->pid == 0)
  73                        return;
  74                kill_pid(find_vpid(padapter->pid), SIGUSR1, 1);
  75        }
  76}
  77
  78/* query rx phy status from fw.
  79 * Adhoc mode: beacon.
  80 * Infrastructure mode: beacon , data. */
  81static void query_fw_rx_phy_status(struct _adapter *padapter)
  82{
  83        u32 val32 = 0;
  84        int pollingcnts = 50;
  85
  86        if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == true) {
  87                r8712_write32(padapter, IOCMD_CTRL_REG, 0xf4000001);
  88                msleep(100);
  89                /* Wait FW complete IO Cmd */
  90                while ((r8712_read32(padapter, IOCMD_CTRL_REG)) &&
  91                       (pollingcnts > 0)) {
  92                        pollingcnts--;
  93                        msleep(20);
  94                }
  95                if (pollingcnts != 0)
  96                        val32 = r8712_read32(padapter, IOCMD_DATA_REG);
  97                else /* time out */
  98                        val32 = 0;
  99                val32 = val32 >> 4;
 100                padapter->recvpriv.fw_rssi =
 101                         (u8)r8712_signal_scale_mapping(val32);
 102        }
 103}
 104
 105/* check mlme, hw, phy, or dynamic algorithm status. */
 106static void StatusWatchdogCallback(struct _adapter *padapter)
 107{
 108        check_hw_pbc(padapter);
 109        query_fw_rx_phy_status(padapter);
 110}
 111
 112static void r871x_internal_cmd_hdl(struct _adapter *padapter, u8 *pbuf)
 113{
 114        struct drvint_cmd_parm *pdrvcmd;
 115
 116        if (!pbuf)
 117                return;
 118        pdrvcmd = (struct drvint_cmd_parm *)pbuf;
 119        switch (pdrvcmd->i_cid) {
 120        case WDG_WK_CID:
 121                StatusWatchdogCallback(padapter);
 122                break;
 123        default:
 124                break;
 125        }
 126        kfree(pdrvcmd->pbuf);
 127}
 128
 129static u8 read_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
 130{
 131        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
 132        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 133
 134        /*  invoke cmd->callback function */
 135        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 136        if (pcmd_callback == NULL)
 137                r8712_free_cmd_obj(pcmd);
 138        else
 139                pcmd_callback(padapter, pcmd);
 140        return H2C_SUCCESS;
 141}
 142
 143static u8 write_macreg_hdl(struct _adapter *padapter, u8 *pbuf)
 144{
 145        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
 146        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 147
 148        /*  invoke cmd->callback function */
 149        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 150        if (pcmd_callback == NULL)
 151                r8712_free_cmd_obj(pcmd);
 152        else
 153                pcmd_callback(padapter, pcmd);
 154        return H2C_SUCCESS;
 155}
 156
 157static u8 read_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
 158{
 159        u32 val;
 160        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj      *pcmd);
 161        struct readBB_parm *prdbbparm;
 162        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 163
 164        prdbbparm = (struct readBB_parm *)pcmd->parmbuf;
 165        if (pcmd->rsp && pcmd->rspsz > 0)
 166                memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
 167        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 168        if (pcmd_callback == NULL)
 169                r8712_free_cmd_obj(pcmd);
 170        else
 171                pcmd_callback(padapter, pcmd);
 172        return H2C_SUCCESS;
 173}
 174
 175static u8 write_bbreg_hdl(struct _adapter *padapter, u8 *pbuf)
 176{
 177        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
 178        struct writeBB_parm *pwritebbparm;
 179        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 180
 181        pwritebbparm = (struct writeBB_parm *)pcmd->parmbuf;
 182        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 183        if (pcmd_callback == NULL)
 184                r8712_free_cmd_obj(pcmd);
 185        else
 186                pcmd_callback(padapter, pcmd);
 187        return H2C_SUCCESS;
 188}
 189
 190static u8 read_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
 191{
 192        u32 val;
 193        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
 194        struct readRF_parm *prdrfparm;
 195        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 196
 197        prdrfparm = (struct readRF_parm *)pcmd->parmbuf;
 198        if (pcmd->rsp && pcmd->rspsz > 0)
 199                memcpy(pcmd->rsp, (u8 *)&val, pcmd->rspsz);
 200        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 201        if (pcmd_callback == NULL)
 202                r8712_free_cmd_obj(pcmd);
 203        else
 204                pcmd_callback(padapter, pcmd);
 205        return H2C_SUCCESS;
 206}
 207
 208static u8 write_rfreg_hdl(struct _adapter *padapter, u8 *pbuf)
 209{
 210        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
 211        struct writeRF_parm *pwriterfparm;
 212        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 213
 214        pwriterfparm = (struct writeRF_parm *)pcmd->parmbuf;
 215        pcmd_callback = cmd_callback[pcmd->cmdcode].callback;
 216        if (pcmd_callback == NULL)
 217                r8712_free_cmd_obj(pcmd);
 218        else
 219                pcmd_callback(padapter, pcmd);
 220        return H2C_SUCCESS;
 221}
 222
 223static u8 sys_suspend_hdl(struct _adapter *padapter, u8 *pbuf)
 224{
 225        struct cmd_obj *pcmd  = (struct cmd_obj *)pbuf;
 226        struct usb_suspend_parm *psetusbsuspend;
 227
 228        psetusbsuspend = (struct usb_suspend_parm *)pcmd->parmbuf;
 229        r8712_free_cmd_obj(pcmd);
 230        return H2C_SUCCESS;
 231}
 232
 233static struct cmd_obj *cmd_hdl_filter(struct _adapter *padapter,
 234                                      struct cmd_obj *pcmd)
 235{
 236        struct cmd_obj *pcmd_r;
 237
 238        if (pcmd == NULL)
 239                return pcmd;
 240        pcmd_r = NULL;
 241
 242        switch (pcmd->cmdcode) {
 243        case GEN_CMD_CODE(_Read_MACREG):
 244                read_macreg_hdl(padapter, (u8 *)pcmd);
 245                pcmd_r = pcmd;
 246                break;
 247        case GEN_CMD_CODE(_Write_MACREG):
 248                write_macreg_hdl(padapter, (u8 *)pcmd);
 249                pcmd_r = pcmd;
 250                break;
 251        case GEN_CMD_CODE(_Read_BBREG):
 252                read_bbreg_hdl(padapter, (u8 *)pcmd);
 253                break;
 254        case GEN_CMD_CODE(_Write_BBREG):
 255                write_bbreg_hdl(padapter, (u8 *)pcmd);
 256                break;
 257        case GEN_CMD_CODE(_Read_RFREG):
 258                read_rfreg_hdl(padapter, (u8 *)pcmd);
 259                break;
 260        case GEN_CMD_CODE(_Write_RFREG):
 261                write_rfreg_hdl(padapter, (u8 *)pcmd);
 262                break;
 263        case GEN_CMD_CODE(_SetUsbSuspend):
 264                sys_suspend_hdl(padapter, (u8 *)pcmd);
 265                break;
 266        case GEN_CMD_CODE(_JoinBss):
 267                r8712_joinbss_reset(padapter);
 268                /* Before set JoinBss_CMD to FW, driver must ensure FW is in
 269                 * PS_MODE_ACTIVE. Directly write rpwm to radio on and assign
 270                 * new pwr_mode to Driver, instead of use workitem to change
 271                 * state. */
 272                if (padapter->pwrctrlpriv.pwr_mode > PS_MODE_ACTIVE) {
 273                        padapter->pwrctrlpriv.pwr_mode = PS_MODE_ACTIVE;
 274                        _enter_pwrlock(&(padapter->pwrctrlpriv.lock));
 275                        r8712_set_rpwm(padapter, PS_STATE_S4);
 276                        up(&(padapter->pwrctrlpriv.lock));
 277                }
 278                pcmd_r = pcmd;
 279                break;
 280        case _DRV_INT_CMD_:
 281                r871x_internal_cmd_hdl(padapter, pcmd->parmbuf);
 282                r8712_free_cmd_obj(pcmd);
 283                pcmd_r = NULL;
 284                break;
 285        default:
 286                pcmd_r = pcmd;
 287                break;
 288        }
 289        return pcmd_r; /* if returning pcmd_r == NULL, pcmd must be free. */
 290}
 291
 292static u8 check_cmd_fifo(struct _adapter *padapter, uint sz)
 293{
 294        u8 res = _SUCCESS;
 295        return res;
 296}
 297
 298u8 r8712_fw_cmd(struct _adapter *pAdapter, u32 cmd)
 299{
 300        int pollingcnts = 50;
 301
 302        r8712_write32(pAdapter, IOCMD_CTRL_REG, cmd);
 303        msleep(100);
 304        while ((0 != r8712_read32(pAdapter, IOCMD_CTRL_REG)) &&
 305               (pollingcnts > 0)) {
 306                pollingcnts--;
 307                msleep(20);
 308        }
 309        if (pollingcnts == 0)
 310                return false;
 311        return true;
 312}
 313
 314void r8712_fw_cmd_data(struct _adapter *pAdapter, u32 *value, u8 flag)
 315{
 316        if (flag == 0)  /* set */
 317                r8712_write32(pAdapter, IOCMD_DATA_REG, *value);
 318        else            /* query */
 319                *value = r8712_read32(pAdapter, IOCMD_DATA_REG);
 320}
 321
 322int r8712_cmd_thread(void *context)
 323{
 324        struct cmd_obj *pcmd;
 325        unsigned int cmdsz, wr_sz, *pcmdbuf, *prspbuf;
 326        struct tx_desc *pdesc;
 327        void (*pcmd_callback)(struct _adapter *dev, struct cmd_obj *pcmd);
 328        struct _adapter *padapter = (struct _adapter *)context;
 329        struct  cmd_priv        *pcmdpriv = &(padapter->cmdpriv);
 330
 331        thread_enter(padapter);
 332        while (1) {
 333                if ((_down_sema(&(pcmdpriv->cmd_queue_sema))) == _FAIL)
 334                        break;
 335                if ((padapter->bDriverStopped == true) ||
 336                    (padapter->bSurpriseRemoved == true))
 337                        break;
 338                if (r8712_register_cmd_alive(padapter) != _SUCCESS)
 339                        continue;
 340_next:
 341                pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
 342                if (!(pcmd)) {
 343                        r8712_unregister_cmd_alive(padapter);
 344                        continue;
 345                }
 346                pcmdbuf = (unsigned int *)pcmdpriv->cmd_buf;
 347                prspbuf = (unsigned int *)pcmdpriv->rsp_buf;
 348                pdesc = (struct tx_desc *)pcmdbuf;
 349                memset(pdesc, 0, TXDESC_SIZE);
 350                pcmd = cmd_hdl_filter(padapter, pcmd);
 351                if (pcmd) { /* if pcmd != NULL, cmd will be handled by f/w */
 352                        struct dvobj_priv *pdvobj = (struct dvobj_priv *)
 353                                                    &padapter->dvobjpriv;
 354                        u8 blnPending = 0;
 355                        pcmdpriv->cmd_issued_cnt++;
 356                        cmdsz = _RND8((pcmd->cmdsz)); /* _RND8  */
 357                        wr_sz = TXDESC_SIZE + 8 + cmdsz;
 358                        pdesc->txdw0 |= cpu_to_le32((wr_sz-TXDESC_SIZE) &
 359                                                     0x0000ffff);
 360                        if (pdvobj->ishighspeed) {
 361                                if ((wr_sz % 512) == 0)
 362                                        blnPending = 1;
 363                        } else {
 364                                if ((wr_sz % 64) == 0)
 365                                        blnPending = 1;
 366                        }
 367                        if (blnPending) /* 32 bytes for TX Desc - 8 offset */
 368                                pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
 369                                                OFFSET_SZ + 8) << OFFSET_SHT) &
 370                                                0x00ff0000);
 371                        else {
 372                                pdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE +
 373                                                              OFFSET_SZ) <<
 374                                                              OFFSET_SHT) &
 375                                                              0x00ff0000);
 376                        }
 377                        pdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
 378                        pdesc->txdw1 |= cpu_to_le32((0x13 << QSEL_SHT) &
 379                                                    0x00001f00);
 380                        pcmdbuf += (TXDESC_SIZE >> 2);
 381                        *pcmdbuf = cpu_to_le32((cmdsz & 0x0000ffff) |
 382                                               (pcmd->cmdcode << 16) |
 383                                               (pcmdpriv->cmd_seq << 24));
 384                        pcmdbuf += 2 ; /* 8 bytes alignment */
 385                        memcpy((u8 *)pcmdbuf, pcmd->parmbuf, pcmd->cmdsz);
 386                        while (check_cmd_fifo(padapter, wr_sz) == _FAIL) {
 387                                if ((padapter->bDriverStopped == true) ||
 388                                    (padapter->bSurpriseRemoved == true))
 389                                        break;
 390                                msleep(100);
 391                                continue;
 392                        }
 393                        if (blnPending)
 394                                wr_sz += 8;   /* Append 8 bytes */
 395                        r8712_write_mem(padapter, RTL8712_DMA_H2CCMD, wr_sz,
 396                                       (u8 *)pdesc);
 397                        pcmdpriv->cmd_seq++;
 398                        if (pcmd->cmdcode == GEN_CMD_CODE(_CreateBss)) {
 399                                pcmd->res = H2C_SUCCESS;
 400                                pcmd_callback = cmd_callback[pcmd->
 401                                                cmdcode].callback;
 402                                if (pcmd_callback)
 403                                        pcmd_callback(padapter, pcmd);
 404                                continue;
 405                        }
 406                        if (pcmd->cmdcode == GEN_CMD_CODE(_SetPwrMode)) {
 407                                if (padapter->pwrctrlpriv.bSleep) {
 408                                        _enter_pwrlock(&(padapter->
 409                                                       pwrctrlpriv.lock));
 410                                        r8712_set_rpwm(padapter, PS_STATE_S2);
 411                                        up(&padapter->pwrctrlpriv.lock);
 412                                }
 413                        }
 414                        r8712_free_cmd_obj(pcmd);
 415                        if (_queue_empty(&(pcmdpriv->cmd_queue))) {
 416                                r8712_unregister_cmd_alive(padapter);
 417                                continue;
 418                        } else
 419                                goto _next;
 420                } else
 421                        goto _next;
 422                flush_signals_thread();
 423        }
 424        /* free all cmd_obj resources */
 425        do {
 426                pcmd = r8712_dequeue_cmd(&(pcmdpriv->cmd_queue));
 427                if (pcmd == NULL)
 428                        break;
 429                r8712_free_cmd_obj(pcmd);
 430        } while (1);
 431        up(&pcmdpriv->terminate_cmdthread_sema);
 432        thread_exit();
 433}
 434
 435void r8712_event_handle(struct _adapter *padapter, uint *peventbuf)
 436{
 437        u8 evt_code, evt_seq;
 438        u16 evt_sz;
 439        void (*event_callback)(struct _adapter *dev, u8 *pbuf);
 440        struct  evt_priv *pevt_priv = &(padapter->evtpriv);
 441
 442        if (peventbuf == NULL)
 443                goto _abort_event_;
 444        evt_sz = (u16)(le32_to_cpu(*peventbuf) & 0xffff);
 445        evt_seq = (u8)((le32_to_cpu(*peventbuf) >> 24) & 0x7f);
 446        evt_code = (u8)((le32_to_cpu(*peventbuf) >> 16) & 0xff);
 447        /* checking event sequence... */
 448        if ((evt_seq & 0x7f) != pevt_priv->event_seq) {
 449                pevt_priv->event_seq = ((evt_seq + 1) & 0x7f);
 450                goto _abort_event_;
 451        }
 452        /* checking if event code is valid */
 453        if (evt_code >= MAX_C2HEVT) {
 454                pevt_priv->event_seq = ((evt_seq+1) & 0x7f);
 455                goto _abort_event_;
 456        } else if ((evt_code == GEN_EVT_CODE(_Survey)) &&
 457                   (evt_sz > sizeof(struct wlan_bssid_ex))) {
 458                pevt_priv->event_seq = ((evt_seq+1)&0x7f);
 459                goto _abort_event_;
 460        }
 461        /* checking if event size match the event parm size */
 462        if ((wlanevents[evt_code].parmsize) &&
 463            (wlanevents[evt_code].parmsize != evt_sz)) {
 464                pevt_priv->event_seq = ((evt_seq+1)&0x7f);
 465                goto _abort_event_;
 466        } else if ((evt_sz == 0) && (evt_code != GEN_EVT_CODE(_WPS_PBC))) {
 467                pevt_priv->event_seq = ((evt_seq+1)&0x7f);
 468                goto _abort_event_;
 469        }
 470        pevt_priv->event_seq++; /* update evt_seq */
 471        if (pevt_priv->event_seq > 127)
 472                pevt_priv->event_seq = 0;
 473        peventbuf = peventbuf + 2; /* move to event content, 8 bytes alignment */
 474        if (peventbuf) {
 475                event_callback = wlanevents[evt_code].event_callback;
 476                if (event_callback)
 477                        event_callback(padapter, (u8 *)peventbuf);
 478        }
 479        pevt_priv->evt_done_cnt++;
 480_abort_event_:
 481        return;
 482}
 483