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, *(u32 *)pdata);
127 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
129 struct device *dev = rtlpriv->io.dev;
130 struct usb_device *udev = to_usb_device(dev);
137 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
138 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
139 rtlpriv->usb_data_index = 0;
140 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
141 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
142 request = REALTEK_USB_VENQT_CMD_REQ;
143 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
146 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
147 return le32_to_cpu(*data);
150 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
152 return (u8)_usb_read_sync(rtlpriv, addr, 1);
155 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
157 return (u16)_usb_read_sync(rtlpriv, addr, 2);
160 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
162 return _usb_read_sync(rtlpriv, addr, 4);
165 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
173 request = REALTEK_USB_VENQT_CMD_REQ;
174 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
175 wvalue = (u16)(addr&0x0000ffff);
176 data = cpu_to_le32(val);
177 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
181 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
183 struct device *dev = rtlpriv->io.dev;
185 _usb_write_async(to_usb_device(dev), addr, val, 1);
188 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
190 struct device *dev = rtlpriv->io.dev;
192 _usb_write_async(to_usb_device(dev), addr, val, 2);
195 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
197 struct device *dev = rtlpriv->io.dev;
199 _usb_write_async(to_usb_device(dev), addr, val, 4);
202 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
205 struct device *dev = rtlpriv->io.dev;
206 struct usb_device *udev = to_usb_device(dev);
207 u8 request = REALTEK_USB_VENQT_CMD_REQ;
208 u8 reqtype = REALTEK_USB_VENQT_WRITE;
210 u16 index = REALTEK_USB_VENQT_CMD_IDX;
211 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
215 wvalue = (u16)(addr&0x0000ffff);
216 buffer = usb_alloc_coherent(udev, (size_t)len, GFP_ATOMIC, &dma_addr);
219 memcpy(buffer, data, len);
220 usb_control_msg(udev, pipe, request, reqtype, wvalue,
221 index, buffer, len, 50);
223 usb_free_coherent(udev, (size_t)len, buffer, dma_addr);
226 static void _rtl_usb_io_handler_init(struct device *dev,
227 struct ieee80211_hw *hw)
229 struct rtl_priv *rtlpriv = rtl_priv(hw);
231 rtlpriv->io.dev = dev;
232 mutex_init(&rtlpriv->io.bb_mutex);
233 rtlpriv->io.write8_async = _usb_write8_async;
234 rtlpriv->io.write16_async = _usb_write16_async;
235 rtlpriv->io.write32_async = _usb_write32_async;
236 rtlpriv->io.read8_sync = _usb_read8_sync;
237 rtlpriv->io.read16_sync = _usb_read16_sync;
238 rtlpriv->io.read32_sync = _usb_read32_sync;
239 rtlpriv->io.writeN_sync = _usb_writeN_sync;
242 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
244 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
246 mutex_destroy(&rtlpriv->io.bb_mutex);
251 * Default aggregation handler. Do nothing and just return the oldest skb.
253 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
254 struct sk_buff_head *list)
256 return skb_dequeue(list);
259 #define IS_HIGH_SPEED_USB(udev) \
260 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
262 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
265 struct rtl_priv *rtlpriv = rtl_priv(hw);
266 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
268 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
269 ? USB_HIGH_SPEED_BULK_SIZE
270 : USB_FULL_SPEED_BULK_SIZE;
272 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
273 rtlusb->max_bulk_out_size);
275 for (i = 0; i < __RTL_TXQ_NUM; i++) {
276 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
278 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
279 "Invalid endpoint map setting!\n");
284 rtlusb->usb_tx_post_hdl =
285 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
286 rtlusb->usb_tx_cleanup =
287 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
288 rtlusb->usb_tx_aggregate_hdl =
289 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
290 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
291 : &_none_usb_tx_aggregate_hdl;
293 init_usb_anchor(&rtlusb->tx_submitted);
294 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
295 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
296 init_usb_anchor(&rtlusb->tx_pending[i]);
301 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
303 struct rtl_priv *rtlpriv = rtl_priv(hw);
304 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
305 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
307 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
308 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
309 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
310 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
311 rtlusb->usb_rx_segregate_hdl =
312 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
314 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
315 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
316 init_usb_anchor(&rtlusb->rx_submitted);
320 static int _rtl_usb_init(struct ieee80211_hw *hw)
322 struct rtl_priv *rtlpriv = rtl_priv(hw);
323 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
324 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
327 struct usb_interface *usb_intf = rtlusb->intf;
328 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
330 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
331 for (epidx = 0; epidx < epnums; epidx++) {
332 struct usb_endpoint_descriptor *pep_desc;
333 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
335 if (usb_endpoint_dir_in(pep_desc))
336 rtlusb->in_ep_nums++;
337 else if (usb_endpoint_dir_out(pep_desc))
338 rtlusb->out_ep_nums++;
340 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
341 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
342 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
343 pep_desc->bInterval);
345 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
346 pr_err("Too few input end points found\n");
349 if (rtlusb->out_ep_nums == 0) {
350 pr_err("No output end points found\n");
353 /* usb endpoint mapping */
354 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
355 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
356 _rtl_usb_init_tx(hw);
357 _rtl_usb_init_rx(hw);
361 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
363 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
364 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
365 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
366 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
369 ppsc->inactiveps = false;
370 ppsc->leisure_ps = false;
371 ppsc->fwctrl_lps = false;
372 ppsc->reg_fwctrl_lps = 3;
373 ppsc->reg_max_lps_awakeintvl = 5;
374 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
377 mac->beacon_interval = 100;
380 mac->min_space_cfg = 0;
381 mac->max_mss_density = 0;
383 /* set sane AMPDU defaults */
384 mac->current_ampdu_density = 7;
385 mac->current_ampdu_factor = 3;
388 rtlusb->acm_method = eAcmWay2_SW;
391 /* HIMR - turn all on */
392 rtlusb->irq_mask[0] = 0xFFFFFFFF;
393 /* HIMR_EX - turn all on */
394 rtlusb->irq_mask[1] = 0xFFFFFFFF;
395 rtlusb->disableHWSM = true;
398 #define __RADIO_TAP_SIZE_RSV 32
400 static void _rtl_rx_completed(struct urb *urb);
402 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
403 struct rtl_usb *rtlusb,
408 struct rtl_priv *rtlpriv = rtl_priv(hw);
410 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
413 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
414 "Failed to __dev_alloc_skb!!\n");
415 return ERR_PTR(-ENOMEM);
418 /* reserve some space for mac80211's radiotap */
419 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
420 usb_fill_bulk_urb(urb, rtlusb->udev,
421 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
422 skb->data, min(skb_tailroom(skb),
423 (int)rtlusb->rx_max_size),
424 _rtl_rx_completed, skb);
426 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
430 #undef __RADIO_TAP_SIZE_RSV
432 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
435 struct rtl_priv *rtlpriv = rtl_priv(hw);
436 u8 *rxdesc = skb->data;
437 struct ieee80211_hdr *hdr;
438 bool unicast = false;
440 struct ieee80211_rx_status rx_status = {0};
441 struct rtl_stats stats = {
447 skb_pull(skb, RTL_RX_DESC_SIZE);
448 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
449 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
450 hdr = (struct ieee80211_hdr *)(skb->data);
451 fc = hdr->frame_control;
453 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
455 if (is_broadcast_ether_addr(hdr->addr1)) {
457 } else if (is_multicast_ether_addr(hdr->addr1)) {
461 rtlpriv->stats.rxbytesunicast += skb->len;
464 rtl_is_special_data(hw, skb, false);
466 if (ieee80211_is_data(fc)) {
467 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
470 rtlpriv->link_info.num_rx_inperiod++;
475 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
478 struct rtl_priv *rtlpriv = rtl_priv(hw);
479 u8 *rxdesc = skb->data;
480 struct ieee80211_hdr *hdr;
481 bool unicast = false;
483 struct ieee80211_rx_status rx_status = {0};
484 struct rtl_stats stats = {
490 skb_pull(skb, RTL_RX_DESC_SIZE);
491 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
492 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
493 hdr = (struct ieee80211_hdr *)(skb->data);
494 fc = hdr->frame_control;
496 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
498 if (is_broadcast_ether_addr(hdr->addr1)) {
500 } else if (is_multicast_ether_addr(hdr->addr1)) {
504 rtlpriv->stats.rxbytesunicast += skb->len;
507 rtl_is_special_data(hw, skb, false);
509 if (ieee80211_is_data(fc)) {
510 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
513 rtlpriv->link_info.num_rx_inperiod++;
515 if (likely(rtl_action_proc(hw, skb, false))) {
516 struct sk_buff *uskb = NULL;
519 uskb = dev_alloc_skb(skb->len + 128);
520 if (uskb) { /* drop packet on allocation failure */
521 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
523 pdata = (u8 *)skb_put(uskb, skb->len);
524 memcpy(pdata, skb->data, skb->len);
525 ieee80211_rx_irqsafe(hw, uskb);
527 dev_kfree_skb_any(skb);
529 dev_kfree_skb_any(skb);
534 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
536 struct sk_buff *_skb;
537 struct sk_buff_head rx_queue;
538 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
540 skb_queue_head_init(&rx_queue);
541 if (rtlusb->usb_rx_segregate_hdl)
542 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
543 WARN_ON(skb_queue_empty(&rx_queue));
544 while (!skb_queue_empty(&rx_queue)) {
545 _skb = skb_dequeue(&rx_queue);
546 _rtl_usb_rx_process_agg(hw, skb);
547 ieee80211_rx_irqsafe(hw, skb);
551 static void _rtl_rx_completed(struct urb *_urb)
553 struct sk_buff *skb = (struct sk_buff *)_urb->context;
554 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
555 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
556 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
557 struct rtl_priv *rtlpriv = rtl_priv(hw);
560 if (unlikely(IS_USB_STOP(rtlusb)))
563 if (likely(0 == _urb->status)) {
564 /* If this code were moved to work queue, would CPU
565 * utilization be improved? NOTE: We shall allocate another skb
566 * and reuse the original one.
568 skb_put(skb, _urb->actual_length);
570 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
571 struct sk_buff *_skb;
572 _rtl_usb_rx_process_noagg(hw, skb);
573 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
576 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
577 "Can't allocate skb for bulk IN!\n");
583 _rtl_rx_pre_process(hw, skb);
584 pr_err("rx agg not supported\n");
589 switch (_urb->status) {
601 skb_reset_tail_pointer(skb);
604 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
605 err = usb_submit_urb(_urb, GFP_ATOMIC);
607 usb_unanchor_urb(_urb);
613 dev_kfree_skb_irq(skb);
616 static int _rtl_usb_receive(struct ieee80211_hw *hw)
622 struct rtl_priv *rtlpriv = rtl_priv(hw);
623 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
625 WARN_ON(0 == rtlusb->rx_urb_num);
626 /* 1600 == 1514 + max WLAN header + rtk info */
627 WARN_ON(rtlusb->rx_max_size < 1600);
629 for (i = 0; i < rtlusb->rx_urb_num; i++) {
631 urb = usb_alloc_urb(0, GFP_KERNEL);
633 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
634 "Failed to alloc URB!!\n");
638 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
640 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
641 "Failed to prep_rx_urb!!\n");
646 usb_anchor_urb(urb, &rtlusb->rx_submitted);
647 err = usb_submit_urb(urb, GFP_KERNEL);
655 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
659 static int rtl_usb_start(struct ieee80211_hw *hw)
662 struct rtl_priv *rtlpriv = rtl_priv(hw);
663 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
664 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
666 err = rtlpriv->cfg->ops->hw_init(hw);
668 rtl_init_rx_config(hw);
670 /* Enable software */
671 SET_USB_START(rtlusb);
672 /* should after adapter start and interrupt enable. */
673 set_hal_start(rtlhal);
676 err = _rtl_usb_receive(hw);
686 /*======================= tx =========================================*/
687 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
690 struct sk_buff *_skb;
691 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
692 struct ieee80211_tx_info *txinfo;
694 SET_USB_STOP(rtlusb);
696 /* clean up rx stuff. */
697 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
699 /* clean up tx stuff */
700 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
701 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
702 rtlusb->usb_tx_cleanup(hw, _skb);
703 txinfo = IEEE80211_SKB_CB(_skb);
704 ieee80211_tx_info_clear_status(txinfo);
705 txinfo->flags |= IEEE80211_TX_STAT_ACK;
706 ieee80211_tx_status_irqsafe(hw, _skb);
708 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
710 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
715 * We may add some struct into struct rtl_usb later. Do deinit here.
718 static void rtl_usb_deinit(struct ieee80211_hw *hw)
723 static void rtl_usb_stop(struct ieee80211_hw *hw)
725 struct rtl_priv *rtlpriv = rtl_priv(hw);
726 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
727 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
729 /* should after adapter start and interrupt enable. */
730 set_hal_stop(rtlhal);
731 /* Enable software */
732 SET_USB_STOP(rtlusb);
734 rtlpriv->cfg->ops->hw_disable(hw);
737 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
740 struct rtl_priv *rtlpriv = rtl_priv(hw);
741 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
743 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
744 err = usb_submit_urb(_urb, GFP_ATOMIC);
748 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
749 "Failed to submit urb\n");
750 usb_unanchor_urb(_urb);
751 skb = (struct sk_buff *)_urb->context;
757 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
760 struct rtl_priv *rtlpriv = rtl_priv(hw);
761 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
762 struct ieee80211_tx_info *txinfo;
764 rtlusb->usb_tx_post_hdl(hw, urb, skb);
765 skb_pull(skb, RTL_TX_HEADER_SIZE);
766 txinfo = IEEE80211_SKB_CB(skb);
767 ieee80211_tx_info_clear_status(txinfo);
768 txinfo->flags |= IEEE80211_TX_STAT_ACK;
771 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
772 "Urb has error status 0x%X\n", urb->status);
775 /* TODO: statistics */
777 ieee80211_tx_status_irqsafe(hw, skb);
781 static void _rtl_tx_complete(struct urb *urb)
783 struct sk_buff *skb = (struct sk_buff *)urb->context;
784 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
785 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
786 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
789 if (unlikely(IS_USB_STOP(rtlusb)))
791 err = _usb_tx_post(hw, urb, skb);
793 /* Ignore error and keep issuiing other urbs */
798 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
799 struct sk_buff *skb, u32 ep_num)
801 struct rtl_priv *rtlpriv = rtl_priv(hw);
802 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
805 WARN_ON(NULL == skb);
806 _urb = usb_alloc_urb(0, GFP_ATOMIC);
808 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
809 "Can't allocate URB for bulk out!\n");
813 _rtl_install_trx_info(rtlusb, skb, ep_num);
814 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
815 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
816 _urb->transfer_flags |= URB_ZERO_PACKET;
820 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
823 struct rtl_priv *rtlpriv = rtl_priv(hw);
824 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
826 struct urb *_urb = NULL;
827 struct sk_buff *_skb = NULL;
828 struct sk_buff_head *skb_list;
829 struct usb_anchor *urb_list;
831 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
832 if (unlikely(IS_USB_STOP(rtlusb))) {
833 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
834 "USB device is stopping...\n");
838 ep_num = rtlusb->ep_map.ep_mapping[qnum];
839 skb_list = &rtlusb->tx_skb_queue[ep_num];
841 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
842 if (unlikely(!_urb)) {
843 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
844 "Can't allocate urb. Drop skb!\n");
847 urb_list = &rtlusb->tx_pending[ep_num];
848 _rtl_submit_tx_urb(hw, _urb);
851 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
852 struct ieee80211_sta *sta,
856 struct rtl_priv *rtlpriv = rtl_priv(hw);
857 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
858 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
859 struct rtl_tx_desc *pdesc = NULL;
860 struct rtl_tcb_desc tcb_desc;
861 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
862 __le16 fc = hdr->frame_control;
863 u8 *pda_addr = hdr->addr1;
869 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
870 if (ieee80211_is_auth(fc)) {
871 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
875 if (rtlpriv->psc.sw_ps_enabled) {
876 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
877 !ieee80211_has_pm(fc))
878 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
881 rtl_action_proc(hw, skb, true);
882 if (is_multicast_ether_addr(pda_addr))
883 rtlpriv->stats.txbytesmulticast += skb->len;
884 else if (is_broadcast_ether_addr(pda_addr))
885 rtlpriv->stats.txbytesbroadcast += skb->len;
887 rtlpriv->stats.txbytesunicast += skb->len;
888 if (ieee80211_is_data_qos(fc)) {
889 qc = ieee80211_get_qos_ctl(hdr);
890 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
891 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
892 IEEE80211_SCTL_SEQ) >> 4;
896 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
897 hw_queue, &tcb_desc);
898 if (!ieee80211_has_morefrags(hdr->frame_control)) {
900 mac->tids[tid].seq_number = seq_number;
902 if (ieee80211_is_data(fc))
903 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
906 static int rtl_usb_tx(struct ieee80211_hw *hw,
907 struct ieee80211_sta *sta,
909 struct rtl_tcb_desc *dummy)
911 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
912 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
913 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
914 __le16 fc = hdr->frame_control;
917 if (unlikely(is_hal_stop(rtlhal)))
919 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
920 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
921 _rtl_usb_transmit(hw, skb, hw_queue);
925 dev_kfree_skb_any(skb);
929 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
930 struct ieee80211_sta *sta,
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 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 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
961 if (!rtlpriv->usb_data)
964 /* this spin lock must be initialized early */
965 spin_lock_init(&rtlpriv->locks.usb_lock);
967 rtlpriv->usb_data_index = 0;
968 init_completion(&rtlpriv->firmware_loading_complete);
969 SET_IEEE80211_DEV(hw, &intf->dev);
970 udev = interface_to_usbdev(intf);
972 usb_priv = rtl_usbpriv(hw);
973 memset(usb_priv, 0, sizeof(*usb_priv));
974 usb_priv->dev.intf = intf;
975 usb_priv->dev.udev = udev;
976 usb_set_intfdata(intf, hw);
977 /* init cfg & intf_ops */
978 rtlpriv->rtlhal.interface = INTF_USB;
979 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
980 rtlpriv->intf_ops = &rtl_usb_ops;
981 rtl_dbgp_flag_init(hw);
982 /* Init IO handler */
983 _rtl_usb_io_handler_init(&udev->dev, hw);
984 rtlpriv->cfg->ops->read_chip_version(hw);
985 /*like read eeprom and so on */
986 rtlpriv->cfg->ops->read_eeprom_info(hw);
987 err = _rtl_usb_init(hw);
991 /* Init mac80211 sw */
992 err = rtl_init_core(hw);
994 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
995 "Can't allocate sw for mac80211\n");
998 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
999 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1002 rtlpriv->cfg->ops->init_sw_leds(hw);
1006 rtl_deinit_core(hw);
1007 _rtl_usb_io_handler_release(hw);
1009 complete(&rtlpriv->firmware_loading_complete);
1012 EXPORT_SYMBOL(rtl_usb_probe);
1014 void rtl_usb_disconnect(struct usb_interface *intf)
1016 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1017 struct rtl_priv *rtlpriv = rtl_priv(hw);
1018 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1019 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1021 if (unlikely(!rtlpriv))
1024 /* just in case driver is removed before firmware callback */
1025 wait_for_completion(&rtlpriv->firmware_loading_complete);
1026 /*ieee80211_unregister_hw will call ops_stop */
1027 if (rtlmac->mac80211_registered == 1) {
1028 ieee80211_unregister_hw(hw);
1029 rtlmac->mac80211_registered = 0;
1031 rtl_deinit_deferred_work(hw);
1032 rtlpriv->intf_ops->adapter_stop(hw);
1035 /* rtl_deinit_rfkill(hw); */
1037 rtl_deinit_core(hw);
1038 kfree(rtlpriv->usb_data);
1039 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1040 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1041 _rtl_usb_io_handler_release(hw);
1042 usb_put_dev(rtlusb->udev);
1043 usb_set_intfdata(intf, NULL);
1044 ieee80211_free_hw(hw);
1046 EXPORT_SYMBOL(rtl_usb_disconnect);
1048 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1052 EXPORT_SYMBOL(rtl_usb_suspend);
1054 int rtl_usb_resume(struct usb_interface *pusb_intf)
1058 EXPORT_SYMBOL(rtl_usb_resume);