Idr3TOC.fm.4587 Page vi Thursday,January 20, 2005 9:30 AM55Char Device Registration58open and release60scull's MemoryUsage63read and write70Playing with the NewDevices70QuickReference734. Debugging Techniques73Debugging Support in the Kernel75Debuggingby Printing82Debugging by Querying91Debugging by Watching93Debugging System Faults99Debuggers and Related Tools5.106ConcurrencyandRaceConditions107Pitfalls in scull107ConcurrencyandItsManagement109Semaphoresand Mutexes114Completions116Spinlocks121Locking Traps123Alternatives to Locking130QuickReference135Advanced CharDriver Operations6.135ioctl147Blocking I/O163poll and select169Asynchronous Notification171Seeking a Device173Access Control on a Device File179QuickReference7.Time,Delays,andDeferredWork183183Measuring TimeLapses188Knowing the Current Time190Delaying Execution196Kernel Timers202TaskletsviTable ofContents
vi | Table of Contents 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 ,ldr3TOC.fm.4587 Page vi Thursday, January 20, 2005 9:30 AM
Idr3TOC.fm.4587 Page vii Thursday, January 20, 2005 9:30 AMWorkqueues205208Quick Reference2138.Allocating Memory213TheReal Storyofkmalloc217Lookaside Caches221get_free_page and Friends224vmalloc and Friends228Per-CPUVariables230Obtaining Large Buffers231Quick Reference2359.CommunicatingwithHardware235VO Ports and I/O Memory239Using I/O Ports245An I/O Port Example248Using I/O Memory255QuickReference25810.Interrupt Handling259Preparing theParallel Port259Installing an Interrupt Handler269Implementing a Handler275Top and Bottom Halves278Interrupt Sharing281Interrupt-Driven I/O286Quick Reference11. Data Types in the Kernel288288Use of Standard CTypes290AssigninganExplicitSizetoDataItems291Interface-Specific Types292OtherPortabilityIssues295Linked Lists299QuickReference30212.PCIDrivers302ThePCIInterface319A Look Back: ISA322PC/104andPC/104+Table of Contents Ivii
Table of Contents | vii 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 ,ldr3TOC.fm.4587 Page vii Thursday, January 20, 2005 9:30 AM
Idr3TOC.fm.4587 Page vili Thursday, January 20, 2005 9:30 AM322OtherPCBusesSBus323324NuBus325External Buses325QuickReference32713.USBDrivers328USBDevice Basics333USB and Sysfs335USBUrbsWritingaUSBDriver346356USBTransfers Without Urbs360QuickReference36214.TheLinuxDeviceModelKobjects, Ksets, and Subsystems364371Low-Level Sysfs Operations375Hotplug EventGeneration377Buses, Devices, and Drivers387Classes391 Putting It All Together397Hotplug405Dealing with Firmware407QuickReference41215.MemoryMapping andDMA412MemoryManagement inLinux422The mmap Device Operation435Performing Direct I/O440Direct MemoryAccess459Quick ReferenceBlock Drivers46416.465Registration471The Block Device Operations474Request ProcessingSome Other Details491494QuickReferenceviliTable of Contents
viii | Table of Contents 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 ,ldr3TOC.fm.4587 Page viii Thursday, January 20, 2005 9:30 AM
Idr3TOC.fm.4587 Page ix Thursday,January 20, 2005 9:30 AM49717.Network Drivers498How snull Is Designed502Connecting to the Kernel506Thenet_deviceStructureinDetail515Opening and Closing516Packet Transmission521Packet Reception523The Interrupt Handler525Receive Interrupt Mitigation528Changes in Link State528The Socket BuffersMAC Address Resolution532535Custom ioctl Commands536Statistical Information537Multicast540AFewOtherDetails542QuickReference18.TTYDrivers546548A Small TTY Driver553tty_driver Function Pointers560TTY Line Settings564ioctls566 proc and sysfs Handling of TTY Devices567The tty_driver Structure in Detail569The tty_operations Structure in Detail571The tty_struct Structure in Detail573QuickReference575Bibliography579IndexTable of Contentsix
Table of Contents | ix 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 ,ldr3TOC.fm.4587 Page ix Thursday, January 20, 2005 9:30 AM
Jdr3TOC.fm.4587 Page x Thursday, January 20, 2005 9:30 AM
,ldr3TOC.fm.4587 Page x Thursday, January 20, 2005 9:30 AM