summaryrefslogtreecommitdiff
path: root/usr/src/cmd/hotplug/hotplug.c
blob: 2b457f8cd4f062d41bd0631cd054738e1364efd8 (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <alloca.h>
#include <getopt.h>
#include <libhotplug.h>
#include <sys/types.h>
#include <sys/sunddi.h>
#include <sys/ddi_hp.h>

#if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
#endif

/*
 * Function prototypes.
 */
static int	cmd_list(int, char **, const char *);
static int	cmd_online(int, char **, const char *);
static int	cmd_offline(int, char **, const char *);
static int	cmd_enable(int, char **, const char *);
static int	cmd_disable(int, char **, const char *);
static int	cmd_poweron(int, char **, const char *);
static int	cmd_poweroff(int, char **, const char *);
static int	cmd_getpriv(int, char **, const char *);
static int	cmd_setpriv(int, char **, const char *);
static int	cmd_changestate(int, char **, const char *);
static void	parse_common(int, char **, const char *);
static void	parse_flags(int, char **, int *, const char *);
static void	parse_target(int, char **, char **, char **, const char *);
static void	parse_options(int, char **, char **, const char *);
static void	bad_option(int, int, const char *);
static void	usage(const char *);
static int	list_cb(hp_node_t, void *);
static int	list_long_cb(hp_node_t, void *);
static int	error_cb(hp_node_t, void *);
static void	print_options(const char *);
static void	print_error(int);
static int	state_atoi(char *);
static char	*state_itoa(int);
static short	valid_target(int);

/*
 * Define a conversion table for hotplug states.
 */
typedef struct {
	int	state;
	char	*state_str;
	short	valid_target;
} hpstate_t;

static hpstate_t hpstates[] = {
	{ DDI_HP_CN_STATE_EMPTY,	"EMPTY",	0 },
	{ DDI_HP_CN_STATE_PRESENT,	"PRESENT",	1 },
	{ DDI_HP_CN_STATE_POWERED,	"POWERED",	1 },
	{ DDI_HP_CN_STATE_ENABLED,	"ENABLED",	1 },
	{ DDI_HP_CN_STATE_PORT_EMPTY,	"PORT-EMPTY",	0 },
	{ DDI_HP_CN_STATE_PORT_PRESENT,	"PORT-PRESENT",	1 },
	{ DDI_HP_CN_STATE_OFFLINE,	"OFFLINE",	1 },
	{ DDI_HP_CN_STATE_ATTACHED,	"ATTACHED",	0 },
	{ DDI_HP_CN_STATE_MAINTENANCE,	"MAINTENANCE",	0 },
	{ DDI_HP_CN_STATE_ONLINE,	"ONLINE",	1 },
	{ 0, 0, 0 }
};

/*
 * Define tables of supported subcommands.
 */
typedef struct {
	char		*usage_str;
	char		*cmd_str;
	int		(*func)(int argc, char *argv[], const char *usage_str);
} subcmd_t;

static subcmd_t	subcmds[] = {
	{ "list       [-l] [-v] [<path> [<connection>]]", "list", cmd_list },
	{ "online     <path> <port>", "online", cmd_online },
	{ "offline    [-f] [-q] <path> <port>", "offline", cmd_offline },
	{ "enable     <path> <connector>", "enable", cmd_enable },
	{ "disable    [-f] [-q] <path> <connector>", "disable", cmd_disable },
	{ "poweron    <path> <connector>", "poweron", cmd_poweron },
	{ "poweroff   [-f] [-q] <path> <connector>", "poweroff", cmd_poweroff },
	{ "get        -o <options> <path> <connector>", "get", cmd_getpriv },
	{ "set        -o <options> <path> <connector>", "set", cmd_setpriv }
};

static subcmd_t hidden_subcmds[] = {
	{ "changestate  [-f] [-q] -s <state> <path> <connection>",
	    "changestate", cmd_changestate }
};

/*
 * Define tables of command line options.
 */
static const struct option common_opts[] = {
	{ "help",	no_argument,		0, '?' },
	{ "version",	no_argument,		0, 'V' },
	{ 0, 0, 0, 0 }
};

static const struct option list_opts[] = {
	{ "list-path",	no_argument,		0, 'l' },
	{ "verbose",	no_argument,		0, 'v' },
	{ 0, 0,	0, 0 }
};

static const struct option flag_opts[] = {
	{ "force",	no_argument,		0, 'f' },
	{ "query",	no_argument,		0, 'q' },
	{ 0, 0,	0, 0 }
};

static const struct option private_opts[] = {
	{ "options",	required_argument,	0, 'o' },
	{ 0, 0,	0, 0 }
};

static const struct option changestate_opts[] = {
	{ "force",	no_argument,		0, 'f' },
	{ "query",	no_argument,		0, 'q' },
	{ "state",	required_argument,	0, 's' },
	{ 0, 0,	0, 0 }
};

/*
 * Define exit codes.
 */
#define	EXIT_OK		0
#define	EXIT_EINVAL	1	/* invalid arguments */
#define	EXIT_ENOENT	2	/* path or connection doesn't exist */
#define	EXIT_FAILED	3	/* operation failed */
#define	EXIT_UNAVAIL	4	/* service not available */

/*
 * Global variables.
 */
static char 	*prog;
static char	version[] = "1.0";
extern int	errno;

/*
 * main()
 *
 *	The main routine determines which subcommand is used,
 *	and dispatches control to the corresponding function.
 */
int
main(int argc, char *argv[])
{
	int 		i, rv;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	if ((prog = strrchr(argv[0], '/')) == NULL)
		prog = argv[0];
	else
		prog++;

	if (argc < 2) {
		usage(NULL);
		return (EXIT_EINVAL);
	}

	parse_common(argc, argv, NULL);

	/* Check the list of defined subcommands. */
	for (i = 0; i < (sizeof (subcmds) / sizeof (subcmd_t)); i++) {
		if (strcmp(argv[1], subcmds[i].cmd_str) == 0) {
			rv = subcmds[i].func(argc - 1, &argv[1],
			    subcmds[i].usage_str);
			goto finished;
		}
	}

	/* Check the list of hidden subcommands. */
	for (i = 0; i < (sizeof (hidden_subcmds) / sizeof (subcmd_t)); i++) {
		if (strcmp(argv[1], hidden_subcmds[i].cmd_str) == 0) {
			rv = hidden_subcmds[i].func(argc - 1, &argv[1],
			    hidden_subcmds[i].usage_str);
			goto finished;
		}
	}

	/* No matching subcommand found. */
	(void) fprintf(stderr, gettext("ERROR: %s: unknown subcommand '%s'\n"),
	    prog, argv[1]);
	usage(NULL);
	exit(EXIT_EINVAL);

finished:
	/* Determine exit code */
	switch (rv) {
	case 0:
		break;
	case EINVAL:
		return (EXIT_EINVAL);
	case ENXIO:
	case ENOENT:
		return (EXIT_ENOENT);
	case EBADF:
		return (EXIT_UNAVAIL);
	default:
		return (EXIT_FAILED);
	}

	return (EXIT_OK);
}

/*
 * cmd_list()
 *
 *	Subcommand to list hotplug information.
 */
static int
cmd_list(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	char		*path = NULL;
	char		*connection = NULL;
	boolean_t	long_flag = B_FALSE;
	int		flags = 0;
	int		opt;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	while ((opt = getopt_clip(argc, argv, "lv", list_opts, NULL)) != -1) {
		switch (opt) {
		case 'l':
			long_flag = B_TRUE;
			break;
		case 'v':
			flags |= HPINFOUSAGE;
			break;
		default:
			bad_option(opt, optopt, usage_str);
			break;
		}
	}
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Default path is "/" */
	if (path == NULL)
		path = "/";

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, flags)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Display hotplug information */
	(void) hp_traverse(root, NULL, long_flag ? list_long_cb : list_cb);

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (0);
}

