USB Drivers

Anil Kumar Pugalia
Anil Kumar PugaliaLinux Geek and Open Source Hardware & Software Freak, Corporate Trainer, Entrepreneur in Automation
© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Drivers
2© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
USB Evolution
USB Subsystem: Host & Gadget
Understanding of USB Protocol
Writing USB Host Drivers
Writing USB Gadget Drivers
3© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Prologue
What was USB designed for?
A Unified Bus for Slow Devices
So, design based on Master-Slave concept
USB (Host) Controller is the “Single Master”
UHC polls the Slave Peripherals / Devices
Later Additions
High Speed Specifications
Bandwidth Allocation Ability
But even today, the polling continues
4© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Device Driver Types
2 Types: Both written for the Device
USB Host (Device) Driver
Runs on Host (Master)
Drives the USB Device (Slave)
USB Gadget (Device) Driver
Runs on the USB Gadget / Device (Slave)
Responds to a Host (Master)
Pre-requisite: Gadget is Linux based
5© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Subsystem View
FS
Layer
USB Host Device Drivers ...
USB Core
USB Host / OTG Controller Driver(s)
...
TTY
Layer
Char
Layer
Net
Layer
Block
Layer
Kernel Space
User Applications
USB Devices ...
Hardware Space
User Space
usbfs
User Mode
Drivers
USB Host / OTG Controller
6© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Subsystem
UHC Driver (uhci, ohci, ehci, otg)
Hardware-specific USB Host Controller driver
Hides all the hardware details from the layers above
Provides a uniform interface to USB Core
USB Core Module (usbcore)
Provides the generic USB Protocol APIs for the kernel, in general
By interfacing with the underlying UHC driver
USB File System Module (usbfs)
Uses USB Core to provide Kernel Windows & USB Devices as entries under /sys
Enables writing User Mode USB Drivers
USB Host Device Driver
USB Device specific Driver
Interfaces with the corresponding USB Device through the USB Core
Provides interface to the User Space through the relevant Vertical(s)
7© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Subsystem View
USB Gadget Device Drivers ...
USB Composite
USB Device / OTG Controller Driver
Kernel Space
USB Host
Hardware Space
USB Device / OTG Controller
Horizontal Layers
Vertical Layers
Peripherals
User Applications
User Space
8© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Subsystem
UDC Driver (*udc)
Hardware-specific USB Device Controller driver
Hides all the hardware details from the layers above
Provides a uniform interface to USB Composite
USB Composite Module (libcomposite)
Provides the generic USB Protocol APIs for the kernel, in general
By interfacing with the underlying UDC driver
USB Gadget Device Driver
USB Device specific Driver
Interfaces with the USB Host through the USB Composite
Exposes peripherals &/or (virtual) functionalities to the Host
May provide (virtual) functionalities to the User Space through the relevant
Layer(s) / Driver(s)
9© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host & sysfs
Command: /sbin/lspci
<dom>:<bus>:<dev>:<fn> for <usbhubid>
Kernel Window
/sys/devices/pci0000:00/<usbhubid>/usb<hub>
usb_device fields
roothub-hubport:config.interface
usb_interface fields
PCI USB HC functions -> USB buses
/sys/kernel/debug/usb/devices
10© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Device Overview (Protocol)
Device
Config Interface
Endpoint
...
Endpoint
Endpoint
...
Interface
USB
Driver
USB
Driver
...
11© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Device Endpoints
Also called Pipes
Direction
OUT (host->device)
IN (device->host)
Four Types
Control
Interrupt
Bulk
Isochronous
12© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Device Driver
13© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Driver Data Structures
Header: <linux/usb.h>
Data Structures
struct usb_device
struct usb_host_config
struct usb_interface
interface_to_usbdev
struct usb_host_endpoint
struct usb_endpoint_descriptor
14© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Core Functionality
USB Host Device Driver Registration
USB Host Device Hot Plugability
probe: Vertical Registration
disconnect: Vertical Unregistration
USB Transfers through URBs
15© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Device Driver Registration
Header: <linux/usb.h>
Data Structure: struct usb_driver
struct module *owner
const char *name
const struct usb_device_id *id_table
int (*probe)(struct usb_interface *, struct usb_device_id *)
int (*disconnect)(struct usb_interface *)
APIs
int usb_register(struct usb_driver *);
int usb_deregister(struct usb_driver *);
16© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Host Device Hot-plug-ability
Callback probe
int usb_register_dev(intf, class);
Callback disconnect
int usb_deregister_dev(intf, class);
Other Useful APIs (Header: <linux/usb.h>
void usb_set_intfdata(intf, void *data);
void *usb_get_intfdata(intf);
17© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Request Block
Header: <linux/usb.h>
Data Structure: struct urb
struct usb_device *dev
unsigned int pipe
unsigned int transfer_flags
void *transfer_buffer
int transfer_buffer_length
usb_complete_t complete
int actual_length
int status
Pipe type specific fields
18© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
URB Operations
Header: <linux/usb.h>
URB Storage
usb_alloc_urb(int iso_pkts, gfp_t flags);
usb_free_urb(struct urb *);
Populating the URB
usb_fill_control_urb(urb, dev, pipe, req, buf, len, fn, ctxt);
usb_fill_int_urb(urb, dev, pipe, buf, len, fn, ctxt, interval);
usb_fill_bulk_urb(urb, dev, pipe, buf, len, fn, ctxt);
Using the URB
usb_submit_urb(struct urb *, gfp_t flags);
usb_unlink_urb(struct urb *) / usb_kill_urb(struct urb *);
19© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
URB Operations' Wrappers
Header: <linux/usb.h>
APIs
usb_control_msg(dev, pipe, req, req_type, value,
index, data, size, timeout);
usb_interrupt_msg(dev, pipe, data, len, &act_len,
timeout);
usb_bulk_msg(dev, pipe, data, len, &act_len,
timeout);
20© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Device Driver
21© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Data Structures
Header: <linux/usb/composite.h>
Data Structures
struct usb_device_descriptor
struct usb_gadget_strings
struct usb_string
struct usb_configuration
struct usb_descriptor_header
struct usb_interface_descriptor
struct usb_endpoint_descriptor
22© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Composite Functionality
USB Gadget Device Driver Registration
USB Gadget Device Creation
bind: Gadget Setup
unbind: Gadget Cleanup
USB Gadget Endpoint Interactions
23© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Driver Registration
Header: <linux/usb/composite.h>
Data Structure: struct usb_composite_driver
const char *name
const struct usb_device_descriptor *dev
struct usb_gadget_strings **strings
enum usb_device_speed max_speed
int (*bind)(struct usb_composite_dev *cdev)
int (*unbind)(struct usb_composite_dev *cdev)
APIs
int usb_composite_probe(struct usb_composite_driver *driver);
void usb_composite_unregister(struct usb_composite_driver *driver);
24© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Device Creation
Header: <linux/usb/composite.h>
Callback bind
int usb_string_ids_tab(struct usb_composite_dev *c,
struct usb_string *str);
int usb_add_config_only(comp_dev, usb_cfg)
int usb_add_function(usb_cfg, usb_fn);
Callback unbind
int usb_put_function(usb_fn);
// int usb_remove_function(usb_cfg, usb_fn);
25© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Function Addition
Header: <linux/usb/composite.h>
Callbacks in struct usb_function:
int (*bind)(struct usb_configuration *c, struct usb_function *f);
void (*unbind)(struct usb_configuration *c, struct usb_function *f);
void (*free_func)(struct usb_function *f);
int (*set_alt)(struct usb_function *f, unsigned interface, unsigned alt); // Must
int (*get_alt)(struct usb_function *f, unsigned interface);
void (*disable)(struct usb_function *f); // Must
int (*setup)(struct usb_function *f, const struct usb_ctrlrequest *ctrlreq);
...
26© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Descriptors Addition
Header: <linux/usb/gadget.h>
Through <linux/usb/composite.h>
Typical invocations through function's bind / unbind/free_func:
int usb_interface_id(struct usb_configuration *c, struct usb_function *f);
int usb_string_id(struct usb_composite_dev *c);
struct usb_ep *usb_ep_autoconfig(struct usb_gadget *gadget,
struct usb_endpoint_descriptor *usb_ep);
void usb_ep_autoconfig_reset(struct usb_gadget *gadget);
int usb_assign_descriptors(struct usb_function *f,
struct usb_descriptor_header **fs,
struct usb_descriptor_header **hs,
struct usb_descriptor_header **ss);
void usb_free_all_descriptors(struct usb_function *f);
27© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Endpoint Interactions
Header: <linux/usb/gadget.h>
Through <linux/usb/composite.h>
Typical (Endpoint Interaction) APIs for set_alt / disable:
int usb_ep_enable(struct usb_ep *ep);
int usb_ep_disable(struct usb_ep *ep);
struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t
gfp_flags);
void usb_ep_free_request(struct usb_ep *ep, struct usb_request *req);
int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t
gfp_flags);
int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
...
28© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Endpoint Request
Header: <linux/usb/gadget.h>
Through <linux/usb/composite.h>
Data Structure: struct usb_request
void *buf
unsigned length
void (*complete)(struct usb_ep *ep, struct usb_request *req);
int status
unsigned actual
...
APIs: As mentioned under “Endpoint Interactions”
29© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
USB Gadget Function Driver
Registration
Header: <linux/usb/composite.h>
Data Structure: struct usb_function_driver
const char *name
struct usb_function_instance *(*alloc_inst)(void);
struct usb_function *(*alloc_func)(struct usb_function_instance *inst);
DECLARE_USB_FUNCTION(fn_name, fn_alloc_instance, fn_alloc);
APIs
int usb_function_register(struct usb_function_driver *);
int usb_function_unregister(struct usb_function_driver *);
Useful APIs for the function user gadget driver
struct usb_function_instance *usb_get_function_instance(char *fn);
struct usb_function *usb_get_function(struct usb_function_instance *);
30© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
Evolution of USB
USB Subsystem
Host: The Four Components
Gadget: The Three Components
Understanding of USB Protocol
USB Device Overview
Writing USB Host Device Drivers
Registration
Hot Plug-ability
Transfers
Writing USB Gadget Device Drivers
Registration
Gadget Device Creation
Function & Descriptors Addition
Endpoint Interactions
Writing USB Gadget Function Drivers
31© 2010-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?
1 of 31

