]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/usbhidctl/usbhid.c
MFC r235519:
[FreeBSD/stable/8.git] / usr.bin / usbhidctl / usbhid.c
1 /*      $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
2 /*      $FreeBSD$ */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@netbsd.org).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <err.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <usbhid.h>
50 #include <dev/usb/usbhid.h>
51
52 struct variable {
53         char *name;
54         int instance;
55         int val;
56         struct hid_item h;
57         struct variable *next;
58 } *vars;
59
60 int verbose = 0;
61 int noname = 0;
62 int hexdump = 0;
63 int wflag = 0;
64 int zflag = 0;
65
66 static void usage(void);
67 static void dumpitem(const char *label, struct hid_item *h);
68 static void dumpitems(report_desc_t r);
69 static void prdata(u_char *buf, struct hid_item *h);
70 static void dumpdata(int f, report_desc_t r, int loop);
71 static void writedata(int f, report_desc_t r);
72
73 static void
74 parceargs(report_desc_t r, int all, int nnames, char **names)
75 {
76         struct hid_data *d;
77         struct hid_item h;
78         char colls[1000];
79         char hname[1000], *tmp1, *tmp2;
80         struct variable *var, **pnext;
81         int i, instance, cp, t;
82
83         pnext = &vars;
84         if (all) {
85                 if (wflag)
86                         errx(1, "Must not specify -w to read variables");
87                 cp = 0;
88                 for (d = hid_start_parse(r,
89                     1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
90                     hid_get_item(d, &h); ) {
91                         if (h.kind == hid_collection) {
92                                 cp += sprintf(&colls[cp], "%s%s:%s",
93                                     cp != 0 ? "." : "",
94                                     hid_usage_page(HID_PAGE(h.usage)),
95                                     hid_usage_in_page(h.usage));
96                         } else if (h.kind == hid_endcollection) {
97                                 tmp1 = strrchr(colls, '.');
98                                 if (tmp1 != NULL) {
99                                         cp -= strlen(tmp1);
100                                         tmp1[0] = 0;
101                                 } else {
102                                         cp = 0;
103                                         colls[0] = 0;
104                                 }
105                         }
106                         if ((h.kind != hid_input && h.kind != hid_output &&
107                             h.kind != hid_feature) || (h.flags & HIO_CONST))
108                                 continue;
109                         var = malloc(sizeof(*var));
110                         memset(var, 0, sizeof(*var));
111                         asprintf(&var->name, "%s%s%s:%s",
112                             colls, colls[0] != 0 ? "." : "",
113                             hid_usage_page(HID_PAGE(h.usage)),
114                             hid_usage_in_page(h.usage));
115                         var->h = h;
116                         *pnext = var;
117                         pnext = &var->next;
118                 }
119                 hid_end_parse(d);
120                 return;
121         }
122         for (i = 0; i < nnames; i++) {
123                 var = malloc(sizeof(*var));
124                 memset(var, 0, sizeof(*var));
125                 tmp1 = tmp2 = strdup(names[i]);
126                 strsep(&tmp2, "=");
127                 var->name = strsep(&tmp1, "#");
128                 if (tmp1 != NULL)
129                         var->instance = atoi(tmp1);
130                 if (tmp2 != NULL) {
131                         if (!wflag)
132                                 errx(1, "Must specify -w to write variables");
133                         var->val = atoi(tmp2);
134                 } else
135                         if (wflag)
136                                 errx(1, "Must not specify -w to read variables");
137                 *pnext = var;
138                 pnext = &var->next;
139
140                 instance = 0;
141                 cp = 0;
142                 for (d = hid_start_parse(r,
143                     1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
144                     hid_get_item(d, &h); ) {
145                         if (h.kind == hid_collection) {
146                                 cp += sprintf(&colls[cp], "%s%s:%s",
147                                     cp != 0 ? "." : "",
148                                     hid_usage_page(HID_PAGE(h.usage)),
149                                     hid_usage_in_page(h.usage));
150                         } else if (h.kind == hid_endcollection) {
151                                 tmp1 = strrchr(colls, '.');
152                                 if (tmp1 != NULL) {
153                                         cp -= strlen(tmp1);
154                                         tmp1[0] = 0;
155                                 } else {
156                                         cp = 0;
157                                         colls[0] = 0;
158                                 }
159                         }
160                         if ((h.kind != hid_input && h.kind != hid_output &&
161                             h.kind != hid_feature) || (h.flags & HIO_CONST))
162                                 continue;
163                         snprintf(hname, sizeof(hname), "%s%s%s:%s",
164                             colls, colls[0] != 0 ? "." : "",
165                             hid_usage_page(HID_PAGE(h.usage)),
166                             hid_usage_in_page(h.usage));
167                         t = strlen(hname) - strlen(var->name);
168                         if (t > 0) {
169                                 if (strcmp(hname + t, var->name) != 0)
170                                         continue;
171                                 if (hname[t - 1] != '.')
172                                         continue;
173                         } else if (strcmp(hname, var->name) != 0)
174                                 continue;
175                         if (var->instance != instance++)
176                                 continue;
177                         var->h = h;
178                         break;
179                 }
180                 hid_end_parse(d);
181                 if (var->h.usage == 0)
182                         errx(1, "Unknown item '%s'", var->name);
183         }
184 }
185
186 static void
187 usage(void)
188 {
189
190         fprintf(stderr,
191                 "usage: %s -f device "
192                 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] name ...\n",
193                 getprogname());
194         fprintf(stderr,
195                 "       %s -f device "
196                 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] -a\n",
197                 getprogname());
198         fprintf(stderr,
199                 "       %s -f device "
200                 "[-t tablefile] [-v] [-z] -w name=value\n",
201                 getprogname());
202         exit(1);
203 }
204
205 static void
206 dumpitem(const char *label, struct hid_item *h)
207 {
208         if ((h->flags & HIO_CONST) && !verbose)
209                 return;
210         printf("%s rid=%d size=%d count=%d page=%s usage=%s%s%s", label,
211                h->report_ID, h->report_size, h->report_count,
212                hid_usage_page(HID_PAGE(h->usage)),
213                hid_usage_in_page(h->usage),
214                h->flags & HIO_CONST ? " Const" : "",
215                h->flags & HIO_VARIABLE ? "" : " Array");
216         printf(", logical range %d..%d",
217                h->logical_minimum, h->logical_maximum);
218         if (h->physical_minimum != h->physical_maximum)
219                 printf(", physical range %d..%d",
220                        h->physical_minimum, h->physical_maximum);
221         if (h->unit)
222                 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
223         printf("\n");
224 }
225
226 static const char *
227 hid_collection_type(int32_t type)
228 {
229         static char num[8];
230
231         switch (type) {
232         case 0: return ("Physical");
233         case 1: return ("Application");
234         case 2: return ("Logical");
235         case 3: return ("Report");
236         case 4: return ("Named_Array");
237         case 5: return ("Usage_Switch");
238         case 6: return ("Usage_Modifier");
239         }
240         snprintf(num, sizeof(num), "0x%02x", type);
241         return (num);
242 }
243
244 static void
245 dumpitems(report_desc_t r)
246 {
247         struct hid_data *d;
248         struct hid_item h;
249         int size;
250
251         for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
252                 switch (h.kind) {
253                 case hid_collection:
254                         printf("Collection type=%s page=%s usage=%s\n",
255                                hid_collection_type(h.collection),
256                                hid_usage_page(HID_PAGE(h.usage)),
257                                hid_usage_in_page(h.usage));
258                         break;
259                 case hid_endcollection:
260                         printf("End collection\n");
261                         break;
262                 case hid_input:
263                         dumpitem("Input  ", &h);
264                         break;
265                 case hid_output:
266                         dumpitem("Output ", &h);
267                         break;
268                 case hid_feature:
269                         dumpitem("Feature", &h);
270                         break;
271                 }
272         }
273         hid_end_parse(d);
274         size = hid_report_size(r, hid_input, -1);
275         printf("Total   input size %d bytes\n", size);
276
277         size = hid_report_size(r, hid_output, -1);
278         printf("Total  output size %d bytes\n", size);
279
280         size = hid_report_size(r, hid_feature, -1);
281         printf("Total feature size %d bytes\n", size);
282 }
283
284 static void
285 prdata(u_char *buf, struct hid_item *h)
286 {
287         u_int data;
288         int i, pos;
289
290         pos = h->pos;
291         for (i = 0; i < h->report_count; i++) {
292                 data = hid_get_data(buf, h);
293                 if (i > 0)
294                         printf(" ");
295                 if (h->logical_minimum < 0)
296                         printf("%d", (int)data);
297                 else
298                         printf("%u", data);
299                 if (hexdump)
300                         printf(" [0x%x]", data);
301                 h->pos += h->report_size;
302         }
303         h->pos = pos;
304 }
305
306 static void
307 dumpdata(int f, report_desc_t rd, int loop)
308 {
309         struct variable *var;
310         int dlen, havedata, i, match, r, rid, use_rid;
311         u_char *dbuf;
312         enum hid_kind kind;
313
314         kind = zflag ? 3 : 0;
315         rid = -1;
316         use_rid = !!hid_get_report_id(f);
317         do {
318                 if (kind < 3) {
319                         if (++rid >= 256) {
320                                 rid = 0;
321                                 kind++;
322                         }
323                         if (kind >= 3)
324                                 rid = -1;
325                         for (var = vars; var; var = var->next) {
326                                 if (rid == var->h.report_ID &&
327                                     kind == var->h.kind)
328                                         break;
329                         }
330                         if (var == NULL)
331                                 continue;
332                 }
333                 dlen = hid_report_size(rd, kind < 3 ? kind : hid_input, rid);
334                 if (dlen <= 0)
335                         continue;
336                 dbuf = malloc(dlen);
337                 memset(dbuf, 0, dlen);
338                 if (kind < 3) {
339                         dbuf[0] = rid;
340                         r = hid_get_report(f, kind, dbuf, dlen);
341                         if (r < 0)
342                                 warn("hid_get_report(rid %d)", rid);
343                         havedata = !r && (rid == 0 || dbuf[0] == rid);
344                         if (rid != 0)
345                                 dbuf[0] = rid;
346                 } else {
347                         r = read(f, dbuf, dlen);
348                         if (r < 1)
349                                 err(1, "read error");
350                         havedata = 1;
351                 }
352                 if (verbose) {
353                         printf("Got %s report %d (%d bytes):",
354                             kind == hid_output ? "output" :
355                             kind == hid_feature ? "feature" : "input",
356                             use_rid ? dbuf[0] : 0, dlen);
357                         if (havedata) {
358                                 for (i = 0; i < dlen; i++)
359                                         printf(" %02x", dbuf[i]);
360                         }
361                         printf("\n");
362                 }
363                 match = 0;
364                 for (var = vars; var; var = var->next) {
365                         if ((kind < 3 ? kind : hid_input) != var->h.kind)
366                                 continue;
367                         if (var->h.report_ID != 0 &&
368                             dbuf[0] != var->h.report_ID)
369                                 continue;
370                         match = 1;
371                         if (!noname)
372                                 printf("%s=", var->name);
373                         if (havedata)
374                                 prdata(dbuf, &var->h);
375                         printf("\n");
376                 }
377                 if (match)
378                         printf("\n");
379                 free(dbuf);
380         } while (loop || kind < 3);
381 }
382
383 static void
384 writedata(int f, report_desc_t rd)
385 {
386         struct variable *var;
387         int dlen, i, r, rid;
388         u_char *dbuf;
389         enum hid_kind kind;
390
391         kind = 0;
392         rid = 0;
393         for (kind = 0; kind < 3; kind ++) {
394             for (rid = 0; rid < 256; rid ++) {
395                 for (var = vars; var; var = var->next) {
396                         if (rid == var->h.report_ID && kind == var->h.kind)
397                                 break;
398                 }
399                 if (var == NULL)
400                         continue;
401                 dlen = hid_report_size(rd, kind, rid);
402                 if (dlen <= 0)
403                         continue;
404                 dbuf = malloc(dlen);
405                 memset(dbuf, 0, dlen);
406                 dbuf[0] = rid;
407                 if (!zflag && hid_get_report(f, kind, dbuf, dlen) == 0) {
408                         if (verbose) {
409                                 printf("Got %s report %d (%d bytes):",
410                                     kind == hid_input ? "input" :
411                                     kind == hid_output ? "output" : "feature",
412                                     rid, dlen);
413                                 for (i = 0; i < dlen; i++)
414                                         printf(" %02x", dbuf[i]);
415                                 printf("\n");
416                         }
417                 } else if (!zflag) {
418                         warn("hid_get_report(rid %d)", rid);
419                         if (verbose) {
420                                 printf("Can't get %s report %d (%d bytes). "
421                                     "Will be initialized with zeros.\n",
422                                     kind == hid_input ? "input" :
423                                     kind == hid_output ? "output" : "feature",
424                                     rid, dlen);
425                         }
426                 }
427                 for (var = vars; var; var = var->next) {
428                         if (rid != var->h.report_ID || kind != var->h.kind)
429                                 continue;
430                         hid_set_data(dbuf, &var->h, var->val);
431                 }
432                 if (verbose) {
433                         printf("Setting %s report %d (%d bytes):",
434                             kind == hid_output ? "output" :
435                             kind == hid_feature ? "feature" : "input",
436                             rid, dlen);
437                         for (i = 0; i < dlen; i++)
438                                 printf(" %02x", dbuf[i]);
439                         printf("\n");
440                 }
441                 r = hid_set_report(f, kind, dbuf, dlen);
442                 if (r != 0)
443                         warn("hid_set_report(rid %d)", rid);
444                 free(dbuf);
445             }
446         }
447 }
448
449 int
450 main(int argc, char **argv)
451 {
452         report_desc_t r;
453         char *table = 0;
454         char devnam[100], *dev = NULL;
455         int f;
456         int all = 0;
457         int ch;
458         int repdump = 0;
459         int loop = 0;
460
461         while ((ch = getopt(argc, argv, "af:lnrt:vwxz")) != -1) {
462                 switch(ch) {
463                 case 'a':
464                         all++;
465                         break;
466                 case 'f':
467                         dev = optarg;
468                         break;
469                 case 'l':
470                         loop ^= 1;
471                         break;
472                 case 'n':
473                         noname++;
474                         break;
475                 case 'r':
476                         repdump++;
477                         break;
478                 case 't':
479                         table = optarg;
480                         break;
481                 case 'v':
482                         verbose++;
483                         break;
484                 case 'w':
485                         wflag = 1;
486                         break;
487                 case 'x':
488                         hexdump = 1;
489                         break;
490                 case 'z':
491                         zflag = 1;
492                         break;
493                 case '?':
494                 default:
495                         usage();
496                 }
497         }
498         argc -= optind;
499         argv += optind;
500         if (dev == NULL)
501                 usage();
502
503         if (argc == 0 && !all && !repdump)
504                 usage();
505
506         if (dev[0] != '/') {
507                 if (isdigit(dev[0]))
508                         snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
509                 else
510                         snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
511                 dev = devnam;
512         }
513
514         hid_init(table);
515
516         f = open(dev, O_RDWR);
517         if (f < 0)
518                 err(1, "%s", dev);
519
520         r = hid_get_report_desc(f);
521         if (r == 0)
522                 errx(1, "USB_GET_REPORT_DESC");
523
524         if (repdump) {
525                 printf("Report descriptor:\n");
526                 dumpitems(r);
527         }
528         if (argc != 0 || all) {
529                 parceargs(r, all, argc, argv);
530                 if (wflag)
531                         writedata(f, r);
532                 else
533                         dumpdata(f, r, loop);
534         }
535
536         hid_dispose_report_desc(r);
537         exit(0);
538 }