-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif_mtw.c
executable file
·4673 lines (3991 loc) · 116 KB
/
if_mtw.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
/*-
* Copyright (c) 2008-2010 Damien Bergamini <[email protected]>
* Copyright (c) 2013-2014 Kevin Lo
* Copyright (c) 2021 James Hastings
* Ported to FreeBSD by Jesper Schmitz Mouridsen [email protected]
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* MediaTek MT7601U 802.11b/g/n WLAN.
*/
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/eventhandler.h>
#include <sys/firmware.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/linker.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>
#include <net80211/ieee80211_ratectl.h>
#include <net80211/ieee80211_regdomain.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include "usbdevs.h"
#define USB_DEBUG_VAR mtw_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_msctest.h>
#include "if_mtwreg.h"
#include "if_mtwvar.h"
#define MTW_DEBUG
#ifdef MTW_DEBUG
int mtw_debug;
static SYSCTL_NODE(_hw_usb, OID_AUTO, mtw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"USB mtw");
SYSCTL_INT(_hw_usb_mtw, OID_AUTO, debug, CTLFLAG_RWTUN, &mtw_debug, 0,
"mtw debug level");
enum {
MTW_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
MTW_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */
MTW_DEBUG_RECV = 0x00000004, /* basic recv operation */
MTW_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */
MTW_DEBUG_STATE = 0x00000010, /* 802.11 state transitions */
MTW_DEBUG_RATE = 0x00000020, /* rate adaptation */
MTW_DEBUG_USB = 0x00000040, /* usb requests */
MTW_DEBUG_FIRMWARE = 0x00000080, /* firmware(9) loading debug */
MTW_DEBUG_BEACON = 0x00000100, /* beacon handling */
MTW_DEBUG_INTR = 0x00000200, /* ISR */
MTW_DEBUG_TEMP = 0x00000400, /* temperature calibration */
MTW_DEBUG_ROM = 0x00000800, /* various ROM info */
MTW_DEBUG_KEY = 0x00001000, /* crypto keys management */
MTW_DEBUG_TXPWR = 0x00002000, /* dump Tx power values */
MTW_DEBUG_RSSI = 0x00004000, /* dump RSSI lookups */
MTW_DEBUG_RESET = 0x00008000, /* initialization progress */
MTW_DEBUG_CALIB = 0x00010000, /* calibration progress */
MTW_DEBUG_CMD = 0x00020000, /* command queue */
MTW_DEBUG_ANY = 0xffffffff
};
#define MTW_DPRINTF(_sc, _m, ...) \
do { \
if (mtw_debug & (_m)) \
device_printf((_sc)->sc_dev, __VA_ARGS__); \
} while (0)
#else
#define MTW_DPRINTF(_sc, _m, ...) \
do { \
(void)_sc; \
} while (0)
#endif
#define IEEE80211_HAS_ADDR4(wh) IEEE80211_IS_DSTODS(wh)
/* NB: "11" is the maximum number of padding bytes needed for Tx */
#define MTW_MAX_TXSZ \
(sizeof(struct mtw_txd) + sizeof(struct mtw_txwi) + MCLBYTES + 11)
/*
* Because of LOR in mtw_key_delete(), use atomic instead.
* '& MTW_CMDQ_MASQ' is to loop cmdq[].
*/
#define MTW_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & MTW_CMDQ_MASQ)
static const STRUCT_USB_HOST_ID mtw_devs[] = {
#define MTW_DEV(v, p) \
{ \
USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) \
}
MTW_DEV(RALINK, MT7601U),
};
#undef MTW_DEV
static device_probe_t mtw_match;
static device_attach_t mtw_attach;
static device_detach_t mtw_detach;
static usb_callback_t mtw_bulk_rx_callback;
static usb_callback_t mtw_bulk_tx_callback0;
static usb_callback_t mtw_bulk_tx_callback1;
static usb_callback_t mtw_bulk_tx_callback2;
static usb_callback_t mtw_bulk_tx_callback3;
static usb_callback_t mtw_bulk_tx_callback4;
static usb_callback_t mtw_bulk_tx_callback5;
static usb_callback_t mtw_fw_callback;
static void mtw_autoinst(void *, struct usb_device *, struct usb_attach_arg *);
static int mtw_driver_loaded(struct module *, int, void *);
static void mtw_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error,
u_int index);
static struct ieee80211vap *mtw_vap_create(struct ieee80211com *,
const char[IFNAMSIZ], int, enum ieee80211_opmode, int,
const uint8_t[IEEE80211_ADDR_LEN], const uint8_t[IEEE80211_ADDR_LEN]);
static void mtw_vap_delete(struct ieee80211vap *);
static void mtw_cmdq_cb(void *, int);
static void mtw_setup_tx_list(struct mtw_softc *, struct mtw_endpoint_queue *);
static void mtw_unsetup_tx_list(struct mtw_softc *,
struct mtw_endpoint_queue *);
static void mtw_load_microcode(void *arg);
static usb_error_t mtw_do_request(struct mtw_softc *,
struct usb_device_request *, void *);
static int mtw_read(struct mtw_softc *, uint16_t, uint32_t *);
static int mtw_read_region_1(struct mtw_softc *, uint16_t, uint8_t *, int);
static int mtw_write_2(struct mtw_softc *, uint16_t, uint16_t);
static int mtw_write(struct mtw_softc *, uint16_t, uint32_t);
static int mtw_write_region_1(struct mtw_softc *, uint16_t, uint8_t *, int);
static int mtw_set_region_4(struct mtw_softc *, uint16_t, uint32_t, int);
static int mtw_efuse_read_2(struct mtw_softc *, uint16_t, uint16_t *);
static int mtw_bbp_read(struct mtw_softc *, uint8_t, uint8_t *);
static int mtw_bbp_write(struct mtw_softc *, uint8_t, uint8_t);
static int mtw_mcu_cmd(struct mtw_softc *sc, uint8_t cmd, void *buf, int len);
static void mtw_get_txpower(struct mtw_softc *);
static int mtw_read_eeprom(struct mtw_softc *);
static struct ieee80211_node *mtw_node_alloc(struct ieee80211vap *,
const uint8_t mac[IEEE80211_ADDR_LEN]);
static int mtw_media_change(if_t);
static int mtw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
static int mtw_wme_update(struct ieee80211com *);
static void mtw_key_set_cb(void *);
static int mtw_key_set(struct ieee80211vap *, struct ieee80211_key *);
static void mtw_key_delete_cb(void *);
static int mtw_key_delete(struct ieee80211vap *, struct ieee80211_key *);
static void mtw_ratectl_to(void *);
static void mtw_ratectl_cb(void *, int);
static void mtw_drain_fifo(void *);
static void mtw_iter_func(void *, struct ieee80211_node *);
static void mtw_newassoc_cb(void *);
static void mtw_newassoc(struct ieee80211_node *, int);
static int mtw_mcu_radio(struct mtw_softc *sc, int func, uint32_t val);
static void mtw_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
const struct ieee80211_rx_stats *, int, int);
static void mtw_rx_frame(struct mtw_softc *, struct mbuf *, uint32_t);
static void mtw_tx_free(struct mtw_endpoint_queue *pq, struct mtw_tx_data *,
int);
static void mtw_set_tx_desc(struct mtw_softc *, struct mtw_tx_data *);
static int mtw_tx(struct mtw_softc *, struct mbuf *, struct ieee80211_node *);
static int mtw_tx_mgt(struct mtw_softc *, struct mbuf *,
struct ieee80211_node *);
static int mtw_sendprot(struct mtw_softc *, const struct mbuf *,
struct ieee80211_node *, int, int);
static int mtw_tx_param(struct mtw_softc *, struct mbuf *,
struct ieee80211_node *, const struct ieee80211_bpf_params *);
static int mtw_raw_xmit(struct ieee80211_node *, struct mbuf *,
const struct ieee80211_bpf_params *);
static int mtw_transmit(struct ieee80211com *, struct mbuf *);
static void mtw_start(struct mtw_softc *);
static void mtw_parent(struct ieee80211com *);
static void mtw_select_chan_group(struct mtw_softc *, int);
static int mtw_set_chan(struct mtw_softc *, struct ieee80211_channel *);
static void mtw_set_channel(struct ieee80211com *);
static void mtw_getradiocaps(struct ieee80211com *, int, int *,
struct ieee80211_channel[]);
static void mtw_scan_start(struct ieee80211com *);
static void mtw_scan_end(struct ieee80211com *);
static void mtw_update_beacon(struct ieee80211vap *, int);
static void mtw_update_beacon_cb(void *);
static void mtw_updateprot(struct ieee80211com *);
static void mtw_updateprot_cb(void *);
static void mtw_usb_timeout_cb(void *);
static int mtw_reset(struct mtw_softc *sc);
static void mtw_enable_tsf_sync(struct mtw_softc *);
static void mtw_enable_mrr(struct mtw_softc *);
static void mtw_set_txpreamble(struct mtw_softc *);
static void mtw_set_basicrates(struct mtw_softc *);
static void mtw_set_leds(struct mtw_softc *, uint16_t);
static void mtw_set_bssid(struct mtw_softc *, const uint8_t *);
static void mtw_set_macaddr(struct mtw_softc *, const uint8_t *);
static void mtw_updateslot(struct ieee80211com *);
static void mtw_updateslot_cb(void *);
static void mtw_update_mcast(struct ieee80211com *);
static int8_t mtw_rssi2dbm(struct mtw_softc *, uint8_t, uint8_t);
static void mtw_update_promisc_locked(struct mtw_softc *);
static void mtw_update_promisc(struct ieee80211com *);
static int mtw_txrx_enable(struct mtw_softc *);
static void mtw_init_locked(struct mtw_softc *);
static void mtw_stop(void *);
static void mtw_delay(struct mtw_softc *, u_int);
static void mtw_update_chw(struct ieee80211com *ic);
static int mtw_ampdu_enable(struct ieee80211_node *ni,
struct ieee80211_tx_ampdu *tap);
static eventhandler_tag mtw_etag;
static const struct {
uint8_t reg;
uint8_t val;
} mt7601_rf_bank0[] = { MT7601_BANK0_RF },
mt7601_rf_bank4[] = { MT7601_BANK4_RF },
mt7601_rf_bank5[] = { MT7601_BANK5_RF };
static const struct {
uint32_t reg;
uint32_t val;
} mt7601_def_mac[] = { MT7601_DEF_MAC };
static const struct {
uint8_t reg;
uint8_t val;
} mt7601_def_bbp[] = { MT7601_DEF_BBP };
static const struct {
u_int chan;
uint8_t r17, r18, r19, r20;
} mt7601_rf_chan[] = { MT7601_RF_CHAN };
static const struct usb_config mtw_config[MTW_N_XFER] = {
[MTW_BULK_RX] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_IN,
.bufsize = MTW_MAX_RXSZ,
.flags = {.pipe_bof = 1,
.short_xfer_ok = 1,},
.callback = mtw_bulk_rx_callback,
},
[MTW_BULK_TX_BE] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 0,},
.callback = mtw_bulk_tx_callback0,
.timeout = 5000, /* ms */
},
[MTW_BULK_TX_BK] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1,},
.callback = mtw_bulk_tx_callback1,
.timeout = 5000, /* ms */
},
[MTW_BULK_TX_VI] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1,},
.callback = mtw_bulk_tx_callback2,
.timeout = 5000, /* ms */
},
[MTW_BULK_TX_VO] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1,},
.callback = mtw_bulk_tx_callback3,
.timeout = 5000, /* ms */
},
[MTW_BULK_TX_HCCA] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1, .no_pipe_ok = 1,},
.callback = mtw_bulk_tx_callback4,
.timeout = 5000, /* ms */
},
[MTW_BULK_TX_PRIO] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1, .no_pipe_ok = 1,},
.callback = mtw_bulk_tx_callback5,
.timeout = 5000, /* ms */
},
[MTW_BULK_FW_CMD] = {
.type = UE_BULK,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = 0x2c44,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1, .no_pipe_ok = 1,},
.callback = mtw_fw_callback,
},
[MTW_BULK_RAW_TX] = {
.type = UE_BULK,
.ep_index = 0,
.endpoint = UE_ADDR_ANY,
.direction = UE_DIR_OUT,
.bufsize = MTW_MAX_TXSZ,
.flags = {.pipe_bof = 1,
.force_short_xfer = 1, .no_pipe_ok = 1,},
.callback = mtw_bulk_tx_callback0,
.timeout = 5000, /* ms */
},
};
static uint8_t mtw_wme_ac_xfer_map[4] = {
[WME_AC_BE] = MTW_BULK_TX_BE,
[WME_AC_BK] = MTW_BULK_TX_BK,
[WME_AC_VI] = MTW_BULK_TX_VI,
[WME_AC_VO] = MTW_BULK_TX_VO,
};
static void
mtw_autoinst(void *arg, struct usb_device *udev, struct usb_attach_arg *uaa)
{
struct usb_interface *iface;
struct usb_interface_descriptor *id;
if (uaa->dev_state != UAA_DEV_READY)
return;
iface = usbd_get_iface(udev, 0);
if (iface == NULL)
return;
id = iface->idesc;
if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
return;
if (usbd_lookup_id_by_uaa(mtw_devs, sizeof(mtw_devs), uaa))
return;
if (usb_msc_eject(udev, 0, MSC_EJECT_STOPUNIT) == 0)
uaa->dev_state = UAA_DEV_EJECTING;
}
static int
mtw_driver_loaded(struct module *mod, int what, void *arg)
{
switch (what) {
case MOD_LOAD:
mtw_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
mtw_autoinst, NULL, EVENTHANDLER_PRI_ANY);
break;
case MOD_UNLOAD:
EVENTHANDLER_DEREGISTER(usb_dev_configured, mtw_etag);
break;
default:
return (EOPNOTSUPP);
}
return (0);
}
static const char *
mtw_get_rf(int rev)
{
switch (rev) {
case MT7601_RF_7601:
return ("MT7601");
case MT7610_RF_7610:
return ("MT7610");
case MT7612_RF_7612:
return ("MT7612");
}
return ("unknown");
}
static int
mtw_wlan_enable(struct mtw_softc *sc, int enable)
{
uint32_t tmp;
int error = 0;
if (enable) {
mtw_read(sc, MTW_WLAN_CTRL, &tmp);
if (sc->asic_ver == 0x7612)
tmp &= ~0xfffff000;
tmp &= ~MTW_WLAN_CLK_EN;
tmp |= MTW_WLAN_EN;
mtw_write(sc, MTW_WLAN_CTRL, tmp);
mtw_delay(sc, 2);
tmp |= MTW_WLAN_CLK_EN;
if (sc->asic_ver == 0x7612) {
tmp |= (MTW_WLAN_RESET | MTW_WLAN_RESET_RF);
}
mtw_write(sc, MTW_WLAN_CTRL, tmp);
mtw_delay(sc, 2);
mtw_read(sc, MTW_OSC_CTRL, &tmp);
tmp |= MTW_OSC_EN;
mtw_write(sc, MTW_OSC_CTRL, tmp);
tmp |= MTW_OSC_CAL_REQ;
mtw_write(sc, MTW_OSC_CTRL, tmp);
} else {
mtw_read(sc, MTW_WLAN_CTRL, &tmp);
tmp &= ~(MTW_WLAN_CLK_EN | MTW_WLAN_EN);
mtw_write(sc, MTW_WLAN_CTRL, tmp);
mtw_read(sc, MTW_OSC_CTRL, &tmp);
tmp &= ~MTW_OSC_EN;
mtw_write(sc, MTW_OSC_CTRL, tmp);
}
return (error);
}
static int
mtw_read_cfg(struct mtw_softc *sc, uint16_t reg, uint32_t *val)
{
usb_device_request_t req;
uint32_t tmp;
uint16_t actlen;
int error;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = MTW_READ_CFG;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 4);
error = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, &tmp, 0,
&actlen, 1000);
if (error == 0)
*val = le32toh(tmp);
else
*val = 0xffffffff;
return (error);
}
static int
mtw_match(device_t self)
{
struct usb_attach_arg *uaa = device_get_ivars(self);
if (uaa->usb_mode != USB_MODE_HOST)
return (ENXIO);
if (uaa->info.bConfigIndex != 0)
return (ENXIO);
if (uaa->info.bIfaceIndex != 0)
return (ENXIO);
return (usbd_lookup_id_by_uaa(mtw_devs, sizeof(mtw_devs), uaa));
}
static int
mtw_attach(device_t self)
{
struct mtw_softc *sc = device_get_softc(self);
struct usb_attach_arg *uaa = device_get_ivars(self);
struct ieee80211com *ic = &sc->sc_ic;
uint32_t ver;
int i, ret;
// uint32_t tmp;
uint8_t iface_index;
int ntries, error;
device_set_usb_desc(self);
sc->sc_udev = uaa->device;
sc->sc_dev = self;
sc->sc_sent = 0;
mtx_init(&sc->sc_mtx,
device_get_nameunit(sc->sc_dev),
MTX_NETWORK_LOCK, MTX_DEF);
iface_index = 0;
error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
mtw_config, MTW_N_XFER, sc, &sc->sc_mtx);
if (error) {
device_printf(sc->sc_dev,
"could not allocate USB transfers, "
"err=%s\n",
usbd_errstr(error));
goto detach;
}
for (i = 0; i < 4; i++) {
sc->txd_fw[i] = (struct mtw_txd_fw *)
malloc(sizeof(struct mtw_txd_fw),
M_USBDEV, M_NOWAIT | M_ZERO);
}
MTW_LOCK(sc);
sc->sc_idx = 0;
mbufq_init(&sc->sc_snd, ifqmaxlen);
/*enable WLAN core */
if ((error = mtw_wlan_enable(sc, 1)) != 0) {
device_printf(sc->sc_dev, "could not enable WLAN core\n");
return (ENXIO);
}
/* wait for the chip to settle */
DELAY(100);
for (ntries = 0; ntries < 100; ntries++) {
if (mtw_read(sc, MTW_ASIC_VER, &ver) != 0) {
goto detach;
}
if (ver != 0 && ver != 0xffffffff)
break;
DELAY(10);
}
if (ntries == 100) {
device_printf(sc->sc_dev,
"timeout waiting for NIC to initialize\n");
goto detach;
}
sc->asic_ver = ver >> 16;
sc->asic_rev = ver & 0xffff;
DELAY(100);
if (sc->asic_ver != 0x7601) {
device_printf(sc->sc_dev,
"Your revision 0x04%x is not supported yet\n",
sc->asic_rev);
goto detach;
}
mtw_load_microcode(sc);
ret = msleep(&sc->fwloading, &sc->sc_mtx, 0, "fwload", 3 * hz);
if (ret == EWOULDBLOCK || sc->fwloading != 1) {
device_printf(sc->sc_dev,
"timeout waiting for MCU to initialize\n");
goto detach;
}
sc->sc_srom_read = mtw_efuse_read_2;
/* retrieve RF rev. no and various other things from EEPROM */
mtw_read_eeprom(sc);
device_printf(sc->sc_dev,
"MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
sc->asic_ver, sc->mac_rev, mtw_get_rf(sc->rf_rev), sc->ntxchains,
sc->nrxchains, ether_sprintf(ic->ic_macaddr));
DELAY(100);
//mtw_set_leds(sc,5);
// mtw_mcu_radio(sc,0x31,0);
MTW_UNLOCK(sc);
ic->ic_softc = sc;
ic->ic_name = device_get_nameunit(self);
ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
ic->ic_caps = IEEE80211_C_STA | /* station mode supported */
IEEE80211_C_MONITOR | /* monitor mode supported */
IEEE80211_C_IBSS |
IEEE80211_C_HOSTAP |
IEEE80211_C_WDS | /* 4-address traffic works */
IEEE80211_C_MBSS |
IEEE80211_C_SHPREAMBLE | /* short preamble supported */
IEEE80211_C_SHSLOT | /* short slot time supported */
IEEE80211_C_WME | /* WME */
IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */
device_printf(sc->sc_dev, "[HT] Enabling 802.11n\n");
ic->ic_htcaps = IEEE80211_HTC_HT
| IEEE80211_HTC_AMPDU
| IEEE80211_HTC_AMSDU
| IEEE80211_HTCAP_MAXAMSDU_3839
| IEEE80211_HTCAP_SMPS_OFF;
ic->ic_rxstream = sc->nrxchains;
ic->ic_txstream = sc->ntxchains;
ic->ic_cryptocaps = IEEE80211_CRYPTO_WEP | IEEE80211_CRYPTO_AES_CCM |
IEEE80211_CRYPTO_AES_OCB | IEEE80211_CRYPTO_TKIP |
IEEE80211_CRYPTO_TKIPMIC;
ic->ic_flags |= IEEE80211_F_DATAPAD;
ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
mtw_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
ic->ic_channels);
ieee80211_ifattach(ic);
ic->ic_scan_start = mtw_scan_start;
ic->ic_scan_end = mtw_scan_end;
ic->ic_set_channel = mtw_set_channel;
ic->ic_getradiocaps = mtw_getradiocaps;
ic->ic_node_alloc = mtw_node_alloc;
ic->ic_newassoc = mtw_newassoc;
ic->ic_update_mcast = mtw_update_mcast;
ic->ic_updateslot = mtw_updateslot;
ic->ic_wme.wme_update = mtw_wme_update;
ic->ic_raw_xmit = mtw_raw_xmit;
ic->ic_update_promisc = mtw_update_promisc;
ic->ic_vap_create = mtw_vap_create;
ic->ic_vap_delete = mtw_vap_delete;
ic->ic_transmit = mtw_transmit;
ic->ic_parent = mtw_parent;
ic->ic_update_chw = mtw_update_chw;
ic->ic_ampdu_enable = mtw_ampdu_enable;
ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
sizeof(sc->sc_txtap), MTW_TX_RADIOTAP_PRESENT,
&sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
MTW_RX_RADIOTAP_PRESENT);
TASK_INIT(&sc->cmdq_task, 0, mtw_cmdq_cb, sc);
TASK_INIT(&sc->ratectl_task, 0, mtw_ratectl_cb, sc);
usb_callout_init_mtx(&sc->ratectl_ch, &sc->sc_mtx, 0);
if (bootverbose)
ieee80211_announce(ic);
return (0);
detach:
MTW_UNLOCK(sc);
mtw_detach(self);
return (ENXIO);
}
static void
mtw_drain_mbufq(struct mtw_softc *sc)
{
struct mbuf *m;
struct ieee80211_node *ni;
MTW_LOCK_ASSERT(sc, MA_OWNED);
while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
m->m_pkthdr.rcvif = NULL;
ieee80211_free_node(ni);
m_freem(m);
}
}
static int
mtw_detach(device_t self)
{
struct mtw_softc *sc = device_get_softc(self);
struct ieee80211com *ic = &sc->sc_ic;
int i;
MTW_LOCK(sc);
mtw_reset(sc);
DELAY(10000);
sc->sc_detached = 1;
MTW_UNLOCK(sc);
/* stop all USB transfers */
for (i = 0; i < MTW_N_XFER; i++)
usbd_transfer_drain(sc->sc_xfer[i]);
MTW_LOCK(sc);
sc->ratectl_run = MTW_RATECTL_OFF;
sc->cmdq_run = sc->cmdq_key_set = MTW_CMDQ_ABORT;
/* free TX list, if any */
if(ic->ic_nrunning >0)
for (i = 0; i < MTW_EP_QUEUES; i++)
mtw_unsetup_tx_list(sc, &sc->sc_epq[i]);
/* Free TX queue */
mtw_drain_mbufq(sc);
MTW_UNLOCK(sc);
if (sc->sc_ic.ic_softc == sc) {
/* drain tasks */
usb_callout_drain(&sc->ratectl_ch);
ieee80211_draintask(ic, &sc->cmdq_task);
ieee80211_draintask(ic, &sc->ratectl_task);
ieee80211_ifdetach(ic);
}
for (i = 0; i < 7; i++) {
free(sc->txd_fw[i], M_USBDEV);
}
mtx_destroy(&sc->sc_mtx);
return (0);
}
static struct ieee80211vap *
mtw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
enum ieee80211_opmode opmode, int flags,
const uint8_t bssid[IEEE80211_ADDR_LEN],
const uint8_t mac[IEEE80211_ADDR_LEN])
{
struct mtw_softc *sc = ic->ic_softc;
struct mtw_vap *rvp;
struct ieee80211vap *vap;
int i;
if (sc->rvp_cnt >= MTW_VAP_MAX) {
device_printf(sc->sc_dev, "number of VAPs maxed out\n");
return (NULL);
}
switch (opmode) {
case IEEE80211_M_STA:
/* enable s/w bmiss handling for sta mode */
flags |= IEEE80211_CLONE_NOBEACONS;
/* fall though */
case IEEE80211_M_IBSS:
case IEEE80211_M_MONITOR:
case IEEE80211_M_HOSTAP:
case IEEE80211_M_MBSS:
/* other than WDS vaps, only one at a time */
if (!TAILQ_EMPTY(&ic->ic_vaps))
return (NULL);
break;
case IEEE80211_M_WDS:
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (vap->iv_opmode != IEEE80211_M_HOSTAP)
continue;
/* WDS vap's always share the local mac address. */
flags &= ~IEEE80211_CLONE_BSSID;
break;
}
if (vap == NULL) {
device_printf(sc->sc_dev,
"wds only supported in ap mode\n");
return (NULL);
}
break;
default:
device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
return (NULL);
}
rvp = malloc(sizeof(struct mtw_vap), M_80211_VAP, M_WAITOK | M_ZERO);
vap = &rvp->vap;
if (ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid) !=
0) {
/* out of memory */
free(rvp, M_80211_VAP);
return (NULL);
}
vap->iv_update_beacon = mtw_update_beacon;
vap->iv_max_aid = MTW_WCID_MAX;
/*
* The linux rt2800 driver limits 1 stream devices to a 32KB
* RX AMPDU.
*/
if (ic->ic_rxstream > 1)
vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
else
vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_2; /* 2uS */
/*
* To delete the right key from h/w, we need wcid.
* Luckily, there is unused space in ieee80211_key{}, wk_pad,
* and matching wcid will be written into there. So, cast
* some spells to remove 'const' from ieee80211_key{}
*/
vap->iv_key_delete = (void *)mtw_key_delete;
vap->iv_key_set = (void *)mtw_key_set;
// override state transition machine
rvp->newstate = vap->iv_newstate;
vap->iv_newstate = mtw_newstate;
if (opmode == IEEE80211_M_IBSS) {
rvp->recv_mgmt = vap->iv_recv_mgmt;
vap->iv_recv_mgmt = mtw_recv_mgmt;
}
ieee80211_ratectl_init(vap);
ieee80211_ratectl_setinterval(vap, 1000); // 1 second
/* complete setup */
ieee80211_vap_attach(vap, mtw_media_change, ieee80211_media_status,
mac);
/* make sure id is always unique */
for (i = 0; i < MTW_VAP_MAX; i++) {
if ((sc->rvp_bmap & 1 << i) == 0) {
sc->rvp_bmap |= 1 << i;
rvp->rvp_id = i;
break;
}
}
if (sc->rvp_cnt++ == 0)
ic->ic_opmode = opmode;
if (opmode == IEEE80211_M_HOSTAP)
sc->cmdq_run = MTW_CMDQ_GO;
MTW_DPRINTF(sc, MTW_DEBUG_STATE, "rvp_id=%d bmap=%x rvp_cnt=%d\n",
rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
return (vap);
}
static void
mtw_vap_delete(struct ieee80211vap *vap)
{
struct mtw_vap *rvp = MTW_VAP(vap);
struct ieee80211com *ic;
struct mtw_softc *sc;
uint8_t rvp_id;
if (vap == NULL)
return;
ic = vap->iv_ic;
sc = ic->ic_softc;
MTW_LOCK(sc);
m_freem(rvp->beacon_mbuf);
rvp->beacon_mbuf = NULL;
rvp_id = rvp->rvp_id;
sc->ratectl_run &= ~(1 << rvp_id);
sc->rvp_bmap &= ~(1 << rvp_id);
mtw_set_region_4(sc, MTW_SKEY(rvp_id, 0), 0, 256);
mtw_set_region_4(sc, (0x7800 + (rvp_id) * 512), 0, 512);
--sc->rvp_cnt;
MTW_DPRINTF(sc, MTW_DEBUG_STATE,
"vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n", vap, rvp_id, sc->rvp_bmap,
sc->rvp_cnt);
MTW_UNLOCK(sc);
ieee80211_ratectl_deinit(vap);
ieee80211_vap_detach(vap);
free(rvp, M_80211_VAP);
}
/*
* There are numbers of functions need to be called in context thread.
* Rather than creating taskqueue event for each of those functions,
* here is all-for-one taskqueue callback function. This function
* guarantees deferred functions are executed in the same order they
* were enqueued.
* '& MTW_CMDQ_MASQ' is to loop cmdq[].
*/
static void
mtw_cmdq_cb(void *arg, int pending)
{
struct mtw_softc *sc = arg;
uint8_t i;
/* call cmdq[].func locked */
MTW_LOCK(sc);
for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
i = sc->cmdq_exec, pending--) {
MTW_DPRINTF(sc, MTW_DEBUG_CMD, "cmdq_exec=%d pending=%d\n", i,
pending);
if (sc->cmdq_run == MTW_CMDQ_GO) {
/*
* If arg0 is NULL, callback func needs more
* than one arg. So, pass ptr to cmdq struct.
*/
if (sc->cmdq[i].arg0)
sc->cmdq[i].func(sc->cmdq[i].arg0);
else
sc->cmdq[i].func(&sc->cmdq[i]);
}
sc->cmdq[i].arg0 = NULL;
sc->cmdq[i].func = NULL;
sc->cmdq_exec++;
sc->cmdq_exec &= MTW_CMDQ_MASQ;
}
MTW_UNLOCK(sc);
}
static void
mtw_setup_tx_list(struct mtw_softc *sc, struct mtw_endpoint_queue *pq)
{
struct mtw_tx_data *data;
memset(pq, 0, sizeof(*pq));
STAILQ_INIT(&pq->tx_qh);
STAILQ_INIT(&pq->tx_fh);
for (data = &pq->tx_data[0]; data < &pq->tx_data[MTW_TX_RING_COUNT];
data++) {
data->sc = sc;
STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
}
pq->tx_nfree = MTW_TX_RING_COUNT;
}
static void
mtw_unsetup_tx_list(struct mtw_softc *sc, struct mtw_endpoint_queue *pq)
{
struct mtw_tx_data *data;
/* make sure any subsequent use of the queues will fail */
pq->tx_nfree = 0;
STAILQ_INIT(&pq->tx_fh);
STAILQ_INIT(&pq->tx_qh);
/* free up all node references and mbufs */
for (data = &pq->tx_data[0]; data < &pq->tx_data[MTW_TX_RING_COUNT];
data++) {
if (data->m != NULL) {
m_freem(data->m);
data->m = NULL;
}
if (data->ni != NULL) {
ieee80211_free_node(data->ni);
data->ni = NULL;
}
}
}
static int
mtw_write_ivb(struct mtw_softc *sc, void *buf, uint16_t len)
{
usb_device_request_t req;
uint16_t actlen;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = MTW_RESET;
USETW(req.wValue, 0x12);
USETW(req.wIndex, 0);
USETW(req.wLength, len);
int error = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, buf,
0, &actlen, 1000);
return (error);
}
static int
mtw_write_cfg(struct mtw_softc *sc, uint16_t reg, uint32_t val)
{
usb_device_request_t req;
int error;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = MTW_WRITE_CFG;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 4);
val = htole32(val);
error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &val);
return (error);
}
static int
mtw_usb_dma_write(struct mtw_softc *sc, uint32_t val)
{