3

我需要使用ioctl. 我需要使用ioctl(fd, USBDEVFS_REAPURBNDELAY, &receive);。但是USBDEVFS_REAPURBNDELAY在内核头文件中定义,所以我包含一些这样的内核文件:

#include <linux/usb.h>
#include <linux/usbdevice_fs.h>
#include <asm/byteorder.h>
#include <linux/usb/ch9.h>

并编译它gcc -g -I /usr/src/linux-3.4.4-2-pae/include/ -o /home/madper/kernel/usb/select_for_usb /home/madper/kernel/usb/select_for_usb.c

它说#warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]

并且伴随着很多错误,那么,使用内核头文件的正确方法是什么?

/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:21:2: error: unknown type name ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:36:2: error: unknown type name ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:37:33: error: ‘kernel_ulong_t’ undeclared here (not in a function)
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:37:3: error: requested alignment is not an integer constant
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:119:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:141:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:154:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:168:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:179:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:187:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:195:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:200:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:227:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:260:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:314:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:341:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:366:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:413:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:433:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:444:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:479:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:508:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:540:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:551:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:566:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/usr/src/linux-3.4.4-2-pae/include/linux/mod_devicetable.h:582:2: error: expected specifier-qualifier-list before ‘kernel_ulong_t’
/home/madper/kernel/usb/select_for_usb.c: In function ‘main’:
/home/madper/kernel/usb/select_for_usb.c:46:30: error: expected expression before ‘void’
4

2 回答 2

3

你真的不应该直接包含内核头文件。在某些情况下,处理它是必要的,并且可能是最好的处理方法是获取内核头文件的副本并仅删除您需要的部分。但在这种情况下,我不确定我是否理解你为什么需要内核头文件。为什么仅仅包含它不起作用<linux/usbdevice_fs.h>

在我查看的所有系统(几个不同的 Ubuntu 系统和一个 RHEL 6 系统)上,该文件可以从用户空间包含并具有USBDEVFS_REAPURBNDELAY. 我试过

#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>

int main()
{
    printf("USBDEVFS_REAPURBNDELAY is %ld\n", USBDEVFS_REAPURBNDELAY);
    return 0;
}

我可以干净地构建它,当我运行它时,我得到

USBDEVFS_REAPURBNDELAY is 1074287885

所以至少根据你上面写的内容,你不需要任何内核头文件来定义那个ioctl。我怀疑您认为用户空间 USB 内容所需的其他任何东西都以干净的标头类似地导出到用户空间。

于 2012-07-20T09:22:19.943 回答
2

您不能将内核头文件用于用户程序。即使您以某种方式这样做,也强烈建议您这样做。检查以下链接以获得更多说明。

http://lwn.net/Articles/113349/

如果您确实想从用户空间控制 USB 设备,请查看 libusb。

于 2012-07-19T10:41:57.033 回答