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,
     /* [previous][next][first][last][top][bottom][index][help] */
  87         unsigned long addr, size_t len, int prot, unsigned long off)
  88 {
  89         struct vm_area_struct * mpnt;
  90 
  91         if (off & 0xfff || off + len < off)
  92                 return -ENXIO;
  93         if (remap_page_range(addr, off, len, prot))
  94                 return -EAGAIN;
  95 /* try to create a dummy vmm-structure so that the rest of the kernel knows we are here */
  96         mpnt = (struct vm_area_struct * ) kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  97         if (!mpnt)
  98                 return 0;
  99 
 100         mpnt->vm_task = current;
 101         mpnt->vm_start = addr;
 102         mpnt->vm_end = addr + len;
 103         mpnt->vm_page_prot = prot;
 104         mpnt->vm_share = NULL;
 105         mpnt->vm_inode = inode;
 106         inode->i_count++;
 107         mpnt->vm_offset = off;
 108         mpnt->vm_ops = NULL;
 109         insert_vm_struct(current, mpnt);
 110         merge_segments(current->mmap, NULL, NULL);
 111         return 0;
 112 }
 113 
 114 static int read_kmem(struct inode *inode, struct file *file, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         int read1, read2;
 117 
 118         read1 = read_mem(inode, file, buf, count);
 119         if (read1 < 0)
 120                 return read1;
 121         read2 = vread(buf + read1, (char *) file->f_pos, count - read1);
 122         if (read2 < 0)
 123                 return read2;
 124         file->f_pos += read2;
 125         return read1 + read2;
 126 }
 127 
 128 static int read_port(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         unsigned int i = file->f_pos;
 131         char * tmp = buf;
 132 
 133         while (count-- > 0 && i < 65536) {
 134                 put_fs_byte(inb(i),tmp);
 135                 i++;
 136                 tmp++;
 137         }
 138         file->f_pos = i;
 139         return tmp-buf;
 140 }
 141 
 142 static int write_port(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144         unsigned int i = file->f_pos;
 145         char * tmp = buf;
 146 
 147         while (count-- > 0 && i < 65536) {
 148                 outb(get_fs_byte(tmp),i);
 149                 i++;
 150                 tmp++;
 151         }
 152         file->f_pos = i;
 153         return tmp-buf;
 154 }
 155 
 156 static int read_null(struct inode * node,struct file * file,char * buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         return 0;
 159 }
 160 
 161 static int write_null(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163         return count;
 164 }
 165 
 166 static int read_zero(struct inode * node,struct file * file,char * buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         int left;
 169 
 170         for (left = count; left > 0; left--) {
 171                 put_fs_byte(0,buf);
 172                 buf++;
 173         }
 174         return count;
 175 }
 176 
 177 static int mmap_zero(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 178         unsigned long addr, size_t len, int prot, unsigned long off)
 179 {
 180         struct vm_area_struct *mpnt;
 181 
 182         if (prot & PAGE_RW)
 183                 return -EINVAL;
 184         if (zeromap_page_range(addr, len, prot))
 185                 return -EAGAIN;
 186         /*
 187          * try to create a dummy vmm-structure so that the
 188          * rest of the kernel knows we are here
 189          */
 190         mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 191         if (!mpnt)
 192                 return 0;
 193 
 194         mpnt->vm_task = current;
 195         mpnt->vm_start = addr;
 196         mpnt->vm_end = addr + len;
 197         mpnt->vm_page_prot = prot;
 198         mpnt->vm_share = NULL;
 199         mpnt->vm_inode = NULL;
 200         mpnt->vm_offset = off;
 201         mpnt->vm_ops = NULL;
 202         insert_vm_struct(current, mpnt);
 203         merge_segments(current->mmap, ignoff_mergep, inode);
 204         return 0;
 205 }
 206 
 207 static int read_full(struct inode * node,struct file * file,char * buf,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 208 {
 209         return count;
 210 }
 211 
 212 static int write_full(struct inode * inode,struct file * file,char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214         return -ENOSPC;
 215 }
 216 
 217 /*
 218  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you can fopen()
 219  * both devices with "a" now.  This was previously impossible.  SRB.
 220  */
 221 
 222 static int null_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 223 {
 224         return file->f_pos=0;
 225 }
 226 /*
 227  * The memory devices use the full 32 bits of the offset, and so we cannot
 228  * check against negative addresses: they are ok. The return value is weird,
 229  * though, in that case (0).
 230  *
 231  * also note that seeking relative to the "end of file" isn't supported:
 232  * it has no meaning, so it returns -EINVAL.
 233  */
 234 static int memory_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236         switch (orig) {
 237                 case 0:
 238                         file->f_pos = offset;
 239                         return file->f_pos;
 240                 case 1:
 241                         file->f_pos += offset;
 242                         return file->f_pos;
 243                 default:
 244                         return -EINVAL;
 245         }
 246         if (file->f_pos < 0)
 247                 return 0;
 248         return file->f_pos;
 249 }
 250 
 251 #define write_kmem      write_mem
 252 #define mmap_kmem       mmap_mem
 253 #define zero_lseek      null_lseek
 254 #define write_zero      write_null
 255 
 256 static struct file_operations ram_fops = {
 257         memory_lseek,
 258         read_ram,
 259         write_ram,
 260         NULL,           /* ram_readdir */
 261         NULL,           /* ram_select */
 262         NULL,           /* ram_ioctl */
 263         NULL,           /* ram_mmap */
 264         NULL,           /* no special open code */
 265         NULL,           /* no special release code */
 266         NULL            /* fsync */
 267 };
 268 
 269 static struct file_operations mem_fops = {
 270         memory_lseek,
 271         read_mem,
 272         write_mem,
 273         NULL,           /* mem_readdir */
 274         NULL,           /* mem_select */
 275         NULL,           /* mem_ioctl */
 276         mmap_mem,
 277         NULL,           /* no special open code */
 278         NULL,           /* no special release code */
 279         NULL            /* fsync */
 280 };
 281 
 282 static struct file_operations kmem_fops = {
 283         memory_lseek,
 284         read_kmem,
 285         write_kmem,
 286         NULL,           /* kmem_readdir */
 287         NULL,           /* kmem_select */
 288         NULL,           /* kmem_ioctl */
 289         mmap_kmem,
 290         NULL,           /* no special open code */
 291         NULL,           /* no special release code */
 292         NULL            /* fsync */
 293 };
 294 
 295 static struct file_operations null_fops = {
 296         null_lseek,
 297         read_null,
 298         write_null,
 299         NULL,           /* null_readdir */
 300         NULL,           /* null_select */
 301         NULL,           /* null_ioctl */
 302         NULL,           /* null_mmap */
 303         NULL,           /* no special open code */
 304         NULL,           /* no special release code */
 305         NULL            /* fsync */
 306 };
 307 
 308 static struct file_operations port_fops = {
 309         memory_lseek,
 310         read_port,
 311         write_port,
 312         NULL,           /* port_readdir */
 313         NULL,           /* port_select */
 314         NULL,           /* port_ioctl */
 315         NULL,           /* port_mmap */
 316         NULL,           /* no special open code */
 317         NULL,           /* no special release code */
 318         NULL            /* fsync */
 319 };
 320 
 321 static struct file_operations zero_fops = {
 322         zero_lseek,
 323         read_zero,
 324         write_zero,
 325         NULL,           /* zero_readdir */
 326         NULL,           /* zero_select */
 327         NULL,           /* zero_ioctl */
 328         mmap_zero,
 329         NULL,           /* no special open code */
 330         NULL            /* no special release code */
 331 };
 332 
 333 static struct file_operations full_fops = {
 334         memory_lseek,
 335         read_full,
 336         write_full,
 337         NULL,           /* full_readdir */
 338         NULL,           /* full_select */
 339         NULL,           /* full_ioctl */        
 340         NULL,           /* full_mmap */
 341         NULL,           /* no special open code */
 342         NULL            /* no special release code */
 343 };
 344 
 345 static int memory_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347         switch (MINOR(inode->i_rdev)) {
 348                 case 0:
 349                         filp->f_op = &ram_fops;
 350                         break;
 351                 case 1:
 352                         filp->f_op = &mem_fops;
 353                         break;
 354                 case 2:
 355                         filp->f_op = &kmem_fops;
 356                         break;
 357                 case 3:
 358                         filp->f_op = &null_fops;
 359                         break;
 360                 case 4:
 361                         filp->f_op = &port_fops;
 362                         break;
 363                 case 5:
 364                         filp->f_op = &zero_fops;
 365                         break;
 366                 case 7:
 367                         filp->f_op = &full_fops;
 368                         break;
 369                 default:
 370                         return -ENODEV;
 371         }
 372         if (filp->f_op && filp->f_op->open)
 373                 return filp->f_op->open(inode,filp);
 374         return 0;
 375 }
 376 
 377 static struct file_operations memory_fops = {
 378         NULL,           /* lseek */
 379         NULL,           /* read */
 380         NULL,           /* write */
 381         NULL,           /* readdir */
 382         NULL,           /* select */
 383         NULL,           /* ioctl */
 384         NULL,           /* mmap */
 385         memory_open,    /* just a selector for the real open */
 386         NULL,           /* release */
 387         NULL            /* fsync */
 388 };
 389 
 390 long chr_dev_init(long mem_start, long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
 391 {
 392         if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
 393                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
 394         mem_start = tty_init(mem_start);
 395 #ifdef CONFIG_PRINTER
 396         mem_start = lp_init(mem_start);
 397 #endif
 398 #if defined (CONFIG_BUSMOUSE) || defined (CONFIG_82C710_MOUSE) || \
 399     defined (CONFIG_PSMOUSE) || defined (CONFIG_MS_BUSMOUSE) || \
 400     defined (CONFIG_ATIXL_BUSMOUSE)
 401         mem_start = mouse_init(mem_start);
 402 #endif
 403 #ifdef CONFIG_SOUND
 404         mem_start = soundcard_init(mem_start);
 405 #endif
 406 #if CONFIG_TAPE_QIC02
 407         mem_start = tape_qic02_init(mem_start);
 408 #endif
 409         return mem_start;
 410 }

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