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