home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / devices.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  4.3 KB  |  199 lines

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