而不是进入 什么是类,或 什么是设备(我不是这方面的专家) Linux内核),我将解决这个问题。
创建字符设备后,您希望能够从用户空间访问它。为此,您需要在/dev 下添加一个设备节点。您可以通过两种方式做到这一点。
使用mknod手动添加设备节点(旧)
mknod /dev/<name> c <major> <minor>
或
使用udev
这就是class_create 和device_create 或class_device_create(旧)的用武之地。
要从您的内核模块通知udev,您首先使用创建一个虚拟设备类
struct class * class_create(owner, name)
现在,名称将出现在/sys/class/<name>。
然后,创建一个设备并将其注册到 sysfs。
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
现在,设备名称将出现在/sys/devices/virtual/<class name>/<device name> 和/dev/<device name>
不清楚您对 /proc 条目的要求是什么。
在你的模块被加载后,它会出现在/proc/modules 中(做一个cat /proc/modules 来查看它)。并且,在分配设备编号后,使用
int register_chrdev_region(dev_t first, unsigned int count, char *name)
,名称将出现在/proc/devices 中(执行cat /proc/devices 即可查看)。
另外,请检查这些函数的内核源代码,因为它们很好地描述了它们在 cmets 中的作用。
好老的LDD3 没有提供这些机制,但它是一个非常好的来源。