blob: 99d182b132c43bf35aada29e973abe9fba7f0d43 [file] [log] [blame]
Rusty Russella23ea922010-01-18 19:14:55 +05301/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Amit Shah17634ba2009-12-21 21:03:25 +05303 * Copyright (C) 2009, 2010 Red Hat, Inc.
Rusty Russell31610432007-10-22 11:03:39 +10004 *
5 * 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
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Amit Shahfb08bd22009-12-21 21:36:04 +053019#include <linux/cdev.h>
20#include <linux/device.h>
Rusty Russell31610432007-10-22 11:03:39 +100021#include <linux/err.h>
Amit Shah2030fa42009-12-21 21:49:30 +053022#include <linux/fs.h>
Rusty Russell31610432007-10-22 11:03:39 +100023#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053024#include <linux/list.h>
Amit Shah2030fa42009-12-21 21:49:30 +053025#include <linux/poll.h>
26#include <linux/sched.h>
Amit Shah38edf582010-01-18 19:15:05 +053027#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100028#include <linux/virtio.h>
29#include <linux/virtio_console.h>
Amit Shah2030fa42009-12-21 21:49:30 +053030#include <linux/wait.h>
Amit Shah17634ba2009-12-21 21:03:25 +053031#include <linux/workqueue.h>
Rusty Russell31610432007-10-22 11:03:39 +100032#include "hvc_console.h"
33
Amit Shah38edf582010-01-18 19:15:05 +053034/*
35 * This is a global struct for storing common data for all the devices
36 * this driver handles.
37 *
38 * Mainly, it has a linked list for all the consoles in one place so
39 * that callbacks from hvc for get_chars(), put_chars() work properly
40 * across multiple devices and multiple ports per device.
41 */
42struct ports_driver_data {
Amit Shahfb08bd22009-12-21 21:36:04 +053043 /* Used for registering chardevs */
44 struct class *class;
45
46 /* Number of devices this driver is handling */
47 unsigned int index;
48
Rusty Russelld8a02bd2010-01-18 19:15:06 +053049 /*
50 * This is used to keep track of the number of hvc consoles
51 * spawned by this driver. This number is given as the first
52 * argument to hvc_alloc(). To correctly map an initial
53 * console spawned via hvc_instantiate to the console being
54 * hooked up via hvc_alloc, we need to pass the same vtermno.
55 *
56 * We also just assume the first console being initialised was
57 * the first one that got used as the initial console.
58 */
59 unsigned int next_vtermno;
60
Amit Shah38edf582010-01-18 19:15:05 +053061 /* All the console devices handled by this driver */
62 struct list_head consoles;
63};
64static struct ports_driver_data pdrvdata;
65
66DEFINE_SPINLOCK(pdrvdata_lock);
67
Amit Shah4f23c572010-01-18 19:15:09 +053068/* This struct holds information that's relevant only for console ports */
69struct console {
70 /* We'll place all consoles in a list in the pdrvdata struct */
71 struct list_head list;
72
73 /* The hvc device associated with this console port */
74 struct hvc_struct *hvc;
75
76 /*
77 * This number identifies the number that we used to register
78 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
79 * number passed on by the hvc callbacks to us to
80 * differentiate between the other console ports handled by
81 * this driver
82 */
83 u32 vtermno;
84};
85
Amit Shahfdb9a052010-01-18 19:15:01 +053086struct port_buffer {
87 char *buf;
88
89 /* size of the buffer in *buf above */
90 size_t size;
91
92 /* used length of the buffer */
93 size_t len;
94 /* offset in the buf from which to consume data */
95 size_t offset;
96};
97
Amit Shah17634ba2009-12-21 21:03:25 +053098/*
99 * This is a per-device struct that stores data common to all the
100 * ports for that device (vdev->priv).
101 */
102struct ports_device {
103 /*
104 * Workqueue handlers where we process deferred work after
105 * notification
106 */
107 struct work_struct control_work;
108
109 struct list_head ports;
110
111 /* To protect the list of ports */
112 spinlock_t ports_lock;
113
114 /* To protect the vq operations for the control channel */
115 spinlock_t cvq_lock;
116
117 /* The current config space is stored here */
118 struct virtio_console_config config;
119
120 /* The virtio device we're associated with */
121 struct virtio_device *vdev;
122
123 /*
124 * A couple of virtqueues for the control channel: one for
125 * guest->host transfers, one for host->guest transfers
126 */
127 struct virtqueue *c_ivq, *c_ovq;
128
129 /* Array of per-port IO virtqueues */
130 struct virtqueue **in_vqs, **out_vqs;
Amit Shahfb08bd22009-12-21 21:36:04 +0530131
132 /* Used for numbering devices for sysfs and debugfs */
133 unsigned int drv_index;
134
135 /* Major number for this device. Ports will be created as minors. */
136 int chr_major;
Amit Shah17634ba2009-12-21 21:03:25 +0530137};
138
Amit Shah1c85bf32010-01-18 19:15:07 +0530139/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +0530140struct port {
Amit Shah17634ba2009-12-21 21:03:25 +0530141 /* Next port in the list, head is in the ports_device */
142 struct list_head list;
143
Amit Shah1c85bf32010-01-18 19:15:07 +0530144 /* Pointer to the parent virtio_console device */
145 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +0530146
147 /* The current buffer from which data has to be fed to readers */
148 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000149
Amit Shah203baab2010-01-18 19:15:12 +0530150 /*
151 * To protect the operations on the in_vq associated with this
152 * port. Has to be a spinlock because it can be called from
153 * interrupt context (get_char()).
154 */
155 spinlock_t inbuf_lock;
156
Amit Shah1c85bf32010-01-18 19:15:07 +0530157 /* The IO vqs for this port */
158 struct virtqueue *in_vq, *out_vq;
159
Amit Shah4f23c572010-01-18 19:15:09 +0530160 /*
161 * The entries in this struct will be valid if this port is
162 * hooked up to an hvc console
163 */
164 struct console cons;
Amit Shah17634ba2009-12-21 21:03:25 +0530165
Amit Shahfb08bd22009-12-21 21:36:04 +0530166 /* Each port associates with a separate char device */
167 struct cdev cdev;
168 struct device *dev;
169
Amit Shah2030fa42009-12-21 21:49:30 +0530170 /* A waitqueue for poll() or blocking read operations */
171 wait_queue_head_t waitqueue;
172
Amit Shah431edb82009-12-21 21:57:40 +0530173 /* The 'name' of the port that we expose via sysfs properties */
174 char *name;
175
Amit Shah17634ba2009-12-21 21:03:25 +0530176 /* The 'id' to identify the port with the Host */
177 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530178
179 /* Is the host device open */
180 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530181
182 /* We should allow only one process to open a port */
183 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530184};
Rusty Russell31610432007-10-22 11:03:39 +1000185
Rusty Russell971f3392010-01-18 19:14:56 +0530186/* This is the very early arch-specified put chars function. */
187static int (*early_put_chars)(u32, const char *, int);
188
Amit Shah38edf582010-01-18 19:15:05 +0530189static struct port *find_port_by_vtermno(u32 vtermno)
190{
191 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530192 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530193 unsigned long flags;
194
195 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530196 list_for_each_entry(cons, &pdrvdata.consoles, list) {
197 if (cons->vtermno == vtermno) {
198 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530199 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530200 }
Amit Shah38edf582010-01-18 19:15:05 +0530201 }
202 port = NULL;
203out:
204 spin_unlock_irqrestore(&pdrvdata_lock, flags);
205 return port;
206}
207
Amit Shah17634ba2009-12-21 21:03:25 +0530208static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
209{
210 struct port *port;
211 unsigned long flags;
212
213 spin_lock_irqsave(&portdev->ports_lock, flags);
214 list_for_each_entry(port, &portdev->ports, list)
215 if (port->id == id)
216 goto out;
217 port = NULL;
218out:
219 spin_unlock_irqrestore(&portdev->ports_lock, flags);
220
221 return port;
222}
223
Amit Shah203baab2010-01-18 19:15:12 +0530224static struct port *find_port_by_vq(struct ports_device *portdev,
225 struct virtqueue *vq)
226{
227 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530228 unsigned long flags;
229
Amit Shah17634ba2009-12-21 21:03:25 +0530230 spin_lock_irqsave(&portdev->ports_lock, flags);
231 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530232 if (port->in_vq == vq || port->out_vq == vq)
233 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530234 port = NULL;
235out:
Amit Shah17634ba2009-12-21 21:03:25 +0530236 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530237 return port;
238}
239
Amit Shah17634ba2009-12-21 21:03:25 +0530240static bool is_console_port(struct port *port)
241{
242 if (port->cons.hvc)
243 return true;
244 return false;
245}
246
247static inline bool use_multiport(struct ports_device *portdev)
248{
249 /*
250 * This condition can be true when put_chars is called from
251 * early_init
252 */
253 if (!portdev->vdev)
254 return 0;
255 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
256}
257
Amit Shahfdb9a052010-01-18 19:15:01 +0530258static void free_buf(struct port_buffer *buf)
259{
260 kfree(buf->buf);
261 kfree(buf);
262}
263
264static struct port_buffer *alloc_buf(size_t buf_size)
265{
266 struct port_buffer *buf;
267
268 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
269 if (!buf)
270 goto fail;
271 buf->buf = kzalloc(buf_size, GFP_KERNEL);
272 if (!buf->buf)
273 goto free_buf;
274 buf->len = 0;
275 buf->offset = 0;
276 buf->size = buf_size;
277 return buf;
278
279free_buf:
280 kfree(buf);
281fail:
282 return NULL;
283}
284
Amit Shaha3cde442010-01-18 19:15:03 +0530285/* Callers should take appropriate locks */
286static void *get_inbuf(struct port *port)
287{
288 struct port_buffer *buf;
289 struct virtqueue *vq;
290 unsigned int len;
291
292 vq = port->in_vq;
293 buf = vq->vq_ops->get_buf(vq, &len);
294 if (buf) {
295 buf->len = len;
296 buf->offset = 0;
297 }
298 return buf;
299}
300
Rusty Russella23ea922010-01-18 19:14:55 +0530301/*
Amit Shahe27b5192010-01-18 19:15:02 +0530302 * Create a scatter-gather list representing our input buffer and put
303 * it in the queue.
304 *
305 * Callers should take appropriate locks.
306 */
Amit Shah203baab2010-01-18 19:15:12 +0530307static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530308{
309 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530310 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530311
Amit Shahe27b5192010-01-18 19:15:02 +0530312 sg_init_one(sg, buf->buf, buf->size);
313
Amit Shah203baab2010-01-18 19:15:12 +0530314 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
Amit Shahe27b5192010-01-18 19:15:02 +0530315 vq->vq_ops->kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530316 return ret;
317}
318
319static bool port_has_data(struct port *port)
320{
321 unsigned long flags;
322 bool ret;
323
324 ret = false;
325 spin_lock_irqsave(&port->inbuf_lock, flags);
326 if (port->inbuf)
327 ret = true;
328 spin_unlock_irqrestore(&port->inbuf_lock, flags);
329
330 return ret;
331}
332
Amit Shah17634ba2009-12-21 21:03:25 +0530333static ssize_t send_control_msg(struct port *port, unsigned int event,
334 unsigned int value)
335{
336 struct scatterlist sg[1];
337 struct virtio_console_control cpkt;
338 struct virtqueue *vq;
339 int len;
340
341 if (!use_multiport(port->portdev))
342 return 0;
343
344 cpkt.id = port->id;
345 cpkt.event = event;
346 cpkt.value = value;
347
348 vq = port->portdev->c_ovq;
349
350 sg_init_one(sg, &cpkt, sizeof(cpkt));
351 if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
352 vq->vq_ops->kick(vq);
353 while (!vq->vq_ops->get_buf(vq, &len))
354 cpu_relax();
355 }
356 return 0;
357}
358
Amit Shahf997f00b2009-12-21 17:28:51 +0530359static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
360{
361 struct scatterlist sg[1];
362 struct virtqueue *out_vq;
363 ssize_t ret;
364 unsigned int len;
365
366 out_vq = port->out_vq;
367
368 sg_init_one(sg, in_buf, in_count);
369 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
370
371 /* Tell Host to go! */
372 out_vq->vq_ops->kick(out_vq);
373
374 if (ret < 0) {
375 len = 0;
376 goto fail;
377 }
378
379 /*
380 * Wait till the host acknowledges it pushed out the data we
381 * sent. Also ensure we return to userspace the number of
382 * bytes that were successfully consumed by the host.
383 */
384 while (!out_vq->vq_ops->get_buf(out_vq, &len))
385 cpu_relax();
386fail:
387 /* We're expected to return the amount of data we wrote */
388 return len;
389}
390
Amit Shah203baab2010-01-18 19:15:12 +0530391/*
392 * Give out the data that's requested from the buffer that we have
393 * queued up.
394 */
Amit Shahb766cee2009-12-21 21:26:45 +0530395static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
396 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530397{
398 struct port_buffer *buf;
399 unsigned long flags;
400
401 if (!out_count || !port_has_data(port))
402 return 0;
403
404 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530405 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530406
Amit Shahb766cee2009-12-21 21:26:45 +0530407 if (to_user) {
408 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530409
Amit Shahb766cee2009-12-21 21:26:45 +0530410 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
411 if (ret)
412 return -EFAULT;
413 } else {
414 memcpy(out_buf, buf->buf + buf->offset, out_count);
415 }
416
Amit Shah203baab2010-01-18 19:15:12 +0530417 buf->offset += out_count;
418
419 if (buf->offset == buf->len) {
420 /*
421 * We're done using all the data in this buffer.
422 * Re-queue so that the Host can send us more data.
423 */
424 spin_lock_irqsave(&port->inbuf_lock, flags);
425 port->inbuf = NULL;
426
427 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530428 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530429
430 spin_unlock_irqrestore(&port->inbuf_lock, flags);
431 }
Amit Shahb766cee2009-12-21 21:26:45 +0530432 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530433 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530434}
435
Amit Shah2030fa42009-12-21 21:49:30 +0530436/* The condition that must be true for polling to end */
437static bool wait_is_over(struct port *port)
438{
439 return port_has_data(port) || !port->host_connected;
440}
441
442static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
443 size_t count, loff_t *offp)
444{
445 struct port *port;
446 ssize_t ret;
447
448 port = filp->private_data;
449
450 if (!port_has_data(port)) {
451 /*
452 * If nothing's connected on the host just return 0 in
453 * case of list_empty; this tells the userspace app
454 * that there's no connection
455 */
456 if (!port->host_connected)
457 return 0;
458 if (filp->f_flags & O_NONBLOCK)
459 return -EAGAIN;
460
461 ret = wait_event_interruptible(port->waitqueue,
462 wait_is_over(port));
463 if (ret < 0)
464 return ret;
465 }
466 /*
467 * We could've received a disconnection message while we were
468 * waiting for more data.
469 *
470 * This check is not clubbed in the if() statement above as we
471 * might receive some data as well as the host could get
472 * disconnected after we got woken up from our wait. So we
473 * really want to give off whatever data we have and only then
474 * check for host_connected.
475 */
476 if (!port_has_data(port) && !port->host_connected)
477 return 0;
478
479 return fill_readbuf(port, ubuf, count, true);
480}
481
482static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
483 size_t count, loff_t *offp)
484{
485 struct port *port;
486 char *buf;
487 ssize_t ret;
488
489 port = filp->private_data;
490
491 count = min((size_t)(32 * 1024), count);
492
493 buf = kmalloc(count, GFP_KERNEL);
494 if (!buf)
495 return -ENOMEM;
496
497 ret = copy_from_user(buf, ubuf, count);
498 if (ret) {
499 ret = -EFAULT;
500 goto free_buf;
501 }
502
503 ret = send_buf(port, buf, count);
504free_buf:
505 kfree(buf);
506 return ret;
507}
508
509static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
510{
511 struct port *port;
512 unsigned int ret;
513
514 port = filp->private_data;
515 poll_wait(filp, &port->waitqueue, wait);
516
517 ret = 0;
518 if (port->inbuf)
519 ret |= POLLIN | POLLRDNORM;
520 if (port->host_connected)
521 ret |= POLLOUT;
522 if (!port->host_connected)
523 ret |= POLLHUP;
524
525 return ret;
526}
527
528static int port_fops_release(struct inode *inode, struct file *filp)
529{
530 struct port *port;
531
532 port = filp->private_data;
533
534 /* Notify host of port being closed */
535 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
536
Amit Shah3c7969c2009-11-26 11:25:38 +0530537 port->guest_connected = false;
538
Amit Shah2030fa42009-12-21 21:49:30 +0530539 return 0;
540}
541
542static int port_fops_open(struct inode *inode, struct file *filp)
543{
544 struct cdev *cdev = inode->i_cdev;
545 struct port *port;
546
547 port = container_of(cdev, struct port, cdev);
548 filp->private_data = port;
549
550 /*
551 * Don't allow opening of console port devices -- that's done
552 * via /dev/hvc
553 */
554 if (is_console_port(port))
555 return -ENXIO;
556
Amit Shah3c7969c2009-11-26 11:25:38 +0530557 /* Allow only one process to open a particular port at a time */
558 spin_lock_irq(&port->inbuf_lock);
559 if (port->guest_connected) {
560 spin_unlock_irq(&port->inbuf_lock);
561 return -EMFILE;
562 }
563
564 port->guest_connected = true;
565 spin_unlock_irq(&port->inbuf_lock);
566
Amit Shah2030fa42009-12-21 21:49:30 +0530567 /* Notify host of port being opened */
568 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
569
570 return 0;
571}
572
573/*
574 * The file operations that we support: programs in the guest can open
575 * a console device, read from it, write to it, poll for data and
576 * close it. The devices are at
577 * /dev/vport<device number>p<port number>
578 */
579static const struct file_operations port_fops = {
580 .owner = THIS_MODULE,
581 .open = port_fops_open,
582 .read = port_fops_read,
583 .write = port_fops_write,
584 .poll = port_fops_poll,
585 .release = port_fops_release,
586};
587
Amit Shahe27b5192010-01-18 19:15:02 +0530588/*
Rusty Russella23ea922010-01-18 19:14:55 +0530589 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000590 *
Rusty Russella23ea922010-01-18 19:14:55 +0530591 * We turn the characters into a scatter-gather list, add it to the
592 * output queue and then kick the Host. Then we sit here waiting for
593 * it to finish: inefficient in theory, but in practice
594 * implementations will do it immediately (lguest's Launcher does).
595 */
Rusty Russell31610432007-10-22 11:03:39 +1000596static int put_chars(u32 vtermno, const char *buf, int count)
597{
Rusty Russell21206ed2010-01-18 19:15:00 +0530598 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530599
600 port = find_port_by_vtermno(vtermno);
601 if (!port)
602 return 0;
Rusty Russell31610432007-10-22 11:03:39 +1000603
Rusty Russell971f3392010-01-18 19:14:56 +0530604 if (unlikely(early_put_chars))
605 return early_put_chars(vtermno, buf, count);
606
Amit Shahf997f00b2009-12-21 17:28:51 +0530607 return send_buf(port, (void *)buf, count);
Rusty Russell31610432007-10-22 11:03:39 +1000608}
609
Rusty Russella23ea922010-01-18 19:14:55 +0530610/*
Rusty Russella23ea922010-01-18 19:14:55 +0530611 * get_chars() is the callback from the hvc_console infrastructure
612 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000613 *
Amit Shah203baab2010-01-18 19:15:12 +0530614 * We call out to fill_readbuf that gets us the required data from the
615 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +0530616 */
Rusty Russell31610432007-10-22 11:03:39 +1000617static int get_chars(u32 vtermno, char *buf, int count)
618{
Rusty Russell21206ed2010-01-18 19:15:00 +0530619 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000620
Amit Shah38edf582010-01-18 19:15:05 +0530621 port = find_port_by_vtermno(vtermno);
622 if (!port)
623 return 0;
Rusty Russell21206ed2010-01-18 19:15:00 +0530624
625 /* If we don't have an input queue yet, we can't get input. */
626 BUG_ON(!port->in_vq);
627
Amit Shahb766cee2009-12-21 21:26:45 +0530628 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000629}
Rusty Russell31610432007-10-22 11:03:39 +1000630
Amit Shahcb06e362010-01-18 19:15:08 +0530631static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100632{
Amit Shahcb06e362010-01-18 19:15:08 +0530633 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100634 struct winsize ws;
635
Amit Shahcb06e362010-01-18 19:15:08 +0530636 vdev = port->portdev->vdev;
637 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
638 vdev->config->get(vdev,
639 offsetof(struct virtio_console_config, cols),
640 &ws.ws_col, sizeof(u16));
641 vdev->config->get(vdev,
642 offsetof(struct virtio_console_config, rows),
643 &ws.ws_row, sizeof(u16));
Amit Shah4f23c572010-01-18 19:15:09 +0530644 hvc_resize(port->cons.hvc, ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100645 }
646}
647
Amit Shahcb06e362010-01-18 19:15:08 +0530648static void virtcons_apply_config(struct virtio_device *vdev)
649{
650 resize_console(find_port_by_vtermno(0));
651}
652
Amit Shah38edf582010-01-18 19:15:05 +0530653/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200654static int notifier_add_vio(struct hvc_struct *hp, int data)
655{
Amit Shah38edf582010-01-18 19:15:05 +0530656 struct port *port;
657
658 port = find_port_by_vtermno(hp->vtermno);
659 if (!port)
660 return -EINVAL;
661
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200662 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530663 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100664
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200665 return 0;
666}
667
668static void notifier_del_vio(struct hvc_struct *hp, int data)
669{
670 hp->irq_requested = 0;
671}
672
Amit Shah17634ba2009-12-21 21:03:25 +0530673/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530674static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530675 .get_chars = get_chars,
676 .put_chars = put_chars,
677 .notifier_add = notifier_add_vio,
678 .notifier_del = notifier_del_vio,
679 .notifier_hangup = notifier_del_vio,
680};
681
682/*
683 * Console drivers are initialized very early so boot messages can go
684 * out, so we do things slightly differently from the generic virtio
685 * initialization of the net and block drivers.
686 *
687 * At this stage, the console is output-only. It's too early to set
688 * up a virtqueue, so we let the drivers do some boutique early-output
689 * thing.
690 */
691int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
692{
693 early_put_chars = put_chars;
694 return hvc_instantiate(0, 0, &hv_ops);
695}
696
Amit Shah17634ba2009-12-21 21:03:25 +0530697int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +0530698{
699 int ret;
700
701 /*
702 * The Host's telling us this port is a console port. Hook it
703 * up with an hvc console.
704 *
705 * To set up and manage our virtual console, we call
706 * hvc_alloc().
707 *
708 * The first argument of hvc_alloc() is the virtual console
709 * number. The second argument is the parameter for the
710 * notification mechanism (like irq number). We currently
711 * leave this as zero, virtqueues have implicit notifications.
712 *
713 * The third argument is a "struct hv_ops" containing the
714 * put_chars() get_chars(), notifier_add() and notifier_del()
715 * pointers. The final argument is the output buffer size: we
716 * can do any size, so we put PAGE_SIZE here.
717 */
718 port->cons.vtermno = pdrvdata.next_vtermno;
719
720 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
721 if (IS_ERR(port->cons.hvc)) {
722 ret = PTR_ERR(port->cons.hvc);
723 port->cons.hvc = NULL;
724 return ret;
725 }
726 spin_lock_irq(&pdrvdata_lock);
727 pdrvdata.next_vtermno++;
728 list_add_tail(&port->cons.list, &pdrvdata.consoles);
729 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530730 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +0530731
Amit Shah2030fa42009-12-21 21:49:30 +0530732 /* Notify host of port being opened */
733 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
734
Amit Shahcfa6d372010-01-18 19:15:10 +0530735 return 0;
736}
737
Amit Shah431edb82009-12-21 21:57:40 +0530738static ssize_t show_port_name(struct device *dev,
739 struct device_attribute *attr, char *buffer)
740{
741 struct port *port;
742
743 port = dev_get_drvdata(dev);
744
745 return sprintf(buffer, "%s\n", port->name);
746}
747
748static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
749
750static struct attribute *port_sysfs_entries[] = {
751 &dev_attr_name.attr,
752 NULL
753};
754
755static struct attribute_group port_attribute_group = {
756 .name = NULL, /* put in device directory */
757 .attrs = port_sysfs_entries,
758};
759
Amit Shah17634ba2009-12-21 21:03:25 +0530760/* Any private messages that the Host and Guest want to share */
761static void handle_control_message(struct ports_device *portdev,
762 struct port_buffer *buf)
763{
764 struct virtio_console_control *cpkt;
765 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +0530766 size_t name_size;
767 int err;
Amit Shah17634ba2009-12-21 21:03:25 +0530768
769 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
770
771 port = find_port_by_id(portdev, cpkt->id);
772 if (!port) {
773 /* No valid header at start of buffer. Drop it. */
774 dev_dbg(&portdev->vdev->dev,
775 "Invalid index %u in control packet\n", cpkt->id);
776 return;
777 }
778
779 switch (cpkt->event) {
780 case VIRTIO_CONSOLE_CONSOLE_PORT:
781 if (!cpkt->value)
782 break;
783 if (is_console_port(port))
784 break;
785
786 init_port_console(port);
787 /*
788 * Could remove the port here in case init fails - but
789 * have to notify the host first.
790 */
791 break;
792 case VIRTIO_CONSOLE_RESIZE:
793 if (!is_console_port(port))
794 break;
795 port->cons.hvc->irq_requested = 1;
796 resize_console(port);
797 break;
Amit Shah2030fa42009-12-21 21:49:30 +0530798 case VIRTIO_CONSOLE_PORT_OPEN:
799 port->host_connected = cpkt->value;
800 wake_up_interruptible(&port->waitqueue);
801 break;
Amit Shah431edb82009-12-21 21:57:40 +0530802 case VIRTIO_CONSOLE_PORT_NAME:
803 /*
804 * Skip the size of the header and the cpkt to get the size
805 * of the name that was sent
806 */
807 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
808
809 port->name = kmalloc(name_size, GFP_KERNEL);
810 if (!port->name) {
811 dev_err(port->dev,
812 "Not enough space to store port name\n");
813 break;
814 }
815 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
816 name_size - 1);
817 port->name[name_size - 1] = 0;
818
819 /*
820 * Since we only have one sysfs attribute, 'name',
821 * create it only if we have a name for the port.
822 */
823 err = sysfs_create_group(&port->dev->kobj,
824 &port_attribute_group);
825 if (err)
826 dev_err(port->dev,
827 "Error %d creating sysfs device attributes\n",
828 err);
829
830 break;
Amit Shah17634ba2009-12-21 21:03:25 +0530831 }
832}
833
834static void control_work_handler(struct work_struct *work)
835{
836 struct ports_device *portdev;
837 struct virtqueue *vq;
838 struct port_buffer *buf;
839 unsigned int len;
840
841 portdev = container_of(work, struct ports_device, control_work);
842 vq = portdev->c_ivq;
843
844 spin_lock(&portdev->cvq_lock);
845 while ((buf = vq->vq_ops->get_buf(vq, &len))) {
846 spin_unlock(&portdev->cvq_lock);
847
848 buf->len = len;
849 buf->offset = 0;
850
851 handle_control_message(portdev, buf);
852
853 spin_lock(&portdev->cvq_lock);
854 if (add_inbuf(portdev->c_ivq, buf) < 0) {
855 dev_warn(&portdev->vdev->dev,
856 "Error adding buffer to queue\n");
857 free_buf(buf);
858 }
859 }
860 spin_unlock(&portdev->cvq_lock);
861}
862
863static void in_intr(struct virtqueue *vq)
864{
865 struct port *port;
866 unsigned long flags;
867
868 port = find_port_by_vq(vq->vdev->priv, vq);
869 if (!port)
870 return;
871
872 spin_lock_irqsave(&port->inbuf_lock, flags);
873 port->inbuf = get_inbuf(port);
874
875 spin_unlock_irqrestore(&port->inbuf_lock, flags);
876
Amit Shah2030fa42009-12-21 21:49:30 +0530877 wake_up_interruptible(&port->waitqueue);
878
Amit Shah17634ba2009-12-21 21:03:25 +0530879 if (is_console_port(port) && hvc_poll(port->cons.hvc))
880 hvc_kick();
881}
882
883static void control_intr(struct virtqueue *vq)
884{
885 struct ports_device *portdev;
886
887 portdev = vq->vdev->priv;
888 schedule_work(&portdev->control_work);
889}
890
891static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
892{
893 struct port_buffer *buf;
894 int ret;
895
896 do {
897 buf = alloc_buf(PAGE_SIZE);
898 if (!buf)
899 break;
900
901 spin_lock_irq(lock);
902 ret = add_inbuf(vq, buf);
903 if (ret < 0) {
904 spin_unlock_irq(lock);
905 free_buf(buf);
906 break;
907 }
908 spin_unlock_irq(lock);
909 } while (ret > 0);
910}
911
912static int add_port(struct ports_device *portdev, u32 id)
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530913{
914 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530915 struct port_buffer *inbuf;
Amit Shahfb08bd22009-12-21 21:36:04 +0530916 dev_t devt;
Rusty Russell31610432007-10-22 11:03:39 +1000917 int err;
Rusty Russell31610432007-10-22 11:03:39 +1000918
Amit Shah1c85bf32010-01-18 19:15:07 +0530919 port = kmalloc(sizeof(*port), GFP_KERNEL);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530920 if (!port) {
921 err = -ENOMEM;
922 goto fail;
Amit Shahf5508042010-01-18 19:14:59 +0530923 }
Rusty Russell73954482010-01-18 19:15:04 +0530924
Amit Shah1c85bf32010-01-18 19:15:07 +0530925 port->portdev = portdev;
Amit Shah17634ba2009-12-21 21:03:25 +0530926 port->id = id;
Amit Shah203baab2010-01-18 19:15:12 +0530927
Amit Shah431edb82009-12-21 21:57:40 +0530928 port->name = NULL;
Amit Shah203baab2010-01-18 19:15:12 +0530929 port->inbuf = NULL;
Amit Shah17634ba2009-12-21 21:03:25 +0530930 port->cons.hvc = NULL;
Amit Shah203baab2010-01-18 19:15:12 +0530931
Amit Shah3c7969c2009-11-26 11:25:38 +0530932 port->host_connected = port->guest_connected = false;
Amit Shah2030fa42009-12-21 21:49:30 +0530933
Amit Shah17634ba2009-12-21 21:03:25 +0530934 port->in_vq = portdev->in_vqs[port->id];
935 port->out_vq = portdev->out_vqs[port->id];
Rusty Russell31610432007-10-22 11:03:39 +1000936
Amit Shah2030fa42009-12-21 21:49:30 +0530937 cdev_init(&port->cdev, &port_fops);
Amit Shahfb08bd22009-12-21 21:36:04 +0530938
939 devt = MKDEV(portdev->chr_major, id);
940 err = cdev_add(&port->cdev, devt, 1);
941 if (err < 0) {
942 dev_err(&port->portdev->vdev->dev,
943 "Error %d adding cdev for port %u\n", err, id);
944 goto free_port;
945 }
946 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
947 devt, port, "vport%up%u",
948 port->portdev->drv_index, id);
949 if (IS_ERR(port->dev)) {
950 err = PTR_ERR(port->dev);
951 dev_err(&port->portdev->vdev->dev,
952 "Error %d creating device for port %u\n",
953 err, id);
954 goto free_cdev;
955 }
956
Amit Shah203baab2010-01-18 19:15:12 +0530957 spin_lock_init(&port->inbuf_lock);
Amit Shah2030fa42009-12-21 21:49:30 +0530958 init_waitqueue_head(&port->waitqueue);
Amit Shah203baab2010-01-18 19:15:12 +0530959
960 inbuf = alloc_buf(PAGE_SIZE);
961 if (!inbuf) {
Amit Shah1c85bf32010-01-18 19:15:07 +0530962 err = -ENOMEM;
Amit Shahfb08bd22009-12-21 21:36:04 +0530963 goto free_device;
Amit Shah1c85bf32010-01-18 19:15:07 +0530964 }
Rusty Russell31610432007-10-22 11:03:39 +1000965
Amit Shah203baab2010-01-18 19:15:12 +0530966 /* Register the input buffer the first time. */
967 add_inbuf(port->in_vq, inbuf);
968
Amit Shah17634ba2009-12-21 21:03:25 +0530969 /*
970 * If we're not using multiport support, this has to be a console port
971 */
972 if (!use_multiport(port->portdev)) {
973 err = init_port_console(port);
974 if (err)
975 goto free_inbuf;
976 }
977
978 spin_lock_irq(&portdev->ports_lock);
979 list_add_tail(&port->list, &port->portdev->ports);
980 spin_unlock_irq(&portdev->ports_lock);
981
982 /*
983 * Tell the Host we're set so that it can send us various
984 * configuration parameters for this port (eg, port name,
985 * caching, whether this is a console port, etc.)
986 */
987 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
Amit Shah38edf582010-01-18 19:15:05 +0530988
Amit Shah1c85bf32010-01-18 19:15:07 +0530989 return 0;
990
991free_inbuf:
Amit Shah203baab2010-01-18 19:15:12 +0530992 free_buf(inbuf);
Amit Shahfb08bd22009-12-21 21:36:04 +0530993free_device:
994 device_destroy(pdrvdata.class, port->dev->devt);
995free_cdev:
996 cdev_del(&port->cdev);
Amit Shah1c85bf32010-01-18 19:15:07 +0530997free_port:
998 kfree(port);
999fail:
1000 return err;
1001}
1002
Amit Shah2658a792010-01-18 19:15:11 +05301003static int init_vqs(struct ports_device *portdev)
1004{
1005 vq_callback_t **io_callbacks;
1006 char **io_names;
1007 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301008 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a792010-01-18 19:15:11 +05301009 int err;
1010
Amit Shah17634ba2009-12-21 21:03:25 +05301011 nr_ports = portdev->config.max_nr_ports;
1012 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a792010-01-18 19:15:11 +05301013
1014 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1015 if (!vqs) {
1016 err = -ENOMEM;
1017 goto fail;
1018 }
1019 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1020 if (!io_callbacks) {
1021 err = -ENOMEM;
1022 goto free_vqs;
1023 }
1024 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1025 if (!io_names) {
1026 err = -ENOMEM;
1027 goto free_callbacks;
1028 }
1029 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1030 GFP_KERNEL);
1031 if (!portdev->in_vqs) {
1032 err = -ENOMEM;
1033 goto free_names;
1034 }
1035 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1036 GFP_KERNEL);
1037 if (!portdev->out_vqs) {
1038 err = -ENOMEM;
1039 goto free_invqs;
1040 }
1041
Amit Shah17634ba2009-12-21 21:03:25 +05301042 /*
1043 * For backward compat (newer host but older guest), the host
1044 * spawns a console port first and also inits the vqs for port
1045 * 0 before others.
1046 */
1047 j = 0;
1048 io_callbacks[j] = in_intr;
1049 io_callbacks[j + 1] = NULL;
1050 io_names[j] = "input";
1051 io_names[j + 1] = "output";
1052 j += 2;
Amit Shah2658a792010-01-18 19:15:11 +05301053
Amit Shah17634ba2009-12-21 21:03:25 +05301054 if (use_multiport(portdev)) {
1055 io_callbacks[j] = control_intr;
1056 io_callbacks[j + 1] = NULL;
1057 io_names[j] = "control-i";
1058 io_names[j + 1] = "control-o";
1059
1060 for (i = 1; i < nr_ports; i++) {
1061 j += 2;
1062 io_callbacks[j] = in_intr;
1063 io_callbacks[j + 1] = NULL;
1064 io_names[j] = "input";
1065 io_names[j + 1] = "output";
1066 }
1067 }
Amit Shah2658a792010-01-18 19:15:11 +05301068 /* Find the queues. */
1069 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1070 io_callbacks,
1071 (const char **)io_names);
1072 if (err)
1073 goto free_outvqs;
1074
Amit Shah17634ba2009-12-21 21:03:25 +05301075 j = 0;
Amit Shah2658a792010-01-18 19:15:11 +05301076 portdev->in_vqs[0] = vqs[0];
1077 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301078 j += 2;
1079 if (use_multiport(portdev)) {
1080 portdev->c_ivq = vqs[j];
1081 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a792010-01-18 19:15:11 +05301082
Amit Shah17634ba2009-12-21 21:03:25 +05301083 for (i = 1; i < nr_ports; i++) {
1084 j += 2;
1085 portdev->in_vqs[i] = vqs[j];
1086 portdev->out_vqs[i] = vqs[j + 1];
1087 }
1088 }
Amit Shah2658a792010-01-18 19:15:11 +05301089 kfree(io_callbacks);
1090 kfree(io_names);
1091 kfree(vqs);
1092
1093 return 0;
1094
1095free_names:
1096 kfree(io_names);
1097free_callbacks:
1098 kfree(io_callbacks);
1099free_outvqs:
1100 kfree(portdev->out_vqs);
1101free_invqs:
1102 kfree(portdev->in_vqs);
1103free_vqs:
1104 kfree(vqs);
1105fail:
1106 return err;
1107}
1108
Amit Shahfb08bd22009-12-21 21:36:04 +05301109static const struct file_operations portdev_fops = {
1110 .owner = THIS_MODULE,
1111};
1112
Amit Shah1c85bf32010-01-18 19:15:07 +05301113/*
1114 * Once we're further in boot, we get probed like any other virtio
1115 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301116 *
1117 * If the host also supports multiple console ports, we check the
1118 * config space to see how many ports the host has spawned. We
1119 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301120 */
1121static int __devinit virtcons_probe(struct virtio_device *vdev)
1122{
Amit Shah1c85bf32010-01-18 19:15:07 +05301123 struct ports_device *portdev;
Amit Shah17634ba2009-12-21 21:03:25 +05301124 u32 i;
Amit Shah1c85bf32010-01-18 19:15:07 +05301125 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301126 bool multiport;
Amit Shah1c85bf32010-01-18 19:15:07 +05301127
1128 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1129 if (!portdev) {
1130 err = -ENOMEM;
1131 goto fail;
1132 }
1133
1134 /* Attach this portdev to this virtio_device, and vice-versa. */
1135 portdev->vdev = vdev;
1136 vdev->priv = portdev;
1137
Amit Shahfb08bd22009-12-21 21:36:04 +05301138 spin_lock_irq(&pdrvdata_lock);
1139 portdev->drv_index = pdrvdata.index++;
1140 spin_unlock_irq(&pdrvdata_lock);
1141
1142 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1143 &portdev_fops);
1144 if (portdev->chr_major < 0) {
1145 dev_err(&vdev->dev,
1146 "Error %d registering chrdev for device %u\n",
1147 portdev->chr_major, portdev->drv_index);
1148 err = portdev->chr_major;
1149 goto free;
1150 }
1151
Amit Shah17634ba2009-12-21 21:03:25 +05301152 multiport = false;
1153 portdev->config.nr_ports = 1;
1154 portdev->config.max_nr_ports = 1;
1155 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1156 multiport = true;
1157 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1158
1159 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1160 nr_ports),
1161 &portdev->config.nr_ports,
1162 sizeof(portdev->config.nr_ports));
1163 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1164 max_nr_ports),
1165 &portdev->config.max_nr_ports,
1166 sizeof(portdev->config.max_nr_ports));
1167 if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
1168 dev_warn(&vdev->dev,
1169 "More ports (%u) specified than allowed (%u). Will init %u ports.",
1170 portdev->config.nr_ports,
1171 portdev->config.max_nr_ports,
1172 portdev->config.max_nr_ports);
1173
1174 portdev->config.nr_ports = portdev->config.max_nr_ports;
1175 }
1176 }
1177
1178 /* Let the Host know we support multiple ports.*/
1179 vdev->config->finalize_features(vdev);
1180
Amit Shah2658a792010-01-18 19:15:11 +05301181 err = init_vqs(portdev);
1182 if (err < 0) {
1183 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301184 goto free_chrdev;
Amit Shah2658a792010-01-18 19:15:11 +05301185 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301186
Amit Shah17634ba2009-12-21 21:03:25 +05301187 spin_lock_init(&portdev->ports_lock);
1188 INIT_LIST_HEAD(&portdev->ports);
1189
1190 if (multiport) {
1191 spin_lock_init(&portdev->cvq_lock);
1192 INIT_WORK(&portdev->control_work, &control_work_handler);
1193
1194 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1195 }
1196
1197 for (i = 0; i < portdev->config.nr_ports; i++)
1198 add_port(portdev, i);
Amit Shah1c85bf32010-01-18 19:15:07 +05301199
Rusty Russell971f3392010-01-18 19:14:56 +05301200 /* Start using the new console output. */
1201 early_put_chars = NULL;
Rusty Russell31610432007-10-22 11:03:39 +10001202 return 0;
1203
Amit Shahfb08bd22009-12-21 21:36:04 +05301204free_chrdev:
1205 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001206free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301207 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001208fail:
1209 return err;
1210}
1211
1212static struct virtio_device_id id_table[] = {
1213 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1214 { 0 },
1215};
1216
Christian Borntraegerc2983452008-11-25 13:36:26 +01001217static unsigned int features[] = {
1218 VIRTIO_CONSOLE_F_SIZE,
Amit Shah17634ba2009-12-21 21:03:25 +05301219 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001220};
1221
Rusty Russell31610432007-10-22 11:03:39 +10001222static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01001223 .feature_table = features,
1224 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10001225 .driver.name = KBUILD_MODNAME,
1226 .driver.owner = THIS_MODULE,
1227 .id_table = id_table,
1228 .probe = virtcons_probe,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001229 .config_changed = virtcons_apply_config,
Rusty Russell31610432007-10-22 11:03:39 +10001230};
1231
1232static int __init init(void)
1233{
Amit Shahfb08bd22009-12-21 21:36:04 +05301234 int err;
1235
1236 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1237 if (IS_ERR(pdrvdata.class)) {
1238 err = PTR_ERR(pdrvdata.class);
1239 pr_err("Error %d creating virtio-ports class\n", err);
1240 return err;
1241 }
Amit Shah38edf582010-01-18 19:15:05 +05301242 INIT_LIST_HEAD(&pdrvdata.consoles);
1243
Rusty Russell31610432007-10-22 11:03:39 +10001244 return register_virtio_driver(&virtio_console);
1245}
1246module_init(init);
1247
1248MODULE_DEVICE_TABLE(virtio, id_table);
1249MODULE_DESCRIPTION("Virtio console driver");
1250MODULE_LICENSE("GPL");