Skip to contentSkip to conversation

Add USB option in machine options

PATCH Superseded Li Zhang 3 participants 10 messages Aug 22, 2012 – Sep 3, 2012

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Li Zhang

author
#0 lore raw

PATCH v9 1/1 Add USB option in machine options

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +1 -1
index 4df2670..c67e95a 100644
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
     n8x0_dss_setup(s);
     n8x0_cbus_setup(s);
     n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))
         n8x0_usb_setup(s);
 
     if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
     pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                  floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
         pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
     }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
     ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
     ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
     cuda_init(&cuda_mem, pic[0x19]);
 
     adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
                dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
     }
 
     if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
                dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
     memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
         sysbus_connect_irq(busdev, 2, pic[50]);
         sysbus_connect_irq(busdev, 3, pic[51]);
         pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
             pci_create_simple(pci_bus, -1, "pci-ohci");
         }
         n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
         spapr->has_graphics = true;
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
         pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                           -1, "pci-ohci");
         if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
             pci_nic_init_nofail(nd, "rtl8139", NULL);
         }
     }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
     n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
             .name = "dump-guest-core",
             .type = QEMU_OPT_BOOL,
             .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
         },
         { /* End of list */ }
     },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                           const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +28 -6
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
     return 0;
 }
 
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
     const char *p;
     USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     /* drivers with .usbdevice_name entry in USBDeviceInfo */
     dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
     if (strstart(devname, "host:", &p))
         return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     p = strchr(devname, '.');
     if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
                 break;
             case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
     current_machine = machine;
 
     /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         if (foreach_device_config(DEV_USB, usb_parse) < 0)
             exit(1);
     }
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
     n8x0_dss_setup(s);
     n8x0_cbus_setup(s);
     n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
         n8x0_usb_setup(s);
 
     if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
     pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                  floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
         pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
     }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
     ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
     ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
     cuda_init(&cuda_mem, pic[0x19]);
 
     adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
                dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
     }
 
     if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
                dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
     memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
         sysbus_connect_irq(busdev, 2, pic[50]);
         sysbus_connect_irq(busdev, 3, pic[51]);
         pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
             pci_create_simple(pci_bus, -1, "pci-ohci");
         }
         n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
         spapr->has_graphics = true;
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
         pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                           -1, "pci-ohci");
         if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
             pci_nic_init_nofail(nd, "rtl8139", NULL);
         }
     }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
     n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
             .name = "dump-guest-core",
             .type = QEMU_OPT_BOOL,
             .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
         },
         { /* End of list */ }
     },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                           const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
     return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 62 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
     const char *p;
     USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     /* drivers with .usbdevice_name entry in USBDeviceInfo */
     dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
     if (strstart(devname, "host:", &p))
         return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     p = strchr(devname, '.');
     if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
                 break;
             case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
     current_machine = machine;
 
     /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         if (foreach_device_config(DEV_USB, usb_parse) < 0)
             exit(1);
     }
--

1.7.7.6

#2 lore raw
Quoted 237 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
    n8x0_dss_setup(s);
    n8x0_cbus_setup(s);
    n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
        n8x0_usb_setup(s);
 
    if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
    pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                 floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
        pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
    }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
    ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
    ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
    cuda_init(&cuda_mem, pic[0x19]);
 
    adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
    macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
               dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
    }
 
    if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
    macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
               dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
    memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
    }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
    }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
        sysbus_connect_irq(busdev, 2, pic[50]);
        sysbus_connect_irq(busdev, 3, pic[51]);
        pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
            pci_create_simple(pci_bus, -1, "pci-ohci");
        }
        n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
        spapr->has_graphics = true;
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
        pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                          -1, "pci-ohci");
        if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
            pci_nic_init_nofail(nd, "rtl8139", NULL);
        }
    }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
    n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
            .name = "dump-guest-core",
            .type = QEMU_OPT_BOOL,
            .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
        },
        { /* End of list */ }
    },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                          const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
    return 0;
 }
 
