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
= kmemdup(data
, len
, GFP_ATOMIC
);
230 usb_control_msg(udev
, pipe
, request
, reqtype
, wvalue
,
231 index
, buffer
, len
, 50);
236 static void _rtl_usb_io_handler_init(struct device
*dev
,
237 struct ieee80211_hw
*hw
)
239 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
241 rtlpriv
->io
.dev
= dev
;
242 mutex_init(&rtlpriv
->io
.bb_mutex
);
243 rtlpriv
->io
.write8_async
= _usb_write8_async
;
244 rtlpriv
->io
.write16_async
= _usb_write16_async
;
245 rtlpriv
->io
.write32_async
= _usb_write32_async
;
246 rtlpriv
->io
.read8_sync
= _usb_read8_sync
;
247 rtlpriv
->io
.read16_sync
= _usb_read16_sync
;
248 rtlpriv
->io
.read32_sync
= _usb_read32_sync
;
249 rtlpriv
->io
.writeN_sync
= _usb_writeN_sync
;
252 static void _rtl_usb_io_handler_release(struct ieee80211_hw
*hw
)
254 struct rtl_priv __maybe_unused
*rtlpriv
= rtl_priv(hw
);
256 mutex_destroy(&rtlpriv
->io
.bb_mutex
);
261 * Default aggregation handler. Do nothing and just return the oldest skb.
263 static struct sk_buff
*_none_usb_tx_aggregate_hdl(struct ieee80211_hw
*hw
,
264 struct sk_buff_head
*list
)
266 return skb_dequeue(list
);
269 #define IS_HIGH_SPEED_USB(udev) \
270 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
272 static int _rtl_usb_init_tx(struct ieee80211_hw
*hw
)
275 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
276 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
278 rtlusb
->max_bulk_out_size
= IS_HIGH_SPEED_USB(rtlusb
->udev
)
279 ? USB_HIGH_SPEED_BULK_SIZE
280 : USB_FULL_SPEED_BULK_SIZE
;
282 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
, "USB Max Bulk-out Size=%d\n",
283 rtlusb
->max_bulk_out_size
);
285 for (i
= 0; i
< __RTL_TXQ_NUM
; i
++) {
286 u32 ep_num
= rtlusb
->ep_map
.ep_mapping
[i
];
288 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
,
289 "Invalid endpoint map setting!\n");
294 rtlusb
->usb_tx_post_hdl
=
295 rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_post_hdl
;
296 rtlusb
->usb_tx_cleanup
=
297 rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_cleanup
;
298 rtlusb
->usb_tx_aggregate_hdl
=
299 (rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_aggregate_hdl
)
300 ? rtlpriv
->cfg
->usb_interface_cfg
->usb_tx_aggregate_hdl
301 : &_none_usb_tx_aggregate_hdl
;
303 init_usb_anchor(&rtlusb
->tx_submitted
);
304 for (i
= 0; i
< RTL_USB_MAX_EP_NUM
; i
++) {
305 skb_queue_head_init(&rtlusb
->tx_skb_queue
[i
]);
306 init_usb_anchor(&rtlusb
->tx_pending
[i
]);
311 static void _rtl_rx_work(unsigned long param
);
313 static int _rtl_usb_init_rx(struct ieee80211_hw
*hw
)
315 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
316 struct rtl_usb_priv
*usb_priv
= rtl_usbpriv(hw
);
317 struct rtl_usb
*rtlusb
= rtl_usbdev(usb_priv
);
319 rtlusb
->rx_max_size
= rtlpriv
->cfg
->usb_interface_cfg
->rx_max_size
;
320 rtlusb
->rx_urb_num
= rtlpriv
->cfg
->usb_interface_cfg
->rx_urb_num
;
321 rtlusb
->in_ep
= rtlpriv
->cfg
->usb_interface_cfg
->in_ep_num
;
322 rtlusb
->usb_rx_hdl
= rtlpriv
->cfg
->usb_interface_cfg
->usb_rx_hdl
;
323 rtlusb
->usb_rx_segregate_hdl
=
324 rtlpriv
->cfg
->usb_interface_cfg
->usb_rx_segregate_hdl
;
326 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
327 rtlusb
->rx_max_size
, rtlusb
->rx_urb_num
, rtlusb
->in_ep
);
328 init_usb_anchor(&rtlusb
->rx_submitted
);
329 init_usb_anchor(&rtlusb
->rx_cleanup_urbs
);
331 skb_queue_head_init(&rtlusb
->rx_queue
);
332 rtlusb
->rx_work_tasklet
.func
= _rtl_rx_work
;
333 rtlusb
->rx_work_tasklet
.data
= (unsigned long)rtlusb
;
338 static int _rtl_usb_init(struct ieee80211_hw
*hw
)
340 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
341 struct rtl_usb_priv
*usb_priv
= rtl_usbpriv(hw
);
342 struct rtl_usb
*rtlusb
= rtl_usbdev(usb_priv
);
345 struct usb_interface
*usb_intf
= rtlusb
->intf
;
346 u8 epnums
= usb_intf
->cur_altsetting
->desc
.bNumEndpoints
;
348 rtlusb
->out_ep_nums
= rtlusb
->in_ep_nums
= 0;
349 for (epidx
= 0; epidx
< epnums
; epidx
++) {
350 struct usb_endpoint_descriptor
*pep_desc
;
351 pep_desc
= &usb_intf
->cur_altsetting
->endpoint
[epidx
].desc
;
353 if (usb_endpoint_dir_in(pep_desc
))
354 rtlusb
->in_ep_nums
++;
355 else if (usb_endpoint_dir_out(pep_desc
))
356 rtlusb
->out_ep_nums
++;
358 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_DMESG
,
359 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
360 pep_desc
->bEndpointAddress
, pep_desc
->wMaxPacketSize
,
361 pep_desc
->bInterval
);
363 if (rtlusb
->in_ep_nums
< rtlpriv
->cfg
->usb_interface_cfg
->in_ep_num
) {
364 pr_err("Too few input end points found\n");
367 if (rtlusb
->out_ep_nums
== 0) {
368 pr_err("No output end points found\n");
371 /* usb endpoint mapping */
372 err
= rtlpriv
->cfg
->usb_interface_cfg
->usb_endpoint_mapping(hw
);
373 rtlusb
->usb_mq_to_hwq
= rtlpriv
->cfg
->usb_interface_cfg
->usb_mq_to_hwq
;
374 _rtl_usb_init_tx(hw
);
375 _rtl_usb_init_rx(hw
);
379 static void rtl_usb_init_sw(struct ieee80211_hw
*hw
)
381 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
382 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
383 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
384 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
387 ppsc
->inactiveps
= false;
388 ppsc
->leisure_ps
= false;
389 ppsc
->fwctrl_lps
= false;
390 ppsc
->reg_fwctrl_lps
= 3;
391 ppsc
->reg_max_lps_awakeintvl
= 5;
392 ppsc
->fwctrl_psmode
= FW_PS_DTIM_MODE
;
395 mac
->beacon_interval
= 100;
398 mac
->min_space_cfg
= 0;
399 mac
->max_mss_density
= 0;
401 /* set sane AMPDU defaults */
402 mac
->current_ampdu_density
= 7;
403 mac
->current_ampdu_factor
= 3;
406 rtlusb
->acm_method
= eAcmWay2_SW
;
409 /* HIMR - turn all on */
410 rtlusb
->irq_mask
[0] = 0xFFFFFFFF;
411 /* HIMR_EX - turn all on */
412 rtlusb
->irq_mask
[1] = 0xFFFFFFFF;
413 rtlusb
->disableHWSM
= true;
416 static void _rtl_rx_completed(struct urb
*urb
);
418 static int _rtl_prep_rx_urb(struct ieee80211_hw
*hw
, struct rtl_usb
*rtlusb
,
419 struct urb
*urb
, gfp_t gfp_mask
)
421 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
424 buf
= usb_alloc_coherent(rtlusb
->udev
, rtlusb
->rx_max_size
, gfp_mask
,
427 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
428 "Failed to usb_alloc_coherent!!\n");
432 usb_fill_bulk_urb(urb
, rtlusb
->udev
,
433 usb_rcvbulkpipe(rtlusb
->udev
, rtlusb
->in_ep
),
434 buf
, rtlusb
->rx_max_size
, _rtl_rx_completed
, rtlusb
);
435 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
440 static void _rtl_usb_rx_process_agg(struct ieee80211_hw
*hw
,
443 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
444 u8
*rxdesc
= skb
->data
;
445 struct ieee80211_hdr
*hdr
;
446 bool unicast
= false;
448 struct ieee80211_rx_status rx_status
= {0};
449 struct rtl_stats stats
= {
455 skb_pull(skb
, RTL_RX_DESC_SIZE
);
456 rtlpriv
->cfg
->ops
->query_rx_desc(hw
, &stats
, &rx_status
, rxdesc
, skb
);
457 skb_pull(skb
, (stats
.rx_drvinfo_size
+ stats
.rx_bufshift
));
458 hdr
= (struct ieee80211_hdr
*)(skb
->data
);
459 fc
= hdr
->frame_control
;
461 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
463 if (is_broadcast_ether_addr(hdr
->addr1
)) {
465 } else if (is_multicast_ether_addr(hdr
->addr1
)) {
469 rtlpriv
->stats
.rxbytesunicast
+= skb
->len
;
472 rtl_is_special_data(hw
, skb
, false);
474 if (ieee80211_is_data(fc
)) {
475 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_RX
);
478 rtlpriv
->link_info
.num_rx_inperiod
++;
483 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw
*hw
,
486 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
487 u8
*rxdesc
= skb
->data
;
488 struct ieee80211_hdr
*hdr
;
489 bool unicast
= false;
491 struct ieee80211_rx_status rx_status
= {0};
492 struct rtl_stats stats
= {
498 skb_pull(skb
, RTL_RX_DESC_SIZE
);
499 rtlpriv
->cfg
->ops
->query_rx_desc(hw
, &stats
, &rx_status
, rxdesc
, skb
);
500 skb_pull(skb
, (stats
.rx_drvinfo_size
+ stats
.rx_bufshift
));
501 hdr
= (struct ieee80211_hdr
*)(skb
->data
);
502 fc
= hdr
->frame_control
;
504 memcpy(IEEE80211_SKB_RXCB(skb
), &rx_status
, sizeof(rx_status
));
506 if (is_broadcast_ether_addr(hdr
->addr1
)) {
508 } else if (is_multicast_ether_addr(hdr
->addr1
)) {
512 rtlpriv
->stats
.rxbytesunicast
+= skb
->len
;
515 rtl_is_special_data(hw
, skb
, false);
517 if (ieee80211_is_data(fc
)) {
518 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_RX
);
521 rtlpriv
->link_info
.num_rx_inperiod
++;
524 /* static bcn for roaming */
525 rtl_beacon_statistic(hw
, skb
);
527 if (likely(rtl_action_proc(hw
, skb
, false)))
528 ieee80211_rx(hw
, skb
);
530 dev_kfree_skb_any(skb
);
534 static void _rtl_rx_pre_process(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
536 struct sk_buff
*_skb
;
537 struct sk_buff_head rx_queue
;
538 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
540 skb_queue_head_init(&rx_queue
);
541 if (rtlusb
->usb_rx_segregate_hdl
)
542 rtlusb
->usb_rx_segregate_hdl(hw
, skb
, &rx_queue
);
543 WARN_ON(skb_queue_empty(&rx_queue
));
544 while (!skb_queue_empty(&rx_queue
)) {
545 _skb
= skb_dequeue(&rx_queue
);
546 _rtl_usb_rx_process_agg(hw
, _skb
);
547 ieee80211_rx(hw
, _skb
);
551 #define __RX_SKB_MAX_QUEUED 32
553 static void _rtl_rx_work(unsigned long param
)
555 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)param
;
556 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
559 while ((skb
= skb_dequeue(&rtlusb
->rx_queue
))) {
560 if (unlikely(IS_USB_STOP(rtlusb
))) {
561 dev_kfree_skb_any(skb
);
565 if (likely(!rtlusb
->usb_rx_segregate_hdl
)) {
566 _rtl_usb_rx_process_noagg(hw
, skb
);
569 _rtl_rx_pre_process(hw
, skb
);
570 pr_err("rx agg not supported\n");
575 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr
*hdr
,
578 unsigned int padding
= 0;
580 /* make function no-op when possible */
581 if (NET_IP_ALIGN
== 0 || len
< sizeof(*hdr
))
584 /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
585 /* TODO: deduplicate common code, define helper function instead? */
587 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
588 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
590 padding
^= NET_IP_ALIGN
;
592 /* Input might be invalid, avoid accessing memory outside
595 if ((unsigned long)qc
- (unsigned long)hdr
< len
&&
596 *qc
& IEEE80211_QOS_CTL_A_MSDU_PRESENT
)
597 padding
^= NET_IP_ALIGN
;
600 if (ieee80211_has_a4(hdr
->frame_control
))
601 padding
^= NET_IP_ALIGN
;
606 #define __RADIO_TAP_SIZE_RSV 32
608 static void _rtl_rx_completed(struct urb
*_urb
)
610 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)_urb
->context
;
611 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
612 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
615 if (unlikely(IS_USB_STOP(rtlusb
)))
618 if (likely(0 == _urb
->status
)) {
619 unsigned int padding
;
622 unsigned int size
= _urb
->actual_length
;
623 struct ieee80211_hdr
*hdr
;
625 if (size
< RTL_RX_DESC_SIZE
+ sizeof(struct ieee80211_hdr
)) {
626 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
627 "Too short packet from bulk IN! (len: %d)\n",
632 qlen
= skb_queue_len(&rtlusb
->rx_queue
);
633 if (qlen
>= __RX_SKB_MAX_QUEUED
) {
634 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
635 "Pending RX skbuff queue full! (qlen: %d)\n",
640 hdr
= (void *)(_urb
->transfer_buffer
+ RTL_RX_DESC_SIZE
);
641 padding
= _rtl_rx_get_padding(hdr
, size
- RTL_RX_DESC_SIZE
);
643 skb
= dev_alloc_skb(size
+ __RADIO_TAP_SIZE_RSV
+ padding
);
645 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
646 "Can't allocate skb for bulk IN!\n");
650 _rtl_install_trx_info(rtlusb
, skb
, rtlusb
->in_ep
);
652 /* Make sure the payload data is 4 byte aligned. */
653 skb_reserve(skb
, padding
);
655 /* reserve some space for mac80211's radiotap */
656 skb_reserve(skb
, __RADIO_TAP_SIZE_RSV
);
658 memcpy(skb_put(skb
, size
), _urb
->transfer_buffer
, size
);
660 skb_queue_tail(&rtlusb
->rx_queue
, skb
);
661 tasklet_schedule(&rtlusb
->rx_work_tasklet
);
666 switch (_urb
->status
) {
678 usb_anchor_urb(_urb
, &rtlusb
->rx_submitted
);
679 err
= usb_submit_urb(_urb
, GFP_ATOMIC
);
681 usb_unanchor_urb(_urb
);
687 /* On some architectures, usb_free_coherent must not be called from
688 * hardirq context. Queue urb to cleanup list.
690 usb_anchor_urb(_urb
, &rtlusb
->rx_cleanup_urbs
);
693 #undef __RADIO_TAP_SIZE_RSV
695 static void _rtl_usb_cleanup_rx(struct ieee80211_hw
*hw
)
697 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
700 usb_kill_anchored_urbs(&rtlusb
->rx_submitted
);
702 tasklet_kill(&rtlusb
->rx_work_tasklet
);
703 skb_queue_purge(&rtlusb
->rx_queue
);
705 while ((urb
= usb_get_from_anchor(&rtlusb
->rx_cleanup_urbs
))) {
706 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
707 urb
->transfer_buffer
, urb
->transfer_dma
);
712 static int _rtl_usb_receive(struct ieee80211_hw
*hw
)
717 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
718 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
720 WARN_ON(0 == rtlusb
->rx_urb_num
);
721 /* 1600 == 1514 + max WLAN header + rtk info */
722 WARN_ON(rtlusb
->rx_max_size
< 1600);
724 for (i
= 0; i
< rtlusb
->rx_urb_num
; i
++) {
726 urb
= usb_alloc_urb(0, GFP_KERNEL
);
728 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
729 "Failed to alloc URB!!\n");
733 err
= _rtl_prep_rx_urb(hw
, rtlusb
, urb
, GFP_KERNEL
);
735 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
736 "Failed to prep_rx_urb!!\n");
741 usb_anchor_urb(urb
, &rtlusb
->rx_submitted
);
742 err
= usb_submit_urb(urb
, GFP_KERNEL
);
750 usb_kill_anchored_urbs(&rtlusb
->rx_submitted
);
751 _rtl_usb_cleanup_rx(hw
);
755 static int rtl_usb_start(struct ieee80211_hw
*hw
)
758 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
759 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
760 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
762 err
= rtlpriv
->cfg
->ops
->hw_init(hw
);
764 rtl_init_rx_config(hw
);
766 /* Enable software */
767 SET_USB_START(rtlusb
);
768 /* should after adapter start and interrupt enable. */
769 set_hal_start(rtlhal
);
772 err
= _rtl_usb_receive(hw
);
782 /*======================= tx =========================================*/
783 static void rtl_usb_cleanup(struct ieee80211_hw
*hw
)
786 struct sk_buff
*_skb
;
787 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
788 struct ieee80211_tx_info
*txinfo
;
790 SET_USB_STOP(rtlusb
);
792 /* clean up rx stuff. */
793 _rtl_usb_cleanup_rx(hw
);
795 /* clean up tx stuff */
796 for (i
= 0; i
< RTL_USB_MAX_EP_NUM
; i
++) {
797 while ((_skb
= skb_dequeue(&rtlusb
->tx_skb_queue
[i
]))) {
798 rtlusb
->usb_tx_cleanup(hw
, _skb
);
799 txinfo
= IEEE80211_SKB_CB(_skb
);
800 ieee80211_tx_info_clear_status(txinfo
);
801 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
802 ieee80211_tx_status_irqsafe(hw
, _skb
);
804 usb_kill_anchored_urbs(&rtlusb
->tx_pending
[i
]);
806 usb_kill_anchored_urbs(&rtlusb
->tx_submitted
);
811 * We may add some struct into struct rtl_usb later. Do deinit here.
814 static void rtl_usb_deinit(struct ieee80211_hw
*hw
)
819 static void rtl_usb_stop(struct ieee80211_hw
*hw
)
821 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
822 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
823 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
825 /* should after adapter start and interrupt enable. */
826 set_hal_stop(rtlhal
);
827 cancel_work_sync(&rtlpriv
->works
.fill_h2c_cmd
);
828 /* Enable software */
829 SET_USB_STOP(rtlusb
);
831 rtlpriv
->cfg
->ops
->hw_disable(hw
);
834 static void _rtl_submit_tx_urb(struct ieee80211_hw
*hw
, struct urb
*_urb
)
837 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
838 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
840 usb_anchor_urb(_urb
, &rtlusb
->tx_submitted
);
841 err
= usb_submit_urb(_urb
, GFP_ATOMIC
);
845 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
846 "Failed to submit urb\n");
847 usb_unanchor_urb(_urb
);
848 skb
= (struct sk_buff
*)_urb
->context
;
854 static int _usb_tx_post(struct ieee80211_hw
*hw
, struct urb
*urb
,
857 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
858 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
859 struct ieee80211_tx_info
*txinfo
;
861 rtlusb
->usb_tx_post_hdl(hw
, urb
, skb
);
862 skb_pull(skb
, RTL_TX_HEADER_SIZE
);
863 txinfo
= IEEE80211_SKB_CB(skb
);
864 ieee80211_tx_info_clear_status(txinfo
);
865 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
868 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
869 "Urb has error status 0x%X\n", urb
->status
);
872 /* TODO: statistics */
874 ieee80211_tx_status_irqsafe(hw
, skb
);
878 static void _rtl_tx_complete(struct urb
*urb
)
880 struct sk_buff
*skb
= (struct sk_buff
*)urb
->context
;
881 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
882 struct rtl_usb
*rtlusb
= (struct rtl_usb
*)info
->rate_driver_data
[0];
883 struct ieee80211_hw
*hw
= usb_get_intfdata(rtlusb
->intf
);
886 if (unlikely(IS_USB_STOP(rtlusb
)))
888 err
= _usb_tx_post(hw
, urb
, skb
);
890 /* Ignore error and keep issuiing other urbs */
895 static struct urb
*_rtl_usb_tx_urb_setup(struct ieee80211_hw
*hw
,
896 struct sk_buff
*skb
, u32 ep_num
)
898 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
899 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
902 WARN_ON(NULL
== skb
);
903 _urb
= usb_alloc_urb(0, GFP_ATOMIC
);
905 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
906 "Can't allocate URB for bulk out!\n");
910 _rtl_install_trx_info(rtlusb
, skb
, ep_num
);
911 usb_fill_bulk_urb(_urb
, rtlusb
->udev
, usb_sndbulkpipe(rtlusb
->udev
,
912 ep_num
), skb
->data
, skb
->len
, _rtl_tx_complete
, skb
);
913 _urb
->transfer_flags
|= URB_ZERO_PACKET
;
917 static void _rtl_usb_transmit(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
920 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
921 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
923 struct urb
*_urb
= NULL
;
924 struct sk_buff
*_skb
= NULL
;
926 WARN_ON(NULL
== rtlusb
->usb_tx_aggregate_hdl
);
927 if (unlikely(IS_USB_STOP(rtlusb
))) {
928 RT_TRACE(rtlpriv
, COMP_USB
, DBG_EMERG
,
929 "USB device is stopping...\n");
933 ep_num
= rtlusb
->ep_map
.ep_mapping
[qnum
];
935 _urb
= _rtl_usb_tx_urb_setup(hw
, _skb
, ep_num
);
936 if (unlikely(!_urb
)) {
937 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
,
938 "Can't allocate urb. Drop skb!\n");
942 _rtl_submit_tx_urb(hw
, _urb
);
945 static void _rtl_usb_tx_preprocess(struct ieee80211_hw
*hw
,
946 struct ieee80211_sta
*sta
,
950 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
951 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
952 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
953 struct rtl_tx_desc
*pdesc
= NULL
;
954 struct rtl_tcb_desc tcb_desc
;
955 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)(skb
->data
);
956 __le16 fc
= hdr
->frame_control
;
957 u8
*pda_addr
= hdr
->addr1
;
963 memset(&tcb_desc
, 0, sizeof(struct rtl_tcb_desc
));
964 if (ieee80211_is_auth(fc
)) {
965 RT_TRACE(rtlpriv
, COMP_SEND
, DBG_DMESG
, "MAC80211_LINKING\n");
969 if (rtlpriv
->psc
.sw_ps_enabled
) {
970 if (ieee80211_is_data(fc
) && !ieee80211_is_nullfunc(fc
) &&
971 !ieee80211_has_pm(fc
))
972 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_PM
);
975 rtl_action_proc(hw
, skb
, true);
976 if (is_multicast_ether_addr(pda_addr
))
977 rtlpriv
->stats
.txbytesmulticast
+= skb
->len
;
978 else if (is_broadcast_ether_addr(pda_addr
))
979 rtlpriv
->stats
.txbytesbroadcast
+= skb
->len
;
981 rtlpriv
->stats
.txbytesunicast
+= skb
->len
;
982 if (ieee80211_is_data_qos(fc
)) {
983 qc
= ieee80211_get_qos_ctl(hdr
);
984 tid
= qc
[0] & IEEE80211_QOS_CTL_TID_MASK
;
985 seq_number
= (le16_to_cpu(hdr
->seq_ctrl
) &
986 IEEE80211_SCTL_SEQ
) >> 4;
990 rtlpriv
->cfg
->ops
->fill_tx_desc(hw
, hdr
, (u8
*)pdesc
, info
, sta
, skb
,
991 hw_queue
, &tcb_desc
);
992 if (!ieee80211_has_morefrags(hdr
->frame_control
)) {
994 mac
->tids
[tid
].seq_number
= seq_number
;
996 if (ieee80211_is_data(fc
))
997 rtlpriv
->cfg
->ops
->led_control(hw
, LED_CTL_TX
);
1000 static int rtl_usb_tx(struct ieee80211_hw
*hw
,
1001 struct ieee80211_sta
*sta
,
1002 struct sk_buff
*skb
,
1003 struct rtl_tcb_desc
*dummy
)
1005 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
1006 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
1007 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)(skb
->data
);
1008 __le16 fc
= hdr
->frame_control
;
1011 if (unlikely(is_hal_stop(rtlhal
)))
1013 hw_queue
= rtlusb
->usb_mq_to_hwq(fc
, skb_get_queue_mapping(skb
));
1014 _rtl_usb_tx_preprocess(hw
, sta
, skb
, hw_queue
);
1015 _rtl_usb_transmit(hw
, skb
, hw_queue
);
1016 return NETDEV_TX_OK
;
1019 dev_kfree_skb_any(skb
);
1020 return NETDEV_TX_OK
;
1023 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw
*hw
,
1024 struct ieee80211_sta
*sta
,
1025 struct sk_buff
*skb
)
1030 static void rtl_fill_h2c_cmd_work_callback(struct work_struct
*work
)
1032 struct rtl_works
*rtlworks
=
1033 container_of(work
, struct rtl_works
, fill_h2c_cmd
);
1034 struct ieee80211_hw
*hw
= rtlworks
->hw
;
1035 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1037 rtlpriv
->cfg
->ops
->fill_h2c_cmd(hw
, H2C_RA_MASK
, 5, rtlpriv
->rate_mask
);
1040 static struct rtl_intf_ops rtl_usb_ops
= {
1041 .adapter_start
= rtl_usb_start
,
1042 .adapter_stop
= rtl_usb_stop
,
1043 .adapter_tx
= rtl_usb_tx
,
1044 .waitq_insert
= rtl_usb_tx_chk_waitq_insert
,
1047 int rtl_usb_probe(struct usb_interface
*intf
,
1048 const struct usb_device_id
*id
,
1049 struct rtl_hal_cfg
*rtl_hal_cfg
)
1052 struct ieee80211_hw
*hw
= NULL
;
1053 struct rtl_priv
*rtlpriv
= NULL
;
1054 struct usb_device
*udev
;
1055 struct rtl_usb_priv
*usb_priv
;
1057 hw
= ieee80211_alloc_hw(sizeof(struct rtl_priv
) +
1058 sizeof(struct rtl_usb_priv
), &rtl_ops
);
1060 RT_ASSERT(false, "ieee80211 alloc failed\n");
1064 rtlpriv
->usb_data
= kzalloc(RTL_USB_MAX_RX_COUNT
* sizeof(u32
),
1066 if (!rtlpriv
->usb_data
)
1069 /* this spin lock must be initialized early */
1070 spin_lock_init(&rtlpriv
->locks
.usb_lock
);
1071 INIT_WORK(&rtlpriv
->works
.fill_h2c_cmd
,
1072 rtl_fill_h2c_cmd_work_callback
);
1074 rtlpriv
->usb_data_index
= 0;
1075 init_completion(&rtlpriv
->firmware_loading_complete
);
1076 SET_IEEE80211_DEV(hw
, &intf
->dev
);
1077 udev
= interface_to_usbdev(intf
);
1079 usb_priv
= rtl_usbpriv(hw
);
1080 memset(usb_priv
, 0, sizeof(*usb_priv
));
1081 usb_priv
->dev
.intf
= intf
;
1082 usb_priv
->dev
.udev
= udev
;
1083 usb_set_intfdata(intf
, hw
);
1084 /* init cfg & intf_ops */
1085 rtlpriv
->rtlhal
.interface
= INTF_USB
;
1086 rtlpriv
->cfg
= rtl_hal_cfg
;
1087 rtlpriv
->intf_ops
= &rtl_usb_ops
;
1088 rtl_dbgp_flag_init(hw
);
1089 /* Init IO handler */
1090 _rtl_usb_io_handler_init(&udev
->dev
, hw
);
1091 rtlpriv
->cfg
->ops
->read_chip_version(hw
);
1092 /*like read eeprom and so on */
1093 rtlpriv
->cfg
->ops
->read_eeprom_info(hw
);
1094 err
= _rtl_usb_init(hw
);
1097 rtl_usb_init_sw(hw
);
1098 /* Init mac80211 sw */
1099 err
= rtl_init_core(hw
);
1101 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
,
1102 "Can't allocate sw for mac80211\n");
1105 if (rtlpriv
->cfg
->ops
->init_sw_vars(hw
)) {
1106 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_EMERG
, "Can't init_sw_vars\n");
1109 rtlpriv
->cfg
->ops
->init_sw_leds(hw
);
1113 rtl_deinit_core(hw
);
1114 _rtl_usb_io_handler_release(hw
);
1116 complete(&rtlpriv
->firmware_loading_complete
);
1119 EXPORT_SYMBOL(rtl_usb_probe
);
1121 void rtl_usb_disconnect(struct usb_interface
*intf
)
1123 struct ieee80211_hw
*hw
= usb_get_intfdata(intf
);
1124 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
1125 struct rtl_mac
*rtlmac
= rtl_mac(rtl_priv(hw
));
1126 struct rtl_usb
*rtlusb
= rtl_usbdev(rtl_usbpriv(hw
));
1128 if (unlikely(!rtlpriv
))
1131 /* just in case driver is removed before firmware callback */
1132 wait_for_completion(&rtlpriv
->firmware_loading_complete
);
1133 /*ieee80211_unregister_hw will call ops_stop */
1134 if (rtlmac
->mac80211_registered
== 1) {
1135 ieee80211_unregister_hw(hw
);
1136 rtlmac
->mac80211_registered
= 0;
1138 rtl_deinit_deferred_work(hw
);
1139 rtlpriv
->intf_ops
->adapter_stop(hw
);
1142 /* rtl_deinit_rfkill(hw); */
1144 rtl_deinit_core(hw
);
1145 kfree(rtlpriv
->usb_data
);
1146 rtlpriv
->cfg
->ops
->deinit_sw_leds(hw
);
1147 rtlpriv
->cfg
->ops
->deinit_sw_vars(hw
);
1148 _rtl_usb_io_handler_release(hw
);
1149 usb_put_dev(rtlusb
->udev
);
1150 usb_set_intfdata(intf
, NULL
);
1151 ieee80211_free_hw(hw
);
1153 EXPORT_SYMBOL(rtl_usb_disconnect
);
1155 int rtl_usb_suspend(struct usb_interface
*pusb_intf
, pm_message_t message
)
1159 EXPORT_SYMBOL(rtl_usb_suspend
);
1161 int rtl_usb_resume(struct usb_interface
*pusb_intf
)
1165 EXPORT_SYMBOL(rtl_usb_resume
);