Char: cyclades, remove some global vars
[linux-2.6/cjktty.git] / drivers / char / vc_screen.c
blob61fbc9e52eeb342f2f5c610b453dddcbefdeccaf
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/kernel.h>
25 #include <linux/major.h>
26 #include <linux/errno.h>
27 #include <linux/tty.h>
28 #include <linux/interrupt.h>
29 #include <linux/mm.h>
30 #include <linux/init.h>
31 #include <linux/vt_kern.h>
32 #include <linux/selection.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/console.h>
35 #include <linux/device.h>
36 #include <asm/uaccess.h>
37 #include <asm/byteorder.h>
38 #include <asm/unaligned.h>
40 #undef attr
41 #undef org
42 #undef addr
43 #define HEADER_SIZE 4
45 static int
46 vcs_size(struct inode *inode)
48 int size;
49 int minor = iminor(inode);
50 int currcons = minor & 127;
51 struct vc_data *vc;
53 if (currcons == 0)
54 currcons = fg_console;
55 else
56 currcons--;
57 if (!vc_cons_allocated(currcons))
58 return -ENXIO;
59 vc = vc_cons[currcons].d;
61 size = vc->vc_rows * vc->vc_cols;
63 if (minor & 128)
64 size = 2*size + HEADER_SIZE;
65 return size;
68 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
70 int size;
72 down(&con_buf_sem);
73 size = vcs_size(file->f_path.dentry->d_inode);
74 switch (orig) {
75 default:
76 up(&con_buf_sem);
77 return -EINVAL;
78 case 2:
79 offset += size;
80 break;
81 case 1:
82 offset += file->f_pos;
83 case 0:
84 break;
86 if (offset < 0 || offset > size) {
87 up(&con_buf_sem);
88 return -EINVAL;
90 file->f_pos = offset;
91 up(&con_buf_sem);
92 return file->f_pos;
96 static ssize_t
97 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
99 struct inode *inode = file->f_path.dentry->d_inode;
100 unsigned int currcons = iminor(inode);
101 struct vc_data *vc;
102 long pos;
103 long viewed, attr, read;
104 int col, maxcol;
105 unsigned short *org = NULL;
106 ssize_t ret;
108 down(&con_buf_sem);
110 pos = *ppos;
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);
119 if (currcons == 0) {
120 currcons = fg_console;
121 viewed = 1;
122 } else {
123 currcons--;
124 viewed = 0;
126 ret = -ENXIO;
127 if (!vc_cons_allocated(currcons))
128 goto unlock_out;
129 vc = vc_cons[currcons].d;
131 ret = -EINVAL;
132 if (pos < 0)
133 goto unlock_out;
134 read = 0;
135 ret = 0;
136 while (count) {
137 char *con_buf0, *con_buf_start;
138 long this_round, size;
139 ssize_t orig_count;
140 long p = pos;
142 /* Check whether we are above size each round,
143 * as copy_to_user at the end of this loop
144 * could sleep.
146 size = vcs_size(inode);
147 if (pos >= size)
148 break;
149 if (count > size - pos)
150 count = size - pos;
152 this_round = count;
153 if (this_round > CON_BUF_SIZE)
154 this_round = CON_BUF_SIZE;
156 /* Perform the whole read into the local con_buf.
157 * Then we can drop the console spinlock and safely
158 * attempt to move it to userspace.
161 con_buf_start = con_buf0 = con_buf;
162 orig_count = this_round;
163 maxcol = vc->vc_cols;
164 if (!attr) {
165 org = screen_pos(vc, p, viewed);
166 col = p % maxcol;
167 p += maxcol - col;
168 while (this_round-- > 0) {
169 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
170 if (++col == maxcol) {
171 org = screen_pos(vc, p, viewed);
172 col = 0;
173 p += maxcol;
176 } else {
177 if (p < HEADER_SIZE) {
178 size_t tmp_count;
180 con_buf0[0] = (char)vc->vc_rows;
181 con_buf0[1] = (char)vc->vc_cols;
182 getconsxy(vc, con_buf0 + 2);
184 con_buf_start += p;
185 this_round += p;
186 if (this_round > CON_BUF_SIZE) {
187 this_round = CON_BUF_SIZE;
188 orig_count = this_round - p;
191 tmp_count = HEADER_SIZE;
192 if (tmp_count > this_round)
193 tmp_count = this_round;
195 /* Advance state pointers and move on. */
196 this_round -= tmp_count;
197 p = HEADER_SIZE;
198 con_buf0 = con_buf + HEADER_SIZE;
199 /* If this_round >= 0, then p is even... */
200 } else if (p & 1) {
201 /* Skip first byte for output if start address is odd
202 * Update region sizes up/down depending on free
203 * space in buffer.
205 con_buf_start++;
206 if (this_round < CON_BUF_SIZE)
207 this_round++;
208 else
209 orig_count--;
211 if (this_round > 0) {
212 unsigned short *tmp_buf = (unsigned short *)con_buf0;
214 p -= HEADER_SIZE;
215 p /= 2;
216 col = p % maxcol;
218 org = screen_pos(vc, p, viewed);
219 p += maxcol - col;
221 /* Buffer has even length, so we can always copy
222 * character + attribute. We do not copy last byte
223 * to userspace if this_round is odd.
225 this_round = (this_round + 1) >> 1;
227 while (this_round) {
228 *tmp_buf++ = vcs_scr_readw(vc, org++);
229 this_round --;
230 if (++col == maxcol) {
231 org = screen_pos(vc, p, viewed);
232 col = 0;
233 p += maxcol;
239 /* Finally, release the console semaphore while we push
240 * all the data to userspace from our temporary buffer.
242 * AKPM: Even though it's a semaphore, we should drop it because
243 * the pagefault handling code may want to call printk().
246 release_console_sem();
247 ret = copy_to_user(buf, con_buf_start, orig_count);
248 acquire_console_sem();
250 if (ret) {
251 read += (orig_count - ret);
252 ret = -EFAULT;
253 break;
255 buf += orig_count;
256 pos += orig_count;
257 read += orig_count;
258 count -= orig_count;
260 *ppos += read;
261 if (read)
262 ret = read;
263 unlock_out:
264 release_console_sem();
265 up(&con_buf_sem);
266 return ret;
269 static ssize_t
270 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
272 struct inode *inode = file->f_path.dentry->d_inode;
273 unsigned int currcons = iminor(inode);
274 struct vc_data *vc;
275 long pos;
276 long viewed, attr, size, written;
277 char *con_buf0;
278 int col, maxcol;
279 u16 *org0 = NULL, *org = NULL;
280 size_t ret;
282 down(&con_buf_sem);
284 pos = *ppos;
286 /* Select the proper current console and verify
287 * sanity of the situation under the console lock.
289 acquire_console_sem();
291 attr = (currcons & 128);
292 currcons = (currcons & 127);
294 if (currcons == 0) {
295 currcons = fg_console;
296 viewed = 1;
297 } else {
298 currcons--;
299 viewed = 0;
301 ret = -ENXIO;
302 if (!vc_cons_allocated(currcons))
303 goto unlock_out;
304 vc = vc_cons[currcons].d;
306 size = vcs_size(inode);
307 ret = -EINVAL;
308 if (pos < 0 || pos > size)
309 goto unlock_out;
310 if (count > size - pos)
311 count = size - pos;
312 written = 0;
313 while (count) {
314 long this_round = count;
315 size_t orig_count;
316 long p;
318 if (this_round > CON_BUF_SIZE)
319 this_round = CON_BUF_SIZE;
321 /* Temporarily drop the console lock so that we can read
322 * in the write data from userspace safely.
324 release_console_sem();
325 ret = copy_from_user(con_buf, buf, this_round);
326 acquire_console_sem();
328 if (ret) {
329 this_round -= ret;
330 if (!this_round) {
331 /* Abort loop if no data were copied. Otherwise
332 * fail with -EFAULT.
334 if (written)
335 break;
336 ret = -EFAULT;
337 goto unlock_out;
341 /* The vcs_size might have changed while we slept to grab
342 * the user buffer, so recheck.
343 * Return data written up to now on failure.
345 size = vcs_size(inode);
346 if (pos >= size)
347 break;
348 if (this_round > size - pos)
349 this_round = size - pos;
351 /* OK, now actually push the write to the console
352 * under the lock using the local kernel buffer.
355 con_buf0 = con_buf;
356 orig_count = this_round;
357 maxcol = vc->vc_cols;
358 p = pos;
359 if (!attr) {
360 org0 = org = screen_pos(vc, p, viewed);
361 col = p % maxcol;
362 p += maxcol - col;
364 while (this_round > 0) {
365 unsigned char c = *con_buf0++;
367 this_round--;
368 vcs_scr_writew(vc,
369 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
370 org++;
371 if (++col == maxcol) {
372 org = screen_pos(vc, p, viewed);
373 col = 0;
374 p += maxcol;
377 } else {
378 if (p < HEADER_SIZE) {
379 char header[HEADER_SIZE];
381 getconsxy(vc, header + 2);
382 while (p < HEADER_SIZE && this_round > 0) {
383 this_round--;
384 header[p++] = *con_buf0++;
386 if (!viewed)
387 putconsxy(vc, header + 2);
389 p -= HEADER_SIZE;
390 col = (p/2) % maxcol;
391 if (this_round > 0) {
392 org0 = org = screen_pos(vc, p/2, viewed);
393 if ((p & 1) && this_round > 0) {
394 char c;
396 this_round--;
397 c = *con_buf0++;
398 #ifdef __BIG_ENDIAN
399 vcs_scr_writew(vc, c |
400 (vcs_scr_readw(vc, org) & 0xff00), org);
401 #else
402 vcs_scr_writew(vc, (c << 8) |
403 (vcs_scr_readw(vc, org) & 0xff), org);
404 #endif
405 org++;
406 p++;
407 if (++col == maxcol) {
408 org = screen_pos(vc, p/2, viewed);
409 col = 0;
412 p /= 2;
413 p += maxcol - col;
415 while (this_round > 1) {
416 unsigned short w;
418 w = get_unaligned(((unsigned short *)con_buf0));
419 vcs_scr_writew(vc, w, org++);
420 con_buf0 += 2;
421 this_round -= 2;
422 if (++col == maxcol) {
423 org = screen_pos(vc, p, viewed);
424 col = 0;
425 p += maxcol;
428 if (this_round > 0) {
429 unsigned char c;
431 c = *con_buf0++;
432 #ifdef __BIG_ENDIAN
433 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
434 #else
435 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
436 #endif
439 count -= orig_count;
440 written += orig_count;
441 buf += orig_count;
442 pos += orig_count;
443 if (org0)
444 update_region(vc, (unsigned long)(org0), org - org0);
446 *ppos += written;
447 ret = written;
449 unlock_out:
450 release_console_sem();
452 up(&con_buf_sem);
454 return ret;
457 static int
458 vcs_open(struct inode *inode, struct file *filp)
460 unsigned int currcons = iminor(inode) & 127;
461 if(currcons && !vc_cons_allocated(currcons-1))
462 return -ENXIO;
463 return 0;
466 static const struct file_operations vcs_fops = {
467 .llseek = vcs_lseek,
468 .read = vcs_read,
469 .write = vcs_write,
470 .open = vcs_open,
473 static struct class *vc_class;
475 void vcs_make_sysfs(struct tty_struct *tty)
477 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
478 "vcs%u", tty->index + 1);
479 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
480 "vcsa%u", tty->index + 1);
483 void vcs_remove_sysfs(struct tty_struct *tty)
485 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
486 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
489 int __init vcs_init(void)
491 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
492 panic("unable to get major %d for vcs device", VCS_MAJOR);
493 vc_class = class_create(THIS_MODULE, "vc");
495 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), "vcs");
496 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), "vcsa");
497 return 0;