Linux IOCTL Functions

Source: Internet
Author: User

The ioctl function I mentioned here is in the driver, because I don't know whether IOCTL is used in other scenarios,
Therefore, the scope of our discussion is defined. Why do I need to write an article? It's because I was mixed up by IOCTL some time ago.
So I figured it out just a few days ago, so I want to clear my mind here.

1. What is IOCTL.
IOCTL is a function used by the device driver to manage the device's I/O channels. To manage the I/O channel
Is to control some features of the device, such as the Serial Transmission baud rate, motor speed and so on. Number of calls
As follows:
Int IOCTL (int fd, IND cmd ,...);
FD indicates the File Identifier returned by the open function when the user program opens the device. CMD indicates
For the standby Control Command, as for the ellipsis behind it, it is some supplementary parameter. Generally, there is at most one, and either or not it is
Cmd.
The ioctl function is an attribute component in the file structure, that is, if your driver provides support for IOCTL
You can use the ioctl function in your program to control the I/O channel of the device.

Ii. Necessity of IOCTL
If you do not need ioctl, you can also control the I/O channel of the device, but that's pretty much the case. For example, we can
When implementing write in the driver, check whether there is any data flow with special conventions. If yes,
Then the control command is followed (usually used in socket programming ). However
As a result, the Division of code is unknown, the program structure is chaotic, and the programmer will also be dizzy.
Therefore, we use IOCTL to implement the control function. Remember, what a user program does is through the command code
To tell the driver what it wants to do, as to how to explain these commands and how to implement these commands, this is what the driver wants
.

Iii. How to Implement IOCTL
This is a very troublesome problem. I can save it. To be clear about it, it cannot be 4000 or 5000 words, so I am
It is impossible to make it very clear, but if a reader is interested in how the user program is connected with the driver
If you are interested, you can refer to the "write mystery" I wrote a while ago. You only need to replace write with IOCTL.
How does the ioctl of the user program relate to the ioctl implementation in the driver program.
Here is a rough idea, because I think the book "Linux Device Driver" has already been very clear.
But it takes some time.
In the ioctl function implemented in the driver, there is actually a switch {Case} structure, each case pair
Use a command code to perform operations. How to implement these operations is a task of every programmer.
Because the devices are specific, it cannot be said here. The key is how to organize the command code, because in ioctl
The command code is the only way to contact user program commands and driver support.
There are some exquisite command code organizations, because we must make sure that commands and devices correspond one by one.
The correct command is sent to the wrong device, the wrong command is sent to the correct device, or the wrong
Command to the wrong device. These errors will lead to unexpected events, and when the programmer discovers these
It is very difficult to debug the program to find errors.
Therefore, the Linux core defines a command code as follows:
____________________________________
| Device type | serial number | direction | data size |
| ---------- | -------- | ------ | -------- |
| 8 bit | 8 bit | 2 bit | 8 ~ 14 bit |
| ---------- | -------- | ------ | -------- |

In this way, a command is converted into an order code in the integer form. However, the command code is not intuitive, so
Some macros are provided in Linux kernel, which can generate a command code based on a string that is easy to understand, or
The command code is a string that you can understand to indicate the device type, device serial number, and number of devices corresponding to this command.
Data Transmission Direction and size.

I will not explain these macros here. For the specific form, please refer to the sum in the Linux core source code.
In addition to the complete definition of these macros. Here I will only talk about one thing, that is, "magic number ".
The magic number is a letter and the data length is also 8. Therefore, a specific letter is used to indicate the device type.
Numbers are the same, but they are more conducive to memory and understanding. That's it. It's no more complicated.
No more. Let's take a look at the source code. We recommend that you read the Linux driver.
Here is an example of short in the source code. It is short and has simple functions. You can understand the functions and details of IOCTL.
Section.

Iv. How to obtain the CMD Parameter
Here, we really want to talk about the CMD parameter in the user program end by some macros according to the device type, serial number, transmission direction,
The data size is generated. This integer is passed to the driver in the kernel through the system call, and then used by the driver.
The Code macro obtains the device type, serial number, transfer direction, data size, and other information from this integer.
Switch {Case} structure for corresponding operations.
To thoroughly understand the source code, I can only read the source code. This article is actually just an introduction. CMD parameter Organization
It is still complicated. I think it takes a lot of time to get familiar with it, but it is worth it.
What is difficult is the understanding of interruptions.

V. Summary
IOCTL has nothing to understand. The key is to understand how the CMD command code is generated in the user program.
In the driver, the most important workload of programmers is in the switch {Case} structure, because
I/O control is implemented through this part of code.

 

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.