Recommended

Bootloaders (U-Boot) by
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot) Omkar Rane
635 views18 slides
U-Boot - An universal bootloader by
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader Emertxe Information Technologies Pvt Ltd
3K views46 slides
linux device driver by
linux device driverlinux device driver
linux device driverRahul Batra
11.4K views37 slides
U boot porting guide for SoC by
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
11.9K views32 slides
U-Boot presentation 2013 by
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013Wave Digitech
18.7K views28 slides
Board Bringup by
Board BringupBoard Bringup
Board BringupAnil Kumar Pugalia
28.7K views16 slides

More Related Content

What's hot

Bootloaders by
BootloadersBootloaders
BootloadersAnil Kumar Pugalia
10K views19 slides
Arm device tree and linux device drivers by
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
17.5K views19 slides
Jagan Teki - U-boot from scratch by
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchlinuxlab_conf
709 views90 slides
Linux Audio Drivers. ALSA by
Linux Audio Drivers. ALSALinux Audio Drivers. ALSA
Linux Audio Drivers. ALSAGlobalLogic Ukraine
3.8K views24 slides
Linux Porting to a Custom Board by
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom BoardPatrick Bellasi
28.6K views46 slides
U-Boot Porting on New Hardware by
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New HardwareRuggedBoardGroup
2.1K views31 slides

