hcd-ohci-pci.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * QEMU USB OHCI Emulation
  3. * Copyright (c) 2004 Gianni Tedesco
  4. * Copyright (c) 2006 CodeSourcery
  5. * Copyright (c) 2006 Openedhand Ltd.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/timer.h"
  23. #include "hw/usb.h"
  24. #include "migration/vmstate.h"
  25. #include "hw/pci/pci_device.h"
  26. #include "hw/sysbus.h"
  27. #include "hw/qdev-dma.h"
  28. #include "hw/qdev-properties.h"
  29. #include "trace.h"
  30. #include "hcd-ohci.h"
  31. #include "qom/object.h"
  32. #define TYPE_PCI_OHCI "pci-ohci"
  33. OBJECT_DECLARE_SIMPLE_TYPE(OHCIPCIState, PCI_OHCI)
  34. struct OHCIPCIState {
  35. /*< private >*/
  36. PCIDevice parent_obj;
  37. /*< public >*/
  38. OHCIState state;
  39. char *masterbus;
  40. uint32_t num_ports;
  41. uint32_t firstport;
  42. };
  43. /**
  44. * A typical PCI OHCI will additionally set PERR in its configspace to
  45. * signal that it got an error.
  46. */
  47. static void ohci_pci_die(struct OHCIState *ohci)
  48. {
  49. OHCIPCIState *dev = container_of(ohci, OHCIPCIState, state);
  50. ohci_sysbus_die(ohci);
  51. pci_set_word(dev->parent_obj.config + PCI_STATUS,
  52. PCI_STATUS_DETECTED_PARITY);
  53. }
  54. static void usb_ohci_realize_pci(PCIDevice *dev, Error **errp)
  55. {
  56. Error *err = NULL;
  57. OHCIPCIState *ohci = PCI_OHCI(dev);
  58. dev->config[PCI_CLASS_PROG] = 0x10; /* OHCI */
  59. dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
  60. usb_ohci_init(&ohci->state, DEVICE(dev), ohci->num_ports, 0,
  61. ohci->masterbus, ohci->firstport,
  62. pci_get_address_space(dev), ohci_pci_die, &err);
  63. if (err) {
  64. error_propagate(errp, err);
  65. return;
  66. }
  67. ohci->state.irq = pci_allocate_irq(dev);
  68. pci_register_bar(dev, 0, 0, &ohci->state.mem);
  69. }
  70. static void usb_ohci_exit(PCIDevice *dev)
  71. {
  72. OHCIPCIState *ohci = PCI_OHCI(dev);
  73. OHCIState *s = &ohci->state;
  74. trace_usb_ohci_exit(s->name);
  75. ohci_bus_stop(s);
  76. if (s->async_td) {
  77. usb_cancel_packet(&s->usb_packet);
  78. s->async_td = 0;
  79. }
  80. ohci_stop_endpoints(s);
  81. if (!ohci->masterbus) {
  82. usb_bus_release(&s->bus);
  83. }
  84. timer_free(s->eof_timer);
  85. }
  86. static void usb_ohci_reset_pci(DeviceState *d)
  87. {
  88. PCIDevice *dev = PCI_DEVICE(d);
  89. OHCIPCIState *ohci = PCI_OHCI(dev);
  90. OHCIState *s = &ohci->state;
  91. ohci_hard_reset(s);
  92. }
  93. static const Property ohci_pci_properties[] = {
  94. DEFINE_PROP_STRING("masterbus", OHCIPCIState, masterbus),
  95. DEFINE_PROP_UINT32("num-ports", OHCIPCIState, num_ports, 3),
  96. DEFINE_PROP_UINT32("firstport", OHCIPCIState, firstport, 0),
  97. };
  98. static const VMStateDescription vmstate_ohci = {
  99. .name = "ohci",
  100. .version_id = 1,
  101. .minimum_version_id = 1,
  102. .fields = (const VMStateField[]) {
  103. VMSTATE_PCI_DEVICE(parent_obj, OHCIPCIState),
  104. VMSTATE_STRUCT(state, OHCIPCIState, 1, vmstate_ohci_state, OHCIState),
  105. VMSTATE_END_OF_LIST()
  106. }
  107. };
  108. static void ohci_pci_class_init(ObjectClass *klass, void *data)
  109. {
  110. DeviceClass *dc = DEVICE_CLASS(klass);
  111. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  112. k->realize = usb_ohci_realize_pci;
  113. k->exit = usb_ohci_exit;
  114. k->vendor_id = PCI_VENDOR_ID_APPLE;
  115. k->device_id = PCI_DEVICE_ID_APPLE_IPID_USB;
  116. k->class_id = PCI_CLASS_SERIAL_USB;
  117. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  118. dc->desc = "Apple USB Controller";
  119. device_class_set_props(dc, ohci_pci_properties);
  120. dc->hotpluggable = false;
  121. dc->vmsd = &vmstate_ohci;
  122. device_class_set_legacy_reset(dc, usb_ohci_reset_pci);
  123. }
  124. static const TypeInfo ohci_pci_info = {
  125. .name = TYPE_PCI_OHCI,
  126. .parent = TYPE_PCI_DEVICE,
  127. .instance_size = sizeof(OHCIPCIState),
  128. .class_init = ohci_pci_class_init,
  129. .interfaces = (InterfaceInfo[]) {
  130. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  131. { },
  132. },
  133. };
  134. static void ohci_pci_register_types(void)
  135. {
  136. type_register_static(&ohci_pci_info);
  137. }
  138. type_init(ohci_pci_register_types)