]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/net/wireless/rtlwifi/usb.c
ath9k_hw: Fix RX gain initvals for AR9485
[~shefty/rdma-dev.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                 kfree(urb->context);
47 }
48
49 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
50                                           u16 value, u16 index, void *pdata,
51                                           u16 len)
52 {
53         int rc;
54         unsigned int pipe;
55         u8 reqtype;
56         struct usb_ctrlrequest *dr;
57         struct urb *urb;
58         struct rtl819x_async_write_data {
59                 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
60                 struct usb_ctrlrequest dr;
61         } *buf;
62
63         pipe = usb_sndctrlpipe(udev, 0); /* write_out */
64         reqtype =  REALTEK_USB_VENQT_WRITE;
65
66         buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
67         if (!buf)
68                 return -ENOMEM;
69
70         urb = usb_alloc_urb(0, GFP_ATOMIC);
71         if (!urb) {
72                 kfree(buf);
73                 return -ENOMEM;
74         }
75
76         dr = &buf->dr;
77
78         dr->bRequestType = reqtype;
79         dr->bRequest = request;
80         dr->wValue = cpu_to_le16(value);
81         dr->wIndex = cpu_to_le16(index);
82         dr->wLength = cpu_to_le16(len);
83         /* data are already in little-endian order */
84         memcpy(buf, pdata, len);
85         usb_fill_control_urb(urb, udev, pipe,
86                              (unsigned char *)dr, buf, len,
87                              usbctrl_async_callback, buf);
88         rc = usb_submit_urb(urb, GFP_ATOMIC);
89         if (rc < 0)
90                 kfree(buf);
91         usb_free_urb(urb);
92         return rc;
93 }
94
95 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
96                                         u16 value, u16 index, void *pdata,
97                                         u16 len)
98 {
99         unsigned int pipe;
100         int status;
101         u8 reqtype;
102         int vendorreq_times = 0;
103         static int count;
104
105         pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
106         reqtype =  REALTEK_USB_VENQT_READ;
107
108         do {
109                 status = usb_control_msg(udev, pipe, request, reqtype, value,
110                                          index, pdata, len, 0); /*max. timeout*/
111                 if (status < 0) {
112                         /* firmware download is checksumed, don't retry */
113                         if ((value >= FW_8192C_START_ADDRESS &&
114                             value <= FW_8192C_END_ADDRESS))
115                                 break;
116                 } else {
117                         break;
118                 }
119         } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
120
121         if (status < 0 && count++ < 4)
122                 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
123                        value, status, *(u32 *)pdata);
124         return status;
125 }
126
127 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
128 {
129         struct device *dev = rtlpriv->io.dev;
130         struct usb_device *udev = to_usb_device(dev);
131         u8 request;
132         u16 wvalue;
133         u16 index;
134         __le32 *data;
135         unsigned long flags;
136
137         spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
138         if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
139                 rtlpriv->usb_data_index = 0;
140         data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
141         spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
142         request = REALTEK_USB_VENQT_CMD_REQ;
143         index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
144
145         wvalue = (u16)addr;
146         _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
147         return le32_to_cpu(*data);
148 }
149
150 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
151 {
152         return (u8)_usb_read_sync(rtlpriv, addr, 1);
153 }
154
155 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
156 {
157         return (u16)_usb_read_sync(rtlpriv, addr, 2);
158 }
159
160 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
161 {
162         return _usb_read_sync(rtlpriv, addr, 4);
163 }
164
165 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
166                              u16 len)
167 {
168         u8 request;
169         u16 wvalue;
170         u16 index;
171         __le32 data;
172
173         request = REALTEK_USB_VENQT_CMD_REQ;
174         index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
175         wvalue = (u16)(addr&0x0000ffff);
176         data = cpu_to_le32(val);
177         _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
178                                        len);
179 }
180
181 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
182 {
183         struct device *dev = rtlpriv->io.dev;
184
185         _usb_write_async(to_usb_device(dev), addr, val, 1);
186 }
187
188 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
189 {
190         struct device *dev = rtlpriv->io.dev;
191
192         _usb_write_async(to_usb_device(dev), addr, val, 2);
193 }
194
195 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
196 {
197         struct device *dev = rtlpriv->io.dev;
198
199         _usb_write_async(to_usb_device(dev), addr, val, 4);
200 }
201
202 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
203                              u16 len)
204 {
205         struct device *dev = rtlpriv->io.dev;
206         struct usb_device *udev = to_usb_device(dev);
207         u8 request = REALTEK_USB_VENQT_CMD_REQ;
208         u8 reqtype =  REALTEK_USB_VENQT_WRITE;
209         u16 wvalue;
210         u16 index = REALTEK_USB_VENQT_CMD_IDX;
211         int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
212         u8 *buffer;
213
214         wvalue = (u16)(addr & 0x0000ffff);
215         buffer = kmalloc(len, GFP_ATOMIC);
216         if (!buffer)
217                 return;
218         memcpy(buffer, data, len);
219         usb_control_msg(udev, pipe, request, reqtype, wvalue,
220                         index, buffer, len, 50);
221
222         kfree(buffer);
223 }
224
225 static void _rtl_usb_io_handler_init(struct device *dev,
226                                      struct ieee80211_hw *hw)
227 {
228         struct rtl_priv *rtlpriv = rtl_priv(hw);
229
230         rtlpriv->io.dev = dev;
231         mutex_init(&rtlpriv->io.bb_mutex);
232         rtlpriv->io.write8_async        = _usb_write8_async;
233         rtlpriv->io.write16_async       = _usb_write16_async;
234         rtlpriv->io.write32_async       = _usb_write32_async;
235         rtlpriv->io.read8_sync          = _usb_read8_sync;
236         rtlpriv->io.read16_sync         = _usb_read16_sync;
237         rtlpriv->io.read32_sync         = _usb_read32_sync;
238         rtlpriv->io.writeN_sync         = _usb_writeN_sync;
239 }
240
241 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
242 {
243         struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
244
245         mutex_destroy(&rtlpriv->io.bb_mutex);
246 }
247
248 /**
249  *
250  *      Default aggregation handler. Do nothing and just return the oldest skb.
251  */
252 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
253                                                   struct sk_buff_head *list)
254 {
255         return skb_dequeue(list);
256 }
257
258 #define IS_HIGH_SPEED_USB(udev) \
259                 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
260
261 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
262 {
263         u32 i;
264         struct rtl_priv *rtlpriv = rtl_priv(hw);
265         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
266
267         rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
268                                                     ? USB_HIGH_SPEED_BULK_SIZE
269                                                     : USB_FULL_SPEED_BULK_SIZE;
270
271         RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
272                  rtlusb->max_bulk_out_size);
273
274         for (i = 0; i < __RTL_TXQ_NUM; i++) {
275                 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
276                 if (!ep_num) {
277                         RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
278                                  "Invalid endpoint map setting!\n");
279                         return -EINVAL;
280                 }
281         }
282
283         rtlusb->usb_tx_post_hdl =
284                  rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
285         rtlusb->usb_tx_cleanup  =
286                  rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
287         rtlusb->usb_tx_aggregate_hdl =
288                  (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
289                  ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
290                  : &_none_usb_tx_aggregate_hdl;
291
292         init_usb_anchor(&rtlusb->tx_submitted);
293         for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
294                 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
295                 init_usb_anchor(&rtlusb->tx_pending[i]);
296         }
297         return 0;
298 }
299
300 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
301 {
302         struct rtl_priv *rtlpriv = rtl_priv(hw);
303         struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
304         struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
305
306         rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
307         rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
308         rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
309         rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
310         rtlusb->usb_rx_segregate_hdl =
311                 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
312
313         pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
314                 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
315         init_usb_anchor(&rtlusb->rx_submitted);
316         return 0;
317 }
318
319 static int _rtl_usb_init(struct ieee80211_hw *hw)
320 {
321         struct rtl_priv *rtlpriv = rtl_priv(hw);
322         struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
323         struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
324         int err;
325         u8 epidx;
326         struct usb_interface    *usb_intf = rtlusb->intf;
327         u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
328
329         rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
330         for (epidx = 0; epidx < epnums; epidx++) {
331                 struct usb_endpoint_descriptor *pep_desc;
332                 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
333
334                 if (usb_endpoint_dir_in(pep_desc))
335                         rtlusb->in_ep_nums++;
336                 else if (usb_endpoint_dir_out(pep_desc))
337                         rtlusb->out_ep_nums++;
338
339                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
340                          "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
341                          pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
342                          pep_desc->bInterval);
343         }
344         if (rtlusb->in_ep_nums <  rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
345                 pr_err("Too few input end points found\n");
346                 return -EINVAL;
347         }
348         if (rtlusb->out_ep_nums == 0) {
349                 pr_err("No output end points found\n");
350                 return -EINVAL;
351         }
352         /* usb endpoint mapping */
353         err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
354         rtlusb->usb_mq_to_hwq =  rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
355         _rtl_usb_init_tx(hw);
356         _rtl_usb_init_rx(hw);
357         return err;
358 }
359
360 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
361 {
362         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
363         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
364         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
365         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
366
367         rtlhal->hw = hw;
368         ppsc->inactiveps = false;
369         ppsc->leisure_ps = false;
370         ppsc->fwctrl_lps = false;
371         ppsc->reg_fwctrl_lps = 3;
372         ppsc->reg_max_lps_awakeintvl = 5;
373         ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
374
375          /* IBSS */
376         mac->beacon_interval = 100;
377
378          /* AMPDU */
379         mac->min_space_cfg = 0;
380         mac->max_mss_density = 0;
381
382         /* set sane AMPDU defaults */
383         mac->current_ampdu_density = 7;
384         mac->current_ampdu_factor = 3;
385
386         /* QOS */
387         rtlusb->acm_method = eAcmWay2_SW;
388
389         /* IRQ */
390         /* HIMR - turn all on */
391         rtlusb->irq_mask[0] = 0xFFFFFFFF;
392         /* HIMR_EX - turn all on */
393         rtlusb->irq_mask[1] = 0xFFFFFFFF;
394         rtlusb->disableHWSM =  true;
395 }
396
397 #define __RADIO_TAP_SIZE_RSV    32
398
399 static void _rtl_rx_completed(struct urb *urb);
400
401 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
402                                         struct rtl_usb *rtlusb,
403                                         struct urb *urb,
404                                         gfp_t gfp_mask)
405 {
406         struct sk_buff *skb;
407         struct rtl_priv *rtlpriv = rtl_priv(hw);
408
409         skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
410                                gfp_mask);
411         if (!skb) {
412                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
413                          "Failed to __dev_alloc_skb!!\n");
414                 return ERR_PTR(-ENOMEM);
415         }
416
417         /* reserve some space for mac80211's radiotap */
418         skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
419         usb_fill_bulk_urb(urb, rtlusb->udev,
420                           usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
421                           skb->data, min(skb_tailroom(skb),
422                           (int)rtlusb->rx_max_size),
423                           _rtl_rx_completed, skb);
424
425         _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
426         return skb;
427 }
428
429 #undef __RADIO_TAP_SIZE_RSV
430
431 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
432                                     struct sk_buff *skb)
433 {
434         struct rtl_priv *rtlpriv = rtl_priv(hw);
435         u8 *rxdesc = skb->data;
436         struct ieee80211_hdr *hdr;
437         bool unicast = false;
438         __le16 fc;
439         struct ieee80211_rx_status rx_status = {0};
440         struct rtl_stats stats = {
441                 .signal = 0,
442                 .noise = -98,
443                 .rate = 0,
444         };
445
446         skb_pull(skb, RTL_RX_DESC_SIZE);
447         rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
448         skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
449         hdr = (struct ieee80211_hdr *)(skb->data);
450         fc = hdr->frame_control;
451         if (!stats.crc) {
452                 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
453
454                 if (is_broadcast_ether_addr(hdr->addr1)) {
455                         /*TODO*/;
456                 } else if (is_multicast_ether_addr(hdr->addr1)) {
457                         /*TODO*/
458                 } else {
459                         unicast = true;
460                         rtlpriv->stats.rxbytesunicast +=  skb->len;
461                 }
462
463                 rtl_is_special_data(hw, skb, false);
464
465                 if (ieee80211_is_data(fc)) {
466                         rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
467
468                         if (unicast)
469                                 rtlpriv->link_info.num_rx_inperiod++;
470                 }
471         }
472 }
473
474 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
475                                       struct sk_buff *skb)
476 {
477         struct rtl_priv *rtlpriv = rtl_priv(hw);
478         u8 *rxdesc = skb->data;
479         struct ieee80211_hdr *hdr;
480         bool unicast = false;
481         __le16 fc;
482         struct ieee80211_rx_status rx_status = {0};
483         struct rtl_stats stats = {
484                 .signal = 0,
485                 .noise = -98,
486                 .rate = 0,
487         };
488
489         skb_pull(skb, RTL_RX_DESC_SIZE);
490         rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
491         skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
492         hdr = (struct ieee80211_hdr *)(skb->data);
493         fc = hdr->frame_control;
494         if (!stats.crc) {
495                 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
496
497                 if (is_broadcast_ether_addr(hdr->addr1)) {
498                         /*TODO*/;
499                 } else if (is_multicast_ether_addr(hdr->addr1)) {
500                         /*TODO*/
501                 } else {
502                         unicast = true;
503                         rtlpriv->stats.rxbytesunicast +=  skb->len;
504                 }
505
506                 rtl_is_special_data(hw, skb, false);
507
508                 if (ieee80211_is_data(fc)) {
509                         rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
510
511                         if (unicast)
512                                 rtlpriv->link_info.num_rx_inperiod++;
513                 }
514                 if (likely(rtl_action_proc(hw, skb, false))) {
515                         struct sk_buff *uskb = NULL;
516                         u8 *pdata;
517
518                         uskb = dev_alloc_skb(skb->len + 128);
519                         if (uskb) {     /* drop packet on allocation failure */
520                                 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
521                                        sizeof(rx_status));
522                                 pdata = (u8 *)skb_put(uskb, skb->len);
523                                 memcpy(pdata, skb->data, skb->len);
524                                 ieee80211_rx_irqsafe(hw, uskb);
525                         }
526                         dev_kfree_skb_any(skb);
527                 } else {
528                         dev_kfree_skb_any(skb);
529                 }
530         }
531 }
532
533 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
534 {
535         struct sk_buff *_skb;
536         struct sk_buff_head rx_queue;
537         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
538
539         skb_queue_head_init(&rx_queue);
540         if (rtlusb->usb_rx_segregate_hdl)
541                 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
542         WARN_ON(skb_queue_empty(&rx_queue));
543         while (!skb_queue_empty(&rx_queue)) {
544                 _skb = skb_dequeue(&rx_queue);
545                 _rtl_usb_rx_process_agg(hw, skb);
546                 ieee80211_rx_irqsafe(hw, skb);
547         }
548 }
549
550 static void _rtl_rx_completed(struct urb *_urb)
551 {
552         struct sk_buff *skb = (struct sk_buff *)_urb->context;
553         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
554         struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
555         struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
556         struct rtl_priv *rtlpriv = rtl_priv(hw);
557         int err = 0;
558
559         if (unlikely(IS_USB_STOP(rtlusb)))
560                 goto free;
561
562         if (likely(0 == _urb->status)) {
563                 /* If this code were moved to work queue, would CPU
564                  * utilization be improved?  NOTE: We shall allocate another skb
565                  * and reuse the original one.
566                  */
567                 skb_put(skb, _urb->actual_length);
568
569                 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
570                         struct sk_buff *_skb;
571                         _rtl_usb_rx_process_noagg(hw, skb);
572                         _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
573                         if (IS_ERR(_skb)) {
574                                 err = PTR_ERR(_skb);
575                                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
576                                          "Can't allocate skb for bulk IN!\n");
577                                 return;
578                         }
579                         skb = _skb;
580                 } else{
581                         /* TO DO */
582                         _rtl_rx_pre_process(hw, skb);
583                         pr_err("rx agg not supported\n");
584                 }
585                 goto resubmit;
586         }
587
588         switch (_urb->status) {
589         /* disconnect */
590         case -ENOENT:
591         case -ECONNRESET:
592         case -ENODEV:
593         case -ESHUTDOWN:
594                 goto free;
595         default:
596                 break;
597         }
598
599 resubmit:
600         skb_reset_tail_pointer(skb);
601         skb_trim(skb, 0);
602
603         usb_anchor_urb(_urb, &rtlusb->rx_submitted);
604         err = usb_submit_urb(_urb, GFP_ATOMIC);
605         if (unlikely(err)) {
606                 usb_unanchor_urb(_urb);
607                 goto free;
608         }
609         return;
610
611 free:
612         dev_kfree_skb_irq(skb);
613 }
614
615 static int _rtl_usb_receive(struct ieee80211_hw *hw)
616 {
617         struct urb *urb;
618         struct sk_buff *skb;
619         int err;
620         int i;
621         struct rtl_priv *rtlpriv = rtl_priv(hw);
622         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
623
624         WARN_ON(0 == rtlusb->rx_urb_num);
625         /* 1600 == 1514 + max WLAN header + rtk info */
626         WARN_ON(rtlusb->rx_max_size < 1600);
627
628         for (i = 0; i < rtlusb->rx_urb_num; i++) {
629                 err = -ENOMEM;
630                 urb = usb_alloc_urb(0, GFP_KERNEL);
631                 if (!urb) {
632                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
633                                  "Failed to alloc URB!!\n");
634                         goto err_out;
635                 }
636
637                 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
638                 if (IS_ERR(skb)) {
639                         RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
640                                  "Failed to prep_rx_urb!!\n");
641                         err = PTR_ERR(skb);
642                         goto err_out;
643                 }
644
645                 usb_anchor_urb(urb, &rtlusb->rx_submitted);
646                 err = usb_submit_urb(urb, GFP_KERNEL);
647                 if (err)
648                         goto err_out;
649                 usb_free_urb(urb);
650         }
651         return 0;
652
653 err_out:
654         usb_kill_anchored_urbs(&rtlusb->rx_submitted);
655         return err;
656 }
657
658 static int rtl_usb_start(struct ieee80211_hw *hw)
659 {
660         int err;
661         struct rtl_priv *rtlpriv = rtl_priv(hw);
662         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
663         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
664
665         err = rtlpriv->cfg->ops->hw_init(hw);
666         if (!err) {
667                 rtl_init_rx_config(hw);
668
669                 /* Enable software */
670                 SET_USB_START(rtlusb);
671                 /* should after adapter start and interrupt enable. */
672                 set_hal_start(rtlhal);
673
674                 /* Start bulk IN */
675                 err = _rtl_usb_receive(hw);
676         }
677
678         return err;
679 }
680 /**
681  *
682  *
683  */
684
685 /*=======================  tx =========================================*/
686 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
687 {
688         u32 i;
689         struct sk_buff *_skb;
690         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
691         struct ieee80211_tx_info *txinfo;
692
693         SET_USB_STOP(rtlusb);
694
695         /* clean up rx stuff. */
696         usb_kill_anchored_urbs(&rtlusb->rx_submitted);
697
698         /* clean up tx stuff */
699         for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
700                 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
701                         rtlusb->usb_tx_cleanup(hw, _skb);
702                         txinfo = IEEE80211_SKB_CB(_skb);
703                         ieee80211_tx_info_clear_status(txinfo);
704                         txinfo->flags |= IEEE80211_TX_STAT_ACK;
705                         ieee80211_tx_status_irqsafe(hw, _skb);
706                 }
707                 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
708         }
709         usb_kill_anchored_urbs(&rtlusb->tx_submitted);
710 }
711
712 /**
713  *
714  * We may add some struct into struct rtl_usb later. Do deinit here.
715  *
716  */
717 static void rtl_usb_deinit(struct ieee80211_hw *hw)
718 {
719         rtl_usb_cleanup(hw);
720 }
721
722 static void rtl_usb_stop(struct ieee80211_hw *hw)
723 {
724         struct rtl_priv *rtlpriv = rtl_priv(hw);
725         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
726         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
727
728         /* should after adapter start and interrupt enable. */
729         set_hal_stop(rtlhal);
730         /* Enable software */
731         SET_USB_STOP(rtlusb);
732         rtl_usb_deinit(hw);
733         rtlpriv->cfg->ops->hw_disable(hw);
734 }
735
736 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
737 {
738         int err;
739         struct rtl_priv *rtlpriv = rtl_priv(hw);
740         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
741
742         usb_anchor_urb(_urb, &rtlusb->tx_submitted);
743         err = usb_submit_urb(_urb, GFP_ATOMIC);
744         if (err < 0) {
745                 struct sk_buff *skb;
746
747                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
748                          "Failed to submit urb\n");
749                 usb_unanchor_urb(_urb);
750                 skb = (struct sk_buff *)_urb->context;
751                 kfree_skb(skb);
752         }
753         usb_free_urb(_urb);
754 }
755
756 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
757                         struct sk_buff *skb)
758 {
759         struct rtl_priv *rtlpriv = rtl_priv(hw);
760         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
761         struct ieee80211_tx_info *txinfo;
762
763         rtlusb->usb_tx_post_hdl(hw, urb, skb);
764         skb_pull(skb, RTL_TX_HEADER_SIZE);
765         txinfo = IEEE80211_SKB_CB(skb);
766         ieee80211_tx_info_clear_status(txinfo);
767         txinfo->flags |= IEEE80211_TX_STAT_ACK;
768
769         if (urb->status) {
770                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
771                          "Urb has error status 0x%X\n", urb->status);
772                 goto out;
773         }
774         /*  TODO:       statistics */
775 out:
776         ieee80211_tx_status_irqsafe(hw, skb);
777         return urb->status;
778 }
779
780 static void _rtl_tx_complete(struct urb *urb)
781 {
782         struct sk_buff *skb = (struct sk_buff *)urb->context;
783         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
784         struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
785         struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
786         int err;
787
788         if (unlikely(IS_USB_STOP(rtlusb)))
789                 return;
790         err = _usb_tx_post(hw, urb, skb);
791         if (err) {
792                 /* Ignore error and keep issuiing other urbs */
793                 return;
794         }
795 }
796
797 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
798                                 struct sk_buff *skb, u32 ep_num)
799 {
800         struct rtl_priv *rtlpriv = rtl_priv(hw);
801         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
802         struct urb *_urb;
803
804         WARN_ON(NULL == skb);
805         _urb = usb_alloc_urb(0, GFP_ATOMIC);
806         if (!_urb) {
807                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
808                          "Can't allocate URB for bulk out!\n");
809                 kfree_skb(skb);
810                 return NULL;
811         }
812         _rtl_install_trx_info(rtlusb, skb, ep_num);
813         usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
814                           ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
815         _urb->transfer_flags |= URB_ZERO_PACKET;
816         return _urb;
817 }
818
819 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
820                        enum rtl_txq qnum)
821 {
822         struct rtl_priv *rtlpriv = rtl_priv(hw);
823         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
824         u32 ep_num;
825         struct urb *_urb = NULL;
826         struct sk_buff *_skb = NULL;
827         struct sk_buff_head *skb_list;
828         struct usb_anchor *urb_list;
829
830         WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
831         if (unlikely(IS_USB_STOP(rtlusb))) {
832                 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
833                          "USB device is stopping...\n");
834                 kfree_skb(skb);
835                 return;
836         }
837         ep_num = rtlusb->ep_map.ep_mapping[qnum];
838         skb_list = &rtlusb->tx_skb_queue[ep_num];
839         _skb = skb;
840         _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
841         if (unlikely(!_urb)) {
842                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
843                          "Can't allocate urb. Drop skb!\n");
844                 return;
845         }
846         urb_list = &rtlusb->tx_pending[ep_num];
847         _rtl_submit_tx_urb(hw, _urb);
848 }
849
850 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
851                                    struct ieee80211_sta *sta,
852                                    struct sk_buff *skb,
853                                    u16 hw_queue)
854 {
855         struct rtl_priv *rtlpriv = rtl_priv(hw);
856         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
857         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
858         struct rtl_tx_desc *pdesc = NULL;
859         struct rtl_tcb_desc tcb_desc;
860         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
861         __le16 fc = hdr->frame_control;
862         u8 *pda_addr = hdr->addr1;
863         /* ssn */
864         u8 *qc = NULL;
865         u8 tid = 0;
866         u16 seq_number = 0;
867
868         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
869         if (ieee80211_is_auth(fc)) {
870                 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
871                 rtl_ips_nic_on(hw);
872         }
873
874         if (rtlpriv->psc.sw_ps_enabled) {
875                 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
876                     !ieee80211_has_pm(fc))
877                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
878         }
879
880         rtl_action_proc(hw, skb, true);
881         if (is_multicast_ether_addr(pda_addr))
882                 rtlpriv->stats.txbytesmulticast += skb->len;
883         else if (is_broadcast_ether_addr(pda_addr))
884                 rtlpriv->stats.txbytesbroadcast += skb->len;
885         else
886                 rtlpriv->stats.txbytesunicast += skb->len;
887         if (ieee80211_is_data_qos(fc)) {
888                 qc = ieee80211_get_qos_ctl(hdr);
889                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
890                 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
891                              IEEE80211_SCTL_SEQ) >> 4;
892                 seq_number += 1;
893                 seq_number <<= 4;
894         }
895         rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
896                                         hw_queue, &tcb_desc);
897         if (!ieee80211_has_morefrags(hdr->frame_control)) {
898                 if (qc)
899                         mac->tids[tid].seq_number = seq_number;
900         }
901         if (ieee80211_is_data(fc))
902                 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
903 }
904
905 static int rtl_usb_tx(struct ieee80211_hw *hw,
906                       struct ieee80211_sta *sta,
907                       struct sk_buff *skb,
908                       struct rtl_tcb_desc *dummy)
909 {
910         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
911         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
912         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
913         __le16 fc = hdr->frame_control;
914         u16 hw_queue;
915
916         if (unlikely(is_hal_stop(rtlhal)))
917                 goto err_free;
918         hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
919         _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
920         _rtl_usb_transmit(hw, skb, hw_queue);
921         return NETDEV_TX_OK;
922
923 err_free:
924         dev_kfree_skb_any(skb);
925         return NETDEV_TX_OK;
926 }
927
928 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
929                                         struct ieee80211_sta *sta,
930                                         struct sk_buff *skb)
931 {
932         return false;
933 }
934
935 static struct rtl_intf_ops rtl_usb_ops = {
936         .adapter_start = rtl_usb_start,
937         .adapter_stop = rtl_usb_stop,
938         .adapter_tx = rtl_usb_tx,
939         .waitq_insert = rtl_usb_tx_chk_waitq_insert,
940 };
941
942 int rtl_usb_probe(struct usb_interface *intf,
943                         const struct usb_device_id *id)
944 {
945         int err;
946         struct ieee80211_hw *hw = NULL;
947         struct rtl_priv *rtlpriv = NULL;
948         struct usb_device       *udev;
949         struct rtl_usb_priv *usb_priv;
950
951         hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
952                                 sizeof(struct rtl_usb_priv), &rtl_ops);
953         if (!hw) {
954                 RT_ASSERT(false, "ieee80211 alloc failed\n");
955                 return -ENOMEM;
956         }
957         rtlpriv = hw->priv;
958         rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
959                                     GFP_KERNEL);
960         if (!rtlpriv->usb_data)
961                 return -ENOMEM;
962
963         /* this spin lock must be initialized early */
964         spin_lock_init(&rtlpriv->locks.usb_lock);
965
966         rtlpriv->usb_data_index = 0;
967         init_completion(&rtlpriv->firmware_loading_complete);
968         SET_IEEE80211_DEV(hw, &intf->dev);
969         udev = interface_to_usbdev(intf);
970         usb_get_dev(udev);
971         usb_priv = rtl_usbpriv(hw);
972         memset(usb_priv, 0, sizeof(*usb_priv));
973         usb_priv->dev.intf = intf;
974         usb_priv->dev.udev = udev;
975         usb_set_intfdata(intf, hw);
976         /* init cfg & intf_ops */
977         rtlpriv->rtlhal.interface = INTF_USB;
978         rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
979         rtlpriv->intf_ops = &rtl_usb_ops;
980         rtl_dbgp_flag_init(hw);
981         /* Init IO handler */
982         _rtl_usb_io_handler_init(&udev->dev, hw);
983         rtlpriv->cfg->ops->read_chip_version(hw);
984         /*like read eeprom and so on */
985         rtlpriv->cfg->ops->read_eeprom_info(hw);
986         err = _rtl_usb_init(hw);
987         if (err)
988                 goto error_out;
989         rtl_usb_init_sw(hw);
990         /* Init mac80211 sw */
991         err = rtl_init_core(hw);
992         if (err) {
993                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
994                          "Can't allocate sw for mac80211\n");
995                 goto error_out;
996         }
997         if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
998                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
999                 goto error_out;
1000         }
1001         rtlpriv->cfg->ops->init_sw_leds(hw);
1002
1003         return 0;
1004 error_out:
1005         rtl_deinit_core(hw);
1006         _rtl_usb_io_handler_release(hw);
1007         usb_put_dev(udev);
1008         complete(&rtlpriv->firmware_loading_complete);
1009         return -ENODEV;
1010 }
1011 EXPORT_SYMBOL(rtl_usb_probe);
1012
1013 void rtl_usb_disconnect(struct usb_interface *intf)
1014 {
1015         struct ieee80211_hw *hw = usb_get_intfdata(intf);
1016         struct rtl_priv *rtlpriv = rtl_priv(hw);
1017         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1018         struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1019
1020         if (unlikely(!rtlpriv))
1021                 return;
1022
1023         /* just in case driver is removed before firmware callback */
1024         wait_for_completion(&rtlpriv->firmware_loading_complete);
1025         /*ieee80211_unregister_hw will call ops_stop */
1026         if (rtlmac->mac80211_registered == 1) {
1027                 ieee80211_unregister_hw(hw);
1028                 rtlmac->mac80211_registered = 0;
1029         } else {
1030                 rtl_deinit_deferred_work(hw);
1031                 rtlpriv->intf_ops->adapter_stop(hw);
1032         }
1033         /*deinit rfkill */
1034         /* rtl_deinit_rfkill(hw); */
1035         rtl_usb_deinit(hw);
1036         rtl_deinit_core(hw);
1037         kfree(rtlpriv->usb_data);
1038         rtlpriv->cfg->ops->deinit_sw_leds(hw);
1039         rtlpriv->cfg->ops->deinit_sw_vars(hw);
1040         _rtl_usb_io_handler_release(hw);
1041         usb_put_dev(rtlusb->udev);
1042         usb_set_intfdata(intf, NULL);
1043         ieee80211_free_hw(hw);
1044 }
1045 EXPORT_SYMBOL(rtl_usb_disconnect);
1046
1047 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1048 {
1049         return 0;
1050 }
1051 EXPORT_SYMBOL(rtl_usb_suspend);
1052
1053 int rtl_usb_resume(struct usb_interface *pusb_intf)
1054 {
1055         return 0;
1056 }
1057 EXPORT_SYMBOL(rtl_usb_resume);