|
大家好!
我现在使用的版本是Fedora 3, kernerl version:2.6.9-1.667;
在使用HIDIOCSUSAGE一次向设备写1个value可以,但使用HIDIOCSUSAGES向设备写多个value却失败。
经过调查发现: kernel从2.4版本开始代码就已经实现了HIDIOCSUSAGES方法,但是从/usr/include/linux/hiddev.h中却没有对HIDIOCSUSAGES和及其相关的hiddev_usage_ref_multi结构进行定义,而且在kernel中关于hid设备driver的说明文件hiddev.txt中也没列出HIDIOCSUSAGES方法,
向在使用HIDIOCSUSAGES向设备写数据不成功,也不知道问题出在哪边?请大家给我点指点,谢谢!
下面是我使用HIDIOCSUSAGES向设备写数据的代码:
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/hiddev.h>
#define DEBUG 1
#define HID_MAX_MULTI_USAGES 1024
#define HIDIOCGUSAGES _IOWR('H', 0x13, struct hiddev_usage_ref_multi)
#define HIDIOCSUSAGES _IOW('H', 0x14, struct hiddev_usage_ref_multi)
#define HID_MAX_MULTI_USAGES 1024
struct hiddev_usage_ref_multi {
struct hiddev_usage_ref uref;
__u32 num_values;
__s32 values[HID_MAX_MULTI_USAGES];
};
int main (int argc, char **argv)
{
int fd = -1;
int rtn,ret;
int version;
unsigned char tmp;
int i,j;
struct hiddev_report_info rinfo;
struct hiddev_usage_ref uref;
struct hiddev_field_info finfo;
struct hiddev_usage_ref_multi urefs;
unsigned char byLgtOn[] = {0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char byLgtOff[] = {0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (argc != 2)
{
fprintf(stderr, "usage: %s hiddevice - probably /dev/usb/hiddev0\n", argv[0]);
exit(1);
}
if ((fd = open(argv[1], O_RDONLY)) < 0)
{
perror("hiddev open");
exit(1);
}
rinfo.report_type = HID_REPORT_TYPE_INPUT;
rinfo.report_id = HID_REPORT_ID_FIRST;
ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
for (i = 0; i < rinfo.num_fields; i++)
{
finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id;
finfo.field_index = i;
printf("finfo.maxusage %d\n", finfo.maxusage);
ioctl(fd, HIDIOCGFIELDINFO, &finfo);
for (j = 0; j < finfo.maxusage; j++)
{
uref.field_index = i;
uref.usage_index = j;
ioctl(fd, HIDIOCGUCODE, &uref);
printf("usage report_type %d report_id %d field_index %d usage_index %d usage_code %d\n",
uref.report_type, uref.report_id, uref.field_index, uref.usage_index, uref.usage_code);
ioctl(fd, HIDIOCGUSAGE, &uref);
printf("\n");
}
}
rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
for (i = 0; i < rinfo.num_fields; i++)
{
printf("num_fields %d\n",rinfo.num_fields );
finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id;
finfo.field_index = i;
printf("finfo.report_id %d\n", finfo.report_id);
ioctl(fd, HIDIOCGFIELDINFO, &finfo);
urefs.uref.report_type = HID_REPORT_TYPE_OUTPUT;
urefs.uref.report_id = finfo.report_id;
urefs.num_values = 8;
for (j = 0; j < finfo.maxusage; j++)
{
urefs.uref.field_index = i;
urefs.uref.usage_index = j;
urefs.uref.usage_code += 4;
urefs.values[j] = byLgtOn[j];
printf("usage report_type %d report_id %d field_index %d usage_index %d usage_code %d\n",
urefs.uref.report_type, urefs.uref.report_id, urefs.uref.field_index,
urefs.uref.usage_index, urefs.uref.usage_code);
}
if (ioctl(fd, HIDIOCSUSAGES, &urefs) == 0)
printf("successfully.....\n");
else
printf("unsuccessfully.....\n");
rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
rinfo.report_id = HID_REPORT_ID_FIRST;
if (ioctl(fd, HIDIOCSREPORT, &rinfo) == 0)
printf("successfully.....\n");
else
printf("unsuccessfully.....\n");
}
/*
while(ret >= 0)
{
printf("ret %d\n", ret);
for (i = 0; i < rinfo.num_fields; i++)
{
printf("num_fields %d\n",rinfo.num_fields );
finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id;
finfo.field_index = i;
printf("finfo.maxusage %d\n", finfo.maxusage);
ioctl(fd, HIDIOCGFIELDINFO, &finfo);
for (j = 0; j < finfo.maxusage; j++)
{
uref.field_index = i;
uref.usage_index = j;
ioctl(fd, HIDIOCGUCODE, &uref);
printf("usage report_type %d report_id %d field_index %d usage_index %d usage_code %d\n",
uref.report_type, uref.report_id, uref.field_index, uref.usage_index, uref.usage_code);
ioctl(fd, HIDIOCGUSAGE, &uref);
printf("\n");
}
}
uref.report_id |= HID_REPORT_ID_NEXT;
ret = ioctl(fd, HIDIOCGREPORTINFO, &uref);
printf("ret %d\n", ret);
}
*/
close(fd);
exit(0);
} |
|