serial-pci.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * QEMU 16550A UART emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2008 Citrix Systems, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /* see docs/specs/pci-serial.txt */
  26. #include "qemu/osdep.h"
  27. #include "qapi/error.h"
  28. #include "qemu/module.h"
  29. #include "hw/char/serial.h"
  30. #include "hw/irq.h"
  31. #include "hw/pci/pci.h"
  32. #include "hw/qdev-properties.h"
  33. #include "migration/vmstate.h"
  34. typedef struct PCISerialState {
  35. PCIDevice dev;
  36. SerialState state;
  37. uint8_t prog_if;
  38. } PCISerialState;
  39. static void serial_pci_realize(PCIDevice *dev, Error **errp)
  40. {
  41. PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
  42. SerialState *s = &pci->state;
  43. Error *err = NULL;
  44. s->baudbase = 115200;
  45. serial_realize_core(s, &err);
  46. if (err != NULL) {
  47. error_propagate(errp, err);
  48. return;
  49. }
  50. pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
  51. pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
  52. s->irq = pci_allocate_irq(&pci->dev);
  53. memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s, "serial", 8);
  54. pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
  55. }
  56. static void serial_pci_exit(PCIDevice *dev)
  57. {
  58. PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
  59. SerialState *s = &pci->state;
  60. serial_exit_core(s);
  61. qemu_free_irq(s->irq);
  62. }
  63. static const VMStateDescription vmstate_pci_serial = {
  64. .name = "pci-serial",
  65. .version_id = 1,
  66. .minimum_version_id = 1,
  67. .fields = (VMStateField[]) {
  68. VMSTATE_PCI_DEVICE(dev, PCISerialState),
  69. VMSTATE_STRUCT(state, PCISerialState, 0, vmstate_serial, SerialState),
  70. VMSTATE_END_OF_LIST()
  71. }
  72. };
  73. static Property serial_pci_properties[] = {
  74. DEFINE_PROP_CHR("chardev", PCISerialState, state.chr),
  75. DEFINE_PROP_UINT8("prog_if", PCISerialState, prog_if, 0x02),
  76. DEFINE_PROP_END_OF_LIST(),
  77. };
  78. static void serial_pci_class_initfn(ObjectClass *klass, void *data)
  79. {
  80. DeviceClass *dc = DEVICE_CLASS(klass);
  81. PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
  82. pc->realize = serial_pci_realize;
  83. pc->exit = serial_pci_exit;
  84. pc->vendor_id = PCI_VENDOR_ID_REDHAT;
  85. pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL;
  86. pc->revision = 1;
  87. pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
  88. dc->vmsd = &vmstate_pci_serial;
  89. dc->props = serial_pci_properties;
  90. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  91. }
  92. static const TypeInfo serial_pci_info = {
  93. .name = "pci-serial",
  94. .parent = TYPE_PCI_DEVICE,
  95. .instance_size = sizeof(PCISerialState),
  96. .class_init = serial_pci_class_initfn,
  97. .interfaces = (InterfaceInfo[]) {
  98. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  99. { },
  100. },
  101. };
  102. static void serial_pci_register_types(void)
  103. {
  104. type_register_static(&serial_pci_info);
  105. }
  106. type_init(serial_pci_register_types)