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