TITLE.9502 Pagei Thursday,January 27, 2005 12:06 PMLINUXDEVICEDRIVERS
LINUX DEVICE DRIVERS ,TITLE.9502 Page i Thursday, January 27, 2005 12:06 PM
TITLE.9502 Pagei Thursday, January 27, 2005 12:06 PMOtherLinuxresourcesfromO'ReillyRelated titlesUnderstandingtheLinuxLinux Pocket GuideKernelBuilding Embedded LinuxLinux in a NutshellSystemsDesigning EmbeddedRunning LinuxHardwareLinux NetworkAdministrator's GuideLinux Bookslinux.oreilly.com is a complete catalog of O'Reilly's books onResourceCenterLinux and Unix and related technologies, including samplechaptersand code examples.ONLamp.com isthepremiersitefortheopen sourcewebplat-ONLamp.comform:Linux, Apache, MySQL, and either Perl, Python, or PHP.ConferencesO'Reilly brings diverse innovators together to nurture the ideasthat spark revolutionaryindustries.We specialize in documenting thelatest tools and systems, translating the innovator'sknowledge into useful skills for those in the trenches. Visit con-ferences.oreilly.comforourupcomingevents.RELLYNETWORKSafari Bookshelf (safari.oreilly.com) is the premier online refer-Safarience library for programmers and IT professionals. ConductBookshelfsearches across more than 1,ooo books. Subscribers can zero inon 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
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 platform: 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 documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online reference 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 simply flip to the page you need. Try it today with a free trial. ,TITLE.9502 Page ii Thursday, January 27, 2005 12:06 PM
TITLE.9502Page iliThursday,January 27,200512:06PMLINUXDEVICEDRIVERSTHIRDEDITIONJonathan Corbet, AlessandroRubini, and Greg Kroah-HartmanO'REILLYBejing · Cambridge·Farnham Koln - Paris -Sebastopol ·Taipei · Tokyo
LINUX DEVICE DRIVERS THIRD EDITION Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo ,TITLE.9502 Page iii Thursday, January 27, 2005 12:06 PM
COPYRIGHT.10321 Page iv Thursday, January 27, 2005 12:09 PMLinux Device Drivers,Third Editionby Jonathan Corbet, Alessandro Rubini, and Greg Kroah-HartmanCopyright 2005, 2001, 1998 0'Reilly Media, Inc. Allrights reserved.Printed in the United States of America.PublishedbyO'ReillyMedia,Inc.,1005GravensteinHighwayNorth,Sebastopol,CA95472O'Relly books may be purchased for educational, business, or sales promotional use. Online editionsare also availableformost titles (safari.oreilly.com).For more information,contact our corporate/insti-tutional salesdepartment: (800) 998-9938or corporate@orelly.com.Editor:Andy OramProduction Editor:Matt HutchinsonProduction Services:Octal Publishing, Inc.Cover Designer:Edie FreedmanInterior Designer:Melanie WangPrinting 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 ofO'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 astrademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware ofatrademark 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 authorsassume no responsibility for errors or omissions, or for damages resulting from the use of theinformation contained herein.This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.0License.To view a copyof this license,visithttp:/lcreativecommons.org/licenses/by-sa/2.0/orsendaletter toCreativeCommons,559NathanAbbottWay,Stanford,California94305,USARepKoverThis book uses RepKover, a durable and flexible lay-flat binding.ISBN:0-596-00590-3[M]
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/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Andy Oram Production Editor: Matt Hutchinson Production Services: Octal Publishing, Inc. Cover Designer: Edie Freedman Interior Designer: 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] ,COPYRIGHT.10321 Page iv Thursday, January 27, 2005 12:09 PM
Idr3TOC.fm.4587 Page v Thursday,January 20, 2005 9:30 AMTable of ContentsPrefacexi1.AnIntroductiontoDeviceDrivers12TheRole oftheDeviceDriver4Splitting the Kernel5Classes of Devices and Modules8SecurityIssues10Version Numbering11License Terms12Joining the Kernel Development Community12Overview of the Book152.Building and Running Modules15Setting Up Your Test System16The Hello World Module18Kernel Modules Versus Applications22Compiling and Loading28The Kernel Symbol Table30Preliminaries31Initializationand Shutdown35ModuleParameters37Doing It in User Space39QuickReference423.CharDrivers中42TheDesign of scull43Major and Minor Numbers49SomeImportantData Structuresv
v Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. An Introduction to Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Role of the Device Driver 2 Splitting the Kernel 4 Classes of Devices and Modules 5 Security Issues 8 Version Numbering 10 License Terms 11 Joining the Kernel Development Community 12 Overview of the Book 12 2. Building and Running Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Setting Up Your Test System 15 The Hello World Module 16 Kernel Modules Versus Applications 18 Compiling and Loading 22 The Kernel Symbol Table 28 Preliminaries 30 Initialization and Shutdown 31 Module Parameters 35 Doing It in User Space 37 Quick Reference 39 3. Char Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Design of scull 42 Major and Minor Numbers 43 Some Important Data Structures 49 ,ldr3TOC.fm.4587 Page v Thursday, January 20, 2005 9:30 AM