What's hot(20)

Arm device tree and linux device drivers by Houcheng Lin
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
Houcheng Lin17.5K views
Jagan Teki - U-boot from scratch by linuxlab_conf
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
linuxlab_conf709 views
Linux Porting to a Custom Board by Patrick Bellasi
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom Board
Patrick Bellasi28.6K views
Linux I2C by KaidenYu
Linux I2CLinux I2C
Linux I2C
KaidenYu797 views
Embedded_Linux_Booting by Rashila Rr
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr513 views
U Boot or Universal Bootloader by Satpal Parmar
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar21.1K views
Linux Initialization Process (2) by shimosawa
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa6.8K views
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware by Linaro
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
Linaro7.6K views

Viewers also liked

Network Drivers by
Network DriversNetwork Drivers
Network DriversAnil Kumar Pugalia
39.4K views15 slides
Serial Drivers by
Serial DriversSerial Drivers
Serial DriversSysPlay eLearning Academy for You
17.1K views13 slides
SPI Drivers by
SPI DriversSPI Drivers
SPI DriversSysPlay eLearning Academy for You
13.2K views20 slides
Interrupts by
InterruptsInterrupts
InterruptsAnil Kumar Pugalia
47.7K views18 slides
PCI Drivers by
PCI DriversPCI Drivers
PCI DriversAnil Kumar Pugalia
44.5K views16 slides
File System Modules by
File System ModulesFile System Modules
File System ModulesAnil Kumar Pugalia
25K views27 slides

