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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
|
/* $OpenBSD: an.c,v 1.49 2006/03/25 22:41:42 djm Exp $ */
/* $NetBSD: an.c,v 1.34 2005/06/20 02:49:18 atatat Exp $ */
/*
* Copyright (c) 1997, 1998, 1999
* Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Bill Paul.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $
*/
/*
* Copyright (c) 2004, 2005 David Young. All rights reserved.
* Copyright (c) 2004, 2005 OJC Technologies. All rights reserved.
* Copyright (c) 2004, 2005 Dayton Data Center Services, LLC. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL David Young AND CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
*
* Written by Bill Paul <wpaul@ctr.columbia.edu>
* Electrical Engineering Department
* Columbia University, New York City
*/
/*
* Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego
* IETF meeting.
*/
#include <sys/cdefs.h>
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/ucred.h>
#include <sys/socket.h>
#include <sys/timeout.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/endian.h>
#include <sys/tree.h>
#include <machine/bus.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_llc.h>
#include <net/if_media.h>
#include <net/if_types.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#endif
#include <net80211/ieee80211_radiotap.h>
#include <net80211/ieee80211_var.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <dev/ic/anreg.h>
#include <dev/ic/anvar.h>
struct cfdriver an_cd = {
NULL, "an", DV_IFNET
};
int an_reset(struct an_softc *);
void an_wait(struct an_softc *);
int an_init(struct ifnet *);
void an_stop(struct ifnet *, int);
void an_start(struct ifnet *);
void an_watchdog(struct ifnet *);
int an_ioctl(struct ifnet *, u_long, caddr_t);
int an_media_change(struct ifnet *);
void an_media_status(struct ifnet *, struct ifmediareq *);
int an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *);
int an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *);
int an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *);
int an_write_wepkey(struct an_softc *, int, struct an_wepkey *,
int);
void an_rxeof(struct an_softc *);
void an_txeof(struct an_softc *, int);
void an_linkstat_intr(struct an_softc *);
int an_cmd(struct an_softc *, int, int);
int an_seek_bap(struct an_softc *, int, int);
int an_read_bap(struct an_softc *, int, int, void *, int);
int an_write_bap(struct an_softc *, int, int, void *, int);
int an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int);
int an_read_rid(struct an_softc *, int, void *, int *);
int an_write_rid(struct an_softc *, int, void *, int);
int an_alloc_nicmem(struct an_softc *, int, int *);
int an_newstate(struct ieee80211com *, enum ieee80211_state, int);
#ifdef AN_DEBUG
int an_debug = 0;
#define DPRINTF(X) if (an_debug) printf X
#define DPRINTF2(X) if (an_debug > 1) printf X
#else
#define DPRINTF(X)
#define DPRINTF2(X)
#endif
static __inline void
an_swap16(u_int16_t *p, int cnt)
{
for (; cnt--; p++)
*p = swap16(*p);
}
int
an_attach(struct an_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
int i;
struct an_rid_wepkey *akey;
int buflen, kid, rid;
int chan, chan_min, chan_max;
sc->sc_invalid = 0;
/* disable interrupts */
CSR_WRITE_2(sc, AN_INT_EN, 0);
CSR_WRITE_2(sc, AN_EVENT_ACK, 0xffff);
// an_wait(sc);
if (an_reset(sc) != 0) {
sc->sc_invalid = 1;
return 1;
}
/* Load factory config */
if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) {
printf("%s: failed to load config data\n",
sc->sc_dev.dv_xname);
return (EIO);
}
/* Read the current configuration */
buflen = sizeof(sc->sc_config);
if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) {
printf("%s: read config failed\n", sc->sc_dev.dv_xname);
return(EIO);
}
/* Read the card capabilities */
buflen = sizeof(sc->sc_caps);
if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) {
printf("%s: read caps failed\n", sc->sc_dev.dv_xname);
return(EIO);
}
/* Read WEP settings from persistent memory */
akey = &sc->sc_buf.sc_wepkey;
buflen = sizeof(struct an_rid_wepkey);
rid = AN_RID_WEP_VOLATILE; /* first persistent key */
while (an_read_rid(sc, rid, akey, &buflen) == 0) {
kid = letoh16(akey->an_key_index);
DPRINTF(("an_attach: wep rid=0x%x len=%d(%d) index=0x%04x "
"mac[0]=%02x keylen=%d\n",
rid, buflen, sizeof(*akey), kid,
akey->an_mac_addr[0], letoh16(akey->an_key_len)));
if (kid == 0xffff) {
sc->sc_tx_perskey = akey->an_mac_addr[0];
sc->sc_tx_key = -1;
break;
}
if (kid >= IEEE80211_WEP_NKID)
break;
sc->sc_perskeylen[kid] = letoh16(akey->an_key_len);
sc->sc_wepkeys[kid].an_wep_keylen = -1;
rid = AN_RID_WEP_PERSISTENT; /* for next key */
buflen = sizeof(struct an_rid_wepkey);
}
IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
printf("%s: Firmware %x.%02x.%02x, Radio: ", ifp->if_xname,
htole16(sc->sc_caps.an_fwrev) >> 8,
htole16(sc->sc_caps.an_fwrev) & 0xff,
sc->sc_caps.an_fwsubrev);
if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_FH)
printf("802.11 FH");
else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_DS)
printf("802.11 DS");
else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_LM2000_DS)
printf("LM2000 DS");
else
printf("unknown (%x)", sc->sc_config.an_radiotype);
printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = an_ioctl;
ifp->if_start = an_start;
ifp->if_init = an_init;
ifp->if_watchdog = an_watchdog;
IFQ_SET_READY(&ifp->if_snd);
ic->ic_phytype = IEEE80211_T_DS;
ic->ic_opmode = IEEE80211_M_STA;
ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_IBSS |
IEEE80211_C_MONITOR;
ic->ic_state = IEEE80211_S_INIT;
IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
switch (letoh16(sc->sc_caps.an_regdomain)) {
default:
case AN_REGDOMAIN_USA:
case AN_REGDOMAIN_CANADA:
chan_min = 1; chan_max = 11; break;
case AN_REGDOMAIN_EUROPE:
case AN_REGDOMAIN_AUSTRALIA:
chan_min = 1; chan_max = 13; break;
case AN_REGDOMAIN_JAPAN:
chan_min = 14; chan_max = 14; break;
case AN_REGDOMAIN_SPAIN:
chan_min = 10; chan_max = 11; break;
case AN_REGDOMAIN_FRANCE:
chan_min = 10; chan_max = 13; break;
case AN_REGDOMAIN_JAPANWIDE:
chan_min = 1; chan_max = 14; break;
}
for (chan = chan_min; chan <= chan_max; chan++) {
ic->ic_channels[chan].ic_freq =
ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
}
ic->ic_ibss_chan = &ic->ic_channels[chan_min];
/* Find supported rate */
for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) {
if (sc->sc_caps.an_rates[i] == 0)
continue;
ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] =
sc->sc_caps.an_rates[i];
}
/*
* Call MI attach routine.
*/
if_attach(ifp);
ieee80211_ifattach(ifp);
sc->sc_newstate = ic->ic_newstate;
ic->ic_newstate = an_newstate;
ieee80211_media_init(ifp, an_media_change, an_media_status);
#if NBPFILTER > 0
bzero(&sc->sc_rxtapu, sizeof(sc->sc_rxtapu));
sc->sc_rxtap.ar_ihdr.it_len = sizeof(sc->sc_rxtapu);
sc->sc_rxtap.ar_ihdr.it_present = AN_RX_RADIOTAP_PRESENT;
bzero(&sc->sc_txtapu, sizeof(sc->sc_txtapu));
sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu);
sc->sc_txtap.at_ihdr.it_present = AN_TX_RADIOTAP_PRESENT;
bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
sizeof(struct ieee80211_frame) + 64);
#endif
sc->sc_sdhook = shutdownhook_establish(an_shutdown, sc);
sc->sc_attached = 1;
return(0);
}
void
an_rxeof(struct an_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ieee80211_frame *wh;
struct ieee80211_node *ni;
struct an_rxframe frmhdr;
struct mbuf *m;
u_int16_t status;
int fid, gaplen, len, off;
uint8_t *gap;
fid = CSR_READ_2(sc, AN_RX_FID);
/* First read in the frame header */
if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
ifp->if_ierrors++;
DPRINTF(("an_rxeof: read fid %x failed\n", fid));
return;
}
status = letoh16(frmhdr.an_rx_status);
if ((status & AN_STAT_ERRSTAT) != 0 &&
ic->ic_opmode != IEEE80211_M_MONITOR) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
ifp->if_ierrors++;
DPRINTF(("an_rxeof: fid %x status %x\n", fid, status));
return;
}
/* the payload length field includes a 16-bit "mystery field" */
len = letoh16(frmhdr.an_rx_payload_len) - sizeof(uint16_t);
off = ALIGN(sizeof(struct ieee80211_frame));
if (off + len > MCLBYTES) {
if (ic->ic_opmode != IEEE80211_M_MONITOR) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
ifp->if_ierrors++;
DPRINTF(("an_rxeof: oversized packet %d\n", len));
return;
}
len = 0;
}
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
ifp->if_ierrors++;
DPRINTF(("an_rxeof: MGET failed\n"));
return;
}
if (off + len + AN_GAPLEN_MAX > MHLEN) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
m_freem(m);
ifp->if_ierrors++;
DPRINTF(("an_rxeof: MCLGET failed\n"));
return;
}
}
m->m_data += off - sizeof(struct ieee80211_frame);
if (ic->ic_opmode != IEEE80211_M_MONITOR) {
gaplen = letoh16(frmhdr.an_gaplen);
if (gaplen > AN_GAPLEN_MAX) {
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
m_freem(m);
ifp->if_ierrors++;
DPRINTF(("%s: gap too long\n", __func__));
return;
}
/*
* We don't need the 16-bit mystery field (payload length?),
* so read it into the region reserved for the 802.11 header.
*
* When Cisco Aironet 350 cards w/ firmware version 5 or
* greater operate with certain Cisco 350 APs,
* the "gap" is filled with the SNAP header. Read
* it in after the 802.11 header.
*/
gap = m->m_data + sizeof(struct ieee80211_frame) -
sizeof(uint16_t);
an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t));
} else
gaplen = 0;
an_read_bap(sc, fid, -1,
m->m_data + sizeof(struct ieee80211_frame) + gaplen, len);
m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen +
len;
memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame));
m->m_pkthdr.rcvif = ifp;
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
#if NBPFILTER > 0
if (sc->sc_drvbpf) {
struct mbuf mb;
struct an_rx_radiotap_header *tap = &sc->sc_rxtap;
tap->ar_rate = frmhdr.an_rx_rate;
tap->ar_antsignal = frmhdr.an_rx_signal_strength;
tap->ar_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
tap->ar_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
M_DUP_PKTHDR(&mb, m);
mb.m_data = (caddr_t)tap;
mb.m_len = sizeof(sc->sc_rxtapu);
mb.m_next = m;
mb.m_pkthdr.len += mb.m_len;
bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
}
#endif /* NPBFILTER > 0 */
wh = mtod(m, struct ieee80211_frame *);
if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
/*
* WEP is decrypted by hardware. Clear WEP bit
* header for ieee80211_input().
*/
wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
}
ni = ieee80211_find_rxnode(ic, wh);
ieee80211_input(ifp, m, ni, frmhdr.an_rx_signal_strength,
letoh32(frmhdr.an_rx_time));
ieee80211_release_node(ic, ni);
}
void
an_txeof(struct an_softc *sc, int status)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
int cur, id;
sc->sc_tx_timer = 0;
ifp->if_flags &= ~IFF_OACTIVE;
id = CSR_READ_2(sc, AN_TX_CMP_FID);
CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC));
if (status & AN_EV_TX_EXC)
ifp->if_oerrors++;
else
ifp->if_opackets++;
cur = sc->sc_txcur;
if (sc->sc_txd[cur].d_fid == id) {
sc->sc_txd[cur].d_inuse = 0;
DPRINTF2(("an_txeof: sent %x/%d\n", id, cur));
AN_INC(cur, AN_TX_RING_CNT);
sc->sc_txcur = cur;
} else {
for (cur = 0; cur < AN_TX_RING_CNT; cur++) {
if (id == sc->sc_txd[cur].d_fid) {
sc->sc_txd[cur].d_inuse = 0;
break;
}
}
if (ifp->if_flags & IFF_DEBUG)
printf("%s: tx mismatch: "
"expected %x(%d), actual %x(%d)\n",
sc->sc_dev.dv_xname,
sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur,
id, cur);
}
}
int
an_intr(void *arg)
{
struct an_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ic.ic_if;
int i;
u_int16_t status;
if (!sc->sc_enabled || sc->sc_invalid ||
(sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
(ifp->if_flags & IFF_RUNNING) == 0)
return 0;
if ((ifp->if_flags & IFF_UP) == 0) {
CSR_WRITE_2(sc, AN_INT_EN, 0);
CSR_WRITE_2(sc, AN_EVENT_ACK, ~0);
return 1;
}
/* maximum 10 loops per interrupt */
for (i = 0; i < 10; i++) {
if (!sc->sc_enabled || sc->sc_invalid)
return 1;
if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) {
DPRINTF(("an_intr: magic number changed: %x\n",
CSR_READ_2(sc, AN_SW0)));
sc->sc_invalid = 1;
return 1;
}
status = CSR_READ_2(sc, AN_EVENT_STAT);
CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS));
if ((status & AN_INTRS) == 0)
break;
if (status & AN_EV_RX)
an_rxeof(sc);
if (status & (AN_EV_TX | AN_EV_TX_EXC))
an_txeof(sc, status);
if (status & AN_EV_LINKSTAT)
an_linkstat_intr(sc);
if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
sc->sc_ic.ic_state == IEEE80211_S_RUN &&
!IFQ_IS_EMPTY(&ifp->if_snd))
an_start(ifp);
}
return 1;
}
/* Must be called at proper protection level! */
int
an_cmd(struct an_softc *sc, int cmd, int val)
{
int i, stat;
/* make sure previous command completed */
if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
printf("%s: command 0x%x busy\n", sc->sc_dev.dv_xname,
CSR_READ_2(sc, AN_COMMAND));
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
}
CSR_WRITE_2(sc, AN_PARAM0, val);
CSR_WRITE_2(sc, AN_PARAM1, 0);
CSR_WRITE_2(sc, AN_PARAM2, 0);
CSR_WRITE_2(sc, AN_COMMAND, cmd);
if (cmd == AN_CMD_FW_RESTART) {
/* XXX: should sleep here */
DELAY(100*1000);
}
for (i = 0; i < AN_TIMEOUT; i++) {
if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
break;
DELAY(10);
}
stat = CSR_READ_2(sc, AN_STATUS);
/* clear stuck command busy if necessary */
if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY)
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
/* Ack the command */
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
if (i == AN_TIMEOUT) {
if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
printf("%s: command 0x%x param 0x%x timeout\n",
sc->sc_dev.dv_xname, cmd, val);
return ETIMEDOUT;
}
if (stat & AN_STAT_CMD_RESULT) {
if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
printf("%s: command 0x%x param 0x%x status 0x%x "
"resp 0x%x 0x%x 0x%x\n",
sc->sc_dev.dv_xname, cmd, val, stat,
CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1),
CSR_READ_2(sc, AN_RESP2));
return EIO;
}
return 0;
}
int
an_reset(struct an_softc *sc)
{
DPRINTF(("an_reset\n"));
if (!sc->sc_enabled)
return ENXIO;
an_cmd(sc, AN_CMD_ENABLE, 0);
an_cmd(sc, AN_CMD_FW_RESTART, 0);
an_cmd(sc, AN_CMD_NOOP2, 0);
if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) {
printf("%s: reset failed\n", sc->sc_dev.dv_xname);
return ETIMEDOUT;
}
an_cmd(sc, AN_CMD_DISABLE, 0);
return 0;
}
void
an_linkstat_intr(struct an_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
u_int16_t status;
status = CSR_READ_2(sc, AN_LINKSTAT);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
DPRINTF(("an_linkstat_intr: status 0x%x\n", status));
if (status == AN_LINKSTAT_ASSOCIATED) {
if (ic->ic_state != IEEE80211_S_RUN ||
ic->ic_opmode == IEEE80211_M_IBSS)
ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
} else {
if (ic->ic_opmode == IEEE80211_M_STA)
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
}
}
/*
* Wait for firmware come up after power enabled.
*/
void
an_wait(struct an_softc *sc)
{
int i;
CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2);
for (i = 0; i < 3*hz; i++) {
if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
break;
(void)tsleep(sc, PWAIT, "anatch", 1);
}
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
}
int
an_read_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
{
int error, cnt;
if (buflen == 0)
return 0;
if (off == -1)
off = sc->sc_bap_off;
if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
if ((error = an_seek_bap(sc, id, off)) != 0)
return EIO;
}
cnt = (buflen + 1) / 2;
CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
sc->sc_bap_off += cnt * 2;
#if BYTE_ORDER == BIG_ENDIAN
switch(id) {
case AN_RID_GENCONFIG:
case AN_RID_ACTUALCFG:
break;
case AN_RID_SSIDLIST:
break;
case AN_RID_APLIST:
break;
case AN_RID_DRVNAME:
break;
case AN_RID_CAPABILITIES:
break;
case AN_RID_STATUS:
break;
case AN_RID_WEP_VOLATILE:
case AN_RID_WEP_PERMANENT:
break;
case AN_RID_32BITS_CUM:
break;
}
#endif
return 0;
}
int
an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
{
int error, cnt;
if (buflen == 0)
return 0;
if (off == -1)
off = sc->sc_bap_off;
if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
if ((error = an_seek_bap(sc, id, off)) != 0)
return EIO;
}
cnt = (buflen + 1) / 2;
CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
sc->sc_bap_off += cnt * 2;
return 0;
}
int
an_seek_bap(struct an_softc *sc, int id, int off)
{
int i, status;
CSR_WRITE_2(sc, AN_SEL0, id);
CSR_WRITE_2(sc, AN_OFF0, off);
for (i = 0; ; i++) {
status = CSR_READ_2(sc, AN_OFF0);
if ((status & AN_OFF_BUSY) == 0)
break;
if (i == AN_TIMEOUT) {
printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n",
sc->sc_dev.dv_xname, id, off);
sc->sc_bap_off = AN_OFF_ERR; /* invalidate */
return ETIMEDOUT;
}
DELAY(10);
}
if (status & AN_OFF_ERR) {
printf("%s: failed in an_seek_bap to 0x%x/0x%x\n",
sc->sc_dev.dv_xname, id, off);
sc->sc_bap_off = AN_OFF_ERR; /* invalidate */
return EIO;
}
sc->sc_bap_id = id;
sc->sc_bap_off = off;
return 0;
}
int
an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen)
{
int error, len, cnt;
if (off == -1)
off = sc->sc_bap_off;
if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
if ((error = an_seek_bap(sc, id, off)) != 0)
return EIO;
}
for (len = 0; m != NULL; m = m->m_next) {
if (m->m_len == 0)
continue;
len = min(m->m_len, totlen);
if ((mtod(m, u_long) & 0x1) || (len & 0x1)) {
m_copydata(m, 0, totlen, (caddr_t)&sc->sc_buf.sc_txbuf);
cnt = (totlen + 1) / 2;
CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0,
sc->sc_buf.sc_val, cnt);
off += cnt * 2;
break;
}
cnt = len / 2;
CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *),
cnt);
off += len;
totlen -= len;
}
sc->sc_bap_off = off;
return 0;
}
int
an_alloc_nicmem(struct an_softc *sc, int len, int *idp)
{
int i;
if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
printf("%s: failed to allocate %d bytes on NIC\n",
sc->sc_dev.dv_xname, len);
return(ENOMEM);
}
for (i = 0; i < AN_TIMEOUT; i++) {
if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC)
break;
if (i == AN_TIMEOUT) {
printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
return ETIMEDOUT;
}
DELAY(10);
}
*idp = CSR_READ_2(sc, AN_ALLOC_FID);
CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
return 0;
}
int
an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp)
{
int error;
u_int16_t len;
/* Tell the NIC to enter record read mode. */
error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid);
if (error)
return error;
/* length in byte, including length itself */
error = an_read_bap(sc, rid, 0, &len, sizeof(len));
if (error)
return error;
len = letoh16(len) - 2;
if (*buflenp < len) {
printf("%s: record buffer is too small, "
"rid=%x, size=%d, len=%d\n",
sc->sc_dev.dv_xname, rid, *buflenp, len);
return ENOSPC;
}
*buflenp = len;
return an_read_bap(sc, rid, sizeof(len), buf, len);
}
int
an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen)
{
int error;
u_int16_t len;
/* length in byte, including length itself */
len = htole16(buflen + 2);
error = an_write_bap(sc, rid, 0, &len, sizeof(len));
if (error)
return error;
error = an_write_bap(sc, rid, sizeof(len), buf, buflen);
if (error)
return error;
return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid);
}
int
an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct an_softc *sc = ifp->if_softc;
struct ifaddr *ifa = (struct ifaddr *)data;
int s, error = 0;
if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
return ENXIO;
s = splnet();
switch(command) {
case SIOCSIFADDR:
ifp->if_flags |= IFF_UP;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
error = an_init(ifp);
arp_ifinit(&sc->sc_ic.ic_ac, ifa);
break;
#endif
default:
error = an_init(ifp);
break;
}
break;
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (sc->sc_enabled) {
/*
* To avoid rescanning another access point,
* do not call an_init() here. Instead, only
* reflect promisc mode settings.
*/
error = an_cmd(sc, AN_CMD_SET_MODE,
(ifp->if_flags & IFF_PROMISC) ? 0xffff : 0);
} else
error = an_init(ifp);
} else if (sc->sc_enabled)
an_stop(ifp, 1);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
/* The Aironet has no multicast filter. */
error = 0;
break;
case SIOCS80211NWKEY:
error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data);
break;
case SIOCG80211NWKEY:
error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data);
break;
default:
error = ieee80211_ioctl(ifp, command, data);
break;
}
if (error == ENETRESET) {
if (sc->sc_enabled)
error = an_init(ifp);
else
error = 0;
}
splx(s);
return(error);
}
int
an_init(struct ifnet *ifp)
{
struct an_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
int i, error, fid;
DPRINTF(("an_init: enabled %d\n", sc->sc_enabled));
if (!sc->sc_enabled) {
if (sc->sc_enable)
(*sc->sc_enable)(sc);
an_wait(sc);
sc->sc_enabled = 1;
} else {
an_stop(ifp, 0);
if ((error = an_reset(sc)) != 0) {
printf("%s: failed to reset\n", ifp->if_xname);
an_stop(ifp, 1);
return error;
}
}
CSR_WRITE_2(sc, AN_SW0, AN_MAGIC);
/* Allocate the TX buffers */
for (i = 0; i < AN_TX_RING_CNT; i++) {
if ((error = an_alloc_nicmem(sc, AN_TX_MAX_LEN, &fid)) != 0) {
printf("%s: failed to allocate nic memory\n",
ifp->if_xname);
an_stop(ifp, 1);
return error;
}
DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid));
sc->sc_txd[i].d_fid = fid;
sc->sc_txd[i].d_inuse = 0;
}
sc->sc_txcur = sc->sc_txnext = 0;
IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr);
sc->sc_config.an_scanmode = htole16(AN_SCANMODE_ACTIVE);
sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_OPEN); /*XXX*/
if (ic->ic_flags & IEEE80211_F_WEPON) {
sc->sc_config.an_authtype |=
htole16(AN_AUTHTYPE_PRIVACY_IN_USE);
}
sc->sc_config.an_listen_interval = htole16(ic->ic_lintval);
sc->sc_config.an_beacon_period = htole16(ic->ic_lintval);
if (ic->ic_flags & IEEE80211_F_PMGTON)
sc->sc_config.an_psave_mode = htole16(AN_PSAVE_PSP);
else
sc->sc_config.an_psave_mode = htole16(AN_PSAVE_CAM);
sc->sc_config.an_ds_channel =
htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
switch (ic->ic_opmode) {
case IEEE80211_M_STA:
sc->sc_config.an_opmode =
htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
break;
case IEEE80211_M_IBSS:
sc->sc_config.an_opmode = htole16(AN_OPMODE_IBSS_ADHOC);
sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
break;
case IEEE80211_M_MONITOR:
sc->sc_config.an_opmode =
htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
sc->sc_config.an_rxmode =
htole16(AN_RXMODE_80211_MONITOR_ANYBSS);
sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_NONE);
if (ic->ic_flags & IEEE80211_F_WEPON)
sc->sc_config.an_authtype |=
htole16(AN_AUTHTYPE_PRIVACY_IN_USE |
AN_AUTHTYPE_ALLOW_UNENCRYPTED);
break;
default:
printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode);
an_stop(ifp, 1);
return EIO;
}
sc->sc_config.an_rxmode |= htole16(AN_RXMODE_NO_8023_HEADER);
/* Set the ssid list */
memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist));
sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len =
htole16(ic->ic_des_esslen);
if (ic->ic_des_esslen)
memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid,
ic->ic_des_essid, ic->ic_des_esslen);
if (an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf,
sizeof(sc->sc_buf.sc_ssidlist)) != 0) {
printf("%s: failed to write ssid list\n", ifp->if_xname);
an_stop(ifp, 1);
return error;
}
/* Set the AP list */
memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist));
(void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf,
sizeof(sc->sc_buf.sc_aplist));
/* Set the encapsulation */
for (i = 0; i < AN_ENCAP_NENTS; i++) {
sc->sc_buf.sc_encap.an_entry[i].an_ethertype = htole16(0);
sc->sc_buf.sc_encap.an_entry[i].an_action =
htole16(AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024);
}
(void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf,
sizeof(sc->sc_buf.sc_encap));
/* Set the WEP Keys */
if (ic->ic_flags & IEEE80211_F_WEPON)
an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys,
sc->sc_tx_key);
/* Set the configuration */
if (an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config,
sizeof(sc->sc_config)) != 0) {
printf("%s: failed to write config\n", ifp->if_xname);
an_stop(ifp, 1);
return error;
}
/* Enable the MAC */
if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
printf("%s: failed to enable MAC\n", sc->sc_dev.dv_xname);
an_stop(ifp, 1);
return ENXIO;
}
if (ifp->if_flags & IFF_PROMISC)
an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
ifp->if_flags |= IFF_RUNNING;
ifp->if_flags &= ~IFF_OACTIVE;
ic->ic_state = IEEE80211_S_INIT;
if (ic->ic_opmode == IEEE80211_M_MONITOR)
ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
/* enable interrupts */
CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS);
return 0;
}
void
an_start(struct ifnet *ifp)
{
struct an_softc *sc = (struct an_softc *)ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_node *ni;
struct ieee80211_frame *wh;
struct an_txframe frmhdr;
struct mbuf *m;
u_int16_t len;
int cur, fid;
if (!sc->sc_enabled || sc->sc_invalid) {
DPRINTF(("an_start: noop: enabled %d invalid %d\n",
sc->sc_enabled, sc->sc_invalid));
return;
}
memset(&frmhdr, 0, sizeof(frmhdr));
cur = sc->sc_txnext;
for (;;) {
if (ic->ic_state != IEEE80211_S_RUN) {
DPRINTF(("an_start: not running %d\n", ic->ic_state));
break;
}
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL) {
DPRINTF2(("an_start: no pending mbuf\n"));
break;
}
if (sc->sc_txd[cur].d_inuse) {
DPRINTF2(("an_start: %x/%d busy\n",
sc->sc_txd[cur].d_fid, cur));
ifp->if_flags |= IFF_OACTIVE;
break;
}
IFQ_DEQUEUE(&ifp->if_snd, m);
ifp->if_opackets++;
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
#endif
if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) {
ifp->if_oerrors++;
continue;
}
if (ni != NULL)
ieee80211_release_node(ic, ni);
#if NBPFILTER > 0
if (ic->ic_rawbpf)
bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
#endif
wh = mtod(m, struct ieee80211_frame *);
if (ic->ic_flags & IEEE80211_F_WEPON)
wh->i_fc[1] |= IEEE80211_FC1_WEP;
m_copydata(m, 0, sizeof(struct ieee80211_frame),
(caddr_t)&frmhdr.an_whdr);
/* insert payload length in front of llc/snap */
len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame));
m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len));
if (mtod(m, u_long) & 0x01)
memcpy(mtod(m, caddr_t), &len, sizeof(len));
else
*mtod(m, u_int16_t *) = len;
/*
* XXX Aironet firmware apparently convert the packet
* with longer than 1500 bytes in length into LLC/SNAP.
* If we have 1500 bytes in ethernet payload, it is
* 1508 bytes including LLC/SNAP and will be inserted
* additional LLC/SNAP header with 1501-1508 in its
* ethertype !!
* So we skip LLC/SNAP header and force firmware to
* convert it to LLC/SNAP again.
*/
m_adj(m, sizeof(struct llc));
frmhdr.an_tx_ctl = htole16(AN_TXCTL_80211);
frmhdr.an_tx_payload_len = htole16(m->m_pkthdr.len);
frmhdr.an_gaplen = htole16(AN_TXGAP_802_11);
if (ic->ic_fixed_rate != -1)
frmhdr.an_tx_rate =
ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
else
frmhdr.an_tx_rate = 0;
if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) +
m->m_pkthdr.len > AN_TX_MAX_LEN) {
ifp->if_oerrors++;
m_freem(m);
continue;
}
#if NBPFILTER > 0
if (sc->sc_drvbpf) {
struct mbuf mb;
struct an_tx_radiotap_header *tap = &sc->sc_txtap;
tap->at_rate =
ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate];
tap->at_chan_freq =
htole16(ic->ic_bss->ni_chan->ic_freq);
tap->at_chan_flags =
htole16(ic->ic_bss->ni_chan->ic_flags);
M_DUP_PKTHDR(&mb, m);
mb.m_data = (caddr_t)tap;
mb.m_len = sizeof(sc->sc_txtapu);
mb.m_next = m;
mb.m_pkthdr.len += mb.m_len;
bpf_mtap(sc->sc_drvbpf, m, BPF_DIRECTION_OUT);
}
#endif
fid = sc->sc_txd[cur].d_fid;
if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
ifp->if_oerrors++;
m_freem(m);
continue;
}
/* dummy write to avoid seek. */
an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11);
an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len);
m_freem(m);
DPRINTF2(("an_start: send %d byte via %x/%d\n",
ntohs(len) + sizeof(struct ieee80211_frame),
fid, cur));
sc->sc_txd[cur].d_inuse = 1;
if (an_cmd(sc, AN_CMD_TX, fid)) {
printf("%s: xmit failed\n", ifp->if_xname);
sc->sc_txd[cur].d_inuse = 0;
continue;
}
sc->sc_tx_timer = 5;
ifp->if_timer = 1;
AN_INC(cur, AN_TX_RING_CNT);
sc->sc_txnext = cur;
}
}
void
an_stop(struct ifnet *ifp, int disable)
{
struct an_softc *sc = ifp->if_softc;
int i, s;
if (!sc->sc_enabled)
return;
DPRINTF(("an_stop: disable %d\n", disable));
s = splnet();
ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
if (!sc->sc_invalid) {
an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
CSR_WRITE_2(sc, AN_INT_EN, 0);
an_cmd(sc, AN_CMD_DISABLE, 0);
for (i = 0; i < AN_TX_RING_CNT; i++)
an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid);
}
sc->sc_tx_timer = 0;
ifp->if_timer = 0;
ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
if (disable) {
if (sc->sc_disable)
(*sc->sc_disable)(sc);
sc->sc_enabled = 0;
}
splx(s);
}
void
an_watchdog(struct ifnet *ifp)
{
struct an_softc *sc = ifp->if_softc;
if (!sc->sc_enabled)
return;
if (sc->sc_tx_timer) {
if (--sc->sc_tx_timer == 0) {
printf("%s: device timeout\n", ifp->if_xname);
ifp->if_oerrors++;
an_init(ifp);
return;
}
ifp->if_timer = 1;
}
ieee80211_watchdog(ifp);
}
void
an_shutdown(void *self)
{
struct an_softc *sc = (struct an_softc *)self;
if (sc->sc_attached)
an_stop(&sc->sc_ic.ic_if, 1);
}
/* TBD factor with ieee80211_media_change */
int
an_media_change(struct ifnet *ifp)
{
struct an_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
struct ifmedia_entry *ime;
enum ieee80211_opmode newmode;
int i, rate, error = 0;
ime = ic->ic_media.ifm_cur;
if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
i = -1;
} else {
struct ieee80211_rateset *rs =
&ic->ic_sup_rates[IEEE80211_MODE_11B];
rate = ieee80211_media2rate(ime->ifm_media);
if (rate == 0)
return EINVAL;
for (i = 0; i < rs->rs_nrates; i++) {
if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
break;
}
if (i == rs->rs_nrates)
return EINVAL;
}
if (ic->ic_fixed_rate != i) {
ic->ic_fixed_rate = i;
error = ENETRESET;
}
if (ime->ifm_media & IFM_IEEE80211_ADHOC)
newmode = IEEE80211_M_IBSS;
else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
newmode = IEEE80211_M_HOSTAP;
else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
newmode = IEEE80211_M_MONITOR;
else
newmode = IEEE80211_M_STA;
if (ic->ic_opmode != newmode) {
ic->ic_opmode = newmode;
error = ENETRESET;
}
if (error == ENETRESET) {
if (sc->sc_enabled)
error = an_init(ifp);
else
error = 0;
}
ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
return error;
}
void
an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
{
struct an_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
int rate, buflen;
if (sc->sc_enabled == 0) {
imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
imr->ifm_status = 0;
return;
}
imr->ifm_status = IFM_AVALID;
imr->ifm_active = IFM_IEEE80211;
if (ic->ic_state == IEEE80211_S_RUN)
imr->ifm_status |= IFM_ACTIVE;
buflen = sizeof(sc->sc_buf);
if (ic->ic_fixed_rate != -1)
rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0)
rate = 0;
else
rate = letoh16(sc->sc_buf.sc_status.an_current_tx_rate);
imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
switch (ic->ic_opmode) {
case IEEE80211_M_STA:
break;
case IEEE80211_M_IBSS:
imr->ifm_active |= IFM_IEEE80211_ADHOC;
break;
case IEEE80211_M_HOSTAP:
imr->ifm_active |= IFM_IEEE80211_HOSTAP;
break;
case IEEE80211_M_MONITOR:
imr->ifm_active |= IFM_IEEE80211_MONITOR;
break;
default:
break;
}
}
int
an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
{
int error;
struct ieee80211com *ic = &sc->sc_ic;
u_int16_t prevauth;
error = 0;
prevauth = sc->sc_config.an_authtype;
switch (nwkey->i_wepon) {
case IEEE80211_NWKEY_OPEN:
sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;
ic->ic_flags &= ~IEEE80211_F_WEPON;
break;
case IEEE80211_NWKEY_WEP:
case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST:
error = an_set_nwkey_wep(sc, nwkey);
if (error == 0 || error == ENETRESET) {
sc->sc_config.an_authtype =
AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE;
ic->ic_flags |= IEEE80211_F_WEPON;
}
break;
default:
error = EINVAL;
break;
}
if (error == 0 && prevauth != sc->sc_config.an_authtype)
error = ENETRESET;
return error;
}
int
an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
{
int i, txkey, anysetkey, needreset, error;
struct an_wepkey keys[IEEE80211_WEP_NKID];
error = 0;
memset(keys, 0, sizeof(keys));
anysetkey = needreset = 0;
/* load argument and sanity check */
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen;
if (keys[i].an_wep_keylen < 0)
continue;
if (keys[i].an_wep_keylen != 0 &&
keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN)
return EINVAL;
if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key))
return EINVAL;
if ((error = copyin(nwkey->i_key[i].i_keydat,
keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0)
return error;
anysetkey++;
}
txkey = nwkey->i_defkid - 1;
if (txkey >= 0) {
if (txkey >= IEEE80211_WEP_NKID)
return EINVAL;
/* default key must have a valid value */
if (keys[txkey].an_wep_keylen == 0 ||
(keys[txkey].an_wep_keylen < 0 &&
sc->sc_perskeylen[txkey] == 0))
return EINVAL;
anysetkey++;
}
DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) "
"pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n",
sc->sc_dev.dv_xname,
((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""),
sc->sc_tx_key,
sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen,
sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen,
sc->sc_tx_perskey,
sc->sc_perskeylen[0], sc->sc_perskeylen[1],
sc->sc_perskeylen[2], sc->sc_perskeylen[3],
txkey,
keys[0].an_wep_keylen, keys[1].an_wep_keylen,
keys[2].an_wep_keylen, keys[3].an_wep_keylen));
if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) {
/* set temporary keys */
sc->sc_tx_key = txkey;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (keys[i].an_wep_keylen < 0)
continue;
memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i]));
}
} else {
/* set persist keys */
if (anysetkey) {
/* prepare to write nvram */
if (!sc->sc_enabled) {
if (sc->sc_enable)
(*sc->sc_enable)(sc);
an_wait(sc);
sc->sc_enabled = 1;
error = an_write_wepkey(sc,
AN_RID_WEP_PERSISTENT, keys, txkey);
if (sc->sc_disable)
(*sc->sc_disable)(sc);
sc->sc_enabled = 0;
} else {
an_cmd(sc, AN_CMD_DISABLE, 0);
error = an_write_wepkey(sc,
AN_RID_WEP_PERSISTENT, keys, txkey);
an_cmd(sc, AN_CMD_ENABLE, 0);
}
if (error)
return error;
}
if (txkey >= 0)
sc->sc_tx_perskey = txkey;
if (sc->sc_tx_key >= 0) {
sc->sc_tx_key = -1;
needreset++;
}
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (sc->sc_wepkeys[i].an_wep_keylen >= 0) {
memset(&sc->sc_wepkeys[i].an_wep_key, 0,
sizeof(sc->sc_wepkeys[i].an_wep_key));
sc->sc_wepkeys[i].an_wep_keylen = -1;
needreset++;
}
if (keys[i].an_wep_keylen >= 0)
sc->sc_perskeylen[i] = keys[i].an_wep_keylen;
}
}
if (needreset) {
/* firmware restart to reload persistent key */
an_reset(sc);
}
if (anysetkey || needreset)
error = ENETRESET;
return error;
}
int
an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
{
int i, error;
error = 0;
if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP)
nwkey->i_wepon = IEEE80211_NWKEY_EAP;
else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
nwkey->i_wepon = IEEE80211_NWKEY_WEP;
else
nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
if (sc->sc_tx_key == -1)
nwkey->i_defkid = sc->sc_tx_perskey + 1;
else
nwkey->i_defkid = sc->sc_tx_key + 1;
if (nwkey->i_key[0].i_keydat == NULL)
return 0;
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (nwkey->i_key[i].i_keydat == NULL)
continue;
/* do not show any keys to non-root user */
if ((error = suser(curproc, 0)) != 0)
break;
nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen;
if (nwkey->i_key[i].i_keylen < 0) {
if (sc->sc_perskeylen[i] == 0)
nwkey->i_key[i].i_keylen = 0;
continue;
}
if ((error = copyout(sc->sc_wepkeys[i].an_wep_key,
nwkey->i_key[i].i_keydat,
sc->sc_wepkeys[i].an_wep_keylen)) != 0)
break;
}
return error;
}
int
an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid)
{
int i, error;
struct an_rid_wepkey *akey;
error = 0;
akey = &sc->sc_buf.sc_wepkey;
memset(akey, 0, sizeof(struct an_rid_wepkey));
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (keys[i].an_wep_keylen < 0 ||
keys[i].an_wep_keylen > sizeof(akey->an_key))
continue;
akey->an_key_len = htole16(keys[i].an_wep_keylen);
akey->an_key_index = htole16(i);
akey->an_mac_addr[0] = 1; /* default mac */
memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen);
if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0)
return error;
}
if (kid >= 0) {
akey->an_key_index = htole16(0xffff);
akey->an_mac_addr[0] = kid;
akey->an_key_len = htole16(0);
memset(akey->an_key, 0, sizeof(akey->an_key));
error = an_write_rid(sc, type, akey, sizeof(*akey));
}
return error;
}
int
an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
{
struct an_softc *sc = ic->ic_softc;
struct ieee80211_node *ni = ic->ic_bss;
enum ieee80211_state ostate;
int buflen;
ostate = ic->ic_state;
DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate],
ieee80211_state_name[nstate]));
switch (nstate) {
case IEEE80211_S_INIT:
ic->ic_flags &= ~IEEE80211_F_IBSSON;
return (*sc->sc_newstate)(ic, nstate, arg);
case IEEE80211_S_RUN:
buflen = sizeof(sc->sc_buf);
an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen);
IEEE80211_ADDR_COPY(ni->ni_bssid,
sc->sc_buf.sc_status.an_cur_bssid);
IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
ni->ni_chan = &ic->ic_channels[
letoh16(sc->sc_buf.sc_status.an_cur_channel)];
ni->ni_esslen = letoh16(sc->sc_buf.sc_status.an_ssidlen);
if (ni->ni_esslen > IEEE80211_NWID_LEN)
ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid,
ni->ni_esslen);
ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; /*XXX*/
if (ic->ic_if.if_flags & IFF_DEBUG) {
printf("%s: ", sc->sc_dev.dv_xname);
if (ic->ic_opmode == IEEE80211_M_STA)
printf("associated ");
else
printf("synchronized ");
printf("with %s ssid ", ether_sprintf(ni->ni_bssid));
ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
printf(" channel %u start %uMb\n",
letoh16(sc->sc_buf.sc_status.an_cur_channel),
letoh16(sc->sc_buf.sc_status.an_current_tx_rate)/2);
}
break;
default:
break;
}
ic->ic_state = nstate;
/* skip standard ieee80211 handling */
return 0;
}
int
an_detach(struct an_softc *sc)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
int s;
if (!sc->sc_attached)
return 0;
s = splnet();
sc->sc_invalid = 1;
an_stop(ifp, 1);
ifmedia_delete_instance(&sc->sc_ic.ic_media, IFM_INST_ANY);
ieee80211_ifdetach(ifp);
if_detach(ifp);
shutdownhook_disestablish(sc->sc_sdhook);
splx(s);
return 0;
}
|