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 usb_device *udev, u32 addr, u16 len)
124 data = kmalloc(sizeof(u32), GFP_KERNEL);
127 request = REALTEK_USB_VENQT_CMD_REQ;
128 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
131 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
137 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
139 struct device *dev = rtlpriv->io.dev;
141 return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
144 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
146 struct device *dev = rtlpriv->io.dev;
148 return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
151 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
153 struct device *dev = rtlpriv->io.dev;
155 return _usb_read_sync(to_usb_device(dev), addr, 4);
158 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
166 request = REALTEK_USB_VENQT_CMD_REQ;
167 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
168 wvalue = (u16)(addr&0x0000ffff);
170 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
174 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
176 struct device *dev = rtlpriv->io.dev;
178 _usb_write_async(to_usb_device(dev), addr, val, 1);
181 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
183 struct device *dev = rtlpriv->io.dev;
185 _usb_write_async(to_usb_device(dev), addr, val, 2);
188 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
190 struct device *dev = rtlpriv->io.dev;
192 _usb_write_async(to_usb_device(dev), addr, val, 4);
195 static void _rtl_usb_io_handler_init(struct device *dev,
196 struct ieee80211_hw *hw)
198 struct rtl_priv *rtlpriv = rtl_priv(hw);
200 rtlpriv->io.dev = dev;
201 mutex_init(&rtlpriv->io.bb_mutex);
202 rtlpriv->io.write8_async = _usb_write8_async;
203 rtlpriv->io.write16_async = _usb_write16_async;
204 rtlpriv->io.write32_async = _usb_write32_async;
205 rtlpriv->io.read8_sync = _usb_read8_sync;
206 rtlpriv->io.read16_sync = _usb_read16_sync;
207 rtlpriv->io.read32_sync = _usb_read32_sync;
210 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
212 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
214 mutex_destroy(&rtlpriv->io.bb_mutex);
219 * Default aggregation handler. Do nothing and just return the oldest skb.
221 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
222 struct sk_buff_head *list)
224 return skb_dequeue(list);
227 #define IS_HIGH_SPEED_USB(udev) \
228 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
230 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
233 struct rtl_priv *rtlpriv = rtl_priv(hw);
234 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
236 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
237 ? USB_HIGH_SPEED_BULK_SIZE
238 : USB_FULL_SPEED_BULK_SIZE;
240 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
241 rtlusb->max_bulk_out_size));
243 for (i = 0; i < __RTL_TXQ_NUM; i++) {
244 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
246 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
247 ("Invalid endpoint map setting!\n"));
252 rtlusb->usb_tx_post_hdl =
253 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
254 rtlusb->usb_tx_cleanup =
255 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
256 rtlusb->usb_tx_aggregate_hdl =
257 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
258 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
259 : &_none_usb_tx_aggregate_hdl;
261 init_usb_anchor(&rtlusb->tx_submitted);
262 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
263 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
264 init_usb_anchor(&rtlusb->tx_pending[i]);
269 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
271 struct rtl_priv *rtlpriv = rtl_priv(hw);
272 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
273 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
275 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
276 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
277 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
278 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
279 rtlusb->usb_rx_segregate_hdl =
280 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
282 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
283 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
284 init_usb_anchor(&rtlusb->rx_submitted);
288 static int _rtl_usb_init(struct ieee80211_hw *hw)
290 struct rtl_priv *rtlpriv = rtl_priv(hw);
291 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
292 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
295 struct usb_interface *usb_intf = rtlusb->intf;
296 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
298 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
299 for (epidx = 0; epidx < epnums; epidx++) {
300 struct usb_endpoint_descriptor *pep_desc;
301 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
303 if (usb_endpoint_dir_in(pep_desc))
304 rtlusb->in_ep_nums++;
305 else if (usb_endpoint_dir_out(pep_desc))
306 rtlusb->out_ep_nums++;
308 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
309 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
310 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
311 pep_desc->bInterval));
313 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
316 /* usb endpoint mapping */
317 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
318 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
319 _rtl_usb_init_tx(hw);
320 _rtl_usb_init_rx(hw);
324 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
326 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
327 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
328 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
329 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
332 ppsc->inactiveps = false;
333 ppsc->leisure_ps = false;
334 ppsc->fwctrl_lps = false;
335 ppsc->reg_fwctrl_lps = 3;
336 ppsc->reg_max_lps_awakeintvl = 5;
337 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
340 mac->beacon_interval = 100;
343 mac->min_space_cfg = 0;
344 mac->max_mss_density = 0;
346 /* set sane AMPDU defaults */
347 mac->current_ampdu_density = 7;
348 mac->current_ampdu_factor = 3;
351 rtlusb->acm_method = eAcmWay2_SW;
354 /* HIMR - turn all on */
355 rtlusb->irq_mask[0] = 0xFFFFFFFF;
356 /* HIMR_EX - turn all on */
357 rtlusb->irq_mask[1] = 0xFFFFFFFF;
358 rtlusb->disableHWSM = true;
362 #define __RADIO_TAP_SIZE_RSV 32
364 static void _rtl_rx_completed(struct urb *urb);
366 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
367 struct rtl_usb *rtlusb,
372 struct rtl_priv *rtlpriv = rtl_priv(hw);
374 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
377 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
378 ("Failed to __dev_alloc_skb!!\n"))
379 return ERR_PTR(-ENOMEM);
382 /* reserve some space for mac80211's radiotap */
383 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
384 usb_fill_bulk_urb(urb, rtlusb->udev,
385 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
386 skb->data, min(skb_tailroom(skb),
387 (int)rtlusb->rx_max_size),
388 _rtl_rx_completed, skb);
390 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
394 #undef __RADIO_TAP_SIZE_RSV
396 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
399 struct rtl_priv *rtlpriv = rtl_priv(hw);
400 u8 *rxdesc = skb->data;
401 struct ieee80211_hdr *hdr;
402 bool unicast = false;
404 struct ieee80211_rx_status rx_status = {0};
405 struct rtl_stats stats = {
411 skb_pull(skb, RTL_RX_DESC_SIZE);
412 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
413 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
414 hdr = (struct ieee80211_hdr *)(skb->data);
415 fc = hdr->frame_control;
417 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
419 if (is_broadcast_ether_addr(hdr->addr1)) {
421 } else if (is_multicast_ether_addr(hdr->addr1)) {
425 rtlpriv->stats.rxbytesunicast += skb->len;
428 rtl_is_special_data(hw, skb, false);
430 if (ieee80211_is_data(fc)) {
431 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
434 rtlpriv->link_info.num_rx_inperiod++;
439 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
442 struct rtl_priv *rtlpriv = rtl_priv(hw);
443 u8 *rxdesc = skb->data;
444 struct ieee80211_hdr *hdr;
445 bool unicast = false;
447 struct ieee80211_rx_status rx_status = {0};
448 struct rtl_stats stats = {
454 skb_pull(skb, RTL_RX_DESC_SIZE);
455 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
456 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
457 hdr = (struct ieee80211_hdr *)(skb->data);
458 fc = hdr->frame_control;
460 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
462 if (is_broadcast_ether_addr(hdr->addr1)) {
464 } else if (is_multicast_ether_addr(hdr->addr1)) {
468 rtlpriv->stats.rxbytesunicast += skb->len;
471 rtl_is_special_data(hw, skb, false);
473 if (ieee80211_is_data(fc)) {
474 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
477 rtlpriv->link_info.num_rx_inperiod++;
479 if (likely(rtl_action_proc(hw, skb, false))) {
480 struct sk_buff *uskb = NULL;
483 uskb = dev_alloc_skb(skb->len + 128);
484 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
486 pdata = (u8 *)skb_put(uskb, skb->len);
487 memcpy(pdata, skb->data, skb->len);
488 dev_kfree_skb_any(skb);
489 ieee80211_rx_irqsafe(hw, uskb);
491 dev_kfree_skb_any(skb);
496 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
498 struct sk_buff *_skb;
499 struct sk_buff_head rx_queue;
500 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
502 skb_queue_head_init(&rx_queue);
503 if (rtlusb->usb_rx_segregate_hdl)
504 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
505 WARN_ON(skb_queue_empty(&rx_queue));
506 while (!skb_queue_empty(&rx_queue)) {
507 _skb = skb_dequeue(&rx_queue);
508 _rtl_usb_rx_process_agg(hw, skb);
509 ieee80211_rx_irqsafe(hw, skb);
513 static void _rtl_rx_completed(struct urb *_urb)
515 struct sk_buff *skb = (struct sk_buff *)_urb->context;
516 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
517 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
518 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
519 struct rtl_priv *rtlpriv = rtl_priv(hw);
522 if (unlikely(IS_USB_STOP(rtlusb)))
525 if (likely(0 == _urb->status)) {
526 /* If this code were moved to work queue, would CPU
527 * utilization be improved? NOTE: We shall allocate another skb
528 * and reuse the original one.
530 skb_put(skb, _urb->actual_length);
532 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
533 struct sk_buff *_skb;
534 _rtl_usb_rx_process_noagg(hw, skb);
535 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
538 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
539 ("Can't allocate skb for bulk IN!\n"));
545 _rtl_rx_pre_process(hw, skb);
546 pr_err("rx agg not supported\n");
551 switch (_urb->status) {
563 skb_reset_tail_pointer(skb);
566 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
567 err = usb_submit_urb(_urb, GFP_ATOMIC);
569 usb_unanchor_urb(_urb);
575 dev_kfree_skb_irq(skb);
578 static int _rtl_usb_receive(struct ieee80211_hw *hw)
584 struct rtl_priv *rtlpriv = rtl_priv(hw);
585 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
587 WARN_ON(0 == rtlusb->rx_urb_num);
588 /* 1600 == 1514 + max WLAN header + rtk info */
589 WARN_ON(rtlusb->rx_max_size < 1600);
591 for (i = 0; i < rtlusb->rx_urb_num; i++) {
593 urb = usb_alloc_urb(0, GFP_KERNEL);
595 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
596 ("Failed to alloc URB!!\n"))
600 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
602 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
603 ("Failed to prep_rx_urb!!\n"))
608 usb_anchor_urb(urb, &rtlusb->rx_submitted);
609 err = usb_submit_urb(urb, GFP_KERNEL);
617 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
621 static int rtl_usb_start(struct ieee80211_hw *hw)
624 struct rtl_priv *rtlpriv = rtl_priv(hw);
625 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
626 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
628 err = rtlpriv->cfg->ops->hw_init(hw);
629 rtl_init_rx_config(hw);
631 /* Enable software */
632 SET_USB_START(rtlusb);
633 /* should after adapter start and interrupt enable. */
634 set_hal_start(rtlhal);
637 _rtl_usb_receive(hw);
646 /*======================= tx =========================================*/
647 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
650 struct sk_buff *_skb;
651 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
652 struct ieee80211_tx_info *txinfo;
654 SET_USB_STOP(rtlusb);
656 /* clean up rx stuff. */
657 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
659 /* clean up tx stuff */
660 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
661 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
662 rtlusb->usb_tx_cleanup(hw, _skb);
663 txinfo = IEEE80211_SKB_CB(_skb);
664 ieee80211_tx_info_clear_status(txinfo);
665 txinfo->flags |= IEEE80211_TX_STAT_ACK;
666 ieee80211_tx_status_irqsafe(hw, _skb);
668 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
670 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
675 * We may add some struct into struct rtl_usb later. Do deinit here.
678 static void rtl_usb_deinit(struct ieee80211_hw *hw)
683 static void rtl_usb_stop(struct ieee80211_hw *hw)
685 struct rtl_priv *rtlpriv = rtl_priv(hw);
686 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
687 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
689 /* should after adapter start and interrupt enable. */
690 set_hal_stop(rtlhal);
691 /* Enable software */
692 SET_USB_STOP(rtlusb);
694 rtlpriv->cfg->ops->hw_disable(hw);
697 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
700 struct rtl_priv *rtlpriv = rtl_priv(hw);
701 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
703 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
704 err = usb_submit_urb(_urb, GFP_ATOMIC);
708 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
709 ("Failed to submit urb.\n"));
710 usb_unanchor_urb(_urb);
711 skb = (struct sk_buff *)_urb->context;
717 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
720 struct rtl_priv *rtlpriv = rtl_priv(hw);
721 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
722 struct ieee80211_tx_info *txinfo;
724 rtlusb->usb_tx_post_hdl(hw, urb, skb);
725 skb_pull(skb, RTL_TX_HEADER_SIZE);
726 txinfo = IEEE80211_SKB_CB(skb);
727 ieee80211_tx_info_clear_status(txinfo);
728 txinfo->flags |= IEEE80211_TX_STAT_ACK;
731 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
732 ("Urb has error status 0x%X\n", urb->status));
735 /* TODO: statistics */
737 ieee80211_tx_status_irqsafe(hw, skb);
741 static void _rtl_tx_complete(struct urb *urb)
743 struct sk_buff *skb = (struct sk_buff *)urb->context;
744 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
745 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
746 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
749 if (unlikely(IS_USB_STOP(rtlusb)))
751 err = _usb_tx_post(hw, urb, skb);
753 /* Ignore error and keep issuiing other urbs */
758 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
759 struct sk_buff *skb, u32 ep_num)
761 struct rtl_priv *rtlpriv = rtl_priv(hw);
762 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
765 WARN_ON(NULL == skb);
766 _urb = usb_alloc_urb(0, GFP_ATOMIC);
768 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
769 ("Can't allocate URB for bulk out!\n"));
773 _rtl_install_trx_info(rtlusb, skb, ep_num);
774 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
775 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
776 _urb->transfer_flags |= URB_ZERO_PACKET;
780 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
783 struct rtl_priv *rtlpriv = rtl_priv(hw);
784 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
786 struct urb *_urb = NULL;
787 struct sk_buff *_skb = NULL;
788 struct sk_buff_head *skb_list;
789 struct usb_anchor *urb_list;
791 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
792 if (unlikely(IS_USB_STOP(rtlusb))) {
793 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
794 ("USB device is stopping...\n"));
798 ep_num = rtlusb->ep_map.ep_mapping[qnum];
799 skb_list = &rtlusb->tx_skb_queue[ep_num];
801 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
802 if (unlikely(!_urb)) {
803 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
804 ("Can't allocate urb. Drop skb!\n"));
807 urb_list = &rtlusb->tx_pending[ep_num];
808 _rtl_submit_tx_urb(hw, _urb);
811 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
814 struct rtl_priv *rtlpriv = rtl_priv(hw);
815 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
816 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
817 struct rtl_tx_desc *pdesc = NULL;
818 struct rtl_tcb_desc tcb_desc;
819 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
820 __le16 fc = hdr->frame_control;
821 u8 *pda_addr = hdr->addr1;
827 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
828 if (ieee80211_is_auth(fc)) {
829 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
833 if (rtlpriv->psc.sw_ps_enabled) {
834 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
835 !ieee80211_has_pm(fc))
836 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
839 rtl_action_proc(hw, skb, true);
840 if (is_multicast_ether_addr(pda_addr))
841 rtlpriv->stats.txbytesmulticast += skb->len;
842 else if (is_broadcast_ether_addr(pda_addr))
843 rtlpriv->stats.txbytesbroadcast += skb->len;
845 rtlpriv->stats.txbytesunicast += skb->len;
846 if (ieee80211_is_data_qos(fc)) {
847 qc = ieee80211_get_qos_ctl(hdr);
848 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
849 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
850 IEEE80211_SCTL_SEQ) >> 4;
854 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
855 hw_queue, &tcb_desc);
856 if (!ieee80211_has_morefrags(hdr->frame_control)) {
858 mac->tids[tid].seq_number = seq_number;
860 if (ieee80211_is_data(fc))
861 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
864 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
865 struct rtl_tcb_desc *dummy)
867 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
868 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
869 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
870 __le16 fc = hdr->frame_control;
873 if (unlikely(is_hal_stop(rtlhal)))
875 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
876 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
877 _rtl_usb_transmit(hw, skb, hw_queue);
881 dev_kfree_skb_any(skb);
885 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
891 static struct rtl_intf_ops rtl_usb_ops = {
892 .adapter_start = rtl_usb_start,
893 .adapter_stop = rtl_usb_stop,
894 .adapter_tx = rtl_usb_tx,
895 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
898 int __devinit rtl_usb_probe(struct usb_interface *intf,
899 const struct usb_device_id *id)
902 struct ieee80211_hw *hw = NULL;
903 struct rtl_priv *rtlpriv = NULL;
904 struct usb_device *udev;
905 struct rtl_usb_priv *usb_priv;
907 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
908 sizeof(struct rtl_usb_priv), &rtl_ops);
910 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
914 SET_IEEE80211_DEV(hw, &intf->dev);
915 udev = interface_to_usbdev(intf);
917 usb_priv = rtl_usbpriv(hw);
918 memset(usb_priv, 0, sizeof(*usb_priv));
919 usb_priv->dev.intf = intf;
920 usb_priv->dev.udev = udev;
921 usb_set_intfdata(intf, hw);
922 /* init cfg & intf_ops */
923 rtlpriv->rtlhal.interface = INTF_USB;
924 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
925 rtlpriv->intf_ops = &rtl_usb_ops;
926 rtl_dbgp_flag_init(hw);
927 /* Init IO handler */
928 _rtl_usb_io_handler_init(&udev->dev, hw);
929 rtlpriv->cfg->ops->read_chip_version(hw);
930 /*like read eeprom and so on */
931 rtlpriv->cfg->ops->read_eeprom_info(hw);
932 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
933 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
934 ("Can't init_sw_vars.\n"));
937 rtlpriv->cfg->ops->init_sw_leds(hw);
938 err = _rtl_usb_init(hw);
939 err = _rtl_usb_init_sw(hw);
940 /* Init mac80211 sw */
941 err = rtl_init_core(hw);
943 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
944 ("Can't allocate sw for mac80211.\n"));
949 /* rtl_init_rfkill(hw); */
951 err = ieee80211_register_hw(hw);
953 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
954 ("Can't register mac80211 hw.\n"));
957 rtlpriv->mac80211.mac80211_registered = 1;
959 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
963 _rtl_usb_io_handler_release(hw);
964 ieee80211_free_hw(hw);
968 EXPORT_SYMBOL(rtl_usb_probe);
970 void rtl_usb_disconnect(struct usb_interface *intf)
972 struct ieee80211_hw *hw = usb_get_intfdata(intf);
973 struct rtl_priv *rtlpriv = rtl_priv(hw);
974 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
975 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
977 if (unlikely(!rtlpriv))
979 /*ieee80211_unregister_hw will call ops_stop */
980 if (rtlmac->mac80211_registered == 1) {
981 ieee80211_unregister_hw(hw);
982 rtlmac->mac80211_registered = 0;
984 rtl_deinit_deferred_work(hw);
985 rtlpriv->intf_ops->adapter_stop(hw);
988 /* rtl_deinit_rfkill(hw); */
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);