The POSIX API provides for a function called ioctl, which allows ``out-of-band'' configuration information to be passed to a device driver through a file descriptor. Using FUSD, you can write a device driver with a callback to handle ioctl requests from clients. For the most part, it's just like writing a callback for read or write, as we've seen in previous sections. From the client's point of view, ioctl traditionally takes three arguments: a file descriptor, a command number, and a pointer to any additional data that might be required for the command.
The Linux header file /usr/include/asm/ioctl.h defines macros that must be used to create the ioctl command number. These macros take various combinations of three arguments:
The macros used to generate command numbers are:
Program 6 is an example header file showing the use of these macros. In real programs, the client executing an ioctl and the driver that services it must share the same header file.
Program 7 shows a client program that executes ioctls using the ioctl command numbers defined in Program 6. The ioctl_data_t is application-specific; our simple test program defines it as a structure containing two arrays of characters. The first ioctl call (line 10) sends the command IOCTL_TEST3, which retrieves strings from the driver. The second ioctl uses the command IOCTL_TEST4 (line 18), which sends strings to the driver.
The portion of the FUSD driver that services these calls is shown in Program 8.
The ioctl example header file and test programs shown in this document (Programs 6, 7, and 8) are actually contained in a larger, single example program included in the FUSD distribution called ioctl.c. That source code shows other variations on calling and servicing ioctl commands.