-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtlfmac.c
More file actions
1668 lines (1412 loc) · 44.7 KB
/
rtlfmac.c
File metadata and controls
1668 lines (1412 loc) · 44.7 KB
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
/******************************************************************************
*
* Portions Copyright(c) 2013 Joshua Roys
* Portions Copyright(c) 2009-2012 Realtek Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* The full GNU General Public License is included in this distribution in the
* file called LICENSE.
*
*****************************************************************************/
#include <linux/etherdevice.h>
#include <linux/ieee80211.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/usb.h>
#include <net/cfg80211.h>
#include "rtlfmac.h"
/* USB communication functions */
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 = REALTEK_USB_VENQT_READ;
do {
status = usb_control_msg(udev, pipe, request, reqtype, value,
index, pdata, len, 0); /*max. timeout*/
} while (status < 0 && ++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, le32_to_cpu(*(u32 *)pdata));
return status;
}
static int _usbctrl_vendorreq_sync_write(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_sndctrlpipe(udev, 0); /* write_out */
reqtype = REALTEK_USB_VENQT_WRITE;
do {
status = usb_control_msg(udev, pipe, request, reqtype, value,
index, pdata, len, 0); /*max. timeout*/
} while (status < 0 && ++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, le32_to_cpu(*(u32 *)pdata));
return status;
}
static u32 _usb_read_sync(struct usb_device *udev, u32 addr, u16 len)
{
u8 request;
u16 wvalue;
u16 index;
u32 *data;
u32 ret;
data = kmalloc(sizeof(u32), GFP_KERNEL);
if (!data)
return -ENOMEM;
request = REALTEK_USB_VENQT_CMD_REQ;
index = REALTEK_USB_VENQT_CMD_IDX;
wvalue = (u16)addr;
_usbctrl_vendorreq_sync_read(udev, request, wvalue, index, data, len);
ret = le32_to_cpu(*data);
kfree(data);
return ret;
}
static int _usb_write_sync(struct usb_device *udev, u32 addr, u32 val, u16 len)
{
u8 request;
u16 wvalue;
u16 index;
u32 *data;
int ret;
data = kmalloc(sizeof(u32), GFP_KERNEL);
if (!data)
return -ENOMEM;
request = REALTEK_USB_VENQT_CMD_REQ;
index = REALTEK_USB_VENQT_CMD_IDX;
wvalue = (u16)addr;
*data = val;
ret = _usbctrl_vendorreq_sync_write(udev, request, wvalue, index, data, len);
kfree(data);
return ret;
}
static void _usb_write_bulk_complete(struct urb *_urb)
{
struct sk_buff *skb = (struct sk_buff *)_urb->context;
dev_kfree_skb_irq(skb);
}
static int _usb_write_bulk(struct usb_device *udev, struct sk_buff *skb, u32 ep_num)
{
int ret;
struct urb *_urb;
_urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!_urb) {
pr_err("%s: failed to allocate URB\n", __func__);
dev_kfree_skb(skb);
return -ENOMEM;
}
usb_fill_bulk_urb(_urb, udev, usb_sndbulkpipe(udev, ep_num), skb->data, skb->len,
_usb_write_bulk_complete, skb);
_urb->transfer_flags |= URB_ZERO_PACKET;
//usb_anchor_urb(_urb, ...); FIXME
ret = usb_submit_urb(_urb, GFP_ATOMIC);
if (ret < 0) {
//usb_unanchor_urb(_urb);
dev_kfree_skb(skb);
}
usb_free_urb(_urb);
return ret;
}
static u8 rtlfmac_read_byte(struct rtlfmac_cfg80211_priv *priv, u32 addr)
{
return _usb_read_sync(priv->usbdev, addr, 1);
}
static u16 rtlfmac_read_word(struct rtlfmac_cfg80211_priv *priv, u32 addr)
{
return _usb_read_sync(priv->usbdev, addr, 2);
}
static u32 rtlfmac_read_dword(struct rtlfmac_cfg80211_priv *priv, u32 addr)
{
return _usb_read_sync(priv->usbdev, addr, 4);
}
static int rtlfmac_write_byte(struct rtlfmac_cfg80211_priv *priv, u32 addr, u8 val)
{
return _usb_write_sync(priv->usbdev, addr, (u32)val, 1);
}
static int rtlfmac_write_word(struct rtlfmac_cfg80211_priv *priv, u32 addr, u16 val)
{
val = cpu_to_le16(val);
return _usb_write_sync(priv->usbdev, addr, (u32)val, 2);
}
static int rtlfmac_write_dword(struct rtlfmac_cfg80211_priv *priv, u32 addr, u32 val)
{
val = cpu_to_le32(val);
return _usb_write_sync(priv->usbdev, addr, val, 4);
}
static int rtlfmac_write_skb(struct rtlfmac_cfg80211_priv *priv, struct sk_buff *skb)
{
u32 ep_num = priv->ep_mapping[skb_get_queue_mapping(skb)];
return _usb_write_bulk(priv->usbdev, skb, ep_num);
}
/* USB RX functions */
static void rtlfmac_rx_cleanup(struct rtlfmac_cfg80211_priv *priv)
{
struct urb *urb;
usb_kill_anchored_urbs(&priv->rx_submitted);
tasklet_kill(&priv->rx_work_tasklet);
skb_queue_purge(&priv->rx_queue);
while ((urb = usb_get_from_anchor(&priv->rx_cleanup))) {
usb_free_coherent(priv->usbdev, urb->transfer_buffer_length,
urb->transfer_buffer, urb->transfer_dma);
usb_free_urb(urb);
}
}
static void rtlfmac_rx_survey_resp(struct rtlfmac_cfg80211_priv *priv, u8 *data)
{
int freq;
size_t ie_len;
struct cfg80211_bss *bss;
struct ieee80211_channel *chan;
struct ndis_802_11_fixed_ies *fixed;
struct ndis_wlan_bssid_ex *survey = (struct ndis_wlan_bssid_ex *)data;
s32 signal;
u8 *ie;
u16 caps, beaconint;
u32 bssid_len;
u64 tsf;
netdev_dbg(priv->ndev, "%s: found BSS %s/%pM, channel %i\n", __func__, survey->ssid.ssid,
survey->macaddr, survey->config.dsconfig);
bssid_len = le32_to_cpu(survey->len);
if (bssid_len < sizeof(struct ndis_wlan_bssid_ex) +
sizeof(struct ndis_802_11_fixed_ies))
return;
fixed = (struct ndis_802_11_fixed_ies *)survey->ies;
if (sizeof(struct ndis_802_11_fixed_ies) > survey->ielen) {
ie = NULL;
ie_len = 0;
} else {
ie = survey->ies + sizeof(struct ndis_802_11_fixed_ies);
ie_len = survey->ielen - sizeof(struct ndis_802_11_fixed_ies);
}
freq = ieee80211_channel_to_frequency(survey->config.dsconfig,
NL80211_BAND_2GHZ);
chan = ieee80211_get_channel(priv->wiphy, freq);
//signal = DBM_TO_MBM(rtl92s_signal_scale_mapping(le32_to_cpu(survey->rssi)));
signal = 0;
tsf = le64_to_cpu(*(__le64 *)fixed->timestamp);
caps = le16_to_cpu(fixed->caps);
beaconint = le16_to_cpu(fixed->beaconint);
bss = cfg80211_inform_bss(priv->wiphy, chan, CFG80211_BSS_FTYPE_UNKNOWN, survey->macaddr,
tsf, caps, beaconint, ie, ie_len, signal, GFP_ATOMIC);
cfg80211_put_bss(priv->wiphy, bss);
}
static void rtlfmac_rx_join_resp(struct rtlfmac_cfg80211_priv *priv, u8 *data)
{
struct wlan_network *res = (struct wlan_network *)data;
u16 status;
if (!priv->connecting) {
return;
}
priv->connecting = false;
netdev_dbg(priv->ndev, "%s: aid(%d) join_res(%i)\n", __func__, res->aid, res->join_res);
switch(res->join_res) {
case -2:
status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
break;
case -1:
status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
break;
default:
status = WLAN_STATUS_SUCCESS;
break;
}
cfg80211_connect_result(priv->ndev, res->network.macaddr, NULL, 0,
res->network.ies, res->network.ielen, status, GFP_ATOMIC);
if (WLAN_STATUS_SUCCESS == status) {
memcpy(priv->bssid, res->network.macaddr, ETH_ALEN);
netif_carrier_on(priv->ndev);
}
}
/* Unfortunately, ieee80211_data_to_8023 doesn't handle the IV junk bytes
* between the 802.11 header and the SNAP header. Thus, our own function.
*/
static int rtlfmac_data_to_8023(struct sk_buff *skb, const uint8_t *addr, enum nl80211_iftype iftype, int iv_len)
{
int hdrlen;
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
hdrlen = ieee80211_hdrlen(hdr->frame_control);
if (ieee80211_has_protected(hdr->frame_control)) {
memmove(skb->data + iv_len, skb->data, hdrlen);
skb_pull(skb, iv_len);
}
return ieee80211_data_to_8023(skb, addr, iftype);
}
static void rtlfmac_rx_process(struct rtlfmac_cfg80211_priv *priv, struct sk_buff *skb)
{
struct rtlfmac_rx_desc *pdesc;
pdesc = (struct rtlfmac_rx_desc *)skb->data;
skb_pull(skb, RTL_RX_HEADER_SIZE);
if (pdesc->macid == 0x1f && pdesc->tid == 0x0f) {
/* C2H event */
u8 evnum;
u16 evlen;
struct rtlfmac_rx_c2h_desc *c2h;
c2h = (struct rtlfmac_rx_c2h_desc *)skb->data;
skb_pull(skb, 8); /* 8 byte alignment */
evnum = c2h->evnum;
evlen = le16_to_cpu(c2h->len);
switch(evnum) {
case C2H_SURVEY_EVENT:
rtlfmac_rx_survey_resp(priv, skb->data);
break;
case C2H_SURVEY_DONE_EVENT:
cfg80211_scan_done(priv->scan_request, false);
priv->scan_request = NULL;
break;
case C2H_JOIN_BSS_EVENT:
rtlfmac_rx_join_resp(priv, skb->data);
break;
case C2H_DEL_STA_EVENT:
cfg80211_disconnected(priv->ndev, 0, NULL, 0, false, GFP_ATOMIC);
break;
case C2H_FWDBG_EVENT:
netdev_dbg(priv->ndev, "%s: fwdbg: %s%s", __func__, skb->data,
(skb->data[evlen - 2] == '\n' ? "" : "\n"));
break;
default:
netdev_warn(priv->ndev, "%s: unhandled C2H %i\n", __func__, evnum);
break;
}
dev_kfree_skb_any(skb);
return;
}
// remove PHY status
skb_pull(skb, pdesc->drvinfo_size * 8);
// convert 802.11 frame to 802.3 frame
if (rtlfmac_data_to_8023(skb, priv->ndev->perm_addr, priv->wdev->iftype, priv->iv_len)) {
netdev_err(priv->ndev, "%s: failed to convert 802.11 to 802.3\n", __func__);
dev_kfree_skb_any(skb);
return;
}
skb->dev = priv->ndev;
skb->protocol = eth_type_trans(skb, skb->dev);
netif_rx(skb);
}
static void rtlfmac_rx_tasklet(unsigned long param)
{
struct rtlfmac_cfg80211_priv *priv = (struct rtlfmac_cfg80211_priv *)param;
struct sk_buff *skb;
while ((skb = skb_dequeue(&priv->rx_queue))) {
rtlfmac_rx_process(priv, skb);
}
}
static void rtlfmac_rx_complete(struct urb *urb)
{
int err;
struct rtlfmac_cfg80211_priv *priv = (struct rtlfmac_cfg80211_priv *)urb->context;
if (likely(0 == urb->status)) {
struct sk_buff *skb;
unsigned int size = urb->actual_length;
/* FIXME check size */
skb = dev_alloc_skb(size + 32); /* for radiotap */
if (!skb) {
pr_err("%s: failed to allocate skb\n", __func__);
goto resubmit;
}
skb_reserve(skb, 32); /* for radiotap */
memcpy(skb_put(skb, size), urb->transfer_buffer, size);
skb_queue_tail(&priv->rx_queue, skb);
tasklet_schedule(&priv->rx_work_tasklet);
goto resubmit;
}
switch(urb->status) {
/* disconnect */
case -ENOENT:
case -ECONNRESET:
case -ENODEV:
case -ESHUTDOWN:
goto free;
default:
break;
}
resubmit:
usb_anchor_urb(urb, &priv->rx_submitted);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(err)) {
pr_err("%s: failed to submit URB\n", __func__);
usb_unanchor_urb(urb);
goto free;
}
return;
free:
/* On some architectures, usb_free_coherent must not be called from
* hardirq context. Queue urb to cleanup list.
*/
usb_anchor_urb(urb, &priv->rx_cleanup);
}
static int rtlfmac_rx_start(struct rtlfmac_cfg80211_priv *priv)
{
int i, ret = 0;
struct urb *urb;
void *buf;
priv->connecting = false;
priv->ieee8021x_blocked = false;
init_usb_anchor(&priv->rx_cleanup);
init_usb_anchor(&priv->rx_submitted);
skb_queue_head_init(&priv->rx_queue);
priv->rx_work_tasklet.func = rtlfmac_rx_tasklet;
priv->rx_work_tasklet.data = (unsigned long)priv;
for(i = 0; i < RTL_NUM_RX_URBS; i++) {
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
pr_err("%s: failed to allocate URB\n", __func__);
ret = -ENOMEM;
break;
}
buf = usb_alloc_coherent(priv->usbdev, RTL_MAX_RX_SIZE, GFP_KERNEL,
&urb->transfer_dma);
if (!buf) {
pr_err("%s: failed to allocate USB coherent buffer\n", __func__);
usb_free_urb(urb);
ret = -ENOMEM;
break;
}
usb_fill_bulk_urb(urb, priv->usbdev, usb_rcvbulkpipe(priv->usbdev, priv->in_ep_num),
buf, RTL_MAX_RX_SIZE, rtlfmac_rx_complete, priv);
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
usb_anchor_urb(urb, &priv->rx_submitted);
ret = usb_submit_urb(urb, GFP_KERNEL);
if (unlikely(ret)) {
pr_err("%s: failed to submit URB\n", __func__);
usb_unanchor_urb(urb);
usb_free_coherent(priv->usbdev, RTL_MAX_RX_SIZE, buf,
urb->transfer_dma);
usb_free_urb(urb);
break;
}
usb_free_urb(urb);
}
if (unlikely(ret)) {
rtlfmac_rx_cleanup(priv);
}
return ret;
}
/* rtlfmac functions */
int rtlfmac_fw_cmd(struct rtlfmac_cfg80211_priv *priv, uint8_t code, void *buf, int len)
{
u8 *ptr;
struct rtlfmac_tx_desc *pdesc;
struct rtlfmac_tx_h2c_desc *h2c;
struct sk_buff *skb;
skb = dev_alloc_skb(len + RTL_TX_HEADER_SIZE + 8);
if (!skb) {
return -ENOMEM;
}
skb_reserve(skb, RTL_TX_HEADER_SIZE + 8);
ptr = skb_put(skb, len);
memcpy(ptr, buf, len);
h2c = (struct rtlfmac_tx_h2c_desc *)skb_push(skb, 8);
memset(h2c, 0, 8);
h2c->len = cpu_to_le16(len);
h2c->cmdid = code;
h2c->seqno = priv->h2c_cmd_seqno++;
pdesc = (struct rtlfmac_tx_desc *)skb_push(skb, RTL_TX_HEADER_SIZE);
memset(pdesc, 0, RTL_TX_HEADER_SIZE);
pdesc->first_seg = pdesc->last_seg = 1;
pdesc->offset = RTL_TX_HEADER_SIZE;
pdesc->pkt_size = cpu_to_le32(len + 8);
pdesc->queue_sel = 0x13;
pdesc->own = 1;
skb->queue_mapping = RTL_TXQ_H2CCMD;
return rtlfmac_write_skb(priv, skb);
}
int rtlfmac_sitesurvey(struct rtlfmac_cfg80211_priv *priv, struct cfg80211_scan_request *req)
{
struct rtlfmac_sitesurvey_cmd cmd;
priv->scan_request = req;
memset(&cmd, 0, sizeof(cmd));
cmd.bsslimit = cpu_to_le32(48);
if (req->n_ssids) {
cmd.active = cpu_to_le32(1);
cmd.ssidlen = cpu_to_le32(req->ssids[0].ssid_len);
memcpy(cmd.ssid, req->ssids[0].ssid, req->ssids[0].ssid_len);
}
return rtlfmac_fw_cmd(priv, H2C_SITESURVEY_CMD, &cmd, sizeof(cmd));
}
int rtlfmac_connect(struct rtlfmac_cfg80211_priv *priv, struct net_device *ndev,
struct cfg80211_connect_params *sme)
{
int chan = -1, ret;
size_t ie_len;
u8 *ie_ptr;
struct cfg80211_bss *bss;
struct ieee80211_channel *channel = sme->channel;
struct ieee80211_ht_cap *ht_cap;
struct ndis_802_11_fixed_ies *fixed;
struct rtlfmac_joinbss_cmd *cmd;
struct rtlfmac_setauth_cmd *authcmd;
struct rtlfmac_setopmode_cmd *modecmd;
bss = cfg80211_get_bss(priv->wiphy, channel, sme->bssid, sme->ssid,
sme->ssid_len, WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
if (!bss) {
netdev_err(ndev, "%s: Unable to find BSS\n", __func__);
return -ENOENT;
}
if (!channel) {
channel = bss->channel;
}
if (channel) {
chan = ieee80211_frequency_to_channel(channel->center_freq);
}
netdev_dbg(ndev, "%s: '%.*s':[%pM]:%d:[%d,0x%x:0x%x]\n", __func__,
(int)sme->ssid_len, sme->ssid, bss->bssid, chan, sme->privacy,
sme->crypto.wpa_versions, sme->auth_type);
// set_opmode
modecmd = kzalloc(sizeof(struct rtlfmac_setopmode_cmd), GFP_KERNEL);
if (!modecmd) {
ret = -ENOMEM;
goto done;
}
modecmd->opmode = IW_MODE_INFRA;
ret = rtlfmac_fw_cmd(priv, H2C_SETOPMODE_CMD, modecmd, sizeof(*modecmd));
kfree(modecmd);
if (ret) {
goto done;
}
// set_auth
authcmd = kzalloc(sizeof(struct rtlfmac_setauth_cmd), GFP_KERNEL);
if (!authcmd) {
ret = -ENOMEM;
goto done;
}
if (sme->crypto.wpa_versions) {
authcmd->mode = IW_AUTHMODE_WPA;
priv->ieee8021x_blocked = true;
} else if (sme->auth_type == NL80211_AUTHTYPE_SHARED_KEY) {
authcmd->mode = IW_AUTHMODE_SHARED;
} else if (sme->auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM) {
authcmd->mode = IW_AUTHMODE_OPEN;
} else { // default to open
authcmd->mode = IW_AUTHMODE_OPEN;
}
priv->cipher_pairwise = sme->crypto.ciphers_pairwise[0];
switch (sme->crypto.ciphers_pairwise[0]) {
case 0:
priv->iv_len = 0;
break;
case WLAN_CIPHER_SUITE_WEP40:
priv->iv_len = IEEE80211_WEP_IV_LEN;
break;
case WLAN_CIPHER_SUITE_CCMP:
priv->iv_len = IEEE80211_CCMP_HDR_LEN;
break;
}
priv->cipher_group = sme->crypto.cipher_group;
ret = rtlfmac_fw_cmd(priv, H2C_SETAUTH_CMD, authcmd, sizeof(*authcmd));
kfree(authcmd);
if (ret) {
goto done;
}
// joinbss
ie_len = sizeof(struct ndis_802_11_fixed_ies) + sme->ie_len;
if (1) {
ie_len += (2 + sizeof(struct ieee80211_ht_cap));
}
cmd = kzalloc(sizeof(struct rtlfmac_joinbss_cmd) + ie_len, GFP_KERNEL);
if (!cmd) {
ret = -ENOMEM;
goto done;
}
cmd->network.len = cpu_to_le32(sizeof(struct ndis_wlan_bssid_ex) + ie_len);
memcpy(cmd->network.macaddr, bss->bssid, ETH_ALEN);
cmd->network.ssid.ssidlen = cpu_to_le32(sme->ssid_len);
memcpy(cmd->network.ssid.ssid, sme->ssid, sme->ssid_len);
cmd->network.privacy = cpu_to_le32(sme->privacy);
cmd->network.networktype = cpu_to_le32(3); // Ndis802_11OFDM24
cmd->network.config.len = sizeof(cmd->network.config);
cmd->network.config.beaconperiod = cpu_to_le32(bss->beacon_interval);
cmd->network.config.dsconfig = cpu_to_le32(chan);
cmd->network.inframode = cpu_to_le32(1); // ???
cmd->network.ielen = cpu_to_le32(ie_len);
cmd->network.supportedrates[0] = 0x82;
cmd->network.supportedrates[1] = 0x84;
cmd->network.supportedrates[2] = 0x8b;
cmd->network.supportedrates[3] = 0x96;
cmd->network.supportedrates[4] = 0x0c;
cmd->network.supportedrates[5] = 0x12;
cmd->network.supportedrates[6] = 0x18;
cmd->network.supportedrates[7] = 0x24;
cmd->network.supportedrates[8] = 0x30;
cmd->network.supportedrates[9] = 0x48;
cmd->network.supportedrates[10] = 0x60;
cmd->network.supportedrates[11] = 0x6c;
// construct fixed IE
fixed = (struct ndis_802_11_fixed_ies *)cmd->network.ies;
fixed->beaconint = cpu_to_le16(bss->beacon_interval);
fixed->caps = cpu_to_le16(bss->capability);
// append provided IEs
ie_ptr = (u8 *)&fixed[1];
memcpy(ie_ptr, sme->ie, sme->ie_len);
ie_ptr += sme->ie_len;
// append constructed IEs
if (1) {
*(ie_ptr++) = WLAN_EID_HT_CAPABILITY;
*(ie_ptr++) = (u8)sizeof(struct ieee80211_ht_cap);
ht_cap = (struct ieee80211_ht_cap *)ie_ptr;
ht_cap->cap_info = RTLFMAC_HT_CAP;
ie_ptr += sizeof(struct ieee80211_ht_cap);
}
ret = rtlfmac_fw_cmd(priv, H2C_JOINBSS_CMD, cmd, sizeof(*cmd) + ie_len);
kfree(cmd);
if (!ret) {
priv->connecting = true;
}
done:
cfg80211_put_bss(priv->wiphy, bss);
return ret;
}
/* rtlfmac cfg80211 functions */
static int rtlfmac_cfg80211_scan(struct wiphy *wiphy,
struct cfg80211_scan_request *request)
{
struct rtlfmac_cfg80211_priv *priv = wiphy_to_cfg(wiphy);
netdev_dbg(priv->ndev, "%s: enter\n", __func__);
return rtlfmac_sitesurvey(priv, request);
}
static int rtlfmac_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev,
struct cfg80211_connect_params *sme)
{
struct rtlfmac_cfg80211_priv *priv = wiphy_to_cfg(wiphy);
netdev_dbg(ndev, "%s: enter\n", __func__);
return rtlfmac_connect(priv, ndev, sme);
}
static int rtlfmac_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *ndev,
u16 reason_code)
{
struct rtlfmac_cfg80211_priv *priv = wiphy_to_cfg(wiphy);
struct rtlfmac_disconnect_cmd cmd;
netdev_dbg(ndev, "%s: enter\n", __func__);
memset(&cmd, 0, sizeof(cmd));
return rtlfmac_fw_cmd(priv, H2C_DISCONNECT_CMD, &cmd, sizeof(cmd));
}
static int rtlfmac_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
u8 key_index, bool pairwise, const u8 *mac_addr, struct key_params *params)
{
u8 algo;
struct rtlfmac_cfg80211_priv *priv = wiphy_to_cfg(wiphy);
struct rtlfmac_setkey_cmd keycmd;
struct rtlfmac_setstakey_cmd stakeycmd;
netdev_dbg(ndev, "%s: enter idx=%hhu pair=%u mac=%pM\n", __func__, key_index, pairwise, mac_addr);
switch(params->cipher) {
case WLAN_CIPHER_SUITE_WEP40:
algo = IW_KEYALGO_WEP40;
break;
case WLAN_CIPHER_SUITE_CCMP:
algo = IW_KEYALGO_AES;
break;
default:
netdev_err(ndev, "%s: unknown cipher: 0x%08X\n", __func__, params->cipher);
return -ENOTSUPP;
}
if (pairwise && mac_addr) {
memcpy(&stakeycmd.addr, mac_addr, ETH_ALEN);
stakeycmd.algo = algo;
memcpy(&stakeycmd.key, params->key, params->key_len);
return rtlfmac_fw_cmd(priv, H2C_SETSTAKEY_CMD, &stakeycmd, sizeof(stakeycmd));
} else {
// assume 802.1x was successful
priv->ieee8021x_blocked = false;
keycmd.algo = algo;
keycmd.keyid = key_index;
keycmd.grpkey = (mac_addr == NULL ? 1 : 0);
memcpy(&keycmd.key, params->key, params->key_len);
return rtlfmac_fw_cmd(priv, H2C_SETKEY_CMD, &keycmd, sizeof(keycmd));
}
}
static int rtlfmac_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
u8 key_index, bool pairwise, const u8 *mac_addr)
{
netdev_dbg(ndev, "%s: enter idx=%hhu\n", __func__, key_index);
return 0;
}
static int rtlfmac_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *ndev,
u8 key_index, bool unicast, bool multicast)
{
struct rtlfmac_cfg80211_priv *priv = wiphy_to_cfg(wiphy);
netdev_dbg(ndev, "%s: enter idx=%hhu ucast=%u mcast=%u\n", __func__, key_index, unicast, multicast);
priv->key_id = key_index;
return 0;
}
/* cfg80211 data */
static struct cfg80211_ops rtlfmac_cfg80211_ops = {
.scan = rtlfmac_cfg80211_scan,
.connect = rtlfmac_cfg80211_connect,
.disconnect = rtlfmac_cfg80211_disconnect,
.add_key = rtlfmac_cfg80211_add_key,
.del_key = rtlfmac_cfg80211_del_key,
.set_default_key = rtlfmac_cfg80211_set_default_key,
};
static struct ieee80211_channel rtl_channeltable_2g[] = {
{.center_freq = 2412, .hw_value = 1,},
{.center_freq = 2417, .hw_value = 2,},
{.center_freq = 2422, .hw_value = 3,},
{.center_freq = 2427, .hw_value = 4,},
{.center_freq = 2432, .hw_value = 5,},
{.center_freq = 2437, .hw_value = 6,},
{.center_freq = 2442, .hw_value = 7,},
{.center_freq = 2447, .hw_value = 8,},
{.center_freq = 2452, .hw_value = 9,},
{.center_freq = 2457, .hw_value = 10,},
{.center_freq = 2462, .hw_value = 11,},
{.center_freq = 2467, .hw_value = 12,},
{.center_freq = 2472, .hw_value = 13,},
{.center_freq = 2484, .hw_value = 14,},
};
static struct ieee80211_rate rtl_ratetable_2g[] = {
{.bitrate = 10, .hw_value = 0x00,},
{.bitrate = 20, .hw_value = 0x01,},
{.bitrate = 55, .hw_value = 0x02,},
{.bitrate = 110, .hw_value = 0x03,},
{.bitrate = 60, .hw_value = 0x04,},
{.bitrate = 90, .hw_value = 0x05,},
{.bitrate = 120, .hw_value = 0x06,},
{.bitrate = 180, .hw_value = 0x07,},
{.bitrate = 240, .hw_value = 0x08,},
{.bitrate = 360, .hw_value = 0x09,},
{.bitrate = 480, .hw_value = 0x0a,},
{.bitrate = 540, .hw_value = 0x0b,},
};
static struct ieee80211_supported_band rtl_band_2ghz = {
.band = NL80211_BAND_2GHZ,
.channels = rtl_channeltable_2g,
.n_channels = ARRAY_SIZE(rtl_channeltable_2g),
.bitrates = rtl_ratetable_2g,
.n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
.ht_cap.cap = RTLFMAC_HT_CAP,
.ht_cap.ht_supported = true,
.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
.ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16,
.ht_cap.mcs.rx_mask[0] = 0xff,
.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED,
};
static const u32 rtlfmac_cipher_suites[] = {
WLAN_CIPHER_SUITE_WEP40,
WLAN_CIPHER_SUITE_WEP104,
WLAN_CIPHER_SUITE_TKIP,
WLAN_CIPHER_SUITE_CCMP,
WLAN_CIPHER_SUITE_AES_CMAC,
};
/* rtlfmac netdev functions */
static int rtlfmac_ndo_open(struct net_device *ndev)
{
netdev_dbg(ndev, "%s: enter\n", __func__);
netif_tx_start_all_queues(ndev);
return 0;
}
static int rtlfmac_ndo_stop(struct net_device *ndev)
{
netdev_dbg(ndev, "%s: enter\n", __func__);
if (ndev->ieee80211_ptr->current_bss) {
cfg80211_disconnected(ndev, 0, NULL, 0, true, GFP_KERNEL);
}
netif_tx_stop_all_queues(ndev);
netif_carrier_off(ndev);
return 0;
}
netdev_tx_t rtlfmac_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev)
{
int hdrlen;
struct ethhdr *eth = (struct ethhdr *)skb->data;
struct ieee80211_qos_hdr *hdr;
struct rtlfmac_cfg80211_priv *priv = wiphy_priv(ndev->ieee80211_ptr->wiphy);
struct rtlfmac_tx_desc *pdesc;
netdev_dbg(ndev, "%s: enter %pM->%pM proto=%04x\n", __func__, eth->h_source, eth->h_dest, be16_to_cpu(eth->h_proto));
if (priv->ieee8021x_blocked && be16_to_cpu(eth->h_proto) != ETH_P_PAE) {
netdev_dbg(ndev, "%s: drop non 802.1x packet\n", __func__);
return NETDEV_TX_OK;
}
if (ieee80211_data_from_8023(skb, ndev->perm_addr, priv->wdev->iftype, priv->bssid, 1)) {
netdev_err(ndev, "%s: failed to convert 802.3 to 802.11\n", __func__);
return -1;
}
hdr = (struct ieee80211_qos_hdr *)skb->data;
hdr->qos_ctrl = 0;
priv->seqno[RTL_TXQ_BE] = ieee80211_sn_inc(priv->seqno[RTL_TXQ_BE]);
hdr->seq_ctrl = cpu_to_le16(IEEE80211_SN_TO_SEQ(priv->seqno[RTL_TXQ_BE]));
// or in PROT if WEP/WPA is enabled
if (priv->cipher_pairwise && !priv->ieee8021x_blocked) {
// 802.1x is complete
hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
}
// need to emulate IEEE80211_KEY_FLAG_PUT_IV_SPACE
if (ieee80211_has_protected(hdr->frame_control)) {
hdrlen = ieee80211_hdrlen(hdr->frame_control);
skb_push(skb, priv->iv_len);
memmove(skb->data, skb->data + priv->iv_len, hdrlen);
switch (priv->cipher_pairwise) {
case WLAN_CIPHER_SUITE_WEP40:
skb->data[hdrlen + 0] = (priv->txiv >> 0) & 0xff;
skb->data[hdrlen + 1] = (priv->txiv >> 8) & 0xff;
skb->data[hdrlen + 2] = (priv->txiv >> 16) & 0xff;
skb->data[hdrlen + 3] = 0 ; // | (key_id << 6)
priv->txiv++;
priv->txiv &= 0xffffffULL;
break;
case WLAN_CIPHER_SUITE_CCMP:
skb->data[hdrlen + 0] = (priv->txiv >> 0) & 0xff;
skb->data[hdrlen + 1] = (priv->txiv >> 8) & 0xff;
skb->data[hdrlen + 2] = 0;
skb->data[hdrlen + 3] = 0x20 ; // | (key_id << 6) if b/m-cast
skb->data[hdrlen + 4] = (priv->txiv >> 16) & 0xff;
skb->data[hdrlen + 5] = (priv->txiv >> 24) & 0xff;
skb->data[hdrlen + 6] = (priv->txiv >> 32) & 0xff;
skb->data[hdrlen + 7] = (priv->txiv >> 40) & 0xff;
priv->txiv++;
priv->txiv &= 0xffffffffffffULL;
break;
}
}
if (skb_headroom(skb) < RTL_TX_HEADER_SIZE) {
netdev_err(ndev, "%s: not enough headroom\n", __func__);
return -1;
}
pdesc = (struct rtlfmac_tx_desc *)skb_push(skb, RTL_TX_HEADER_SIZE);
memset(pdesc, 0, RTL_TX_HEADER_SIZE);
pdesc->first_seg = pdesc->last_seg = 1;
pdesc->offset = RTL_TX_HEADER_SIZE;
pdesc->pkt_size = cpu_to_le16(skb->len - RTL_TX_HEADER_SIZE);
pdesc->macid = 5;
pdesc->queue_sel = 0x03; // BE
switch (priv->cipher_pairwise) {
case WLAN_CIPHER_SUITE_WEP40:
pdesc->sec_type = 0x01;
pdesc->key_id = priv->key_id;
break;
case WLAN_CIPHER_SUITE_CCMP:
pdesc->sec_type = 0x03;
break;
}
pdesc->own = 1;
skb->queue_mapping = RTL_TXQ_BE;
rtlfmac_write_skb(priv, skb);
return NETDEV_TX_OK;
}
/* net_device data */
static const struct net_device_ops rtlfmac_netdev_ops = {
.ndo_open = rtlfmac_ndo_open,
.ndo_stop = rtlfmac_ndo_stop,
.ndo_start_xmit = rtlfmac_ndo_start_xmit,
.ndo_set_mac_address = eth_mac_addr,
.ndo_validate_addr = eth_validate_addr,
};
/* chip init functions */
int rtlfmac_chip_init(struct rtlfmac_cfg80211_priv *priv)
{
int loop;
u8 val8;
u16 val16;
/* Switch to HW control */
val16 = rtlfmac_read_word(priv, REG_SYS_CLKR);
if (val16 & FWHW_SEL) {
val16 &= ~(FWHW_SEL | SWHW_SEL);
rtlfmac_write_word(priv, REG_SYS_CLKR, val16);
}
/* Reset CPU Core, Digital Core and MAC I/O */
val16 = rtlfmac_read_word(priv, REG_SYS_FUNC_EN);
val16 &= ~(FEN_MREGEN | FEN_DCORE | FEN_CPUEN);
rtlfmac_write_word(priv, REG_SYS_FUNC_EN, val16);
msleep(20);
rtlfmac_write_byte(priv, REG_SPS0_CTRL + 1, 0x53);