1 /******************************************************************************
3 * Copyright(c) 2009-2012 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
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 *****************************************************************************/
33 #include "rtl8192c/fw_common.h"
34 #include <linux/export.h>
36 #define REALTEK_USB_VENQT_READ 0xC0
37 #define REALTEK_USB_VENQT_WRITE 0x40
38 #define REALTEK_USB_VENQT_CMD_REQ 0x05
39 #define REALTEK_USB_VENQT_CMD_IDX 0x00
41 #define MAX_USBCTRL_VENDORREQ_TIMES 10
43 static void usbctrl_async_callback(struct urb *urb)
49 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
50 u16 value, u16 index, void *pdata,
56 struct usb_ctrlrequest *dr;
58 struct rtl819x_async_write_data {
59 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
60 struct usb_ctrlrequest dr;
63 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
64 reqtype = REALTEK_USB_VENQT_WRITE;
66 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
70 urb = usb_alloc_urb(0, GFP_ATOMIC);
78 dr->bRequestType = reqtype;
79 dr->bRequest = request;
80 dr->wValue = cpu_to_le16(value);
81 dr->wIndex = cpu_to_le16(index);
82 dr->wLength = cpu_to_le16(len);
83 /* data are already in little-endian order */
84 memcpy(buf, pdata, len);
85 usb_fill_control_urb(urb, udev, pipe,
86 (unsigned char *)dr, buf, len,
87 usbctrl_async_callback, buf);
88 rc = usb_submit_urb(urb, GFP_ATOMIC);
95 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
96 u16 value, u16 index, void *pdata,
102 int vendorreq_times = 0;
105 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
106 reqtype = REALTEK_USB_VENQT_READ;
109 status = usb_control_msg(udev, pipe, request, reqtype, value,
110 index, pdata, len, 0); /*max. timeout*/
112 /* firmware download is checksumed, don't retry */
113 if ((value >= FW_8192C_START_ADDRESS &&
114 value <= FW_8192C_END_ADDRESS))
119 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
121 if (status < 0 && count++ < 4)
122 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
123 value, status, le32_to_cpu(*(u32 *)pdata));
127 static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
135 data = kmalloc(sizeof(u32), GFP_KERNEL);
138 request = REALTEK_USB_VENQT_CMD_REQ;
139 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
142 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
143 ret = le32_to_cpu(*data);
148 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
150 struct device *dev = rtlpriv->io.dev;
152 return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
155 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
157 struct device *dev = rtlpriv->io.dev;
159 return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
162 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
164 struct device *dev = rtlpriv->io.dev;
166 return _usb_read_sync(to_usb_device(dev), addr, 4);
169 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
177 request = REALTEK_USB_VENQT_CMD_REQ;
178 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
179 wvalue = (u16)(addr&0x0000ffff);
180 data = cpu_to_le32(val);
181 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
185 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
187 struct device *dev = rtlpriv->io.dev;
189 _usb_write_async(to_usb_device(dev), addr, val, 1);
192 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
194 struct device *dev = rtlpriv->io.dev;
196 _usb_write_async(to_usb_device(dev), addr, val, 2);
199 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
201 struct device *dev = rtlpriv->io.dev;
203 _usb_write_async(to_usb_device(dev), addr, val, 4);
206 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
209 struct device *dev = rtlpriv->io.dev;
210 struct usb_device *udev = to_usb_device(dev);
211 u8 request = REALTEK_USB_VENQT_CMD_REQ;
212 u8 reqtype = REALTEK_USB_VENQT_WRITE;
214 u16 index = REALTEK_USB_VENQT_CMD_IDX;
215 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
219 wvalue = (u16)(addr&0x0000ffff);
220 buffer = usb_alloc_coherent(udev, (size_t)len, GFP_ATOMIC, &dma_addr);
223 memcpy(buffer, data, len);
224 usb_control_msg(udev, pipe, request, reqtype, wvalue,
225 index, buffer, len, 50);
227 usb_free_coherent(udev, (size_t)len, buffer, dma_addr);
230 static void _rtl_usb_io_handler_init(struct device *dev,
231 struct ieee80211_hw *hw)
233 struct rtl_priv *rtlpriv = rtl_priv(hw);
235 rtlpriv->io.dev = dev;
236 mutex_init(&rtlpriv->io.bb_mutex);
237 rtlpriv->io.write8_async = _usb_write8_async;
238 rtlpriv->io.write16_async = _usb_write16_async;
239 rtlpriv->io.write32_async = _usb_write32_async;
240 rtlpriv->io.read8_sync = _usb_read8_sync;
241 rtlpriv->io.read16_sync = _usb_read16_sync;
242 rtlpriv->io.read32_sync = _usb_read32_sync;
243 rtlpriv->io.writeN_sync = _usb_writeN_sync;
246 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
248 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
250 mutex_destroy(&rtlpriv->io.bb_mutex);
255 * Default aggregation handler. Do nothing and just return the oldest skb.
257 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
258 struct sk_buff_head *list)
260 return skb_dequeue(list);
263 #define IS_HIGH_SPEED_USB(udev) \
264 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
266 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
269 struct rtl_priv *rtlpriv = rtl_priv(hw);
270 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
272 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
273 ? USB_HIGH_SPEED_BULK_SIZE
274 : USB_FULL_SPEED_BULK_SIZE;
276 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
277 rtlusb->max_bulk_out_size);
279 for (i = 0; i < __RTL_TXQ_NUM; i++) {
280 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
282 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
283 "Invalid endpoint map setting!\n");
288 rtlusb->usb_tx_post_hdl =
289 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
290 rtlusb->usb_tx_cleanup =
291 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
292 rtlusb->usb_tx_aggregate_hdl =
293 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
294 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
295 : &_none_usb_tx_aggregate_hdl;
297 init_usb_anchor(&rtlusb->tx_submitted);
298 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
299 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
300 init_usb_anchor(&rtlusb->tx_pending[i]);
305 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
307 struct rtl_priv *rtlpriv = rtl_priv(hw);
308 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
309 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
311 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
312 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
313 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
314 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
315 rtlusb->usb_rx_segregate_hdl =
316 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
318 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
319 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
320 init_usb_anchor(&rtlusb->rx_submitted);
324 static int _rtl_usb_init(struct ieee80211_hw *hw)
326 struct rtl_priv *rtlpriv = rtl_priv(hw);
327 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
328 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
331 struct usb_interface *usb_intf = rtlusb->intf;
332 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
334 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
335 for (epidx = 0; epidx < epnums; epidx++) {
336 struct usb_endpoint_descriptor *pep_desc;
337 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
339 if (usb_endpoint_dir_in(pep_desc))
340 rtlusb->in_ep_nums++;
341 else if (usb_endpoint_dir_out(pep_desc))
342 rtlusb->out_ep_nums++;
344 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
345 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
346 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
347 pep_desc->bInterval);
349 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
350 pr_err("Too few input end points found\n");
353 if (rtlusb->out_ep_nums == 0) {
354 pr_err("No output end points found\n");
357 /* usb endpoint mapping */
358 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
359 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
360 _rtl_usb_init_tx(hw);
361 _rtl_usb_init_rx(hw);
365 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
367 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
368 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
369 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
370 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
373 ppsc->inactiveps = false;
374 ppsc->leisure_ps = false;
375 ppsc->fwctrl_lps = false;
376 ppsc->reg_fwctrl_lps = 3;
377 ppsc->reg_max_lps_awakeintvl = 5;
378 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
381 mac->beacon_interval = 100;
384 mac->min_space_cfg = 0;
385 mac->max_mss_density = 0;
387 /* set sane AMPDU defaults */
388 mac->current_ampdu_density = 7;
389 mac->current_ampdu_factor = 3;
392 rtlusb->acm_method = eAcmWay2_SW;
395 /* HIMR - turn all on */
396 rtlusb->irq_mask[0] = 0xFFFFFFFF;
397 /* HIMR_EX - turn all on */
398 rtlusb->irq_mask[1] = 0xFFFFFFFF;
399 rtlusb->disableHWSM = true;
403 #define __RADIO_TAP_SIZE_RSV 32
405 static void _rtl_rx_completed(struct urb *urb);
407 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
408 struct rtl_usb *rtlusb,
413 struct rtl_priv *rtlpriv = rtl_priv(hw);
415 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
418 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
419 "Failed to __dev_alloc_skb!!\n");
420 return ERR_PTR(-ENOMEM);
423 /* reserve some space for mac80211's radiotap */
424 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
425 usb_fill_bulk_urb(urb, rtlusb->udev,
426 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
427 skb->data, min(skb_tailroom(skb),
428 (int)rtlusb->rx_max_size),
429 _rtl_rx_completed, skb);
431 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
435 #undef __RADIO_TAP_SIZE_RSV
437 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
440 struct rtl_priv *rtlpriv = rtl_priv(hw);
441 u8 *rxdesc = skb->data;
442 struct ieee80211_hdr *hdr;
443 bool unicast = false;
445 struct ieee80211_rx_status rx_status = {0};
446 struct rtl_stats stats = {
452 skb_pull(skb, RTL_RX_DESC_SIZE);
453 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
454 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
455 hdr = (struct ieee80211_hdr *)(skb->data);
456 fc = hdr->frame_control;
458 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
460 if (is_broadcast_ether_addr(hdr->addr1)) {
462 } else if (is_multicast_ether_addr(hdr->addr1)) {
466 rtlpriv->stats.rxbytesunicast += skb->len;
469 rtl_is_special_data(hw, skb, false);
471 if (ieee80211_is_data(fc)) {
472 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
475 rtlpriv->link_info.num_rx_inperiod++;
480 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
483 struct rtl_priv *rtlpriv = rtl_priv(hw);
484 u8 *rxdesc = skb->data;
485 struct ieee80211_hdr *hdr;
486 bool unicast = false;
488 struct ieee80211_rx_status rx_status = {0};
489 struct rtl_stats stats = {
495 skb_pull(skb, RTL_RX_DESC_SIZE);
496 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
497 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
498 hdr = (struct ieee80211_hdr *)(skb->data);
499 fc = hdr->frame_control;
501 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
503 if (is_broadcast_ether_addr(hdr->addr1)) {
505 } else if (is_multicast_ether_addr(hdr->addr1)) {
509 rtlpriv->stats.rxbytesunicast += skb->len;
512 rtl_is_special_data(hw, skb, false);
514 if (ieee80211_is_data(fc)) {
515 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
518 rtlpriv->link_info.num_rx_inperiod++;
520 if (likely(rtl_action_proc(hw, skb, false))) {
521 struct sk_buff *uskb = NULL;
524 uskb = dev_alloc_skb(skb->len + 128);
525 if (uskb) { /* drop packet on allocation failure */
526 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
528 pdata = (u8 *)skb_put(uskb, skb->len);
529 memcpy(pdata, skb->data, skb->len);
530 ieee80211_rx_irqsafe(hw, uskb);
532 dev_kfree_skb_any(skb);
534 dev_kfree_skb_any(skb);
539 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
541 struct sk_buff *_skb;
542 struct sk_buff_head rx_queue;
543 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
545 skb_queue_head_init(&rx_queue);
546 if (rtlusb->usb_rx_segregate_hdl)
547 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
548 WARN_ON(skb_queue_empty(&rx_queue));
549 while (!skb_queue_empty(&rx_queue)) {
550 _skb = skb_dequeue(&rx_queue);
551 _rtl_usb_rx_process_agg(hw, skb);
552 ieee80211_rx_irqsafe(hw, skb);
556 static void _rtl_rx_completed(struct urb *_urb)
558 struct sk_buff *skb = (struct sk_buff *)_urb->context;
559 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
560 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
561 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
562 struct rtl_priv *rtlpriv = rtl_priv(hw);
565 if (unlikely(IS_USB_STOP(rtlusb)))
568 if (likely(0 == _urb->status)) {
569 /* If this code were moved to work queue, would CPU
570 * utilization be improved? NOTE: We shall allocate another skb
571 * and reuse the original one.
573 skb_put(skb, _urb->actual_length);
575 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
576 struct sk_buff *_skb;
577 _rtl_usb_rx_process_noagg(hw, skb);
578 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
581 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
582 "Can't allocate skb for bulk IN!\n");
588 _rtl_rx_pre_process(hw, skb);
589 pr_err("rx agg not supported\n");
594 switch (_urb->status) {
606 skb_reset_tail_pointer(skb);
609 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
610 err = usb_submit_urb(_urb, GFP_ATOMIC);
612 usb_unanchor_urb(_urb);
618 dev_kfree_skb_irq(skb);
621 static int _rtl_usb_receive(struct ieee80211_hw *hw)
627 struct rtl_priv *rtlpriv = rtl_priv(hw);
628 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
630 WARN_ON(0 == rtlusb->rx_urb_num);
631 /* 1600 == 1514 + max WLAN header + rtk info */
632 WARN_ON(rtlusb->rx_max_size < 1600);
634 for (i = 0; i < rtlusb->rx_urb_num; i++) {
636 urb = usb_alloc_urb(0, GFP_KERNEL);
638 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
639 "Failed to alloc URB!!\n");
643 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
645 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
646 "Failed to prep_rx_urb!!\n");
651 usb_anchor_urb(urb, &rtlusb->rx_submitted);
652 err = usb_submit_urb(urb, GFP_KERNEL);
660 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
664 static int rtl_usb_start(struct ieee80211_hw *hw)
667 struct rtl_priv *rtlpriv = rtl_priv(hw);
668 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
669 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
671 err = rtlpriv->cfg->ops->hw_init(hw);
673 rtl_init_rx_config(hw);
675 /* Enable software */
676 SET_USB_START(rtlusb);
677 /* should after adapter start and interrupt enable. */
678 set_hal_start(rtlhal);
681 _rtl_usb_receive(hw);
691 /*======================= tx =========================================*/
692 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
695 struct sk_buff *_skb;
696 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
697 struct ieee80211_tx_info *txinfo;
699 SET_USB_STOP(rtlusb);
701 /* clean up rx stuff. */
702 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
704 /* clean up tx stuff */
705 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
706 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
707 rtlusb->usb_tx_cleanup(hw, _skb);
708 txinfo = IEEE80211_SKB_CB(_skb);
709 ieee80211_tx_info_clear_status(txinfo);
710 txinfo->flags |= IEEE80211_TX_STAT_ACK;
711 ieee80211_tx_status_irqsafe(hw, _skb);
713 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
715 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
720 * We may add some struct into struct rtl_usb later. Do deinit here.
723 static void rtl_usb_deinit(struct ieee80211_hw *hw)
728 static void rtl_usb_stop(struct ieee80211_hw *hw)
730 struct rtl_priv *rtlpriv = rtl_priv(hw);
731 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
732 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
734 /* should after adapter start and interrupt enable. */
735 set_hal_stop(rtlhal);
736 /* Enable software */
737 SET_USB_STOP(rtlusb);
739 rtlpriv->cfg->ops->hw_disable(hw);
742 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
745 struct rtl_priv *rtlpriv = rtl_priv(hw);
746 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
748 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
749 err = usb_submit_urb(_urb, GFP_ATOMIC);
753 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
754 "Failed to submit urb\n");
755 usb_unanchor_urb(_urb);
756 skb = (struct sk_buff *)_urb->context;
762 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
765 struct rtl_priv *rtlpriv = rtl_priv(hw);
766 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
767 struct ieee80211_tx_info *txinfo;
769 rtlusb->usb_tx_post_hdl(hw, urb, skb);
770 skb_pull(skb, RTL_TX_HEADER_SIZE);
771 txinfo = IEEE80211_SKB_CB(skb);
772 ieee80211_tx_info_clear_status(txinfo);
773 txinfo->flags |= IEEE80211_TX_STAT_ACK;
776 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
777 "Urb has error status 0x%X\n", urb->status);
780 /* TODO: statistics */
782 ieee80211_tx_status_irqsafe(hw, skb);
786 static void _rtl_tx_complete(struct urb *urb)
788 struct sk_buff *skb = (struct sk_buff *)urb->context;
789 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
790 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
791 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
794 if (unlikely(IS_USB_STOP(rtlusb)))
796 err = _usb_tx_post(hw, urb, skb);
798 /* Ignore error and keep issuiing other urbs */
803 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
804 struct sk_buff *skb, u32 ep_num)
806 struct rtl_priv *rtlpriv = rtl_priv(hw);
807 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
810 WARN_ON(NULL == skb);
811 _urb = usb_alloc_urb(0, GFP_ATOMIC);
813 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
814 "Can't allocate URB for bulk out!\n");
818 _rtl_install_trx_info(rtlusb, skb, ep_num);
819 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
820 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
821 _urb->transfer_flags |= URB_ZERO_PACKET;
825 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
828 struct rtl_priv *rtlpriv = rtl_priv(hw);
829 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
831 struct urb *_urb = NULL;
832 struct sk_buff *_skb = NULL;
833 struct sk_buff_head *skb_list;
834 struct usb_anchor *urb_list;
836 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
837 if (unlikely(IS_USB_STOP(rtlusb))) {
838 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
839 "USB device is stopping...\n");
843 ep_num = rtlusb->ep_map.ep_mapping[qnum];
844 skb_list = &rtlusb->tx_skb_queue[ep_num];
846 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
847 if (unlikely(!_urb)) {
848 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
849 "Can't allocate urb. Drop skb!\n");
852 urb_list = &rtlusb->tx_pending[ep_num];
853 _rtl_submit_tx_urb(hw, _urb);
856 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
859 struct rtl_priv *rtlpriv = rtl_priv(hw);
860 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
861 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
862 struct rtl_tx_desc *pdesc = NULL;
863 struct rtl_tcb_desc tcb_desc;
864 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
865 __le16 fc = hdr->frame_control;
866 u8 *pda_addr = hdr->addr1;
872 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
873 if (ieee80211_is_auth(fc)) {
874 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
878 if (rtlpriv->psc.sw_ps_enabled) {
879 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
880 !ieee80211_has_pm(fc))
881 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
884 rtl_action_proc(hw, skb, true);
885 if (is_multicast_ether_addr(pda_addr))
886 rtlpriv->stats.txbytesmulticast += skb->len;
887 else if (is_broadcast_ether_addr(pda_addr))
888 rtlpriv->stats.txbytesbroadcast += skb->len;
890 rtlpriv->stats.txbytesunicast += skb->len;
891 if (ieee80211_is_data_qos(fc)) {
892 qc = ieee80211_get_qos_ctl(hdr);
893 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
894 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
895 IEEE80211_SCTL_SEQ) >> 4;
899 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
900 hw_queue, &tcb_desc);
901 if (!ieee80211_has_morefrags(hdr->frame_control)) {
903 mac->tids[tid].seq_number = seq_number;
905 if (ieee80211_is_data(fc))
906 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
909 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
910 struct rtl_tcb_desc *dummy)
912 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
913 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
914 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
915 __le16 fc = hdr->frame_control;
918 if (unlikely(is_hal_stop(rtlhal)))
920 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
921 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
922 _rtl_usb_transmit(hw, skb, hw_queue);
926 dev_kfree_skb_any(skb);
930 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
936 static struct rtl_intf_ops rtl_usb_ops = {
937 .adapter_start = rtl_usb_start,
938 .adapter_stop = rtl_usb_stop,
939 .adapter_tx = rtl_usb_tx,
940 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
943 int __devinit rtl_usb_probe(struct usb_interface *intf,
944 const struct usb_device_id *id)
947 struct ieee80211_hw *hw = NULL;
948 struct rtl_priv *rtlpriv = NULL;
949 struct usb_device *udev;
950 struct rtl_usb_priv *usb_priv;
952 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
953 sizeof(struct rtl_usb_priv), &rtl_ops);
955 RT_ASSERT(false, "ieee80211 alloc failed\n");
959 init_completion(&rtlpriv->firmware_loading_complete);
960 SET_IEEE80211_DEV(hw, &intf->dev);
961 udev = interface_to_usbdev(intf);
963 usb_priv = rtl_usbpriv(hw);
964 memset(usb_priv, 0, sizeof(*usb_priv));
965 usb_priv->dev.intf = intf;
966 usb_priv->dev.udev = udev;
967 usb_set_intfdata(intf, hw);
968 /* init cfg & intf_ops */
969 rtlpriv->rtlhal.interface = INTF_USB;
970 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
971 rtlpriv->intf_ops = &rtl_usb_ops;
972 rtl_dbgp_flag_init(hw);
973 /* Init IO handler */
974 _rtl_usb_io_handler_init(&udev->dev, hw);
975 rtlpriv->cfg->ops->read_chip_version(hw);
976 /*like read eeprom and so on */
977 rtlpriv->cfg->ops->read_eeprom_info(hw);
978 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
979 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
982 rtlpriv->cfg->ops->init_sw_leds(hw);
983 err = _rtl_usb_init(hw);
986 err = _rtl_usb_init_sw(hw);
987 /* Init mac80211 sw */
988 err = rtl_init_core(hw);
990 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
991 "Can't allocate sw for mac80211\n");
998 _rtl_usb_io_handler_release(hw);
1000 complete(&rtlpriv->firmware_loading_complete);
1003 EXPORT_SYMBOL(rtl_usb_probe);
1005 void rtl_usb_disconnect(struct usb_interface *intf)
1007 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1008 struct rtl_priv *rtlpriv = rtl_priv(hw);
1009 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1010 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1012 if (unlikely(!rtlpriv))
1015 /* just in case driver is removed before firmware callback */
1016 wait_for_completion(&rtlpriv->firmware_loading_complete);
1017 /*ieee80211_unregister_hw will call ops_stop */
1018 if (rtlmac->mac80211_registered == 1) {
1019 ieee80211_unregister_hw(hw);
1020 rtlmac->mac80211_registered = 0;
1022 rtl_deinit_deferred_work(hw);
1023 rtlpriv->intf_ops->adapter_stop(hw);
1026 /* rtl_deinit_rfkill(hw); */
1028 rtl_deinit_core(hw);
1029 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1030 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1031 _rtl_usb_io_handler_release(hw);
1032 usb_put_dev(rtlusb->udev);
1033 usb_set_intfdata(intf, NULL);
1034 ieee80211_free_hw(hw);
1036 EXPORT_SYMBOL(rtl_usb_disconnect);
1038 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1042 EXPORT_SYMBOL(rtl_usb_suspend);
1044 int rtl_usb_resume(struct usb_interface *pusb_intf)
1048 EXPORT_SYMBOL(rtl_usb_resume);