,TITLE.9502 Page i Thursday, January 27, 2005 12:06 PM
LINUX
DEVICE
DRIVERS
,TITLE.9502 Page ii Thursday, January 27, 2005 12:06 PM
Other Linux resources from O’Reilly
Related titles Understanding the Linux
Kernel
Linux in a Nutshell
Running Linux
Linux Network
Administrator’s Guide
Linux Pocket Guide
Building Embedded Linux
Systems
Designing Embedded
Hardware
Linux Books
Resource Center
linux.oreilly.com is a complete catalog of O’Reilly’s books on
Linux and Unix and related technologies, including sample
chapters and code examples.
ONLamp.com is the premier site for the open source web plat-
form: Linux, Apache, MySQL, and either Perl, Python, or PHP.
Conferences O’Reilly brings diverse innovators together to nurture the ideas
that spark revolutionary industries. We specialize in document-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit con-
ferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today with a free trial.
,TITLE.9502 Page iii Thursday, January 27, 2005 12:06 PM
LINUX
DEVICE
DRIVERS
THIRD EDITION
Jonathan Corbet, Alessandro
Rubini, and Greg Kroah-Hartman
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol
• Taipei
• Tokyo
,COPYRIGHT.10321 Page iv Thursday, January 27, 2005 12:09 PM
Linux Device Drivers, Third Edition
by Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman
Copyright © 2005, 2001, 1998 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti-
tutional sales department: (800) 998-9938 or corporate@oreilly.com.
Andy Oram
Matt Hutchinson
Editor:
Production Editor:
Production Services: Octal Publishing, Inc.
Cover Designer:
Interior Designer:
Edie Freedman
Melanie Wang
Printing History:
February 1998:
First Edition.
June 2001:
Second Edition.
February 2005:
Third Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. The Linux series designations, Linux Device Drivers, images of the American West,
and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors
assume no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.0
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a
letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 0-596-00590-3
[M]
,ldr3TOC.fm.4587 Page v Thursday, January 20, 2005 9:30 AM
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. An Introduction to Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2
4
5
8
10
11
12
12
The Role of the Device Driver
Splitting the Kernel
Classes of Devices and Modules
Security Issues
Version Numbering
License Terms
Joining the Kernel Development Community
Overview of the Book
2. Building and Running Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
15
16
18
22
28
30
31
35
37
39
Setting Up Your Test System
The Hello World Module
Kernel Modules Versus Applications
Compiling and Loading
The Kernel Symbol Table
Preliminaries
Initialization and Shutdown
Module Parameters
Doing It in User Space
Quick Reference
3. Char Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
42
43
49
The Design of scull
Major and Minor Numbers
Some Important Data Structures
v
,ldr3TOC.fm.4587 Page vi Thursday, January 20, 2005 9:30 AM
Char Device Registration
open and release
scull’s Memory Usage
read and write
Playing with the New Devices
Quick Reference
55
58
60
63
70
70
4. Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
73
75
82
91
93
99
Debugging Support in the Kernel
Debugging by Printing
Debugging by Querying
Debugging by Watching
Debugging System Faults
Debuggers and Related Tools
5. Concurrency and Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
107
107
109
114
116
121
123
130
Pitfalls in scull
Concurrency and Its Management
Semaphores and Mutexes
Completions
Spinlocks
Locking Traps
Alternatives to Locking
Quick Reference
6. Advanced Char Driver Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
135
147
163
169
171
173
179
ioctl
Blocking I/O
poll and select
Asynchronous Notification
Seeking a Device
Access Control on a Device File
Quick Reference
7. Time, Delays, and Deferred Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
183
188
190
196
202
Measuring Time Lapses
Knowing the Current Time
Delaying Execution
Kernel Timers
Tasklets
vi
|
Table of Contents
,ldr3TOC.fm.4587 Page vii Thursday, January 20, 2005 9:30 AM
Workqueues
Quick Reference
205
208
8. Allocating Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
213
217
221
224
228
230
231
The Real Story of kmalloc
Lookaside Caches
get_free_page and Friends
vmalloc and Friends
Per-CPU Variables
Obtaining Large Buffers
Quick Reference
9. Communicating with Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
235
239
245
248
255
I/O Ports and I/O Memory
Using I/O Ports
An I/O Port Example
Using I/O Memory
Quick Reference
10.
Interrupt Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
259
Preparing the Parallel Port
259
Installing an Interrupt Handler
269
Implementing a Handler
Top and Bottom Halves
275
278
Interrupt Sharing
281
Interrupt-Driven I/O
Quick Reference
286
11. Data Types in the Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
288
290
291
292
295
299
Use of Standard C Types
Assigning an Explicit Size to Data Items
Interface-Specific Types
Other Portability Issues
Linked Lists
Quick Reference
12. PCI Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
302
319
322
The PCI Interface
A Look Back: ISA
PC/104 and PC/104+
Table of Contents
|
vii
,ldr3TOC.fm.4587 Page viii Thursday, January 20, 2005 9:30 AM
Other PC Buses
SBus
NuBus
External Buses
Quick Reference
322
323
324
325
325
13. USB Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
328
333
335
346
356
360
USB Device Basics
USB and Sysfs
USB Urbs
Writing a USB Driver
USB Transfers Without Urbs
Quick Reference
14. The Linux Device Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
364
371
375
377
387
391
397
405
407
Kobjects, Ksets, and Subsystems
Low-Level Sysfs Operations
Hotplug Event Generation
Buses, Devices, and Drivers
Classes
Putting It All Together
Hotplug
Dealing with Firmware
Quick Reference
15. Memory Mapping and DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
412
422
435
440
459
Memory Management in Linux
The mmap Device Operation
Performing Direct I/O
Direct Memory Access
Quick Reference
16. Block Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
465
471
474
491
494
Registration
The Block Device Operations
Request Processing
Some Other Details
Quick Reference
viii
|
Table of Contents