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