Detailed Explanation of VxWorks Device Drivers

University of Science and Technology of China (Anhui, Hefei)

Department of Modern Physics

Fast Electronics Laboratory

Author: Cao Guiping

Click “Read the original text” to download the high-definition e-book

For learning reference only

VxWorks is a commonly used embedded real-time operating system, with applications in many fields. However, due to the lack of public source code for the VxWorks operating system, although various driver designs are documented, many issues still arise in practice. This book is based on the author’s experience with drivers, combined with some official documents and source code provided by WindRiver, offering a detailed introduction and analysis of various types of device drivers for VxWorks. It serves as a guide for designing device drivers under VxWorks. Additionally, it provides a detailed analysis of various types of VxWorks boot methods and the composition of image files, clarifying many questions that VxWorks BSP developers may have.Content of the Book

This book is divided into 11 chapters, with the following content:

The first chapter briefly introduces embedded systems and provides a simple explanation of the characteristics of the VxWorks operating system.

The second chapter provides a detailed introduction to several main components of the VxWorks operating system, including tasks, task scheduling, inter-task communication, memory management, and interrupt handling. We do not translate official documents but share insights based on the author’s experience.

The third chapter analyzes the types of VxWorks kernel images and boot methods in detail, and provides an in-depth analysis and introduction to the bootrom used in download methods, followed by a review of the VxWorks operating system boot process. This chapter will clarify many of the reader’s questions regarding VxWorks booting.

The fourth chapter discusses the basic functions and structure of device drivers, introducing common strategies and considerations in driver programming.

The fifth chapter introduces the kernel structure hierarchy of VxWorks device drivers, focusing on the IO subsystem and the three system tables it maintains, and presents several commonly used drivers in VxWorks with code examples to illustrate their usage.

The sixth chapter begins the design of specific device drivers, starting with the simplest structure of a regular character device driver, using an SPI interface driver code as an example to discuss the structure, design methods, and specific implementations of regular character device drivers.

The seventh chapter provides a detailed analysis of serial port driver design and implementation. The serial port, being a type of character device, is commonly used, and the VxWorks kernel provides a TTY intermediate layer to enhance the efficiency of serial port driver design and reduce its complexity.

The eighth chapter delves into the design and implementation analysis of the second major category of devices—block device drivers. We will analyze the basic structure of block device drivers starting from data structures, and then discuss their specific implementations. Block device drivers in VxWorks operate in a unique manner, using blocking read/write methods, which differ from the interrupt read/write methods used in general operating systems, due to VxWorks’ special working environment.

The ninth chapter provides a detailed introduction to FLASH device drivers. Flash devices are the most common and widely used devices on embedded platforms for storing operating system kernel images and user data. This chapter will expand on the TrueFFS intermediate layer provided by the VxWorks kernel, analyzing various aspects involved in FLASH device drivers.

The tenth chapter enters the design and implementation of the third major category of devices—network device drivers. Due to their unique operating methods, the kernel driver hierarchy for network devices differs from that of the other two categories (character and block devices), as they do not fall under the management of the IO subsystem but work directly within the kernel network stack. To simplify the complexity of network device driver design, VxWorks provides a MUX intermediate layer, and drivers implemented at this level are often referred to as enhanced network drivers. This chapter also starts from data structures, using the network interface driver code from an actual project as an example to gradually complete the design and implementation of network device drivers.

The eleventh chapter analyzes the design and implementation of USB device drivers. This chapter first provides a detailed introduction to USB itself, then clarifies the objects we aim to drive. Generally, USB device drivers refer to the drivers for USB host or target controllers, which must have a clear understanding of the kernel USB stack’s implementation to successfully complete USB host controller driver development. This chapter follows a USB class driver layer reading data request, tracks the request’s path in the kernel USB stack, and provides a detailed analysis and introduction to the key functions called along the path and the data structures used. It then uses the Mass Storage class driver as an example to introduce the initialization process of class drivers, and the UHCI controller driver as an example to introduce the initialization process of host controller drivers, summarizing the driver structure of USB host controllers and providing the implementation framework for two central functions in the driver.

