USB - 通过configfs配置Linux USB Gadget

2024-03-20 08:44
文章标签 配置 linux usb gadget configfs

本文主要是介绍USB - 通过configfs配置Linux USB Gadget,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Linux USB gadget configured through configfs

Overview

USB Linux 小工具是一种具有 UDC(USB 设备控制器)的设备,可连接到 USB 主机,以扩展其附加功能,如串行端口或大容量存储功能。

A USB Linux Gadget is a device which has a UDC (USB Device Controller) and can be connected to a USB Host to extend it with additional functions like a serial port or a mass storage capability.

主机将gadget视为一组配置,每个配置都包含若干接口,从gadget的角度看,这些接口被称为功能,每个功能代表一个串行连接或 SCSI 磁盘等。

A gadget is seen by its host as a set of configurations, each of which contains a number of interfaces which, from the gadget's perspective, are known as functions, each function representing e.g. a serial connection or a SCSI disk.

Linux 为gadget提供了许多功能。

Linux provides a number of functions for gadgets to use.

创建gadget意味着要决定有哪些配置,以及每个配置将提供哪些功能。

Creating a gadget means deciding what configurations there will be and which functions each configuration will provide.

Configfs(请参阅 Configfs - 用户空间驱动的内核对象配置)可以很好地将上述决定告知内核。本文档将介绍如何做到这一点。 (Configfs - Userspace-driven Kernel Object Configuration — The Linux Kernel documentation)

Configfs (please see Configfs - Userspace-driven Kernel Object Configuration) lends itself nicely for the purpose of telling the kernel about the above mentioned decision. This document is about how to do it.

Configfs(请参阅 Configfs - 用户空间驱动的内核对象配置)可以很好地将上述决定告知内核。本文档将介绍如何做到这一点。

本文还将介绍如何将 configfs 集成到gadget中。

It also describes how configfs integration into gadget is designed.

Requirements

为使其正常工作,configfs 必须可用,因此 .config 中的 CONFIGFS_FS 必须为 "y "或 "m"。目前,USB_LIBCOMPOSITE 选择 CONFIGFS_FS。

In order for this to work configfs must be available, so CONFIGFS_FS must be 'y' or 'm' in .config. As of this writing USB_LIBCOMPOSITE selects CONFIGFS_FS.

Usage

描述通过 configfs 提供第一个功能的原始帖子可在此处查看:

The original post describing the first function made available through configfs can be seen here:

  [PATCH 30/30] usb/gadget: the start of the configfs interface — Linux USB 

$ modprobe libcomposite

$ mount none $CONFIGFS_HOME -t configfs

其中 CONFIGFS_HOME变量 是 configfs 的挂载点 (一个文件夹路径)。

where CONFIGFS_HOME is the mount point for configfs.

1. Creating the gadgets [创建Gadget]

必须为每个要创建的小工具创建相应的目录:

For each gadget to be created its corresponding directory must be created:

$ mkdir $CONFIGFS_HOME/usb_gadget/<gadget name>

e.g.:

$ mkdir $CONFIGFS_HOME/usb_gadget/g1

...

...

...

$ cd $CONFIGFS_HOME/usb_gadget/g1

每个小工具都需要指定供应商 ID <VID> 和产品 ID <PID>:

Each gadget needs to have its vendor id <VID> and product id <PID> specified:

$ echo <VID> > idVendor

$ echo <PID> > idProduct

当执行上面第一行语句后,在此gadget目录中,就会自动创建出一系列文件及文件夹。

Gadget还需要序列号、制造商和产品字符串。为了存放这些字符串,必须为每种语言创建一个字符串子目录,如:

A gadget also needs its serial number, manufacturer and product strings. In order to have a place to store them, a strings subdirectory must be created for each language, e.g.:

$ mkdir strings/0x409   # 0x409表示英文

然后就可以指定字符串了:

Then the strings can be specified:

$ echo <serial number> > strings/0x409/serialnumber

$ echo <manufacturer> > strings/0x409/manufacturer

$ echo <product> > strings/0x409/product

此外,还可以在语言目录中创建自定义字符串描述符目录,并将字符串文本写入字符串目录中的 "s "属性:

Further custom string descriptors can be created as directories within the language's directory, with the string text being written to the "s" attribute within the string's directory:

$ mkdir strings/0x409/xu.0 

$ echo <string text> > strings/0x409/xu.0/s

在函数驱动程序支持的情况下,函数可以允许这些自定义字符串描述符的符号链接,以便将这些字符串与类描述符关联起来。

Where function drivers support it, functions may allow symlinks to these custom string descriptors to associate those strings with class descriptors.

2. Creating the configurations [创建配置]

每个gadget都将包含多个配置,必须创建相应的目录:

Each gadget will consist of a number of configurations, their corresponding directories must be created:

$ mkdir configs/<name>.<number>

其中 <name> 可以是任何在文件系统中合法的字符串,<number> 是配置的编号,例如

where <name> can be any string which is legal in a filesystem and the <number> is the configuration's number, e.g.:

$ mkdir configs/c.1

...

...

...

每个配置还需要其字符串,因此必须为每种语言创建一个子目录,例如

Each configuration also needs its strings, so a subdirectory must be created for each language, e.g.:

$ mkdir configs/c.1/strings/0x409

然后就可以指定配置字符串了:

Then the configuration string can be specified:

$ echo <configuration> > configs/c.1/strings/0x409/configuration

也可为配置设置某些属性,例如:

Some attributes can also be set for a configuration, e.g.:

$ echo 120 > configs/c.1/MaxPower

3. Creating the functions [创建功能]

gadget将提供一些功能,必须为每个功能创建相应的目录:

The gadget will provide some functions, for each function its corresponding directory must be created:

