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>
35 #include <linux/module.h>
37 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
38 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
39 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
40 MODULE_LICENSE("GPL");
41 MODULE_DESCRIPTION("USB basic driver for rtlwifi");
43 #define REALTEK_USB_VENQT_READ 0xC0
44 #define REALTEK_USB_VENQT_WRITE 0x40
45 #define REALTEK_USB_VENQT_CMD_REQ 0x05
46 #define REALTEK_USB_VENQT_CMD_IDX 0x00
48 #define MAX_USBCTRL_VENDORREQ_TIMES 10
50 static void usbctrl_async_callback(struct urb
*urb
)
54 kfree(urb
->setup_packet
);
56 kfree(urb
->transfer_buffer
);
60 static int _usbctrl_vendorreq_async_write(struct usb_device
*udev
, u8 request
,
61 u16 value
, u16 index
, void *pdata
,
67 struct usb_ctrlrequest
*dr
;
69 const u16 databuf_maxlen
= REALTEK_USB_VENQT_MAX_BUF_SIZE
;
72 if (WARN_ON_ONCE(len
> databuf_maxlen
))
75 pipe
= usb_sndctrlpipe(udev
, 0); /* write_out */
76 reqtype
= REALTEK_USB_VENQT_WRITE
;
78 dr
= kmalloc(sizeof(*dr
), GFP_ATOMIC
);
82 databuf
= kmalloc(databuf_maxlen
, GFP_ATOMIC
);
88 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
95 dr
->bRequestType
= reqtype
;
96 dr
->bRequest
= request
;
97 dr
->wValue
= cpu_to_le16(value
);
98 dr
->wIndex
= cpu_to_le16(index
);
99 dr
->wLength
= cpu_to_le16(len
);
100 /* data are already in little-endian order */
101 memcpy(databuf
, pdata
, len
);
102 usb_fill_control_urb(urb
, udev
, pipe
,
103 (unsigned char *)dr
, databuf
, len
,
104 usbctrl_async_callback
, NULL
);
105 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
114 static int _usbctrl_vendorreq_sync_read(struct usb_device
*udev
, u8 request
,
115 u16 value
, u16 index
, void *pdata
,
121 int vendorreq_times
= 0;
124 pipe
= usb_rcvctrlpipe(udev
, 0); /* read_in */
125 reqtype
= REALTEK_USB_VENQT_READ
;
128 status
= usb_control_msg(udev
, pipe
, request
, reqtype
, value
,
129 index
, pdata
, len
, 1000);
131 /* firmware download is checksumed, don't retry */
132 if ((value
>= FW_8192C_START_ADDRESS
&&
133 value
<= FW_8192C_END_ADDRESS
))
138 } while (++vendorreq_times
< MAX_USBCTRL_VENDORREQ_TIMES
);
140 if (status
< 0 && count
++ < 4)
141 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
142 value
, status
, *(u32
*)pdata
);
146 static u32
_usb_read_sync(struct rtl_priv
*rtlpriv
, u32 addr
, u16 len
)
148 struct device
*dev
= rtlpriv
->io
.dev
;
149 struct usb_device
*udev
= to_usb_device(dev
);
156 spin_lock_irqsave(&rtlpriv
->locks
.usb_lock
, flags
);
157 if (++rtlpriv
->usb_data_index
>= RTL_USB_MAX_RX_COUNT
)
158 rtlpriv
->usb_data_index
= 0;
159 data
= &rtlpriv
->usb_data
[rtlpriv
->usb_data_index
];
160 spin_unlock_irqrestore(&rtlpriv
->locks
.usb_lock
, flags
);
161 request
= REALTEK_USB_VENQT_CMD_REQ
;
162 index
= REALTEK_USB_VENQT_CMD_IDX
; /* n/a */
165 _usbctrl_vendorreq_sync_read(udev
, request
, wvalue
, index
, data
, len
);
166 return le32_to_cpu(*data
);
169 static u8
_usb_read8_sync(struct rtl_priv
*rtlpriv
, u32 addr
)
171 return (u8
)_usb_read_sync(rtlpriv
, addr
, 1);
174 static u16
_usb_read16_sync(struct rtl_priv
*rtlpriv
, u32 addr
)
176 return (u16
)_usb_read_sync(rtlpriv
, addr
, 2);
179 static u32
_usb_read32_sync(struct rtl_priv
*rtlpriv
, u32 addr
)
181 return _usb_read_sync(rtlpriv
, addr
, 4);
184 static void _usb_write_async(struct usb_device
*udev
, u32 addr
, u32 val
,
192 request
= REALTEK_USB_VENQT_CMD_REQ
;
193 index
= REALTEK_USB_VENQT_CMD_IDX
; /* n/a */
194 wvalue
= (u16
)(addr
&0x0000ffff);
195 data
= cpu_to_le32(val
);
196 _usbctrl_vendorreq_async_write(udev
, request
, wvalue
, index
, &data
,
200 static void _usb_write8_async(struct rtl_priv
*rtlpriv
, u32 addr
, u8 val
)
202 struct device
*dev
= rtlpriv
->io
.dev
;
204 _usb_write_async(to_usb_device(dev
), addr
, val
, 1);
207 static void _usb_write16_async(struct rtl_priv
*rtlpriv
, u32 addr
, u16 val
)
209 struct device
*dev
= rtlpriv
->io
.dev
;
211 _usb_write_async(to_usb_device(dev
), addr
, val
, 2);
214 static void _usb_write32_async(struct rtl_priv
*rtlpriv
, u32 addr
, u32 val
)
216 struct device
*dev
= rtlpriv
->io
.dev
;
218 _usb_write_async(to_usb_device(dev
), addr
, val
, 4);
221 static void _usb_writeN_sync(struct rtl_priv
*rtlpriv
, u32 addr
, void *data
,
224 struct device
*dev
= rtlpriv
->io
.dev
;
225 struct usb_device
*udev
= to_usb_device(dev
);
226 u8 request
= REALTEK_USB_VENQT_CMD_REQ
;
227 u8 reqtype
= REALTEK_USB_VENQT_WRITE
;
229 u16 index
= REALTEK_USB_VENQT_CMD_IDX
;
230 int pipe
= usb_sndctrlpipe(udev
, 0); /* write_out */
233 wvalue
= (u16
)(addr
& 0x0000ffff);
234 buffer
= kmemdup(data
, len
, GFP_ATOMIC
);
237 usb_control_msg(udev
, pipe
, request
, reqtype
, wvalue
,
238 index
, buffer
, len
, 50);
243 static void _rtl_usb_io_handler_init(struct device
*dev
,
244 struct ieee80211_hw
*hw
)
246 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
248 rtlpriv
->io
.dev
= dev
;
249 mutex_init(&rtlpriv
->io
.bb_mutex
);
250 rtlpriv
->io
.write8_async
= _usb_write8_async
;
251 rtlpriv
->io
.write16_async
= _usb_write16_async
;
252 rtlpriv
->io
.write32_async
= _usb_write32_async
;
253 rtlpriv
->io
.read8_sync
= _usb_read8_sync
;
254 rtlpriv
->io
.read16_sync
= _usb_read16_sync
;
255 rtlpriv
->io
.read32_sync
= _usb_read32_sync
;
256 rtlpriv
->io
.writeN_sync
= _usb_writeN_sync
;
259 static void _rtl_usb_io_handler_release(struct ieee80211_hw
*hw
)
261 struct rtl_priv __maybe_unused
*rtlpriv
= rtl_priv(hw
);
263 mutex_destroy(&rtlpriv
->io
.bb_mutex
);
268 * Default aggregation handler. Do nothing and just return the oldest skb.
270 static struct sk_buff
*_none_usb_tx_aggregate_hdl(struct ieee80211_hw
*hw
,
271 struct sk_buff_head
*list
)
273 return skb_dequeue(list
);
276 #define IS_HIGH_SPEED_USB(udev) \
277 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
279 static int _rtl_usb_init_tx(struct ieee80211_hw
*hw
)
282 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
283 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
285 rtlusb
->max_bulk_out_size
= IS_HIGH_SPEED_USB(rtlusb
->udev
)
286 ? USB_HIGH_SPEED_BULK_SIZE
287 : USB_FULL_SPEED_BULK_SIZE
;
289 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
, "USB Max Bulk-out Size=%d\n",
290 rtlusb
->max_bulk_out_size
);
292 for (i
= 0; i
< __RTL_TXQ_NUM
; i
++) {
293 u32 ep_num
= rtlusb
->ep_map
.ep_mapping
[i
];
295 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
,
296 "Invalid endpoint map setting!\n");
301 rtlusb
->usb_tx_post_hdl
=
302 rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_post_hdl
;
303 rtlusb
->usb_tx_cleanup
=
304 rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_cleanup
;
305 rtlusb
->usb_tx_aggregate_hdl
=
306 (rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_aggregate_hdl
)
307 ? rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_aggregate_hdl
308 : &_none_usb_tx_aggregate_hdl
;
310 init_usb_anchor(&rtlusb
->tx_submitted
);
311 for (i
= 0; i
< RTL_USB_MAX_EP_NUM
; i
++) {
312 skb_queue_head_init(&rtlusb
->tx_skb_queue
[i
]);
313 init_usb_anchor(&rtlusb
->tx_pending
[i
]);
318 static void _rtl_rx_work(unsigned long param
);
320 static int _rtl_usb_init_rx(struct ieee80211_hw
*hw
)
322 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
323 struct rtl_usb_priv
*usb_priv
= rtl_usbpriv(hw
);
324 struct rtl_usb
*rtlusb
= rtl_usbdev(usb_priv
);
326 rtlusb
->rx_max_size
= rtlpriv
->cfg
->usb_interface_cfg
->rx_max_size
;
327 rtlusb
->rx_urb_num
= rtlpriv
->cfg
->usb_interface_cfg
->rx_urb_num
;
328 rtlusb
->in_ep
= rtlpriv
->cfg
->usb_interface_cfg
->in_ep_num
;
329 rtlusb
->usb_rx_hdl
= rtlpriv
->cfg
->usb_interface_cfg
->usb_rx_hdl
;
330 rtlusb
->usb_rx_segregate_hdl
=
331 rtlpriv
->cfg
->usb_interface_cfg
->usb_rx_segregate_hdl
;
333 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
334 rtlusb
->rx_max_size
, rtlusb
->rx_urb_num
, rtlusb
->in_ep
);
335 init_usb_anchor(&rtlusb
->rx_submitted
);
336 init_usb_anchor(&rtlusb
->rx_cleanup_urbs
);
338 skb_queue_head_init(&rtlusb
->rx_queue
);
339 rtlusb
->rx_work_tasklet
.func
= _rtl_rx_work
;
340 rtlusb
->rx_work_tasklet
.data
= (unsigned long)rtlusb
;
345 static int _rtl_usb_init(struct ieee80211_hw
*hw
)
347 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
348 struct rtl_usb_priv
*usb_priv
= rtl_usbpriv(hw
);
349 struct rtl_usb
*rtlusb
= rtl_usbdev(usb_priv
);
352 struct usb_interface
*usb_intf
= rtlusb
->intf
;
353 u8 epnums
= usb_intf
->cur_altsetting
->desc
.bNumEndpoints
;
355 rtlusb
->out_ep_nums
= rtlusb
->in_ep_nums
= 0;
356 for (epidx
= 0; epidx
< epnums
; epidx
++) {
357 struct usb_endpoint_descriptor
*pep_desc
;
358 pep_desc
= &usb_intf
->cur_altsetting
->endpoint
[epidx
].desc
;
360 if (usb_endpoint_dir_in(pep_desc
))
361 rtlusb
->in_ep_nums
++;
362 else if (usb_endpoint_dir_out(pep_desc
))
363 rtlusb
->out_ep_nums
++;
365 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
,
366 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
367 pep_desc
->bEndpointAddress
, pep_desc
->wMaxPacketSize
,
368 pep_desc
->bInterval
);
370 if (rtlusb
->in_ep_nums
< rtlpriv
->cfg
->usb_interface_cfg
->in_ep_num
) {
371 pr_err("Too few input end points found\n");
374 if (rtlusb
->out_ep_nums
== 0) {
375 pr_err("No output end points found\n");
378 /* usb endpoint mapping */
379 err
= rtlpriv
->cfg
->usb_interface_cfg
->usb_endpoint_mapping(hw
);
380 rtlusb
->usb_mq_to_hwq
= rtlpriv
->cfg
->usb_interface_cfg
->usb_mq_to_hwq
;
381 _rtl_usb_init_tx(hw
);
382 _rtl_usb_init_rx(hw
);
386 static void rtl_usb_init_sw(struct ieee80211_hw
*hw
)
388 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
389 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
390 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
391 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
394 ppsc
->inactiveps
= false;
395 ppsc
->leisure_ps
= false;
396 ppsc
->fwctrl_lps
= false;
397 ppsc
->reg_fwctrl_lps
= 3;
398 ppsc
->reg_max_lps_awakeintvl
= 5;
399 ppsc
->fwctrl_psmode
= FW_PS_DTIM_MODE
;
402 mac
->beacon_interval
= 100;
405 mac
->min_space_cfg
= 0;
406 mac
->max_mss_density
= 0;
408 /* set sane AMPDU defaults */
409 mac
->current_ampdu_density
= 7;
410 mac
->current_ampdu_factor
= 3;
413 rtlusb
->acm_method
= eAcmWay2_SW
;
416 /* HIMR - turn all on */
417 rtlusb
->irq_mask
[0] = 0xFFFFFFFF;
418 /* HIMR_EX - turn all on */
419 rtlusb
->irq_mask
[1] = 0xFFFFFFFF;
420 rtlusb
->disableHWSM
= true;
423 static void _rtl_rx_completed(struct urb
*urb
);
425 static int _rtl_prep_rx_urb(struct ieee80211_hw
*hw
, struct rtl_usb
*rtlusb
,
426 struct urb
*urb
, gfp_t gfp_mask
)
428 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
431 buf
= usb_alloc_coherent(rtlusb
->udev
, rtlusb
->rx_max_size
, gfp_mask
,
434 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
435 "Failed to usb_alloc_coherent!!\n");
439 usb_fill_bulk_urb(urb
, rtlusb
->udev
,
440 usb_rcvbulkpipe(rtlusb
->udev
, rtlusb
->in_ep
),
441 buf
, rtlusb
->rx_max_size
, _rtl_rx_completed
, rtlusb
);
442 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
447 static void _rtl_usb_rx_process_agg(struct ieee80211_hw
*hw
,
450 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
451 u8
*rxdesc
= skb
->data
;
452 struct ieee80211_hdr
*hdr
;
453 bool unicast
= false;
455 struct ieee80211_rx_status rx_status
= {0};
456 struct rtl_stats stats
= {
462 skb_pull(skb
, RTL_RX_DESC_SIZE
);
463 rtlpriv
->cfg
->ops
->query_rx_desc(hw
, &stats
, &rx_status
, rxdesc
, skb
);
464 skb_pull(skb
, (stats
.rx_drvinfo_size
+ stats
.rx_bufshift
));
465 hdr
= (struct ieee80211_hdr
*)(skb
->data
);
466 fc
= hdr
->frame_control
;
468 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
470 if (is_broadcast_ether_addr(hdr
->addr1
)) {
472 } else if (is_multicast_ether_addr(hdr
->addr1
)) {
476 rtlpriv
->stats
.rxbytesunicast
+= skb
->len
;
479 rtl_is_special_data(hw
, skb
, false);
481 if (ieee80211_is_data(fc
)) {
482 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_RX
);
485 rtlpriv
->link_info
.num_rx_inperiod
++;
487 /* static bcn for roaming */
488 rtl_beacon_statistic(hw
, skb
);
492 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw
*hw
,
495 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
496 u8
*rxdesc
= skb
->data
;
497 struct ieee80211_hdr
*hdr
;
498 bool unicast
= false;
500 struct ieee80211_rx_status rx_status
= {0};
501 struct rtl_stats stats
= {
507 skb_pull(skb
, RTL_RX_DESC_SIZE
);
508 rtlpriv
->cfg
->ops
->query_rx_desc(hw
, &stats
, &rx_status
, rxdesc
, skb
);
509 skb_pull(skb
, (stats
.rx_drvinfo_size
+ stats
.rx_bufshift
));
510 hdr
= (struct ieee80211_hdr
*)(skb
->data
);
511 fc
= hdr
->frame_control
;
513 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
515 if (is_broadcast_ether_addr(hdr
->addr1
)) {
517 } else if (is_multicast_ether_addr(hdr
->addr1
)) {
521 rtlpriv
->stats
.rxbytesunicast
+= skb
->len
;
524 rtl_is_special_data(hw
, skb
, false);
526 if (ieee80211_is_data(fc
)) {
527 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_RX
);
530 rtlpriv
->link_info
.num_rx_inperiod
++;
533 /* static bcn for roaming */
534 rtl_beacon_statistic(hw
, skb
);
536 if (likely(rtl_action_proc(hw
, skb
, false)))
537 ieee80211_rx(hw
, skb
);
539 dev_kfree_skb_any(skb
);
543 static void _rtl_rx_pre_process(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
545 struct sk_buff
*_skb
;
546 struct sk_buff_head rx_queue
;
547 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
549 skb_queue_head_init(&rx_queue
);
550 if (rtlusb
->usb_rx_segregate_hdl
)
551 rtlusb
->usb_rx_segregate_hdl(hw
, skb
, &rx_queue
);
552 WARN_ON(skb_queue_empty(&rx_queue
));
553 while (!skb_queue_empty(&rx_queue
)) {
554 _skb
= skb_dequeue(&rx_queue
);
555 _rtl_usb_rx_process_agg(hw
, _skb
);
556 ieee80211_rx(hw
, _skb
);
560 #define __RX_SKB_MAX_QUEUED 64
562 static void _rtl_rx_work(unsigned long param
)
564 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)param
;
565 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
568 while ((skb
= skb_dequeue(&rtlusb
->rx_queue
))) {
569 if (unlikely(IS_USB_STOP(rtlusb
))) {
570 dev_kfree_skb_any(skb
);
574 if (likely(!rtlusb
->usb_rx_segregate_hdl
)) {
575 _rtl_usb_rx_process_noagg(hw
, skb
);
578 _rtl_rx_pre_process(hw
, skb
);
579 pr_err("rx agg not supported\n");
584 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr
*hdr
,
587 unsigned int padding
= 0;
589 /* make function no-op when possible */
590 if (NET_IP_ALIGN
== 0 || len
< sizeof(*hdr
))
593 /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
594 /* TODO: deduplicate common code, define helper function instead? */
596 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
597 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
599 padding
^= NET_IP_ALIGN
;
601 /* Input might be invalid, avoid accessing memory outside
604 if ((unsigned long)qc
- (unsigned long)hdr
< len
&&
605 *qc
& IEEE80211_QOS_CTL_A_MSDU_PRESENT
)
606 padding
^= NET_IP_ALIGN
;
609 if (ieee80211_has_a4(hdr
->frame_control
))
610 padding
^= NET_IP_ALIGN
;
615 #define __RADIO_TAP_SIZE_RSV 32
617 static void _rtl_rx_completed(struct urb
*_urb
)
619 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)_urb
->context
;
620 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
621 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
624 if (unlikely(IS_USB_STOP(rtlusb
)))
627 if (likely(0 == _urb
->status
)) {
628 unsigned int padding
;
631 unsigned int size
= _urb
->actual_length
;
632 struct ieee80211_hdr
*hdr
;
634 if (size
< RTL_RX_DESC_SIZE
+ sizeof(struct ieee80211_hdr
)) {
635 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
636 "Too short packet from bulk IN! (len: %d)\n",
641 qlen
= skb_queue_len(&rtlusb
->rx_queue
);
642 if (qlen
>= __RX_SKB_MAX_QUEUED
) {
643 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
644 "Pending RX skbuff queue full! (qlen: %d)\n",
649 hdr
= (void *)(_urb
->transfer_buffer
+ RTL_RX_DESC_SIZE
);
650 padding
= _rtl_rx_get_padding(hdr
, size
- RTL_RX_DESC_SIZE
);
652 skb
= dev_alloc_skb(size
+ __RADIO_TAP_SIZE_RSV
+ padding
);
654 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
655 "Can't allocate skb for bulk IN!\n");
659 _rtl_install_trx_info(rtlusb
, skb
, rtlusb
->in_ep
);
661 /* Make sure the payload data is 4 byte aligned. */
662 skb_reserve(skb
, padding
);
664 /* reserve some space for mac80211's radiotap */
665 skb_reserve(skb
, __RADIO_TAP_SIZE_RSV
);
667 memcpy(skb_put(skb
, size
), _urb
->transfer_buffer
, size
);
669 skb_queue_tail(&rtlusb
->rx_queue
, skb
);
670 tasklet_schedule(&rtlusb
->rx_work_tasklet
);
675 switch (_urb
->status
) {
687 usb_anchor_urb(_urb
, &rtlusb
->rx_submitted
);
688 err
= usb_submit_urb(_urb
, GFP_ATOMIC
);
690 usb_unanchor_urb(_urb
);
696 /* On some architectures, usb_free_coherent must not be called from
697 * hardirq context. Queue urb to cleanup list.
699 usb_anchor_urb(_urb
, &rtlusb
->rx_cleanup_urbs
);
702 #undef __RADIO_TAP_SIZE_RSV
704 static void _rtl_usb_cleanup_rx(struct ieee80211_hw
*hw
)
706 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
709 usb_kill_anchored_urbs(&rtlusb
->rx_submitted
);
711 tasklet_kill(&rtlusb
->rx_work_tasklet
);
712 skb_queue_purge(&rtlusb
->rx_queue
);
714 while ((urb
= usb_get_from_anchor(&rtlusb
->rx_cleanup_urbs
))) {
715 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
716 urb
->transfer_buffer
, urb
->transfer_dma
);
721 static int _rtl_usb_receive(struct ieee80211_hw
*hw
)
726 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
727 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
729 WARN_ON(0 == rtlusb
->rx_urb_num
);
730 /* 1600 == 1514 + max WLAN header + rtk info */
731 WARN_ON(rtlusb
->rx_max_size
< 1600);
733 for (i
= 0; i
< rtlusb
->rx_urb_num
; i
++) {
735 urb
= usb_alloc_urb(0, GFP_KERNEL
);
737 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
738 "Failed to alloc URB!!\n");
742 err
= _rtl_prep_rx_urb(hw
, rtlusb
, urb
, GFP_KERNEL
);
744 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
745 "Failed to prep_rx_urb!!\n");
750 usb_anchor_urb(urb
, &rtlusb
->rx_submitted
);
751 err
= usb_submit_urb(urb
, GFP_KERNEL
);
759 usb_kill_anchored_urbs(&rtlusb
->rx_submitted
);
760 _rtl_usb_cleanup_rx(hw
);
764 static int rtl_usb_start(struct ieee80211_hw
*hw
)
767 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
768 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
769 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
771 err
= rtlpriv
->cfg
->ops
->hw_init(hw
);
773 rtl_init_rx_config(hw
);
775 /* Enable software */
776 SET_USB_START(rtlusb
);
777 /* should after adapter start and interrupt enable. */
778 set_hal_start(rtlhal
);
781 err
= _rtl_usb_receive(hw
);
791 /*======================= tx =========================================*/
792 static void rtl_usb_cleanup(struct ieee80211_hw
*hw
)
795 struct sk_buff
*_skb
;
796 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
797 struct ieee80211_tx_info
*txinfo
;
799 SET_USB_STOP(rtlusb
);
801 /* clean up rx stuff. */
802 _rtl_usb_cleanup_rx(hw
);
804 /* clean up tx stuff */
805 for (i
= 0; i
< RTL_USB_MAX_EP_NUM
; i
++) {
806 while ((_skb
= skb_dequeue(&rtlusb
->tx_skb_queue
[i
]))) {
807 rtlusb
->usb_tx_cleanup(hw
, _skb
);
808 txinfo
= IEEE80211_SKB_CB(_skb
);
809 ieee80211_tx_info_clear_status(txinfo
);
810 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
811 ieee80211_tx_status_irqsafe(hw
, _skb
);
813 usb_kill_anchored_urbs(&rtlusb
->tx_pending
[i
]);
815 usb_kill_anchored_urbs(&rtlusb
->tx_submitted
);
820 * We may add some struct into struct rtl_usb later. Do deinit here.
823 static void rtl_usb_deinit(struct ieee80211_hw
*hw
)
828 static void rtl_usb_stop(struct ieee80211_hw
*hw
)
830 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
831 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
832 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
834 /* should after adapter start and interrupt enable. */
835 set_hal_stop(rtlhal
);
836 cancel_work_sync(&rtlpriv
->works
.fill_h2c_cmd
);
837 /* Enable software */
838 SET_USB_STOP(rtlusb
);
840 rtlpriv
->cfg
->ops
->hw_disable(hw
);
843 static void _rtl_submit_tx_urb(struct ieee80211_hw
*hw
, struct urb
*_urb
)
846 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
847 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
849 usb_anchor_urb(_urb
, &rtlusb
->tx_submitted
);
850 err
= usb_submit_urb(_urb
, GFP_ATOMIC
);
854 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
855 "Failed to submit urb\n");
856 usb_unanchor_urb(_urb
);
857 skb
= (struct sk_buff
*)_urb
->context
;
863 static int _usb_tx_post(struct ieee80211_hw
*hw
, struct urb
*urb
,
866 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
867 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
868 struct ieee80211_tx_info
*txinfo
;
870 rtlusb
->usb_tx_post_hdl(hw
, urb
, skb
);
871 skb_pull(skb
, RTL_TX_HEADER_SIZE
);
872 txinfo
= IEEE80211_SKB_CB(skb
);
873 ieee80211_tx_info_clear_status(txinfo
);
874 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
877 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
878 "Urb has error status 0x%X\n", urb
->status
);
881 /* TODO: statistics */
883 ieee80211_tx_status_irqsafe(hw
, skb
);
887 static void _rtl_tx_complete(struct urb
*urb
)
889 struct sk_buff
*skb
= (struct sk_buff
*)urb
->context
;
890 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
891 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)info
->rate_driver_data
[0];
892 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
895 if (unlikely(IS_USB_STOP(rtlusb
)))
897 err
= _usb_tx_post(hw
, urb
, skb
);
899 /* Ignore error and keep issuiing other urbs */
904 static struct urb
*_rtl_usb_tx_urb_setup(struct ieee80211_hw
*hw
,
905 struct sk_buff
*skb
, u32 ep_num
)
907 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
908 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
911 WARN_ON(NULL
== skb
);
912 _urb
= usb_alloc_urb(0, GFP_ATOMIC
);
914 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
915 "Can't allocate URB for bulk out!\n");
919 _rtl_install_trx_info(rtlusb
, skb
, ep_num
);
920 usb_fill_bulk_urb(_urb
, rtlusb
->udev
, usb_sndbulkpipe(rtlusb
->udev
,
921 ep_num
), skb
->data
, skb
->len
, _rtl_tx_complete
, skb
);
922 _urb
->transfer_flags
|= URB_ZERO_PACKET
;
926 static void _rtl_usb_transmit(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
929 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
930 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
932 struct urb
*_urb
= NULL
;
933 struct sk_buff
*_skb
= NULL
;
935 WARN_ON(NULL
== rtlusb
->usb_tx_aggregate_hdl
);
936 if (unlikely(IS_USB_STOP(rtlusb
))) {
937 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
938 "USB device is stopping...\n");
942 ep_num
= rtlusb
->ep_map
.ep_mapping
[qnum
];
944 _urb
= _rtl_usb_tx_urb_setup(hw
, _skb
, ep_num
);
945 if (unlikely(!_urb
)) {
946 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
,
947 "Can't allocate urb. Drop skb!\n");
951 _rtl_submit_tx_urb(hw
, _urb
);
954 static void _rtl_usb_tx_preprocess(struct ieee80211_hw
*hw
,
955 struct ieee80211_sta
*sta
,
959 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
960 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
961 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
962 struct rtl_tx_desc
*pdesc
= NULL
;
963 struct rtl_tcb_desc tcb_desc
;
964 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)(skb
->data
);
965 __le16 fc
= hdr
->frame_control
;
966 u8
*pda_addr
= hdr
->addr1
;
972 memset(&tcb_desc
, 0, sizeof(struct rtl_tcb_desc
));
973 if (ieee80211_is_auth(fc
)) {
974 RT_TRACE(rtlpriv
, COMP_SEND
, DBG_DMESG
, "MAC80211_LINKING\n");
978 if (rtlpriv
->psc
.sw_ps_enabled
) {
979 if (ieee80211_is_data(fc
) && !ieee80211_is_nullfunc(fc
) &&
980 !ieee80211_has_pm(fc
))
981 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
984 rtl_action_proc(hw
, skb
, true);
985 if (is_multicast_ether_addr(pda_addr
))
986 rtlpriv
->stats
.txbytesmulticast
+= skb
->len
;
987 else if (is_broadcast_ether_addr(pda_addr
))
988 rtlpriv
->stats
.txbytesbroadcast
+= skb
->len
;
990 rtlpriv
->stats
.txbytesunicast
+= skb
->len
;
991 if (ieee80211_is_data_qos(fc
)) {
992 qc
= ieee80211_get_qos_ctl(hdr
);
993 tid
= qc
[0] & IEEE80211_QOS_CTL_TID_MASK
;
994 seq_number
= (le16_to_cpu(hdr
->seq_ctrl
) &
995 IEEE80211_SCTL_SEQ
) >> 4;
999 rtlpriv
->cfg
->ops
->fill_tx_desc(hw
, hdr
, (u8
*)pdesc
, info
, sta
, skb
,
1000 hw_queue
, &tcb_desc
);
1001 if (!ieee80211_has_morefrags(hdr
->frame_control
)) {
1003 mac
->tids
[tid
].seq_number
= seq_number
;
1005 if (ieee80211_is_data(fc
))
1006 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_TX
);
1009 static int rtl_usb_tx(struct ieee80211_hw
*hw
,
1010 struct ieee80211_sta
*sta
,
1011 struct sk_buff
*skb
,
1012 struct rtl_tcb_desc
*dummy
)
1014 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
1015 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
1016 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)(skb
->data
);
1017 __le16 fc
= hdr
->frame_control
;
1020 if (unlikely(is_hal_stop(rtlhal
)))
1022 hw_queue
= rtlusb
->usb_mq_to_hwq(fc
, skb_get_queue_mapping(skb
));
1023 _rtl_usb_tx_preprocess(hw
, sta
, skb
, hw_queue
);
1024 _rtl_usb_transmit(hw
, skb
, hw_queue
);
1025 return NETDEV_TX_OK
;
1028 dev_kfree_skb_any(skb
);
1029 return NETDEV_TX_OK
;
1032 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw
*hw
,
1033 struct ieee80211_sta
*sta
,
1034 struct sk_buff
*skb
)
1039 static void rtl_fill_h2c_cmd_work_callback(struct work_struct
*work
)
1041 struct rtl_works
*rtlworks
=
1042 container_of(work
, struct rtl_works
, fill_h2c_cmd
);
1043 struct ieee80211_hw
*hw
= rtlworks
->hw
;
1044 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1046 rtlpriv
->cfg
->ops
->fill_h2c_cmd(hw
, H2C_RA_MASK
, 5, rtlpriv
->rate_mask
);
1049 static struct rtl_intf_ops rtl_usb_ops
= {
1050 .adapter_start
= rtl_usb_start
,
1051 .adapter_stop
= rtl_usb_stop
,
1052 .adapter_tx
= rtl_usb_tx
,
1053 .waitq_insert
= rtl_usb_tx_chk_waitq_insert
,
1056 int rtl_usb_probe(struct usb_interface
*intf
,
1057 const struct usb_device_id
*id
,
1058 struct rtl_hal_cfg
*rtl_hal_cfg
)
1061 struct ieee80211_hw
*hw
= NULL
;
1062 struct rtl_priv
*rtlpriv
= NULL
;
1063 struct usb_device
*udev
;
1064 struct rtl_usb_priv
*usb_priv
;
1066 hw
= ieee80211_alloc_hw(sizeof(struct rtl_priv
) +
1067 sizeof(struct rtl_usb_priv
), &rtl_ops
);
1069 RT_ASSERT(false, "ieee80211 alloc failed\n");
1073 rtlpriv
->usb_data
= kzalloc(RTL_USB_MAX_RX_COUNT
* sizeof(u32
),
1075 if (!rtlpriv
->usb_data
)
1078 /* this spin lock must be initialized early */
1079 spin_lock_init(&rtlpriv
->locks
.usb_lock
);
1080 INIT_WORK(&rtlpriv
->works
.fill_h2c_cmd
,
1081 rtl_fill_h2c_cmd_work_callback
);
1082 INIT_WORK(&rtlpriv
->works
.lps_change_work
,
1083 rtl_lps_change_work_callback
);
1085 rtlpriv
->usb_data_index
= 0;
1086 init_completion(&rtlpriv
->firmware_loading_complete
);
1087 SET_IEEE80211_DEV(hw
, &intf
->dev
);
1088 udev
= interface_to_usbdev(intf
);
1090 usb_priv
= rtl_usbpriv(hw
);
1091 memset(usb_priv
, 0, sizeof(*usb_priv
));
1092 usb_priv
->dev
.intf
= intf
;
1093 usb_priv
->dev
.udev
= udev
;
1094 usb_set_intfdata(intf
, hw
);
1095 /* init cfg & intf_ops */
1096 rtlpriv
->rtlhal
.interface
= INTF_USB
;
1097 rtlpriv
->cfg
= rtl_hal_cfg
;
1098 rtlpriv
->intf_ops
= &rtl_usb_ops
;
1099 rtl_dbgp_flag_init(hw
);
1100 /* Init IO handler */
1101 _rtl_usb_io_handler_init(&udev
->dev
, hw
);
1102 rtlpriv
->cfg
->ops
->read_chip_version(hw
);
1103 /*like read eeprom and so on */
1104 rtlpriv
->cfg
->ops
->read_eeprom_info(hw
);
1105 err
= _rtl_usb_init(hw
);
1108 rtl_usb_init_sw(hw
);
1109 /* Init mac80211 sw */
1110 err
= rtl_init_core(hw
);
1112 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
,
1113 "Can't allocate sw for mac80211\n");
1116 if (rtlpriv
->cfg
->ops
->init_sw_vars(hw
)) {
1117 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
, "Can't init_sw_vars\n");
1120 rtlpriv
->cfg
->ops
->init_sw_leds(hw
);
1124 rtl_deinit_core(hw
);
1125 _rtl_usb_io_handler_release(hw
);
1127 complete(&rtlpriv
->firmware_loading_complete
);
1130 EXPORT_SYMBOL(rtl_usb_probe
);
1132 void rtl_usb_disconnect(struct usb_interface
*intf
)
1134 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1135 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1136 struct rtl_mac
*rtlmac
= rtl_mac(rtl_priv(hw
));
1137 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
1139 if (unlikely(!rtlpriv
))
1142 /* just in case driver is removed before firmware callback */
1143 wait_for_completion(&rtlpriv
->firmware_loading_complete
);
1144 /*ieee80211_unregister_hw will call ops_stop */
1145 if (rtlmac
->mac80211_registered
== 1) {
1146 ieee80211_unregister_hw(hw
);
1147 rtlmac
->mac80211_registered
= 0;
1149 rtl_deinit_deferred_work(hw
);
1150 rtlpriv
->intf_ops
->adapter_stop(hw
);
1153 /* rtl_deinit_rfkill(hw); */
1155 rtl_deinit_core(hw
);
1156 kfree(rtlpriv
->usb_data
);
1157 rtlpriv
->cfg
->ops
->deinit_sw_leds(hw
);
1158 rtlpriv
->cfg
->ops
->deinit_sw_vars(hw
);
1159 _rtl_usb_io_handler_release(hw
);
1160 usb_put_dev(rtlusb
->udev
);
1161 usb_set_intfdata(intf
, NULL
);
1162 ieee80211_free_hw(hw
);
1164 EXPORT_SYMBOL(rtl_usb_disconnect
);
1166 int rtl_usb_suspend(struct usb_interface
*pusb_intf
, pm_message_t message
)
1170 EXPORT_SYMBOL(rtl_usb_suspend
);
1172 int rtl_usb_resume(struct usb_interface
*pusb_intf
)
1176 EXPORT_SYMBOL(rtl_usb_resume
);