Quoted 14 lines

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
    const char *p;
    USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
        return -1;
+    }
 
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
    dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
    if (strstart(devname, "host:", &p))
        return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
        return -1;
+    }
 
    p = strchr(devname, '.');
    if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
                }
                break;
            case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
                break;
            case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                add_device_config(DEV_USB, optarg);
                break;
            case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
    current_machine = machine;
 
    /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
            exit(1);
    }
--

1.7.7.6

#3 lore raw
Quoted 256 lines · Alexander Graf
Quoted 237 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
    n8x0_dss_setup(s);
    n8x0_cbus_setup(s);
    n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
        n8x0_usb_setup(s);
 
    if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
    pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                 floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
        pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
    }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
    ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
    ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
    cuda_init(&cuda_mem, pic[0x19]);
 
    adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
    macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
               dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
    }
 
    if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
    macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
               dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
    memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
    }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
    }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
        sysbus_connect_irq(busdev, 2, pic[50]);
        sysbus_connect_irq(busdev, 3, pic[51]);
        pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
            pci_create_simple(pci_bus, -1, "pci-ohci");
        }
        n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
        spapr->has_graphics = true;
    }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
        pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                          -1, "pci-ohci");
        if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
            pci_nic_init_nofail(nd, "rtl8139", NULL);
        }
    }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        pci_create_simple(pci_bus, -1, "pci-ohci");
    }
    n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
            .name = "dump-guest-core",
            .type = QEMU_OPT_BOOL,
            .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
        },
        { /* End of list */ }
    },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                          const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
    return 0;
 }
 
Quoted 14 lines

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

Quoted 67 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
    const char *p;
    USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
        return -1;
+    }
 
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
    dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
    if (strstart(devname, "host:", &p))
        return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
        return -1;
+    }
 
    p = strchr(devname, '.');
    if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
                }
                break;
            case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
                break;
            case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                add_device_config(DEV_USB, optarg);
                break;
            case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
    current_machine = machine;
 
    /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
            exit(1);
    }
--

1.7.7.6

#4 lore raw
Quoted 257 lines · Blue Swirl
Quoted 256 lines · Alexander Graf
Quoted 251 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
   n8x0_dss_setup(s);
   n8x0_cbus_setup(s);
   n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
       n8x0_usb_setup(s);
 
   if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
   pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
       pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
   }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
   ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
   ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
   cuda_init(&cuda_mem, pic[0x19]);
 
   adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
              dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
   }
 
   if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
              dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
   memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
       sysbus_connect_irq(busdev, 2, pic[50]);
       sysbus_connect_irq(busdev, 3, pic[51]);
       pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
           pci_create_simple(pci_bus, -1, "pci-ohci");
       }
       n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
       spapr->has_graphics = true;
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
       pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                         -1, "pci-ohci");
       if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
           pci_nic_init_nofail(nd, "rtl8139", NULL);
       }
   }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
   n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
           .name = "dump-guest-core",
           .type = QEMU_OPT_BOOL,
           .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
       },
       { /* End of list */ }
   },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                         const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
   return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 3 lines
Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

It would still be duplication of information bits that need to be synchronized. I don't see any point for the set_usb_option helper. So why bother with another variable when we have one in the machine opts?

Alex

Quoted 68 lines
Quoted 68 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
   const char *p;
   USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   /* drivers with .usbdevice_name entry in USBDeviceInfo */
   dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
   if (strstart(devname, "host:", &p))
       return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   p = strchr(devname, '.');
   if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
               }
               break;
           case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
               break;
           case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
               add_device_config(DEV_USB, optarg);
               break;
           case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
   current_machine = machine;
 
   /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       if (foreach_device_config(DEV_USB, usb_parse) < 0)
           exit(1);
   }
--

1.7.7.6

