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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/772462.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据分析面试题(21~30)

21、简单说一下说说置信区间、置信度。 ①置信区间是指由样本统计量所构成的总体参数的估计区间。通常以一个样本统计量的估计值为中心&#xff0c;加减一个标准误差的倍数&#xff0c;构成一个区间。 ②置信度是对置信区间的信心程度的度量&#xff0c;通常以百分比的形式表…

Protocol Buffers设计要点

概述 一种开源跨平台的序列化结构化数据的协议。可用于存储数据或在网络上进行数据通信。它提供了用于描述数据结构的接口描述语言&#xff08;IDL&#xff09;&#xff0c;也提供了根据 IDL 产生代码的程序工具。Protocol Buffers的设计目标是简单和性能&#xff0c;所以与 XM…

(执行上下文作用域链)前端八股文修炼Day4

一 作用域作用域链 作用域&#xff08;Scope&#xff09;是指程序中定义变量的区域&#xff0c;作用域规定了在这个区域内变量的可访问性。在 JavaScript 中&#xff0c;作用域可以分为全局作用域和局部作用域。 全局作用域&#xff1a;在代码中任何地方都可以访问的作用域&am…

基于Springboot的狱内罪犯危险性评估系统的设计与实现(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的狱内罪犯危险性评估系统的设计与实现&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#…

宝塔部署项目

如何在云服务器上使用宝塔 登录到你的云服务器后&#xff0c;执行宝塔面板安装命令&#xff0c;阿里云服务器网使用的CentOS操作系统&#xff0c;命令如下 yum install -y wget && wget -O install.sh https://download.bt.cn/install/install_6.0.sh && sh …

题。。。。

O - 胜利大逃亡(续) 题目分析 bfs状态压缩&#xff08;在bfs的基础上&#xff0c;存储持有不同钥匙时&#xff0c;此点位是否走过的情况&#xff09;&#xff1b; -----状态压缩使用二进制实现&#xff0c;同时通过位运算修改是否转移至另一状态&#xff08;详情见代码及注释…

解决 Xshell 等工具连接虚拟机失败

这里以 Xshell 等工具连接 Linux 虚拟机为例 对于我们使用 Xshell 等工具连接虚拟机失败&#xff0c;我们可以从以下的几个方面进行检查和解决 检查连接工具中的连接会话配置是否正确 对于这方面&#xff0c;我们要检查连接工具中连接会话配置的虚拟机 IP 地址和端口号是否正…

理解Harris角点检测的数学原理

Harris角点检测的数学原理 Harris角点检测基于图像的局部自相似性,它通过分析图像窗口在各个方向上移动时灰度变化的程度来识别角点,它通过计算每个像素点的Harris响应值来评估该点是否为角点。数学上,这种变化可以通过构建一个二次型函数来量化,该函数基于图像在x和y方向上…

Postman核心功能解析-参数化和测试报告

一、参数化处理 参数化&#xff1a;针对于某一个接口&#xff0c;有大量的的测试数据需要批量验证&#xff0c;一个一个的更改请求参数太耗时耗力&#xff0c;使用参数化批量处理数据会比较高效&#xff0c;常规通过文档参数化实现。 创建文件 格式CSV 文件内第一行信息 需要…

Ansible Playbook 精髓:书写与应用全攻略

Ansible Playbook 精髓&#xff1a;书写与应用全攻略 在当今的自动化运维领域&#xff0c;Ansible 以其简洁高效的特点受到了广泛欢迎。Playbook 作为 Ansible 的核心组件&#xff0c;允许我们使用人类可读的语言来描述配置和应用部署的过程。本文将深入探讨 Ansible Playbook…

音频干扰检测(时域方法)

请注意注释掉的代码&#xff1a;逐个包络比对就不能加窗了。 import librosa import numpy as np from scipy.signal import windows import matplotlib.pyplot as plt # 读取音频文件 audio_file sine.wav signal, sample_rate librosa.load(audio_file, srNone, mono…

操作系统的理解|冯·若依曼体系结构|进程的状态

操作系统的理解 冯诺伊曼体系结构为什么必须通过内存然后到cpu存储金字塔冯诺伊曼结构的改进在哪&#xff1f;我们可不可以全部用寄存器来做存储器在硬件数据流动角度学以致用&#xff1a;解释程序运行为什么要加载到内存程序没被运行之前存在哪里&#xff1f; 操作系统概念广义…

应急响应实战笔记04Windows实战篇(2)

第2篇&#xff1a;蠕虫病毒 0x00 前言 ​ 蠕虫病毒是一种十分古老的计算机病毒&#xff0c;它是一种自包含的程序&#xff08;或是一套程序&#xff09;&#xff0c;通常通过网络途径传播&#xff0c;每入侵到一台新的计算机&#xff0c;它就在这台计算机上复制自己&#xff…

第一个C++程序,我也没看明白,暂时。

#include<iostream> using namespace std; int main() { cout << "hello world and you too number!" << endl; system("pause"); return 0; } 运行结果为&#xff1a;

优化生产流程,解决无尘布擦拭留下划痕问题

在现代化工生产中&#xff0c;无尘布被广泛应用于清洁工作&#xff0c;然而&#xff0c;河北一家化工企业在使用无尘布进行擦拭时却发现产品表面留下了划痕&#xff0c;给生产过程带来了不小的困扰。针对这一问题&#xff0c;一家化工企业向供应商优斯特寻求解决方案&#xff0…

AI视频激光综合驱鸟装置:全自动、大范围驱鸟 | 真驱鸟科技

在电力系统中&#xff0c;鸟害事故已成为一个不容忽视的问题&#xff0c;直接威胁到电网的正常运行。但鸟类拥有极强的环境适应能力&#xff0c;它们能够在各种环境中生存和繁衍。这种强大的适应性使得传统的单一功能驱鸟器&#xff0c;在面对鸟类时显得力不从心&#xff0c;无…

苹果与百度合作,将在iPhone 16中使用生成式AI

3月25日&#xff0c;《科创板日报》消息&#xff0c;苹果将与百度进行技术合作&#xff0c;为今年即将发布的iPhone16、Mac系统和iOS 18提供生成式AI&#xff08;AIGC&#xff09;功能。 据悉&#xff0c;苹果曾与阿里巴巴以及另外一家国产大模型厂商进行了技术合作洽谈。最终…

#Linux系统编程(ps和kill命令)

&#xff08;一&#xff09;发行版&#xff1a;Ubuntu16.04.7 &#xff08;二&#xff09;记录&#xff1a; &#xff08;1&#xff09;ps命令 可以列出系统中当前运行的那些进程。 命令格式&#xff1a;ps 参数(常用-aux) 命令功能&#xff1a;用来显示当前进程的状态 常…

[STL]priority_queue类及反向迭代器的模拟实现

&#x1fa90;&#x1fa90;&#x1fa90;欢迎来到程序员餐厅&#x1f4ab;&#x1f4ab;&#x1f4ab; 今日主菜&#xff1a; priority_queue类及反向迭代器 主厨&#xff1a;邪王真眼 主厨的主页&#xff1a;Chef‘s blog 所属专栏&#xff1a;c大冒险 向着c&…

istio 设置 istio-proxy sidecar 的 resource 的 limit 和 request

方式一 修改 configmap 查看当前 sidecar 的 cpu 和 memory 的配额 在 istio-sidecar-injector 中查找,修改后重启 pod 可以生效(下面那个 proxy_init 配置不管,不知道是干嘛的) 方式二 如果是通过 iop 安装的 istio,可以修改 iop 文件中的配置 spec:values:global:…