summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/cardbus/cardbus.c
blob: 643549b5dcba58c79708dc74d626b785a035dc54 (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
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
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c)  * Copyright (c) 2001 Tadpole Technology plc
 * All rights reserved.
 * From "@(#)pcicfg.c   1.31    99/06/18 SMI"
 */

/*
 * Cardbus module
 */

#include <sys/conf.h>
#include <sys/modctl.h>

#include <sys/pci.h>

#include <sys/ddi.h>
#include <sys/sunndi.h>
#include <sys/ddi_impldefs.h>

#include <sys/hotplug/hpcsvc.h>

#include <sys/pctypes.h>
#include <sys/pcmcia.h>
#include <sys/sservice.h>
#include <sys/note.h>

#include <sys/pci/pci_types.h>
#include <sys/pci/pci_sc.h>

#include <sys/pcic_reg.h>
#include <sys/pcic_var.h>
#include <sys/pcmcia.h>

#ifdef sparc
#include <sys/ddi_subrdefs.h>
#elif defined(__x86)
#include <sys/pci_intr_lib.h>
#include <sys/mach_intr.h>
#endif

#include "cardbus.h"
#include "cardbus_parse.h"
#include "cardbus_hp.h"
#include "cardbus_cfg.h"

static int cardbus_command_default = PCI_COMM_SERR_ENABLE |
				PCI_COMM_WAIT_CYC_ENAB |
				PCI_COMM_PARITY_DETECT |
				PCI_COMM_ME | PCI_COMM_MAE |
				PCI_COMM_IO;

static int cardbus_next_instance = 0;
static int cardbus_count = 0;
int number_of_cardbus_cards = 0;

static int cardbus_bus_map(dev_info_t *dip, dev_info_t *rdip,
		ddi_map_req_t *mp, off_t offset, off_t len, caddr_t *vaddrp);
static void pcirp2rp(const pci_regspec_t *pci_rp, struct regspec *rp);

static int cardbus_ctlops(dev_info_t *, dev_info_t *,
			ddi_ctl_enum_t, void *arg, void *);
static void cardbus_init_child_regs(dev_info_t *child);
static int cardbus_initchild(dev_info_t *, dev_info_t *,
			dev_info_t *, void *);
static int cardbus_name_child(dev_info_t *, char *, int);
static void cardbus_removechild(dev_info_t *dip);

static int cardbus_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
		ddi_dma_handle_t *handlep);
static int cardbus_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_handle_t handle);
static int cardbus_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
		ddi_dma_cookie_t *cp, uint_t *ccountp);
static int cardbus_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_handle_t handle);
static int cardbus_dma_flush(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_handle_t handle, off_t off, size_t len,
		uint_t cache_flags);
static int cardbus_dma_win(dev_info_t *dip, dev_info_t *rdip,
		ddi_dma_handle_t handle, uint_t win, off_t *offp,
		size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp);
static int cardbus_dma_map(dev_info_t *dip, dev_info_t *rdip,
		struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep);

static int cardbus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
		ddi_prop_op_t prop_op, int mod_flags,
		char *name, caddr_t valuep, int *lengthp);

static int cardbus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
		char *eventname, ddi_eventcookie_t *cookiep);
static int cardbus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
		ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
		ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
		void *arg, ddi_callback_id_t *cb_id);
static int cardbus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id);
static int cardbus_post_event(dev_info_t *dip, dev_info_t *rdip,
		ddi_eventcookie_t cookie, void *bus_impldata);

static int cardbus_intr_ops(dev_info_t *dip, dev_info_t *rdip,
		ddi_intr_op_t intr_op,
		ddi_intr_handle_impl_t *hdlp, void *result);

static int check_token(char *token, int *len);
static char *find_token(char **cp, int *l, char *endc);
static int parse_token(char *token);
static int token_to_hex(char *token, unsigned *val, int len);
static int token_to_dec(char *token, unsigned *val, int len);
static void cardbus_add_prop(struct cb_deviceset_props *cdsp, int type,
		char *name, caddr_t vp, int len);
static void cardbus_add_stringprop(struct cb_deviceset_props *cdsp,
		char *name, char *vp, int len);
static void cardbus_prop_free(ddi_prop_t *propp);
static void cardbus_devprops_free(struct cb_deviceset_props *cbdp);
static int cardbus_parse_devprop(cbus_t *cbp, char *cp);
static void cardbus_device_props(cbus_t *cbp);

static void cardbus_expand_busrange(dev_info_t *dip);

static int cardbus_convert_properties(dev_info_t *dip);
static void cardbus_revert_properties(dev_info_t *dip);

/*
 * driver global data
 */
kmutex_t cardbus_list_mutex; /* Protects the probe handle list */
void *cardbus_state;
int cardbus_latency_timer = 0x40;
int cardbus_debug = 0;

/*
 * Module linkage information for the kernel.
 */
extern struct mod_ops mod_miscops;
static struct modlmisc modlmisc = {
	&mod_miscops,
	"Cardbus Configurator support",
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modlmisc,
	NULL
};

int
_init(void)
{
	int error;

	error =  ddi_soft_state_init(&cardbus_state, sizeof (cbus_t), 0);
	if (error != 0)
		return (error);

	mutex_init(&cardbus_list_mutex, NULL, MUTEX_DRIVER, NULL);
	if ((error = mod_install(&modlinkage)) != 0) {
		mutex_destroy(&cardbus_list_mutex);
	}

	return (error);
}

int
_fini(void)
{
	int error;
	if ((error = mod_remove(&modlinkage)) == 0) {
		mutex_destroy(&cardbus_list_mutex);
		ddi_soft_state_fini(&cardbus_state);
	}
	return (error);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static
struct bus_ops cardbusbus_ops = {
	BUSO_REV,
	cardbus_bus_map,
	NULL,
	NULL,
	NULL,
	i_ddi_map_fault,
	cardbus_dma_map,
	cardbus_dma_allochdl,
	cardbus_dma_freehdl,
	cardbus_dma_bindhdl,
	cardbus_dma_unbindhdl,
	cardbus_dma_flush,
	cardbus_dma_win,
	ddi_dma_mctl,
	cardbus_ctlops,			/* (*bus_ctl)();		*/
	cardbus_prop_op,
	cardbus_get_eventcookie,	/* (*bus_get_eventcookie)();	*/
	cardbus_add_eventcall,		/* (*bus_add_eventcall)();	*/
	cardbus_remove_eventcall,	/* (*bus_remove_eventcall)();	*/
	cardbus_post_event,		/* (*bus_post_event)();		*/
	NULL,				/* (*bus_intr_ctl)();		*/
	NULL,				/* (*bus_config)();		*/
	NULL,				/* (*bus_unconfig)();		*/
	NULL,				/* (*bus_fm_init)();		*/
	NULL,				/* (*bus_fm_fini)();		*/
	NULL,				/* (*bus_enter)();		*/
	NULL,				/* (*bus_exit)();		*/
	NULL,				/* (*bus_power)();		*/
	cardbus_intr_ops		/* (*bus_intr_op)();		*/
};

#define	CB_EVENT_TAG_INSERT	0
#define	CB_EVENT_TAG_REMOVE	1

static ndi_event_definition_t cb_ndi_event_defs[] = {
	{ CB_EVENT_TAG_INSERT, DDI_DEVI_INSERT_EVENT, EPL_INTERRUPT, 0 },
	{ CB_EVENT_TAG_REMOVE, DDI_DEVI_REMOVE_EVENT, EPL_INTERRUPT, 0 }
};

#define	CB_N_NDI_EVENTS \
	(sizeof (cb_ndi_event_defs) / sizeof (cb_ndi_event_defs[0]))

#ifdef sparc
struct busnum_ctrl {
	int	rv;
	dev_info_t *dip;
	cardbus_bus_range_t *range;
};

static int
cardbus_claim_pci_busnum(dev_info_t *dip, void *arg)
{
	cardbus_bus_range_t pci_bus_range;
	struct busnum_ctrl *ctrl;
	ndi_ra_request_t req;
	char bus_type[16] = "(unknown)";
	int len;
	uint64_t base;
	uint64_t retlen;

	ctrl = (struct busnum_ctrl *)arg;

	/* check if this is a PCI bus node */
	len = sizeof (bus_type);
	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
	    "device_type",
	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
		return (0);	/* (DDI_WALK_PRUNECHILD); */

	if ((strcmp(bus_type, "pci") != 0) &&
	    (strcmp(bus_type, "pciex") != 0)) /* it is not a pci bus type */
		return (0);	/* (DDI_WALK_PRUNECHILD); */

	/* look for the bus-range property */
	len = sizeof (struct cardbus_bus_range);
	if (ddi_getlongprop_buf(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
		cardbus_err(dip, 1, "cardbus_claim_pci_busnum: %u -> %u \n",
		    pci_bus_range.lo, pci_bus_range.hi);
		if ((pci_bus_range.lo >= ctrl->range->lo) &&
		    (pci_bus_range.hi <= ctrl->range->hi)) {
			cardbus_err(dip, 1,
			    "cardbus_claim_pci_busnum: claim %u -> %u \n",
			    pci_bus_range.lo, pci_bus_range.hi);

			/* claim the bus range from the bus resource map */
			bzero((caddr_t)&req, sizeof (req));
			req.ra_addr = (uint64_t)pci_bus_range.lo;
			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
			req.ra_len = (uint64_t)pci_bus_range.hi -
			    (uint64_t)pci_bus_range.lo + 1;

			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
				return (0);	/* (DDI_WALK_PRUNECHILD); */
		}
	}

	/*
	 * never Error return.
	 */
	ctrl->rv = DDI_SUCCESS;
	return (DDI_WALK_TERMINATE);
}

static void
cardbus_walk_node_child(dev_info_t *parent,
    int (*f)(dev_info_t *, void *), void *arg)
{
	dev_info_t *dip;
	int ret;

	for (dip = ddi_get_child(parent); dip;
	    dip = ddi_get_next_sibling(dip)) {

		ret = (*f) (dip, arg);
		if (ret)
			return;
	}
}

static void cardbus_fix_hostbridge_busrange(dev_info_t *dip)
{
	cardbus_bus_range_t bus_range;
	struct busnum_ctrl ctrl;

	uint64_t next_bus;
	uint64_t blen;
	ndi_ra_request_t req;
	int	len;

	cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange\n");

	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
	req.ra_len = 1;
	if (ndi_ra_alloc(dip, &req,
	    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
	    0) != NDI_SUCCESS) {
		(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);

		if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM)
		    == NDI_FAILURE) {
			cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
			    "NDI_RA_TYPE_PCI_BUSNUM setup fail\n");
			return;
		}

		bus_range.lo = 0;
		(void) ddi_getlongprop_buf(DDI_DEV_T_NONE, dip,
		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&bus_range, &len);
		bus_range.hi = 255;

		(void) ndi_ra_free(dip,
		    (uint64_t)bus_range.lo + 1,
		    (uint64_t)bus_range.hi - (uint64_t)bus_range.lo,
		    NDI_RA_TYPE_PCI_BUSNUM, 0);

		ctrl.rv = DDI_SUCCESS;
		ctrl.dip = dip;
		ctrl.range = &bus_range;

		cardbus_walk_node_child(dip, cardbus_claim_pci_busnum,
		    (void*)&ctrl);

		if (ctrl.rv != DDI_SUCCESS)
			cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
			    "cardbus_walk_node_child fails\n");

		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
		    "bus-range", (int *)&bus_range, 2);

	} else {
		cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
		    "already set up %x\n", (int)next_bus);
		(void) ndi_ra_free(dip, next_bus, (uint64_t)1,
		    NDI_RA_TYPE_PCI_BUSNUM, 0);
	}
}

