Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ioctls

An ioctl request is one 32-bit number. It carries the direction of the data, the size of the argument, the class of the device and the command:

 31    30 29            16 15       8 7        0
+--------+----------------+----------+----------+
| dir    | size (14 bit)  | group    | command  |
+--------+----------------+----------+----------+

The head of usr/vex/ioctl.h fixes that layout with __k_IOCTL_SET_DIR, __k_IOCTL_SET_SIZE, __k_IOCTL_SET_GROUP and __k_IOCTL_SET_CMD. The helpers __k_IOCTL_VOID, __k_IOCTL_READ, __k_IOCTL_WRITE and __k_IOCTL_RDWR build a request from these parts, and each takes sizeof() of the structure that it passes.

Direction and size

DirectionConstantData moves
0__k_IOCTL_DIR_VOIDNothing
1__k_IOCTL_DIR_WRITEUser to kernel
2__k_IOCTL_DIR_READKernel to user
3__k_IOCTL_DIR_RDWRBoth ways

The size field holds the size of the argument, at most 16383 bytes. A driver can therefore accept a structure of an older size when the kernel has one more field. The request of each size stays different.

A request of a group that a file does not know fails with ENOTTY.

The groups

GroupHeaderDevice
'f'ioctl.hAny file, such as __k_FIONREAD
'F'fb.hThe framebuffer
'H'hwtree.hThe device tree feed
'L'kplog.hThe kernel log feed
'S'serial.hA serial port
'T'termios.hA terminal

__k_FIONREAD is the only request that every file takes. It reports a __k_int32_t with the bytes that are ready to read.

The pages of the device classes list the requests of each group:

A new request takes the next free command in its group. A new class of device takes a free letter.

Source

  • ioctl.h — the layout and the generic request