简体   繁体   English

Linux:如何从用户空间通过 cdc-adm 驱动程序的 USB 批量端点传输数据

[英]Linux : how to transfer data through USB bulk endpoint of cdc-adm driver from userspace

I am a beginner to Linux drivers and I started with writing an application for a cdc-acm based USB device in linux.我是 Linux 驱动程序的初学者,我开始在 linux 中为基于 cdc-acm 的 USB 设备编写应用程序。 Therefore, I have used the cdc_acm driver.因此,我使用了 cdc_acm 驱动程序。 The USB device that I am using has 2 Bulk endpoints (read and write) and one interrupts endpoint.我使用的 USB 设备有 2 个批量端点(读取和写入)和一个中断端点。

Now, my question is whether all these endpoints operate on the same /dev/ttyACM0 file, and how does the write call on this tty file get convert into acm_write_bulk fops call?现在,我的问题是所有这些端点是否都在同一个 /dev/ttyACM0 文件上运行,以及如何将此 tty 文件上的 write 调用转换为 acm_write_bulk fops 调用?

If I write a data to trigger a USB functionality into the ttyACM0 file, will the data get sent through bulk out endpoint?如果我将数据写入 ttyACM0 文件以触发 USB 功能,数据是否会通过批量输出端点发送? Or how should I send the data to the bulk endpoint directly from user space.或者我应该如何直接从用户空间将数据发送到批量端点。 Should I write any supporting driver in kernel space?我应该在内核空间中编写任何支持驱动程序吗? Similarly, how do I read the data from interrupt endpoint in the userspace?同样,如何从用户空间中的中断端点读取数据?

I appreciate your help in advance.我提前感谢您的帮助。

There is no need to write a kernel space driver.无需编写内核空间驱动程序。 You can open /dev/ttyACM0 with the open system call, set parameters for it using termios (optional), and then use the read and write system calls to read and write bulk data from your device.您可以使用open系统调用打开/dev/ttyACM0 ,使用termios (可选)为其设置参数,然后使用readwrite系统调用从您的设备读取和写入批量数据。 These system calls are easiest to access from C and C++, but most languages have a library you can use to access serial ports.这些系统调用最容易从 C 和 C++ 访问,但大多数语言都有一个可用于访问串行端口的库。

I don't know of a great way to read data from the interrupt endpoint in Linux but you can at least look into the TIOCMGET, TIOCGICOUNT, and TIOCMIWAIT ioctls in you really need to do that.我不知道从 Linux 中的中断端点读取数据的好方法,但您至少可以查看 TIOCMGET、TIOCGICOUNT 和 TIOCMIWAIT ioctls,因为您确实需要这样做。

The Linux serial port interface abstracts away all details about USB, endpoints, and bulk transfers, so you can use a simpler, more abstract API to communicate with the serial port. Linux 串行端口接口抽象了有关 USB、端点和批量传输的所有细节,因此您可以使用更简单、更抽象的 API 与串行端口进行通信。 In fact, you can use the same code on any type of serial port, regardless of which kernel driver implements the serial port.实际上,您可以在任何类型的串口上使用相同的代码,而不管该串口是由哪个内核驱动程序实现的。 It might help you to search the Internet for things like "linux serial port programming" or "posix serial port programming" to understand more about how to do this.它可能会帮助您在 Internet 上搜索诸如“linux 串口编程”或“posix 串口编程”之类的内容,以了解有关如何执行此操作的更多信息。

If you really are curious about how the Linux CDC ACM driver works, and how it converts a write system call into the corresponding USB transfer, you can read the source of the cdc-acm driver .如果您真的对 Linux CDC ACM 驱动程序的工作原理以及它如何将write系统调用转换为相应的 USB 传输感到好奇,您可以阅读cdc-acm 驱动程序源代码 However, that is way beyond what you need to do to simply use a serial port.但是,这远远超出了您只需使用串行端口所需要做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM