Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
/*	$NetBSD: if_malo_pcmcia.c,v 1.27 2021/11/10 17:19:30 msaitoh Exp $	*/
/*      $OpenBSD: if_malo.c,v 1.65 2009/03/29 21:53:53 sthen Exp $ */

/*
 * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
 *
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_malo_pcmcia.c,v 1.27 2021/11/10 17:19:30 msaitoh Exp $");

#ifdef _MODULE
#include <sys/module.h>
#endif

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/condvar.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/pmf.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/systm.h>

#include <net/bpf.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#include <net/if_llc.h>

#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>

#include <dev/firmload.h>

#include <dev/pcmcia/pcmciareg.h>
#include <dev/pcmcia/pcmciavar.h>
#include <dev/pcmcia/pcmciadevs.h>

#include <dev/pcmcia/if_malo_pcmciavar.h>
#include <dev/pcmcia/if_malo_pcmciareg.h>

/*
 * Driver for the Marvell 88W8385 chip (Compact Flash).
 */

#ifdef CMALO_DEBUG
int cmalo_d = 1;
#define DPRINTF(l, x...)	do { if ((l) <= cmalo_d) printf(x); } while (0)
#else
#define DPRINTF(l, x...)	do {} while (0)
#endif

static int	malo_pcmcia_match(device_t, cfdata_t, void *);
static void	malo_pcmcia_attach(device_t, device_t, void *);
static int	malo_pcmcia_detach(device_t, int);
static int	malo_pcmcia_activate(device_t, devact_t);

static int	malo_pcmcia_validate_config(struct pcmcia_config_entry *);

static int	malo_pcmcia_enable(struct malo_softc *);
static void	malo_pcmcia_disable(struct malo_softc *);

static void	cmalo_attach(void *);
static void	cmalo_detach(void *);
static int	cmalo_intr(void *);
static void	cmalo_softintr(void *);

static void	cmalo_start(struct ifnet *);
static int	cmalo_ioctl(struct ifnet *, u_long, void *);
static int	cmalo_init(struct ifnet *);
static void	cmalo_watchdog(struct ifnet *);
static int	cmalo_media_change(struct ifnet *);
static int	cmalo_newstate(struct ieee80211com *, enum ieee80211_state,
			       int);

static int	firmware_load(const char *, const char *, uint8_t **, size_t *);
static int	cmalo_fw_alloc(struct malo_softc *);
static void	cmalo_fw_free(struct malo_softc *);
static int	cmalo_fw_load_helper(struct malo_softc *);
static int	cmalo_fw_load_main(struct malo_softc *);

static void	cmalo_stop(struct malo_softc *);
static void	cmalo_intr_mask(struct malo_softc *, int);
static void	cmalo_rx(struct malo_softc *);
static int	cmalo_tx(struct malo_softc *, struct mbuf *);
static void	cmalo_tx_done(struct malo_softc *);
static void	cmalo_event(struct malo_softc *);
static void	cmalo_select_network(struct malo_softc *);
static void	cmalo_reflect_network(struct malo_softc *);
static int	cmalo_wep(struct malo_softc *);
static int	cmalo_rate2bitmap(int);

static void	cmalo_hexdump(void *, int);
static int	cmalo_cmd_get_hwspec(struct malo_softc *);
static int	cmalo_cmd_rsp_hwspec(struct malo_softc *);
static int	cmalo_cmd_set_reset(struct malo_softc *);
static int	cmalo_cmd_set_scan(struct malo_softc *);
static int	cmalo_cmd_rsp_scan(struct malo_softc *);
static int	cmalo_parse_elements(struct malo_softc *, uint8_t *, int, int);
static int	cmalo_cmd_set_auth(struct malo_softc *);
static int	cmalo_cmd_set_wep(struct malo_softc *, uint16_t,
		    struct ieee80211_key *);
static int	cmalo_cmd_set_snmp(struct malo_softc *, uint16_t);
static int	cmalo_cmd_set_radio(struct malo_softc *, uint16_t);
static int	cmalo_cmd_set_channel(struct malo_softc *, uint16_t);
static int	cmalo_cmd_set_txpower(struct malo_softc *, int16_t);
static int	cmalo_cmd_set_antenna(struct malo_softc *, uint16_t);
static int	cmalo_cmd_set_macctrl(struct malo_softc *);
static int	cmalo_cmd_set_macaddr(struct malo_softc *, uint8_t *);
static int	cmalo_cmd_set_assoc(struct malo_softc *);
static int	cmalo_cmd_rsp_assoc(struct malo_softc *);
static int	cmalo_cmd_set_rate(struct malo_softc *, int);
static int	cmalo_cmd_request(struct malo_softc *, uint16_t, int);
static int	cmalo_cmd_response(struct malo_softc *);

/*
 * PCMCIA bus.
 */
struct malo_pcmcia_softc {
	struct malo_softc	 sc_malo;

	struct pcmcia_function	*sc_pf;
	struct pcmcia_io_handle	 sc_pcioh;
	int			 sc_io_window;
	void			*sc_ih;
};

CFATTACH_DECL_NEW(malo_pcmcia, sizeof(struct malo_pcmcia_softc),
	malo_pcmcia_match, malo_pcmcia_attach, malo_pcmcia_detach,
	malo_pcmcia_activate);


static int
malo_pcmcia_match(device_t parent, cfdata_t match, void *aux)
{
	struct pcmcia_attach_args *pa = aux;

	if (pa->manufacturer == PCMCIA_VENDOR_AMBICOM &&
	    pa->product == PCMCIA_PRODUCT_AMBICOM_WL54CF)
		return 1;

	return 0;
}

static void
malo_pcmcia_attach(device_t parent, device_t self, void *aux)
{
	struct malo_pcmcia_softc *psc = device_private(self);
	struct malo_softc *sc = &psc->sc_malo;
	struct pcmcia_attach_args *pa = aux;
	struct pcmcia_config_entry *cfe;
	int error;

	sc->sc_dev = self;
	psc->sc_pf = pa->pf;

	error = pcmcia_function_configure(pa->pf, malo_pcmcia_validate_config);
	if (error) {
		aprint_error_dev(self, "configure failed, error=%d\n", error);
		return;
	}

	sc->sc_soft_ih = softint_establish(SOFTINT_NET, cmalo_softintr, sc);
	if (sc->sc_soft_ih == NULL) {
		aprint_error_dev(self, "couldn't establish softint\n");
		return;
	}

	malo_pcmcia_enable(sc);

	cfe = pa->pf->cfe;
	sc->sc_iot = cfe->iospace[0].handle.iot;
	sc->sc_ioh = cfe->iospace[0].handle.ioh;

	cmalo_attach(sc);
	if (!(sc->sc_flags & MALO_DEVICE_ATTACHED))
		goto fail;

	if (pmf_device_register(self, NULL, NULL))
		pmf_class_network_register(self, &sc->sc_if);
	else
		aprint_error_dev(self, "couldn't establish power handler\n");

fail:
	malo_pcmcia_disable(sc);

	if (sc->sc_flags & MALO_DEVICE_ATTACHED)
		return;

	softint_disestablish(sc->sc_soft_ih);
	sc->sc_soft_ih = NULL;

	pcmcia_function_unconfigure(pa->pf);
	return;
}

static int
malo_pcmcia_detach(device_t dev, int flags)
{
	struct malo_pcmcia_softc *psc = device_private(dev);
	struct malo_softc *sc = &psc->sc_malo;

	cmalo_detach(sc);
	malo_pcmcia_disable(sc);
	softint_disestablish(sc->sc_soft_ih);
	sc->sc_soft_ih = NULL;
	pcmcia_function_unconfigure(psc->sc_pf);

	return 0;
}

