rtlwifi: usb: use usb_alloc_coherent for RX buffers
[firefly-linux-kernel-4.4.55.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 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
324         pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
325                 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
326         init_usb_anchor(&rtlusb->rx_submitted);
327         init_usb_anchor(&rtlusb->rx_cleanup_urbs);
328         return 0;
329 }
330
331 static int _rtl_usb_init(struct ieee80211_hw *hw)
332 {
333         struct rtl_priv *rtlpriv = rtl_priv(hw);
334         struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
335         struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
336         int err;
337         u8 epidx;
338         struct usb_interface    *usb_intf = rtlusb->intf;
339         u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
340
341         rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
342         for (epidx = 0; epidx < epnums; epidx++) {
343                 struct usb_endpoint_descriptor *pep_desc;
344                 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
345
346                 if (usb_endpoint_dir_in(pep_desc))
347                         rtlusb->in_ep_nums++;
348                 else if (usb_endpoint_dir_out(pep_desc))
349                         rtlusb->out_ep_nums++;
350
351                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
352                          "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
353                          pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
354                          pep_desc->bInterval);
355         }
356         if (rtlusb->in_ep_nums <  rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
357                 pr_err("Too few input end points found\n");
358                 return -EINVAL;
359         }
360         if (rtlusb->out_ep_nums == 0) {
361                 pr_err("No output end points found\n");
362                 return -EINVAL;
363         }
364         /* usb endpoint mapping */
365         err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
366         rtlusb->usb_mq_to_hwq =  rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
367         _rtl_usb_init_tx(hw);
368         _rtl_usb_init_rx(hw);
369         return err;
370 }
371
372 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
373 {
374         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
375         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
376         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
377         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
378
379         rtlhal->hw = hw;
380         ppsc->inactiveps = false;
381         ppsc->leisure_ps = false;
382         ppsc->fwctrl_lps = false;
383         ppsc->reg_fwctrl_lps = 3;
384         ppsc->reg_max_lps_awakeintvl = 5;
385         ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
386
387          /* IBSS */
388         mac->beacon_interval = 100;
389
390          /* AMPDU */
391         mac->min_space_cfg = 0;
392         mac->max_mss_density = 0;
393
394         /* set sane AMPDU defaults */
395         mac->current_ampdu_density = 7;
396         mac->current_ampdu_factor = 3;
397
398         /* QOS */
399         rtlusb->acm_method = eAcmWay2_SW;
400
401         /* IRQ */
402         /* HIMR - turn all on */
403         rtlusb->irq_mask[0] = 0xFFFFFFFF;
404         /* HIMR_EX - turn all on */
405         rtlusb->irq_mask[1] = 0xFFFFFFFF;
406         rtlusb->disableHWSM =  true;
407 }
408
409 static void _rtl_rx_completed(struct urb *urb);
410
411 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
412                               struct urb *urb, gfp_t gfp_mask)
413 {
414         struct rtl_priv *rtlpriv = rtl_priv(hw);
415         void *buf;
416
417         buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
418                                  &urb->transfer_dma);
419         if (!buf) {
420                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
421                          "Failed to usb_alloc_coherent!!\n");
422                 return -ENOMEM;
423         }
424
425         usb_fill_bulk_urb(urb, rtlusb->udev,
426                           usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
427                           buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
428         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
429
430         return 0;
431 }
432
433 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
434                                     struct sk_buff *skb)
435 {
436         struct rtl_priv *rtlpriv = rtl_priv(hw);
437         u8 *rxdesc = skb->data;
438         struct ieee80211_hdr *hdr;
439         bool unicast = false;
440         __le16 fc;
441         struct ieee80211_rx_status rx_status = {0};
442         struct rtl_stats stats = {
443                 .signal = 0,
444                 .noise = -98,
445                 .rate = 0,
446         };
447
448         skb_pull(skb, RTL_RX_DESC_SIZE);
449         rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
450         skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
451         hdr = (struct ieee80211_hdr *)(skb->data);
452         fc = hdr->frame_control;
453         if (!stats.crc) {
454                 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
455
456                 if (is_broadcast_ether_addr(hdr->addr1)) {
457                         /*TODO*/;
458                 } else if (is_multicast_ether_addr(hdr->addr1)) {
459                         /*TODO*/
460                 } else {
461                         unicast = true;
462                         rtlpriv->stats.rxbytesunicast +=  skb->len;
463                 }
464
465                 rtl_is_special_data(hw, skb, false);
466
467                 if (ieee80211_is_data(fc)) {
468                         rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
469
470                         if (unicast)
471                                 rtlpriv->link_info.num_rx_inperiod++;
472                 }
473         }
474 }
475
476 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
477                                       struct sk_buff *skb)
478 {
479         struct rtl_priv *rtlpriv = rtl_priv(hw);
480         u8 *rxdesc = skb->data;
481         struct ieee80211_hdr *hdr;
482         bool unicast = false;
483         __le16 fc;
484         struct ieee80211_rx_status rx_status = {0};
485         struct rtl_stats stats = {
486                 .signal = 0,
487                 .noise = -98,
488                 .rate = 0,
489         };
490
491         skb_pull(skb, RTL_RX_DESC_SIZE);
492         rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
493         skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
494         hdr = (struct ieee80211_hdr *)(skb->data);
495         fc = hdr->frame_control;
496         if (!stats.crc) {
497                 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
498
499                 if (is_broadcast_ether_addr(hdr->addr1)) {
500                         /*TODO*/;
501                 } else if (is_multicast_ether_addr(hdr->addr1)) {
502                         /*TODO*/
503                 } else {
504                         unicast = true;
505                         rtlpriv->stats.rxbytesunicast +=  skb->len;
506                 }
507
508                 rtl_is_special_data(hw, skb, false);
509
510                 if (ieee80211_is_data(fc)) {
511                         rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
512
513                         if (unicast)
514                                 rtlpriv->link_info.num_rx_inperiod++;
515                 }
516                 if (likely(rtl_action_proc(hw, skb, false))) {
517                         struct sk_buff *uskb = NULL;
518                         u8 *pdata;
519
520                         uskb = dev_alloc_skb(skb->len + 128);
521                         if (uskb) {     /* drop packet on allocation failure */
522                                 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
523                                        sizeof(rx_status));
524                                 pdata = (u8 *)skb_put(uskb, skb->len);
525                                 memcpy(pdata, skb->data, skb->len);
526                                 ieee80211_rx_irqsafe(hw, uskb);
527                         }
528                         dev_kfree_skb_any(skb);
529                 } else {
530                         dev_kfree_skb_any(skb);
531                 }
532         }
533 }
534
535 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
536 {
537         struct sk_buff *_skb;
538         struct sk_buff_head rx_queue;
539         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
540
541         skb_queue_head_init(&rx_queue);
542         if (rtlusb->usb_rx_segregate_hdl)
543                 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
544         WARN_ON(skb_queue_empty(&rx_queue));
545         while (!skb_queue_empty(&rx_queue)) {
546                 _skb = skb_dequeue(&rx_queue);
547                 _rtl_usb_rx_process_agg(hw, _skb);
548                 ieee80211_rx_irqsafe(hw, _skb);
549         }
550 }
551
552 #define __RADIO_TAP_SIZE_RSV    32
553
554 static void _rtl_rx_completed(struct urb *_urb)
555 {
556         struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
557         struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
558         struct rtl_priv *rtlpriv = rtl_priv(hw);
559         int err = 0;
560
561         if (unlikely(IS_USB_STOP(rtlusb)))
562                 goto free;
563
564         if (likely(0 == _urb->status)) {
565                 struct sk_buff *skb;
566                 unsigned int size = _urb->actual_length;
567
568                 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
569                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
570                                  "Too short packet from bulk IN! (len: %d)\n",
571                                  size);
572                         goto resubmit;
573                 }
574
575                 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV);
576                 if (!skb) {
577                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
578                                  "Can't allocate skb for bulk IN!\n");
579                         goto resubmit;
580                 }
581
582                 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
583
584                 /* reserve some space for mac80211's radiotap */
585                 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
586
587                 memcpy(skb_put(skb, size), _urb->transfer_buffer, size);
588
589                 /* TODO: Do further processing in tasklet (queue skbs,
590                  * schedule tasklet)
591                  */
592
593                 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
594                         _rtl_usb_rx_process_noagg(hw, skb);
595                 } else {
596                         /* TO DO */
597                         _rtl_rx_pre_process(hw, skb);
598                         pr_err("rx agg not supported\n");
599                 }
600
601                 goto resubmit;
602         }
603
604         switch (_urb->status) {
605         /* disconnect */
606         case -ENOENT:
607         case -ECONNRESET:
608         case -ENODEV:
609         case -ESHUTDOWN:
610                 goto free;
611         default:
612                 break;
613         }
614
615 resubmit:
616         usb_anchor_urb(_urb, &rtlusb->rx_submitted);
617         err = usb_submit_urb(_urb, GFP_ATOMIC);
618         if (unlikely(err)) {
619                 usb_unanchor_urb(_urb);
620                 goto free;
621         }
622         return;
623
624 free:
625         /* On some architectures, usb_free_coherent must not be called from
626          * hardirq context. Queue urb to cleanup list.
627          */
628         usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
629 }
630
631 #undef __RADIO_TAP_SIZE_RSV
632
633 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
634 {
635         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
636         struct urb *urb;
637
638         usb_kill_anchored_urbs(&rtlusb->rx_submitted);
639
640         while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
641                 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
642                                 urb->transfer_buffer, urb->transfer_dma);
643                 usb_free_urb(urb);
644         }
645 }
646
647 static int _rtl_usb_receive(struct ieee80211_hw *hw)
648 {
649         struct urb *urb;
650         int err;
651         int i;
652         struct rtl_priv *rtlpriv = rtl_priv(hw);
653         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
654
655         WARN_ON(0 == rtlusb->rx_urb_num);
656         /* 1600 == 1514 + max WLAN header + rtk info */
657         WARN_ON(rtlusb->rx_max_size < 1600);
658
659         for (i = 0; i < rtlusb->rx_urb_num; i++) {
660                 err = -ENOMEM;
661                 urb = usb_alloc_urb(0, GFP_KERNEL);
662                 if (!urb) {
663                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
664                                  "Failed to alloc URB!!\n");
665                         goto err_out;
666                 }
667
668                 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
669                 if (err < 0) {
670                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
671                                  "Failed to prep_rx_urb!!\n");
672                         usb_free_urb(urb);
673                         goto err_out;
674                 }
675
676                 usb_anchor_urb(urb, &rtlusb->rx_submitted);
677                 err = usb_submit_urb(urb, GFP_KERNEL);
678                 if (err)
679                         goto err_out;
680                 usb_free_urb(urb);
681         }
682         return 0;
683
684 err_out:
685         usb_kill_anchored_urbs(&rtlusb->rx_submitted);
686         _rtl_usb_cleanup_rx(hw);
687         return err;
688 }
689
690 static int rtl_usb_start(struct ieee80211_hw *hw)
691 {
692         int err;
693         struct rtl_priv *rtlpriv = rtl_priv(hw);
694         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
695         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
696
697         err = rtlpriv->cfg->ops->hw_init(hw);
698         if (!err) {
699                 rtl_init_rx_config(hw);
700
701                 /* Enable software */
702                 SET_USB_START(rtlusb);
703                 /* should after adapter start and interrupt enable. */
704                 set_hal_start(rtlhal);
705
706                 /* Start bulk IN */
707                 err = _rtl_usb_receive(hw);
708         }
709
710         return err;
711 }
712 /**
713  *
714  *
715  */
716
717 /*=======================  tx =========================================*/
718 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
719 {
720         u32 i;
721         struct sk_buff *_skb;
722         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
723         struct ieee80211_tx_info *txinfo;
724
725         SET_USB_STOP(rtlusb);
726
727         /* clean up rx stuff. */
728         _rtl_usb_cleanup_rx(hw);
729
730         /* clean up tx stuff */
731         for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
732                 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
733                         rtlusb->usb_tx_cleanup(hw, _skb);
734                         txinfo = IEEE80211_SKB_CB(_skb);
735                         ieee80211_tx_info_clear_status(txinfo);
736                         txinfo->flags |= IEEE80211_TX_STAT_ACK;
737                         ieee80211_tx_status_irqsafe(hw, _skb);
738                 }
739                 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
740         }
741         usb_kill_anchored_urbs(&rtlusb->tx_submitted);
742 }
743
744 /**
745  *
746  * We may add some struct into struct rtl_usb later. Do deinit here.
747  *
748  */
749 static void rtl_usb_deinit(struct ieee80211_hw *hw)
750 {
751         rtl_usb_cleanup(hw);
752 }
753
754 static void rtl_usb_stop(struct ieee80211_hw *hw)
755 {
756         struct rtl_priv *rtlpriv = rtl_priv(hw);
757         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
758         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
759
760         /* should after adapter start and interrupt enable. */
761         set_hal_stop(rtlhal);
762         /* Enable software */
763         SET_USB_STOP(rtlusb);
764         rtl_usb_deinit(hw);
765         rtlpriv->cfg->ops->hw_disable(hw);
766 }
767
768 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
769 {
770         int err;
771         struct rtl_priv *rtlpriv = rtl_priv(hw);
772         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
773
774         usb_anchor_urb(_urb, &rtlusb->tx_submitted);
775         err = usb_submit_urb(_urb, GFP_ATOMIC);
776         if (err < 0) {
777                 struct sk_buff *skb;
778
779                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
780                          "Failed to submit urb\n");
781                 usb_unanchor_urb(_urb);
782                 skb = (struct sk_buff *)_urb->context;
783                 kfree_skb(skb);
784         }
785         usb_free_urb(_urb);
786 }
787
788 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
789                         struct sk_buff *skb)
790 {
791         struct rtl_priv *rtlpriv = rtl_priv(hw);
792         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
793         struct ieee80211_tx_info *txinfo;
794
795         rtlusb->usb_tx_post_hdl(hw, urb, skb);
796         skb_pull(skb, RTL_TX_HEADER_SIZE);
797         txinfo = IEEE80211_SKB_CB(skb);
798         ieee80211_tx_info_clear_status(txinfo);
799         txinfo->flags |= IEEE80211_TX_STAT_ACK;
800
801         if (urb->status) {
802                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
803                          "Urb has error status 0x%X\n", urb->status);
804                 goto out;
805         }
806         /*  TODO:       statistics */
807 out:
808         ieee80211_tx_status_irqsafe(hw, skb);
809         return urb->status;
810 }
811
812 static void _rtl_tx_complete(struct urb *urb)
813 {
814         struct sk_buff *skb = (struct sk_buff *)urb->context;
815         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
816         struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
817         struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
818         int err;
819
820         if (unlikely(IS_USB_STOP(rtlusb)))
821                 return;
822         err = _usb_tx_post(hw, urb, skb);
823         if (err) {
824                 /* Ignore error and keep issuiing other urbs */
825                 return;
826         }
827 }
828
829 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
830                                 struct sk_buff *skb, u32 ep_num)
831 {
832         struct rtl_priv *rtlpriv = rtl_priv(hw);
833         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
834         struct urb *_urb;
835
836         WARN_ON(NULL == skb);
837         _urb = usb_alloc_urb(0, GFP_ATOMIC);
838         if (!_urb) {
839                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
840                          "Can't allocate URB for bulk out!\n");
841                 kfree_skb(skb);
842                 return NULL;
843         }
844         _rtl_install_trx_info(rtlusb, skb, ep_num);
845         usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
846                           ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
847         _urb->transfer_flags |= URB_ZERO_PACKET;
848         return _urb;
849 }
850
851 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
852                        enum rtl_txq qnum)
853 {
854         struct rtl_priv *rtlpriv = rtl_priv(hw);
855         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
856         u32 ep_num;
857         struct urb *_urb = NULL;
858         struct sk_buff *_skb = NULL;
859
860         WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
861         if (unlikely(IS_USB_STOP(rtlusb))) {
862                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
863                          "USB device is stopping...\n");
864                 kfree_skb(skb);
865                 return;
866         }
867         ep_num = rtlusb->ep_map.ep_mapping[qnum];
868         _skb = skb;
869         _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
870         if (unlikely(!_urb)) {
871                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
872                          "Can't allocate urb. Drop skb!\n");
873                 return;
874         }
875         _rtl_submit_tx_urb(hw, _urb);
876 }
877
878 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
879                                    struct ieee80211_sta *sta,
880                                    struct sk_buff *skb,
881                                    u16 hw_queue)
882 {
883         struct rtl_priv *rtlpriv = rtl_priv(hw);
884         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
885         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
886         struct rtl_tx_desc *pdesc = NULL;
887         struct rtl_tcb_desc tcb_desc;
888         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
889         __le16 fc = hdr->frame_control;
890         u8 *pda_addr = hdr->addr1;
891         /* ssn */
892         u8 *qc = NULL;
893         u8 tid = 0;
894         u16 seq_number = 0;
895
896         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
897         if (ieee80211_is_auth(fc)) {
898                 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
899                 rtl_ips_nic_on(hw);
900         }
901
902         if (rtlpriv->psc.sw_ps_enabled) {
903                 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
904                     !ieee80211_has_pm(fc))
905                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
906         }
907
908         rtl_action_proc(hw, skb, true);
909         if (is_multicast_ether_addr(pda_addr))
910                 rtlpriv->stats.txbytesmulticast += skb->len;
911         else if (is_broadcast_ether_addr(pda_addr))
912                 rtlpriv->stats.txbytesbroadcast += skb->len;
913         else
914                 rtlpriv->stats.txbytesunicast += skb->len;
915         if (ieee80211_is_data_qos(fc)) {
916                 qc = ieee80211_get_qos_ctl(hdr);
917                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
918                 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
919                              IEEE80211_SCTL_SEQ) >> 4;
920                 seq_number += 1;
921                 seq_number <<= 4;
922         }
923         rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
924                                         hw_queue, &tcb_desc);
925         if (!ieee80211_has_morefrags(hdr->frame_control)) {
926                 if (qc)
927                         mac->tids[tid].seq_number = seq_number;
928         }
929         if (ieee80211_is_data(fc))
930                 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
931 }
932
933 static int rtl_usb_tx(struct ieee80211_hw *hw,
934                       struct ieee80211_sta *sta,
935                       struct sk_buff *skb,
936                       struct rtl_tcb_desc *dummy)
937 {
938         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
939         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
940         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
941         __le16 fc = hdr->frame_control;
942         u16 hw_queue;
943
944         if (unlikely(is_hal_stop(rtlhal)))
945                 goto err_free;
946         hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
947         _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
948         _rtl_usb_transmit(hw, skb, hw_queue);
949         return NETDEV_TX_OK;
950
951 err_free:
952         dev_kfree_skb_any(skb);
953         return NETDEV_TX_OK;
954 }
955
956 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
957                                         struct ieee80211_sta *sta,
958                                         struct sk_buff *skb)
959 {
960         return false;
961 }
962
963 static struct rtl_intf_ops rtl_usb_ops = {
964         .adapter_start = rtl_usb_start,
965         .adapter_stop = rtl_usb_stop,
966         .adapter_tx = rtl_usb_tx,
967         .waitq_insert = rtl_usb_tx_chk_waitq_insert,
968 };
969
970 int rtl_usb_probe(struct usb_interface *intf,
971                   const struct usb_device_id *id,
972                   struct rtl_hal_cfg *rtl_hal_cfg)
973 {
974         int err;
975         struct ieee80211_hw *hw = NULL;
976         struct rtl_priv *rtlpriv = NULL;
977         struct usb_device       *udev;
978         struct rtl_usb_priv *usb_priv;
979
980         hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
981                                 sizeof(struct rtl_usb_priv), &rtl_ops);
982         if (!hw) {
983                 RT_ASSERT(false, "ieee80211 alloc failed\n");
984                 return -ENOMEM;
985         }
986         rtlpriv = hw->priv;
987         rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
988                                     GFP_KERNEL);
989         if (!rtlpriv->usb_data)
990                 return -ENOMEM;
991
992         /* this spin lock must be initialized early */
993         spin_lock_init(&rtlpriv->locks.usb_lock);
994
995         rtlpriv->usb_data_index = 0;
996         init_completion(&rtlpriv->firmware_loading_complete);
997         SET_IEEE80211_DEV(hw, &intf->dev);
998         udev = interface_to_usbdev(intf);
999         usb_get_dev(udev);
1000         usb_priv = rtl_usbpriv(hw);
1001         memset(usb_priv, 0, sizeof(*usb_priv));
1002         usb_priv->dev.intf = intf;
1003         usb_priv->dev.udev = udev;
1004         usb_set_intfdata(intf, hw);
1005         /* init cfg & intf_ops */
1006         rtlpriv->rtlhal.interface = INTF_USB;
1007         rtlpriv->cfg = rtl_hal_cfg;
1008         rtlpriv->intf_ops = &rtl_usb_ops;
1009         rtl_dbgp_flag_init(hw);
1010         /* Init IO handler */
1011         _rtl_usb_io_handler_init(&udev->dev, hw);
1012         rtlpriv->cfg->ops->read_chip_version(hw);
1013         /*like read eeprom and so on */
1014         rtlpriv->cfg->ops->read_eeprom_info(hw);
1015         err = _rtl_usb_init(hw);
1016         if (err)
1017                 goto error_out;
1018         rtl_usb_init_sw(hw);
1019         /* Init mac80211 sw */
1020         err = rtl_init_core(hw);
1021         if (err) {
1022                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1023                          "Can't allocate sw for mac80211\n");
1024                 goto error_out;
1025         }
1026         if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1027                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1028                 goto error_out;
1029         }
1030         rtlpriv->cfg->ops->init_sw_leds(hw);
1031
1032         return 0;
1033 error_out:
1034         rtl_deinit_core(hw);
1035         _rtl_usb_io_handler_release(hw);
1036         usb_put_dev(udev);
1037         complete(&rtlpriv->firmware_loading_complete);
1038         return -ENODEV;
1039 }
1040 EXPORT_SYMBOL(rtl_usb_probe);
1041
1042 void rtl_usb_disconnect(struct usb_interface *intf)
1043 {
1044         struct ieee80211_hw *hw = usb_get_intfdata(intf);
1045         struct rtl_priv *rtlpriv = rtl_priv(hw);
1046         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1047         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1048
1049         if (unlikely(!rtlpriv))
1050                 return;
1051
1052         /* just in case driver is removed before firmware callback */
1053         wait_for_completion(&rtlpriv->firmware_loading_complete);
1054         /*ieee80211_unregister_hw will call ops_stop */
1055         if (rtlmac->mac80211_registered == 1) {
1056                 ieee80211_unregister_hw(hw);
1057                 rtlmac->mac80211_registered = 0;
1058         } else {
1059                 rtl_deinit_deferred_work(hw);
1060                 rtlpriv->intf_ops->adapter_stop(hw);
1061         }
1062         /*deinit rfkill */
1063         /* rtl_deinit_rfkill(hw); */
1064         rtl_usb_deinit(hw);
1065         rtl_deinit_core(hw);
1066         kfree(rtlpriv->usb_data);
1067         rtlpriv->cfg->ops->deinit_sw_leds(hw);
1068         rtlpriv->cfg->ops->deinit_sw_vars(hw);
1069         _rtl_usb_io_handler_release(hw);
1070         usb_put_dev(rtlusb->udev);
1071         usb_set_intfdata(intf, NULL);
1072         ieee80211_free_hw(hw);
1073 }
1074 EXPORT_SYMBOL(rtl_usb_disconnect);
1075
1076 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1077 {
1078         return 0;
1079 }
1080 EXPORT_SYMBOL(rtl_usb_suspend);
1081
1082 int rtl_usb_resume(struct usb_interface *pusb_intf)
1083 {
1084         return 0;
1085 }
1086 EXPORT_SYMBOL(rtl_usb_resume);