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>
36 #include "rtl8192c/fw_common.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
44 #define MAX_USBCTRL_VENDORREQ_TIMES 10
46 static void usbctrl_async_callback(struct urb *urb)
52 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
53 u16 value, u16 index, void *pdata,
59 struct usb_ctrlrequest *dr;
61 struct rtl819x_async_write_data {
62 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
63 struct usb_ctrlrequest dr;
66 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
67 reqtype = REALTEK_USB_VENQT_WRITE;
69 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
73 urb = usb_alloc_urb(0, GFP_ATOMIC);
81 dr->bRequestType = reqtype;
82 dr->bRequest = request;
83 dr->wValue = cpu_to_le16(value);
84 dr->wIndex = cpu_to_le16(index);
85 dr->wLength = cpu_to_le16(len);
86 memcpy(buf, pdata, len);
87 usb_fill_control_urb(urb, udev, pipe,
88 (unsigned char *)dr, buf, len,
89 usbctrl_async_callback, buf);
90 rc = usb_submit_urb(urb, GFP_ATOMIC);
97 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
98 u16 value, u16 index, void *pdata,
104 int vendorreq_times = 0;
106 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
107 reqtype = REALTEK_USB_VENQT_READ;
109 while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) {
110 status = usb_control_msg(udev, pipe, request, reqtype, value,
111 index, pdata, len, 0); /*max. timeout*/
113 /* firmware download is checksumed, don't retry */
114 if ((value >= FW_8192C_START_ADDRESS &&
115 value <= FW_8192C_END_ADDRESS))
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 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);
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);
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 _rtl_usb_io_handler_init(struct device *dev,
207 struct ieee80211_hw *hw)
209 struct rtl_priv *rtlpriv = rtl_priv(hw);
211 rtlpriv->io.dev = dev;
212 mutex_init(&rtlpriv->io.bb_mutex);
213 rtlpriv->io.write8_async = _usb_write8_async;
214 rtlpriv->io.write16_async = _usb_write16_async;
215 rtlpriv->io.write32_async = _usb_write32_async;
216 rtlpriv->io.read8_sync = _usb_read8_sync;
217 rtlpriv->io.read16_sync = _usb_read16_sync;
218 rtlpriv->io.read32_sync = _usb_read32_sync;
221 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
223 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
225 mutex_destroy(&rtlpriv->io.bb_mutex);
230 * Default aggregation handler. Do nothing and just return the oldest skb.
232 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
233 struct sk_buff_head *list)
235 return skb_dequeue(list);
238 #define IS_HIGH_SPEED_USB(udev) \
239 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
241 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
244 struct rtl_priv *rtlpriv = rtl_priv(hw);
245 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
247 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
248 ? USB_HIGH_SPEED_BULK_SIZE
249 : USB_FULL_SPEED_BULK_SIZE;
251 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
252 rtlusb->max_bulk_out_size));
254 for (i = 0; i < __RTL_TXQ_NUM; i++) {
255 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
257 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
258 ("Invalid endpoint map setting!\n"));
263 rtlusb->usb_tx_post_hdl =
264 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
265 rtlusb->usb_tx_cleanup =
266 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
267 rtlusb->usb_tx_aggregate_hdl =
268 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
269 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
270 : &_none_usb_tx_aggregate_hdl;
272 init_usb_anchor(&rtlusb->tx_submitted);
273 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
274 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
275 init_usb_anchor(&rtlusb->tx_pending[i]);
280 static int _rtl_usb_init_rx(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);
286 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
287 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
288 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
289 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
290 rtlusb->usb_rx_segregate_hdl =
291 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
293 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
294 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
295 init_usb_anchor(&rtlusb->rx_submitted);
299 static int _rtl_usb_init(struct ieee80211_hw *hw)
301 struct rtl_priv *rtlpriv = rtl_priv(hw);
302 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
303 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
306 struct usb_interface *usb_intf = rtlusb->intf;
307 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
309 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
310 for (epidx = 0; epidx < epnums; epidx++) {
311 struct usb_endpoint_descriptor *pep_desc;
312 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
314 if (usb_endpoint_dir_in(pep_desc))
315 rtlusb->in_ep_nums++;
316 else if (usb_endpoint_dir_out(pep_desc))
317 rtlusb->out_ep_nums++;
319 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
320 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
321 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
322 pep_desc->bInterval));
324 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
327 /* usb endpoint mapping */
328 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
329 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
330 _rtl_usb_init_tx(hw);
331 _rtl_usb_init_rx(hw);
335 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
337 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
338 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
339 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
340 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
343 ppsc->inactiveps = false;
344 ppsc->leisure_ps = false;
345 ppsc->fwctrl_lps = false;
346 ppsc->reg_fwctrl_lps = 3;
347 ppsc->reg_max_lps_awakeintvl = 5;
348 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
351 mac->beacon_interval = 100;
354 mac->min_space_cfg = 0;
355 mac->max_mss_density = 0;
357 /* set sane AMPDU defaults */
358 mac->current_ampdu_density = 7;
359 mac->current_ampdu_factor = 3;
362 rtlusb->acm_method = eAcmWay2_SW;
365 /* HIMR - turn all on */
366 rtlusb->irq_mask[0] = 0xFFFFFFFF;
367 /* HIMR_EX - turn all on */
368 rtlusb->irq_mask[1] = 0xFFFFFFFF;
369 rtlusb->disableHWSM = true;
373 #define __RADIO_TAP_SIZE_RSV 32
375 static void _rtl_rx_completed(struct urb *urb);
377 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
378 struct rtl_usb *rtlusb,
383 struct rtl_priv *rtlpriv = rtl_priv(hw);
385 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
388 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
389 ("Failed to __dev_alloc_skb!!\n"))
390 return ERR_PTR(-ENOMEM);
393 /* reserve some space for mac80211's radiotap */
394 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
395 usb_fill_bulk_urb(urb, rtlusb->udev,
396 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
397 skb->data, min(skb_tailroom(skb),
398 (int)rtlusb->rx_max_size),
399 _rtl_rx_completed, skb);
401 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
405 #undef __RADIO_TAP_SIZE_RSV
407 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
410 struct rtl_priv *rtlpriv = rtl_priv(hw);
411 u8 *rxdesc = skb->data;
412 struct ieee80211_hdr *hdr;
413 bool unicast = false;
415 struct ieee80211_rx_status rx_status = {0};
416 struct rtl_stats stats = {
422 skb_pull(skb, RTL_RX_DESC_SIZE);
423 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
424 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
425 hdr = (struct ieee80211_hdr *)(skb->data);
426 fc = hdr->frame_control;
428 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
430 if (is_broadcast_ether_addr(hdr->addr1)) {
432 } else if (is_multicast_ether_addr(hdr->addr1)) {
436 rtlpriv->stats.rxbytesunicast += skb->len;
439 rtl_is_special_data(hw, skb, false);
441 if (ieee80211_is_data(fc)) {
442 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
445 rtlpriv->link_info.num_rx_inperiod++;
450 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
453 struct rtl_priv *rtlpriv = rtl_priv(hw);
454 u8 *rxdesc = skb->data;
455 struct ieee80211_hdr *hdr;
456 bool unicast = false;
458 struct ieee80211_rx_status rx_status = {0};
459 struct rtl_stats stats = {
465 skb_pull(skb, RTL_RX_DESC_SIZE);
466 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
467 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
468 hdr = (struct ieee80211_hdr *)(skb->data);
469 fc = hdr->frame_control;
471 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
473 if (is_broadcast_ether_addr(hdr->addr1)) {
475 } else if (is_multicast_ether_addr(hdr->addr1)) {
479 rtlpriv->stats.rxbytesunicast += skb->len;
482 rtl_is_special_data(hw, skb, false);
484 if (ieee80211_is_data(fc)) {
485 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
488 rtlpriv->link_info.num_rx_inperiod++;
490 if (likely(rtl_action_proc(hw, skb, false))) {
491 struct sk_buff *uskb = NULL;
494 uskb = dev_alloc_skb(skb->len + 128);
495 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
497 pdata = (u8 *)skb_put(uskb, skb->len);
498 memcpy(pdata, skb->data, skb->len);
499 dev_kfree_skb_any(skb);
500 ieee80211_rx_irqsafe(hw, uskb);
502 dev_kfree_skb_any(skb);
507 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
509 struct sk_buff *_skb;
510 struct sk_buff_head rx_queue;
511 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
513 skb_queue_head_init(&rx_queue);
514 if (rtlusb->usb_rx_segregate_hdl)
515 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
516 WARN_ON(skb_queue_empty(&rx_queue));
517 while (!skb_queue_empty(&rx_queue)) {
518 _skb = skb_dequeue(&rx_queue);
519 _rtl_usb_rx_process_agg(hw, skb);
520 ieee80211_rx_irqsafe(hw, skb);
524 static void _rtl_rx_completed(struct urb *_urb)
526 struct sk_buff *skb = (struct sk_buff *)_urb->context;
527 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
528 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
529 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
530 struct rtl_priv *rtlpriv = rtl_priv(hw);
533 if (unlikely(IS_USB_STOP(rtlusb)))
536 if (likely(0 == _urb->status)) {
537 /* If this code were moved to work queue, would CPU
538 * utilization be improved? NOTE: We shall allocate another skb
539 * and reuse the original one.
541 skb_put(skb, _urb->actual_length);
543 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
544 struct sk_buff *_skb;
545 _rtl_usb_rx_process_noagg(hw, skb);
546 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
549 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
550 ("Can't allocate skb for bulk IN!\n"));
556 _rtl_rx_pre_process(hw, skb);
557 pr_err("rx agg not supported\n");
562 switch (_urb->status) {
574 skb_reset_tail_pointer(skb);
577 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
578 err = usb_submit_urb(_urb, GFP_ATOMIC);
580 usb_unanchor_urb(_urb);
586 dev_kfree_skb_irq(skb);
589 static int _rtl_usb_receive(struct ieee80211_hw *hw)
595 struct rtl_priv *rtlpriv = rtl_priv(hw);
596 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
598 WARN_ON(0 == rtlusb->rx_urb_num);
599 /* 1600 == 1514 + max WLAN header + rtk info */
600 WARN_ON(rtlusb->rx_max_size < 1600);
602 for (i = 0; i < rtlusb->rx_urb_num; i++) {
604 urb = usb_alloc_urb(0, GFP_KERNEL);
606 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
607 ("Failed to alloc URB!!\n"))
611 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
613 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
614 ("Failed to prep_rx_urb!!\n"))
619 usb_anchor_urb(urb, &rtlusb->rx_submitted);
620 err = usb_submit_urb(urb, GFP_KERNEL);
628 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
632 static int rtl_usb_start(struct ieee80211_hw *hw)
635 struct rtl_priv *rtlpriv = rtl_priv(hw);
636 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
637 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
639 err = rtlpriv->cfg->ops->hw_init(hw);
640 rtl_init_rx_config(hw);
642 /* Enable software */
643 SET_USB_START(rtlusb);
644 /* should after adapter start and interrupt enable. */
645 set_hal_start(rtlhal);
648 _rtl_usb_receive(hw);
657 /*======================= tx =========================================*/
658 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
661 struct sk_buff *_skb;
662 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
663 struct ieee80211_tx_info *txinfo;
665 SET_USB_STOP(rtlusb);
667 /* clean up rx stuff. */
668 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
670 /* clean up tx stuff */
671 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
672 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
673 rtlusb->usb_tx_cleanup(hw, _skb);
674 txinfo = IEEE80211_SKB_CB(_skb);
675 ieee80211_tx_info_clear_status(txinfo);
676 txinfo->flags |= IEEE80211_TX_STAT_ACK;
677 ieee80211_tx_status_irqsafe(hw, _skb);
679 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
681 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
686 * We may add some struct into struct rtl_usb later. Do deinit here.
689 static void rtl_usb_deinit(struct ieee80211_hw *hw)
694 static void rtl_usb_stop(struct ieee80211_hw *hw)
696 struct rtl_priv *rtlpriv = rtl_priv(hw);
697 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
698 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
700 /* should after adapter start and interrupt enable. */
701 set_hal_stop(rtlhal);
702 /* Enable software */
703 SET_USB_STOP(rtlusb);
705 rtlpriv->cfg->ops->hw_disable(hw);
708 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
711 struct rtl_priv *rtlpriv = rtl_priv(hw);
712 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
714 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
715 err = usb_submit_urb(_urb, GFP_ATOMIC);
719 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
720 ("Failed to submit urb.\n"));
721 usb_unanchor_urb(_urb);
722 skb = (struct sk_buff *)_urb->context;
728 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
731 struct rtl_priv *rtlpriv = rtl_priv(hw);
732 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
733 struct ieee80211_tx_info *txinfo;
735 rtlusb->usb_tx_post_hdl(hw, urb, skb);
736 skb_pull(skb, RTL_TX_HEADER_SIZE);
737 txinfo = IEEE80211_SKB_CB(skb);
738 ieee80211_tx_info_clear_status(txinfo);
739 txinfo->flags |= IEEE80211_TX_STAT_ACK;
742 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
743 ("Urb has error status 0x%X\n", urb->status));
746 /* TODO: statistics */
748 ieee80211_tx_status_irqsafe(hw, skb);
752 static void _rtl_tx_complete(struct urb *urb)
754 struct sk_buff *skb = (struct sk_buff *)urb->context;
755 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
756 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
757 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
760 if (unlikely(IS_USB_STOP(rtlusb)))
762 err = _usb_tx_post(hw, urb, skb);
764 /* Ignore error and keep issuiing other urbs */
769 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
770 struct sk_buff *skb, u32 ep_num)
772 struct rtl_priv *rtlpriv = rtl_priv(hw);
773 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
776 WARN_ON(NULL == skb);
777 _urb = usb_alloc_urb(0, GFP_ATOMIC);
779 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
780 ("Can't allocate URB for bulk out!\n"));
784 _rtl_install_trx_info(rtlusb, skb, ep_num);
785 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
786 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
787 _urb->transfer_flags |= URB_ZERO_PACKET;
791 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
794 struct rtl_priv *rtlpriv = rtl_priv(hw);
795 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
797 struct urb *_urb = NULL;
798 struct sk_buff *_skb = NULL;
799 struct sk_buff_head *skb_list;
800 struct usb_anchor *urb_list;
802 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
803 if (unlikely(IS_USB_STOP(rtlusb))) {
804 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
805 ("USB device is stopping...\n"));
809 ep_num = rtlusb->ep_map.ep_mapping[qnum];
810 skb_list = &rtlusb->tx_skb_queue[ep_num];
812 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
813 if (unlikely(!_urb)) {
814 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
815 ("Can't allocate urb. Drop skb!\n"));
818 urb_list = &rtlusb->tx_pending[ep_num];
819 _rtl_submit_tx_urb(hw, _urb);
822 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
825 struct rtl_priv *rtlpriv = rtl_priv(hw);
826 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
827 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
828 struct rtl_tx_desc *pdesc = NULL;
829 struct rtl_tcb_desc tcb_desc;
830 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
831 __le16 fc = hdr->frame_control;
832 u8 *pda_addr = hdr->addr1;
838 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
839 if (ieee80211_is_auth(fc)) {
840 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
844 if (rtlpriv->psc.sw_ps_enabled) {
845 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
846 !ieee80211_has_pm(fc))
847 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
850 rtl_action_proc(hw, skb, true);
851 if (is_multicast_ether_addr(pda_addr))
852 rtlpriv->stats.txbytesmulticast += skb->len;
853 else if (is_broadcast_ether_addr(pda_addr))
854 rtlpriv->stats.txbytesbroadcast += skb->len;
856 rtlpriv->stats.txbytesunicast += skb->len;
857 if (ieee80211_is_data_qos(fc)) {
858 qc = ieee80211_get_qos_ctl(hdr);
859 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
860 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
861 IEEE80211_SCTL_SEQ) >> 4;
865 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
866 hw_queue, &tcb_desc);
867 if (!ieee80211_has_morefrags(hdr->frame_control)) {
869 mac->tids[tid].seq_number = seq_number;
871 if (ieee80211_is_data(fc))
872 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
875 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
876 struct rtl_tcb_desc *dummy)
878 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
879 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
880 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
881 __le16 fc = hdr->frame_control;
884 if (unlikely(is_hal_stop(rtlhal)))
886 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
887 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
888 _rtl_usb_transmit(hw, skb, hw_queue);
892 dev_kfree_skb_any(skb);
896 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
902 static struct rtl_intf_ops rtl_usb_ops = {
903 .adapter_start = rtl_usb_start,
904 .adapter_stop = rtl_usb_stop,
905 .adapter_tx = rtl_usb_tx,
906 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
909 int __devinit rtl_usb_probe(struct usb_interface *intf,
910 const struct usb_device_id *id)
913 struct ieee80211_hw *hw = NULL;
914 struct rtl_priv *rtlpriv = NULL;
915 struct usb_device *udev;
916 struct rtl_usb_priv *usb_priv;
918 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
919 sizeof(struct rtl_usb_priv), &rtl_ops);
921 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
925 SET_IEEE80211_DEV(hw, &intf->dev);
926 udev = interface_to_usbdev(intf);
928 usb_priv = rtl_usbpriv(hw);
929 memset(usb_priv, 0, sizeof(*usb_priv));
930 usb_priv->dev.intf = intf;
931 usb_priv->dev.udev = udev;
932 usb_set_intfdata(intf, hw);
933 /* init cfg & intf_ops */
934 rtlpriv->rtlhal.interface = INTF_USB;
935 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
936 rtlpriv->intf_ops = &rtl_usb_ops;
937 rtl_dbgp_flag_init(hw);
938 /* Init IO handler */
939 _rtl_usb_io_handler_init(&udev->dev, hw);
940 rtlpriv->cfg->ops->read_chip_version(hw);
941 /*like read eeprom and so on */
942 rtlpriv->cfg->ops->read_eeprom_info(hw);
943 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
944 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
945 ("Can't init_sw_vars.\n"));
948 rtlpriv->cfg->ops->init_sw_leds(hw);
949 err = _rtl_usb_init(hw);
950 err = _rtl_usb_init_sw(hw);
951 /* Init mac80211 sw */
952 err = rtl_init_core(hw);
954 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
955 ("Can't allocate sw for mac80211.\n"));
960 /* rtl_init_rfkill(hw); */
962 err = ieee80211_register_hw(hw);
964 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
965 ("Can't register mac80211 hw.\n"));
968 rtlpriv->mac80211.mac80211_registered = 1;
970 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
974 _rtl_usb_io_handler_release(hw);
975 ieee80211_free_hw(hw);
979 EXPORT_SYMBOL(rtl_usb_probe);
981 void rtl_usb_disconnect(struct usb_interface *intf)
983 struct ieee80211_hw *hw = usb_get_intfdata(intf);
984 struct rtl_priv *rtlpriv = rtl_priv(hw);
985 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
986 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
988 if (unlikely(!rtlpriv))
990 /*ieee80211_unregister_hw will call ops_stop */
991 if (rtlmac->mac80211_registered == 1) {
992 ieee80211_unregister_hw(hw);
993 rtlmac->mac80211_registered = 0;
995 rtl_deinit_deferred_work(hw);
996 rtlpriv->intf_ops->adapter_stop(hw);
999 /* rtl_deinit_rfkill(hw); */
1001 rtl_deinit_core(hw);
1002 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1003 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1004 _rtl_usb_io_handler_release(hw);
1005 usb_put_dev(rtlusb->udev);
1006 usb_set_intfdata(intf, NULL);
1007 ieee80211_free_hw(hw);
1009 EXPORT_SYMBOL(rtl_usb_disconnect);
1011 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1015 EXPORT_SYMBOL(rtl_usb_suspend);
1017 int rtl_usb_resume(struct usb_interface *pusb_intf)
1021 EXPORT_SYMBOL(rtl_usb_resume);