virtio-console.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Virtio Console and Generic Serial Port Devices
  3. *
  4. * Copyright Red Hat, Inc. 2009, 2010
  5. *
  6. * Authors:
  7. * Amit Shah <amit.shah@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. */
  12. #include "qemu-char.h"
  13. #include "qemu-error.h"
  14. #include "trace.h"
  15. #include "virtio-serial.h"
  16. typedef struct VirtConsole {
  17. VirtIOSerialPort port;
  18. CharDriverState *chr;
  19. } VirtConsole;
  20. /* Callback function that's called when the guest sends us data */
  21. static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
  22. {
  23. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  24. ssize_t ret;
  25. ret = qemu_chr_write(vcon->chr, buf, len);
  26. trace_virtio_console_flush_buf(port->id, len, ret);
  27. if (ret < 0) {
  28. /*
  29. * Ideally we'd get a better error code than just -1, but
  30. * that's what the chardev interface gives us right now. If
  31. * we had a finer-grained message, like -EPIPE, we could close
  32. * this connection. Absent such error messages, the most we
  33. * can do is to return 0 here.
  34. *
  35. * This will prevent stray -1 values to go to
  36. * virtio-serial-bus.c and cause abort()s in
  37. * do_flush_queued_data().
  38. */
  39. ret = 0;
  40. }
  41. return ret;
  42. }
  43. /* Callback function that's called when the guest opens the port */
  44. static void guest_open(VirtIOSerialPort *port)
  45. {
  46. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  47. qemu_chr_guest_open(vcon->chr);
  48. }
  49. /* Callback function that's called when the guest closes the port */
  50. static void guest_close(VirtIOSerialPort *port)
  51. {
  52. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  53. qemu_chr_guest_close(vcon->chr);
  54. }
  55. /* Readiness of the guest to accept data on a port */
  56. static int chr_can_read(void *opaque)
  57. {
  58. VirtConsole *vcon = opaque;
  59. return virtio_serial_guest_ready(&vcon->port);
  60. }
  61. /* Send data from a char device over to the guest */
  62. static void chr_read(void *opaque, const uint8_t *buf, int size)
  63. {
  64. VirtConsole *vcon = opaque;
  65. trace_virtio_console_chr_read(vcon->port.id, size);
  66. virtio_serial_write(&vcon->port, buf, size);
  67. }
  68. static void chr_event(void *opaque, int event)
  69. {
  70. VirtConsole *vcon = opaque;
  71. trace_virtio_console_chr_event(vcon->port.id, event);
  72. switch (event) {
  73. case CHR_EVENT_OPENED:
  74. virtio_serial_open(&vcon->port);
  75. break;
  76. case CHR_EVENT_CLOSED:
  77. virtio_serial_close(&vcon->port);
  78. break;
  79. }
  80. }
  81. static int virtconsole_initfn(VirtIOSerialPort *port)
  82. {
  83. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  84. VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
  85. vcon->port.dev.info);
  86. if (port->id == 0 && !info->is_console) {
  87. error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
  88. return -1;
  89. }
  90. if (vcon->chr) {
  91. qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
  92. vcon);
  93. info->have_data = flush_buf;
  94. info->guest_open = guest_open;
  95. info->guest_close = guest_close;
  96. }
  97. return 0;
  98. }
  99. static int virtconsole_exitfn(VirtIOSerialPort *port)
  100. {
  101. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  102. if (vcon->chr) {
  103. /*
  104. * Instead of closing the chardev, free it so it can be used
  105. * for other purposes.
  106. */
  107. qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
  108. }
  109. return 0;
  110. }
  111. static VirtIOSerialPortInfo virtconsole_info = {
  112. .qdev.name = "virtconsole",
  113. .qdev.size = sizeof(VirtConsole),
  114. .is_console = true,
  115. .init = virtconsole_initfn,
  116. .exit = virtconsole_exitfn,
  117. .qdev.props = (Property[]) {
  118. DEFINE_PROP_CHR("chardev", VirtConsole, chr),
  119. DEFINE_PROP_END_OF_LIST(),
  120. },
  121. };
  122. static void virtconsole_register(void)
  123. {
  124. virtio_serial_port_qdev_register(&virtconsole_info);
  125. }
  126. device_init(virtconsole_register)
  127. static VirtIOSerialPortInfo virtserialport_info = {
  128. .qdev.name = "virtserialport",
  129. .qdev.size = sizeof(VirtConsole),
  130. .init = virtconsole_initfn,
  131. .exit = virtconsole_exitfn,
  132. .qdev.props = (Property[]) {
  133. DEFINE_PROP_CHR("chardev", VirtConsole, chr),
  134. DEFINE_PROP_END_OF_LIST(),
  135. },
  136. };
  137. static void virtserialport_register(void)
  138. {
  139. virtio_serial_port_qdev_register(&virtserialport_info);
  140. }
  141. device_init(virtserialport_register)