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