static dev_info_t *
cardbus_find_hsbridge_dip(dev_info_t *dip)
{
	dev_info_t *pdip;

	pdip = ddi_get_parent(dip);
	while (pdip) {
		if (ddi_get_parent(pdip) == ddi_root_node())
			break;
		pdip = ddi_get_parent(pdip);
	}

	return (pdip);
}
#endif /* sparc */

/*
 * Attach a device to the cardbus infrastructure.
 */
int
cardbus_attach(dev_info_t *dip, cb_nexus_cb_t *nex_ops)
{
	cbus_t *cbp;
	int cb_instance;
	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
	struct dev_info *devi = DEVI(dip);

	mutex_enter(&cardbus_list_mutex);

	/*
	 * Make sure that it is not already initialized.
	 */
	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
	    "cbus-instance") == 1) {
		cmn_err(CE_WARN,
		    "%s%d: cardbus instance already initialized!\n",
		    ddi_driver_name(dip), ddi_get_instance(dip));
			mutex_exit(&cardbus_list_mutex);
		return (DDI_FAILURE);
	}

	/*
	 * initialize soft state structure for the bus instance.
	 */
	cb_instance = cardbus_next_instance++;

	if (ddi_soft_state_zalloc(cardbus_state, cb_instance) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "%s%d: can't allocate cardbus soft state\n",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		mutex_exit(&cardbus_list_mutex);
		return (DDI_FAILURE);
	}

	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
	cbp->cb_instance = cb_instance;
	cbp->cb_dip = dip;
	mutex_init(&cbp->cb_mutex, NULL, MUTEX_DRIVER, NULL);

	/*
	 * Save the instance number of the soft state structure for
	 * this bus as a devinfo property.
	 */
	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
	    "cbus-instance", (caddr_t)&cb_instance,
	    sizeof (cb_instance)) != DDI_SUCCESS) {
		cmn_err(CE_WARN,
		    "%s%d: failed to add the property 'cbus-instance'",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		ddi_soft_state_free(cardbus_state, cb_instance);
		mutex_exit(&cardbus_list_mutex);
		return (DDI_FAILURE);
	}

	cbp->cb_nex_ops = nex_ops;
	/*
	 * TODO - Should probably be some sort of locking on the devinfo here.
	 */
	cbp->orig_dopsp = devi->devi_ops;
	cbp->orig_bopsp = devi->devi_ops->devo_bus_ops;
	cbp->cb_dops = *devi->devi_ops;
	devi->devi_ops = &cbp->cb_dops;

	if (ndi_event_alloc_hdl(dip, *anp->an_iblock, &cbp->cb_ndi_event_hdl,
	    NDI_SLEEP) == NDI_SUCCESS) {
		cbp->cb_ndi_events.ndi_n_events = CB_N_NDI_EVENTS;
		cbp->cb_ndi_events.ndi_events_version = NDI_EVENTS_REV1;
		cbp->cb_ndi_events.ndi_event_defs = cb_ndi_event_defs;
		if (ndi_event_bind_set(cbp->cb_ndi_event_hdl,
		    &cbp->cb_ndi_events,
		    NDI_SLEEP) != NDI_SUCCESS) {
			cardbus_err(dip, 1,
			    "cardbus_attach: ndi_event_bind_set failed\n");
		}
	}

	/*
	 * Check for device initialization property.
	 */
	cardbus_device_props(cbp);

	if (cardbus_init_hotplug(cbp) != DDI_SUCCESS) {
		ddi_soft_state_free(cardbus_state, cb_instance);
		mutex_exit(&cardbus_list_mutex);
		return (DDI_FAILURE);
	}

#ifdef sparc
	/* a hack to fix the bus-range problem on pci root nodes */
	{
		dev_info_t *hs_dip;

		hs_dip = cardbus_find_hsbridge_dip(dip);
		cardbus_fix_hostbridge_busrange(hs_dip);
	}
#endif

	cardbus_expand_busrange(dip);
	cardbus_count++;
	mutex_exit(&cardbus_list_mutex);
	return (DDI_SUCCESS);
}

#ifdef TODO
static int
cardbus_detach(dev_info_t *dip)
{
	int cb_instance;
	cbus_t *cbp;

	mutex_enter(&cardbus_list_mutex);
	/* get the instance number for the cardbus soft state data */
	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	if (cb_instance < 0) {
		mutex_exit(&cardbus_list_mutex);
		return (DDI_FAILURE); /* no instance is setup for this bus */
	}

	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	if (cbp->cb_dsp) {
		struct cb_deviceset_props *cbdp, *ncbdp;

		cbdp = cbp->cb_dsp;
		while (cbdp) {
			ncbdp = cbdp->next;
			cardbus_devprops_free(cbdp);
			cbdp = ncbdp;
		}
	}
	/*
	 * Unregister the bus with the HPS.
	 *
	 * (Note: It is assumed that the HPS framework uninstalls
	 *  event handlers for all the hot plug slots on this bus.)
	 */
	(void) hpc_nexus_unregister_bus(dip);

	if (cbp->cb_ndi_event_hdl != NULL) {
		(void) ndi_event_unbind_set(cbp->cb_ndi_event_hdl,
		    &cbp->cb_ndi_events, NDI_SLEEP);
		ndi_event_free_hdl(cbp->cb_ndi_event_hdl);
	}

	mutex_destroy(&cbp->cb_mutex);
	if (cbp->nexus_path)
		kmem_free(cbp->nexus_path, strlen(cbp->nexus_path) + 1);
	if (cbp->name)
		kmem_free(cbp->name, strlen(cbp->name) + 1);

	ddi_soft_state_free(cardbus_state, cb_instance);

	/* remove the 'cbus-instance' property from the devinfo node */
	(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "cbus-instance");

	ASSERT(cardbus_count != 0);
	--cardbus_count;

	mutex_exit(&cardbus_list_mutex);
	return (DDI_SUCCESS);
}
#endif

boolean_t
cardbus_load_cardbus(dev_info_t *dip, uint_t socket, uint32_t pc_base)
{
#ifndef HOTPLUG
	struct cardbus_config_ctrl ctrl;
	int circular_count;
#endif
	int cb_instance;
	cbus_t *cbp;
	struct dev_info *devi = DEVI(dip);

	_NOTE(ARGUNUSED(socket, pc_base))

#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 6, "cardbus_load_cardbus\n");
#endif

	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	ASSERT(cb_instance >= 0);
	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	if (cbp->fatal_problem)
		return (B_FALSE);

	if (cardbus_convert_properties(dip) == DDI_FAILURE)
		return (B_FALSE);

	number_of_cardbus_cards++;
	devi->devi_ops->devo_bus_ops = &cardbusbus_ops;

#ifdef HOTPLUG
	mutex_enter(&cbp->cb_mutex);
	cbp->card_present = B_TRUE;

	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_INSERTION, 0);
	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_POWER_ON, 0);
	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_CONFIGURE, 0);

	mutex_exit(&cbp->cb_mutex);
