root/fs/char_dev.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. tty_read
  2. tty_write
  3. inb
  4. get_fs_byte
  5. panic
  6. printk
  7. panic
  8. MINOR

   1 /*
   2  *  linux/fs/char_dev.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #include <errno.h>
   8 #include <sys/types.h>
   9 
  10 #include <linux/sched.h>
  11 #include <linux/kernel.h>
  12 
  13 #include <asm/segment.h>
  14 #include <asm/io.h>
  15 
  16 extern int tty_read(unsigned minor,char * buf,int count);
  17 extern int tty_write(unsigned minor,char * buf,int count);
  18 
  19 typedef (*crw_ptr)(int rw,unsigned minor,char * buf,int count,off_t * pos);
  20 
  21 static int rw_ttyx(int rw,unsigned minor,char * buf,int count,off_t * pos)
  22 {
  23         return ((rw==READ)?tty_read(minor,buf,count):
     /* [previous][next][first][last][top][bottom][index][help] */
  24                 tty_write(minor,buf,count));
     /* [previous][next][first][last][top][bottom][index][help] */
  25 }
  26 
  27 static int rw_tty(int rw,unsigned minor,char * buf,int count, off_t * pos)
  28 {
  29         if (current->tty<0)
  30                 return -EPERM;
  31         return rw_ttyx(rw,current->tty,buf,count,pos);
  32 }
  33 
  34 static int rw_ram(int rw,char * buf, int count, off_t *pos)
  35 {
  36         return -EIO;
  37 }
  38 
  39 static int rw_mem(int rw,char * buf, int count, off_t * pos)
  40 {
  41         return -EIO;
  42 }
  43 
  44 static int rw_kmem(int rw,char * buf, int count, off_t * pos)
  45 {
  46         return -EIO;
  47 }
  48 
  49 static int rw_port(int rw,char * buf, int count, off_t * pos)
  50 {
  51         int i=*pos;
  52 
  53         while (count-->0 && i<65536) {
  54                 if (rw==READ)
  55                         put_fs_byte(inb(i),buf++);
     /* [previous][next][first][last][top][bottom][index][help] */
  56                 else
  57                         outb(get_fs_byte(buf++),i);
     /* [previous][next][first][last][top][bottom][index][help] */
  58                 i++;
  59         }
  60         i -= *pos;
  61         *pos += i;
  62         return i;
  63 }
  64 
  65 static int rw_memory(int rw, unsigned minor, char * buf, int count, off_t * pos)
  66 {
  67         switch(minor) {
  68                 case 0:
  69                         return rw_ram(rw,buf,count,pos);
  70                 case 1:
  71                         return rw_mem(rw,buf,count,pos);
  72                 case 2:
  73                         return rw_kmem(rw,buf,count,pos);
  74                 case 3:
  75                         return (rw==READ)?0:count;      /* rw_null */
  76                 case 4:
  77                         return rw_port(rw,buf,count,pos);
  78                 default:
  79                         return -EIO;
  80         }
  81 }
  82 
  83 #define NRDEVS ((sizeof (crw_table))/(sizeof (crw_ptr)))
  84 
  85 static crw_ptr crw_table[]={
  86         NULL,           /* nodev */
  87         rw_memory,      /* /dev/mem etc */
  88         NULL,           /* /dev/fd */
  89         NULL,           /* /dev/hd */
  90         rw_ttyx,        /* /dev/ttyx */
  91         rw_tty,         /* /dev/tty */
  92         NULL,           /* /dev/lp */
  93         NULL};          /* unnamed pipes */
  94 
  95 int rw_char(int rw,int dev, char * buf, int count, off_t * pos)
  96 {
  97         crw_ptr call_addr;
  98 
  99         if (MAJOR(dev)>=NRDEVS)
 100                 panic("rw_char: dev>NRDEV");
     /* [previous][next][first][last][top][bottom][index][help] */
 101         if (!(call_addr=crw_table[MAJOR(dev)])) {
 102                 printk("dev: %04x\n",dev);
     /* [previous][next][first][last][top][bottom][index][help] */
 103                 panic("Trying to r/w from/to nonexistent character device");
     /* [previous][next][first][last][top][bottom][index][help] */
 104         }
 105         return call_addr(rw,MINOR(dev),buf,count,pos);
     /* [previous][next][first][last][top][bottom][index][help] */
 106 }

/* [previous][next][first][last][top][bottom][index][help] */