Linux Device Drivers, 3rd Edition FROM OUR EDITORS
The Barnes & Noble Review
There are a world of devices just waiting to have Linux devices written for them, and a world of users who will be forever grateful if you take on the quest. It's a quest that'll take you deep inside the bowels of the Linux kernel -- and offer powerful psychic rewards when you succeed. Take this guide on your journey: Linux Device Drivers, Second Edition.
Alessandro Rubini and Jonathan Corbet have done an excellent job of getting their arms around this very big subject. They first introduce modularization and what it means for modules to run in kernel space rather than user space. (Those who've built device drivers for Windows NT will recognize this distinction.)
You'll learn how to set up kernel modules properly, avoiding header files and eschewing namespace pollution (remember, even the smallest kernel module links to the entire kernel, and can wreak havoc if you're not careful). You'll also learn how modules use system resources such as memory, I/O ports, and IRQs.
By Chapter 3, you're writing a complete character device driver -- which is all that some simple hardware devices will need. To illuminate the techniques they present, Rubini and Corbet draw upon examples from scull, the Linux Simple Character Utility for Loading Localities (which, in essence, treats an area of memory as if it were a device). They introduce file operations and struct files, and introduce the problem of race conditions (a classic problem with device drivers which will get much deeper consideration later in the book).
Once you've learned how to provide for read and write operations, you'll learn how to perform a variety of hardware control tasks using ioctl, a device-specific entry point for the driver to handle commands. The authors warn you about some gotchas you might otherwise not notice -- gotchas related to numbering your commands, and working with pointers to user space.
Real-world drivers need to pay especially careful attention to timing. Linux Device Drivers contains a full chapter on the topic. It introduces the timer interrupt, shows how to retrieve the current time, how to delay execution of a piece of code for a specified amount of time (to give the hardware time to finish what it's doing); and how to schedule functions using task queues, tasklets, and kernel timers.
Early in the book, Rubini and Corbet introduce debugging -- a special challenge when it comes to device drivers, which don't lend themselves to easy execution or tracing by a debugger. (It doesn't help that Linus dislikes interactive debuggers almost as much as he likes penguins, and won't build one in.) Your options include debugging by printing (using printk); by querying; and by watching. If you still can't track a bug down, you can at least collect information about its behavior when it generates a system fault.
There's a short but extremely valuable chapter on portability. The Linux kernel is extremely portable, but you have to watch out for your data typing, and be suspicious of explicit constant values (if you've lived your life in the x86 universe, you may find time intervals, page sizes, and byte order to be especially problematic).
From start to finish, each chapter of Linux Device Drivers covers a single problem; for example, getting hold of memory, managing I/O, handling interrupts. Part II moves on to block drivers, network drivers, peripheral buses, and finally, a quick look at the layout of kernel source. (The book's written for the 2.4 kernel but also covers 2.2, and offers some workarounds for 2.0. Throughout, the authors call your attention to significant changes in the latest kernels, including changes to resource management, wait queues, and the block device layer, to name a few examples.)
Rubini and Corbet won't completely absolve you from grepping through kernel sources, but they've done a lot of that work for you. You'll still need to join the Linux-kernel mailing list, but hey: you'll understand what they're talking about! (Bill Camarda)
Bill Camarda is a consultant, writer, and web/multimedia content developer with nearly 20 years' experience in helping technology companies deploy and market advanced software, computing, and networking products and services. His 15 books include Special Edition Using Word 2000 and Upgrading & Fixing Networks For Dummies®, Second Edition.
ANNOTATION
"We're back to the times when men were men and wrote their own device drivers." -- Linus Torvalds. Of course, this is also a very good publication for the excellent, equally facile (and less macho) kernel and hardware programmers of the opposite gender. Comments aside, this is a very good (and to-date the only publication) for programmers and developers that covers device drivers in-depth. The author assumes that you understand the Linux kernel and C code.
FROM THE PUBLISHER
This book, a classic entering its third edition, is a must-read for anyone who wants to support computer peripherals under the Linux operating system, wants to run new hardware under Linux, or is simply interested in Linux kernel programming in general. Linux Device Drivers reveals information-heretofore shared only by word of mouth or in cryptic source code comments-on how to write drivers for a wide range of devices.
The authors of the book are leaders in the Linux community. Jonathan Corbet is an occasional kernel contributor and the executive editor of the highly regarded LWN.net news and information site. Alessandro Rubini is a contributor to Linux code and a central figure in the vibrant Italian Linux community. Greg Kroah-Hartman is the current kernel maintainer for the USB, PCI, and driver core subsystems documented in this book.
This new edition has been competely updated for Version 2.6.10 of the Linux kernel. This version of the kernel has rationalized and simplified several ways of doing common tasks, such as plug and play, interaction with user space through the sysfs filesystem, and management of multiple devices on standard buses.
You don't have to be a kernel hacker to understand and enjoy Linux Device Drivers; 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 you can compile and run without special hardware. The PCI, USB, and tty (terminal) subsystems each get their own chapter. 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.
SYNOPSIS
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. As the fastest-growing segment of the Unix market, Linux 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 shows how to write drivers for a wide range of devices, revealing information heretofore available only by word-of-mouth or in cryptic source code comments.
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. Drivers for character devices, block devices, and network interfaces are all described in step-by-step form and are illustrated with full-featured examples that show driver design issues and can be executed without special hardware. Major changes in the second edition include support for symmetric multiprocessing (SMP), Universal Service Bus (USB), and some new platforms.
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 kernel version 2.4, but includes information for kernels back to 2.0 where feasible. You'll also learn 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
Timing
Handling symmetric multiprocessing (SMP) systems
Memory management and DMA
Interrupts
Portability issues
Peripheral Component Interconnect (PCI)