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内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SQL server配置管理器找不到如何打开它

《SQLserver配置管理器找不到如何打开它》最近遇到了SQLserver配置管理器打不开的问题,尝试在开始菜单栏搜SQLServerManager无果,于是将自己找到的方法总结分享给大家,对SQ... 目录方法一:桌面图标进入方法二:运行窗口进入方法三:查找文件路径方法四:检查 SQL Server 安

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.