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