use qdev for -usbdevice
cover use qdev for -usbdevice
Hi,
This patch series changes the way the -usbdevice switch (and the usb_add monitor command) is handled. Instead of hard-coding stuff in vl.c it is integrated with qdev by adding new fields to USBDeviceInfo. First patch adds the infrastructure. Follwing patches switch over the usb drivers to the new way of handling things. Not converted (yet) are:
* bt: bluetooth is not qdev-ified at all, needs investigation.
* host: big project, especially the auto-add-by-id stuff.
* net: too many net patches in flight right now.
cheers,
Gerd
PATCH 1/5 usb core: use qdev for -usbdevice
This patchs adds infrastructure to handle -usbdevice via qdev callbacks. USBDeviceInfo gets a name field (for the -usbdevice driver name) and a callback for -usbdevice parameter parsing.
The new usbdevice_create() function walks the qdev driver list and looks for a usb driver with a matching name. When a parameter parsing callback is present it is called, otherwise the device is created via usb_create_simple().
--- hw/qdev.c | 2 +- hw/qdev.h | 1 + hw/usb-bus.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ hw/usb.h | 5 +++++ vl.c | 5 +++++ 5 files changed, 59 insertions(+), 1 deletions(-)
hw/qdev.c +1 -1
@@ -35,7 +35,7 @@ static int qdev_hotplug = 0;/* This is a nasty hack to allow passing a NULL bus to qdev_create. */ static BusState *main_system_bus; -static DeviceInfo *device_info_list; +DeviceInfo *device_info_list;static BusState *qbus_find_recursive (BusState *bus,const char *name,const BusInfo *info);
hw/qdev.h +1 -0
@@ -140,6 +140,7 @@ struct DeviceInfo {BusInfo *bus_info;struct DeviceInfo *next; }; +extern DeviceInfo *device_info_list;void qdev_register (DeviceInfo *info);
hw/usb-bus.c +47 -0
50 lines not on this page · raw message
hw/usb.h +5 -0
@@ -183,6 +183,10 @@ struct USBDeviceInfo {* Returns length or one of the USB_RET_ codes. */ int (*handle_data)(USBDevice *dev,USBPacket *p); + +/* handle legacy -usbdevice command line options */ +const char *usbdevice_name; +USBDevice *(*usbdevice_init)(const char *params); };typedef void (*usb_attachfn )(USBPort *port,USBDevice *dev);
@@ -309,6 +313,7 @@ void usb_qdev_register(USBDeviceInfo *info);void usb_qdev_register_many (USBDeviceInfo *info);USBDevice *usb_create (USBBus *bus,const char *name);USBDevice *usb_create_simple (USBBus *bus,const char *name); +USBDevice *usbdevice_create (const char *cmdline);void usb_register_port (USBBus *bus,USBPort *port,void *opaque,int index,usb_attachfn attach);void usb_unregister_port (USBBus *bus,USBPort *port);
vl.c +5 -0
@@ -2562,6 +2562,11 @@ static int usb_device_add(const char *devname, int is_hotplug)if (!usb_enabled)return -1 ; +/* drivers with .usbdevice_name entry in USBDeviceInfo */ + dev =usbdevice_create (devname); +if (dev) + goto done; +/* simple devices which don't need extra care */ for (i =0 ; i <ARRAY_SIZE (usbdevs); i++) {if (strcmp (devname, usbdevs[i].name) !=0 )
Signature · 1 line
1.6.2.5
PATCH 2/5 usb-hid: use qdev for -usbdevice
In reply to Gerd Hoffmann--- hw/usb-hid.c | 3 +++ hw/usb-wacom.c | 1 + vl.c | 29 ----------------------------- 3 files changed, 4 insertions(+), 29 deletions(-)
hw/usb-hid.c +3 -0
@@ -882,6 +882,7 @@ static struct USBDeviceInfo hid_info[] = { { .qdev.name = "QEMU USB Tablet", .qdev.alias = "usb-tablet", + .usbdevice_name = "tablet", .qdev.size = sizeof(USBHIDState), .init = usb_tablet_initfn, .handle_packet = usb_generic_handle_packet,
@@ -892,6 +893,7 @@ static struct USBDeviceInfo hid_info[] = { },{ .qdev.name = "QEMU USB Mouse", .qdev.alias = "usb-mouse", + .usbdevice_name = "mouse", .qdev.size = sizeof(USBHIDState), .init = usb_mouse_initfn, .handle_packet = usb_generic_handle_packet,
@@ -902,6 +904,7 @@ static struct USBDeviceInfo hid_info[] = { },{ .qdev.name = "QEMU USB Keyboard", .qdev.alias = "usb-kbd", + .usbdevice_name = "keyboard", .qdev.size = sizeof(USBHIDState), .init = usb_keyboard_initfn, .handle_packet = usb_generic_handle_packet,
hw/usb-wacom.c +1 -0
@@ -411,6 +411,7 @@ static int usb_wacom_initfn(USBDevice *dev)static struct USBDeviceInfo wacom_info = { .qdev.name ="QEMU PenPartner Tablet" , .qdev.alias ="wacom-tablet" , + .usbdevice_name ="wacom-tablet" , .qdev.size =sizeof (USBWacomState), .init = usb_wacom_initfn, .handle_packet = usb_generic_handle_packet,
vl.c +0 -29
@@ -2533,31 +2533,10 @@ static void usb_msd_password_cb(void *opaque, int err) dev->info->handle_destroy (dev); } -static struct { -const char *name; -const char *qdev; -} usbdevs[] = { - { - .name ="mouse" , - .qdev ="QEMU USB Mouse" , - },{ - .name ="tablet" , - .qdev ="QEMU USB Tablet" , - },{ - .name ="keyboard" , - .qdev ="QEMU USB Keyboard" , - },{ - .name ="wacom-tablet" , - .qdev ="QEMU PenPartner Tablet" , - } -}; -static int usb_device_add (const char *devname,int is_hotplug) {const char *p; -USBBus *bus =usb_bus_find (-1 /* any */ );USBDevice *dev =NULL ; -int i;if (!usb_enabled)return -1 ;
@@ -2567,14 +2546,6 @@ static int usb_device_add(const char *devname, int is_hotplug)if (dev) goto done; -/* simple devices which don't need extra care */ -for (i =0 ; i <ARRAY_SIZE (usbdevs); i++) { -if (strcmp (devname, usbdevs[i].name) !=0 ) -continue ; - dev =usb_create_simple (bus, usbdevs[i].qdev); - goto done; - } -/* the other ones */ if (strstart (devname,"host:" , &p)) { dev =usb_host_device_open (p);
Signature · 1 line
1.6.2.5
PATCH 3/5 usb-serial and braille: use qdev for -usbdevice
In reply to Gerd Hoffmann--- hw/baum.c | 6 ---- hw/baum.h | 3 -- hw/usb-serial.c | 85 ++++++++++++++++++++++++++++++++++++++++++------------- hw/usb.h | 3 -- vl.c | 6 ---- 5 files changed, 65 insertions(+), 38 deletions(-)
hw/baum.c +0 -6
@@ -627,9 +627,3 @@ fail_handle:free (baum);return NULL ; } - -USBDevice *usb_baum_init (void ) -{ -/* USB Product ID of Super Vario 40 */ -return usb_serial_init ("productid=FE72:braille" ); -}
hw/baum.h +0 -3
@@ -22,8 +22,5 @@* THE SOFTWARE. */ -/* usb device */ -USBDevice *usb_baum_init (void ); -/* char device */ CharDriverState *chr_baum_init (QemuOpts *opts);
hw/usb-serial.c +65 -20
147 lines not on this page · raw message
hw/usb.h +0 -3
@@ -265,9 +265,6 @@ USBDevice *usb_net_init(NICInfo *nd);/* usb-bt.c */ USBDevice *usb_bt_init (HCIInfo *hci); -/* usb-serial.c */ -USBDevice *usb_serial_init (const char *filename); -/* usb ports of the VM */ #define VM_USB_HUB_SIZE 8
vl.c +0 -6
@@ -2564,12 +2564,6 @@ static int usb_device_add(const char *devname, int is_hotplug)return 0 ; } } - }else if (strstart (devname,"serial:" , &p)) { - dev =usb_serial_init (p); -#ifdef CONFIG_BRLAPI - }else if (!strcmp (devname,"braille" )) { - dev =usb_baum_init (); -#endif }else if (strstart (devname,"net:" , &p)) {QemuOpts *opts;int idx;
Signature · 1 line
1.6.2.5
PATCH 4/5 usb: make attach optional.
In reply to Gerd HoffmannAdd a auto_attach field to USBDevice, which is enabled by default. USB drivers can clear this field in case they do *not* want the device being attached (i.e. plugged into a usb port) automatically after successfull init().
Use cases:
* attaching encrypted mass storage devices (see next patch).
* maybe also -usbdevice host:$vendorid:$productid
--- hw/usb-bus.c | 7 +++++-- hw/usb.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-)
hw/usb-bus.c +5 -2
@@ -46,7 +46,7 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)pstrcpy (dev->devname,sizeof (dev->devname), qdev->info->name); dev->info = info; rc = dev->info->init (dev); -if (rc ==0 ) +if (rc ==0 && dev->auto_attach)usb_device_attach (dev);return rc; }
@@ -82,6 +82,7 @@ void usb_qdev_register_many(USBDeviceInfo *info)USBDevice *usb_create (USBBus *bus,const char *name) {DeviceState *dev; +USBDevice *usb;#if 1 /* temporary stopgap until all usb is properly qdev-ified */
@@ -95,7 +96,9 @@ USBDevice *usb_create(USBBus *bus, const char *name)#endif dev =qdev_create (&bus->qbus, name); -return DO_UPCAST (USBDevice, qdev, dev); + usb =DO_UPCAST (USBDevice, qdev, dev); + usb->auto_attach =1 ; +return usb; }USBDevice *usb_create_simple (USBBus *bus,const char *name)
hw/usb.h +1 -0
@@ -133,6 +133,7 @@ struct USBDevice {int speed;uint8_t addr;char devname[32 ]; +int auto_attach;int attached;int state;
Signature · 1 line
1.6.2.5
PATCH 5/5 usb-storage: use qdev for -usbdevice
In reply to Gerd HoffmannHook up usb_msd_init.
Also rework handling of encrypted block devices,
move the code out vl.c.
--- hw/usb-msd.c | 33 +++++++++++++++++++++++++-------- hw/usb.h | 4 ---- vl.c | 25 ------------------------- 3 files changed, 25 insertions(+), 37 deletions(-)
hw/usb-msd.c +25 -8
@@ -14,6 +14,7 @@#include "block.h" #include "scsi-disk.h" #include "console.h" +#include "monitor.h" //#define DEBUG_MSD
@@ -508,6 +509,16 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)return ret; } +static void usb_msd_password_cb (void *opaque,int err) +{ +MSDState *s = opaque; + +if (!err) +usb_device_attach (&s->dev); +else +qdev_unplug (&s->dev.qdev); +} +static int usb_msd_initfn (USBDevice *dev) {MSDState *s =DO_UPCAST (MSDState, dev, dev);
@@ -522,10 +533,21 @@ static int usb_msd_initfn(USBDevice *dev) s->scsi_dev =scsi_bus_legacy_add_drive (&s->bus, s->dinfo,0 ); s->bus.qbus.allow_hotplug =0 ;usb_msd_handle_reset (dev); + +if (bdrv_key_required (s->dinfo->bdrv)) { +if (s->dev.qdev.hotplugged) { +monitor_read_bdrv_key_start (cur_mon, s->dinfo->bdrv, + usb_msd_password_cb, s); + s->dev.auto_attach =0 ; + }else { + autostart =0 ; + } + } +return 0 ; } -USBDevice *usb_msd_init (const char *filename) +static USBDevice *usb_msd_init (const char *filename) {static int nr=0 ;char id[8 ];
@@ -577,13 +599,6 @@ USBDevice *usb_msd_init(const char *filename)return dev; } -BlockDriverState *usb_msd_get_bdrv (USBDevice *dev) -{ -MSDState *s = (MSDState *)dev; - -return s->dinfo->bdrv; -} -static struct USBDeviceInfo msd_info = { .qdev.name ="QEMU USB MSD" , .qdev.alias ="usb-storage" ,
@@ -593,6 +608,8 @@ static struct USBDeviceInfo msd_info = { .handle_reset = usb_msd_handle_reset, .handle_control = usb_msd_handle_control, .handle_data = usb_msd_handle_data, + .usbdevice_name = "disk", + .usbdevice_init = usb_msd_init, .qdev.props = (Property []) {DEFINE_PROP_DRIVE ("drive" , MSDState, dinfo),DEFINE_PROP_END_OF_LIST (),
hw/usb.h +0 -4
@@ -256,10 +256,6 @@ void usb_host_info(Monitor *mon);/* usb-hid.c */ void usb_hid_datain_cb (USBDevice *dev,void *opaque,void (*datain)(void *)); -/* usb-msd.c */ -USBDevice *usb_msd_init (const char *filename); -BlockDriverState *usb_msd_get_bdrv (USBDevice *dev); -/* usb-net.c */ USBDevice *usb_net_init (NICInfo *nd);
vl.c +0 -25
@@ -2523,16 +2523,6 @@ static void smp_parse(const char *optarg)/***********************************************************/ /* USB devices */ -static void usb_msd_password_cb (void *opaque,int err) -{ -USBDevice *dev = opaque; - -if (!err) -usb_device_attach (dev); -else - dev->info->handle_destroy (dev); -} -static int usb_device_add (const char *devname,int is_hotplug) {const char *p;
@@ -2549,21 +2539,6 @@ static int usb_device_add(const char *devname, int is_hotplug)/* the other ones */ if (strstart (devname,"host:" , &p)) { dev =usb_host_device_open (p); - }else if (strstart (devname,"disk:" , &p)) { -BlockDriverState *bs; - - dev =usb_msd_init (p); -if (!dev) -return -1 ; - bs =usb_msd_get_bdrv (dev); -if (bdrv_key_required (bs)) { - autostart =0 ; -if (is_hotplug) { -monitor_read_bdrv_key_start (cur_mon, bs, usb_msd_password_cb, - dev); -return 0 ; - } - } }else if (strstart (devname,"net:" , &p)) {QemuOpts *opts;int idx;
Signature · 1 line
1.6.2.5