USB - USB Gadget on Linux

February, 2012. Embedded Linux Conference 2012.
Agenda
  • Introduction to USB
  • USB Gadget API
  • Existing Gadgets
  • Design your own Gadget
  • Demo
  • Conclusio
About the Author
Software engineer at Adeneo Embedded
  • Linux, Android
  • Main activities:
– BSP adaptation
– Driver development
– System integration
Context and objectives
General knowledge of the API
  • Focused on USB not general driver development
  • Nothing on the host side
Case study
  • Using a generic embedded device, see how we cancreate a USB Gadget
Show potential
  • How to fulfill every need
Universal Serial Bus
  • Industry standard developed in the mid-1990s
  • Defines the cables, connectors and protocols used forconnection, communication and power supplybetween computers and electronic devices
  • 2 billion USB devices were sold each year (as of 2008)
Benefits:
  • Replace lots of old buses
  • Automatic configuration
  • Multiple speeds
  • Reliable
Limits:
  • Distance
  • Peer-To-Peer
  • Broadcasting
Architecture:
  • Master-Slave protocol
  • Up to 127 devices addressable
  • Can be hot-plugged
  • Identification to the host
  • Supports high speeds
  • Supports high speeds
Description:
Endpoints
  • Source and Sink of data
  • Uniquely identifiable
  • Unique direction (except setup)
4 transfer types:
  • Control
    Configuration and control information
  • Interrupt
    Small quantities time-sensitive data
  • Bulk
    Large quantities time-insensitive data
  • Isochronous
    Real-time data at predictable bit rates
Typical Device Driver
  • Device Firmware Driver
    • Hardware specific routines
    • USB interrupts/events
  • Chapter 9
    • Enumeration process
    • Transfer data to upper layer
  • USB Class Driver
    • Defines the behavior
    • Provides configuration
Gadget API
  • Provides essential infrastructure
  • Similar to Chapter 9 in typical USB device software
  • Handles USB protocol specific requirements
  • Flexible enough to expose more complex USB devicecapabilities
Gadget API vs. Linux-USB API
  • Similarities
    • Share common definitions for the standard USB messages,structures and constants
    • Use queues of request objects to package I/O buffers
    • Both APIs bind and unbind drivers to devices
  • Differences
    • Control transfers
    • Configuration management
=> Thanks to similarities, Gadget API supports OTG
Gadget API
Lower boundary: 
  • handling setup requests (ep0 protocol responses)possibly including class-specific functionality
  • returning configuration and string descriptors
  • (re)setting configurations and interface alternate settings, including enabling and configuring endpoints
  • handling life cycle events, such as managing bindingsto hardware, USB suspend/resume, remote wakeup,and disconnection from the USB host
  • managing IN and OUT transfers on all currentlyenabled endpoints
Upper layer:
  • user mode code, using generic (gadgetfs) or applicationspecific files in /dev
  • networking subsystem (for network gadgets, like theCDC Ethernet Model gadget driver)
  • data capture drivers, perhaps video4Linux or a scannerdriver; or test and measurement hardware
  • input subsystem (for HID gadgets)
  • sound subsystem (for audio gadgets)
  • file system (for PTP gadgets)
  • block i/o subsystem (for usb-storage gadgets)
Gadget API – Main structures
struct usb_gadget – represents a gadget device
> usb_gadget_ops – contains callbacks for hardware operations
struct usb_ep – device hardware management
> usb_ep_ops – contains callbacks for endpoints operations
struct usb_gadget_driver – device functions management (bind, unbind, suspend etc...)
struct usb_request – USB transfers management
Gadget API – Main functions
General operations (usb_gadget_x()):
  • probe_driver / unregister_driver
  • set_selfpowered / clear_selfpowered
  • vbus_connect / vbus_disconnect
  • connect / disconnect
  • frame_number
Endpoint operations (usb_ep_x()):
  • autoconf / autoconf_reset
  • enable / disable
  • alloc / free
  • queue / dequeue
  • set_halt / clear_halt
  • fifo_status / fifo_flush
Descriptor operations:
  • usb_descriptor_fillbuf
  • usb_gadget_config_buf
Gadget API
Driver life cycle:
  • Register driver for a particular device controller
  • Register gadget driver (bind)
  • Hardware powered, enumeration starts
  • Gadget driver returns descriptors (setup)
  • Gadget driver returns interfaces configuration
  • Do real work (data transfer) until disconnect
  • Gadget driver unloaded (unbind)
Existing Gadgets
Ethernet
  • Enumerate to the host as an Ethernet device
  • Can easily be bridging, routing, or firewalling access to other networks
  • Interoperability with hosts running Linux, MS Windows among others
  • Possibility to set parameters such as MAC address,IP configuration or DHCP use thanks to the bootargs if using a boot firmware like U-Boot
GadgetFS
  • Provides User-Mode API
  • Each endpoint presented as single I/O file descriptor
  • Normal read() and write() calls
  • Async I/O supported
  • Configuration and descriptors written into files
