字符杂项设备 misc_register

字符杂项设备 misc_register
0


#i nclude <linux/miscdevice.h>
#i nclude <linux/fs.h>       
#i nclude <linux/module.h>
#i nclude <linux/init.h>

/*  misedevice 结构:
struct miscdevice  {
    int minor; //次设备号,若为 MISC_DYNAMIC_MINOR 自动分配
    const char *name; //设备名
    struct file_operations *fops; //设备操作
    struct list_head list;
    struct device *dev;
    struct class_device *class;
    char devfs_name[64];
};
*/

/*
 使用misc_register,在加载模块时会自动创建设备文件,为主设备号为10的字符设备。
 使用misc_deregister,在卸载模块时会自动删除设备文件
*/

#define MISC_NAME "miscdriver"

static int misc_open(struct inode *inode, struct file *file)
{
 printk("misc open\n");
 return 0;
}

static const struct file_operations misc_fops =
{
    .owner   =   THIS_MODULE,
    .open    =   misc_open,
};

static struct miscdevice misc_dev =
{
 .minor = MISC_DYNAMIC_MINOR,
 .name = MISC_NAME,
 .fops = &misc_fops,
};

static int __init misc_init(void)
{
 int ret;
 ret = misc_register(&misc_dev);
 if (ret)
 {
  printk("misc_register error\n");
  return ret;
 }
 
 return 0;
}

static void __exit misc_exit(void)
{
  misc_deregister(&misc_dev);
}

module_init(misc_init);
module_exit(misc_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Decly");

 

 

 

注册杂项字符设备,该类设备使用同一个主设备号10
杂项字符设备使用的数据结构
struct miscdevice  {
    int minor;
    const char *name;
    struct file_operations *fops;
    struct list_head list;
    struct device *dev;
    struct class_device *class;
    char devfs_name[64];
};

杂项设备(misc device)
在 Linux 内核的include\linux\miscdevice.h文件,要把自己定义的misc device从设备定义在这里。其实是因为这些字符设备不符合预先确定的字符设备范畴,所有这些设备采用主设备号10 ,一起归于misc device,其实misc_register就是用主设备号10调用register_chrdev()的。也就是说,misc设备其实也就是特殊的字符设备。

misc_device是特殊的字符设备。注册驱动程序时采用misc_register函数注册,此函数中会自动创建设备节点,即设备文件。无需mknod指令创建设备文件。因为misc_register()会调用class_device_create()或者device_create()。

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值