Full support for Ginger Console
[linux-ginger.git] / drivers / char / vc_screen.c
blobc1791a63d99d40c2e1dbe7a4fba6e1f8cfba8c16
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/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>
43 #undef attr
44 #undef org
45 #undef addr
46 #define HEADER_SIZE 4
48 static int
49 vcs_size(struct inode *inode)
51 int size;
52 int minor = iminor(inode);
53 int currcons = minor & 127;
54 struct vc_data *vc;
56 if (currcons == 0)
57 currcons = fg_console;
58 else
59 currcons--;
60 if (!vc_cons_allocated(currcons))
61 return -ENXIO;
62 vc = vc_cons[currcons].d;
64 size = vc->vc_rows * vc->vc_cols;
66 if (minor & 128)
67 size = 2*size + HEADER_SIZE;
68 return size;
71 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
73 int size;
75 mutex_lock(&con_buf_mtx);
76 size = vcs_size(file->f_path.dentry->d_inode);
77 switch (orig) {
78 default:
79 mutex_unlock(&con_buf_mtx);
80 return -EINVAL;
81 case 2:
82 offset += size;
83 break;
84 case 1:
85 offset += file->f_pos;
86 case 0:
87 break;
89 if (offset < 0 || offset > size) {
90 mutex_unlock(&con_buf_mtx);
91 return -EINVAL;
93 file->f_pos = offset;
94 mutex_unlock(&con_buf_mtx);
95 return file->f_pos;
99 static ssize_t
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);
104 struct vc_data *vc;
105 long pos;
106 long viewed, attr, read;
107 int col, maxcol;
108 unsigned short *org = NULL;
109 ssize_t ret;
111 mutex_lock(&con_buf_mtx);
113 pos = *ppos;
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);
122 if (currcons == 0) {
123 currcons = fg_console;
124 viewed = 1;
125 } else {
126 currcons--;
127 viewed = 0;
129 ret = -ENXIO;
130 if (!vc_cons_allocated(currcons))
131 goto unlock_out;
132 vc = vc_cons[currcons].d;
134 ret = -EINVAL;
135 if (pos < 0)
136 goto unlock_out;
137 read = 0;
138 ret = 0;
139 while (count) {
140 char *con_buf0, *con_buf_start;
141 long this_round, size;
142 ssize_t orig_count;
143 long p = pos;
145 /* Check whether we are above size each round,
146 * as copy_to_user at the end of this loop
147 * could sleep.
149 size = vcs_size(inode);
150 if (pos >= size)
151 break;
152 if (count > size - pos)
153 count = size - pos;
155 this_round = count;
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;
167 if (!attr) {
168 org = screen_pos(vc, p, viewed);
169 col = p % maxcol;
170 p += maxcol - col;
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);
175 col = 0;
176 p += maxcol;
179 } else {
180 if (p < HEADER_SIZE) {
181 size_t tmp_count;
183 con_buf0[0] = (char)vc->vc_rows;
184 con_buf0[1] = (char)vc->vc_cols;
185 getconsxy(vc, con_buf0 + 2);
187 con_buf_start += p;
188 this_round += p;
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;
200 p = HEADER_SIZE;
201 con_buf0 = con_buf + HEADER_SIZE;
202 /* If this_round >= 0, then p is even... */
203 } else if (p & 1) {
204 /* Skip first byte for output if start address is odd
205 * Update region sizes up/down depending on free
206 * space in buffer.
208 con_buf_start++;
209 if (this_round < CON_BUF_SIZE)
210 this_round++;
211 else
212 orig_count--;
214 if (this_round > 0) {
215 unsigned short *tmp_buf = (unsigned short *)con_buf0;
217 p -= HEADER_SIZE;
218 p /= 2;
219 col = p % maxcol;
221 org = screen_pos(vc, p, viewed);
222 p += maxcol - col;
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;
230 while (this_round) {
231 *tmp_buf++ = vcs_scr_readw(vc, org++);
232 this_round --;
233 if (++col == maxcol) {
234 org = screen_pos(vc, p, viewed);
235 col = 0;
236 p += maxcol;
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();
253 if (ret) {
254 read += (orig_count - ret);
255 ret = -EFAULT;
256 break;
258 buf += orig_count;
259 pos += orig_count;
260 read += orig_count;
261 count -= orig_count;
263 *ppos += read;
264 if (read)
265 ret = read;
266 unlock_out:
267 release_console_sem();
268 mutex_unlock(&con_buf_mtx);
269 return ret;
272 static ssize_t
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);
277 struct vc_data *vc;
278 long pos;
279 long viewed, attr, size, written;
280 char *con_buf0;
281 int col, maxcol;
282 u16 *org0 = NULL, *org = NULL;
283 size_t ret;
285 mutex_lock(&con_buf_mtx);
287 pos = *ppos;
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);
297 if (currcons == 0) {
298 currcons = fg_console;
299 viewed = 1;
300 } else {
301 currcons--;
302 viewed = 0;
304 ret = -ENXIO;
305 if (!vc_cons_allocated(currcons))
306 goto unlock_out;
307 vc = vc_cons[currcons].d;
309 size = vcs_size(inode);
310 ret = -EINVAL;
311 if (pos < 0 || pos > size)
312 goto unlock_out;
313 if (count > size - pos)
314 count = size - pos;
315 written = 0;
316 while (count) {
317 long this_round = count;
318 size_t orig_count;
319 long p;
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();
331 if (ret) {
332 this_round -= ret;
333 if (!this_round) {
334 /* Abort loop if no data were copied. Otherwise
335 * fail with -EFAULT.
337 if (written)
338 break;
339 ret = -EFAULT;
340 goto unlock_out;
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);
349 if (pos >= size)
350 break;
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.
358 con_buf0 = con_buf;
359 orig_count = this_round;
360 maxcol = vc->vc_cols;
361 p = pos;
362 if (!attr) {
363 org0 = org = screen_pos(vc, p, viewed);
364 col = p % maxcol;
365 p += maxcol - col;
367 while (this_round > 0) {
368 unsigned char c = *con_buf0++;
370 this_round--;
371 vcs_scr_writew(vc,
372 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
373 org++;
374 if (++col == maxcol) {
375 org = screen_pos(vc, p, viewed);
376 col = 0;
377 p += maxcol;
380 } else {
381 if (p < HEADER_SIZE) {
382 char header[HEADER_SIZE];
384 getconsxy(vc, header + 2);
385 while (p < HEADER_SIZE && this_round > 0) {
386 this_round--;
387 header[p++] = *con_buf0++;
389 if (!viewed)
390 putconsxy(vc, header + 2);
392 p -= HEADER_SIZE;
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) {
397 char c;
399 this_round--;
400 c = *con_buf0++;
401 #ifdef __BIG_ENDIAN
402 vcs_scr_writew(vc, c |
403 (vcs_scr_readw(vc, org) & 0xff00), org);
404 #else
405 vcs_scr_writew(vc, (c << 8) |
406 (vcs_scr_readw(vc, org) & 0xff), org);
407 #endif
408 org++;
409 p++;
410 if (++col == maxcol) {
411 org = screen_pos(vc, p/2, viewed);
412 col = 0;
415 p /= 2;
416 p += maxcol - col;
418 while (this_round > 1) {
419 unsigned short w;
421 w = get_unaligned(((unsigned short *)con_buf0));
422 vcs_scr_writew(vc, w, org++);
423 con_buf0 += 2;
424 this_round -= 2;
425 if (++col == maxcol) {
426 org = screen_pos(vc, p, viewed);
427 col = 0;
428 p += maxcol;
431 if (this_round > 0) {
432 unsigned char c;
434 c = *con_buf0++;
435 #ifdef __BIG_ENDIAN
436 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
437 #else
438 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
439 #endif
442 count -= orig_count;
443 written += orig_count;
444 buf += orig_count;
445 pos += orig_count;
446 if (org0)
447 update_region(vc, (unsigned long)(org0), org - org0);
449 *ppos += written;
450 ret = written;
452 unlock_out:
453 release_console_sem();
455 mutex_unlock(&con_buf_mtx);
457 return ret;
460 static int
461 vcs_open(struct inode *inode, struct file *filp)
463 unsigned int currcons = iminor(inode) & 127;
464 int ret = 0;
466 lock_kernel();
467 if(currcons && !vc_cons_allocated(currcons-1))
468 ret = -ENXIO;
469 unlock_kernel();
470 return ret;
473 static const struct file_operations vcs_fops = {
474 .llseek = vcs_lseek,
475 .read = vcs_read,
476 .write = vcs_write,
477 .open = vcs_open,
480 static struct class *vc_class;
482 void vcs_make_sysfs(int index)
484 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
485 "vcs%u", index + 1);
486 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
487 "vcsa%u", index + 1);
490 void vcs_remove_sysfs(int index)
492 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
493 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
496 int __init vcs_init(void)
498 unsigned int i;
500 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
501 panic("unable to get major %d for vcs device", VCS_MAJOR);
502 vc_class = class_create(THIS_MODULE, "vc");
504 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
505 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
506 for (i = 0; i < MIN_NR_CONSOLES; i++)
507 vcs_make_sysfs(i);
508 return 0;