Skip to contentSkip to conversation

hw/usb: Add a usbredir server transport and AST1030 UDC support

PATCH Superseded Jamin Lin 3 participants 24 messages Sep 22 – Sep 30

Subsystems (2)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

cover hw/usb: Add a usbredir server transport and AST1030 UDC support

This series redirects a USB device emulated in one QEMU instance to a USB host controller emulated in another one, over the usbredir protocol, and uses it to bring up the ASPEED AST1030 USB Device Controller (UDC).

It is the second step of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:

1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB host (EHCI) controllers, so that work targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]
https://lore.kernel.org/qemu-devel/[email protected]/

2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The UDC gadget is redirected out of the guest with libusbredir and attached to another QEMU that runs a USB host (a VMM, or an AST2600 / AST2700 guest). [this series]

3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]

This series implements goal 2.

The transport
=============

usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in another one.

The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.

    remote QEMU: guest driver -> EHCI/XHCI
             |                     ^
   USBPacket |                     | USBPacket
             v                     |
    "usb-redir" (the client)
             |                     ^
    usbredir |   chardev socket    | usbredir
             v                     |
    usb-redir-server (the server, this series)
             |                     ^
   USBPacket |                     | USBPacket
             v                     |
    any USBDevice, "-device <dev>,bus=<id>.0"

