usbhid.c revision 1.2 1 1.2 augustss /* $NetBSD: usbhid.c,v 1.2 1998/07/13 20:44:04 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * Author: Lennart Augustsson
8 1.1 augustss *
9 1.1 augustss * Redistribution and use in source and binary forms, with or without
10 1.1 augustss * modification, are permitted provided that the following conditions
11 1.1 augustss * are met:
12 1.1 augustss * 1. Redistributions of source code must retain the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer.
14 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer in the
16 1.1 augustss * documentation and/or other materials provided with the distribution.
17 1.1 augustss * 3. All advertising materials mentioning features or use of this software
18 1.1 augustss * must display the following acknowledgement:
19 1.1 augustss * This product includes software developed by the NetBSD
20 1.1 augustss * Foundation, Inc. and its contributors.
21 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 augustss * contributors may be used to endorse or promote products derived
23 1.1 augustss * from this software without specific prior written permission.
24 1.1 augustss *
25 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
36 1.1 augustss */
37 1.1 augustss
38 1.1 augustss #include <stdio.h>
39 1.1 augustss #include <stdlib.h>
40 1.1 augustss #include <sys/types.h>
41 1.1 augustss #include <fcntl.h>
42 1.1 augustss #include <unistd.h>
43 1.1 augustss #include <err.h>
44 1.1 augustss #include <dev/usb/usb.h>
45 1.1 augustss #include <dev/usb/usbhid.h>
46 1.1 augustss
47 1.1 augustss #include "hidsubr.h"
48 1.1 augustss
49 1.1 augustss #define HIDTABLE "/usr/share/misc/usb_hid_usages"
50 1.1 augustss
51 1.1 augustss #define USBDEV "/dev/uhid0"
52 1.1 augustss
53 1.1 augustss int verbose = 0;
54 1.1 augustss
55 1.1 augustss void prbits(int bits, char **strs, int n);
56 1.1 augustss void usage(void);
57 1.1 augustss void dumpitems(u_char *buf, int len);
58 1.1 augustss void rev(struct hid_item **p);
59 1.1 augustss u_long getdata(u_char *buf, int hpos, int hsize, int sign);
60 1.1 augustss void prdata(u_char *buf, struct hid_item *h);
61 1.1 augustss void dumpdata(int f, u_char *buf, int len, int loop);
62 1.1 augustss
63 1.1 augustss void
64 1.1 augustss prbits(int bits, char **strs, int n)
65 1.1 augustss {
66 1.1 augustss int i;
67 1.1 augustss
68 1.1 augustss for(i = 0; i < n; i++, bits >>= 1)
69 1.1 augustss if (strs[i*2])
70 1.1 augustss printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
71 1.1 augustss }
72 1.1 augustss
73 1.1 augustss void
74 1.1 augustss usage(void)
75 1.1 augustss {
76 1.1 augustss extern char *__progname;
77 1.1 augustss
78 1.1 augustss fprintf(stderr, "Usage: %s -f device [-l] [-n] [-r] [-t tablefile] [-v]\n", __progname);
79 1.1 augustss exit(1);
80 1.1 augustss }
81 1.1 augustss
82 1.1 augustss void
83 1.1 augustss dumpitems(u_char *buf, int len)
84 1.1 augustss {
85 1.1 augustss struct hid_data *d;
86 1.1 augustss struct hid_item h;
87 1.1 augustss
88 1.1 augustss for (d = hid_start_parse(buf, len, ~0); hid_get_item(d, &h); ) {
89 1.1 augustss switch (h.kind) {
90 1.1 augustss case hid_collection:
91 1.1 augustss printf("Collection page=%s usage=%s\n",
92 1.1 augustss usage_page(HID_PAGE(h.usage)),
93 1.1 augustss usage_in_page(h.usage));
94 1.1 augustss break;
95 1.1 augustss case hid_endcollection:
96 1.1 augustss printf("End collection\n");
97 1.1 augustss break;
98 1.1 augustss case hid_input:
99 1.1 augustss printf("Input size=%d count=%d page=%s usage=%s%s\n",
100 1.1 augustss h.report_size, h.report_count,
101 1.1 augustss usage_page(HID_PAGE(h.usage)),
102 1.1 augustss usage_in_page(h.usage),
103 1.1 augustss h.flags & HIO_CONST ? " Const" : "");
104 1.1 augustss break;
105 1.1 augustss case hid_output:
106 1.1 augustss printf("Output size=%d count=%d page=%s usage=%s%s\n",
107 1.1 augustss h.report_size, h.report_count,
108 1.1 augustss usage_page(HID_PAGE(h.usage)),
109 1.1 augustss usage_in_page(h.usage),
110 1.1 augustss h.flags & HIO_CONST ? " Const" : "");
111 1.1 augustss break;
112 1.1 augustss case hid_feature:
113 1.1 augustss printf("Feature size=%d count=%d page=%s usage=%s%s\n",
114 1.1 augustss h.report_size, h.report_count,
115 1.1 augustss usage_page(HID_PAGE(h.usage)),
116 1.1 augustss usage_in_page(h.usage),
117 1.1 augustss h.flags & HIO_CONST ? " Const" : "");
118 1.1 augustss break;
119 1.1 augustss }
120 1.1 augustss }
121 1.1 augustss hid_end_parse(d);
122 1.1 augustss printf("Total input size %d bytes\n",
123 1.1 augustss hid_report_size(buf, len, hid_input));
124 1.1 augustss printf("Total output size %d bytes\n",
125 1.1 augustss hid_report_size(buf, len, hid_output));
126 1.1 augustss printf("Total feature size %d bytes\n",
127 1.1 augustss hid_report_size(buf, len, hid_feature));
128 1.1 augustss }
129 1.1 augustss
130 1.1 augustss void
131 1.1 augustss rev(struct hid_item **p)
132 1.1 augustss {
133 1.1 augustss struct hid_item *cur, *prev, *next;
134 1.1 augustss
135 1.1 augustss prev = 0;
136 1.1 augustss cur = *p;
137 1.1 augustss while(cur != 0) {
138 1.1 augustss next = cur->next;
139 1.1 augustss cur->next = prev;
140 1.1 augustss prev = cur;
141 1.1 augustss cur = next;
142 1.1 augustss }
143 1.1 augustss *p = prev;
144 1.1 augustss }
145 1.1 augustss
146 1.1 augustss u_long
147 1.1 augustss getdata(u_char *buf, int hpos, int hsize, int sign)
148 1.1 augustss {
149 1.1 augustss u_long data;
150 1.1 augustss int i, size, s;
151 1.1 augustss
152 1.1 augustss data = 0;
153 1.1 augustss s = hpos/8;
154 1.2 augustss for (i = hpos; i < hpos+hsize; i += 8)
155 1.2 augustss data |= buf[i / 8] << ((i/8-s) * 8);
156 1.1 augustss data >>= (hpos % 8);
157 1.1 augustss data &= (1 << hsize) - 1;
158 1.1 augustss size = 32 - hsize;
159 1.2 augustss if (sign)
160 1.1 augustss /* Need to sign extend */
161 1.1 augustss data = ((long)data << size) >> size;
162 1.1 augustss return data;
163 1.1 augustss }
164 1.1 augustss
165 1.1 augustss void
166 1.1 augustss prdata(u_char *buf, struct hid_item *h)
167 1.1 augustss {
168 1.1 augustss u_long data;
169 1.1 augustss int i, pos;
170 1.1 augustss
171 1.1 augustss pos = h->pos;
172 1.1 augustss for (i = 0; i < h->report_count; i++) {
173 1.1 augustss data = getdata(buf, pos, h->report_size,
174 1.1 augustss h->logical_minimum < 0);
175 1.1 augustss if (h->logical_minimum < 0)
176 1.1 augustss printf("%ld", (long)data);
177 1.1 augustss else
178 1.1 augustss printf("%lu", data);
179 1.1 augustss pos += h->report_size;
180 1.1 augustss }
181 1.1 augustss }
182 1.1 augustss
183 1.1 augustss void
184 1.1 augustss dumpdata(int f, u_char *buf, int len, int loop)
185 1.1 augustss {
186 1.1 augustss struct hid_data *d;
187 1.1 augustss struct hid_item h, *hids, *n;
188 1.1 augustss int r, dlen;
189 1.1 augustss u_char *dbuf;
190 1.1 augustss static int one = 1;
191 1.1 augustss u_int32_t colls[100];
192 1.1 augustss int sp = 0;
193 1.1 augustss
194 1.1 augustss hids = 0;
195 1.1 augustss for (d = hid_start_parse(buf, len, 1<<hid_input);
196 1.1 augustss hid_get_item(d, &h); ) {
197 1.1 augustss if (h.kind == hid_collection)
198 1.1 augustss colls[++sp] = h.usage;
199 1.1 augustss else if (h.kind == hid_endcollection)
200 1.1 augustss --sp;
201 1.1 augustss if (h.kind != hid_input || (h.flags & HIO_CONST))
202 1.1 augustss continue;
203 1.1 augustss h.next = hids;
204 1.1 augustss h.collection = colls[sp];
205 1.1 augustss hids = malloc(sizeof *hids);
206 1.1 augustss *hids = h;
207 1.1 augustss }
208 1.1 augustss hid_end_parse(d);
209 1.1 augustss rev(&hids);
210 1.1 augustss dlen = hid_report_size(buf, len, hid_input);
211 1.1 augustss dbuf = malloc(dlen);
212 1.1 augustss if (!loop)
213 1.1 augustss if (ioctl(f, USB_SET_IMMED, &one) < 0)
214 1.1 augustss err(1, "USB_SET_IMMED");
215 1.1 augustss do {
216 1.1 augustss r = read(f, dbuf, dlen);
217 1.1 augustss if (r != dlen) {
218 1.1 augustss err(1, "bad read %d != %d", r, dlen);
219 1.1 augustss }
220 1.1 augustss for (n = hids; n; n = n->next) {
221 1.2 augustss printf("%s:%s.",
222 1.1 augustss usage_page(HID_PAGE(n->collection)),
223 1.1 augustss usage_in_page(n->collection));
224 1.2 augustss printf("%s:%s=",
225 1.1 augustss usage_page(HID_PAGE(n->usage)),
226 1.1 augustss usage_in_page(n->usage));
227 1.1 augustss prdata(dbuf, n);
228 1.1 augustss if (verbose)
229 1.1 augustss printf(" [%d - %d]",
230 1.1 augustss n->logical_minimum, n->logical_maximum);
231 1.1 augustss printf("\n");
232 1.1 augustss }
233 1.1 augustss if (loop)
234 1.1 augustss printf("\n");
235 1.1 augustss } while (loop);
236 1.1 augustss free(dbuf);
237 1.1 augustss }
238 1.1 augustss
239 1.1 augustss int
240 1.1 augustss main(int argc, char **argv)
241 1.1 augustss {
242 1.1 augustss int f, r;
243 1.1 augustss char *dev = 0;
244 1.1 augustss int ch;
245 1.1 augustss extern char *optarg;
246 1.1 augustss extern int optind;
247 1.1 augustss struct usb_ctl_report_desc rep;
248 1.1 augustss int repdump = 0;
249 1.1 augustss int loop = 0;
250 1.1 augustss int nothing = 0;
251 1.1 augustss char *table = HIDTABLE;
252 1.1 augustss
253 1.1 augustss while ((ch = getopt(argc, argv, "f:lnrt:v")) != -1) {
254 1.1 augustss switch(ch) {
255 1.1 augustss case 'f':
256 1.1 augustss dev = optarg;
257 1.1 augustss break;
258 1.1 augustss case 'l':
259 1.1 augustss loop ^= 1;
260 1.1 augustss break;
261 1.1 augustss case 'n':
262 1.1 augustss nothing++;
263 1.1 augustss break;
264 1.1 augustss case 'r':
265 1.1 augustss repdump++;
266 1.1 augustss break;
267 1.1 augustss case 't':
268 1.1 augustss table = optarg;
269 1.1 augustss break;
270 1.1 augustss case 'v':
271 1.1 augustss verbose++;
272 1.1 augustss break;
273 1.1 augustss case '?':
274 1.1 augustss default:
275 1.1 augustss usage();
276 1.1 augustss }
277 1.1 augustss }
278 1.1 augustss argc -= optind;
279 1.1 augustss argv += optind;
280 1.1 augustss if (argc != 0 || dev == 0)
281 1.1 augustss usage();
282 1.1 augustss
283 1.1 augustss init_hid(table);
284 1.1 augustss
285 1.1 augustss f = open(dev, O_RDWR);
286 1.1 augustss if (f < 0)
287 1.1 augustss err(1, "%s", dev);
288 1.1 augustss
289 1.1 augustss rep.size = 0;
290 1.1 augustss r = ioctl(f, USB_GET_REPORT_DESC, &rep);
291 1.1 augustss if (r)
292 1.1 augustss errx(1, "USB_GET_REPORT_DESC");
293 1.1 augustss
294 1.1 augustss if (repdump) {
295 1.1 augustss printf("Report descriptor\n");
296 1.1 augustss dumpitems(rep.data, rep.size);
297 1.1 augustss }
298 1.1 augustss if (!nothing)
299 1.1 augustss dumpdata(f, rep.data, rep.size, loop);
300 1.1 augustss
301 1.1 augustss exit(0);
302 1.1 augustss }
303