You are on page 1of 5

USB. Practical programming with Windows API in C ++.

USB is good for everything - use its power!

USB 2.0 and 3.0 standards and connected devices, or hardware data transmission basis
Data transmission in the USB standard, that is communication and interoperability of system
resources of various devices
Libraries and multithreaded programs, ie detailed aspects of USB data transfer programming

Table of Contents

Admission ................................................. ............................................. 7

Chapter 1. USB standard ............................................. ................................... 11

The physical and signaling environment of USB ............................................. .............................


13

USB 2.0 ................................................ .................................................. .................. 13

USB 3.0 ................................................ .................................................. .................. 15

Mini and Micro connectors .............................................. .................................................. .. 19

Frames and micro frames ............................................... .................................................. ...... 24

Data transfer ................................................ .................................................. ............ 24

USB 2.0 packages ............................................... .................................................. ............ 28

USB 2.0 transactions ............................................... .................................................. ....... 33

Packages in Super Speed mode ............................................. ............................................ 38

Transaction operations USB 3.0 .............................................. ........................................ 46

Comparison of USB 2.0 and 3.0 standards ............................................ ................... 53

Wireless USB ................................................ .................................................. ............... 54

Summary ................................................. .................................................. ............. 56

Chapter 2. Information about devices ............................................ .................. 57

Device identifiers ................................................ ............................................... 57

Hardware identifiers ................................................ ............................................... 58


Identification of compliance ................................................ .......................................... 58

Rating and selection of driver packages ............................................. ...................... 58

Device installation classes ............................................... ................................................. 58

Device Manager ................................................ .................................................. ....... 59

System registry ................................................ .................................................. ........ 63

The thematic key HKEY_LOCAL_MACHINE ............................................... ...... 64

The subject subitem \ Class .............................................. ....................................... 65

The subkey of the subject subkey \ Class ............................................. ................... 66

GUIDs ................................................ ................................................ 67

.Inf files ............................................... .................................................. ......................... 69

Summary ................................................. .................................................. ............. 71

Chapter 3. Introduction to data transmission ........................................... ................. 73


The structure of the USB 2.0 system .............................................. ...............................................
73
Functional layer ................................................ .............................................. 73
Physical layer ................................................ .................................................. ... 74
Logic layer ................................................ .................................................. ... 75
The structure of the USB 3.0 system .............................................. ...............................................
76
Data streams................................................ .................................................. ............... 77
Devices and descriptors of USB devices ............................................. ........................... 80
Concentrators and descriptors of USB hubs ............................................. ............ 84
Endpoints and endpoint descriptors ............................................ ............ 89
USB device interface interfaces and descriptors ............................................ ............ 95
Configuration configurations and descriptors .............................................. ......................... 100
Text descriptors ................................................ .................................................. 104
Communication of the user program with the device ............................................. ...... 104
Summary ................................................. .................................................. ........... 110
Chapter 4. HID class devices ............................................ ........................ 111
Report descriptor ................................................ .................................................. ...... 111
Collection and End Collection items ............................................. .......................... 112
Types of reports ................................................ .................................................. 113
The content of reports ................................................ ................................................ 114
Data format ................................................ .................................................. ...... 115
Ranges of data values ............................................... ......................................... 115
Units of measurement ................................................ .................................................. ...... 115
Basic functions of HID class devices ............................................. ....................... 116
Functions of the HidD_Xxx () family ............................................. .................................... 117
Functions of the HidP_Xxx () family ............................................. .................................... 125
HID.dll library .............................................. .................................................. ........ 144
Summary ................................................. .................................................. ........... 147
Chapter 5. Detection and identification of devices connected to the USB bus .. 149
Basic system resources ............................................... .................................... 151
Function SetupDiGetClassDevs () .............................................. .............................. 152
Function SetupDiEnumDeviceInterfaces () .............................................. ................ 152
Structure SP_DEVINFO_DATA ................................................ ........................... 153
Structure SP_DEVICE_INTERFACE_DATA ................................................ ...... 154
Structure SP_DEVICE_INTERFACE_DETAIL_DATA ...................................... 155
Function SetupDiGetDeviceInterfaceDetail () .............................................. ............ 155
Function SetupDiDestroyDeviceInfoList () .............................................. ................ 157
Detection of device interfaces ............................................... ................................ 157
Counting device interfaces ............................................... ................................ 161
Function SetupDiGetDeviceRegistryPegerty () .............................................. ............... 163
Data structures ................................................ .................................................. ........ 168
Usbiodef.h module .............................................. .................................................. .......... 174
Module cfgmgr32.h .............................................. .................................................. ........ 176
The Setupapi library ................................................ .................................................. .... 182
Notification about connecting and disconnecting devices ............................................ ......... 185
Summary ................................................. .................................................. ........... 189

