O'Reilly - Linux Device Drivers 2nd Edition.pdf

(7537 KB) Pobierz
Linux Device Drivers, 2nd Edition
15746819.003.png
 
P REFACE
This is, on the surface, a book about writing device drivers for the Linux system.
That is a worthy goal, of course; the flow of new hardware products is not likely
to slow down anytime soon, and somebody is going to have to make all those
new gadgets work with Linux. But this book is also about how the Linux kernel
works and how to adapt its workings to your needs or interests. Linux is an open
system; with this book, we hope, it will be more open and accessible to a larger
community of developers.
Much has changed with Linux since the first edition of this book came out. Linux
now runs on many more processors and supports a much wider variety of hard-
ware. Many of the internal programming interfaces have changed significantly.
Thus, the second edition. This book covers the 2.4 kernel, with all of the new fea-
tures that it provides, while still giving a look backward to earlier releases for
those who need to support them.
We hope you’ll enjoy reading this book as much as we have enjoyed writing it.
Alessandro’s Introduction
As an electronic engineer and a do-it-yourself kind of person, I have always
enjoyed using the computer to control external hardware. Ever since the days of
my father’s Apple IIe, I have been looking for another platform where I could con-
nect my custom circuitry and write my own driver software. Unfortunately, the PC
of the 1980s wasn’t powerful enough, at either the software or the hardware level:
the internal design of the PC is much worse than that of the Apple II, and the
available documentation has long been unsatisfying. But then Linux appeared, and
I decided to give it a try by buying an expensive 386 motherboard and no propri-
etary software at all.
xi
15746819.004.png
Preface
At the time, I was using Unix systems at the university and was greatly excited by
the smart operating system, in particular when supplemented by the even smarter
utilities that the GNU project donates to the user base. Running the Linux kernel
on my own PC motherboard has always been an interesting experience, and I
could even write my own device drivers and play with the soldering iron once
again. I continue to tell people, “When I grow up, I wanna be a hacker,” and
GNU/Linux is the perfect platform for such dreams. That said, I don’t know if I
will ever grow up.
As Linux matures, more and more people get interested in writing drivers for cus-
tom circuitry and for commercial devices. As Linus Torvalds noted, “We’re back to
the times when men were men and wrote their own device drivers.”
Back in 1996, I was hacking with my own toy device drivers that let me play with
some loaned, donated, or even home-built hardware. I already had contributed a
few pages to the Ker nel Hacker’s Guide , by Michael Johnson, and began writing
kernel-related articles for Linux Journal , the magazine Michael founded and
directed. Michael put me in touch with Andy Oram at O’Reilly; he expressed an
interest in having me write a whole book about device drivers, and I accepted this
task, which kept me pretty busy for quite a lot of time.
In 1999 it was clear I couldn’t find the energy to update the book by myself: my
family had grown and I had enough programming work to keep busy producing
exclusively GPL’d software. Besides, the kernel had grown bigger and supported
more diverse platforms than it used to, and the API had turned more broad and
more mature. That’s when Jonathan offered to help: he had just the right skills and
enthusiasm to start the update and to force me to stay on track with the sched-
ule—which slipped quite a lot anyway. He’s been an invaluable mate in the pro-
cess, which he pushed forward with good skills and dedication, definitely more
than I could put in. I really enjoyed working with him, both on a technical and
personal level.
Jon’s Introduction
I first started actively playing with Linux early in 1994, when I convinced my
employer to buy me a laptop from a company called, then, Fintronic Systems.
Having been a Unix user since the beginning of the 1980s, and having played
around in the source since about then, I was immediately hooked. Even in 1994,
Linux was a highly capable system, and the first truly free system that I had ever
been able to work with. I lost almost all my interest in working with proprietary
systems at that point.
I didn’t ever really plan to get into writing about Linux, though. Instead, when I
started talking with O’Reilly about helping with the second edition of this book, I
had recently quit my job of 18 years to start a Linux consulting company. As a way
xii
15746819.005.png
Preface
of attracting attention to ourselves, we launched a Linux news site, Linux Weekly
News ( http://lwn.net ), which, among other things, covered kernel development. As
Linux exploded in popularity, the web site did too, and the consulting business
was eventually forgotten.
But my first interest has always been systems programming. In the early days, that
interest took the form of “fixing” the original BSD Unix paging code (which has to
have been a horrible hack job) or making recalcitrant tape drives work on a
VAX/VMS system (where source was available, if you didn’t mind the fact that it
was in assembly and Bliss, and came on microfiche only). As time passed, I got to
hack drivers on systems with names like Alliant, Ardent, and Sun, before moving
into tasks such as deploying Linux as a real-time radar data collection system or, in
the process of writing this book, fixing the I/O request queue locking in the Linux
floppy driver.
So I welcomed the opportunity to work on this book for several reasons. As much
as anything, it was a chance to get deeply into the code and to help others with a
similar goal. Linux has always been intended to be fun as well as useful, and play-
ing around with the kernel is one of the most fun parts of all—at least, for those
with a certain warped sense of fun. Working with Alessandro has been a joy, and I
must thank him for trusting me to hack on his excellent text, being patient with
me as I came up to speed and as I broke things, and for that jet-lagged bicycle
tour of Pavia. Writing this book has been a great time.
Audience of This Book
On the technical side, this text should offer a hands-on approach to understanding
the kernel internals and some of the design choices made by the Linux develop-
ers. Although the main, official target of the book is teaching how to write device
drivers, the material should give an interesting overview of the kernel implementa-
tion as well.
Although real hackers can find all the necessary information in the official kernel
sources, usually a written text can be helpful in developing programming skills.
The text you are approaching is the result of hours of patient grepping through
the kernel sources, and we hope the final result is worth the effort it took.
This book should be an interesting source of information both for people who
want to experiment with their computer and for technical programmers who face
the need to deal with the inner levels of a Linux box. Note that “a Linux box” is a
wider concept than “a PC running Linux,” as many platforms are supported by our
operating system, and kernel programming is by no means bound to a specific
platform. We hope this book will be useful as a starting point for people who
want to become kernel hackers but don’t know where to start.
xiii
15746819.001.png
Preface
The Linux enthusiast should find in this book enough food for her mind to start
playing with the code base and should be able to join the group of developers
that is continuously working on new capabilities and performance enhancements.
This book does not cover the Linux kernel in its entirety, of course, but Linux
device driver authors need to know how to work with many of the kernel’s sub-
systems. It thus makes a good introduction to kernel programming in general.
Linux is still a work in progress, and there’s always a place for new programmers
to jump into the game.
If, on the other hand, you are just trying to write a device driver for your own
device, and you don’t want to muck with the kernel internals, the text should be
modularized enough to fit your needs as well. If you don’t want to go deep into
the details, you can just skip the most technical sections and stick to the standard
API used by device drivers to seamlessly integrate with the rest of the kernel.
The main target of this book is writing kernel modules for version 2.4 of the Linux
kernel. A module is object code that can be loaded at runtime to add new func-
tionality to a running kernel. Wherever possible, however, our sample code also
runs on versions 2.2 and 2.0 of the kernel, and we point out where things have
changed along the way.
Organization of the Material
The book introduces its topics in ascending order of complexity and is divided
into two parts. The first part (Chapters 1 to 10) begins with the proper setup of
kernel modules and goes on to describe the various aspects of programming that
you’ll need in order to write a full-featured driver for a char-oriented device. Every
chapter covers a distinct problem and includes a “symbol table” at the end, which
can be used as a reference during actual development.
Throughout the first part of the book, the organization of the material moves
roughly from the software-oriented concepts to the hardware-related ones. This
organization is meant to allow you to test the software on your own computer as
far as possible without the need to plug external hardware into the machine. Every
chapter includes source code and points to sample drivers that you can run on any
Linux computer. In Chapter 8 and Chapter 9, however, we’ll ask you to connect an
inch of wire to the parallel port in order to test out hardware handling, but this
requirement should be manageable by everyone.
The second half of the book describes block drivers and network interfaces and
goes deeper into more advanced topics. Many driver authors will not need this
material, but we encourage you to go on reading anyway. Much of the material
found there is interesting as a view into how the Linux kernel works, even if you
do not need it for a specific project.
xiv
15746819.002.png
Zgłoś jeśli naruszono regulamin