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.

Figure 2. Character Drivers
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).

Figure 3. Block Drivers
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.

Figure 4. Terminal Drivers
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.

Figure 5. Stream Drivers
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
| Android's Limits | Jun 04, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Introduction to MapReduce with Hadoop on Linux
- Senior Perl Developer
- Technical Support Rep
- Weechat, Irssi's Little Brother
- UX Designer
- One Tail Just Isn't Enough
- Android's Limits
- Reply to comment | Linux Journal
14 min 45 sec ago - Reply to comment | Linux Journal
15 min 12 sec ago - Replica Watches
2 hours 40 min ago - Reply to comment | Linux Journal
6 hours 50 min ago - on the path to understanding
6 hours 54 min ago - As a fisher,we know that a
1 day 2 hours ago - All I Say Is Worth Share!
1 day 3 hours ago - GeekSays
1 day 3 hours ago - thanks
1 day 7 hours ago - You should consider visiting
1 day 8 hours ago




Comments
I like this one
Good introduction, I think, for those who is not familiar with Linux drivers at all.
Thanks a lot to the author. I enjoyed the article.
This article twice uses the
This article twice uses the term "protected mode" where it should be using the term "supervisor mode".
Protected mode is a mode of the Intel x86 processor which provides various protection features, such as memory protection and the ability to disable privileged instructions. Under Linux, all software runs in protected mode, but user applications run at a different privilege level to the kernel.
Of course this article is quite out of date (though surprisingly much of it is still relevant) but on this point it was wrong even back when it was written.
sidebar & port table ?
where is the sidebar & port table that were discussed in this article..?
links and sidebars
This is not the magazine so there are no "sidebars" Check the links in the articles they have what you are looking for.
"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup