linux device drivers third edition

638 3.7K 0
linux device drivers third edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info www.it-ebooks.info LINUX DEVICE DRIVERS www.it-ebooks.info 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. www.it-ebooks.info LINUX DEVICE DRIVERS THIRD EDITION Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info 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. 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: 978-0-596-00590-0 [M] [1/10] www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info [...]... Chapter 1: An Introduction to Device Drivers This is the Title of the Book, eMatter Edition www.it-ebooks.info Copyright © 2010 O’Reilly & Associates, Inc All rights reserved Block devices Like char devices, block devices are accessed by filesystem nodes in the /dev directory A block device is a device (e.g., a disk) that can host a filesystem In most Unix systems, a block device can only handle I/O... and so on Every USB device is driven by a USB module that works with the USB subsystem, but the device itself shows up in the system as a char device (a USB serial port, say), a block device (a USB memory card reader), or a network device (a USB Ethernet interface) Other classes of device drivers have been added to the kernel in recent times, including FireWire drivers and I2C drivers In the same way... makes Linux drivers easy to write, to the point that there are now hundreds of them available There are a number of reasons to be interested in the writing of Linux device drivers The rate at which new hardware becomes available (and obsolete!) alone guarantees that driver writers will be busy for the foreseeable future Individuals may need to know about drivers in order to gain access to a particular device. .. more; but ideas are moving fast these days, and it’s already time to plan for the fourth edition, and look for a fourth author to help Greg’s Introduction It seems like a long time ago that I picked up the first edition of this Linux Device Drivers book in order to figure out how to write a real Linux driver That first edition was a great guide to helping me understand the internals of this operating system... by anybody with the requisite skills Linux has helped to democratize operating systems The Linux kernel remains a large and complex body of code, however, and would-be kernel hackers need an entry point where they can approach the code without being overwhelmed by complexity Often, device drivers provide that gateway Device drivers take on a special role in the Linux kernel They are distinct “black... Associates, Inc All rights reserved Device control Almost every system operation eventually maps to a physical device With the exception of the processor, memory, and a very few other entities, any and all device control operations are performed by code that is specific to the device being addressed That code is called a device driver The kernel must have embedded in it a device driver for every peripheral... 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... larger power of two) bytes in length Linux, instead, allows the application to read and write a block device like a char device it permits the transfer of any number of bytes at a time As a result, block and char devices differ only in the way data is managed internally by the kernel, and thus in the kernel/driver software interface Like a char device, each block device is accessed through a filesystem... 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 gadgets 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... SCSI drivers, kernel developers collected class-wide features and exported them to driver implementers to avoid duplicating work and bugs, thus simplifying and strengthening the process of writing such drivers Classes of Devices and Modules | This is the Title of the Book, eMatter Edition www.it-ebooks.info Copyright © 2010 O’Reilly & Associates, Inc All rights reserved 7 In addition to device drivers, . www.it-ebooks.info www.it-ebooks.info LINUX DEVICE DRIVERS www.it-ebooks.info Other Linux resources from O’Reilly Related titles Understanding the Linux Kernel Linux in a Nutshell Running Linux Linux Network Administrator’s. trial. www.it-ebooks.info LINUX DEVICE DRIVERS THIRD EDITION Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Linux Device. 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

Ngày đăng: 24/04/2014, 15:28

Mục lục

  • Audience for This Book

  • Organization of the Material

  • Online Version and License

  • Conventions Used in This Book

  • We’d Like to Hear from You

  • An Introduction to Device Drivers

    • The Role of the Device Driver

    • Splitting the Kernel

      • Loadable Modules

      • Classes of Devices and Modules

      • Joining the Kernel Development Community

      • Overview of the Book

      • Building and Running Modules

        • Setting Up Your Test System

        • The Hello World Module

        • Kernel Modules Versus Applications

          • User Space and Kernel Space

          • Concurrency in the Kernel

          • A Few Other Details

          • Compiling and Loading

            • Compiling Modules

            • Loading and Unloading Modules

            • The Kernel Symbol Table

            • Initialization and Shutdown

              • The Cleanup Function

              • Error Handling During Initialization

Tài liệu cùng người dùng

Tài liệu liên quan