George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Larry Finger | a8d7606 | 2012-01-07 20:46:42 -0600 | [diff] [blame] | 3 | * Copyright(c) 2009-2012 Realtek Corporation. All rights reserved. |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 4 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
| 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in the |
| 19 | * file called LICENSE. |
| 20 | * |
| 21 | * Contact Information: |
| 22 | * wlanfae <wlanfae@realtek.com> |
| 23 | * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, |
| 24 | * Hsinchu 300, Taiwan. |
| 25 | * |
| 26 | *****************************************************************************/ |
Joe Perches | 292b119 | 2011-07-20 08:51:35 -0700 | [diff] [blame] | 27 | |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 28 | #include "wifi.h" |
Larry Finger | d273bb2 | 2012-01-27 13:59:25 -0600 | [diff] [blame] | 29 | #include "core.h" |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 30 | #include "usb.h" |
| 31 | #include "base.h" |
| 32 | #include "ps.h" |
George | 040a727 | 2011-11-17 12:14:42 -0600 | [diff] [blame] | 33 | #include "rtl8192c/fw_common.h" |
Larry Finger | d273bb2 | 2012-01-27 13:59:25 -0600 | [diff] [blame] | 34 | #include <linux/export.h> |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 35 | |
| 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 |
| 40 | |
George | 040a727 | 2011-11-17 12:14:42 -0600 | [diff] [blame] | 41 | #define MAX_USBCTRL_VENDORREQ_TIMES 10 |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 42 | |
| 43 | static void usbctrl_async_callback(struct urb *urb) |
| 44 | { |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 45 | if (urb) { |
| 46 | /* free dr */ |
| 47 | kfree(urb->setup_packet); |
| 48 | /* free databuf */ |
| 49 | kfree(urb->transfer_buffer); |
| 50 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request, |
| 54 | u16 value, u16 index, void *pdata, |
| 55 | u16 len) |
| 56 | { |
| 57 | int rc; |
| 58 | unsigned int pipe; |
| 59 | u8 reqtype; |
| 60 | struct usb_ctrlrequest *dr; |
| 61 | struct urb *urb; |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 62 | const u16 databuf_maxlen = REALTEK_USB_VENQT_MAX_BUF_SIZE; |
| 63 | u8 *databuf; |
| 64 | |
| 65 | if (WARN_ON_ONCE(len > databuf_maxlen)) |
| 66 | len = databuf_maxlen; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 67 | |
| 68 | pipe = usb_sndctrlpipe(udev, 0); /* write_out */ |
| 69 | reqtype = REALTEK_USB_VENQT_WRITE; |
| 70 | |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 71 | dr = kmalloc(sizeof(*dr), GFP_ATOMIC); |
| 72 | if (!dr) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 73 | return -ENOMEM; |
| 74 | |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 75 | databuf = kmalloc(databuf_maxlen, GFP_ATOMIC); |
| 76 | if (!databuf) { |
| 77 | kfree(dr); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 78 | return -ENOMEM; |
| 79 | } |
| 80 | |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 81 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 82 | if (!urb) { |
| 83 | kfree(databuf); |
| 84 | kfree(dr); |
| 85 | return -ENOMEM; |
| 86 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 87 | |
| 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); |
Larry Finger | abfabc9 | 2011-11-17 12:14:44 -0600 | [diff] [blame] | 93 | /* data are already in little-endian order */ |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 94 | memcpy(databuf, pdata, len); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 95 | usb_fill_control_urb(urb, udev, pipe, |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 96 | (unsigned char *)dr, databuf, len, |
| 97 | usbctrl_async_callback, NULL); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 98 | rc = usb_submit_urb(urb, GFP_ATOMIC); |
Jussi Kivilinna | bc6b892 | 2013-02-18 10:29:30 +0200 | [diff] [blame] | 99 | if (rc < 0) { |
| 100 | kfree(databuf); |
| 101 | kfree(dr); |
| 102 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 103 | usb_free_urb(urb); |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request, |
| 108 | u16 value, u16 index, void *pdata, |
| 109 | u16 len) |
| 110 | { |
| 111 | unsigned int pipe; |
| 112 | int status; |
| 113 | u8 reqtype; |
George | 040a727 | 2011-11-17 12:14:42 -0600 | [diff] [blame] | 114 | int vendorreq_times = 0; |
Larry Finger | abfabc9 | 2011-11-17 12:14:44 -0600 | [diff] [blame] | 115 | static int count; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 116 | |
| 117 | pipe = usb_rcvctrlpipe(udev, 0); /* read_in */ |
| 118 | reqtype = REALTEK_USB_VENQT_READ; |
| 119 | |
John W. Linville | eb1852b | 2011-11-21 16:37:26 -0500 | [diff] [blame] | 120 | do { |
George | 040a727 | 2011-11-17 12:14:42 -0600 | [diff] [blame] | 121 | status = usb_control_msg(udev, pipe, request, reqtype, value, |
| 122 | index, pdata, len, 0); /*max. timeout*/ |
| 123 | if (status < 0) { |
| 124 | /* firmware download is checksumed, don't retry */ |
| 125 | if ((value >= FW_8192C_START_ADDRESS && |
| 126 | value <= FW_8192C_END_ADDRESS)) |
| 127 | break; |
| 128 | } else { |
| 129 | break; |
| 130 | } |
John W. Linville | eb1852b | 2011-11-21 16:37:26 -0500 | [diff] [blame] | 131 | } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES); |
| 132 | |
Larry Finger | abfabc9 | 2011-11-17 12:14:44 -0600 | [diff] [blame] | 133 | if (status < 0 && count++ < 4) |
Joe Perches | 292b119 | 2011-07-20 08:51:35 -0700 | [diff] [blame] | 134 | pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n", |
Larry Finger | 8e2c406 | 2012-08-31 15:39:00 -0500 | [diff] [blame] | 135 | value, status, *(u32 *)pdata); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 136 | return status; |
| 137 | } |
| 138 | |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 139 | static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 140 | { |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 141 | struct device *dev = rtlpriv->io.dev; |
| 142 | struct usb_device *udev = to_usb_device(dev); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 143 | u8 request; |
| 144 | u16 wvalue; |
| 145 | u16 index; |
Larry Finger | 3ce4d85 | 2012-07-11 14:37:28 -0500 | [diff] [blame] | 146 | __le32 *data; |
| 147 | unsigned long flags; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 148 | |
Larry Finger | 3ce4d85 | 2012-07-11 14:37:28 -0500 | [diff] [blame] | 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); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 154 | request = REALTEK_USB_VENQT_CMD_REQ; |
| 155 | index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */ |
| 156 | |
| 157 | wvalue = (u16)addr; |
| 158 | _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len); |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 159 | return le32_to_cpu(*data); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr) |
| 163 | { |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 164 | return (u8)_usb_read_sync(rtlpriv, addr, 1); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr) |
| 168 | { |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 169 | return (u16)_usb_read_sync(rtlpriv, addr, 2); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr) |
| 173 | { |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 174 | return _usb_read_sync(rtlpriv, addr, 4); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val, |
| 178 | u16 len) |
| 179 | { |
| 180 | u8 request; |
| 181 | u16 wvalue; |
| 182 | u16 index; |
Larry Finger | abfabc9 | 2011-11-17 12:14:44 -0600 | [diff] [blame] | 183 | __le32 data; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 184 | |
| 185 | request = REALTEK_USB_VENQT_CMD_REQ; |
| 186 | index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */ |
| 187 | wvalue = (u16)(addr&0x0000ffff); |
Larry Finger | abfabc9 | 2011-11-17 12:14:44 -0600 | [diff] [blame] | 188 | data = cpu_to_le32(val); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 189 | _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data, |
| 190 | len); |
| 191 | } |
| 192 | |
| 193 | static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val) |
| 194 | { |
| 195 | struct device *dev = rtlpriv->io.dev; |
| 196 | |
| 197 | _usb_write_async(to_usb_device(dev), addr, val, 1); |
| 198 | } |
| 199 | |
| 200 | static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val) |
| 201 | { |
| 202 | struct device *dev = rtlpriv->io.dev; |
| 203 | |
| 204 | _usb_write_async(to_usb_device(dev), addr, val, 2); |
| 205 | } |
| 206 | |
| 207 | static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val) |
| 208 | { |
| 209 | struct device *dev = rtlpriv->io.dev; |
| 210 | |
| 211 | _usb_write_async(to_usb_device(dev), addr, val, 4); |
| 212 | } |
| 213 | |
Larry Finger | ff6ff96 | 2011-11-17 12:14:43 -0600 | [diff] [blame] | 214 | static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data, |
| 215 | u16 len) |
| 216 | { |
| 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; |
| 221 | u16 wvalue; |
| 222 | u16 index = REALTEK_USB_VENQT_CMD_IDX; |
| 223 | int pipe = usb_sndctrlpipe(udev, 0); /* write_out */ |
| 224 | u8 *buffer; |
Larry Finger | ff6ff96 | 2011-11-17 12:14:43 -0600 | [diff] [blame] | 225 | |
Jussi Kivilinna | 4c3de59 | 2012-12-20 16:24:43 +0200 | [diff] [blame] | 226 | wvalue = (u16)(addr & 0x0000ffff); |
| 227 | buffer = kmalloc(len, GFP_ATOMIC); |
Larry Finger | ff6ff96 | 2011-11-17 12:14:43 -0600 | [diff] [blame] | 228 | if (!buffer) |
| 229 | return; |
| 230 | memcpy(buffer, data, len); |
| 231 | usb_control_msg(udev, pipe, request, reqtype, wvalue, |
| 232 | index, buffer, len, 50); |
| 233 | |
Jussi Kivilinna | 4c3de59 | 2012-12-20 16:24:43 +0200 | [diff] [blame] | 234 | kfree(buffer); |
Larry Finger | ff6ff96 | 2011-11-17 12:14:43 -0600 | [diff] [blame] | 235 | } |
| 236 | |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 237 | static void _rtl_usb_io_handler_init(struct device *dev, |
| 238 | struct ieee80211_hw *hw) |
| 239 | { |
| 240 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 241 | |
| 242 | rtlpriv->io.dev = dev; |
| 243 | mutex_init(&rtlpriv->io.bb_mutex); |
| 244 | rtlpriv->io.write8_async = _usb_write8_async; |
| 245 | rtlpriv->io.write16_async = _usb_write16_async; |
| 246 | rtlpriv->io.write32_async = _usb_write32_async; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 247 | rtlpriv->io.read8_sync = _usb_read8_sync; |
| 248 | rtlpriv->io.read16_sync = _usb_read16_sync; |
| 249 | rtlpriv->io.read32_sync = _usb_read32_sync; |
Larry Finger | ff6ff96 | 2011-11-17 12:14:43 -0600 | [diff] [blame] | 250 | rtlpriv->io.writeN_sync = _usb_writeN_sync; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw) |
| 254 | { |
Larry Finger | 2e3e66e | 2011-04-02 18:10:22 -0500 | [diff] [blame] | 255 | struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 256 | |
| 257 | mutex_destroy(&rtlpriv->io.bb_mutex); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * |
| 262 | * Default aggregation handler. Do nothing and just return the oldest skb. |
| 263 | */ |
| 264 | static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw, |
| 265 | struct sk_buff_head *list) |
| 266 | { |
| 267 | return skb_dequeue(list); |
| 268 | } |
| 269 | |
| 270 | #define IS_HIGH_SPEED_USB(udev) \ |
| 271 | ((USB_SPEED_HIGH == (udev)->speed) ? true : false) |
| 272 | |
| 273 | static int _rtl_usb_init_tx(struct ieee80211_hw *hw) |
| 274 | { |
| 275 | u32 i; |
| 276 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 277 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 278 | |
| 279 | rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev) |
| 280 | ? USB_HIGH_SPEED_BULK_SIZE |
| 281 | : USB_FULL_SPEED_BULK_SIZE; |
| 282 | |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 283 | RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n", |
| 284 | rtlusb->max_bulk_out_size); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 285 | |
| 286 | for (i = 0; i < __RTL_TXQ_NUM; i++) { |
| 287 | u32 ep_num = rtlusb->ep_map.ep_mapping[i]; |
| 288 | if (!ep_num) { |
| 289 | RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 290 | "Invalid endpoint map setting!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | rtlusb->usb_tx_post_hdl = |
| 296 | rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl; |
| 297 | rtlusb->usb_tx_cleanup = |
| 298 | rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup; |
| 299 | rtlusb->usb_tx_aggregate_hdl = |
| 300 | (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl) |
| 301 | ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl |
| 302 | : &_none_usb_tx_aggregate_hdl; |
| 303 | |
| 304 | init_usb_anchor(&rtlusb->tx_submitted); |
| 305 | for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) { |
| 306 | skb_queue_head_init(&rtlusb->tx_skb_queue[i]); |
| 307 | init_usb_anchor(&rtlusb->tx_pending[i]); |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int _rtl_usb_init_rx(struct ieee80211_hw *hw) |
| 313 | { |
| 314 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 315 | struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw); |
| 316 | struct rtl_usb *rtlusb = rtl_usbdev(usb_priv); |
| 317 | |
| 318 | rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size; |
| 319 | rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num; |
| 320 | rtlusb->in_ep = rtlpriv->cfg->usb_interface_cfg->in_ep_num; |
| 321 | rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl; |
| 322 | rtlusb->usb_rx_segregate_hdl = |
| 323 | rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl; |
| 324 | |
Joe Perches | 292b119 | 2011-07-20 08:51:35 -0700 | [diff] [blame] | 325 | pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n", |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 326 | rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep); |
| 327 | init_usb_anchor(&rtlusb->rx_submitted); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int _rtl_usb_init(struct ieee80211_hw *hw) |
| 332 | { |
| 333 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 334 | struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw); |
| 335 | struct rtl_usb *rtlusb = rtl_usbdev(usb_priv); |
| 336 | int err; |
| 337 | u8 epidx; |
| 338 | struct usb_interface *usb_intf = rtlusb->intf; |
| 339 | u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints; |
| 340 | |
| 341 | rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0; |
| 342 | for (epidx = 0; epidx < epnums; epidx++) { |
| 343 | struct usb_endpoint_descriptor *pep_desc; |
| 344 | pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc; |
| 345 | |
| 346 | if (usb_endpoint_dir_in(pep_desc)) |
| 347 | rtlusb->in_ep_nums++; |
| 348 | else if (usb_endpoint_dir_out(pep_desc)) |
| 349 | rtlusb->out_ep_nums++; |
| 350 | |
| 351 | RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 352 | "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n", |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 353 | pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 354 | pep_desc->bInterval); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 355 | } |
Larry Finger | 48de1a1 | 2012-03-02 13:21:22 -0600 | [diff] [blame] | 356 | if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num) { |
| 357 | pr_err("Too few input end points found\n"); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | if (rtlusb->out_ep_nums == 0) { |
| 361 | pr_err("No output end points found\n"); |
| 362 | return -EINVAL; |
| 363 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 364 | /* usb endpoint mapping */ |
| 365 | err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw); |
| 366 | rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq; |
| 367 | _rtl_usb_init_tx(hw); |
| 368 | _rtl_usb_init_rx(hw); |
| 369 | return err; |
| 370 | } |
| 371 | |
Larry Finger | 8f526ab | 2012-03-02 13:22:46 -0600 | [diff] [blame] | 372 | static void rtl_usb_init_sw(struct ieee80211_hw *hw) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 373 | { |
| 374 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 375 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 376 | struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); |
| 377 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 378 | |
| 379 | rtlhal->hw = hw; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 380 | ppsc->inactiveps = false; |
| 381 | ppsc->leisure_ps = false; |
| 382 | ppsc->fwctrl_lps = false; |
| 383 | ppsc->reg_fwctrl_lps = 3; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 384 | ppsc->reg_max_lps_awakeintvl = 5; |
| 385 | ppsc->fwctrl_psmode = FW_PS_DTIM_MODE; |
| 386 | |
| 387 | /* IBSS */ |
| 388 | mac->beacon_interval = 100; |
| 389 | |
| 390 | /* AMPDU */ |
| 391 | mac->min_space_cfg = 0; |
| 392 | mac->max_mss_density = 0; |
| 393 | |
| 394 | /* set sane AMPDU defaults */ |
| 395 | mac->current_ampdu_density = 7; |
| 396 | mac->current_ampdu_factor = 3; |
| 397 | |
| 398 | /* QOS */ |
| 399 | rtlusb->acm_method = eAcmWay2_SW; |
| 400 | |
| 401 | /* IRQ */ |
| 402 | /* HIMR - turn all on */ |
| 403 | rtlusb->irq_mask[0] = 0xFFFFFFFF; |
| 404 | /* HIMR_EX - turn all on */ |
| 405 | rtlusb->irq_mask[1] = 0xFFFFFFFF; |
| 406 | rtlusb->disableHWSM = true; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | #define __RADIO_TAP_SIZE_RSV 32 |
| 410 | |
| 411 | static void _rtl_rx_completed(struct urb *urb); |
| 412 | |
| 413 | static struct sk_buff *_rtl_prep_rx_urb(struct ieee80211_hw *hw, |
| 414 | struct rtl_usb *rtlusb, |
| 415 | struct urb *urb, |
| 416 | gfp_t gfp_mask) |
| 417 | { |
| 418 | struct sk_buff *skb; |
| 419 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 420 | |
| 421 | skb = __dev_alloc_skb((rtlusb->rx_max_size + __RADIO_TAP_SIZE_RSV), |
| 422 | gfp_mask); |
| 423 | if (!skb) { |
| 424 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 425 | "Failed to __dev_alloc_skb!!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 426 | return ERR_PTR(-ENOMEM); |
| 427 | } |
| 428 | |
| 429 | /* reserve some space for mac80211's radiotap */ |
| 430 | skb_reserve(skb, __RADIO_TAP_SIZE_RSV); |
| 431 | usb_fill_bulk_urb(urb, rtlusb->udev, |
| 432 | usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep), |
| 433 | skb->data, min(skb_tailroom(skb), |
| 434 | (int)rtlusb->rx_max_size), |
| 435 | _rtl_rx_completed, skb); |
| 436 | |
| 437 | _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep); |
| 438 | return skb; |
| 439 | } |
| 440 | |
| 441 | #undef __RADIO_TAP_SIZE_RSV |
| 442 | |
| 443 | static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw, |
| 444 | struct sk_buff *skb) |
| 445 | { |
| 446 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 447 | u8 *rxdesc = skb->data; |
| 448 | struct ieee80211_hdr *hdr; |
| 449 | bool unicast = false; |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 450 | __le16 fc; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 451 | struct ieee80211_rx_status rx_status = {0}; |
| 452 | struct rtl_stats stats = { |
| 453 | .signal = 0, |
| 454 | .noise = -98, |
| 455 | .rate = 0, |
| 456 | }; |
| 457 | |
| 458 | skb_pull(skb, RTL_RX_DESC_SIZE); |
| 459 | rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb); |
| 460 | skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift)); |
| 461 | hdr = (struct ieee80211_hdr *)(skb->data); |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 462 | fc = hdr->frame_control; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 463 | if (!stats.crc) { |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 464 | memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); |
| 465 | |
| 466 | if (is_broadcast_ether_addr(hdr->addr1)) { |
| 467 | /*TODO*/; |
| 468 | } else if (is_multicast_ether_addr(hdr->addr1)) { |
| 469 | /*TODO*/ |
| 470 | } else { |
| 471 | unicast = true; |
| 472 | rtlpriv->stats.rxbytesunicast += skb->len; |
| 473 | } |
| 474 | |
| 475 | rtl_is_special_data(hw, skb, false); |
| 476 | |
| 477 | if (ieee80211_is_data(fc)) { |
| 478 | rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX); |
| 479 | |
| 480 | if (unicast) |
| 481 | rtlpriv->link_info.num_rx_inperiod++; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw, |
| 487 | struct sk_buff *skb) |
| 488 | { |
| 489 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 490 | u8 *rxdesc = skb->data; |
| 491 | struct ieee80211_hdr *hdr; |
| 492 | bool unicast = false; |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 493 | __le16 fc; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 494 | struct ieee80211_rx_status rx_status = {0}; |
| 495 | struct rtl_stats stats = { |
| 496 | .signal = 0, |
| 497 | .noise = -98, |
| 498 | .rate = 0, |
| 499 | }; |
| 500 | |
| 501 | skb_pull(skb, RTL_RX_DESC_SIZE); |
| 502 | rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb); |
| 503 | skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift)); |
| 504 | hdr = (struct ieee80211_hdr *)(skb->data); |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 505 | fc = hdr->frame_control; |
Larry Finger | 7ea4724 | 2011-02-19 16:28:57 -0600 | [diff] [blame] | 506 | if (!stats.crc) { |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 507 | memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status)); |
| 508 | |
| 509 | if (is_broadcast_ether_addr(hdr->addr1)) { |
| 510 | /*TODO*/; |
| 511 | } else if (is_multicast_ether_addr(hdr->addr1)) { |
| 512 | /*TODO*/ |
| 513 | } else { |
| 514 | unicast = true; |
| 515 | rtlpriv->stats.rxbytesunicast += skb->len; |
| 516 | } |
| 517 | |
| 518 | rtl_is_special_data(hw, skb, false); |
| 519 | |
| 520 | if (ieee80211_is_data(fc)) { |
| 521 | rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX); |
| 522 | |
| 523 | if (unicast) |
| 524 | rtlpriv->link_info.num_rx_inperiod++; |
| 525 | } |
| 526 | if (likely(rtl_action_proc(hw, skb, false))) { |
| 527 | struct sk_buff *uskb = NULL; |
| 528 | u8 *pdata; |
| 529 | |
| 530 | uskb = dev_alloc_skb(skb->len + 128); |
Larry Finger | 76a92be | 2012-01-07 20:46:40 -0600 | [diff] [blame] | 531 | if (uskb) { /* drop packet on allocation failure */ |
| 532 | memcpy(IEEE80211_SKB_RXCB(uskb), &rx_status, |
| 533 | sizeof(rx_status)); |
| 534 | pdata = (u8 *)skb_put(uskb, skb->len); |
| 535 | memcpy(pdata, skb->data, skb->len); |
| 536 | ieee80211_rx_irqsafe(hw, uskb); |
| 537 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 538 | dev_kfree_skb_any(skb); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 539 | } else { |
| 540 | dev_kfree_skb_any(skb); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb) |
| 546 | { |
| 547 | struct sk_buff *_skb; |
| 548 | struct sk_buff_head rx_queue; |
| 549 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 550 | |
| 551 | skb_queue_head_init(&rx_queue); |
| 552 | if (rtlusb->usb_rx_segregate_hdl) |
| 553 | rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue); |
| 554 | WARN_ON(skb_queue_empty(&rx_queue)); |
| 555 | while (!skb_queue_empty(&rx_queue)) { |
| 556 | _skb = skb_dequeue(&rx_queue); |
Larry Finger | 0a06ad8 | 2013-01-27 16:24:25 -0600 | [diff] [blame] | 557 | _rtl_usb_rx_process_agg(hw, _skb); |
| 558 | ieee80211_rx_irqsafe(hw, _skb); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | static void _rtl_rx_completed(struct urb *_urb) |
| 563 | { |
| 564 | struct sk_buff *skb = (struct sk_buff *)_urb->context; |
| 565 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 566 | struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0]; |
| 567 | struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf); |
| 568 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 569 | int err = 0; |
| 570 | |
| 571 | if (unlikely(IS_USB_STOP(rtlusb))) |
| 572 | goto free; |
| 573 | |
| 574 | if (likely(0 == _urb->status)) { |
| 575 | /* If this code were moved to work queue, would CPU |
| 576 | * utilization be improved? NOTE: We shall allocate another skb |
| 577 | * and reuse the original one. |
| 578 | */ |
| 579 | skb_put(skb, _urb->actual_length); |
| 580 | |
| 581 | if (likely(!rtlusb->usb_rx_segregate_hdl)) { |
| 582 | struct sk_buff *_skb; |
| 583 | _rtl_usb_rx_process_noagg(hw, skb); |
| 584 | _skb = _rtl_prep_rx_urb(hw, rtlusb, _urb, GFP_ATOMIC); |
| 585 | if (IS_ERR(_skb)) { |
| 586 | err = PTR_ERR(_skb); |
| 587 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 588 | "Can't allocate skb for bulk IN!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | skb = _skb; |
| 592 | } else{ |
| 593 | /* TO DO */ |
| 594 | _rtl_rx_pre_process(hw, skb); |
Joe Perches | 292b119 | 2011-07-20 08:51:35 -0700 | [diff] [blame] | 595 | pr_err("rx agg not supported\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 596 | } |
| 597 | goto resubmit; |
| 598 | } |
| 599 | |
| 600 | switch (_urb->status) { |
| 601 | /* disconnect */ |
| 602 | case -ENOENT: |
| 603 | case -ECONNRESET: |
| 604 | case -ENODEV: |
| 605 | case -ESHUTDOWN: |
| 606 | goto free; |
| 607 | default: |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | resubmit: |
| 612 | skb_reset_tail_pointer(skb); |
| 613 | skb_trim(skb, 0); |
| 614 | |
| 615 | usb_anchor_urb(_urb, &rtlusb->rx_submitted); |
| 616 | err = usb_submit_urb(_urb, GFP_ATOMIC); |
| 617 | if (unlikely(err)) { |
| 618 | usb_unanchor_urb(_urb); |
| 619 | goto free; |
| 620 | } |
| 621 | return; |
| 622 | |
| 623 | free: |
| 624 | dev_kfree_skb_irq(skb); |
| 625 | } |
| 626 | |
| 627 | static int _rtl_usb_receive(struct ieee80211_hw *hw) |
| 628 | { |
| 629 | struct urb *urb; |
| 630 | struct sk_buff *skb; |
| 631 | int err; |
| 632 | int i; |
| 633 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 634 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 635 | |
| 636 | WARN_ON(0 == rtlusb->rx_urb_num); |
| 637 | /* 1600 == 1514 + max WLAN header + rtk info */ |
| 638 | WARN_ON(rtlusb->rx_max_size < 1600); |
| 639 | |
| 640 | for (i = 0; i < rtlusb->rx_urb_num; i++) { |
| 641 | err = -ENOMEM; |
| 642 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 643 | if (!urb) { |
| 644 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 645 | "Failed to alloc URB!!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 646 | goto err_out; |
| 647 | } |
| 648 | |
| 649 | skb = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL); |
| 650 | if (IS_ERR(skb)) { |
| 651 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 652 | "Failed to prep_rx_urb!!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 653 | err = PTR_ERR(skb); |
Jesper Juhl | 1474a89 | 2012-12-26 21:51:12 +0100 | [diff] [blame] | 654 | usb_free_urb(urb); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 655 | goto err_out; |
| 656 | } |
| 657 | |
| 658 | usb_anchor_urb(urb, &rtlusb->rx_submitted); |
| 659 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 660 | if (err) |
| 661 | goto err_out; |
| 662 | usb_free_urb(urb); |
| 663 | } |
| 664 | return 0; |
| 665 | |
| 666 | err_out: |
| 667 | usb_kill_anchored_urbs(&rtlusb->rx_submitted); |
| 668 | return err; |
| 669 | } |
| 670 | |
| 671 | static int rtl_usb_start(struct ieee80211_hw *hw) |
| 672 | { |
| 673 | int err; |
| 674 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 675 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 676 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 677 | |
| 678 | err = rtlpriv->cfg->ops->hw_init(hw); |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 679 | if (!err) { |
| 680 | rtl_init_rx_config(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 681 | |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 682 | /* Enable software */ |
| 683 | SET_USB_START(rtlusb); |
| 684 | /* should after adapter start and interrupt enable. */ |
| 685 | set_hal_start(rtlhal); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 686 | |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 687 | /* Start bulk IN */ |
Christian Lamparter | 0c7e920 | 2012-10-14 22:15:48 +0200 | [diff] [blame] | 688 | err = _rtl_usb_receive(hw); |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 689 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 690 | |
| 691 | return err; |
| 692 | } |
| 693 | /** |
| 694 | * |
| 695 | * |
| 696 | */ |
| 697 | |
| 698 | /*======================= tx =========================================*/ |
| 699 | static void rtl_usb_cleanup(struct ieee80211_hw *hw) |
| 700 | { |
| 701 | u32 i; |
| 702 | struct sk_buff *_skb; |
| 703 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 704 | struct ieee80211_tx_info *txinfo; |
| 705 | |
| 706 | SET_USB_STOP(rtlusb); |
| 707 | |
| 708 | /* clean up rx stuff. */ |
| 709 | usb_kill_anchored_urbs(&rtlusb->rx_submitted); |
| 710 | |
| 711 | /* clean up tx stuff */ |
| 712 | for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) { |
| 713 | while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) { |
| 714 | rtlusb->usb_tx_cleanup(hw, _skb); |
| 715 | txinfo = IEEE80211_SKB_CB(_skb); |
| 716 | ieee80211_tx_info_clear_status(txinfo); |
| 717 | txinfo->flags |= IEEE80211_TX_STAT_ACK; |
| 718 | ieee80211_tx_status_irqsafe(hw, _skb); |
| 719 | } |
| 720 | usb_kill_anchored_urbs(&rtlusb->tx_pending[i]); |
| 721 | } |
| 722 | usb_kill_anchored_urbs(&rtlusb->tx_submitted); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * |
| 727 | * We may add some struct into struct rtl_usb later. Do deinit here. |
| 728 | * |
| 729 | */ |
| 730 | static void rtl_usb_deinit(struct ieee80211_hw *hw) |
| 731 | { |
| 732 | rtl_usb_cleanup(hw); |
| 733 | } |
| 734 | |
| 735 | static void rtl_usb_stop(struct ieee80211_hw *hw) |
| 736 | { |
| 737 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 738 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 739 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 740 | |
| 741 | /* should after adapter start and interrupt enable. */ |
| 742 | set_hal_stop(rtlhal); |
| 743 | /* Enable software */ |
| 744 | SET_USB_STOP(rtlusb); |
| 745 | rtl_usb_deinit(hw); |
| 746 | rtlpriv->cfg->ops->hw_disable(hw); |
| 747 | } |
| 748 | |
| 749 | static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb) |
| 750 | { |
| 751 | int err; |
| 752 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 753 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 754 | |
| 755 | usb_anchor_urb(_urb, &rtlusb->tx_submitted); |
| 756 | err = usb_submit_urb(_urb, GFP_ATOMIC); |
| 757 | if (err < 0) { |
| 758 | struct sk_buff *skb; |
| 759 | |
| 760 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 761 | "Failed to submit urb\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 762 | usb_unanchor_urb(_urb); |
| 763 | skb = (struct sk_buff *)_urb->context; |
| 764 | kfree_skb(skb); |
| 765 | } |
| 766 | usb_free_urb(_urb); |
| 767 | } |
| 768 | |
| 769 | static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb, |
| 770 | struct sk_buff *skb) |
| 771 | { |
| 772 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 773 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 774 | struct ieee80211_tx_info *txinfo; |
| 775 | |
| 776 | rtlusb->usb_tx_post_hdl(hw, urb, skb); |
| 777 | skb_pull(skb, RTL_TX_HEADER_SIZE); |
| 778 | txinfo = IEEE80211_SKB_CB(skb); |
| 779 | ieee80211_tx_info_clear_status(txinfo); |
| 780 | txinfo->flags |= IEEE80211_TX_STAT_ACK; |
| 781 | |
| 782 | if (urb->status) { |
| 783 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 784 | "Urb has error status 0x%X\n", urb->status); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 785 | goto out; |
| 786 | } |
| 787 | /* TODO: statistics */ |
| 788 | out: |
| 789 | ieee80211_tx_status_irqsafe(hw, skb); |
| 790 | return urb->status; |
| 791 | } |
| 792 | |
| 793 | static void _rtl_tx_complete(struct urb *urb) |
| 794 | { |
| 795 | struct sk_buff *skb = (struct sk_buff *)urb->context; |
| 796 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 797 | struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0]; |
| 798 | struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf); |
| 799 | int err; |
| 800 | |
| 801 | if (unlikely(IS_USB_STOP(rtlusb))) |
| 802 | return; |
| 803 | err = _usb_tx_post(hw, urb, skb); |
| 804 | if (err) { |
| 805 | /* Ignore error and keep issuiing other urbs */ |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw, |
| 811 | struct sk_buff *skb, u32 ep_num) |
| 812 | { |
| 813 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 814 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 815 | struct urb *_urb; |
| 816 | |
| 817 | WARN_ON(NULL == skb); |
| 818 | _urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 819 | if (!_urb) { |
| 820 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 821 | "Can't allocate URB for bulk out!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 822 | kfree_skb(skb); |
| 823 | return NULL; |
| 824 | } |
| 825 | _rtl_install_trx_info(rtlusb, skb, ep_num); |
| 826 | usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev, |
| 827 | ep_num), skb->data, skb->len, _rtl_tx_complete, skb); |
| 828 | _urb->transfer_flags |= URB_ZERO_PACKET; |
| 829 | return _urb; |
| 830 | } |
| 831 | |
| 832 | static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb, |
| 833 | enum rtl_txq qnum) |
| 834 | { |
| 835 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 836 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 837 | u32 ep_num; |
| 838 | struct urb *_urb = NULL; |
| 839 | struct sk_buff *_skb = NULL; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 840 | |
| 841 | WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl); |
| 842 | if (unlikely(IS_USB_STOP(rtlusb))) { |
| 843 | RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 844 | "USB device is stopping...\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 845 | kfree_skb(skb); |
| 846 | return; |
| 847 | } |
| 848 | ep_num = rtlusb->ep_map.ep_mapping[qnum]; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 849 | _skb = skb; |
| 850 | _urb = _rtl_usb_tx_urb_setup(hw, _skb, ep_num); |
| 851 | if (unlikely(!_urb)) { |
| 852 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 853 | "Can't allocate urb. Drop skb!\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 854 | return; |
| 855 | } |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 856 | _rtl_submit_tx_urb(hw, _urb); |
| 857 | } |
| 858 | |
Thomas Huehn | 36323f8 | 2012-07-23 21:33:42 +0200 | [diff] [blame] | 859 | static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, |
| 860 | struct ieee80211_sta *sta, |
| 861 | struct sk_buff *skb, |
| 862 | u16 hw_queue) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 863 | { |
| 864 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 865 | struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); |
| 866 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 867 | struct rtl_tx_desc *pdesc = NULL; |
Chaoming_Li | d93cdee | 2011-04-25 12:53:29 -0500 | [diff] [blame] | 868 | struct rtl_tcb_desc tcb_desc; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 869 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 870 | __le16 fc = hdr->frame_control; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 871 | u8 *pda_addr = hdr->addr1; |
| 872 | /* ssn */ |
| 873 | u8 *qc = NULL; |
| 874 | u8 tid = 0; |
| 875 | u16 seq_number = 0; |
| 876 | |
Larry Finger | 831d854 | 2011-09-22 22:59:02 -0500 | [diff] [blame] | 877 | memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc)); |
Chaoming_Li | d93cdee | 2011-04-25 12:53:29 -0500 | [diff] [blame] | 878 | if (ieee80211_is_auth(fc)) { |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 879 | RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n"); |
Chaoming_Li | d93cdee | 2011-04-25 12:53:29 -0500 | [diff] [blame] | 880 | rtl_ips_nic_on(hw); |
| 881 | } |
| 882 | |
| 883 | if (rtlpriv->psc.sw_ps_enabled) { |
| 884 | if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) && |
| 885 | !ieee80211_has_pm(fc)) |
| 886 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); |
| 887 | } |
| 888 | |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 889 | rtl_action_proc(hw, skb, true); |
| 890 | if (is_multicast_ether_addr(pda_addr)) |
| 891 | rtlpriv->stats.txbytesmulticast += skb->len; |
| 892 | else if (is_broadcast_ether_addr(pda_addr)) |
| 893 | rtlpriv->stats.txbytesbroadcast += skb->len; |
| 894 | else |
| 895 | rtlpriv->stats.txbytesunicast += skb->len; |
| 896 | if (ieee80211_is_data_qos(fc)) { |
| 897 | qc = ieee80211_get_qos_ctl(hdr); |
| 898 | tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK; |
| 899 | seq_number = (le16_to_cpu(hdr->seq_ctrl) & |
| 900 | IEEE80211_SCTL_SEQ) >> 4; |
| 901 | seq_number += 1; |
| 902 | seq_number <<= 4; |
| 903 | } |
Thomas Huehn | 36323f8 | 2012-07-23 21:33:42 +0200 | [diff] [blame] | 904 | rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, sta, skb, |
Chaoming_Li | d93cdee | 2011-04-25 12:53:29 -0500 | [diff] [blame] | 905 | hw_queue, &tcb_desc); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 906 | if (!ieee80211_has_morefrags(hdr->frame_control)) { |
| 907 | if (qc) |
| 908 | mac->tids[tid].seq_number = seq_number; |
| 909 | } |
| 910 | if (ieee80211_is_data(fc)) |
| 911 | rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX); |
| 912 | } |
| 913 | |
Thomas Huehn | 36323f8 | 2012-07-23 21:33:42 +0200 | [diff] [blame] | 914 | static int rtl_usb_tx(struct ieee80211_hw *hw, |
| 915 | struct ieee80211_sta *sta, |
| 916 | struct sk_buff *skb, |
Chaoming_Li | 0baa0fd | 2011-04-25 13:23:10 -0500 | [diff] [blame] | 917 | struct rtl_tcb_desc *dummy) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 918 | { |
| 919 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 920 | struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); |
| 921 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data); |
Larry Finger | 17c9ac6 | 2011-02-19 16:29:57 -0600 | [diff] [blame] | 922 | __le16 fc = hdr->frame_control; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 923 | u16 hw_queue; |
| 924 | |
| 925 | if (unlikely(is_hal_stop(rtlhal))) |
| 926 | goto err_free; |
| 927 | hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb)); |
Thomas Huehn | 36323f8 | 2012-07-23 21:33:42 +0200 | [diff] [blame] | 928 | _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 929 | _rtl_usb_transmit(hw, skb, hw_queue); |
| 930 | return NETDEV_TX_OK; |
| 931 | |
| 932 | err_free: |
| 933 | dev_kfree_skb_any(skb); |
| 934 | return NETDEV_TX_OK; |
| 935 | } |
| 936 | |
| 937 | static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw, |
Thomas Huehn | 36323f8 | 2012-07-23 21:33:42 +0200 | [diff] [blame] | 938 | struct ieee80211_sta *sta, |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 939 | struct sk_buff *skb) |
| 940 | { |
| 941 | return false; |
| 942 | } |
| 943 | |
| 944 | static struct rtl_intf_ops rtl_usb_ops = { |
| 945 | .adapter_start = rtl_usb_start, |
| 946 | .adapter_stop = rtl_usb_stop, |
| 947 | .adapter_tx = rtl_usb_tx, |
| 948 | .waitq_insert = rtl_usb_tx_chk_waitq_insert, |
| 949 | }; |
| 950 | |
Bill Pemberton | 9e2ff36 | 2012-12-03 09:56:43 -0500 | [diff] [blame] | 951 | int rtl_usb_probe(struct usb_interface *intf, |
Larry Finger | 957f4ac | 2013-02-06 12:54:17 -0600 | [diff] [blame] | 952 | const struct usb_device_id *id, |
| 953 | struct rtl_hal_cfg *rtl_hal_cfg) |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 954 | { |
| 955 | int err; |
| 956 | struct ieee80211_hw *hw = NULL; |
| 957 | struct rtl_priv *rtlpriv = NULL; |
| 958 | struct usb_device *udev; |
| 959 | struct rtl_usb_priv *usb_priv; |
| 960 | |
| 961 | hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) + |
| 962 | sizeof(struct rtl_usb_priv), &rtl_ops); |
| 963 | if (!hw) { |
Joe Perches | 9d833ed | 2012-01-04 19:40:43 -0800 | [diff] [blame] | 964 | RT_ASSERT(false, "ieee80211 alloc failed\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 965 | return -ENOMEM; |
| 966 | } |
| 967 | rtlpriv = hw->priv; |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 968 | rtlpriv->usb_data = kzalloc(RTL_USB_MAX_RX_COUNT * sizeof(u32), |
| 969 | GFP_KERNEL); |
| 970 | if (!rtlpriv->usb_data) |
| 971 | return -ENOMEM; |
Larry Finger | 3ce4d85 | 2012-07-11 14:37:28 -0500 | [diff] [blame] | 972 | |
| 973 | /* this spin lock must be initialized early */ |
| 974 | spin_lock_init(&rtlpriv->locks.usb_lock); |
| 975 | |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 976 | rtlpriv->usb_data_index = 0; |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 977 | init_completion(&rtlpriv->firmware_loading_complete); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 978 | SET_IEEE80211_DEV(hw, &intf->dev); |
| 979 | udev = interface_to_usbdev(intf); |
| 980 | usb_get_dev(udev); |
| 981 | usb_priv = rtl_usbpriv(hw); |
| 982 | memset(usb_priv, 0, sizeof(*usb_priv)); |
| 983 | usb_priv->dev.intf = intf; |
| 984 | usb_priv->dev.udev = udev; |
| 985 | usb_set_intfdata(intf, hw); |
| 986 | /* init cfg & intf_ops */ |
| 987 | rtlpriv->rtlhal.interface = INTF_USB; |
Larry Finger | 957f4ac | 2013-02-06 12:54:17 -0600 | [diff] [blame] | 988 | rtlpriv->cfg = rtl_hal_cfg; |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 989 | rtlpriv->intf_ops = &rtl_usb_ops; |
| 990 | rtl_dbgp_flag_init(hw); |
| 991 | /* Init IO handler */ |
| 992 | _rtl_usb_io_handler_init(&udev->dev, hw); |
| 993 | rtlpriv->cfg->ops->read_chip_version(hw); |
| 994 | /*like read eeprom and so on */ |
| 995 | rtlpriv->cfg->ops->read_eeprom_info(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 996 | err = _rtl_usb_init(hw); |
Larry Finger | 48de1a1 | 2012-03-02 13:21:22 -0600 | [diff] [blame] | 997 | if (err) |
| 998 | goto error_out; |
Larry Finger | 8f526ab | 2012-03-02 13:22:46 -0600 | [diff] [blame] | 999 | rtl_usb_init_sw(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1000 | /* Init mac80211 sw */ |
| 1001 | err = rtl_init_core(hw); |
| 1002 | if (err) { |
| 1003 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, |
Joe Perches | f30d750 | 2012-01-04 19:40:41 -0800 | [diff] [blame] | 1004 | "Can't allocate sw for mac80211\n"); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1005 | goto error_out; |
| 1006 | } |
Larry Finger | 574e02a | 2012-05-04 08:27:43 -0500 | [diff] [blame] | 1007 | if (rtlpriv->cfg->ops->init_sw_vars(hw)) { |
| 1008 | RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Can't init_sw_vars\n"); |
| 1009 | goto error_out; |
| 1010 | } |
| 1011 | rtlpriv->cfg->ops->init_sw_leds(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1012 | |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1013 | return 0; |
| 1014 | error_out: |
| 1015 | rtl_deinit_core(hw); |
| 1016 | _rtl_usb_io_handler_release(hw); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1017 | usb_put_dev(udev); |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 1018 | complete(&rtlpriv->firmware_loading_complete); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1019 | return -ENODEV; |
| 1020 | } |
| 1021 | EXPORT_SYMBOL(rtl_usb_probe); |
| 1022 | |
| 1023 | void rtl_usb_disconnect(struct usb_interface *intf) |
| 1024 | { |
| 1025 | struct ieee80211_hw *hw = usb_get_intfdata(intf); |
| 1026 | struct rtl_priv *rtlpriv = rtl_priv(hw); |
| 1027 | struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw)); |
| 1028 | struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw)); |
| 1029 | |
| 1030 | if (unlikely(!rtlpriv)) |
| 1031 | return; |
Larry Finger | b0302ab | 2012-01-30 09:54:49 -0600 | [diff] [blame] | 1032 | |
| 1033 | /* just in case driver is removed before firmware callback */ |
| 1034 | wait_for_completion(&rtlpriv->firmware_loading_complete); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1035 | /*ieee80211_unregister_hw will call ops_stop */ |
| 1036 | if (rtlmac->mac80211_registered == 1) { |
| 1037 | ieee80211_unregister_hw(hw); |
| 1038 | rtlmac->mac80211_registered = 0; |
| 1039 | } else { |
| 1040 | rtl_deinit_deferred_work(hw); |
| 1041 | rtlpriv->intf_ops->adapter_stop(hw); |
| 1042 | } |
| 1043 | /*deinit rfkill */ |
| 1044 | /* rtl_deinit_rfkill(hw); */ |
| 1045 | rtl_usb_deinit(hw); |
| 1046 | rtl_deinit_core(hw); |
Larry Finger | a7959c1 | 2012-03-19 15:44:31 -0500 | [diff] [blame] | 1047 | kfree(rtlpriv->usb_data); |
George | 2ca20f7 | 2011-02-11 14:27:49 -0600 | [diff] [blame] | 1048 | rtlpriv->cfg->ops->deinit_sw_leds(hw); |
| 1049 | rtlpriv->cfg->ops->deinit_sw_vars(hw); |
| 1050 | _rtl_usb_io_handler_release(hw); |
| 1051 | usb_put_dev(rtlusb->udev); |
| 1052 | usb_set_intfdata(intf, NULL); |
| 1053 | ieee80211_free_hw(hw); |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(rtl_usb_disconnect); |
| 1056 | |
| 1057 | int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message) |
| 1058 | { |
| 1059 | return 0; |
| 1060 | } |
| 1061 | EXPORT_SYMBOL(rtl_usb_suspend); |
| 1062 | |
| 1063 | int rtl_usb_resume(struct usb_interface *pusb_intf) |
| 1064 | { |
| 1065 | return 0; |
| 1066 | } |
| 1067 | EXPORT_SYMBOL(rtl_usb_resume); |