I have a 64 bit Enterprice SuSE 11
I have an application which open a HIDRAW device and operate an ioctl function on it to get raw info from this device like below:
struct hidraw_devinfo devinfo;
int fd = open("/dev/hidraw0", 0);
int ret = ioctl(fd, HIDIOCGRAWINFO, &devinfo);
...
If I compile this program in 64 bit mode there is no error and no problem and when I execute the application the ioctl function works properly.
g++ main.cpp
If I complie this program in 32 bit mode there is also no error and no problem. but when I execute the application the ioctl function return EINVAL error(errno = 22 , Invalid Argument)
g++ -m32 main.cpp
what’s the problem?
Note:
struct hidraw_devinfo
{
__u32 bustype;
__s16 vendor;
__s16 product;
}
Linux
ioctldefinitions and compatibility layers are a fascinating topic I’ve just bashed my head against.Typically
ioctldefinitions use a family of macros _IOW/_IOR et al that take your argument type-name as a reference, along with a magic number and ordinal value that are munged to give you your ioctl argument value (egHIDIOCGRAWINFO). The type-name is used to encodesizeof(arg_type)into the definition. This means that the type used in user space determines the value generated by theioctlmacro – ie HIDIOCGRAWINFO may vary based on include conditions.Here is the first point where 32- and 64-bit differ, the
sizeofmay differ, depending on packing, use of vague data-sizes (eg long), but especially (and unavoidably) if you use a pointer argument. So in this case a 64-bit kernel module what wants to support 32-bit clients needs do define a compatibility argument-type to match the layout of the 32-bit equivalent of the argument type and thus a 32-bit compatible ioctl. These 32-bit equivalent definitions make use of a kernel facility/layer calledcompat.In your case the
sizeof()is the same so that is not the path you are taking – but its important to understand the whole of what could be happening.In addition a kernel configuration may define
CONFIG_COMPATwhich changes the sys-call wrappers (especially the code surrounding the user/kernel interface wrtioctl) to ease the burden of supporting 32- and 64-bit. Part of this includes a compatibilityioctlcallback calledioctl_compat.What I’ve seen is with
CONFIG_COMPATdefined that 32-bit programs will generate code that deliversioctls to theioctl_compatcallback, even if it could generate the sameioctlvalue as 64-bit does (eg in your case). So the driver writer needs to make sure thatioctl_compathandles both the special (different) 32-bit compatibleioctlTYPEs and the normal “64-bit – or unchanged 32-bit” types.So a kernel-module designed and tested on 32-bit only and 64-bit only systems (without CONFIG_COMPAT) may work for 32- and 64-bit programs, but not for one which supports both.
So looking in HID I see this was added in 2.6.38:
http://lxr.linux.no/#linux+v2.6.38/drivers/hid/hidraw.c#L347