xref: /linux/drivers/net/wireless/realtek/rtw88/usb.c (revision 90602c251cda8a1e526efb250f28c1ea3f87cd78)
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 static bool rtw_switch_usb_mode = true;
18 module_param_named(switch_usb_mode, rtw_switch_usb_mode, bool, 0644);
19 MODULE_PARM_DESC(switch_usb_mode,
20 		 "Set to N to disable switching to USB 3 mode to avoid potential interference in the 2.4 GHz band (default: Y)");
21 
22 #define RTW_USB_MAX_RXQ_LEN	512
23 
24 struct rtw_usb_txcb {
25 	struct rtw_dev *rtwdev;
26 	struct sk_buff_head tx_ack_queue;
27 };
28 
rtw_usb_fill_tx_checksum(struct rtw_usb * rtwusb,struct sk_buff * skb,int agg_num)29 static void rtw_usb_fill_tx_checksum(struct rtw_usb *rtwusb,
30 				     struct sk_buff *skb, int agg_num)
31 {
32 	struct rtw_tx_desc *tx_desc = (struct rtw_tx_desc *)skb->data;
33 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
34 	struct rtw_tx_pkt_info pkt_info;
35 
36 	le32p_replace_bits(&tx_desc->w7, agg_num, RTW_TX_DESC_W7_DMA_TXAGG_NUM);
37 	pkt_info.pkt_offset = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_PKT_OFFSET);
38 	rtw_tx_fill_txdesc_checksum(rtwdev, &pkt_info, skb->data);
39 }
40 
rtw_usb_reg_sec(struct rtw_dev * rtwdev,u32 addr,__le32 * data)41 static void rtw_usb_reg_sec(struct rtw_dev *rtwdev, u32 addr, __le32 *data)
42 {
43 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
44 	struct usb_device *udev = rtwusb->udev;
45 	bool reg_on_section = false;
46 	u16 t_reg = 0x4e0;
47 	u8 t_len = 1;
48 	int status;
49 
50 	/* There are three sections:
51 	 * 1. on (0x00~0xFF; 0x1000~0x10FF): this section is always powered on
52 	 * 2. off (< 0xFE00, excluding "on" section): this section could be
53 	 *    powered off
54 	 * 3. local (>= 0xFE00): usb specific registers section
55 	 */
56 	if (addr <= 0xff || (addr >= 0x1000 && addr <= 0x10ff))
57 		reg_on_section = true;
58 
59 	if (!reg_on_section)
60 		return;
61 
62 	status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
63 				 RTW_USB_CMD_REQ, RTW_USB_CMD_WRITE,
64 				 t_reg, 0, data, t_len, 500);
65 
66 	if (status != t_len && status != -ENODEV)
67 		rtw_err(rtwdev, "%s: reg 0x%x, usb write %u fail, status: %d\n",
68 			__func__, t_reg, t_len, status);
69 }
70 
rtw_usb_read(struct rtw_dev * rtwdev,u32 addr,u16 len)71 static u32 rtw_usb_read(struct rtw_dev *rtwdev, u32 addr, u16 len)
72 {
73 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
74 	struct usb_device *udev = rtwusb->udev;
75 	__le32 *data;
76 	unsigned long flags;
77 	int idx, ret;
78 	static int count;
79 
80 	spin_lock_irqsave(&rtwusb->usb_lock, flags);
81 
82 	idx = rtwusb->usb_data_index;
83 	rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1);
84 
85 	spin_unlock_irqrestore(&rtwusb->usb_lock, flags);
86 
87 	data = &rtwusb->usb_data[idx];
88 
89 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
90 			      RTW_USB_CMD_REQ, RTW_USB_CMD_READ, addr,
91 			      RTW_USB_VENQT_CMD_IDX, data, len, 1000);
92 	if (ret < 0 && ret != -ENODEV && count++ < 4)
93 		rtw_err(rtwdev, "read register 0x%x failed with %d\n",
94 			addr, ret);
95 
96 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C ||
97 	    rtwdev->chip->id == RTW_CHIP_TYPE_8822B ||
98 	    rtwdev->chip->id == RTW_CHIP_TYPE_8821C)
99 		rtw_usb_reg_sec(rtwdev, addr, data);
100 
101 	return le32_to_cpu(*data);
102 }
103 
rtw_usb_read8(struct rtw_dev * rtwdev,u32 addr)104 static u8 rtw_usb_read8(struct rtw_dev *rtwdev, u32 addr)
105 {
106 	return (u8)rtw_usb_read(rtwdev, addr, 1);
107 }
108 
rtw_usb_read16(struct rtw_dev * rtwdev,u32 addr)109 static u16 rtw_usb_read16(struct rtw_dev *rtwdev, u32 addr)
110 {
111 	return (u16)rtw_usb_read(rtwdev, addr, 2);
112 }
113 
rtw_usb_read32(struct rtw_dev * rtwdev,u32 addr)114 static u32 rtw_usb_read32(struct rtw_dev *rtwdev, u32 addr)
115 {
116 	return (u32)rtw_usb_read(rtwdev, addr, 4);
117 }
118 
rtw_usb_write(struct rtw_dev * rtwdev,u32 addr,u32 val,int len)119 static void rtw_usb_write(struct rtw_dev *rtwdev, u32 addr, u32 val, int len)
120 {
121 	struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv;
122 	struct usb_device *udev = rtwusb->udev;
123 	unsigned long flags;
124 	__le32 *data;
125 	int idx, ret;
126 	static int count;
127 
128 	spin_lock_irqsave(&rtwusb->usb_lock, flags);
129 
130 	idx = rtwusb->usb_data_index;
131 	rtwusb->usb_data_index = (idx + 1) & (RTW_USB_MAX_RXTX_COUNT - 1);
132 
133 	spin_unlock_irqrestore(&rtwusb->usb_lock, flags);
134 
135 	data = &rtwusb->usb_data[idx];
136 
137 	*data = cpu_to_le32(val);
138 
139 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
140 			      RTW_USB_CMD_REQ, RTW_USB_CMD_WRITE,
141 			      addr, 0, data, len, 30000);
142 	if (ret < 0 && ret != -ENODEV && count++ < 4)
143 		rtw_err(rtwdev, "write register 0x%x failed with %d\n",
144 			addr, ret);
145 
146 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C ||
147 	    rtwdev->chip->id == RTW_CHIP_TYPE_8822B ||
148 	    rtwdev->chip->id == RTW_CHIP_TYPE_8821C)
149 		rtw_usb_reg_sec(rtwdev, addr, data);
150 }
151 
rtw_usb_write8(struct rtw_dev * rtwdev,u32 addr,u8 val)152 static void rtw_usb_write8(struct rtw_dev *rtwdev, u32 addr, u8 val)
153 {
154 	rtw_usb_write(rtwdev, addr, val, 1);
155 }
156 
rtw_usb_write16(struct rtw_dev * rtwdev,u32 addr,u16 val)157 static void rtw_usb_write16(struct rtw_dev *rtwdev, u32 addr, u16 val)
158 {
159 	rtw_usb_write(rtwdev, addr, val, 2);
160 }
161 
rtw_usb_write32(struct rtw_dev * rtwdev,u32 addr,u32 val)162 static void rtw_usb_write32(struct rtw_dev *rtwdev, u32 addr, u32 val)
163 {
164 	rtw_usb_write(rtwdev, addr, val, 4);
165 }
166 
dma_mapping_to_ep(enum rtw_dma_mapping dma_mapping)167 static int dma_mapping_to_ep(enum rtw_dma_mapping dma_mapping)
168 {
169 	switch (dma_mapping) {
170 	case RTW_DMA_MAPPING_HIGH:
171 		return 0;
172 	case RTW_DMA_MAPPING_NORMAL:
173 		return 1;
174 	case RTW_DMA_MAPPING_LOW:
175 		return 2;
176 	case RTW_DMA_MAPPING_EXTRA:
177 		return 3;
178 	default:
179 		return -EINVAL;
180 	}
181 }
182 
rtw_usb_parse(struct rtw_dev * rtwdev,struct usb_interface * interface)183 static int rtw_usb_parse(struct rtw_dev *rtwdev,
184 			 struct usb_interface *interface)
185 {
186 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
187 	struct usb_host_interface *host_interface = &interface->altsetting[0];
188 	struct usb_interface_descriptor *interface_desc = &host_interface->desc;
189 	struct usb_endpoint_descriptor *endpoint;
190 	int num_out_pipes = 0;
191 	int i;
192 	u8 num;
193 	const struct rtw_chip_info *chip = rtwdev->chip;
194 	const struct rtw_rqpn *rqpn;
195 
196 	for (i = 0; i < interface_desc->bNumEndpoints; i++) {
197 		endpoint = &host_interface->endpoint[i].desc;
198 		num = usb_endpoint_num(endpoint);
199 
200 		if (usb_endpoint_dir_in(endpoint) &&
201 		    usb_endpoint_xfer_bulk(endpoint)) {
202 			if (rtwusb->pipe_in) {
203 				rtw_err(rtwdev, "IN pipes overflow\n");
204 				return -EINVAL;
205 			}
206 
207 			rtwusb->pipe_in = num;
208 		}
209 
210 		if (usb_endpoint_dir_in(endpoint) &&
211 		    usb_endpoint_xfer_int(endpoint)) {
212 			if (rtwusb->pipe_interrupt) {
213 				rtw_err(rtwdev, "INT pipes overflow\n");
214 				return -EINVAL;
215 			}
216 
217 			rtwusb->pipe_interrupt = num;
218 		}
219 
220 		if (usb_endpoint_dir_out(endpoint) &&
221 		    usb_endpoint_xfer_bulk(endpoint)) {
222 			if (num_out_pipes >= ARRAY_SIZE(rtwusb->out_ep)) {
223 				rtw_err(rtwdev, "OUT pipes overflow\n");
224 				return -EINVAL;
225 			}
226 
227 			rtwusb->out_ep[num_out_pipes++] = num;
228 		}
229 	}
230 
231 	rtwdev->hci.bulkout_num = num_out_pipes;
232 
233 	if (num_out_pipes < 1 || num_out_pipes > 4) {
234 		rtw_err(rtwdev, "invalid number of endpoints %d\n", num_out_pipes);
235 		return -EINVAL;
236 	}
237 
238 	rqpn = &chip->rqpn_table[num_out_pipes];
239 
240 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID0] = dma_mapping_to_ep(rqpn->dma_map_be);
241 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID1] = dma_mapping_to_ep(rqpn->dma_map_bk);
242 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID2] = dma_mapping_to_ep(rqpn->dma_map_bk);
243 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID3] = dma_mapping_to_ep(rqpn->dma_map_be);
244 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID4] = dma_mapping_to_ep(rqpn->dma_map_vi);
245 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID5] = dma_mapping_to_ep(rqpn->dma_map_vi);
246 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID6] = dma_mapping_to_ep(rqpn->dma_map_vo);
247 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID7] = dma_mapping_to_ep(rqpn->dma_map_vo);
248 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID8] = -EINVAL;
249 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID9] = -EINVAL;
250 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID10] = -EINVAL;
251 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID11] = -EINVAL;
252 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID12] = -EINVAL;
253 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID13] = -EINVAL;
254 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID14] = -EINVAL;
255 	rtwusb->qsel_to_ep[TX_DESC_QSEL_TID15] = -EINVAL;
256 	rtwusb->qsel_to_ep[TX_DESC_QSEL_BEACON] = dma_mapping_to_ep(rqpn->dma_map_hi);
257 	rtwusb->qsel_to_ep[TX_DESC_QSEL_HIGH] = dma_mapping_to_ep(rqpn->dma_map_hi);
258 	rtwusb->qsel_to_ep[TX_DESC_QSEL_MGMT] = dma_mapping_to_ep(rqpn->dma_map_mg);
259 	rtwusb->qsel_to_ep[TX_DESC_QSEL_H2C] = dma_mapping_to_ep(rqpn->dma_map_hi);
260 
261 	return 0;
262 }
263 
rtw_usb_write_port_tx_complete(struct urb * urb)264 static void rtw_usb_write_port_tx_complete(struct urb *urb)
265 {
266 	struct rtw_usb_txcb *txcb = urb->context;
267 	struct rtw_dev *rtwdev = txcb->rtwdev;
268 	struct ieee80211_hw *hw = rtwdev->hw;
269 
270 	while (true) {
271 		struct sk_buff *skb = skb_dequeue(&txcb->tx_ack_queue);
272 		struct ieee80211_tx_info *info;
273 		struct rtw_usb_tx_data *tx_data;
274 
275 		if (!skb)
276 			break;
277 
278 		info = IEEE80211_SKB_CB(skb);
279 		tx_data = rtw_usb_get_tx_data(skb);
280 
281 		skb_pull(skb, rtwdev->chip->tx_pkt_desc_sz);
282 
283 		/* enqueue to wait for tx report */
284 		if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) {
285 			rtw_tx_report_enqueue(rtwdev, skb, tx_data->sn);
286 			continue;
287 		}
288 
289 		/* always ACK for others, then they won't be marked as drop */
290 		ieee80211_tx_info_clear_status(info);
291 		if (info->flags & IEEE80211_TX_CTL_NO_ACK)
292 			info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
293 		else
294 			info->flags |= IEEE80211_TX_STAT_ACK;
295 
296 		ieee80211_tx_status_irqsafe(hw, skb);
297 	}
298 
299 	kfree(txcb);
300 }
301 
qsel_to_ep(struct rtw_usb * rtwusb,unsigned int qsel)302 static int qsel_to_ep(struct rtw_usb *rtwusb, unsigned int qsel)
303 {
304 	if (qsel >= ARRAY_SIZE(rtwusb->qsel_to_ep))
305 		return -EINVAL;
306 
307 	return rtwusb->qsel_to_ep[qsel];
308 }
309 
rtw_usb_write_port(struct rtw_dev * rtwdev,u8 qsel,struct sk_buff * skb,usb_complete_t cb,void * context)310 static int rtw_usb_write_port(struct rtw_dev *rtwdev, u8 qsel, struct sk_buff *skb,
311 			      usb_complete_t cb, void *context)
312 {
313 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
314 	struct usb_device *usbd = rtwusb->udev;
315 	struct urb *urb;
316 	unsigned int pipe;
317 	int ret;
318 	int ep = qsel_to_ep(rtwusb, qsel);
319 
320 	if (ep < 0)
321 		return ep;
322 
323 	pipe = usb_sndbulkpipe(usbd, rtwusb->out_ep[ep]);
324 	urb = usb_alloc_urb(0, GFP_ATOMIC);
325 	if (!urb)
326 		return -ENOMEM;
327 
328 	usb_fill_bulk_urb(urb, usbd, pipe, skb->data, skb->len, cb, context);
329 	urb->transfer_flags |= URB_ZERO_PACKET;
330 	ret = usb_submit_urb(urb, GFP_ATOMIC);
331 
332 	usb_free_urb(urb);
333 
334 	return ret;
335 }
336 
rtw_usb_tx_agg_skb(struct rtw_usb * rtwusb,struct sk_buff_head * list)337 static bool rtw_usb_tx_agg_skb(struct rtw_usb *rtwusb, struct sk_buff_head *list)
338 {
339 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
340 	struct rtw_tx_desc *tx_desc;
341 	struct rtw_usb_txcb *txcb;
342 	struct sk_buff *skb_head;
343 	struct sk_buff *skb_iter;
344 	int agg_num = 0;
345 	unsigned int align_next = 0;
346 	u8 qsel;
347 
348 	if (skb_queue_empty(list))
349 		return false;
350 
351 	txcb = kmalloc(sizeof(*txcb), GFP_ATOMIC);
352 	if (!txcb)
353 		return false;
354 
355 	txcb->rtwdev = rtwdev;
356 	skb_queue_head_init(&txcb->tx_ack_queue);
357 
358 	skb_iter = skb_dequeue(list);
359 
360 	if (skb_queue_empty(list)) {
361 		skb_head = skb_iter;
362 		goto queue;
363 	}
364 
365 	skb_head = dev_alloc_skb(RTW_USB_MAX_XMITBUF_SZ);
366 	if (!skb_head) {
367 		skb_head = skb_iter;
368 		goto queue;
369 	}
370 
371 	while (skb_iter) {
372 		unsigned long flags;
373 
374 		skb_put(skb_head, align_next);
375 		skb_put_data(skb_head, skb_iter->data, skb_iter->len);
376 
377 		align_next = ALIGN(skb_iter->len, 8) - skb_iter->len;
378 
379 		agg_num++;
380 
381 		skb_queue_tail(&txcb->tx_ack_queue, skb_iter);
382 
383 		spin_lock_irqsave(&list->lock, flags);
384 
385 		skb_iter = skb_peek(list);
386 
387 		if (skb_iter &&
388 		    skb_iter->len + skb_head->len <= RTW_USB_MAX_XMITBUF_SZ &&
389 		    agg_num < rtwdev->chip->usb_tx_agg_desc_num)
390 			__skb_unlink(skb_iter, list);
391 		else
392 			skb_iter = NULL;
393 		spin_unlock_irqrestore(&list->lock, flags);
394 	}
395 
396 	if (agg_num > 1)
397 		rtw_usb_fill_tx_checksum(rtwusb, skb_head, agg_num);
398 
399 queue:
400 	skb_queue_tail(&txcb->tx_ack_queue, skb_head);
401 	tx_desc = (struct rtw_tx_desc *)skb_head->data;
402 	qsel = le32_get_bits(tx_desc->w1, RTW_TX_DESC_W1_QSEL);
403 
404 	rtw_usb_write_port(rtwdev, qsel, skb_head, rtw_usb_write_port_tx_complete, txcb);
405 
406 	return true;
407 }
408 
rtw_usb_tx_handler(struct work_struct * work)409 static void rtw_usb_tx_handler(struct work_struct *work)
410 {
411 	struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, tx_work);
412 	int i, limit;
413 
414 	for (i = ARRAY_SIZE(rtwusb->tx_queue) - 1; i >= 0; i--) {
415 		for (limit = 0; limit < 200; limit++) {
416 			struct sk_buff_head *list = &rtwusb->tx_queue[i];
417 
418 			if (!rtw_usb_tx_agg_skb(rtwusb, list))
419 				break;
420 		}
421 	}
422 }
423 
rtw_usb_tx_queue_purge(struct rtw_usb * rtwusb)424 static void rtw_usb_tx_queue_purge(struct rtw_usb *rtwusb)
425 {
426 	int i;
427 
428 	for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++)
429 		skb_queue_purge(&rtwusb->tx_queue[i]);
430 }
431 
rtw_usb_write_port_complete(struct urb * urb)432 static void rtw_usb_write_port_complete(struct urb *urb)
433 {
434 	struct sk_buff *skb = urb->context;
435 
436 	dev_kfree_skb_any(skb);
437 }
438 
rtw_usb_write_data(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf)439 static int rtw_usb_write_data(struct rtw_dev *rtwdev,
440 			      struct rtw_tx_pkt_info *pkt_info,
441 			      u8 *buf)
442 {
443 	const struct rtw_chip_info *chip = rtwdev->chip;
444 	struct sk_buff *skb;
445 	unsigned int size;
446 	u8 qsel;
447 	int ret = 0;
448 
449 	size = pkt_info->tx_pkt_size;
450 	qsel = pkt_info->qsel;
451 
452 	skb = dev_alloc_skb(chip->tx_pkt_desc_sz + size);
453 	if (unlikely(!skb))
454 		return -ENOMEM;
455 
456 	skb_reserve(skb, chip->tx_pkt_desc_sz);
457 	skb_put_data(skb, buf, size);
458 	skb_push(skb, chip->tx_pkt_desc_sz);
459 	memset(skb->data, 0, chip->tx_pkt_desc_sz);
460 	rtw_tx_fill_tx_desc(pkt_info, skb);
461 	rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
462 
463 	ret = rtw_usb_write_port(rtwdev, qsel, skb,
464 				 rtw_usb_write_port_complete, skb);
465 	if (unlikely(ret))
466 		rtw_err(rtwdev, "failed to do USB write, ret=%d\n", ret);
467 
468 	return ret;
469 }
470 
rtw_usb_write_data_rsvd_page(struct rtw_dev * rtwdev,u8 * buf,u32 size)471 static int rtw_usb_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf,
472 					u32 size)
473 {
474 	const struct rtw_chip_info *chip = rtwdev->chip;
475 	struct rtw_tx_pkt_info pkt_info = {0};
476 
477 	pkt_info.tx_pkt_size = size;
478 	pkt_info.qsel = TX_DESC_QSEL_BEACON;
479 	pkt_info.offset = chip->tx_pkt_desc_sz;
480 
481 	return rtw_usb_write_data(rtwdev, &pkt_info, buf);
482 }
483 
rtw_usb_write_data_h2c(struct rtw_dev * rtwdev,u8 * buf,u32 size)484 static int rtw_usb_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
485 {
486 	struct rtw_tx_pkt_info pkt_info = {0};
487 
488 	pkt_info.tx_pkt_size = size;
489 	pkt_info.qsel = TX_DESC_QSEL_H2C;
490 
491 	return rtw_usb_write_data(rtwdev, &pkt_info, buf);
492 }
493 
rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff * skb)494 static u8 rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff *skb)
495 {
496 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
497 	__le16 fc = hdr->frame_control;
498 	u8 qsel;
499 
500 	if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
501 		qsel = TX_DESC_QSEL_MGMT;
502 	else if (is_broadcast_ether_addr(hdr->addr1) ||
503 		 is_multicast_ether_addr(hdr->addr1))
504 		qsel = TX_DESC_QSEL_HIGH;
505 	else if (skb_get_queue_mapping(skb) <= IEEE80211_AC_BK)
506 		qsel = skb->priority;
507 	else
508 		qsel = TX_DESC_QSEL_BEACON;
509 
510 	return qsel;
511 }
512 
rtw_usb_tx_write(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)513 static int rtw_usb_tx_write(struct rtw_dev *rtwdev,
514 			    struct rtw_tx_pkt_info *pkt_info,
515 			    struct sk_buff *skb)
516 {
517 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
518 	const struct rtw_chip_info *chip = rtwdev->chip;
519 	struct rtw_usb_tx_data *tx_data;
520 	u8 *pkt_desc;
521 	int ep;
522 
523 	pkt_info->qsel = rtw_usb_tx_queue_mapping_to_qsel(skb);
524 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
525 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
526 	ep = qsel_to_ep(rtwusb, pkt_info->qsel);
527 	rtw_tx_fill_tx_desc(pkt_info, skb);
528 	rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
529 	tx_data = rtw_usb_get_tx_data(skb);
530 	tx_data->sn = pkt_info->sn;
531 
532 	skb_queue_tail(&rtwusb->tx_queue[ep], skb);
533 
534 	return 0;
535 }
536 
rtw_usb_tx_kick_off(struct rtw_dev * rtwdev)537 static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev)
538 {
539 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
540 
541 	queue_work(rtwusb->txwq, &rtwusb->tx_work);
542 }
543 
rtw_usb_rx_handler(struct work_struct * work)544 static void rtw_usb_rx_handler(struct work_struct *work)
545 {
546 	struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);
547 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
548 	const struct rtw_chip_info *chip = rtwdev->chip;
549 	u32 pkt_desc_sz = chip->rx_pkt_desc_sz;
550 	struct ieee80211_rx_status rx_status;
551 	u32 pkt_offset, next_pkt, urb_len;
552 	struct rtw_rx_pkt_stat pkt_stat;
553 	struct sk_buff *next_skb;
554 	struct sk_buff *skb;
555 	u8 *rx_desc;
556 	int limit;
557 
558 	for (limit = 0; limit < 200; limit++) {
559 		skb = skb_dequeue(&rtwusb->rx_queue);
560 		if (!skb)
561 			break;
562 
563 		if (skb_queue_len(&rtwusb->rx_queue) >= RTW_USB_MAX_RXQ_LEN) {
564 			dev_dbg_ratelimited(rtwdev->dev, "failed to get rx_queue, overflow\n");
565 			dev_kfree_skb_any(skb);
566 			continue;
567 		}
568 
569 		urb_len = skb->len;
570 
571 		do {
572 			rx_desc = skb->data;
573 			chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat,
574 						 &rx_status);
575 			pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz +
576 				     pkt_stat.shift;
577 
578 			next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8);
579 
580 			if (urb_len >= next_pkt + pkt_desc_sz)
581 				next_skb = skb_clone(skb, GFP_KERNEL);
582 			else
583 				next_skb = NULL;
584 
585 			if (pkt_stat.is_c2h) {
586 				skb_trim(skb, pkt_stat.pkt_len + pkt_offset);
587 				rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb);
588 			} else {
589 				skb_pull(skb, pkt_offset);
590 				skb_trim(skb, pkt_stat.pkt_len);
591 				rtw_update_rx_freq_for_invalid(rtwdev, skb,
592 							       &rx_status,
593 							       &pkt_stat);
594 				rtw_rx_stats(rtwdev, pkt_stat.vif, skb);
595 				memcpy(skb->cb, &rx_status, sizeof(rx_status));
596 				ieee80211_rx_irqsafe(rtwdev->hw, skb);
597 			}
598 
599 			skb = next_skb;
600 			if (skb)
601 				skb_pull(skb, next_pkt);
602 
603 			urb_len -= next_pkt;
604 		} while (skb);
605 	}
606 }
607 
608 static void rtw_usb_read_port_complete(struct urb *urb);
609 
rtw_usb_rx_resubmit(struct rtw_usb * rtwusb,struct rx_usb_ctrl_block * rxcb)610 static void rtw_usb_rx_resubmit(struct rtw_usb *rtwusb, struct rx_usb_ctrl_block *rxcb)
611 {
612 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
613 	int error;
614 
615 	rxcb->rx_skb = alloc_skb(RTW_USB_MAX_RECVBUF_SZ, GFP_ATOMIC);
616 	if (!rxcb->rx_skb)
617 		return;
618 
619 	usb_fill_bulk_urb(rxcb->rx_urb, rtwusb->udev,
620 			  usb_rcvbulkpipe(rtwusb->udev, rtwusb->pipe_in),
621 			  rxcb->rx_skb->data, RTW_USB_MAX_RECVBUF_SZ,
622 			  rtw_usb_read_port_complete, rxcb);
623 
624 	error = usb_submit_urb(rxcb->rx_urb, GFP_ATOMIC);
625 	if (error) {
626 		kfree_skb(rxcb->rx_skb);
627 		if (error != -ENODEV)
628 			rtw_err(rtwdev, "Err sending rx data urb %d\n",
629 				error);
630 	}
631 }
632 
rtw_usb_read_port_complete(struct urb * urb)633 static void rtw_usb_read_port_complete(struct urb *urb)
634 {
635 	struct rx_usb_ctrl_block *rxcb = urb->context;
636 	struct rtw_dev *rtwdev = rxcb->rtwdev;
637 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
638 	struct sk_buff *skb = rxcb->rx_skb;
639 
640 	if (urb->status == 0) {
641 		if (urb->actual_length >= RTW_USB_MAX_RECVBUF_SZ ||
642 		    urb->actual_length < 24) {
643 			rtw_err(rtwdev, "failed to get urb length:%d\n",
644 				urb->actual_length);
645 			if (skb)
646 				dev_kfree_skb_any(skb);
647 		} else {
648 			skb_put(skb, urb->actual_length);
649 			skb_queue_tail(&rtwusb->rx_queue, skb);
650 			queue_work(rtwusb->rxwq, &rtwusb->rx_work);
651 		}
652 		rtw_usb_rx_resubmit(rtwusb, rxcb);
653 	} else {
654 		switch (urb->status) {
655 		case -EINVAL:
656 		case -EPIPE:
657 		case -ENODEV:
658 		case -ESHUTDOWN:
659 		case -ENOENT:
660 		case -EPROTO:
661 		case -EILSEQ:
662 		case -ETIME:
663 		case -ECOMM:
664 		case -EOVERFLOW:
665 		case -EINPROGRESS:
666 			break;
667 		default:
668 			rtw_err(rtwdev, "status %d\n", urb->status);
669 			break;
670 		}
671 		if (skb)
672 			dev_kfree_skb_any(skb);
673 	}
674 }
675 
rtw_usb_cancel_rx_bufs(struct rtw_usb * rtwusb)676 static void rtw_usb_cancel_rx_bufs(struct rtw_usb *rtwusb)
677 {
678 	struct rx_usb_ctrl_block *rxcb;
679 	int i;
680 
681 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
682 		rxcb = &rtwusb->rx_cb[i];
683 		usb_kill_urb(rxcb->rx_urb);
684 	}
685 }
686 
rtw_usb_free_rx_bufs(struct rtw_usb * rtwusb)687 static void rtw_usb_free_rx_bufs(struct rtw_usb *rtwusb)
688 {
689 	struct rx_usb_ctrl_block *rxcb;
690 	int i;
691 
692 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
693 		rxcb = &rtwusb->rx_cb[i];
694 		usb_kill_urb(rxcb->rx_urb);
695 		usb_free_urb(rxcb->rx_urb);
696 	}
697 }
698 
rtw_usb_alloc_rx_bufs(struct rtw_usb * rtwusb)699 static int rtw_usb_alloc_rx_bufs(struct rtw_usb *rtwusb)
700 {
701 	int i;
702 
703 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
704 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
705 
706 		rxcb->rtwdev = rtwusb->rtwdev;
707 		rxcb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
708 		if (!rxcb->rx_urb)
709 			goto err;
710 	}
711 
712 	return 0;
713 err:
714 	rtw_usb_free_rx_bufs(rtwusb);
715 	return -ENOMEM;
716 }
717 
rtw_usb_setup(struct rtw_dev * rtwdev)718 static int rtw_usb_setup(struct rtw_dev *rtwdev)
719 {
720 	/* empty function for rtw_hci_ops */
721 	return 0;
722 }
723 
rtw_usb_start(struct rtw_dev * rtwdev)724 static int rtw_usb_start(struct rtw_dev *rtwdev)
725 {
726 	return 0;
727 }
728 
rtw_usb_stop(struct rtw_dev * rtwdev)729 static void rtw_usb_stop(struct rtw_dev *rtwdev)
730 {
731 }
732 
rtw_usb_deep_ps(struct rtw_dev * rtwdev,bool enter)733 static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter)
734 {
735 	/* empty function for rtw_hci_ops */
736 }
737 
rtw_usb_link_ps(struct rtw_dev * rtwdev,bool enter)738 static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter)
739 {
740 	/* empty function for rtw_hci_ops */
741 }
742 
rtw_usb_init_burst_pkt_len(struct rtw_dev * rtwdev)743 static void rtw_usb_init_burst_pkt_len(struct rtw_dev *rtwdev)
744 {
745 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
746 	enum usb_device_speed speed = rtwusb->udev->speed;
747 	u8 rxdma, burst_size;
748 
749 	rxdma = BIT_DMA_BURST_CNT | BIT_DMA_MODE;
750 
751 	if (speed == USB_SPEED_SUPER)
752 		burst_size = BIT_DMA_BURST_SIZE_1024;
753 	else if (speed == USB_SPEED_HIGH)
754 		burst_size = BIT_DMA_BURST_SIZE_512;
755 	else
756 		burst_size = BIT_DMA_BURST_SIZE_64;
757 
758 	u8p_replace_bits(&rxdma, burst_size, BIT_DMA_BURST_SIZE);
759 
760 	rtw_write8(rtwdev, REG_RXDMA_MODE, rxdma);
761 	rtw_write16_set(rtwdev, REG_TXDMA_OFFSET_CHK, BIT_DROP_DATA_EN);
762 }
763 
rtw_usb_interface_cfg(struct rtw_dev * rtwdev)764 static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev)
765 {
766 	rtw_usb_init_burst_pkt_len(rtwdev);
767 }
768 
rtw_usb_dynamic_rx_agg_v1(struct rtw_dev * rtwdev,bool enable)769 static void rtw_usb_dynamic_rx_agg_v1(struct rtw_dev *rtwdev, bool enable)
770 {
771 	u8 size, timeout;
772 	u16 val16;
773 
774 	rtw_write8_set(rtwdev, REG_TXDMA_PQ_MAP, BIT_RXDMA_AGG_EN);
775 	rtw_write8_clr(rtwdev, REG_RXDMA_AGG_PG_TH + 3, BIT(7));
776 
777 	if (enable) {
778 		size = 0x5;
779 		timeout = 0x20;
780 	} else {
781 		size = 0x0;
782 		timeout = 0x1;
783 	}
784 	val16 = u16_encode_bits(size, BIT_RXDMA_AGG_PG_TH) |
785 		u16_encode_bits(timeout, BIT_DMA_AGG_TO_V1);
786 
787 	rtw_write16(rtwdev, REG_RXDMA_AGG_PG_TH, val16);
788 }
789 
rtw_usb_dynamic_rx_agg(struct rtw_dev * rtwdev,bool enable)790 static void rtw_usb_dynamic_rx_agg(struct rtw_dev *rtwdev, bool enable)
791 {
792 	switch (rtwdev->chip->id) {
793 	case RTW_CHIP_TYPE_8822C:
794 	case RTW_CHIP_TYPE_8822B:
795 	case RTW_CHIP_TYPE_8821C:
796 		rtw_usb_dynamic_rx_agg_v1(rtwdev, enable);
797 		break;
798 	case RTW_CHIP_TYPE_8723D:
799 		/* Doesn't like aggregation. */
800 		break;
801 	case RTW_CHIP_TYPE_8703B:
802 		/* Likely not found in USB devices. */
803 		break;
804 	}
805 }
806 
807 static struct rtw_hci_ops rtw_usb_ops = {
808 	.tx_write = rtw_usb_tx_write,
809 	.tx_kick_off = rtw_usb_tx_kick_off,
810 	.setup = rtw_usb_setup,
811 	.start = rtw_usb_start,
812 	.stop = rtw_usb_stop,
813 	.deep_ps = rtw_usb_deep_ps,
814 	.link_ps = rtw_usb_link_ps,
815 	.interface_cfg = rtw_usb_interface_cfg,
816 	.dynamic_rx_agg = rtw_usb_dynamic_rx_agg,
817 
818 	.write8  = rtw_usb_write8,
819 	.write16 = rtw_usb_write16,
820 	.write32 = rtw_usb_write32,
821 	.read8	= rtw_usb_read8,
822 	.read16 = rtw_usb_read16,
823 	.read32 = rtw_usb_read32,
824 
825 	.write_data_rsvd_page = rtw_usb_write_data_rsvd_page,
826 	.write_data_h2c = rtw_usb_write_data_h2c,
827 };
828 
rtw_usb_init_rx(struct rtw_dev * rtwdev)829 static int rtw_usb_init_rx(struct rtw_dev *rtwdev)
830 {
831 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
832 
833 	rtwusb->rxwq = create_singlethread_workqueue("rtw88_usb: rx wq");
834 	if (!rtwusb->rxwq) {
835 		rtw_err(rtwdev, "failed to create RX work queue\n");
836 		return -ENOMEM;
837 	}
838 
839 	skb_queue_head_init(&rtwusb->rx_queue);
840 
841 	INIT_WORK(&rtwusb->rx_work, rtw_usb_rx_handler);
842 
843 	return 0;
844 }
845 
rtw_usb_setup_rx(struct rtw_dev * rtwdev)846 static void rtw_usb_setup_rx(struct rtw_dev *rtwdev)
847 {
848 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
849 	int i;
850 
851 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
852 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
853 
854 		rtw_usb_rx_resubmit(rtwusb, rxcb);
855 	}
856 }
857 
rtw_usb_deinit_rx(struct rtw_dev * rtwdev)858 static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev)
859 {
860 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
861 
862 	skb_queue_purge(&rtwusb->rx_queue);
863 
864 	flush_workqueue(rtwusb->rxwq);
865 	destroy_workqueue(rtwusb->rxwq);
866 }
867 
rtw_usb_init_tx(struct rtw_dev * rtwdev)868 static int rtw_usb_init_tx(struct rtw_dev *rtwdev)
869 {
870 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
871 	int i;
872 
873 	rtwusb->txwq = create_singlethread_workqueue("rtw88_usb: tx wq");
874 	if (!rtwusb->txwq) {
875 		rtw_err(rtwdev, "failed to create TX work queue\n");
876 		return -ENOMEM;
877 	}
878 
879 	for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++)
880 		skb_queue_head_init(&rtwusb->tx_queue[i]);
881 
882 	INIT_WORK(&rtwusb->tx_work, rtw_usb_tx_handler);
883 
884 	return 0;
885 }
886 
rtw_usb_deinit_tx(struct rtw_dev * rtwdev)887 static void rtw_usb_deinit_tx(struct rtw_dev *rtwdev)
888 {
889 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
890 
891 	rtw_usb_tx_queue_purge(rtwusb);
892 	flush_workqueue(rtwusb->txwq);
893 	destroy_workqueue(rtwusb->txwq);
894 }
895 
rtw_usb_intf_init(struct rtw_dev * rtwdev,struct usb_interface * intf)896 static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
897 			     struct usb_interface *intf)
898 {
899 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
900 	struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf));
901 	int ret;
902 
903 	rtwusb->udev = udev;
904 	ret = rtw_usb_parse(rtwdev, intf);
905 	if (ret)
906 		return ret;
907 
908 	rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
909 				   GFP_KERNEL);
910 	if (!rtwusb->usb_data)
911 		return -ENOMEM;
912 
913 	usb_set_intfdata(intf, rtwdev->hw);
914 
915 	SET_IEEE80211_DEV(rtwdev->hw, &intf->dev);
916 	spin_lock_init(&rtwusb->usb_lock);
917 
918 	return 0;
919 }
920 
rtw_usb_intf_deinit(struct rtw_dev * rtwdev,struct usb_interface * intf)921 static void rtw_usb_intf_deinit(struct rtw_dev *rtwdev,
922 				struct usb_interface *intf)
923 {
924 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
925 
926 	usb_put_dev(rtwusb->udev);
927 	kfree(rtwusb->usb_data);
928 	usb_set_intfdata(intf, NULL);
929 }
930 
rtw_usb_switch_mode_new(struct rtw_dev * rtwdev)931 static int rtw_usb_switch_mode_new(struct rtw_dev *rtwdev)
932 {
933 	enum usb_device_speed cur_speed;
934 	u8 id = rtwdev->chip->id;
935 	bool can_switch;
936 	u32 pad_ctrl2;
937 
938 	if (rtw_read8(rtwdev, REG_SYS_CFG2 + 3) == 0x20)
939 		cur_speed = USB_SPEED_SUPER;
940 	else
941 		cur_speed = USB_SPEED_HIGH;
942 
943 	if (cur_speed == USB_SPEED_SUPER)
944 		return 0;
945 
946 	pad_ctrl2 = rtw_read32(rtwdev, REG_PAD_CTRL2);
947 
948 	can_switch = !!(pad_ctrl2 & (BIT_MASK_USB23_SW_MODE_V1 |
949 				     BIT_USB3_USB2_TRANSITION));
950 
951 	if (!can_switch) {
952 		rtw_dbg(rtwdev, RTW_DBG_USB,
953 			"Switching to USB 3 mode unsupported by the chip\n");
954 		return 0;
955 	}
956 
957 	/* At this point cur_speed is USB_SPEED_HIGH. If we already tried
958 	 * to switch don't try again - it's a USB 2 port.
959 	 */
960 	if (u32_get_bits(pad_ctrl2, BIT_MASK_USB23_SW_MODE_V1) == BIT_USB_MODE_U3)
961 		return 0;
962 
963 	/* Enable IO wrapper timeout */
964 	if (id == RTW_CHIP_TYPE_8822B || id == RTW_CHIP_TYPE_8821C)
965 		rtw_write8_clr(rtwdev, REG_SW_MDIO + 3, BIT(0));
966 
967 	u32p_replace_bits(&pad_ctrl2, BIT_USB_MODE_U3, BIT_MASK_USB23_SW_MODE_V1);
968 	pad_ctrl2 |= BIT_RSM_EN_V1;
969 
970 	rtw_write32(rtwdev, REG_PAD_CTRL2, pad_ctrl2);
971 	rtw_write8(rtwdev, REG_PAD_CTRL2 + 1, 4);
972 
973 	rtw_write16_set(rtwdev, REG_SYS_PW_CTRL, BIT_APFM_OFFMAC);
974 	usleep_range(1000, 1001);
975 	rtw_write32_set(rtwdev, REG_PAD_CTRL2, BIT_NO_PDN_CHIPOFF_V1);
976 
977 	return 1;
978 }
979 
rtw_usb_switch_mode(struct rtw_dev * rtwdev)980 static int rtw_usb_switch_mode(struct rtw_dev *rtwdev)
981 {
982 	u8 id = rtwdev->chip->id;
983 
984 	if (id != RTW_CHIP_TYPE_8822C && id != RTW_CHIP_TYPE_8822B)
985 		return 0;
986 
987 	if (!rtwdev->efuse.usb_mode_switch) {
988 		rtw_dbg(rtwdev, RTW_DBG_USB,
989 			"Switching to USB 3 mode disabled by chip's efuse\n");
990 		return 0;
991 	}
992 
993 	if (!rtw_switch_usb_mode) {
994 		rtw_dbg(rtwdev, RTW_DBG_USB,
995 			"Switching to USB 3 mode disabled by module parameter\n");
996 		return 0;
997 	}
998 
999 	return rtw_usb_switch_mode_new(rtwdev);
1000 }
1001 
rtw_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)1002 int rtw_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1003 {
1004 	struct rtw_dev *rtwdev;
1005 	struct ieee80211_hw *hw;
1006 	struct rtw_usb *rtwusb;
1007 	int drv_data_size;
1008 	int ret;
1009 
1010 	drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_usb);
1011 	hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops);
1012 	if (!hw)
1013 		return -ENOMEM;
1014 
1015 	rtwdev = hw->priv;
1016 	rtwdev->hw = hw;
1017 	rtwdev->dev = &intf->dev;
1018 	rtwdev->chip = (struct rtw_chip_info *)id->driver_info;
1019 	rtwdev->hci.ops = &rtw_usb_ops;
1020 	rtwdev->hci.type = RTW_HCI_TYPE_USB;
1021 
1022 	rtwusb = rtw_get_usb_priv(rtwdev);
1023 	rtwusb->rtwdev = rtwdev;
1024 
1025 	ret = rtw_usb_alloc_rx_bufs(rtwusb);
1026 	if (ret)
1027 		goto err_release_hw;
1028 
1029 	ret = rtw_core_init(rtwdev);
1030 	if (ret)
1031 		goto err_free_rx_bufs;
1032 
1033 	ret = rtw_usb_intf_init(rtwdev, intf);
1034 	if (ret) {
1035 		rtw_err(rtwdev, "failed to init USB interface\n");
1036 		goto err_deinit_core;
1037 	}
1038 
1039 	ret = rtw_usb_init_tx(rtwdev);
1040 	if (ret) {
1041 		rtw_err(rtwdev, "failed to init USB TX\n");
1042 		goto err_destroy_usb;
1043 	}
1044 
1045 	ret = rtw_usb_init_rx(rtwdev);
1046 	if (ret) {
1047 		rtw_err(rtwdev, "failed to init USB RX\n");
1048 		goto err_destroy_txwq;
1049 	}
1050 
1051 	ret = rtw_chip_info_setup(rtwdev);
1052 	if (ret) {
1053 		rtw_err(rtwdev, "failed to setup chip information\n");
1054 		goto err_destroy_rxwq;
1055 	}
1056 
1057 	ret = rtw_usb_switch_mode(rtwdev);
1058 	if (ret) {
1059 		/* Not a fail, but we do need to skip rtw_register_hw. */
1060 		rtw_dbg(rtwdev, RTW_DBG_USB, "switching to USB 3 mode\n");
1061 		ret = 0;
1062 		goto err_destroy_rxwq;
1063 	}
1064 
1065 	ret = rtw_register_hw(rtwdev, rtwdev->hw);
1066 	if (ret) {
1067 		rtw_err(rtwdev, "failed to register hw\n");
1068 		goto err_destroy_rxwq;
1069 	}
1070 
1071 	rtw_usb_setup_rx(rtwdev);
1072 
1073 	return 0;
1074 
1075 err_destroy_rxwq:
1076 	rtw_usb_deinit_rx(rtwdev);
1077 
1078 err_destroy_txwq:
1079 	rtw_usb_deinit_tx(rtwdev);
1080 
1081 err_destroy_usb:
1082 	rtw_usb_intf_deinit(rtwdev, intf);
1083 
1084 err_deinit_core:
1085 	rtw_core_deinit(rtwdev);
1086 
1087 err_free_rx_bufs:
1088 	rtw_usb_free_rx_bufs(rtwusb);
1089 
1090 err_release_hw:
1091 	ieee80211_free_hw(hw);
1092 
1093 	return ret;
1094 }
1095 EXPORT_SYMBOL(rtw_usb_probe);
1096 
rtw_usb_disconnect(struct usb_interface * intf)1097 void rtw_usb_disconnect(struct usb_interface *intf)
1098 {
1099 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
1100 	struct rtw_dev *rtwdev;
1101 	struct rtw_usb *rtwusb;
1102 
1103 	if (!hw)
1104 		return;
1105 
1106 	rtwdev = hw->priv;
1107 	rtwusb = rtw_get_usb_priv(rtwdev);
1108 
1109 	rtw_usb_cancel_rx_bufs(rtwusb);
1110 
1111 	rtw_unregister_hw(rtwdev, hw);
1112 	rtw_usb_deinit_tx(rtwdev);
1113 	rtw_usb_deinit_rx(rtwdev);
1114 
1115 	if (rtwusb->udev->state != USB_STATE_NOTATTACHED)
1116 		usb_reset_device(rtwusb->udev);
1117 
1118 	rtw_usb_free_rx_bufs(rtwusb);
1119 
1120 	rtw_usb_intf_deinit(rtwdev, intf);
1121 	rtw_core_deinit(rtwdev);
1122 	ieee80211_free_hw(hw);
1123 }
1124 EXPORT_SYMBOL(rtw_usb_disconnect);
1125 
1126 MODULE_AUTHOR("Realtek Corporation");
1127 MODULE_DESCRIPTION("Realtek USB 802.11ac wireless driver");
1128 MODULE_LICENSE("Dual BSD/GPL");
1129