Article Title: Linux operating system kernel and device file dialog. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Device files are used to represent physical devices. Most physical devices are used for output or input. Therefore, the device driver in the kernel must get output from the process to the device. This can be done by opening the output device file and writing it, and you want to write a common file. In the following example, this is implemented by device_write.
This is not always effective. Imagine that you are connected to a serial port of the modem (the technology is that you have an inner cat, from the CPU point of view it is also implemented as a serial port, so you do not need to think this idea is too difficult ). The most natural thing to do is to use the device file to write the content to the modem (whether using the modem command or telephone line) or read the information from the modem (you can also use the modem command to answer or use the telephone line ). But the question behind this is what needs to be done when you need to talk to the serial port itself? For example, the rate at which data is sent and received.
The answer is that Unix uses a special function called ioctl (short for input output control. Each device has its own ioctl command, which can be read, written, or both. The Ioctl function is called by three parameters: The description sub-, the number of ioctl, and a long integer parameter of the appropriate device. You can assign a role to pass everything.
The Ioctl number is used to encode the device master code, ioctl type, encoding, and parameter type. The Ioctl count is usually called by a macro in the header file (_ IO, _ IOR, _ IOW or _ IOWR -- depends on the type ). This header file must be included in the program and kernel module (so they can generate the correct ioctl's) using ioctl. In the following example, the header file is chardev. h and its program is ioctl. c.
If you want to use ioctl's in your own kernel module, you 'd better accept a formal ioctl position so that you can get others' ioctl's or they will get you, then you can know what went wrong. If you want more information, go to 'documentation/ioctl-number.txt 'to view the kernel source file tree.
Ex chardev. c
/* Chardev. c
*
* Create an input/output character device
*/
/* Copyright (C) 1998-99 by Ori Pomerantz */
/* The necessary header files */
/* Standard in kernel modules */
# Include/* Were doing kernel work */
# Include/* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
# If CONFIG_MODVERSIONS = 1
# Define MODVERSIONS
# Include
# Endif
/* For character devices */
/* The character device definitions are here */
# Include
/* A wrapper which does next to nothing
* At present, but may help for compatibility
* With future versions of Linux */
# Include
/* Our own ioctl numbers */
# Include "chardev. h"
/* In 2.2.3/usr/include/linux/version. h between des
* Macro for this, but 2.0.35 doesnt-so I add it
* Here if necessary .*/
# Ifndef KERNEL_VERSION
# Define KERNEL_VERSION (a, B, c) (a) x 65536 + (B) * 256 + (c ))
# Endif
# If LINUX_VERSION_CODE> = KERNEL_VERSION (2, 2, 0)
# Include/* for get_user and put_user */
# Endif
# Define SUCCESS 0
[1] [2] [3] [4] [5] [6] Next page