static int
malo_pcmcia_activate(device_t dev, devact_t act)
{
	struct malo_pcmcia_softc *psc = device_private(dev);
	struct malo_softc *sc = &psc->sc_malo;
	struct ifnet *ifp = &sc->sc_if;
	int s;

	s = splnet();
	switch (act) {
	case DVACT_DEACTIVATE:
		if_deactivate(ifp);
		break;
	default:
		splx(s);
		return EOPNOTSUPP;
	}
	splx(s);

	return 0;
}


int
malo_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
{

	if (cfe->iftype != PCMCIA_IFTYPE_IO || cfe->num_iospace != 1)
		return EINVAL;
	/* Some cards have a memory space, but we don't use it. */
	cfe->num_memspace = 0;
	return 0;
}


static int
malo_pcmcia_enable(struct malo_softc *sc)
{
	struct malo_pcmcia_softc *psc = (struct malo_pcmcia_softc *)sc;

	/* establish interrupt */
	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cmalo_intr, sc);
	if (psc->sc_ih == NULL) {
		aprint_error(": can't establish interrupt\n");
		return -1;
	}

	if (pcmcia_function_enable(psc->sc_pf)) {
		aprint_error(": can't enable function\n");
		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
		return -1;
	}
	sc->sc_flags |= MALO_DEVICE_ENABLED;

	return 0;
}

static void
malo_pcmcia_disable(struct malo_softc *sc)
{
	struct malo_pcmcia_softc *psc = (struct malo_pcmcia_softc *)sc;

	pcmcia_function_disable(psc->sc_pf);
	if (psc->sc_ih)
		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
	psc->sc_ih = NULL;
	sc->sc_flags &= ~MALO_DEVICE_ENABLED;
}


/*
 * Driver.
 */
static void
cmalo_attach(void *arg)
{
	struct malo_softc *sc = arg;
	struct ieee80211com *ic = &sc->sc_ic;
	struct ifnet *ifp = &sc->sc_if;
	int i;

	/* disable interrupts */
	cmalo_intr_mask(sc, 0);

	/* load firmware */
	if (cmalo_fw_alloc(sc) != 0 ||
	    cmalo_fw_load_helper(sc) != 0 ||
	    cmalo_fw_load_main(sc) != 0) {
		/* free firmware */
		cmalo_fw_free(sc);
		goto fail;
	}
	sc->sc_flags |= MALO_FW_LOADED;

	/* allocate command buffer */
	sc->sc_cmd = malloc(MALO_CMD_BUFFER_SIZE, M_DEVBUF, M_WAITOK);

	/* allocate data buffer */
	sc->sc_data = malloc(MALO_DATA_BUFFER_SIZE, M_DEVBUF, M_WAITOK);

	/* enable interrupts */
	cmalo_intr_mask(sc, 1);

	/* we are context save here for FW commands */
	sc->sc_cmd_ctxsave = 1;

	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
	cv_init(&sc->sc_cv, "malo");

	/* get hardware specs */
	cmalo_cmd_get_hwspec(sc);

	/* setup interface */
	ifp->if_softc = sc;
	ifp->if_start = cmalo_start;
	ifp->if_ioctl = cmalo_ioctl;
	ifp->if_init = cmalo_init;
	ifp->if_watchdog = cmalo_watchdog;
	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
	IFQ_SET_READY(&ifp->if_snd);

	ic->ic_ifp = ifp;
	ic->ic_phytype = IEEE80211_T_OFDM;
	ic->ic_opmode = IEEE80211_M_STA;
	ic->ic_state = IEEE80211_S_INIT;
	ic->ic_caps = IEEE80211_C_MONITOR | IEEE80211_C_WEP;

	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;

	for (i = 0; i <= 14; i++) {
		ic->ic_channels[i].ic_freq =
		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
		ic->ic_channels[i].ic_flags =
		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
	}

	/* attach interface */
	if_initialize(ifp);
	ieee80211_ifattach(ic);
	/* Use common softint-based if_input */
	ifp->if_percpuq = if_percpuq_create(ifp);
	if_register(ifp);

	sc->sc_newstate = ic->ic_newstate;
	ic->ic_newstate = cmalo_newstate;
	ieee80211_media_init(ic, cmalo_media_change, ieee80211_media_status);

	/* second attach line */
	aprint_normal_dev(sc->sc_dev, "address %s\n",
	    ether_sprintf(ic->ic_myaddr));

	ieee80211_announce(ic);

	/* device attached */
	sc->sc_flags |= MALO_DEVICE_ATTACHED;

	return;

fail:
	cmalo_fw_free(sc);
}

static void
cmalo_detach(void *arg)
{
	struct malo_softc *sc = arg;
	struct ieee80211com *ic = &sc->sc_ic;
	struct ifnet *ifp = &sc->sc_if;

	if (!(sc->sc_flags & MALO_DEVICE_ATTACHED)) {
		/* free firmware */
		cmalo_fw_free(sc);

		/* device was not properly attached */
		return;
	}

	if (ifp->if_flags & IFF_RUNNING)
		cmalo_stop(sc);

	/* free command buffer */
	if (sc->sc_cmd != NULL)
		free(sc->sc_cmd, M_DEVBUF);

	/* free data buffer */
	if (sc->sc_data != NULL)
		free(sc->sc_data, M_DEVBUF);

	/* free firmware */
	cmalo_fw_free(sc);

	/* detach inferface */
	ieee80211_ifdetach(ic);
	if_detach(ifp);

	mutex_destroy(&sc->sc_mtx);
	cv_destroy(&sc->sc_cv);
}

static int
cmalo_intr(void *arg)
{
	struct malo_softc *sc = arg;
	uint16_t intr;

	/* read interrupt reason */
	intr = MALO_READ_2(sc, MALO_REG_HOST_INTR_CAUSE);
	if (intr == 0)
		/* interrupt not for us */
		return 0;
	if (intr == 0xffff)
		/* card has been detached */
		return 0;

	/* disable interrupts */
	cmalo_intr_mask(sc, 0);

	DPRINTF(2, "%s: interrupt handler called (intr = 0x%04x)\n",
	    device_xname(sc->sc_dev), intr);

	softint_schedule(sc->sc_soft_ih);
	return 1;
}

static void
cmalo_softintr(void *arg)
{
	struct malo_softc *sc = arg;
	uint16_t intr;

	/* read interrupt reason */
	intr = MALO_READ_2(sc, MALO_REG_HOST_INTR_CAUSE);
	if (intr == 0 || intr == 0xffff)
		goto out;

	/* acknowledge interrupt */
	MALO_WRITE_2(sc, MALO_REG_HOST_INTR_CAUSE,
	    intr & MALO_VAL_HOST_INTR_MASK_ON);

	if (intr & MALO_VAL_HOST_INTR_TX)
		/* TX frame sent */
		cmalo_tx_done(sc);
	if (intr & MALO_VAL_HOST_INTR_RX)
		/* RX frame received */
		cmalo_rx(sc);
	if (intr & MALO_VAL_HOST_INTR_CMD) {
		/* command response */
		mutex_enter(&sc->sc_mtx);
		cv_signal(&sc->sc_cv);
		mutex_exit(&sc->sc_mtx);
		if (!sc->sc_cmd_ctxsave)
			cmalo_cmd_response(sc);
	}
	if (intr & MALO_VAL_HOST_INTR_EVENT)
		/* event */
		cmalo_event(sc);

 out:
	/* enable interrupts */
	cmalo_intr_mask(sc, 1);
}


/*
 * Network functions
 */
