|
|
|
Introduction to Windows driversThe function of a device driver is doing the communication with a device, instructing it what to do. The applications running in the higher layers do not know how to talk to devices directly and get the work they want done. Lets take an example. Assume a program wants to read a file from a CD in a CD drive. The program do not even need to know whether the file is in a CD or in hard drive. It just call the Win32 API function ReadFile() with correct path to the file. Finally, someone has to instruct the CD drive where to move the optical head, read data, etc to read the file and fill the buffer the application provided with the data read from the file. The device driver of the CD drive does that. Device drivers are usually written by the manufactures of the hardware device. They are the people who know what control codes should be written to the control buffer of the device to instruct the device to get the desired work done by the device, because they manufactured the device. But, for some hardware such as hard drives and cd drives, there are standard drivers, and the device manufactures should manufactures should manufacture the device to support that standard driver. That's why you do not need to install drivers for hard drives you install. The standard device driver for hard drives comes with the operating system. The device drivers also display the layered architecture. For an example, There can be a layered stack of drivers to handle a hard drive. In the top, there is the file system driver, whose work is to manage the file system, It keeps track of free space, handle the directory tree, determine where to put the files, etc. It do not need to know how to move disk arms, read, write, etc. It just instruct lower layer drivers to do those low level tasks. And, the driver at the lowest layer do not know anything about files, directories. It just moves the arm, reads, writes as the upper layer instructs it. Usually, the device drivers should run in the kernel because it has to use privileged instructions such as writing and reading the device buffers. So, when you write a driver for windows, it runs in the kernel space. It cannot use the Win32 API because it is for those applications running in user space. There are separate set of driver support routines provided by the kernel, memory manager, object manager, etc. You can read the documentation of these support routines from MSDN library. Drivers are usually written in C. Drivers are basically procedural and do not support object oriented programming. So C++ is not used. Because you are running in the kernel, you have an extreme power. In Windows, it is not a must for a driver to handle a device. It can be just a program running with higher privileges. But, you have to do everything very carefully. If your program running at user space does anything wrong, it will crash without affecting the operating system. But if your driver did anything wrong, The whole system will crash giving the Blue Screen Of Death. |