| 1 | /* |
| 2 | * Virtio serial PCI Bindings |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2007 |
| 5 | * Copyright (c) 2009 CodeSourcery |
| 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Paul Brook <paul@codesourcery.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
| 14 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 15 | * GNU GPL, version 2 or (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include "qemu/osdep.h" |
| 19 | |
| 20 | #include "hw/core/qdev-properties.h" |
| 21 | #include "hw/virtio/virtio-serial.h" |
| 22 | #include "qemu/module.h" |
| 23 | #include "hw/virtio/virtio-pci.h" |
| 24 | #include "qom/object.h" |
| 25 | |
| 26 | typedef struct VirtIOSerialPCI VirtIOSerialPCI; |
| 27 | |
| 28 | /* |
| 29 | * virtio-serial-pci: This extends VirtioPCIProxy. |
| 30 | */ |
| 31 | #define TYPE_VIRTIO_SERIAL_PCI "virtio-serial-pci-base" |
| 32 | DECLARE_INSTANCE_CHECKER(VirtIOSerialPCI, VIRTIO_SERIAL_PCI, |
| 33 | TYPE_VIRTIO_SERIAL_PCI) |
| 34 | |
| 35 | struct VirtIOSerialPCI { |
| 36 | VirtIOPCIProxy parent_obj; |
| 37 | VirtIOSerial vdev; |
| 38 | }; |
| 39 | |
| 40 | static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 41 | { |
| 42 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); |
| 43 | DeviceState *vdev = DEVICE(&dev->vdev); |
| 44 | DeviceState *proxy = DEVICE(vpci_dev); |
| 45 | char *bus_name; |
| 46 | |
| 47 | if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && |
| 48 | vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ |
| 49 | vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ |
| 50 | vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; |
| 51 | } |
| 52 | |
| 53 | /* backwards-compatibility with machines that were created with |
| 54 | DEV_NVECTORS_UNSPECIFIED */ |
| 55 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { |
| 56 | vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * For command line compatibility, this sets the virtio-serial-device bus |
| 61 | * name as before. |
| 62 | */ |
| 63 | if (proxy->id) { |
| 64 | bus_name = g_strdup_printf("%s.0", proxy->id); |
| 65 | virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); |
| 66 | g_free(bus_name); |
| 67 | } |
| 68 | |
| 69 | qdev_realize(vdev, BUS(&vpci_dev->bus), errp); |
| 70 | } |
| 71 | |
| 72 | static const Property virtio_serial_pci_properties[] = { |
| 73 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, |
| 74 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
| 75 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
| 76 | DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), |
| 77 | }; |
| 78 | |
| 79 | static void virtio_serial_pci_class_init(ObjectClass *klass, const void *data) |
| 80 | { |
| 81 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 82 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 83 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 84 | k->realize = virtio_serial_pci_realize; |
| 85 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
| 86 | device_class_set_props(dc, virtio_serial_pci_properties); |
| 87 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
| 88 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; |
| 89 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; |
| 90 | pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; |
| 91 | } |
| 92 | |
| 93 | static void virtio_serial_pci_instance_init(Object *obj) |
| 94 | { |
| 95 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); |
| 96 | |
| 97 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 98 | TYPE_VIRTIO_SERIAL); |
| 99 | } |
| 100 | |
| 101 | static const VirtioPCIDeviceTypeInfo virtio_serial_pci_info = { |
| 102 | .base_name = TYPE_VIRTIO_SERIAL_PCI, |
| 103 | .generic_name = "virtio-serial-pci", |
| 104 | .transitional_name = "virtio-serial-pci-transitional", |
| 105 | .non_transitional_name = "virtio-serial-pci-non-transitional", |
| 106 | .instance_size = sizeof(VirtIOSerialPCI), |
| 107 | .instance_init = virtio_serial_pci_instance_init, |
| 108 | .class_init = virtio_serial_pci_class_init, |
| 109 | }; |
| 110 | |
| 111 | static void virtio_serial_pci_register(void) |
| 112 | { |
| 113 | virtio_pci_types_register(&virtio_serial_pci_info); |
| 114 | } |
| 115 | |
| 116 | type_init(virtio_serial_pci_register) |