static void
cmalo_start(struct ifnet *ifp)
{
	struct malo_softc *sc = ifp->if_softc;
	struct mbuf *m;

	/* don't transmit packets if interface is busy or down */
	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
		return;

	IFQ_POLL(&ifp->if_snd, m);
	if (m == NULL)
		return;

	IFQ_DEQUEUE(&ifp->if_snd, m);

	bpf_mtap(ifp, m, BPF_D_OUT);

	if (cmalo_tx(sc, m) != 0)
		if_statinc(ifp, if_oerrors);
}

static int
cmalo_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct malo_softc *sc = ifp->if_softc;
	struct ieee80211com *ic = &sc->sc_ic;
	int s, error = 0;

	s = splnet();

	switch (cmd) {
	case SIOCSIFFLAGS:
		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
			break;
		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
		case IFF_RUNNING:
			cmalo_stop(sc);
			break;

		case IFF_UP:
			cmalo_init(ifp);
			break;

		default:
			break;
		}
		error = 0;
		break;

	case SIOCADDMULTI:
	case SIOCDELMULTI:
		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET)
			/* setup multicast filter, etc */
			error = 0;
		break;

	default:
		error = ieee80211_ioctl(ic, cmd, data);
		break;
	}

	if (error == ENETRESET) {
		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
		    (IFF_UP | IFF_RUNNING))
			cmalo_init(ifp);
		error = 0;
	}

	splx(s);

	return error;
}

static int
cmalo_init(struct ifnet *ifp)
{
	struct malo_softc *sc = ifp->if_softc;
	struct ieee80211com *ic = &sc->sc_ic;

	if (!(sc->sc_flags & MALO_DEVICE_ENABLED))
		malo_pcmcia_enable(sc);

	/* reload the firmware if necessary */
	if (!(sc->sc_flags & MALO_FW_LOADED)) {
		/* disable interrupts */
		cmalo_intr_mask(sc, 0);

		/* load firmware */
		if (cmalo_fw_load_helper(sc) != 0)
			return EIO;
		if (cmalo_fw_load_main(sc) != 0)
			return EIO;
		sc->sc_flags |= MALO_FW_LOADED;

		/* enable interrupts */
		cmalo_intr_mask(sc, 1);
	}

	if (ifp->if_flags & IFF_RUNNING)
		cmalo_stop(sc);

	/* reset association state flag */
	sc->sc_flags &= ~MALO_ASSOC_FAILED;

	/* get current channel */
	ic->ic_curchan = ic->ic_ibss_chan;
	sc->sc_curchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
	DPRINTF(1, "%s: current channel is %d\n",
	    device_xname(sc->sc_dev), sc->sc_curchan);

	/* setup device */
	if (cmalo_cmd_set_macctrl(sc) != 0)
		return EIO;
	if (cmalo_cmd_set_txpower(sc, 15) != 0)
		return EIO;
	if (cmalo_cmd_set_antenna(sc, 1) != 0)
		return EIO;
	if (cmalo_cmd_set_antenna(sc, 2) != 0)
		return EIO;
	if (cmalo_cmd_set_radio(sc, 1) != 0)
		return EIO;
	if (cmalo_cmd_set_channel(sc, sc->sc_curchan) != 0)
		return EIO;
	if (cmalo_cmd_set_rate(sc, ic->ic_fixed_rate) != 0)
		return EIO;
	if (cmalo_cmd_set_snmp(sc, MALO_OID_RTSTRESH) != 0)
		return EIO;
	if (cmalo_cmd_set_snmp(sc, MALO_OID_SHORTRETRY) != 0)
		return EIO;
	if (cmalo_cmd_set_snmp(sc, MALO_OID_FRAGTRESH) != 0)
		return EIO;
	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
	if (cmalo_cmd_set_macaddr(sc, ic->ic_myaddr) != 0)
		return EIO;
	if (ic->ic_flags & IEEE80211_F_PRIVACY)
		if (cmalo_wep(sc) != 0)
			return EIO;

	/* device up */
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;

	/* start network */
	if (ic->ic_opmode != IEEE80211_M_MONITOR)
		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
	if (sc->sc_flags & MALO_ASSOC_FAILED)
		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
	else
		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);

	/* we are not context save anymore for FW commands */
	sc->sc_cmd_ctxsave = 0;

	return 0;
}

static void
cmalo_watchdog(struct ifnet *ifp)
{
	DPRINTF(2, "watchdog timeout\n");

	/* accept TX packets again */
	ifp->if_flags &= ~IFF_OACTIVE;
}

static int
cmalo_media_change(struct ifnet *ifp)
{
	int error;

	if ((error = ieee80211_media_change(ifp)) != ENETRESET)
		return error;

	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
		cmalo_init(ifp);

	return 0;
}

static int
cmalo_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
{
	struct malo_softc *sc = ic->ic_ifp->if_softc;
	enum ieee80211_state ostate;

	ostate = ic->ic_state;

	if (ostate == nstate)
		goto out;

	switch (nstate) {
		case IEEE80211_S_INIT:
			DPRINTF(1, "%s: newstate is IEEE80211_S_INIT\n",
			    device_xname(sc->sc_dev));
			break;
		case IEEE80211_S_SCAN:
			DPRINTF(1, "%s: newstate is IEEE80211_S_SCAN\n",
			    device_xname(sc->sc_dev));
			cmalo_cmd_set_scan(sc);
			if (!sc->sc_net_num) {
				/* no networks found */
				DPRINTF(1, "%s: no networks found\n",
				    device_xname(sc->sc_dev));
				break;
			}
			cmalo_select_network(sc);
			cmalo_cmd_set_auth(sc);
			cmalo_cmd_set_assoc(sc);
			break;
		case IEEE80211_S_AUTH:
			DPRINTF(1, "%s: newstate is IEEE80211_S_AUTH\n",
			    device_xname(sc->sc_dev));
			break;
		case IEEE80211_S_ASSOC:
			DPRINTF(1, "%s: newstate is IEEE80211_S_ASSOC\n",
			    device_xname(sc->sc_dev));
			break;
		case IEEE80211_S_RUN:
			DPRINTF(1, "%s: newstate is IEEE80211_S_RUN\n",
			    device_xname(sc->sc_dev));
			cmalo_reflect_network(sc);
			break;
		default:
			break;
	}

out:
	return sc->sc_newstate(ic, nstate, arg);
}


static int
firmware_load(const char *dname, const char *iname, uint8_t **ucodep,
	      size_t *sizep)
{
	firmware_handle_t fh;
	int error;

	if ((error = firmware_open(dname, iname, &fh)) != 0)
		return error;
	*sizep = firmware_get_size(fh);
	if ((*ucodep = firmware_malloc(*sizep)) == NULL) {
		firmware_close(fh);
		return ENOMEM;
	}
	if ((error = firmware_read(fh, 0, *ucodep, *sizep)) != 0)
		firmware_free(*ucodep, *sizep);
	firmware_close(fh);

	return error;
}

static int
cmalo_fw_alloc(struct malo_softc *sc)
{
	const char *name_h = "malo8385-h";
	const char *name_m = "malo8385-m";
	int error;

	if (sc->sc_fw_h == NULL) {
		/* read helper firmware image */
		error = firmware_load("malo", name_h, &sc->sc_fw_h,
		    &sc->sc_fw_h_size);
		if (error != 0) {
			aprint_error_dev(sc->sc_dev,
			    "error %d, could not read firmware %s\n",
			    error, name_h);
			return EIO;
		}
	}

	if (sc->sc_fw_m == NULL) {
		/* read main firmware image */
		error = firmware_load("malo", name_m, &sc->sc_fw_m,
		    &sc->sc_fw_m_size);
		if (error != 0) {
			aprint_error_dev(sc->sc_dev,
			    "error %d, could not read firmware %s\n",
			    error, name_m);
			return EIO;
		}
	}

	return 0;
}

