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