summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/topo/modules/common/usb/topo_usb.c
blob: 90553aba9724cdd1a69b96a1ef8c18701c2d07e5 (plain)
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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright (c) 2019, Joyent, Inc.
 */

/*
 * The purpose of this module is to build topology information for USB devices.
 * USB devices are more complicated to build topology information for, as there
 * are multiple sources of information needed to correctly understand the
 * topology, and the way they present themselves is not always straightforward.
 *
 * We enumerate two different types of devices:
 *
 *   o USB ports
 *   o USB devices
 *
 * A USB port represents a logical port, while a USB device represents an actual
 * device that's been plugged in. If a device is a hub, then we'll enumerate
 * that device as well.
 *
 * Now, some basics. There are several different USB controllers that exist in
 * the system. Some are part of the chipset, while others may be present via
 * add-on cards. The system interfaces initially with USB devices through a host
 * controller. Prior to USB 3.0/xhci, a single controller only supported a
 * single protocol. With USB 3.0, it is possible for a port to share wiring with
 * both USB 2.0 devices and USB 3.0 devices. However, to the host controller
 * this appears as two different logical ports.
 *
 * To make matters worse, during the transition to USB 3, the ports that were
 * controlled could be routed to and from a USB 2 controller to a USB 3
 * controller. This means that there are a lot of ways for ports to overlap.
 *
 * In the first case, controllers define a way to perform this mapping by
 * leveraging ACPI information. Of course, this only helps us if the platform
 * provides ACPI information, which it may not. When we do know that two ports
 * are actually the same port, either because of ACPI or because of a
 * product-specific mapping file, then we'll use that to say two ports are the
 * same. Otherwise, we'll enumerate them as two separate logical ports.
 *
 * To perform the actual enumeration, the first time we're asked to enumerate a
 * node, we go through and put together an entire picture of all of the USB
 * devices in the system. This is done so we can make sure to enumerate devices
 * under specific devices.  The actual topology is determined in a few different
 * passes.
 *
 * Before we walk any trees, we look to see if we have a topo USB metadata file
 * and if present, load it. However, we do not apply any information from it.
 *
 * The first pass uses the devinfo tree to determine all of the USB controllers
 * and devices that are in the system. We use properties in the devices tree to
 * identify whether items are a root hub. When a root hub is found, we walk all
 * of its children and make a note of all of the logical ports under it.
 *
 * Next, we walk the information provided by ACPI to try and reduplicate
 * information about the ports on the system. If the USB topology metadata tells
 * us that we should not skip ACPI, then we use it. This is done by walking the
 * /devices/fw tree, looking for USB nodes and then linking them to their
 * corresponding entries found from the first devinfo walk.
 *
 * Finally, we go back and apply metadata to ports that match.
 *
 *
 * To logically keep track of all of this, we have several different structures:
 *
 *  topo_usb_controller_t  - Represents a physical controller.
 *  topo_usb_port_t	   - Represents a physical port. This is a synthetic
 *			     construct that we put together based on ACPI
 *			     information.
 *  topo_usb_lport_t	   - Represents a logical port. This is what the OS
 *			     actually detects and sees. Each logical port
 *			     belongs to a corresponding topo_usb_port_t.
 *  topo_usb_t		   - Represents the overall topology enumeration state.
 *
 *
 * This topo module is invoked at three different points by the surrounding code
 * and logic. Specifically:
 *
 *   * Dynamically by the pcibus enumerator when we encounter PCI add on cards
 *     which are present in a physical slot. Traditional chipset devices are not
 *     considered a part of this.
 *
 *   * Statically under the motherboard. All ports that don't belong to a PCI
 *     device are assumed to belong under the motherboard, unless a
 *     platform-specific topology map maps them under the chassis.
 *
 *   * Statically under the chassis. Ports are only placed under the chassis if
 *     a platform-specific topology file indicates that the port is a part of
 *     the chassis.
 */

#include <libdevinfo.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/debug.h>
#include <unistd.h>

#include <sys/fm/protocol.h>
#include <fm/topo_mod.h>
#include <fm/topo_list.h>
#include <fm/topo_method.h>

#include <topo_port.h>

#include "topo_usb.h"
#include "topo_usb_int.h"

typedef enum topo_usb_type {
	TOPO_USB_PCI,
	TOPO_USB_MOBO,
	TOPO_USB_CHASSIS
} topo_usb_type_t;

typedef enum topo_usb_cdrv {
	TOPO_USB_D_UNKNOWN,
	TOPO_USB_D_UHCI,
	TOPO_USB_D_OHCI,
	TOPO_USB_D_EHCI,
	TOPO_USB_D_XHCI
} topo_usb_cdrv_t;

typedef enum topo_usb_protocol {
	TOPO_USB_P_UNKNOWN,
	TOPO_USB_P_1x,
	TOPO_USB_P_20,
	TOPO_USB_P_30,
	TOPO_USB_P_31
} topo_usb_protocol_t;

typedef enum topo_usb_port_connected {
	TOPO_USB_C_UNKNOWN,
	TOPO_USB_C_DISCONNECTED,
	TOPO_USB_C_CONNECTED
} topo_usb_port_connected_t;

typedef struct topo_usb_port {
	topo_list_t	tup_link;
	uint_t		tup_nlports;
	topo_list_t	tup_lports;
	boolean_t	tup_pld_valid;
	acpi_pld_info_t	tup_pld;
	uint_t		tup_port_type;
	topo_usb_port_connected_t	tup_port_connected;
	topo_usb_meta_port_t	*tup_meta;
} topo_usb_port_t;

typedef struct topo_usb_lport {
	topo_list_t		tul_link;
	uint_t			tul_portno;
	topo_usb_protocol_t	tul_protocol;
	di_node_t		tul_device;
	di_node_t		tul_acpi_device;
	topo_usb_port_t		*tul_port;
	uint_t			tul_nhubd_ports;
	uint_t			tul_nports;
	topo_list_t		tul_ports;
	char			tul_name[PATH_MAX];
	const char		*tul_acpi_name;
} topo_usb_lport_t;

typedef struct topo_usb_controller {
	topo_list_t	tuc_link;
	di_node_t	tuc_devinfo;
	char		*tuc_path;
	char		*tuc_acpi_path;
	char		tuc_name[PATH_MAX];
	topo_usb_cdrv_t	tuc_driver;
	/*
	 * Number of actual ports we've created (some of the logical ports are
	 * deduped).
	 */
	uint_t		tuc_nports;
	topo_list_t	tuc_ports;
	/*
	 * Total number of logical ports we expect to exist on this controller.
	 * This may be greater than the number of actual ports we've created
	 * under it because some physical ports represent more than one logical
	 * port (xhci with USB2/3).
	 */
	uint_t		tuc_nhubd_ports;
	/*
	 * Keep track of port number and offset information. This is only done
	 * for xhci.
	 */
	uint_t		tuc_nusb20;
	uint_t		tuc_fusb20;
	uint_t		tuc_nusb30;
	uint_t		tuc_fusb30;
	uint_t		tuc_nusb31;
	uint_t		tuc_fusb31;
	boolean_t	tuc_enumed;
} topo_usb_controller_t;

typedef struct topo_usb {
	topo_list_t	tu_controllers;
	boolean_t	tu_enum_done;
	di_node_t	tu_devinfo;
	topo_list_t	tu_metadata;
	topo_usb_meta_flags_t	tu_meta_flags;
	topo_list_t	tu_chassis_ports;
	uint_t		tu_nchassis_ports;
} topo_usb_t;

typedef struct topo_usb_devcfg_arg {
	topo_usb_t	*tda_usb;
	topo_mod_t	*tda_mod;
	boolean_t	tda_fatal;
} topo_usb_devcfg_arg_t;

static const topo_pgroup_info_t topo_usb_port_pgroup = {
	TOPO_PGROUP_USB_PORT,
	TOPO_STABILITY_PRIVATE,
	TOPO_STABILITY_PRIVATE,
	1
};

static const topo_pgroup_info_t topo_io_pgroup = {
	TOPO_PGROUP_IO,
	TOPO_STABILITY_PRIVATE,
	TOPO_STABILITY_PRIVATE,
	1
};

static const topo_pgroup_info_t topo_binding_pgroup = {
	TOPO_PGROUP_BINDING,
	TOPO_STABILITY_PRIVATE,
	TOPO_STABILITY_PRIVATE,
	1
};