$ mkdir functions/<name>.<instance name>

其中 <name> 是可使用函数名中的一个,而实例名则是文件系统中允许的任意字符串,例如

where <name> corresponds to one of allowed function names and instance name is an arbitrary string allowed in a filesystem, e.g.:

$ mkdir functions/ncm.usb0 # usb_f_ncm.ko gets loaded with request_module()

...

...

...

每个函数都提供其特定的属性集,具有只读或读写访问权限。在适用的情况下,需要对它们进行适当的写入。更多信息请参阅 Documentation/ABI/testing/configfs-usb-gadget。

Each function provides its specific set of attributes, with either read-only or read-write access. Where applicable they need to be written to as appropriate. Please refer to Documentation/ABI/testing/configfs-usb-gadget for more information.

4. Associating the functions with their configurations [将功能和配置进行关联]

此时会创建许多gadget,每个gadget都有指定的配置和可用的功能。剩下的工作就是指定哪个功能在哪个配置中可用(同一个功能可以在多个配置中使用)。这可以通过创建符号链接来实现:

At this moment a number of gadgets is created, each of which has a number of configurations specified and a number of functions available. What remains is specifying which function is available in which configuration (the same function can be used in multiple configurations). This is achieved with creating symbolic links:

$ ln -s functions/<name>.<instance name> configs/<name>.<number>

e.g.:

$ ln -s functions/ncm.usb0 configs/c.1

...

...

...

5. Enabling the gadget [使能Gadget]

上述所有步骤都是为了将配置和功能组成一个gadget。

目录结构示例如下:

All the above steps serve the purpose of composing the gadget of configurations and functions.

An example directory structure might look like this:

.

./strings

./strings/0x409

./strings/0x409/serialnumber

./strings/0x409/product

./strings/0x409/manufacturer

./configs

./configs/c.1

./configs/c.1/ncm.usb0 -> ../../../../usb_gadget/g1/functions/ncm.usb0

./configs/c.1/strings

./configs/c.1/strings/0x409

./configs/c.1/strings/0x409/configuration

./configs/c.1/bmAttributes

./configs/c.1/MaxPower

./functions

./functions/ncm.usb0

./functions/ncm.usb0/ifname

./functions/ncm.usb0/qmult

./functions/ncm.usb0/host_addr

./functions/ncm.usb0/dev_addr

./UDC

./bcdUSB

./bcdDevice

./idProduct

./idVendor

./bMaxPacketSize0

./bDeviceProtocol

./bDeviceSubClass

./bDeviceClass

需要最后使能这个Gadget,以便 USB 主机能对其进行枚举。

为了启用Gadget,必须将其绑定到 UDC(USB 设备控制器)上:

Such a gadget must be finally enabled so that the USB host can enumerate it.

In order to enable the gadget it must be bound to a UDC (USB Device Controller):

$ echo <udc name> > UDC

其中 <udc 名称> 是在 /sys/class/udc/* 中找到的名称之一,例如

where <udc name> is one of those found in /sys/class/udc/* e.g.:

$ echo s3c-hsotg > UDC

6. Disabling the gadge [停止Gadget]

$ echo "" > UDC

7. Cleaning up [移除]

从配置中删除功能:

Remove functions from configurations:

$ rm configs/<config name>.<number>/<function>

其中 <config name>.<number> 指定配置,<function> 是指向从配置中移除的函数的符号链接,例如

where <config name>.<number> specify the configuration and <function> is a symlink to a function being removed from the configuration, e.g.:

$ rm configs/c.1/ncm.usb0

...

...

...

删除配置中的字符串目录:

Remove strings directories in configurations:

$ rmdir configs/<config name>.<number>/strings/<lang>

e.g.:

$ rmdir configs/c.1/strings/0x409

...

...

...

and remove the configurations:

$ rmdir configs/<config name>.<number>

e.g.:

rmdir configs/c.1

...

...

...

移除函数(但函数模块不会被卸载):

Remove functions (function modules are not unloaded, though):

$ rmdir functions/<name>.<instance name>

e.g.:

$ rmdir functions/ncm.usb0

...

...

...

Remove strings directories in the gadget:

$ rmdir strings/<lang>

e.g.:

$ rmdir strings/0x409

and finally remove the gadget:

$ cd ..

$ rmdir <gadget name>

e.g.:

$ rmdir g1

参考:

Linux USB gadget configured through configfs — The Linux Kernel documentation

这篇关于USB - 通过configfs配置Linux USB Gadget的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/828902

相关文章

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

IDEA中配置Tomcat全过程

《IDEA中配置Tomcat全过程》文章介绍了在IDEA中配置Tomcat的六步流程,包括添加服务器、配置部署选项、设置应用服务器及启动,并提及Maven依赖可能因约定大于配置导致问题,需检查依赖版本... 目录第一步第二步第三步第四步第五步第六步总结第一步选择这个方框第二步选择+号,找到Tomca

Win10安装Maven与环境变量配置过程

《Win10安装Maven与环境变量配置过程》本文介绍Maven的安装与配置方法,涵盖下载、环境变量设置、本地仓库及镜像配置,指导如何在IDEA中正确配置Maven,适用于Java及其他语言项目的构建... 目录Maven 是什么?一、下载二、安装三、配置环境四、验证测试五、配置本地仓库六、配置国内镜像地址

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

Debian系和Redhat系防火墙配置方式

《Debian系和Redhat系防火墙配置方式》文章对比了Debian系UFW和Redhat系Firewalld防火墙的安装、启用禁用、端口管理、规则查看及注意事项,强调SSH端口需开放、规则持久化,... 目录Debian系UFW防火墙1. 安装2. 启用与禁用3. 基本命令4. 注意事项5. 示例配置R

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方