0001
0002
0003
0004 #include "wifi.h"
0005 #include "core.h"
0006 #include "usb.h"
0007 #include "base.h"
0008 #include "ps.h"
0009 #include "rtl8192c/fw_common.h"
0010 #include <linux/export.h>
0011 #include <linux/module.h>
0012
0013 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
0014 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
0015 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
0016 MODULE_LICENSE("GPL");
0017 MODULE_DESCRIPTION("USB basic driver for rtlwifi");
0018
0019 #define REALTEK_USB_VENQT_READ 0xC0
0020 #define REALTEK_USB_VENQT_WRITE 0x40
0021 #define REALTEK_USB_VENQT_CMD_REQ 0x05
0022 #define REALTEK_USB_VENQT_CMD_IDX 0x00
0023
0024 #define MAX_USBCTRL_VENDORREQ_TIMES 10
0025
0026 static void usbctrl_async_callback(struct urb *urb)
0027 {
0028 if (urb) {
0029
0030 kfree(urb->setup_packet);
0031
0032 kfree(urb->transfer_buffer);
0033 }
0034 }
0035
0036 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
0037 u16 value, u16 index, void *pdata,
0038 u16 len)
0039 {
0040 int rc;
0041 unsigned int pipe;
0042 u8 reqtype;
0043 struct usb_ctrlrequest *dr;
0044 struct urb *urb;
0045 const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE;
0046 u8 *databuf;
0047
0048 if (WARN_ON_ONCE(len > databuf_maxlen))
0049 len = databuf_maxlen;
0050
0051 pipe = usb_sndctrlpipe(udev, 0);
0052 reqtype = REALTEK_USB_VENQT_WRITE;
0053
0054 dr = kzalloc(sizeof(*dr), GFP_ATOMIC);
0055 if (!dr)
0056 return -ENOMEM;
0057
0058 databuf = kzalloc(databuf_maxlen, GFP_ATOMIC);
0059 if (!databuf) {
0060 kfree(dr);
0061 return -ENOMEM;
0062 }
0063
0064 urb = usb_alloc_urb(0, GFP_ATOMIC);
0065 if (!urb) {
0066 kfree(databuf);
0067 kfree(dr);
0068 return -ENOMEM;
0069 }
0070
0071 dr->bRequestType = reqtype;
0072 dr->bRequest = request;
0073 dr->wValue = cpu_to_le16(value);
0074 dr->wIndex = cpu_to_le16(index);
0075 dr->wLength = cpu_to_le16(len);
0076
0077 memcpy(databuf, pdata, len);
0078 usb_fill_control_urb(urb, udev, pipe,
0079 (unsigned char *)dr, databuf, len,
0080 usbctrl_async_callback, NULL);
0081 rc = usb_submit_urb(urb, GFP_ATOMIC);
0082 if (rc < 0) {
0083 kfree(databuf);
0084 kfree(dr);
0085 }
0086 usb_free_urb(urb);
0087 return rc;
0088 }
0089
0090 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
0091 u16 value, u16 index, void *pdata,
0092 u16 len)
0093 {
0094 unsigned int pipe;
0095 int status;
0096 u8 reqtype;
0097 int vendorreq_times = 0;
0098 static int count;
0099
0100 pipe = usb_rcvctrlpipe(udev, 0);
0101 reqtype = REALTEK_USB_VENQT_READ;
0102
0103 do {
0104 status = usb_control_msg(udev, pipe, request, reqtype, value,
0105 index, pdata, len, 1000);
0106 if (status < 0) {
0107
0108 if ((value >= FW_8192C_START_ADDRESS &&
0109 value <= FW_8192C_END_ADDRESS))
0110 break;
0111 } else {
0112 break;
0113 }
0114 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
0115
0116 if (status < 0 && count++ < 4)
0117 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
0118 value, status, *(u32 *)pdata);
0119 return status;
0120 }
0121
0122 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
0123 {
0124 struct device *dev = rtlpriv->io.dev;
0125 struct usb_device *udev = to_usb_device(dev);
0126 u8 request;
0127 u16 wvalue;
0128 u16 index;
0129 __le32 *data;
0130 unsigned long flags;
0131
0132 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
0133 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
0134 rtlpriv->usb_data_index = 0;
0135 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
0136 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
0137 request = REALTEK_USB_VENQT_CMD_REQ;
0138 index = REALTEK_USB_VENQT_CMD_IDX;
0139
0140 wvalue = (u16)addr;
0141 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
0142 return le32_to_cpu(*data);
0143 }
0144
0145 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
0146 {
0147 return (u8)_usb_read_sync(rtlpriv, addr, 1);
0148 }
0149
0150 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
0151 {
0152 return (u16)_usb_read_sync(rtlpriv, addr, 2);
0153 }
0154
0155 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
0156 {
0157 return _usb_read_sync(rtlpriv, addr, 4);
0158 }
0159
0160 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
0161 u16 len)
0162 {
0163 u8 request;
0164 u16 wvalue;
0165 u16 index;
0166 __le32 data;
0167
0168 request = REALTEK_USB_VENQT_CMD_REQ;
0169 index = REALTEK_USB_VENQT_CMD_IDX;
0170 wvalue = (u16)(addr&0x0000ffff);
0171 data = cpu_to_le32(val);
0172 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
0173 len);
0174 }
0175
0176 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
0177 {
0178 struct device *dev = rtlpriv->io.dev;
0179
0180 _usb_write_async(to_usb_device(dev), addr, val, 1);
0181 }
0182
0183 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
0184 {
0185 struct device *dev = rtlpriv->io.dev;
0186
0187 _usb_write_async(to_usb_device(dev), addr, val, 2);
0188 }
0189
0190 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
0191 {
0192 struct device *dev = rtlpriv->io.dev;
0193
0194 _usb_write_async(to_usb_device(dev), addr, val, 4);
0195 }
0196
0197 static void _usb_writen_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
0198 u16 len)
0199 {
0200 struct device *dev = rtlpriv->io.dev;
0201 struct usb_device *udev = to_usb_device(dev);
0202 u8 request = REALTEK_USB_VENQT_CMD_REQ;
0203 u8 reqtype = REALTEK_USB_VENQT_WRITE;
0204 u16 wvalue;
0205 u16 index = REALTEK_USB_VENQT_CMD_IDX;
0206 int pipe = usb_sndctrlpipe(udev, 0);
0207 u8 *buffer;
0208
0209 wvalue = (u16)(addr & 0x0000ffff);
0210 buffer = kmemdup(data, len, GFP_ATOMIC);
0211 if (!buffer)
0212 return;
0213 usb_control_msg(udev, pipe, request, reqtype, wvalue,
0214 index, buffer, len, 50);
0215
0216 kfree(buffer);
0217 }
0218
0219 static void _rtl_usb_io_handler_init(struct device *dev,
0220 struct ieee80211_hw *hw)
0221 {
0222 struct rtl_priv *rtlpriv = rtl_priv(hw);
0223
0224 rtlpriv->io.dev = dev;
0225 mutex_init(&rtlpriv->io.bb_mutex);
0226 rtlpriv->io.write8_async = _usb_write8_async;
0227 rtlpriv->io.write16_async = _usb_write16_async;
0228 rtlpriv->io.write32_async = _usb_write32_async;
0229 rtlpriv->io.read8_sync = _usb_read8_sync;
0230 rtlpriv->io.read16_sync = _usb_read16_sync;
0231 rtlpriv->io.read32_sync = _usb_read32_sync;
0232 rtlpriv->io.writen_sync = _usb_writen_sync;
0233 }
0234
0235 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
0236 {
0237 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
0238
0239 mutex_destroy(&rtlpriv->io.bb_mutex);
0240 }
0241
0242
0243 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
0244 struct sk_buff_head *list)
0245 {
0246 return skb_dequeue(list);
0247 }
0248
0249 #define IS_HIGH_SPEED_USB(udev) \
0250 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
0251
0252 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
0253 {
0254 u32 i;
0255 struct rtl_priv *rtlpriv = rtl_priv(hw);
0256 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0257
0258 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
0259 ? USB_HIGH_SPEED_BULK_SIZE
0260 : USB_FULL_SPEED_BULK_SIZE;
0261
0262 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
0263 rtlusb->max_bulk_out_size);
0264
0265 for (i = 0; i < __RTL_TXQ_NUM; i++) {
0266 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
0267
0268 if (!ep_num) {
0269 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
0270 "Invalid endpoint map setting!\n");
0271 return -EINVAL;
0272 }
0273 }
0274
0275 rtlusb->usb_tx_post_hdl =
0276 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
0277 rtlusb->usb_tx_cleanup =
0278 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
0279 rtlusb->usb_tx_aggregate_hdl =
0280 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
0281 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
0282 : &_none_usb_tx_aggregate_hdl;
0283
0284 init_usb_anchor(&rtlusb->tx_submitted);
0285 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
0286 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
0287 init_usb_anchor(&rtlusb->tx_pending[i]);
0288 }
0289 return 0;
0290 }
0291
0292 static void _rtl_rx_work(struct tasklet_struct *t);
0293
0294 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
0295 {
0296 struct rtl_priv *rtlpriv = rtl_priv(hw);
0297 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
0298 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
0299
0300 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
0301 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
0302 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
0303 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
0304 rtlusb->usb_rx_segregate_hdl =
0305 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
0306
0307 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
0308 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
0309 init_usb_anchor(&rtlusb->rx_submitted);
0310 init_usb_anchor(&rtlusb->rx_cleanup_urbs);
0311
0312 skb_queue_head_init(&rtlusb->rx_queue);
0313 tasklet_setup(&rtlusb->rx_work_tasklet, _rtl_rx_work);
0314
0315 return 0;
0316 }
0317
0318 static int _rtl_usb_init(struct ieee80211_hw *hw)
0319 {
0320 struct rtl_priv *rtlpriv = rtl_priv(hw);
0321 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
0322 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
0323 int err;
0324 u8 epidx;
0325 struct usb_interface *usb_intf = rtlusb->intf;
0326 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
0327
0328 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
0329 for (epidx = 0; epidx < epnums; epidx++) {
0330 struct usb_endpoint_descriptor *pep_desc;
0331
0332 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
0333
0334 if (usb_endpoint_dir_in(pep_desc))
0335 rtlusb->in_ep_nums++;
0336 else if (usb_endpoint_dir_out(pep_desc))
0337 rtlusb->out_ep_nums++;
0338
0339 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
0340 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
0341 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
0342 pep_desc->bInterval);
0343 }
0344 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
0345 pr_err("Too few input end points found\n");
0346 return -EINVAL;
0347 }
0348 if (rtlusb->out_ep_nums == 0) {
0349 pr_err("No output end points found\n");
0350 return -EINVAL;
0351 }
0352
0353 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
0354 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
0355 _rtl_usb_init_tx(hw);
0356 _rtl_usb_init_rx(hw);
0357 return err;
0358 }
0359
0360 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
0361 {
0362 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0363 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0364 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
0365 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0366
0367 rtlhal->hw = hw;
0368 ppsc->inactiveps = false;
0369 ppsc->leisure_ps = false;
0370 ppsc->fwctrl_lps = false;
0371 ppsc->reg_fwctrl_lps = 3;
0372 ppsc->reg_max_lps_awakeintvl = 5;
0373 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
0374
0375
0376 mac->beacon_interval = 100;
0377
0378
0379 mac->min_space_cfg = 0;
0380 mac->max_mss_density = 0;
0381
0382
0383 mac->current_ampdu_density = 7;
0384 mac->current_ampdu_factor = 3;
0385
0386
0387 rtlusb->acm_method = EACMWAY2_SW;
0388
0389
0390
0391 rtlusb->irq_mask[0] = 0xFFFFFFFF;
0392
0393 rtlusb->irq_mask[1] = 0xFFFFFFFF;
0394 rtlusb->disablehwsm = true;
0395 }
0396
0397 static void _rtl_rx_completed(struct urb *urb);
0398
0399 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
0400 struct urb *urb, gfp_t gfp_mask)
0401 {
0402 void *buf;
0403
0404 buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
0405 &urb->transfer_dma);
0406 if (!buf) {
0407 pr_err("Failed to usb_alloc_coherent!!\n");
0408 return -ENOMEM;
0409 }
0410
0411 usb_fill_bulk_urb(urb, rtlusb->udev,
0412 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
0413 buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
0414 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0415
0416 return 0;
0417 }
0418
0419 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
0420 struct sk_buff *skb)
0421 {
0422 struct rtl_priv *rtlpriv = rtl_priv(hw);
0423 u8 *rxdesc = skb->data;
0424 struct ieee80211_hdr *hdr;
0425 bool unicast = false;
0426 __le16 fc;
0427 struct ieee80211_rx_status rx_status = {0};
0428 struct rtl_stats stats = {
0429 .signal = 0,
0430 .rate = 0,
0431 };
0432
0433 skb_pull(skb, RTL_RX_DESC_SIZE);
0434 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
0435 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
0436 hdr = (struct ieee80211_hdr *)(skb->data);
0437 fc = hdr->frame_control;
0438 if (!stats.crc) {
0439 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
0440
0441 if (is_broadcast_ether_addr(hdr->addr1)) {
0442 ;
0443 } else if (is_multicast_ether_addr(hdr->addr1)) {
0444
0445 } else {
0446 unicast = true;
0447 rtlpriv->stats.rxbytesunicast += skb->len;
0448 }
0449
0450 if (ieee80211_is_data(fc)) {
0451 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
0452
0453 if (unicast)
0454 rtlpriv->link_info.num_rx_inperiod++;
0455 }
0456
0457 rtl_beacon_statistic(hw, skb);
0458 }
0459 }
0460
0461 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
0462 struct sk_buff *skb)
0463 {
0464 struct rtl_priv *rtlpriv = rtl_priv(hw);
0465 u8 *rxdesc = skb->data;
0466 struct ieee80211_hdr *hdr;
0467 bool unicast = false;
0468 __le16 fc;
0469 struct ieee80211_rx_status rx_status = {0};
0470 struct rtl_stats stats = {
0471 .signal = 0,
0472 .rate = 0,
0473 };
0474
0475 skb_pull(skb, RTL_RX_DESC_SIZE);
0476 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
0477 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
0478 hdr = (struct ieee80211_hdr *)(skb->data);
0479 fc = hdr->frame_control;
0480 if (!stats.crc) {
0481 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
0482
0483 if (is_broadcast_ether_addr(hdr->addr1)) {
0484 ;
0485 } else if (is_multicast_ether_addr(hdr->addr1)) {
0486
0487 } else {
0488 unicast = true;
0489 rtlpriv->stats.rxbytesunicast += skb->len;
0490 }
0491
0492 if (ieee80211_is_data(fc)) {
0493 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
0494
0495 if (unicast)
0496 rtlpriv->link_info.num_rx_inperiod++;
0497 }
0498
0499
0500 rtl_beacon_statistic(hw, skb);
0501
0502 if (likely(rtl_action_proc(hw, skb, false)))
0503 ieee80211_rx(hw, skb);
0504 else
0505 dev_kfree_skb_any(skb);
0506 } else {
0507 dev_kfree_skb_any(skb);
0508 }
0509 }
0510
0511 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
0512 {
0513 struct sk_buff *_skb;
0514 struct sk_buff_head rx_queue;
0515 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0516
0517 skb_queue_head_init(&rx_queue);
0518 if (rtlusb->usb_rx_segregate_hdl)
0519 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
0520 WARN_ON(skb_queue_empty(&rx_queue));
0521 while (!skb_queue_empty(&rx_queue)) {
0522 _skb = skb_dequeue(&rx_queue);
0523 _rtl_usb_rx_process_agg(hw, _skb);
0524 ieee80211_rx(hw, _skb);
0525 }
0526 }
0527
0528 #define __RX_SKB_MAX_QUEUED 64
0529
0530 static void _rtl_rx_work(struct tasklet_struct *t)
0531 {
0532 struct rtl_usb *rtlusb = from_tasklet(rtlusb, t, rx_work_tasklet);
0533 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
0534 struct sk_buff *skb;
0535
0536 while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
0537 if (unlikely(IS_USB_STOP(rtlusb))) {
0538 dev_kfree_skb_any(skb);
0539 continue;
0540 }
0541
0542 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
0543 _rtl_usb_rx_process_noagg(hw, skb);
0544 } else {
0545
0546 _rtl_rx_pre_process(hw, skb);
0547 pr_err("rx agg not supported\n");
0548 }
0549 }
0550 }
0551
0552 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr,
0553 unsigned int len)
0554 {
0555 #if NET_IP_ALIGN != 0
0556 unsigned int padding = 0;
0557 #endif
0558
0559
0560 if (NET_IP_ALIGN == 0 || len < sizeof(*hdr))
0561 return 0;
0562
0563 #if NET_IP_ALIGN != 0
0564
0565
0566
0567 if (ieee80211_is_data_qos(hdr->frame_control)) {
0568 u8 *qc = ieee80211_get_qos_ctl(hdr);
0569
0570 padding ^= NET_IP_ALIGN;
0571
0572
0573
0574
0575 if ((unsigned long)qc - (unsigned long)hdr < len &&
0576 *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
0577 padding ^= NET_IP_ALIGN;
0578 }
0579
0580 if (ieee80211_has_a4(hdr->frame_control))
0581 padding ^= NET_IP_ALIGN;
0582
0583 return padding;
0584 #endif
0585 }
0586
0587 #define __RADIO_TAP_SIZE_RSV 32
0588
0589 static void _rtl_rx_completed(struct urb *_urb)
0590 {
0591 struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
0592 int err = 0;
0593
0594 if (unlikely(IS_USB_STOP(rtlusb)))
0595 goto free;
0596
0597 if (likely(0 == _urb->status)) {
0598 unsigned int padding;
0599 struct sk_buff *skb;
0600 unsigned int qlen;
0601 unsigned int size = _urb->actual_length;
0602 struct ieee80211_hdr *hdr;
0603
0604 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
0605 pr_err("Too short packet from bulk IN! (len: %d)\n",
0606 size);
0607 goto resubmit;
0608 }
0609
0610 qlen = skb_queue_len(&rtlusb->rx_queue);
0611 if (qlen >= __RX_SKB_MAX_QUEUED) {
0612 pr_err("Pending RX skbuff queue full! (qlen: %d)\n",
0613 qlen);
0614 goto resubmit;
0615 }
0616
0617 hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE);
0618 padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE);
0619
0620 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding);
0621 if (!skb) {
0622 pr_err("Can't allocate skb for bulk IN!\n");
0623 goto resubmit;
0624 }
0625
0626 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
0627
0628
0629 skb_reserve(skb, padding);
0630
0631
0632 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
0633
0634 skb_put_data(skb, _urb->transfer_buffer, size);
0635
0636 skb_queue_tail(&rtlusb->rx_queue, skb);
0637 tasklet_schedule(&rtlusb->rx_work_tasklet);
0638
0639 goto resubmit;
0640 }
0641
0642 switch (_urb->status) {
0643
0644 case -ENOENT:
0645 case -ECONNRESET:
0646 case -ENODEV:
0647 case -ESHUTDOWN:
0648 goto free;
0649 default:
0650 break;
0651 }
0652
0653 resubmit:
0654 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
0655 err = usb_submit_urb(_urb, GFP_ATOMIC);
0656 if (unlikely(err)) {
0657 usb_unanchor_urb(_urb);
0658 goto free;
0659 }
0660 return;
0661
0662 free:
0663
0664
0665
0666 usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
0667 }
0668
0669 #undef __RADIO_TAP_SIZE_RSV
0670
0671 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
0672 {
0673 struct rtl_priv *rtlpriv = rtl_priv(hw);
0674 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0675 struct urb *urb;
0676
0677 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
0678
0679 tasklet_kill(&rtlusb->rx_work_tasklet);
0680 cancel_work_sync(&rtlpriv->works.lps_change_work);
0681
0682 if (rtlpriv->works.rtl_wq) {
0683 destroy_workqueue(rtlpriv->works.rtl_wq);
0684 rtlpriv->works.rtl_wq = NULL;
0685 }
0686
0687 skb_queue_purge(&rtlusb->rx_queue);
0688
0689 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
0690 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0691 urb->transfer_buffer, urb->transfer_dma);
0692 usb_free_urb(urb);
0693 }
0694 }
0695
0696 static int _rtl_usb_receive(struct ieee80211_hw *hw)
0697 {
0698 struct urb *urb;
0699 int err;
0700 int i;
0701 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0702
0703 WARN_ON(0 == rtlusb->rx_urb_num);
0704
0705 WARN_ON(rtlusb->rx_max_size < 1600);
0706
0707 for (i = 0; i < rtlusb->rx_urb_num; i++) {
0708 err = -ENOMEM;
0709 urb = usb_alloc_urb(0, GFP_KERNEL);
0710 if (!urb)
0711 goto err_out;
0712
0713 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
0714 if (err < 0) {
0715 pr_err("Failed to prep_rx_urb!!\n");
0716 usb_free_urb(urb);
0717 goto err_out;
0718 }
0719
0720 usb_anchor_urb(urb, &rtlusb->rx_submitted);
0721 err = usb_submit_urb(urb, GFP_KERNEL);
0722 if (err) {
0723 usb_unanchor_urb(urb);
0724 usb_free_urb(urb);
0725 goto err_out;
0726 }
0727 usb_free_urb(urb);
0728 }
0729 return 0;
0730
0731 err_out:
0732 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
0733 return err;
0734 }
0735
0736 static int rtl_usb_start(struct ieee80211_hw *hw)
0737 {
0738 int err;
0739 struct rtl_priv *rtlpriv = rtl_priv(hw);
0740 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0741 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0742
0743 err = rtlpriv->cfg->ops->hw_init(hw);
0744 if (!err) {
0745 rtl_init_rx_config(hw);
0746
0747
0748 SET_USB_START(rtlusb);
0749
0750 set_hal_start(rtlhal);
0751
0752
0753 err = _rtl_usb_receive(hw);
0754 }
0755
0756 return err;
0757 }
0758
0759
0760 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
0761 {
0762 u32 i;
0763 struct sk_buff *_skb;
0764 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0765 struct ieee80211_tx_info *txinfo;
0766
0767
0768 _rtl_usb_cleanup_rx(hw);
0769
0770
0771 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
0772 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
0773 rtlusb->usb_tx_cleanup(hw, _skb);
0774 txinfo = IEEE80211_SKB_CB(_skb);
0775 ieee80211_tx_info_clear_status(txinfo);
0776 txinfo->flags |= IEEE80211_TX_STAT_ACK;
0777 ieee80211_tx_status_irqsafe(hw, _skb);
0778 }
0779 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
0780 }
0781 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
0782 }
0783
0784
0785 static void rtl_usb_deinit(struct ieee80211_hw *hw)
0786 {
0787 rtl_usb_cleanup(hw);
0788 }
0789
0790 static void rtl_usb_stop(struct ieee80211_hw *hw)
0791 {
0792 struct rtl_priv *rtlpriv = rtl_priv(hw);
0793 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0794 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0795 struct urb *urb;
0796
0797
0798 set_hal_stop(rtlhal);
0799 cancel_work_sync(&rtlpriv->works.fill_h2c_cmd);
0800
0801 SET_USB_STOP(rtlusb);
0802
0803
0804 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
0805
0806 tasklet_kill(&rtlusb->rx_work_tasklet);
0807 cancel_work_sync(&rtlpriv->works.lps_change_work);
0808 cancel_work_sync(&rtlpriv->works.update_beacon_work);
0809
0810 flush_workqueue(rtlpriv->works.rtl_wq);
0811
0812 skb_queue_purge(&rtlusb->rx_queue);
0813
0814 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
0815 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0816 urb->transfer_buffer, urb->transfer_dma);
0817 usb_free_urb(urb);
0818 }
0819
0820 rtlpriv->cfg->ops->hw_disable(hw);
0821 }
0822
0823 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
0824 {
0825 int err;
0826 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0827
0828 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
0829 err = usb_submit_urb(_urb, GFP_ATOMIC);
0830 if (err < 0) {
0831 struct sk_buff *skb;
0832
0833 pr_err("Failed to submit urb\n");
0834 usb_unanchor_urb(_urb);
0835 skb = (struct sk_buff *)_urb->context;
0836 kfree_skb(skb);
0837 }
0838 usb_free_urb(_urb);
0839 }
0840
0841 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
0842 struct sk_buff *skb)
0843 {
0844 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0845 struct ieee80211_tx_info *txinfo;
0846
0847 rtlusb->usb_tx_post_hdl(hw, urb, skb);
0848 skb_pull(skb, RTL_TX_HEADER_SIZE);
0849 txinfo = IEEE80211_SKB_CB(skb);
0850 ieee80211_tx_info_clear_status(txinfo);
0851 txinfo->flags |= IEEE80211_TX_STAT_ACK;
0852
0853 if (urb->status) {
0854 pr_err("Urb has error status 0x%X\n", urb->status);
0855 goto out;
0856 }
0857
0858 out:
0859 ieee80211_tx_status_irqsafe(hw, skb);
0860 return urb->status;
0861 }
0862
0863 static void _rtl_tx_complete(struct urb *urb)
0864 {
0865 struct sk_buff *skb = (struct sk_buff *)urb->context;
0866 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0867 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
0868 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
0869 int err;
0870
0871 if (unlikely(IS_USB_STOP(rtlusb)))
0872 return;
0873 err = _usb_tx_post(hw, urb, skb);
0874 if (err) {
0875
0876 return;
0877 }
0878 }
0879
0880 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
0881 struct sk_buff *skb, u32 ep_num)
0882 {
0883 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0884 struct urb *_urb;
0885
0886 WARN_ON(NULL == skb);
0887 _urb = usb_alloc_urb(0, GFP_ATOMIC);
0888 if (!_urb)
0889 return NULL;
0890 _rtl_install_trx_info(rtlusb, skb, ep_num);
0891 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
0892 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
0893 _urb->transfer_flags |= URB_ZERO_PACKET;
0894 return _urb;
0895 }
0896
0897 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
0898 enum rtl_txq qnum)
0899 {
0900 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0901 u32 ep_num;
0902 struct urb *_urb = NULL;
0903
0904 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
0905 if (unlikely(IS_USB_STOP(rtlusb))) {
0906 pr_err("USB device is stopping...\n");
0907 kfree_skb(skb);
0908 return;
0909 }
0910 ep_num = rtlusb->ep_map.ep_mapping[qnum];
0911 _urb = _rtl_usb_tx_urb_setup(hw, skb, ep_num);
0912 if (unlikely(!_urb)) {
0913 pr_err("Can't allocate urb. Drop skb!\n");
0914 kfree_skb(skb);
0915 return;
0916 }
0917 _rtl_submit_tx_urb(hw, _urb);
0918 }
0919
0920 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
0921 struct ieee80211_sta *sta,
0922 struct sk_buff *skb,
0923 u16 hw_queue)
0924 {
0925 struct rtl_priv *rtlpriv = rtl_priv(hw);
0926 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0927 struct rtl_tx_desc *pdesc = NULL;
0928 struct rtl_tcb_desc tcb_desc;
0929 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
0930 __le16 fc = hdr->frame_control;
0931 u8 *pda_addr = hdr->addr1;
0932
0933 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
0934 if (ieee80211_is_auth(fc)) {
0935 rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
0936 }
0937
0938 if (rtlpriv->psc.sw_ps_enabled) {
0939 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
0940 !ieee80211_has_pm(fc))
0941 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
0942 }
0943
0944 rtl_action_proc(hw, skb, true);
0945 if (is_multicast_ether_addr(pda_addr))
0946 rtlpriv->stats.txbytesmulticast += skb->len;
0947 else if (is_broadcast_ether_addr(pda_addr))
0948 rtlpriv->stats.txbytesbroadcast += skb->len;
0949 else
0950 rtlpriv->stats.txbytesunicast += skb->len;
0951 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, NULL, info, sta, skb,
0952 hw_queue, &tcb_desc);
0953 if (ieee80211_is_data(fc))
0954 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
0955 }
0956
0957 static int rtl_usb_tx(struct ieee80211_hw *hw,
0958 struct ieee80211_sta *sta,
0959 struct sk_buff *skb,
0960 struct rtl_tcb_desc *dummy)
0961 {
0962 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
0963 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
0964 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
0965 __le16 fc = hdr->frame_control;
0966 u16 hw_queue;
0967
0968 if (unlikely(is_hal_stop(rtlhal)))
0969 goto err_free;
0970 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
0971 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
0972 _rtl_usb_transmit(hw, skb, hw_queue);
0973 return NETDEV_TX_OK;
0974
0975 err_free:
0976 dev_kfree_skb_any(skb);
0977 return NETDEV_TX_OK;
0978 }
0979
0980 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
0981 struct ieee80211_sta *sta,
0982 struct sk_buff *skb)
0983 {
0984 return false;
0985 }
0986
0987 static void rtl_fill_h2c_cmd_work_callback(struct work_struct *work)
0988 {
0989 struct rtl_works *rtlworks =
0990 container_of(work, struct rtl_works, fill_h2c_cmd);
0991 struct ieee80211_hw *hw = rtlworks->hw;
0992 struct rtl_priv *rtlpriv = rtl_priv(hw);
0993
0994 rtlpriv->cfg->ops->fill_h2c_cmd(hw, H2C_RA_MASK, 5, rtlpriv->rate_mask);
0995 }
0996
0997 static const struct rtl_intf_ops rtl_usb_ops = {
0998 .adapter_start = rtl_usb_start,
0999 .adapter_stop = rtl_usb_stop,
1000 .adapter_tx = rtl_usb_tx,
1001 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
1002 };
1003
1004 int rtl_usb_probe(struct usb_interface *intf,
1005 const struct usb_device_id *id,
1006 struct rtl_hal_cfg *rtl_hal_cfg)
1007 {
1008 int err;
1009 struct ieee80211_hw *hw = NULL;
1010 struct rtl_priv *rtlpriv = NULL;
1011 struct usb_device *udev;
1012 struct rtl_usb_priv *usb_priv;
1013
1014 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
1015 sizeof(struct rtl_usb_priv), &rtl_ops);
1016 if (!hw) {
1017 pr_warn("rtl_usb: ieee80211 alloc failed\n");
1018 return -ENOMEM;
1019 }
1020 rtlpriv = hw->priv;
1021 rtlpriv->hw = hw;
1022 rtlpriv->usb_data = kcalloc(RTL_USB_MAX_RX_COUNT, sizeof(u32),
1023 GFP_KERNEL);
1024 if (!rtlpriv->usb_data) {
1025 ieee80211_free_hw(hw);
1026 return -ENOMEM;
1027 }
1028
1029
1030 spin_lock_init(&rtlpriv->locks.usb_lock);
1031 INIT_WORK(&rtlpriv->works.fill_h2c_cmd,
1032 rtl_fill_h2c_cmd_work_callback);
1033 INIT_WORK(&rtlpriv->works.lps_change_work,
1034 rtl_lps_change_work_callback);
1035 INIT_WORK(&rtlpriv->works.update_beacon_work,
1036 rtl_update_beacon_work_callback);
1037
1038 rtlpriv->usb_data_index = 0;
1039 init_completion(&rtlpriv->firmware_loading_complete);
1040 SET_IEEE80211_DEV(hw, &intf->dev);
1041 udev = interface_to_usbdev(intf);
1042 usb_get_dev(udev);
1043 usb_priv = rtl_usbpriv(hw);
1044 memset(usb_priv, 0, sizeof(*usb_priv));
1045 usb_priv->dev.intf = intf;
1046 usb_priv->dev.udev = udev;
1047 usb_set_intfdata(intf, hw);
1048
1049 rtlpriv->rtlhal.interface = INTF_USB;
1050 rtlpriv->cfg = rtl_hal_cfg;
1051 rtlpriv->intf_ops = &rtl_usb_ops;
1052
1053 _rtl_usb_io_handler_init(&udev->dev, hw);
1054 rtlpriv->cfg->ops->read_chip_version(hw);
1055
1056 rtlpriv->cfg->ops->read_eeprom_info(hw);
1057 err = _rtl_usb_init(hw);
1058 if (err)
1059 goto error_out2;
1060 rtl_usb_init_sw(hw);
1061
1062 err = rtl_init_core(hw);
1063 if (err) {
1064 pr_err("Can't allocate sw for mac80211\n");
1065 goto error_out2;
1066 }
1067 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1068 pr_err("Can't init_sw_vars\n");
1069 goto error_out;
1070 }
1071 rtlpriv->cfg->ops->init_sw_leds(hw);
1072
1073 err = ieee80211_register_hw(hw);
1074 if (err) {
1075 pr_err("Can't register mac80211 hw.\n");
1076 goto error_out;
1077 }
1078 rtlpriv->mac80211.mac80211_registered = 1;
1079
1080 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1081 return 0;
1082
1083 error_out:
1084 rtl_deinit_core(hw);
1085 error_out2:
1086 _rtl_usb_io_handler_release(hw);
1087 usb_put_dev(udev);
1088 complete(&rtlpriv->firmware_loading_complete);
1089 kfree(rtlpriv->usb_data);
1090 ieee80211_free_hw(hw);
1091 return -ENODEV;
1092 }
1093 EXPORT_SYMBOL(rtl_usb_probe);
1094
1095 void rtl_usb_disconnect(struct usb_interface *intf)
1096 {
1097 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1098 struct rtl_priv *rtlpriv = rtl_priv(hw);
1099 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1100 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1101
1102 if (unlikely(!rtlpriv))
1103 return;
1104
1105 wait_for_completion(&rtlpriv->firmware_loading_complete);
1106 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1107
1108 if (rtlmac->mac80211_registered == 1) {
1109 ieee80211_unregister_hw(hw);
1110 rtlmac->mac80211_registered = 0;
1111 } else {
1112 rtl_deinit_deferred_work(hw, false);
1113 rtlpriv->intf_ops->adapter_stop(hw);
1114 }
1115
1116
1117 rtl_usb_deinit(hw);
1118 rtl_deinit_core(hw);
1119 kfree(rtlpriv->usb_data);
1120 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1121 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1122 _rtl_usb_io_handler_release(hw);
1123 usb_put_dev(rtlusb->udev);
1124 usb_set_intfdata(intf, NULL);
1125 ieee80211_free_hw(hw);
1126 }
1127 EXPORT_SYMBOL(rtl_usb_disconnect);
1128
1129 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1130 {
1131 return 0;
1132 }
1133 EXPORT_SYMBOL(rtl_usb_suspend);
1134
1135 int rtl_usb_resume(struct usb_interface *pusb_intf)
1136 {
1137 return 0;
1138 }
1139 EXPORT_SYMBOL(rtl_usb_resume);