文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux 1.0 内核注解 linux/fs/devices.c

linux 1.0 内核注解 linux/fs/devices.c

时间:2009-05-07  来源:taozhijiangscu

/********************************************
 *Created By: 陶治江
 *Date:       2009年5月2日20:38:08
 ********************************************/
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/ext_fs.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/errno.h>
struct device_struct {
 const char * name;
 struct file_operations * fops;
};
static struct device_struct chrdevs[MAX_CHRDEV] = {  //32
 { NULL, NULL },
};
static struct device_struct blkdevs[MAX_BLKDEV] = {  //32
 { NULL, NULL },
};
struct file_operations * get_blkfops(unsigned int major)
{
 if (major >= MAX_BLKDEV)
  return NULL;
 return blkdevs[major].fops;
}
struct file_operations * get_chrfops(unsigned int major)
{
 if (major >= MAX_CHRDEV)
  return NULL;
 return chrdevs[major].fops;
}
//这里是进行新的设备的注册操作了,提供了主设备号,设备名,操作结构
//作为参数,在系统的设备数组中注册项目
//这真是让人激动人心,同0.11版变化太大了 :-)
int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
{
 if (major >= MAX_CHRDEV) 
  return -EINVAL; 
  
 //是否已经被使用了(即使没有实际的操作,但是本身不能是NULL)
 //而且下面的代码也可以看出,对于一个设备的存在性是用fops检查的
 //而不是使用name检查的~~~
 if (chrdevs[major].fops) 
  return -EBUSY;
  
 //进行注册插入了哦
 chrdevs[major].name = name;
 chrdevs[major].fops = fops;
 return 0;
}
int register_blkdev(unsigned int major, const char * name, struct file_operations *fops)
{
 if (major >= MAX_BLKDEV)
  return -EINVAL;
 if (blkdevs[major].fops)
  return -EBUSY;
 blkdevs[major].name = name;
 blkdevs[major].fops = fops;
 return 0;
}
//注销设备
int unregister_chrdev(unsigned int major, const char * name)
{
 if (major >= MAX_CHRDEV)
  return -EINVAL;
 if (!chrdevs[major].fops)
  return -EINVAL;
 if (strcmp(chrdevs[major].name, name)) //检查设备名是否正确
  return -EINVAL;
 
 chrdevs[major].name = NULL;
 chrdevs[major].fops = NULL;
 return 0;
}
int unregister_blkdev(unsigned int major, const char * name)
{
 if (major >= MAX_BLKDEV)
  return -EINVAL;
 if (!blkdevs[major].fops)
  return -EINVAL;
 if (strcmp(blkdevs[major].name, name))
  return -EINVAL;
 blkdevs[major].name = NULL;
 blkdevs[major].fops = NULL;
 return 0;
}
//块设备文件打开都要调用的函数
int blkdev_open(struct inode * inode, struct file * filp)
{
 int i;
 i = MAJOR(inode->i_rdev);  //获得主设备号
 if (i >= MAX_BLKDEV || !blkdevs[i].fops)
  return -ENODEV;
  
 filp->f_op = blkdevs[i].fops;
 
 if (filp->f_op->open)
  return filp->f_op->open(inode,filp);
 return 0;
//下面的都是操作结构的设置,块设备和字符设备是
//一样的,对于文件操作,提供了打开操作,而节点
//也只提供了默认的文件操作(也就是打开操作),一
//般来说这都是要提供的,而其他的根据设备类型可以
//选择需要的添加
//或许到具体的文件系统时候会更加的清晰吧
struct file_operations def_blk_fops = {
 NULL,  /* lseek */
 NULL,  /* read */
 NULL,  /* write */
 NULL,  /* readdir */
 NULL,  /* select */
 NULL,  /* ioctl */
 NULL,  /* mmap */
 blkdev_open, /* open */
 NULL,  /* release */
};
struct inode_operations blkdev_inode_operations = {
 &def_blk_fops,  /* default file operations */
 NULL,   /* create */
 NULL,   /* lookup */
 NULL,   /* link */
 NULL,   /* unlink */
 NULL,   /* symlink */
 NULL,   /* mkdir */
 NULL,   /* rmdir */
 NULL,   /* mknod */
 NULL,   /* rename */
 NULL,   /* readlink */
 NULL,   /* follow_link */
 NULL,   /* bmap */
 NULL,   /* truncate */
 NULL   /* permission */
};
int chrdev_open(struct inode * inode, struct file * filp)
{
 int i;
 i = MAJOR(inode->i_rdev); //获得主设备号
 
 if (i >= MAX_CHRDEV || !chrdevs[i].fops)
  return -ENODEV;
 filp->f_op = chrdevs[i].fops;
 if (filp->f_op->open)
  return filp->f_op->open(inode,filp);
 return 0;
}
struct file_operations def_chr_fops = {
 NULL,  /* lseek */
 NULL,  /* read */
 NULL,  /* write */
 NULL,  /* readdir */
 NULL,  /* select */
 NULL,  /* ioctl */
 NULL,  /* mmap */
 chrdev_open, /* open */
 NULL,  /* release */
};
struct inode_operations chrdev_inode_operations = {
 &def_chr_fops,  /* default file operations */
 NULL,   /* create */
 NULL,   /* lookup */
 NULL,   /* link */
 NULL,   /* unlink */
 NULL,   /* symlink */
 NULL,   /* mkdir */
 NULL,   /* rmdir */
 NULL,   /* mknod */
 NULL,   /* rename */
 NULL,   /* readlink */
 NULL,   /* follow_link */
 NULL,   /* bmap */
 NULL,   /* truncate */
 NULL   /* permission */
};
  文档地址:http://blogimg.chinaunix.net/blog/upfile2/090503233148.pdf
相关阅读 更多 +
排行榜 更多 +
味子夫

味子夫

购物比价 下载
恩猫

恩猫

购物比价 下载
街头纷争

街头纷争

动作格斗 下载