1.1 Linux device driver classification
Linux device drivers occupy a large proportion of the Linux kernel source code, and the length of the source code is increasing, mainly the increase of the driver. During the continuous upgrade of Linux kernel, the structure of driver is still relatively stable. In the 2.0.xx to 2.2.xx changes, the driver's writing has made some changes, but from 2.0.xx Drive to 2.2.xx porting only a small amount of work. The devices in the Linux system are divided into character devices (char device), block device, and network devices (net work device) three. A character device is a device that is not cached when it is accessed. Block devices are read and written with caching to support, and block devices must be able to be accessed randomly (random access), which is not required for character devices. Typical character devices include a mouse, a keyboard, a serial port, and so on. Block equipment mainly includes hard disk floppy disk equipment, CD-ROM and so on. A filesystem must be mounted on a block device to be installed into the operating system. Network devices do special processing in Linux. Linux's network system is mainly based on the BSD UNIX socket mechanism. There is a specific data structure (SK_BUFF) defined between the system and the driver to carry out the transfer of information. The system supports the caching of sending data and receiving data, provides the flow control mechanism, and provides support for multi-protocol.
1.2 Some basic concepts for writing drivers
Regardless of the operating system driver, there are some common concepts. The support of the operating system to provide the driver is also roughly the same. The following is a brief introduction to some basic requirements for network device drivers.
1.2.1 Send and receive
This is the most basic function of a network device. What a network card does is to send and receive work. So the driver tells the system where to send the function, and the system calls your sending program when there is data to send. There is also a driver because it is directly manipulating the hardware, so the network hardware has data to receive the first data is the driver, it is responsible for these raw data processing necessary and then sent to the system. Here, the operating system must provide two mechanisms, one is to find the driver's sending function, one is the driver to send the data received to the system.
1.2.2 Interrupted
Interruption has an important place in the structure of modern computers. The operating system must provide the ability of the driver to respond to interrupts. Typically, an interrupt handler is registered in the system. The operating system invokes a handler for the driver after a hardware outage occurs. Linux supports interrupted sharing, where multiple devices share a single interrupt.
1.2.3 Clock
When implementing a driver, a clock is used in many places. such as timeout processing in some protocols, no interrupt mechanism for hardware polling, and so on. The operating system should provide a timing mechanism for the driver. It is generally after the scheduled time to callback the registered clock function. In a network driver, a timer can provide a polling (poll) way to access hardware if the hardware is not interrupted. Or it is necessary to implement some protocol timeout retransmission, and so on.