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>
37 #define REALTEK_USB_VENQT_READ 0xC0
38 #define REALTEK_USB_VENQT_WRITE 0x40
39 #define REALTEK_USB_VENQT_CMD_REQ 0x05
40 #define REALTEK_USB_VENQT_CMD_IDX 0x00
42 #define REALTEK_USB_VENQT_MAX_BUF_SIZE 254
44 static void usbctrl_async_callback(struct urb *urb)
50 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
51 u16 value, u16 index, void *pdata,
57 struct usb_ctrlrequest *dr;
59 struct rtl819x_async_write_data {
60 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
61 struct usb_ctrlrequest dr;
64 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
65 reqtype = REALTEK_USB_VENQT_WRITE;
67 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
71 urb = usb_alloc_urb(0, GFP_ATOMIC);
79 dr->bRequestType = reqtype;
80 dr->bRequest = request;
81 dr->wValue = cpu_to_le16(value);
82 dr->wIndex = cpu_to_le16(index);
83 dr->wLength = cpu_to_le16(len);
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,
103 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
104 reqtype = REALTEK_USB_VENQT_READ;
106 status = usb_control_msg(udev, pipe, request, reqtype, value, index,
107 pdata, len, 0); /* max. timeout */
110 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
111 value, status, *(u32 *)pdata);
115 static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
123 data = kmalloc(sizeof(u32), GFP_KERNEL);
126 request = REALTEK_USB_VENQT_CMD_REQ;
127 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
130 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
136 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
138 struct device *dev = rtlpriv->io.dev;
140 return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
143 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
145 struct device *dev = rtlpriv->io.dev;
147 return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
150 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
152 struct device *dev = rtlpriv->io.dev;
154 return _usb_read_sync(to_usb_device(dev), addr, 4);
157 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
165 request = REALTEK_USB_VENQT_CMD_REQ;
166 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
167 wvalue = (u16)(addr&0x0000ffff);
169 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
173 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
175 struct device *dev = rtlpriv->io.dev;
177 _usb_write_async(to_usb_device(dev), addr, val, 1);
180 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
182 struct device *dev = rtlpriv->io.dev;
184 _usb_write_async(to_usb_device(dev), addr, val, 2);
187 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
189 struct device *dev = rtlpriv->io.dev;
191 _usb_write_async(to_usb_device(dev), addr, val, 4);
194 static void _rtl_usb_io_handler_init(struct device *dev,
195 struct ieee80211_hw *hw)
197 struct rtl_priv *rtlpriv = rtl_priv(hw);
199 rtlpriv->io.dev = dev;
200 mutex_init(&rtlpriv->io.bb_mutex);
201 rtlpriv->io.write8_async = _usb_write8_async;
202 rtlpriv->io.write16_async = _usb_write16_async;
203 rtlpriv->io.write32_async = _usb_write32_async;
204 rtlpriv->io.read8_sync = _usb_read8_sync;
205 rtlpriv->io.read16_sync = _usb_read16_sync;
206 rtlpriv->io.read32_sync = _usb_read32_sync;
209 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
211 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
213 mutex_destroy(&rtlpriv->io.bb_mutex);
218 * Default aggregation handler. Do nothing and just return the oldest skb.
220 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
221 struct sk_buff_head *list)
223 return skb_dequeue(list);
226 #define IS_HIGH_SPEED_USB(udev) \
227 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
229 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
232 struct rtl_priv *rtlpriv = rtl_priv(hw);
233 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
235 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
236 ? USB_HIGH_SPEED_BULK_SIZE
237 : USB_FULL_SPEED_BULK_SIZE;
239 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
240 rtlusb->max_bulk_out_size));
242 for (i = 0; i < __RTL_TXQ_NUM; i++) {
243 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
245 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
246 ("Invalid endpoint map setting!\n"));
251 rtlusb->usb_tx_post_hdl =
252 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
253 rtlusb->usb_tx_cleanup =
254 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
255 rtlusb->usb_tx_aggregate_hdl =
256 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
257 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
258 : &_none_usb_tx_aggregate_hdl;
260 init_usb_anchor(&rtlusb->tx_submitted);
261 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
262 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
263 init_usb_anchor(&rtlusb->tx_pending[i]);
268 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
270 struct rtl_priv *rtlpriv = rtl_priv(hw);
271 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
272 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
274 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
275 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
276 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
277 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
278 rtlusb->usb_rx_segregate_hdl =
279 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
281 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
282 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
283 init_usb_anchor(&rtlusb->rx_submitted);
287 static int _rtl_usb_init(struct ieee80211_hw *hw)
289 struct rtl_priv *rtlpriv = rtl_priv(hw);
290 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
291 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
294 struct usb_interface *usb_intf = rtlusb->intf;
295 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
297 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
298 for (epidx = 0; epidx < epnums; epidx++) {
299 struct usb_endpoint_descriptor *pep_desc;
300 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
302 if (usb_endpoint_dir_in(pep_desc))
303 rtlusb->in_ep_nums++;
304 else if (usb_endpoint_dir_out(pep_desc))
305 rtlusb->out_ep_nums++;
307 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
308 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
309 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
310 pep_desc->bInterval));
312 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
315 /* usb endpoint mapping */
316 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
317 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
318 _rtl_usb_init_tx(hw);
319 _rtl_usb_init_rx(hw);
323 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
325 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
326 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
327 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
328 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
331 ppsc->inactiveps = false;
332 ppsc->leisure_ps = false;
333 ppsc->fwctrl_lps = false;
334 ppsc->reg_fwctrl_lps = 3;
335 ppsc->reg_max_lps_awakeintvl = 5;
336 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
339 mac->beacon_interval = 100;
342 mac->min_space_cfg = 0;
343 mac->max_mss_density = 0;
345 /* set sane AMPDU defaults */
346 mac->current_ampdu_density = 7;
347 mac->current_ampdu_factor = 3;
350 rtlusb->acm_method = eAcmWay2_SW;
353 /* HIMR - turn all on */
354 rtlusb->irq_mask[0] = 0xFFFFFFFF;
355 /* HIMR_EX - turn all on */
356 rtlusb->irq_mask[1] = 0xFFFFFFFF;
357 rtlusb->disableHWSM = true;
361 #define __RADIO_TAP_SIZE_RSV 32
363 static void _rtl_rx_completed(struct urb *urb);
365 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
366 struct rtl_usb *rtlusb,
371 struct rtl_priv *rtlpriv = rtl_priv(hw);
373 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
376 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
377 ("Failed to __dev_alloc_skb!!\n"))
378 return ERR_PTR(-ENOMEM);
381 /* reserve some space for mac80211's radiotap */
382 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
383 usb_fill_bulk_urb(urb, rtlusb->udev,
384 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
385 skb->data, min(skb_tailroom(skb),
386 (int)rtlusb->rx_max_size),
387 _rtl_rx_completed, skb);
389 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
393 #undef __RADIO_TAP_SIZE_RSV
395 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
398 struct rtl_priv *rtlpriv = rtl_priv(hw);
399 u8 *rxdesc = skb->data;
400 struct ieee80211_hdr *hdr;
401 bool unicast = false;
403 struct ieee80211_rx_status rx_status = {0};
404 struct rtl_stats stats = {
410 skb_pull(skb, RTL_RX_DESC_SIZE);
411 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
412 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
413 hdr = (struct ieee80211_hdr *)(skb->data);
414 fc = hdr->frame_control;
416 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
418 if (is_broadcast_ether_addr(hdr->addr1)) {
420 } else if (is_multicast_ether_addr(hdr->addr1)) {
424 rtlpriv->stats.rxbytesunicast += skb->len;
427 rtl_is_special_data(hw, skb, false);
429 if (ieee80211_is_data(fc)) {
430 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
433 rtlpriv->link_info.num_rx_inperiod++;
438 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
441 struct rtl_priv *rtlpriv = rtl_priv(hw);
442 u8 *rxdesc = skb->data;
443 struct ieee80211_hdr *hdr;
444 bool unicast = false;
446 struct ieee80211_rx_status rx_status = {0};
447 struct rtl_stats stats = {
453 skb_pull(skb, RTL_RX_DESC_SIZE);
454 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
455 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
456 hdr = (struct ieee80211_hdr *)(skb->data);
457 fc = hdr->frame_control;
459 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
461 if (is_broadcast_ether_addr(hdr->addr1)) {
463 } else if (is_multicast_ether_addr(hdr->addr1)) {
467 rtlpriv->stats.rxbytesunicast += skb->len;
470 rtl_is_special_data(hw, skb, false);
472 if (ieee80211_is_data(fc)) {
473 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
476 rtlpriv->link_info.num_rx_inperiod++;
478 if (likely(rtl_action_proc(hw, skb, false))) {
479 struct sk_buff *uskb = NULL;
482 uskb = dev_alloc_skb(skb->len + 128);
483 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
485 pdata = (u8 *)skb_put(uskb, skb->len);
486 memcpy(pdata, skb->data, skb->len);
487 dev_kfree_skb_any(skb);
488 ieee80211_rx_irqsafe(hw, uskb);
490 dev_kfree_skb_any(skb);
495 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
497 struct sk_buff *_skb;
498 struct sk_buff_head rx_queue;
499 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
501 skb_queue_head_init(&rx_queue);
502 if (rtlusb->usb_rx_segregate_hdl)
503 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
504 WARN_ON(skb_queue_empty(&rx_queue));
505 while (!skb_queue_empty(&rx_queue)) {
506 _skb = skb_dequeue(&rx_queue);
507 _rtl_usb_rx_process_agg(hw, skb);
508 ieee80211_rx_irqsafe(hw, skb);
512 static void _rtl_rx_completed(struct urb *_urb)
514 struct sk_buff *skb = (struct sk_buff *)_urb->context;
515 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
516 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
517 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
518 struct rtl_priv *rtlpriv = rtl_priv(hw);
521 if (unlikely(IS_USB_STOP(rtlusb)))
524 if (likely(0 == _urb->status)) {
525 /* If this code were moved to work queue, would CPU
526 * utilization be improved? NOTE: We shall allocate another skb
527 * and reuse the original one.
529 skb_put(skb, _urb->actual_length);
531 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
532 struct sk_buff *_skb;
533 _rtl_usb_rx_process_noagg(hw, skb);
534 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
537 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
538 ("Can't allocate skb for bulk IN!\n"));
544 _rtl_rx_pre_process(hw, skb);
545 pr_err("rx agg not supported\n");
550 switch (_urb->status) {
562 skb_reset_tail_pointer(skb);
565 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
566 err = usb_submit_urb(_urb, GFP_ATOMIC);
568 usb_unanchor_urb(_urb);
574 dev_kfree_skb_irq(skb);
577 static int _rtl_usb_receive(struct ieee80211_hw *hw)
583 struct rtl_priv *rtlpriv = rtl_priv(hw);
584 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
586 WARN_ON(0 == rtlusb->rx_urb_num);
587 /* 1600 == 1514 + max WLAN header + rtk info */
588 WARN_ON(rtlusb->rx_max_size < 1600);
590 for (i = 0; i < rtlusb->rx_urb_num; i++) {
592 urb = usb_alloc_urb(0, GFP_KERNEL);
594 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
595 ("Failed to alloc URB!!\n"))
599 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
601 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
602 ("Failed to prep_rx_urb!!\n"))
607 usb_anchor_urb(urb, &rtlusb->rx_submitted);
608 err = usb_submit_urb(urb, GFP_KERNEL);
616 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
620 static int rtl_usb_start(struct ieee80211_hw *hw)
623 struct rtl_priv *rtlpriv = rtl_priv(hw);
624 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
625 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
627 err = rtlpriv->cfg->ops->hw_init(hw);
628 rtl_init_rx_config(hw);
630 /* Enable software */
631 SET_USB_START(rtlusb);
632 /* should after adapter start and interrupt enable. */
633 set_hal_start(rtlhal);
636 _rtl_usb_receive(hw);
645 /*======================= tx =========================================*/
646 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
649 struct sk_buff *_skb;
650 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
651 struct ieee80211_tx_info *txinfo;
653 SET_USB_STOP(rtlusb);
655 /* clean up rx stuff. */
656 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
658 /* clean up tx stuff */
659 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
660 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
661 rtlusb->usb_tx_cleanup(hw, _skb);
662 txinfo = IEEE80211_SKB_CB(_skb);
663 ieee80211_tx_info_clear_status(txinfo);
664 txinfo->flags |= IEEE80211_TX_STAT_ACK;
665 ieee80211_tx_status_irqsafe(hw, _skb);
667 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
669 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
674 * We may add some struct into struct rtl_usb later. Do deinit here.
677 static void rtl_usb_deinit(struct ieee80211_hw *hw)
682 static void rtl_usb_stop(struct ieee80211_hw *hw)
684 struct rtl_priv *rtlpriv = rtl_priv(hw);
685 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
686 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
688 /* should after adapter start and interrupt enable. */
689 set_hal_stop(rtlhal);
690 /* Enable software */
691 SET_USB_STOP(rtlusb);
693 rtlpriv->cfg->ops->hw_disable(hw);
696 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
699 struct rtl_priv *rtlpriv = rtl_priv(hw);
700 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
702 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
703 err = usb_submit_urb(_urb, GFP_ATOMIC);
707 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
708 ("Failed to submit urb.\n"));
709 usb_unanchor_urb(_urb);
710 skb = (struct sk_buff *)_urb->context;
716 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
719 struct rtl_priv *rtlpriv = rtl_priv(hw);
720 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
721 struct ieee80211_tx_info *txinfo;
723 rtlusb->usb_tx_post_hdl(hw, urb, skb);
724 skb_pull(skb, RTL_TX_HEADER_SIZE);
725 txinfo = IEEE80211_SKB_CB(skb);
726 ieee80211_tx_info_clear_status(txinfo);
727 txinfo->flags |= IEEE80211_TX_STAT_ACK;
730 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
731 ("Urb has error status 0x%X\n", urb->status));
734 /* TODO: statistics */
736 ieee80211_tx_status_irqsafe(hw, skb);
740 static void _rtl_tx_complete(struct urb *urb)
742 struct sk_buff *skb = (struct sk_buff *)urb->context;
743 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
744 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
745 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
748 if (unlikely(IS_USB_STOP(rtlusb)))
750 err = _usb_tx_post(hw, urb, skb);
752 /* Ignore error and keep issuiing other urbs */
757 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
758 struct sk_buff *skb, u32 ep_num)
760 struct rtl_priv *rtlpriv = rtl_priv(hw);
761 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
764 WARN_ON(NULL == skb);
765 _urb = usb_alloc_urb(0, GFP_ATOMIC);
767 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
768 ("Can't allocate URB for bulk out!\n"));
772 _rtl_install_trx_info(rtlusb, skb, ep_num);
773 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
774 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
775 _urb->transfer_flags |= URB_ZERO_PACKET;
779 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
782 struct rtl_priv *rtlpriv = rtl_priv(hw);
783 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
785 struct urb *_urb = NULL;
786 struct sk_buff *_skb = NULL;
787 struct sk_buff_head *skb_list;
788 struct usb_anchor *urb_list;
790 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
791 if (unlikely(IS_USB_STOP(rtlusb))) {
792 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
793 ("USB device is stopping...\n"));
797 ep_num = rtlusb->ep_map.ep_mapping[qnum];
798 skb_list = &rtlusb->tx_skb_queue[ep_num];
800 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
801 if (unlikely(!_urb)) {
802 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
803 ("Can't allocate urb. Drop skb!\n"));
806 urb_list = &rtlusb->tx_pending[ep_num];
807 _rtl_submit_tx_urb(hw, _urb);
810 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
813 struct rtl_priv *rtlpriv = rtl_priv(hw);
814 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
815 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
816 struct rtl_tx_desc *pdesc = NULL;
817 struct rtl_tcb_desc tcb_desc;
818 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
819 __le16 fc = hdr->frame_control;
820 u8 *pda_addr = hdr->addr1;
826 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
827 if (ieee80211_is_auth(fc)) {
828 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
832 if (rtlpriv->psc.sw_ps_enabled) {
833 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
834 !ieee80211_has_pm(fc))
835 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
838 rtl_action_proc(hw, skb, true);
839 if (is_multicast_ether_addr(pda_addr))
840 rtlpriv->stats.txbytesmulticast += skb->len;
841 else if (is_broadcast_ether_addr(pda_addr))
842 rtlpriv->stats.txbytesbroadcast += skb->len;
844 rtlpriv->stats.txbytesunicast += skb->len;
845 if (ieee80211_is_data_qos(fc)) {
846 qc = ieee80211_get_qos_ctl(hdr);
847 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
848 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
849 IEEE80211_SCTL_SEQ) >> 4;
853 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
854 hw_queue, &tcb_desc);
855 if (!ieee80211_has_morefrags(hdr->frame_control)) {
857 mac->tids[tid].seq_number = seq_number;
859 if (ieee80211_is_data(fc))
860 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
863 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
864 struct rtl_tcb_desc *dummy)
866 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
867 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
868 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
869 __le16 fc = hdr->frame_control;
872 if (unlikely(is_hal_stop(rtlhal)))
874 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
875 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
876 _rtl_usb_transmit(hw, skb, hw_queue);
880 dev_kfree_skb_any(skb);
884 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
890 static struct rtl_intf_ops rtl_usb_ops = {
891 .adapter_start = rtl_usb_start,
892 .adapter_stop = rtl_usb_stop,
893 .adapter_tx = rtl_usb_tx,
894 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
897 int __devinit rtl_usb_probe(struct usb_interface *intf,
898 const struct usb_device_id *id)
901 struct ieee80211_hw *hw = NULL;
902 struct rtl_priv *rtlpriv = NULL;
903 struct usb_device *udev;
904 struct rtl_usb_priv *usb_priv;
906 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
907 sizeof(struct rtl_usb_priv), &rtl_ops);
909 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
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 rtlpriv->cfg->ops->deinit_sw_leds(hw);
991 rtlpriv->cfg->ops->deinit_sw_vars(hw);
992 _rtl_usb_io_handler_release(hw);
993 usb_put_dev(rtlusb->udev);
994 usb_set_intfdata(intf, NULL);
995 ieee80211_free_hw(hw);
997 EXPORT_SYMBOL(rtl_usb_disconnect);
999 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1003 EXPORT_SYMBOL(rtl_usb_suspend);
1005 int rtl_usb_resume(struct usb_interface *pusb_intf)
1009 EXPORT_SYMBOL(rtl_usb_resume);