gpt4 book ai didi

linux-kernel - 如何获得 Linux 字符设备的结构设备

转载 作者:行者123 更新时间:2023-12-04 05:24:23 25 4
gpt4 key购买 nike

我有一个实现字符设备驱动程序的 Linux 内核模块。我已通读 Linux Device Drivers并遵循了几个教程。此时,我有一个简单的模块,它提供了 open , release , 和 write文件操作。

我正在尝试使用通用 DMA 层来创建流式 DMA 映射。我对LDD的以下摘录感到困惑:

Many of the functions below require a struct device. This structure is the low-level representation of a device within the Linux device model. It is not something that drivers often have to work with directly, but you do need ot when using the generic DMA layer. Usually, you can find this structure buried inside the bus specific that describes your device. For example, it can be found as the dev field in struct pci_device or struct usb_device.



我进一步阅读了 Linux 设备模型,并遇到了以下问题:

At the lowest level, every device in a Linux system is represented by an instance of struct device.



我怎样才能得到 struct device为我的字符设备?是否在幕后为我创建了一个,还是我需要创建它?

我尝试使用 class_create() 手动创建一个类然后用它来创建一个带有 device_create() 的设备,但是当我使用该设备设置 DMA 映射时,我想我只是得到了一个虚假地址。这是正确的方法吗?

有关我的平台的更多信息,我正在使用 Altera SoCFPGA 平台 (ARM),因此我的设备不是真正的硬件设备,如 USB 或 PCI 设备,而是在 FPGA 中实现的逻辑。

我在 Chapter 14 of LDD 中找到了很多信息我认为可能是相关的(公共(public)汽车、设备、驱动程序等),但我只是不确定何时或如何使用它。对我来说,那一章似乎在讨论所有设备和驱动程序都使用的许多数据结构,但我很困惑,因为我不必使用其中的任何一个。

最佳答案

我最终创建了一个平台驱动程序和平台设备。平台设备结构有它自己的 struct device与平台总线相关联,它是专为此类事情设计的“伪总线”。官方documentation对于平台驱动程序在这里很有帮助。

最后,我的模块最终实现了平台驱动程序和字符设备驱动程序。给我带来最大麻烦的部分是创建一个平台设备并将其与我的平台驱动程序相关联。我首先使用 platform_device_alloc 手动创 build 备(在模块安装时)和 platform_device_register .一旦我完成了这项工作,我最终删除了手动设备创建,而是依靠设备树条目来创建我的设备。

关于linux-kernel - 如何获得 Linux 字符设备的结构设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com