Block Device Drivers
Total Page:16
File Type:pdf, Size:1020Kb
Writing Device Drivers for sea UNIX Writing Device Drivers forSCOUNIX A Practical Approach Peter Kettle The Santa Cruz Operation Ltd Steve Statler Sequent Computer Systems Ltd ~~• Addison-Wesley Publishing Company Wokingham, England . Reading, Massachusetts . Menlo Park, California New York· Don Mills, Ontario· Amsterdam· Bonn· Sydney· Singapore Tokyo· Madrid· San Juan· Milan· Paris· Mexico City· Seoul· Taipei © 1993 Peter Kettle and Steve Statler 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 prior written permission of the publisher. The programs in this book have been included for their instructional value. They have been tested with care but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Addison-Wesley has made every attempt to supply trademark information about manufacturers and their products mentioned in this book. A list of the trademark designations and their owners appears on p. xvi. Cover designed by Designers & Partners of Oxford and printed by The Riverside Printing Co. (Reading) Ltd. Typeset by CRB (Drayton) Typesetting Services, Norwich, Norfolk. Printed in Great Britain at the University Press, Cambridge. First printed 1992. British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library. Library of Congress Cataloging in Publication Data Kettle, Peter. Writing device drivers for SCO UNIX: a practical approach {Peter Kettle, Steve Statler. p. cm. Includes index. ISBN 0-201-54425-3 1. UNIX device drivers (computer programs) I. Statler, Steve. II. Title. QA76.76.D49K48 1992 005,4'3--dc20 92-36521 CIP This book is dedicated to our parents. Foreword UNIX started life as an operating system for technical or scientific users, and following its adoption as the basis for the Open System operating environment, it has now progressed to become a general purpose operating environment for business applications. By com bining UNIX and computer systems based on industry standard Personal Computer hardware, solutions based on Open Systems are now becoming increasingly popular in an ever-expanding range of applications. This move into new territories has been largely as a result of the desire by large scale users of computers to build complex systems out of standard low-cost components and to break free from the restrictions created when they have a single source provider for their computer hardware. The Open Systems standards established by organizations such as XlOpen and the commitment to these standards from software pro viders such as sca, have meant that highly powerful systems that are capable of running the most sophisticated applications can be put together at previously undreamed of costs to match the users' needs exactly. This flexibility to produce powerful and sophisticated systems has created a need for special software device drivers to be produced to run the ever-increasing range of special hardware products available for standard PC hardware. There are already many hundreds of device drivers available for SCO UNIX and yet every week many new requirements appear. These might be for a new point-of-sale ter minal, a new type of tape back-up drive, a new graphical display card, or maybe for an intelligent telephone exchange! We at SCO are totally committed to the Open System process, and through this process, to providing our customers with the freedom to put together the very best systems that precisely meet their needs. The development of special device drivers for incorporating the optimum components into a system is a key element in our approach vii viii Foreword to Open Systems. I believe that this excellent book based on training courses delivered by the Santa Cruz Operation provides you with an insight into the requirements for producing a device driver for SCO UNIX and I hope that it will encourage you to take on the challenge of developing many new device drivers. Lars Turndal Senior Vice President and Managing Director The Santa Cruz Operation Ltd Preface Welcome! This book is written for students of computer science and systems programming professionals. Our objective is to offer information about writing UNIX device drivers and the operation of the UNIX kernel that is practical and accessible. On successful completion of this book you will be able to write a variety of device drivers. If you have completed the exercises set at the end of each chapter, you will have built a UNIX kernel, written a device driver for a mouse, experimented with interrupts, written a simple line discipline, written a Stream driver and modified a disk driver. You are likely to have a better grasp of operating system functions and the inside of UNIX than most of your colleagues in the computer industry. Although it is rare for most systems programmers to have to write device drivers, the investigation of this topic can pay many divi dends. It yields an understanding of the following areas: (1) The structure and mechanisms of an Operating System. (2) Device drivers and the concept of device independence. (3) Computer and peripheral hardware architecture. To provide an accessible environment, the exercises are designed for SCO UNIX. At the time of writing the current release is SCO UNIX Release 3.2 Version 4.0. The exercises are set at the end of each chapter with hints and model answers to aid the reader. They can be readily adapted to operate on other vendors' versions of UNIX after consultation with the appropriate implementation-specific documen tation. Although we use UNIX as the basis for this book, many of the principles that we discuss extend to other operating systems. The information that we have distilled into this book has tradi tionally been fragmented in many different places: computer science ix x Preface text books, hardware manuals, software guides and the minds of a few developers who may be difficult to contact or to understand. Conventions Throughout the text of the book, when UNIX system calls or com mands are mentioned, they will be followed by an abbreviation in parentheses to indicate where they are documented in the SCO UNIX manual set. For example run (CP) indicates that the nrn command is documented under Commands: Programming (CP) in the Program mer's Reference Manual. UNIX commands, system calls and function names will be in constant width type, as will code extracts, structures and variables. Where UNIX commands are listed they will be preceded by a prompt that will indicate whether the command needs to be typed as root (#) or any user ($). A convention that we have not adhered to is the use of troff (CT) in the preparation of this book. We used Microsoft Word! Prerequisites In order to gain the most from this book it is necessary to have experience of using the C programming language, including an understanding of the use of libraries, system calls, pointers and bitwise operations. In order to attempt the exercises it will be neces sary to have experience of developing programs using UNIX and access to a machine running SCO UNIX. Some knowledge of the issues relating to UNIX system administration would also be useful but is not essential. Choice of operating system UNIX is supported by nearly every major mainframe, mini and micro computer manufacturer in the world. Since its appearance in the early 1970s it has been adopted by Altos, Amdahl, AT&T, Bull, Cray, Data General, DEC, Fujitsu, HP, IBM, ICL, Intel, MIPS, Motorola, NCR, Olivetti, Prime, Sequent, Siemens/Nixdorf, Sun, Tandem, Tandy, Unisys, Wang and many others. SCO XENIX and latterly sca UNIX have proliferated throughout industry and academia, running on many different manufacturers' computers and in greater quantities than any other variant of UNIX. Preface xi The latest estimates run at 500000 sea licences sold, most of which are multi-user licences. sea UNIX will run on some of the least expensive Intel i386 and i486 microprocessor-based computers. It is likely that if you have a Personal Computer in your office that is running UNIX, it will be running sea UNIX. Device drivers for sea XENIX and sea UNIX differ in some areas. We have chosen sea UNIX as a basis for the examples in this book, as most developers are working with sea UNIX rather than XENIX. How to use this book At the end of each chapter there is a short quiz. We recommend that you use these to test your understanding of the chapter. There is also a practical exercise at the end of each chapter. Answers to quizzes and exercises are provided in separate chapters at the end of the text. If you attempt the quizzes and the exercises, you are more likely to retain more of what you have read, gain a deeper understanding of the topics that we have discussed, derive more satisfaction and have an opportunity to learn about related issues through the process of exploration. We also suggest that you discard the conventional reverence for books and annotate the text wherever needed. Materials required to perform the exercises If you are going to attempt the exercises, the following materials will be useful: (1) A copy of sea UNIX and the sea UNIX Development System. (2) A computer based on either the Intel i386 or i486 microprocessor, with the above software installed. (3) A copy of the sea UNIX Device Driver Writer's Guide, which contains more information about Sea-specific details. (4) For a number of the exercises, you will need a mouse.