]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/hvc_lguest.c
1de8967cce0690a4f71aed21986adc5fceb8c15f
[linux-2.6-omap-h63xx.git] / drivers / char / hvc_lguest.c
1 /*D:300
2  * The Guest console driver
3  *
4  * This is a trivial console driver: we use lguest's DMA mechanism to send
5  * bytes out, and register a DMA buffer to receive bytes in.  It is assumed to
6  * be present and available from the very beginning of boot.
7  *
8  * Writing console drivers is one of the few remaining Dark Arts in Linux.
9  * Fortunately for us, the path of virtual consoles has been well-trodden by
10  * the PowerPC folks, who wrote "hvc_console.c" to generically support any
11  * virtual console.  We use that infrastructure which only requires us to write
12  * the basic put_chars and get_chars functions and call the right register
13  * functions.
14  :*/
15
16 /* Copyright (C) 2006 Rusty Russell, IBM Corporation
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32 #include <linux/err.h>
33 #include <linux/init.h>
34 #include <linux/lguest_bus.h>
35 #include "hvc_console.h"
36
37 /*D:340 This is our single console input buffer, with associated "struct
38  * lguest_dma" referring to it.  Note the 0-terminated length array, and the
39  * use of physical address for the buffer itself. */
40 static char inbuf[256];
41 static struct lguest_dma cons_input = { .used_len = 0,
42                                         .addr[0] = __pa(inbuf),
43                                         .len[0] = sizeof(inbuf),
44                                         .len[1] = 0 };
45
46 /*D:310 The put_chars() callback is pretty straightforward.
47  *
48  * First we put the pointer and length in a "struct lguest_dma": we only have
49  * one pointer, so we set the second length to 0.  Then we use SEND_DMA to send
50  * the data to (Host) buffers attached to the console key.  Usually a device's
51  * key is a physical address within the device's memory, but because the
52  * console device doesn't have any associated physical memory, we use the
53  * LGUEST_CONSOLE_DMA_KEY constant (aka 0). */
54 static int put_chars(u32 vtermno, const char *buf, int count)
55 {
56         struct lguest_dma dma;
57
58         /* FIXME: DMA buffers in a "struct lguest_dma" are not allowed
59          * to go over page boundaries.  This never seems to happen,
60          * but if it did we'd need to fix this code. */
61         dma.len[0] = count;
62         dma.len[1] = 0;
63         dma.addr[0] = __pa(buf);
64
65         lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
66         /* We're expected to return the amount of data we wrote: all of it. */
67         return count;
68 }
69
70 /*D:350 get_chars() is the callback from the hvc_console infrastructure when
71  * an interrupt is received.
72  *
73  * Firstly we see if our buffer has been filled: if not, we return.  The rest
74  * of the code deals with the fact that the hvc_console() infrastructure only
75  * asks us for 16 bytes at a time.  We keep a "cons_offset" variable for
76  * partially-read buffers. */
77 static int get_chars(u32 vtermno, char *buf, int count)
78 {
79         static int cons_offset;
80
81         /* Nothing left to see here... */
82         if (!cons_input.used_len)
83                 return 0;
84
85         /* You want more than we have to give?  Well, try wanting less! */
86         if (cons_input.used_len - cons_offset < count)
87                 count = cons_input.used_len - cons_offset;
88
89         /* Copy across to their buffer and increment offset. */
90         memcpy(buf, inbuf + cons_offset, count);
91         cons_offset += count;
92
93         /* Finished?  Zero offset, and reset cons_input so Host will use it
94          * again. */
95         if (cons_offset == cons_input.used_len) {
96                 cons_offset = 0;
97                 cons_input.used_len = 0;
98         }
99         return count;
100 }
101 /*:*/
102
103 static struct hv_ops lguest_cons = {
104         .get_chars = get_chars,
105         .put_chars = put_chars,
106 };
107
108 /*D:320 Console drivers are initialized very early so boot messages can go
109  * out.  At this stage, the console is output-only.  Our driver checks we're a
110  * Guest, and if so hands hvc_instantiate() the console number (0), priority
111  * (0), and the struct hv_ops containing the put_chars() function. */
112 static int __init cons_init(void)
113 {
114         if (strcmp(paravirt_ops.name, "lguest") != 0)
115                 return 0;
116
117         return hvc_instantiate(0, 0, &lguest_cons);
118 }
119 console_initcall(cons_init);
120
121 /*D:370 To set up and manage our virtual console, we call hvc_alloc() and
122  * stash the result in the private pointer of the "struct lguest_device".
123  * Since we never remove the console device we never need this pointer again,
124  * but using ->private is considered good form, and you never know who's going
125  * to copy your driver.
126  *
127  * Once the console is set up, we bind our input buffer ready for input. */
128 static int lguestcons_probe(struct lguest_device *lgdev)
129 {
130         int err;
131
132         /* The first argument of hvc_alloc() is the virtual console number, so
133          * we use zero.  The second argument is the interrupt number.
134          *
135          * The third argument is a "struct hv_ops" containing the put_chars()
136          * and get_chars() pointers.  The final argument is the output buffer
137          * size: we use 256 and expect the Host to have room for us to send
138          * that much. */
139         lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
140         if (IS_ERR(lgdev->private))
141                 return PTR_ERR(lgdev->private);
142
143         /* We bind a single DMA buffer at key LGUEST_CONSOLE_DMA_KEY.
144          * "cons_input" is that statically-initialized global DMA buffer we saw
145          * above, and we also give the interrupt we want. */
146         err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
147                               lgdev_irq(lgdev));
148         if (err)
149                 printk("lguest console: failed to bind buffer.\n");
150         return err;
151 }
152 /* Note the use of lgdev_irq() for the interrupt number.  We tell hvc_alloc()
153  * to expect input when this interrupt is triggered, and then tell
154  * lguest_bind_dma() that is the interrupt to send us when input comes in. */
155
156 /*D:360 From now on the console driver follows standard Guest driver form:
157  * register_lguest_driver() registers the device type and probe function, and
158  * the probe function sets up the device.
159  *
160  * The standard "struct lguest_driver": */
161 static struct lguest_driver lguestcons_drv = {
162         .name = "lguestcons",
163         .owner = THIS_MODULE,
164         .device_type = LGUEST_DEVICE_T_CONSOLE,
165         .probe = lguestcons_probe,
166 };
167
168 /* The standard init function */
169 static int __init hvc_lguest_init(void)
170 {
171         return register_lguest_driver(&lguestcons_drv);
172 }
173 module_init(hvc_lguest_init);