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