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/sched.h>
29 #include <linux/interrupt.h>
31 #include <linux/init.h>
32 #include <linux/vt_kern.h>
33 #include <linux/selection.h>
34 #include <linux/kbd_kern.h>
35 #include <linux/console.h>
36 #include <linux/smp_lock.h>
37 #include <linux/device.h>
38 #include <asm/uaccess.h>
39 #include <asm/byteorder.h>
40 #include <asm/unaligned.h>
48 vcs_size(struct inode
*inode
)
51 int minor
= iminor(inode
);
52 int currcons
= minor
& 127;
56 currcons
= fg_console
;
59 if (!vc_cons_allocated(currcons
))
61 vc
= vc_cons
[currcons
].d
;
63 size
= vc
->vc_rows
* vc
->vc_cols
;
66 size
= 2*size
+ HEADER_SIZE
;
70 static loff_t
vcs_lseek(struct file
*file
, loff_t offset
, int orig
)
75 size
= vcs_size(file
->f_path
.dentry
->d_inode
);
84 offset
+= file
->f_pos
;
88 if (offset
< 0 || offset
> size
) {
99 vcs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
101 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
102 unsigned int currcons
= iminor(inode
);
105 long viewed
, attr
, read
;
107 unsigned short *org
= NULL
;
114 /* Select the proper current console and verify
115 * sanity of the situation under the console lock.
117 acquire_console_sem();
119 attr
= (currcons
& 128);
120 currcons
= (currcons
& 127);
122 currcons
= fg_console
;
129 if (!vc_cons_allocated(currcons
))
131 vc
= vc_cons
[currcons
].d
;
139 char *con_buf0
, *con_buf_start
;
140 long this_round
, size
;
144 /* Check whether we are above size each round,
145 * as copy_to_user at the end of this loop
148 size
= vcs_size(inode
);
151 if (count
> size
- pos
)
155 if (this_round
> CON_BUF_SIZE
)
156 this_round
= CON_BUF_SIZE
;
158 /* Perform the whole read into the local con_buf.
159 * Then we can drop the console spinlock and safely
160 * attempt to move it to userspace.
163 con_buf_start
= con_buf0
= con_buf
;
164 orig_count
= this_round
;
165 maxcol
= vc
->vc_cols
;
167 org
= screen_pos(vc
, p
, viewed
);
170 while (this_round
-- > 0) {
171 *con_buf0
++ = (vcs_scr_readw(vc
, org
++) & 0xff);
172 if (++col
== maxcol
) {
173 org
= screen_pos(vc
, p
, viewed
);
179 if (p
< HEADER_SIZE
) {
182 con_buf0
[0] = (char)vc
->vc_rows
;
183 con_buf0
[1] = (char)vc
->vc_cols
;
184 getconsxy(vc
, con_buf0
+ 2);
188 if (this_round
> CON_BUF_SIZE
) {
189 this_round
= CON_BUF_SIZE
;
190 orig_count
= this_round
- p
;
193 tmp_count
= HEADER_SIZE
;
194 if (tmp_count
> this_round
)
195 tmp_count
= this_round
;
197 /* Advance state pointers and move on. */
198 this_round
-= tmp_count
;
200 con_buf0
= con_buf
+ HEADER_SIZE
;
201 /* If this_round >= 0, then p is even... */
203 /* Skip first byte for output if start address is odd
204 * Update region sizes up/down depending on free
208 if (this_round
< CON_BUF_SIZE
)
213 if (this_round
> 0) {
214 unsigned short *tmp_buf
= (unsigned short *)con_buf0
;
220 org
= screen_pos(vc
, p
, viewed
);
223 /* Buffer has even length, so we can always copy
224 * character + attribute. We do not copy last byte
225 * to userspace if this_round is odd.
227 this_round
= (this_round
+ 1) >> 1;
230 *tmp_buf
++ = vcs_scr_readw(vc
, org
++);
232 if (++col
== maxcol
) {
233 org
= screen_pos(vc
, p
, viewed
);
241 /* Finally, release the console semaphore while we push
242 * all the data to userspace from our temporary buffer.
244 * AKPM: Even though it's a semaphore, we should drop it because
245 * the pagefault handling code may want to call printk().
248 release_console_sem();
249 ret
= copy_to_user(buf
, con_buf_start
, orig_count
);
250 acquire_console_sem();
253 read
+= (orig_count
- ret
);
266 release_console_sem();
272 vcs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
274 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
275 unsigned int currcons
= iminor(inode
);
278 long viewed
, attr
, size
, written
;
281 u16
*org0
= NULL
, *org
= NULL
;
288 /* Select the proper current console and verify
289 * sanity of the situation under the console lock.
291 acquire_console_sem();
293 attr
= (currcons
& 128);
294 currcons
= (currcons
& 127);
297 currcons
= fg_console
;
304 if (!vc_cons_allocated(currcons
))
306 vc
= vc_cons
[currcons
].d
;
308 size
= vcs_size(inode
);
310 if (pos
< 0 || pos
> size
)
312 if (count
> size
- pos
)
316 long this_round
= count
;
320 if (this_round
> CON_BUF_SIZE
)
321 this_round
= CON_BUF_SIZE
;
323 /* Temporarily drop the console lock so that we can read
324 * in the write data from userspace safely.
326 release_console_sem();
327 ret
= copy_from_user(con_buf
, buf
, this_round
);
328 acquire_console_sem();
333 /* Abort loop if no data were copied. Otherwise
343 /* The vcs_size might have changed while we slept to grab
344 * the user buffer, so recheck.
345 * Return data written up to now on failure.
347 size
= vcs_size(inode
);
350 if (this_round
> size
- pos
)
351 this_round
= size
- pos
;
353 /* OK, now actually push the write to the console
354 * under the lock using the local kernel buffer.
358 orig_count
= this_round
;
359 maxcol
= vc
->vc_cols
;
362 org0
= org
= screen_pos(vc
, p
, viewed
);
366 while (this_round
> 0) {
367 unsigned char c
= *con_buf0
++;
371 (vcs_scr_readw(vc
, org
) & 0xff00) | c
, org
);
373 if (++col
== maxcol
) {
374 org
= screen_pos(vc
, p
, viewed
);
380 if (p
< HEADER_SIZE
) {
381 char header
[HEADER_SIZE
];
383 getconsxy(vc
, header
+ 2);
384 while (p
< HEADER_SIZE
&& this_round
> 0) {
386 header
[p
++] = *con_buf0
++;
389 putconsxy(vc
, header
+ 2);
392 col
= (p
/2) % maxcol
;
393 if (this_round
> 0) {
394 org0
= org
= screen_pos(vc
, p
/2, viewed
);
395 if ((p
& 1) && this_round
> 0) {
401 vcs_scr_writew(vc
, c
|
402 (vcs_scr_readw(vc
, org
) & 0xff00), org
);
404 vcs_scr_writew(vc
, (c
<< 8) |
405 (vcs_scr_readw(vc
, org
) & 0xff), org
);
409 if (++col
== maxcol
) {
410 org
= screen_pos(vc
, p
/2, viewed
);
417 while (this_round
> 1) {
420 w
= get_unaligned(((unsigned short *)con_buf0
));
421 vcs_scr_writew(vc
, w
, org
++);
424 if (++col
== maxcol
) {
425 org
= screen_pos(vc
, p
, viewed
);
430 if (this_round
> 0) {
435 vcs_scr_writew(vc
, (vcs_scr_readw(vc
, org
) & 0xff) | (c
<< 8), org
);
437 vcs_scr_writew(vc
, (vcs_scr_readw(vc
, org
) & 0xff00) | c
, org
);
442 written
+= orig_count
;
446 update_region(vc
, (unsigned long)(org0
), org
- org0
);
452 release_console_sem();
460 vcs_open(struct inode
*inode
, struct file
*filp
)
462 unsigned int currcons
= iminor(inode
) & 127;
463 if(currcons
&& !vc_cons_allocated(currcons
-1))
468 static const struct file_operations vcs_fops
= {
475 static struct class *vc_class
;
477 void vcs_make_sysfs(struct tty_struct
*tty
)
479 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, tty
->index
+ 1),
480 "vcs%u", tty
->index
+ 1);
481 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, tty
->index
+ 129),
482 "vcsa%u", tty
->index
+ 1);
485 void vcs_remove_sysfs(struct tty_struct
*tty
)
487 device_destroy(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 1));
488 device_destroy(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 129));
491 int __init
vcs_init(void)
493 if (register_chrdev(VCS_MAJOR
, "vcs", &vcs_fops
))
494 panic("unable to get major %d for vcs device", VCS_MAJOR
);
495 vc_class
= class_create(THIS_MODULE
, "vc");
497 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, 0), "vcs");
498 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, 128), "vcsa");