You are on page 1of 52

INTRODUCTION TO DEVICE DRIVERS

INDEX
Device drivers basics Types of drivers Driver Implementation Concepts Introduction to DP Boards & Drivers Details of DP-MM-1105 Details of DP-cPCI-3096 Details of DP-IP-4221 Details of DP-cPCI-4509

Device driver basics

Basics of device driver


What is a device driver ? Why we need device driver ? Where device drivers are located ? What are the types of device drivers ?

Basics of device driver


Specialized device specific software that controls the device and exports usable interface for other programs to interact with the device. OS cannot be expected to know about the details of each device so, to control the device, device specific software (device driver) is necessary. Device drivers are modules that can be plugged into the Linux kernel dynamically.

Device driver in kernel

Types of device drivers

Types of device drivers


Character drivers Block drivers Network drivers

CHARACTER DRIVERS

Character driver concepts


Char devices are accessed using files called device files (or) node files (or) special files. Char device files are located at the location /dev/ Identified by c in first column of output of ls -l command. Major & Minor Number :
The major number identifies the driver associated with the device. The minor number is used by the kernel to determine exactly which device is being referred to.

Character driver concepts


Internal representation of device number The dev_t type (defined in <linux/types.h>) is used to hold device numbersboth the major and minor parts. To obtain the major or minor parts of a dev_t, use:
MAJOR(dev_t dev); MINOR(dev_t dev);

Character driver concepts


Data Structures: 1. struct file_operations Connects driver operations to device numbers Defined in <linux/fs.h> Its a collection of function pointers Each member points to the specific function in the driver Fields of the file_operations structure
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

Character driver concepts


int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *);

struct file_operations dpxxxx_fops = { .read = dpxxxx_read, .write = dpxxxx_write, .ioctl = dpxxxx_ioctl, .open = dpxxxx_open, .release = dpxxxx_release };

Character driver concepts


2. struct file Defined in <linux/fs.h>. Different from file pointer of user space program. Every open file in the system has an associated struct file in kernel space. Driver doesnt create but only use this structure. Field of struct file
mode_t f_mode; loff_t f_pos; unsigned int f_flags; struct file_operations *f_op; void *private_data; struct dentry *f_dentry;

Character driver concepts


3. struct inode The inode structure is used by the kernel internally to represent files. There can be numerous file structures representing multiple open descriptors on a single file, but they all point to a single inode structure. Following two fields of this structure are of interest for writing driver code:
dev_t i_rdev struct cdev *i_cdev

Character driver concepts


Char Device Registration & Release: Function to register a char device driver:
int register_chrdev(unsigned int major, const char *name,

struct file_operations *fops); Function to release a char device driver:


int unregister_chrdev(unsigned int major, const char *name);

BLOCK DRIVERS

Block driver concepts


Provides access to devices that transfer randomly accessible data in fixed-size blocks disk drives. Block : A block is a fixed-size chunk of data, the size being determined by the kernel. (normal size 4096 bytes). Sector : A sector is a small block whose size is usually determined by the underlying hardware. The kernel expects to be dealing with devices that implement 512-byte sectors.

Block driver concepts


Block Driver Registration: int register_blkdev(unsigned int major, const char *name);
To register block driver in the kernel. Declared in <linux/fs.h>

int unregister_blkdev(unsigned int major, const char *name);


For canceling a block driver registration.

Block driver concepts


Data Structures: 1. struct block_device_operations Used to make driver operations available to the system Declared in <linux/fs.h> Structure fields
int (*open)(struct inode *inode, struct file *filp); int (*release)(struct inode *inode, struct file *filp); int (*ioctl)(struct inode *inode, struct file *filp, unsigned int cmd,

unsigned long arg);


int (*media_changed) (struct gendisk *gd); int (*revalidate_disk) (struct gendisk *gd); struct module *owner;

Block driver concepts


