diff options
| author | Alan Ott <alan@signal11.us> | 2011-03-19 20:29:44 -0400 |
|---|---|---|
| committer | Jiri Kosina <jkosina@suse.cz> | 2011-03-22 06:43:50 -0400 |
| commit | c54ea4918c2b7722d7242ea53271356501988a9b (patch) | |
| tree | e4c7b3c77748eef20e8b844633643cb00d8bc5e2 | |
| parent | 99759619b27662d1290901228d77a293e6e83200 (diff) | |
HID: Documentation for hidraw
Documenation for the hidraw driver, with sample program.
Signed-off-by: Alan Ott <alan@signal11.us>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
| -rw-r--r-- | Documentation/hid/hidraw.txt | 119 | ||||
| -rw-r--r-- | samples/Kconfig | 6 | ||||
| -rw-r--r-- | samples/Makefile | 2 | ||||
| -rw-r--r-- | samples/hidraw/Makefile | 8 | ||||
| -rw-r--r-- | samples/hidraw/hid-example.c | 167 |
5 files changed, 301 insertions, 1 deletions
diff --git a/Documentation/hid/hidraw.txt b/Documentation/hid/hidraw.txt new file mode 100644 index 000000000000..029e6cb9a7e8 --- /dev/null +++ b/Documentation/hid/hidraw.txt | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | HIDRAW - Raw Access to USB and Bluetooth Human Interface Devices | ||
| 2 | ================================================================== | ||
| 3 | |||
| 4 | The hidraw driver provides a raw interface to USB and Bluetooth Human | ||
| 5 | Interface Devices (HIDs). It differs from hiddev in that reports sent and | ||
| 6 | received are not parsed by the HID parser, but are sent to and received from | ||
| 7 | the device unmodified. | ||
| 8 | |||
| 9 | Hidraw should be used if the userspace application knows exactly how to | ||
| 10 | communicate with the hardware device, and is able to construct the HID | ||
| 11 | reports manually. This is often the case when making userspace drivers for | ||
| 12 | custom HID devices. | ||
| 13 | |||
| 14 | Hidraw is also useful for communicating with non-conformant HID devices | ||
| 15 | which send and receive data in a way that is inconsistent with their report | ||
| 16 | descriptors. Because hiddev parses reports which are sent and received | ||
| 17 | through it, checking them against the device's report descriptor, such | ||
| 18 | communication with these non-conformant devices is impossible using hiddev. | ||
| 19 | Hidraw is the only alternative, short of writing a custom kernel driver, for | ||
| 20 | these non-conformant devices. | ||
| 21 | |||
| 22 | A benefit of hidraw is that its use by userspace applications is independent | ||
| 23 | of the underlying hardware type. Currently, Hidraw is implemented for USB | ||
| 24 | and Bluetooth. In the future, as new hardware bus types are developed which | ||
| 25 | use the HID specification, hidraw will be expanded to add support for these | ||
| 26 | new bus types. | ||
| 27 | |||
| 28 | Hidraw uses a dynamic major number, meaning that udev should be relied on to | ||
| 29 | create hidraw device nodes. Udev will typically create the device nodes | ||
| 30 | directly under /dev (eg: /dev/hidraw0). As this location is distribution- | ||
| 31 | and udev rule-dependent, applications should use libudev to locate hidraw | ||
| 32 | devices attached to the system. There is a tutorial on libudev with a | ||
| 33 | working example at: | ||
| 34 | http://www.signal11.us/oss/udev/ | ||
| 35 | |||
| 36 | The HIDRAW API | ||
| 37 | --------------- | ||
| 38 | |||
| 39 | read() | ||
| 40 | ------- | ||
| 41 | read() will read a queued report received from the HID device. On USB | ||
| 42 | devices, the reports read using read() are the reports sent from the device | ||
| 43 | on the INTERRUPT IN endpoint. By default, read() will block until there is | ||
| 44 | a report available to be read. read() can be made non-blocking, by passing | ||
| 45 | the O_NONBLOCK flag to open(), or by setting the O_NONBLOCK flag using | ||
| 46 | fcntl(). | ||
| 47 | |||
| 48 | On a device which uses numbered reports, the first byte of the returned data | ||
| 49 | will be the report number; the report data follows, beginning in the second | ||
| 50 | byte. For devices which do not use numbered reports, the report data | ||
| 51 | will begin at the first byte. | ||
| 52 | |||
| 53 | write() | ||
| 54 | -------- | ||
| 55 | The write() function will write a report to the device. For USB devices, if | ||
| 56 | the device has an INTERRUPT OUT endpoint, the report will be sent on that | ||
| 57 | endpoint. If it does not, the report will be sent over the control endpoint, | ||
| 58 | using a SET_REPORT transfer. | ||
| 59 | |||
| 60 | The first byte of the buffer passed to write() should be set to the report | ||
| 61 | number. If the device does not use numbered reports, the first byte should | ||
| 62 | be set to 0. The report data itself should begin at the second byte. | ||
| 63 | |||
| 64 | ioctl() | ||
| 65 | -------- | ||
| 66 | Hidraw supports the following ioctls: | ||
| 67 | |||
| 68 | HIDIOCGRDESCSIZE: Get Report Descriptor Size | ||
| 69 | This ioctl will get the size of the device's report descriptor. | ||
| 70 | |||
| 71 | HIDIOCGRDESC: Get Report Descriptor | ||
| 72 | This ioctl returns the device's report descriptor using a | ||
| 73 | hidraw_report_descriptor struct. Make sure to set the size field of the | ||
| 74 | hidraw_report_descriptor struct to the size returned from HIDIOCGRDESCSIZE. | ||
| 75 | |||
| 76 | HIDIOCGRAWINFO: Get Raw Info | ||
| 77 | This ioctl will return a hidraw_devinfo struct containing the bus type, the | ||
| 78 | vendor ID (VID), and product ID (PID) of the device. The bus type can be one | ||
| 79 | of: | ||
| 80 | BUS_USB | ||
| 81 | BUS_HIL | ||
| 82 | BUS_BLUETOOTH | ||
| 83 | BUS_VIRTUAL | ||
| 84 | which are defined in linux/input.h. | ||
| 85 | |||
| 86 | HIDIOCGRAWNAME(len): Get Raw Name | ||
| 87 | This ioctl returns a string containing the vendor and product strings of | ||
| 88 | the device. The returned string is Unicode, UTF-8 encoded. | ||
| 89 | |||
| 90 | HIDIOCGRAWPHYS(len): Get Physical Address | ||
| 91 | This ioctl returns a string representing the physical address of the device. | ||
| 92 | For USB devices, the string contains the physical path to the device (the | ||
| 93 | USB controller, hubs, ports, etc). For Bluetooth devices, the string | ||
| 94 | contains the hardware (MAC) address of the device. | ||
| 95 | |||
| 96 | HIDIOCSFEATURE(len): Send a Feature Report | ||
| 97 | This ioctl will send a feature report to the device. Per the HID | ||
| 98 | specification, feature reports are always sent using the control endpoint. | ||
| 99 | Set the first byte of the supplied buffer to the report number. For devices | ||
| 100 | which do not use numbered reports, set the first byte to 0. The report data | ||
| 101 | begins in the second byte. Make sure to set len accordingly, to one more | ||
| 102 | than the length of the report (to account for the report number). | ||
| 103 | |||
| 104 | HIDIOCGFEATURE(len): Get a Feature Report | ||
| 105 | This ioctl will request a feature report from the device using the control | ||
| 106 | endpoint. The first byte of the supplied buffer should be set to the report | ||
| 107 | number of the requested report. For devices which do not use numbered | ||
| 108 | reports, set the first byte to 0. The report will be returned starting at | ||
| 109 | the first byte of the buffer (ie: the report number is not returned). | ||
| 110 | |||
| 111 | Example | ||
| 112 | --------- | ||
| 113 | In samples/, find hid-example.c, which shows examples of read(), write(), | ||
| 114 | and all the ioctls for hidraw. The code may be used by anyone for any | ||
| 115 | purpose, and can serve as a starting point for developing applications using | ||
| 116 | hidraw. | ||
| 117 | |||
| 118 | Document by: | ||
| 119 | Alan Ott <alan@signal11.us>, Signal 11 Software | ||
diff --git a/samples/Kconfig b/samples/Kconfig index e03cf0e374d7..52f4264b3006 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 | ||
| 64 | config 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 | |||
| 64 | endif # SAMPLES | 70 | endif # SAMPLES |
diff --git a/samples/Makefile b/samples/Makefile index f26c0959fd86..6280817c2b7e 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 | ||
| 3 | obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \ | 3 | obj-$(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 000000000000..7811cb0289aa --- /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. | ||
| 2 | obj- := dummy.o | ||
| 3 | |||
| 4 | # List of programs to build | ||
| 5 | hostprogs-y := hid-example | ||
| 6 | |||
| 7 | # Tell kbuild to always build the programs | ||
| 8 | always := $(hostprogs-y) | ||
diff --git a/samples/hidraw/hid-example.c b/samples/hidraw/hid-example.c new file mode 100644 index 000000000000..40e3d6200582 --- /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 | |||
| 30 | const char *bus_str(int bus); | ||
| 31 | |||
| 32 | int 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 | |||
| 147 | const char * | ||
| 148 | bus_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 | } | ||
