生成设备节点
杂项设备(10 主设备号)
- 是对字符设备的封装,不好归类的设备也是杂项设备
- 主设备号255 个,封装好了,驱动写起来简单
- 源文件在drivers/char/misc.c 是强制编译的
生成杂项设备节点
- 杂项设备注册的头文件在include/linux/miscdevice.h
- 常用参数
- .minor 设备号
- .name 生成设备节点的名称(与注册设备的platform_driver 中的name 不同)
- .fops 指向一个设备节点文件
- 包含文件结构体的头文件是include/linux/fs.h
- 文件结构体file_operations 参数:
- .owner 一般是THIS_MODULE,
- .open 打开文件的函数
- .realease 关闭文件函数
- .unlocked_ioctl 对GPIO 的操作,应用向底层驱动传值(非必选)
源码
#defind DRIVER_NAME "hello_ctl123";
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("xubaipei")
module_init(hello_init);
module_exit(hello_exit);
static struct file_operation hello_ops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.unlocked_ioctl = hello_ioctl,
};
static struct miscdevice hello_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DRIVER_NAME,
.fops = &hello_ops,
};
static int hello_ioctl (struct file *file ,unsigned int cmd,unsigned long arg)
{
printk("cmd is %d,arg is %d \n",amd,arg);
return 0;
}
static int hello_release(struct inode *inode,struct file * file)
{
printk(KERN_EMERG,"hello_release");
return 0;
}
static int hello_open(struct inode *inode,struct file * file)
{
printk(KERN_EMERG,"hello_open\n");
return 0;
}
static int hello_init(void)
{
misc_register(&hello_dev);
return 0;
}
static int hello_exit(void)
{
misc_deregister(&hello_dev);
return 0;
}`