Skip to content

usbip: number endpoints per direction to keep addresses valid - #139

Merged
alexpevzner merged 4 commits into
OpenPrinting:masterfrom
atharrva01:fix/usbip-descriptors
Nov 26, 2026
Merged

alexpevzner merged 4 commits into
OpenPrinting:masterfrom
atharrva01:fix/usbip-descriptors

Conversation

@atharrva01

Copy link
Copy Markdown
Contributor

While testing the HP LaserJet MFP M426fdn model over USB/IP, I found that its last two interfaces (HP's "IPP Printer", 255/9/1) get invalid endpoint addresses: 0x90, 0x11 and 0x92.

The cause is that usbip numbers endpoints with one counter shared by IN and OUT. An endpoint number only has 4 bits (1–15), and this model has 18 endpoints (11 IN, 7 OUT), so the counter runs past 15. The real device is fine because USB numbers IN and OUT separately.

The host can only send a 4-bit endpoint number, so URBs meant for interface 6 arrive as ep 1/2 and land in HP SCAN's queues. Interface 5's IN endpoint can't be reached at all. NewDevice didn't catch it because it checked a different limit (32 total).

What I changed:

  • NewDevice gives each endpoint a number once, with separate IN and OUT counters, and stores it on the Endpoint. The descriptor and the routing both use that number, so they can't disagree again.
  • Routing uses two fixed [16]*Endpoint tables (IN and OUT) through a small dev.endpoint(num, input) helper.
  • NewDevice now rejects more than 15 IN or 15 OUT endpoints, which is the real USB limit.

I added descriptor_test.go. It reads the configuration descriptor through the real Server.serve(), the way the kernel does on attach, then sends a URB to each advertised endpoint and checks that it lands on the right interface. It fails on master and passes with this change. I also updated the #133 tests to the new numbering. All usbip tests pass 3 runs in a row with -race.

Heads-up: #134's test sends bulk OUT to ep 2, which becomes ep 1 with this change. I'll update whichever PR merges second.

@atharrva01

Copy link
Copy Markdown
Contributor Author

@alexpevzner This PR makes usbip number endpoints separately for IN and OUT, like real devices, so printers with more than 15 endpoints get valid addresses and URBs reach the right interface.

@alexpevzner

Copy link
Copy Markdown
Member

With this change, 2 different endpoints with the opposite direction may get the same endpoint number

It breaks the logic around dev.endpointsTree. In the following assignment the second occurrence will simply replace the first one, which is obviously not correct:

        dev.endpointsTree[confno][iffno][altno][epno] = epp

@alexpevzner

Copy link
Copy Markdown
Member

Looks that the entire logic around dev.endpointsTree needs to be revisited.

I'd start from the more comprehensive test coverage to make sure we will not break something there...

@atharrva01

Copy link
Copy Markdown
Contributor Author

Hi @alexpevzner on the replacement concern, epno in that line is the endpoint's position inside alt.Endpoints (from range alt.Endpoints), not its endpoint number. So an IN and an OUT endpoint that share number 1 (0x81 / 0x01) always land in different slots of the tree. I renamed it to i and added a comment above the tree so that's clearer.

As you suggested, I started with more test coverage. endpoints_test.go runs 5 layouts (the HP M426fdn, Kyocera and Xerox model shapes, a simple device, and a two-config device) and checks:

  • the tree has one distinct Endpoint per descriptor entry
  • the lookup helpers return the right entries
  • the configuration descriptors on the wire are well-formed
  • every advertised endpoint routes to its tree entry and unlinks from there, including IN/OUT pairs that share a number

The tests caught two existing bugs, which I fixed here too:

  • GET_DESCRIPTOR(CONFIGURATION) with index == number of configs panicked the server (> instead of >=)
  • bNumEndpoints used the max over all alt settings instead of the alt's own count (Kyocera interface 2 alt 0 claimed 2 endpoints and listed none)

I also merged master and updated the #134 tests for the new numbering. All usbip tests pass with -race.

@alexpevzner

Copy link
Copy Markdown
Member

I've merged it and refactored a little bit after merge

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.

2 participants