> 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.
> 
> 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.
> 
> Signed-off-by: Jamin Lin <[email protected]>
> Reviewed-by: Marc-AndrĂ© Lureau <[email protected]>
> Message-ID: <[email protected]>
> ---
>  hw/usb/redirect-server.c | 130 ++++++++++++++++++++++++++++++++++++++-
>  hw/usb/trace-events      |   7 +++
>  2 files changed, 136 insertions(+), 1 deletion(-)
>
> diff --git a/hw/usb/redirect-server.c b/hw/usb/redirect-server.c
> index 4971d81af489..ad6e4c422bb9 100644
> --- a/hw/usb/redirect-server.c
> +++ b/hw/usb/redirect-server.c
> @@ -56,6 +56,7 @@
>  #include "qemu/main-loop.h"
>  #include "qemu/module.h"
>  #include "migration/vmstate.h"
> +#include "qemu/cutils.h"
>  #include "hw/core/sysbus.h"
>  #include "hw/usb/usb.h"
>  #include "chardev/char-fe.h"
> @@ -90,12 +91,88 @@ struct USBRedirServer {
>      bool in_write;
>      guint watch;
>      bool host_connected;
> +    bool device_announced;
>  };
>  
> +/*
> + * The device is whatever USBDevice the user plugged into our port with
> + * "-device <device>,bus=<id>.0". NULL until then.
> + */
> +static USBDevice *usbredir_server_device(USBRedirServer *s)
> +{
> +    return s->port.dev;
> +}
> +
> +/*
> + * Device announcement
> + */
> +
> +static uint8_t usbredir_server_speed(USBDevice *device)
> +{
> +    switch (device->speed) {
> +    case USB_SPEED_LOW:
> +        return usb_redir_speed_low;
> +    case USB_SPEED_FULL:
> +        return usb_redir_speed_full;
> +    default:
> +        return usb_redir_speed_high;
> +    }
> +}
> +
> +static void usbredir_server_announce_device(USBRedirServer *s)
> +{
> +    USBDevice *device = usbredir_server_device(s);
> +    struct usb_redir_interface_info_header iface_info = {
> +        .interface_count = 0,
> +    };
> +    struct usb_redir_device_connect_header conn = {};
> +
> +    if (!s->host_connected || s->device_announced ||
> +        !device || !device->attached) {
> +        return;
> +    }
> +    s->device_announced = true;
> +    conn.speed = usbredir_server_speed(device);
> +
> +    /* Put the device in DEFAULT state. The host may not reset the bus. */
> +    device->addr = 0;
> +    device->state = USB_STATE_DEFAULT;

usb_attach() overwrites this with USB_STATE_ATTACHED right after the
port attach callback returns. A subsequent control request from the peer
would trip the dev->state == USB_STATE_DEFAULT assertion in
usb_handle_packet().

You could announce with a BH instead (cancel it on detach) and use
usb_device_reset() instead of manually poking the fields before
announcing.

-- 
Marc-AndrĂ© Lureau <[email protected]>


Reply via email to