Linux Device Driver study: scull Parsing

Source: Internet
Author: User

After nearly two months, I finally moved to the nearest point. I have a detailed understanding of the scull driver in chapter 3 of Linux Device Drivers (3rd edition), and I will analyze the scull driver, for later viewing.

1. Create a character driver:

1. Get the device Number:

Int register_chrdev_region (dev_t first, unsigned int count, char * name );

Int alloc_chrdev_region (dev_t * dev, unsigned int firstminor, unsigned int count, char * name );

The above two functions, the former is to register the device number when determining the master device number (first, the latter automatically obtains and registers the master device number (the obtained device number is stored in dev ).

2. Associate the device operation function with the device Number:

Initialization: void cdev_init (struct cdev * dev, struct file_operations * fops );

Add to kernel: int cdev_add (struct cdev * dev, dev_t num, unsigned int count)

Note: The fops used in step 2 has been initialized. design the open, read, write, release and other functions of the device driver, and define the global variable struct cdev my_cdev, or use kalloc to allocate space for the driver code.


Ii. Code routine:

You can refer to the routines provided in this book and the implementation of scull_init_module in main. c in the appendix.


Iii. scull driver code analysis and scull application calls

1. Global variable in scull driver code: struct scull_dev * scull_devices; // allocate space for it in scull_init_module;

Scull_major, scull_minor: Device number

2. After the scull driver is compiled and installed in the kernel, you can use the application for read verification. For example: file1 = fopen ("/dev/scull0", "rb +"); stringlen = fwrite (stringBuffer, 1, sizeof (stringBuffer), file1)

3. fopen implementation:

A) After the application calls the fopen library function, it will execute the underlying application function interface int open (const char * path, int oflags ), then run the kernel code long do_sys_open (int dfd, const char _ user * filename, int flags, int mode) (kernel code open. c file ). Do_sys_open implement get struct through filename
File * f, bind f to int fd, and return fd. The fd is the return value of the open function.

B) int scull_open (struct inode * inode, struct file * filp) in the scull driver)

Struct scull_dev * dev;

Dev = container_of (inode-> I _cdev, struct scull_dev, cdev );
Filp-> private_data = dev;/* for other methods */

In the scull driver, write and read data through filp-> private_data (pointing to dev.

When reading this code, I have many questions. For example, inode-> I _cdev should point to the scull_devices.dev section defined in the scull driver, then, when is the relationship between the address of jiangscull_devices.dev assigned to inode-> I _cdev, and the relationship between scull_open and do_sys_open in kernel code. Your understanding is as follows:

(1) scull_init_module calls the scull_setup_cdev function. scull_setup_cdev implements the cdev_init and cdev_add functions. The cdev_add implementation tells the kernel that, the corresponding device Code corresponds to a specific device file and operation function (scull_devices and f_ops), that is, register in the kernel, and then find the corresponding scull_devices through the device number. When a node is created (mknod), the device number (for example, mknod/dev/scull0-C) is used.
$ MajorNumber 0). At this time, the created inode, its I _cdev has pointed to the scull_devices corresponding to the specific device number.

(2) The kernel source code do_sys_open mainly obtains the file pointer f through the file path and binds it to the corresponding handle fd.

Do_sys_open calls struct file * f = do_filp_open (DFD, TMP, flags, mode, 0); the substantial open work in the do_filp_open function is the do_last function.

In the do_last function, no matter whether the flag in open_flag determines whether the file needs to be created, the nameidata_to_filp function will be called, and nameidata_to_filp will call _ dentry_open, the _ dentry_open function calls F-> f_op-> open, that is, scull_open.

Whether a file node needs to be created varies with the time when f_op is called. When a node needs to be created, nameidata_to_filp is called after _ open_namei_create; When a node is not required, nameidata_to_filp is called in finish_open.

(3) In the scull driver process, use file-> private_data to transmit the scull_devices address (mainly implemented in the scull_open code), and then read and write the data of scull_devices.


4. scull_devices ):

When performing scull_write and scull_read, call the scull_follow function to allocate data space (kmalloc)

Struct scull_dev {
Struct scull_qset * data;/* pointer to First Quantum Set */
Int quantum;/* The current quantum size */
Int qset;/* The current array size */
Unsigned long size;/* amount of data stored here */
Unsigned int access_key;/* used by sculluid and scullpriv */
Struct semaphore sem;/* mutual exclusion semaphore */
Struct cdev;
/* Char device structure */
};

Struct scull_qset {
Void ** data;
Struct scull_qset * next;
};

Struct cdev {
Struct kobject kobj;
Struct module * owner;
Const struct file_operations * ops;
Struct list_head list;
Dev_t dev;
Unsigned int count;
};




References:

1. LInux Device Drivers chapter 3

2. Linux kernel source code

3. Analysis of kernel code open: http://www.cublog.cn/u3/119372/showart_2518539.html

4. About file-> private_data http://linux.chinaunix.net/techdoc/develop/2007/09/09/967423.shtml


Related Article

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.