This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit aee12ada6a8ab28bc9581a93ac5985cbf0f23430 Author: wangjinjing1 <[email protected]> AuthorDate: Thu Oct 22 22:49:47 2026 +0800 drivers/vhost/vhost-can: carry the CAN FD BRS flag over the wire The VirtIO CAN wire format only encodes the FD flag, so the Bit Rate Switch bit is lost in both directions: frames sent with CANFD_BRS arrive as plain FD frames, and received FD frames never report BRS. The frame type is also guessed from the frame length, so an 8-byte FD frame goes out as a classic one. Add VIRTIO_CAN_FLAGS_BRS (0x1000) to the wire flags. Rebuild CANFD_FDF | CANFD_BRS from the wire flags when decoding, and take the FD and BRS bits from canfd_frame.flags when encoding. Classic frames still decode with flags cleared. Signed-off-by: wangjinjing1 <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- drivers/vhost/vhost-can.c | 70 ++++++++++++++++++++++++++++----------- include/nuttx/virtio/virtio-can.h | 1 + 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/drivers/vhost/vhost-can.c b/drivers/vhost/vhost-can.c index 30535555bf7..e93a1ce7123 100644 --- a/drivers/vhost/vhost-can.c +++ b/drivers/vhost/vhost-can.c @@ -324,7 +324,25 @@ static size_t vhost_can_wire2frame(FAR struct vhost_can_priv_s *priv, cfd->can_id = canid; cfd->len = len; - cfd->flags = 0; + + /* Rebuild canfd_frame.flags from the wire flags. An FD frame carries + * FDF plus any BRS/ESI the sender set; a classic frame leaves flags 0. + */ + + if (flags & VIRTIO_CAN_FLAGS_FD) + { + cfd->flags = CANFD_FDF; + + if (flags & VIRTIO_CAN_FLAGS_BRS) + { + cfd->flags |= CANFD_BRS; + } + } + else + { + cfd->flags = 0; + } + cfd->__res0 = 0; cfd->__res1 = 0; *frame = cfd; @@ -340,9 +358,24 @@ static size_t vhost_can_wire2frame(FAR struct vhost_can_priv_s *priv, ****************************************************************************/ static void vhost_can_rxhdr(FAR struct virtio_can_rx_s *rx, uint32_t canid, - uint8_t dlen) + uint8_t dlen, uint8_t fdflags) { - uint32_t flags = dlen > CAN_MAX_DLEN ? VIRTIO_CAN_FLAGS_FD : 0; + uint32_t flags = 0; + + /* The CANFD_FDF flag is the authoritative FD indicator; deriving it from + * the sdu length would misclassify an 8-byte FD frame as classic. BRS + * only applies to an FD frame. + */ + + if (fdflags & CANFD_FDF) + { + flags |= VIRTIO_CAN_FLAGS_FD; + + if (fdflags & CANFD_BRS) + { + flags |= VIRTIO_CAN_FLAGS_BRS; + } + } if (canid & CAN_EFF_FLAG) { @@ -375,6 +408,7 @@ static void vhost_can_rxhdr(FAR struct virtio_can_rx_s *rx, uint32_t canid, static size_t vhost_can_frame2wire(FAR struct virtqueue_buf *vb, size_t n, FAR const void *data, size_t len) { + FAR const struct canfd_frame *cfd = data; struct virtio_can_rx_s rx; FAR const uint8_t *sdu; size_t total = 0; @@ -392,25 +426,20 @@ static size_t vhost_can_frame2wire(FAR struct virtqueue_buf *vb, size_t n, return 0; } - if (len >= sizeof(struct canfd_frame)) - { - FAR const struct canfd_frame *cfd = data; + canid = cfd->can_id; + sdu = cfd->data; - canid = cfd->can_id; - dlen = MIN(cfd->len, CANFD_MAX_DLEN); - sdu = cfd->data; + if (cfd->flags & CANFD_FDF) + { + dlen = MIN(cfd->len, CANFD_MAX_DLEN); } else { - FAR const struct can_frame *cf = data; - - canid = cf->can_id; - dlen = MIN(cf->can_dlc, CAN_MAX_DLEN); - sdu = cf->data; + dlen = MIN(cfd->len, CAN_MAX_DLEN); } dlen = MIN(dlen, total - VHOST_CAN_HDRLEN); - vhost_can_rxhdr(&rx, canid, dlen); + vhost_can_rxhdr(&rx, canid, dlen, cfd->flags); vhost_can_stream_write(vb, n, 0, &rx, VHOST_CAN_HDRLEN); vhost_can_stream_write(vb, n, VHOST_CAN_HDRLEN, sdu, dlen); return VIRTIO_CAN_RX_SIZE(dlen); @@ -754,12 +783,15 @@ static void vhost_can_rxq(FAR void *arg) * chain is consumed with length 0, dropping the frame. */ + frame->flags = nread >= sizeof(buf) ? frame->flags : 0; if (frame != &buf) { - dlen = MIN(frame->len, nread >= (ssize_t)sizeof(buf) ? - CANFD_MAX_DLEN : CAN_MAX_DLEN); - vhost_can_rxhdr(priv->rxvb[0].buf, frame->can_id, dlen); - used = VIRTIO_CAN_RX_SIZE(dlen); + dlen = MIN(frame->len, + (frame->flags & CANFD_FDF) ? CANFD_MAX_DLEN : + CAN_MAX_DLEN); + vhost_can_rxhdr(priv->rxvb[0].buf, frame->can_id, dlen, + frame->flags); + used = VIRTIO_CAN_RX_SIZE(dlen); } else { diff --git a/include/nuttx/virtio/virtio-can.h b/include/nuttx/virtio/virtio-can.h index d42c8087b36..ded3f629925 100644 --- a/include/nuttx/virtio/virtio-can.h +++ b/include/nuttx/virtio/virtio-can.h @@ -52,6 +52,7 @@ #define VIRTIO_CAN_FLAGS_EXTENDED 0x8000 #define VIRTIO_CAN_FLAGS_FD 0x4000 #define VIRTIO_CAN_FLAGS_RTR 0x2000 +#define VIRTIO_CAN_FLAGS_BRS 0x1000 #define VIRTIO_CAN_IFNAME_SIZE 16
