a7b54f631293b62997dec2f9774336a1b452650d
[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, 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 }
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 }
481 }
482
483 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
484 struct sk_buff *skb)
485 {
486 struct rtl_priv *rtlpriv = rtl_priv(hw);
487 u8 *rxdesc = skb->data;
488 struct ieee80211_hdr *hdr;
489 bool unicast = false;
490 __le16 fc;
491 struct ieee80211_rx_status rx_status = {0};
492 struct rtl_stats stats = {
493 .signal = 0,
494 .noise = -98,
495 .rate = 0,
496 };
497
498 skb_pull(skb, RTL_RX_DESC_SIZE);
499 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
500 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
501 hdr = (struct ieee80211_hdr *)(skb->data);
502 fc = hdr->frame_control;
503 if (!stats.crc) {
504 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
505
506 if (is_broadcast_ether_addr(hdr->addr1)) {
507 /*TODO*/;
508 } else if (is_multicast_ether_addr(hdr->addr1)) {
509 /*TODO*/
510 } else {
511 unicast = true;
512 rtlpriv->stats.rxbytesunicast += skb->len;
513 }
514
515 rtl_is_special_data(hw, skb, false);
516
517 if (ieee80211_is_data(fc)) {
518 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
519
520 if (unicast)
521 rtlpriv->link_info.num_rx_inperiod++;
522 }
523
524 if (likely(rtl_action_proc(hw, skb, false)))
525 ieee80211_rx(hw, skb);
526 else
527 dev_kfree_skb_any(skb);
528 }
529 }
530
531 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
532 {
533 struct sk_buff *_skb;
534 struct sk_buff_head rx_queue;
535 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
536
537 skb_queue_head_init(&rx_queue);
538 if (rtlusb->usb_rx_segregate_hdl)
539 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
540 WARN_ON(skb_queue_empty(&rx_queue));
541 while (!skb_queue_empty(&rx_queue)) {
542 _skb = skb_dequeue(&rx_queue);
543 _rtl_usb_rx_process_agg(hw, _skb);
544 ieee80211_rx(hw, _skb);
545 }
546 }
547
548 #define __RX_SKB_MAX_QUEUED 32
549
550 static void _rtl_rx_work(unsigned long param)
551 {
552 struct rtl_usb *rtlusb = (struct rtl_usb *)param;
553 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
554 struct sk_buff *skb;
555
556 while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
557 if (unlikely(IS_USB_STOP(rtlusb))) {
558 dev_kfree_skb_any(skb);
559 continue;
560 }
561
562 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
563 _rtl_usb_rx_process_noagg(hw, skb);
564 } else {
565 /* TO DO */
566 _rtl_rx_pre_process(hw, skb);
567 pr_err("rx agg not supported\n");
568 }
569 }
570 }
571
572 #define __RADIO_TAP_SIZE_RSV 32
573
574 static void _rtl_rx_completed(struct urb *_urb)
575 {
576 struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
577 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
578 struct rtl_priv *rtlpriv = rtl_priv(hw);
579 int err = 0;
580
581 if (unlikely(IS_USB_STOP(rtlusb)))
582 goto free;
583
584 if (likely(0 == _urb->status)) {
585 struct sk_buff *skb;
586 unsigned int qlen;
587 unsigned int size = _urb->actual_length;
588
589 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
590 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
591 "Too short packet from bulk IN! (len: %d)\n",
592 size);
593 goto resubmit;
594 }
595
596 qlen = skb_queue_len(&rtlusb->rx_queue);
597 if (qlen >= __RX_SKB_MAX_QUEUED) {
598 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
599 "Pending RX skbuff queue full! (qlen: %d)\n",
600 qlen);
601 goto resubmit;
602 }
603
604 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV);
605 if (!skb) {
606 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
607 "Can't allocate skb for bulk IN!\n");
608 goto resubmit;
609 }
610
611 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
612
613 /* reserve some space for mac80211's radiotap */
614 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
615
616 memcpy(skb_put(skb, size), _urb->transfer_buffer, size);
617
618 skb_queue_tail(&rtlusb->rx_queue, skb);
619 tasklet_schedule(&rtlusb->rx_work_tasklet);
620
621 goto resubmit;
622 }
623
624 switch (_urb->status) {
625 /* disconnect */
626 case -ENOENT:
627 case -ECONNRESET:
628 case -ENODEV:
629 case -ESHUTDOWN:
630 goto free;
631 default:
632 break;
633 }
634
635 resubmit:
636 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
637 err = usb_submit_urb(_urb, GFP_ATOMIC);
638 if (unlikely(err)) {
639 usb_unanchor_urb(_urb);
640 goto free;
641 }
642 return;
643
644 free:
645 /* On some architectures, usb_free_coherent must not be called from
646 * hardirq context. Queue urb to cleanup list.
647 */
648 usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
649 }
650
651 #undef __RADIO_TAP_SIZE_RSV
652
653 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
654 {
655 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
656 struct urb *urb;
657
658 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
659
660 tasklet_kill(&rtlusb->rx_work_tasklet);
661 skb_queue_purge(&rtlusb->rx_queue);
662
663 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
664 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
665 urb->transfer_buffer, urb->transfer_dma);
666 usb_free_urb(urb);
667 }
668 }
669
670 static int _rtl_usb_receive(struct ieee80211_hw *hw)
671 {
672 struct urb *urb;
673 int err;
674 int i;
675 struct rtl_priv *rtlpriv = rtl_priv(hw);
676 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
677
678 WARN_ON(0 == rtlusb->rx_urb_num);
679 /* 1600 == 1514 + max WLAN header + rtk info */
680 WARN_ON(rtlusb->rx_max_size < 1600);
681
682 for (i = 0; i < rtlusb->rx_urb_num; i++) {
683 err = -ENOMEM;
684 urb = usb_alloc_urb(0, GFP_KERNEL);
685 if (!urb) {
686 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
687 "Failed to alloc URB!!\n");
688 goto err_out;
689 }
690
691 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
692 if (err < 0) {
693 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
694 "Failed to prep_rx_urb!!\n");
695 usb_free_urb(urb);
696 goto err_out;
697 }
698
699 usb_anchor_urb(urb, &rtlusb->rx_submitted);
700 err = usb_submit_urb(urb, GFP_KERNEL);
701 if (err)
702 goto err_out;
703 usb_free_urb(urb);
704 }
705 return 0;
706
707 err_out:
708 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
709 _rtl_usb_cleanup_rx(hw);
710 return err;
711 }
712
713 static int rtl_usb_start(struct ieee80211_hw *hw)
714 {
715 int err;
716 struct rtl_priv *rtlpriv = rtl_priv(hw);
717 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
718 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
719
720 err = rtlpriv->cfg->ops->hw_init(hw);
721 if (!err) {
722 rtl_init_rx_config(hw);
723
724 /* Enable software */
725 SET_USB_START(rtlusb);
726 /* should after adapter start and interrupt enable. */
727 set_hal_start(rtlhal);
728
729 /* Start bulk IN */
730 err = _rtl_usb_receive(hw);
731 }
732
733 return err;
734 }
735 /**
736 *
737 *
738 */
739
740 /*======================= tx =========================================*/
741 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
742 {
743 u32 i;
744 struct sk_buff *_skb;
745 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
746 struct ieee80211_tx_info *txinfo;
747
748 SET_USB_STOP(rtlusb);
749
750 /* clean up rx stuff. */
751 _rtl_usb_cleanup_rx(hw);
752
753 /* clean up tx stuff */
754 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
755 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
756 rtlusb->usb_tx_cleanup(hw, _skb);
757 txinfo = IEEE80211_SKB_CB(_skb);
758 ieee80211_tx_info_clear_status(txinfo);
759 txinfo->flags |= IEEE80211_TX_STAT_ACK;
760 ieee80211_tx_status_irqsafe(hw, _skb);
761 }
762 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
763 }
764 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
765 }
766
767 /**
768 *
769 * We may add some struct into struct rtl_usb later. Do deinit here.
770 *
771 */
772 static void rtl_usb_deinit(struct ieee80211_hw *hw)
773 {
774 rtl_usb_cleanup(hw);
775 }
776
777 static void rtl_usb_stop(struct ieee80211_hw *hw)
778 {
779 struct rtl_priv *rtlpriv = rtl_priv(hw);
780 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
781 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
782
783 /* should after adapter start and interrupt enable. */
784 set_hal_stop(rtlhal);
785 /* Enable software */
786 SET_USB_STOP(rtlusb);
787 rtl_usb_deinit(hw);
788 rtlpriv->cfg->ops->hw_disable(hw);
789 }
790
791 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
792 {
793 int err;
794 struct rtl_priv *rtlpriv = rtl_priv(hw);
795 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
796
797 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
798 err = usb_submit_urb(_urb, GFP_ATOMIC);
799 if (err < 0) {
800 struct sk_buff *skb;
801
802 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
803 "Failed to submit urb\n");
804 usb_unanchor_urb(_urb);
805 skb = (struct sk_buff *)_urb->context;
806 kfree_skb(skb);
807 }
808 usb_free_urb(_urb);
809 }
810
811 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
812 struct sk_buff *skb)
813 {
814 struct rtl_priv *rtlpriv = rtl_priv(hw);
815 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
816 struct ieee80211_tx_info *txinfo;
817
818 rtlusb->usb_tx_post_hdl(hw, urb, skb);
819 skb_pull(skb, RTL_TX_HEADER_SIZE);
820 txinfo = IEEE80211_SKB_CB(skb);
821 ieee80211_tx_info_clear_status(txinfo);
822 txinfo->flags |= IEEE80211_TX_STAT_ACK;
823
824 if (urb->status) {
825 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
826 "Urb has error status 0x%X\n", urb->status);
827 goto out;
828 }
829 /* TODO: statistics */
830 out:
831 ieee80211_tx_status_irqsafe(hw, skb);
832 return urb->status;
833 }
834
835 static void _rtl_tx_complete(struct urb *urb)
836 {
837 struct sk_buff *skb = (struct sk_buff *)urb->context;
838 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
839 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
840 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
841 int err;
842
843 if (unlikely(IS_USB_STOP(rtlusb)))
844 return;
845 err = _usb_tx_post(hw, urb, skb);
846 if (err) {
847 /* Ignore error and keep issuiing other urbs */
848 return;
849 }
850 }
851
852 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
853 struct sk_buff *skb, u32 ep_num)
854 {
855 struct rtl_priv *rtlpriv = rtl_priv(hw);
856 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
857 struct urb *_urb;
858
859 WARN_ON(NULL == skb);
860 _urb = usb_alloc_urb(0, GFP_ATOMIC);
861 if (!_urb) {
862 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
863 "Can't allocate URB for bulk out!\n");
864 kfree_skb(skb);
865 return NULL;
866 }
867 _rtl_install_trx_info(rtlusb, skb, ep_num);
868 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
869 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
870 _urb->transfer_flags |= URB_ZERO_PACKET;
871 return _urb;
872 }
873
874 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
875 enum rtl_txq qnum)
876 {
877 struct rtl_priv *rtlpriv = rtl_priv(hw);
878 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
879 u32 ep_num;
880 struct urb *_urb = NULL;
881 struct sk_buff *_skb = NULL;
882
883 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
884 if (unlikely(IS_USB_STOP(rtlusb))) {
885 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
886 "USB device is stopping...\n");
887 kfree_skb(skb);
888 return;
889 }
890 ep_num = rtlusb->ep_map.ep_mapping[qnum];
891 _skb = skb;
892 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
893 if (unlikely(!_urb)) {
894 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
895 "Can't allocate urb. Drop skb!\n");
896 return;
897 }
898 _rtl_submit_tx_urb(hw, _urb);
899 }
900
901 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
902 struct ieee80211_sta *sta,
903 struct sk_buff *skb,
904 u16 hw_queue)
905 {
906 struct rtl_priv *rtlpriv = rtl_priv(hw);
907 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
908 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
909 struct rtl_tx_desc *pdesc = NULL;
910 struct rtl_tcb_desc tcb_desc;
911 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
912 __le16 fc = hdr->frame_control;
913 u8 *pda_addr = hdr->addr1;
914 /* ssn */
915 u8 *qc = NULL;
916 u8 tid = 0;
917 u16 seq_number = 0;
918
919 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
920 if (ieee80211_is_auth(fc)) {
921 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
922 rtl_ips_nic_on(hw);
923 }
924
925 if (rtlpriv->psc.sw_ps_enabled) {
926 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
927 !ieee80211_has_pm(fc))
928 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
929 }
930
931 rtl_action_proc(hw, skb, true);
932 if (is_multicast_ether_addr(pda_addr))
933 rtlpriv->stats.txbytesmulticast += skb->len;
934 else if (is_broadcast_ether_addr(pda_addr))
935 rtlpriv->stats.txbytesbroadcast += skb->len;
936 else
937 rtlpriv->stats.txbytesunicast += skb->len;
938 if (ieee80211_is_data_qos(fc)) {
939 qc = ieee80211_get_qos_ctl(hdr);
940 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
941 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
942 IEEE80211_SCTL_SEQ) >> 4;
943 seq_number += 1;
944 seq_number <<= 4;
945 }
946 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
947 hw_queue, &tcb_desc);
948 if (!ieee80211_has_morefrags(hdr->frame_control)) {
949 if (qc)
950 mac->tids[tid].seq_number = seq_number;
951 }
952 if (ieee80211_is_data(fc))
953 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
954 }
955
956 static int rtl_usb_tx(struct ieee80211_hw *hw,
957 struct ieee80211_sta *sta,
958 struct sk_buff *skb,
959 struct rtl_tcb_desc *dummy)
960 {
961 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
962 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
963 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
964 __le16 fc = hdr->frame_control;
965 u16 hw_queue;
966
967 if (unlikely(is_hal_stop(rtlhal)))
968 goto err_free;
969 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
970 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
971 _rtl_usb_transmit(hw, skb, hw_queue);
972 return NETDEV_TX_OK;
973
974 err_free:
975 dev_kfree_skb_any(skb);
976 return NETDEV_TX_OK;
977 }
978
979 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
980 struct ieee80211_sta *sta,
981 struct sk_buff *skb)
982 {
983 return false;
984 }
985
986 static struct rtl_intf_ops rtl_usb_ops = {
987 .adapter_start = rtl_usb_start,
988 .adapter_stop = rtl_usb_stop,
989 .adapter_tx = rtl_usb_tx,
990 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
991 };
992
993 int rtl_usb_probe(struct usb_interface *intf,
994 const struct usb_device_id *id,
995 struct rtl_hal_cfg *rtl_hal_cfg)
996 {
997 int err;
998 struct ieee80211_hw *hw = NULL;
999 struct rtl_priv *rtlpriv = NULL;
1000 struct usb_device *udev;
1001 struct rtl_usb_priv *usb_priv;
1002
1003 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
1004 sizeof(struct rtl_usb_priv), &rtl_ops);
1005 if (!hw) {
1006 RT_ASSERT(false, "ieee80211 alloc failed\n");
1007 return -ENOMEM;
1008 }
1009 rtlpriv = hw->priv;
1010 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
1011 GFP_KERNEL);
1012 if (!rtlpriv->usb_data)
1013 return -ENOMEM;
1014
1015 /* this spin lock must be initialized early */
1016 spin_lock_init(&rtlpriv->locks.usb_lock);
1017
1018 rtlpriv->usb_data_index = 0;
1019 init_completion(&rtlpriv->firmware_loading_complete);
1020 SET_IEEE80211_DEV(hw, &intf->dev);
1021 udev = interface_to_usbdev(intf);
1022 usb_get_dev(udev);
1023 usb_priv = rtl_usbpriv(hw);
1024 memset(usb_priv, 0, sizeof(*usb_priv));
1025 usb_priv->dev.intf = intf;
1026 usb_priv->dev.udev = udev;
1027 usb_set_intfdata(intf, hw);
1028 /* init cfg & intf_ops */
1029 rtlpriv->rtlhal.interface = INTF_USB;
1030 rtlpriv->cfg = rtl_hal_cfg;
1031 rtlpriv->intf_ops = &rtl_usb_ops;
1032 rtl_dbgp_flag_init(hw);
1033 /* Init IO handler */
1034 _rtl_usb_io_handler_init(&udev->dev, hw);
1035 rtlpriv->cfg->ops->read_chip_version(hw);
1036 /*like read eeprom and so on */
1037 rtlpriv->cfg->ops->read_eeprom_info(hw);
1038 err = _rtl_usb_init(hw);
1039 if (err)
1040 goto error_out;
1041 rtl_usb_init_sw(hw);
1042 /* Init mac80211 sw */
1043 err = rtl_init_core(hw);
1044 if (err) {
1045 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1046 "Can't allocate sw for mac80211\n");
1047 goto error_out;
1048 }
1049 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1050 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1051 goto error_out;
1052 }
1053 rtlpriv->cfg->ops->init_sw_leds(hw);
1054
1055 return 0;
1056 error_out:
1057 rtl_deinit_core(hw);
1058 _rtl_usb_io_handler_release(hw);
1059 usb_put_dev(udev);
1060 complete(&rtlpriv->firmware_loading_complete);
1061 return -ENODEV;
1062 }
1063 EXPORT_SYMBOL(rtl_usb_probe);
1064
1065 void rtl_usb_disconnect(struct usb_interface *intf)
1066 {
1067 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1068 struct rtl_priv *rtlpriv = rtl_priv(hw);
1069 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1070 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1071
1072 if (unlikely(!rtlpriv))
1073 return;
1074
1075 /* just in case driver is removed before firmware callback */
1076 wait_for_completion(&rtlpriv->firmware_loading_complete);
1077 /*ieee80211_unregister_hw will call ops_stop */
1078 if (rtlmac->mac80211_registered == 1) {
1079 ieee80211_unregister_hw(hw);
1080 rtlmac->mac80211_registered = 0;
1081 } else {
1082 rtl_deinit_deferred_work(hw);
1083 rtlpriv->intf_ops->adapter_stop(hw);
1084 }
1085 /*deinit rfkill */
1086 /* rtl_deinit_rfkill(hw); */
1087 rtl_usb_deinit(hw);
1088 rtl_deinit_core(hw);
1089 kfree(rtlpriv->usb_data);
1090 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1091 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1092 _rtl_usb_io_handler_release(hw);
1093 usb_put_dev(rtlusb->udev);
1094 usb_set_intfdata(intf, NULL);
1095 ieee80211_free_hw(hw);
1096 }
1097 EXPORT_SYMBOL(rtl_usb_disconnect);
1098
1099 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1100 {
1101 return 0;
1102 }
1103 EXPORT_SYMBOL(rtl_usb_suspend);
1104
1105 int rtl_usb_resume(struct usb_interface *pusb_intf)
1106 {
1107 return 0;
1108 }
1109 EXPORT_SYMBOL(rtl_usb_resume);