Note that user mode gadget drivers do not neccesarily need to be licensed according to the GPL.
File-backed Storage
  • Implements the USB Mass Storage Class
  • Up to 8 disk drives can be set
  • Store file or block device is called the “backing storage”
  • Backing storage requires preparation
    • If a file is used, it must created with its desired size before launching the driver
    • If a block device, it must match host requirements (DOS partition for MS-Windows host)
  • The backing storage must not change while FBS is running, only the host should access it
Webcam
  • Acts as a composite USB Audio and Video Class device
  • Provides a user space API to process UVC control requests and stream video data
Serial Gadget
  • Useful for TTY style operation
  • Supports a CDC-ACM module option
MIDI
  • Exposes an ALSA MIDI interface
  • Both recording and playback support
GadgetZero
  • Useful to test device controller driver
  • Helps verify that the driver stack pass USB-IF (forUSB branding)
  • On the host side, useful to test USB stack
Design your own Gadget
3 main operations to consider
  • Hardware
  • Functional
  • Endpoints
  • First implement the register/unregister functions
    • usb_gadget_probe_driver
      • Resgistration of the usb_gadget_driver
      • Responsible for binding the gadget driver and powering upthe device
    • usb_gadget_unregister_driver
      • Responsible for unbinding the gadget from the functionaldriver and powering down the device
  • Then define callbacks hardware related
    • Fill usb_ep_ops and usb_gadget_ops
    • Not necessary to support all functions
  • Implement the control request handles (ep0)
    • Gadget driver handles only a part of it
    • The rest is routed to the class driver

  • Power Management requests
    • Comes from the PDC to the Gadget
    • The Gadget must pass the events to the class driver
  • Once enumeration is done, class driver requests usb_request structure for IN/OUT transfers
    • Gadget receives data in interrupt routine (OUT)    
      • Only when the expected amount is received the Gadgetcalls the complete function
    • Gadget sends data to the PDC (IN)
      • Wait send completion to inform the class driver
Demo: Hardware
BeagleBoard xM
  • ARM™ Cortex™-A8 1000 MHz
  • USB connectivity:
    • 4 host ports
    • 1 OTG port (used as device)
Demo: Software
  • Bootloader
    • U-boot 2011.12 r4
  • Kernel
    • 3.0.17 r115c   
  • Root filesystem
    • Console image
      • Custom recipe (lighttpd)
    • Additional modules
Conclusion
  • Easy to implement
  • Hardware independent
  • Scalability
  • Large panel of existing gadgets
  • Awareness of limitations
Appendix: References
  • Linux-USB Gadget API Framework: Generalpresentation. 
    Linux-USB Gadget API Framework
  • USB Gadget API for Linux: Full description of the API.
    https://archive.kernel.org/oldlinux/htmldocs/gadget/index.html
  • Essential Linux Device Drivers (SreekrishnanVenkateswaran) : General device driver bookcontaining a useful USB section.
  • Bootstrap Yourself with Linux-USB Stack (RajaramRegupathy): Very detailed and easy-to-read book aboutLinux-USB.
Other resources
  • USB Raw Gadget — The Linux Kernel documentation
  • USB Gadget API for Linux — The Linux Kernel documentation (Current newest version docs)
参考:
1, eLinux
https://elinux.org/images/8/81/Useful_USB_Gadgets_on_Linux.pdf

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

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

相关文章

Android系统 关于ntp的修改(网络时间同步)

一,现象: 1. NTP介绍 NTP:网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟&#x…

MC78L05ACDR2G线性稳压器芯片中文资料规格书PDF数据手册引脚图参数图片价格

产品概述: MC78L00A系列线性稳压器价格便宜,易于使用,适用于各种需要最高100mA的调节电源的应用。与大功率MC7800和MC78M00系列一样,这款稳压器也提供内部电流限制和高温关断,因此非常坚固耐用。在很多应用中&#xf…

4500万英镑!英国深化发展量子计算背后“内有乾坤”

内容来源:量子前哨(ID:Qforepost) 编辑丨慕一 编译/排版丨沛贤 深度好文:2200字丨15分钟阅读 近期,英国国家量子计算中心(NQCC)宣布量子计算实验台竞赛的结果,七家量子…

Python 编程中反斜杠 “\” 的作用:作为续行符和转义字符,处理文件路径和正则表达式时需特别注意。

🍉 CSDN 叶庭云:https://yetingyun.blog.csdn.net/ Python 中的反斜杠 \ 可以被用作续行符,它允许你将一行代码分成多行来书写,以提高代码的可读性。这在处理长字符串、复杂的数学表达式或其他需要多行布局的代码时非常有用。 使…

ios开发错误积累

