Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / wireless / rtlwifi / usb.c
blob049e2070ce2452706e495f2a9bd4d0aacd19657c
1 /******************************************************************************
3 * Copyright(c) 2009-2011 Realtek Corporation. All rights reserved.
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.
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.
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 *****************************************************************************/
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/usb.h>
31 #include <linux/export.h>
32 #include "wifi.h"
33 #include "core.h"
34 #include "usb.h"
35 #include "base.h"
36 #include "ps.h"
37 #include "rtl8192c/fw_common.h"
39 #define REALTEK_USB_VENQT_READ 0xC0
40 #define REALTEK_USB_VENQT_WRITE 0x40
41 #define REALTEK_USB_VENQT_CMD_REQ 0x05
42 #define REALTEK_USB_VENQT_CMD_IDX 0x00
44 #define MAX_USBCTRL_VENDORREQ_TIMES 10
46 static void usbctrl_async_callback(struct urb *urb)
48 if (urb)
49 kfree(urb->context);
52 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
53 u16 value, u16 index, void *pdata,
54 u16 len)
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;
66 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
67 reqtype = REALTEK_USB_VENQT_WRITE;
69 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
70 if (!buf)
71 return -ENOMEM;
73 urb = usb_alloc_urb(0, GFP_ATOMIC);
74 if (!urb) {
75 kfree(buf);
76 return -ENOMEM;
79 dr = &buf->dr;
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 /* data are already in little-endian order */
87 memcpy(buf, pdata, len);
88 usb_fill_control_urb(urb, udev, pipe,
89 (unsigned char *)dr, buf, len,
90 usbctrl_async_callback, buf);
91 rc = usb_submit_urb(urb, GFP_ATOMIC);
92 if (rc < 0)
93 kfree(buf);
94 usb_free_urb(urb);
95 return rc;
98 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
99 u16 value, u16 index, void *pdata,
100 u16 len)
102 unsigned int pipe;
103 int status;
104 u8 reqtype;
105 int vendorreq_times = 0;
106 static int count;
108 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
109 reqtype = REALTEK_USB_VENQT_READ;
111 do {
112 status = usb_control_msg(udev, pipe, request, reqtype, value,
113 index, pdata, len, 0); /*max. timeout*/
114 if (status < 0) {
115 /* firmware download is checksumed, don't retry */
116 if ((value >= FW_8192C_START_ADDRESS &&
117 value <= FW_8192C_END_ADDRESS))
118 break;
119 } else {
120 break;
122 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
124 if (status < 0 && count++ < 4)
125 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
126 value, status, le32_to_cpu(*(u32 *)pdata));
127 return status;
130 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
132 struct device *dev = rtlpriv->io.dev;
133 struct usb_device *udev = to_usb_device(dev);
134 u8 request;
135 u16 wvalue;
136 u16 index;
137 __le32 *data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
139 request = REALTEK_USB_VENQT_CMD_REQ;
140 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
142 wvalue = (u16)addr;
143 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
144 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
145 rtlpriv->usb_data_index = 0;
146 return le32_to_cpu(*data);
149 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
151 return (u8)_usb_read_sync(rtlpriv, addr, 1);
154 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
156 return (u16)_usb_read_sync(rtlpriv, addr, 2);
159 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
161 return _usb_read_sync(rtlpriv, addr, 4);
164 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
165 u16 len)
167 u8 request;
168 u16 wvalue;
169 u16 index;
170 __le32 data;
172 request = REALTEK_USB_VENQT_CMD_REQ;
173 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
174 wvalue = (u16)(addr&0x0000ffff);
175 data = cpu_to_le32(val);
176 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
177 len);
180 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
182 struct device *dev = rtlpriv->io.dev;
184 _usb_write_async(to_usb_device(dev), addr, val, 1);
187 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
189 struct device *dev = rtlpriv->io.dev;
191 _usb_write_async(to_usb_device(dev), addr, val, 2);
194 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
196 struct device *dev = rtlpriv->io.dev;
198 _usb_write_async(to_usb_device(dev), addr, val, 4);
201 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
202 u16 len)
204 struct device *dev = rtlpriv->io.dev;
205 struct usb_device *udev = to_usb_device(dev);
206 u8 request = REALTEK_USB_VENQT_CMD_REQ;
207 u8 reqtype = REALTEK_USB_VENQT_WRITE;
208 u16 wvalue;
209 u16 index = REALTEK_USB_VENQT_CMD_IDX;
210 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
211 u8 *buffer;
212 dma_addr_t dma_addr;
214 wvalue = (u16)(addr&0x0000ffff);
215 buffer = usb_alloc_coherent(udev, (size_t)len, GFP_ATOMIC, &dma_addr);
216 if (!buffer)
217 return;
218 memcpy(buffer, data, len);
219 usb_control_msg(udev, pipe, request, reqtype, wvalue,
220 index, buffer, len, 50);
222 usb_free_coherent(udev, (size_t)len, buffer, dma_addr);
225 static void _rtl_usb_io_handler_init(struct device *dev,
226 struct ieee80211_hw *hw)
228 struct rtl_priv *rtlpriv = rtl_priv(hw);
230 rtlpriv->io.dev = dev;
231 mutex_init(&rtlpriv->io.bb_mutex);
232 rtlpriv->io.write8_async = _usb_write8_async;
233 rtlpriv->io.write16_async = _usb_write16_async;
234 rtlpriv->io.write32_async = _usb_write32_async;
235 rtlpriv->io.read8_sync = _usb_read8_sync;
236 rtlpriv->io.read16_sync = _usb_read16_sync;
237 rtlpriv->io.read32_sync = _usb_read32_sync;
238 rtlpriv->io.writeN_sync = _usb_writeN_sync;
241 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
243 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
245 mutex_destroy(&rtlpriv->io.bb_mutex);
250 * Default aggregation handler. Do nothing and just return the oldest skb.
252 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
253 struct sk_buff_head *list)
255 return skb_dequeue(list);
258 #define IS_HIGH_SPEED_USB(udev) \
259 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
261 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
263 u32 i;
264 struct rtl_priv *rtlpriv = rtl_priv(hw);
265 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
267 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
268 ? USB_HIGH_SPEED_BULK_SIZE
269 : USB_FULL_SPEED_BULK_SIZE;
271 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
272 rtlusb->max_bulk_out_size));
274 for (i = 0; i < __RTL_TXQ_NUM; i++) {
275 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
276 if (!ep_num) {
277 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
278 ("Invalid endpoint map setting!\n"));
279 return -EINVAL;
283 rtlusb->usb_tx_post_hdl =
284 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
285 rtlusb->usb_tx_cleanup =
286 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
287 rtlusb->usb_tx_aggregate_hdl =
288 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
289 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
290 : &_none_usb_tx_aggregate_hdl;
292 init_usb_anchor(&rtlusb->tx_submitted);
293 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
294 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
295 init_usb_anchor(&rtlusb->tx_pending[i]);
297 return 0;
300 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
302 struct rtl_priv *rtlpriv = rtl_priv(hw);
303 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
304 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
306 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
307 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
308 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
309 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
310 rtlusb->usb_rx_segregate_hdl =
311 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
313 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
314 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
315 init_usb_anchor(&rtlusb->rx_submitted);
316 return 0;
319 static int _rtl_usb_init(struct ieee80211_hw *hw)
321 struct rtl_priv *rtlpriv = rtl_priv(hw);
322 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
323 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
324 int err;
325 u8 epidx;
326 struct usb_interface *usb_intf = rtlusb->intf;
327 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
329 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
330 for (epidx = 0; epidx < epnums; epidx++) {
331 struct usb_endpoint_descriptor *pep_desc;
332 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
334 if (usb_endpoint_dir_in(pep_desc))
335 rtlusb->in_ep_nums++;
336 else if (usb_endpoint_dir_out(pep_desc))
337 rtlusb->out_ep_nums++;
339 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
340 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
341 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
342 pep_desc->bInterval));
344 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
345 return -EINVAL ;
347 /* usb endpoint mapping */
348 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
349 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
350 _rtl_usb_init_tx(hw);
351 _rtl_usb_init_rx(hw);
352 return err;
355 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
357 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
358 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
359 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
360 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
362 rtlhal->hw = hw;
363 ppsc->inactiveps = false;
364 ppsc->leisure_ps = false;
365 ppsc->fwctrl_lps = false;
366 ppsc->reg_fwctrl_lps = 3;
367 ppsc->reg_max_lps_awakeintvl = 5;
368 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
370 /* IBSS */
371 mac->beacon_interval = 100;
373 /* AMPDU */
374 mac->min_space_cfg = 0;
375 mac->max_mss_density = 0;
377 /* set sane AMPDU defaults */
378 mac->current_ampdu_density = 7;
379 mac->current_ampdu_factor = 3;
381 /* QOS */
382 rtlusb->acm_method = eAcmWay2_SW;
384 /* IRQ */
385 /* HIMR - turn all on */
386 rtlusb->irq_mask[0] = 0xFFFFFFFF;
387 /* HIMR_EX - turn all on */
388 rtlusb->irq_mask[1] = 0xFFFFFFFF;
389 rtlusb->disableHWSM = true;
390 return 0;
393 #define __RADIO_TAP_SIZE_RSV 32
395 static void _rtl_rx_completed(struct urb *urb);
397 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
398 struct rtl_usb *rtlusb,
399 struct urb *urb,
400 gfp_t gfp_mask)
402 struct sk_buff *skb;
403 struct rtl_priv *rtlpriv = rtl_priv(hw);
405 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
406 gfp_mask);
407 if (!skb) {
408 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
409 ("Failed to __dev_alloc_skb!!\n"))
410 return ERR_PTR(-ENOMEM);
413 /* reserve some space for mac80211's radiotap */
414 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
415 usb_fill_bulk_urb(urb, rtlusb->udev,
416 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
417 skb->data, min(skb_tailroom(skb),
418 (int)rtlusb->rx_max_size),
419 _rtl_rx_completed, skb);
421 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
422 return skb;
425 #undef __RADIO_TAP_SIZE_RSV
427 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
428 struct sk_buff *skb)
430 struct rtl_priv *rtlpriv = rtl_priv(hw);
431 u8 *rxdesc = skb->data;
432 struct ieee80211_hdr *hdr;
433 bool unicast = false;
434 __le16 fc;
435 struct ieee80211_rx_status rx_status = {0};
436 struct rtl_stats stats = {
437 .signal = 0,
438 .noise = -98,
439 .rate = 0,
442 skb_pull(skb, RTL_RX_DESC_SIZE);
443 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
444 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
445 hdr = (struct ieee80211_hdr *)(skb->data);
446 fc = hdr->frame_control;
447 if (!stats.crc) {
448 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
450 if (is_broadcast_ether_addr(hdr->addr1)) {
451 /*TODO*/;
452 } else if (is_multicast_ether_addr(hdr->addr1)) {
453 /*TODO*/
454 } else {
455 unicast = true;
456 rtlpriv->stats.rxbytesunicast += skb->len;
459 rtl_is_special_data(hw, skb, false);
461 if (ieee80211_is_data(fc)) {
462 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
464 if (unicast)
465 rtlpriv->link_info.num_rx_inperiod++;
470 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
471 struct sk_buff *skb)
473 struct rtl_priv *rtlpriv = rtl_priv(hw);
474 u8 *rxdesc = skb->data;
475 struct ieee80211_hdr *hdr;
476 bool unicast = false;
477 __le16 fc;
478 struct ieee80211_rx_status rx_status = {0};
479 struct rtl_stats stats = {
480 .signal = 0,
481 .noise = -98,
482 .rate = 0,
485 skb_pull(skb, RTL_RX_DESC_SIZE);
486 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
487 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
488 hdr = (struct ieee80211_hdr *)(skb->data);
489 fc = hdr->frame_control;
490 if (!stats.crc) {
491 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
493 if (is_broadcast_ether_addr(hdr->addr1)) {
494 /*TODO*/;
495 } else if (is_multicast_ether_addr(hdr->addr1)) {
496 /*TODO*/
497 } else {
498 unicast = true;
499 rtlpriv->stats.rxbytesunicast += skb->len;
502 rtl_is_special_data(hw, skb, false);
504 if (ieee80211_is_data(fc)) {
505 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
507 if (unicast)
508 rtlpriv->link_info.num_rx_inperiod++;
510 if (likely(rtl_action_proc(hw, skb, false))) {
511 struct sk_buff *uskb = NULL;
512 u8 *pdata;
514 uskb = dev_alloc_skb(skb->len + 128);
515 if (uskb) { /* drop packet on allocation failure */
516 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
517 sizeof(rx_status));
518 pdata = (u8 *)skb_put(uskb, skb->len);
519 memcpy(pdata, skb->data, skb->len);
520 ieee80211_rx_irqsafe(hw, uskb);
522 dev_kfree_skb_any(skb);
523 } else {
524 dev_kfree_skb_any(skb);
529 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
531 struct sk_buff *_skb;
532 struct sk_buff_head rx_queue;
533 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
535 skb_queue_head_init(&rx_queue);
536 if (rtlusb->usb_rx_segregate_hdl)
537 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
538 WARN_ON(skb_queue_empty(&rx_queue));
539 while (!skb_queue_empty(&rx_queue)) {
540 _skb = skb_dequeue(&rx_queue);
541 _rtl_usb_rx_process_agg(hw, skb);
542 ieee80211_rx_irqsafe(hw, skb);
546 static void _rtl_rx_completed(struct urb *_urb)
548 struct sk_buff *skb = (struct sk_buff *)_urb->context;
549 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
550 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
551 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
552 struct rtl_priv *rtlpriv = rtl_priv(hw);
553 int err = 0;
555 if (unlikely(IS_USB_STOP(rtlusb)))
556 goto free;
558 if (likely(0 == _urb->status)) {
559 /* If this code were moved to work queue, would CPU
560 * utilization be improved? NOTE: We shall allocate another skb
561 * and reuse the original one.
563 skb_put(skb, _urb->actual_length);
565 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
566 struct sk_buff *_skb;
567 _rtl_usb_rx_process_noagg(hw, skb);
568 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
569 if (IS_ERR(_skb)) {
570 err = PTR_ERR(_skb);
571 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
572 ("Can't allocate skb for bulk IN!\n"));
573 return;
575 skb = _skb;
576 } else{
577 /* TO DO */
578 _rtl_rx_pre_process(hw, skb);
579 pr_err("rx agg not supported\n");
581 goto resubmit;
584 switch (_urb->status) {
585 /* disconnect */
586 case -ENOENT:
587 case -ECONNRESET:
588 case -ENODEV:
589 case -ESHUTDOWN:
590 goto free;
591 default:
592 break;
595 resubmit:
596 skb_reset_tail_pointer(skb);
597 skb_trim(skb, 0);
599 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
600 err = usb_submit_urb(_urb, GFP_ATOMIC);
601 if (unlikely(err)) {
602 usb_unanchor_urb(_urb);
603 goto free;
605 return;
607 free:
608 dev_kfree_skb_irq(skb);
611 static int _rtl_usb_receive(struct ieee80211_hw *hw)
613 struct urb *urb;
614 struct sk_buff *skb;
615 int err;
616 int i;
617 struct rtl_priv *rtlpriv = rtl_priv(hw);
618 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
620 WARN_ON(0 == rtlusb->rx_urb_num);
621 /* 1600 == 1514 + max WLAN header + rtk info */
622 WARN_ON(rtlusb->rx_max_size < 1600);
624 for (i = 0; i < rtlusb->rx_urb_num; i++) {
625 err = -ENOMEM;
626 urb = usb_alloc_urb(0, GFP_KERNEL);
627 if (!urb) {
628 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
629 ("Failed to alloc URB!!\n"))
630 goto err_out;
633 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
634 if (IS_ERR(skb)) {
635 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
636 ("Failed to prep_rx_urb!!\n"))
637 err = PTR_ERR(skb);
638 goto err_out;
641 usb_anchor_urb(urb, &rtlusb->rx_submitted);
642 err = usb_submit_urb(urb, GFP_KERNEL);
643 if (err)
644 goto err_out;
645 usb_free_urb(urb);
647 return 0;
649 err_out:
650 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
651 return err;
654 static int rtl_usb_start(struct ieee80211_hw *hw)
656 int err;
657 struct rtl_priv *rtlpriv = rtl_priv(hw);
658 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
659 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
661 err = rtlpriv->cfg->ops->hw_init(hw);
662 if (!err) {
663 rtl_init_rx_config(hw);
665 /* Enable software */
666 SET_USB_START(rtlusb);
667 /* should after adapter start and interrupt enable. */
668 set_hal_start(rtlhal);
670 /* Start bulk IN */
671 _rtl_usb_receive(hw);
674 return err;
681 /*======================= tx =========================================*/
682 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
684 u32 i;
685 struct sk_buff *_skb;
686 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
687 struct ieee80211_tx_info *txinfo;
689 SET_USB_STOP(rtlusb);
691 /* clean up rx stuff. */
692 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
694 /* clean up tx stuff */
695 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
696 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
697 rtlusb->usb_tx_cleanup(hw, _skb);
698 txinfo = IEEE80211_SKB_CB(_skb);
699 ieee80211_tx_info_clear_status(txinfo);
700 txinfo->flags |= IEEE80211_TX_STAT_ACK;
701 ieee80211_tx_status_irqsafe(hw, _skb);
703 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
705 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
710 * We may add some struct into struct rtl_usb later. Do deinit here.
713 static void rtl_usb_deinit(struct ieee80211_hw *hw)
715 rtl_usb_cleanup(hw);
718 static void rtl_usb_stop(struct ieee80211_hw *hw)
720 struct rtl_priv *rtlpriv = rtl_priv(hw);
721 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
722 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
724 /* should after adapter start and interrupt enable. */
725 set_hal_stop(rtlhal);
726 /* Enable software */
727 SET_USB_STOP(rtlusb);
728 rtl_usb_deinit(hw);
729 rtlpriv->cfg->ops->hw_disable(hw);
732 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
734 int err;
735 struct rtl_priv *rtlpriv = rtl_priv(hw);
736 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
738 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
739 err = usb_submit_urb(_urb, GFP_ATOMIC);
740 if (err < 0) {
741 struct sk_buff *skb;
743 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
744 ("Failed to submit urb.\n"));
745 usb_unanchor_urb(_urb);
746 skb = (struct sk_buff *)_urb->context;
747 kfree_skb(skb);
749 usb_free_urb(_urb);
752 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
753 struct sk_buff *skb)
755 struct rtl_priv *rtlpriv = rtl_priv(hw);
756 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
757 struct ieee80211_tx_info *txinfo;
759 rtlusb->usb_tx_post_hdl(hw, urb, skb);
760 skb_pull(skb, RTL_TX_HEADER_SIZE);
761 txinfo = IEEE80211_SKB_CB(skb);
762 ieee80211_tx_info_clear_status(txinfo);
763 txinfo->flags |= IEEE80211_TX_STAT_ACK;
765 if (urb->status) {
766 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
767 ("Urb has error status 0x%X\n", urb->status));
768 goto out;
770 /* TODO: statistics */
771 out:
772 ieee80211_tx_status_irqsafe(hw, skb);
773 return urb->status;
776 static void _rtl_tx_complete(struct urb *urb)
778 struct sk_buff *skb = (struct sk_buff *)urb->context;
779 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
780 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
781 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
782 int err;
784 if (unlikely(IS_USB_STOP(rtlusb)))
785 return;
786 err = _usb_tx_post(hw, urb, skb);
787 if (err) {
788 /* Ignore error and keep issuiing other urbs */
789 return;
793 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
794 struct sk_buff *skb, u32 ep_num)
796 struct rtl_priv *rtlpriv = rtl_priv(hw);
797 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
798 struct urb *_urb;
800 WARN_ON(NULL == skb);
801 _urb = usb_alloc_urb(0, GFP_ATOMIC);
802 if (!_urb) {
803 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
804 ("Can't allocate URB for bulk out!\n"));
805 kfree_skb(skb);
806 return NULL;
808 _rtl_install_trx_info(rtlusb, skb, ep_num);
809 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
810 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
811 _urb->transfer_flags |= URB_ZERO_PACKET;
812 return _urb;
815 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
816 enum rtl_txq qnum)
818 struct rtl_priv *rtlpriv = rtl_priv(hw);
819 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
820 u32 ep_num;
821 struct urb *_urb = NULL;
822 struct sk_buff *_skb = NULL;
823 struct sk_buff_head *skb_list;
824 struct usb_anchor *urb_list;
826 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
827 if (unlikely(IS_USB_STOP(rtlusb))) {
828 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
829 ("USB device is stopping...\n"));
830 kfree_skb(skb);
831 return;
833 ep_num = rtlusb->ep_map.ep_mapping[qnum];
834 skb_list = &rtlusb->tx_skb_queue[ep_num];
835 _skb = skb;
836 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
837 if (unlikely(!_urb)) {
838 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
839 ("Can't allocate urb. Drop skb!\n"));
840 return;
842 urb_list = &rtlusb->tx_pending[ep_num];
843 _rtl_submit_tx_urb(hw, _urb);
846 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
847 u16 hw_queue)
849 struct rtl_priv *rtlpriv = rtl_priv(hw);
850 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
851 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
852 struct rtl_tx_desc *pdesc = NULL;
853 struct rtl_tcb_desc tcb_desc;
854 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
855 __le16 fc = hdr->frame_control;
856 u8 *pda_addr = hdr->addr1;
857 /* ssn */
858 u8 *qc = NULL;
859 u8 tid = 0;
860 u16 seq_number = 0;
862 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
863 if (ieee80211_is_auth(fc)) {
864 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
865 rtl_ips_nic_on(hw);
868 if (rtlpriv->psc.sw_ps_enabled) {
869 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
870 !ieee80211_has_pm(fc))
871 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
874 rtl_action_proc(hw, skb, true);
875 if (is_multicast_ether_addr(pda_addr))
876 rtlpriv->stats.txbytesmulticast += skb->len;
877 else if (is_broadcast_ether_addr(pda_addr))
878 rtlpriv->stats.txbytesbroadcast += skb->len;
879 else
880 rtlpriv->stats.txbytesunicast += skb->len;
881 if (ieee80211_is_data_qos(fc)) {
882 qc = ieee80211_get_qos_ctl(hdr);
883 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
884 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
885 IEEE80211_SCTL_SEQ) >> 4;
886 seq_number += 1;
887 seq_number <<= 4;
889 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
890 hw_queue, &tcb_desc);
891 if (!ieee80211_has_morefrags(hdr->frame_control)) {
892 if (qc)
893 mac->tids[tid].seq_number = seq_number;
895 if (ieee80211_is_data(fc))
896 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
899 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
900 struct rtl_tcb_desc *dummy)
902 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
903 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
904 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
905 __le16 fc = hdr->frame_control;
906 u16 hw_queue;
908 if (unlikely(is_hal_stop(rtlhal)))
909 goto err_free;
910 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
911 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
912 _rtl_usb_transmit(hw, skb, hw_queue);
913 return NETDEV_TX_OK;
915 err_free:
916 dev_kfree_skb_any(skb);
917 return NETDEV_TX_OK;
920 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
921 struct sk_buff *skb)
923 return false;
926 static struct rtl_intf_ops rtl_usb_ops = {
927 .adapter_start = rtl_usb_start,
928 .adapter_stop = rtl_usb_stop,
929 .adapter_tx = rtl_usb_tx,
930 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
933 int __devinit rtl_usb_probe(struct usb_interface *intf,
934 const struct usb_device_id *id)
936 int err;
937 struct ieee80211_hw *hw = NULL;
938 struct rtl_priv *rtlpriv = NULL;
939 struct usb_device *udev;
940 struct rtl_usb_priv *usb_priv;
942 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
943 sizeof(struct rtl_usb_priv), &rtl_ops);
944 if (!hw) {
945 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
946 return -ENOMEM;
948 rtlpriv = hw->priv;
949 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
950 GFP_KERNEL);
951 if (!rtlpriv->usb_data)
952 return -ENOMEM;
953 rtlpriv->usb_data_index = 0;
954 init_completion(&rtlpriv->firmware_loading_complete);
955 SET_IEEE80211_DEV(hw, &intf->dev);
956 udev = interface_to_usbdev(intf);
957 usb_get_dev(udev);
958 usb_priv = rtl_usbpriv(hw);
959 memset(usb_priv, 0, sizeof(*usb_priv));
960 usb_priv->dev.intf = intf;
961 usb_priv->dev.udev = udev;
962 usb_set_intfdata(intf, hw);
963 /* init cfg & intf_ops */
964 rtlpriv->rtlhal.interface = INTF_USB;
965 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
966 rtlpriv->intf_ops = &rtl_usb_ops;
967 rtl_dbgp_flag_init(hw);
968 /* Init IO handler */
969 _rtl_usb_io_handler_init(&udev->dev, hw);
970 rtlpriv->cfg->ops->read_chip_version(hw);
971 /*like read eeprom and so on */
972 rtlpriv->cfg->ops->read_eeprom_info(hw);
973 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
974 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
975 ("Can't init_sw_vars.\n"));
976 goto error_out;
978 rtlpriv->cfg->ops->init_sw_leds(hw);
979 err = _rtl_usb_init(hw);
980 err = _rtl_usb_init_sw(hw);
981 /* Init mac80211 sw */
982 err = rtl_init_core(hw);
983 if (err) {
984 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
985 ("Can't allocate sw for mac80211.\n"));
986 goto error_out;
989 return 0;
990 error_out:
991 rtl_deinit_core(hw);
992 _rtl_usb_io_handler_release(hw);
993 usb_put_dev(udev);
994 complete(&rtlpriv->firmware_loading_complete);
995 return -ENODEV;
997 EXPORT_SYMBOL(rtl_usb_probe);
999 void rtl_usb_disconnect(struct usb_interface *intf)
1001 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1002 struct rtl_priv *rtlpriv = rtl_priv(hw);
1003 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1004 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1006 if (unlikely(!rtlpriv))
1007 return;
1009 /* just in case driver is removed before firmware callback */
1010 wait_for_completion(&rtlpriv->firmware_loading_complete);
1011 /*ieee80211_unregister_hw will call ops_stop */
1012 if (rtlmac->mac80211_registered == 1) {
1013 ieee80211_unregister_hw(hw);
1014 rtlmac->mac80211_registered = 0;
1015 } else {
1016 rtl_deinit_deferred_work(hw);
1017 rtlpriv->intf_ops->adapter_stop(hw);
1019 /*deinit rfkill */
1020 /* rtl_deinit_rfkill(hw); */
1021 rtl_usb_deinit(hw);
1022 rtl_deinit_core(hw);
1023 kfree(rtlpriv->usb_data);
1024 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1025 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1026 _rtl_usb_io_handler_release(hw);
1027 usb_put_dev(rtlusb->udev);
1028 usb_set_intfdata(intf, NULL);
1029 ieee80211_free_hw(hw);
1031 EXPORT_SYMBOL(rtl_usb_disconnect);
1033 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1035 return 0;
1037 EXPORT_SYMBOL(rtl_usb_suspend);
1039 int rtl_usb_resume(struct usb_interface *pusb_intf)
1041 return 0;
1043 EXPORT_SYMBOL(rtl_usb_resume);