[Linux driver] device drivers

Source: Internet
Author: User

I have been learning device-driven programming for some time and have written several drivers. So I have a new understanding and understanding of the device driver. I have been learning device-driven programming for some time and have written several drivers. So I have a new understanding and understanding of the device driver.

★What is a driver?

When I first started learning the device driver, there were a lot of problems. What is a driver? What is the driver? How does it work? How does it relate to the operating system? A series of problems are still unclear in some places, but I feel a lot clearer than at the beginning.

 

Device Drivers are actually interfaces that provide applications with a set of hardware device operations. The driver can directly operate hardware resources, such as the various registers of GPIO, to control the GPIO direction (output or input), the level of the pin, interrupt, and so on. Most of what the driver does is similar to the functions of programs written directly on 51 or avr scm. What is different from them is that the driver provides a unified interface for the operating system.

★File_operations struct

There are many types of drivers, and their programs will be different. How can drivers provide unified interfaces? The most important thing is this 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_read) (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 poll_table_struct *);int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);long (*compat_ioctl) (struct file *, unsigned int, unsigned long);int (*mmap) (struct file *, struct vm_area_struct *);int (*open) (struct inode *, struct file *);int (*flush) (struct file *, fl_owner_t id);int (*release) (struct inode *, struct file *);int (*fsync) (struct file *, struct dentry *, int datasync);int (*aio_fsync) (struct kiocb *, int datasync);int (*fasync) (int, struct file *, int);int (*lock) (struct file *, int, struct file_lock *);ssize_t (*sendfile) (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 long);int (*check_flags)(int);int (*dir_notify)(struct file *filp, unsigned long arg);int (*flock) (struct file *, int, struct 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);};
Use this struct to link the functions that operate hardware resources in the driver. Then, after the operating system knows the struct, it knows the driver. How does the operating system know this struct?

In fact, Linux devices can be divided into two types: character devices and Block devices. Two global variables are used in the kernel to store these two types of drivers:


Important struct of character devices:


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;/* will 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 stores the file_operations struct in the array chrdevs [CHRDEV_MAJOR_HASH_SIZE] array, the array subscript is the driver master device number, so that the driver can be associated with the kernel.

★Device Files

What is the purpose of a device node? We know that all devices exist in the form of files in Liunx. The driver runs in the kernel, and the application accesses the driver by using system functions to access the device file. Therefore, the file system must have a device file that corresponds to your device driver so that the application can access your device driver.

The device files are generally stored in the/dev directory. You can run the ls command to view the files. How to create a file node for a device?

You can run the mknod command. For example, if you want to create a device file with a master device Number of 103 and a secondary device Number of 0, you can enter: mknod/dev/XXX c 103 0 where XXX in/dev/XXX indicates 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.