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/console_struct.h>
36 #include <linux/selection.h>
37 #include <linux/kbd_kern.h>
38 #include <linux/console.h>
39 #include <asm/uaccess.h>
40 #include <asm/byteorder.h>
48 vcs_size(struct inode
*inode
)
51 int currcons
= MINOR(inode
->i_rdev
) & 127;
53 currcons
= fg_console
;
56 if (!vc_cons_allocated(currcons
))
59 size
= video_num_lines
* video_num_columns
;
61 if (MINOR(inode
->i_rdev
) & 128)
62 size
= 2*size
+ HEADER_SIZE
;
66 static loff_t
vcs_lseek(struct file
*file
, loff_t offset
, int orig
)
68 int size
= vcs_size(file
->f_dentry
->d_inode
);
77 offset
+= file
->f_pos
;
81 if (offset
< 0 || offset
> size
)
87 /* We share this temporary buffer with the console write code
88 * so that we can easily avoid touching user space while holding the
91 extern char con_buf
[PAGE_SIZE
];
92 #define CON_BUF_SIZE PAGE_SIZE
93 extern struct semaphore con_buf_sem
;
96 vcs_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
98 struct inode
*inode
= file
->f_dentry
->d_inode
;
99 unsigned int currcons
= MINOR(inode
->i_rdev
);
101 long viewed
, attr
, read
;
103 unsigned short *org
= NULL
;
108 /* Select the proper current console and verify
109 * sanity of the situation under the console lock.
111 spin_lock_irq(&console_lock
);
113 attr
= (currcons
& 128);
114 currcons
= (currcons
& 127);
116 currcons
= fg_console
;
123 if (!vc_cons_allocated(currcons
))
132 char *con_buf0
, *con_buf_start
;
133 long this_round
, size
;
137 /* Check whether we are above size each round,
138 * as copy_to_user at the end of this loop
141 size
= vcs_size(inode
);
144 if (count
> size
- pos
)
148 if (this_round
> CON_BUF_SIZE
)
149 this_round
= CON_BUF_SIZE
;
151 /* Perform the whole read into the local con_buf.
152 * Then we can drop the console spinlock and safely
153 * attempt to move it to userspace.
156 con_buf_start
= con_buf0
= con_buf
;
157 orig_count
= this_round
;
158 maxcol
= video_num_columns
;
160 org
= screen_pos(currcons
, p
, viewed
);
163 while (this_round
-- > 0) {
164 *con_buf0
++ = (vcs_scr_readw(currcons
, org
++) & 0xff);
165 if (++col
== maxcol
) {
166 org
= screen_pos(currcons
, p
, viewed
);
172 if (p
< HEADER_SIZE
) {
175 con_buf0
[0] = (char) video_num_lines
;
176 con_buf0
[1] = (char) video_num_columns
;
177 getconsxy(currcons
, con_buf0
+ 2);
179 tmp_count
= HEADER_SIZE
- p
;
180 if (tmp_count
> this_round
)
181 tmp_count
= this_round
;
183 /* Advance state pointers and move on. */
184 this_round
-= tmp_count
;
188 con_buf0
= con_buf
+ p
;
191 col
= (p
/2) % maxcol
;
192 if (this_round
> 0) {
195 org
= screen_pos(currcons
, p
/2, viewed
);
196 if ((p
& 1) && this_round
> 0) {
198 tmp_byte
= vcs_scr_readw(currcons
, org
++) & 0xff;
200 tmp_byte
= vcs_scr_readw(currcons
, org
++) >> 8;
203 *con_buf0
++ = tmp_byte
;
207 if (++col
== maxcol
) {
208 org
= screen_pos(currcons
, p
/2, viewed
);
216 if (this_round
> 1) {
217 size_t tmp_count
= this_round
;
218 unsigned short *tmp_buf
= (unsigned short *)con_buf0
;
220 while (tmp_count
> 1) {
221 *tmp_buf
++ = vcs_scr_readw(currcons
, org
++);
223 if (++col
== maxcol
) {
224 org
= screen_pos(currcons
, p
, viewed
);
230 /* Advance pointers, and move on. */
231 this_round
= tmp_count
;
232 con_buf0
= (char*)tmp_buf
;
234 if (this_round
> 0) {
238 tmp_byte
= vcs_scr_readw(currcons
, org
) >> 8;
240 tmp_byte
= vcs_scr_readw(currcons
, org
) & 0xff;
243 *con_buf0
++ = tmp_byte
;
247 /* Finally, temporarily drop the console lock and push
248 * all the data to userspace from our temporary buffer.
251 spin_unlock_irq(&console_lock
);
252 ret
= copy_to_user(buf
, con_buf_start
, orig_count
);
253 spin_lock_irq(&console_lock
);
256 read
+= (orig_count
- ret
);
269 spin_unlock_irq(&console_lock
);
275 vcs_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
277 struct inode
*inode
= file
->f_dentry
->d_inode
;
278 unsigned int currcons
= MINOR(inode
->i_rdev
);
280 long viewed
, attr
, size
, written
;
283 u16
*org0
= NULL
, *org
= NULL
;
288 /* Select the proper current console and verify
289 * sanity of the situation under the console lock.
291 spin_lock_irq(&console_lock
);
293 attr
= (currcons
& 128);
294 currcons
= (currcons
& 127);
297 currcons
= fg_console
;
304 if (!vc_cons_allocated(currcons
))
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 spin_unlock_irq(&console_lock
);
326 ret
= copy_from_user(con_buf
, buf
, this_round
);
327 spin_lock_irq(&console_lock
);
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
= video_num_columns
;
361 org0
= org
= screen_pos(currcons
, p
, viewed
);
365 while (this_round
> 0) {
366 unsigned char c
= *con_buf0
++;
369 vcs_scr_writew(currcons
,
370 (vcs_scr_readw(currcons
, org
) & 0xff00) | c
, org
);
372 if (++col
== maxcol
) {
373 org
= screen_pos(currcons
, p
, viewed
);
379 if (p
< HEADER_SIZE
) {
380 char header
[HEADER_SIZE
];
382 getconsxy(currcons
, header
+ 2);
383 while (p
< HEADER_SIZE
&& this_round
> 0) {
385 header
[p
++] = *con_buf0
++;
388 putconsxy(currcons
, header
+ 2);
391 col
= (p
/2) % maxcol
;
392 if (this_round
> 0) {
393 org0
= org
= screen_pos(currcons
, p
/2, viewed
);
394 if ((p
& 1) && this_round
> 0) {
400 vcs_scr_writew(currcons
, c
|
401 (vcs_scr_readw(currcons
, org
) & 0xff00), org
);
403 vcs_scr_writew(currcons
, (c
<< 8) |
404 (vcs_scr_readw(currcons
, org
) & 0xff), org
);
408 if (++col
== maxcol
) {
409 org
= screen_pos(currcons
, p
/2, viewed
);
416 while (this_round
> 1) {
419 w
= *((const unsigned short *)con_buf0
);
420 vcs_scr_writew(currcons
, w
, org
++);
423 if (++col
== maxcol
) {
424 org
= screen_pos(currcons
, p
, viewed
);
429 if (this_round
> 0) {
434 vcs_scr_writew(currcons
, (vcs_scr_readw(currcons
, org
) & 0xff) | (c
<< 8), org
);
436 vcs_scr_writew(currcons
, (vcs_scr_readw(currcons
, org
) & 0xff00) | c
, org
);
441 written
+= orig_count
;
445 update_region(currcons
, (unsigned long)(org0
), org
-org0
);
451 spin_unlock_irq(&console_lock
);
459 vcs_open(struct inode
*inode
, struct file
*filp
)
461 unsigned int currcons
= (MINOR(inode
->i_rdev
) & 127);
462 if(currcons
&& !vc_cons_allocated(currcons
-1))
467 static struct file_operations vcs_fops
= {
474 static devfs_handle_t devfs_handle
= NULL
;
476 void vcs_make_devfs (unsigned int index
, int unregister
)
478 #ifdef CONFIG_DEVFS_FS
481 sprintf (name
, "a%u", index
+ 1);
484 devfs_unregister ( devfs_find_handle (devfs_handle
, name
+ 1, 0, 0, 0,
485 DEVFS_SPECIAL_CHR
, 0) );
486 devfs_unregister ( devfs_find_handle (devfs_handle
, name
, 0, 0, 0,
487 DEVFS_SPECIAL_CHR
, 0) );
491 devfs_register (devfs_handle
, name
+ 1, 0, DEVFS_FL_DEFAULT
,
492 VCS_MAJOR
, index
+ 1,
493 S_IFCHR
| S_IRUSR
| S_IWUSR
, 0, 0, &vcs_fops
, NULL
);
494 devfs_register (devfs_handle
, name
, 0, DEVFS_FL_DEFAULT
,
495 VCS_MAJOR
, index
+ 129,
496 S_IFCHR
| S_IRUSR
| S_IWUSR
, 0, 0, &vcs_fops
, NULL
);
498 #endif /* CONFIG_DEVFS_FS */
501 int __init
vcs_init(void)
505 error
= devfs_register_chrdev(VCS_MAJOR
, "vcs", &vcs_fops
);
508 printk("unable to get major %d for vcs device", VCS_MAJOR
);
510 devfs_handle
= devfs_mk_dir (NULL
, "vcc", 3, NULL
);
511 devfs_register (devfs_handle
, "0", 1, DEVFS_FL_DEFAULT
,
513 S_IFCHR
| S_IRUSR
| S_IWUSR
, 0, 0, &vcs_fops
, NULL
);
514 devfs_register (devfs_handle
, "a", 1, DEVFS_FL_DEFAULT
,
516 S_IFCHR
| S_IRUSR
| S_IWUSR
, 0, 0, &vcs_fops
, NULL
);