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

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

  • Subject: RE: [OCLUG-Tech] device driver magic number for ioctl() in linux
  • From: "Zhao, Joe" <jozhao [ at ] ciena [ dot ] com>
  • Date: Tue, 4 Nov 2008 09:04:18 -0500
Hi, Bart:
Thanks a lot for providing me the detailed information. I am writing
character device driver and using _IO, _IOW,_IOWR, _IOR macros in linux
2.6 kernel. I did observe the problem if the magic number is greater
than 0xFF which makes sense based on your information (i.e. type is only
8bit size). Is there any way we can handle the case if the type has to
be greater than 0xFF? Can the user use any value for the type within the
range of 0x00 to 0xFF? 

Regards,
Joe

-----Original Message-----
From: Bart Trojanowski [mailto:bart-oclug [ at ] jukie [ dot ] net] 
Sent: Tuesday, November 04, 2008 12:20 AM
To: Zhao, Joe; linux [ at ] lists [ dot ] oclug [ dot ] on [ dot ] ca
Subject: 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/