






摘" 要: 針對Linux下通過USB Gadget框架將USB接口模擬成其他USB設備時存在設備配置信息修改不方便的問題,可以使用Configfs動態配置USB設備。文中介紹了USB Gadget的框架與原理,重點分析了Configfs的主要數據結構和內部實現機制,給出了使用Configfs在用戶模式配置USB設備的具體方法,并在I.MX6ULL平臺上將Gadget功能驅動層已有的串口功能驅動程序使用Configfs進行配置后,實現了將USB接口虛擬成USB串口設備。經過測試,虛擬串口設備傳輸正常,為開發人員在USB Gadget下使用Configfs配置USB設備提供了參考。
關鍵詞: 虛擬串口; USB Gadget; Configfs; Linux; 串口設備; I.MX6ULL
中圖分類號: TN919?34; TP368.1; TP316.81" " " " " 文獻標識碼: A" " " " " " " "文章編號: 1004?373X(2025)02?0046?05
Research on Gadget Configfs and its application in USB virtual serial port
ZHANG Yi1, BAO Aida1, GUO Xin2, LIU Xinyi1
(1. College of Instrumentation and Electronics, North University of China, Taiyuan 030051, China;
2. School of Information and Communication Engineering, North University of China, Taiyuan 030051, China)
Abstract: In allusion to the problem of inconvenient modification of device configuration information when simulating USB interfaces to other USB devices by means of the USB Gadget framework under Linux, Configfs can be used to configure USB devices dynamically. The framework and principle of USB Gadget is introduced, and the main data structure and internal mechanism of Configfs is analyzed stressly. A specific method for configuring USB devices in user mode using Configfs is provided. After configuring the existing serial port function driver program of the Gadget function driver layer on the I.MX6ULL platform using Configfs, the USB interface was virtualized into a USB serial port device. The testing results show that the virtual serial device transmission is normal, providing a reference for developers to configure USB devices using Configfs under USB Gadget.
Keywords: virtual serial port; USB Gadget; Configfs; Linux; serial port device; I.MX6ULL
0" 引" 言
隨著嵌入式系統的發展,在Linux下通過通用的Gadget驅動框架將USB接口模擬成其他類型設備,從而滿足開發人員在不需要實際硬件設備的情況下進行軟件開發和測試需求的方法也日益增多[1?6]。而以往通過Gadget驅動框架實現USB設備端驅動時,需要在gadget\legacy功能驅動層編寫驅動代碼配置設備信息,存在配置更改不方便的問題。利用內核提供的Configfs允許用戶直接通過文件系統的操作,在運行時根據需要動態配置USB Gadget的參數和屬性,而無需重新編譯內核或啟動系統,配置設備信息更加靈活便捷,這對于需要適應不同應用場景和需求的嵌入式系統特別有用。因此本文研究了如何通過Configfs配置USB Gadget虛擬的串口設備。介紹了USB Gadget框架與原理;重點分析了Configfs的數據結構和內部實現機制,并給出了通過Configfs配置Gadget以實現USB接口虛擬成USB串口設備的具體操作方法;最后編寫串口測試程序,在應用程序中設置好串口參數進行測試。……