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