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 #define RTW_USB_MAX_RXQ_LEN 512 18 19 struct rtw_usb_txcb { 20 struct rtw_dev *rtwdev; 21 struct sk_buff_head tx_ack_queue; 22 }; 23 24 static void rtw_usb_fill_tx_checksum(struct rtw_usb *rtwusb, 25 struct sk_buff *skb, int agg_num) 26 { 27 struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data; 28 struct rtw_dev *rtwdev = rtwusb->rtwdev; 29 struct rtw_tx_pkt_info pkt_info; 30 31 le32p_replace_bits(&tx_desc->w7, agg_num, RTW_TX_DESC_W7_DMA_TXAGG_NUM); 32 pkt_info.pkt_offset = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_PKT_OFFSET); 33 rtw_tx_fill_txdesc_checksum(rtwdev, &pkt_info, skb->data); 34 } 35 36 static u32 rtw_usb_read(struct rtw_dev *rtwdev, u32 addr, u16 len) 37 { 38 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 39 struct usb_device *udev = rtwusb->udev; 40 __le32 *data; 41 unsigned long flags; 42 int idx, ret; 43 static int count; 44 45 spin_lock_irqsave(&rtwusb->usb_lock, flags); 46 47 idx = rtwusb->usb_data_index; 48 rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1); 49 50 spin_unlock_irqrestore(&rtwusb->usb_lock, flags); 51 52 data = &rtwusb->usb_data[idx]; 53 54 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 55 RTW_USB_CMD_REQ, RTW_USB_CMD_READ, addr, 56 RTW_USB_VENQT_CMD_IDX, data, len, 1000); 57 if (ret < 0 && ret != -ENODEV && count++ < 4) 58 rtw_err(rtwdev, "read register 0x%x failed with %d\n", 59 addr, ret); 60 61 return le32_to_cpu(*data); 62 } 63 64 static u8 rtw_usb_read8(struct rtw_dev *rtwdev, u32 addr) 65 { 66 return (u8)rtw_usb_read(rtwdev, addr, 1); 67 } 68 69 static u16 rtw_usb_read16(struct rtw_dev *rtwdev, u32 addr) 70 { 71 return (u16)rtw_usb_read(rtwdev, addr, 2); 72 } 73 74 static u32 rtw_usb_read32(struct rtw_dev *rtwdev, u32 addr) 75 { 76 return (u32)rtw_usb_read(rtwdev, addr, 4); 77 } 78 79 static void rtw_usb_write(struct rtw_dev *rtwdev, u32 addr, u32 val, int len) 80 { 81 struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv; 82 struct usb_device *udev = rtwusb->udev; 83 unsigned long flags; 84 __le32 *data; 85 int idx, ret; 86 static int count; 87 88 spin_lock_irqsave(&rtwusb->usb_lock, flags); 89 90 idx = rtwusb->usb_data_index; 91 rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1); 92 93 spin_unlock_irqrestore(&rtwusb->usb_lock, flags); 94 95 data = &rtwusb->usb_data[idx]; 96 97 *data = cpu_to_le32(val); 98 99 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 100 RTW_USB_CMD_REQ, RTW_USB_CMD_WRITE, 101 addr, 0, data, len, 30000); 102 if (ret < 0 && ret != -ENODEV && count++ < 4) 103 rtw_err(rtwdev, "write register 0x%x failed with %d\n", 104 addr, ret); 105 } 106 107 static void rtw_usb_write8(struct rtw_dev *rtwdev, u32 addr, u8 val) 108 { 109 rtw_usb_write(rtwdev, addr, val, 1); 110 } 111 112 static void rtw_usb_write16(struct rtw_dev *rtwdev, u32 addr, u16 val) 113 { 114 rtw_usb_write(rtwdev, addr, val, 2); 115 } 116 117 static void rtw_usb_write32(struct rtw_dev *rtwdev, u32 addr, u32 val) 118 { 119 rtw_usb_write(rtwdev, addr, val, 4); 120 } 121 122 static int dma_mapping_to_ep(enum rtw_dma_mapping dma_mapping) 123 { 124 switch (dma_mapping) { 125 case RTW_DMA_MAPPING_HIGH: 126 return 0; 127 case RTW_DMA_MAPPING_NORMAL: 128 return 1; 129 case RTW_DMA_MAPPING_LOW: 130 return 2; 131 case RTW_DMA_MAPPING_EXTRA: 132 return 3; 133 default: 134 return -EINVAL; 135 } 136 } 137 138 static int rtw_usb_parse(struct rtw_dev *rtwdev, 139 struct usb_interface *interface) 140 { 141 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 142 struct usb_host_interface *host_interface = &interface->altsetting[0]; 143 struct usb_interface_descriptor *interface_desc = &host_interface->desc; 144 struct usb_endpoint_descriptor *endpoint; 145 int num_out_pipes = 0; 146 int i; 147 u8 num; 148 const struct rtw_chip_info *chip = rtwdev->chip; 149 const struct rtw_rqpn *rqpn; 150 151 for (i = 0; i < interface_desc->bNumEndpoints; i++) { 152 endpoint = &host_interface->endpoint[i].desc; 153 num = usb_endpoint_num(endpoint); 154 155 if (usb_endpoint_dir_in(endpoint) && 156 usb_endpoint_xfer_bulk(endpoint)) { 157 if (rtwusb->pipe_in) { 158 rtw_err(rtwdev, "IN pipes overflow\n"); 159 return -EINVAL; 160 } 161 162 rtwusb->pipe_in = num; 163 } 164 165 if (usb_endpoint_dir_in(endpoint) && 166 usb_endpoint_xfer_int(endpoint)) { 167 if (rtwusb->pipe_interrupt) { 168 rtw_err(rtwdev, "INT pipes overflow\n"); 169 return -EINVAL; 170 } 171 172 rtwusb->pipe_interrupt = num; 173 } 174 175 if (usb_endpoint_dir_out(endpoint) && 176 usb_endpoint_xfer_bulk(endpoint)) { 177 if (num_out_pipes >= ARRAY_SIZE(rtwusb->out_ep)) { 178 rtw_err(rtwdev, "OUT pipes overflow\n"); 179 return -EINVAL; 180 } 181 182 rtwusb->out_ep[num_out_pipes++] = num; 183 } 184 } 185 186 rtwdev->hci.bulkout_num = num_out_pipes; 187 188 if (num_out_pipes < 1 || num_out_pipes > 4) { 189 rtw_err(rtwdev, "invalid number of endpoints %d\n", num_out_pipes); 190 return -EINVAL; 191 } 192 193 rqpn = &chip->rqpn_table[num_out_pipes]; 194 195 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID0] = dma_mapping_to_ep(rqpn->dma_map_be); 196 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID1] = dma_mapping_to_ep(rqpn->dma_map_bk); 197 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID2] = dma_mapping_to_ep(rqpn->dma_map_bk); 198 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID3] = dma_mapping_to_ep(rqpn->dma_map_be); 199 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID4] = dma_mapping_to_ep(rqpn->dma_map_vi); 200 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID5] = dma_mapping_to_ep(rqpn->dma_map_vi); 201 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID6] = dma_mapping_to_ep(rqpn->dma_map_vo); 202 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID7] = dma_mapping_to_ep(rqpn->dma_map_vo); 203 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID8] = -EINVAL; 204 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID9] = -EINVAL; 205 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID10] = -EINVAL; 206 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID11] = -EINVAL; 207 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID12] = -EINVAL; 208 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID13] = -EINVAL; 209 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID14] = -EINVAL; 210 rtwusb->qsel_to_ep[TX_DESC_QSEL_TID15] = -EINVAL; 211 rtwusb->qsel_to_ep[TX_DESC_QSEL_BEACON] = dma_mapping_to_ep(rqpn->dma_map_hi); 212 rtwusb->qsel_to_ep[TX_DESC_QSEL_HIGH] = dma_mapping_to_ep(rqpn->dma_map_hi); 213 rtwusb->qsel_to_ep[TX_DESC_QSEL_MGMT] = dma_mapping_to_ep(rqpn->dma_map_mg); 214 rtwusb->qsel_to_ep[TX_DESC_QSEL_H2C] = dma_mapping_to_ep(rqpn->dma_map_hi); 215 216 return 0; 217 } 218 219 static void rtw_usb_write_port_tx_complete(struct urb *urb) 220 { 221 struct rtw_usb_txcb *txcb = urb->context; 222 struct rtw_dev *rtwdev = txcb->rtwdev; 223 struct ieee80211_hw *hw = rtwdev->hw; 224 225 while (true) { 226 struct sk_buff *skb = skb_dequeue(&txcb->tx_ack_queue); 227 struct ieee80211_tx_info *info; 228 struct rtw_usb_tx_data *tx_data; 229 230 if (!skb) 231 break; 232 233 info = IEEE80211_SKB_CB(skb); 234 tx_data = rtw_usb_get_tx_data(skb); 235 236 /* enqueue to wait for tx report */ 237 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) { 238 rtw_tx_report_enqueue(rtwdev, skb, tx_data->sn); 239 continue; 240 } 241 242 /* always ACK for others, then they won't be marked as drop */ 243 ieee80211_tx_info_clear_status(info); 244 if (info->flags & IEEE80211_TX_CTL_NO_ACK) 245 info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED; 246 else 247 info->flags |= IEEE80211_TX_STAT_ACK; 248 249 ieee80211_tx_status_irqsafe(hw, skb); 250 } 251 252 kfree(txcb); 253 } 254 255 static int qsel_to_ep(struct rtw_usb *rtwusb, unsigned int qsel) 256 { 257 if (qsel >= ARRAY_SIZE(rtwusb->qsel_to_ep)) 258 return -EINVAL; 259 260 return rtwusb->qsel_to_ep[qsel]; 261 } 262 263 static int rtw_usb_write_port(struct rtw_dev *rtwdev, u8 qsel, struct sk_buff *skb, 264 usb_complete_t cb, void *context) 265 { 266 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 267 struct usb_device *usbd = rtwusb->udev; 268 struct urb *urb; 269 unsigned int pipe; 270 int ret; 271 int ep = qsel_to_ep(rtwusb, qsel); 272 273 if (ep < 0) 274 return ep; 275 276 pipe = usb_sndbulkpipe(usbd, rtwusb->out_ep[ep]); 277 urb = usb_alloc_urb(0, GFP_ATOMIC); 278 if (!urb) 279 return -ENOMEM; 280 281 usb_fill_bulk_urb(urb, usbd, pipe, skb->data, skb->len, cb, context); 282 urb->transfer_flags |= URB_ZERO_PACKET; 283 ret = usb_submit_urb(urb, GFP_ATOMIC); 284 285 usb_free_urb(urb); 286 287 return ret; 288 } 289 290 static bool rtw_usb_tx_agg_skb(struct rtw_usb *rtwusb, struct sk_buff_head *list) 291 { 292 struct rtw_dev *rtwdev = rtwusb->rtwdev; 293 struct rtw_tx_desc *tx_desc; 294 struct rtw_usb_txcb *txcb; 295 struct sk_buff *skb_head; 296 struct sk_buff *skb_iter; 297 int agg_num = 0; 298 unsigned int align_next = 0; 299 u8 qsel; 300 301 if (skb_queue_empty(list)) 302 return false; 303 304 txcb = kmalloc(sizeof(*txcb), GFP_ATOMIC); 305 if (!txcb) 306 return false; 307 308 txcb->rtwdev = rtwdev; 309 skb_queue_head_init(&txcb->tx_ack_queue); 310 311 skb_iter = skb_dequeue(list); 312 313 if (skb_queue_empty(list)) { 314 skb_head = skb_iter; 315 goto queue; 316 } 317 318 skb_head = dev_alloc_skb(RTW_USB_MAX_XMITBUF_SZ); 319 if (!skb_head) { 320 skb_head = skb_iter; 321 goto queue; 322 } 323 324 while (skb_iter) { 325 unsigned long flags; 326 327 skb_put(skb_head, align_next); 328 skb_put_data(skb_head, skb_iter->data, skb_iter->len); 329 330 align_next = ALIGN(skb_iter->len, 8) - skb_iter->len; 331 332 agg_num++; 333 334 skb_queue_tail(&txcb->tx_ack_queue, skb_iter); 335 336 spin_lock_irqsave(&list->lock, flags); 337 338 skb_iter = skb_peek(list); 339 340 if (skb_iter && skb_iter->len + skb_head->len <= RTW_USB_MAX_XMITBUF_SZ) 341 __skb_unlink(skb_iter, list); 342 else 343 skb_iter = NULL; 344 spin_unlock_irqrestore(&list->lock, flags); 345 } 346 347 if (agg_num > 1) 348 rtw_usb_fill_tx_checksum(rtwusb, skb_head, agg_num); 349 350 queue: 351 skb_queue_tail(&txcb->tx_ack_queue, skb_head); 352 tx_desc = (struct rtw_tx_desc *)skb_head->data; 353 qsel = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_QSEL); 354 355 rtw_usb_write_port(rtwdev, qsel, skb_head, rtw_usb_write_port_tx_complete, txcb); 356 357 return true; 358 } 359 360 static void rtw_usb_tx_handler(struct work_struct *work) 361 { 362 struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, tx_work); 363 int i, limit; 364 365 for (i = ARRAY_SIZE(rtwusb->tx_queue) - 1; i >= 0; i--) { 366 for (limit = 0; limit < 200; limit++) { 367 struct sk_buff_head *list = &rtwusb->tx_queue[i]; 368 369 if (!rtw_usb_tx_agg_skb(rtwusb, list)) 370 break; 371 } 372 } 373 } 374 375 static void rtw_usb_tx_queue_purge(struct rtw_usb *rtwusb) 376 { 377 int i; 378 379 for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++) 380 skb_queue_purge(&rtwusb->tx_queue[i]); 381 } 382 383 static void rtw_usb_write_port_complete(struct urb *urb) 384 { 385 struct sk_buff *skb = urb->context; 386 387 dev_kfree_skb_any(skb); 388 } 389 390 static int rtw_usb_write_data(struct rtw_dev *rtwdev, 391 struct rtw_tx_pkt_info *pkt_info, 392 u8 *buf) 393 { 394 const struct rtw_chip_info *chip = rtwdev->chip; 395 struct sk_buff *skb; 396 unsigned int desclen, headsize, size; 397 u8 qsel; 398 int ret = 0; 399 400 size = pkt_info->tx_pkt_size; 401 qsel = pkt_info->qsel; 402 desclen = chip->tx_pkt_desc_sz; 403 headsize = pkt_info->offset ? pkt_info->offset : desclen; 404 405 skb = dev_alloc_skb(headsize + size); 406 if (unlikely(!skb)) 407 return -ENOMEM; 408 409 skb_reserve(skb, headsize); 410 skb_put_data(skb, buf, size); 411 skb_push(skb, headsize); 412 memset(skb->data, 0, headsize); 413 rtw_tx_fill_tx_desc(pkt_info, skb); 414 rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data); 415 416 ret = rtw_usb_write_port(rtwdev, qsel, skb, 417 rtw_usb_write_port_complete, skb); 418 if (unlikely(ret)) 419 rtw_err(rtwdev, "failed to do USB write, ret=%d\n", ret); 420 421 return ret; 422 } 423 424 static int rtw_usb_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, 425 u32 size) 426 { 427 const struct rtw_chip_info *chip = rtwdev->chip; 428 struct rtw_tx_pkt_info pkt_info = {0}; 429 430 pkt_info.tx_pkt_size = size; 431 pkt_info.qsel = TX_DESC_QSEL_BEACON; 432 pkt_info.offset = chip->tx_pkt_desc_sz; 433 434 return rtw_usb_write_data(rtwdev, &pkt_info, buf); 435 } 436 437 static int rtw_usb_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size) 438 { 439 struct rtw_tx_pkt_info pkt_info = {0}; 440 441 pkt_info.tx_pkt_size = size; 442 pkt_info.qsel = TX_DESC_QSEL_H2C; 443 444 return rtw_usb_write_data(rtwdev, &pkt_info, buf); 445 } 446 447 static u8 rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff *skb) 448 { 449 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 450 __le16 fc = hdr->frame_control; 451 u8 qsel; 452 453 if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))) 454 qsel = TX_DESC_QSEL_MGMT; 455 else if (is_broadcast_ether_addr(hdr->addr1) || 456 is_multicast_ether_addr(hdr->addr1)) 457 qsel = TX_DESC_QSEL_HIGH; 458 else if (skb_get_queue_mapping(skb) <= IEEE80211_AC_BK) 459 qsel = skb->priority; 460 else 461 qsel = TX_DESC_QSEL_BEACON; 462 463 return qsel; 464 } 465 466 static int rtw_usb_tx_write(struct rtw_dev *rtwdev, 467 struct rtw_tx_pkt_info *pkt_info, 468 struct sk_buff *skb) 469 { 470 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 471 const struct rtw_chip_info *chip = rtwdev->chip; 472 struct rtw_usb_tx_data *tx_data; 473 u8 *pkt_desc; 474 int ep; 475 476 pkt_info->qsel = rtw_usb_tx_queue_mapping_to_qsel(skb); 477 pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz); 478 memset(pkt_desc, 0, chip->tx_pkt_desc_sz); 479 ep = qsel_to_ep(rtwusb, pkt_info->qsel); 480 rtw_tx_fill_tx_desc(pkt_info, skb); 481 rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data); 482 tx_data = rtw_usb_get_tx_data(skb); 483 tx_data->sn = pkt_info->sn; 484 485 skb_queue_tail(&rtwusb->tx_queue[ep], skb); 486 487 return 0; 488 } 489 490 static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev) 491 { 492 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 493 494 queue_work(rtwusb->txwq, &rtwusb->tx_work); 495 } 496 497 static void rtw_usb_rx_handler(struct work_struct *work) 498 { 499 struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work); 500 struct rtw_dev *rtwdev = rtwusb->rtwdev; 501 const struct rtw_chip_info *chip = rtwdev->chip; 502 struct rtw_rx_pkt_stat pkt_stat; 503 struct ieee80211_rx_status rx_status; 504 struct sk_buff *skb; 505 u32 pkt_desc_sz = chip->rx_pkt_desc_sz; 506 u32 pkt_offset; 507 u8 *rx_desc; 508 int limit; 509 510 for (limit = 0; limit < 200; limit++) { 511 skb = skb_dequeue(&rtwusb->rx_queue); 512 if (!skb) 513 break; 514 515 rx_desc = skb->data; 516 chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat, 517 &rx_status); 518 pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + 519 pkt_stat.shift; 520 521 if (pkt_stat.is_c2h) { 522 skb_put(skb, pkt_stat.pkt_len + pkt_offset); 523 rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb); 524 continue; 525 } 526 527 if (skb_queue_len(&rtwusb->rx_queue) >= RTW_USB_MAX_RXQ_LEN) { 528 dev_dbg_ratelimited(rtwdev->dev, "failed to get rx_queue, overflow\n"); 529 dev_kfree_skb_any(skb); 530 continue; 531 } 532 533 skb_put(skb, pkt_stat.pkt_len); 534 skb_reserve(skb, pkt_offset); 535 memcpy(skb->cb, &rx_status, sizeof(rx_status)); 536 ieee80211_rx_irqsafe(rtwdev->hw, skb); 537 } 538 } 539 540 static void rtw_usb_read_port_complete(struct urb *urb); 541 542 static void rtw_usb_rx_resubmit(struct rtw_usb *rtwusb, struct rx_usb_ctrl_block *rxcb) 543 { 544 struct rtw_dev *rtwdev = rtwusb->rtwdev; 545 int error; 546 547 rxcb->rx_skb = alloc_skb(RTW_USB_MAX_RECVBUF_SZ, GFP_ATOMIC); 548 if (!rxcb->rx_skb) 549 return; 550 551 usb_fill_bulk_urb(rxcb->rx_urb, rtwusb->udev, 552 usb_rcvbulkpipe(rtwusb->udev, rtwusb->pipe_in), 553 rxcb->rx_skb->data, RTW_USB_MAX_RECVBUF_SZ, 554 rtw_usb_read_port_complete, rxcb); 555 556 error = usb_submit_urb(rxcb->rx_urb, GFP_ATOMIC); 557 if (error) { 558 kfree_skb(rxcb->rx_skb); 559 if (error != -ENODEV) 560 rtw_err(rtwdev, "Err sending rx data urb %d\n", 561 error); 562 } 563 } 564 565 static void rtw_usb_read_port_complete(struct urb *urb) 566 { 567 struct rx_usb_ctrl_block *rxcb = urb->context; 568 struct rtw_dev *rtwdev = rxcb->rtwdev; 569 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 570 struct sk_buff *skb = rxcb->rx_skb; 571 572 if (urb->status == 0) { 573 if (urb->actual_length >= RTW_USB_MAX_RECVBUF_SZ || 574 urb->actual_length < 24) { 575 rtw_err(rtwdev, "failed to get urb length:%d\n", 576 urb->actual_length); 577 if (skb) 578 dev_kfree_skb_any(skb); 579 } else { 580 skb_queue_tail(&rtwusb->rx_queue, skb); 581 queue_work(rtwusb->rxwq, &rtwusb->rx_work); 582 } 583 rtw_usb_rx_resubmit(rtwusb, rxcb); 584 } else { 585 switch (urb->status) { 586 case -EINVAL: 587 case -EPIPE: 588 case -ENODEV: 589 case -ESHUTDOWN: 590 case -ENOENT: 591 case -EPROTO: 592 case -EILSEQ: 593 case -ETIME: 594 case -ECOMM: 595 case -EOVERFLOW: 596 case -EINPROGRESS: 597 break; 598 default: 599 rtw_err(rtwdev, "status %d\n", urb->status); 600 break; 601 } 602 if (skb) 603 dev_kfree_skb_any(skb); 604 } 605 } 606 607 static void rtw_usb_cancel_rx_bufs(struct rtw_usb *rtwusb) 608 { 609 struct rx_usb_ctrl_block *rxcb; 610 int i; 611 612 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 613 rxcb = &rtwusb->rx_cb[i]; 614 if (rxcb->rx_urb) 615 usb_kill_urb(rxcb->rx_urb); 616 } 617 } 618 619 static void rtw_usb_free_rx_bufs(struct rtw_usb *rtwusb) 620 { 621 struct rx_usb_ctrl_block *rxcb; 622 int i; 623 624 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 625 rxcb = &rtwusb->rx_cb[i]; 626 if (rxcb->rx_urb) { 627 usb_kill_urb(rxcb->rx_urb); 628 usb_free_urb(rxcb->rx_urb); 629 } 630 } 631 } 632 633 static int rtw_usb_alloc_rx_bufs(struct rtw_usb *rtwusb) 634 { 635 int i; 636 637 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 638 struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i]; 639 640 rxcb->rtwdev = rtwusb->rtwdev; 641 rxcb->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 642 if (!rxcb->rx_urb) 643 goto err; 644 } 645 646 return 0; 647 err: 648 rtw_usb_free_rx_bufs(rtwusb); 649 return -ENOMEM; 650 } 651 652 static int rtw_usb_setup(struct rtw_dev *rtwdev) 653 { 654 /* empty function for rtw_hci_ops */ 655 return 0; 656 } 657 658 static int rtw_usb_start(struct rtw_dev *rtwdev) 659 { 660 return 0; 661 } 662 663 static void rtw_usb_stop(struct rtw_dev *rtwdev) 664 { 665 } 666 667 static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter) 668 { 669 /* empty function for rtw_hci_ops */ 670 } 671 672 static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter) 673 { 674 /* empty function for rtw_hci_ops */ 675 } 676 677 static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev) 678 { 679 /* empty function for rtw_hci_ops */ 680 } 681 682 static struct rtw_hci_ops rtw_usb_ops = { 683 .tx_write = rtw_usb_tx_write, 684 .tx_kick_off = rtw_usb_tx_kick_off, 685 .setup = rtw_usb_setup, 686 .start = rtw_usb_start, 687 .stop = rtw_usb_stop, 688 .deep_ps = rtw_usb_deep_ps, 689 .link_ps = rtw_usb_link_ps, 690 .interface_cfg = rtw_usb_interface_cfg, 691 692 .write8 = rtw_usb_write8, 693 .write16 = rtw_usb_write16, 694 .write32 = rtw_usb_write32, 695 .read8 = rtw_usb_read8, 696 .read16 = rtw_usb_read16, 697 .read32 = rtw_usb_read32, 698 699 .write_data_rsvd_page = rtw_usb_write_data_rsvd_page, 700 .write_data_h2c = rtw_usb_write_data_h2c, 701 }; 702 703 static int rtw_usb_init_rx(struct rtw_dev *rtwdev) 704 { 705 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 706 int i; 707 708 rtwusb->rxwq = create_singlethread_workqueue("rtw88_usb: rx wq"); 709 if (!rtwusb->rxwq) { 710 rtw_err(rtwdev, "failed to create RX work queue\n"); 711 return -ENOMEM; 712 } 713 714 skb_queue_head_init(&rtwusb->rx_queue); 715 716 INIT_WORK(&rtwusb->rx_work, rtw_usb_rx_handler); 717 718 for (i = 0; i < RTW_USB_RXCB_NUM; i++) { 719 struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i]; 720 721 rtw_usb_rx_resubmit(rtwusb, rxcb); 722 } 723 724 return 0; 725 } 726 727 static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev) 728 { 729 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 730 731 skb_queue_purge(&rtwusb->rx_queue); 732 733 flush_workqueue(rtwusb->rxwq); 734 destroy_workqueue(rtwusb->rxwq); 735 } 736 737 static int rtw_usb_init_tx(struct rtw_dev *rtwdev) 738 { 739 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 740 int i; 741 742 rtwusb->txwq = create_singlethread_workqueue("rtw88_usb: tx wq"); 743 if (!rtwusb->txwq) { 744 rtw_err(rtwdev, "failed to create TX work queue\n"); 745 return -ENOMEM; 746 } 747 748 for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++) 749 skb_queue_head_init(&rtwusb->tx_queue[i]); 750 751 INIT_WORK(&rtwusb->tx_work, rtw_usb_tx_handler); 752 753 return 0; 754 } 755 756 static void rtw_usb_deinit_tx(struct rtw_dev *rtwdev) 757 { 758 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 759 760 rtw_usb_tx_queue_purge(rtwusb); 761 flush_workqueue(rtwusb->txwq); 762 destroy_workqueue(rtwusb->txwq); 763 } 764 765 static int rtw_usb_intf_init(struct rtw_dev *rtwdev, 766 struct usb_interface *intf) 767 { 768 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 769 struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf)); 770 int ret; 771 772 rtwusb->udev = udev; 773 ret = rtw_usb_parse(rtwdev, intf); 774 if (ret) 775 return ret; 776 777 rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32), 778 GFP_KERNEL); 779 if (!rtwusb->usb_data) 780 return -ENOMEM; 781 782 usb_set_intfdata(intf, rtwdev->hw); 783 784 SET_IEEE80211_DEV(rtwdev->hw, &intf->dev); 785 spin_lock_init(&rtwusb->usb_lock); 786 787 return 0; 788 } 789 790 static void rtw_usb_intf_deinit(struct rtw_dev *rtwdev, 791 struct usb_interface *intf) 792 { 793 struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev); 794 795 usb_put_dev(rtwusb->udev); 796 kfree(rtwusb->usb_data); 797 usb_set_intfdata(intf, NULL); 798 } 799 800 int rtw_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 801 { 802 struct rtw_dev *rtwdev; 803 struct ieee80211_hw *hw; 804 struct rtw_usb *rtwusb; 805 int drv_data_size; 806 int ret; 807 808 drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_usb); 809 hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops); 810 if (!hw) 811 return -ENOMEM; 812 813 rtwdev = hw->priv; 814 rtwdev->hw = hw; 815 rtwdev->dev = &intf->dev; 816 rtwdev->chip = (struct rtw_chip_info *)id->driver_info; 817 rtwdev->hci.ops = &rtw_usb_ops; 818 rtwdev->hci.type = RTW_HCI_TYPE_USB; 819 820 rtwusb = rtw_get_usb_priv(rtwdev); 821 rtwusb->rtwdev = rtwdev; 822 823 ret = rtw_usb_alloc_rx_bufs(rtwusb); 824 if (ret) 825 goto err_release_hw; 826 827 ret = rtw_core_init(rtwdev); 828 if (ret) 829 goto err_free_rx_bufs; 830 831 ret = rtw_usb_intf_init(rtwdev, intf); 832 if (ret) { 833 rtw_err(rtwdev, "failed to init USB interface\n"); 834 goto err_deinit_core; 835 } 836 837 ret = rtw_usb_init_tx(rtwdev); 838 if (ret) { 839 rtw_err(rtwdev, "failed to init USB TX\n"); 840 goto err_destroy_usb; 841 } 842 843 ret = rtw_usb_init_rx(rtwdev); 844 if (ret) { 845 rtw_err(rtwdev, "failed to init USB RX\n"); 846 goto err_destroy_txwq; 847 } 848 849 ret = rtw_chip_info_setup(rtwdev); 850 if (ret) { 851 rtw_err(rtwdev, "failed to setup chip information\n"); 852 goto err_destroy_rxwq; 853 } 854 855 ret = rtw_register_hw(rtwdev, rtwdev->hw); 856 if (ret) { 857 rtw_err(rtwdev, "failed to register hw\n"); 858 goto err_destroy_rxwq; 859 } 860 861 return 0; 862 863 err_destroy_rxwq: 864 rtw_usb_deinit_rx(rtwdev); 865 866 err_destroy_txwq: 867 rtw_usb_deinit_tx(rtwdev); 868 869 err_destroy_usb: 870 rtw_usb_intf_deinit(rtwdev, intf); 871 872 err_deinit_core: 873 rtw_core_deinit(rtwdev); 874 875 err_free_rx_bufs: 876 rtw_usb_free_rx_bufs(rtwusb); 877 878 err_release_hw: 879 ieee80211_free_hw(hw); 880 881 return ret; 882 } 883 EXPORT_SYMBOL(rtw_usb_probe); 884 885 void rtw_usb_disconnect(struct usb_interface *intf) 886 { 887 struct ieee80211_hw *hw = usb_get_intfdata(intf); 888 struct rtw_dev *rtwdev; 889 struct rtw_usb *rtwusb; 890 891 if (!hw) 892 return; 893 894 rtwdev = hw->priv; 895 rtwusb = rtw_get_usb_priv(rtwdev); 896 897 rtw_usb_cancel_rx_bufs(rtwusb); 898 899 rtw_unregister_hw(rtwdev, hw); 900 rtw_usb_deinit_tx(rtwdev); 901 rtw_usb_deinit_rx(rtwdev); 902 903 if (rtwusb->udev->state != USB_STATE_NOTATTACHED) 904 usb_reset_device(rtwusb->udev); 905 906 rtw_usb_free_rx_bufs(rtwusb); 907 908 rtw_usb_intf_deinit(rtwdev, intf); 909 rtw_core_deinit(rtwdev); 910 ieee80211_free_hw(hw); 911 } 912 EXPORT_SYMBOL(rtw_usb_disconnect); 913 914 MODULE_AUTHOR("Realtek Corporation"); 915 MODULE_DESCRIPTION("Realtek USB 802.11ac wireless driver"); 916 MODULE_LICENSE("Dual BSD/GPL"); 917