Skip to content

usb: device_next: class: gs_usb: complete echo handling for failed CAN TX - #286

Open
eribradl wants to merge 3 commits into
CANnectivity:mainfrom
eribradl:gs-usb-echo-followup-268
Open

eribradl wants to merge 3 commits into
CANnectivity:mainfrom
eribradl:gs-usb-echo-followup-268

Conversation

@eribradl

Copy link
Copy Markdown
Contributor

This completes #268, which echoes host frames that fail in the CAN TX callback. Two cases were left open:

  • Frames rejected by can_send(), e.g. with -ENETUNREACH while bus-off, are still freed without an echo. Each one leaks a TX context in the Linux gs_usb driver, so with traffic queued, host TX stops at the first bus-off and stays stopped. These frames are now completed through gs_usb_can_tx_callback().
  • Frames aborted by can_stop() on a channel reset are now echoed, although the host has already released their echo IDs and may have reused them. The TX callback now drops these frames once the channel is stopped.

The host tests gain support for host frames, plus two tests. In one, the fake CAN controller goes bus-off with frames queued. In the other, a frame stays pending until the channel is reset. Both tests fail on main.

Testing:

  • PyUSB host tests on native_sim, at full speed and high speed: 13 of 13 pass. On main, the two new tests fail.
  • DSD TECH SH-C31A with Linux gs_usb, CAN FD at 1 Mbit/s / 5 Mbit/s, repeated bus-off under full TX load: main stopped transmitting after the first bus-off (2720 frames sent, 2700 echoed). With this change, 43295 of 43295 frames were echoed through 11 bus-offs.
  • Same adapter, channel reset with frames pending: main sends 3 echoes per reset, one per FDCAN TX buffer, for echo IDs the host has already released. With this change, none.

Add helpers for sending and receiving host frames on the bulk
endpoints, and let the fake CAN controllers complete the frames sent to
them, so that host frame handling can be tested.

Sending a frame with CAN ID 0x7ff makes a fake CAN controller go
bus-off: that frame fails in the TX callback, and further frames fail in
can_send() until the channel is started again.

Assisted-by: Amp:claude-opus-5.5
Signed-off-by: Eric Bradley <eric@basepowercompany.com>
…N TX

Complete host frames rejected by can_send(), e.g. with -ENETUNREACH
while bus-off, through the TX callback, so that they are echoed like
frames that fail in the TX callback. Otherwise, each of them leaks a TX
context in the host driver, and host TX stops at the first bus-off.

Do not echo frames aborted by a channel reset. The host releases all TX
contexts when it resets the channel, and may already have reused their
echo IDs.

Assisted-by: Amp:claude-opus-5.5
Signed-off-by: Eric Bradley <eric@basepowercompany.com>
Test that host frames are echoed when they fail in the TX callback or in
can_send(), here because the CAN controller goes bus-off, and that
frames aborted by a channel reset are not echoed.

A frame sent with CAN ID 0x7fe now stays pending on a fake CAN
controller until the controller is stopped.

Assisted-by: Amp:claude-opus-5.5
Signed-off-by: Eric Bradley <eric@basepowercompany.com>
@henrikbrixandersen henrikbrixandersen self-assigned this Nov 21, 2026
@AlekseyMamontov

Copy link
Copy Markdown

Hi @eribradl,

If I understand your point correctly, the patch attached here might be relevant: it makes it possible to enable the echo for frames that could not be sent (CONFIG_USBD_GS_USB_TX_DROP_ECHO, only available together with CONFIG_USBD_GS_USB_TX_NONBLOCKING, both default to n, so the default behavior stays unchanged).

It also addresses a different case from the bus-off one: a channel stuck in error-passive (no ACK on the bus), where can_send(..., K_FOREVER, ...) blocks the shared TX thread and stalls TX on all other channels (#217). With the option enabled, the thread waits for a free mailbox only for CONFIG_USBD_GS_USB_TX_TIMEOUT_MS, and a CAN_ERR_TX_TIMEOUT error frame is sent to the host for each dropped frame.

#217

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.

3 participants