Repository navigation
examples: USB/IP bridge for the USB device model - #605
Closed
teknoman117 wants to merge 13 commits into
Closed
teknoman117 wants to merge 13 commits into
teknoman117 wants to merge 13 commits into
Conversation
- Per-core endpoint geometry: endpoint count, DPRAM size, maximum size and bank count per endpoint. CFGOK is only set when the configuration fits. Cores that leave these at zero keep the old at90usb162 limits. - 16 bit byte count (UEBCHX) for the 256 byte endpoints of the xxU4. - VBUS pad: AVR_IOCTL_USB_VBUS drives USBSTA.VBUS and VBUSTI when OTGPADE is set, a part with a VBUS pad only attaches with VBUS present. - USB_GEN and USB_COM are level triggered: enabling an interrupt whose flag is already set fires it. - Start of frame every 1ms after a bus reset, with frame number. - UERST, EPEN, RSTDT, UENUM and read-modify-write of STALLRQ/STALLRQC behave as in the datasheets, UDINT/USBINT bits are clear only. - Host ioctls NAK while detached or on unconfigured endpoints, refuse endpoints the part does not have, truncate oversized packets and report STALL on the endpoint that was accessed.
Move the xxU2 and xxU4 cores to sim_megaxu2.h and sim_megaxu4.h templates, the parts of a family only differ in memory sizes: - at90usb82, at90usb162, atmega8u2, atmega16u2, atmega32u2: 5 endpoints, 176 bytes of DPRAM. - atmega16u4, atmega32u4: 7 endpoints, 832 bytes of DPRAM, 256 byte EP1, VBUS pad. The at90usb162 now honours PRR0.PRSPI. avr/iousb82.h lacks the signature bytes, sim_usb82.c provides them.
A bare metal device firmware probes endpoint configurations, enumerates and echoes bulk data. The host side checks attach/VBUS handling, control transfers, STALL, the CFGOK results and the echo, plus each core's memory sizes and signature.
The %.axf rule only depends on its .c file, so editing the shared firmware header left stale images behind.
Intro.html gets a USB section: which parts are simulated, how the program using libsimavr acts as the host through the AVR_IOCTL_USB_* calls, a short enumeration fragment, what is not simulated, and the state of the usb-vhci bridge in examples/extra_board_usb. avr_usb.h documents each IOCTL's parameter, return values and the bulk IN zero length quirk, which was only discoverable by reading avr_usb.c.
The host side could write the data stage of a control write before the firmware had read the SETUP packet, overwriting it in the bank. A real controller NAKs the OUT token until RXSTPI is cleared. Hosts that pause between transactions never saw this; one that sends the data stage straight after the SETUP (the USB/IP part) got a confused firmware and a stalled status stage.
A bulk IN read with no data returned success with no bytes instead of
NAK, a workaround for the vhci bridge ("Linux stops polling the data
ep if we send naks"). A real device NAKs, and a host that maps the IOCTLs
onto URBs (USB/IP) needs the NAK: an empty success completes the URB
with zero bytes, and the host resubmits in a busy loop.
The workaround moves to vhci_usb.c, which gives a bulk IN NAK back to
the kernel as an empty transfer, so the vhci bridge behaves as before.
Intro.html no longer documents the quirk.
examples/parts/usbip_usb.c turns the simulated USB device into a USB/IP server (protocol 1.1.1, TCP port 3240): it answers OP_REQ_DEVLIST and OP_REQ_IMPORT and maps USBIP_CMD_SUBMIT/UNLINK onto the AVR_IOCTL_USB_* calls. A client such as Linux's mainline vhci-hcd driver and usbip tool attaches it to the host's USB stack, an alternative to vhci_usb.c that needs no out-of-tree kernel module. It runs in the simulation thread from a 100us cycle timer: the socket is polled, requests are parsed, and pending URBs are retried until the firmware stops NAKing them, oldest first on each pipe. Like usbip-host on a real server, it enumerates the device when it attaches, to list it and to learn its endpoint sizes, and it resets and readdresses it when it is imported. board_usbip runs extra_board_usb's CDC loopback firmware, or any .hex or .elf given on the command line, with the part, clock, port and reset vector as options. It is left out of the Windows build like simduino, as it uses mmap and sockets. usbip_check.py drives the server like vhci-hcd and cdc-acm would, for testing without a kernel. Intro.html describes both bridges, and drops the bulk IN quirk removed in the previous commit.
Use the SO_NOSIGPIPE socket option there instead, so that a client going away doesn't kill the simulator with SIGPIPE.
Use sim_network.h, as the gdb server does, for the Winsock headers, WSAStartup and the send/recv casts. Sockets stay blocking and are only accepted or read from once select() says they are ready, which avoids poll(), fcntl(O_NONBLOCK) and EAGAIN, none of which Winsock has in the same form. closesocket() and WSAGetLastError() are used on Windows, and SO_REUSEADDR is only set elsewhere, as on Windows it would let another program take the port.
USB/IP carries Linux errno values on the wire. The host's errno.h was used instead, which only agrees on Linux: on Windows and macOS ECONNRESET is not 104, so an unlinked URB was reported with a code the client doesn't recognise.
Listening on all interfaces let anyone on the network attach the simulated device, and makes the Windows firewall ask about it. A NULL address now means 127.0.0.1; "0.0.0.0" still listens everywhere.
simusbip kept the flash in a file through mmap, which Windows doesn't have. Persistence is now opt-in with -F file: the file is read when the core starts, the firmware is loaded over it, and it is written back when simusbip stops, Ctrl-C included. -a sets the listening address. usbip_usb.o and board_usbip are no longer left out of the Windows build; Makefile.common already links ws2_32 there. The README covers attaching the device with usbip-win2. Checked with a mingw-w64 build run under Wine, with usbip_check.py as the client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a USB/IP bridge for the USB device model, as an alternative to
examples/parts/vhci_usb.c, which needs the unmaintained out-of-tree usb-vhci kernel module.It depends on #603, so please merge that first; the first five commits here are that PR's. The new ones are the last eight.
The first two fix the USB model, and only matter to a host that drives it the way a real one does:
vhci_usb.c, which turns a bulk IN NAK into an empty transfer itself, so the vhci bridge behaves as before. I couldn't linkvhci_usb.chere, but it compiles against the libusb_vhci 0.6 and vhci-hcd 1.14 headers thatexamples/vhciuses.examples/parts/usbip_usb.cmakes the simulated device a USB/IP server (protocol 1.1.1, TCP port 3240). It answers the device list and import requests, then mapsUSBIP_CMD_SUBMIT/UNLINKonto the existingAVR_IOCTL_USB_*calls. A USB/IP client then attaches the device to the host's real USB stack:usbip attach -r 127.0.0.1 -b 1-1)The part runs in the simulation thread from a cycle timer, so nothing calls into the model from another thread. Each tick it polls the socket and retries the URBs the firmware is still NAKing, oldest first on each endpoint. Like usbip-host on a real server, it enumerates the device itself when the firmware connects, to list it and to learn its endpoint sizes.
examples/board_usbipruns extra_board_usb's CDC loopback firmware with it, or any .hex or .elf given on the command line. It has options for the part, clock, listening address and port, the reset vector, and flash kept in a file between runs.usbip_check.pyexercises the server without a kernel. Apart from the bulk IN workaround above,vhci_usb.candextra_board_usbare unchanged.Testing:
MSG_NOSIGNAL) is in, but I haven't been able to build there.usbip_check.pypasses against both the Linux and Windows builds, and the existing tests pass.Not supported: isochronous transfers, and more than one client at a time. When the firmware disconnects from the bus, for example a bootloader starting the application, the client has to attach again.