#else
	if (cardbus_configure(cbp) != PCICFG_SUCCESS) {
#if defined(CARDBUS_DEBUG)
		cardbus_err(dip, 6, "cardbus_configure failed\n");
#endif
		return (B_FALSE);
	}

	ctrl.rv = NDI_SUCCESS;
	ctrl.busno = cardbus_primary_busno(dip);
	ctrl.op = PCICFG_OP_ONLINE;
	ctrl.dip = NULL;
	ctrl.flags = PCICFG_FLAGS_CONTINUE;

	/*
	 * The child of the dip is the cardbus dip. The child of the
	 * cardbus dip is the device itself
	 */
#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 8, "cardbus_load_cardbus: calling cbus_configure\n");
#endif
	ndi_devi_enter(dip, &circular_count);
	ddi_walk_devs(ddi_get_child(dip), cbus_configure, (void *)&ctrl);
	ndi_devi_exit(dip, circular_count);

	if (ctrl.rv != NDI_SUCCESS) {
		cardbus_err(dip, 1,
		    "cardbus_load_cardbus (%s%d): failed to attach (%d)\n",
		    ctrl.dip ? ddi_driver_name(ctrl.dip) : "Unknown",
		    ctrl.dip ? ddi_get_instance(ctrl.dip) : 0,
		    ctrl.rv);

		/*
		 * Returning error here will cause the pcic_load_cardbus() call
		 * to fail. This will invoke pcic_unload_cardbus() which calls
		 * cardbus_unload_cardbus() below.
		 */
		return (B_FALSE);
	}
#endif

#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 7, "cardbus_load_cardbus: returning TRUE\n");
#endif

	return (B_TRUE);
}

/*
 * Unload the cardbus module
 */
void
cardbus_unload_cardbus(dev_info_t *dip)
{
	int	cb_instance;
#ifndef HOTPLUG
	int	prim_bus = cardbus_primary_busno(dip);
	int	rval;
#endif
	cbus_t *cbp;

	cardbus_err(dip, 6, "cardbus_unload_cardbus\n");

	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	ASSERT(cb_instance >= 0);
	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	if (number_of_cardbus_cards == 0)
		return;

#ifdef HOTPLUG
	mutex_enter(&cbp->cb_mutex);
	cbp->card_present = B_FALSE;

	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_POWER_OFF, 0);
	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_UNCONFIGURE, 0);
	(void) hpc_slot_event_notify(cbp->slot_handle,
	    HPC_EVENT_SLOT_REMOVAL, 0);

	mutex_exit(&cbp->cb_mutex);
#else

	cardbus_err(dip, 8,
	    "cardbus_unload_cardbus: calling cardbus_unconfigure_node\n");

	rval = cardbus_unconfigure_node(dip, prim_bus, B_TRUE);

	if (rval != NDI_SUCCESS) {
		cardbus_err(dip, 4,
		    "cardbus_unload_cardbus: "
		    "cardbus_unconfigure_node failed\n");
		number_of_cardbus_cards--;
		cbp->fatal_problem = B_TRUE;
		cmn_err(CE_WARN,
		    "cardbus(%s%d): Failed to remove device tree: "
		    "Slot disabled",
		    ddi_get_name(dip), ddi_get_instance(dip));
		return;
	}

	(void) cardbus_unconfigure(cbp);
#endif

	/*
	 * Inform the lower drivers that the card has been removed
	 */
	if (cbp->cb_ndi_event_hdl != NULL) {
		ddi_eventcookie_t cookie;
		if (ndi_event_retrieve_cookie(cbp->cb_ndi_event_hdl, dip,
		    DDI_DEVI_REMOVE_EVENT, &cookie, 0) == NDI_SUCCESS) {
			(void) ndi_event_run_callbacks(cbp->cb_ndi_event_hdl,
			    dip, cookie, NULL);
		}
	}

	cardbus_revert_properties(dip);
}

static boolean_t
is_32bit_pccard(dev_info_t *dip)
{
	int len;
	char bus_type[16];

	len = sizeof (bus_type);
	if (ddi_prop_op(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    PROP_LEN_AND_VAL_BUF, DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
	    "device_type", (caddr_t)&bus_type, &len) != DDI_SUCCESS)
		return (B_FALSE);

	if ((strcmp(bus_type, "pci") != 0) &&
	    (strcmp(bus_type, "pciex") != 0) &&
	    (strcmp(bus_type, "cardbus") != 0)) /* not of pci type */
		return (B_FALSE);

	return (B_TRUE);
}

void
cardbus_save_children(dev_info_t *dip)
{
	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
		cardbus_save_children(ddi_get_child(dip));

		if (strcmp("pcs", ddi_node_name(dip)) == 0)
			continue;
		if (!is_32bit_pccard(dip))
			continue;
		cardbus_err(dip, 1, "Saving device\n");
		(void) pci_save_config_regs(dip);
	}

}

void
cardbus_restore_children(dev_info_t *dip)
{
	for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
		cardbus_restore_children(ddi_get_child(dip));

		if (strcmp("pcs", ddi_node_name(dip)) == 0)
			continue;
		if (!is_32bit_pccard(dip))
			continue;
		cardbus_err(dip, 1, "restoring device\n");
		(void) pci_restore_config_regs(dip);
	}

}

static int
cardbus_convert_properties(dev_info_t *dip)
{
	struct pcm_regs *pcic_avail_p, *old_avail_p;
	pci_regspec_t *cb_avail_p, *new_avail_p;
	pcic_ranges_t *pcic_range_p, *old_range_p;
	cardbus_range_t *cb_range_p, *new_range_p;
	int range_len, range_entries, i;
	int avail_len, avail_entries;

#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 6, "cardbus_convert_properties\n");
#endif

	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
	    "#address-cells", 3) != DDI_SUCCESS) {
		cardbus_err(dip, 1, "cardbus_convert_properties: "
		    "failed to update #address-cells property\n");
		return (DDI_FAILURE);
	}
	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
	    "#size-cells", 2) != DDI_SUCCESS) {
		cardbus_err(dip, 1, "cardbus_convert_properties: "
		    "failed to update #size-cells property\n");
		return (DDI_FAILURE);
	}

	if (ddi_getlongprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS, "available",
	    (caddr_t)&pcic_avail_p, &avail_len) != DDI_PROP_SUCCESS) {
		cardbus_err(dip, 1, "cardbus_convert_properties: "
		    "no available property for pcmcia\n");
	} else {
		avail_entries = avail_len / sizeof (struct pcm_regs);
		cb_avail_p = kmem_alloc(sizeof (pci_regspec_t) * avail_entries,
		    KM_SLEEP);

		old_avail_p = pcic_avail_p;
		new_avail_p = cb_avail_p;
		for (i = 0; i < avail_entries;
		    i++, old_avail_p++, new_avail_p++) {
			new_avail_p->pci_phys_hi = old_avail_p->phys_hi;
			new_avail_p->pci_phys_mid = 0;
			new_avail_p->pci_phys_low = old_avail_p->phys_lo;
			new_avail_p->pci_size_hi = 0;
			new_avail_p->pci_size_low = old_avail_p->phys_len;
		}

		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
		    "available",
		    (int *)cb_avail_p,
		    (sizeof (pci_regspec_t) * avail_entries)/sizeof (int));

		kmem_free(pcic_avail_p, avail_len);
		kmem_free(cb_avail_p, sizeof (pci_regspec_t) * avail_entries);
	}

	if (ddi_getlongprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS, "ranges",
	    (caddr_t)&pcic_range_p, &range_len) != DDI_PROP_SUCCESS) {
		cardbus_err(dip, 1, "cardbus_convert_properties: "
		    "no ranges property for pcmcia\n");
	} else {
		range_entries = range_len / sizeof (pcic_ranges_t);
		cb_range_p = kmem_alloc(
		    sizeof (cardbus_range_t) * range_entries, KM_SLEEP);

		old_range_p = pcic_range_p;
		new_range_p = cb_range_p;
		for (i = 0; i < range_entries;
		    i++, old_range_p++, new_range_p++) {
			new_range_p->child_hi =
			    old_range_p->pcic_range_caddrhi;
			new_range_p->child_mid = 0;
			new_range_p->child_lo =
			    old_range_p->pcic_range_caddrlo;
			new_range_p->parent_hi =
			    old_range_p->pcic_range_paddrhi;
			new_range_p->parent_mid =
			    old_range_p->pcic_range_paddrmid;
			new_range_p->parent_lo =
			    old_range_p->pcic_range_paddrlo;
			new_range_p->size_hi = 0;
			new_range_p->size_lo = old_range_p->pcic_range_size;
		}

		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
		    (int *)cb_range_p,
		    (sizeof (cardbus_range_t) * range_entries)/sizeof (int));

		kmem_free(pcic_range_p, range_len);
		kmem_free(cb_range_p, sizeof (cardbus_range_t) * range_entries);
	}

	return (DDI_SUCCESS);
}

