sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / net / wireless / rtlwifi / usb.c
bloba9367eba1ea7502d60787c6850859305779ac0be
1 /******************************************************************************
3 * Copyright(c) 2009-2011 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
12 * more details.
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 *****************************************************************************/
27 #include <linux/usb.h>
28 #include "core.h"
29 #include "wifi.h"
30 #include "usb.h"
31 #include "base.h"
32 #include "ps.h"
34 #define REALTEK_USB_VENQT_READ 0xC0
35 #define REALTEK_USB_VENQT_WRITE 0x40
36 #define REALTEK_USB_VENQT_CMD_REQ 0x05
37 #define REALTEK_USB_VENQT_CMD_IDX 0x00
39 #define REALTEK_USB_VENQT_MAX_BUF_SIZE 254
41 static void usbctrl_async_callback(struct urb *urb)
43 if (urb)
44 kfree(urb->context);
47 static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
48 u16 value, u16 index, void *pdata,
49 u16 len)
51 int rc;
52 unsigned int pipe;
53 u8 reqtype;
54 struct usb_ctrlrequest *dr;
55 struct urb *urb;
56 struct rtl819x_async_write_data {
57 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
58 struct usb_ctrlrequest dr;
59 } *buf;
61 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
62 reqtype = REALTEK_USB_VENQT_WRITE;
64 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
65 if (!buf)
66 return -ENOMEM;
68 urb = usb_alloc_urb(0, GFP_ATOMIC);
69 if (!urb) {
70 kfree(buf);
71 return -ENOMEM;
74 dr = &buf->dr;
76 dr->bRequestType = reqtype;
77 dr->bRequest = request;
78 dr->wValue = cpu_to_le16(value);
79 dr->wIndex = cpu_to_le16(index);
80 dr->wLength = cpu_to_le16(len);
81 memcpy(buf, pdata, len);
82 usb_fill_control_urb(urb, udev, pipe,
83 (unsigned char *)dr, buf, len,
84 usbctrl_async_callback, buf);
85 rc = usb_submit_urb(urb, GFP_ATOMIC);
86 if (rc < 0)
87 kfree(buf);
88 usb_free_urb(urb);
89 return rc;
92 static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
93 u16 value, u16 index, void *pdata,
94 u16 len)
96 unsigned int pipe;
97 int status;
98 u8 reqtype;
100 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
101 reqtype = REALTEK_USB_VENQT_READ;
103 status = usb_control_msg(udev, pipe, request, reqtype, value, index,
104 pdata, len, 0); /* max. timeout */
106 if (status < 0)
107 printk(KERN_ERR "reg 0x%x, usbctrl_vendorreq TimeOut! "
108 "status:0x%x value=0x%x\n", value, status,
109 *(u32 *)pdata);
110 return status;
113 static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
115 u8 request;
116 u16 wvalue;
117 u16 index;
118 u32 *data;
119 u32 ret;
121 data = kmalloc(sizeof(u32), GFP_KERNEL);
122 if (!data)
123 return -ENOMEM;
124 request = REALTEK_USB_VENQT_CMD_REQ;
125 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
127 wvalue = (u16)addr;
128 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
129 ret = *data;
130 kfree(data);
131 return ret;
134 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
136 struct device *dev = rtlpriv->io.dev;
138 return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
141 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
143 struct device *dev = rtlpriv->io.dev;
145 return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
148 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
150 struct device *dev = rtlpriv->io.dev;
152 return _usb_read_sync(to_usb_device(dev), addr, 4);
155 static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
156 u16 len)
158 u8 request;
159 u16 wvalue;
160 u16 index;
161 u32 data;
163 request = REALTEK_USB_VENQT_CMD_REQ;
164 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
165 wvalue = (u16)(addr&0x0000ffff);
166 data = val;
167 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
168 len);
171 static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
173 struct device *dev = rtlpriv->io.dev;
175 _usb_write_async(to_usb_device(dev), addr, val, 1);
178 static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
180 struct device *dev = rtlpriv->io.dev;
182 _usb_write_async(to_usb_device(dev), addr, val, 2);
185 static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
187 struct device *dev = rtlpriv->io.dev;
189 _usb_write_async(to_usb_device(dev), addr, val, 4);
192 static int _usb_nbytes_read_write(struct usb_device *udev, bool read, u32 addr,
193 u16 len, u8 *pdata)
195 int status;
196 u8 request;
197 u16 wvalue;
198 u16 index;
200 request = REALTEK_USB_VENQT_CMD_REQ;
201 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
202 wvalue = (u16)addr;
203 if (read)
204 status = _usbctrl_vendorreq_sync_read(udev, request, wvalue,
205 index, pdata, len);
206 else
207 status = _usbctrl_vendorreq_async_write(udev, request, wvalue,
208 index, pdata, len);
209 return status;
212 static int _usb_readN_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len,
213 u8 *pdata)
215 struct device *dev = rtlpriv->io.dev;
217 return _usb_nbytes_read_write(to_usb_device(dev), true, addr, len,
218 pdata);
221 static int _usb_writeN_async(struct rtl_priv *rtlpriv, u32 addr, u16 len,
222 u8 *pdata)
224 struct device *dev = rtlpriv->io.dev;
226 return _usb_nbytes_read_write(to_usb_device(dev), false, addr, len,
227 pdata);
230 static void _rtl_usb_io_handler_init(struct device *dev,
231 struct ieee80211_hw *hw)
233 struct rtl_priv *rtlpriv = rtl_priv(hw);
235 rtlpriv->io.dev = dev;
236 mutex_init(&rtlpriv->io.bb_mutex);
237 rtlpriv->io.write8_async = _usb_write8_async;
238 rtlpriv->io.write16_async = _usb_write16_async;
239 rtlpriv->io.write32_async = _usb_write32_async;
240 rtlpriv->io.writeN_async = _usb_writeN_async;
241 rtlpriv->io.read8_sync = _usb_read8_sync;
242 rtlpriv->io.read16_sync = _usb_read16_sync;
243 rtlpriv->io.read32_sync = _usb_read32_sync;
244 rtlpriv->io.readN_sync = _usb_readN_sync;
247 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
249 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
251 mutex_destroy(&rtlpriv->io.bb_mutex);
256 * Default aggregation handler. Do nothing and just return the oldest skb.
258 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
259 struct sk_buff_head *list)
261 return skb_dequeue(list);
264 #define IS_HIGH_SPEED_USB(udev) \
265 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
267 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
269 u32 i;
270 struct rtl_priv *rtlpriv = rtl_priv(hw);
271 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
273 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
274 ? USB_HIGH_SPEED_BULK_SIZE
275 : USB_FULL_SPEED_BULK_SIZE;
277 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
278 rtlusb->max_bulk_out_size));
280 for (i = 0; i < __RTL_TXQ_NUM; i++) {
281 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
282 if (!ep_num) {
283 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
284 ("Invalid endpoint map setting!\n"));
285 return -EINVAL;
289 rtlusb->usb_tx_post_hdl =
290 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
291 rtlusb->usb_tx_cleanup =
292 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
293 rtlusb->usb_tx_aggregate_hdl =
294 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
295 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
296 : &_none_usb_tx_aggregate_hdl;
298 init_usb_anchor(&rtlusb->tx_submitted);
299 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
300 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
301 init_usb_anchor(&rtlusb->tx_pending[i]);
303 return 0;
306 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
308 struct rtl_priv *rtlpriv = rtl_priv(hw);
309 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
310 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
312 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
313 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
314 rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num;
315 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
316 rtlusb->usb_rx_segregate_hdl =
317 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
319 printk(KERN_INFO "rtl8192cu: rx_max_size %d, rx_urb_num %d, in_ep %d\n",
320 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
321 init_usb_anchor(&rtlusb->rx_submitted);
322 return 0;
325 static int _rtl_usb_init(struct ieee80211_hw *hw)
327 struct rtl_priv *rtlpriv = rtl_priv(hw);
328 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
329 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
330 int err;
331 u8 epidx;
332 struct usb_interface *usb_intf = rtlusb->intf;
333 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
335 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
336 for (epidx = 0; epidx < epnums; epidx++) {
337 struct usb_endpoint_descriptor *pep_desc;
338 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
340 if (usb_endpoint_dir_in(pep_desc))
341 rtlusb->in_ep_nums++;
342 else if (usb_endpoint_dir_out(pep_desc))
343 rtlusb->out_ep_nums++;
345 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
346 ("USB EP(0x%02x), MaxPacketSize=%d ,Interval=%d.\n",
347 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
348 pep_desc->bInterval));
350 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
351 return -EINVAL ;
353 /* usb endpoint mapping */
354 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
355 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
356 _rtl_usb_init_tx(hw);
357 _rtl_usb_init_rx(hw);
358 return err;
361 static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
363 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
364 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
365 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
366 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
368 rtlhal->hw = hw;
369 ppsc->inactiveps = false;
370 ppsc->leisure_ps = false;
371 ppsc->fwctrl_lps = false;
372 ppsc->reg_fwctrl_lps = 3;
373 ppsc->reg_max_lps_awakeintvl = 5;
374 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
376 /* IBSS */
377 mac->beacon_interval = 100;
379 /* AMPDU */
380 mac->min_space_cfg = 0;
381 mac->max_mss_density = 0;
383 /* set sane AMPDU defaults */
384 mac->current_ampdu_density = 7;
385 mac->current_ampdu_factor = 3;
387 /* QOS */
388 rtlusb->acm_method = eAcmWay2_SW;
390 /* IRQ */
391 /* HIMR - turn all on */
392 rtlusb->irq_mask[0] = 0xFFFFFFFF;
393 /* HIMR_EX - turn all on */
394 rtlusb->irq_mask[1] = 0xFFFFFFFF;
395 rtlusb->disableHWSM = true;
396 return 0;
399 #define __RADIO_TAP_SIZE_RSV 32
401 static void _rtl_rx_completed(struct urb *urb);
403 static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw,
404 struct rtl_usb *rtlusb,
405 struct urb *urb,
406 gfp_t gfp_mask)
408 struct sk_buff *skb;
409 struct rtl_priv *rtlpriv = rtl_priv(hw);
411 skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV),
412 gfp_mask);
413 if (!skb) {
414 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
415 ("Failed to __dev_alloc_skb!!\n"))
416 return ERR_PTR(-ENOMEM);
419 /* reserve some space for mac80211's radiotap */
420 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
421 usb_fill_bulk_urb(urb, rtlusb->udev,
422 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
423 skb->data, min(skb_tailroom(skb),
424 (int)rtlusb->rx_max_size),
425 _rtl_rx_completed, skb);
427 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
428 return skb;
431 #undef __RADIO_TAP_SIZE_RSV
433 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
434 struct sk_buff *skb)
436 struct rtl_priv *rtlpriv = rtl_priv(hw);
437 u8 *rxdesc = skb->data;
438 struct ieee80211_hdr *hdr;
439 bool unicast = false;
440 __le16 fc;
441 struct ieee80211_rx_status rx_status = {0};
442 struct rtl_stats stats = {
443 .signal = 0,
444 .noise = -98,
445 .rate = 0,
448 skb_pull(skb, RTL_RX_DESC_SIZE);
449 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
450 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
451 hdr = (struct ieee80211_hdr *)(skb->data);
452 fc = hdr->frame_control;
453 if (!stats.crc) {
454 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
456 if (is_broadcast_ether_addr(hdr->addr1)) {
457 /*TODO*/;
458 } else if (is_multicast_ether_addr(hdr->addr1)) {
459 /*TODO*/
460 } else {
461 unicast = true;
462 rtlpriv->stats.rxbytesunicast += skb->len;
465 rtl_is_special_data(hw, skb, false);
467 if (ieee80211_is_data(fc)) {
468 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
470 if (unicast)
471 rtlpriv->link_info.num_rx_inperiod++;
476 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
477 struct sk_buff *skb)
479 struct rtl_priv *rtlpriv = rtl_priv(hw);
480 u8 *rxdesc = skb->data;
481 struct ieee80211_hdr *hdr;
482 bool unicast = false;
483 __le16 fc;
484 struct ieee80211_rx_status rx_status = {0};
485 struct rtl_stats stats = {
486 .signal = 0,
487 .noise = -98,
488 .rate = 0,
491 skb_pull(skb, RTL_RX_DESC_SIZE);
492 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
493 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
494 hdr = (struct ieee80211_hdr *)(skb->data);
495 fc = hdr->frame_control;
496 if (!stats.crc) {
497 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
499 if (is_broadcast_ether_addr(hdr->addr1)) {
500 /*TODO*/;
501 } else if (is_multicast_ether_addr(hdr->addr1)) {
502 /*TODO*/
503 } else {
504 unicast = true;
505 rtlpriv->stats.rxbytesunicast += skb->len;
508 rtl_is_special_data(hw, skb, false);
510 if (ieee80211_is_data(fc)) {
511 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
513 if (unicast)
514 rtlpriv->link_info.num_rx_inperiod++;
516 if (likely(rtl_action_proc(hw, skb, false))) {
517 struct sk_buff *uskb = NULL;
518 u8 *pdata;
520 uskb = dev_alloc_skb(skb->len + 128);
521 memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status,
522 sizeof(rx_status));
523 pdata = (u8 *)skb_put(uskb, skb->len);
524 memcpy(pdata, skb->data, skb->len);
525 dev_kfree_skb_any(skb);
526 ieee80211_rx_irqsafe(hw, uskb);
527 } else {
528 dev_kfree_skb_any(skb);
533 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
535 struct sk_buff *_skb;
536 struct sk_buff_head rx_queue;
537 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
539 skb_queue_head_init(&rx_queue);
540 if (rtlusb->usb_rx_segregate_hdl)
541 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
542 WARN_ON(skb_queue_empty(&rx_queue));
543 while (!skb_queue_empty(&rx_queue)) {
544 _skb = skb_dequeue(&rx_queue);
545 _rtl_usb_rx_process_agg(hw, skb);
546 ieee80211_rx_irqsafe(hw, skb);
550 static void _rtl_rx_completed(struct urb *_urb)
552 struct sk_buff *skb = (struct sk_buff *)_urb->context;
553 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
554 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
555 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
556 struct rtl_priv *rtlpriv = rtl_priv(hw);
557 int err = 0;
559 if (unlikely(IS_USB_STOP(rtlusb)))
560 goto free;
562 if (likely(0 == _urb->status)) {
563 /* If this code were moved to work queue, would CPU
564 * utilization be improved? NOTE: We shall allocate another skb
565 * and reuse the original one.
567 skb_put(skb, _urb->actual_length);
569 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
570 struct sk_buff *_skb;
571 _rtl_usb_rx_process_noagg(hw, skb);
572 _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC);
573 if (IS_ERR(_skb)) {
574 err = PTR_ERR(_skb);
575 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
576 ("Can't allocate skb for bulk IN!\n"));
577 return;
579 skb = _skb;
580 } else{
581 /* TO DO */
582 _rtl_rx_pre_process(hw, skb);
583 printk(KERN_ERR "rtlwifi: rx agg not supported\n");
585 goto resubmit;
588 switch (_urb->status) {
589 /* disconnect */
590 case -ENOENT:
591 case -ECONNRESET:
592 case -ENODEV:
593 case -ESHUTDOWN:
594 goto free;
595 default:
596 break;
599 resubmit:
600 skb_reset_tail_pointer(skb);
601 skb_trim(skb, 0);
603 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
604 err = usb_submit_urb(_urb, GFP_ATOMIC);
605 if (unlikely(err)) {
606 usb_unanchor_urb(_urb);
607 goto free;
609 return;
611 free:
612 dev_kfree_skb_irq(skb);
615 static int _rtl_usb_receive(struct ieee80211_hw *hw)
617 struct urb *urb;
618 struct sk_buff *skb;
619 int err;
620 int i;
621 struct rtl_priv *rtlpriv = rtl_priv(hw);
622 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
624 WARN_ON(0 == rtlusb->rx_urb_num);
625 /* 1600 == 1514 + max WLAN header + rtk info */
626 WARN_ON(rtlusb->rx_max_size < 1600);
628 for (i = 0; i < rtlusb->rx_urb_num; i++) {
629 err = -ENOMEM;
630 urb = usb_alloc_urb(0, GFP_KERNEL);
631 if (!urb) {
632 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
633 ("Failed to alloc URB!!\n"))
634 goto err_out;
637 skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
638 if (IS_ERR(skb)) {
639 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
640 ("Failed to prep_rx_urb!!\n"))
641 err = PTR_ERR(skb);
642 goto err_out;
645 usb_anchor_urb(urb, &rtlusb->rx_submitted);
646 err = usb_submit_urb(urb, GFP_KERNEL);
647 if (err)
648 goto err_out;
649 usb_free_urb(urb);
651 return 0;
653 err_out:
654 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
655 return err;
658 static int rtl_usb_start(struct ieee80211_hw *hw)
660 int err;
661 struct rtl_priv *rtlpriv = rtl_priv(hw);
662 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
663 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
665 err = rtlpriv->cfg->ops->hw_init(hw);
666 rtl_init_rx_config(hw);
668 /* Enable software */
669 SET_USB_START(rtlusb);
670 /* should after adapter start and interrupt enable. */
671 set_hal_start(rtlhal);
673 /* Start bulk IN */
674 _rtl_usb_receive(hw);
676 return err;
683 /*======================= tx =========================================*/
684 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
686 u32 i;
687 struct sk_buff *_skb;
688 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
689 struct ieee80211_tx_info *txinfo;
691 SET_USB_STOP(rtlusb);
693 /* clean up rx stuff. */
694 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
696 /* clean up tx stuff */
697 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
698 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
699 rtlusb->usb_tx_cleanup(hw, _skb);
700 txinfo = IEEE80211_SKB_CB(_skb);
701 ieee80211_tx_info_clear_status(txinfo);
702 txinfo->flags |= IEEE80211_TX_STAT_ACK;
703 ieee80211_tx_status_irqsafe(hw, _skb);
705 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
707 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
712 * We may add some struct into struct rtl_usb later. Do deinit here.
715 static void rtl_usb_deinit(struct ieee80211_hw *hw)
717 rtl_usb_cleanup(hw);
720 static void rtl_usb_stop(struct ieee80211_hw *hw)
722 struct rtl_priv *rtlpriv = rtl_priv(hw);
723 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
724 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
726 /* should after adapter start and interrupt enable. */
727 set_hal_stop(rtlhal);
728 /* Enable software */
729 SET_USB_STOP(rtlusb);
730 rtl_usb_deinit(hw);
731 rtlpriv->cfg->ops->hw_disable(hw);
734 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
736 int err;
737 struct rtl_priv *rtlpriv = rtl_priv(hw);
738 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
740 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
741 err = usb_submit_urb(_urb, GFP_ATOMIC);
742 if (err < 0) {
743 struct sk_buff *skb;
745 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
746 ("Failed to submit urb.\n"));
747 usb_unanchor_urb(_urb);
748 skb = (struct sk_buff *)_urb->context;
749 kfree_skb(skb);
751 usb_free_urb(_urb);
754 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
755 struct sk_buff *skb)
757 struct rtl_priv *rtlpriv = rtl_priv(hw);
758 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
759 struct ieee80211_tx_info *txinfo;
761 rtlusb->usb_tx_post_hdl(hw, urb, skb);
762 skb_pull(skb, RTL_TX_HEADER_SIZE);
763 txinfo = IEEE80211_SKB_CB(skb);
764 ieee80211_tx_info_clear_status(txinfo);
765 txinfo->flags |= IEEE80211_TX_STAT_ACK;
767 if (urb->status) {
768 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
769 ("Urb has error status 0x%X\n", urb->status));
770 goto out;
772 /* TODO: statistics */
773 out:
774 ieee80211_tx_status_irqsafe(hw, skb);
775 return urb->status;
778 static void _rtl_tx_complete(struct urb *urb)
780 struct sk_buff *skb = (struct sk_buff *)urb->context;
781 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
782 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
783 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
784 int err;
786 if (unlikely(IS_USB_STOP(rtlusb)))
787 return;
788 err = _usb_tx_post(hw, urb, skb);
789 if (err) {
790 /* Ignore error and keep issuiing other urbs */
791 return;
795 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
796 struct sk_buff *skb, u32 ep_num)
798 struct rtl_priv *rtlpriv = rtl_priv(hw);
799 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
800 struct urb *_urb;
802 WARN_ON(NULL == skb);
803 _urb = usb_alloc_urb(0, GFP_ATOMIC);
804 if (!_urb) {
805 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
806 ("Can't allocate URB for bulk out!\n"));
807 kfree_skb(skb);
808 return NULL;
810 _rtl_install_trx_info(rtlusb, skb, ep_num);
811 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
812 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
813 _urb->transfer_flags |= URB_ZERO_PACKET;
814 return _urb;
817 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
818 enum rtl_txq qnum)
820 struct rtl_priv *rtlpriv = rtl_priv(hw);
821 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
822 u32 ep_num;
823 struct urb *_urb = NULL;
824 struct sk_buff *_skb = NULL;
825 struct sk_buff_head *skb_list;
826 struct usb_anchor *urb_list;
828 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
829 if (unlikely(IS_USB_STOP(rtlusb))) {
830 RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
831 ("USB device is stopping...\n"));
832 kfree_skb(skb);
833 return;
835 ep_num = rtlusb->ep_map.ep_mapping[qnum];
836 skb_list = &rtlusb->tx_skb_queue[ep_num];
837 _skb = skb;
838 _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num);
839 if (unlikely(!_urb)) {
840 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
841 ("Can't allocate urb. Drop skb!\n"));
842 return;
844 urb_list = &rtlusb->tx_pending[ep_num];
845 _rtl_submit_tx_urb(hw, _urb);
848 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
849 u16 hw_queue)
851 struct rtl_priv *rtlpriv = rtl_priv(hw);
852 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
853 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
854 struct rtl_tx_desc *pdesc = NULL;
855 struct rtl_tcb_desc tcb_desc;
856 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
857 __le16 fc = hdr->frame_control;
858 u8 *pda_addr = hdr->addr1;
859 /* ssn */
860 u8 *qc = NULL;
861 u8 tid = 0;
862 u16 seq_number = 0;
864 if (ieee80211_is_auth(fc)) {
865 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
866 rtl_ips_nic_on(hw);
869 if (rtlpriv->psc.sw_ps_enabled) {
870 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
871 !ieee80211_has_pm(fc))
872 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
875 rtl_action_proc(hw, skb, true);
876 if (is_multicast_ether_addr(pda_addr))
877 rtlpriv->stats.txbytesmulticast += skb->len;
878 else if (is_broadcast_ether_addr(pda_addr))
879 rtlpriv->stats.txbytesbroadcast += skb->len;
880 else
881 rtlpriv->stats.txbytesunicast += skb->len;
882 if (ieee80211_is_data_qos(fc)) {
883 qc = ieee80211_get_qos_ctl(hdr);
884 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
885 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
886 IEEE80211_SCTL_SEQ) >> 4;
887 seq_number += 1;
888 seq_number <<= 4;
890 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
891 hw_queue, &tcb_desc);
892 if (!ieee80211_has_morefrags(hdr->frame_control)) {
893 if (qc)
894 mac->tids[tid].seq_number = seq_number;
896 if (ieee80211_is_data(fc))
897 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
900 static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
901 struct rtl_tcb_desc *dummy)
903 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
904 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
905 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
906 __le16 fc = hdr->frame_control;
907 u16 hw_queue;
909 if (unlikely(is_hal_stop(rtlhal)))
910 goto err_free;
911 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
912 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
913 _rtl_usb_transmit(hw, skb, hw_queue);
914 return NETDEV_TX_OK;
916 err_free:
917 dev_kfree_skb_any(skb);
918 return NETDEV_TX_OK;
921 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
922 struct sk_buff *skb)
924 return false;
927 static struct rtl_intf_ops rtl_usb_ops = {
928 .adapter_start = rtl_usb_start,
929 .adapter_stop = rtl_usb_stop,
930 .adapter_tx = rtl_usb_tx,
931 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
934 int __devinit rtl_usb_probe(struct usb_interface *intf,
935 const struct usb_device_id *id)
937 int err;
938 struct ieee80211_hw *hw = NULL;
939 struct rtl_priv *rtlpriv = NULL;
940 struct usb_device *udev;
941 struct rtl_usb_priv *usb_priv;
943 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
944 sizeof(struct rtl_usb_priv), &rtl_ops);
945 if (!hw) {
946 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
947 return -ENOMEM;
949 rtlpriv = hw->priv;
950 SET_IEEE80211_DEV(hw, &intf->dev);
951 udev = interface_to_usbdev(intf);
952 usb_get_dev(udev);
953 usb_priv = rtl_usbpriv(hw);
954 memset(usb_priv, 0, sizeof(*usb_priv));
955 usb_priv->dev.intf = intf;
956 usb_priv->dev.udev = udev;
957 usb_set_intfdata(intf, hw);
958 /* init cfg & intf_ops */
959 rtlpriv->rtlhal.interface = INTF_USB;
960 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
961 rtlpriv->intf_ops = &rtl_usb_ops;
962 rtl_dbgp_flag_init(hw);
963 /* Init IO handler */
964 _rtl_usb_io_handler_init(&udev->dev, hw);
965 rtlpriv->cfg->ops->read_chip_version(hw);
966 /*like read eeprom and so on */
967 rtlpriv->cfg->ops->read_eeprom_info(hw);
968 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
969 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
970 ("Can't init_sw_vars.\n"));
971 goto error_out;
973 rtlpriv->cfg->ops->init_sw_leds(hw);
974 err = _rtl_usb_init(hw);
975 err = _rtl_usb_init_sw(hw);
976 /* Init mac80211 sw */
977 err = rtl_init_core(hw);
978 if (err) {
979 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
980 ("Can't allocate sw for mac80211.\n"));
981 goto error_out;
984 /*init rfkill */
985 /* rtl_init_rfkill(hw); */
987 err = ieee80211_register_hw(hw);
988 if (err) {
989 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
990 ("Can't register mac80211 hw.\n"));
991 goto error_out;
992 } else {
993 rtlpriv->mac80211.mac80211_registered = 1;
995 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
996 return 0;
997 error_out:
998 rtl_deinit_core(hw);
999 _rtl_usb_io_handler_release(hw);
1000 ieee80211_free_hw(hw);
1001 usb_put_dev(udev);
1002 return -ENODEV;
1004 EXPORT_SYMBOL(rtl_usb_probe);
1006 void rtl_usb_disconnect(struct usb_interface *intf)
1008 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1009 struct rtl_priv *rtlpriv = rtl_priv(hw);
1010 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1011 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1013 if (unlikely(!rtlpriv))
1014 return;
1015 /*ieee80211_unregister_hw will call ops_stop */
1016 if (rtlmac->mac80211_registered == 1) {
1017 ieee80211_unregister_hw(hw);
1018 rtlmac->mac80211_registered = 0;
1019 } else {
1020 rtl_deinit_deferred_work(hw);
1021 rtlpriv->intf_ops->adapter_stop(hw);
1023 /*deinit rfkill */
1024 /* rtl_deinit_rfkill(hw); */
1025 rtl_usb_deinit(hw);
1026 rtl_deinit_core(hw);
1027 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1028 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1029 _rtl_usb_io_handler_release(hw);
1030 usb_put_dev(rtlusb->udev);
1031 usb_set_intfdata(intf, NULL);
1032 ieee80211_free_hw(hw);
1034 EXPORT_SYMBOL(rtl_usb_disconnect);
1036 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1038 return 0;
1040 EXPORT_SYMBOL(rtl_usb_suspend);
1042 int rtl_usb_resume(struct usb_interface *pusb_intf)
1044 return 0;
1046 EXPORT_SYMBOL(rtl_usb_resume);