"Linux Driver" device drivers understand again

Source: Internet
Author: User

Learning device driver Programming also has a period of time, also wrote a few drivers, so there are some new understanding and understanding of the device driver, summed up. Learning device driver Programming also has a period of time, also wrote a few drivers, so there are some new understanding and understanding of the device driver, summed up.

★ What is a driver

When I first started learning about device drivers, there were a lot of problems. What is a driver? What are the drivers for? How does it work? How does it relate to the operating system? A series of questions, now some places are not necessarily clear, but compared to the beginning of the stage, feel that they are still a lot clearer.

The device driver is plainly (in essence) a set of interfaces that operate a hardware device for an application. Drivers are the ability to directly manipulate resources on the hardware, such as the various registers of the GPIO, to control the direction of the GPIO (output or input), the level of the pin, the interrupt, and so on. Most of what the driver does is similar to a program that is written directly on a 51 single-chip or AVR microcontroller, unlike the driver that provides a unified interface to the operating system.

★file_operations Structural Body

There are many kinds of drive devices, so the programs that drive them are different, so how does the driver provide a unified interface? The key is the struct, which is defined in the Include/linux/fs.h file.

struct File_operations {struct module *owner;loff_t (*llseek) (struct file *, loff_t, int), ssize_t (*read) (struct file *, Char __user *, size_t, loff_t *), ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_re  AD) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t); ssize_t (*aio_write) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct P Oll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); Long (*UNLOCKED_IOCTL) (Stru CT file *, unsigned int, unsigned long), Long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (str UCT file *, struct vm_area_struct *), int (*open) (struct inode *, struct file *), int (*flush) (struct file *, fl_owner_t I d); Int (*release) (struct inode *, struct file *), int (*fsync) (struct file *, struct dentry *, int datasync); Int (*aio_fs Ync) (struct KIOCB *, int datasync); int (*fasync) (int, struct file *, int), int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendfil  e) (struct file *, loff_t *, size_t, read_actor_t, void *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned lo NG); int (*check_flags) (int); int (*dir_notify) (struct file *filp, unsigned long arg); int (*flock) (struct file *, int, stru CT File_lock *); ssize_t (*splice_write) (struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t ( *splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);};
This structure links the functions that manipulate the hardware resources in the driver. So when the operating system knows the structure, it knows the driver. So how does the operating system know about this struct?

In fact, Linux under the device can be divided into two kinds: character devices, block devices. The kernel uses 2 global variables to store these two types of drivers:


Character device-Important structure:


static struct Char_device_struct {struct char_device_struct *next;unsigned int major;unsigned int baseminor;int minorct; Char name[64];struct file_operations *fops;struct cdev *cdev;/* would die */} *chrdevs[chrdev_major_hash_size];
when the driver calls Register_chrdev (unsigned int major, const char * name, struct file_operations *fops) The registration function is the File_ The operations structure is stored in the array chrdevs[chrdev_major_hash_size] array, and the array subscript is the drive main device number so that the driver can be associated with the kernel.

★ Device File

What is the use of device nodes? We know that under Liunx, devices are in the form of files. The driver runs in the kernel, and the application Access driver uses system functions to access the device files. Therefore, you must have a device file in your file system that corresponds to your device driver so that your application can access your device drivers.

The device files are generally placed under the/dev directory and can be viewed through the LS command. How do I create a device file node?

You can use the Mknod command, for example, if I want to create a character device file with a main device number of 103 and a second device number of 0, you can enter: Mknod/dev/xxx C 103 0 where XXX in/dev/xxx represents the name of the device file






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.