Viewers also liked(17)

Similar to USB Drivers

Usb Drive Protector by
Usb Drive ProtectorUsb Drive Protector
Usb Drive ProtectorAashiq Ahamed N
83 views10 slides
Useful USB Gadgets on Linux by
Useful USB Gadgets on LinuxUseful USB Gadgets on Linux
Useful USB Gadgets on LinuxGary Bisson
2.4K views35 slides
SR-IOV Introduce by
SR-IOV IntroduceSR-IOV Introduce
SR-IOV IntroduceLingfei Kong
4.2K views16 slides
Embedded I/O Management by
Embedded I/O ManagementEmbedded I/O Management
Embedded I/O ManagementAnil Kumar Pugalia
5.1K views22 slides
Study on Android Emulator by
Study on Android EmulatorStudy on Android Emulator
Study on Android EmulatorSamael Wang
6.7K views84 slides
Cold front - bridging the web and the physical world by
Cold front - bridging the web and the physical worldCold front - bridging the web and the physical world
Cold front - bridging the web and the physical worldKenneth Rohde Christiansen
124 views118 slides

Similar to USB Drivers(20)

Useful USB Gadgets on Linux by Gary Bisson
Useful USB Gadgets on LinuxUseful USB Gadgets on Linux
Useful USB Gadgets on Linux
Gary Bisson2.4K views
Study on Android Emulator by Samael Wang
Study on Android EmulatorStudy on Android Emulator
Study on Android Emulator
Samael Wang6.7K views
Embedded Fest 2019. Игорь Опанюк. Das U-boot v2019: a look under the hood by EmbeddedFest
Embedded Fest 2019. Игорь Опанюк. Das U-boot v2019: a look under the hoodEmbedded Fest 2019. Игорь Опанюк. Das U-boot v2019: a look under the hood
Embedded Fest 2019. Игорь Опанюк. Das U-boot v2019: a look under the hood
EmbeddedFest421 views
SESI 7 RouterTroubleshooting.pptx by FirmanAFauzi1
SESI 7 RouterTroubleshooting.pptxSESI 7 RouterTroubleshooting.pptx
SESI 7 RouterTroubleshooting.pptx
FirmanAFauzi15 views
UP Board AI Core Configuration memo by Naoto MATSUMOTO
UP Board AI Core Configuration memoUP Board AI Core Configuration memo
UP Board AI Core Configuration memo
Naoto MATSUMOTO284 views