static void
cmalo_fw_free(struct malo_softc *sc)
{

	if (sc->sc_fw_h != NULL) {
		firmware_free(sc->sc_fw_h, sc->sc_fw_h_size);
		sc->sc_fw_h = NULL;
	}

	if (sc->sc_fw_m != NULL) {
		firmware_free(sc->sc_fw_m, sc->sc_fw_m_size);
		sc->sc_fw_m = NULL;
	}
}

static int
cmalo_fw_load_helper(struct malo_softc *sc)
{
	uint8_t val8;
	uint16_t bsize, *uc;
	int offset, i;

	/* verify if the card is ready for firmware download */
	val8 = MALO_READ_1(sc, MALO_REG_SCRATCH);
	if (val8 == MALO_VAL_SCRATCH_FW_LOADED)
		/* firmware already loaded */
		return 0;
	if (val8 != MALO_VAL_SCRATCH_READY) {
		/* bad register value */
		aprint_error_dev(sc->sc_dev,
		    "device not ready for FW download\n");
		return EIO;
	}

	/* download the helper firmware */
	for (offset = 0; offset < sc->sc_fw_h_size; offset += bsize) {
		if (sc->sc_fw_h_size - offset >= MALO_FW_HELPER_BSIZE)
			bsize = MALO_FW_HELPER_BSIZE;
		else
			bsize = sc->sc_fw_h_size - offset;

		/* send a block in words and confirm it */
		DPRINTF(3, "%s: download helper FW block (%d bytes, %d off)\n",
		    device_xname(sc->sc_dev), bsize, offset);
		MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, bsize);
		uc = (uint16_t *)(sc->sc_fw_h + offset);
		for (i = 0; i < bsize / 2; i++)
			MALO_WRITE_2(sc, MALO_REG_CMD_WRITE, htole16(uc[i]));
		MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
		MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE,
		    MALO_VAL_CMD_DL_OVER);

		/* poll for an acknowledgement */
		for (i = 0; i < 50; i++) {
			if (MALO_READ_1(sc, MALO_REG_CARD_STATUS) ==
			    MALO_VAL_CMD_DL_OVER)
				break;
			delay(1000);
		}
		if (i == 50) {
			aprint_error_dev(sc->sc_dev,
			    "timeout while helper FW block download\n");
			return EIO;
		}
	}

	/* helper firmware download done */
	MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, 0);
	MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
	MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_CMD_DL_OVER);
	DPRINTF(1, "%s: helper FW downloaded\n", device_xname(sc->sc_dev));

	return 0;
}

static int
cmalo_fw_load_main(struct malo_softc *sc)
{
	uint16_t val16, bsize = 0, *uc;
	int offset, i, retry = 0;

	/* verify if the helper firmware has been loaded correctly */
	for (i = 0; i < 10; i++) {
		if (MALO_READ_1(sc, MALO_REG_RBAL) == MALO_FW_HELPER_LOADED)
			break;
		delay(1000);
	}
	if (i == 10) {
		aprint_error_dev(sc->sc_dev, "helper FW not loaded\n");
		return EIO;
	}
	DPRINTF(1, "%s: helper FW loaded successfully\n",
	    device_xname(sc->sc_dev));

	/* download the main firmware */
	for (offset = 0; offset < sc->sc_fw_m_size; offset += bsize) {
		val16 = MALO_READ_2(sc, MALO_REG_RBAL);
		/*
		 * If the helper firmware serves us an odd integer then
		 * something went wrong and we retry to download the last
		 * block until we receive a good integer again, or give up.
		 */
		if (val16 & 0x0001) {
			if (retry > MALO_FW_MAIN_MAXRETRY) {
				aprint_error_dev(sc->sc_dev,
				    "main FW download failed\n");
				return EIO;
			}
			retry++;
			offset -= bsize;
		} else {
			retry = 0;
			bsize = val16;
		}

		/* send a block in words and confirm it */
		DPRINTF(3, "%s: download main FW block (%d bytes, %d off)\n",
		    device_xname(sc->sc_dev), bsize, offset);
		MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, bsize);
		uc = (uint16_t *)(sc->sc_fw_m + offset);
		for (i = 0; i < bsize / 2; i++)
			MALO_WRITE_2(sc, MALO_REG_CMD_WRITE, htole16(uc[i]));
		MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
		MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE,
		    MALO_VAL_CMD_DL_OVER);

		/* poll for an acknowledgement */
		for (i = 0; i < 5000; i++) {
			if (MALO_READ_1(sc, MALO_REG_CARD_STATUS) ==
			    MALO_VAL_CMD_DL_OVER)
				break;
		}
		if (i == 5000) {
			aprint_error_dev(sc->sc_dev,
			    "timeout while main FW block download\n");
			return EIO;
		}
	}

	DPRINTF(1, "%s: main FW downloaded\n", device_xname(sc->sc_dev));

	/* verify if the main firmware has been loaded correctly */
	for (i = 0; i < 500; i++) {
		if (MALO_READ_1(sc, MALO_REG_SCRATCH) ==
		    MALO_VAL_SCRATCH_FW_LOADED)
			break;
		delay(1000);
	}
	if (i == 500) {
		aprint_error_dev(sc->sc_dev, "main FW not loaded\n");
		return EIO;
	}

	DPRINTF(1, "%s: main FW loaded successfully\n",
	    device_xname(sc->sc_dev));

	return 0;
}

static void
cmalo_stop(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct ifnet *ifp = &sc->sc_if;

	/* device down */
	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);

	/* change device back to initial state */
	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);

	/* reset device */
	cmalo_cmd_set_reset(sc);
	sc->sc_flags &= ~MALO_FW_LOADED;

	if (sc->sc_flags & MALO_DEVICE_ENABLED)
		malo_pcmcia_disable(sc);

	DPRINTF(1, "%s: device down\n", device_xname(sc->sc_dev));
}

static void
cmalo_intr_mask(struct malo_softc *sc, int enable)
{
	uint16_t val16;

	val16 = MALO_READ_2(sc, MALO_REG_HOST_INTR_MASK);

	DPRINTF(3, "%s: intr mask changed from 0x%04x ",
	    device_xname(sc->sc_dev), val16);

	if (enable)
		MALO_WRITE_2(sc, MALO_REG_HOST_INTR_MASK,
		    val16 & ~MALO_VAL_HOST_INTR_MASK_ON);
	else
		MALO_WRITE_2(sc, MALO_REG_HOST_INTR_MASK,
		    val16 | MALO_VAL_HOST_INTR_MASK_ON);

	val16 = MALO_READ_2(sc, MALO_REG_HOST_INTR_MASK);

	DPRINTF(3, "to 0x%04x\n", val16);
}

