容桂销售型网站建设,友情链接互换网站,蓬莱网站建设,图片网站 seo链接#xff1a;https://pan.baidu.com/s/1V0E9IHSoLbpiWJsncmFgdA?pwd1688 提取码#xff1a;1688 驱动程序编写好后#xff0c;还需要创建设备节点#xff0c;有两种方式#xff0c;一是通过mknod命令去手动创建#xff0c;例如#xff1a;mknod /dev/hello c 250 0https://pan.baidu.com/s/1V0E9IHSoLbpiWJsncmFgdA?pwd1688 提取码1688 驱动程序编写好后还需要创建设备节点有两种方式一是通过mknod命令去手动创建例如mknod /dev/hello c 250 0/dev/hello为设备节点名字c代表字符设备250和0代表它的主次设备号。二是使用udev或mdev来实现自动创建设备节点。使用mknod手动创建设备节点不够灵活如果是动态分配的设备号怎么办难道每次加载驱动后去查看/proc/devices文件中查看它的主设备号要是产品发布时怎么办显然不太现实
利用udev(mdev)来实现设备节点的自动创建udev(mdev)存在于应用层。
包含头文件
#includelinux/device.h
新建一个class结构体指针
static struct class *my_class;
在初始化函数中调用class_create()函数创建一个类参数分别为模块所有者和class结构name字段在/sys/class/下体现。
my_class class_create(THIS_MODULE, my_class);
if(IS_ERR(my_class))
{ printk(Err: failed increating class.\n); return -1; }
调用device_create()函数创建设备节点参数分别为所从属类这个设备的父设备没有就制定NULL设备号设备的私有数据最后一组参数指定设备节点名比如这里的名为hello。函数原型
extern struct device *device_create(struct class *cls, structdevice *parent, dev_t devt, void *drvdata,const char *fmt, ...)
具体实现在drivers/base/core.c中
程序中使用
device_create(my_class,NULL,dev_n,NULL,%s,hello);
或者
device_create(my_class,NULL,dev_n,NULL,hello);
设备卸载删除类和设备节点
device_destroy(my_class,dev_n);
class_destroy(my_class);