2 * The Guest console driver
4 * Writing console drivers is one of the few remaining Dark Arts in Linux.
5 * Fortunately for us, the path of virtual consoles has been well-trodden by
6 * the PowerPC folks, who wrote "hvc_console.c" to generically support any
7 * virtual console. We use that infrastructure which only requires us to write
8 * the basic put_chars and get_chars functions and call the right register
12 /*M:002 The console can be flooded: while the Guest is processing input the
13 * Host can send more. Buffering in the Host could alleviate this, but it is a
14 * difficult problem in general. :*/
15 /* Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/err.h>
32 #include <linux/init.h>
33 #include <linux/virtio.h>
34 #include <linux/virtio_console.h>
35 #include "hvc_console.h"
37 /*D:340 These represent our input and output console queues, and the virtio
38 * operations for them. */
39 static struct virtqueue *in_vq, *out_vq;
40 static struct virtio_device *vdev;
42 /* This is our input buffer, and how much data is left in it. */
43 static unsigned int in_len;
44 static char *in, *inbuf;
46 /* The operations for our console. */
47 static struct hv_ops virtio_cons;
49 /*D:310 The put_chars() callback is pretty straightforward.
51 * We turn the characters into a scatter-gather list, add it to the output
52 * queue and then kick the Host. Then we sit here waiting for it to finish:
53 * inefficient in theory, but in practice implementations will do it
54 * immediately (lguest's Launcher does). */
55 static int put_chars(u32 vtermno, const char *buf, int count)
57 struct scatterlist sg[1];
60 /* This is a convenient routine to initialize a single-elem sg list */
61 sg_init_one(sg, buf, count);
63 /* add_buf wants a token to identify this buffer: we hand it any
64 * non-NULL pointer, since there's only ever one buffer. */
65 if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) {
66 /* Tell Host to go! */
67 out_vq->vq_ops->kick(out_vq);
68 /* Chill out until it's done with the buffer. */
69 while (!out_vq->vq_ops->get_buf(out_vq, &len))
73 /* We're expected to return the amount of data we wrote: all of it. */
77 /* Create a scatter-gather list representing our input buffer and put it in the
79 static void add_inbuf(void)
81 struct scatterlist sg[1];
82 sg_init_one(sg, inbuf, PAGE_SIZE);
84 /* We should always be able to add one buffer to an empty queue. */
85 if (in_vq->vq_ops->add_buf(in_vq, sg, 0, 1, inbuf) != 0)
87 in_vq->vq_ops->kick(in_vq);
90 /*D:350 get_chars() is the callback from the hvc_console infrastructure when
91 * an interrupt is received.
93 * Most of the code deals with the fact that the hvc_console() infrastructure
94 * only asks us for 16 bytes at a time. We keep in_offset and in_used fields
95 * for partially-filled buffers. */
96 static int get_chars(u32 vtermno, char *buf, int count)
98 /* If we don't have an input queue yet, we can't get input. */
101 /* No buffer? Try to get one. */
103 in = in_vq->vq_ops->get_buf(in_vq, &in_len);
108 /* You want more than we have to give? Well, try wanting less! */
112 /* Copy across to their buffer and increment offset. */
113 memcpy(buf, in, count);
117 /* Finished? Re-register buffer so Host will use it again. */
125 /*D:320 Console drivers are initialized very early so boot messages can go out,
126 * so we do things slightly differently from the generic virtio initialization
127 * of the net and block drivers.
129 * At this stage, the console is output-only. It's too early to set up a
130 * virtqueue, so we let the drivers do some boutique early-output thing. */
131 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
133 virtio_cons.put_chars = put_chars;
134 return hvc_instantiate(0, 0, &virtio_cons);
137 /*D:370 Once we're further in boot, we get probed like any other virtio device.
138 * At this stage we set up the output virtqueue.
140 * To set up and manage our virtual console, we call hvc_alloc(). Since we
141 * never remove the console device we never need this pointer again.
143 * Finally we put our input buffer in the input queue, ready to receive. */
144 static int __devinit virtcons_probe(struct virtio_device *dev)
147 struct hvc_struct *hvc;
151 /* This is the scratch page we use to receive console input */
152 inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
158 /* Find the input queue. */
159 /* FIXME: This is why we want to wean off hvc: we do nothing
160 * when input comes in. */
161 in_vq = vdev->config->find_vq(vdev, 0, NULL);
163 err = PTR_ERR(in_vq);
167 out_vq = vdev->config->find_vq(vdev, 1, NULL);
168 if (IS_ERR(out_vq)) {
169 err = PTR_ERR(out_vq);
173 /* Start using the new console output. */
174 virtio_cons.get_chars = get_chars;
175 virtio_cons.put_chars = put_chars;
177 /* The first argument of hvc_alloc() is the virtual console number, so
178 * we use zero. The second argument is the interrupt number; we
179 * currently leave this as zero: it would be better not to use the
180 * hvc mechanism and fix this (FIXME!).
182 * The third argument is a "struct hv_ops" containing the put_chars()
183 * and get_chars() pointers. The final argument is the output buffer
184 * size: we can do any size, so we put PAGE_SIZE here. */
185 hvc = hvc_alloc(0, 0, &virtio_cons, PAGE_SIZE);
191 /* Register the input buffer the first time. */
196 vdev->config->del_vq(out_vq);
198 vdev->config->del_vq(in_vq);
205 static struct virtio_device_id id_table[] = {
206 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
210 static struct virtio_driver virtio_console = {
211 .driver.name = KBUILD_MODNAME,
212 .driver.owner = THIS_MODULE,
213 .id_table = id_table,
214 .probe = virtcons_probe,
217 static int __init init(void)
219 return register_virtio_driver(&virtio_console);
223 MODULE_DEVICE_TABLE(virtio, id_table);
224 MODULE_DESCRIPTION("Virtio console driver");
225 MODULE_LICENSE("GPL");