[Zrouter-src-freebsd] ZRouter.org: push to FreeBSD HEAD tree

zrouter-src-freebsd at zrouter.org zrouter-src-freebsd at zrouter.org
Mon Mar 5 15:38:27 UTC 2012


details:   http://zrouter.org/hg/FreeBSD/head//rev/966636b21c19
changeset: 419:966636b21c19
user:      ray at terran.dlink.ua
date:      Mon Mar 05 17:37:06 2012 +0200
description:
Rework using getopt.

diffstat:

 head/sbin/devctl/devctl.8 |   91 ++++++++++++++++++++------------
 head/sbin/devctl/devctl.c |  126 ++++++++++++++++++++++++++++-----------------
 2 files changed, 134 insertions(+), 83 deletions(-)

diffs (321 lines):

diff -r c8205343f688 -r 966636b21c19 head/sbin/devctl/devctl.8
--- a/head/sbin/devctl/devctl.8	Mon Mar 05 16:03:19 2012 +0200
+++ b/head/sbin/devctl/devctl.8	Mon Mar 05 17:37:06 2012 +0200
@@ -28,7 +28,7 @@
 .\"     @(#)devctl.8	8.1 (Berkeley) 6/5/93
 .\" $FreeBSD$
 .\"
-.Dd January 31, 2012
+.Dd March 5, 2012
 .Dt DEVCTL 8
 .Os
 .Sh NAME
@@ -36,61 +36,75 @@
 .Nd "attach/detach devices that can not be auto enumerated"
 .Sh SYNOPSIS
 .Nm devctl
-.Ar attach | hinted
-.Ar parentdevclass
-.Ar parentunit
-.Ar devclass
-.Op Ar unit
-.Nm devctl
-.Ar detach
-.Ar pdevclass
-.Ar punit
-.Ar devclass
-.Ar unit
+.Fl c Ar command
+.Fl b Ar pdevclass
+.Fl B Ar punit
+.Fl d Ar devclass
+.Op Fl D Ar unit
 .Sh DESCRIPTION
 The
 .Nm devctl
-utilities are used to attach or detach devices that can not be auto enumerated.
-.Bl -tag -width indent
+utilities are used to attach devices that can not be auto enumerated. Also
+it have abbility to run simple newbus methods like
+.Xr DEVICE_DETACH 9 ,
+.Xr DEVICE_SUSPEND 9 ,
+.Xr DEVICE_RESUME 9 ,
+.Xr DEVICE_SHUTDOWN 9
+That may help in tests for correctness of device behaviur for suspend/resume.
+.Pp
+The following options are accepted.
+.Bl -tag -width ".Fl b Ar pdevclass"
+.It Fl c Ar command
+Command type which will be done on device.
+.It Fl b Ar pdevclass
+Parent device name (bus devclass).
+.It Fl B Ar punit
+Parent device unit (bus unit).
+.It Fl d Ar devclass
+Target device name (device devclass).
+.It Fl D Ar unit
+Target device unit (device unit).
+.El
+.Pp
+The following command types are accepted for key
+.Fl c
+:
+.Bl -tag -width ".Fl c Ar command"
 .It Ar attach
 to call
 .Xr device_add_child 9
 and then 
 .Xr device_probe_and_attach 9
+.It Ar detach
+to remove device by call to
+.Xr device_delete_child 9
+.
 .It Ar hinted
-to attach device with BUS_HINTED_CHILD macro which call bus_hinted_child device method
-.It Ar detach
-to remove device from parent device child list by call to
-.Xr device_delete_child 9
-.It Ar pdevclass
-name of device which is parent to new device or device which will be removed
-.It Ar punit
-unit of parent device
-.It Ar devclass
-devclass of new device or device which will be removed
-.It Ar unit
-unit of new device, optional for
-.Ar attach
-and
-.Ar hinted
-, required for
-.Ar detach
-.
+to attach device with BUS_HINTED_CHILD macro which call bus_hinted_child device
+method.
+.It Ar resume
+resume device with DEVICE_RESUME newbus macro.
+.It Ar shutdown
+shutdown device with
+.Xr DEVICE_SHUTDOWN 9
+newbus macro.
+.It Ar suspend
+suspend device with DEVICE_SUSPEND newbus macro.
 .El
 .Pp
 To enable this facility in kernel put options DEVCTL_ATTACH_ENABLED into kernel config file.
 Kernel will create device file with name
 .Xr devctl2 4
 .Sh EXAMPLES
