"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-driven programming has also been a while, and several drivers have been written. So there are some new understanding and understanding of the device driver, summarize.

★ 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, some of which are not always clear, but feel very much more clear than at the beginning of the stage.

The device driver is plainly (in essence) a set of interfaces that operate a hardware device for an application.

The driver is able to directly manipulate the resources on the hardware, such as the various registers of the GPIO, thus controlling 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 a lot of drive devices, so the programs that drive them are different, so how does the driver provide a unified interface? The key is the structure, 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 structure?

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 register function is to 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. This allows the driver to 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. Driver execution in the kernel, an application access driver is a system function that uses system functions to interview device files.

So be sure to have a device file in your filesystem that corresponds to your device driver, so your app has access to your device drivers.

Device files are typically placed below the/dev folder. You can view it through the LS command. How do I create a device file node?

The ability to use the Mknod command, such as I want to create a master device number of 103. A character device file with a secondary device number of 0. You can enter: Mknod/dev/xxx C 103 0 The XXX in/dev/xxx indicates the name of the device file






"Linux Driver" device drivers understand again

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.