Linux驱动开发--自动创建设备文件节点

函数原型:

#define class_create(owner, name)		\
({						\
	static struct lock_class_key __key;	\
	__class_create(owner, name, &__key);	\
})
struct device *device_create(struct class *class, struct device *parent,
			     dev_t devt, void *drvdata, const char *fmt, ...)
{
	va_list vargs;
	struct device *dev;

	va_start(vargs, fmt);
	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
	va_end(vargs);
	return dev;
}

void device_destroy(struct class *class, dev_t devt) {  struct device *dev;

 dev = class_find_device(class, NULL, &devt, __match_devt);  if (dev) {   put_device(dev);   device_unregister(dev);  } }

void class_destroy(struct class *cls)
{
	if ((cls == NULL) || (IS_ERR(cls)))
		return;

	class_unregister(cls);
}

实例:

在__init函数中,添加以下代码:

my_class= class_create(THIS_MODULE, "my_class");

if(IS_ERR(my_class)) 
{
       printk("Err: failed increating class.\n");

       return -1; 

 }
 device_create( my_class, NULL, devno,NULL, DEV_NAME);//DEV_NAME=设备名;  

在__exit函数中,添加以下代码(在unregister_chrdev_region前):

device_destroy(virtual_disk_class, MKDEV(VirtualDisk_major, 0));
class_destroy(virtual_disk_class);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值