"usb-redir" (hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacket

usb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to read

A USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.

usbredir carries one device, not a bus. A hub cannot be exported: the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.

AST1030 gadget to an AST2700 host
=================================

        QEMU #1  -M ast1030-evb              QEMU #2  -M ast2700a2-evb
  +-----------------------------+      +-----------------------------+
  | Zephyr   usb_dc_aspeed      |      | Linux    ehci-platform      |
  +--------------+--------------+      +--------------+--------------+
                 | MMIO                               | MMIO
  +--------------v--------------+      +--------------v--------------+
  | aspeed.udc                  |      | EHCI3        (usb-bus.3)    |
  +--------------+--------------+      +--------------+--------------+
                 | USBPacket                          | USBPacket
  +--------------v--------------+      +--------------v--------------+
  | aspeed.udc-gadget           |      | usb-redir                   |
  +--------------+--------------+      +--------------+--------------+
                 | USBPacket                          |
  +--------------v--------------+                     |
  | usb-redir-server            |                     |
  +--------------+--------------+                     |
                 |                                    |
                 +=========== unix socket ============+

usbredir protocol

Testing
=======

1. AST1030 CDC-ACM gadget, enumerated by an AST2700 host
--------------------------------------------------------

The device side owns the socket and has to be started first.

  $ qemu-system-arm -M ast1030-evb -nographic \
        -kernel zephyr.bin \
        -chardev socket,id=u,path=/tmp/usbredir.sock,server=on,wait=off \
        -device usb-redir-server,id=udcredir,chardev=u \
        -device aspeed.udc-gadget,bus=udcredir.0,udc=/machine/soc/udc
  uart:~$ usb enable
  uart:~$ usb print 1
  $ qemu-system-aarch64 -M ast2700a2-evb -nographic \
        -drive file=image-bmc,if=mtd,format=raw -snapshot \
        -net nic -net user \
        -chardev socket,id=r,path=/tmp/usbredir.sock \
        -device usb-redir,chardev=r,bus=usb-bus.3
  root@ast2700-default:~# lsusb
  Bus 001 Device 001: ID 1d6b:0002 Linux 6.18.36 ehci_hcd EHCI Host Controller
  Bus 001 Device 002: ID 2245:1030 ZEPHYR Zephyr DFU sample

Data was checked:

2700 -> 1030 echo '1234567890' > /dev/ttyACM0

                 1030 console: Print Data: 0x31 0x32 ... 0x30

2. A plain usb-storage exported the same way
--------------------------------------------

Any USBDevice can be exported, not just the UDC gadget. The machine on the exporting side is only a container for usb-redir-server, so it does not have to run anything.

  $ qemu-system-arm -M ast2600-evb -nographic -serial null \
        -chardev socket,id=u,path=/tmp/storage.sock,server=on,wait=off \
        -device usb-redir-server,id=keyredir,chardev=u \
        -drive id=usbdisk,if=none,file=disk.img,format=raw -snapshot \
        -device usb-storage,bus=keyredir.0,id=mystorage,drive=usbdisk
  $ qemu-system-aarch64 -M ast2700a2-evb -nographic \
        -drive file=image-bmc,if=mtd,format=raw -snapshot \
        -net nic -net user \
        -chardev socket,id=r,path=/tmp/storage.sock \
        -device usb-redir,chardev=r,bus=usb-bus.3
  root@ast2700-default:~# lsusb
  Bus 001 Device 001: ID 1d6b:0002 Linux 6.18.36 ehci_hcd EHCI Host Controller
  Bus 001 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVE
  root@ast2700-default:~# mount /dev/sda /mnt/usb
  MOUNTED

A 4 MiB file was written, unmounted and remounted, and the sha256 matched.

v1
1. Add a usbredir server transport
1. Add a usbredir server device
2. Announce the exported device
3. Implement control transfers
4. Implement bulk and interrupt transfers
5. Stream interrupt IN endpoints
2. ASPEED UDC
1. Make the SETUP buffer byte addressable
2. Support single-stage IN transfers
3. Clear EP_POOL_ACK when no endpoint ack is left
4. Mask the reserved bits on register writes
5. Use the endpoint max packet size for OUT transfers
6. Test the usbredir server transport
7. Add the USB device controller for AST1030

Jamin Lin (19):
hw/usb/bus: Let a bus opt out of automatic hub insertion hw/usb/redirect-server: Add a usbredir server device hw/usb/redirect-server: Connect usbredirparser to a chardev hw/usb/redirect-server: Announce the exported device hw/usb/redirect-server: Implement control transfers hw/usb/redirect-server: Implement bulk and interrupt transfers hw/usb/redirect-server: Stream interrupt IN endpoints hw/arm/aspeed: Enable the usbredir server transport hw/usb/aspeed-udc: Make the SETUP buffer byte addressable hw/usb/aspeed-udc: Mask the reserved bits on register writes hw/usb/aspeed-udc: Clear EP_POOL_ACK when no endpoint ack is left hw/usb/aspeed-udc: Rename the transfer helpers after their DMA mode hw/usb/aspeed-udc: Store the EP_DMA_STS write first hw/usb/aspeed-udc: Pick the EP_DMA_STS path by direction hw/usb/aspeed-udc: Support single-stage IN transfers hw/usb/aspeed-udc: Use the endpoint max packet size for OUT transfers hw/usb/aspeed-udc: Add trace events for the transfer paths hw/arm/aspeed_ast10x0: Add the USB device controller tests/functional/arm/test_aspeed_ast1030: Test the usbredir server
transport

 include/hw/arm/aspeed_soc.h                 |    1 +
 include/hw/usb/aspeed-udc.h                 |   13 +-
 include/hw/usb/redirect-server.h            |  116 ++
 include/hw/usb/usb.h                        |    1 +
 hw/arm/aspeed.c                             |    6 +
 hw/arm/aspeed_ast10x0.c                     |   17 +-
 hw/usb/aspeed-udc.c                         |  290 +++-
 hw/usb/bus.c                                |    3 +-
 hw/usb/redirect-server.c                    | 1604 +++++++++++++++++++
 hw/usb/meson.build                          |    3 +-
 hw/usb/trace-events                         |   36 +
 tests/functional/arm/meson.build            |    1 +
 tests/functional/arm/test_aspeed_ast1030.py |   52 +
 13 files changed, 2088 insertions(+), 55 deletions(-)
 create mode 100644 include/hw/usb/redirect-server.h
 create mode 100644 hw/usb/redirect-server.c
Signature · 1 line
2.53.0
author
#12 lore raw

PATCH 12/19 hw/usb/aspeed-udc: Rename the transfer helpers after their DMA mode

The controller can run an endpoint in descriptor list mode or in single stage mode. Put the mode in the helper names. That makes the code easier to read.

No functional change.

---
 hw/usb/aspeed-udc.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
hw/usb/aspeed-udc.c +15 -15
index bdbd1a78b8..b43f077c2d 100644
@@ -459,8 +459,8 @@ static bool aspeed_udc_ep_copy_to_pkt(AspeedUDCState *s, int ep, uint32_t addr,
  * This function raises the endpoint ACK by itself when the ring becomes empty
  * or when a descriptor asks for an interrupt.
  */
-static AspeedUDCXferResult aspeed_udc_ep_xfer_in(AspeedUDCState *s, int ep,
-                                                 USBPacket *p)
+static AspeedUDCXferResult aspeed_udc_ep_xfer_in_desc(AspeedUDCState *s,
+                                                      int ep, USBPacket *p)
 {
     QEMUIOVector *pktiov = p->combined ? &p->combined->iov : &p->iov;
     AspeedUDCEP *e = &s->ep[ep];
@@ -568,15 +568,15 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in(AspeedUDCState *s, int ep,
 
 /*
  * OUT transfer: receive data from the host by copying its OUT packet into the
- * buffer the guest gadget driver set up (single-stage mode).
+ * buffer the guest gadget driver set up.
  *
  * A host packet can be bigger than one buffer, so we copy at most PKT_SIZE
  * bytes per call, continuing from where the last call stopped
  * (p->actual_length). The caller keeps the packet parked until it is fully
  * copied.
  */
-static AspeedUDCXferResult aspeed_udc_ep_xfer_out(AspeedUDCState *s, int ep,
-                                                  USBPacket *p)
+static AspeedUDCXferResult aspeed_udc_ep_xfer_out_single(AspeedUDCState *s,
+                                                         int ep, USBPacket *p)
 {
     AspeedUDCEP *e = &s->ep[ep];
     uint32_t chunk = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, PKT_SIZE);
@@ -619,7 +619,7 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_out(AspeedUDCState *s, int ep,
  * If the request needs more data than was queued, keep it parked and wait for
  * the next kick.
  */
-static void aspeed_udc_ep_in_kick(AspeedUDCState *s, int ep, uint32_t val)
+static void aspeed_udc_ep_in_kick_desc(AspeedUDCState *s, int ep, uint32_t val)
 {
     AspeedUDCEP *e = &s->ep[ep];
     uint32_t cur_rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
@@ -649,7 +649,7 @@ static void aspeed_udc_ep_in_kick(AspeedUDCState *s, int ep, uint32_t val)
         return;
     }
 
-    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    switch (aspeed_udc_ep_xfer_in_desc(s, ep, p)) {
     case ASPEED_UDC_XFER_DONE:
         e->pkt = NULL;
         p->status = USB_RET_SUCCESS;
@@ -671,7 +671,7 @@ static void aspeed_udc_ep_in_kick(AspeedUDCState *s, int ep, uint32_t val)
  * buffer before), copy its data into the buffer now and finish it. If the
  * packet has more data than fits, keep it parked and wait for the next buffer.
  */
-static void aspeed_udc_ep_out_kick(AspeedUDCState *s, int ep)
+static void aspeed_udc_ep_out_kick_single(AspeedUDCState *s, int ep)
 {
     AspeedUDCEP *e = &s->ep[ep];
     USBPacket *p = e->pkt;
@@ -681,7 +681,7 @@ static void aspeed_udc_ep_out_kick(AspeedUDCState *s, int ep)
         return;
     }
 
-    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    switch (aspeed_udc_ep_xfer_out_single(s, ep, p)) {
     case ASPEED_UDC_XFER_DONE:
         e->pkt = NULL;
         p->status = USB_RET_SUCCESS;
@@ -727,11 +727,11 @@ static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
         val &= 0x77ffffff;
         if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
             /* IN, descriptor-list mode */
-            aspeed_udc_ep_in_kick(s, e->index, val);
+            aspeed_udc_ep_in_kick_desc(s, e->index, val);
         } else {
             /* OUT, single-stage mode */
             e->regs[reg] = val;
-            aspeed_udc_ep_out_kick(s, e->index);
+            aspeed_udc_ep_out_kick_single(s, e->index);
         }
         break;
     default:
@@ -895,7 +895,7 @@ static void aspeed_udc_ep_data_in(AspeedUDCState *s, int ep, USBPacket *p)
         /*
          * No IN data is queued yet. Save the packet and return ASYNC
          * instead of NAK. A NAK would make the host retry slowly.
-         * aspeed_udc_ep_in_kick() serves and completes this packet later,
+         * aspeed_udc_ep_in_kick_desc() serves and completes this packet later,
          * once the guest gadget driver queues descriptors.
          */
         e->pkt = p;
@@ -903,7 +903,7 @@ static void aspeed_udc_ep_data_in(AspeedUDCState *s, int ep, USBPacket *p)
         return;
     }
 
-    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    switch (aspeed_udc_ep_xfer_in_desc(s, ep, p)) {
     case ASPEED_UDC_XFER_DONE:
         p->status = USB_RET_SUCCESS;
         break;
@@ -931,7 +931,7 @@ static void aspeed_udc_ep_data_out(AspeedUDCState *s, int ep, USBPacket *p)
          * No OUT buffer is ready yet. Save the packet and return ASYNC
          * instead of NAK. Writing now could use an old buffer address and
          * lose the data (for example a mass-storage CBW). A NAK would make
-         * the host retry slowly. aspeed_udc_ep_out_kick() delivers this
+         * the host retry slowly. aspeed_udc_ep_out_kick_single() delivers this
          * packet later, once the guest gadget driver sets up a buffer.
          */
         e->pkt = p;
@@ -939,7 +939,7 @@ static void aspeed_udc_ep_data_out(AspeedUDCState *s, int ep, USBPacket *p)
         return;
     }
 
-    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    switch (aspeed_udc_ep_xfer_out_single(s, ep, p)) {
     case ASPEED_UDC_XFER_DONE:
         p->status = USB_RET_SUCCESS;
         break;
Signature · 1 line
2.53.0
author
#19 lore raw

PATCH 19/19 tests/functional/arm/test_aspeed_ast1030: Test the usbredir server transport

In reply to Jamin Lin

Export the AST1030 UDC gadget with usb-redir-server and import it on an AST2600 with usb-redir, then check that the AST2600 enumerates it. That covers the announce, control and enumeration paths of the transport, and the UDC model behind it.

The two machines have to come from the same QEMU binary, because a functional test gets only one. ast1030-evb and ast2600-evb are both in qemu-system-arm.

---
 tests/functional/arm/meson.build            |  1 +
 tests/functional/arm/test_aspeed_ast1030.py | 52 +++++++++++++++++++++
 2 files changed, 53 insertions(+)
tests/functional/arm/meson.build +1 -0
index 61b00932db..cfbd376b0d 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 test_arm_timeouts = {
+  'aspeed_ast1030' : 720,
   'aspeed_palmetto' : 120,
   'aspeed_romulus' : 120,
   'aspeed_witherspoon' : 120,
tests/functional/arm/test_aspeed_ast1030.py +52 -0

64 lines not on this page · raw message

Signature · 1 line
2.53.0
author
#3 lore raw

PATCH 3/19 hw/usb/redirect-server: Connect usbredirparser to a chardev

In reply to Jamin Lin

Create a usbredirparser when the chardev opens. Feed it from the chardev read handler. Destroy it when the chardev closes.

The side that exports a device sets usbredirparser_fl_usb_host. This side drives the device, so it is the USB host for it. redirect.c is the other side and does not set the flag. The library uses it to decide which direction a packet may travel.

The chardev handling follows hw/usb/redirect.c: take no data before the parser exists, do not write to a closed backend, do not re-enter the writer, and let a close that is still queued finish before a new connection opens.

---
 include/hw/usb/redirect-server.h |  15 ++
 hw/usb/redirect-server.c         | 268 +++++++++++++++++++++++++++++++
 hw/usb/trace-events              |   6 +
 3 files changed, 289 insertions(+)
include/hw/usb/redirect-server.h +15 -0
index e81d4e0608..2af881bda9 100644
@@ -11,8 +11,11 @@
 
 #include "hw/core/sysbus.h"
 #include "hw/usb/usb.h"
+#include "chardev/char-fe.h"
 #include "qom/object.h"
 
+#include <usbredirparser.h>
+
 #define TYPE_USB_REDIR_SERVER "usb-redir-server"
 OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)
 
@@ -22,6 +25,18 @@ struct USBRedirServer {
     /* USB bus */
     USBBus bus;
     USBPort port;
+
+    /* Properties */
+    CharFrontend cs;
+
+    /* usbredir over the chardev */
+    struct usbredirparser *parser;
+    QEMUBH *chardev_close_bh;
+    const uint8_t *read_buf;
+    int read_buf_size;
+    bool in_write;
+    guint watch;
+    bool host_connected;
 };
 
 #endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +268 -0

306 lines not on this page · raw message

hw/usb/trace-events +6 -0
index 80ead23358..d141661673 100644
@@ -393,3 +393,9 @@ aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx) "ep
 aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t iov) "ep %u, rptr %u, wptr %u, iov %u"
 aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail, uint32_t iov) "ep %u, wptr %u, avail %u, iov %u"
 aspeed_udc_ep_ack(unsigned ep) "ep %u"
+
+# redirect-server.c
+usbredir_server_chardev_open(void) "chardev opened"
+usbredir_server_chardev_close(void) "chardev closed"
+usbredir_server_write_recursion(void) "recursive write, leaving it queued"
+usbredir_server_log(const char *msg) "%s"
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/19 hw/usb/bus: Let a bus opt out of automatic hub insertion

In reply to Jamin Lin

usb_claim_port() inserts a usb-hub when a device is plugged in and only one port is still free, so that more devices can follow. That is what a host controller with root ports wants.

It is wrong for a transport that carries exactly one device. The hub takes the last port, and the device that was being plugged in ends up behind the hub instead of on the port the controller watches.

Add no_auto_hub for such a bus. It keeps the ports it registered, and -device fails with "no free ports" once they are used up.

---
 include/hw/usb/usb.h | 1 +
 hw/usb/bus.c         | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/hw/usb/usb.h +1 -0
index cbd4711933..ab96812adb 100644
@@ -484,6 +484,7 @@ struct USBBus {
     int busnr;
     int nfree;
     int nused;
+    bool no_auto_hub;
     QTAILQ_HEAD(, USBPort) free;
     QTAILQ_HEAD(, USBPort) used;
     QTAILQ_ENTRY(USBBus) next;
hw/usb/bus.c +2 -1
index 5cc5ffec33..8a24b1837a 100644
@@ -413,7 +413,8 @@ void usb_claim_port(USBDevice *dev, Error **errp)
             return;
         }
     } else {
-        if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
+        if (bus->nfree == 1 && !bus->no_auto_hub &&
+            strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
             /* Create a new hub and chain it on */
             hub = USB_DEVICE(qdev_try_new("usb-hub"));
             if (hub) {
Signature · 1 line
2.53.0
author
#16 lore raw

PATCH 16/19 hw/usb/aspeed-udc: Use the endpoint max packet size for OUT transfers

In reply to Jamin Lin

In single mode EP_DMA_STS.PKT_SIZE on an OUT endpoint is where the controller reports the length it received. It is not a buffer size. The model took the copy length from it, so a zero in that field meant no data was copied at all.

One maximum packet is the most that can arrive, so copy at most EP_CONFIG.MAX_PKT bytes per call instead. That field cannot hold 1024, so a value of 0 there means 1024 bytes. The IN path decoded that by hand; move it into one helper and use it from both paths.

Take the packet size from the combined packet when there is one, as the IN paths do. A combined packet holds more than p->iov says, so reading p->iov.size cut the transfer short.

---
 hw/usb/aspeed-udc.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
hw/usb/aspeed-udc.c +18 -14
index dcced1b8fb..09f5913aef 100644
@@ -416,6 +416,13 @@ static const MemoryRegionOps aspeed_udc_setup_ops = {
     },
 };
 
+static uint32_t aspeed_udc_ep_get_max_packet(AspeedUDCEP *e)
+{
+    uint32_t mps = FIELD_EX32(e->regs[R_EP_CONFIG], EP_CONFIG, MAX_PKT);
+
+    return mps ? mps : ASPEED_UDC_EP_MAXPKT;
+}
+
 /*
  * Copy len bytes from guest memory at addr into the IN packet, going through
  * a bounce buffer one buf-full at a time. Returns false on DMA failure.
@@ -464,10 +471,10 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_desc(AspeedUDCState *s,
 {
     QEMUIOVector *pktiov = p->combined ? &p->combined->iov : &p->iov;
     AspeedUDCEP *e = &s->ep[ep];
-    uint32_t mps = FIELD_EX32(e->regs[R_EP_CONFIG], EP_CONFIG, MAX_PKT);
     uint32_t wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR);
     uint32_t rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
     uint32_t desc_base = e->regs[R_EP_DMA_BUFF];
+    uint32_t mps = aspeed_udc_ep_get_max_packet(e);
     uint32_t desc_addr;
     uint32_t remaining;
     uint32_t desc_ctrl;
@@ -480,11 +487,6 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_desc(AspeedUDCState *s,
     bool done = false;
     bool ack = false;
 
-    if (mps == 0) {
-        /* a MAX_PKT field of 0 means the maximum packet size */
-        mps = ASPEED_UDC_EP_MAXPKT;
-    }
-
     trace_aspeed_udc_ep_data_in(ep, rptr, wptr, pktiov->size);
 
     /* walk the queued descriptors, filling the packet */
@@ -624,19 +626,21 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_single(AspeedUDCState *s,
  * OUT transfer: receive data from the host by copying its OUT packet into the
  * buffer the guest gadget driver set up.
  *
- * A host packet can be bigger than one buffer, so we copy at most PKT_SIZE
- * bytes per call, continuing from where the last call stopped
- * (p->actual_length). The caller keeps the packet parked until it is fully
- * copied.
+ * In single mode PKT_SIZE is where the controller reports the length it
+ * received, so it is not a buffer size. One maximum packet is the most that
+ * can arrive, so copy at most that much per call, continuing from where the
+ * last call stopped (p->actual_length). The caller keeps the packet parked
+ * until it is fully copied.
  */
 static AspeedUDCXferResult aspeed_udc_ep_xfer_out_single(AspeedUDCState *s,
                                                          int ep, USBPacket *p)
 {
+    QEMUIOVector *pktiov = p->combined ? &p->combined->iov : &p->iov;
     AspeedUDCEP *e = &s->ep[ep];
-    uint32_t chunk = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, PKT_SIZE);
-    uint32_t remaining = p->iov.size - (uint32_t)p->actual_length;
+    uint32_t remaining = pktiov->size > (uint32_t)p->actual_length ?
+                         pktiov->size - (uint32_t)p->actual_length : 0;
+    uint32_t len = MIN(remaining, aspeed_udc_ep_get_max_packet(e));
     uint32_t data_buf_addr = e->regs[R_EP_DMA_BUFF];
-    uint32_t len = MIN(remaining, chunk);
     g_autofree uint8_t *buf = g_malloc(len);
 
     if (data_buf_addr && len) {
@@ -659,7 +663,7 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_out_single(AspeedUDCState *s,
                                         PROC_STS, EP_DMA_CTRL_STS_RX_IDLE);
     aspeed_udc_raise_ep_ack(s, ep);
 
-    if ((uint32_t)p->actual_length >= p->iov.size) {
+    if ((uint32_t)p->actual_length >= pktiov->size) {
         return ASPEED_UDC_XFER_DONE;
     }
 
Signature · 1 line
2.53.0
author
#10 lore raw

PATCH 10/19 hw/usb/aspeed-udc: Mask the reserved bits on register writes

In reply to Jamin Lin

Writes to the interrupt registers were not masked, so a guest could set a reserved bit and read it back. The hardware returns 0 for those.

Mask each one to the bits the datasheet marks read/write:

  UDC_IER         17, 16, 8:6, 4:0
  UDC_ISR         8:6, 4:0
  UDC_EP_ACK_IER  3:0
  UDC_EP_NAK_IER  3:0
  UDC_EP_ACK_ISR  3:0
  UDC_EP_NAK_ISR  3:0

UDC_ISR bits 17 and 16 summarise the endpoint pool. They are read only, so a write cannot clear them.

---
 hw/usb/aspeed-udc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/usb/aspeed-udc.c +11 -1
index 355512822b..95fe823534 100644
@@ -319,14 +319,24 @@ static void aspeed_udc_write(void *opaque, hwaddr offset, uint64_t data,
         s->regs[R_UDC_FUNC_CTRL] = val;
         break;
     case R_UDC_IER:
+        val &= 0x000301df;
+        s->regs[reg] = val;
+        aspeed_udc_update_irq(s);
+        break;
+    case R_UDC_ISR:
+        val &= 0x000001df;
+        s->regs[reg] &= ~val;
+        aspeed_udc_update_irq(s);
+        break;
     case R_UDC_EP_ACK_IER:
     case R_UDC_EP_NAK_IER:
+        val &= 0x0000000f;
         s->regs[reg] = val;
         aspeed_udc_update_irq(s);
         break;
-    case R_UDC_ISR:
     case R_UDC_EP_ACK_ISR:
     case R_UDC_EP_NAK_ISR:
+        val &= 0x0000000f;
         s->regs[reg] &= ~val;
         aspeed_udc_update_irq(s);
         break;
Signature · 1 line
2.53.0
author
#15 lore raw

PATCH 15/19 hw/usb/aspeed-udc: Support single-stage IN transfers

In reply to Jamin Lin

The controller can send an IN endpoint's data from a descriptor ring or from a single buffer. Only the descriptor ring was modelled, so an IN endpoint in single stage mode sent nothing.

Add the path: one buffer at EP_DMA_BUFF, its length in PKT_SIZE, queued by setting the write pointer. How to tell whether anything is queued also depends on the mode. A descriptor ring is empty when its two pointers are equal. A single stage buffer is queued by the write pointer alone.

The buffer can hold more than the host asks for in one request, so keep an offset into it. The rest goes out when the host asks again, and the guest is told only once all of it has gone.

---
 include/hw/usb/aspeed-udc.h |   2 +
 hw/usb/aspeed-udc.c         | 108 ++++++++++++++++++++++++++++++++++--
 2 files changed, 106 insertions(+), 4 deletions(-)
include/hw/usb/aspeed-udc.h +2 -0
index 42916c0bd6..6665609501 100644
@@ -53,6 +53,8 @@ typedef struct AspeedUDCEP {
     USBPacket *pkt;
     /* bytes of the current IN descriptor already served */
     uint32_t desc_off;
+    /* bytes of the single-stage IN buffer already sent */
+    uint32_t single_buf_off;
 } AspeedUDCEP;
 
 struct AspeedUDCGadget {
hw/usb/aspeed-udc.c +104 -4

150 lines not on this page · raw message

Signature · 1 line
2.53.0
author
#4 lore raw

PATCH 4/19 hw/usb/redirect-server: Announce the exported device

In reply to Jamin Lin

Send device_connect when a device is attached and the host has greeted us, device_disconnect when it goes away, and handle the bus reset the host sends before enumerating.

Wait a short time before sending device_connect. Firmware often toggles the USB pull-up a few times while it starts its controller. If the host starts to enumerate while the device is off, we cannot answer its first request, and it only retries after a control timeout of several seconds.

device_connect carries no identity. The peer asks the device itself with GET_DESCRIPTOR as soon as it enumerates, so anything we put there would be made up. The speed is real: it comes from the device on our port.

---
 include/hw/usb/redirect-server.h |   2 +
 hw/usb/redirect-server.c         | 166 ++++++++++++++++++++++++++++++-
 hw/usb/trace-events              |   7 ++
 3 files changed, 174 insertions(+), 1 deletion(-)
include/hw/usb/redirect-server.h +2 -0
index 2af881bda9..a3d5084424 100644
@@ -31,12 +31,14 @@ struct USBRedirServer {
 
     /* usbredir over the chardev */
     struct usbredirparser *parser;
+    QEMUTimer *announce_timer;
     QEMUBH *chardev_close_bh;
     const uint8_t *read_buf;
     int read_buf_size;
     bool in_write;
     guint watch;
     bool host_connected;
+    bool device_announced;
 };
 
 #endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +165 -1

225 lines not on this page · raw message

hw/usb/trace-events +7 -0
index d141661673..2cc6a244b9 100644
@@ -399,3 +399,10 @@ usbredir_server_chardev_open(void) "chardev opened"
 usbredir_server_chardev_close(void) "chardev closed"
 usbredir_server_write_recursion(void) "recursive write, leaving it queued"
 usbredir_server_log(const char *msg) "%s"
+usbredir_server_hello(const char *version) "peer is %s"
+usbredir_server_attach(void) "device attached"
+usbredir_server_detach(bool announced) "device detached, was announced %d"
+usbredir_server_announce(uint8_t speed) "device_connect speed %u"
+usbredir_server_disconnect(void) "device_disconnect sent"
+usbredir_server_bus_reset(bool attached) "bus reset, attached %d"
+usbredir_server_filter_reject(void) "host rejected our device"
Signature · 1 line
2.53.0
author
#8 lore raw

PATCH 8/19 hw/arm/aspeed: Enable the usbredir server transport

In reply to Jamin Lin

Let the ASPEED machines accept "usb-redir-server" with -device, so a UDC gadget can be exported to a USB host running in another QEMU:

-device usb-redir-server,id=redirsrv,chardev=<chr> -device aspeed.udc-gadget,bus=redirsrv.0,udc=/machine/soc/udc

usb-redir-server sits on the main system bus so that the USB bus it provides can be found by "bus=<id>.0". That makes it a sysbus device, and a sysbus device can only be created with -device if the machine lists it.

---
 hw/arm/aspeed.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/arm/aspeed.c +6 -0
index a7cfbd6c5e..4e1ecbabe7 100644
@@ -409,6 +409,12 @@ static void aspeed_machine_class_init(ObjectClass *oc, const void *data)
     mc->no_cdrom = 1;
     mc->no_parallel = 1;
     mc->default_ram_id = "ram";
+    /*
+     * Lets a UDC gadget be exported to another QEMU instance over usbredir.
+     * The type name is spelled out rather than used through its macro so
+     * that this still builds when the usbredir library is absent.
+     */
+    machine_class_allow_dynamic_sysbus_dev(mc, "usb-redir-server");
     amc->macs_mask = ASPEED_MAC0_ON;
     amc->uart_default = ASPEED_DEV_UART5;
 
Signature · 1 line
2.53.0
author
#17 lore raw

PATCH 17/19 hw/usb/aspeed-udc: Add trace events for the transfer paths

In reply to Jamin Lin

The register traces show what the guest wrote, but not which transfer path the write chose, how much each call moved, or when the host gave up on a packet.

Add one event per path, so the name says which path ran and a single path can be enabled on its own: a kick, a transfer result, and a cancelled packet.

---
 hw/usb/aspeed-udc.c | 23 +++++++++++++++++++++++
 hw/usb/trace-events |  7 +++++++
 2 files changed, 30 insertions(+)
hw/usb/aspeed-udc.c +23 -0
index 09f5913aef..1c99e45d9a 100644
@@ -565,6 +565,9 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_desc(AspeedUDCState *s,
         aspeed_udc_raise_ep_ack(s, ep);
     }
 
+    trace_aspeed_udc_ep_xfer_in_desc(ep, (uint32_t)p->actual_length,
+                                     done ? "done" : "more");
+
     return done ? ASPEED_UDC_XFER_DONE : ASPEED_UDC_XFER_MORE;
 }
 
@@ -606,6 +609,9 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_single(AspeedUDCState *s,
      * buffer, and the next IN request sends the rest.
      */
     if (e->single_buf_off < buf_len) {
+        trace_aspeed_udc_ep_xfer_in_single(ep,
+                                           (uint32_t)p->actual_length,
+                                           "buffer left");
         return ASPEED_UDC_XFER_DONE;
     }
 
@@ -619,6 +625,9 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_in_single(AspeedUDCState *s,
                                         PROC_STS, EP_DMA_CTRL_STS_TX_IDLE);
     aspeed_udc_raise_ep_ack(s, ep);
 
+    trace_aspeed_udc_ep_xfer_in_single(ep, (uint32_t)p->actual_length,
+                                       "done");
+
     return ASPEED_UDC_XFER_DONE;
 }
 
@@ -664,9 +673,15 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_out_single(AspeedUDCState *s,
     aspeed_udc_raise_ep_ack(s, ep);
 
     if ((uint32_t)p->actual_length >= pktiov->size) {
+        trace_aspeed_udc_ep_xfer_out_single(ep,
+                                            (uint32_t)p->actual_length,
+                                            "done");
         return ASPEED_UDC_XFER_DONE;
     }
 
+    trace_aspeed_udc_ep_xfer_out_single(ep, (uint32_t)p->actual_length,
+                                        "more");
+
     return ASPEED_UDC_XFER_MORE;
 }
 
@@ -686,6 +701,8 @@ static void aspeed_udc_ep_in_kick_desc(AspeedUDCState *s, int ep,
     uint32_t cur_rptr = FIELD_EX32(old_val, EP_DMA_STS, RPTR);
     USBPacket *p = e->pkt;
 
+    trace_aspeed_udc_ep_in_kick_desc(ep);
+
     /*
      * A normal kick only sets the write pointer and leaves the read-pointer
      * field 0 (the read pointer is ours to advance). The guest resets the ring
@@ -735,6 +752,8 @@ static void aspeed_udc_ep_in_kick_single(AspeedUDCState *s, int ep)
     AspeedUDCEP *e = &s->ep[ep];
     USBPacket *p = e->pkt;
 
+    trace_aspeed_udc_ep_in_kick_single(ep);
+
     /* Do nothing if no host request is waiting, or no buffer was queued. */
     if (!p || !FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR)) {
         return;
@@ -770,6 +789,8 @@ static void aspeed_udc_ep_out_kick_single(AspeedUDCState *s, int ep)
     AspeedUDCEP *e = &s->ep[ep];
     USBPacket *p = e->pkt;
 
+    trace_aspeed_udc_ep_out_kick_single(ep);
+
     /* nothing to do unless an OUT packet is waiting and a buffer is ready */
     if (!p || !FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR)) {
         return;
@@ -1155,6 +1176,8 @@ static void aspeed_udc_gadget_cancel_packet(USBDevice *udev, USBPacket *p)
     AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
     int i;
 
+    trace_aspeed_udc_cancel_packet(p->ep->nr);
+
     if (s->ep0_packet == p) {
         s->ep0_packet = NULL;
     }
hw/usb/trace-events +7 -0
index e84561d2e8..3b828da36d 100644
@@ -395,6 +395,13 @@ aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx) "ep
 aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t iov) "ep %u, rptr %u, wptr %u, iov %u"
 aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail, uint32_t iov) "ep %u, wptr %u, avail %u, iov %u"
 aspeed_udc_ep_ack(unsigned ep) "ep %u"
+aspeed_udc_ep_in_kick_desc(int ep) "ep %d"
+aspeed_udc_ep_in_kick_single(int ep) "ep %d"
+aspeed_udc_ep_out_kick_single(int ep) "ep %d"
+aspeed_udc_ep_xfer_in_desc(int ep, uint32_t sent, const char *res) "ep %d, sent %u, %s"
+aspeed_udc_ep_xfer_in_single(int ep, uint32_t sent, const char *res) "ep %d, sent %u, %s"
+aspeed_udc_ep_xfer_out_single(int ep, uint32_t received, const char *res) "ep %d, received %u, %s"
+aspeed_udc_cancel_packet(int ep) "ep %d"
 
 # redirect-server.c
 usbredir_server_chardev_open(void) "chardev opened"
Signature · 1 line
2.53.0
author
#7 lore raw

PATCH 7/19 hw/usb/redirect-server: Stream interrupt IN endpoints

In reply to Jamin Lin

Interrupt IN is not a request and an answer in usbredir. The host asks once with start_interrupt_receiving. After that it expects a packet every time the device has data.

Park one IN packet on the device for each streaming endpoint. A device that answers USB_RET_ASYNC holds the packet and completes it when it has data. A device that answers USB_RET_NAK gives the packet back at once, so park a new one when the bus reports a wakeup on that endpoint. A slow timer parks one too, for a device that does not report a wakeup.

---
 include/hw/usb/redirect-server.h |  15 ++
 hw/usb/redirect-server.c         | 248 +++++++++++++++++++++++++++++++
 hw/usb/trace-events              |   4 +
 3 files changed, 267 insertions(+)
include/hw/usb/redirect-server.h +15 -0
index f7b2261db4..1fb2a8a7a4 100644
@@ -28,16 +28,23 @@ OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)
 #define USBREDIR_SERVER_MAX_EP 32
 #define USBREDIR_SERVER_EP_IN_BASE 16
 
+/* An endpoint number is 4 bits, so 0 to 15. */
+#define USBREDIR_SERVER_MAX_EP_NR 16
+
 /*
  * The bulk length field is 32 bits, so the host can ask for up to 4 GB.
  * This is the largest transfer accepted.
  */
 #define USBREDIR_SERVER_MAX_BULK (1 * MiB)
 
+/* Buffer size for an interrupt IN endpoint before its descriptor is seen. */
+#define USBREDIR_SERVER_INTR_DEFAULT_LEN 64
+
 #define USBREDIR_SERVER_CTRL_SETUP 0
 #define USBREDIR_SERVER_CTRL_STATUS 1
 #define USBREDIR_SERVER_BULK 2
 #define USBREDIR_SERVER_INTR 3
+#define USBREDIR_SERVER_INTR_STREAM 4
 
 /* Which message answers the host when a control transfer ends. */
 typedef enum {
@@ -87,6 +94,14 @@ struct USBRedirServer {
     bool host_connected;
     bool device_announced;
 
+    /*
+     * Interrupt IN streaming, indexed by endpoint number. intr_bh asks the
+     * device again; intr_retry does the same after a delay on NAK.
+     */
+    bool intr_in_started[USBREDIR_SERVER_MAX_EP_NR];
+    QEMUBH *intr_bh;
+    QEMUTimer *intr_retry;
+
     /* In-flight packet tracking */
     QTAILQ_HEAD(, USBRedirServerPkt) inflight;
     uint64_t next_id;
hw/usb/redirect-server.c +248 -0

309 lines not on this page · raw message

hw/usb/trace-events +4 -0
index c9ab80c6ba..931996e8cd 100644
@@ -416,3 +416,7 @@ usbredir_server_bulk_complete(uint64_t id, uint8_t ep, int status, int actual) "
 usbredir_server_bulk_too_big(uint64_t id, uint8_t ep, uint32_t len) "id %" PRIu64 " ep 0x%02x len %u"
 usbredir_server_interrupt(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu"
 usbredir_server_intr_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d"
+usbredir_server_intr_start(uint8_t ep, int status) "start_interrupt_receiving ep 0x%02x -> status %d"
+usbredir_server_intr_stop(uint8_t ep) "stop_interrupt_receiving ep 0x%02x"
+usbredir_server_intr_park(unsigned ep_nr, int len) "parked IN packet ep %u len %d"
+usbredir_server_intr_stream_complete(unsigned ep_nr, int status, int actual) "ep %u status %d actual %d"
Signature · 1 line
2.53.0
author
#11 lore raw

PATCH 11/19 hw/usb/aspeed-udc: Clear EP_POOL_ACK when no endpoint ack is left

In reply to Jamin Lin

UDC_ISR.EP_POOL_ACK says that at least one endpoint in UDC_EP_ACK_ISR has an ack. It is read only, so a write cannot clear it.

Clear it when bits 3:0 of UDC_EP_ACK_ISR are all 0, which means no programmable endpoint has an ack.

---
 hw/usb/aspeed-udc.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/usb/aspeed-udc.c +7 -0
index 95fe823534..bdbd1a78b8 100644
@@ -335,6 +335,13 @@ static void aspeed_udc_write(void *opaque, hwaddr offset, uint64_t data,
         aspeed_udc_update_irq(s);
         break;
     case R_UDC_EP_ACK_ISR:
+        val &= 0x0000000f;
+        s->regs[reg] &= ~val;
+        if (!s->regs[R_UDC_EP_ACK_ISR]) {
+            s->regs[R_UDC_ISR] &= ~R_UDC_ISR_EP_POOL_ACK_MASK;
+        }
+        aspeed_udc_update_irq(s);
+        break;
     case R_UDC_EP_NAK_ISR:
         val &= 0x0000000f;
         s->regs[reg] &= ~val;
Signature · 1 line
2.53.0
author
#9 lore raw

PATCH 9/19 hw/usb/aspeed-udc: Make the SETUP buffer byte addressable

In reply to Jamin Lin

The controller registers need a 4 byte access, but the 8 bytes at 0x80 are not registers. They are a SRAM buffer holding the EP0 SETUP packet. A read or a write that is not 4 bytes returned the wrong data, so the driver saw a broken SETUP packet.

Introduce a separate SETUP MMIO region that handles 1, 2 and 4 byte reads and writes.

---
 include/hw/usb/aspeed-udc.h | 11 ++++++--
 hw/usb/aspeed-udc.c         | 56 +++++++++++++++++++++++++++++++++----
 hw/usb/trace-events         |  2 ++
 3 files changed, 61 insertions(+), 8 deletions(-)
include/hw/usb/aspeed-udc.h +8 -3
index 7701c1aa34..42916c0bd6 100644
@@ -24,11 +24,14 @@ OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCState, ASPEED_UDC)
 OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCGadget, ASPEED_UDC_GADGET)
 
 /*
- * Register map: root/global block at 0x000 - 0x087, then one 0x10 byte bank
- * per programmable endpoint from 0x200.
+ * Register map: root/global block at 0x000 - 0x07f, the SETUP buffer at
+ * 0x080 - 0x087, then one 0x10 byte bank per programmable endpoint from
+ * 0x200.
  */
 #define ASPEED_UDC_MEM_SIZE     0x300
-#define ASPEED_UDC_ROOT_NR_REGS (0x88 >> 2)
+#define ASPEED_UDC_ROOT_NR_REGS (0x80 >> 2)
+#define ASPEED_UDC_SETUP_BASE   0x80
+#define ASPEED_UDC_SETUP_SIZE   0x08
 #define ASPEED_UDC_EP_REG_BASE  0x200
 #define ASPEED_UDC_EP_NR_REGS   (0x10 >> 2)
 
@@ -62,9 +65,11 @@ struct AspeedUDCState {
 
     MemoryRegion udc_container;
     MemoryRegion root_mr;
+    MemoryRegion setup_mr;
     MemoryRegion *dram_mr;
     AddressSpace dram_as;
     uint32_t regs[ASPEED_UDC_ROOT_NR_REGS];
+    uint8_t setup_buf[ASPEED_UDC_SETUP_SIZE];
     AspeedUDCEP ep[ASPEED_UDC_NUM_EP];
     qemu_irq irq;
 
hw/usb/aspeed-udc.c +51 -5

86 lines not on this page · raw message

hw/usb/trace-events +2 -0
index 931996e8cd..e84561d2e8 100644
@@ -381,6 +381,8 @@ canokey_unrealize(void)
 # aspeed-udc.c
 aspeed_udc_read(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
 aspeed_udc_write(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_setup_read(uint64_t offset, unsigned size, uint64_t value) "offset 0x%" PRIx64 " size %u value 0x%" PRIx64
+aspeed_udc_setup_write(uint64_t offset, unsigned size, uint64_t value) "offset 0x%" PRIx64 " size %u value 0x%" PRIx64
 aspeed_udc_ep_read(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
 aspeed_udc_ep_write(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
 aspeed_udc_pullup(int on, int attached) "on %d, attached %d"
Signature · 1 line
2.53.0
author
#13 lore raw

PATCH 13/19 hw/usb/aspeed-udc: Store the EP_DMA_STS write first

In reply to Jamin Lin

The other helpers run after EP_DMA_STS is written. The descriptor mode kick did not: it had to run before the write, because it stored the value itself.

Write the register first in every case, then call the helper. The order is now the same everywhere, so a later change cannot go wrong on it, and the code is easier to maintain.

No functional change.

---
 hw/usb/aspeed-udc.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
hw/usb/aspeed-udc.c +11 -8
index b43f077c2d..4572914e16 100644
@@ -619,12 +619,13 @@ static AspeedUDCXferResult aspeed_udc_ep_xfer_out_single(AspeedUDCState *s,
  * If the request needs more data than was queued, keep it parked and wait for
  * the next kick.
  */
-static void aspeed_udc_ep_in_kick_desc(AspeedUDCState *s, int ep, uint32_t val)
+static void aspeed_udc_ep_in_kick_desc(AspeedUDCState *s, int ep,
+                                       uint32_t old_val)
 {
     AspeedUDCEP *e = &s->ep[ep];
-    uint32_t cur_rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
-    uint32_t new_rptr = FIELD_EX32(val, EP_DMA_STS, RPTR);
-    uint32_t new_wptr = FIELD_EX32(val, EP_DMA_STS, WPTR);
+    uint32_t new_rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
+    uint32_t new_wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR);
+    uint32_t cur_rptr = FIELD_EX32(old_val, EP_DMA_STS, RPTR);
     USBPacket *p = e->pkt;
 
     /*
@@ -641,8 +642,9 @@ static void aspeed_udc_ep_in_kick_desc(AspeedUDCState *s, int ep, uint32_t val)
         cur_rptr = new_rptr;
         e->desc_off = 0;
     }
-    /* store the guest's write, but keep our own read pointer */
-    e->regs[R_EP_DMA_STS] = FIELD_DP32(val, EP_DMA_STS, RPTR, cur_rptr);
+    /* keep our own read pointer */
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS], EP_DMA_STS,
+                                       RPTR, cur_rptr);
 
     /* nothing to do unless an IN packet is waiting and the ring has data */
     if (!p || cur_rptr == new_wptr) {
@@ -715,6 +717,7 @@ static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
     AspeedUDCEP *e = opaque;
     AspeedUDCState *s = container_of(e - e->index, AspeedUDCState, ep[0]);
     uint32_t reg = offset >> 2;
+    uint32_t old_val = e->regs[reg];
     uint32_t val = data;
 
     trace_aspeed_udc_ep_write(e->index, offset, val);
@@ -725,12 +728,12 @@ static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
         break;
     case R_EP_DMA_STS:
         val &= 0x77ffffff;
+        e->regs[reg] = val;
         if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
             /* IN, descriptor-list mode */
-            aspeed_udc_ep_in_kick_desc(s, e->index, val);
+            aspeed_udc_ep_in_kick_desc(s, e->index, old_val);
         } else {
             /* OUT, single-stage mode */
-            e->regs[reg] = val;
             aspeed_udc_ep_out_kick_single(s, e->index);
         }
         break;
Signature · 1 line
2.53.0
author
#5 lore raw

PATCH 5/19 hw/usb/redirect-server: Implement control transfers

In reply to Jamin Lin

The host sends a control_packet. Turn it into the SETUP, DATA and STATUS tokens the QEMU USB core wants, run them on the device, and send the answer back.

The device may answer later, so we have to remember the packets we sent. Each one stays on a list until it comes back. We drop the whole list on a bus reset or a chardev close. When the host asks us to cancel a packet, we answer it and drop it.

usbredir has its own messages for set_configuration, get_configuration, set_alt_setting and get_alt_setting. Each one is a control transfer, so run it like one and send back the status message.

usbredir wants an ep_info message. It says what kind each endpoint is:
bulk, interrupt or control. QEMU cannot tell us. It only knows after it has read the device's descriptors, and it never reads them for a device that just forwards transfers. So we read them ourselves: the configuration descriptor passes through us on its way to the host.

---
 include/hw/usb/redirect-server.h |  46 +++
 hw/usb/redirect-server.c         | 603 ++++++++++++++++++++++++++++++-
 hw/usb/trace-events              |   5 +
 3 files changed, 653 insertions(+), 1 deletion(-)
include/hw/usb/redirect-server.h +46 -0
index a3d5084424..83c4524040 100644
@@ -15,10 +15,46 @@
 #include "qom/object.h"
 
 #include <usbredirparser.h>
+#include <usbredirproto.h>
 
 #define TYPE_USB_REDIR_SERVER "usb-redir-server"
 OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)
 
+/*
+ * The usbredir ep_info message has one slot per endpoint and direction.
+ * Index 0-15 are the OUT endpoints, 16-31 the IN ones.
+ */
+#define USBREDIR_SERVER_MAX_EP 32
+#define USBREDIR_SERVER_EP_IN_BASE 16
+
+#define USBREDIR_SERVER_CTRL_SETUP 0
+#define USBREDIR_SERVER_CTRL_STATUS 1
+
+/* Which message answers the host when a control transfer ends. */
+typedef enum {
+    USBREDIR_SERVER_REPLY_CONTROL,
+    USBREDIR_SERVER_REPLY_CONFIG,
+    USBREDIR_SERVER_REPLY_ALT,
+} USBRedirServerReply;
+
+typedef struct USBRedirServerPkt {
+    USBPacket pkt;
+
+    /* Saved headers for the usbredir response */
+    struct usb_redir_control_packet_header ctrl_hdr;
+
+    /*
+     * The IOV points here. IN data lands in it, OUT data is copied in.
+     * Allocated and freed with the packet; data_size is how big it is.
+     */
+    uint8_t *data;
+    int data_size;
+    QTAILQ_ENTRY(USBRedirServerPkt) next;
+    USBRedirServerReply reply;
+    uint64_t redir_id;
+    int type;
+} USBRedirServerPkt;
+
 struct USBRedirServer {
     SysBusDevice parent_obj;
 
@@ -39,6 +75,16 @@ struct USBRedirServer {
     guint watch;
     bool host_connected;
     bool device_announced;
+
+    /* In-flight packet tracking */
+    QTAILQ_HEAD(, USBRedirServerPkt) inflight;
+    uint64_t next_id;
+
+    /* Endpoint tables for the usbredir ep_info message. */
+    uint8_t ep_type[USBREDIR_SERVER_MAX_EP];
+    uint16_t ep_max_packet[USBREDIR_SERVER_MAX_EP];
+    uint8_t ep_interval[USBREDIR_SERVER_MAX_EP];
+    uint8_t ep_iface[USBREDIR_SERVER_MAX_EP];
 };
 
 #endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c +602 -1

697 lines not on this page · raw message

hw/usb/trace-events +5 -0
index 2cc6a244b9..25219a018a 100644
@@ -406,3 +406,8 @@ usbredir_server_announce(uint8_t speed) "device_connect speed %u"
 usbredir_server_disconnect(void) "device_disconnect sent"
 usbredir_server_bus_reset(bool attached) "bus reset, attached %d"
 usbredir_server_filter_reject(void) "host rejected our device"
+usbredir_server_cancel(uint64_t id, bool found) "id %" PRIu64 " still in flight %d"
+usbredir_server_ep_info(unsigned idx, uint8_t type, uint16_t mps, uint8_t interval, uint8_t iface) "ep_info[%u] type %u mps %u interval %u iface %u"
+usbredir_server_control(uint64_t id, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, uint16_t length) "id %" PRIu64 " type 0x%02x request 0x%02x value 0x%04x index 0x%04x length %u"
+usbredir_server_ctrl_setup_complete(uint64_t id, int status, int actual) "id %" PRIu64 " status %d actual %d"
+usbredir_server_ctrl_status_complete(uint64_t id, int status) "id %" PRIu64 " status %d"
Signature · 1 line
2.53.0
author
#14 lore raw

PATCH 14/19 hw/usb/aspeed-udc: Pick the EP_DMA_STS path by direction

In reply to Jamin Lin

DESC_OP_EN is the DMA mode. It does not tell the direction. The write handler used it as the direction anyway: set meant IN, clear meant OUT.

Both directions support both modes. So take the direction from EP_CONFIG.DIR_OUT first, then take the mode from DESC_OP_EN.

The model does not support OUT descriptor mode. Log it as not implemented. It does not support IN single stage mode yet either, so that path does nothing until the next patch.

---
 hw/usb/aspeed-udc.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
hw/usb/aspeed-udc.c +10 -5
index 4572914e16..c48e2c5f1b 100644
@@ -729,12 +729,17 @@ static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
     case R_EP_DMA_STS:
         val &= 0x77ffffff;
         e->regs[reg] = val;
-        if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
-            /* IN, descriptor-list mode */
-            aspeed_udc_ep_in_kick_desc(s, e->index, old_val);
-        } else {
-            /* OUT, single-stage mode */
+        if (FIELD_EX32(e->regs[R_EP_CONFIG], EP_CONFIG, DIR_OUT)) {
+            if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
+                qemu_log_mask(LOG_UNIMP, "%s: ep %d OUT descriptor mode is not"
+                              " implemented\n", __func__, e->index);
+                break;
+            }
             aspeed_udc_ep_out_kick_single(s, e->index);
+        } else {
+            if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
+                aspeed_udc_ep_in_kick_desc(s, e->index, old_val);
+            }
         }
         break;
     default:
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH 2/19 hw/usb/redirect-server: Add a usbredir server device

In reply to Jamin Lin

Add "usb-redir-server". It exports a USB device from this QEMU to a USB host in another QEMU, using the usbredir protocol. The other side is the existing "usb-redir" device.

The exported device is plugged in with "-device <dev>,bus=<id>.0". So we have to provide a USB bus. To find that bus, qdev starts at the main system bus and walks down the tree. A device that is not on a bus is not in the tree, so the bus it provides cannot be found. We have to sit on the main system bus. That makes this a sysbus device, even though it has no MMIO and no IRQ.

usbredir carries one device, not a bus. A hub cannot be exported because the protocol has no device address field, so only the device on the first port is redirected. To export several devices, run one usb-redir-server per device, each with its own chardev.

The link to the remote host is a chardev, so this device cannot be migrated. Its VMStateDescription says so with unmigratable.

---
 include/hw/usb/redirect-server.h |  27 +++++++
 hw/usb/redirect-server.c         | 130 +++++++++++++++++++++++++++++++
 hw/usb/meson.build               |   3 +-
 3 files changed, 159 insertions(+), 1 deletion(-)
 create mode 100644 include/hw/usb/redirect-server.h
 create mode 100644 hw/usb/redirect-server.c
include/hw/usb/redirect-server.h new file +27 -0
new file mode 100644
index 0000000000..e81d4e0608
@@ -0,0 +1,27 @@
+/*
+ * USB redirector, server side
+ *
+ * Copyright (c) 2026 ASPEED Technology Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_USB_REDIRECT_SERVER_H
+#define HW_USB_REDIRECT_SERVER_H
+
+#include "hw/core/sysbus.h"
+#include "hw/usb/usb.h"
+#include "qom/object.h"
+
+#define TYPE_USB_REDIR_SERVER "usb-redir-server"
+OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)
+
+struct USBRedirServer {
+    SysBusDevice parent_obj;
+
+    /* USB bus */
+    USBBus bus;
+    USBPort port;
+};
+
+#endif /* HW_USB_REDIRECT_SERVER_H */
hw/usb/redirect-server.c new file +130 -0

130 lines not on this page · raw message

hw/usb/meson.build +2 -1
index d4ba60a91c..8e95cc7b74 100644
@@ -77,7 +77,8 @@ endif
 if usbredir.found()
   usbredir_ss = ss.source_set()
   usbredir_ss.add(when: 'CONFIG_USB',
-                  if_true: [usbredir, files('redirect.c', 'quirks.c')])
+                  if_true: [usbredir, files('redirect.c', 'quirks.c',
+                                            'redirect-server.c')])
   hw_usb_modules += {'redirect': usbredir_ss}
 endif
 
Signature · 1 line
2.53.0
author
#18 lore raw

PATCH 18/19 hw/arm/aspeed_ast10x0: Add the USB device controller

In reply to Jamin Lin

Add the USB UDC device at 0x7E6A2000 with IRQ 9. The AST10x0 UDC controller is compatible with the AST2600 one, so reuse TYPE_ASPEED_UDC.

---
 include/hw/arm/aspeed_soc.h |  1 +
 hw/arm/aspeed_ast10x0.c     | 17 +++++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
include/hw/arm/aspeed_soc.h +1 -0
index 38226d9b80..4459188c57 100644
@@ -167,6 +167,7 @@ struct Aspeed10x0SoCState {
     AspeedSoCState parent;
 
     ARMv7MState armv7m;
+    AspeedUDCState udc;
 };
 
 #define TYPE_ASPEED10X0_SOC "aspeed10x0-soc"
hw/arm/aspeed_ast10x0.c +13 -4
index aeb5a4423d..81f2d78a45 100644
@@ -162,10 +162,11 @@ static void aspeed_soc_ast10x0_init(Object *obj, const char *socname)
     snprintf(typename, sizeof(typename), "aspeed.hace-%s", socname);
     object_initialize_child(obj, "hace", &s->hace, typename);
 
+    object_initialize_child(obj, "udc", &a->udc, TYPE_ASPEED_UDC);
+
     object_initialize_child(obj, "iomem", &s->iomem, TYPE_UNIMPLEMENTED_DEVICE);
     object_initialize_child(obj, "pwm", &s->pwm, TYPE_UNIMPLEMENTED_DEVICE);
     object_initialize_child(obj, "espi", &s->espi, TYPE_UNIMPLEMENTED_DEVICE);
-    object_initialize_child(obj, "udc", &s->udc, TYPE_UNIMPLEMENTED_DEVICE);
     object_initialize_child(obj, "sgpiom", &s->sgpiom,
                             TYPE_UNIMPLEMENTED_DEVICE);
     object_initialize_child(obj, "jtag[0]", &s->jtag[0],
@@ -394,6 +395,17 @@ static bool aspeed_soc_ast10x0_realize(Aspeed10x0SoCState *a, Error **errp)
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), 0,
                        aspeed_soc_ast1030_get_irq(s, ASPEED_DEV_GPIO));
 
+    /* UDC - USB 2.0 Device Controller */
+    object_property_set_link(OBJECT(&a->udc), "dram",
+                             OBJECT(&s->sram[0]), &error_abort);
+    if (!sysbus_realize(SYS_BUS_DEVICE(&a->udc), errp)) {
+        return false;
+    }
+    aspeed_mmio_map(s->memory, SYS_BUS_DEVICE(&a->udc), 0,
+                    sc->memmap[ASPEED_DEV_UDC]);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&a->udc), 0,
+                       aspeed_soc_ast1030_get_irq(s, ASPEED_DEV_UDC));
+
     aspeed_mmio_map_unimplemented(s->memory, SYS_BUS_DEVICE(&s->pwm),
                                   "aspeed.pwm",
                                   sc->memmap[ASPEED_DEV_PWM], 0x100);
@@ -402,9 +414,6 @@ static bool aspeed_soc_ast10x0_realize(Aspeed10x0SoCState *a, Error **errp)
                                   "aspeed.espi",
                                   sc->memmap[ASPEED_DEV_ESPI], 0x800);
 
-    aspeed_mmio_map_unimplemented(s->memory, SYS_BUS_DEVICE(&s->udc),
-                                  "aspeed.udc",
-                                  sc->memmap[ASPEED_DEV_UDC], 0x1000);
     aspeed_mmio_map_unimplemented(s->memory, SYS_BUS_DEVICE(&s->sgpiom),
                                   "aspeed.sgpiom",
                                   sc->memmap[ASPEED_DEV_SGPIOM0], 0x100);
Signature · 1 line
2.53.0
author
#6 lore raw

PATCH 6/19 hw/usb/redirect-server: Implement bulk and interrupt transfers

In reply to Jamin Lin

Send bulk and interrupt packets to the device and return the result to the host. Each one is a request and an answer, so they reuse the packet tracking added for control transfers.

A bulk request carries a 32 bit length, so the host can ask us to allocate up to 4 GB. Refuse a request that big. Do not answer it with fewer bytes instead: the host counts the bytes it gets back, and it resets the device when some are missing.

---
 include/hw/usb/redirect-server.h |  11 ++
 hw/usb/redirect-server.c         | 193 +++++++++++++++++++++++++++++++
 hw/usb/trace-events              |   5 +
 3 files changed, 209 insertions(+)
include/hw/usb/redirect-server.h +11 -0
index 83c4524040..f7b2261db4 100644
@@ -9,6 +9,7 @@
 #ifndef HW_USB_REDIRECT_SERVER_H
 #define HW_USB_REDIRECT_SERVER_H
 
+#include "qemu/units.h"
 #include "hw/core/sysbus.h"
 #include "hw/usb/usb.h"
 #include "chardev/char-fe.h"
@@ -27,8 +28,16 @@ OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer, USB_REDIR_SERVER)
 #define USBREDIR_SERVER_MAX_EP 32
 #define USBREDIR_SERVER_EP_IN_BASE 16
 
+/*
+ * The bulk length field is 32 bits, so the host can ask for up to 4 GB.
+ * This is the largest transfer accepted.
+ */
+#define USBREDIR_SERVER_MAX_BULK (1 * MiB)
+
 #define USBREDIR_SERVER_CTRL_SETUP 0
 #define USBREDIR_SERVER_CTRL_STATUS 1
+#define USBREDIR_SERVER_BULK 2
+#define USBREDIR_SERVER_INTR 3
 
 /* Which message answers the host when a control transfer ends. */
 typedef enum {
@@ -41,7 +50,9 @@ typedef struct USBRedirServerPkt {
     USBPacket pkt;
 
     /* Saved headers for the usbredir response */
+    struct usb_redir_interrupt_packet_header intr_hdr;
     struct usb_redir_control_packet_header ctrl_hdr;
+    struct usb_redir_bulk_packet_header bulk_hdr;
 
     /*
      * The IOV points here. IN data lands in it, OUT data is copied in.
hw/usb/redirect-server.c +193 -0

243 lines not on this page · raw message

hw/usb/trace-events +5 -0
index 25219a018a..c9ab80c6ba 100644
@@ -411,3 +411,8 @@ usbredir_server_ep_info(unsigned idx, uint8_t type, uint16_t mps, uint8_t interv
 usbredir_server_control(uint64_t id, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, uint16_t length) "id %" PRIu64 " type 0x%02x request 0x%02x value 0x%04x index 0x%04x length %u"
 usbredir_server_ctrl_setup_complete(uint64_t id, int status, int actual) "id %" PRIu64 " status %d actual %d"
 usbredir_server_ctrl_status_complete(uint64_t id, int status) "id %" PRIu64 " status %d"
+usbredir_server_bulk(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu"
+usbredir_server_bulk_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d"
+usbredir_server_bulk_too_big(uint64_t id, uint8_t ep, uint32_t len) "id %" PRIu64 " ep 0x%02x len %u"
+usbredir_server_interrupt(uint64_t id, uint8_t ep, size_t len) "id %" PRIu64 " ep 0x%02x len %zu"
+usbredir_server_intr_complete(uint64_t id, uint8_t ep, int status, int actual) "id %" PRIu64 " ep 0x%02x status %d actual %d"
Signature · 1 line
2.53.0
author
#20 lore raw
In reply to Jamin Lin

+ Philippe Mathieu-Daudé <[email protected]>
+ Cédric Le Goater <[email protected]>

Thanks,
Jamin

Quoted 222 lines

Subject: [PATCH v1 00/19] hw/usb: Add a usbredir server transport and AST1030 UDC support

This series redirects a USB device emulated in one QEMU instance to a USB host controller emulated in another one, over the usbredir protocol, and uses it to bring up the ASPEED AST1030 USB Device Controller (UDC).

It is the second step of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:

1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB host
(EHCI) controllers, so that work targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [Done]

https://lore.kernel.org/qemu-devel/20260902021542.3194812-1-jamin_lin@as
peedtech.com/

2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The UDC gadget is redirected out of the guest with libusbredir and attached to another QEMU that runs a USB host (a VMM, or an AST2600 / AST2700 guest). [this series]

3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]

This series implements goal 2.

The transport
=============

usb-redir-server exports a locally emulated USB device to a remote USB host over the usbredir protocol, so a device emulated in one QEMU instance can be enumerated by a host controller emulated in another one.

The left column is a request going to the device. The right column is the answer coming back. The middle hop carries usbredir messages over a socket. The top and bottom hops carry USBPackets inside QEMU.

    remote QEMU: guest driver -> EHCI/XHCI
             |                     ^
   USBPacket |                     | USBPacket
             v                     |
    "usb-redir" (the client)
             |                     ^
    usbredir |   chardev socket    | usbredir
             v                     |
    usb-redir-server (the server, this series)
             |                     ^
   USBPacket |                     | USBPacket
             v                     |
    any USBDevice, "-device <dev>,bus=<id>.0"

"usb-redir" (hw/usb/redirect.c) is the client:
- it takes a USBPacket from the remote guest and writes it to the socket as a usbredir message
- it reads the answer from the socket and completes the USBPacket

usb-redir-server is the server. It does the same thing, but backwards:
- it reads a usbredir message from the socket and runs it as a USBPacket on the bus below
- it takes the result of that USBPacket and writes it back to the same socket as a usbredir message, for the client to read

A USB device has to sit on a USB bus, and in QEMU a USB bus is always made by a host controller. So usb-redir-server makes one and acts as the host controller on this side. It models no real chip: its cable is the chardev socket. The real host is in the other QEMU.

usbredir carries one device, not a bus. A hub cannot be exported: the protocol has no device address field. To export several devices, run one usb-redir-server per device, each with its own chardev.

AST1030 gadget to an AST2700 host
=================================

QEMU #1 -M ast1030-evb QEMU #2 -M
ast2700a2-evb

  +-----------------------------+      +-----------------------------+
  | Zephyr   usb_dc_aspeed      |      | Linux    ehci-platform
|
  +--------------+--------------+      +--------------+--------------+
                 | MMIO                               | MMIO
  +--------------v--------------+      +--------------v--------------+
  | aspeed.udc                  |      | EHCI3        (usb-bus.3)
|
  +--------------+--------------+      +--------------+--------------+
                 | USBPacket                          | USBPacket
  +--------------v--------------+      +--------------v--------------+
  | aspeed.udc-gadget           |      | usb-redir
|
  +--------------+--------------+      +--------------+--------------+
                 | USBPacket                          |
  +--------------v--------------+                     |
  | usb-redir-server            |                     |
  +--------------+--------------+                     |
                 |                                    |
                 +=========== unix socket ============+

usbredir protocol

Testing
=======

1. AST1030 CDC-ACM gadget, enumerated by an AST2700 host
--------------------------------------------------------

The device side owns the socket and has to be started first.

  $ qemu-system-arm -M ast1030-evb -nographic \
        -kernel zephyr.bin \
        -chardev socket,id=u,path=/tmp/usbredir.sock,server=on,wait=off \
        -device usb-redir-server,id=udcredir,chardev=u \
        -device aspeed.udc-gadget,bus=udcredir.0,udc=/machine/soc/udc
  uart:~$ usb enable
  uart:~$ usb print 1
  $ qemu-system-aarch64 -M ast2700a2-evb -nographic \
        -drive file=image-bmc,if=mtd,format=raw -snapshot \
        -net nic -net user \
        -chardev socket,id=r,path=/tmp/usbredir.sock \
        -device usb-redir,chardev=r,bus=usb-bus.3
  root@ast2700-default:~# lsusb
  Bus 001 Device 001: ID 1d6b:0002 Linux 6.18.36 ehci_hcd EHCI Host
Controller
  Bus 001 Device 002: ID 2245:1030 ZEPHYR Zephyr DFU sample

Data was checked:

2700 -> 1030 echo '1234567890' > /dev/ttyACM0

                 1030 console: Print Data: 0x31 0x32 ... 0x30

2. A plain usb-storage exported the same way
--------------------------------------------

Any USBDevice can be exported, not just the UDC gadget. The machine on the exporting side is only a container for usb-redir-server, so it does not have to run anything.

  $ qemu-system-arm -M ast2600-evb -nographic -serial null \
        -chardev socket,id=u,path=/tmp/storage.sock,server=on,wait=off \
        -device usb-redir-server,id=keyredir,chardev=u \
        -drive id=usbdisk,if=none,file=disk.img,format=raw -snapshot \
        -device usb-storage,bus=keyredir.0,id=mystorage,drive=usbdisk
  $ qemu-system-aarch64 -M ast2700a2-evb -nographic \
        -drive file=image-bmc,if=mtd,format=raw -snapshot \
        -net nic -net user \
        -chardev socket,id=r,path=/tmp/storage.sock \
        -device usb-redir,chardev=r,bus=usb-bus.3
  root@ast2700-default:~# lsusb
  Bus 001 Device 001: ID 1d6b:0002 Linux 6.18.36 ehci_hcd EHCI Host
Controller
  Bus 001 Device 002: ID 46f4:0001 QEMU QEMU USB HARDDRIVE
  root@ast2700-default:~# mount /dev/sda /mnt/usb
  MOUNTED

A 4 MiB file was written, unmounted and remounted, and the sha256 matched.

v1
1. Add a usbredir server transport
1. Add a usbredir server device
2. Announce the exported device
3. Implement control transfers
4. Implement bulk and interrupt transfers
5. Stream interrupt IN endpoints
2. ASPEED UDC
1. Make the SETUP buffer byte addressable
2. Support single-stage IN transfers
3. Clear EP_POOL_ACK when no endpoint ack is left
4. Mask the reserved bits on register writes
5. Use the endpoint max packet size for OUT transfers
6. Test the usbredir server transport
7. Add the USB device controller for AST1030

Jamin Lin (19):
hw/usb/bus: Let a bus opt out of automatic hub insertion hw/usb/redirect-server: Add a usbredir server device hw/usb/redirect-server: Connect usbredirparser to a chardev hw/usb/redirect-server: Announce the exported device hw/usb/redirect-server: Implement control transfers hw/usb/redirect-server: Implement bulk and interrupt transfers hw/usb/redirect-server: Stream interrupt IN endpoints hw/arm/aspeed: Enable the usbredir server transport hw/usb/aspeed-udc: Make the SETUP buffer byte addressable hw/usb/aspeed-udc: Mask the reserved bits on register writes hw/usb/aspeed-udc: Clear EP_POOL_ACK when no endpoint ack is left hw/usb/aspeed-udc: Rename the transfer helpers after their DMA mode hw/usb/aspeed-udc: Store the EP_DMA_STS write first hw/usb/aspeed-udc: Pick the EP_DMA_STS path by direction hw/usb/aspeed-udc: Support single-stage IN transfers hw/usb/aspeed-udc: Use the endpoint max packet size for OUT transfers hw/usb/aspeed-udc: Add trace events for the transfer paths hw/arm/aspeed_ast10x0: Add the USB device controller tests/functional/arm/test_aspeed_ast1030: Test the usbredir server
transport

 include/hw/arm/aspeed_soc.h                 |    1 +
 include/hw/usb/aspeed-udc.h                 |   13 +-
 include/hw/usb/redirect-server.h            |  116 ++
 include/hw/usb/usb.h                        |    1 +
 hw/arm/aspeed.c                             |    6 +
 hw/arm/aspeed_ast10x0.c                     |   17 +-
 hw/usb/aspeed-udc.c                         |  290 +++-
 hw/usb/bus.c                                |    3 +-
 hw/usb/redirect-server.c                    | 1604
+++++++++++++++++++
 hw/usb/meson.build                          |    3 +-
 hw/usb/trace-events                         |   36 +
 tests/functional/arm/meson.build            |    1 +
 tests/functional/arm/test_aspeed_ast1030.py |   52 +
 13 files changed, 2088 insertions(+), 55 deletions(-)  create mode 100644

include/hw/usb/redirect-server.h create mode 100644 hw/usb/redirect-server.c

--
2.53.0

#21 lore raw

Hello Jamin,

Quoted 2 lines · Jamin Lin

+ Philippe Mathieu-Daudé <[email protected]>
+ Cédric Le Goater <[email protected]>

I have seen it :)

What would help is to split the series in two : Aspeed and usb. The Aspeed part could be reviewed by the Aspeed reviewers: Steven, Troy, Kane, Andrew, Joel and me. I already did quite a lot in this QEMU cycle, so stepping back a bit for now.

If any of the Aspeed employees mentioned above no longer have time to perform reviews, please remove them from the list. AFAICT, only you and Kane are active.

Philippe is our joker :) Don't put too much on him, he does an awful lot already !

USB is without a maintainer. It is best to address once all the required infrastructure is merged.

Here is a series that would require attention

* [PATCH 00/50] Aspeed: I2C device models and the Minerva BMC machine
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks,

C.

author
#22 lore raw

Hi Cédric

Thanks for the clarification.

I will split the series into ASPEED and USB parts as suggested.

I will also take a look at the Minerva series and see where I can help with the review.

Thanks,
Jamin

Quoted 40 lines

Subject: Re: [PATCH v1 00/19] hw/usb: Add a usbredir server transport and AST1030 UDC support

Hello Jamin,

Quoted 2 lines · Jamin Lin

+ Philippe Mathieu-Daudé <[email protected]> Cédric Le Goater
+ <[email protected]>

I have seen it :)

What would help is to split the series in two : Aspeed and usb. The Aspeed part could be reviewed by the Aspeed reviewers: Steven, Troy, Kane, Andrew, Joel and me. I already did quite a lot in this QEMU cycle, so stepping back a bit for now.

If any of the Aspeed employees mentioned above no longer have time to perform reviews, please remove them from the list. AFAICT, only you and Kane are active.

Philippe is our joker :) Don't put too much on him, he does an awful lot already !

USB is without a maintainer. It is best to address once all the required infrastructure is merged.

Here is a series that would require attention

* [PATCH 00/50] Aspeed: I2C device models and the Minerva BMC machine

https://lore.kernel.org/qemu-devel/20260907-bmc-minerva-v1-0-0d83ad610c3
[email protected]/

Thanks,

C.

#23 lore raw
Quoted 7 lines · Jamin Lin

Hi Cédric

Thanks for the clarification.

I will split the series into ASPEED and USB parts as suggested.

I will also take a look at the Minerva series and see where I can help with the review.

Thanks a lot.

C.