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