static const topo_pgroup_info_t topo_usb_props_pgroup = {
	TOPO_PGROUP_USB_PROPS,
	TOPO_STABILITY_PRIVATE,
	TOPO_STABILITY_PRIVATE,
	1
};

/* Required forwards */
static int topo_usb_enum_device(topo_mod_t *, tnode_t *, topo_usb_port_t *);

/*
 * Defines the maximum number of USB ports that can exist. Ports are basically
 * defined by a uint8_t, meaning that we can go up to UINT8_MAX inclusively.
 */
#define	USB_TOPO_PORT_MAX	256

/*
 * Default value to indicate that a USB port has no valid type.
 */
#define	USB_TOPO_PORT_TYPE_DEFAULT	0xff

/*
 * These come from the ACPI 6.2 / Table 9-290 UPC Return Package Values.
 */
static const char *
topo_usb_port_type_to_string(int type)
{
	switch (type) {
	case 0x00:
		return ("Type A connector");
	case 0x01:
		return ("Mini-AB connector");
	case 0x02:
		return ("ExpressCard");
	case 0x03:
		return ("USB 3 Standard-A connector");
	case 0x04:
		return ("USB 3 Standard-B connector");
	case 0x05:
		return ("USB 3 Micro-B connector");
	case 0x06:
		return ("USB 3 Micro-AB connector");
	case 0x07:
		return ("USB 3 Power-B connector");
	case 0x08:
		return ("Type C connector - USB2-only");
	case 0x09:
		return ("Type C connector - USB2 and SS with Switch");
	case 0x0A:
		return ("Type C connector - USB2 and SS without Switch");
	/* 0x0B->0xFE are reserved. Treat them like 0xFF */
	case 0xFF:
	default:
		return ("Unknown");
	}
}

/*
 * Searches the list of ports at a given layer (not recursively) for the
 * specific port id.
 */
static topo_usb_lport_t *
topo_usb_lport_find(topo_list_t *plist, uint_t logid)
{
	topo_usb_port_t *p;

	for (p = topo_list_next(plist); p != NULL; p = topo_list_next(p)) {
		topo_usb_lport_t *l;

		for (l = topo_list_next(&p->tup_lports); l != NULL;
		    l = topo_list_next(l)) {
			if (l->tul_portno == logid)
				return (l);
		}
	}
	return (NULL);
}

/*
 * Create an instance of a controller and seed the basic information.
 */
static topo_usb_controller_t *
topo_usb_controller_create(topo_mod_t *mod, topo_usb_t *usb, di_node_t node)
{
	int *pcount, inst;
	char *drvname, *acpi;
	topo_usb_controller_t *c;

	/*
	 * If we can't get the port count or the driver, then this node is
	 * uninteresting.
	 */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "usb-port-count",
	    &pcount) != 1) {
		return (NULL);
	}

	if ((drvname = di_driver_name(node)) == NULL ||
	    (inst = di_instance(node) == -1))
		return (NULL);

	if ((c = topo_mod_zalloc(mod, sizeof (topo_usb_controller_t))) ==
	    NULL || *pcount <= 0) {
		return (NULL);
	}

	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "acpi-namespace",
	    &acpi) == 1) {
		c->tuc_acpi_path = acpi;
	}

	c->tuc_nhubd_ports = (uint_t)*pcount;
	c->tuc_devinfo = node;
	c->tuc_path = di_devfs_path(node);
	(void) snprintf(c->tuc_name, sizeof (c->tuc_name), "%s%d", drvname,
	    inst);
	if (strcmp(drvname, "xhci") == 0) {
		int *p;

		c->tuc_driver = TOPO_USB_D_XHCI;

		/*
		 * Grab the properties that we need so we can better do a port
		 * speed mapping.
		 */
		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb2.0-port-count", &p) == 1 && *p > 0) {
			c->tuc_nusb20 = (uint_t)*p;
		}

		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb2.0-first-port", &p) == 1 && *p > 0) {
			c->tuc_fusb20 = (uint_t)*p;
		}

		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb3.0-port-count", &p) == 1 && *p > 0) {
			c->tuc_nusb30 = (uint_t)*p;
		}

		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb3.0-first-port", &p) == 1 && *p > 0) {
			c->tuc_fusb30 = (uint_t)*p;
		}

		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb3.1-port-count", &p) == 1 && *p > 0) {
			c->tuc_nusb31 = (uint_t)*p;
		}

		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
		    "usb3.1-first-port", &p) == 1 && *p > 0) {
			c->tuc_fusb31 = (uint_t)*p;
		}
	} else if (strcmp(drvname, "ehci") == 0) {
		c->tuc_driver = TOPO_USB_D_EHCI;
	} else if (strcmp(drvname, "uhci") == 0) {
		c->tuc_driver = TOPO_USB_D_UHCI;
	} else if (strcmp(drvname, "ohci") == 0) {
		c->tuc_driver = TOPO_USB_D_OHCI;
	} else {
		c->tuc_driver = TOPO_USB_D_UNKNOWN;
	}
	topo_list_append(&usb->tu_controllers, c);
	topo_mod_dprintf(mod, "created new USB controller at %s", c->tuc_path);

	return (c);
}

/*
 * Process this port and any others that might exist.
 */
static boolean_t
topo_usb_gather_acpi_port(topo_mod_t *mod, topo_usb_t *usb, topo_list_t *plist,
    uint_t *nports, topo_usb_controller_t *tuc, di_node_t portinfo)
{
	int64_t *portno;
	uchar_t *loc;
	int loclen, *type;
	char *acpi;
	acpi_pld_info_t pld;
	boolean_t pld_valid = B_FALSE;
	topo_usb_port_t *port = NULL;
	topo_usb_lport_t *lport;
	di_node_t child;

	/*
	 * Get the port's address, it's a required value. Because this is coming
	 * from firmware, we cannot trust the port's value to be correct.
	 */
	if (di_prop_lookup_int64(DDI_DEV_T_ANY, portinfo, "acpi-address",
	    &portno) != 1 || *portno < 1 || *portno >= USB_TOPO_PORT_MAX) {
		return (B_FALSE);
	}

	if (di_prop_lookup_strings(DDI_DEV_T_ANY, portinfo, "acpi-namespace",
	    &acpi) != 1) {
		return (B_FALSE);
	}

	/*
	 * Check to see if we have any ACPI location information. If we do, we
	 * can decode it.
	 */
	if ((loclen = di_prop_lookup_bytes(DDI_DEV_T_ANY, portinfo,
	    "acpi-physical-location", &loc)) >= ACPI_PLD_REV1_BUFFER_SIZE &&
	    usbtopo_decode_pld(loc, loclen, &pld)) {
		pld_valid = B_TRUE;
	}

	/*
	 * Find the corresponding lport. If this node doesn't happen to match
	 * something we've enumerated from the hub. Warn about that fact and
	 * consider this bad data.
	 */
	lport = topo_usb_lport_find(plist, (uint_t)*portno);
	if (lport == NULL) {
		topo_mod_dprintf(mod, "failed to find physical usb port for "
		    "%s/%u", acpi, (uint_t)*portno);
		return (B_TRUE);
	}

	if (lport->tul_acpi_device != DI_NODE_NIL) {
		topo_mod_dprintf(mod, "logical port already bound to %s, not "
		    "binding to %s", lport->tul_acpi_name, acpi);
		return (B_FALSE);
	}

	lport->tul_acpi_device = portinfo;
	lport->tul_acpi_name = acpi;
	port = lport->tul_port;

	if (pld_valid) {
		port->tup_pld_valid = B_TRUE;
		port->tup_pld = pld;
	}

	if (di_prop_lookup_ints(DDI_DEV_T_ANY, portinfo, "usb-port-type",
	    &type) == 1 && *type >= 0) {
		port->tup_port_type = *type;
	} else {
		port->tup_port_type = USB_TOPO_PORT_TYPE_DEFAULT;
	}

	if (di_prop_find(DDI_DEV_T_ANY, portinfo,
	    "usb-port-connectable") != DI_PROP_NIL) {
		port->tup_port_connected = TOPO_USB_C_CONNECTED;
	} else {
		port->tup_port_connected = TOPO_USB_C_DISCONNECTED;
	}

	for (child = di_child_node(portinfo); child != NULL;
	    child = di_sibling_node(child)) {
		const char *pname;

		pname = di_node_name(child);
		if (pname == NULL || strcmp(pname, "port") != 0) {
			continue;
		}

		if (!topo_usb_gather_acpi_port(mod, usb, &lport->tul_ports,
		    &lport->tul_nports, tuc, child)) {
			return (B_FALSE);
		}
	}

	topo_mod_dprintf(mod, "discovered %u ACPI usb child ports",
	    lport->tul_nports);

	return (B_TRUE);
}