#5 lore raw
Quoted 261 lines · Alexander Graf
Quoted 257 lines · Blue Swirl
Quoted 256 lines · Alexander Graf
Quoted 251 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
   n8x0_dss_setup(s);
   n8x0_cbus_setup(s);
   n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
       n8x0_usb_setup(s);
 
   if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
   pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
       pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
   }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
   ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
   ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
   cuda_init(&cuda_mem, pic[0x19]);
 
   adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
              dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
   }
 
   if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
              dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
   memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
       sysbus_connect_irq(busdev, 2, pic[50]);
       sysbus_connect_irq(busdev, 3, pic[51]);
       pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
           pci_create_simple(pci_bus, -1, "pci-ohci");
       }
       n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
       spapr->has_graphics = true;
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
       pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                         -1, "pci-ohci");
       if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
           pci_nic_init_nofail(nd, "rtl8139", NULL);
       }
   }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
   n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
           .name = "dump-guest-core",
           .type = QEMU_OPT_BOOL,
           .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
       },
       { /* End of list */ }
   },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                         const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
   return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

It would still be duplication of information bits that need to be synchronized. I don't see any point for the set_usb_option helper. So why bother with another variable when we have one in the machine opts?

If set_usb_option() is deleted (I don't see any callers), the variable could be static to usb_enabled(). But yes, the function is probably called only once at board init, so caching would not make sense.

Quoted 71 lines

Alex

Quoted 68 lines
Quoted 68 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
   const char *p;
   USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   /* drivers with .usbdevice_name entry in USBDeviceInfo */
   dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
   if (strstart(devname, "host:", &p))
       return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   p = strchr(devname, '.');
   if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
               }
               break;
           case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
               break;
           case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
               add_device_config(DEV_USB, optarg);
               break;
           case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
   current_machine = machine;
 
   /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       if (foreach_device_config(DEV_USB, usb_parse) < 0)
           exit(1);
   }
--

1.7.7.6

author
#6 lore raw
Quoted 262 lines · Blue Swirl
Quoted 261 lines · Alexander Graf
Quoted 257 lines · Blue Swirl
Quoted 256 lines · Alexander Graf
Quoted 251 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
   n8x0_dss_setup(s);
   n8x0_cbus_setup(s);
   n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
       n8x0_usb_setup(s);
 
   if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
   pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
       pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
   }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
   ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
   ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
   cuda_init(&cuda_mem, pic[0x19]);
 
   adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
              dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
   }
 
   if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
   macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
              dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
   memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
       s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       sysbus_create_simple("sysbus-ohci", 0x4c000000,
                       qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
   }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
       sysbus_connect_irq(busdev, 2, pic[50]);
       sysbus_connect_irq(busdev, 3, pic[51]);
       pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
           pci_create_simple(pci_bus, -1, "pci-ohci");
       }
       n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
       spapr->has_graphics = true;
   }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
       pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                         -1, "pci-ohci");
       if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
           pci_nic_init_nofail(nd, "rtl8139", NULL);
       }
   }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       pci_create_simple(pci_bus, -1, "pci-ohci");
   }
   n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
           .name = "dump-guest-core",
           .type = QEMU_OPT_BOOL,
           .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
       },
       { /* End of list */ }
   },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                         const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
   return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

It would still be duplication of information bits that need to be synchronized. I don't see any point for the set_usb_option helper. So why bother with another variable when we have one in the machine opts?

If set_usb_option() is deleted (I don't see any callers), the variable

When -usb or -usbdevice is used, this function is called. It is as the following in my patch.
case QEMU_OPTION_usb:
- usb_enabled = 1;

+                set_usb_option(true);
                 break;
             case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                 add_device_config(DEV_USB, optarg);
                 break;
Quoted 74 lines

could be static to usb_enabled(). But yes, the function is probably called only once at board init, so caching would not make sense.

Quoted 71 lines

Alex

Quoted 68 lines
Quoted 68 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
   const char *p;
   USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   /* drivers with .usbdevice_name entry in USBDeviceInfo */
   dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
   if (strstart(devname, "host:", &p))
       return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
       return -1;
+    }
 
   p = strchr(devname, '.');
   if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
               }
               break;
           case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
               break;
           case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
               add_device_config(DEV_USB, optarg);
               break;
           case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
   current_machine = machine;
 
   /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
       if (foreach_device_config(DEV_USB, usb_parse) < 0)
           exit(1);
   }