-.Dl devctl hinted spibus 0 mx25l 0
+.Dl devctl -c hinted -b spibus -B 0 -d mx25l -D 0
 .Pp
 will try to attach device with class mx25l and unit 0 to bus spibus0.
 .Pp
-.Dl devctl hinted spibus 0 mx25l
+.Dl devctl -c hinted -b spibus -B 0 -d mx25l
 .Pp
 same, but unit number will be assigned automatically.
 .Pp
-.Dl devctl detach spibus 0 mx25l 0
+.Dl devctl -c detach -d mx25l -D 0
 .Pp
 will delete device mx25l0 from child list of bus spibus0.
 .Sh DIAGNOSTICS
@@ -120,6 +134,13 @@
 .El
 .Sh SEE ALSO
 .Xr devctl2 4 ,
+.Xr device_add_child 9 ,
+.Xr device_probe_and_attach 9 ,
+.Xr device_delete_child 9 ,
+.Xr DEVICE_DETACH 9 ,
+.Xr DEVICE_RESUME 9 ,
+.Xr DEVICE_SHUTDOWN 9 ,
+.Xr DEVICE_SUSPEND 9
 .Sh HISTORY
 The
 .Nm devctl
diff -r c8205343f688 -r 966636b21c19 head/sbin/devctl/devctl.c
--- a/head/sbin/devctl/devctl.c	Mon Mar 05 16:03:19 2012 +0200
+++ b/head/sbin/devctl/devctl.c	Mon Mar 05 17:37:06 2012 +0200
@@ -42,27 +42,31 @@
 #include <limits.h>
 
 #define	DEVCTL	"/dev/devctl2"
-#define	MODE_ATTACH	1
-#define	MODE_HINTED	2
-#define	MODE_DETACH	3
 
 static void	usage(void);
 
 struct cmd {
 	const char 	*name;
 	unsigned long 	ioctl;
-	int 		bargpos;
-	int 		dargpos;
-	int 		argc_min;
-	int		optnl;	/* Optional args count */
+	int 		flags;
+#define	FLAGS_BUS_NAME_RQRD	(1<<0)
+#define	FLAGS_BUS_UNIT_RQRD	(1<<1)
+#define	FLAGS_DEV_NAME_RQRD	(1<<2)
+#define	FLAGS_DEV_UNIT_RQRD	(1<<3)
 } cmd[] = {
-	{ "attach",	DEVCTLADEV,	2,	4,	5,	1},
-	{ "hinted",	DEVCTLAHDEV,	2,	4,	5,	1},
-	{ "detach",	DEVCTLDDEV,	-1,	2,	4,	0},
-	{ "suspend",	DEVCTLSUSDEV,	-1,	2,	4,	0},
-	{ "resume",	DEVCTLRSMDEV,	-1,	2,	4,	0},
-	{ "shutdown",	DEVCTLSHTDEV,	-1,	2,	4,	0},
-	{ NULL, 0, 0, 0, 0, 0},
+	{ "attach",	DEVCTLADEV,
+	    FLAGS_BUS_NAME_RQRD | FLAGS_BUS_UNIT_RQRD | FLAGS_DEV_NAME_RQRD},
+	{ "hinted",	DEVCTLAHDEV,
+	    FLAGS_BUS_NAME_RQRD | FLAGS_BUS_UNIT_RQRD | FLAGS_DEV_NAME_RQRD},
+	{ "detach",	DEVCTLDDEV,
+	    FLAGS_DEV_NAME_RQRD | FLAGS_DEV_UNIT_RQRD},
+	{ "suspend",	DEVCTLSUSDEV,
+	    FLAGS_DEV_NAME_RQRD | FLAGS_DEV_UNIT_RQRD},
+	{ "resume",	DEVCTLRSMDEV,
+	    FLAGS_DEV_NAME_RQRD | FLAGS_DEV_UNIT_RQRD},
+	{ "shutdown",	DEVCTLSHTDEV,
+	    FLAGS_DEV_NAME_RQRD | FLAGS_DEV_UNIT_RQRD},
+	{ NULL, 0, 0},
 };
 
 
@@ -70,9 +74,14 @@
 usage(void)
 {
 	printf("Usage:\n");
-	printf("\tdevctl attach bus_name bus_unit dev_name [dev_unit]\n");
-	printf("\tdevctl hinted bus_name bus_unit dev_name [dev_unit]\n");
-	printf("\tdevctl detach dev_name dev_unit\n");
+	printf("\tdevctl -c attach -b bus_name -B bus_unit -d dev_name "
+	    "[-D dev_unit]\n");
+	printf("\tdevctl -c hinted -b bus_name -B bus_unit -d dev_name "
+	    "[-D dev_unit]\n");
+	printf("\tdevctl -c detach -d dev_name -D dev_unit\n");
+	printf("\tdevctl -c resume -d dev_name -D dev_unit\n");
+	printf("\tdevctl -c shutdown -d dev_name -D dev_unit\n");
+	printf("\tdevctl -c suspend -d dev_name -D dev_unit\n");
 }
 
 int
