Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 3 | * Copyright (C) 2009, 2010 Red Hat, Inc. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 4 | * |
| 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 Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 19 | #include <linux/cdev.h> |
| 20 | #include <linux/device.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 21 | #include <linux/err.h> |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 22 | #include <linux/fs.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 23 | #include <linux/init.h> |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 24 | #include <linux/list.h> |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 25 | #include <linux/poll.h> |
| 26 | #include <linux/sched.h> |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 28 | #include <linux/virtio.h> |
| 29 | #include <linux/virtio_console.h> |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 30 | #include <linux/wait.h> |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 31 | #include <linux/workqueue.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 32 | #include "hvc_console.h" |
| 33 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 34 | /* |
| 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 | */ |
| 42 | struct ports_driver_data { |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 43 | /* Used for registering chardevs */ |
| 44 | struct class *class; |
| 45 | |
| 46 | /* Number of devices this driver is handling */ |
| 47 | unsigned int index; |
| 48 | |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 49 | /* |
| 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 Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 61 | /* All the console devices handled by this driver */ |
| 62 | struct list_head consoles; |
| 63 | }; |
| 64 | static struct ports_driver_data pdrvdata; |
| 65 | |
| 66 | DEFINE_SPINLOCK(pdrvdata_lock); |
| 67 | |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 68 | /* This struct holds information that's relevant only for console ports */ |
| 69 | struct 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 Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 86 | struct 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 98 | /* |
| 99 | * This is a per-device struct that stores data common to all the |
| 100 | * ports for that device (vdev->priv). |
| 101 | */ |
| 102 | struct 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 Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 131 | |
| 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 137 | }; |
| 138 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 139 | /* This struct holds the per-port data */ |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 140 | struct port { |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 141 | /* Next port in the list, head is in the ports_device */ |
| 142 | struct list_head list; |
| 143 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 144 | /* Pointer to the parent virtio_console device */ |
| 145 | struct ports_device *portdev; |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 146 | |
| 147 | /* The current buffer from which data has to be fed to readers */ |
| 148 | struct port_buffer *inbuf; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 149 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 150 | /* |
| 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 Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 157 | /* The IO vqs for this port */ |
| 158 | struct virtqueue *in_vq, *out_vq; |
| 159 | |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 160 | /* |
| 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 165 | |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 166 | /* Each port associates with a separate char device */ |
| 167 | struct cdev cdev; |
| 168 | struct device *dev; |
| 169 | |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 170 | /* A waitqueue for poll() or blocking read operations */ |
| 171 | wait_queue_head_t waitqueue; |
| 172 | |
Amit Shah | 431edb8 | 2009-12-21 21:57:40 +0530 | [diff] [blame^] | 173 | /* The 'name' of the port that we expose via sysfs properties */ |
| 174 | char *name; |
| 175 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 176 | /* The 'id' to identify the port with the Host */ |
| 177 | u32 id; |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 178 | |
| 179 | /* Is the host device open */ |
| 180 | bool host_connected; |
Amit Shah | 3c7969c | 2009-11-26 11:25:38 +0530 | [diff] [blame] | 181 | |
| 182 | /* We should allow only one process to open a port */ |
| 183 | bool guest_connected; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 184 | }; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 185 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 186 | /* This is the very early arch-specified put chars function. */ |
| 187 | static int (*early_put_chars)(u32, const char *, int); |
| 188 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 189 | static struct port *find_port_by_vtermno(u32 vtermno) |
| 190 | { |
| 191 | struct port *port; |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 192 | struct console *cons; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 193 | unsigned long flags; |
| 194 | |
| 195 | spin_lock_irqsave(&pdrvdata_lock, flags); |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 196 | list_for_each_entry(cons, &pdrvdata.consoles, list) { |
| 197 | if (cons->vtermno == vtermno) { |
| 198 | port = container_of(cons, struct port, cons); |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 199 | goto out; |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 200 | } |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 201 | } |
| 202 | port = NULL; |
| 203 | out: |
| 204 | spin_unlock_irqrestore(&pdrvdata_lock, flags); |
| 205 | return port; |
| 206 | } |
| 207 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 208 | static 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; |
| 218 | out: |
| 219 | spin_unlock_irqrestore(&portdev->ports_lock, flags); |
| 220 | |
| 221 | return port; |
| 222 | } |
| 223 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 224 | static struct port *find_port_by_vq(struct ports_device *portdev, |
| 225 | struct virtqueue *vq) |
| 226 | { |
| 227 | struct port *port; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 228 | unsigned long flags; |
| 229 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 230 | spin_lock_irqsave(&portdev->ports_lock, flags); |
| 231 | list_for_each_entry(port, &portdev->ports, list) |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 232 | if (port->in_vq == vq || port->out_vq == vq) |
| 233 | goto out; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 234 | port = NULL; |
| 235 | out: |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 236 | spin_unlock_irqrestore(&portdev->ports_lock, flags); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 237 | return port; |
| 238 | } |
| 239 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 240 | static bool is_console_port(struct port *port) |
| 241 | { |
| 242 | if (port->cons.hvc) |
| 243 | return true; |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | static 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 Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 258 | static void free_buf(struct port_buffer *buf) |
| 259 | { |
| 260 | kfree(buf->buf); |
| 261 | kfree(buf); |
| 262 | } |
| 263 | |
| 264 | static 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 | |
| 279 | free_buf: |
| 280 | kfree(buf); |
| 281 | fail: |
| 282 | return NULL; |
| 283 | } |
| 284 | |
Amit Shah | a3cde44 | 2010-01-18 19:15:03 +0530 | [diff] [blame] | 285 | /* Callers should take appropriate locks */ |
| 286 | static 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 Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 301 | /* |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 302 | * 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 Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 307 | static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf) |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 308 | { |
| 309 | struct scatterlist sg[1]; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 310 | int ret; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 311 | |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 312 | sg_init_one(sg, buf->buf, buf->size); |
| 313 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 314 | ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf); |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 315 | vq->vq_ops->kick(vq); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 333 | static 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 Shah | f997f00b | 2009-12-21 17:28:51 +0530 | [diff] [blame] | 359 | static 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(); |
| 386 | fail: |
| 387 | /* We're expected to return the amount of data we wrote */ |
| 388 | return len; |
| 389 | } |
| 390 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 391 | /* |
| 392 | * Give out the data that's requested from the buffer that we have |
| 393 | * queued up. |
| 394 | */ |
Amit Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 395 | static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count, |
| 396 | bool to_user) |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 397 | { |
| 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 Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 405 | out_count = min(out_count, buf->len - buf->offset); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 406 | |
Amit Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 407 | if (to_user) { |
| 408 | ssize_t ret; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 409 | |
Amit Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 410 | 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 Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 417 | 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 Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 428 | dev_warn(port->dev, "failed add_buf\n"); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 429 | |
| 430 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
| 431 | } |
Amit Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 432 | /* Return the number of bytes actually copied */ |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 433 | return out_count; |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 434 | } |
| 435 | |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 436 | /* The condition that must be true for polling to end */ |
| 437 | static bool wait_is_over(struct port *port) |
| 438 | { |
| 439 | return port_has_data(port) || !port->host_connected; |
| 440 | } |
| 441 | |
| 442 | static 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 | |
| 482 | static 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); |
| 504 | free_buf: |
| 505 | kfree(buf); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static 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 | |
| 528 | static 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 Shah | 3c7969c | 2009-11-26 11:25:38 +0530 | [diff] [blame] | 537 | port->guest_connected = false; |
| 538 | |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static 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 Shah | 3c7969c | 2009-11-26 11:25:38 +0530 | [diff] [blame] | 557 | /* 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 Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 567 | /* 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 | */ |
| 579 | static 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 Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 588 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 589 | * The put_chars() callback is pretty straightforward. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 590 | * |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 591 | * 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 Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 596 | static int put_chars(u32 vtermno, const char *buf, int count) |
| 597 | { |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 598 | struct port *port; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 599 | |
| 600 | port = find_port_by_vtermno(vtermno); |
| 601 | if (!port) |
| 602 | return 0; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 603 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 604 | if (unlikely(early_put_chars)) |
| 605 | return early_put_chars(vtermno, buf, count); |
| 606 | |
Amit Shah | f997f00b | 2009-12-21 17:28:51 +0530 | [diff] [blame] | 607 | return send_buf(port, (void *)buf, count); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 608 | } |
| 609 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 610 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 611 | * get_chars() is the callback from the hvc_console infrastructure |
| 612 | * when an interrupt is received. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 613 | * |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 614 | * We call out to fill_readbuf that gets us the required data from the |
| 615 | * buffers that are queued up. |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 616 | */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 617 | static int get_chars(u32 vtermno, char *buf, int count) |
| 618 | { |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 619 | struct port *port; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 620 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 621 | port = find_port_by_vtermno(vtermno); |
| 622 | if (!port) |
| 623 | return 0; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 624 | |
| 625 | /* If we don't have an input queue yet, we can't get input. */ |
| 626 | BUG_ON(!port->in_vq); |
| 627 | |
Amit Shah | b766cee | 2009-12-21 21:26:45 +0530 | [diff] [blame] | 628 | return fill_readbuf(port, buf, count, false); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 629 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 630 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 631 | static void resize_console(struct port *port) |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 632 | { |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 633 | struct virtio_device *vdev; |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 634 | struct winsize ws; |
| 635 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 636 | 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 Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 644 | hvc_resize(port->cons.hvc, ws); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 648 | static void virtcons_apply_config(struct virtio_device *vdev) |
| 649 | { |
| 650 | resize_console(find_port_by_vtermno(0)); |
| 651 | } |
| 652 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 653 | /* We set the configuration at this point, since we now have a tty */ |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 654 | static int notifier_add_vio(struct hvc_struct *hp, int data) |
| 655 | { |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 656 | struct port *port; |
| 657 | |
| 658 | port = find_port_by_vtermno(hp->vtermno); |
| 659 | if (!port) |
| 660 | return -EINVAL; |
| 661 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 662 | hp->irq_requested = 1; |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 663 | resize_console(port); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 664 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static void notifier_del_vio(struct hvc_struct *hp, int data) |
| 669 | { |
| 670 | hp->irq_requested = 0; |
| 671 | } |
| 672 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 673 | /* The operations for console ports. */ |
Rusty Russell | 1dff399 | 2009-11-28 12:20:26 +0530 | [diff] [blame] | 674 | static const struct hv_ops hv_ops = { |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 675 | .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 | */ |
| 691 | int __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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 697 | int init_port_console(struct port *port) |
Amit Shah | cfa6d37 | 2010-01-18 19:15:10 +0530 | [diff] [blame] | 698 | { |
| 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 Shah | 3c7969c | 2009-11-26 11:25:38 +0530 | [diff] [blame] | 730 | port->guest_connected = true; |
Amit Shah | cfa6d37 | 2010-01-18 19:15:10 +0530 | [diff] [blame] | 731 | |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 732 | /* Notify host of port being opened */ |
| 733 | send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); |
| 734 | |
Amit Shah | cfa6d37 | 2010-01-18 19:15:10 +0530 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
Amit Shah | 431edb8 | 2009-12-21 21:57:40 +0530 | [diff] [blame^] | 738 | static 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 | |
| 748 | static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL); |
| 749 | |
| 750 | static struct attribute *port_sysfs_entries[] = { |
| 751 | &dev_attr_name.attr, |
| 752 | NULL |
| 753 | }; |
| 754 | |
| 755 | static struct attribute_group port_attribute_group = { |
| 756 | .name = NULL, /* put in device directory */ |
| 757 | .attrs = port_sysfs_entries, |
| 758 | }; |
| 759 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 760 | /* Any private messages that the Host and Guest want to share */ |
| 761 | static 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 Shah | 431edb8 | 2009-12-21 21:57:40 +0530 | [diff] [blame^] | 766 | size_t name_size; |
| 767 | int err; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 768 | |
| 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 Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 798 | case VIRTIO_CONSOLE_PORT_OPEN: |
| 799 | port->host_connected = cpkt->value; |
| 800 | wake_up_interruptible(&port->waitqueue); |
| 801 | break; |
Amit Shah | 431edb8 | 2009-12-21 21:57:40 +0530 | [diff] [blame^] | 802 | 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | static 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 | |
| 863 | static 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 Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 877 | wake_up_interruptible(&port->waitqueue); |
| 878 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 879 | if (is_console_port(port) && hvc_poll(port->cons.hvc)) |
| 880 | hvc_kick(); |
| 881 | } |
| 882 | |
| 883 | static 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 | |
| 891 | static 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 | |
| 912 | static int add_port(struct ports_device *portdev, u32 id) |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 913 | { |
| 914 | struct port *port; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 915 | struct port_buffer *inbuf; |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 916 | dev_t devt; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 917 | int err; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 918 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 919 | port = kmalloc(sizeof(*port), GFP_KERNEL); |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 920 | if (!port) { |
| 921 | err = -ENOMEM; |
| 922 | goto fail; |
Amit Shah | f550804 | 2010-01-18 19:14:59 +0530 | [diff] [blame] | 923 | } |
Rusty Russell | 7395448 | 2010-01-18 19:15:04 +0530 | [diff] [blame] | 924 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 925 | port->portdev = portdev; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 926 | port->id = id; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 927 | |
Amit Shah | 431edb8 | 2009-12-21 21:57:40 +0530 | [diff] [blame^] | 928 | port->name = NULL; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 929 | port->inbuf = NULL; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 930 | port->cons.hvc = NULL; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 931 | |
Amit Shah | 3c7969c | 2009-11-26 11:25:38 +0530 | [diff] [blame] | 932 | port->host_connected = port->guest_connected = false; |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 933 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 934 | port->in_vq = portdev->in_vqs[port->id]; |
| 935 | port->out_vq = portdev->out_vqs[port->id]; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 936 | |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 937 | cdev_init(&port->cdev, &port_fops); |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 938 | |
| 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 Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 957 | spin_lock_init(&port->inbuf_lock); |
Amit Shah | 2030fa4 | 2009-12-21 21:49:30 +0530 | [diff] [blame] | 958 | init_waitqueue_head(&port->waitqueue); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 959 | |
| 960 | inbuf = alloc_buf(PAGE_SIZE); |
| 961 | if (!inbuf) { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 962 | err = -ENOMEM; |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 963 | goto free_device; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 964 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 965 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 966 | /* Register the input buffer the first time. */ |
| 967 | add_inbuf(port->in_vq, inbuf); |
| 968 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 969 | /* |
| 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 Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 988 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 989 | return 0; |
| 990 | |
| 991 | free_inbuf: |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame] | 992 | free_buf(inbuf); |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 993 | free_device: |
| 994 | device_destroy(pdrvdata.class, port->dev->devt); |
| 995 | free_cdev: |
| 996 | cdev_del(&port->cdev); |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 997 | free_port: |
| 998 | kfree(port); |
| 999 | fail: |
| 1000 | return err; |
| 1001 | } |
| 1002 | |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1003 | static int init_vqs(struct ports_device *portdev) |
| 1004 | { |
| 1005 | vq_callback_t **io_callbacks; |
| 1006 | char **io_names; |
| 1007 | struct virtqueue **vqs; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1008 | u32 i, j, nr_ports, nr_queues; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1009 | int err; |
| 1010 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1011 | nr_ports = portdev->config.max_nr_ports; |
| 1012 | nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1013 | |
| 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1042 | /* |
| 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 Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1053 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1054 | 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 Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1068 | /* 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1075 | j = 0; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1076 | portdev->in_vqs[0] = vqs[0]; |
| 1077 | portdev->out_vqs[0] = vqs[1]; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1078 | j += 2; |
| 1079 | if (use_multiport(portdev)) { |
| 1080 | portdev->c_ivq = vqs[j]; |
| 1081 | portdev->c_ovq = vqs[j + 1]; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1082 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1083 | 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 Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1089 | kfree(io_callbacks); |
| 1090 | kfree(io_names); |
| 1091 | kfree(vqs); |
| 1092 | |
| 1093 | return 0; |
| 1094 | |
| 1095 | free_names: |
| 1096 | kfree(io_names); |
| 1097 | free_callbacks: |
| 1098 | kfree(io_callbacks); |
| 1099 | free_outvqs: |
| 1100 | kfree(portdev->out_vqs); |
| 1101 | free_invqs: |
| 1102 | kfree(portdev->in_vqs); |
| 1103 | free_vqs: |
| 1104 | kfree(vqs); |
| 1105 | fail: |
| 1106 | return err; |
| 1107 | } |
| 1108 | |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 1109 | static const struct file_operations portdev_fops = { |
| 1110 | .owner = THIS_MODULE, |
| 1111 | }; |
| 1112 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1113 | /* |
| 1114 | * Once we're further in boot, we get probed like any other virtio |
| 1115 | * device. |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1116 | * |
| 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 Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1120 | */ |
| 1121 | static int __devinit virtcons_probe(struct virtio_device *vdev) |
| 1122 | { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1123 | struct ports_device *portdev; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1124 | u32 i; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1125 | int err; |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1126 | bool multiport; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1127 | |
| 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 Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 1138 | 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 Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1152 | 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 Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1181 | err = init_vqs(portdev); |
| 1182 | if (err < 0) { |
| 1183 | dev_err(&vdev->dev, "Error %d initializing vqs\n", err); |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 1184 | goto free_chrdev; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 1185 | } |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1186 | |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1187 | 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 Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1199 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 1200 | /* Start using the new console output. */ |
| 1201 | early_put_chars = NULL; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1202 | return 0; |
| 1203 | |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 1204 | free_chrdev: |
| 1205 | unregister_chrdev(portdev->chr_major, "virtio-portsdev"); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1206 | free: |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 1207 | kfree(portdev); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1208 | fail: |
| 1209 | return err; |
| 1210 | } |
| 1211 | |
| 1212 | static struct virtio_device_id id_table[] = { |
| 1213 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, |
| 1214 | { 0 }, |
| 1215 | }; |
| 1216 | |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 1217 | static unsigned int features[] = { |
| 1218 | VIRTIO_CONSOLE_F_SIZE, |
Amit Shah | 17634ba | 2009-12-21 21:03:25 +0530 | [diff] [blame] | 1219 | VIRTIO_CONSOLE_F_MULTIPORT, |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 1220 | }; |
| 1221 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1222 | static struct virtio_driver virtio_console = { |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 1223 | .feature_table = features, |
| 1224 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1225 | .driver.name = KBUILD_MODNAME, |
| 1226 | .driver.owner = THIS_MODULE, |
| 1227 | .id_table = id_table, |
| 1228 | .probe = virtcons_probe, |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 1229 | .config_changed = virtcons_apply_config, |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1230 | }; |
| 1231 | |
| 1232 | static int __init init(void) |
| 1233 | { |
Amit Shah | fb08bd2 | 2009-12-21 21:36:04 +0530 | [diff] [blame] | 1234 | 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 Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 1242 | INIT_LIST_HEAD(&pdrvdata.consoles); |
| 1243 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1244 | return register_virtio_driver(&virtio_console); |
| 1245 | } |
| 1246 | module_init(init); |
| 1247 | |
| 1248 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 1249 | MODULE_DESCRIPTION("Virtio console driver"); |
| 1250 | MODULE_LICENSE("GPL"); |