/*
 * First, bootstrap all of our information by reading the ACPI information
 * exposed in the devinfo tree. All of the nodes we care about will be under
 * /fw/sb@XX/usbrootub@YYY/port@ZZZ
 */
static boolean_t
topo_usb_gather_acpi(topo_mod_t *mod, topo_usb_t *usb)
{
	di_node_t fwroot, sbnode;

	/*
	 * If we can't find the /fw node, that's fine. We may not have any ACPI
	 * information on the system.
	 */
	fwroot = di_lookup_node(usb->tu_devinfo, "/fw");
	if (fwroot == DI_NODE_NIL)
		return (B_TRUE);

	for (sbnode = di_child_node(fwroot); sbnode != DI_NODE_NIL;
	    sbnode = di_sibling_node(sbnode)) {
		const char *sbname;
		di_node_t hub;

		sbname = di_node_name(sbnode);
		if (sbname == NULL || strcmp(sbname, "sb") != 0) {
			continue;
		}

		for (hub = di_child_node(sbnode); hub != DI_NODE_NIL;
		    hub = di_sibling_node(hub)) {
			const char *hubname;
			char *acpi;
			topo_usb_controller_t *tuc;
			di_node_t port;

			hubname = di_node_name(hub);
			if (hubname == NULL ||
			    strcmp(hubname, "usbroothub") != 0) {
				continue;
			}

			if (di_prop_lookup_strings(DDI_DEV_T_ANY, hub,
			    "acpi-controller-name", &acpi) != 1) {
				continue;
			}

			for (tuc = topo_list_next(&usb->tu_controllers);
			    tuc != NULL;
			    tuc = topo_list_next(tuc)) {
				if (tuc->tuc_acpi_path != NULL &&
				    strcmp(acpi, tuc->tuc_acpi_path) == 0)
					break;
			}

			if (tuc == NULL) {
				topo_mod_dprintf(mod, "failed to find USB "
				    "controller for ACPI path %s", acpi);
				continue;
			}

			for (port = di_child_node(hub); port != NULL;
			    port = di_sibling_node(port)) {
				const char *pname;

				pname = di_node_name(port);
				if (pname == NULL ||
				    strcmp(pname, "port") != 0) {
					continue;
				}

				if (!topo_usb_gather_acpi_port(mod, usb,
				    &tuc->tuc_ports, &tuc->tuc_nports, tuc,
				    port)) {
					return (B_FALSE);
				}
			}

			topo_mod_dprintf(mod, "found ACPI usb controller %s "
			    "with %d top-level ports", tuc->tuc_path,
			    tuc->tuc_nports);
		}
	}

	return (B_TRUE);
}

static topo_usb_port_t *
topo_usb_port_create(topo_mod_t *mod, uint_t portno, const char *parent,
    char sep)
{
	topo_usb_lport_t *l;
	topo_usb_port_t *p;

	if ((l = topo_mod_zalloc(mod, sizeof (topo_usb_lport_t))) == NULL) {
		return (NULL);
	}
	l->tul_portno = portno;
	if (snprintf(l->tul_name, sizeof (l->tul_name), "%s%c%u", parent, sep,
	    portno) >= sizeof (l->tul_name)) {
		topo_mod_free(mod, l, sizeof (topo_usb_lport_t));
		return (NULL);
	}

	if ((p = topo_mod_zalloc(mod, sizeof (topo_usb_port_t))) == NULL) {
		topo_mod_free(mod, l, sizeof (topo_usb_lport_t));
		return (NULL);
	}
	l->tul_port = p;
	p->tup_port_type = USB_TOPO_PORT_TYPE_DEFAULT;
	topo_list_append(&p->tup_lports, l);
	p->tup_nlports++;

	return (p);
}

/*
 * Set the protocol of a port that belongs to a root hub.
 */
static void
topo_usb_set_rhub_port_protocol(topo_mod_t *mod, topo_usb_controller_t *tuc,
    topo_usb_lport_t *lport)
{
	switch (tuc->tuc_driver) {
	case TOPO_USB_D_XHCI:
		break;
	case TOPO_USB_D_UHCI:
	case TOPO_USB_D_OHCI:
		lport->tul_protocol = TOPO_USB_P_1x;
		return;
	case TOPO_USB_D_EHCI:
		lport->tul_protocol = TOPO_USB_P_20;
		return;
	case TOPO_USB_D_UNKNOWN:
	default:
		lport->tul_protocol = TOPO_USB_P_UNKNOWN;
		return;
	}

	/*
	 * The xHCI controller can support multiple different, protocols. It
	 * communicates this information to us via devinfo properties. It's
	 * possible that a port that is within max ports is not within the range
	 * here. If that's the case, we'll set it to unknown.
	 */
	if (lport->tul_portno >= tuc->tuc_fusb20 &&
	    lport->tul_portno < tuc->tuc_fusb20 + tuc->tuc_nusb20) {
		lport->tul_protocol = TOPO_USB_P_20;
	} else if (lport->tul_portno >= tuc->tuc_fusb30 &&
	    lport->tul_portno < tuc->tuc_fusb30 + tuc->tuc_nusb30) {
		lport->tul_protocol = TOPO_USB_P_30;
	} else if (lport->tul_portno >= tuc->tuc_fusb31 &&
	    lport->tul_portno < tuc->tuc_fusb31 + tuc->tuc_nusb31) {
		lport->tul_protocol = TOPO_USB_P_31;
	} else {
		lport->tul_protocol = TOPO_USB_P_UNKNOWN;
	}
}

/*
 * We've found a node on the list. Attempt to find its corresponding port. If we
 * find a hub, then we will descend further down this part of the tree.
 */
static int
topo_usb_gather_devcfg_port(topo_mod_t *mod, topo_usb_controller_t *c,
    topo_list_t *plist, di_node_t node)
{
	int *vend, *reg, *nports;
	topo_usb_lport_t *l;
	char *drvname;

	/*
	 * Look for the presence of the usb-vendor-id property to determine
	 * whether or not this is a usb device node. usba always adds this
	 * to the devices that it enumerates.
	 */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "usb-vendor-id",
	    &vend) != 1) {
		topo_mod_dprintf(mod, "failed to find usb-vendor-id property "
		    "for child");
		return (0);
	}

	/*
	 * For usb-devices, the reg property is one entry long and it has the
	 * logical port that the controller sees.
	 */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &reg) != 1 ||
	    *reg <= 0) {
		topo_mod_dprintf(mod, "got bad \"reg\" property");
		return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
	}

	if ((l = topo_usb_lport_find(plist, (uint_t)*reg)) == NULL) {
		topo_mod_dprintf(mod, "failed to find topo_usb_lport_t for "
		    "port %d", *reg);
		return (topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM));
	}

	l->tul_device = node;

	/*
	 * Check to see if we have a hub and if so, process it.
	 */
	if ((drvname = di_driver_name(node)) != NULL &&
	    strcmp(drvname, "hubd") == 0 &&
	    di_prop_lookup_ints(DDI_DEV_T_ANY, node, "usb-port-count",
	    &nports) == 1 && *nports >= 1) {
		di_node_t child;

		/*
		 * First go through and try and discover and create all the
		 * logical ports that exist. It is possible that these ports
		 * already exist and that we have ACPI information about them.
		 * This would happen when a root port is connected into a set of
		 * hubs that are built-in.
		 */
		l->tul_nhubd_ports = (uint_t)*nports;
		for (uint_t i = 1; i <= l->tul_nhubd_ports; i++) {
			topo_usb_lport_t *clport;
			topo_usb_port_t *cport;

			if ((cport = topo_usb_port_create(mod, i, l->tul_name,
			    '.')) == NULL) {
				return (topo_mod_seterrno(mod, EMOD_NOMEM));
			}

			clport = topo_list_next(&cport->tup_lports);
			topo_list_append(&l->tul_ports, cport);
			l->tul_nports++;

			clport->tul_protocol = l->tul_protocol;
		}

		/*
		 * Now go through and discover its children.
		 */
		for (child = di_child_node(node); child != NULL;
		    child = di_sibling_node(child)) {
			int ret;

			if ((ret = topo_usb_gather_devcfg_port(mod, c,
			    &l->tul_ports, child)) != 0) {
				return (-1);
			}
		}
	}

	return (0);
}

