1 /******************************************************************************
3 * Copyright(c) 2009-2012 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 *****************************************************************************/
33 #include "rtl8192c/fw_common.h"
34 #include <linux/export.h>
36 #define REALTEK_USB_VENQT_READ 0xC0
37 #define REALTEK_USB_VENQT_WRITE 0x40
38 #define REALTEK_USB_VENQT_CMD_REQ 0x05
39 #define REALTEK_USB_VENQT_CMD_IDX 0x00
41 #define MAX_USBCTRL_VENDORREQ_TIMES 10
43 static void usbctrl_async_callback(struct urb *urb)
47 kfree(urb->setup_packet);
49 kfree(urb->transfer_buffer);
53 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
54 u16 value, u16 index, void *pdata,
60 struct usb_ctrlrequest *dr;
62 const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE;
65 if (WARN_ON_ONCE(len > databuf_maxlen))
68 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
69 reqtype = REALTEK_USB_VENQT_WRITE;
71 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
75 databuf = kmalloc(databuf_maxlen, GFP_ATOMIC);
81 urb = usb_alloc_urb(0, GFP_ATOMIC);
88 dr->bRequestType = reqtype;
89 dr->bRequest = request;
90 dr->wValue = cpu_to_le16(value);
91 dr->wIndex = cpu_to_le16(index);
92 dr->wLength = cpu_to_le16(len);
93 /* data are already in little-endian order */
94 memcpy(databuf, pdata, len);
95 usb_fill_control_urb(urb, udev, pipe,
96 (unsigned char *)dr, databuf, len,
97 usbctrl_async_callback, NULL);
98 rc = usb_submit_urb(urb, GFP_ATOMIC);
107 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
108 u16 value, u16 index, void *pdata,
114 int vendorreq_times = 0;
117 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
118 reqtype = REALTEK_USB_VENQT_READ;
121 status = usb_control_msg(udev, pipe, request, reqtype, value,
122 index, pdata, len, 0); /*max. timeout*/
124 /* firmware download is checksumed, don't retry */
125 if ((value >= FW_8192C_START_ADDRESS &&
126 value <= FW_8192C_END_ADDRESS))
131 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
133 if (status < 0 && count++ < 4)
134 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
135 value, status, *(u32 *)pdata);
139 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
141 struct device *dev = rtlpriv->io.dev;
142 struct usb_device *udev = to_usb_device(dev);
149 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
150 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
151 rtlpriv->usb_data_index = 0;
152 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
153 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
154 request = REALTEK_USB_VENQT_CMD_REQ;
155 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
158 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
159 return le32_to_cpu(*data);
162 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
164 return (u8)_usb_read_sync(rtlpriv, addr, 1);
167 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
169 return (u16)_usb_read_sync(rtlpriv, addr, 2);
172 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
174 return _usb_read_sync(rtlpriv, addr, 4);
177 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
185 request = REALTEK_USB_VENQT_CMD_REQ;
186 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
187 wvalue = (u16)(addr&0x0000ffff);
188 data = cpu_to_le32(val);
189 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
193 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
195 struct device *dev = rtlpriv->io.dev;
197 _usb_write_async(to_usb_device(dev), addr, val, 1);
200 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
202 struct device *dev = rtlpriv->io.dev;
204 _usb_write_async(to_usb_device(dev), addr, val, 2);
207 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
209 struct device *dev = rtlpriv->io.dev;
211 _usb_write_async(to_usb_device(dev), addr, val, 4);
214 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
217 struct device *dev = rtlpriv->io.dev;
218 struct usb_device *udev = to_usb_device(dev);
219 u8 request = REALTEK_USB_VENQT_CMD_REQ;
220 u8 reqtype = REALTEK_USB_VENQT_WRITE;
222 u16 index = REALTEK_USB_VENQT_CMD_IDX;
223 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
226 wvalue = (u16)(addr & 0x0000ffff);
227 buffer = kmalloc(len, GFP_ATOMIC);
230 memcpy(buffer, data, len);
231 usb_control_msg(udev, pipe, request, reqtype, wvalue,
232 index, buffer, len, 50);
237 static void _rtl_usb_io_handler_init(struct device *dev,
238 struct ieee80211_hw *hw)
240 struct rtl_priv *rtlpriv = rtl_priv(hw);
242 rtlpriv->io.dev = dev;
243 mutex_init(&rtlpriv->io.bb_mutex);
244 rtlpriv->io.write8_async = _usb_write8_async;
245 rtlpriv->io.write16_async = _usb_write16_async;
246 rtlpriv->io.write32_async = _usb_write32_async;
247 rtlpriv->io.read8_sync = _usb_read8_sync;
248 rtlpriv->io.read16_sync = _usb_read16_sync;
249 rtlpriv->io.read32_sync = _usb_read32_sync;
250 rtlpriv->io.writeN_sync = _usb_writeN_sync;
253 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
255 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
257 mutex_destroy(&rtlpriv->io.bb_mutex);
262 * Default aggregation handler. Do nothing and just return the oldest skb.
264 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
265 struct sk_buff_head *list)
267 return skb_dequeue(list);
270 #define IS_HIGH_SPEED_USB(udev) \
271 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
273 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
276 struct rtl_priv *rtlpriv = rtl_priv(hw);
277 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
279 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
280 ? USB_HIGH_SPEED_BULK_SIZE
281 : USB_FULL_SPEED_BULK_SIZE;
283 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
284 rtlusb->max_bulk_out_size);
286 for (i = 0; i < __RTL_TXQ_NUM; i++) {
287 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
289 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
290 "Invalid endpoint map setting!\n");
295 rtlusb->usb_tx_post_hdl =
296 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
297 rtlusb->usb_tx_cleanup =
298 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
299 rtlusb->usb_tx_aggregate_hdl =
300 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
301 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
302 : &_none_usb_tx_aggregate_hdl;
304 init_usb_anchor(&rtlusb->tx_submitted);
305 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
306 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
307 init_usb_anchor(&rtlusb->tx_pending[i]);
312 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
314 struct rtl_priv *rtlpriv = rtl_priv(hw);
315 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
316 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
318 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
319 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
320 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
321 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
322 rtlusb->usb_rx_segregate_hdl =
323 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
325 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
326 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
327 init_usb_anchor(&rtlusb->rx_submitted);
331 static int _rtl_usb_init(struct ieee80211_hw *hw)
333 struct rtl_priv *rtlpriv = rtl_priv(hw);
334 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
335 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
338 struct usb_interface *usb_intf = rtlusb->intf;
339 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
341 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
342 for (epidx = 0; epidx < epnums; epidx++) {
343 struct usb_endpoint_descriptor *pep_desc;
344 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
346 if (usb_endpoint_dir_in(pep_desc))
347 rtlusb->in_ep_nums++;
348 else if (usb_endpoint_dir_out(pep_desc))
349 rtlusb->out_ep_nums++;
351 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
352 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
353 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
354 pep_desc->bInterval);
356 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
357 pr_err("Too few input end points found\n");
360 if (rtlusb->out_ep_nums == 0) {
361 pr_err("No output end points found\n");
364 /* usb endpoint mapping */
365 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
366 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
367 _rtl_usb_init_tx(hw);
368 _rtl_usb_init_rx(hw);
372 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
374 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
375 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
376 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
377 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
380 ppsc->inactiveps = false;
381 ppsc->leisure_ps = false;
382 ppsc->fwctrl_lps = false;
383 ppsc->reg_fwctrl_lps = 3;
384 ppsc->reg_max_lps_awakeintvl = 5;
385 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
388 mac->beacon_interval = 100;
391 mac->min_space_cfg = 0;
392 mac->max_mss_density = 0;
394 /* set sane AMPDU defaults */
395 mac->current_ampdu_density = 7;
396 mac->current_ampdu_factor = 3;
399 rtlusb->acm_method = eAcmWay2_SW;
402 /* HIMR - turn all on */
403 rtlusb->irq_mask[0] = 0xFFFFFFFF;
404 /* HIMR_EX - turn all on */
405 rtlusb->irq_mask[1] = 0xFFFFFFFF;
406 rtlusb->disableHWSM = true;
409 #define __RADIO_TAP_SIZE_RSV 32
411 static void _rtl_rx_completed(struct urb *urb);
413 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
414 struct rtl_usb *rtlusb,
419 struct rtl_priv *rtlpriv = rtl_priv(hw);
421 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
424 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
425 "Failed to __dev_alloc_skb!!\n");
426 return ERR_PTR(-ENOMEM);
429 /* reserve some space for mac80211's radiotap */
430 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
431 usb_fill_bulk_urb(urb, rtlusb->udev,
432 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
433 skb->data, min(skb_tailroom(skb),
434 (int)rtlusb->rx_max_size),
435 _rtl_rx_completed, skb);
437 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
441 #undef __RADIO_TAP_SIZE_RSV
443 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
446 struct rtl_priv *rtlpriv = rtl_priv(hw);
447 u8 *rxdesc = skb->data;
448 struct ieee80211_hdr *hdr;
449 bool unicast = false;
451 struct ieee80211_rx_status rx_status = {0};
452 struct rtl_stats stats = {
458 skb_pull(skb, RTL_RX_DESC_SIZE);
459 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
460 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
461 hdr = (struct ieee80211_hdr *)(skb->data);
462 fc = hdr->frame_control;
464 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
466 if (is_broadcast_ether_addr(hdr->addr1)) {
468 } else if (is_multicast_ether_addr(hdr->addr1)) {
472 rtlpriv->stats.rxbytesunicast += skb->len;
475 rtl_is_special_data(hw, skb, false);
477 if (ieee80211_is_data(fc)) {
478 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
481 rtlpriv->link_info.num_rx_inperiod++;
486 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
489 struct rtl_priv *rtlpriv = rtl_priv(hw);
490 u8 *rxdesc = skb->data;
491 struct ieee80211_hdr *hdr;
492 bool unicast = false;
494 struct ieee80211_rx_status rx_status = {0};
495 struct rtl_stats stats = {
501 skb_pull(skb, RTL_RX_DESC_SIZE);
502 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
503 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
504 hdr = (struct ieee80211_hdr *)(skb->data);
505 fc = hdr->frame_control;
507 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
509 if (is_broadcast_ether_addr(hdr->addr1)) {
511 } else if (is_multicast_ether_addr(hdr->addr1)) {
515 rtlpriv->stats.rxbytesunicast += skb->len;
518 rtl_is_special_data(hw, skb, false);
520 if (ieee80211_is_data(fc)) {
521 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
524 rtlpriv->link_info.num_rx_inperiod++;
526 if (likely(rtl_action_proc(hw, skb, false))) {
527 struct sk_buff *uskb = NULL;
530 uskb = dev_alloc_skb(skb->len + 128);
531 if (uskb) { /* drop packet on allocation failure */
532 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
534 pdata = (u8 *)skb_put(uskb, skb->len);
535 memcpy(pdata, skb->data, skb->len);
536 ieee80211_rx_irqsafe(hw, uskb);
538 dev_kfree_skb_any(skb);
540 dev_kfree_skb_any(skb);
545 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
547 struct sk_buff *_skb;
548 struct sk_buff_head rx_queue;
549 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
551 skb_queue_head_init(&rx_queue);
552 if (rtlusb->usb_rx_segregate_hdl)
553 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
554 WARN_ON(skb_queue_empty(&rx_queue));
555 while (!skb_queue_empty(&rx_queue)) {
556 _skb = skb_dequeue(&rx_queue);
557 _rtl_usb_rx_process_agg(hw, _skb);
558 ieee80211_rx_irqsafe(hw, _skb);
562 static void _rtl_rx_completed(struct urb *_urb)
564 struct sk_buff *skb = (struct sk_buff *)_urb->context;
565 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
566 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
567 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
568 struct rtl_priv *rtlpriv = rtl_priv(hw);
571 if (unlikely(IS_USB_STOP(rtlusb)))
574 if (likely(0 == _urb->status)) {
575 /* If this code were moved to work queue, would CPU
576 * utilization be improved? NOTE: We shall allocate another skb
577 * and reuse the original one.
579 skb_put(skb, _urb->actual_length);
581 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
582 struct sk_buff *_skb;
583 _rtl_usb_rx_process_noagg(hw, skb);
584 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
587 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
588 "Can't allocate skb for bulk IN!\n");
594 _rtl_rx_pre_process(hw, skb);
595 pr_err("rx agg not supported\n");
600 switch (_urb->status) {
612 skb_reset_tail_pointer(skb);
615 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
616 err = usb_submit_urb(_urb, GFP_ATOMIC);
618 usb_unanchor_urb(_urb);
624 dev_kfree_skb_irq(skb);
627 static int _rtl_usb_receive(struct ieee80211_hw *hw)
633 struct rtl_priv *rtlpriv = rtl_priv(hw);
634 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
636 WARN_ON(0 == rtlusb->rx_urb_num);
637 /* 1600 == 1514 + max WLAN header + rtk info */
638 WARN_ON(rtlusb->rx_max_size < 1600);
640 for (i = 0; i < rtlusb->rx_urb_num; i++) {
642 urb = usb_alloc_urb(0, GFP_KERNEL);
644 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
645 "Failed to alloc URB!!\n");
649 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
651 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
652 "Failed to prep_rx_urb!!\n");
658 usb_anchor_urb(urb, &rtlusb->rx_submitted);
659 err = usb_submit_urb(urb, GFP_KERNEL);
667 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
671 static int rtl_usb_start(struct ieee80211_hw *hw)
674 struct rtl_priv *rtlpriv = rtl_priv(hw);
675 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
676 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
678 err = rtlpriv->cfg->ops->hw_init(hw);
680 rtl_init_rx_config(hw);
682 /* Enable software */
683 SET_USB_START(rtlusb);
684 /* should after adapter start and interrupt enable. */
685 set_hal_start(rtlhal);
688 err = _rtl_usb_receive(hw);
698 /*======================= tx =========================================*/
699 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
702 struct sk_buff *_skb;
703 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
704 struct ieee80211_tx_info *txinfo;
706 SET_USB_STOP(rtlusb);
708 /* clean up rx stuff. */
709 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
711 /* clean up tx stuff */
712 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
713 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
714 rtlusb->usb_tx_cleanup(hw, _skb);
715 txinfo = IEEE80211_SKB_CB(_skb);
716 ieee80211_tx_info_clear_status(txinfo);
717 txinfo->flags |= IEEE80211_TX_STAT_ACK;
718 ieee80211_tx_status_irqsafe(hw, _skb);
720 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
722 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
727 * We may add some struct into struct rtl_usb later. Do deinit here.
730 static void rtl_usb_deinit(struct ieee80211_hw *hw)
735 static void rtl_usb_stop(struct ieee80211_hw *hw)
737 struct rtl_priv *rtlpriv = rtl_priv(hw);
738 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
739 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
741 /* should after adapter start and interrupt enable. */
742 set_hal_stop(rtlhal);
743 /* Enable software */
744 SET_USB_STOP(rtlusb);
746 rtlpriv->cfg->ops->hw_disable(hw);
749 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
752 struct rtl_priv *rtlpriv = rtl_priv(hw);
753 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
755 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
756 err = usb_submit_urb(_urb, GFP_ATOMIC);
760 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
761 "Failed to submit urb\n");
762 usb_unanchor_urb(_urb);
763 skb = (struct sk_buff *)_urb->context;
769 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
772 struct rtl_priv *rtlpriv = rtl_priv(hw);
773 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
774 struct ieee80211_tx_info *txinfo;
776 rtlusb->usb_tx_post_hdl(hw, urb, skb);
777 skb_pull(skb, RTL_TX_HEADER_SIZE);
778 txinfo = IEEE80211_SKB_CB(skb);
779 ieee80211_tx_info_clear_status(txinfo);
780 txinfo->flags |= IEEE80211_TX_STAT_ACK;
783 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
784 "Urb has error status 0x%X\n", urb->status);
787 /* TODO: statistics */
789 ieee80211_tx_status_irqsafe(hw, skb);
793 static void _rtl_tx_complete(struct urb *urb)
795 struct sk_buff *skb = (struct sk_buff *)urb->context;
796 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
797 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
798 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
801 if (unlikely(IS_USB_STOP(rtlusb)))
803 err = _usb_tx_post(hw, urb, skb);
805 /* Ignore error and keep issuiing other urbs */
810 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
811 struct sk_buff *skb, u32 ep_num)
813 struct rtl_priv *rtlpriv = rtl_priv(hw);
814 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
817 WARN_ON(NULL == skb);
818 _urb = usb_alloc_urb(0, GFP_ATOMIC);
820 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
821 "Can't allocate URB for bulk out!\n");
825 _rtl_install_trx_info(rtlusb, skb, ep_num);
826 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
827 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
828 _urb->transfer_flags |= URB_ZERO_PACKET;
832 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
835 struct rtl_priv *rtlpriv = rtl_priv(hw);
836 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
838 struct urb *_urb = NULL;
839 struct sk_buff *_skb = NULL;
841 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
842 if (unlikely(IS_USB_STOP(rtlusb))) {
843 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
844 "USB device is stopping...\n");
848 ep_num = rtlusb->ep_map.ep_mapping[qnum];
850 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
851 if (unlikely(!_urb)) {
852 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
853 "Can't allocate urb. Drop skb!\n");
857 _rtl_submit_tx_urb(hw, _urb);
860 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
861 struct ieee80211_sta *sta,
865 struct rtl_priv *rtlpriv = rtl_priv(hw);
866 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
867 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
868 struct rtl_tx_desc *pdesc = NULL;
869 struct rtl_tcb_desc tcb_desc;
870 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
871 __le16 fc = hdr->frame_control;
872 u8 *pda_addr = hdr->addr1;
878 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
879 if (ieee80211_is_auth(fc)) {
880 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
884 if (rtlpriv->psc.sw_ps_enabled) {
885 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
886 !ieee80211_has_pm(fc))
887 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
890 rtl_action_proc(hw, skb, true);
891 if (is_multicast_ether_addr(pda_addr))
892 rtlpriv->stats.txbytesmulticast += skb->len;
893 else if (is_broadcast_ether_addr(pda_addr))
894 rtlpriv->stats.txbytesbroadcast += skb->len;
896 rtlpriv->stats.txbytesunicast += skb->len;
897 if (ieee80211_is_data_qos(fc)) {
898 qc = ieee80211_get_qos_ctl(hdr);
899 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
900 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
901 IEEE80211_SCTL_SEQ) >> 4;
905 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb,
906 hw_queue, &tcb_desc);
907 if (!ieee80211_has_morefrags(hdr->frame_control)) {
909 mac->tids[tid].seq_number = seq_number;
911 if (ieee80211_is_data(fc))
912 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
915 static int rtl_usb_tx(struct ieee80211_hw *hw,
916 struct ieee80211_sta *sta,
918 struct rtl_tcb_desc *dummy)
920 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
921 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
922 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
923 __le16 fc = hdr->frame_control;
926 if (unlikely(is_hal_stop(rtlhal)))
928 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
929 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
930 _rtl_usb_transmit(hw, skb, hw_queue);
934 dev_kfree_skb_any(skb);
938 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
939 struct ieee80211_sta *sta,
945 static struct rtl_intf_ops rtl_usb_ops = {
946 .adapter_start = rtl_usb_start,
947 .adapter_stop = rtl_usb_stop,
948 .adapter_tx = rtl_usb_tx,
949 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
952 int rtl_usb_probe(struct usb_interface *intf,
953 const struct usb_device_id *id,
954 struct rtl_hal_cfg *rtl_hal_cfg)
957 struct ieee80211_hw *hw = NULL;
958 struct rtl_priv *rtlpriv = NULL;
959 struct usb_device *udev;
960 struct rtl_usb_priv *usb_priv;
962 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
963 sizeof(struct rtl_usb_priv), &rtl_ops);
965 RT_ASSERT(false, "ieee80211 alloc failed\n");
969 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
971 if (!rtlpriv->usb_data)
974 /* this spin lock must be initialized early */
975 spin_lock_init(&rtlpriv->locks.usb_lock);
977 rtlpriv->usb_data_index = 0;
978 init_completion(&rtlpriv->firmware_loading_complete);
979 SET_IEEE80211_DEV(hw, &intf->dev);
980 udev = interface_to_usbdev(intf);
982 usb_priv = rtl_usbpriv(hw);
983 memset(usb_priv, 0, sizeof(*usb_priv));
984 usb_priv->dev.intf = intf;
985 usb_priv->dev.udev = udev;
986 usb_set_intfdata(intf, hw);
987 /* init cfg & intf_ops */
988 rtlpriv->rtlhal.interface = INTF_USB;
989 rtlpriv->cfg = rtl_hal_cfg;
990 rtlpriv->intf_ops = &rtl_usb_ops;
991 rtl_dbgp_flag_init(hw);
992 /* Init IO handler */
993 _rtl_usb_io_handler_init(&udev->dev, hw);
994 rtlpriv->cfg->ops->read_chip_version(hw);
995 /*like read eeprom and so on */
996 rtlpriv->cfg->ops->read_eeprom_info(hw);
997 err = _rtl_usb_init(hw);
1000 rtl_usb_init_sw(hw);
1001 /* Init mac80211 sw */
1002 err = rtl_init_core(hw);
1004 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1005 "Can't allocate sw for mac80211\n");
1008 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1009 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1012 rtlpriv->cfg->ops->init_sw_leds(hw);
1016 rtl_deinit_core(hw);
1017 _rtl_usb_io_handler_release(hw);
1019 complete(&rtlpriv->firmware_loading_complete);
1022 EXPORT_SYMBOL(rtl_usb_probe);
1024 void rtl_usb_disconnect(struct usb_interface *intf)
1026 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1027 struct rtl_priv *rtlpriv = rtl_priv(hw);
1028 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1029 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1031 if (unlikely(!rtlpriv))
1034 /* just in case driver is removed before firmware callback */
1035 wait_for_completion(&rtlpriv->firmware_loading_complete);
1036 /*ieee80211_unregister_hw will call ops_stop */
1037 if (rtlmac->mac80211_registered == 1) {
1038 ieee80211_unregister_hw(hw);
1039 rtlmac->mac80211_registered = 0;
1041 rtl_deinit_deferred_work(hw);
1042 rtlpriv->intf_ops->adapter_stop(hw);
1045 /* rtl_deinit_rfkill(hw); */
1047 rtl_deinit_core(hw);
1048 kfree(rtlpriv->usb_data);
1049 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1050 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1051 _rtl_usb_io_handler_release(hw);
1052 usb_put_dev(rtlusb->udev);
1053 usb_set_intfdata(intf, NULL);
1054 ieee80211_free_hw(hw);
1056 EXPORT_SYMBOL(rtl_usb_disconnect);
1058 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1062 EXPORT_SYMBOL(rtl_usb_suspend);
1064 int rtl_usb_resume(struct usb_interface *pusb_intf)
1068 EXPORT_SYMBOL(rtl_usb_resume);