static void
cardbus_revert_properties(dev_info_t *dip)
{
#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 6, "cardbus_revert_properties\n");
#endif

	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "#address-cells");

	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "#size-cells");

	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
}

static int
cardbus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
    ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
#if defined(CARDBUS_DEBUG)
	if ((ch_dip != dip) || (cardbus_debug >= 9))
		cardbus_err(dip, 6,
		    "cardbus_prop_op(%s) (dip=0x%p, op=%d, name=%s)\n",
		    ddi_driver_name(ch_dip), (void *) dip, prop_op, name);
#endif
	return (impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
	    mod_flags, name, valuep, lengthp));
}

static int
cardbus_ctlops(dev_info_t *dip, dev_info_t *rdip,
    ddi_ctl_enum_t ctlop, void *arg, void *result)
{
	pci_regspec_t *regs;
	int	totreg, reglen;
	const char	*dname = ddi_driver_name(dip);

	ASSERT(number_of_cardbus_cards != 0);

	cardbus_err(dip, 6,
	    "cardbus_ctlops(%p, %p, %d, %p, %p)\n",
	    (void *)dip, (void *)rdip, ctlop, (void *)arg, (void *)result);

	switch (ctlop) {
	case DDI_CTLOPS_UNINITCHILD:
		cardbus_removechild((dev_info_t *)arg);
		return (DDI_SUCCESS);
	case DDI_CTLOPS_POWER:
		return (DDI_SUCCESS);

	default:
		/*
		 * Do Nothing
		 */
		cardbus_err(dip, 8,
		    "cardbus_ctlops: Unsupported DDI_CTLOP %d\n", ctlop);
		return (ddi_ctlops(dip, rdip, ctlop, arg, result));

	case DDI_CTLOPS_SIDDEV:		/* see ddi_dev_is_sid(9F) */
		return (DDI_SUCCESS);

	case DDI_CTLOPS_SLAVEONLY:	/* see ddi_slaveonly(9F) */
		return (DDI_FAILURE);	/* cardbus */

	case DDI_CTLOPS_REGSIZE:
	case DDI_CTLOPS_NREGS:
		if (rdip == (dev_info_t *)NULL) {
			*(int *)result = 0;
			return (DDI_FAILURE);
		}
		break;

	case DDI_CTLOPS_IOMIN:
		/*
		 * If we are using the streaming cache, align at
		 * least on a cache line boundary. Otherwise use
		 * whatever alignment is passed in.
		 */

		if (arg) {
			int	val = *((int *)result);

#ifdef  PCI_SBUF_LINE_SIZE
			val = maxbit(val, PCI_SBUF_LINE_SIZE);
#else
			val = maxbit(val, 64);
#endif
			*((int *)result) = val;
		}
		return (DDI_SUCCESS);

	case DDI_CTLOPS_INITCHILD:
		return (cardbus_initchild(rdip, dip, (dev_info_t *)arg,
		    result));

	case DDI_CTLOPS_REPORTDEV:
		if (rdip == (dev_info_t *)0)
			return (DDI_FAILURE);

		if (strcmp("pcs", ddi_node_name(rdip)) == 0)
			cardbus_err(dip, 1,
			    "cardbus_ctlops: PCCard socket %d at %s@%s\n",
			    ddi_get_instance(rdip),
			    dname, ddi_get_name_addr(dip));
		else {
			pci_regspec_t *pci_rp;
			dev_info_t *next;
			int	length;

			if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
			    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
			    (uint_t *)&length) != DDI_PROP_SUCCESS)
				return (DDI_FAILURE);

			if (pci_rp->pci_phys_hi == 0)
				cardbus_err(dip, 1, "%s%d at %s@%s\n",
				    ddi_driver_name(rdip),
				    ddi_get_instance(rdip),
				    dname, ddi_get_name_addr(dip));
			else {
				uint8_t bus, device, function;
				int32_t val32;
				char	*ptr, buf[128];

				bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
				device = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
				function = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);

				ptr = buf;
				(void) sprintf(ptr, "  "
				    "Bus %3d Device %2d Function %2d",
				    bus, device, function);
				ptr = &ptr[strlen(ptr)];

				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
				    "vendor-id", -1);
				if (val32 != -1) {
					(void) sprintf(ptr, " Vendor 0x%04x",
					    val32);
					ptr = &ptr[strlen(ptr)];
				}
				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
				    "device-id", -1);
				if (val32 != -1) {
					(void) sprintf(ptr, " Device 0x%04x",
					    val32);
					ptr = &ptr[strlen(ptr)];
				}
				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
				    "class-code", -1);
				if (val32 != -1) {
					const char	*name;

					if ((name = ddi_get_name(rdip)) !=
					    NULL)
						(void) sprintf(ptr, " Name %s",
						    name);
					else
						(void) sprintf(ptr,
						    " Class 0x%x", val32 >> 8);
					ptr = &ptr[strlen(ptr)];
				}

				*ptr++ = '\n';
				ASSERT(((caddr_t)ptr - (caddr_t)buf) <
				    sizeof (buf));
				*ptr = '\0';

				cardbus_err(dip, 1, buf);
			}
			ddi_prop_free(pci_rp);

			for (next = ddi_get_child(rdip); next;
			    next = ddi_get_next_sibling(next))
				(void) cardbus_ctlops(next, next,
				    DDI_CTLOPS_REPORTDEV, arg, result);
		}
		return (DDI_SUCCESS);
	}
	*(int *)result = 0;

	if (ddi_getlongprop(DDI_DEV_T_NONE, rdip,
	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
	    (caddr_t)&regs, &reglen) != DDI_SUCCESS)
		return (DDI_FAILURE);

	totreg = reglen / sizeof (pci_regspec_t);
	if (ctlop == DDI_CTLOPS_NREGS) {
		cardbus_err(dip, 6,
		    "cardbus_ctlops, returning NREGS = %d\n", totreg);
		*(int *)result = totreg;
	} else if (ctlop == DDI_CTLOPS_REGSIZE) {
		const int	rn = *(int *)arg;
		if (rn > totreg)
			return (DDI_FAILURE);
		cardbus_err(dip, 6,
		    "cardbus_ctlops, returning REGSIZE(%d) = %d\n",
		    rn, regs[rn].pci_size_low);
		*(off_t *)result = regs[rn].pci_size_low;
	}
	kmem_free(regs, reglen);
	return (DDI_SUCCESS);
}

static void
cardbus_init_child_regs(dev_info_t *child)
{
	ddi_acc_handle_t config_handle;
	uint16_t command_preserve, command;
#if !defined(__x86)
	uint8_t bcr;
#endif
	uint8_t header_type;
	uint8_t min_gnt, latency_timer;
	uint_t n;

	/*
	 * Map the child configuration space to for initialization.
	 *
	 *  Set the latency-timer register to values appropriate
	 *  for the devices on the bus (based on other devices
	 *  MIN_GNT and MAX_LAT registers.
	 *
	 *  Set the fast back-to-back enable bit in the command
	 *  register if it's supported and all devices on the bus
	 *  have the capability.
	 *
	 */
	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS)
		return;

	cardbus_err(child, 6, "cardbus_init_child_regs()\n");

	/*
	 * Determine the configuration header type.
	 */
	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);

	/*
	 * Support for "command-preserve" property.  Note that we
	 * add PCI_COMM_BACK2BACK_ENAB to the bits to be preserved
	 * since the obp will set this if the device supports and
	 * all targets on the same bus support it.  Since psycho
	 * doesn't support PCI_COMM_BACK2BACK_ENAB, it will never
	 * be set.  This is just here in case future revs do support
	 * PCI_COMM_BACK2BACK_ENAB.
	 */
	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
	    DDI_PROP_DONTPASS,
	    "command-preserve", 0);
	command = pci_config_get16(config_handle, PCI_CONF_COMM);
	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
	command |= (cardbus_command_default & ~command_preserve);
	pci_config_put16(config_handle, PCI_CONF_COMM, command);
	command = pci_config_get16(config_handle, PCI_CONF_COMM);

#if !defined(__x86)
	/*
	 * If the device has a bus control register then program it
	 * based on the settings in the command register.
	 */
	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
		if (cardbus_command_default & PCI_COMM_PARITY_DETECT)
			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
		if (cardbus_command_default & PCI_COMM_SERR_ENABLE)
			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
	}
#endif

	/*
	 * Initialize cache-line-size configuration register if needed.
	 */
	if (ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
	    "cache-line-size", 0) == 0) {

		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
		    PCI_CACHE_LINE_SIZE);
		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
		if (n != 0)
			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
			    "cache-line-size", n);
	}

	/*
	 * Initialize latency timer registers if needed.
	 */
	if (ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
	    "latency-timer", 0) == 0) {

		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
			latency_timer = cardbus_latency_timer;
			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
			    latency_timer);
		} else {
			min_gnt = pci_config_get8(config_handle,
			    PCI_CONF_MIN_G);

			/*
			 * Cardbus os only 33Mhz
			 */
			if (min_gnt != 0) {
				latency_timer = min_gnt * 8;
			}
		}
		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
		    latency_timer);
		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
		if (n != 0)
			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
			"latency-timer", n);
	}

	pci_config_teardown(&config_handle);
}