static void
cmalo_rx(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct ifnet *ifp = &sc->sc_if;
	struct malo_rx_desc *rxdesc;
	struct mbuf *m;
	uint8_t *data;
	uint16_t psize;
	int i;

	/* read the whole RX packet which is always 802.3 */
	psize = MALO_READ_2(sc, MALO_REG_DATA_READ_LEN);
	if (psize > MALO_DATA_BUFFER_SIZE) {
		aprint_error_dev(sc->sc_dev,
		    "received data too large: %dbyte\n", psize);
		return;
	}

	MALO_READ_MULTI_2(sc, MALO_REG_DATA_READ,
	    (uint16_t *)sc->sc_data, psize / sizeof(uint16_t));
	if (psize & 0x0001)
		sc->sc_data[psize - 1] = MALO_READ_1(sc, MALO_REG_DATA_READ);
	MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_RX_DL_OVER);
	MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_RX_DL_OVER);

	/* access RX packet descriptor */
	rxdesc = (struct malo_rx_desc *)sc->sc_data;
	rxdesc->status = le16toh(rxdesc->status);
	rxdesc->pkglen = le16toh(rxdesc->pkglen);
	rxdesc->pkgoffset = le32toh(rxdesc->pkgoffset);

	DPRINTF(2, "RX status=%d, pkglen=%d, pkgoffset=%d\n",
	    rxdesc->status, rxdesc->pkglen, rxdesc->pkgoffset);

	if (rxdesc->status != MALO_RX_STATUS_OK)
		/* RX packet is not OK */
		return;

	/* remove the LLC / SNAP header */
	data = sc->sc_data + rxdesc->pkgoffset;
	i = (ETHER_ADDR_LEN * 2) + sizeof(struct llc);
	memcpy(data + (ETHER_ADDR_LEN * 2), data + i, rxdesc->pkglen - i);
	rxdesc->pkglen -= sizeof(struct llc);

	/* prepare mbuf */
	m = m_devget(sc->sc_data + rxdesc->pkgoffset,
	    rxdesc->pkglen, ETHER_ALIGN, ifp);
	if (m == NULL) {
		DPRINTF(1, "RX m_devget failed\n");
		if_statinc(ifp, if_ierrors);
		return;
	}

	/* push the frame up to the network stack if not in monitor mode */
	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
		if_percpuq_enqueue(ifp->if_percpuq, m);
	} else {
		/* XXX: we don't do anything with it? */
		m_freem(m);
	}
}

static int
cmalo_tx(struct malo_softc *sc, struct mbuf *m)
{
	struct ifnet *ifp = &sc->sc_if;
	struct malo_tx_desc *txdesc = (struct malo_tx_desc *)sc->sc_data;
	uint8_t *data;
	uint16_t psize;

	memset(sc->sc_data, 0, sizeof(*txdesc));
	psize = sizeof(*txdesc) + m->m_pkthdr.len;
	data = mtod(m, uint8_t *);

	/* prepare TX descriptor */
	txdesc->pkgoffset = htole32(sizeof(*txdesc));
	txdesc->pkglen = htole16(m->m_pkthdr.len);
	memcpy(txdesc->dstaddr, data, ETHER_ADDR_LEN);

	/* copy mbuf data to the buffer */
	m_copydata(m, 0, m->m_pkthdr.len, sc->sc_data + sizeof(*txdesc));
	m_freem(m);

	/* send TX packet to the device */
	MALO_WRITE_2(sc, MALO_REG_DATA_WRITE_LEN, psize);
	MALO_WRITE_MULTI_2(sc, MALO_REG_DATA_WRITE,
	    (uint16_t *)sc->sc_data, psize / sizeof(uint16_t));
	if (psize & 0x0001) {
		data = sc->sc_data;
		MALO_WRITE_1(sc, MALO_REG_DATA_WRITE, data[psize - 1]);
	}
	MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_TX_DL_OVER);
	MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_TX_DL_OVER);

	ifp->if_flags |= IFF_OACTIVE;
	ifp->if_timer = 5;

	DPRINTF(2, "%s: TX status=%d, pkglen=%d, pkgoffset=%zd\n",
	    device_xname(sc->sc_dev), txdesc->status, le16toh(txdesc->pkglen),
	    sizeof(*txdesc));

	return 0;
}

static void
cmalo_tx_done(struct malo_softc *sc)
{
	struct ifnet *ifp = &sc->sc_if;
	int s;

	DPRINTF(2, "%s: TX done\n", device_xname(sc->sc_dev));

	s = splnet();
	if_statinc(ifp, if_opackets);
	ifp->if_flags &= ~IFF_OACTIVE;
	ifp->if_timer = 0;
	cmalo_start(ifp);
	splx(s);
}

static void
cmalo_event(struct malo_softc *sc)
{
	uint16_t event;

	/* read event reason */
	event = MALO_READ_2(sc, MALO_REG_CARD_STATUS);
	event &= MALO_VAL_CARD_STATUS_MASK;
	event = event >> 8;

	switch (event) {
	case MALO_EVENT_DEAUTH:
		DPRINTF(1, "%s: got deauthentication event (0x%04x)\n",
		    device_xname(sc->sc_dev), event);
		/* try to associate again */
		cmalo_cmd_set_assoc(sc);
		break;
	case MALO_EVENT_DISASSOC:
		DPRINTF(1, "%s: got disassociation event (0x%04x)\n",
		    device_xname(sc->sc_dev), event);
		/* try to associate again */
		cmalo_cmd_set_assoc(sc);
		break;
	default:
		DPRINTF(1, "%s: got unknown event (0x%04x)\n",
		    device_xname(sc->sc_dev), event);
		break;
	}

	/* acknowledge event */
	MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_HOST_INTR_EVENT);
}

static void
cmalo_select_network(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	int i, best_rssi;

	/* reset last selected network */
	sc->sc_net_cur = 0;

	/* get desired network */
	if (ic->ic_des_esslen) {
		for (i = 0; i < sc->sc_net_num; i++) {
			if (!strcmp(ic->ic_des_essid, sc->sc_net[i].ssid)) {
				sc->sc_net_cur = i;
				DPRINTF(1, "%s: desired network found (%s)\n",
				    device_xname(sc->sc_dev),
				    ic->ic_des_essid);
				return;
			}
		}
		DPRINTF(1, "%s: desired network not found in scan results "
		    "(%s)\n",
		    device_xname(sc->sc_dev), ic->ic_des_essid);
	}

	/* get network with best signal strength */
	best_rssi = sc->sc_net[0].rssi;
	for (i = 0; i < sc->sc_net_num; i++) {
		if (best_rssi < sc->sc_net[i].rssi) {
			best_rssi = sc->sc_net[i].rssi;
			sc->sc_net_cur = i;
		}
	}
	DPRINTF(1, "%s: best network found (%s)\n",
	    device_xname(sc->sc_dev), sc->sc_net[sc->sc_net_cur].ssid);
}

static void
cmalo_reflect_network(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	uint8_t chan;

	/* reflect active network to our 80211 stack */

	/* BSSID */
	IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid,
	    sc->sc_net[sc->sc_net_cur].bssid);

	/* SSID */
	ic->ic_bss->ni_esslen = strlen(sc->sc_net[sc->sc_net_cur].ssid);
	memcpy(ic->ic_bss->ni_essid, sc->sc_net[sc->sc_net_cur].ssid,
	    ic->ic_bss->ni_esslen);

	/* channel */
	chan = sc->sc_net[sc->sc_net_cur].channel;
	ic->ic_curchan = &ic->ic_channels[chan];
}

static int
cmalo_wep(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	int i;

	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
		struct ieee80211_key *key = &ic->ic_crypto.cs_nw_keys[i];

		if (!key->wk_keylen)
			continue;

		DPRINTF(1, "%s: setting wep key for index %d\n",
		    device_xname(sc->sc_dev), i);

		cmalo_cmd_set_wep(sc, i, key);
	}

	return 0;
}

static int
cmalo_rate2bitmap(int rate)
{
	switch (rate) {
	/* CCK rates */
	case  0:	return MALO_RATE_BITMAP_DS1;
	case  1:	return MALO_RATE_BITMAP_DS2;
	case  2:	return MALO_RATE_BITMAP_DS5;
	case  3:	return MALO_RATE_BITMAP_DS11;

	/* OFDM rates */
	case  4:	return MALO_RATE_BITMAP_OFDM6;
	case  5:	return MALO_RATE_BITMAP_OFDM9;
	case  6:	return MALO_RATE_BITMAP_OFDM12;
	case  7:	return MALO_RATE_BITMAP_OFDM18;
	case  8:	return MALO_RATE_BITMAP_OFDM24;
	case  9:	return MALO_RATE_BITMAP_OFDM36;
	case 10:	return MALO_RATE_BITMAP_OFDM48;
	case 11:	return MALO_RATE_BITMAP_OFDM54;

	/* unknown rate: should not happen */
	default:	return 0;
	}
}