1.xcode 下载模拟器报错 Could not download iOS 报错: 解决: 1、去官网下载自己需要 地址(https://developer.apple.com/download/all) 2、下载完成后,执行以下命令添加:xcrun simctl runtime add /路径…

【PyTorch】基础学习:一文详细介绍 load_state_dict() 的用法和应用

【PyTorch】基础学习:一文详细介绍 load_state_dict() 的用法和应用 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程&#…

【算法与数据结构】堆排序TOP-K问题

文章目录 📝堆排序🌠 TOP-K问题🌠造数据🌉topk找最大 🚩总结 📝堆排序 堆排序即利用堆的思想来进行排序,总共分为两个步骤: 建堆 升序:建大堆 降序:建小堆利…

R语言深度学习-6-模型优化与调试

本教程参考《RDeepLearningEssential》 这是本专栏的最后一篇文章,一路走来,大家应该都可以独立的建立一个自己的神经网络进行特征学习和预测了吧! 6.1 缺失值处理 在我们使用大量数据进行建模的时候,缺失值对模型表现的影响非常…

定位及解决OOM

一、定义 内存溢出:OutOfMemoryError,是指因内存不够,导致操作新对象没有剩余空间。会导致频繁fullgc出现STW从而导致性能下降。 内存泄漏:指用malloc或new申请了一块内存,但是没有通过free或delete将内存释放&#…

30.HarmonyOS App(JAVA)鸿蒙系统app多线程任务分发器

HarmonyOS App(JAVA)多线程任务分发器 打印时间,记录到编辑框textfield信息显示 同步分发,异步分发,异步延迟分发,分组任务分发,屏蔽任务分发,多次任务分发 参考代码注释 场景介绍 如果应用的业务逻辑比…

LLM之Alpaca:深入了解大模型Alpaca

博客首发地址:LLM之Alpaca:深入了解大模型Alpaca - 知乎 官方链接:https://crfm.stanford.edu/2023/03/13/alpaca.html官方Git:tatsu-lab/stanford_alpaca官方模型:https://huggingface.co/tatsu-lab/alpaca-7b-wdiff…

Android Studio 打包 Maker MV apk 详细步骤

一.使用RPG Make MV 部署项目,获取项目文件夹 这步基本都不会有问题: 二.安装Android Studio 安装过程参考教材就行了: https://blog.csdn.net/m0_62491877/article/details/126832118 但是有的版本面板没有Android的选项(勾…

龙芯新世界系统(安同AOCS OS)安装Cinnamon桌面最新版6.0.4

龙芯的新世界系统安同AOCS OS是十分优秀的操作系统,处于纯社区方式运行,她的各组件更新得很及时,很多组件都处于最新的状态,给我们安装使用最新的开源软件提供了很好的基础。由于本人一直使用Cinnamon桌面环境,各方面都…

LM2903BIDR比较器芯片中文资料规格书PDF数据手册参数引脚图功能封装尺寸图

产品概述: M393B 和 LM2903B 器件是业界通用 LM393 和 LM2903 比较器系列的下一代版本。下一代 B 版本比较器具有更低的失调电压、更高的电源电压能力、更低的电源电流、更低的输入偏置电流和更低的传播延迟,并通过专用 ESD 钳位提高了 2kV ESD 性能和输…

【教学类-44-07】20240318 0-9数字描字帖 A4横版整页(宋体、黑体、文鼎虚线体、print dashed 德彪行书行楷)

背景需求: 前文制作了三种字体的A4横版数字描字帖 【教学类-44-06】20240318 0-9数字描字帖 A4横版整页(宋体、黑体、文鼎虚线体)-CSDN博客【教学类-44-06】20240318 0-9数字描字帖 A4横版整页(宋体、黑体、文鼎虚线体)https://…

stable diffusion webui 搭建和初步使用

官方repo: GitHub - AUTOMATIC1111/stable-diffusion-webui: Stable Diffusion web UI 关于stable-diffusion的介绍:Stable Diffusion|图解稳定扩散原理 - 知乎 一、环境搭建和启动 准备在容器里面搞一下 以 ubuntu22.04 为基础镜像,新建…

UnityShader(十六)凹凸映射

前言: 纹理的一种常见应用就是凹凸映射(bump mapping)。凹凸映射目的就是用一张纹理图来修改模型表面的法线,让模型看起来更加细节,这种方法不会改变模型原本的顶点位置(也就是不会修改模型的形状&#xf…

数据结构之顺序存储-顺序表的基本操作c/c++(创建、初始化、赋值、插入、删除、查询、替换、输出)

学习参考博文&#xff1a;http://t.csdnimg.cn/Qi8DD 学习总结&#xff0c;同时更正原博主在顺序表中插入元素的错误。 数据结构顺序表——基本代码实现&#xff08;使用工具&#xff1a;VS2022&#xff09;&#xff1a; #define _CRT_SECURE_NO_WARNINGS #include <stdi…

gitlab cicd问题整理

1、docker设置数据目录&#xff1a; 原数据目录磁盘空间不足&#xff0c;需要更换目录&#xff1a; /etc/docker/daemon.json //写入/etc/docker/daemon.json {"data-root": "/data/docker" } 2、Dockerfile中ADD指令不生效 因为要ADD的文件被.docker…

【计算机网络】什么是http?

​ 目录 前言 1. 什么是HTTP协议&#xff1f; 2. 为什么使用HTTP协议&#xff1f; 3. HTTP协议通信过程 4. 什么是url&#xff1f; 5. HTTP报文 5.1 请求报文 5.2 响应报文 6. HTTP请求方式 7. HTTP头部字段 8. HTTP状态码 9. 连接管理 长连接与短连接 管线化连接…