blob: d01051060be34ee9553e4219bb7aa8d0e3f4040a [file] [log] [blame]
Rusty Russella23ea922010-01-18 19:14:55 +05301/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Rusty Russell31610432007-10-22 11:03:39 +10003 *
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 Shah38edf582010-01-18 19:15:05 +053020#include <linux/list.h>
21#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100022#include <linux/virtio.h>
23#include <linux/virtio_console.h>
24#include "hvc_console.h"
25
Amit Shah38edf582010-01-18 19:15:05 +053026/*
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 */
34struct ports_driver_data {
Rusty Russelld8a02bd2010-01-18 19:15:06 +053035 /*
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 Shah38edf582010-01-18 19:15:05 +053047 /* All the console devices handled by this driver */
48 struct list_head consoles;
49};
50static struct ports_driver_data pdrvdata;
51
52DEFINE_SPINLOCK(pdrvdata_lock);
53
Amit Shah4f23c572010-01-18 19:15:09 +053054/* This struct holds information that's relevant only for console ports */
55struct console {
56 /* We'll place all consoles in a list in the pdrvdata struct */
57 struct list_head list;
58
59 /* The hvc device associated with this console port */
60 struct hvc_struct *hvc;
61
62 /*
63 * This number identifies the number that we used to register
64 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
65 * number passed on by the hvc callbacks to us to
66 * differentiate between the other console ports handled by
67 * this driver
68 */
69 u32 vtermno;
70};
71
Amit Shah1c85bf32010-01-18 19:15:07 +053072/*
73 * This is a per-device struct that stores data common to all the
74 * ports for that device (vdev->priv).
75 */
76struct ports_device {
Amit Shah2658a792010-01-18 19:15:11 +053077 /* Array of per-port IO virtqueues */
78 struct virtqueue **in_vqs, **out_vqs;
79
Amit Shah1c85bf32010-01-18 19:15:07 +053080 struct virtio_device *vdev;
81};
82
Amit Shahfdb9a052010-01-18 19:15:01 +053083struct port_buffer {
84 char *buf;
85
86 /* size of the buffer in *buf above */
87 size_t size;
88
89 /* used length of the buffer */
90 size_t len;
91 /* offset in the buf from which to consume data */
92 size_t offset;
93};
94
Amit Shah1c85bf32010-01-18 19:15:07 +053095/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +053096struct port {
Amit Shah1c85bf32010-01-18 19:15:07 +053097 /* Pointer to the parent virtio_console device */
98 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +053099
100 /* The current buffer from which data has to be fed to readers */
101 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000102
Amit Shah203baab2010-01-18 19:15:12 +0530103 /*
104 * To protect the operations on the in_vq associated with this
105 * port. Has to be a spinlock because it can be called from
106 * interrupt context (get_char()).
107 */
108 spinlock_t inbuf_lock;
109
Amit Shah1c85bf32010-01-18 19:15:07 +0530110 /* The IO vqs for this port */
111 struct virtqueue *in_vq, *out_vq;
112
Amit Shah4f23c572010-01-18 19:15:09 +0530113 /*
114 * The entries in this struct will be valid if this port is
115 * hooked up to an hvc console
116 */
117 struct console cons;
Rusty Russell21206ed2010-01-18 19:15:00 +0530118};
Rusty Russell31610432007-10-22 11:03:39 +1000119
Rusty Russell971f3392010-01-18 19:14:56 +0530120/* This is the very early arch-specified put chars function. */
121static int (*early_put_chars)(u32, const char *, int);
122
Amit Shah38edf582010-01-18 19:15:05 +0530123static struct port *find_port_by_vtermno(u32 vtermno)
124{
125 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530126 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530127 unsigned long flags;
128
129 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530130 list_for_each_entry(cons, &pdrvdata.consoles, list) {
131 if (cons->vtermno == vtermno) {
132 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530133 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530134 }
Amit Shah38edf582010-01-18 19:15:05 +0530135 }
136 port = NULL;
137out:
138 spin_unlock_irqrestore(&pdrvdata_lock, flags);
139 return port;
140}
141
Amit Shah203baab2010-01-18 19:15:12 +0530142static struct port *find_port_by_vq(struct ports_device *portdev,
143 struct virtqueue *vq)
144{
145 struct port *port;
146 struct console *cons;
147 unsigned long flags;
148
149 spin_lock_irqsave(&pdrvdata_lock, flags);
150 list_for_each_entry(cons, &pdrvdata.consoles, list) {
151 port = container_of(cons, struct port, cons);
152 if (port->in_vq == vq || port->out_vq == vq)
153 goto out;
154 }
155 port = NULL;
156out:
157 spin_unlock_irqrestore(&pdrvdata_lock, flags);
158 return port;
159}
160
Amit Shahfdb9a052010-01-18 19:15:01 +0530161static void free_buf(struct port_buffer *buf)
162{
163 kfree(buf->buf);
164 kfree(buf);
165}
166
167static struct port_buffer *alloc_buf(size_t buf_size)
168{
169 struct port_buffer *buf;
170
171 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
172 if (!buf)
173 goto fail;
174 buf->buf = kzalloc(buf_size, GFP_KERNEL);
175 if (!buf->buf)
176 goto free_buf;
177 buf->len = 0;
178 buf->offset = 0;
179 buf->size = buf_size;
180 return buf;
181
182free_buf:
183 kfree(buf);
184fail:
185 return NULL;
186}
187
Amit Shaha3cde442010-01-18 19:15:03 +0530188/* Callers should take appropriate locks */
189static void *get_inbuf(struct port *port)
190{
191 struct port_buffer *buf;
192 struct virtqueue *vq;
193 unsigned int len;
194
195 vq = port->in_vq;
196 buf = vq->vq_ops->get_buf(vq, &len);
197 if (buf) {
198 buf->len = len;
199 buf->offset = 0;
200 }
201 return buf;
202}
203
Rusty Russella23ea922010-01-18 19:14:55 +0530204/*
Amit Shahe27b5192010-01-18 19:15:02 +0530205 * Create a scatter-gather list representing our input buffer and put
206 * it in the queue.
207 *
208 * Callers should take appropriate locks.
209 */
Amit Shah203baab2010-01-18 19:15:12 +0530210static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530211{
212 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530213 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530214
Amit Shahe27b5192010-01-18 19:15:02 +0530215 sg_init_one(sg, buf->buf, buf->size);
216
Amit Shah203baab2010-01-18 19:15:12 +0530217 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
Amit Shahe27b5192010-01-18 19:15:02 +0530218 vq->vq_ops->kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530219 return ret;
220}
221
222static bool port_has_data(struct port *port)
223{
224 unsigned long flags;
225 bool ret;
226
227 ret = false;
228 spin_lock_irqsave(&port->inbuf_lock, flags);
229 if (port->inbuf)
230 ret = true;
231 spin_unlock_irqrestore(&port->inbuf_lock, flags);
232
233 return ret;
234}
235
Amit Shahf997f00b2009-12-21 17:28:51 +0530236static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
237{
238 struct scatterlist sg[1];
239 struct virtqueue *out_vq;
240 ssize_t ret;
241 unsigned int len;
242
243 out_vq = port->out_vq;
244
245 sg_init_one(sg, in_buf, in_count);
246 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
247
248 /* Tell Host to go! */
249 out_vq->vq_ops->kick(out_vq);
250
251 if (ret < 0) {
252 len = 0;
253 goto fail;
254 }
255
256 /*
257 * Wait till the host acknowledges it pushed out the data we
258 * sent. Also ensure we return to userspace the number of
259 * bytes that were successfully consumed by the host.
260 */
261 while (!out_vq->vq_ops->get_buf(out_vq, &len))
262 cpu_relax();
263fail:
264 /* We're expected to return the amount of data we wrote */
265 return len;
266}
267
Amit Shah203baab2010-01-18 19:15:12 +0530268/*
269 * Give out the data that's requested from the buffer that we have
270 * queued up.
271 */
272static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count)
273{
274 struct port_buffer *buf;
275 unsigned long flags;
276
277 if (!out_count || !port_has_data(port))
278 return 0;
279
280 buf = port->inbuf;
281 if (out_count > buf->len - buf->offset)
282 out_count = buf->len - buf->offset;
283
284 memcpy(out_buf, buf->buf + buf->offset, out_count);
285
286 /* Return the number of bytes actually copied */
287 buf->offset += out_count;
288
289 if (buf->offset == buf->len) {
290 /*
291 * We're done using all the data in this buffer.
292 * Re-queue so that the Host can send us more data.
293 */
294 spin_lock_irqsave(&port->inbuf_lock, flags);
295 port->inbuf = NULL;
296
297 if (add_inbuf(port->in_vq, buf) < 0)
298 dev_warn(&port->portdev->vdev->dev, "failed add_buf\n");
299
300 spin_unlock_irqrestore(&port->inbuf_lock, flags);
301 }
302 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530303}
304
305/*
Rusty Russella23ea922010-01-18 19:14:55 +0530306 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000307 *
Rusty Russella23ea922010-01-18 19:14:55 +0530308 * We turn the characters into a scatter-gather list, add it to the
309 * output queue and then kick the Host. Then we sit here waiting for
310 * it to finish: inefficient in theory, but in practice
311 * implementations will do it immediately (lguest's Launcher does).
312 */
Rusty Russell31610432007-10-22 11:03:39 +1000313static int put_chars(u32 vtermno, const char *buf, int count)
314{
Rusty Russell21206ed2010-01-18 19:15:00 +0530315 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530316
317 port = find_port_by_vtermno(vtermno);
318 if (!port)
319 return 0;
Rusty Russell31610432007-10-22 11:03:39 +1000320
Rusty Russell971f3392010-01-18 19:14:56 +0530321 if (unlikely(early_put_chars))
322 return early_put_chars(vtermno, buf, count);
323
Amit Shahf997f00b2009-12-21 17:28:51 +0530324 return send_buf(port, (void *)buf, count);
Rusty Russell31610432007-10-22 11:03:39 +1000325}
326
Rusty Russella23ea922010-01-18 19:14:55 +0530327/*
Rusty Russella23ea922010-01-18 19:14:55 +0530328 * get_chars() is the callback from the hvc_console infrastructure
329 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000330 *
Amit Shah203baab2010-01-18 19:15:12 +0530331 * We call out to fill_readbuf that gets us the required data from the
332 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +0530333 */
Rusty Russell31610432007-10-22 11:03:39 +1000334static int get_chars(u32 vtermno, char *buf, int count)
335{
Rusty Russell21206ed2010-01-18 19:15:00 +0530336 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000337
Amit Shah38edf582010-01-18 19:15:05 +0530338 port = find_port_by_vtermno(vtermno);
339 if (!port)
340 return 0;
Rusty Russell21206ed2010-01-18 19:15:00 +0530341
342 /* If we don't have an input queue yet, we can't get input. */
343 BUG_ON(!port->in_vq);
344
Amit Shah203baab2010-01-18 19:15:12 +0530345 return fill_readbuf(port, buf, count);
Rusty Russell31610432007-10-22 11:03:39 +1000346}
Rusty Russell31610432007-10-22 11:03:39 +1000347
Amit Shahcb06e362010-01-18 19:15:08 +0530348static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100349{
Amit Shahcb06e362010-01-18 19:15:08 +0530350 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100351 struct winsize ws;
352
Amit Shahcb06e362010-01-18 19:15:08 +0530353 vdev = port->portdev->vdev;
354 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
355 vdev->config->get(vdev,
356 offsetof(struct virtio_console_config, cols),
357 &ws.ws_col, sizeof(u16));
358 vdev->config->get(vdev,
359 offsetof(struct virtio_console_config, rows),
360 &ws.ws_row, sizeof(u16));
Amit Shah4f23c572010-01-18 19:15:09 +0530361 hvc_resize(port->cons.hvc, ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100362 }
363}
364
Amit Shahcb06e362010-01-18 19:15:08 +0530365static void virtcons_apply_config(struct virtio_device *vdev)
366{
367 resize_console(find_port_by_vtermno(0));
368}
369
Amit Shah38edf582010-01-18 19:15:05 +0530370/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200371static int notifier_add_vio(struct hvc_struct *hp, int data)
372{
Amit Shah38edf582010-01-18 19:15:05 +0530373 struct port *port;
374
375 port = find_port_by_vtermno(hp->vtermno);
376 if (!port)
377 return -EINVAL;
378
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200379 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530380 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100381
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200382 return 0;
383}
384
385static void notifier_del_vio(struct hvc_struct *hp, int data)
386{
387 hp->irq_requested = 0;
388}
389
390static void hvc_handle_input(struct virtqueue *vq)
391{
Amit Shah203baab2010-01-18 19:15:12 +0530392 struct port *port;
393 unsigned long flags;
Rusty Russell73954482010-01-18 19:15:04 +0530394
Amit Shah203baab2010-01-18 19:15:12 +0530395 port = find_port_by_vq(vq->vdev->priv, vq);
396 if (!port)
397 return;
Amit Shah1c85bf32010-01-18 19:15:07 +0530398
Amit Shah203baab2010-01-18 19:15:12 +0530399 spin_lock_irqsave(&port->inbuf_lock, flags);
400 port->inbuf = get_inbuf(port);
401 spin_unlock_irqrestore(&port->inbuf_lock, flags);
402
403 if (hvc_poll(port->cons.hvc))
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200404 hvc_kick();
405}
406
Rusty Russell971f3392010-01-18 19:14:56 +0530407/* The operations for the console. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530408static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530409 .get_chars = get_chars,
410 .put_chars = put_chars,
411 .notifier_add = notifier_add_vio,
412 .notifier_del = notifier_del_vio,
413 .notifier_hangup = notifier_del_vio,
414};
415
416/*
417 * Console drivers are initialized very early so boot messages can go
418 * out, so we do things slightly differently from the generic virtio
419 * initialization of the net and block drivers.
420 *
421 * At this stage, the console is output-only. It's too early to set
422 * up a virtqueue, so we let the drivers do some boutique early-output
423 * thing.
424 */
425int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
426{
427 early_put_chars = put_chars;
428 return hvc_instantiate(0, 0, &hv_ops);
429}
430
Amit Shahcfa6d372010-01-18 19:15:10 +0530431int __devinit init_port_console(struct port *port)
432{
433 int ret;
434
435 /*
436 * The Host's telling us this port is a console port. Hook it
437 * up with an hvc console.
438 *
439 * To set up and manage our virtual console, we call
440 * hvc_alloc().
441 *
442 * The first argument of hvc_alloc() is the virtual console
443 * number. The second argument is the parameter for the
444 * notification mechanism (like irq number). We currently
445 * leave this as zero, virtqueues have implicit notifications.
446 *
447 * The third argument is a "struct hv_ops" containing the
448 * put_chars() get_chars(), notifier_add() and notifier_del()
449 * pointers. The final argument is the output buffer size: we
450 * can do any size, so we put PAGE_SIZE here.
451 */
452 port->cons.vtermno = pdrvdata.next_vtermno;
453
454 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
455 if (IS_ERR(port->cons.hvc)) {
456 ret = PTR_ERR(port->cons.hvc);
457 port->cons.hvc = NULL;
458 return ret;
459 }
460 spin_lock_irq(&pdrvdata_lock);
461 pdrvdata.next_vtermno++;
462 list_add_tail(&port->cons.list, &pdrvdata.consoles);
463 spin_unlock_irq(&pdrvdata_lock);
464
465 return 0;
466}
467
Amit Shah1c85bf32010-01-18 19:15:07 +0530468static int __devinit add_port(struct ports_device *portdev)
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530469{
470 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530471 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000472 int err;
Rusty Russell31610432007-10-22 11:03:39 +1000473
Amit Shah1c85bf32010-01-18 19:15:07 +0530474 port = kmalloc(sizeof(*port), GFP_KERNEL);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530475 if (!port) {
476 err = -ENOMEM;
477 goto fail;
Amit Shahf5508042010-01-18 19:14:59 +0530478 }
Rusty Russell73954482010-01-18 19:15:04 +0530479
Amit Shah1c85bf32010-01-18 19:15:07 +0530480 port->portdev = portdev;
Amit Shah203baab2010-01-18 19:15:12 +0530481
482 port->inbuf = NULL;
483
Amit Shah2658a792010-01-18 19:15:11 +0530484 port->in_vq = portdev->in_vqs[0];
485 port->out_vq = portdev->out_vqs[0];
Rusty Russell31610432007-10-22 11:03:39 +1000486
Amit Shah203baab2010-01-18 19:15:12 +0530487 spin_lock_init(&port->inbuf_lock);
488
489 inbuf = alloc_buf(PAGE_SIZE);
490 if (!inbuf) {
Amit Shah1c85bf32010-01-18 19:15:07 +0530491 err = -ENOMEM;
492 goto free_port;
493 }
Rusty Russell31610432007-10-22 11:03:39 +1000494
Amit Shah203baab2010-01-18 19:15:12 +0530495 /* Register the input buffer the first time. */
496 add_inbuf(port->in_vq, inbuf);
497
Amit Shahcfa6d372010-01-18 19:15:10 +0530498 err = init_port_console(port);
499 if (err)
Amit Shah1c85bf32010-01-18 19:15:07 +0530500 goto free_inbuf;
Amit Shah38edf582010-01-18 19:15:05 +0530501
Amit Shah1c85bf32010-01-18 19:15:07 +0530502 return 0;
503
504free_inbuf:
Amit Shah203baab2010-01-18 19:15:12 +0530505 free_buf(inbuf);
Amit Shah1c85bf32010-01-18 19:15:07 +0530506free_port:
507 kfree(port);
508fail:
509 return err;
510}
511
Amit Shah2658a792010-01-18 19:15:11 +0530512static int init_vqs(struct ports_device *portdev)
513{
514 vq_callback_t **io_callbacks;
515 char **io_names;
516 struct virtqueue **vqs;
517 u32 nr_ports, nr_queues;
518 int err;
519
520 /* We currently only have one port and two queues for that port */
521 nr_ports = 1;
522 nr_queues = 2;
523
524 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
525 if (!vqs) {
526 err = -ENOMEM;
527 goto fail;
528 }
529 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
530 if (!io_callbacks) {
531 err = -ENOMEM;
532 goto free_vqs;
533 }
534 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
535 if (!io_names) {
536 err = -ENOMEM;
537 goto free_callbacks;
538 }
539 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
540 GFP_KERNEL);
541 if (!portdev->in_vqs) {
542 err = -ENOMEM;
543 goto free_names;
544 }
545 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
546 GFP_KERNEL);
547 if (!portdev->out_vqs) {
548 err = -ENOMEM;
549 goto free_invqs;
550 }
551
552 io_callbacks[0] = hvc_handle_input;
553 io_callbacks[1] = NULL;
554 io_names[0] = "input";
555 io_names[1] = "output";
556
557 /* Find the queues. */
558 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
559 io_callbacks,
560 (const char **)io_names);
561 if (err)
562 goto free_outvqs;
563
564 portdev->in_vqs[0] = vqs[0];
565 portdev->out_vqs[0] = vqs[1];
566
567 kfree(io_callbacks);
568 kfree(io_names);
569 kfree(vqs);
570
571 return 0;
572
573free_names:
574 kfree(io_names);
575free_callbacks:
576 kfree(io_callbacks);
577free_outvqs:
578 kfree(portdev->out_vqs);
579free_invqs:
580 kfree(portdev->in_vqs);
581free_vqs:
582 kfree(vqs);
583fail:
584 return err;
585}
586
Amit Shah1c85bf32010-01-18 19:15:07 +0530587/*
588 * Once we're further in boot, we get probed like any other virtio
589 * device.
590 */
591static int __devinit virtcons_probe(struct virtio_device *vdev)
592{
Amit Shah1c85bf32010-01-18 19:15:07 +0530593 struct ports_device *portdev;
594 int err;
595
596 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
597 if (!portdev) {
598 err = -ENOMEM;
599 goto fail;
600 }
601
602 /* Attach this portdev to this virtio_device, and vice-versa. */
603 portdev->vdev = vdev;
604 vdev->priv = portdev;
605
Amit Shah2658a792010-01-18 19:15:11 +0530606 err = init_vqs(portdev);
607 if (err < 0) {
608 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shah1c85bf32010-01-18 19:15:07 +0530609 goto free;
Amit Shah2658a792010-01-18 19:15:11 +0530610 }
Amit Shah1c85bf32010-01-18 19:15:07 +0530611
612 /* We only have one port. */
613 err = add_port(portdev);
614 if (err)
615 goto free_vqs;
616
Rusty Russell971f3392010-01-18 19:14:56 +0530617 /* Start using the new console output. */
618 early_put_chars = NULL;
Rusty Russell31610432007-10-22 11:03:39 +1000619 return 0;
620
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600621free_vqs:
622 vdev->config->del_vqs(vdev);
Amit Shah2658a792010-01-18 19:15:11 +0530623 kfree(portdev->in_vqs);
624 kfree(portdev->out_vqs);
Rusty Russell31610432007-10-22 11:03:39 +1000625free:
Amit Shah1c85bf32010-01-18 19:15:07 +0530626 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +1000627fail:
628 return err;
629}
630
631static struct virtio_device_id id_table[] = {
632 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
633 { 0 },
634};
635
Christian Borntraegerc2983452008-11-25 13:36:26 +0100636static unsigned int features[] = {
637 VIRTIO_CONSOLE_F_SIZE,
638};
639
Rusty Russell31610432007-10-22 11:03:39 +1000640static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +0100641 .feature_table = features,
642 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +1000643 .driver.name = KBUILD_MODNAME,
644 .driver.owner = THIS_MODULE,
645 .id_table = id_table,
646 .probe = virtcons_probe,
Christian Borntraegerc2983452008-11-25 13:36:26 +0100647 .config_changed = virtcons_apply_config,
Rusty Russell31610432007-10-22 11:03:39 +1000648};
649
650static int __init init(void)
651{
Amit Shah38edf582010-01-18 19:15:05 +0530652 INIT_LIST_HEAD(&pdrvdata.consoles);
653
Rusty Russell31610432007-10-22 11:03:39 +1000654 return register_virtio_driver(&virtio_console);
655}
656module_init(init);
657
658MODULE_DEVICE_TABLE(virtio, id_table);
659MODULE_DESCRIPTION("Virtio console driver");
660MODULE_LICENSE("GPL");