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 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 *****************************************************************************/
29 #include "rtl8192c/fw_common.h"
30 #include <linux/export.h>
31 #include <linux/module.h>
33 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
34 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
35 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
36 MODULE_LICENSE("GPL");
37 MODULE_DESCRIPTION("USB basic driver for rtlwifi");
39 #define REALTEK_USB_VENQT_READ 0xC0
40 #define REALTEK_USB_VENQT_WRITE 0x40
41 #define REALTEK_USB_VENQT_CMD_REQ 0x05
42 #define REALTEK_USB_VENQT_CMD_IDX 0x00
44 #define MAX_USBCTRL_VENDORREQ_TIMES 10
46 static void usbctrl_async_callback(struct urb *urb)
50 kfree(urb->setup_packet);
52 kfree(urb->transfer_buffer);
56 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
57 u16 value, u16 index, void *pdata,
63 struct usb_ctrlrequest *dr;
65 const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE;
68 if (WARN_ON_ONCE(len > databuf_maxlen))
71 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
72 reqtype = REALTEK_USB_VENQT_WRITE;
74 dr = kzalloc(sizeof(*dr), GFP_ATOMIC);
78 databuf = kzalloc(databuf_maxlen, GFP_ATOMIC);
84 urb = usb_alloc_urb(0, GFP_ATOMIC);
91 dr->bRequestType = reqtype;
92 dr->bRequest = request;
93 dr->wValue = cpu_to_le16(value);
94 dr->wIndex = cpu_to_le16(index);
95 dr->wLength = cpu_to_le16(len);
96 /* data are already in little-endian order */
97 memcpy(databuf, pdata, len);
98 usb_fill_control_urb(urb, udev, pipe,
99 (unsigned char *)dr, databuf, len,
100 usbctrl_async_callback, NULL);
101 rc = usb_submit_urb(urb, GFP_ATOMIC);
110 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
111 u16 value, u16 index, void *pdata,
117 int vendorreq_times = 0;
120 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
121 reqtype = REALTEK_USB_VENQT_READ;
124 status = usb_control_msg(udev, pipe, request, reqtype, value,
125 index, pdata, len, 1000);
127 /* firmware download is checksumed, don't retry */
128 if ((value >= FW_8192C_START_ADDRESS &&
129 value <= FW_8192C_END_ADDRESS))
134 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
136 if (status < 0 && count++ < 4)
137 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
138 value, status, *(u32 *)pdata);
142 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
144 struct device *dev = rtlpriv->io.dev;
145 struct usb_device *udev = to_usb_device(dev);
152 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
153 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
154 rtlpriv->usb_data_index = 0;
155 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
156 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
157 request = REALTEK_USB_VENQT_CMD_REQ;
158 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
161 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
162 return le32_to_cpu(*data);
165 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
167 return (u8)_usb_read_sync(rtlpriv, addr, 1);
170 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
172 return (u16)_usb_read_sync(rtlpriv, addr, 2);
175 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
177 return _usb_read_sync(rtlpriv, addr, 4);
180 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
188 request = REALTEK_USB_VENQT_CMD_REQ;
189 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
190 wvalue = (u16)(addr&0x0000ffff);
191 data = cpu_to_le32(val);
192 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
196 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
198 struct device *dev = rtlpriv->io.dev;
200 _usb_write_async(to_usb_device(dev), addr, val, 1);
203 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
205 struct device *dev = rtlpriv->io.dev;
207 _usb_write_async(to_usb_device(dev), addr, val, 2);
210 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
212 struct device *dev = rtlpriv->io.dev;
214 _usb_write_async(to_usb_device(dev), addr, val, 4);
217 static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
220 struct device *dev = rtlpriv->io.dev;
221 struct usb_device *udev = to_usb_device(dev);
222 u8 request = REALTEK_USB_VENQT_CMD_REQ;
223 u8 reqtype = REALTEK_USB_VENQT_WRITE;
225 u16 index = REALTEK_USB_VENQT_CMD_IDX;
226 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
229 wvalue = (u16)(addr & 0x0000ffff);
230 buffer = kmemdup(data, len, GFP_ATOMIC);
233 usb_control_msg(udev, pipe, request, reqtype, wvalue,
234 index, buffer, len, 50);
239 static void _rtl_usb_io_handler_init(struct device *dev,
240 struct ieee80211_hw *hw)
242 struct rtl_priv *rtlpriv = rtl_priv(hw);
244 rtlpriv->io.dev = dev;
245 mutex_init(&rtlpriv->io.bb_mutex);
246 rtlpriv->io.write8_async = _usb_write8_async;
247 rtlpriv->io.write16_async = _usb_write16_async;
248 rtlpriv->io.write32_async = _usb_write32_async;
249 rtlpriv->io.read8_sync = _usb_read8_sync;
250 rtlpriv->io.read16_sync = _usb_read16_sync;
251 rtlpriv->io.read32_sync = _usb_read32_sync;
252 rtlpriv->io.writeN_sync = _usb_writeN_sync;
255 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
257 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
259 mutex_destroy(&rtlpriv->io.bb_mutex);
264 * Default aggregation handler. Do nothing and just return the oldest skb.
266 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
267 struct sk_buff_head *list)
269 return skb_dequeue(list);
272 #define IS_HIGH_SPEED_USB(udev) \
273 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
275 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
278 struct rtl_priv *rtlpriv = rtl_priv(hw);
279 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
281 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
282 ? USB_HIGH_SPEED_BULK_SIZE
283 : USB_FULL_SPEED_BULK_SIZE;
285 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
286 rtlusb->max_bulk_out_size);
288 for (i = 0; i < __RTL_TXQ_NUM; i++) {
289 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
291 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
292 "Invalid endpoint map setting!\n");
297 rtlusb->usb_tx_post_hdl =
298 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
299 rtlusb->usb_tx_cleanup =
300 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
301 rtlusb->usb_tx_aggregate_hdl =
302 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
303 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
304 : &_none_usb_tx_aggregate_hdl;
306 init_usb_anchor(&rtlusb->tx_submitted);
307 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
308 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
309 init_usb_anchor(&rtlusb->tx_pending[i]);
314 static void _rtl_rx_work(unsigned long param);
316 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
318 struct rtl_priv *rtlpriv = rtl_priv(hw);
319 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
320 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
322 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
323 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
324 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
325 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
326 rtlusb->usb_rx_segregate_hdl =
327 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
329 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
330 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
331 init_usb_anchor(&rtlusb->rx_submitted);
332 init_usb_anchor(&rtlusb->rx_cleanup_urbs);
334 skb_queue_head_init(&rtlusb->rx_queue);
335 rtlusb->rx_work_tasklet.func = _rtl_rx_work;
336 rtlusb->rx_work_tasklet.data = (unsigned long)rtlusb;
341 static int _rtl_usb_init(struct ieee80211_hw *hw)
343 struct rtl_priv *rtlpriv = rtl_priv(hw);
344 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
345 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
348 struct usb_interface *usb_intf = rtlusb->intf;
349 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
351 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
352 for (epidx = 0; epidx < epnums; epidx++) {
353 struct usb_endpoint_descriptor *pep_desc;
354 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
356 if (usb_endpoint_dir_in(pep_desc))
357 rtlusb->in_ep_nums++;
358 else if (usb_endpoint_dir_out(pep_desc))
359 rtlusb->out_ep_nums++;
361 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
362 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
363 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
364 pep_desc->bInterval);
366 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) {
367 pr_err("Too few input end points found\n");
370 if (rtlusb->out_ep_nums == 0) {
371 pr_err("No output end points found\n");
374 /* usb endpoint mapping */
375 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
376 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
377 _rtl_usb_init_tx(hw);
378 _rtl_usb_init_rx(hw);
382 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
384 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
385 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
386 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
387 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
390 ppsc->inactiveps = false;
391 ppsc->leisure_ps = false;
392 ppsc->fwctrl_lps = false;
393 ppsc->reg_fwctrl_lps = 3;
394 ppsc->reg_max_lps_awakeintvl = 5;
395 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
398 mac->beacon_interval = 100;
401 mac->min_space_cfg = 0;
402 mac->max_mss_density = 0;
404 /* set sane AMPDU defaults */
405 mac->current_ampdu_density = 7;
406 mac->current_ampdu_factor = 3;
409 rtlusb->acm_method = EACMWAY2_SW;
412 /* HIMR - turn all on */
413 rtlusb->irq_mask[0] = 0xFFFFFFFF;
414 /* HIMR_EX - turn all on */
415 rtlusb->irq_mask[1] = 0xFFFFFFFF;
416 rtlusb->disableHWSM = true;
419 static void _rtl_rx_completed(struct urb *urb);
421 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
422 struct urb *urb, gfp_t gfp_mask)
424 struct rtl_priv *rtlpriv = rtl_priv(hw);
427 buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
430 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
431 "Failed to usb_alloc_coherent!!\n");
435 usb_fill_bulk_urb(urb, rtlusb->udev,
436 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
437 buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
438 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
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 = {
457 skb_pull(skb, RTL_RX_DESC_SIZE);
458 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
459 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
460 hdr = (struct ieee80211_hdr *)(skb->data);
461 fc = hdr->frame_control;
463 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
465 if (is_broadcast_ether_addr(hdr->addr1)) {
467 } else if (is_multicast_ether_addr(hdr->addr1)) {
471 rtlpriv->stats.rxbytesunicast += skb->len;
474 if (ieee80211_is_data(fc)) {
475 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
478 rtlpriv->link_info.num_rx_inperiod++;
480 /* static bcn for roaming */
481 rtl_beacon_statistic(hw, skb);
485 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
488 struct rtl_priv *rtlpriv = rtl_priv(hw);
489 u8 *rxdesc = skb->data;
490 struct ieee80211_hdr *hdr;
491 bool unicast = false;
493 struct ieee80211_rx_status rx_status = {0};
494 struct rtl_stats stats = {
499 skb_pull(skb, RTL_RX_DESC_SIZE);
500 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
501 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
502 hdr = (struct ieee80211_hdr *)(skb->data);
503 fc = hdr->frame_control;
505 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
507 if (is_broadcast_ether_addr(hdr->addr1)) {
509 } else if (is_multicast_ether_addr(hdr->addr1)) {
513 rtlpriv->stats.rxbytesunicast += skb->len;
516 if (ieee80211_is_data(fc)) {
517 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
520 rtlpriv->link_info.num_rx_inperiod++;
523 /* static bcn for roaming */
524 rtl_beacon_statistic(hw, skb);
526 if (likely(rtl_action_proc(hw, skb, false)))
527 ieee80211_rx(hw, skb);
529 dev_kfree_skb_any(skb);
531 dev_kfree_skb_any(skb);
535 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
537 struct sk_buff *_skb;
538 struct sk_buff_head rx_queue;
539 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
541 skb_queue_head_init(&rx_queue);
542 if (rtlusb->usb_rx_segregate_hdl)
543 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
544 WARN_ON(skb_queue_empty(&rx_queue));
545 while (!skb_queue_empty(&rx_queue)) {
546 _skb = skb_dequeue(&rx_queue);
547 _rtl_usb_rx_process_agg(hw, _skb);
548 ieee80211_rx(hw, _skb);
552 #define __RX_SKB_MAX_QUEUED 64
554 static void _rtl_rx_work(unsigned long param)
556 struct rtl_usb *rtlusb = (struct rtl_usb *)param;
557 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
560 while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
561 if (unlikely(IS_USB_STOP(rtlusb))) {
562 dev_kfree_skb_any(skb);
566 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
567 _rtl_usb_rx_process_noagg(hw, skb);
570 _rtl_rx_pre_process(hw, skb);
571 pr_err("rx agg not supported\n");
576 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr,
579 #if NET_IP_ALIGN != 0
580 unsigned int padding = 0;
583 /* make function no-op when possible */
584 if (NET_IP_ALIGN == 0 || len < sizeof(*hdr))
587 #if NET_IP_ALIGN != 0
588 /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
589 /* TODO: deduplicate common code, define helper function instead? */
591 if (ieee80211_is_data_qos(hdr->frame_control)) {
592 u8 *qc = ieee80211_get_qos_ctl(hdr);
594 padding ^= NET_IP_ALIGN;
596 /* Input might be invalid, avoid accessing memory outside
599 if ((unsigned long)qc - (unsigned long)hdr < len &&
600 *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
601 padding ^= NET_IP_ALIGN;
604 if (ieee80211_has_a4(hdr->frame_control))
605 padding ^= NET_IP_ALIGN;
611 #define __RADIO_TAP_SIZE_RSV 32
613 static void _rtl_rx_completed(struct urb *_urb)
615 struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
616 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
617 struct rtl_priv *rtlpriv = rtl_priv(hw);
620 if (unlikely(IS_USB_STOP(rtlusb)))
623 if (likely(0 == _urb->status)) {
624 unsigned int padding;
627 unsigned int size = _urb->actual_length;
628 struct ieee80211_hdr *hdr;
630 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
631 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
632 "Too short packet from bulk IN! (len: %d)\n",
637 qlen = skb_queue_len(&rtlusb->rx_queue);
638 if (qlen >= __RX_SKB_MAX_QUEUED) {
639 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
640 "Pending RX skbuff queue full! (qlen: %d)\n",
645 hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE);
646 padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE);
648 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding);
650 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
651 "Can't allocate skb for bulk IN!\n");
655 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
657 /* Make sure the payload data is 4 byte aligned. */
658 skb_reserve(skb, padding);
660 /* reserve some space for mac80211's radiotap */
661 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
663 memcpy(skb_put(skb, size), _urb->transfer_buffer, size);
665 skb_queue_tail(&rtlusb->rx_queue, skb);
666 tasklet_schedule(&rtlusb->rx_work_tasklet);
671 switch (_urb->status) {
683 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
684 err = usb_submit_urb(_urb, GFP_ATOMIC);
686 usb_unanchor_urb(_urb);
692 /* On some architectures, usb_free_coherent must not be called from
693 * hardirq context. Queue urb to cleanup list.
695 usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
698 #undef __RADIO_TAP_SIZE_RSV
700 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
702 struct rtl_priv *rtlpriv = rtl_priv(hw);
703 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
706 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
708 tasklet_kill(&rtlusb->rx_work_tasklet);
709 cancel_work_sync(&rtlpriv->works.lps_change_work);
711 flush_workqueue(rtlpriv->works.rtl_wq);
712 destroy_workqueue(rtlpriv->works.rtl_wq);
714 skb_queue_purge(&rtlusb->rx_queue);
716 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
717 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
718 urb->transfer_buffer, urb->transfer_dma);
723 static int _rtl_usb_receive(struct ieee80211_hw *hw)
728 struct rtl_priv *rtlpriv = rtl_priv(hw);
729 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
731 WARN_ON(0 == rtlusb->rx_urb_num);
732 /* 1600 == 1514 + max WLAN header + rtk info */
733 WARN_ON(rtlusb->rx_max_size < 1600);
735 for (i = 0; i < rtlusb->rx_urb_num; i++) {
737 urb = usb_alloc_urb(0, GFP_KERNEL);
741 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
743 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
744 "Failed to prep_rx_urb!!\n");
749 usb_anchor_urb(urb, &rtlusb->rx_submitted);
750 err = usb_submit_urb(urb, GFP_KERNEL);
758 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
759 _rtl_usb_cleanup_rx(hw);
763 static int rtl_usb_start(struct ieee80211_hw *hw)
766 struct rtl_priv *rtlpriv = rtl_priv(hw);
767 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
768 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
770 err = rtlpriv->cfg->ops->hw_init(hw);
772 rtl_init_rx_config(hw);
774 /* Enable software */
775 SET_USB_START(rtlusb);
776 /* should after adapter start and interrupt enable. */
777 set_hal_start(rtlhal);
780 err = _rtl_usb_receive(hw);
790 /*======================= tx =========================================*/
791 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
794 struct sk_buff *_skb;
795 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
796 struct ieee80211_tx_info *txinfo;
798 /* clean up rx stuff. */
799 _rtl_usb_cleanup_rx(hw);
801 /* clean up tx stuff */
802 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
803 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
804 rtlusb->usb_tx_cleanup(hw, _skb);
805 txinfo = IEEE80211_SKB_CB(_skb);
806 ieee80211_tx_info_clear_status(txinfo);
807 txinfo->flags |= IEEE80211_TX_STAT_ACK;
808 ieee80211_tx_status_irqsafe(hw, _skb);
810 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
812 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
817 * We may add some struct into struct rtl_usb later. Do deinit here.
820 static void rtl_usb_deinit(struct ieee80211_hw *hw)
825 static void rtl_usb_stop(struct ieee80211_hw *hw)
827 struct rtl_priv *rtlpriv = rtl_priv(hw);
828 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
829 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
831 /* should after adapter start and interrupt enable. */
832 set_hal_stop(rtlhal);
833 cancel_work_sync(&rtlpriv->works.fill_h2c_cmd);
834 /* Enable software */
835 SET_USB_STOP(rtlusb);
836 rtlpriv->cfg->ops->hw_disable(hw);
839 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
842 struct rtl_priv *rtlpriv = rtl_priv(hw);
843 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
845 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
846 err = usb_submit_urb(_urb, GFP_ATOMIC);
850 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
851 "Failed to submit urb\n");
852 usb_unanchor_urb(_urb);
853 skb = (struct sk_buff *)_urb->context;
859 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
862 struct rtl_priv *rtlpriv = rtl_priv(hw);
863 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
864 struct ieee80211_tx_info *txinfo;
866 rtlusb->usb_tx_post_hdl(hw, urb, skb);
867 skb_pull(skb, RTL_TX_HEADER_SIZE);
868 txinfo = IEEE80211_SKB_CB(skb);
869 ieee80211_tx_info_clear_status(txinfo);
870 txinfo->flags |= IEEE80211_TX_STAT_ACK;
873 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
874 "Urb has error status 0x%X\n", urb->status);
877 /* TODO: statistics */
879 ieee80211_tx_status_irqsafe(hw, skb);
883 static void _rtl_tx_complete(struct urb *urb)
885 struct sk_buff *skb = (struct sk_buff *)urb->context;
886 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
887 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
888 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
891 if (unlikely(IS_USB_STOP(rtlusb)))
893 err = _usb_tx_post(hw, urb, skb);
895 /* Ignore error and keep issuiing other urbs */
900 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
901 struct sk_buff *skb, u32 ep_num)
903 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
906 WARN_ON(NULL == skb);
907 _urb = usb_alloc_urb(0, GFP_ATOMIC);
912 _rtl_install_trx_info(rtlusb, skb, ep_num);
913 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
914 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
915 _urb->transfer_flags |= URB_ZERO_PACKET;
919 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
922 struct rtl_priv *rtlpriv = rtl_priv(hw);
923 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
925 struct urb *_urb = NULL;
926 struct sk_buff *_skb = NULL;
928 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
929 if (unlikely(IS_USB_STOP(rtlusb))) {
930 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
931 "USB device is stopping...\n");
935 ep_num = rtlusb->ep_map.ep_mapping[qnum];
937 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
938 if (unlikely(!_urb)) {
939 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
940 "Can't allocate urb. Drop skb!\n");
944 _rtl_submit_tx_urb(hw, _urb);
947 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
948 struct ieee80211_sta *sta,
952 struct rtl_priv *rtlpriv = rtl_priv(hw);
953 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
954 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
955 struct rtl_tx_desc *pdesc = NULL;
956 struct rtl_tcb_desc tcb_desc;
957 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
958 __le16 fc = hdr->frame_control;
959 u8 *pda_addr = hdr->addr1;
965 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
966 if (ieee80211_is_auth(fc)) {
967 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
971 if (rtlpriv->psc.sw_ps_enabled) {
972 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
973 !ieee80211_has_pm(fc))
974 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
977 rtl_action_proc(hw, skb, true);
978 if (is_multicast_ether_addr(pda_addr))
979 rtlpriv->stats.txbytesmulticast += skb->len;
980 else if (is_broadcast_ether_addr(pda_addr))
981 rtlpriv->stats.txbytesbroadcast += skb->len;
983 rtlpriv->stats.txbytesunicast += skb->len;
984 if (ieee80211_is_data_qos(fc)) {
985 qc = ieee80211_get_qos_ctl(hdr);
986 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
987 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
988 IEEE80211_SCTL_SEQ) >> 4;
992 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, NULL, info, sta, skb,
993 hw_queue, &tcb_desc);
994 if (!ieee80211_has_morefrags(hdr->frame_control)) {
996 mac->tids[tid].seq_number = seq_number;
998 if (ieee80211_is_data(fc))
999 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
1002 static int rtl_usb_tx(struct ieee80211_hw *hw,
1003 struct ieee80211_sta *sta,
1004 struct sk_buff *skb,
1005 struct rtl_tcb_desc *dummy)
1007 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1008 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1009 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
1010 __le16 fc = hdr->frame_control;
1013 if (unlikely(is_hal_stop(rtlhal)))
1015 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
1016 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
1017 _rtl_usb_transmit(hw, skb, hw_queue);
1018 return NETDEV_TX_OK;
1021 dev_kfree_skb_any(skb);
1022 return NETDEV_TX_OK;
1025 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
1026 struct ieee80211_sta *sta,
1027 struct sk_buff *skb)
1032 static void rtl_fill_h2c_cmd_work_callback(struct work_struct *work)
1034 struct rtl_works *rtlworks =
1035 container_of(work, struct rtl_works, fill_h2c_cmd);
1036 struct ieee80211_hw *hw = rtlworks->hw;
1037 struct rtl_priv *rtlpriv = rtl_priv(hw);
1039 rtlpriv->cfg->ops->fill_h2c_cmd(hw, H2C_RA_MASK, 5, rtlpriv->rate_mask);
1042 static const struct rtl_intf_ops rtl_usb_ops = {
1043 .adapter_start = rtl_usb_start,
1044 .adapter_stop = rtl_usb_stop,
1045 .adapter_tx = rtl_usb_tx,
1046 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
1049 int rtl_usb_probe(struct usb_interface *intf,
1050 const struct usb_device_id *id,
1051 struct rtl_hal_cfg *rtl_hal_cfg)
1054 struct ieee80211_hw *hw = NULL;
1055 struct rtl_priv *rtlpriv = NULL;
1056 struct usb_device *udev;
1057 struct rtl_usb_priv *usb_priv;
1059 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
1060 sizeof(struct rtl_usb_priv), &rtl_ops);
1062 RT_ASSERT(false, "ieee80211 alloc failed\n");
1066 rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32),
1068 if (!rtlpriv->usb_data)
1071 /* this spin lock must be initialized early */
1072 spin_lock_init(&rtlpriv->locks.usb_lock);
1073 INIT_WORK(&rtlpriv->works.fill_h2c_cmd,
1074 rtl_fill_h2c_cmd_work_callback);
1075 INIT_WORK(&rtlpriv->works.lps_change_work,
1076 rtl_lps_change_work_callback);
1078 rtlpriv->usb_data_index = 0;
1079 init_completion(&rtlpriv->firmware_loading_complete);
1080 SET_IEEE80211_DEV(hw, &intf->dev);
1081 udev = interface_to_usbdev(intf);
1083 usb_priv = rtl_usbpriv(hw);
1084 memset(usb_priv, 0, sizeof(*usb_priv));
1085 usb_priv->dev.intf = intf;
1086 usb_priv->dev.udev = udev;
1087 usb_set_intfdata(intf, hw);
1088 /* init cfg & intf_ops */
1089 rtlpriv->rtlhal.interface = INTF_USB;
1090 rtlpriv->cfg = rtl_hal_cfg;
1091 rtlpriv->intf_ops = &rtl_usb_ops;
1092 rtl_dbgp_flag_init(hw);
1093 /* Init IO handler */
1094 _rtl_usb_io_handler_init(&udev->dev, hw);
1095 rtlpriv->cfg->ops->read_chip_version(hw);
1096 /*like read eeprom and so on */
1097 rtlpriv->cfg->ops->read_eeprom_info(hw);
1098 err = _rtl_usb_init(hw);
1101 rtl_usb_init_sw(hw);
1102 /* Init mac80211 sw */
1103 err = rtl_init_core(hw);
1105 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1106 "Can't allocate sw for mac80211\n");
1109 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1110 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n");
1113 rtlpriv->cfg->ops->init_sw_leds(hw);
1115 err = ieee80211_register_hw(hw);
1117 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1118 "Can't register mac80211 hw.\n");
1122 rtlpriv->mac80211.mac80211_registered = 1;
1124 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1128 rtl_deinit_core(hw);
1129 _rtl_usb_io_handler_release(hw);
1131 complete(&rtlpriv->firmware_loading_complete);
1134 EXPORT_SYMBOL(rtl_usb_probe);
1136 void rtl_usb_disconnect(struct usb_interface *intf)
1138 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1139 struct rtl_priv *rtlpriv = rtl_priv(hw);
1140 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1141 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1143 if (unlikely(!rtlpriv))
1145 /* just in case driver is removed before firmware callback */
1146 wait_for_completion(&rtlpriv->firmware_loading_complete);
1147 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1148 /*ieee80211_unregister_hw will call ops_stop */
1149 if (rtlmac->mac80211_registered == 1) {
1150 ieee80211_unregister_hw(hw);
1151 rtlmac->mac80211_registered = 0;
1153 rtl_deinit_deferred_work(hw);
1154 rtlpriv->intf_ops->adapter_stop(hw);
1157 /* rtl_deinit_rfkill(hw); */
1159 rtl_deinit_core(hw);
1160 kfree(rtlpriv->usb_data);
1161 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1162 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1163 _rtl_usb_io_handler_release(hw);
1164 usb_put_dev(rtlusb->udev);
1165 usb_set_intfdata(intf, NULL);
1166 ieee80211_free_hw(hw);
1168 EXPORT_SYMBOL(rtl_usb_disconnect);
1170 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1174 EXPORT_SYMBOL(rtl_usb_suspend);
1176 int rtl_usb_resume(struct usb_interface *pusb_intf)
1180 EXPORT_SYMBOL(rtl_usb_resume);