]>
Commit | Line | Data |
---|---|---|
a23ea924 RR |
1 | /* |
2 | * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation | |
17634ba2 | 3 | * Copyright (C) 2009, 2010 Red Hat, Inc. |
31610434 RR |
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 | */ | |
fb08bd27 | 19 | #include <linux/cdev.h> |
d99393ef | 20 | #include <linux/debugfs.h> |
fb08bd27 | 21 | #include <linux/device.h> |
31610434 | 22 | #include <linux/err.h> |
2030fa49 | 23 | #include <linux/fs.h> |
31610434 | 24 | #include <linux/init.h> |
38edf58d | 25 | #include <linux/list.h> |
2030fa49 AS |
26 | #include <linux/poll.h> |
27 | #include <linux/sched.h> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
38edf58d | 29 | #include <linux/spinlock.h> |
31610434 RR |
30 | #include <linux/virtio.h> |
31 | #include <linux/virtio_console.h> | |
2030fa49 | 32 | #include <linux/wait.h> |
17634ba2 | 33 | #include <linux/workqueue.h> |
31610434 RR |
34 | #include "hvc_console.h" |
35 | ||
38edf58d AS |
36 | /* |
37 | * This is a global struct for storing common data for all the devices | |
38 | * this driver handles. | |
39 | * | |
40 | * Mainly, it has a linked list for all the consoles in one place so | |
41 | * that callbacks from hvc for get_chars(), put_chars() work properly | |
42 | * across multiple devices and multiple ports per device. | |
43 | */ | |
44 | struct ports_driver_data { | |
fb08bd27 AS |
45 | /* Used for registering chardevs */ |
46 | struct class *class; | |
47 | ||
d99393ef AS |
48 | /* Used for exporting per-port information to debugfs */ |
49 | struct dentry *debugfs_dir; | |
50 | ||
6bdf2afd AS |
51 | /* List of all the devices we're handling */ |
52 | struct list_head portdevs; | |
53 | ||
fb08bd27 AS |
54 | /* Number of devices this driver is handling */ |
55 | unsigned int index; | |
56 | ||
d8a02bd5 RR |
57 | /* |
58 | * This is used to keep track of the number of hvc consoles | |
59 | * spawned by this driver. This number is given as the first | |
60 | * argument to hvc_alloc(). To correctly map an initial | |
61 | * console spawned via hvc_instantiate to the console being | |
62 | * hooked up via hvc_alloc, we need to pass the same vtermno. | |
63 | * | |
64 | * We also just assume the first console being initialised was | |
65 | * the first one that got used as the initial console. | |
66 | */ | |
67 | unsigned int next_vtermno; | |
68 | ||
38edf58d AS |
69 | /* All the console devices handled by this driver */ |
70 | struct list_head consoles; | |
71 | }; | |
72 | static struct ports_driver_data pdrvdata; | |
73 | ||
74 | DEFINE_SPINLOCK(pdrvdata_lock); | |
75 | ||
4f23c573 AS |
76 | /* This struct holds information that's relevant only for console ports */ |
77 | struct console { | |
78 | /* We'll place all consoles in a list in the pdrvdata struct */ | |
79 | struct list_head list; | |
80 | ||
81 | /* The hvc device associated with this console port */ | |
82 | struct hvc_struct *hvc; | |
83 | ||
9778829c AS |
84 | /* The size of the console */ |
85 | struct winsize ws; | |
86 | ||
4f23c573 AS |
87 | /* |
88 | * This number identifies the number that we used to register | |
89 | * with hvc in hvc_instantiate() and hvc_alloc(); this is the | |
90 | * number passed on by the hvc callbacks to us to | |
91 | * differentiate between the other console ports handled by | |
92 | * this driver | |
93 | */ | |
94 | u32 vtermno; | |
95 | }; | |
96 | ||
fdb9a054 AS |
97 | struct port_buffer { |
98 | char *buf; | |
99 | ||
100 | /* size of the buffer in *buf above */ | |
101 | size_t size; | |
102 | ||
103 | /* used length of the buffer */ | |
104 | size_t len; | |
105 | /* offset in the buf from which to consume data */ | |
106 | size_t offset; | |
107 | }; | |
108 | ||
17634ba2 AS |
109 | /* |
110 | * This is a per-device struct that stores data common to all the | |
111 | * ports for that device (vdev->priv). | |
112 | */ | |
113 | struct ports_device { | |
6bdf2afd AS |
114 | /* Next portdev in the list, head is in the pdrvdata struct */ |
115 | struct list_head list; | |
116 | ||
17634ba2 AS |
117 | /* |
118 | * Workqueue handlers where we process deferred work after | |
119 | * notification | |
120 | */ | |
121 | struct work_struct control_work; | |
122 | ||
123 | struct list_head ports; | |
124 | ||
125 | /* To protect the list of ports */ | |
126 | spinlock_t ports_lock; | |
127 | ||
128 | /* To protect the vq operations for the control channel */ | |
129 | spinlock_t cvq_lock; | |
130 | ||
131 | /* The current config space is stored here */ | |
b99fa815 | 132 | struct virtio_console_config config; |
17634ba2 AS |
133 | |
134 | /* The virtio device we're associated with */ | |
135 | struct virtio_device *vdev; | |
136 | ||
137 | /* | |
138 | * A couple of virtqueues for the control channel: one for | |
139 | * guest->host transfers, one for host->guest transfers | |
140 | */ | |
141 | struct virtqueue *c_ivq, *c_ovq; | |
142 | ||
143 | /* Array of per-port IO virtqueues */ | |
144 | struct virtqueue **in_vqs, **out_vqs; | |
fb08bd27 AS |
145 | |
146 | /* Used for numbering devices for sysfs and debugfs */ | |
147 | unsigned int drv_index; | |
148 | ||
149 | /* Major number for this device. Ports will be created as minors. */ | |
150 | int chr_major; | |
17634ba2 AS |
151 | }; |
152 | ||
1c85bf35 | 153 | /* This struct holds the per-port data */ |
21206ede | 154 | struct port { |
17634ba2 AS |
155 | /* Next port in the list, head is in the ports_device */ |
156 | struct list_head list; | |
157 | ||
1c85bf35 AS |
158 | /* Pointer to the parent virtio_console device */ |
159 | struct ports_device *portdev; | |
fdb9a054 AS |
160 | |
161 | /* The current buffer from which data has to be fed to readers */ | |
162 | struct port_buffer *inbuf; | |
21206ede | 163 | |
203baab8 AS |
164 | /* |
165 | * To protect the operations on the in_vq associated with this | |
166 | * port. Has to be a spinlock because it can be called from | |
167 | * interrupt context (get_char()). | |
168 | */ | |
169 | spinlock_t inbuf_lock; | |
170 | ||
cdfadfc1 AS |
171 | /* Protect the operations on the out_vq. */ |
172 | spinlock_t outvq_lock; | |
173 | ||
1c85bf35 AS |
174 | /* The IO vqs for this port */ |
175 | struct virtqueue *in_vq, *out_vq; | |
176 | ||
d99393ef AS |
177 | /* File in the debugfs directory that exposes this port's information */ |
178 | struct dentry *debugfs_file; | |
179 | ||
4f23c573 AS |
180 | /* |
181 | * The entries in this struct will be valid if this port is | |
182 | * hooked up to an hvc console | |
183 | */ | |
184 | struct console cons; | |
17634ba2 | 185 | |
fb08bd27 AS |
186 | /* Each port associates with a separate char device */ |
187 | struct cdev cdev; | |
188 | struct device *dev; | |
189 | ||
2030fa49 AS |
190 | /* A waitqueue for poll() or blocking read operations */ |
191 | wait_queue_head_t waitqueue; | |
192 | ||
431edb8a AS |
193 | /* The 'name' of the port that we expose via sysfs properties */ |
194 | char *name; | |
195 | ||
17634ba2 AS |
196 | /* The 'id' to identify the port with the Host */ |
197 | u32 id; | |
2030fa49 | 198 | |
cdfadfc1 AS |
199 | bool outvq_full; |
200 | ||
2030fa49 AS |
201 | /* Is the host device open */ |
202 | bool host_connected; | |
3c7969cc AS |
203 | |
204 | /* We should allow only one process to open a port */ | |
205 | bool guest_connected; | |
21206ede | 206 | }; |
31610434 | 207 | |
971f3390 RR |
208 | /* This is the very early arch-specified put chars function. */ |
209 | static int (*early_put_chars)(u32, const char *, int); | |
210 | ||
38edf58d AS |
211 | static struct port *find_port_by_vtermno(u32 vtermno) |
212 | { | |
213 | struct port *port; | |
4f23c573 | 214 | struct console *cons; |
38edf58d AS |
215 | unsigned long flags; |
216 | ||
217 | spin_lock_irqsave(&pdrvdata_lock, flags); | |
4f23c573 AS |
218 | list_for_each_entry(cons, &pdrvdata.consoles, list) { |
219 | if (cons->vtermno == vtermno) { | |
220 | port = container_of(cons, struct port, cons); | |
38edf58d | 221 | goto out; |
4f23c573 | 222 | } |
38edf58d AS |
223 | } |
224 | port = NULL; | |
225 | out: | |
226 | spin_unlock_irqrestore(&pdrvdata_lock, flags); | |
227 | return port; | |
228 | } | |
229 | ||
04950cdf AS |
230 | static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev, |
231 | dev_t dev) | |
232 | { | |
233 | struct port *port; | |
234 | unsigned long flags; | |
235 | ||
236 | spin_lock_irqsave(&portdev->ports_lock, flags); | |
237 | list_for_each_entry(port, &portdev->ports, list) | |
238 | if (port->cdev.dev == dev) | |
239 | goto out; | |
240 | port = NULL; | |
241 | out: | |
242 | spin_unlock_irqrestore(&portdev->ports_lock, flags); | |
243 | ||
244 | return port; | |
245 | } | |
246 | ||
247 | static struct port *find_port_by_devt(dev_t dev) | |
248 | { | |
249 | struct ports_device *portdev; | |
250 | struct port *port; | |
251 | unsigned long flags; | |
252 | ||
253 | spin_lock_irqsave(&pdrvdata_lock, flags); | |
254 | list_for_each_entry(portdev, &pdrvdata.portdevs, list) { | |
255 | port = find_port_by_devt_in_portdev(portdev, dev); | |
256 | if (port) | |
257 | goto out; | |
258 | } | |
259 | port = NULL; | |
260 | out: | |
261 | spin_unlock_irqrestore(&pdrvdata_lock, flags); | |
262 | return port; | |
263 | } | |
264 | ||
17634ba2 AS |
265 | static struct port *find_port_by_id(struct ports_device *portdev, u32 id) |
266 | { | |
267 | struct port *port; | |
268 | unsigned long flags; | |
269 | ||
270 | spin_lock_irqsave(&portdev->ports_lock, flags); | |
271 | list_for_each_entry(port, &portdev->ports, list) | |
272 | if (port->id == id) | |
273 | goto out; | |
274 | port = NULL; | |
275 | out: | |
276 | spin_unlock_irqrestore(&portdev->ports_lock, flags); | |
277 | ||
278 | return port; | |
279 | } | |
280 | ||
203baab8 AS |
281 | static struct port *find_port_by_vq(struct ports_device *portdev, |
282 | struct virtqueue *vq) | |
283 | { | |
284 | struct port *port; | |
203baab8 AS |
285 | unsigned long flags; |
286 | ||
17634ba2 AS |
287 | spin_lock_irqsave(&portdev->ports_lock, flags); |
288 | list_for_each_entry(port, &portdev->ports, list) | |
203baab8 AS |
289 | if (port->in_vq == vq || port->out_vq == vq) |
290 | goto out; | |
203baab8 AS |
291 | port = NULL; |
292 | out: | |
17634ba2 | 293 | spin_unlock_irqrestore(&portdev->ports_lock, flags); |
203baab8 AS |
294 | return port; |
295 | } | |
296 | ||
17634ba2 AS |
297 | static bool is_console_port(struct port *port) |
298 | { | |
299 | if (port->cons.hvc) | |
300 | return true; | |
301 | return false; | |
302 | } | |
303 | ||
304 | static inline bool use_multiport(struct ports_device *portdev) | |
305 | { | |
306 | /* | |
307 | * This condition can be true when put_chars is called from | |
308 | * early_init | |
309 | */ | |
310 | if (!portdev->vdev) | |
311 | return 0; | |
312 | return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT); | |
313 | } | |
314 | ||
fdb9a054 AS |
315 | static void free_buf(struct port_buffer *buf) |
316 | { | |
317 | kfree(buf->buf); | |
318 | kfree(buf); | |
319 | } | |
320 | ||
321 | static struct port_buffer *alloc_buf(size_t buf_size) | |
322 | { | |
323 | struct port_buffer *buf; | |
324 | ||
325 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); | |
326 | if (!buf) | |
327 | goto fail; | |
328 | buf->buf = kzalloc(buf_size, GFP_KERNEL); | |
329 | if (!buf->buf) | |
330 | goto free_buf; | |
331 | buf->len = 0; | |
332 | buf->offset = 0; | |
333 | buf->size = buf_size; | |
334 | return buf; | |
335 | ||
336 | free_buf: | |
337 | kfree(buf); | |
338 | fail: | |
339 | return NULL; | |
340 | } | |
341 | ||
a3cde449 AS |
342 | /* Callers should take appropriate locks */ |
343 | static void *get_inbuf(struct port *port) | |
344 | { | |
345 | struct port_buffer *buf; | |
346 | struct virtqueue *vq; | |
347 | unsigned int len; | |
348 | ||
349 | vq = port->in_vq; | |
505b0451 | 350 | buf = virtqueue_get_buf(vq, &len); |
a3cde449 AS |
351 | if (buf) { |
352 | buf->len = len; | |
353 | buf->offset = 0; | |
354 | } | |
355 | return buf; | |
356 | } | |
357 | ||
e27b5198 AS |
358 | /* |
359 | * Create a scatter-gather list representing our input buffer and put | |
360 | * it in the queue. | |
361 | * | |
362 | * Callers should take appropriate locks. | |
363 | */ | |
203baab8 | 364 | static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf) |
e27b5198 AS |
365 | { |
366 | struct scatterlist sg[1]; | |
203baab8 | 367 | int ret; |
1c85bf35 | 368 | |
e27b5198 AS |
369 | sg_init_one(sg, buf->buf, buf->size); |
370 | ||
505b0451 MT |
371 | ret = virtqueue_add_buf(vq, sg, 0, 1, buf); |
372 | virtqueue_kick(vq); | |
203baab8 AS |
373 | return ret; |
374 | } | |
375 | ||
88f251ac AS |
376 | /* Discard any unread data this port has. Callers lockers. */ |
377 | static void discard_port_data(struct port *port) | |
378 | { | |
379 | struct port_buffer *buf; | |
380 | struct virtqueue *vq; | |
381 | unsigned int len; | |
d6933561 | 382 | int ret; |
88f251ac AS |
383 | |
384 | vq = port->in_vq; | |
385 | if (port->inbuf) | |
386 | buf = port->inbuf; | |
387 | else | |
505b0451 | 388 | buf = virtqueue_get_buf(vq, &len); |
88f251ac | 389 | |
d6933561 AS |
390 | ret = 0; |
391 | while (buf) { | |
392 | if (add_inbuf(vq, buf) < 0) { | |
393 | ret++; | |
394 | free_buf(buf); | |
395 | } | |
505b0451 | 396 | buf = virtqueue_get_buf(vq, &len); |
88f251ac | 397 | } |
88f251ac | 398 | port->inbuf = NULL; |
d6933561 AS |
399 | if (ret) |
400 | dev_warn(port->dev, "Errors adding %d buffers back to vq\n", | |
401 | ret); | |
88f251ac AS |
402 | } |
403 | ||
203baab8 AS |
404 | static bool port_has_data(struct port *port) |
405 | { | |
406 | unsigned long flags; | |
407 | bool ret; | |
408 | ||
203baab8 | 409 | spin_lock_irqsave(&port->inbuf_lock, flags); |
d6933561 | 410 | if (port->inbuf) { |
203baab8 | 411 | ret = true; |
d6933561 AS |
412 | goto out; |
413 | } | |
414 | port->inbuf = get_inbuf(port); | |
415 | if (port->inbuf) { | |
416 | ret = true; | |
417 | goto out; | |
418 | } | |
419 | ret = false; | |
420 | out: | |
203baab8 | 421 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
203baab8 AS |
422 | return ret; |
423 | } | |
424 | ||
3425e706 AS |
425 | static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id, |
426 | unsigned int event, unsigned int value) | |
17634ba2 AS |
427 | { |
428 | struct scatterlist sg[1]; | |
429 | struct virtio_console_control cpkt; | |
430 | struct virtqueue *vq; | |
604b2ad7 | 431 | unsigned int len; |
17634ba2 | 432 | |
3425e706 | 433 | if (!use_multiport(portdev)) |
17634ba2 AS |
434 | return 0; |
435 | ||
3425e706 | 436 | cpkt.id = port_id; |
17634ba2 AS |
437 | cpkt.event = event; |
438 | cpkt.value = value; | |
439 | ||
3425e706 | 440 | vq = portdev->c_ovq; |
17634ba2 AS |
441 | |
442 | sg_init_one(sg, &cpkt, sizeof(cpkt)); | |
505b0451 MT |
443 | if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) { |
444 | virtqueue_kick(vq); | |
445 | while (!virtqueue_get_buf(vq, &len)) | |
17634ba2 AS |
446 | cpu_relax(); |
447 | } | |
448 | return 0; | |
449 | } | |
450 | ||
3425e706 AS |
451 | static ssize_t send_control_msg(struct port *port, unsigned int event, |
452 | unsigned int value) | |
453 | { | |
84ec06c5 AS |
454 | /* Did the port get unplugged before userspace closed it? */ |
455 | if (port->portdev) | |
456 | return __send_control_msg(port->portdev, port->id, event, value); | |
457 | return 0; | |
3425e706 AS |
458 | } |
459 | ||
cdfadfc1 AS |
460 | /* Callers must take the port->outvq_lock */ |
461 | static void reclaim_consumed_buffers(struct port *port) | |
462 | { | |
463 | void *buf; | |
464 | unsigned int len; | |
465 | ||
466 | while ((buf = virtqueue_get_buf(port->out_vq, &len))) { | |
467 | kfree(buf); | |
468 | port->outvq_full = false; | |
469 | } | |
470 | } | |
471 | ||
472 | static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count, | |
473 | bool nonblock) | |
f997f00b AS |
474 | { |
475 | struct scatterlist sg[1]; | |
476 | struct virtqueue *out_vq; | |
477 | ssize_t ret; | |
cdfadfc1 | 478 | unsigned long flags; |
f997f00b AS |
479 | unsigned int len; |
480 | ||
481 | out_vq = port->out_vq; | |
482 | ||
cdfadfc1 AS |
483 | spin_lock_irqsave(&port->outvq_lock, flags); |
484 | ||
485 | reclaim_consumed_buffers(port); | |
486 | ||
f997f00b | 487 | sg_init_one(sg, in_buf, in_count); |
505b0451 | 488 | ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf); |
f997f00b AS |
489 | |
490 | /* Tell Host to go! */ | |
505b0451 | 491 | virtqueue_kick(out_vq); |
f997f00b AS |
492 | |
493 | if (ret < 0) { | |
9ff4cfab | 494 | in_count = 0; |
cdfadfc1 | 495 | goto done; |
f997f00b AS |
496 | } |
497 | ||
cdfadfc1 AS |
498 | if (ret == 0) |
499 | port->outvq_full = true; | |
500 | ||
501 | if (nonblock) | |
502 | goto done; | |
503 | ||
504 | /* | |
505 | * Wait till the host acknowledges it pushed out the data we | |
531295e6 AS |
506 | * sent. This is done for data from the hvc_console; the tty |
507 | * operations are performed with spinlocks held so we can't | |
508 | * sleep here. An alternative would be to copy the data to a | |
509 | * buffer and relax the spinning requirement. The downside is | |
510 | * we need to kmalloc a GFP_ATOMIC buffer each time the | |
511 | * console driver writes something out. | |
cdfadfc1 | 512 | */ |
505b0451 | 513 | while (!virtqueue_get_buf(out_vq, &len)) |
f997f00b | 514 | cpu_relax(); |
cdfadfc1 AS |
515 | done: |
516 | spin_unlock_irqrestore(&port->outvq_lock, flags); | |
517 | /* | |
518 | * We're expected to return the amount of data we wrote -- all | |
519 | * of it | |
520 | */ | |
9ff4cfab | 521 | return in_count; |
f997f00b AS |
522 | } |
523 | ||
203baab8 AS |
524 | /* |
525 | * Give out the data that's requested from the buffer that we have | |
526 | * queued up. | |
527 | */ | |
b766ceed AS |
528 | static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count, |
529 | bool to_user) | |
203baab8 AS |
530 | { |
531 | struct port_buffer *buf; | |
532 | unsigned long flags; | |
533 | ||
534 | if (!out_count || !port_has_data(port)) | |
535 | return 0; | |
536 | ||
537 | buf = port->inbuf; | |
b766ceed | 538 | out_count = min(out_count, buf->len - buf->offset); |
203baab8 | 539 | |
b766ceed AS |
540 | if (to_user) { |
541 | ssize_t ret; | |
542 | ||
543 | ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count); | |
544 | if (ret) | |
545 | return -EFAULT; | |
546 | } else { | |
547 | memcpy(out_buf, buf->buf + buf->offset, out_count); | |
548 | } | |
203baab8 | 549 | |
203baab8 AS |
550 | buf->offset += out_count; |
551 | ||
552 | if (buf->offset == buf->len) { | |
553 | /* | |
554 | * We're done using all the data in this buffer. | |
555 | * Re-queue so that the Host can send us more data. | |
556 | */ | |
557 | spin_lock_irqsave(&port->inbuf_lock, flags); | |
558 | port->inbuf = NULL; | |
559 | ||
560 | if (add_inbuf(port->in_vq, buf) < 0) | |
fb08bd27 | 561 | dev_warn(port->dev, "failed add_buf\n"); |
203baab8 AS |
562 | |
563 | spin_unlock_irqrestore(&port->inbuf_lock, flags); | |
564 | } | |
b766ceed | 565 | /* Return the number of bytes actually copied */ |
203baab8 | 566 | return out_count; |
e27b5198 AS |
567 | } |
568 | ||
2030fa49 | 569 | /* The condition that must be true for polling to end */ |
60caacd3 | 570 | static bool will_read_block(struct port *port) |
2030fa49 | 571 | { |
3709ea7a AS |
572 | if (!port->guest_connected) { |
573 | /* Port got hot-unplugged. Let's exit. */ | |
574 | return false; | |
575 | } | |
60caacd3 | 576 | return !port_has_data(port) && port->host_connected; |
2030fa49 AS |
577 | } |
578 | ||
cdfadfc1 AS |
579 | static bool will_write_block(struct port *port) |
580 | { | |
581 | bool ret; | |
582 | ||
60e5e0b8 AS |
583 | if (!port->guest_connected) { |
584 | /* Port got hot-unplugged. Let's exit. */ | |
585 | return false; | |
586 | } | |
cdfadfc1 AS |
587 | if (!port->host_connected) |
588 | return true; | |
589 | ||
590 | spin_lock_irq(&port->outvq_lock); | |
591 | /* | |
592 | * Check if the Host has consumed any buffers since we last | |
593 | * sent data (this is only applicable for nonblocking ports). | |
594 | */ | |
595 | reclaim_consumed_buffers(port); | |
596 | ret = port->outvq_full; | |
597 | spin_unlock_irq(&port->outvq_lock); | |
598 | ||
599 | return ret; | |
600 | } | |
601 | ||
2030fa49 AS |
602 | static ssize_t port_fops_read(struct file *filp, char __user *ubuf, |
603 | size_t count, loff_t *offp) | |
604 | { | |
605 | struct port *port; | |
606 | ssize_t ret; | |
607 | ||
608 | port = filp->private_data; | |
609 | ||
610 | if (!port_has_data(port)) { | |
611 | /* | |
612 | * If nothing's connected on the host just return 0 in | |
613 | * case of list_empty; this tells the userspace app | |
614 | * that there's no connection | |
615 | */ | |
616 | if (!port->host_connected) | |
617 | return 0; | |
618 | if (filp->f_flags & O_NONBLOCK) | |
619 | return -EAGAIN; | |
620 | ||
621 | ret = wait_event_interruptible(port->waitqueue, | |
60caacd3 | 622 | !will_read_block(port)); |
2030fa49 AS |
623 | if (ret < 0) |
624 | return ret; | |
625 | } | |
b3dddb9e AS |
626 | /* Port got hot-unplugged. */ |
627 | if (!port->guest_connected) | |
628 | return -ENODEV; | |
2030fa49 AS |
629 | /* |
630 | * We could've received a disconnection message while we were | |
631 | * waiting for more data. | |
632 | * | |
633 | * This check is not clubbed in the if() statement above as we | |
634 | * might receive some data as well as the host could get | |
635 | * disconnected after we got woken up from our wait. So we | |
636 | * really want to give off whatever data we have and only then | |
637 | * check for host_connected. | |
638 | */ | |
639 | if (!port_has_data(port) && !port->host_connected) | |
640 | return 0; | |
641 | ||
642 | return fill_readbuf(port, ubuf, count, true); | |
643 | } | |
644 | ||
645 | static ssize_t port_fops_write(struct file *filp, const char __user *ubuf, | |
646 | size_t count, loff_t *offp) | |
647 | { | |
648 | struct port *port; | |
649 | char *buf; | |
650 | ssize_t ret; | |
cdfadfc1 | 651 | bool nonblock; |
2030fa49 | 652 | |
65745422 AS |
653 | /* Userspace could be out to fool us */ |
654 | if (!count) | |
655 | return 0; | |
656 | ||
2030fa49 AS |
657 | port = filp->private_data; |
658 | ||
cdfadfc1 AS |
659 | nonblock = filp->f_flags & O_NONBLOCK; |
660 | ||
661 | if (will_write_block(port)) { | |
662 | if (nonblock) | |
663 | return -EAGAIN; | |
664 | ||
665 | ret = wait_event_interruptible(port->waitqueue, | |
666 | !will_write_block(port)); | |
667 | if (ret < 0) | |
668 | return ret; | |
669 | } | |
f4028119 AS |
670 | /* Port got hot-unplugged. */ |
671 | if (!port->guest_connected) | |
672 | return -ENODEV; | |
cdfadfc1 | 673 | |
2030fa49 AS |
674 | count = min((size_t)(32 * 1024), count); |
675 | ||
676 | buf = kmalloc(count, GFP_KERNEL); | |
677 | if (!buf) | |
678 | return -ENOMEM; | |
679 | ||
680 | ret = copy_from_user(buf, ubuf, count); | |
681 | if (ret) { | |
682 | ret = -EFAULT; | |
683 | goto free_buf; | |
684 | } | |
685 | ||
531295e6 AS |
686 | /* |
687 | * We now ask send_buf() to not spin for generic ports -- we | |
688 | * can re-use the same code path that non-blocking file | |
689 | * descriptors take for blocking file descriptors since the | |
690 | * wait is already done and we're certain the write will go | |
691 | * through to the host. | |
692 | */ | |
693 | nonblock = true; | |
cdfadfc1 AS |
694 | ret = send_buf(port, buf, count, nonblock); |
695 | ||
696 | if (nonblock && ret > 0) | |
697 | goto out; | |
698 | ||
2030fa49 AS |
699 | free_buf: |
700 | kfree(buf); | |
cdfadfc1 | 701 | out: |
2030fa49 AS |
702 | return ret; |
703 | } | |
704 | ||
705 | static unsigned int port_fops_poll(struct file *filp, poll_table *wait) | |
706 | { | |
707 | struct port *port; | |
708 | unsigned int ret; | |
709 | ||
710 | port = filp->private_data; | |
711 | poll_wait(filp, &port->waitqueue, wait); | |
712 | ||
8529a504 AS |
713 | if (!port->guest_connected) { |
714 | /* Port got unplugged */ | |
715 | return POLLHUP; | |
716 | } | |
2030fa49 | 717 | ret = 0; |
6df7aadc | 718 | if (!will_read_block(port)) |
2030fa49 | 719 | ret |= POLLIN | POLLRDNORM; |
cdfadfc1 | 720 | if (!will_write_block(port)) |
2030fa49 AS |
721 | ret |= POLLOUT; |
722 | if (!port->host_connected) | |
723 | ret |= POLLHUP; | |
724 | ||
725 | return ret; | |
726 | } | |
727 | ||
728 | static int port_fops_release(struct inode *inode, struct file *filp) | |
729 | { | |
730 | struct port *port; | |
731 | ||
732 | port = filp->private_data; | |
733 | ||
734 | /* Notify host of port being closed */ | |
735 | send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0); | |
736 | ||
88f251ac | 737 | spin_lock_irq(&port->inbuf_lock); |
3c7969cc AS |
738 | port->guest_connected = false; |
739 | ||
88f251ac AS |
740 | discard_port_data(port); |
741 | ||
742 | spin_unlock_irq(&port->inbuf_lock); | |
743 | ||
cdfadfc1 AS |
744 | spin_lock_irq(&port->outvq_lock); |
745 | reclaim_consumed_buffers(port); | |
746 | spin_unlock_irq(&port->outvq_lock); | |
747 | ||
2030fa49 AS |
748 | return 0; |
749 | } | |
750 | ||
751 | static int port_fops_open(struct inode *inode, struct file *filp) | |
752 | { | |
753 | struct cdev *cdev = inode->i_cdev; | |
754 | struct port *port; | |
8ad37e83 | 755 | int ret; |
2030fa49 | 756 | |
04950cdf | 757 | port = find_port_by_devt(cdev->dev); |
2030fa49 AS |
758 | filp->private_data = port; |
759 | ||
760 | /* | |
761 | * Don't allow opening of console port devices -- that's done | |
762 | * via /dev/hvc | |
763 | */ | |
8ad37e83 AS |
764 | if (is_console_port(port)) { |
765 | ret = -ENXIO; | |
766 | goto out; | |
767 | } | |
2030fa49 | 768 | |
3c7969cc AS |
769 | /* Allow only one process to open a particular port at a time */ |
770 | spin_lock_irq(&port->inbuf_lock); | |
771 | if (port->guest_connected) { | |
772 | spin_unlock_irq(&port->inbuf_lock); | |
8ad37e83 AS |
773 | ret = -EMFILE; |
774 | goto out; | |
3c7969cc AS |
775 | } |
776 | ||
777 | port->guest_connected = true; | |
778 | spin_unlock_irq(&port->inbuf_lock); | |
779 | ||
cdfadfc1 AS |
780 | spin_lock_irq(&port->outvq_lock); |
781 | /* | |
782 | * There might be a chance that we missed reclaiming a few | |
783 | * buffers in the window of the port getting previously closed | |
784 | * and opening now. | |
785 | */ | |
786 | reclaim_consumed_buffers(port); | |
787 | spin_unlock_irq(&port->outvq_lock); | |
788 | ||
2030fa49 AS |
789 | /* Notify host of port being opened */ |
790 | send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1); | |
791 | ||
792 | return 0; | |
8ad37e83 AS |
793 | out: |
794 | return ret; | |
2030fa49 AS |
795 | } |
796 | ||
797 | /* | |
798 | * The file operations that we support: programs in the guest can open | |
799 | * a console device, read from it, write to it, poll for data and | |
800 | * close it. The devices are at | |
801 | * /dev/vport<device number>p<port number> | |
802 | */ | |
803 | static const struct file_operations port_fops = { | |
804 | .owner = THIS_MODULE, | |
805 | .open = port_fops_open, | |
806 | .read = port_fops_read, | |
807 | .write = port_fops_write, | |
808 | .poll = port_fops_poll, | |
809 | .release = port_fops_release, | |
810 | }; | |
811 | ||
a23ea924 RR |
812 | /* |
813 | * The put_chars() callback is pretty straightforward. | |
31610434 | 814 | * |
a23ea924 RR |
815 | * We turn the characters into a scatter-gather list, add it to the |
816 | * output queue and then kick the Host. Then we sit here waiting for | |
817 | * it to finish: inefficient in theory, but in practice | |
818 | * implementations will do it immediately (lguest's Launcher does). | |
819 | */ | |
31610434 RR |
820 | static int put_chars(u32 vtermno, const char *buf, int count) |
821 | { | |
21206ede | 822 | struct port *port; |
38edf58d | 823 | |
162a689a FD |
824 | if (unlikely(early_put_chars)) |
825 | return early_put_chars(vtermno, buf, count); | |
826 | ||
38edf58d AS |
827 | port = find_port_by_vtermno(vtermno); |
828 | if (!port) | |
6dc69f97 | 829 | return -EPIPE; |
31610434 | 830 | |
cdfadfc1 | 831 | return send_buf(port, (void *)buf, count, false); |
31610434 RR |
832 | } |
833 | ||
a23ea924 RR |
834 | /* |
835 | * get_chars() is the callback from the hvc_console infrastructure | |
836 | * when an interrupt is received. | |
31610434 | 837 | * |
203baab8 AS |
838 | * We call out to fill_readbuf that gets us the required data from the |
839 | * buffers that are queued up. | |
a23ea924 | 840 | */ |
31610434 RR |
841 | static int get_chars(u32 vtermno, char *buf, int count) |
842 | { | |
21206ede RR |
843 | struct port *port; |
844 | ||
6dc69f97 AS |
845 | /* If we've not set up the port yet, we have no input to give. */ |
846 | if (unlikely(early_put_chars)) | |
847 | return 0; | |
848 | ||
38edf58d AS |
849 | port = find_port_by_vtermno(vtermno); |
850 | if (!port) | |
6dc69f97 | 851 | return -EPIPE; |
21206ede | 852 | |
31610434 | 853 | /* If we don't have an input queue yet, we can't get input. */ |
21206ede | 854 | BUG_ON(!port->in_vq); |
31610434 | 855 | |
b766ceed | 856 | return fill_readbuf(port, buf, count, false); |
31610434 | 857 | } |
31610434 | 858 | |
cb06e367 | 859 | static void resize_console(struct port *port) |
c2983458 | 860 | { |
cb06e367 | 861 | struct virtio_device *vdev; |
c2983458 | 862 | |
2de16a49 | 863 | /* The port could have been hot-unplugged */ |
9778829c | 864 | if (!port || !is_console_port(port)) |
2de16a49 AS |
865 | return; |
866 | ||
cb06e367 | 867 | vdev = port->portdev->vdev; |
9778829c AS |
868 | if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) |
869 | hvc_resize(port->cons.hvc, port->cons.ws); | |
c2983458 CB |
870 | } |
871 | ||
38edf58d | 872 | /* We set the configuration at this point, since we now have a tty */ |
91fcad19 CB |
873 | static int notifier_add_vio(struct hvc_struct *hp, int data) |
874 | { | |
38edf58d AS |
875 | struct port *port; |
876 | ||
877 | port = find_port_by_vtermno(hp->vtermno); | |
878 | if (!port) | |
879 | return -EINVAL; | |
880 | ||
91fcad19 | 881 | hp->irq_requested = 1; |
cb06e367 | 882 | resize_console(port); |
c2983458 | 883 | |
91fcad19 CB |
884 | return 0; |
885 | } | |
886 | ||
887 | static void notifier_del_vio(struct hvc_struct *hp, int data) | |
888 | { | |
889 | hp->irq_requested = 0; | |
890 | } | |
891 | ||
17634ba2 | 892 | /* The operations for console ports. */ |
1dff3996 | 893 | static const struct hv_ops hv_ops = { |
971f3390 RR |
894 | .get_chars = get_chars, |
895 | .put_chars = put_chars, | |
896 | .notifier_add = notifier_add_vio, | |
897 | .notifier_del = notifier_del_vio, | |
898 | .notifier_hangup = notifier_del_vio, | |
899 | }; | |
900 | ||
901 | /* | |
902 | * Console drivers are initialized very early so boot messages can go | |
903 | * out, so we do things slightly differently from the generic virtio | |
904 | * initialization of the net and block drivers. | |
905 | * | |
906 | * At this stage, the console is output-only. It's too early to set | |
907 | * up a virtqueue, so we let the drivers do some boutique early-output | |
908 | * thing. | |
909 | */ | |
910 | int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int)) | |
911 | { | |
912 | early_put_chars = put_chars; | |
913 | return hvc_instantiate(0, 0, &hv_ops); | |
914 | } | |
915 | ||
17634ba2 | 916 | int init_port_console(struct port *port) |
cfa6d379 AS |
917 | { |
918 | int ret; | |
919 | ||
920 | /* | |
921 | * The Host's telling us this port is a console port. Hook it | |
922 | * up with an hvc console. | |
923 | * | |
924 | * To set up and manage our virtual console, we call | |
925 | * hvc_alloc(). | |
926 | * | |
927 | * The first argument of hvc_alloc() is the virtual console | |
928 | * number. The second argument is the parameter for the | |
929 | * notification mechanism (like irq number). We currently | |
930 | * leave this as zero, virtqueues have implicit notifications. | |
931 | * | |
932 | * The third argument is a "struct hv_ops" containing the | |
933 | * put_chars() get_chars(), notifier_add() and notifier_del() | |
934 | * pointers. The final argument is the output buffer size: we | |
935 | * can do any size, so we put PAGE_SIZE here. | |
936 | */ | |
937 | port->cons.vtermno = pdrvdata.next_vtermno; | |
938 | ||
939 | port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE); | |
940 | if (IS_ERR(port->cons.hvc)) { | |
941 | ret = PTR_ERR(port->cons.hvc); | |
298add72 AS |
942 | dev_err(port->dev, |
943 | "error %d allocating hvc for port\n", ret); | |
cfa6d379 AS |
944 | port->cons.hvc = NULL; |
945 | return ret; | |
946 | } | |
947 | spin_lock_irq(&pdrvdata_lock); | |
948 | pdrvdata.next_vtermno++; | |
949 | list_add_tail(&port->cons.list, &pdrvdata.consoles); | |
950 | spin_unlock_irq(&pdrvdata_lock); | |
3c7969cc | 951 | port->guest_connected = true; |
cfa6d379 | 952 | |
1d05160b AS |
953 | /* |
954 | * Start using the new console output if this is the first | |
955 | * console to come up. | |
956 | */ | |
957 | if (early_put_chars) | |
958 | early_put_chars = NULL; | |
959 | ||
2030fa49 AS |
960 | /* Notify host of port being opened */ |
961 | send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); | |
962 | ||
cfa6d379 AS |
963 | return 0; |
964 | } | |
965 | ||
431edb8a AS |
966 | static ssize_t show_port_name(struct device *dev, |
967 | struct device_attribute *attr, char *buffer) | |
968 | { | |
969 | struct port *port; | |
970 | ||
971 | port = dev_get_drvdata(dev); | |
972 | ||
973 | return sprintf(buffer, "%s\n", port->name); | |
974 | } | |
975 | ||
976 | static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL); | |
977 | ||
978 | static struct attribute *port_sysfs_entries[] = { | |
979 | &dev_attr_name.attr, | |
980 | NULL | |
981 | }; | |
982 | ||
983 | static struct attribute_group port_attribute_group = { | |
984 | .name = NULL, /* put in device directory */ | |
985 | .attrs = port_sysfs_entries, | |
986 | }; | |
987 | ||
d99393ef AS |
988 | static int debugfs_open(struct inode *inode, struct file *filp) |
989 | { | |
990 | filp->private_data = inode->i_private; | |
991 | return 0; | |
992 | } | |
993 | ||
994 | static ssize_t debugfs_read(struct file *filp, char __user *ubuf, | |
995 | size_t count, loff_t *offp) | |
996 | { | |
997 | struct port *port; | |
998 | char *buf; | |
999 | ssize_t ret, out_offset, out_count; | |
1000 | ||
1001 | out_count = 1024; | |
1002 | buf = kmalloc(out_count, GFP_KERNEL); | |
1003 | if (!buf) | |
1004 | return -ENOMEM; | |
1005 | ||
1006 | port = filp->private_data; | |
1007 | out_offset = 0; | |
1008 | out_offset += snprintf(buf + out_offset, out_count, | |
1009 | "name: %s\n", port->name ? port->name : ""); | |
1010 | out_offset += snprintf(buf + out_offset, out_count - out_offset, | |
1011 | "guest_connected: %d\n", port->guest_connected); | |
1012 | out_offset += snprintf(buf + out_offset, out_count - out_offset, | |
1013 | "host_connected: %d\n", port->host_connected); | |
cdfadfc1 AS |
1014 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
1015 | "outvq_full: %d\n", port->outvq_full); | |
d99393ef AS |
1016 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
1017 | "is_console: %s\n", | |
1018 | is_console_port(port) ? "yes" : "no"); | |
1019 | out_offset += snprintf(buf + out_offset, out_count - out_offset, | |
1020 | "console_vtermno: %u\n", port->cons.vtermno); | |
1021 | ||
1022 | ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset); | |
1023 | kfree(buf); | |
1024 | return ret; | |
1025 | } | |
1026 | ||
1027 | static const struct file_operations port_debugfs_ops = { | |
1028 | .owner = THIS_MODULE, | |
1029 | .open = debugfs_open, | |
1030 | .read = debugfs_read, | |
1031 | }; | |
1032 | ||
9778829c AS |
1033 | static void set_console_size(struct port *port, u16 rows, u16 cols) |
1034 | { | |
1035 | if (!port || !is_console_port(port)) | |
1036 | return; | |
1037 | ||
1038 | port->cons.ws.ws_row = rows; | |
1039 | port->cons.ws.ws_col = cols; | |
1040 | } | |
1041 | ||
c446f8fc AS |
1042 | static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock) |
1043 | { | |
1044 | struct port_buffer *buf; | |
1045 | unsigned int nr_added_bufs; | |
1046 | int ret; | |
1047 | ||
1048 | nr_added_bufs = 0; | |
1049 | do { | |
1050 | buf = alloc_buf(PAGE_SIZE); | |
1051 | if (!buf) | |
1052 | break; | |
1053 | ||
1054 | spin_lock_irq(lock); | |
1055 | ret = add_inbuf(vq, buf); | |
1056 | if (ret < 0) { | |
1057 | spin_unlock_irq(lock); | |
1058 | free_buf(buf); | |
1059 | break; | |
1060 | } | |
1061 | nr_added_bufs++; | |
1062 | spin_unlock_irq(lock); | |
1063 | } while (ret > 0); | |
1064 | ||
1065 | return nr_added_bufs; | |
1066 | } | |
1067 | ||
1068 | static int add_port(struct ports_device *portdev, u32 id) | |
1069 | { | |
1070 | char debugfs_name[16]; | |
1071 | struct port *port; | |
1072 | struct port_buffer *buf; | |
1073 | dev_t devt; | |
1074 | unsigned int nr_added_bufs; | |
1075 | int err; | |
1076 | ||
1077 | port = kmalloc(sizeof(*port), GFP_KERNEL); | |
1078 | if (!port) { | |
1079 | err = -ENOMEM; | |
1080 | goto fail; | |
1081 | } | |
1082 | ||
1083 | port->portdev = portdev; | |
1084 | port->id = id; | |
1085 | ||
1086 | port->name = NULL; | |
1087 | port->inbuf = NULL; | |
1088 | port->cons.hvc = NULL; | |
1089 | ||
9778829c AS |
1090 | port->cons.ws.ws_row = port->cons.ws.ws_col = 0; |
1091 | ||
c446f8fc AS |
1092 | port->host_connected = port->guest_connected = false; |
1093 | ||
cdfadfc1 AS |
1094 | port->outvq_full = false; |
1095 | ||
c446f8fc AS |
1096 | port->in_vq = portdev->in_vqs[port->id]; |
1097 | port->out_vq = portdev->out_vqs[port->id]; | |
1098 | ||
1099 | cdev_init(&port->cdev, &port_fops); | |
1100 | ||
1101 | devt = MKDEV(portdev->chr_major, id); | |
1102 | err = cdev_add(&port->cdev, devt, 1); | |
1103 | if (err < 0) { | |
1104 | dev_err(&port->portdev->vdev->dev, | |
1105 | "Error %d adding cdev for port %u\n", err, id); | |
1106 | goto free_port; | |
1107 | } | |
1108 | port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev, | |
1109 | devt, port, "vport%up%u", | |
1110 | port->portdev->drv_index, id); | |
1111 | if (IS_ERR(port->dev)) { | |
1112 | err = PTR_ERR(port->dev); | |
1113 | dev_err(&port->portdev->vdev->dev, | |
1114 | "Error %d creating device for port %u\n", | |
1115 | err, id); | |
1116 | goto free_cdev; | |
1117 | } | |
1118 | ||
1119 | spin_lock_init(&port->inbuf_lock); | |
cdfadfc1 | 1120 | spin_lock_init(&port->outvq_lock); |
c446f8fc AS |
1121 | init_waitqueue_head(&port->waitqueue); |
1122 | ||
1123 | /* Fill the in_vq with buffers so the host can send us data. */ | |
1124 | nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock); | |
1125 | if (!nr_added_bufs) { | |
1126 | dev_err(port->dev, "Error allocating inbufs\n"); | |
1127 | err = -ENOMEM; | |
1128 | goto free_device; | |
1129 | } | |
1130 | ||
1131 | /* | |
1132 | * If we're not using multiport support, this has to be a console port | |
1133 | */ | |
1134 | if (!use_multiport(port->portdev)) { | |
1135 | err = init_port_console(port); | |
1136 | if (err) | |
1137 | goto free_inbufs; | |
1138 | } | |
1139 | ||
1140 | spin_lock_irq(&portdev->ports_lock); | |
1141 | list_add_tail(&port->list, &port->portdev->ports); | |
1142 | spin_unlock_irq(&portdev->ports_lock); | |
1143 | ||
1144 | /* | |
1145 | * Tell the Host we're set so that it can send us various | |
1146 | * configuration parameters for this port (eg, port name, | |
1147 | * caching, whether this is a console port, etc.) | |
1148 | */ | |
1149 | send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); | |
1150 | ||
1151 | if (pdrvdata.debugfs_dir) { | |
1152 | /* | |
1153 | * Finally, create the debugfs file that we can use to | |
1154 | * inspect a port's state at any time | |
1155 | */ | |
1156 | sprintf(debugfs_name, "vport%up%u", | |
1157 | port->portdev->drv_index, id); | |
1158 | port->debugfs_file = debugfs_create_file(debugfs_name, 0444, | |
1159 | pdrvdata.debugfs_dir, | |
1160 | port, | |
1161 | &port_debugfs_ops); | |
1162 | } | |
1163 | return 0; | |
1164 | ||
1165 | free_inbufs: | |
1166 | while ((buf = virtqueue_detach_unused_buf(port->in_vq))) | |
1167 | free_buf(buf); | |
1168 | free_device: | |
1169 | device_destroy(pdrvdata.class, port->dev->devt); | |
1170 | free_cdev: | |
1171 | cdev_del(&port->cdev); | |
1172 | free_port: | |
1173 | kfree(port); | |
1174 | fail: | |
1175 | /* The host might want to notify management sw about port add failure */ | |
0643e4c6 | 1176 | __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0); |
c446f8fc AS |
1177 | return err; |
1178 | } | |
1179 | ||
1f7aa42d | 1180 | /* Remove all port-specific data. */ |
7a285317 | 1181 | static void remove_port(struct port *port) |
1f7aa42d | 1182 | { |
a9cdd485 AS |
1183 | struct port_buffer *buf; |
1184 | ||
0047634d AS |
1185 | if (port->guest_connected) { |
1186 | port->guest_connected = false; | |
1187 | port->host_connected = false; | |
1188 | wake_up_interruptible(&port->waitqueue); | |
1189 | send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0); | |
1190 | } | |
1191 | ||
1f7aa42d AS |
1192 | spin_lock_irq(&port->portdev->ports_lock); |
1193 | list_del(&port->list); | |
1194 | spin_unlock_irq(&port->portdev->ports_lock); | |
1195 | ||
1196 | if (is_console_port(port)) { | |
1197 | spin_lock_irq(&pdrvdata_lock); | |
1198 | list_del(&port->cons.list); | |
1199 | spin_unlock_irq(&pdrvdata_lock); | |
69eb9a9f AS |
1200 | #if 0 |
1201 | /* | |
1202 | * hvc_remove() not called as removing one hvc port | |
1203 | * results in other hvc ports getting frozen. | |
1204 | * | |
1205 | * Once this is resolved in hvc, this functionality | |
1206 | * will be enabled. Till that is done, the -EPIPE | |
1207 | * return from get_chars() above will help | |
1208 | * hvc_console.c to clean up on ports we remove here. | |
1209 | */ | |
1f7aa42d | 1210 | hvc_remove(port->cons.hvc); |
69eb9a9f | 1211 | #endif |
1f7aa42d | 1212 | } |
1f7aa42d AS |
1213 | sysfs_remove_group(&port->dev->kobj, &port_attribute_group); |
1214 | device_destroy(pdrvdata.class, port->dev->devt); | |
1215 | cdev_del(&port->cdev); | |
1216 | ||
a9cdd485 | 1217 | /* Remove unused data this port might have received. */ |
1f7aa42d | 1218 | discard_port_data(port); |
a9cdd485 | 1219 | |
cdfadfc1 AS |
1220 | reclaim_consumed_buffers(port); |
1221 | ||
a9cdd485 | 1222 | /* Remove buffers we queued up for the Host to send us data in. */ |
505b0451 | 1223 | while ((buf = virtqueue_detach_unused_buf(port->in_vq))) |
a9cdd485 AS |
1224 | free_buf(buf); |
1225 | ||
1f7aa42d AS |
1226 | kfree(port->name); |
1227 | ||
d99393ef AS |
1228 | debugfs_remove(port->debugfs_file); |
1229 | ||
1f7aa42d | 1230 | kfree(port); |
1f7aa42d AS |
1231 | } |
1232 | ||
17634ba2 AS |
1233 | /* Any private messages that the Host and Guest want to share */ |
1234 | static void handle_control_message(struct ports_device *portdev, | |
1235 | struct port_buffer *buf) | |
1236 | { | |
1237 | struct virtio_console_control *cpkt; | |
1238 | struct port *port; | |
431edb8a AS |
1239 | size_t name_size; |
1240 | int err; | |
17634ba2 AS |
1241 | |
1242 | cpkt = (struct virtio_console_control *)(buf->buf + buf->offset); | |
1243 | ||
1244 | port = find_port_by_id(portdev, cpkt->id); | |
f909f850 | 1245 | if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) { |
17634ba2 AS |
1246 | /* No valid header at start of buffer. Drop it. */ |
1247 | dev_dbg(&portdev->vdev->dev, | |
1248 | "Invalid index %u in control packet\n", cpkt->id); | |
1249 | return; | |
1250 | } | |
1251 | ||
1252 | switch (cpkt->event) { | |
f909f850 AS |
1253 | case VIRTIO_CONSOLE_PORT_ADD: |
1254 | if (port) { | |
1d05160b AS |
1255 | dev_dbg(&portdev->vdev->dev, |
1256 | "Port %u already added\n", port->id); | |
f909f850 AS |
1257 | send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); |
1258 | break; | |
1259 | } | |
1260 | if (cpkt->id >= portdev->config.max_nr_ports) { | |
1261 | dev_warn(&portdev->vdev->dev, | |
1262 | "Request for adding port with out-of-bound id %u, max. supported id: %u\n", | |
1263 | cpkt->id, portdev->config.max_nr_ports - 1); | |
1264 | break; | |
1265 | } | |
1266 | add_port(portdev, cpkt->id); | |
1267 | break; | |
1268 | case VIRTIO_CONSOLE_PORT_REMOVE: | |
1269 | remove_port(port); | |
1270 | break; | |
17634ba2 AS |
1271 | case VIRTIO_CONSOLE_CONSOLE_PORT: |
1272 | if (!cpkt->value) | |
1273 | break; | |
1274 | if (is_console_port(port)) | |
1275 | break; | |
1276 | ||
1277 | init_port_console(port); | |
1278 | /* | |
1279 | * Could remove the port here in case init fails - but | |
1280 | * have to notify the host first. | |
1281 | */ | |
1282 | break; | |
8345adbf AS |
1283 | case VIRTIO_CONSOLE_RESIZE: { |
1284 | struct { | |
1285 | __u16 rows; | |
1286 | __u16 cols; | |
1287 | } size; | |
1288 | ||
17634ba2 AS |
1289 | if (!is_console_port(port)) |
1290 | break; | |
8345adbf AS |
1291 | |
1292 | memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt), | |
1293 | sizeof(size)); | |
1294 | set_console_size(port, size.rows, size.cols); | |
1295 | ||
17634ba2 AS |
1296 | port->cons.hvc->irq_requested = 1; |
1297 | resize_console(port); | |
1298 | break; | |
8345adbf | 1299 | } |
2030fa49 AS |
1300 | case VIRTIO_CONSOLE_PORT_OPEN: |
1301 | port->host_connected = cpkt->value; | |
1302 | wake_up_interruptible(&port->waitqueue); | |
cdfadfc1 AS |
1303 | /* |
1304 | * If the host port got closed and the host had any | |
1305 | * unconsumed buffers, we'll be able to reclaim them | |
1306 | * now. | |
1307 | */ | |
1308 | spin_lock_irq(&port->outvq_lock); | |
1309 | reclaim_consumed_buffers(port); | |
1310 | spin_unlock_irq(&port->outvq_lock); | |
2030fa49 | 1311 | break; |
431edb8a AS |
1312 | case VIRTIO_CONSOLE_PORT_NAME: |
1313 | /* | |
1314 | * Skip the size of the header and the cpkt to get the size | |
1315 | * of the name that was sent | |
1316 | */ | |
1317 | name_size = buf->len - buf->offset - sizeof(*cpkt) + 1; | |
1318 | ||
1319 | port->name = kmalloc(name_size, GFP_KERNEL); | |
1320 | if (!port->name) { | |
1321 | dev_err(port->dev, | |
1322 | "Not enough space to store port name\n"); | |
1323 | break; | |
1324 | } | |
1325 | strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt), | |
1326 | name_size - 1); | |
1327 | port->name[name_size - 1] = 0; | |
1328 | ||
1329 | /* | |
1330 | * Since we only have one sysfs attribute, 'name', | |
1331 | * create it only if we have a name for the port. | |
1332 | */ | |
1333 | err = sysfs_create_group(&port->dev->kobj, | |
1334 | &port_attribute_group); | |
ec64213c | 1335 | if (err) { |
431edb8a AS |
1336 | dev_err(port->dev, |
1337 | "Error %d creating sysfs device attributes\n", | |
1338 | err); | |
ec64213c AS |
1339 | } else { |
1340 | /* | |
1341 | * Generate a udev event so that appropriate | |
1342 | * symlinks can be created based on udev | |
1343 | * rules. | |
1344 | */ | |
1345 | kobject_uevent(&port->dev->kobj, KOBJ_CHANGE); | |
1346 | } | |
431edb8a | 1347 | break; |
17634ba2 AS |
1348 | } |
1349 | } | |
1350 | ||
1351 | static void control_work_handler(struct work_struct *work) | |
1352 | { | |
1353 | struct ports_device *portdev; | |
1354 | struct virtqueue *vq; | |
1355 | struct port_buffer *buf; | |
1356 | unsigned int len; | |
1357 | ||
1358 | portdev = container_of(work, struct ports_device, control_work); | |
1359 | vq = portdev->c_ivq; | |
1360 | ||
1361 | spin_lock(&portdev->cvq_lock); | |
505b0451 | 1362 | while ((buf = virtqueue_get_buf(vq, &len))) { |
17634ba2 AS |
1363 | spin_unlock(&portdev->cvq_lock); |
1364 | ||
1365 | buf->len = len; | |
1366 | buf->offset = 0; | |
1367 | ||
1368 | handle_control_message(portdev, buf); | |
1369 | ||
1370 | spin_lock(&portdev->cvq_lock); | |
1371 | if (add_inbuf(portdev->c_ivq, buf) < 0) { | |
1372 | dev_warn(&portdev->vdev->dev, | |
1373 | "Error adding buffer to queue\n"); | |
1374 | free_buf(buf); | |
1375 | } | |
1376 | } | |
1377 | spin_unlock(&portdev->cvq_lock); | |
1378 | } | |
1379 | ||
1380 | static void in_intr(struct virtqueue *vq) | |
1381 | { | |
1382 | struct port *port; | |
1383 | unsigned long flags; | |
1384 | ||
1385 | port = find_port_by_vq(vq->vdev->priv, vq); | |
1386 | if (!port) | |
1387 | return; | |
1388 | ||
1389 | spin_lock_irqsave(&port->inbuf_lock, flags); | |
d6933561 AS |
1390 | if (!port->inbuf) |
1391 | port->inbuf = get_inbuf(port); | |
17634ba2 | 1392 | |
88f251ac AS |
1393 | /* |
1394 | * Don't queue up data when port is closed. This condition | |
1395 | * can be reached when a console port is not yet connected (no | |
1396 | * tty is spawned) and the host sends out data to console | |
1397 | * ports. For generic serial ports, the host won't | |
1398 | * (shouldn't) send data till the guest is connected. | |
1399 | */ | |
1400 | if (!port->guest_connected) | |
1401 | discard_port_data(port); | |
1402 | ||
17634ba2 AS |
1403 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
1404 | ||
2030fa49 AS |
1405 | wake_up_interruptible(&port->waitqueue); |
1406 | ||
17634ba2 AS |
1407 | if (is_console_port(port) && hvc_poll(port->cons.hvc)) |
1408 | hvc_kick(); | |
1409 | } | |
1410 | ||
1411 | static void control_intr(struct virtqueue *vq) | |
1412 | { | |
1413 | struct ports_device *portdev; | |
1414 | ||
1415 | portdev = vq->vdev->priv; | |
1416 | schedule_work(&portdev->control_work); | |
1417 | } | |
1418 | ||
7f5d810d AS |
1419 | static void config_intr(struct virtio_device *vdev) |
1420 | { | |
1421 | struct ports_device *portdev; | |
1422 | ||
1423 | portdev = vdev->priv; | |
99f905f8 | 1424 | |
4038f5b7 | 1425 | if (!use_multiport(portdev)) { |
9778829c AS |
1426 | struct port *port; |
1427 | u16 rows, cols; | |
1428 | ||
1429 | vdev->config->get(vdev, | |
1430 | offsetof(struct virtio_console_config, cols), | |
1431 | &cols, sizeof(u16)); | |
1432 | vdev->config->get(vdev, | |
1433 | offsetof(struct virtio_console_config, rows), | |
1434 | &rows, sizeof(u16)); | |
1435 | ||
1436 | port = find_port_by_id(portdev, 0); | |
1437 | set_console_size(port, rows, cols); | |
1438 | ||
4038f5b7 AS |
1439 | /* |
1440 | * We'll use this way of resizing only for legacy | |
1441 | * support. For newer userspace | |
1442 | * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages | |
1443 | * to indicate console size changes so that it can be | |
1444 | * done per-port. | |
1445 | */ | |
9778829c | 1446 | resize_console(port); |
4038f5b7 | 1447 | } |
7f5d810d AS |
1448 | } |
1449 | ||
2658a79a AS |
1450 | static int init_vqs(struct ports_device *portdev) |
1451 | { | |
1452 | vq_callback_t **io_callbacks; | |
1453 | char **io_names; | |
1454 | struct virtqueue **vqs; | |
17634ba2 | 1455 | u32 i, j, nr_ports, nr_queues; |
2658a79a AS |
1456 | int err; |
1457 | ||
17634ba2 AS |
1458 | nr_ports = portdev->config.max_nr_ports; |
1459 | nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; | |
2658a79a AS |
1460 | |
1461 | vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL); | |
1462 | if (!vqs) { | |
1463 | err = -ENOMEM; | |
1464 | goto fail; | |
1465 | } | |
1466 | io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL); | |
1467 | if (!io_callbacks) { | |
1468 | err = -ENOMEM; | |
1469 | goto free_vqs; | |
1470 | } | |
1471 | io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL); | |
1472 | if (!io_names) { | |
1473 | err = -ENOMEM; | |
1474 | goto free_callbacks; | |
1475 | } | |
1476 | portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), | |
1477 | GFP_KERNEL); | |
1478 | if (!portdev->in_vqs) { | |
1479 | err = -ENOMEM; | |
1480 | goto free_names; | |
1481 | } | |
1482 | portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), | |
1483 | GFP_KERNEL); | |
1484 | if (!portdev->out_vqs) { | |
1485 | err = -ENOMEM; | |
1486 | goto free_invqs; | |
1487 | } | |
1488 | ||
17634ba2 AS |
1489 | /* |
1490 | * For backward compat (newer host but older guest), the host | |
1491 | * spawns a console port first and also inits the vqs for port | |
1492 | * 0 before others. | |
1493 | */ | |
1494 | j = 0; | |
1495 | io_callbacks[j] = in_intr; | |
1496 | io_callbacks[j + 1] = NULL; | |
1497 | io_names[j] = "input"; | |
1498 | io_names[j + 1] = "output"; | |
1499 | j += 2; | |
1500 | ||
1501 | if (use_multiport(portdev)) { | |
1502 | io_callbacks[j] = control_intr; | |
1503 | io_callbacks[j + 1] = NULL; | |
1504 | io_names[j] = "control-i"; | |
1505 | io_names[j + 1] = "control-o"; | |
1506 | ||
1507 | for (i = 1; i < nr_ports; i++) { | |
1508 | j += 2; | |
1509 | io_callbacks[j] = in_intr; | |
1510 | io_callbacks[j + 1] = NULL; | |
1511 | io_names[j] = "input"; | |
1512 | io_names[j + 1] = "output"; | |
1513 | } | |
1514 | } | |
2658a79a AS |
1515 | /* Find the queues. */ |
1516 | err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs, | |
1517 | io_callbacks, | |
1518 | (const char **)io_names); | |
1519 | if (err) | |
1520 | goto free_outvqs; | |
1521 | ||
17634ba2 | 1522 | j = 0; |
2658a79a AS |
1523 | portdev->in_vqs[0] = vqs[0]; |
1524 | portdev->out_vqs[0] = vqs[1]; | |
17634ba2 AS |
1525 | j += 2; |
1526 | if (use_multiport(portdev)) { | |
1527 | portdev->c_ivq = vqs[j]; | |
1528 | portdev->c_ovq = vqs[j + 1]; | |
1529 | ||
1530 | for (i = 1; i < nr_ports; i++) { | |
1531 | j += 2; | |
1532 | portdev->in_vqs[i] = vqs[j]; | |
1533 | portdev->out_vqs[i] = vqs[j + 1]; | |
1534 | } | |
1535 | } | |
2658a79a AS |
1536 | kfree(io_callbacks); |
1537 | kfree(io_names); | |
1538 | kfree(vqs); | |
1539 | ||
1540 | return 0; | |
1541 | ||
1542 | free_names: | |
1543 | kfree(io_names); | |
1544 | free_callbacks: | |
1545 | kfree(io_callbacks); | |
1546 | free_outvqs: | |
1547 | kfree(portdev->out_vqs); | |
1548 | free_invqs: | |
1549 | kfree(portdev->in_vqs); | |
1550 | free_vqs: | |
1551 | kfree(vqs); | |
1552 | fail: | |
1553 | return err; | |
1554 | } | |
1555 | ||
fb08bd27 AS |
1556 | static const struct file_operations portdev_fops = { |
1557 | .owner = THIS_MODULE, | |
1558 | }; | |
1559 | ||
1c85bf35 AS |
1560 | /* |
1561 | * Once we're further in boot, we get probed like any other virtio | |
1562 | * device. | |
17634ba2 AS |
1563 | * |
1564 | * If the host also supports multiple console ports, we check the | |
1565 | * config space to see how many ports the host has spawned. We | |
1566 | * initialize each port found. | |
1c85bf35 AS |
1567 | */ |
1568 | static int __devinit virtcons_probe(struct virtio_device *vdev) | |
1569 | { | |
1c85bf35 AS |
1570 | struct ports_device *portdev; |
1571 | int err; | |
17634ba2 | 1572 | bool multiport; |
1c85bf35 AS |
1573 | |
1574 | portdev = kmalloc(sizeof(*portdev), GFP_KERNEL); | |
1575 | if (!portdev) { | |
1576 | err = -ENOMEM; | |
1577 | goto fail; | |
1578 | } | |
1579 | ||
1580 | /* Attach this portdev to this virtio_device, and vice-versa. */ | |
1581 | portdev->vdev = vdev; | |
1582 | vdev->priv = portdev; | |
1583 | ||
fb08bd27 AS |
1584 | spin_lock_irq(&pdrvdata_lock); |
1585 | portdev->drv_index = pdrvdata.index++; | |
1586 | spin_unlock_irq(&pdrvdata_lock); | |
1587 | ||
1588 | portdev->chr_major = register_chrdev(0, "virtio-portsdev", | |
1589 | &portdev_fops); | |
1590 | if (portdev->chr_major < 0) { | |
1591 | dev_err(&vdev->dev, | |
1592 | "Error %d registering chrdev for device %u\n", | |
1593 | portdev->chr_major, portdev->drv_index); | |
1594 | err = portdev->chr_major; | |
1595 | goto free; | |
1596 | } | |
1597 | ||
17634ba2 | 1598 | multiport = false; |
17634ba2 AS |
1599 | portdev->config.max_nr_ports = 1; |
1600 | if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) { | |
1601 | multiport = true; | |
1602 | vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT; | |
1603 | ||
b99fa815 AS |
1604 | vdev->config->get(vdev, offsetof(struct virtio_console_config, |
1605 | max_nr_ports), | |
17634ba2 AS |
1606 | &portdev->config.max_nr_ports, |
1607 | sizeof(portdev->config.max_nr_ports)); | |
17634ba2 AS |
1608 | } |
1609 | ||
1610 | /* Let the Host know we support multiple ports.*/ | |
1611 | vdev->config->finalize_features(vdev); | |
1612 | ||
2658a79a AS |
1613 | err = init_vqs(portdev); |
1614 | if (err < 0) { | |
1615 | dev_err(&vdev->dev, "Error %d initializing vqs\n", err); | |
fb08bd27 | 1616 | goto free_chrdev; |
2658a79a | 1617 | } |
1c85bf35 | 1618 | |
17634ba2 AS |
1619 | spin_lock_init(&portdev->ports_lock); |
1620 | INIT_LIST_HEAD(&portdev->ports); | |
1621 | ||
1622 | if (multiport) { | |
335a64a5 AS |
1623 | unsigned int nr_added_bufs; |
1624 | ||
17634ba2 AS |
1625 | spin_lock_init(&portdev->cvq_lock); |
1626 | INIT_WORK(&portdev->control_work, &control_work_handler); | |
1627 | ||
335a64a5 AS |
1628 | nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock); |
1629 | if (!nr_added_bufs) { | |
22a29eac AS |
1630 | dev_err(&vdev->dev, |
1631 | "Error allocating buffers for control queue\n"); | |
1632 | err = -ENOMEM; | |
1633 | goto free_vqs; | |
1634 | } | |
1d05160b AS |
1635 | } else { |
1636 | /* | |
1637 | * For backward compatibility: Create a console port | |
1638 | * if we're running on older host. | |
1639 | */ | |
1640 | add_port(portdev, 0); | |
17634ba2 AS |
1641 | } |
1642 | ||
6bdf2afd AS |
1643 | spin_lock_irq(&pdrvdata_lock); |
1644 | list_add_tail(&portdev->list, &pdrvdata.portdevs); | |
1645 | spin_unlock_irq(&pdrvdata_lock); | |
1646 | ||
f909f850 AS |
1647 | __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID, |
1648 | VIRTIO_CONSOLE_DEVICE_READY, 1); | |
31610434 RR |
1649 | return 0; |
1650 | ||
22a29eac | 1651 | free_vqs: |
0643e4c6 JL |
1652 | /* The host might want to notify mgmt sw about device add failure */ |
1653 | __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID, | |
1654 | VIRTIO_CONSOLE_DEVICE_READY, 0); | |
22a29eac AS |
1655 | vdev->config->del_vqs(vdev); |
1656 | kfree(portdev->in_vqs); | |
1657 | kfree(portdev->out_vqs); | |
fb08bd27 AS |
1658 | free_chrdev: |
1659 | unregister_chrdev(portdev->chr_major, "virtio-portsdev"); | |
31610434 | 1660 | free: |
1c85bf35 | 1661 | kfree(portdev); |
31610434 RR |
1662 | fail: |
1663 | return err; | |
1664 | } | |
1665 | ||
7177876f AS |
1666 | static void virtcons_remove(struct virtio_device *vdev) |
1667 | { | |
1668 | struct ports_device *portdev; | |
1669 | struct port *port, *port2; | |
7177876f AS |
1670 | |
1671 | portdev = vdev->priv; | |
1672 | ||
6bdf2afd AS |
1673 | spin_lock_irq(&pdrvdata_lock); |
1674 | list_del(&portdev->list); | |
1675 | spin_unlock_irq(&pdrvdata_lock); | |
1676 | ||
02238959 AS |
1677 | /* Disable interrupts for vqs */ |
1678 | vdev->config->reset(vdev); | |
1679 | /* Finish up work that's lined up */ | |
7177876f | 1680 | cancel_work_sync(&portdev->control_work); |
7177876f AS |
1681 | |
1682 | list_for_each_entry_safe(port, port2, &portdev->ports, list) | |
1683 | remove_port(port); | |
1684 | ||
1685 | unregister_chrdev(portdev->chr_major, "virtio-portsdev"); | |
1686 | ||
96eb872b AS |
1687 | if (use_multiport(portdev)) { |
1688 | struct port_buffer *buf; | |
1689 | unsigned int len; | |
7177876f | 1690 | |
96eb872b AS |
1691 | while ((buf = virtqueue_get_buf(portdev->c_ivq, &len))) |
1692 | free_buf(buf); | |
1693 | ||
1694 | while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq))) | |
1695 | free_buf(buf); | |
1696 | } | |
7177876f AS |
1697 | |
1698 | vdev->config->del_vqs(vdev); | |
1699 | kfree(portdev->in_vqs); | |
1700 | kfree(portdev->out_vqs); | |
1701 | ||
1702 | kfree(portdev); | |
1703 | } | |
1704 | ||
31610434 RR |
1705 | static struct virtio_device_id id_table[] = { |
1706 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, | |
1707 | { 0 }, | |
1708 | }; | |
1709 | ||
c2983458 CB |
1710 | static unsigned int features[] = { |
1711 | VIRTIO_CONSOLE_F_SIZE, | |
b99fa815 | 1712 | VIRTIO_CONSOLE_F_MULTIPORT, |
c2983458 CB |
1713 | }; |
1714 | ||
31610434 | 1715 | static struct virtio_driver virtio_console = { |
c2983458 CB |
1716 | .feature_table = features, |
1717 | .feature_table_size = ARRAY_SIZE(features), | |
31610434 RR |
1718 | .driver.name = KBUILD_MODNAME, |
1719 | .driver.owner = THIS_MODULE, | |
1720 | .id_table = id_table, | |
1721 | .probe = virtcons_probe, | |
7177876f | 1722 | .remove = virtcons_remove, |
7f5d810d | 1723 | .config_changed = config_intr, |
31610434 RR |
1724 | }; |
1725 | ||
1726 | static int __init init(void) | |
1727 | { | |
fb08bd27 AS |
1728 | int err; |
1729 | ||
1730 | pdrvdata.class = class_create(THIS_MODULE, "virtio-ports"); | |
1731 | if (IS_ERR(pdrvdata.class)) { | |
1732 | err = PTR_ERR(pdrvdata.class); | |
1733 | pr_err("Error %d creating virtio-ports class\n", err); | |
1734 | return err; | |
1735 | } | |
d99393ef AS |
1736 | |
1737 | pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL); | |
1738 | if (!pdrvdata.debugfs_dir) { | |
1739 | pr_warning("Error %ld creating debugfs dir for virtio-ports\n", | |
1740 | PTR_ERR(pdrvdata.debugfs_dir)); | |
1741 | } | |
38edf58d | 1742 | INIT_LIST_HEAD(&pdrvdata.consoles); |
6bdf2afd | 1743 | INIT_LIST_HEAD(&pdrvdata.portdevs); |
38edf58d | 1744 | |
31610434 RR |
1745 | return register_virtio_driver(&virtio_console); |
1746 | } | |
7177876f AS |
1747 | |
1748 | static void __exit fini(void) | |
1749 | { | |
1750 | unregister_virtio_driver(&virtio_console); | |
1751 | ||
1752 | class_destroy(pdrvdata.class); | |
1753 | if (pdrvdata.debugfs_dir) | |
1754 | debugfs_remove_recursive(pdrvdata.debugfs_dir); | |
1755 | } | |
31610434 | 1756 | module_init(init); |
7177876f | 1757 | module_exit(fini); |
31610434 RR |
1758 | |
1759 | MODULE_DEVICE_TABLE(virtio, id_table); | |
1760 | MODULE_DESCRIPTION("Virtio console driver"); | |
1761 | MODULE_LICENSE("GPL"); |