Embedded Systems Programming Writing Device Drivers.

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Linux device-driver issues
Linux Device Drivers & Project3 preview CSC345. Project 3 Preview Write a device driver for a pseudo stack device Idea from
Lecture 101 Lecture 10: Kernel Modules and Device Drivers ECE 412: Microcomputer Laboratory.
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Computer System Laboratory
USERSPACE I/O Reporter: R 張凱富.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Computer Systems/Operating Systems - Class 8
Processes CSCI 444/544 Operating Systems Fall 2008.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
Embedded System Programming Introduction to Device Drivers.
Embedded System Programming Programming DIO on the Puppeteer.
1Lecture 5 Lecture 5: Linux for Hardware Developers ECE 412: Microcomputer Laboratory.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Software Development and Software Loading in Embedded Systems.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating System Program 5 I/O System DMA Device Driver.
System Calls 1.
Eric Keller, Evan Green Princeton University PRESTO /22/08 Virtualizing the Data Plane Through Source Code Merging.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Processes Introduction to Operating Systems: Module 3.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Operating Systems Process Creation
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
Interfacing Device Drivers with the Kernel
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Input/Output (I/O) Important OS function – control I/O
Linux Details: Device Drivers
Linux Kernel Module Programming
CS 6560: Operating Systems Design
Computer System Laboratory
Thread Programming.
Linux Kernel Driver.
Threads, SMP, and Microkernels
Chapter 2: System Structures
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Thread Programming.
Kernel Structure and Infrastructure
Linux Details: Device Drivers
Chapter 2: Operating-System Structures
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
Implementation of Embedded OS
Computer System Laboratory
CS510 Operating System Foundations
Loadable Kernel Modules
Chapter 2: Operating-System Structures
Presentation transcript:

Embedded Systems Programming Writing Device Drivers

Writing a new device driver When writing a device driver you can either –Create a new, bespoke driver from scratch Or –Port an existing driver Generally pick option 2 unless –The hardware is so new to make porting impossible –You are really experienced in device driver writing –The driver has to do something really special or diffferent

Linux Execution Environment

Linux Execution Paths Execution paths

Kernel Modules Kernel modules are inserted and unloaded dynamically –Kernel code extensibility at run time –insmod / lsmod/ rmmod commands. Look at /proc/modules –Kernel and servers can detect and install them automatically, for example, cardmgr (pc card services manager) Modules execute in kernel space –Access to kernel resources (memory, I/O ports) and global variables ( look at /proc/ksyms) –Export their own visible variables, register_symtab (); –Can implement new kernel services (new system calls, policies) or low level drivers (new devices, mechanisms) –Use internal kernel basic interface and can interact with other modules (pcmcia memory_cs uses generic card services module) –Need to implement init_module and cleanup_module entry points, and specific subsystem functions (open, read, write, close, ioctl …)

#include #define DRIVER_AUTHOR "craig duffy #define DRIVER_DESC "FPGA DIO driver" #define LED_ADDRESS 0xf #define RGGG 0xf000 static int fpga_dio_init(void) { static int fpga_j; static short unsigned pattern; printk(KERN_ALERT "fpga dio loaded\n"); pattern=RGGG; pattern = pattern >> 4; for ( fpga_j=0; fpga_j != 16 ; fpga_j++) { printk("pattern %x\n",pattern); udelay(400); writew(pattern,LED_ADDRESS); pattern = pattern >> 8; pattern--; pattern = pattern << 8; }*/ return 0; } static void fpga_dio_exit(void) { printk(KERN_ALERT "fpga dio unloaded\n"); } module_init(fpga_dio_init); module_exit(fpga_dio_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_SUPPORTED_DEVICE("fpga_dio");

Linking a module to the kernel (from Rubini’s book)

Module programming Be careful: a kernel fault is fatal to the current process and sometimes the whole system Modules should support concurrency (support calls by different processes). Distinct data structures for each process (since the same code is executed) to ensure data is not corrupted. Driver code must be reentrant: keep status in local (stack allocated) variables or dynamic memory allocation: kmalloc / kfree This allows the process executing to suspend (e.g., wait for pcmcia card interrupt) and other processes to execute the same code. It is not a good idea to assume your code won’t be interrupted. Sleep_on(wait_queue) or interruptible_sleep_on(wait_queue) to yield the cpu to another process /proc/ioports contains information about registered ports. /proc/iomem contains info about I/O memory

Register Capability You can register a new device driver from the kernel: –int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); –A negative return value indicates an error, 0 or positive indicates success. –major: the major number being requested (a number < 128 or 256). –name: the name of the device (which appears in /proc/devices). –fops: a pointer to a global jump table used to invoke driver functions. Then give to the programs a name by which they can request the driver through a device node in /dev –To create a char device node with major 254 and minor 0, use: mknod /dev/memory_common c –Minor numbers should be in the range of 0 to 255.

Finding a device driver Obviously you need to be able to categorise the device –Some Linux categories are not clear You can look at other open systems – for example NetBSD –This is useful as they may have ported the driver themselves Get as much reliable information about the device –Data sheets, tested, stand alone code

Example of a Parallel port It is quite often useful to use a parallel port as an interface to some specialised hardware. There are a number of approaches you could take to controlling such hardware from Linux –A simple, low level kernel device driver –Porting the parallel port package, ppdev, –Writing a full blown device driver through the file system –Writing a driver through the /procfs interface

A simple low level kernel driver Under Linux it is always possible to write directly to the hardware This uses some low level calls to read and write data –inb(), outb(), inw(), outw() The areas of memory are maped by the kernel and ioremap/iounmap shopuld be used Data has to be copied to and from user space unto kernel space

Problems with low level driver It is a good idea to use to test the hardware but isn’t a long term solution for driver development –Requires root access to run the kernel No file system interface –Will be very limited functionality –Can’t use the kernel very effectively –Isn’t portable –Can crash the kernel

Porting an existing device With the parallel port one can use ppdev This give a high level interface into the file system through /dev/parport0-n –Don’t need root access Can have well structured driver Should be ported (in-time) with new kernel releases Less likely to cause kernel panics

Problems with ppdev Unless it is a very standard parallel port application it is likely to be difficult to get the correct level of functionality Will require understanding a lot of details of parallel port and the driver which won’t be useful for your app

Writing a device driver This allows you to create a new device in the system Uses VFS calls to access the drive –Can open, read, write and close –Uses ioctl calls Driver can be a module –Only loaded when needed –Easier development

Problems