7、Linux驱动开发:设备-自动创建设备节点

目录

🍅点击这里查看所有博文

  随着自己工作的进行,接触到的技术栈也越来越多。给我一个很直观的感受就是,某一项技术/经验在刚开始接触的时候都记得很清楚。往往过了几个月都会忘记的差不多了,只有经常会用到的东西才有可能真正记下来。存在很多在特殊情况下有一点用处的技巧,用的不多的技巧可能一个星期就忘了。

  想了很久想通过一些手段把这些事情记录下来。也尝试过在书上记笔记,这也只是一时的,书不在手边的时候那些笔记就和没记一样,不是很方便。

  很多时候我们遇到了问题,一般情况下都是选择在搜索引擎检索相关内容,这样来的也更快一点,除非真的找不到才会去选择翻书。后来就想到了写博客,博客作为自己的一个笔记平台倒是挺合适的。随时可以查阅,不用随身携带。

  同时由于写博客是对外的,既然是对外的就不能随便写,任何人都可以看到。经验对于我来说那就只是经验而已,公布出来说不一定我的一些经验可以帮助到其他的人。遇到和我相同问题时可以少走一些弯路。

  既然决定了要写博客,那就只能认真去写。不管写的好不好,尽力就行。千里之行始于足下,一步一个脚印,慢慢来 ,写的多了慢慢也会变好的。权当是记录自己的成长的一个过程,等到以后再往回看时,就会发现自己以前原来这么菜😂。

  本系列博客所述资料均来自互联网资料,并不是本人原创(只有博客是自己写的)。出于热心,本人将自己的所学笔记整理并推出相对应的使用教程,方面其他人学习。为国内的物联网事业发展尽自己的一份绵薄之力,没有为自己谋取私利的想法。若出现侵权现象,请告知本人,本人会立即停止更新,并删除相应的文章和代码。

前言

  在前面两小节中,我们学习到了设备注册。可以将一个设备驱动注册到内核中。设备注册完成后,还需要通过mknod指令在用户空间中手动创建该驱动对应的设备节点。

root@ubuntu:# mknod /dev/hello_test0 c 237 0

  该命令在执行是不会检查参数的合法性。也不会检查设备驱动是否存在。如果系统中所有的驱动都通过该方法创建设备节点,就会出现一个问题。当设备未接入时,就可能会出现很多的设备节点。

  实际上Linux内核为我们提供了一组函数,可以在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点,当然前提条件是用户空间移植了udev

udev

  udev是一个工作在用户空间的工具,它能根据系统中硬件设备的状态动态的更新设备文件,包括设备文件的创建,删除,权限等。这些文件通常都定义在/dev 目录下,但也可以在配置文件中指定。

  当插入新设备—>加入驱动模块—>在sysfs上注册新的数据后,udev会自动创建新的设备节点。udev运行在用户模式中,而并非内核中。

输入图片说明

接口

  内核中定义了struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类。代码中出现的class指的是 设备类(device classes),是对于设备的高级抽象。但 实际上class也是一个结构体,只不过class结构体在声明时是按照类的思想来组织其成员的。

