Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6] / 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/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>
38
39 #include <asm/uaccess.h>
40 #include <asm/byteorder.h>
41 #include <asm/unaligned.h>
42
43 #undef attr
44 #undef org
45 #undef addr
46 #define HEADER_SIZE     4
47
48 static int
49 vcs_size(struct inode *inode)
50 {
51         int size;
52         int minor = iminor(inode);
53         int currcons = minor & 127;
54         struct vc_data *vc;
55
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;
63
64         size = vc->vc_rows * vc->vc_cols;
65
66         if (minor & 128)
67                 size = 2*size + HEADER_SIZE;
68         return size;
69 }
70
71 static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
72 {
73         int size;
74
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;
88         }
89         if (offset < 0 || offset > size) {
90                 mutex_unlock(&con_buf_mtx);
91                 return -EINVAL;
92         }
93         file->f_pos = offset;
94         mutex_unlock(&con_buf_mtx);
95         return file->f_pos;
96 }
97
98
99 static ssize_t
100 vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
101 {
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;
110
111         mutex_lock(&con_buf_mtx);
112
113         pos = *ppos;
114
115         /* Select the proper current console and verify
116          * sanity of the situation under the console lock.
117          */
118         acquire_console_sem();
119
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;
128         }
129         ret = -ENXIO;
130         if (!vc_cons_allocated(currcons))
131                 goto unlock_out;
132         vc = vc_cons[currcons].d;
133
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;
144
145                 /* Check whether we are above size each round,
146                  * as copy_to_user at the end of this loop
147                  * could sleep.
148                  */
149                 size = vcs_size(inode);
150                 if (pos >= size)
151                         break;
152                 if (count > size - pos)
153                         count = size - pos;
154
155                 this_round = count;
156                 if (this_round > CON_BUF_SIZE)
157                         this_round = CON_BUF_SIZE;
158
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.
162                  */
163
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;
177                                 }
178                         }
179                 } else {
180                         if (p < HEADER_SIZE) {
181                                 size_t tmp_count;
182
183                                 con_buf0[0] = (char)vc->vc_rows;
184                                 con_buf0[1] = (char)vc->vc_cols;
185                                 getconsxy(vc, con_buf0 + 2);
186
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;
192                                 }
193
194                                 tmp_count = HEADER_SIZE;
195                                 if (tmp_count > this_round)
196                                         tmp_count = this_round;
197
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.
207                                  */
208                                 con_buf_start++;
209                                 if (this_round < CON_BUF_SIZE)
210                                         this_round++;
211                                 else
212                                         orig_count--;
213                         }
214                         if (this_round > 0) {
215                                 unsigned short *tmp_buf = (unsigned short *)con_buf0;
216
217                                 p -= HEADER_SIZE;
218                                 p /= 2;
219                                 col = p % maxcol;
220
221                                 org = screen_pos(vc, p, viewed);
222                                 p += maxcol - col;
223
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.
227                                  */
228                                 this_round = (this_round + 1) >> 1;
229
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;
237                                         }
238                                 }
239                         }
240                 }
241
242                 /* Finally, release the console semaphore while we push
243                  * all the data to userspace from our temporary buffer.
244                  *
245                  * AKPM: Even though it's a semaphore, we should drop it because
246                  * the pagefault handling code may want to call printk().
247                  */
248
249                 release_console_sem();
250                 ret = copy_to_user(buf, con_buf_start, orig_count);
251                 acquire_console_sem();
252
253                 if (ret) {
254                         read += (orig_count - ret);
255                         ret = -EFAULT;
256                         break;
257                 }
258                 buf += orig_count;
259                 pos += orig_count;
260                 read += orig_count;
261                 count -= orig_count;
262         }
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;
270 }
271
272 static ssize_t
273 vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
274 {
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;
284
285         mutex_lock(&con_buf_mtx);
286
287         pos = *ppos;
288
289         /* Select the proper current console and verify
290          * sanity of the situation under the console lock.
291          */
292         acquire_console_sem();
293
294         attr = (currcons & 128);
295         currcons = (currcons & 127);
296
297         if (currcons == 0) {
298                 currcons = fg_console;
299                 viewed = 1;
300         } else {
301                 currcons--;
302                 viewed = 0;
303         }
304         ret = -ENXIO;
305         if (!vc_cons_allocated(currcons))
306                 goto unlock_out;
307         vc = vc_cons[currcons].d;
308
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;
320
321                 if (this_round > CON_BUF_SIZE)
322                         this_round = CON_BUF_SIZE;
323
324                 /* Temporarily drop the console lock so that we can read
325                  * in the write data from userspace safely.
326                  */
327                 release_console_sem();
328                 ret = copy_from_user(con_buf, buf, this_round);
329                 acquire_console_sem();
330
331                 if (ret) {
332                         this_round -= ret;
333                         if (!this_round) {
334                                 /* Abort loop if no data were copied. Otherwise
335                                  * fail with -EFAULT.
336                                  */
337                                 if (written)
338                                         break;
339                                 ret = -EFAULT;
340                                 goto unlock_out;
341                         }
342                 }
343
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.
347                  */
348                 size = vcs_size(inode);
349                 if (pos >= size)
350                         break;
351                 if (this_round > size - pos)
352                         this_round = size - pos;
353
354                 /* OK, now actually push the write to the console
355                  * under the lock using the local kernel buffer.
356                  */
357
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;
366
367                         while (this_round > 0) {
368                                 unsigned char c = *con_buf0++;
369
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;
378                                 }
379                         }
380                 } else {
381                         if (p < HEADER_SIZE) {
382                                 char header[HEADER_SIZE];
383
384                                 getconsxy(vc, header + 2);
385                                 while (p < HEADER_SIZE && this_round > 0) {
386                                         this_round--;
387                                         header[p++] = *con_buf0++;
388                                 }
389                                 if (!viewed)
390                                         putconsxy(vc, header + 2);
391                         }
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;
398
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;
413                                         }
414                                 }
415                                 p /= 2;
416                                 p += maxcol - col;
417                         }
418                         while (this_round > 1) {
419                                 unsigned short w;
420
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;
429                                 }
430                         }
431                         if (this_round > 0) {
432                                 unsigned char c;
433
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
440                         }
441                 }
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);
448         }
449         *ppos += written;
450         ret = written;
451
452 unlock_out:
453         release_console_sem();
454
455         mutex_unlock(&con_buf_mtx);
456
457         return ret;
458 }
459
460 static int
461 vcs_open(struct inode *inode, struct file *filp)
462 {
463         unsigned int currcons = iminor(inode) & 127;
464         int ret = 0;
465         
466         lock_kernel();
467         if(currcons && !vc_cons_allocated(currcons-1))
468                 ret = -ENXIO;
469         unlock_kernel();
470         return ret;
471 }
472
473 static const struct file_operations vcs_fops = {
474         .llseek         = vcs_lseek,
475         .read           = vcs_read,
476         .write          = vcs_write,
477         .open           = vcs_open,
478 };
479
480 static struct class *vc_class;
481
482 void vcs_make_sysfs(struct tty_struct *tty)
483 {
484         device_create_drvdata(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 1),
485                               NULL, "vcs%u", tty->index + 1);
486         device_create_drvdata(vc_class, NULL, MKDEV(VCS_MAJOR, tty->index + 129),
487                               NULL, "vcsa%u", tty->index + 1);
488 }
489
490 void vcs_remove_sysfs(struct tty_struct *tty)
491 {
492         device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 1));
493         device_destroy(vc_class, MKDEV(VCS_MAJOR, tty->index + 129));
494 }
495
496 int __init vcs_init(void)
497 {
498         if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
499                 panic("unable to get major %d for vcs device", VCS_MAJOR);
500         vc_class = class_create(THIS_MODULE, "vc");
501
502         device_create_drvdata(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
503         device_create_drvdata(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
504         return 0;
505 }