/*
 * cmd_online()
 *
 *	Subcommand to online a hotplug port.
 */
static int
cmd_online(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a port */
	if (hp_type(root) != HP_NODE_PORT) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a port).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do state change */
	rv = hp_set_state(root, 0, DDI_HP_CN_STATE_ONLINE, &results);

	/* Display results */
	if (rv == EIO) {
		(void) fprintf(stderr, gettext("ERROR: failed to attach device "
		    "drivers or other internal errors.\n"));
	} else if (rv != 0) {
		print_error(rv);
	}
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_offline()
 *
 *	Subcommand to offline a hotplug port.
 */
static int
cmd_offline(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		flags = 0;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_flags(argc, argv, &flags, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a port */
	if (hp_type(root) != HP_NODE_PORT) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a port).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do state change */
	rv = hp_set_state(root, flags, DDI_HP_CN_STATE_OFFLINE, &results);

	/* Display results */
	print_error(rv);
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_enable()
 *
 *	Subcommand to enable a hotplug connector.
 */
static int
cmd_enable(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do state change */
	rv = hp_set_state(root, 0, DDI_HP_CN_STATE_ENABLED, &results);

	/* Display results */
	print_error(rv);
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_disable()
 *
 *	Subcommand to disable a hotplug connector.
 */
static int
cmd_disable(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		flags = 0;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_flags(argc, argv, &flags, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/*
	 * Do nothing unless the connector is in the ENABLED state.
	 * Otherwise this subcommand becomes an alias for 'poweron.'
	 */
	if (hp_state(root) != DDI_HP_CN_STATE_ENABLED) {
		hp_fini(root);
		return (0);
	}

	/* Do state change */
	rv = hp_set_state(root, flags, DDI_HP_CN_STATE_POWERED, &results);

	/* Display results */
	print_error(rv);
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_poweron()
 *
 *	Subcommand to power on a hotplug connector.
 */
static int
cmd_poweron(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/*
	 * Do nothing if the connector is already powered.
	 * Otherwise this subcommand becomes an alias for 'disable.'
	 */
	if (hp_state(root) >= DDI_HP_CN_STATE_POWERED) {
		hp_fini(root);
		return (0);
	}

	/* Do state change */
	rv = hp_set_state(root, 0, DDI_HP_CN_STATE_POWERED, &results);

	/* Display results */
	print_error(rv);
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_poweroff()
 *
 *	Subcommand to power off a hotplug connector.
 */
static int
cmd_poweroff(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		flags = 0;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_flags(argc, argv, &flags, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Path and connection are required */
	if ((path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do state change */
	rv = hp_set_state(root, flags, DDI_HP_CN_STATE_PRESENT, &results);

	/* Display results */
	print_error(rv);
	if (results != NULL) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_getpriv()
 *
 *	Subcommand to get and display bus private options.
 */
static int
cmd_getpriv(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	char		*path = NULL;
	char		*connection = NULL;
	char		*options = NULL;
	char		*results = NULL;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_options(argc, argv, &options, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Options, path, and connection are all required */
	if ((options == NULL) || (path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do the operation */
	rv = hp_get_private(root, options, &results);

	/* Display results */
	if (rv == ENOTSUP) {
		(void) fprintf(stderr,
		    gettext("ERROR: unsupported property name or value.\n"));
		(void) fprintf(stderr,
		    gettext("(Properties may depend upon connector state.)\n"));
	} else if (rv != 0) {
		print_error(rv);
	}
	if (results != NULL) {
		print_options(results);
		free(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_setpriv()
 *
 *	Subcommand to set bus private options.
 */
static int
cmd_setpriv(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	char		*path = NULL;
	char		*connection = NULL;
	char		*options = NULL;
	char		*results = NULL;
	int		rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	parse_options(argc, argv, &options, usage_str);
	parse_target(argc, argv, &path, &connection, usage_str);

	/* Options, path, and connection are all required */
	if ((options == NULL) || (path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Verify target is a connector */
	if (hp_type(root) != HP_NODE_CONNECTOR) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target (must be a connector).\n"));
		hp_fini(root);
		return (EINVAL);
	}

	/* Do the operation */
	rv = hp_set_private(root, options, &results);

	/* Display results */
	if (rv == ENOTSUP) {
		(void) fprintf(stderr,
		    gettext("ERROR: unsupported property name or value.\n"));
		(void) fprintf(stderr,
		    gettext("(Properties may depend upon connector state.)\n"));
	} else if (rv != 0) {
		print_error(rv);
	}
	if (results != NULL) {
		print_options(results);
		free(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * cmd_changestate()
 *
 *	Subcommand to initiate a state change operation.  This is
 *	a hidden subcommand to directly set a connector or port to
 *	a specific target state.
 */
static int
cmd_changestate(int argc, char *argv[], const char *usage_str)
{
	hp_node_t	root;
	hp_node_t	results = NULL;
	char		*path = NULL;
	char		*connection = NULL;
	int		state = -1;
	int		flags = 0;
	int		opt, rv;

	/* Parse command line options */
	parse_common(argc, argv, usage_str);
	while ((opt = getopt_clip(argc, argv, "fqs:", changestate_opts,
	    NULL)) != -1) {
		switch (opt) {
		case 'f':
			flags |= HPFORCE;
			break;
		case 'q':
			flags |= HPQUERY;
			break;
		case 's':
			if ((state = state_atoi(optarg)) == -1) {
				(void) printf("ERROR: invalid target state\n");
				return (EINVAL);
			}
			break;
		default:
			bad_option(opt, optopt, usage_str);
			break;
		}
	}
	parse_target(argc, argv, &path, &connection, usage_str);

	/* State, path, and connection are all required */
	if ((state == -1) || (path == NULL) || (connection == NULL)) {
		(void) fprintf(stderr, gettext("ERROR: too few arguments.\n"));
		usage(usage_str);
		return (EINVAL);
	}

	/* Check that target state is valid */
	if (valid_target(state) == 0) {
		(void) fprintf(stderr,
		    gettext("ERROR: invalid target state\n"));
		return (EINVAL);
	}

	/* Get hotplug information snapshot */
	if ((root = hp_init(path, connection, 0)) == NULL) {
		print_error(errno);
		return (errno);
	}

	/* Initiate state change operation on root of snapshot */
	rv = hp_set_state(root, flags, state, &results);

	/* Display results */
	print_error(rv);
	if (results) {
		(void) hp_traverse(results, NULL, error_cb);
		hp_fini(results);
	}

	/* Discard hotplug information snapshot */
	hp_fini(root);

	return (rv);
}

/*
 * parse_common()
 *
 *	Parse command line options that are common to the
 *	entire program, and to each of its subcommands.
 */
static void
parse_common(int argc, char *argv[], const char *usage_str)
{
	int		opt;
	extern int	opterr;
	extern int	optind;

	/* Turn off error reporting */
	opterr = 0;

	while ((opt = getopt_clip(argc, argv, "?V", common_opts, NULL)) != -1) {
		switch (opt) {
		case '?':
			if (optopt == '?') {
				usage(usage_str);
				exit(0);
			}
			break;
		case 'V':
			(void) printf(gettext("%s: Version %s\n"),
			    prog, version);
			exit(0);
		default:
			break;
		}
	}

	/* Reset option index */
	optind = 1;
}

/*
 * parse_flags()
 *
 *	Parse command line flags common to all downward state
 *	change operations (offline, disable, poweoff).
 */
static void
parse_flags(int argc, char *argv[], int *flagsp, const char *usage_str)
{
	int	opt;
	int	flags = 0;

	while ((opt = getopt_clip(argc, argv, "fq", flag_opts, NULL)) != -1) {
		switch (opt) {
		case 'f':
			flags |= HPFORCE;
			break;
		case 'q':
			flags |= HPQUERY;
			break;
		default:
			bad_option(opt, optopt, usage_str);
			break;
		}
	}

	*flagsp = flags;
}

/*
 * parse_options()
 *
 *	Parse command line options common to the bus private set and
 *	get subcommands.
 */
static void
parse_options(int argc, char *argv[], char **optionsp, const char *usage_str)
{
	int	opt;

	while ((opt = getopt_clip(argc, argv, "o:", private_opts,
	    NULL)) != -1) {
		switch (opt) {
		case 'o':
			*optionsp = optarg;
			break;
		default:
			bad_option(opt, optopt, usage_str);
			break;
		}
	}
}

/*
 * parse_target()
 *
 *	Parse the target path and connection name from the command line.
 */
static void
parse_target(int argc, char *argv[], char **pathp, char **connectionp,
    const char *usage_str)
{
	extern int	optind;

	if (optind < argc)
		*pathp = argv[optind++];

	if (optind < argc)
		*connectionp = argv[optind++];

	if (optind < argc) {
		(void) fprintf(stderr, gettext("ERROR: too many arguments.\n"));
		usage(usage_str);
		exit(EINVAL);
	}
}

/*
 * bad_option()
 *
 *	Routine to handle bad command line options.
 */
static void
bad_option(int opt, int optopt, const char *usage_str)
{
	switch (opt) {
	case ':':
		(void) fprintf(stderr,
		    gettext("ERROR: option '%c' requires an argument.\n"),
		    optopt);
		break;
	default:
		if (optopt == '?') {
			usage(usage_str);
			exit(EXIT_OK);
		}
		(void) fprintf(stderr,
		    gettext("ERROR: unrecognized option '%c'.\n"), optopt);
		break;
	}

	usage(usage_str);

	exit(EXIT_EINVAL);
}

/*
 * usage()
 *
 *	Display general usage of the command.  Including
 *	the usage synopsis of each defined subcommand.
 */
static void
usage(const char *usage_str)
{
	int	i;

	if (usage_str != NULL) {
		(void) fprintf(stderr, gettext("Usage:   %s  %s\n\n"),
		    prog, usage_str);
		return;
	}

	(void) fprintf(stderr, gettext("Usage:  %s  <subcommand> [<args>]\n\n"),
	    prog);

	(void) fprintf(stderr, gettext("Subcommands:\n\n"));

	for (i = 0; i < (sizeof (subcmds) / sizeof (subcmd_t)); i++)
		(void) fprintf(stderr, "   %s\n\n", subcmds[i].usage_str);
}

/*
 * list_cb()
 *
 *	Callback function for hp_traverse(), to display nodes
 *	of a hotplug information snapshot.  (Short version.)
 */
/*ARGSUSED*/
static int
list_cb(hp_node_t node, void *arg)
{
	hp_node_t	parent;

	/* Indent */
	for (parent = hp_parent(node); parent; parent = hp_parent(parent))
		if (hp_type(parent) == HP_NODE_DEVICE)
			(void) printf("     ");

	switch (hp_type(node)) {
	case HP_NODE_DEVICE:
		(void) printf("%s\n", hp_name(node));
		break;

	case HP_NODE_CONNECTOR:
		(void) printf("[%s]", hp_name(node));
		(void) printf("  (%s)", state_itoa(hp_state(node)));
		(void) printf("\n");
		break;

	case HP_NODE_PORT:
		(void) printf("<%s>", hp_name(node));
		(void) printf("  (%s)", state_itoa(hp_state(node)));
		(void) printf("\n");
		break;

	case HP_NODE_USAGE:
		(void) printf("{ %s }\n", hp_usage(node));
		break;
	}

	return (HP_WALK_CONTINUE);
}

/*
 * list_long_cb()
 *
 *	Callback function for hp_traverse(), to display nodes
 *	of a hotplug information snapshot.  (Long version.)
 */
/*ARGSUSED*/
static int
list_long_cb(hp_node_t node, void *arg)
{
	char	path[MAXPATHLEN];
	char	connection[MAXPATHLEN];

	if (hp_type(node) != HP_NODE_USAGE) {
		if (hp_path(node, path, connection) != 0)
			return (HP_WALK_CONTINUE);
		(void) printf("%s", path);
	}

	switch (hp_type(node)) {
	case HP_NODE_CONNECTOR:
		(void) printf(" [%s]", connection);
		(void) printf(" (%s)", state_itoa(hp_state(node)));
		break;

	case HP_NODE_PORT:
		(void) printf(" <%s>", connection);
		(void) printf(" (%s)", state_itoa(hp_state(node)));
		break;

	case HP_NODE_USAGE:
		(void) printf("    { %s }", hp_usage(node));
		break;
	}

	(void) printf("\n");

	return (HP_WALK_CONTINUE);
}

/*
 * error_cb()
 *
 *	Callback function for hp_traverse(), to display
 *	error results from a state change operation.
 */
/*ARGSUSED*/
static int
error_cb(hp_node_t node, void *arg)
{
	hp_node_t	child;
	char		*usage_str;
	static char	path[MAXPATHLEN];
	static char	connection[MAXPATHLEN];

	if (((child = hp_child(node)) != NULL) &&
	    (hp_type(child) == HP_NODE_USAGE)) {
		if (hp_path(node, path, connection) == 0)
			(void) printf("%s:\n", path);
		return (HP_WALK_CONTINUE);
	}

	if ((hp_type(node) == HP_NODE_USAGE) &&
	    ((usage_str = hp_usage(node)) != NULL))
		(void) printf("   { %s }\n", usage_str);

	return (HP_WALK_CONTINUE);
}

/*
 * print_options()
 *
 *	Parse and display bus private options.  The options are
 *	formatted as a string which conforms to the getsubopt(3C)
 *	format.  This routine only splits the string elements as
 *	separated by commas, and displays each portion on its own
 *	separate line of output.
 */
static void
print_options(const char *options)
{
	char	*buf, *curr, *next;
	size_t	len;

	/* Do nothing if options string is empty */
	if ((len = strlen(options)) == 0)
		return;

	/* To avoid modifying the input string, make a copy on the stack */
	if ((buf = (char *)alloca(len + 1)) == NULL) {
		(void) printf("%s\n", options);
		return;
	}
	(void) strlcpy(buf, options, len + 1);

	/* Iterate through each comma-separated name/value pair */
	curr = buf;
	do {
		if ((next = strchr(curr, ',')) != NULL) {
			*next = '\0';
			next++;
		}
		(void) printf("%s\n", curr);
	} while ((curr = next) != NULL);
}

/*
 * print_error()
 *
 *	Common routine to print error numbers in an appropriate way.
 *	Prints nothing if error code is 0.
 */
static void
print_error(int error)
{
	switch (error) {
	case 0:
		/* No error */
		return;
	case EACCES:
		(void) fprintf(stderr,
		    gettext("ERROR: operation not authorized.\n"));
		break;
	case EBADF:
		(void) fprintf(stderr,
		    gettext("ERROR: hotplug service is not available.\n"));
		break;
	case EBUSY:
		(void) fprintf(stderr,
		    gettext("ERROR: devices or resources are busy.\n"));
		break;
	case EEXIST:
		(void) fprintf(stderr,
		    gettext("ERROR: resource already exists.\n"));
		break;
	case EFAULT:
		(void) fprintf(stderr,
		    gettext("ERROR: internal failure in hotplug service.\n"));
		break;
	case EINVAL:
		(void) fprintf(stderr,
		    gettext("ERROR: invalid arguments.\n"));
		break;
	case ENOENT:
		(void) fprintf(stderr,
		    gettext("ERROR: there are no connections to display.\n"));
		(void) fprintf(stderr,
		    gettext("(See hotplug(8) for more information.)\n"));
		break;
	case ENXIO:
		(void) fprintf(stderr,
		    gettext("ERROR: no such path or connection.\n"));
		break;
	case ENOMEM:
		(void) fprintf(stderr,
		    gettext("ERROR: not enough memory.\n"));
		break;
	case ENOTSUP:
		(void) fprintf(stderr,
		    gettext("ERROR: operation not supported.\n"));
		break;
	case EIO:
		(void) fprintf(stderr,
		    gettext("ERROR: hardware or driver specific failure.\n"));
		break;
	default:
		(void) fprintf(stderr, gettext("ERROR: operation failed: %s\n"),
		    strerror(error));
		break;
	}
}

/*
 * state_atoi()
 *
 *	Convert a hotplug state from a string to an integer.
 */
static int
state_atoi(char *state)
{
	int	i;

	for (i = 0; hpstates[i].state_str != NULL; i++)
		if (strcasecmp(state, hpstates[i].state_str) == 0)
			return (hpstates[i].state);

	return (-1);
}

/*
 * state_itoa()
 *
 *	Convert a hotplug state from an integer to a string.
 */
static char *
state_itoa(int state)
{
	static char	unknown[] = "UNKNOWN";
	int		i;

	for (i = 0; hpstates[i].state_str != NULL; i++)
		if (state == hpstates[i].state)
			return (hpstates[i].state_str);

	return (unknown);
}

/*
 * valid_target()
 *
 *	Check if a state is a valid target for a changestate command.
 */
static short
valid_target(int state)
{
	int	i;

	for (i = 0; hpstates[i].state_str != NULL; i++)
		if (state == hpstates[i].state)
			return (hpstates[i].valid_target);

	return (0);
}