Article Title: Overview of the driver framework in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I. linux Device Drivers and external interfaces can be divided into three parts:
1. interfaces between the driver and the operating system kernel. This is done through the file_operations (include/linux/fs. h) data structure.
2. Drivers and system-guided interfaces. This part uses the driver to initialize the device.
3. interfaces between drivers and devices. This section describes how drivers interact with devices, which are closely related to specific devices.
2. the device driver code consists of the following parts:
1. Register and deregister the driver.
2. Enable and release the device.
3. read/write operations on the device.
4. Device control operations.
5. device interruption and polling.
3. Driver registration and cancellation:
The device driver can be initialized when the system is started or dynamically loaded as needed. The initialization of the character device is completed by chr_dev_init (), including the memory (devfs_register_chrdev (MEM_MAJOR, "mem", & memory_fops), terminal (tty_init ()), the initialization of character devices such as printer (lp_init () and mouse (misc_init.
The block device Initialization is completed by blk_dev_init (), which includes initialization of the IDE Hard Drive (ide_init (), floppy_init (), and optical drive.
Each character device or block device is initialized to the kernel through devfs_register_chrdev () or devfs_register_blkdev. When you disable a character device or block device, you also need to log off the device from the kernel through devfs_unregister_chrdev () or devfs_unregister_blkdev.
4. Enable and release a device:
Open () is used to open a device. For example, the printer is opened with lp_open (), while the hard disk is opened with hd_open. In most device drivers, open completes the following tasks:
1. Increase the device count.
2. Check related device errors, such as the device is not ready or similar hardware problems.
3. If the check is enabled for the first time, the device is initialized.
4. Identify the secondary device number. Update the f_op pointer if necessary.
5. If necessary, allocate and set the data structure to be placed in filp-> private_data.
Releasing a device is done by release (). For example, lp_release () is used to release the printer, and tty_release () is used to release the terminal device (). The general steps for releasing a device include:
1. Release the memory allocated by open in filp-> private_data.
2. If it is the last release, disable the device.
3. decrease the value of count.
5. read/write operations on devices:
Character devices use their own read () and write () to read and write data. For example, the virtual terminal reads and writes data through vcs_read () and vcs_write.
Block devices use generic generic_file_read () and generic_file_write () to read and write data. These two general functions add read/write requests to the request table. The kernel can use ll_rw_block () to optimize the request sequence. Because the operation is performed on the memory buffer rather than the device, read and write requests can be accelerated. If there is no data to be read in the memory buffer or you want to write requests to the device, you must actually perform data transmission. This is done through the data structure request_queue and request_fn () (include/linux/blkdev. h ).
Vi. Device Control Operations:
In addition to read/write operations, you also need to control the device. This can be done through ioctl () in the device driver. For example, the IDE hard drive can be controlled through hd_ioctl (), and the optical drive can be controlled through cdrom_ioctl ().
Unlike read/write operations, ioctl () usage is closely related to specific devices. Taking floppy_ioctl () of the soft drive as an example (drivers/block/floppy. c ):
Static int fd_ioctl (struct inode * inode,
Struct file * filp,
Unsigned int cmd,
Unsigned long param );
The value and meaning of cmd are related to the soft drive. For example, FDEJECT indicates that a floppy disk is displayed.
In addition to ioctl (), the device driver may have other control functions, such as llseek.
VII. device polling and interrupt handling:
For devices that do not support interruption, You need to poll the device status during read/write and determine whether to continue data transmission. For example, a printer. If the device supports interruption, the device can be interrupted.