提交 0d92ed30 编写于 作者: P pbrook

OHCI USB host emulation.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1928 c046a42c-6fe2-441c-8c8c-71466251a162
上级 6650ee6d
......@@ -307,7 +307,7 @@ SOUND_HW += fmopl.o adlib.o
endif
# USB layer
VL_OBJS+= usb.o usb-hub.o usb-linux.o usb-hid.o
VL_OBJS+= usb.o usb-hub.o usb-linux.o usb-hid.o usb-ohci.o
# PCI network cards
VL_OBJS+= ne2000.o rtl8139.o
......
......@@ -40,7 +40,6 @@ static fdctrl_t *floppy_controller;
static RTCState *rtc_state;
static PITState *pit;
static IOAPICState *ioapic;
static USBPort *usb_root_ports[2];
static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
{
......@@ -833,8 +832,7 @@ static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
cmos_init(ram_size, boot_device, bs_table);
if (pci_enabled && usb_enabled) {
usb_uhci_init(pci_bus, usb_root_ports, piix3_devfn + 2);
usb_attach(usb_root_ports[0], vm_usb_hub);
usb_uhci_init(pci_bus, piix3_devfn + 2);
}
if (pci_enabled && acpi_enabled) {
......
......@@ -506,7 +506,11 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
arch_name = "MAC99";
}
if (usb_enabled) {
usb_ohci_init(pci_bus, 3, -1);
}
if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
graphic_depth = 15;
......
......@@ -665,6 +665,10 @@ static void ppc_prep_init(int ram_size, int vga_ram_size, int boot_device,
cpu_register_physical_memory(0xFEFF0000, 0x1000, PPC_io_memory);
#endif
if (usb_enabled) {
usb_ohci_init(pci_bus, 3, -1);
}
nvram = m48t59_init(8, 0, 0x0074, NVRAM_SIZE, 59);
if (nvram == NULL)
return;
......
......@@ -179,6 +179,9 @@ static void usb_hub_attach(USBPort *port1, USBDevice *dev)
else
port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
port->port.dev = dev;
/* send the attach message */
dev->handle_packet(dev,
USB_MSG_ATTACH, 0, 0, NULL, 0);
} else {
dev = port->port.dev;
if (dev) {
......@@ -188,6 +191,9 @@ static void usb_hub_attach(USBPort *port1, USBDevice *dev)
port->wPortStatus &= ~PORT_STAT_ENABLE;
port->wPortChange |= PORT_STAT_C_ENABLE;
}
/* send the detach message */
dev->handle_packet(dev,
USB_MSG_DETACH, 0, 0, NULL, 0);
port->port.dev = NULL;
}
}
......@@ -517,7 +523,7 @@ static int usb_hub_handle_packet(USBDevice *dev, int pid,
return usb_generic_handle_packet(dev, pid, devaddr, devep, data, len);
}
USBDevice *usb_hub_init(USBPort **usb_ports, int nb_ports)
USBDevice *usb_hub_init(int nb_ports)
{
USBHubState *s;
USBHubPort *port;
......@@ -539,12 +545,9 @@ USBDevice *usb_hub_init(USBPort **usb_ports, int nb_ports)
s->nb_ports = nb_ports;
for(i = 0; i < s->nb_ports; i++) {
port = &s->ports[i];
qemu_register_usb_port(&port->port, s, i, usb_hub_attach);
port->wPortStatus = PORT_STAT_POWER;
port->wPortChange = 0;
port->port.attach = usb_hub_attach;
port->port.opaque = s;
port->port.index = i;
usb_ports[i] = &port->port;
}
return (USBDevice *)s;
}
此差异已折叠。
......@@ -638,11 +638,10 @@ static void uhci_map(PCIDevice *pci_dev, int region_num,
register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
}
void usb_uhci_init(PCIBus *bus, USBPort **usb_ports, int devfn)
void usb_uhci_init(PCIBus *bus, int devfn)
{
UHCIState *s;
uint8_t *pci_conf;
UHCIPort *port;
int i;
s = (UHCIState *)pci_register_device(bus,
......@@ -662,11 +661,7 @@ void usb_uhci_init(PCIBus *bus, USBPort **usb_ports, int devfn)
pci_conf[0x60] = 0x10; // release number
for(i = 0; i < NB_PORTS; i++) {
port = &s->ports[i];
port->port.opaque = s;
port->port.index = i;
port->port.attach = uhci_attach;
usb_ports[i] = &port->port;
qemu_register_usb_port(&s->ports[i].port, s, i, uhci_attach);
}
s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
......
......@@ -137,12 +137,15 @@ struct USBDevice {
int setup_index;
};
typedef void (*usb_attachfn)(USBPort *port, USBDevice *dev);
/* USB port on which a device can be connected */
struct USBPort {
USBDevice *dev;
void (*attach)(USBPort *port, USBDevice *dev);
usb_attachfn attach;
void *opaque;
int index; /* internal port index, may be used with the opaque */
struct USBPort *next; /* Used internally by qemu. */
};
void usb_attach(USBPort *port, USBDevice *dev);
......@@ -152,10 +155,13 @@ int usb_generic_handle_packet(USBDevice *s, int pid,
int set_usb_string(uint8_t *buf, const char *str);
/* usb hub */
USBDevice *usb_hub_init(USBPort **usb_ports, int nb_ports);
USBDevice *usb_hub_init(int nb_ports);
/* usb-uhci.c */
void usb_uhci_init(PCIBus *bus, USBPort **usb_ports, int devfn);
void usb_uhci_init(PCIBus *bus, int devfn);
/* usb-ohci.c */
void usb_ohci_init(struct PCIBus *bus, int num_ports, int devfn);
/* usb-linux.c */
USBDevice *usb_host_device_open(const char *devname);
......
......@@ -374,6 +374,9 @@ static void versatile_init(int ram_size, int vga_ram_size, int boot_device,
pci_nic_init(pci_bus, nd);
}
}
if (usb_enabled) {
usb_ohci_init(pci_bus, 3, -1);
}
pl011_init(0x101f1000, pic, 12, serial_hds[0]);
pl011_init(0x101f2000, pic, 13, serial_hds[1]);
......
......@@ -106,6 +106,9 @@
/* in ms */
#define GUI_REFRESH_INTERVAL 30
/* Max number of USB devices that can be specified on the commandline. */
#define MAX_USB_CMDLINE 8
/* XXX: use a two level table to limit memory usage */
#define MAX_IOPORTS 65536
......@@ -145,8 +148,6 @@ CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
int win2k_install_hack = 0;
#endif
int usb_enabled = 0;
USBPort *vm_usb_ports[MAX_VM_USB_PORTS];
USBDevice *vm_usb_hub;
static VLANState *first_vlan;
int smp_cpus = 1;
int vnc_display = -1;
......@@ -3249,47 +3250,71 @@ void do_info_network(void)
/***********************************************************/
/* USB devices */
static USBPort *used_usb_ports;
static USBPort *free_usb_ports;
/* ??? Maybe change this to register a hub to keep track of the topology. */
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
usb_attachfn attach)
{
port->opaque = opaque;
port->index = index;
port->attach = attach;
port->next = free_usb_ports;
free_usb_ports = port;
}
static int usb_device_add(const char *devname)
{
const char *p;
USBDevice *dev;
int i;
USBPort *port;
if (!vm_usb_hub)
return -1;
for(i = 0;i < MAX_VM_USB_PORTS; i++) {
if (!vm_usb_ports[i]->dev)
break;
}
if (i == MAX_VM_USB_PORTS)
if (!free_usb_ports)
return -1;
if (strstart(devname, "host:", &p)) {
dev = usb_host_device_open(p);
if (!dev)
return -1;
} else if (!strcmp(devname, "mouse")) {
dev = usb_mouse_init();
if (!dev)
return -1;
} else if (!strcmp(devname, "tablet")) {
dev = usb_tablet_init();
if (!dev)
return -1;
} else {
return -1;
}
usb_attach(vm_usb_ports[i], dev);
if (!dev)
return -1;
/* Find a USB port to add the device to. */
port = free_usb_ports;
if (!port->next) {
USBDevice *hub;
/* Create a new hub and chain it on. */
free_usb_ports = NULL;
port->next = used_usb_ports;
used_usb_ports = port;
hub = usb_hub_init(VM_USB_HUB_SIZE);
usb_attach(port, hub);
port = free_usb_ports;
}
free_usb_ports = port->next;
port->next = used_usb_ports;
used_usb_ports = port;
usb_attach(port, dev);
return 0;
}
static int usb_device_del(const char *devname)
{
USBDevice *dev;
int bus_num, addr, i;
USBPort *port;
USBPort **lastp;
int bus_num, addr;
const char *p;
if (!vm_usb_hub)
if (!used_usb_ports)
return -1;
p = strchr(devname, '.');
......@@ -3299,14 +3324,21 @@ static int usb_device_del(const char *devname)
addr = strtoul(p + 1, NULL, 0);
if (bus_num != 0)
return -1;
for(i = 0;i < MAX_VM_USB_PORTS; i++) {
dev = vm_usb_ports[i]->dev;
if (dev && dev->addr == addr)
break;
lastp = &used_usb_ports;
port = used_usb_ports;
while (port && port->dev->addr != addr) {
lastp = &port->next;
port = port->next;
}
if (i == MAX_VM_USB_PORTS)
if (!port)
return -1;
usb_attach(vm_usb_ports[i], NULL);
*lastp = port->next;
usb_attach(port, NULL);
port->next = free_usb_ports;
free_usb_ports = port;
return 0;
}
......@@ -3329,35 +3361,34 @@ void do_usb_del(const char *devname)
void usb_info(void)
{
USBDevice *dev;
int i;
USBPort *port;
const char *speed_str;
if (!vm_usb_hub) {
if (!usb_enabled) {
term_printf("USB support not enabled\n");
return;
}
for(i = 0; i < MAX_VM_USB_PORTS; i++) {
dev = vm_usb_ports[i]->dev;
if (dev) {
term_printf("Hub port %d:\n", i);
switch(dev->speed) {
case USB_SPEED_LOW:
speed_str = "1.5";
break;
case USB_SPEED_FULL:
speed_str = "12";
break;
case USB_SPEED_HIGH:
speed_str = "480";
break;
default:
speed_str = "?";
break;
}
term_printf(" Device %d.%d, speed %s Mb/s\n",
0, dev->addr, speed_str);
for (port = used_usb_ports; port; port = port->next) {
dev = port->dev;
if (!dev)
continue;
switch(dev->speed) {
case USB_SPEED_LOW:
speed_str = "1.5";
break;
case USB_SPEED_FULL:
speed_str = "12";
break;
case USB_SPEED_HIGH:
speed_str = "480";
break;
default:
speed_str = "?";
break;
}
term_printf(" Device %d.%d, speed %s Mb/s\n",
0, dev->addr, speed_str);
}
}
......@@ -5066,7 +5097,7 @@ int main(int argc, char **argv)
int parallel_device_index;
const char *loadvm = NULL;
QEMUMachine *machine;
char usb_devices[MAX_VM_USB_PORTS][128];
char usb_devices[MAX_USB_CMDLINE][128];
int usb_devices_index;
LIST_INIT (&vm_change_state_head);
......@@ -5425,7 +5456,7 @@ int main(int argc, char **argv)
break;
case QEMU_OPTION_usbdevice:
usb_enabled = 1;
if (usb_devices_index >= MAX_VM_USB_PORTS) {
if (usb_devices_index >= MAX_USB_CMDLINE) {
fprintf(stderr, "Too many USB devices\n");
exit(1);
}
......@@ -5596,17 +5627,6 @@ int main(int argc, char **argv)
}
}
/* init USB devices */
if (usb_enabled) {
vm_usb_hub = usb_hub_init(vm_usb_ports, MAX_VM_USB_PORTS);
for(i = 0; i < usb_devices_index; i++) {
if (usb_device_add(usb_devices[i]) < 0) {
fprintf(stderr, "Warning: could not add USB device %s\n",
usb_devices[i]);
}
}
}
register_savevm("timer", 0, 1, timer_save, timer_load, NULL);
register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
......@@ -5710,6 +5730,16 @@ int main(int argc, char **argv)
ds, fd_filename, snapshot,
kernel_filename, kernel_cmdline, initrd_filename);
/* init USB devices */
if (usb_enabled) {
for(i = 0; i < usb_devices_index; i++) {
if (usb_device_add(usb_devices[i]) < 0) {
fprintf(stderr, "Warning: could not add USB device %s\n",
usb_devices[i]);
}
}
}
gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
......
......@@ -1022,10 +1022,10 @@ int cuda_init(SetIRQFunc *set_irq, void *irq_opaque, int irq);
/* usb ports of the VM */
#define MAX_VM_USB_PORTS 8
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
usb_attachfn attach);
extern USBPort *vm_usb_ports[MAX_VM_USB_PORTS];
extern USBDevice *vm_usb_hub;
#define VM_USB_HUB_SIZE 8
void do_usb_add(const char *devname);
void do_usb_del(const char *devname);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册