How to configure a USB remote server with an STM32 MPU board

Revision as of 11:54, 22 March 2021 by Registered User

1. Article purpose[edit source]

The purpose of this article is to explain how to configure an USB remote server with STM32MP157x-DKx More info green.png , managed by Linux on Cortex®-A7.

The Goal are to transform STM32MP157x-DKx More info green.png board on an USB remote server, manage USB device on a PC while the USB device are connected to a remote server on network.

USB remote verser
USB remote server

To configure a STM32MP157x-DKx More info green.png board on an USB remote server we will use USBIP to

This article provides step-by-step instructions to:

  • explain how to transform a linux board on an USB remote server with USBIP
  • configure Linux software to enable USBIP server (aka USB remote server),
  • configure PC client to connect to USBIP server.
  • to further, manage power for USB device on server with uhubctl tool
Info white.png Information
This configuration can be replicated with other STM32 MPU while you have activated on the Linux kernel USBIP kernel modules

2. Configure STM32 MPU board with USBIP[edit source]

2.1. Overview[edit source]

USB/IP Project aims to develop a general USB device sharing system over IP network. To share USB devices between computers with their full functionality, USB/IP encapsulates "USB I/O messages" into TCP/IP payloads and transmits them between computers. [1]

2.2. step by step on server[edit source]

The Installation of USBIP server are made on Linux embedded board.
The following chapter will show how to configure manually on board the USBIP service and how to enable it on STM32 MPU board on which all the configuration, file, and service are present.

2.2.1. Manual setup[edit source]

Before to use the usbip command you need to load the kernel modules associated to usbip:

  modprobe usbip-core
  modprobe usbip-host

Start the usbip server:

  usbipd -D

To make available an usb device connected to Linux embedded board you need to bind the device.

List available usb device:

  usbip list -l
- busid 2-1.1 (03f0:e207)
  HP, Inc : unknown product (03f0:e207)

Bind this specific device to be visible by a PC client (here device 2-1.1):

  usbip bind -b 2-1.1

(to automate the binding of new usb device, see the next section Automated setup)

2.2.2. Automated setup (available by default on STM32 MPU Board)[edit source]

All the configurations, files, services are present on STM32 MPU board by default.

  • Automate server startup via systemd

Create on board a service dedicated to start usbip server
Content of /lib/systemd/system/usbip.service

[Unit]
Description=USB-IP Bindind
After=network.target
Wants=network.target
 
[Service]
Type=forking
ExecStartPre=-/sbin/modprobe usbip-core
ExecStartPre=-/sbin/modprobe usbip-host
ExecStart=/usr/sbin/usbipd -D
ExecStop=/usr/sbin/stm32mp-usbip-bind-unbind.sh unload

[Install]
WantedBy=multi-user.target

Start systemd usbip service:

  systemctl daemon-reload 
  systemctl start usbip
  • Automate binding of new usb device

The binding of new usb device can be made by a script which bind or unbind the usb device following if it's plugged or unplugged.
This script must be called by an udev rules for the case of plug/unplug of device.

Content of script: /usr/sbin/stm32mp-usbip-bind-unbind.sh

#!/bin/sh -
#echo "Parameter number $#" >> /tmp/usbip.log
action=$1
devpath=$2
#echo "Parameter: $1" >> /tmp/usbip.log
#echo "Parameter: $2" >> /tmp/usbip.log

case $1 in
unload)
    modprobe -r usbip-core
    modprobe -r usbip-host
    kill -9 `pgrep usbipd`
    exit 0
    ;;
esac

subpath=$(echo $2 | sed "s|.*/\([^/]*\)|\1|")
#echo "subpath  >$subpath<" >> /tmp/usbip.log
if $(echo $subpath | grep -q ":");
then
    #echo "No valid path" >> /tmp/usbip.log
    exit 0
fi

# if usbip-core are not loaded, do nothing
if [ -z "$(cat /proc/modules | grep usbip_core)"  ]; then
   #echo "no module usbip_core loaded" >> /tmp/usbip.log
   exit 0
fi

case $1 in
add)
    #echo ">>> bind $subpath" >> /tmp/usbip.log
    usbip bind -b $subpath
    ;;
remove)
    #echo "<<< unbind $subpath" >> /tmp/usbip.log
    usbip unbind -b $subpath
    ;;
esac

Content of udev rules: /etc/udev/rules.d/99-usb-usbip.rules

ACTION=="remove", SUBSYSTEM=="usb", RUN+="/usr/sbin/stm32mp-usbip-bind-unbind.sh remove %p"

ACTION=="add", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/sbin/stm32mp-usbip-bind-unbind.sh add %p"

2.3. step by step on PC client[edit source]

2.3.1. On Windows[edit source]

An USBIP client exist for windows and are available via Github usbip-win

2.3.1.1. Installation[edit source]

For installation, follow instruction specified on README.md.

2.3.1.2. Usage[edit source]

