From: Rusty Russell <ru...@rustcorp.com.au>

Now we can use an allocation function to remove our global console variable.

Signed-off-by: Rusty Russell <ru...@rustcorp.com.au>
Signed-off-by: Amit Shah <amit.s...@redhat.com>
---
 drivers/char/virtio_console.c |   70 +++++++++++++++++++++++++++-------------
 1 files changed, 47 insertions(+), 23 deletions(-)

diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index c133bf6..98a5249 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -32,6 +32,18 @@
  * across multiple devices and multiple ports per device.
  */
 struct ports_driver_data {
+       /*
+        * This is used to keep track of the number of hvc consoles
+        * spawned by this driver.  This number is given as the first
+        * argument to hvc_alloc().  To correctly map an initial
+        * console spawned via hvc_instantiate to the console being
+        * hooked up via hvc_alloc, we need to pass the same vtermno.
+        *
+        * We also just assume the first console being initialised was
+        * the first one that got used as the initial console.
+        */
+       unsigned int next_vtermno;
+
        /* All the console devices handled by this driver */
        struct list_head consoles;
 };
@@ -57,9 +69,6 @@ struct port {
        u32 vtermno;
 };
 
-/* We have one port ready to go immediately, for a console. */
-static struct port console;
-
 /* This is the very early arch-specified put chars function. */
 static int (*early_put_chars)(u32, const char *, int);
 
@@ -243,6 +252,31 @@ int __init virtio_cons_early_init(int (*put_chars)(u32, 
const char *, int))
        return hvc_instantiate(0, 0, &hv_ops);
 }
 
+static struct port *__devinit add_port(u32 vtermno)
+{
+       struct port *port;
+
+       port = kzalloc(sizeof *port, GFP_KERNEL);
+       if (!port)
+               return NULL;
+
+       port->used_len = port->offset = 0;
+       port->inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+       if (!port->inbuf) {
+               kfree(port);
+               return NULL;
+       }
+       port->hvc = NULL;
+       port->vtermno = vtermno;
+       return port;
+}
+
+static void free_port(struct port *port)
+{
+       kfree(port->inbuf);
+       kfree(port);
+}
+
 /*
  * Once we're further in boot, we get probed like any other virtio
  * device.  At this stage we set up the output virtqueue.
@@ -262,25 +296,15 @@ static int __devinit virtcons_probe(struct virtio_device 
*vdev)
        struct port *port;
        int err;
 
-       port = &console;
-       if (port->vdev) {
-               dev_warn(&port->vdev->dev,
-                        "Multiple virtio-console devices not supported yet\n");
-               return -EEXIST;
-       }
+       err = -ENOMEM;
+       port = add_port(pdrvdata.next_vtermno);
+       if (!port)
+               goto fail;
 
        /* Attach this port to this virtio_device, and vice-versa. */
        port->vdev = vdev;
        vdev->priv = port;
 
-       /* This is the scratch page we use to receive console input */
-       port->used_len = 0;
-       port->inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       if (!port->inbuf) {
-               err = -ENOMEM;
-               goto fail;
-       }
-
        /* Find the queues. */
        err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
        if (err)
@@ -291,17 +315,16 @@ static int __devinit virtcons_probe(struct virtio_device 
*vdev)
 
        /*
         * The first argument of hvc_alloc() is the virtual console
-        * number, so we use zero.  The second argument is the
-        * parameter for the notification mechanism (like irq
-        * number). We currently leave this as zero, virtqueues have
-        * implicit notifications.
+        * number.  The second argument is the parameter for the
+        * notification mechanism (like irq number).  We currently
+        * leave this as zero, virtqueues have implicit notifications.
         *
         * The third argument is a "struct hv_ops" containing the
         * put_chars(), get_chars(), notifier_add() and notifier_del()
         * pointers.  The final argument is the output buffer size: we
         * can do any size, so we put PAGE_SIZE here.
         */
-       port->hvc = hvc_alloc(0, 0, &hv_ops, PAGE_SIZE);
+       port->hvc = hvc_alloc(port->vtermno, 0, &hv_ops, PAGE_SIZE);
        if (IS_ERR(port->hvc)) {
                err = PTR_ERR(port->hvc);
                goto free_vqs;
@@ -309,6 +332,7 @@ static int __devinit virtcons_probe(struct virtio_device 
*vdev)
 
        /* Add to vtermno list. */
        spin_lock_irq(&pdrvdata_lock);
+       pdrvdata.next_vtermno++;
        list_add(&port->list, &pdrvdata.consoles);
        spin_unlock_irq(&pdrvdata_lock);
 
@@ -322,7 +346,7 @@ static int __devinit virtcons_probe(struct virtio_device 
*vdev)
 free_vqs:
        vdev->config->del_vqs(vdev);
 free:
-       kfree(port->inbuf);
+       free_port(port);
 fail:
        return err;
 }
-- 
1.6.2.5

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/virtualization

Reply via email to