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