--

1.7.7.6

Signature · 2 lines
Best Regards
-Li
#7 lore raw
Quoted 273 lines · Li Zhang
Quoted 262 lines · Blue Swirl
Quoted 261 lines · Alexander Graf
Quoted 257 lines · Blue Swirl
Quoted 256 lines · Alexander Graf
Quoted 251 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
  n8x0_dss_setup(s);
  n8x0_cbus_setup(s);
  n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
      n8x0_usb_setup(s);
 
  if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
  pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
               floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
      pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
  }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
  ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
  ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
  cuda_init(&cuda_mem, pic[0x19]);
 
  adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
  macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
             dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
  }
 
  if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
  macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
             dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
  memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
      s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      sysbus_create_simple("sysbus-ohci", 0x4c000000,
                      qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
  }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
      s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      sysbus_create_simple("sysbus-ohci", 0x4c000000,
                      qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
  }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
      sysbus_connect_irq(busdev, 2, pic[50]);
      sysbus_connect_irq(busdev, 3, pic[51]);
      pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
          pci_create_simple(pci_bus, -1, "pci-ohci");
      }
      n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
      spapr->has_graphics = true;
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
      pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                        -1, "pci-ohci");
      if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
          pci_nic_init_nofail(nd, "rtl8139", NULL);
      }
  }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
  n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
          .name = "dump-guest-core",
          .type = QEMU_OPT_BOOL,
          .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
      },
      { /* End of list */ }
  },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                        const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
  return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

It would still be duplication of information bits that need to be synchronized. I don't see any point for the set_usb_option helper. So why bother with another variable when we have one in the machine opts?

If set_usb_option() is deleted (I don't see any callers), the variable

When -usb or -usbdevice is used, this function is called. It is as the following in my patch.
case QEMU_OPTION_usb:

Review

Quoted 6 lines
(quoted diff) +2 -2
-                usb_enabled = 1;
+                set_usb_option(true);
                break;
            case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);

Yes, just open-code it or move the function to vl.c.

Alex

Quoted 84 lines
                add_device_config(DEV_USB, optarg);
                break;
Quoted 74 lines

could be static to usb_enabled(). But yes, the function is probably called only once at board init, so caching would not make sense.

Quoted 71 lines

Alex

Quoted 68 lines
Quoted 68 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
  const char *p;
  USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
      return -1;
+    }
 
  /* drivers with .usbdevice_name entry in USBDeviceInfo */
  dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
  if (strstart(devname, "host:", &p))
      return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
      return -1;
+    }
 
  p = strchr(devname, '.');
  if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
              }
              break;
          case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
              break;
          case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
              add_device_config(DEV_USB, optarg);
              break;
          case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
  current_machine = machine;
 
  /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      if (foreach_device_config(DEV_USB, usb_parse) < 0)
          exit(1);
  }
--

1.7.7.6

Signature · 2 lines
Best Regards
-Li
author
#8 lore raw
Quoted 284 lines · Alexander Graf
Quoted 273 lines · Li Zhang
Quoted 262 lines · Blue Swirl
Quoted 261 lines · Alexander Graf
Quoted 257 lines · Blue Swirl
Quoted 256 lines · Alexander Graf
Quoted 251 lines · Blue Swirl
Quoted 32 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

hw/nseries.c +0 -0
index 4df2670..c67e95a 100644

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 5 lines
hw/nseries.c +1 -1
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
  n8x0_dss_setup(s);
  n8x0_cbus_setup(s);
  n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Quoted 193 lines