static int
cardbus_initchild(dev_info_t *rdip, dev_info_t *dip, dev_info_t *child,
    void *result)
{
	char	name[MAXNAMELEN];
	const char	*dname = ddi_driver_name(dip);
	const struct cb_ops *cop;

	_NOTE(ARGUNUSED(rdip, result))

	cardbus_err(child, 6, "cardbus_initchild\n");

	/*
	 * Name the child
	 */
	if (cardbus_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
		return (DDI_FAILURE);

	ddi_set_name_addr(child, name);
	ddi_set_parent_data(child, NULL);

	if (ndi_dev_is_persistent_node(child) == 0) {
		/*
		 * Try to merge the properties from this prototype
		 * node into real h/w nodes.
		 */
		if (ndi_merge_node(child, cardbus_name_child) == DDI_SUCCESS) {
			/*
			 * Merged ok - return failure to remove the node.
			 */
			cardbus_removechild(child);
			return (DDI_FAILURE);
		}
		/*
		 * The child was not merged into a h/w node,
		 * but there's not much we can do with it other
		 * than return failure to cause the node to be removed.
		 */
		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
		    ddi_driver_name(child), ddi_get_name_addr(child),
		    ddi_driver_name(child));
		cardbus_removechild(child);
		return (DDI_NOT_WELL_FORMED);
	}
	cop = DEVI(dip)->devi_ops->devo_cb_ops;

	if ((cop == NULL) || (!(cop->cb_flag & D_HOTPLUG))) {
		cmn_err(CE_WARN, "%s: driver doesn't support HOTPLUG\n", dname);
		return (DDI_FAILURE);
	}

	cardbus_init_child_regs(child);

	/*
	 * Create ppd if needed.
	 */
	if (ddi_get_parent_data(child) == NULL) {
		struct cardbus_parent_private_data *ppd;

#ifdef sparc
		ppd = (struct cardbus_parent_private_data *)
		    kmem_zalloc(sizeof (struct cardbus_parent_private_data),
		    KM_SLEEP);

#elif defined(__x86)
		ppd = (struct cardbus_parent_private_data *)
		    kmem_zalloc(sizeof (struct cardbus_parent_private_data)
		    + sizeof (struct intrspec), KM_SLEEP);

		ppd->ppd.par_intr = (struct intrspec *)(ppd + 1);
		(ppd->ppd.par_intr)->intrspec_pri = 0;
		(ppd->ppd.par_intr)->intrspec_vec = 0;
		(ppd->ppd.par_intr)->intrspec_func = (uint_t (*)()) 0;
#endif

		if (ddi_getprop(DDI_DEV_T_NONE, child, DDI_PROP_DONTPASS,
		    "interrupts", -1) != -1)
			ppd->ppd.par_nintr = 1;

		ppd->code = CB_PPD_CODE;

		cardbus_err(child, 5,
		    "cardbus_initchild: Creating empty ppd\n");
		ppd->ppd.par_nreg = 0;
		ppd->ppd.par_reg = NULL;

		ddi_set_parent_data(child, (caddr_t)ppd);
	}

	return (DDI_SUCCESS);
}

static int
cardbus_name_child(dev_info_t *child, char *name, int namelen)
{
	pci_regspec_t *pci_rp;
	char	**unit_addr;
	uint_t n;
	int	bus, device, func;

	/*
	 * Pseudo nodes indicate a prototype node with per-instance
	 * properties to be merged into the real h/w device node.
	 * The interpretation of the unit-address is DD[,F]
	 * where DD is the device id and F is the function.
	 */
	if (ndi_dev_is_persistent_node(child) == 0) {
		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
		    DDI_PROP_DONTPASS,
		    "unit-address", &unit_addr, &n) != DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "cannot name node from %s.conf",
			    ddi_driver_name(child));
			return (DDI_FAILURE);
		}
		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
			cmn_err(CE_WARN, "unit-address property in %s.conf"
			    " not well-formed", ddi_driver_name(child));
			ddi_prop_free(unit_addr);
			return (DDI_FAILURE);
		}
		(void) snprintf(name, namelen, "%s", *unit_addr);
		ddi_prop_free(unit_addr);
		return (DDI_SUCCESS);
	}

	/*
	 * Get the address portion of the node name based on
	 * the function and device number.
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
	device = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
	ddi_prop_free(pci_rp);

	if (func != 0)
		(void) snprintf(name, namelen, "%x,%x", device, func);
	else
		(void) snprintf(name, namelen, "%x", device);

	cardbus_err(child, 8,
	    "cardbus_name_child: system init done [%x][%x][%x]"
	    " for %s [%s] nodeid: %x @%s\n",
	    bus, device, func,
	    ddi_get_name(child), ddi_get_name_addr(child),
	    DEVI(child)->devi_nodeid, name);

	return (DDI_SUCCESS);
}

static void
cardbus_removechild(dev_info_t *dip)
{
	struct cardbus_parent_private_data *ppd;

	ddi_set_name_addr(dip, NULL);
	impl_rem_dev_props(dip);
	ppd = (struct cardbus_parent_private_data *)ddi_get_parent_data(dip);
	if (ppd && (ppd->code == CB_PPD_CODE)) {
		if (ppd->ppd.par_reg && (ppd->ppd.par_nreg > 0))
			kmem_free((caddr_t)ppd->ppd.par_reg,
			    ppd->ppd.par_nreg * sizeof (struct regspec));
#ifdef sparc
		kmem_free(ppd, sizeof (struct cardbus_parent_private_data));
#elif defined(__x86)
		kmem_free(ppd, sizeof (struct cardbus_parent_private_data) +
		    sizeof (struct intrspec));
#endif
		cardbus_err(dip, 5,
		    "cardbus_removechild: ddi_set_parent_data(NULL)\n");
		ddi_set_parent_data(dip, NULL);
	}
}


static char	cb_bnamestr[] = "binding_name";
static char	cb_venidstr[] = "VendorID";
static char	cb_devidstr[] = "DeviceID";
static char	cb_nnamestr[] = "nodename";

static cb_props_parse_tree_t cb_props_parse_tree[] = {
	{ cb_bnamestr, PT_STATE_STRING_VAR },
	{ cb_venidstr, PT_STATE_HEX_VAR },
	{ cb_devidstr, PT_STATE_HEX_VAR } };

static int
check_token(char *token, int *len)
{
	int	state = PT_STATE_DEC_VAR;
	int	sl = strlen(token), il = 1;
	char	c;

	if (token[0] == '0' && token[2] && (token[1] == 'x' || token[1] ==
	    'X')) {
		state = PT_STATE_HEX_VAR;
		token += 2;
	}

	while (c = *token++) {
		if (isdigit(c))
			continue;
		if (c == PARSE_COMMA) {
			il++;
			if (token[0] == '0' && token[2] && isx(token[1])) {
				state = PT_STATE_HEX_VAR;
				token += 2;
			}
			continue;
		}
		if (!isxdigit(c)) {
			*len = sl;
			return (PT_STATE_STRING_VAR);
		}
		state = PT_STATE_HEX_VAR;
	}
	*len = il;
	return (state);
}


static char *
find_token(char **cp, int *l, char *endc)
{
	char	*cpp = *cp;

	while ((**cp && (isalpha(**cp) || isxdigit(**cp) ||
	    (**cp == PARSE_UNDERSCORE) ||
	    (**cp == PARSE_COMMA) ||
	    (**cp == PARSE_DASH)))) {
		(*cp)++;
		(*l)++;
	}

	*endc = **cp;
	**cp = '\0';

	return (cpp);
}

static int
parse_token(char *token)
{
	cb_props_parse_tree_t *pt = cb_props_parse_tree;
	int	k = sizeof (cb_props_parse_tree) /
	    sizeof (cb_props_parse_tree_t);

	while (k--) {
		if (strcmp((char *)token, pt->token) == 0)
			return (pt->state);
		pt++;
	}

	return (PT_STATE_UNKNOWN);
}

static int
token_to_hex(char *token, unsigned *val, int len)
{
	uchar_t c;

	*val = 0;
	if (token[0] == '0' && (token[1] == 'x' || token[1] == 'X')) {
		token += 2;
	}

	while (*token) {
		if (!isxdigit(*token)) {
			if (*token == PARSE_COMMA) {
				if (!(--len))
					return (1);
				val++;
				*val = 0;
				token++;
				if (token[0] == '0' && (token[1] == 'x' ||
				    token[1] == 'X')) {
					token += 2;
				}
				continue;
			}
			return (0);
		}
		c = toupper(*token);
		if (c >= 'A')
			c = c - 'A' + 10 + '0';
		*val = ((*val * 16) + (c - '0'));
		token++;
	}

	return (1);
}

static int
token_to_dec(char *token, unsigned *val, int len)
{
	*val = 0;

	while (*token) {
		if (!isdigit(*token)) {
			if (*token == PARSE_COMMA) {
				if (!(--len))
					return (1);
				val++;
				*val = 0;
				token++;
				continue;
			}
			return (0);
		}
		*val = ((*val * 10) + (*token - '0'));
		token++;
	}

	return (1);
}

static void
cardbus_add_prop(struct cb_deviceset_props *cdsp, int type, char *name,
    caddr_t vp, int len)
{
	ddi_prop_t *propp;
	int	pnlen = strlen(name) + 1;

	propp = (ddi_prop_t *)kmem_zalloc(sizeof (ddi_prop_t), KM_SLEEP);
	propp->prop_name = (char *)kmem_alloc(pnlen, KM_SLEEP);
	propp->prop_val = vp;
	bcopy(name, propp->prop_name, pnlen);
	propp->prop_len = len;
	propp->prop_flags = type;
	propp->prop_next = cdsp->prop_list;
	cdsp->prop_list = propp;
}

static void
cardbus_add_stringprop(struct cb_deviceset_props *cdsp, char *name,
    char *vp, int len)
{
	char	*nstr = kmem_zalloc(len + 1, KM_SLEEP);

	bcopy(vp, nstr, len);
	cardbus_add_prop(cdsp, DDI_PROP_TYPE_STRING, name, (caddr_t)nstr,
	    len + 1);
}

static void
cardbus_prop_free(ddi_prop_t *propp)
{
	if (propp->prop_len) {
		switch (propp->prop_flags) {
		case DDI_PROP_TYPE_STRING:
			kmem_free(propp->prop_val, propp->prop_len);
			break;
		case DDI_PROP_TYPE_INT:
			kmem_free(propp->prop_val,
			    propp->prop_len * sizeof (int));
			break;
		}
	}
	kmem_free(propp->prop_name, strlen(propp->prop_name) + 1);
	kmem_free(propp, sizeof (ddi_prop_t *));
}

static void
cardbus_devprops_free(struct cb_deviceset_props *cbdp)
{
	ddi_prop_t *propp, *npropp;

	propp = cbdp->prop_list;
	while (propp) {
		npropp = propp->prop_next;
		cardbus_prop_free(propp);
		propp = npropp;
	}
	if (cbdp->nodename)
		kmem_free(cbdp->nodename, strlen(cbdp->nodename) + 1);
	if (cbdp->binding_name)
		kmem_free(cbdp->binding_name, strlen(cbdp->binding_name) +
		    1);
	kmem_free(cbdp, sizeof (*cbdp));
}

/*
 * Format of "cb-device-init-props" property:
 * Anything before the semi-colon is an identifying equate, anything
 * after the semi-colon is a setting equate.
 *
 * "binding_name=xXxXxX VendorID=NNNN DeviceID=NNNN; nodename=NewName
 *					Prop=PropVal"
 *
 */
