USB - USB Gadget on Linux

2024-03-18 02:12
文章标签 linux usb gadget

本文主要是介绍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

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



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

相关文章

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级

【Linux】应用层http协议

一、HTTP协议 1.1 简要介绍一下HTTP        我们在网络的应用层中可以自己定义协议,但是,已经有大佬定义了一些现成的,非常好用的应用层协议,供我们直接使用,HTTP(超文本传输协议)就是其中之一。        在互联网世界中,HTTP(超文本传输协议)是一个至关重要的协议,他定义了客户端(如浏览器)与服务器之间如何进行通信,以交换或者传输超文本(比如HTML文档)。

如何编写Linux PCIe设备驱动器 之二

如何编写Linux PCIe设备驱动器 之二 功能(capability)集功能(capability)APIs通过pci_bus_read_config完成功能存取功能APIs参数pos常量值PCI功能结构 PCI功能IDMSI功能电源功率管理功能 功能(capability)集 功能(capability)APIs int pcie_capability_read_wo