static void
cmalo_hexdump(void *buf, int len)
{
#ifdef CMALO_DEBUG
	int i;

	if (cmalo_d >= 2) {
		for (i = 0; i < len; i++) {
			if (i % 16 == 0)
				printf("%s%5i:", i ? "\n" : "", i);
			if (i % 4 == 0)
				printf(" ");
			printf("%02x", (int)*((u_char *)buf + i));
		}
		printf("\n");
	}
#endif
}

static int
cmalo_cmd_get_hwspec(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_spec *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_HWSPEC);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_spec *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	/* set all bits for MAC address, otherwise we won't get one back */
	memset(body->macaddr, 0xff, ETHER_ADDR_LEN);

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_rsp_hwspec(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
	struct malo_cmd_body_spec *body;

	body = (struct malo_cmd_body_spec *)(hdr + 1);

	/* get our MAC address */
	memcpy(ic->ic_myaddr, body->macaddr, ETHER_ADDR_LEN);

	return 0;
}

static int
cmalo_cmd_set_reset(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
	const uint16_t psize = sizeof(*hdr);

	hdr->cmd = htole16(MALO_CMD_RESET);
	hdr->size = 0;
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 1) != 0)
		return EIO;

	/* give the device some time to finish the reset */
	delay(100);

	return 0;
}

static int
cmalo_cmd_set_scan(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_scan *body;
	struct malo_cmd_tlv_ssid *body_ssid;
	struct malo_cmd_tlv_chanlist *body_chanlist;
	struct malo_cmd_tlv_rates *body_rates;
	uint16_t psize;
	int i;

	psize = sizeof(*hdr) + sizeof(*body) +
	    sizeof(*body_ssid) + sizeof(*body_chanlist) + sizeof(*body_rates);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_SCAN);
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_scan *)(hdr + 1);
	body->bsstype = 0x03; /* any BSS */
	memset(body->bssid, 0xff, ETHER_ADDR_LEN);

	body_ssid = (struct malo_cmd_tlv_ssid *)(body + 1);
	body_ssid->type = htole16(MALO_TLV_TYPE_SSID);
	body_ssid->size = htole16(0);

	body_chanlist = (struct malo_cmd_tlv_chanlist *)(body_ssid + 1);
	body_chanlist->type = htole16(MALO_TLV_TYPE_CHANLIST);
	body_chanlist->size = htole16(sizeof(body_chanlist->data));
	for (i = 0; i < CHANNELS; i++) {
		body_chanlist->data[i].radiotype = 0x00;
		body_chanlist->data[i].channumber = (i + 1);
		body_chanlist->data[i].scantype = 0x00; /* active */
		body_chanlist->data[i].minscantime = htole16(0);
		body_chanlist->data[i].maxscantime = htole16(100);
	}

	body_rates = (struct malo_cmd_tlv_rates *)(body_chanlist + 1);
	body_rates->type = htole16(MALO_TLV_TYPE_RATES);
	body_rates->size =
	    htole16(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates);
	memcpy(body_rates->data, ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates,
	    ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates);
	psize += le16toh(body_rates->size);

	memset((char *)(body_rates + 1) + le16toh(body_rates->size), 0,
	    sizeof(struct malo_cmd_tlv_numprobes));

	hdr->size = htole16(psize - sizeof(*hdr));

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_rsp_scan(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
	struct malo_cmd_body_rsp_scan *body;
	struct malo_cmd_body_rsp_scan_set *set;
	int i;

	memset(sc->sc_net, 0, sizeof(sc->sc_net));

	body = (struct malo_cmd_body_rsp_scan *)(hdr + 1);
	body->bufsize = le16toh(body->bufsize);

	DPRINTF(1, "bufsize=%d, APs=%d\n", body->bufsize, body->numofset);
	sc->sc_net_num = body->numofset;

	set = (struct malo_cmd_body_rsp_scan_set *)(body + 1);

	/* cycle through found networks */
	for (i = 0; i < body->numofset; i++) {
		set->size = le16toh(set->size);
		set->beaconintvl = le16toh(set->beaconintvl);
		set->capinfo = le16toh(set->capinfo);

		DPRINTF(1, "size=%d, bssid=%s, rssi=%d, beaconintvl=%d, "
		    "capinfo=0x%04x\n",
		    set->size, ether_sprintf(set->bssid), set->rssi,
		    set->beaconintvl, set->capinfo);

		/* save scan results */
		memcpy(sc->sc_net[i].bssid, set->bssid, sizeof(set->bssid));
		sc->sc_net[i].rssi = set->rssi;
		memcpy(sc->sc_net[i].timestamp, set->timestamp,
		    sizeof(set->timestamp));
		sc->sc_net[i].beaconintvl = set->beaconintvl;
		sc->sc_net[i].capinfo = set->capinfo;

		cmalo_parse_elements(sc, set->data,
		    set->size - (sizeof(*set) - sizeof(set->size)), i);

		set = (struct malo_cmd_body_rsp_scan_set *)
				((char *)set + sizeof(set->size) + set->size);
	}

	return 0;
}

static int
cmalo_parse_elements(struct malo_softc *sc, uint8_t *buf, int size, int pos)
{
	uint8_t eid, len;
	int i;

	DPRINTF(2, "element_size=%d, element_pos=%d\n", size, pos);

	for (i = 0; i < size; ) {
		eid = *(uint8_t *)(buf + i);
		i++;
		len = *(uint8_t *)(buf + i);
		i++;
		DPRINTF(2, "eid=%d, len=%d, ", eid, len);

		switch (eid) {
		case IEEE80211_ELEMID_SSID:
			memcpy(sc->sc_net[pos].ssid, buf + i, len);
			DPRINTF(2, "ssid=%s\n", sc->sc_net[pos].ssid);
			break;
		case IEEE80211_ELEMID_RATES:
			memcpy(sc->sc_net[pos].rates, buf + i, len);
			DPRINTF(2, "rates\n");
			break;
		case IEEE80211_ELEMID_DSPARMS:
			sc->sc_net[pos].channel = *(uint8_t *)(buf + i);
			DPRINTF(2, "chnl=%d\n", sc->sc_net[pos].channel);
			break;
		default:
			DPRINTF(2, "unknown\n");
			break;
		}

		i += len;
	}

	return 0;
}

