aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/hvc_lguest.c
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2007-07-26 13:41:03 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-26 14:35:17 -0400
commite2c9784325490c878b7f69aeec1bed98b288bd97 (patch)
treed474007607c713a30db818107ca0581269f059a2 /drivers/char/hvc_lguest.c
parentb2b47c214f4e85ce3968120d42e8b18eccb4f4e3 (diff)
lguest: documentation III: Drivers
Documentation: The Drivers Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/char/hvc_lguest.c')
-rw-r--r--drivers/char/hvc_lguest.c77
1 files changed, 74 insertions, 3 deletions
diff --git a/drivers/char/hvc_lguest.c b/drivers/char/hvc_lguest.c
index e7b889e404a7..1de8967cce06 100644
--- a/drivers/char/hvc_lguest.c
+++ b/drivers/char/hvc_lguest.c
@@ -1,6 +1,19 @@
1/* Simple console for lguest. 1/*D:300
2 * The Guest console driver
2 * 3 *
3 * Copyright (C) 2006 Rusty Russell, IBM Corporation 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
4 * 17 *
5 * This program is free software; you can redistribute it and/or modify 18 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 19 * it under the terms of the GNU General Public License as published by
@@ -21,49 +34,81 @@
21#include <linux/lguest_bus.h> 34#include <linux/lguest_bus.h>
22#include "hvc_console.h" 35#include "hvc_console.h"
23 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. */
24static char inbuf[256]; 40static char inbuf[256];
25static struct lguest_dma cons_input = { .used_len = 0, 41static struct lguest_dma cons_input = { .used_len = 0,
26 .addr[0] = __pa(inbuf), 42 .addr[0] = __pa(inbuf),
27 .len[0] = sizeof(inbuf), 43 .len[0] = sizeof(inbuf),
28 .len[1] = 0 }; 44 .len[1] = 0 };
29 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). */
30static int put_chars(u32 vtermno, const char *buf, int count) 54static int put_chars(u32 vtermno, const char *buf, int count)
31{ 55{
32 struct lguest_dma dma; 56 struct lguest_dma dma;
33 57
34 /* FIXME: what if it's over a page boundary? */ 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. */
35 dma.len[0] = count; 61 dma.len[0] = count;
36 dma.len[1] = 0; 62 dma.len[1] = 0;
37 dma.addr[0] = __pa(buf); 63 dma.addr[0] = __pa(buf);
38 64
39 lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma); 65 lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
66 /* We're expected to return the amount of data we wrote: all of it. */
40 return count; 67 return count;
41} 68}
42 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. */
43static int get_chars(u32 vtermno, char *buf, int count) 77static int get_chars(u32 vtermno, char *buf, int count)
44{ 78{
45 static int cons_offset; 79 static int cons_offset;
46 80
81 /* Nothing left to see here... */
47 if (!cons_input.used_len) 82 if (!cons_input.used_len)
48 return 0; 83 return 0;
49 84
85 /* You want more than we have to give? Well, try wanting less! */
50 if (cons_input.used_len - cons_offset < count) 86 if (cons_input.used_len - cons_offset < count)
51 count = cons_input.used_len - cons_offset; 87 count = cons_input.used_len - cons_offset;
52 88
89 /* Copy across to their buffer and increment offset. */
53 memcpy(buf, inbuf + cons_offset, count); 90 memcpy(buf, inbuf + cons_offset, count);
54 cons_offset += count; 91 cons_offset += count;
92
93 /* Finished? Zero offset, and reset cons_input so Host will use it
94 * again. */
55 if (cons_offset == cons_input.used_len) { 95 if (cons_offset == cons_input.used_len) {
56 cons_offset = 0; 96 cons_offset = 0;
57 cons_input.used_len = 0; 97 cons_input.used_len = 0;
58 } 98 }
59 return count; 99 return count;
60} 100}
101/*:*/
61 102
62static struct hv_ops lguest_cons = { 103static struct hv_ops lguest_cons = {
63 .get_chars = get_chars, 104 .get_chars = get_chars,
64 .put_chars = put_chars, 105 .put_chars = put_chars,
65}; 106};
66 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. */
67static int __init cons_init(void) 112static int __init cons_init(void)
68{ 113{
69 if (strcmp(paravirt_ops.name, "lguest") != 0) 114 if (strcmp(paravirt_ops.name, "lguest") != 0)
@@ -73,21 +118,46 @@ static int __init cons_init(void)
73} 118}
74console_initcall(cons_init); 119console_initcall(cons_init);
75 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. */
76static int lguestcons_probe(struct lguest_device *lgdev) 128static int lguestcons_probe(struct lguest_device *lgdev)
77{ 129{
78 int err; 130 int err;
79 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. */
80 lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256); 139 lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
81 if (IS_ERR(lgdev->private)) 140 if (IS_ERR(lgdev->private))
82 return PTR_ERR(lgdev->private); 141 return PTR_ERR(lgdev->private);
83 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. */
84 err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1, 146 err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
85 lgdev_irq(lgdev)); 147 lgdev_irq(lgdev));
86 if (err) 148 if (err)
87 printk("lguest console: failed to bind buffer.\n"); 149 printk("lguest console: failed to bind buffer.\n");
88 return err; 150 return err;
89} 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. */
90 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": */
91static struct lguest_driver lguestcons_drv = { 161static struct lguest_driver lguestcons_drv = {
92 .name = "lguestcons", 162 .name = "lguestcons",
93 .owner = THIS_MODULE, 163 .owner = THIS_MODULE,
@@ -95,6 +165,7 @@ static struct lguest_driver lguestcons_drv = {
95 .probe = lguestcons_probe, 165 .probe = lguestcons_probe,
96}; 166};
97 167
168/* The standard init function */
98static int __init hvc_lguest_init(void) 169static int __init hvc_lguest_init(void)
99{ 170{
100 return register_lguest_driver(&lguestcons_drv); 171 return register_lguest_driver(&lguestcons_drv);