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