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