usb initialization is starting in vl.c, line 4259. as below.

if (usb_enabled(false)) {
if (foreach_device_config(DEV_USB, usb_parse) < 0)
exit(1);
}

static int usb_parse(const char *cmdline)
{
int r;
r = usb_device_add(cmdline);
if (r < 0) {
fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
}
return r;
}

static int usb_device_add(const char *devname)
{
USBDevice *dev = NULL;
#ifndef CONFIG_LINUX
const char *p;
#endif

if (!usb_enabled(false)) {
return -1;
}

/* drivers with .usbdevice_name entry in USBDeviceInfo */
dev = usbdevice_create(devname);
if (dev)
goto done;

/* the other ones */
#ifndef CONFIG_LINUX
/* only the linux version is qdev-ified, usb-bsd still needs this */
if (strstart(devname, "host:", &p)) {
dev = usb_host_device_open(usb_bus_find(-1), p);
}
#endif
if (!dev)
return -1;

done:
return 0;
}

USBDevice *usbdevice_create(const char *cmdline)
{
USBBus *bus = usb_bus_find(-1 /* any */);
LegacyUSBFactory *f = NULL;
GSList *i;
char driver[32];
const char *params;
int len;

params = strchr(cmdline,':');
if (params) {
params++;
len = params - cmdline;
if (len > sizeof(driver))
len = sizeof(driver);
pstrcpy(driver, len, cmdline);
} else {
params = "";
pstrcpy(driver, sizeof(driver), cmdline);
}

for (i = legacy_usb_factory; i; i = i->next) {
f = i->data;
if (strcmp(f->usbdevice_name, driver) == 0) {
break;
}
}
if (i == NULL) {
#if 0
/* no error because some drivers are not converted (yet) */
error_report("usbdevice %s not found", driver);
#endif
return NULL;
}

if (!bus) {
error_report("Error: no usb bus to attach usbdevice %s, "
"please try -machine usb=on and check that "
"the machine model supports USB", driver);
return NULL;
}

if (!f->usbdevice_init) {
if (*params) {
error_report("usbdevice %s accepts no params", driver);
return NULL;
}
return usb_create_simple(bus, f->name);
}
return f->usbdevice_init(bus, params);
}

if it uses classic device, usb_create_simple will be called for usb_device_type_info.

And,  if it doesn't, we should find what is assinged to usbdevice_init function pointer.

classic initialization for USB is as below.

static void usb_device_class_init(ObjectClass *klass, void *data)
{
DeviceClass *k = DEVICE_CLASS(klass);
k->bus_type = TYPE_USB_BUS;
k->init = usb_qdev_init;
k->unplug = qdev_simple_unplug_cb;
k->exit = usb_qdev_exit;
k->props = usb_props;
}

static const TypeInfo usb_device_type_info = {
.name = TYPE_USB_DEVICE,
.parent = TYPE_DEVICE,
.instance_size = sizeof(USBDevice),
.abstract = true,
.class_size = sizeof(USBDeviceClass),
.class_init = usb_device_class_init,
};

and here are a function initialization for USB. every usb function uses this usb_legacy_register function to initialize.

void usb_legacy_register(const char *typename, const char *usbdevice_name,
USBDevice *(*usbdevice_init)(USBBus *bus,
const char *params))
{
if (usbdevice_name) {
LegacyUSBFactory *f = g_malloc0(sizeof(*f));
f->name = typename;
f->usbdevice_name = usbdevice_name;
f->usbdevice_init = usbdevice_init;
legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
}
}

but, usbdevice_init function is also a function pointer. so i listed up functions using usb_legacy_register function.

ccid_register_type, usb_audio_register_type, usb_bt_register_type, usb_hid_register_type, usb_msd_register_type, usb_host_register_type, usb_net_register_type, usb_serial_register_type, usb_wacom_register_type.

 

The list up above, those are USBDeviceClass, not DeviceClass.  both have init function, so the function DeviceClass has will be called, and then, the functions USBDeviceClass will be called by DeviceClass's init function.

 

and finally, all those functions are going to call qdev_init_nofail function.

as we know, this function call  object_property_set_bool(OBJECT(dev), true, "realized", &local_err); for initializing device.

then, props->set is going to be called and props and a init function is assign in classes for USB. 

DeviceState calls device_set_realized, and DeviceClass calls deivce_realize. and then, DeviceClass calls init function.

Posted by MeatNBrew
l