linux 驱动中的dev ops,Linux 自动在 /dev 下创建设备文件

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

insmod *.ko 之后,部分驱动需要执行 mknod 才能正常工作,区别如下

现象

针对驱动实现不同,使用不同,主要在于是否需要手动创建设备文件mknod /dev/panel0 c 22 0

insmod /lib/modules/ `uname -r` /panel.ko

设备管理

在 Linux 中有手动和自动两种设备管理,一种是 mknod,在 insmod 后 /dev 下必须有对应的设备文件,需要 mknod 来创建;另外一种在驱动中自动创建

利用 udev(mdev) 来实现设备文件的自动创建,首先应保证支持 udev(mdev),由 busybox 配置。

在驱动用加入对 mdev 的支持主要做的就是:在驱动初始化的代码里调用 class_create(...) 为该设备创建一个 class,再为每个设备调用 device_create(...) 创建对应的设备。

内核中定义的 struct class 结构体,顾名思义,一个 struct class 结构体类型变量对应一个类,内核同时提供了 class_create(…) 函数,可以用它来创建一个类,这个类存放于 sysfs 下面,一旦创建好了这个类,再调用 device_create(…) 函数来在 /dev 目录下创建相应的设备节点。

这样,加载模块的时候,用户空间中的 mdev 会自动响应 device_create() 函数,去 /sysfs 下寻找对应的类从而创建设备节点。#include

#include

#include

#include

static int major = 250;

static int minor=0;

static dev_t devno;

static struct class *cls;

static struct device *test_device;

static int hello_open (struct inode *inode, struct file *filep)

{

printk("hello_open n");

return 0;

}

static struct file_operations hello_ops=

{

.open = hello_open,

};

static int hello_init(void)

{

int ret;

printk("hello_init n");

devno = MKDEV(major,minor);

ret = register_chrdev(major,"hello",&hello_ops);

cls = class_create(THIS_MODULE, "myclass");

if(IS_ERR(cls))

{

unregister_chrdev(major,"hello");

return -EBUSY;

}

test_device = device_create(cls,NULL,devno,NULL,"hello");//mknod /dev/hello

if(IS_ERR(test_device))

{

class_destroy(cls);

unregister_chrdev(major,"hello");

return -EBUSY;

}

return 0;

}

static void hello_exit(void)

{

device_destroy(cls,devno);

class_destroy(cls);

unregister_chrdev(major,"hello");

printk("hello_exit n");

}

MODULE_LICENSE("GPL");

module_init(hello_init);

module_exit(hello_exit);

下面可以看几个 class 几个名字的对应关系:

a2a7209be9690947bf41a592d69f7bba.png

不同内核版本区别#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)

# define CLASS_DEV_CREATE(class, devt, device, id) device_create(class, devt, device, NULL, "hello%d", id)

# define CLASS_DEV_DESTROY(class, devt) device_destroy(class, devt)

#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)

# define CLASS_DEV_CREATE(class, devt, device, id) device_create(class, devt, device, NULL, "hello%d", id)

# define CLASS_DEV_DESTROY(class, devt) device_destroy(class, devt)

#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)

# define CLASS_DEV_CREATE(class, devt, device, id) class_device_create(class, devt, device, NULL, "hello%d", id)

# define CLASS_DEV_DESTROY(class, devt) class_device_destroy(class, devt)

#else

# define CLASS_DEV_CREATE(class, devt, device, id) device_create_drvdata(class, devt, device, "hello%d", id)

# define CLASS_DEV_DESTROY(classs, devt) device_destroy(class, devt)

#endif

mdevecho "mdev..."

echo /sbin/mdev > /proc/sys/kernel/hotplug

/sbin/mdev -s

mdev.conf 如下null 0:0 666

zero 0:0 666

urandom 0:0 444

kmem 0:9 000

mem 0:9 640

port 0:9 640

console 0:5 600

ptmx 0:5 660

pty[a-z]. 0:0 660

tty[a-z]. 0:0 660

tty[0-9][0-9] 0:0 660

ttyS[0-9] 0:20 640

sd[a-z][0-9]* 0:6 660 * /etc/hotplug/usb/automount.sh $MDEV $ACTION

fd[0-9]* 0:11 660

hd[a-z]* 0:6 660

Ref

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值