从 linux 内核模块访问串行端口 [英] Accessing a serial port from a linux kernel module

查看:38
本文介绍了从 linux 内核模块访问串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 Linux 内核驱动程序大师!

Hello Linux Kernel Driver Gurus!

我正在为使用串行接口进行配置的相机编写 v4l2 驱动程序.我希望驱动程序能够配置相机,因为它可以使不同相机型号的客户端代码保持一致.问题是:从驱动程序模块访问相机串行接口的最佳方式是什么?

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?

推荐答案

我推测,由于涉及到串口,所以这一定是某种嵌入式系统.毕竟,没有多少个人电脑甚至有串口.我还假设串行端口可以被视为永久连接,至少从用户的角度来看是这样.如果这一切都是真的,那么你真的不需要 TTY 设备.您想将设备作为私有 UART 访问.

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.

如果您查看 Wolfson 音频编解码器 (sound/soc/wm*.c),您将看到一个主要通过 I2S 进行通信但具有用于配置的辅助 I2C 接口的设备示例.我相信,这在概念上就是你想要的.驱动程序为软件提供了统一的接口,并向任何合适的硬件发出命令.显然,这比向用户空间公开硬件实现细节要干净得多.

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.

我在内核中找不到以这种方式工作的 UART 驱动程序的好例子,但希望我已经描述了要查找的内容.从实用而非技术纯度的角度来看,从内核进行文件 I/O 可能更好.

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.

这篇关于从 linux 内核模块访问串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