More from Anil Kumar Pugalia

File System Modules by
File System ModulesFile System Modules
File System ModulesAnil Kumar Pugalia
21K views37 slides
Kernel Debugging & Profiling by
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & ProfilingAnil Kumar Pugalia
6K views14 slides
Processes by
ProcessesProcesses
ProcessesAnil Kumar Pugalia
7K views33 slides
System Calls by
System CallsSystem Calls
System CallsAnil Kumar Pugalia
4.2K views17 slides
Introduction to Linux by
Introduction to LinuxIntroduction to Linux
Introduction to LinuxAnil Kumar Pugalia
4K views33 slides
Embedded Software Design by
Embedded Software DesignEmbedded Software Design
Embedded Software DesignAnil Kumar Pugalia
7K views29 slides

More from Anil Kumar Pugalia(20)

Recently uploaded

How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... by
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...ShapeBlue
46 views28 slides
Network Source of Truth and Infrastructure as Code revisited by
Network Source of Truth and Infrastructure as Code revisitedNetwork Source of Truth and Infrastructure as Code revisited
Network Source of Truth and Infrastructure as Code revisitedNetwork Automation Forum
32 views45 slides
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...ShapeBlue
55 views12 slides
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Moses Kemibaro
27 views38 slides
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueShapeBlue
70 views7 slides
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Safe Software
317 views86 slides

Recently uploaded(20)

How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... by ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue46 views
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by ShapeBlue
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
ShapeBlue55 views
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro27 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue70 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software317 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue44 views
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue38 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray1042 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue40 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue54 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue26 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue44 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院

