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