博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux驱动之自动创建设备节点
阅读量:2436 次
发布时间:2019-05-10

本文共 2487 字,大约阅读时间需要 8 分钟。

利用cat /proc/devices查看申请到的设备名,设备号。

创建设备节点
1.使用mknod手工创建:mknod filename type major minor
2.自动创建设备节点:利用udev(mdev)来实现设备文件的自动创建,首先应保证支持udev(mdev),由busybox配置。

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

内核中定义的struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了class_create(…)函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建好了这个类,再调用 device_create(…)函数来在/dev目录下创建相应的设备节点。
这样,加载模块的时候,用户空间中的udev会自动响应 device_create()函数,去/sysfs下寻找对应的类从而创建设备节点。

下面是两个函数的解析:

1、class_create(…) 函数
功能:创建一个类;
内核源码:

#define class_create(owner, name)       \({                      \    static struct lock_class_key __key; \    __class_create(owner, name, &__key);    \})

owner:THIS_MODULE

name : 类名字
2.设备类的销毁函数

void class_destroy(struct class *cls){    if ((cls == NULL) || (IS_ERR(cls)))        return;    class_unregister(cls);}

2.device_create(…) 函数

struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, …)
{
va_list vargs;
struct device *dev;

va_start(vargs, fmt);

dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
va_end(vargs);
return dev;
}
功能:创建一个字符设备文件
参数:
struct class *class :类
struct device *parent:NULL
dev_t devt :设备号
void *drvdata :null、
const char *fmt :名字
返回:
struct device *

代码示例

#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);

转载地址:http://bxomb.baihongyu.com/

你可能感兴趣的文章
麒麟信安面向场景化创新,赋能openEuler商业验证
查看>>
3 年培养 10 万“码农”,郑州推出“码农计划”
查看>>
去年我年薪 30W,今年我一天做 3 顿饭
查看>>
入职大厂,我容易吗?
查看>>
漫画:什么是加密算法?
查看>>
漫画 | 一个前端渣渣的成功逆袭
查看>>
与吴恩达并肩战斗,她是 AI 界的女超人!|人物志
查看>>
微信手机 WeOS 的可行性到底有多大?
查看>>
C++ 程序员到高级架构师,必须经历的三个阶段
查看>>
程序媛到最后,拼的到底是什么?
查看>>
技术引路:机器学习仍大有可为,但方向在哪里?
查看>>
漫画:如何给女朋友解释什么是编译与反编译
查看>>
刷屏了!这篇 Python 学习贴,90% 的程序员都用的上!
查看>>
程序员爬取 3 万条评论,《长安十二时辰》槽点大揭秘!
查看>>
一年参加一次就够,全新升级的 AI 开发者大会议程出炉!
查看>>
基于 XDanmuku 的 Android 性能优化实战
查看>>
基于嵌入式操作系统的物联网安全
查看>>
移动周刊第 186 期:移动 App 客户端性能优化、iOS 开源库源码解析
查看>>
包学会之浅入浅出 Vue.js:开学篇
查看>>
移动周刊第 187 期:App 模块化实战经验总结
查看>>