当前位置: 首页 > news >正文

PCIe驱动开发(3)— 驱动设备文件的创建与操作

PCIe驱动开发(3)— 驱动设备文件的创建与操作

一、前言

在 Linux 中一切皆为文件,驱动加载成功以后会在“/dev”目录下生成一个相应的文件,应用程序通过对这个名为“/dev/xxx” (xxx 是具体的驱动文件名字)的文件进行相应的操作即可实现对硬件的操作。

二、创建设备文件

PCIe设备属于字符设备,我们按如下步骤创建一个字符设备:

	/* 1、Request device number */ret = alloc_chrdev_region(&hello_pci_info.dev_id, 0, 1, "hello_pcie");/* 2、Initial char_dev */hello_pci_info.cdev.owner = THIS_MODULE;cdev_init(&hello_pci_info.char_dev, &hello_pci_fops);/* 3、add char_dev */cdev_add(&hello_pci_info.char_dev, hello_pci_info.dev_id, 1);/* 4、create class */hello_pci_info.class = class_create(THIS_MODULE, "hello_pcie");if (IS_ERR(hello_pci_info.class)) {return PTR_ERR(hello_pci_info.class);}/* 5、create device */hello_pci_info.device = device_create(hello_pci_info.class, NULL, hello_pci_info.dev_id, NULL, "hello_pcie");if (IS_ERR(newchrled.device)) {return PTR_ERR(newchrled.device);}

其中需要定义一个设备文件操作函数结构体,可以暂时定义为如下所示:

/* device file operations function */
static struct file_operations hello_pcie_fops = {.owner = THIS_MODULE,
};

将上述创建一个字符设备的操作加在hello_pci_init函数里,同时hello_pci_exit添加对应的卸载操作:

static void __exit hello_pci_exit(void)
{if(hello_pci_info.dev != NULL) {cdev_del(&hello_pci_info.char_dev);						/* del cdev */unregister_chrdev_region(hello_pci_info.dev_id, 1); 	/* unregister device number */device_destroy(hello_pci_info.class, hello_pci_info.dev_id);class_destroy(hello_pci_info.class);}pci_unregister_driver(&hello_pci_driver);
}

然后编译加载驱动,便可以看到在/dev下有我们创建的hello_pcie设备了:
在这里插入图片描述

http://www.lryc.cn/news/399659.html

相关文章:

  • 【Redis】简单了解Redis中常用的命令与数据结构
  • IDEA启动Web项目总是提示端口占用
  • JRT打印鉴定记录单
  • 数据处理-Matplotlib 绘图展示
  • Nginx -Web服务器/反向代理/负载均衡
  • 机器人三定律及伦理分析
  • 自动驾驶算法———车道检测(一)
  • 小程序自学教程
  • How do I format markdown chatgpt response in tkinter frame python?
  • vs2019 QT无法打开源文件QModbusTcpClient
  • 初识c++(命名空间,缺省参数,函数重载)
  • 印尼Facebook直播网络需要达到什么要求?
  • 力扣题解(最长回文子串)
  • 数据湖表格式 Hudi/Iceberg/DeltaLake/Paimon TPCDS 性能对比(Spark 引擎)
  • 脚本练习-每5分钟执行一次获取当前服务器的基本情况
  • 技术探索之kotlin浅谈
  • 机器学习之常用优化器
  • 机器学习基本概念,Numpy,matplotlib和张量Tensor知识进一步学习
  • 博客前端项目学习day01
  • java Collections.synchronizedCollection方法介绍
  • 力扣每日一题:3011. 判断一个数组是否可以变为有序
  • ubuntu 上vscode +cmake的debug调试配置方法
  • 使用Redis实现签到功能:Java示例解析
  • tableau标靶图,甘特图与瀑布图绘制 - 9
  • 双向链表专题
  • SpringCoud组件
  • 向量的定义和解释
  • IoTDB 集群高效管理:一键启停功能介绍
  • 一个spring boot项目的启动过程分析
  • 智驭未来:人工智能与目标检测的深度交融