root/fs/devices.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_blkfops
  2. get_chrfops
  3. register_chrdev
  4. register_blkdev
  5. unregister_chrdev
  6. unregister_blkdev
  7. blkdev_open
  8. chrdev_open

   1 /*
   2  *  linux/fs/devices.c
   3  *
   4  * (C) 1993 Matthias Urlichs -- collected common code and tables.
   5  * 
   6  *  Copyright (C) 1991, 1992  Linus Torvalds
   7  */
   8 
   9 #include <linux/fs.h>
  10 #include <linux/major.h>
  11 #include <linux/string.h>
  12 #include <linux/sched.h>
  13 #include <linux/ext_fs.h>
  14 #include <linux/tty.h>
  15 #include <linux/stat.h>
  16 #include <linux/fcntl.h>
  17 #include <linux/errno.h>
  18 
  19 struct device_struct {
  20         const char * name;
  21         struct file_operations * fops;
  22 };
  23 
  24 static struct device_struct chrdevs[MAX_CHRDEV] = {
  25         { NULL, NULL },
  26 };
  27 
  28 static struct device_struct blkdevs[MAX_BLKDEV] = {
  29         { NULL, NULL },
  30 };
  31 
  32 struct file_operations * get_blkfops(unsigned int major)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34         if (major >= MAX_BLKDEV)
  35                 return NULL;
  36         return blkdevs[major].fops;
  37 }
  38 
  39 struct file_operations * get_chrfops(unsigned int major)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         if (major >= MAX_CHRDEV)
  42                 return NULL;
  43         return chrdevs[major].fops;
  44 }
  45 
  46 int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         if (major >= MAX_CHRDEV)
  49                 return -EINVAL;
  50         if (chrdevs[major].fops)
  51                 return -EBUSY;
  52         chrdevs[major].name = name;
  53         chrdevs[major].fops = fops;
  54         return 0;
  55 }
  56 
  57 int register_blkdev(unsigned int major, const char * name, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         if (major >= MAX_BLKDEV)
  60                 return -EINVAL;
  61         if (blkdevs[major].fops)
  62                 return -EBUSY;
  63         blkdevs[major].name = name;
  64         blkdevs[major].fops = fops;
  65         return 0;
  66 }
  67 
  68 int unregister_chrdev(unsigned int major, const char * name)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         if (major >= MAX_CHRDEV)
  71                 return -EINVAL;
  72         if (!chrdevs[major].fops)
  73                 return -EINVAL;
  74         if (strcmp(chrdevs[major].name, name))
  75                 return -EINVAL;
  76         chrdevs[major].name = NULL;
  77         chrdevs[major].fops = NULL;
  78         return 0;
  79 }
  80 
  81 int unregister_blkdev(unsigned int major, const char * name)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83         if (major >= MAX_BLKDEV)
  84                 return -EINVAL;
  85         if (!blkdevs[major].fops)
  86                 return -EINVAL;
  87         if (strcmp(blkdevs[major].name, name))
  88                 return -EINVAL;
  89         blkdevs[major].name = NULL;
  90         blkdevs[major].fops = NULL;
  91         return 0;
  92 }
  93 
  94 /*
  95  * Called every time a block special file is opened
  96  */
  97 int blkdev_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         int i;
 100 
 101         i = MAJOR(inode->i_rdev);
 102         if (i >= MAX_BLKDEV || !blkdevs[i].fops)
 103                 return -ENODEV;
 104         filp->f_op = blkdevs[i].fops;
 105         if (filp->f_op->open)
 106                 return filp->f_op->open(inode,filp);
 107         return 0;
 108 }       
 109 
 110 /*
 111  * Dummy default file-operations: the only thing this does
 112  * is contain the open that then fills in the correct operations
 113  * depending on the special file...
 114  */
 115 struct file_operations def_blk_fops = {
 116         NULL,           /* lseek */
 117         NULL,           /* read */
 118         NULL,           /* write */
 119         NULL,           /* readdir */
 120         NULL,           /* select */
 121         NULL,           /* ioctl */
 122         NULL,           /* mmap */
 123         blkdev_open,    /* open */
 124         NULL,           /* release */
 125 };
 126 
 127 struct inode_operations blkdev_inode_operations = {
 128         &def_blk_fops,          /* default file operations */
 129         NULL,                   /* create */
 130         NULL,                   /* lookup */
 131         NULL,                   /* link */
 132         NULL,                   /* unlink */
 133         NULL,                   /* symlink */
 134         NULL,                   /* mkdir */
 135         NULL,                   /* rmdir */
 136         NULL,                   /* mknod */
 137         NULL,                   /* rename */
 138         NULL,                   /* readlink */
 139         NULL,                   /* follow_link */
 140         NULL,                   /* bmap */
 141         NULL,                   /* truncate */
 142         NULL                    /* permission */
 143 };
 144 
 145 /*
 146  * Called every time a character special file is opened
 147  */
 148 int chrdev_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150         int i;
 151 
 152         i = MAJOR(inode->i_rdev);
 153         if (i >= MAX_CHRDEV || !chrdevs[i].fops)
 154                 return -ENODEV;
 155         filp->f_op = chrdevs[i].fops;
 156         if (filp->f_op->open)
 157                 return filp->f_op->open(inode,filp);
 158         return 0;
 159 }
 160 
 161 /*
 162  * Dummy default file-operations: the only thing this does
 163  * is contain the open that then fills in the correct operations
 164  * depending on the special file...
 165  */
 166 struct file_operations def_chr_fops = {
 167         NULL,           /* lseek */
 168         NULL,           /* read */
 169         NULL,           /* write */
 170         NULL,           /* readdir */
 171         NULL,           /* select */
 172         NULL,           /* ioctl */
 173         NULL,           /* mmap */
 174         chrdev_open,    /* open */
 175         NULL,           /* release */
 176 };
 177 
 178 struct inode_operations chrdev_inode_operations = {
 179         &def_chr_fops,          /* default file operations */
 180         NULL,                   /* create */
 181         NULL,                   /* lookup */
 182         NULL,                   /* link */
 183         NULL,                   /* unlink */
 184         NULL,                   /* symlink */
 185         NULL,                   /* mkdir */
 186         NULL,                   /* rmdir */
 187         NULL,                   /* mknod */
 188         NULL,                   /* rename */
 189         NULL,                   /* readlink */
 190         NULL,                   /* follow_link */
 191         NULL,                   /* bmap */
 192         NULL,                   /* truncate */
 193         NULL                    /* permission */
 194 };

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