root/drivers/char/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_mem
  4. write_mem
  5. mmap_mem
  6. read_kmem
  7. read_port
  8. write_port
  9. read_null
  10. write_null
  11. read_zero
  12. mmap_zero
  13. read_full
  14. write_full
  15. null_lseek
  16. memory_lseek
  17. memory_open
  18. chr_dev_init

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

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