[U-Boot] [PATCH] cmd_usb.c: implemented standard subcommand handling

Frans Meulenbroeks fransmeulenbroeks at gmail.com
Thu Mar 25 21:23:02 CET 2010


also added a missing \n in the help messages

Signed-off-by: Frans Meulenbroeks <fransmeulenbroeks at gmail.com>
---
 common/cmd_usb.c |  391 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 233 insertions(+), 158 deletions(-)

as discussed before on the mailing list
tested on sheevaplug

diff --git a/common/cmd_usb.c b/common/cmd_usb.c
index 9de515c..ef80d78 100644
--- a/common/cmd_usb.c
+++ b/common/cmd_usb.c
@@ -502,199 +502,274 @@ int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 }
 #endif /* CONFIG_USB_STORAGE */
 
+static int do_usb_reset(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	int i;
 
-/******************************************************************************
- * usb command intepreter
- */
-int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+	usb_stop();
+	printf("(Re)start USB...\n");
+	i = usb_init();
+#ifdef CONFIG_USB_STORAGE
+	/* try to recognize storage devices immediately */
+	if (i >= 0)
+		usb_stor_curr_dev = usb_stor_scan(1);
+#endif
+	return 0;
+}
+
+extern char usb_started;
+
+static int do_usb_tree(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 {
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
+	}
+	printf("\nDevice Tree:\n");
+	usb_show_tree(usb_get_dev_index(0));
+	return 0;
+}
 
