next up previous
Next: Integrating FUSD With Your Up: FUSD: A Linux Framework Previous: Using Information in fusd_file_info

Subsections

Writing ioctl Callbacks

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.

Using macros to generate ioctl command numbers

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:


\begin{Program}
% latex2html id marker 562\listinginput[5]{1}{ioctl.h.example}...
...sing the {\tt \_IO} macros to generate {\tt ioctl}
command numbers}\end{Program}

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.

Example client calls and driver callbacks

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.


\begin{Program}
% latex2html id marker 581\listinginput[5]{1}{ioctl-client.c.e...
....c: A program that makes {\tt ioctl} requests on
a file descriptor}\end{Program}

The portion of the FUSD driver that services these calls is shown in Program 8.


\begin{Program}
% latex2html id marker 588\listinginput[5]{1}{ioctl-server.c.e...
...caption{ioctl-server.c: A driver that handles {\tt ioctl} requests}\end{Program}

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.


next up previous
Next: Integrating FUSD With Your Up: FUSD: A Linux Framework Previous: Using Information in fusd_file_info