从 Linux 内核模块访问串行端口
Linux 内核驱动大师们大家好!
我正在为使用串行接口进行配置的相机编写 v4l2 驱动程序。我希望驱动程序能够配置相机,因为它可以使客户端代码在相机型号之间保持一致。问题是:从驱动程序模块访问相机串行接口的最佳方法是什么?
据我所知,从内核驱动程序访问文件是一个很大的禁忌,但这是可以做到的。因此,我目前正在使用以下代码片段,但感觉就像是黑客攻击。
oldfs = get_fs();
set_fs(KERNEL_DS);
fd->f_pos=0;
fd->f_op->write(fd, data, data_len, &fd->f_pos);
set_fs(oldfs);
我的问题确实是:这样做的正确方法是什么?
Hello Linux Kernel Driver Gurus!
I'm writing a v4l2 driver for a camera that uses a serial interface for configuration. I'd like the driver to configure the camera, as it keeps the client code consistent across camera models. The question is: what's the best way to access the camera's serial interface from the driver module?
From what I hear, accessing files from a kernel driver is a big no-no, but it can be done. As such, I'm currently using the following code snippet, but it feels like a hack.
oldfs = get_fs();
set_fs(KERNEL_DS);
fd->f_pos=0;
fd->f_op->write(fd, data, data_len, &fd->f_pos);
set_fs(oldfs);
My question is really: what's the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为既然涉及到串口,那么这一定是某种嵌入式系统。毕竟,没有多少 PC 具有串行端口。我还假设串行端口可以被视为永久连接,至少从用户的角度来看是这样。如果这都是真的,那么您实际上并不需要 TTY 设备。您希望将设备作为私有 UART 进行访问。
如果您查看 Wolfson 音频编解码器 (sound/soc/wm*.c),您将看到主要通过 I2S 通信但具有用于配置的辅助 I2C 接口的设备示例。我相信,从概念上讲,这就是您想要的。驱动程序为软件提供了统一的接口,并向任何合适的硬件发出命令。显然,这比必须向用户空间公开硬件实现细节要干净得多。
我在内核中找不到以这种方式工作的 UART 驱动程序的好示例,但希望我已经描述了要查找的内容。从实用而非技术纯粹的角度来看,从内核执行文件 I/O 可能更好。
I presume that since a serial port is involved, this must be some kind of embedded system. After all, not many PCs even have serial ports. I also assume that the serial port can be considered a permanent connection, at least from the user's perspective. If that is all true, then you don't really want a TTY device. You want to access the device as a private UART.
If you have a look at the Wolfson audio codecs (sound/soc/wm*.c) you will see an example of devices that primarily communicate over I2S but have an auxiliary I2C interface for configuration. This is conceptually what you want, I believe. The driver presents a unified interface to software, and issues commands to whatever hardware is appropriate. Obviously this is much cleaner than having to expose hardware implementation details to userspace.
I couldn't find a good example of a UART driver in the kernel that works this way, but hopefully I've described what to look for. From a practical rather than technical purity point of view, it might just be better to do file I/O from the kernel.
首先,如果可能的话,我建议您找到一种从用户空间执行此操作的方法:您在这里尝试实现的实际上是内核代码中的用户空间代码。
但如果您找不到方法,可以阅读这篇文章 向您展示如何在内核空间中进行用户空间调用。
由于您想要访问串行端口,因此应该有面向 tty 的调用,例如 open:
First I would advise you to find a way to do this from the userspace if possible: what you try to achieve here really is userspace code in kernel code.
But if you don't find a way to do it, this article shows you how to do userspace calls in kernelspace.
Since you want to access a serial port, you should have calls that tty oriented, for instance for open: