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