Skip to content

examples: USB/IP server for the USB device model - #608

Open
teknoman117 wants to merge 5 commits into
buserror:masterfrom
teknoman117:usbip
Open

teknoman117 wants to merge 5 commits into
buserror:masterfrom
teknoman117:usbip

Conversation

@teknoman117

@teknoman117 teknoman117 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

This supersedes #247. Closes #286: the out of tree usb-vhci kernel modules that issue is about are removed. vhci-hcd is in the mainline kernel (drivers/usb/usbip) and replaces them.

It exports the simulated USB device over USB/IP so that a real USB stack can use it. The host's own tools then talk to the firmware as to a board plugged in. Avrdude can upload through Caterina (The bootloader for Arduino Leonardo and Micro) and CDC sketches show up as /dev/ttyACMn.

USB/IP carries USB requests over TCP. A client lists the devices a server exports and imports one. The client's virtual host controller then sends each transfer as a request and gets the reply on the same connection. On Linux the client is the mainline vhci-hcd driver with the usbip tool. On Windows it is usbip-win2. The protocol and how the part maps it onto avr_usb.c are described at the top of usbip_usb.c.

Commits:

  • examples: remove the usb-vhci bridge: the usb-vhci module and libusb_vhci are unmaintained and the module no longer builds on current kernels. vhci-hcd replaces them.
  • avr_usb sim: fixes for a USB/IP host: a control write data stage NAKs until the firmware has read the SETUP. A bulk IN read with no data NAKs instead of returning zero bytes. That reply was a workaround for the removed bridge. avr_reset() zeroes the cycle count before the peripheral reset callbacks so the timers they start are due on time.
  • examples/parts: USB/IP server: usbip_usb.c. It runs from a 100us cycle timer in the simulation thread so nothing calls into the model from another thread. It enumerates the device when the firmware attaches. Several clients can list the device and one can import it. Packets the firmware NAKs are tried again on the next tick. It builds with POSIX sockets and Winsock.
  • examples: board_simduino_leonardo: an Arduino Leonardo. The ATmega32U4 runs Caterina from the boot section through the BOOTRST and BOOTSZ fuses. The flash and EEPROM persist between runs in an Intel HEX file. Ctrl-\ presses the reset button.
  • examples: board_usbserial: the Uno R3's USB to serial adapter. The ATmega16U2 runs Arduino-usbserial with USART1 looped back or on a pty. usbip_check.py drives the server the way vhci-hcd and cdc-acm do without needing a kernel.

The two .hex images are the ArduinoCore-avr ones. The Caterina image has its demonstration sketch removed. Each board's LICENSE gives the origin and the LUFA notice.

Testing:

  • Linux with vhci-hcd and usbip: avrdude uploads a sketch through Caterina and verifies it. The sketch echoes through /dev/ttyACM0 and the 1200 baud touch resets it into the bootloader.
  • usbip_check.py against board_usbserial: it checks the listing while the device is imported and that a second import is refused. It also checks loopback data and unlinks.
  • The test suite passes. The part and both boards compile with mingw-w64 for 32 and 64 bit Windows. They haven't been run against usbip-win2 on real Windows. I haven't been able to build on macOS.

Limitations:

  • No isochronous transfers.
  • When the firmware leaves the bus the connection closes and the client has to attach again. A real device on a USB/IP server behaves the same way.
  • The server is only serviced while the simulation runs. It doesn't answer while gdb has the core stopped.

The usb-vhci kernel module and libusb_vhci are unmaintained and the
module does not build on current kernels. The mainline vhci-hcd USB/IP
client replaces them.

The examples Makefile now uses filter-out for the board list.
- A control write data stage NAKs until the firmware reads the SETUP.
- A bulk IN read with no data NAKs instead of returning zero bytes.
  The zero byte reply was a workaround for the removed vhci bridge.
- avr_reset() zeroes the cycle count before the peripheral reset
  callbacks. Timers they start were due late otherwise.
usbip_usb.c exports the simulated USB device over USB/IP 1.1.1 on TCP
port 3240. Linux vhci-hcd and usbip-win2 can attach it.

- Runs from a 100us cycle timer in the simulation thread.
- Enumerates the device on attach to list it and learn its endpoint
  sizes.
- Several clients can list the device. One can import it.
- EP0 gets one transaction per tick. Bulk pipes get up to 16 packets.
- Waits 10ms after a bus reset.
- Restarts its timer after an AVR reset. avr_terminate() frees it.
- Builds with POSIX sockets and Winsock. Uses SO_NOSIGPIPE on macOS.
- Sockets are non-blocking. A full send buffer waits in select().
- Protocol messages are structs converted with htonl() and ntohl().
- Listens on 127.0.0.1 by default.
An Arduino Leonardo over USB/IP: an ATmega32U4 at 16MHz running
Caterina with the Leonardo BOOTRST and BOOTSZ bits.

- The flash and EEPROM persist between runs in an Intel HEX file.
- -r starts as after the reset button. Ctrl-\ presses it.
- A sketch can be given as .hex or .elf.

Caterina-Leonardo-bootloader.hex is the ArduinoCore-avr image with the
demonstration sketch below 0x7000 removed. LICENSE has its origin and
the LUFA notice. The .hex is force-added past the *.hex ignore rule.
The Arduino Uno R3 USB to serial adapter over USB/IP: an ATmega16U2
running Arduino-usbserial.

- USART1 is looped back or connected to a pty with -t.
- Changes of the 328P RESET line are printed.
- Other firmware can be given as .hex or .elf.

usbip_check.py drives the server like vhci-hcd and cdc-acm without a
kernel. Arduino-usbserial-atmega16u2-Uno-Rev3.hex is the unchanged
ArduinoCore-avr image. LICENSE has its origin and the LUFA notice. The
.hex is force-added past the *.hex ignore rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

USB Support Revisited - VHCI

1 participant