xref: /linux/drivers/net/wireless/realtek/rtlwifi/usb.c (revision 385f186aba3d2f7122b71d6d4c7e236b9d4e8003)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "wifi.h"
5 #include "core.h"
6 #include "usb.h"
7 #include "base.h"
8 #include "ps.h"
9 #include "rtl8192c/fw_common.h"
10 #include <linux/export.h>
11 #include <linux/module.h>
12 
13 MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
14 MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
15 MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
16 MODULE_LICENSE("GPL");
17 MODULE_DESCRIPTION("USB basic driver for rtlwifi");
18 
19 #define	REALTEK_USB_VENQT_READ			0xC0
20 #define	REALTEK_USB_VENQT_WRITE			0x40
21 #define REALTEK_USB_VENQT_CMD_REQ		0x05
22 #define	REALTEK_USB_VENQT_CMD_IDX		0x00
23 
24 #define MAX_USBCTRL_VENDORREQ_TIMES		10
25 
26 static void _rtl_usb_cleanup_tx(struct ieee80211_hw *hw);
27 
28 static void _usbctrl_vendorreq_sync(struct usb_device *udev, u8 reqtype,
29 				   u16 value, void *pdata, u16 len)
30 {
31 	unsigned int pipe;
32 	int status;
33 	int vendorreq_times = 0;
34 	static int count;
35 
36 	if (reqtype == REALTEK_USB_VENQT_READ)
37 		pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
38 	else
39 		pipe = usb_sndctrlpipe(udev, 0); /* write_out */
40 
41 	do {
42 		status = usb_control_msg(udev, pipe, REALTEK_USB_VENQT_CMD_REQ,
43 					 reqtype, value, REALTEK_USB_VENQT_CMD_IDX,
44 					 pdata, len, 1000);
45 		if (status < 0) {
46 			/* firmware download is checksumed, don't retry */
47 			if ((value >= FW_8192C_START_ADDRESS &&
48 			    value <= FW_8192C_END_ADDRESS))
49 				break;
50 		} else {
51 			break;
52 		}
53 	} while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
54 
55 	if (status < 0 && count++ < 4)
56 		dev_err(&udev->dev, "reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x reqtype=0x%x\n",
57 		       value, status, *(u32 *)pdata, reqtype);
58 }
59 
60 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
61 {
62 	struct device *dev = rtlpriv->io.dev;
63 	struct usb_device *udev = to_usb_device(dev);
64 	u16 wvalue;
65 	__le32 *data;
66 	unsigned long flags;
67 
68 	spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
69 	if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
70 		rtlpriv->usb_data_index = 0;
71 	data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
72 	spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
73 
74 	wvalue = (u16)addr;
75 	_usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_READ, wvalue, data, len);
76 	return le32_to_cpu(*data);
77 }
78 
79 
80 static void _usb_write_sync(struct rtl_priv *rtlpriv, u32 addr, u32 val, u16 len)
81 {
82 	struct device *dev = rtlpriv->io.dev;
83 	struct usb_device *udev = to_usb_device(dev);
84 	unsigned long flags;
85 	__le32 *data;
86 	u16 wvalue;
87 
88 	spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
89 	if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
90 		rtlpriv->usb_data_index = 0;
91 	data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
92 	spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
93 
94 	wvalue = (u16)(addr & 0x0000ffff);
95 	*data = cpu_to_le32(val);
96 
97 	_usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_WRITE, wvalue, data, len);
98 }
99 
100 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
101 {
102 	return (u8)_usb_read_sync(rtlpriv, addr, 1);
103 }
104 
105 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
106 {
107 	return (u16)_usb_read_sync(rtlpriv, addr, 2);
108 }
109 
110 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
111 {
112 	return _usb_read_sync(rtlpriv, addr, 4);
113 }
114 
115 static void _usb_write8_sync(struct rtl_priv *rtlpriv, u32 addr, u8 val)
116 {
117 	_usb_write_sync(rtlpriv, addr, val, 1);
118 }
119 
120 static void _usb_write16_sync(struct rtl_priv *rtlpriv, u32 addr, u16 val)
121 {
122 	_usb_write_sync(rtlpriv, addr, val, 2);
123 }
124 
125 static void _usb_write32_sync(struct rtl_priv *rtlpriv, u32 addr, u32 val)
126 {
127 	_usb_write_sync(rtlpriv, addr, val, 4);
128 }
129 
130 static void _usb_write_chunk_sync(struct rtl_priv *rtlpriv, u32 addr,
131 				  u32 length, u8 *data)
132 {
133 	struct usb_device *udev = to_usb_device(rtlpriv->io.dev);
134 
135 	_usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_WRITE, addr, data, length);
136 }
137 
138 static void _rtl_usb_io_handler_init(struct device *dev,
139 				     struct ieee80211_hw *hw)
140 {
141 	struct rtl_priv *rtlpriv = rtl_priv(hw);
142 
143 	rtlpriv->io.dev = dev;
144 	mutex_init(&rtlpriv->io.bb_mutex);
145 	rtlpriv->io.write8	= _usb_write8_sync;
146 	rtlpriv->io.write16	= _usb_write16_sync;
147 	rtlpriv->io.write32	= _usb_write32_sync;
148 	rtlpriv->io.write_chunk	= _usb_write_chunk_sync;
149 	rtlpriv->io.read8	= _usb_read8_sync;
150 	rtlpriv->io.read16	= _usb_read16_sync;
151 	rtlpriv->io.read32	= _usb_read32_sync;
152 }
153 
154 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
155 {
156 	struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
157 
158 	mutex_destroy(&rtlpriv->io.bb_mutex);
159 }
160 
161 /*	Default aggregation handler. Do nothing and just return the oldest skb.  */
162 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
163 						  struct sk_buff_head *list)
164 {
165 	return skb_dequeue(list);
166 }
167 
168 #define IS_HIGH_SPEED_USB(udev) \
169 		((USB_SPEED_HIGH == (udev)->speed) ? true : false)
170 
171 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
172 {
173 	u32 i;
174 	struct rtl_priv *rtlpriv = rtl_priv(hw);
175 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
176 
177 	rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
178 						    ? USB_HIGH_SPEED_BULK_SIZE
179 						    : USB_FULL_SPEED_BULK_SIZE;
180 
181 	rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
182 		rtlusb->max_bulk_out_size);
183 
184 	for (i = 0; i < __RTL_TXQ_NUM; i++) {
185 		u32 ep_num = rtlusb->ep_map.ep_mapping[i];
186 
187 		if (!ep_num) {
188 			rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
189 				"Invalid endpoint map setting!\n");
190 			return -EINVAL;
191 		}
192 	}
193 
194 	rtlusb->usb_tx_post_hdl =
195 		 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
196 	rtlusb->usb_tx_cleanup	=
197 		 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
198 	rtlusb->usb_tx_aggregate_hdl =
199 		 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
200 		 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
201 		 : &_none_usb_tx_aggregate_hdl;
202 
203 	init_usb_anchor(&rtlusb->tx_submitted);
204 	for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
205 		skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
206 		init_usb_anchor(&rtlusb->tx_pending[i]);
207 	}
208 	return 0;
209 }
210 
211 static void _rtl_rx_work(struct tasklet_struct *t);
212 
213 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
214 {
215 	struct rtl_priv *rtlpriv = rtl_priv(hw);
216 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
217 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
218 
219 	rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
220 	rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
221 	rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
222 	rtlusb->usb_rx_segregate_hdl =
223 		rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
224 
225 	pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
226 		rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
227 	init_usb_anchor(&rtlusb->rx_submitted);
228 	init_usb_anchor(&rtlusb->rx_cleanup_urbs);
229 
230 	skb_queue_head_init(&rtlusb->rx_queue);
231 	tasklet_setup(&rtlusb->rx_work_tasklet, _rtl_rx_work);
232 
233 	return 0;
234 }
235 
236 static int _rtl_usb_init(struct ieee80211_hw *hw)
237 {
238 	struct rtl_priv *rtlpriv = rtl_priv(hw);
239 	struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
240 	struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
241 	int err;
242 	u8 epidx;
243 	struct usb_interface	*usb_intf = rtlusb->intf;
244 	u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
245 
246 	rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
247 	for (epidx = 0; epidx < epnums; epidx++) {
248 		struct usb_endpoint_descriptor *pep_desc;
249 
250 		pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
251 
252 		if (usb_endpoint_dir_in(pep_desc)) {
253 			if (usb_endpoint_xfer_bulk(pep_desc)) {
254 				/* The vendor drivers assume there is only one
255 				 * bulk in ep and that it's the first in ep.
256 				 */
257 				if (rtlusb->in_ep_nums == 0)
258 					rtlusb->in_ep = usb_endpoint_num(pep_desc);
259 				else
260 					pr_warn("%s: bulk in endpoint is not the first in endpoint\n",
261 						__func__);
262 			}
263 
264 			rtlusb->in_ep_nums++;
265 		} else if (usb_endpoint_dir_out(pep_desc)) {
266 			if (rtlusb->out_ep_nums < RTL_USB_MAX_BULKOUT_NUM) {
267 				if (usb_endpoint_xfer_bulk(pep_desc))
268 					rtlusb->out_eps[rtlusb->out_ep_nums] =
269 							usb_endpoint_num(pep_desc);
270 			} else {
271 				pr_warn("%s: found more bulk out endpoints than the expected %d\n",
272 					__func__, RTL_USB_MAX_BULKOUT_NUM);
273 			}
274 
275 			rtlusb->out_ep_nums++;
276 		}
277 
278 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
279 			"USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
280 			pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
281 			pep_desc->bInterval);
282 	}
283 
284 	if (rtlusb->out_ep_nums == 0) {
285 		pr_err("No output end points found\n");
286 		return -EINVAL;
287 	}
288 	/* usb endpoint mapping */
289 	err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
290 	if (err)
291 		return err;
292 
293 	rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
294 
295 	err = _rtl_usb_init_tx(hw);
296 	if (err)
297 		return err;
298 
299 	err = _rtl_usb_init_rx(hw);
300 	if (err)
301 		goto err_out;
302 
303 	return 0;
304 
305 err_out:
306 	_rtl_usb_cleanup_tx(hw);
307 	return err;
308 }
309 
310 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
311 {
312 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
313 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
314 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
315 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
316 
317 	rtlhal->hw = hw;
318 	ppsc->inactiveps = false;
319 	ppsc->leisure_ps = false;
320 	ppsc->fwctrl_lps = false;
321 	ppsc->reg_fwctrl_lps = 3;
322 	ppsc->reg_max_lps_awakeintvl = 5;
323 	ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
324 
325 	 /* IBSS */
326 	mac->beacon_interval = 100;
327 
328 	 /* AMPDU */
329 	mac->min_space_cfg = 0;
330 	mac->max_mss_density = 0;
331 
332 	/* set sane AMPDU defaults */
333 	mac->current_ampdu_density = 7;
334 	mac->current_ampdu_factor = 3;
335 
336 	/* QOS */
337 	rtlusb->acm_method = EACMWAY2_SW;
338 
339 	/* IRQ */
340 	/* HIMR - turn all on */
341 	rtlusb->irq_mask[0] = 0xFFFFFFFF;
342 	/* HIMR_EX - turn all on */
343 	rtlusb->irq_mask[1] = 0xFFFFFFFF;
344 	rtlusb->disablehwsm =  true;
345 }
346 
347 static void _rtl_rx_completed(struct urb *urb);
348 
349 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
350 			      struct urb *urb, gfp_t gfp_mask)
351 {
352 	void *buf;
353 
354 	buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
355 				 &urb->transfer_dma);
356 	if (!buf) {
357 		pr_err("Failed to usb_alloc_coherent!!\n");
358 		return -ENOMEM;
359 	}
360 
361 	usb_fill_bulk_urb(urb, rtlusb->udev,
362 			  usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
363 			  buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
364 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
365 
366 	return 0;
367 }
368 
369 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
370 				    struct sk_buff *skb)
371 {
372 	struct rtl_priv *rtlpriv = rtl_priv(hw);
373 	u8 *rxdesc = skb->data;
374 	struct ieee80211_hdr *hdr;
375 	bool unicast = false;
376 	__le16 fc;
377 	struct ieee80211_rx_status rx_status = {0};
378 	struct rtl_stats stats = {
379 		.signal = 0,
380 		.rate = 0,
381 	};
382 
383 	skb_pull(skb, RTL_RX_DESC_SIZE);
384 	rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
385 	skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
386 	hdr = rtl_get_hdr(skb);
387 	fc = hdr->frame_control;
388 	if (!stats.crc) {
389 		memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
390 
391 		if (is_broadcast_ether_addr(hdr->addr1)) {
392 			/*TODO*/;
393 		} else if (is_multicast_ether_addr(hdr->addr1)) {
394 			/*TODO*/
395 		} else {
396 			unicast = true;
397 			rtlpriv->stats.rxbytesunicast +=  skb->len;
398 		}
399 
400 		if (ieee80211_is_data(fc)) {
401 			rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
402 
403 			if (unicast)
404 				rtlpriv->link_info.num_rx_inperiod++;
405 		}
406 		/* static bcn for roaming */
407 		rtl_beacon_statistic(hw, skb);
408 	}
409 }
410 
411 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
412 				      struct sk_buff *skb)
413 {
414 	struct rtl_priv *rtlpriv = rtl_priv(hw);
415 	u8 *rxdesc = skb->data;
416 	struct ieee80211_hdr *hdr;
417 	bool unicast = false;
418 	__le16 fc;
419 	struct ieee80211_rx_status rx_status = {0};
420 	struct rtl_stats stats = {
421 		.signal = 0,
422 		.rate = 0,
423 	};
424 
425 	skb_pull(skb, RTL_RX_DESC_SIZE);
426 	rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
427 	skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
428 	hdr = rtl_get_hdr(skb);
429 	fc = hdr->frame_control;
430 	if (!stats.crc) {
431 		memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
432 
433 		if (is_broadcast_ether_addr(hdr->addr1)) {
434 			/*TODO*/;
435 		} else if (is_multicast_ether_addr(hdr->addr1)) {
436 			/*TODO*/
437 		} else {
438 			unicast = true;
439 			rtlpriv->stats.rxbytesunicast +=  skb->len;
440 		}
441 
442 		if (ieee80211_is_data(fc)) {
443 			rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
444 
445 			if (unicast)
446 				rtlpriv->link_info.num_rx_inperiod++;
447 		}
448 
449 		/* static bcn for roaming */
450 		rtl_beacon_statistic(hw, skb);
451 
452 		if (likely(rtl_action_proc(hw, skb, false)))
453 			ieee80211_rx(hw, skb);
454 		else
455 			dev_kfree_skb_any(skb);
456 	} else {
457 		dev_kfree_skb_any(skb);
458 	}
459 }
460 
461 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
462 {
463 	struct sk_buff *_skb;
464 	struct sk_buff_head rx_queue;
465 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
466 
467 	skb_queue_head_init(&rx_queue);
468 	if (rtlusb->usb_rx_segregate_hdl)
469 		rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
470 	WARN_ON(skb_queue_empty(&rx_queue));
471 	while (!skb_queue_empty(&rx_queue)) {
472 		_skb = skb_dequeue(&rx_queue);
473 		_rtl_usb_rx_process_agg(hw, _skb);
474 		ieee80211_rx(hw, _skb);
475 	}
476 }
477 
478 #define __RX_SKB_MAX_QUEUED	64
479 
480 static void _rtl_rx_work(struct tasklet_struct *t)
481 {
482 	struct rtl_usb *rtlusb = from_tasklet(rtlusb, t, rx_work_tasklet);
483 	struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
484 	struct sk_buff *skb;
485 
486 	while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
487 		if (unlikely(IS_USB_STOP(rtlusb))) {
488 			dev_kfree_skb_any(skb);
489 			continue;
490 		}
491 
492 		if (likely(!rtlusb->usb_rx_segregate_hdl)) {
493 			_rtl_usb_rx_process_noagg(hw, skb);
494 		} else {
495 			/* TO DO */
496 			_rtl_rx_pre_process(hw, skb);
497 			pr_err("rx agg not supported\n");
498 		}
499 	}
500 }
501 
502 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr,
503 					unsigned int len)
504 {
505 #if NET_IP_ALIGN != 0
506 	unsigned int padding = 0;
507 #endif
508 
509 	/* make function no-op when possible */
510 	if (NET_IP_ALIGN == 0 || len < sizeof(*hdr))
511 		return 0;
512 
513 #if NET_IP_ALIGN != 0
514 	/* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
515 	/* TODO: deduplicate common code, define helper function instead? */
516 
517 	if (ieee80211_is_data_qos(hdr->frame_control)) {
518 		u8 *qc = ieee80211_get_qos_ctl(hdr);
519 
520 		padding ^= NET_IP_ALIGN;
521 
522 		/* Input might be invalid, avoid accessing memory outside
523 		 * the buffer.
524 		 */
525 		if ((unsigned long)qc - (unsigned long)hdr < len &&
526 		    *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
527 			padding ^= NET_IP_ALIGN;
528 	}
529 
530 	if (ieee80211_has_a4(hdr->frame_control))
531 		padding ^= NET_IP_ALIGN;
532 
533 	return padding;
534 #endif
535 }
536 
537 #define __RADIO_TAP_SIZE_RSV	32
538 
539 static void _rtl_rx_completed(struct urb *_urb)
540 {
541 	struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
542 	int err = 0;
543 
544 	if (unlikely(IS_USB_STOP(rtlusb)))
545 		goto free;
546 
547 	if (likely(0 == _urb->status)) {
548 		unsigned int padding;
549 		struct sk_buff *skb;
550 		unsigned int qlen;
551 		unsigned int size = _urb->actual_length;
552 		struct ieee80211_hdr *hdr;
553 
554 		if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
555 			pr_err("Too short packet from bulk IN! (len: %d)\n",
556 			       size);
557 			goto resubmit;
558 		}
559 
560 		qlen = skb_queue_len(&rtlusb->rx_queue);
561 		if (qlen >= __RX_SKB_MAX_QUEUED) {
562 			pr_err("Pending RX skbuff queue full! (qlen: %d)\n",
563 			       qlen);
564 			goto resubmit;
565 		}
566 
567 		hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE);
568 		padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE);
569 
570 		skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding);
571 		if (!skb) {
572 			pr_err("Can't allocate skb for bulk IN!\n");
573 			goto resubmit;
574 		}
575 
576 		_rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
577 
578 		/* Make sure the payload data is 4 byte aligned. */
579 		skb_reserve(skb, padding);
580 
581 		/* reserve some space for mac80211's radiotap */
582 		skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
583 
584 		skb_put_data(skb, _urb->transfer_buffer, size);
585 
586 		skb_queue_tail(&rtlusb->rx_queue, skb);
587 		tasklet_schedule(&rtlusb->rx_work_tasklet);
588 
589 		goto resubmit;
590 	}
591 
592 	switch (_urb->status) {
593 	/* disconnect */
594 	case -ENOENT:
595 	case -ECONNRESET:
596 	case -ENODEV:
597 	case -ESHUTDOWN:
598 		goto free;
599 	default:
600 		break;
601 	}
602 
603 resubmit:
604 	usb_anchor_urb(_urb, &rtlusb->rx_submitted);
605 	err = usb_submit_urb(_urb, GFP_ATOMIC);
606 	if (unlikely(err)) {
607 		usb_unanchor_urb(_urb);
608 		goto free;
609 	}
610 	return;
611 
612 free:
613 	/* On some architectures, usb_free_coherent must not be called from
614 	 * hardirq context. Queue urb to cleanup list.
615 	 */
616 	usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
617 }
618 
619 #undef __RADIO_TAP_SIZE_RSV
620 
621 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
622 {
623 	struct rtl_priv *rtlpriv = rtl_priv(hw);
624 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
625 	struct urb *urb;
626 
627 	usb_kill_anchored_urbs(&rtlusb->rx_submitted);
628 
629 	tasklet_kill(&rtlusb->rx_work_tasklet);
630 	cancel_work_sync(&rtlpriv->works.lps_change_work);
631 
632 	skb_queue_purge(&rtlusb->rx_queue);
633 
634 	while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
635 		usb_free_coherent(urb->dev, urb->transfer_buffer_length,
636 				urb->transfer_buffer, urb->transfer_dma);
637 		usb_free_urb(urb);
638 	}
639 }
640 
641 static int _rtl_usb_receive(struct ieee80211_hw *hw)
642 {
643 	struct urb *urb;
644 	int err;
645 	int i;
646 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
647 
648 	WARN_ON(0 == rtlusb->rx_urb_num);
649 	/* 1600 == 1514 + max WLAN header + rtk info */
650 	WARN_ON(rtlusb->rx_max_size < 1600);
651 
652 	for (i = 0; i < rtlusb->rx_urb_num; i++) {
653 		err = -ENOMEM;
654 		urb = usb_alloc_urb(0, GFP_KERNEL);
655 		if (!urb)
656 			goto err_out;
657 
658 		err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
659 		if (err < 0) {
660 			pr_err("Failed to prep_rx_urb!!\n");
661 			usb_free_urb(urb);
662 			goto err_out;
663 		}
664 
665 		usb_anchor_urb(urb, &rtlusb->rx_submitted);
666 		err = usb_submit_urb(urb, GFP_KERNEL);
667 		if (err) {
668 			usb_unanchor_urb(urb);
669 			usb_free_urb(urb);
670 			goto err_out;
671 		}
672 		usb_free_urb(urb);
673 	}
674 	return 0;
675 
676 err_out:
677 	usb_kill_anchored_urbs(&rtlusb->rx_submitted);
678 	return err;
679 }
680 
681 static int rtl_usb_start(struct ieee80211_hw *hw)
682 {
683 	int err;
684 	struct rtl_priv *rtlpriv = rtl_priv(hw);
685 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
686 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
687 
688 	err = rtlpriv->cfg->ops->hw_init(hw);
689 	if (!err) {
690 		rtl_init_rx_config(hw);
691 
692 		/* Enable software */
693 		SET_USB_START(rtlusb);
694 		/* should after adapter start and interrupt enable. */
695 		set_hal_start(rtlhal);
696 
697 		/* Start bulk IN */
698 		err = _rtl_usb_receive(hw);
699 	}
700 
701 	return err;
702 }
703 
704 /*=======================  tx =========================================*/
705 static void _rtl_usb_cleanup_tx(struct ieee80211_hw *hw)
706 {
707 	u32 i;
708 	struct sk_buff *_skb;
709 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
710 	struct ieee80211_tx_info *txinfo;
711 
712 	for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
713 		while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
714 			rtlusb->usb_tx_cleanup(hw, _skb);
715 			txinfo = IEEE80211_SKB_CB(_skb);
716 			ieee80211_tx_info_clear_status(txinfo);
717 			txinfo->flags |= IEEE80211_TX_STAT_ACK;
718 			ieee80211_tx_status_irqsafe(hw, _skb);
719 		}
720 		usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
721 	}
722 	usb_kill_anchored_urbs(&rtlusb->tx_submitted);
723 }
724 
725 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
726 {
727 	_rtl_usb_cleanup_rx(hw);
728 	_rtl_usb_cleanup_tx(hw);
729 }
730 
731 /* We may add some struct into struct rtl_usb later. Do deinit here.  */
732 static void rtl_usb_deinit(struct ieee80211_hw *hw)
733 {
734 	rtl_usb_cleanup(hw);
735 }
736 
737 static void rtl_usb_stop(struct ieee80211_hw *hw)
738 {
739 	struct rtl_priv *rtlpriv = rtl_priv(hw);
740 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
741 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
742 	struct urb *urb;
743 
744 	/* should after adapter start and interrupt enable. */
745 	set_hal_stop(rtlhal);
746 	cancel_work_sync(&rtlpriv->works.fill_h2c_cmd);
747 	/* Enable software */
748 	SET_USB_STOP(rtlusb);
749 
750 	/* free pre-allocated URBs from rtl_usb_start() */
751 	usb_kill_anchored_urbs(&rtlusb->rx_submitted);
752 
753 	tasklet_kill(&rtlusb->rx_work_tasklet);
754 	cancel_work_sync(&rtlpriv->works.lps_change_work);
755 	cancel_work_sync(&rtlpriv->works.update_beacon_work);
756 
757 	flush_workqueue(rtlpriv->works.rtl_wq);
758 
759 	skb_queue_purge(&rtlusb->rx_queue);
760 
761 	while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
762 		usb_free_coherent(urb->dev, urb->transfer_buffer_length,
763 				urb->transfer_buffer, urb->transfer_dma);
764 		usb_free_urb(urb);
765 	}
766 
767 	rtlpriv->cfg->ops->hw_disable(hw);
768 }
769 
770 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
771 {
772 	int err;
773 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
774 
775 	usb_anchor_urb(_urb, &rtlusb->tx_submitted);
776 	err = usb_submit_urb(_urb, GFP_ATOMIC);
777 	if (err < 0) {
778 		struct sk_buff *skb;
779 
780 		pr_err("Failed to submit urb\n");
781 		usb_unanchor_urb(_urb);
782 		skb = (struct sk_buff *)_urb->context;
783 		kfree_skb(skb);
784 	}
785 	usb_free_urb(_urb);
786 }
787 
788 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
789 			struct sk_buff *skb)
790 {
791 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
792 	struct ieee80211_tx_info *txinfo;
793 
794 	rtlusb->usb_tx_post_hdl(hw, urb, skb);
795 	skb_pull(skb, RTL_TX_HEADER_SIZE);
796 	txinfo = IEEE80211_SKB_CB(skb);
797 	ieee80211_tx_info_clear_status(txinfo);
798 	txinfo->flags |= IEEE80211_TX_STAT_ACK;
799 
800 	if (urb->status) {
801 		pr_err("Urb has error status 0x%X\n", urb->status);
802 		goto out;
803 	}
804 	/*  TODO:	statistics */
805 out:
806 	ieee80211_tx_status_irqsafe(hw, skb);
807 	return urb->status;
808 }
809 
810 static void _rtl_tx_complete(struct urb *urb)
811 {
812 	struct sk_buff *skb = (struct sk_buff *)urb->context;
813 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
814 	struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
815 	struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
816 	int err;
817 
818 	if (unlikely(IS_USB_STOP(rtlusb)))
819 		return;
820 	err = _usb_tx_post(hw, urb, skb);
821 	if (err) {
822 		/* Ignore error and keep issuiing other urbs */
823 		return;
824 	}
825 }
826 
827 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
828 				struct sk_buff *skb, u32 ep_num)
829 {
830 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
831 	struct urb *_urb;
832 
833 	WARN_ON(NULL == skb);
834 	_urb = usb_alloc_urb(0, GFP_ATOMIC);
835 	if (!_urb)
836 		return NULL;
837 	_rtl_install_trx_info(rtlusb, skb, ep_num);
838 	usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
839 			  ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
840 	_urb->transfer_flags |= URB_ZERO_PACKET;
841 	return _urb;
842 }
843 
844 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
845 		       enum rtl_txq qnum)
846 {
847 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
848 	u32 ep_num;
849 	struct urb *_urb = NULL;
850 
851 	WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
852 	if (unlikely(IS_USB_STOP(rtlusb))) {
853 		pr_err("USB device is stopping...\n");
854 		kfree_skb(skb);
855 		return;
856 	}
857 	ep_num = rtlusb->ep_map.ep_mapping[qnum];
858 	_urb = _rtl_usb_tx_urb_setup(hw, skb, ep_num);
859 	if (unlikely(!_urb)) {
860 		pr_err("Can't allocate urb. Drop skb!\n");
861 		kfree_skb(skb);
862 		return;
863 	}
864 	_rtl_submit_tx_urb(hw, _urb);
865 }
866 
867 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
868 				   struct ieee80211_sta *sta,
869 				   struct sk_buff *skb,
870 				   u16 hw_queue)
871 {
872 	struct rtl_priv *rtlpriv = rtl_priv(hw);
873 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
874 	struct rtl_tx_desc *pdesc = NULL;
875 	struct rtl_tcb_desc tcb_desc;
876 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
877 	__le16 fc = hdr->frame_control;
878 	u8 *pda_addr = hdr->addr1;
879 
880 	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
881 	if (ieee80211_is_auth(fc)) {
882 		rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
883 	}
884 
885 	if (rtlpriv->psc.sw_ps_enabled) {
886 		if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
887 		    !ieee80211_has_pm(fc))
888 			hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
889 	}
890 
891 	rtl_action_proc(hw, skb, true);
892 	if (is_multicast_ether_addr(pda_addr))
893 		rtlpriv->stats.txbytesmulticast += skb->len;
894 	else if (is_broadcast_ether_addr(pda_addr))
895 		rtlpriv->stats.txbytesbroadcast += skb->len;
896 	else
897 		rtlpriv->stats.txbytesunicast += skb->len;
898 	rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, NULL, info, sta, skb,
899 					hw_queue, &tcb_desc);
900 	if (ieee80211_is_data(fc))
901 		rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
902 }
903 
904 static int rtl_usb_tx(struct ieee80211_hw *hw,
905 		      struct ieee80211_sta *sta,
906 		      struct sk_buff *skb,
907 		      struct rtl_tcb_desc *dummy)
908 {
909 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
910 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
911 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
912 	__le16 fc = hdr->frame_control;
913 	u16 hw_queue;
914 
915 	if (unlikely(is_hal_stop(rtlhal)))
916 		goto err_free;
917 	hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
918 	_rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
919 	_rtl_usb_transmit(hw, skb, hw_queue);
920 	return NETDEV_TX_OK;
921 
922 err_free:
923 	dev_kfree_skb_any(skb);
924 	return NETDEV_TX_OK;
925 }
926 
927 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
928 					struct ieee80211_sta *sta,
929 					struct sk_buff *skb)
930 {
931 	return false;
932 }
933 
934 static void rtl_fill_h2c_cmd_work_callback(struct work_struct *work)
935 {
936 	struct rtl_works *rtlworks =
937 	    container_of(work, struct rtl_works, fill_h2c_cmd);
938 	struct ieee80211_hw *hw = rtlworks->hw;
939 	struct rtl_priv *rtlpriv = rtl_priv(hw);
940 
941 	rtlpriv->cfg->ops->fill_h2c_cmd(hw, H2C_RA_MASK, 5, rtlpriv->rate_mask);
942 }
943 
944 static const struct rtl_intf_ops rtl_usb_ops = {
945 	.adapter_start = rtl_usb_start,
946 	.adapter_stop = rtl_usb_stop,
947 	.adapter_tx = rtl_usb_tx,
948 	.waitq_insert = rtl_usb_tx_chk_waitq_insert,
949 };
950 
951 int rtl_usb_probe(struct usb_interface *intf,
952 		  const struct usb_device_id *id,
953 		  const struct rtl_hal_cfg *rtl_hal_cfg)
954 {
955 	int err;
956 	struct ieee80211_hw *hw = NULL;
957 	struct rtl_priv *rtlpriv = NULL;
958 	struct usb_device	*udev;
959 	struct rtl_usb_priv *usb_priv;
960 
961 	hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
962 				sizeof(struct rtl_usb_priv), &rtl_ops);
963 	if (!hw) {
964 		pr_warn("rtl_usb: ieee80211 alloc failed\n");
965 		return -ENOMEM;
966 	}
967 	rtlpriv = hw->priv;
968 	rtlpriv->hw = hw;
969 	rtlpriv->usb_data = kcalloc(RTL_USB_MAX_RX_COUNT, sizeof(u32),
970 				    GFP_KERNEL);
971 	if (!rtlpriv->usb_data) {
972 		ieee80211_free_hw(hw);
973 		return -ENOMEM;
974 	}
975 
976 	/* this spin lock must be initialized early */
977 	spin_lock_init(&rtlpriv->locks.usb_lock);
978 	INIT_WORK(&rtlpriv->works.fill_h2c_cmd,
979 		  rtl_fill_h2c_cmd_work_callback);
980 	INIT_WORK(&rtlpriv->works.lps_change_work,
981 		  rtl_lps_change_work_callback);
982 	INIT_WORK(&rtlpriv->works.update_beacon_work,
983 		  rtl_update_beacon_work_callback);
984 
985 	rtlpriv->usb_data_index = 0;
986 	init_completion(&rtlpriv->firmware_loading_complete);
987 	SET_IEEE80211_DEV(hw, &intf->dev);
988 	udev = interface_to_usbdev(intf);
989 	usb_get_dev(udev);
990 	usb_priv = rtl_usbpriv(hw);
991 	memset(usb_priv, 0, sizeof(*usb_priv));
992 	usb_priv->dev.intf = intf;
993 	usb_priv->dev.udev = udev;
994 	usb_set_intfdata(intf, hw);
995 	/* For dual MAC RTL8192DU, which has two interfaces. */
996 	rtlpriv->rtlhal.interfaceindex =
997 		intf->altsetting[0].desc.bInterfaceNumber;
998 	/* init cfg & intf_ops */
999 	rtlpriv->rtlhal.interface = INTF_USB;
1000 	rtlpriv->cfg = rtl_hal_cfg;
1001 	rtlpriv->intf_ops = &rtl_usb_ops;
1002 	/* Init IO handler */
1003 	_rtl_usb_io_handler_init(&udev->dev, hw);
1004 	rtlpriv->cfg->ops->read_chip_version(hw);
1005 	/*like read eeprom and so on */
1006 	rtlpriv->cfg->ops->read_eeprom_info(hw);
1007 	err = _rtl_usb_init(hw);
1008 	if (err)
1009 		goto error_out2;
1010 	rtl_usb_init_sw(hw);
1011 	/* Init mac80211 sw */
1012 	err = rtl_init_core(hw);
1013 	if (err) {
1014 		pr_err("Can't allocate sw for mac80211\n");
1015 		goto error_out2;
1016 	}
1017 	if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1018 		pr_err("Can't init_sw_vars\n");
1019 		goto error_out;
1020 	}
1021 	rtl_init_sw_leds(hw);
1022 
1023 	err = ieee80211_register_hw(hw);
1024 	if (err) {
1025 		pr_err("Can't register mac80211 hw.\n");
1026 		goto error_init_vars;
1027 	}
1028 	rtlpriv->mac80211.mac80211_registered = 1;
1029 
1030 	set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1031 	return 0;
1032 
1033 error_init_vars:
1034 	wait_for_completion(&rtlpriv->firmware_loading_complete);
1035 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
1036 error_out:
1037 	rtl_usb_deinit(hw);
1038 	rtl_deinit_core(hw);
1039 error_out2:
1040 	_rtl_usb_io_handler_release(hw);
1041 	usb_put_dev(udev);
1042 	kfree(rtlpriv->usb_data);
1043 	ieee80211_free_hw(hw);
1044 	return -ENODEV;
1045 }
1046 EXPORT_SYMBOL(rtl_usb_probe);
1047 
1048 void rtl_usb_disconnect(struct usb_interface *intf)
1049 {
1050 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
1051 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1052 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1053 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1054 
1055 	if (unlikely(!rtlpriv))
1056 		return;
1057 	/* just in case driver is removed before firmware callback */
1058 	wait_for_completion(&rtlpriv->firmware_loading_complete);
1059 	clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1060 	/*ieee80211_unregister_hw will call ops_stop */
1061 	if (rtlmac->mac80211_registered == 1) {
1062 		ieee80211_unregister_hw(hw);
1063 		rtlmac->mac80211_registered = 0;
1064 	} else {
1065 		rtl_deinit_deferred_work(hw, false);
1066 		rtlpriv->intf_ops->adapter_stop(hw);
1067 	}
1068 	/*deinit rfkill */
1069 	/* rtl_deinit_rfkill(hw); */
1070 	rtl_usb_deinit(hw);
1071 	rtl_deinit_core(hw);
1072 	kfree(rtlpriv->usb_data);
1073 	rtlpriv->cfg->ops->deinit_sw_vars(hw);
1074 	_rtl_usb_io_handler_release(hw);
1075 	usb_put_dev(rtlusb->udev);
1076 	usb_set_intfdata(intf, NULL);
1077 	ieee80211_free_hw(hw);
1078 }
1079 EXPORT_SYMBOL(rtl_usb_disconnect);
1080 
1081 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1082 {
1083 	return 0;
1084 }
1085 EXPORT_SYMBOL(rtl_usb_suspend);
1086 
1087 int rtl_usb_resume(struct usb_interface *pusb_intf)
1088 {
1089 	return 0;
1090 }
1091 EXPORT_SYMBOL(rtl_usb_resume);
1092