1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "qemu/osdep.h"
29#include "qapi/error.h"
30#include "qemu/module.h"
31#include "hw/char/serial.h"
32#include "hw/pci/pci.h"
33
34typedef struct PCISerialState {
35 PCIDevice dev;
36 SerialState state;
37 uint8_t prog_if;
38} PCISerialState;
39
40
41static void serial_pci_realize(PCIDevice *dev, Error **errp)
42{
43 PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
44 SerialState *s = &pci->state;
45 Error *err = NULL;
46
47 s->baudbase = 115200;
48 serial_realize_core(s, &err);
49 if (err != NULL) {
50 error_propagate(errp, err);
51 return;
52 }
53
54 pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
55 pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
56 s->irq = pci_allocate_irq(&pci->dev);
57
58 memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s, "serial", 8);
59 pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
60}
61
62static void serial_pci_exit(PCIDevice *dev)
63{
64 PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
65 SerialState *s = &pci->state;
66
67 serial_exit_core(s);
68 qemu_free_irq(s->irq);
69}
70
71static const VMStateDescription vmstate_pci_serial = {
72 .name = "pci-serial",
73 .version_id = 1,
74 .minimum_version_id = 1,
75 .fields = (VMStateField[]) {
76 VMSTATE_PCI_DEVICE(dev, PCISerialState),
77 VMSTATE_STRUCT(state, PCISerialState, 0, vmstate_serial, SerialState),
78 VMSTATE_END_OF_LIST()
79 }
80};
81
82static Property serial_pci_properties[] = {
83 DEFINE_PROP_CHR("chardev", PCISerialState, state.chr),
84 DEFINE_PROP_UINT8("prog_if", PCISerialState, prog_if, 0x02),
85 DEFINE_PROP_END_OF_LIST(),
86};
87
88static void serial_pci_class_initfn(ObjectClass *klass, void *data)
89{
90 DeviceClass *dc = DEVICE_CLASS(klass);
91 PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
92 pc->realize = serial_pci_realize;
93 pc->exit = serial_pci_exit;
94 pc->vendor_id = PCI_VENDOR_ID_REDHAT;
95 pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL;
96 pc->revision = 1;
97 pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
98 dc->vmsd = &vmstate_pci_serial;
99 dc->props = serial_pci_properties;
100 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
101}
102
103static const TypeInfo serial_pci_info = {
104 .name = "pci-serial",
105 .parent = TYPE_PCI_DEVICE,
106 .instance_size = sizeof(PCISerialState),
107 .class_init = serial_pci_class_initfn,
108 .interfaces = (InterfaceInfo[]) {
109 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
110 { },
111 },
112};
113
114static void serial_pci_register_types(void)
115{
116 type_register_static(&serial_pci_info);
117}
118
119type_init(serial_pci_register_types)
120