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 		usb_kill_urb(rxcb->rx_urb);
615 	}
616 }
617 
618 static void rtw_usb_free_rx_bufs(struct rtw_usb *rtwusb)
619 {
620 	struct rx_usb_ctrl_block *rxcb;
621 	int i;
622 
623 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
624 		rxcb = &rtwusb->rx_cb[i];
625 		usb_kill_urb(rxcb->rx_urb);
626 		usb_free_urb(rxcb->rx_urb);
627 	}
628 }
629 
630 static int rtw_usb_alloc_rx_bufs(struct rtw_usb *rtwusb)
631 {
632 	int i;
633 
634 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
635 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
636 
637 		rxcb->rtwdev = rtwusb->rtwdev;
638 		rxcb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
639 		if (!rxcb->rx_urb)
640 			goto err;
641 	}
642 
643 	return 0;
644 err:
645 	rtw_usb_free_rx_bufs(rtwusb);
646 	return -ENOMEM;
647 }
648 
649 static int rtw_usb_setup(struct rtw_dev *rtwdev)
650 {
651 	/* empty function for rtw_hci_ops */
652 	return 0;
653 }
654 
655 static int rtw_usb_start(struct rtw_dev *rtwdev)
656 {
657 	return 0;
658 }
659 
660 static void rtw_usb_stop(struct rtw_dev *rtwdev)
661 {
662 }
663 
664 static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter)
665 {
666 	/* empty function for rtw_hci_ops */
667 }
668 
669 static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter)
670 {
671 	/* empty function for rtw_hci_ops */
672 }
673 
674 static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev)
675 {
676 	/* empty function for rtw_hci_ops */
677 }
678 
679 static struct rtw_hci_ops rtw_usb_ops = {
680 	.tx_write = rtw_usb_tx_write,
681 	.tx_kick_off = rtw_usb_tx_kick_off,
682 	.setup = rtw_usb_setup,
683 	.start = rtw_usb_start,
684 	.stop = rtw_usb_stop,
685 	.deep_ps = rtw_usb_deep_ps,
686 	.link_ps = rtw_usb_link_ps,
687 	.interface_cfg = rtw_usb_interface_cfg,
688 
689 	.write8  = rtw_usb_write8,
690 	.write16 = rtw_usb_write16,
691 	.write32 = rtw_usb_write32,
692 	.read8	= rtw_usb_read8,
693 	.read16 = rtw_usb_read16,
694 	.read32 = rtw_usb_read32,
695 
696 	.write_data_rsvd_page = rtw_usb_write_data_rsvd_page,
697 	.write_data_h2c = rtw_usb_write_data_h2c,
698 };
699 
700 static int rtw_usb_init_rx(struct rtw_dev *rtwdev)
701 {
702 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
703 	int i;
704 
705 	rtwusb->rxwq = create_singlethread_workqueue("rtw88_usb: rx wq");
706 	if (!rtwusb->rxwq) {
707 		rtw_err(rtwdev, "failed to create RX work queue\n");
708 		return -ENOMEM;
709 	}
710 
711 	skb_queue_head_init(&rtwusb->rx_queue);
712 
713 	INIT_WORK(&rtwusb->rx_work, rtw_usb_rx_handler);
714 
715 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
716 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
717 
718 		rtw_usb_rx_resubmit(rtwusb, rxcb);
719 	}
720 
721 	return 0;
722 }
723 
724 static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev)
725 {
726 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
727 
728 	skb_queue_purge(&rtwusb->rx_queue);
729 
730 	flush_workqueue(rtwusb->rxwq);
731 	destroy_workqueue(rtwusb->rxwq);
732 }
733 
734 static int rtw_usb_init_tx(struct rtw_dev *rtwdev)
735 {
736 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
737 	int i;
738 
739 	rtwusb->txwq = create_singlethread_workqueue("rtw88_usb: tx wq");
740 	if (!rtwusb->txwq) {
741 		rtw_err(rtwdev, "failed to create TX work queue\n");
742 		return -ENOMEM;
743 	}
744 
745 	for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++)
746 		skb_queue_head_init(&rtwusb->tx_queue[i]);
747 
748 	INIT_WORK(&rtwusb->tx_work, rtw_usb_tx_handler);
749 
750 	return 0;
751 }
752 
753 static void rtw_usb_deinit_tx(struct rtw_dev *rtwdev)
754 {
755 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
756 
757 	rtw_usb_tx_queue_purge(rtwusb);
758 	flush_workqueue(rtwusb->txwq);
759 	destroy_workqueue(rtwusb->txwq);
760 }
761 
762 static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
763 			     struct usb_interface *intf)
764 {
765 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
766 	struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf));
767 	int ret;
768 
769 	rtwusb->udev = udev;
770 	ret = rtw_usb_parse(rtwdev, intf);
771 	if (ret)
772 		return ret;
773 
774 	rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
775 				   GFP_KERNEL);
776 	if (!rtwusb->usb_data)
777 		return -ENOMEM;
778 
779 	usb_set_intfdata(intf, rtwdev->hw);
780 
781 	SET_IEEE80211_DEV(rtwdev->hw, &intf->dev);
782 	spin_lock_init(&rtwusb->usb_lock);
783 
784 	return 0;
785 }
786 
787 static void rtw_usb_intf_deinit(struct rtw_dev *rtwdev,
788 				struct usb_interface *intf)
789 {
790 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
791 
792 	usb_put_dev(rtwusb->udev);
793 	kfree(rtwusb->usb_data);
794 	usb_set_intfdata(intf, NULL);
795 }
796 
797 int rtw_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
798 {
799 	struct rtw_dev *rtwdev;
800 	struct ieee80211_hw *hw;
801 	struct rtw_usb *rtwusb;
802 	int drv_data_size;
803 	int ret;
804 
805 	drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_usb);
806 	hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops);
807 	if (!hw)
808 		return -ENOMEM;
809 
810 	rtwdev = hw->priv;
811 	rtwdev->hw = hw;
812 	rtwdev->dev = &intf->dev;
813 	rtwdev->chip = (struct rtw_chip_info *)id->driver_info;
814 	rtwdev->hci.ops = &rtw_usb_ops;
815 	rtwdev->hci.type = RTW_HCI_TYPE_USB;
816 
817 	rtwusb = rtw_get_usb_priv(rtwdev);
818 	rtwusb->rtwdev = rtwdev;
819 
820 	ret = rtw_usb_alloc_rx_bufs(rtwusb);
821 	if (ret)
822 		goto err_release_hw;
823 
824 	ret = rtw_core_init(rtwdev);
825 	if (ret)
826 		goto err_free_rx_bufs;
827 
828 	ret = rtw_usb_intf_init(rtwdev, intf);
829 	if (ret) {
830 		rtw_err(rtwdev, "failed to init USB interface\n");
831 		goto err_deinit_core;
832 	}
833 
834 	ret = rtw_usb_init_tx(rtwdev);
835 	if (ret) {
836 		rtw_err(rtwdev, "failed to init USB TX\n");
837 		goto err_destroy_usb;
838 	}
839 
840 	ret = rtw_usb_init_rx(rtwdev);
841 	if (ret) {
842 		rtw_err(rtwdev, "failed to init USB RX\n");
843 		goto err_destroy_txwq;
844 	}
845 
846 	ret = rtw_chip_info_setup(rtwdev);
847 	if (ret) {
848 		rtw_err(rtwdev, "failed to setup chip information\n");
849 		goto err_destroy_rxwq;
850 	}
851 
852 	ret = rtw_register_hw(rtwdev, rtwdev->hw);
853 	if (ret) {
854 		rtw_err(rtwdev, "failed to register hw\n");
855 		goto err_destroy_rxwq;
856 	}
857 
858 	return 0;
859 
860 err_destroy_rxwq:
861 	rtw_usb_deinit_rx(rtwdev);
862 
863 err_destroy_txwq:
864 	rtw_usb_deinit_tx(rtwdev);
865 
866 err_destroy_usb:
867 	rtw_usb_intf_deinit(rtwdev, intf);
868 
869 err_deinit_core:
870 	rtw_core_deinit(rtwdev);
871 
872 err_free_rx_bufs:
873 	rtw_usb_free_rx_bufs(rtwusb);
874 
875 err_release_hw:
876 	ieee80211_free_hw(hw);
877 
878 	return ret;
879 }
880 EXPORT_SYMBOL(rtw_usb_probe);
881 
882 void rtw_usb_disconnect(struct usb_interface *intf)
883 {
884 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
885 	struct rtw_dev *rtwdev;
886 	struct rtw_usb *rtwusb;
887 
888 	if (!hw)
889 		return;
890 
891 	rtwdev = hw->priv;
892 	rtwusb = rtw_get_usb_priv(rtwdev);
893 
894 	rtw_usb_cancel_rx_bufs(rtwusb);
895 
896 	rtw_unregister_hw(rtwdev, hw);
897 	rtw_usb_deinit_tx(rtwdev);
898 	rtw_usb_deinit_rx(rtwdev);
899 
900 	if (rtwusb->udev->state != USB_STATE_NOTATTACHED)
901 		usb_reset_device(rtwusb->udev);
902 
903 	rtw_usb_free_rx_bufs(rtwusb);
904 
905 	rtw_usb_intf_deinit(rtwdev, intf);
906 	rtw_core_deinit(rtwdev);
907 	ieee80211_free_hw(hw);
908 }
909 EXPORT_SYMBOL(rtw_usb_disconnect);
910 
911 MODULE_AUTHOR("Realtek Corporation");
912 MODULE_DESCRIPTION("Realtek USB 802.11ac wireless driver");
913 MODULE_LICENSE("Dual BSD/GPL");
914