static int
cardbus_parse_devprop(cbus_t *cbp, char *cp)
{
	int	state = PT_STATE_TOKEN, qm = 0, em = 0, smc = 0, l = 0;
	int	length;
	char	*token = "beginning of line";
	char	*ptoken = NULL, *quote;
	char	eq = '\0';
	struct cb_deviceset_props *cdsp;

	cdsp = kmem_zalloc(sizeof (*cdsp), KM_SLEEP);
	length = strlen(cp);

	while ((*cp) && (l < length)) {
		/*
		 * Check for escaped characters
		 */
		if (*cp == PARSE_ESCAPE) {
			char	*cpp = cp, *cppp = cp + 1;

			em = 1;

			if (!qm) {
				cmn_err(CE_CONT, "cardbus_parse_devprop: "
				    "escape not allowed outside "
				    "of quotes at [%s]\n", token);
				return (DDI_FAILURE);

			} /* if (!qm) */

			while (*cppp)
				*cpp++ = *cppp++;

			l++;

			*cpp = '\0';
		} /* PARSE_ESCAPE */

		/*
		 * Check for quoted strings
		 */
		if (!em && (*cp == PARSE_QUOTE)) {
			qm ^= 1;
			if (qm) {
				quote = cp + 1;
			} else {
				*cp = '\0';
				if (state == PT_STATE_CHECK) {
					if (strcmp(token, cb_nnamestr) == 0) {
						cdsp->nodename = kmem_alloc(
						    strlen(quote) + 1,
						    KM_SLEEP);
						(void) strcpy(cdsp->nodename,
						    quote);
					} else
						cardbus_add_stringprop(cdsp,
						    token, quote,
						    strlen(quote));
				} else if (state != PT_STATE_STRING_VAR) {
					cmn_err(CE_CONT,
					    "cardbus_parse_devprop: "
					    "unexpected string [%s] after "
					    "[%s]\n", quote, token);
					return (DDI_FAILURE);
				} else {
					if (strcmp(token, cb_bnamestr) == 0) {
						cdsp->binding_name = kmem_alloc(
						    strlen(quote) + 1,
						    KM_SLEEP);
						(void) strcpy(
						    cdsp->binding_name, quote);
					}
				}
				state = PT_STATE_TOKEN;
			} /* if (qm) */
		} /* PARSE_QUOTE */

		em = 0;

		if (!qm && (*cp == PARSE_SEMICOLON)) {
			smc = 1;
		}

		/*
		 * Check for tokens
		 */
		else if (!qm && (isalpha(*cp) || isxdigit(*cp))) {
			int	tl;
			unsigned	*intp;
			ptoken = token;
			token = find_token(&cp, &l, &eq);

			switch (state) {
			case PT_STATE_TOKEN:
				if (smc) {
					if (eq == PARSE_EQUALS)
						state = PT_STATE_CHECK;
					else
						cardbus_add_prop(cdsp,
						    DDI_PROP_TYPE_ANY,
						    token,
						    NULL, 0);
				} else if (eq == PARSE_EQUALS)
					switch (state = parse_token(token)) {
					case PT_STATE_UNKNOWN:
						cmn_err(CE_CONT,
						    "cardbus_parse_devprop: "
						    "unknown token [%s]\n",
						    token);
						state = PT_STATE_TOKEN;
					} /* switch (parse_token) */
				else
					state = PT_STATE_TOKEN;
				break;

			case PT_STATE_CHECK:
				switch (check_token(token, &tl)) {
				case PT_STATE_DEC_VAR:
					intp = (unsigned *)kmem_alloc(
					    sizeof (int)*tl,
					    KM_SLEEP);
					if (token_to_dec(token, intp, tl))
						cardbus_add_prop(cdsp,
						    DDI_PROP_TYPE_INT, ptoken,
						    (caddr_t)intp, tl);
					else
						kmem_free(intp,
						    sizeof (int)*tl);
					break;
				case PT_STATE_HEX_VAR:
					intp = (unsigned *)kmem_alloc(
					    sizeof (int)*tl,
					    KM_SLEEP);
					if (token_to_hex(token, intp, tl))
						cardbus_add_prop(cdsp,
						    DDI_PROP_TYPE_INT,
						    ptoken,
						    (caddr_t)intp, tl);
					else
						kmem_free(intp,
						    sizeof (int)*tl);
					break;
				case PT_STATE_STRING_VAR:
					if (strcmp(ptoken, cb_nnamestr) == 0) {
						cdsp->nodename = kmem_alloc(
						    tl + 1, KM_SLEEP);
						(void) strcpy(cdsp->nodename,
						    token);
					} else
						cardbus_add_stringprop(cdsp,
						    ptoken, token, tl);
					break;
				}
				state = PT_STATE_TOKEN;
				break;

			case PT_STATE_HEX_VAR:
				if (strcmp(ptoken, cb_venidstr) == 0) {
					uint_t val;
					if (token_to_hex(token, &val, 1))
						cdsp->venid = val;
				} else if (strcmp(ptoken, cb_devidstr) == 0) {
					uint_t val;
					if (token_to_hex(token, &val, 1))
						cdsp->devid = val;
				}
				state = PT_STATE_TOKEN;
				break;

			case PT_STATE_DEC_VAR:
				if (strcmp(ptoken, cb_venidstr) == 0) {
					uint_t val;
					if (token_to_dec(token, &val, 1))
						cdsp->venid = val;
				} else if (strcmp(ptoken, cb_devidstr) == 0) {
					uint_t val;
					if (token_to_dec(token, &val, 1))
						cdsp->devid = val;
				}
				state = PT_STATE_TOKEN;
				break;

			case PT_STATE_STRING_VAR:
				if (strcmp(ptoken, cb_bnamestr) == 0) {
					cdsp->binding_name = kmem_alloc(
					    strlen(token) + 1, KM_SLEEP);
					(void) strcpy(cdsp->binding_name,
					    token);
				}
				state = PT_STATE_TOKEN;
				break;

			default:
				cmn_err(CE_CONT, "cardbus_parse_devprop: "
				    "unknown state machine state = %d\n",
				    state);

				cardbus_devprops_free(cdsp);
				return (DDI_FAILURE);
			} /* switch (state) */
			if (eq == PARSE_SEMICOLON)
				smc = 1;
		}
		cp++;
		l++;
	} /* while (*cp) */

	if (qm) {
		cmn_err(CE_CONT, "cb_props_parse_line: unterminated "
		    "string = [%s]\n", quote);
		cardbus_devprops_free(cdsp);
		return (DDI_FAILURE);
	}

	if (state != PT_STATE_TOKEN) {
		cmn_err(CE_CONT, "cardbus_parse_devprop: token [%s] "
		    "requires value\n", token);
		cardbus_devprops_free(cdsp);
		return (DDI_FAILURE);
	}

	if (cdsp->venid == 0 || cdsp->devid == 0) {
		cmn_err(CE_CONT, "cardbus_parse_devprop: Entry "
		    "requires VendorID and DeviceID\n");
		cardbus_devprops_free(cdsp);
		return (DDI_FAILURE);
	}

	cdsp->next = cbp->cb_dsp;
	cbp->cb_dsp = cdsp;
	return (DDI_SUCCESS);
}

