DETAILED parameters and ioctl

  

ioctl

function name: ioctl

Function: Control I /O device

Usage: int ioctl (int handle, int cmd, [int *argdx, int argcx]);

Comments for macros defined in include/asm/ioctl.h:

#define _IOC_NRBITS 8 //word bits of the number field Width, 8bits

#define _IOC_TYPEBITS 8 //The width of the word field of the type field, 8bits

#define _IOC_SIZEBITS 14 //The width of the word size of the size field, 14bits
word bit width

#define _IOC_DIRBITS 2 //direction (direction) field, 2bits

#define _IOC_NRMASK ((1 & lt; & lt; _IOC_NRBITS) -1) //sequence number field mask, 0x000000FF

#define _IOC_TYPEMASK ((1 & lt; & lt; _IOC_TYPEBITS) -1) //mask magic number field, 0x000000FF

#define _IOC_SIZEMASK ((1 & lt; & lt ; _IOC_SIZEBITS)-1) //Mask of the size field, 0x00003FFF

#define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1) //Mask of the direction field, 0x00000003

#define _IOC_NRSHIFT 0 //The displacement of the ordinal field in the whole field, 0

#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS) //The displacement of the magic field, 8

#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+ _IOC_TYPEBITS) //The displacement of the size field, 16

#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS) //The displacement of the direction field, 30

/*

* Direction bits.

*/

#define _IOC_NONE 0U //No data transfer

#define _IOC_WRITE 1U //Write data to the device, the driver must read data from user space

#define _IOC_READ 2U //Read data from the device, the driver must write data to the user space

#define _IOC(dir,type,nr,size) \\

(((dir) << _IOC_DIRSHIFT) |  \\

((type) << _IOC_TYPESHIFT) |  \\

((nr) << _IOC_NRSHIFT) |  \\

((size) << _IOC_SIZESHIFT))

/*

* used to create numbers

*/

//Construct a command number with no arguments

#define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0)

//Construct from the driver Command number for reading data

#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),sizeof(size))

//for Driver write data command

#define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),sizeof(size))

//for two-way Transfer

#define _IOWR(type,nr,size) _IOC(_IOC_READ| _IOC_WRITE,(type),(nr),sizeof(size))

/*

*used to decode ioctl numbers..

*/

//Parse the data direction from the command parameters, ie write or read

#define _IOC_DIR(nr) (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)

//parse the magic number from the command parameters type

#define _IOC_TYPE(nr) (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)

//from the command parameters Interpret the number in the number

#define _IOC_NR(nr) (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)

//Resolve the user data size from the command parameters

#define _IOC_SIZE (nr) (((nr) & gt; & gt; _IOC_SIZESHIFT) & _IOC_SIZEMASK)

/* ... and for the drivers /sound files ... * /

#define IOC_IN (_IOC_WRITE & lt; & lt; _IOC_DIRSHIFT)

#define IOC_OUT (_IOC_READ & lt; & lt; _IOC_DIRSHIFT)

#define IOC_INOUT ((_IOC_WRITE | _IOC_READ) & lt; & lt; _IOC_DIRSHIFT)

#define IOCSIZE_MASK (_IOC_SIZEMASK & lt; & lt; _IOC_SIZESHIFT)

#define IOCSIZE_SHIFT (_IOC_SIZESHIFT)

Program Example:
< #include <stdlib.h>

#include <stdio.h>

#include <sys/ioctl.h>

int main(void ) {

..int stat;

/* use func 8 to determine if the default drive is removable */

..stat = ioctl(0, 8, 0, 0);

..if (!stat)

....printf("Drive %c is removable.\ ", getdisk() + 'A') ;

..else

....printf("Drive %c is not removable.\ ", getdisk() + 'A');

..return 0;

}

Copyright © Windows knowledge All Rights Reserved