static int
cmalo_cmd_set_auth(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_auth *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_AUTH);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_auth *)(hdr + 1);
	memcpy(body->peermac, sc->sc_net[sc->sc_net_cur].bssid, ETHER_ADDR_LEN);
	body->authtype = 0;

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_wep(struct malo_softc *sc, uint16_t index,
		  struct ieee80211_key *key)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_wep *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_WEP);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_wep *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	body->action = htole16(MALO_WEP_ACTION_TYPE_ADD);
	body->key_index = htole16(index);

	if (body->key_index == 0) {
		if (key->wk_keylen > 5)
			body->key_type_1 = MALO_WEP_KEY_TYPE_104BIT;
		else
			body->key_type_1 = MALO_WEP_KEY_TYPE_40BIT;
		memcpy(body->key_value_1, key->wk_key, key->wk_keylen);
	}
	if (body->key_index == 1) {
		if (key->wk_keylen > 5)
			body->key_type_2 = MALO_WEP_KEY_TYPE_104BIT;
		else
			body->key_type_2 = MALO_WEP_KEY_TYPE_40BIT;
		memcpy(body->key_value_2, key->wk_key, key->wk_keylen);
	}
	if (body->key_index == 2) {
		if (key->wk_keylen > 5)
			body->key_type_3 = MALO_WEP_KEY_TYPE_104BIT;
		else
			body->key_type_3 = MALO_WEP_KEY_TYPE_40BIT;
		memcpy(body->key_value_3, key->wk_key, key->wk_keylen);
	}
	if (body->key_index == 3) {
		if (key->wk_keylen > 5)
			body->key_type_4 = MALO_WEP_KEY_TYPE_104BIT;
		else
			body->key_type_4 = MALO_WEP_KEY_TYPE_40BIT;
		memcpy(body->key_value_4, key->wk_key, key->wk_keylen);
	}

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_snmp(struct malo_softc *sc, uint16_t oid)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_snmp *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_SNMP);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_snmp *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	body->action = htole16(1);

	switch (oid) {
	case MALO_OID_RTSTRESH:
		body->oid = htole16(MALO_OID_RTSTRESH);
		body->size = htole16(2);
		*(uint16_t *)body->data = htole16(2347);
		break;
	case MALO_OID_SHORTRETRY:
		body->oid = htole16(MALO_OID_SHORTRETRY);
		body->size = htole16(2);
		*(uint16_t *)body->data = htole16(4);
		break;
	case MALO_OID_FRAGTRESH:
		body->oid = htole16(MALO_OID_FRAGTRESH);
		body->size = htole16(2);
		*(uint16_t *)body->data = htole16(2346);
		break;
	case MALO_OID_80211D:
		body->oid = htole16(MALO_OID_80211D);
		body->size = htole16(2);
		*(uint16_t *)body->data = htole16(1);
		break;
	default:
		break;
	}

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_radio(struct malo_softc *sc, uint16_t control)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_radio *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_RADIO);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_radio *)(hdr + 1);
	body->action = htole16(1);
	if (control)
		body->control =
		    htole16(MALO_CMD_RADIO_ON | MALO_CMD_RADIO_AUTO_P);
	else
		body->control = 0;

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_channel(struct malo_softc *sc, uint16_t channel)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_channel *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_CHANNEL);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_channel *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	body->action = htole16(1);
	body->channel = htole16(channel);

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}


static int
cmalo_cmd_set_txpower(struct malo_softc *sc, int16_t txpower)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_txpower *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_TXPOWER);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_txpower *)(hdr + 1);
	body->action = htole16(1);
	body->txpower = htole16(txpower);

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_antenna(struct malo_softc *sc, uint16_t action)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_antenna *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_ANTENNA);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_antenna *)(hdr + 1);
	/* 1 = set RX, 2 = set TX */
	body->action = htole16(action);

	switch (action) {
	case 1:
		/* set RX antenna */
		body->antenna_mode = htole16(0xffff);
		break;

	case 2:
		/* set TX antenna */
		body->antenna_mode = htole16(2);
		break;

	default:
		body->antenna_mode = 0;
		break;
	}

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_macctrl(struct malo_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_macctrl *body;
	uint16_t psize;

	psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_MACCTRL);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_macctrl *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	body->action = htole16(MALO_CMD_MACCTRL_RX_ON | MALO_CMD_MACCTRL_TX_ON);
	if (ic->ic_opmode == IEEE80211_M_MONITOR)
		body->action |= htole16(MALO_CMD_MACCTRL_PROMISC_ON);

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_macaddr(struct malo_softc *sc, uint8_t *macaddr)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_macaddr *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_MACADDR);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_macaddr *)(hdr + 1);
	body->action = htole16(1);
	memcpy(body->macaddr, macaddr, ETHER_ADDR_LEN);

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_set_assoc(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_assoc *body;
	struct malo_cmd_tlv_ssid *body_ssid;
	struct malo_cmd_tlv_phy *body_phy;
	struct malo_cmd_tlv_cf *body_cf;
	struct malo_cmd_tlv_rates *body_rates;
	struct malo_cmd_tlv_passeid *body_passeid;
	uint16_t psize;

	psize = sizeof(*hdr) + sizeof(*body) + sizeof(*body_ssid) +
	    sizeof(body_phy) + sizeof(*body_cf) + sizeof(*body_rates);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_ASSOC);
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_assoc *)(hdr + 1);
	memset(body, 0, sizeof(*body));
	memcpy(body->peermac, sc->sc_net[sc->sc_net_cur].bssid, ETHER_ADDR_LEN);
	body->capinfo = htole16(sc->sc_net[sc->sc_net_cur].capinfo);
	body->listenintrv = htole16(10);

	body_ssid = (struct malo_cmd_tlv_ssid *)(body + 1);
	body_ssid->type = htole16(MALO_TLV_TYPE_SSID);
	body_ssid->size = htole16(strlen(sc->sc_net[sc->sc_net_cur].ssid));
	memcpy(body_ssid->data, sc->sc_net[sc->sc_net_cur].ssid,
	    le16toh(body_ssid->size));
	psize += le16toh(body_ssid->size);

	body_phy = (struct malo_cmd_tlv_phy *)
			((char *)(body_ssid + 1) + le16toh(body_ssid->size));
	body_phy->type = htole16(MALO_TLV_TYPE_PHY);
	body_phy->size = htole16(1);
	body_phy->data[0] = sc->sc_net[sc->sc_net_cur].channel;
	psize += le16toh(body_phy->size);

	body_cf = (struct malo_cmd_tlv_cf *)
			((char *)(body_phy + 1) + le16toh(body_phy->size));
	body_cf->type = htole16(MALO_TLV_TYPE_CF);
	body_cf->size = htole16(0);

	body_rates = (struct malo_cmd_tlv_rates *)(body_cf + 1);
	body_rates->type = htole16(MALO_TLV_TYPE_RATES);
	body_rates->size = htole16(strlen(sc->sc_net[sc->sc_net_cur].rates));
	memcpy(body_rates->data, sc->sc_net[sc->sc_net_cur].rates,
	    le16toh(body_rates->size));
	psize += le16toh(body_rates->size);

	/* hack to correct FW's wrong generated rates-element-id */
	body_passeid = (struct malo_cmd_tlv_passeid *)
			((char *)(body_rates + 1) + le16toh(body_rates->size));
	body_passeid->type = htole16(MALO_TLV_TYPE_PASSEID);
	body_passeid->size = body_rates->size;
	memcpy(body_passeid->data, body_rates->data, le16toh(body_rates->size));
	psize += le16toh(body_passeid->size);

	hdr->size = htole16(psize - sizeof(*hdr));

	/* process command request */
	if (!sc->sc_cmd_ctxsave) {
		if (cmalo_cmd_request(sc, psize, 1) != 0)
			return EIO;
		return 0;
	}
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_rsp_assoc(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
	struct malo_cmd_body_rsp_assoc *body;

	body = (struct malo_cmd_body_rsp_assoc *)(hdr + 1);

	if (body->status) {
		DPRINTF(1, "%s: association failed (status %d)\n",
		    device_xname(sc->sc_dev), body->status);
		sc->sc_flags |= MALO_ASSOC_FAILED;
	} else
		DPRINTF(1, "%s: association successful\n",
		    device_xname(sc->sc_dev));

	return 0;
}

static int
cmalo_cmd_set_rate(struct malo_softc *sc, int rate)
{
	struct malo_cmd_header *hdr;
	struct malo_cmd_body_rate *body;
	const uint16_t psize = sizeof(*hdr) + sizeof(*body);

	hdr = (struct malo_cmd_header *)sc->sc_cmd;
	hdr->cmd = htole16(MALO_CMD_RATE);
	hdr->size = htole16(sizeof(*body));
	hdr->seqnum = htole16(1);
	hdr->result = 0;

	body = (struct malo_cmd_body_rate *)(hdr + 1);
	body->action = htole16(1);
	if (rate == IEEE80211_FIXED_RATE_NONE) {
 		body->hwauto = htole16(1);
		body->ratebitmap = htole16(MALO_RATE_BITMAP_AUTO);
	} else {
 		body->hwauto = 0;
		body->ratebitmap = htole16(cmalo_rate2bitmap(rate));
	}

	/* process command request */
	if (cmalo_cmd_request(sc, psize, 0) != 0)
		return EIO;

	/* process command repsonse */
	cmalo_cmd_response(sc);

	return 0;
}

