Uh-oh, it looks like your Internet Explorer is out of date.
For a better shopping experience, please upgrade now.
This book is for anyone who wants to support computer peripherals under the Linux operating system or who wants to develop new hardware and run it under Linux. Linux is the fastest-growing segment of the Unix market, is winning over enthusiastic adherents in many application areas, and is being viewed more and more as a good platform for embedded systems. Linux Device Drivers, already a classic in its second edition, reveals information that heretofore has been shared by word of mouth or in cryptic source code comments, on how to write drivers for a wide range of devices.Version 2.4 of the Linux kernel includes significant changes to device drivers, simplifying many activities, but providing subtle new features that can make a driver both more efficient and more flexible. The second edition of this book thoroughly covers these changes, as well as new processors and buses.You don't have to be a kernel hacker to understand and enjoy this book; all you need is an understanding of C and some background in Unix system calls. You'll learn how to write drivers for character devices, block devices, and network interfaces, guided by full-featured examples that you can compile and run without special hardware. Major changes in the second edition include discussions of symmetric multiprocessing (SMP) and locking, new CPUs, and recently supported buses. For those who are curious about how an operating system does its job, this book provides insights into address spaces, asynchronous events, and I/O.Portability is a major concern in the text. The book is centered on version 2.4, but includes information for kernels back to 2.0 where feasible. Linux Device Driver also shows how to maximize portability among hardware platforms; examples were tested on IA32 (PC) and IA64, PowerPC, SPARC and SPARC64, Alpha, ARM, and MIPS.Contents include:
- Building a driver and loading modules
- Complete character, block, and network drivers
- Debugging a driver
- Handling symmetric multiprocessing (SMP) systems
- Memory management and DMA
- Portability issues
- Peripheral Component Interconnect (PCI)
|Publisher:||O'Reilly Media, Incorporated|
|Edition description:||Second Edition|
|Product dimensions:||7.12(w) x 9.22(h) x 1.15(d)|
About the Author
Jonathan Corbet got his first look at the BSD Unix source back in 1981, when an instructor at the University of Colorado let him "fix" the paging algorithm. He has been digging around inside every system he could get his hands on ever since, working on drivers for VAX, Sun, Ardent, and x86 systems on the way. He got his first Linux system in 1993, and has never looked back. Mr. Corbet is the co-founder and executive editor of Linux Weekly News; he lives in Boulder, Colorado with his wife and two children.
Alessandro Rubini installed Linux 0.99.14 soon after getting his degree as an electronic engineer. He then received a Ph.D in computer science at the University of Pavia despite his aversion toward modern technology. Alas, he still enjoys digging in technology and discovering the intelligence of people who created it: that's why he now works in his apartment with three PCs, an Alpha, a SPARC, and an Apple2 the last without Linux. But you might find him roaming around in the north of Italy on his bike, which doesn't carry an electronic cyclometer.
Read an Excerpt
Chapter One: An Introduction to the Linux KernelPeople all around the world are delving into the Linux kernel, mostly to write device drivers. While each driver is different, and you have to know your specific device, many principles and basic techniques are the same om one driver to another. In this book, you'll learn to write your own device drivers and to hack around in related parts of the kernel. This book covers device-independent programming techniques, without binding the examples to any specific device.
This chapter doesn't actually get into writing code. However, I'm going to introduce some background concepts about the Linux kernel that you'll be glad you know later, when we do launch into writing code.
As you learn to write drivers, you will find out a lot about the Linux kernel in general; this may help you understand how your machine works and why things aren't always as fast as you expect or don't do quite what you want. We'll introduce new ideas smoothly, starting off with very simple drivers and building upon them; every new concept will be accompanied by sample code that doesn't need special hardware to be tested.
The Role of the Driver WriterAs a programmer, you will be able to make your own choices about your driver, choosing an acceptable tradeoff between the programming time required and the flexibility of the result. Though it may appear strange to say that a driver is "flexible," I like this word because it emphasizes that the role of a device driver is providing mechanisms, not policies.
The distinction between mechanism and policy is one of the best ideas behind the Unix design. Most programming problems can indeed be split into two parts: "what needs to be done" (the mechanism) and "how can the program be used" (the policy). If the two issues are addressed by different parts of the program, or even by different programs altogether, the software package is much easier to develop and to adapt to particular needs.
For example, Unix management of the graphic display is split between the X server, which knows the hardware and offers a unified interface to user programs, and the window manager, which implements a particular policy without knowing anything about the hardware. People can use the same window manager on different hardware, and different users can run different configurations on the same workstation. Another example is the layered structure of TCP/IP networking: the operating system offers the socket abstraction, which is policy-free, while different servers are in charge of the services. Moreover, a server like ftpd provides the file transfer mechanism, while users can use whatever client they prefer; both command-line and graphic clients exist, and anyone can write a new user interface to transfer files.
Where drivers are concerned, the same role-splitting applies. The floppy driver is policy-free -- its role is only to show the diskette as a continuous byte array. How to use the device is the role of the application: tar writes it sequentially, while mkfs prepares the device to be mounted, and mcopy relies on the existence of a specific data structure on the device.
When writing drivers, a programmer should pay particular attention to this fundamental problem: we need to write kernel code to access the hardware, but we shouldn't force particular policies on the user, since different users have different needs. The driver should only deal with hardware handling, leaving all the issues about how to use the hardware to the applications. A driver, then, is "flexible" if it offers access to the hardware capabilities without adding constraints. Sometimes, however, some policy decisions must be made.
You can also look at your driver from a different perspective: it is a software layer that lies between the applications and the actual device. This privileged role of the driver allows the driver programmer to choose exactly how the device should appear: different drivers can offer different capabilities, even for the same device. The actual driver design should be a balance between many different considerations. For instance, a single device may be used concurrently by different programs, and the driver programmer has complete freedom to determine how to handle concurrency. You could implement memory mapping on the device independently of its hardware capabilities, or you could provide a user library to help application programmers implement new policies on top of the available primitives, and so forth. One major consideration is the tradeoff between the desire to present the user with as many options as possible, balanced against the time you have to do the writing and the need to keep things simple so that errors don't creep in.
If a driver is designed for both synchronous and asynchronous operations, if it allows itself to be opened multiple times, and if it is able to exploit all the hardware capabilities without adding a software layer "to simplify things" -- like converting binary data to text or other policy-related operations -- then it will turn out to be easier to write and to maintain. Being "policy-free" is actually a common target for software designers.
Most device drivers, indeed, are released together with user programs to help with configuration and access to the target device. Those programs can range from simple configuring utilities to complete graphical applications. Usually a client library is provided as well.
The scope of this book is the kernel, so we'll try not to deal with policy issues, nor with application programs or support libraries. Sometimes I'll talk about different policies and how to support them, but I won't go into much detail about programs using the device or the policies they enforce. You should understand, however, that user programs are an integral part of a software package and that even policy-free packages are distributed with configuration files that apply a default behavior to the underlying mechanisms.
Splitting the KernelIn a Unix system, several concurrent processes attend to different tasks. Each process asks for system resources, be it computing power, memory, network connectivity, or some other resource. The kernel is the big chunk of executable code in charge of handling all such requests. Though the distinction between the different kernel tasks isn't always clearly marked, the kernel's role can be split, as shown in Figure 1-1, into the following parts:
The kernel is in charge of creating and destroying processes, and handling their connection to the outside world (input and output). Communication among different processes (through signals, pipes, or interprocess communication primitives) is basic to the overall system functionality, and is also handled by the kernel. In addition, the scheduler, probably the most critical routine in the whole operating system, is part of process management. More generally, the kernel's process management activity implements the abstraction of several processes on top of a single CPU.
The computer's memory is a major resource, and the policy used to deal with it is a critical one for system performance. The kernel builds up a virtual addressing space for any and all processes on top of the limited available resources. The different parts of the kernel interact with the memory-management subsystem through a set of function calls, ranging from simple malloc/free equivalents to much more exotic functionalities.
Unix is heavily based on the filesystem concept; almost everything in Unix can be treated as a file. The kernel builds a structured filesystem on top of unstructured hardware, and the resulting file abstraction is heavily used throughout the whole system. In addition, Linux supports multiple filesystem types, i.e., different ways of organizing data on the physical medium.
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 a I device control operations are performed by code that is specific to the device being addressed. That code is called a device driver. The kernel must have embedded in it a device driver for every peripheral present on your system, from the hard drive to the keyboard and the tape streamer. This aspect of the kernel's functions is our primary interest in this book.
Networking must be managed by the operating system because most network operations are not specific to a process: incoming packets are asynchronous events. The packets must be collected, identified, and dispatched before a process takes care of them. The system is in charge of delivering data packets across program and network interfaces, and it must correctly put to sleep and wake programs waiting for data from the network. Additionally, all the routing and address resolution issues are implemented within the kernel.
Towards the end of this book, in Chapter 16, Physical Layout of the Kernel Source, you'll find a roadmap to the Linux kernel, but these few words should suffice for now.
One of the good features of Linux is the ability to expand the kernel code at run time. This means that you can add functionality to the kernel while the system is up and running.
Each piece of code that can be added to the kernel is called a module. The Linux kernel offers support for quite a few different types (or "classes") of modules, including, but not limited to, device drivers. Each module is made up of object code (not linked to be a complete executable) that can be dynamically linked to the running kernel by the insmod program and can be unlinked by the rmmod program.
In Figure 1-1, you can identify different classes of modules in charge of specific tasks -- a module is said to belong to a specific class according to the functionality it offers. ...
Table of Contents
Audience of This Book;
Organization of the Material;
Sources of Further Information;
Online Version and License;
Conventions Used in This Book;
We’d Like to Hear from You;
Chapter 1: An Introduction to Device Drivers;
1.1 The Role of the Device Driver;
1.2 Splitting the Kernel;
1.3 Classes of Devices and Modules;
1.4 Security Issues;
1.5 Version Numbering;
1.6 License Terms;
1.7 Joining the Kernel Development Community;
1.8 Overview of the Book;
Chapter 2: Building and Running Modules;
2.1 Kernel Modules Versus Applications;
2.2 Compiling and Loading;
2.3 The Kernel Symbol Table;
2.4 Initialization and Shutdown;
2.5 Using Resources;
2.6 Automatic and Manual Configuration;
2.7 Doing It in User Space;
2.8 Backward Compatibility;
2.9 Quick Reference;
Chapter 3: Char Drivers;
3.1 The Design of scull;
3.2 Major and Minor Numbers;
3.3 File Operations;
3.4 The file Structure;
3.5 open and release;
3.6 scull’s Memory Usage;
3.7 A Brief Introduction to Race Conditions;
3.8 read and write;
3.9 Playing with the New Devices;
3.10 The Device Filesystem;
3.11 Backward Compatibility;
3.12 Quick Reference;
Chapter 4: Debugging Techniques;
4.1 Debugging by Printing;
4.2 Debugging by Querying;
4.3 Debugging by Watching;
4.4 Debugging System Faults;
4.5 Debuggers and Related Tools;
Chapter 5: Enhanced Char Driver Operations;
5.2 Blocking I/O;
5.3 poll and select;
5.4 Asynchronous Notification;
5.5 Seeking a Device;
5.6 Access Control on a Device File;
5.7 Backward Compatibility;
5.8 Quick Reference;
Chapter 6: Flow of Time;
6.1 Time Intervals in the Kernel;
6.2 Knowing the Current Time;
6.3 Delaying Execution;
6.4 Task Queues;
6.5 Kernel Timers;
6.6 Backward Compatibility;
6.7 Quick Reference;
Chapter 7: Getting Hold of Memory;
7.1 The Real Story of kmalloc;
7.2 Lookaside Caches;
7.3 get_free_page and Friends;
7.4 vmalloc and Friends;
7.5 Boot-Time Allocation;
7.6 Backward Compatibility;
7.7 Quick Reference;
Chapter 8: Hardware Management;
8.1 I/O Ports and I/O Memory;
8.2 Using I/O Ports;
8.3 Using Digital I/O Ports;
8.4 Using I/O Memory;
8.5 Backward Compatibility;
8.6 Quick Reference;
Chapter 9: Interrupt Handling;
9.1 Overall Control of Interrupts;
9.2 Preparing the Parallel Port;
9.3 Installing an Interrupt Handler;
9.4 Implementing a Handler;
9.5 Tasklets and Bottom-Half Processing;
9.6 Interrupt Sharing;
9.7 Interrupt-Driven I/O;
9.8 Race Conditions;
9.9 Backward Compatibility;
9.10 Quick Reference;
Chapter 10: Judicious Use of Data Types;
10.1 Use of Standard C Types;
10.2 Assigning an Explicit Size to Data Items;
10.3 Interface-Specific Types;
10.4 Other Portability Issues;
10.5 Linked Lists;
10.6 Quick Reference;
Chapter 11: kmod and Advanced Modularization;
11.1 Loading Modules on Demand;
11.2 Intermodule Communication;
11.3 Version Control in Modules;
11.4 Backward Compatibility;
11.5 Quick Reference;
Chapter 12: Loading Block Drivers;
12.1 Registering the Driver;
12.2 The Header File blk.h;
12.3 Handling Requests: A Simple Introduction;
12.4 Handling Requests: The Detailed View;
12.5 How Mounting and Unmounting Works;
12.6 The ioctl Method;
12.7 Removable Devices;
12.8 Partitionable Devices;
12.9 Interrupt-Driven Block Drivers;
12.10 Backward Compatibility;
12.11 Quick Reference;
Chapter 13: mmap and DMA;
13.1 Memory Management in Linux;
13.2 The mmap Device Operation;
13.3 The kiobuf Interface;
13.4 Direct Memory Access and Bus Mastering;
13.5 Backward Compatibility;
13.6 Quick Reference;
Chapter 14: Network Drivers;
14.1 How snull Is Designed;
14.2 Connecting to the Kernel;
14.3 The net_device Structure in Detail;
14.4 Opening and Closing;
14.5 Packet Transmission;
14.6 Packet Reception;
14.7 The Interrupt Handler;
14.8 Changes in Link State;
14.9 The Socket Buffers;
14.10 MAC Address Resolution;
14.11 Custom ioctl Commands;
14.12 Statistical Information;
14.14 Backward Compatibility;
14.15 Quick Reference;
Chapter 15: Overview of Peripheral Buses;
15.1 The PCI Interface;
15.2 A Look Back: ISA;
15.3 PC/104 and PC/104+;
15.4 Other PC Buses;
15.7 External Buses;
15.8 Backward Compatibility;
15.9 Quick Reference;
Chapter 16: Physical Layout of the Kernel Source;
16.1 Booting the Kernel;
16.2 Before Booting;
16.3 The init Process;
16.4 The kernel Directory;
16.5 The fs Directory;
16.6 The mm Directory;
16.7 The net directory;
16.8 ipc and lib;
16.9 include and arch;
Most Helpful Customer Reviews
[A review of the 3RD EDITION, 2005.] Device drivers will always be a small speciality in any operating system. Linux is no exception. While it grows strongly, most programmers using it simply can ignore issues of hooking up to various hardware items. Someone has already worked those out. Well, here you are that someone and this book addresses many of your needs. The coding is in C. No fancy object oriented stuff for you. Many higher level OO programmers are simply unaware of the extra overhead it takes. But you need to maximise performance, so it is C for you. Plus, to understand much of the book, it really helps to have written some assembly code, because it makes it easier to understand many low level operations discussed. Prior acquaintance with the overall design of a linux memory manager and interrupt handlers is also good. The book explains well individual issues as they arise. But having a clear, top-down understanding of the linux kernel may give you more context to understand the chapters.