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