嵌入式学习笔记(七)- 生成设备节点

生成设备节点

杂项设备(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;
}`


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值