root/kernel/chr_drv/mem.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_ram
  2. write_ram
  3. read_core
  4. read_mem
  5. write_mem
  6. mmap_mem
  7. read_port
  8. write_port
  9. read_null
  10. write_null
  11. read_zero
  12. write_zero
  13. mmap_zero
  14. memory_lseek
  15. memory_open
  16. chr_dev_init

   1 /*
   2  *  linux/kernel/chr_drv/mem.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/types.h>
   8 #include <linux/errno.h>
   9 #include <linux/sched.h>
  10 #include <linux/kernel.h>
  11 #include <linux/tty.h>
  12 #include <linux/mouse.h>
  13 #include <linux/tpqic02.h>
  14 
  15 #include <linux/user.h>
  16 #include <linux/a.out.h>
  17 #include <linux/string.h>
  18 
  19 #include <asm/segment.h>
  20 #include <asm/io.h>
  21 
  22 extern long soundcard_init(long mem_start);
  23 
  24 static int read_ram(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         return -EIO;
  27 }
  28 
  29 static int write_ram(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  30 {
  31         return -EIO;
  32 }
  33 
  34 static int read_core(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36         unsigned long p = file->f_pos;
  37         int read;
  38         int count1;
  39         char * pnt;
  40         struct user dump;
  41 
  42         memset(&dump, 0, sizeof(struct user));
  43         dump.magic = CMAGIC;
  44         dump.u_dsize = high_memory >> 12;
  45 
  46         if (count < 0)
  47                 return -EINVAL;
  48         if (p >= high_memory)
  49                 return 0;
  50         if (count > high_memory - p)
  51                 count = high_memory - p;
  52         read = 0;
  53 
  54         if (p < sizeof(struct user) && count > 0) {
  55                 count1 = count;
  56                 if (p + count1 > sizeof(struct user))
  57                         count1 = sizeof(struct user)-p;
  58                 pnt = (char *) &dump + p;
  59                 memcpy_tofs(buf,(void *) pnt, count1);
  60                 buf += count1;
  61                 p += count1;
  62                 count -= count1;
  63                 read += count1;
  64         }
  65 
  66         while (p < (4096 + 4096) && count > 0) {
  67                 put_fs_byte(0,buf);
  68                 buf++;
  69                 p++;
  70                 count--;
  71                 read++;
  72         }
  73         memcpy_tofs(buf,(void *) (p - 4096),count);
  74         read += count;
  75         file->f_pos += read;
  76         return read;
  77 }
  78 
  79 static int read_mem(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81         unsigned long p = file->f_pos;
  82         int read;
  83 
  84         if (count < 0)
  85                 return -EINVAL;
  86         if (p >= high_memory)
  87                 return 0;
  88         if (count > high_memory - p)
  89                 count = high_memory - p;
  90         read = 0;
  91         while (p < 4096 && count > 0) {
  92                 put_fs_byte(0,buf);
  93                 buf++;
  94                 p++;
  95                 count--;
  96                 read++;
  97         }
  98         memcpy_tofs(buf,(void *) p,count);
  99         read += count;
 100         file->f_pos += read;
 101         return read;
 102 }
 103 
 104 static int write_mem(struct inode * inode, struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106         unsigned long p = file->f_pos;
 107         int written;
 108 
 109         if (count < 0)
 110                 return -EINVAL;
 111         if (p >= high_memory)
 112                 return 0;
 113         if (count > high_memory - p)
 114                 count = high_memory - p;
 115         written = 0;
 116         while (p < 4096 && count > 0) {
 117                 /* Hmm. Do something? */
 118                 buf++;
 119                 p++;
 120                 count--;
 121                 written++;
 122         }
 123         memcpy_fromfs((void *) p,buf,count);
 124         written += count;
 125         file->f_pos += written;
 126         return count;
 127 }
 128 
 129 static int mmap_mem(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 130         unsigned long addr, size_t len, int prot, unsigned long off)
 131 {
 132         if (off & 0xfff || off + len < off)
 133                 return -ENXIO;
 134 
 135         if (remap_page_range(addr, off, len, prot))
 136                 return -EAGAIN;
 137         
 138         return 0;
 139 }
 140 
 141 static int read_port(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         unsigned int i = file->f_pos;
 144         char * tmp = buf;
 145 
 146         while (count-- > 0 && i < 65536) {
 147                 put_fs_byte(inb(i),tmp);
 148                 i++;
 149                 tmp++;
 150         }
 151         file->f_pos = i;
 152         return tmp-buf;
 153 }
 154 
 155 static int write_port(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         unsigned int i = file->f_pos;
 158         char * tmp = buf;
 159 
 160         while (count-- > 0 && i < 65536) {
 161                 outb(get_fs_byte(tmp),i);
 162                 i++;
 163                 tmp++;
 164         }
 165         file->f_pos = i;
 166         return tmp-buf;
 167 }
 168 
 169 static int read_null(struct inode * node,struct file * file,char * buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171         return 0;
 172 }
 173 
 174 static int write_null(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         return count;
 177 }
 178 
 179 static int read_zero(struct inode * node,struct file * file,char * buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         int left;
 182 
 183         for (left = count; left > 0; left--) {
 184                 put_fs_byte(0,buf);
 185                 buf++;
 186         }
 187         return count;
 188 }
 189 
 190 static int write_zero(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192         return count;
 193 }
 194 
 195 static int mmap_zero(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 196         unsigned long addr, size_t len, int prot, unsigned long off)
 197 {
 198         if (prot & PAGE_RW)
 199                 return -EINVAL;
 200         if (zeromap_page_range(addr, len, prot))
 201                 return -EAGAIN;
 202         return 0;
 203 }
 204 
 205 /*
 206  * The memory devices use the full 32 bits of the offset, and so we cannot
 207  * check against negative addresses: they are ok. The return value is weird,
 208  * though, in that case (0).
 209  *
 210  * also note that seeking relative to the "end of file" isn't supported:
 211  * it has no meaning, so it returns -EINVAL.
 212  */
 213 static int memory_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         switch (orig) {
 216                 case 0:
 217                         file->f_pos = offset;
 218                         return file->f_pos;
 219                 case 1:
 220                         file->f_pos += offset;
 221                         return file->f_pos;
 222                 default:
 223                         return -EINVAL;
 224         }
 225         if (file->f_pos < 0)
 226                 return 0;
 227         return file->f_pos;
 228 }
 229 
 230 #define read_kmem read_mem
 231 #define write_kmem write_mem
 232 #define mmap_kmem mmap_mem
 233 
 234 static struct file_operations ram_fops = {
 235         memory_lseek,
 236         read_ram,
 237         write_ram,
 238         NULL,           /* ram_readdir */
 239         NULL,           /* ram_select */
 240         NULL,           /* ram_ioctl */
 241         NULL,           /* ram_mmap */
 242         NULL,           /* no special open code */
 243         NULL,           /* no special release code */
 244         NULL            /* fsync */
 245 };
 246 
 247 static struct file_operations mem_fops = {
 248         memory_lseek,
 249         read_mem,
 250         write_mem,
 251         NULL,           /* mem_readdir */
 252         NULL,           /* mem_select */
 253         NULL,           /* mem_ioctl */
 254         mmap_mem,
 255         NULL,           /* no special open code */
 256         NULL,           /* no special release code */
 257         NULL            /* fsync */
 258 };
 259 
 260 static struct file_operations kmem_fops = {
 261         memory_lseek,
 262         read_kmem,
 263         write_kmem,
 264         NULL,           /* kmem_readdir */
 265         NULL,           /* kmem_select */
 266         NULL,           /* kmem_ioctl */
 267         mmap_kmem,
 268         NULL,           /* no special open code */
 269         NULL,           /* no special release code */
 270         NULL            /* fsync */
 271 };
 272 
 273 static struct file_operations null_fops = {
 274         memory_lseek,
 275         read_null,
 276         write_null,
 277         NULL,           /* null_readdir */
 278         NULL,           /* null_select */
 279         NULL,           /* null_ioctl */
 280         NULL,           /* null_mmap */
 281         NULL,           /* no special open code */
 282         NULL,           /* no special release code */
 283         NULL            /* fsync */
 284 };
 285 
 286 static struct file_operations port_fops = {
 287         memory_lseek,
 288         read_port,
 289         write_port,
 290         NULL,           /* port_readdir */
 291         NULL,           /* port_select */
 292         NULL,           /* port_ioctl */
 293         NULL,           /* port_mmap */
 294         NULL,           /* no special open code */
 295         NULL,           /* no special release code */
 296         NULL            /* fsync */
 297 };
 298 
 299 static struct file_operations zero_fops = {
 300         memory_lseek,
 301         read_zero,
 302         write_zero,
 303         NULL,           /* zero_readdir */
 304         NULL,           /* zero_select */
 305         NULL,           /* zero_ioctl */
 306         mmap_zero,
 307         NULL,           /* no special open code */
 308         NULL            /* no special release code */
 309 };
 310 
 311 static struct file_operations core_fops = {
 312         memory_lseek,
 313         read_core,
 314         NULL,
 315         NULL,           /* zero_readdir */
 316         NULL,           /* zero_select */
 317         NULL,           /* zero_ioctl */
 318         NULL,           /* zero_mmap */
 319         NULL,           /* no special open code */
 320         NULL,           /* no special release code */
 321         NULL            /* fsync */
 322 };
 323 
 324 static int memory_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         switch (MINOR(inode->i_rdev)) {
 327                 case 0:
 328                         filp->f_op = &ram_fops;
 329                         break;
 330                 case 1:
 331                         filp->f_op = &mem_fops;
 332                         break;
 333                 case 2:
 334                         filp->f_op = &kmem_fops;
 335                         break;
 336                 case 3:
 337                         filp->f_op = &null_fops;
 338                         break;
 339                 case 4:
 340                         filp->f_op = &port_fops;
 341                         break;
 342                 case 5:
 343                         filp->f_op = &zero_fops;
 344                         break;
 345                 case 6:
 346                         filp->f_op = &core_fops;
 347                         break;
 348                 default:
 349                         return -ENODEV;
 350         }
 351         if (filp->f_op && filp->f_op->open)
 352                 return filp->f_op->open(inode,filp);
 353         return 0;
 354 }
 355 
 356 static struct file_operations memory_fops = {
 357         NULL,           /* lseek */
 358         NULL,           /* read */
 359         NULL,           /* write */
 360         NULL,           /* readdir */
 361         NULL,           /* select */
 362         NULL,           /* ioctl */
 363         NULL,           /* mmap */
 364         memory_open,    /* just a selector for the real open */
 365         NULL,           /* release */
 366         NULL            /* fsync */
 367 };
 368 
 369 long chr_dev_init(long mem_start, long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371         if (register_chrdev(1,"mem",&memory_fops))
 372                 printk("unable to get major 1 for memory devs\n");
 373         mem_start = tty_init(mem_start);
 374         mem_start = lp_init(mem_start);
 375         mem_start = mouse_init(mem_start);
 376         mem_start = soundcard_init(mem_start);
 377 #if CONFIG_TAPE_QIC02
 378         mem_start = tape_qic02_init(mem_start);
 379 #endif
 380         return mem_start;
 381 }

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