1 /* 2 * QEMU USB EHCI Emulation 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or(at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "hw/usb/hcd-ehci.h" 19 #include "qemu/range.h" 20 21 typedef struct EHCIPCIInfo { 22 const char *name; 23 uint16_t vendor_id; 24 uint16_t device_id; 25 uint8_t revision; 26 } EHCIPCIInfo; 27 28 static int usb_ehci_pci_initfn(PCIDevice *dev) 29 { 30 EHCIPCIState *i = PCI_EHCI(dev); 31 EHCIState *s = &i->ehci; 32 uint8_t *pci_conf = dev->config; 33 34 pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20); 35 36 /* capabilities pointer */ 37 pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00); 38 /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */ 39 40 pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */ 41 pci_set_byte(&pci_conf[PCI_MIN_GNT], 0); 42 pci_set_byte(&pci_conf[PCI_MAX_LAT], 0); 43 44 /* pci_conf[0x50] = 0x01; *//* power management caps */ 45 46 pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */ 47 pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */ 48 pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */ 49 50 pci_conf[0x64] = 0x00; 51 pci_conf[0x65] = 0x00; 52 pci_conf[0x66] = 0x00; 53 pci_conf[0x67] = 0x00; 54 pci_conf[0x68] = 0x01; 55 pci_conf[0x69] = 0x00; 56 pci_conf[0x6a] = 0x00; 57 pci_conf[0x6b] = 0x00; /* USBLEGSUP */ 58 pci_conf[0x6c] = 0x00; 59 pci_conf[0x6d] = 0x00; 60 pci_conf[0x6e] = 0x00; 61 pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */ 62 63 s->irq = pci_allocate_irq(dev); 64 s->as = pci_get_address_space(dev); 65 66 usb_ehci_realize(s, DEVICE(dev), NULL); 67 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem); 68 69 return 0; 70 } 71 72 static void usb_ehci_pci_init(Object *obj) 73 { 74 EHCIPCIState *i = PCI_EHCI(obj); 75 EHCIState *s = &i->ehci; 76 77 s->caps[0x09] = 0x68; /* EECP */ 78 79 s->capsbase = 0x00; 80 s->opregbase = 0x20; 81 s->portscbase = 0x44; 82 s->portnr = NB_PORTS; 83 84 usb_ehci_init(s, DEVICE(obj)); 85 } 86 87 static void usb_ehci_pci_exit(PCIDevice *dev) 88 { 89 EHCIPCIState *i = PCI_EHCI(dev); 90 EHCIState *s = &i->ehci; 91 92 usb_ehci_unrealize(s, DEVICE(dev), NULL); 93 94 if (s->irq) { 95 g_free(s->irq); 96 s->irq = NULL; 97 } 98 } 99 100 static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr, 101 uint32_t val, int l) 102 { 103 EHCIPCIState *i = PCI_EHCI(dev); 104 bool busmaster; 105 106 pci_default_write_config(dev, addr, val, l); 107 108 if (!range_covers_byte(addr, l, PCI_COMMAND)) { 109 return; 110 } 111 busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER; 112 i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory; 113 } 114 115 static Property ehci_pci_properties[] = { 116 DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128), 117 DEFINE_PROP_END_OF_LIST(), 118 }; 119 120 static const VMStateDescription vmstate_ehci_pci = { 121 .name = "ehci", 122 .version_id = 2, 123 .minimum_version_id = 1, 124 .fields = (VMStateField[]) { 125 VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState), 126 VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState), 127 VMSTATE_END_OF_LIST() 128 } 129 }; 130 131 static void ehci_class_init(ObjectClass *klass, void *data) 132 { 133 DeviceClass *dc = DEVICE_CLASS(klass); 134 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 135 136 k->init = usb_ehci_pci_initfn; 137 k->exit = usb_ehci_pci_exit; 138 k->class_id = PCI_CLASS_SERIAL_USB; 139 k->config_write = usb_ehci_pci_write_config; 140 dc->hotpluggable = false; 141 dc->vmsd = &vmstate_ehci_pci; 142 dc->props = ehci_pci_properties; 143 } 144 145 static const TypeInfo ehci_pci_type_info = { 146 .name = TYPE_PCI_EHCI, 147 .parent = TYPE_PCI_DEVICE, 148 .instance_size = sizeof(EHCIPCIState), 149 .instance_init = usb_ehci_pci_init, 150 .abstract = true, 151 .class_init = ehci_class_init, 152 }; 153 154 static void ehci_data_class_init(ObjectClass *klass, void *data) 155 { 156 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 157 DeviceClass *dc = DEVICE_CLASS(klass); 158 EHCIPCIInfo *i = data; 159 160 k->vendor_id = i->vendor_id; 161 k->device_id = i->device_id; 162 k->revision = i->revision; 163 set_bit(DEVICE_CATEGORY_USB, dc->categories); 164 } 165 166 static struct EHCIPCIInfo ehci_pci_info[] = { 167 { 168 .name = "usb-ehci", 169 .vendor_id = PCI_VENDOR_ID_INTEL, 170 .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */ 171 .revision = 0x10, 172 },{ 173 .name = "ich9-usb-ehci1", /* 00:1d.7 */ 174 .vendor_id = PCI_VENDOR_ID_INTEL, 175 .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1, 176 .revision = 0x03, 177 },{ 178 .name = "ich9-usb-ehci2", /* 00:1a.7 */ 179 .vendor_id = PCI_VENDOR_ID_INTEL, 180 .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2, 181 .revision = 0x03, 182 } 183 }; 184 185 static void ehci_pci_register_types(void) 186 { 187 TypeInfo ehci_type_info = { 188 .parent = TYPE_PCI_EHCI, 189 .class_init = ehci_data_class_init, 190 }; 191 int i; 192 193 type_register_static(&ehci_pci_type_info); 194 195 for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) { 196 ehci_type_info.name = ehci_pci_info[i].name; 197 ehci_type_info.class_data = ehci_pci_info + i; 198 type_register(&ehci_type_info); 199 } 200 } 201 202 type_init(ehci_pci_register_types) 203 204 struct ehci_companions { 205 const char *name; 206 int func; 207 int port; 208 }; 209 210 static const struct ehci_companions ich9_1d[] = { 211 { .name = "ich9-usb-uhci1", .func = 0, .port = 0 }, 212 { .name = "ich9-usb-uhci2", .func = 1, .port = 2 }, 213 { .name = "ich9-usb-uhci3", .func = 2, .port = 4 }, 214 }; 215 216 static const struct ehci_companions ich9_1a[] = { 217 { .name = "ich9-usb-uhci4", .func = 0, .port = 0 }, 218 { .name = "ich9-usb-uhci5", .func = 1, .port = 2 }, 219 { .name = "ich9-usb-uhci6", .func = 2, .port = 4 }, 220 }; 221 222 int ehci_create_ich9_with_companions(PCIBus *bus, int slot) 223 { 224 const struct ehci_companions *comp; 225 PCIDevice *ehci, *uhci; 226 BusState *usbbus; 227 const char *name; 228 int i; 229 230 switch (slot) { 231 case 0x1d: 232 name = "ich9-usb-ehci1"; 233 comp = ich9_1d; 234 break; 235 case 0x1a: 236 name = "ich9-usb-ehci2"; 237 comp = ich9_1a; 238 break; 239 default: 240 return -1; 241 } 242 243 ehci = pci_create_multifunction(bus, PCI_DEVFN(slot, 7), true, name); 244 qdev_init_nofail(&ehci->qdev); 245 usbbus = QLIST_FIRST(&ehci->qdev.child_bus); 246 247 for (i = 0; i < 3; i++) { 248 uhci = pci_create_multifunction(bus, PCI_DEVFN(slot, comp[i].func), 249 true, comp[i].name); 250 qdev_prop_set_string(&uhci->qdev, "masterbus", usbbus->name); 251 qdev_prop_set_uint32(&uhci->qdev, "firstport", comp[i].port); 252 qdev_init_nofail(&uhci->qdev); 253 } 254 return 0; 255 } 256