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);
126 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
127 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
128 rtlpriv->usb_data_index = 0;
129 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
130 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
131 request = REALTEK_USB_VENQT_CMD_REQ;
132 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
135 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
136 return le32_to_cpu(*data);
139 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
141 return (u8)_usb_read_sync(rtlpriv, addr, 1);
144 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
146 return (u16)_usb_read_sync(rtlpriv, addr, 2);
149 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
151 return _usb_read_sync(rtlpriv, addr, 4);
154 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
162 request = REALTEK_USB_VENQT_CMD_REQ;
163 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
164 wvalue = (u16)(addr&0x0000ffff);
166 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
170 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
172 struct device *dev = rtlpriv->io.dev;
174 _usb_write_async(to_usb_device(dev), addr, val, 1);
177 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
179 struct device *dev = rtlpriv->io.dev;
181 _usb_write_async(to_usb_device(dev), addr, val, 2);
184 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
186 struct device *dev = rtlpriv->io.dev;
188 _usb_write_async(to_usb_device(dev), addr, val, 4);
191 static void _rtl_usb_io_handler_init(struct device *dev,
192 struct ieee80211_hw *hw)
194 struct rtl_priv *rtlpriv = rtl_priv(hw);
196 rtlpriv->io.dev = dev;
197 mutex_init(&rtlpriv->io.bb_mutex);
198 rtlpriv->io.write8_async = _usb_write8_async;
199 rtlpriv->io.write16_async = _usb_write16_async;
200 rtlpriv->io.write32_async = _usb_write32_async;
201 rtlpriv->io.read8_sync = _usb_read8_sync;
202 rtlpriv->io.read16_sync = _usb_read16_sync;
203 rtlpriv->io.read32_sync = _usb_read32_sync;
206 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
208 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
210 mutex_destroy(&rtlpriv->io.bb_mutex);
215 * Default aggregation handler. Do nothing and just return the oldest skb.
217 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
218 struct sk_buff_head *list)
220 return skb_dequeue(list);
223 #define IS_HIGH_SPEED_USB(udev) \
224 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
226 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
229 struct rtl_priv *rtlpriv = rtl_priv(hw);
230 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
232 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
233 ? USB_HIGH_SPEED_BULK_SIZE
234 : USB_FULL_SPEED_BULK_SIZE;
236 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
237 rtlusb->max_bulk_out_size));
239 for (i = 0; i < __RTL_TXQ_NUM; i++) {
240 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
242 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
243 ("Invalid endpoint map setting!\n"));
248 rtlusb->usb_tx_post_hdl =
249 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
250 rtlusb->usb_tx_cleanup =
251 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
252 rtlusb->usb_tx_aggregate_hdl =
253 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
254 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
255 : &_none_usb_tx_aggregate_hdl;
257 init_usb_anchor(&rtlusb->tx_submitted);
258 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
259 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
260 init_usb_anchor(&rtlusb->tx_pending[i]);
265 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
267 struct rtl_priv *rtlpriv = rtl_priv(hw);
268 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
269 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
271 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
272 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
273 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
274 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
275 rtlusb->usb_rx_segregate_hdl =
276 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
278 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
279 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
280 init_usb_anchor(&rtlusb->rx_submitted);
284 static int _rtl_usb_init(struct ieee80211_hw *hw)
286 struct rtl_priv *rtlpriv = rtl_priv(hw);
287 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
288 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
291 struct usb_interface *usb_intf = rtlusb->intf;
292 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
294 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
295 for (epidx = 0; epidx < epnums; epidx++) {
296 struct usb_endpoint_descriptor *pep_desc;
297 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
299 if (usb_endpoint_dir_in(pep_desc))
300 rtlusb->in_ep_nums++;
301 else if (usb_endpoint_dir_out(pep_desc))
302 rtlusb->out_ep_nums++;
304 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
305 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
306 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
307 pep_desc->bInterval));
309 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
312 /* usb endpoint mapping */
313 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
314 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
315 _rtl_usb_init_tx(hw);
316 _rtl_usb_init_rx(hw);
320 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
322 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
323 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
324 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
325 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
328 ppsc->inactiveps = false;
329 ppsc->leisure_ps = false;
330 ppsc->fwctrl_lps = false;
331 ppsc->reg_fwctrl_lps = 3;
332 ppsc->reg_max_lps_awakeintvl = 5;
333 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
336 mac->beacon_interval = 100;
339 mac->min_space_cfg = 0;
340 mac->max_mss_density = 0;
342 /* set sane AMPDU defaults */
343 mac->current_ampdu_density = 7;
344 mac->current_ampdu_factor = 3;
347 rtlusb->acm_method = eAcmWay2_SW;
350 /* HIMR - turn all on */
351 rtlusb->irq_mask[0] = 0xFFFFFFFF;
352 /* HIMR_EX - turn all on */
353 rtlusb->irq_mask[1] = 0xFFFFFFFF;
354 rtlusb->disableHWSM = true;
358 #define __RADIO_TAP_SIZE_RSV 32
360 static void _rtl_rx_completed(struct urb *urb);
362 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
363 struct rtl_usb *rtlusb,
368 struct rtl_priv *rtlpriv = rtl_priv(hw);
370 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
373 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
374 ("Failed to __dev_alloc_skb!!\n"))
375 return ERR_PTR(-ENOMEM);
378 /* reserve some space for mac80211's radiotap */
379 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
380 usb_fill_bulk_urb(urb, rtlusb->udev,
381 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
382 skb->data, min(skb_tailroom(skb),
383 (int)rtlusb->rx_max_size),
384 _rtl_rx_completed, skb);
386 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
390 #undef __RADIO_TAP_SIZE_RSV
392 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
395 struct rtl_priv *rtlpriv = rtl_priv(hw);
396 u8 *rxdesc = skb->data;
397 struct ieee80211_hdr *hdr;
398 bool unicast = false;
400 struct ieee80211_rx_status rx_status = {0};
401 struct rtl_stats stats = {
407 skb_pull(skb, RTL_RX_DESC_SIZE);
408 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
409 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
410 hdr = (struct ieee80211_hdr *)(skb->data);
411 fc = hdr->frame_control;
413 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
415 if (is_broadcast_ether_addr(hdr->addr1)) {
417 } else if (is_multicast_ether_addr(hdr->addr1)) {
421 rtlpriv->stats.rxbytesunicast += skb->len;
424 rtl_is_special_data(hw, skb, false);
426 if (ieee80211_is_data(fc)) {
427 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
430 rtlpriv->link_info.num_rx_inperiod++;
435 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
438 struct rtl_priv *rtlpriv = rtl_priv(hw);
439 u8 *rxdesc = skb->data;
440 struct ieee80211_hdr *hdr;
441 bool unicast = false;
443 struct ieee80211_rx_status rx_status = {0};
444 struct rtl_stats stats = {
450 skb_pull(skb, RTL_RX_DESC_SIZE);
451 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
452 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
453 hdr = (struct ieee80211_hdr *)(skb->data);
454 fc = hdr->frame_control;
456 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
458 if (is_broadcast_ether_addr(hdr->addr1)) {
460 } else if (is_multicast_ether_addr(hdr->addr1)) {
464 rtlpriv->stats.rxbytesunicast += skb->len;
467 rtl_is_special_data(hw, skb, false);
469 if (ieee80211_is_data(fc)) {
470 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
473 rtlpriv->link_info.num_rx_inperiod++;
475 if (likely(rtl_action_proc(hw, skb, false))) {
476 struct sk_buff *uskb = NULL;
479 uskb = dev_alloc_skb(skb->len + 128);
480 if (uskb) { /* drop packet on allocation failure */
481 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
483 pdata = (u8 *)skb_put(uskb, skb->len);
484 memcpy(pdata, skb->data, skb->len);
485 ieee80211_rx_irqsafe(hw, uskb);
487 dev_kfree_skb_any(skb);
489 dev_kfree_skb_any(skb);
494 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
496 struct sk_buff *_skb;
497 struct sk_buff_head rx_queue;
498 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
500 skb_queue_head_init(&rx_queue);
501 if (rtlusb->usb_rx_segregate_hdl)
502 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
503 WARN_ON(skb_queue_empty(&rx_queue));
504 while (!skb_queue_empty(&rx_queue)) {
505 _skb = skb_dequeue(&rx_queue);
506 _rtl_usb_rx_process_agg(hw, skb);
507 ieee80211_rx_irqsafe(hw, skb);
511 static void _rtl_rx_completed(struct urb *_urb)
513 struct sk_buff *skb = (struct sk_buff *)_urb->context;
514 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
515 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
516 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
517 struct rtl_priv *rtlpriv = rtl_priv(hw);
520 if (unlikely(IS_USB_STOP(rtlusb)))
523 if (likely(0 == _urb->status)) {
524 /* If this code were moved to work queue, would CPU
525 * utilization be improved? NOTE: We shall allocate another skb
526 * and reuse the original one.
528 skb_put(skb, _urb->actual_length);
530 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
531 struct sk_buff *_skb;
532 _rtl_usb_rx_process_noagg(hw, skb);
533 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
536 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
537 ("Can't allocate skb for bulk IN!\n"));
543 _rtl_rx_pre_process(hw, skb);
544 pr_err("rx agg not supported\n");
549 switch (_urb->status) {
561 skb_reset_tail_pointer(skb);
564 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
565 err = usb_submit_urb(_urb, GFP_ATOMIC);
567 usb_unanchor_urb(_urb);
573 dev_kfree_skb_irq(skb);
576 static int _rtl_usb_receive(struct ieee80211_hw *hw)
582 struct rtl_priv *rtlpriv = rtl_priv(hw);
583 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
585 WARN_ON(0 == rtlusb->rx_urb_num);
586 /* 1600 == 1514 + max WLAN header + rtk info */
587 WARN_ON(rtlusb->rx_max_size < 1600);
589 for (i = 0; i < rtlusb->rx_urb_num; i++) {
591 urb = usb_alloc_urb(0, GFP_KERNEL);
593 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
594 ("Failed to alloc URB!!\n"))
598 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
600 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
601 ("Failed to prep_rx_urb!!\n"))
606 usb_anchor_urb(urb, &rtlusb->rx_submitted);
607 err = usb_submit_urb(urb, GFP_KERNEL);
615 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
619 static int rtl_usb_start(struct ieee80211_hw *hw)
622 struct rtl_priv *rtlpriv = rtl_priv(hw);
623 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
624 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
626 err = rtlpriv->cfg->ops->hw_init(hw);
627 rtl_init_rx_config(hw);
629 /* Enable software */
630 SET_USB_START(rtlusb);
631 /* should after adapter start and interrupt enable. */
632 set_hal_start(rtlhal);
635 _rtl_usb_receive(hw);
644 /*======================= tx =========================================*/
645 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
648 struct sk_buff *_skb;
649 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
650 struct ieee80211_tx_info *txinfo;
652 SET_USB_STOP(rtlusb);
654 /* clean up rx stuff. */
655 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
657 /* clean up tx stuff */
658 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
659 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
660 rtlusb->usb_tx_cleanup(hw, _skb);
661 txinfo = IEEE80211_SKB_CB(_skb);
662 ieee80211_tx_info_clear_status(txinfo);
663 txinfo->flags |= IEEE80211_TX_STAT_ACK;
664 ieee80211_tx_status_irqsafe(hw, _skb);
666 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
668 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
673 * We may add some struct into struct rtl_usb later. Do deinit here.
676 static void rtl_usb_deinit(struct ieee80211_hw *hw)
681 static void rtl_usb_stop(struct ieee80211_hw *hw)
683 struct rtl_priv *rtlpriv = rtl_priv(hw);
684 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
685 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
687 /* should after adapter start and interrupt enable. */
688 set_hal_stop(rtlhal);
689 /* Enable software */
690 SET_USB_STOP(rtlusb);
692 rtlpriv->cfg->ops->hw_disable(hw);
695 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
698 struct rtl_priv *rtlpriv = rtl_priv(hw);
699 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
701 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
702 err = usb_submit_urb(_urb, GFP_ATOMIC);
706 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
707 ("Failed to submit urb.\n"));
708 usb_unanchor_urb(_urb);
709 skb = (struct sk_buff *)_urb->context;
715 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
718 struct rtl_priv *rtlpriv = rtl_priv(hw);
719 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
720 struct ieee80211_tx_info *txinfo;
722 rtlusb->usb_tx_post_hdl(hw, urb, skb);
723 skb_pull(skb, RTL_TX_HEADER_SIZE);
724 txinfo = IEEE80211_SKB_CB(skb);
725 ieee80211_tx_info_clear_status(txinfo);
726 txinfo->flags |= IEEE80211_TX_STAT_ACK;
729 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
730 ("Urb has error status 0x%X\n", urb->status));
733 /* TODO: statistics */
735 ieee80211_tx_status_irqsafe(hw, skb);
739 static void _rtl_tx_complete(struct urb *urb)
741 struct sk_buff *skb = (struct sk_buff *)urb->context;
742 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
743 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
744 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
747 if (unlikely(IS_USB_STOP(rtlusb)))
749 err = _usb_tx_post(hw, urb, skb);
751 /* Ignore error and keep issuiing other urbs */
756 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
757 struct sk_buff *skb, u32 ep_num)
759 struct rtl_priv *rtlpriv = rtl_priv(hw);
760 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
763 WARN_ON(NULL == skb);
764 _urb = usb_alloc_urb(0, GFP_ATOMIC);
766 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
767 ("Can't allocate URB for bulk out!\n"));
771 _rtl_install_trx_info(rtlusb, skb, ep_num);
772 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
773 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
774 _urb->transfer_flags |= URB_ZERO_PACKET;
778 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
781 struct rtl_priv *rtlpriv = rtl_priv(hw);
782 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
784 struct urb *_urb = NULL;
785 struct sk_buff *_skb = NULL;
786 struct sk_buff_head *skb_list;
787 struct usb_anchor *urb_list;
789 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
790 if (unlikely(IS_USB_STOP(rtlusb))) {
791 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
792 ("USB device is stopping...\n"));
796 ep_num = rtlusb->ep_map.ep_mapping[qnum];
797 skb_list = &rtlusb->tx_skb_queue[ep_num];
799 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
800 if (unlikely(!_urb)) {
801 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
802 ("Can't allocate urb. Drop skb!\n"));
805 urb_list = &rtlusb->tx_pending[ep_num];
806 _rtl_submit_tx_urb(hw, _urb);
809 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
812 struct rtl_priv *rtlpriv = rtl_priv(hw);
813 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
814 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
815 struct rtl_tx_desc *pdesc = NULL;
816 struct rtl_tcb_desc tcb_desc;
817 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
818 __le16 fc = hdr->frame_control;
819 u8 *pda_addr = hdr->addr1;
825 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
826 if (ieee80211_is_auth(fc)) {
827 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
831 if (rtlpriv->psc.sw_ps_enabled) {
832 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
833 !ieee80211_has_pm(fc))
834 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
837 rtl_action_proc(hw, skb, true);
838 if (is_multicast_ether_addr(pda_addr))
839 rtlpriv->stats.txbytesmulticast += skb->len;
840 else if (is_broadcast_ether_addr(pda_addr))
841 rtlpriv->stats.txbytesbroadcast += skb->len;
843 rtlpriv->stats.txbytesunicast += skb->len;
844 if (ieee80211_is_data_qos(fc)) {
845 qc = ieee80211_get_qos_ctl(hdr);
846 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
847 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
848 IEEE80211_SCTL_SEQ) >> 4;
852 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
853 hw_queue, &tcb_desc);
854 if (!ieee80211_has_morefrags(hdr->frame_control)) {
856 mac->tids[tid].seq_number = seq_number;
858 if (ieee80211_is_data(fc))
859 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
862 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
863 struct rtl_tcb_desc *dummy)
865 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
866 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
867 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
868 __le16 fc = hdr->frame_control;
871 if (unlikely(is_hal_stop(rtlhal)))
873 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
874 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
875 _rtl_usb_transmit(hw, skb, hw_queue);
879 dev_kfree_skb_any(skb);
883 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
889 static struct rtl_intf_ops rtl_usb_ops = {
890 .adapter_start = rtl_usb_start,
891 .adapter_stop = rtl_usb_stop,
892 .adapter_tx = rtl_usb_tx,
893 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
896 int __devinit rtl_usb_probe(struct usb_interface *intf,
897 const struct usb_device_id *id)
900 struct ieee80211_hw *hw = NULL;
901 struct rtl_priv *rtlpriv = NULL;
902 struct usb_device *udev;
903 struct rtl_usb_priv *usb_priv;
905 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
906 sizeof(struct rtl_usb_priv), &rtl_ops);
908 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
912 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
914 if (!rtlpriv->usb_data)
917 /* this spin lock must be initialized early */
918 spin_lock_init(&rtlpriv->locks.usb_lock);
920 rtlpriv->usb_data_index = 0;
921 SET_IEEE80211_DEV(hw, &intf->dev);
922 udev = interface_to_usbdev(intf);
924 usb_priv = rtl_usbpriv(hw);
925 memset(usb_priv, 0, sizeof(*usb_priv));
926 usb_priv->dev.intf = intf;
927 usb_priv->dev.udev = udev;
928 usb_set_intfdata(intf, hw);
929 /* init cfg & intf_ops */
930 rtlpriv->rtlhal.interface = INTF_USB;
931 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
932 rtlpriv->intf_ops = &rtl_usb_ops;
933 rtl_dbgp_flag_init(hw);
934 /* Init IO handler */
935 _rtl_usb_io_handler_init(&udev->dev, hw);
936 rtlpriv->cfg->ops->read_chip_version(hw);
937 /*like read eeprom and so on */
938 rtlpriv->cfg->ops->read_eeprom_info(hw);
939 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
940 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
941 ("Can't init_sw_vars.\n"));
944 rtlpriv->cfg->ops->init_sw_leds(hw);
945 err = _rtl_usb_init(hw);
946 err = _rtl_usb_init_sw(hw);
947 /* Init mac80211 sw */
948 err = rtl_init_core(hw);
950 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
951 ("Can't allocate sw for mac80211.\n"));
956 /* rtl_init_rfkill(hw); */
958 err = ieee80211_register_hw(hw);
960 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
961 ("Can't register mac80211 hw.\n"));
964 rtlpriv->mac80211.mac80211_registered = 1;
966 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
970 _rtl_usb_io_handler_release(hw);
971 ieee80211_free_hw(hw);
975 EXPORT_SYMBOL(rtl_usb_probe);
977 void rtl_usb_disconnect(struct usb_interface *intf)
979 struct ieee80211_hw *hw = usb_get_intfdata(intf);
980 struct rtl_priv *rtlpriv = rtl_priv(hw);
981 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
982 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
984 if (unlikely(!rtlpriv))
986 /*ieee80211_unregister_hw will call ops_stop */
987 if (rtlmac->mac80211_registered == 1) {
988 ieee80211_unregister_hw(hw);
989 rtlmac->mac80211_registered = 0;
991 rtl_deinit_deferred_work(hw);
992 rtlpriv->intf_ops->adapter_stop(hw);
995 /* rtl_deinit_rfkill(hw); */
998 kfree(rtlpriv->usb_data);
999 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1000 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1001 _rtl_usb_io_handler_release(hw);
1002 usb_put_dev(rtlusb->udev);
1003 usb_set_intfdata(intf, NULL);
1004 ieee80211_free_hw(hw);
1006 EXPORT_SYMBOL(rtl_usb_disconnect);
1008 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1012 EXPORT_SYMBOL(rtl_usb_suspend);
1014 int rtl_usb_resume(struct usb_interface *pusb_intf)
1018 EXPORT_SYMBOL(rtl_usb_resume);