2. struct gendisk Kernels representation of an individual disk device. Declared in <linux/genhd.h> Structure fields
int major; int first_minor; int minors; char disk_name[32]; struct block_device_operations *fops; struct request_queue *queue;

Block driver concepts


int flags; sector_t capacity; void *private_data;

Kernel Functions to work with gendisk structure


struct gendisk *alloc_disk(int minors); void del_gendisk(struct gendisk *gd); void add_disk(struct gendisk *gd);

Block driver concepts


Request Function : Core function of every block driver. Prototype:
void request(request_queue_t *queue);

Associated with request queue. Runs atomically. Asynchronous to user space process.

Block driver concepts


Request Queue : A queue of block I/O requests Keep track of outstanding block I/O requests. Stores parameters that describes kinds of requests the device is able to service:
Maximum size. Maximum no. of segments may go into a request. Hardware sector size. Alignment requirements.

Implements a plug-in interface to allow the use of multiple I/O schedulers (or elevators).

NETWORK DRIVERS

Network driver concepts


Features Necessary for data communication. No special files. Block drivers operate only in response to requests from the kernel, whereas network drivers receive packets asynchronously from the outside. Should support administrative tasks.
Set addresses. Modify transmission parameters. Maintain traffic and error statistics.

Driver Implementation Concepts

Two ways of implementing drivers


Traditional built-in drivers
compiled into the kernel loaded when the kernel boots always loaded, can not be removed programming interface somewhat limited

Loadable kernel module drivers


can be loaded and unloaded on demand a more general programming interface Can be designed, written and installed long after kernel has been compiled and OS installed

Kernel space - User space interaction methods


Real Time FIFO Device File IOCTL Layer
copy_from_user copy_to_user

Control Flow diagram

application

IOCTL Layer
User Space Kernel Space
FIFO FIFO

Driver Module

Kernel

Hardware Device

Introduction to DP Boards & Drivers

DP Boards
DP Boards are categorized as follows: 1. 2. 3. 4. Data Acquisition Boards (DP-MM-1105) Input/Output Boards (DP-cPCI-3096) Communication Boards (DP-IP-4221) Relay Boards (DP-cPCI-4509)

DP Drivers
DP Drivers are Character drivers General driver API Calls
DPxxxx_FindNoOfBoards DPxxxx_Open (DPxxxx_RTOpen) DPxxxx_Close (DPxxxx_RTClose) DPxxxx_GetErrorMessage DPxxxx_GetDriverVersion

DP-MM-1105

DP-MM-1105 Features
Fast analog input M-Module
16 bit, Analog to Digital Conversion 8 differential voltage inputs or 16 single ended voltage inputs Programmable gain 1, 10,100 or 1000 512 Entry Scan RAM for Scan Sequencing 1 K Sample FIFO for converted data Numerically controlled Oscillator for flexible scan frequency Digitally isolated channel to system for high linearity

DP-MM-1105 API Calls


DP1105_DetectMModules() DP1105_Open() (or) DP1105_RTOpen() DP1105_Close() (or) DP1105_RTClose() DP1105_ConfigureADC() DP1105_UpdateScanList() DP1105_StartAcquisition() DP1105_Trigger() DP1105_StopAcquisition() DP1105_GetRecentSample() DP1105_BulkRead()

DP-MM-1105 API Calls


DP1105_ReadBuffer () DP1105_CheckStatus() DP1105_ReadCalibrationTable() DP1105_WriteCalibrationTable() DP1105_Calibrate () DP1105_GetErrorMessage() DP1105_CalibrateSingleEnded() DP1105_EnableInterrupt() DP1105_GetDrvVer() DP1105_GetFifo()

DP-MM-1105 Configuration
DP1105_CONFIGURATION Structure.
Input type (1 Single ended, 0 Differential).