static int
topo_usb_gather_devcfg_cb(di_node_t node, void *arg)
{
	uint_t i;
	topo_usb_controller_t *tuc;
	di_prop_t prop = DI_PROP_NIL;
	boolean_t rh = B_FALSE, pc = B_FALSE;
	topo_usb_devcfg_arg_t *tda = arg;
	topo_usb_t *usb = tda->tda_usb;
	topo_mod_t *mod = tda->tda_mod;
	di_node_t child;

	while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
		const char *name = di_prop_name(prop);
		int *ports;

		if (strcmp(name, "root-hub") == 0 &&
		    di_prop_type(prop) == DI_PROP_TYPE_BOOLEAN) {
			rh = B_TRUE;
		} else if (strcmp(name, "usb-port-count") == 0 &&
		    di_prop_ints(prop, &ports) == 1 && *ports > 0 &&
		    *ports < USB_TOPO_PORT_MAX) {
			pc = B_TRUE;
		}
	}

	if (!rh || !pc)
		return (DI_WALK_CONTINUE);

	if ((tuc = topo_usb_controller_create(mod, usb, node)) == NULL) {
		tda->tda_fatal = B_TRUE;
		return (DI_WALK_TERMINATE);
	}

	/*
	 * Check to make sure that every logical port exists at this level and
	 * that we have its speed information filled in. If it does not exist,
	 * create it.
	 */
	for (i = 1; i <= tuc->tuc_nhubd_ports; i++) {
		topo_usb_lport_t *l;
		topo_usb_port_t *p;

		topo_mod_dprintf(mod, "attempting to discover lport %u on "
		    "controller %s", i, tuc->tuc_path);

		if ((p = topo_usb_port_create(mod, i, tuc->tuc_name, '@')) ==
		    NULL) {
			topo_mod_dprintf(mod, "failed to create "
			    "port %u", i);
			tda->tda_fatal = B_TRUE;
			return (DI_WALK_TERMINATE);
		}

		topo_list_append(&tuc->tuc_ports, p);
		tuc->tuc_nports++;
		l = topo_list_next(&p->tup_lports);

		topo_usb_set_rhub_port_protocol(mod, tuc, l);
	}

	for (child = di_child_node(tuc->tuc_devinfo); child != NULL;
	    child = di_sibling_node(child)) {
		int ret;

		if ((ret = topo_usb_gather_devcfg_port(mod, tuc,
		    &tuc->tuc_ports, child)) != 0) {
			tda->tda_fatal = B_TRUE;
			return (DI_WALK_TERMINATE);
		}
	}

	return (DI_WALK_PRUNECHILD);
}

/*
 * To find all the controllers in the system, look for device nodes that have
 * the 'root-hub' property and also a valid usb-port-count property.
 */
static boolean_t
topo_usb_gather_devcfg(topo_mod_t *mod, topo_usb_t *usb)
{
	topo_usb_devcfg_arg_t tda;

	tda.tda_usb = usb;
	tda.tda_mod = mod;
	tda.tda_fatal = B_FALSE;

	(void) di_walk_node(usb->tu_devinfo, DI_WALK_CLDFIRST,
	    &tda, topo_usb_gather_devcfg_cb);

	return (!tda.tda_fatal);
}

/*
 * For more information on the matching logic here, see xHCI r1.1 / Appendix D -
 * Port to Connector Mapping.
 */
static boolean_t
topo_usb_acpi_pld_match(const acpi_pld_info_t *l, const acpi_pld_info_t *r)
{
	if (l->Panel == r->Panel &&
	    l->VerticalPosition == r->VerticalPosition &&
	    l->HorizontalPosition == r->HorizontalPosition &&
	    l->Shape == r->Shape &&
	    l->GroupOrientation == r->GroupOrientation &&
	    l->GroupPosition == r->GroupPosition &&
	    l->GroupToken == r->GroupToken) {
		return (B_TRUE);
	}

	return (B_FALSE);
}

typedef boolean_t (*topo_usb_port_match_f)(topo_usb_port_t *, void *);

static topo_usb_port_t *
topo_usb_port_match_lport(topo_usb_lport_t *lport, boolean_t remove,
    topo_usb_port_match_f func, void *arg)
{
	topo_usb_port_t *p;

	for (p = topo_list_next(&lport->tul_ports); p != NULL;
	    p = topo_list_next(p)) {
		topo_usb_lport_t *l;
		topo_usb_port_t *ret;

		if (func(p, arg)) {
			if (remove) {
				topo_list_delete(&lport->tul_ports, p);
				lport->tul_nports--;
			}

			return (p);
		}

		for (l = topo_list_next(&p->tup_lports); l != NULL;
		    l = topo_list_next(l)) {
			if ((ret = topo_usb_port_match_lport(l,
			    remove, func, arg)) != NULL) {
				return (ret);
			}
		}
	}

	return (NULL);
}

static topo_usb_port_t *
topo_usb_port_match_controller(topo_usb_controller_t *c, boolean_t remove,
    topo_usb_port_match_f func, void *arg)
{
	topo_usb_port_t *p;

	for (p = topo_list_next(&c->tuc_ports); p != NULL;
	    p = topo_list_next(p)) {
		topo_usb_lport_t *l;
		topo_usb_port_t *ret;

		if (func(p, arg)) {
			if (remove) {
				topo_list_delete(&c->tuc_ports, p);
				c->tuc_nports--;
			}

			return (p);
		}

		for (l = topo_list_next(&p->tup_lports); l != NULL;
		    l = topo_list_next(l)) {
			if ((ret = topo_usb_port_match_lport(l,
			    remove, func, arg)) != NULL) {
				return (ret);
			}
		}
	}

	return (NULL);
}

static topo_usb_port_t *
topo_usb_port_match(topo_usb_t *usb, boolean_t remove,
    topo_usb_port_match_f func, void *arg)
{
	topo_usb_controller_t *c;

	for (c = topo_list_next(&usb->tu_controllers); c != NULL;
	    c = topo_list_next(c)) {
		topo_usb_port_t *p;

		if ((p = topo_usb_port_match_controller(c, remove, func,
		    arg)) != NULL)
			return (p);
	}
	return (NULL);
}

/*
 * Merge all of the local ports and information in source, to sink.
 */
static void
topo_usb_port_merge(topo_usb_port_t *sink, topo_usb_port_t *source)
{
	topo_usb_lport_t *l;

	while ((l = topo_list_next(&source->tup_lports)) != NULL) {
		topo_list_delete(&source->tup_lports, l);
		source->tup_nlports--;
		topo_list_append(&sink->tup_lports, l);
		sink->tup_nlports++;
	}

	if (sink->tup_port_type == USB_TOPO_PORT_TYPE_DEFAULT) {
		sink->tup_port_type = source->tup_port_type;
	}

	if (sink->tup_port_connected == TOPO_USB_C_UNKNOWN) {
		sink->tup_port_connected = source->tup_port_connected;
	}
}

static boolean_t
topo_usb_acpi_port_match(topo_usb_port_t *port, void *arg)
{
	topo_usb_port_t *target = arg;

	return (port != target && port->tup_pld_valid &&
	    topo_usb_acpi_pld_match(&port->tup_pld, &target->tup_pld));
}

/*
 * Ports on an xhci controller can match up. If we've been told that we should
 * do so, attempt to perform that match. We only try to find matches in the top
 * level ports of an xhci controller as that's what's most common on systems,
 * though we'll search all the descendants.
 */
static void
topo_usb_acpi_match(topo_mod_t *mod, topo_usb_controller_t *tuc)
{
	topo_usb_port_t *p;

	for (p = topo_list_next(&tuc->tuc_ports); p != NULL;
	    p = topo_list_next(p)) {
		topo_usb_port_t *match;

		if ((match = topo_usb_port_match_controller(tuc, B_TRUE,
		    topo_usb_acpi_port_match, p)) != NULL) {
			VERIFY3P(p, !=, match);
			topo_usb_port_merge(p, match);
			topo_mod_free(mod, match, sizeof (topo_usb_port_t));
		}
	}
}

