-
Notifications
You must be signed in to change notification settings - Fork 15
/
usb.c
1226 lines (1020 loc) · 29.9 KB
/
usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* Copyright(c) 2018-2019 Realtek Corporation
*/
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#include "main.h"
#include "debug.h"
#include "reg.h"
#include "tx.h"
#include "rx.h"
#include "fw.h"
#include "ps.h"
#include "usb.h"
#define RTW_USB_CONTROL_MSG_TIMEOUT 30000 /* (us) */
#define RTW_USB_MSG_TIMEOUT 3000 /* (ms) */
#define RTW_USB_MAX_RXQ_LEN 128
struct rtw_usb_txcb_t {
struct rtw_dev *rtwdev;
struct sk_buff_head tx_ack_queue;
};
struct rtw_usb_ctrlcb_t {
bool done;
int status;
};
/* RTW queue / pipe functions */
static u8 rtw_usb_ac_to_hwq[] = {
[IEEE80211_AC_VO] = RTW_TX_QUEUE_VO,
[IEEE80211_AC_VI] = RTW_TX_QUEUE_VI,
[IEEE80211_AC_BE] = RTW_TX_QUEUE_BE,
[IEEE80211_AC_BK] = RTW_TX_QUEUE_BK,
};
static void rtw_usb_read_port(struct rtw_dev *rtwdev, u8 addr,
struct rx_usb_ctrl_block *rxcb);
static void rtw_usb_tx_agg(struct rtw_usb *rtwusb, struct sk_buff *skb);
static void rtw_usb_txcb_ack(struct rtw_usb_txcb_t *txcb);
static void rtw_usb_fill_tx_checksum(struct rtw_usb *rtwusb,
struct sk_buff *skb, int agg_num)
{
struct rtw_dev *rtwdev = rtwusb->rtwdev;
struct rtw_tx_pkt_info pkt_info;
SET_TX_DESC_DMA_TXAGG_NUM(skb->data, agg_num);
pkt_info.pkt_offset = GET_TX_DESC_PKT_OFFSET(skb->data);
rtw_tx_fill_txdesc_checksum(rtwdev, &pkt_info, skb->data);
}
static void usbctrl_async_callback(struct urb *urb)
{
if (urb) {
/* free dr */
kfree(urb->setup_packet);
/* free databuf */
kfree(urb->transfer_buffer);
}
}
static int _usbctrl_vendorreq_async_write(struct usb_device *udev, u8 request,
u16 value, u16 index, void *pdata,
u16 len)
{
int rc;
unsigned int pipe;
u8 reqtype;
struct usb_ctrlrequest *dr;
struct urb *urb;
const u16 databuf_maxlen = RTW_USB_VENQT_MAX_BUF_SIZE;
u8 *databuf;
if (WARN_ON_ONCE(len > databuf_maxlen))
len = databuf_maxlen;
pipe = usb_sndctrlpipe(udev, 0); /* write_out */
reqtype = RTW_USB_CMD_WRITE;
dr = kzalloc(sizeof(*dr), GFP_ATOMIC);
if (!dr)
return -ENOMEM;
databuf = kzalloc(databuf_maxlen, GFP_ATOMIC);
if (!databuf) {
kfree(dr);
return -ENOMEM;
}
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb) {
kfree(databuf);
kfree(dr);
return -ENOMEM;
}
dr->bRequestType = reqtype;
dr->bRequest = request;
dr->wValue = cpu_to_le16(value);
dr->wIndex = cpu_to_le16(index);
dr->wLength = cpu_to_le16(len);
/* data are already in little-endian order */
memcpy(databuf, pdata, len);
usb_fill_control_urb(urb, udev, pipe,
(unsigned char *)dr, databuf, len,
usbctrl_async_callback, NULL);
rc = usb_submit_urb(urb, GFP_ATOMIC);
if (rc < 0) {
kfree(databuf);
kfree(dr);
}
usb_free_urb(urb);
return rc;
}
static int _usbctrl_vendorreq_sync_read(struct usb_device *udev, u8 request,
u16 value, u16 index, void *pdata,
u16 len)
{
unsigned int pipe;
int status;
u8 reqtype;
int vendorreq_times = 0;
static int count;
pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
reqtype = RTW_USB_CMD_READ;
do {
status = usb_control_msg(udev, pipe, request, reqtype, value,
index, pdata, len, 1000);
if (status < 0) {
/* firmware download is checksumed, don't retry */
if ((value >= FW_8192C_START_ADDRESS &&
value <= FW_8192C_END_ADDRESS))
break;
} else {
break;
}
} while (++vendorreq_times < MAX_USBCTRL_VENDORREQ_TIMES);
if (status < 0 && count++ < 4)
pr_err("reg 0x%x, usbctrl_vendorreq TimeOut! status:0x%x value=0x%x\n",
value, status, *(u32 *)pdata);
return status;
}
static u32 _rtw_usb_read_sync(struct rtw_dev *rtwdev, u32 addr, u16 len)
{
struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv;
struct usb_device *udev = rtwusb->udev;
u8 request;
u16 wvalue;
u16 index;
__le32 *data;
unsigned long flags;
spin_lock_irqsave(&rtwusb->usb_lock, flags);
if (++rtwusb->usb_data_index >= RTW_USB_MAX_RX_COUNT)
rtwusb->usb_data_index = 0;
data = &rtwusb->usb_data[rtwusb->usb_data_index];
spin_unlock_irqrestore(&rtwusb->usb_lock, flags);
request = RTW_USB_CMD_REQ;
index = RTW_USB_VENQT_CMD_IDX; /* n/a */
wvalue = (u16)addr;
_usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
return le32_to_cpu(*data);
}
static u8 rtw_usb_read8_sync(struct rtw_dev *rtwdev, u32 addr)
{
return (u8)_rtw_usb_read_sync(rtwdev, addr, 1);
}
static u16 rtw_usb_read16_sync(struct rtw_dev *rtwdev, u32 addr)
{
return (u16)_rtw_usb_read_sync(rtwdev, addr, 2);
}
static u32 rtw_usb_read32_sync(struct rtw_dev *rtwdev, u32 addr)
{
return (u32)_rtw_usb_read_sync(rtwdev, addr, 4);
}
static void _rtw_usb_write_async(struct rtw_dev *rtwdev, u32 addr, u32 val,
u16 len)
{
struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv;
struct usb_device *udev = rtwusb->udev;
u8 request;
u16 wvalue;
u16 index;
__le32 data;
request = RTW_USB_CMD_REQ;
index = RTW_USB_VENQT_CMD_IDX; /* n/a */
wvalue = (u16)(addr&0x0000ffff);
data = cpu_to_le32(val);
_usbctrl_vendorreq_async_write(udev, request, wvalue, index, &data,
len);
}
static void rtw_usb_write8_async(struct rtw_dev *rtwdev, u32 addr, u8 val)
{
_rtw_usb_write_async(rtwdev, addr, val, 1);
}
static void rtw_usb_write16_async(struct rtw_dev *rtwdev, u32 addr, u16 val)
{
_rtw_usb_write_async(rtwdev, addr, val, 2);
}
static void rtw_usb_write32_async(struct rtw_dev *rtwdev, u32 addr, u32 val)
{
_rtw_usb_write_async(rtwdev, addr, val, 4);
}
static int rtw_usb_parse(struct rtw_dev *rtwdev,
struct usb_interface *interface)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct usb_host_interface *host_interface = &interface->altsetting[0];
struct usb_interface_descriptor *interface_desc = &host_interface->desc;
struct usb_endpoint_descriptor *endpoint;
struct usb_device *usbd = interface_to_usbdev(interface);
int endpoints = interface_desc->bNumEndpoints;
int i, ret = 0;
u8 num;
rtwusb->num_in_pipes = 0;
rtwusb->num_out_pipes = 0;
for (i = 0; i < endpoints; i++) {
endpoint = &host_interface->endpoint[i].desc;
num = usb_endpoint_num(endpoint);
if (usb_endpoint_dir_in(endpoint) &&
usb_endpoint_xfer_bulk(endpoint)) {
if (rtwusb->pipe_in) {
rtw_err(rtwdev, "IN pipes overflow\n");
ret = -EINVAL;
goto exit;
}
rtwusb->pipe_in = num;
rtwusb->num_in_pipes++;
}
if (usb_endpoint_dir_in(endpoint) &&
usb_endpoint_xfer_int(endpoint)) {
if (rtwusb->pipe_interrupt) {
rtw_err(rtwdev, "INT pipes overflow\n");
ret = -EINVAL;
goto exit;
}
rtwusb->pipe_interrupt = num;
}
if (usb_endpoint_dir_out(endpoint) &&
usb_endpoint_xfer_bulk(endpoint)) {
if (rtwusb->num_out_pipes >=
ARRAY_SIZE(rtwusb->out_ep)) {
rtw_err(rtwdev, "OUT pipes overflow\n");
ret = -EINVAL;
goto exit;
}
rtwusb->out_ep[rtwusb->num_out_pipes++] = num;
}
}
switch (usbd->speed) {
case USB_SPEED_LOW:
case USB_SPEED_FULL:
rtw_info(rtwdev, "USB: 1.1\n");
rtwusb->usb_speed = RTW_USB_SPEED_1_1;
break;
case USB_SPEED_HIGH:
rtw_info(rtwdev, "USB: 2\n");
rtwusb->usb_speed = RTW_USB_SPEED_2;
break;
case USB_SPEED_SUPER:
rtw_info(rtwdev, "USB: 3\n");
rtwusb->usb_speed = RTW_USB_SPEED_3;
break;
default:
rtw_err(rtwdev, "failed to detect usb speed\n");
break;
}
exit:
return ret;
}
static u8 rtw_usb_tx_queue_mapping(struct sk_buff *skb)
{
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
__le16 fc = hdr->frame_control;
u8 q_mapping = skb_get_queue_mapping(skb);
u8 queue = RTW_TX_QUEUE_BCN;
if (unlikely(ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc)))
queue = RTW_TX_QUEUE_MGMT;
else if (q_mapping <= IEEE80211_AC_BK)
queue = rtw_usb_ac_to_hwq[q_mapping];
return queue;
}
static unsigned int rtw_usb_get_pipe(struct rtw_usb *rtwusb, u32 addr)
{
struct rtw_dev *rtwdev = rtwusb->rtwdev;
struct usb_device *usbd = rtwusb->udev;
unsigned int ep_num, pipe = 0;
if (addr == RTW_USB_BULK_IN_ADDR) {
pipe = usb_rcvbulkpipe(usbd, rtwusb->pipe_in);
} else if (addr == RTW_USB_INT_IN_ADDR) {
pipe = usb_rcvintpipe(usbd, rtwusb->pipe_interrupt);
} else if (addr < RTW_USB_HW_QUEUE_ENTRY) {
ep_num = rtwusb->queue_to_pipe[addr];
pipe = usb_sndbulkpipe(usbd, ep_num);
} else {
rtw_err(rtwdev, "failed to get USB pipe, addr: %d\n", addr);
}
return pipe;
}
static void rtw_usb_one_outpipe_mapping(struct rtw_usb *rtwusb)
{
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VO] = rtwusb->out_ep[0];/* VO */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VI] = rtwusb->out_ep[0];/* VI */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BE] = rtwusb->out_ep[0];/* BE */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BK] = rtwusb->out_ep[0];/* BK */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BCN] = rtwusb->out_ep[0];/* BCN */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_MGMT] = rtwusb->out_ep[0];/* MGT */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_HI0] = rtwusb->out_ep[0];/* HIGH */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_H2C] = rtwusb->out_ep[0];/* TXCMD */
}
static void rtw_usb_two_outpipe_mapping(struct rtw_usb *rtwusb)
{
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VO] = rtwusb->out_ep[0];/* VO */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VI] = rtwusb->out_ep[0];/* VI */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BE] = rtwusb->out_ep[1];/* BE */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BK] = rtwusb->out_ep[1];/* BK */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BCN] = rtwusb->out_ep[0];/* BCN */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_MGMT] = rtwusb->out_ep[0];/* MGT */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_HI0] = rtwusb->out_ep[0];/* HIGH */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_H2C] = rtwusb->out_ep[0];/* TXCMD */
}
static void rtw_usb_three_outpipe_mapping(struct rtw_usb *rtwusb)
{
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VO] = rtwusb->out_ep[0];/* VO */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_VI] = rtwusb->out_ep[1];/* VI */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BE] = rtwusb->out_ep[2];/* BE */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BK] = rtwusb->out_ep[2];/* BK */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_BCN] = rtwusb->out_ep[0];/* BCN */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_MGMT] = rtwusb->out_ep[0];/* MGT */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_HI0] = rtwusb->out_ep[0];/* HIGH */
rtwusb->queue_to_pipe[RTW_TX_QUEUE_H2C] = rtwusb->out_ep[0];/* TXCMD */
}
static void rtw_usb_set_queue_pipe_mapping(struct rtw_dev *rtwdev, u8 in_pipes,
u8 out_pipes)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
rtwusb->out_ep_queue_sel = 0;
rtwdev->hci.bulkout_num = 0;
switch (out_pipes) {
case 4:
rtwusb->out_ep_queue_sel = RTW_USB_TX_SEL_HQ |
RTW_USB_TX_SEL_LQ |
RTW_USB_TX_SEL_NQ;
rtwdev->hci.bulkout_num = 4;
rtw_usb_three_outpipe_mapping(rtwusb);
break;
case 3:
rtwusb->out_ep_queue_sel = RTW_USB_TX_SEL_HQ |
RTW_USB_TX_SEL_LQ |
RTW_USB_TX_SEL_NQ;
rtwdev->hci.bulkout_num = 3;
rtw_usb_three_outpipe_mapping(rtwusb);
break;
case 2:
rtwusb->out_ep_queue_sel = RTW_USB_TX_SEL_HQ |
RTW_USB_TX_SEL_NQ;
rtwdev->hci.bulkout_num = 2;
rtw_usb_two_outpipe_mapping(rtwusb);
break;
case 1:
rtwusb->out_ep_queue_sel = RTW_USB_TX_SEL_HQ;
rtwdev->hci.bulkout_num = 1;
rtw_usb_one_outpipe_mapping(rtwusb);
break;
default:
rtw_err(rtwdev, "failed to get out_pipes(%d)\n", out_pipes);
break;
}
}
static void rtw_usb_interface_configure(struct rtw_dev *rtwdev)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
if (RTW_USB_IS_SUPER_SPEED(rtwusb))
rtwusb->bulkout_size = RTW_USB_SUPER_SPEED_BULK_SIZE;
else if (RTW_USB_IS_HIGH_SPEED(rtwusb))
rtwusb->bulkout_size = RTW_USB_HIGH_SPEED_BULK_SIZE;
else
rtwusb->bulkout_size = RTW_USB_FULL_SPEED_BULK_SIZE;
rtw_usb_set_queue_pipe_mapping(rtwdev, rtwusb->num_in_pipes,
rtwusb->num_out_pipes);
}
static void rtw_usb_tx_handler(struct work_struct *work)
{
struct rtw_usb_work_data *work_data = container_of(work,
struct rtw_usb_work_data,
work);
struct rtw_dev *rtwdev = work_data->rtwdev;
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct sk_buff *skb;
int index, limit;
index = RTK_MAX_TX_QUEUE_NUM - 1;
for (index = RTK_MAX_TX_QUEUE_NUM - 1; index >= 0; index--) {
for (limit = 0; limit < 200; limit++) {
skb = skb_dequeue(&rtwusb->tx_queue[index]);
if (skb)
rtw_usb_tx_agg(rtwusb, skb);
else
break;
}
}
}
static void rtw_usb_indicate_tx_status(struct rtw_dev *rtwdev,
struct sk_buff *skb)
{
struct ieee80211_hw *hw = rtwdev->hw;
struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
struct rtw_usb_tx_data *tx_data = rtw_usb_get_tx_data(skb);
/* enqueue to wait for tx report */
if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) {
rtw_tx_report_enqueue(rtwdev, skb, tx_data->sn);
return;
}
/* always ACK for others, then they won't be marked as drop */
ieee80211_tx_info_clear_status(info);
if (info->flags & IEEE80211_TX_CTL_NO_ACK)
info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
else
info->flags |= IEEE80211_TX_STAT_ACK;
ieee80211_tx_status_irqsafe(hw, skb);
}
static void rtw_usb_write_port_complete(struct urb *urb)
{
struct sk_buff *skb;
skb = (struct sk_buff *)urb->context;
dev_kfree_skb_any(skb);
}
static int rtw_usb_write_port(struct rtw_dev *rtwdev, u8 addr, u32 cnt,
struct sk_buff *skb,
usb_complete_t cb, void *context)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct usb_device *usbd = rtwusb->udev;
struct urb *urb;
unsigned int pipe;
int ret;
pipe = rtw_usb_get_pipe(rtwusb, addr);
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb)
return -ENOMEM;
usb_fill_bulk_urb(urb, usbd, pipe, skb->data, (int)cnt,
cb, context);
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(ret))
rtw_err(rtwdev, "failed to submit write urb, ret=%d\n", ret);
usb_free_urb(urb);
return ret;
}
static void rtw_usb_write_port_tx_complete(struct urb *urb)
{
struct rtw_usb_txcb_t *txcb;
txcb = urb->context;
rtw_usb_txcb_ack(txcb);
kfree(txcb);
}
static int rtw_usb_write_port_tx(struct rtw_dev *rtwdev, u8 addr, u32 cnt,
struct sk_buff *skb,
struct rtw_usb_txcb_t *txcb)
{
return rtw_usb_write_port(rtwdev, addr, cnt, skb,
rtw_usb_write_port_tx_complete, txcb);
}
static void rtw_usb_tx_queue_init(struct rtw_usb *rtwusb)
{
int i;
for (i = 0; i < RTK_MAX_TX_QUEUE_NUM; i++)
skb_queue_head_init(&rtwusb->tx_queue[i]);
}
static void rtw_usb_tx_queue_purge(struct rtw_usb *rtwusb)
{
int i;
for (i = 0; i < RTK_MAX_TX_QUEUE_NUM; i++)
skb_queue_purge(&rtwusb->tx_queue[i]);
}
static void rtw_usb_rx_queue_purge(struct rtw_usb *rtwusb)
{
skb_queue_purge(&rtwusb->rx_queue);
}
static struct rtw_usb_txcb_t *rtw_usb_txcb_init(struct rtw_dev *rtwdev)
{
struct rtw_usb_txcb_t *txcb;
txcb = kmalloc(sizeof(*txcb), GFP_ATOMIC);
if (!txcb)
return NULL;
txcb->rtwdev = rtwdev;
skb_queue_head_init(&txcb->tx_ack_queue);
return txcb;
}
static void rtw_usb_txcb_enqueue(struct rtw_usb_txcb_t *txcb,
struct sk_buff *skb)
{
skb_queue_tail(&txcb->tx_ack_queue, skb);
}
static void rtw_usb_txcb_ack(struct rtw_usb_txcb_t *txcb)
{
struct rtw_dev *rtwdev = txcb->rtwdev;
struct sk_buff *skb;
u8 qsel, queue;
int limit;
for (limit = 0; limit < 200; limit++) {
skb = skb_dequeue(&txcb->tx_ack_queue);
if (!skb) break;
qsel = GET_TX_DESC_QSEL(skb->data);
queue = rtw_tx_qsel_to_queue(qsel);
if (queue <= RTW_TX_QUEUE_VO)
rtw_usb_indicate_tx_status(rtwdev, skb);
else
dev_kfree_skb_any(skb);
}
}
static void rtw_usb_tx_agg_skb(struct rtw_usb *rtwusb,
struct sk_buff_head *list,
struct sk_buff *skb_head, struct sk_buff *skb,
struct rtw_usb_txcb_t *txcb)
{
struct sk_buff *skb_iter;
unsigned long flags;
u8 *data_ptr;
int agg_num = 0, len, max_len;
data_ptr = skb_head->data;
skb_iter = skb;
while (skb_iter) {
memcpy(data_ptr, skb_iter->data, skb_iter->len);
len = ALIGN(skb_iter->len, 8);
skb_put(skb_head, len);
data_ptr += len;
agg_num++;
rtw_usb_txcb_enqueue(txcb, skb);
spin_lock_irqsave(&list->lock, flags);
skb_iter = skb_peek(list);
max_len = RTW_USB_MAX_XMITBUF_SZ - skb_head->len;
if (skb_iter && skb_iter->len < max_len)
__skb_unlink(skb_iter, list);
else
skb_iter = NULL;
spin_unlock_irqrestore(&list->lock, flags);
}
if (agg_num > 1)
rtw_usb_fill_tx_checksum(rtwusb, skb_head, agg_num);
}
static struct sk_buff *rtw_usb_tx_agg_check(struct rtw_usb *rtwusb,
struct sk_buff *skb,
u8 queue,
struct rtw_usb_txcb_t *txcb)
{
struct sk_buff_head *list;
struct sk_buff *skb_head;
if (queue != RTW_TX_QUEUE_VO)
return NULL;
list = &rtwusb->tx_queue[queue];
if (skb_queue_empty(list))
return NULL;
skb_head = dev_alloc_skb(RTW_USB_MAX_XMITBUF_SZ);
if (!skb_head)
return NULL;
rtw_usb_tx_agg_skb(rtwusb, list, skb_head, skb, txcb);
return skb_head;
}
static void rtw_usb_tx_agg(struct rtw_usb *rtwusb, struct sk_buff *skb)
{
struct rtw_dev *rtwdev = rtwusb->rtwdev;
struct sk_buff *skb_head;
struct rtw_usb_txcb_t *txcb;
u8 queue, qsel;
int ret;
txcb = rtw_usb_txcb_init(rtwdev);
if (!txcb)
return;
qsel = GET_TX_DESC_QSEL(skb->data);
queue = rtw_tx_qsel_to_queue(qsel);
skb_head = rtw_usb_tx_agg_check(rtwusb, skb, queue, txcb);
if (!skb_head) {
skb_head = skb;
rtw_usb_txcb_enqueue(txcb, skb);
}
ret = rtw_usb_write_port_tx(rtwdev, queue, skb_head->len, skb_head,
txcb);
if (ret)
rtw_err(rtwdev, "failed to do USB write sync, ret=%d\n", ret);
if (skb_head != skb)
dev_kfree_skb(skb_head);
}
static int rtw_usb_write_data(struct rtw_dev *rtwdev,
struct rtw_tx_pkt_info *pkt_info,
u8 *buf)
{
struct rtw_chip_info *chip = rtwdev->chip;
struct sk_buff *skb;
unsigned int desclen, len, headsize, size;
u8 queue, qsel;
int ret = 0;
size = pkt_info->tx_pkt_size;
qsel = pkt_info->qsel;
desclen = chip->tx_pkt_desc_sz;
headsize = (pkt_info->offset) ? pkt_info->offset : desclen;
len = headsize + size;
skb = dev_alloc_skb(len);
if (unlikely(!skb))
return -ENOMEM;
skb_reserve(skb, headsize);
skb_put_data(skb, buf, size);
skb_push(skb, headsize);
memset(skb->data, 0, headsize);
rtw_tx_fill_tx_desc(pkt_info, skb);
rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
queue = rtw_tx_qsel_to_queue(qsel);
ret = rtw_usb_write_port(rtwdev, queue, len, skb,
rtw_usb_write_port_complete, skb);
if (unlikely(ret))
rtw_err(rtwdev, "failed to do USB write, ret=%d\n", ret);
return ret;
}
static int rtw_usb_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf,
u32 size)
{
struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_usb *rtwusb;
struct rtw_tx_pkt_info pkt_info = {0};
u32 len, desclen;
u8 qsel = TX_DESC_QSEL_BEACON;
if (unlikely(!rtwdev))
return -EINVAL;
rtwusb = rtw_get_usb_priv(rtwdev);
if (unlikely(!rtwusb))
return -EINVAL;
pkt_info.tx_pkt_size = size;
pkt_info.qsel = qsel;
desclen = chip->tx_pkt_desc_sz;
len = desclen + size;
if (len % rtwusb->bulkout_size == 0) {
len += RTW_USB_PACKET_OFFSET_SZ;
pkt_info.offset = desclen + RTW_USB_PACKET_OFFSET_SZ;
pkt_info.pkt_offset = 1;
} else {
pkt_info.offset = desclen;
}
return rtw_usb_write_data(rtwdev, &pkt_info, buf);
}
static int rtw_usb_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
{
struct rtw_tx_pkt_info pkt_info = {0};
u8 qsel = TX_DESC_QSEL_H2C;
pkt_info.tx_pkt_size = size;
pkt_info.qsel = qsel;
return rtw_usb_write_data(rtwdev, &pkt_info, buf);
}
static int rtw_usb_tx_write(struct rtw_dev *rtwdev,
struct rtw_tx_pkt_info *pkt_info,
struct sk_buff *skb)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_usb_tx_data *tx_data;
u8 *pkt_desc;
u8 queue = rtw_usb_tx_queue_mapping(skb);
if (!pkt_info)
return -EINVAL;
pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
pkt_info->qsel = rtw_tx_queue_to_qsel(skb, queue);
rtw_tx_fill_tx_desc(pkt_info, skb);
rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, skb->data);
tx_data = rtw_usb_get_tx_data(skb);
tx_data->sn = pkt_info->sn;
skb_queue_tail(&rtwusb->tx_queue[queue], skb);
return 0;
}
static void rtw_usb_tx_kick_off(struct rtw_dev *rtwdev)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
queue_work(rtwusb->txwq, &rtwusb->tx_handler_data->work);
}
static void rtw_usb_rx_handler(struct work_struct *work)
{
struct rtw_usb_work_data *work_data = container_of(work,
struct rtw_usb_work_data,
work);
struct rtw_dev *rtwdev = work_data->rtwdev;
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_rx_pkt_stat pkt_stat;
struct ieee80211_rx_status rx_status;
struct sk_buff *skb;
u32 pkt_desc_sz = chip->rx_pkt_desc_sz;
u32 pkt_offset;
u8 *rx_desc;
int limit;
for (limit = 0; limit < 200; limit++) {
skb = skb_dequeue(&rtwusb->rx_queue);
if (!skb) break;
rx_desc = skb->data;
chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat,
&rx_status);
pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz +
pkt_stat.shift;
if (pkt_stat.is_c2h) {
skb_put(skb, pkt_stat.pkt_len + pkt_offset);
rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset,
skb);
continue;
}
if (skb_queue_len(&rtwusb->rx_queue) >= RTW_USB_MAX_RXQ_LEN) {
rtw_err(rtwdev, "failed to get rx_queue, overflow\n");
dev_kfree_skb(skb);
continue;
}
skb_put(skb, pkt_stat.pkt_len);
skb_reserve(skb, pkt_offset);
memcpy(skb->cb, &rx_status, sizeof(rx_status));
ieee80211_rx_irqsafe(rtwdev->hw, skb);
}
}
static void rtw_usb_read_port_complete(struct urb *urb)
{
struct rx_usb_ctrl_block *rxcb = urb->context;
struct rtw_dev *rtwdev = (struct rtw_dev *)rxcb->data;
struct rtw_usb *rtwusb = (struct rtw_usb *)rtwdev->priv;
struct sk_buff *skb = rxcb->rx_skb;
rxcb->rx_urb = NULL;
if (urb->status == 0) {
if (urb->actual_length >= RTW_USB_MAX_RECVBUF_SZ ||
urb->actual_length < 24) {
rtw_err(rtwdev, "failed to get urb length:%d\n",
urb->actual_length);
if (skb)
dev_kfree_skb_any(skb);
} else {
skb_queue_tail(&rtwusb->rx_queue, skb);
queue_work(rtwusb->rxwq,
&rtwusb->rx_handler_data->work);
}
rtw_usb_read_port(rtwdev, RTW_USB_BULK_IN_ADDR, rxcb);
} else {
switch (urb->status) {
case -EINVAL:
case -EPIPE:
case -ENODEV:
case -ESHUTDOWN:
case -ENOENT:
case -EPROTO:
case -EILSEQ:
case -ETIME:
case -ECOMM:
case -EOVERFLOW:
case -EINPROGRESS:
break;
default:
rtw_err(rtwdev, "status unknown=%d\n", urb->status);
break;
}
if (skb)
dev_kfree_skb_any(skb);
}
}
static void rtw_usb_read_port(struct rtw_dev *rtwdev, u8 addr,
struct rx_usb_ctrl_block *rxcb)
{
struct urb *urb = NULL;
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct usb_device *usbd = rtwusb->udev;
struct sk_buff *skb;
unsigned int pipe;
size_t alignment;
u32 len;
int ret;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb)
return;
rxcb->data = (void *)rtwdev;
pipe = rtw_usb_get_pipe(rtwusb, RTW_USB_BULK_IN_ADDR);
len = RTW_USB_MAX_RECVBUF_SZ + RTW_USB_RECVBUFF_ALIGN_SZ;
skb = dev_alloc_skb(len);
if (!skb) {
usb_free_urb(urb);
return;
}
alignment = (size_t)skb->data & (RTW_USB_RECVBUFF_ALIGN_SZ - 1);
skb_reserve(skb, RTW_USB_RECVBUFF_ALIGN_SZ - alignment);
urb->transfer_buffer = skb->data;
rxcb->rx_urb = urb;
rxcb->rx_skb = skb;
usb_fill_bulk_urb(urb, usbd, pipe,
urb->transfer_buffer,
RTW_USB_MAX_RECVBUF_SZ,
rtw_usb_read_port_complete,
rxcb);
ret = usb_submit_urb(urb, GFP_ATOMIC);
if (ret)
rtw_err(rtwdev, "failed to submit USB urb, ret=%d\n", ret);
usb_free_urb(urb);
}
static void rtw_usb_inirp_init(struct rtw_dev *rtwdev)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct rx_usb_ctrl_block *rxcb;
int i;
for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
rxcb = &rtwusb->rx_cb[i];
rxcb->rx_urb = NULL;
}
for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
rxcb = &rtwusb->rx_cb[i];
rtw_usb_read_port(rtwdev, RTW_USB_BULK_IN_ADDR, rxcb);
}
}
static void rtw_usb_inirp_deinit(struct rtw_dev *rtwdev)
{
struct rtw_usb *rtwusb = rtw_get_usb_priv(rtwdev);
struct rx_usb_ctrl_block *rxcb;
int i;
for (i = 0; i < RTW_USB_RXCB_NUM; i++) {
rxcb = &rtwusb->rx_cb[i];
if (rxcb->rx_urb) {
usb_kill_urb(rxcb->rx_urb);
usb_free_urb(rxcb->rx_urb);
}
}
}
static int rtw_usb_setup(struct rtw_dev *rtwdev)
{
/* empty function for rtw_hci_ops */
return 0;
}
static int rtw_usb_start(struct rtw_dev *rtwdev)
{
rtw_usb_inirp_init(rtwdev);
return 0;
}
static void rtw_usb_stop(struct rtw_dev *rtwdev)
{
rtw_usb_inirp_deinit(rtwdev);
}
static void rtw_usb_deep_ps(struct rtw_dev *rtwdev, bool enter)
{
/* empty function for rtw_hci_ops */
}
static void rtw_usb_link_ps(struct rtw_dev *rtwdev, bool enter)
{
/* empty function for rtw_hci_ops */
}
static void rtw_usb_interface_cfg(struct rtw_dev *rtwdev)
{
/* empty function for rtw_hci_ops */
}
static struct rtw_hci_ops rtw_usb_ops = {
.tx_write = rtw_usb_tx_write,
.tx_kick_off = rtw_usb_tx_kick_off,
.setup = rtw_usb_setup,
.start = rtw_usb_start,
.stop = rtw_usb_stop,
.deep_ps = rtw_usb_deep_ps,
.link_ps = rtw_usb_link_ps,
.interface_cfg = rtw_usb_interface_cfg,
.write8 = rtw_usb_write8_async,
.write16 = rtw_usb_write16_async,
.write32 = rtw_usb_write32_async,
.read8 = rtw_usb_read8_sync,
.read16 = rtw_usb_read16_sync,
.read32 = rtw_usb_read32_sync,