Transcription of LINUX - iitg.ac.in
1 LINUXDEVICEDRIVERS, Page i Thursday, January 27, 2005 12:06 PMOther LINUX resources from O ReillyRelated titlesUnderstanding the LinuxKernelLinux in a NutshellRunning LinuxLinux NetworkAdministrator s GuideLinux Pocket GuideBuilding Embedded LinuxSystemsDesigning EmbeddedHardwareLinux BooksResource a complete catalog of O Reilly s books onLinux and Unix and related technologies, including samplechapters and code the premier site for the open source web plat-form: LINUX , Apache, MySQL, and either Perl, Python, or Reilly brings diverse innovators together to nurture the ideasthat spark revolutionary industries. We specialize in document-ing the latest tools and systems, translating the innovator sknowledge into useful skills for those in the trenches. for our upcoming Bookshelf ( ) is the premier online refer-ence library for programmers and IT professionals.
2 Conductsearches across more than 1,000 books. Subscribers can zero inon answers to time-critical questions in a matter of 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., Page ii Thursday, January 27, 2005 12:06 PMLINUXDEVICEDRIVERSTHIRD EDITIONJ onathan Corbet, AlessandroRubini, and Greg Kroah-HartmanBeijing Cambridge Farnham K ln Paris Sebastopol Taipei Tokyo, Page iii Thursday, January 27, 2005 12:06 PMLinux Device Drivers, Third Editionby Jonathan Corbet, Alessandro Rubini, and Greg Kroah-HartmanCopyright 2005, 2001, 1998 O Reilly Media, Inc. All rights in the United States of by O Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA Reilly books may be purchased for educational, business, or sales promotional use.
3 Online editionsare also available for most titles ( ). For more information, contact our corporate/insti-tutional sales department: (800) 998-9938 OramProduction Editor:Matt HutchinsonProduction Services:Octal Publishing, Designer:Edie FreedmanInterior Designer:Melanie WangPrinting History:February 1998:First 2001:Second 2005:Third Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO Reilly Media, Inc. TheLinuxseries designations,LinuxDeviceDrivers, images of the American West,and related trade dress are trademarks of O Reilly Media, 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 of atrademark claim, the designations have been printed in caps or initial 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 work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike To view a copy of this license, send aletter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, book uses RepKover , a durable and flexible lay-flat : 0-596-00590-3[M], Page iv Thursday, January 27, 2005 12:09 PMvTable of ContentsPreface.
4 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 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 Drivers..42 The Design of scull 42 Major and Minor Numbers 43 Some Important Data Structures 49, Page v Thursday, January 20, 2005 9:30 AMvi|Table of ContentsChar Device Registration 55open and release 58scull s Memory Usage 60read and write 63 Playing with the New Devices 70 Quick Reference Techniques.
5 73 Debugging Support in the kernel 73 Debugging by Printing 75 Debugging by Querying 82 Debugging by Watching 91 Debugging System Faults 93 Debuggers and Related Tools and Race Conditions..106 Pitfalls in scull 107 Concurrency and Its Management 107 Semaphores and Mutexes 109 Completions 114 Spinlocks 116 Locking Traps 121 Alternatives to Locking 123 Quick Reference Char Driver Operations ..135ioctl 135 Blocking I/O 147poll and select 163 Asynchronous Notification 169 Seeking a Device 171 Access Control on a Device File 173 Quick Reference , Delays, and Deferred Work ..183 Measuring Time Lapses 183 Knowing the Current Time 188 Delaying Execution 190 kernel Timers 196 Tasklets 202, Page vi Thursday, January 20, 2005 9:30 AMTable of Contents|viiWorkqueues 205 Quick Reference Memory ..213 The Real Story of kmalloc 213 Lookaside Caches 217get_free_page and Friends 221vmalloc and Friends 224 Per-CPU Variables 228 Obtaining Large Buffers 230 Quick Reference with Hardware.
6 235I/O Ports and I/O Memory 235 Using I/O Ports 239An I/O Port Example 245 Using I/O Memory 248 Quick Reference Handling ..258 Preparing the Parallel Port 259 Installing an Interrupt Handler 259 Implementing a Handler 269 Top and Bottom Halves 275 Interrupt Sharing 278 Interrupt-Driven I/O 281 Quick Reference Types in the kernel ..288 Use of Standard C Types 288 Assigning an Explicit Size to Data Items 290 Interface-Specific Types 291 Other Portability Issues 292 Linked Lists 295 Quick Reference Drivers..302 The PCI Interface 302A Look Back: ISA 319PC/104 and PC/104+ 322, Page vii Thursday, January 20, 2005 9:30 AMviii|Table of ContentsOther PC Buses 322 SBus 323 NuBus 324 External Buses 325 Quick Reference Drivers ..327 USB Device Basics 328 USB and Sysfs 333 USB Urbs 335 Writing a USB Driver 346 USB Transfers Without Urbs 356 Quick Reference LINUX Device Model.
7 362 Kobjects, Ksets, and Subsystems 364 Low-Level Sysfs Operations 371 Hotplug Event Generation 375 Buses, Devices, and Drivers 377 Classes 387 Putting It All Together 391 Hotplug 397 Dealing with Firmware 405 Quick Reference Mapping and DMA ..412 Memory Management in LINUX 412 The mmap Device Operation 422 Performing Direct I/O 435 Direct Memory Access 440 Quick Reference Drivers ..464 Registration 465 The Block Device Operations 471 Request Processing 474 Some Other Details 491 Quick Reference 494, Page viii Thursday, January 20, 2005 9:30 AMTable of Contents| Drivers ..497 How snull Is Designed 498 Connecting to the kernel 502 The net_device Structure in Detail 506 Opening and Closing 515 Packet Transmission 516 Packet Reception 521 The Interrupt Handler 523 Receive Interrupt Mitigation 525 Changes in Link State 528 The Socket Buffers 528 MAC Address Resolution 532 Custom ioctl Commands 535 Statistical Information 536 Multicast 537A Few Other Details 540 Quick Reference Drivers.
8 546A Small TTY Driver 548tty_driver Function Pointers 553 TTY Line Settings 560ioctls 564proc and sysfs Handling of TTY Devices 566 The tty_driver Structure in Detail 567 The tty_operations Structure in Detail 569 The tty_struct Structure in Detail 571 Quick Reference 573 Bibliography..575 Index ..579, Page ix Thursday, January 20, 2005 9:30 AM, Page x Thursday, January 20, 2005 9:30 AMThis is the Title of the Book, eMatter EditionCopyright 2005 O Reilly & Associates, Inc. All rights is, on the surface, a book about writing device drivers for the LINUX is a worthy goal, of course; the flow of new hardware products is not likely toslow down anytime soon, and somebody is going to have to make all those new gad-gets work with LINUX . But this book is also about how the LINUX kernel works andhow to adapt its workings to your needs or interests.
9 LINUX is an open system; withthis book, we hope, it is more open and accessible to a larger community of is the third edition ofLinux Device Drivers. The kernel has changed greatlysince this book was first published, and we have tried to evolve the text to edition covers the kernel as completely as we are able. We have, this timearound, elected to omit the discussion of backward compatibility with previous ker-nel versions. The changes from are simply too large, and the interfaceremains well documented in the (freely available) second edition contains quite a bit of new material relevant to the kernel . The dis-cussion of locking and concurrency has been expanded and moved into its ownchapter. The LINUX device model, which is new in , is covered in detail. There arenew chapters on the USB bus and the serial driver subsystem; the chapter on PCI hasalso been enhanced.
10 While the organization of the rest of the book resembles that ofthe earlier editions, every chapter has been thoroughly hope you enjoy reading this book as much as we have enjoyed writing s IntroductionThe publication of this edition coincides with my twelth year of working with Linuxand, shockingly, my twenty-fifth year in the computing field. Computing seemed likea fast-moving field back in 1980, but things have sped up a lot since then. KeepingLinux Device Driversup to date is increasingly a challenge; the LINUX kernel hackerscontinue to improve their code, and they have little patience for documentation thatfails to keep up., Page xi Thursday, January 27, 2005 12:11 PMThis is the Title of the Book, eMatter EditionCopyright 2005 O Reilly & Associates, Inc. All rights |PrefaceLinux continues to succeed in the market and, more importantly, in the hearts andminds of developers worldwide.