static boolean_t
topo_usb_metadata_match(topo_usb_port_t *port, void *arg)
{
	topo_usb_meta_port_path_t *path = arg;
	topo_usb_lport_t *l;

	if (path->tmpp_type != TOPO_USB_T_ACPI)
		return (B_FALSE);

	for (l = topo_list_next(&port->tup_lports); l != NULL;
	    l = topo_list_next(l)) {
		if (l->tul_acpi_name != NULL && strcmp(path->tmpp_path,
		    l->tul_acpi_name) == 0) {
			return (B_TRUE);
		}
	}

	return (B_FALSE);
}

/*
 * We've found metadata describing the USB ports. We need to now go through and
 * try to match that data up to actual nodes.
 */
static void
topo_usb_apply_metadata(topo_mod_t *mod, topo_usb_t *usb)
{
	topo_usb_meta_port_t *m;

	for (m = topo_list_next(&usb->tu_metadata); m != NULL;
	    m = topo_list_next(m)) {
		topo_usb_port_t *p, *sink = NULL;
		topo_usb_meta_port_path_t *path;
		boolean_t remove = B_FALSE;

		/*
		 * If this is a chassis node, we'll remove the port and move it
		 * to the chassis.
		 */
		if (m->tmp_flags & TOPO_USB_F_CHASSIS) {
			remove = B_TRUE;
		}

		for (path = topo_list_next(&m->tmp_paths); path != NULL;
		    path = topo_list_next(path)) {
			topo_mod_dprintf(mod, "considering metadata path %s",
			    path->tmpp_path);
			if ((p = topo_usb_port_match(usb, remove,
			    topo_usb_metadata_match, path)) == NULL)
				continue;
			topo_mod_dprintf(mod, "matched path to a logical port");
			p->tup_meta = m;

			/*
			 * Check if we can move this to the Chassis. We should
			 * always do this on the first port in a group. However,
			 * if it's a match candidate, then it will have already
			 * been appended.
			 */
			if ((m->tmp_flags & TOPO_USB_F_CHASSIS) != 0 &&
			    sink == NULL) {
				topo_list_append(&usb->tu_chassis_ports, p);
				usb->tu_nchassis_ports++;
			}

			if ((usb->tu_meta_flags & TOPO_USB_M_METADATA_MATCH) !=
			    0) {
				if (sink == NULL) {
					sink = p;
					remove = B_TRUE;
				} else {
					VERIFY3P(p, !=, sink);
					topo_usb_port_merge(sink, p);
					topo_mod_free(mod, p,
					    sizeof (topo_usb_port_t));
				}
				continue;
			}

			break;
		}

	}
}

static int
topo_usb_gather(topo_mod_t *mod, topo_usb_t *usb, tnode_t *pnode)
{
	int ret;

	if ((ret = topo_usb_load_metadata(mod, pnode, &usb->tu_metadata,
	    &usb->tu_meta_flags)) != 0) {
		topo_mod_dprintf(mod, "failed to read usb metadata");
		return (-1);
	}
	topo_mod_dprintf(mod, "loaded metadata flags: %d", usb->tu_meta_flags);

	if (!topo_usb_gather_devcfg(mod, usb)) {
		topo_mod_dprintf(mod, "encountered fatal error while "
		    "gathering physical data");
		return (-1);
	}

	if ((usb->tu_meta_flags & TOPO_USB_M_NO_ACPI) == 0 &&
	    !topo_usb_gather_acpi(mod, usb)) {
		topo_mod_dprintf(mod, "encountered fatal error while "
		    "gathering ACPI data");
		return (-1);
	}

	if ((usb->tu_meta_flags & TOPO_USB_M_ACPI_MATCH) != 0) {
		topo_usb_controller_t *c;

		for (c = topo_list_next(&usb->tu_controllers); c != NULL;
		    c = topo_list_next(c)) {
			if (c->tuc_driver == TOPO_USB_D_XHCI) {
				topo_usb_acpi_match(mod, c);
			}
		}
	}

	topo_usb_apply_metadata(mod, usb);

	return (0);
}

static int
topo_usb_port_properties(topo_mod_t *mod, tnode_t *tn, topo_usb_port_t *port)
{
	int err;
	char **strs = NULL;
	uint_t i;
	topo_usb_lport_t *l;
	char *label;
	const char *ptype;
	size_t strlen;

	strlen = sizeof (char *) * MAX(port->tup_nlports,
	    TOPO_PROP_USB_PORT_NATTRS);
	if ((strs = topo_mod_zalloc(mod, strlen)) == NULL) {
		return (-1);
	}

	label = NULL;
	if (port->tup_meta != NULL) {
		label = port->tup_meta->tmp_label;
	}

	if (port->tup_meta != NULL && port->tup_meta->tmp_port_type !=
	    USB_TOPO_PORT_TYPE_DEFAULT) {
		ptype =
		    topo_usb_port_type_to_string(port->tup_meta->tmp_port_type);
	} else {
		ptype = topo_usb_port_type_to_string(port->tup_port_type);
	}

	if (topo_pgroup_create(tn, &topo_usb_port_pgroup, &err) != 0) {
		topo_mod_dprintf(mod, "failed to create property group %s: "
		    "%s\n", TOPO_PGROUP_USB_PORT, topo_strerror(err));
		goto error;

	}

	if (label != NULL && topo_node_label_set(tn, label, &err) != 0) {
		topo_mod_dprintf(mod, "failed to set label on port: %s",
		    topo_strerror(err));
		goto error;
	}

	if (ptype != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PORT,
	    TOPO_PROP_USB_PORT_TYPE, TOPO_PROP_IMMUTABLE, ptype, &err) != 0) {
		topo_mod_dprintf(mod, "failed to set %s property: %s",
		    TOPO_PROP_USB_PORT_TYPE, topo_strerror(err));
		goto error;
	}

	for (i = 0, l = topo_list_next(&port->tup_lports); l != NULL;
	    l = topo_list_next(l)) {
		char *vers;
		int j;

		switch (l->tul_protocol) {
		case TOPO_USB_P_1x:
			vers = "1.x";
			break;
		case TOPO_USB_P_20:
			vers = "2.0";
			break;
		case TOPO_USB_P_30:
			vers = "3.0";
			break;
		case TOPO_USB_P_31:
			vers = "3.1";
			break;
		default:
			continue;
		}

		/*
		 * Make sure we don't already have this string. This can happen
		 * when we have an ehci port and xhci support that both provide
		 * USB 2.0 service.
		 */
		for (j = 0; j < i; j++) {
			if (strcmp(strs[j], vers) == 0)
				break;
		}

		if (j < i)
			continue;
		strs[i++] = vers;
	}

	if (i > 0 && topo_prop_set_string_array(tn, TOPO_PGROUP_USB_PORT,
	    TOPO_PROP_USB_PORT_VERSIONS, TOPO_PROP_IMMUTABLE,
	    (const char **)strs, i, &err) != 0) {
		topo_mod_dprintf(mod, "failed to set %s property: %s",
		    TOPO_PROP_USB_PORT_VERSIONS, topo_strerror(err));
		goto error;
	}

	i = 0;
	if (port->tup_pld_valid && port->tup_pld.UserVisible != 0 &&
	    port->tup_port_connected == TOPO_USB_C_CONNECTED) {
		strs[i++] = TOPO_PROP_USB_PORT_A_VISIBLE;
	} else if (port->tup_port_connected == TOPO_USB_C_CONNECTED) {
		strs[i++] = TOPO_PROP_USB_PORT_A_CONNECTED;
	} else if (port->tup_port_connected == TOPO_USB_C_DISCONNECTED) {
		strs[i++] = TOPO_PROP_USB_PORT_A_DISCONNECTED;
	}

	if (port->tup_meta != NULL) {
		if (port->tup_meta->tmp_flags & TOPO_USB_F_INTERNAL) {
			strs[i++] = TOPO_PROP_USB_PORT_A_INTERNAL;
		}

		if (port->tup_meta->tmp_flags & TOPO_USB_F_EXTERNAL) {
			strs[i++] = TOPO_PROP_USB_PORT_A_EXTERNAL;
		}
	}

	if (i > 0 && topo_prop_set_string_array(tn, TOPO_PGROUP_USB_PORT,
	    TOPO_PROP_USB_PORT_ATTRIBUTES, TOPO_PROP_IMMUTABLE,
	    (const char **)strs, i, &err) != 0) {
		topo_mod_dprintf(mod, "failed to set %s property: %s",
		    TOPO_PROP_USB_PORT_VERSIONS, topo_strerror(err));
		goto error;
	}

	for (i = 0, l = topo_list_next(&port->tup_lports); l != NULL;
	    l = topo_list_next(l)) {
		strs[i++] = l->tul_name;
	}

	if (i > 0 && topo_prop_set_string_array(tn, TOPO_PGROUP_USB_PORT,
	    TOPO_PROP_USB_PORT_LPORTS, TOPO_PROP_IMMUTABLE,
	    (const char **)strs, i, &err) != 0) {
		topo_mod_dprintf(mod, "failed to set %s propert: %s",
		    TOPO_PROP_USB_PORT_LPORTS, topo_strerror(err));
		goto error;
	}

	err = 0;
