patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / vc_screen.c
1 /*
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
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/selection.h>
36 #include <linux/kbd_kern.h>
37 #include <linux/console.h>
38 #include <linux/smp_lock.h>
39 #include <linux/device.h>
40 #include <asm/uaccess.h>
41 #include <asm/byteorder.h>
42 #include <asm/unaligned.h>
43
44 #undef attr
45 #undef org
46 #undef addr
47 #define HEADER_SIZE     4
48
49 static int
50 vcs_size(struct inode *inode)
51 {
52         int size;
53         int minor = iminor(inode);
54         int currcons = minor & 127;
55         if (currcons == 0)
56                 currcons = fg_console;
57         else
58                 currcons--;
59         if (!vc_cons_allocated(currcons))
60                 return -ENXIO;
61
62         size = video_num_lines * video_num_columns;
63
64         if (minor & 128)
65                 size = 2*size + HEADER_SIZE;
66         return size;
67 }
68
69 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
70 {
71         int size;
72
73         lock_kernel();
74         size = vcs_size(file->f_dentry->d_inode);
75         switch (orig) {
76                 default:
77                         unlock_kernel();
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                 unlock_kernel();
89                 return -EINVAL;
90         }
91         file->f_pos = offset;
92         unlock_kernel();
93         return file->f_pos;
94 }
95
96 /* We share this temporary buffer with the console write code
97  * so that we can easily avoid touching user space while holding the
98  * console spinlock.
99  */
100 extern char con_buf[PAGE_SIZE];
101 #define CON_BUF_SIZE    PAGE_SIZE
102 extern struct semaphore con_buf_sem;
103
104 static ssize_t
105 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
106 {
107         struct inode *inode = file->f_dentry->d_inode;
108         unsigned int currcons = iminor(inode);
109         long pos = *ppos;
110         long viewed, attr, read;
111         int col, maxcol;
112         unsigned short *org = NULL;
113         ssize_t ret;
114
115         down(&con_buf_sem);
116
117         /* Select the proper current console and verify
118          * sanity of the situation under the console lock.
119          */
120         acquire_console_sem();
121
122         attr = (currcons & 128);
123         currcons = (currcons & 127);
124         if (currcons == 0) {
125                 currcons = fg_console;
126                 viewed = 1;
127         } else {
128                 currcons--;
129                 viewed = 0;
130         }
131         ret = -ENXIO;
132         if (!vc_cons_allocated(currcons))
133                 goto unlock_out;
134
135         ret = -EINVAL;
136         if (pos < 0)
137                 goto unlock_out;
138         read = 0;
139         ret = 0;
140         while (count) {
141                 char *con_buf0, *con_buf_start;
142                 long this_round, size;
143                 ssize_t orig_count;
144                 long p = pos;
145
146                 /* Check whether we are above size each round,
147                  * as copy_to_user at the end of this loop
148                  * could sleep.
149                  */
150                 size = vcs_size(inode);
151                 if (pos >= size)
152                         break;
153                 if (count > size - pos)
154                         count = size - pos;
155
156                 this_round = count;
157                 if (this_round > CON_BUF_SIZE)
158                         this_round = CON_BUF_SIZE;
159
160                 /* Perform the whole read into the local con_buf.
161                  * Then we can drop the console spinlock and safely
162                  * attempt to move it to userspace.
163                  */
164
165                 con_buf_start = con_buf0 = con_buf;
166                 orig_count = this_round;
167                 maxcol = video_num_columns;
168                 if (!attr) {
169                         org = screen_pos(currcons, p, viewed);
170                         col = p % maxcol;
171                         p += maxcol - col;
172                         while (this_round-- > 0) {
173                                 *con_buf0++ = (vcs_scr_readw(currcons, org++) & 0xff);
174                                 if (++col == maxcol) {
175                                         org = screen_pos(currcons, p, viewed);
176                                         col = 0;
177                                         p += maxcol;
178                                 }
179                         }
180                 } else {
181                         if (p < HEADER_SIZE) {
182                                 size_t tmp_count;
183
184                                 con_buf0[0] = (char) video_num_lines;
185                                 con_buf0[1] = (char) video_num_columns;
186                                 getconsxy(currcons, con_buf0 + 2);
187
188                                 con_buf_start += p;
189                                 this_round += p;
190                                 if (this_round > CON_BUF_SIZE) {
191                                         this_round = CON_BUF_SIZE;
192                                         orig_count = this_round - p;
193                                 }
194
195                                 tmp_count = HEADER_SIZE;
196                                 if (tmp_count > this_round)
197                                         tmp_count = this_round;
198
199                                 /* Advance state pointers and move on. */
200                                 this_round -= tmp_count;
201                                 p = HEADER_SIZE;
202                                 con_buf0 = con_buf + HEADER_SIZE;
203                                 /* If this_round >= 0, then p is even... */
204                         } else if (p & 1) {
205                                 /* Skip first byte for output if start address is odd
206                                  * Update region sizes up/down depending on free
207                                  * space in buffer.
208                                  */
209                                 con_buf_start++;
210                                 if (this_round < CON_BUF_SIZE)
211                                         this_round++;
212                                 else
213                                         orig_count--;
214                         }
215                         if (this_round > 0) {
216                                 unsigned short *tmp_buf = (unsigned short *)con_buf0;
217
218                                 p -= HEADER_SIZE;
219                                 p /= 2;
220                                 col = p % maxcol;
221
222                                 org = screen_pos(currcons, p, viewed);
223                                 p += maxcol - col;
224
225                                 /* Buffer has even length, so we can always copy
226                                  * character + attribute. We do not copy last byte
227                                  * to userspace if this_round is odd.
228                                  */
229                                 this_round = (this_round + 1) >> 1;
230
231                                 while (this_round) {
232                                         *tmp_buf++ = vcs_scr_readw(currcons, org++);
233                                         this_round --;
234                                         if (++col == maxcol) {
235                                                 org = screen_pos(currcons, p, viewed);
236                                                 col = 0;
237                                                 p += maxcol;
238                                         }
239                                 }
240                         }
241                 }
242
243                 /* Finally, release the console semaphore while we push
244                  * all the data to userspace from our temporary buffer.
245                  *
246                  * AKPM: Even though it's a semaphore, we should drop it because
247                  * the pagefault handling code may want to call printk().
248                  */
249
250                 release_console_sem();
251                 ret = copy_to_user(buf, con_buf_start, orig_count);
252                 acquire_console_sem();
253
254                 if (ret) {
255                         read += (orig_count - ret);
256                         ret = -EFAULT;
257                         break;
258                 }
259                 buf += orig_count;
260                 pos += orig_count;
261                 read += orig_count;
262                 count -= orig_count;
263         }
264         *ppos += read;
265         if (read)
266                 ret = read;
267 unlock_out:
268         release_console_sem();
269         up(&con_buf_sem);
270         return ret;
271 }
272
273 static ssize_t
274 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
275 {
276         struct inode *inode = file->f_dentry->d_inode;
277         unsigned int currcons = iminor(inode);
278         long pos = *ppos;
279         long viewed, attr, size, written;
280         char *con_buf0;
281         int col, maxcol;
282         u16 *org0 = NULL, *org = NULL;
283         size_t ret;
284
285         down(&con_buf_sem);
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
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;
317
318                 if (this_round > CON_BUF_SIZE)
319                         this_round = CON_BUF_SIZE;
320
321                 /* Temporarily drop the console lock so that we can read
322                  * in the write data from userspace safely.
323                  */
324                 release_console_sem();
325                 ret = copy_from_user(con_buf, buf, this_round);
326                 acquire_console_sem();
327
328                 if (ret) {
329                         this_round -= ret;
330                         if (!this_round) {
331                                 /* Abort loop if no data were copied. Otherwise
332                                  * fail with -EFAULT.
333                                  */
334                                 if (written)
335                                         break;
336                                 ret = -EFAULT;
337                                 goto unlock_out;
338                         }
339                 }
340
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.
344                  */
345                 size = vcs_size(inode);
346                 if (pos >= size)
347                         break;
348                 if (this_round > size - pos)
349                         this_round = size - pos;
350
351                 /* OK, now actually push the write to the console
352                  * under the lock using the local kernel buffer.
353                  */
354
355                 con_buf0 = con_buf;
356                 orig_count = this_round;
357                 maxcol = video_num_columns;
358                 p = pos;
359                 if (!attr) {
360                         org0 = org = screen_pos(currcons, p, viewed);
361                         col = p % maxcol;
362                         p += maxcol - col;
363
364                         while (this_round > 0) {
365                                 unsigned char c = *con_buf0++;
366
367                                 this_round--;
368                                 vcs_scr_writew(currcons,
369                                                (vcs_scr_readw(currcons, org) & 0xff00) | c, org);
370                                 org++;
371                                 if (++col == maxcol) {
372                                         org = screen_pos(currcons, p, viewed);
373                                         col = 0;
374                                         p += maxcol;
375                                 }
376                         }
377                 } else {
378                         if (p < HEADER_SIZE) {
379                                 char header[HEADER_SIZE];
380
381                                 getconsxy(currcons, header + 2);
382                                 while (p < HEADER_SIZE && this_round > 0) {
383                                         this_round--;
384                                         header[p++] = *con_buf0++;
385                                 }
386                                 if (!viewed)
387                                         putconsxy(currcons, header + 2);
388                         }
389                         p -= HEADER_SIZE;
390                         col = (p/2) % maxcol;
391                         if (this_round > 0) {
392                                 org0 = org = screen_pos(currcons, p/2, viewed);
393                                 if ((p & 1) && this_round > 0) {
394                                         char c;
395
396                                         this_round--;
397                                         c = *con_buf0++;
398 #ifdef __BIG_ENDIAN
399                                         vcs_scr_writew(currcons, c |
400                                              (vcs_scr_readw(currcons, org) & 0xff00), org);
401 #else
402                                         vcs_scr_writew(currcons, (c << 8) |
403                                              (vcs_scr_readw(currcons, org) & 0xff), org);
404 #endif
405                                         org++;
406                                         p++;
407                                         if (++col == maxcol) {
408                                                 org = screen_pos(currcons, p/2, viewed);
409                                                 col = 0;
410                                         }
411                                 }
412                                 p /= 2;
413                                 p += maxcol - col;
414                         }
415                         while (this_round > 1) {
416                                 unsigned short w;
417
418                                 w = get_unaligned(((const unsigned short *)con_buf0));
419                                 vcs_scr_writew(currcons, w, org++);
420                                 con_buf0 += 2;
421                                 this_round -= 2;
422                                 if (++col == maxcol) {
423                                         org = screen_pos(currcons, p, viewed);
424                                         col = 0;
425                                         p += maxcol;
426                                 }
427                         }
428                         if (this_round > 0) {
429                                 unsigned char c;
430
431                                 c = *con_buf0++;
432 #ifdef __BIG_ENDIAN
433                                 vcs_scr_writew(currcons, (vcs_scr_readw(currcons, org) & 0xff) | (c << 8), org);
434 #else
435                                 vcs_scr_writew(currcons, (vcs_scr_readw(currcons, org) & 0xff00) | c, org);
436 #endif
437                         }
438                 }
439                 count -= orig_count;
440                 written += orig_count;
441                 buf += orig_count;
442                 pos += orig_count;
443                 if (org0)
444                         update_region(currcons, (unsigned long)(org0), org-org0);
445         }
446         *ppos += written;
447         ret = written;
448
449 unlock_out:
450         release_console_sem();
451
452         up(&con_buf_sem);
453
454         return ret;
455 }
456
457 static int
458 vcs_open(struct inode *inode, struct file *filp)
459 {
460         unsigned int currcons = iminor(inode) & 127;
461         if(currcons && !vc_cons_allocated(currcons-1))
462                 return -ENXIO;
463         return 0;
464 }
465
466 static struct file_operations vcs_fops = {
467         .llseek         = vcs_lseek,
468         .read           = vcs_read,
469         .write          = vcs_write,
470         .open           = vcs_open,
471 };
472
473 static struct class_simple *vc_class;
474
475 void vcs_make_devfs(struct tty_struct *tty)
476 {
477         devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 1),
478                         S_IFCHR|S_IRUSR|S_IWUSR,
479                         "vcc/%u", tty->index + 1);
480         devfs_mk_cdev(MKDEV(VCS_MAJOR, tty->index + 129),
481                         S_IFCHR|S_IRUSR|S_IWUSR,
482                         "vcc/a%u", tty->index + 1);
483         class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, tty->index + 1), NULL, "vcs%u", tty->index + 1);
484         class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, tty->index + 129), NULL, "vcsa%u", tty->index + 1);
485 }
486 void vcs_remove_devfs(struct tty_struct *tty)
487 {
488         devfs_remove("vcc/%u", tty->index + 1);
489         devfs_remove("vcc/a%u", tty->index + 1);
490         class_simple_device_remove(MKDEV(VCS_MAJOR, tty->index + 1));
491         class_simple_device_remove(MKDEV(VCS_MAJOR, tty->index + 129));
492 }
493
494 int __init vcs_init(void)
495 {
496         if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
497                 panic("unable to get major %d for vcs device", VCS_MAJOR);
498         vc_class = class_simple_create(THIS_MODULE, "vc");
499
500         devfs_mk_cdev(MKDEV(VCS_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/0");
501         devfs_mk_cdev(MKDEV(VCS_MAJOR, 128), S_IFCHR|S_IRUSR|S_IWUSR, "vcc/a0");
502         class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
503         class_simple_device_add(vc_class, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
504         return 0;
505 }