How to fix Usually Slightly Broken devices and drivers?

Similar documents
Debugging Usually Slightly Broken Devices and Drivers

Understand USB (in Linux)

I/O Management Software. Chapter 5

Universal Serial Bus - USB 2.0

ARM Cortex core microcontrollers

Introduction USB Pisanje gonilnika USB. USB drivers

Introduction to USB. Alan Ott SCaLE 15x March 2-5, 2017

RINGDALE USB (UNIVERSAL SERIAL BUS) HID RELAY CONTROLLER (1543)

Driving Me Nuts Writing a Simple USB Driver

Storage Systems. NPTEL Course Jan K. Gopinath Indian Institute of Science

Device Wire Adapter (DWA) Test Specification. Designed using the Certified Wireless USB Base Specification, Revision 1.0

Application Note: AN00136 USB Vendor Specific Device

USB Complete. The Developer's Guide Fifth Edition. Jan Axelson. Lakeview Research LLC Madison, WI 53704

BOOTSTRAP YOURSELF WITH LINUX-USB STACK: DESIGN, DEVELOP, DEBUG, AND VALIDATE EMBEDDED USB

Virtualised USB Fuzzing using QEMU and Scapy

Guide to Kernel Driver Integration in Linux for Huawei Modules. Issue 2.0. Date

Testing and Debugging

UC20 WinCE USB Driver

Kernel USB Gadget Configfs Interface. Matt Porter Linaro

ECE 471 Embedded Systems Lecture 30

Guide to Kernel Driver Integration in Android for Huawei Modules. Issue Date

Guide to Kernel Driver Integration in Android for Huawei Modules. Issue Date

LibUSB - Create a Solution Without the Class Struggle

Application Note. 32-bit Cortex -M0 MCU NuMicro Family. Application Note of NUVOTON 32-bit NuMicro Family

Interrupt transfers & USB 2.0 & USB 3.0. Group Members Mehwish Awan Mehwish Kiran

Universal Serial Bus Host Stack User s Manual V3.41

Serial Communications

USB System Design in Sitara Devices Using Linux. [Part 4]: Verify USB in Linux sysfs Bin Liu (EP, Processors)

Introducing Class-Level Decoding Video See a video demonstration of the new real-time class-level decoding feature of the Data Center Software.

Serial Communications

QNX Momentics DDK. Universal Serial Bus (USB) Devices. For QNX Neutrino or QNX , QNX Software Systems Ltd.

Hacking the Kinect. Created by lady ada. Last updated on :21:33 AM UTC

Design Of Linux USB Device Driver For LPC2148 Based Data Acquisition System Including GSM.

Introduction to USB/LPC23xx

