home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] device driver magic number for ioctl() in linux

* Zhao, Joe <jozhao [ at ] ciena [ dot ] com> [081103 20:12]:
> Can we define the magic number with the value greater than 0xFF? Where
> is this magic number used by linux kernel?

In most cases it is not used by the kernel.  Particularly, if you write
a simple driver that does not fit into a larger hierarchy -- like a
character device driver -- the ioctl command numbers will be an opaque
integer (32bit) to the rest of the kernel.

If you write character device driver, the only numbers that are
interpreted by a 2.6.27 kernel are:

        #define FIONBIO     0x5421
        #define FIONCLEX    0x5450
        #define FIOCLEX     0x5451
        #define FIOASYNC    0x5452
        #define FIOQSIZE    0x5460

so as long as you avoid the 3rd "magic" byte of 0x54 you should be fine.

Now, having said that, best practices suggest that you use the _IO,
_IOW, _IOR, and _IOWR macros.  The output of those macros is a 32bit
number that contains 8bits for the type (semi-uniqe for your driver),
8bits for the number (each command in your driver is given a number),
2bits for the direction, and 14 bits for the size.

Example:

        // common header shared between kernel and application...

        #define FOO_MAGIC 123       // completely arbitrary

        #define FOO_CMD_READ        _IOR(FOO_MAGIC, 1, struct foo)
        #define FOO_CMD_WRITE       _IOW(FOO_MAGIC, 2, struct foo)

-Bart

-- 
				WebSig: http://www.jukie.net/~bart/sig/