如何从内核模块访问(打开/读取/等)/dev 设备?
我收到了一个驱动程序(用于 PCI 卡的 PCAN 驱动程序,使用 rtdm),它创建 /dev/pcan0 并将 /dev/pcan1 编译为 netdev 驱动程序。
该驱动程序附带了许多功能,但它们都是针对读取 CAN 消息的用户级程序。然而,我需要的是从内核模块读取这些消息。 PCAN 驱动程序不导出任何变量/函数,这意味着它不提供内核级 API 供我使用。
我简要地查看了代码,从 /dev 设备读取数据并写入它不使用 copy_from_user
或 copy_to_user
。因此,我认为从内核模块打开 /dev/pcan0 并从中读取应该是安全的。
现在我的问题是,如何从内核模块打开/读取 /dev 设备?
PS 我想从 RTAI 实时线程读取 CAN 总线,你认为这会导致问题吗(例如每次读取都会通过 Linux 内核,从而破坏实时条件?)
I have received a driver (PCAN driver for PCI card, using rtdm), that creates /dev/pcan0 and /dev/pcan1 is compiled as a netdev driver.
There are many facilities that come with this driver, but they are all targeted at a user-level program reading CAN messages. What I need however is to read these messages from a kernel module. The PCAN driver doesn't export any variables/functions which means it doesn't provide a kernel-level API for me to use.
I looked briefly at the code and reading from the /dev device and writing to it don't use copy_from_user
or copy_to_user
. Therefore, I thought it should be safe for me to open the /dev/pcan0 from my kernel module and read from it.
Now my question is, how do I open/read from a /dev device from a kernel module?
P.S. I want to read from the CAN bus from an RTAI real-time thread, do you think that would cause a problem (for example every read passing through linux kernel and thus breaking real-time conditions?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于我使用的是 RTDM,有两种选择:
Given that I was using RTDM, there were two options:
您可以直接从内核空间使用系统调用:sys_open()、sys_read()、sys_close()。
Linuxjournal 文章对此进行了介绍。
P/S:copy_from_user() 与内核空间地址完美配合。
You can use syscalls directly from kernel space: sys_open(), sys_read(), sys_close().
There's Linuxjournal article about that.
P/S: copy_from_user() works perfectly with kernel-space addresses.