2 * linux/drivers/char/vc_screen.c
4 * Provide access to virtual console memory.
5 * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled)
6 * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
9 * /dev/vcsaN: idem, but including attributes, and prefixed with
10 * the 4 bytes lines,columns,x,y (as screendump used to give).
11 * Attribute/character pair is in native endianity.
14 * This replaces screendump and part of selection, so that the system
15 * administrator can control access using file system permissions.
17 * aeb@cwi.nl - efter Friedas begravelse - 950211
19 * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
20 * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
21 * - making it shorter - scr_readw are macros which expand in PRETTY long code
24 #include <linux/kernel.h>
25 #include <linux/major.h>
26 #include <linux/errno.h>
27 #include <linux/tty.h>
28 #include <linux/interrupt.h>
30 #include <linux/init.h>
31 #include <linux/vt_kern.h>
32 #include <linux/selection.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/console.h>
35 #include <linux/device.h>
36 #include <asm/uaccess.h>
37 #include <asm/byteorder.h>
38 #include <asm/unaligned.h>
46 vcs_size(struct inode *inode)
49 int minor = iminor(inode);
50 int currcons = minor & 127;
54 currcons = fg_console;
57 if (!vc_cons_allocated(currcons))
59 vc = vc_cons[currcons].d;
61 size = vc->vc_rows * vc->vc_cols;
64 size = 2*size + HEADER_SIZE;
68 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
73 size = vcs_size(file->f_path.dentry->d_inode);
82 offset += file->f_pos;
86 if (offset < 0 || offset > size) {
97 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
99 struct inode *inode = file->f_path.dentry->d_inode;
100 unsigned int currcons = iminor(inode);
103 long viewed, attr, read;
105 unsigned short *org = NULL;
112 /* Select the proper current console and verify
113 * sanity of the situation under the console lock.
115 acquire_console_sem();
117 attr = (currcons & 128);
118 currcons = (currcons & 127);
120 currcons = fg_console;
127 if (!vc_cons_allocated(currcons))
129 vc = vc_cons[currcons].d;
137 char *con_buf0, *con_buf_start;
138 long this_round, size;
142 /* Check whether we are above size each round,
143 * as copy_to_user at the end of this loop
146 size = vcs_size(inode);
149 if (count > size - pos)
153 if (this_round > CON_BUF_SIZE)
154 this_round = CON_BUF_SIZE;
156 /* Perform the whole read into the local con_buf.
157 * Then we can drop the console spinlock and safely
158 * attempt to move it to userspace.
161 con_buf_start = con_buf0 = con_buf;
162 orig_count = this_round;
163 maxcol = vc->vc_cols;
165 org = screen_pos(vc, p, viewed);
168 while (this_round-- > 0) {
169 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
170 if (++col == maxcol) {
171 org = screen_pos(vc, p, viewed);
177 if (p < HEADER_SIZE) {
180 con_buf0[0] = (char)vc->vc_rows;
181 con_buf0[1] = (char)vc->vc_cols;
182 getconsxy(vc, con_buf0 + 2);
186 if (this_round > CON_BUF_SIZE) {
187 this_round = CON_BUF_SIZE;
188 orig_count = this_round - p;
191 tmp_count = HEADER_SIZE;
192 if (tmp_count > this_round)
193 tmp_count = this_round;
195 /* Advance state pointers and move on. */
196 this_round -= tmp_count;
198 con_buf0 = con_buf + HEADER_SIZE;
199 /* If this_round >= 0, then p is even... */
201 /* Skip first byte for output if start address is odd
202 * Update region sizes up/down depending on free
206 if (this_round < CON_BUF_SIZE)
211 if (this_round > 0) {
212 unsigned short *tmp_buf = (unsigned short *)con_buf0;
218 org = screen_pos(vc, p, viewed);
221 /* Buffer has even length, so we can always copy
222 * character + attribute. We do not copy last byte
223 * to userspace if this_round is odd.
225 this_round = (this_round + 1) >> 1;
228 *tmp_buf++ = vcs_scr_readw(vc, org++);
230 if (++col == maxcol) {
231 org = screen_pos(vc, p, viewed);
239 /* Finally, release the console semaphore while we push
240 * all the data to userspace from our temporary buffer.
242 * AKPM: Even though it's a semaphore, we should drop it because
243 * the pagefault handling code may want to call printk().
246 release_console_sem();
247 ret = copy_to_user(buf, con_buf_start, orig_count);
248 acquire_console_sem();
251 read += (orig_count - ret);
264 release_console_sem();
270 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
272 struct inode *inode = file->f_path.dentry->d_inode;
273 unsigned int currcons = iminor(inode);
276 long viewed, attr, size, written;
279 u16 *org0 = NULL, *org = NULL;
286 /* Select the proper current console and verify
287 * sanity of the situation under the console lock.
289 acquire_console_sem();
291 attr = (currcons & 128);
292 currcons = (currcons & 127);
295 currcons = fg_console;
302 if (!vc_cons_allocated(currcons))
304 vc = vc_cons[currcons].d;
306 size = vcs_size(inode);
308 if (pos < 0 || pos > size)
310 if (count > size - pos)
314 long this_round = count;
318 if (this_round > CON_BUF_SIZE)
319 this_round = CON_BUF_SIZE;
321 /* Temporarily drop the console lock so that we can read
322 * in the write data from userspace safely.
324 release_console_sem();
325 ret = copy_from_user(con_buf, buf, this_round);
326 acquire_console_sem();
331 /* Abort loop if no data were copied. Otherwise
341 /* The vcs_size might have changed while we slept to grab
342 * the user buffer, so recheck.
343 * Return data written up to now on failure.
345 size = vcs_size(inode);
348 if (this_round > size - pos)
349 this_round = size - pos;
351 /* OK, now actually push the write to the console
352 * under the lock using the local kernel buffer.
356 orig_count = this_round;
357 maxcol = vc->vc_cols;
360 org0 = org = screen_pos(vc, p, viewed);
364 while (this_round > 0) {
365 unsigned char c = *con_buf0++;
369 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
371 if (++col == maxcol) {
372 org = screen_pos(vc, p, viewed);
378 if (p < HEADER_SIZE) {
379 char header[HEADER_SIZE];
381 getconsxy(vc, header + 2);
382 while (p < HEADER_SIZE && this_round > 0) {
384 header[p++] = *con_buf0++;
387 putconsxy(vc, header + 2);
390 col = (p/2) % maxcol;
391 if (this_round > 0) {
392 org0 = org = screen_pos(vc, p/2, viewed);
393 if ((p & 1) && this_round > 0) {
399 vcs_scr_writew(vc, c |
400 (vcs_scr_readw(vc, org) & 0xff00), org);
402 vcs_scr_writew(vc, (c << 8) |
403 (vcs_scr_readw(vc, org) & 0xff), org);
407 if (++col == maxcol) {
408 org = screen_pos(vc, p/2, viewed);
415 while (this_round > 1) {
418 w = get_unaligned(((unsigned short *)con_buf0));
419 vcs_scr_writew(vc, w, org++);
422 if (++col == maxcol) {
423 org = screen_pos(vc, p, viewed);
428 if (this_round > 0) {
433 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
435 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
440 written += orig_count;
444 update_region(vc, (unsigned long)(org0), org - org0);
450 release_console_sem();
458 vcs_open(struct inode *inode, struct file *filp)
460 unsigned int currcons = iminor(inode) & 127;
461 if(currcons && !vc_cons_allocated(currcons-1))
466 static const struct file_operations vcs_fops = {
473 static struct class *vc_class;
475 void vcs_make_sysfs(struct tty_struct *tty)
477 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
478 "vcs%u", tty->index + 1);
479 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
480 "vcsa%u", tty->index + 1);
483 void vcs_remove_sysfs(struct tty_struct *tty)
485 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
486 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
489 int __init vcs_init(void)
491 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
492 panic("unable to get major %d for vcs device", VCS_MAJOR);
493 vc_class = class_create(THIS_MODULE, "vc");
495 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), "vcs");
496 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), "vcsa");