1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2009-2012 Realtek Corporation.*/ 3 4 #include "wifi.h" 5 #include "core.h" 6 #include "usb.h" 7 #include "base.h" 8 #include "ps.h" 9 #include "rtl8192c/fw_common.h" 10 #include <linux/export.h> 11 #include <linux/module.h> 12 13 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>"); 14 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>"); 15 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>"); 16 MODULE_LICENSE("GPL"); 17 MODULE_DESCRIPTION("USB basic driver for rtlwifi"); 18 19 #define REALTEK_USB_VENQT_READ 0xC0 20 #define REALTEK_USB_VENQT_WRITE 0x40 21 #define REALTEK_USB_VENQT_CMD_REQ 0x05 22 #define REALTEK_USB_VENQT_CMD_IDX 0x00 23 24 #define MAX_USBCTRL_VENDORREQ_TIMES 10 25 26 static void usbctrl_async_callback(struct urb *urb) 27 { 28 if (urb) { 29 /* free dr */ 30 kfree(urb->setup_packet); 31 /* free databuf */ 32 kfree(urb->transfer_buffer); 33 } 34 } 35 36 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request, 37 u16 value, u16 index, void *pdata, 38 u16 len) 39 { 40 int rc; 41 unsigned int pipe; 42 u8 reqtype; 43 struct usb_ctrlrequest *dr; 44 struct urb *urb; 45 const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE; 46 u8 *databuf; 47 48 if (WARN_ON_ONCE(len > databuf_maxlen)) 49 len = databuf_maxlen; 50 51 pipe = usb_sndctrlpipe(udev, 0); /* write_out */ 52 reqtype = REALTEK_USB_VENQT_WRITE; 53 54 dr = kzalloc(sizeof(*dr), GFP_ATOMIC); 55 if (!dr) 56 return -ENOMEM; 57 58 databuf = kzalloc(databuf_maxlen, GFP_ATOMIC); 59 if (!databuf) { 60 kfree(dr); 61 return -ENOMEM; 62 } 63 64 urb = usb_alloc_urb(0, GFP_ATOMIC); 65 if (!urb) { 66 kfree(databuf); 67 kfree(dr); 68 return -ENOMEM; 69 } 70 71 dr->bRequestType = reqtype; 72 dr->bRequest = request; 73 dr->wValue = cpu_to_le16(value); 74 dr->wIndex = cpu_to_le16(index); 75 dr->wLength = cpu_to_le16(len); 76 /* data are already in little-endian order */ 77 memcpy(databuf, pdata, len); 78 usb_fill_control_urb(urb, udev, pipe, 79 (unsigned char *)dr, databuf, len, 80 usbctrl_async_callback, NULL); 81 rc = usb_submit_urb(urb, GFP_ATOMIC); 82 if (rc < 0) { 83 kfree(databuf); 84 kfree(dr); 85 } 86 usb_free_urb(urb); 87 return rc; 88 } 89 90 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request, 91 u16 value, u16 index, void *pdata, 92 u16 len) 93 { 94 unsigned int pipe; 95 int status; 96 u8 reqtype; 97 int vendorreq_times = 0; 98 static int count; 99 100 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */ 101 reqtype = REALTEK_USB_VENQT_READ; 102 103 do { 104 status = usb_control_msg(udev, pipe, request, reqtype, value, 105 index, pdata, len, 1000); 106 if (status < 0) { 107 /* firmware download is checksumed, don't retry */ 108 if ((value >= FW_8192C_START_ADDRESS && 109 value <= FW_8192C_END_ADDRESS)) 110 break; 111 } else { 112 break; 113 } 114 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES); 115 116 if (status < 0 && count++ < 4) 117 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n", 118 value, status, *(u32 *)pdata); 119 return status; 120 } 121 122 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len) 123 { 124 struct device *dev = rtlpriv->io.dev; 125 struct usb_device *udev = to_usb_device(dev); 126 u8 request; 127 u16 wvalue; 128 u16 index; 129 __le32 *data; 130 unsigned long flags; 131 132 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags); 133 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT) 134 rtlpriv->usb_data_index = 0; 135 data = &rtlpriv->usb_data[rtlpriv->usb_data_index]; 136 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags); 137 request = REALTEK_USB_VENQT_CMD_REQ; 138 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */ 139 140 wvalue = (u16)addr; 141 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len); 142 return le32_to_cpu(*data); 143 } 144 145 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr) 146 { 147 return (u8)_usb_read_sync(rtlpriv, addr, 1); 148 } 149 150 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr) 151 { 152 return (u16)_usb_read_sync(rtlpriv, addr, 2); 153 } 154 155 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr) 156 { 157 return _usb_read_sync(rtlpriv, addr, 4); 158 } 159 160 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val, 161 u16 len) 162 { 163 u8 request; 164 u16 wvalue; 165 u16 index; 166 __le32 data; 167 168 request = REALTEK_USB_VENQT_CMD_REQ; 169 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */ 170 wvalue = (u16)(addr&0x0000ffff); 171 data = cpu_to_le32(val); 172 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data, 173 len); 174 } 175 176 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val) 177 { 178 struct device *dev = rtlpriv->io.dev; 179 180 _usb_write_async(to_usb_device(dev), addr, val, 1); 181 } 182 183 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val) 184 { 185 struct device *dev = rtlpriv->io.dev; 186 187 _usb_write_async(to_usb_device(dev), addr, val, 2); 188 } 189 190 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val) 191 { 192 struct device *dev = rtlpriv->io.dev; 193 194 _usb_write_async(to_usb_device(dev), addr, val, 4); 195 } 196 197 static void _usb_writen_sync(struct rtl_priv *rtlpriv, u32 addr, void *data, 198 u16 len) 199 { 200 struct device *dev = rtlpriv->io.dev; 201 struct usb_device *udev = to_usb_device(dev); 202 u8 request = REALTEK_USB_VENQT_CMD_REQ; 203 u8 reqtype = REALTEK_USB_VENQT_WRITE; 204 u16 wvalue; 205 u16 index = REALTEK_USB_VENQT_CMD_IDX; 206 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */ 207 u8 *buffer; 208 209 wvalue = (u16)(addr & 0x0000ffff); 210 buffer = kmemdup(data, len, GFP_ATOMIC); 211 if (!buffer) 212 return; 213 usb_control_msg(udev, pipe, request, reqtype, wvalue, 214 index, buffer, len, 50); 215 216 kfree(buffer); 217 } 218 219 static void _rtl_usb_io_handler_init(struct device *dev, 220 struct ieee80211_hw *hw) 221 { 222 struct rtl_priv *rtlpriv = rtl_priv(hw); 223 224 rtlpriv->io.dev = dev; 225 mutex_init(&rtlpriv->io.bb_mutex); 226 rtlpriv->io.write8_async = _usb_write8_async; 227 rtlpriv->io.write16_async = _usb_write16_async; 228 rtlpriv->io.write32_async = _usb_write32_async; 229 rtlpriv->io.read8_sync = _usb_read8_sync; 230 rtlpriv->io.read16_sync = _usb_read16_sync; 231 rtlpriv->io.read32_sync = _usb_read32_sync; 232 rtlpriv->io.writen_sync = _usb_writen_sync; 233 } 234 235 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw) 236 { 237 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw); 238 239 mutex_destroy(&rtlpriv->io.bb_mutex); 240 } 241 242 /** 243 * 244 * Default aggregation handler. Do nothing and just return the oldest skb. 245 */ 246 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw, 247 struct sk_buff_head *list) 248 { 249 return skb_dequeue(list); 250 } 251 252 #define IS_HIGH_SPEED_USB(udev) \ 253 ((USB_SPEED_HIGH == (udev)->speed) ? true : false) 254 255 static int _rtl_usb_init_tx(struct ieee80211_hw *hw) 256 { 257 u32 i; 258 struct rtl_priv *rtlpriv = rtl_priv(hw); 259 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 260 261 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev) 262 ? USB_HIGH_SPEED_BULK_SIZE 263 : USB_FULL_SPEED_BULK_SIZE; 264 265 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n", 266 rtlusb->max_bulk_out_size); 267 268 for (i = 0; i < __RTL_TXQ_NUM; i++) { 269 u32 ep_num = rtlusb->ep_map.ep_mapping[i]; 270 271 if (!ep_num) { 272 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, 273 "Invalid endpoint map setting!\n"); 274 return -EINVAL; 275 } 276 } 277 278 rtlusb->usb_tx_post_hdl = 279 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl; 280 rtlusb->usb_tx_cleanup = 281 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup; 282 rtlusb->usb_tx_aggregate_hdl = 283 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl) 284 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl 285 : &_none_usb_tx_aggregate_hdl; 286 287 init_usb_anchor(&rtlusb->tx_submitted); 288 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) { 289 skb_queue_head_init(&rtlusb->tx_skb_queue[i]); 290 init_usb_anchor(&rtlusb->tx_pending[i]); 291 } 292 return 0; 293 } 294 295 static void _rtl_rx_work(unsigned long param); 296 297 static int _rtl_usb_init_rx(struct ieee80211_hw *hw) 298 { 299 struct rtl_priv *rtlpriv = rtl_priv(hw); 300 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw); 301 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv); 302 303 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size; 304 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num; 305 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num; 306 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl; 307 rtlusb->usb_rx_segregate_hdl = 308 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl; 309 310 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n", 311 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep); 312 init_usb_anchor(&rtlusb->rx_submitted); 313 init_usb_anchor(&rtlusb->rx_cleanup_urbs); 314 315 skb_queue_head_init(&rtlusb->rx_queue); 316 rtlusb->rx_work_tasklet.func = _rtl_rx_work; 317 rtlusb->rx_work_tasklet.data = (unsigned long)rtlusb; 318 319 return 0; 320 } 321 322 static int _rtl_usb_init(struct ieee80211_hw *hw) 323 { 324 struct rtl_priv *rtlpriv = rtl_priv(hw); 325 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw); 326 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv); 327 int err; 328 u8 epidx; 329 struct usb_interface *usb_intf = rtlusb->intf; 330 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints; 331 332 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0; 333 for (epidx = 0; epidx < epnums; epidx++) { 334 struct usb_endpoint_descriptor *pep_desc; 335 336 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc; 337 338 if (usb_endpoint_dir_in(pep_desc)) 339 rtlusb->in_ep_nums++; 340 else if (usb_endpoint_dir_out(pep_desc)) 341 rtlusb->out_ep_nums++; 342 343 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, 344 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n", 345 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize, 346 pep_desc->bInterval); 347 } 348 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) { 349 pr_err("Too few input end points found\n"); 350 return -EINVAL; 351 } 352 if (rtlusb->out_ep_nums == 0) { 353 pr_err("No output end points found\n"); 354 return -EINVAL; 355 } 356 /* usb endpoint mapping */ 357 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw); 358 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq; 359 _rtl_usb_init_tx(hw); 360 _rtl_usb_init_rx(hw); 361 return err; 362 } 363 364 static void rtl_usb_init_sw(struct ieee80211_hw *hw) 365 { 366 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 367 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 368 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 369 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 370 371 rtlhal->hw = hw; 372 ppsc->inactiveps = false; 373 ppsc->leisure_ps = false; 374 ppsc->fwctrl_lps = false; 375 ppsc->reg_fwctrl_lps = 3; 376 ppsc->reg_max_lps_awakeintvl = 5; 377 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE; 378 379 /* IBSS */ 380 mac->beacon_interval = 100; 381 382 /* AMPDU */ 383 mac->min_space_cfg = 0; 384 mac->max_mss_density = 0; 385 386 /* set sane AMPDU defaults */ 387 mac->current_ampdu_density = 7; 388 mac->current_ampdu_factor = 3; 389 390 /* QOS */ 391 rtlusb->acm_method = EACMWAY2_SW; 392 393 /* IRQ */ 394 /* HIMR - turn all on */ 395 rtlusb->irq_mask[0] = 0xFFFFFFFF; 396 /* HIMR_EX - turn all on */ 397 rtlusb->irq_mask[1] = 0xFFFFFFFF; 398 rtlusb->disablehwsm = true; 399 } 400 401 static void _rtl_rx_completed(struct urb *urb); 402 403 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb, 404 struct urb *urb, gfp_t gfp_mask) 405 { 406 void *buf; 407 408 buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask, 409 &urb->transfer_dma); 410 if (!buf) { 411 pr_err("Failed to usb_alloc_coherent!!\n"); 412 return -ENOMEM; 413 } 414 415 usb_fill_bulk_urb(urb, rtlusb->udev, 416 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep), 417 buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb); 418 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 419 420 return 0; 421 } 422 423 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw, 424 struct sk_buff *skb) 425 { 426 struct rtl_priv *rtlpriv = rtl_priv(hw); 427 u8 *rxdesc = skb->data; 428 struct ieee80211_hdr *hdr; 429 bool unicast = false; 430 __le16 fc; 431 struct ieee80211_rx_status rx_status = {0}; 432 struct rtl_stats stats = { 433 .signal = 0, 434 .rate = 0, 435 }; 436 437 skb_pull(skb, RTL_RX_DESC_SIZE); 438 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb); 439 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift)); 440 hdr = (struct ieee80211_hdr *)(skb->data); 441 fc = hdr->frame_control; 442 if (!stats.crc) { 443 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); 444 445 if (is_broadcast_ether_addr(hdr->addr1)) { 446 /*TODO*/; 447 } else if (is_multicast_ether_addr(hdr->addr1)) { 448 /*TODO*/ 449 } else { 450 unicast = true; 451 rtlpriv->stats.rxbytesunicast += skb->len; 452 } 453 454 if (ieee80211_is_data(fc)) { 455 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX); 456 457 if (unicast) 458 rtlpriv->link_info.num_rx_inperiod++; 459 } 460 /* static bcn for roaming */ 461 rtl_beacon_statistic(hw, skb); 462 } 463 } 464 465 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw, 466 struct sk_buff *skb) 467 { 468 struct rtl_priv *rtlpriv = rtl_priv(hw); 469 u8 *rxdesc = skb->data; 470 struct ieee80211_hdr *hdr; 471 bool unicast = false; 472 __le16 fc; 473 struct ieee80211_rx_status rx_status = {0}; 474 struct rtl_stats stats = { 475 .signal = 0, 476 .rate = 0, 477 }; 478 479 skb_pull(skb, RTL_RX_DESC_SIZE); 480 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb); 481 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift)); 482 hdr = (struct ieee80211_hdr *)(skb->data); 483 fc = hdr->frame_control; 484 if (!stats.crc) { 485 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); 486 487 if (is_broadcast_ether_addr(hdr->addr1)) { 488 /*TODO*/; 489 } else if (is_multicast_ether_addr(hdr->addr1)) { 490 /*TODO*/ 491 } else { 492 unicast = true; 493 rtlpriv->stats.rxbytesunicast += skb->len; 494 } 495 496 if (ieee80211_is_data(fc)) { 497 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX); 498 499 if (unicast) 500 rtlpriv->link_info.num_rx_inperiod++; 501 } 502 503 /* static bcn for roaming */ 504 rtl_beacon_statistic(hw, skb); 505 506 if (likely(rtl_action_proc(hw, skb, false))) 507 ieee80211_rx(hw, skb); 508 else 509 dev_kfree_skb_any(skb); 510 } else { 511 dev_kfree_skb_any(skb); 512 } 513 } 514 515 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb) 516 { 517 struct sk_buff *_skb; 518 struct sk_buff_head rx_queue; 519 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 520 521 skb_queue_head_init(&rx_queue); 522 if (rtlusb->usb_rx_segregate_hdl) 523 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue); 524 WARN_ON(skb_queue_empty(&rx_queue)); 525 while (!skb_queue_empty(&rx_queue)) { 526 _skb = skb_dequeue(&rx_queue); 527 _rtl_usb_rx_process_agg(hw, _skb); 528 ieee80211_rx(hw, _skb); 529 } 530 } 531 532 #define __RX_SKB_MAX_QUEUED 64 533 534 static void _rtl_rx_work(unsigned long param) 535 { 536 struct rtl_usb *rtlusb = (struct rtl_usb *)param; 537 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf); 538 struct sk_buff *skb; 539 540 while ((skb = skb_dequeue(&rtlusb->rx_queue))) { 541 if (unlikely(IS_USB_STOP(rtlusb))) { 542 dev_kfree_skb_any(skb); 543 continue; 544 } 545 546 if (likely(!rtlusb->usb_rx_segregate_hdl)) { 547 _rtl_usb_rx_process_noagg(hw, skb); 548 } else { 549 /* TO DO */ 550 _rtl_rx_pre_process(hw, skb); 551 pr_err("rx agg not supported\n"); 552 } 553 } 554 } 555 556 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr, 557 unsigned int len) 558 { 559 #if NET_IP_ALIGN != 0 560 unsigned int padding = 0; 561 #endif 562 563 /* make function no-op when possible */ 564 if (NET_IP_ALIGN == 0 || len < sizeof(*hdr)) 565 return 0; 566 567 #if NET_IP_ALIGN != 0 568 /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */ 569 /* TODO: deduplicate common code, define helper function instead? */ 570 571 if (ieee80211_is_data_qos(hdr->frame_control)) { 572 u8 *qc = ieee80211_get_qos_ctl(hdr); 573 574 padding ^= NET_IP_ALIGN; 575 576 /* Input might be invalid, avoid accessing memory outside 577 * the buffer. 578 */ 579 if ((unsigned long)qc - (unsigned long)hdr < len && 580 *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT) 581 padding ^= NET_IP_ALIGN; 582 } 583 584 if (ieee80211_has_a4(hdr->frame_control)) 585 padding ^= NET_IP_ALIGN; 586 587 return padding; 588 #endif 589 } 590 591 #define __RADIO_TAP_SIZE_RSV 32 592 593 static void _rtl_rx_completed(struct urb *_urb) 594 { 595 struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context; 596 int err = 0; 597 598 if (unlikely(IS_USB_STOP(rtlusb))) 599 goto free; 600 601 if (likely(0 == _urb->status)) { 602 unsigned int padding; 603 struct sk_buff *skb; 604 unsigned int qlen; 605 unsigned int size = _urb->actual_length; 606 struct ieee80211_hdr *hdr; 607 608 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) { 609 pr_err("Too short packet from bulk IN! (len: %d)\n", 610 size); 611 goto resubmit; 612 } 613 614 qlen = skb_queue_len(&rtlusb->rx_queue); 615 if (qlen >= __RX_SKB_MAX_QUEUED) { 616 pr_err("Pending RX skbuff queue full! (qlen: %d)\n", 617 qlen); 618 goto resubmit; 619 } 620 621 hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE); 622 padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE); 623 624 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding); 625 if (!skb) { 626 pr_err("Can't allocate skb for bulk IN!\n"); 627 goto resubmit; 628 } 629 630 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep); 631 632 /* Make sure the payload data is 4 byte aligned. */ 633 skb_reserve(skb, padding); 634 635 /* reserve some space for mac80211's radiotap */ 636 skb_reserve(skb, __RADIO_TAP_SIZE_RSV); 637 638 skb_put_data(skb, _urb->transfer_buffer, size); 639 640 skb_queue_tail(&rtlusb->rx_queue, skb); 641 tasklet_schedule(&rtlusb->rx_work_tasklet); 642 643 goto resubmit; 644 } 645 646 switch (_urb->status) { 647 /* disconnect */ 648 case -ENOENT: 649 case -ECONNRESET: 650 case -ENODEV: 651 case -ESHUTDOWN: 652 goto free; 653 default: 654 break; 655 } 656 657 resubmit: 658 usb_anchor_urb(_urb, &rtlusb->rx_submitted); 659 err = usb_submit_urb(_urb, GFP_ATOMIC); 660 if (unlikely(err)) { 661 usb_unanchor_urb(_urb); 662 goto free; 663 } 664 return; 665 666 free: 667 /* On some architectures, usb_free_coherent must not be called from 668 * hardirq context. Queue urb to cleanup list. 669 */ 670 usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs); 671 } 672 673 #undef __RADIO_TAP_SIZE_RSV 674 675 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw) 676 { 677 struct rtl_priv *rtlpriv = rtl_priv(hw); 678 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 679 struct urb *urb; 680 681 usb_kill_anchored_urbs(&rtlusb->rx_submitted); 682 683 tasklet_kill(&rtlusb->rx_work_tasklet); 684 cancel_work_sync(&rtlpriv->works.lps_change_work); 685 686 flush_workqueue(rtlpriv->works.rtl_wq); 687 destroy_workqueue(rtlpriv->works.rtl_wq); 688 689 skb_queue_purge(&rtlusb->rx_queue); 690 691 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) { 692 usb_free_coherent(urb->dev, urb->transfer_buffer_length, 693 urb->transfer_buffer, urb->transfer_dma); 694 usb_free_urb(urb); 695 } 696 } 697 698 static int _rtl_usb_receive(struct ieee80211_hw *hw) 699 { 700 struct urb *urb; 701 int err; 702 int i; 703 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 704 705 WARN_ON(0 == rtlusb->rx_urb_num); 706 /* 1600 == 1514 + max WLAN header + rtk info */ 707 WARN_ON(rtlusb->rx_max_size < 1600); 708 709 for (i = 0; i < rtlusb->rx_urb_num; i++) { 710 err = -ENOMEM; 711 urb = usb_alloc_urb(0, GFP_KERNEL); 712 if (!urb) 713 goto err_out; 714 715 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL); 716 if (err < 0) { 717 pr_err("Failed to prep_rx_urb!!\n"); 718 usb_free_urb(urb); 719 goto err_out; 720 } 721 722 usb_anchor_urb(urb, &rtlusb->rx_submitted); 723 err = usb_submit_urb(urb, GFP_KERNEL); 724 if (err) 725 goto err_out; 726 usb_free_urb(urb); 727 } 728 return 0; 729 730 err_out: 731 usb_kill_anchored_urbs(&rtlusb->rx_submitted); 732 _rtl_usb_cleanup_rx(hw); 733 return err; 734 } 735 736 static int rtl_usb_start(struct ieee80211_hw *hw) 737 { 738 int err; 739 struct rtl_priv *rtlpriv = rtl_priv(hw); 740 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 741 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 742 743 err = rtlpriv->cfg->ops->hw_init(hw); 744 if (!err) { 745 rtl_init_rx_config(hw); 746 747 /* Enable software */ 748 SET_USB_START(rtlusb); 749 /* should after adapter start and interrupt enable. */ 750 set_hal_start(rtlhal); 751 752 /* Start bulk IN */ 753 err = _rtl_usb_receive(hw); 754 } 755 756 return err; 757 } 758 759 /** 760 * 761 * 762 */ 763 764 /*======================= tx =========================================*/ 765 static void rtl_usb_cleanup(struct ieee80211_hw *hw) 766 { 767 u32 i; 768 struct sk_buff *_skb; 769 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 770 struct ieee80211_tx_info *txinfo; 771 772 /* clean up rx stuff. */ 773 _rtl_usb_cleanup_rx(hw); 774 775 /* clean up tx stuff */ 776 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) { 777 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) { 778 rtlusb->usb_tx_cleanup(hw, _skb); 779 txinfo = IEEE80211_SKB_CB(_skb); 780 ieee80211_tx_info_clear_status(txinfo); 781 txinfo->flags |= IEEE80211_TX_STAT_ACK; 782 ieee80211_tx_status_irqsafe(hw, _skb); 783 } 784 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]); 785 } 786 usb_kill_anchored_urbs(&rtlusb->tx_submitted); 787 } 788 789 /** 790 * 791 * We may add some struct into struct rtl_usb later. Do deinit here. 792 * 793 */ 794 static void rtl_usb_deinit(struct ieee80211_hw *hw) 795 { 796 rtl_usb_cleanup(hw); 797 } 798 799 static void rtl_usb_stop(struct ieee80211_hw *hw) 800 { 801 struct rtl_priv *rtlpriv = rtl_priv(hw); 802 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 803 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 804 struct urb *urb; 805 806 /* should after adapter start and interrupt enable. */ 807 set_hal_stop(rtlhal); 808 cancel_work_sync(&rtlpriv->works.fill_h2c_cmd); 809 /* Enable software */ 810 SET_USB_STOP(rtlusb); 811 812 /* free pre-allocated URBs from rtl_usb_start() */ 813 usb_kill_anchored_urbs(&rtlusb->rx_submitted); 814 815 tasklet_kill(&rtlusb->rx_work_tasklet); 816 cancel_work_sync(&rtlpriv->works.lps_change_work); 817 818 flush_workqueue(rtlpriv->works.rtl_wq); 819 820 skb_queue_purge(&rtlusb->rx_queue); 821 822 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) { 823 usb_free_coherent(urb->dev, urb->transfer_buffer_length, 824 urb->transfer_buffer, urb->transfer_dma); 825 usb_free_urb(urb); 826 } 827 828 rtlpriv->cfg->ops->hw_disable(hw); 829 } 830 831 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb) 832 { 833 int err; 834 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 835 836 usb_anchor_urb(_urb, &rtlusb->tx_submitted); 837 err = usb_submit_urb(_urb, GFP_ATOMIC); 838 if (err < 0) { 839 struct sk_buff *skb; 840 841 pr_err("Failed to submit urb\n"); 842 usb_unanchor_urb(_urb); 843 skb = (struct sk_buff *)_urb->context; 844 kfree_skb(skb); 845 } 846 usb_free_urb(_urb); 847 } 848 849 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb, 850 struct sk_buff *skb) 851 { 852 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 853 struct ieee80211_tx_info *txinfo; 854 855 rtlusb->usb_tx_post_hdl(hw, urb, skb); 856 skb_pull(skb, RTL_TX_HEADER_SIZE); 857 txinfo = IEEE80211_SKB_CB(skb); 858 ieee80211_tx_info_clear_status(txinfo); 859 txinfo->flags |= IEEE80211_TX_STAT_ACK; 860 861 if (urb->status) { 862 pr_err("Urb has error status 0x%X\n", urb->status); 863 goto out; 864 } 865 /* TODO: statistics */ 866 out: 867 ieee80211_tx_status_irqsafe(hw, skb); 868 return urb->status; 869 } 870 871 static void _rtl_tx_complete(struct urb *urb) 872 { 873 struct sk_buff *skb = (struct sk_buff *)urb->context; 874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 875 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0]; 876 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf); 877 int err; 878 879 if (unlikely(IS_USB_STOP(rtlusb))) 880 return; 881 err = _usb_tx_post(hw, urb, skb); 882 if (err) { 883 /* Ignore error and keep issuiing other urbs */ 884 return; 885 } 886 } 887 888 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw, 889 struct sk_buff *skb, u32 ep_num) 890 { 891 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 892 struct urb *_urb; 893 894 WARN_ON(NULL == skb); 895 _urb = usb_alloc_urb(0, GFP_ATOMIC); 896 if (!_urb) { 897 kfree_skb(skb); 898 return NULL; 899 } 900 _rtl_install_trx_info(rtlusb, skb, ep_num); 901 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev, 902 ep_num), skb->data, skb->len, _rtl_tx_complete, skb); 903 _urb->transfer_flags |= URB_ZERO_PACKET; 904 return _urb; 905 } 906 907 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb, 908 enum rtl_txq qnum) 909 { 910 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 911 u32 ep_num; 912 struct urb *_urb = NULL; 913 struct sk_buff *_skb = NULL; 914 915 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl); 916 if (unlikely(IS_USB_STOP(rtlusb))) { 917 pr_err("USB device is stopping...\n"); 918 kfree_skb(skb); 919 return; 920 } 921 ep_num = rtlusb->ep_map.ep_mapping[qnum]; 922 _skb = skb; 923 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num); 924 if (unlikely(!_urb)) { 925 pr_err("Can't allocate urb. Drop skb!\n"); 926 kfree_skb(skb); 927 return; 928 } 929 _rtl_submit_tx_urb(hw, _urb); 930 } 931 932 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, 933 struct ieee80211_sta *sta, 934 struct sk_buff *skb, 935 u16 hw_queue) 936 { 937 struct rtl_priv *rtlpriv = rtl_priv(hw); 938 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 939 struct rtl_tx_desc *pdesc = NULL; 940 struct rtl_tcb_desc tcb_desc; 941 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); 942 __le16 fc = hdr->frame_control; 943 u8 *pda_addr = hdr->addr1; 944 945 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc)); 946 if (ieee80211_is_auth(fc)) { 947 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n"); 948 } 949 950 if (rtlpriv->psc.sw_ps_enabled) { 951 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) && 952 !ieee80211_has_pm(fc)) 953 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); 954 } 955 956 rtl_action_proc(hw, skb, true); 957 if (is_multicast_ether_addr(pda_addr)) 958 rtlpriv->stats.txbytesmulticast += skb->len; 959 else if (is_broadcast_ether_addr(pda_addr)) 960 rtlpriv->stats.txbytesbroadcast += skb->len; 961 else 962 rtlpriv->stats.txbytesunicast += skb->len; 963 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, NULL, info, sta, skb, 964 hw_queue, &tcb_desc); 965 if (ieee80211_is_data(fc)) 966 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX); 967 } 968 969 static int rtl_usb_tx(struct ieee80211_hw *hw, 970 struct ieee80211_sta *sta, 971 struct sk_buff *skb, 972 struct rtl_tcb_desc *dummy) 973 { 974 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 975 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 976 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); 977 __le16 fc = hdr->frame_control; 978 u16 hw_queue; 979 980 if (unlikely(is_hal_stop(rtlhal))) 981 goto err_free; 982 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb)); 983 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue); 984 _rtl_usb_transmit(hw, skb, hw_queue); 985 return NETDEV_TX_OK; 986 987 err_free: 988 dev_kfree_skb_any(skb); 989 return NETDEV_TX_OK; 990 } 991 992 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw, 993 struct ieee80211_sta *sta, 994 struct sk_buff *skb) 995 { 996 return false; 997 } 998 999 static void rtl_fill_h2c_cmd_work_callback(struct work_struct *work) 1000 { 1001 struct rtl_works *rtlworks = 1002 container_of(work, struct rtl_works, fill_h2c_cmd); 1003 struct ieee80211_hw *hw = rtlworks->hw; 1004 struct rtl_priv *rtlpriv = rtl_priv(hw); 1005 1006 rtlpriv->cfg->ops->fill_h2c_cmd(hw, H2C_RA_MASK, 5, rtlpriv->rate_mask); 1007 } 1008 1009 static const struct rtl_intf_ops rtl_usb_ops = { 1010 .adapter_start = rtl_usb_start, 1011 .adapter_stop = rtl_usb_stop, 1012 .adapter_tx = rtl_usb_tx, 1013 .waitq_insert = rtl_usb_tx_chk_waitq_insert, 1014 }; 1015 1016 int rtl_usb_probe(struct usb_interface *intf, 1017 const struct usb_device_id *id, 1018 struct rtl_hal_cfg *rtl_hal_cfg) 1019 { 1020 int err; 1021 struct ieee80211_hw *hw = NULL; 1022 struct rtl_priv *rtlpriv = NULL; 1023 struct usb_device *udev; 1024 struct rtl_usb_priv *usb_priv; 1025 1026 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) + 1027 sizeof(struct rtl_usb_priv), &rtl_ops); 1028 if (!hw) { 1029 WARN_ONCE(true, "rtl_usb: ieee80211 alloc failed\n"); 1030 return -ENOMEM; 1031 } 1032 rtlpriv = hw->priv; 1033 rtlpriv->hw = hw; 1034 rtlpriv->usb_data = kcalloc(RTL_USB_MAX_RX_COUNT, sizeof(u32), 1035 GFP_KERNEL); 1036 if (!rtlpriv->usb_data) 1037 return -ENOMEM; 1038 1039 /* this spin lock must be initialized early */ 1040 spin_lock_init(&rtlpriv->locks.usb_lock); 1041 INIT_WORK(&rtlpriv->works.fill_h2c_cmd, 1042 rtl_fill_h2c_cmd_work_callback); 1043 INIT_WORK(&rtlpriv->works.lps_change_work, 1044 rtl_lps_change_work_callback); 1045 1046 rtlpriv->usb_data_index = 0; 1047 init_completion(&rtlpriv->firmware_loading_complete); 1048 SET_IEEE80211_DEV(hw, &intf->dev); 1049 udev = interface_to_usbdev(intf); 1050 usb_get_dev(udev); 1051 usb_priv = rtl_usbpriv(hw); 1052 memset(usb_priv, 0, sizeof(*usb_priv)); 1053 usb_priv->dev.intf = intf; 1054 usb_priv->dev.udev = udev; 1055 usb_set_intfdata(intf, hw); 1056 /* init cfg & intf_ops */ 1057 rtlpriv->rtlhal.interface = INTF_USB; 1058 rtlpriv->cfg = rtl_hal_cfg; 1059 rtlpriv->intf_ops = &rtl_usb_ops; 1060 /* Init IO handler */ 1061 _rtl_usb_io_handler_init(&udev->dev, hw); 1062 rtlpriv->cfg->ops->read_chip_version(hw); 1063 /*like read eeprom and so on */ 1064 rtlpriv->cfg->ops->read_eeprom_info(hw); 1065 err = _rtl_usb_init(hw); 1066 if (err) 1067 goto error_out2; 1068 rtl_usb_init_sw(hw); 1069 /* Init mac80211 sw */ 1070 err = rtl_init_core(hw); 1071 if (err) { 1072 pr_err("Can't allocate sw for mac80211\n"); 1073 goto error_out2; 1074 } 1075 if (rtlpriv->cfg->ops->init_sw_vars(hw)) { 1076 pr_err("Can't init_sw_vars\n"); 1077 goto error_out; 1078 } 1079 rtlpriv->cfg->ops->init_sw_leds(hw); 1080 1081 err = ieee80211_register_hw(hw); 1082 if (err) { 1083 pr_err("Can't register mac80211 hw.\n"); 1084 err = -ENODEV; 1085 goto error_out; 1086 } 1087 rtlpriv->mac80211.mac80211_registered = 1; 1088 1089 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status); 1090 return 0; 1091 1092 error_out: 1093 rtl_deinit_core(hw); 1094 error_out2: 1095 _rtl_usb_io_handler_release(hw); 1096 usb_put_dev(udev); 1097 complete(&rtlpriv->firmware_loading_complete); 1098 return -ENODEV; 1099 } 1100 EXPORT_SYMBOL(rtl_usb_probe); 1101 1102 void rtl_usb_disconnect(struct usb_interface *intf) 1103 { 1104 struct ieee80211_hw *hw = usb_get_intfdata(intf); 1105 struct rtl_priv *rtlpriv = rtl_priv(hw); 1106 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw)); 1107 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); 1108 1109 if (unlikely(!rtlpriv)) 1110 return; 1111 /* just in case driver is removed before firmware callback */ 1112 wait_for_completion(&rtlpriv->firmware_loading_complete); 1113 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status); 1114 /*ieee80211_unregister_hw will call ops_stop */ 1115 if (rtlmac->mac80211_registered == 1) { 1116 ieee80211_unregister_hw(hw); 1117 rtlmac->mac80211_registered = 0; 1118 } else { 1119 rtl_deinit_deferred_work(hw, false); 1120 rtlpriv->intf_ops->adapter_stop(hw); 1121 } 1122 /*deinit rfkill */ 1123 /* rtl_deinit_rfkill(hw); */ 1124 rtl_usb_deinit(hw); 1125 rtl_deinit_core(hw); 1126 kfree(rtlpriv->usb_data); 1127 rtlpriv->cfg->ops->deinit_sw_leds(hw); 1128 rtlpriv->cfg->ops->deinit_sw_vars(hw); 1129 _rtl_usb_io_handler_release(hw); 1130 usb_put_dev(rtlusb->udev); 1131 usb_set_intfdata(intf, NULL); 1132 ieee80211_free_hw(hw); 1133 } 1134 EXPORT_SYMBOL(rtl_usb_disconnect); 1135 1136 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message) 1137 { 1138 return 0; 1139 } 1140 EXPORT_SYMBOL(rtl_usb_suspend); 1141 1142 int rtl_usb_resume(struct usb_interface *pusb_intf) 1143 { 1144 return 0; 1145 } 1146 EXPORT_SYMBOL(rtl_usb_resume); 1147