linux/drivers/net/wireless/rtlwifi/usb.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2009-2011  Realtek Corporation. All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc.,
  16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called LICENSE.
  20 *
  21 * Contact Information:
  22 * wlanfae <wlanfae@realtek.com>
  23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24 * Hsinchu 300, Taiwan.
  25 *
  26 *****************************************************************************/
  27
  28#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29
  30#include <linux/usb.h>
  31#include <linux/export.h>
  32#include "core.h"
  33#include "wifi.h"
  34#include "usb.h"
  35#include "base.h"
  36#include "ps.h"
  37
  38#define REALTEK_USB_VENQT_READ                  0xC0
  39#define REALTEK_USB_VENQT_WRITE                 0x40
  40#define REALTEK_USB_VENQT_CMD_REQ               0x05
  41#define REALTEK_USB_VENQT_CMD_IDX               0x00
  42
  43#define REALTEK_USB_VENQT_MAX_BUF_SIZE          254
  44
  45static void usbctrl_async_callback(struct urb *urb)
  46{
  47        if (urb)
  48                kfree(urb->context);
  49}
  50
  51static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
  52                                          u16 value, u16 index, void *pdata,
  53                                          u16 len)
  54{
  55        int rc;
  56        unsigned int pipe;
  57        u8 reqtype;
  58        struct usb_ctrlrequest *dr;
  59        struct urb *urb;
  60        struct rtl819x_async_write_data {
  61                u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
  62                struct usb_ctrlrequest dr;
  63        } *buf;
  64
  65        pipe = usb_sndctrlpipe(udev, 0); /* write_out */
  66        reqtype =  REALTEK_USB_VENQT_WRITE;
  67
  68        buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  69        if (!buf)
  70                return -ENOMEM;
  71
  72        urb = usb_alloc_urb(0, GFP_ATOMIC);
  73        if (!urb) {
  74                kfree(buf);
  75                return -ENOMEM;
  76        }
  77
  78        dr = &buf->dr;
  79
  80        dr->bRequestType = reqtype;
  81        dr->bRequest = request;
  82        dr->wValue = cpu_to_le16(value);
  83        dr->wIndex = cpu_to_le16(index);
  84        dr->wLength = cpu_to_le16(len);
  85        memcpy(buf, pdata, len);
  86        usb_fill_control_urb(urb, udev, pipe,
  87                             (unsigned char *)dr, buf, len,
  88                             usbctrl_async_callback, buf);
  89        rc = usb_submit_urb(urb, GFP_ATOMIC);
  90        if (rc < 0)
  91                kfree(buf);
  92        usb_free_urb(urb);
  93        return rc;
  94}
  95
  96static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
  97                                        u16 value, u16 index, void *pdata,
  98                                        u16 len)
  99{
 100        unsigned int pipe;
 101        int status;
 102        u8 reqtype;
 103
 104        pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
 105        reqtype =  REALTEK_USB_VENQT_READ;
 106
 107        status = usb_control_msg(udev, pipe, request, reqtype, value, index,
 108                                 pdata, len, 0); /* max. timeout */
 109
 110        if (status < 0)
 111                pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
 112                       value, status, *(u32 *)pdata);
 113        return status;
 114}
 115
 116static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
 117{
 118        u8 request;
 119        u16 wvalue;
 120        u16 index;
 121        u32 *data;
 122        u32 ret;
 123
 124        data = kmalloc(sizeof(u32), GFP_KERNEL);
 125        if (!data)
 126                return -ENOMEM;
 127        request = REALTEK_USB_VENQT_CMD_REQ;
 128        index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
 129
 130        wvalue = (u16)addr;
 131        _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
 132        ret = *data;
 133        kfree(data);
 134        return ret;
 135}
 136
 137static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
 138{
 139        struct device *dev = rtlpriv->io.dev;
 140
 141        return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
 142}
 143
 144static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
 145{
 146        struct device *dev = rtlpriv->io.dev;
 147
 148        return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
 149}
 150
 151static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
 152{
 153        struct device *dev = rtlpriv->io.dev;
 154
 155        return _usb_read_sync(to_usb_device(dev), addr, 4);
 156}
 157
 158static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
 159                             u16 len)
 160{
 161        u8 request;
 162        u16 wvalue;
 163        u16 index;
 164        u32 data;
 165
 166        request = REALTEK_USB_VENQT_CMD_REQ;
 167        index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
 168        wvalue = (u16)(addr&0x0000ffff);
 169        data = val;
 170        _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
 171                                       len);
 172}
 173
 174static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
 175{
 176        struct device *dev = rtlpriv->io.dev;
 177
 178        _usb_write_async(to_usb_device(dev), addr, val, 1);
 179}
 180
 181static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
 182{
 183        struct device *dev = rtlpriv->io.dev;
 184
 185        _usb_write_async(to_usb_device(dev), addr, val, 2);
 186}
 187
 188static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
 189{
 190        struct device *dev = rtlpriv->io.dev;
 191
 192        _usb_write_async(to_usb_device(dev), addr, val, 4);
 193}
 194
 195static void _rtl_usb_io_handler_init(struct device *dev,
 196                                     struct ieee80211_hw *hw)
 197{
 198        struct rtl_priv *rtlpriv = rtl_priv(hw);
 199
 200        rtlpriv->io.dev = dev;
 201        mutex_init(&rtlpriv->io.bb_mutex);
 202        rtlpriv->io.write8_async        = _usb_write8_async;
 203        rtlpriv->io.write16_async       = _usb_write16_async;
 204        rtlpriv->io.write32_async       = _usb_write32_async;
 205        rtlpriv->io.read8_sync          = _usb_read8_sync;
 206        rtlpriv->io.read16_sync         = _usb_read16_sync;
 207        rtlpriv->io.read32_sync         = _usb_read32_sync;
 208}
 209
 210static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
 211{
 212        struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
 213
 214        mutex_destroy(&rtlpriv->io.bb_mutex);
 215}
 216
 217/**
 218 *
 219 *      Default aggregation handler. Do nothing and just return the oldest skb.
 220 */
 221static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
 222                                                  struct sk_buff_head *list)
 223{
 224        return skb_dequeue(list);
 225}
 226
 227#define IS_HIGH_SPEED_USB(udev) \
 228                ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
 229
 230static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
 231{
 232        u32 i;
 233        struct rtl_priv *rtlpriv = rtl_priv(hw);
 234        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 235
 236        rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
 237                                                    ? USB_HIGH_SPEED_BULK_SIZE
 238                                                    : USB_FULL_SPEED_BULK_SIZE;
 239
 240        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
 241                 rtlusb->max_bulk_out_size));
 242
 243        for (i = 0; i < __RTL_TXQ_NUM; i++) {
 244                u32 ep_num = rtlusb->ep_map.ep_mapping[i];
 245                if (!ep_num) {
 246                        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
 247                                 ("Invalid endpoint map setting!\n"));
 248                        return -EINVAL;
 249                }
 250        }
 251
 252        rtlusb->usb_tx_post_hdl =
 253                 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
 254        rtlusb->usb_tx_cleanup  =
 255                 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
 256        rtlusb->usb_tx_aggregate_hdl =
 257                 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
 258                 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
 259                 : &_none_usb_tx_aggregate_hdl;
 260
 261        init_usb_anchor(&rtlusb->tx_submitted);
 262        for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
 263                skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
 264                init_usb_anchor(&rtlusb->tx_pending[i]);
 265        }
 266        return 0;
 267}
 268
 269static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
 270{
 271        struct rtl_priv *rtlpriv = rtl_priv(hw);
 272        struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
 273        struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
 274
 275        rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
 276        rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
 277        rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
 278        rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
 279        rtlusb->usb_rx_segregate_hdl =
 280                rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
 281
 282        pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
 283                rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
 284        init_usb_anchor(&rtlusb->rx_submitted);
 285        return 0;
 286}
 287
 288static int _rtl_usb_init(struct ieee80211_hw *hw)
 289{
 290        struct rtl_priv *rtlpriv = rtl_priv(hw);
 291        struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
 292        struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
 293        int err;
 294        u8 epidx;
 295        struct usb_interface    *usb_intf = rtlusb->intf;
 296        u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
 297
 298        rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
 299        for (epidx = 0; epidx < epnums; epidx++) {
 300                struct usb_endpoint_descriptor *pep_desc;
 301                pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
 302
 303                if (usb_endpoint_dir_in(pep_desc))
 304                        rtlusb->in_ep_nums++;
 305                else if (usb_endpoint_dir_out(pep_desc))
 306                        rtlusb->out_ep_nums++;
 307
 308                RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
 309                         ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
 310                         pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
 311                         pep_desc->bInterval));
 312        }
 313        if (rtlusb->in_ep_nums <  rtlpriv->cfg->usb_interface_cfg->in_ep_num)
 314                return -EINVAL ;
 315
 316        /* usb endpoint mapping */
 317        err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
 318        rtlusb->usb_mq_to_hwq =  rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
 319        _rtl_usb_init_tx(hw);
 320        _rtl_usb_init_rx(hw);
 321        return err;
 322}
 323
 324static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
 325{
 326        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 327        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 328        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 329        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 330
 331        rtlhal->hw = hw;
 332        ppsc->inactiveps = false;
 333        ppsc->leisure_ps = false;
 334        ppsc->fwctrl_lps = false;
 335        ppsc->reg_fwctrl_lps = 3;
 336        ppsc->reg_max_lps_awakeintvl = 5;
 337        ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
 338
 339         /* IBSS */
 340        mac->beacon_interval = 100;
 341
 342         /* AMPDU */
 343        mac->min_space_cfg = 0;
 344        mac->max_mss_density = 0;
 345
 346        /* set sane AMPDU defaults */
 347        mac->current_ampdu_density = 7;
 348        mac->current_ampdu_factor = 3;
 349
 350        /* QOS */
 351        rtlusb->acm_method = eAcmWay2_SW;
 352
 353        /* IRQ */
 354        /* HIMR - turn all on */
 355        rtlusb->irq_mask[0] = 0xFFFFFFFF;
 356        /* HIMR_EX - turn all on */
 357        rtlusb->irq_mask[1] = 0xFFFFFFFF;
 358        rtlusb->disableHWSM =  true;
 359        return 0;
 360}
 361
 362#define __RADIO_TAP_SIZE_RSV    32
 363
 364static void _rtl_rx_completed(struct urb *urb);
 365
 366static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
 367                                        struct rtl_usb *rtlusb,
 368                                        struct urb *urb,
 369                                        gfp_t gfp_mask)
 370{
 371        struct sk_buff *skb;
 372        struct rtl_priv *rtlpriv = rtl_priv(hw);
 373
 374        skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
 375                               gfp_mask);
 376        if (!skb) {
 377                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 378                         ("Failed to __dev_alloc_skb!!\n"))
 379                return ERR_PTR(-ENOMEM);
 380        }
 381
 382        /* reserve some space for mac80211's radiotap */
 383        skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
 384        usb_fill_bulk_urb(urb, rtlusb->udev,
 385                          usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
 386                          skb->data, min(skb_tailroom(skb),
 387                          (int)rtlusb->rx_max_size),
 388                          _rtl_rx_completed, skb);
 389
 390        _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
 391        return skb;
 392}
 393
 394#undef __RADIO_TAP_SIZE_RSV
 395
 396static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
 397                                    struct sk_buff *skb)
 398{
 399        struct rtl_priv *rtlpriv = rtl_priv(hw);
 400        u8 *rxdesc = skb->data;
 401        struct ieee80211_hdr *hdr;
 402        bool unicast = false;
 403        __le16 fc;
 404        struct ieee80211_rx_status rx_status = {0};
 405        struct rtl_stats stats = {
 406                .signal = 0,
 407                .noise = -98,
 408                .rate = 0,
 409        };
 410
 411        skb_pull(skb, RTL_RX_DESC_SIZE);
 412        rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
 413        skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
 414        hdr = (struct ieee80211_hdr *)(skb->data);
 415        fc = hdr->frame_control;
 416        if (!stats.crc) {
 417                memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
 418
 419                if (is_broadcast_ether_addr(hdr->addr1)) {
 420                        /*TODO*/;
 421                } else if (is_multicast_ether_addr(hdr->addr1)) {
 422                        /*TODO*/
 423                } else {
 424                        unicast = true;
 425                        rtlpriv->stats.rxbytesunicast +=  skb->len;
 426                }
 427
 428                rtl_is_special_data(hw, skb, false);
 429
 430                if (ieee80211_is_data(fc)) {
 431                        rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
 432
 433                        if (unicast)
 434                                rtlpriv->link_info.num_rx_inperiod++;
 435                }
 436        }
 437}
 438
 439static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
 440                                      struct sk_buff *skb)
 441{
 442        struct rtl_priv *rtlpriv = rtl_priv(hw);
 443        u8 *rxdesc = skb->data;
 444        struct ieee80211_hdr *hdr;
 445        bool unicast = false;
 446        __le16 fc;
 447        struct ieee80211_rx_status rx_status = {0};
 448        struct rtl_stats stats = {
 449                .signal = 0,
 450                .noise = -98,
 451                .rate = 0,
 452        };
 453
 454        skb_pull(skb, RTL_RX_DESC_SIZE);
 455        rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
 456        skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
 457        hdr = (struct ieee80211_hdr *)(skb->data);
 458        fc = hdr->frame_control;
 459        if (!stats.crc) {
 460                memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
 461
 462                if (is_broadcast_ether_addr(hdr->addr1)) {
 463                        /*TODO*/;
 464                } else if (is_multicast_ether_addr(hdr->addr1)) {
 465                        /*TODO*/
 466                } else {
 467                        unicast = true;
 468                        rtlpriv->stats.rxbytesunicast +=  skb->len;
 469                }
 470
 471                rtl_is_special_data(hw, skb, false);
 472
 473                if (ieee80211_is_data(fc)) {
 474                        rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
 475
 476                        if (unicast)
 477                                rtlpriv->link_info.num_rx_inperiod++;
 478                }
 479                if (likely(rtl_action_proc(hw, skb, false))) {
 480                        struct sk_buff *uskb = NULL;
 481                        u8 *pdata;
 482
 483                        uskb = dev_alloc_skb(skb->len + 128);
 484                        memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
 485                               sizeof(rx_status));
 486                        pdata = (u8 *)skb_put(uskb, skb->len);
 487                        memcpy(pdata, skb->data, skb->len);
 488                        dev_kfree_skb_any(skb);
 489                        ieee80211_rx_irqsafe(hw, uskb);
 490                } else {
 491                        dev_kfree_skb_any(skb);
 492                }
 493        }
 494}
 495
 496static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
 497{
 498        struct sk_buff *_skb;
 499        struct sk_buff_head rx_queue;
 500        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 501
 502        skb_queue_head_init(&rx_queue);
 503        if (rtlusb->usb_rx_segregate_hdl)
 504                rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
 505        WARN_ON(skb_queue_empty(&rx_queue));
 506        while (!skb_queue_empty(&rx_queue)) {
 507                _skb = skb_dequeue(&rx_queue);
 508                _rtl_usb_rx_process_agg(hw, skb);
 509                ieee80211_rx_irqsafe(hw, skb);
 510        }
 511}
 512
 513static void _rtl_rx_completed(struct urb *_urb)
 514{
 515        struct sk_buff *skb = (struct sk_buff *)_urb->context;
 516        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 517        struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
 518        struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
 519        struct rtl_priv *rtlpriv = rtl_priv(hw);
 520        int err = 0;
 521
 522        if (unlikely(IS_USB_STOP(rtlusb)))
 523                goto free;
 524
 525        if (likely(0 == _urb->status)) {
 526                /* If this code were moved to work queue, would CPU
 527                 * utilization be improved?  NOTE: We shall allocate another skb
 528                 * and reuse the original one.
 529                 */
 530                skb_put(skb, _urb->actual_length);
 531
 532                if (likely(!rtlusb->usb_rx_segregate_hdl)) {
 533                        struct sk_buff *_skb;
 534                        _rtl_usb_rx_process_noagg(hw, skb);
 535                        _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
 536                        if (IS_ERR(_skb)) {
 537                                err = PTR_ERR(_skb);
 538                                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 539                                        ("Can't allocate skb for bulk IN!\n"));
 540                                return;
 541                        }
 542                        skb = _skb;
 543                } else{
 544                        /* TO DO */
 545                        _rtl_rx_pre_process(hw, skb);
 546                        pr_err("rx agg not supported\n");
 547                }
 548                goto resubmit;
 549        }
 550
 551        switch (_urb->status) {
 552        /* disconnect */
 553        case -ENOENT:
 554        case -ECONNRESET:
 555        case -ENODEV:
 556        case -ESHUTDOWN:
 557                goto free;
 558        default:
 559                break;
 560        }
 561
 562resubmit:
 563        skb_reset_tail_pointer(skb);
 564        skb_trim(skb, 0);
 565
 566        usb_anchor_urb(_urb, &rtlusb->rx_submitted);
 567        err = usb_submit_urb(_urb, GFP_ATOMIC);
 568        if (unlikely(err)) {
 569                usb_unanchor_urb(_urb);
 570                goto free;
 571        }
 572        return;
 573
 574free:
 575        dev_kfree_skb_irq(skb);
 576}
 577
 578static int _rtl_usb_receive(struct ieee80211_hw *hw)
 579{
 580        struct urb *urb;
 581        struct sk_buff *skb;
 582        int err;
 583        int i;
 584        struct rtl_priv *rtlpriv = rtl_priv(hw);
 585        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 586
 587        WARN_ON(0 == rtlusb->rx_urb_num);
 588        /* 1600 == 1514 + max WLAN header + rtk info */
 589        WARN_ON(rtlusb->rx_max_size < 1600);
 590
 591        for (i = 0; i < rtlusb->rx_urb_num; i++) {
 592                err = -ENOMEM;
 593                urb = usb_alloc_urb(0, GFP_KERNEL);
 594                if (!urb) {
 595                        RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 596                                 ("Failed to alloc URB!!\n"))
 597                        goto err_out;
 598                }
 599
 600                skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
 601                if (IS_ERR(skb)) {
 602                        RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 603                                 ("Failed to prep_rx_urb!!\n"))
 604                        err = PTR_ERR(skb);
 605                        goto err_out;
 606                }
 607
 608                usb_anchor_urb(urb, &rtlusb->rx_submitted);
 609                err = usb_submit_urb(urb, GFP_KERNEL);
 610                if (err)
 611                        goto err_out;
 612                usb_free_urb(urb);
 613        }
 614        return 0;
 615
 616err_out:
 617        usb_kill_anchored_urbs(&rtlusb->rx_submitted);
 618        return err;
 619}
 620
 621static int rtl_usb_start(struct ieee80211_hw *hw)
 622{
 623        int err;
 624        struct rtl_priv *rtlpriv = rtl_priv(hw);
 625        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 626        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 627
 628        err = rtlpriv->cfg->ops->hw_init(hw);
 629        rtl_init_rx_config(hw);
 630
 631        /* Enable software */
 632        SET_USB_START(rtlusb);
 633        /* should after adapter start and interrupt enable. */
 634        set_hal_start(rtlhal);
 635
 636        /* Start bulk IN */
 637        _rtl_usb_receive(hw);
 638
 639        return err;
 640}
 641/**
 642 *
 643 *
 644 */
 645
 646/*=======================  tx =========================================*/
 647static void rtl_usb_cleanup(struct ieee80211_hw *hw)
 648{
 649        u32 i;
 650        struct sk_buff *_skb;
 651        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 652        struct ieee80211_tx_info *txinfo;
 653
 654        SET_USB_STOP(rtlusb);
 655
 656        /* clean up rx stuff. */
 657        usb_kill_anchored_urbs(&rtlusb->rx_submitted);
 658
 659        /* clean up tx stuff */
 660        for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
 661                while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
 662                        rtlusb->usb_tx_cleanup(hw, _skb);
 663                        txinfo = IEEE80211_SKB_CB(_skb);
 664                        ieee80211_tx_info_clear_status(txinfo);
 665                        txinfo->flags |= IEEE80211_TX_STAT_ACK;
 666                        ieee80211_tx_status_irqsafe(hw, _skb);
 667                }
 668                usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
 669        }
 670        usb_kill_anchored_urbs(&rtlusb->tx_submitted);
 671}
 672
 673/**
 674 *
 675 * We may add some struct into struct rtl_usb later. Do deinit here.
 676 *
 677 */
 678static void rtl_usb_deinit(struct ieee80211_hw *hw)
 679{
 680        rtl_usb_cleanup(hw);
 681}
 682
 683static void rtl_usb_stop(struct ieee80211_hw *hw)
 684{
 685        struct rtl_priv *rtlpriv = rtl_priv(hw);
 686        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 687        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 688
 689        /* should after adapter start and interrupt enable. */
 690        set_hal_stop(rtlhal);
 691        /* Enable software */
 692        SET_USB_STOP(rtlusb);
 693        rtl_usb_deinit(hw);
 694        rtlpriv->cfg->ops->hw_disable(hw);
 695}
 696
 697static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
 698{
 699        int err;
 700        struct rtl_priv *rtlpriv = rtl_priv(hw);
 701        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 702
 703        usb_anchor_urb(_urb, &rtlusb->tx_submitted);
 704        err = usb_submit_urb(_urb, GFP_ATOMIC);
 705        if (err < 0) {
 706                struct sk_buff *skb;
 707
 708                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 709                         ("Failed to submit urb.\n"));
 710                usb_unanchor_urb(_urb);
 711                skb = (struct sk_buff *)_urb->context;
 712                kfree_skb(skb);
 713        }
 714        usb_free_urb(_urb);
 715}
 716
 717static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
 718                        struct sk_buff *skb)
 719{
 720        struct rtl_priv *rtlpriv = rtl_priv(hw);
 721        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 722        struct ieee80211_tx_info *txinfo;
 723
 724        rtlusb->usb_tx_post_hdl(hw, urb, skb);
 725        skb_pull(skb, RTL_TX_HEADER_SIZE);
 726        txinfo = IEEE80211_SKB_CB(skb);
 727        ieee80211_tx_info_clear_status(txinfo);
 728        txinfo->flags |= IEEE80211_TX_STAT_ACK;
 729
 730        if (urb->status) {
 731                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 732                         ("Urb has error status 0x%X\n", urb->status));
 733                goto out;
 734        }
 735        /*  TODO:       statistics */
 736out:
 737        ieee80211_tx_status_irqsafe(hw, skb);
 738        return urb->status;
 739}
 740
 741static void _rtl_tx_complete(struct urb *urb)
 742{
 743        struct sk_buff *skb = (struct sk_buff *)urb->context;
 744        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 745        struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
 746        struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
 747        int err;
 748
 749        if (unlikely(IS_USB_STOP(rtlusb)))
 750                return;
 751        err = _usb_tx_post(hw, urb, skb);
 752        if (err) {
 753                /* Ignore error and keep issuiing other urbs */
 754                return;
 755        }
 756}
 757
 758static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
 759                                struct sk_buff *skb, u32 ep_num)
 760{
 761        struct rtl_priv *rtlpriv = rtl_priv(hw);
 762        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 763        struct urb *_urb;
 764
 765        WARN_ON(NULL == skb);
 766        _urb = usb_alloc_urb(0, GFP_ATOMIC);
 767        if (!_urb) {
 768                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 769                         ("Can't allocate URB for bulk out!\n"));
 770                kfree_skb(skb);
 771                return NULL;
 772        }
 773        _rtl_install_trx_info(rtlusb, skb, ep_num);
 774        usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
 775                          ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
 776        _urb->transfer_flags |= URB_ZERO_PACKET;
 777        return _urb;
 778}
 779
 780static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
 781                       enum rtl_txq qnum)
 782{
 783        struct rtl_priv *rtlpriv = rtl_priv(hw);
 784        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 785        u32 ep_num;
 786        struct urb *_urb = NULL;
 787        struct sk_buff *_skb = NULL;
 788        struct sk_buff_head *skb_list;
 789        struct usb_anchor *urb_list;
 790
 791        WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
 792        if (unlikely(IS_USB_STOP(rtlusb))) {
 793                RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
 794                         ("USB device is stopping...\n"));
 795                kfree_skb(skb);
 796                return;
 797        }
 798        ep_num = rtlusb->ep_map.ep_mapping[qnum];
 799        skb_list = &rtlusb->tx_skb_queue[ep_num];
 800        _skb = skb;
 801        _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
 802        if (unlikely(!_urb)) {
 803                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
 804                         ("Can't allocate urb. Drop skb!\n"));
 805                return;
 806        }
 807        urb_list = &rtlusb->tx_pending[ep_num];
 808        _rtl_submit_tx_urb(hw, _urb);
 809}
 810
 811static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
 812                            u16 hw_queue)
 813{
 814        struct rtl_priv *rtlpriv = rtl_priv(hw);
 815        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 816        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 817        struct rtl_tx_desc *pdesc = NULL;
 818        struct rtl_tcb_desc tcb_desc;
 819        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
 820        __le16 fc = hdr->frame_control;
 821        u8 *pda_addr = hdr->addr1;
 822        /* ssn */
 823        u8 *qc = NULL;
 824        u8 tid = 0;
 825        u16 seq_number = 0;
 826
 827        memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
 828        if (ieee80211_is_auth(fc)) {
 829                RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
 830                rtl_ips_nic_on(hw);
 831        }
 832
 833        if (rtlpriv->psc.sw_ps_enabled) {
 834                if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
 835                    !ieee80211_has_pm(fc))
 836                        hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
 837        }
 838
 839        rtl_action_proc(hw, skb, true);
 840        if (is_multicast_ether_addr(pda_addr))
 841                rtlpriv->stats.txbytesmulticast += skb->len;
 842        else if (is_broadcast_ether_addr(pda_addr))
 843                rtlpriv->stats.txbytesbroadcast += skb->len;
 844        else
 845                rtlpriv->stats.txbytesunicast += skb->len;
 846        if (ieee80211_is_data_qos(fc)) {
 847                qc = ieee80211_get_qos_ctl(hdr);
 848                tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
 849                seq_number = (le16_to_cpu(hdr->seq_ctrl) &
 850                             IEEE80211_SCTL_SEQ) >> 4;
 851                seq_number += 1;
 852                seq_number <<= 4;
 853        }
 854        rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
 855                                        hw_queue, &tcb_desc);
 856        if (!ieee80211_has_morefrags(hdr->frame_control)) {
 857                if (qc)
 858                        mac->tids[tid].seq_number = seq_number;
 859        }
 860        if (ieee80211_is_data(fc))
 861                rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
 862}
 863
 864static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
 865                      struct rtl_tcb_desc *dummy)
 866{
 867        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 868        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 869        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
 870        __le16 fc = hdr->frame_control;
 871        u16 hw_queue;
 872
 873        if (unlikely(is_hal_stop(rtlhal)))
 874                goto err_free;
 875        hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
 876        _rtl_usb_tx_preprocess(hw, skb, hw_queue);
 877        _rtl_usb_transmit(hw, skb, hw_queue);
 878        return NETDEV_TX_OK;
 879
 880err_free:
 881        dev_kfree_skb_any(skb);
 882        return NETDEV_TX_OK;
 883}
 884
 885static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
 886                                        struct sk_buff *skb)
 887{
 888        return false;
 889}
 890
 891static struct rtl_intf_ops rtl_usb_ops = {
 892        .adapter_start = rtl_usb_start,
 893        .adapter_stop = rtl_usb_stop,
 894        .adapter_tx = rtl_usb_tx,
 895        .waitq_insert = rtl_usb_tx_chk_waitq_insert,
 896};
 897
 898int __devinit rtl_usb_probe(struct usb_interface *intf,
 899                        const struct usb_device_id *id)
 900{
 901        int err;
 902        struct ieee80211_hw *hw = NULL;
 903        struct rtl_priv *rtlpriv = NULL;
 904        struct usb_device       *udev;
 905        struct rtl_usb_priv *usb_priv;
 906
 907        hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
 908                                sizeof(struct rtl_usb_priv), &rtl_ops);
 909        if (!hw) {
 910                RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
 911                return -ENOMEM;
 912        }
 913        rtlpriv = hw->priv;
 914        SET_IEEE80211_DEV(hw, &intf->dev);
 915        udev = interface_to_usbdev(intf);
 916        usb_get_dev(udev);
 917        usb_priv = rtl_usbpriv(hw);
 918        memset(usb_priv, 0, sizeof(*usb_priv));
 919        usb_priv->dev.intf = intf;
 920        usb_priv->dev.udev = udev;
 921        usb_set_intfdata(intf, hw);
 922        /* init cfg & intf_ops */
 923        rtlpriv->rtlhal.interface = INTF_USB;
 924        rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
 925        rtlpriv->intf_ops = &rtl_usb_ops;
 926        rtl_dbgp_flag_init(hw);
 927        /* Init IO handler */
 928        _rtl_usb_io_handler_init(&udev->dev, hw);
 929        rtlpriv->cfg->ops->read_chip_version(hw);
 930        /*like read eeprom and so on */
 931        rtlpriv->cfg->ops->read_eeprom_info(hw);
 932        if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
 933                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
 934                         ("Can't init_sw_vars.\n"));
 935                goto error_out;
 936        }
 937        rtlpriv->cfg->ops->init_sw_leds(hw);
 938        err = _rtl_usb_init(hw);
 939        err = _rtl_usb_init_sw(hw);
 940        /* Init mac80211 sw */
 941        err = rtl_init_core(hw);
 942        if (err) {
 943                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
 944                         ("Can't allocate sw for mac80211.\n"));
 945                goto error_out;
 946        }
 947
 948        /*init rfkill */
 949        /* rtl_init_rfkill(hw); */
 950
 951        err = ieee80211_register_hw(hw);
 952        if (err) {
 953                RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
 954                         ("Can't register mac80211 hw.\n"));
 955                goto error_out;
 956        } else {
 957                rtlpriv->mac80211.mac80211_registered = 1;
 958        }
 959        set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
 960        return 0;
 961error_out:
 962        rtl_deinit_core(hw);
 963        _rtl_usb_io_handler_release(hw);
 964        ieee80211_free_hw(hw);
 965        usb_put_dev(udev);
 966        return -ENODEV;
 967}
 968EXPORT_SYMBOL(rtl_usb_probe);
 969
 970void rtl_usb_disconnect(struct usb_interface *intf)
 971{
 972        struct ieee80211_hw *hw = usb_get_intfdata(intf);
 973        struct rtl_priv *rtlpriv = rtl_priv(hw);
 974        struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
 975        struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
 976
 977        if (unlikely(!rtlpriv))
 978                return;
 979        /*ieee80211_unregister_hw will call ops_stop */
 980        if (rtlmac->mac80211_registered == 1) {
 981                ieee80211_unregister_hw(hw);
 982                rtlmac->mac80211_registered = 0;
 983        } else {
 984                rtl_deinit_deferred_work(hw);
 985                rtlpriv->intf_ops->adapter_stop(hw);
 986        }
 987        /*deinit rfkill */
 988        /* rtl_deinit_rfkill(hw); */
 989        rtl_usb_deinit(hw);
 990        rtl_deinit_core(hw);
 991        rtlpriv->cfg->ops->deinit_sw_leds(hw);
 992        rtlpriv->cfg->ops->deinit_sw_vars(hw);
 993        _rtl_usb_io_handler_release(hw);
 994        usb_put_dev(rtlusb->udev);
 995        usb_set_intfdata(intf, NULL);
 996        ieee80211_free_hw(hw);
 997}
 998EXPORT_SYMBOL(rtl_usb_disconnect);
 999
1000int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1001{
1002        return 0;
1003}
1004EXPORT_SYMBOL(rtl_usb_suspend);
1005
1006int rtl_usb_resume(struct usb_interface *pusb_intf)
1007{
1008        return 0;
1009}
1010EXPORT_SYMBOL(rtl_usb_resume);
1011