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/config.h>
25 #include <linux/kernel.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/tty.h>
29 #include <linux/devfs_fs_kernel.h>
30 #include <linux/sched.h>
31 #include <linux/interrupt.h>
33 #include <linux/init.h>
34 #include <linux/vt_kern.h>
35 #include <linux/selection.h>
36 #include <linux/kbd_kern.h>
37 #include <linux/console.h>
38 #include <linux/smp_lock.h>
39 #include <linux/device.h>
40 #include <asm/uaccess.h>
41 #include <asm/byteorder.h>
42 #include <asm/unaligned.h>
50 vcs_size(struct inode *inode)
53 int minor = iminor(inode);
54 int currcons = minor & 127;
58 currcons = fg_console;
61 if (!vc_cons_allocated(currcons))
63 vc = vc_cons[currcons].d;
65 size = vc->vc_rows * vc->vc_cols;
68 size = 2*size + HEADER_SIZE;
72 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
77 size = vcs_size(file->f_dentry->d_inode);
86 offset += file->f_pos;
90 if (offset < 0 || offset > size) {
101 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
103 struct inode *inode = file->f_dentry->d_inode;
104 unsigned int currcons = iminor(inode);
107 long viewed, attr, read;
109 unsigned short *org = NULL;
116 /* Select the proper current console and verify
117 * sanity of the situation under the console lock.
119 acquire_console_sem();
121 attr = (currcons & 128);
122 currcons = (currcons & 127);
124 currcons = fg_console;
131 if (!vc_cons_allocated(currcons))
133 vc = vc_cons[currcons].d;
141 char *con_buf0, *con_buf_start;
142 long this_round, size;
146 /* Check whether we are above size each round,
147 * as copy_to_user at the end of this loop
150 size = vcs_size(inode);
153 if (count > size - pos)
157 if (this_round > CON_BUF_SIZE)
158 this_round = CON_BUF_SIZE;
160 /* Perform the whole read into the local con_buf.
161 * Then we can drop the console spinlock and safely
162 * attempt to move it to userspace.
165 con_buf_start = con_buf0 = con_buf;
166 orig_count = this_round;
167 maxcol = vc->vc_cols;
169 org = screen_pos(vc, p, viewed);
172 while (this_round-- > 0) {
173 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
174 if (++col == maxcol) {
175 org = screen_pos(vc, p, viewed);
181 if (p < HEADER_SIZE) {
184 con_buf0[0] = (char)vc->vc_rows;
185 con_buf0[1] = (char)vc->vc_cols;
186 getconsxy(vc, con_buf0 + 2);
190 if (this_round > CON_BUF_SIZE) {
191 this_round = CON_BUF_SIZE;
192 orig_count = this_round - p;
195 tmp_count = HEADER_SIZE;
196 if (tmp_count > this_round)
197 tmp_count = this_round;
199 /* Advance state pointers and move on. */
200 this_round -= tmp_count;
202 con_buf0 = con_buf + HEADER_SIZE;
203 /* If this_round >= 0, then p is even... */
205 /* Skip first byte for output if start address is odd
206 * Update region sizes up/down depending on free
210 if (this_round < CON_BUF_SIZE)
215 if (this_round > 0) {
216 unsigned short *tmp_buf = (unsigned short *)con_buf0;
222 org = screen_pos(vc, p, viewed);
225 /* Buffer has even length, so we can always copy
226 * character + attribute. We do not copy last byte
227 * to userspace if this_round is odd.
229 this_round = (this_round + 1) >> 1;
232 *tmp_buf++ = vcs_scr_readw(vc, org++);
234 if (++col == maxcol) {
235 org = screen_pos(vc, p, viewed);
243 /* Finally, release the console semaphore while we push
244 * all the data to userspace from our temporary buffer.
246 * AKPM: Even though it's a semaphore, we should drop it because
247 * the pagefault handling code may want to call printk().
250 release_console_sem();
251 ret = copy_to_user(buf, con_buf_start, orig_count);
252 acquire_console_sem();
255 read += (orig_count - ret);
268 release_console_sem();
274 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
276 struct inode *inode = file->f_dentry->d_inode;
277 unsigned int currcons = iminor(inode);
280 long viewed, attr, size, written;
283 u16 *org0 = NULL, *org = NULL;
290 /* Select the proper current console and verify
291 * sanity of the situation under the console lock.
293 acquire_console_sem();
295 attr = (currcons & 128);
296 currcons = (currcons & 127);
299 currcons = fg_console;
306 if (!vc_cons_allocated(currcons))
308 vc = vc_cons[currcons].d;
310 size = vcs_size(inode);
312 if (pos < 0 || pos > size)
314 if (count > size - pos)
318 long this_round = count;
322 if (this_round > CON_BUF_SIZE)
323 this_round = CON_BUF_SIZE;
325 /* Temporarily drop the console lock so that we can read
326 * in the write data from userspace safely.
328 release_console_sem();
329 ret = copy_from_user(con_buf, buf, this_round);
330 acquire_console_sem();
335 /* Abort loop if no data were copied. Otherwise
345 /* The vcs_size might have changed while we slept to grab
346 * the user buffer, so recheck.
347 * Return data written up to now on failure.
349 size = vcs_size(inode);
352 if (this_round > size - pos)
353 this_round = size - pos;
355 /* OK, now actually push the write to the console
356 * under the lock using the local kernel buffer.
360 orig_count = this_round;
361 maxcol = vc->vc_cols;
364 org0 = org = screen_pos(vc, p, viewed);
368 while (this_round > 0) {
369 unsigned char c = *con_buf0++;
373 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
375 if (++col == maxcol) {
376 org = screen_pos(vc, p, viewed);
382 if (p < HEADER_SIZE) {
383 char header[HEADER_SIZE];
385 getconsxy(vc, header + 2);
386 while (p < HEADER_SIZE && this_round > 0) {
388 header[p++] = *con_buf0++;
391 putconsxy(vc, header + 2);
394 col = (p/2) % maxcol;
395 if (this_round > 0) {
396 org0 = org = screen_pos(vc, p/2, viewed);
397 if ((p & 1) && this_round > 0) {
403 vcs_scr_writew(vc, c |
404 (vcs_scr_readw(vc, org) & 0xff00), org);
406 vcs_scr_writew(vc, (c << 8) |
407 (vcs_scr_readw(vc, org) & 0xff), org);
411 if (++col == maxcol) {
412 org = screen_pos(vc, p/2, viewed);
419 while (this_round > 1) {
422 w = get_unaligned(((const unsigned short *)con_buf0));
423 vcs_scr_writew(vc, w, org++);
426 if (++col == maxcol) {
427 org = screen_pos(vc, p, viewed);
432 if (this_round > 0) {
437 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
439 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
444 written += orig_count;
448 update_region(vc, (unsigned long)(org0), org - org0);
454 release_console_sem();
462 vcs_open(struct inode *inode, struct file *filp)
464 unsigned int currcons = iminor(inode) & 127;
465 if(currcons && !vc_cons_allocated(currcons-1))
470 static struct file_operations vcs_fops = {
477 static struct class_simple *vc_class;
479 void vcs_make_devfs(struct tty_struct *tty)
481 devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 1),
482 S_IFCHR|S_IRUSR|S_IWUSR,
483 "vcc/%u", tty->index + 1);
484 devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 129),
485 S_IFCHR|S_IRUSR|S_IWUSR,
486 "vcc/a%u", tty->index + 1);
487 class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, tty->index + 1), NULL, "vcs%u", tty->index + 1);
488 class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, tty->index + 129), NULL, "vcsa%u", tty->index + 1);
490 void vcs_remove_devfs(struct tty_struct *tty)
492 devfs_remove("vcc/%u", tty->index + 1);
493 devfs_remove("vcc/a%u", tty->index + 1);
494 class_simple_device_remove(MKDEV(VCS_MAJOR, tty->index + 1));
495 class_simple_device_remove(MKDEV(VCS_MAJOR, tty->index + 129));
498 int __init vcs_init(void)
500 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
501 panic("unable to get major %d for vcs device", VCS_MAJOR);
502 vc_class = class_simple_create(THIS_MODULE, "vc");
504 devfs_mk_cdev(MKDEV(VCS_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/0");
505 devfs_mk_cdev(MKDEV(VCS_MAJOR, 128), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/a0");
506 class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
507 class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");