mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
@ 2014-05-21  8:54 Antony Pavlov
  2014-05-21 13:20 ` Holger Schurig
  0 siblings, 1 reply; 7+ messages in thread
From: Antony Pavlov @ 2014-05-21  8:54 UTC (permalink / raw)
  To: barebox

This patch adds U-Boot 'usb tree' command functionality to barebox.

Here is a sample 'lsusb -t' output on RC Module MB 77.07 board
with numerous USB devices attached:

MB 77.07: / lsusb -t
USB: scanning bus for devices...
0 USB Device(s) found
  1 ID 0000:0000
  |  u-boot EHCI Host Controller
  |
  +-2 ID 1a40:0101
    |   USB 2.0 Hub
    |
    +-3 ID 05e3:0660
    | |   USB2.0 Hub
    | |
    | +-4 ID 2001:3c05
    | |    D-Link Corporation DUB-E100 000001
    | |
    | +-5 ID 046d:0824
    | |      225ACF90
    | |
    | +-6 ID 05e3:0608
    | | |   USB2.0 Hub
    | | |
    | | +-7 ID 13fe:3e00
    | | |    UFD 2.0 Silicon-Power4G 201212SP0014070F2CB454C8B411
    | | |
    | | +-8 ID 04d9:1605
    | | |      USB Keyboard
    | | |
    | | +-9 ID 0424:7500
    | | |    SMSC LAN7500 00000001b
    | | |
    | | +-10 ID 067b:2303
    | |      Prolific Technology Inc. USB-Serial Controller
    | |
    | +-11 ID 1267:0201
    |       PS/2+USB Mouse
    |
    +-12 ID 067b:2305
    |    Prolific Technology Inc. IEEE-1284 Controller
    |
    +-13 ID 1005:b113
          USB FLASH DRIVE 070F29A906CF5145

Original 'usb tree' output contains addition information
on device type, bandwidth and consumption current.

Here is a U-Boot 'usb tree' output on the same board:

MBOOT # usb tree

Device Tree:
  1  Hub (480 Mb/s, 0mA)
  |  u-boot EHCI Host Controller
  |
  +-2  Hub (480 Mb/s, 100mA)
    |   USB 2.0 Hub
    |
    +-3  Hub (480 Mb/s, 100mA)
    | |   USB2.0 Hub
    | |
    | +-4  Vendor specific (480 Mb/s, 250mA)
    | |    D-Link Corporation DUB-E100 000001
    | |
    | +-5  See Interface (480 Mb/s, 500mA)
    | |      225ACF90
    | |
    | +-6  Hub (480 Mb/s, 100mA)
    | | |   USB2.0 Hub
    | | |
    | | +-7  Mass Storage (480 Mb/s, 200mA)
    | | |    UFD 2.0 Silicon-Power4G 201212SP0014070F2CB454C8B411
    | | |
    | | +-8  Human Interface (1.5 Mb/s, 100mA)
    | | |      USB Keyboard
    | | |
    | | +-9  Vendor specific (480 Mb/s, 320mA)
    | | |    SMSC LAN7500 00000001b
    | | |
    | | +-10  Vendor specific (12 Mb/s, 100mA)
    | |      Prolific Technology Inc. USB-Serial Controller
    | |
    | +-11  Human Interface (1.5 Mb/s, 100mA)
    |       PS/2+USB Mouse
    |
    +-12  Printer (12 Mb/s, 100mA)
    |    Prolific Technology Inc. IEEE-1284 Controller
    |
    +-13  Mass Storage (480 Mb/s, 200mA)
          USB FLASH DRIVE 070F29A906CF5145

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 commands/usb.c         | 118 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/usb.c |   2 +-
 include/usb/usb.h      |   3 ++
 3 files changed, 122 insertions(+), 1 deletion(-)

diff --git a/commands/usb.c b/commands/usb.c
index 073c79c..08cfa42 100644
--- a/commands/usb.c
+++ b/commands/usb.c
@@ -55,3 +55,121 @@ BAREBOX_CMD_START(usb)
 	BAREBOX_CMD_HELP(cmd_usb_help)
 	BAREBOX_CMD_COMPLETE(empty_complete)
 BAREBOX_CMD_END
+
+/* shows the device tree recursively */
+static void usb_show_tree_graph(struct usb_device *dev, char *pre)
+{
+	int i, index;
+	int has_child, last_child;
+
+	index = strlen(pre);
+	printf(" %s", pre);
+	/* check if the device has connected children */
+	has_child = 0;
+	for (i = 0; i < dev->maxchild; i++) {
+		if (dev->children[i] != NULL)
+			has_child = 1;
+	}
+	/* check if we are the last one */
+	last_child = 1;
+	if (dev->parent != NULL) {
+		for (i = 0; i < dev->parent->maxchild; i++) {
+			/* search for children */
+			if (dev->parent->children[i] == dev) {
+				/* found our pointer, see if we have a
+				 * little sister
+				 */
+				while (i++ < dev->parent->maxchild) {
+					if (dev->parent->children[i] != NULL) {
+						/* found a sister */
+						last_child = 0;
+						break;
+					} /* if */
+				} /* while */
+			} /* device found */
+		} /* for all children of the parent */
+		printf("\b+-");
+		/* correct last child */
+		if (last_child)
+			pre[index-1] = ' ';
+	} /* if not root hub */
+	else
+		printf(" ");
+	printf("%d ", dev->devnum);
+	pre[index++] = ' ';
+	pre[index++] = has_child ? '|' : ' ';
+	pre[index] = 0;
+	printf("ID %04x:%04x\n",
+		dev->descriptor->idVendor, dev->descriptor->idProduct);
+	if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
+		printf(" %s  %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
+	printf(" %s\n", pre);
+	if (dev->maxchild > 0) {
+		for (i = 0; i < dev->maxchild; i++) {
+			if (dev->children[i] != NULL) {
+				usb_show_tree_graph(dev->children[i], pre);
+				pre[index] = 0;
+			}
+		}
+	}
+}
+
+/* main routine for the tree command */
+static void usb_show_tree(struct usb_device *dev)
+{
+	char preamble[32];
+
+	memset(preamble, 0, 32);
+	usb_show_tree_graph(dev, &preamble[0]);
+}
+
+static int do_lsusb(int argc, char *argv[])
+{
+	int opt;
+	int tree = 0;
+	struct usb_device *dev;
+
+	while ((opt = getopt(argc, argv, "t")) > 0) {
+		switch (opt) {
+		case 't':
+			tree = 1;
+			break;
+		default:
+			return opt;
+		}
+	}
+
+	usb_rescan(0);
+
+	list_for_each_entry(dev, &usb_device_list, list) {
+
+		if (tree) {
+			if (dev->parent == NULL)
+				usb_show_tree(dev);
+		} else {
+			printf("Bus %03d Device %03d: ID %04x:%04x %s\n",
+				dev->host->busnum, dev->devnum,
+				dev->descriptor->idVendor,
+				dev->descriptor->idProduct,
+				dev->prod);
+		}
+	}
+
+	return 0;
+}
+
+BAREBOX_CMD_HELP_START(lsusb)
+BAREBOX_CMD_HELP_TEXT("list USB devices")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-t", "dump the physical USB device hierarchy as a tree")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(lsusb)
+	.cmd		= do_lsusb,
+	BAREBOX_CMD_DESC("list USB devices")
+	BAREBOX_CMD_OPTS("[-t]")
+	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+	BAREBOX_CMD_HELP(cmd_lsusb_help)
+	BAREBOX_CMD_COMPLETE(empty_complete)
+BAREBOX_CMD_END
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 9c1571d..32ea5ec 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -70,7 +70,7 @@ static int hub_port_reset(struct usb_device *dev, int port,
 			  unsigned short *portstat);
 
 static LIST_HEAD(host_list);
-static LIST_HEAD(usb_device_list);
+LIST_HEAD(usb_device_list);
 
 static void print_usb_device(struct usb_device *dev)
 {
diff --git a/include/usb/usb.h b/include/usb/usb.h
index 821724e..9319f2e 100644
--- a/include/usb/usb.h
+++ b/include/usb/usb.h
@@ -544,4 +544,7 @@ enum usb_phy_interface {
 	USBPHY_INTERFACE_MODE_SERIAL,
 	USBPHY_INTERFACE_MODE_HSIC,
 };
+
+extern struct list_head usb_device_list;
+
 #endif /*_USB_H_ */
-- 
1.9.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-05-21  8:54 [PATCH] [RFC] [WIP] commands: add tree view capable lsusb Antony Pavlov
@ 2014-05-21 13:20 ` Holger Schurig
  2014-05-21 14:11   ` Antony Pavlov
  0 siblings, 1 reply; 7+ messages in thread
From: Holger Schurig @ 2014-05-21 13:20 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

Nice, I would just remove the extra empty lines. Linux' "lsusb -t" for
example produces this:

holger@desktop:~$ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/8p, 480M
        |__ Port 1: Dev 27, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 2: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
        |__ Port 6: Dev 126, If 0, Class=hub, Driver=hub/2p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M

Also please add an entry into commands/Kconfig: And put it into the
info group, lsusb just shows something, it doesn't manipulate any
hardware.

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-05-21 13:20 ` Holger Schurig
@ 2014-05-21 14:11   ` Antony Pavlov
  2014-06-02  7:24     ` Sascha Hauer
  0 siblings, 1 reply; 7+ messages in thread