error:
	if (strs != NULL) {
		topo_mod_free(mod, strs, strlen);
	}

	if (err != 0) {
		return (topo_mod_seterrno(mod, err));
	}

	return (err);
}

/*
 * Create a disk node under the scsa2usb node. When we have an scsa2usb node,
 * we'll have a child devinfo which is a disk. To successfully enumerate this,
 * we need to find the child node (which should be our only direct descendent)
 * and get its devfs path. From there we can construct a 'binding' property
 * group with the 'occupantpath' property that points to the module. At that
 * point we can invoke the disk enumerator.
 */
static int
topo_usb_enum_scsa2usb(topo_mod_t *mod, tnode_t *tn, topo_usb_lport_t *lport)
{
	int ret;
	di_node_t child;
	char *devfs = NULL;
	topo_instance_t min = 0, max = 0;

	if ((child = di_child_node(lport->tul_device)) == DI_NODE_NIL ||
	    strcmp("disk", di_node_name(child)) != 0) {
		return (0);
	}

	if ((devfs = di_devfs_path(child)) == NULL) {
		topo_mod_dprintf(mod, "failed to get USB disk child device "
		    "devfs path");
		return (topo_mod_seterrno(mod, EMOD_NOMEM));
	}

	if (topo_mod_load(mod, DISK, TOPO_VERSION) == NULL) {
		topo_mod_dprintf(mod, "failed to load disk module: %s",
		    topo_mod_errmsg(mod));
		goto error;
	}

	if (topo_pgroup_create(tn, &topo_binding_pgroup, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create \"binding\" "
		    "property group: %s", topo_strerror(ret));
		goto error;
	}

	if (topo_prop_set_string(tn, TOPO_PGROUP_BINDING,
	    TOPO_BINDING_OCCUPANT, TOPO_PROP_IMMUTABLE, devfs, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_IO_MODULE, topo_strerror(ret));
		goto error;
	}
	di_devfs_path_free(devfs);
	devfs = NULL;

	if (topo_node_range_create(mod, tn, DISK, min, max) != 0) {
		topo_mod_dprintf(mod, "failed to create disk node range %s: %s",
		    devfs, topo_mod_errmsg(mod));
		goto error;
	}

	if (topo_mod_enumerate(mod, tn, DISK, DISK, min, max, NULL) != 0) {
		topo_mod_dprintf(mod, "failed to create disk node %s: %s",
		    devfs, topo_mod_errmsg(mod));
		goto error;
	}

	return (0);

error:
	di_devfs_path_free(devfs);
	return (-1);
}

static int
topo_usb_enum_port_children(topo_mod_t *mod, tnode_t *pn,
    topo_usb_lport_t *plport)
{
	int ret;
	topo_usb_port_t *port;
	topo_instance_t min = 0, i;

	if ((ret = port_range_create(mod, pn, min, plport->tul_nports)) != 0) {
		topo_mod_dprintf(mod, "failed to create port range [%u, %u) "
		    "for child hub", 0, plport->tul_nports);
		return (ret);
	}

	for (i = 0, port = topo_list_next(&plport->tul_ports); port != NULL;
	    port = topo_list_next(port)) {
		tnode_t *tn;
		if ((ret = port_create_usb(mod, pn, i, &tn)) != 0)
			return (ret);

		if ((ret = topo_usb_port_properties(mod, tn, port)) != 0) {
			return (ret);
		}

		if ((ret = topo_usb_enum_device(mod, tn, port)) != 0)
			return (ret);

		i++;
	}

	return (0);
}

/*
 * Enumerate the requested device. Depending on the driver associated with it
 * (if any), we may have to create child nodes.
 */
