My Lecture15 DeviceDrivers
My Lecture15 DeviceDrivers
My Lecture15 DeviceDrivers
January 2012
Agenda
Device Drivers
1
Device Drivers
Kernel view of Things
2
Device Drivers
Kernel view of Things
1. The Kernel is the big chunk of executable code in charge of handling all requests →
computing power, memory, network connectivity, any other resource allocation.
2. The distinction between the different kernel tasks isnt́ always clearly marked, the
kernelś role can be split into :-
3. Process Management :: Creating and destroying processes and handling their connec-
tion to the outside world (IO). Communication among different processes (through
signals, pipes, IPC). The scheduler, which controls how processes share the CPU, is
part of process management.
4. Memory Mangement :: The computerś memory is a major resource, and the policy
to deal with it is a critical one for overall system performance. The kernel builds up a
virtual address space for all processes. The different parts of the kernel interact with
the memory-management subsystem through simple malloc / free and other more
complex memory functions.
6. Device Control :: Almost every system operation eventually maps to a physical device.
With the exception of the processor, memory and a very few other entities; any and
3
all device control operations are performed by code that is specific to the device being
addressed. That code is called Device Driver. The kernel must have embedded in
it a device driver for every peripheral present on a system (hard drive, printer, kbd,
tape..)
1. A set of software procedures or apiś which enable higher level of programs (applica-
tions) to interact with hardware device
3. The driver ha few functions and provides the logic to initialize and communicate with
the hardware
4
Device Driver
Device Driver Types
1. Service Device Driver :: If the device driver will be communicating directly with the
hardware device, this interface will be the memory ranges, registers and/or ports
through which I/O to the device takes place.
2. System Device Driver :: If the device driver will be communicating with its device
via an intermediate device, this interface will be whatever APIs the driver for the
intermediate device exposes. Eg. a GPS device driver will use the serial port driver
API – the stream interface functions – to communicate with a GPS receiver.
5
Device Driver
Device Driver Need & Challenges
1. By providing a clear abstraction between the driver and the application, one can
essentially free the application of the specifics of a certain peripheral and port it more
easily to new hardware
2. Device Drivers have a tight connection to the target device and the development
environment, these are usually not portable. Eg., device drivers across microcontroller
families.
3. Device Drivers consist of a lot of ”bit bashing” and register programming. One needs
to get all the details right → the bits, sequences of initialization and exit, timing (say
flash, ddr), or else the hardware would malfunction or not provide the desired output.
5. GROUP DISCUSSION :: So, How would you go about developing a Device Driver?
6
Device Driver
Device Driver Development → HardWare Side
2. Learn about the hardware / platform. Identify the interface between the driver and
the device
(a) How the device is reset
(b) How the device is ”address mapped”
(c) Return codes and software protocols recognized by the device
(d) What are the types of DMA transfers possible with the device
(e) How the device reports hardwar failures
(f) How the device sends / responds to interrupts
3. Test the hardware to make sure it is functioning. This becomes important for a newly
developed device.
7
Device Driver
Device Driver Development → SoftWare Side
8
Device Driver
Device Driver Development → SoftWare Side
9
Device Driver
Device Driver Development → SoftWare Side PROs and CONs of Monolithic Vs Layered
2. Layered ::
(a) Because of modularity, the structure of software is easy to understand.
(b) It is easy to add or modify features of the overall application as it evolves and gets
deployed.
(c) Because there is one module that ever interacts directly with the peripherals reg-
isters, the state of the hardware device can be more accurately tracked.
(d) Software changes that result from hardware changes are localized to the device
driver, making software more portable.
(e) Enhance the reusability, but bit of extra effort up front, at design time, in order
to realize the savings.
10
Device Driver
1. One of the most basic requirements of a Device Driver is the need to ensure that only
one application task at a time can request an input or output operation on a specific
device
2. Example :: Same hardware I2C is shared between multiple devices like FM tuner,
LCD, Temperature sensor on Hardware board
3. Semaphores can be used in the open and close function to ensure mutual exclusion
4. In case the target allows multiple simultaneous accesses, like in some complex file IO,
it can be through system of arbitration logic
12
Device Driver
Concepts → Synchronous Vs Asynchronous driver operation
1. Do you want the application task that called the device driver to wait for the result
of the IO operation that it asked for? → Blocking (Synchronous)
2. OR do you want the application task to continue to run while the device driver is
doing its IO operation? → Not Blocking (Asynchronous)
4. Question to Ask (Hardware Side) :: Does the completion of the IO operation through
driver provide a logical status only or it also provides an interrupt?
6. GROUP DISCUSSION :: if a task calls a driver via a ”read” call, the driver function
implementing the call would act as a subroutine of the caller task. And if this driver
attempts to get a semaphore token that is not present, the driver function would be
blocked from continuing exectuion; and together with it, the requesting task would
be in waiting state → Synchronous or Asynchronous
13
Device Driver
Concepts → Synchronous Vs Asynchronous driver operation
1. GROUP DISCUSSION :: if a task calls a driver via a ”read” call, the driver function
implementing the call would act as a subroutine of the caller task. And if this driver
attempts to get a semaphore token that is not present, the driver function would be
blocked from continuing exectuion; and together with it, the requesting task would
be in waiting state → Synchronous* or Asynchronous
3. this in turn calls the device driver routine named dev read()
14
4. the drivers read function will request the device hardware to perform a read operation,
and then it will attempt to get a token from the semaphore to its right.
5. Since the semaphore initially has no tokens, the driver ”read” function, and hence all
the related tasks will be ”BLOCKED”.
7. When device hardware completes the previous read operation, it would trigger this
ISR, that will put a token into the semaphore.
8. this is the semaphore token for which the Task is waiting, and will become unblocked
and proceed to fetch the newly-read data from hardware
dev_read() {
Start IO Device Read Operations;
Get Synchronizer Semaphore Token /*Wait for Semaphore token */;
Get Device Status and Data;
Give Device Information to Requesting Task;
}
dev_isr {
Calm down the hardware device;
Put a token into synchronizer semaphore;
}
1. GROUP DISCUSSION :: Write a similar code for Asynchronous Drivers (NON Block-
ing)
Device Driver
Concepts → Synchronous Vs Asynchronous driver operation
1. In an ”Asynchronous” driver, the application task that called the device driver may
continue executing, without waiting for the result of the IO operation it requested
3. GROUP DISCUSSION :: Write a similar code for Asynchronous Drivers (NON Block-
ing)
dev_read_async() {
Get Message from the Queue /* Wait only if Queue is Empty */;
Start new IO device read operation;
Give old Device Information to the Requesting Task;
}
dev_read_async_isr() {
Calm down the hardware device;
Get Data/Status information from Hardware;
Package this information into a Message;
Put Message into the Queue;
}
15
Device Driver
Concepts → Miscellaneous
1. Compiler Selected :: for code optimization, selection of variable types (global, volatile,
loops..)
3. Interrupt Vs Polling
7. Data structure
16
Device Driver
Concepts → Performance Analysis
3. Memory Foot Print :: For a Network Interface Controller (NIC) what is the memory
requirement on per packet basis, or the max number of packets if burst modes are
supported
5. Hardware Resource Used :: Some device drivers may use multiple hardware resources.
Say a higher level CodecDriver would use DMA, SPI, Memory interfaces including
DSP Core bandwidth.
17
Device Driver
Example → LoopBack (codec) driver
1. Writing a codec driver for a DSP involves programming three different peripherals ::
the codec itself, the SPI, and the DMA controller
void main() {
void* buf0, buf1, buf2, buf3;
/*Allocate buffers for the SIO buffer exchange*/
buf0 = (void*) MEM_calloc(0, BUFSIZE, BUFALIGN);
buf1 = (void*) MEM_calloc(0, BUFSIZE, BUFALIGN);
buf2 = (void*) MEM_calloc(0, BUFSIZE, BUFALIGN);
buf3 = (void*) MEM_calloc(0, BUFSIZE, BUFALIGN);
18
Device Driver
Example → LoopBack (codec) driver
1. The application uses the SIO create() call to create the channels.
2. The SIO create() function arguments indicate the design decisions when implementing
the driver.
3. The SIO stream can be opened either for reading or writing, but not both. Example
:: LCD is only write and KBD is only read.
4. If BiDi communication is required, the application opens two channels (as above
example).
19
Device Driver
Example → LoopBack (codec) driver
void echo(){
int sizeRead; // Number of buffer units read
unsigned short *inbuf, *outbuf;
for (;;) {
/* Reclaim full buffer from input stream
and empty from output stream. */
sizeRead = SIO_reclaim(inStream, (void**)&inbuf, NULL);
SIO_reclaim(outStream, (void**)&outbuf, NULL);
20
Device Driver
Example → LoopBack (codec) driver
1. mdCreateChan() :: Create a channel. Add data structure which can include channel
state information, information about the current IO packet being processed, a linked
list of packets queued for processing, and the call back function that is to be used to
notify the driver that a packet processing is complete.
21
Device Driver
Example → LoopBack (codec) driver
1. GROUP DISCUSSION :: Write a state diagram of the use of the driver by the
application.
23
Device Driver
General look of a Driver The structure of a driver is similar for a peripheral device (say
TouchScreen)
1. There is an init routine for initializing the hardware, setting memory from the kernel
and hooking the driver-routines into the kernel.
2. There is a data structure that is initialized with routines that are provided with the
device. This strucutre is key and is used by the applications.
4. Mostly there are routines for reading and writing data to or from the driver, a ioctl
routine that can perform special commands to driver like config requests or options.
6. The Driver itself, can be either ”Compiled as part of Kernel” or ”Dynamically Loaded”
at runtime. The only differences between a loadable ”Module” and a kernel linked
driver are a special init() routine that is called when the module is loaded into the
kernel and a cleanup routine that is called when the module is removed.
24
Device Driver
ReCall → Touch Screen System Device Driver
5. Send changes in touch status and position to the higher level graphics software.
25
Device Driver
ReCall → Touch Screen System Device Driver
Configure the controller hardware.
2. When the controller is in the detection mode and a touch is detected, an internal
interrupt can be generated called PEN DOWN IRQ.
3. This detection is based on Y-axis touch plane tied high, X-axis touch plane tied low,
and on the basis of touch the planes are shorted together and Y-axis plane is pulled
low.
4. The driver task would not consume any CPU time until the PEN DOWN IRQ event occurs.
It would wake up and go into conversion mode only once the user touches screen.
26
Device Driver
ReCall → Touch Screen System Device Driver
1. In an ideal scenario, the calibration would be run once during initial product power up
and reference values saved in ”non-volatile” memory.
27
Send changes in touch status and position to the higher level graphics software.
2. Create a function named TouchTask (). This routine calls the actual task the user
intended to be run while using the touch screen.
Device Driver
Serial Device Driver Example from Linux
28
Taking Stock
1. Introduction of the course – Harware developments from pure function based to cpu
based design. HW - SW codesign flow.
3. Basic themes of HSCD – Modeling, Analysis and Estimation, System Level Par-
titioning, synthesis and interfacing, Implementation Generation, Co-simulation and
Emulation.
9. Analysis using Process Path Algorithm(s) and parallel process execution on one CPU.
10. Task Scheduling on one CPU – Rate Monotonic Priority Assignment algorithm, Dead-
line driven analysis, Mixed Scheduling.
29
11. Partitioning paper by Kalavade and Lee – Binary Partitioning, Extended Partitioning,
Global Criticality Local Phase (GCLP) algorithm.
13. FPGA and Emulation systems :: descriptions, differences and their usage
14. Compiler, Linker, Loader :: Basic Steps, Intermediate Format Generation, Challenges
for ASISP and ReTargetable Compilers. With examples on point items.
16. Static Image Processing – Introduction, DCT, Quantization, Huffman Encoding, Run
Length encoding. With point examples.
17. Video Image Processing – Motion Vector, Motion Estimation, Frame Types.
18. Device Drivers – Introduction and basic Methodology. Example of Touch Screen,
Serial Loopback.
30