DEVICE DRIVERS ,TITLE.9502 Page Ii Thursday, January 27, 2005 12:06 PM
Total Page:16
File Type:pdf, Size:1020Kb
,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 Linux Pocket Guide Kernel Building Embedded Linux Linux in a Nutshell Systems Running Linux Designing Embedded Linux Network Hardware Administrator’s Guide Linux Books linux.oreilly.com is a complete catalog of O’Reilly’s books on Resource Center 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 ,ldr3TOC.fm.4587 Page v Thursday, January 20, 2005 9:30 AM 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 v ,ldr3TOC.fm.4587 Page vi Thursday, January 20, 2005 9:30 AM Char Device Registration 55 open and release 58 scull’s Memory Usage 60 read and write 63 Playing with the New Devices 70 Quick Reference 70 4. Debugging Techniques . 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 99 5. Concurrency 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 130 6. Advanced Char Driver Operations . 135 ioctl 135 Blocking I/O 147 poll and select 163 Asynchronous Notification 169 Seeking a Device 171 Access Control on a Device File 173 Quick Reference 179 7. Time, Delays, and Deferred Work . 183 Measuring Time Lapses 183 Knowing the Current Time 188 Delaying Execution 190 Kernel Timers 196 Tasklets 202 vi | Table of Contents ,ldr3TOC.fm.4587 Page vii Thursday, January 20, 2005 9:30 AM Workqueues 205 Quick Reference 208 8. Allocating Memory . 213 The Real Story of kmalloc 213 Lookaside Caches 217 get_free_page and Friends 221 vmalloc and Friends 224 Per-CPU Variables 228 Obtaining Large Buffers 230 Quick Reference 231 9. Communicating with Hardware . 235 I/O Ports and I/O Memory 235 Using I/O Ports 239 An I/O Port Example 245 Using I/O Memory 248 Quick Reference 255 10. Interrupt 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 286 11. Data 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 299 12. PCI Drivers . 302 The PCI Interface 302 A Look Back: ISA 319 PC/104 and PC/104+ 322 Table of Contents | vii ,ldr3TOC.fm.4587 Page viii Thursday, January 20, 2005 9:30 AM Other PC Buses 322 SBus 323 NuBus 324 External Buses 325 Quick Reference 325 13. USB 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 360 14. The Linux Device Model . 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 407 15. Memory 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 459 16. Block Drivers . 464 Registration 465 The Block Device Operations 471 Request Processing 474 Some Other Details 491 Quick Reference 494 viii | Table of Contents ,ldr3TOC.fm.4587 Page ix Thursday, January 20, 2005 9:30 AM 17. Network 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 537 A Few Other Details 540 Quick Reference 542 18. TTY Drivers . 546 A Small TTY Driver 548 tty_driver Function Pointers 553 TTY Line Settings 560 ioctls 564 proc 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 Table of Contents | ix ,ldr3TOC.fm.4587 Page x Thursday, January 20, 2005 9:30 AM ,ch00.11770 Page xi Thursday, January 27, 2005 12:11 PM Preface 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 gad- gets 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 is more open and accessible to a larger community of developers. This is the third edition of Linux Device Drivers. The kernel has changed greatly since this book was first published, and we have tried to evolve the text to match. This edition covers the 2.6.10 kernel as completely as we are able. We have, this time around, elected to omit the discussion of backward compatibility with previous ker- nel versions. The changes from 2.4 are simply too large, and the 2.4 interface remains well documented in the (freely available) second edition. This edition contains quite a bit of new material relevant to the 2.6 kernel. The dis- cussion of locking and concurrency has been expanded and moved into its own chapter. The Linux device model, which is new in 2.6, is covered in detail. There are new chapters on the USB bus and the serial driver subsystem; the chapter on PCI has also been enhanced. While the organization of the rest of the book resembles that of the earlier editions, every chapter has been thoroughly updated. We hope you enjoy reading this book as much as we have enjoyed writing it. Jon’s Introduction The publication of this edition coincides with my twelth year of working with Linux and, shockingly, my twenty-fifth year in the computing field. Computing seemed like a fast-moving field back in 1980, but things have sped up a lot since then. Keeping Linux Device Drivers up to date is increasingly a challenge; the Linux kernel hackers continue to improve their code, and they have little patience for documentation that fails to keep up. xi This is the Title of the Book, eMatter Edition Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved. ,ch00.11770 Page xii Thursday, January 27, 2005 12:11 PM Linux continues to succeed in the market and, more importantly, in the hearts and minds of developers worldwide. The success of Linux is clearly a testament to its technical quality and to the numerous benefits of free software in general. But the true key to its success, in my opinion, lies in the fact that it has brought the fun back to computing. With Linux, anybody can get their hands into the system and play in a sandbox where contributions from any direction are welcome, but where technical excellence is valued above all else. Linux not only provides us with a top-quality operating system; it gives us the opportunity to be part of its future development and to have fun while we’re at it. In my 25 years in the field, I have had many interesting opportunities, from program- ming the first Cray computers (in Fortran, on punch cards) to seeing the minicom- puter and Unix workstation waves, through to the current, microprocessor- dominated era. Never, though, have I seen the field more full of life, opportunity, and fun.