1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2018-2019 Realtek Corporation 3 */ 4 5 #include <linux/module.h> 6 #include <linux/usb.h> 7 #include <linux/mutex.h> 8 #include "main.h" 9 #include "debug.h" 10 #include "reg.h" 11 #include "tx.h" 12 #include "rx.h" 13 #include "fw.h" 14 #include "ps.h" 15 #include "usb.h" 16 17 static bool rtw_switch_usb_mode = true; 18 module_param_named(switch_usb_mode, rtw_switch_usb_mode, bool, 0644); 19 MODULE_PARM_DESC(switch_usb_mode, 20 "Set to N to disable switching to USB 3 mode to avoid potential interference in the 2.4 GHz band (default: Y)"); 21 22 #define RTW_USB_MAX_RXQ_LEN 512 23 24 struct rtw_usb_txcb { 25 struct rtw_dev *rtwdev; 26 struct sk_buff_head tx_ack_queue; 27 }; 28 29 static void rtw_usb_fill_tx_checksum(struct rtw_usb *rtwusb, 30 struct sk_buff *skb, int agg_num) 31 { 32 struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data; 33 struct rtw_dev *rtwdev = rtwusb->rtwdev; 34 struct rtw_tx_pkt_info pkt_info; 35 36 le32p_replace_bits(&tx_desc->w7, agg_num, RTW_TX_DESC_W7_DMA_TXAGG_NUM); 37 pkt_info.pkt_offset = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_PKT_OFFSET); 38 rtw_tx_fill_txdesc_checksum(rtwdev, &pkt_info, skb->data); 39 } 40 41 static void rtw_usb_reg_sec(struct rtw_dev *rtwdev, u32 addr, __le32 *data) 42 { 43 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 44 struct usb_device *udev = rtwusb->udev; 45 bool reg_on_section = false; 46 u16 t_reg = 0x4e0; 47 u8 t_len = 1; 48 int status; 49 50 /* There are three sections: 51 * 1. on (0x00~0xFF; 0x1000~0x10FF): this section is always powered on 52 * 2. off (< 0xFE00, excluding "on" section): this section could be 53 * powered off 54 * 3. local (>= 0xFE00): usb specific registers section 55 */ 56 if (addr <= 0xff || (addr >= 0x1000 && addr <= 0x10ff)) 57 reg_on_section = true; 58 59 if (!reg_on_section) 60 return; 61 62 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 63 RTW_USB_CMD_REQ, RTW_USB_CMD_WRITE, 64 t_reg, 0, data, t_len, 500); 65 66 if (status != t_len && status != -ENODEV) 67 rtw_err(rtwdev, "%s: reg 0x%x, usb write %u fail, status: %d\n", 68 __func__, t_reg, t_len, status); 69 } 70 71 static u32 rtw_usb_read(struct rtw_dev *rtwdev, u32 addr, u16 len) 72 { 73 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 74 struct usb_device *udev = rtwusb->udev; 75 __le32 *data; 76 unsigned long flags; 77 int idx, ret; 78 static int count; 79 80 spin_lock_irqsave(&rtwusb->usb_lock, flags); 81 82 idx = rtwusb->usb_data_index; 83 rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1); 84 85 spin_unlock_irqrestore(&rtwusb->usb_lock, flags); 86 87 data = &rtwusb->usb_data[idx]; 88 89 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 90 RTW_USB_CMD_REQ, RTW_USB_CMD_READ, addr, 91 RTW_USB_VENQT_CMD_IDX, data, len, 1000); 92 if (ret < 0 && ret != -ENODEV && count++ < 4) 93 rtw_err(rtwdev, "read register 0x%x failed with %d\n", 94 addr, ret); 95 96 if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C || 97 rtwdev->chip->id == RTW_CHIP_TYPE_8822B || 98 rtwdev->chip->id == RTW_CHIP_TYPE_8821C) 99 rtw_usb_reg_sec(rtwdev, addr, data); 100 101 return le32_to_cpu(*data); 102 } 103 104 static u8 rtw_usb_read8(struct rtw_dev *rtwdev, u32 addr) 105 { 106 return (u8)rtw_usb_read(rtwdev, addr, 1); 107 } 108 109 static u16 rtw_usb_read16(struct rtw_dev *rtwdev, u32 addr) 110 { 111 return (u16)rtw_usb_read(rtwdev, addr, 2); 112 } 113 114 static u32 rtw_usb_read32(struct rtw_dev *rtwdev, u32 addr) 115 { 116 return (u32)rtw_usb_read(rtwdev, addr, 4); 117 } 118 119 static void rtw_usb_write(struct rtw_dev *rtwdev, u32 addr, u32 val, int len) 120 { 121 struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv; 122 struct usb_device *udev = rtwusb->udev; 123 unsigned long flags; 124 __le32 *data; 125 int idx, ret; 126 static int count; 127 128 spin_lock_irqsave(&rtwusb->usb_lock, flags); 129 130 idx = rtwusb->usb_data_index; 131 rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1); 132 133 spin_unlock_irqrestore(&rtwusb->usb_lock, flags); 134 135 data = &rtwusb->usb_data[idx]; 136 137 *data = cpu_to_le32(val); 138 139 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 140 RTW_USB_CMD_REQ, RTW_USB_CMD_WRITE, 141 addr, 0, data, len, 30000); 142 if (ret < 0 && ret != -ENODEV && count++ < 4) 143 rtw_err(rtwdev, "write register 0x%x failed with %d\n", 144 addr, ret); 145 146 if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C || 147 rtwdev->chip->id == RTW_CHIP_TYPE_8822B || 148 rtwdev->chip->id == RTW_CHIP_TYPE_8821C) 149 rtw_usb_reg_sec(rtwdev, addr, data); 150 } 151 152 static void rtw_usb_write8(struct rtw_dev *rtwdev, u32 addr, u8 val) 153 { 154 rtw_usb_write(rtwdev, addr, val, 1); 155 } 156 157 static void rtw_usb_write16(struct rtw_dev *rtwdev, u32 addr, u16 val) 158 { 159 rtw_usb_write(rtwdev, addr, val, 2); 160 } 161 162 static void rtw_usb_write32(struct rtw_dev *rtwdev, u32 addr, u32 val) 163 { 164 rtw_usb_write(rtwdev, addr, val, 4); 165 } 166 167 static int dma_mapping_to_ep(enum rtw_dma_mapping dma_mapping) 168 { 169 switch (dma_mapping) { 170 case RTW_DMA_MAPPING_HIGH: 171 return 0; 172 case RTW_DMA_MAPPING_NORMAL: 173 return 1; 174 case RTW_DMA_MAPPING_LOW: 175 return 2; 176 case RTW_DMA_MAPPING_EXTRA: 177 return 3; 178 default: 179 return -EINVAL; 180 } 181 } 182 183 static int rtw_usb_parse(struct rtw_dev *rtwdev, 184 struct usb_interface *interface) 185 { 186 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 187 struct usb_host_interface *host_interface = &interface->altsetting[0]; 188 struct usb_interface_descriptor *interface_desc = &host_interface->desc; 189 struct usb_endpoint_descriptor *endpoint; 190 int num_out_pipes = 0; 191 int i; 192 u8 num; 193 const struct rtw_chip_info *chip = rtwdev->chip; 194 const struct rtw_rqpn *rqpn; 195 196 for (i = 0; i < interface_desc->bNumEndpoints; i++) { 197 endpoint = &host_interface->endpoint[i].desc; 198 num = usb_endpoint_num(endpoint); 199 200 if (usb_endpoint_dir_in(endpoint) && 201 usb_endpoint_xfer_bulk(endpoint)) { 202 if (rtwusb->pipe_in) { 203 rtw_err(rtwdev, "IN pipes overflow\n"); 204 return -EINVAL; 205 } 206 207 rtwusb->pipe_in = num; 208 } 209 210 if (usb_endpoint_dir_in(endpoint) && 211 usb_endpoint_xfer_int(endpoint)) { 212 if (rtwusb->pipe_interrupt) { 213 rtw_err(rtwdev, "INT pipes overflow\n"); 214 return -EINVAL; 215 } 216 217 rtwusb->pipe_interrupt = num; 218 } 219 220 if (usb_endpoint_dir_out(endpoint) && 221 usb_endpoint_xfer_bulk(endpoint)) { 222 if (num_out_pipes >= ARRAY_SIZE(rtwusb->out_ep)) { 223 rtw_err(rtwdev, "OUT pipes overflow\n"); 224 return -EINVAL; 225 } 226 227 rtwusb->out_ep[num_out_pipes++] = num; 228 } 229 } 230 231 rtwdev->hci.bulkout_num = num_out_pipes; 232 233 if (num_out_pipes < 1 || num_out_pipes > 4) { 234 rtw_err(rtwdev, "invalid number of endpoints %d\n", num_out_pipes); 235 return -EINVAL; 236 } 237 238 rqpn = &chip->rqpn_table[num_out_pipes]; 239 240 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID0] = dma_mapping_to_ep(rqpn->dma_map_be); 241 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID1] = dma_mapping_to_ep(rqpn->dma_map_bk); 242 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID2] = dma_mapping_to_ep(rqpn->dma_map_bk); 243 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID3] = dma_mapping_to_ep(rqpn->dma_map_be); 244 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID4] = dma_mapping_to_ep(rqpn->dma_map_vi); 245 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID5] = dma_mapping_to_ep(rqpn->dma_map_vi); 246 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID6] = dma_mapping_to_ep(rqpn->dma_map_vo); 247 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID7] = dma_mapping_to_ep(rqpn->dma_map_vo); 248 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID8] = -EINVAL; 249 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID9] = -EINVAL; 250 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID10] = -EINVAL; 251 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID11] = -EINVAL; 252 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID12] = -EINVAL; 253 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID13] = -EINVAL; 254 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID14] = -EINVAL; 255 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID15] = -EINVAL; 256 rtwusb->qsel_to_ep[TX_DESC_QSEL_BEACON] = dma_mapping_to_ep(rqpn->dma_map_hi); 257 rtwusb->qsel_to_ep[TX_DESC_QSEL_HIGH] = dma_mapping_to_ep(rqpn->dma_map_hi); 258 rtwusb->qsel_to_ep[TX_DESC_QSEL_MGMT] = dma_mapping_to_ep(rqpn->dma_map_mg); 259 rtwusb->qsel_to_ep[TX_DESC_QSEL_H2C] = dma_mapping_to_ep(rqpn->dma_map_hi); 260 261 return 0; 262 } 263 264 static void rtw_usb_write_port_tx_complete(struct urb *urb) 265 { 266 struct rtw_usb_txcb *txcb = urb->context; 267 struct rtw_dev *rtwdev = txcb->rtwdev; 268 struct ieee80211_hw *hw = rtwdev->hw; 269 270 while (true) { 271 struct sk_buff *skb = skb_dequeue(&txcb->tx_ack_queue); 272 struct ieee80211_tx_info *info; 273 struct rtw_usb_tx_data *tx_data; 274 275 if (!skb) 276 break; 277 278 info = IEEE80211_SKB_CB(skb); 279 tx_data = rtw_usb_get_tx_data(skb); 280 281 skb_pull(skb, rtwdev->chip->tx_pkt_desc_sz); 282 283 /* enqueue to wait for tx report */ 284 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) { 285 rtw_tx_report_enqueue(rtwdev, skb, tx_data->sn); 286 continue; 287 } 288 289 /* always ACK for others, then they won't be marked as drop */ 290 ieee80211_tx_info_clear_status(info); 291 if (info->flags & IEEE80211_TX_CTL_NO_ACK) 292 info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED; 293 else 294 info->flags |= IEEE80211_TX_STAT_ACK; 295 296 ieee80211_tx_status_irqsafe(hw, skb); 297 } 298 299 kfree(txcb); 300 } 301 302 static int qsel_to_ep(struct rtw_usb *rtwusb, unsigned int qsel) 303 { 304 if (qsel >= ARRAY_SIZE(rtwusb->qsel_to_ep)) 305 return -EINVAL; 306 307 return rtwusb->qsel_to_ep[qsel]; 308 } 309 310 static int rtw_usb_write_port(struct rtw_dev *rtwdev, u8 qsel, struct sk_buff *skb, 311 usb_complete_t cb, void *context) 312 { 313 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 314 struct usb_device *usbd = rtwusb->udev; 315 struct urb *urb; 316 unsigned int pipe; 317 int ret; 318 int ep = qsel_to_ep(rtwusb, qsel); 319 320 if (ep < 0) 321 return ep; 322 323 pipe = usb_sndbulkpipe(usbd, rtwusb->out_ep[ep]); 324 urb = usb_alloc_urb(0, GFP_ATOMIC); 325 if (!urb) 326 return -ENOMEM; 327 328 usb_fill_bulk_urb(urb, usbd, pipe, skb->data, skb->len, cb, context); 329 urb->transfer_flags |= URB_ZERO_PACKET; 330 ret = usb_submit_urb(urb, GFP_ATOMIC); 331 332 usb_free_urb(urb); 333 334 return ret; 335 } 336 337 static bool rtw_usb_tx_agg_skb(struct rtw_usb *rtwusb, struct sk_buff_head *list) 338 { 339 struct rtw_dev *rtwdev = rtwusb->rtwdev; 340 struct rtw_tx_desc *tx_desc; 341 struct rtw_usb_txcb *txcb; 342 struct sk_buff *skb_head; 343 struct sk_buff *skb_iter; 344 int agg_num = 0; 345 unsigned int align_next = 0; 346 u8 qsel; 347 348 if (skb_queue_empty(list)) 349 return false; 350 351 txcb = kmalloc(sizeof(*txcb), GFP_ATOMIC); 352 if (!txcb) 353 return false; 354 355 txcb->rtwdev = rtwdev; 356 skb_queue_head_init(&txcb->tx_ack_queue); 357 358 skb_iter = skb_dequeue(list); 359 360 if (skb_queue_empty(list)) { 361 skb_head = skb_iter; 362 goto queue; 363 } 364 365 skb_head = dev_alloc_skb(RTW_USB_MAX_XMITBUF_SZ); 366 if (!skb_head) { 367 skb_head = skb_iter; 368 goto queue; 369 } 370 371 while (skb_iter) { 372 unsigned long flags; 373 374 skb_put(skb_head, align_next); 375 skb_put_data(skb_head, skb_iter->data, skb_iter->len); 376 377 align_next = ALIGN(skb_iter->len, 8) - skb_iter->len; 378 379 agg_num++; 380 381 skb_queue_tail(&txcb->tx_ack_queue, skb_iter); 382 383 spin_lock_irqsave(&list->lock, flags); 384 385 skb_iter = skb_peek(list); 386 387 if (skb_iter && 388 skb_iter->len + skb_head->len <= RTW_USB_MAX_XMITBUF_SZ && 389 agg_num < rtwdev->chip->usb_tx_agg_desc_num) 390 __skb_unlink(skb_iter, list); 391 else 392 skb_iter = NULL; 393 spin_unlock_irqrestore(&list->lock, flags); 394 } 395 396 if (agg_num > 1) 397 rtw_usb_fill_tx_checksum(rtwusb, skb_head, agg_num); 398 399 queue: 400 skb_queue_tail(&txcb->tx_ack_queue, skb_head); 401 tx_desc = (struct rtw_tx_desc *)skb_head->data; 402 qsel = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_QSEL); 403 404 rtw_usb_write_port(rtwdev, qsel, skb_head, rtw_usb_write_port_tx_complete, txcb); 405 406 return true; 407 } 408 409 static void rtw_usb_tx_handler(struct work_struct *work) 410 { 411 struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, tx_work); 412 int i, limit; 413 414 for (i = ARRAY_SIZE(rtwusb->tx_queue) - 1; i >= 0; i--) { 415 for (limit = 0; limit < 200; limit++) { 416 struct sk_buff_head *list = &rtwusb->tx_queue[i]; 417 418 if (!rtw_usb_tx_agg_skb(rtwusb, list)) 419 break; 420 } 421 } 422 } 423 424 static void rtw_usb_tx_queue_purge(struct rtw_usb *rtwusb) 425 { 426 int i; 427 428 for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++) 429 skb_queue_purge(&rtwusb->tx_queue[i]); 430 } 431 432 static void rtw_usb_write_port_complete(struct urb *urb) 433 { 434 struct sk_buff *skb = urb->context; 435 436 dev_kfree_skb_any(skb); 437 } 438 439 static int rtw_usb_write_data(struct rtw_dev *rtwdev, 440 struct rtw_tx_pkt_info *pkt_info, 441 u8 *buf) 442 { 443 const struct rtw_chip_info *chip = rtwdev->chip; 444 struct sk_buff *skb; 445 unsigned int size; 446 u8 qsel; 447 int ret = 0; 448 449 size = pkt_info->tx_pkt_size; 450 qsel = pkt_info->qsel; 451 452 skb = dev_alloc_skb(chip->tx_pkt_desc_sz + size); 453 if (unlikely(!skb)) 454 return -ENOMEM; 455 456 skb_reserve(skb, chip->tx_pkt_desc_sz); 457 skb_put_data(skb, buf, size); 458 skb_push(skb, chip->tx_pkt_desc_sz); 459 memset(skb->data, 0, chip->tx_pkt_desc_sz); 460 rtw_tx_fill_tx_desc(pkt_info, skb); 461 rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data); 462 463 ret = rtw_usb_write_port(rtwdev, qsel, skb, 464 rtw_usb_write_port_complete, skb); 465 if (unlikely(ret)) 466 rtw_err(rtwdev, "failed to do USB write, ret=%d\n", ret); 467 468 return ret; 469 } 470 471 static int rtw_usb_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, 472 u32 size) 473 { 474 const struct rtw_chip_info *chip = rtwdev->chip; 475 struct rtw_tx_pkt_info pkt_info = {0}; 476 477 pkt_info.tx_pkt_size = size; 478 pkt_info.qsel = TX_DESC_QSEL_BEACON; 479 pkt_info.offset = chip->tx_pkt_desc_sz; 480 481 return rtw_usb_write_data(rtwdev, &pkt_info, buf); 482 } 483 484 static int rtw_usb_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size) 485 { 486 struct rtw_tx_pkt_info pkt_info = {0}; 487 488 pkt_info.tx_pkt_size = size; 489 pkt_info.qsel = TX_DESC_QSEL_H2C; 490 491 return rtw_usb_write_data(rtwdev, &pkt_info, buf); 492 } 493 494 static u8 rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff *skb) 495 { 496 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 497 __le16 fc = hdr->frame_control; 498 u8 qsel; 499 500 if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))) 501 qsel = TX_DESC_QSEL_MGMT; 502 else if (is_broadcast_ether_addr(hdr->addr1) || 503 is_multicast_ether_addr(hdr->addr1)) 504 qsel = TX_DESC_QSEL_HIGH; 505 else if (skb_get_queue_mapping(skb) <= IEEE80211_AC_BK) 506 qsel = skb->priority; 507 else 508 qsel = TX_DESC_QSEL_BEACON; 509 510 return qsel; 511 } 512 513 static int rtw_usb_tx_write(struct rtw_dev *rtwdev, 514 struct rtw_tx_pkt_info *pkt_info, 515 struct sk_buff *skb) 516 { 517 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 518 const struct rtw_chip_info *chip = rtwdev->chip; 519 struct rtw_usb_tx_data *tx_data; 520 u8 *pkt_desc; 521 int ep; 522 523 pkt_info->qsel = rtw_usb_tx_queue_mapping_to_qsel(skb); 524 pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz); 525 memset(pkt_desc, 0, chip->tx_pkt_desc_sz); 526 ep = qsel_to_ep(rtwusb, pkt_info->qsel); 527 rtw_tx_fill_tx_desc(pkt_info, skb); 528 rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data); 529 tx_data = rtw_usb_get_tx_data(skb); 530 tx_data->sn = pkt_info->sn; 531 532 skb_queue_tail(&rtwusb->tx_queue[ep], skb); 533 534 return 0; 535 } 536 537 static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev) 538 { 539 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 540 541 queue_work(rtwusb->txwq, &rtwusb->tx_work); 542 } 543 544 static void rtw_usb_rx_handler(struct work_struct *work) 545 { 546 struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work); 547 struct rtw_dev *rtwdev = rtwusb->rtwdev; 548 const struct rtw_chip_info *chip = rtwdev->chip; 549 u32 pkt_desc_sz = chip->rx_pkt_desc_sz; 550 struct ieee80211_rx_status rx_status; 551 u32 pkt_offset, next_pkt, urb_len; 552 struct rtw_rx_pkt_stat pkt_stat; 553 struct sk_buff *next_skb; 554 struct sk_buff *skb; 555 u8 *rx_desc; 556 int limit; 557 558 for (limit = 0; limit < 200; limit++) { 559 skb = skb_dequeue(&rtwusb->rx_queue); 560 if (!skb) 561 break; 562 563 if (skb_queue_len(&rtwusb->rx_queue) >= RTW_USB_MAX_RXQ_LEN) { 564 dev_dbg_ratelimited(rtwdev->dev, "failed to get rx_queue, overflow\n"); 565 dev_kfree_skb_any(skb); 566 continue; 567 } 568 569 urb_len = skb->len; 570 571 do { 572 rx_desc = skb->data; 573 chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat, 574 &rx_status); 575 pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + 576 pkt_stat.shift; 577 578 next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8); 579 580 if (urb_len >= next_pkt + pkt_desc_sz) 581 next_skb = skb_clone(skb, GFP_KERNEL); 582 else 583 next_skb = NULL; 584 585 if (pkt_stat.is_c2h) { 586 skb_trim(skb, pkt_stat.pkt_len + pkt_offset); 587 rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb); 588 } else { 589 skb_pull(skb, pkt_offset); 590 skb_trim(skb, pkt_stat.pkt_len); 591 rtw_update_rx_freq_for_invalid(rtwdev, skb, 592 &rx_status, 593 &pkt_stat); 594 rtw_rx_stats(rtwdev, pkt_stat.vif, skb); 595 memcpy(skb->cb, &rx_status, sizeof(rx_status)); 596 ieee80211_rx_irqsafe(rtwdev->hw, skb); 597 } 598 599 skb = next_skb; 600 if (skb) 601 skb_pull(skb, next_pkt); 602 603 urb_len -= next_pkt; 604 } while (skb); 605 } 606 } 607 608 static void rtw_usb_read_port_complete(struct urb *urb); 609 610 static void rtw_usb_rx_resubmit(struct rtw_usb *rtwusb, struct rx_usb_ctrl_block *rxcb) 611 { 612 struct rtw_dev *rtwdev = rtwusb->rtwdev; 613 int error; 614 615 rxcb->rx_skb = alloc_skb(RTW_USB_MAX_RECVBUF_SZ, GFP_ATOMIC); 616 if (!rxcb->rx_skb) 617 return; 618 619 usb_fill_bulk_urb(rxcb->rx_urb, rtwusb->udev, 620 usb_rcvbulkpipe(rtwusb->udev, rtwusb->pipe_in), 621 rxcb->rx_skb->data, RTW_USB_MAX_RECVBUF_SZ, 622 rtw_usb_read_port_complete, rxcb); 623 624 error = usb_submit_urb(rxcb->rx_urb, GFP_ATOMIC); 625 if (error) { 626 kfree_skb(rxcb->rx_skb); 627 if (error != -ENODEV) 628 rtw_err(rtwdev, "Err sending rx data urb %d\n", 629 error); 630 } 631 } 632 633 static void rtw_usb_read_port_complete(struct urb *urb) 634 { 635 struct rx_usb_ctrl_block *rxcb = urb->context; 636 struct rtw_dev *rtwdev = rxcb->rtwdev; 637 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 638 struct sk_buff *skb = rxcb->rx_skb; 639 640 if (urb->status == 0) { 641 if (urb->actual_length >= RTW_USB_MAX_RECVBUF_SZ || 642 urb->actual_length < 24) { 643 rtw_err(rtwdev, "failed to get urb length:%d\n", 644 urb->actual_length); 645 if (skb) 646 dev_kfree_skb_any(skb); 647 } else { 648 skb_put(skb, urb->actual_length); 649 skb_queue_tail(&rtwusb->rx_queue, skb); 650 queue_work(rtwusb->rxwq, &rtwusb->rx_work); 651 } 652 rtw_usb_rx_resubmit(rtwusb, rxcb); 653 } else { 654 switch (urb->status) { 655 case -EINVAL: 656 case -EPIPE: 657 case -ENODEV: 658 case -ESHUTDOWN: 659 case -ENOENT: 660 case -EPROTO: 661 case -EILSEQ: 662 case -ETIME: 663 case -ECOMM: 664 case -EOVERFLOW: 665 case -EINPROGRESS: 666 break; 667 default: 668 rtw_err(rtwdev, "status %d\n", urb->status); 669 break; 670 } 671 if (skb) 672 dev_kfree_skb_any(skb); 673 } 674 } 675 676 static void rtw_usb_cancel_rx_bufs(struct rtw_usb *rtwusb) 677 { 678 struct rx_usb_ctrl_block *rxcb; 679 int i; 680 681 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 682 rxcb = &rtwusb->rx_cb[i]; 683 usb_kill_urb(rxcb->rx_urb); 684 } 685 } 686 687 static void rtw_usb_free_rx_bufs(struct rtw_usb *rtwusb) 688 { 689 struct rx_usb_ctrl_block *rxcb; 690 int i; 691 692 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 693 rxcb = &rtwusb->rx_cb[i]; 694 usb_kill_urb(rxcb->rx_urb); 695 usb_free_urb(rxcb->rx_urb); 696 } 697 } 698 699 static int rtw_usb_alloc_rx_bufs(struct rtw_usb *rtwusb) 700 { 701 int i; 702 703 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 704 struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i]; 705 706 rxcb->rtwdev = rtwusb->rtwdev; 707 rxcb->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 708 if (!rxcb->rx_urb) 709 goto err; 710 } 711 712 return 0; 713 err: 714 rtw_usb_free_rx_bufs(rtwusb); 715 return -ENOMEM; 716 } 717 718 static int rtw_usb_setup(struct rtw_dev *rtwdev) 719 { 720 /* empty function for rtw_hci_ops */ 721 return 0; 722 } 723 724 static int rtw_usb_start(struct rtw_dev *rtwdev) 725 { 726 return 0; 727 } 728 729 static void rtw_usb_stop(struct rtw_dev *rtwdev) 730 { 731 } 732 733 static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter) 734 { 735 /* empty function for rtw_hci_ops */ 736 } 737 738 static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter) 739 { 740 /* empty function for rtw_hci_ops */ 741 } 742 743 static void rtw_usb_init_burst_pkt_len(struct rtw_dev *rtwdev) 744 { 745 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 746 enum usb_device_speed speed = rtwusb->udev->speed; 747 u8 rxdma, burst_size; 748 749 rxdma = BIT_DMA_BURST_CNT | BIT_DMA_MODE; 750 751 if (speed == USB_SPEED_SUPER) 752 burst_size = BIT_DMA_BURST_SIZE_1024; 753 else if (speed == USB_SPEED_HIGH) 754 burst_size = BIT_DMA_BURST_SIZE_512; 755 else 756 burst_size = BIT_DMA_BURST_SIZE_64; 757 758 u8p_replace_bits(&rxdma, burst_size, BIT_DMA_BURST_SIZE); 759 760 rtw_write8(rtwdev, REG_RXDMA_MODE, rxdma); 761 rtw_write16_set(rtwdev, REG_TXDMA_OFFSET_CHK, BIT_DROP_DATA_EN); 762 } 763 764 static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev) 765 { 766 rtw_usb_init_burst_pkt_len(rtwdev); 767 } 768 769 static void rtw_usb_dynamic_rx_agg_v1(struct rtw_dev *rtwdev, bool enable) 770 { 771 u8 size, timeout; 772 u16 val16; 773 774 rtw_write32_set(rtwdev, REG_RXDMA_AGG_PG_TH, BIT_EN_PRE_CALC); 775 rtw_write8_set(rtwdev, REG_TXDMA_PQ_MAP, BIT_RXDMA_AGG_EN); 776 rtw_write8_clr(rtwdev, REG_RXDMA_AGG_PG_TH + 3, BIT(7)); 777 778 if (enable) { 779 size = 0x5; 780 timeout = 0x20; 781 } else { 782 size = 0x0; 783 timeout = 0x1; 784 } 785 val16 = u16_encode_bits(size, BIT_RXDMA_AGG_PG_TH) | 786 u16_encode_bits(timeout, BIT_DMA_AGG_TO_V1); 787 788 rtw_write16(rtwdev, REG_RXDMA_AGG_PG_TH, val16); 789 } 790 791 static void rtw_usb_dynamic_rx_agg(struct rtw_dev *rtwdev, bool enable) 792 { 793 switch (rtwdev->chip->id) { 794 case RTW_CHIP_TYPE_8822C: 795 case RTW_CHIP_TYPE_8822B: 796 case RTW_CHIP_TYPE_8821C: 797 rtw_usb_dynamic_rx_agg_v1(rtwdev, enable); 798 break; 799 case RTW_CHIP_TYPE_8723D: 800 /* Doesn't like aggregation. */ 801 break; 802 case RTW_CHIP_TYPE_8703B: 803 /* Likely not found in USB devices. */ 804 break; 805 } 806 } 807 808 static struct rtw_hci_ops rtw_usb_ops = { 809 .tx_write = rtw_usb_tx_write, 810 .tx_kick_off = rtw_usb_tx_kick_off, 811 .setup = rtw_usb_setup, 812 .start = rtw_usb_start, 813 .stop = rtw_usb_stop, 814 .deep_ps = rtw_usb_deep_ps, 815 .link_ps = rtw_usb_link_ps, 816 .interface_cfg = rtw_usb_interface_cfg, 817 .dynamic_rx_agg = rtw_usb_dynamic_rx_agg, 818 819 .write8 = rtw_usb_write8, 820 .write16 = rtw_usb_write16, 821 .write32 = rtw_usb_write32, 822 .read8 = rtw_usb_read8, 823 .read16 = rtw_usb_read16, 824 .read32 = rtw_usb_read32, 825 826 .write_data_rsvd_page = rtw_usb_write_data_rsvd_page, 827 .write_data_h2c = rtw_usb_write_data_h2c, 828 }; 829 830 static int rtw_usb_init_rx(struct rtw_dev *rtwdev) 831 { 832 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 833 834 rtwusb->rxwq = create_singlethread_workqueue("rtw88_usb: rx wq"); 835 if (!rtwusb->rxwq) { 836 rtw_err(rtwdev, "failed to create RX work queue\n"); 837 return -ENOMEM; 838 } 839 840 skb_queue_head_init(&rtwusb->rx_queue); 841 842 INIT_WORK(&rtwusb->rx_work, rtw_usb_rx_handler); 843 844 return 0; 845 } 846 847 static void rtw_usb_setup_rx(struct rtw_dev *rtwdev) 848 { 849 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 850 int i; 851 852 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 853 struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i]; 854 855 rtw_usb_rx_resubmit(rtwusb, rxcb); 856 } 857 } 858 859 static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev) 860 { 861 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 862 863 skb_queue_purge(&rtwusb->rx_queue); 864 865 flush_workqueue(rtwusb->rxwq); 866 destroy_workqueue(rtwusb->rxwq); 867 } 868 869 static int rtw_usb_init_tx(struct rtw_dev *rtwdev) 870 { 871 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 872 int i; 873 874 rtwusb->txwq = create_singlethread_workqueue("rtw88_usb: tx wq"); 875 if (!rtwusb->txwq) { 876 rtw_err(rtwdev, "failed to create TX work queue\n"); 877 return -ENOMEM; 878 } 879 880 for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++) 881 skb_queue_head_init(&rtwusb->tx_queue[i]); 882 883 INIT_WORK(&rtwusb->tx_work, rtw_usb_tx_handler); 884 885 return 0; 886 } 887 888 static void rtw_usb_deinit_tx(struct rtw_dev *rtwdev) 889 { 890 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 891 892 rtw_usb_tx_queue_purge(rtwusb); 893 flush_workqueue(rtwusb->txwq); 894 destroy_workqueue(rtwusb->txwq); 895 } 896 897 static int rtw_usb_intf_init(struct rtw_dev *rtwdev, 898 struct usb_interface *intf) 899 { 900 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 901 struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf)); 902 int ret; 903 904 rtwusb->udev = udev; 905 ret = rtw_usb_parse(rtwdev, intf); 906 if (ret) 907 return ret; 908 909 rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32), 910 GFP_KERNEL); 911 if (!rtwusb->usb_data) 912 return -ENOMEM; 913 914 usb_set_intfdata(intf, rtwdev->hw); 915 916 SET_IEEE80211_DEV(rtwdev->hw, &intf->dev); 917 spin_lock_init(&rtwusb->usb_lock); 918 919 return 0; 920 } 921 922 static void rtw_usb_intf_deinit(struct rtw_dev *rtwdev, 923 struct usb_interface *intf) 924 { 925 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 926 927 usb_put_dev(rtwusb->udev); 928 kfree(rtwusb->usb_data); 929 usb_set_intfdata(intf, NULL); 930 } 931 932 static int rtw_usb_switch_mode_new(struct rtw_dev *rtwdev) 933 { 934 enum usb_device_speed cur_speed; 935 u8 id = rtwdev->chip->id; 936 bool can_switch; 937 u32 pad_ctrl2; 938 939 if (rtw_read8(rtwdev, REG_SYS_CFG2 + 3) == 0x20) 940 cur_speed = USB_SPEED_SUPER; 941 else 942 cur_speed = USB_SPEED_HIGH; 943 944 if (cur_speed == USB_SPEED_SUPER) 945 return 0; 946 947 pad_ctrl2 = rtw_read32(rtwdev, REG_PAD_CTRL2); 948 949 can_switch = !!(pad_ctrl2 & (BIT_MASK_USB23_SW_MODE_V1 | 950 BIT_USB3_USB2_TRANSITION)); 951 952 if (!can_switch) { 953 rtw_dbg(rtwdev, RTW_DBG_USB, 954 "Switching to USB 3 mode unsupported by the chip\n"); 955 return 0; 956 } 957 958 /* At this point cur_speed is USB_SPEED_HIGH. If we already tried 959 * to switch don't try again - it's a USB 2 port. 960 */ 961 if (u32_get_bits(pad_ctrl2, BIT_MASK_USB23_SW_MODE_V1) == BIT_USB_MODE_U3) 962 return 0; 963 964 /* Enable IO wrapper timeout */ 965 if (id == RTW_CHIP_TYPE_8822B || id == RTW_CHIP_TYPE_8821C) 966 rtw_write8_clr(rtwdev, REG_SW_MDIO + 3, BIT(0)); 967 968 u32p_replace_bits(&pad_ctrl2, BIT_USB_MODE_U3, BIT_MASK_USB23_SW_MODE_V1); 969 pad_ctrl2 |= BIT_RSM_EN_V1; 970 971 rtw_write32(rtwdev, REG_PAD_CTRL2, pad_ctrl2); 972 rtw_write8(rtwdev, REG_PAD_CTRL2 + 1, 4); 973 974 rtw_write16_set(rtwdev, REG_SYS_PW_CTRL, BIT_APFM_OFFMAC); 975 usleep_range(1000, 1001); 976 rtw_write32_set(rtwdev, REG_PAD_CTRL2, BIT_NO_PDN_CHIPOFF_V1); 977 978 return 1; 979 } 980 981 static int rtw_usb_switch_mode(struct rtw_dev *rtwdev) 982 { 983 u8 id = rtwdev->chip->id; 984 985 if (id != RTW_CHIP_TYPE_8822C && id != RTW_CHIP_TYPE_8822B) 986 return 0; 987 988 if (!rtwdev->efuse.usb_mode_switch) { 989 rtw_dbg(rtwdev, RTW_DBG_USB, 990 "Switching to USB 3 mode disabled by chip's efuse\n"); 991 return 0; 992 } 993 994 if (!rtw_switch_usb_mode) { 995 rtw_dbg(rtwdev, RTW_DBG_USB, 996 "Switching to USB 3 mode disabled by module parameter\n"); 997 return 0; 998 } 999 1000 return rtw_usb_switch_mode_new(rtwdev); 1001 } 1002 1003 int rtw_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 1004 { 1005 struct rtw_dev *rtwdev; 1006 struct ieee80211_hw *hw; 1007 struct rtw_usb *rtwusb; 1008 int drv_data_size; 1009 int ret; 1010 1011 drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_usb); 1012 hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops); 1013 if (!hw) 1014 return -ENOMEM; 1015 1016 rtwdev = hw->priv; 1017 rtwdev->hw = hw; 1018 rtwdev->dev = &intf->dev; 1019 rtwdev->chip = (struct rtw_chip_info *)id->driver_info; 1020 rtwdev->hci.ops = &rtw_usb_ops; 1021 rtwdev->hci.type = RTW_HCI_TYPE_USB; 1022 1023 rtwusb = rtw_get_usb_priv(rtwdev); 1024 rtwusb->rtwdev = rtwdev; 1025 1026 ret = rtw_usb_alloc_rx_bufs(rtwusb); 1027 if (ret) 1028 goto err_release_hw; 1029 1030 ret = rtw_core_init(rtwdev); 1031 if (ret) 1032 goto err_free_rx_bufs; 1033 1034 ret = rtw_usb_intf_init(rtwdev, intf); 1035 if (ret) { 1036 rtw_err(rtwdev, "failed to init USB interface\n"); 1037 goto err_deinit_core; 1038 } 1039 1040 ret = rtw_usb_init_tx(rtwdev); 1041 if (ret) { 1042 rtw_err(rtwdev, "failed to init USB TX\n"); 1043 goto err_destroy_usb; 1044 } 1045 1046 ret = rtw_usb_init_rx(rtwdev); 1047 if (ret) { 1048 rtw_err(rtwdev, "failed to init USB RX\n"); 1049 goto err_destroy_txwq; 1050 } 1051 1052 ret = rtw_chip_info_setup(rtwdev); 1053 if (ret) { 1054 rtw_err(rtwdev, "failed to setup chip information\n"); 1055 goto err_destroy_rxwq; 1056 } 1057 1058 ret = rtw_usb_switch_mode(rtwdev); 1059 if (ret) { 1060 /* Not a fail, but we do need to skip rtw_register_hw. */ 1061 rtw_dbg(rtwdev, RTW_DBG_USB, "switching to USB 3 mode\n"); 1062 ret = 0; 1063 goto err_destroy_rxwq; 1064 } 1065 1066 ret = rtw_register_hw(rtwdev, rtwdev->hw); 1067 if (ret) { 1068 rtw_err(rtwdev, "failed to register hw\n"); 1069 goto err_destroy_rxwq; 1070 } 1071 1072 rtw_usb_setup_rx(rtwdev); 1073 1074 return 0; 1075 1076 err_destroy_rxwq: 1077 rtw_usb_deinit_rx(rtwdev); 1078 1079 err_destroy_txwq: 1080 rtw_usb_deinit_tx(rtwdev); 1081 1082 err_destroy_usb: 1083 rtw_usb_intf_deinit(rtwdev, intf); 1084 1085 err_deinit_core: 1086 rtw_core_deinit(rtwdev); 1087 1088 err_free_rx_bufs: 1089 rtw_usb_free_rx_bufs(rtwusb); 1090 1091 err_release_hw: 1092 ieee80211_free_hw(hw); 1093 1094 return ret; 1095 } 1096 EXPORT_SYMBOL(rtw_usb_probe); 1097 1098 void rtw_usb_disconnect(struct usb_interface *intf) 1099 { 1100 struct ieee80211_hw *hw = usb_get_intfdata(intf); 1101 struct rtw_dev *rtwdev; 1102 struct rtw_usb *rtwusb; 1103 1104 if (!hw) 1105 return; 1106 1107 rtwdev = hw->priv; 1108 rtwusb = rtw_get_usb_priv(rtwdev); 1109 1110 rtw_usb_cancel_rx_bufs(rtwusb); 1111 1112 rtw_unregister_hw(rtwdev, hw); 1113 rtw_usb_deinit_tx(rtwdev); 1114 rtw_usb_deinit_rx(rtwdev); 1115 1116 if (rtwusb->udev->state != USB_STATE_NOTATTACHED) 1117 usb_reset_device(rtwusb->udev); 1118 1119 rtw_usb_free_rx_bufs(rtwusb); 1120 1121 rtw_usb_intf_deinit(rtwdev, intf); 1122 rtw_core_deinit(rtwdev); 1123 ieee80211_free_hw(hw); 1124 } 1125 EXPORT_SYMBOL(rtw_usb_disconnect); 1126 1127 MODULE_AUTHOR("Realtek Corporation"); 1128 MODULE_DESCRIPTION("Realtek USB 802.11ac wireless driver"); 1129 MODULE_LICENSE("Dual BSD/GPL"); 1130