blob: 79889b83287e1ae2418f3751b6f3224d557014b8 [file] [log] [blame]
George2ca20f72011-02-11 14:27:49 -06001/******************************************************************************
2 *
3 * Copyright(c) 2009-2011 Realtek Corporation. All rights reserved.
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 Perches292b1192011-07-20 08:51:35 -070027
28#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
George2ca20f72011-02-11 14:27:49 -060030#include <linux/usb.h>
31#include "core.h"
32#include "wifi.h"
33#include "usb.h"
34#include "base.h"
35#include "ps.h"
George040a7272011-11-17 12:14:42 -060036#include "rtl8192c/fw_common.h"
George2ca20f72011-02-11 14:27:49 -060037
38#define REALTEK_USB_VENQT_READ 0xC0
39#define REALTEK_USB_VENQT_WRITE 0x40
40#define REALTEK_USB_VENQT_CMD_REQ 0x05
41#define REALTEK_USB_VENQT_CMD_IDX 0x00
42
George040a7272011-11-17 12:14:42 -060043#define MAX_USBCTRL_VENDORREQ_TIMES 10
George2ca20f72011-02-11 14:27:49 -060044
45static void usbctrl_async_callback(struct urb *urb)
46{
47 if (urb)
48 kfree(urb->context);
49}
50
51static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
52 u16 value, u16 index, void *pdata,
53 u16 len)
54{
55 int rc;
56 unsigned int pipe;
57 u8 reqtype;
58 struct usb_ctrlrequest *dr;
59 struct urb *urb;
60 struct rtl819x_async_write_data {
61 u8 data[REALTEK_USB_VENQT_MAX_BUF_SIZE];
62 struct usb_ctrlrequest dr;
63 } *buf;
64
65 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
66 reqtype = REALTEK_USB_VENQT_WRITE;
67
68 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
69 if (!buf)
70 return -ENOMEM;
71
72 urb = usb_alloc_urb(0, GFP_ATOMIC);
73 if (!urb) {
74 kfree(buf);
75 return -ENOMEM;
76 }
77
78 dr = &buf->dr;
79
80 dr->bRequestType = reqtype;
81 dr->bRequest = request;
82 dr->wValue = cpu_to_le16(value);
83 dr->wIndex = cpu_to_le16(index);
84 dr->wLength = cpu_to_le16(len);
Larry Fingerabfabc92011-11-17 12:14:44 -060085 /* data are already in little-endian order */
George2ca20f72011-02-11 14:27:49 -060086 memcpy(buf, pdata, len);
87 usb_fill_control_urb(urb, udev, pipe,
88 (unsigned char *)dr, buf, len,
89 usbctrl_async_callback, buf);
90 rc = usb_submit_urb(urb, GFP_ATOMIC);
91 if (rc < 0)
92 kfree(buf);
93 usb_free_urb(urb);
94 return rc;
95}
96
97static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
98 u16 value, u16 index, void *pdata,
99 u16 len)
100{
101 unsigned int pipe;
102 int status;
103 u8 reqtype;
George040a7272011-11-17 12:14:42 -0600104 int vendorreq_times = 0;
Larry Fingerabfabc92011-11-17 12:14:44 -0600105 static int count;
George2ca20f72011-02-11 14:27:49 -0600106
107 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
108 reqtype = REALTEK_USB_VENQT_READ;
109
George040a7272011-11-17 12:14:42 -0600110 while (++vendorreq_times <= MAX_USBCTRL_VENDORREQ_TIMES) {
111 status = usb_control_msg(udev, pipe, request, reqtype, value,
112 index, pdata, len, 0); /*max. timeout*/
113 if (status < 0) {
114 /* firmware download is checksumed, don't retry */
115 if ((value >= FW_8192C_START_ADDRESS &&
116 value <= FW_8192C_END_ADDRESS))
117 break;
118 } else {
119 break;
120 }
121 }
Larry Fingerabfabc92011-11-17 12:14:44 -0600122 if (status < 0 && count++ < 4)
Joe Perches292b1192011-07-20 08:51:35 -0700123 pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
Larry Fingerabfabc92011-11-17 12:14:44 -0600124 value, status, le32_to_cpu(*(u32 *)pdata));
George2ca20f72011-02-11 14:27:49 -0600125 return status;
126}
127
128static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
129{
130 u8 request;
131 u16 wvalue;
132 u16 index;
133 u32 *data;
134 u32 ret;
135
136 data = kmalloc(sizeof(u32), GFP_KERNEL);
137 if (!data)
138 return -ENOMEM;
139 request = REALTEK_USB_VENQT_CMD_REQ;
140 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
141
142 wvalue = (u16)addr;
143 _usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
Larry Fingerabfabc92011-11-17 12:14:44 -0600144 ret = le32_to_cpu(*data);
George2ca20f72011-02-11 14:27:49 -0600145 kfree(data);
146 return ret;
147}
148
149static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
150{
151 struct device *dev = rtlpriv->io.dev;
152
153 return (u8)_usb_read_sync(to_usb_device(dev), addr, 1);
154}
155
156static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
157{
158 struct device *dev = rtlpriv->io.dev;
159
160 return (u16)_usb_read_sync(to_usb_device(dev), addr, 2);
161}
162
163static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
164{
165 struct device *dev = rtlpriv->io.dev;
166
167 return _usb_read_sync(to_usb_device(dev), addr, 4);
168}
169
170static void _usb_write_async(struct usb_device *udev, u32 addr, u32 val,
171 u16 len)
172{
173 u8 request;
174 u16 wvalue;
175 u16 index;
Larry Fingerabfabc92011-11-17 12:14:44 -0600176 __le32 data;
George2ca20f72011-02-11 14:27:49 -0600177
178 request = REALTEK_USB_VENQT_CMD_REQ;
179 index = REALTEK_USB_VENQT_CMD_IDX; /* n/a */
180 wvalue = (u16)(addr&0x0000ffff);
Larry Fingerabfabc92011-11-17 12:14:44 -0600181 data = cpu_to_le32(val);
George2ca20f72011-02-11 14:27:49 -0600182 _usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
183 len);
184}
185
186static void _usb_write8_async(struct rtl_priv *rtlpriv, u32 addr, u8 val)
187{
188 struct device *dev = rtlpriv->io.dev;
189
190 _usb_write_async(to_usb_device(dev), addr, val, 1);
191}
192
193static void _usb_write16_async(struct rtl_priv *rtlpriv, u32 addr, u16 val)
194{
195 struct device *dev = rtlpriv->io.dev;
196
197 _usb_write_async(to_usb_device(dev), addr, val, 2);
198}
199
200static void _usb_write32_async(struct rtl_priv *rtlpriv, u32 addr, u32 val)
201{
202 struct device *dev = rtlpriv->io.dev;
203
204 _usb_write_async(to_usb_device(dev), addr, val, 4);
205}
206
Larry Fingerff6ff962011-11-17 12:14:43 -0600207static void _usb_writeN_sync(struct rtl_priv *rtlpriv, u32 addr, void *data,
208 u16 len)
209{
210 struct device *dev = rtlpriv->io.dev;
211 struct usb_device *udev = to_usb_device(dev);
212 u8 request = REALTEK_USB_VENQT_CMD_REQ;
213 u8 reqtype = REALTEK_USB_VENQT_WRITE;
214 u16 wvalue;
215 u16 index = REALTEK_USB_VENQT_CMD_IDX;
216 int pipe = usb_sndctrlpipe(udev, 0); /* write_out */
217 u8 *buffer;
218 dma_addr_t dma_addr;
219
220 wvalue = (u16)(addr&0x0000ffff);
221 buffer = usb_alloc_coherent(udev, (size_t)len, GFP_ATOMIC, &dma_addr);
222 if (!buffer)
223 return;
224 memcpy(buffer, data, len);
225 usb_control_msg(udev, pipe, request, reqtype, wvalue,
226 index, buffer, len, 50);
227
228 usb_free_coherent(udev, (size_t)len, buffer, dma_addr);
229}
230
George2ca20f72011-02-11 14:27:49 -0600231static void _rtl_usb_io_handler_init(struct device *dev,
232 struct ieee80211_hw *hw)
233{
234 struct rtl_priv *rtlpriv = rtl_priv(hw);
235
236 rtlpriv->io.dev = dev;
237 mutex_init(&rtlpriv->io.bb_mutex);
238 rtlpriv->io.write8_async = _usb_write8_async;
239 rtlpriv->io.write16_async = _usb_write16_async;
240 rtlpriv->io.write32_async = _usb_write32_async;
George2ca20f72011-02-11 14:27:49 -0600241 rtlpriv->io.read8_sync = _usb_read8_sync;
242 rtlpriv->io.read16_sync = _usb_read16_sync;
243 rtlpriv->io.read32_sync = _usb_read32_sync;
Larry Fingerff6ff962011-11-17 12:14:43 -0600244 rtlpriv->io.writeN_sync = _usb_writeN_sync;
George2ca20f72011-02-11 14:27:49 -0600245}
246
247static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
248{
Larry Finger2e3e66e2011-04-02 18:10:22 -0500249 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
George2ca20f72011-02-11 14:27:49 -0600250
251 mutex_destroy(&rtlpriv->io.bb_mutex);
252}
253
254/**
255 *
256 * Default aggregation handler. Do nothing and just return the oldest skb.
257 */
258static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
259 struct sk_buff_head *list)
260{
261 return skb_dequeue(list);
262}
263
264#define IS_HIGH_SPEED_USB(udev) \
265 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
266
267static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
268{
269 u32 i;
270 struct rtl_priv *rtlpriv = rtl_priv(hw);
271 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
272
273 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
274 ? USB_HIGH_SPEED_BULK_SIZE
275 : USB_FULL_SPEED_BULK_SIZE;
276
277 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("USB Max Bulk-out Size=%d\n",
278 rtlusb->max_bulk_out_size));
279
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;
286 }
287 }
288
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;
297
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]);
302 }
303 return 0;
304}
305
306static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
307{
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);
311
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;
318
Joe Perches292b1192011-07-20 08:51:35 -0700319 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
George2ca20f72011-02-11 14:27:49 -0600320 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
321 init_usb_anchor(&rtlusb->rx_submitted);
322 return 0;
323}
324
325static int _rtl_usb_init(struct ieee80211_hw *hw)
326{
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;
334
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;
339
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++;
344
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));
349 }
350 if (rtlusb->in_ep_nums < rtlpriv->cfg->usb_interface_cfg->in_ep_num)
351 return -EINVAL ;
352
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;
359}
360
361static int _rtl_usb_init_sw(struct ieee80211_hw *hw)
362{
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));
367
368 rtlhal->hw = hw;
Larry Finger7ea47242011-02-19 16:28:57 -0600369 ppsc->inactiveps = false;
370 ppsc->leisure_ps = false;
371 ppsc->fwctrl_lps = false;
372 ppsc->reg_fwctrl_lps = 3;
George2ca20f72011-02-11 14:27:49 -0600373 ppsc->reg_max_lps_awakeintvl = 5;
374 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
375
376 /* IBSS */
377 mac->beacon_interval = 100;
378
379 /* AMPDU */
380 mac->min_space_cfg = 0;
381 mac->max_mss_density = 0;
382
383 /* set sane AMPDU defaults */
384 mac->current_ampdu_density = 7;
385 mac->current_ampdu_factor = 3;
386
387 /* QOS */
388 rtlusb->acm_method = eAcmWay2_SW;
389
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;
397}
398
399#define __RADIO_TAP_SIZE_RSV 32
400
401static void _rtl_rx_completed(struct urb *urb);
402
403static 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)
407{
408 struct sk_buff *skb;
409 struct rtl_priv *rtlpriv = rtl_priv(hw);
410
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);
417 }
418
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);
426
427 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
428 return skb;
429}
430
431#undef __RADIO_TAP_SIZE_RSV
432
433static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
434 struct sk_buff *skb)
435{
436 struct rtl_priv *rtlpriv = rtl_priv(hw);
437 u8 *rxdesc = skb->data;
438 struct ieee80211_hdr *hdr;
439 bool unicast = false;
Larry Finger17c9ac62011-02-19 16:29:57 -0600440 __le16 fc;
George2ca20f72011-02-11 14:27:49 -0600441 struct ieee80211_rx_status rx_status = {0};
442 struct rtl_stats stats = {
443 .signal = 0,
444 .noise = -98,
445 .rate = 0,
446 };
447
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);
Larry Finger17c9ac62011-02-19 16:29:57 -0600452 fc = hdr->frame_control;
Larry Finger7ea47242011-02-19 16:28:57 -0600453 if (!stats.crc) {
George2ca20f72011-02-11 14:27:49 -0600454 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
455
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;
463 }
464
465 rtl_is_special_data(hw, skb, false);
466
467 if (ieee80211_is_data(fc)) {
468 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
469
470 if (unicast)
471 rtlpriv->link_info.num_rx_inperiod++;
472 }
473 }
474}
475
476static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
477 struct sk_buff *skb)
478{
479 struct rtl_priv *rtlpriv = rtl_priv(hw);
480 u8 *rxdesc = skb->data;
481 struct ieee80211_hdr *hdr;
482 bool unicast = false;
Larry Finger17c9ac62011-02-19 16:29:57 -0600483 __le16 fc;
George2ca20f72011-02-11 14:27:49 -0600484 struct ieee80211_rx_status rx_status = {0};
485 struct rtl_stats stats = {
486 .signal = 0,
487 .noise = -98,
488 .rate = 0,
489 };
490
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);
Larry Finger17c9ac62011-02-19 16:29:57 -0600495 fc = hdr->frame_control;
Larry Finger7ea47242011-02-19 16:28:57 -0600496 if (!stats.crc) {
George2ca20f72011-02-11 14:27:49 -0600497 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
498
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;
506 }
507
508 rtl_is_special_data(hw, skb, false);
509
510 if (ieee80211_is_data(fc)) {
511 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
512
513 if (unicast)
514 rtlpriv->link_info.num_rx_inperiod++;
515 }
516 if (likely(rtl_action_proc(hw, skb, false))) {
517 struct sk_buff *uskb = NULL;
518 u8 *pdata;
519
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);
529 }
530 }
531}
532
533static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
534{
535 struct sk_buff *_skb;
536 struct sk_buff_head rx_queue;
537 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
538
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);
547 }
548}
549
550static void _rtl_rx_completed(struct urb *_urb)
551{
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;
558
559 if (unlikely(IS_USB_STOP(rtlusb)))
560 goto free;
561
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.
566 */
567 skb_put(skb, _urb->actual_length);
568
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;
578 }
579 skb = _skb;
580 } else{
581 /* TO DO */
582 _rtl_rx_pre_process(hw, skb);
Joe Perches292b1192011-07-20 08:51:35 -0700583 pr_err("rx agg not supported\n");
George2ca20f72011-02-11 14:27:49 -0600584 }
585 goto resubmit;
586 }
587
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;
597 }
598
599resubmit:
600 skb_reset_tail_pointer(skb);
601 skb_trim(skb, 0);
602
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;
608 }
609 return;
610
611free:
612 dev_kfree_skb_irq(skb);
613}
614
615static int _rtl_usb_receive(struct ieee80211_hw *hw)
616{
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));
623
624 WARN_ON(0 == rtlusb->rx_urb_num);
625 /* 1600 == 1514 + max WLAN header + rtk info */
626 WARN_ON(rtlusb->rx_max_size < 1600);
627
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;
635 }
636
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;
643 }
644
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);
650 }
651 return 0;
652
653err_out:
654 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
655 return err;
656}
657
658static int rtl_usb_start(struct ieee80211_hw *hw)
659{
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));
664
665 err = rtlpriv->cfg->ops->hw_init(hw);
666 rtl_init_rx_config(hw);
667
668 /* Enable software */
669 SET_USB_START(rtlusb);
670 /* should after adapter start and interrupt enable. */
671 set_hal_start(rtlhal);
672
673 /* Start bulk IN */
674 _rtl_usb_receive(hw);
675
676 return err;
677}
678/**
679 *
680 *
681 */
682
683/*======================= tx =========================================*/
684static void rtl_usb_cleanup(struct ieee80211_hw *hw)
685{
686 u32 i;
687 struct sk_buff *_skb;
688 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
689 struct ieee80211_tx_info *txinfo;
690
691 SET_USB_STOP(rtlusb);
692
693 /* clean up rx stuff. */
694 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
695
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);
704 }
705 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
706 }
707 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
708}
709
710/**
711 *
712 * We may add some struct into struct rtl_usb later. Do deinit here.
713 *
714 */
715static void rtl_usb_deinit(struct ieee80211_hw *hw)
716{
717 rtl_usb_cleanup(hw);
718}
719
720static void rtl_usb_stop(struct ieee80211_hw *hw)
721{
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));
725
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);
732}
733
734static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
735{
736 int err;
737 struct rtl_priv *rtlpriv = rtl_priv(hw);
738 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
739
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;
744
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);
750 }
751 usb_free_urb(_urb);
752}
753
754static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
755 struct sk_buff *skb)
756{
757 struct rtl_priv *rtlpriv = rtl_priv(hw);
758 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
759 struct ieee80211_tx_info *txinfo;
760
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;
766
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;
771 }
772 /* TODO: statistics */
773out:
774 ieee80211_tx_status_irqsafe(hw, skb);
775 return urb->status;
776}
777
778static void _rtl_tx_complete(struct urb *urb)
779{
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;
785
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;
792 }
793}
794
795static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
796 struct sk_buff *skb, u32 ep_num)
797{
798 struct rtl_priv *rtlpriv = rtl_priv(hw);
799 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
800 struct urb *_urb;
801
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;
809 }
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;
815}
816
817static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
818 enum rtl_txq qnum)
819{
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;
827
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;
834 }
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;
843 }
844 urb_list = &rtlusb->tx_pending[ep_num];
845 _rtl_submit_tx_urb(hw, _urb);
846}
847
848static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw, struct sk_buff *skb,
849 u16 hw_queue)
850{
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;
Chaoming_Lid93cdee2011-04-25 12:53:29 -0500855 struct rtl_tcb_desc tcb_desc;
George2ca20f72011-02-11 14:27:49 -0600856 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
Larry Finger17c9ac62011-02-19 16:29:57 -0600857 __le16 fc = hdr->frame_control;
George2ca20f72011-02-11 14:27:49 -0600858 u8 *pda_addr = hdr->addr1;
859 /* ssn */
860 u8 *qc = NULL;
861 u8 tid = 0;
862 u16 seq_number = 0;
863
Larry Finger831d8542011-09-22 22:59:02 -0500864 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
Chaoming_Lid93cdee2011-04-25 12:53:29 -0500865 if (ieee80211_is_auth(fc)) {
866 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
867 rtl_ips_nic_on(hw);
868 }
869
870 if (rtlpriv->psc.sw_ps_enabled) {
871 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
872 !ieee80211_has_pm(fc))
873 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
874 }
875
George2ca20f72011-02-11 14:27:49 -0600876 rtl_action_proc(hw, skb, true);
877 if (is_multicast_ether_addr(pda_addr))
878 rtlpriv->stats.txbytesmulticast += skb->len;
879 else if (is_broadcast_ether_addr(pda_addr))
880 rtlpriv->stats.txbytesbroadcast += skb->len;
881 else
882 rtlpriv->stats.txbytesunicast += skb->len;
883 if (ieee80211_is_data_qos(fc)) {
884 qc = ieee80211_get_qos_ctl(hdr);
885 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
886 seq_number = (le16_to_cpu(hdr->seq_ctrl) &
887 IEEE80211_SCTL_SEQ) >> 4;
888 seq_number += 1;
889 seq_number <<= 4;
890 }
891 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, info, skb,
Chaoming_Lid93cdee2011-04-25 12:53:29 -0500892 hw_queue, &tcb_desc);
George2ca20f72011-02-11 14:27:49 -0600893 if (!ieee80211_has_morefrags(hdr->frame_control)) {
894 if (qc)
895 mac->tids[tid].seq_number = seq_number;
896 }
897 if (ieee80211_is_data(fc))
898 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
899}
900
Chaoming_Li0baa0fd2011-04-25 13:23:10 -0500901static int rtl_usb_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
902 struct rtl_tcb_desc *dummy)
George2ca20f72011-02-11 14:27:49 -0600903{
904 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
905 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
906 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
Larry Finger17c9ac62011-02-19 16:29:57 -0600907 __le16 fc = hdr->frame_control;
George2ca20f72011-02-11 14:27:49 -0600908 u16 hw_queue;
909
910 if (unlikely(is_hal_stop(rtlhal)))
911 goto err_free;
912 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
913 _rtl_usb_tx_preprocess(hw, skb, hw_queue);
914 _rtl_usb_transmit(hw, skb, hw_queue);
915 return NETDEV_TX_OK;
916
917err_free:
918 dev_kfree_skb_any(skb);
919 return NETDEV_TX_OK;
920}
921
922static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
923 struct sk_buff *skb)
924{
925 return false;
926}
927
928static struct rtl_intf_ops rtl_usb_ops = {
929 .adapter_start = rtl_usb_start,
930 .adapter_stop = rtl_usb_stop,
931 .adapter_tx = rtl_usb_tx,
932 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
933};
934
935int __devinit rtl_usb_probe(struct usb_interface *intf,
936 const struct usb_device_id *id)
937{
938 int err;
939 struct ieee80211_hw *hw = NULL;
940 struct rtl_priv *rtlpriv = NULL;
941 struct usb_device *udev;
942 struct rtl_usb_priv *usb_priv;
943
944 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
945 sizeof(struct rtl_usb_priv), &rtl_ops);
946 if (!hw) {
947 RT_ASSERT(false, ("%s : ieee80211 alloc failed\n", __func__));
948 return -ENOMEM;
949 }
950 rtlpriv = hw->priv;
951 SET_IEEE80211_DEV(hw, &intf->dev);
952 udev = interface_to_usbdev(intf);
953 usb_get_dev(udev);
954 usb_priv = rtl_usbpriv(hw);
955 memset(usb_priv, 0, sizeof(*usb_priv));
956 usb_priv->dev.intf = intf;
957 usb_priv->dev.udev = udev;
958 usb_set_intfdata(intf, hw);
959 /* init cfg & intf_ops */
960 rtlpriv->rtlhal.interface = INTF_USB;
961 rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_info);
962 rtlpriv->intf_ops = &rtl_usb_ops;
963 rtl_dbgp_flag_init(hw);
964 /* Init IO handler */
965 _rtl_usb_io_handler_init(&udev->dev, hw);
966 rtlpriv->cfg->ops->read_chip_version(hw);
967 /*like read eeprom and so on */
968 rtlpriv->cfg->ops->read_eeprom_info(hw);
969 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
970 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
971 ("Can't init_sw_vars.\n"));
972 goto error_out;
973 }
974 rtlpriv->cfg->ops->init_sw_leds(hw);
975 err = _rtl_usb_init(hw);
976 err = _rtl_usb_init_sw(hw);
977 /* Init mac80211 sw */
978 err = rtl_init_core(hw);
979 if (err) {
980 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
981 ("Can't allocate sw for mac80211.\n"));
982 goto error_out;
983 }
984
985 /*init rfkill */
986 /* rtl_init_rfkill(hw); */
987
988 err = ieee80211_register_hw(hw);
989 if (err) {
990 RT_TRACE(rtlpriv, COMP_INIT, DBG_EMERG,
991 ("Can't register mac80211 hw.\n"));
992 goto error_out;
993 } else {
994 rtlpriv->mac80211.mac80211_registered = 1;
995 }
996 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
997 return 0;
998error_out:
999 rtl_deinit_core(hw);
1000 _rtl_usb_io_handler_release(hw);
1001 ieee80211_free_hw(hw);
1002 usb_put_dev(udev);
1003 return -ENODEV;
1004}
1005EXPORT_SYMBOL(rtl_usb_probe);
1006
1007void rtl_usb_disconnect(struct usb_interface *intf)
1008{
1009 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1010 struct rtl_priv *rtlpriv = rtl_priv(hw);
1011 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1012 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1013
1014 if (unlikely(!rtlpriv))
1015 return;
1016 /*ieee80211_unregister_hw will call ops_stop */
1017 if (rtlmac->mac80211_registered == 1) {
1018 ieee80211_unregister_hw(hw);
1019 rtlmac->mac80211_registered = 0;
1020 } else {
1021 rtl_deinit_deferred_work(hw);
1022 rtlpriv->intf_ops->adapter_stop(hw);
1023 }
1024 /*deinit rfkill */
1025 /* rtl_deinit_rfkill(hw); */
1026 rtl_usb_deinit(hw);
1027 rtl_deinit_core(hw);
1028 rtlpriv->cfg->ops->deinit_sw_leds(hw);
1029 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1030 _rtl_usb_io_handler_release(hw);
1031 usb_put_dev(rtlusb->udev);
1032 usb_set_intfdata(intf, NULL);
1033 ieee80211_free_hw(hw);
1034}
1035EXPORT_SYMBOL(rtl_usb_disconnect);
1036
1037int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1038{
1039 return 0;
1040}
1041EXPORT_SYMBOL(rtl_usb_suspend);
1042
1043int rtl_usb_resume(struct usb_interface *pusb_intf)
1044{
1045 return 0;
1046}
1047EXPORT_SYMBOL(rtl_usb_resume);