User action:

  • List usb device available on specificy server
 usbip.exe list -l -r <usbip server ip>
  • Attach an usb device available on specificy server (here 2-1)
 usbip.exe list -r <usbip server ip> -b 2-1
  • List attached usb device on host PC
 usbip.exe port
  • Detach a specific attached usb device on host PC
List attached device on host PC and detach it (here 00)
 usbip detach -p 00

2.3.2. On Linux[edit source]

2.3.2.1. Installation[edit source]

On Ubuntu, you need to install some pre-requisite package:

 sudo apt-get install sysfsutils libwrap0-dev libglib2.0-dev libtool automake autoconf pkg-config linux-tools-generic


For having a translation of usb device name and ID you need to insatll a specific harware data base

  wget http://www.linux-usb.org/usb.ids
  sudo mkdir -p /usr/share/hwdata/
  sudo cp usb.ids /usr/share/hwdata/
2.3.2.2. Usage[edit source]

Before to use the usbip command you need to load the kernel modules associated to usbip:

 sudo modprobe usbip-core
 modprobe vhci-hcd
  • List usb remote device available on specific server:
 sudo usbip list -r <server IP>
  • Attach an usb remote device available on specific server:
 sudo usbip attach -r <server IP> -b <USB ID>
  • Detach a specific attached usb device on host PC

List usb device attached

 sudo usbip port

Detach specific usb device attached as 00

 sudo usbip detach -p 00

3. Configure STM32 MPU board to add usb power management with uhubctl[edit source]

3.1. Overview[edit source]

uhubctl is utility to control USB power per-port on smart USB hubs. Smart hub is defined as one that implements per-port power switching.[2]

3.2. Usage[edit source]

 uhubctl 
Current status for hub 2-1 [0424:2514]
 Port 1: 0507 power highspeed suspend enable connect [03f0:e207 Hewlett Packard HP Webcam HD 2300]
 Port 2: 0100 power
 Port 3: 0100 power
 Port 4: 0100 power
Current status for hub 2 [1d6b:0002 Linux 5.9.0-rc4 ehci_hcd EHCI Host Controller 5800d000.usbh-ehci]
 Port 1: 0503 power highspeed enable connect [0424:2514]
 Port 2: 0100 power
Current status for hub 1 [1d6b:0002 Linux 5.9.0-rc4 dwc2_hsotg DWC OTG Controller 49000000.usb-otg]
 Port 1: 0000 off

On hub 2-1 Port 1 a usb webcam are plugged:

 cat /sys/class/video4linux/video1/name 
HP Webcam HD 2300: HP Webcam HD

To power Off the usb webcam connected on this port:

 uhubctl -a off -p 1 -l 2-1
Current status for hub 2-1 [0424:2514]
 Port 1: 0507 power highspeed suspend enable connect [03f0:e207 Hewlett Packard HP Webcam HD 2300]
Sent power off request
New status for hub 2-1 [0424:2514]
 Port 1: 0000 off
 uhubctl 
Current status for hub 2-1 [0424:2514]
 Port 1: 0503 power highspeed enable connect [03f0:e207]
 Port 2: 0100 power
 Port 3: 0100 power
 Port 4: 0100 power
Current status for hub 2 [1d6b:0002 Linux 5.9.0-rc4 ehci_hcd EHCI Host Controller 5800d000.usbh-ehci]
 Port 1: 0503 power highspeed enable connect [0424:2514]
 Port 2: 0100 power
Current status for hub 1 [1d6b:0002 Linux 5.9.0-rc4 dwc2_hsotg DWC OTG Controller 49000000.usb-otg]
 Port 1: 0000 off

To power On the usb webcam connected on this port:

 uhubctl -a on -p 1 -l 2-1
Current status for hub 2-1 [0424:2514]
 Port 1: 0507 power highspeed suspend enable connect [03f0:e207 Hewlett Packard HP Webcam HD 2300]
Sent power on request
New status for hub 2-1 [0424:2514]
 Port 1: 0503 power highspeed enable connect [03f0:e207 Hewlett Packard HP Webcam HD 2300]
 uhubctl
Current status for hub 2-1 [0424:2514]
 Port 1: 0507 power highspeed suspend enable connect [03f0:e207 Hewlett Packard HP Webcam HD 2300]
 Port 2: 0100 power
 Port 3: 0100 power
 Port 4: 0100 power
Current status for hub 2 [1d6b:0002 Linux 5.9.0-rc4 ehci_hcd EHCI Host Controller 5800d000.usbh-ehci]
 Port 1: 0503 power highspeed enable connect [0424:2514]
 Port 2: 0100 power
Current status for hub 1 [1d6b:0002 Linux 5.9.0-rc4 dwc2_hsotg DWC OTG Controller 49000000.usb-otg]
 Port 1: 0000 off

4. References[edit source]


  1. USB/IP Project web page [1]
  2. uhubctl web page [2]