USB proxy the other way
Hi,
attached is an experimental patch that makes use of the Linux' USB
gadgetfs userspace API to allow connecting emulated USB devices to the host instead of to the qemu virtual machine. This is very much proof-of-concept now but I hope to use it for connecting emulated S3C2410-based devices (and possibly PXA and/or OMAP310, all of which can work as USB slaves) to the computer on which qemu is running and have them appear as if an actual device (say a mobile phone) was connected to its USB cradle.
To enable gadgetfs on a Linux 2.6 host you will need to enable the following three options:
Device Drivers -> USB support -> USB Gadget Support -> Support for USB Gadgets Device Drivers -> USB support -> USB Gadget Support -> Gadget Filesystem Device Drivers -> USB support -> USB Gadget Support -> USB Peripheral Controller = Dummy HCD (unless you have a real USB slave controller)
You will need to edit drivers/usb/gadget/Kconfig and comment out line
221 (# select USB_GADGET_DUALSPEED) because Dummy HCD is
dualspeed by default and qemu currently only has full-speed devices. If you don't do that then gadgetfs expects high-speed descriptors and qemu doesn't provide those, Linux panics and locks-up (yes, gadgetfs seems to be totally untested). This could also be worked around in qemu easily.
Not all devices can be emulated this way because of gadgetfs and dummy_hcd limitations. For example a USB Hub cannot be emulated because dummy_hcd will report it as a bus-powered device and Linux power policy will not accept bus-powered Hubs. (This is unfortunate because qemu connects all devices through a hub normally). GadgetFS'es signalling of requests on data endpoints is also very strange and I haven't found any way to know when a request occured other than polling. Does anyone have experience with gadgetfs?
I tested with the following devices:
QEMU USB-HID mouse and tablet.
QEMU USB CDC/RNDIS NIC which was posted a long time ago in
http://lists.gnu.org/archive/html/qemu-devel/2006-11/msg00034.html - gadgetfs doesn't support multiple configuration devices so I had to make some changes on top of it that are in the second attached file.
Wacom Penpartner tablet emulation from
http://svn.o-hand.com/view/misc/trunk/qemu-packaging/qemu/debian/patches/90-oh-wacom.patch?rev=158&view=auto
A host IRDA dongle (attached through usb_add host:2.26)
QEMU USB-Hub - this didn't work because of the power policy thing. QEMU USB Mass Storage Device - this also didn't work, probably the timing of the bulk transfers is not correct with the polling - advices welcome.
Any suggestions about the cmdline switches/monitor commands to deal with virtual USB slaves, also welcome.
Here are some funny logs from my (non-virtual) PC:
Apr 23 03:05:27 [kernel] gadgetfs: bound to dummy_udc driver Apr 23 03:05:28 [kernel] usb 3-1: new full speed USB device using dummy_hcd and address 35 Apr 23 03:05:28 [kernel] gadgetfs: connected Apr 23 03:05:28 [kernel] gadgetfs: disconnected Apr 23 03:05:28 [kernel] gadgetfs: connected Apr 23 03:05:28 [kernel] usb 3-1: Product: QEMU USB Mouse Apr 23 03:05:28 [kernel] usb 3-1: Manufacturer: QEMU 0.8.2 Apr 23 03:05:28 [kernel] usb 3-1: SerialNumber: 1 Apr 23 03:05:28 [kernel] usb 3-1: configuration #1 chosen from 1 choice Apr 23 03:05:28 [kernel] gadgetfs: configuration #1 Apr 23 03:05:28 [kernel] input: QEMU 0.8.2 QEMU USB Mouse as /class/input/input24 Apr 23 03:05:28 [kernel] input: USB HID v0.01 Mouse [QEMU 0.8.2 QEMU USB Mouse] on usb-dummy_hcd-1 Apr 23 03:06:08 [kernel] gadgetfs: disconnected Apr 23 03:06:08 [kernel] usb 3-1: USB disconnect, address 35
Apr 24 18:11:09 [kernel] gadgetfs: bound to dummy_udc driver Apr 24 18:11:10 [kernel] usb 3-1: new full speed USB device using dummy_hcd and address 15 Apr 24 18:11:10 [kernel] gadgetfs: connected Apr 24 18:11:10 [kernel] gadgetfs: disconnected Apr 24 18:11:10 [kernel] gadgetfs: connected Apr 24 18:11:10 [kernel] usb 3-1: Product: RNDIS/QEMU USB Network Device Apr 24 18:11:10 [kernel] usb 3-1: Manufacturer: QEMU Apr 24 18:11:10 [kernel] usb 3-1: SerialNumber: 1 Apr 24 18:11:10 [kernel] usb 3-1: configuration #2 chosen from 1 choice Apr 24 18:11:10 [kernel] gadgetfs: configuration #2 Apr 24 18:11:10 [kernel] eth1: register 'cdc_ether' at usb-dummy_hcd-1, CDC Ethernet Device, 40:01:02:03:04:05 Apr 24 18:11:10 [kernel] usbcore: registered new interface driver cdc_ether Apr 24 18:12:30 [kernel] gadgetfs: disconnected Apr 24 18:12:30 [kernel] usb 3-1: USB disconnect, address 15 Apr 24 18:12:30 [kernel] eth1: unregister 'cdc_ether' usb-dummy_hcd-1,
CDC Ethernet Device
Apr 24 21:18:44 [kernel] gadgetfs: bound to dummy_udc driver
Apr 24 21:18:45 [kernel] usb 3-1: new full speed USB device using
dummy_hcd and address 17
Apr 24 21:18:45 [kernel] gadgetfs: connected
Apr 24 21:18:45 [kernel] gadgetfs: disconnected
Apr 24 21:18:45 [kernel] gadgetfs: connected
Apr 24 21:18:45 [kernel] usb 3-1: Product: IrDA/USB Bridge
Apr 24 21:18:45 [kernel] usb 3-1: Manufacturer: Sigmatel Inc
Apr 24 21:18:45 [kernel] usb 3-1: configuration #1 chosen from 1 choice
Apr 24 21:18:45 [kernel] gadgetfs: configuration #1
- Last output repeated twice -
Apr 24 21:18:45 [kernel] SigmaTel STIr4200 IRDA/USB found at address
17, Vendor: 66f, Product: 4200
Apr 24 21:18:45 [kernel] drivers/net/irda/stir4200.c: IrDA: Registered
SigmaTel device irda0
Apr 24 21:19:09 [kernel] gadgetfs: disconnected
Apr 24 21:19:09 [kernel] usb 3-1: USB disconnect, address 17Regards,
Andrzej
Makefile.target +1 -0
@@ -338,6 +338,7 @@ VL_OBJS+= scsi-disk.o cdrom.o lsi53c895a.o # USB layer VL_OBJS+= usb.o usb-hub.o usb-linux.o usb-hid.o usb-ohci.o usb-msd.o usb-net.o +VL_OBJS+= usb-linux-gadget.o # PCI network cards VL_OBJS+= ne2000.o rtl8139.o pcnet.o
hw/usb.h +3 -0
@@ -214,6 +214,9 @@ void usb_ohci_init_memio(target_phys_addr_t base, int num_ports, int devfn,USBDevice *usb_host_device_open (const char *devname);void usb_host_info (void ); +/* usb-linux-gadget.c */ +int usb_gadget_init (void ); +/* usb-hid.c */ USBDevice *usb_mouse_init (void );USBDevice *usb_tablet_init (void );
usb-linux-gadget.c new file +726 -0
726 lines not on this page · raw message
vl.c +13 -0
@@ -6135,6 +6135,7 @@ void help(void)#endif "-usb enable the USB driver (will be the default soon)\n" "-usbdevice name add the host or guest USB device 'name'\n" + "-usbgadget enable the linux USB GadgetFS as an HCD\n"#if defined(TARGET_PPC ) || defined(TARGET_SPARC ) "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"#endif
@@ -6287,6 +6288,7 @@ enum { QEMU_OPTION_win2k_hack, QEMU_OPTION_usb, QEMU_OPTION_usbdevice, + QEMU_OPTION_usbgadget, QEMU_OPTION_smp, QEMU_OPTION_vnc, QEMU_OPTION_no_acpi,
@@ -6369,6 +6371,7 @@ const QEMUOption qemu_options[] = { { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, + { "usbgadget", 0, QEMU_OPTION_usbgadget }, { "smp", HAS_ARG, QEMU_OPTION_smp }, { "vnc", HAS_ARG, QEMU_OPTION_vnc },
@@ -6626,6 +6629,7 @@ int main(int argc, char **argv)QEMUMachine *machine;char usb_devices[MAX_USB_CMDLINE ][128 ];int usb_devices_index; +int usbgadget_enabled =0 ;int fds[2 ];LIST_INIT (&vm_change_state_head);
@@ -7038,6 +7042,9 @@ int main(int argc, char **argv) optarg); usb_devices_index++;break ; +case QEMU_OPTION_usbgadget: + usbgadget_enabled =1 ; +break ;case QEMU_OPTION_smp: smp_cpus =atoi (optarg);if (smp_cpus <1 || smp_cpus >MAX_CPUS ) {
@@ -7324,6 +7331,12 @@ int main(int argc, char **argv) } } +if (usbgadget_enabled) { +if (usb_gadget_init () <0 ) { +fprintf (stderr,"Warning: could not find USB gadgetfs\n" ); + } + } + gui_timer =qemu_new_timer (rt_clock, gui_update,NULL );qemu_mod_timer (gui_timer,qemu_get_clock (rt_clock));
hw/usb-net.c +5 -5
@@ -111,14 +111,14 @@ static const uint8_t qemu_net_dev_descriptor[] = {STRING_MANUFACTURER ,/* u8 iManufacturer; */ STRING_PRODUCT ,/* u8 iProduct; */ STRING_SERIALNUMBER ,/* u8 iSerialNumber; */ -0x02 /* u8 bNumConfigurations; */ +0x01 /* u8 bNumConfigurations; */ };static const uint8_t qemu_net_rndis_config_descriptor[] = {/* Configuration Descriptor */ 0x09 ,/* u8 bLength */ USB_DT_CONFIG ,/* u8 bDescriptorType */ -0x00 ,0x00 ,/* le16 wTotalLength */ +0x43 ,0x00 ,/* le16 wTotalLength */ 0x02 ,/* u8 bNumInterfaces */ DEV_RNDIS_CONFIG_VALUE ,/* u8 bConfigurationValue */ STRING_RNDIS, /* u8 iConfiguration */
@@ -193,7 +193,7 @@ static const uint8_t qemu_net_cdc_config_descriptor[] = {/* Configuration Descriptor */ 0x09 ,/* u8 bLength */ USB_DT_CONFIG ,/* u8 bDescriptorType */ -0x00 ,0x00 ,/* le16 wTotalLength */ +0x50 ,0x00 ,/* le16 wTotalLength */ 0x02 ,/* u8 bNumInterfaces */ DEV_RNDIS_CONFIG_VALUE ,/* u8 bConfigurationValue */ STRING_RNDIS, /* u8 iConfiguration */
@@ -972,12 +972,12 @@ static int usb_net_handle_control(USBDevice *dev, int request, int value,case USB_DT_CONFIG :switch (value &0xff ) { -case 0 : +case 1 : ret =sizeof (qemu_net_rndis_config_descriptor);memcpy (data, qemu_net_rndis_config_descriptor, ret);break ; -case 1 : +case 0 : ret =sizeof (qemu_net_cdc_config_descriptor);memcpy (data, qemu_net_cdc_config_descriptor, ret);break ;