Writing a Linux Driver
The main features of a driver are:
It performs input/output (I/O) management.
It provides transparent device management, avoiding low-level programming (ports).
It increases I/O speed, because usually it has been optimized.
It includes software and hardware error management.
It allows concurrent access to the hardware by several processes.
There are four types of drivers: character drivers, block drivers, terminal drivers and streams. Character drivers transmit information from the user to the device (or vice versa) byte per byte (see Figure 2). Two examples are the printer, /dev/lp, and the memory (yes, the memory is also a device), /dev/mem.
Block drivers (see Figure 3) transmit information block per block. This means that the incoming data (from the user or from the device) are stored in a buffer until the buffer is full. When this occurs, the buffer content is physically sent to the device or to the user. This is the reason why all the printed messages do not appear in the screen when a user program crashes (the messages in the buffer were lost), or the floppy drive light does not always turn on when the user writes to a file. The clearest examples of this type of driver are disks: floppy disks (/dev/fd0), IDE hard disks (/dev/hda) and SCSI hard disks (/dev/sd1).
Terminal drivers (see Figure 4) constitute a special set of character drivers for user communication. For example, command tools in an open windows environment, an X terminal or a console, are devices which require special functions, e.g., the up and down arrows for a command buffer manager or tabbing in the bash shell. Examples of block drivers are /dev/tty0 or /dev/ttya (a serial port). In both cases the kernel includes special routines, and the driver special procedures, to cope with all particular features.
Streams are the youngest drivers (see Figure 5) and are designed for very high speed data flows. Both the kernel and the driver include several protocol layers. The best example of this type is a network driver.
As we have said, a driver is a piece of a program. It is composed of a set of C functions, some of which are mandatory. For example, for a printer device, some typical functions only called by the kernel, may be:
lp_init(): Initializes the driver and is called only at boot time.
lp_open(): Opens a connection with the device.
lp_read(): Reads from the device.
lp_write(): Writes to the device.
lp_ioctl(): Performs device configuration operations.
lp_release(): Interrupts connection with device.
lp_irqhandler(): Specific functions called by the device to handle interrupts.
Some additional functions are available for particular applications, like *_lseek(), *_readdir(), *_select() and *_mmap(). You may find more information about them in Michael Johnson's Hacker's Guide (see Resources).
There are several reasons for writing our own device driver:
To solve concurrency problems when two or more processes try to access a device at the same time.
To use hardware interrupts: as the kernel runs in protected mode, the user cannot manage interrupts directly from a program.
To handle other unusual applications, such as managing a virtual device (a RAM disk or a device simulator).
To obtain satisfaction as a programmer: writing a driver increases personal motivation as well as control over the computer.
To learn about the internal parts of the system.
Conversely, there are also several reasons for not writing our own driver:
It requires a good deal of mental preparation.
It requires low-level programming, i.e., direct management of ports and interrupt handlers.
In the debug process, the kernel hangs easily, and it is not possible to use debuggers or C library functions such as printf.
In order to understand the following explanation, you must know the C programming language, the basic I/O procedures, a minimum about the internal architecture of a PC and have some experience in the development of software applications for Unix systems.
Finally, we must add that writing our own device driver is only necessary when the device manufacturer does not supply a driver for our OS or when we wish to add extra functionality to the one we have.
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- A Topic for Discussion - Open Source Feature-Richness?
- Dynamic DNS—an Object Lesson in Problem Solving
- Tech Tip: Really Simple HTTP Server with Python
- Please correct the URL for Salt Stack's web site
2 hours 16 min ago
- Android is Linux -- why no better inter-operation
4 hours 31 min ago
- Connecting Android device to desktop Linux via USB
5 hours 14 sec ago
- Find new cell phone and tablet pc
5 hours 58 min ago
7 hours 27 min ago
- Automatically updating Guest Additions
8 hours 35 min ago
- I like your topic on android
9 hours 22 min ago
- This is the easiest tutorial
15 hours 57 min ago
- Ahh, the Koolaid.
21 hours 36 min ago
- git-annex assistant
1 day 3 hours ago