IOCTL(9)

NAME
ioctl - how to implement a new ioctl call to access device drivers

SYNOPSIS
#include <sys/ioctl.h>
#include <sys/ioccom.h>
int
ioctl(int,, unsigned, long,, ...);

DESCRIPTION
Ioctl are internally defined as

#define FOOIOCTL fun(t,n,pt)
where the different wariables and funcions are:

FOOIOCTL()
    the name as the ioctl is later known in a as second argument to a ioctl system call. E.g. ioctl(s,FOOIOCTL,...)

fun()
    a macro which can be one of

    _IOR
        the call only reads parameters from the kernel and does not pass any to it

    _IOW
        the call only writes parameters to the kernel, but does not want anything back

    _IOWR
        the call writes data to the kernel and wants information back.

t   This integer describes to which subsystem the ioctl applies. T can be one of

    'd' the disk subsystem

    'f' files

    'i' a (pseudo) interface

    'r' the routing subsystem

    's' the socket layer

    't' the tty layer

    'u' user defined ???

n   This numbers the ioctl within the group. There may be only one n() for a given t(). This is a unsigned 8 bit number.

pt  This specifyies the type of the passed parameter. This one gets internally transformed to the size of the parameter, so if you e.g. want to pass a structure, then you have to specify that structure and not a pointer to it or sizeof(struct foo)
In order for the new ioctl to be known to the system it is installed in either <sys/ioctl.h> or one of the files that are reached from <sys/ioctl.h>.

EXAMPLE
#define FOOIOCTL _IOWR('i',23,int) int a=3; error = ioctl(s,FOOICTL, &a); Within the ioctl()-routine of the driver, it can be then accessd like driver_ioctl(..,cmd,data) u_long cmd; caddr_t data; ... switch(cmd) { case FOOIOCTL: int *a = (int *)data; printf(" Value passed: %d,a); }

NOTES
Note that if you e.g. try to read information from e.g. a ethernet driver where the name of the card is included in the third argument (e.g. ioctl(s,READFROMETH,struct ifreq *)), then you have to use the _IOWR() form not the _IOR(), as passing the name of the card to the kernel already consists of writing data.

SEE ALSO
ioctl(2)