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)
49 static int _usbctrl_vendorreq_async_write(
struct usb_device *
udev,
u8 request,
58 struct rtl819x_async_write_data {
63 pipe = usb_sndctrlpipe(udev, 0);
85 usb_fill_control_urb(urb, udev, pipe,
86 (
unsigned char *)dr,
buf, len,
87 usbctrl_async_callback,
buf);
95 static int _usbctrl_vendorreq_sync_read(
struct usb_device *udev,
u8 request,
96 u16 value,
u16 index,
void *pdata,
102 int vendorreq_times = 0;
105 pipe = usb_rcvctrlpipe(udev, 0);
110 index, pdata, len, 0);
121 if (status < 0 && count++ < 4)
122 pr_err(
"reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
123 value, status, *(
u32 *)pdata);
130 struct usb_device *udev = to_usb_device(dev);
141 spin_unlock_irqrestore(&rtlpriv->
locks.usb_lock, flags);
146 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
150 static u8 _usb_read8_sync(
struct rtl_priv *rtlpriv,
u32 addr)
152 return (
u8)_usb_read_sync(rtlpriv, addr, 1);
157 return (
u16)_usb_read_sync(rtlpriv, addr, 2);
162 return _usb_read_sync(rtlpriv, addr, 4);
165 static void _usb_write_async(
struct usb_device *udev,
u32 addr,
u32 val,
175 wvalue = (
u16)(addr&0x0000ffff);
177 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
181 static void _usb_write8_async(
struct rtl_priv *rtlpriv,
u32 addr,
u8 val)
183 struct device *dev = rtlpriv->
io.dev;
185 _usb_write_async(to_usb_device(dev), addr, val, 1);
188 static void _usb_write16_async(
struct rtl_priv *rtlpriv,
u32 addr,
u16 val)
190 struct device *dev = rtlpriv->
io.dev;
192 _usb_write_async(to_usb_device(dev), addr, val, 2);
195 static void _usb_write32_async(
struct rtl_priv *rtlpriv,
u32 addr,
u32 val)
197 struct device *dev = rtlpriv->
io.dev;
199 _usb_write_async(to_usb_device(dev), addr, val, 4);
202 static void _usb_writeN_sync(
struct rtl_priv *rtlpriv,
u32 addr,
void *data,
205 struct device *dev = rtlpriv->
io.dev;
206 struct usb_device *udev = to_usb_device(dev);
211 int pipe = usb_sndctrlpipe(udev, 0);
215 wvalue = (
u16)(addr&0x0000ffff);
219 memcpy(buffer, data, len);
221 index, buffer, len, 50);
226 static void _rtl_usb_io_handler_init(
struct device *dev,
231 rtlpriv->
io.dev =
dev;
233 rtlpriv->
io.write8_async = _usb_write8_async;
234 rtlpriv->
io.write16_async = _usb_write16_async;
235 rtlpriv->
io.write32_async = _usb_write32_async;
236 rtlpriv->
io.read8_sync = _usb_read8_sync;
237 rtlpriv->
io.read16_sync = _usb_read16_sync;
238 rtlpriv->
io.read32_sync = _usb_read32_sync;
239 rtlpriv->
io.writeN_sync = _usb_writeN_sync;
259 #define IS_HIGH_SPEED_USB(udev) \
260 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
279 "Invalid endpoint map setting!\n");
285 rtlpriv->
cfg->usb_interface_cfg->usb_tx_post_hdl;
287 rtlpriv->
cfg->usb_interface_cfg->usb_tx_cleanup;
289 (rtlpriv->
cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
290 ? rtlpriv->
cfg->usb_interface_cfg->usb_tx_aggregate_hdl
291 : &_none_usb_tx_aggregate_hdl;
307 rtlusb->
rx_max_size = rtlpriv->
cfg->usb_interface_cfg->rx_max_size;
308 rtlusb->
rx_urb_num = rtlpriv->
cfg->usb_interface_cfg->rx_urb_num;
309 rtlusb->
in_ep = rtlpriv->
cfg->usb_interface_cfg->in_ep_num;
310 rtlusb->
usb_rx_hdl = rtlpriv->
cfg->usb_interface_cfg->usb_rx_hdl;
312 rtlpriv->
cfg->usb_interface_cfg->usb_rx_segregate_hdl;
314 pr_info(
"rx_max_size %d, rx_urb_num %d, in_ep %d\n",
328 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
331 for (epidx = 0; epidx < epnums; epidx++) {
333 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
335 if (usb_endpoint_dir_in(pep_desc))
337 else if (usb_endpoint_dir_out(pep_desc))
341 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
345 if (rtlusb->
in_ep_nums < rtlpriv->
cfg->usb_interface_cfg->in_ep_num) {
346 pr_err(
"Too few input end points found\n");
350 pr_err(
"No output end points found\n");
354 err = rtlpriv->
cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
356 _rtl_usb_init_tx(hw);
357 _rtl_usb_init_rx(hw);
398 #define __RADIO_TAP_SIZE_RSV 32
400 static void _rtl_rx_completed(
struct urb *urb);
414 "Failed to __dev_alloc_skb!!\n");
420 usb_fill_bulk_urb(urb, rtlusb->
udev,
421 usb_rcvbulkpipe(rtlusb->
udev, rtlusb->
in_ep),
422 skb->
data,
min(skb_tailroom(skb),
424 _rtl_rx_completed, skb);
426 _rtl_install_trx_info(rtlusb, skb, rtlusb->
in_ep);
430 #undef __RADIO_TAP_SIZE_RSV
432 static void _rtl_usb_rx_process_agg(
struct ieee80211_hw *hw,
438 bool unicast =
false;
448 rtlpriv->
cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
453 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status,
sizeof(rx_status));
455 if (is_broadcast_ether_addr(hdr->
addr1)) {
457 }
else if (is_multicast_ether_addr(hdr->
addr1)) {
461 rtlpriv->
stats.rxbytesunicast += skb->
len;
466 if (ieee80211_is_data(fc)) {
475 static void _rtl_usb_rx_process_noagg(
struct ieee80211_hw *hw,
481 bool unicast =
false;
491 rtlpriv->
cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
496 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status,
sizeof(rx_status));
498 if (is_broadcast_ether_addr(hdr->
addr1)) {
500 }
else if (is_multicast_ether_addr(hdr->
addr1)) {
504 rtlpriv->
stats.rxbytesunicast += skb->
len;
509 if (ieee80211_is_data(fc)) {
519 uskb = dev_alloc_skb(skb->
len + 128);
521 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
544 while (!skb_queue_empty(&
rx_queue)) {
546 _rtl_usb_rx_process_agg(hw, skb);
551 static void _rtl_rx_completed(
struct urb *_urb)
563 if (
likely(0 == _urb->status)) {
568 skb_put(skb, _urb->actual_length);
572 _rtl_usb_rx_process_noagg(hw, skb);
573 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb,
GFP_ATOMIC);
577 "Can't allocate skb for bulk IN!\n");
583 _rtl_rx_pre_process(hw, skb);
584 pr_err(
"rx agg not supported\n");
589 switch (_urb->status) {
601 skb_reset_tail_pointer(skb);
634 "Failed to alloc URB!!\n");
638 skb = _rtl_prep_rx_urb(hw, rtlusb, urb,
GFP_KERNEL);
641 "Failed to prep_rx_urb!!\n");
666 err = rtlpriv->
cfg->ops->hw_init(hw);
673 set_hal_start(rtlhal);
676 err = _rtl_usb_receive(hw);
703 txinfo = IEEE80211_SKB_CB(_skb);
704 ieee80211_tx_info_clear_status(txinfo);
730 set_hal_stop(rtlhal);
734 rtlpriv->
cfg->ops->hw_disable(hw);
737 static void _rtl_submit_tx_urb(
struct ieee80211_hw *hw,
struct urb *_urb)
749 "Failed to submit urb\n");
751 skb = (
struct sk_buff *)_urb->context;
757 static int _usb_tx_post(
struct ieee80211_hw *hw,
struct urb *urb,
766 txinfo = IEEE80211_SKB_CB(skb);
767 ieee80211_tx_info_clear_status(txinfo);
772 "Urb has error status 0x%X\n", urb->status);
781 static void _rtl_tx_complete(
struct urb *urb)
791 err = _usb_tx_post(hw, urb, skb);
798 static struct urb *_rtl_usb_tx_urb_setup(
struct ieee80211_hw *hw,
809 "Can't allocate URB for bulk out!\n");
813 _rtl_install_trx_info(rtlusb, skb, ep_num);
814 usb_fill_bulk_urb(_urb, rtlusb->
udev, usb_sndbulkpipe(rtlusb->
udev,
815 ep_num), skb->
data, skb->
len, _rtl_tx_complete, skb);
816 _urb->transfer_flags |= URB_ZERO_PACKET;
826 struct urb *_urb =
NULL;
834 "USB device is stopping...\n");
838 ep_num = rtlusb->
ep_map.ep_mapping[qnum];
841 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
844 "Can't allocate urb. Drop skb!\n");
848 _rtl_submit_tx_urb(hw, _urb);
851 static void _rtl_usb_tx_preprocess(
struct ieee80211_hw *hw,
870 if (ieee80211_is_auth(fc)) {
875 if (rtlpriv->
psc.sw_ps_enabled) {
876 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
877 !ieee80211_has_pm(fc))
882 if (is_multicast_ether_addr(pda_addr))
883 rtlpriv->
stats.txbytesmulticast += skb->
len;
884 else if (is_broadcast_ether_addr(pda_addr))
885 rtlpriv->
stats.txbytesbroadcast += skb->
len;
887 rtlpriv->
stats.txbytesunicast += skb->
len;
888 if (ieee80211_is_data_qos(fc)) {
889 qc = ieee80211_get_qos_ctl(hdr);
896 rtlpriv->
cfg->ops->fill_tx_desc(hw, hdr, (
u8 *)pdesc, info, sta, skb,
897 hw_queue, &tcb_desc);
900 mac->
tids[tid].seq_number = seq_number;
902 if (ieee80211_is_data(fc))
919 hw_queue = rtlusb->
usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
920 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
921 _rtl_usb_transmit(hw, skb, hw_queue);
929 static bool rtl_usb_tx_chk_waitq_insert(
struct ieee80211_hw *hw,
937 .adapter_start = rtl_usb_start,
938 .adapter_stop = rtl_usb_stop,
939 .adapter_tx = rtl_usb_tx,
940 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
949 struct usb_device *
udev;
955 RT_ASSERT(
false,
"ieee80211 alloc failed\n");
969 SET_IEEE80211_DEV(hw, &intf->dev);
970 udev = interface_to_usbdev(intf);
973 memset(usb_priv, 0,
sizeof(*usb_priv));
976 usb_set_intfdata(intf, hw);
983 _rtl_usb_io_handler_init(&udev->dev, hw);
984 rtlpriv->
cfg->ops->read_chip_version(hw);
986 rtlpriv->
cfg->ops->read_eeprom_info(hw);
987 err = _rtl_usb_init(hw);
995 "Can't allocate sw for mac80211\n");
998 if (rtlpriv->
cfg->ops->init_sw_vars(hw)) {
1002 rtlpriv->
cfg->ops->init_sw_leds(hw);
1007 _rtl_usb_io_handler_release(hw);
1032 rtlpriv->
intf_ops->adapter_stop(hw);
1039 rtlpriv->
cfg->ops->deinit_sw_leds(hw);
1040 rtlpriv->
cfg->ops->deinit_sw_vars(hw);
1041 _rtl_usb_io_handler_release(hw);
1043 usb_set_intfdata(intf,
NULL);