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