hw/nseries.c +0 -0
      n8x0_usb_setup(s);
 
  if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
  pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
               floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
      pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
  }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
  ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
  ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
  cuda_init(&cuda_mem, pic[0x19]);
 
  adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
  macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
             dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
  }
 
  if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
  macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
             dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
  memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
      s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      sysbus_create_simple("sysbus-ohci", 0x4c000000,
                      qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
  }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
      s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      sysbus_create_simple("sysbus-ohci", 0x4c000000,
                      qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
  }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
      sysbus_connect_irq(busdev, 2, pic[50]);
      sysbus_connect_irq(busdev, 3, pic[51]);
      pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
          pci_create_simple(pci_bus, -1, "pci-ohci");
      }
      n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
      spapr->has_graphics = true;
  }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
      pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                        -1, "pci-ohci");
      if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
          pci_nic_init_nofail(nd, "rtl8139", NULL);
      }
  }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      pci_create_simple(pci_bus, -1, "pci-ohci");
  }
  n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
          .name = "dump-guest-core",
          .type = QEMU_OPT_BOOL,
          .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
      },
      { /* End of list */ }
  },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                        const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
  return 0;
 }
 

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

This is not a performance critical code path, right? So I'd prefer to have a single point of information (machine opts) rather than yet another global variable.

The flags don't have to be global, they can be static to vl.c.

It would still be duplication of information bits that need to be synchronized. I don't see any point for the set_usb_option helper. So why bother with another variable when we have one in the machine opts?

If set_usb_option() is deleted (I don't see any callers), the variable

When -usb or -usbdevice is used, this function is called. It is as the following in my patch.
case QEMU_OPTION_usb:

Review

Quoted 6 lines
(quoted diff) +2 -2
-                usb_enabled = 1;
+                set_usb_option(true);
                break;
            case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
Quoted 2 lines

Yes, just open-code it or move the function to vl.c.

OK, thanks. -:)

Quoted 86 lines

Alex

Quoted 84 lines
                add_device_config(DEV_USB, optarg);
                break;
Quoted 74 lines

could be static to usb_enabled(). But yes, the function is probably called only once at board init, so caching would not make sense.

Quoted 71 lines

Alex

Quoted 68 lines
Quoted 68 lines

Alex

Quoted 64 lines
Quoted 63 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
  const char *p;
  USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
      return -1;
+    }
 
  /* drivers with .usbdevice_name entry in USBDeviceInfo */
  dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
  if (strstart(devname, "host:", &p))
      return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
      return -1;
+    }
 
  p = strchr(devname, '.');
  if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
              }
              break;
          case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
              break;
          case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
              add_device_config(DEV_USB, optarg);
              break;
          case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
  current_machine = machine;
 
  /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
      if (foreach_device_config(DEV_USB, usb_parse) < 0)
          exit(1);
  }
--

1.7.7.6

--

Best Regards
-Li

Signature · 2 lines
Best Regards
-Li
author
#9 lore raw
In reply to Blue Swirl
Quoted 28 lines · Blue Swirl
Quoted 27 lines · Li Zhang

When -usb option is used, global varible usb_enabled is set. And all the plafrom will create one USB controller according to this variable. In fact, global varibles make code hard to read.

So this patch is to remove global variable usb_enabled and add USB option in machine options. All the plaforms will get USB option value from machine options.

USB option of machine options will be set either by:
* -usb
* -machine type=pseries,usb=on

Both these ways can work now. They both set USB option in machine options. In the future, the first way will be removed.

---
v7->v8 :
* Declare usb_enabled() and set_usb_option() in sysemu.h
* Separate USB enablement on sPAPR platform.

v8->v9:
* Fix usb_enable() default value on sPAPR and MAC99

Quoted 12 lines

