stschmit
Posts: 1
Joined: Wed May 05, 2021 3:20 pm

Consistent USB Symlink for Physical USB Ports Across Hardware Revisions

Fri May 07, 2021 7:59 pm

Greetings,
I (like others) have a desire to assign fixed port names or symlinks to physical USB ports on my pi. This allows us to easily connect support equipment in a repeatable way and be able to make safe assumptions in our system testing. I followed the guidance described in this thread:
viewtopic.php?t=157930

The Problem
That solution worked great for my RaspberryPi 3 B. When I used the same UDEV rules on a RaspberryPi 3 B+, I noticed the KERNELS string for the same USB device on the same USB port was different for the different Pi hardware revisions. Here's a side by side comparison of the USB device Kernels string for my Pi 3 B vs B+... (note the reuse of a couple strings for different ports)

Pi 3 B
Top Left USB: "1-1.2"
Bottom Left USB: "1-1.3"
Top Right USB: "1-1.4"
Bottom Right USB: "1-1.5"

Pi 3 B+
Top Left USB: "1-1.1.2"
Bottom Left USB: "1-1-1.3"
Top Right USB: "1-1.3"
Bottom Right USB: "1-1.2"

Because of this difference in KERNELS string in the different hardware, my original udev rules wouldn't work as expected on the Pi 3B+. On the 3B+, if I connected the same USB device to the lower right USB port, it would give me the symlink for top left USB port on the 3B. If I connected the USB device to top left, I would get no symlink at all!

My Solution
I tested a few different Pi 3B+ boards and they all enumerated the same way, so I ended up creating a bash script that checks the hardware revision and determines what to name the resulting symlink depending on the hardware revision and the KERNELS string of the connected device. I then updated my udev rules file to call this script in order to create the symlink properly whenever a ttyUSB device is connected to a specific USB port. Helpful udev rules link: http://reactivated.net/writing_udev_rules.html Hardware revisions taken from here: https://elinux.org/RPi_HardwareHistory

I attached the udev rules file I'm currently using.
udev_rules.jpg
udev_rules.jpg (53 KiB) Viewed 92 times
I attached the bash script that is determining the symlink name depending on hardware revision and KERNELS string.
getUsbComSymlink.jpg
getUsbComSymlink.jpg (64.09 KiB) Viewed 92 times
My Question
This works consistently now. If I connect the same USB device to the top left USB port on either hardware revision, I get a symlink to that device at /dev/COM1. Bottom left, /dev/COM2. Top Right, /dev/COM3. Bottom Right, /dev/COM4. So now our test can be written to use "/dev/COM1" for serial communication regardless of pi hardware revision. An obvious downside to this, is every time we want this same behavior on different/newer motherboards, we need to update the bash script and potentially the udev if the usb enumeration behaves differently. My solution works, but is there a better way? Is there any documentation around how to predict how USB devices will get connected on the USB bus(es) for the different pi hardware revisions?

Return to “Interfacing (DSI, CSI, I2C, etc.)”