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