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