Digitized by the Internet Archive in 2018 with funding from Kahle/Austin Foundation https://archive.org/details/writingunixdevicOOpaja_O Writing UNIX® Device Drivers George Pajari Addison-Wesley Publishing Company, Inc. Reading, Massachusetts Menlo Park, California New York Don Mills, Ontario Wokingham, England Amsterdam Bonn Sydney Singapore Tokyo Madrid San Juan Paris Seoul Milan Mexico City Taipei Many of the designations used by manufacturers and sellers to distin¬ guish their products are claimed as trademarks. Where those designa¬ tions appear in this book and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial capital letters. The authors and publishers have taken care in preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for inciden¬ tal or consequential damages in connection with or arising out of the use of the information or programs contained herein. Library of Congress Cataloging-in-Publication Data Pajari, George. Writing UNIX device drivers / by George Pajari. p. cm. Includes index. ISBN 0-201-52374-4 1. UNIX device drivers (Computer programs) I. Title. QA76.76.D49P35 1991 005.4’3—dc20 91-42361 Copyright © 1992 by George Pajari All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultaneously in Canada. ISBN 0-201-52374-4 Managing Editor: Amorette Pedersen Set in 11-point New Century Schoolbook by Benchmark Productions 2 3 4 5 6 7 8 9-MW-95949392 Second printing, April 1992 This book is dedicated to Carol, who has taken the fun out of being alone. Acknowledgments « To my colleagues at Driver Design Labs: Jeff Tate, who checked that it made sense and helped make it somewhat understandable; Carolanne Reynolds, who spent many hours reading and revising drafts trying to convert it into something much closer to English; and to Frank Pronk who provided a useful reality check. The unseen corrections and improvements are theirs (many remaining problems and errors are mine). To Harry Henderson, Mitch Waite, Rebecca Thomas, David Flack, Tom Ward, Ben Smith, and Jane Tazelaar; all of whom have helped with one or more of my previously published works on device drivers. To Chris Williams and Amy Pedersen who, as editors of this book, exhib¬ ited patience and understanding above and beyond the call. No author could ask for more, and aspiring authors of technical books are urged to contact them. To Jeff Kulick and the Department of Computing and Information Science at Queen’s University at Kingston: both gave me the opportunity to play with the UNIX kernel well over a decade ago and both have to take at least some responsibility for all of this. To Ed Palmer and the organizers of the annual UniForum Conference and Exhibition, where the author has had the pleasure of delivering the Writing UNIX Device Drivers tutorial for several years. The course notes for those tutorials formed the basis for this book. To the Scottish & Newcastle Breweries PLC Edinburgh, whose McEwan’s Scotch Malt Liquor had a very enjoyable, if detrimental, effect on the writing of this book. To my parents and grandparents who have always encouraged and sup¬ ported me. And most importantly to my wife, Carolanne Reynolds, for more reasons than there is room in these notes to list. in Table of Contents Chapter 1 y What Is A Device Driver? 1 The Grand Design 1 The Details 3 Types of Device Drivers 6 The Gross Anatomy of a Device Driver 10 General Programming Considerations 12 Summary 13 Chapter 2 Character Driver I: A Test Data Generator 15 The Design Issues 16 The Driver 19 The Recapitulation 29 Summary 31 Exercises 32 Chapter 3 Character Drivers II: An A/D Converter 35 The Design Issues 37 The Driver 37 Exercises 54 iv Table of Contents u Chapter 4 Character Drivers III: A Line Printer 55 The Device 56 The Driver 56 Summary 78 Exercises 78 Chapter 5 Block Drivers I: A Test Data Generator 81 The Design Issues 83 The Driver 84 Summary 92 Exercises 92 Chapter 6 Block Drivers III: A RAM Disk Driver 93 The Design Issues 93 The Driver 94 Summary 106 Exercises 107 7 Chapter Block Drivers III: A SCSI Disk Driver 109 The Design Issues 109 The Device 110 Summary 145 Exercises 145 Chapter 8 Character Drivers IV: The Raw Disk Driver 147 DMA and Character Drivers 148 The General Structure 151 Exercises 153 vi WRITING UNIX DEVICE DRIVERS Chapter 9 Terminal Drivers I: The C0M1 Port 155 Line Disciplines 156 The Device 156 The Driver 157 Beyond Line Disciplines 185 Summary 185 Exercises 186 Chapter 10 Character Drivers V: A Tape Drive 187 A SCSI Tape Drive 187 The Driver 189 Possible Enhancements 207 Summary 209 Exercises 209 Chapter 11 STREAMS Drivers I: A Loop-Back Driver 211 The Design Issues 219 A Loop-Back STREAMS Driver 219 Summary 230 Exercises 231 Chapter 12 Streams Drivers II: The COM1 Port (Revisited) 233 The Driver 234 Suggestions for Improvements 273 Summary 274 Exercises 274 Table of Contents vii Chapter 13 Driver Installation 275 Compiling the Driver 276 Configuring the Kernel 276 Installing the New Driver 279 Building a New Kernel 280 Creating Entries in /dev 280 Rebooting the New Kernel 281 Installation Example 282 Advanced Driver Installation 284 Exercises 285 Chapter 14 Zen and the Art of Device Driver Writing 287 Preparing to Write a Driver 289 Debugging Device Drivers 291 Driver Performance 296 Unusual Driver Applications 300 Exercises 301 Chapter 15 Writing Drivers for System V Release 4 303 The Good News 304 The New Order 304 Sample SVR4 Drivers 307 Summary 311 Exercises 312 Index 313 Preface So you want to write a UNIX device driver. Or perhaps you just want to learn a bit more about a topic that has historically been the exclusive domain of systems gurus and programming wizards. In either case, this book is written expressly for you. Writing UNIX Device Drivers Writing device drivers is one of the most challenging and interesting types of programming. In contrast to applications programming, the device driver writer has to be concerned with hardware details at the low¬ est levels as well as the problems of concurrency. These additional com¬ plexities not only make device driver programming much more difficult and frustrating, they make it far more interesting and challenging. Let us consider some of the problems that regularly arise during the development of a device driver—problems which, by the way, rarely if ever affect applications programmers. Device drivers interface with the kernel using an entirely different set of routines than those used by applications software. This programming interface is all too often incompletely (or incorrectly) documented, fre¬ quently leaving the device driver programmer to debug the documenta¬ tion as well as the driver. Device drivers usually interface with hardware (i.e., disk drives or tape drives). Not only is this interface almost always incompletely documented, it usually suffers from idiosyncrasies that are very different from software interfaces. Some hardware devices will malfunction if their control regis¬ ters are written in the wrong order, or if written before being read, or if vm