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