hw/nseries.c static void n8x0_init(ram_addr_t ram_size, const char *boot_device, · line 1325

Quoted 10 lines
hw/nseries.c +1 -1
index 4df2670..c67e95a 100644
@@ -1322,7 +1322,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
     n8x0_dss_setup(s);
     n8x0_cbus_setup(s);
     n8x0_uart_setup(s);
-    if (usb_enabled)
+    if (usb_enabled(false))

Please add braces.

OK, I will do that.

Quoted 3 lines

I don't like this usb_enabled(false) way very much but I don't have anything better to suggest.

Thanks,
It seems that default value is false on most of platforms, only true for several platforms.

Quoted 193 lines
Quoted 193 lines
hw/nseries.c +0 -0
         n8x0_usb_setup(s);
 
     if (kernel_filename) {
hw/pc_piix.c +1 -1
index 0c0096f..b662192 100644
@@ -267,7 +267,7 @@ static void pc_init1(MemoryRegion *system_memory,
     pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                  floppy, idebus[0], idebus[1], rtc_state);
 
-    if (pci_enabled && usb_enabled) {
+    if (pci_enabled && usb_enabled(false)) {
         pci_create_simple(pci_bus, piix3_devfn + 2, "piix3-usb-uhci");
     }
 
hw/ppc_newworld.c +7 -12
index e95cfe8..1d4f494 100644
@@ -348,10 +348,6 @@ static void ppc_core99_init (ram_addr_t ram_size,
     ide_mem[1] = pmac_ide_init(hd, pic[0x0d], dbdma, 0x16, pic[0x02]);
     ide_mem[2] = pmac_ide_init(&hd[MAX_IDE_DEVS], pic[0x0e], dbdma, 0x1a, pic[0x02]);
 
-    /* cuda also initialize ADB */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usb_enabled = 1;
-    }
     cuda_init(&cuda_mem, pic[0x19]);
 
     adb_kbd_init(&adb_bus);
@@ -360,15 +356,14 @@ static void ppc_core99_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem,
                dbdma_mem, cuda_mem, NULL, 3, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(machine_arch == ARCH_MAC99_U3)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
-    }
-
-    /* U3 needs to use USB for input because Linux doesn't support via-cuda
-       on PPC64 */
-    if (machine_arch == ARCH_MAC99_U3) {
-        usbdevice_create("keyboard");
-        usbdevice_create("mouse");
+        /* U3 needs to use USB for input because Linux doesn't support via-cuda
+        on PPC64 */
+        if (machine_arch == ARCH_MAC99_U3) {
+            usbdevice_create("keyboard");
+            usbdevice_create("mouse");
+        }
     }
 
     if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
hw/ppc_oldworld.c +1 -1
index 1dcd8a6..1468a32 100644
@@ -286,7 +286,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
     macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
                dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/ppc_prep.c +1 -1
index 7a87616..feeb903 100644
@@ -662,7 +662,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
     memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
 #endif
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
 
hw/pxa2xx.c +2 -2
index d5f1420..4787279 100644
@@ -2108,7 +2108,7 @@ PXA2xxState *pxa270_init(MemoryRegion *address_space,
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
@@ -2239,7 +2239,7 @@ PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         sysbus_create_simple("sysbus-ohci", 0x4c000000,
                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
     }
hw/realview.c +1 -1
index 19db4d0..a8d6f97 100644
@@ -227,7 +227,7 @@ static void realview_init(ram_addr_t ram_size,
         sysbus_connect_irq(busdev, 2, pic[50]);
         sysbus_connect_irq(busdev, 3, pic[51]);
         pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci");
-        if (usb_enabled) {
+        if (usb_enabled(false)) {
             pci_create_simple(pci_bus, -1, "pci-ohci");
         }
         n = drive_get_max_bus(IF_SCSI);
hw/spapr.c +1 -1
index be533ee..2e31797 100644
@@ -766,7 +766,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,
         spapr->has_graphics = true;
     }
 
-    if (usb_enabled) {
+    if (usb_enabled(spapr->has_graphics)) {
         pci_create_simple(QLIST_FIRST(&spapr->phbs)->host_state.bus,
                           -1, "pci-ohci");
         if (spapr->has_graphics) {
hw/versatilepb.c +1 -1
index 7a92034..df32c8b 100644
@@ -247,7 +247,7 @@ static void versatile_init(ram_addr_t ram_size,
             pci_nic_init_nofail(nd, "rtl8139", NULL);
         }
     }
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         pci_create_simple(pci_bus, -1, "pci-ohci");
     }
     n = drive_get_max_bus(IF_SCSI);
qemu-config.c +4 -0
index c05ffbc..d1a86cf 100644
@@ -612,6 +612,10 @@ static QemuOptsList qemu_machine_opts = {
             .name = "dump-guest-core",
             .type = QEMU_OPT_BOOL,
             .help = "Include guest memory in  a core dump",
+        },{
+            .name = "usb",
+            .type = QEMU_OPT_BOOL,
+            .help = "Set on/off to enable/disable usb",
         },
         { /* End of list */ }
     },
sysemu.h +4 -1
index 65552ac..be0905e 100644
@@ -119,7 +119,6 @@ extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
 extern int ctrl_grab;
-extern int usb_enabled;
 extern int smp_cpus;
 extern int max_cpus;
 extern int cursor_hide;
@@ -189,4 +188,8 @@ void register_devices(void);
 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
                           const char *suffix);
 char *get_boot_devices_list(uint32_t *size);
+
+bool usb_enabled(bool default_usb);
+void set_usb_option(bool usb_option);
+
 #endif
vl.c +0 -1
index 7c577fa..4702e33 100644
@@ -203,7 +203,6 @@ CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
-int usb_enabled = 0;
 int singlestep = 0;
 int smp_cpus = 1;
 int max_cpus = 0;
@@ -790,6 +789,27 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
     return 0;
 }
 
Quoted 15 lines

vl.c static int parse_sandbox(QemuOpts *opts, void *opaque) · line 801

Quoted 10 lines
vl.c +10 -0
+/*********QEMU USB setting******/
+bool usb_enabled(bool default_usb)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        return qemu_opt_get_bool(mach_opts, "usb", default_usb);
+    }
+    return default_usb;
+}

