|
root / base / usr / src / lib / libpicl
libpicl Plain Text 1710 lines 44.9 KB
   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
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include	$(SRC)/lib/Makefile.lib

# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

# conditional assignments
all :		TARGET= all
install : TARGET= install
clean : TARGET= clean
clobber : TARGET= clobber
_msg : TARGET= _msg

# definitions for install_h target
HDRS=           picl.h
ROOTHDRDIR=     $(ROOT)/usr/include
ROOTHDRS=       $(HDRS:%=$(ROOTHDRDIR)/%)
CHECKHDRS=      $(HDRS:%.h=%.check)

.KEEP_STATE:

all install clean clobber _msg: $(SUBDIRS)

# install rule for install_h target

$(ROOTHDRDIR)/%: %
	$(INS.file)

install_h: $(ROOTHDRDIR) $(ROOTHDRS)

$(ROOTHDRDIR):
	$(INS.dir)

check: $(CHECKHDRS)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

LIBRARY=	libpicl.a
VERS=		.1
OBJECTS=	picl.o

include $(SRC)/lib/Makefile.lib

LIBS =		$(DYNLIB)
LDLIBS +=	-lc

CPPFLAGS +=	-I.. -D_REENTRANT
CFLAGS +=	$(CCVERBOSE)

XGETFLAGS +=	-a
POFILE =	picl.po

.KEEP_STATE:

all: $(LIBS)


%.po:	../%.c
	$(CP) $< $<.i
	$(BUILD.po)

_msg:	$(MSGDOMAINPOFILE)

include $(SRC)/lib/Makefile.targ
include $(SRC)/Makefile.msg.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#

include ../Makefile.com
include ../../Makefile.lib.64

install: all $(ROOTLIBS64) $(ROOTLINKS64)
#
# 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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNW_1.3 {
    global:
	picl_find_node;
	picl_get_frutree_parent;
	picl_get_node_by_path;
} SUNW_1.2;

SYMBOL_VERSION SUNW_1.2 {
    global:
	picl_get_propinfo_by_name;
	picl_walk_tree_by_class;
} SUNW_1.1;

SYMBOL_VERSION SUNW_1.1 {
    global:
	picl_get_first_prop;
	picl_get_next_by_col;
	picl_get_next_by_row;
	picl_get_next_prop;
	picl_get_prop_by_name;
	picl_get_propinfo;
	picl_get_propval;
	picl_get_propval_by_name;
	picl_get_root;
	picl_initialize;
	picl_set_propval;
	picl_set_propval_by_name;
	picl_shutdown;
	picl_strerror;
	picl_wait;
    local:
	*;
};
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This module implements the PICL Interface used by PICL clients
 * to access services of the PICL daemon
 *
 * Locking Strategy
 * A single reader/writer lock (icl_lock) protects the access to the interface
 * to the picl daemon, and the reference count, refcnt, variable.
 * A reader lock is obtained to send a request to the daemon.
 * A writer lock is obtained to initialize, reinitialize, or shutdown
 * the interface.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <fcntl.h>
#include <libintl.h>
#include <errno.h>
#include <sys/mman.h>
#include <door.h>
#include <sys/door.h>
#include <sys/time.h>
#include <assert.h>
#include <synch.h>
#include <limits.h>
#include <picl.h>
#include "picl2door.h"

/*
 * Module variables
 */
static	int		door_handle = -1;
static	uint32_t	refcnt = 0;
static	rwlock_t	picl_lock = DEFAULTRWLOCK;

static	char 		*picl_errmsg[] = {
	"No error",
	"General system failure",
	"Daemon not responding",
	"Unknown PICL service",
	"Session not initialized",
	"Invalid arguments",
	"Argument too big",
	"Property not found",
	"Not a table property handle",
	"Not a node handle",
	"Not a property handle",
	"End of property list",
	"Property already exists",
	"Property not writable",
	"Insufficient permissions",
	"Invalid handle",
	"Stale handle",
	"Unsupported version",
	"Wait timed out",
	"Attempting to destroy before delete",
	"PICL Tree is busy",
	"Already has a parent",
	"Property name is reserved",
	"Invalid reference value",
	"Continue tree walk",
	"Terminate tree walk",
	"Node not found",
	"Not enough space available",
	"Property not readable",
	"Property value unavailable"
};

#define	N_ERRORS 		(sizeof (picl_errmsg)/sizeof (picl_errmsg[0]))
#define	SEND_REQ_TRYCOUNT	1

/*
 * This function sends the client request to the daemon using a door call.
 * If door_handle is -1, it returns PICL_NOTINITIALIZED.
 * If the door_call fails, it returns PICL_NORESPONSE. Otherwise, it
 * checks the response from the daemon for error. If an error is returned
 * this function returns the error code returned and unmaps any
 * memory mapped by the door call. For successful results, the caller is
 * responsible to unmap the mapped memory after retrieving the results.
 *
 * This function does not attempt to reinitialize the interface if the
 * initial door_call fails. It is called from handshake() , shutdown()
 * and trysend_req() routines.
 */
