17. Character Device Control technology

Source: Internet
Author: User

Character Device Control Technology

In front of the study, we learned the driver, to realize the program read and write function. Now the device has a more important function-control.

Functions for controlling devices: IOCTL.

CMD: The parameter is a command we send, such as a reboot.

The third parameter: is dependent on the second, for example we want to modify the baud rate value, we will be in the third parameter to pass the baud rate value. When the second does not require a parameter, it is a null value.

As we know before, when we call the Read function, the system automatically calls the ***.read function. Above is the version of the function called by the system when we call the IOCTL system. Parameter one by one corresponds to delivery.

Summary: We know from the above read and write content that we call the Read function in the program, the system will call the ***_read function, and then find the corresponding implementation function in the kernel. Write is the same: Write->***_write, his implementation of the kernel. So now control procedures: IOCTL, corresponding to the system of UNLOCK_IOCTL, followed by how the system is realized.

To define a command:

The system uses macros to define commands:

We know that the type is 8 bits, and then a letter is exactly 8 bits, a character such as M.

0 is the ordinal of the command, and the last int is the type of the command.

As you know, we know how to do this. The following see the implementation of the program, function: the first implementation of the device Restart command, the second is set parameters.

The first is to define the command macro: memdev.h:

Then we add our control function to the MEM_FOPS structure:

The next thing I do is implement the control function:

The UNLOCKED_IOCTL function prototype in the kernel:

From the above we get:

Long mem_iotcl (struct file *filp, unsigned int cmd, unsigned long arg). We know that there is a switch in the selection structure.

Remember that it was implemented in front of the struct structure

Results of the operation:

Next is to write an application to test, MEM_CTL.C:

Compilation Result:

Write the driver Memdev.ko and application mem_ctl and copy it to our Development Board:

The first is to install the Memdev.ko driver and view the main device number:

Next create the character device file: Memdev0:

Next, run our application:

The./mem_ctl executes this command and then outputs:

ARG is 115200

Restart devices

17. Character Device Control technology

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.