Development of a Linux Driver for a MOST Interface and Porting to RTAI
Total Page:16
File Type:pdf, Size:1020Kb
FACHHOCHSCHULE LANDSHUT FACHBEREICH INFORMATIK Development of a Linux Driver for a MOST Interface and Porting to RTAI Diplomarbeit Vorgelegt von: Bernhard Walle aus Neufahrn i. NB Eingereicht am: 15. September 2006 Betreuer FH: Prof. Dr. rer. nat. Peter Hartlmüller Betreuer Siemens: Gernot Hillier, Siemens AG, CT SE 2 Erklärung zur Diplomarbeit (gemäß § 31, Abs. 7 RaPO) Name, Vorname des Studierenden: Bernhard Walle Fachhochschule Landshut Fachbereich Informatik Hiermit erkläre ich, dass ich die Arbeit selbständig verfasst, noch nicht an- derweitig für Prüfungszwecke vorgelegt, keine anderen als die angegebenen Quellen oder Hilfsmittel benützt sowie wörtliche und sinngemäße Zitate als solche gekennzeichnet habe. 15. 09. 2006 Datum Unterschrift des Studierenden Contents 1 Introduction 19 1.1 About the Topic of this Thesis .............................. 19 1.2 Overview ......................................... 20 1.3 Conventions ....................................... 20 1.3.1 Terms ....................................... 20 1.3.2 Units ....................................... 21 1.3.3 Typographical Conventions ........................... 21 1.4 Source Code ....................................... 21 1.4.1 Listings ...................................... 21 1.4.2 Original Software Code ............................. 22 1.4.3 MOST Driver and Utilities ........................... 22 2 Basics 23 2.1 Linux Device drivers ................................... 23 2.1.1 Kernel Modules ................................. 23 2.1.2 Device Files and System Calls .......................... 25 2.1.3 Linux Driver API ................................. 25 2.1.4 The Proc FS and Other Virtual File Systems .................. 26 2.1.5 Hardware Communication ........................... 26 2.1.5.1 I/O Ports and I/O Memory ...................... 26 2.1.5.2 Interrupts ............................... 27 2.1.6 PCI ........................................ 28 2.1.6.1 PCI Configuration .......................... 28 2.1.6.2 The PCI Subsystem in Linux ..................... 28 2.1.7 Managing Concurrency ............................. 30 2.1.7.1 Contexts ................................ 30 2.1.7.2 Mechanisms .............................. 31 2.1.8 Timestamps ................................... 32 2.1.8.1 Hardware Timers ........................... 32 2.1.8.2 Linux Timekeeping Architecture .................. 32 2.1.9 Softirqs ...................................... 34 2.2 Real-time Operating Systems .............................. 35 2.2.1 Overview ..................................... 35 2.2.2 Real-time Linux ................................. 36 2.2.2.1 Introduction ............................. 36 2.2.2.2 Virtualisation Layers ......................... 36 2.2.3 Real-time Applications with RTAI ....................... 38 2.2.3.1 Kernelspace .............................. 38 2.2.3.2 Userspace ............................... 40 2.2.3.3 Communication with Linux Applications .............. 40 2.2.4 Xenomai ..................................... 42 5 2.2.5 Real-time Drivers ................................ 43 2.2.5.1 Motivation .............................. 43 2.2.5.2 Accessing Device Drivers from RTAI Tasks ............. 43 2.2.5.3 Already Existing Real-Time Drivers ................. 44 2.3 MOST ........................................... 45 2.3.1 Overall Information ............................... 45 2.3.2 Data Transfer ................................... 46 2.3.2.1 Synchronous Data .......................... 46 2.3.2.2 Control Data ............................. 46 2.3.2.3 Asynchronous Data .......................... 47 2.3.3 System Architecture ............................... 48 2.3.3.1 Terms ................................. 48 2.3.3.2 Hardware ............................... 48 2.3.3.3 Software ................................ 49 2.3.4 MOST PCI Board ................................ 50 2.3.4.1 Overview ............................... 50 2.3.4.2 Data Flow ............................... 50 2.3.5 OptoLyzer .................................... 52 2.3.6 Windows Software Architecture ........................ 53 2.3.6.1 Control Messages ........................... 53 2.3.6.2 Synchronous and Asynchronous Data ................ 54 3 Requirements 55 3.1 Current Situation ..................................... 55 3.1.1 Real-time Drivers ................................ 55 3.1.2 MOST ...................................... 56 3.2 Functional Requirements ................................ 56 3.2.1 Linux Driver ................................... 56 3.2.2 RTDM Driver .................................. 56 3.2.3 Hardware ..................................... 57 3.2.4 Sample Applications ............................... 57 3.3 Non-functional Requirements .............................. 57 3.3.1 Data rates .................................... 57 3.3.2 Relationship to PCI Timing ........................... 58 3.3.3 Calculation of Timing Constraints ....................... 59 3.3.4 Result ....................................... 59 4 Linux Driver 61 4.1 Structure ......................................... 61 4.1.1 Overview ..................................... 61 4.1.2 Base driver .................................... 61 4.1.3 Low and High Drivers .............................. 62 4.1.3.1 Low Driver .............................. 62 4.1.3.2 High Driver .............................. 63 4.1.3.3 Driver Structures ........................... 64 4.1.4 MOST Device .................................. 64 4.1.4.1 Managing the Device Count ..................... 66 4.2 MOST NetServices .................................... 67 4.2.1 Introduction ................................... 67 6 Contents 4.2.2 Userspace vs. Kernelspace ........................... 67 4.2.3 The Kernel Module ............................... 68 4.2.3.1 General Description ......................... 68 4.2.3.2 Interrupt Processing ......................... 69 4.2.4 Userspace NetServices Implementation .................... 71 4.2.4.1 Device Access and Callback Functions ............... 71 4.2.4.2 Initialisation and Deinitialisation .................. 72 4.2.4.3 Service Thread ............................ 72 4.2.5 Sample Program for Control Messages ..................... 73 4.3 MOST Synchronous Driver ............................... 75 4.3.1 Access the Driver from Userspace ....................... 75 4.3.1.1 Configuring the Routing Engine ................... 75 4.3.1.2 Configuring the Driver ........................ 76 4.3.1.3 Reading and Writing Data ...................... 77 4.3.2 MOST Synchronous Kernel Driver ....................... 77 4.3.2.1 Buffering of Data ........................... 78 4.3.2.2 Managing the Data Flow in the Driver ................ 79 4.3.2.3 Data Structures ............................ 79 4.3.2.4 Synchronous Transmission ..................... 80 4.3.3 Sample Program for Synchronous Transfer .................. 81 4.3.4 PCI Bus Transfers ................................ 81 4.3.4.1 Setting up the PCI Tracer ....................... 82 4.3.4.2 Transfers on the Bus ......................... 82 5 Porting to RTAI 85 5.1 Introduction ....................................... 85 5.1.1 Overview ..................................... 85 5.1.2 RTNRT Porting Framework ........................... 85 5.1.3 Error Handling ................................. 86 5.2 Real Time Driver Model (RTDM) ............................ 86 5.2.1 Introduction ................................... 86 5.2.2 User API ..................................... 87 5.2.2.1 Overview ............................... 87 5.2.2.2 Using the RTDM in an Example ................... 87 5.2.2.3 Drawbacks .............................. 88 5.2.3 Device Profiles .................................. 89 5.2.4 Driver Development API ............................ 89 5.2.5 API Versioning .................................. 90 5.3 Structure of a Character Device Driver ......................... 90 5.3.1 Partitioning ................................... 90 5.3.2 Basic Structure of a Simple Driver ....................... 91 5.3.3 Registering a Character Device ......................... 91 5.3.4 The Device Context ............................... 93 5.3.5 Per-device Data ................................. 93 5.4 Porting Common Patterns Found in Drivers ...................... 94 5.4.1 Resource Management and Memory Access .................. 94 5.4.2 Interrupt Handling ............................... 94 5.4.2.1 Registering an Interrupt Handler .................. 94 5.4.2.2 Deregistering an Interrupt Handler ................. 95 Contents 7 5.4.2.3 Sharing Interrupts Between RTAI and Linux ............ 95 5.4.2.4 Return Value of the Interrupt Handler ............... 97 5.4.2.5 Using the RTNRT Framework .................... 97 5.4.3 Synchronisation ................................. 100 5.4.3.1 Contexts ................................ 100 5.4.3.2 Spinlocks ............................... 100 5.4.3.3 Semaphores and Mutexes ...................... 101 5.4.3.4 Wait Queues ............................. 102 5.4.3.5 Sequence Locks ............................ 104 5.4.4 Allocating Memory ............................... 106 5.4.5 Copying From and To Userspace ........................ 106 5.4.5.1 Basics ................................. 106 5.4.5.2 Using the Functions in the RTNRT Framework ........... 106 5.4.6 Kernel Threads ................................. 107 5.4.6.1 Linux Kernel Threads ........................ 108 5.4.6.2 Real-time Task ............................ 108 5.4.7 Time Stamps ..................................