+static int do_usb_info(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	int d;
 	int i;
 	struct usb_device *dev = NULL;
-	extern char usb_started;
-#ifdef CONFIG_USB_STORAGE
-	block_dev_desc_t *stor_dev;
-#endif
 
-	if ((strncmp(argv[1], "reset", 5) == 0) ||
-		 (strncmp(argv[1], "start", 5) == 0)) {
-		usb_stop();
-		printf("(Re)start USB...\n");
-		i = usb_init();
-#ifdef CONFIG_USB_STORAGE
-		/* try to recognize storage devices immediately */
-		if (i >= 0)
-			usb_stor_curr_dev = usb_stor_scan(1);
-#endif
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
+	}
+
+	if (argc == 1) {
+		for (d = 0; d < USB_MAX_DEVICE; d++) {
+			dev = usb_get_dev_index(d);
+			if (dev == NULL)
+				break;
+			usb_display_desc(dev);
+			usb_display_config(dev);
+		}
 		return 0;
+	} else {
+		i = simple_strtoul(argv[1], NULL, 16);
+		printf("config for device %d\n", i);
+		for (d = 0; d < USB_MAX_DEVICE; d++) {
+			dev = usb_get_dev_index(d);
+			if (dev == NULL)
+				break;
+			if (dev->devnum == i)
+				break;
+		}
+		if (dev == NULL) {
+			printf("*** NO Device avaiable ***\n");
+			return 0;
+		} else {
+			usb_display_desc(dev);
+			usb_display_config(dev);
+		}
 	}
-	if (strncmp(argv[1], "stop", 4) == 0) {
+	return 0;
+}
+
+#if defined(CONFIG_USB_STORAGE)
+static int do_usb_stop(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
 #ifdef CONFIG_USB_KEYBOARD
-		if (argc == 2) {
-			if (usb_kbd_deregister() != 0) {
-				printf("USB not stopped: usbkbd still"
-					" using USB\n");
-				return 1;
-			}
-		} else {
-			/* forced stop, switch console in to serial */
-			console_assign(stdin, "serial");
-			usb_kbd_deregister();
+	if (argc == 1) {
+		if (usb_kbd_deregister() != 0) {
+			printf("USB not stopped: usbkbd still"
+				" using USB\n");
+			return 1;
 		}
-#endif
-		printf("stopping USB..\n");
-		usb_stop();
-		return 0;
+	} else {
+		/* forced stop, switch console in to serial */
+		console_assign(stdin, "serial");
+		usb_kbd_deregister();
 	}
+#endif
+	printf("stopping USB..\n");
+	usb_stop();
+	return 0;
+}
+
+static int do_usb_storage(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
 	if (!usb_started) {
 		printf("USB is stopped. Please issue 'usb start' first.\n");
 		return 1;
 	}
-	if (strncmp(argv[1], "tree", 4) == 0) {
-		printf("\nDevice Tree:\n");
-		usb_show_tree(usb_get_dev_index(0));
-		return 0;
+	return usb_stor_info();
+}
+
+static int do_usb_dev(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	block_dev_desc_t *stor_dev;
+
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
 	}
-	if (strncmp(argv[1], "inf", 3) == 0) {
-		int d;
-		if (argc == 2) {
-			for (d = 0; d < USB_MAX_DEVICE; d++) {
-				dev = usb_get_dev_index(d);
-				if (dev == NULL)
-					break;
-				usb_display_desc(dev);
-				usb_display_config(dev);
-			}
-			return 0;
-		} else {
-			int d;
-
-			i = simple_strtoul(argv[2], NULL, 16);
-			printf("config for device %d\n", i);
-			for (d = 0; d < USB_MAX_DEVICE; d++) {
-				dev = usb_get_dev_index(d);
-				if (dev == NULL)
-					break;
-				if (dev->devnum == i)
-					break;
-			}
-			if (dev == NULL) {
-				printf("*** NO Device avaiable ***\n");
-				return 0;
-			} else {
-				usb_display_desc(dev);
-				usb_display_config(dev);
-			}
+	if (argc == 2) {
+		int dev = (int)simple_strtoul(argv[1], NULL, 10);
+		printf("\nUSB device %d: ", dev);
+		if (dev >= USB_MAX_STOR_DEV) {
+			printf("unknown device\n");
+			return 1;
 		}
+		printf("\n    Device %d: ", dev);
+		stor_dev = usb_stor_get_dev(dev);
+		dev_print(stor_dev);
+		if (stor_dev->type == DEV_TYPE_UNKNOWN)
+			return 1;
+		usb_stor_curr_dev = dev;
+		printf("... is now current device\n");
+		return 0;
+	} else {
+		printf("\nUSB device %d: ", usb_stor_curr_dev);
+		stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
+		dev_print(stor_dev);
+		if (stor_dev->type == DEV_TYPE_UNKNOWN)
+			return 1;
 		return 0;
 	}
-#ifdef CONFIG_USB_STORAGE
-	if (strncmp(argv[1], "stor", 4) == 0)
-		return usb_stor_info();
-
-	if (strncmp(argv[1], "part", 4) == 0) {
-		int devno, ok = 0;
-		if (argc == 2) {
-			for (devno = 0; devno < USB_MAX_STOR_DEV; ++devno) {
-				stor_dev = usb_stor_get_dev(devno);
-				if (stor_dev->type != DEV_TYPE_UNKNOWN) {
-					ok++;
-					if (devno)
-						printf("\n");
-					printf("print_part of %x\n", devno);
-					print_part(stor_dev);
-				}
-			}
-		} else {
-			devno = simple_strtoul(argv[2], NULL, 16);
+	return 0;
+}
+
+static int do_usb_part(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	int devno, ok = 0;
+	block_dev_desc_t *stor_dev;
+
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
+	}
+	if (argc == 1) {
+		for (devno = 0; devno < USB_MAX_STOR_DEV; ++devno) {
 			stor_dev = usb_stor_get_dev(devno);
 			if (stor_dev->type != DEV_TYPE_UNKNOWN) {
 				ok++;
+				if (devno)
+					printf("\n");
 				printf("print_part of %x\n", devno);
 				print_part(stor_dev);
 			}
 		}
-		if (!ok) {
-			printf("\nno USB devices available\n");
-			return 1;
+	} else {
+		devno = simple_strtoul(argv[1], NULL, 16);
+		stor_dev = usb_stor_get_dev(devno);
+		if (stor_dev->type != DEV_TYPE_UNKNOWN) {
+			ok++;
+			printf("print_part of %x\n", devno);
+			print_part(stor_dev);
 		}
-		return 0;
 	}
-	if (strcmp(argv[1], "read") == 0) {
-		if (usb_stor_curr_dev < 0) {
-			printf("no current device selected\n");
-			return 1;
-		}
-		if (argc == 5) {
-			unsigned long addr = simple_strtoul(argv[2], NULL, 16);
-			unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
-			unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
-			unsigned long n;
-			printf("\nUSB read: device %d block # %ld, count %ld"
-				" ... ", usb_stor_curr_dev, blk, cnt);
-			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
-			n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
-						 (ulong *)addr);
-			printf("%ld blocks read: %s\n", n,
-				(n == cnt) ? "OK" : "ERROR");
-			if (n == cnt)
-				return 0;
-			return 1;
-		}
+	if (!ok) {
+		printf("\nno USB devices available\n");
+		return 1;
 	}
-	if (strcmp(argv[1], "write") == 0) {
-		if (usb_stor_curr_dev < 0) {
-			printf("no current device selected\n");
-			return 1;
-		}
-		if (argc == 5) {
-			unsigned long addr = simple_strtoul(argv[2], NULL, 16);
-			unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
-			unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
-			unsigned long n;
-			printf("\nUSB write: device %d block # %ld, count %ld"
-				" ... ", usb_stor_curr_dev, blk, cnt);
-			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
-			n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
-						(ulong *)addr);
-			printf("%ld blocks write: %s\n", n,
-				(n == cnt) ? "OK" : "ERROR");
-			if (n == cnt)
-				return 0;
-			return 1;
-		}
+	return 0;
+}
+
+static int do_usb_read(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	block_dev_desc_t *stor_dev;
+
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
 	}
-	if (strncmp(argv[1], "dev", 3) == 0) {
-		if (argc == 3) {
-			int dev = (int)simple_strtoul(argv[2], NULL, 10);
-			printf("\nUSB device %d: ", dev);
-			if (dev >= USB_MAX_STOR_DEV) {
-				printf("unknown device\n");
-				return 1;
-			}
-			printf("\n    Device %d: ", dev);
-			stor_dev = usb_stor_get_dev(dev);
-			dev_print(stor_dev);
-			if (stor_dev->type == DEV_TYPE_UNKNOWN)
-				return 1;
-			usb_stor_curr_dev = dev;
-			printf("... is now current device\n");
-			return 0;
-		} else {
-			printf("\nUSB device %d: ", usb_stor_curr_dev);
-			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
-			dev_print(stor_dev);
-			if (stor_dev->type == DEV_TYPE_UNKNOWN)
-				return 1;
+	if (usb_stor_curr_dev < 0) {
+		printf("no current device selected\n");
+		return 1;
+	}
+	if (argc == 4) {
+		unsigned long addr = simple_strtoul(argv[1], NULL, 16);
+		unsigned long blk  = simple_strtoul(argv[2], NULL, 16);
+		unsigned long cnt  = simple_strtoul(argv[3], NULL, 16);
+		unsigned long n;
+		printf("\nUSB read: device %d block # %ld, count %ld"
+			" ... ", usb_stor_curr_dev, blk, cnt);
+		stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
+		n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
+					 (ulong *)addr);
+		printf("%ld blocks read: %s\n", n,
+			(n == cnt) ? "OK" : "ERROR");
+		if (n == cnt)
 			return 0;
-		}
-		return 0;
 	}
-#endif /* CONFIG_USB_STORAGE */
 	cmd_usage(cmdtp);
 	return 1;
 }
 
+static int do_usb_write(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	block_dev_desc_t *stor_dev;
+
+	if (!usb_started) {
+		printf("USB is stopped. Please issue 'usb start' first.\n");
+		return 1;
+	}
+	if (usb_stor_curr_dev < 0) {
+		printf("no current device selected\n");
+		return 1;
+	}
+	if (argc == 4) {
+		unsigned long addr = simple_strtoul(argv[1], NULL, 16);
+		unsigned long blk  = simple_strtoul(argv[2], NULL, 16);
+		unsigned long cnt  = simple_strtoul(argv[3], NULL, 16);
+		unsigned long n;
+		printf("\nUSB write: device %d block # %ld, count %ld"
+			" ... ", usb_stor_curr_dev, blk, cnt);
+		stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
+		n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
+					(ulong *)addr);
+		printf("%ld blocks write: %s\n", n,
+			(n == cnt) ? "OK" : "ERROR");
+		if (n == cnt)
+			return 0;
+	} 
+	cmd_usage(cmdtp);
+	return 1;
+}
+#endif  /* CONFIG_USB_STORAGE */
+
+static cmd_tbl_t cmd_usb_sub[] = {
+	U_BOOT_CMD_MKENT(reset, 1, 0, do_usb_reset, "", ""),
+	U_BOOT_CMD_MKENT(start, 1, 0, do_usb_reset, "", ""),
+	U_BOOT_CMD_MKENT(tree, 1, 0, do_usb_tree, "", ""),
+	U_BOOT_CMD_MKENT(info, 2, 0, do_usb_info, "", ""),
+#if defined(CONFIG_USB_STORAGE)
+	U_BOOT_CMD_MKENT(stop, 2, 0, do_usb_stop, "", ""),
+	U_BOOT_CMD_MKENT(storage, 1, 0, do_usb_storage, "", ""),
+	U_BOOT_CMD_MKENT(dev, 2, 0, do_usb_dev, "", ""),
+	U_BOOT_CMD_MKENT(part, 2, 0, do_usb_part, "", ""),
+	U_BOOT_CMD_MKENT(read, 4, 0, do_usb_read, "", ""),
+	U_BOOT_CMD_MKENT(write, 4, 0, do_usb_write, "", ""),
+#endif  /* CONFIG_USB_STORAGE */
+};
+
+/******************************************************************************
+ * usb command intepreter
+ */
+static int do_usb(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	cmd_tbl_t *c;
+
+	/* Strip off leading 'usb' command argument */
+	argc--;
+	argv++;
+
+	c = find_cmd_tbl(argv[0], &cmd_usb_sub[0], ARRAY_SIZE(cmd_usb_sub));
+
+	if (c) {
+		return  c->cmd(cmdtp, flag, argc, argv);
+	} else {
+		cmd_usage(cmdtp);
+		return 1;
+	}
+}
+
 #ifdef CONFIG_USB_STORAGE
 U_BOOT_CMD(
 	usb,	5,	1,	do_usb,
@@ -708,7 +783,7 @@ U_BOOT_CMD(
 	"usb part [dev] - print partition table of one or all USB storage"
 	" devices\n"
 	"usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
-	"    to memory address `addr'"
+	"    to memory address `addr'\n"
 	"usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
 	"    from memory address `addr'"
 );
-- 
1.6.4.2



More information about the U-Boot mailing list