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 566d507c75SJiri 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 252*0f66eee3SJiri Slaby static int vcs_read_buf_uni(struct vc_data *vc, char *con_buf, 253*0f66eee3SJiri Slaby unsigned int pos, unsigned int count, bool viewed) 254*0f66eee3SJiri Slaby { 255*0f66eee3SJiri Slaby unsigned int nr, row, col, maxcol = vc->vc_cols; 256*0f66eee3SJiri Slaby int ret; 257*0f66eee3SJiri Slaby 258*0f66eee3SJiri Slaby ret = vc_uniscr_check(vc); 259*0f66eee3SJiri Slaby if (ret) 260*0f66eee3SJiri Slaby return ret; 261*0f66eee3SJiri Slaby 262*0f66eee3SJiri Slaby pos /= 4; 263*0f66eee3SJiri Slaby row = pos / maxcol; 264*0f66eee3SJiri Slaby col = pos % maxcol; 265*0f66eee3SJiri Slaby nr = maxcol - col; 266*0f66eee3SJiri Slaby do { 267*0f66eee3SJiri Slaby if (nr > count / 4) 268*0f66eee3SJiri Slaby nr = count / 4; 269*0f66eee3SJiri Slaby vc_uniscr_copy_line(vc, con_buf, viewed, row, col, nr); 270*0f66eee3SJiri Slaby con_buf += nr * 4; 271*0f66eee3SJiri Slaby count -= nr * 4; 272*0f66eee3SJiri Slaby row++; 273*0f66eee3SJiri Slaby col = 0; 274*0f66eee3SJiri Slaby nr = maxcol; 275*0f66eee3SJiri Slaby } while (count); 276*0f66eee3SJiri Slaby 277*0f66eee3SJiri Slaby return 0; 278*0f66eee3SJiri Slaby } 27960d4ae8dSGreg Kroah-Hartman 28060d4ae8dSGreg Kroah-Hartman static ssize_t 28160d4ae8dSGreg Kroah-Hartman vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 28260d4ae8dSGreg Kroah-Hartman { 283496ad9aaSAl Viro struct inode *inode = file_inode(file); 28460d4ae8dSGreg Kroah-Hartman struct vc_data *vc; 28560d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll; 28636c39220SJiri Slaby u16 *org; 287*0f66eee3SJiri Slaby unsigned int read, col, maxcol; 28860d4ae8dSGreg Kroah-Hartman ssize_t ret; 289fcdba07eSJiri Olsa char *con_buf; 29036c39220SJiri Slaby loff_t pos; 29136c39220SJiri Slaby bool viewed, attr, uni_mode; 29260d4ae8dSGreg Kroah-Hartman 293fcdba07eSJiri Olsa con_buf = (char *) __get_free_page(GFP_KERNEL); 294fcdba07eSJiri Olsa if (!con_buf) 295fcdba07eSJiri Olsa return -ENOMEM; 29660d4ae8dSGreg Kroah-Hartman 29760d4ae8dSGreg Kroah-Hartman pos = *ppos; 29860d4ae8dSGreg Kroah-Hartman 29960d4ae8dSGreg Kroah-Hartman /* Select the proper current console and verify 30060d4ae8dSGreg Kroah-Hartman * sanity of the situation under the console lock. 30160d4ae8dSGreg Kroah-Hartman */ 302ac751efaSTorben Hohn console_lock(); 30360d4ae8dSGreg Kroah-Hartman 304d21b0be2SNicolas Pitre uni_mode = use_unicode(inode); 305d21b0be2SNicolas Pitre attr = use_attributes(inode); 30660d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 307fcdba07eSJiri Olsa vc = vcs_vc(inode, &viewed); 308fcdba07eSJiri Olsa if (!vc) 30960d4ae8dSGreg Kroah-Hartman goto unlock_out; 31060d4ae8dSGreg Kroah-Hartman 31160d4ae8dSGreg Kroah-Hartman ret = -EINVAL; 31260d4ae8dSGreg Kroah-Hartman if (pos < 0) 31360d4ae8dSGreg Kroah-Hartman goto unlock_out; 314d21b0be2SNicolas Pitre /* we enforce 32-bit alignment for pos and count in unicode mode */ 315d21b0be2SNicolas Pitre if (uni_mode && (pos | count) & 3) 316d21b0be2SNicolas Pitre goto unlock_out; 317d21b0be2SNicolas Pitre 31860d4ae8dSGreg Kroah-Hartman poll = file->private_data; 31960d4ae8dSGreg Kroah-Hartman if (count && poll) 3201bf931abSNicolas Pitre poll->event = 0; 32160d4ae8dSGreg Kroah-Hartman read = 0; 32260d4ae8dSGreg Kroah-Hartman ret = 0; 32360d4ae8dSGreg Kroah-Hartman while (count) { 32460d4ae8dSGreg Kroah-Hartman char *con_buf0, *con_buf_start; 32536c39220SJiri Slaby unsigned int this_round, orig_count, p = pos; 32636c39220SJiri Slaby int size; 32760d4ae8dSGreg Kroah-Hartman 32860d4ae8dSGreg Kroah-Hartman /* Check whether we are above size each round, 32960d4ae8dSGreg Kroah-Hartman * as copy_to_user at the end of this loop 33060d4ae8dSGreg Kroah-Hartman * could sleep. 33160d4ae8dSGreg Kroah-Hartman */ 33271d4abfaSJiri Slaby size = vcs_size(vc, attr, uni_mode); 333dc1892c4SJiri Olsa if (size < 0) { 334dc1892c4SJiri Olsa if (read) 335dc1892c4SJiri Olsa break; 336dc1892c4SJiri Olsa ret = size; 337dc1892c4SJiri Olsa goto unlock_out; 338dc1892c4SJiri Olsa } 33960d4ae8dSGreg Kroah-Hartman if (pos >= size) 34060d4ae8dSGreg Kroah-Hartman break; 34160d4ae8dSGreg Kroah-Hartman if (count > size - pos) 34260d4ae8dSGreg Kroah-Hartman count = size - pos; 34360d4ae8dSGreg Kroah-Hartman 34460d4ae8dSGreg Kroah-Hartman this_round = count; 34560d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) 34660d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 34760d4ae8dSGreg Kroah-Hartman 34860d4ae8dSGreg Kroah-Hartman /* Perform the whole read into the local con_buf. 34960d4ae8dSGreg Kroah-Hartman * Then we can drop the console spinlock and safely 35060d4ae8dSGreg Kroah-Hartman * attempt to move it to userspace. 35160d4ae8dSGreg Kroah-Hartman */ 35260d4ae8dSGreg Kroah-Hartman 35360d4ae8dSGreg Kroah-Hartman con_buf_start = con_buf0 = con_buf; 35460d4ae8dSGreg Kroah-Hartman orig_count = this_round; 35560d4ae8dSGreg Kroah-Hartman maxcol = vc->vc_cols; 356d21b0be2SNicolas Pitre if (uni_mode) { 357*0f66eee3SJiri Slaby ret = vcs_read_buf_uni(vc, con_buf, pos, this_round, 358*0f66eee3SJiri Slaby viewed); 359d21b0be2SNicolas Pitre if (ret) 360d21b0be2SNicolas Pitre break; 361d21b0be2SNicolas Pitre } else if (!attr) { 36260d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 36360d4ae8dSGreg Kroah-Hartman col = p % maxcol; 36460d4ae8dSGreg Kroah-Hartman p += maxcol - col; 36560d4ae8dSGreg Kroah-Hartman while (this_round-- > 0) { 36660d4ae8dSGreg Kroah-Hartman *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff); 36760d4ae8dSGreg Kroah-Hartman if (++col == maxcol) { 36860d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 36960d4ae8dSGreg Kroah-Hartman col = 0; 37060d4ae8dSGreg Kroah-Hartman p += maxcol; 37160d4ae8dSGreg Kroah-Hartman } 37260d4ae8dSGreg Kroah-Hartman } 37360d4ae8dSGreg Kroah-Hartman } else { 37460d4ae8dSGreg Kroah-Hartman if (p < HEADER_SIZE) { 3758a085494SNicolas Pitre /* clamp header values if they don't fit */ 3768a085494SNicolas Pitre con_buf0[0] = min(vc->vc_rows, 0xFFu); 3778a085494SNicolas Pitre con_buf0[1] = min(vc->vc_cols, 0xFFu); 37860d4ae8dSGreg Kroah-Hartman getconsxy(vc, con_buf0 + 2); 37960d4ae8dSGreg Kroah-Hartman 38060d4ae8dSGreg Kroah-Hartman con_buf_start += p; 38160d4ae8dSGreg Kroah-Hartman this_round += p; 38260d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) { 38360d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 38460d4ae8dSGreg Kroah-Hartman orig_count = this_round - p; 38560d4ae8dSGreg Kroah-Hartman } 38660d4ae8dSGreg Kroah-Hartman 38760d4ae8dSGreg Kroah-Hartman /* Advance state pointers and move on. */ 3886d507c75SJiri Slaby this_round -= min(HEADER_SIZE, this_round); 38960d4ae8dSGreg Kroah-Hartman p = HEADER_SIZE; 39060d4ae8dSGreg Kroah-Hartman con_buf0 = con_buf + HEADER_SIZE; 39160d4ae8dSGreg Kroah-Hartman /* If this_round >= 0, then p is even... */ 39260d4ae8dSGreg Kroah-Hartman } else if (p & 1) { 39360d4ae8dSGreg Kroah-Hartman /* Skip first byte for output if start address is odd 39460d4ae8dSGreg Kroah-Hartman * Update region sizes up/down depending on free 39560d4ae8dSGreg Kroah-Hartman * space in buffer. 39660d4ae8dSGreg Kroah-Hartman */ 39760d4ae8dSGreg Kroah-Hartman con_buf_start++; 39860d4ae8dSGreg Kroah-Hartman if (this_round < CON_BUF_SIZE) 39960d4ae8dSGreg Kroah-Hartman this_round++; 40060d4ae8dSGreg Kroah-Hartman else 40160d4ae8dSGreg Kroah-Hartman orig_count--; 40260d4ae8dSGreg Kroah-Hartman } 40360d4ae8dSGreg Kroah-Hartman if (this_round > 0) { 40460d4ae8dSGreg Kroah-Hartman unsigned short *tmp_buf = (unsigned short *)con_buf0; 40560d4ae8dSGreg Kroah-Hartman 40660d4ae8dSGreg Kroah-Hartman p -= HEADER_SIZE; 40760d4ae8dSGreg Kroah-Hartman p /= 2; 40860d4ae8dSGreg Kroah-Hartman col = p % maxcol; 40960d4ae8dSGreg Kroah-Hartman 41060d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 41160d4ae8dSGreg Kroah-Hartman p += maxcol - col; 41260d4ae8dSGreg Kroah-Hartman 41360d4ae8dSGreg Kroah-Hartman /* Buffer has even length, so we can always copy 41460d4ae8dSGreg Kroah-Hartman * character + attribute. We do not copy last byte 41560d4ae8dSGreg Kroah-Hartman * to userspace if this_round is odd. 41660d4ae8dSGreg Kroah-Hartman */ 41760d4ae8dSGreg Kroah-Hartman this_round = (this_round + 1) >> 1; 41860d4ae8dSGreg Kroah-Hartman 41960d4ae8dSGreg Kroah-Hartman while (this_round) { 42060d4ae8dSGreg Kroah-Hartman *tmp_buf++ = vcs_scr_readw(vc, org++); 42160d4ae8dSGreg Kroah-Hartman this_round --; 42260d4ae8dSGreg Kroah-Hartman if (++col == maxcol) { 42360d4ae8dSGreg Kroah-Hartman org = screen_pos(vc, p, viewed); 42460d4ae8dSGreg Kroah-Hartman col = 0; 42560d4ae8dSGreg Kroah-Hartman p += maxcol; 42660d4ae8dSGreg Kroah-Hartman } 42760d4ae8dSGreg Kroah-Hartman } 42860d4ae8dSGreg Kroah-Hartman } 42960d4ae8dSGreg Kroah-Hartman } 43060d4ae8dSGreg Kroah-Hartman 43160d4ae8dSGreg Kroah-Hartman /* Finally, release the console semaphore while we push 43260d4ae8dSGreg Kroah-Hartman * all the data to userspace from our temporary buffer. 43360d4ae8dSGreg Kroah-Hartman * 43460d4ae8dSGreg Kroah-Hartman * AKPM: Even though it's a semaphore, we should drop it because 43560d4ae8dSGreg Kroah-Hartman * the pagefault handling code may want to call printk(). 43660d4ae8dSGreg Kroah-Hartman */ 43760d4ae8dSGreg Kroah-Hartman 438ac751efaSTorben Hohn console_unlock(); 43960d4ae8dSGreg Kroah-Hartman ret = copy_to_user(buf, con_buf_start, orig_count); 440ac751efaSTorben Hohn console_lock(); 44160d4ae8dSGreg Kroah-Hartman 44260d4ae8dSGreg Kroah-Hartman if (ret) { 44360d4ae8dSGreg Kroah-Hartman read += (orig_count - ret); 44460d4ae8dSGreg Kroah-Hartman ret = -EFAULT; 44560d4ae8dSGreg Kroah-Hartman break; 44660d4ae8dSGreg Kroah-Hartman } 44760d4ae8dSGreg Kroah-Hartman buf += orig_count; 44860d4ae8dSGreg Kroah-Hartman pos += orig_count; 44960d4ae8dSGreg Kroah-Hartman read += orig_count; 45060d4ae8dSGreg Kroah-Hartman count -= orig_count; 45160d4ae8dSGreg Kroah-Hartman } 45260d4ae8dSGreg Kroah-Hartman *ppos += read; 45360d4ae8dSGreg Kroah-Hartman if (read) 45460d4ae8dSGreg Kroah-Hartman ret = read; 45560d4ae8dSGreg Kroah-Hartman unlock_out: 456ac751efaSTorben Hohn console_unlock(); 457fcdba07eSJiri Olsa free_page((unsigned long) con_buf); 45860d4ae8dSGreg Kroah-Hartman return ret; 45960d4ae8dSGreg Kroah-Hartman } 46060d4ae8dSGreg Kroah-Hartman 4619e636378SJiri Slaby static u16 *vcs_write_buf_noattr(struct vc_data *vc, const char *con_buf, 4629e636378SJiri Slaby unsigned int pos, unsigned int count, bool viewed, u16 **org0) 4639e636378SJiri Slaby { 4649e636378SJiri Slaby u16 *org; 4659e636378SJiri Slaby unsigned int col, maxcol = vc->vc_cols; 4669e636378SJiri Slaby 4679e636378SJiri Slaby *org0 = org = screen_pos(vc, pos, viewed); 4689e636378SJiri Slaby col = pos % maxcol; 4699e636378SJiri Slaby pos += maxcol - col; 4709e636378SJiri Slaby 4719e636378SJiri Slaby while (count > 0) { 4729e636378SJiri Slaby unsigned char c = *con_buf++; 4739e636378SJiri Slaby 4749e636378SJiri Slaby count--; 4759e636378SJiri Slaby vcs_scr_writew(vc, 4769e636378SJiri Slaby (vcs_scr_readw(vc, org) & 0xff00) | c, org); 4779e636378SJiri Slaby org++; 4789e636378SJiri Slaby if (++col == maxcol) { 4799e636378SJiri Slaby org = screen_pos(vc, pos, viewed); 4809e636378SJiri Slaby col = 0; 4819e636378SJiri Slaby pos += maxcol; 4829e636378SJiri Slaby } 4839e636378SJiri Slaby } 4849e636378SJiri Slaby 4859e636378SJiri Slaby return org; 4869e636378SJiri Slaby } 4879e636378SJiri Slaby 488d7c91c50SJiri Slaby /* 489d7c91c50SJiri Slaby * Compilers (gcc 10) are unable to optimize the swap in cpu_to_le16. So do it 490d7c91c50SJiri Slaby * the poor man way. 491d7c91c50SJiri Slaby */ 492d7c91c50SJiri Slaby static inline u16 vc_compile_le16(u8 hi, u8 lo) 493d7c91c50SJiri Slaby { 494d7c91c50SJiri Slaby #ifdef __BIG_ENDIAN 495d7c91c50SJiri Slaby return (lo << 8u) | hi; 496d7c91c50SJiri Slaby #else 497d7c91c50SJiri Slaby return (hi << 8u) | lo; 498d7c91c50SJiri Slaby #endif 499d7c91c50SJiri Slaby } 500d7c91c50SJiri Slaby 50195e0d57fSJiri Slaby static u16 *vcs_write_buf(struct vc_data *vc, const char *con_buf, 50295e0d57fSJiri Slaby unsigned int pos, unsigned int count, bool viewed, u16 **org0) 50395e0d57fSJiri Slaby { 50495e0d57fSJiri Slaby u16 *org; 50595e0d57fSJiri Slaby unsigned int col, maxcol = vc->vc_cols; 50695e0d57fSJiri Slaby unsigned char c; 50795e0d57fSJiri Slaby 50895e0d57fSJiri Slaby /* header */ 50995e0d57fSJiri Slaby if (pos < HEADER_SIZE) { 51095e0d57fSJiri Slaby char header[HEADER_SIZE]; 51195e0d57fSJiri Slaby 51295e0d57fSJiri Slaby getconsxy(vc, header + 2); 51395e0d57fSJiri Slaby while (pos < HEADER_SIZE && count > 0) { 51495e0d57fSJiri Slaby count--; 51595e0d57fSJiri Slaby header[pos++] = *con_buf++; 51695e0d57fSJiri Slaby } 51795e0d57fSJiri Slaby if (!viewed) 51895e0d57fSJiri Slaby putconsxy(vc, header + 2); 51995e0d57fSJiri Slaby } 52095e0d57fSJiri Slaby 52195e0d57fSJiri Slaby if (!count) 52295e0d57fSJiri Slaby return NULL; 52395e0d57fSJiri Slaby 52495e0d57fSJiri Slaby pos -= HEADER_SIZE; 52595e0d57fSJiri Slaby col = (pos/2) % maxcol; 52695e0d57fSJiri Slaby 52795e0d57fSJiri Slaby *org0 = org = screen_pos(vc, pos/2, viewed); 52895e0d57fSJiri Slaby 52995e0d57fSJiri Slaby /* odd pos -- the first single character */ 53095e0d57fSJiri Slaby if (pos & 1) { 53195e0d57fSJiri Slaby count--; 53295e0d57fSJiri Slaby c = *con_buf++; 533d7c91c50SJiri Slaby vcs_scr_writew(vc, vc_compile_le16(c, vcs_scr_readw(vc, org)), 534d7c91c50SJiri Slaby org); 53595e0d57fSJiri Slaby org++; 53695e0d57fSJiri Slaby pos++; 53795e0d57fSJiri Slaby if (++col == maxcol) { 53895e0d57fSJiri Slaby org = screen_pos(vc, pos/2, viewed); 53995e0d57fSJiri Slaby col = 0; 54095e0d57fSJiri Slaby } 54195e0d57fSJiri Slaby } 54295e0d57fSJiri Slaby 54395e0d57fSJiri Slaby pos /= 2; 54495e0d57fSJiri Slaby pos += maxcol - col; 54595e0d57fSJiri Slaby 54695e0d57fSJiri Slaby /* even pos -- handle attr+character pairs */ 54795e0d57fSJiri Slaby while (count > 1) { 54895e0d57fSJiri Slaby unsigned short w; 54995e0d57fSJiri Slaby 55095e0d57fSJiri Slaby w = get_unaligned(((unsigned short *)con_buf)); 55195e0d57fSJiri Slaby vcs_scr_writew(vc, w, org++); 55295e0d57fSJiri Slaby con_buf += 2; 55395e0d57fSJiri Slaby count -= 2; 55495e0d57fSJiri Slaby if (++col == maxcol) { 55595e0d57fSJiri Slaby org = screen_pos(vc, pos, viewed); 55695e0d57fSJiri Slaby col = 0; 55795e0d57fSJiri Slaby pos += maxcol; 55895e0d57fSJiri Slaby } 55995e0d57fSJiri Slaby } 56095e0d57fSJiri Slaby 56195e0d57fSJiri Slaby if (!count) 56295e0d57fSJiri Slaby return org; 56395e0d57fSJiri Slaby 56495e0d57fSJiri Slaby /* odd pos -- the remaining character */ 56595e0d57fSJiri Slaby c = *con_buf++; 566d7c91c50SJiri Slaby vcs_scr_writew(vc, vc_compile_le16(vcs_scr_readw(vc, org) >> 8, c), 567d7c91c50SJiri Slaby org); 56895e0d57fSJiri Slaby 56995e0d57fSJiri Slaby return org; 57095e0d57fSJiri Slaby } 57195e0d57fSJiri Slaby 57260d4ae8dSGreg Kroah-Hartman static ssize_t 57360d4ae8dSGreg Kroah-Hartman vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 57460d4ae8dSGreg Kroah-Hartman { 575496ad9aaSAl Viro struct inode *inode = file_inode(file); 57660d4ae8dSGreg Kroah-Hartman struct vc_data *vc; 57795e0d57fSJiri Slaby char *con_buf; 57895e0d57fSJiri Slaby u16 *org0, *org; 57995e0d57fSJiri Slaby unsigned int written; 5802665bef4SJiri Slaby int size; 5812665bef4SJiri Slaby ssize_t ret; 5822665bef4SJiri Slaby loff_t pos; 5832665bef4SJiri Slaby bool viewed, attr; 58460d4ae8dSGreg Kroah-Hartman 5850c9acb1aSNicolas Pitre if (use_unicode(inode)) 5860c9acb1aSNicolas Pitre return -EOPNOTSUPP; 5870c9acb1aSNicolas Pitre 588fcdba07eSJiri Olsa con_buf = (char *) __get_free_page(GFP_KERNEL); 589fcdba07eSJiri Olsa if (!con_buf) 590fcdba07eSJiri Olsa return -ENOMEM; 59160d4ae8dSGreg Kroah-Hartman 59260d4ae8dSGreg Kroah-Hartman pos = *ppos; 59360d4ae8dSGreg Kroah-Hartman 59460d4ae8dSGreg Kroah-Hartman /* Select the proper current console and verify 59560d4ae8dSGreg Kroah-Hartman * sanity of the situation under the console lock. 59660d4ae8dSGreg Kroah-Hartman */ 597ac751efaSTorben Hohn console_lock(); 59860d4ae8dSGreg Kroah-Hartman 599d21b0be2SNicolas Pitre attr = use_attributes(inode); 60060d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 601fcdba07eSJiri Olsa vc = vcs_vc(inode, &viewed); 602fcdba07eSJiri Olsa if (!vc) 60360d4ae8dSGreg Kroah-Hartman goto unlock_out; 60460d4ae8dSGreg Kroah-Hartman 60571d4abfaSJiri Slaby size = vcs_size(vc, attr, false); 60671d4abfaSJiri Slaby if (size < 0) { 60771d4abfaSJiri Slaby ret = size; 60871d4abfaSJiri Slaby goto unlock_out; 60971d4abfaSJiri Slaby } 61060d4ae8dSGreg Kroah-Hartman ret = -EINVAL; 61160d4ae8dSGreg Kroah-Hartman if (pos < 0 || pos > size) 61260d4ae8dSGreg Kroah-Hartman goto unlock_out; 61360d4ae8dSGreg Kroah-Hartman if (count > size - pos) 61460d4ae8dSGreg Kroah-Hartman count = size - pos; 61560d4ae8dSGreg Kroah-Hartman written = 0; 61660d4ae8dSGreg Kroah-Hartman while (count) { 61795e0d57fSJiri Slaby unsigned int this_round = count; 61860d4ae8dSGreg Kroah-Hartman 61960d4ae8dSGreg Kroah-Hartman if (this_round > CON_BUF_SIZE) 62060d4ae8dSGreg Kroah-Hartman this_round = CON_BUF_SIZE; 62160d4ae8dSGreg Kroah-Hartman 62260d4ae8dSGreg Kroah-Hartman /* Temporarily drop the console lock so that we can read 62360d4ae8dSGreg Kroah-Hartman * in the write data from userspace safely. 62460d4ae8dSGreg Kroah-Hartman */ 625ac751efaSTorben Hohn console_unlock(); 62660d4ae8dSGreg Kroah-Hartman ret = copy_from_user(con_buf, buf, this_round); 627ac751efaSTorben Hohn console_lock(); 62860d4ae8dSGreg Kroah-Hartman 62960d4ae8dSGreg Kroah-Hartman if (ret) { 63060d4ae8dSGreg Kroah-Hartman this_round -= ret; 63160d4ae8dSGreg Kroah-Hartman if (!this_round) { 63260d4ae8dSGreg Kroah-Hartman /* Abort loop if no data were copied. Otherwise 63360d4ae8dSGreg Kroah-Hartman * fail with -EFAULT. 63460d4ae8dSGreg Kroah-Hartman */ 63560d4ae8dSGreg Kroah-Hartman if (written) 63660d4ae8dSGreg Kroah-Hartman break; 63760d4ae8dSGreg Kroah-Hartman ret = -EFAULT; 63860d4ae8dSGreg Kroah-Hartman goto unlock_out; 63960d4ae8dSGreg Kroah-Hartman } 64060d4ae8dSGreg Kroah-Hartman } 64160d4ae8dSGreg Kroah-Hartman 64260d4ae8dSGreg Kroah-Hartman /* The vcs_size might have changed while we slept to grab 64360d4ae8dSGreg Kroah-Hartman * the user buffer, so recheck. 64460d4ae8dSGreg Kroah-Hartman * Return data written up to now on failure. 64560d4ae8dSGreg Kroah-Hartman */ 64671d4abfaSJiri Slaby size = vcs_size(vc, attr, false); 647dc1892c4SJiri Olsa if (size < 0) { 648dc1892c4SJiri Olsa if (written) 649dc1892c4SJiri Olsa break; 650dc1892c4SJiri Olsa ret = size; 651dc1892c4SJiri Olsa goto unlock_out; 652dc1892c4SJiri Olsa } 65360d4ae8dSGreg Kroah-Hartman if (pos >= size) 65460d4ae8dSGreg Kroah-Hartman break; 65560d4ae8dSGreg Kroah-Hartman if (this_round > size - pos) 65660d4ae8dSGreg Kroah-Hartman this_round = size - pos; 65760d4ae8dSGreg Kroah-Hartman 65860d4ae8dSGreg Kroah-Hartman /* OK, now actually push the write to the console 65960d4ae8dSGreg Kroah-Hartman * under the lock using the local kernel buffer. 66060d4ae8dSGreg Kroah-Hartman */ 66160d4ae8dSGreg Kroah-Hartman 66295e0d57fSJiri Slaby if (attr) 66395e0d57fSJiri Slaby org = vcs_write_buf(vc, con_buf, pos, this_round, 66495e0d57fSJiri Slaby viewed, &org0); 66595e0d57fSJiri Slaby else 6669e636378SJiri Slaby org = vcs_write_buf_noattr(vc, con_buf, pos, this_round, 6679e636378SJiri Slaby viewed, &org0); 66860d4ae8dSGreg Kroah-Hartman 66995e0d57fSJiri Slaby count -= this_round; 67095e0d57fSJiri Slaby written += this_round; 67195e0d57fSJiri Slaby buf += this_round; 67295e0d57fSJiri Slaby pos += this_round; 67395e0d57fSJiri Slaby if (org) 67460d4ae8dSGreg Kroah-Hartman update_region(vc, (unsigned long)(org0), org - org0); 67560d4ae8dSGreg Kroah-Hartman } 67660d4ae8dSGreg Kroah-Hartman *ppos += written; 67760d4ae8dSGreg Kroah-Hartman ret = written; 67860d4ae8dSGreg Kroah-Hartman if (written) 67960d4ae8dSGreg Kroah-Hartman vcs_scr_updated(vc); 68060d4ae8dSGreg Kroah-Hartman 68160d4ae8dSGreg Kroah-Hartman unlock_out: 682ac751efaSTorben Hohn console_unlock(); 683fcdba07eSJiri Olsa free_page((unsigned long) con_buf); 68460d4ae8dSGreg Kroah-Hartman return ret; 68560d4ae8dSGreg Kroah-Hartman } 68660d4ae8dSGreg Kroah-Hartman 687afc9a42bSAl Viro static __poll_t 68860d4ae8dSGreg Kroah-Hartman vcs_poll(struct file *file, poll_table *wait) 68960d4ae8dSGreg Kroah-Hartman { 69060d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll = vcs_poll_data_get(file); 6911bf931abSNicolas Pitre __poll_t ret = DEFAULT_POLLMASK|EPOLLERR; 69260d4ae8dSGreg Kroah-Hartman 69360d4ae8dSGreg Kroah-Hartman if (poll) { 69460d4ae8dSGreg Kroah-Hartman poll_wait(file, &poll->waitq, wait); 6951bf931abSNicolas Pitre switch (poll->event) { 6961bf931abSNicolas Pitre case VT_UPDATE: 6971bf931abSNicolas Pitre ret = DEFAULT_POLLMASK|EPOLLPRI; 6981bf931abSNicolas Pitre break; 6991bf931abSNicolas Pitre case VT_DEALLOCATE: 7001bf931abSNicolas Pitre ret = DEFAULT_POLLMASK|EPOLLHUP|EPOLLERR; 7011bf931abSNicolas Pitre break; 7021bf931abSNicolas Pitre case 0: 70347c344d0SNicolas Pitre ret = DEFAULT_POLLMASK; 7041bf931abSNicolas Pitre break; 7051bf931abSNicolas Pitre } 70660d4ae8dSGreg Kroah-Hartman } 70760d4ae8dSGreg Kroah-Hartman return ret; 70860d4ae8dSGreg Kroah-Hartman } 70960d4ae8dSGreg Kroah-Hartman 71060d4ae8dSGreg Kroah-Hartman static int 71160d4ae8dSGreg Kroah-Hartman vcs_fasync(int fd, struct file *file, int on) 71260d4ae8dSGreg Kroah-Hartman { 71360d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll = file->private_data; 71460d4ae8dSGreg Kroah-Hartman 71560d4ae8dSGreg Kroah-Hartman if (!poll) { 71660d4ae8dSGreg Kroah-Hartman /* don't allocate anything if all we want is disable fasync */ 71760d4ae8dSGreg Kroah-Hartman if (!on) 71860d4ae8dSGreg Kroah-Hartman return 0; 71960d4ae8dSGreg Kroah-Hartman poll = vcs_poll_data_get(file); 72060d4ae8dSGreg Kroah-Hartman if (!poll) 72160d4ae8dSGreg Kroah-Hartman return -ENOMEM; 72260d4ae8dSGreg Kroah-Hartman } 72360d4ae8dSGreg Kroah-Hartman 72460d4ae8dSGreg Kroah-Hartman return fasync_helper(fd, file, on, &poll->fasync); 72560d4ae8dSGreg Kroah-Hartman } 72660d4ae8dSGreg Kroah-Hartman 72760d4ae8dSGreg Kroah-Hartman static int 72860d4ae8dSGreg Kroah-Hartman vcs_open(struct inode *inode, struct file *filp) 72960d4ae8dSGreg Kroah-Hartman { 730d21b0be2SNicolas Pitre unsigned int currcons = console(inode); 731d21b0be2SNicolas Pitre bool attr = use_attributes(inode); 732d21b0be2SNicolas Pitre bool uni_mode = use_unicode(inode); 73360d4ae8dSGreg Kroah-Hartman int ret = 0; 73460d4ae8dSGreg Kroah-Hartman 735d21b0be2SNicolas Pitre /* we currently don't support attributes in unicode mode */ 736d21b0be2SNicolas Pitre if (attr && uni_mode) 737d21b0be2SNicolas Pitre return -EOPNOTSUPP; 738d21b0be2SNicolas Pitre 7394001d7b7SAlan Cox console_lock(); 74060d4ae8dSGreg Kroah-Hartman if(currcons && !vc_cons_allocated(currcons-1)) 74160d4ae8dSGreg Kroah-Hartman ret = -ENXIO; 7424001d7b7SAlan Cox console_unlock(); 74360d4ae8dSGreg Kroah-Hartman return ret; 74460d4ae8dSGreg Kroah-Hartman } 74560d4ae8dSGreg Kroah-Hartman 74660d4ae8dSGreg Kroah-Hartman static int vcs_release(struct inode *inode, struct file *file) 74760d4ae8dSGreg Kroah-Hartman { 74860d4ae8dSGreg Kroah-Hartman struct vcs_poll_data *poll = file->private_data; 74960d4ae8dSGreg Kroah-Hartman 75060d4ae8dSGreg Kroah-Hartman if (poll) 75160d4ae8dSGreg Kroah-Hartman vcs_poll_data_free(poll); 75260d4ae8dSGreg Kroah-Hartman return 0; 75360d4ae8dSGreg Kroah-Hartman } 75460d4ae8dSGreg Kroah-Hartman 75560d4ae8dSGreg Kroah-Hartman static const struct file_operations vcs_fops = { 75660d4ae8dSGreg Kroah-Hartman .llseek = vcs_lseek, 75760d4ae8dSGreg Kroah-Hartman .read = vcs_read, 75860d4ae8dSGreg Kroah-Hartman .write = vcs_write, 75960d4ae8dSGreg Kroah-Hartman .poll = vcs_poll, 76060d4ae8dSGreg Kroah-Hartman .fasync = vcs_fasync, 76160d4ae8dSGreg Kroah-Hartman .open = vcs_open, 76260d4ae8dSGreg Kroah-Hartman .release = vcs_release, 76360d4ae8dSGreg Kroah-Hartman }; 76460d4ae8dSGreg Kroah-Hartman 76560d4ae8dSGreg Kroah-Hartman static struct class *vc_class; 76660d4ae8dSGreg Kroah-Hartman 76760d4ae8dSGreg Kroah-Hartman void vcs_make_sysfs(int index) 76860d4ae8dSGreg Kroah-Hartman { 76960d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL, 77060d4ae8dSGreg Kroah-Hartman "vcs%u", index + 1); 771d21b0be2SNicolas Pitre device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 65), NULL, 772d21b0be2SNicolas Pitre "vcsu%u", index + 1); 77360d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL, 77460d4ae8dSGreg Kroah-Hartman "vcsa%u", index + 1); 77560d4ae8dSGreg Kroah-Hartman } 77660d4ae8dSGreg Kroah-Hartman 77760d4ae8dSGreg Kroah-Hartman void vcs_remove_sysfs(int index) 77860d4ae8dSGreg Kroah-Hartman { 77960d4ae8dSGreg Kroah-Hartman device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1)); 780d21b0be2SNicolas Pitre device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 65)); 78160d4ae8dSGreg Kroah-Hartman device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129)); 78260d4ae8dSGreg Kroah-Hartman } 78360d4ae8dSGreg Kroah-Hartman 78460d4ae8dSGreg Kroah-Hartman int __init vcs_init(void) 78560d4ae8dSGreg Kroah-Hartman { 78660d4ae8dSGreg Kroah-Hartman unsigned int i; 78760d4ae8dSGreg Kroah-Hartman 78860d4ae8dSGreg Kroah-Hartman if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops)) 78960d4ae8dSGreg Kroah-Hartman panic("unable to get major %d for vcs device", VCS_MAJOR); 79060d4ae8dSGreg Kroah-Hartman vc_class = class_create(THIS_MODULE, "vc"); 79160d4ae8dSGreg Kroah-Hartman 79260d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs"); 793d21b0be2SNicolas Pitre device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 64), NULL, "vcsu"); 79460d4ae8dSGreg Kroah-Hartman device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa"); 79560d4ae8dSGreg Kroah-Hartman for (i = 0; i < MIN_NR_CONSOLES; i++) 79660d4ae8dSGreg Kroah-Hartman vcs_make_sysfs(i); 79760d4ae8dSGreg Kroah-Hartman return 0; 79860d4ae8dSGreg Kroah-Hartman } 799