1

参考这个链接http://stackoverflow.com/questions/8922102/adding-new-ioctls-into-kernel-number-range,我知道如果copy-to-user/copy-from-user不使用,将方向编码为 ioctl 数字是强制性的。

请有人解释如何通过设置编码方向来获得新的 ioctl 号码。

4

1 回答 1

1

您需要使用官方 ioctl-number 文档中记录的 _IO() 系列宏和指南。_IO 宏在ioctl.h中声明。大多数采用 8 位 int 来表示类型,如果您打算将数据传递给 IOCTL 调用,则使用 8 位 int 来表示 ioctl 编号和数据类型。理想情况下,该类型对您的司机来说是唯一的,但是大多数号码已经分配,​​所以这很难做到。ioctl 编号只是为了与其他编号区分开来,可以按顺序分配。

您可以从LDD3 的第 6 章获得更多信息。


编辑:你的评论让我相信你需要一个很难的例子。您不应该通过它的十六进制值来引用 IOCTL 编号。而是像这样使用 _IO() 宏:

// 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)

宏对 ioctl 编号中的数据进行编码。因此,与其指代单个十六进制数字,不如指代 ioctl 的类型和数字更合适。这些宏还有一个额外的好处,就是它们记录了数据去往/来自的方向以及该数据的类型。

您可以从LDD3 的第 6 章获得更多信息。

于 2013-07-05T06:15:56.447 回答