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/mutex.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/device.h>
37 #include <linux/smp_lock.h>
39 #include <asm/uaccess.h>
40 #include <asm/byteorder.h>
41 #include <asm/unaligned.h>
49 vcs_size(struct inode
*inode
)
52 int minor
= iminor(inode
);
53 int currcons
= minor
& 127;
57 currcons
= fg_console
;
60 if (!vc_cons_allocated(currcons
))
62 vc
= vc_cons
[currcons
].d
;
64 size
= vc
->vc_rows
* vc
->vc_cols
;
67 size
= 2*size
+ HEADER_SIZE
;
71 static loff_t
vcs_lseek(struct file
*file
, loff_t offset
, int orig
)
75 mutex_lock(&con_buf_mtx
);
76 size
= vcs_size(file
->f_path
.dentry
->d_inode
);
79 mutex_unlock(&con_buf_mtx
);
85 offset
+= file
->f_pos
;
89 if (offset
< 0 || offset
> size
) {
90 mutex_unlock(&con_buf_mtx
);
94 mutex_unlock(&con_buf_mtx
);
100 vcs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
102 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
103 unsigned int currcons
= iminor(inode
);
106 long viewed
, attr
, read
;
108 unsigned short *org
= NULL
;
111 mutex_lock(&con_buf_mtx
);
115 /* Select the proper current console and verify
116 * sanity of the situation under the console lock.
118 acquire_console_sem();
120 attr
= (currcons
& 128);
121 currcons
= (currcons
& 127);
123 currcons
= fg_console
;
130 if (!vc_cons_allocated(currcons
))
132 vc
= vc_cons
[currcons
].d
;
140 char *con_buf0
, *con_buf_start
;
141 long this_round
, size
;
145 /* Check whether we are above size each round,
146 * as copy_to_user at the end of this loop
149 size
= vcs_size(inode
);
152 if (count
> size
- pos
)
156 if (this_round
> CON_BUF_SIZE
)
157 this_round
= CON_BUF_SIZE
;
159 /* Perform the whole read into the local con_buf.
160 * Then we can drop the console spinlock and safely
161 * attempt to move it to userspace.
164 con_buf_start
= con_buf0
= con_buf
;
165 orig_count
= this_round
;
166 maxcol
= vc
->vc_cols
;
168 org
= screen_pos(vc
, p
, viewed
);
171 while (this_round
-- > 0) {
172 *con_buf0
++ = (vcs_scr_readw(vc
, org
++) & 0xff);
173 if (++col
== maxcol
) {
174 org
= screen_pos(vc
, p
, viewed
);
180 if (p
< HEADER_SIZE
) {
183 con_buf0
[0] = (char)vc
->vc_rows
;
184 con_buf0
[1] = (char)vc
->vc_cols
;
185 getconsxy(vc
, con_buf0
+ 2);
189 if (this_round
> CON_BUF_SIZE
) {
190 this_round
= CON_BUF_SIZE
;
191 orig_count
= this_round
- p
;
194 tmp_count
= HEADER_SIZE
;
195 if (tmp_count
> this_round
)
196 tmp_count
= this_round
;
198 /* Advance state pointers and move on. */
199 this_round
-= tmp_count
;
201 con_buf0
= con_buf
+ HEADER_SIZE
;
202 /* If this_round >= 0, then p is even... */
204 /* Skip first byte for output if start address is odd
205 * Update region sizes up/down depending on free
209 if (this_round
< CON_BUF_SIZE
)
214 if (this_round
> 0) {
215 unsigned short *tmp_buf
= (unsigned short *)con_buf0
;
221 org
= screen_pos(vc
, p
, viewed
);
224 /* Buffer has even length, so we can always copy
225 * character + attribute. We do not copy last byte
226 * to userspace if this_round is odd.
228 this_round
= (this_round
+ 1) >> 1;
231 *tmp_buf
++ = vcs_scr_readw(vc
, org
++);
233 if (++col
== maxcol
) {
234 org
= screen_pos(vc
, p
, viewed
);
242 /* Finally, release the console semaphore while we push
243 * all the data to userspace from our temporary buffer.
245 * AKPM: Even though it's a semaphore, we should drop it because
246 * the pagefault handling code may want to call printk().
249 release_console_sem();
250 ret
= copy_to_user(buf
, con_buf_start
, orig_count
);
251 acquire_console_sem();
254 read
+= (orig_count
- ret
);
267 release_console_sem();
268 mutex_unlock(&con_buf_mtx
);
273 vcs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
275 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
276 unsigned int currcons
= iminor(inode
);
279 long viewed
, attr
, size
, written
;
282 u16
*org0
= NULL
, *org
= NULL
;
285 mutex_lock(&con_buf_mtx
);
289 /* Select the proper current console and verify
290 * sanity of the situation under the console lock.
292 acquire_console_sem();
294 attr
= (currcons
& 128);
295 currcons
= (currcons
& 127);
298 currcons
= fg_console
;
305 if (!vc_cons_allocated(currcons
))
307 vc
= vc_cons
[currcons
].d
;
309 size
= vcs_size(inode
);
311 if (pos
< 0 || pos
> size
)
313 if (count
> size
- pos
)
317 long this_round
= count
;
321 if (this_round
> CON_BUF_SIZE
)
322 this_round
= CON_BUF_SIZE
;
324 /* Temporarily drop the console lock so that we can read
325 * in the write data from userspace safely.
327 release_console_sem();
328 ret
= copy_from_user(con_buf
, buf
, this_round
);
329 acquire_console_sem();
334 /* Abort loop if no data were copied. Otherwise
344 /* The vcs_size might have changed while we slept to grab
345 * the user buffer, so recheck.
346 * Return data written up to now on failure.
348 size
= vcs_size(inode
);
351 if (this_round
> size
- pos
)
352 this_round
= size
- pos
;
354 /* OK, now actually push the write to the console
355 * under the lock using the local kernel buffer.
359 orig_count
= this_round
;
360 maxcol
= vc
->vc_cols
;
363 org0
= org
= screen_pos(vc
, p
, viewed
);
367 while (this_round
> 0) {
368 unsigned char c
= *con_buf0
++;
372 (vcs_scr_readw(vc
, org
) & 0xff00) | c
, org
);
374 if (++col
== maxcol
) {
375 org
= screen_pos(vc
, p
, viewed
);
381 if (p
< HEADER_SIZE
) {
382 char header
[HEADER_SIZE
];
384 getconsxy(vc
, header
+ 2);
385 while (p
< HEADER_SIZE
&& this_round
> 0) {
387 header
[p
++] = *con_buf0
++;
390 putconsxy(vc
, header
+ 2);
393 col
= (p
/2) % maxcol
;
394 if (this_round
> 0) {
395 org0
= org
= screen_pos(vc
, p
/2, viewed
);
396 if ((p
& 1) && this_round
> 0) {
402 vcs_scr_writew(vc
, c
|
403 (vcs_scr_readw(vc
, org
) & 0xff00), org
);
405 vcs_scr_writew(vc
, (c
<< 8) |
406 (vcs_scr_readw(vc
, org
) & 0xff), org
);
410 if (++col
== maxcol
) {
411 org
= screen_pos(vc
, p
/2, viewed
);
418 while (this_round
> 1) {
421 w
= get_unaligned(((unsigned short *)con_buf0
));
422 vcs_scr_writew(vc
, w
, org
++);
425 if (++col
== maxcol
) {
426 org
= screen_pos(vc
, p
, viewed
);
431 if (this_round
> 0) {
436 vcs_scr_writew(vc
, (vcs_scr_readw(vc
, org
) & 0xff) | (c
<< 8), org
);
438 vcs_scr_writew(vc
, (vcs_scr_readw(vc
, org
) & 0xff00) | c
, org
);
443 written
+= orig_count
;
447 update_region(vc
, (unsigned long)(org0
), org
- org0
);
453 release_console_sem();
455 mutex_unlock(&con_buf_mtx
);
461 vcs_open(struct inode
*inode
, struct file
*filp
)
463 unsigned int currcons
= iminor(inode
) & 127;
467 if(currcons
&& !vc_cons_allocated(currcons
-1))
473 static const struct file_operations vcs_fops
= {
480 static struct class *vc_class
;
482 void vcs_make_sysfs(struct tty_struct
*tty
)
484 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, tty
->index
+ 1), NULL
,
485 "vcs%u", tty
->index
+ 1);
486 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, tty
->index
+ 129), NULL
,
487 "vcsa%u", tty
->index
+ 1);
490 void vcs_remove_sysfs(struct tty_struct
*tty
)
492 device_destroy(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 1));
493 device_destroy(vc_class
, MKDEV(VCS_MAJOR
, tty
->index
+ 129));
496 int __init
vcs_init(void)
498 if (register_chrdev(VCS_MAJOR
, "vcs", &vcs_fops
))
499 panic("unable to get major %d for vcs device", VCS_MAJOR
);
500 vc_class
= class_create(THIS_MODULE
, "vc");
502 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, 0), NULL
, "vcs");
503 device_create(vc_class
, NULL
, MKDEV(VCS_MAJOR
, 128), NULL
, "vcsa");