static int
post_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
    door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize)
{
	int		err;
	picl_service_t	*ret;
	int		req_cnum;

	req_cnum = ((picl_service_t *)data_ptr)->in.cnum;
	dargp->data_ptr = data_ptr;
	dargp->data_size = data_size;
	dargp->desc_ptr = desc_ptr;
	dargp->desc_num = desc_num;
	dargp->rbuf = rbuf;
	dargp->rsize = rsize;

	if (door_call(door_handle, dargp) < 0)
		return (PICL_NORESPONSE);

	/*LINTED*/
	ret = (picl_service_t *)dargp->rbuf;
	if (ret->in.cnum == req_cnum)
		return (PICL_SUCCESS);
	else if ((ret->in.cnum == PICL_CNUM_ERROR) &&
	    (ret->ret_error.in_cnum == req_cnum))
		err = ret->ret_error.errnum;
	else
	    err = PICL_UNKNOWNSERVICE;
	if (dargp->rbuf != rbuf)
		(void) munmap(dargp->rbuf, dargp->rsize);
	return (err);
}

/*
 * This function posts an INIT message to the daemon to
 * verify communication channel.
 */
static int
handshake(void)
{
	int		err;
	door_arg_t	darg;
	picl_reqinit_t	req;
	picl_retinit_t	outargs;

	req.cnum = PICL_CNUM_INIT;
	req.clrev = PICL_VERSION_1;

	if ((err = post_req(&darg, &req, sizeof (picl_reqinit_t), NULL,
	    0, &outargs, sizeof (picl_retinit_t))) != PICL_SUCCESS)
		return (err);

	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function calls post_req() to make door_call and reinitializes
 * the interface is post_req() fails.
 */
static int
trysend_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
    door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize,
	unsigned int trycount)
{
	int	err;
	int	write_locked;

	write_locked = 0;
	(void) rw_rdlock(&picl_lock);
	if (refcnt == 0) {
		(void) rw_unlock(&picl_lock);	/* read unlock */
		return (PICL_NOTINITIALIZED);
	}

	while ((err = post_req(dargp, data_ptr, data_size, desc_ptr, desc_num,
	    rbuf, rsize)) == PICL_NORESPONSE) {
		if (trycount == 0)	/* no more retry */
			break;

		if (write_locked == 1) {	/* close and open door */
			(void) close(door_handle);
			if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
				err = PICL_NORESPONSE;
				break;
			}
			--trycount;
			continue;
		}
		/*
		 * Upgrade read to a write lock
		 */
		(void) rw_unlock(&picl_lock);
		(void) rw_wrlock(&picl_lock);

		/*
		 * if picl_shutdown happens during lock upgrade
		 */
		if (refcnt == 0) {
			err =  PICL_NOTINITIALIZED;
			break;
		}
		write_locked = 1;
		continue;
	}
	(void) rw_unlock(&picl_lock);	/* read or write unlock */
	return (err);
}

/*
 * Initialize the PICL interface
 * Increment the reference count.
 */
int
picl_initialize(void)
{
	int	err;

	(void) rw_wrlock(&picl_lock);
	if (refcnt > 0) {		/* previously initialized */
		err = handshake();
		if (err == PICL_SUCCESS) {
			++refcnt;
			(void) rw_unlock(&picl_lock);	/* write unlock */
			return (err);
		}
		if (err != PICL_NORESPONSE) {
			(void) rw_unlock(&picl_lock);	/* write unlock */
			return (err);
		}
		(void) close(door_handle);	/* close bad door */
	}

	/*
	 * Open picld door and initialize door_handle
	 */
	if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
		(void) rw_unlock(&picl_lock); /* write unlock */
		return (PICL_NORESPONSE);
	}

	err = handshake();
	if (err != PICL_SUCCESS)
		(void) close(door_handle);
	else
		++refcnt;
	(void) rw_unlock(&picl_lock);	/* write unlock */
	return (err);
}

/*
 * Shutdown the PICL interface
 * Decrement the reference count and close the door_handle if refcnt is zero
 */
