Writing OS/2 2.1 Device Drivers in C Book Review

By Carsten Whimster

Introduction
The Codesmith's Library is a monthly column which focuses on development oriented books and materials. This will mostly consist of programming books, such as this month's Learning to Program OS/2 2.0 Presentation Manager by Example, but could also cover books like The Design of OS/2. The column is from a beginning PM programmer's eyes (because that's what I am), and hopefully that is most useful to the community in general. As I get on with the column, however, I expect to get better, and so within a year or so, I will presumably :) qualify as an intermediate programmer, or perhaps even somewhat of an expert. Hopefully you, the readers, will move with me in that respect. When reading this column, try to pick up whichever book strikes your fancy, and join the growing group of people following our introductory PM programming columns. I will try to review books aimed at beginners to start with, and then move on from there.

The column will largely be demand-oriented (after I get my own books out of the way early on), so if you have a book you would like reviewed, send me e-mail and tell me about it, or even better, send me the book. Finally, please send me your comments and thoughts so that I can make this column as effective as possible. After all, this is our magazine, and it will be most effective with lots of reader feedback.

This month's selection was chosen because I own a copy :), and because it has a number of good chapters in it. It was purchased through IBM. It is obviously for the previous version of OS/2, and so does not have any 2.1 features in it. However, most of the API is unchanged so there is still lots of useful material in it.

Errata
I have just picked up Watcom C/C++ 9.5, so things have changed a bit since last month. The makefile that comes with Real World Programming for OS/2 2.1 works fine, with a couple of modifications:

Add a section for C++, if you plan on using the makefile for C++ programming. You can just copy the C section, and modify it as follows: .cpp.obj: @echo *** Compiling $*.cpp using WATCOM C/C++ options *** wpp386 -d1 -zq -W2 -D__WATCOM__ $(WCL386OPTS) $*.cpp and add .cpp to the .EXTENSIONS directive after .c. In each .DEP file, add .SYMBOLIC to the end of the ALL: line. This eliminates some error messages that otherwise occur.

Writing OS/2 2.1 Device Drivers in C, 2nd Edition
This book is a must for device driver programmers! It has extensive reference material all the way through, and brief examples of how to use many of the functions. Skeleton code is presented throughout, to guide the novice and intermediate OS/2 device driver programmer. Strategies are explained and much of the inside workings of OS/2 and hardware are explained. Here are the chapter headings:
 * The Evolution of PC Device Drivers
 * Understanding Device Drivers
 * The PC Hardware Architecture
 * An Overview of the OS/2 Operating System
 * The Anatomy of an OS/2 Device Driver
 * Device Driver Strategy Commands
 * A Simple OS/2 Physical Device Driver
 * The Micro Channel Bus
 * OS/2 2.1 Virtual Device Drivers
 * Memory Mapped Adapters and IOPL
 * Direct Memory Access (DMA)
 * Extended Device Driver Interface
 * Debugging OS/2 2.1 Device Drivers
 * An Introduction to Presentation Drivers
 * Working with Pointers
 * PCMCIA Device Drivers
 * Tips and Techniques
 * Device Helper Reference
 * Reference Publications
 * Listings
 * OEMHLP and TESTCFG

The first four chapters serve to introduce the area of device driver programming by way of explaining where we came from, and where OS/2 is headed. First, we are treated to a brief history of the micro-computer, starting with the Altair 8800, and covering the very early ways of programming directly for the hardware. This stuff is even worse than DOS, believe it or not. The very early programs each had their own built-in operating system!! The origins of micro-computer operating systems are outlined along with explanations of BIOS and some architecture basics, like the bus.

In chapter three, the PC hardware architecture is explained, starting with the 8088, and going through the 80286, the AT bus, the PS/2, the Micro Channel bus, EISA, real mode, protect mode, addressing and the ring architecture of the 80386 and 80486. In chapter four we are then walked through the early versions of OS/2, disadvantages and all, to the OS we all know and love, OS/2 2.x.

The book really starts in chapter five. The early chapters were very brief, and could be read comfortably in one sitting. This chapter gets more complicated, and has some spine chilling warnings like "It may be difficult or impossible to find a device driver problem using normal debugging techniques." Luckily, we have the OS/2 kernel debugger, KDB. Installation of the kernel debugger is explained, and then the basic OS/2 device driver design is explained step by step. The various components of the device driver are outlined, and code for a start-up routine given. Unfortunately, the start-up routine is in assembly code, and Watcom C/C++, my compiler, does not come with an assembler, so I could not follow this on my computer.

Device drivers have a strategy section, which is basically a switch for the message type the OS calls the driver with. Depending on what the OS needs, a separate section of the device driver is entered, and the need fulfilled. Device drivers can either fulfil the request immediately, or it can acknowledge the receipt of the request, and fulfil it as time permits. There are quite a few different strategy commands that the OS can use, depending on what type of driver you are writing.

Chapter seven explains a sample device driver for an 8-bit parallel port. You can enhance this code to suit your needs, if you have to program such a port. This is the only fairly complete device driver code that the book contains, but of course most people attempting to program a device driver have the specifications for the particular device they are interested in, and so the skeleton code suffices.

The remainder of the chapters discuss various other types of device drivers, such as memory mapped drivers, presentation device drivers, and so on. These chapters are fairly introductory in nature, and no-one is going to get a free ride here. This book has to be supplemented with device specific reference material! On the other hand, I shudder to think how anyone would program a device driver for OS/2 without this book, or something similar. There are many efficiency considerations to take into account, and trying to program for OS/2 with only DOS device driver knowledge would be like trying to climb Mont Blanc after walking up the local Devil's Hill. You are bound to do things wrong without some guidelines.

Programming device drivers for OS/2 is not for the faint of heart, but neither is it impossible. It requires the right tools, the right attitude, the right information, the patience, and this book (or another similar book, but I have never heard of any other book like this). Like any difficult programming problem, successfully writing a device driver for OS/2 is tricky, but potentially very rewarding. Just take a look at the popularity of the SIO drivers, which are considered to be better than OS/2's own by most people. Good luck!

Summary
This is a very authoritative book, almost more of a reference book than a tutorial. It has a lot of tables of flag bits, device helper functions, PCMCIA card services, KDB keywords, ad infinitum. Although it is a large book (at 547 pages), it still has a very terse feel to it, and is definitely aimed at advanced programmers. Anyone considering writing device drivers should definitely be comfortable with C, and familiar with computer architecture and the specific hardware they want to program for. This is a "bible" type of book, and as such is probably required reading for OS/2 device driver programmers in much the same way that the ARM is required for C++ programmers. The only downside is what is not there. IFS drivers are not covered, and neither are SCSI or ADD drivers. This is mentioned in the introduction, however, and is promised for the next printing. I did feel a little surprised by the fact that the DevHlp library used in the book has to be purchased separately, but I suppose it would be a little much to ask for it to be included with such a reasonably priced book.


 * Writing OS/2 2.1 Device Drivers in C, 2nd Edition, Steven J. Mastrianni
 * Van Nostrand Reinhold, ISBN 0-442-01729-4, US $35, CAN$45.
 * Intended audience: Advanced C Programmers
 * Mark: A-