xref: /linux/drivers/net/wireless/realtek/rtw88/usb.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
427 	int i;
428 
429 	for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++)
430 		ieee80211_purge_tx_queue(rtwdev->hw, &rtwusb->tx_queue[i]);
431 }
432 
rtw_usb_write_port_complete(struct urb * urb)433 static void rtw_usb_write_port_complete(struct urb *urb)
434 {
435 	struct sk_buff *skb = urb->context;
436 
437 	dev_kfree_skb_any(skb);
438 }
439 
rtw_usb_write_data(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,u8 * buf)440 static int rtw_usb_write_data(struct rtw_dev *rtwdev,
441 			      struct rtw_tx_pkt_info *pkt_info,
442 			      u8 *buf)
443 {
444 	const struct rtw_chip_info *chip = rtwdev->chip;
445 	struct sk_buff *skb;
446 	unsigned int size;
447 	u8 qsel;
448 	int ret = 0;
449 
450 	size = pkt_info->tx_pkt_size;
451 	qsel = pkt_info->qsel;
452 
453 	skb = dev_alloc_skb(chip->tx_pkt_desc_sz + size);
454 	if (unlikely(!skb))
455 		return -ENOMEM;
456 
457 	skb_reserve(skb, chip->tx_pkt_desc_sz);
458 	skb_put_data(skb, buf, size);
459 	skb_push(skb, chip->tx_pkt_desc_sz);
460 	memset(skb->data, 0, chip->tx_pkt_desc_sz);
461 	rtw_tx_fill_tx_desc(rtwdev, pkt_info, skb);
462 	rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
463 
464 	ret = rtw_usb_write_port(rtwdev, qsel, skb,
465 				 rtw_usb_write_port_complete, skb);
466 	if (unlikely(ret))
467 		rtw_err(rtwdev, "failed to do USB write, ret=%d\n", ret);
468 
469 	return ret;
470 }
471 
rtw_usb_write_data_rsvd_page(struct rtw_dev * rtwdev,u8 * buf,u32 size)472 static int rtw_usb_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf,
473 					u32 size)
474 {
475 	const struct rtw_chip_info *chip = rtwdev->chip;
476 	struct rtw_tx_pkt_info pkt_info = {0};
477 
478 	pkt_info.tx_pkt_size = size;
479 	pkt_info.qsel = TX_DESC_QSEL_BEACON;
480 	pkt_info.offset = chip->tx_pkt_desc_sz;
481 	pkt_info.ls = true;
482 
483 	return rtw_usb_write_data(rtwdev, &pkt_info, buf);
484 }
485 
rtw_usb_write_data_h2c(struct rtw_dev * rtwdev,u8 * buf,u32 size)486 static int rtw_usb_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
487 {
488 	struct rtw_tx_pkt_info pkt_info = {0};
489 
490 	pkt_info.tx_pkt_size = size;
491 	pkt_info.qsel = TX_DESC_QSEL_H2C;
492 
493 	return rtw_usb_write_data(rtwdev, &pkt_info, buf);
494 }
495 
rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff * skb)496 static u8 rtw_usb_tx_queue_mapping_to_qsel(struct sk_buff *skb)
497 {
498 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
499 	__le16 fc = hdr->frame_control;
500 	u8 qsel;
501 
502 	if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
503 		qsel = TX_DESC_QSEL_MGMT;
504 	else if (is_broadcast_ether_addr(hdr->addr1) ||
505 		 is_multicast_ether_addr(hdr->addr1))
506 		qsel = TX_DESC_QSEL_HIGH;
507 	else if (skb_get_queue_mapping(skb) <= IEEE80211_AC_BK)
508 		qsel = skb->priority;
509 	else
510 		qsel = TX_DESC_QSEL_BEACON;
511 
512 	return qsel;
513 }
514 
rtw_usb_tx_write(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)515 static int rtw_usb_tx_write(struct rtw_dev *rtwdev,
516 			    struct rtw_tx_pkt_info *pkt_info,
517 			    struct sk_buff *skb)
518 {
519 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
520 	const struct rtw_chip_info *chip = rtwdev->chip;
521 	struct rtw_usb_tx_data *tx_data;
522 	u8 *pkt_desc;
523 	int ep;
524 
525 	pkt_info->qsel = rtw_usb_tx_queue_mapping_to_qsel(skb);
526 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
527 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
528 	ep = qsel_to_ep(rtwusb, pkt_info->qsel);
529 	rtw_tx_fill_tx_desc(rtwdev, pkt_info, skb);
530 	rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
531 	tx_data = rtw_usb_get_tx_data(skb);
532 	tx_data->sn = pkt_info->sn;
533 
534 	skb_queue_tail(&rtwusb->tx_queue[ep], skb);
535 
536 	return 0;
537 }
538 
rtw_usb_tx_kick_off(struct rtw_dev * rtwdev)539 static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev)
540 {
541 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
542 
543 	queue_work(rtwusb->txwq, &rtwusb->tx_work);
544 }
545 
rtw_usb_rx_handler(struct work_struct * work)546 static void rtw_usb_rx_handler(struct work_struct *work)
547 {
548 	struct rtw_usb *rtwusb = container_of(work, struct rtw_usb, rx_work);
549 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
550 	const struct rtw_chip_info *chip = rtwdev->chip;
551 	u32 pkt_desc_sz = chip->rx_pkt_desc_sz;
552 	struct ieee80211_rx_status rx_status;
553 	u32 pkt_offset, next_pkt, urb_len;
554 	struct rtw_rx_pkt_stat pkt_stat;
555 	struct sk_buff *next_skb;
556 	struct sk_buff *skb;
557 	u8 *rx_desc;
558 	int limit;
559 
560 	for (limit = 0; limit < 200; limit++) {
561 		skb = skb_dequeue(&rtwusb->rx_queue);
562 		if (!skb)
563 			break;
564 
565 		if (skb_queue_len(&rtwusb->rx_queue) >= RTW_USB_MAX_RXQ_LEN) {
566 			dev_dbg_ratelimited(rtwdev->dev, "failed to get rx_queue, overflow\n");
567 			dev_kfree_skb_any(skb);
568 			continue;
569 		}
570 
571 		urb_len = skb->len;
572 
573 		do {
574 			rx_desc = skb->data;
575 			rtw_rx_query_rx_desc(rtwdev, rx_desc, &pkt_stat,
576 					     &rx_status);
577 			pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz +
578 				     pkt_stat.shift;
579 
580 			next_pkt = round_up(pkt_stat.pkt_len + pkt_offset, 8);
581 
582 			if (urb_len >= next_pkt + pkt_desc_sz)
583 				next_skb = skb_clone(skb, GFP_KERNEL);
584 			else
585 				next_skb = NULL;
586 
587 			if (pkt_stat.is_c2h) {
588 				skb_trim(skb, pkt_stat.pkt_len + pkt_offset);
589 				rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb);
590 			} else {
591 				skb_pull(skb, pkt_offset);
592 				skb_trim(skb, pkt_stat.pkt_len);
593 				rtw_update_rx_freq_for_invalid(rtwdev, skb,
594 							       &rx_status,
595 							       &pkt_stat);
596 				rtw_rx_stats(rtwdev, pkt_stat.vif, skb);
597 				memcpy(skb->cb, &rx_status, sizeof(rx_status));
598 				ieee80211_rx_irqsafe(rtwdev->hw, skb);
599 			}
600 
601 			skb = next_skb;
602 			if (skb)
603 				skb_pull(skb, next_pkt);
604 
605 			urb_len -= next_pkt;
606 		} while (skb);
607 	}
608 }
609 
610 static void rtw_usb_read_port_complete(struct urb *urb);
611 
rtw_usb_rx_resubmit(struct rtw_usb * rtwusb,struct rx_usb_ctrl_block * rxcb)612 static void rtw_usb_rx_resubmit(struct rtw_usb *rtwusb, struct rx_usb_ctrl_block *rxcb)
613 {
614 	struct rtw_dev *rtwdev = rtwusb->rtwdev;
615 	int error;
616 
617 	rxcb->rx_skb = alloc_skb(RTW_USB_MAX_RECVBUF_SZ, GFP_ATOMIC);
618 	if (!rxcb->rx_skb)
619 		return;
620 
621 	usb_fill_bulk_urb(rxcb->rx_urb, rtwusb->udev,
622 			  usb_rcvbulkpipe(rtwusb->udev, rtwusb->pipe_in),
623 			  rxcb->rx_skb->data, RTW_USB_MAX_RECVBUF_SZ,
624 			  rtw_usb_read_port_complete, rxcb);
625 
626 	error = usb_submit_urb(rxcb->rx_urb, GFP_ATOMIC);
627 	if (error) {
628 		kfree_skb(rxcb->rx_skb);
629 		if (error != -ENODEV)
630 			rtw_err(rtwdev, "Err sending rx data urb %d\n",
631 				error);
632 	}
633 }
634 
rtw_usb_read_port_complete(struct urb * urb)635 static void rtw_usb_read_port_complete(struct urb *urb)
636 {
637 	struct rx_usb_ctrl_block *rxcb = urb->context;
638 	struct rtw_dev *rtwdev = rxcb->rtwdev;
639 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
640 	struct sk_buff *skb = rxcb->rx_skb;
641 
642 	if (urb->status == 0) {
643 		if (urb->actual_length >= RTW_USB_MAX_RECVBUF_SZ ||
644 		    urb->actual_length < 24) {
645 			rtw_err(rtwdev, "failed to get urb length:%d\n",
646 				urb->actual_length);
647 			if (skb)
648 				dev_kfree_skb_any(skb);
649 		} else {
650 			skb_put(skb, urb->actual_length);
651 			skb_queue_tail(&rtwusb->rx_queue, skb);
652 			queue_work(rtwusb->rxwq, &rtwusb->rx_work);
653 		}
654 		rtw_usb_rx_resubmit(rtwusb, rxcb);
655 	} else {
656 		switch (urb->status) {
657 		case -EINVAL:
658 		case -EPIPE:
659 		case -ENODEV:
660 		case -ESHUTDOWN:
661 		case -ENOENT:
662 		case -EPROTO:
663 		case -EILSEQ:
664 		case -ETIME:
665 		case -ECOMM:
666 		case -EOVERFLOW:
667 		case -EINPROGRESS:
668 			break;
669 		default:
670 			rtw_err(rtwdev, "status %d\n", urb->status);
671 			break;
672 		}
673 		if (skb)
674 			dev_kfree_skb_any(skb);
675 	}
676 }
677 
rtw_usb_cancel_rx_bufs(struct rtw_usb * rtwusb)678 static void rtw_usb_cancel_rx_bufs(struct rtw_usb *rtwusb)
679 {
680 	struct rx_usb_ctrl_block *rxcb;
681 	int i;
682 
683 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
684 		rxcb = &rtwusb->rx_cb[i];
685 		usb_kill_urb(rxcb->rx_urb);
686 	}
687 }
688 
rtw_usb_free_rx_bufs(struct rtw_usb * rtwusb)689 static void rtw_usb_free_rx_bufs(struct rtw_usb *rtwusb)
690 {
691 	struct rx_usb_ctrl_block *rxcb;
692 	int i;
693 
694 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
695 		rxcb = &rtwusb->rx_cb[i];
696 		usb_kill_urb(rxcb->rx_urb);
697 		usb_free_urb(rxcb->rx_urb);
698 	}
699 }
700 
rtw_usb_alloc_rx_bufs(struct rtw_usb * rtwusb)701 static int rtw_usb_alloc_rx_bufs(struct rtw_usb *rtwusb)
702 {
703 	int i;
704 
705 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
706 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
707 
708 		rxcb->rtwdev = rtwusb->rtwdev;
709 		rxcb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
710 		if (!rxcb->rx_urb)
711 			goto err;
712 	}
713 
714 	return 0;
715 err:
716 	rtw_usb_free_rx_bufs(rtwusb);
717 	return -ENOMEM;
718 }
719 
rtw_usb_setup(struct rtw_dev * rtwdev)720 static int rtw_usb_setup(struct rtw_dev *rtwdev)
721 {
722 	/* empty function for rtw_hci_ops */
723 	return 0;
724 }
725 
rtw_usb_start(struct rtw_dev * rtwdev)726 static int rtw_usb_start(struct rtw_dev *rtwdev)
727 {
728 	return 0;
729 }
730 
rtw_usb_stop(struct rtw_dev * rtwdev)731 static void rtw_usb_stop(struct rtw_dev *rtwdev)
732 {
733 }
734 
rtw_usb_deep_ps(struct rtw_dev * rtwdev,bool enter)735 static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter)
736 {
737 	/* empty function for rtw_hci_ops */
738 }
739 
rtw_usb_link_ps(struct rtw_dev * rtwdev,bool enter)740 static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter)
741 {
742 	/* empty function for rtw_hci_ops */
743 }
744 
rtw_usb_init_burst_pkt_len(struct rtw_dev * rtwdev)745 static void rtw_usb_init_burst_pkt_len(struct rtw_dev *rtwdev)
746 {
747 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
748 	enum usb_device_speed speed = rtwusb->udev->speed;
749 	u8 rxdma, burst_size;
750 
751 	rxdma = BIT_DMA_BURST_CNT | BIT_DMA_MODE;
752 
753 	if (speed == USB_SPEED_SUPER)
754 		burst_size = BIT_DMA_BURST_SIZE_1024;
755 	else if (speed == USB_SPEED_HIGH)
756 		burst_size = BIT_DMA_BURST_SIZE_512;
757 	else
758 		burst_size = BIT_DMA_BURST_SIZE_64;
759 
760 	u8p_replace_bits(&rxdma, burst_size, BIT_DMA_BURST_SIZE);
761 
762 	rtw_write8(rtwdev, REG_RXDMA_MODE, rxdma);
763 	rtw_write16_set(rtwdev, REG_TXDMA_OFFSET_CHK, BIT_DROP_DATA_EN);
764 }
765 
rtw_usb_interface_cfg(struct rtw_dev * rtwdev)766 static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev)
767 {
768 	rtw_usb_init_burst_pkt_len(rtwdev);
769 }
770 
rtw_usb_dynamic_rx_agg_v1(struct rtw_dev * rtwdev,bool enable)771 static void rtw_usb_dynamic_rx_agg_v1(struct rtw_dev *rtwdev, bool enable)
772 {
773 	u8 size, timeout;
774 	u16 val16;
775 
776 	rtw_write8_set(rtwdev, REG_TXDMA_PQ_MAP, BIT_RXDMA_AGG_EN);
777 	rtw_write8_clr(rtwdev, REG_RXDMA_AGG_PG_TH + 3, BIT(7));
778 
779 	if (enable) {
780 		size = 0x5;
781 		timeout = 0x20;
782 	} else {
783 		size = 0x0;
784 		timeout = 0x1;
785 	}
786 	val16 = u16_encode_bits(size, BIT_RXDMA_AGG_PG_TH) |
787 		u16_encode_bits(timeout, BIT_DMA_AGG_TO_V1);
788 
789 	rtw_write16(rtwdev, REG_RXDMA_AGG_PG_TH, val16);
790 }
791 
rtw_usb_dynamic_rx_agg(struct rtw_dev * rtwdev,bool enable)792 static void rtw_usb_dynamic_rx_agg(struct rtw_dev *rtwdev, bool enable)
793 {
794 	switch (rtwdev->chip->id) {
795 	case RTW_CHIP_TYPE_8822C:
796 	case RTW_CHIP_TYPE_8822B:
797 	case RTW_CHIP_TYPE_8821C:
798 		rtw_usb_dynamic_rx_agg_v1(rtwdev, enable);
799 		break;
800 	case RTW_CHIP_TYPE_8723D:
801 		/* Doesn't like aggregation. */
802 		break;
803 	case RTW_CHIP_TYPE_8703B:
804 		/* Likely not found in USB devices. */
805 		break;
806 	}
807 }
808 
809 static struct rtw_hci_ops rtw_usb_ops = {
810 	.tx_write = rtw_usb_tx_write,
811 	.tx_kick_off = rtw_usb_tx_kick_off,
812 	.setup = rtw_usb_setup,
813 	.start = rtw_usb_start,
814 	.stop = rtw_usb_stop,
815 	.deep_ps = rtw_usb_deep_ps,
816 	.link_ps = rtw_usb_link_ps,
817 	.interface_cfg = rtw_usb_interface_cfg,
818 	.dynamic_rx_agg = rtw_usb_dynamic_rx_agg,
819 
820 	.write8  = rtw_usb_write8,
821 	.write16 = rtw_usb_write16,
822 	.write32 = rtw_usb_write32,
823 	.read8	= rtw_usb_read8,
824 	.read16 = rtw_usb_read16,
825 	.read32 = rtw_usb_read32,
826 
827 	.write_data_rsvd_page = rtw_usb_write_data_rsvd_page,
828 	.write_data_h2c = rtw_usb_write_data_h2c,
829 };
830 
rtw_usb_init_rx(struct rtw_dev * rtwdev)831 static int rtw_usb_init_rx(struct rtw_dev *rtwdev)
832 {
833 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
834 
835 	rtwusb->rxwq = create_singlethread_workqueue("rtw88_usb: rx wq");
836 	if (!rtwusb->rxwq) {
837 		rtw_err(rtwdev, "failed to create RX work queue\n");
838 		return -ENOMEM;
839 	}
840 
841 	skb_queue_head_init(&rtwusb->rx_queue);
842 
843 	INIT_WORK(&rtwusb->rx_work, rtw_usb_rx_handler);
844 
845 	return 0;
846 }
847 
rtw_usb_setup_rx(struct rtw_dev * rtwdev)848 static void rtw_usb_setup_rx(struct rtw_dev *rtwdev)
849 {
850 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
851 	int i;
852 
853 	for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
854 		struct rx_usb_ctrl_block *rxcb = &rtwusb->rx_cb[i];
855 
856 		rtw_usb_rx_resubmit(rtwusb, rxcb);
857 	}
858 }
859 
rtw_usb_deinit_rx(struct rtw_dev * rtwdev)860 static void rtw_usb_deinit_rx(struct rtw_dev *rtwdev)
861 {
862 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
863 
864 	skb_queue_purge(&rtwusb->rx_queue);
865 
866 	flush_workqueue(rtwusb->rxwq);
867 	destroy_workqueue(rtwusb->rxwq);
868 }
869 
rtw_usb_init_tx(struct rtw_dev * rtwdev)870 static int rtw_usb_init_tx(struct rtw_dev *rtwdev)
871 {
872 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
873 	int i;
874 
875 	rtwusb->txwq = create_singlethread_workqueue("rtw88_usb: tx wq");
876 	if (!rtwusb->txwq) {
877 		rtw_err(rtwdev, "failed to create TX work queue\n");
878 		return -ENOMEM;
879 	}
880 
881 	for (i = 0; i < ARRAY_SIZE(rtwusb->tx_queue); i++)
882 		skb_queue_head_init(&rtwusb->tx_queue[i]);
883 
884 	INIT_WORK(&rtwusb->tx_work, rtw_usb_tx_handler);
885 
886 	return 0;
887 }
888 
rtw_usb_deinit_tx(struct rtw_dev * rtwdev)889 static void rtw_usb_deinit_tx(struct rtw_dev *rtwdev)
890 {
891 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
892 
893 	flush_workqueue(rtwusb->txwq);
894 	destroy_workqueue(rtwusb->txwq);
895 	rtw_usb_tx_queue_purge(rtwusb);
896 }
897 
rtw_usb_intf_init(struct rtw_dev * rtwdev,struct usb_interface * intf)898 static int rtw_usb_intf_init(struct rtw_dev *rtwdev,
899 			     struct usb_interface *intf)
900 {
901 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
902 	struct usb_device *udev = usb_get_dev(interface_to_usbdev(intf));
903 	int ret;
904 
905 	rtwusb->udev = udev;
906 	ret = rtw_usb_parse(rtwdev, intf);
907 	if (ret)
908 		return ret;
909 
910 	rtwusb->usb_data = kcalloc(RTW_USB_MAX_RXTX_COUNT, sizeof(u32),
911 				   GFP_KERNEL);
912 	if (!rtwusb->usb_data)
913 		return -ENOMEM;
914 
915 	usb_set_intfdata(intf, rtwdev->hw);
916 
917 	SET_IEEE80211_DEV(rtwdev->hw, &intf->dev);
918 	spin_lock_init(&rtwusb->usb_lock);
919 
920 	return 0;
921 }
922 
rtw_usb_intf_deinit(struct rtw_dev * rtwdev,struct usb_interface * intf)923 static void rtw_usb_intf_deinit(struct rtw_dev *rtwdev,
924 				struct usb_interface *intf)
925 {
926 	struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
927 
928 	usb_put_dev(rtwusb->udev);
929 	kfree(rtwusb->usb_data);
930 	usb_set_intfdata(intf, NULL);
931 }
932 
rtw_usb_switch_mode_new(struct rtw_dev * rtwdev)933 static int rtw_usb_switch_mode_new(struct rtw_dev *rtwdev)
934 {
935 	enum usb_device_speed cur_speed;
936 	u8 id = rtwdev->chip->id;
937 	bool can_switch;
938 	u32 pad_ctrl2;
939 
940 	if (rtw_read8(rtwdev, REG_SYS_CFG2 + 3) == 0x20)
941 		cur_speed = USB_SPEED_SUPER;
942 	else
943 		cur_speed = USB_SPEED_HIGH;
944 
945 	if (cur_speed == USB_SPEED_SUPER)
946 		return 0;
947 
948 	pad_ctrl2 = rtw_read32(rtwdev, REG_PAD_CTRL2);
949 
950 	can_switch = !!(pad_ctrl2 & (BIT_MASK_USB23_SW_MODE_V1 |
951 				     BIT_USB3_USB2_TRANSITION));
952 
953 	if (!can_switch) {
954 		rtw_dbg(rtwdev, RTW_DBG_USB,
955 			"Switching to USB 3 mode unsupported by the chip\n");
956 		return 0;
957 	}
958 
959 	/* At this point cur_speed is USB_SPEED_HIGH. If we already tried
960 	 * to switch don't try again - it's a USB 2 port.
961 	 */
962 	if (u32_get_bits(pad_ctrl2, BIT_MASK_USB23_SW_MODE_V1) == BIT_USB_MODE_U3)
963 		return 0;
964 
965 	/* Enable IO wrapper timeout */
966 	if (id == RTW_CHIP_TYPE_8822B || id == RTW_CHIP_TYPE_8821C)
967 		rtw_write8_clr(rtwdev, REG_SW_MDIO + 3, BIT(0));
968 
969 	u32p_replace_bits(&pad_ctrl2, BIT_USB_MODE_U3, BIT_MASK_USB23_SW_MODE_V1);
970 	pad_ctrl2 |= BIT_RSM_EN_V1;
971 
972 	rtw_write32(rtwdev, REG_PAD_CTRL2, pad_ctrl2);
973 	rtw_write8(rtwdev, REG_PAD_CTRL2 + 1, 4);
974 
975 	rtw_write16_set(rtwdev, REG_SYS_PW_CTRL, BIT_APFM_OFFMAC);
976 	usleep_range(1000, 1001);
977 	rtw_write32_set(rtwdev, REG_PAD_CTRL2, BIT_NO_PDN_CHIPOFF_V1);
978 
979 	return 1;
980 }
981 
rtw_usb_switch_mode(struct rtw_dev * rtwdev)982 static int rtw_usb_switch_mode(struct rtw_dev *rtwdev)
983 {
984 	u8 id = rtwdev->chip->id;
985 
986 	if (id != RTW_CHIP_TYPE_8822C && id != RTW_CHIP_TYPE_8822B)
987 		return 0;
988 
989 	if (!rtwdev->efuse.usb_mode_switch) {
990 		rtw_dbg(rtwdev, RTW_DBG_USB,
991 			"Switching to USB 3 mode disabled by chip's efuse\n");
992 		return 0;
993 	}
994 
995 	if (!rtw_switch_usb_mode) {
996 		rtw_dbg(rtwdev, RTW_DBG_USB,
997 			"Switching to USB 3 mode disabled by module parameter\n");
998 		return 0;
999 	}
1000 
1001 	return rtw_usb_switch_mode_new(rtwdev);
1002 }
1003 
rtw_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)1004 int rtw_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1005 {
1006 	struct rtw_dev *rtwdev;
1007 	struct ieee80211_hw *hw;
1008 	struct rtw_usb *rtwusb;
1009 	int drv_data_size;
1010 	int ret;
1011 
1012 	drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_usb);
1013 	hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops);
1014 	if (!hw)
1015 		return -ENOMEM;
1016 
1017 	rtwdev = hw->priv;
1018 	rtwdev->hw = hw;
1019 	rtwdev->dev = &intf->dev;
1020 	rtwdev->chip = (struct rtw_chip_info *)id->driver_info;
1021 	rtwdev->hci.ops = &rtw_usb_ops;
1022 	rtwdev->hci.type = RTW_HCI_TYPE_USB;
1023 
1024 	rtwusb = rtw_get_usb_priv(rtwdev);
1025 	rtwusb->rtwdev = rtwdev;
1026 
1027 	ret = rtw_usb_alloc_rx_bufs(rtwusb);
1028 	if (ret)
1029 		goto err_release_hw;
1030 
1031 	ret = rtw_core_init(rtwdev);
1032 	if (ret)
1033 		goto err_free_rx_bufs;
1034 
1035 	ret = rtw_usb_intf_init(rtwdev, intf);
1036 	if (ret) {
1037 		rtw_err(rtwdev, "failed to init USB interface\n");
1038 		goto err_deinit_core;
1039 	}
1040 
1041 	ret = rtw_usb_init_tx(rtwdev);
1042 	if (ret) {
1043 		rtw_err(rtwdev, "failed to init USB TX\n");
1044 		goto err_destroy_usb;
1045 	}
1046 
1047 	ret = rtw_usb_init_rx(rtwdev);
1048 	if (ret) {
1049 		rtw_err(rtwdev, "failed to init USB RX\n");
1050 		goto err_destroy_txwq;
1051 	}
1052 
1053 	ret = rtw_chip_info_setup(rtwdev);
1054 	if (ret) {
1055 		rtw_err(rtwdev, "failed to setup chip information\n");
1056 		goto err_destroy_rxwq;
1057 	}
1058 
1059 	ret = rtw_usb_switch_mode(rtwdev);
1060 	if (ret) {
1061 		/* Not a fail, but we do need to skip rtw_register_hw. */
1062 		rtw_dbg(rtwdev, RTW_DBG_USB, "switching to USB 3 mode\n");
1063 		ret = 0;
1064 		goto err_destroy_rxwq;
1065 	}
1066 
1067 	ret = rtw_register_hw(rtwdev, rtwdev->hw);
1068 	if (ret) {
1069 		rtw_err(rtwdev, "failed to register hw\n");
1070 		goto err_destroy_rxwq;
1071 	}
1072 
1073 	rtw_usb_setup_rx(rtwdev);
1074 
1075 	return 0;
1076 
1077 err_destroy_rxwq:
1078 	rtw_usb_deinit_rx(rtwdev);
1079 
1080 err_destroy_txwq:
1081 	rtw_usb_deinit_tx(rtwdev);
1082 
1083 err_destroy_usb:
1084 	rtw_usb_intf_deinit(rtwdev, intf);
1085 
1086 err_deinit_core:
1087 	rtw_core_deinit(rtwdev);
1088 
1089 err_free_rx_bufs:
1090 	rtw_usb_free_rx_bufs(rtwusb);
1091 
1092 err_release_hw:
1093 	ieee80211_free_hw(hw);
1094 
1095 	return ret;
1096 }
1097 EXPORT_SYMBOL(rtw_usb_probe);
1098 
rtw_usb_disconnect(struct usb_interface * intf)1099 void rtw_usb_disconnect(struct usb_interface *intf)
1100 {
1101 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
1102 	struct rtw_dev *rtwdev;
1103 	struct rtw_usb *rtwusb;
1104 
1105 	if (!hw)
1106 		return;
1107 
1108 	rtwdev = hw->priv;
1109 	rtwusb = rtw_get_usb_priv(rtwdev);
1110 
1111 	rtw_usb_cancel_rx_bufs(rtwusb);
1112 
1113 	rtw_unregister_hw(rtwdev, hw);
1114 	rtw_usb_deinit_tx(rtwdev);
1115 	rtw_usb_deinit_rx(rtwdev);
1116 
1117 	if (rtwusb->udev->state != USB_STATE_NOTATTACHED)
1118 		usb_reset_device(rtwusb->udev);
1119 
1120 	rtw_usb_free_rx_bufs(rtwusb);
1121 
1122 	rtw_usb_intf_deinit(rtwdev, intf);
1123 	rtw_core_deinit(rtwdev);
1124 	ieee80211_free_hw(hw);
1125 }
1126 EXPORT_SYMBOL(rtw_usb_disconnect);
1127 
1128 MODULE_AUTHOR("Realtek Corporation");
1129 MODULE_DESCRIPTION("Realtek USB 802.11ac wireless driver");
1130 MODULE_LICENSE("Dual BSD/GPL");
1131