Chapter 6. Unlocking the device for transmission. Reading and writing data ....... 191
Unblocking the device for transmission .............................................. ......................... 191
CreateFile () function .............................................. ................................................. 192
CloseHandle () function .............................................. ............................................. 194
Sample text environment program .............................................. ........ 194
Reading data in the form of a report ............................................. ..................................... 198
ReadFile () function .............................................. .................................................. 199
Readout of data buffer length .............................................. ................................ 203
Function HidD_GetInputReport () .............................................. .............................. 207
Reading button properties ............................................... ................................. 208
Reading the ownership of the value ............................................... ...................................... 213
Graphic environment application ............................................... ........................... 218
Recording of data in the form of a report ............................................. ........................................
225
WriteFile () function .............................................. .................................................. 225
Functions HidD_SetOutputReport () and HidD_SetFeature () ................................... 226
OVERLAPPED structure ................................................ ............................................ 227
Functions xxxEx ................................................ .................................................. ..... 230
The COMMTIMEOUTS structure ................................................ ...................................... 234
Features GetCommTimeouts () and SetCommTimeouts () .......................................... 235
DeviceIoControl () function .............................................. ............................................. 236
Orders from the hidclass.h ............................................ ..................................... 242
Orders from the usbioctl.h module ............................................ ............................................ 245
Identification of devices connected to the USB hub ........................... 247
URB structure ................................................ .................................................. ............ 262
The UsbBuildGetDescriptorRequest () function .............................................. ............... 267
Summary ................................................. .................................................. ........... 268
Exercises ................................................. .................................................. .................. 268
Chapter 7. WinUSB and LibUSB libraries ........................................... .......... 271
WinUSB library ................................................ .................................................. .... 271
Preparation of the installation package ............................................... .................... 272
Export functions of WinUSB library .............................................. .................. 277
LibusB Library ................................................ .................................................. ..... 289
Functions of the library kernel ............................................... ........................................... 292
Functions for managing the libusb device ............................................. ............. 293
Functions performing mass transfer .............................................. ....................... 300
Functions performing interrupt transfer .............................................. .............. 301
Asynchronous functions ................................................ ......................................... 301
Summary ................................................. .................................................. ........... 305
Chapter 8. Object oriented programming of USB transmission .................................... 307
Objectivity ................................................. .................................................. .............. 307
Project patterns ................................................ .................................................. ..... 314
Singleton ................................................. .................................................. ............. 314
Interfaces ................................................. .................................................. .................... 319
Counting interface references .............................................. ............................... 326

Interface ID ................................................ ........................................... 327

Visual components ................................................ .................................................. 336

Summary ................................................. .................................................. ........... 340

Exercises ................................................. .................................................. .................. 340

Chapter 9. Internal data structures ............................................ ........... 351


Procedural program ................................................ .................................................. 352
Object-oriented program ................................................ .................................................. .... 359
Graphic environment application ............................................... ................................. 366
Summary ................................................. .................................................. ........... 375
Exercises ................................................. .................................................. .................. 375
Chapter 10. Multithreaded programs ............................................. .................. 379
Threads and processes ............................................... .................................................. ............ 379
CreateThread () function .............................................. .................................................. 381
TThread class ................................................ .................................................. ............ 389
Summary ................................................. .................................................. ........... 397
Exercises ................................................. .................................................. .................. 397
Chapter 11. USB adapters ............................................. ................................. 401
Adapters USB / RS 232C ............................................. .................................................. 401
Adapter port properties ............................................... ..................................... 402
Adapters USB / IEEE-488 ............................................ ................................................. 404
Adapters USB / Bluetooth .............................................. ................................................ 405
Summary ................................................. .................................................. ........... 413
Literature ................................................. .................................... 415
Index ................................................. ................................... 417

You might also like