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