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/smp_lock.h>
36 #include <linux/device.h>
37 #include <asm/uaccess.h>
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
47 vcs_size(struct inode *inode)
50 int minor = iminor(inode);
51 int currcons = minor & 127;
55 currcons = fg_console;
58 if (!vc_cons_allocated(currcons))
60 vc = vc_cons[currcons].d;
62 size = vc->vc_rows * vc->vc_cols;
65 size = 2*size + HEADER_SIZE;
69 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
74 size = vcs_size(file->f_path.dentry->d_inode);
83 offset += file->f_pos;
87 if (offset < 0 || offset > size) {
98 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
100 struct inode *inode = file->f_path.dentry->d_inode;
101 unsigned int currcons = iminor(inode);
104 long viewed, attr, read;
106 unsigned short *org = NULL;
113 /* Select the proper current console and verify
114 * sanity of the situation under the console lock.
116 acquire_console_sem();
118 attr = (currcons & 128);
119 currcons = (currcons & 127);
121 currcons = fg_console;
128 if (!vc_cons_allocated(currcons))
130 vc = vc_cons[currcons].d;
138 char *con_buf0, *con_buf_start;
139 long this_round, size;
143 /* Check whether we are above size each round,
144 * as copy_to_user at the end of this loop
147 size = vcs_size(inode);
150 if (count > size - pos)
154 if (this_round > CON_BUF_SIZE)
155 this_round = CON_BUF_SIZE;
157 /* Perform the whole read into the local con_buf.
158 * Then we can drop the console spinlock and safely
159 * attempt to move it to userspace.
162 con_buf_start = con_buf0 = con_buf;
163 orig_count = this_round;
164 maxcol = vc->vc_cols;
166 org = screen_pos(vc, p, viewed);
169 while (this_round-- > 0) {
170 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
171 if (++col == maxcol) {
172 org = screen_pos(vc, p, viewed);
178 if (p < HEADER_SIZE) {
181 con_buf0[0] = (char)vc->vc_rows;
182 con_buf0[1] = (char)vc->vc_cols;
183 getconsxy(vc, con_buf0 + 2);
187 if (this_round > CON_BUF_SIZE) {
188 this_round = CON_BUF_SIZE;
189 orig_count = this_round - p;
192 tmp_count = HEADER_SIZE;
193 if (tmp_count > this_round)
194 tmp_count = this_round;
196 /* Advance state pointers and move on. */
197 this_round -= tmp_count;
199 con_buf0 = con_buf + HEADER_SIZE;
200 /* If this_round >= 0, then p is even... */
202 /* Skip first byte for output if start address is odd
203 * Update region sizes up/down depending on free
207 if (this_round < CON_BUF_SIZE)
212 if (this_round > 0) {
213 unsigned short *tmp_buf = (unsigned short *)con_buf0;
219 org = screen_pos(vc, p, viewed);
222 /* Buffer has even length, so we can always copy
223 * character + attribute. We do not copy last byte
224 * to userspace if this_round is odd.
226 this_round = (this_round + 1) >> 1;
229 *tmp_buf++ = vcs_scr_readw(vc, org++);
231 if (++col == maxcol) {
232 org = screen_pos(vc, p, viewed);
240 /* Finally, release the console semaphore while we push
241 * all the data to userspace from our temporary buffer.
243 * AKPM: Even though it's a semaphore, we should drop it because
244 * the pagefault handling code may want to call printk().
247 release_console_sem();
248 ret = copy_to_user(buf, con_buf_start, orig_count);
249 acquire_console_sem();
252 read += (orig_count - ret);
265 release_console_sem();
271 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
273 struct inode *inode = file->f_path.dentry->d_inode;
274 unsigned int currcons = iminor(inode);
277 long viewed, attr, size, written;
280 u16 *org0 = NULL, *org = NULL;
287 /* Select the proper current console and verify
288 * sanity of the situation under the console lock.
290 acquire_console_sem();
292 attr = (currcons & 128);
293 currcons = (currcons & 127);
296 currcons = fg_console;
303 if (!vc_cons_allocated(currcons))
305 vc = vc_cons[currcons].d;
307 size = vcs_size(inode);
309 if (pos < 0 || pos > size)
311 if (count > size - pos)
315 long this_round = count;
319 if (this_round > CON_BUF_SIZE)
320 this_round = CON_BUF_SIZE;
322 /* Temporarily drop the console lock so that we can read
323 * in the write data from userspace safely.
325 release_console_sem();
326 ret = copy_from_user(con_buf, buf, this_round);
327 acquire_console_sem();
332 /* Abort loop if no data were copied. Otherwise
342 /* The vcs_size might have changed while we slept to grab
343 * the user buffer, so recheck.
344 * Return data written up to now on failure.
346 size = vcs_size(inode);
349 if (this_round > size - pos)
350 this_round = size - pos;
352 /* OK, now actually push the write to the console
353 * under the lock using the local kernel buffer.
357 orig_count = this_round;
358 maxcol = vc->vc_cols;
361 org0 = org = screen_pos(vc, p, viewed);
365 while (this_round > 0) {
366 unsigned char c = *con_buf0++;
370 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
372 if (++col == maxcol) {
373 org = screen_pos(vc, p, viewed);
379 if (p < HEADER_SIZE) {
380 char header[HEADER_SIZE];
382 getconsxy(vc, header + 2);
383 while (p < HEADER_SIZE && this_round > 0) {
385 header[p++] = *con_buf0++;
388 putconsxy(vc, header + 2);
391 col = (p/2) % maxcol;
392 if (this_round > 0) {
393 org0 = org = screen_pos(vc, p/2, viewed);
394 if ((p & 1) && this_round > 0) {
400 vcs_scr_writew(vc, c |
401 (vcs_scr_readw(vc, org) & 0xff00), org);
403 vcs_scr_writew(vc, (c << 8) |
404 (vcs_scr_readw(vc, org) & 0xff), org);
408 if (++col == maxcol) {
409 org = screen_pos(vc, p/2, viewed);
416 while (this_round > 1) {
419 w = get_unaligned(((unsigned short *)con_buf0));
420 vcs_scr_writew(vc, w, org++);
423 if (++col == maxcol) {
424 org = screen_pos(vc, p, viewed);
429 if (this_round > 0) {
434 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
436 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
441 written += orig_count;
445 update_region(vc, (unsigned long)(org0), org - org0);
451 release_console_sem();
459 vcs_open(struct inode *inode, struct file *filp)
461 unsigned int currcons = iminor(inode) & 127;
462 if(currcons && !vc_cons_allocated(currcons-1))
467 static const struct file_operations vcs_fops = {
474 static struct class *vc_class;
476 void vcs_make_sysfs(struct tty_struct *tty)
478 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
479 "vcs%u", tty->index + 1);
480 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
481 "vcsa%u", tty->index + 1);
484 void vcs_remove_sysfs(struct tty_struct *tty)
486 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
487 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
490 int __init vcs_init(void)
492 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
493 panic("unable to get major %d for vcs device", VCS_MAJOR);
494 vc_class = class_create(THIS_MODULE, "vc");
496 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), "vcs");
497 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), "vcsa");