USB Drivers

  • 1. © 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Drivers
  • 2. 2© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? USB Evolution USB Subsystem: Host & Gadget Understanding of USB Protocol Writing USB Host Drivers Writing USB Gadget Drivers
  • 3. 3© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Prologue What was USB designed for? A Unified Bus for Slow Devices So, design based on Master-Slave concept USB (Host) Controller is the “Single Master” UHC polls the Slave Peripherals / Devices Later Additions High Speed Specifications Bandwidth Allocation Ability But even today, the polling continues
  • 4. 4© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Device Driver Types 2 Types: Both written for the Device USB Host (Device) Driver Runs on Host (Master) Drives the USB Device (Slave) USB Gadget (Device) Driver Runs on the USB Gadget / Device (Slave) Responds to a Host (Master) Pre-requisite: Gadget is Linux based
  • 5. 5© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Subsystem View FS Layer USB Host Device Drivers ... USB Core USB Host / OTG Controller Driver(s) ... TTY Layer Char Layer Net Layer Block Layer Kernel Space User Applications USB Devices ... Hardware Space User Space usbfs User Mode Drivers USB Host / OTG Controller
  • 6. 6© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Subsystem UHC Driver (uhci, ohci, ehci, otg) Hardware-specific USB Host Controller driver Hides all the hardware details from the layers above Provides a uniform interface to USB Core USB Core Module (usbcore) Provides the generic USB Protocol APIs for the kernel, in general By interfacing with the underlying UHC driver USB File System Module (usbfs) Uses USB Core to provide Kernel Windows & USB Devices as entries under /sys Enables writing User Mode USB Drivers USB Host Device Driver USB Device specific Driver Interfaces with the corresponding USB Device through the USB Core Provides interface to the User Space through the relevant Vertical(s)
  • 7. 7© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Subsystem View USB Gadget Device Drivers ... USB Composite USB Device / OTG Controller Driver Kernel Space USB Host Hardware Space USB Device / OTG Controller Horizontal Layers Vertical Layers Peripherals User Applications User Space
  • 8. 8© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Subsystem UDC Driver (*udc) Hardware-specific USB Device Controller driver Hides all the hardware details from the layers above Provides a uniform interface to USB Composite USB Composite Module (libcomposite) Provides the generic USB Protocol APIs for the kernel, in general By interfacing with the underlying UDC driver USB Gadget Device Driver USB Device specific Driver Interfaces with the USB Host through the USB Composite Exposes peripherals &/or (virtual) functionalities to the Host May provide (virtual) functionalities to the User Space through the relevant Layer(s) / Driver(s)
  • 9. 9© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host & sysfs Command: /sbin/lspci <dom>:<bus>:<dev>:<fn> for <usbhubid> Kernel Window /sys/devices/pci0000:00/<usbhubid>/usb<hub> usb_device fields roothub-hubport:config.interface usb_interface fields PCI USB HC functions -> USB buses /sys/kernel/debug/usb/devices
  • 10. 10© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Device Overview (Protocol) Device Config Interface Endpoint ... Endpoint Endpoint ... Interface USB Driver USB Driver ...
  • 11. 11© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Device Endpoints Also called Pipes Direction OUT (host->device) IN (device->host) Four Types Control Interrupt Bulk Isochronous
  • 12. 12© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Device Driver
  • 13. 13© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Driver Data Structures Header: <linux/usb.h> Data Structures struct usb_device struct usb_host_config struct usb_interface interface_to_usbdev struct usb_host_endpoint struct usb_endpoint_descriptor
  • 14. 14© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Core Functionality USB Host Device Driver Registration USB Host Device Hot Plugability probe: Vertical Registration disconnect: Vertical Unregistration USB Transfers through URBs
  • 15. 15© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Device Driver Registration Header: <linux/usb.h> Data Structure: struct usb_driver struct module *owner const char *name const struct usb_device_id *id_table int (*probe)(struct usb_interface *, struct usb_device_id *) int (*disconnect)(struct usb_interface *) APIs int usb_register(struct usb_driver *); int usb_deregister(struct usb_driver *);
  • 16. 16© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Host Device Hot-plug-ability Callback probe int usb_register_dev(intf, class); Callback disconnect int usb_deregister_dev(intf, class); Other Useful APIs (Header: <linux/usb.h> void usb_set_intfdata(intf, void *data); void *usb_get_intfdata(intf);
  • 17. 17© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Request Block Header: <linux/usb.h> Data Structure: struct urb struct usb_device *dev unsigned int pipe unsigned int transfer_flags void *transfer_buffer int transfer_buffer_length usb_complete_t complete int actual_length int status Pipe type specific fields
  • 18. 18© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. URB Operations Header: <linux/usb.h> URB Storage usb_alloc_urb(int iso_pkts, gfp_t flags); usb_free_urb(struct urb *); Populating the URB usb_fill_control_urb(urb, dev, pipe, req, buf, len, fn, ctxt); usb_fill_int_urb(urb, dev, pipe, buf, len, fn, ctxt, interval); usb_fill_bulk_urb(urb, dev, pipe, buf, len, fn, ctxt); Using the URB usb_submit_urb(struct urb *, gfp_t flags); usb_unlink_urb(struct urb *) / usb_kill_urb(struct urb *);
  • 19. 19© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. URB Operations' Wrappers Header: <linux/usb.h> APIs usb_control_msg(dev, pipe, req, req_type, value, index, data, size, timeout); usb_interrupt_msg(dev, pipe, data, len, &act_len, timeout); usb_bulk_msg(dev, pipe, data, len, &act_len, timeout);
  • 20. 20© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Device Driver
  • 21. 21© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Data Structures Header: <linux/usb/composite.h> Data Structures struct usb_device_descriptor struct usb_gadget_strings struct usb_string struct usb_configuration struct usb_descriptor_header struct usb_interface_descriptor struct usb_endpoint_descriptor
  • 22. 22© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Composite Functionality USB Gadget Device Driver Registration USB Gadget Device Creation bind: Gadget Setup unbind: Gadget Cleanup USB Gadget Endpoint Interactions
  • 23. 23© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Driver Registration Header: <linux/usb/composite.h> Data Structure: struct usb_composite_driver const char *name const struct usb_device_descriptor *dev struct usb_gadget_strings **strings enum usb_device_speed max_speed int (*bind)(struct usb_composite_dev *cdev) int (*unbind)(struct usb_composite_dev *cdev) APIs int usb_composite_probe(struct usb_composite_driver *driver); void usb_composite_unregister(struct usb_composite_driver *driver);
  • 24. 24© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Device Creation Header: <linux/usb/composite.h> Callback bind int usb_string_ids_tab(struct usb_composite_dev *c, struct usb_string *str); int usb_add_config_only(comp_dev, usb_cfg) int usb_add_function(usb_cfg, usb_fn); Callback unbind int usb_put_function(usb_fn); // int usb_remove_function(usb_cfg, usb_fn);
  • 25. 25© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Function Addition Header: <linux/usb/composite.h> Callbacks in struct usb_function: int (*bind)(struct usb_configuration *c, struct usb_function *f); void (*unbind)(struct usb_configuration *c, struct usb_function *f); void (*free_func)(struct usb_function *f); int (*set_alt)(struct usb_function *f, unsigned interface, unsigned alt); // Must int (*get_alt)(struct usb_function *f, unsigned interface); void (*disable)(struct usb_function *f); // Must int (*setup)(struct usb_function *f, const struct usb_ctrlrequest *ctrlreq); ...
  • 26. 26© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Descriptors Addition Header: <linux/usb/gadget.h> Through <linux/usb/composite.h> Typical invocations through function's bind / unbind/free_func: int usb_interface_id(struct usb_configuration *c, struct usb_function *f); int usb_string_id(struct usb_composite_dev *c); struct usb_ep *usb_ep_autoconfig(struct usb_gadget *gadget, struct usb_endpoint_descriptor *usb_ep); void usb_ep_autoconfig_reset(struct usb_gadget *gadget); int usb_assign_descriptors(struct usb_function *f, struct usb_descriptor_header **fs, struct usb_descriptor_header **hs, struct usb_descriptor_header **ss); void usb_free_all_descriptors(struct usb_function *f);
  • 27. 27© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Endpoint Interactions Header: <linux/usb/gadget.h> Through <linux/usb/composite.h> Typical (Endpoint Interaction) APIs for set_alt / disable: int usb_ep_enable(struct usb_ep *ep); int usb_ep_disable(struct usb_ep *ep); struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags); void usb_ep_free_request(struct usb_ep *ep, struct usb_request *req); int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags); int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req); ...
  • 28. 28© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Endpoint Request Header: <linux/usb/gadget.h> Through <linux/usb/composite.h> Data Structure: struct usb_request void *buf unsigned length void (*complete)(struct usb_ep *ep, struct usb_request *req); int status unsigned actual ... APIs: As mentioned under “Endpoint Interactions”
  • 29. 29© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. USB Gadget Function Driver Registration Header: <linux/usb/composite.h> Data Structure: struct usb_function_driver const char *name struct usb_function_instance *(*alloc_inst)(void); struct usb_function *(*alloc_func)(struct usb_function_instance *inst); DECLARE_USB_FUNCTION(fn_name, fn_alloc_instance, fn_alloc); APIs int usb_function_register(struct usb_function_driver *); int usb_function_unregister(struct usb_function_driver *); Useful APIs for the function user gadget driver struct usb_function_instance *usb_get_function_instance(char *fn); struct usb_function *usb_get_function(struct usb_function_instance *);
  • 30. 30© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Evolution of USB USB Subsystem Host: The Four Components Gadget: The Three Components Understanding of USB Protocol USB Device Overview Writing USB Host Device Drivers Registration Hot Plug-ability Transfers Writing USB Gadget Device Drivers Registration Gadget Device Creation Function & Descriptors Addition Endpoint Interactions Writing USB Gadget Function Drivers
  • 31. 31© 2010-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?