2 * Hidraw Userspace Example
4 * Copyright (c) 2010 Alan Ott <alan@signal11.us>
5 * Copyright (c) 2010 Signal 11 Software
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using hidraw.
13 #include <linux/types.h>
14 #include <linux/input.h>
15 #include <linux/hidraw.h>
18 * Ugly hack to work around failing compilation on systems that don't
19 * yet populate new version of hidraw.h to userspace.
21 #ifndef HIDIOCSFEATURE
22 #warning Please have your distro update the userspace kernel headers
23 #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
24 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
40 const char *bus_str(int bus
);
42 int main(int argc
, char **argv
)
45 int i
, res
, desc_size
= 0;
47 struct hidraw_report_descriptor rpt_desc
;
48 struct hidraw_devinfo info
;
49 char *device
= "/dev/hidraw0";
54 /* Open the Device with non-blocking reads. In real life,
55 don't use a hard coded path; use libudev instead. */
56 fd
= open(device
, O_RDWR
|O_NONBLOCK
);
59 perror("Unable to open device");
63 memset(&rpt_desc
, 0x0, sizeof(rpt_desc
));
64 memset(&info
, 0x0, sizeof(info
));
65 memset(buf
, 0x0, sizeof(buf
));
67 /* Get Report Descriptor Size */
68 res
= ioctl(fd
, HIDIOCGRDESCSIZE
, &desc_size
);
70 perror("HIDIOCGRDESCSIZE");
72 printf("Report Descriptor Size: %d\n", desc_size
);
74 /* Get Report Descriptor */
75 rpt_desc
.size
= desc_size
;
76 res
= ioctl(fd
, HIDIOCGRDESC
, &rpt_desc
);
78 perror("HIDIOCGRDESC");
80 printf("Report Descriptor:\n");
81 for (i
= 0; i
< rpt_desc
.size
; i
++)
82 printf("%hhx ", rpt_desc
.value
[i
]);
87 res
= ioctl(fd
, HIDIOCGRAWNAME(256), buf
);
89 perror("HIDIOCGRAWNAME");
91 printf("Raw Name: %s\n", buf
);
93 /* Get Physical Location */
94 res
= ioctl(fd
, HIDIOCGRAWPHYS(256), buf
);
96 perror("HIDIOCGRAWPHYS");
98 printf("Raw Phys: %s\n", buf
);
101 res
= ioctl(fd
, HIDIOCGRAWINFO
, &info
);
103 perror("HIDIOCGRAWINFO");
105 printf("Raw Info:\n");
106 printf("\tbustype: %d (%s)\n",
107 info
.bustype
, bus_str(info
.bustype
));
108 printf("\tvendor: 0x%04hx\n", info
.vendor
);
109 printf("\tproduct: 0x%04hx\n", info
.product
);
113 buf
[0] = 0x9; /* Report Number */
117 res
= ioctl(fd
, HIDIOCSFEATURE(4), buf
);
119 perror("HIDIOCSFEATURE");
121 printf("ioctl HIDIOCGFEATURE returned: %d\n", res
);
124 buf
[0] = 0x9; /* Report Number */
125 res
= ioctl(fd
, HIDIOCGFEATURE(256), buf
);
127 perror("HIDIOCGFEATURE");
129 printf("ioctl HIDIOCGFEATURE returned: %d\n", res
);
130 printf("Report data (not containing the report number):\n\t");
131 for (i
= 0; i
< res
; i
++)
132 printf("%hhx ", buf
[i
]);
136 /* Send a Report to the Device */
137 buf
[0] = 0x1; /* Report Number */
139 res
= write(fd
, buf
, 2);
141 printf("Error: %d\n", errno
);
144 printf("write() wrote %d bytes\n", res
);
147 /* Get a report from the device */
148 res
= read(fd
, buf
, 16);
152 printf("read() read %d bytes:\n\t", res
);
153 for (i
= 0; i
< res
; i
++)
154 printf("%hhx ", buf
[i
]);