USB INTERFACE AND DRIVER Mentor: Dr. Yann Hang Lee Team Members: Jubin Mehta, Koshik Samota (jmehta3,

David Harrison, Design Engineer for Model Sounds Inc.

Bulk endpoints transport data whenever required and reliably; bulk data is acknowledged and therefore fault tolerant.

Revealing Embedded Fingerprints: Deriving Intelligence from USB Stack Interactions

Universal Serial Bus Device Class Definition for Mass Storage Devices

QNX Momentics DDK. Universal Serial Bus (USB) Devices. For QNX Neutrino or QNX , QNX Software Systems GmbH & Co. KG.

CLD SC58x CDC Library v.1.00 Users Guide Users Guide Revision For Use With Analog Devices ADSP-SC58x Series Processors. Closed Loop Design, LLC

MCCI Universal Serial Bus Windows Kernel Bus Interface for USB 3.0 Streams Device Drivers

ADVANCED OPERATING SYSTEMS USB in a microkernel based operating system

USB System Design in Sitara Devices Using Linux. [Part 6]: Use USB in Device Mode Bin Liu (EP, Processors)

Certified Wireless USB Wire Adapter Model

USB INTERFACE SPECIFICATION

MCCI Universal Serial Bus Windows Kernel Bus Interface for USB 3.0 Streams Device Drivers

S1R72U06 Technical Manual

CLD BF70x CDC Library v.1.3 Users Guide Users Guide Revision 1.3. For Use With Analog Devices ADSP-BF70x Series Processors. Closed Loop Design, LLC

QEMU AS A USB MTP RESPONDER

USB BF70x Audio 1.0 Library v.1.2 Users Guide Users Guide Revision 1.3. For Use With Analog Devices ADSP-BF70x Series Processors

Application Note AN_164. Vinculum-II USB Slave. Writing a Function Driver

USB 3.0 Software Architecture and Implementation Issues. Terry Moore, CEO MCCI Corporation

Reliable Linux Wireless - Techniques for Debugging Wireless Module Integrations STEVE DEROSIER / CAL-SIERRA CONSULTING

Diploma Thesis. USB for DROPS. Gerd Grießbach Dresden University of Technology Operating System Group

An Introduction to Universal Serial Bus

PL-25A1 Hi-Speed USB Host-to-Host Bridge Controller (Chip Revision B) Product Datasheet

JSR80 API Specification

Universal Serial Bus Device Class Definition For Content Security Devices

Microprocessors LCD Parallel Port USB Port

Fuzzing the USB in your devices

AN2554. Creating a Multi-LUN USB Mass Storage Class Device Using the MPLAB Harmony USB Device Stack INTRODUCTION CONTROL TRANSFERS

Universal Serial Bus Device Class Definition Billboard Devices

Peripheral-side USB support for NetBSD

USB Integration of Transceivers

Human Interface Devices: Using Control and Interrupt Transfers

Introducing Android Open Accessories and ADK. Jeff Brown, Erik Gilling, Mike Lockwood May 10, 2011

Storage Systems Dr. K. Gopinath Department of Computer Science and Engineering Indian Institute of Science, Bangalore

Citrix IMA Service Error Codes

USB BF70x Bulk Library v.1.1 Users Guide Users Guide Revision 1.1. For Use With Analog Devices ADSP-BF70x Series Processors. Closed Loop Design, LLC

More on IO: The Universal Serial Bus (USB)

Future Technology Devices International Ltd. Application Note AN_168. Vinculum-II USB Slave. Customizing an FT232 Device

Hubs: the Link between Devices and the Host

Challenges with Userspace USB Embedded Device Interfacing

Cross-Domain Development Kit XDK110 Platform for Application Development

Universal Serial Bus Device Class Definition for Printing Devices

AN USB HID Intermediate with PSoC 3 and PSoC 5LP. Contents. 1 Introduction

Open Universal Serial Bus Driver Interface (OpenUSBDI) Specification

Course 10: Interfaces Agenda

CM6327A USB Single-Chip Audio Solution for Mono Microphone

Exploiting USB/IP in Linux

Vinod Koul, Sanyog Kale Intel Corp. MIPI SoundWire Linux Subsystem: An introduction to Protocol and Linux Subsystem

DRIVER MODEL ULB Darmstadt

USB 3.0 A Cost Effective High Bandwidth Solution for FPGA Host Interface Introduction

Creating a USB Audio Device on a PIC32 MCU Using MPLAB Harmony

8086 Interrupts and Interrupt Responses:

SuSE Labs / Novell.

USB. The Universal Serial Bus. Most of the material is taken from Jan Axelsson: USB Complete, third edition Lakeview Research ISBN:

Logitech hidpp 1.0 excerpt for public release

Introduction PCI Interface Booting PCI driver registration Other buses. Linux Device Drivers PCI Drivers

PL-2305 USB to Printer Bridge Controller (Chip Rev I) Product Datasheet

IAR PowerPac USB User Guide

Open Universal Serial Bus Driver Interface (OpenUSBDI) Specification

PLX USB Development Kit

USB Interrupt Transfer Example PSoC 3 / PSoC 5

USB Feature Specification: Shared Endpoints

Future Technology Devices International Ltd. Application Note AN_172. Vinculum-II. Using the USB Slave Driver

IVI-6.2: VISA Interoperability Requirements for USBTMC Specification

13th ANNUAL WORKSHOP 2017 VERBS KERNEL ABI. Liran Liss, Matan Barak. Mellanox Technologies LTD. [March 27th, 2017 ]

Ellisys USB 2.0 Protocol Analyzer

Transcription:

How to fix Usually Slightly Broken devices and drivers? Krzysztof Opasiak Samsung R&D Institute Poland

Agenda USB basics Plug & Play Plug & do what I want Plug & tell me more Summary Q & A 1

This presentation USB is about: USB devices management USB drivers policy modification USB traffic sniffing is NOT about: Kernel code debugging Using kgdb Using tracepoints Using JTAG 2

USB basics

What USB is about? It's about providing services! Storage Printing Ethernet Camera Any other 4

Endpoints Device may have up to 31 endpoints (including ep0) Each of them gets a unique endpoint address Endpoint 0 may transfer data in both directions All other endpoints may transfer data in one direction: IN Transfer data from device to host OUT Transfer data from host to device 5

Endpoint types Control Bi-directional endpoint Used for enumeration Can be used for application Interrupt Transfers a small amount of low-latency data Reserves bandwidth on the bus Used for time-sensitive data (HID) 6

Endpoint types Bulk Used for large data transfers Used for large, time-insensitive data (Network packets, Mass Storage, etc). Does not reserve bandwidth on bus, uses whatever time is left over Isochronous Transfers a large amount of time-sensitive data Delivery is not guaranteed (no ACKs are sent) Used for Audio and Video streams Late data is as good as no data Better to drop a frame than to delay and force a re-transmission 7

USB device 8

USB descriptors 9

USB classes 00h Device Use class information in the Interface Descriptors 01h Interface Audio 02h Both Communications and CDC Control 03h Interface HID (Human Interface Device) 05h Interface Physical 06h Interface Image 07h Interface Printer 08h Interface Mass Storage 09h Device Hub 0Ah Interface CDC-Data 0Bh Interface Smart Card 0Dh Interface Content Security 0Eh Interface Video 0Fh Interface Personal Healthcare 10h Interface Audio/Video Devices 11h Device Billboard Device Class DCh Both Diagnostic Device E0h Interface Wireless Controller EFh Both Miscellaneous FEh Interface Application Specific FFh Both Vendor Specific 10

USB descriptors 11

USB device example dmesg & lsusb DEMO 12

Plug & Play

Step by step Plug in device Detect Connection Set address Get device info Choose configuration Choose drivers for interfaces Use it ;) 14