/*** struct class - device classes* @name:	Name of the class.* @owner:	The module owner.* @class_attrs: Default attributes of this class.* @dev_groups:	Default attributes of the devices that belong to the class.* @dev_kobj:	The kobject that represents this class and links it into the hierarchy.* @dev_uevent:	Called when a device is added, removed from this class, or a*		few other things that generate uevents to add the environment*		variables.* @devnode:	Callback to provide the devtmpfs.* @class_release: Called to release this class.* @dev_release: Called to release the device.* @suspend:	Used to put the device to sleep mode, usually to a low power*		state.* @resume:	Used to bring the device from the sleep mode.* @ns_type:	Callbacks so sysfs can detemine namespaces.* @namespace:	Namespace of the device belongs to this class.* @pm:		The default device power management operations of this class.* @p:		The private data of the driver core, no one other than the*		driver core can touch this.** A class is a higher-level view of a device that abstracts out low-level* implementation details. Drivers may see a SCSI disk or an ATA disk, but,* at the class level, they are all simply disks. Classes allow user space* to work with devices based on what they do, rather than how they are* connected or how they work.*/
struct class {const char		*name;struct module		*owner;struct class_attribute		*class_attrs;const struct attribute_group	**dev_groups;struct kobject			*dev_kobj;int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);char *(*devnode)(struct device *dev, umode_t *mode);void (*class_release)(struct class *class);void (*dev_release)(struct device *dev);int (*suspend)(struct device *dev, pm_message_t state);int (*resume)(struct device *dev);const struct kobj_ns_type_operations *ns_type;const void *(*namespace)(struct device *dev);const struct dev_pm_ops *pm;struct subsys_private *p;
};

  内核同时提供了class_create宏。用于动态创建设备的逻辑类,并完成部分字段的初始化,然后将其添加进Linux内核系统中。此函数的执行效果就是在**/sys/class/**目录下创建一个新的文件夹,此文件夹的名字为此函数的第二个输入参数。

  class_create 一共有两个参数,参数 owner 一般为 THIS_MODULE,参数 name 是类名字。返回值是个指向结构体 class 的指针,也就是创建的类。

/* This is a #define to keep the compiler from merging different* instances of the __key variable */
#define class_create(owner, name)		\
({						\static struct lock_class_key __key;	\__class_create(owner, name, &__key);	\
})/*** class_create - create a struct class structure* @owner: pointer to the module that is to "own" this struct class* @name: pointer to a string for the name of this class.* @key: the lock_class_key for this class; used by mutex lock debugging** This is used to create a struct class pointer that can then be used* in calls to device_create().** Returns &struct class pointer on success, or ERR_PTR() on error.** Note, the pointer created here is to be destroyed when finished by* making a call to class_destroy().*/
struct class *__class_create(struct module *owner, const char *name,struct lock_class_key *key)
{struct class *cls;int retval;cls = kzalloc(sizeof(*cls), GFP_KERNEL);if (!cls) {retval = -ENOMEM;goto error;}cls->name = name;cls->owner = owner;cls->class_release = class_create_release;retval = __class_register(cls, key);if (retval)goto error;return cls;
error:kfree(cls);return ERR_PTR(retval);
}

  函数device_create用于动态创建逻辑设备,对新的逻辑设备进行相应初始化,然后将此逻辑设备加入到Linux内核系统的设备驱动程序模型中。

  device_create是个可变参数函数,参数 class 就是设备要创建在哪个类下面。参数 parent 是父设备,一般为 NULL,也就是没有父设备。参数 devt 是设备号。参数 drvdata 是设备可能会使用的一些数据,一般为 NULL。参数 fmt 是设备名字,如果设置 fmt=xxx 的话,就会生成/dev/xxx这个设备文件。返回值就是创建好的设备。