@@ -80,52 +89,74 @@
 {
 	struct devctl_attach_args args;
 	int fd, mode, error;
+	char *command, ch;
 
 	args.bus_name = NULL;
-	args.bus_unit = 0;
+	args.bus_unit = -1;
+	args.dev_name = NULL;
+	args.dev_unit = -1;
 	error = 0;
-	if (argc < 3) {
-		usage();
-		return (1);
-	}
+	command = NULL;
 
-	for (mode = 0; cmd[mode].name; mode ++) {
-		if (strcmp(argv[1], cmd[mode].name) == 0)
+	while ((ch = getopt(argc, argv, "c:b:B:d:D:h")) != -1)
+		switch(ch) {
+		case 'c':
+			command = optarg;
 			break;
-	}
-	if (cmd[mode].name == NULL) {
-		printf("Unsupported command \"%s\".\n", argv[1]);
+		case 'b':
+			args.bus_name = optarg;
+			break;
+		case 'B':
+			args.bus_unit = strtoul(optarg, 0, 0);
+			break;
+		case 'd':
+			args.dev_name = optarg;
+			break;
+		case 'D':
+			args.dev_unit = strtoul(optarg, 0, 0);
+			break;
+		case '?':
+		case 'h':
+		default:
+			usage();
+		}
+	argc -= optind;
+	if ((argc != 0) || (command == NULL)) {
 		usage();
 		/* support embedd into sh(1) */
 		return (1);
 	}
 
-	if (argc < cmd[mode].argc_min) {
-		printf("Arguments list to short.\n");
+	for (mode = 0; cmd[mode].name; mode ++) {
+		if (strcmp(command, cmd[mode].name) == 0)
+			break;
+	}
+
+	if (cmd[mode].name == NULL) {
+		printf("Unsupported command \"%s\".\n", command);
 		usage();
 		return (1);
 	}
 
-	if (argc > (cmd[mode].argc_min + cmd[mode].optnl)) {
-		printf("Too much arguments, check command-line please.\n");
+	if (cmd[mode].flags & FLAGS_BUS_NAME_RQRD && (args.bus_name == NULL)) {
+		printf("Command %s require bus name.\n", command);
 		usage();
 		return (1);
 	}
-
-	/* Get bus class name and bus unit */
-	if (cmd[mode].bargpos != -1) {
-		args.bus_name = argv[cmd[mode].bargpos];
-		args.bus_unit = strtoul(argv[cmd[mode].bargpos + 1], 0, 0);
+	if (cmd[mode].flags & FLAGS_BUS_UNIT_RQRD && (args.bus_unit == -1)) {
+		printf("Command %s require bus unit.\n", command);
+		usage();
+		return (1);
 	}
-
-	/* Get device class name */
-	args.dev_name = argv[cmd[mode].dargpos];
-
-	/* Get device unit */
-	if (argc > cmd[mode].argc_min) {
-		args.dev_unit = strtoul(argv[cmd[mode].dargpos + 1], 0, 0);
-	} else {
-		args.dev_unit = -1;
+	if (cmd[mode].flags & FLAGS_DEV_NAME_RQRD && (args.dev_name == NULL)) {
+		printf("Command %s require dev name.\n", command);
+		usage();
+		return (1);
+	}
+	if (cmd[mode].flags & FLAGS_DEV_UNIT_RQRD && (args.dev_unit == -1)) {
+		printf("Command %s require dev unit.\n", command);
+		usage();
+		return (1);
 	}
 
 #ifdef DEBUG
@@ -133,12 +164,11 @@
 		    "\tbus = %s%d\n"
 		    "\tdevice = %s%d\n",
 		cmd[mode].name,
-		(cmd[mode].bargpos != -1)?args.bus_name:"any",
-		(cmd[mode].bargpos != -1)?args.bus_unit:0,
+		(args.bus_name != NULL) ? args.bus_name : "any",
+		(args.bus_unit != -1) ? args.bus_unit : 0,
 		args.dev_name,
 		args.dev_unit
 	);
-	exit(0);
 #endif
 
 	fd = open(DEVCTL, O_RDONLY);


More information about the Zrouter-src-freebsd mailing list