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;
56 currcons
= fg_console
;
59 if (!vc_cons_allocated(currcons
))
62 size
= video_num_lines
* video_num_columns
;
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_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_dentry
->d_inode
;
101 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
))
136 char *con_buf0
, *con_buf_start
;
137 long this_round
, size
;
141 /* Check whether we are above size each round,
142 * as copy_to_user at the end of this loop
145 size
= vcs_size(inode
);
148 if (count
> size
- pos
)
152 if (this_round
> CON_BUF_SIZE
)
153 this_round
= CON_BUF_SIZE
;
155 /* Perform the whole read into the local con_buf.
156 * Then we can drop the console spinlock and safely
157 * attempt to move it to userspace.
160 con_buf_start
= con_buf0
= con_buf
;
161 orig_count
= this_round
;
162 maxcol
= video_num_columns
;
164 org
= screen_pos(currcons
, p
, viewed
);
167 while (this_round
-- > 0) {
168 *con_buf0
++ = (vcs_scr_readw(currcons
, org
++) & 0xff);
169 if (++col
== maxcol
) {
170 org
= screen_pos(currcons
, p
, viewed
);
176 if (p
< HEADER_SIZE
) {
179 con_buf0
[0] = (char) video_num_lines
;
180 con_buf0
[1] = (char) video_num_columns
;
181 getconsxy(currcons
, con_buf0
+ 2);
185 if (this_round
> CON_BUF_SIZE
) {
186 this_round
= CON_BUF_SIZE
;
187 orig_count
= this_round
- p
;
190 tmp_count
= HEADER_SIZE
;
191 if (tmp_count
> this_round
)
192 tmp_count
= this_round
;
194 /* Advance state pointers and move on. */
195 this_round
-= tmp_count
;
197 con_buf0
= con_buf
+ HEADER_SIZE
;
198 /* If this_round >= 0, then p is even... */
200 /* Skip first byte for output if start address is odd
201 * Update region sizes up/down depending on free
205 if (this_round
< CON_BUF_SIZE
)
210 if (this_round
> 0) {
211 unsigned short *tmp_buf
= (unsigned short *)con_buf0
;
217 org
= screen_pos(currcons
, p
, viewed
);
220 /* Buffer has even length, so we can always copy
221 * character + attribute. We do not copy last byte
222 * to userspace if this_round is odd.
224 this_round
= (this_round
+ 1) >> 1;
227 *tmp_buf
++ = vcs_scr_readw(currcons
, org
++);
229 if (++col
== maxcol
) {
230 org
= screen_pos(currcons
, p
, viewed
);
238 /* Finally, release the console semaphore while we push
239 * all the data to userspace from our temporary buffer.
241 * AKPM: Even though it's a semaphore, we should drop it because
242 * the pagefault handling code may want to call printk().
245 release_console_sem();
246 ret
= copy_to_user(buf
, con_buf_start
, orig_count
);
247 acquire_console_sem();
250 read
+= (orig_count
- ret
);
263 release_console_sem();
269 vcs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
271 struct inode
*inode
= file
->f_dentry
->d_inode
;
272 unsigned int currcons
= iminor(inode
);
274 long viewed
, attr
, size
, written
;
277 u16
*org0
= NULL
, *org
= NULL
;
284 /* Select the proper current console and verify
285 * sanity of the situation under the console lock.
287 acquire_console_sem();
289 attr
= (currcons
& 128);
290 currcons
= (currcons
& 127);
293 currcons
= fg_console
;
300 if (!vc_cons_allocated(currcons
))
303 size
= vcs_size(inode
);
305 if (pos
< 0 || pos
> size
)
307 if (count
> size
- pos
)
311 long this_round
= count
;
315 if (this_round
> CON_BUF_SIZE
)
316 this_round
= CON_BUF_SIZE
;
318 /* Temporarily drop the console lock so that we can read
319 * in the write data from userspace safely.
321 release_console_sem();
322 ret
= copy_from_user(con_buf
, buf
, this_round
);
323 acquire_console_sem();
328 /* Abort loop if no data were copied. Otherwise
338 /* The vcs_size might have changed while we slept to grab
339 * the user buffer, so recheck.
340 * Return data written up to now on failure.
342 size
= vcs_size(inode
);
345 if (this_round
> size
- pos
)
346 this_round
= size
- pos
;
348 /* OK, now actually push the write to the console
349 * under the lock using the local kernel buffer.
353 orig_count
= this_round
;
354 maxcol
= video_num_columns
;
357 org0
= org
= screen_pos(currcons
, p
, viewed
);
361 while (this_round
> 0) {
362 unsigned char c
= *con_buf0
++;
365 vcs_scr_writew(currcons
,
366 (vcs_scr_readw(currcons
, org
) & 0xff00) | c
, org
);
368 if (++col
== maxcol
) {
369 org
= screen_pos(currcons
, p
, viewed
);
375 if (p
< HEADER_SIZE
) {
376 char header
[HEADER_SIZE
];
378 getconsxy(currcons
, header
+ 2);
379 while (p
< HEADER_SIZE
&& this_round
> 0) {
381 header
[p
++] = *con_buf0
++;
384 putconsxy(currcons
, header
+ 2);
387 col
= (p
/2) % maxcol
;
388 if (this_round
> 0) {
389 org0
= org
= screen_pos(currcons
, p
/2, viewed
);
390 if ((p
& 1) && this_round
> 0) {
396 vcs_scr_writew(currcons
, c
|
397 (vcs_scr_readw(currcons
, org
) & 0xff00), org
);
399 vcs_scr_writew(currcons
, (c
<< 8) |
400 (vcs_scr_readw(currcons
, org
) & 0xff), org
);
404 if (++col
== maxcol
) {
405 org
= screen_pos(currcons
, p
/2, viewed
);
412 while (this_round
> 1) {
415 w
= get_unaligned(((const unsigned short *)con_buf0
));
416 vcs_scr_writew(currcons
, w
, org
++);
419 if (++col
== maxcol
) {
420 org
= screen_pos(currcons
, p
, viewed
);
425 if (this_round
> 0) {
430 vcs_scr_writew(currcons
, (vcs_scr_readw(currcons
, org
) & 0xff) | (c
<< 8), org
);
432 vcs_scr_writew(currcons
, (vcs_scr_readw(currcons
, org
) & 0xff00) | c
, org
);
437 written
+= orig_count
;
441 update_region(currcons
, (unsigned long)(org0
), org
-org0
);
447 release_console_sem();
455 vcs_open(struct inode
*inode
, struct file
*filp
)
457 unsigned int currcons
= iminor(inode
) & 127;
458 if(currcons
&& !vc_cons_allocated(currcons
-1))
463 static struct file_operations vcs_fops
= {
470 static struct class_simple
*vc_class
;
472 void vcs_make_devfs(struct tty_struct
*tty
)
474 devfs_mk_cdev(MKDEV(VCS_MAJOR
, tty
->index
+ 1),
475 S_IFCHR
|S_IRUSR
|S_IWUSR
,
476 "vcc/%u", tty
->index
+ 1);
477 devfs_mk_cdev(MKDEV(VCS_MAJOR
, tty
->index
+ 129),
478 S_IFCHR
|S_IRUSR
|S_IWUSR
,
479 "vcc/a%u", tty
->index
+ 1);
480 class_simple_device_add(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 1), NULL
, "vcs%u", tty
->index
+ 1);
481 class_simple_device_add(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 129), NULL
, "vcsa%u", tty
->index
+ 1);
483 void vcs_remove_devfs(struct tty_struct
*tty
)
485 devfs_remove("vcc/%u", tty
->index
+ 1);
486 devfs_remove("vcc/a%u", tty
->index
+ 1);
487 class_simple_device_remove(MKDEV(VCS_MAJOR
, tty
->index
+ 1));
488 class_simple_device_remove(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_simple_create(THIS_MODULE
, "vc");
497 devfs_mk_cdev(MKDEV(VCS_MAJOR
, 0), S_IFCHR
|S_IRUSR
|S_IWUSR
, "vcc/0");
498 devfs_mk_cdev(MKDEV(VCS_MAJOR
, 128), S_IFCHR
|S_IRUSR
|S_IWUSR
, "vcc/a0");
499 class_simple_device_add(vc_class
, MKDEV(VCS_MAJOR
, 0), NULL
, "vcs");
500 class_simple_device_add(vc_class
, MKDEV(VCS_MAJOR
, 128), NULL
, "vcsa");