linux chrdev 字符设备 注册 和 卸载

linux 2.6 字符设别 
 
字符设备的注册过程:
 
1 , 注册设别号:
 

       register_chrdev_region(dev_t dev, unsigned count, const char *name)

       alloc_chrdev_region(dev_t dev, unsigned baseminor, unsigned count,  const char *name);

这两个函数都是用来注册一个设备号的 第一个用于注册一个已经确定的设备号, 而第二个是动态申请一个设备号, 这样就不会造成设备号重复的冲突。

 

       下面介绍一个用到的结构体吧

      struct cdev {
        struct kobject kobj;
        struct module *owner;   //所属模块
        const struct file_operations *ops;  
                //文件操作结构,在写驱动时,其结构体内的大部分函数要被实现
        struct list_head list;
        dev_t dev;          //设备号,int 类型,高12位为主设备号,低20位为次设备号
        unsigned int count;
};
可以使用如下宏调用来获得主、次设备号:
MAJOR(dev_t dev)
MINOR(dev_t dev)
MKDEV(int major,int minor) //通过主次设备号来生成dev_t
以上宏调用在内核源码中如此定义:

#define MINORBITS       20
#define MINORMASK       ((1U << MINORBITS) - 1)
        //(1<<20 -1) 此操作后,MINORMASK宏的低20位为1,高12位为0
#define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))

      struct file_operations fops;这个结构体主要 就是对设备操作了

由于成员太多了 这里就不详细介绍了;

     my_cdev = alloc_cdev();  //动态申请一个cdev存贮空间

     cdev_put(struct cdev *p);//释放一个cdev

     cdev_add(struct cdev *, dev_t unsigned);//注销设备,通过发生在驱动模块的加载函数中

     cdev_del(struct cdev *)//注销设备,通常发生在模块的卸载函数中

    unsigned_chrdev_region(dev_t dev, unsigned count );

 

      1 #include <linux/init.h>
      2 #include <linux/module.h>
      3 #include <linux/cdev.h>
      4 #include <linux/types.h>
      5 #include <linux/kdev_t.h>
      6 #include <linux/fs.h>
      7 MODULE_LICENSE("Dual BSD/GPL");
      8 static struct file_operations fops = {NULL};
      9 static struct cdev *my_cdev  = {NULL};
     10 static dev_t dev;

     11 static int testdev_init(void)
     12
     13 {
     14           static dev_t major_no;
     16         int res;
     17
     18         alloc_chrdev_region(major_no, 1,1,"cdev_login" );
     19
     20         if( major_no < 0)
     21         {
     22                 printk("the registerchar dev region is error/n");
     23                 return -1;
     24         }
     25
     26         dev = MKDEV(major_no, 1);
     27         my_cdev = cdev_alloc();
     28
     29         cdev_init(my_cdev,&fops);
     30         my_cdev->owner = THIS_MODULE;
     31
     32         res = cdev_add(my_cdev, dev, 1);
     33         if( res )
     34                 printk("the my_cdev add is error/n");
     35         printk("the cdev register success/n");
     36         return 0;
     37 }
     38
     39 static int testdev_exit(void)
     40 {
     41         cdev_del(my_cdev);

                 unsigned_chrdev_region(dev, 1);
     42 }
     43 module_init(testdev_init);
     44 module_exit(testdev_exit);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值