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
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
|
/*-
* Copyright (c) 1997,1998 Doug Rabson
* 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 AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "opt_bus.h"
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/module.h>
#ifdef DEVICE_SYSCTLS
#include <sys/sysctl.h>
#endif
#include <sys/kobj.h>
#include <sys/bus_private.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/stdarg.h> /* for device_printf() */
MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
#ifdef BUS_DEBUG
#include <sys/sysctl.h>
static int bus_debug = 1;
SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, "Debug bus code");
#define PDEBUG(a) if (bus_debug) {printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n");}
#define DEVICENAME(d) ((d)? device_get_name(d): "no device")
#define DRIVERNAME(d) ((d)? d->name : "no driver")
#define DEVCLANAME(d) ((d)? d->name : "no devclass")
/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
* prevent syslog from deleting initial spaces
*/
#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0)
static void print_device_short(device_t dev, int indent);
static void print_device(device_t dev, int indent);
void print_device_tree_short(device_t dev, int indent);
void print_device_tree(device_t dev, int indent);
static void print_driver_short(driver_t *driver, int indent);
static void print_driver(driver_t *driver, int indent);
static void print_driver_list(driver_list_t drivers, int indent);
static void print_devclass_short(devclass_t dc, int indent);
static void print_devclass(devclass_t dc, int indent);
void print_devclass_list_short(void);
void print_devclass_list(void);
#else
/* Make the compiler ignore the function calls */
#define PDEBUG(a) /* nop */
#define DEVICENAME(d) /* nop */
#define DRIVERNAME(d) /* nop */
#define DEVCLANAME(d) /* nop */
#define print_device_short(d,i) /* nop */
#define print_device(d,i) /* nop */
#define print_device_tree_short(d,i) /* nop */
#define print_device_tree(d,i) /* nop */
#define print_driver_short(d,i) /* nop */
#define print_driver(d,i) /* nop */
#define print_driver_list(d,i) /* nop */
#define print_devclass_short(d,i) /* nop */
#define print_devclass(d,i) /* nop */
#define print_devclass_list_short() /* nop */
#define print_devclass_list() /* nop */
#endif
#ifdef DEVICE_SYSCTLS
static void device_register_oids(device_t dev);
static void device_unregister_oids(device_t dev);
#endif
extern char static_hints[];
static int hints_loaded;
kobj_method_t null_methods[] = {
{ 0, 0 }
};
DEFINE_CLASS(null, null_methods, 0);
/*
* Devclass implementation
*/
static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
static devclass_t
devclass_find_internal(const char *classname, int create)
{
devclass_t dc;
PDEBUG(("looking for %s", classname));
if (!classname)
return NULL;
for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
if (!strcmp(dc->name, classname))
return dc;
PDEBUG(("%s not found%s", classname, (create? ", creating": "")));
if (create) {
dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
M_BUS, M_NOWAIT);
if (!dc)
return NULL;
bzero(dc, sizeof(struct devclass) + strlen(classname) + 1);
dc->name = (char*) (dc + 1);
strcpy(dc->name, classname);
dc->devices = NULL;
dc->maxunit = 0;
TAILQ_INIT(&dc->drivers);
TAILQ_INSERT_TAIL(&devclasses, dc, link);
}
return dc;
}
devclass_t
devclass_create(const char *classname)
{
return devclass_find_internal(classname, TRUE);
}
devclass_t
devclass_find(const char *classname)
{
return devclass_find_internal(classname, FALSE);
}
int
devclass_add_driver(devclass_t dc, driver_t *driver)
{
driverlink_t dl;
int i;
PDEBUG(("%s", DRIVERNAME(driver)));
dl = malloc(sizeof *dl, M_BUS, M_NOWAIT);
if (!dl)
return ENOMEM;
bzero(dl, sizeof *dl);
/*
* Compile the driver's methods. Also increase the reference count
* so that the class doesn't get freed when the last instance
* goes. This means we can safely use static methods and avoids a
* double-free in devclass_delete_driver.
*/
kobj_class_compile((kobj_class_t) driver);
/*
* Make sure the devclass which the driver is implementing exists.
*/
devclass_find_internal(driver->name, TRUE);
dl->driver = driver;
TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
driver->refs++;
/*
* Call BUS_DRIVER_ADDED for any existing busses in this class.
*/
for (i = 0; i < dc->maxunit; i++)
if (dc->devices[i])
BUS_DRIVER_ADDED(dc->devices[i], driver);
return 0;
}
int
devclass_delete_driver(devclass_t busclass, driver_t *driver)
{
devclass_t dc = devclass_find(driver->name);
driverlink_t dl;
device_t dev;
int i;
int error;
PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
if (!dc)
return 0;
/*
* Find the link structure in the bus' list of drivers.
*/
for (dl = TAILQ_FIRST(&busclass->drivers); dl;
dl = TAILQ_NEXT(dl, link)) {
if (dl->driver == driver)
break;
}
if (!dl) {
PDEBUG(("%s not found in %s list", driver->name, busclass->name));
return ENOENT;
}
/*
* Disassociate from any devices. We iterate through all the
* devices in the devclass of the driver and detach any which are
* using the driver and which have a parent in the devclass which
* we are deleting from.
*
* Note that since a driver can be in multiple devclasses, we
* should not detach devices which are not children of devices in
* the affected devclass.
*/
for (i = 0; i < dc->maxunit; i++) {
if (dc->devices[i]) {
dev = dc->devices[i];
if (dev->driver == driver
&& dev->parent && dev->parent->devclass == busclass) {
if ((error = device_detach(dev)) != 0)
return error;
device_set_driver(dev, NULL);
}
}
}
TAILQ_REMOVE(&busclass->drivers, dl, link);
free(dl, M_BUS);
driver->refs--;
if (driver->refs == 0)
kobj_class_free((kobj_class_t) driver);
return 0;
}
static driverlink_t
devclass_find_driver_internal(devclass_t dc, const char *classname)
{
driverlink_t dl;
PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link)) {
if (!strcmp(dl->driver->name, classname))
return dl;
}
PDEBUG(("not found"));
return NULL;
}
driver_t *
devclass_find_driver(devclass_t dc, const char *classname)
{
driverlink_t dl;
dl = devclass_find_driver_internal(dc, classname);
if (dl)
return dl->driver;
else
return NULL;
}
const char *
devclass_get_name(devclass_t dc)
{
return dc->name;
}
device_t
devclass_get_device(devclass_t dc, int unit)
{
if (dc == NULL || unit < 0 || unit >= dc->maxunit)
return NULL;
return dc->devices[unit];
}
void *
devclass_get_softc(devclass_t dc, int unit)
{
device_t dev;
dev = devclass_get_device(dc, unit);
if (!dev)
return (NULL);
return (device_get_softc(dev));
}
int
devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
{
int i;
int count;
device_t *list;
count = 0;
for (i = 0; i < dc->maxunit; i++)
if (dc->devices[i])
count++;
list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
if (!list)
return ENOMEM;
bzero(list, count * sizeof(device_t));
count = 0;
for (i = 0; i < dc->maxunit; i++)
if (dc->devices[i]) {
list[count] = dc->devices[i];
count++;
}
*devlistp = list;
*devcountp = count;
return 0;
}
int
devclass_get_maxunit(devclass_t dc)
{
return dc->maxunit;
}
static int
devclass_alloc_unit(devclass_t dc, int *unitp)
{
int unit = *unitp;
PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
/* If we have been given a wired unit number, check for existing device */
if (unit != -1) {
if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) {
if (bootverbose)
printf("%s-: %s%d exists, using next available unit number\n",
dc->name, dc->name, unit);
/* find the next available slot */
while (++unit < dc->maxunit && dc->devices[unit] != NULL)
;
}
}
else {
/* Unwired device, find the next available slot for it */
unit = 0;
while (unit < dc->maxunit && dc->devices[unit] != NULL)
unit++;
}
/*
* We've selected a unit beyond the length of the table, so let's extend
* the table to make room for all units up to and including this one.
*/
if (unit >= dc->maxunit) {
device_t *newlist;
int newsize;
newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
if (!newlist)
return ENOMEM;
bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
bzero(newlist + dc->maxunit,
sizeof(device_t) * (newsize - dc->maxunit));
if (dc->devices)
free(dc->devices, M_BUS);
dc->devices = newlist;
dc->maxunit = newsize;
}
PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
*unitp = unit;
return 0;
}
static int
devclass_add_device(devclass_t dc, device_t dev)
{
int buflen, error;
PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
buflen = strlen(dc->name) + 5;
dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT);
if (!dev->nameunit)
return ENOMEM;
bzero(dev->nameunit, buflen);
if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
free(dev->nameunit, M_BUS);
dev->nameunit = NULL;
return error;
}
dc->devices[dev->unit] = dev;
dev->devclass = dc;
snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
#ifdef DEVICE_SYSCTLS
device_register_oids(dev);
#endif
return 0;
}
static int
devclass_delete_device(devclass_t dc, device_t dev)
{
if (!dc || !dev)
return 0;
PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
if (dev->devclass != dc
|| dc->devices[dev->unit] != dev)
panic("devclass_delete_device: inconsistent device class");
dc->devices[dev->unit] = NULL;
if (dev->flags & DF_WILDCARD)
dev->unit = -1;
dev->devclass = NULL;
free(dev->nameunit, M_BUS);
dev->nameunit = NULL;
#ifdef DEVICE_SYSCTLS
device_unregister_oids(dev);
#endif
return 0;
}
static device_t
make_device(device_t parent, const char *name, int unit)
{
device_t dev;
devclass_t dc;
PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
if (name) {
dc = devclass_find_internal(name, TRUE);
if (!dc) {
printf("make_device: can't find device class %s\n", name);
return NULL;
}
} else
dc = NULL;
dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT);
if (!dev)
return 0;
bzero(dev, sizeof(struct device));
dev->parent = parent;
TAILQ_INIT(&dev->children);
kobj_init((kobj_t) dev, &null_class);
dev->driver = NULL;
dev->devclass = NULL;
dev->unit = unit;
dev->nameunit = NULL;
dev->desc = NULL;
dev->busy = 0;
dev->devflags = 0;
dev->flags = DF_ENABLED;
dev->order = 0;
if (unit == -1)
dev->flags |= DF_WILDCARD;
if (name) {
dev->flags |= DF_FIXEDCLASS;
devclass_add_device(dc, dev);
}
dev->ivars = NULL;
dev->softc = NULL;
dev->state = DS_NOTPRESENT;
kobj_init((kobj_t) dev, &null_class);
return dev;
}
static int
device_print_child(device_t dev, device_t child)
{
int retval = 0;
if (device_is_alive(child)) {
retval += BUS_PRINT_CHILD(dev, child);
} else
retval += device_printf(child, " not found\n");
return (retval);
}
device_t
device_add_child(device_t dev, const char *name, int unit)
{
return device_add_child_ordered(dev, 0, name, unit);
}
device_t
device_add_child_ordered(device_t dev, int order, const char *name, int unit)
{
device_t child;
device_t place;
PDEBUG(("%s at %s with order %d as unit %d",
name, DEVICENAME(dev), order, unit));
child = make_device(dev, name, unit);
if (child == NULL)
return child;
child->order = order;
TAILQ_FOREACH(place, &dev->children, link)
if (place->order > order)
break;
if (place) {
/*
* The device 'place' is the first device whose order is
* greater than the new child.
*/
TAILQ_INSERT_BEFORE(place, child, link);
} else {
/*
* The new child's order is greater or equal to the order of
* any existing device. Add the child to the tail of the list.
*/
TAILQ_INSERT_TAIL(&dev->children, child, link);
}
return child;
}
int
device_delete_child(device_t dev, device_t child)
{
int error;
device_t grandchild;
PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
/* remove children first */
while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
error = device_delete_child(child, grandchild);
if (error)
return error;
}
if ((error = device_detach(child)) != 0)
return error;
if (child->devclass)
devclass_delete_device(child->devclass, child);
TAILQ_REMOVE(&dev->children, child, link);
device_set_desc(child, NULL);
free(child, M_BUS);
return 0;
}
/*
* Find only devices attached to this bus.
*/
device_t
device_find_child(device_t dev, const char *classname, int unit)
{
devclass_t dc;
device_t child;
dc = devclass_find(classname);
if (!dc)
return NULL;
child = devclass_get_device(dc, unit);
if (child && child->parent == dev)
return child;
return NULL;
}
static driverlink_t
first_matching_driver(devclass_t dc, device_t dev)
{
if (dev->devclass)
return devclass_find_driver_internal(dc, dev->devclass->name);
else
return TAILQ_FIRST(&dc->drivers);
}
static driverlink_t
next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
{
if (dev->devclass) {
driverlink_t dl;
for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
if (!strcmp(dev->devclass->name, dl->driver->name))
return dl;
return NULL;
} else
return TAILQ_NEXT(last, link);
}
static int
device_probe_child(device_t dev, device_t child)
{
devclass_t dc;
driverlink_t best = 0;
driverlink_t dl;
int result, pri = 0;
int hasclass = (child->devclass != 0);
dc = dev->devclass;
if (!dc)
panic("device_probe_child: parent device has no devclass");
if (child->state == DS_ALIVE)
return 0;
for (dl = first_matching_driver(dc, child);
dl;
dl = next_matching_driver(dc, child, dl)) {
PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
device_set_driver(child, dl->driver);
if (!hasclass)
device_set_devclass(child, dl->driver->name);
result = DEVICE_PROBE(child);
if (!hasclass)
device_set_devclass(child, 0);
/*
* If the driver returns SUCCESS, there can be no higher match
* for this device.
*/
if (result == 0) {
best = dl;
pri = 0;
break;
}
/*
* The driver returned an error so it certainly doesn't match.
*/
if (result > 0) {
device_set_driver(child, 0);
continue;
}
/*
* A priority lower than SUCCESS, remember the best matching
* driver. Initialise the value of pri for the first match.
*/
if (best == 0 || result > pri) {
best = dl;
pri = result;
continue;
}
}
/*
* If we found a driver, change state and initialise the devclass.
*/
if (best) {
if (!child->devclass)
device_set_devclass(child, best->driver->name);
device_set_driver(child, best->driver);
if (pri < 0) {
/*
* A bit bogus. Call the probe method again to make sure
* that we have the right description.
*/
DEVICE_PROBE(child);
}
child->state = DS_ALIVE;
return 0;
}
return ENXIO;
}
device_t
device_get_parent(device_t dev)
{
return dev->parent;
}
int
device_get_children(device_t dev, device_t **devlistp, int *devcountp)
{
int count;
device_t child;
device_t *list;
count = 0;
for (child = TAILQ_FIRST(&dev->children); child;
child = TAILQ_NEXT(child, link))
count++;
list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
if (!list)
return ENOMEM;
bzero(list, count * sizeof(device_t));
count = 0;
for (child = TAILQ_FIRST(&dev->children); child;
child = TAILQ_NEXT(child, link)) {
list[count] = child;
count++;
}
*devlistp = list;
*devcountp = count;
return 0;
}
driver_t *
device_get_driver(device_t dev)
{
return dev->driver;
}
devclass_t
device_get_devclass(device_t dev)
{
return dev->devclass;
}
const char *
device_get_name(device_t dev)
{
if (dev->devclass)
return devclass_get_name(dev->devclass);
return NULL;
}
const char *
device_get_nameunit(device_t dev)
{
return dev->nameunit;
}
int
device_get_unit(device_t dev)
{
return dev->unit;
}
const char *
device_get_desc(device_t dev)
{
return dev->desc;
}
u_int32_t
device_get_flags(device_t dev)
{
return dev->devflags;
}
int
device_print_prettyname(device_t dev)
{
const char *name = device_get_name(dev);
if (name == 0)
return printf("unknown: ");
else
return printf("%s%d: ", name, device_get_unit(dev));
}
int
device_printf(device_t dev, const char * fmt, ...)
{
va_list ap;
int retval;
retval = device_print_prettyname(dev);
va_start(ap, fmt);
retval += vprintf(fmt, ap);
va_end(ap);
return retval;
}
static void
device_set_desc_internal(device_t dev, const char* desc, int copy)
{
if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
free(dev->desc, M_BUS);
dev->flags &= ~DF_DESCMALLOCED;
dev->desc = NULL;
}
if (copy && desc) {
dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
if (dev->desc) {
strcpy(dev->desc, desc);
dev->flags |= DF_DESCMALLOCED;
}
} else
/* Avoid a -Wcast-qual warning */
dev->desc = (char *)(uintptr_t) desc;
#ifdef DEVICE_SYSCTLS
{
struct sysctl_oid *oid = &dev->oid[1];
oid->oid_arg1 = dev->desc ? dev->desc : "";
oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
}
#endif
}
void
device_set_desc(device_t dev, const char* desc)
{
device_set_desc_internal(dev, desc, FALSE);
}
void
device_set_desc_copy(device_t dev, const char* desc)
{
device_set_desc_internal(dev, desc, TRUE);
}
void
device_set_flags(device_t dev, u_int32_t flags)
{
dev->devflags = flags;
}
void *
device_get_softc(device_t dev)
{
return dev->softc;
}
void
device_set_softc(device_t dev, void *softc)
{
if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
free(dev->softc, M_BUS);
dev->softc = softc;
if (dev->softc)
dev->flags |= DF_EXTERNALSOFTC;
else
dev->flags &= ~DF_EXTERNALSOFTC;
}
void *
device_get_ivars(device_t dev)
{
return dev->ivars;
}
void
device_set_ivars(device_t dev, void * ivars)
{
if (!dev)
return;
dev->ivars = ivars;
return;
}
device_state_t
device_get_state(device_t dev)
{
return dev->state;
}
void
device_enable(device_t dev)
{
dev->flags |= DF_ENABLED;
}
void
device_disable(device_t dev)
{
dev->flags &= ~DF_ENABLED;
}
void
device_busy(device_t dev)
{
if (dev->state < DS_ATTACHED)
panic("device_busy: called for unattached device");
if (dev->busy == 0 && dev->parent)
device_busy(dev->parent);
dev->busy++;
dev->state = DS_BUSY;
}
void
device_unbusy(device_t dev)
{
if (dev->state != DS_BUSY)
panic("device_unbusy: called for non-busy device");
dev->busy--;
if (dev->busy == 0) {
if (dev->parent)
device_unbusy(dev->parent);
dev->state = DS_ATTACHED;
}
}
void
device_quiet(device_t dev)
{
dev->flags |= DF_QUIET;
}
void
device_verbose(device_t dev)
{
dev->flags &= ~DF_QUIET;
}
int
device_is_quiet(device_t dev)
{
return (dev->flags & DF_QUIET) != 0;
}
int
device_is_enabled(device_t dev)
{
return (dev->flags & DF_ENABLED) != 0;
}
int
device_is_alive(device_t dev)
{
return dev->state >= DS_ALIVE;
}
int
device_set_devclass(device_t dev, const char *classname)
{
devclass_t dc;
if (!classname) {
if (dev->devclass)
devclass_delete_device(dev->devclass, dev);
return 0;
}
if (dev->devclass) {
printf("device_set_devclass: device class already set\n");
return EINVAL;
}
dc = devclass_find_internal(classname, TRUE);
if (!dc)
return ENOMEM;
return devclass_add_device(dc, dev);
}
int
device_set_driver(device_t dev, driver_t *driver)
{
if (dev->state >= DS_ATTACHED)
return EBUSY;
if (dev->driver == driver)
return 0;
if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
free(dev->softc, M_BUS);
dev->softc = NULL;
}
kobj_delete((kobj_t) dev, 0);
dev->driver = driver;
if (driver) {
kobj_init((kobj_t) dev, (kobj_class_t) driver);
if (!(dev->flags & DF_EXTERNALSOFTC)) {
dev->softc = malloc(driver->size, M_BUS, M_NOWAIT);
if (!dev->softc) {
kobj_init((kobj_t) dev, &null_class);
dev->driver = NULL;
return ENOMEM;
}
bzero(dev->softc, driver->size);
}
} else
kobj_init((kobj_t) dev, &null_class);
return 0;
}
int
device_probe_and_attach(device_t dev)
{
device_t bus = dev->parent;
int error = 0;
if (dev->state >= DS_ALIVE)
return 0;
if (dev->flags & DF_ENABLED) {
error = device_probe_child(bus, dev);
if (!error) {
if (!device_is_quiet(dev))
device_print_child(bus, dev);
error = DEVICE_ATTACH(dev);
if (!error)
dev->state = DS_ATTACHED;
else {
printf("device_probe_and_attach: %s%d attach returned %d\n",
dev->driver->name, dev->unit, error);
device_set_driver(dev, NULL);
dev->state = DS_NOTPRESENT;
}
} else {
if (!(dev->flags & DF_DONENOMATCH)) {
BUS_PROBE_NOMATCH(bus, dev);
dev->flags |= DF_DONENOMATCH;
}
}
} else {
if (bootverbose) {
device_print_prettyname(dev);
printf("not probed (disabled)\n");
}
}
return error;
}
int
device_detach(device_t dev)
{
int error;
PDEBUG(("%s", DEVICENAME(dev)));
if (dev->state == DS_BUSY)
return EBUSY;
if (dev->state != DS_ATTACHED)
return 0;
if ((error = DEVICE_DETACH(dev)) != 0)
return error;
device_printf(dev, "detached\n");
if (dev->parent)
BUS_CHILD_DETACHED(dev->parent, dev);
if (!(dev->flags & DF_FIXEDCLASS))
devclass_delete_device(dev->devclass, dev);
dev->state = DS_NOTPRESENT;
device_set_driver(dev, NULL);
return 0;
}
int
device_shutdown(device_t dev)
{
if (dev->state < DS_ATTACHED)
return 0;
return DEVICE_SHUTDOWN(dev);
}
int
device_set_unit(device_t dev, int unit)
{
devclass_t dc;
int err;
dc = device_get_devclass(dev);
if (unit < dc->maxunit && dc->devices[unit])
return EBUSY;
err = devclass_delete_device(dc, dev);
if (err)
return err;
dev->unit = unit;
err = devclass_add_device(dc, dev);
if (err)
return err;
return 0;
}
#ifdef DEVICE_SYSCTLS
/*
* Sysctl nodes for devices.
*/
SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
static int
sysctl_handle_children(SYSCTL_HANDLER_ARGS)
{
device_t dev = arg1;
device_t child;
int first = 1, error = 0;
for (child = TAILQ_FIRST(&dev->children); child;
child = TAILQ_NEXT(child, link)) {
if (child->nameunit) {
if (!first) {
error = SYSCTL_OUT(req, ",", 1);
if (error) return error;
} else {
first = 0;
}
error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit));
if (error) return error;
}
}
error = SYSCTL_OUT(req, "", 1);
return error;
}
static int
sysctl_handle_state(SYSCTL_HANDLER_ARGS)
{
device_t dev = arg1;
switch (dev->state) {
case DS_NOTPRESENT:
return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
case DS_ALIVE:
return SYSCTL_OUT(req, "alive", sizeof("alive"));
case DS_ATTACHED:
return SYSCTL_OUT(req, "attached", sizeof("attached"));
case DS_BUSY:
return SYSCTL_OUT(req, "busy", sizeof("busy"));
}
return 0;
}
static void
device_register_oids(device_t dev)
{
struct sysctl_oid* oid;
oid = &dev->oid[0];
bzero(oid, sizeof(*oid));
oid->oid_parent = &sysctl__hw_devices_children;
oid->oid_number = OID_AUTO;
oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
oid->oid_arg1 = &dev->oidlist[0];
oid->oid_arg2 = 0;
oid->oid_name = dev->nameunit;
oid->oid_handler = 0;
oid->oid_fmt = "N";
SLIST_INIT(&dev->oidlist[0]);
sysctl_register_oid(oid);
oid = &dev->oid[1];
bzero(oid, sizeof(*oid));
oid->oid_parent = &dev->oidlist[0];
oid->oid_number = OID_AUTO;
oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
oid->oid_arg1 = dev->desc ? dev->desc : "";
oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
oid->oid_name = "desc";
oid->oid_handler = sysctl_handle_string;
oid->oid_fmt = "A";
sysctl_register_oid(oid);
oid = &dev->oid[2];
bzero(oid, sizeof(*oid));
oid->oid_parent = &dev->oidlist[0];
oid->oid_number = OID_AUTO;
oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
oid->oid_arg1 = dev;
oid->oid_arg2 = 0;
oid->oid_name = "children";
oid->oid_handler = sysctl_handle_children;
oid->oid_fmt = "A";
sysctl_register_oid(oid);
oid = &dev->oid[3];
bzero(oid, sizeof(*oid));
oid->oid_parent = &dev->oidlist[0];
oid->oid_number = OID_AUTO;
oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
oid->oid_arg1 = dev;
oid->oid_arg2 = 0;
oid->oid_name = "state";
oid->oid_handler = sysctl_handle_state;
oid->oid_fmt = "A";
sysctl_register_oid(oid);
}
static void
device_unregister_oids(device_t dev)
{
sysctl_unregister_oid(&dev->oid[0]);
sysctl_unregister_oid(&dev->oid[1]);
sysctl_unregister_oid(&dev->oid[2]);
}
#endif
/*======================================*/
/*
* Access functions for device resources.
*/
/* Runtime version */
static struct config_device *devtab;
static int devtab_count = 0;
static int
resource_new_name(const char *name, int unit)
{
struct config_device *new;
new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
if (new == NULL)
return -1;
if (devtab && devtab_count > 0)
bcopy(devtab, new, devtab_count * sizeof(*new));
bzero(&new[devtab_count], sizeof(*new));
new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_NOWAIT);
if (new[devtab_count].name == NULL) {
free(new, M_TEMP);
return -1;
}
strcpy(new[devtab_count].name, name);
new[devtab_count].unit = unit;
new[devtab_count].resource_count = 0;
new[devtab_count].resources = NULL;
devtab = new;
return devtab_count++;
}
static int
resource_new_resname(int j, const char *resname, resource_type type)
{
struct config_resource *new;
int i;
i = devtab[j].resource_count;
new = malloc((i + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
if (new == NULL)
return -1;
if (devtab[j].resources && i > 0)
bcopy(devtab[j].resources, new, i * sizeof(*new));
bzero(&new[i], sizeof(*new));
new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_NOWAIT);
if (new[i].name == NULL) {
free(new, M_TEMP);
return -1;
}
strcpy(new[i].name, resname);
new[i].type = type;
if (devtab[j].resources)
free(devtab[j].resources, M_TEMP);
devtab[j].resources = new;
devtab[j].resource_count = i + 1;
return i;
}
static int
resource_match_string(int i, const char *resname, const char *value)
{
int j;
struct config_resource *res;
for (j = 0, res = devtab[i].resources;
j < devtab[i].resource_count; j++, res++)
if (!strcmp(res->name, resname)
&& res->type == RES_STRING
&& !strcmp(res->u.stringval, value))
return j;
return -1;
}
static int
resource_find_hard(const char *cp, const char *name, int unit,
const char *resname, struct config_resource **result)
{
char match[256];
int matchlen;
char *op;
long val;
snprintf(match, sizeof(match), "hint.%s.%d.%s=", name, unit, resname);
matchlen = strlen(match);
while (cp) {
if (strncmp(match, cp, matchlen) == 0)
break;
while (*cp != '\0')
cp++;
cp++;
if (*cp == '\0') {
cp = NULL;
break;
}
}
if (cp)
cp += matchlen; /* skip over name and '=' */
else
return ENOENT;
val = strtoul(cp, &op, 0);
if (*cp != '\0' && *op == '\0') {
(*result)->type = RES_INT;
(*result)->u.intval = val;
} else {
(*result)->type = RES_STRING;
(*result)->u.stringval = cp;
}
return 0;
}
static int
resource_find(const char *name, int unit, const char *resname,
struct config_resource **result)
{
int i, j;
struct config_resource *res;
if (!hints_loaded) {
/* First specific, then generic. Dynamic over static. */
i = resource_find_hard(kern_envp, name, unit, resname, result);
if (i == 0)
return 0;
i = resource_find_hard(static_hints, name, unit, resname,
result);
if (i == 0)
return 0;
i = resource_find_hard(kern_envp, name, -1, resname, result);
if (i == 0)
return 0;
i = resource_find_hard(static_hints, name, -1, resname, result);
return i;
}
/*
* First check specific instances, then generic.
*/
for (i = 0; i < devtab_count; i++) {
if (devtab[i].unit < 0)
continue;
if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
res = devtab[i].resources;
for (j = 0; j < devtab[i].resource_count; j++, res++)
if (!strcmp(res->name, resname)) {
*result = res;
return 0;
}
}
}
for (i = 0; i < devtab_count; i++) {
if (devtab[i].unit >= 0)
continue;
if (!strcmp(devtab[i].name, name)) {
res = devtab[i].resources;
for (j = 0; j < devtab[i].resource_count; j++, res++)
if (!strcmp(res->name, resname)) {
*result = res;
return 0;
}
}
}
return ENOENT;
}
int
resource_int_value(const char *name, int unit, const char *resname, int *result)
{
int error;
struct config_resource *res;
if ((error = resource_find(name, unit, resname, &res)) != 0)
return error;
if (res->type != RES_INT)
return EFTYPE;
*result = res->u.intval;
return 0;
}
int
resource_long_value(const char *name, int unit, const char *resname,
long *result)
{
int error;
struct config_resource *res;
if ((error = resource_find(name, unit, resname, &res)) != 0)
return error;
if (res->type != RES_LONG)
return EFTYPE;
*result = res->u.longval;
return 0;
}
int
resource_string_value(const char *name, int unit, const char *resname,
char **result)
{
int error;
struct config_resource *res;
if ((error = resource_find(name, unit, resname, &res)) != 0)
return error;
if (res->type != RES_STRING)
return EFTYPE;
*result = res->u.stringval;
return 0;
}
int
resource_query_string(int i, const char *resname, const char *value)
{
if (i < 0)
i = 0;
else
i = i + 1;
for (; i < devtab_count; i++)
if (resource_match_string(i, resname, value) >= 0)
return i;
return -1;
}
int
resource_locate(int i, const char *resname)
{
if (i < 0)
i = 0;
else
i = i + 1;
for (; i < devtab_count; i++)
if (!strcmp(devtab[i].name, resname))
return i;
return -1;
}
int
resource_count(void)
{
return devtab_count;
}
char *
resource_query_name(int i)
{
return devtab[i].name;
}
int
resource_query_unit(int i)
{
return devtab[i].unit;
}
static int
resource_create(const char *name, int unit, const char *resname,
resource_type type, struct config_resource **result)
{
int i, j;
struct config_resource *res = NULL;
for (i = 0; i < devtab_count; i++) {
if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
res = devtab[i].resources;
break;
}
}
if (res == NULL) {
i = resource_new_name(name, unit);
if (i < 0)
return ENOMEM;
res = devtab[i].resources;
}
for (j = 0; j < devtab[i].resource_count; j++, res++) {
if (!strcmp(res->name, resname)) {
*result = res;
return 0;
}
}
j = resource_new_resname(i, resname, type);
if (j < 0)
return ENOMEM;
res = &devtab[i].resources[j];
*result = res;
return 0;
}
int
resource_set_int(const char *name, int unit, const char *resname, int value)
{
int error;
struct config_resource *res;
error = resource_create(name, unit, resname, RES_INT, &res);
if (error)
return error;
if (res->type != RES_INT)
return EFTYPE;
res->u.intval = value;
return 0;
}
int
resource_set_long(const char *name, int unit, const char *resname, long value)
{
int error;
struct config_resource *res;
error = resource_create(name, unit, resname, RES_LONG, &res);
if (error)
return error;
if (res->type != RES_LONG)
return EFTYPE;
res->u.longval = value;
return 0;
}
int
resource_set_string(const char *name, int unit, const char *resname,
const char *value)
{
int error;
struct config_resource *res;
error = resource_create(name, unit, resname, RES_STRING, &res);
if (error)
return error;
if (res->type != RES_STRING)
return EFTYPE;
if (res->u.stringval)
free(res->u.stringval, M_TEMP);
res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_NOWAIT);
if (res->u.stringval == NULL)
return ENOMEM;
strcpy(res->u.stringval, value);
return 0;
}
/*
* We use the identify routine to get the hints for all the other devices.
* Strings that are all digits or begin with 0x are integers.
*
* hint.aha.0.bus_speedup=1
* hint.aha.1.irq=10
* hint.wl.0.netid=PLUG
* hint.wl.1.netid=XYZZY
*/
static void
hint_load(char *cp)
{
char *ep, *op, *walker;
int len;
int val;
char name[20];
int unit;
char resname[255];
for (ep = cp; *ep != '=' && *ep != '\0'; ep++)
;
len = ep - cp;
if (*ep == '=')
ep++;
if (strncmp(cp, "hint.", 5) != 0)
return;
walker = cp;
walker += 5;
op = walker;
while (*walker && *walker != '.')
walker++;
if (*walker != '.')
return;
if (walker - op > sizeof(name))
return;
strncpy(name, op, walker - op);
name[walker - op] = '\0';
walker++;
op = walker;
while (*walker && *walker != '.')
walker++;
if (*walker != '.')
return;
unit = strtoul(op, &walker, 0);
if (*walker != '.')
return;
walker++;
op = walker;
while (*walker && *walker != '=')
walker++;
if (*walker != '=')
return;
if (walker - op > sizeof(resname))
return;
strncpy(resname, op, walker - op);
resname[walker - op] = '\0';
walker++;
if (walker != ep)
return;
if (bootverbose)
printf("Setting %s %d %s to ", name, unit, resname);
val = strtoul(ep, &op, 0);
if (*ep != '\0' && *op == '\0') {
resource_set_int(name, unit, resname, val);
if (bootverbose)
printf("%d (int)\n", val);
} else {
resource_set_string(name, unit, resname, ep);
if (bootverbose)
printf("%s (string)\n", ep);
}
}
static void
hints_load(void *dummy __unused)
{
char *cp;
cp = static_hints;
while (cp) {
hint_load(cp);
while (*cp != '\0')
cp++;
cp++;
if (*cp == '\0')
break;
}
cp = kern_envp;
while (cp) {
hint_load(cp);
while (*cp != '\0')
cp++;
cp++;
if (*cp == '\0')
break;
}
hints_loaded++;
}
SYSINIT(cfghints, SI_SUB_KMEM, SI_ORDER_ANY + 60, hints_load, 0)
/*======================================*/
/*
* Some useful method implementations to make life easier for bus drivers.
*/
void
resource_list_init(struct resource_list *rl)
{
SLIST_INIT(rl);
}
void
resource_list_free(struct resource_list *rl)
{
struct resource_list_entry *rle;
while ((rle = SLIST_FIRST(rl)) != NULL) {
if (rle->res)
panic("resource_list_free: resource entry is busy");
SLIST_REMOVE_HEAD(rl, link);
free(rle, M_BUS);
}
}
void
resource_list_add(struct resource_list *rl,
int type, int rid,
u_long start, u_long end, u_long count)
{
struct resource_list_entry *rle;
rle = resource_list_find(rl, type, rid);
if (!rle) {
rle = malloc(sizeof(struct resource_list_entry), M_BUS, M_NOWAIT);
if (!rle)
panic("resource_list_add: can't record entry");
SLIST_INSERT_HEAD(rl, rle, link);
rle->type = type;
rle->rid = rid;
rle->res = NULL;
}
if (rle->res)
panic("resource_list_add: resource entry is busy");
rle->start = start;
rle->end = end;
rle->count = count;
}
struct resource_list_entry*
resource_list_find(struct resource_list *rl,
int type, int rid)
{
struct resource_list_entry *rle;
SLIST_FOREACH(rle, rl, link)
if (rle->type == type && rle->rid == rid)
return rle;
return NULL;
}
void
resource_list_delete(struct resource_list *rl,
int type, int rid)
{
struct resource_list_entry *rle = resource_list_find(rl, type, rid);
if (rle) {
SLIST_REMOVE(rl, rle, resource_list_entry, link);
free(rle, M_BUS);
}
}
struct resource *
resource_list_alloc(struct resource_list *rl,
device_t bus, device_t child,
int type, int *rid,
u_long start, u_long end,
u_long count, u_int flags)
{
struct resource_list_entry *rle = 0;
int passthrough = (device_get_parent(child) != bus);
int isdefault = (start == 0UL && end == ~0UL);
if (passthrough) {
return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
type, rid,
start, end, count, flags);
}
rle = resource_list_find(rl, type, *rid);
if (!rle)
return 0; /* no resource of that type/rid */
if (rle->res)
panic("resource_list_alloc: resource entry is busy");
if (isdefault) {
start = rle->start;
count = max(count, rle->count);
end = max(rle->end, start + count - 1);
}
rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
type, rid, start, end, count, flags);
/*
* Record the new range.
*/
if (rle->res) {
rle->start = rman_get_start(rle->res);
rle->end = rman_get_end(rle->res);
rle->count = count;
}
return rle->res;
}
int
resource_list_release(struct resource_list *rl,
device_t bus, device_t child,
int type, int rid, struct resource *res)
{
struct resource_list_entry *rle = 0;
int passthrough = (device_get_parent(child) != bus);
int error;
if (passthrough) {
return BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
type, rid, res);
}
rle = resource_list_find(rl, type, rid);
if (!rle)
panic("resource_list_release: can't find resource");
if (!rle->res)
panic("resource_list_release: resource entry is not busy");
error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
type, rid, res);
if (error)
return error;
rle->res = NULL;
return 0;
}
/*
* Call DEVICE_IDENTIFY for each driver.
*/
int
bus_generic_probe(device_t dev)
{
devclass_t dc = dev->devclass;
driverlink_t dl;
for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link))
DEVICE_IDENTIFY(dl->driver, dev);
return 0;
}
int
bus_generic_attach(device_t dev)
{
device_t child;
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link))
device_probe_and_attach(child);
return 0;
}
int
bus_generic_detach(device_t dev)
{
device_t child;
int error;
if (dev->state != DS_ATTACHED)
return EBUSY;
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link))
if ((error = device_detach(child)) != 0)
return error;
return 0;
}
int
bus_generic_shutdown(device_t dev)
{
device_t child;
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link))
device_shutdown(child);
return 0;
}
int
bus_generic_suspend(device_t dev)
{
int error;
device_t child, child2;
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link)) {
error = DEVICE_SUSPEND(child);
if (error) {
for (child2 = TAILQ_FIRST(&dev->children);
child2 && child2 != child;
child2 = TAILQ_NEXT(child2, link))
DEVICE_RESUME(child2);
return (error);
}
}
return 0;
}
int
bus_generic_resume(device_t dev)
{
device_t child;
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link)) {
DEVICE_RESUME(child);
/* if resume fails, there's nothing we can usefully do... */
}
return 0;
}
int
bus_print_child_header (device_t dev, device_t child)
{
int retval = 0;
if (device_get_desc(child)) {
retval += device_printf(child, "<%s>",
device_get_desc(child));
} else {
retval += printf("%s", device_get_nameunit(child));
}
return (retval);
}
int
bus_print_child_footer (device_t dev, device_t child)
{
return(printf(" on %s\n", device_get_nameunit(dev)));
}
int
bus_generic_print_child(device_t dev, device_t child)
{
int retval = 0;
retval += bus_print_child_header(dev, child);
retval += bus_print_child_footer(dev, child);
return (retval);
}
int
bus_generic_read_ivar(device_t dev, device_t child, int index,
uintptr_t * result)
{
return ENOENT;
}
int
bus_generic_write_ivar(device_t dev, device_t child, int index,
uintptr_t value)
{
return ENOENT;
}
void
bus_generic_driver_added(device_t dev, driver_t *driver)
{
device_t child;
DEVICE_IDENTIFY(driver, dev);
for (child = TAILQ_FIRST(&dev->children);
child; child = TAILQ_NEXT(child, link))
if (child->state == DS_NOTPRESENT)
device_probe_and_attach(child);
}
int
bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
int flags, driver_intr_t *intr, void *arg,
void **cookiep)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
intr, arg, cookiep));
else
return (EINVAL);
}
int
bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
void *cookie)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
else
return (EINVAL);
}
struct resource *
bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
u_long start, u_long end, u_long count, u_int flags)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
start, end, count, flags));
else
return (NULL);
}
int
bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
struct resource *r)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
r));
else
return (EINVAL);
}
int
bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
struct resource *r)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
r));
else
return (EINVAL);
}
int
bus_generic_deactivate_resource(device_t dev, device_t child, int type,
int rid, struct resource *r)
{
/* Propagate up the bus hierarchy until someone handles it. */
if (dev->parent)
return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
r));
else
return (EINVAL);
}
/*
* Some convenience functions to make it easier for drivers to use the
* resource-management functions. All these really do is hide the
* indirection through the parent's method table, making for slightly
* less-wordy code. In the future, it might make sense for this code
* to maintain some sort of a list of resources allocated by each device.
*/
struct resource *
bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
u_long count, u_int flags)
{
if (dev->parent == 0)
return (0);
return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
count, flags));
}
int
bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
{
if (dev->parent == 0)
return (EINVAL);
return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
}
int
bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
{
if (dev->parent == 0)
return (EINVAL);
return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
}
int
bus_release_resource(device_t dev, int type, int rid, struct resource *r)
{
if (dev->parent == 0)
return (EINVAL);
return (BUS_RELEASE_RESOURCE(dev->parent, dev,
type, rid, r));
}
int
bus_setup_intr(device_t dev, struct resource *r, int flags,
driver_intr_t handler, void *arg, void **cookiep)
{
if (dev->parent == 0)
return (EINVAL);
return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
handler, arg, cookiep));
}
int
bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
{
if (dev->parent == 0)
return (EINVAL);
return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
}
int
bus_set_resource(device_t dev, int type, int rid,
u_long start, u_long count)
{
return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
start, count);
}
int
bus_get_resource(device_t dev, int type, int rid,
u_long *startp, u_long *countp)
{
return BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
startp, countp);
}
u_long
bus_get_resource_start(device_t dev, int type, int rid)
{
u_long start, count;
int error;
error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
&start, &count);
if (error)
return 0;
return start;
}
u_long
bus_get_resource_count(device_t dev, int type, int rid)
{
u_long start, count;
int error;
error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
&start, &count);
if (error)
return 0;
return count;
}
void
bus_delete_resource(device_t dev, int type, int rid)
{
BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
}
static int
root_print_child(device_t dev, device_t child)
{
return (0);
}
static int
root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
void **cookiep)
{
/*
* If an interrupt mapping gets to here something bad has happened.
*/
panic("root_setup_intr");
}
static kobj_method_t root_methods[] = {
/* Device interface */
KOBJMETHOD(device_shutdown, bus_generic_shutdown),
KOBJMETHOD(device_suspend, bus_generic_suspend),
KOBJMETHOD(device_resume, bus_generic_resume),
/* Bus interface */
KOBJMETHOD(bus_print_child, root_print_child),
KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
KOBJMETHOD(bus_setup_intr, root_setup_intr),
{ 0, 0 }
};
static driver_t root_driver = {
"root",
root_methods,
1, /* no softc */
};
device_t root_bus;
devclass_t root_devclass;
static int
root_bus_module_handler(module_t mod, int what, void* arg)
{
switch (what) {
case MOD_LOAD:
kobj_class_compile((kobj_class_t) &root_driver);
root_bus = make_device(NULL, "root", 0);
root_bus->desc = "System root bus";
kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
root_bus->driver = &root_driver;
root_bus->state = DS_ATTACHED;
root_devclass = devclass_find_internal("root", FALSE);
return 0;
case MOD_SHUTDOWN:
device_shutdown(root_bus);
return 0;
}
return 0;
}
static moduledata_t root_bus_mod = {
"rootbus",
root_bus_module_handler,
0
};
DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
void
root_bus_configure(void)
{
device_t dev;
PDEBUG(("."));
for (dev = TAILQ_FIRST(&root_bus->children); dev;
dev = TAILQ_NEXT(dev, link)) {
device_probe_and_attach(dev);
}
}
int
driver_module_handler(module_t mod, int what, void *arg)
{
int error, i;
struct driver_module_data *dmd;
devclass_t bus_devclass;
dmd = (struct driver_module_data *)arg;
bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
error = 0;
switch (what) {
case MOD_LOAD:
if (dmd->dmd_chainevh)
error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
PDEBUG(("Loading module: driver %s on bus %s",
DRIVERNAME(dmd->dmd_drivers[i]),
dmd->dmd_busname));
error = devclass_add_driver(bus_devclass,
dmd->dmd_drivers[i]);
}
if (error)
break;
/*
* The drivers loaded in this way are assumed to all
* implement the same devclass.
*/
*dmd->dmd_devclass =
devclass_find_internal(dmd->dmd_drivers[0]->name,
TRUE);
break;
case MOD_UNLOAD:
for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
PDEBUG(("Unloading module: driver %s from bus %s",
DRIVERNAME(dmd->dmd_drivers[i]),
dmd->dmd_busname));
error = devclass_delete_driver(bus_devclass,
dmd->dmd_drivers[i]);
}
if (!error && dmd->dmd_chainevh)
error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
break;
}
return (error);
}
#ifdef BUS_DEBUG
/* the _short versions avoid iteration by not calling anything that prints
* more than oneliners. I love oneliners.
*/
static void
print_device_short(device_t dev, int indent)
{
if (!dev)
return;
indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
dev->unit, dev->desc,
(dev->parent? "":"no "),
(TAILQ_EMPTY(&dev->children)? "no ":""),
(dev->flags&DF_ENABLED? "enabled,":"disabled,"),
(dev->flags&DF_FIXEDCLASS? "fixed,":""),
(dev->flags&DF_WILDCARD? "wildcard,":""),
(dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
(dev->ivars? "":"no "),
(dev->softc? "":"no "),
dev->busy));
}
static void
print_device(device_t dev, int indent)
{
if (!dev)
return;
print_device_short(dev, indent);
indentprintf(("Parent:\n"));
print_device_short(dev->parent, indent+1);
indentprintf(("Driver:\n"));
print_driver_short(dev->driver, indent+1);
indentprintf(("Devclass:\n"));
print_devclass_short(dev->devclass, indent+1);
}
void
print_device_tree_short(device_t dev, int indent)
/* print the device and all its children (indented) */
{
device_t child;
if (!dev)
return;
print_device_short(dev, indent);
for (child = TAILQ_FIRST(&dev->children); child;
child = TAILQ_NEXT(child, link))
print_device_tree_short(child, indent+1);
}
void
print_device_tree(device_t dev, int indent)
/* print the device and all its children (indented) */
{
device_t child;
if (!dev)
return;
print_device(dev, indent);
for (child = TAILQ_FIRST(&dev->children); child;
child = TAILQ_NEXT(child, link))
print_device_tree(child, indent+1);
}
static void
print_driver_short(driver_t *driver, int indent)
{
if (!driver)
return;
indentprintf(("driver %s: softc size = %d\n",
driver->name, driver->size));
}
static void
print_driver(driver_t *driver, int indent)
{
if (!driver)
return;
print_driver_short(driver, indent);
}
static void
print_driver_list(driver_list_t drivers, int indent)
{
driverlink_t driver;
for (driver = TAILQ_FIRST(&drivers); driver;
driver = TAILQ_NEXT(driver, link))
print_driver(driver->driver, indent);
}
static void
print_devclass_short(devclass_t dc, int indent)
{
if ( !dc )
return;
indentprintf(("devclass %s: max units = %d\n",
dc->name, dc->maxunit));
}
static void
print_devclass(devclass_t dc, int indent)
{
int i;
if ( !dc )
return;
print_devclass_short(dc, indent);
indentprintf(("Drivers:\n"));
print_driver_list(dc->drivers, indent+1);
indentprintf(("Devices:\n"));
for (i = 0; i < dc->maxunit; i++)
if (dc->devices[i])
print_device(dc->devices[i], indent+1);
}
void
print_devclass_list_short(void)
{
devclass_t dc;
printf("Short listing of devclasses, drivers & devices:\n");
for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
print_devclass_short(dc, 0);
}
void
print_devclass_list(void)
{
devclass_t dc;
printf("Full listing of devclasses, drivers & devices:\n");
for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
print_devclass(dc, 0);
}
#endif
|