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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

PXVDI企业级PVE免费桌面虚拟化部署教程ProxmoxVE

什么是PXVDI? PXVDI是一款基于Proxmox VE为底层的可商用的免费云桌面套件。对熟悉PVE的人来说,这点非常的点赞。首先是PVE是免费的,其次PVE的免费云桌面方案也极为少数。 根据官方提出的价格清单,免费版和商业版在功能上主要的区…

使用CURL命令确定Access-Control-Allow-Origin问题

一、问题描述 有前端小伙伴反馈ajax请求遇到跨域问题,也让后端小伙伴设置了跨域允许,但诡异的事情是在前端小伙伴的微信开发者工具中Network headers中看到了两行:Access-Control-Allow-Origin,其中居然出现了:“Acce…

51单片机—DS18B20温度传感器

目录 一.元件介绍及原理 二,应用:DS18B20读取温度 一.元件介绍及原理 1.元件 2.内部介绍 本次元件使用的是单总线 以下为单总线的介绍 时序结构 操作流程 本次需要使用的是SKIP ROM 跳过, CONVERT T温度变化,READ SCRATCHPAD…

Linux:系统初始化,内核优化,性能优化(2)

优化ssh协议 Linux:ssh配置_ssh配置文件-CSDN博客https://blog.csdn.net/w14768855/article/details/131520745?ops_request_misc%257B%2522request%255Fid%2522%253A%2522171068202516800197044705%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fb…

redis 常见的异常

目录 一、缓存穿透 1、概念 解决方案 (1)布隆过滤器 (2)、缓存空对象 二、缓存雪崩 1、概念 解决方案 (1)redis高可用 (2)限流降级 (3)数据预热 一、缓存穿透 1、概念 缓…

JavaWeb后端——分层解耦 IOC DI

分层/三层架构概述 三层架构:Controller、Service、Dao 解耦/IOC&DI概述 分层解耦 容器称为:IOC容器/Spring容器 IOC 容器中创建,管理的对象,称为:bean 对象 IOC&DI入门 实现 IOC&DI 需要的注解&#…

【MySQL】 MySQL的内置函数——日期函数、字符串函数、数学函数、聚合函数、其他函数

文章目录 MySQL1. 日期函数1.1 查看时间1.2 对时间进行计算 2. 字符串函数2.1 字符串查找2.2 字符串修改显示 3. 数学函数4. 聚合函数5. 其他函数 MySQL 1. 日期函数 在MySQL中,提供了多种时间函数供我们使用,其中包括用于查看时间的函数和计算日期的函数…

基于java+springboot+vue实现的高校教师工作量管理系统(文末源码+Lw+ppt)23-451

摘 要 高校教师工作量管理系统采用B/S架构,数据库是MySQL。网站的搭建与开发采用了先进的java进行编写,使用了springboot框架。该系统从两个对象:由管理员和教师来对系统进行设计构建。主要功能包括:个人信息修改,对…

Jmeter文件上传不成功问题

前言 最近好忙呀,项目上线然后紧接着又客户培训了,由于项目有个模块全是走配置的,所以导致问题不断,近期要培训为了保障培训时客户同时操作的情况,所以把我从功能端抽出来做压测了,之前安排了2个同事写压测…

数据结构的基本框架以及泛型

目录 集合框架复杂度大O的渐进表示法 装包(箱)或者拆包(箱)装包拆包 泛型泛型的上界泛型方法求最大值 集合框架 Java的集合框架,Java Collection Framework 又被称为容器container, 定义在java.util包下的一组 interfaces 和其实现类 classes interface: 接口 abstracb class…

基于Linux内核的socket编程(TCP)的C语言示例

原文地址&#xff1a;https://www.geeksforgeeks.org/socket-programming-cc/ 服务端&#xff1a; #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h>#…

【鸿蒙HarmonyOS开发笔记】开发小技巧之Blank组件与layoutWeight属性的使用

Blank组件 Blank可作为Column和Row容器的子组件 该组件不显示任何内容&#xff0c;并且会始终充满容器主轴方向上的剩余空间&#xff0c;效果如下&#xff1a; Entry Component struct BlankPage {build() {Column({ space: 50 }) {Row() {Image($r(app.media.icon_bluetoot…

uniapp修改头像,选择图片

一、页面效果 二、手机上的效果 使用过的实例&#xff1a; 手机上就会显示类似如下&#xff1a; 三、代码 <view class"cleaner-top" click"chooseImg"><view class"cleaner-avatar"><image :src"imgArr" mode"…

蚁群算法实现 - 全局路径规划算法

参考博客&#xff1a; &#xff08;1&#xff09;【人工智能】蚁群算法(密恐勿入) &#xff08;2&#xff09;计算智能——蚁群算法 &#xff08;3&#xff09;蚁群算法(实例帮助理解) &#xff08;4&#xff09;【数之道 04】解决最优路径问题的妙招-蚁群ACO算法 &#xff08;…

《A ConvNet for the 2020s》阅读笔记

论文标题 《A ConvNet for the 2020s》 面向 2020 年代的 ConvNet 作者 Zhuang Liu、Hanzi Mao、Chao-Yuan Wu、Christoph Feichtenhofer、Trevor Darrell 和 Saining Xie 来自 Facebook AI Research (FAIR) 和加州大学伯克利分校 初读 摘要 “ViT 盛 Conv 衰” 的现状&…

EI Scopus检索 | 第二届大数据、物联网与云计算国际会议(ICBICC 2024) |

会议简介 Brief Introduction 2024年第二届大数据、物联网与云计算国际会议(ICBICC 2024) 会议时间&#xff1a;2024年12月29日-2025年1月1日 召开地点&#xff1a;中国西双版纳 大会官网&#xff1a;ICBICC 2024-2024 International Conference on Big data, IoT, and Cloud C…

视频基础知识(一) 视频编码 | H.26X 系列 | MPEG 系列 | H.265

文章目录 一、视频编码二、 H.26X 系列1、H.2612、H.2633、H.2643.1 I帧3.2 P帧3.3 B帧 4、H.265 三、 MPEG 系列1、MPEG-12、MPEG-23、MPEG-44、MPEG-7 &#x1f680; 个人简介&#xff1a;CSDN「博客新星」TOP 10 &#xff0c; C/C 领域新星创作者&#x1f49f; 作 者&…

OSPF协议全面学习笔记

作者&#xff1a;BSXY_19计科_陈永跃 BSXY_信息学院 注&#xff1a;未经允许禁止转发任何内容 OSPF协议全面学习笔记 1、OSPF基础2、DR与BDR3、OSPF多区域4、虚链路Vlink5、OSPF报文6、LSA结构1、一类/二类LSA&#xff08;Router-LSA/Network-LSA&#xff09; 更新完善中... 1、…

STM32的USART能否支持9位数据格式话题

1、问题描述 STM32L051 这款单片机。平常的 USART 串口传输是 8 位数据&#xff0c;但是他的项目需要用串口传输 9 位数据。当设置为 8 位数据时&#xff0c;串口响应中断正常。但是&#xff0c;当设置为 9 位数据时&#xff0c;串口就不产生中断了。USART2 的 ISR 寄存器 RXN…

Monorepo 解决方案 — 基于 Bazel 的 Xcode 性能优化实践

背景介绍 书接上回《Monorepo 解决方案 — Bazel 在头条 iOS 的实践》&#xff0c;在头条工程切换至 Bazel 构建系统后&#xff0c;为了支持用户使用 Xcode 开发的习惯&#xff0c;我们使用了开源项目 Tulsi 作为生成工具&#xff0c;用于将 Bazel 工程转换为 Xcode 工程。但是…