aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/hvc/Makefile1
-rw-r--r--drivers/tty/hvc/virtio_console.c1838
-rw-r--r--drivers/tty/n_gsm.c1
-rw-r--r--drivers/tty/n_hdlc.c90
-rw-r--r--drivers/tty/serial/68328serial.c29
-rw-r--r--drivers/tty/serial/68360serial.c1
-rw-r--r--drivers/tty/serial/8250.c3
-rw-r--r--drivers/tty/serial/Kconfig1
-rw-r--r--drivers/tty/serial/amba-pl010.c2
-rw-r--r--drivers/tty/serial/amba-pl011.c513
-rw-r--r--drivers/tty/serial/bfin_5xx.c15
-rw-r--r--drivers/tty/serial/max3100.c2
-rw-r--r--drivers/tty/serial/max3107.c2
-rw-r--r--drivers/tty/serial/sb1250-duart.c2
-rw-r--r--drivers/tty/serial/serial_cs.c1
-rw-r--r--drivers/tty/sysrq.c19
-rw-r--r--drivers/tty/tty_io.c8
-rw-r--r--drivers/tty/vt/selection.c4
-rw-r--r--drivers/tty/vt/vc_screen.c16
-rw-r--r--drivers/tty/vt/vt.c135
-rw-r--r--drivers/tty/vt/vt_ioctl.c60
21 files changed, 657 insertions, 2086 deletions
diff --git a/drivers/tty/hvc/Makefile b/drivers/tty/hvc/Makefile
index e6bed5f177ff..d79e7e9bf9d2 100644
--- a/drivers/tty/hvc/Makefile
+++ b/drivers/tty/hvc/Makefile
@@ -10,4 +10,3 @@ obj-$(CONFIG_HVC_XEN) += hvc_xen.o
10obj-$(CONFIG_HVC_IUCV) += hvc_iucv.o 10obj-$(CONFIG_HVC_IUCV) += hvc_iucv.o
11obj-$(CONFIG_HVC_UDBG) += hvc_udbg.o 11obj-$(CONFIG_HVC_UDBG) += hvc_udbg.o
12obj-$(CONFIG_HVCS) += hvcs.o 12obj-$(CONFIG_HVCS) += hvcs.o
13obj-$(CONFIG_VIRTIO_CONSOLE) += virtio_console.o
diff --git a/drivers/tty/hvc/virtio_console.c b/drivers/tty/hvc/virtio_console.c
deleted file mode 100644
index 896a2ced1d27..000000000000
--- a/drivers/tty/hvc/virtio_console.c
+++ /dev/null
@@ -1,1838 +0,0 @@
1/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010 Red Hat, Inc.
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 */
19#include <linux/cdev.h>
20#include <linux/debugfs.h>
21#include <linux/device.h>
22#include <linux/err.h>
23#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/poll.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/virtio.h>
31#include <linux/virtio_console.h>
32#include <linux/wait.h>
33#include <linux/workqueue.h>
34#include "hvc_console.h"
35
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 */
44struct ports_driver_data {
45 /* Used for registering chardevs */
46 struct class *class;
47
48 /* Used for exporting per-port information to debugfs */
49 struct dentry *debugfs_dir;
50
51 /* List of all the devices we're handling */
52 struct list_head portdevs;
53
54 /* Number of devices this driver is handling */
55 unsigned int index;
56
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
69 /* All the console devices handled by this driver */
70 struct list_head consoles;
71};
72static struct ports_driver_data pdrvdata;
73
74DEFINE_SPINLOCK(pdrvdata_lock);
75
76/* This struct holds information that's relevant only for console ports */
77struct 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
84 /* The size of the console */
85 struct winsize ws;
86
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
97struct 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
109/*
110 * This is a per-device struct that stores data common to all the
111 * ports for that device (vdev->priv).
112 */
113struct ports_device {
114 /* Next portdev in the list, head is in the pdrvdata struct */
115 struct list_head list;
116
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 */
132 struct virtio_console_config config;
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;
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;
151};
152
153/* This struct holds the per-port data */
154struct port {
155 /* Next port in the list, head is in the ports_device */
156 struct list_head list;
157
158 /* Pointer to the parent virtio_console device */
159 struct ports_device *portdev;
160
161 /* The current buffer from which data has to be fed to readers */
162 struct port_buffer *inbuf;
163
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
171 /* Protect the operations on the out_vq. */
172 spinlock_t outvq_lock;
173
174 /* The IO vqs for this port */
175 struct virtqueue *in_vq, *out_vq;
176
177 /* File in the debugfs directory that exposes this port's information */
178 struct dentry *debugfs_file;
179
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;
185
186 /* Each port associates with a separate char device */
187 struct cdev *cdev;
188 struct device *dev;
189
190 /* Reference-counting to handle port hot-unplugs and file operations */
191 struct kref kref;
192
193 /* A waitqueue for poll() or blocking read operations */
194 wait_queue_head_t waitqueue;
195
196 /* The 'name' of the port that we expose via sysfs properties */
197 char *name;
198
199 /* We can notify apps of host connect / disconnect events via SIGIO */
200 struct fasync_struct *async_queue;
201
202 /* The 'id' to identify the port with the Host */
203 u32 id;
204
205 bool outvq_full;
206
207 /* Is the host device open */
208 bool host_connected;
209
210 /* We should allow only one process to open a port */
211 bool guest_connected;
212};
213
214/* This is the very early arch-specified put chars function. */
215static int (*early_put_chars)(u32, const char *, int);
216
217static struct port *find_port_by_vtermno(u32 vtermno)
218{
219 struct port *port;
220 struct console *cons;
221 unsigned long flags;
222
223 spin_lock_irqsave(&pdrvdata_lock, flags);
224 list_for_each_entry(cons, &pdrvdata.consoles, list) {
225 if (cons->vtermno == vtermno) {
226 port = container_of(cons, struct port, cons);
227 goto out;
228 }
229 }
230 port = NULL;
231out:
232 spin_unlock_irqrestore(&pdrvdata_lock, flags);
233 return port;
234}
235
236static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
237 dev_t dev)
238{
239 struct port *port;
240 unsigned long flags;
241
242 spin_lock_irqsave(&portdev->ports_lock, flags);
243 list_for_each_entry(port, &portdev->ports, list)
244 if (port->cdev->dev == dev)
245 goto out;
246 port = NULL;
247out:
248 spin_unlock_irqrestore(&portdev->ports_lock, flags);
249
250 return port;
251}
252
253static struct port *find_port_by_devt(dev_t dev)
254{
255 struct ports_device *portdev;
256 struct port *port;
257 unsigned long flags;
258
259 spin_lock_irqsave(&pdrvdata_lock, flags);
260 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
261 port = find_port_by_devt_in_portdev(portdev, dev);
262 if (port)
263 goto out;
264 }
265 port = NULL;
266out:
267 spin_unlock_irqrestore(&pdrvdata_lock, flags);
268 return port;
269}
270
271static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
272{
273 struct port *port;
274 unsigned long flags;
275
276 spin_lock_irqsave(&portdev->ports_lock, flags);
277 list_for_each_entry(port, &portdev->ports, list)
278 if (port->id == id)
279 goto out;
280 port = NULL;
281out:
282 spin_unlock_irqrestore(&portdev->ports_lock, flags);
283
284 return port;
285}
286
287static struct port *find_port_by_vq(struct ports_device *portdev,
288 struct virtqueue *vq)
289{
290 struct port *port;
291 unsigned long flags;
292
293 spin_lock_irqsave(&portdev->ports_lock, flags);
294 list_for_each_entry(port, &portdev->ports, list)
295 if (port->in_vq == vq || port->out_vq == vq)
296 goto out;
297 port = NULL;
298out:
299 spin_unlock_irqrestore(&portdev->ports_lock, flags);
300 return port;
301}
302
303static bool is_console_port(struct port *port)
304{
305 if (port->cons.hvc)
306 return true;
307 return false;
308}
309
310static inline bool use_multiport(struct ports_device *portdev)
311{
312 /*
313 * This condition can be true when put_chars is called from
314 * early_init
315 */
316 if (!portdev->vdev)
317 return 0;
318 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
319}
320
321static void free_buf(struct port_buffer *buf)
322{
323 kfree(buf->buf);
324 kfree(buf);
325}
326
327static struct port_buffer *alloc_buf(size_t buf_size)
328{
329 struct port_buffer *buf;
330
331 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
332 if (!buf)
333 goto fail;
334 buf->buf = kzalloc(buf_size, GFP_KERNEL);
335 if (!buf->buf)
336 goto free_buf;
337 buf->len = 0;
338 buf->offset = 0;
339 buf->size = buf_size;
340 return buf;
341
342free_buf:
343 kfree(buf);
344fail:
345 return NULL;
346}
347
348/* Callers should take appropriate locks */
349static void *get_inbuf(struct port *port)
350{
351 struct port_buffer *buf;
352 struct virtqueue *vq;
353 unsigned int len;
354
355 vq = port->in_vq;
356 buf = virtqueue_get_buf(vq, &len);
357 if (buf) {
358 buf->len = len;
359 buf->offset = 0;
360 }
361 return buf;
362}
363
364/*
365 * Create a scatter-gather list representing our input buffer and put
366 * it in the queue.
367 *
368 * Callers should take appropriate locks.
369 */
370static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
371{
372 struct scatterlist sg[1];
373 int ret;
374
375 sg_init_one(sg, buf->buf, buf->size);
376
377 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
378 virtqueue_kick(vq);
379 return ret;
380}
381
382/* Discard any unread data this port has. Callers lockers. */
383static void discard_port_data(struct port *port)
384{
385 struct port_buffer *buf;
386 struct virtqueue *vq;
387 unsigned int len;
388 int ret;
389
390 vq = port->in_vq;
391 if (port->inbuf)
392 buf = port->inbuf;
393 else
394 buf = virtqueue_get_buf(vq, &len);
395
396 ret = 0;
397 while (buf) {
398 if (add_inbuf(vq, buf) < 0) {
399 ret++;
400 free_buf(buf);
401 }
402 buf = virtqueue_get_buf(vq, &len);
403 }
404 port->inbuf = NULL;
405 if (ret)
406 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
407 ret);
408}
409
410static bool port_has_data(struct port *port)
411{
412 unsigned long flags;
413 bool ret;
414
415 spin_lock_irqsave(&port->inbuf_lock, flags);
416 if (port->inbuf) {
417 ret = true;
418 goto out;
419 }
420 port->inbuf = get_inbuf(port);
421 if (port->inbuf) {
422 ret = true;
423 goto out;
424 }
425 ret = false;
426out:
427 spin_unlock_irqrestore(&port->inbuf_lock, flags);
428 return ret;
429}
430
431static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
432 unsigned int event, unsigned int value)
433{
434 struct scatterlist sg[1];
435 struct virtio_console_control cpkt;
436 struct virtqueue *vq;
437 unsigned int len;
438
439 if (!use_multiport(portdev))
440 return 0;
441
442 cpkt.id = port_id;
443 cpkt.event = event;
444 cpkt.value = value;
445
446 vq = portdev->c_ovq;
447
448 sg_init_one(sg, &cpkt, sizeof(cpkt));
449 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
450 virtqueue_kick(vq);
451 while (!virtqueue_get_buf(vq, &len))
452 cpu_relax();
453 }
454 return 0;
455}
456
457static ssize_t send_control_msg(struct port *port, unsigned int event,
458 unsigned int value)
459{
460 /* Did the port get unplugged before userspace closed it? */
461 if (port->portdev)
462 return __send_control_msg(port->portdev, port->id, event, value);
463 return 0;
464}
465
466/* Callers must take the port->outvq_lock */
467static void reclaim_consumed_buffers(struct port *port)
468{
469 void *buf;
470 unsigned int len;
471
472 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
473 kfree(buf);
474 port->outvq_full = false;
475 }
476}
477
478static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
479 bool nonblock)
480{
481 struct scatterlist sg[1];
482 struct virtqueue *out_vq;
483 ssize_t ret;
484 unsigned long flags;
485 unsigned int len;
486
487 out_vq = port->out_vq;
488
489 spin_lock_irqsave(&port->outvq_lock, flags);
490
491 reclaim_consumed_buffers(port);
492
493 sg_init_one(sg, in_buf, in_count);
494 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
495
496 /* Tell Host to go! */
497 virtqueue_kick(out_vq);
498
499 if (ret < 0) {
500 in_count = 0;
501 goto done;
502 }
503
504 if (ret == 0)
505 port->outvq_full = true;
506
507 if (nonblock)
508 goto done;
509
510 /*
511 * Wait till the host acknowledges it pushed out the data we
512 * sent. This is done for data from the hvc_console; the tty
513 * operations are performed with spinlocks held so we can't
514 * sleep here. An alternative would be to copy the data to a
515 * buffer and relax the spinning requirement. The downside is
516 * we need to kmalloc a GFP_ATOMIC buffer each time the
517 * console driver writes something out.
518 */
519 while (!virtqueue_get_buf(out_vq, &len))
520 cpu_relax();
521done:
522 spin_unlock_irqrestore(&port->outvq_lock, flags);
523 /*
524 * We're expected to return the amount of data we wrote -- all
525 * of it
526 */
527 return in_count;
528}
529
530/*
531 * Give out the data that's requested from the buffer that we have
532 * queued up.
533 */
534static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
535 bool to_user)
536{
537 struct port_buffer *buf;
538 unsigned long flags;
539
540 if (!out_count || !port_has_data(port))
541 return 0;
542
543 buf = port->inbuf;
544 out_count = min(out_count, buf->len - buf->offset);
545
546 if (to_user) {
547 ssize_t ret;
548
549 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
550 if (ret)
551 return -EFAULT;
552 } else {
553 memcpy(out_buf, buf->buf + buf->offset, out_count);
554 }
555
556 buf->offset += out_count;
557
558 if (buf->offset == buf->len) {
559 /*
560 * We're done using all the data in this buffer.
561 * Re-queue so that the Host can send us more data.
562 */
563 spin_lock_irqsave(&port->inbuf_lock, flags);
564 port->inbuf = NULL;
565
566 if (add_inbuf(port->in_vq, buf) < 0)
567 dev_warn(port->dev, "failed add_buf\n");
568
569 spin_unlock_irqrestore(&port->inbuf_lock, flags);
570 }
571 /* Return the number of bytes actually copied */
572 return out_count;
573}
574
575/* The condition that must be true for polling to end */
576static bool will_read_block(struct port *port)
577{
578 if (!port->guest_connected) {
579 /* Port got hot-unplugged. Let's exit. */
580 return false;
581 }
582 return !port_has_data(port) && port->host_connected;
583}
584
585static bool will_write_block(struct port *port)
586{
587 bool ret;
588
589 if (!port->guest_connected) {
590 /* Port got hot-unplugged. Let's exit. */
591 return false;
592 }
593 if (!port->host_connected)
594 return true;
595
596 spin_lock_irq(&port->outvq_lock);
597 /*
598 * Check if the Host has consumed any buffers since we last
599 * sent data (this is only applicable for nonblocking ports).
600 */
601 reclaim_consumed_buffers(port);
602 ret = port->outvq_full;
603 spin_unlock_irq(&port->outvq_lock);
604
605 return ret;
606}
607
608static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
609 size_t count, loff_t *offp)
610{
611 struct port *port;
612 ssize_t ret;
613
614 port = filp->private_data;
615
616 if (!port_has_data(port)) {
617 /*
618 * If nothing's connected on the host just return 0 in
619 * case of list_empty; this tells the userspace app
620 * that there's no connection
621 */
622 if (!port->host_connected)
623 return 0;
624 if (filp->f_flags & O_NONBLOCK)
625 return -EAGAIN;
626
627 ret = wait_event_interruptible(port->waitqueue,
628 !will_read_block(port));
629 if (ret < 0)
630 return ret;
631 }
632 /* Port got hot-unplugged. */
633 if (!port->guest_connected)
634 return -ENODEV;
635 /*
636 * We could've received a disconnection message while we were
637 * waiting for more data.
638 *
639 * This check is not clubbed in the if() statement above as we
640 * might receive some data as well as the host could get
641 * disconnected after we got woken up from our wait. So we
642 * really want to give off whatever data we have and only then
643 * check for host_connected.
644 */
645 if (!port_has_data(port) && !port->host_connected)
646 return 0;
647
648 return fill_readbuf(port, ubuf, count, true);
649}
650
651static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
652 size_t count, loff_t *offp)
653{
654 struct port *port;
655 char *buf;
656 ssize_t ret;
657 bool nonblock;
658
659 /* Userspace could be out to fool us */
660 if (!count)
661 return 0;
662
663 port = filp->private_data;
664
665 nonblock = filp->f_flags & O_NONBLOCK;
666
667 if (will_write_block(port)) {
668 if (nonblock)
669 return -EAGAIN;
670
671 ret = wait_event_interruptible(port->waitqueue,
672 !will_write_block(port));
673 if (ret < 0)
674 return ret;
675 }
676 /* Port got hot-unplugged. */
677 if (!port->guest_connected)
678 return -ENODEV;
679
680 count = min((size_t)(32 * 1024), count);
681
682 buf = kmalloc(count, GFP_KERNEL);
683 if (!buf)
684 return -ENOMEM;
685
686 ret = copy_from_user(buf, ubuf, count);
687 if (ret) {
688 ret = -EFAULT;
689 goto free_buf;
690 }
691
692 /*
693 * We now ask send_buf() to not spin for generic ports -- we
694 * can re-use the same code path that non-blocking file
695 * descriptors take for blocking file descriptors since the
696 * wait is already done and we're certain the write will go
697 * through to the host.
698 */
699 nonblock = true;
700 ret = send_buf(port, buf, count, nonblock);
701
702 if (nonblock && ret > 0)
703 goto out;
704
705free_buf:
706 kfree(buf);
707out:
708 return ret;
709}
710
711static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
712{
713 struct port *port;
714 unsigned int ret;
715
716 port = filp->private_data;
717 poll_wait(filp, &port->waitqueue, wait);
718
719 if (!port->guest_connected) {
720 /* Port got unplugged */
721 return POLLHUP;
722 }
723 ret = 0;
724 if (!will_read_block(port))
725 ret |= POLLIN | POLLRDNORM;
726 if (!will_write_block(port))
727 ret |= POLLOUT;
728 if (!port->host_connected)
729 ret |= POLLHUP;
730
731 return ret;
732}
733
734static void remove_port(struct kref *kref);
735
736static int port_fops_release(struct inode *inode, struct file *filp)
737{
738 struct port *port;
739
740 port = filp->private_data;
741
742 /* Notify host of port being closed */
743 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
744
745 spin_lock_irq(&port->inbuf_lock);
746 port->guest_connected = false;
747
748 discard_port_data(port);
749
750 spin_unlock_irq(&port->inbuf_lock);
751
752 spin_lock_irq(&port->outvq_lock);
753 reclaim_consumed_buffers(port);
754 spin_unlock_irq(&port->outvq_lock);
755
756 /*
757 * Locks aren't necessary here as a port can't be opened after
758 * unplug, and if a port isn't unplugged, a kref would already
759 * exist for the port. Plus, taking ports_lock here would
760 * create a dependency on other locks taken by functions
761 * inside remove_port if we're the last holder of the port,
762 * creating many problems.
763 */
764 kref_put(&port->kref, remove_port);
765
766 return 0;
767}
768
769static int port_fops_open(struct inode *inode, struct file *filp)
770{
771 struct cdev *cdev = inode->i_cdev;
772 struct port *port;
773 int ret;
774
775 port = find_port_by_devt(cdev->dev);
776 filp->private_data = port;
777
778 /* Prevent against a port getting hot-unplugged at the same time */
779 spin_lock_irq(&port->portdev->ports_lock);
780 kref_get(&port->kref);
781 spin_unlock_irq(&port->portdev->ports_lock);
782
783 /*
784 * Don't allow opening of console port devices -- that's done
785 * via /dev/hvc
786 */
787 if (is_console_port(port)) {
788 ret = -ENXIO;
789 goto out;
790 }
791
792 /* Allow only one process to open a particular port at a time */
793 spin_lock_irq(&port->inbuf_lock);
794 if (port->guest_connected) {
795 spin_unlock_irq(&port->inbuf_lock);
796 ret = -EMFILE;
797 goto out;
798 }
799
800 port->guest_connected = true;
801 spin_unlock_irq(&port->inbuf_lock);
802
803 spin_lock_irq(&port->outvq_lock);
804 /*
805 * There might be a chance that we missed reclaiming a few
806 * buffers in the window of the port getting previously closed
807 * and opening now.
808 */
809 reclaim_consumed_buffers(port);
810 spin_unlock_irq(&port->outvq_lock);
811
812 nonseekable_open(inode, filp);
813
814 /* Notify host of port being opened */
815 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
816
817 return 0;
818out:
819 kref_put(&port->kref, remove_port);
820 return ret;
821}
822
823static int port_fops_fasync(int fd, struct file *filp, int mode)
824{
825 struct port *port;
826
827 port = filp->private_data;
828 return fasync_helper(fd, filp, mode, &port->async_queue);
829}
830
831/*
832 * The file operations that we support: programs in the guest can open
833 * a console device, read from it, write to it, poll for data and
834 * close it. The devices are at
835 * /dev/vport<device number>p<port number>
836 */
837static const struct file_operations port_fops = {
838 .owner = THIS_MODULE,
839 .open = port_fops_open,
840 .read = port_fops_read,
841 .write = port_fops_write,
842 .poll = port_fops_poll,
843 .release = port_fops_release,
844 .fasync = port_fops_fasync,
845 .llseek = no_llseek,
846};
847
848/*
849 * The put_chars() callback is pretty straightforward.
850 *
851 * We turn the characters into a scatter-gather list, add it to the
852 * output queue and then kick the Host. Then we sit here waiting for
853 * it to finish: inefficient in theory, but in practice
854 * implementations will do it immediately (lguest's Launcher does).
855 */
856static int put_chars(u32 vtermno, const char *buf, int count)
857{
858 struct port *port;
859
860 if (unlikely(early_put_chars))
861 return early_put_chars(vtermno, buf, count);
862
863 port = find_port_by_vtermno(vtermno);
864 if (!port)
865 return -EPIPE;
866
867 return send_buf(port, (void *)buf, count, false);
868}
869
870/*
871 * get_chars() is the callback from the hvc_console infrastructure
872 * when an interrupt is received.
873 *
874 * We call out to fill_readbuf that gets us the required data from the
875 * buffers that are queued up.
876 */
877static int get_chars(u32 vtermno, char *buf, int count)
878{
879 struct port *port;
880
881 /* If we've not set up the port yet, we have no input to give. */
882 if (unlikely(early_put_chars))
883 return 0;
884
885 port = find_port_by_vtermno(vtermno);
886 if (!port)
887 return -EPIPE;
888
889 /* If we don't have an input queue yet, we can't get input. */
890 BUG_ON(!port->in_vq);
891
892 return fill_readbuf(port, buf, count, false);
893}
894
895static void resize_console(struct port *port)
896{
897 struct virtio_device *vdev;
898
899 /* The port could have been hot-unplugged */
900 if (!port || !is_console_port(port))
901 return;
902
903 vdev = port->portdev->vdev;
904 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
905 hvc_resize(port->cons.hvc, port->cons.ws);
906}
907
908/* We set the configuration at this point, since we now have a tty */
909static int notifier_add_vio(struct hvc_struct *hp, int data)
910{
911 struct port *port;
912
913 port = find_port_by_vtermno(hp->vtermno);
914 if (!port)
915 return -EINVAL;
916
917 hp->irq_requested = 1;
918 resize_console(port);
919
920 return 0;
921}
922
923static void notifier_del_vio(struct hvc_struct *hp, int data)
924{
925 hp->irq_requested = 0;
926}
927
928/* The operations for console ports. */
929static const struct hv_ops hv_ops = {
930 .get_chars = get_chars,
931 .put_chars = put_chars,
932 .notifier_add = notifier_add_vio,
933 .notifier_del = notifier_del_vio,
934 .notifier_hangup = notifier_del_vio,
935};
936
937/*
938 * Console drivers are initialized very early so boot messages can go
939 * out, so we do things slightly differently from the generic virtio
940 * initialization of the net and block drivers.
941 *
942 * At this stage, the console is output-only. It's too early to set
943 * up a virtqueue, so we let the drivers do some boutique early-output
944 * thing.
945 */
946int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
947{
948 early_put_chars = put_chars;
949 return hvc_instantiate(0, 0, &hv_ops);
950}
951
952int init_port_console(struct port *port)
953{
954 int ret;
955
956 /*
957 * The Host's telling us this port is a console port. Hook it
958 * up with an hvc console.
959 *
960 * To set up and manage our virtual console, we call
961 * hvc_alloc().
962 *
963 * The first argument of hvc_alloc() is the virtual console
964 * number. The second argument is the parameter for the
965 * notification mechanism (like irq number). We currently
966 * leave this as zero, virtqueues have implicit notifications.
967 *
968 * The third argument is a "struct hv_ops" containing the
969 * put_chars() get_chars(), notifier_add() and notifier_del()
970 * pointers. The final argument is the output buffer size: we
971 * can do any size, so we put PAGE_SIZE here.
972 */
973 port->cons.vtermno = pdrvdata.next_vtermno;
974
975 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
976 if (IS_ERR(port->cons.hvc)) {
977 ret = PTR_ERR(port->cons.hvc);
978 dev_err(port->dev,
979 "error %d allocating hvc for port\n", ret);
980 port->cons.hvc = NULL;
981 return ret;
982 }
983 spin_lock_irq(&pdrvdata_lock);
984 pdrvdata.next_vtermno++;
985 list_add_tail(&port->cons.list, &pdrvdata.consoles);
986 spin_unlock_irq(&pdrvdata_lock);
987 port->guest_connected = true;
988
989 /*
990 * Start using the new console output if this is the first
991 * console to come up.
992 */
993 if (early_put_chars)
994 early_put_chars = NULL;
995
996 /* Notify host of port being opened */
997 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
998
999 return 0;
1000}
1001
1002static ssize_t show_port_name(struct device *dev,
1003 struct device_attribute *attr, char *buffer)
1004{
1005 struct port *port;
1006
1007 port = dev_get_drvdata(dev);
1008
1009 return sprintf(buffer, "%s\n", port->name);
1010}
1011
1012static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1013
1014static struct attribute *port_sysfs_entries[] = {
1015 &dev_attr_name.attr,
1016 NULL
1017};
1018
1019static struct attribute_group port_attribute_group = {
1020 .name = NULL, /* put in device directory */
1021 .attrs = port_sysfs_entries,
1022};
1023
1024static int debugfs_open(struct inode *inode, struct file *filp)
1025{
1026 filp->private_data = inode->i_private;
1027 return 0;
1028}
1029
1030static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1031 size_t count, loff_t *offp)
1032{
1033 struct port *port;
1034 char *buf;
1035 ssize_t ret, out_offset, out_count;
1036
1037 out_count = 1024;
1038 buf = kmalloc(out_count, GFP_KERNEL);
1039 if (!buf)
1040 return -ENOMEM;
1041
1042 port = filp->private_data;
1043 out_offset = 0;
1044 out_offset += snprintf(buf + out_offset, out_count,
1045 "name: %s\n", port->name ? port->name : "");
1046 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1047 "guest_connected: %d\n", port->guest_connected);
1048 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1049 "host_connected: %d\n", port->host_connected);
1050 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1051 "outvq_full: %d\n", port->outvq_full);
1052 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1053 "is_console: %s\n",
1054 is_console_port(port) ? "yes" : "no");
1055 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1056 "console_vtermno: %u\n", port->cons.vtermno);
1057
1058 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1059 kfree(buf);
1060 return ret;
1061}
1062
1063static const struct file_operations port_debugfs_ops = {
1064 .owner = THIS_MODULE,
1065 .open = debugfs_open,
1066 .read = debugfs_read,
1067};
1068
1069static void set_console_size(struct port *port, u16 rows, u16 cols)
1070{
1071 if (!port || !is_console_port(port))
1072 return;
1073
1074 port->cons.ws.ws_row = rows;
1075 port->cons.ws.ws_col = cols;
1076}
1077
1078static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1079{
1080 struct port_buffer *buf;
1081 unsigned int nr_added_bufs;
1082 int ret;
1083
1084 nr_added_bufs = 0;
1085 do {
1086 buf = alloc_buf(PAGE_SIZE);
1087 if (!buf)
1088 break;
1089
1090 spin_lock_irq(lock);
1091 ret = add_inbuf(vq, buf);
1092 if (ret < 0) {
1093 spin_unlock_irq(lock);
1094 free_buf(buf);
1095 break;
1096 }
1097 nr_added_bufs++;
1098 spin_unlock_irq(lock);
1099 } while (ret > 0);
1100
1101 return nr_added_bufs;
1102}
1103
1104static void send_sigio_to_port(struct port *port)
1105{
1106 if (port->async_queue && port->guest_connected)
1107 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1108}
1109
1110static int add_port(struct ports_device *portdev, u32 id)
1111{
1112 char debugfs_name[16];
1113 struct port *port;
1114 struct port_buffer *buf;
1115 dev_t devt;
1116 unsigned int nr_added_bufs;
1117 int err;
1118
1119 port = kmalloc(sizeof(*port), GFP_KERNEL);
1120 if (!port) {
1121 err = -ENOMEM;
1122 goto fail;
1123 }
1124 kref_init(&port->kref);
1125
1126 port->portdev = portdev;
1127 port->id = id;
1128
1129 port->name = NULL;
1130 port->inbuf = NULL;
1131 port->cons.hvc = NULL;
1132 port->async_queue = NULL;
1133
1134 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1135
1136 port->host_connected = port->guest_connected = false;
1137
1138 port->outvq_full = false;
1139
1140 port->in_vq = portdev->in_vqs[port->id];
1141 port->out_vq = portdev->out_vqs[port->id];
1142
1143 port->cdev = cdev_alloc();
1144 if (!port->cdev) {
1145 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1146 err = -ENOMEM;
1147 goto free_port;
1148 }
1149 port->cdev->ops = &port_fops;
1150
1151 devt = MKDEV(portdev->chr_major, id);
1152 err = cdev_add(port->cdev, devt, 1);
1153 if (err < 0) {
1154 dev_err(&port->portdev->vdev->dev,
1155 "Error %d adding cdev for port %u\n", err, id);
1156 goto free_cdev;
1157 }
1158 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1159 devt, port, "vport%up%u",
1160 port->portdev->drv_index, id);
1161 if (IS_ERR(port->dev)) {
1162 err = PTR_ERR(port->dev);
1163 dev_err(&port->portdev->vdev->dev,
1164 "Error %d creating device for port %u\n",
1165 err, id);
1166 goto free_cdev;
1167 }
1168
1169 spin_lock_init(&port->inbuf_lock);
1170 spin_lock_init(&port->outvq_lock);
1171 init_waitqueue_head(&port->waitqueue);
1172
1173 /* Fill the in_vq with buffers so the host can send us data. */
1174 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1175 if (!nr_added_bufs) {
1176 dev_err(port->dev, "Error allocating inbufs\n");
1177 err = -ENOMEM;
1178 goto free_device;
1179 }
1180
1181 /*
1182 * If we're not using multiport support, this has to be a console port
1183 */
1184 if (!use_multiport(port->portdev)) {
1185 err = init_port_console(port);
1186 if (err)
1187 goto free_inbufs;
1188 }
1189
1190 spin_lock_irq(&portdev->ports_lock);
1191 list_add_tail(&port->list, &port->portdev->ports);
1192 spin_unlock_irq(&portdev->ports_lock);
1193
1194 /*
1195 * Tell the Host we're set so that it can send us various
1196 * configuration parameters for this port (eg, port name,
1197 * caching, whether this is a console port, etc.)
1198 */
1199 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1200
1201 if (pdrvdata.debugfs_dir) {
1202 /*
1203 * Finally, create the debugfs file that we can use to
1204 * inspect a port's state at any time
1205 */
1206 sprintf(debugfs_name, "vport%up%u",
1207 port->portdev->drv_index, id);
1208 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1209 pdrvdata.debugfs_dir,
1210 port,
1211 &port_debugfs_ops);
1212 }
1213 return 0;
1214
1215free_inbufs:
1216 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1217 free_buf(buf);
1218free_device:
1219 device_destroy(pdrvdata.class, port->dev->devt);
1220free_cdev:
1221 cdev_del(port->cdev);
1222free_port:
1223 kfree(port);
1224fail:
1225 /* The host might want to notify management sw about port add failure */
1226 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1227 return err;
1228}
1229
1230/* No users remain, remove all port-specific data. */
1231static void remove_port(struct kref *kref)
1232{
1233 struct port *port;
1234
1235 port = container_of(kref, struct port, kref);
1236
1237 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1238 device_destroy(pdrvdata.class, port->dev->devt);
1239 cdev_del(port->cdev);
1240
1241 kfree(port->name);
1242
1243 debugfs_remove(port->debugfs_file);
1244
1245 kfree(port);
1246}
1247
1248/*
1249 * Port got unplugged. Remove port from portdev's list and drop the
1250 * kref reference. If no userspace has this port opened, it will
1251 * result in immediate removal the port.
1252 */
1253static void unplug_port(struct port *port)
1254{
1255 struct port_buffer *buf;
1256
1257 spin_lock_irq(&port->portdev->ports_lock);
1258 list_del(&port->list);
1259 spin_unlock_irq(&port->portdev->ports_lock);
1260
1261 if (port->guest_connected) {
1262 port->guest_connected = false;
1263 port->host_connected = false;
1264 wake_up_interruptible(&port->waitqueue);
1265
1266 /* Let the app know the port is going down. */
1267 send_sigio_to_port(port);
1268 }
1269
1270 if (is_console_port(port)) {
1271 spin_lock_irq(&pdrvdata_lock);
1272 list_del(&port->cons.list);
1273 spin_unlock_irq(&pdrvdata_lock);
1274#if 0
1275 /*
1276 * hvc_remove() not called as removing one hvc port
1277 * results in other hvc ports getting frozen.
1278 *
1279 * Once this is resolved in hvc, this functionality
1280 * will be enabled. Till that is done, the -EPIPE
1281 * return from get_chars() above will help
1282 * hvc_console.c to clean up on ports we remove here.
1283 */
1284 hvc_remove(port->cons.hvc);
1285#endif
1286 }
1287
1288 /* Remove unused data this port might have received. */
1289 discard_port_data(port);
1290
1291 reclaim_consumed_buffers(port);
1292
1293 /* Remove buffers we queued up for the Host to send us data in. */
1294 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1295 free_buf(buf);
1296
1297 /*
1298 * We should just assume the device itself has gone off --
1299 * else a close on an open port later will try to send out a
1300 * control message.
1301 */
1302 port->portdev = NULL;
1303
1304 /*
1305 * Locks around here are not necessary - a port can't be
1306 * opened after we removed the port struct from ports_list
1307 * above.
1308 */
1309 kref_put(&port->kref, remove_port);
1310}
1311
1312/* Any private messages that the Host and Guest want to share */
1313static void handle_control_message(struct ports_device *portdev,
1314 struct port_buffer *buf)
1315{
1316 struct virtio_console_control *cpkt;
1317 struct port *port;
1318 size_t name_size;
1319 int err;
1320
1321 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1322
1323 port = find_port_by_id(portdev, cpkt->id);
1324 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1325 /* No valid header at start of buffer. Drop it. */
1326 dev_dbg(&portdev->vdev->dev,
1327 "Invalid index %u in control packet\n", cpkt->id);
1328 return;
1329 }
1330
1331 switch (cpkt->event) {
1332 case VIRTIO_CONSOLE_PORT_ADD:
1333 if (port) {
1334 dev_dbg(&portdev->vdev->dev,
1335 "Port %u already added\n", port->id);
1336 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1337 break;
1338 }
1339 if (cpkt->id >= portdev->config.max_nr_ports) {
1340 dev_warn(&portdev->vdev->dev,
1341 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1342 cpkt->id, portdev->config.max_nr_ports - 1);
1343 break;
1344 }
1345 add_port(portdev, cpkt->id);
1346 break;
1347 case VIRTIO_CONSOLE_PORT_REMOVE:
1348 unplug_port(port);
1349 break;
1350 case VIRTIO_CONSOLE_CONSOLE_PORT:
1351 if (!cpkt->value)
1352 break;
1353 if (is_console_port(port))
1354 break;
1355
1356 init_port_console(port);
1357 /*
1358 * Could remove the port here in case init fails - but
1359 * have to notify the host first.
1360 */
1361 break;
1362 case VIRTIO_CONSOLE_RESIZE: {
1363 struct {
1364 __u16 rows;
1365 __u16 cols;
1366 } size;
1367
1368 if (!is_console_port(port))
1369 break;
1370
1371 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1372 sizeof(size));
1373 set_console_size(port, size.rows, size.cols);
1374
1375 port->cons.hvc->irq_requested = 1;
1376 resize_console(port);
1377 break;
1378 }
1379 case VIRTIO_CONSOLE_PORT_OPEN:
1380 port->host_connected = cpkt->value;
1381 wake_up_interruptible(&port->waitqueue);
1382 /*
1383 * If the host port got closed and the host had any
1384 * unconsumed buffers, we'll be able to reclaim them
1385 * now.
1386 */
1387 spin_lock_irq(&port->outvq_lock);
1388 reclaim_consumed_buffers(port);
1389 spin_unlock_irq(&port->outvq_lock);
1390
1391 /*
1392 * If the guest is connected, it'll be interested in
1393 * knowing the host connection state changed.
1394 */
1395 send_sigio_to_port(port);
1396 break;
1397 case VIRTIO_CONSOLE_PORT_NAME:
1398 /*
1399 * Skip the size of the header and the cpkt to get the size
1400 * of the name that was sent
1401 */
1402 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1403
1404 port->name = kmalloc(name_size, GFP_KERNEL);
1405 if (!port->name) {
1406 dev_err(port->dev,
1407 "Not enough space to store port name\n");
1408 break;
1409 }
1410 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1411 name_size - 1);
1412 port->name[name_size - 1] = 0;
1413
1414 /*
1415 * Since we only have one sysfs attribute, 'name',
1416 * create it only if we have a name for the port.
1417 */
1418 err = sysfs_create_group(&port->dev->kobj,
1419 &port_attribute_group);
1420 if (err) {
1421 dev_err(port->dev,
1422 "Error %d creating sysfs device attributes\n",
1423 err);
1424 } else {
1425 /*
1426 * Generate a udev event so that appropriate
1427 * symlinks can be created based on udev
1428 * rules.
1429 */
1430 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1431 }
1432 break;
1433 }
1434}
1435
1436static void control_work_handler(struct work_struct *work)
1437{
1438 struct ports_device *portdev;
1439 struct virtqueue *vq;
1440 struct port_buffer *buf;
1441 unsigned int len;
1442
1443 portdev = container_of(work, struct ports_device, control_work);
1444 vq = portdev->c_ivq;
1445
1446 spin_lock(&portdev->cvq_lock);
1447 while ((buf = virtqueue_get_buf(vq, &len))) {
1448 spin_unlock(&portdev->cvq_lock);
1449
1450 buf->len = len;
1451 buf->offset = 0;
1452
1453 handle_control_message(portdev, buf);
1454
1455 spin_lock(&portdev->cvq_lock);
1456 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1457 dev_warn(&portdev->vdev->dev,
1458 "Error adding buffer to queue\n");
1459 free_buf(buf);
1460 }
1461 }
1462 spin_unlock(&portdev->cvq_lock);
1463}
1464
1465static void in_intr(struct virtqueue *vq)
1466{
1467 struct port *port;
1468 unsigned long flags;
1469
1470 port = find_port_by_vq(vq->vdev->priv, vq);
1471 if (!port)
1472 return;
1473
1474 spin_lock_irqsave(&port->inbuf_lock, flags);
1475 if (!port->inbuf)
1476 port->inbuf = get_inbuf(port);
1477
1478 /*
1479 * Don't queue up data when port is closed. This condition
1480 * can be reached when a console port is not yet connected (no
1481 * tty is spawned) and the host sends out data to console
1482 * ports. For generic serial ports, the host won't
1483 * (shouldn't) send data till the guest is connected.
1484 */
1485 if (!port->guest_connected)
1486 discard_port_data(port);
1487
1488 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1489
1490 wake_up_interruptible(&port->waitqueue);
1491
1492 /* Send a SIGIO indicating new data in case the process asked for it */
1493 send_sigio_to_port(port);
1494
1495 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1496 hvc_kick();
1497}
1498
1499static void control_intr(struct virtqueue *vq)
1500{
1501 struct ports_device *portdev;
1502
1503 portdev = vq->vdev->priv;
1504 schedule_work(&portdev->control_work);
1505}
1506
1507static void config_intr(struct virtio_device *vdev)
1508{
1509 struct ports_device *portdev;
1510
1511 portdev = vdev->priv;
1512
1513 if (!use_multiport(portdev)) {
1514 struct port *port;
1515 u16 rows, cols;
1516
1517 vdev->config->get(vdev,
1518 offsetof(struct virtio_console_config, cols),
1519 &cols, sizeof(u16));
1520 vdev->config->get(vdev,
1521 offsetof(struct virtio_console_config, rows),
1522 &rows, sizeof(u16));
1523
1524 port = find_port_by_id(portdev, 0);
1525 set_console_size(port, rows, cols);
1526
1527 /*
1528 * We'll use this way of resizing only for legacy
1529 * support. For newer userspace
1530 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1531 * to indicate console size changes so that it can be
1532 * done per-port.
1533 */
1534 resize_console(port);
1535 }
1536}
1537
1538static int init_vqs(struct ports_device *portdev)
1539{
1540 vq_callback_t **io_callbacks;
1541 char **io_names;
1542 struct virtqueue **vqs;
1543 u32 i, j, nr_ports, nr_queues;
1544 int err;
1545
1546 nr_ports = portdev->config.max_nr_ports;
1547 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1548
1549 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1550 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1551 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1552 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1553 GFP_KERNEL);
1554 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1555 GFP_KERNEL);
1556 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1557 !portdev->out_vqs) {
1558 err = -ENOMEM;
1559 goto free;
1560 }
1561
1562 /*
1563 * For backward compat (newer host but older guest), the host
1564 * spawns a console port first and also inits the vqs for port
1565 * 0 before others.
1566 */
1567 j = 0;
1568 io_callbacks[j] = in_intr;
1569 io_callbacks[j + 1] = NULL;
1570 io_names[j] = "input";
1571 io_names[j + 1] = "output";
1572 j += 2;
1573
1574 if (use_multiport(portdev)) {
1575 io_callbacks[j] = control_intr;
1576 io_callbacks[j + 1] = NULL;
1577 io_names[j] = "control-i";
1578 io_names[j + 1] = "control-o";
1579
1580 for (i = 1; i < nr_ports; i++) {
1581 j += 2;
1582 io_callbacks[j] = in_intr;
1583 io_callbacks[j + 1] = NULL;
1584 io_names[j] = "input";
1585 io_names[j + 1] = "output";
1586 }
1587 }
1588 /* Find the queues. */
1589 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1590 io_callbacks,
1591 (const char **)io_names);
1592 if (err)
1593 goto free;
1594
1595 j = 0;
1596 portdev->in_vqs[0] = vqs[0];
1597 portdev->out_vqs[0] = vqs[1];
1598 j += 2;
1599 if (use_multiport(portdev)) {
1600 portdev->c_ivq = vqs[j];
1601 portdev->c_ovq = vqs[j + 1];
1602
1603 for (i = 1; i < nr_ports; i++) {
1604 j += 2;
1605 portdev->in_vqs[i] = vqs[j];
1606 portdev->out_vqs[i] = vqs[j + 1];
1607 }
1608 }
1609 kfree(io_names);
1610 kfree(io_callbacks);
1611 kfree(vqs);
1612
1613 return 0;
1614
1615free:
1616 kfree(portdev->out_vqs);
1617 kfree(portdev->in_vqs);
1618 kfree(io_names);
1619 kfree(io_callbacks);
1620 kfree(vqs);
1621
1622 return err;
1623}
1624
1625static const struct file_operations portdev_fops = {
1626 .owner = THIS_MODULE,
1627};
1628
1629/*
1630 * Once we're further in boot, we get probed like any other virtio
1631 * device.
1632 *
1633 * If the host also supports multiple console ports, we check the
1634 * config space to see how many ports the host has spawned. We
1635 * initialize each port found.
1636 */
1637static int __devinit virtcons_probe(struct virtio_device *vdev)
1638{
1639 struct ports_device *portdev;
1640 int err;
1641 bool multiport;
1642
1643 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1644 if (!portdev) {
1645 err = -ENOMEM;
1646 goto fail;
1647 }
1648
1649 /* Attach this portdev to this virtio_device, and vice-versa. */
1650 portdev->vdev = vdev;
1651 vdev->priv = portdev;
1652
1653 spin_lock_irq(&pdrvdata_lock);
1654 portdev->drv_index = pdrvdata.index++;
1655 spin_unlock_irq(&pdrvdata_lock);
1656
1657 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1658 &portdev_fops);
1659 if (portdev->chr_major < 0) {
1660 dev_err(&vdev->dev,
1661 "Error %d registering chrdev for device %u\n",
1662 portdev->chr_major, portdev->drv_index);
1663 err = portdev->chr_major;
1664 goto free;
1665 }
1666
1667 multiport = false;
1668 portdev->config.max_nr_ports = 1;
1669 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1670 multiport = true;
1671 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1672
1673 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1674 max_nr_ports),
1675 &portdev->config.max_nr_ports,
1676 sizeof(portdev->config.max_nr_ports));
1677 }
1678
1679 /* Let the Host know we support multiple ports.*/
1680 vdev->config->finalize_features(vdev);
1681
1682 err = init_vqs(portdev);
1683 if (err < 0) {
1684 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1685 goto free_chrdev;
1686 }
1687
1688 spin_lock_init(&portdev->ports_lock);
1689 INIT_LIST_HEAD(&portdev->ports);
1690
1691 if (multiport) {
1692 unsigned int nr_added_bufs;
1693
1694 spin_lock_init(&portdev->cvq_lock);
1695 INIT_WORK(&portdev->control_work, &control_work_handler);
1696
1697 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1698 if (!nr_added_bufs) {
1699 dev_err(&vdev->dev,
1700 "Error allocating buffers for control queue\n");
1701 err = -ENOMEM;
1702 goto free_vqs;
1703 }
1704 } else {
1705 /*
1706 * For backward compatibility: Create a console port
1707 * if we're running on older host.
1708 */
1709 add_port(portdev, 0);
1710 }
1711
1712 spin_lock_irq(&pdrvdata_lock);
1713 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1714 spin_unlock_irq(&pdrvdata_lock);
1715
1716 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1717 VIRTIO_CONSOLE_DEVICE_READY, 1);
1718 return 0;
1719
1720free_vqs:
1721 /* The host might want to notify mgmt sw about device add failure */
1722 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1723 VIRTIO_CONSOLE_DEVICE_READY, 0);
1724 vdev->config->del_vqs(vdev);
1725 kfree(portdev->in_vqs);
1726 kfree(portdev->out_vqs);
1727free_chrdev:
1728 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1729free:
1730 kfree(portdev);
1731fail:
1732 return err;
1733}
1734
1735static void virtcons_remove(struct virtio_device *vdev)
1736{
1737 struct ports_device *portdev;
1738 struct port *port, *port2;
1739
1740 portdev = vdev->priv;
1741
1742 spin_lock_irq(&pdrvdata_lock);
1743 list_del(&portdev->list);
1744 spin_unlock_irq(&pdrvdata_lock);
1745
1746 /* Disable interrupts for vqs */
1747 vdev->config->reset(vdev);
1748 /* Finish up work that's lined up */
1749 cancel_work_sync(&portdev->control_work);
1750
1751 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1752 unplug_port(port);
1753
1754 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1755
1756 /*
1757 * When yanking out a device, we immediately lose the
1758 * (device-side) queues. So there's no point in keeping the
1759 * guest side around till we drop our final reference. This
1760 * also means that any ports which are in an open state will
1761 * have to just stop using the port, as the vqs are going
1762 * away.
1763 */
1764 if (use_multiport(portdev)) {
1765 struct port_buffer *buf;
1766 unsigned int len;
1767
1768 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1769 free_buf(buf);
1770
1771 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1772 free_buf(buf);
1773 }
1774
1775 vdev->config->del_vqs(vdev);
1776 kfree(portdev->in_vqs);
1777 kfree(portdev->out_vqs);
1778
1779 kfree(portdev);
1780}
1781
1782static struct virtio_device_id id_table[] = {
1783 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1784 { 0 },
1785};
1786
1787static unsigned int features[] = {
1788 VIRTIO_CONSOLE_F_SIZE,
1789 VIRTIO_CONSOLE_F_MULTIPORT,
1790};
1791
1792static struct virtio_driver virtio_console = {
1793 .feature_table = features,
1794 .feature_table_size = ARRAY_SIZE(features),
1795 .driver.name = KBUILD_MODNAME,
1796 .driver.owner = THIS_MODULE,
1797 .id_table = id_table,
1798 .probe = virtcons_probe,
1799 .remove = virtcons_remove,
1800 .config_changed = config_intr,
1801};
1802
1803static int __init init(void)
1804{
1805 int err;
1806
1807 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1808 if (IS_ERR(pdrvdata.class)) {
1809 err = PTR_ERR(pdrvdata.class);
1810 pr_err("Error %d creating virtio-ports class\n", err);
1811 return err;
1812 }
1813
1814 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1815 if (!pdrvdata.debugfs_dir) {
1816 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1817 PTR_ERR(pdrvdata.debugfs_dir));
1818 }
1819 INIT_LIST_HEAD(&pdrvdata.consoles);
1820 INIT_LIST_HEAD(&pdrvdata.portdevs);
1821
1822 return register_virtio_driver(&virtio_console);
1823}
1824
1825static void __exit fini(void)
1826{
1827 unregister_virtio_driver(&virtio_console);
1828
1829 class_destroy(pdrvdata.class);
1830 if (pdrvdata.debugfs_dir)
1831 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1832}
1833module_init(init);
1834module_exit(fini);
1835
1836MODULE_DEVICE_TABLE(virtio, id_table);
1837MODULE_DESCRIPTION("Virtio console driver");
1838MODULE_LICENSE("GPL");
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 44b8412a04e8..aa2e5d3eb01a 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -2414,6 +2414,7 @@ static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
2414 2414
2415 gsm->initiator = c->initiator; 2415 gsm->initiator = c->initiator;
2416 gsm->mru = c->mru; 2416 gsm->mru = c->mru;
2417 gsm->mtu = c->mtu;
2417 gsm->encoding = c->encapsulation; 2418 gsm->encoding = c->encapsulation;
2418 gsm->adaption = c->adaption; 2419 gsm->adaption = c->adaption;
2419 gsm->n2 = c->n2; 2420 gsm->n2 = c->n2;
diff --git a/drivers/tty/n_hdlc.c b/drivers/tty/n_hdlc.c
index 47d32281032c..52fc0c9a6364 100644
--- a/drivers/tty/n_hdlc.c
+++ b/drivers/tty/n_hdlc.c
@@ -581,8 +581,9 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
581 __u8 __user *buf, size_t nr) 581 __u8 __user *buf, size_t nr)
582{ 582{
583 struct n_hdlc *n_hdlc = tty2n_hdlc(tty); 583 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
584 int ret; 584 int ret = 0;
585 struct n_hdlc_buf *rbuf; 585 struct n_hdlc_buf *rbuf;
586 DECLARE_WAITQUEUE(wait, current);
586 587
587 if (debuglevel >= DEBUG_LEVEL_INFO) 588 if (debuglevel >= DEBUG_LEVEL_INFO)
588 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__); 589 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
@@ -598,57 +599,55 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
598 return -EFAULT; 599 return -EFAULT;
599 } 600 }
600 601
601 tty_lock(); 602 add_wait_queue(&tty->read_wait, &wait);
602 603
603 for (;;) { 604 for (;;) {
604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { 605 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
605 tty_unlock(); 606 ret = -EIO;
606 return -EIO; 607 break;
607 } 608 }
609 if (tty_hung_up_p(file))
610 break;
608 611
609 n_hdlc = tty2n_hdlc (tty); 612 set_current_state(TASK_INTERRUPTIBLE);
610 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
611 tty != n_hdlc->tty) {
612 tty_unlock();
613 return 0;
614 }
615 613
616 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); 614 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
617 if (rbuf) 615 if (rbuf) {
616 if (rbuf->count > nr) {
617 /* too large for caller's buffer */
618 ret = -EOVERFLOW;
619 } else {
620 if (copy_to_user(buf, rbuf->buf, rbuf->count))
621 ret = -EFAULT;
622 else
623 ret = rbuf->count;
624 }
625
626 if (n_hdlc->rx_free_buf_list.count >
627 DEFAULT_RX_BUF_COUNT)
628 kfree(rbuf);
629 else
630 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
618 break; 631 break;
632 }
619 633
620 /* no data */ 634 /* no data */
621 if (file->f_flags & O_NONBLOCK) { 635 if (file->f_flags & O_NONBLOCK) {
622 tty_unlock(); 636 ret = -EAGAIN;
623 return -EAGAIN; 637 break;
624 } 638 }
625 639
626 interruptible_sleep_on (&tty->read_wait); 640 schedule();
641
627 if (signal_pending(current)) { 642 if (signal_pending(current)) {
628 tty_unlock(); 643 ret = -EINTR;
629 return -EINTR; 644 break;
630 } 645 }
631 } 646 }
632 647
633 if (rbuf->count > nr) 648 remove_wait_queue(&tty->read_wait, &wait);
634 /* frame too large for caller's buffer (discard frame) */ 649 __set_current_state(TASK_RUNNING);
635 ret = -EOVERFLOW; 650
636 else {
637 /* Copy the data to the caller's buffer */
638 if (copy_to_user(buf, rbuf->buf, rbuf->count))
639 ret = -EFAULT;
640 else
641 ret = rbuf->count;
642 }
643
644 /* return HDLC buffer to free list unless the free list */
645 /* count has exceeded the default value, in which case the */
646 /* buffer is freed back to the OS to conserve memory */
647 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
648 kfree(rbuf);
649 else
650 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
651 tty_unlock();
652 return ret; 651 return ret;
653 652
654} /* end of n_hdlc_tty_read() */ 653} /* end of n_hdlc_tty_read() */
@@ -691,14 +690,15 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
691 count = maxframe; 690 count = maxframe;
692 } 691 }
693 692
694 tty_lock();
695
696 add_wait_queue(&tty->write_wait, &wait); 693 add_wait_queue(&tty->write_wait, &wait);
697 set_current_state(TASK_INTERRUPTIBLE); 694
695 for (;;) {
696 set_current_state(TASK_INTERRUPTIBLE);
698 697
699 /* Allocate transmit buffer */ 698 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
700 /* sleep until transmit buffer available */ 699 if (tbuf)
701 while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) { 700 break;
701
702 if (file->f_flags & O_NONBLOCK) { 702 if (file->f_flags & O_NONBLOCK) {
703 error = -EAGAIN; 703 error = -EAGAIN;
704 break; 704 break;
@@ -719,7 +719,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
719 } 719 }
720 } 720 }
721 721
722 set_current_state(TASK_RUNNING); 722 __set_current_state(TASK_RUNNING);
723 remove_wait_queue(&tty->write_wait, &wait); 723 remove_wait_queue(&tty->write_wait, &wait);
724 724
725 if (!error) { 725 if (!error) {
@@ -731,7 +731,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); 731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
732 n_hdlc_send_frames(n_hdlc,tty); 732 n_hdlc_send_frames(n_hdlc,tty);
733 } 733 }
734 tty_unlock(); 734
735 return error; 735 return error;
736 736
737} /* end of n_hdlc_tty_write() */ 737} /* end of n_hdlc_tty_write() */
diff --git a/drivers/tty/serial/68328serial.c b/drivers/tty/serial/68328serial.c
index be0ebce36e54..de0160e3f8c4 100644
--- a/drivers/tty/serial/68328serial.c
+++ b/drivers/tty/serial/68328serial.c
@@ -262,7 +262,7 @@ static void status_handle(struct m68k_serial *info, unsigned short status)
262 262
263static void receive_chars(struct m68k_serial *info, unsigned short rx) 263static void receive_chars(struct m68k_serial *info, unsigned short rx)
264{ 264{
265 struct tty_struct *tty = info->port.tty; 265 struct tty_struct *tty = info->tty;
266 m68328_uart *uart = &uart_addr[info->line]; 266 m68328_uart *uart = &uart_addr[info->line];
267 unsigned char ch, flag; 267 unsigned char ch, flag;
268 268
@@ -329,7 +329,7 @@ static void transmit_chars(struct m68k_serial *info)
329 goto clear_and_return; 329 goto clear_and_return;
330 } 330 }
331 331
332 if((info->xmit_cnt <= 0) || info->port.tty->stopped) { 332 if((info->xmit_cnt <= 0) || info->tty->stopped) {
333 /* That's peculiar... TX ints off */ 333 /* That's peculiar... TX ints off */
334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK; 334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335 goto clear_and_return; 335 goto clear_and_return;
@@ -383,7 +383,7 @@ static void do_softint(struct work_struct *work)
383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue); 383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
384 struct tty_struct *tty; 384 struct tty_struct *tty;
385 385
386 tty = info->port.tty; 386 tty = info->tty;
387 if (!tty) 387 if (!tty)
388 return; 388 return;
389#if 0 389#if 0
@@ -407,7 +407,7 @@ static void do_serial_hangup(struct work_struct *work)
407 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup); 407 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
408 struct tty_struct *tty; 408 struct tty_struct *tty;
409 409
410 tty = info->port.tty; 410 tty = info->tty;
411 if (!tty) 411 if (!tty)
412 return; 412 return;
413 413
@@ -451,8 +451,8 @@ static int startup(struct m68k_serial * info)
451 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK; 451 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
452#endif 452#endif
453 453
454 if (info->port.tty) 454 if (info->tty)
455 clear_bit(TTY_IO_ERROR, &info->port.tty->flags); 455 clear_bit(TTY_IO_ERROR, &info->tty->flags);
456 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 456 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
457 457
458 /* 458 /*
@@ -486,8 +486,8 @@ static void shutdown(struct m68k_serial * info)
486 info->xmit_buf = 0; 486 info->xmit_buf = 0;
487 } 487 }
488 488
489 if (info->port.tty) 489 if (info->tty)
490 set_bit(TTY_IO_ERROR, &info->port.tty->flags); 490 set_bit(TTY_IO_ERROR, &info->tty->flags);
491 491
492 info->flags &= ~S_INITIALIZED; 492 info->flags &= ~S_INITIALIZED;
493 local_irq_restore(flags); 493 local_irq_restore(flags);
@@ -553,9 +553,9 @@ static void change_speed(struct m68k_serial *info)
553 unsigned cflag; 553 unsigned cflag;
554 int i; 554 int i;
555 555
556 if (!info->port.tty || !info->port.tty->termios) 556 if (!info->tty || !info->tty->termios)
557 return; 557 return;
558 cflag = info->port.tty->termios->c_cflag; 558 cflag = info->tty->termios->c_cflag;
559 if (!(port = info->port)) 559 if (!(port = info->port))
560 return; 560 return;
561 561
@@ -970,7 +970,6 @@ static void send_break(struct m68k_serial * info, unsigned int duration)
970static int rs_ioctl(struct tty_struct *tty, struct file * file, 970static int rs_ioctl(struct tty_struct *tty, struct file * file,
971 unsigned int cmd, unsigned long arg) 971 unsigned int cmd, unsigned long arg)
972{ 972{
973 int error;
974 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; 973 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
975 int retval; 974 int retval;
976 975
@@ -1104,7 +1103,7 @@ static void rs_close(struct tty_struct *tty, struct file * filp)
1104 tty_ldisc_flush(tty); 1103 tty_ldisc_flush(tty);
1105 tty->closing = 0; 1104 tty->closing = 0;
1106 info->event = 0; 1105 info->event = 0;
1107 info->port.tty = NULL; 1106 info->tty = NULL;
1108#warning "This is not and has never been valid so fix it" 1107#warning "This is not and has never been valid so fix it"
1109#if 0 1108#if 0
1110 if (tty->ldisc.num != ldiscs[N_TTY].num) { 1109 if (tty->ldisc.num != ldiscs[N_TTY].num) {
@@ -1142,7 +1141,7 @@ void rs_hangup(struct tty_struct *tty)
1142 info->event = 0; 1141 info->event = 0;
1143 info->count = 0; 1142 info->count = 0;
1144 info->flags &= ~S_NORMAL_ACTIVE; 1143 info->flags &= ~S_NORMAL_ACTIVE;
1145 info->port.tty = NULL; 1144 info->tty = NULL;
1146 wake_up_interruptible(&info->open_wait); 1145 wake_up_interruptible(&info->open_wait);
1147} 1146}
1148 1147
@@ -1261,7 +1260,7 @@ int rs_open(struct tty_struct *tty, struct file * filp)
1261 1260
1262 info->count++; 1261 info->count++;
1263 tty->driver_data = info; 1262 tty->driver_data = info;
1264 info->port.tty = tty; 1263 info->tty = tty;
1265 1264
1266 /* 1265 /*
1267 * Start up serial port 1266 * Start up serial port
@@ -1338,7 +1337,7 @@ rs68328_init(void)
1338 info = &m68k_soft[i]; 1337 info = &m68k_soft[i];
1339 info->magic = SERIAL_MAGIC; 1338 info->magic = SERIAL_MAGIC;
1340 info->port = (int) &uart_addr[i]; 1339 info->port = (int) &uart_addr[i];
1341 info->port.tty = NULL; 1340 info->tty = NULL;
1342 info->irq = uart_irqs[i]; 1341 info->irq = uart_irqs[i];
1343 info->custom_divisor = 16; 1342 info->custom_divisor = 16;
1344 info->close_delay = 50; 1343 info->close_delay = 50;
diff --git a/drivers/tty/serial/68360serial.c b/drivers/tty/serial/68360serial.c
index 88b13356ec10..bc21eeae8fde 100644
--- a/drivers/tty/serial/68360serial.c
+++ b/drivers/tty/serial/68360serial.c
@@ -2428,6 +2428,7 @@ static const struct tty_operations rs_360_ops = {
2428 /* .read_proc = rs_360_read_proc, */ 2428 /* .read_proc = rs_360_read_proc, */
2429 .tiocmget = rs_360_tiocmget, 2429 .tiocmget = rs_360_tiocmget,
2430 .tiocmset = rs_360_tiocmset, 2430 .tiocmset = rs_360_tiocmset,
2431 .get_icount = rs_360_get_icount,
2431}; 2432};
2432 2433
2433static int __init rs_360_init(void) 2434static int __init rs_360_init(void)
diff --git a/drivers/tty/serial/8250.c b/drivers/tty/serial/8250.c
index b25e6e490530..3975df6f7fdb 100644
--- a/drivers/tty/serial/8250.c
+++ b/drivers/tty/serial/8250.c
@@ -236,7 +236,8 @@ static const struct serial8250_config uart_config[] = {
236 .fifo_size = 128, 236 .fifo_size = 128,
237 .tx_loadsz = 128, 237 .tx_loadsz = 128,
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
239 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 239 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
240 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
240 }, 241 },
241 [PORT_16654] = { 242 [PORT_16654] = {
242 .name = "ST16654", 243 .name = "ST16654",
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index b1682d7f1d8a..2b8334601c8b 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -1518,6 +1518,7 @@ config SERIAL_BCM63XX_CONSOLE
1518config SERIAL_GRLIB_GAISLER_APBUART 1518config SERIAL_GRLIB_GAISLER_APBUART
1519 tristate "GRLIB APBUART serial support" 1519 tristate "GRLIB APBUART serial support"
1520 depends on OF 1520 depends on OF
1521 select SERIAL_CORE
1521 ---help--- 1522 ---help---
1522 Add support for the GRLIB APBUART serial port. 1523 Add support for the GRLIB APBUART serial port.
1523 1524
diff --git a/drivers/tty/serial/amba-pl010.c b/drivers/tty/serial/amba-pl010.c
index 2904aa044126..d742dd2c525c 100644
--- a/drivers/tty/serial/amba-pl010.c
+++ b/drivers/tty/serial/amba-pl010.c
@@ -676,7 +676,7 @@ static struct uart_driver amba_reg = {
676 .cons = AMBA_CONSOLE, 676 .cons = AMBA_CONSOLE,
677}; 677};
678 678
679static int pl010_probe(struct amba_device *dev, struct amba_id *id) 679static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
680{ 680{
681 struct uart_amba_port *uap; 681 struct uart_amba_port *uap;
682 void __iomem *base; 682 void __iomem *base;
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
index e76d7d000128..57731e870085 100644
--- a/drivers/tty/serial/amba-pl011.c
+++ b/drivers/tty/serial/amba-pl011.c
@@ -96,6 +96,22 @@ static struct vendor_data vendor_st = {
96}; 96};
97 97
98/* Deals with DMA transactions */ 98/* Deals with DMA transactions */
99
100struct pl011_sgbuf {
101 struct scatterlist sg;
102 char *buf;
103};
104
105struct pl011_dmarx_data {
106 struct dma_chan *chan;
107 struct completion complete;
108 bool use_buf_b;
109 struct pl011_sgbuf sgbuf_a;
110 struct pl011_sgbuf sgbuf_b;
111 dma_cookie_t cookie;
112 bool running;
113};
114
99struct pl011_dmatx_data { 115struct pl011_dmatx_data {
100 struct dma_chan *chan; 116 struct dma_chan *chan;
101 struct scatterlist sg; 117 struct scatterlist sg;
@@ -120,12 +136,70 @@ struct uart_amba_port {
120 char type[12]; 136 char type[12];
121#ifdef CONFIG_DMA_ENGINE 137#ifdef CONFIG_DMA_ENGINE
122 /* DMA stuff */ 138 /* DMA stuff */
123 bool using_dma; 139 bool using_tx_dma;
140 bool using_rx_dma;
141 struct pl011_dmarx_data dmarx;
124 struct pl011_dmatx_data dmatx; 142 struct pl011_dmatx_data dmatx;
125#endif 143#endif
126}; 144};
127 145
128/* 146/*
147 * Reads up to 256 characters from the FIFO or until it's empty and
148 * inserts them into the TTY layer. Returns the number of characters
149 * read from the FIFO.
150 */
151static int pl011_fifo_to_tty(struct uart_amba_port *uap)
152{
153 u16 status, ch;
154 unsigned int flag, max_count = 256;
155 int fifotaken = 0;
156
157 while (max_count--) {
158 status = readw(uap->port.membase + UART01x_FR);
159 if (status & UART01x_FR_RXFE)
160 break;
161
162 /* Take chars from the FIFO and update status */
163 ch = readw(uap->port.membase + UART01x_DR) |
164 UART_DUMMY_DR_RX;
165 flag = TTY_NORMAL;
166 uap->port.icount.rx++;
167 fifotaken++;
168
169 if (unlikely(ch & UART_DR_ERROR)) {
170 if (ch & UART011_DR_BE) {
171 ch &= ~(UART011_DR_FE | UART011_DR_PE);
172 uap->port.icount.brk++;
173 if (uart_handle_break(&uap->port))
174 continue;
175 } else if (ch & UART011_DR_PE)
176 uap->port.icount.parity++;
177 else if (ch & UART011_DR_FE)
178 uap->port.icount.frame++;
179 if (ch & UART011_DR_OE)
180 uap->port.icount.overrun++;
181
182 ch &= uap->port.read_status_mask;
183
184 if (ch & UART011_DR_BE)
185 flag = TTY_BREAK;
186 else if (ch & UART011_DR_PE)
187 flag = TTY_PARITY;
188 else if (ch & UART011_DR_FE)
189 flag = TTY_FRAME;
190 }
191
192 if (uart_handle_sysrq_char(&uap->port, ch & 255))
193 continue;
194
195 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
196 }
197
198 return fifotaken;
199}
200
201
202/*
129 * All the DMA operation mode stuff goes inside this ifdef. 203 * All the DMA operation mode stuff goes inside this ifdef.
130 * This assumes that you have a generic DMA device interface, 204 * This assumes that you have a generic DMA device interface,
131 * no custom DMA interfaces are supported. 205 * no custom DMA interfaces are supported.
@@ -134,6 +208,31 @@ struct uart_amba_port {
134 208
135#define PL011_DMA_BUFFER_SIZE PAGE_SIZE 209#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
136 210
211static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
212 enum dma_data_direction dir)
213{
214 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
215 if (!sg->buf)
216 return -ENOMEM;
217
218 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
219
220 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
221 kfree(sg->buf);
222 return -EINVAL;
223 }
224 return 0;
225}
226
227static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
228 enum dma_data_direction dir)
229{
230 if (sg->buf) {
231 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
232 kfree(sg->buf);
233 }
234}
235
137static void pl011_dma_probe_initcall(struct uart_amba_port *uap) 236static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
138{ 237{
139 /* DMA is the sole user of the platform data right now */ 238 /* DMA is the sole user of the platform data right now */
@@ -153,7 +252,7 @@ static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
153 return; 252 return;
154 } 253 }
155 254
156 /* Try to acquire a generic DMA engine slave channel */ 255 /* Try to acquire a generic DMA engine slave TX channel */
157 dma_cap_zero(mask); 256 dma_cap_zero(mask);
158 dma_cap_set(DMA_SLAVE, mask); 257 dma_cap_set(DMA_SLAVE, mask);
159 258
@@ -168,6 +267,28 @@ static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
168 267
169 dev_info(uap->port.dev, "DMA channel TX %s\n", 268 dev_info(uap->port.dev, "DMA channel TX %s\n",
170 dma_chan_name(uap->dmatx.chan)); 269 dma_chan_name(uap->dmatx.chan));
270
271 /* Optionally make use of an RX channel as well */
272 if (plat->dma_rx_param) {
273 struct dma_slave_config rx_conf = {
274 .src_addr = uap->port.mapbase + UART01x_DR,
275 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
276 .direction = DMA_FROM_DEVICE,
277 .src_maxburst = uap->fifosize >> 1,
278 };
279
280 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
281 if (!chan) {
282 dev_err(uap->port.dev, "no RX DMA channel!\n");
283 return;
284 }
285
286 dmaengine_slave_config(chan, &rx_conf);
287 uap->dmarx.chan = chan;
288
289 dev_info(uap->port.dev, "DMA channel RX %s\n",
290 dma_chan_name(uap->dmarx.chan));
291 }
171} 292}
172 293
173#ifndef MODULE 294#ifndef MODULE
@@ -219,9 +340,10 @@ static void pl011_dma_remove(struct uart_amba_port *uap)
219 /* TODO: remove the initcall if it has not yet executed */ 340 /* TODO: remove the initcall if it has not yet executed */
220 if (uap->dmatx.chan) 341 if (uap->dmatx.chan)
221 dma_release_channel(uap->dmatx.chan); 342 dma_release_channel(uap->dmatx.chan);
343 if (uap->dmarx.chan)
344 dma_release_channel(uap->dmarx.chan);
222} 345}
223 346
224
225/* Forward declare this for the refill routine */ 347/* Forward declare this for the refill routine */
226static int pl011_dma_tx_refill(struct uart_amba_port *uap); 348static int pl011_dma_tx_refill(struct uart_amba_port *uap);
227 349
@@ -380,7 +502,7 @@ static int pl011_dma_tx_refill(struct uart_amba_port *uap)
380 */ 502 */
381static bool pl011_dma_tx_irq(struct uart_amba_port *uap) 503static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
382{ 504{
383 if (!uap->using_dma) 505 if (!uap->using_tx_dma)
384 return false; 506 return false;
385 507
386 /* 508 /*
@@ -432,7 +554,7 @@ static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
432{ 554{
433 u16 dmacr; 555 u16 dmacr;
434 556
435 if (!uap->using_dma) 557 if (!uap->using_tx_dma)
436 return false; 558 return false;
437 559
438 if (!uap->port.x_char) { 560 if (!uap->port.x_char) {
@@ -492,7 +614,7 @@ static void pl011_dma_flush_buffer(struct uart_port *port)
492{ 614{
493 struct uart_amba_port *uap = (struct uart_amba_port *)port; 615 struct uart_amba_port *uap = (struct uart_amba_port *)port;
494 616
495 if (!uap->using_dma) 617 if (!uap->using_tx_dma)
496 return; 618 return;
497 619
498 /* Avoid deadlock with the DMA engine callback */ 620 /* Avoid deadlock with the DMA engine callback */
@@ -508,9 +630,219 @@ static void pl011_dma_flush_buffer(struct uart_port *port)
508 } 630 }
509} 631}
510 632
633static void pl011_dma_rx_callback(void *data);
634
635static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
636{
637 struct dma_chan *rxchan = uap->dmarx.chan;
638 struct dma_device *dma_dev;
639 struct pl011_dmarx_data *dmarx = &uap->dmarx;
640 struct dma_async_tx_descriptor *desc;
641 struct pl011_sgbuf *sgbuf;
642
643 if (!rxchan)
644 return -EIO;
645
646 /* Start the RX DMA job */
647 sgbuf = uap->dmarx.use_buf_b ?
648 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
649 dma_dev = rxchan->device;
650 desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
651 DMA_FROM_DEVICE,
652 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
653 /*
654 * If the DMA engine is busy and cannot prepare a
655 * channel, no big deal, the driver will fall back
656 * to interrupt mode as a result of this error code.
657 */
658 if (!desc) {
659 uap->dmarx.running = false;
660 dmaengine_terminate_all(rxchan);
661 return -EBUSY;
662 }
663
664 /* Some data to go along to the callback */
665 desc->callback = pl011_dma_rx_callback;
666 desc->callback_param = uap;
667 dmarx->cookie = dmaengine_submit(desc);
668 dma_async_issue_pending(rxchan);
669
670 uap->dmacr |= UART011_RXDMAE;
671 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
672 uap->dmarx.running = true;
673
674 uap->im &= ~UART011_RXIM;
675 writew(uap->im, uap->port.membase + UART011_IMSC);
676
677 return 0;
678}
679
680/*
681 * This is called when either the DMA job is complete, or
682 * the FIFO timeout interrupt occurred. This must be called
683 * with the port spinlock uap->port.lock held.
684 */
685static void pl011_dma_rx_chars(struct uart_amba_port *uap,
686 u32 pending, bool use_buf_b,
687 bool readfifo)
688{
689 struct tty_struct *tty = uap->port.state->port.tty;
690 struct pl011_sgbuf *sgbuf = use_buf_b ?
691 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
692 struct device *dev = uap->dmarx.chan->device->dev;
693 int dma_count = 0;
694 u32 fifotaken = 0; /* only used for vdbg() */
695
696 /* Pick everything from the DMA first */
697 if (pending) {
698 /* Sync in buffer */
699 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
700
701 /*
702 * First take all chars in the DMA pipe, then look in the FIFO.
703 * Note that tty_insert_flip_buf() tries to take as many chars
704 * as it can.
705 */
706 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
707 sgbuf->buf, pending);
708
709 /* Return buffer to device */
710 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
711
712 uap->port.icount.rx += dma_count;
713 if (dma_count < pending)
714 dev_warn(uap->port.dev,
715 "couldn't insert all characters (TTY is full?)\n");
716 }
717
718 /*
719 * Only continue with trying to read the FIFO if all DMA chars have
720 * been taken first.
721 */
722 if (dma_count == pending && readfifo) {
723 /* Clear any error flags */
724 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
725 uap->port.membase + UART011_ICR);
726
727 /*
728 * If we read all the DMA'd characters, and we had an
729 * incomplete buffer, that could be due to an rx error, or
730 * maybe we just timed out. Read any pending chars and check
731 * the error status.
732 *
733 * Error conditions will only occur in the FIFO, these will
734 * trigger an immediate interrupt and stop the DMA job, so we
735 * will always find the error in the FIFO, never in the DMA
736 * buffer.
737 */
738 fifotaken = pl011_fifo_to_tty(uap);
739 }
740
741 spin_unlock(&uap->port.lock);
742 dev_vdbg(uap->port.dev,
743 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
744 dma_count, fifotaken);
745 tty_flip_buffer_push(tty);
746 spin_lock(&uap->port.lock);
747}
748
749static void pl011_dma_rx_irq(struct uart_amba_port *uap)
750{
751 struct pl011_dmarx_data *dmarx = &uap->dmarx;
752 struct dma_chan *rxchan = dmarx->chan;
753 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
754 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
755 size_t pending;
756 struct dma_tx_state state;
757 enum dma_status dmastat;
758
759 /*
760 * Pause the transfer so we can trust the current counter,
761 * do this before we pause the PL011 block, else we may
762 * overflow the FIFO.
763 */
764 if (dmaengine_pause(rxchan))
765 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
766 dmastat = rxchan->device->device_tx_status(rxchan,
767 dmarx->cookie, &state);
768 if (dmastat != DMA_PAUSED)
769 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
770
771 /* Disable RX DMA - incoming data will wait in the FIFO */
772 uap->dmacr &= ~UART011_RXDMAE;
773 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
774 uap->dmarx.running = false;
775
776 pending = sgbuf->sg.length - state.residue;
777 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
778 /* Then we terminate the transfer - we now know our residue */
779 dmaengine_terminate_all(rxchan);
780
781 /*
782 * This will take the chars we have so far and insert
783 * into the framework.
784 */
785 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
786
787 /* Switch buffer & re-trigger DMA job */
788 dmarx->use_buf_b = !dmarx->use_buf_b;
789 if (pl011_dma_rx_trigger_dma(uap)) {
790 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
791 "fall back to interrupt mode\n");
792 uap->im |= UART011_RXIM;
793 writew(uap->im, uap->port.membase + UART011_IMSC);
794 }
795}
796
797static void pl011_dma_rx_callback(void *data)
798{
799 struct uart_amba_port *uap = data;
800 struct pl011_dmarx_data *dmarx = &uap->dmarx;
801 bool lastbuf = dmarx->use_buf_b;
802 int ret;
803
804 /*
805 * This completion interrupt occurs typically when the
806 * RX buffer is totally stuffed but no timeout has yet
807 * occurred. When that happens, we just want the RX
808 * routine to flush out the secondary DMA buffer while
809 * we immediately trigger the next DMA job.
810 */
811 spin_lock_irq(&uap->port.lock);
812 uap->dmarx.running = false;
813 dmarx->use_buf_b = !lastbuf;
814 ret = pl011_dma_rx_trigger_dma(uap);
815
816 pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
817 spin_unlock_irq(&uap->port.lock);
818 /*
819 * Do this check after we picked the DMA chars so we don't
820 * get some IRQ immediately from RX.
821 */
822 if (ret) {
823 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
824 "fall back to interrupt mode\n");
825 uap->im |= UART011_RXIM;
826 writew(uap->im, uap->port.membase + UART011_IMSC);
827 }
828}
829
830/*
831 * Stop accepting received characters, when we're shutting down or
832 * suspending this port.
833 * Locking: called with port lock held and IRQs disabled.
834 */
835static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
836{
837 /* FIXME. Just disable the DMA enable */
838 uap->dmacr &= ~UART011_RXDMAE;
839 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
840}
511 841
512static void pl011_dma_startup(struct uart_amba_port *uap) 842static void pl011_dma_startup(struct uart_amba_port *uap)
513{ 843{
844 int ret;
845
514 if (!uap->dmatx.chan) 846 if (!uap->dmatx.chan)
515 return; 847 return;
516 848
@@ -525,8 +857,33 @@ static void pl011_dma_startup(struct uart_amba_port *uap)
525 857
526 /* The DMA buffer is now the FIFO the TTY subsystem can use */ 858 /* The DMA buffer is now the FIFO the TTY subsystem can use */
527 uap->port.fifosize = PL011_DMA_BUFFER_SIZE; 859 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
528 uap->using_dma = true; 860 uap->using_tx_dma = true;
861
862 if (!uap->dmarx.chan)
863 goto skip_rx;
864
865 /* Allocate and map DMA RX buffers */
866 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
867 DMA_FROM_DEVICE);
868 if (ret) {
869 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
870 "RX buffer A", ret);
871 goto skip_rx;
872 }
873
874 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
875 DMA_FROM_DEVICE);
876 if (ret) {
877 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
878 "RX buffer B", ret);
879 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
880 DMA_FROM_DEVICE);
881 goto skip_rx;
882 }
529 883
884 uap->using_rx_dma = true;
885
886skip_rx:
530 /* Turn on DMA error (RX/TX will be enabled on demand) */ 887 /* Turn on DMA error (RX/TX will be enabled on demand) */
531 uap->dmacr |= UART011_DMAONERR; 888 uap->dmacr |= UART011_DMAONERR;
532 writew(uap->dmacr, uap->port.membase + UART011_DMACR); 889 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
@@ -539,11 +896,17 @@ static void pl011_dma_startup(struct uart_amba_port *uap)
539 if (uap->vendor->dma_threshold) 896 if (uap->vendor->dma_threshold)
540 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16, 897 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
541 uap->port.membase + ST_UART011_DMAWM); 898 uap->port.membase + ST_UART011_DMAWM);
899
900 if (uap->using_rx_dma) {
901 if (pl011_dma_rx_trigger_dma(uap))
902 dev_dbg(uap->port.dev, "could not trigger initial "
903 "RX DMA job, fall back to interrupt mode\n");
904 }
542} 905}
543 906
544static void pl011_dma_shutdown(struct uart_amba_port *uap) 907static void pl011_dma_shutdown(struct uart_amba_port *uap)
545{ 908{
546 if (!uap->using_dma) 909 if (!(uap->using_tx_dma || uap->using_rx_dma))
547 return; 910 return;
548 911
549 /* Disable RX and TX DMA */ 912 /* Disable RX and TX DMA */
@@ -555,19 +918,39 @@ static void pl011_dma_shutdown(struct uart_amba_port *uap)
555 writew(uap->dmacr, uap->port.membase + UART011_DMACR); 918 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
556 spin_unlock_irq(&uap->port.lock); 919 spin_unlock_irq(&uap->port.lock);
557 920
558 /* In theory, this should already be done by pl011_dma_flush_buffer */ 921 if (uap->using_tx_dma) {
559 dmaengine_terminate_all(uap->dmatx.chan); 922 /* In theory, this should already be done by pl011_dma_flush_buffer */
560 if (uap->dmatx.queued) { 923 dmaengine_terminate_all(uap->dmatx.chan);
561 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1, 924 if (uap->dmatx.queued) {
562 DMA_TO_DEVICE); 925 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
563 uap->dmatx.queued = false; 926 DMA_TO_DEVICE);
927 uap->dmatx.queued = false;
928 }
929
930 kfree(uap->dmatx.buf);
931 uap->using_tx_dma = false;
564 } 932 }
565 933
566 kfree(uap->dmatx.buf); 934 if (uap->using_rx_dma) {
935 dmaengine_terminate_all(uap->dmarx.chan);
936 /* Clean up the RX DMA */
937 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
938 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
939 uap->using_rx_dma = false;
940 }
941}
567 942
568 uap->using_dma = false; 943static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
944{
945 return uap->using_rx_dma;
569} 946}
570 947
948static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
949{
950 return uap->using_rx_dma && uap->dmarx.running;
951}
952
953
571#else 954#else
572/* Blank functions if the DMA engine is not available */ 955/* Blank functions if the DMA engine is not available */
573static inline void pl011_dma_probe(struct uart_amba_port *uap) 956static inline void pl011_dma_probe(struct uart_amba_port *uap)
@@ -600,6 +983,29 @@ static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
600 return false; 983 return false;
601} 984}
602 985
986static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
987{
988}
989
990static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
991{
992}
993
994static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
995{
996 return -EIO;
997}
998
999static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1000{
1001 return false;
1002}
1003
1004static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1005{
1006 return false;
1007}
1008
603#define pl011_dma_flush_buffer NULL 1009#define pl011_dma_flush_buffer NULL
604#endif 1010#endif
605 1011
@@ -630,6 +1036,8 @@ static void pl011_stop_rx(struct uart_port *port)
630 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM| 1036 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
631 UART011_PEIM|UART011_BEIM|UART011_OEIM); 1037 UART011_PEIM|UART011_BEIM|UART011_OEIM);
632 writew(uap->im, uap->port.membase + UART011_IMSC); 1038 writew(uap->im, uap->port.membase + UART011_IMSC);
1039
1040 pl011_dma_rx_stop(uap);
633} 1041}
634 1042
635static void pl011_enable_ms(struct uart_port *port) 1043static void pl011_enable_ms(struct uart_port *port)
@@ -643,51 +1051,24 @@ static void pl011_enable_ms(struct uart_port *port)
643static void pl011_rx_chars(struct uart_amba_port *uap) 1051static void pl011_rx_chars(struct uart_amba_port *uap)
644{ 1052{
645 struct tty_struct *tty = uap->port.state->port.tty; 1053 struct tty_struct *tty = uap->port.state->port.tty;
646 unsigned int status, ch, flag, max_count = 256;
647
648 status = readw(uap->port.membase + UART01x_FR);
649 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
650 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
651 flag = TTY_NORMAL;
652 uap->port.icount.rx++;
653
654 /*
655 * Note that the error handling code is
656 * out of the main execution path
657 */
658 if (unlikely(ch & UART_DR_ERROR)) {
659 if (ch & UART011_DR_BE) {
660 ch &= ~(UART011_DR_FE | UART011_DR_PE);
661 uap->port.icount.brk++;
662 if (uart_handle_break(&uap->port))
663 goto ignore_char;
664 } else if (ch & UART011_DR_PE)
665 uap->port.icount.parity++;
666 else if (ch & UART011_DR_FE)
667 uap->port.icount.frame++;
668 if (ch & UART011_DR_OE)
669 uap->port.icount.overrun++;
670
671 ch &= uap->port.read_status_mask;
672
673 if (ch & UART011_DR_BE)
674 flag = TTY_BREAK;
675 else if (ch & UART011_DR_PE)
676 flag = TTY_PARITY;
677 else if (ch & UART011_DR_FE)
678 flag = TTY_FRAME;
679 }
680 1054
681 if (uart_handle_sysrq_char(&uap->port, ch & 255)) 1055 pl011_fifo_to_tty(uap);
682 goto ignore_char;
683
684 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
685 1056
686 ignore_char:
687 status = readw(uap->port.membase + UART01x_FR);
688 }
689 spin_unlock(&uap->port.lock); 1057 spin_unlock(&uap->port.lock);
690 tty_flip_buffer_push(tty); 1058 tty_flip_buffer_push(tty);
1059 /*
1060 * If we were temporarily out of DMA mode for a while,
1061 * attempt to switch back to DMA mode again.
1062 */
1063 if (pl011_dma_rx_available(uap)) {
1064 if (pl011_dma_rx_trigger_dma(uap)) {
1065 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1066 "fall back to interrupt mode again\n");
1067 uap->im |= UART011_RXIM;
1068 } else
1069 uap->im &= ~UART011_RXIM;
1070 writew(uap->im, uap->port.membase + UART011_IMSC);
1071 }
691 spin_lock(&uap->port.lock); 1072 spin_lock(&uap->port.lock);
692} 1073}
693 1074
@@ -767,8 +1148,12 @@ static irqreturn_t pl011_int(int irq, void *dev_id)
767 UART011_RXIS), 1148 UART011_RXIS),
768 uap->port.membase + UART011_ICR); 1149 uap->port.membase + UART011_ICR);
769 1150
770 if (status & (UART011_RTIS|UART011_RXIS)) 1151 if (status & (UART011_RTIS|UART011_RXIS)) {
771 pl011_rx_chars(uap); 1152 if (pl011_dma_rx_running(uap))
1153 pl011_dma_rx_irq(uap);
1154 else
1155 pl011_rx_chars(uap);
1156 }
772 if (status & (UART011_DSRMIS|UART011_DCDMIS| 1157 if (status & (UART011_DSRMIS|UART011_DCDMIS|
773 UART011_CTSMIS|UART011_RIMIS)) 1158 UART011_CTSMIS|UART011_RIMIS))
774 pl011_modem_status(uap); 1159 pl011_modem_status(uap);
@@ -945,10 +1330,14 @@ static int pl011_startup(struct uart_port *port)
945 pl011_dma_startup(uap); 1330 pl011_dma_startup(uap);
946 1331
947 /* 1332 /*
948 * Finally, enable interrupts 1333 * Finally, enable interrupts, only timeouts when using DMA
1334 * if initial RX DMA job failed, start in interrupt mode
1335 * as well.
949 */ 1336 */
950 spin_lock_irq(&uap->port.lock); 1337 spin_lock_irq(&uap->port.lock);
951 uap->im = UART011_RXIM | UART011_RTIM; 1338 uap->im = UART011_RTIM;
1339 if (!pl011_dma_rx_running(uap))
1340 uap->im |= UART011_RXIM;
952 writew(uap->im, uap->port.membase + UART011_IMSC); 1341 writew(uap->im, uap->port.membase + UART011_IMSC);
953 spin_unlock_irq(&uap->port.lock); 1342 spin_unlock_irq(&uap->port.lock);
954 1343
@@ -1349,7 +1738,7 @@ static struct uart_driver amba_reg = {
1349 .cons = AMBA_CONSOLE, 1738 .cons = AMBA_CONSOLE,
1350}; 1739};
1351 1740
1352static int pl011_probe(struct amba_device *dev, struct amba_id *id) 1741static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
1353{ 1742{
1354 struct uart_amba_port *uap; 1743 struct uart_amba_port *uap;
1355 struct vendor_data *vendor = id->data; 1744 struct vendor_data *vendor = id->data;
diff --git a/drivers/tty/serial/bfin_5xx.c b/drivers/tty/serial/bfin_5xx.c
index e381b895b04d..9b1ff2b6bb37 100644
--- a/drivers/tty/serial/bfin_5xx.c
+++ b/drivers/tty/serial/bfin_5xx.c
@@ -370,10 +370,8 @@ static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
370{ 370{
371 struct bfin_serial_port *uart = dev_id; 371 struct bfin_serial_port *uart = dev_id;
372 372
373 spin_lock(&uart->port.lock);
374 while (UART_GET_LSR(uart) & DR) 373 while (UART_GET_LSR(uart) & DR)
375 bfin_serial_rx_chars(uart); 374 bfin_serial_rx_chars(uart);
376 spin_unlock(&uart->port.lock);
377 375
378 return IRQ_HANDLED; 376 return IRQ_HANDLED;
379} 377}
@@ -490,9 +488,8 @@ void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
490{ 488{
491 int x_pos, pos; 489 int x_pos, pos;
492 490
493 dma_disable_irq(uart->tx_dma_channel); 491 dma_disable_irq_nosync(uart->rx_dma_channel);
494 dma_disable_irq(uart->rx_dma_channel); 492 spin_lock_bh(&uart->rx_lock);
495 spin_lock_bh(&uart->port.lock);
496 493
497 /* 2D DMA RX buffer ring is used. Because curr_y_count and 494 /* 2D DMA RX buffer ring is used. Because curr_y_count and
498 * curr_x_count can't be read as an atomic operation, 495 * curr_x_count can't be read as an atomic operation,
@@ -523,8 +520,7 @@ void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
523 uart->rx_dma_buf.tail = uart->rx_dma_buf.head; 520 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
524 } 521 }
525 522
526 spin_unlock_bh(&uart->port.lock); 523 spin_unlock_bh(&uart->rx_lock);
527 dma_enable_irq(uart->tx_dma_channel);
528 dma_enable_irq(uart->rx_dma_channel); 524 dma_enable_irq(uart->rx_dma_channel);
529 525
530 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES); 526 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
@@ -571,7 +567,7 @@ static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
571 unsigned short irqstat; 567 unsigned short irqstat;
572 int x_pos, pos; 568 int x_pos, pos;
573 569
574 spin_lock(&uart->port.lock); 570 spin_lock(&uart->rx_lock);
575 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel); 571 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
576 clear_dma_irqstat(uart->rx_dma_channel); 572 clear_dma_irqstat(uart->rx_dma_channel);
577 573
@@ -589,7 +585,7 @@ static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
589 uart->rx_dma_buf.tail = uart->rx_dma_buf.head; 585 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
590 } 586 }
591 587
592 spin_unlock(&uart->port.lock); 588 spin_unlock(&uart->rx_lock);
593 589
594 return IRQ_HANDLED; 590 return IRQ_HANDLED;
595} 591}
@@ -1332,6 +1328,7 @@ static int bfin_serial_probe(struct platform_device *pdev)
1332 } 1328 }
1333 1329
1334#ifdef CONFIG_SERIAL_BFIN_DMA 1330#ifdef CONFIG_SERIAL_BFIN_DMA
1331 spin_lock_init(&uart->rx_lock);
1335 uart->tx_done = 1; 1332 uart->tx_done = 1;
1336 uart->tx_count = 0; 1333 uart->tx_count = 0;
1337 1334
diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
index beb1afa27d8d..7b951adac54b 100644
--- a/drivers/tty/serial/max3100.c
+++ b/drivers/tty/serial/max3100.c
@@ -601,7 +601,7 @@ static int max3100_startup(struct uart_port *port)
601 s->rts = 0; 601 s->rts = 0;
602 602
603 sprintf(b, "max3100-%d", s->minor); 603 sprintf(b, "max3100-%d", s->minor);
604 s->workqueue = create_freezeable_workqueue(b); 604 s->workqueue = create_freezable_workqueue(b);
605 if (!s->workqueue) { 605 if (!s->workqueue) {
606 dev_warn(&s->spi->dev, "cannot create workqueue\n"); 606 dev_warn(&s->spi->dev, "cannot create workqueue\n");
607 return -EBUSY; 607 return -EBUSY;
diff --git a/drivers/tty/serial/max3107.c b/drivers/tty/serial/max3107.c
index 910870edf708..750b4f627315 100644
--- a/drivers/tty/serial/max3107.c
+++ b/drivers/tty/serial/max3107.c
@@ -833,7 +833,7 @@ static int max3107_startup(struct uart_port *port)
833 struct max3107_port *s = container_of(port, struct max3107_port, port); 833 struct max3107_port *s = container_of(port, struct max3107_port, port);
834 834
835 /* Initialize work queue */ 835 /* Initialize work queue */
836 s->workqueue = create_freezeable_workqueue("max3107"); 836 s->workqueue = create_freezable_workqueue("max3107");
837 if (!s->workqueue) { 837 if (!s->workqueue) {
838 dev_err(&s->spi->dev, "Workqueue creation failed\n"); 838 dev_err(&s->spi->dev, "Workqueue creation failed\n");
839 return -EBUSY; 839 return -EBUSY;
diff --git a/drivers/tty/serial/sb1250-duart.c b/drivers/tty/serial/sb1250-duart.c
index a2f2b3254499..602d9845c52f 100644
--- a/drivers/tty/serial/sb1250-duart.c
+++ b/drivers/tty/serial/sb1250-duart.c
@@ -829,7 +829,7 @@ static void __init sbd_probe_duarts(void)
829#ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE 829#ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
830/* 830/*
831 * Serial console stuff. Very basic, polling driver for doing serial 831 * Serial console stuff. Very basic, polling driver for doing serial
832 * console output. The console_sem is held by the caller, so we 832 * console output. The console_lock is held by the caller, so we
833 * shouldn't be interrupted for more console activity. 833 * shouldn't be interrupted for more console activity.
834 */ 834 */
835static void sbd_console_putchar(struct uart_port *uport, int ch) 835static void sbd_console_putchar(struct uart_port *uport, int ch)
diff --git a/drivers/tty/serial/serial_cs.c b/drivers/tty/serial/serial_cs.c
index 93760b2ea172..1ef4df9bf7e4 100644
--- a/drivers/tty/serial/serial_cs.c
+++ b/drivers/tty/serial/serial_cs.c
@@ -712,6 +712,7 @@ static struct pcmcia_device_id serial_ids[] = {
712 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Xircom", "CreditCard Ethernet+Modem II", 0x2e3ee845, 0xeca401bf), 712 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Xircom", "CreditCard Ethernet+Modem II", 0x2e3ee845, 0xeca401bf),
713 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0e01), 713 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0e01),
714 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0a05), 714 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0a05),
715 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0b05),
715 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x1101), 716 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x1101),
716 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0104, 0x0070), 717 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0104, 0x0070),
717 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0101, 0x0562), 718 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0101, 0x0562),
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index c556ed9db13d..81f13958e751 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -46,7 +46,7 @@
46#include <asm/irq_regs.h> 46#include <asm/irq_regs.h>
47 47
48/* Whether we react on sysrq keys or just ignore them */ 48/* Whether we react on sysrq keys or just ignore them */
49static int __read_mostly sysrq_enabled = 1; 49static int __read_mostly sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
50static bool __read_mostly sysrq_always_enabled; 50static bool __read_mostly sysrq_always_enabled;
51 51
52static bool sysrq_on(void) 52static bool sysrq_on(void)
@@ -571,6 +571,7 @@ struct sysrq_state {
571 unsigned int alt_use; 571 unsigned int alt_use;
572 bool active; 572 bool active;
573 bool need_reinject; 573 bool need_reinject;
574 bool reinjecting;
574}; 575};
575 576
576static void sysrq_reinject_alt_sysrq(struct work_struct *work) 577static void sysrq_reinject_alt_sysrq(struct work_struct *work)
@@ -581,6 +582,10 @@ static void sysrq_reinject_alt_sysrq(struct work_struct *work)
581 unsigned int alt_code = sysrq->alt_use; 582 unsigned int alt_code = sysrq->alt_use;
582 583
583 if (sysrq->need_reinject) { 584 if (sysrq->need_reinject) {
585 /* we do not want the assignment to be reordered */
586 sysrq->reinjecting = true;
587 mb();
588
584 /* Simulate press and release of Alt + SysRq */ 589 /* Simulate press and release of Alt + SysRq */
585 input_inject_event(handle, EV_KEY, alt_code, 1); 590 input_inject_event(handle, EV_KEY, alt_code, 1);
586 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1); 591 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
@@ -589,6 +594,9 @@ static void sysrq_reinject_alt_sysrq(struct work_struct *work)
589 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0); 594 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
590 input_inject_event(handle, EV_KEY, alt_code, 0); 595 input_inject_event(handle, EV_KEY, alt_code, 0);
591 input_inject_event(handle, EV_SYN, SYN_REPORT, 1); 596 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
597
598 mb();
599 sysrq->reinjecting = false;
592 } 600 }
593} 601}
594 602
@@ -599,6 +607,13 @@ static bool sysrq_filter(struct input_handle *handle,
599 bool was_active = sysrq->active; 607 bool was_active = sysrq->active;
600 bool suppress; 608 bool suppress;
601 609
610 /*
611 * Do not filter anything if we are in the process of re-injecting
612 * Alt+SysRq combination.
613 */
614 if (sysrq->reinjecting)
615 return false;
616
602 switch (type) { 617 switch (type) {
603 618
604 case EV_SYN: 619 case EV_SYN:
@@ -629,7 +644,7 @@ static bool sysrq_filter(struct input_handle *handle,
629 sysrq->alt_use = sysrq->alt; 644 sysrq->alt_use = sysrq->alt;
630 /* 645 /*
631 * If nothing else will be pressed we'll need 646 * If nothing else will be pressed we'll need
632 * to * re-inject Alt-SysRq keysroke. 647 * to re-inject Alt-SysRq keysroke.
633 */ 648 */
634 sysrq->need_reinject = true; 649 sysrq->need_reinject = true;
635 } 650 }
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 464d09d97873..0065da4b11c1 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3256,8 +3256,8 @@ static ssize_t show_cons_active(struct device *dev,
3256 struct console *c; 3256 struct console *c;
3257 ssize_t count = 0; 3257 ssize_t count = 0;
3258 3258
3259 acquire_console_sem(); 3259 console_lock();
3260 for (c = console_drivers; c; c = c->next) { 3260 for_each_console(c) {
3261 if (!c->device) 3261 if (!c->device)
3262 continue; 3262 continue;
3263 if (!c->write) 3263 if (!c->write)
@@ -3271,7 +3271,7 @@ static ssize_t show_cons_active(struct device *dev,
3271 while (i--) 3271 while (i--)
3272 count += sprintf(buf + count, "%s%d%c", 3272 count += sprintf(buf + count, "%s%d%c",
3273 cs[i]->name, cs[i]->index, i ? ' ':'\n'); 3273 cs[i]->name, cs[i]->index, i ? ' ':'\n');
3274 release_console_sem(); 3274 console_unlock();
3275 3275
3276 return count; 3276 return count;
3277} 3277}
@@ -3306,7 +3306,7 @@ int __init tty_init(void)
3306 if (IS_ERR(consdev)) 3306 if (IS_ERR(consdev))
3307 consdev = NULL; 3307 consdev = NULL;
3308 else 3308 else
3309 device_create_file(consdev, &dev_attr_active); 3309 WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
3310 3310
3311#ifdef CONFIG_VT 3311#ifdef CONFIG_VT
3312 vty_init(&console_fops); 3312 vty_init(&console_fops);
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
index ebae344ce910..c956ed6c83a3 100644
--- a/drivers/tty/vt/selection.c
+++ b/drivers/tty/vt/selection.c
@@ -316,9 +316,9 @@ int paste_selection(struct tty_struct *tty)
316 /* always called with BTM from vt_ioctl */ 316 /* always called with BTM from vt_ioctl */
317 WARN_ON(!tty_locked()); 317 WARN_ON(!tty_locked());
318 318
319 acquire_console_sem(); 319 console_lock();
320 poke_blanked_console(); 320 poke_blanked_console();
321 release_console_sem(); 321 console_unlock();
322 322
323 ld = tty_ldisc_ref(tty); 323 ld = tty_ldisc_ref(tty);
324 if (!ld) { 324 if (!ld) {
diff --git a/drivers/tty/vt/vc_screen.c b/drivers/tty/vt/vc_screen.c
index eab3a1ff99e4..a672ed192d33 100644
--- a/drivers/tty/vt/vc_screen.c
+++ b/drivers/tty/vt/vc_screen.c
@@ -202,7 +202,7 @@ vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
202 /* Select the proper current console and verify 202 /* Select the proper current console and verify
203 * sanity of the situation under the console lock. 203 * sanity of the situation under the console lock.
204 */ 204 */
205 acquire_console_sem(); 205 console_lock();
206 206
207 attr = (currcons & 128); 207 attr = (currcons & 128);
208 currcons = (currcons & 127); 208 currcons = (currcons & 127);
@@ -336,9 +336,9 @@ vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
336 * the pagefault handling code may want to call printk(). 336 * the pagefault handling code may want to call printk().
337 */ 337 */
338 338
339 release_console_sem(); 339 console_unlock();
340 ret = copy_to_user(buf, con_buf_start, orig_count); 340 ret = copy_to_user(buf, con_buf_start, orig_count);
341 acquire_console_sem(); 341 console_lock();
342 342
343 if (ret) { 343 if (ret) {
344 read += (orig_count - ret); 344 read += (orig_count - ret);
@@ -354,7 +354,7 @@ vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
354 if (read) 354 if (read)
355 ret = read; 355 ret = read;
356unlock_out: 356unlock_out:
357 release_console_sem(); 357 console_unlock();
358 mutex_unlock(&con_buf_mtx); 358 mutex_unlock(&con_buf_mtx);
359 return ret; 359 return ret;
360} 360}
@@ -379,7 +379,7 @@ vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
379 /* Select the proper current console and verify 379 /* Select the proper current console and verify
380 * sanity of the situation under the console lock. 380 * sanity of the situation under the console lock.
381 */ 381 */
382 acquire_console_sem(); 382 console_lock();
383 383
384 attr = (currcons & 128); 384 attr = (currcons & 128);
385 currcons = (currcons & 127); 385 currcons = (currcons & 127);
@@ -414,9 +414,9 @@ vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
414 /* Temporarily drop the console lock so that we can read 414 /* Temporarily drop the console lock so that we can read
415 * in the write data from userspace safely. 415 * in the write data from userspace safely.
416 */ 416 */
417 release_console_sem(); 417 console_unlock();
418 ret = copy_from_user(con_buf, buf, this_round); 418 ret = copy_from_user(con_buf, buf, this_round);
419 acquire_console_sem(); 419 console_lock();
420 420
421 if (ret) { 421 if (ret) {
422 this_round -= ret; 422 this_round -= ret;
@@ -542,7 +542,7 @@ vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
542 vcs_scr_updated(vc); 542 vcs_scr_updated(vc);
543 543
544unlock_out: 544unlock_out:
545 release_console_sem(); 545 console_unlock();
546 546
547 mutex_unlock(&con_buf_mtx); 547 mutex_unlock(&con_buf_mtx);
548 548
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index 76407eca9ab0..147ede3423df 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -1003,9 +1003,9 @@ static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1003 struct vc_data *vc = tty->driver_data; 1003 struct vc_data *vc = tty->driver_data;
1004 int ret; 1004 int ret;
1005 1005
1006 acquire_console_sem(); 1006 console_lock();
1007 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row); 1007 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1008 release_console_sem(); 1008 console_unlock();
1009 return ret; 1009 return ret;
1010} 1010}
1011 1011
@@ -1271,7 +1271,7 @@ static void default_attr(struct vc_data *vc)
1271 vc->vc_color = vc->vc_def_color; 1271 vc->vc_color = vc->vc_def_color;
1272} 1272}
1273 1273
1274/* console_sem is held */ 1274/* console_lock is held */
1275static void csi_m(struct vc_data *vc) 1275static void csi_m(struct vc_data *vc)
1276{ 1276{
1277 int i; 1277 int i;
@@ -1415,7 +1415,7 @@ int mouse_reporting(void)
1415 return vc_cons[fg_console].d->vc_report_mouse; 1415 return vc_cons[fg_console].d->vc_report_mouse;
1416} 1416}
1417 1417
1418/* console_sem is held */ 1418/* console_lock is held */
1419static void set_mode(struct vc_data *vc, int on_off) 1419static void set_mode(struct vc_data *vc, int on_off)
1420{ 1420{
1421 int i; 1421 int i;
@@ -1485,7 +1485,7 @@ static void set_mode(struct vc_data *vc, int on_off)
1485 } 1485 }
1486} 1486}
1487 1487
1488/* console_sem is held */ 1488/* console_lock is held */
1489static void setterm_command(struct vc_data *vc) 1489static void setterm_command(struct vc_data *vc)
1490{ 1490{
1491 switch(vc->vc_par[0]) { 1491 switch(vc->vc_par[0]) {
@@ -1545,7 +1545,7 @@ static void setterm_command(struct vc_data *vc)
1545 } 1545 }
1546} 1546}
1547 1547
1548/* console_sem is held */ 1548/* console_lock is held */
1549static void csi_at(struct vc_data *vc, unsigned int nr) 1549static void csi_at(struct vc_data *vc, unsigned int nr)
1550{ 1550{
1551 if (nr > vc->vc_cols - vc->vc_x) 1551 if (nr > vc->vc_cols - vc->vc_x)
@@ -1555,7 +1555,7 @@ static void csi_at(struct vc_data *vc, unsigned int nr)
1555 insert_char(vc, nr); 1555 insert_char(vc, nr);
1556} 1556}
1557 1557
1558/* console_sem is held */ 1558/* console_lock is held */
1559static void csi_L(struct vc_data *vc, unsigned int nr) 1559static void csi_L(struct vc_data *vc, unsigned int nr)
1560{ 1560{
1561 if (nr > vc->vc_rows - vc->vc_y) 1561 if (nr > vc->vc_rows - vc->vc_y)
@@ -1566,7 +1566,7 @@ static void csi_L(struct vc_data *vc, unsigned int nr)
1566 vc->vc_need_wrap = 0; 1566 vc->vc_need_wrap = 0;
1567} 1567}
1568 1568
1569/* console_sem is held */ 1569/* console_lock is held */
1570static void csi_P(struct vc_data *vc, unsigned int nr) 1570static void csi_P(struct vc_data *vc, unsigned int nr)
1571{ 1571{
1572 if (nr > vc->vc_cols - vc->vc_x) 1572 if (nr > vc->vc_cols - vc->vc_x)
@@ -1576,7 +1576,7 @@ static void csi_P(struct vc_data *vc, unsigned int nr)
1576 delete_char(vc, nr); 1576 delete_char(vc, nr);
1577} 1577}
1578 1578
1579/* console_sem is held */ 1579/* console_lock is held */
1580static void csi_M(struct vc_data *vc, unsigned int nr) 1580static void csi_M(struct vc_data *vc, unsigned int nr)
1581{ 1581{
1582 if (nr > vc->vc_rows - vc->vc_y) 1582 if (nr > vc->vc_rows - vc->vc_y)
@@ -1587,7 +1587,7 @@ static void csi_M(struct vc_data *vc, unsigned int nr)
1587 vc->vc_need_wrap = 0; 1587 vc->vc_need_wrap = 0;
1588} 1588}
1589 1589
1590/* console_sem is held (except via vc_init->reset_terminal */ 1590/* console_lock is held (except via vc_init->reset_terminal */
1591static void save_cur(struct vc_data *vc) 1591static void save_cur(struct vc_data *vc)
1592{ 1592{
1593 vc->vc_saved_x = vc->vc_x; 1593 vc->vc_saved_x = vc->vc_x;
@@ -1603,7 +1603,7 @@ static void save_cur(struct vc_data *vc)
1603 vc->vc_saved_G1 = vc->vc_G1_charset; 1603 vc->vc_saved_G1 = vc->vc_G1_charset;
1604} 1604}
1605 1605
1606/* console_sem is held */ 1606/* console_lock is held */
1607static void restore_cur(struct vc_data *vc) 1607static void restore_cur(struct vc_data *vc)
1608{ 1608{
1609 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y); 1609 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
@@ -1625,7 +1625,7 @@ enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1625 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd, 1625 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1626 ESpalette }; 1626 ESpalette };
1627 1627
1628/* console_sem is held (except via vc_init()) */ 1628/* console_lock is held (except via vc_init()) */
1629static void reset_terminal(struct vc_data *vc, int do_clear) 1629static void reset_terminal(struct vc_data *vc, int do_clear)
1630{ 1630{
1631 vc->vc_top = 0; 1631 vc->vc_top = 0;
@@ -1685,7 +1685,7 @@ static void reset_terminal(struct vc_data *vc, int do_clear)
1685 csi_J(vc, 2); 1685 csi_J(vc, 2);
1686} 1686}
1687 1687
1688/* console_sem is held */ 1688/* console_lock is held */
1689static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c) 1689static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1690{ 1690{
1691 /* 1691 /*
@@ -2119,7 +2119,7 @@ static int is_double_width(uint32_t ucs)
2119 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1); 2119 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2120} 2120}
2121 2121
2122/* acquires console_sem */ 2122/* acquires console_lock */
2123static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count) 2123static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2124{ 2124{
2125#ifdef VT_BUF_VRAM_ONLY 2125#ifdef VT_BUF_VRAM_ONLY
@@ -2147,11 +2147,11 @@ static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int co
2147 2147
2148 might_sleep(); 2148 might_sleep();
2149 2149
2150 acquire_console_sem(); 2150 console_lock();
2151 vc = tty->driver_data; 2151 vc = tty->driver_data;
2152 if (vc == NULL) { 2152 if (vc == NULL) {
2153 printk(KERN_ERR "vt: argh, driver_data is NULL !\n"); 2153 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2154 release_console_sem(); 2154 console_unlock();
2155 return 0; 2155 return 0;
2156 } 2156 }
2157 2157
@@ -2159,7 +2159,7 @@ static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int co
2159 if (!vc_cons_allocated(currcons)) { 2159 if (!vc_cons_allocated(currcons)) {
2160 /* could this happen? */ 2160 /* could this happen? */
2161 printk_once("con_write: tty %d not allocated\n", currcons+1); 2161 printk_once("con_write: tty %d not allocated\n", currcons+1);
2162 release_console_sem(); 2162 console_unlock();
2163 return 0; 2163 return 0;
2164 } 2164 }
2165 2165
@@ -2375,7 +2375,7 @@ rescan_last_byte:
2375 } 2375 }
2376 FLUSH 2376 FLUSH
2377 console_conditional_schedule(); 2377 console_conditional_schedule();
2378 release_console_sem(); 2378 console_unlock();
2379 notify_update(vc); 2379 notify_update(vc);
2380 return n; 2380 return n;
2381#undef FLUSH 2381#undef FLUSH
@@ -2388,11 +2388,11 @@ rescan_last_byte:
2388 * us to do the switches asynchronously (needed when we want 2388 * us to do the switches asynchronously (needed when we want
2389 * to switch due to a keyboard interrupt). Synchronization 2389 * to switch due to a keyboard interrupt). Synchronization
2390 * with other console code and prevention of re-entrancy is 2390 * with other console code and prevention of re-entrancy is
2391 * ensured with console_sem. 2391 * ensured with console_lock.
2392 */ 2392 */
2393static void console_callback(struct work_struct *ignored) 2393static void console_callback(struct work_struct *ignored)
2394{ 2394{
2395 acquire_console_sem(); 2395 console_lock();
2396 2396
2397 if (want_console >= 0) { 2397 if (want_console >= 0) {
2398 if (want_console != fg_console && 2398 if (want_console != fg_console &&
@@ -2422,7 +2422,7 @@ static void console_callback(struct work_struct *ignored)
2422 } 2422 }
2423 notify_update(vc_cons[fg_console].d); 2423 notify_update(vc_cons[fg_console].d);
2424 2424
2425 release_console_sem(); 2425 console_unlock();
2426} 2426}
2427 2427
2428int set_console(int nr) 2428int set_console(int nr)
@@ -2603,7 +2603,7 @@ static struct console vt_console_driver = {
2603 */ 2603 */
2604 2604
2605/* 2605/*
2606 * Generally a bit racy with respect to console_sem(). 2606 * Generally a bit racy with respect to console_lock();.
2607 * 2607 *
2608 * There are some functions which don't need it. 2608 * There are some functions which don't need it.
2609 * 2609 *
@@ -2629,17 +2629,17 @@ int tioclinux(struct tty_struct *tty, unsigned long arg)
2629 switch (type) 2629 switch (type)
2630 { 2630 {
2631 case TIOCL_SETSEL: 2631 case TIOCL_SETSEL:
2632 acquire_console_sem(); 2632 console_lock();
2633 ret = set_selection((struct tiocl_selection __user *)(p+1), tty); 2633 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2634 release_console_sem(); 2634 console_unlock();
2635 break; 2635 break;
2636 case TIOCL_PASTESEL: 2636 case TIOCL_PASTESEL:
2637 ret = paste_selection(tty); 2637 ret = paste_selection(tty);
2638 break; 2638 break;
2639 case TIOCL_UNBLANKSCREEN: 2639 case TIOCL_UNBLANKSCREEN:
2640 acquire_console_sem(); 2640 console_lock();
2641 unblank_screen(); 2641 unblank_screen();
2642 release_console_sem(); 2642 console_unlock();
2643 break; 2643 break;
2644 case TIOCL_SELLOADLUT: 2644 case TIOCL_SELLOADLUT:
2645 ret = sel_loadlut(p); 2645 ret = sel_loadlut(p);
@@ -2688,10 +2688,10 @@ int tioclinux(struct tty_struct *tty, unsigned long arg)
2688 } 2688 }
2689 break; 2689 break;
2690 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ 2690 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2691 acquire_console_sem(); 2691 console_lock();
2692 ignore_poke = 1; 2692 ignore_poke = 1;
2693 do_blank_screen(0); 2693 do_blank_screen(0);
2694 release_console_sem(); 2694 console_unlock();
2695 break; 2695 break;
2696 case TIOCL_BLANKEDSCREEN: 2696 case TIOCL_BLANKEDSCREEN:
2697 ret = console_blanked; 2697 ret = console_blanked;
@@ -2790,11 +2790,11 @@ static void con_flush_chars(struct tty_struct *tty)
2790 return; 2790 return;
2791 2791
2792 /* if we race with con_close(), vt may be null */ 2792 /* if we race with con_close(), vt may be null */
2793 acquire_console_sem(); 2793 console_lock();
2794 vc = tty->driver_data; 2794 vc = tty->driver_data;
2795 if (vc) 2795 if (vc)
2796 set_cursor(vc); 2796 set_cursor(vc);
2797 release_console_sem(); 2797 console_unlock();
2798} 2798}
2799 2799
2800/* 2800/*
@@ -2805,7 +2805,7 @@ static int con_open(struct tty_struct *tty, struct file *filp)
2805 unsigned int currcons = tty->index; 2805 unsigned int currcons = tty->index;
2806 int ret = 0; 2806 int ret = 0;
2807 2807
2808 acquire_console_sem(); 2808 console_lock();
2809 if (tty->driver_data == NULL) { 2809 if (tty->driver_data == NULL) {
2810 ret = vc_allocate(currcons); 2810 ret = vc_allocate(currcons);
2811 if (ret == 0) { 2811 if (ret == 0) {
@@ -2813,7 +2813,7 @@ static int con_open(struct tty_struct *tty, struct file *filp)
2813 2813
2814 /* Still being freed */ 2814 /* Still being freed */
2815 if (vc->port.tty) { 2815 if (vc->port.tty) {
2816 release_console_sem(); 2816 console_unlock();
2817 return -ERESTARTSYS; 2817 return -ERESTARTSYS;
2818 } 2818 }
2819 tty->driver_data = vc; 2819 tty->driver_data = vc;
@@ -2827,11 +2827,11 @@ static int con_open(struct tty_struct *tty, struct file *filp)
2827 tty->termios->c_iflag |= IUTF8; 2827 tty->termios->c_iflag |= IUTF8;
2828 else 2828 else
2829 tty->termios->c_iflag &= ~IUTF8; 2829 tty->termios->c_iflag &= ~IUTF8;
2830 release_console_sem(); 2830 console_unlock();
2831 return ret; 2831 return ret;
2832 } 2832 }
2833 } 2833 }
2834 release_console_sem(); 2834 console_unlock();
2835 return ret; 2835 return ret;
2836} 2836}
2837 2837
@@ -2844,9 +2844,9 @@ static void con_shutdown(struct tty_struct *tty)
2844{ 2844{
2845 struct vc_data *vc = tty->driver_data; 2845 struct vc_data *vc = tty->driver_data;
2846 BUG_ON(vc == NULL); 2846 BUG_ON(vc == NULL);
2847 acquire_console_sem(); 2847 console_lock();
2848 vc->port.tty = NULL; 2848 vc->port.tty = NULL;
2849 release_console_sem(); 2849 console_unlock();
2850 tty_shutdown(tty); 2850 tty_shutdown(tty);
2851} 2851}
2852 2852
@@ -2893,13 +2893,13 @@ static int __init con_init(void)
2893 struct vc_data *vc; 2893 struct vc_data *vc;
2894 unsigned int currcons = 0, i; 2894 unsigned int currcons = 0, i;
2895 2895
2896 acquire_console_sem(); 2896 console_lock();
2897 2897
2898 if (conswitchp) 2898 if (conswitchp)
2899 display_desc = conswitchp->con_startup(); 2899 display_desc = conswitchp->con_startup();
2900 if (!display_desc) { 2900 if (!display_desc) {
2901 fg_console = 0; 2901 fg_console = 0;
2902 release_console_sem(); 2902 console_unlock();
2903 return 0; 2903 return 0;
2904 } 2904 }
2905 2905
@@ -2946,7 +2946,7 @@ static int __init con_init(void)
2946 printable = 1; 2946 printable = 1;
2947 printk("\n"); 2947 printk("\n");
2948 2948
2949 release_console_sem(); 2949 console_unlock();
2950 2950
2951#ifdef CONFIG_VT_CONSOLE 2951#ifdef CONFIG_VT_CONSOLE
2952 register_console(&vt_console_driver); 2952 register_console(&vt_console_driver);
@@ -2994,7 +2994,7 @@ int __init vty_init(const struct file_operations *console_fops)
2994 if (IS_ERR(tty0dev)) 2994 if (IS_ERR(tty0dev))
2995 tty0dev = NULL; 2995 tty0dev = NULL;
2996 else 2996 else
2997 device_create_file(tty0dev, &dev_attr_active); 2997 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
2998 2998
2999 vcs_init(); 2999 vcs_init();
3000 3000
@@ -3037,7 +3037,7 @@ static int bind_con_driver(const struct consw *csw, int first, int last,
3037 if (!try_module_get(owner)) 3037 if (!try_module_get(owner))
3038 return -ENODEV; 3038 return -ENODEV;
3039 3039
3040 acquire_console_sem(); 3040 console_lock();
3041 3041
3042 /* check if driver is registered */ 3042 /* check if driver is registered */
3043 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3043 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
@@ -3122,7 +3122,7 @@ static int bind_con_driver(const struct consw *csw, int first, int last,
3122 3122
3123 retval = 0; 3123 retval = 0;
3124err: 3124err:
3125 release_console_sem(); 3125 console_unlock();
3126 module_put(owner); 3126 module_put(owner);
3127 return retval; 3127 return retval;
3128}; 3128};
@@ -3171,7 +3171,7 @@ int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3171 if (!try_module_get(owner)) 3171 if (!try_module_get(owner))
3172 return -ENODEV; 3172 return -ENODEV;
3173 3173
3174 acquire_console_sem(); 3174 console_lock();
3175 3175
3176 /* check if driver is registered and if it is unbindable */ 3176 /* check if driver is registered and if it is unbindable */
3177 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3177 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
@@ -3185,7 +3185,7 @@ int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3185 } 3185 }
3186 3186
3187 if (retval) { 3187 if (retval) {
3188 release_console_sem(); 3188 console_unlock();
3189 goto err; 3189 goto err;
3190 } 3190 }
3191 3191
@@ -3204,12 +3204,12 @@ int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3204 } 3204 }
3205 3205
3206 if (retval) { 3206 if (retval) {
3207 release_console_sem(); 3207 console_unlock();
3208 goto err; 3208 goto err;
3209 } 3209 }
3210 3210
3211 if (!con_is_bound(csw)) { 3211 if (!con_is_bound(csw)) {
3212 release_console_sem(); 3212 console_unlock();
3213 goto err; 3213 goto err;
3214 } 3214 }
3215 3215
@@ -3238,7 +3238,7 @@ int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3238 if (!con_is_bound(csw)) 3238 if (!con_is_bound(csw))
3239 con_driver->flag &= ~CON_DRIVER_FLAG_INIT; 3239 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3240 3240
3241 release_console_sem(); 3241 console_unlock();
3242 /* ignore return value, binding should not fail */ 3242 /* ignore return value, binding should not fail */
3243 bind_con_driver(defcsw, first, last, deflt); 3243 bind_con_driver(defcsw, first, last, deflt);
3244err: 3244err:
@@ -3538,14 +3538,14 @@ int register_con_driver(const struct consw *csw, int first, int last)
3538 if (!try_module_get(owner)) 3538 if (!try_module_get(owner))
3539 return -ENODEV; 3539 return -ENODEV;
3540 3540
3541 acquire_console_sem(); 3541 console_lock();
3542 3542
3543 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3543 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3544 con_driver = &registered_con_driver[i]; 3544 con_driver = &registered_con_driver[i];
3545 3545
3546 /* already registered */ 3546 /* already registered */
3547 if (con_driver->con == csw) 3547 if (con_driver->con == csw)
3548 retval = -EINVAL; 3548 retval = -EBUSY;
3549 } 3549 }
3550 3550
3551 if (retval) 3551 if (retval)
@@ -3592,7 +3592,7 @@ int register_con_driver(const struct consw *csw, int first, int last)
3592 } 3592 }
3593 3593
3594err: 3594err:
3595 release_console_sem(); 3595 console_unlock();
3596 module_put(owner); 3596 module_put(owner);
3597 return retval; 3597 return retval;
3598} 3598}
@@ -3613,7 +3613,7 @@ int unregister_con_driver(const struct consw *csw)
3613{ 3613{
3614 int i, retval = -ENODEV; 3614 int i, retval = -ENODEV;
3615 3615
3616 acquire_console_sem(); 3616 console_lock();
3617 3617
3618 /* cannot unregister a bound driver */ 3618 /* cannot unregister a bound driver */
3619 if (con_is_bound(csw)) 3619 if (con_is_bound(csw))
@@ -3639,7 +3639,7 @@ int unregister_con_driver(const struct consw *csw)
3639 } 3639 }
3640 } 3640 }
3641err: 3641err:
3642 release_console_sem(); 3642 console_unlock();
3643 return retval; 3643 return retval;
3644} 3644}
3645EXPORT_SYMBOL(unregister_con_driver); 3645EXPORT_SYMBOL(unregister_con_driver);
@@ -3656,7 +3656,12 @@ int take_over_console(const struct consw *csw, int first, int last, int deflt)
3656 int err; 3656 int err;
3657 3657
3658 err = register_con_driver(csw, first, last); 3658 err = register_con_driver(csw, first, last);
3659 3659 /* if we get an busy error we still want to bind the console driver
3660 * and return success, as we may have unbound the console driver
3661  * but not unregistered it.
3662 */
3663 if (err == -EBUSY)
3664 err = 0;
3660 if (!err) 3665 if (!err)
3661 bind_con_driver(csw, first, last, deflt); 3666 bind_con_driver(csw, first, last, deflt);
3662 3667
@@ -3934,9 +3939,9 @@ int con_set_cmap(unsigned char __user *arg)
3934{ 3939{
3935 int rc; 3940 int rc;
3936 3941
3937 acquire_console_sem(); 3942 console_lock();
3938 rc = set_get_cmap (arg,1); 3943 rc = set_get_cmap (arg,1);
3939 release_console_sem(); 3944 console_unlock();
3940 3945
3941 return rc; 3946 return rc;
3942} 3947}
@@ -3945,9 +3950,9 @@ int con_get_cmap(unsigned char __user *arg)
3945{ 3950{
3946 int rc; 3951 int rc;
3947 3952
3948 acquire_console_sem(); 3953 console_lock();
3949 rc = set_get_cmap (arg,0); 3954 rc = set_get_cmap (arg,0);
3950 release_console_sem(); 3955 console_unlock();
3951 3956
3952 return rc; 3957 return rc;
3953} 3958}
@@ -3994,12 +3999,12 @@ static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3994 } else 3999 } else
3995 font.data = NULL; 4000 font.data = NULL;
3996 4001
3997 acquire_console_sem(); 4002 console_lock();
3998 if (vc->vc_sw->con_font_get) 4003 if (vc->vc_sw->con_font_get)
3999 rc = vc->vc_sw->con_font_get(vc, &font); 4004 rc = vc->vc_sw->con_font_get(vc, &font);
4000 else 4005 else
4001 rc = -ENOSYS; 4006 rc = -ENOSYS;
4002 release_console_sem(); 4007 console_unlock();
4003 4008
4004 if (rc) 4009 if (rc)
4005 goto out; 4010 goto out;
@@ -4076,12 +4081,12 @@ static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4076 font.data = memdup_user(op->data, size); 4081 font.data = memdup_user(op->data, size);
4077 if (IS_ERR(font.data)) 4082 if (IS_ERR(font.data))
4078 return PTR_ERR(font.data); 4083 return PTR_ERR(font.data);
4079 acquire_console_sem(); 4084 console_lock();
4080 if (vc->vc_sw->con_font_set) 4085 if (vc->vc_sw->con_font_set)
4081 rc = vc->vc_sw->con_font_set(vc, &font, op->flags); 4086 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4082 else 4087 else
4083 rc = -ENOSYS; 4088 rc = -ENOSYS;
4084 release_console_sem(); 4089 console_unlock();
4085 kfree(font.data); 4090 kfree(font.data);
4086 return rc; 4091 return rc;
4087} 4092}
@@ -4103,12 +4108,12 @@ static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4103 else 4108 else
4104 name[MAX_FONT_NAME - 1] = 0; 4109 name[MAX_FONT_NAME - 1] = 0;
4105 4110
4106 acquire_console_sem(); 4111 console_lock();
4107 if (vc->vc_sw->con_font_default) 4112 if (vc->vc_sw->con_font_default)
4108 rc = vc->vc_sw->con_font_default(vc, &font, s); 4113 rc = vc->vc_sw->con_font_default(vc, &font, s);
4109 else 4114 else
4110 rc = -ENOSYS; 4115 rc = -ENOSYS;
4111 release_console_sem(); 4116 console_unlock();
4112 if (!rc) { 4117 if (!rc) {
4113 op->width = font.width; 4118 op->width = font.width;
4114 op->height = font.height; 4119 op->height = font.height;
@@ -4124,7 +4129,7 @@ static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4124 if (vc->vc_mode != KD_TEXT) 4129 if (vc->vc_mode != KD_TEXT)
4125 return -EINVAL; 4130 return -EINVAL;
4126 4131
4127 acquire_console_sem(); 4132 console_lock();
4128 if (!vc->vc_sw->con_font_copy) 4133 if (!vc->vc_sw->con_font_copy)
4129 rc = -ENOSYS; 4134 rc = -ENOSYS;
4130 else if (con < 0 || !vc_cons_allocated(con)) 4135 else if (con < 0 || !vc_cons_allocated(con))
@@ -4133,7 +4138,7 @@ static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4133 rc = 0; 4138 rc = 0;
4134 else 4139 else
4135 rc = vc->vc_sw->con_font_copy(vc, con); 4140 rc = vc->vc_sw->con_font_copy(vc, con);
4136 release_console_sem(); 4141 console_unlock();
4137 return rc; 4142 return rc;
4138} 4143}
4139 4144
diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
index 6b68a0fb4611..1235ebda6e1c 100644
--- a/drivers/tty/vt/vt_ioctl.c
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -649,12 +649,12 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
649 /* 649 /*
650 * explicitly blank/unblank the screen if switching modes 650 * explicitly blank/unblank the screen if switching modes
651 */ 651 */
652 acquire_console_sem(); 652 console_lock();
653 if (arg == KD_TEXT) 653 if (arg == KD_TEXT)
654 do_unblank_screen(1); 654 do_unblank_screen(1);
655 else 655 else
656 do_blank_screen(1); 656 do_blank_screen(1);
657 release_console_sem(); 657 console_unlock();
658 break; 658 break;
659 659
660 case KDGETMODE: 660 case KDGETMODE:
@@ -893,7 +893,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
893 ret = -EINVAL; 893 ret = -EINVAL;
894 goto out; 894 goto out;
895 } 895 }
896 acquire_console_sem(); 896 console_lock();
897 vc->vt_mode = tmp; 897 vc->vt_mode = tmp;
898 /* the frsig is ignored, so we set it to 0 */ 898 /* the frsig is ignored, so we set it to 0 */
899 vc->vt_mode.frsig = 0; 899 vc->vt_mode.frsig = 0;
@@ -901,7 +901,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
901 vc->vt_pid = get_pid(task_pid(current)); 901 vc->vt_pid = get_pid(task_pid(current));
902 /* no switch is required -- saw@shade.msu.ru */ 902 /* no switch is required -- saw@shade.msu.ru */
903 vc->vt_newvt = -1; 903 vc->vt_newvt = -1;
904 release_console_sem(); 904 console_unlock();
905 break; 905 break;
906 } 906 }
907 907
@@ -910,9 +910,9 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
910 struct vt_mode tmp; 910 struct vt_mode tmp;
911 int rc; 911 int rc;
912 912
913 acquire_console_sem(); 913 console_lock();
914 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode)); 914 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
915 release_console_sem(); 915 console_unlock();
916 916
917 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode)); 917 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
918 if (rc) 918 if (rc)
@@ -965,9 +965,9 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
965 ret = -ENXIO; 965 ret = -ENXIO;
966 else { 966 else {
967 arg--; 967 arg--;
968 acquire_console_sem(); 968 console_lock();
969 ret = vc_allocate(arg); 969 ret = vc_allocate(arg);
970 release_console_sem(); 970 console_unlock();
971 if (ret) 971 if (ret)
972 break; 972 break;
973 set_console(arg); 973 set_console(arg);
@@ -990,7 +990,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
990 ret = -ENXIO; 990 ret = -ENXIO;
991 else { 991 else {
992 vsa.console--; 992 vsa.console--;
993 acquire_console_sem(); 993 console_lock();
994 ret = vc_allocate(vsa.console); 994 ret = vc_allocate(vsa.console);
995 if (ret == 0) { 995 if (ret == 0) {
996 struct vc_data *nvc; 996 struct vc_data *nvc;
@@ -1003,7 +1003,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1003 put_pid(nvc->vt_pid); 1003 put_pid(nvc->vt_pid);
1004 nvc->vt_pid = get_pid(task_pid(current)); 1004 nvc->vt_pid = get_pid(task_pid(current));
1005 } 1005 }
1006 release_console_sem(); 1006 console_unlock();
1007 if (ret) 1007 if (ret)
1008 break; 1008 break;
1009 /* Commence switch and lock */ 1009 /* Commence switch and lock */
@@ -1044,7 +1044,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1044 /* 1044 /*
1045 * Switching-from response 1045 * Switching-from response
1046 */ 1046 */
1047 acquire_console_sem(); 1047 console_lock();
1048 if (vc->vt_newvt >= 0) { 1048 if (vc->vt_newvt >= 0) {
1049 if (arg == 0) 1049 if (arg == 0)
1050 /* 1050 /*
@@ -1063,7 +1063,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1063 vc->vt_newvt = -1; 1063 vc->vt_newvt = -1;
1064 ret = vc_allocate(newvt); 1064 ret = vc_allocate(newvt);
1065 if (ret) { 1065 if (ret) {
1066 release_console_sem(); 1066 console_unlock();
1067 break; 1067 break;
1068 } 1068 }
1069 /* 1069 /*
@@ -1083,7 +1083,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1083 if (arg != VT_ACKACQ) 1083 if (arg != VT_ACKACQ)
1084 ret = -EINVAL; 1084 ret = -EINVAL;
1085 } 1085 }
1086 release_console_sem(); 1086 console_unlock();
1087 break; 1087 break;
1088 1088
1089 /* 1089 /*
@@ -1096,20 +1096,20 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1096 } 1096 }
1097 if (arg == 0) { 1097 if (arg == 0) {
1098 /* deallocate all unused consoles, but leave 0 */ 1098 /* deallocate all unused consoles, but leave 0 */
1099 acquire_console_sem(); 1099 console_lock();
1100 for (i=1; i<MAX_NR_CONSOLES; i++) 1100 for (i=1; i<MAX_NR_CONSOLES; i++)
1101 if (! VT_BUSY(i)) 1101 if (! VT_BUSY(i))
1102 vc_deallocate(i); 1102 vc_deallocate(i);
1103 release_console_sem(); 1103 console_unlock();
1104 } else { 1104 } else {
1105 /* deallocate a single console, if possible */ 1105 /* deallocate a single console, if possible */
1106 arg--; 1106 arg--;
1107 if (VT_BUSY(arg)) 1107 if (VT_BUSY(arg))
1108 ret = -EBUSY; 1108 ret = -EBUSY;
1109 else if (arg) { /* leave 0 */ 1109 else if (arg) { /* leave 0 */
1110 acquire_console_sem(); 1110 console_lock();
1111 vc_deallocate(arg); 1111 vc_deallocate(arg);
1112 release_console_sem(); 1112 console_unlock();
1113 } 1113 }
1114 } 1114 }
1115 break; 1115 break;
@@ -1126,7 +1126,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1126 get_user(cc, &vtsizes->v_cols)) 1126 get_user(cc, &vtsizes->v_cols))
1127 ret = -EFAULT; 1127 ret = -EFAULT;
1128 else { 1128 else {
1129 acquire_console_sem(); 1129 console_lock();
1130 for (i = 0; i < MAX_NR_CONSOLES; i++) { 1130 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1131 vc = vc_cons[i].d; 1131 vc = vc_cons[i].d;
1132 1132
@@ -1135,7 +1135,7 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1135 vc_resize(vc_cons[i].d, cc, ll); 1135 vc_resize(vc_cons[i].d, cc, ll);
1136 } 1136 }
1137 } 1137 }
1138 release_console_sem(); 1138 console_unlock();
1139 } 1139 }
1140 break; 1140 break;
1141 } 1141 }
@@ -1187,14 +1187,14 @@ int vt_ioctl(struct tty_struct *tty, struct file * file,
1187 for (i = 0; i < MAX_NR_CONSOLES; i++) { 1187 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1188 if (!vc_cons[i].d) 1188 if (!vc_cons[i].d)
1189 continue; 1189 continue;
1190 acquire_console_sem(); 1190 console_lock();
1191 if (vlin) 1191 if (vlin)
1192 vc_cons[i].d->vc_scan_lines = vlin; 1192 vc_cons[i].d->vc_scan_lines = vlin;
1193 if (clin) 1193 if (clin)
1194 vc_cons[i].d->vc_font.height = clin; 1194 vc_cons[i].d->vc_font.height = clin;
1195 vc_cons[i].d->vc_resize_user = 1; 1195 vc_cons[i].d->vc_resize_user = 1;
1196 vc_resize(vc_cons[i].d, cc, ll); 1196 vc_resize(vc_cons[i].d, cc, ll);
1197 release_console_sem(); 1197 console_unlock();
1198 } 1198 }
1199 break; 1199 break;
1200 } 1200 }
@@ -1367,7 +1367,7 @@ void vc_SAK(struct work_struct *work)
1367 struct vc_data *vc; 1367 struct vc_data *vc;
1368 struct tty_struct *tty; 1368 struct tty_struct *tty;
1369 1369
1370 acquire_console_sem(); 1370 console_lock();
1371 vc = vc_con->d; 1371 vc = vc_con->d;
1372 if (vc) { 1372 if (vc) {
1373 tty = vc->port.tty; 1373 tty = vc->port.tty;
@@ -1379,7 +1379,7 @@ void vc_SAK(struct work_struct *work)
1379 __do_SAK(tty); 1379 __do_SAK(tty);
1380 reset_vc(vc); 1380 reset_vc(vc);
1381 } 1381 }
1382 release_console_sem(); 1382 console_unlock();
1383} 1383}
1384 1384
1385#ifdef CONFIG_COMPAT 1385#ifdef CONFIG_COMPAT
@@ -1737,10 +1737,10 @@ int vt_move_to_console(unsigned int vt, int alloc)
1737{ 1737{
1738 int prev; 1738 int prev;
1739 1739
1740 acquire_console_sem(); 1740 console_lock();
1741 /* Graphics mode - up to X */ 1741 /* Graphics mode - up to X */
1742 if (disable_vt_switch) { 1742 if (disable_vt_switch) {
1743 release_console_sem(); 1743 console_unlock();
1744 return 0; 1744 return 0;
1745 } 1745 }
1746 prev = fg_console; 1746 prev = fg_console;
@@ -1748,7 +1748,7 @@ int vt_move_to_console(unsigned int vt, int alloc)
1748 if (alloc && vc_allocate(vt)) { 1748 if (alloc && vc_allocate(vt)) {
1749 /* we can't have a free VC for now. Too bad, 1749 /* we can't have a free VC for now. Too bad,
1750 * we don't want to mess the screen for now. */ 1750 * we don't want to mess the screen for now. */
1751 release_console_sem(); 1751 console_unlock();
1752 return -ENOSPC; 1752 return -ENOSPC;
1753 } 1753 }
1754 1754
@@ -1758,10 +1758,10 @@ int vt_move_to_console(unsigned int vt, int alloc)
1758 * Let the calling function know so it can decide 1758 * Let the calling function know so it can decide
1759 * what to do. 1759 * what to do.
1760 */ 1760 */
1761 release_console_sem(); 1761 console_unlock();
1762 return -EIO; 1762 return -EIO;
1763 } 1763 }
1764 release_console_sem(); 1764 console_unlock();
1765 tty_lock(); 1765 tty_lock();
1766 if (vt_waitactive(vt + 1)) { 1766 if (vt_waitactive(vt + 1)) {
1767 pr_debug("Suspend: Can't switch VCs."); 1767 pr_debug("Suspend: Can't switch VCs.");
@@ -1781,8 +1781,8 @@ int vt_move_to_console(unsigned int vt, int alloc)
1781 */ 1781 */
1782void pm_set_vt_switch(int do_switch) 1782void pm_set_vt_switch(int do_switch)
1783{ 1783{
1784 acquire_console_sem(); 1784 console_lock();
1785 disable_vt_switch = !do_switch; 1785 disable_vt_switch = !do_switch;
1786 release_console_sem(); 1786 console_unlock();
1787} 1787}
1788EXPORT_SYMBOL(pm_set_vt_switch); 1788EXPORT_SYMBOL(pm_set_vt_switch);