#define _IO (TYPE,NR) _ioc (_ioc_none, (Type), (NR), 0)
#define _ior (type,nr,size) _ioc (_ioc_read, (Type), (NR), (_ioc_typecheck (size)))
#define _IOW (type,nr,size) _ioc (_ioc_write, (Type), (NR), (_ioc_typecheck (size)))
#define _IOWR (type,nr,size) _ioc (_ioc_read|_ioc_write, (Type), (NR), (_ioc_typecheck (size)))
IOCTL () Variables passed on the functionCMD is the value that the application uses to differentiate the contents of the device driver request processing.In addition to distinguishing numbers, CMD contains several appropriate information that can help with processing.The size of CMD is 32 bits and is divided into 4 domains:
The Bit31~bit30 2-bit is a "read-and- write " area that distinguishes between a read command or a write command.
bit29~bit15 14 bits is the data size area, which represents the amount of memory transferred by the ARG variable in the IOCTL ().
The bit20~bit08 8-bit is the "magic Number" (also known as the "magic number ") area, which is used to differentiate from the IOCTL commands of other device drivers .
bit07~bit00 8-bit is the "difference ordinal " area, which is a command sequence ordinal that distinguishes commands.
Parameter 1: Magic number (magic number)
Magic Range is 0~255 . Usually, the English characters "a" ~ "Z" or "a" ~ "Z" to represent. The device driver obtains the magic number from the command passed in, and then compares it to the number of demons it handles, and if the same is handled, the difference is not processed. The magic number is the primary auxiliary state that denies misuse. Device drivers programs can get magic numbers by _ioc_type (cmd) . Different device drivers are best to set different magic numbers, but not absolute, but also the number of magic numbers that can be used by other device drivers.
Parameter 2: number of Base (serial number )
The cardinality is used to differentiate between various commands. Typically, the value is incremented from 0, which can be reused on the same device driver. For example, the same cardinality is used in the read and write commands, and device drivers can be distinguished because the device driver uses switch when differentiating commands and uses the command variable cmd value directly. The macro generated by creating a command is composed of multiple domains, so even the same cardinality is judged as a different command. To get the cardinality from the command, the device driver uses the following macro:
_IOC_NR (CMD)
Typically, the casevalue in switch uses the command itself.
Parameter 3: Variable type
The variable type uses the arg variable to specify the size of the data being transferred, but instead of substituting the input directly, it is substituting the variable or the type of the variable, because the sizeof () Compile command is already included in the use of the macro creation command.
When using _ior (), _iow (), _IOWR (), the ARG variable value of the IOCTL () specifies the cache (struct) address at which data is written or read on the device driver . That is, the third parameter is determined by the third parameter of the IOCTL () function and does not require the user to fill it out, just specify the type.
The use of _IO,_IOR,_IOW,_IOWR macros in the Linux kernel