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