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
|
/* $OpenBSD: if_wi_usb.c,v 1.35 2007/05/21 05:40:27 jsg Exp $ */
/*
* Copyright (c) 2003 Dale Rahn. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
*
* Effort sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F30602-01-2-0537.
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/device.h>
#include <sys/kthread.h>
#include <sys/tree.h>
#include <net/if.h>
#include <net/if_dl.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 <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#define ROUNDUP64(x) (((x)+63) & ~63)
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_ioctl.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <machine/bus.h>
#include <dev/rndvar.h>
#include <dev/ic/if_wireg.h>
#include <dev/ic/if_wi_ieee.h>
#include <dev/ic/if_wivar.h>
#include <dev/usb/if_wi_usb.h>
int wi_usb_do_transmit_sync(struct wi_usb_softc *wsc, struct wi_usb_chain *c,
void *ident);
void wi_usb_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status);
void wi_usb_txeof_frm(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status);
void wi_usb_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status);
void wi_usb_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status);
void wi_usb_stop(struct wi_usb_softc *usc);
int wi_usb_tx_list_init(struct wi_usb_softc *usc);
int wi_usb_rx_list_init(struct wi_usb_softc *usc);
int wi_usb_open_pipes(struct wi_usb_softc *usc);
void wi_usb_cmdresp(struct wi_usb_chain *c);
void wi_usb_rridresp(struct wi_usb_chain *c);
void wi_usb_wridresp(struct wi_usb_chain *c);
void wi_usb_infofrm(struct wi_usb_chain *c, int len);
int wi_send_packet(struct wi_usb_softc *sc, int id);
void wi_usb_rxfrm(struct wi_usb_softc *usc, wi_usb_usbin *uin, int total_len);
void wi_usb_txfrm(struct wi_usb_softc *usc, wi_usb_usbin *uin, int total_len);
void wi_usb_start_thread(void *);
int wi_usb_tx_lock_try(struct wi_usb_softc *sc);
void wi_usb_tx_lock(struct wi_usb_softc *usc);
void wi_usb_tx_unlock(struct wi_usb_softc *usc);
void wi_usb_ctl_lock(struct wi_usb_softc *usc);
void wi_usb_ctl_unlock(struct wi_usb_softc *usc);
void wi_dump_data(void *buffer, int len);
void wi_usb_thread(void *arg);
#ifdef WI_USB_DEBUG
#define DPRINTF(x) do { if (wi_usbdebug) printf x; } while (0)
#define DPRINTFN(n,x) do { if (wi_usbdebug >= (n)) printf x; } while (0)
int wi_usbdebug = 1;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
struct wi_usb_thread_info {
int status;
int dying;
int idle;
};
/* thread status flags */
#define WI_START 0x01
#define WI_DYING 0x02
#define WI_INQUIRE 0x04
#define WI_WATCHDOG 0x08
struct wi_usb_softc {
struct wi_softc sc_wi;
#define wi_usb_dev sc_wi.sc_dev
usb_callout_t wi_usb_stat_ch;
usbd_device_handle wi_usb_udev;
usbd_interface_handle wi_usb_iface;
u_int16_t wi_usb_vendor;
u_int16_t wi_usb_product;
int wi_usb_ed[WI_USB_ENDPT_MAX];
usbd_pipe_handle wi_usb_ep[WI_USB_ENDPT_MAX];
struct wi_usb_chain wi_usb_tx_chain[WI_USB_TX_LIST_CNT];
struct wi_usb_chain wi_usb_rx_chain[WI_USB_RX_LIST_CNT];
int wi_usb_refcnt;
char wi_usb_dying;
char wi_usb_attached;
int wi_usb_intr_errs;
struct timeval wi_usb_rx_notice;
int wi_usb_pollpending;
wi_usb_usbin wi_usb_ibuf;
int wi_usb_tx_prod;
int wi_usb_tx_cons;
int wi_usb_tx_cnt;
int wi_usb_rx_prod;
struct wi_ltv_gen *ridltv;
int ridresperr;
int cmdresp;
int cmdresperr;
int txresp;
int txresperr;
/* nummem (tx/mgmt) */
int wi_usb_nummem;
#define MAX_WI_NMEM 3
void *wi_usb_txmem[MAX_WI_NMEM];
int wi_usb_txmemsize[MAX_WI_NMEM];
void *wi_usb_rxmem;
int wi_usb_rxmemsize;
void *wi_info;
void *wi_rxframe;
/* prevent multiple outstanding USB requests */
int wi_lock;
int wi_lockwait;
/* prevent multiple command requests */
int wi_ctllock;
int wi_ctllockwait;
struct proc *wi_curproc;
/* kthread */
struct wi_usb_thread_info *wi_thread_info;
int wi_resetonce;
};
struct wi_funcs wi_func_usb = {
wi_cmd_usb,
wi_read_record_usb,
wi_write_record_usb,
wi_alloc_nicmem_usb,
wi_read_data_usb,
wi_write_data_usb,
wi_get_fid_usb,
wi_init_usb,
wi_start_usb,
wi_ioctl_usb,
wi_watchdog_usb,
wi_inquire_usb,
};
/*
* Various supported device vendors/products.
*/
const struct wi_usb_type {
struct usb_devno wi_usb_device;
u_int16_t wi_usb_flags;
/* XXX */
} wi_usb_devs[] = {
{{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_111 }, 0 },
{{ USB_VENDOR_ACERW, USB_PRODUCT_ACERW_WARPLINK }, 0 },
{{ USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_FREELAN }, 0 },
{{ USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_PRISM_25 }, 0 },
{{ USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_PRISM_25A }, 0 },
{{ USB_VENDOR_ADAPTEC, USB_PRODUCT_ADAPTEC_AWN8020 }, 0 },
{{ USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_WLAN }, 0 },
{{ USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL140 }, 0 },
{{ USB_VENDOR_AVERATEC, USB_PRODUCT_AVERATEC_USBWLAN }, 0 },
{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_W100 }, 0 },
{{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_W200 }, 0 },
{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_WLANUSB }, 0 },
{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_WLUSB_11_KEY }, 0 },
{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_TM1180 }, 0 },
{{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWL120F }, 0 },
{{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWL122 }, 0 },
{{ USB_VENDOR_INTEL, USB_PRODUCT_INTEL_I2011B }, 0 },
{{ USB_VENDOR_INTERSIL, USB_PRODUCT_INTERSIL_PRISM_2X }, 0 },
{{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBWNB11 }, 0 },
{{ USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_XP7250_WL }, 0 },
{{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_WUSB11_25 }, 0 },
{{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_WUSB12_11 }, 0 },
{{ USB_VENDOR_LINKSYS3, USB_PRODUCT_LINKSYS3_WUSB11V30 }, 0 },
{{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KB11 }, 0 },
{{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KS11G }, 0 },
{{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_S11 }, 0 },
{{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN510 }, 0 },
{{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_MA111NA }, 0 },
{{ USB_VENDOR_PHEENET, USB_PRODUCT_PHEENET_WL503IA }, 0 },
{{ USB_VENDOR_PHEENET, USB_PRODUCT_PHEENET_WM168B }, 0 },
{{ USB_VENDOR_PLANEX, USB_PRODUCT_PLANEX_GW_US11H }, 0 },
{{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM22 }, 0 },
{{ USB_VENDOR_SITECOM2, USB_PRODUCT_SITECOM2_WL022 }, 0 },
{{ USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_0193 }, 0 },
{{ USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_ZYAIR_B200 }, 0 },
{{ USB_VENDOR_USR, USB_PRODUCT_USR_USR1120 }, 0 },
{{ USB_VENDOR_VIEWSONIC, USB_PRODUCT_VIEWSONIC_AIRSYNC }, 0 },
{{ USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XI725 }, 0 },
{{ USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XI735 }, 0 }
};
#define wi_usb_lookup(v, p) ((struct wi_usb_type *)usb_lookup(wi_usb_devs, v, p))
USB_DECLARE_DRIVER_CLASS(wi_usb, DV_IFNET);
USB_MATCH(wi_usb)
{
USB_MATCH_START(wi_usb, uaa);
if (uaa->iface != NULL)
return (UMATCH_NONE);
return (wi_usb_lookup(uaa->vendor, uaa->product) != NULL ?
UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
}
/*
* Attach the interface. Allocate softc structures, do ifmedia
* setup and ethernet/BPF attach.
*/
USB_ATTACH(wi_usb)
{
USB_ATTACH_START(wi_usb, sc, uaa);
char *devinfop;
/* int s; */
usbd_device_handle dev = uaa->device;
usbd_interface_handle iface;
usbd_status err;
usb_interface_descriptor_t *id;
usb_endpoint_descriptor_t *ed;
int i;
DPRINTFN(5,(" : wi_usb_attach: sc=%p", sc));
err = usbd_set_config_no(dev, WI_USB_CONFIG_NO, 1);
if (err) {
printf("%s: setting config no failed\n",
USBDEVNAME(sc->wi_usb_dev));
USB_ATTACH_ERROR_RETURN;
}
devinfop = usbd_devinfo_alloc(dev, 0);
USB_ATTACH_SETUP;
printf("%s: %s\n", USBDEVNAME(sc->wi_usb_dev), devinfop);
usbd_devinfo_free(devinfop);
/* XXX - any tasks? */
err = usbd_device2interface_handle(dev, WI_USB_IFACE_IDX, &iface);
if (err) {
printf("%s: getting interface handle failed\n",
USBDEVNAME(sc->wi_usb_dev));
USB_ATTACH_ERROR_RETURN;
}
/* XXX - flags? */
sc->wi_usb_udev = dev;
sc->wi_usb_iface = iface;
sc->wi_usb_product = uaa->product;
sc->wi_usb_vendor = uaa->vendor;
sc->sc_wi.wi_usb_cdata = sc;
sc->sc_wi.wi_flags |= WI_FLAGS_BUS_USB;
sc->wi_lock = 0;
sc->wi_lockwait = 0;
sc->wi_resetonce = 0;
id = usbd_get_interface_descriptor(iface);
/* Find endpoints. */
for (i = 0; i < id->bNumEndpoints; i++) {
ed = usbd_interface2endpoint_descriptor(iface, i);
if (ed == NULL) {
printf("%s: couldn't get endpoint descriptor %d\n",
USBDEVNAME(sc->wi_usb_dev), i);
USB_ATTACH_ERROR_RETURN;
}
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
sc->wi_usb_ed[WI_USB_ENDPT_RX] = ed->bEndpointAddress;
} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
sc->wi_usb_ed[WI_USB_ENDPT_TX] = ed->bEndpointAddress;
} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
sc->wi_usb_ed[WI_USB_ENDPT_INTR] = ed->bEndpointAddress;
}
}
sc->wi_usb_nummem = 0;
/* attach wi device */
if (wi_usb_rx_list_init(sc)) {
printf("%s: rx list init failed\n",
USBDEVNAME(sc->wi_usb_dev));
USB_ATTACH_ERROR_RETURN;
}
if (wi_usb_tx_list_init(sc)) {
printf("%s: tx list init failed\n",
USBDEVNAME(sc->wi_usb_dev));
USB_ATTACH_ERROR_RETURN;
}
if (wi_usb_open_pipes(sc)){
printf("%s: open pipes failed\n",
USBDEVNAME(sc->wi_usb_dev));
USB_ATTACH_ERROR_RETURN;
}
sc->wi_usb_attached = 1;
kthread_create_deferred(wi_usb_start_thread, sc);
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->wi_usb_udev,
USBDEV(sc->wi_usb_dev));
USB_ATTACH_SUCCESS_RETURN;
}
USB_DETACH(wi_usb)
{
USB_DETACH_START(wi_usb, sc);
struct ifnet *ifp = WI_GET_IFP(sc);
struct wi_softc *wsc = &sc->sc_wi;
int s;
int err;
sc->wi_usb_dying = 1;
if (sc->wi_thread_info != NULL) {
sc->wi_thread_info->dying = 1;
sc->wi_thread_info->status |= WI_DYING;
if (sc->wi_thread_info->idle)
wakeup(sc->wi_thread_info);
}
if (!sc->wi_usb_attached) {
/* Detached before attach finished, so just bail out. */
return (0);
}
/* tasks? */
s = splusb();
/* detatch wi */
if (!(wsc->wi_flags & WI_FLAGS_ATTACHED)) {
printf("%s: already detached\n", USBDEVNAME(sc->wi_usb_dev));
splx(s);
return (0);
}
wi_detach(&sc->sc_wi);
wsc->wi_flags = 0;
ether_ifdetach(ifp);
if_detach(ifp);
sc->wi_usb_attached = 0;
if (--sc->wi_usb_refcnt >= 0) {
/* Wait for processes to go away. */
usb_detach_wait(USBDEV(sc->wi_usb_dev));
}
while (sc->wi_usb_nummem) {
sc->wi_usb_nummem--;
if (sc->wi_usb_txmem[sc->wi_usb_nummem] != NULL)
free(sc->wi_usb_txmem[sc->wi_usb_nummem], M_DEVBUF);
sc->wi_usb_txmem[sc->wi_usb_nummem] = NULL;
}
if (sc->wi_usb_ep[WI_USB_ENDPT_INTR] != NULL) {
err = usbd_abort_pipe(sc->wi_usb_ep[WI_USB_ENDPT_INTR]);
if (err) {
printf("%s: abort intr pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
err = usbd_close_pipe(sc->wi_usb_ep[WI_USB_ENDPT_INTR]);
if (err) {
printf("%s: close intr pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
sc->wi_usb_ep[WI_USB_ENDPT_INTR] = NULL;
}
if (sc->wi_usb_ep[WI_USB_ENDPT_TX] != NULL) {
usbd_abort_pipe(sc->wi_usb_ep[WI_USB_ENDPT_TX]);
if (err) {
printf("%s: abort tx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
err = usbd_close_pipe(sc->wi_usb_ep[WI_USB_ENDPT_TX]);
if (err) {
printf("%s: close tx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
sc->wi_usb_ep[WI_USB_ENDPT_TX] = NULL;
}
if (sc->wi_usb_ep[WI_USB_ENDPT_RX] != NULL) {
usbd_abort_pipe(sc->wi_usb_ep[WI_USB_ENDPT_RX]);
if (err) {
printf("%s: abort rx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
err = usbd_close_pipe(sc->wi_usb_ep[WI_USB_ENDPT_RX]);
if (err) {
printf("%s: close rx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
}
sc->wi_usb_ep[WI_USB_ENDPT_RX] = NULL;
}
splx(s);
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->wi_usb_udev,
USBDEV(sc->wi_usb_dev));
return (0);
}
int
wi_send_packet(struct wi_usb_softc *sc, int id)
{
struct wi_usb_chain *c;
struct wi_frame *wibuf;
int total_len, rnd_len;
int err;
c = &sc->wi_usb_tx_chain[0];
DPRINTFN(10,("%s: %s: id=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, id));
/* assemble packet from write_data buffer */
if (id == 0 || id == 1) {
/* tx_lock acquired before wi_start() */
wibuf = sc->wi_usb_txmem[id];
total_len = sizeof (struct wi_frame) +
letoh16(wibuf->wi_dat_len);
rnd_len = ROUNDUP64(total_len);
if ((total_len > sc->wi_usb_txmemsize[id]) ||
(rnd_len > WI_USB_BUFSZ )){
printf("invalid packet len: %x memsz %x max %x\n",
total_len, sc->wi_usb_txmemsize[id], WI_USB_BUFSZ);
total_len = sc->wi_usb_txmemsize[id];
err = EIO;
goto err_ret;
}
sc->txresp = WI_CMD_TX;
sc->txresperr = 0;
bcopy(wibuf, c->wi_usb_buf, total_len);
bzero(((char *)c->wi_usb_buf)+total_len,
rnd_len - total_len);
/* zero old packet for next TX */
bzero(wibuf, total_len);
total_len = rnd_len;
DPRINTFN(5,("%s: %s: id=%x len=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, id, total_len));
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_TX],
c, c->wi_usb_buf, rnd_len,
USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
WI_USB_TX_TIMEOUT, wi_usb_txeof_frm);
err = usbd_transfer(c->wi_usb_xfer);
if (err != USBD_IN_PROGRESS && err != USBD_NORMAL_COMPLETION) {
printf("%s: %s: error=%s\n",
USBDEVNAME(sc->wi_usb_dev), __func__,
usbd_errstr(err));
/* Stop the interface from process context. */
wi_usb_stop(sc);
err = EIO;
} else {
err = 0;
}
DPRINTFN(5,("%s: %s: exit err=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, err));
err_ret:
return err;
}
printf("%s:%s: invalid packet id sent %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, id);
return 0;
}
int
wi_cmd_usb(struct wi_softc *wsc, int cmd, int val0, int val1, int val2)
{
struct wi_usb_chain *c;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
struct wi_cmdreq *pcmd;
int total_len, rnd_len;
int err;
DPRINTFN(5,("%s: %s: enter cmd=%x %x %x %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, cmd, val0, val1, val2));
if ((cmd & WI_CMD_CODE_MASK) == WI_CMD_TX) {
return wi_send_packet(sc, val0);
}
if ((cmd & WI_CMD_CODE_MASK) == WI_CMD_INI) {
/* free alloc_nicmem regions */
while (sc->wi_usb_nummem) {
sc->wi_usb_nummem--;
free(sc->wi_usb_txmem[sc->wi_usb_nummem], M_DEVBUF);
sc->wi_usb_txmem[sc->wi_usb_nummem] = NULL;
}
#if 0
/* if this is the first time, init, otherwise do not?? */
if (sc->wi_resetonce) {
return 0;
} else
sc->wi_resetonce = 1;
#endif
}
wi_usb_ctl_lock(sc);
wi_usb_tx_lock(sc);
c = &sc->wi_usb_tx_chain[0];
pcmd = c->wi_usb_buf;
total_len = sizeof (struct wi_cmdreq);
rnd_len = ROUNDUP64(total_len);
if (rnd_len > WI_USB_BUFSZ) {
printf("read_record buf size err %x %x\n",
rnd_len, WI_USB_BUFSZ);
err = EIO;
goto err_ret;
}
sc->cmdresp = cmd;
sc->cmdresperr = 0;
pcmd->type = htole16(WI_USB_CMDREQ);
pcmd->cmd = htole16(cmd);
pcmd->param0 = htole16(val0);
pcmd->param1 = htole16(val1);
pcmd->param2 = htole16(val2);
bzero(((char*)pcmd)+total_len, rnd_len - total_len);
total_len = rnd_len;
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_TX],
c, c->wi_usb_buf, rnd_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
WI_USB_TX_TIMEOUT, wi_usb_txeof);
err = wi_usb_do_transmit_sync(sc, c, &sc->cmdresperr);
if (err == 0)
err = sc->cmdresperr;
sc->cmdresperr = 0;
err_ret:
wi_usb_tx_unlock(sc);
wi_usb_ctl_unlock(sc);
DPRINTFN(5,("%s: %s: exit err=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, err));
return err;
}
int
wi_read_record_usb(struct wi_softc *wsc, struct wi_ltv_gen *ltv)
{
struct wi_usb_chain *c;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
struct wi_rridreq *prid;
int total_len, rnd_len;
int err;
struct wi_ltv_gen *oltv, p2ltv;
DPRINTFN(5,("%s: %s: enter rid=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, ltv->wi_type));
/* Do we need to deal with these here, as in _io version?
* WI_RID_ENCRYPTION -> WI_RID_P2_ENCRYPTION
* WI_RID_TX_CRYPT_KEY -> WI_RID_P2_TX_CRYPT_KEY
*/
if (wsc->sc_firmware_type != WI_LUCENT) {
oltv = ltv;
switch (ltv->wi_type) {
case WI_RID_ENCRYPTION:
p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
p2ltv.wi_len = 2;
ltv = &p2ltv;
break;
case WI_RID_TX_CRYPT_KEY:
if (ltv->wi_val > WI_NLTV_KEYS)
return (EINVAL);
p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
p2ltv.wi_len = 2;
ltv = &p2ltv;
break;
}
}
wi_usb_tx_lock(sc);
c = &sc->wi_usb_tx_chain[0];
prid = c->wi_usb_buf;
total_len = sizeof(struct wi_rridreq);
rnd_len = ROUNDUP64(total_len);
if (rnd_len > WI_USB_BUFSZ) {
printf("read_record buf size err %x %x\n",
rnd_len, WI_USB_BUFSZ);
wi_usb_tx_unlock(sc);
return EIO;
}
sc->ridltv = ltv;
sc->ridresperr = 0;
prid->type = htole16(WI_USB_RRIDREQ);
prid->frmlen = htole16(2); /* variable size? */
prid->rid = htole16(ltv->wi_type);
bzero(((char*)prid)+total_len, rnd_len - total_len);
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_TX],
c, c->wi_usb_buf, rnd_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
WI_USB_TX_TIMEOUT, wi_usb_txeof);
DPRINTFN(10,("%s: %s: total_len=%x, wilen %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, total_len, ltv->wi_len));
err = wi_usb_do_transmit_sync(sc, c, &sc->ridresperr);
/* Do we need to deal with these here, as in _io version?
*
* WI_RID_TX_RATE
* WI_RID_CUR_TX_RATE
* WI_RID_ENCRYPTION
* WI_RID_TX_CRYPT_KEY
* WI_RID_CNFAUTHMODE
*/
if (ltv->wi_type == WI_RID_PORTTYPE && wsc->wi_ptype == WI_PORTTYPE_IBSS
&& ltv->wi_val == wsc->wi_ibss_port) {
/*
* Convert vendor IBSS port type to WI_PORTTYPE_IBSS.
* Since Lucent uses port type 1 for BSS *and* IBSS we
* have to rely on wi_ptype to distinguish this for us.
*/
ltv->wi_val = htole16(WI_PORTTYPE_IBSS);
} else if (wsc->sc_firmware_type != WI_LUCENT) {
int v;
switch (oltv->wi_type) {
case WI_RID_TX_RATE:
case WI_RID_CUR_TX_RATE:
switch (letoh16(ltv->wi_val)) {
case 1: v = 1; break;
case 2: v = 2; break;
case 3: v = 6; break;
case 4: v = 5; break;
case 7: v = 7; break;
case 8: v = 11; break;
case 15: v = 3; break;
default: v = 0x100 + letoh16(ltv->wi_val); break;
}
oltv->wi_val = htole16(v);
break;
case WI_RID_ENCRYPTION:
oltv->wi_len = 2;
if (ltv->wi_val & htole16(0x01))
oltv->wi_val = htole16(1);
else
oltv->wi_val = htole16(0);
break;
case WI_RID_TX_CRYPT_KEY:
case WI_RID_CNFAUTHMODE:
oltv->wi_len = 2;
oltv->wi_val = ltv->wi_val;
break;
}
}
if (err == 0)
err = sc->ridresperr;
sc->ridresperr = 0;
wi_usb_tx_unlock(sc);
DPRINTFN(5,("%s: %s: exit err=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, err));
return err;
}
int
wi_write_record_usb(struct wi_softc *wsc, struct wi_ltv_gen *ltv)
{
struct wi_usb_chain *c;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
struct wi_wridreq *prid;
int total_len, rnd_len;
int err;
struct wi_ltv_gen p2ltv;
u_int16_t val = 0;
int i;
DPRINTFN(5,("%s: %s: enter rid=%x wi_len %d copying %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, ltv->wi_type, ltv->wi_len,
(ltv->wi_len-1)*2 ));
/* Do we need to deal with these here, as in _io version?
* WI_PORTTYPE_IBSS -> WI_RID_PORTTYPE
* RID_TX_RATE munging
* RID_ENCRYPTION
* WI_RID_TX_CRYPT_KEY
* WI_RID_DEFLT_CRYPT_KEYS
*/
if (ltv->wi_type == WI_RID_PORTTYPE &&
letoh16(ltv->wi_val) == WI_PORTTYPE_IBSS) {
/* Convert WI_PORTTYPE_IBSS to vendor IBSS port type. */
p2ltv.wi_type = WI_RID_PORTTYPE;
p2ltv.wi_len = 2;
p2ltv.wi_val = wsc->wi_ibss_port;
ltv = &p2ltv;
} else if (wsc->sc_firmware_type != WI_LUCENT) {
int v;
switch (ltv->wi_type) {
case WI_RID_TX_RATE:
p2ltv.wi_type = WI_RID_TX_RATE;
p2ltv.wi_len = 2;
switch (letoh16(ltv->wi_val)) {
case 1: v = 1; break;
case 2: v = 2; break;
case 3: v = 15; break;
case 5: v = 4; break;
case 6: v = 3; break;
case 7: v = 7; break;
case 11: v = 8; break;
default: return EINVAL;
}
p2ltv.wi_val = htole16(v);
ltv = &p2ltv;
break;
case WI_RID_ENCRYPTION:
p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
p2ltv.wi_len = 2;
if (ltv->wi_val & htole16(0x01)) {
val = PRIVACY_INVOKED;
/*
* If using shared key WEP we must set the
* EXCLUDE_UNENCRYPTED bit. Symbol cards
* need this bit set even when not using
* shared key. We can't just test for
* IEEE80211_AUTH_SHARED since Symbol cards
* have 2 shared key modes.
*/
if (wsc->wi_authtype != IEEE80211_AUTH_OPEN ||
wsc->sc_firmware_type == WI_SYMBOL)
val |= EXCLUDE_UNENCRYPTED;
switch (wsc->wi_crypto_algorithm) {
case WI_CRYPTO_FIRMWARE_WEP:
/*
* TX encryption is broken in
* Host AP mode.
*/
if (wsc->wi_ptype == WI_PORTTYPE_HOSTAP)
val |= HOST_ENCRYPT;
break;
case WI_CRYPTO_SOFTWARE_WEP:
val |= HOST_ENCRYPT|HOST_DECRYPT;
break;
}
p2ltv.wi_val = htole16(val);
} else
p2ltv.wi_val = htole16(HOST_ENCRYPT | HOST_DECRYPT);
ltv = &p2ltv;
break;
case WI_RID_TX_CRYPT_KEY:
if (ltv->wi_val > WI_NLTV_KEYS)
return (EINVAL);
p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
p2ltv.wi_len = 2;
p2ltv.wi_val = ltv->wi_val;
ltv = &p2ltv;
break;
case WI_RID_DEFLT_CRYPT_KEYS: {
int error;
int keylen;
struct wi_ltv_str ws;
struct wi_ltv_keys *wk;
wk = (struct wi_ltv_keys *)ltv;
keylen = wk->wi_keys[wsc->wi_tx_key].wi_keylen;
keylen = letoh16(keylen);
for (i = 0; i < 4; i++) {
bzero(&ws, sizeof(ws));
ws.wi_len = (keylen > 5) ? 8 : 4;
ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i;
bcopy(&wk->wi_keys[i].wi_keydat,
ws.wi_str, keylen);
error = wi_write_record_usb(wsc,
(struct wi_ltv_gen *)&ws);
if (error)
return (error);
}
}
return (0);
}
}
wi_usb_tx_lock(sc);
c = &sc->wi_usb_tx_chain[0];
prid = c->wi_usb_buf;
total_len = sizeof(prid->type) + sizeof(prid->frmlen) +
sizeof(prid->rid) + (ltv->wi_len-1)*2;
rnd_len = ROUNDUP64(total_len);
if (rnd_len > WI_USB_BUFSZ) {
printf("write_record buf size err %x %x\n",
rnd_len, WI_USB_BUFSZ);
wi_usb_tx_unlock(sc);
return EIO;
}
prid->type = htole16(WI_USB_WRIDREQ);
prid->frmlen = htole16(ltv->wi_len);
prid->rid = htole16(ltv->wi_type);
if (ltv->wi_len > 1)
bcopy((u_int8_t *)<v->wi_val, (u_int8_t *)&prid->data[0],
(ltv->wi_len-1)*2);
bzero(((char*)prid)+total_len, rnd_len - total_len);
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_TX],
c, c->wi_usb_buf, rnd_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
WI_USB_TX_TIMEOUT, wi_usb_txeof);
err = wi_usb_do_transmit_sync(sc, c, &sc->ridresperr);
if (err == 0)
err = sc->ridresperr;
sc->ridresperr = 0;
wi_usb_tx_unlock(sc);
DPRINTFN(5,("%s: %s: exit err=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, err));
return err;
}
/*
* This is an ugly compat portion to emulate the I/O which writes
* a packet or management information
* The data is copied into local memory for the requested
* 'id' then on the wi_cmd WI_CMD_TX, the id argument
* will identify which buffer to use
*/
int
wi_alloc_nicmem_usb(struct wi_softc *wsc, int len, int *id)
{
int nmem;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
DPRINTFN(10,("%s: %s: enter len=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, len));
/*
* NOTE THIS IS A USB DEVICE WHICH WILL LIKELY HAVE MANY
* CONNECTS/DISCONNECTS, FREE THIS MEMORY XXX XXX XXX !!! !!!
*/
nmem = sc->wi_usb_nummem++;
if (nmem >= MAX_WI_NMEM) {
sc->wi_usb_nummem--;
return ENOMEM;
}
sc->wi_usb_txmem[nmem] = malloc(len, M_DEVBUF, M_WAITOK);
if (sc->wi_usb_txmem[nmem] == NULL) {
sc->wi_usb_nummem--;
return ENOMEM;
}
sc->wi_usb_txmemsize[nmem] = len;
*id = nmem;
return 0;
}
/*
* this is crazy, we skip the first 16 bits of the buf so that it
* can be used as the 'type' of the usb transfer.
*/
int
wi_write_data_usb(struct wi_softc *wsc, int id, int off, caddr_t buf, int len)
{
u_int8_t *ptr;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
DPRINTFN(10,("%s: %s: id %x off %x len %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, id, off, len));
if (id < 0 && id >= sc->wi_usb_nummem)
return EIO;
ptr = (u_int8_t *)(sc->wi_usb_txmem[id]) + off;
if (len + off > sc->wi_usb_txmemsize[id])
return EIO;
DPRINTFN(10,("%s: %s: completed \n",
USBDEVNAME(sc->wi_usb_dev), __func__));
bcopy(buf, ptr, len);
return 0;
}
/*
* On the prism I/O, this read_data points to the hardware buffer
* which contains the
*/
int
wi_read_data_usb(struct wi_softc *wsc, int id, int off, caddr_t buf, int len)
{
u_int8_t *ptr;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
DPRINTFN(10,("%s: %s: id %x off %x len %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, id, off, len));
if (id == 0x1001 && sc->wi_info != NULL)
ptr = (u_int8_t *)sc->wi_info + off;
else if (id == 0x1000 && sc->wi_rxframe != NULL)
ptr = (u_int8_t *)sc->wi_rxframe + off;
else if (id >= 0 && id < sc->wi_usb_nummem) {
if (sc->wi_usb_txmem[id] == NULL)
return EIO;
if (len + off > sc->wi_usb_txmemsize[id])
return EIO;
ptr = (u_int8_t *)(sc->wi_usb_txmem[id]) + off;
} else
return EIO;
if (id < sc->wi_usb_nummem) {
ptr = (u_int8_t *)(sc->wi_usb_txmem[id]) + off;
if (len + off > sc->wi_usb_txmemsize[id])
return EIO;
}
bcopy(ptr, buf, len);
return 0;
}
void
wi_usb_stop(struct wi_usb_softc *sc)
{
DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev),__func__));
/* XXX */
/* Stop transfers */
}
int
wi_usb_do_transmit_sync(struct wi_usb_softc *sc, struct wi_usb_chain *c,
void *ident)
{
usbd_status err;
DPRINTFN(10,("%s: %s:\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
sc->wi_usb_refcnt++;
err = usbd_transfer(c->wi_usb_xfer);
if (err != USBD_IN_PROGRESS && err != USBD_NORMAL_COMPLETION) {
printf("%s: %s error=%s\n",
USBDEVNAME(sc->wi_usb_dev), __func__,
usbd_errstr(err));
/* Stop the interface from process context. */
wi_usb_stop(sc);
err = EIO;
goto done;
}
err = tsleep(ident, PRIBIO, "wiTXsync", hz*1);
if (err) {
DPRINTFN(1,("%s: %s: err %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, err));
err = ETIMEDOUT;
}
done:
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
return err;
}
/*
* A command/rrid/wrid was sent to the chip. It's safe for us to clean up
* the list buffers.
*/
void
wi_usb_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status)
{
struct wi_usb_chain *c = priv;
struct wi_usb_softc *sc = c->wi_usb_sc;
int s;
if (sc->wi_usb_dying)
return;
s = splnet();
DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->wi_usb_dev),
__func__, status));
if (status != USBD_NORMAL_COMPLETION) {
if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
splx(s);
return;
}
printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->wi_usb_dev),
usbd_errstr(status));
if (status == USBD_STALLED) {
sc->wi_usb_refcnt++;
usbd_clear_endpoint_stall_async(
sc->wi_usb_ep[WI_USB_ENDPT_TX]);
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
}
splx(s);
return;
}
splx(s);
}
/*
* A packet was sent to the chip. It's safe for us to clean up
* the list buffers.
*/
void
wi_usb_txeof_frm(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status)
{
struct wi_usb_chain *c = priv;
struct wi_usb_softc *sc = c->wi_usb_sc;
struct wi_softc *wsc = &sc->sc_wi;
struct ifnet *ifp = &wsc->sc_ic.ic_if;
int s;
int err = 0;
if (sc->wi_usb_dying)
return;
s = splnet();
DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->wi_usb_dev),
__func__, status));
if (status != USBD_NORMAL_COMPLETION) {
if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
splx(s);
return;
}
printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->wi_usb_dev),
usbd_errstr(status));
if (status == USBD_STALLED) {
sc->wi_usb_refcnt++;
usbd_clear_endpoint_stall_async(
sc->wi_usb_ep[WI_USB_ENDPT_TX]);
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
}
splx(s);
return;
}
if (status)
err = WI_EV_TX_EXC;
wi_txeof(wsc, err);
wi_usb_tx_unlock(sc);
if (!IFQ_IS_EMPTY(&ifp->if_snd))
wi_start_usb(ifp);
splx(s);
}
int
wi_usb_rx_list_init(struct wi_usb_softc *sc)
{
struct wi_usb_chain *c;
int i;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
for (i = 0; i < WI_USB_RX_LIST_CNT; i++) {
c = &sc->wi_usb_rx_chain[i];
c->wi_usb_sc = sc;
c->wi_usb_idx = i;
if (c->wi_usb_xfer != NULL) {
printf("UGH RX\n");
}
if (c->wi_usb_xfer == NULL) {
c->wi_usb_xfer = usbd_alloc_xfer(sc->wi_usb_udev);
if (c->wi_usb_xfer == NULL)
return (ENOBUFS);
c->wi_usb_buf = usbd_alloc_buffer(c->wi_usb_xfer,
WI_USB_BUFSZ);
if (c->wi_usb_buf == NULL)
return (ENOBUFS); /* XXX free xfer */
}
}
return (0);
}
int
wi_usb_tx_list_init(struct wi_usb_softc *sc)
{
struct wi_usb_chain *c;
int i;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
for (i = 0; i < WI_USB_TX_LIST_CNT; i++) {
c = &sc->wi_usb_tx_chain[i];
c->wi_usb_sc = sc;
c->wi_usb_idx = i;
c->wi_usb_mbuf = NULL;
if (c->wi_usb_xfer != NULL) {
printf("UGH TX\n");
}
if (c->wi_usb_xfer == NULL) {
c->wi_usb_xfer = usbd_alloc_xfer(sc->wi_usb_udev);
if (c->wi_usb_xfer == NULL)
return (ENOBUFS);
c->wi_usb_buf = usbd_alloc_buffer(c->wi_usb_xfer,
WI_USB_BUFSZ);
if (c->wi_usb_buf == NULL)
return (ENOBUFS);
}
}
return (0);
}
int
wi_usb_open_pipes(struct wi_usb_softc *sc)
{
usbd_status err;
int error = 0;
struct wi_usb_chain *c;
int i;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev),__func__));
sc->wi_usb_refcnt++;
/* Open RX and TX pipes. */
err = usbd_open_pipe(sc->wi_usb_iface, sc->wi_usb_ed[WI_USB_ENDPT_RX],
USBD_EXCLUSIVE_USE, &sc->wi_usb_ep[WI_USB_ENDPT_RX]);
if (err) {
printf("%s: open rx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
error = EIO;
goto done;
}
err = usbd_open_pipe(sc->wi_usb_iface, sc->wi_usb_ed[WI_USB_ENDPT_TX],
USBD_EXCLUSIVE_USE, &sc->wi_usb_ep[WI_USB_ENDPT_TX]);
if (err) {
printf("%s: open tx pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
error = EIO;
goto done;
}
/* is this used? */
err = usbd_open_pipe_intr(sc->wi_usb_iface,
sc->wi_usb_ed[WI_USB_ENDPT_INTR], USBD_EXCLUSIVE_USE,
&sc->wi_usb_ep[WI_USB_ENDPT_INTR], sc, &sc->wi_usb_ibuf,
WI_USB_INTR_PKTLEN, wi_usb_intr, WI_USB_INTR_INTERVAL);
if (err) {
printf("%s: open intr pipe failed: %s\n",
USBDEVNAME(sc->wi_usb_dev), usbd_errstr(err));
error = EIO;
goto done;
}
/* Start up the receive pipe. */
for (i = 0; i < WI_USB_RX_LIST_CNT; i++) {
c = &sc->wi_usb_rx_chain[i];
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_RX],
c, c->wi_usb_buf, WI_USB_BUFSZ,
USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
wi_usb_rxeof);
DPRINTFN(10,("%s: %s: start read\n", USBDEVNAME(sc->wi_usb_dev),
__func__));
usbd_transfer(c->wi_usb_xfer);
}
done:
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
return (error);
}
/*
* This is a bit of a kludge, however wi_rxeof and wi_update_stats
* call wi_get_fid to determine where the data associated with
* the transaction is located, the returned id is then used to
* wi_read_data the information out.
*
* This code returns which 'fid' should be used. The results are only valid
* during a wi_usb_rxeof because the data is received packet is 'held'
* an a variable for reading by wi_read_data_usb for that period.
*
* for magic numbers this uses 0x1000, 0x1001 for rx/info
*/
int
wi_get_fid_usb(struct wi_softc *sc, int fid)
{
switch (fid) {
case WI_RX_FID:
return 0x1000;
case WI_INFO_FID:
return 0x1001;
default:
return 0x1111;
}
}
int
wi_usb_activate(device_ptr_t self, enum devact act)
{
struct wi_usb_softc *sc = (struct wi_usb_softc *)self;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
switch (act) {
case DVACT_ACTIVATE:
break;
case DVACT_DEACTIVATE:
if_deactivate(&sc->sc_wi.wi_ec.ec_if);
sc->wi_usb_dying = 1;
sc->wi_thread_info->dying = 1;
break;
}
return (0);
}
#if 0
void
wi_dump_data(void *buffer, int len)
{
int i;
for (i = 0; i < len; i++) {
if (((i) % 16) == 0)
printf("\n %02x:", i);
printf(" %02x",
((uint8_t *)(buffer))[i]);
}
printf("\n");
}
#endif
/*
* A frame has been received.
*/
void
wi_usb_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
{
struct wi_usb_chain *c = priv;
struct wi_usb_softc *sc = c->wi_usb_sc;
wi_usb_usbin *uin;
int total_len = 0;
u_int16_t rtype;
if (sc->wi_usb_dying)
return;
DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->wi_usb_dev),
__func__, status));
if (status != USBD_NORMAL_COMPLETION) {
if (status == USBD_NOT_STARTED || status == USBD_IOERROR
|| status == USBD_CANCELLED) {
printf("%s: %u usb errors on rx: %s\n",
USBDEVNAME(sc->wi_usb_dev), 1,
/* sc->wi_usb_rx_errs, */
usbd_errstr(status));
return;
}
#if 0
sc->wi_usb_rx_errs++;
if (usbd_ratecheck(&sc->wi_usb_rx_notice)) {
printf("%s: %u usb errors on rx: %s\n",
USBDEVNAME(sc->wi_usb_dev), sc->wi_usb_rx_errs,
usbd_errstr(status));
sc->wi_usb_rx_errs = 0;
}
#endif
if (status == USBD_STALLED) {
sc->wi_usb_refcnt++;
usbd_clear_endpoint_stall_async(
sc->wi_usb_ep[WI_USB_ENDPT_RX]);
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
}
goto done;
}
usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
if (total_len < 6) /* short XXX */
goto done;
uin = (wi_usb_usbin *)(c->wi_usb_buf);
rtype = letoh16(uin->type);
#if 0
wi_dump_data(c->wi_usb_buf, total_len);
#endif
if (WI_USB_ISRXFRM(rtype)) {
wi_usb_rxfrm(sc, uin, total_len);
goto done;
}
if (WI_USB_ISTXFRM(rtype)) {
DPRINTFN(2,("%s: %s: txfrm type %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rtype));
wi_usb_txfrm(sc, uin, total_len);
goto done;
}
switch (rtype) {
case WI_USB_INFOFRM:
/* info packet, INFO_FID hmm */
DPRINTFN(10,("%s: %s: infofrm type %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rtype));
wi_usb_infofrm(c, total_len);
break;
case WI_USB_CMDRESP:
wi_usb_cmdresp(c);
break;
case WI_USB_WRIDRESP:
wi_usb_wridresp(c);
break;
case WI_USB_RRIDRESP:
wi_usb_rridresp(c);
break;
case WI_USB_WMEMRESP:
/* Not currently used */
DPRINTFN(2,("%s: %s: wmemresp type %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rtype));
break;
case WI_USB_RMEMRESP:
/* Not currently used */
DPRINTFN(2,("%s: %s: rmemresp type %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rtype));
break;
case WI_USB_BUFAVAIL:
printf("wi_usb: received USB_BUFAVAIL packet\n"); /* XXX */
break;
case WI_USB_ERROR:
printf("wi_usb: received USB_ERROR packet\n"); /* XXX */
break;
#if 0
default:
printf("wi_usb: received Unknown packet 0x%x len %x\n",
rtype, total_len);
wi_dump_data(c->wi_usb_buf, total_len);
#endif
}
done:
/* Setup new transfer. */
usbd_setup_xfer(c->wi_usb_xfer, sc->wi_usb_ep[WI_USB_ENDPT_RX],
c, c->wi_usb_buf, WI_USB_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
USBD_NO_TIMEOUT, wi_usb_rxeof);
sc->wi_usb_refcnt++;
usbd_transfer(c->wi_usb_xfer);
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->wi_usb_dev),
__func__));
}
void
wi_usb_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
{
struct wi_usb_softc *sc = priv;
DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
if (sc->wi_usb_dying)
return;
if (status != USBD_NORMAL_COMPLETION) {
if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
return;
if (status == USBD_STALLED) {
sc->wi_usb_refcnt++;
usbd_clear_endpoint_stall_async(
sc->wi_usb_ep[WI_USB_ENDPT_RX]);
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
}
return;
}
/* XXX oerrors or collisions? */
}
void
wi_usb_cmdresp(struct wi_usb_chain *c)
{
struct wi_cmdresp *presp = (struct wi_cmdresp *)(c->wi_usb_buf);
u_int16_t status = letoh16(presp->status);
struct wi_usb_softc *sc = c->wi_usb_sc;
uint16_t type;
uint16_t cmdresperr;
type = htole16(presp->type);
cmdresperr = letoh16(presp->resp0);
DPRINTFN(10,("%s: %s: enter type=%x, status=%x, cmdresp=%x, "
"resp=%x,%x,%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, type, status, sc->cmdresp,
cmdresperr, letoh16(presp->resp1),
letoh16(presp->resp2)));
/* XXX */
if (sc->cmdresp != (status & WI_STAT_CMD_CODE)) {
DPRINTFN(1,("%s: cmd ty %x st %x cmd %x failed %x\n",
USBDEVNAME(sc->wi_usb_dev),
type, status, sc->cmdresp, cmdresperr));
return;
}
sc->cmdresperr = (status & WI_STAT_CMD_RESULT) >> 8;
sc->cmdresp = 0; /* good value for idle == INI ?? XXX */
wakeup(&sc->cmdresperr);
}
void
wi_usb_rridresp(struct wi_usb_chain *c)
{
struct wi_rridresp *presp = (struct wi_rridresp *)(c->wi_usb_buf);
u_int16_t frmlen = letoh16(presp->frmlen);
struct wi_usb_softc *sc = c->wi_usb_sc;
struct wi_ltv_gen *ltv;
uint16_t rid;
rid = letoh16(presp->rid);
ltv = sc->ridltv;
if (ltv == 0) {
DPRINTFN(5,("%s: %s: enter ltv = 0 rid=%x len %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rid,
frmlen));
return;
}
DPRINTFN(5,("%s: %s: enter rid=%x expecting %x len %d exptlen %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, rid, ltv->wi_type,
frmlen, ltv->wi_len));
rid = letoh16(presp->rid);
if (rid != ltv->wi_type) {
sc->ridresperr = EIO;
return;
}
if (frmlen > ltv->wi_len) {
sc->ridresperr = ENOSPC;
sc->ridltv = 0;
wakeup(&sc->ridresperr);
return;
}
ltv->wi_len = frmlen;
DPRINTFN(10,("%s: %s: copying %d frmlen %d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, (ltv->wi_len-1)*2,
frmlen));
if (ltv->wi_len > 1)
bcopy(&presp->data[0], (u_int8_t *)<v->wi_val,
(ltv->wi_len-1)*2);
sc->ridresperr = 0;
sc->ridltv = 0;
wakeup(&sc->ridresperr);
}
void
wi_usb_wridresp(struct wi_usb_chain *c)
{
struct wi_wridresp *presp = (struct wi_wridresp *)(c->wi_usb_buf);
struct wi_usb_softc *sc = c->wi_usb_sc;
uint16_t status;
status = letoh16(presp->status);
DPRINTFN(10,("%s: %s: enter status=%x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, status));
sc->ridresperr = (status & WI_STAT_CMD_RESULT) >> 8;
sc->ridltv = 0;
wakeup(&sc->ridresperr);
}
void
wi_usb_infofrm(struct wi_usb_chain *c, int len)
{
struct wi_usb_softc *sc = c->wi_usb_sc;
DPRINTFN(10,("%s: %s: enter\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
sc->wi_info = ((char *)c->wi_usb_buf) + 2;
wi_update_stats(&sc->sc_wi);
sc->wi_info = NULL;
}
void
wi_usb_txfrm(struct wi_usb_softc *sc, wi_usb_usbin *uin, int total_len)
{
u_int16_t status;
int s;
struct wi_softc *wsc = &sc->sc_wi;
struct ifnet *ifp = &wsc->sc_ic.ic_if;
s = splnet();
status = letoh16(uin->type); /* XXX -- type == status */
DPRINTFN(2,("%s: %s: enter status=%d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, status));
if (sc->txresp == WI_CMD_TX) {
sc->txresperr=status;
sc->txresp = 0;
wakeup(&sc->txresperr);
} else {
if (status != 0) /* XXX */
wi_watchdog_usb(ifp);
DPRINTFN(1,("%s: %s: txresp not expected status=%d \n",
USBDEVNAME(sc->wi_usb_dev), __func__, status));
}
splx(s);
}
void
wi_usb_rxfrm(struct wi_usb_softc *sc, wi_usb_usbin *uin, int total_len)
{
int s;
DPRINTFN(5,("%s: %s: enter len=%d\n",
USBDEVNAME(sc->wi_usb_dev), __func__, total_len));
s = splnet();
sc->wi_rxframe = (void *)uin;
wi_rxeof(&sc->sc_wi);
sc->wi_rxframe = NULL;
splx(s);
}
void
wi_usb_start_thread(void *arg)
{
struct wi_usb_softc *sc = arg;
kthread_create (wi_usb_thread, arg, NULL, USBDEVNAME(sc->wi_usb_dev));
}
void
wi_start_usb(struct ifnet *ifp)
{
struct wi_softc *wsc;
struct wi_usb_softc *sc;
int s;
wsc = ifp->if_softc;
sc = wsc->wi_usb_cdata;
s = splnet();
DPRINTFN(5,("%s: %s:\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
if (wi_usb_tx_lock_try(sc)) {
/* lock acquired do start now */
wi_func_io.f_start(ifp);
} else {
sc->wi_thread_info->status |= WI_START;
if (sc->wi_thread_info->idle)
wakeup(sc->wi_thread_info);
}
splx(s);
}
/*
* inquire is called from interrupt context (timeout)
* It is not possible to sleep in interrupt context so it is necessary
* to signal the kernel thread to perform the action.
*/
void
wi_init_usb(struct wi_softc *wsc)
{
DPRINTFN(5,("%s: %s:\n", WI_PRT_ARG(wsc), __func__));
wi_usb_ctl_lock(wsc->wi_usb_cdata);
wi_func_io.f_init(wsc);
wi_usb_ctl_unlock(wsc->wi_usb_cdata);
}
/*
* inquire is called from interrupt context (timeout)
* It is not possible to sleep in interrupt context so it is necessary
* to signal the kernel thread to perform the action.
*/
void
wi_inquire_usb(void *xsc)
{
struct wi_softc *wsc = xsc;
struct wi_usb_softc *sc = wsc->wi_usb_cdata;
int s;
s = splnet();
DPRINTFN(2,("%s: %s:\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
sc->wi_thread_info->status |= WI_INQUIRE;
if (sc->wi_thread_info->idle)
wakeup(sc->wi_thread_info);
splx(s);
}
/*
* Watchdog is normally called from interrupt context (timeout)
* It is not possible to sleep in interrupt context so it is necessary
* to signal the kernel thread to perform the action.
*/
void
wi_watchdog_usb(struct ifnet *ifp)
{
struct wi_softc *wsc;
struct wi_usb_softc *sc;
int s;
wsc = ifp->if_softc;
sc = wsc->wi_usb_cdata;
s = splnet();
DPRINTFN(5,("%s: %s: ifp %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__, ifp));
sc->wi_thread_info->status |= WI_WATCHDOG;
if (sc->wi_thread_info->idle)
wakeup(sc->wi_thread_info);
splx(s);
}
/*
* ioctl will always be called from a user context,
* therefore it is possible to sleep in the calling context
* acquire the lock and call the real ioctl fucntion directly
*/
int
wi_ioctl_usb(struct ifnet *ifp, u_long command, caddr_t data)
{
struct wi_softc *wsc;
int err;
wsc = ifp->if_softc;
wi_usb_ctl_lock(wsc->wi_usb_cdata);
err = wi_func_io.f_ioctl(ifp, command, data);
wi_usb_ctl_unlock(wsc->wi_usb_cdata);
return err;
}
void
wi_usb_thread(void *arg)
{
struct wi_usb_softc *sc = arg;
struct wi_usb_thread_info *wi_thread_info;
int s;
wi_thread_info = malloc(sizeof(*wi_thread_info), M_DEVBUF, M_WAITOK);
/*
* is there a remote possibility that the device could
* be removed before the kernel thread starts up?
*/
sc->wi_usb_refcnt++;
sc->wi_thread_info = wi_thread_info;
wi_thread_info->dying = 0;
wi_thread_info->status = 0;
wi_usb_ctl_lock(sc);
wi_attach(&sc->sc_wi, &wi_func_usb);
wi_usb_ctl_unlock(sc);
for(;;) {
if (wi_thread_info->dying) {
if (--sc->wi_usb_refcnt < 0)
usb_detach_wakeup(USBDEV(sc->wi_usb_dev));
kthread_exit(0);
}
DPRINTFN(5,("%s: %s: dying %x status %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__,
wi_thread_info->dying, wi_thread_info->status));
wi_usb_ctl_lock(sc);
DPRINTFN(5,("%s: %s: starting %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__,
wi_thread_info->status));
s = splusb();
if (wi_thread_info->status & WI_START) {
wi_thread_info->status &= ~WI_START;
wi_usb_tx_lock(sc);
wi_func_io.f_start(&sc->sc_wi.sc_ic.ic_if);
/*
* tx_unlock is explicitly missing here
* it is done in txeof_frm
*/
} else if (wi_thread_info->status & WI_INQUIRE) {
wi_thread_info->status &= ~WI_INQUIRE;
wi_func_io.f_inquire(&sc->sc_wi);
} else if (wi_thread_info->status & WI_WATCHDOG) {
wi_thread_info->status &= ~WI_WATCHDOG;
wi_func_io.f_watchdog( &sc->sc_wi.sc_ic.ic_if);
}
splx(s);
DPRINTFN(5,("%s: %s: ending %x\n",
USBDEVNAME(sc->wi_usb_dev), __func__,
wi_thread_info->status));
wi_usb_ctl_unlock(sc);
if (wi_thread_info->status == 0) {
s = splnet();
wi_thread_info->idle = 1;
tsleep(wi_thread_info, PRIBIO, "wiIDL", 0);
wi_thread_info->idle = 0;
splx(s);
}
}
}
int
wi_usb_tx_lock_try(struct wi_usb_softc *sc)
{
int s;
s = splnet();
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
if (sc->wi_lock != 0) {
return 0; /* failed to aquire lock */
}
sc->wi_lock = 1;
splx(s);
return 1;
}
void
wi_usb_tx_lock(struct wi_usb_softc *sc)
{
int s;
s = splnet();
again:
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
if (sc->wi_lock != 0) {
sc->wi_lockwait++;
DPRINTFN(10,("%s: %s: busy %d\n", USBDEVNAME(sc->wi_usb_dev),
__func__, sc->wi_lockwait ));
tsleep(&sc->wi_lock, PRIBIO, "witxl", 0);
}
if (sc->wi_lock != 0)
goto again;
sc->wi_lock = 1;
splx(s);
return;
}
void
wi_usb_tx_unlock(struct wi_usb_softc *sc)
{
int s;
s = splnet();
sc->wi_lock = 0;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
if (sc->wi_lockwait) {
DPRINTFN(10,("%s: %s: waking\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
sc->wi_lockwait = 0;
wakeup(&sc->wi_lock);
}
splx(s);
}
void
wi_usb_ctl_lock(struct wi_usb_softc *sc)
{
int s;
s = splnet();
again:
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev),
__func__));
if (sc->wi_ctllock != 0) {
if (curproc == sc->wi_curproc) {
/* allow recursion */
sc->wi_ctllock++;
splx(s);
return;
}
sc->wi_ctllockwait++;
DPRINTFN(10,("%s: %s: busy %d\n", USBDEVNAME(sc->wi_usb_dev),
__func__, sc->wi_ctllockwait ));
tsleep(&sc->wi_ctllock, PRIBIO, "wiusbthr", 0);
}
if (sc->wi_ctllock != 0)
goto again;
sc->wi_ctllock++;
sc->wi_curproc = curproc;
splx(s);
return;
}
void
wi_usb_ctl_unlock(struct wi_usb_softc *sc)
{
int s;
s = splnet();
sc->wi_ctllock--;
DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->wi_usb_dev), __func__));
if (sc->wi_ctllock == 0 && sc->wi_ctllockwait) {
DPRINTFN(10,("%s: %s: waking\n",
USBDEVNAME(sc->wi_usb_dev), __func__));
sc->wi_ctllockwait = 0;
sc->wi_curproc = 0;
wakeup(&sc->wi_ctllock);
}
splx(s);
}
|