~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/vc_screen.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.