/*** device_create - creates a device and registers it with sysfs* @class: pointer to the struct class that this device should be registered to* @parent: pointer to the parent struct device of this new device, if any* @devt: the dev_t for the char device to be added* @drvdata: the data to be added to the device for callbacks* @fmt: string for the device's name** This function can be used by char device classes.  A struct device* will be created in sysfs, registered to the specified class.** A "dev" file will be created, showing the dev_t for the device, if* the dev_t is not 0,0.* If a pointer to a parent struct device is passed in, the newly created* struct device will be a child of that device in sysfs.* The pointer to the struct device will be returned from the call.* Any further sysfs files that might be required can be created using this* pointer.** Returns &struct device pointer on success, or ERR_PTR() on error.** Note: the struct class passed to this function must have previously* been created with a call to class_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;
}

  该函数会自动地在/sys/devices/virtual目录下创建新的逻辑设备目录。并将其软连接到/sys/class/目录中对应的类下。同时还会在/dev目录下创建与逻辑类对应地设备文件。

root@ubuntu:# ll /sys/class/hellocls/
total 0
lrwxrwxrwx 1 root root 0 Sep 17 06:11 hellodev -> ../../devices/virtual/hellocls/hellodevroot@ubuntu:# ll /dev/hellodev 
crw------- 1 root root 237, 0 Sep 17 06:11 /dev/hellodev

代码实现

  示例代码实现也比较简单,完成设备的注册后。class_create创建一个hellocls的类,该函数最终会在/sys/class目录中创建一个名为hellocls的文件夹。device_create函数将设备驱动存放到hellocls类中,并创建对应的设备文件。

static int hello_init(void)
{int result;	printk("hello_init \n");result = register_chrdev( major, "hello", &hello_ops);if(result < 0){printk("register_chrdev fail \n");return result;}cls = class_create(THIS_MODULE, "hellocls");if (IS_ERR(cls)) {printk(KERN_ERR "class_create() failed for cls\n");result = PTR_ERR(cls);goto out_err_1;}devno = MKDEV(major, minor);	class_dev = device_create(cls, NULL, devno, NULL, "hellodev");if (IS_ERR(class_dev)) {result = PTR_ERR(class_dev);goto out_err_2;}return 0;
out_err_2:class_destroy(cls);
out_err_1:unregister_chrdev(major,"hello");return 	result;
}static void hello_exit(void)
{printk("hello_exit \n");device_destroy(cls, devno);class_destroy(cls);unregister_chrdev(major,"hello");return;
}

实验结果

  测试程序如下,打开/dev/hellodev字符设备。紧接着关闭掉。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{int fd;fd = open("/dev/hellodev",O_RDWR);if(fd<0){perror("open fail \n");return;}printf("open ok \n");close(fd);printf("close ok \n");
}

  加载模块,用户空间编译测试程序。运行测试程序对驱动进行打开和关闭的操作。日志可以看到驱动中的hello_openhello_release都被正常调用。

root@ubuntu:# insmod ./hello.ko
root@ubuntu:# gcc ./test.c 
root@ubuntu:# ./a.out 
open ok 
close ok 
root@ubuntu:# dmesg
[170236.680298] hello_exit()
[170280.990839] hello_init 
[222202.880295] hello_open()
[222202.880418] hello_release()

  进入到系统的类目录,查看dev文件和uevent文件。其中记录的就是驱动模块中注册的设备号。

root@ubuntu:# cd /sys/class//hellocls/hellodev
root@ubuntu:# $ cat dev
237:0
root@ubuntu:# cat uevent 
MAJOR=237
MINOR=0
DEVNAME=hellodev
root@ubuntu:# ll /dev/hellodev 
crw------- 1 root root 237, 0 Sep 17 06:11 /dev/hellodev

  那么本篇博客就到此结束了,这里只是记录了一些我个人的学习笔记,其中存在大量我自己的理解。文中所述不一定是完全正确的,可能有的地方我自己也理解错了。如果有些错的地方,欢迎大家批评指正。如有问题直接在对应的博客评论区指出即可,不需要私聊我。我们交流的内容留下来也有助于其他人查看,说不一定也有其他人遇到了同样的问题呢😂。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/112088.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【力扣刷题】回文链表、环形链表、合并两个有序链表

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaEE 操作系统 Redis 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 刷题篇 一、回文链表1.1 题目描述1.2 思路分…

msvcr120.dll缺失怎么修复,快速修复msvcr120.dll丢失的三个有效方法

随着计算机技术的不断发展&#xff0c;我们在使用软件或游戏时经常会遇到各种错误提示&#xff0c;其中找不到msvcr120.dll就是一种常见的错误。那么&#xff0c;msvcr120.dll是什么&#xff1f;它的作用是什么&#xff1f;如何修复这一错误呢&#xff1f;本文将为您详细介绍几…

7.7亿参数,超越5400亿PaLM!UW谷歌提出「分步蒸馏」,只需80%训练数据|ACL 2023

LLM不实用&#xff0c;小模型蒸馏才是「现实」的大模型应用路线&#xff0c;全面领先微调技术&#xff01;土豪请无视。。。 大型语言模型虽然性能优异&#xff0c;可以用零样本或少样本提示解决新任务&#xff0c;但LLM在实际应用部署时却很不实用&#xff0c;内存利用效率低…

基于Java的汽车维修预约管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

群晖synology DSM 7.2设置钉钉Webhooks通知

现在越来越多的小伙伴都有了自己的Nas系统&#xff0c;为了更加方便的接收Nas的消息&#xff0c;这篇文章带着大家一起配置一个钉钉&#xff08;机器人&#xff09;即时消息通知 首先登录钉钉的开放平台&#xff1a;开发者后台统一登录 - 钉钉统一身份认证 1.创建一个机器人&…

基于蛇优化优化的BP神经网络(分类应用) - 附代码

基于蛇优化优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码 文章目录 基于蛇优化优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码1.鸢尾花iris数据介绍2.数据集整理3.蛇优化优化BP神经网络3.1 BP神经网络参数设置3.2 蛇优化算法应用 4.测试结果&#x…

基于Java的勤工助学管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

CEC2013(MATLAB):白鲨优化算法(White Shark Optimizer,WSO)求解CEC2013(提供MATLAB代码及参考文献)

一、白鲨优化算法原理 白鲨优化算法&#xff08;White Shark Optimizer&#xff0c;WSO&#xff09;由Malik Braik等人于2022年提出&#xff0c;该算法受大白鲨导航和觅食时具有的非凡听觉和嗅觉启发。该算法思路新颖&#xff0c;策略高效。【精选】单目标应用&#xff1a;白鲨…

微信小程序修改van-popup的背景颜色

效果图&#xff1a; van-popup背景颜色渐变 使用深度修改样式不生效&#xff0c;直接在 custom-style里面修改即可&#xff1b; <van-popup position"bottom"custom-style"height:25%;background:linear-gradient(95deg, #F8FCFF -0.03%, #EDF5FF 64.44…

01_introduction_to_diffusers_CN

&#x1f917; Diffusers 介绍 来源&#xff1a;https://github.com/huggingface/diffusion-models-class/blob/main/unit1/01_introduction_to_diffusers.ipynb 预备知识 在进入 Notebook 之前&#xff0c;你需要: &#x1f4d6; 阅读第一单元的材料&#x1f917; 在 Hugg…

第十四章lambda表达式与流处理

14.1 Iambda表达式 Iambda表达式简介 lambda表达式可以用非常少的代码实现抽象方法。 lambda表达式不能独立执行&#xff0c;因此必须实现函数式接口&#xff0c;并且会返回一个函数式接口的对象。 lambdab表达式的语法非常特殊 语法格式&#xff1a; &#xff08;&#xff0…

虚拟机如何联网【NAT】

查看VMWARE的IP地址 #进入root用户 su -#更改虚拟网卡设置界面 vi /etc/sysconfig/network-scripts/ifcfg-ens33 修改ONBOOT为yes BOOTPROTO为static IPADDR为前面的网段 192.168.211.xx (xx为自己设置的&#xff0c;可以随意设置&#xff0c;前面的为前面查看的IP地址的前…

Vue2之防抖_debounce封装函数v-debounce自定义指令(传参/不传)

目录 1、防抖 2、debounce - 封装函数 3、v-debounce 全局自定义指令 1、防抖 推荐文章 &#xff1a; https://blog.csdn.net/weixin_58099903/article/details/119902796 2、debounce - 封装函数 utils / tools.js /*** 函数防抖 是n秒后延迟执行&#xff0c;多用于页面scr…

MATLAB——神经网络参考代码

欢迎关注“电击小子程高兴的MATLAB小屋” %% I. 清空环境变量 clear all clc %% II. 训练集/测试集产生 %% % 1. 导入数据 load spectra_data.mat %% % 2. 随机产生训练集和测试集 temp randperm(size(NIR,1)); %打乱60个样本排序 % 训练集——50个样本 P_train NIR(…

YOLOv8改进实战 | 更换主干网络Backbone之轻量化模型Efficientvit

前言 轻量化网络设计是一种针对移动设备等资源受限环境的深度学习模型设计方法。下面是一些常见的轻量化网络设计方法: 网络剪枝:移除神经网络中冗余的连接和参数,以达到模型压缩和加速的目的。分组卷积:将卷积操作分解为若干个较小的卷积操作,并将它们分别作用于输入的不…

机器学习基础之《回归与聚类算法(3)—线性回归优化:岭回归》

一、什么是岭回归 其实岭回归就是带L2正则化的线性回归 岭回归&#xff0c;其实也是一种线性回归。只不过在算法建立回归方程时候&#xff0c;加上L2正则化的限制&#xff0c;从而达到解决过拟合的效果 二、API 1、sklearn.linear_model.Ridge(alpha1.0, fit_interceptTrue…

【无人机】太阳能伪卫星VoLTE无人机设计(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

从0到1,申请cos服务器并上传图片到cos文件服务器

目录 准备工作 Java代码编写 控制台打印 整理成工具类 编写接口 Postman测试 准备工作 1.进入网址腾讯云 产业智变云启未来 - 腾讯 (tencent.com) 2.搜索cos,点击立即使用&#xff0c;刚开始会免费赠送你 3.存储都是基于桶的&#xff0c;先创建桶&#xff0c;在桶里面创…

PyQt 问题记录

1.现成的组件不一定线程安全&#xff0c;&#xff08;包括且不限于数据的修改竞争,和一些组件的崩溃 ) 对于PyQt 的线程使用&#xff0c;可能还需要更谨慎些 保存逻辑 QuestionBox("保存/Save")def Save(self):okFlagFalseerrFlagFalseWriteCmd{}for it in self.Mode…

视频SDK开发,多平台SDK快速接入

随着科技的不断发展&#xff0c;视频已经成为了企业业务中不可或缺的一部分。无论是在线教育、企业培训还是产品展示&#xff0c;视频都发挥着至关重要的作用。为了满足企业对视频应用的需求&#xff0c;美摄视频SDK应运而生&#xff0c;为企业提供了一站式的视频解决方案。 一…