Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 20 | #include <linux/list.h> |
| 21 | #include <linux/spinlock.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 22 | #include <linux/virtio.h> |
| 23 | #include <linux/virtio_console.h> |
| 24 | #include "hvc_console.h" |
| 25 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 26 | /* |
| 27 | * This is a global struct for storing common data for all the devices |
| 28 | * this driver handles. |
| 29 | * |
| 30 | * Mainly, it has a linked list for all the consoles in one place so |
| 31 | * that callbacks from hvc for get_chars(), put_chars() work properly |
| 32 | * across multiple devices and multiple ports per device. |
| 33 | */ |
| 34 | struct ports_driver_data { |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 35 | /* |
| 36 | * This is used to keep track of the number of hvc consoles |
| 37 | * spawned by this driver. This number is given as the first |
| 38 | * argument to hvc_alloc(). To correctly map an initial |
| 39 | * console spawned via hvc_instantiate to the console being |
| 40 | * hooked up via hvc_alloc, we need to pass the same vtermno. |
| 41 | * |
| 42 | * We also just assume the first console being initialised was |
| 43 | * the first one that got used as the initial console. |
| 44 | */ |
| 45 | unsigned int next_vtermno; |
| 46 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 47 | /* All the console devices handled by this driver */ |
| 48 | struct list_head consoles; |
| 49 | }; |
| 50 | static struct ports_driver_data pdrvdata; |
| 51 | |
| 52 | DEFINE_SPINLOCK(pdrvdata_lock); |
| 53 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 54 | /* |
| 55 | * This is a per-device struct that stores data common to all the |
| 56 | * ports for that device (vdev->priv). |
| 57 | */ |
| 58 | struct ports_device { |
| 59 | struct virtqueue *in_vq, *out_vq; |
| 60 | struct virtio_device *vdev; |
| 61 | }; |
| 62 | |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 63 | struct port_buffer { |
| 64 | char *buf; |
| 65 | |
| 66 | /* size of the buffer in *buf above */ |
| 67 | size_t size; |
| 68 | |
| 69 | /* used length of the buffer */ |
| 70 | size_t len; |
| 71 | /* offset in the buf from which to consume data */ |
| 72 | size_t offset; |
| 73 | }; |
| 74 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 75 | /* This struct holds the per-port data */ |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 76 | struct port { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 77 | /* Pointer to the parent virtio_console device */ |
| 78 | struct ports_device *portdev; |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 79 | |
| 80 | /* The current buffer from which data has to be fed to readers */ |
| 81 | struct port_buffer *inbuf; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 82 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 83 | /* The IO vqs for this port */ |
| 84 | struct virtqueue *in_vq, *out_vq; |
| 85 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 86 | /* For console ports, hvc != NULL and these are valid. */ |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 87 | /* The hvc device */ |
| 88 | struct hvc_struct *hvc; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 89 | |
| 90 | /* We'll place all consoles in a list in the pdrvdata struct */ |
| 91 | struct list_head list; |
| 92 | |
| 93 | /* Our vterm number. */ |
| 94 | u32 vtermno; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 95 | }; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 96 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 97 | /* This is the very early arch-specified put chars function. */ |
| 98 | static int (*early_put_chars)(u32, const char *, int); |
| 99 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 100 | static struct port *find_port_by_vtermno(u32 vtermno) |
| 101 | { |
| 102 | struct port *port; |
| 103 | unsigned long flags; |
| 104 | |
| 105 | spin_lock_irqsave(&pdrvdata_lock, flags); |
| 106 | list_for_each_entry(port, &pdrvdata.consoles, list) { |
| 107 | if (port->vtermno == vtermno) |
| 108 | goto out; |
| 109 | } |
| 110 | port = NULL; |
| 111 | out: |
| 112 | spin_unlock_irqrestore(&pdrvdata_lock, flags); |
| 113 | return port; |
| 114 | } |
| 115 | |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 116 | static void free_buf(struct port_buffer *buf) |
| 117 | { |
| 118 | kfree(buf->buf); |
| 119 | kfree(buf); |
| 120 | } |
| 121 | |
| 122 | static struct port_buffer *alloc_buf(size_t buf_size) |
| 123 | { |
| 124 | struct port_buffer *buf; |
| 125 | |
| 126 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 127 | if (!buf) |
| 128 | goto fail; |
| 129 | buf->buf = kzalloc(buf_size, GFP_KERNEL); |
| 130 | if (!buf->buf) |
| 131 | goto free_buf; |
| 132 | buf->len = 0; |
| 133 | buf->offset = 0; |
| 134 | buf->size = buf_size; |
| 135 | return buf; |
| 136 | |
| 137 | free_buf: |
| 138 | kfree(buf); |
| 139 | fail: |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Amit Shah | a3cde44 | 2010-01-18 19:15:03 +0530 | [diff] [blame] | 143 | /* Callers should take appropriate locks */ |
| 144 | static void *get_inbuf(struct port *port) |
| 145 | { |
| 146 | struct port_buffer *buf; |
| 147 | struct virtqueue *vq; |
| 148 | unsigned int len; |
| 149 | |
| 150 | vq = port->in_vq; |
| 151 | buf = vq->vq_ops->get_buf(vq, &len); |
| 152 | if (buf) { |
| 153 | buf->len = len; |
| 154 | buf->offset = 0; |
| 155 | } |
| 156 | return buf; |
| 157 | } |
| 158 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 159 | /* |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 160 | * Create a scatter-gather list representing our input buffer and put |
| 161 | * it in the queue. |
| 162 | * |
| 163 | * Callers should take appropriate locks. |
| 164 | */ |
| 165 | static void add_inbuf(struct virtqueue *vq, struct port_buffer *buf) |
| 166 | { |
| 167 | struct scatterlist sg[1]; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 168 | |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 169 | sg_init_one(sg, buf->buf, buf->size); |
| 170 | |
| 171 | if (vq->vq_ops->add_buf(vq, sg, 0, 1, buf) < 0) |
| 172 | BUG(); |
| 173 | vq->vq_ops->kick(vq); |
| 174 | } |
| 175 | |
| 176 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 177 | * The put_chars() callback is pretty straightforward. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 178 | * |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 179 | * We turn the characters into a scatter-gather list, add it to the |
| 180 | * output queue and then kick the Host. Then we sit here waiting for |
| 181 | * it to finish: inefficient in theory, but in practice |
| 182 | * implementations will do it immediately (lguest's Launcher does). |
| 183 | */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 184 | static int put_chars(u32 vtermno, const char *buf, int count) |
| 185 | { |
| 186 | struct scatterlist sg[1]; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 187 | struct port *port; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 188 | struct virtqueue *out_vq; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 189 | unsigned int len; |
| 190 | |
| 191 | port = find_port_by_vtermno(vtermno); |
| 192 | if (!port) |
| 193 | return 0; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 194 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 195 | if (unlikely(early_put_chars)) |
| 196 | return early_put_chars(vtermno, buf, count); |
| 197 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 198 | out_vq = port->out_vq; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 199 | /* This is a convenient routine to initialize a single-elem sg list */ |
| 200 | sg_init_one(sg, buf, count); |
| 201 | |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 202 | /* This shouldn't fail: if it does, we lose chars. */ |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 203 | if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) { |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 204 | /* Tell Host to go! */ |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 205 | out_vq->vq_ops->kick(out_vq); |
| 206 | while (!out_vq->vq_ops->get_buf(out_vq, &len)) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 207 | cpu_relax(); |
| 208 | } |
| 209 | |
| 210 | /* We're expected to return the amount of data we wrote: all of it. */ |
| 211 | return count; |
| 212 | } |
| 213 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 214 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 215 | * get_chars() is the callback from the hvc_console infrastructure |
| 216 | * when an interrupt is received. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 217 | * |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 218 | * Most of the code deals with the fact that the hvc_console() |
| 219 | * infrastructure only asks us for 16 bytes at a time. We keep |
| 220 | * in_offset and in_used fields for partially-filled buffers. |
| 221 | */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 222 | static int get_chars(u32 vtermno, char *buf, int count) |
| 223 | { |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 224 | struct port *port; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 225 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 226 | port = find_port_by_vtermno(vtermno); |
| 227 | if (!port) |
| 228 | return 0; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 229 | |
| 230 | /* If we don't have an input queue yet, we can't get input. */ |
| 231 | BUG_ON(!port->in_vq); |
| 232 | |
| 233 | /* No more in buffer? See if they've (re)used it. */ |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 234 | if (port->inbuf->offset == port->inbuf->len) { |
Amit Shah | a3cde44 | 2010-01-18 19:15:03 +0530 | [diff] [blame] | 235 | if (!get_inbuf(port)) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* You want more than we have to give? Well, try wanting less! */ |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 240 | if (port->inbuf->offset + count > port->inbuf->len) |
| 241 | count = port->inbuf->len - port->inbuf->offset; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 242 | |
| 243 | /* Copy across to their buffer and increment offset. */ |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 244 | memcpy(buf, port->inbuf->buf + port->inbuf->offset, count); |
| 245 | port->inbuf->offset += count; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 246 | |
| 247 | /* Finished? Re-register buffer so Host will use it again. */ |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 248 | if (port->inbuf->offset == port->inbuf->len) |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 249 | add_inbuf(port->in_vq, port->inbuf); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 250 | |
| 251 | return count; |
| 252 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 253 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 254 | /* |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 255 | * virtio console configuration. This supports: |
| 256 | * - console resize |
| 257 | */ |
| 258 | static void virtcons_apply_config(struct virtio_device *dev) |
| 259 | { |
| 260 | struct winsize ws; |
| 261 | |
| 262 | if (virtio_has_feature(dev, VIRTIO_CONSOLE_F_SIZE)) { |
| 263 | dev->config->get(dev, |
| 264 | offsetof(struct virtio_console_config, cols), |
| 265 | &ws.ws_col, sizeof(u16)); |
| 266 | dev->config->get(dev, |
| 267 | offsetof(struct virtio_console_config, rows), |
| 268 | &ws.ws_row, sizeof(u16)); |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 269 | /* This is the pre-multiport style: we use control messages |
| 270 | * these days which specify the port. So this means port 0. */ |
| 271 | hvc_resize(find_port_by_vtermno(0)->hvc, ws); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 275 | /* We set the configuration at this point, since we now have a tty */ |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 276 | static int notifier_add_vio(struct hvc_struct *hp, int data) |
| 277 | { |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 278 | struct port *port; |
| 279 | |
| 280 | port = find_port_by_vtermno(hp->vtermno); |
| 281 | if (!port) |
| 282 | return -EINVAL; |
| 283 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 284 | hp->irq_requested = 1; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 285 | virtcons_apply_config(port->portdev->vdev); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 286 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static void notifier_del_vio(struct hvc_struct *hp, int data) |
| 291 | { |
| 292 | hp->irq_requested = 0; |
| 293 | } |
| 294 | |
| 295 | static void hvc_handle_input(struct virtqueue *vq) |
| 296 | { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 297 | struct port *port; |
| 298 | bool activity = false; |
Rusty Russell | 7395448 | 2010-01-18 19:15:04 +0530 | [diff] [blame] | 299 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 300 | list_for_each_entry(port, &pdrvdata.consoles, list) |
| 301 | activity |= hvc_poll(port->hvc); |
| 302 | |
| 303 | if (activity) |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 304 | hvc_kick(); |
| 305 | } |
| 306 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 307 | /* The operations for the console. */ |
Rusty Russell | 1dff399 | 2009-11-28 12:20:26 +0530 | [diff] [blame] | 308 | static const struct hv_ops hv_ops = { |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 309 | .get_chars = get_chars, |
| 310 | .put_chars = put_chars, |
| 311 | .notifier_add = notifier_add_vio, |
| 312 | .notifier_del = notifier_del_vio, |
| 313 | .notifier_hangup = notifier_del_vio, |
| 314 | }; |
| 315 | |
| 316 | /* |
| 317 | * Console drivers are initialized very early so boot messages can go |
| 318 | * out, so we do things slightly differently from the generic virtio |
| 319 | * initialization of the net and block drivers. |
| 320 | * |
| 321 | * At this stage, the console is output-only. It's too early to set |
| 322 | * up a virtqueue, so we let the drivers do some boutique early-output |
| 323 | * thing. |
| 324 | */ |
| 325 | int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int)) |
| 326 | { |
| 327 | early_put_chars = put_chars; |
| 328 | return hvc_instantiate(0, 0, &hv_ops); |
| 329 | } |
| 330 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 331 | static int __devinit add_port(struct ports_device *portdev) |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 332 | { |
| 333 | struct port *port; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 334 | int err; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 335 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 336 | port = kmalloc(sizeof(*port), GFP_KERNEL); |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 337 | if (!port) { |
| 338 | err = -ENOMEM; |
| 339 | goto fail; |
Amit Shah | f550804 | 2010-01-18 19:14:59 +0530 | [diff] [blame] | 340 | } |
Rusty Russell | 7395448 | 2010-01-18 19:15:04 +0530 | [diff] [blame] | 341 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 342 | port->portdev = portdev; |
| 343 | port->in_vq = portdev->in_vq; |
| 344 | port->out_vq = portdev->out_vq; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 345 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 346 | port->inbuf = alloc_buf(PAGE_SIZE); |
| 347 | if (!port->inbuf) { |
| 348 | err = -ENOMEM; |
| 349 | goto free_port; |
| 350 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 351 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 352 | /* |
| 353 | * The first argument of hvc_alloc() is the virtual console |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 354 | * number. The second argument is the parameter for the |
| 355 | * notification mechanism (like irq number). We currently |
| 356 | * leave this as zero, virtqueues have implicit notifications. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 357 | * |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 358 | * The third argument is a "struct hv_ops" containing the |
| 359 | * put_chars(), get_chars(), notifier_add() and notifier_del() |
| 360 | * pointers. The final argument is the output buffer size: we |
| 361 | * can do any size, so we put PAGE_SIZE here. |
| 362 | */ |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 363 | port->vtermno = pdrvdata.next_vtermno; |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 364 | port->hvc = hvc_alloc(port->vtermno, 0, &hv_ops, PAGE_SIZE); |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 365 | if (IS_ERR(port->hvc)) { |
| 366 | err = PTR_ERR(port->hvc); |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 367 | goto free_inbuf; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 368 | } |
| 369 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 370 | /* Add to vtermno list. */ |
| 371 | spin_lock_irq(&pdrvdata_lock); |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 372 | pdrvdata.next_vtermno++; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 373 | list_add(&port->list, &pdrvdata.consoles); |
| 374 | spin_unlock_irq(&pdrvdata_lock); |
| 375 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 376 | /* Register the input buffer the first time. */ |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 377 | add_inbuf(port->in_vq, port->inbuf); |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 378 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 379 | return 0; |
| 380 | |
| 381 | free_inbuf: |
| 382 | free_buf(port->inbuf); |
| 383 | free_port: |
| 384 | kfree(port); |
| 385 | fail: |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Once we're further in boot, we get probed like any other virtio |
| 391 | * device. |
| 392 | */ |
| 393 | static int __devinit virtcons_probe(struct virtio_device *vdev) |
| 394 | { |
| 395 | vq_callback_t *callbacks[] = { hvc_handle_input, NULL}; |
| 396 | const char *names[] = { "input", "output" }; |
| 397 | struct virtqueue *vqs[2]; |
| 398 | struct ports_device *portdev; |
| 399 | int err; |
| 400 | |
| 401 | portdev = kmalloc(sizeof(*portdev), GFP_KERNEL); |
| 402 | if (!portdev) { |
| 403 | err = -ENOMEM; |
| 404 | goto fail; |
| 405 | } |
| 406 | |
| 407 | /* Attach this portdev to this virtio_device, and vice-versa. */ |
| 408 | portdev->vdev = vdev; |
| 409 | vdev->priv = portdev; |
| 410 | |
| 411 | /* Find the queues. */ |
| 412 | err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names); |
| 413 | if (err) |
| 414 | goto free; |
| 415 | |
| 416 | portdev->in_vq = vqs[0]; |
| 417 | portdev->out_vq = vqs[1]; |
| 418 | |
| 419 | /* We only have one port. */ |
| 420 | err = add_port(portdev); |
| 421 | if (err) |
| 422 | goto free_vqs; |
| 423 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 424 | /* Start using the new console output. */ |
| 425 | early_put_chars = NULL; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 426 | return 0; |
| 427 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 428 | free_vqs: |
| 429 | vdev->config->del_vqs(vdev); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 430 | free: |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame^] | 431 | kfree(portdev); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 432 | fail: |
| 433 | return err; |
| 434 | } |
| 435 | |
| 436 | static struct virtio_device_id id_table[] = { |
| 437 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, |
| 438 | { 0 }, |
| 439 | }; |
| 440 | |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 441 | static unsigned int features[] = { |
| 442 | VIRTIO_CONSOLE_F_SIZE, |
| 443 | }; |
| 444 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 445 | static struct virtio_driver virtio_console = { |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 446 | .feature_table = features, |
| 447 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 448 | .driver.name = KBUILD_MODNAME, |
| 449 | .driver.owner = THIS_MODULE, |
| 450 | .id_table = id_table, |
| 451 | .probe = virtcons_probe, |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 452 | .config_changed = virtcons_apply_config, |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static int __init init(void) |
| 456 | { |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 457 | INIT_LIST_HEAD(&pdrvdata.consoles); |
| 458 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 459 | return register_virtio_driver(&virtio_console); |
| 460 | } |
| 461 | module_init(init); |
| 462 | |
| 463 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 464 | MODULE_DESCRIPTION("Virtio console driver"); |
| 465 | MODULE_LICENSE("GPL"); |