Chapter 1 Quick Introduction to VxWorks Operating System
Chapter 1 Characteristics and Applications of VxWorks Embedded Operating System 2
1.1 Overview of Embedded Systems 3
1.1.1 Definition of Embedded Systems 3
1.1.2 Composition and Characteristics of Embedded Systems 3
1.1.3 Development Trends of Embedded Systems 4
1.1.4 Real-Time Operating Systems 5
1. Real-Time Operating System Definition 5
2. Characteristics of Real-Time Operating Systems 6
3. Related Concepts of Real-Time Operating Systems 6
1.2 Basic Characteristics of VxWorks Operating System 7
1.2.1 Operating System Kernel Structure—Microkernel and Monolithic Kernel 7
1. Monolithic Kernel 7
2. Microkernel 8
1.2.2 VxWorks Kernel—High-Performance Microkernel Design 8
1.2.3 VxWorks Development Support—Trimmable Runtime Software 8
1.2.4 VxWorks Network Support—Comprehensive Networking Tools 9
1.2.5 VxWorks Portability Support 9
1. Compatibility with POSIX 1003.1b Standard 9
2. Platform Selection 9
3. Easy Porting to User Hardware 9
1.2.6 VxWorks Operating System Options 10
1.3 Application Scope of VxWorks Operating System 10
1.4 Summary of This Chapter 11
Chapter 2 Basic Components of VxWorks Operating System 12
2.1 VxWorks Tasks 13
2.1.1 Basic Principles of Kernel Implementation 13
2.1.2 Task Operation Functions 15
2.1.3 In-Depth Understanding of Task Stacks 19
2.1.4 Task Name Length Issues 20
2.1.5 Correctly Ending Tasks 21
2.1.6 Task Hook Functions—Hacker Mechanism 23
2.1.7 Task Summary 25
2.2 VxWorks Task Scheduling Algorithm—Priority-Based Preemptive Scheduling 26
2.3 Inter-Task Communication Strategies in VxWorks 29
2.3.1 Semaphores 30
2.3.2 Message Queues 31
2.3.3 Pipes 32
2.3.4 Network Sockets 32
2.3.5 Special Mechanisms for Inter-Task Communication: Signals 33
2.4 VxWorks Memory Management—Support for Virtual Address Space 33
2.5 VxWorks Interrupt Handling—Multi-Level Interrupt Transfer 38
2.5.1 Detailed Explanation of Interrupt Transfer Process in VxWorks (Based on ARM Platform) 40
2.5.2 Why Functions that Cause Sleep Cannot Be Called in Interrupt Context 43
2.6 Summary of This Chapter 45
Chapter 3 Detailed Explanation of VxWorks Operating System Boot Process 46
3.1 Booting the VxWorks Operating System 47
3.1.1 Basic Boot Methods of VxWorks 47
3.1.2 Memory Layout of VxWorks Operating System 48
1. Uncompressed Form 48
2. Compressed Form 48
3.1.3 Overview of Download Boot Method 50
3.1.4 Overview of ROM Boot Method 53
3.2 In-Depth Understanding of Bootrom—The "Swiss Army Knife" in Download Boot Method 54
3.2.1 Composition of Bootrom 55
3.2.2 Creation of Bootrom Scripts 56
3.2.3 Analysis of Script Execution Process 56
3.2.4 Relocation of Bootrom 60
3.2.5 Bootrom Code Running in RAM 62
1. usrinit Function 62
2. usrroot Function 65
3. bootcmdloop Function 68
3.2.6 Adding User Code to Bootrom 68
3.2.7 Other Considerations and Explanations 69
3.3 In-Depth VxWorks Boot Process 71
3.3.1 Detailed Early Boot Process of ROM Image 71
1. rominit Function 71
2. romstart Function 79
3.3.2 Detailed Early Boot Process of Download Image 81
3.3.3 Detailed Common Boot Process 82
1. Analysis of usrinit Function 83
2. Analysis of usrroot Function 84
3. Booting User Code 89
3.4 Composition of VxWorks BSP Files 90
3.4.1 Source Files 91
3.4.2 Header Files 94
3.4.3 Makefile 97
3.4.4 Extension Files 100
3.4.5 Documentation Files 100
3.5 Summary of This Chapter 100
Chapter 2 Starting Point of VxWorks Device Drivers
Chapter 4 Device Drivers 102
4.1 Functions of Device Drivers 103
4.2 Structure of Device Drivers 103
4.3 Basic Characteristics of Device Drivers 105
4.3.1 Execution Environment of Driver Code—Task and Interrupt Context 105
4.3.2 Basic Classification of Devices 105
4.3.3 Security of Driver Code—Parameter Validity Checks 106
4.3.4 Basic Operating Modes of Drivers—Polling and Interrupts 106
4.3.5 Interaction Methods of Drivers with Hardware Data—DMA and Direct Copy 107
4.3.6 Other Considerations—Volatile Keyword 107
4.4 Summary of This Chapter 108
Chapter 5 Kernel Structure Hierarchy of Device Drivers in VxWorks 109
5.1 Understanding the Basic Kernel Levels of VxWorks Device Drivers 110
1. USB Device Driver Kernel Levels 110
2. Block Device Driver Kernel Levels 111
3. Character Device Driver Kernel Levels 111
4. Network Device Driver Kernel Levels 112
5.2 Basic Structure of VxWorks Kernel Drivers—Three System Tables 114
5.2.1 System Device Table 114
5.2.2 System Driver Table 116
5.2.3 System File Descriptor Table 118
5.2.4 Relationships Between the Three Kernel Tables 120
5.3 VxWorks Kernel Driver Support—"Free Lunch" 122
5.3.1 Pipe Virtual Device Driver Support 123
5.3.2 Virtual Memory Device Driver Support 124
1. Virtual Memory Device Creation Functions 124
2. Examples of Virtual Memory Device Usage 125
5.3.3 Ramdisk Device Driver Support 127
5.3.4 High-Level Driver Support for Network Devices (netdrv) 129
1. Basic Working Principle of netdrv Driver 129
2. Creation of Network Device Nodes 130
3. Acquisition of Key Parameters in netdrv Driver 131
5.4 VxWorks File System Support 132
5.4.1 Virtual Root File System (vrfs) 132
5.4.2 Transactional File System (hrfs) 132
5.4.3 MS-DOS Compatible File System (dosfs) 133
5.4.4 Raw File System (rawfs) 134
5.4.5 CD-ROM File System (cdromfs) 135
5.4.6 Read-Only File System (romfs) 135
5.4.7 Target Machine File System (tsfs) 136
5.5 Adding Drivers to the VxWorks Kernel 136
1. Compiling Driver Code into Kernel Image 136
2. Registering Drivers and Creating Devices 137
5.6 Summary of This Chapter 138
Chapter 3 Six Core Device Drivers of VxWorks
Chapter 6 Detailed Explanation of VxWorks Character Device Drivers 140
6.1 Transmission of User Requests to Character Device Driver Service Functions 141
6.2 Entry Example: Implementation of SPI Interface Serial Port Expansion Chip VK3224 Driver 144
6.3 Understanding the Basic Data Structure of VxWorks Character Device Drivers—dev_hdr Structure 145
6.4 Registering Character Device Drivers and Creating Character Device Nodes 147
6.5 Writing Character Device Driver Lower-Level Service Functions 150
6.5.1 Writing Character Device Open Function 150
6.5.2 Writing Character Device Read/Write Functions 152
6.5.3 Writing Character Device Control Function 155
6.5.4 Writing Character Device Close Function 157
6.5.5 Choosing the Operating Mode of Device Drivers 160
6.5.6 Writing Character Device Delete Function 161
6.6 Deleting Character Device Nodes and Unloading Character Device Drivers 163
6.6.1 Deleting Character Device Nodes 163
6.6.2 Unloading Character Device Drivers 164
6.7 Summary of This Chapter 165
Chapter 7 Detailed Explanation of VxWorks Serial Port Drivers 166
7.1 Understanding the VxWorks Kernel TTY Intermediate Layer—The Foundation of Serial Port Drivers 168
7.1.1 Functions Required of the TTY Intermediate Layer 168
7.1.2 Implementation Ideas for Serial Port Drivers Based on TTY 169
7.1.3 Relationship Between TTY Intermediate Layer and Lower-Level Serial Port Drivers 171
7.2 Detailed Initialization of VxWorks Kernel TTY Intermediate Layer 171
7.2.1 ttydrv Function 172
7.2.2 ttydevcreate Function 173
7.2.3 sysserialhwinit and sysserialhwinit2 Functions 174
7.2.4 Summary and Considerations of TTY Intermediate Layer Initialization Process 174
7.3 Understanding the Basic Data Structure of Serial Port Drivers in VxWorks—sio_chan Structure 176
7.3.1 Definition of Kernel sio_chan Structure 176
7.3.2 Encapsulation of sio_chan Structure 178
7.4 Basic Composition of VxWorks Serial Port Driver Files 180
7.5 Implementation of VxWorks Serial Port Driver Kernel Interface File sysserial.c 183
1. Template File for sysserial.c 184
2. Functionality Description of Template File Functions 188
3. Summary of sysserial.c File Functions 190
7.6 Writing Lower-Level Service Functions for VxWorks Serial Port Drivers 190
7.6.1 Writing Serial Port Driver Initialization Function 191
7.6.2 Writing Serial Port Driver Callback Function—arm926uartcallback-install 193
7.6.3 Writing Serial Port Driver Control Function—arm926uartioctl 195
7.6.4 Writing Serial Port Driver Interrupt Handling Function—arm926uartint 197
7.6.5 Writing Serial Port Driver Start Sending Function—arm926uart-txstartup 200
7.6.6 Writing Serial Port Driver Polling Mode Support Function 202
1. Writing Sending Function arm926uartpolloutput 202
2. Writing Receiving Function arm926uartpollinput 203
7.7 Revisiting the VxWorks Kernel TTY Intermediate Layer 205
7.8 Summary of This Chapter 208
Chapter 8 Detailed Explanation of VxWorks Block Device Drivers 208
8.1 Understanding the Basic Kernel Levels of VxWorks Block Device Drivers 210
8.2 The Foundation of VxWorks Block Device Drivers—Kernel File System Support 211
8.2.1 Detailed Explanation of Rawfs File System 211
1. First Step of Rawfs File System Initialization: rawfsinit Function 212
2. Second Step of Rawfs File System Initialization: rawfsdevinit Function 212
3. Example of Rawfs File System Usage 214
4. Brief Analysis of CBIO Intermediate Layer 216
8.2.2 Detailed Explanation of Dosfs File System 220
1. Basic Process of Dosfs Initialization 221
2. First Step of Initialization: dosfslibinit Function 222
3. Second Step of Initialization: dosfsdevcreate Function 223
4. Formatting Dosfs File System 225
5. Summary of Dosfs File System Initialization 226
8.3 Understanding the Basic Data Structure of VxWorks Block Device Drivers—blk_dev Structure 228
8.3.1 Definition of Kernel blk_dev Structure 229
8.3.2 Encapsulation of blk_dev Structure 232
8.4 Basic Architecture of VxWorks Block Device Drivers 233
8.4.1 Characteristics of Block Device Driver Operations 233
8.4.2 Kernel Levels of Block Device Drivers Based on CBIO Intermediate Layer 234
8.4.3 Composition of Lower-Level Functions of Block Device Drivers 234
8.5 Writing Lower-Level Service Functions for VxWorks Block Device Drivers 235
8.5.1 Understanding ATA (IDE) Hard Disk Structure 236
8.5.2 Understanding Hard Disk Partitions 237
8.5.3 Understanding CBIO Partition Management Layer 239
8.5.4 Writing Block Device Driver Initialization Function 241
8.5.5 Writing Block Device Driver Read Function atablkrd 244
8.5.6 Writing Block Device Driver Write Function atablkwrt 245
8.5.7 Writing Block Device Driver Control Function ataioctl 247
8.5.8 Writing Block Device Driver Status Query Function atastatus 248
8.5.9 Writing Block Device Driver Reset Function atareset 248
8.6 Summary of This Chapter 249
Chapter 9 Detailed Explanation of VxWorks Flash Device Drivers 249
9.1 Understanding Flash Devices 251
9.1.1 Overview 251
9.1.2 Differences in Flash Device Hardware Interfaces 252
9.1.3 Flash Device Capacity and Cost 252
9.1.4 Reliability and Durability of Flash Devices 252
9.1.5 Usability of Flash Devices 253
9.1.6 Software Support for Flash Devices 253
9.2 In-Depth Look at NAND Flash Devices 254
9.3 In-Depth Look at NOR Flash Devices 256
9.3.1 Characteristics of NOR Flash Memory 256
9.3.2 NOR Flash Command Set BCS / SCS 256
9.3.3 NOR Flash Interface Access Standards 257
9.4 Understanding Flash Device Address Issues—Driver "Traps" 258
9.5 Detailed Kernel Levels of Flash Device Drivers in VxWorks—Understanding TrueFFS Intermediate Layer 259
9.6 Detailed Initialization of VxWorks Kernel TrueFFS Intermediate Layer 261
9.7 Creating and Using Flash Devices 264
9.8 Understanding the Basic Architecture of Flash Device Drivers in VxWorks 267
9.9 Writing Socket Layer Service Functions for Flash Device Drivers in VxWorks 268
9.9.1 Composition of Socket Driver Layer Files 268
9.9.2 Implementation Example of Socket Driver Layer 269
9.9.3 Summary of Socket Layer Implementation 279
9.10 Writing MTD Layer Service Functions for Flash Device Drivers in VxWorks 285
9.10.1 tffsconfig.c File—Entry Point for Flash Device Driver Initialization 286
9.10.2 tffsmtd.c File—Location of MTD Layer Service Functions for Flash Device Drivers 289
9.10.2.1 Writing Flash Device Identification Function 289
9.10.2.2 Writing Flash Device Read Function 292
9.10.2.3 Writing Flash Device Write Function 293
9.10.2.4 Writing Flash Device Block Erase Function 296
9.10.2.5 Writing Flash Device Address Mapping Function 297
9.11 Summary of This Chapter 297
Chapter 10 Detailed Explanation of VxWorks Network Device Drivers 298
10.1 Basic Kernel Levels of Network Device Drivers in VxWorks—Understanding MUX Interface Layer 300
10.1.1 Basic Characteristics of Network Device Drivers 300
10.1.2 Kernel Levels of Network Device Drivers 301
10.1.3 Understanding MUX Intermediate Layer 302
10.2 Understanding the Basic Data Structure of VxWorks Network Device Drivers—end_obj Structure 304
10.3 Example Introduction: EMAC Network Interface Driver Based on TMS320D-M6446 Platform 305
10.4 Defining Custom Structures for VxWorks Network Device Drivers—"Information Hub" 310
10.5 Loading and Starting VxWorks Network Device Drivers 311
10.5.1 Basic Process of Network Device Driver Initialization 311
10.5.2 Modifying confignet.h File 313
10.5.3 Implementation of Network Device Driver Loading Function armload 314
10.5.4 Implementation of Network Device Driver Start Function armstart 319
10.6 Background Processing Support for Data Frames in VxWorks Network Device Drivers: netjobadd 320
10.7 Writing Data Frame Reception Functions for VxWorks Network Device Drivers 323
10.7.1 Writing Entry Function for Receiving "Lower Half" of Data Frames 323
10.7.2 Encapsulation Requirements for Data Frames by VxWorks Kernel Network Stack 324
10.7.3 Processing and Uploading Network Data Frames 330
10.7.4 Revisiting Data Frame Reception 332
10.8 Writing Data Frame Sending Functions for VxWorks Network Device Drivers 333
10.9 Writing Control Functions for VxWorks Network Device Drivers 337
10.10 Writing Query Mode Support Functions for VxWorks Network Device Drivers 342
1. Implementation of armpollsend Function 343
2. Implementation of armpollrcv Function 344
10.11 Writing Stop and Unload Functions for VxWorks Network Device Drivers 345
1. Implementation of armstop Function 346
2. Implementation of armunload Function 347
10.12 Understanding Kernel Support Functions for VxWorks Network Device Drivers 347
10.13 Summary of VxWorks Network Device Driver Implementation 349
10.14 Understanding IP Address and MAC Address of Network Devices 350
10.15 Support for Multiple Network Interfaces in VxWorks Network Device Drivers 351
10.15.1 Modifying Lower-Level Drivers 352
10.15.2 Modifying confignet.h File 353
10.15.3 Modifying usrnetinit Function 354
10.16 Summary of This Chapter 355
Chapter 11 Detailed Explanation of VxWorks USB Device Drivers 355
11.1 Detailed Explanation of USB 357
11.1.1 Definition of USB 357
11.1.2 Understanding USB Descriptors and Their Relationships 360
1. Device Descriptor 360
2. Configuration Descriptor 360
3. Interface Descriptor 361
4. Endpoint Descriptor 362
11.1.3 Basic Classification of USB Controllers 363
11.1.4 Understanding USB Hardware Interfaces 363
11.2 Understanding Kernel Levels and Driver Objects of USB Device Drivers in VxWorks 365
11.3 Example Introduction: Basic Working Principle of UHCI USB Host Controller 367
11.3.1 UHCI Specification 367
11.3.2 Basic Working Principle and Data Structures of UHCI 368
11.3.3 Overview of UHCI Controller Driver Principles 371
11.4 Detailed Explanation of Kernel Transmission Process for USB Device Operation Requests in VxWorks 372
11.4.1 First Layer Entry Function: usbbulkdevblkrd 373
11.4.2 Second Layer Entry Function: usbbulkcmdexecute 376
11.4.3 Third Layer Entry Function: usbdtransfer 381
11.4.4 Fourth Layer Entry Function: urbexecblock 383
11.4.5 Fifth Layer Entry Function: usbdcoreentry 384
11.4.6 Sixth Layer Entry Function: fnctransfer 386
11.4.7 Seventh Layer Entry Function: usbhcdirpsubmit 388
11.4.8 Eighth Layer Entry Function: Bottom Layer HCD General Entry Function 389
11.4.9 Summary of Kernel Transmission Process for USB Device Operation Requests in VxWorks 391
11.5 Detailed Initialization of Application Layer Class Drivers for USB Devices in VxWorks 392
11.6 Detailed Initialization of USB Controller Drivers in VxWorks 397
11.7 Architecture of USB Controller Drivers in VxWorks 402
11.8 Writing Lower-Level Service Functions for USB Controller Drivers in VxWorks 403
11.8.1 Writing General Entry Function for USB Operation Requests 403
11.8.2 Writing Interrupt Handling Functions for USB Controller Drivers 404
11.8.3 Writing Specific Service Functions for USB Controller Drivers 408
11.9 Summary of This Chapter 408
References 409

Leave a Comment