1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012 Realtek Corporation.*/
3
4 #include "wifi.h"
5 #include "core.h"
6 #include "usb.h"
7 #include "base.h"
8 #include "ps.h"
9 #include "rtl8192c/fw_common.h"
10 #include <linux/export.h>
11 #include <linux/module.h>
12
13 MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
14 MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
15 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
16 MODULE_LICENSE("GPL");
17 MODULE_DESCRIPTION("USB basic driver for rtlwifi");
18
19 #define REALTEK_USB_VENQT_READ 0xC0
20 #define REALTEK_USB_VENQT_WRITE 0x40
21 #define REALTEK_USB_VENQT_CMD_REQ 0x05
22 #define REALTEK_USB_VENQT_CMD_IDX 0x00
23
24 #define MAX_USBCTRL_VENDORREQ_TIMES 10
25
26 static void _rtl_usb_cleanup_tx(struct ieee80211_hw *hw);
27
_usbctrl_vendorreq_sync(struct usb_device * udev,u8 reqtype,u16 value,void * pdata,u16 len)28 static void _usbctrl_vendorreq_sync(struct usb_device *udev, u8 reqtype,
29 u16 value, void *pdata, u16 len)
30 {
31 unsigned int pipe;
32 int status;
33 int vendorreq_times = 0;
34 static int count;
35
36 if (reqtype == REALTEK_USB_VENQT_READ)
37 pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
38 else
39 pipe = usb_sndctrlpipe(udev, 0); /* write_out */
40
41 do {
42 status = usb_control_msg(udev, pipe, REALTEK_USB_VENQT_CMD_REQ,
43 reqtype, value, REALTEK_USB_VENQT_CMD_IDX,
44 pdata, len, 1000);
45 if (status < 0) {
46 /* firmware download is checksumed, don't retry */
47 if ((value >= FW_8192C_START_ADDRESS &&
48 value <= FW_8192C_END_ADDRESS))
49 break;
50 } else {
51 break;
52 }
53 } while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
54
55 if (status < 0 && count++ < 4)
56 dev_err(&udev->dev, "reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x reqtype=0x%x\n",
57 value, status, *(u32 *)pdata, reqtype);
58 }
59
_usb_read_sync(struct rtl_priv * rtlpriv,u32 addr,u16 len)60 static u32 _usb_read_sync(struct rtl_priv *rtlpriv, u32 addr, u16 len)
61 {
62 struct device *dev = rtlpriv->io.dev;
63 struct usb_device *udev = to_usb_device(dev);
64 u16 wvalue;
65 __le32 *data;
66 unsigned long flags;
67
68 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
69 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
70 rtlpriv->usb_data_index = 0;
71 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
72 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
73
74 wvalue = (u16)addr;
75 _usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_READ, wvalue, data, len);
76 return le32_to_cpu(*data);
77 }
78
79
_usb_write_sync(struct rtl_priv * rtlpriv,u32 addr,u32 val,u16 len)80 static void _usb_write_sync(struct rtl_priv *rtlpriv, u32 addr, u32 val, u16 len)
81 {
82 struct device *dev = rtlpriv->io.dev;
83 struct usb_device *udev = to_usb_device(dev);
84 unsigned long flags;
85 __le32 *data;
86 u16 wvalue;
87
88 spin_lock_irqsave(&rtlpriv->locks.usb_lock, flags);
89 if (++rtlpriv->usb_data_index >= RTL_USB_MAX_RX_COUNT)
90 rtlpriv->usb_data_index = 0;
91 data = &rtlpriv->usb_data[rtlpriv->usb_data_index];
92 spin_unlock_irqrestore(&rtlpriv->locks.usb_lock, flags);
93
94 wvalue = (u16)(addr & 0x0000ffff);
95 *data = cpu_to_le32(val);
96
97 _usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_WRITE, wvalue, data, len);
98 }
99
_usb_read8_sync(struct rtl_priv * rtlpriv,u32 addr)100 static u8 _usb_read8_sync(struct rtl_priv *rtlpriv, u32 addr)
101 {
102 return (u8)_usb_read_sync(rtlpriv, addr, 1);
103 }
104
_usb_read16_sync(struct rtl_priv * rtlpriv,u32 addr)105 static u16 _usb_read16_sync(struct rtl_priv *rtlpriv, u32 addr)
106 {
107 return (u16)_usb_read_sync(rtlpriv, addr, 2);
108 }
109
_usb_read32_sync(struct rtl_priv * rtlpriv,u32 addr)110 static u32 _usb_read32_sync(struct rtl_priv *rtlpriv, u32 addr)
111 {
112 return _usb_read_sync(rtlpriv, addr, 4);
113 }
114
_usb_write8_sync(struct rtl_priv * rtlpriv,u32 addr,u8 val)115 static void _usb_write8_sync(struct rtl_priv *rtlpriv, u32 addr, u8 val)
116 {
117 _usb_write_sync(rtlpriv, addr, val, 1);
118 }
119
_usb_write16_sync(struct rtl_priv * rtlpriv,u32 addr,u16 val)120 static void _usb_write16_sync(struct rtl_priv *rtlpriv, u32 addr, u16 val)
121 {
122 _usb_write_sync(rtlpriv, addr, val, 2);
123 }
124
_usb_write32_sync(struct rtl_priv * rtlpriv,u32 addr,u32 val)125 static void _usb_write32_sync(struct rtl_priv *rtlpriv, u32 addr, u32 val)
126 {
127 _usb_write_sync(rtlpriv, addr, val, 4);
128 }
129
_usb_write_chunk_sync(struct rtl_priv * rtlpriv,u32 addr,u32 length,u8 * data)130 static void _usb_write_chunk_sync(struct rtl_priv *rtlpriv, u32 addr,
131 u32 length, u8 *data)
132 {
133 struct usb_device *udev = to_usb_device(rtlpriv->io.dev);
134
135 _usbctrl_vendorreq_sync(udev, REALTEK_USB_VENQT_WRITE, addr, data, length);
136 }
137
_rtl_usb_io_handler_init(struct device * dev,struct ieee80211_hw * hw)138 static void _rtl_usb_io_handler_init(struct device *dev,
139 struct ieee80211_hw *hw)
140 {
141 struct rtl_priv *rtlpriv = rtl_priv(hw);
142
143 rtlpriv->io.dev = dev;
144 mutex_init(&rtlpriv->io.bb_mutex);
145 rtlpriv->io.write8 = _usb_write8_sync;
146 rtlpriv->io.write16 = _usb_write16_sync;
147 rtlpriv->io.write32 = _usb_write32_sync;
148 rtlpriv->io.write_chunk = _usb_write_chunk_sync;
149 rtlpriv->io.read8 = _usb_read8_sync;
150 rtlpriv->io.read16 = _usb_read16_sync;
151 rtlpriv->io.read32 = _usb_read32_sync;
152 }
153
_rtl_usb_io_handler_release(struct ieee80211_hw * hw)154 static void _rtl_usb_io_handler_release(struct ieee80211_hw *hw)
155 {
156 struct rtl_priv __maybe_unused *rtlpriv = rtl_priv(hw);
157
158 mutex_destroy(&rtlpriv->io.bb_mutex);
159 }
160
161 /* Default aggregation handler. Do nothing and just return the oldest skb. */
_none_usb_tx_aggregate_hdl(struct ieee80211_hw * hw,struct sk_buff_head * list)162 static struct sk_buff *_none_usb_tx_aggregate_hdl(struct ieee80211_hw *hw,
163 struct sk_buff_head *list)
164 {
165 return skb_dequeue(list);
166 }
167
168 #define IS_HIGH_SPEED_USB(udev) \
169 ((USB_SPEED_HIGH == (udev)->speed) ? true : false)
170
_rtl_usb_init_tx(struct ieee80211_hw * hw)171 static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
172 {
173 u32 i;
174 struct rtl_priv *rtlpriv = rtl_priv(hw);
175 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
176
177 rtlusb->max_bulk_out_size = IS_HIGH_SPEED_USB(rtlusb->udev)
178 ? USB_HIGH_SPEED_BULK_SIZE
179 : USB_FULL_SPEED_BULK_SIZE;
180
181 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG, "USB Max Bulk-out Size=%d\n",
182 rtlusb->max_bulk_out_size);
183
184 for (i = 0; i < __RTL_TXQ_NUM; i++) {
185 u32 ep_num = rtlusb->ep_map.ep_mapping[i];
186
187 if (!ep_num) {
188 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
189 "Invalid endpoint map setting!\n");
190 return -EINVAL;
191 }
192 }
193
194 rtlusb->usb_tx_post_hdl =
195 rtlpriv->cfg->usb_interface_cfg->usb_tx_post_hdl;
196 rtlusb->usb_tx_cleanup =
197 rtlpriv->cfg->usb_interface_cfg->usb_tx_cleanup;
198 rtlusb->usb_tx_aggregate_hdl =
199 (rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl)
200 ? rtlpriv->cfg->usb_interface_cfg->usb_tx_aggregate_hdl
201 : &_none_usb_tx_aggregate_hdl;
202
203 init_usb_anchor(&rtlusb->tx_submitted);
204 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
205 skb_queue_head_init(&rtlusb->tx_skb_queue[i]);
206 init_usb_anchor(&rtlusb->tx_pending[i]);
207 }
208 return 0;
209 }
210
211 static void _rtl_rx_work(struct tasklet_struct *t);
212
_rtl_usb_init_rx(struct ieee80211_hw * hw)213 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
214 {
215 struct rtl_priv *rtlpriv = rtl_priv(hw);
216 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
217 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
218
219 rtlusb->rx_max_size = rtlpriv->cfg->usb_interface_cfg->rx_max_size;
220 rtlusb->rx_urb_num = rtlpriv->cfg->usb_interface_cfg->rx_urb_num;
221 rtlusb->usb_rx_hdl = rtlpriv->cfg->usb_interface_cfg->usb_rx_hdl;
222 rtlusb->usb_rx_segregate_hdl =
223 rtlpriv->cfg->usb_interface_cfg->usb_rx_segregate_hdl;
224
225 pr_info("rx_max_size %d, rx_urb_num %d, in_ep %d\n",
226 rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
227 init_usb_anchor(&rtlusb->rx_submitted);
228 init_usb_anchor(&rtlusb->rx_cleanup_urbs);
229
230 skb_queue_head_init(&rtlusb->rx_queue);
231 tasklet_setup(&rtlusb->rx_work_tasklet, _rtl_rx_work);
232
233 return 0;
234 }
235
_rtl_usb_init(struct ieee80211_hw * hw)236 static int _rtl_usb_init(struct ieee80211_hw *hw)
237 {
238 struct rtl_priv *rtlpriv = rtl_priv(hw);
239 struct rtl_usb_priv *usb_priv = rtl_usbpriv(hw);
240 struct rtl_usb *rtlusb = rtl_usbdev(usb_priv);
241 int err;
242 u8 epidx;
243 struct usb_interface *usb_intf = rtlusb->intf;
244 u8 epnums = usb_intf->cur_altsetting->desc.bNumEndpoints;
245
246 rtlusb->out_ep_nums = rtlusb->in_ep_nums = 0;
247 for (epidx = 0; epidx < epnums; epidx++) {
248 struct usb_endpoint_descriptor *pep_desc;
249
250 pep_desc = &usb_intf->cur_altsetting->endpoint[epidx].desc;
251
252 if (usb_endpoint_dir_in(pep_desc)) {
253 if (usb_endpoint_xfer_bulk(pep_desc)) {
254 /* The vendor drivers assume there is only one
255 * bulk in ep and that it's the first in ep.
256 */
257 if (rtlusb->in_ep_nums == 0)
258 rtlusb->in_ep = usb_endpoint_num(pep_desc);
259 else
260 pr_warn("%s: bulk in endpoint is not the first in endpoint\n",
261 __func__);
262 }
263
264 rtlusb->in_ep_nums++;
265 } else if (usb_endpoint_dir_out(pep_desc)) {
266 if (rtlusb->out_ep_nums < RTL_USB_MAX_BULKOUT_NUM) {
267 if (usb_endpoint_xfer_bulk(pep_desc))
268 rtlusb->out_eps[rtlusb->out_ep_nums] =
269 usb_endpoint_num(pep_desc);
270 } else {
271 pr_warn("%s: found more bulk out endpoints than the expected %d\n",
272 __func__, RTL_USB_MAX_BULKOUT_NUM);
273 }
274
275 rtlusb->out_ep_nums++;
276 }
277
278 rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
279 "USB EP(0x%02x), MaxPacketSize=%d, Interval=%d\n",
280 pep_desc->bEndpointAddress, pep_desc->wMaxPacketSize,
281 pep_desc->bInterval);
282 }
283
284 if (rtlusb->out_ep_nums == 0) {
285 pr_err("No output end points found\n");
286 return -EINVAL;
287 }
288 /* usb endpoint mapping */
289 err = rtlpriv->cfg->usb_interface_cfg->usb_endpoint_mapping(hw);
290 if (err)
291 return err;
292
293 rtlusb->usb_mq_to_hwq = rtlpriv->cfg->usb_interface_cfg->usb_mq_to_hwq;
294
295 err = _rtl_usb_init_tx(hw);
296 if (err)
297 return err;
298
299 err = _rtl_usb_init_rx(hw);
300 if (err)
301 goto err_out;
302
303 return 0;
304
305 err_out:
306 _rtl_usb_cleanup_tx(hw);
307 return err;
308 }
309
rtl_usb_init_sw(struct ieee80211_hw * hw)310 static void rtl_usb_init_sw(struct ieee80211_hw *hw)
311 {
312 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
313 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
314 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
315 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
316
317 rtlhal->hw = hw;
318 ppsc->inactiveps = false;
319 ppsc->leisure_ps = false;
320 ppsc->fwctrl_lps = false;
321 ppsc->reg_fwctrl_lps = 3;
322 ppsc->reg_max_lps_awakeintvl = 5;
323 ppsc->fwctrl_psmode = FW_PS_DTIM_MODE;
324
325 /* IBSS */
326 mac->beacon_interval = 100;
327
328 /* AMPDU */
329 mac->min_space_cfg = 0;
330 mac->max_mss_density = 0;
331
332 /* set sane AMPDU defaults */
333 mac->current_ampdu_density = 7;
334 mac->current_ampdu_factor = 3;
335
336 /* QOS */
337 rtlusb->acm_method = EACMWAY2_SW;
338
339 /* IRQ */
340 /* HIMR - turn all on */
341 rtlusb->irq_mask[0] = 0xFFFFFFFF;
342 /* HIMR_EX - turn all on */
343 rtlusb->irq_mask[1] = 0xFFFFFFFF;
344 rtlusb->disablehwsm = true;
345 }
346
347 static void _rtl_rx_completed(struct urb *urb);
348
_rtl_prep_rx_urb(struct ieee80211_hw * hw,struct rtl_usb * rtlusb,struct urb * urb,gfp_t gfp_mask)349 static int _rtl_prep_rx_urb(struct ieee80211_hw *hw, struct rtl_usb *rtlusb,
350 struct urb *urb, gfp_t gfp_mask)
351 {
352 void *buf;
353
354 buf = usb_alloc_coherent(rtlusb->udev, rtlusb->rx_max_size, gfp_mask,
355 &urb->transfer_dma);
356 if (!buf) {
357 pr_err("Failed to usb_alloc_coherent!!\n");
358 return -ENOMEM;
359 }
360
361 usb_fill_bulk_urb(urb, rtlusb->udev,
362 usb_rcvbulkpipe(rtlusb->udev, rtlusb->in_ep),
363 buf, rtlusb->rx_max_size, _rtl_rx_completed, rtlusb);
364 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
365
366 return 0;
367 }
368
_rtl_usb_rx_process_agg(struct ieee80211_hw * hw,struct sk_buff * skb)369 static void _rtl_usb_rx_process_agg(struct ieee80211_hw *hw,
370 struct sk_buff *skb)
371 {
372 struct rtl_priv *rtlpriv = rtl_priv(hw);
373 u8 *rxdesc = skb->data;
374 struct ieee80211_hdr *hdr;
375 bool unicast = false;
376 __le16 fc;
377 struct ieee80211_rx_status rx_status = {0};
378 struct rtl_stats stats = {
379 .signal = 0,
380 .rate = 0,
381 };
382
383 skb_pull(skb, RTL_RX_DESC_SIZE);
384 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
385 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
386 hdr = rtl_get_hdr(skb);
387 fc = hdr->frame_control;
388 if (!stats.crc) {
389 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
390
391 if (is_broadcast_ether_addr(hdr->addr1)) {
392 /*TODO*/;
393 } else if (is_multicast_ether_addr(hdr->addr1)) {
394 /*TODO*/
395 } else {
396 unicast = true;
397 rtlpriv->stats.rxbytesunicast += skb->len;
398 }
399
400 if (ieee80211_is_data(fc)) {
401 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
402
403 if (unicast)
404 rtlpriv->link_info.num_rx_inperiod++;
405 }
406 /* static bcn for roaming */
407 rtl_beacon_statistic(hw, skb);
408 }
409 }
410
_rtl_usb_rx_process_noagg(struct ieee80211_hw * hw,struct sk_buff * skb)411 static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
412 struct sk_buff *skb)
413 {
414 struct rtl_priv *rtlpriv = rtl_priv(hw);
415 u8 *rxdesc = skb->data;
416 struct ieee80211_hdr *hdr;
417 bool unicast = false;
418 __le16 fc;
419 struct ieee80211_rx_status rx_status = {0};
420 struct rtl_stats stats = {
421 .signal = 0,
422 .rate = 0,
423 };
424
425 skb_pull(skb, RTL_RX_DESC_SIZE);
426 rtlpriv->cfg->ops->query_rx_desc(hw, &stats, &rx_status, rxdesc, skb);
427 skb_pull(skb, (stats.rx_drvinfo_size + stats.rx_bufshift));
428 hdr = rtl_get_hdr(skb);
429 fc = hdr->frame_control;
430 if (!stats.crc) {
431 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
432
433 if (is_broadcast_ether_addr(hdr->addr1)) {
434 /*TODO*/;
435 } else if (is_multicast_ether_addr(hdr->addr1)) {
436 /*TODO*/
437 } else {
438 unicast = true;
439 rtlpriv->stats.rxbytesunicast += skb->len;
440 }
441
442 if (ieee80211_is_data(fc)) {
443 rtlpriv->cfg->ops->led_control(hw, LED_CTL_RX);
444
445 if (unicast)
446 rtlpriv->link_info.num_rx_inperiod++;
447 }
448
449 /* static bcn for roaming */
450 rtl_beacon_statistic(hw, skb);
451
452 if (likely(rtl_action_proc(hw, skb, false)))
453 ieee80211_rx(hw, skb);
454 else
455 dev_kfree_skb_any(skb);
456 } else {
457 dev_kfree_skb_any(skb);
458 }
459 }
460
_rtl_rx_pre_process(struct ieee80211_hw * hw,struct sk_buff * skb)461 static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
462 {
463 struct sk_buff *_skb;
464 struct sk_buff_head rx_queue;
465 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
466
467 skb_queue_head_init(&rx_queue);
468 if (rtlusb->usb_rx_segregate_hdl)
469 rtlusb->usb_rx_segregate_hdl(hw, skb, &rx_queue);
470 WARN_ON(skb_queue_empty(&rx_queue));
471 while (!skb_queue_empty(&rx_queue)) {
472 _skb = skb_dequeue(&rx_queue);
473 _rtl_usb_rx_process_agg(hw, _skb);
474 ieee80211_rx(hw, _skb);
475 }
476 }
477
478 #define __RX_SKB_MAX_QUEUED 64
479
_rtl_rx_work(struct tasklet_struct * t)480 static void _rtl_rx_work(struct tasklet_struct *t)
481 {
482 struct rtl_usb *rtlusb = from_tasklet(rtlusb, t, rx_work_tasklet);
483 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
484 struct sk_buff *skb;
485
486 while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
487 if (unlikely(IS_USB_STOP(rtlusb))) {
488 dev_kfree_skb_any(skb);
489 continue;
490 }
491
492 if (likely(!rtlusb->usb_rx_segregate_hdl)) {
493 _rtl_usb_rx_process_noagg(hw, skb);
494 } else {
495 /* TO DO */
496 _rtl_rx_pre_process(hw, skb);
497 pr_err("rx agg not supported\n");
498 }
499 }
500 }
501
_rtl_rx_get_padding(struct ieee80211_hdr * hdr,unsigned int len)502 static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr,
503 unsigned int len)
504 {
505 #if NET_IP_ALIGN != 0
506 unsigned int padding = 0;
507 #endif
508
509 /* make function no-op when possible */
510 if (NET_IP_ALIGN == 0 || len < sizeof(*hdr))
511 return 0;
512
513 #if NET_IP_ALIGN != 0
514 /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
515 /* TODO: deduplicate common code, define helper function instead? */
516
517 if (ieee80211_is_data_qos(hdr->frame_control)) {
518 u8 *qc = ieee80211_get_qos_ctl(hdr);
519
520 padding ^= NET_IP_ALIGN;
521
522 /* Input might be invalid, avoid accessing memory outside
523 * the buffer.
524 */
525 if ((unsigned long)qc - (unsigned long)hdr < len &&
526 *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
527 padding ^= NET_IP_ALIGN;
528 }
529
530 if (ieee80211_has_a4(hdr->frame_control))
531 padding ^= NET_IP_ALIGN;
532
533 return padding;
534 #endif
535 }
536
537 #define __RADIO_TAP_SIZE_RSV 32
538
_rtl_rx_completed(struct urb * _urb)539 static void _rtl_rx_completed(struct urb *_urb)
540 {
541 struct rtl_usb *rtlusb = (struct rtl_usb *)_urb->context;
542 int err = 0;
543
544 if (unlikely(IS_USB_STOP(rtlusb)))
545 goto free;
546
547 if (likely(0 == _urb->status)) {
548 unsigned int padding;
549 struct sk_buff *skb;
550 unsigned int qlen;
551 unsigned int size = _urb->actual_length;
552 struct ieee80211_hdr *hdr;
553
554 if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
555 pr_err("Too short packet from bulk IN! (len: %d)\n",
556 size);
557 goto resubmit;
558 }
559
560 qlen = skb_queue_len(&rtlusb->rx_queue);
561 if (qlen >= __RX_SKB_MAX_QUEUED) {
562 pr_err("Pending RX skbuff queue full! (qlen: %d)\n",
563 qlen);
564 goto resubmit;
565 }
566
567 hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE);
568 padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE);
569
570 skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding);
571 if (!skb) {
572 pr_err("Can't allocate skb for bulk IN!\n");
573 goto resubmit;
574 }
575
576 _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
577
578 /* Make sure the payload data is 4 byte aligned. */
579 skb_reserve(skb, padding);
580
581 /* reserve some space for mac80211's radiotap */
582 skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
583
584 skb_put_data(skb, _urb->transfer_buffer, size);
585
586 skb_queue_tail(&rtlusb->rx_queue, skb);
587 tasklet_schedule(&rtlusb->rx_work_tasklet);
588
589 goto resubmit;
590 }
591
592 switch (_urb->status) {
593 /* disconnect */
594 case -ENOENT:
595 case -ECONNRESET:
596 case -ENODEV:
597 case -ESHUTDOWN:
598 goto free;
599 default:
600 break;
601 }
602
603 resubmit:
604 usb_anchor_urb(_urb, &rtlusb->rx_submitted);
605 err = usb_submit_urb(_urb, GFP_ATOMIC);
606 if (unlikely(err)) {
607 usb_unanchor_urb(_urb);
608 goto free;
609 }
610 return;
611
612 free:
613 /* On some architectures, usb_free_coherent must not be called from
614 * hardirq context. Queue urb to cleanup list.
615 */
616 usb_anchor_urb(_urb, &rtlusb->rx_cleanup_urbs);
617 }
618
619 #undef __RADIO_TAP_SIZE_RSV
620
_rtl_usb_cleanup_rx(struct ieee80211_hw * hw)621 static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
622 {
623 struct rtl_priv *rtlpriv = rtl_priv(hw);
624 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
625 struct urb *urb;
626
627 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
628
629 tasklet_kill(&rtlusb->rx_work_tasklet);
630 cancel_work_sync(&rtlpriv->works.lps_change_work);
631
632 if (rtlpriv->works.rtl_wq) {
633 destroy_workqueue(rtlpriv->works.rtl_wq);
634 rtlpriv->works.rtl_wq = NULL;
635 }
636
637 skb_queue_purge(&rtlusb->rx_queue);
638
639 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
640 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
641 urb->transfer_buffer, urb->transfer_dma);
642 usb_free_urb(urb);
643 }
644 }
645
_rtl_usb_receive(struct ieee80211_hw * hw)646 static int _rtl_usb_receive(struct ieee80211_hw *hw)
647 {
648 struct urb *urb;
649 int err;
650 int i;
651 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
652
653 WARN_ON(0 == rtlusb->rx_urb_num);
654 /* 1600 == 1514 + max WLAN header + rtk info */
655 WARN_ON(rtlusb->rx_max_size < 1600);
656
657 for (i = 0; i < rtlusb->rx_urb_num; i++) {
658 err = -ENOMEM;
659 urb = usb_alloc_urb(0, GFP_KERNEL);
660 if (!urb)
661 goto err_out;
662
663 err = _rtl_prep_rx_urb(hw, rtlusb, urb, GFP_KERNEL);
664 if (err < 0) {
665 pr_err("Failed to prep_rx_urb!!\n");
666 usb_free_urb(urb);
667 goto err_out;
668 }
669
670 usb_anchor_urb(urb, &rtlusb->rx_submitted);
671 err = usb_submit_urb(urb, GFP_KERNEL);
672 if (err) {
673 usb_unanchor_urb(urb);
674 usb_free_urb(urb);
675 goto err_out;
676 }
677 usb_free_urb(urb);
678 }
679 return 0;
680
681 err_out:
682 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
683 return err;
684 }
685
rtl_usb_start(struct ieee80211_hw * hw)686 static int rtl_usb_start(struct ieee80211_hw *hw)
687 {
688 int err;
689 struct rtl_priv *rtlpriv = rtl_priv(hw);
690 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
691 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
692
693 err = rtlpriv->cfg->ops->hw_init(hw);
694 if (!err) {
695 rtl_init_rx_config(hw);
696
697 /* Enable software */
698 SET_USB_START(rtlusb);
699 /* should after adapter start and interrupt enable. */
700 set_hal_start(rtlhal);
701
702 /* Start bulk IN */
703 err = _rtl_usb_receive(hw);
704 }
705
706 return err;
707 }
708
709 /*======================= tx =========================================*/
_rtl_usb_cleanup_tx(struct ieee80211_hw * hw)710 static void _rtl_usb_cleanup_tx(struct ieee80211_hw *hw)
711 {
712 u32 i;
713 struct sk_buff *_skb;
714 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
715 struct ieee80211_tx_info *txinfo;
716
717 for (i = 0; i < RTL_USB_MAX_EP_NUM; i++) {
718 while ((_skb = skb_dequeue(&rtlusb->tx_skb_queue[i]))) {
719 rtlusb->usb_tx_cleanup(hw, _skb);
720 txinfo = IEEE80211_SKB_CB(_skb);
721 ieee80211_tx_info_clear_status(txinfo);
722 txinfo->flags |= IEEE80211_TX_STAT_ACK;
723 ieee80211_tx_status_irqsafe(hw, _skb);
724 }
725 usb_kill_anchored_urbs(&rtlusb->tx_pending[i]);
726 }
727 usb_kill_anchored_urbs(&rtlusb->tx_submitted);
728 }
729
rtl_usb_cleanup(struct ieee80211_hw * hw)730 static void rtl_usb_cleanup(struct ieee80211_hw *hw)
731 {
732 _rtl_usb_cleanup_rx(hw);
733 _rtl_usb_cleanup_tx(hw);
734 }
735
736 /* We may add some struct into struct rtl_usb later. Do deinit here. */
rtl_usb_deinit(struct ieee80211_hw * hw)737 static void rtl_usb_deinit(struct ieee80211_hw *hw)
738 {
739 rtl_usb_cleanup(hw);
740 }
741
rtl_usb_stop(struct ieee80211_hw * hw)742 static void rtl_usb_stop(struct ieee80211_hw *hw)
743 {
744 struct rtl_priv *rtlpriv = rtl_priv(hw);
745 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
746 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
747 struct urb *urb;
748
749 /* should after adapter start and interrupt enable. */
750 set_hal_stop(rtlhal);
751 cancel_work_sync(&rtlpriv->works.fill_h2c_cmd);
752 /* Enable software */
753 SET_USB_STOP(rtlusb);
754
755 /* free pre-allocated URBs from rtl_usb_start() */
756 usb_kill_anchored_urbs(&rtlusb->rx_submitted);
757
758 tasklet_kill(&rtlusb->rx_work_tasklet);
759 cancel_work_sync(&rtlpriv->works.lps_change_work);
760 cancel_work_sync(&rtlpriv->works.update_beacon_work);
761
762 flush_workqueue(rtlpriv->works.rtl_wq);
763
764 skb_queue_purge(&rtlusb->rx_queue);
765
766 while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
767 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
768 urb->transfer_buffer, urb->transfer_dma);
769 usb_free_urb(urb);
770 }
771
772 rtlpriv->cfg->ops->hw_disable(hw);
773 }
774
_rtl_submit_tx_urb(struct ieee80211_hw * hw,struct urb * _urb)775 static void _rtl_submit_tx_urb(struct ieee80211_hw *hw, struct urb *_urb)
776 {
777 int err;
778 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
779
780 usb_anchor_urb(_urb, &rtlusb->tx_submitted);
781 err = usb_submit_urb(_urb, GFP_ATOMIC);
782 if (err < 0) {
783 struct sk_buff *skb;
784
785 pr_err("Failed to submit urb\n");
786 usb_unanchor_urb(_urb);
787 skb = (struct sk_buff *)_urb->context;
788 kfree_skb(skb);
789 }
790 usb_free_urb(_urb);
791 }
792
_usb_tx_post(struct ieee80211_hw * hw,struct urb * urb,struct sk_buff * skb)793 static int _usb_tx_post(struct ieee80211_hw *hw, struct urb *urb,
794 struct sk_buff *skb)
795 {
796 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
797 struct ieee80211_tx_info *txinfo;
798
799 rtlusb->usb_tx_post_hdl(hw, urb, skb);
800 skb_pull(skb, RTL_TX_HEADER_SIZE);
801 txinfo = IEEE80211_SKB_CB(skb);
802 ieee80211_tx_info_clear_status(txinfo);
803 txinfo->flags |= IEEE80211_TX_STAT_ACK;
804
805 if (urb->status) {
806 pr_err("Urb has error status 0x%X\n", urb->status);
807 goto out;
808 }
809 /* TODO: statistics */
810 out:
811 ieee80211_tx_status_irqsafe(hw, skb);
812 return urb->status;
813 }
814
_rtl_tx_complete(struct urb * urb)815 static void _rtl_tx_complete(struct urb *urb)
816 {
817 struct sk_buff *skb = (struct sk_buff *)urb->context;
818 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
819 struct rtl_usb *rtlusb = (struct rtl_usb *)info->rate_driver_data[0];
820 struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
821 int err;
822
823 if (unlikely(IS_USB_STOP(rtlusb)))
824 return;
825 err = _usb_tx_post(hw, urb, skb);
826 if (err) {
827 /* Ignore error and keep issuiing other urbs */
828 return;
829 }
830 }
831
_rtl_usb_tx_urb_setup(struct ieee80211_hw * hw,struct sk_buff * skb,u32 ep_num)832 static struct urb *_rtl_usb_tx_urb_setup(struct ieee80211_hw *hw,
833 struct sk_buff *skb, u32 ep_num)
834 {
835 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
836 struct urb *_urb;
837
838 WARN_ON(NULL == skb);
839 _urb = usb_alloc_urb(0, GFP_ATOMIC);
840 if (!_urb)
841 return NULL;
842 _rtl_install_trx_info(rtlusb, skb, ep_num);
843 usb_fill_bulk_urb(_urb, rtlusb->udev, usb_sndbulkpipe(rtlusb->udev,
844 ep_num), skb->data, skb->len, _rtl_tx_complete, skb);
845 _urb->transfer_flags |= URB_ZERO_PACKET;
846 return _urb;
847 }
848
_rtl_usb_transmit(struct ieee80211_hw * hw,struct sk_buff * skb,enum rtl_txq qnum)849 static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
850 enum rtl_txq qnum)
851 {
852 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
853 u32 ep_num;
854 struct urb *_urb = NULL;
855
856 WARN_ON(NULL == rtlusb->usb_tx_aggregate_hdl);
857 if (unlikely(IS_USB_STOP(rtlusb))) {
858 pr_err("USB device is stopping...\n");
859 kfree_skb(skb);
860 return;
861 }
862 ep_num = rtlusb->ep_map.ep_mapping[qnum];
863 _urb = _rtl_usb_tx_urb_setup(hw, skb, ep_num);
864 if (unlikely(!_urb)) {
865 pr_err("Can't allocate urb. Drop skb!\n");
866 kfree_skb(skb);
867 return;
868 }
869 _rtl_submit_tx_urb(hw, _urb);
870 }
871
_rtl_usb_tx_preprocess(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb,u16 hw_queue)872 static void _rtl_usb_tx_preprocess(struct ieee80211_hw *hw,
873 struct ieee80211_sta *sta,
874 struct sk_buff *skb,
875 u16 hw_queue)
876 {
877 struct rtl_priv *rtlpriv = rtl_priv(hw);
878 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
879 struct rtl_tx_desc *pdesc = NULL;
880 struct rtl_tcb_desc tcb_desc;
881 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
882 __le16 fc = hdr->frame_control;
883 u8 *pda_addr = hdr->addr1;
884
885 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
886 if (ieee80211_is_auth(fc)) {
887 rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
888 }
889
890 if (rtlpriv->psc.sw_ps_enabled) {
891 if (ieee80211_is_data(fc) && !ieee80211_is_nullfunc(fc) &&
892 !ieee80211_has_pm(fc))
893 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
894 }
895
896 rtl_action_proc(hw, skb, true);
897 if (is_multicast_ether_addr(pda_addr))
898 rtlpriv->stats.txbytesmulticast += skb->len;
899 else if (is_broadcast_ether_addr(pda_addr))
900 rtlpriv->stats.txbytesbroadcast += skb->len;
901 else
902 rtlpriv->stats.txbytesunicast += skb->len;
903 rtlpriv->cfg->ops->fill_tx_desc(hw, hdr, (u8 *)pdesc, NULL, info, sta, skb,
904 hw_queue, &tcb_desc);
905 if (ieee80211_is_data(fc))
906 rtlpriv->cfg->ops->led_control(hw, LED_CTL_TX);
907 }
908
rtl_usb_tx(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb,struct rtl_tcb_desc * dummy)909 static int rtl_usb_tx(struct ieee80211_hw *hw,
910 struct ieee80211_sta *sta,
911 struct sk_buff *skb,
912 struct rtl_tcb_desc *dummy)
913 {
914 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
915 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
916 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
917 __le16 fc = hdr->frame_control;
918 u16 hw_queue;
919
920 if (unlikely(is_hal_stop(rtlhal)))
921 goto err_free;
922 hw_queue = rtlusb->usb_mq_to_hwq(fc, skb_get_queue_mapping(skb));
923 _rtl_usb_tx_preprocess(hw, sta, skb, hw_queue);
924 _rtl_usb_transmit(hw, skb, hw_queue);
925 return NETDEV_TX_OK;
926
927 err_free:
928 dev_kfree_skb_any(skb);
929 return NETDEV_TX_OK;
930 }
931
rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb)932 static bool rtl_usb_tx_chk_waitq_insert(struct ieee80211_hw *hw,
933 struct ieee80211_sta *sta,
934 struct sk_buff *skb)
935 {
936 return false;
937 }
938
rtl_fill_h2c_cmd_work_callback(struct work_struct * work)939 static void rtl_fill_h2c_cmd_work_callback(struct work_struct *work)
940 {
941 struct rtl_works *rtlworks =
942 container_of(work, struct rtl_works, fill_h2c_cmd);
943 struct ieee80211_hw *hw = rtlworks->hw;
944 struct rtl_priv *rtlpriv = rtl_priv(hw);
945
946 rtlpriv->cfg->ops->fill_h2c_cmd(hw, H2C_RA_MASK, 5, rtlpriv->rate_mask);
947 }
948
949 static const struct rtl_intf_ops rtl_usb_ops = {
950 .adapter_start = rtl_usb_start,
951 .adapter_stop = rtl_usb_stop,
952 .adapter_tx = rtl_usb_tx,
953 .waitq_insert = rtl_usb_tx_chk_waitq_insert,
954 };
955
rtl_usb_probe(struct usb_interface * intf,const struct usb_device_id * id,const struct rtl_hal_cfg * rtl_hal_cfg)956 int rtl_usb_probe(struct usb_interface *intf,
957 const struct usb_device_id *id,
958 const struct rtl_hal_cfg *rtl_hal_cfg)
959 {
960 int err;
961 struct ieee80211_hw *hw = NULL;
962 struct rtl_priv *rtlpriv = NULL;
963 struct usb_device *udev;
964 struct rtl_usb_priv *usb_priv;
965
966 hw = ieee80211_alloc_hw(sizeof(struct rtl_priv) +
967 sizeof(struct rtl_usb_priv), &rtl_ops);
968 if (!hw) {
969 pr_warn("rtl_usb: ieee80211 alloc failed\n");
970 return -ENOMEM;
971 }
972 rtlpriv = hw->priv;
973 rtlpriv->hw = hw;
974 rtlpriv->usb_data = kcalloc(RTL_USB_MAX_RX_COUNT, sizeof(u32),
975 GFP_KERNEL);
976 if (!rtlpriv->usb_data) {
977 ieee80211_free_hw(hw);
978 return -ENOMEM;
979 }
980
981 /* this spin lock must be initialized early */
982 spin_lock_init(&rtlpriv->locks.usb_lock);
983 INIT_WORK(&rtlpriv->works.fill_h2c_cmd,
984 rtl_fill_h2c_cmd_work_callback);
985 INIT_WORK(&rtlpriv->works.lps_change_work,
986 rtl_lps_change_work_callback);
987 INIT_WORK(&rtlpriv->works.update_beacon_work,
988 rtl_update_beacon_work_callback);
989
990 rtlpriv->usb_data_index = 0;
991 init_completion(&rtlpriv->firmware_loading_complete);
992 SET_IEEE80211_DEV(hw, &intf->dev);
993 udev = interface_to_usbdev(intf);
994 usb_get_dev(udev);
995 usb_priv = rtl_usbpriv(hw);
996 memset(usb_priv, 0, sizeof(*usb_priv));
997 usb_priv->dev.intf = intf;
998 usb_priv->dev.udev = udev;
999 usb_set_intfdata(intf, hw);
1000 /* For dual MAC RTL8192DU, which has two interfaces. */
1001 rtlpriv->rtlhal.interfaceindex =
1002 intf->altsetting[0].desc.bInterfaceNumber;
1003 /* init cfg & intf_ops */
1004 rtlpriv->rtlhal.interface = INTF_USB;
1005 rtlpriv->cfg = rtl_hal_cfg;
1006 rtlpriv->intf_ops = &rtl_usb_ops;
1007 /* Init IO handler */
1008 _rtl_usb_io_handler_init(&udev->dev, hw);
1009 rtlpriv->cfg->ops->read_chip_version(hw);
1010 /*like read eeprom and so on */
1011 rtlpriv->cfg->ops->read_eeprom_info(hw);
1012 err = _rtl_usb_init(hw);
1013 if (err)
1014 goto error_out2;
1015 rtl_usb_init_sw(hw);
1016 /* Init mac80211 sw */
1017 err = rtl_init_core(hw);
1018 if (err) {
1019 pr_err("Can't allocate sw for mac80211\n");
1020 goto error_out2;
1021 }
1022 if (rtlpriv->cfg->ops->init_sw_vars(hw)) {
1023 pr_err("Can't init_sw_vars\n");
1024 goto error_out;
1025 }
1026 rtl_init_sw_leds(hw);
1027
1028 err = ieee80211_register_hw(hw);
1029 if (err) {
1030 pr_err("Can't register mac80211 hw.\n");
1031 goto error_out;
1032 }
1033 rtlpriv->mac80211.mac80211_registered = 1;
1034
1035 set_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1036 return 0;
1037
1038 error_out:
1039 rtl_deinit_core(hw);
1040 error_out2:
1041 _rtl_usb_io_handler_release(hw);
1042 usb_put_dev(udev);
1043 complete(&rtlpriv->firmware_loading_complete);
1044 kfree(rtlpriv->usb_data);
1045 ieee80211_free_hw(hw);
1046 return -ENODEV;
1047 }
1048 EXPORT_SYMBOL(rtl_usb_probe);
1049
rtl_usb_disconnect(struct usb_interface * intf)1050 void rtl_usb_disconnect(struct usb_interface *intf)
1051 {
1052 struct ieee80211_hw *hw = usb_get_intfdata(intf);
1053 struct rtl_priv *rtlpriv = rtl_priv(hw);
1054 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1055 struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
1056
1057 if (unlikely(!rtlpriv))
1058 return;
1059 /* just in case driver is removed before firmware callback */
1060 wait_for_completion(&rtlpriv->firmware_loading_complete);
1061 clear_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status);
1062 /*ieee80211_unregister_hw will call ops_stop */
1063 if (rtlmac->mac80211_registered == 1) {
1064 ieee80211_unregister_hw(hw);
1065 rtlmac->mac80211_registered = 0;
1066 } else {
1067 rtl_deinit_deferred_work(hw, false);
1068 rtlpriv->intf_ops->adapter_stop(hw);
1069 }
1070 /*deinit rfkill */
1071 /* rtl_deinit_rfkill(hw); */
1072 rtl_usb_deinit(hw);
1073 rtl_deinit_core(hw);
1074 kfree(rtlpriv->usb_data);
1075 rtlpriv->cfg->ops->deinit_sw_vars(hw);
1076 _rtl_usb_io_handler_release(hw);
1077 usb_put_dev(rtlusb->udev);
1078 usb_set_intfdata(intf, NULL);
1079 ieee80211_free_hw(hw);
1080 }
1081 EXPORT_SYMBOL(rtl_usb_disconnect);
1082
rtl_usb_suspend(struct usb_interface * pusb_intf,pm_message_t message)1083 int rtl_usb_suspend(struct usb_interface *pusb_intf, pm_message_t message)
1084 {
1085 return 0;
1086 }
1087 EXPORT_SYMBOL(rtl_usb_suspend);
1088
rtl_usb_resume(struct usb_interface * pusb_intf)1089 int rtl_usb_resume(struct usb_interface *pusb_intf)
1090 {
1091 return 0;
1092 }
1093 EXPORT_SYMBOL(rtl_usb_resume);
1094