aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/hid/hidraw.txt119
-rw-r--r--samples/Kconfig6
-rw-r--r--samples/Makefile2
-rw-r--r--samples/hidraw/Makefile8
-rw-r--r--samples/hidraw/hid-example.c167
5 files changed, 301 insertions, 1 deletions
diff --git a/Documentation/hid/hidraw.txt b/Documentation/hid/hidraw.txt
new file mode 100644
index 00000000000..029e6cb9a7e
--- /dev/null
+++ b/Documentation/hid/hidraw.txt
@@ -0,0 +1,119 @@
1 HIDRAW - Raw Access to USB and Bluetooth Human Interface Devices
2 ==================================================================
3
4The hidraw driver provides a raw interface to USB and Bluetooth Human
5Interface Devices (HIDs). It differs from hiddev in that reports sent and
6received are not parsed by the HID parser, but are sent to and received from
7the device unmodified.
8
9Hidraw should be used if the userspace application knows exactly how to
10communicate with the hardware device, and is able to construct the HID
11reports manually. This is often the case when making userspace drivers for
12custom HID devices.
13
14Hidraw is also useful for communicating with non-conformant HID devices
15which send and receive data in a way that is inconsistent with their report
16descriptors. Because hiddev parses reports which are sent and received
17through it, checking them against the device's report descriptor, such
18communication with these non-conformant devices is impossible using hiddev.
19Hidraw is the only alternative, short of writing a custom kernel driver, for
20these non-conformant devices.
21
22A benefit of hidraw is that its use by userspace applications is independent
23of the underlying hardware type. Currently, Hidraw is implemented for USB
24and Bluetooth. In the future, as new hardware bus types are developed which
25use the HID specification, hidraw will be expanded to add support for these
26new bus types.
27
28Hidraw uses a dynamic major number, meaning that udev should be relied on to
29create hidraw device nodes. Udev will typically create the device nodes
30directly under /dev (eg: /dev/hidraw0). As this location is distribution-
31and udev rule-dependent, applications should use libudev to locate hidraw
32devices attached to the system. There is a tutorial on libudev with a
33working example at:
34 http://www.signal11.us/oss/udev/
35
36The HIDRAW API
37---------------
38
39read()
40-------
41read() will read a queued report received from the HID device. On USB
42devices, the reports read using read() are the reports sent from the device
43on the INTERRUPT IN endpoint. By default, read() will block until there is
44a report available to be read. read() can be made non-blocking, by passing
45the O_NONBLOCK flag to open(), or by setting the O_NONBLOCK flag using
46fcntl().
47
48On a device which uses numbered reports, the first byte of the returned data
49will be the report number; the report data follows, beginning in the second
50byte. For devices which do not use numbered reports, the report data
51will begin at the first byte.
52
53write()
54--------
55The write() function will write a report to the device. For USB devices, if
56the device has an INTERRUPT OUT endpoint, the report will be sent on that
57endpoint. If it does not, the report will be sent over the control endpoint,
58using a SET_REPORT transfer.
59
60The first byte of the buffer passed to write() should be set to the report
61number. If the device does not use numbered reports, the first byte should
62be set to 0. The report data itself should begin at the second byte.
63
64ioctl()
65--------
66Hidraw supports the following ioctls:
67
68HIDIOCGRDESCSIZE: Get Report Descriptor Size
69This ioctl will get the size of the device's report descriptor.
70
71HIDIOCGRDESC: Get Report Descriptor
72This ioctl returns the device's report descriptor using a
73hidraw_report_descriptor struct. Make sure to set the size field of the
74hidraw_report_descriptor struct to the size returned from HIDIOCGRDESCSIZE.
75
76HIDIOCGRAWINFO: Get Raw Info
77This ioctl will return a hidraw_devinfo struct containing the bus type, the
78vendor ID (VID), and product ID (PID) of the device. The bus type can be one
79of:
80 BUS_USB
81 BUS_HIL
82 BUS_BLUETOOTH
83 BUS_VIRTUAL
84which are defined in linux/input.h.
85
86HIDIOCGRAWNAME(len): Get Raw Name
87This ioctl returns a string containing the vendor and product strings of
88the device. The returned string is Unicode, UTF-8 encoded.
89
90HIDIOCGRAWPHYS(len): Get Physical Address
91This ioctl returns a string representing the physical address of the device.
92For USB devices, the string contains the physical path to the device (the
93USB controller, hubs, ports, etc). For Bluetooth devices, the string
94contains the hardware (MAC) address of the device.
95
96HIDIOCSFEATURE(len): Send a Feature Report
97This ioctl will send a feature report to the device. Per the HID
98specification, feature reports are always sent using the control endpoint.
99Set the first byte of the supplied buffer to the report number. For devices
100which do not use numbered reports, set the first byte to 0. The report data
101begins in the second byte. Make sure to set len accordingly, to one more
102than the length of the report (to account for the report number).
103
104HIDIOCGFEATURE(len): Get a Feature Report
105This ioctl will request a feature report from the device using the control
106endpoint. The first byte of the supplied buffer should be set to the report
107number of the requested report. For devices which do not use numbered
108reports, set the first byte to 0. The report will be returned starting at
109the first byte of the buffer (ie: the report number is not returned).
110
111Example
112---------
113In samples/, find hid-example.c, which shows examples of read(), write(),
114and all the ioctls for hidraw. The code may be used by anyone for any
115purpose, and can serve as a starting point for developing applications using
116hidraw.
117
118Document by:
119 Alan Ott <alan@signal11.us>, Signal 11 Software
diff --git a/samples/Kconfig b/samples/Kconfig
index e03cf0e374d..52f4264b300 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -61,4 +61,10 @@ config SAMPLE_KDB
61 Build an example of how to dynamically add the hello 61 Build an example of how to dynamically add the hello
62 command to the kdb shell. 62 command to the kdb shell.
63 63
64config SAMPLE_HIDRAW
65 tristate "Build simple hidraw example"
66 depends on HIDRAW
67 help
68 Build an example of how to use hidraw from userspace.
69
64endif # SAMPLES 70endif # SAMPLES
diff --git a/samples/Makefile b/samples/Makefile
index f26c0959fd8..6280817c2b7 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,4 +1,4 @@
1# Makefile for Linux samples code 1# Makefile for Linux samples code
2 2
3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \ 3obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \
4 hw_breakpoint/ kfifo/ kdb/ 4 hw_breakpoint/ kfifo/ kdb/ hidraw/
diff --git a/samples/hidraw/Makefile b/samples/hidraw/Makefile
new file mode 100644
index 00000000000..7811cb0289a
--- /dev/null
+++ b/samples/hidraw/Makefile
@@ -0,0 +1,8 @@
1# kbuild trick to avoid linker error. Can be omitted if a module is built.
2obj- := dummy.o
3
4# List of programs to build
5hostprogs-y := hid-example
6
7# Tell kbuild to always build the programs
8always := $(hostprogs-y)
diff --git a/samples/hidraw/hid-example.c b/samples/hidraw/hid-example.c
new file mode 100644
index 00000000000..40e3d620058
--- /dev/null
+++ b/samples/hidraw/hid-example.c
@@ -0,0 +1,167 @@
1/*
2 * Hidraw Userspace Example
3 *
4 * Copyright (c) 2010 Alan Ott <alan@signal11.us>
5 * Copyright (c) 2010 Signal 11 Software
6 *
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.
10 */
11
12/* Linux */
13#include <linux/types.h>
14#include <linux/input.h>
15#include <linux/hidraw.h>
16
17/* Unix */
18#include <sys/ioctl.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <unistd.h>
23
24/* C */
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28#include <errno.h>
29
30const char *bus_str(int bus);
31
32int main(int argc, char **argv)
33{
34 int fd;
35 int i, res, desc_size = 0;
36 char buf[256];
37 struct hidraw_report_descriptor rpt_desc;
38 struct hidraw_devinfo info;
39
40 /* Open the Device with non-blocking reads. In real life,
41 don't use a hard coded path; use libudev instead. */
42 fd = open("/dev/hidraw0", O_RDWR|O_NONBLOCK);
43
44 if (fd < 0) {
45 perror("Unable to open device");
46 return 1;
47 }
48
49 memset(&rpt_desc, 0x0, sizeof(rpt_desc));
50 memset(&info, 0x0, sizeof(info));
51 memset(buf, 0x0, sizeof(buf));
52
53 /* Get Report Descriptor Size */
54 res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
55 if (res < 0)
56 perror("HIDIOCGRDESCSIZE");
57 else
58 printf("Report Descriptor Size: %d\n", desc_size);
59
60 /* Get Report Descriptor */
61 rpt_desc.size = desc_size;
62 res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
63 if (res < 0) {
64 perror("HIDIOCGRDESC");
65 } else {
66 printf("Report Descriptor:\n");
67 for (i = 0; i < rpt_desc.size; i++)
68 printf("%hhx ", rpt_desc.value[i]);
69 puts("\n");
70 }
71
72 /* Get Raw Name */
73 res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
74 if (res < 0)
75 perror("HIDIOCGRAWNAME");
76 else
77 printf("Raw Name: %s\n", buf);
78
79 /* Get Physical Location */
80 res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
81 if (res < 0)
82 perror("HIDIOCGRAWPHYS");
83 else
84 printf("Raw Phys: %s\n", buf);
85
86 /* Get Raw Info */
87 res = ioctl(fd, HIDIOCGRAWINFO, &info);
88 if (res < 0) {
89 perror("HIDIOCGRAWINFO");
90 } else {
91 printf("Raw Info:\n");
92 printf("\tbustype: %d (%s)\n",
93 info.bustype, bus_str(info.bustype));
94 printf("\tvendor: 0x%04hx\n", info.vendor);
95 printf("\tproduct: 0x%04hx\n", info.product);
96 }
97
98 /* Set Feature */
99 buf[0] = 0x9; /* Report Number */
100 buf[1] = 0xff;
101 buf[2] = 0xff;
102 buf[3] = 0xff;
103 res = ioctl(fd, HIDIOCSFEATURE(4), buf);
104 if (res < 0)
105 perror("HIDIOCSFEATURE");
106 else
107 printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
108
109 /* Get Feature */
110 buf[0] = 0x9; /* Report Number */
111 res = ioctl(fd, HIDIOCGFEATURE(256), buf);
112 if (res < 0) {
113 perror("HIDIOCGFEATURE");
114 } else {
115 printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
116 printf("Report data (not containing the report number):\n\t");
117 for (i = 0; i < res; i++)
118 printf("%hhx ", buf[i]);
119 puts("\n");
120 }
121
122 /* Send a Report to the Device */
123 buf[0] = 0x1; /* Report Number */
124 buf[1] = 0x77;
125 res = write(fd, buf, 2);
126 if (res < 0) {
127 printf("Error: %d\n", errno);
128 perror("write");
129 } else {
130 printf("write() wrote %d bytes\n", res);
131 }
132
133 /* Get a report from the device */
134 res = read(fd, buf, 16);
135 if (res < 0) {
136 perror("read");
137 } else {
138 printf("read() read %d bytes:\n\t", res);
139 for (i = 0; i < res; i++)
140 printf("%hhx ", buf[i]);
141 puts("\n");
142 }
143 close(fd);
144 return 0;
145}
146
147const char *
148bus_str(int bus)
149{
150 switch (bus) {
151 case BUS_USB:
152 return "USB";
153 break;
154 case BUS_HIL:
155 return "HIL";
156 break;
157 case BUS_BLUETOOTH:
158 return "Bluetooth";
159 break;
160 case BUS_VIRTUAL:
161 return "Virtual";
162 break;
163 default:
164 return "Other";
165 break;
166 }
167}