xref: /linux/drivers/tty/vt/vc_screen.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
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>
51*5f60d5f6SAl Viro #include <linux/unaligned.h>
5260d4ae8dSGreg Kroah-Hartman 
536d507c75SJiri Slaby #define HEADER_SIZE	4u
54b3e90f37SYoann Congal #define CON_BUF_SIZE (IS_ENABLED(CONFIG_BASE_SMALL) ? 256 : PAGE_SIZE)
55fcdba07eSJiri Olsa 
56d21b0be2SNicolas Pitre /*
57d21b0be2SNicolas Pitre  * Our minor space:
58d21b0be2SNicolas Pitre  *
59d21b0be2SNicolas Pitre  *   0 ... 63	glyph mode without attributes
60d21b0be2SNicolas Pitre  *  64 ... 127	unicode mode without attributes
61d21b0be2SNicolas Pitre  * 128 ... 191	glyph mode with attributes
62d21b0be2SNicolas Pitre  * 192 ... 255	unused (reserved for unicode with attributes)
63d21b0be2SNicolas Pitre  *
64d21b0be2SNicolas Pitre  * This relies on MAX_NR_CONSOLES being  <= 63, meaning 63 actual consoles
65d21b0be2SNicolas Pitre  * with minors 0, 64, 128 and 192 being proxies for the foreground console.
66d21b0be2SNicolas Pitre  */
67d21b0be2SNicolas Pitre #if MAX_NR_CONSOLES > 63
68d21b0be2SNicolas Pitre #warning "/dev/vcs* devices may not accommodate more than 63 consoles"
69d21b0be2SNicolas Pitre #endif
70d21b0be2SNicolas Pitre 
71d21b0be2SNicolas Pitre #define console(inode)		(iminor(inode) & 63)
72d21b0be2SNicolas Pitre #define use_unicode(inode)	(iminor(inode) & 64)
73d21b0be2SNicolas Pitre #define use_attributes(inode)	(iminor(inode) & 128)
74d21b0be2SNicolas Pitre 
75d21b0be2SNicolas Pitre 
7660d4ae8dSGreg Kroah-Hartman struct vcs_poll_data {
7760d4ae8dSGreg Kroah-Hartman 	struct notifier_block notifier;
7860d4ae8dSGreg Kroah-Hartman 	unsigned int cons_num;
791bf931abSNicolas Pitre 	int event;
8060d4ae8dSGreg Kroah-Hartman 	wait_queue_head_t waitq;
8160d4ae8dSGreg Kroah-Hartman 	struct fasync_struct *fasync;
8260d4ae8dSGreg Kroah-Hartman };
8360d4ae8dSGreg Kroah-Hartman 
8460d4ae8dSGreg Kroah-Hartman static int
vcs_notifier(struct notifier_block * nb,unsigned long code,void * _param)8560d4ae8dSGreg Kroah-Hartman vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
8660d4ae8dSGreg Kroah-Hartman {
8760d4ae8dSGreg Kroah-Hartman 	struct vt_notifier_param *param = _param;
8860d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = param->vc;
8960d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll =
9060d4ae8dSGreg Kroah-Hartman 		container_of(nb, struct vcs_poll_data, notifier);
9160d4ae8dSGreg Kroah-Hartman 	int currcons = poll->cons_num;
92fad08b20SNicolas Pitre 	int fa_band;
9360d4ae8dSGreg Kroah-Hartman 
94fad08b20SNicolas Pitre 	switch (code) {
95fad08b20SNicolas Pitre 	case VT_UPDATE:
96fad08b20SNicolas Pitre 		fa_band = POLL_PRI;
97fad08b20SNicolas Pitre 		break;
98fad08b20SNicolas Pitre 	case VT_DEALLOCATE:
99fad08b20SNicolas Pitre 		fa_band = POLL_HUP;
100fad08b20SNicolas Pitre 		break;
101fad08b20SNicolas Pitre 	default:
10260d4ae8dSGreg Kroah-Hartman 		return NOTIFY_DONE;
103fad08b20SNicolas Pitre 	}
10460d4ae8dSGreg Kroah-Hartman 
10560d4ae8dSGreg Kroah-Hartman 	if (currcons == 0)
10660d4ae8dSGreg Kroah-Hartman 		currcons = fg_console;
10760d4ae8dSGreg Kroah-Hartman 	else
10860d4ae8dSGreg Kroah-Hartman 		currcons--;
10960d4ae8dSGreg Kroah-Hartman 	if (currcons != vc->vc_num)
11060d4ae8dSGreg Kroah-Hartman 		return NOTIFY_DONE;
11160d4ae8dSGreg Kroah-Hartman 
1121bf931abSNicolas Pitre 	poll->event = code;
11360d4ae8dSGreg Kroah-Hartman 	wake_up_interruptible(&poll->waitq);
114fad08b20SNicolas Pitre 	kill_fasync(&poll->fasync, SIGIO, fa_band);
11560d4ae8dSGreg Kroah-Hartman 	return NOTIFY_OK;
11660d4ae8dSGreg Kroah-Hartman }
11760d4ae8dSGreg Kroah-Hartman 
11860d4ae8dSGreg Kroah-Hartman static void
vcs_poll_data_free(struct vcs_poll_data * poll)11960d4ae8dSGreg Kroah-Hartman vcs_poll_data_free(struct vcs_poll_data *poll)
12060d4ae8dSGreg Kroah-Hartman {
12160d4ae8dSGreg Kroah-Hartman 	unregister_vt_notifier(&poll->notifier);
12260d4ae8dSGreg Kroah-Hartman 	kfree(poll);
12360d4ae8dSGreg Kroah-Hartman }
12460d4ae8dSGreg Kroah-Hartman 
12560d4ae8dSGreg Kroah-Hartman static struct vcs_poll_data *
vcs_poll_data_get(struct file * file)12660d4ae8dSGreg Kroah-Hartman vcs_poll_data_get(struct file *file)
12760d4ae8dSGreg Kroah-Hartman {
128e8cd8169SAl Viro 	struct vcs_poll_data *poll = file->private_data, *kill = NULL;
12960d4ae8dSGreg Kroah-Hartman 
13060d4ae8dSGreg Kroah-Hartman 	if (poll)
13160d4ae8dSGreg Kroah-Hartman 		return poll;
13260d4ae8dSGreg Kroah-Hartman 
13360d4ae8dSGreg Kroah-Hartman 	poll = kzalloc(sizeof(*poll), GFP_KERNEL);
13460d4ae8dSGreg Kroah-Hartman 	if (!poll)
13560d4ae8dSGreg Kroah-Hartman 		return NULL;
136d21b0be2SNicolas Pitre 	poll->cons_num = console(file_inode(file));
13760d4ae8dSGreg Kroah-Hartman 	init_waitqueue_head(&poll->waitq);
13860d4ae8dSGreg Kroah-Hartman 	poll->notifier.notifier_call = vcs_notifier;
13995252f9cSNicolas Pitre 	/*
14095252f9cSNicolas Pitre 	 * In order not to lose any update event, we must pretend one might
14195252f9cSNicolas Pitre 	 * have occurred before we have a chance to register our notifier.
14295252f9cSNicolas Pitre 	 * This is also how user space has come to detect which kernels
14395252f9cSNicolas Pitre 	 * support POLLPRI on /dev/vcs* devices i.e. using poll() with
14495252f9cSNicolas Pitre 	 * POLLPRI and a zero timeout.
14595252f9cSNicolas Pitre 	 */
14695252f9cSNicolas Pitre 	poll->event = VT_UPDATE;
14795252f9cSNicolas Pitre 
14860d4ae8dSGreg Kroah-Hartman 	if (register_vt_notifier(&poll->notifier) != 0) {
14960d4ae8dSGreg Kroah-Hartman 		kfree(poll);
15060d4ae8dSGreg Kroah-Hartman 		return NULL;
15160d4ae8dSGreg Kroah-Hartman 	}
15260d4ae8dSGreg Kroah-Hartman 
15360d4ae8dSGreg Kroah-Hartman 	/*
15460d4ae8dSGreg Kroah-Hartman 	 * This code may be called either through ->poll() or ->fasync().
15560d4ae8dSGreg Kroah-Hartman 	 * If we have two threads using the same file descriptor, they could
15660d4ae8dSGreg Kroah-Hartman 	 * both enter this function, both notice that the structure hasn't
15760d4ae8dSGreg Kroah-Hartman 	 * been allocated yet and go ahead allocating it in parallel, but
15860d4ae8dSGreg Kroah-Hartman 	 * only one of them must survive and be shared otherwise we'd leak
15960d4ae8dSGreg Kroah-Hartman 	 * memory with a dangling notifier callback.
16060d4ae8dSGreg Kroah-Hartman 	 */
16160d4ae8dSGreg Kroah-Hartman 	spin_lock(&file->f_lock);
16260d4ae8dSGreg Kroah-Hartman 	if (!file->private_data) {
16360d4ae8dSGreg Kroah-Hartman 		file->private_data = poll;
16460d4ae8dSGreg Kroah-Hartman 	} else {
16560d4ae8dSGreg Kroah-Hartman 		/* someone else raced ahead of us */
166e8cd8169SAl Viro 		kill = poll;
16760d4ae8dSGreg Kroah-Hartman 		poll = file->private_data;
16860d4ae8dSGreg Kroah-Hartman 	}
16960d4ae8dSGreg Kroah-Hartman 	spin_unlock(&file->f_lock);
170e8cd8169SAl Viro 	if (kill)
171e8cd8169SAl Viro 		vcs_poll_data_free(kill);
17260d4ae8dSGreg Kroah-Hartman 
17360d4ae8dSGreg Kroah-Hartman 	return poll;
17460d4ae8dSGreg Kroah-Hartman }
17560d4ae8dSGreg Kroah-Hartman 
1767d62549aSJiri Slaby /**
177c38f45efSJiri Slaby (SUSE)  * vcs_vc - return VC for @inode
1787d62549aSJiri Slaby  * @inode: inode for which to return a VC
1797d62549aSJiri Slaby  * @viewed: returns whether this console is currently foreground (viewed)
1807d62549aSJiri Slaby  *
181fcdba07eSJiri Olsa  * Must be called with console_lock.
182fcdba07eSJiri Olsa  */
vcs_vc(struct inode * inode,bool * viewed)1837d62549aSJiri Slaby static struct vc_data *vcs_vc(struct inode *inode, bool *viewed)
184fcdba07eSJiri Olsa {
185d21b0be2SNicolas Pitre 	unsigned int currcons = console(inode);
186fcdba07eSJiri Olsa 
187fcdba07eSJiri Olsa 	WARN_CONSOLE_UNLOCKED();
188fcdba07eSJiri Olsa 
189fcdba07eSJiri Olsa 	if (currcons == 0) {
190fcdba07eSJiri Olsa 		currcons = fg_console;
191fcdba07eSJiri Olsa 		if (viewed)
1927d62549aSJiri Slaby 			*viewed = true;
193fcdba07eSJiri Olsa 	} else {
194fcdba07eSJiri Olsa 		currcons--;
195fcdba07eSJiri Olsa 		if (viewed)
1967d62549aSJiri Slaby 			*viewed = false;
197fcdba07eSJiri Olsa 	}
198fcdba07eSJiri Olsa 	return vc_cons[currcons].d;
199fcdba07eSJiri Olsa }
200fcdba07eSJiri Olsa 
20171d4abfaSJiri Slaby /**
202c38f45efSJiri Slaby (SUSE)  * vcs_size - return size for a VC in @vc
20371d4abfaSJiri Slaby  * @vc: which VC
20471d4abfaSJiri Slaby  * @attr: does it use attributes?
20571d4abfaSJiri Slaby  * @unicode: is it unicode?
20671d4abfaSJiri Slaby  *
207fcdba07eSJiri Olsa  * Must be called with console_lock.
208fcdba07eSJiri Olsa  */
vcs_size(const struct vc_data * vc,bool attr,bool unicode)20971d4abfaSJiri Slaby static int vcs_size(const struct vc_data *vc, bool attr, bool unicode)
21060d4ae8dSGreg Kroah-Hartman {
21160d4ae8dSGreg Kroah-Hartman 	int size;
21260d4ae8dSGreg Kroah-Hartman 
213fcdba07eSJiri Olsa 	WARN_CONSOLE_UNLOCKED();
214fcdba07eSJiri Olsa 
21560d4ae8dSGreg Kroah-Hartman 	size = vc->vc_rows * vc->vc_cols;
21660d4ae8dSGreg Kroah-Hartman 
21771d4abfaSJiri Slaby 	if (attr) {
21871d4abfaSJiri Slaby 		if (unicode)
219d21b0be2SNicolas Pitre 			return -EOPNOTSUPP;
22071d4abfaSJiri Slaby 
22160d4ae8dSGreg Kroah-Hartman 		size = 2 * size + HEADER_SIZE;
22271d4abfaSJiri Slaby 	} else if (unicode)
223d21b0be2SNicolas Pitre 		size *= 4;
22471d4abfaSJiri Slaby 
22560d4ae8dSGreg Kroah-Hartman 	return size;
22660d4ae8dSGreg Kroah-Hartman }
22760d4ae8dSGreg Kroah-Hartman 
vcs_lseek(struct file * file,loff_t offset,int orig)22860d4ae8dSGreg Kroah-Hartman static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
22960d4ae8dSGreg Kroah-Hartman {
23071d4abfaSJiri Slaby 	struct inode *inode = file_inode(file);
23171d4abfaSJiri Slaby 	struct vc_data *vc;
23260d4ae8dSGreg Kroah-Hartman 	int size;
23360d4ae8dSGreg Kroah-Hartman 
234dc1892c4SJiri Olsa 	console_lock();
23571d4abfaSJiri Slaby 	vc = vcs_vc(inode, NULL);
23671d4abfaSJiri Slaby 	if (!vc) {
23771d4abfaSJiri Slaby 		console_unlock();
23871d4abfaSJiri Slaby 		return -ENXIO;
23971d4abfaSJiri Slaby 	}
24071d4abfaSJiri Slaby 
24171d4abfaSJiri Slaby 	size = vcs_size(vc, use_attributes(inode), use_unicode(inode));
242dc1892c4SJiri Olsa 	console_unlock();
243fcdba07eSJiri Olsa 	if (size < 0)
244dc1892c4SJiri Olsa 		return size;
24565004276SAl Viro 	return fixed_size_llseek(file, offset, orig, size);
24660d4ae8dSGreg Kroah-Hartman }
24760d4ae8dSGreg Kroah-Hartman 
vcs_read_buf_uni(struct vc_data * vc,char * con_buf,unsigned int pos,unsigned int count,bool viewed)2480f66eee3SJiri Slaby static int vcs_read_buf_uni(struct vc_data *vc, char *con_buf,
2490f66eee3SJiri Slaby 		unsigned int pos, unsigned int count, bool viewed)
2500f66eee3SJiri Slaby {
2510f66eee3SJiri Slaby 	unsigned int nr, row, col, maxcol = vc->vc_cols;
2520f66eee3SJiri Slaby 	int ret;
2530f66eee3SJiri Slaby 
2540f66eee3SJiri Slaby 	ret = vc_uniscr_check(vc);
2550f66eee3SJiri Slaby 	if (ret)
2560f66eee3SJiri Slaby 		return ret;
2570f66eee3SJiri Slaby 
2580f66eee3SJiri Slaby 	pos /= 4;
2590f66eee3SJiri Slaby 	row = pos / maxcol;
2600f66eee3SJiri Slaby 	col = pos % maxcol;
2610f66eee3SJiri Slaby 	nr = maxcol - col;
2620f66eee3SJiri Slaby 	do {
2630f66eee3SJiri Slaby 		if (nr > count / 4)
2640f66eee3SJiri Slaby 			nr = count / 4;
2650f66eee3SJiri Slaby 		vc_uniscr_copy_line(vc, con_buf, viewed, row, col, nr);
2660f66eee3SJiri Slaby 		con_buf += nr * 4;
2670f66eee3SJiri Slaby 		count -= nr * 4;
2680f66eee3SJiri Slaby 		row++;
2690f66eee3SJiri Slaby 		col = 0;
2700f66eee3SJiri Slaby 		nr = maxcol;
2710f66eee3SJiri Slaby 	} while (count);
2720f66eee3SJiri Slaby 
2730f66eee3SJiri Slaby 	return 0;
2740f66eee3SJiri Slaby }
27560d4ae8dSGreg Kroah-Hartman 
vcs_read_buf_noattr(const struct vc_data * vc,char * con_buf,unsigned int pos,unsigned int count,bool viewed)2765a52baaaSJiri Slaby static void vcs_read_buf_noattr(const struct vc_data *vc, char *con_buf,
2775a52baaaSJiri Slaby 		unsigned int pos, unsigned int count, bool viewed)
2785a52baaaSJiri Slaby {
2795a52baaaSJiri Slaby 	u16 *org;
2805a52baaaSJiri Slaby 	unsigned int col, maxcol = vc->vc_cols;
2815a52baaaSJiri Slaby 
2825a52baaaSJiri Slaby 	org = screen_pos(vc, pos, viewed);
2835a52baaaSJiri Slaby 	col = pos % maxcol;
2845a52baaaSJiri Slaby 	pos += maxcol - col;
2855a52baaaSJiri Slaby 
2865a52baaaSJiri Slaby 	while (count-- > 0) {
2875a52baaaSJiri Slaby 		*con_buf++ = (vcs_scr_readw(vc, org++) & 0xff);
2885a52baaaSJiri Slaby 		if (++col == maxcol) {
2895a52baaaSJiri Slaby 			org = screen_pos(vc, pos, viewed);
2905a52baaaSJiri Slaby 			col = 0;
2915a52baaaSJiri Slaby 			pos += maxcol;
2925a52baaaSJiri Slaby 		}
2935a52baaaSJiri Slaby 	}
2945a52baaaSJiri Slaby }
2955a52baaaSJiri Slaby 
vcs_read_buf(const struct vc_data * vc,char * con_buf,unsigned int pos,unsigned int count,bool viewed,unsigned int * skip)2966a6b76ccSJiri Slaby static unsigned int vcs_read_buf(const struct vc_data *vc, char *con_buf,
2976a6b76ccSJiri Slaby 		unsigned int pos, unsigned int count, bool viewed,
2986a6b76ccSJiri Slaby 		unsigned int *skip)
2996a6b76ccSJiri Slaby {
3006a6b76ccSJiri Slaby 	u16 *org, *con_buf16;
3016a6b76ccSJiri Slaby 	unsigned int col, maxcol = vc->vc_cols;
3026a6b76ccSJiri Slaby 	unsigned int filled = count;
3036a6b76ccSJiri Slaby 
3046a6b76ccSJiri Slaby 	if (pos < HEADER_SIZE) {
30552c3c3a5SJiri Slaby 		/* clamp header values if they don't fit */
30652c3c3a5SJiri Slaby 		con_buf[0] = min(vc->vc_rows, 0xFFu);
30752c3c3a5SJiri Slaby 		con_buf[1] = min(vc->vc_cols, 0xFFu);
30852c3c3a5SJiri Slaby 		getconsxy(vc, con_buf + 2);
3096a6b76ccSJiri Slaby 
31052c3c3a5SJiri Slaby 		*skip += pos;
31152c3c3a5SJiri Slaby 		count += pos;
31252c3c3a5SJiri Slaby 		if (count > CON_BUF_SIZE) {
31352c3c3a5SJiri Slaby 			count = CON_BUF_SIZE;
31452c3c3a5SJiri Slaby 			filled = count - pos;
31552c3c3a5SJiri Slaby 		}
31652c3c3a5SJiri Slaby 
31752c3c3a5SJiri Slaby 		/* Advance state pointers and move on. */
31852c3c3a5SJiri Slaby 		count -= min(HEADER_SIZE, count);
3196a6b76ccSJiri Slaby 		pos = HEADER_SIZE;
3206a6b76ccSJiri Slaby 		con_buf += HEADER_SIZE;
3216a6b76ccSJiri Slaby 		/* If count >= 0, then pos is even... */
3226a6b76ccSJiri Slaby 	} else if (pos & 1) {
3236a6b76ccSJiri Slaby 		/*
3246a6b76ccSJiri Slaby 		 * Skip first byte for output if start address is odd. Update
3256a6b76ccSJiri Slaby 		 * region sizes up/down depending on free space in buffer.
3266a6b76ccSJiri Slaby 		 */
3276a6b76ccSJiri Slaby 		(*skip)++;
3286a6b76ccSJiri Slaby 		if (count < CON_BUF_SIZE)
3296a6b76ccSJiri Slaby 			count++;
3306a6b76ccSJiri Slaby 		else
3316a6b76ccSJiri Slaby 			filled--;
3326a6b76ccSJiri Slaby 	}
3336a6b76ccSJiri Slaby 
3346a6b76ccSJiri Slaby 	if (!count)
3356a6b76ccSJiri Slaby 		return filled;
3366a6b76ccSJiri Slaby 
3376a6b76ccSJiri Slaby 	pos -= HEADER_SIZE;
3386a6b76ccSJiri Slaby 	pos /= 2;
3396a6b76ccSJiri Slaby 	col = pos % maxcol;
3406a6b76ccSJiri Slaby 
3416a6b76ccSJiri Slaby 	org = screen_pos(vc, pos, viewed);
3426a6b76ccSJiri Slaby 	pos += maxcol - col;
3436a6b76ccSJiri Slaby 
3446a6b76ccSJiri Slaby 	/*
3456a6b76ccSJiri Slaby 	 * Buffer has even length, so we can always copy character + attribute.
3466a6b76ccSJiri Slaby 	 * We do not copy last byte to userspace if count is odd.
3476a6b76ccSJiri Slaby 	 */
3486a6b76ccSJiri Slaby 	count = (count + 1) / 2;
3496a6b76ccSJiri Slaby 	con_buf16 = (u16 *)con_buf;
3506a6b76ccSJiri Slaby 
3516a6b76ccSJiri Slaby 	while (count) {
3526a6b76ccSJiri Slaby 		*con_buf16++ = vcs_scr_readw(vc, org++);
3536a6b76ccSJiri Slaby 		count--;
3546a6b76ccSJiri Slaby 		if (++col == maxcol) {
3556a6b76ccSJiri Slaby 			org = screen_pos(vc, pos, viewed);
3566a6b76ccSJiri Slaby 			col = 0;
3576a6b76ccSJiri Slaby 			pos += maxcol;
3586a6b76ccSJiri Slaby 		}
3596a6b76ccSJiri Slaby 	}
3606a6b76ccSJiri Slaby 
3616a6b76ccSJiri Slaby 	return filled;
3626a6b76ccSJiri Slaby }
3636a6b76ccSJiri Slaby 
36460d4ae8dSGreg Kroah-Hartman static ssize_t
vcs_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)36560d4ae8dSGreg Kroah-Hartman vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
36660d4ae8dSGreg Kroah-Hartman {
367496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
36860d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc;
36960d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll;
3706a6b76ccSJiri Slaby 	unsigned int read;
37160d4ae8dSGreg Kroah-Hartman 	ssize_t ret;
372fcdba07eSJiri Olsa 	char *con_buf;
37336c39220SJiri Slaby 	loff_t pos;
37436c39220SJiri Slaby 	bool viewed, attr, uni_mode;
37560d4ae8dSGreg Kroah-Hartman 
376fcdba07eSJiri Olsa 	con_buf = (char *) __get_free_page(GFP_KERNEL);
377fcdba07eSJiri Olsa 	if (!con_buf)
378fcdba07eSJiri Olsa 		return -ENOMEM;
37960d4ae8dSGreg Kroah-Hartman 
38060d4ae8dSGreg Kroah-Hartman 	pos = *ppos;
38160d4ae8dSGreg Kroah-Hartman 
38260d4ae8dSGreg Kroah-Hartman 	/* Select the proper current console and verify
38360d4ae8dSGreg Kroah-Hartman 	 * sanity of the situation under the console lock.
38460d4ae8dSGreg Kroah-Hartman 	 */
385ac751efaSTorben Hohn 	console_lock();
38660d4ae8dSGreg Kroah-Hartman 
387d21b0be2SNicolas Pitre 	uni_mode = use_unicode(inode);
388d21b0be2SNicolas Pitre 	attr = use_attributes(inode);
38960d4ae8dSGreg Kroah-Hartman 
39060d4ae8dSGreg Kroah-Hartman 	ret = -EINVAL;
39160d4ae8dSGreg Kroah-Hartman 	if (pos < 0)
39260d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
393d21b0be2SNicolas Pitre 	/* we enforce 32-bit alignment for pos and count in unicode mode */
394d21b0be2SNicolas Pitre 	if (uni_mode && (pos | count) & 3)
395d21b0be2SNicolas Pitre 		goto unlock_out;
396d21b0be2SNicolas Pitre 
39760d4ae8dSGreg Kroah-Hartman 	poll = file->private_data;
39860d4ae8dSGreg Kroah-Hartman 	if (count && poll)
3991bf931abSNicolas Pitre 		poll->event = 0;
40060d4ae8dSGreg Kroah-Hartman 	read = 0;
40160d4ae8dSGreg Kroah-Hartman 	ret = 0;
40260d4ae8dSGreg Kroah-Hartman 	while (count) {
4036a6b76ccSJiri Slaby 		unsigned int this_round, skip = 0;
40436c39220SJiri Slaby 		int size;
40560d4ae8dSGreg Kroah-Hartman 
406226fae12SGeorge Kennedy 		vc = vcs_vc(inode, &viewed);
407ae3419fbSThomas Weißschuh 		if (!vc) {
408ae3419fbSThomas Weißschuh 			ret = -ENXIO;
409ae3419fbSThomas Weißschuh 			break;
410ae3419fbSThomas Weißschuh 		}
411226fae12SGeorge Kennedy 
41260d4ae8dSGreg Kroah-Hartman 		/* Check whether we are above size each round,
41360d4ae8dSGreg Kroah-Hartman 		 * as copy_to_user at the end of this loop
41460d4ae8dSGreg Kroah-Hartman 		 * could sleep.
41560d4ae8dSGreg Kroah-Hartman 		 */
41671d4abfaSJiri Slaby 		size = vcs_size(vc, attr, uni_mode);
417dc1892c4SJiri Olsa 		if (size < 0) {
418dc1892c4SJiri Olsa 			ret = size;
41946d733d0SGeorge Kennedy 			break;
420dc1892c4SJiri Olsa 		}
42160d4ae8dSGreg Kroah-Hartman 		if (pos >= size)
42260d4ae8dSGreg Kroah-Hartman 			break;
42360d4ae8dSGreg Kroah-Hartman 		if (count > size - pos)
42460d4ae8dSGreg Kroah-Hartman 			count = size - pos;
42560d4ae8dSGreg Kroah-Hartman 
42660d4ae8dSGreg Kroah-Hartman 		this_round = count;
42760d4ae8dSGreg Kroah-Hartman 		if (this_round > CON_BUF_SIZE)
42860d4ae8dSGreg Kroah-Hartman 			this_round = CON_BUF_SIZE;
42960d4ae8dSGreg Kroah-Hartman 
43060d4ae8dSGreg Kroah-Hartman 		/* Perform the whole read into the local con_buf.
43160d4ae8dSGreg Kroah-Hartman 		 * Then we can drop the console spinlock and safely
43260d4ae8dSGreg Kroah-Hartman 		 * attempt to move it to userspace.
43360d4ae8dSGreg Kroah-Hartman 		 */
43460d4ae8dSGreg Kroah-Hartman 
435d21b0be2SNicolas Pitre 		if (uni_mode) {
4360f66eee3SJiri Slaby 			ret = vcs_read_buf_uni(vc, con_buf, pos, this_round,
4370f66eee3SJiri Slaby 					viewed);
438d21b0be2SNicolas Pitre 			if (ret)
439d21b0be2SNicolas Pitre 				break;
440d21b0be2SNicolas Pitre 		} else if (!attr) {
4415a52baaaSJiri Slaby 			vcs_read_buf_noattr(vc, con_buf, pos, this_round,
4425a52baaaSJiri Slaby 					viewed);
44360d4ae8dSGreg Kroah-Hartman 		} else {
4446a6b76ccSJiri Slaby 			this_round = vcs_read_buf(vc, con_buf, pos, this_round,
4456a6b76ccSJiri Slaby 					viewed, &skip);
44660d4ae8dSGreg Kroah-Hartman 		}
44760d4ae8dSGreg Kroah-Hartman 
44860d4ae8dSGreg Kroah-Hartman 		/* Finally, release the console semaphore while we push
44960d4ae8dSGreg Kroah-Hartman 		 * all the data to userspace from our temporary buffer.
45060d4ae8dSGreg Kroah-Hartman 		 *
45160d4ae8dSGreg Kroah-Hartman 		 * AKPM: Even though it's a semaphore, we should drop it because
45260d4ae8dSGreg Kroah-Hartman 		 * the pagefault handling code may want to call printk().
45360d4ae8dSGreg Kroah-Hartman 		 */
45460d4ae8dSGreg Kroah-Hartman 
455ac751efaSTorben Hohn 		console_unlock();
4566a6b76ccSJiri Slaby 		ret = copy_to_user(buf, con_buf + skip, this_round);
457ac751efaSTorben Hohn 		console_lock();
45860d4ae8dSGreg Kroah-Hartman 
45960d4ae8dSGreg Kroah-Hartman 		if (ret) {
4606a6b76ccSJiri Slaby 			read += this_round - ret;
46160d4ae8dSGreg Kroah-Hartman 			ret = -EFAULT;
46260d4ae8dSGreg Kroah-Hartman 			break;
46360d4ae8dSGreg Kroah-Hartman 		}
4646a6b76ccSJiri Slaby 		buf += this_round;
4656a6b76ccSJiri Slaby 		pos += this_round;
4666a6b76ccSJiri Slaby 		read += this_round;
4676a6b76ccSJiri Slaby 		count -= this_round;
46860d4ae8dSGreg Kroah-Hartman 	}
46960d4ae8dSGreg Kroah-Hartman 	*ppos += read;
47060d4ae8dSGreg Kroah-Hartman 	if (read)
47160d4ae8dSGreg Kroah-Hartman 		ret = read;
47260d4ae8dSGreg Kroah-Hartman unlock_out:
473ac751efaSTorben Hohn 	console_unlock();
474fcdba07eSJiri Olsa 	free_page((unsigned long) con_buf);
47560d4ae8dSGreg Kroah-Hartman 	return ret;
47660d4ae8dSGreg Kroah-Hartman }
47760d4ae8dSGreg Kroah-Hartman 
vcs_write_buf_noattr(struct vc_data * vc,const char * con_buf,unsigned int pos,unsigned int count,bool viewed,u16 ** org0)4789e636378SJiri Slaby static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf,
4799e636378SJiri Slaby 		unsigned int pos, unsigned int count, bool viewed, u16 **org0)
4809e636378SJiri Slaby {
4819e636378SJiri Slaby 	u16 *org;
4829e636378SJiri Slaby 	unsigned int col, maxcol = vc->vc_cols;
4839e636378SJiri Slaby 
4849e636378SJiri Slaby 	*org0 = org = screen_pos(vc, pos, viewed);
4859e636378SJiri Slaby 	col = pos % maxcol;
4869e636378SJiri Slaby 	pos += maxcol - col;
4879e636378SJiri Slaby 
4889e636378SJiri Slaby 	while (count > 0) {
4899e636378SJiri Slaby 		unsigned char c = *con_buf++;
4909e636378SJiri Slaby 
4919e636378SJiri Slaby 		count--;
4929e636378SJiri Slaby 		vcs_scr_writew(vc,
4939e636378SJiri Slaby 			       (vcs_scr_readw(vc, org) & 0xff00) | c, org);
4949e636378SJiri Slaby 		org++;
4959e636378SJiri Slaby 		if (++col == maxcol) {
4969e636378SJiri Slaby 			org = screen_pos(vc, pos, viewed);
4979e636378SJiri Slaby 			col = 0;
4989e636378SJiri Slaby 			pos += maxcol;
4999e636378SJiri Slaby 		}
5009e636378SJiri Slaby 	}
5019e636378SJiri Slaby 
5029e636378SJiri Slaby 	return org;
5039e636378SJiri Slaby }
5049e636378SJiri Slaby 
505d7c91c50SJiri Slaby /*
506d7c91c50SJiri Slaby  * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it
507d7c91c50SJiri Slaby  * the poor man way.
508d7c91c50SJiri Slaby  */
vc_compile_le16(u8 hi,u8 lo)509d7c91c50SJiri Slaby static inline u16 vc_compile_le16(u8 hi, u8 lo)
510d7c91c50SJiri Slaby {
511d7c91c50SJiri Slaby #ifdef __BIG_ENDIAN
512d7c91c50SJiri Slaby 	return (lo << 8u) | hi;
513d7c91c50SJiri Slaby #else
514d7c91c50SJiri Slaby 	return (hi << 8u) | lo;
515d7c91c50SJiri Slaby #endif
516d7c91c50SJiri Slaby }
517d7c91c50SJiri Slaby 
vcs_write_buf(struct vc_data * vc,const char * con_buf,unsigned int pos,unsigned int count,bool viewed,u16 ** org0)51895e0d57fSJiri Slaby static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf,
51995e0d57fSJiri Slaby 		unsigned int pos, unsigned int count, bool viewed, u16 **org0)
52095e0d57fSJiri Slaby {
52195e0d57fSJiri Slaby 	u16 *org;
52295e0d57fSJiri Slaby 	unsigned int col, maxcol = vc->vc_cols;
52395e0d57fSJiri Slaby 	unsigned char c;
52495e0d57fSJiri Slaby 
52595e0d57fSJiri Slaby 	/* header */
52695e0d57fSJiri Slaby 	if (pos < HEADER_SIZE) {
52795e0d57fSJiri Slaby 		char header[HEADER_SIZE];
52895e0d57fSJiri Slaby 
52995e0d57fSJiri Slaby 		getconsxy(vc, header + 2);
53095e0d57fSJiri Slaby 		while (pos < HEADER_SIZE && count > 0) {
53195e0d57fSJiri Slaby 			count--;
53295e0d57fSJiri Slaby 			header[pos++] = *con_buf++;
53395e0d57fSJiri Slaby 		}
53495e0d57fSJiri Slaby 		if (!viewed)
53595e0d57fSJiri Slaby 			putconsxy(vc, header + 2);
53695e0d57fSJiri Slaby 	}
53795e0d57fSJiri Slaby 
53895e0d57fSJiri Slaby 	if (!count)
53995e0d57fSJiri Slaby 		return NULL;
54095e0d57fSJiri Slaby 
54195e0d57fSJiri Slaby 	pos -= HEADER_SIZE;
54295e0d57fSJiri Slaby 	col = (pos/2) % maxcol;
54395e0d57fSJiri Slaby 
54495e0d57fSJiri Slaby 	*org0 = org = screen_pos(vc, pos/2, viewed);
54595e0d57fSJiri Slaby 
54695e0d57fSJiri Slaby 	/* odd pos -- the first single character */
54795e0d57fSJiri Slaby 	if (pos & 1) {
54895e0d57fSJiri Slaby 		count--;
54995e0d57fSJiri Slaby 		c = *con_buf++;
550d7c91c50SJiri Slaby 		vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)),
551d7c91c50SJiri Slaby 				org);
55295e0d57fSJiri Slaby 		org++;
55395e0d57fSJiri Slaby 		pos++;
55495e0d57fSJiri Slaby 		if (++col == maxcol) {
55595e0d57fSJiri Slaby 			org = screen_pos(vc, pos/2, viewed);
55695e0d57fSJiri Slaby 			col = 0;
55795e0d57fSJiri Slaby 		}
55895e0d57fSJiri Slaby 	}
55995e0d57fSJiri Slaby 
56095e0d57fSJiri Slaby 	pos /= 2;
56195e0d57fSJiri Slaby 	pos += maxcol - col;
56295e0d57fSJiri Slaby 
56395e0d57fSJiri Slaby 	/* even pos -- handle attr+character pairs */
56495e0d57fSJiri Slaby 	while (count > 1) {
56595e0d57fSJiri Slaby 		unsigned short w;
56695e0d57fSJiri Slaby 
56795e0d57fSJiri Slaby 		w = get_unaligned(((unsigned short *)con_buf));
56895e0d57fSJiri Slaby 		vcs_scr_writew(vc, w, org++);
56995e0d57fSJiri Slaby 		con_buf += 2;
57095e0d57fSJiri Slaby 		count -= 2;
57195e0d57fSJiri Slaby 		if (++col == maxcol) {
57295e0d57fSJiri Slaby 			org = screen_pos(vc, pos, viewed);
57395e0d57fSJiri Slaby 			col = 0;
57495e0d57fSJiri Slaby 			pos += maxcol;
57595e0d57fSJiri Slaby 		}
57695e0d57fSJiri Slaby 	}
57795e0d57fSJiri Slaby 
57895e0d57fSJiri Slaby 	if (!count)
57995e0d57fSJiri Slaby 		return org;
58095e0d57fSJiri Slaby 
58195e0d57fSJiri Slaby 	/* odd pos -- the remaining character */
58295e0d57fSJiri Slaby 	c = *con_buf++;
583d7c91c50SJiri Slaby 	vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c),
584d7c91c50SJiri Slaby 				org);
58595e0d57fSJiri Slaby 
58695e0d57fSJiri Slaby 	return org;
58795e0d57fSJiri Slaby }
58895e0d57fSJiri Slaby 
58960d4ae8dSGreg Kroah-Hartman static ssize_t
vcs_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)59060d4ae8dSGreg Kroah-Hartman vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
59160d4ae8dSGreg Kroah-Hartman {
592496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
59360d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc;
59495e0d57fSJiri Slaby 	char *con_buf;
59595e0d57fSJiri Slaby 	u16 *org0, *org;
59695e0d57fSJiri Slaby 	unsigned int written;
5972665bef4SJiri Slaby 	int size;
5982665bef4SJiri Slaby 	ssize_t ret;
5992665bef4SJiri Slaby 	loff_t pos;
6002665bef4SJiri Slaby 	bool viewed, attr;
60160d4ae8dSGreg Kroah-Hartman 
6020c9acb1aSNicolas Pitre 	if (use_unicode(inode))
6030c9acb1aSNicolas Pitre 		return -EOPNOTSUPP;
6040c9acb1aSNicolas Pitre 
605fcdba07eSJiri Olsa 	con_buf = (char *) __get_free_page(GFP_KERNEL);
606fcdba07eSJiri Olsa 	if (!con_buf)
607fcdba07eSJiri Olsa 		return -ENOMEM;
60860d4ae8dSGreg Kroah-Hartman 
60960d4ae8dSGreg Kroah-Hartman 	pos = *ppos;
61060d4ae8dSGreg Kroah-Hartman 
61160d4ae8dSGreg Kroah-Hartman 	/* Select the proper current console and verify
61260d4ae8dSGreg Kroah-Hartman 	 * sanity of the situation under the console lock.
61360d4ae8dSGreg Kroah-Hartman 	 */
614ac751efaSTorben Hohn 	console_lock();
61560d4ae8dSGreg Kroah-Hartman 
616d21b0be2SNicolas Pitre 	attr = use_attributes(inode);
61760d4ae8dSGreg Kroah-Hartman 	ret = -ENXIO;
618fcdba07eSJiri Olsa 	vc = vcs_vc(inode, &viewed);
619fcdba07eSJiri Olsa 	if (!vc)
62060d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
62160d4ae8dSGreg Kroah-Hartman 
62271d4abfaSJiri Slaby 	size = vcs_size(vc, attr, false);
62371d4abfaSJiri Slaby 	if (size < 0) {
62471d4abfaSJiri Slaby 		ret = size;
62571d4abfaSJiri Slaby 		goto unlock_out;
62671d4abfaSJiri Slaby 	}
62760d4ae8dSGreg Kroah-Hartman 	ret = -EINVAL;
62860d4ae8dSGreg Kroah-Hartman 	if (pos < 0 || pos > size)
62960d4ae8dSGreg Kroah-Hartman 		goto unlock_out;
63060d4ae8dSGreg Kroah-Hartman 	if (count > size - pos)
63160d4ae8dSGreg Kroah-Hartman 		count = size - pos;
63260d4ae8dSGreg Kroah-Hartman 	written = 0;
63360d4ae8dSGreg Kroah-Hartman 	while (count) {
63495e0d57fSJiri Slaby 		unsigned int this_round = count;
63560d4ae8dSGreg Kroah-Hartman 
63660d4ae8dSGreg Kroah-Hartman 		if (this_round > CON_BUF_SIZE)
63760d4ae8dSGreg Kroah-Hartman 			this_round = CON_BUF_SIZE;
63860d4ae8dSGreg Kroah-Hartman 
63960d4ae8dSGreg Kroah-Hartman 		/* Temporarily drop the console lock so that we can read
64060d4ae8dSGreg Kroah-Hartman 		 * in the write data from userspace safely.
64160d4ae8dSGreg Kroah-Hartman 		 */
642ac751efaSTorben Hohn 		console_unlock();
64360d4ae8dSGreg Kroah-Hartman 		ret = copy_from_user(con_buf, buf, this_round);
644ac751efaSTorben Hohn 		console_lock();
64560d4ae8dSGreg Kroah-Hartman 
64660d4ae8dSGreg Kroah-Hartman 		if (ret) {
64760d4ae8dSGreg Kroah-Hartman 			this_round -= ret;
64860d4ae8dSGreg Kroah-Hartman 			if (!this_round) {
64960d4ae8dSGreg Kroah-Hartman 				/* Abort loop if no data were copied. Otherwise
65060d4ae8dSGreg Kroah-Hartman 				 * fail with -EFAULT.
65160d4ae8dSGreg Kroah-Hartman 				 */
65260d4ae8dSGreg Kroah-Hartman 				if (written)
65360d4ae8dSGreg Kroah-Hartman 					break;
65460d4ae8dSGreg Kroah-Hartman 				ret = -EFAULT;
65560d4ae8dSGreg Kroah-Hartman 				goto unlock_out;
65660d4ae8dSGreg Kroah-Hartman 			}
65760d4ae8dSGreg Kroah-Hartman 		}
65860d4ae8dSGreg Kroah-Hartman 
6598fb9ea65SGeorge Kennedy 		/* The vc might have been freed or vcs_size might have changed
6608fb9ea65SGeorge Kennedy 		 * while we slept to grab the user buffer, so recheck.
66160d4ae8dSGreg Kroah-Hartman 		 * Return data written up to now on failure.
66260d4ae8dSGreg Kroah-Hartman 		 */
6638fb9ea65SGeorge Kennedy 		vc = vcs_vc(inode, &viewed);
6648fb9ea65SGeorge Kennedy 		if (!vc) {
6658fb9ea65SGeorge Kennedy 			if (written)
6668fb9ea65SGeorge Kennedy 				break;
6678fb9ea65SGeorge Kennedy 			ret = -ENXIO;
6688fb9ea65SGeorge Kennedy 			goto unlock_out;
6698fb9ea65SGeorge Kennedy 		}
67071d4abfaSJiri Slaby 		size = vcs_size(vc, attr, false);
671dc1892c4SJiri Olsa 		if (size < 0) {
672dc1892c4SJiri Olsa 			if (written)
673dc1892c4SJiri Olsa 				break;
674dc1892c4SJiri Olsa 			ret = size;
675dc1892c4SJiri Olsa 			goto unlock_out;
676dc1892c4SJiri Olsa 		}
67760d4ae8dSGreg Kroah-Hartman 		if (pos >= size)
67860d4ae8dSGreg Kroah-Hartman 			break;
67960d4ae8dSGreg Kroah-Hartman 		if (this_round > size - pos)
68060d4ae8dSGreg Kroah-Hartman 			this_round = size - pos;
68160d4ae8dSGreg Kroah-Hartman 
68260d4ae8dSGreg Kroah-Hartman 		/* OK, now actually push the write to the console
68360d4ae8dSGreg Kroah-Hartman 		 * under the lock using the local kernel buffer.
68460d4ae8dSGreg Kroah-Hartman 		 */
68560d4ae8dSGreg Kroah-Hartman 
68695e0d57fSJiri Slaby 		if (attr)
68795e0d57fSJiri Slaby 			org = vcs_write_buf(vc, con_buf, pos, this_round,
68895e0d57fSJiri Slaby 					viewed, &org0);
68995e0d57fSJiri Slaby 		else
6909e636378SJiri Slaby 			org = vcs_write_buf_noattr(vc, con_buf, pos, this_round,
6919e636378SJiri Slaby 					viewed, &org0);
69260d4ae8dSGreg Kroah-Hartman 
69395e0d57fSJiri Slaby 		count -= this_round;
69495e0d57fSJiri Slaby 		written += this_round;
69595e0d57fSJiri Slaby 		buf += this_round;
69695e0d57fSJiri Slaby 		pos += this_round;
69795e0d57fSJiri Slaby 		if (org)
69860d4ae8dSGreg Kroah-Hartman 			update_region(vc, (unsigned long)(org0), org - org0);
69960d4ae8dSGreg Kroah-Hartman 	}
70060d4ae8dSGreg Kroah-Hartman 	*ppos += written;
70160d4ae8dSGreg Kroah-Hartman 	ret = written;
70260d4ae8dSGreg Kroah-Hartman 	if (written)
70360d4ae8dSGreg Kroah-Hartman 		vcs_scr_updated(vc);
70460d4ae8dSGreg Kroah-Hartman 
70560d4ae8dSGreg Kroah-Hartman unlock_out:
706ac751efaSTorben Hohn 	console_unlock();
707fcdba07eSJiri Olsa 	free_page((unsigned long) con_buf);
70860d4ae8dSGreg Kroah-Hartman 	return ret;
70960d4ae8dSGreg Kroah-Hartman }
71060d4ae8dSGreg Kroah-Hartman 
711afc9a42bSAl Viro static __poll_t
vcs_poll(struct file * file,poll_table * wait)71260d4ae8dSGreg Kroah-Hartman vcs_poll(struct file *file, poll_table *wait)
71360d4ae8dSGreg Kroah-Hartman {
71460d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = vcs_poll_data_get(file);
7151bf931abSNicolas Pitre 	__poll_t ret = DEFAULT_POLLMASK|EPOLLERR;
71660d4ae8dSGreg Kroah-Hartman 
71760d4ae8dSGreg Kroah-Hartman 	if (poll) {
71860d4ae8dSGreg Kroah-Hartman 		poll_wait(file, &poll->waitq, wait);
7191bf931abSNicolas Pitre 		switch (poll->event) {
7201bf931abSNicolas Pitre 		case VT_UPDATE:
7211bf931abSNicolas Pitre 			ret = DEFAULT_POLLMASK|EPOLLPRI;
7221bf931abSNicolas Pitre 			break;
7231bf931abSNicolas Pitre 		case VT_DEALLOCATE:
7241bf931abSNicolas Pitre 			ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR;
7251bf931abSNicolas Pitre 			break;
7261bf931abSNicolas Pitre 		case 0:
72747c344d0SNicolas Pitre 			ret = DEFAULT_POLLMASK;
7281bf931abSNicolas Pitre 			break;
7291bf931abSNicolas Pitre 		}
73060d4ae8dSGreg Kroah-Hartman 	}
73160d4ae8dSGreg Kroah-Hartman 	return ret;
73260d4ae8dSGreg Kroah-Hartman }
73360d4ae8dSGreg Kroah-Hartman 
73460d4ae8dSGreg Kroah-Hartman static int
vcs_fasync(int fd,struct file * file,int on)73560d4ae8dSGreg Kroah-Hartman vcs_fasync(int fd, struct file *file, int on)
73660d4ae8dSGreg Kroah-Hartman {
73760d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = file->private_data;
73860d4ae8dSGreg Kroah-Hartman 
73960d4ae8dSGreg Kroah-Hartman 	if (!poll) {
74060d4ae8dSGreg Kroah-Hartman 		/* don't allocate anything if all we want is disable fasync */
74160d4ae8dSGreg Kroah-Hartman 		if (!on)
74260d4ae8dSGreg Kroah-Hartman 			return 0;
74360d4ae8dSGreg Kroah-Hartman 		poll = vcs_poll_data_get(file);
74460d4ae8dSGreg Kroah-Hartman 		if (!poll)
74560d4ae8dSGreg Kroah-Hartman 			return -ENOMEM;
74660d4ae8dSGreg Kroah-Hartman 	}
74760d4ae8dSGreg Kroah-Hartman 
74860d4ae8dSGreg Kroah-Hartman 	return fasync_helper(fd, file, on, &poll->fasync);
74960d4ae8dSGreg Kroah-Hartman }
75060d4ae8dSGreg Kroah-Hartman 
75160d4ae8dSGreg Kroah-Hartman static int
vcs_open(struct inode * inode,struct file * filp)75260d4ae8dSGreg Kroah-Hartman vcs_open(struct inode *inode, struct file *filp)
75360d4ae8dSGreg Kroah-Hartman {
754d21b0be2SNicolas Pitre 	unsigned int currcons = console(inode);
755d21b0be2SNicolas Pitre 	bool attr = use_attributes(inode);
756d21b0be2SNicolas Pitre 	bool uni_mode = use_unicode(inode);
75760d4ae8dSGreg Kroah-Hartman 	int ret = 0;
75860d4ae8dSGreg Kroah-Hartman 
759d21b0be2SNicolas Pitre 	/* we currently don't support attributes in unicode mode */
760d21b0be2SNicolas Pitre 	if (attr && uni_mode)
761d21b0be2SNicolas Pitre 		return -EOPNOTSUPP;
762d21b0be2SNicolas Pitre 
7634001d7b7SAlan Cox 	console_lock();
76460d4ae8dSGreg Kroah-Hartman 	if(currcons && !vc_cons_allocated(currcons-1))
76560d4ae8dSGreg Kroah-Hartman 		ret = -ENXIO;
7664001d7b7SAlan Cox 	console_unlock();
76760d4ae8dSGreg Kroah-Hartman 	return ret;
76860d4ae8dSGreg Kroah-Hartman }
76960d4ae8dSGreg Kroah-Hartman 
vcs_release(struct inode * inode,struct file * file)77060d4ae8dSGreg Kroah-Hartman static int vcs_release(struct inode *inode, struct file *file)
77160d4ae8dSGreg Kroah-Hartman {
77260d4ae8dSGreg Kroah-Hartman 	struct vcs_poll_data *poll = file->private_data;
77360d4ae8dSGreg Kroah-Hartman 
77460d4ae8dSGreg Kroah-Hartman 	if (poll)
77560d4ae8dSGreg Kroah-Hartman 		vcs_poll_data_free(poll);
77660d4ae8dSGreg Kroah-Hartman 	return 0;
77760d4ae8dSGreg Kroah-Hartman }
77860d4ae8dSGreg Kroah-Hartman 
77960d4ae8dSGreg Kroah-Hartman static const struct file_operations vcs_fops = {
78060d4ae8dSGreg Kroah-Hartman 	.llseek		= vcs_lseek,
78160d4ae8dSGreg Kroah-Hartman 	.read		= vcs_read,
78260d4ae8dSGreg Kroah-Hartman 	.write		= vcs_write,
78360d4ae8dSGreg Kroah-Hartman 	.poll		= vcs_poll,
78460d4ae8dSGreg Kroah-Hartman 	.fasync		= vcs_fasync,
78560d4ae8dSGreg Kroah-Hartman 	.open		= vcs_open,
78660d4ae8dSGreg Kroah-Hartman 	.release	= vcs_release,
78760d4ae8dSGreg Kroah-Hartman };
78860d4ae8dSGreg Kroah-Hartman 
7895a1cc963SGreg Kroah-Hartman static const struct class vc_class = {
7905a1cc963SGreg Kroah-Hartman 	.name = "vc",
7915a1cc963SGreg Kroah-Hartman };
79260d4ae8dSGreg Kroah-Hartman 
vcs_make_sysfs(int index)79360d4ae8dSGreg Kroah-Hartman void vcs_make_sysfs(int index)
79460d4ae8dSGreg Kroah-Hartman {
7955a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, "vcs%u", index + 1);
7965a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL, "vcsu%u", index + 1);
7975a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, "vcsa%u", index + 1);
79860d4ae8dSGreg Kroah-Hartman }
79960d4ae8dSGreg Kroah-Hartman 
vcs_remove_sysfs(int index)80060d4ae8dSGreg Kroah-Hartman void vcs_remove_sysfs(int index)
80160d4ae8dSGreg Kroah-Hartman {
8025a1cc963SGreg Kroah-Hartman 	device_destroy(&vc_class, MKDEV(VCS_MAJOR, index + 1));
8035a1cc963SGreg Kroah-Hartman 	device_destroy(&vc_class, MKDEV(VCS_MAJOR, index + 65));
8045a1cc963SGreg Kroah-Hartman 	device_destroy(&vc_class, MKDEV(VCS_MAJOR, index + 129));
80560d4ae8dSGreg Kroah-Hartman }
80660d4ae8dSGreg Kroah-Hartman 
vcs_init(void)80760d4ae8dSGreg Kroah-Hartman int __init vcs_init(void)
80860d4ae8dSGreg Kroah-Hartman {
80960d4ae8dSGreg Kroah-Hartman 	unsigned int i;
81060d4ae8dSGreg Kroah-Hartman 
81160d4ae8dSGreg Kroah-Hartman 	if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
81260d4ae8dSGreg Kroah-Hartman 		panic("unable to get major %d for vcs device", VCS_MAJOR);
8135a1cc963SGreg Kroah-Hartman 	if (class_register(&vc_class))
8145a1cc963SGreg Kroah-Hartman 		panic("unable to create vc_class");
81560d4ae8dSGreg Kroah-Hartman 
8165a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
8175a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu");
8185a1cc963SGreg Kroah-Hartman 	device_create(&vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
81960d4ae8dSGreg Kroah-Hartman 	for (i = 0; i < MIN_NR_CONSOLES; i++)
82060d4ae8dSGreg Kroah-Hartman 		vcs_make_sysfs(i);
82160d4ae8dSGreg Kroah-Hartman 	return 0;
82260d4ae8dSGreg Kroah-Hartman }
823