2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/cdev.h>
21 #include <linux/debugfs.h>
22 #include <linux/completion.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/freezer.h>
27 #include <linux/splice.h>
28 #include <linux/pagemap.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/poll.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/virtio.h>
36 #include <linux/virtio_console.h>
37 #include <linux/wait.h>
38 #include <linux/workqueue.h>
39 #include <linux/module.h>
40 #include "../tty/hvc/hvc_console.h"
43 * This is a global struct for storing common data for all the devices
44 * this driver handles.
46 * Mainly, it has a linked list for all the consoles in one place so
47 * that callbacks from hvc for get_chars(), put_chars() work properly
48 * across multiple devices and multiple ports per device.
50 struct ports_driver_data {
51 /* Used for registering chardevs */
54 /* Used for exporting per-port information to debugfs */
55 struct dentry *debugfs_dir;
57 /* List of all the devices we're handling */
58 struct list_head portdevs;
60 /* Number of devices this driver is handling */
64 * This is used to keep track of the number of hvc consoles
65 * spawned by this driver. This number is given as the first
66 * argument to hvc_alloc(). To correctly map an initial
67 * console spawned via hvc_instantiate to the console being
68 * hooked up via hvc_alloc, we need to pass the same vtermno.
70 * We also just assume the first console being initialised was
71 * the first one that got used as the initial console.
73 unsigned int next_vtermno;
75 /* All the console devices handled by this driver */
76 struct list_head consoles;
78 static struct ports_driver_data pdrvdata;
80 DEFINE_SPINLOCK(pdrvdata_lock);
81 DECLARE_COMPLETION(early_console_added);
83 /* This struct holds information that's relevant only for console ports */
85 /* We'll place all consoles in a list in the pdrvdata struct */
86 struct list_head list;
88 /* The hvc device associated with this console port */
89 struct hvc_struct *hvc;
91 /* The size of the console */
95 * This number identifies the number that we used to register
96 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
97 * number passed on by the hvc callbacks to us to
98 * differentiate between the other console ports handled by
107 /* size of the buffer in *buf above */
110 /* used length of the buffer */
112 /* offset in the buf from which to consume data */
117 * This is a per-device struct that stores data common to all the
118 * ports for that device (vdev->priv).
120 struct ports_device {
121 /* Next portdev in the list, head is in the pdrvdata struct */
122 struct list_head list;
125 * Workqueue handlers where we process deferred work after
128 struct work_struct control_work;
130 struct list_head ports;
132 /* To protect the list of ports */
133 spinlock_t ports_lock;
135 /* To protect the vq operations for the control channel */
138 /* The current config space is stored here */
139 struct virtio_console_config config;
141 /* The virtio device we're associated with */
142 struct virtio_device *vdev;
145 * A couple of virtqueues for the control channel: one for
146 * guest->host transfers, one for host->guest transfers
148 struct virtqueue *c_ivq, *c_ovq;
150 /* Array of per-port IO virtqueues */
151 struct virtqueue **in_vqs, **out_vqs;
153 /* Used for numbering devices for sysfs and debugfs */
154 unsigned int drv_index;
156 /* Major number for this device. Ports will be created as minors. */
161 unsigned long bytes_sent, bytes_received, bytes_discarded;
164 /* This struct holds the per-port data */
166 /* Next port in the list, head is in the ports_device */
167 struct list_head list;
169 /* Pointer to the parent virtio_console device */
170 struct ports_device *portdev;
172 /* The current buffer from which data has to be fed to readers */
173 struct port_buffer *inbuf;
176 * To protect the operations on the in_vq associated with this
177 * port. Has to be a spinlock because it can be called from
178 * interrupt context (get_char()).
180 spinlock_t inbuf_lock;
182 /* Protect the operations on the out_vq. */
183 spinlock_t outvq_lock;
185 /* The IO vqs for this port */
186 struct virtqueue *in_vq, *out_vq;
188 /* File in the debugfs directory that exposes this port's information */
189 struct dentry *debugfs_file;
192 * Keep count of the bytes sent, received and discarded for
193 * this port for accounting and debugging purposes. These
194 * counts are not reset across port open / close events.
196 struct port_stats stats;
199 * The entries in this struct will be valid if this port is
200 * hooked up to an hvc console
204 /* Each port associates with a separate char device */
208 /* Reference-counting to handle port hot-unplugs and file operations */
211 /* A waitqueue for poll() or blocking read operations */
212 wait_queue_head_t waitqueue;
214 /* The 'name' of the port that we expose via sysfs properties */
217 /* We can notify apps of host connect / disconnect events via SIGIO */
218 struct fasync_struct *async_queue;
220 /* The 'id' to identify the port with the Host */
225 /* Is the host device open */
228 /* We should allow only one process to open a port */
229 bool guest_connected;
232 /* This is the very early arch-specified put chars function. */
233 static int (*early_put_chars)(u32, const char *, int);
235 static struct port *find_port_by_vtermno(u32 vtermno)
238 struct console *cons;
241 spin_lock_irqsave(&pdrvdata_lock, flags);
242 list_for_each_entry(cons, &pdrvdata.consoles, list) {
243 if (cons->vtermno == vtermno) {
244 port = container_of(cons, struct port, cons);
250 spin_unlock_irqrestore(&pdrvdata_lock, flags);
254 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
260 spin_lock_irqsave(&portdev->ports_lock, flags);
261 list_for_each_entry(port, &portdev->ports, list)
262 if (port->cdev->dev == dev)
266 spin_unlock_irqrestore(&portdev->ports_lock, flags);
271 static struct port *find_port_by_devt(dev_t dev)
273 struct ports_device *portdev;
277 spin_lock_irqsave(&pdrvdata_lock, flags);
278 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
279 port = find_port_by_devt_in_portdev(portdev, dev);
285 spin_unlock_irqrestore(&pdrvdata_lock, flags);
289 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
294 spin_lock_irqsave(&portdev->ports_lock, flags);
295 list_for_each_entry(port, &portdev->ports, list)
300 spin_unlock_irqrestore(&portdev->ports_lock, flags);
305 static struct port *find_port_by_vq(struct ports_device *portdev,
306 struct virtqueue *vq)
311 spin_lock_irqsave(&portdev->ports_lock, flags);
312 list_for_each_entry(port, &portdev->ports, list)
313 if (port->in_vq == vq || port->out_vq == vq)
317 spin_unlock_irqrestore(&portdev->ports_lock, flags);
321 static bool is_console_port(struct port *port)
328 static inline bool use_multiport(struct ports_device *portdev)
331 * This condition can be true when put_chars is called from
336 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
339 static void free_buf(struct port_buffer *buf)
345 static struct port_buffer *alloc_buf(size_t buf_size)
347 struct port_buffer *buf;
349 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
352 buf->buf = kzalloc(buf_size, GFP_KERNEL);
357 buf->size = buf_size;
366 /* Callers should take appropriate locks */
367 static struct port_buffer *get_inbuf(struct port *port)
369 struct port_buffer *buf;
375 buf = virtqueue_get_buf(port->in_vq, &len);
379 port->stats.bytes_received += len;
385 * Create a scatter-gather list representing our input buffer and put
388 * Callers should take appropriate locks.
390 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
392 struct scatterlist sg[1];
395 sg_init_one(sg, buf->buf, buf->size);
397 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
402 /* Discard any unread data this port has. Callers lockers. */
403 static void discard_port_data(struct port *port)
405 struct port_buffer *buf;
408 if (!port->portdev) {
409 /* Device has been unplugged. vqs are already gone. */
412 buf = get_inbuf(port);
416 port->stats.bytes_discarded += buf->len - buf->offset;
417 if (add_inbuf(port->in_vq, buf) < 0) {
422 buf = get_inbuf(port);
425 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
429 static bool port_has_data(struct port *port)
435 spin_lock_irqsave(&port->inbuf_lock, flags);
436 port->inbuf = get_inbuf(port);
440 spin_unlock_irqrestore(&port->inbuf_lock, flags);
444 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
445 unsigned int event, unsigned int value)
447 struct scatterlist sg[1];
448 struct virtio_console_control cpkt;
449 struct virtqueue *vq;
452 if (!use_multiport(portdev))
461 sg_init_one(sg, &cpkt, sizeof(cpkt));
462 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) {
464 while (!virtqueue_get_buf(vq, &len))
470 static ssize_t send_control_msg(struct port *port, unsigned int event,
473 /* Did the port get unplugged before userspace closed it? */
475 return __send_control_msg(port->portdev, port->id, event, value);
479 struct buffer_token {
482 struct scatterlist *sg;
484 /* If sgpages == 0 then buf is used, else sg is used */
485 unsigned int sgpages;
488 static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages)
493 for (i = 0; i < nrpages; i++) {
494 page = sg_page(&sg[i]);
502 /* Callers must take the port->outvq_lock */
503 static void reclaim_consumed_buffers(struct port *port)
505 struct buffer_token *tok;
508 if (!port->portdev) {
509 /* Device has been unplugged. vqs are already gone. */
512 while ((tok = virtqueue_get_buf(port->out_vq, &len))) {
514 reclaim_sg_pages(tok->u.sg, tok->sgpages);
518 port->outvq_full = false;
522 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
523 int nents, size_t in_count,
524 struct buffer_token *tok, bool nonblock)
526 struct virtqueue *out_vq;
531 out_vq = port->out_vq;
533 spin_lock_irqsave(&port->outvq_lock, flags);
535 reclaim_consumed_buffers(port);
537 ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC);
539 /* Tell Host to go! */
540 virtqueue_kick(out_vq);
548 port->outvq_full = true;
554 * Wait till the host acknowledges it pushed out the data we
555 * sent. This is done for data from the hvc_console; the tty
556 * operations are performed with spinlocks held so we can't
557 * sleep here. An alternative would be to copy the data to a
558 * buffer and relax the spinning requirement. The downside is
559 * we need to kmalloc a GFP_ATOMIC buffer each time the
560 * console driver writes something out.
562 while (!virtqueue_get_buf(out_vq, &len))
565 spin_unlock_irqrestore(&port->outvq_lock, flags);
567 port->stats.bytes_sent += in_count;
569 * We're expected to return the amount of data we wrote -- all
575 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
578 struct scatterlist sg[1];
579 struct buffer_token *tok;
581 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
587 sg_init_one(sg, in_buf, in_count);
589 return __send_to_port(port, sg, 1, in_count, tok, nonblock);
592 static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
593 size_t in_count, bool nonblock)
595 struct buffer_token *tok;
597 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
600 tok->sgpages = nents;
603 return __send_to_port(port, sg, nents, in_count, tok, nonblock);
607 * Give out the data that's requested from the buffer that we have
610 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
613 struct port_buffer *buf;
616 if (!out_count || !port_has_data(port))
620 out_count = min(out_count, buf->len - buf->offset);
625 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
629 memcpy(out_buf, buf->buf + buf->offset, out_count);
632 buf->offset += out_count;
634 if (buf->offset == buf->len) {
636 * We're done using all the data in this buffer.
637 * Re-queue so that the Host can send us more data.
639 spin_lock_irqsave(&port->inbuf_lock, flags);
642 if (add_inbuf(port->in_vq, buf) < 0)
643 dev_warn(port->dev, "failed add_buf\n");
645 spin_unlock_irqrestore(&port->inbuf_lock, flags);
647 /* Return the number of bytes actually copied */
651 /* The condition that must be true for polling to end */
652 static bool will_read_block(struct port *port)
654 if (!port->guest_connected) {
655 /* Port got hot-unplugged. Let's exit. */
658 return !port_has_data(port) && port->host_connected;
661 static bool will_write_block(struct port *port)
665 if (!port->guest_connected) {
666 /* Port got hot-unplugged. Let's exit. */
669 if (!port->host_connected)
672 spin_lock_irq(&port->outvq_lock);
674 * Check if the Host has consumed any buffers since we last
675 * sent data (this is only applicable for nonblocking ports).
677 reclaim_consumed_buffers(port);
678 ret = port->outvq_full;
679 spin_unlock_irq(&port->outvq_lock);
684 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
685 size_t count, loff_t *offp)
690 port = filp->private_data;
692 if (!port_has_data(port)) {
694 * If nothing's connected on the host just return 0 in
695 * case of list_empty; this tells the userspace app
696 * that there's no connection
698 if (!port->host_connected)
700 if (filp->f_flags & O_NONBLOCK)
703 ret = wait_event_freezable(port->waitqueue,
704 !will_read_block(port));
708 /* Port got hot-unplugged. */
709 if (!port->guest_connected)
712 * We could've received a disconnection message while we were
713 * waiting for more data.
715 * This check is not clubbed in the if() statement above as we
716 * might receive some data as well as the host could get
717 * disconnected after we got woken up from our wait. So we
718 * really want to give off whatever data we have and only then
719 * check for host_connected.
721 if (!port_has_data(port) && !port->host_connected)
724 return fill_readbuf(port, ubuf, count, true);
727 static int wait_port_writable(struct port *port, bool nonblock)
731 if (will_write_block(port)) {
735 ret = wait_event_freezable(port->waitqueue,
736 !will_write_block(port));
740 /* Port got hot-unplugged. */
741 if (!port->guest_connected)
747 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
748 size_t count, loff_t *offp)
755 /* Userspace could be out to fool us */
759 port = filp->private_data;
761 nonblock = filp->f_flags & O_NONBLOCK;
763 ret = wait_port_writable(port, nonblock);
767 count = min((size_t)(32 * 1024), count);
769 buf = kmalloc(count, GFP_KERNEL);
773 ret = copy_from_user(buf, ubuf, count);
780 * We now ask send_buf() to not spin for generic ports -- we
781 * can re-use the same code path that non-blocking file
782 * descriptors take for blocking file descriptors since the
783 * wait is already done and we're certain the write will go
784 * through to the host.
787 ret = send_buf(port, buf, count, nonblock);
789 if (nonblock && ret > 0)
802 struct scatterlist *sg;
805 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
806 struct splice_desc *sd)
808 struct sg_list *sgl = sd->u.data;
809 unsigned int offset, len;
811 if (sgl->n == sgl->size)
814 /* Try lock this page */
815 if (buf->ops->steal(pipe, buf) == 0) {
816 /* Get reference and unlock page for moving */
818 unlock_page(buf->page);
820 len = min(buf->len, sd->len);
821 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
823 /* Failback to copying a page */
824 struct page *page = alloc_page(GFP_KERNEL);
825 char *src = buf->ops->map(pipe, buf, 1);
832 offset = sd->pos & ~PAGE_MASK;
835 if (len + offset > PAGE_SIZE)
836 len = PAGE_SIZE - offset;
838 memcpy(dst + offset, src + buf->offset, len);
841 buf->ops->unmap(pipe, buf, src);
843 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
851 /* Faster zero-copy write by splicing */
852 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
853 struct file *filp, loff_t *ppos,
854 size_t len, unsigned int flags)
856 struct port *port = filp->private_data;
859 struct splice_desc sd = {
866 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
872 sgl.size = pipe->nrbufs;
873 sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL);
874 if (unlikely(!sgl.sg))
877 sg_init_table(sgl.sg, sgl.size);
878 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
880 ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true);
885 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
890 port = filp->private_data;
891 poll_wait(filp, &port->waitqueue, wait);
893 if (!port->guest_connected) {
894 /* Port got unplugged */
898 if (!will_read_block(port))
899 ret |= POLLIN | POLLRDNORM;
900 if (!will_write_block(port))
902 if (!port->host_connected)
908 static void remove_port(struct kref *kref);
910 static int port_fops_release(struct inode *inode, struct file *filp)
914 port = filp->private_data;
916 /* Notify host of port being closed */
917 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
919 spin_lock_irq(&port->inbuf_lock);
920 port->guest_connected = false;
922 discard_port_data(port);
924 spin_unlock_irq(&port->inbuf_lock);
926 spin_lock_irq(&port->outvq_lock);
927 reclaim_consumed_buffers(port);
928 spin_unlock_irq(&port->outvq_lock);
931 * Locks aren't necessary here as a port can't be opened after
932 * unplug, and if a port isn't unplugged, a kref would already
933 * exist for the port. Plus, taking ports_lock here would
934 * create a dependency on other locks taken by functions
935 * inside remove_port if we're the last holder of the port,
936 * creating many problems.
938 kref_put(&port->kref, remove_port);
943 static int port_fops_open(struct inode *inode, struct file *filp)
945 struct cdev *cdev = inode->i_cdev;
949 port = find_port_by_devt(cdev->dev);
950 filp->private_data = port;
952 /* Prevent against a port getting hot-unplugged at the same time */
953 spin_lock_irq(&port->portdev->ports_lock);
954 kref_get(&port->kref);
955 spin_unlock_irq(&port->portdev->ports_lock);
958 * Don't allow opening of console port devices -- that's done
961 if (is_console_port(port)) {
966 /* Allow only one process to open a particular port at a time */
967 spin_lock_irq(&port->inbuf_lock);
968 if (port->guest_connected) {
969 spin_unlock_irq(&port->inbuf_lock);
974 port->guest_connected = true;
975 spin_unlock_irq(&port->inbuf_lock);
977 spin_lock_irq(&port->outvq_lock);
979 * There might be a chance that we missed reclaiming a few
980 * buffers in the window of the port getting previously closed
983 reclaim_consumed_buffers(port);
984 spin_unlock_irq(&port->outvq_lock);
986 nonseekable_open(inode, filp);
988 /* Notify host of port being opened */
989 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
993 kref_put(&port->kref, remove_port);
997 static int port_fops_fasync(int fd, struct file *filp, int mode)
1001 port = filp->private_data;
1002 return fasync_helper(fd, filp, mode, &port->async_queue);
1006 * The file operations that we support: programs in the guest can open
1007 * a console device, read from it, write to it, poll for data and
1008 * close it. The devices are at
1009 * /dev/vport<device number>p<port number>
1011 static const struct file_operations port_fops = {
1012 .owner = THIS_MODULE,
1013 .open = port_fops_open,
1014 .read = port_fops_read,
1015 .write = port_fops_write,
1016 .splice_write = port_fops_splice_write,
1017 .poll = port_fops_poll,
1018 .release = port_fops_release,
1019 .fasync = port_fops_fasync,
1020 .llseek = no_llseek,
1024 * The put_chars() callback is pretty straightforward.
1026 * We turn the characters into a scatter-gather list, add it to the
1027 * output queue and then kick the Host. Then we sit here waiting for
1028 * it to finish: inefficient in theory, but in practice
1029 * implementations will do it immediately (lguest's Launcher does).
1031 static int put_chars(u32 vtermno, const char *buf, int count)
1035 if (unlikely(early_put_chars))
1036 return early_put_chars(vtermno, buf, count);
1038 port = find_port_by_vtermno(vtermno);
1042 return send_buf(port, (void *)buf, count, false);
1046 * get_chars() is the callback from the hvc_console infrastructure
1047 * when an interrupt is received.
1049 * We call out to fill_readbuf that gets us the required data from the
1050 * buffers that are queued up.
1052 static int get_chars(u32 vtermno, char *buf, int count)
1056 /* If we've not set up the port yet, we have no input to give. */
1057 if (unlikely(early_put_chars))
1060 port = find_port_by_vtermno(vtermno);
1064 /* If we don't have an input queue yet, we can't get input. */
1065 BUG_ON(!port->in_vq);
1067 return fill_readbuf(port, buf, count, false);
1070 static void resize_console(struct port *port)
1072 struct virtio_device *vdev;
1074 /* The port could have been hot-unplugged */
1075 if (!port || !is_console_port(port))
1078 vdev = port->portdev->vdev;
1079 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1080 hvc_resize(port->cons.hvc, port->cons.ws);
1083 /* We set the configuration at this point, since we now have a tty */
1084 static int notifier_add_vio(struct hvc_struct *hp, int data)
1088 port = find_port_by_vtermno(hp->vtermno);
1092 hp->irq_requested = 1;
1093 resize_console(port);
1098 static void notifier_del_vio(struct hvc_struct *hp, int data)
1100 hp->irq_requested = 0;
1103 /* The operations for console ports. */
1104 static const struct hv_ops hv_ops = {
1105 .get_chars = get_chars,
1106 .put_chars = put_chars,
1107 .notifier_add = notifier_add_vio,
1108 .notifier_del = notifier_del_vio,
1109 .notifier_hangup = notifier_del_vio,
1113 * Console drivers are initialized very early so boot messages can go
1114 * out, so we do things slightly differently from the generic virtio
1115 * initialization of the net and block drivers.
1117 * At this stage, the console is output-only. It's too early to set
1118 * up a virtqueue, so we let the drivers do some boutique early-output
1121 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1123 early_put_chars = put_chars;
1124 return hvc_instantiate(0, 0, &hv_ops);
1127 int init_port_console(struct port *port)
1132 * The Host's telling us this port is a console port. Hook it
1133 * up with an hvc console.
1135 * To set up and manage our virtual console, we call
1138 * The first argument of hvc_alloc() is the virtual console
1139 * number. The second argument is the parameter for the
1140 * notification mechanism (like irq number). We currently
1141 * leave this as zero, virtqueues have implicit notifications.
1143 * The third argument is a "struct hv_ops" containing the
1144 * put_chars() get_chars(), notifier_add() and notifier_del()
1145 * pointers. The final argument is the output buffer size: we
1146 * can do any size, so we put PAGE_SIZE here.
1148 port->cons.vtermno = pdrvdata.next_vtermno;
1150 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1151 if (IS_ERR(port->cons.hvc)) {
1152 ret = PTR_ERR(port->cons.hvc);
1154 "error %d allocating hvc for port\n", ret);
1155 port->cons.hvc = NULL;
1158 spin_lock_irq(&pdrvdata_lock);
1159 pdrvdata.next_vtermno++;
1160 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1161 spin_unlock_irq(&pdrvdata_lock);
1162 port->guest_connected = true;
1165 * Start using the new console output if this is the first
1166 * console to come up.
1168 if (early_put_chars)
1169 early_put_chars = NULL;
1171 /* Notify host of port being opened */
1172 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1177 static ssize_t show_port_name(struct device *dev,
1178 struct device_attribute *attr, char *buffer)
1182 port = dev_get_drvdata(dev);
1184 return sprintf(buffer, "%s\n", port->name);
1187 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1189 static struct attribute *port_sysfs_entries[] = {
1190 &dev_attr_name.attr,
1194 static struct attribute_group port_attribute_group = {
1195 .name = NULL, /* put in device directory */
1196 .attrs = port_sysfs_entries,
1199 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1200 size_t count, loff_t *offp)
1204 ssize_t ret, out_offset, out_count;
1207 buf = kmalloc(out_count, GFP_KERNEL);
1211 port = filp->private_data;
1213 out_offset += snprintf(buf + out_offset, out_count,
1214 "name: %s\n", port->name ? port->name : "");
1215 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1216 "guest_connected: %d\n", port->guest_connected);
1217 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1218 "host_connected: %d\n", port->host_connected);
1219 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1220 "outvq_full: %d\n", port->outvq_full);
1221 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1222 "bytes_sent: %lu\n", port->stats.bytes_sent);
1223 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1224 "bytes_received: %lu\n",
1225 port->stats.bytes_received);
1226 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1227 "bytes_discarded: %lu\n",
1228 port->stats.bytes_discarded);
1229 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1231 is_console_port(port) ? "yes" : "no");
1232 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1233 "console_vtermno: %u\n", port->cons.vtermno);
1235 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1240 static const struct file_operations port_debugfs_ops = {
1241 .owner = THIS_MODULE,
1242 .open = simple_open,
1243 .read = debugfs_read,
1246 static void set_console_size(struct port *port, u16 rows, u16 cols)
1248 if (!port || !is_console_port(port))
1251 port->cons.ws.ws_row = rows;
1252 port->cons.ws.ws_col = cols;
1255 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1257 struct port_buffer *buf;
1258 unsigned int nr_added_bufs;
1263 buf = alloc_buf(PAGE_SIZE);
1267 spin_lock_irq(lock);
1268 ret = add_inbuf(vq, buf);
1270 spin_unlock_irq(lock);
1275 spin_unlock_irq(lock);
1278 return nr_added_bufs;
1281 static void send_sigio_to_port(struct port *port)
1283 if (port->async_queue && port->guest_connected)
1284 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1287 static int add_port(struct ports_device *portdev, u32 id)
1289 char debugfs_name[16];
1291 struct port_buffer *buf;
1293 unsigned int nr_added_bufs;
1296 port = kmalloc(sizeof(*port), GFP_KERNEL);
1301 kref_init(&port->kref);
1303 port->portdev = portdev;
1308 port->cons.hvc = NULL;
1309 port->async_queue = NULL;
1311 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1313 port->host_connected = port->guest_connected = false;
1314 port->stats = (struct port_stats) { 0 };
1316 port->outvq_full = false;
1318 port->in_vq = portdev->in_vqs[port->id];
1319 port->out_vq = portdev->out_vqs[port->id];
1321 port->cdev = cdev_alloc();
1323 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1327 port->cdev->ops = &port_fops;
1329 devt = MKDEV(portdev->chr_major, id);
1330 err = cdev_add(port->cdev, devt, 1);
1332 dev_err(&port->portdev->vdev->dev,
1333 "Error %d adding cdev for port %u\n", err, id);
1336 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1337 devt, port, "vport%up%u",
1338 port->portdev->drv_index, id);
1339 if (IS_ERR(port->dev)) {
1340 err = PTR_ERR(port->dev);
1341 dev_err(&port->portdev->vdev->dev,
1342 "Error %d creating device for port %u\n",
1347 spin_lock_init(&port->inbuf_lock);
1348 spin_lock_init(&port->outvq_lock);
1349 init_waitqueue_head(&port->waitqueue);
1351 /* Fill the in_vq with buffers so the host can send us data. */
1352 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1353 if (!nr_added_bufs) {
1354 dev_err(port->dev, "Error allocating inbufs\n");
1360 * If we're not using multiport support, this has to be a console port
1362 if (!use_multiport(port->portdev)) {
1363 err = init_port_console(port);
1368 spin_lock_irq(&portdev->ports_lock);
1369 list_add_tail(&port->list, &port->portdev->ports);
1370 spin_unlock_irq(&portdev->ports_lock);
1373 * Tell the Host we're set so that it can send us various
1374 * configuration parameters for this port (eg, port name,
1375 * caching, whether this is a console port, etc.)
1377 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1379 if (pdrvdata.debugfs_dir) {
1381 * Finally, create the debugfs file that we can use to
1382 * inspect a port's state at any time
1384 sprintf(debugfs_name, "vport%up%u",
1385 port->portdev->drv_index, id);
1386 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1387 pdrvdata.debugfs_dir,
1394 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1397 device_destroy(pdrvdata.class, port->dev->devt);
1399 cdev_del(port->cdev);
1403 /* The host might want to notify management sw about port add failure */
1404 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1408 /* No users remain, remove all port-specific data. */
1409 static void remove_port(struct kref *kref)
1413 port = container_of(kref, struct port, kref);
1415 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1416 device_destroy(pdrvdata.class, port->dev->devt);
1417 cdev_del(port->cdev);
1421 debugfs_remove(port->debugfs_file);
1426 static void remove_port_data(struct port *port)
1428 struct port_buffer *buf;
1430 /* Remove unused data this port might have received. */
1431 discard_port_data(port);
1433 reclaim_consumed_buffers(port);
1435 /* Remove buffers we queued up for the Host to send us data in. */
1436 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1441 * Port got unplugged. Remove port from portdev's list and drop the
1442 * kref reference. If no userspace has this port opened, it will
1443 * result in immediate removal the port.
1445 static void unplug_port(struct port *port)
1447 spin_lock_irq(&port->portdev->ports_lock);
1448 list_del(&port->list);
1449 spin_unlock_irq(&port->portdev->ports_lock);
1451 if (port->guest_connected) {
1452 port->guest_connected = false;
1453 port->host_connected = false;
1454 wake_up_interruptible(&port->waitqueue);
1456 /* Let the app know the port is going down. */
1457 send_sigio_to_port(port);
1460 if (is_console_port(port)) {
1461 spin_lock_irq(&pdrvdata_lock);
1462 list_del(&port->cons.list);
1463 spin_unlock_irq(&pdrvdata_lock);
1464 hvc_remove(port->cons.hvc);
1467 remove_port_data(port);
1470 * We should just assume the device itself has gone off --
1471 * else a close on an open port later will try to send out a
1474 port->portdev = NULL;
1477 * Locks around here are not necessary - a port can't be
1478 * opened after we removed the port struct from ports_list
1481 kref_put(&port->kref, remove_port);
1484 /* Any private messages that the Host and Guest want to share */
1485 static void handle_control_message(struct ports_device *portdev,
1486 struct port_buffer *buf)
1488 struct virtio_console_control *cpkt;
1493 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1495 port = find_port_by_id(portdev, cpkt->id);
1496 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1497 /* No valid header at start of buffer. Drop it. */
1498 dev_dbg(&portdev->vdev->dev,
1499 "Invalid index %u in control packet\n", cpkt->id);
1503 switch (cpkt->event) {
1504 case VIRTIO_CONSOLE_PORT_ADD:
1506 dev_dbg(&portdev->vdev->dev,
1507 "Port %u already added\n", port->id);
1508 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1511 if (cpkt->id >= portdev->config.max_nr_ports) {
1512 dev_warn(&portdev->vdev->dev,
1513 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1514 cpkt->id, portdev->config.max_nr_ports - 1);
1517 add_port(portdev, cpkt->id);
1519 case VIRTIO_CONSOLE_PORT_REMOVE:
1522 case VIRTIO_CONSOLE_CONSOLE_PORT:
1525 if (is_console_port(port))
1528 init_port_console(port);
1529 complete(&early_console_added);
1531 * Could remove the port here in case init fails - but
1532 * have to notify the host first.
1535 case VIRTIO_CONSOLE_RESIZE: {
1541 if (!is_console_port(port))
1544 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1546 set_console_size(port, size.rows, size.cols);
1548 port->cons.hvc->irq_requested = 1;
1549 resize_console(port);
1552 case VIRTIO_CONSOLE_PORT_OPEN:
1553 port->host_connected = cpkt->value;
1554 wake_up_interruptible(&port->waitqueue);
1556 * If the host port got closed and the host had any
1557 * unconsumed buffers, we'll be able to reclaim them
1560 spin_lock_irq(&port->outvq_lock);
1561 reclaim_consumed_buffers(port);
1562 spin_unlock_irq(&port->outvq_lock);
1565 * If the guest is connected, it'll be interested in
1566 * knowing the host connection state changed.
1568 send_sigio_to_port(port);
1570 case VIRTIO_CONSOLE_PORT_NAME:
1572 * If we woke up after hibernation, we can get this
1573 * again. Skip it in that case.
1579 * Skip the size of the header and the cpkt to get the size
1580 * of the name that was sent
1582 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1584 port->name = kmalloc(name_size, GFP_KERNEL);
1587 "Not enough space to store port name\n");
1590 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1592 port->name[name_size - 1] = 0;
1595 * Since we only have one sysfs attribute, 'name',
1596 * create it only if we have a name for the port.
1598 err = sysfs_create_group(&port->dev->kobj,
1599 &port_attribute_group);
1602 "Error %d creating sysfs device attributes\n",
1606 * Generate a udev event so that appropriate
1607 * symlinks can be created based on udev
1610 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1616 static void control_work_handler(struct work_struct *work)
1618 struct ports_device *portdev;
1619 struct virtqueue *vq;
1620 struct port_buffer *buf;
1623 portdev = container_of(work, struct ports_device, control_work);
1624 vq = portdev->c_ivq;
1626 spin_lock(&portdev->cvq_lock);
1627 while ((buf = virtqueue_get_buf(vq, &len))) {
1628 spin_unlock(&portdev->cvq_lock);
1633 handle_control_message(portdev, buf);
1635 spin_lock(&portdev->cvq_lock);
1636 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1637 dev_warn(&portdev->vdev->dev,
1638 "Error adding buffer to queue\n");
1642 spin_unlock(&portdev->cvq_lock);
1645 static void out_intr(struct virtqueue *vq)
1649 port = find_port_by_vq(vq->vdev->priv, vq);
1653 wake_up_interruptible(&port->waitqueue);
1656 static void in_intr(struct virtqueue *vq)
1659 unsigned long flags;
1661 port = find_port_by_vq(vq->vdev->priv, vq);
1665 spin_lock_irqsave(&port->inbuf_lock, flags);
1666 port->inbuf = get_inbuf(port);
1669 * Don't queue up data when port is closed. This condition
1670 * can be reached when a console port is not yet connected (no
1671 * tty is spawned) and the host sends out data to console
1672 * ports. For generic serial ports, the host won't
1673 * (shouldn't) send data till the guest is connected.
1675 if (!port->guest_connected)
1676 discard_port_data(port);
1678 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1680 wake_up_interruptible(&port->waitqueue);
1682 /* Send a SIGIO indicating new data in case the process asked for it */
1683 send_sigio_to_port(port);
1685 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1689 static void control_intr(struct virtqueue *vq)
1691 struct ports_device *portdev;
1693 portdev = vq->vdev->priv;
1694 schedule_work(&portdev->control_work);
1697 static void config_intr(struct virtio_device *vdev)
1699 struct ports_device *portdev;
1701 portdev = vdev->priv;
1703 if (!use_multiport(portdev)) {
1707 vdev->config->get(vdev,
1708 offsetof(struct virtio_console_config, cols),
1709 &cols, sizeof(u16));
1710 vdev->config->get(vdev,
1711 offsetof(struct virtio_console_config, rows),
1712 &rows, sizeof(u16));
1714 port = find_port_by_id(portdev, 0);
1715 set_console_size(port, rows, cols);
1718 * We'll use this way of resizing only for legacy
1719 * support. For newer userspace
1720 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1721 * to indicate console size changes so that it can be
1724 resize_console(port);
1728 static int init_vqs(struct ports_device *portdev)
1730 vq_callback_t **io_callbacks;
1732 struct virtqueue **vqs;
1733 u32 i, j, nr_ports, nr_queues;
1736 nr_ports = portdev->config.max_nr_ports;
1737 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1739 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1740 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1741 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1742 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1744 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1746 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1747 !portdev->out_vqs) {
1753 * For backward compat (newer host but older guest), the host
1754 * spawns a console port first and also inits the vqs for port
1758 io_callbacks[j] = in_intr;
1759 io_callbacks[j + 1] = out_intr;
1760 io_names[j] = "input";
1761 io_names[j + 1] = "output";
1764 if (use_multiport(portdev)) {
1765 io_callbacks[j] = control_intr;
1766 io_callbacks[j + 1] = NULL;
1767 io_names[j] = "control-i";
1768 io_names[j + 1] = "control-o";
1770 for (i = 1; i < nr_ports; i++) {
1772 io_callbacks[j] = in_intr;
1773 io_callbacks[j + 1] = out_intr;
1774 io_names[j] = "input";
1775 io_names[j + 1] = "output";
1778 /* Find the queues. */
1779 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1781 (const char **)io_names);
1786 portdev->in_vqs[0] = vqs[0];
1787 portdev->out_vqs[0] = vqs[1];
1789 if (use_multiport(portdev)) {
1790 portdev->c_ivq = vqs[j];
1791 portdev->c_ovq = vqs[j + 1];
1793 for (i = 1; i < nr_ports; i++) {
1795 portdev->in_vqs[i] = vqs[j];
1796 portdev->out_vqs[i] = vqs[j + 1];
1800 kfree(io_callbacks);
1806 kfree(portdev->out_vqs);
1807 kfree(portdev->in_vqs);
1809 kfree(io_callbacks);
1815 static const struct file_operations portdev_fops = {
1816 .owner = THIS_MODULE,
1819 static void remove_vqs(struct ports_device *portdev)
1821 portdev->vdev->config->del_vqs(portdev->vdev);
1822 kfree(portdev->in_vqs);
1823 kfree(portdev->out_vqs);
1826 static void remove_controlq_data(struct ports_device *portdev)
1828 struct port_buffer *buf;
1831 if (!use_multiport(portdev))
1834 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1837 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1842 * Once we're further in boot, we get probed like any other virtio
1845 * If the host also supports multiple console ports, we check the
1846 * config space to see how many ports the host has spawned. We
1847 * initialize each port found.
1849 static int virtcons_probe(struct virtio_device *vdev)
1851 struct ports_device *portdev;
1854 bool early = early_put_chars != NULL;
1856 /* Ensure to read early_put_chars now */
1859 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1865 /* Attach this portdev to this virtio_device, and vice-versa. */
1866 portdev->vdev = vdev;
1867 vdev->priv = portdev;
1869 spin_lock_irq(&pdrvdata_lock);
1870 portdev->drv_index = pdrvdata.index++;
1871 spin_unlock_irq(&pdrvdata_lock);
1873 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1875 if (portdev->chr_major < 0) {
1877 "Error %d registering chrdev for device %u\n",
1878 portdev->chr_major, portdev->drv_index);
1879 err = portdev->chr_major;
1884 portdev->config.max_nr_ports = 1;
1885 if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1886 offsetof(struct virtio_console_config,
1888 &portdev->config.max_nr_ports) == 0)
1891 err = init_vqs(portdev);
1893 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1897 spin_lock_init(&portdev->ports_lock);
1898 INIT_LIST_HEAD(&portdev->ports);
1901 unsigned int nr_added_bufs;
1903 spin_lock_init(&portdev->cvq_lock);
1904 INIT_WORK(&portdev->control_work, &control_work_handler);
1906 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1907 if (!nr_added_bufs) {
1909 "Error allocating buffers for control queue\n");
1915 * For backward compatibility: Create a console port
1916 * if we're running on older host.
1918 add_port(portdev, 0);
1921 spin_lock_irq(&pdrvdata_lock);
1922 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1923 spin_unlock_irq(&pdrvdata_lock);
1925 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1926 VIRTIO_CONSOLE_DEVICE_READY, 1);
1929 * If there was an early virtio console, assume that there are no
1930 * other consoles. We need to wait until the hvc_alloc matches the
1931 * hvc_instantiate, otherwise tty_open will complain, resulting in
1932 * a "Warning: unable to open an initial console" boot failure.
1933 * Without multiport this is done in add_port above. With multiport
1934 * this might take some host<->guest communication - thus we have to
1937 if (multiport && early)
1938 wait_for_completion(&early_console_added);
1943 /* The host might want to notify mgmt sw about device add failure */
1944 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1945 VIRTIO_CONSOLE_DEVICE_READY, 0);
1946 remove_vqs(portdev);
1948 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1955 static void virtcons_remove(struct virtio_device *vdev)
1957 struct ports_device *portdev;
1958 struct port *port, *port2;
1960 portdev = vdev->priv;
1962 spin_lock_irq(&pdrvdata_lock);
1963 list_del(&portdev->list);
1964 spin_unlock_irq(&pdrvdata_lock);
1966 /* Disable interrupts for vqs */
1967 vdev->config->reset(vdev);
1968 /* Finish up work that's lined up */
1969 cancel_work_sync(&portdev->control_work);
1971 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1974 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1977 * When yanking out a device, we immediately lose the
1978 * (device-side) queues. So there's no point in keeping the
1979 * guest side around till we drop our final reference. This
1980 * also means that any ports which are in an open state will
1981 * have to just stop using the port, as the vqs are going
1984 remove_controlq_data(portdev);
1985 remove_vqs(portdev);
1989 static struct virtio_device_id id_table[] = {
1990 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1994 static unsigned int features[] = {
1995 VIRTIO_CONSOLE_F_SIZE,
1996 VIRTIO_CONSOLE_F_MULTIPORT,
2000 static int virtcons_freeze(struct virtio_device *vdev)
2002 struct ports_device *portdev;
2005 portdev = vdev->priv;
2007 vdev->config->reset(vdev);
2009 virtqueue_disable_cb(portdev->c_ivq);
2010 cancel_work_sync(&portdev->control_work);
2012 * Once more: if control_work_handler() was running, it would
2013 * enable the cb as the last step.
2015 virtqueue_disable_cb(portdev->c_ivq);
2016 remove_controlq_data(portdev);
2018 list_for_each_entry(port, &portdev->ports, list) {
2019 virtqueue_disable_cb(port->in_vq);
2020 virtqueue_disable_cb(port->out_vq);
2022 * We'll ask the host later if the new invocation has
2023 * the port opened or closed.
2025 port->host_connected = false;
2026 remove_port_data(port);
2028 remove_vqs(portdev);
2033 static int virtcons_restore(struct virtio_device *vdev)
2035 struct ports_device *portdev;
2039 portdev = vdev->priv;
2041 ret = init_vqs(portdev);
2045 if (use_multiport(portdev))
2046 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
2048 list_for_each_entry(port, &portdev->ports, list) {
2049 port->in_vq = portdev->in_vqs[port->id];
2050 port->out_vq = portdev->out_vqs[port->id];
2052 fill_queue(port->in_vq, &port->inbuf_lock);
2054 /* Get port open/close status on the host */
2055 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2058 * If a port was open at the time of suspending, we
2059 * have to let the host know that it's still open.
2061 if (port->guest_connected)
2062 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2068 static struct virtio_driver virtio_console = {
2069 .feature_table = features,
2070 .feature_table_size = ARRAY_SIZE(features),
2071 .driver.name = KBUILD_MODNAME,
2072 .driver.owner = THIS_MODULE,
2073 .id_table = id_table,
2074 .probe = virtcons_probe,
2075 .remove = virtcons_remove,
2076 .config_changed = config_intr,
2078 .freeze = virtcons_freeze,
2079 .restore = virtcons_restore,
2083 static int __init init(void)
2087 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2088 if (IS_ERR(pdrvdata.class)) {
2089 err = PTR_ERR(pdrvdata.class);
2090 pr_err("Error %d creating virtio-ports class\n", err);
2094 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2095 if (!pdrvdata.debugfs_dir) {
2096 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
2097 PTR_ERR(pdrvdata.debugfs_dir));
2099 INIT_LIST_HEAD(&pdrvdata.consoles);
2100 INIT_LIST_HEAD(&pdrvdata.portdevs);
2102 err = register_virtio_driver(&virtio_console);
2104 pr_err("Error %d registering virtio driver\n", err);
2109 if (pdrvdata.debugfs_dir)
2110 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2111 class_destroy(pdrvdata.class);
2115 static void __exit fini(void)
2117 unregister_virtio_driver(&virtio_console);
2119 class_destroy(pdrvdata.class);
2120 if (pdrvdata.debugfs_dir)
2121 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2126 MODULE_DEVICE_TABLE(virtio, id_table);
2127 MODULE_DESCRIPTION("Virtio console driver");
2128 MODULE_LICENSE("GPL");