Making hardware work is tedious. To write to a hard disk, for example, requires that you write magic numbers in magic places, wait for the hard drive to say that it is ready to receive data, and then feed it the data it wants, very carefully. To write to a floppy disk is even harder, and requires that the program supervise the floppy disk drive almost constantly while it is running.
Instead of putting code in each application you write to control each device, you share the code between applications. To make sure that that code is not compromised, you protect it from users and normal programs that use it. If you do it right, you will be able to add and remove devices from your system without changing your applications at all. Furthermore, you need to be able to load your program into memory and run it, which the operating system also does. So, an operating system is essentially a preiviledged, general, sharable library or low-level hardware and memory and process control functions and routines.
All versions of have an abstract way of reading and writing devices. By making the devices act as much as possible like regular files, the same calls (read(), write(), etc.) can be used for devices and files. Within the kernel, there are a set of functions, registered with the filesystem, which are called to handle requests to do I/O on ``device special files,'' which are those which represent devices.
All devices controlled by the same device driver are given the same major number, and of those with the same major number, different devices are distinguished by different minor numbers.
This chapter explains how to write any type of device driver that you might need to, including character, block, SCSI, and network drivers. [Well, it will when it is done...] It explains what functions you need to write, how to initialize your drivers and obtain memory for them efficiently, and what function are built in to \ to make your job easier.
Creating device drivers for is easier than you might think. It merely involves writing a few functions and registering them with the Virtual Filesystem Switch (VFS), so that when the proper device special files are accessed, the VFS can call your functions.
However, a word of warning is due here: Writing a device driver is writing a part of the kernel. This means that your driver runs with kernel permissions, and can do anything it wants to: write to any memory, reformat your hard drive, damage your monitor or video card, or even break your dishes, if your dishwasher is controlled by your computer. Be careful.
Also, your driver will run in kernel mode, and the kernel, like most kernels, is non-pre-emptible. This means that if you driver takes a long time to work without giving other programs a chance to work, your computer will appear to ``freeze'' when your driver is running. Normal user-mode pre-emptive scheduling does not apply to your driver.
If you choose to write a device driver, you must take everything written here as a guide, and no more. I cannot guarantee that this chapter will be free of errors, and I cannot guarantee that you will not damage your computer, even if you follow these instructions exactly. It is highly unlikely that you will damage it, but I cannot guarantee against it. There is only one ``infallible'' direction I can give you: Back up! Back up before you test your new device driver, or you may regret it later.