wiki:Docs/825gen2/Dev/Devices/USB

Version 11 (modified by Don Wilson, 10 months ago) ( diff )

--

USB

The lsusb terminal command may be used to view USB devices.

USB-C Flash drive

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

USB3 flash drive plugged into blue host connector closest to circuit board

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 4, If 0, Class=Mass Storage, Driver=usb-storage, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

USB3 flash drive plugged into blue host connector farther from circuit board

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 2: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

USB2 flash drive plugged into black USB2 connector

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 4: Dev 6, If 0, Class=Mass Storage, Driver=usb-storage, 480M

USB2 flash drive plugged into blue host connector closest to circuit board

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 7, If 0, Class=Mass Storage, Driver=usb-storage, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

USB2 flash drive plugged into blue host connector farther from circuit board

card825gen2:~$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 5000M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 2: Dev 8, If 0, Class=Mass Storage, Driver=usb-storage, 480M
        |__ Port 3: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M

The sysfs filesystem /sys can be used to detect USB devices.

Sysfs nothing plugged into 825 USB

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1  1-1.3  1-1.3:1.0  1-1:1.0  2-0:1.0  2-1  2-1:1.0  3-0:1.0  4-0:1.0  usb1  usb2  usb3  usb4

USB-C flash drive plugged in

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1  1-1.3  1-1.3:1.0  1-1:1.0  2-0:1.0  2-1  2-1:1.0  3-0:1.0  4-0:1.0  4-1  4-1:1.0  usb1  usb2  usb3  usb4
card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/4-1/product
 SanDisk 3.2Gen1

USB-3 flash drive plugged into blue host port closest to circuit board

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1.1      1-1.3      1-1:1.0  2-1      3-0:1.0  usb1  usb3
1-1      1-1.1:1.0  1-1.3:1.0  2-0:1.0  2-1:1.0  4-0:1.0  usb2  usb4
card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/1-1.1/product
Cruzer

USB-3 flash drive plugged into blue host port farther from circuit board

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1.2      1-1.3      1-1:1.0  2-1      3-0:1.0  usb1  usb3
1-1      1-1.2:1.0  1-1.3:1.0  2-0:1.0  2-1:1.0  4-0:1.0  usb2  usb4

card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/1-1.2/product
Cruzer

USB-2 flash drive plugged into black USB-2 host port

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1.3      1-1.4      1-1:1.0  2-1      3-0:1.0  usb1  usb3
1-1      1-1.3:1.0  1-1.4:1.0  2-0:1.0  2-1:1.0  4-0:1.0  usb2  usb4

card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/1-1.4/product
Mass storage

USB-3 flash drive plugged into black USB-2 host port

card825gen2:/sys/bus/usb/devices$ ls
1-0:1.0  1-1.3      1-1.4      1-1:1.0  2-1      3-0:1.0  usb1  usb3
1-1      1-1.3:1.0  1-1.4:1.0  2-0:1.0  2-1:1.0  4-0:1.0  usb2  usb4


card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/1-1.4/product
Cruzer

card825gen2:/sys/bus/usb/devices$ cat /sys/bus/usb/devices/1-1.4/product
 SanDisk 3.2Gen1

To programmatically detect what is plugged into each port the sysfs file tree may be read. The following example demonstrates a method of checking each port and reading the desciption of any attached devices.

typedef struct
{
	const char* portName;
	const char* sysfsPath;
	const char* usb2sysfsPath;

} USBPortInfo_t;

const USBPortInfo_t usbPortInfo[] = {
		{ "USB2",	"/sys/bus/usb/devices/1-1.4", 	NULL },
		{ "USB3-1",	"/sys/bus/usb/devices/2-1.1", 	"/sys/bus/usb/devices/1-1.1" },
		{ "USB3-2",	"/sys/bus/usb/devices/2-1.2", 	"/sys/bus/usb/devices/1-1.2" },
		{ "USB-C",	"/sys/bus/usb/devices/4-1", 	NULL },
};

void ScanUSBports(void)
{
   int n;
   char tmp[100];
   struct stat st;
   FILE* file;
   const char* pathFound;

   typedef enum { noUsb, usbFound, usb2Found } USBFound_t;

   USBFound_t found = noUsb;

   for(n = 0; n < ARRAY_ELEMS(usbPortInfo); n++) {
      if(stat(usbPortInfo[n].sysfsPath, &st) == 0) {
	found = usbFound;
	pathFound = usbPortInfo[n].sysfsPath;
      } else if((usbPortInfo[n].usb2sysfsPath != NULL) && (stat(usbPortInfo[n].usb2sysfsPath, &st) == 0)) {
	found = usb2Found;
	pathFound = usbPortInfo[n].usb2sysfsPath;
      } else {
	found = noUsb;
      }

      if(found != noUsb) {
	 sprintf_s(tmp, "%s/product", pathFound);
	 file = fopen(tmp, "r");
    	 if(file != NULL) {
	    if(found == usb2Found) {
	       strcpy(tmp, "*USB2* ");
	       fgets(tmp + 7, sizeof(tmp) - 7, file);
	    } else {
	       fgets(tmp, sizeof(tmp), file);
	    }
	    printf("%s [%s]\r\n", usbPortInfo[n].portName, tmp);
	    fclose(file);
	} 
	
    } 

Note: See TracWiki for help on using the wiki.