int
picl_shutdown(void)
{
	int		err;
	door_arg_t	darg;
	picl_reqfini_t	req_fini;
	picl_retfini_t	outargs;

	(void) rw_wrlock(&picl_lock);	/* write lock */
	if (refcnt == 0) {
		(void) rw_unlock(&picl_lock);	/* write unlock */
		return (PICL_NOTINITIALIZED);
	}
	req_fini.cnum = PICL_CNUM_FINI;
	err = post_req(&darg, &req_fini, sizeof (picl_reqfini_t),
	    NULL, 0, &outargs, sizeof (picl_retfini_t));
	--refcnt;
	if (refcnt == 0)
		(void) close(door_handle);
	(void) rw_unlock(&picl_lock);	/* write unlock */
	if (err != PICL_SUCCESS)
		return (err);
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function waits for the specified number of seconds for a PICL
 * tree refresh.
 */
int
picl_wait(unsigned int secs)
{
	door_arg_t	darg;
	picl_reqwait_t	req_wait;
	picl_retwait_t	outargs;
	picl_service_t	*ret;
	int		err;

	req_wait.cnum = PICL_CNUM_WAIT;
	req_wait.secs = secs;
	err = trysend_req(&darg, &req_wait, sizeof (picl_reqwait_t),
	    NULL, 0, &outargs, sizeof (picl_retwait_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	err = ret->ret_wait.retcode;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}

/*
 * This function copies the handle of the root node of the PICL tree into
 * the buffer <rooth>
 */
int
picl_get_root(picl_nodehdl_t *rooth)
{
	door_arg_t	darg;
	picl_reqroot_t	req_root;
	picl_retroot_t	outargs;
	picl_service_t	*ret;
	int	err;

	req_root.cnum = PICL_CNUM_GETROOT;
	err = trysend_req(&darg, &req_root, sizeof (picl_reqroot_t), NULL,
	    0, &outargs, sizeof (picl_retroot_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);
	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*rooth = ret->ret_root.rnode;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the value of the property specified by its handle
 * into the buffer <valbuf>.
 */
int
picl_get_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
{
	door_arg_t		darg;
	picl_reqattrval_t	req_attrval;
	picl_service_t		*ret;
	picl_retattrval_t	*outargs;
	int			err;

	req_attrval.cnum = PICL_CNUM_GETATTRVAL;
	req_attrval.attr = proph;
	req_attrval.bufsize = (uint32_t)nbytes;
	if ((size_t)req_attrval.bufsize != nbytes)
		return (PICL_VALUETOOBIG);
	outargs = alloca(sizeof (picl_retattrval_t) + nbytes);

	err = trysend_req(&darg, &req_attrval, sizeof (picl_reqattrval_t),
	    NULL, 0, outargs, sizeof (picl_retattrval_t) + nbytes,
	    SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	if (ret->ret_attrval.nbytes > (uint32_t)nbytes)
		err = PICL_VALUETOOBIG;
	else
		(void) memcpy(valbuf, ret->ret_attrval.ret_buf,
		    (size_t)ret->ret_attrval.nbytes);
	if (darg.rbuf != (char *)outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}

/*
 * This function copies the value of the property specified by its
 * name into the buffer <valbuf>
 */
int
picl_get_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
    void *valbuf, size_t nbytes)
{
	door_arg_t		darg;
	picl_reqattrvalbyname_t	req_attrvalbyname;
	picl_service_t		*ret;
	picl_retattrvalbyname_t	*outargs;
	int			err;

	req_attrvalbyname.cnum = PICL_CNUM_GETATTRVALBYNAME;
	req_attrvalbyname.nodeh = nodeh;
	(void) strcpy(req_attrvalbyname.propname, propname);
	req_attrvalbyname.bufsize = (uint32_t)nbytes;
	if ((size_t)req_attrvalbyname.bufsize != nbytes)
		return (PICL_VALUETOOBIG);
	outargs = alloca(sizeof (picl_retattrvalbyname_t) + nbytes);

	err = trysend_req(&darg, &req_attrvalbyname,
	    sizeof (picl_reqattrvalbyname_t), NULL, 0, outargs,
	    sizeof (picl_retattrvalbyname_t) + nbytes, SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	if (ret->ret_attrvalbyname.nbytes > (uint32_t)nbytes)
		err = PICL_VALUETOOBIG;
	else
		(void) memcpy(valbuf, ret->ret_attrvalbyname.ret_buf,
		    (size_t)ret->ret_attrvalbyname.nbytes);
	if (darg.rbuf != (char *)outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}

/*
 * This function sets the value of the property specified by its
 * handle with the value specified in <valbuf>.
 */
int
picl_set_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
{
	door_arg_t		darg;
	picl_reqsetattrval_t	ret_setattrval;
	picl_reqsetattrval_t	*inargs;
	int			err;

	if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
		return (PICL_VALUETOOBIG);

	inargs = alloca(sizeof (picl_reqsetattrval_t) + nbytes);
	inargs->cnum = PICL_CNUM_SETATTRVAL;
	inargs->attr = proph;
	inargs->bufsize = (uint32_t)nbytes;
	if ((size_t)inargs->bufsize != nbytes)
		return (PICL_VALUETOOBIG);
	(void) memcpy(inargs->valbuf, valbuf, nbytes);

	err = trysend_req(&darg, inargs, sizeof (picl_reqsetattrval_t) +
	    nbytes, NULL, 0, &ret_setattrval,
	    sizeof (picl_retsetattrval_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	if (darg.rbuf != (char *)&ret_setattrval)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function sets the value of the property specified by its
 * name with the value given in <valbuf>
 */
int
picl_set_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
    void *valbuf, size_t nbytes)
{
	door_arg_t			darg;
	picl_retsetattrvalbyname_t	ret_setattrvalbyname;
	picl_reqsetattrvalbyname_t	*inargs;
	int				err;

	if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
		return (PICL_VALUETOOBIG);

	inargs = alloca(sizeof (picl_reqsetattrvalbyname_t) + nbytes);
	inargs->cnum = PICL_CNUM_SETATTRVALBYNAME;
	inargs->nodeh = nodeh;
	(void) strcpy(inargs->propname, propname);
	inargs->bufsize = (uint32_t)nbytes;
	if ((size_t)inargs->bufsize != nbytes)
		return (PICL_VALUETOOBIG);
	(void) memcpy(inargs->valbuf, valbuf, nbytes);

	err = trysend_req(&darg, inargs,
	    sizeof (picl_reqsetattrvalbyname_t) + nbytes, NULL, 0,
	    &ret_setattrvalbyname, sizeof (picl_retsetattrvalbyname_t),
	    SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	if (darg.rbuf != (char *)&ret_setattrvalbyname)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the information of the specified property
 * into <pinfo>
 */
int
picl_get_propinfo(picl_prophdl_t proph, picl_propinfo_t *pinfo)
{
	door_arg_t		darg;
	picl_reqattrinfo_t	req_attrinfo;
	picl_service_t		*ret;
	picl_retattrinfo_t	outargs;
	int			err;

	req_attrinfo.cnum = PICL_CNUM_GETATTRINFO;
	req_attrinfo.attr = proph;

	err = trysend_req(&darg, &req_attrinfo,
	    sizeof (picl_reqattrinfo_t), NULL, 0, &outargs,
	    sizeof (picl_retattrinfo_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	pinfo->type = ret->ret_attrinfo.type;
	pinfo->accessmode = ret->ret_attrinfo.accessmode;
	pinfo->size = (size_t)ret->ret_attrinfo.size;
	(void) strcpy(pinfo->name, ret->ret_attrinfo.name);
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the handle of the first property of a node into
 * <proph>
 */
int
picl_get_first_prop(picl_nodehdl_t nodeh, picl_prophdl_t *proph)
{
	door_arg_t		darg;
	picl_reqfirstattr_t	req_firstattr;
	picl_service_t		*ret;
	picl_retfirstattr_t	outargs;
	int			err;

	req_firstattr.cnum = PICL_CNUM_GETFIRSTATTR;
	req_firstattr.nodeh = nodeh;

	err = trysend_req(&darg, &req_firstattr,
	    sizeof (picl_reqfirstattr_t), NULL, 0, &outargs,
	    sizeof (picl_retfirstattr_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*proph = ret->ret_firstattr.attr;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the handle of the next property in list
 * into <nextprop>.
 */
int
picl_get_next_prop(picl_prophdl_t proph, picl_prophdl_t *nextprop)
{
	door_arg_t		darg;
	picl_reqnextattr_t	req_nextattr;
	picl_service_t		*ret;
	picl_retnextattr_t	outargs;
	int			err;


	req_nextattr.cnum = PICL_CNUM_GETNEXTATTR;
	req_nextattr.attr = proph;

	err = trysend_req(&darg, &req_nextattr,
	    sizeof (picl_reqnextattr_t), NULL, 0,  &outargs,
	    sizeof (picl_retnextattr_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*nextprop = ret->ret_nextattr.nextattr;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the handle of the property specified by its
 * name into <proph>.
 */
int
picl_get_prop_by_name(picl_nodehdl_t nodeh, const char *name,
    picl_prophdl_t *proph)
{
	door_arg_t		darg;
	picl_reqattrbyname_t	req_attrbyname;
	picl_service_t		*ret;
	picl_retattrbyname_t	outargs;
	int			err;

	req_attrbyname.cnum = PICL_CNUM_GETATTRBYNAME;
	req_attrbyname.nodeh = nodeh;
	(void) strcpy(req_attrbyname.propname, name);

	err = trysend_req(&darg, &req_attrbyname,
	    sizeof (picl_reqattrbyname_t), NULL, 0, &outargs,
	    sizeof (picl_retattrbyname_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*proph = ret->ret_attrbyname.attr;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the handle of the next property on the same
 * row of the table into <rowproph>.
 * When proph is the table handle, the handle of the property that is
 * in first row and first column is copied.
 */
int
picl_get_next_by_row(picl_prophdl_t proph, picl_prophdl_t *rowproph)
{
	door_arg_t		darg;
	picl_reqattrbyrow_t	req_attrbyrow;
	picl_service_t		*ret;
	picl_retattrbyrow_t	outargs;
	int			err;

	req_attrbyrow.cnum = PICL_CNUM_GETATTRBYROW;
	req_attrbyrow.attr = proph;

	err = trysend_req(&darg, &req_attrbyrow,
	    sizeof (picl_reqattrbyrow_t), NULL, 0, &outargs,
	    sizeof (picl_retattrbyrow_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*rowproph = ret->ret_attrbyrow.rowattr;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function copies the handle of the next property on the same
 * column of the table into <colproph>.
 * When proph is the table handle, the handle of the property that is
 * in the first row and first column is copied.
 */
int
picl_get_next_by_col(picl_prophdl_t proph, picl_prophdl_t *colproph)
{
	door_arg_t		darg;
	picl_reqattrbycol_t	req_attrbycol;
	picl_service_t		*ret;
	picl_retattrbycol_t	outargs;
	int			err;

	req_attrbycol.cnum = PICL_CNUM_GETATTRBYCOL;
	req_attrbycol.attr = proph;

	err = trysend_req(&darg, (char *)&req_attrbycol,
	    sizeof (picl_reqattrbycol_t), NULL, 0, (char *)&outargs,
	    sizeof (picl_retattrbycol_t), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*colproph = ret->ret_attrbycol.colattr;
	if (darg.rbuf != (char *)&outargs)
		(void) munmap(darg.rbuf, darg.rsize);
	return (PICL_SUCCESS);
}

/*
 * This function returns the picl error messages corresponding to the
 * error number.
 */
char *
picl_strerror(int err)
{
	if ((err < N_ERRORS) && (err >= 0)) {
		return (gettext(picl_errmsg[err]));
	}
	return ((char *)NULL);
}

/*
 * recursively visit all nodes
 */
static int
do_walk(picl_nodehdl_t rooth, const char *classname,
    void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
{
	int		err;
	picl_nodehdl_t	chdh;
	char		classval[PICL_CLASSNAMELEN_MAX];

	err = picl_get_propval_by_name(rooth, PICL_PROP_CHILD, &chdh,
	    sizeof (chdh));
	while (err == PICL_SUCCESS) {
		err = picl_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
		    classval, sizeof (classval));
		if (err != PICL_SUCCESS)
			return (err);

		if ((classname == NULL) || (strcmp(classname, classval) == 0)) {
			err = callback_fn(chdh, c_args);
			if (err != PICL_WALK_CONTINUE)
				return (err);
		}

		if ((err = do_walk(chdh, classname, c_args, callback_fn)) !=
		    PICL_WALK_CONTINUE)
			return (err);

		err = picl_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
		    sizeof (chdh));
	}
	if (err == PICL_PROPNOTFOUND)	/* end of a branch */
		return (PICL_WALK_CONTINUE);
	return (err);

}

/*
 * This function walks the tree by class and invokes the callback
 * function on class name matches.
 */
int
picl_walk_tree_by_class(picl_nodehdl_t rooth, const char *classname,
    void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
{
	int		err;

	if (callback_fn == NULL)
		return (PICL_INVALIDARG);
	err = do_walk(rooth, classname, c_args, callback_fn);
	if ((err == PICL_WALK_CONTINUE) || (err == PICL_WALK_TERMINATE))
		return (PICL_SUCCESS);
	return (err);
}

/*
 * This function gets propinfo and prop handle of the named property
 */
int
picl_get_propinfo_by_name(picl_nodehdl_t nodeh, const char *prop_name,
    picl_propinfo_t *pinfo, picl_prophdl_t *proph)
{
	int		err;
	picl_prophdl_t	tmpproph;
	picl_propinfo_t	tmppinfo;

	err = picl_get_prop_by_name(nodeh, prop_name, &tmpproph);
	if (err != PICL_SUCCESS)
		return (err);

	err = picl_get_propinfo(tmpproph, &tmppinfo);
	if (err != PICL_SUCCESS)
		return (err);

	*proph = tmpproph;
	*pinfo = tmppinfo;
	return (PICL_SUCCESS);
}

int
picl_get_node_by_path(const char *piclpath, picl_nodehdl_t *nodeh)
{
	door_arg_t		darg;
	picl_reqnodebypath_t	req;
	picl_retnodebypath_t	out;
	picl_service_t		*ret;
	int	err;

	req.cnum = PICL_CNUM_NODEBYPATH;
	req.psize = PATH_MAX;
	if (strlen(piclpath) >= PATH_MAX)
		return (PICL_VALUETOOBIG);
	(void) strncpy(req.pathbuf, piclpath, PATH_MAX);

	err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
	    sizeof (out), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*nodeh = ret->ret_nodebypath.nodeh;
	if (darg.rbuf != (char *)&out)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}

int
picl_find_node(picl_nodehdl_t rooth, char *pname, picl_prop_type_t ptype,
    void *pval, size_t valsize, picl_nodehdl_t *retnodeh)
{
	door_arg_t		darg;
	picl_reqfindnode_t	*req;
	picl_service_t		*ret;
	picl_retfindnode_t	out;
	int			err;

	req = alloca(sizeof (picl_reqfindnode_t) + valsize);
	req->cnum = PICL_CNUM_FINDNODE;
	req->nodeh = rooth;
	if (strlen(pname) >= PICL_PROPNAMELEN_MAX)
		return (PICL_VALUETOOBIG);
	(void) strncpy(req->propname, pname, PICL_PROPNAMELEN_MAX);
	req->ptype = ptype;
	req->valsize = (uint32_t)valsize;
	if ((size_t)req->valsize != valsize)
		return (PICL_VALUETOOBIG);
	(void) memcpy(req->valbuf, pval, valsize);

	err = trysend_req(&darg, req, sizeof (picl_reqfindnode_t) + valsize,
	    NULL, 0, &out, sizeof (out), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*retnodeh = ret->ret_findnode.rnodeh;
	if (darg.rbuf != (char *)&out)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}

int
picl_get_frutree_parent(picl_nodehdl_t devh, picl_nodehdl_t *fruh)
{
	door_arg_t		darg;
	picl_reqfruparent_t	req;
	picl_retfruparent_t	out;
	picl_service_t		*ret;
	int			err;

	req.cnum = PICL_CNUM_FRUTREEPARENT;
	req.devh = devh;

	err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
	    sizeof (out), SEND_REQ_TRYCOUNT);
	if (err != PICL_SUCCESS)
		return (err);

	/*LINTED*/
	ret = (picl_service_t *)darg.rbuf;
	*fruh = ret->ret_fruparent.fruh;
	if (darg.rbuf != (char *)&out)
		(void) munmap(darg.rbuf, darg.rsize);
	return (err);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_PICL_H
#define	_PICL_H

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * PICL Interface
 */

#include <sys/types.h>

#define	PICL_VERSION_1	0x1

/*
 * A PICL handle
 */
typedef uint64_t picl_nodehdl_t;
typedef uint64_t picl_prophdl_t;

/*
 * Maximum length of a property name
 */
#define	PICL_PROPNAMELEN_MAX	256
#define	PICL_CLASSNAMELEN_MAX	(PICL_PROPNAMELEN_MAX - sizeof ("__"))

/*
 * Maximum size of a property value
 */
#define	PICL_PROPSIZE_MAX	(512 * 1024)

/*
 * PICL property access modes
 */
#define	PICL_READ		0x1
#define	PICL_WRITE		0x2
/* Not seen by clients */
#define	PICL_VOLATILE		0x4

/*
 * PICL error numbers
 */
typedef enum {
	PICL_SUCCESS = 0x0,
	PICL_FAILURE,		/* general failure */
	PICL_NORESPONSE,	/* No response */
	PICL_UNKNOWNSERVICE,	/* unknown PICL service */
	PICL_NOTINITIALIZED,	/* interface not initialized */
	PICL_INVALIDARG,	/* invalid arguments passed */
	PICL_VALUETOOBIG,	/* value too big for buffer */
	PICL_PROPNOTFOUND,	/* property not found */
	PICL_NOTTABLE,		/* not a table */
	PICL_NOTNODE,		/* not a node */
	PICL_NOTPROP,		/* not a prop */
	PICL_ENDOFLIST,		/* end of list */
	PICL_PROPEXISTS,	/* prop already exists */
	PICL_NOTWRITABLE,	/* not writable */
	PICL_PERMDENIED,	/* permission denied */
	PICL_INVALIDHANDLE,	/* invalid handle */
	PICL_STALEHANDLE,	/* stale handle */
	PICL_NOTSUPPORTED,	/* version not supported */
	PICL_TIMEDOUT,		/* timed out */
	PICL_CANTDESTROY,	/* cannot destroy */
	PICL_TREEBUSY,		/* too busy to lock tree */
	PICL_CANTPARENT,	/* already has a parent */
	PICL_RESERVEDNAME,	/* property name is reserved */
	PICL_INVREFERENCE,	/* Invalid reference value */
	PICL_WALK_CONTINUE,	/* continue walking tree */
	PICL_WALK_TERMINATE,	/* stop walking tree */
	PICL_NODENOTFOUND,	/* node not found */
	PICL_NOSPACE,		/* not enough space available */
	PICL_NOTREADABLE,	/* property not readable */
	PICL_PROPVALUNAVAILABLE	/* property value unavailable */
} picl_errno_t;

/*
 * PICL property types
 */
typedef	enum {
	PICL_PTYPE_UNKNOWN = 0x0,
	PICL_PTYPE_VOID,		/* exists or not */
	PICL_PTYPE_INT,			/* scalar */
	PICL_PTYPE_UNSIGNED_INT,	/* scalar */
	PICL_PTYPE_FLOAT,		/* scalar */
	PICL_PTYPE_REFERENCE,		/* reference handle */
	PICL_PTYPE_TABLE,		/* table handle */
	PICL_PTYPE_TIMESTAMP,		/* time stamp */
	PICL_PTYPE_BYTEARRAY,		/* array of bytes */
	PICL_PTYPE_CHARSTRING		/* nul terminated array of chars */
} picl_prop_type_t;

typedef struct {
	picl_prop_type_t	type;
	unsigned int		accessmode;	/* always == PICL_READ */
	size_t			size;		/* item size or string size */
	char			name[PICL_PROPNAMELEN_MAX];
} picl_propinfo_t;

/*
 * -------------------------------------
 * Function prototypes of PICL Interface
 * -------------------------------------
 */

extern	int  picl_initialize(void);
extern	int  picl_shutdown(void);
extern	int  picl_get_root(picl_nodehdl_t *nodehandle);
extern	int  picl_get_propval(picl_prophdl_t proph, void *valbuf,
		size_t sz);
extern	int  picl_get_propval_by_name(picl_nodehdl_t nodeh,
		const char *propname, void *valbuf, size_t sz);
extern	int  picl_set_propval(picl_prophdl_t proph, void *valbuf,
		size_t sz);
extern	int  picl_set_propval_by_name(picl_nodehdl_t nodeh,
		const char *propname, void *valbuf, size_t sz);
extern  int  picl_get_propinfo(picl_prophdl_t proph, picl_propinfo_t *pi);
extern	int  picl_get_first_prop(picl_nodehdl_t nodeh, picl_prophdl_t *proph);
extern	int  picl_get_next_prop(picl_prophdl_t proph, picl_prophdl_t *nexth);
extern	int  picl_get_prop_by_name(picl_nodehdl_t nodeh, const char *nm,
			picl_prophdl_t *ph);
extern	int  picl_get_next_by_row(picl_prophdl_t thish, picl_prophdl_t *proph);
extern	int  picl_get_next_by_col(picl_prophdl_t thish, picl_prophdl_t *proph);
extern	int  picl_wait(unsigned int secs);
extern	char *picl_strerror(int err);
extern	int  picl_walk_tree_by_class(picl_nodehdl_t rooth,
		const char *classname, void *c_args,
		int (*callback_fn)(picl_nodehdl_t hdl, void *args));
extern	int  picl_get_propinfo_by_name(picl_nodehdl_t nodeh, const char *pname,
		picl_propinfo_t *pinfo, picl_prophdl_t *proph);

extern	int  picl_find_node(picl_nodehdl_t rooth, char *pname,
		picl_prop_type_t ptype, void *pval, size_t valsize,
		picl_nodehdl_t *retnodeh);

extern	int  picl_get_node_by_path(const char *piclpath, picl_nodehdl_t *nodeh);

extern	int  picl_get_frutree_parent(picl_nodehdl_t devh, picl_nodehdl_t *fruh);

/*
 * Standard PICL names: properties and nodes
 */
#define	PICL_NODE_ROOT		"/"
#define	PICL_NODE_PLATFORM	"platform"
#define	PICL_NODE_OBP		"obp"
#define	PICL_NODE_FRUTREE	"frutree"

#define	PICL_PROP_NAME		"name"
#define	PICL_PROP_CLASSNAME	"_class"
#define	PICL_PROP_PARENT	"_parent"
#define	PICL_PROP_CHILD		"_child"
#define	PICL_PROP_PEER		"_peer"

#define	PICL_CLASS_PICL		"picl"

#ifdef	__cplusplus
}
#endif

#endif	/* _PICL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_PICL2DOOR_H
#define	_PICL2DOOR_H

#ifdef	__cplusplus
extern "C" {
#endif

#define	PICLD_DOOR_VERSION	1
#define	PICLD_DOOR	"/var/run/picld_door"
#define	PICLD_DOOR_COOKIE	((void *)(0xdeaffeed ^ PICLD_DOOR_VERSION))

/*
 * PICL service calls
 */
typedef enum {
	PICL_CNUM_INIT = 0x1,		/* initialize */
	PICL_CNUM_FINI,			/* fini */
	PICL_CNUM_GETROOT,		/* get root node */
	PICL_CNUM_GETATTRVAL, 		/* get attr val */
	PICL_CNUM_GETATTRVALBYNAME,	/* get attr val by name */
	PICL_CNUM_GETATTRINFO,		/* get attribute information */
	PICL_CNUM_GETFIRSTATTR, 	/* get first attribute */
	PICL_CNUM_GETNEXTATTR, 		/* get next attribute */
	PICL_CNUM_GETATTRBYNAME,	/* get attr by name */
	PICL_CNUM_GETATTRBYROW,		/* get attr by row */
	PICL_CNUM_GETATTRBYCOL,		/* get attr by column */
	PICL_CNUM_SETATTRVAL, 		/* set attribute's value */
	PICL_CNUM_SETATTRVALBYNAME,	/* set attr val by name */
	PICL_CNUM_PING,			/* ping daemon */
	PICL_CNUM_WAIT,			/* wait n seconds for refresh */
	PICL_CNUM_ERROR,		/* error response */
	PICL_CNUM_FINDNODE,		/* find node */
	PICL_CNUM_NODEBYPATH,		/* get node by path */
	PICL_CNUM_FRUTREEPARENT		/* get frutree parent */
} picl_callnumber_t;

typedef	union {
		picl_nodehdl_t	nodeh;
		picl_prophdl_t	proph;
		char		str[1];
} propval_t;
#define	ret_buf		u.str
#define	ret_nodeh	u.nodeh
#define	ret_proph	u.proph

/*
 * Generic picl service request argument
 */
typedef struct {
	picl_callnumber_t	cnum;	/* service call number */
	char			buf[4];	/* buffer containing input arguments */
} picl_req_t;

typedef struct {
	picl_callnumber_t	cnum;	/* service call number */
	char			buf[4];	/* buffer containing the results */
} picl_ret_t;

	/*
	 * PICL initialize
	 */
typedef struct {
	picl_callnumber_t	cnum;	/* PICL_CNUM_INIT */
	unsigned int		clrev;	/* client's ID and revision number */
} picl_reqinit_t;

typedef struct {
	picl_callnumber_t	cnum;	/* PICL_CNUM_INIT */
	int			rev;	/* PICL daemon's revision number */
} picl_retinit_t;


	/*
	 * PICL shutdown
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FINI */
} picl_reqfini_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FINI */
} picl_retfini_t;

	/*
	 * PICL get root
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETROOT */
} picl_reqroot_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETROOT */
	picl_nodehdl_t		rnode;		/* root handle */
} picl_retroot_t;

	/*
	 * PICL get attr val
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRVAL */
	picl_prophdl_t		attr;		/* attribute handle */
	uint32_t		bufsize;	/* value buffer size */
} picl_reqattrval_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRVAL */
	picl_prophdl_t		attr;		/* attribute handle */
	uint32_t		nbytes;		/* return value size */
	propval_t 		u;
} picl_retattrval_t;

	/*
	 * PICL get attr val by name
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRVALBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attribute name */
	char			propname[PICL_PROPNAMELEN_MAX];
	uint32_t		bufsize;	/* buffer size */
} picl_reqattrvalbyname_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRVALBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attribute name */
	char			propname[PICL_PROPNAMELEN_MAX];
	uint32_t		nbytes;		/* return value size */
	propval_t		u;		/* return value */
} picl_retattrvalbyname_t;

	/*
	 * PICL get attr info
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRINFO */
	picl_prophdl_t		attr;		/* attribute handle */
} picl_reqattrinfo_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRINFO */
	picl_prophdl_t		attr;		/* attribute handle */
	picl_prop_type_t	type;		/* attribute type */
	unsigned int		accessmode;	/* access mode */
	uint32_t		size;		/* value size */
						/* attr name */
	char			name[PICL_PROPNAMELEN_MAX];
} picl_retattrinfo_t;

	/*
	 * PICL get first attr
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETFIRSTATTR */
	picl_nodehdl_t		nodeh;		/* node handle */
} picl_reqfirstattr_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETFIRSTATTR */
	picl_nodehdl_t		nodeh;		/* node handle */
	picl_prophdl_t		attr;		/* first attribute handle */
} picl_retfirstattr_t;

	/*
	 * PICL get next attr
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETNEXTATTR */
	picl_prophdl_t		attr;		/* attribute handle */
} picl_reqnextattr_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETNEXTATTR */
	picl_prophdl_t		attr;		/* attribute handle */
	picl_prophdl_t		nextattr;	/* next attribute handle */
} picl_retnextattr_t;

	/*
	 * PICL get attr by name
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attr name */
	char			propname[PICL_PROPNAMELEN_MAX];
} picl_reqattrbyname_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attr name */
	char			propname[PICL_PROPNAMELEN_MAX];
	picl_prophdl_t		attr;		/* attr handle */
} picl_retattrbyname_t;

	/*
	 * PICL get attr by row
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYROW */
	picl_prophdl_t		attr;		/* attr handle */
} picl_reqattrbyrow_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYROW */
	picl_prophdl_t		attr;		/* attr handle */
	picl_prophdl_t		rowattr;	/* attr by row handle */
} picl_retattrbyrow_t;

	/*
	 * PICL get attr by column
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYCOL */
	picl_prophdl_t		attr;		/* attr handle */
} picl_reqattrbycol_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_GETATTRBYCOL */
	picl_prophdl_t		attr;		/* attr handle */
	picl_prophdl_t		colattr;	/* attr by col handle */
} picl_retattrbycol_t;

	/*
	 * PICL set attr val
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_SETATTRVAL */
	picl_prophdl_t		attr;		/* attribute handle */
	uint32_t		bufsize;	/* value buffer size */
	char			valbuf[1];
} picl_reqsetattrval_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_SETATTRVAL */
	picl_prophdl_t		attr;		/* attribute handle */
} picl_retsetattrval_t;

	/*
	 * PICL set attr val by name
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_SETATTRVALBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attr name */
	char			propname[PICL_PROPNAMELEN_MAX];
	uint32_t		bufsize;	/* buffer size */
	char			valbuf[1];
} picl_reqsetattrvalbyname_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_SETATTRVALBYNAME */
	picl_nodehdl_t		nodeh;		/* node handle */
						/* attr name */
	char			propname[PICL_PROPNAMELEN_MAX];
} picl_retsetattrvalbyname_t;

	/*
	 * PICL ping
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_PING */
} picl_reqping_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_PING */
} picl_retping_t;

	/*
	 * PICL wait
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_WAIT */
	unsigned int		secs;		/* number of seconds */
} picl_reqwait_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_WAIT service */
	unsigned int		secs;		/* input seconds */
	int			retcode;	/* return code */
} picl_retwait_t;

	/*
	 * PICL find node
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FINDNODE */
	picl_nodehdl_t		nodeh;		/* top node handle */
						/* property name */
	char			propname[PICL_PROPNAMELEN_MAX];
	picl_prop_type_t	ptype;		/* property type */
	uint32_t		valsize;	/* size of prop value */
	char			valbuf[1];	/* prop value */
} picl_reqfindnode_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FINDNODE */
	picl_nodehdl_t		rnodeh;		/* matched node */
} picl_retfindnode_t;

	/*
	 * PICL get node by path
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_NODEBYPATH */
	uint32_t		psize;		/* size of path */
	char			pathbuf[PATH_MAX];	/* picl path */
} picl_reqnodebypath_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_NODEBYPATH */
	picl_nodehdl_t		nodeh;		/* node handle */
} picl_retnodebypath_t;

	/*
	 * PICL get frutree parent
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FRUTREEPARENT */
	picl_nodehdl_t		devh;		/* dev node handle */
} picl_reqfruparent_t;

typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_FRUTREEPARENT */
	picl_nodehdl_t		fruh;		/* fru parent handle */
} picl_retfruparent_t;

	/*
	 * PICL error return
	 */
typedef struct {
	picl_callnumber_t	cnum;		/* PICL_CNUM_ERROR */
	picl_callnumber_t	in_cnum;	/* requested service number */
	picl_errno_t		errnum;		/* return error code */
} picl_reterror_t;

typedef union {
	picl_req_t		in;			/* req arguments */
	picl_ret_t		out;			/* out results */

	picl_reqinit_t		req_init;		/* req initialize */
	picl_retinit_t		ret_init;		/* ret initialize */

	picl_reqfini_t		req_fini;		/* req fini */
	picl_retfini_t		ret_fini;		/* ret fini */

	picl_reqroot_t		req_root;		/* req root node */
	picl_retroot_t		ret_root;		/* ret root node */

	picl_reqattrval_t	req_attrval;		/* req attr value */
	picl_retattrval_t	ret_attrval;		/* ret attr value */

						/* req attr val by name */
	picl_reqattrvalbyname_t	req_attrvalbyname;
						/* ret attr val by name */
	picl_retattrvalbyname_t	ret_attrvalbyname;

	picl_reqattrinfo_t    	req_attrinfo;		/* req attr info */
	picl_retattrinfo_t    	ret_attrinfo;		/* ret attr info */

	picl_reqfirstattr_t	req_firstattr;		/* req first attr */
	picl_retfirstattr_t	ret_firstattr;		/* ret first attr */

	picl_reqnextattr_t	req_nextattr;		/* req next attr */
	picl_retnextattr_t	ret_nextattr;		/* ret next attr */

	picl_reqattrbyname_t	req_attrbyname;		/* req attr by name */
	picl_retattrbyname_t	ret_attrbyname;		/* ret attr by name */

	picl_reqattrbyrow_t	req_attrbyrow;		/* req attr by row */
	picl_retattrbyrow_t	ret_attrbyrow;		/* ret attr by row */

	picl_reqattrbycol_t	req_attrbycol;		/* req attr by col */
	picl_retattrbycol_t	ret_attrbycol;		/* ret attr by col */

						/* set attribute value */
	picl_reqsetattrval_t	req_setattrval;
						/* ret set attribute value */
	picl_retsetattrval_t	ret_setattrval;

						/* set attr val by name */
	picl_reqsetattrvalbyname_t	req_setattrvalbyname;
						/* set attr val by name */
	picl_retsetattrvalbyname_t	ret_setattrvalbyname;

	picl_reqping_t		req_ping;		/* req ping */
	picl_retping_t		ret_ping;		/* ret ping */

	picl_reqwait_t		req_wait;		/* req wait */
	picl_retwait_t		ret_wait;		/* ret wait */

	picl_reqfindnode_t	req_findnode;	/* req find node */
	picl_retfindnode_t	ret_findnode;	/* ret find node */

	picl_reqnodebypath_t	req_nodebypath;	/* get node by path */
	picl_retnodebypath_t	ret_nodebypath;	/* ret node by path */

	picl_reqfruparent_t	req_fruparent;	/* get frutree parent */
	picl_retfruparent_t	ret_fruparent;	/* ret frutree parent */

	picl_reterror_t		ret_error;		/* return error */
} picl_service_t;

#ifdef	__cplusplus
}
#endif

#endif	/* _PICL2DOOR_H */