static void
cardbus_device_props(cbus_t *cbp)
{
	char	**prop_array;
	uint_t i, n;

	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, cbp->cb_dip,
	    DDI_PROP_DONTPASS,
	    "cb-device-init-props", &prop_array,
	    &n) != DDI_PROP_SUCCESS)
		return;

	for (i = 0; i < n; i++)
		(void) cardbus_parse_devprop(cbp, prop_array[i]);

	ddi_prop_free(prop_array);
}

static int
cardbus_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
    off_t offset, off_t len, caddr_t *vaddrp)
{
	register dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
	int	rc;

	cardbus_err(dip, 9,
	    "cardbus_bus_map(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	/* A child has asked us to set something up */
	cardbus_err(dip, 9,
	    "cardbus_bus_map(%s) calling %s - 0x%p, "
	    "offset 0x%x, len 0x%x\n",
	    ddi_driver_name(rdip),
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_map,
	    (int)offset, (int)len);

	rc = (DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
	    (pdip, rdip, mp, offset, len, vaddrp);
	/* rc = ddi_map(dip, mp, offset, len, vaddrp); */

	if (rc != DDI_SUCCESS) {
		cardbus_err(rdip, 8, "cardbus_bus_map failed, rc = %d\n", rc);
		return (DDI_FAILURE);
	} else {
		cardbus_err(rdip, 9, "cardbus_bus_map OK\n");
		return (DDI_SUCCESS);
	}
}

static void
pcirp2rp(const pci_regspec_t *pci_rp, struct regspec *rp)
{
	/* bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi); */
	if (PCI_REG_ADDR_G(pci_rp->pci_phys_hi) ==
	    PCI_REG_ADDR_G(PCI_ADDR_IO)) {
		/* I/O */
		rp->regspec_bustype = 1;
	} else {
		/* memory */
		rp->regspec_bustype = 0;
	}
	rp->regspec_addr = pci_rp->pci_phys_low;
	rp->regspec_size = pci_rp->pci_size_low;
}

static int
cardbus_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
    int (*waitfp)(caddr_t), caddr_t arg,
    ddi_dma_handle_t *handlep)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_allochdl(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_allochdl calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_allochdl);

	return (ddi_dma_allochdl(dip, rdip, attr, waitfp, arg, handlep));
}

static int
cardbus_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_freehdl(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_freehdl calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_freehdl);

	return (ddi_dma_freehdl(dip, rdip, handle));
}

static int
cardbus_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cp, uint_t *ccountp)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_bindhdl(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_bindhdl calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl);

	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl(pdip,
	    rdip, handle, dmareq, cp, ccountp));
}

static int
cardbus_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_unbindhdl(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_unbindhdl calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl);

	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl(pdip,
	    rdip, handle));
}

static int
cardbus_dma_flush(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, off_t off, size_t len,
    uint_t cache_flags)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_flush(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_flush calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush);

	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush(pdip, rdip,
	    handle, off, len, cache_flags));
}

static int
cardbus_dma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, uint_t win, off_t *offp,
    size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	dev_info_t *pdip = ddi_get_parent(dip);
	cardbus_err(dip, 6,
	    "cardbus_dma_win(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 8,
	    "cardbus_dma_win calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win);

	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win(pdip, rdip,
	    handle, win, offp, lenp, cookiep, ccountp));
}

static int
cardbus_dma_map(dev_info_t *dip, dev_info_t *rdip,
    struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
{
	dev_info_t *pdip = ddi_get_parent(dip);

	cardbus_err(dip, 10,
	    "cardbus_dma_map(dip=0x%p, rdip=0x%p)\n",
	    (void *) dip, (void *) rdip);

	if (pdip == NULL)
		return (DDI_FAILURE);

	cardbus_err(dip, 11,
	    "cardbus_dma_map calling %s - 0x%p\n",
	    ddi_driver_name(pdip),
	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map);

	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map(pdip, rdip,
	    dmareqp, handlep));
}

static int
cardbus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
    char *eventname, ddi_eventcookie_t *cookiep)
{
	cbus_t *cbp;
	int	cb_instance;
	int	rc;

	/*
	 * get the soft state structure for the bus instance.
	 */
	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	ASSERT(cb_instance >= 0);
	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	cardbus_err(dip, 6, "cardbus_get_eventcookie %s\n", eventname);

	ASSERT(number_of_cardbus_cards != 0);

	if (cbp->cb_ndi_event_hdl == NULL) {
		/*
		 * We can't handle up (probably called at the attachment
		 * point) so pass it on up
		 */
		dev_info_t *pdip = ddi_get_parent(dip);
		cardbus_err(dip, 8,
		    "cardbus_get_eventcookie calling %s - 0x%p\n",
		    ddi_driver_name(pdip),
		    (void *)
		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie);
		return (DEVI(pdip)->devi_ops->devo_bus_ops->
		    bus_get_eventcookie(pdip, rdip, eventname, cookiep));
	}

	cardbus_err(dip, 8,
	    "cardbus_get_eventcookie calling ndi_event_retrieve_cookie\n");

	rc = ndi_event_retrieve_cookie(cbp->cb_ndi_event_hdl, rdip, eventname,
	    cookiep, NDI_EVENT_NOPASS);

	cardbus_err(dip, 7,
	    "cardbus_get_eventcookie rc %d cookie %p\n", rc, (void *)*cookiep);
	return (rc);
}

static int
cardbus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
    ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
    ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
    void *arg, ddi_callback_id_t *cb_id)
{
	cbus_t *cbp;
	int	cb_instance;
	int	rc;

	/*
	 * get the soft state structure for the bus instance.
	 */
	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	ASSERT(cb_instance >= 0);
	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	cardbus_err(dip, 6, "cardbus_add_eventcall\n");

	ASSERT(number_of_cardbus_cards != 0);

	if (cbp->cb_ndi_event_hdl == NULL) {
		/*
		 * We can't handle up (probably called at the attachment
		 * point) so pass it on up
		 */
		dev_info_t *pdip = ddi_get_parent(dip);
		cardbus_err(dip, 8,
		    "cardbus_add_eventcall calling %s - 0x%p\n",
		    ddi_driver_name(pdip),
		    (void *)
		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_add_eventcall);
		return (DEVI(pdip)->devi_ops->devo_bus_ops->
		    bus_add_eventcall(pdip, rdip, cookie, callback,
		    arg, cb_id));
	}

	cardbus_err(dip, 8,
	    "cardbus_add_eventcall calling ndi_event_add_callback\n");

	rc = ndi_event_add_callback(cbp->cb_ndi_event_hdl, rdip, cookie,
	    callback, arg, NDI_EVENT_NOPASS, cb_id);
	cardbus_err(dip, 7,
	    "cardbus_add_eventcall rc %d cookie %p\n", rc, (void *)cookie);
	return (rc);
}

static int
cardbus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
{
	cbus_t *cbp;
	int	cb_instance;

	/*
	 * get the soft state structure for the bus instance.
	 */
	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "cbus-instance", -1);
	ASSERT(cb_instance >= 0);
	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);

	cardbus_err(dip, 6, "cardbus_remove_eventcall\n");

	ASSERT(number_of_cardbus_cards != 0);

	if (cbp->cb_ndi_event_hdl == NULL) {
		/*
		 * We can't handle up (probably called at the attachment
		 * point) so pass it on up
		 */
		dev_info_t *pdip = ddi_get_parent(dip);
		cardbus_err(dip, 8,
		    "cardbus_remove_eventcall calling %s - 0x%p\n",
		    ddi_driver_name(pdip),
		    (void *)
		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_remove_eventcall);
		return (DEVI(pdip)->devi_ops->devo_bus_ops->
		    bus_remove_eventcall(pdip, cb_id));
	}

	return (ndi_event_remove_callback(cbp->cb_ndi_event_hdl, cb_id));
}

static int
cardbus_post_event(dev_info_t *dip, dev_info_t *rdip,
    ddi_eventcookie_t cookie, void *bus_impldata)
{
	_NOTE(ARGUNUSED(rdip, cookie, bus_impldata))
	cardbus_err(dip, 1, "cardbus_post_event()\n");
	return (DDI_FAILURE);
}

static int cardbus_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
		ddi_intr_handle_impl_t *hdlp);
static int cardbus_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
		ddi_intr_handle_impl_t *hdlp);
static int cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
		ddi_intr_handle_impl_t *hdlp);
static int cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
		ddi_intr_handle_impl_t *hdlp);

static int
cardbus_get_pil(dev_info_t *dip)
{
	return ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "interrupt-priorities", 6);
}

