1

With reference to this link http://stackoverflow.com/questions/8922102/adding-new-ioctls-into-kernel-number-range I came to know that it is mandatory to encode directions into ioctl numbers, if copy-to-user/copy-from-user is not used.

Kindly please someone explain how to arrive at new ioctl numbers with setting encoded direction.

kzs
  • 1,793
  • 3
  • 24
  • 44
  • What sort of driver is it? – Ignacio Vazquez-Abrams Jul 05 '13 at 04:03
  • it is actually i2c driver. I am again registering it as misc driver. so major is 10 – kzs Jul 05 '13 at 06:44
  • @IgnacioVazquez-Abrams: I have reffered to `include/asm-generic/ioctls.h` and arch specific ioctls.h and started giving numbers from say for eg: last ioctl number in that header file is 0x5460 , I started giving from 0x5461. Am i going in right direction.? – kzs Jul 05 '13 at 06:50
  • 1
    The question that you did (not actually) link to has a double negation: encoding the parameter direction is necessary if you use some common copy_from/to_user code. – CL. Jul 05 '13 at 08:20
  • thanks for that. and what still i wanted to know how to encode directions into the codes. – kzs Jul 05 '13 at 08:40

1 Answers1

1

You need to use the _IO() series of macros and the guidelines which are documented in the official ioctl-number documentation. The _IO macros are declared in ioctl.h. Most take a an 8-bit int to represent the type, an 8-bit int to represent the ioctl number and the data type if the data you intend to pass into the IOCTL call. Ideally, the type is unique que to your driver, however most numbers have already been assigned so this is difficult to do. The ioctl number is just to differentiate it from other numbers and may be assigned sequentially.

You can get more info from Chapter 6 of the LDD3.


Edit: Your comment leads me to believe you need a hard example. You shouldn't refer to an IOCTL number by it's hex value. Instead use the _IO() macros like so:

// The type for all of my IOCTL calls.
// This number is from 0 to 255.
// Does not conflict with any number assignments in ioctl-number.txt.
#define MYIOC_TYPE 0xA4

// This ioctl takes no arguments.  It does something in the driver
// without passing data back and forth.  The ioctl number is from 0 to 255.
#define MYIOC_DOFOO _IO(MYIOC_TYPE, 0x00)

// This ioctl reads an integer value from the driver.
#define MYIOC_GETFOO _IOR(MYIOC_TYPE, 0x01, int)

// This ioctl writes an integer value from the driver.
#define MYIOC_SETFOO _IOW(MYIOC_TYPE, 0x02, int)

// This ioctl is confusing and is probably to be avoided.
// It writes a value to the driver while at the same time
// retrieves a value in the same pointer.
#define MYIOC_SETANDGETFOO _IOWR(MYIOC_TYPE, 0x03, int)

The macros encode the data in the ioctl number. So instead of referring to a single hex number it is much more appropriate to refer to an ioctl's type and number. These macros have the added benefit that they document what direction data goes to/from and what the type of that data is.

You can get more info from Chapter 6 of the LDD3.

Benjamin Leinweber
  • 2,774
  • 1
  • 24
  • 41
  • I have reffered to include/asm-generic/ioctls.h and arch specific ioctls.h and started giving numbers from say for eg: last ioctl number in that header file is 0x5460 , I started giving from 0x5461. Am i going in right direction.? – kzs Jul 05 '13 at 08:41
  • See updates above. This is typed on my phone, so sorry if there is a syntax error. Code is hard to write on a phone. – Benjamin Leinweber Jul 05 '13 at 15:41