Device Drivers Writing UNIX Device Drivers George Pajari Chapter : 1,2,5,13 Design and anatomy of UNIX device driver: Types of device driver General design of UNIX character device driver General design of UNIX block device driver UNIX device driver installation. Chapter - 1 What is Device Driver ?? What is Device Driver ? A Device Driver is glue between an OS and its I/O devices. Act as Translators, Requests to Command etc. Device drivers communicate directly with devices. A device is a physical or logical entity that requires control, resource management, or both from the operating system (OS). A device driver is a software module that manages the operation of a virtual or physical device, a protocol, or a service. What is Device Driver ? Major Design Issue OS / Driver Communication Exchange information (command and data) Support functions that the kernel provides Driver / Hardware Communication Exchange information (command and data) Software talks to the hardware Hardware talks to the software Driver Opertaions Interrupting ,scheduling, managing, read, write, accepting Types of Device Drivers Block Drivers Character Drivers Terminal Drivers Stream Drivers Block Drivers It communicate with OS through a collection of fixed-sized bufferss. For example, disks are commonly implemented as block devices. Character Drivers It can handle I/O requests of arbitary size. Mostly used devices, Pritners Terminal Drivers Same as character drivers to deal with communication terminals that connect users to OS. E.g. MODEM, STREAM Drivers It can handle high speed communication devices such as networking adapters that deal with unusal sized chunks of data and that need to handle protocol. It is also known as Network Device Drivers. eg. Network Devices Chapter - 2 Charcter Driver I – A Test Data Generator Character Driver : Entry Points init( ): Initialize hardware start( ): Boot time initialization (require system services) open(dev, flag, id): initialization for read or write close(dev, flag, id): release resources after read and write halt( ): call before the system is shutdown intr(vector): called by the kernel on a hardware interrupt read/write calls: data transfer poll(pri): called by the kernel 25 to 100 times a second ioctl(dev, cmd, arg, mode): special request processing Character Driver : Prologue It includes all of the definition and declarations required by the driver. Three types of Prologue : Reference to the header files (Data types / Structures) Definitions that local for driver Declaration local to the Driver Reference to the header files #include <sys/types.h> #include <sys/param.h> #include <sys/dir.h> #include <sys/signal.h> #include <sys/errno.h> All above header files are found in the directory /usr/include/sys and read before writing first driver. Header files example Sys/types.h : contain variaous data types Sys/param.h : contain definition of various kernel parameter and macros that are needed by sys/user.h Sys/dir.h : contains definition of directory structure Sys/signal.h : contains the definition related to signals needed by sys/user.h Sys/user.h : contains the definition of user structure. Sys/errno.h : contains the definition of various Init() Entry Point To check device actually installed on the machine To print message (Presence / Absence) Initialize device (Prior to open) Allocate Local Memory It is optional entry point Chapter - 5 Block Drivers I : A Test Data Generator Block Drivers - Prologue #include <sys/types.h> #include <sys/buf.h> - Definition of buffer header that are used to coordinate the transfer of data to and from block drivers. #include <sys/cmn_err.h> - definition used by the error reporting routine. Block Drivers – Entry Points Init() Open() Close() Strategy() - Responsible for handling requests for data (in or Out) and replace both the read and write entry points found in character drivers. Print() - it to used by the kernel report problems related to the driver. It needs to print the error message on the console and usyally calls upon the kernel routine cmn_err() to display message. Chapter - 13 Driver Installation Driver Installation Steps Compiling the device driver Modifying the kernel configuration tables and files Linking the device with the kernel object files to produce a new kernel Creating the necessary entries in the /dev directory Rebooting the system with the new kernel. Compiling the driver It is similar to compiling any other C Program. Driver is not a stand-alone program, It is always compiled to produce an Object rather than a linked executable file. It need to linking in to the kernel. Necessary information obtained from system's documentation Configuing the Kernel Kernel Configuration tables to reflect the addition of new driver. Method varies between different Version of Unix. There are two Config files used by the system to generate a new kernel are the mdevice and sdevice file contain entry for each device driver that exists on the system. Sdevice contains info for each device driver that is to be incorporated into the kernel Mdevice contains entry for each device driver that exist on the system. Mdevice file Device Name Fucntion List Driver characteristics Handle Prefix Block Major Number Character Major Number Minimum Unit Maximum Unit DMA Channel Sdevice file Device Name Configure Unit Lpl Interrupt Type Vector Start I/O Address End I/O Address Start Controller Memory Address Building a New Kernel Sytem utlity to run is idbuild. Creating Entries in /dev /dev directory that reference your device driver. It can be done with mknod command. On most system it will be automatically. Node files has four fields. Device Name Node Name Node Type Minor Device Number Rebooting the New Kernel Reboot the system ALL THE BEST