static int
cardbus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
    ddi_intr_handle_impl_t *hdlp, void *result)
{
	int ret = DDI_SUCCESS;

#if defined(CARDBUS_DEBUG)
	cardbus_err(dip, 8, "cardbus_intr_ops() intr_op=%d\n", (int)intr_op);
#endif

	switch (intr_op) {
	case DDI_INTROP_GETCAP:
		*(int *)result = DDI_INTR_FLAG_LEVEL;
		break;
	case DDI_INTROP_ALLOC:
		*(int *)result = hdlp->ih_scratch1;
		break;
	case DDI_INTROP_FREE:
		break;
	case DDI_INTROP_GETPRI:
		*(int *)result = hdlp->ih_pri ?
		    hdlp->ih_pri : cardbus_get_pil(dip);
		break;
	case DDI_INTROP_SETPRI:
		break;
	case DDI_INTROP_ADDISR:
	case DDI_INTROP_REMISR:
		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
			cardbus_err(dip, 1, "Only fixed interrupts\n");
			return (DDI_FAILURE);
		}
		break;
	case DDI_INTROP_ENABLE:
		ret = cardbus_enable_intr_impl(dip, rdip, hdlp);
		break;
	case DDI_INTROP_DISABLE:
		ret = cardbus_disable_intr_impl(dip, rdip, hdlp);
		break;
	case DDI_INTROP_NINTRS:
	case DDI_INTROP_NAVAIL:
#ifdef sparc
		*(int *)result = i_ddi_get_intx_nintrs(rdip);
#else
		*(int *)result = 1;
#endif
		break;
	case DDI_INTROP_SUPPORTED_TYPES:
		*(int *)result = DDI_INTR_TYPE_FIXED;
		break;
	default:
		ret = DDI_ENOTSUP;
		break;
	}

	return (ret);
}

static int
cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
    ddi_intr_handle_impl_t *hdlp)
{
	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
	set_irq_handler_t sih;
	uint_t socket = 0; /* We only support devices */
			    /* with one socket per function */

	ASSERT(anp != NULL);

	cardbus_err(dip, 9,
	    "cardbus_enable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
	    "rdip=0x%p(%s)\n",
	    (void *) hdlp->ih_cb_func,
	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
	    (void *) rdip, ddi_driver_name(rdip));

	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
		cardbus_err(dip, 1, "Only fixed interrupts\n");
		return (DDI_FAILURE);
	}

	sih.socket = socket;
	sih.handler_id = (unsigned)(long)rdip;
	sih.handler = (f_tt *)(uintptr_t)hdlp->ih_cb_func;
	sih.arg1 = hdlp->ih_cb_arg1;
	sih.arg2 = hdlp->ih_cb_arg2;
	sih.irq = cardbus_get_pil(dip);

	if ((*anp->an_if->pcif_set_interrupt)(dip, &sih) != SUCCESS)
		return (DDI_FAILURE);

	return (DDI_SUCCESS);
}

static int
cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
    ddi_intr_handle_impl_t *hdlp)
{
	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
	clear_irq_handler_t cih;
	uint_t socket = 0; /* We only support devices with 1 socket per */
			    /* function. */

	ASSERT(anp != NULL);

	cardbus_err(dip, 9,
	    "cardbus_disable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
	    "rdip=0x%p(%s%d)\n",
	    (void *) hdlp->ih_cb_func,
	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
	    (void *) rdip, ddi_driver_name(rdip), ddi_get_instance(rdip));

	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
		cardbus_err(dip, 1, "Only fixed interrupts\n");
		return (DDI_FAILURE);
	}

	cih.socket = socket;
	cih.handler_id = (unsigned)(long)rdip;
	cih.handler = (f_tt *)(uintptr_t)hdlp->ih_cb_func;

	if ((*anp->an_if->pcif_clr_interrupt)(dip, &cih) != SUCCESS)
		return (DDI_FAILURE);

	return (DDI_SUCCESS);
}

#if defined(CARDBUS_DEBUG)
static int	cardbus_do_pprintf = 0;
#endif

/*PRINTFLIKE3*/
void
cardbus_err(dev_info_t *dip, int level, const char *fmt, ...)
{
	if (cardbus_debug && (level <= cardbus_debug)) {
		va_list adx;
		int	instance;
		char	buf[256];
		const char	*name;
		char	*nl = "";
#if !defined(CARDBUS_DEBUG)
		int	ce;
		char	qmark = 0;

		if (level <= 3)
			ce = CE_WARN;
		else
			ce = CE_CONT;
		if (level == 4)
			qmark = 1;
#endif

		if (dip) {
			instance = ddi_get_instance(dip);
			/* name = ddi_binding_name(dip); */
			name = ddi_driver_name(dip);
		} else {
			instance = 0;
			name = "";
		}

		va_start(adx, fmt);
		/* vcmn_err(ce, fmt, adx); */
		/* vprintf(fmt, adx); */
		/* prom_vprintf(fmt, adx); */
		(void) vsprintf(buf, fmt, adx);
		va_end(adx);

		if (buf[strlen(buf) - 1] != '\n')
			nl = "\n";

#if defined(CARDBUS_DEBUG)
		if (cardbus_do_pprintf) {
			if (dip) {
				if (instance >= 0)
					prom_printf("%s(%d),0x%p: %s%s",
					    name, instance, (void *)dip,
					    buf, nl);
				else
					prom_printf("%s,0x%p: %s%s", name,
					    (void *)dip, buf, nl);
			} else
				prom_printf("%s%s", buf, nl);
		} else {
			if (dip) {
				if (instance >= 0)
					cmn_err(CE_CONT, "%s(%d),0x%p: %s%s",
					    name, instance, (void *)dip,
					    buf, nl);
				else
					cmn_err(CE_CONT, "%s,0x%p: %s%s",
					    name, (void *)dip, buf, nl);
			} else
				cmn_err(CE_CONT, "%s%s", buf, nl);
		}
#else
		if (dip)
			cmn_err(ce, qmark ? "?%s%d: %s%s" : "%s%d: %s%s",
			    name, instance, buf, nl);
		else
			cmn_err(ce, qmark ? "?%s%s" : "%s%s", buf, nl);
#endif
	}
}

static void cardbus_expand_busrange(dev_info_t *dip)
{
	dev_info_t *pdip;
	cardbus_bus_range_t *bus_range;
	int len;

	pdip = ddi_get_parent(dip);

	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, "bus-range",
	    (caddr_t)&bus_range, &len) == DDI_PROP_SUCCESS) {
		ndi_ra_request_t req;
		uint64_t next_bus, blen;
		uint32_t ret;
		ddi_acc_handle_t handle;

		if (bus_range->lo != bus_range->hi)
			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
			    "%u -> %u\n", bus_range->lo, bus_range->hi);
		else {

			bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
			req.ra_addr = bus_range->lo + 1;
			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
			req.ra_len = 12;

			while ((req.ra_len > 0) &&
			    (ret = ndi_ra_alloc(ddi_get_parent(pdip), &req,
			    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
			    NDI_RA_PASS)) != NDI_SUCCESS)
				req.ra_len--;

			if (ret != NDI_SUCCESS) {
				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
				    "fail to allocate bus number\n");
				goto exit;
			}

			bus_range->hi = bus_range->lo + req.ra_len;
			if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip,
			    "bus-range", (int *)bus_range, 2) != DDI_SUCCESS) {
				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
				    "fail to update bus-range property\n");
				goto exit;
			}

			if (pci_config_setup(pdip, &handle) != DDI_SUCCESS) {
				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
				    "fail to pci_config_setup\n");
				goto exit;
			}

			pci_config_put8(handle, PCI_BCNF_SECBUS, bus_range->lo);
			pci_config_put8(handle, PCI_BCNF_SUBBUS, bus_range->hi);

			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
			    "parent dip %u -> %u\n",
			    pci_config_get8(handle, PCI_BCNF_SECBUS),
			    pci_config_get8(handle, PCI_BCNF_SUBBUS));
			pci_config_teardown(&handle);

			if (ndi_ra_map_setup(pdip, NDI_RA_TYPE_PCI_BUSNUM)
			    != NDI_SUCCESS) {
				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
				    "fail to ndi_ra_map_setup of bus number\n");
				goto exit;
			}

			(void) ndi_ra_free(pdip,
			    (uint64_t)bus_range->lo + 1, req.ra_len,
			    NDI_RA_TYPE_PCI_BUSNUM, 0);
		}

		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
		req.ra_len = 2;

		while ((req.ra_len > 0) &&
		    (ret = ndi_ra_alloc(pdip, &req,
		    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
		    0)) != NDI_SUCCESS)
			req.ra_len--;

		cardbus_err(dip, 1, "cardbus_expand_busrange: "
		    "cardbus dip base %u length %d\n",
		    (int)next_bus, (int)req.ra_len);

		if (ret != NDI_SUCCESS) {
			cardbus_err(dip, 1, "cardbus_expand_busrange: "
			    "fail to allocate bus number of length %d "
			    "from parent\n",
			    (int)req.ra_len);
			goto exit;
		}

		if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM)
		    != NDI_SUCCESS) {
			cardbus_err(dip, 1, "cardbus_expand_busrange: "
			    "fail to ndi_ra_map_setup of bus numbers\n");
			goto exit;
		}

		(void) ndi_ra_free(dip,
		    (uint64_t)next_bus, req.ra_len,
		    NDI_RA_TYPE_PCI_BUSNUM, 0);
exit:
		kmem_free(bus_range, len);

	} else
		cardbus_err(pdip, 1, "cardbus_expand_busrange: "
		    "parent dip doesn't have busrange prop\n");
}