OSDN Git Service

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