xref: /linux/drivers/tty/vt/vc_screen.c (revision 7d62549a22ee500ea2ad764b9d04b87c6bbcd75f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
260d4ae8dSGreg Kroah-Hartman /*
360d4ae8dSGreg Kroah-Hartman  * Provide access to virtual console memory.
467fbfc39SJakub Wilk  * /dev/vcs: the screen as it is being viewed right now (possibly scrolled)
560d4ae8dSGreg Kroah-Hartman  * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
660d4ae8dSGreg Kroah-Hartman  *            [minor: N]
760d4ae8dSGreg Kroah-Hartman  *
860d4ae8dSGreg Kroah-Hartman  * /dev/vcsaN: idem, but including attributes, and prefixed with
960d4ae8dSGreg Kroah-Hartman  *	the 4 bytes lines,columns,x,y (as screendump used to give).
1060d4ae8dSGreg Kroah-Hartman  *	Attribute/character pair is in native endianity.
1160d4ae8dSGreg Kroah-Hartman  *            [minor: N+128]
1260d4ae8dSGreg Kroah-Hartman  *
13d21b0be2SNicolas Pitre  * /dev/vcsuN: similar to /dev/vcsaN but using 4-byte unicode values
14d21b0be2SNicolas Pitre  *	instead of 1-byte screen glyph values.
15d21b0be2SNicolas Pitre  *            [minor: N+64]
16d21b0be2SNicolas Pitre  *
17d21b0be2SNicolas Pitre  * /dev/vcsuaN: same idea as /dev/vcsaN for unicode (not yet implemented).
18d21b0be2SNicolas Pitre  *
1960d4ae8dSGreg Kroah-Hartman  * This replaces screendump and part of selection, so that the system
2060d4ae8dSGreg Kroah-Hartman  * administrator can control access using file system permissions.
2160d4ae8dSGreg Kroah-Hartman  *
2260d4ae8dSGreg Kroah-Hartman  * aeb@cwi.nl - efter Friedas begravelse - 950211
2360d4ae8dSGreg Kroah-Hartman  *
2460d4ae8dSGreg Kroah-Hartman  * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
2560d4ae8dSGreg Kroah-Hartman  *	 - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
2660d4ae8dSGreg Kroah-Hartman  *	 - making it shorter - scr_readw are macros which expand in PRETTY long code
2760d4ae8dSGreg Kroah-Hartman  */
2860d4ae8dSGreg Kroah-Hartman 
2960d4ae8dSGreg Kroah-Hartman #include <linux/kernel.h>
3060d4ae8dSGreg Kroah-Hartman #include <linux/major.h>
3160d4ae8dSGreg Kroah-Hartman #include <linux/errno.h>
320e648f42SPaul Gortmaker #include <linux/export.h>
3360d4ae8dSGreg Kroah-Hartman #include <linux/tty.h>
3460d4ae8dSGreg Kroah-Hartman #include <linux/interrupt.h>
3560d4ae8dSGreg Kroah-Hartman #include <linux/mm.h>
3660d4ae8dSGreg Kroah-Hartman #include <linux/init.h>
3760d4ae8dSGreg Kroah-Hartman #include <linux/vt_kern.h>
3860d4ae8dSGreg Kroah-Hartman #include <linux/selection.h>
3960d4ae8dSGreg Kroah-Hartman #include <linux/kbd_kern.h>
4060d4ae8dSGreg Kroah-Hartman #include <linux/console.h>
4160d4ae8dSGreg Kroah-Hartman #include <linux/device.h>
4260d4ae8dSGreg Kroah-Hartman #include <linux/sched.h>
4360d4ae8dSGreg Kroah-Hartman #include <linux/fs.h>
4460d4ae8dSGreg Kroah-Hartman #include <linux/poll.h>
4560d4ae8dSGreg Kroah-Hartman #include <linux/signal.h>
4660d4ae8dSGreg Kroah-Hartman #include <linux/slab.h>
4760d4ae8dSGreg Kroah-Hartman #include <linux/notifier.h>
4860d4ae8dSGreg Kroah-Hartman 
497c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
5060d4ae8dSGreg Kroah-Hartman #include <asm/byteorder.h>
5160d4ae8dSGreg Kroah-Hartman #include <asm/unaligned.h>
5260d4ae8dSGreg Kroah-Hartman 
5360d4ae8dSGreg Kroah-Hartman #undef attr
5460d4ae8dSGreg Kroah-Hartman #undef org
5560d4ae8dSGreg Kroah-Hartman #undef addr
5660d4ae8dSGreg Kroah-Hartman #define HEADER_SIZE	4
5760d4ae8dSGreg Kroah-Hartman 
58fcdba07eSJiri Olsa #define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
59fcdba07eSJiri Olsa 
60d21b0be2SNicolas Pitre /*
61d21b0be2SNicolas Pitre  * Our minor space:
62d21b0be2SNicolas Pitre  *
63d21b0be2SNicolas Pitre  *   0 ... 63	glyph mode without attributes
64d21b0be2SNicolas Pitre  *  64 ... 127	unicode mode without attributes
65d21b0be2SNicolas Pitre  * 128 ... 191	glyph mode with attributes
66d21b0be2SNicolas Pitre  * 192 ... 255	unused (reserved for unicode with attributes)
67d21b0be2SNicolas Pitre  *
68d21b0be2SNicolas Pitre  * This relies on MAX_NR_CONSOLES being  <= 63, meaning 63 actual consoles
69d21b0be2SNicolas Pitre  * with minors 0, 64, 128 and 192 being proxies for the foreground console.
70d21b0be2SNicolas Pitre  */
71d21b0be2SNicolas Pitre #if MAX_NR_CONSOLES > 63
72d21b0be2SNicolas Pitre #warning "/dev/vcs* devices may not accommodate more than 63 consoles"
73d21b0be2SNicolas Pitre #endif
74d21b0be2SNicolas Pitre 
75d21b0be2SNicolas Pitre #define console(inode)		(iminor(inode) & 63)
76d21b0be2SNicolas Pitre #define use_unicode(inode)	(iminor(inode) & 64)
77d21b0be2SNicolas Pitre #define use_attributes(inode)	(iminor(inode) & 128)
78d21b0be2SNicolas Pitre 
79d21b0be2SNicolas Pitre 
8060d4ae8dSGreg Kroah-Hartman struct vcs_poll_data {
8160d4ae8dSGreg Kroah-Hartman 	struct notifier_block notifier;
8260d4ae8dSGreg Kroah-Hartman 	unsigned int cons_num;
831bf931abSNicolas Pitre 	int event;
8460d4ae8dSGreg Kroah-Hartman 	wait_queue_head_t waitq;
8560d4ae8dSGreg Kroah-Hartman 	struct fasync_struct *fasync;
8660d4ae8dSGreg Kroah-Hartman };
8760d4ae8dSGreg Kroah-Hartman 
8860d4ae8dSGreg Kroah-Hartman static int
8960d4ae8dSGreg Kroah-Hartman vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
9060d4ae8dSGreg Kroah-Hartman {
9160d4ae8dSGreg Kroah-Hartman 	struct vt_notifier_param *param = _param;
9260d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = param->vc;
9360d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll =
9460d4ae8dSGreg Kroah-Hartman 		container_of(nb, struct vcs_poll_data, notifier);
9560d4ae8dSGreg Kroah-Hartman 	int currcons = poll->cons_num;
96fad08b20SNicolas Pitre 	int fa_band;
9760d4ae8dSGreg Kroah-Hartman 
98fad08b20SNicolas Pitre 	switch (code) {
99fad08b20SNicolas Pitre 	case VT_UPDATE:
100fad08b20SNicolas Pitre 		fa_band = POLL_PRI;
101fad08b20SNicolas Pitre 		break;
102fad08b20SNicolas Pitre 	case VT_DEALLOCATE:
103fad08b20SNicolas Pitre 		fa_band = POLL_HUP;
104fad08b20SNicolas Pitre 		break;
105fad08b20SNicolas Pitre 	default:
10660d4ae8dSGreg Kroah-Hartman 		return NOTIFY_DONE;
107fad08b20SNicolas Pitre 	}
10860d4ae8dSGreg Kroah-Hartman 
10960d4ae8dSGreg Kroah-Hartman 	if (currcons == 0)
11060d4ae8dSGreg Kroah-Hartman 		currcons = fg_console;
11160d4ae8dSGreg Kroah-Hartman 	else
11260d4ae8dSGreg Kroah-Hartman 		currcons--;
11360d4ae8dSGreg Kroah-Hartman 	if (currcons != vc->vc_num)
11460d4ae8dSGreg Kroah-Hartman 		return NOTIFY_DONE;
11560d4ae8dSGreg Kroah-Hartman 
1161bf931abSNicolas Pitre 	poll->event = code;
11760d4ae8dSGreg Kroah-Hartman 	wake_up_interruptible(&poll->waitq);
118fad08b20SNicolas Pitre 	kill_fasync(&poll->fasync, SIGIO, fa_band);
11960d4ae8dSGreg Kroah-Hartman 	return NOTIFY_OK;
12060d4ae8dSGreg Kroah-Hartman }
12160d4ae8dSGreg Kroah-Hartman 
12260d4ae8dSGreg Kroah-Hartman static void
12360d4ae8dSGreg Kroah-Hartman vcs_poll_data_free(struct vcs_poll_data *poll)
12460d4ae8dSGreg Kroah-Hartman {
12560d4ae8dSGreg Kroah-Hartman 	unregister_vt_notifier(&poll->notifier);
12660d4ae8dSGreg Kroah-Hartman 	kfree(poll);
12760d4ae8dSGreg Kroah-Hartman }
12860d4ae8dSGreg Kroah-Hartman 
12960d4ae8dSGreg Kroah-Hartman static struct vcs_poll_data *
13060d4ae8dSGreg Kroah-Hartman vcs_poll_data_get(struct file *file)
13160d4ae8dSGreg Kroah-Hartman {
132e8cd8169SAl Viro 	struct vcs_poll_data *poll = file->private_data, *kill = NULL;
13360d4ae8dSGreg Kroah-Hartman 
13460d4ae8dSGreg Kroah-Hartman 	if (poll)
13560d4ae8dSGreg Kroah-Hartman 		return poll;
13660d4ae8dSGreg Kroah-Hartman 
13760d4ae8dSGreg Kroah-Hartman 	poll = kzalloc(sizeof(*poll), GFP_KERNEL);
13860d4ae8dSGreg Kroah-Hartman 	if (!poll)
13960d4ae8dSGreg Kroah-Hartman 		return NULL;
140d21b0be2SNicolas Pitre 	poll->cons_num = console(file_inode(file));
14160d4ae8dSGreg Kroah-Hartman 	init_waitqueue_head(&poll->waitq);
14260d4ae8dSGreg Kroah-Hartman 	poll->notifier.notifier_call = vcs_notifier;
14395252f9cSNicolas Pitre 	/*
14495252f9cSNicolas Pitre 	 * In order not to lose any update event, we must pretend one might
14595252f9cSNicolas Pitre 	 * have occurred before we have a chance to register our notifier.
14695252f9cSNicolas Pitre 	 * This is also how user space has come to detect which kernels
14795252f9cSNicolas Pitre 	 * support POLLPRI on /dev/vcs* devices i.e. using poll() with
14895252f9cSNicolas Pitre 	 * POLLPRI and a zero timeout.
14995252f9cSNicolas Pitre 	 */
15095252f9cSNicolas Pitre 	poll->event = VT_UPDATE;
15195252f9cSNicolas Pitre 
15260d4ae8dSGreg Kroah-Hartman 	if (register_vt_notifier(&poll->notifier) != 0) {
15360d4ae8dSGreg Kroah-Hartman 		kfree(poll);
15460d4ae8dSGreg Kroah-Hartman 		return NULL;
15560d4ae8dSGreg Kroah-Hartman 	}
15660d4ae8dSGreg Kroah-Hartman 
15760d4ae8dSGreg Kroah-Hartman 	/*
15860d4ae8dSGreg Kroah-Hartman 	 * This code may be called either through ->poll() or ->fasync().
15960d4ae8dSGreg Kroah-Hartman 	 * If we have two threads using the same file descriptor, they could
16060d4ae8dSGreg Kroah-Hartman 	 * both enter this function, both notice that the structure hasn't
16160d4ae8dSGreg Kroah-Hartman 	 * been allocated yet and go ahead allocating it in parallel, but
16260d4ae8dSGreg Kroah-Hartman 	 * only one of them must survive and be shared otherwise we'd leak
16360d4ae8dSGreg Kroah-Hartman 	 * memory with a dangling notifier callback.
16460d4ae8dSGreg Kroah-Hartman 	 */
16560d4ae8dSGreg Kroah-Hartman 	spin_lock(&file->f_lock);
16660d4ae8dSGreg Kroah-Hartman 	if (!file->private_data) {
16760d4ae8dSGreg Kroah-Hartman 		file->private_data = poll;
16860d4ae8dSGreg Kroah-Hartman 	} else {
16960d4ae8dSGreg Kroah-Hartman 		/* someone else raced ahead of us */
170e8cd8169SAl Viro 		kill = poll;
17160d4ae8dSGreg Kroah-Hartman 		poll = file->private_data;
17260d4ae8dSGreg Kroah-Hartman 	}
17360d4ae8dSGreg Kroah-Hartman 	spin_unlock(&file->f_lock);
174e8cd8169SAl Viro 	if (kill)
175e8cd8169SAl Viro 		vcs_poll_data_free(kill);
17660d4ae8dSGreg Kroah-Hartman 
17760d4ae8dSGreg Kroah-Hartman 	return poll;
17860d4ae8dSGreg Kroah-Hartman }
17960d4ae8dSGreg Kroah-Hartman 
180*7d62549aSJiri Slaby /**
181*7d62549aSJiri Slaby  * vcs_vc -- return VC for @inode
182*7d62549aSJiri Slaby  * @inode: inode for which to return a VC
183*7d62549aSJiri Slaby  * @viewed: returns whether this console is currently foreground (viewed)
184*7d62549aSJiri Slaby  *
185fcdba07eSJiri Olsa  * Must be called with console_lock.
186fcdba07eSJiri Olsa  */
187*7d62549aSJiri Slaby static struct vc_data *vcs_vc(struct inode *inode, bool *viewed)
188fcdba07eSJiri Olsa {
189d21b0be2SNicolas Pitre 	unsigned int currcons = console(inode);
190fcdba07eSJiri Olsa 
191fcdba07eSJiri Olsa 	WARN_CONSOLE_UNLOCKED();
192fcdba07eSJiri Olsa 
193fcdba07eSJiri Olsa 	if (currcons == 0) {
194fcdba07eSJiri Olsa 		currcons = fg_console;
195fcdba07eSJiri Olsa 		if (viewed)
196*7d62549aSJiri Slaby 			*viewed = true;
197fcdba07eSJiri Olsa 	} else {
198fcdba07eSJiri Olsa 		currcons--;
199fcdba07eSJiri Olsa 		if (viewed)
200*7d62549aSJiri Slaby 			*viewed = false;
201fcdba07eSJiri Olsa 	}
202fcdba07eSJiri Olsa 	return vc_cons[currcons].d;
203fcdba07eSJiri Olsa }
204fcdba07eSJiri Olsa 
205fcdba07eSJiri Olsa /*
206fcdba07eSJiri Olsa  * Returns size for VC carried by inode.
207fcdba07eSJiri Olsa  * Must be called with console_lock.
208fcdba07eSJiri Olsa  */
20960d4ae8dSGreg Kroah-Hartman static int
21060d4ae8dSGreg Kroah-Hartman vcs_size(struct inode *inode)
21160d4ae8dSGreg Kroah-Hartman {
21260d4ae8dSGreg Kroah-Hartman 	int size;
21360d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc;
21460d4ae8dSGreg Kroah-Hartman 
215fcdba07eSJiri Olsa 	WARN_CONSOLE_UNLOCKED();
216fcdba07eSJiri Olsa 
217fcdba07eSJiri Olsa 	vc = vcs_vc(inode, NULL);
218fcdba07eSJiri Olsa 	if (!vc)
21960d4ae8dSGreg Kroah-Hartman 		return -ENXIO;
22060d4ae8dSGreg Kroah-Hartman 
22160d4ae8dSGreg Kroah-Hartman 	size = vc->vc_rows * vc->vc_cols;
22260d4ae8dSGreg Kroah-Hartman 
223d21b0be2SNicolas Pitre 	if (use_attributes(inode)) {
224d21b0be2SNicolas Pitre 		if (use_unicode(inode))
225d21b0be2SNicolas Pitre 			return -EOPNOTSUPP;
22660d4ae8dSGreg Kroah-Hartman 		size = 2*size + HEADER_SIZE;
227d21b0be2SNicolas Pitre 	} else if (use_unicode(inode))
228d21b0be2SNicolas Pitre 		size *= 4;
22960d4ae8dSGreg Kroah-Hartman 	return size;
23060d4ae8dSGreg Kroah-Hartman }
23160d4ae8dSGreg Kroah-Hartman 
23260d4ae8dSGreg Kroah-Hartman static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
23360d4ae8dSGreg Kroah-Hartman {
23460d4ae8dSGreg Kroah-Hartman 	int size;
23560d4ae8dSGreg Kroah-Hartman 
236dc1892c4SJiri Olsa 	console_lock();
237496ad9aaSAl Viro 	size = vcs_size(file_inode(file));
238dc1892c4SJiri Olsa 	console_unlock();
239fcdba07eSJiri Olsa 	if (size < 0)
240dc1892c4SJiri Olsa 		return size;
24165004276SAl Viro 	return fixed_size_llseek(file, offset, orig, size);
24260d4ae8dSGreg Kroah-Hartman }
24360d4ae8dSGreg Kroah-Hartman 
24460d4ae8dSGreg Kroah-Hartman 
24560d4ae8dSGreg Kroah-Hartman static ssize_t
24660d4ae8dSGreg Kroah-Hartman vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
24760d4ae8dSGreg Kroah-Hartman {
248496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
24960d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc;
25060d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll;
251d21b0be2SNicolas Pitre 	long pos, read;
252*7d62549aSJiri Slaby 	int attr, uni_mode, row, col, maxcol;
25360d4ae8dSGreg Kroah-Hartman 	unsigned short *org = NULL;
25460d4ae8dSGreg Kroah-Hartman 	ssize_t ret;
255fcdba07eSJiri Olsa 	char *con_buf;
256*7d62549aSJiri Slaby 	bool viewed;
25760d4ae8dSGreg Kroah-Hartman 
258fcdba07eSJiri Olsa 	con_buf = (char *) __get_free_page(GFP_KERNEL);
259fcdba07eSJiri Olsa 	if (!con_buf)
260fcdba07eSJiri Olsa 		return -ENOMEM;
26160d4ae8dSGreg Kroah-Hartman 
26260d4ae8dSGreg Kroah-Hartman 	pos = *ppos;
26360d4ae8dSGreg Kroah-Hartman 
26460d4ae8dSGreg Kroah-Hartman 	/* Select the proper current console and verify
26560d4ae8dSGreg Kroah-Hartman 	 * sanity of the situation under the console lock.
26660d4ae8dSGreg Kroah-Hartman 	 */
267ac751efaSTorben Hohn 	console_lock();
26860d4ae8dSGreg Kroah-Hartman 
269d21b0be2SNicolas Pitre 	uni_mode = use_unicode(inode);
270d21b0be2SNicolas Pitre 	attr = use_attributes(inode);
27160d4ae8dSGreg Kroah-Hartman 	ret = -ENXIO;
272fcdba07eSJiri Olsa 	vc = vcs_vc(inode, &viewed);
273fcdba07eSJiri Olsa 	if (!vc)
27460d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
27560d4ae8dSGreg Kroah-Hartman 
27660d4ae8dSGreg Kroah-Hartman 	ret = -EINVAL;
27760d4ae8dSGreg Kroah-Hartman 	if (pos < 0)
27860d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
279d21b0be2SNicolas Pitre 	/* we enforce 32-bit alignment for pos and count in unicode mode */
280d21b0be2SNicolas Pitre 	if (uni_mode && (pos | count) & 3)
281d21b0be2SNicolas Pitre 		goto unlock_out;
282d21b0be2SNicolas Pitre 
28360d4ae8dSGreg Kroah-Hartman 	poll = file->private_data;
28460d4ae8dSGreg Kroah-Hartman 	if (count && poll)
2851bf931abSNicolas Pitre 		poll->event = 0;
28660d4ae8dSGreg Kroah-Hartman 	read = 0;
28760d4ae8dSGreg Kroah-Hartman 	ret = 0;
28860d4ae8dSGreg Kroah-Hartman 	while (count) {
28960d4ae8dSGreg Kroah-Hartman 		char *con_buf0, *con_buf_start;
29060d4ae8dSGreg Kroah-Hartman 		long this_round, size;
29160d4ae8dSGreg Kroah-Hartman 		ssize_t orig_count;
29260d4ae8dSGreg Kroah-Hartman 		long p = pos;
29360d4ae8dSGreg Kroah-Hartman 
29460d4ae8dSGreg Kroah-Hartman 		/* Check whether we are above size each round,
29560d4ae8dSGreg Kroah-Hartman 		 * as copy_to_user at the end of this loop
29660d4ae8dSGreg Kroah-Hartman 		 * could sleep.
29760d4ae8dSGreg Kroah-Hartman 		 */
29860d4ae8dSGreg Kroah-Hartman 		size = vcs_size(inode);
299dc1892c4SJiri Olsa 		if (size < 0) {
300dc1892c4SJiri Olsa 			if (read)
301dc1892c4SJiri Olsa 				break;
302dc1892c4SJiri Olsa 			ret = size;
303dc1892c4SJiri Olsa 			goto unlock_out;
304dc1892c4SJiri Olsa 		}
30560d4ae8dSGreg Kroah-Hartman 		if (pos >= size)
30660d4ae8dSGreg Kroah-Hartman 			break;
30760d4ae8dSGreg Kroah-Hartman 		if (count > size - pos)
30860d4ae8dSGreg Kroah-Hartman 			count = size - pos;
30960d4ae8dSGreg Kroah-Hartman 
31060d4ae8dSGreg Kroah-Hartman 		this_round = count;
31160d4ae8dSGreg Kroah-Hartman 		if (this_round > CON_BUF_SIZE)
31260d4ae8dSGreg Kroah-Hartman 			this_round = CON_BUF_SIZE;
31360d4ae8dSGreg Kroah-Hartman 
31460d4ae8dSGreg Kroah-Hartman 		/* Perform the whole read into the local con_buf.
31560d4ae8dSGreg Kroah-Hartman 		 * Then we can drop the console spinlock and safely
31660d4ae8dSGreg Kroah-Hartman 		 * attempt to move it to userspace.
31760d4ae8dSGreg Kroah-Hartman 		 */
31860d4ae8dSGreg Kroah-Hartman 
31960d4ae8dSGreg Kroah-Hartman 		con_buf_start = con_buf0 = con_buf;
32060d4ae8dSGreg Kroah-Hartman 		orig_count = this_round;
32160d4ae8dSGreg Kroah-Hartman 		maxcol = vc->vc_cols;
322d21b0be2SNicolas Pitre 		if (uni_mode) {
323d21b0be2SNicolas Pitre 			unsigned int nr;
324d21b0be2SNicolas Pitre 
325d21b0be2SNicolas Pitre 			ret = vc_uniscr_check(vc);
326d21b0be2SNicolas Pitre 			if (ret)
327d21b0be2SNicolas Pitre 				break;
328d21b0be2SNicolas Pitre 			p /= 4;
329d21b0be2SNicolas Pitre 			row = p / vc->vc_cols;
330d21b0be2SNicolas Pitre 			col = p % maxcol;
331d21b0be2SNicolas Pitre 			nr = maxcol - col;
332d21b0be2SNicolas Pitre 			do {
333d21b0be2SNicolas Pitre 				if (nr > this_round/4)
334d21b0be2SNicolas Pitre 					nr = this_round/4;
335708d0bffSNicolas Pitre 				vc_uniscr_copy_line(vc, con_buf0, viewed,
336708d0bffSNicolas Pitre 						    row, col, nr);
337d21b0be2SNicolas Pitre 				con_buf0 += nr * 4;
338d21b0be2SNicolas Pitre 				this_round -= nr * 4;
339d21b0be2SNicolas Pitre 				row++;
340d21b0be2SNicolas Pitre 				col = 0;
341d21b0be2SNicolas Pitre 				nr = maxcol;
342d21b0be2SNicolas Pitre 			} while (this_round);
343d21b0be2SNicolas Pitre 		} else if (!attr) {
34460d4ae8dSGreg Kroah-Hartman 			org = screen_pos(vc, p, viewed);
34560d4ae8dSGreg Kroah-Hartman 			col = p % maxcol;
34660d4ae8dSGreg Kroah-Hartman 			p += maxcol - col;
34760d4ae8dSGreg Kroah-Hartman 			while (this_round-- > 0) {
34860d4ae8dSGreg Kroah-Hartman 				*con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
34960d4ae8dSGreg Kroah-Hartman 				if (++col == maxcol) {
35060d4ae8dSGreg Kroah-Hartman 					org = screen_pos(vc, p, viewed);
35160d4ae8dSGreg Kroah-Hartman 					col = 0;
35260d4ae8dSGreg Kroah-Hartman 					p += maxcol;
35360d4ae8dSGreg Kroah-Hartman 				}
35460d4ae8dSGreg Kroah-Hartman 			}
35560d4ae8dSGreg Kroah-Hartman 		} else {
35660d4ae8dSGreg Kroah-Hartman 			if (p < HEADER_SIZE) {
35760d4ae8dSGreg Kroah-Hartman 				size_t tmp_count;
35860d4ae8dSGreg Kroah-Hartman 
3598a085494SNicolas Pitre 				/* clamp header values if they don't fit */
3608a085494SNicolas Pitre 				con_buf0[0] = min(vc->vc_rows, 0xFFu);
3618a085494SNicolas Pitre 				con_buf0[1] = min(vc->vc_cols, 0xFFu);
36260d4ae8dSGreg Kroah-Hartman 				getconsxy(vc, con_buf0 + 2);
36360d4ae8dSGreg Kroah-Hartman 
36460d4ae8dSGreg Kroah-Hartman 				con_buf_start += p;
36560d4ae8dSGreg Kroah-Hartman 				this_round += p;
36660d4ae8dSGreg Kroah-Hartman 				if (this_round > CON_BUF_SIZE) {
36760d4ae8dSGreg Kroah-Hartman 					this_round = CON_BUF_SIZE;
36860d4ae8dSGreg Kroah-Hartman 					orig_count = this_round - p;
36960d4ae8dSGreg Kroah-Hartman 				}
37060d4ae8dSGreg Kroah-Hartman 
37160d4ae8dSGreg Kroah-Hartman 				tmp_count = HEADER_SIZE;
37260d4ae8dSGreg Kroah-Hartman 				if (tmp_count > this_round)
37360d4ae8dSGreg Kroah-Hartman 					tmp_count = this_round;
37460d4ae8dSGreg Kroah-Hartman 
37560d4ae8dSGreg Kroah-Hartman 				/* Advance state pointers and move on. */
37660d4ae8dSGreg Kroah-Hartman 				this_round -= tmp_count;
37760d4ae8dSGreg Kroah-Hartman 				p = HEADER_SIZE;
37860d4ae8dSGreg Kroah-Hartman 				con_buf0 = con_buf + HEADER_SIZE;
37960d4ae8dSGreg Kroah-Hartman 				/* If this_round >= 0, then p is even... */
38060d4ae8dSGreg Kroah-Hartman 			} else if (p & 1) {
38160d4ae8dSGreg Kroah-Hartman 				/* Skip first byte for output if start address is odd
38260d4ae8dSGreg Kroah-Hartman 				 * Update region sizes up/down depending on free
38360d4ae8dSGreg Kroah-Hartman 				 * space in buffer.
38460d4ae8dSGreg Kroah-Hartman 				 */
38560d4ae8dSGreg Kroah-Hartman 				con_buf_start++;
38660d4ae8dSGreg Kroah-Hartman 				if (this_round < CON_BUF_SIZE)
38760d4ae8dSGreg Kroah-Hartman 					this_round++;
38860d4ae8dSGreg Kroah-Hartman 				else
38960d4ae8dSGreg Kroah-Hartman 					orig_count--;
39060d4ae8dSGreg Kroah-Hartman 			}
39160d4ae8dSGreg Kroah-Hartman 			if (this_round > 0) {
39260d4ae8dSGreg Kroah-Hartman 				unsigned short *tmp_buf = (unsigned short *)con_buf0;
39360d4ae8dSGreg Kroah-Hartman 
39460d4ae8dSGreg Kroah-Hartman 				p -= HEADER_SIZE;
39560d4ae8dSGreg Kroah-Hartman 				p /= 2;
39660d4ae8dSGreg Kroah-Hartman 				col = p % maxcol;
39760d4ae8dSGreg Kroah-Hartman 
39860d4ae8dSGreg Kroah-Hartman 				org = screen_pos(vc, p, viewed);
39960d4ae8dSGreg Kroah-Hartman 				p += maxcol - col;
40060d4ae8dSGreg Kroah-Hartman 
40160d4ae8dSGreg Kroah-Hartman 				/* Buffer has even length, so we can always copy
40260d4ae8dSGreg Kroah-Hartman 				 * character + attribute. We do not copy last byte
40360d4ae8dSGreg Kroah-Hartman 				 * to userspace if this_round is odd.
40460d4ae8dSGreg Kroah-Hartman 				 */
40560d4ae8dSGreg Kroah-Hartman 				this_round = (this_round + 1) >> 1;
40660d4ae8dSGreg Kroah-Hartman 
40760d4ae8dSGreg Kroah-Hartman 				while (this_round) {
40860d4ae8dSGreg Kroah-Hartman 					*tmp_buf++ = vcs_scr_readw(vc, org++);
40960d4ae8dSGreg Kroah-Hartman 					this_round --;
41060d4ae8dSGreg Kroah-Hartman 					if (++col == maxcol) {
41160d4ae8dSGreg Kroah-Hartman 						org = screen_pos(vc, p, viewed);
41260d4ae8dSGreg Kroah-Hartman 						col = 0;
41360d4ae8dSGreg Kroah-Hartman 						p += maxcol;
41460d4ae8dSGreg Kroah-Hartman 					}
41560d4ae8dSGreg Kroah-Hartman 				}
41660d4ae8dSGreg Kroah-Hartman 			}
41760d4ae8dSGreg Kroah-Hartman 		}
41860d4ae8dSGreg Kroah-Hartman 
41960d4ae8dSGreg Kroah-Hartman 		/* Finally, release the console semaphore while we push
42060d4ae8dSGreg Kroah-Hartman 		 * all the data to userspace from our temporary buffer.
42160d4ae8dSGreg Kroah-Hartman 		 *
42260d4ae8dSGreg Kroah-Hartman 		 * AKPM: Even though it's a semaphore, we should drop it because
42360d4ae8dSGreg Kroah-Hartman 		 * the pagefault handling code may want to call printk().
42460d4ae8dSGreg Kroah-Hartman 		 */
42560d4ae8dSGreg Kroah-Hartman 
426ac751efaSTorben Hohn 		console_unlock();
42760d4ae8dSGreg Kroah-Hartman 		ret = copy_to_user(buf, con_buf_start, orig_count);
428ac751efaSTorben Hohn 		console_lock();
42960d4ae8dSGreg Kroah-Hartman 
43060d4ae8dSGreg Kroah-Hartman 		if (ret) {
43160d4ae8dSGreg Kroah-Hartman 			read += (orig_count - ret);
43260d4ae8dSGreg Kroah-Hartman 			ret = -EFAULT;
43360d4ae8dSGreg Kroah-Hartman 			break;
43460d4ae8dSGreg Kroah-Hartman 		}
43560d4ae8dSGreg Kroah-Hartman 		buf += orig_count;
43660d4ae8dSGreg Kroah-Hartman 		pos += orig_count;
43760d4ae8dSGreg Kroah-Hartman 		read += orig_count;
43860d4ae8dSGreg Kroah-Hartman 		count -= orig_count;
43960d4ae8dSGreg Kroah-Hartman 	}
44060d4ae8dSGreg Kroah-Hartman 	*ppos += read;
44160d4ae8dSGreg Kroah-Hartman 	if (read)
44260d4ae8dSGreg Kroah-Hartman 		ret = read;
44360d4ae8dSGreg Kroah-Hartman unlock_out:
444ac751efaSTorben Hohn 	console_unlock();
445fcdba07eSJiri Olsa 	free_page((unsigned long) con_buf);
44660d4ae8dSGreg Kroah-Hartman 	return ret;
44760d4ae8dSGreg Kroah-Hartman }
44860d4ae8dSGreg Kroah-Hartman 
44960d4ae8dSGreg Kroah-Hartman static ssize_t
45060d4ae8dSGreg Kroah-Hartman vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
45160d4ae8dSGreg Kroah-Hartman {
452496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
45360d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc;
45460d4ae8dSGreg Kroah-Hartman 	long pos;
455fcdba07eSJiri Olsa 	long attr, size, written;
45660d4ae8dSGreg Kroah-Hartman 	char *con_buf0;
457*7d62549aSJiri Slaby 	int col, maxcol;
45860d4ae8dSGreg Kroah-Hartman 	u16 *org0 = NULL, *org = NULL;
45960d4ae8dSGreg Kroah-Hartman 	size_t ret;
460fcdba07eSJiri Olsa 	char *con_buf;
461*7d62549aSJiri Slaby 	bool viewed;
46260d4ae8dSGreg Kroah-Hartman 
4630c9acb1aSNicolas Pitre 	if (use_unicode(inode))
4640c9acb1aSNicolas Pitre 		return -EOPNOTSUPP;
4650c9acb1aSNicolas Pitre 
466fcdba07eSJiri Olsa 	con_buf = (char *) __get_free_page(GFP_KERNEL);
467fcdba07eSJiri Olsa 	if (!con_buf)
468fcdba07eSJiri Olsa 		return -ENOMEM;
46960d4ae8dSGreg Kroah-Hartman 
47060d4ae8dSGreg Kroah-Hartman 	pos = *ppos;
47160d4ae8dSGreg Kroah-Hartman 
47260d4ae8dSGreg Kroah-Hartman 	/* Select the proper current console and verify
47360d4ae8dSGreg Kroah-Hartman 	 * sanity of the situation under the console lock.
47460d4ae8dSGreg Kroah-Hartman 	 */
475ac751efaSTorben Hohn 	console_lock();
47660d4ae8dSGreg Kroah-Hartman 
477d21b0be2SNicolas Pitre 	attr = use_attributes(inode);
47860d4ae8dSGreg Kroah-Hartman 	ret = -ENXIO;
479fcdba07eSJiri Olsa 	vc = vcs_vc(inode, &viewed);
480fcdba07eSJiri Olsa 	if (!vc)
48160d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
48260d4ae8dSGreg Kroah-Hartman 
48360d4ae8dSGreg Kroah-Hartman 	size = vcs_size(inode);
48460d4ae8dSGreg Kroah-Hartman 	ret = -EINVAL;
48560d4ae8dSGreg Kroah-Hartman 	if (pos < 0 || pos > size)
48660d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
48760d4ae8dSGreg Kroah-Hartman 	if (count > size - pos)
48860d4ae8dSGreg Kroah-Hartman 		count = size - pos;
48960d4ae8dSGreg Kroah-Hartman 	written = 0;
49060d4ae8dSGreg Kroah-Hartman 	while (count) {
49160d4ae8dSGreg Kroah-Hartman 		long this_round = count;
49260d4ae8dSGreg Kroah-Hartman 		size_t orig_count;
49360d4ae8dSGreg Kroah-Hartman 		long p;
49460d4ae8dSGreg Kroah-Hartman 
49560d4ae8dSGreg Kroah-Hartman 		if (this_round > CON_BUF_SIZE)
49660d4ae8dSGreg Kroah-Hartman 			this_round = CON_BUF_SIZE;
49760d4ae8dSGreg Kroah-Hartman 
49860d4ae8dSGreg Kroah-Hartman 		/* Temporarily drop the console lock so that we can read
49960d4ae8dSGreg Kroah-Hartman 		 * in the write data from userspace safely.
50060d4ae8dSGreg Kroah-Hartman 		 */
501ac751efaSTorben Hohn 		console_unlock();
50260d4ae8dSGreg Kroah-Hartman 		ret = copy_from_user(con_buf, buf, this_round);
503ac751efaSTorben Hohn 		console_lock();
50460d4ae8dSGreg Kroah-Hartman 
50560d4ae8dSGreg Kroah-Hartman 		if (ret) {
50660d4ae8dSGreg Kroah-Hartman 			this_round -= ret;
50760d4ae8dSGreg Kroah-Hartman 			if (!this_round) {
50860d4ae8dSGreg Kroah-Hartman 				/* Abort loop if no data were copied. Otherwise
50960d4ae8dSGreg Kroah-Hartman 				 * fail with -EFAULT.
51060d4ae8dSGreg Kroah-Hartman 				 */
51160d4ae8dSGreg Kroah-Hartman 				if (written)
51260d4ae8dSGreg Kroah-Hartman 					break;
51360d4ae8dSGreg Kroah-Hartman 				ret = -EFAULT;
51460d4ae8dSGreg Kroah-Hartman 				goto unlock_out;
51560d4ae8dSGreg Kroah-Hartman 			}
51660d4ae8dSGreg Kroah-Hartman 		}
51760d4ae8dSGreg Kroah-Hartman 
51860d4ae8dSGreg Kroah-Hartman 		/* The vcs_size might have changed while we slept to grab
51960d4ae8dSGreg Kroah-Hartman 		 * the user buffer, so recheck.
52060d4ae8dSGreg Kroah-Hartman 		 * Return data written up to now on failure.
52160d4ae8dSGreg Kroah-Hartman 		 */
52260d4ae8dSGreg Kroah-Hartman 		size = vcs_size(inode);
523dc1892c4SJiri Olsa 		if (size < 0) {
524dc1892c4SJiri Olsa 			if (written)
525dc1892c4SJiri Olsa 				break;
526dc1892c4SJiri Olsa 			ret = size;
527dc1892c4SJiri Olsa 			goto unlock_out;
528dc1892c4SJiri Olsa 		}
52960d4ae8dSGreg Kroah-Hartman 		if (pos >= size)
53060d4ae8dSGreg Kroah-Hartman 			break;
53160d4ae8dSGreg Kroah-Hartman 		if (this_round > size - pos)
53260d4ae8dSGreg Kroah-Hartman 			this_round = size - pos;
53360d4ae8dSGreg Kroah-Hartman 
53460d4ae8dSGreg Kroah-Hartman 		/* OK, now actually push the write to the console
53560d4ae8dSGreg Kroah-Hartman 		 * under the lock using the local kernel buffer.
53660d4ae8dSGreg Kroah-Hartman 		 */
53760d4ae8dSGreg Kroah-Hartman 
53860d4ae8dSGreg Kroah-Hartman 		con_buf0 = con_buf;
53960d4ae8dSGreg Kroah-Hartman 		orig_count = this_round;
54060d4ae8dSGreg Kroah-Hartman 		maxcol = vc->vc_cols;
54160d4ae8dSGreg Kroah-Hartman 		p = pos;
54260d4ae8dSGreg Kroah-Hartman 		if (!attr) {
54360d4ae8dSGreg Kroah-Hartman 			org0 = org = screen_pos(vc, p, viewed);
54460d4ae8dSGreg Kroah-Hartman 			col = p % maxcol;
54560d4ae8dSGreg Kroah-Hartman 			p += maxcol - col;
54660d4ae8dSGreg Kroah-Hartman 
54760d4ae8dSGreg Kroah-Hartman 			while (this_round > 0) {
54860d4ae8dSGreg Kroah-Hartman 				unsigned char c = *con_buf0++;
54960d4ae8dSGreg Kroah-Hartman 
55060d4ae8dSGreg Kroah-Hartman 				this_round--;
55160d4ae8dSGreg Kroah-Hartman 				vcs_scr_writew(vc,
55260d4ae8dSGreg Kroah-Hartman 					       (vcs_scr_readw(vc, org) & 0xff00) | c, org);
55360d4ae8dSGreg Kroah-Hartman 				org++;
55460d4ae8dSGreg Kroah-Hartman 				if (++col == maxcol) {
55560d4ae8dSGreg Kroah-Hartman 					org = screen_pos(vc, p, viewed);
55660d4ae8dSGreg Kroah-Hartman 					col = 0;
55760d4ae8dSGreg Kroah-Hartman 					p += maxcol;
55860d4ae8dSGreg Kroah-Hartman 				}
55960d4ae8dSGreg Kroah-Hartman 			}
56060d4ae8dSGreg Kroah-Hartman 		} else {
56160d4ae8dSGreg Kroah-Hartman 			if (p < HEADER_SIZE) {
56260d4ae8dSGreg Kroah-Hartman 				char header[HEADER_SIZE];
56360d4ae8dSGreg Kroah-Hartman 
56460d4ae8dSGreg Kroah-Hartman 				getconsxy(vc, header + 2);
56560d4ae8dSGreg Kroah-Hartman 				while (p < HEADER_SIZE && this_round > 0) {
56660d4ae8dSGreg Kroah-Hartman 					this_round--;
56760d4ae8dSGreg Kroah-Hartman 					header[p++] = *con_buf0++;
56860d4ae8dSGreg Kroah-Hartman 				}
56960d4ae8dSGreg Kroah-Hartman 				if (!viewed)
57060d4ae8dSGreg Kroah-Hartman 					putconsxy(vc, header + 2);
57160d4ae8dSGreg Kroah-Hartman 			}
57260d4ae8dSGreg Kroah-Hartman 			p -= HEADER_SIZE;
57360d4ae8dSGreg Kroah-Hartman 			col = (p/2) % maxcol;
57460d4ae8dSGreg Kroah-Hartman 			if (this_round > 0) {
57560d4ae8dSGreg Kroah-Hartman 				org0 = org = screen_pos(vc, p/2, viewed);
57660d4ae8dSGreg Kroah-Hartman 				if ((p & 1) && this_round > 0) {
57760d4ae8dSGreg Kroah-Hartman 					char c;
57860d4ae8dSGreg Kroah-Hartman 
57960d4ae8dSGreg Kroah-Hartman 					this_round--;
58060d4ae8dSGreg Kroah-Hartman 					c = *con_buf0++;
58160d4ae8dSGreg Kroah-Hartman #ifdef __BIG_ENDIAN
58260d4ae8dSGreg Kroah-Hartman 					vcs_scr_writew(vc, c |
58360d4ae8dSGreg Kroah-Hartman 					     (vcs_scr_readw(vc, org) & 0xff00), org);
58460d4ae8dSGreg Kroah-Hartman #else
58560d4ae8dSGreg Kroah-Hartman 					vcs_scr_writew(vc, (c << 8) |
58660d4ae8dSGreg Kroah-Hartman 					     (vcs_scr_readw(vc, org) & 0xff), org);
58760d4ae8dSGreg Kroah-Hartman #endif
58860d4ae8dSGreg Kroah-Hartman 					org++;
58960d4ae8dSGreg Kroah-Hartman 					p++;
59060d4ae8dSGreg Kroah-Hartman 					if (++col == maxcol) {
59160d4ae8dSGreg Kroah-Hartman 						org = screen_pos(vc, p/2, viewed);
59260d4ae8dSGreg Kroah-Hartman 						col = 0;
59360d4ae8dSGreg Kroah-Hartman 					}
59460d4ae8dSGreg Kroah-Hartman 				}
59560d4ae8dSGreg Kroah-Hartman 				p /= 2;
59660d4ae8dSGreg Kroah-Hartman 				p += maxcol - col;
59760d4ae8dSGreg Kroah-Hartman 			}
59860d4ae8dSGreg Kroah-Hartman 			while (this_round > 1) {
59960d4ae8dSGreg Kroah-Hartman 				unsigned short w;
60060d4ae8dSGreg Kroah-Hartman 
60160d4ae8dSGreg Kroah-Hartman 				w = get_unaligned(((unsigned short *)con_buf0));
60260d4ae8dSGreg Kroah-Hartman 				vcs_scr_writew(vc, w, org++);
60360d4ae8dSGreg Kroah-Hartman 				con_buf0 += 2;
60460d4ae8dSGreg Kroah-Hartman 				this_round -= 2;
60560d4ae8dSGreg Kroah-Hartman 				if (++col == maxcol) {
60660d4ae8dSGreg Kroah-Hartman 					org = screen_pos(vc, p, viewed);
60760d4ae8dSGreg Kroah-Hartman 					col = 0;
60860d4ae8dSGreg Kroah-Hartman 					p += maxcol;
60960d4ae8dSGreg Kroah-Hartman 				}
61060d4ae8dSGreg Kroah-Hartman 			}
61160d4ae8dSGreg Kroah-Hartman 			if (this_round > 0) {
61260d4ae8dSGreg Kroah-Hartman 				unsigned char c;
61360d4ae8dSGreg Kroah-Hartman 
61460d4ae8dSGreg Kroah-Hartman 				c = *con_buf0++;
61560d4ae8dSGreg Kroah-Hartman #ifdef __BIG_ENDIAN
61660d4ae8dSGreg Kroah-Hartman 				vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
61760d4ae8dSGreg Kroah-Hartman #else
61860d4ae8dSGreg Kroah-Hartman 				vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
61960d4ae8dSGreg Kroah-Hartman #endif
62060d4ae8dSGreg Kroah-Hartman 			}
62160d4ae8dSGreg Kroah-Hartman 		}
62260d4ae8dSGreg Kroah-Hartman 		count -= orig_count;
62360d4ae8dSGreg Kroah-Hartman 		written += orig_count;
62460d4ae8dSGreg Kroah-Hartman 		buf += orig_count;
62560d4ae8dSGreg Kroah-Hartman 		pos += orig_count;
62660d4ae8dSGreg Kroah-Hartman 		if (org0)
62760d4ae8dSGreg Kroah-Hartman 			update_region(vc, (unsigned long)(org0), org - org0);
62860d4ae8dSGreg Kroah-Hartman 	}
62960d4ae8dSGreg Kroah-Hartman 	*ppos += written;
63060d4ae8dSGreg Kroah-Hartman 	ret = written;
63160d4ae8dSGreg Kroah-Hartman 	if (written)
63260d4ae8dSGreg Kroah-Hartman 		vcs_scr_updated(vc);
63360d4ae8dSGreg Kroah-Hartman 
63460d4ae8dSGreg Kroah-Hartman unlock_out:
635ac751efaSTorben Hohn 	console_unlock();
636fcdba07eSJiri Olsa 	free_page((unsigned long) con_buf);
63760d4ae8dSGreg Kroah-Hartman 	return ret;
63860d4ae8dSGreg Kroah-Hartman }
63960d4ae8dSGreg Kroah-Hartman 
640afc9a42bSAl Viro static __poll_t
64160d4ae8dSGreg Kroah-Hartman vcs_poll(struct file *file, poll_table *wait)
64260d4ae8dSGreg Kroah-Hartman {
64360d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = vcs_poll_data_get(file);
6441bf931abSNicolas Pitre 	__poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
64560d4ae8dSGreg Kroah-Hartman 
64660d4ae8dSGreg Kroah-Hartman 	if (poll) {
64760d4ae8dSGreg Kroah-Hartman 		poll_wait(file, &poll->waitq, wait);
6481bf931abSNicolas Pitre 		switch (poll->event) {
6491bf931abSNicolas Pitre 		case VT_UPDATE:
6501bf931abSNicolas Pitre 			ret = DEFAULT_POLLMASK|EPOLLPRI;
6511bf931abSNicolas Pitre 			break;
6521bf931abSNicolas Pitre 		case VT_DEALLOCATE:
6531bf931abSNicolas Pitre 			ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
6541bf931abSNicolas Pitre 			break;
6551bf931abSNicolas Pitre 		case 0:
65647c344d0SNicolas Pitre 			ret = DEFAULT_POLLMASK;
6571bf931abSNicolas Pitre 			break;
6581bf931abSNicolas Pitre 		}
65960d4ae8dSGreg Kroah-Hartman 	}
66060d4ae8dSGreg Kroah-Hartman 	return ret;
66160d4ae8dSGreg Kroah-Hartman }
66260d4ae8dSGreg Kroah-Hartman 
66360d4ae8dSGreg Kroah-Hartman static int
66460d4ae8dSGreg Kroah-Hartman vcs_fasync(int fd, struct file *file, int on)
66560d4ae8dSGreg Kroah-Hartman {
66660d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = file->private_data;
66760d4ae8dSGreg Kroah-Hartman 
66860d4ae8dSGreg Kroah-Hartman 	if (!poll) {
66960d4ae8dSGreg Kroah-Hartman 		/* don't allocate anything if all we want is disable fasync */
67060d4ae8dSGreg Kroah-Hartman 		if (!on)
67160d4ae8dSGreg Kroah-Hartman 			return 0;
67260d4ae8dSGreg Kroah-Hartman 		poll = vcs_poll_data_get(file);
67360d4ae8dSGreg Kroah-Hartman 		if (!poll)
67460d4ae8dSGreg Kroah-Hartman 			return -ENOMEM;
67560d4ae8dSGreg Kroah-Hartman 	}
67660d4ae8dSGreg Kroah-Hartman 
67760d4ae8dSGreg Kroah-Hartman 	return fasync_helper(fd, file, on, &poll->fasync);
67860d4ae8dSGreg Kroah-Hartman }
67960d4ae8dSGreg Kroah-Hartman 
68060d4ae8dSGreg Kroah-Hartman static int
68160d4ae8dSGreg Kroah-Hartman vcs_open(struct inode *inode, struct file *filp)
68260d4ae8dSGreg Kroah-Hartman {
683d21b0be2SNicolas Pitre 	unsigned int currcons = console(inode);
684d21b0be2SNicolas Pitre 	bool attr = use_attributes(inode);
685d21b0be2SNicolas Pitre 	bool uni_mode = use_unicode(inode);
68660d4ae8dSGreg Kroah-Hartman 	int ret = 0;
68760d4ae8dSGreg Kroah-Hartman 
688d21b0be2SNicolas Pitre 	/* we currently don't support attributes in unicode mode */
689d21b0be2SNicolas Pitre 	if (attr && uni_mode)
690d21b0be2SNicolas Pitre 		return -EOPNOTSUPP;
691d21b0be2SNicolas Pitre 
6924001d7b7SAlan Cox 	console_lock();
69360d4ae8dSGreg Kroah-Hartman 	if(currcons && !vc_cons_allocated(currcons-1))
69460d4ae8dSGreg Kroah-Hartman 		ret = -ENXIO;
6954001d7b7SAlan Cox 	console_unlock();
69660d4ae8dSGreg Kroah-Hartman 	return ret;
69760d4ae8dSGreg Kroah-Hartman }
69860d4ae8dSGreg Kroah-Hartman 
69960d4ae8dSGreg Kroah-Hartman static int vcs_release(struct inode *inode, struct file *file)
70060d4ae8dSGreg Kroah-Hartman {
70160d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = file->private_data;
70260d4ae8dSGreg Kroah-Hartman 
70360d4ae8dSGreg Kroah-Hartman 	if (poll)
70460d4ae8dSGreg Kroah-Hartman 		vcs_poll_data_free(poll);
70560d4ae8dSGreg Kroah-Hartman 	return 0;
70660d4ae8dSGreg Kroah-Hartman }
70760d4ae8dSGreg Kroah-Hartman 
70860d4ae8dSGreg Kroah-Hartman static const struct file_operations vcs_fops = {
70960d4ae8dSGreg Kroah-Hartman 	.llseek		= vcs_lseek,
71060d4ae8dSGreg Kroah-Hartman 	.read		= vcs_read,
71160d4ae8dSGreg Kroah-Hartman 	.write		= vcs_write,
71260d4ae8dSGreg Kroah-Hartman 	.poll		= vcs_poll,
71360d4ae8dSGreg Kroah-Hartman 	.fasync		= vcs_fasync,
71460d4ae8dSGreg Kroah-Hartman 	.open		= vcs_open,
71560d4ae8dSGreg Kroah-Hartman 	.release	= vcs_release,
71660d4ae8dSGreg Kroah-Hartman };
71760d4ae8dSGreg Kroah-Hartman 
71860d4ae8dSGreg Kroah-Hartman static struct class *vc_class;
71960d4ae8dSGreg Kroah-Hartman 
72060d4ae8dSGreg Kroah-Hartman void vcs_make_sysfs(int index)
72160d4ae8dSGreg Kroah-Hartman {
72260d4ae8dSGreg Kroah-Hartman 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
72360d4ae8dSGreg Kroah-Hartman 		      "vcs%u", index + 1);
724d21b0be2SNicolas Pitre 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL,
725d21b0be2SNicolas Pitre 		      "vcsu%u", index + 1);
72660d4ae8dSGreg Kroah-Hartman 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
72760d4ae8dSGreg Kroah-Hartman 		      "vcsa%u", index + 1);
72860d4ae8dSGreg Kroah-Hartman }
72960d4ae8dSGreg Kroah-Hartman 
73060d4ae8dSGreg Kroah-Hartman void vcs_remove_sysfs(int index)
73160d4ae8dSGreg Kroah-Hartman {
73260d4ae8dSGreg Kroah-Hartman 	device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
733d21b0be2SNicolas Pitre 	device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65));
73460d4ae8dSGreg Kroah-Hartman 	device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
73560d4ae8dSGreg Kroah-Hartman }
73660d4ae8dSGreg Kroah-Hartman 
73760d4ae8dSGreg Kroah-Hartman int __init vcs_init(void)
73860d4ae8dSGreg Kroah-Hartman {
73960d4ae8dSGreg Kroah-Hartman 	unsigned int i;
74060d4ae8dSGreg Kroah-Hartman 
74160d4ae8dSGreg Kroah-Hartman 	if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
74260d4ae8dSGreg Kroah-Hartman 		panic("unable to get major %d for vcs device", VCS_MAJOR);
74360d4ae8dSGreg Kroah-Hartman 	vc_class = class_create(THIS_MODULE, "vc");
74460d4ae8dSGreg Kroah-Hartman 
74560d4ae8dSGreg Kroah-Hartman 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
746d21b0be2SNicolas Pitre 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
74760d4ae8dSGreg Kroah-Hartman 	device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
74860d4ae8dSGreg Kroah-Hartman 	for (i = 0; i < MIN_NR_CONSOLES; i++)
74960d4ae8dSGreg Kroah-Hartman 		vcs_make_sysfs(i);
75060d4ae8dSGreg Kroah-Hartman 	return 0;
75160d4ae8dSGreg Kroah-Hartman }
752