linux/drivers/staging/rtl8192u/r819xU_cmdpkt.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 *  (c) Copyright 2008, RealTEK Technologies Inc. All Rights Reserved.
   4 *
   5 *  Module:     r819xusb_cmdpkt.c
   6 *              (RTL8190 TX/RX command packet handler Source C File)
   7 *
   8 *  Note:       The module is responsible for handling TX and RX command packet.
   9 *              1. TX : Send set and query configuration command packet.
  10 *              2. RX : Receive tx feedback, beacon state, query configuration
  11 *                      command packet.
  12 *
  13 *  Function:
  14 *
  15 *  Export:
  16 *
  17 *  Abbrev:
  18 *
  19 *  History:
  20 *
  21 *      Date            Who             Remark
  22 *      05/06/2008      amy             Create initial version porting from
  23 *                                      windows driver.
  24 *
  25 ******************************************************************************/
  26#include "r8192U.h"
  27#include "r819xU_cmdpkt.h"
  28
  29rt_status SendTxCommandPacket(struct net_device *dev, void *pData, u32 DataLen)
  30{
  31        struct r8192_priv   *priv = ieee80211_priv(dev);
  32        struct sk_buff      *skb;
  33        struct cb_desc      *tcb_desc;
  34        unsigned char       *ptr_buf;
  35
  36        /* Get TCB and local buffer from common pool.
  37         * (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
  38         */
  39        skb  = dev_alloc_skb(USB_HWDESC_HEADER_LEN + DataLen + 4);
  40        if (!skb)
  41                return RT_STATUS_FAILURE;
  42        memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
  43        tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
  44        tcb_desc->queue_index = TXCMD_QUEUE;
  45        tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_NORMAL;
  46        tcb_desc->bLastIniPkt = 0;
  47        skb_reserve(skb, USB_HWDESC_HEADER_LEN);
  48        ptr_buf = skb_put(skb, DataLen);
  49        memcpy(ptr_buf, pData, DataLen);
  50        tcb_desc->txbuf_size = (u16)DataLen;
  51
  52        if (!priv->ieee80211->check_nic_enough_desc(dev, tcb_desc->queue_index) ||
  53            (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index])) ||
  54            (priv->ieee80211->queue_stop)) {
  55                RT_TRACE(COMP_FIRMWARE, "=== NULL packet ======> tx full!\n");
  56                skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
  57        } else {
  58                priv->ieee80211->softmac_hard_start_xmit(skb, dev);
  59        }
  60
  61        return RT_STATUS_SUCCESS;
  62}
  63
  64/*-----------------------------------------------------------------------------
  65 * Function:    cmpk_counttxstatistic()
  66 *
  67 * Overview:
  68 *
  69 * Input:       PADAPTER        pAdapter
  70 *              CMPK_TXFB_T     *psTx_FB
  71 *
  72 * Output:      NONE
  73 *
  74 * Return:      NONE
  75 *
  76 * Revised History:
  77 *  When                Who     Remark
  78 *  05/12/2008          amy     Create Version 0 porting from windows code.
  79 *
  80 *---------------------------------------------------------------------------
  81 */
  82static void cmpk_count_txstatistic(struct net_device *dev, cmpk_txfb_t *pstx_fb)
  83{
  84        struct r8192_priv *priv = ieee80211_priv(dev);
  85#ifdef ENABLE_PS
  86        RT_RF_POWER_STATE       rtState;
  87
  88        pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
  89                                          (pu1Byte)(&rtState));
  90
  91        /* When RF is off, we should not count the packet for hw/sw synchronize
  92         * reason, ie. there may be a duration while sw switch is changed and
  93         * hw switch is being changed.
  94         */
  95        if (rtState == eRfOff)
  96                return;
  97#endif
  98
  99#ifdef TODO
 100        if (pAdapter->bInHctTest)
 101                return;
 102#endif
 103        /* We can not know the packet length and transmit type:
 104         * broadcast or uni or multicast. So the relative statistics
 105         * must be collected in tx feedback info.
 106         */
 107        if (pstx_fb->tok) {
 108                priv->stats.txfeedbackok++;
 109                priv->stats.txoktotal++;
 110                priv->stats.txokbytestotal += pstx_fb->pkt_length;
 111                priv->stats.txokinperiod++;
 112
 113                /* We can not make sure broadcast/multicast or unicast mode. */
 114                if (pstx_fb->pkt_type == PACKET_MULTICAST) {
 115                        priv->stats.txmulticast++;
 116                        priv->stats.txbytesmulticast += pstx_fb->pkt_length;
 117                } else if (pstx_fb->pkt_type == PACKET_BROADCAST) {
 118                        priv->stats.txbroadcast++;
 119                        priv->stats.txbytesbroadcast += pstx_fb->pkt_length;
 120                } else {
 121                        priv->stats.txunicast++;
 122                        priv->stats.txbytesunicast += pstx_fb->pkt_length;
 123                }
 124        } else {
 125                priv->stats.txfeedbackfail++;
 126                priv->stats.txerrtotal++;
 127                priv->stats.txerrbytestotal += pstx_fb->pkt_length;
 128
 129                /* We can not make sure broadcast/multicast or unicast mode. */
 130                if (pstx_fb->pkt_type == PACKET_MULTICAST)
 131                        priv->stats.txerrmulticast++;
 132                else if (pstx_fb->pkt_type == PACKET_BROADCAST)
 133                        priv->stats.txerrbroadcast++;
 134                else
 135                        priv->stats.txerrunicast++;
 136        }
 137
 138        priv->stats.txretrycount += pstx_fb->retry_cnt;
 139        priv->stats.txfeedbackretry += pstx_fb->retry_cnt;
 140}
 141
 142/*-----------------------------------------------------------------------------
 143 * Function:    cmpk_handle_tx_feedback()
 144 *
 145 * Overview:    The function is responsible for extract the message inside TX
 146 *              feedbck message from firmware. It will contain dedicated info in
 147 *              ws-06-0063-rtl8190-command-packet-specification.
 148 *              Please refer to chapter "TX Feedback Element".
 149 *              We have to read 20 bytes in the command packet.
 150 *
 151 * Input:       struct net_device       *dev
 152 *              u8                      *pmsg   - Msg Ptr of the command packet.
 153 *
 154 * Output:      NONE
 155 *
 156 * Return:      NONE
 157 *
 158 * Revised History:
 159 *  When                Who     Remark
 160 *  05/08/2008          amy     Create Version 0 porting from windows code.
 161 *
 162 *---------------------------------------------------------------------------
 163 */
 164static void cmpk_handle_tx_feedback(struct net_device *dev, u8 *pmsg)
 165{
 166        struct r8192_priv *priv = ieee80211_priv(dev);
 167        cmpk_txfb_t             rx_tx_fb;
 168
 169        priv->stats.txfeedback++;
 170
 171        /* 1. Extract TX feedback info from RFD to temp structure buffer. */
 172        /* It seems that FW use big endian(MIPS) and DRV use little endian in
 173         * windows OS. So we have to read the content byte by byte or transfer
 174         * endian type before copy the message copy.
 175         */
 176        /* Use pointer to transfer structure memory. */
 177        memcpy((u8 *)&rx_tx_fb, pmsg, sizeof(cmpk_txfb_t));
 178        /* 2. Use tx feedback info to count TX statistics. */
 179        cmpk_count_txstatistic(dev, &rx_tx_fb);
 180        /* Comment previous method for TX statistic function. */
 181        /* Collect info TX feedback packet to fill TCB. */
 182        /* We can not know the packet length and transmit type: broadcast or uni
 183         * or multicast.
 184         */
 185}
 186
 187static void cmdpkt_beacontimerinterrupt_819xusb(struct net_device *dev)
 188{
 189        struct r8192_priv *priv = ieee80211_priv(dev);
 190        u16 tx_rate;
 191                /* 87B have to S/W beacon for DTM encryption_cmn. */
 192                if (priv->ieee80211->current_network.mode == IEEE_A ||
 193                    priv->ieee80211->current_network.mode == IEEE_N_5G ||
 194                    (priv->ieee80211->current_network.mode == IEEE_N_24G &&
 195                     (!priv->ieee80211->pHTInfo->bCurSuppCCK))) {
 196                        tx_rate = 60;
 197                        DMESG("send beacon frame  tx rate is 6Mbpm\n");
 198                } else {
 199                        tx_rate = 10;
 200                        DMESG("send beacon frame  tx rate is 1Mbpm\n");
 201                }
 202
 203                rtl819xusb_beacon_tx(dev, tx_rate); /* HW Beacon */
 204}
 205
 206/*-----------------------------------------------------------------------------
 207 * Function:    cmpk_handle_interrupt_status()
 208 *
 209 * Overview:    The function is responsible for extract the message from
 210 *              firmware. It will contain dedicated info in
 211 *              ws-07-0063-v06-rtl819x-command-packet-specification-070315.doc.
 212 *              Please refer to chapter "Interrupt Status Element".
 213 *
 214 * Input:       struct net_device *dev
 215 *              u8 *pmsg                - Message Pointer of the command packet.
 216 *
 217 * Output:      NONE
 218 *
 219 * Return:      NONE
 220 *
 221 * Revised History:
 222 *  When                Who     Remark
 223 *  05/12/2008          amy     Add this for rtl8192 porting from windows code.
 224 *
 225 *---------------------------------------------------------------------------
 226 */
 227static void cmpk_handle_interrupt_status(struct net_device *dev, u8 *pmsg)
 228{
 229        cmpk_intr_sta_t         rx_intr_status; /* */
 230        struct r8192_priv *priv = ieee80211_priv(dev);
 231
 232        DMESG("---> cmpk_Handle_Interrupt_Status()\n");
 233
 234        /* 1. Extract TX feedback info from RFD to temp structure buffer. */
 235        /* It seems that FW use big endian(MIPS) and DRV use little endian in
 236         * windows OS. So we have to read the content byte by byte or transfer
 237         * endian type before copy the message copy.
 238         */
 239        rx_intr_status.length = pmsg[1];
 240        if (rx_intr_status.length != (sizeof(cmpk_intr_sta_t) - 2)) {
 241                DMESG("cmpk_Handle_Interrupt_Status: wrong length!\n");
 242                return;
 243        }
 244
 245        /* Statistics of beacon for ad-hoc mode. */
 246        if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
 247                /* 2 maybe need endian transform? */
 248                rx_intr_status.interrupt_status = *((u32 *)(pmsg + 4));
 249
 250                DMESG("interrupt status = 0x%x\n",
 251                      rx_intr_status.interrupt_status);
 252
 253                if (rx_intr_status.interrupt_status & ISR_TxBcnOk) {
 254                        priv->ieee80211->bibsscoordinator = true;
 255                        priv->stats.txbeaconokint++;
 256                } else if (rx_intr_status.interrupt_status & ISR_TxBcnErr) {
 257                        priv->ieee80211->bibsscoordinator = false;
 258                        priv->stats.txbeaconerr++;
 259                }
 260
 261                if (rx_intr_status.interrupt_status & ISR_BcnTimerIntr)
 262                        cmdpkt_beacontimerinterrupt_819xusb(dev);
 263        }
 264
 265        /* Other informations in interrupt status we need? */
 266
 267        DMESG("<---- cmpk_handle_interrupt_status()\n");
 268}
 269
 270/*-----------------------------------------------------------------------------
 271 * Function:    cmpk_handle_query_config_rx()
 272 *
 273 * Overview:    The function is responsible for extract the message from
 274 *              firmware. It will contain dedicated info in
 275 *              ws-06-0063-rtl8190-command-packet-specification. Please
 276 *              refer to chapter "Beacon State Element".
 277 *
 278 * Input:       u8    *pmsg     -       Message Pointer of the command packet.
 279 *
 280 * Output:      NONE
 281 *
 282 * Return:      NONE
 283 *
 284 * Revised History:
 285 *  When                Who     Remark
 286 *  05/12/2008          amy     Create Version 0 porting from windows code.
 287 *
 288 *---------------------------------------------------------------------------
 289 */
 290static void cmpk_handle_query_config_rx(struct net_device *dev, u8 *pmsg)
 291{
 292        cmpk_query_cfg_t        rx_query_cfg;
 293
 294        /* 1. Extract TX feedback info from RFD to temp structure buffer. */
 295        /* It seems that FW use big endian(MIPS) and DRV use little endian in
 296         * windows OS. So we have to read the content byte by byte or transfer
 297         * endian type before copy the message copy.
 298         */
 299        rx_query_cfg.cfg_action         = (pmsg[4] & 0x80000000) >> 31;
 300        rx_query_cfg.cfg_type           = (pmsg[4] & 0x60) >> 5;
 301        rx_query_cfg.cfg_size           = (pmsg[4] & 0x18) >> 3;
 302        rx_query_cfg.cfg_page           = (pmsg[6] & 0x0F) >> 0;
 303        rx_query_cfg.cfg_offset         = pmsg[7];
 304        rx_query_cfg.value              = (pmsg[8]  << 24) | (pmsg[9]  << 16) |
 305                                          (pmsg[10] <<  8) | (pmsg[11] <<  0);
 306        rx_query_cfg.mask               = (pmsg[12] << 24) | (pmsg[13] << 16) |
 307                                          (pmsg[14] <<  8) | (pmsg[15] <<  0);
 308}
 309
 310/*-----------------------------------------------------------------------------
 311 * Function:    cmpk_count_tx_status()
 312 *
 313 * Overview:    Count aggregated tx status from firmwar of one type rx command
 314 *              packet element id = RX_TX_STATUS.
 315 *
 316 * Input:       NONE
 317 *
 318 * Output:      NONE
 319 *
 320 * Return:      NONE
 321 *
 322 * Revised History:
 323 *      When            Who     Remark
 324 *      05/12/2008      amy     Create Version 0 porting from windows code.
 325 *
 326 *---------------------------------------------------------------------------
 327 */
 328static void cmpk_count_tx_status(struct net_device *dev,
 329                                 cmpk_tx_status_t *pstx_status)
 330{
 331        struct r8192_priv *priv = ieee80211_priv(dev);
 332
 333#ifdef ENABLE_PS
 334
 335        RT_RF_POWER_STATE       rtstate;
 336
 337        pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
 338                                          (pu1Byte)(&rtState));
 339
 340        /* When RF is off, we should not count the packet for hw/sw synchronize
 341         * reason, ie. there may be a duration while sw switch is changed and
 342         * hw switch is being changed.
 343         */
 344        if (rtState == eRfOff)
 345                return;
 346#endif
 347
 348        priv->stats.txfeedbackok        += pstx_status->txok;
 349        priv->stats.txoktotal           += pstx_status->txok;
 350
 351        priv->stats.txfeedbackfail      += pstx_status->txfail;
 352        priv->stats.txerrtotal          += pstx_status->txfail;
 353
 354        priv->stats.txretrycount        += pstx_status->txretry;
 355        priv->stats.txfeedbackretry     += pstx_status->txretry;
 356
 357
 358        priv->stats.txmulticast         += pstx_status->txmcok;
 359        priv->stats.txbroadcast         += pstx_status->txbcok;
 360        priv->stats.txunicast           += pstx_status->txucok;
 361
 362        priv->stats.txerrmulticast      += pstx_status->txmcfail;
 363        priv->stats.txerrbroadcast      += pstx_status->txbcfail;
 364        priv->stats.txerrunicast        += pstx_status->txucfail;
 365
 366        priv->stats.txbytesmulticast    += pstx_status->txmclength;
 367        priv->stats.txbytesbroadcast    += pstx_status->txbclength;
 368        priv->stats.txbytesunicast      += pstx_status->txuclength;
 369
 370        priv->stats.last_packet_rate    = pstx_status->rate;
 371}
 372
 373/*-----------------------------------------------------------------------------
 374 * Function:    cmpk_handle_tx_status()
 375 *
 376 * Overview:    Firmware add a new tx feedback status to reduce rx command
 377 *              packet buffer operation load.
 378 *
 379 * Input:               NONE
 380 *
 381 * Output:              NONE
 382 *
 383 * Return:              NONE
 384 *
 385 * Revised History:
 386 *      When            Who     Remark
 387 *      05/12/2008      amy     Create Version 0 porting from windows code.
 388 *
 389 *---------------------------------------------------------------------------
 390 */
 391static void cmpk_handle_tx_status(struct net_device *dev, u8 *pmsg)
 392{
 393        cmpk_tx_status_t        rx_tx_sts;
 394
 395        memcpy((void *)&rx_tx_sts, (void *)pmsg, sizeof(cmpk_tx_status_t));
 396        /* 2. Use tx feedback info to count TX statistics. */
 397        cmpk_count_tx_status(dev, &rx_tx_sts);
 398}
 399
 400/*-----------------------------------------------------------------------------
 401 * Function:    cmpk_handle_tx_rate_history()
 402 *
 403 * Overview:    Firmware add a new tx rate history
 404 *
 405 * Input:               NONE
 406 *
 407 * Output:              NONE
 408 *
 409 * Return:              NONE
 410 *
 411 * Revised History:
 412 *      When            Who     Remark
 413 *      05/12/2008      amy     Create Version 0 porting from windows code.
 414 *
 415 *---------------------------------------------------------------------------
 416 */
 417static void cmpk_handle_tx_rate_history(struct net_device *dev, u8 *pmsg)
 418{
 419        cmpk_tx_rahis_t *ptxrate;
 420        u8              i, j;
 421        u16             length = sizeof(cmpk_tx_rahis_t);
 422        u32             *ptemp;
 423        struct r8192_priv *priv = ieee80211_priv(dev);
 424
 425#ifdef ENABLE_PS
 426        pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
 427                                          (pu1Byte)(&rtState));
 428
 429        /* When RF is off, we should not count the packet for hw/sw synchronize
 430         * reason, ie. there may be a duration while sw switch is changed and
 431         * hw switch is being changed.
 432         */
 433        if (rtState == eRfOff)
 434                return;
 435#endif
 436
 437        ptemp = (u32 *)pmsg;
 438
 439        /* Do endian transfer to word alignment(16 bits) for windows system.
 440         * You must do different endian transfer for linux and MAC OS
 441         */
 442        for (i = 0; i < (length/4); i++) {
 443                u16      temp1, temp2;
 444
 445                temp1 = ptemp[i] & 0x0000FFFF;
 446                temp2 = ptemp[i] >> 16;
 447                ptemp[i] = (temp1 << 16) | temp2;
 448        }
 449
 450        ptxrate = (cmpk_tx_rahis_t *)pmsg;
 451
 452        if (ptxrate == NULL)
 453                return;
 454
 455        for (i = 0; i < 16; i++) {
 456                /* Collect CCK rate packet num */
 457                if (i < 4)
 458                        priv->stats.txrate.cck[i] += ptxrate->cck[i];
 459
 460                /* Collect OFDM rate packet num */
 461                if (i < 8)
 462                        priv->stats.txrate.ofdm[i] += ptxrate->ofdm[i];
 463
 464                for (j = 0; j < 4; j++)
 465                        priv->stats.txrate.ht_mcs[j][i] += ptxrate->ht_mcs[j][i];
 466        }
 467}
 468
 469/*-----------------------------------------------------------------------------
 470 * Function:    cmpk_message_handle_rx()
 471 *
 472 * Overview:    In the function, we will capture different RX command packet
 473 *              info. Every RX command packet element has different message
 474 *              length and meaning in content. We only support three type of RX
 475 *              command packet now. Please refer to document
 476 *              ws-06-0063-rtl8190-command-packet-specification.
 477 *
 478 * Input:       NONE
 479 *
 480 * Output:      NONE
 481 *
 482 * Return:      NONE
 483 *
 484 * Revised History:
 485 *  When                Who     Remark
 486 *  05/06/2008          amy     Create Version 0 porting from windows code.
 487 *
 488 *---------------------------------------------------------------------------
 489 */
 490u32 cmpk_message_handle_rx(struct net_device *dev,
 491                           struct ieee80211_rx_stats *pstats)
 492{
 493        int                     total_length;
 494        u8                      cmd_length, exe_cnt = 0;
 495        u8                      element_id;
 496        u8                      *pcmd_buff;
 497
 498        /* 0. Check inpt arguments. If is is a command queue message or
 499         * pointer is null.
 500         */
 501        if (pstats == NULL)
 502                return 0;       /* This is not a command packet. */
 503
 504        /* 1. Read received command packet message length from RFD. */
 505        total_length = pstats->Length;
 506
 507        /* 2. Read virtual address from RFD. */
 508        pcmd_buff = pstats->virtual_address;
 509
 510        /* 3. Read command packet element id and length. */
 511        element_id = pcmd_buff[0];
 512
 513        /* 4. Check every received command packet content according to different
 514         *    element type. Because FW may aggregate RX command packet to
 515         *    minimize transmit time between DRV and FW.
 516         */
 517        /* Add a counter to prevent the lock in the loop from being held too
 518         * long
 519         */
 520        while (total_length > 0 && exe_cnt++ < 100) {
 521                /* We support aggregation of different cmd in the same packet */
 522                element_id = pcmd_buff[0];
 523
 524                switch (element_id) {
 525                case RX_TX_FEEDBACK:
 526                        cmpk_handle_tx_feedback(dev, pcmd_buff);
 527                        cmd_length = CMPK_RX_TX_FB_SIZE;
 528                        break;
 529
 530                case RX_INTERRUPT_STATUS:
 531                        cmpk_handle_interrupt_status(dev, pcmd_buff);
 532                        cmd_length = sizeof(cmpk_intr_sta_t);
 533                        break;
 534
 535                case BOTH_QUERY_CONFIG:
 536                        cmpk_handle_query_config_rx(dev, pcmd_buff);
 537                        cmd_length = CMPK_BOTH_QUERY_CONFIG_SIZE;
 538                        break;
 539
 540                case RX_TX_STATUS:
 541                        cmpk_handle_tx_status(dev, pcmd_buff);
 542                        cmd_length = CMPK_RX_TX_STS_SIZE;
 543                        break;
 544
 545                case RX_TX_PER_PKT_FEEDBACK:
 546                        /* You must at lease add a switch case element here,
 547                         * Otherwise, we will jump to default case.
 548                         */
 549                        cmd_length = CMPK_RX_TX_FB_SIZE;
 550                        break;
 551
 552                case RX_TX_RATE_HISTORY:
 553                        cmpk_handle_tx_rate_history(dev, pcmd_buff);
 554                        cmd_length = CMPK_TX_RAHIS_SIZE;
 555                        break;
 556
 557                default:
 558
 559                        RT_TRACE(COMP_ERR, "---->%s():unknown CMD Element\n",
 560                                 __func__);
 561                        return 1;       /* This is a command packet. */
 562                }
 563
 564                total_length -= cmd_length;
 565                pcmd_buff    += cmd_length;
 566        }
 567        return  1;      /* This is a command packet. */
 568}
 569