blob: 791930320a13c21e2a33c2f81b0c18b233463dba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vc_screen.c
3 *
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]
8 *
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]
13 *
14 * This replaces screendump and part of selection, so that the system
15 * administrator can control access using file system permissions.
16 *
17 * aeb@cwi.nl - efter Friedas begravelse - 950211
18 *
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
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/major.h>
26#include <linux/errno.h>
27#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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/smp_lock.h>
36#include <linux/device.h>
37#include <asm/uaccess.h>
38#include <asm/byteorder.h>
39#include <asm/unaligned.h>
40
41#undef attr
42#undef org
43#undef addr
44#define HEADER_SIZE 4
45
46static int
47vcs_size(struct inode *inode)
48{
49 int size;
50 int minor = iminor(inode);
51 int currcons = minor & 127;
52 struct vc_data *vc;
53
54 if (currcons == 0)
55 currcons = fg_console;
56 else
57 currcons--;
58 if (!vc_cons_allocated(currcons))
59 return -ENXIO;
60 vc = vc_cons[currcons].d;
61
62 size = vc->vc_rows * vc->vc_cols;
63
64 if (minor & 128)
65 size = 2*size + HEADER_SIZE;
66 return size;
67}
68
69static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
70{
71 int size;
72
73 down(&con_buf_sem);
Josef Sipeka7113a92006-12-08 02:36:55 -080074 size = vcs_size(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 switch (orig) {
76 default:
77 up(&con_buf_sem);
78 return -EINVAL;
79 case 2:
80 offset += size;
81 break;
82 case 1:
83 offset += file->f_pos;
84 case 0:
85 break;
86 }
87 if (offset < 0 || offset > size) {
88 up(&con_buf_sem);
89 return -EINVAL;
90 }
91 file->f_pos = offset;
92 up(&con_buf_sem);
93 return file->f_pos;
94}
95
96
97static ssize_t
98vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
99{
Josef Sipeka7113a92006-12-08 02:36:55 -0800100 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned int currcons = iminor(inode);
102 struct vc_data *vc;
103 long pos;
104 long viewed, attr, read;
105 int col, maxcol;
106 unsigned short *org = NULL;
107 ssize_t ret;
108
109 down(&con_buf_sem);
110
111 pos = *ppos;
112
113 /* Select the proper current console and verify
114 * sanity of the situation under the console lock.
115 */
116 acquire_console_sem();
117
118 attr = (currcons & 128);
119 currcons = (currcons & 127);
120 if (currcons == 0) {
121 currcons = fg_console;
122 viewed = 1;
123 } else {
124 currcons--;
125 viewed = 0;
126 }
127 ret = -ENXIO;
128 if (!vc_cons_allocated(currcons))
129 goto unlock_out;
130 vc = vc_cons[currcons].d;
131
132 ret = -EINVAL;
133 if (pos < 0)
134 goto unlock_out;
135 read = 0;
136 ret = 0;
137 while (count) {
138 char *con_buf0, *con_buf_start;
139 long this_round, size;
140 ssize_t orig_count;
141 long p = pos;
142
143 /* Check whether we are above size each round,
144 * as copy_to_user at the end of this loop
145 * could sleep.
146 */
147 size = vcs_size(inode);
148 if (pos >= size)
149 break;
150 if (count > size - pos)
151 count = size - pos;
152
153 this_round = count;
154 if (this_round > CON_BUF_SIZE)
155 this_round = CON_BUF_SIZE;
156
157 /* Perform the whole read into the local con_buf.
158 * Then we can drop the console spinlock and safely
159 * attempt to move it to userspace.
160 */
161
162 con_buf_start = con_buf0 = con_buf;
163 orig_count = this_round;
164 maxcol = vc->vc_cols;
165 if (!attr) {
166 org = screen_pos(vc, p, viewed);
167 col = p % maxcol;
168 p += maxcol - col;
169 while (this_round-- > 0) {
170 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
171 if (++col == maxcol) {
172 org = screen_pos(vc, p, viewed);
173 col = 0;
174 p += maxcol;
175 }
176 }
177 } else {
178 if (p < HEADER_SIZE) {
179 size_t tmp_count;
180
181 con_buf0[0] = (char)vc->vc_rows;
182 con_buf0[1] = (char)vc->vc_cols;
183 getconsxy(vc, con_buf0 + 2);
184
185 con_buf_start += p;
186 this_round += p;
187 if (this_round > CON_BUF_SIZE) {
188 this_round = CON_BUF_SIZE;
189 orig_count = this_round - p;
190 }
191
192 tmp_count = HEADER_SIZE;
193 if (tmp_count > this_round)
194 tmp_count = this_round;
195
196 /* Advance state pointers and move on. */
197 this_round -= tmp_count;
198 p = HEADER_SIZE;
199 con_buf0 = con_buf + HEADER_SIZE;
200 /* If this_round >= 0, then p is even... */
201 } else if (p & 1) {
202 /* Skip first byte for output if start address is odd
203 * Update region sizes up/down depending on free
204 * space in buffer.
205 */
206 con_buf_start++;
207 if (this_round < CON_BUF_SIZE)
208 this_round++;
209 else
210 orig_count--;
211 }
212 if (this_round > 0) {
213 unsigned short *tmp_buf = (unsigned short *)con_buf0;
214
215 p -= HEADER_SIZE;
216 p /= 2;
217 col = p % maxcol;
218
219 org = screen_pos(vc, p, viewed);
220 p += maxcol - col;
221
222 /* Buffer has even length, so we can always copy
223 * character + attribute. We do not copy last byte
224 * to userspace if this_round is odd.
225 */
226 this_round = (this_round + 1) >> 1;
227
228 while (this_round) {
229 *tmp_buf++ = vcs_scr_readw(vc, org++);
230 this_round --;
231 if (++col == maxcol) {
232 org = screen_pos(vc, p, viewed);
233 col = 0;
234 p += maxcol;
235 }
236 }
237 }
238 }
239
240 /* Finally, release the console semaphore while we push
241 * all the data to userspace from our temporary buffer.
242 *
243 * AKPM: Even though it's a semaphore, we should drop it because
244 * the pagefault handling code may want to call printk().
245 */
246
247 release_console_sem();
248 ret = copy_to_user(buf, con_buf_start, orig_count);
249 acquire_console_sem();
250
251 if (ret) {
252 read += (orig_count - ret);
253 ret = -EFAULT;
254 break;
255 }
256 buf += orig_count;
257 pos += orig_count;
258 read += orig_count;
259 count -= orig_count;
260 }
261 *ppos += read;
262 if (read)
263 ret = read;
264unlock_out:
265 release_console_sem();
266 up(&con_buf_sem);
267 return ret;
268}
269
270static ssize_t
271vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
272{
Josef Sipeka7113a92006-12-08 02:36:55 -0800273 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned int currcons = iminor(inode);
275 struct vc_data *vc;
276 long pos;
277 long viewed, attr, size, written;
278 char *con_buf0;
279 int col, maxcol;
280 u16 *org0 = NULL, *org = NULL;
281 size_t ret;
282
283 down(&con_buf_sem);
284
285 pos = *ppos;
286
287 /* Select the proper current console and verify
288 * sanity of the situation under the console lock.
289 */
290 acquire_console_sem();
291
292 attr = (currcons & 128);
293 currcons = (currcons & 127);
294
295 if (currcons == 0) {
296 currcons = fg_console;
297 viewed = 1;
298 } else {
299 currcons--;
300 viewed = 0;
301 }
302 ret = -ENXIO;
303 if (!vc_cons_allocated(currcons))
304 goto unlock_out;
305 vc = vc_cons[currcons].d;
306
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;
318
319 if (this_round > CON_BUF_SIZE)
320 this_round = CON_BUF_SIZE;
321
322 /* Temporarily drop the console lock so that we can read
323 * in the write data from userspace safely.
324 */
325 release_console_sem();
326 ret = copy_from_user(con_buf, buf, this_round);
327 acquire_console_sem();
328
329 if (ret) {
330 this_round -= ret;
331 if (!this_round) {
332 /* Abort loop if no data were copied. Otherwise
333 * fail with -EFAULT.
334 */
335 if (written)
336 break;
337 ret = -EFAULT;
338 goto unlock_out;
339 }
340 }
341
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.
345 */
346 size = vcs_size(inode);
347 if (pos >= size)
348 break;
349 if (this_round > size - pos)
350 this_round = size - pos;
351
352 /* OK, now actually push the write to the console
353 * under the lock using the local kernel buffer.
354 */
355
356 con_buf0 = con_buf;
357 orig_count = this_round;
358 maxcol = vc->vc_cols;
359 p = pos;
360 if (!attr) {
361 org0 = org = screen_pos(vc, p, viewed);
362 col = p % maxcol;
363 p += maxcol - col;
364
365 while (this_round > 0) {
366 unsigned char c = *con_buf0++;
367
368 this_round--;
369 vcs_scr_writew(vc,
370 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
371 org++;
372 if (++col == maxcol) {
373 org = screen_pos(vc, p, viewed);
374 col = 0;
375 p += maxcol;
376 }
377 }
378 } else {
379 if (p < HEADER_SIZE) {
380 char header[HEADER_SIZE];
381
382 getconsxy(vc, header + 2);
383 while (p < HEADER_SIZE && this_round > 0) {
384 this_round--;
385 header[p++] = *con_buf0++;
386 }
387 if (!viewed)
388 putconsxy(vc, header + 2);
389 }
390 p -= HEADER_SIZE;
391 col = (p/2) % maxcol;
392 if (this_round > 0) {
393 org0 = org = screen_pos(vc, p/2, viewed);
394 if ((p & 1) && this_round > 0) {
395 char c;
396
397 this_round--;
398 c = *con_buf0++;
399#ifdef __BIG_ENDIAN
400 vcs_scr_writew(vc, c |
401 (vcs_scr_readw(vc, org) & 0xff00), org);
402#else
403 vcs_scr_writew(vc, (c << 8) |
404 (vcs_scr_readw(vc, org) & 0xff), org);
405#endif
406 org++;
407 p++;
408 if (++col == maxcol) {
409 org = screen_pos(vc, p/2, viewed);
410 col = 0;
411 }
412 }
413 p /= 2;
414 p += maxcol - col;
415 }
416 while (this_round > 1) {
417 unsigned short w;
418
Dave Jonesee025942005-12-28 20:01:04 -0500419 w = get_unaligned(((unsigned short *)con_buf0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 vcs_scr_writew(vc, w, org++);
421 con_buf0 += 2;
422 this_round -= 2;
423 if (++col == maxcol) {
424 org = screen_pos(vc, p, viewed);
425 col = 0;
426 p += maxcol;
427 }
428 }
429 if (this_round > 0) {
430 unsigned char c;
431
432 c = *con_buf0++;
433#ifdef __BIG_ENDIAN
434 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
435#else
436 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
437#endif
438 }
439 }
440 count -= orig_count;
441 written += orig_count;
442 buf += orig_count;
443 pos += orig_count;
444 if (org0)
445 update_region(vc, (unsigned long)(org0), org - org0);
446 }
447 *ppos += written;
448 ret = written;
449
450unlock_out:
451 release_console_sem();
452
453 up(&con_buf_sem);
454
455 return ret;
456}
457
458static int
459vcs_open(struct inode *inode, struct file *filp)
460{
461 unsigned int currcons = iminor(inode) & 127;
462 if(currcons && !vc_cons_allocated(currcons-1))
463 return -ENXIO;
464 return 0;
465}
466
Arjan van de Ven62322d22006-07-03 00:24:21 -0700467static const struct file_operations vcs_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .llseek = vcs_lseek,
469 .read = vcs_read,
470 .write = vcs_write,
471 .open = vcs_open,
472};
473
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800474static struct class *vc_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Alan Coxd09d7dd2006-09-29 01:59:47 -0700476void vcs_make_sysfs(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Greg Kroah-Hartmancd154222006-08-07 22:19:37 -0700478 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
479 "vcs%u", tty->index + 1);
480 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
481 "vcsa%u", tty->index + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Alan Coxd09d7dd2006-09-29 01:59:47 -0700483
484void vcs_remove_sysfs(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Greg Kroah-Hartmancd154222006-08-07 22:19:37 -0700486 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
487 device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490int __init vcs_init(void)
491{
492 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
493 panic("unable to get major %d for vcs device", VCS_MAJOR);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800494 vc_class = class_create(THIS_MODULE, "vc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Greg Kroah-Hartmancd154222006-08-07 22:19:37 -0700496 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), "vcs");
497 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), "vcsa");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499}