I see into header file
#define IOCTL_MAGIC 'A'
#define IOCTL_NAME _IOWR(IOCTL_MAGIC, 2, ioctl_param)
How can I know that is the ioctl number of IOCTL_NAME
?
The way of constructing of ioctl number is described at the top of the header include/uapi/asm-generic/ioctl.h:
ioctl command encoding: 32 bits total, command in lower 16 bits, size of the parameter structure in the lower 14 bits of the upper 16 bits.
...
De facto, however, the top 8 bits of the lower 16 bits are indeed used as a type field
That is, an ioctl number is constructed from 4 fields, from upper to lower:
dir
- direction, 2 bits.
size
- size of the argument, 14 bits.type
- a number uniquely representing a driver, 8 bit.nr
- a number which is unique for the type (for the driver), 8 bit.For decode
#define IOCTL_MAGIC 'A'
#define IOCTL_NAME _IOWR(IOCTL_MAGIC, 2, ioctl_param)
you need to know the size of ioctl_param
structure (sizeof(ioctl_param)
).
E.g., if the size of the structure is 16 bytes, then ioctl fields are:
dir
- 0x3 (both read and write).size
- 0x10 (size of the structure, 16).type
- 0x41 (ASCII code of the character A
).nr
- 0x2 (the second argument).and the ioctl number itself is 0xc0104102.