static int
cmalo_cmd_request(struct malo_softc *sc, uint16_t psize, int no_response)
{
	uint8_t *cmd;

	mutex_enter(&sc->sc_mtx);

	cmalo_hexdump(sc->sc_cmd, psize);

	/* send command request */
	MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, psize);
	MALO_WRITE_MULTI_2(sc, MALO_REG_CMD_WRITE,
	    (uint16_t *)sc->sc_cmd, psize / sizeof(uint16_t));
	if (psize & 0x0001) {
		cmd = sc->sc_cmd;
		MALO_WRITE_1(sc, MALO_REG_CMD_WRITE, cmd[psize - 1]);
	}
	MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
	MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_CMD_DL_OVER);

	if (no_response) {
		mutex_exit(&sc->sc_mtx);

		/* we don't expect a response */
		return 0;
	}

	/* wait for the command response */
	if (cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, 500) == EWOULDBLOCK) {
		mutex_exit(&sc->sc_mtx);
		aprint_error_dev(sc->sc_dev,
		    "timeout while waiting for cmd response\n");
		return EIO;
	}
	mutex_exit(&sc->sc_mtx);

	return 0;
}

static int
cmalo_cmd_response(struct malo_softc *sc)
{
	struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
	uint16_t psize;
	int s;

	s = splnet();

#ifdef CMALO_DEBUG
	memset(sc->sc_cmd, 0, MALO_CMD_BUFFER_SIZE);
#endif

	/* read the whole command response */
	psize = MALO_READ_2(sc, MALO_REG_CMD_READ_LEN);
	if (psize > MALO_CMD_BUFFER_SIZE) {
		aprint_error_dev(sc->sc_dev,
		    "command response too large: %dbyte\n", psize);
		splx(s);
		return EIO;
	}

	MALO_READ_MULTI_2(sc, MALO_REG_CMD_READ,
	    (uint16_t *)sc->sc_cmd, psize / sizeof(uint16_t));
	if (psize & 0x0001)
		sc->sc_cmd[psize - 1] = MALO_READ_1(sc, MALO_REG_CMD_READ);

	cmalo_hexdump(sc->sc_cmd, psize);

	/*
	 * We convert the header values into the machines correct endianness,
	 * so we don't have to le16toh() all over the code.  The body is
	 * kept in the cards order, little endian.  We need to take care
	 * about the body endianness in the corresponding response routines.
	 */
	hdr->cmd = le16toh(hdr->cmd);
	hdr->size = le16toh(hdr->size);
	hdr->seqnum = le16toh(hdr->seqnum);
	hdr->result = le16toh(hdr->result);

	/* check for a valid command response */
	if (!(hdr->cmd & MALO_CMD_RESP)) {
		aprint_error_dev(sc->sc_dev,
		    "got invalid command response (0x%04x)\n", hdr->cmd);
		splx(s);
		return EIO;
	}
	hdr->cmd &= ~MALO_CMD_RESP;

	/* association cmd response is special */
	if (hdr->cmd == 0x0012)
		hdr->cmd = MALO_CMD_ASSOC;

	/* to which command does the response belong */
	switch (hdr->cmd) {
	case MALO_CMD_HWSPEC:
		DPRINTF(1, "%s: got hwspec cmd response\n",
		    device_xname(sc->sc_dev));
		cmalo_cmd_rsp_hwspec(sc);
		break;
	case MALO_CMD_RESET:
		/* reset will not send back a response */
		break;
	case MALO_CMD_SCAN:
		DPRINTF(1, "%s: got scan cmd response\n",
		    device_xname(sc->sc_dev));
		cmalo_cmd_rsp_scan(sc);
		break;
	case MALO_CMD_AUTH:
		/* do nothing */
		DPRINTF(1, "%s: got auth cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_WEP:
		/* do nothing */
		DPRINTF(1, "%s: got wep cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_SNMP:
		/* do nothing */
		DPRINTF(1, "%s: got snmp cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_RADIO:
		/* do nothing */
		DPRINTF(1, "%s: got radio cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_CHANNEL:
		/* do nothing */
		DPRINTF(1, "%s: got channel cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_TXPOWER:
		/* do nothing */
		DPRINTF(1, "%s: got txpower cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_ANTENNA:
		/* do nothing */
		DPRINTF(1, "%s: got antenna cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_MACCTRL:
		/* do nothing */
		DPRINTF(1, "%s: got macctrl cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_MACADDR:
		/* do nothing */
		DPRINTF(1, "%s: got macaddr cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_ASSOC:
		/* do nothing */
		DPRINTF(1, "%s: got assoc cmd response\n",
		    device_xname(sc->sc_dev));
		cmalo_cmd_rsp_assoc(sc);
		break;
	case MALO_CMD_80211D:
		/* do nothing */
		DPRINTF(1, "%s: got 80211d cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_BGSCAN_CONFIG:
		/* do nothing */
		DPRINTF(1, "%s: got bgscan config cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_BGSCAN_QUERY:
		/* do nothing */
		DPRINTF(1, "%s: got bgscan query cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	case MALO_CMD_RATE:
		/* do nothing */
		DPRINTF(1, "%s: got rate cmd response\n",
		    device_xname(sc->sc_dev));
		break;
	default:
		aprint_error_dev(sc->sc_dev,
		    "got unknown cmd response (0x%04x)\n", hdr->cmd);
		break;
	}

	splx(s);

	return 0;
}

#ifdef _MODULE

MODULE(MODULE_CLASS_DRIVER, malo_pcmcia, NULL);

CFDRIVER_DECL(malo_pcmcia, DV_IFNET, NULL);
extern struct cfattach malo_pcmcia_ca;
static int malo_pcmcialoc[] = { -1 };
static struct cfparent pcmciaparent = {
	"pcmcia", NULL, DVUNIT_ANY
};
static struct cfdata malo_pcmcia_cfdata[] = {
	{
		.cf_name = "malo_pcmcia",
		.cf_atname = "malo",
		.cf_unit = 0,
		.cf_fstate = FSTATE_STAR,
		.cf_loc = malo_pcmcialoc,
		.cf_flags = 0,
		.cf_pspec = &pcmciaparent,
	},
	{ NULL }
};

static int
malo_pcmcia_modcmd(modcmd_t cmd, void *arg)
{
	int err;

	switch (cmd) {
	case MODULE_CMD_INIT:
		err = config_cfdriver_attach(&malo_pcmcia_cd);
		if (err)
			return err;
		err = config_cfattach_attach("malo_pcmcia", &malo_pcmcia_ca);
		if (err) {
			config_cfdriver_detach(&malo_pcmcia_cd);
			return err;
		}
		err = config_cfdata_attach(malo_pcmcia_cfdata, 1);
		if (err) {
			config_cfattach_detach("malo_pcmcia", &malo_pcmcia_ca);
			config_cfdriver_detach(&malo_pcmcia_cd);
			return err;
		}
		return 0;
	case MODULE_CMD_FINI:
		err = config_cfdata_detach(malo_pcmcia_cfdata);
		if (err)
			return err;
		config_cfattach_detach("malo_pcmcia", &malo_pcmcia_ca);
		config_cfdriver_detach(&malo_pcmcia_cd);
		return 0;
	default:
		return ENOTTY;
	}
}
#endif