Linux内核使用misc_register函数注册一个混杂设备,使用misc_deregister移除一个混杂设备。注册成功后,linux内核为自动为该设备创建设备节点,在/dev/下会产生相应的节点。
HAL层
fd = open("/dev/shf",READONY);
open的路径跟kernel中路径一样,就会走到kernel对应的系统调用中了
kernel端
struct file_operations shf_fops = {
.owner = THIS_MODULE,
.open= shf_open,
.release = shf_release,
.unlocked_ioctl = shf_unlocked_ioctrl,
}
struct miscdevice shf_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "shf", //节点名字
.fops = &shf_fops,
};
misc_regiseter(&shf_device); //会在/dev下创建shf节点,即/dev/shf
顺序
misc_register(miscdevice)->声明一个misc device,且其中包含fops->声明一个fops,且里面包含open,read,write,unlocked_ioctl的实现
参考博客:
https://www.cnblogs.com/snowdrop/articles/8678389.html