欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Linux驱动学习(二)--字符设备

Linux驱动学习(二)--字符设备

2025/2/23 10:41:56 来源:https://blog.csdn.net/fantasyYXQ/article/details/145714808  浏览:    关键词:Linux驱动学习(二)--字符设备

设备分类

  • 字符设备
  • 块设备
  • 网络设备

内核结构图:

 字符设备号

字符设备号是32位的无符号整型值

  • 高12位:主设备号
  • 低20位:次设备号

 查看设备号

  • cat /proc/devices

设备号构造

 直接使用宏MKDEV

#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))

设备号注册/注销

注册设备号函数:

 设备号注销函数

实验 

结果

代码

 

/**chr_dev.c*Original Author: luoyunheng, 2025-02-19** Linux驱动之字符设备的设备号
*/#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>static int major = 222;
static int minor = 0;static dev_t devno;static int hello_init(void)
{int result;printk("hello_init\n");devno = MKDEV(major, minor);result = register_chrdev_region(devno, 1, "loh");if (result < 0 ) {printk("register dev number failed\n");return result;}return 0;
}static void hello_exit(void)
{printk("hello_exit\n");unregister_chrdev_region(devno, 1);\return;
}module_init(hello_init);
module_exit(hello_exit);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词