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