Import 2.3.47pre4
[davej-history.git] / drivers / char / vc_screen.c
blob8c1c9a71923e9c6fb6236625c4c808926678cecf
1 /*
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)
7 * [minor: N]
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.
12 * [minor: N+128]
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>
32 #include <linux/mm.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>
42 #undef attr
43 #undef org
44 #undef addr
45 #define HEADER_SIZE 4
47 static int
48 vcs_size(struct inode *inode)
50 int size;
51 int currcons = MINOR(inode->i_rdev) & 127;
52 if (currcons == 0)
53 currcons = fg_console;
54 else
55 currcons--;
56 if (!vc_cons_allocated(currcons))
57 return -ENXIO;
59 size = video_num_lines * video_num_columns;
61 if (MINOR(inode->i_rdev) & 128)
62 size = 2*size + HEADER_SIZE;
63 return 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);
70 switch (orig) {
71 default:
72 return -EINVAL;
73 case 2:
74 offset += size;
75 break;
76 case 1:
77 offset += file->f_pos;
78 case 0:
79 break;
81 if (offset < 0 || offset > size)
82 return -EINVAL;
83 file->f_pos = offset;
84 return file->f_pos;
87 /* We share this temporary buffer with the console write code
88 * so that we can easily avoid touching user space while holding the
89 * console spinlock.
91 extern char con_buf[PAGE_SIZE];
92 #define CON_BUF_SIZE PAGE_SIZE
93 extern struct semaphore con_buf_sem;
95 static ssize_t
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);
100 long pos = *ppos;
101 long viewed, attr, read;
102 int col, maxcol;
103 unsigned short *org = NULL;
104 ssize_t ret;
106 down(&con_buf_sem);
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);
115 if (currcons == 0) {
116 currcons = fg_console;
117 viewed = 1;
118 } else {
119 currcons--;
120 viewed = 0;
122 ret = -ENXIO;
123 if (!vc_cons_allocated(currcons))
124 goto unlock_out;
126 ret = -EINVAL;
127 if (pos < 0)
128 goto unlock_out;
129 read = 0;
130 ret = 0;
131 while (count) {
132 char *con_buf0, *con_buf_start;
133 long this_round, size;
134 ssize_t orig_count;
135 long p = pos;
137 /* Check whether we are above size each round,
138 * as copy_to_user at the end of this loop
139 * could sleep.
141 size = vcs_size(inode);
142 if (pos >= size)
143 break;
144 if (count > size - pos)
145 count = size - pos;
147 this_round = count;
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;
159 if (!attr) {
160 org = screen_pos(currcons, p, viewed);
161 col = p % maxcol;
162 p += maxcol - col;
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);
167 col = 0;
168 p += maxcol;
171 } else {
172 if (p < HEADER_SIZE) {
173 size_t tmp_count;
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;
185 con_buf_start += p;
186 orig_count -= p;
187 p += tmp_count;
188 con_buf0 = con_buf + p;
190 p -= HEADER_SIZE;
191 col = (p/2) % maxcol;
192 if (this_round > 0) {
193 char tmp_byte;
195 org = screen_pos(currcons, p/2, viewed);
196 if ((p & 1) && this_round > 0) {
197 #ifdef __BIG_ENDIAN
198 tmp_byte = vcs_scr_readw(currcons, org++) & 0xff;
199 #else
200 tmp_byte = vcs_scr_readw(currcons, org++) >> 8;
201 #endif
203 *con_buf0++ = tmp_byte;
205 this_round--;
206 p++;
207 if (++col == maxcol) {
208 org = screen_pos(currcons, p/2, viewed);
209 col = 0;
212 p /= 2;
213 p += maxcol - col;
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++);
222 tmp_count -= 2;
223 if (++col == maxcol) {
224 org = screen_pos(currcons, p, viewed);
225 col = 0;
226 p += maxcol;
230 /* Advance pointers, and move on. */
231 this_round = tmp_count;
232 con_buf0 = (char*)tmp_buf;
234 if (this_round > 0) {
235 char tmp_byte;
237 #ifdef __BIG_ENDIAN
238 tmp_byte = vcs_scr_readw(currcons, org) >> 8;
239 #else
240 tmp_byte = vcs_scr_readw(currcons, org) & 0xff;
241 #endif
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);
255 if (ret) {
256 read += (orig_count - ret);
257 ret = -EFAULT;
258 break;
260 buf += orig_count;
261 pos += orig_count;
262 read += orig_count;
263 count -= orig_count;
265 *ppos += read;
266 if (read)
267 ret = read;
268 unlock_out:
269 spin_unlock_irq(&console_lock);
270 up(&con_buf_sem);
271 return ret;
274 static ssize_t
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);
279 long pos = *ppos;
280 long viewed, attr, size, written;
281 char *con_buf0;
282 int col, maxcol;
283 u16 *org0 = NULL, *org = NULL;
284 size_t ret;
286 down(&con_buf_sem);
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);
296 if (currcons == 0) {
297 currcons = fg_console;
298 viewed = 1;
299 } else {
300 currcons--;
301 viewed = 0;
303 ret = -ENXIO;
304 if (!vc_cons_allocated(currcons))
305 goto unlock_out;
307 size = vcs_size(inode);
308 ret = -EINVAL;
309 if (pos < 0 || pos > size)
310 goto unlock_out;
311 if (count > size - pos)
312 count = size - pos;
313 written = 0;
314 while (count) {
315 long this_round = count;
316 size_t orig_count;
317 long p;
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);
329 if (ret) {
330 this_round -= ret;
331 if (!this_round) {
332 /* Abort loop if no data were copied. Otherwise
333 * fail with -EFAULT.
335 if (written)
336 break;
337 ret = -EFAULT;
338 goto unlock_out;
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);
347 if (pos >= size)
348 break;
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.
356 con_buf0 = con_buf;
357 orig_count = this_round;
358 maxcol = video_num_columns;
359 p = pos;
360 if (!attr) {
361 org0 = org = screen_pos(currcons, p, viewed);
362 col = p % maxcol;
363 p += maxcol - col;
365 while (this_round > 0) {
366 unsigned char c = *con_buf0++;
368 this_round--;
369 vcs_scr_writew(currcons,
370 (vcs_scr_readw(currcons, org) & 0xff00) | c, org);
371 org++;
372 if (++col == maxcol) {
373 org = screen_pos(currcons, p, viewed);
374 col = 0;
375 p += maxcol;
378 } else {
379 if (p < HEADER_SIZE) {
380 char header[HEADER_SIZE];
382 getconsxy(currcons, header + 2);
383 while (p < HEADER_SIZE && this_round > 0) {
384 this_round--;
385 header[p++] = *con_buf0++;
387 if (!viewed)
388 putconsxy(currcons, header + 2);
390 p -= HEADER_SIZE;
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) {
395 char c;
397 this_round--;
398 c = *con_buf0++;
399 #ifdef __BIG_ENDIAN
400 vcs_scr_writew(currcons, c |
401 (vcs_scr_readw(currcons, org) & 0xff00), org);
402 #else
403 vcs_scr_writew(currcons, (c << 8) |
404 (vcs_scr_readw(currcons, org) & 0xff), org);
405 #endif
406 org++;
407 p++;
408 if (++col == maxcol) {
409 org = screen_pos(currcons, p/2, viewed);
410 col = 0;
413 p /= 2;
414 p += maxcol - col;
416 while (this_round > 1) {
417 unsigned short w;
419 w = *((const unsigned short *)con_buf0);
420 vcs_scr_writew(currcons, w, org++);
421 con_buf0 += 2;
422 this_round -= 2;
423 if (++col == maxcol) {
424 org = screen_pos(currcons, p, viewed);
425 col = 0;
426 p += maxcol;
429 if (this_round > 0) {
430 unsigned char c;
432 c = *con_buf0++;
433 #ifdef __BIG_ENDIAN
434 vcs_scr_writew(currcons, (vcs_scr_readw(currcons, org) & 0xff) | (c << 8), org);
435 #else
436 vcs_scr_writew(currcons, (vcs_scr_readw(currcons, org) & 0xff00) | c, org);
437 #endif
440 count -= orig_count;
441 written += orig_count;
442 buf += orig_count;
443 pos += orig_count;
444 if (org0)
445 update_region(currcons, (unsigned long)(org0), org-org0);
447 *ppos += written;
448 ret = written;
450 unlock_out:
451 spin_unlock_irq(&console_lock);
453 up(&con_buf_sem);
455 return ret;
458 static int
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))
463 return -ENXIO;
464 return 0;
467 static struct file_operations vcs_fops = {
468 llseek: vcs_lseek,
469 read: vcs_read,
470 write: vcs_write,
471 open: vcs_open,
474 static devfs_handle_t devfs_handle = NULL;
476 void vcs_make_devfs (unsigned int index, int unregister)
478 #ifdef CONFIG_DEVFS_FS
479 char name[8];
481 sprintf (name, "a%u", index + 1);
482 if (unregister)
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) );
489 else
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)
503 int error;
505 error = devfs_register_chrdev(VCS_MAJOR, "vcs", &vcs_fops);
507 if (error)
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,
512 VCS_MAJOR, 0,
513 S_IFCHR | S_IRUSR | S_IWUSR, 0, 0, &vcs_fops, NULL);
514 devfs_register (devfs_handle, "a", 1, DEVFS_FL_DEFAULT,
515 VCS_MAJOR, 128,
516 S_IFCHR | S_IRUSR | S_IWUSR, 0, 0, &vcs_fops, NULL);
518 return error;