访问Linux /dev/USB作为标准文件与USB设备通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9540267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Linux /dev/USB as standard files to communicate with USB device
提问by Chimera
I'm researching ways to communicate with a USB device in Linux and would prefer to not write a Linux Kernel driver. I understand that libusb exists and is a user-land library that would work, but our embedded device doesn't support usbfs ( and would be really a pain to change kernels to add the support ).
我正在研究在 Linux 中与 USB 设备通信的方法,并且不想编写 Linux 内核驱动程序。我知道 libusb 存在并且是一个可以工作的用户级库,但是我们的嵌入式设备不支持 usbfs(并且更改内核以添加支持真的很痛苦)。
So my question is basically this: Is it possible / advisable to communicate with a USB device by directly reading and writing to the /dev/USB or the udev file corresponding to the USB device thus bypassing the need for a custom Linux Driver and usbfs?
所以我的问题基本上是这样的:是否可以/建议通过直接读取和写入 /dev/USB 或与 USB 设备对应的 udev 文件来与 USB 设备通信,从而绕过对自定义 Linux 驱动程序和 usbfs 的需要?
I'm hoping it's possible to communicate using the USB devices protocol just by reading / writing protocol packets directly through file-type read/write commands once the /dev/USB or udev device file is open.
我希望一旦 /dev/USB 或 udev 设备文件打开,就可以通过直接通过文件类型读/写命令读取/写入协议数据包来使用 USB 设备协议进行通信。
Thoughts and suggestions please.
请提出想法和建议。
FOLLOW UP:
跟进:
Since the USB device I needed to talk to is a USB HID class device, I was able to use libudev and the standard Linux USB HID RAW driver by reading / writing directly to /dev/hidraw0 ( or the appropriate /dev/hidraw device ). It wasn't necessary to write a custom driver for a simple USB HID device.
由于我需要与之通信的 USB 设备是 USB HID 类设备,因此我能够通过直接读取/写入 /dev/hidraw0(或适当的 /dev/hidraw 设备)来使用 libudev 和标准 Linux USB HID RAW 驱动程序. 没有必要为简单的 USB HID 设备编写自定义驱动程序。
采纳答案by Pavan Manjunath
Jim, I don't think you can escape the need to write a driver and just manage to read the USB file in /dev
. Because who defines as to what should happen when you do a read()
on the USB device file? And who defines what action should be initiated when you invoke sysioctl()
? Your driver!In other words, the device files are themselves incapable of anything until they are supported by the underlying drivers. In fact, you can treat the device files to be an abstraction of the underlying driver! So, no driver, no use of device file :(
吉姆,我认为您无法避免编写驱动程序并设法读取/dev
. 因为谁定义了read()
在 USB 设备文件上执行操作时会发生什么?谁定义了调用时应启动的操作sysioctl()
?你的司机!换句话说,在底层驱动程序支持之前,设备文件本身无能为力。实际上,您可以将设备文件视为底层驱动程序的抽象!所以,没有驱动程序,没有使用设备文件:(
I suggest you go through the following articles about how to write a driver and also understand the USB internals-
我建议您阅读以下有关如何编写驱动程序并了解 USB 内部结构的文章-
http://www.linuxjournal.com/article/4786( Slightly outdated )