2
0

hcd-ehci-pci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.1 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 Lesser General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/qdev-properties.h"
  19. #include "hw/usb/hcd-ehci.h"
  20. #include "migration/vmstate.h"
  21. #include "qemu/module.h"
  22. #include "qemu/range.h"
  23. typedef struct EHCIPCIInfo {
  24. const char *name;
  25. uint16_t vendor_id;
  26. uint16_t device_id;
  27. uint8_t revision;
  28. bool companion;
  29. } EHCIPCIInfo;
  30. static void usb_ehci_pci_realize(PCIDevice *dev, Error **errp)
  31. {
  32. EHCIPCIState *i = PCI_EHCI(dev);
  33. EHCIState *s = &i->ehci;
  34. uint8_t *pci_conf = dev->config;
  35. pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20);
  36. /* capabilities pointer */
  37. pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00);
  38. /* pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); */
  39. pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */
  40. pci_set_byte(&pci_conf[PCI_MIN_GNT], 0);
  41. pci_set_byte(&pci_conf[PCI_MAX_LAT], 0);
  42. /* pci_conf[0x50] = 0x01; *//* power management caps */
  43. pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); /* release # (2.1.4) */
  44. pci_set_byte(&pci_conf[0x61], 0x20); /* frame length adjustment (2.1.5) */
  45. pci_set_word(&pci_conf[0x62], 0x00); /* port wake up capability (2.1.6) */
  46. pci_conf[0x64] = 0x00;
  47. pci_conf[0x65] = 0x00;
  48. pci_conf[0x66] = 0x00;
  49. pci_conf[0x67] = 0x00;
  50. pci_conf[0x68] = 0x01;
  51. pci_conf[0x69] = 0x00;
  52. pci_conf[0x6a] = 0x00;
  53. pci_conf[0x6b] = 0x00; /* USBLEGSUP */
  54. pci_conf[0x6c] = 0x00;
  55. pci_conf[0x6d] = 0x00;
  56. pci_conf[0x6e] = 0x00;
  57. pci_conf[0x6f] = 0xc0; /* USBLEFCTLSTS */
  58. s->irq = pci_allocate_irq(dev);
  59. s->as = pci_get_address_space(dev);
  60. usb_ehci_realize(s, DEVICE(dev), NULL);
  61. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
  62. }
  63. static void usb_ehci_pci_init(Object *obj)
  64. {
  65. DeviceClass *dc = DEVICE_GET_CLASS(obj);
  66. EHCIPCIState *i = PCI_EHCI(obj);
  67. EHCIState *s = &i->ehci;
  68. s->caps[0x09] = 0x68; /* EECP */
  69. s->capsbase = 0x00;
  70. s->opregbase = 0x20;
  71. s->portscbase = 0x44;
  72. s->portnr = EHCI_PORTS;
  73. if (!dc->hotpluggable) {
  74. s->companion_enable = true;
  75. }
  76. usb_ehci_init(s, DEVICE(obj));
  77. }
  78. static void usb_ehci_pci_finalize(Object *obj)
  79. {
  80. EHCIPCIState *i = PCI_EHCI(obj);
  81. EHCIState *s = &i->ehci;
  82. usb_ehci_finalize(s);
  83. }
  84. static void usb_ehci_pci_exit(PCIDevice *dev)
  85. {
  86. EHCIPCIState *i = PCI_EHCI(dev);
  87. EHCIState *s = &i->ehci;
  88. usb_ehci_unrealize(s, DEVICE(dev));
  89. g_free(s->irq);
  90. s->irq = NULL;
  91. }
  92. static void usb_ehci_pci_reset(DeviceState *dev)
  93. {
  94. PCIDevice *pci_dev = PCI_DEVICE(dev);
  95. EHCIPCIState *i = PCI_EHCI(pci_dev);
  96. EHCIState *s = &i->ehci;
  97. ehci_reset(s);
  98. }
  99. static void usb_ehci_pci_write_config(PCIDevice *dev, uint32_t addr,
  100. uint32_t val, int l)
  101. {
  102. EHCIPCIState *i = PCI_EHCI(dev);
  103. bool busmaster;
  104. pci_default_write_config(dev, addr, val, l);
  105. if (!range_covers_byte(addr, l, PCI_COMMAND)) {
  106. return;
  107. }
  108. busmaster = pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_MASTER;
  109. i->ehci.as = busmaster ? pci_get_address_space(dev) : &address_space_memory;
  110. }
  111. static const Property ehci_pci_properties[] = {
  112. DEFINE_PROP_UINT32("maxframes", EHCIPCIState, ehci.maxframes, 128),
  113. };
  114. static const VMStateDescription vmstate_ehci_pci = {
  115. .name = "ehci",
  116. .version_id = 2,
  117. .minimum_version_id = 1,
  118. .fields = (const VMStateField[]) {
  119. VMSTATE_PCI_DEVICE(pcidev, EHCIPCIState),
  120. VMSTATE_STRUCT(ehci, EHCIPCIState, 2, vmstate_ehci, EHCIState),
  121. VMSTATE_END_OF_LIST()
  122. }
  123. };
  124. static void ehci_class_init(ObjectClass *klass, void *data)
  125. {
  126. DeviceClass *dc = DEVICE_CLASS(klass);
  127. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  128. k->realize = usb_ehci_pci_realize;
  129. k->exit = usb_ehci_pci_exit;
  130. k->class_id = PCI_CLASS_SERIAL_USB;
  131. k->config_write = usb_ehci_pci_write_config;
  132. dc->vmsd = &vmstate_ehci_pci;
  133. device_class_set_props(dc, ehci_pci_properties);
  134. device_class_set_legacy_reset(dc, usb_ehci_pci_reset);
  135. }
  136. static const TypeInfo ehci_pci_type_info = {
  137. .name = TYPE_PCI_EHCI,
  138. .parent = TYPE_PCI_DEVICE,
  139. .instance_size = sizeof(EHCIPCIState),
  140. .instance_init = usb_ehci_pci_init,
  141. .instance_finalize = usb_ehci_pci_finalize,
  142. .abstract = true,
  143. .class_init = ehci_class_init,
  144. .interfaces = (InterfaceInfo[]) {
  145. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  146. { },
  147. },
  148. };
  149. static void ehci_data_class_init(ObjectClass *klass, void *data)
  150. {
  151. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. const EHCIPCIInfo *i = data;
  154. k->vendor_id = i->vendor_id;
  155. k->device_id = i->device_id;
  156. k->revision = i->revision;
  157. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  158. if (i->companion) {
  159. dc->hotpluggable = false;
  160. }
  161. }
  162. static struct EHCIPCIInfo ehci_pci_info[] = {
  163. {
  164. .name = "usb-ehci",
  165. .vendor_id = PCI_VENDOR_ID_INTEL,
  166. .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
  167. .revision = 0x10,
  168. },{
  169. .name = "ich9-usb-ehci1", /* 00:1d.7 */
  170. .vendor_id = PCI_VENDOR_ID_INTEL,
  171. .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
  172. .revision = 0x03,
  173. .companion = true,
  174. },{
  175. .name = "ich9-usb-ehci2", /* 00:1a.7 */
  176. .vendor_id = PCI_VENDOR_ID_INTEL,
  177. .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
  178. .revision = 0x03,
  179. .companion = true,
  180. }
  181. };
  182. static void ehci_pci_register_types(void)
  183. {
  184. TypeInfo ehci_type_info = {
  185. .parent = TYPE_PCI_EHCI,
  186. .class_init = ehci_data_class_init,
  187. };
  188. int i;
  189. type_register_static(&ehci_pci_type_info);
  190. for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) {
  191. ehci_type_info.name = ehci_pci_info[i].name;
  192. ehci_type_info.class_data = ehci_pci_info + i;
  193. type_register_static(&ehci_type_info);
  194. }
  195. }
  196. type_init(ehci_pci_register_types)