qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Amit Shah <amit.shah@redhat.com>
To: qemu-devel@nongnu.org
Cc: Amit Shah <amit.shah@redhat.com>
Subject: [Qemu-devel] [PATCH 4/4] virtio-console: Add a in-qemu api for open/read/write/close ports
Date: Tue, 22 Sep 2009 21:53:47 +0530	[thread overview]
Message-ID: <1253636627-12746-5-git-send-email-amit.shah@redhat.com> (raw)
In-Reply-To: <1253636627-12746-4-git-send-email-amit.shah@redhat.com>

This is a simple-to-use api for opening a port, registering
a callback for read, writing to a port and closing it.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 hw/virtio-console.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++--
 hw/virtio-console.h |    5 ++++
 2 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/hw/virtio-console.c b/hw/virtio-console.c
index d750c8e..c41f13c 100644
--- a/hw/virtio-console.c
+++ b/hw/virtio-console.c
@@ -58,6 +58,11 @@ struct VirtIOConsolePort {
 
     QTAILQ_HEAD(, VirtIOConsolePortBuffer) unflushed_buffer_head;
 
+    /* Callback that's invoked when we have a buffer that can be consumed
+     * by an in-qemu user of this port
+     */
+    size_t (*read_callback)(const uint8_t *buf, const size_t len);
+
     bool guest_connected;
     bool host_connected;
 };
@@ -128,10 +133,15 @@ static bool has_complete_data(VirtIOConsolePort *port)
 
 static size_t flush_buf(VirtIOConsolePort *port, const uint8_t *buf, size_t len)
 {
-    if (!port->hd) {
-        return 0;
+    int ret;
+
+    ret = 0;
+    if (port->read_callback) {
+        ret = port->read_callback(buf, len);
+    } else if (port->hd) {
+        ret = qemu_chr_write(port->hd, buf, len);
     }
-    return qemu_chr_write(port->hd, buf, len);
+    return ret;
 }
 
 static void flush_queue(VirtIOConsolePort *port)
@@ -443,6 +453,57 @@ static void vcon_event(void *opaque, int event)
     send_control_event(port, &cpkt);
 }
 
+
+/* Functions for use inside qemu to open and read from/write to ports */
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len))
+{
+    VirtIOConsolePort *port;
+    struct virtio_console_control cpkt;
+
+    port = get_port_from_id(virtio_console, id);
+    if (!port) {
+        return NULL;
+    }
+    /* Don't allow opening an already-open port */
+    if (port->host_connected) {
+        return NULL;
+    }
+    port->read_callback = read_callback;
+
+    /* Send port open notification to the guest */
+    port->host_connected = true;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 1;
+    send_control_event(port, &cpkt);
+
+    return port;
+}
+
+void virtio_console_close(VirtIOConsolePort *port)
+{
+    struct virtio_console_control cpkt;
+
+    if (!port)
+        return;
+
+    port->read_callback = NULL;
+
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 0;
+    send_control_event(port, &cpkt);
+}
+
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size)
+{
+    if (!port || !port->host_connected) {
+        return 0;
+    }
+    return write_to_port(port, buf, size, false);
+}
+
+
 static void set_active_in_map(uint32_t *map, uint32_t idx)
 {
     int i;
diff --git a/hw/virtio-console.h b/hw/virtio-console.h
index 526713e..5f0f549 100644
--- a/hw/virtio-console.h
+++ b/hw/virtio-console.h
@@ -88,5 +88,10 @@ VirtConBus *virtcon_bus_new(DeviceState *dev);
 typedef struct VirtIOConsolePort VirtIOConsolePort;
 void virtio_console_monitor_command(Monitor *mon,
                                     const char *command, const char *param);
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len));
+void virtio_console_close(VirtIOConsolePort *port);
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size);
 
 #endif
-- 
1.6.2.5

  reply	other threads:[~2009-09-22 16:24 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-22 16:23 [Qemu-devel] Multiple port support for virtio-console Amit Shah
2009-09-22 16:23 ` [Qemu-devel] [PATCH 1/4] char: Emit 'OPENED' events on char device open Amit Shah
2009-09-22 16:23   ` [Qemu-devel] [PATCH 2/4] qdev: add string property Amit Shah
2009-09-22 16:23     ` [Qemu-devel] [PATCH 3/4] virtio-console: Add support for multiple ports for generic guest-host communication Amit Shah
2009-09-22 16:23       ` Amit Shah [this message]
2009-09-23  9:12         ` [Qemu-devel] [PATCH 4/4] virtio-console: Add a in-qemu api for open/read/write/close ports Gerd Hoffmann
2009-09-23  9:07       ` [Qemu-devel] [PATCH 3/4] virtio-console: Add support for multiple ports for generic guest-host communication Gerd Hoffmann
2009-09-23  9:43         ` Amit Shah
2009-09-23 11:20           ` Gerd Hoffmann
2009-09-23 11:50             ` Amit Shah
2009-09-23 12:30               ` Gerd Hoffmann
2009-09-23 12:40                 ` Amit Shah
2009-09-23 13:04                   ` Gerd Hoffmann
2009-09-23 13:17                     ` Amit Shah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1253636627-12746-5-git-send-email-amit.shah@redhat.com \
    --to=amit.shah@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).