static int
topo_usb_enum_lport(topo_mod_t *mod, tnode_t *pn, topo_usb_port_t *port,
    topo_usb_lport_t *lport, topo_instance_t topo_inst)
{
	int ret, inst;
	int *vendid = NULL, *prodid = NULL, *revid = NULL, *release = NULL;
	char *vend = NULL, *prod = NULL, *serial = NULL, *speed = NULL;
	char *min_speed = NULL, *sup_speeds = NULL;
	int nsup_speeds = 0;
	char *driver, *devfs;
	char revbuf[32], relbuf[32];
	tnode_t *tn = NULL;
	di_prop_t prop = DI_PROP_NIL;
	nvlist_t *auth = NULL, *fmri = NULL, *modnvl = NULL;

	/*
	 * Look up the information we'll need to create the usb-properties. We
	 * do this first because this information is often part of the FMRI.
	 */
	for (prop = di_prop_next(lport->tul_device, DI_PROP_NIL);
	    prop != DI_PROP_NIL; prop = di_prop_next(lport->tul_device, prop)) {
		const char *pname = di_prop_name(prop);

		if (strcmp(pname, "usb-vendor-id") == 0) {
			if (di_prop_ints(prop, &vendid) != 1)
				vendid = NULL;
		} else if (strcmp(pname, "usb-product-id") == 0) {
			if (di_prop_ints(prop, &prodid) != 1)
				prodid = NULL;
		} else if (strcmp(pname, "usb-revision-id") == 0) {
			if (di_prop_ints(prop, &revid) != 1) {
				revid = NULL;
			} else {
				(void) snprintf(revbuf, sizeof (revbuf), "%x",
				    *revid);
			}
		} else if (strcmp(pname, "usb-release") == 0) {
			if (di_prop_ints(prop, &release) != 1) {
				release = NULL;
			} else {
				(void) snprintf(relbuf, sizeof (relbuf),
				    "%x.%x", *release >> 8,
				    (*release >> 4) & 0xf);
			}
		} else if (strcmp(pname, "usb-vendor-name") == 0) {
			if (di_prop_strings(prop, &vend) != 1)
				vend = NULL;
		} else if (strcmp(pname, "usb-product-name") == 0) {
			if (di_prop_strings(prop, &prod) != 1)
				prod = NULL;
		} else if (strcmp(pname, "usb-serialno") == 0) {
			if (di_prop_strings(prop, &serial) != 1)
				serial = NULL;
		} else if (strcmp(pname, "full-speed") == 0) {
			speed = "full-speed";
		} else if (strcmp(pname, "low-speed") == 0) {
			speed = "low-speed";
		} else if (strcmp(pname, "high-speed") == 0) {
			speed = "high-speed";
		} else if (strcmp(pname, "super-speed") == 0) {
			speed = "super-speed";
		} else if (strcmp(pname, "usb-minimum-speed") == 0) {
			if (di_prop_strings(prop, &min_speed) != 1)
				min_speed = NULL;
		} else if (strcmp(pname, "usb-supported-speeds") == 0) {
			nsup_speeds = di_prop_strings(prop, &sup_speeds);
			if (nsup_speeds <= 0) {
				sup_speeds = NULL;
			}
		}
	}

	driver = di_driver_name(lport->tul_device);
	inst = di_instance(lport->tul_device);
	devfs = di_devfs_path(lport->tul_device);

	if ((auth = topo_mod_auth(mod, pn)) == NULL) {
		topo_mod_dprintf(mod, "failed to get authority for USB device: "
		    "%s", topo_mod_errmsg(mod));
		goto error;
	}

	if ((fmri = topo_mod_hcfmri(mod, pn, FM_HC_SCHEME_VERSION, USB_DEVICE,
	    topo_inst, NULL, auth, prod, revbuf, serial)) == NULL) {
		topo_mod_dprintf(mod, "failed to generate fmri for USB "
		    "device %s: %s", di_devfs_path(lport->tul_device),
		    topo_mod_errmsg(mod));
		goto error;
	}

	if ((tn = topo_node_bind(mod, pn, USB_DEVICE, topo_inst, fmri)) ==
	    NULL) {
		topo_mod_dprintf(mod, "failed to bind USB device node: %s",
		    topo_mod_errmsg(mod));
		goto error;
	}

	/*
	 * In general, we expect a USB device to be its own FRU. There are some
	 * exceptions to this, for example, a built-in hub. However, it's hard
	 * for us to generally know. It may be nice to allow the platform to
	 * override this in the future.
	 */
	if (topo_node_fru_set(tn, fmri, 0, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to set FRU: %s",
		    topo_strerror(ret));
		(void) topo_mod_seterrno(mod, ret);
		goto error;
	}

	/*
	 * Inherit the label from the port on the device. This is intended to
	 * only go a single way.
	 */
	if (port->tup_meta != NULL && port->tup_meta->tmp_label != NULL &&
	    topo_node_label_set(tn, port->tup_meta->tmp_label, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to set label on device: %s",
		    topo_strerror(ret));
		goto error;
	}

	/*
	 * USB-properties
	 */
	if (topo_pgroup_create(tn, &topo_usb_props_pgroup, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create \"usb-properties\" "
		    "property group: %s", topo_strerror(ret));
		goto error;
	}

	if (topo_prop_set_uint32(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_PORT, TOPO_PROP_IMMUTABLE, lport->tul_portno,
	    &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_PORT, topo_strerror(ret));
		goto error;
	}

	if (vendid != NULL && topo_prop_set_int32(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_VID, TOPO_PROP_IMMUTABLE, *vendid, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_VID, topo_strerror(ret));
		goto error;
	}

	if (prodid != NULL && topo_prop_set_int32(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_PID, TOPO_PROP_IMMUTABLE, *prodid, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_PID, topo_strerror(ret));
		goto error;
	}

	if (revid != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_REV, TOPO_PROP_IMMUTABLE, revbuf, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_REV, topo_strerror(ret));
		goto error;
	}

	if (release != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_VERSION, TOPO_PROP_IMMUTABLE, relbuf, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_VERSION, topo_strerror(ret));
		goto error;
	}

	if (vend != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_VNAME, TOPO_PROP_IMMUTABLE, vend, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_VNAME, topo_strerror(ret));
		goto error;
	}

	if (prod != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_PNAME, TOPO_PROP_IMMUTABLE, prod, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_PNAME, topo_strerror(ret));
		goto error;
	}

	if (serial != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_SN, TOPO_PROP_IMMUTABLE, serial, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_SN, topo_strerror(ret));
		goto error;
	}

	if (speed != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_SPEED, TOPO_PROP_IMMUTABLE, speed, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_SPEED, topo_strerror(ret));
		goto error;
	}

	if (min_speed != NULL && topo_prop_set_string(tn, TOPO_PGROUP_USB_PROPS,
	    TOPO_PGROUP_USB_PROPS_MIN_SPEED, TOPO_PROP_IMMUTABLE, min_speed,
	    &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_PGROUP_USB_PROPS_MIN_SPEED, topo_strerror(ret));
		goto error;
	}

	if (sup_speeds != NULL) {
		const char **strings, *c;
		int i, rval;

		if ((strings = topo_mod_zalloc(mod, sizeof (char *) *
		    nsup_speeds)) == NULL) {
			topo_mod_dprintf(mod, "failed to allocate character "
			    "array for property %s",
			    TOPO_PGROUP_USB_PROPS_SUPPORTED_SPEEDS);
			goto error;
		}

		/*
		 * devinfo string properties are concatenated NUL-terminated
		 * strings. We need to translate that to a string array.
		 */
		for (c = sup_speeds, i = 0; i < nsup_speeds; i++) {
			size_t len;

			strings[i] = c;
			if (i + 1 < nsup_speeds) {
				len = strlen(c);
				c += len + 1;
			}
		}

		rval = topo_prop_set_string_array(tn, TOPO_PGROUP_USB_PROPS,
		    TOPO_PGROUP_USB_PROPS_SUPPORTED_SPEEDS, TOPO_PROP_IMMUTABLE,
		    strings, nsup_speeds, &ret);
		topo_mod_free(mod, strings, sizeof (char *) * nsup_speeds);
		if (rval != 0) {
			topo_mod_dprintf(mod, "failed to create property %s: "
			    "%s", TOPO_PGROUP_USB_PROPS_SUPPORTED_SPEEDS,
			    topo_strerror(ret));
		}
	}

	/*
	 * I/O pgroup
	 */
	if (topo_pgroup_create(tn, &topo_io_pgroup, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create \"io\" "
		    "property group: %s", topo_strerror(ret));
		goto error;
	}

	if (driver != NULL && topo_prop_set_string(tn, TOPO_PGROUP_IO,
	    TOPO_IO_DRIVER, TOPO_PROP_IMMUTABLE, driver, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_IO_DRIVER, topo_strerror(ret));
		goto error;
	}

	if (inst != -1 && topo_prop_set_uint32(tn, TOPO_PGROUP_IO,
	    TOPO_IO_INSTANCE, TOPO_PROP_IMMUTABLE, inst, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_IO_INSTANCE, topo_strerror(ret));
		goto error;
	}

	if (devfs != NULL && topo_prop_set_string(tn, TOPO_PGROUP_IO,
	    TOPO_IO_DEV_PATH, TOPO_PROP_IMMUTABLE, devfs, &ret) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_IO_DEV_PATH, topo_strerror(ret));
		goto error;
	}

	if (driver != NULL && (modnvl = topo_mod_modfmri(mod,
	    FM_MOD_SCHEME_VERSION, driver)) != NULL &&
	    topo_prop_set_fmri(tn, TOPO_PGROUP_IO, TOPO_IO_MODULE,
	    TOPO_PROP_IMMUTABLE, modnvl, &ret) != 0) {
		topo_mod_dprintf(mod, "failed to create property %s: %s",
		    TOPO_IO_MODULE, topo_strerror(ret));
		goto error;
	}

	/*
	 * Check the drivers to determine special behavior that we should do.
	 * The following are cases that we want to handle:
	 *
	 *   o Creating disk nodes for scsa2usb devices
	 *   o Creating children ports and searching them for hubd
	 */
	if (driver != NULL && strcmp(driver, "scsa2usb") == 0) {
		if ((ret = topo_usb_enum_scsa2usb(mod, tn, lport)) != 0)
			goto error;
	}

	if (lport->tul_nports > 0 && driver != NULL &&
	    strcmp(driver, "hubd") == 0) {
		if ((ret = topo_usb_enum_port_children(mod, tn, lport)) != 0)
			goto error;
	}

	di_devfs_path_free(devfs);
	nvlist_free(fmri);
	nvlist_free(auth);
	nvlist_free(modnvl);
	return (0);

error:
	topo_node_unbind(tn);
	di_devfs_path_free(devfs);
	nvlist_free(fmri);
	nvlist_free(auth);
	nvlist_free(modnvl);
	return (-1);
}

static int
topo_usb_enum_device(topo_mod_t *mod, tnode_t *pn, topo_usb_port_t *port)
{
	int ret;
	topo_instance_t i, max;
	topo_usb_lport_t *l;

	max = 0;
	for (l = topo_list_next(&port->tup_lports); l != NULL;
	    l = topo_list_next(l)) {
		if (l->tul_device != DI_NODE_NIL)
			max++;
	}

	if (max == 0) {
		return (0);
	}

	if ((ret = topo_node_range_create(mod, pn, USB_DEVICE, 0, max - 1)) !=
	    0) {
		return (-1);
	}

	for (i = 0, l = topo_list_next(&port->tup_lports); l != NULL;
	    l = topo_list_next(l)) {
		if (l->tul_device != DI_NODE_NIL) {
			topo_mod_dprintf(mod, "enumerating device on lport "
			    "%u, log inst %d", l->tul_portno, i);
			if ((ret = topo_usb_enum_lport(mod, pn, port, l,
			    i)) != 0) {
				return (ret);
			}
			i++;
		}
	}

	return (0);
}

static int
topo_usb_enum_controller(topo_mod_t *mod, tnode_t *pnode,
    topo_usb_controller_t *c, topo_instance_t base)
{
	int ret;
	topo_usb_port_t *port;

	if (c->tuc_enumed)
		return (0);

	c->tuc_enumed = B_TRUE;
	if (c->tuc_nports == 0)
		return (0);

	for (port = topo_list_next(&c->tuc_ports); port != NULL;
	    port = topo_list_next(port)) {
		tnode_t *tn;
		if ((ret = port_create_usb(mod, pnode, base, &tn)) != 0)
			return (ret);

		if ((ret = topo_usb_port_properties(mod, tn, port)) != 0) {
			return (ret);
		}

		if ((ret = topo_usb_enum_device(mod, tn, port)) != 0)
			return (ret);

		base++;
	}

	return (0);
}