From: Antony Pavlov @ 2014-05-21 14:11 UTC (permalink / raw)
  To: Holger Schurig; +Cc: barebox

On Wed, 21 May 2014 15:20:21 +0200
Holger Schurig <holgerschurig@gmail.com> wrote:

> Nice, I would just remove the extra empty lines. Linux' "lsusb -t" for

Good idea! I'll take a look on Linux' lsusb too.

> example produces this:
> 
> holger@desktop:~$ lsusb -t
> /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
>     |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/8p, 480M
>         |__ Port 1: Dev 27, If 0, Class=HID, Driver=usbhid, 1.5M
>         |__ Port 2: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
>         |__ Port 6: Dev 126, If 0, Class=hub, Driver=hub/2p, 480M
> /:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
>     |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
> 
> Also please add an entry into commands/Kconfig:

I'll separate lsusb code into independent lsusb.c file.

> And put it into the
> info group, lsusb just shows something, it doesn't manipulate any
> hardware.

Not exactly so. It invokes usb_rescan(0).

-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-05-21 14:11   ` Antony Pavlov
@ 2014-06-02  7:24     ` Sascha Hauer
  2014-06-02  7:59       ` Holger Schurig
  0 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2014-06-02  7:24 UTC (permalink / raw)
  To: Antony Pavlov; +Cc: barebox

On Wed, May 21, 2014 at 06:11:23PM +0400, Antony Pavlov wrote:
> On Wed, 21 May 2014 15:20:21 +0200
> Holger Schurig <holgerschurig@gmail.com> wrote:
> 
> > Nice, I would just remove the extra empty lines. Linux' "lsusb -t" for
> 
> Good idea! I'll take a look on Linux' lsusb too.
> 
> > example produces this:
> > 
> > holger@desktop:~$ lsusb -t
> > /:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
> >     |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/8p, 480M
> >         |__ Port 1: Dev 27, If 0, Class=HID, Driver=usbhid, 1.5M
> >         |__ Port 2: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
> >         |__ Port 6: Dev 126, If 0, Class=hub, Driver=hub/2p, 480M
> > /:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
> >     |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
> > 
> > Also please add an entry into commands/Kconfig:
> 
> I'll separate lsusb code into independent lsusb.c file.
> 
> > And put it into the
> > info group, lsusb just shows something, it doesn't manipulate any
> > hardware.
> 
> Not exactly so. It invokes usb_rescan(0).

You could drop the usb_rescan(). Would that be too confusing?

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-06-02  7:24     ` Sascha Hauer
@ 2014-06-02  7:59       ` Holger Schurig
  2014-06-02  8:31         ` Sascha Hauer
  0 siblings, 1 reply; 7+ messages in thread
