Skip to contentSkip to conversation

Difficulty making usb-serial device visible within the guest

DISCUSSION Mark Cave-Ayland 2 participants 3 messages Aug 20, 2020

lore
Share & tools
lore
#0 lore raw

Hi all,

I've been playing a bit with the usb-serial device recently and found it surprisingly difficult to make it visible within the guest. As an example my first test was to simply plug the device into the guest USB and verify that the drivers could identify the device:

$ ./qemu-system-ppc -M mac99 -chardev null,id=dummy -device usb-serial,chardev=dummy

QEMU starts up fine, but the usb-serial device doesn't appear on the bus. My next thought was that there was an issue with the null chardev device, so I then tried this:

$ ./qemu-system-ppc -M mac99 -chardev pty,path=/tmp/foo,id=dummy -device
usb-serial,chardev=dummy

That didn't work either. I then tried to set up a socket device and use socat to connect:

$ ./qemu-system-ppc -M mac99 -chardev socket,server,wait,path=/tmp/foo,id=dummy
-device usb-serial,chardev=dummy
$ socat UNIX-CONNECT:/tmp/foo -

Finally I was able to see my usb-serial device within the guest. As this behaviour was unexpected I had a look at dev-serial.c and found that it was being controlled by this if() statement in usb_serial_realize():

    if (qemu_chr_fe_backend_open(&s->cs) && !dev->attached) {
        usb_device_attach(dev, &error_abort);
    }

With auto-attach disabled this effectively prevents the device from attaching to the USB unless the chardev is opened which feels odd. I would expect that if I add a device to QEMU using -device then it is immediately visible in the guest, and if the chardev isn't connected then the device should report its status as disconnected as you would expect with a real USB to RS232 adapter.

According to git log the behaviour was introduced in commit da124e62de "usb-serial:
only expose device in guest when the chardev is open". Gerd, can you remember why this behaviour was introduced in the first place?

ATB,

Mark.

#1 lore raw

Hi,

Quoted 2 lines

With auto-attach disabled this effectively prevents the device from attaching to the USB unless the chardev is opened which feels odd.

Why? It's not needed to connect at boot btw, the device will be hotplugged if the chardev is opened.

Quoted 4 lines

I would expect that if I add a device to QEMU using -device then it is immediately visible in the guest, and if the chardev isn't connected then the device should report its status as disconnected as you would expect with a real USB to RS232 adapter.

Huh? How would that work? You can do some guesswork using modem line status etc, but as far I know _reliable_ connected/disconnected reporting simply doesn't exist in the world of serial lines ...

I'm open to add a device property to turn off the hotplug behavior. Patches are welcome.

take care,
Gerd

#2 lore raw
Quoted 5 lines · Gerd Hoffmann
Quoted 2 lines

With auto-attach disabled this effectively prevents the device from attaching to the USB unless the chardev is opened which feels odd.

Why? It's not needed to connect at boot btw, the device will be hotplugged if the chardev is opened.

Yes, I see that - however...

Quoted 8 lines
Quoted 4 lines

I would expect that if I add a device to QEMU using -device then it is immediately visible in the guest, and if the chardev isn't connected then the device should report its status as disconnected as you would expect with a real USB to RS232 adapter.

Huh? How would that work? You can do some guesswork using modem line status etc, but as far I know _reliable_ connected/disconnected reporting simply doesn't exist in the world of serial lines ...

I'm just comparing the behaviour with a real FTDI adapter: if I plug it in, it's always visible on the guest USB regardless as to whether there is a cable attached - the line status is returned as it is seen on the wire. Whether the guest OS has a concept as to whether a cable is attached is something that is internal to the driver, and completely separate from QEMU's notion as to whether a chardev is attached.

For someone who has a reasonable amount of QEMU development experience I was just surprised that I ended up having to going through the source code to understand why my usb-serial device wasn't appearing in the guest. Certainly there is no mention of this requirement in the QEMU documentation.

Something else I noticed was that this behaviour is unique to USB: in-built serial ports behave exactly as I described above which matches the real FTDI USB hardware. And another question: if this behaviour is acceptable for USB, should it not be the same for other hotplug buses, e.g. should a PCIe serial card plugged into a PCIe bus plug and unplug itself as the chardev is attached/detached in a similar way?

ATB,

Mark.