Pacer Source (0 Internal, 1 External, 2 Software Pacer). Pace Frequency If Internal Pacer source is selected (in Hz). Trigger type (0 Internal Trigger (through software), 1 External Trigger). Continuous Trigger type (No Continuous Trigger 0, Timer Trigger Enable 1, Continuous Internal Trigger Enable 2). External Trigger Signal type (0 Positive Edge sensitive, 1 Negative Edge sensitive). Time Period Time period between two successive triggers if Timer Trigger is enable (In multiples of 100 microseconds). Buffer size The internal buffer size (in number of samples). Source Type The Parameter specifies the Voltage and Current type. Possible value are 1 and 2.

Input / Output Modules General Features


Self Test Facility Interrupt types Input Interrupt State Change Interrupt Board Strobe Interrupt Group Strobe Interrupt Ability to Read Back Actual Data Interrupt Interrupt data State Change Input data Strobed Input data

Primary Latch & Secondary Latch for Output Channels Programmable Debounce Time Feature Some of the DP I/O Modules are (3096 3069, 3015)

DP-cPCI-3096

DP-cPCI-3096 Features
48 Channel Isolated Digital Input / Interrupt 48 Channel Isolated Digital Open Collector Output with 200mA Sink Current Isolation in group of 16 channels Jumper selectable input levels of 5, 12, 24 & 48V DC Time programmable debounce for input Programmable interrupt (Maskable, Edge selectable). Build-in self test for each group. Additional 48 Input and 48 Output channel through rear I/O Relay coil flyback protection for output channels Hot swap compliant

DP-cPCI-3096 API Calls


DPcPCI3096_FindDevices DPcPCI3096_Open (or) DPcPCI3096_RTOpen DPcPCI3096_Close (or) DPcPCI3096_RTClose DPcPCI3096_ConfigMode DPcPCI3096_SetDebounceTime DPcPCI3096_SelfTest DPcPCI3096_GroupStatus DPcPCI3096_MaskGroup DPCPCI3096_MaskChannel DPcPCI3096_GroupEdgeSelect DPcPCI3096_ChannelEdgeSelect

DP-cPCI-3096 API Calls


DPcPCI3096_StrobeGroup DPcPCI3096_BoardStrobe DPcPCI3096_ClearInterrupt DPcPCI3096_ClearAllInterrupts DPcPCI3096_WriteGroupData DPcPCI3096_EnableOutput DPcPCI3096_ReadbackInput DPcPCI3096_ReadInterruptStatus DPcPCI3096_ReadbackOutput DPcPCI3096_Reset DPCPCI3096_GetErrorMsg

DP-IP-4221

DP-IP-4221 Features
Provides eight asynchronous serial communication ports from a single IP carrier slot. Provides programmable Baud rates, character-sizes, stop bits, and parity. Each serial port is equipped with 128-byte FIFO buffers each on transmit and receive lines, minimizing CPU interaction for improved system performance.

DP-IP-4221 API Calls


DPIP4221_FindDevices DPIP4221_Open DPIP4221_Close DPIP4221_SetBaudRate DPIP4221_SetWordLength DPIP4221_SetStopBit DPIP4221_SetParity DPIP4221_SetMode DPIP4221_SetModemControl DPIP4221_GetRxFIFOCount

DP-IP-4221 API Calls


DPIP4221_GetTxFIFOCount DPIP4221_LoopBackMode DPIP4221_TransmitData DPIP4221_ReceiveData DPIP4221_FlushFIFO

DP-cPCI-4509

DP-cPCI-4509 Features
High voltage switching of up to 220V AC/ DC Relays can be operated individually, or in groups of 16 relays each Relay status provided through read back Rugged connectors for field terminations Hot swap feature

DP-cPCI-4509 API Calls


DPcPCI4509_FindDevices DPCPCI4509_Open DPCPCI4509_Close DPcPCI4509_SetRelay DPcPCI4509_SetGroup DPcPCI4509_SetTrigger DPcPCI4509_ReadSetValue DPcPCI4509_ReadRelay DPcPCI4509_ReadGroup DPcPCI4509_GetErrorMsg

Question(?) Time

THANK YOU

You might also like