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