It would be more optimal to go through the options only once after machine selection in vl.c, setting some flags as a result. Then this and below functions would only check against those flags.

The following function is called when using -usb or -usbdevice. It also sets USB option when -usb or -usbdevice is used. The following function hasn't be called by other platforms.

Quoted 62 lines
Quoted 62 lines
vl.c +18 -6
+
+void set_usb_option(bool usb_option)
+{
+    QemuOpts *mach_opts;
+    mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (mach_opts) {
+        qemu_opt_set_bool(mach_opts, "usb", usb_option);
+    }
+}
+
+
 /***********************************************************/
 /* QEMU Block devices */
 
@@ -1078,8 +1098,9 @@ static int usb_device_add(const char *devname)
     const char *p;
     USBDevice *dev = NULL;
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     /* drivers with .usbdevice_name entry in USBDeviceInfo */
     dev = usbdevice_create(devname);
@@ -1115,8 +1136,9 @@ static int usb_device_del(const char *devname)
     if (strstart(devname, "host:", &p))
         return usb_host_device_close(p);
 
-    if (!usb_enabled)
+    if (!usb_enabled(false)) {
         return -1;
+    }
 
     p = strchr(devname, '.');
     if (!p)
@@ -3062,10 +3084,10 @@ int main(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_usb:
-                usb_enabled = 1;
+                set_usb_option(true);
                 break;
             case QEMU_OPTION_usbdevice:
-                usb_enabled = 1;
+                set_usb_option(true);
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
@@ -3623,7 +3645,7 @@ int main(int argc, char **argv, char **envp)
     current_machine = machine;
 
     /* init USB devices */
-    if (usb_enabled) {
+    if (usb_enabled(false)) {
         if (foreach_device_config(DEV_USB, usb_parse) < 0)
             exit(1);
     }
--

1.7.7.6

Signature · 2 lines
Best Regards
-Li