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 56*6d507c75SJiri Slaby #define HEADER_SIZE 4u 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 1807d62549aSJiri Slaby /** 1817d62549aSJiri Slaby * vcs_vc -- return VC for @inode 1827d62549aSJiri Slaby * @inode: inode for which to return a VC 1837d62549aSJiri Slaby * @viewed: returns whether this console is currently foreground (viewed) 1847d62549aSJiri Slaby * 185fcdba07eSJiri Olsa * Must be called with console_lock. 186fcdba07eSJiri Olsa */ 1877d62549aSJiri 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) 1967d62549aSJiri Slaby *viewed = true; 197fcdba07eSJiri Olsa } else { 198fcdba07eSJiri Olsa currcons--; 199fcdba07eSJiri Olsa if (viewed) 2007d62549aSJiri Slaby *viewed = false; 201fcdba07eSJiri Olsa } 202fcdba07eSJiri Olsa return vc_cons[currcons].d; 203fcdba07eSJiri Olsa } 204fcdba07eSJiri Olsa 20571d4abfaSJiri Slaby /** 20671d4abfaSJiri Slaby * vcs_size -- return size for a VC in @vc 20771d4abfaSJiri Slaby * @vc: which VC 20871d4abfaSJiri Slaby * @attr: does it use attributes? 20971d4abfaSJiri Slaby * @unicode: is it unicode? 21071d4abfaSJiri Slaby * 211fcdba07eSJiri Olsa * Must be called with console_lock. 212fcdba07eSJiri Olsa */ 21371d4abfaSJiri Slaby static int vcs_size(const struct vc_data *vc, bool attr, bool unicode) 21460d4ae8dSGreg Kroah-Hartman { 21560d4ae8dSGreg Kroah-Hartman int size; 21660d4ae8dSGreg Kroah-Hartman 217fcdba07eSJiri Olsa WARN_CONSOLE_UNLOCKED(); 218fcdba07eSJiri Olsa 21960d4ae8dSGreg Kroah-Hartman size = vc->vc_rows * vc->vc_cols; 22060d4ae8dSGreg Kroah-Hartman 22171d4abfaSJiri Slaby if (attr) { 22271d4abfaSJiri Slaby if (unicode) 223d21b0be2SNicolas Pitre return -EOPNOTSUPP; 22471d4abfaSJiri Slaby 22560d4ae8dSGreg Kroah-Hartman size = 2 * size + HEADER_SIZE; 22671d4abfaSJiri Slaby } else if (unicode) 227d21b0be2SNicolas Pitre size *= 4; 22871d4abfaSJiri Slaby 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 { 23471d4abfaSJiri Slaby struct inode *inode = file_inode(file); 23571d4abfaSJiri Slaby struct vc_data *vc; 23660d4ae8dSGreg Kroah-Hartman int size; 23760d4ae8dSGreg Kroah-Hartman 238dc1892c4SJiri Olsa console_lock(); 23971d4abfaSJiri Slaby vc = vcs_vc(inode, NULL); 24071d4abfaSJiri Slaby if (!vc) { 24171d4abfaSJiri Slaby console_unlock(); 24271d4abfaSJiri Slaby return -ENXIO; 24371d4abfaSJiri Slaby } 24471d4abfaSJiri Slaby 24571d4abfaSJiri Slaby size = vcs_size(vc, use_attributes(inode), use_unicode(inode)); 246dc1892c4SJiri Olsa console_unlock(); 247fcdba07eSJiri Olsa if (size < 0) 248dc1892c4SJiri Olsa return size; 24965004276SAl Viro return fixed_size_llseek(file, offset, orig, size); 25060d4ae8dSGreg Kroah-Hartman } 25160d4ae8dSGreg Kroah-Hartman 25260d4ae8dSGreg Kroah-Hartman 25360d4ae8dSGreg Kroah-Hartman static ssize_t 25460d4ae8dSGreg Kroah-Hartman vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 25560d4ae8dSGreg Kroah-Hartman { 256496ad9aaSAl Viro struct inode *inode = file_inode(file); 25760d4ae8dSGreg Kroah-Hartman struct vc_data *vc; 25860d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll; 25936c39220SJiri Slaby u16 *org; 26036c39220SJiri Slaby unsigned int read, row, col, maxcol; 26160d4ae8dSGreg Kroah-Hartman ssize_t ret; 262fcdba07eSJiri Olsa char *con_buf; 26336c39220SJiri Slaby loff_t pos; 26436c39220SJiri Slaby bool viewed, attr, uni_mode; 26560d4ae8dSGreg Kroah-Hartman 266fcdba07eSJiri Olsa con_buf = (char *) __get_free_page(GFP_KERNEL); 267fcdba07eSJiri Olsa if (!con_buf) 268fcdba07eSJiri Olsa return -ENOMEM; 26960d4ae8dSGreg Kroah-Hartman 27060d4ae8dSGreg Kroah-Hartman pos = *ppos; 27160d4ae8dSGreg Kroah-Hartman 27260d4ae8dSGreg Kroah-Hartman /* Select the proper current console and verify 27360d4ae8dSGreg Kroah-Hartman * sanity of the situation under the console lock. 27460d4ae8dSGreg Kroah-Hartman */ 275ac751efaSTorben Hohn console_lock(); 27660d4ae8dSGreg Kroah-Hartman 277d21b0be2SNicolas Pitre uni_mode = use_unicode(inode); 278d21b0be2SNicolas Pitre attr = use_attributes(inode); 27960d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 280fcdba07eSJiri Olsa vc = vcs_vc(inode, &viewed); 281fcdba07eSJiri Olsa if (!vc) 28260d4ae8dSGreg Kroah-Hartman goto unlock_out; 28360d4ae8dSGreg Kroah-Hartman 28460d4ae8dSGreg Kroah-Hartman ret = -EINVAL; 28560d4ae8dSGreg Kroah-Hartman if (pos < 0) 28660d4ae8dSGreg Kroah-Hartman goto unlock_out; 287d21b0be2SNicolas Pitre /* we enforce 32-bit alignment for pos and count in unicode mode */ 288d21b0be2SNicolas Pitre if (uni_mode && (pos | count) & 3) 289d21b0be2SNicolas Pitre goto unlock_out; 290d21b0be2SNicolas Pitre 29160d4ae8dSGreg Kroah-Hartman poll = file->private_data; 29260d4ae8dSGreg Kroah-Hartman if (count && poll) 2931bf931abSNicolas Pitre poll->event = 0; 29460d4ae8dSGreg Kroah-Hartman read = 0; 29560d4ae8dSGreg Kroah-Hartman ret = 0; 29660d4ae8dSGreg Kroah-Hartman while (count) { 29760d4ae8dSGreg Kroah-Hartman char *con_buf0, *con_buf_start; 29836c39220SJiri Slaby unsigned int this_round, orig_count, p = pos; 29936c39220SJiri Slaby int size; 30060d4ae8dSGreg Kroah-Hartman 30160d4ae8dSGreg Kroah-Hartman /* Check whether we are above size each round, 30260d4ae8dSGreg Kroah-Hartman * as copy_to_user at the end of this loop 30360d4ae8dSGreg Kroah-Hartman * could sleep. 30460d4ae8dSGreg Kroah-Hartman */ 30571d4abfaSJiri Slaby size = vcs_size(vc, attr, uni_mode); 306dc1892c4SJiri Olsa if (size < 0) { 307dc1892c4SJiri Olsa if (read) 308dc1892c4SJiri Olsa break; 309dc1892c4SJiri Olsa ret = size; 310dc1892c4SJiri Olsa goto unlock_out; 311dc1892c4SJiri Olsa } 31260d4ae8dSGreg Kroah-Hartman if (pos >= size) 31360d4ae8dSGreg Kroah-Hartman break; 31460d4ae8dSGreg Kroah-Hartman if (count > size - pos) 31560d4ae8dSGreg Kroah-Hartman count = size - pos; 31660d4ae8dSGreg Kroah-Hartman 31760d4ae8dSGreg Kroah-Hartman this_round = count; 31860d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) 31960d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 32060d4ae8dSGreg Kroah-Hartman 32160d4ae8dSGreg Kroah-Hartman /* Perform the whole read into the local con_buf. 32260d4ae8dSGreg Kroah-Hartman * Then we can drop the console spinlock and safely 32360d4ae8dSGreg Kroah-Hartman * attempt to move it to userspace. 32460d4ae8dSGreg Kroah-Hartman */ 32560d4ae8dSGreg Kroah-Hartman 32660d4ae8dSGreg Kroah-Hartman con_buf_start = con_buf0 = con_buf; 32760d4ae8dSGreg Kroah-Hartman orig_count = this_round; 32860d4ae8dSGreg Kroah-Hartman maxcol = vc->vc_cols; 329d21b0be2SNicolas Pitre if (uni_mode) { 330d21b0be2SNicolas Pitre unsigned int nr; 331d21b0be2SNicolas Pitre 332d21b0be2SNicolas Pitre ret = vc_uniscr_check(vc); 333d21b0be2SNicolas Pitre if (ret) 334d21b0be2SNicolas Pitre break; 335d21b0be2SNicolas Pitre p /= 4; 336d21b0be2SNicolas Pitre row = p / vc->vc_cols; 337d21b0be2SNicolas Pitre col = p % maxcol; 338d21b0be2SNicolas Pitre nr = maxcol - col; 339d21b0be2SNicolas Pitre do { 340d21b0be2SNicolas Pitre if (nr > this_round/4) 341d21b0be2SNicolas Pitre nr = this_round/4; 342708d0bffSNicolas Pitre vc_uniscr_copy_line(vc, con_buf0, viewed, 343708d0bffSNicolas Pitre row, col, nr); 344d21b0be2SNicolas Pitre con_buf0 += nr * 4; 345d21b0be2SNicolas Pitre this_round -= nr * 4; 346d21b0be2SNicolas Pitre row++; 347d21b0be2SNicolas Pitre col = 0; 348d21b0be2SNicolas Pitre nr = maxcol; 349d21b0be2SNicolas Pitre } while (this_round); 350d21b0be2SNicolas Pitre } else if (!attr) { 35160d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 35260d4ae8dSGreg Kroah-Hartman col = p % maxcol; 35360d4ae8dSGreg Kroah-Hartman p += maxcol - col; 35460d4ae8dSGreg Kroah-Hartman while (this_round-- > 0) { 35560d4ae8dSGreg Kroah-Hartman *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff); 35660d4ae8dSGreg Kroah-Hartman if (++col == maxcol) { 35760d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 35860d4ae8dSGreg Kroah-Hartman col = 0; 35960d4ae8dSGreg Kroah-Hartman p += maxcol; 36060d4ae8dSGreg Kroah-Hartman } 36160d4ae8dSGreg Kroah-Hartman } 36260d4ae8dSGreg Kroah-Hartman } else { 36360d4ae8dSGreg Kroah-Hartman if (p < HEADER_SIZE) { 3648a085494SNicolas Pitre /* clamp header values if they don't fit */ 3658a085494SNicolas Pitre con_buf0[0] = min(vc->vc_rows, 0xFFu); 3668a085494SNicolas Pitre con_buf0[1] = min(vc->vc_cols, 0xFFu); 36760d4ae8dSGreg Kroah-Hartman getconsxy(vc, con_buf0 + 2); 36860d4ae8dSGreg Kroah-Hartman 36960d4ae8dSGreg Kroah-Hartman con_buf_start += p; 37060d4ae8dSGreg Kroah-Hartman this_round += p; 37160d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) { 37260d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 37360d4ae8dSGreg Kroah-Hartman orig_count = this_round - p; 37460d4ae8dSGreg Kroah-Hartman } 37560d4ae8dSGreg Kroah-Hartman 37660d4ae8dSGreg Kroah-Hartman /* Advance state pointers and move on. */ 377*6d507c75SJiri Slaby this_round -= min(HEADER_SIZE, this_round); 37860d4ae8dSGreg Kroah-Hartman p = HEADER_SIZE; 37960d4ae8dSGreg Kroah-Hartman con_buf0 = con_buf + HEADER_SIZE; 38060d4ae8dSGreg Kroah-Hartman /* If this_round >= 0, then p is even... */ 38160d4ae8dSGreg Kroah-Hartman } else if (p & 1) { 38260d4ae8dSGreg Kroah-Hartman /* Skip first byte for output if start address is odd 38360d4ae8dSGreg Kroah-Hartman * Update region sizes up/down depending on free 38460d4ae8dSGreg Kroah-Hartman * space in buffer. 38560d4ae8dSGreg Kroah-Hartman */ 38660d4ae8dSGreg Kroah-Hartman con_buf_start++; 38760d4ae8dSGreg Kroah-Hartman if (this_round < CON_BUF_SIZE) 38860d4ae8dSGreg Kroah-Hartman this_round++; 38960d4ae8dSGreg Kroah-Hartman else 39060d4ae8dSGreg Kroah-Hartman orig_count--; 39160d4ae8dSGreg Kroah-Hartman } 39260d4ae8dSGreg Kroah-Hartman if (this_round > 0) { 39360d4ae8dSGreg Kroah-Hartman unsigned short *tmp_buf = (unsigned short *)con_buf0; 39460d4ae8dSGreg Kroah-Hartman 39560d4ae8dSGreg Kroah-Hartman p -= HEADER_SIZE; 39660d4ae8dSGreg Kroah-Hartman p /= 2; 39760d4ae8dSGreg Kroah-Hartman col = p % maxcol; 39860d4ae8dSGreg Kroah-Hartman 39960d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 40060d4ae8dSGreg Kroah-Hartman p += maxcol - col; 40160d4ae8dSGreg Kroah-Hartman 40260d4ae8dSGreg Kroah-Hartman /* Buffer has even length, so we can always copy 40360d4ae8dSGreg Kroah-Hartman * character + attribute. We do not copy last byte 40460d4ae8dSGreg Kroah-Hartman * to userspace if this_round is odd. 40560d4ae8dSGreg Kroah-Hartman */ 40660d4ae8dSGreg Kroah-Hartman this_round = (this_round + 1) >> 1; 40760d4ae8dSGreg Kroah-Hartman 40860d4ae8dSGreg Kroah-Hartman while (this_round) { 40960d4ae8dSGreg Kroah-Hartman *tmp_buf++ = vcs_scr_readw(vc, org++); 41060d4ae8dSGreg Kroah-Hartman this_round --; 41160d4ae8dSGreg Kroah-Hartman if (++col == maxcol) { 41260d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 41360d4ae8dSGreg Kroah-Hartman col = 0; 41460d4ae8dSGreg Kroah-Hartman p += maxcol; 41560d4ae8dSGreg Kroah-Hartman } 41660d4ae8dSGreg Kroah-Hartman } 41760d4ae8dSGreg Kroah-Hartman } 41860d4ae8dSGreg Kroah-Hartman } 41960d4ae8dSGreg Kroah-Hartman 42060d4ae8dSGreg Kroah-Hartman /* Finally, release the console semaphore while we push 42160d4ae8dSGreg Kroah-Hartman * all the data to userspace from our temporary buffer. 42260d4ae8dSGreg Kroah-Hartman * 42360d4ae8dSGreg Kroah-Hartman * AKPM: Even though it's a semaphore, we should drop it because 42460d4ae8dSGreg Kroah-Hartman * the pagefault handling code may want to call printk(). 42560d4ae8dSGreg Kroah-Hartman */ 42660d4ae8dSGreg Kroah-Hartman 427ac751efaSTorben Hohn console_unlock(); 42860d4ae8dSGreg Kroah-Hartman ret = copy_to_user(buf, con_buf_start, orig_count); 429ac751efaSTorben Hohn console_lock(); 43060d4ae8dSGreg Kroah-Hartman 43160d4ae8dSGreg Kroah-Hartman if (ret) { 43260d4ae8dSGreg Kroah-Hartman read += (orig_count - ret); 43360d4ae8dSGreg Kroah-Hartman ret = -EFAULT; 43460d4ae8dSGreg Kroah-Hartman break; 43560d4ae8dSGreg Kroah-Hartman } 43660d4ae8dSGreg Kroah-Hartman buf += orig_count; 43760d4ae8dSGreg Kroah-Hartman pos += orig_count; 43860d4ae8dSGreg Kroah-Hartman read += orig_count; 43960d4ae8dSGreg Kroah-Hartman count -= orig_count; 44060d4ae8dSGreg Kroah-Hartman } 44160d4ae8dSGreg Kroah-Hartman *ppos += read; 44260d4ae8dSGreg Kroah-Hartman if (read) 44360d4ae8dSGreg Kroah-Hartman ret = read; 44460d4ae8dSGreg Kroah-Hartman unlock_out: 445ac751efaSTorben Hohn console_unlock(); 446fcdba07eSJiri Olsa free_page((unsigned long) con_buf); 44760d4ae8dSGreg Kroah-Hartman return ret; 44860d4ae8dSGreg Kroah-Hartman } 44960d4ae8dSGreg Kroah-Hartman 4509e636378SJiri Slaby static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf, 4519e636378SJiri Slaby unsigned int pos, unsigned int count, bool viewed, u16 **org0) 4529e636378SJiri Slaby { 4539e636378SJiri Slaby u16 *org; 4549e636378SJiri Slaby unsigned int col, maxcol = vc->vc_cols; 4559e636378SJiri Slaby 4569e636378SJiri Slaby *org0 = org = screen_pos(vc, pos, viewed); 4579e636378SJiri Slaby col = pos % maxcol; 4589e636378SJiri Slaby pos += maxcol - col; 4599e636378SJiri Slaby 4609e636378SJiri Slaby while (count > 0) { 4619e636378SJiri Slaby unsigned char c = *con_buf++; 4629e636378SJiri Slaby 4639e636378SJiri Slaby count--; 4649e636378SJiri Slaby vcs_scr_writew(vc, 4659e636378SJiri Slaby (vcs_scr_readw(vc, org) & 0xff00) | c, org); 4669e636378SJiri Slaby org++; 4679e636378SJiri Slaby if (++col == maxcol) { 4689e636378SJiri Slaby org = screen_pos(vc, pos, viewed); 4699e636378SJiri Slaby col = 0; 4709e636378SJiri Slaby pos += maxcol; 4719e636378SJiri Slaby } 4729e636378SJiri Slaby } 4739e636378SJiri Slaby 4749e636378SJiri Slaby return org; 4759e636378SJiri Slaby } 4769e636378SJiri Slaby 477d7c91c50SJiri Slaby /* 478d7c91c50SJiri Slaby * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it 479d7c91c50SJiri Slaby * the poor man way. 480d7c91c50SJiri Slaby */ 481d7c91c50SJiri Slaby static inline u16 vc_compile_le16(u8 hi, u8 lo) 482d7c91c50SJiri Slaby { 483d7c91c50SJiri Slaby #ifdef __BIG_ENDIAN 484d7c91c50SJiri Slaby return (lo << 8u) | hi; 485d7c91c50SJiri Slaby #else 486d7c91c50SJiri Slaby return (hi << 8u) | lo; 487d7c91c50SJiri Slaby #endif 488d7c91c50SJiri Slaby } 489d7c91c50SJiri Slaby 49095e0d57fSJiri Slaby static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf, 49195e0d57fSJiri Slaby unsigned int pos, unsigned int count, bool viewed, u16 **org0) 49295e0d57fSJiri Slaby { 49395e0d57fSJiri Slaby u16 *org; 49495e0d57fSJiri Slaby unsigned int col, maxcol = vc->vc_cols; 49595e0d57fSJiri Slaby unsigned char c; 49695e0d57fSJiri Slaby 49795e0d57fSJiri Slaby /* header */ 49895e0d57fSJiri Slaby if (pos < HEADER_SIZE) { 49995e0d57fSJiri Slaby char header[HEADER_SIZE]; 50095e0d57fSJiri Slaby 50195e0d57fSJiri Slaby getconsxy(vc, header + 2); 50295e0d57fSJiri Slaby while (pos < HEADER_SIZE && count > 0) { 50395e0d57fSJiri Slaby count--; 50495e0d57fSJiri Slaby header[pos++] = *con_buf++; 50595e0d57fSJiri Slaby } 50695e0d57fSJiri Slaby if (!viewed) 50795e0d57fSJiri Slaby putconsxy(vc, header + 2); 50895e0d57fSJiri Slaby } 50995e0d57fSJiri Slaby 51095e0d57fSJiri Slaby if (!count) 51195e0d57fSJiri Slaby return NULL; 51295e0d57fSJiri Slaby 51395e0d57fSJiri Slaby pos -= HEADER_SIZE; 51495e0d57fSJiri Slaby col = (pos/2) % maxcol; 51595e0d57fSJiri Slaby 51695e0d57fSJiri Slaby *org0 = org = screen_pos(vc, pos/2, viewed); 51795e0d57fSJiri Slaby 51895e0d57fSJiri Slaby /* odd pos -- the first single character */ 51995e0d57fSJiri Slaby if (pos & 1) { 52095e0d57fSJiri Slaby count--; 52195e0d57fSJiri Slaby c = *con_buf++; 522d7c91c50SJiri Slaby vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)), 523d7c91c50SJiri Slaby org); 52495e0d57fSJiri Slaby org++; 52595e0d57fSJiri Slaby pos++; 52695e0d57fSJiri Slaby if (++col == maxcol) { 52795e0d57fSJiri Slaby org = screen_pos(vc, pos/2, viewed); 52895e0d57fSJiri Slaby col = 0; 52995e0d57fSJiri Slaby } 53095e0d57fSJiri Slaby } 53195e0d57fSJiri Slaby 53295e0d57fSJiri Slaby pos /= 2; 53395e0d57fSJiri Slaby pos += maxcol - col; 53495e0d57fSJiri Slaby 53595e0d57fSJiri Slaby /* even pos -- handle attr+character pairs */ 53695e0d57fSJiri Slaby while (count > 1) { 53795e0d57fSJiri Slaby unsigned short w; 53895e0d57fSJiri Slaby 53995e0d57fSJiri Slaby w = get_unaligned(((unsigned short *)con_buf)); 54095e0d57fSJiri Slaby vcs_scr_writew(vc, w, org++); 54195e0d57fSJiri Slaby con_buf += 2; 54295e0d57fSJiri Slaby count -= 2; 54395e0d57fSJiri Slaby if (++col == maxcol) { 54495e0d57fSJiri Slaby org = screen_pos(vc, pos, viewed); 54595e0d57fSJiri Slaby col = 0; 54695e0d57fSJiri Slaby pos += maxcol; 54795e0d57fSJiri Slaby } 54895e0d57fSJiri Slaby } 54995e0d57fSJiri Slaby 55095e0d57fSJiri Slaby if (!count) 55195e0d57fSJiri Slaby return org; 55295e0d57fSJiri Slaby 55395e0d57fSJiri Slaby /* odd pos -- the remaining character */ 55495e0d57fSJiri Slaby c = *con_buf++; 555d7c91c50SJiri Slaby vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c), 556d7c91c50SJiri Slaby org); 55795e0d57fSJiri Slaby 55895e0d57fSJiri Slaby return org; 55995e0d57fSJiri Slaby } 56095e0d57fSJiri Slaby 56160d4ae8dSGreg Kroah-Hartman static ssize_t 56260d4ae8dSGreg Kroah-Hartman vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 56360d4ae8dSGreg Kroah-Hartman { 564496ad9aaSAl Viro struct inode *inode = file_inode(file); 56560d4ae8dSGreg Kroah-Hartman struct vc_data *vc; 56695e0d57fSJiri Slaby char *con_buf; 56795e0d57fSJiri Slaby u16 *org0, *org; 56895e0d57fSJiri Slaby unsigned int written; 5692665bef4SJiri Slaby int size; 5702665bef4SJiri Slaby ssize_t ret; 5712665bef4SJiri Slaby loff_t pos; 5722665bef4SJiri Slaby bool viewed, attr; 57360d4ae8dSGreg Kroah-Hartman 5740c9acb1aSNicolas Pitre if (use_unicode(inode)) 5750c9acb1aSNicolas Pitre return -EOPNOTSUPP; 5760c9acb1aSNicolas Pitre 577fcdba07eSJiri Olsa con_buf = (char *) __get_free_page(GFP_KERNEL); 578fcdba07eSJiri Olsa if (!con_buf) 579fcdba07eSJiri Olsa return -ENOMEM; 58060d4ae8dSGreg Kroah-Hartman 58160d4ae8dSGreg Kroah-Hartman pos = *ppos; 58260d4ae8dSGreg Kroah-Hartman 58360d4ae8dSGreg Kroah-Hartman /* Select the proper current console and verify 58460d4ae8dSGreg Kroah-Hartman * sanity of the situation under the console lock. 58560d4ae8dSGreg Kroah-Hartman */ 586ac751efaSTorben Hohn console_lock(); 58760d4ae8dSGreg Kroah-Hartman 588d21b0be2SNicolas Pitre attr = use_attributes(inode); 58960d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 590fcdba07eSJiri Olsa vc = vcs_vc(inode, &viewed); 591fcdba07eSJiri Olsa if (!vc) 59260d4ae8dSGreg Kroah-Hartman goto unlock_out; 59360d4ae8dSGreg Kroah-Hartman 59471d4abfaSJiri Slaby size = vcs_size(vc, attr, false); 59571d4abfaSJiri Slaby if (size < 0) { 59671d4abfaSJiri Slaby ret = size; 59771d4abfaSJiri Slaby goto unlock_out; 59871d4abfaSJiri Slaby } 59960d4ae8dSGreg Kroah-Hartman ret = -EINVAL; 60060d4ae8dSGreg Kroah-Hartman if (pos < 0 || pos > size) 60160d4ae8dSGreg Kroah-Hartman goto unlock_out; 60260d4ae8dSGreg Kroah-Hartman if (count > size - pos) 60360d4ae8dSGreg Kroah-Hartman count = size - pos; 60460d4ae8dSGreg Kroah-Hartman written = 0; 60560d4ae8dSGreg Kroah-Hartman while (count) { 60695e0d57fSJiri Slaby unsigned int this_round = count; 60760d4ae8dSGreg Kroah-Hartman 60860d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) 60960d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 61060d4ae8dSGreg Kroah-Hartman 61160d4ae8dSGreg Kroah-Hartman /* Temporarily drop the console lock so that we can read 61260d4ae8dSGreg Kroah-Hartman * in the write data from userspace safely. 61360d4ae8dSGreg Kroah-Hartman */ 614ac751efaSTorben Hohn console_unlock(); 61560d4ae8dSGreg Kroah-Hartman ret = copy_from_user(con_buf, buf, this_round); 616ac751efaSTorben Hohn console_lock(); 61760d4ae8dSGreg Kroah-Hartman 61860d4ae8dSGreg Kroah-Hartman if (ret) { 61960d4ae8dSGreg Kroah-Hartman this_round -= ret; 62060d4ae8dSGreg Kroah-Hartman if (!this_round) { 62160d4ae8dSGreg Kroah-Hartman /* Abort loop if no data were copied. Otherwise 62260d4ae8dSGreg Kroah-Hartman * fail with -EFAULT. 62360d4ae8dSGreg Kroah-Hartman */ 62460d4ae8dSGreg Kroah-Hartman if (written) 62560d4ae8dSGreg Kroah-Hartman break; 62660d4ae8dSGreg Kroah-Hartman ret = -EFAULT; 62760d4ae8dSGreg Kroah-Hartman goto unlock_out; 62860d4ae8dSGreg Kroah-Hartman } 62960d4ae8dSGreg Kroah-Hartman } 63060d4ae8dSGreg Kroah-Hartman 63160d4ae8dSGreg Kroah-Hartman /* The vcs_size might have changed while we slept to grab 63260d4ae8dSGreg Kroah-Hartman * the user buffer, so recheck. 63360d4ae8dSGreg Kroah-Hartman * Return data written up to now on failure. 63460d4ae8dSGreg Kroah-Hartman */ 63571d4abfaSJiri Slaby size = vcs_size(vc, attr, false); 636dc1892c4SJiri Olsa if (size < 0) { 637dc1892c4SJiri Olsa if (written) 638dc1892c4SJiri Olsa break; 639dc1892c4SJiri Olsa ret = size; 640dc1892c4SJiri Olsa goto unlock_out; 641dc1892c4SJiri Olsa } 64260d4ae8dSGreg Kroah-Hartman if (pos >= size) 64360d4ae8dSGreg Kroah-Hartman break; 64460d4ae8dSGreg Kroah-Hartman if (this_round > size - pos) 64560d4ae8dSGreg Kroah-Hartman this_round = size - pos; 64660d4ae8dSGreg Kroah-Hartman 64760d4ae8dSGreg Kroah-Hartman /* OK, now actually push the write to the console 64860d4ae8dSGreg Kroah-Hartman * under the lock using the local kernel buffer. 64960d4ae8dSGreg Kroah-Hartman */ 65060d4ae8dSGreg Kroah-Hartman 65195e0d57fSJiri Slaby if (attr) 65295e0d57fSJiri Slaby org = vcs_write_buf(vc, con_buf, pos, this_round, 65395e0d57fSJiri Slaby viewed, &org0); 65495e0d57fSJiri Slaby else 6559e636378SJiri Slaby org = vcs_write_buf_noattr(vc, con_buf, pos, this_round, 6569e636378SJiri Slaby viewed, &org0); 65760d4ae8dSGreg Kroah-Hartman 65895e0d57fSJiri Slaby count -= this_round; 65995e0d57fSJiri Slaby written += this_round; 66095e0d57fSJiri Slaby buf += this_round; 66195e0d57fSJiri Slaby pos += this_round; 66295e0d57fSJiri Slaby if (org) 66360d4ae8dSGreg Kroah-Hartman update_region(vc, (unsigned long)(org0), org - org0); 66460d4ae8dSGreg Kroah-Hartman } 66560d4ae8dSGreg Kroah-Hartman *ppos += written; 66660d4ae8dSGreg Kroah-Hartman ret = written; 66760d4ae8dSGreg Kroah-Hartman if (written) 66860d4ae8dSGreg Kroah-Hartman vcs_scr_updated(vc); 66960d4ae8dSGreg Kroah-Hartman 67060d4ae8dSGreg Kroah-Hartman unlock_out: 671ac751efaSTorben Hohn console_unlock(); 672fcdba07eSJiri Olsa free_page((unsigned long) con_buf); 67360d4ae8dSGreg Kroah-Hartman return ret; 67460d4ae8dSGreg Kroah-Hartman } 67560d4ae8dSGreg Kroah-Hartman 676afc9a42bSAl Viro static __poll_t 67760d4ae8dSGreg Kroah-Hartman vcs_poll(struct file *file, poll_table *wait) 67860d4ae8dSGreg Kroah-Hartman { 67960d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll = vcs_poll_data_get(file); 6801bf931abSNicolas Pitre __poll_t ret = DEFAULT_POLLMASK|EPOLLERR; 68160d4ae8dSGreg Kroah-Hartman 68260d4ae8dSGreg Kroah-Hartman if (poll) { 68360d4ae8dSGreg Kroah-Hartman poll_wait(file, &poll->waitq, wait); 6841bf931abSNicolas Pitre switch (poll->event) { 6851bf931abSNicolas Pitre case VT_UPDATE: 6861bf931abSNicolas Pitre ret = DEFAULT_POLLMASK|EPOLLPRI; 6871bf931abSNicolas Pitre break; 6881bf931abSNicolas Pitre case VT_DEALLOCATE: 6891bf931abSNicolas Pitre ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR; 6901bf931abSNicolas Pitre break; 6911bf931abSNicolas Pitre case 0: 69247c344d0SNicolas Pitre ret = DEFAULT_POLLMASK; 6931bf931abSNicolas Pitre break; 6941bf931abSNicolas Pitre } 69560d4ae8dSGreg Kroah-Hartman } 69660d4ae8dSGreg Kroah-Hartman return ret; 69760d4ae8dSGreg Kroah-Hartman } 69860d4ae8dSGreg Kroah-Hartman 69960d4ae8dSGreg Kroah-Hartman static int 70060d4ae8dSGreg Kroah-Hartman vcs_fasync(int fd, struct file *file, int on) 70160d4ae8dSGreg Kroah-Hartman { 70260d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll = file->private_data; 70360d4ae8dSGreg Kroah-Hartman 70460d4ae8dSGreg Kroah-Hartman if (!poll) { 70560d4ae8dSGreg Kroah-Hartman /* don't allocate anything if all we want is disable fasync */ 70660d4ae8dSGreg Kroah-Hartman if (!on) 70760d4ae8dSGreg Kroah-Hartman return 0; 70860d4ae8dSGreg Kroah-Hartman poll = vcs_poll_data_get(file); 70960d4ae8dSGreg Kroah-Hartman if (!poll) 71060d4ae8dSGreg Kroah-Hartman return -ENOMEM; 71160d4ae8dSGreg Kroah-Hartman } 71260d4ae8dSGreg Kroah-Hartman 71360d4ae8dSGreg Kroah-Hartman return fasync_helper(fd, file, on, &poll->fasync); 71460d4ae8dSGreg Kroah-Hartman } 71560d4ae8dSGreg Kroah-Hartman 71660d4ae8dSGreg Kroah-Hartman static int 71760d4ae8dSGreg Kroah-Hartman vcs_open(struct inode *inode, struct file *filp) 71860d4ae8dSGreg Kroah-Hartman { 719d21b0be2SNicolas Pitre unsigned int currcons = console(inode); 720d21b0be2SNicolas Pitre bool attr = use_attributes(inode); 721d21b0be2SNicolas Pitre bool uni_mode = use_unicode(inode); 72260d4ae8dSGreg Kroah-Hartman int ret = 0; 72360d4ae8dSGreg Kroah-Hartman 724d21b0be2SNicolas Pitre /* we currently don't support attributes in unicode mode */ 725d21b0be2SNicolas Pitre if (attr && uni_mode) 726d21b0be2SNicolas Pitre return -EOPNOTSUPP; 727d21b0be2SNicolas Pitre 7284001d7b7SAlan Cox console_lock(); 72960d4ae8dSGreg Kroah-Hartman if(currcons && !vc_cons_allocated(currcons-1)) 73060d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 7314001d7b7SAlan Cox console_unlock(); 73260d4ae8dSGreg Kroah-Hartman return ret; 73360d4ae8dSGreg Kroah-Hartman } 73460d4ae8dSGreg Kroah-Hartman 73560d4ae8dSGreg Kroah-Hartman static int vcs_release(struct inode *inode, struct file *file) 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 vcs_poll_data_free(poll); 74160d4ae8dSGreg Kroah-Hartman return 0; 74260d4ae8dSGreg Kroah-Hartman } 74360d4ae8dSGreg Kroah-Hartman 74460d4ae8dSGreg Kroah-Hartman static const struct file_operations vcs_fops = { 74560d4ae8dSGreg Kroah-Hartman .llseek = vcs_lseek, 74660d4ae8dSGreg Kroah-Hartman .read = vcs_read, 74760d4ae8dSGreg Kroah-Hartman .write = vcs_write, 74860d4ae8dSGreg Kroah-Hartman .poll = vcs_poll, 74960d4ae8dSGreg Kroah-Hartman .fasync = vcs_fasync, 75060d4ae8dSGreg Kroah-Hartman .open = vcs_open, 75160d4ae8dSGreg Kroah-Hartman .release = vcs_release, 75260d4ae8dSGreg Kroah-Hartman }; 75360d4ae8dSGreg Kroah-Hartman 75460d4ae8dSGreg Kroah-Hartman static struct class *vc_class; 75560d4ae8dSGreg Kroah-Hartman 75660d4ae8dSGreg Kroah-Hartman void vcs_make_sysfs(int index) 75760d4ae8dSGreg Kroah-Hartman { 75860d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, 75960d4ae8dSGreg Kroah-Hartman "vcs%u", index + 1); 760d21b0be2SNicolas Pitre device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL, 761d21b0be2SNicolas Pitre "vcsu%u", index + 1); 76260d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, 76360d4ae8dSGreg Kroah-Hartman "vcsa%u", index + 1); 76460d4ae8dSGreg Kroah-Hartman } 76560d4ae8dSGreg Kroah-Hartman 76660d4ae8dSGreg Kroah-Hartman void vcs_remove_sysfs(int index) 76760d4ae8dSGreg Kroah-Hartman { 76860d4ae8dSGreg Kroah-Hartman device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1)); 769d21b0be2SNicolas Pitre device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65)); 77060d4ae8dSGreg Kroah-Hartman device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129)); 77160d4ae8dSGreg Kroah-Hartman } 77260d4ae8dSGreg Kroah-Hartman 77360d4ae8dSGreg Kroah-Hartman int __init vcs_init(void) 77460d4ae8dSGreg Kroah-Hartman { 77560d4ae8dSGreg Kroah-Hartman unsigned int i; 77660d4ae8dSGreg Kroah-Hartman 77760d4ae8dSGreg Kroah-Hartman if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops)) 77860d4ae8dSGreg Kroah-Hartman panic("unable to get major %d for vcs device", VCS_MAJOR); 77960d4ae8dSGreg Kroah-Hartman vc_class = class_create(THIS_MODULE, "vc"); 78060d4ae8dSGreg Kroah-Hartman 78160d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs"); 782d21b0be2SNicolas Pitre device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu"); 78360d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa"); 78460d4ae8dSGreg Kroah-Hartman for (i = 0; i < MIN_NR_CONSOLES; i++) 78560d4ae8dSGreg Kroah-Hartman vcs_make_sysfs(i); 78660d4ae8dSGreg Kroah-Hartman return 0; 78760d4ae8dSGreg Kroah-Hartman } 788