从 Linux 内核模块访问串行端口

发布于 2024-10-24 07:04:13 字数 386 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-10-31 07:04:13

我认为既然涉及到串口,那么这一定是某种嵌入式系统。毕竟,没有多少 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.

绝不服输 2024-10-31 07:04:13

首先,如果可能的话,我建议您找到一种从用户空间执行此操作的方法:您在这里尝试实现的实际上是内核代码中的用户空间代码。

但如果您找不到方法,可以阅读这篇文章 向您展示如何在内核空间中进行用户空间调用。

由于您想要访问串行端口,因此应该有面向 tty 的调用,例如 open:

serial_fd = sys_open("/dev/ttyS0",  O_RDWR | O_NOCTTY | O_NONBLOCK))

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:

serial_fd = sys_open("/dev/ttyS0",  O_RDWR | O_NOCTTY | O_NONBLOCK))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文