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
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>
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
43 #define REALTEK_USB_VENQT_MAX_BUF_SIZE 254
45 static void usbctrl_async_callback(struct urb *urb)
51 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
52 u16 value, u16 index, void *pdata,
58 struct usb_ctrlrequest *dr;
60 struct rtl819x_async_write_data {
61 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
62 struct usb_ctrlrequest dr;
65 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
66 reqtype = REALTEK_USB_VENQT_WRITE;
68 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
72 urb = usb_alloc_urb(0, GFP_ATOMIC);
80 dr->bRequestType = reqtype;
81 dr->bRequest = request;
82 dr->wValue = cpu_to_le16(value);
83 dr->wIndex = cpu_to_le16(index);
84 dr->wLength = cpu_to_le16(len);
85 memcpy(buf, pdata, len);
86 usb_fill_control_urb(urb, udev, pipe,
87 (unsigned char *)dr, buf, len,
88 usbctrl_async_callback, buf);
89 rc = usb_submit_urb(urb, GFP_ATOMIC);
96 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
97 u16 value, u16 index, void *pdata,
104 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
105 reqtype = REALTEK_USB_VENQT_READ;
107 status = usb_control_msg(udev, pipe, request, reqtype, value, index,
108 pdata, len, 0); /* max. timeout */
111 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
112 value, status, *(u32 *)pdata);
116 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
118 struct device *dev = rtlpriv->io.dev;
119 struct usb_device *udev = to_usb_device(dev);
123 __le32 *data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
125 request = REALTEK_USB_VENQT_CMD_REQ;
126 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
129 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
130 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
131 rtlpriv->usb_data_index = 0;
132 return le32_to_cpu(*data);
135 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
137 return (u8)_usb_read_sync(rtlpriv, addr, 1);
140 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
142 return (u16)_usb_read_sync(rtlpriv, addr, 2);
145 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
147 return _usb_read_sync(rtlpriv, addr, 4);
150 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
158 request = REALTEK_USB_VENQT_CMD_REQ;
159 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
160 wvalue = (u16)(addr&0x0000ffff);
162 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
166 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
168 struct device *dev = rtlpriv->io.dev;
170 _usb_write_async(to_usb_device(dev), addr, val, 1);
173 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
175 struct device *dev = rtlpriv->io.dev;
177 _usb_write_async(to_usb_device(dev), addr, val, 2);
180 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
182 struct device *dev = rtlpriv->io.dev;
184 _usb_write_async(to_usb_device(dev), addr, val, 4);
187 static void _rtl_usb_io_handler_init(struct device *dev,
188 struct ieee80211_hw *hw)
190 struct rtl_priv *rtlpriv = rtl_priv(hw);
192 rtlpriv->io.dev = dev;
193 mutex_init(&rtlpriv->io.bb_mutex);
194 rtlpriv->io.write8_async = _usb_write8_async;
195 rtlpriv->io.write16_async = _usb_write16_async;
196 rtlpriv->io.write32_async = _usb_write32_async;
197 rtlpriv->io.read8_sync = _usb_read8_sync;
198 rtlpriv->io.read16_sync = _usb_read16_sync;
199 rtlpriv->io.read32_sync = _usb_read32_sync;
202 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
204 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
206 mutex_destroy(&rtlpriv->io.bb_mutex);
211 * Default aggregation handler. Do nothing and just return the oldest skb.
213 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
214 struct sk_buff_head *list)
216 return skb_dequeue(list);
219 #define IS_HIGH_SPEED_USB(udev) \
220 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
222 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
225 struct rtl_priv *rtlpriv = rtl_priv(hw);
226 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
228 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
229 ? USB_HIGH_SPEED_BULK_SIZE
230 : USB_FULL_SPEED_BULK_SIZE;
232 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
233 rtlusb->max_bulk_out_size));
235 for (i = 0; i < __RTL_TXQ_NUM; i++) {
236 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
238 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
239 ("Invalid endpoint map setting!\n"));
244 rtlusb->usb_tx_post_hdl =
245 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
246 rtlusb->usb_tx_cleanup =
247 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
248 rtlusb->usb_tx_aggregate_hdl =
249 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
250 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
251 : &_none_usb_tx_aggregate_hdl;
253 init_usb_anchor(&rtlusb->tx_submitted);
254 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
255 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
256 init_usb_anchor(&rtlusb->tx_pending[i]);
261 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
263 struct rtl_priv *rtlpriv = rtl_priv(hw);
264 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
265 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
267 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
268 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
269 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
270 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
271 rtlusb->usb_rx_segregate_hdl =
272 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
274 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
275 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
276 init_usb_anchor(&rtlusb->rx_submitted);
280 static int _rtl_usb_init(struct ieee80211_hw *hw)
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);
287 struct usb_interface *usb_intf = rtlusb->intf;
288 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
290 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
291 for (epidx = 0; epidx < epnums; epidx++) {
292 struct usb_endpoint_descriptor *pep_desc;
293 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
295 if (usb_endpoint_dir_in(pep_desc))
296 rtlusb->in_ep_nums++;
297 else if (usb_endpoint_dir_out(pep_desc))
298 rtlusb->out_ep_nums++;
300 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
301 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
302 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
303 pep_desc->bInterval));
305 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
308 /* usb endpoint mapping */
309 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
310 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
311 _rtl_usb_init_tx(hw);
312 _rtl_usb_init_rx(hw);
316 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
318 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
319 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
320 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
321 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
324 ppsc->inactiveps = false;
325 ppsc->leisure_ps = false;
326 ppsc->fwctrl_lps = false;
327 ppsc->reg_fwctrl_lps = 3;
328 ppsc->reg_max_lps_awakeintvl = 5;
329 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
332 mac->beacon_interval = 100;
335 mac->min_space_cfg = 0;
336 mac->max_mss_density = 0;
338 /* set sane AMPDU defaults */
339 mac->current_ampdu_density = 7;
340 mac->current_ampdu_factor = 3;
343 rtlusb->acm_method = eAcmWay2_SW;
346 /* HIMR - turn all on */
347 rtlusb->irq_mask[0] = 0xFFFFFFFF;
348 /* HIMR_EX - turn all on */
349 rtlusb->irq_mask[1] = 0xFFFFFFFF;
350 rtlusb->disableHWSM = true;
354 #define __RADIO_TAP_SIZE_RSV 32
356 static void _rtl_rx_completed(struct urb *urb);
358 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
359 struct rtl_usb *rtlusb,
364 struct rtl_priv *rtlpriv = rtl_priv(hw);
366 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
369 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
370 ("Failed to __dev_alloc_skb!!\n"))
371 return ERR_PTR(-ENOMEM);
374 /* reserve some space for mac80211's radiotap */
375 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
376 usb_fill_bulk_urb(urb, rtlusb->udev,
377 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
378 skb->data, min(skb_tailroom(skb),
379 (int)rtlusb->rx_max_size),
380 _rtl_rx_completed, skb);
382 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
386 #undef __RADIO_TAP_SIZE_RSV
388 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
391 struct rtl_priv *rtlpriv = rtl_priv(hw);
392 u8 *rxdesc = skb->data;
393 struct ieee80211_hdr *hdr;
394 bool unicast = false;
396 struct ieee80211_rx_status rx_status = {0};
397 struct rtl_stats stats = {
403 skb_pull(skb, RTL_RX_DESC_SIZE);
404 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
405 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
406 hdr = (struct ieee80211_hdr *)(skb->data);
407 fc = hdr->frame_control;
409 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
411 if (is_broadcast_ether_addr(hdr->addr1)) {
413 } else if (is_multicast_ether_addr(hdr->addr1)) {
417 rtlpriv->stats.rxbytesunicast += skb->len;
420 rtl_is_special_data(hw, skb, false);
422 if (ieee80211_is_data(fc)) {
423 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
426 rtlpriv->link_info.num_rx_inperiod++;
431 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
434 struct rtl_priv *rtlpriv = rtl_priv(hw);
435 u8 *rxdesc = skb->data;
436 struct ieee80211_hdr *hdr;
437 bool unicast = false;
439 struct ieee80211_rx_status rx_status = {0};
440 struct rtl_stats stats = {
446 skb_pull(skb, RTL_RX_DESC_SIZE);
447 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
448 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
449 hdr = (struct ieee80211_hdr *)(skb->data);
450 fc = hdr->frame_control;
452 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
454 if (is_broadcast_ether_addr(hdr->addr1)) {
456 } else if (is_multicast_ether_addr(hdr->addr1)) {
460 rtlpriv->stats.rxbytesunicast += skb->len;
463 rtl_is_special_data(hw, skb, false);
465 if (ieee80211_is_data(fc)) {
466 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
469 rtlpriv->link_info.num_rx_inperiod++;
471 if (likely(rtl_action_proc(hw, skb, false))) {
472 struct sk_buff *uskb = NULL;
475 uskb = dev_alloc_skb(skb->len + 128);
476 if (uskb) { /* drop packet on allocation failure */
477 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
479 pdata = (u8 *)skb_put(uskb, skb->len);
480 memcpy(pdata, skb->data, skb->len);
481 ieee80211_rx_irqsafe(hw, uskb);
483 dev_kfree_skb_any(skb);
485 dev_kfree_skb_any(skb);
490 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
492 struct sk_buff *_skb;
493 struct sk_buff_head rx_queue;
494 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
496 skb_queue_head_init(&rx_queue);
497 if (rtlusb->usb_rx_segregate_hdl)
498 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
499 WARN_ON(skb_queue_empty(&rx_queue));
500 while (!skb_queue_empty(&rx_queue)) {
501 _skb = skb_dequeue(&rx_queue);
502 _rtl_usb_rx_process_agg(hw, skb);
503 ieee80211_rx_irqsafe(hw, skb);
507 static void _rtl_rx_completed(struct urb *_urb)
509 struct sk_buff *skb = (struct sk_buff *)_urb->context;
510 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
511 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
512 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
513 struct rtl_priv *rtlpriv = rtl_priv(hw);
516 if (unlikely(IS_USB_STOP(rtlusb)))
519 if (likely(0 == _urb->status)) {
520 /* If this code were moved to work queue, would CPU
521 * utilization be improved? NOTE: We shall allocate another skb
522 * and reuse the original one.
524 skb_put(skb, _urb->actual_length);
526 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
527 struct sk_buff *_skb;
528 _rtl_usb_rx_process_noagg(hw, skb);
529 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
532 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
533 ("Can't allocate skb for bulk IN!\n"));
539 _rtl_rx_pre_process(hw, skb);
540 pr_err("rx agg not supported\n");
545 switch (_urb->status) {
557 skb_reset_tail_pointer(skb);
560 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
561 err = usb_submit_urb(_urb, GFP_ATOMIC);
563 usb_unanchor_urb(_urb);
569 dev_kfree_skb_irq(skb);
572 static int _rtl_usb_receive(struct ieee80211_hw *hw)
578 struct rtl_priv *rtlpriv = rtl_priv(hw);
579 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
581 WARN_ON(0 == rtlusb->rx_urb_num);
582 /* 1600 == 1514 + max WLAN header + rtk info */
583 WARN_ON(rtlusb->rx_max_size < 1600);
585 for (i = 0; i < rtlusb->rx_urb_num; i++) {
587 urb = usb_alloc_urb(0, GFP_KERNEL);
589 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
590 ("Failed to alloc URB!!\n"))
594 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
596 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
597 ("Failed to prep_rx_urb!!\n"))
602 usb_anchor_urb(urb, &rtlusb->rx_submitted);
603 err = usb_submit_urb(urb, GFP_KERNEL);
611 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
615 static int rtl_usb_start(struct ieee80211_hw *hw)
618 struct rtl_priv *rtlpriv = rtl_priv(hw);
619 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
620 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
622 err = rtlpriv->cfg->ops->hw_init(hw);
623 rtl_init_rx_config(hw);
625 /* Enable software */
626 SET_USB_START(rtlusb);
627 /* should after adapter start and interrupt enable. */
628 set_hal_start(rtlhal);
631 _rtl_usb_receive(hw);
640 /*======================= tx =========================================*/
641 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
644 struct sk_buff *_skb;
645 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
646 struct ieee80211_tx_info *txinfo;
648 SET_USB_STOP(rtlusb);
650 /* clean up rx stuff. */
651 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
653 /* clean up tx stuff */
654 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
655 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
656 rtlusb->usb_tx_cleanup(hw, _skb);
657 txinfo = IEEE80211_SKB_CB(_skb);
658 ieee80211_tx_info_clear_status(txinfo);
659 txinfo->flags |= IEEE80211_TX_STAT_ACK;
660 ieee80211_tx_status_irqsafe(hw, _skb);
662 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
664 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
669 * We may add some struct into struct rtl_usb later. Do deinit here.
672 static void rtl_usb_deinit(struct ieee80211_hw *hw)
677 static void rtl_usb_stop(struct ieee80211_hw *hw)
679 struct rtl_priv *rtlpriv = rtl_priv(hw);
680 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
681 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
683 /* should after adapter start and interrupt enable. */
684 set_hal_stop(rtlhal);
685 /* Enable software */
686 SET_USB_STOP(rtlusb);
688 rtlpriv->cfg->ops->hw_disable(hw);
691 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
694 struct rtl_priv *rtlpriv = rtl_priv(hw);
695 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
697 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
698 err = usb_submit_urb(_urb, GFP_ATOMIC);
702 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
703 ("Failed to submit urb.\n"));
704 usb_unanchor_urb(_urb);
705 skb = (struct sk_buff *)_urb->context;
711 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
714 struct rtl_priv *rtlpriv = rtl_priv(hw);
715 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
716 struct ieee80211_tx_info *txinfo;
718 rtlusb->usb_tx_post_hdl(hw, urb, skb);
719 skb_pull(skb, RTL_TX_HEADER_SIZE);
720 txinfo = IEEE80211_SKB_CB(skb);
721 ieee80211_tx_info_clear_status(txinfo);
722 txinfo->flags |= IEEE80211_TX_STAT_ACK;
725 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
726 ("Urb has error status 0x%X\n", urb->status));
729 /* TODO: statistics */
731 ieee80211_tx_status_irqsafe(hw, skb);
735 static void _rtl_tx_complete(struct urb *urb)
737 struct sk_buff *skb = (struct sk_buff *)urb->context;
738 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
739 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
740 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
743 if (unlikely(IS_USB_STOP(rtlusb)))
745 err = _usb_tx_post(hw, urb, skb);
747 /* Ignore error and keep issuiing other urbs */
752 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
753 struct sk_buff *skb, u32 ep_num)
755 struct rtl_priv *rtlpriv = rtl_priv(hw);
756 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
759 WARN_ON(NULL == skb);
760 _urb = usb_alloc_urb(0, GFP_ATOMIC);
762 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
763 ("Can't allocate URB for bulk out!\n"));
767 _rtl_install_trx_info(rtlusb, skb, ep_num);
768 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
769 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
770 _urb->transfer_flags |= URB_ZERO_PACKET;
774 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
777 struct rtl_priv *rtlpriv = rtl_priv(hw);
778 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
780 struct urb *_urb = NULL;
781 struct sk_buff *_skb = NULL;
782 struct sk_buff_head *skb_list;
783 struct usb_anchor *urb_list;
785 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
786 if (unlikely(IS_USB_STOP(rtlusb))) {
787 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
788 ("USB device is stopping...\n"));
792 ep_num = rtlusb->ep_map.ep_mapping[qnum];
793 skb_list = &rtlusb->tx_skb_queue[ep_num];
795 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
796 if (unlikely(!_urb)) {
797 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
798 ("Can't allocate urb. Drop skb!\n"));
801 urb_list = &rtlusb->tx_pending[ep_num];
802 _rtl_submit_tx_urb(hw, _urb);
805 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
808 struct rtl_priv *rtlpriv = rtl_priv(hw);
809 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
810 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
811 struct rtl_tx_desc *pdesc = NULL;
812 struct rtl_tcb_desc tcb_desc;
813 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
814 __le16 fc = hdr->frame_control;
815 u8 *pda_addr = hdr->addr1;
821 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
822 if (ieee80211_is_auth(fc)) {
823 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
827 if (rtlpriv->psc.sw_ps_enabled) {
828 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
829 !ieee80211_has_pm(fc))
830 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
833 rtl_action_proc(hw, skb, true);
834 if (is_multicast_ether_addr(pda_addr))
835 rtlpriv->stats.txbytesmulticast += skb->len;
836 else if (is_broadcast_ether_addr(pda_addr))
837 rtlpriv->stats.txbytesbroadcast += skb->len;
839 rtlpriv->stats.txbytesunicast += skb->len;
840 if (ieee80211_is_data_qos(fc)) {
841 qc = ieee80211_get_qos_ctl(hdr);
842 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
843 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
844 IEEE80211_SCTL_SEQ) >> 4;
848 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
849 hw_queue, &tcb_desc);
850 if (!ieee80211_has_morefrags(hdr->frame_control)) {
852 mac->tids[tid].seq_number = seq_number;
854 if (ieee80211_is_data(fc))
855 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
858 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
859 struct rtl_tcb_desc *dummy)
861 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
862 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
863 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
864 __le16 fc = hdr->frame_control;
867 if (unlikely(is_hal_stop(rtlhal)))
869 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
870 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
871 _rtl_usb_transmit(hw, skb, hw_queue);
875 dev_kfree_skb_any(skb);
879 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
885 static struct rtl_intf_ops rtl_usb_ops = {
886 .adapter_start = rtl_usb_start,
887 .adapter_stop = rtl_usb_stop,
888 .adapter_tx = rtl_usb_tx,
889 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
892 int __devinit rtl_usb_probe(struct usb_interface *intf,
893 const struct usb_device_id *id)
896 struct ieee80211_hw *hw = NULL;
897 struct rtl_priv *rtlpriv = NULL;
898 struct usb_device *udev;
899 struct rtl_usb_priv *usb_priv;
901 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
902 sizeof(struct rtl_usb_priv), &rtl_ops);
904 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
908 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
910 if (!rtlpriv->usb_data)
912 rtlpriv->usb_data_index = 0;
913 SET_IEEE80211_DEV(hw, &intf->dev);
914 udev = interface_to_usbdev(intf);
916 usb_priv = rtl_usbpriv(hw);
917 memset(usb_priv, 0, sizeof(*usb_priv));
918 usb_priv->dev.intf = intf;
919 usb_priv->dev.udev = udev;
920 usb_set_intfdata(intf, hw);
921 /* init cfg & intf_ops */
922 rtlpriv->rtlhal.interface = INTF_USB;
923 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
924 rtlpriv->intf_ops = &rtl_usb_ops;
925 rtl_dbgp_flag_init(hw);
926 /* Init IO handler */
927 _rtl_usb_io_handler_init(&udev->dev, hw);
928 rtlpriv->cfg->ops->read_chip_version(hw);
929 /*like read eeprom and so on */
930 rtlpriv->cfg->ops->read_eeprom_info(hw);
931 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
932 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
933 ("Can't init_sw_vars.\n"));
936 rtlpriv->cfg->ops->init_sw_leds(hw);
937 err = _rtl_usb_init(hw);
938 err = _rtl_usb_init_sw(hw);
939 /* Init mac80211 sw */
940 err = rtl_init_core(hw);
942 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
943 ("Can't allocate sw for mac80211.\n"));
948 /* rtl_init_rfkill(hw); */
950 err = ieee80211_register_hw(hw);
952 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
953 ("Can't register mac80211 hw.\n"));
956 rtlpriv->mac80211.mac80211_registered = 1;
958 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
962 _rtl_usb_io_handler_release(hw);
963 ieee80211_free_hw(hw);
967 EXPORT_SYMBOL(rtl_usb_probe);
969 void rtl_usb_disconnect(struct usb_interface *intf)
971 struct ieee80211_hw *hw = usb_get_intfdata(intf);
972 struct rtl_priv *rtlpriv = rtl_priv(hw);
973 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
974 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
976 if (unlikely(!rtlpriv))
978 /*ieee80211_unregister_hw will call ops_stop */
979 if (rtlmac->mac80211_registered == 1) {
980 ieee80211_unregister_hw(hw);
981 rtlmac->mac80211_registered = 0;
983 rtl_deinit_deferred_work(hw);
984 rtlpriv->intf_ops->adapter_stop(hw);
987 /* rtl_deinit_rfkill(hw); */
990 kfree(rtlpriv->usb_data);
991 rtlpriv->cfg->ops->deinit_sw_leds(hw);
992 rtlpriv->cfg->ops->deinit_sw_vars(hw);
993 _rtl_usb_io_handler_release(hw);
994 usb_put_dev(rtlusb->udev);
995 usb_set_intfdata(intf, NULL);
996 ieee80211_free_hw(hw);
998 EXPORT_SYMBOL(rtl_usb_disconnect);
1000 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1004 EXPORT_SYMBOL(rtl_usb_suspend);
1006 int rtl_usb_resume(struct usb_interface *pusb_intf)
1010 EXPORT_SYMBOL(rtl_usb_resume);