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

Revision as of 15:54, 29 September 2020 by Registered User (→‎Article purpose)

1. Article purpose[edit source]

The purpose of this article is to explain how to configure an USB remote server with STM32MP157C-DK2, managed by Linux on Cortex®-A7.

The Goal are to transform STM32MP157C-DK2 board on an USB remote server, manage USB device on a PC while the USB device are connected to a remote server on network.

                                                                          |-----------| 
                   ___________________                              /-----|   PC 1    | Debug session via ST-LINK (USB2 over network)
 USB1 (webcam)--   | USB REMOTE      |                             /      |___________|       
                 \ | SERVER          |======== network ===========        |-----------|
                 / | STM32MP157C-DK2 |                             \      |   PC 2    | Preview camera via usb camera (USB1 over network)
 USB2 (STLINK)--   |_________________|                              \-----|___________| 
            

To configure a STM32MP157C-DK2 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

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.

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 render 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[edit source]

  • 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]