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