Set address On plug-in device uses default address 0x00 Only one device is being enumerated at once Hosts assigns unique address for new device Usually it's just the next one (dev.addr = addr++) 15

USB Device Details 16

Which configuration is the most suitable? Do we have enough power for it (bmaxpower)? Does it have at least one interface? If the device has only one config The first one! If the device has multiple configs The first one which first interface class is different than Vendor Specific All interfaces of chosen configuration become available so let's use them 17

What USB driver really is? Piece of kernel code (often a module) struct usb_driver Usually it provides something to userspace (network interface, block device, tty, etc.) Implementation of some communication protocol so it's a little bit equivalent of web browser, ssh client etc. 18

How driver is chosen? Kernel has a list of registered drivers Each driver has an array of acceptable device IDs Kernel goes through the list and if some id matches calls driver's probe() If driver is not ther udev may load it's module based on alias Module aliases are generated based on acceptable device IDs 19

USB device identity struct usb_device_id { /* which fields to match against? */ u16 match_flags; /* Used for product specific matches */ u16 idvendor; u16 idproduct; u16 bcddevice_lo; u16 bcddevice_hi; /* Used for device class matches */ u8 bdeviceclass; u8 bdevicesubclass; u8 bdeviceprotocol; #define USB_DEVICE_ID_MATCH_VENDOR 0x0001 #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002 #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004 #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008 #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010 #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020 #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040 #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080 #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100 #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 #define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400 }; /* Used for interface class matches */ u8 binterfaceclass; u8 binterfacesubclass; u8 binterfaceprotocol; /* * Used for vendor -specific * interface matches */ u8 binterfacenumber; /* not matched against */ kernel_ulong_t driver_info;. 20

USB Host Big Picture 21

Plug & do what I want

Automation is good but not always: Too many devices allowed Only part of device functionality is needed Wrong config chosen No matching driver found Wrong driver bound 23

What kernel gives us? SysFS infrastructure Device Information Device Management Drivers Information Device node Device Information Device Communication Used by libusb 24

/sys/bus/usb/devices/ demystified usbx X ID of host controller on your machine X-A.B.C X HCD ID (as above) A.B.C Physical path to port where your USB device is connected X-A.B.C:Y.Z X-A.B.C Device path (as above) Y Active configuration Z binterfacenumber 25

Limit number of allowed devices Let's use USB Device Authorization! Each USB device has authorized attribute in sysfs directory Each HCD (usbx) has authorized_default attribute If authorized == 0 then device is left in unconfigured state When authorized, drivers probed automatically Can be automated using usbguard project 26

Device Authorization HOWTO # Choose USB bus $ cd /sys/bus/usb/devices/usb$x # Stop authorizing devices by default $ echo 0 > authorized_default # Connect new device, do other stuff # Authorize device of your choice $ cd /sys/bus/usb/devices/$dev_dir $ echo 1 > authorized 27

Use only subset of functionality Let's use USB Interface Authorization! (v4.4+) Each USB interface has authorized attribute in sysfs directory Each HCD (usbx) has authorized_default attribute If authorized == 0 then drivers are not allow to bind Driver probing has to be triggered manually after authorization 28

Interface Authorization HOWTO # Choose USB bus $ cd /sys/bus/usb/devices/usb$x # Stop authorizing devices by default $ echo 0 > interface_authorized_default # Authorize interface of your choice $ cd /sys/bus/usb/devices/$interface_dir $ echo 1 > authorized # Trigger driver search $ echo -n $INTERFACE_DIR \ > /sys/bus/usb/drivers_probe 29

Change configuration Configuration is chosen by kernel Choice is based on hardcoded heuristic But we may change it: $ cd $DEV_DIR # Check current config $ cat bconfigurationvalue 1 # Set new one $ echo $NEW_CONFIG > bconfigurationvalue 30

Add device ID to driver Sometimes you get a device which is compatible with another one But has a little bit different VID:PID info This new VID:PID is not listed in driver's id table This means that your driver is not going to bind to it:( 31

Dynamic IDs - formats VID+PID: echo $VID $PID VID+PID+Intf Class: echo $VID $PID $IntfClass VID+PID+Intf Class+dev_info: echo $VID $PID $IntfClass $RefVID $RefPID 32

Dynamic IDs - formats VID+PID: echo $VID $PID VID+PID+Intf Class: echo $VID $PID $IntfClass VID+PID+Intf Class+dev_info: echo $VID $PID $IntfClass $RefVID $RefPID All umbers interpreted as HEX! 32

Dynamic IDs - handling Add new device ID $ echo $VID $PID > \ /sys/bus/usb/ drivers/ $DRV_NAME/ new_id Show the list of dynamic IDs $ cat /sys/bus/usb/ drivers/ $DRV_NAME/ new_id Remove previously added device ID $ echo $VID $PID > \ /sys/bus/usb/ drivers/ $DRV_NAME/ remove_id 33

Bind/Unbind particular interface Check which driver is bound $ readlink \ /sys/bus/usb/ devices/ $INTERFACE_DIR/ driver Unbind driver $ echo -n $INTERFACE_DIR > \ /sys/bus/usb/ drivers/ $DRV_NAME/ unbind Bind driver (device id must match) $ echo -n $INTERFACE_DIR > \ /sys/bus/usb/ drivers/ $DRV_NAME/ unbind 34

Let's try this DEMO 35

Plug & tell me more

USB bus USB is a Host-controlled bus Nothing on the bus happens without the host first initiating it. Devices cannot initiate any communication. The USB is a Polled Bus. The Host polls each device, requesting data or sending data. 37

USB transfer vs transaction Transaction Delivery of data to endpoint Limited by wmaxpacketsize Transfer One or more transactions May be large or small Completion conditions 38

USB Request Block struct urb { struct list_head urb_list; Kernel provides hardware independent API for drivers This API is asynchronous URB is a kind of envelope for USB data }; struct usb_device *dev; unsigned int pipe; int status; unsigned int transfer_flags; void *transfer_buffer; u32 transfer_buffer_length; u32 actual_length; unsigned char *setup_packet; void *context; usb_complete_t complete; 39

Typical USB driver Where? What? probe() disconnect() complete() related to other subsystem check device + allocate resources release resources check status, get data, resubmit depends on susbsys 40

Typical bugs? Missing descriptors No error path on missing entities No correct error handling in complete() Malformed packets 41

HW USB sniffers - Commercial 2850$ 1400$ 42

HW USB sniffers - Open Hardware about 100$ 43

USBMon Kind of logger for URB related events: submit() complete() submit_error() Text interface Binary Interface One instance for each USB bus 44

submit vs complete Data in URB buffer may is not always valid Validity depends on transfer results And on endpoint direction: IN OUT submit() NO YES complete() YES NO 45

Good old friend Wireshark 46

Let's catch sth DEMO 47

Summary

Summary USB descriptors are a device ID You can get them using lsusb Drivers declares list of compatible devices USB devices are manageable via SysFS: Change active config Add new device to driver Black list device Bind/Unbind driver Device/Interface authorization Drivers communicate using URBs In some cases USBMon can be used instead of expensive HW analyzers 49

Q & A

Thank you! Krzysztof Opasiak Samsung R&D Institute Poland +48 605 125 174 k.opasiak@samsung.com 51