static int
topo_usb_enum_mobo(topo_mod_t *mod, tnode_t *pnode, topo_usb_t *usb)
{
	int ret;
	topo_usb_controller_t *c;
	topo_instance_t inst = 0;

	/*
	 * First count the number of ports, so we can create the right range.
	 * Then go back and actually create things. Some of the ports here may
	 * be actually on the chassis, that's OK, we don't mind over counting
	 * here.
	 */
	for (c = topo_list_next(&usb->tu_controllers); c != NULL;
	    c = topo_list_next(c)) {
		inst += c->tuc_nports;
	}

	if ((ret = port_range_create(mod, pnode, 0, inst)) != 0) {
		topo_mod_dprintf(mod, "failed to create port range [%u, %u) "
		    "for mobo", 0, inst);
		return (ret);
	}

	inst = 0;
	for (c = topo_list_next(&usb->tu_controllers); c != NULL;
	    c = topo_list_next(c)) {
		if (c->tuc_enumed)
			continue;
		if ((ret = topo_usb_enum_controller(mod, pnode, c, inst)) !=
		    0) {
			return (ret);
		}
		inst += c->tuc_nports;
	}

	return (0);
}

static int
topo_usb_enum_pci(topo_mod_t *mod, tnode_t *pnode, topo_usb_t *usb,
    di_node_t din)
{
	int ret;
	topo_usb_controller_t *c;
	topo_instance_t min = 0;

	for (c = topo_list_next(&usb->tu_controllers); c != NULL;
	    c = topo_list_next(c)) {
		if (din == c->tuc_devinfo) {
			break;
		}
	}

	if (c == NULL) {
		return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
	}

	if ((ret = port_range_create(mod, pnode, min, c->tuc_nports)) != 0) {
		topo_mod_dprintf(mod, "failed to create port range [%u, %u) "
		    "for controller %s", min, c->tuc_nports, c->tuc_path);
		return (ret);
	}

	return (topo_usb_enum_controller(mod, pnode, c, min));
}

static int
topo_usb_enum_chassis(topo_mod_t *mod, tnode_t *pnode, topo_usb_t *usb)
{
	int ret;
	topo_usb_port_t *p;
	topo_instance_t base = 0;

	if (usb->tu_nchassis_ports == 0)
		return (0);

	if ((ret = port_range_create(mod, pnode, 0, usb->tu_nchassis_ports)) !=
	    0) {
		topo_mod_dprintf(mod, "failed to create port range [%u, %u) "
		    "for mobo", 0, usb);
		return (ret);
	}

	for (p = topo_list_next(&usb->tu_chassis_ports); p != NULL;
	    p = topo_list_next(p)) {
		tnode_t *tn;
		if ((ret = port_create_usb(mod, pnode, base, &tn)) != 0)
			return (ret);

		if ((ret = topo_usb_port_properties(mod, tn, p)) != 0) {
			return (ret);
		}

		if ((ret = topo_usb_enum_device(mod, tn, p)) != 0)
			return (ret);

		base++;
	}

	return (0);
}

/* ARGSUSED */
static int
topo_usb_enum(topo_mod_t *mod, tnode_t *pnode, const char *name,
    topo_instance_t min, topo_instance_t max, void *modarg, void *data)
{
	topo_usb_t *usb;
	topo_usb_type_t type;

	if (strcmp(name, USB_PCI) == 0) {
		type = TOPO_USB_PCI;
	} else if (strcmp(name, USB_MOBO) == 0) {
		type = TOPO_USB_MOBO;
	} else if (strcmp(name, USB_CHASSIS) == 0) {
		type = TOPO_USB_CHASSIS;
	} else {
		topo_mod_dprintf(mod, "usb_enum: asked to enumerate unknown "
		    "component: %s\n", name);
		return (-1);
	}

	if (type == TOPO_USB_PCI && data == NULL) {
		topo_mod_dprintf(mod, "usb_enum: missing argument to "
		    "PCI controller enum");
		return (-1);
	} else if (type != TOPO_USB_PCI && data != NULL) {
		topo_mod_dprintf(mod, "extraneous argument to non-controller "
		    "enum %s", name);
		return (-1);
	}

	if ((usb = topo_mod_getspecific(mod)) == NULL) {
		return (-1);
	}

	if (!usb->tu_enum_done) {
		if (topo_usb_gather(mod, usb, pnode) != 0)
			return (-1);
		usb->tu_enum_done = B_TRUE;
	}

	/*
	 * Now that we've built up the topo nodes, enumerate the specific nodes
	 * based on the requested type.
	 */
	if (type == TOPO_USB_PCI) {
		return (topo_usb_enum_pci(mod, pnode, usb, data));
	} else if (type == TOPO_USB_MOBO) {
		return (topo_usb_enum_mobo(mod, pnode, usb));
	} else if (type == TOPO_USB_CHASSIS) {
		return (topo_usb_enum_chassis(mod, pnode, usb));
	}

	return (0);
}

static const topo_modops_t usb_ops = {
	topo_usb_enum, NULL
};

static topo_modinfo_t usb_mod = {
	USB, FM_FMRI_SCHEME_HC, USB_VERSION, &usb_ops
};

static void
topo_usb_port_free(topo_mod_t *mod, topo_usb_port_t *p)
{
	topo_usb_lport_t *lport;

	while ((lport = topo_list_next(&p->tup_lports)) != NULL) {
		topo_usb_port_t *child;

		topo_list_delete(&p->tup_lports, lport);
		while ((child = topo_list_next(&lport->tul_ports)) != NULL) {
			topo_list_delete(&lport->tul_ports, child);
			topo_usb_port_free(mod, child);
		}
		topo_mod_free(mod, lport, sizeof (topo_usb_lport_t));
	}

	topo_mod_free(mod, p, sizeof (topo_usb_port_t));
}

static void
topo_usb_free(topo_mod_t *mod, topo_usb_t *usb)
{
	topo_usb_controller_t *c;
	topo_usb_port_t *p;

	if (usb == NULL)
		return;

	while ((p = topo_list_next(&usb->tu_chassis_ports)) != NULL) {
		topo_list_delete(&usb->tu_chassis_ports, p);
		topo_usb_port_free(mod, p);
	}

	while ((c = topo_list_next(&usb->tu_controllers)) != NULL) {

		topo_list_delete(&usb->tu_controllers, c);
		di_devfs_path_free(c->tuc_path);

		while ((p = topo_list_next(&c->tuc_ports)) != NULL) {
			topo_list_delete(&c->tuc_ports, p);
			topo_usb_port_free(mod, p);
		}
		topo_mod_free(mod, c, sizeof (topo_usb_controller_t));
	}

	topo_usb_free_metadata(mod, &usb->tu_metadata);

	/*
	 * The devinfo handle came from fm, don't do anything ourselevs.
	 */
	usb->tu_devinfo = DI_NODE_NIL;

	topo_mod_free(mod, usb, sizeof (topo_usb_t));
}

static topo_usb_t *
topo_usb_alloc(topo_mod_t *mod)
{
	topo_usb_t *usb = NULL;

	if ((usb = topo_mod_zalloc(mod, sizeof (topo_usb_t))) == NULL) {
		goto free;
	}

	if ((usb->tu_devinfo = topo_mod_devinfo(mod)) == DI_NODE_NIL) {
		goto free;
	}

	return (usb);

free:
	topo_usb_free(mod, usb);
	return (NULL);
}

int
_topo_init(topo_mod_t *mod, topo_version_t version)
{
	topo_usb_t *usb;

	if (getenv("TOPOUSBDEBUG") != NULL)
		topo_mod_setdebug(mod);

	topo_mod_dprintf(mod, "_mod_init: initializing %s enumerator\n", USB);

	if (version != USB_VERSION) {
		return (-1);
	}

	if ((usb = topo_usb_alloc(mod)) == NULL) {
		return (-1);
	}

	if (topo_mod_register(mod, &usb_mod, TOPO_VERSION) != 0) {
		topo_usb_free(mod, usb);
		return (-1);
	}

	topo_mod_setspecific(mod, usb);

	return (0);
}

void
_topo_fini(topo_mod_t *mod)
{
	topo_usb_free(mod, topo_mod_getspecific(mod));
	topo_mod_setspecific(mod, NULL);
}