From: Holger Schurig @ 2014-06-02  7:59 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

Hmm, I like that it does a rescan, so that it always display the
currently connected USB device automatically.

However, for me a usb_rescan() isn't really hardware manipulation.
Sure, the USB device will get send down it's ID during enumeration,
but it doesn't turn things on/off, erase, program it etc.

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-06-02  7:59       ` Holger Schurig
@ 2014-06-02  8:31         ` Sascha Hauer
  2014-06-02 10:20           ` Antony Pavlov
  0 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2014-06-02  8:31 UTC (permalink / raw)
  To: Holger Schurig; +Cc: barebox

On Mon, Jun 02, 2014 at 09:59:31AM +0200, Holger Schurig wrote:
> Hmm, I like that it does a rescan, so that it always display the
> currently connected USB device automatically.

Note that we only scan scan the bus once during the lifetime of barebox
unless the force parameter of usb_rescan is true. This is because
we currently remove all existing USB devices and reinstantiate all
USB devices found during scanning. The removal of devices is not very
well implemented. USB only works properly when the tree is scanned once.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] [RFC] [WIP] commands: add tree view capable lsusb
  2014-06-02  8:31         ` Sascha Hauer
@ 2014-06-02 10:20           ` Antony Pavlov
  0 siblings, 0 replies; 7+ messages in thread
From: Antony Pavlov @ 2014-06-02 10:20 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Mon, 2 Jun 2014 10:31:07 +0200
Sascha Hauer <s.hauer@pengutronix.de> wrote:

> On Mon, Jun 02, 2014 at 09:59:31AM +0200, Holger Schurig wrote:
> > Hmm, I like that it does a rescan, so that it always display the
> > currently connected USB device automatically.
> 
> Note that we only scan scan the bus once during the lifetime of barebox
> unless the force parameter of usb_rescan is true. This is because
> we currently remove all existing USB devices and reinstantiate all
> USB devices found during scanning. The removal of devices is not very
> well implemented. USB only works properly when the tree is scanned once.

If I drop usb_rescan(0) then I have to run 'usb' command to scan bus manually before running 'lsusb'.
It is very uncomfortable.

-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-06-02 10:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-21  8:54 [PATCH] [RFC] [WIP] commands: add tree view capable lsusb Antony Pavlov
2014-05-21 13:20 ` Holger Schurig
2014-05-21 14:11   ` Antony Pavlov
2014-06-02  7:24     ` Sascha Hauer
2014-06-02  7:59       ` Holger Schurig
2014-06-02  8:31         ` Sascha Hauer
2014-06-02 10:20           ` Antony Pavlov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox