字符驱动设备中几个重要的结构体(cdev,file

2025-04-07 07:59:37
推荐回答(1个)
回答1:

1. cdev结构体

[cpp] view plain copy

  • struct cdev {  

  • struct kobject kobj; //内嵌的kobject对象  

  • struct module *owner; //所属模板  

  • const struct file_operations *ops; //文件操作的结构体  

  • struct list_head list;  

  • dev_t dev; //设备号  

  • unsigned int count;  

  • };  

  • 1.1 cdev的相关操作

    [cpp] view plain copy

  • void cdev_init(struct cdev *, const struct file_operations *); //初始化使其和文件操作结构相连接  

  • struct cdev *cdev_alloc(void); //为cdev分配内存  

  • int cdev_add(struct cdev *, dev_t, unsigned); //向内核注册一个设备  

  • void cdev_del(struct cdev *); //从内核删除一个设备  

  • 1.2 设备号的分配

    1.21 主次设备号和dev_t的相互转换

    由dev_t号获取主次设备号

    [cpp] view plain copy

  • MAJOR(dev_t dev)  

  • MINOR(dev_t dev)  

  • 由主次设备号获得dev_t

    [cpp] view plain copy

  • MKDEV(int major,int minor)  

  • 1.22 获取及注销设备号

    [cpp] view plain copy

  • int register_chrdev_region(dev_t from,unsigned count,const char *name); //手动注册  

  • int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name); //由内核分配  

  • void unregion_chrdev_region(dev_t from,unsigned count); //注销设备号