blob: f52167ecc4a9e564e2844c814fadd80e38793591 [file] [log] [blame]
Andrew Duggan052556f2014-04-16 11:32:30 -07001/*
2 * Copyright (C) 2013 - 2014 Andrew Duggan
3 * Copyright (C) 2013 - 2014 Synaptics Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Andrew Duggan4e811252014-04-03 15:17:57 -070018#include <stdio.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/ioctl.h>
26#include <sys/select.h>
Andrew Duggan63078d52014-04-08 11:20:15 -070027#include <getopt.h>
Andrew Duggan4e811252014-04-03 15:17:57 -070028
29#include <linux/types.h>
30#include <linux/input.h>
31#include <linux/hidraw.h>
32#include <signal.h>
33#include <stdlib.h>
34
35#include "hiddevice.h"
36
Andrew Duggana0d74e02015-05-11 15:50:59 -070037#define RMI4UPDATE_GETOPTS "hp:ir:w:foambde"
Andrew Duggane9a5cd02014-04-29 13:34:42 -070038
39 enum rmihidtool_cmd {
40 RMIHIDTOOL_CMD_INTERACTIVE,
41 RMIHIDTOOL_CMD_READ,
42 RMIHIDTOOL_CMD_WRITE,
43 RMIHIDTOOL_CMD_FW_ID,
44 RMIHIDTOOL_CMD_PROPS,
45 RMIHIDTOOL_CMD_ATTN,
46 RMIHIDTOOL_CMD_PRINT_FUNCTIONS,
Andrew Duggan161c83c2015-05-06 18:06:03 -070047 RMIHIDTOOL_CMD_REBIND_DRIVER,
Andrew Duggan2c24adb2015-05-06 18:18:06 -070048 RMIHIDTOOL_CMD_PRINT_DEVICE_INFO,
Andrew Duggana0d74e02015-05-11 15:50:59 -070049 RMIHIDTOOL_CMD_RESET_DEVICE,
Andrew Duggane9a5cd02014-04-29 13:34:42 -070050};
Andrew Duggan63078d52014-04-08 11:20:15 -070051
Andrew Duggan4e811252014-04-03 15:17:57 -070052static int report_attn = 0;
Andrew Duggan63078d52014-04-08 11:20:15 -070053static RMIDevice * g_device = NULL;
54
Andrew Duggane9a5cd02014-04-29 13:34:42 -070055void print_help(const char *prog_name)
Andrew Duggan63078d52014-04-08 11:20:15 -070056{
Andrew Duggane9a5cd02014-04-29 13:34:42 -070057 fprintf(stdout, "Usage: %s [OPTIONS] DEVICEFILE\n", prog_name);
58 fprintf(stdout, "\t-h, --help\t\t\t\tPrint this message\n");
59 fprintf(stdout, "\t-p, --protocol [protocol]\t\tSet which transport prototocl to use.\n");
60 fprintf(stdout, "\t-i, --interactive\t\t\tRun in interactive mode.\n");
61 fprintf(stdout, "\t-r, --read [address] [length]\t\tRead registers starting at the address.\n");
62 fprintf(stdout, "\t-r, --write [address] [length] [data]\tWrite registers starting at the address.\n");
63 fprintf(stdout, "\t-f, --firmware-id\t\t\tPrint the firmware id\n");
64 fprintf(stdout, "\t-o, --props\t\t\t\tPrint device properties\n");
65 fprintf(stdout, "\t-a, --attention\t\t\t\tPrint attention reports until control + c\n");
66 fprintf(stdout, "\t-m, --print-functions\t\t\tPrint RMI4 functions for the device.\n");
Andrew Duggan161c83c2015-05-06 18:06:03 -070067 fprintf(stdout, "\t-b, --rebind-driver\t\t\tRebind the driver to force an update of device properties.\n");
Andrew Duggan2c24adb2015-05-06 18:18:06 -070068 fprintf(stdout, "\t-d, --device-info\t\t\tPrint protocol specific information about the device.\n");
Andrew Duggana0d74e02015-05-11 15:50:59 -070069 fprintf(stdout, "\t-e, --reset-device\t\t\tReset the device.\n");
Andrew Duggan63078d52014-04-08 11:20:15 -070070}
Andrew Duggan4e811252014-04-03 15:17:57 -070071
Andrew Duggane9a5cd02014-04-29 13:34:42 -070072void print_cmd_usage()
Andrew Duggan4e811252014-04-03 15:17:57 -070073{
74 fprintf(stdout, "Commands:\n");
75 fprintf(stdout, "s [0,1,2]: Set RMIMode\n");
76 fprintf(stdout, "r address size: read size bytes from address\n");
77 fprintf(stdout, "w address { values }: write bytes to address\n");
78 fprintf(stdout, "a: Wait for attention\n");
79 fprintf(stdout, "q: quit\n");
80}
81
82int find_token(char * input, char * result, char ** endpp)
83{
84 int i = 0;
85 char * start = input;
86 char * end;
87
88 while (input[i] == ' ') {
89 ++start;
90 ++i;
91 }
92
93 while (input[i] != '\0') {
Andrew Duggan849fea32014-11-13 21:15:48 -080094 if (input[++i] == ' ')
Andrew Duggan4e811252014-04-03 15:17:57 -070095 break;
Andrew Duggan4e811252014-04-03 15:17:57 -070096 }
97 end = &input[i];
98
Andrew Duggane9a5cd02014-04-29 13:34:42 -070099 if (start == end)
Andrew Duggan4e811252014-04-03 15:17:57 -0700100 return 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700101
102 *endpp = end;
103 strncpy(result, start, end - start);
Andrew Duggan849fea32014-11-13 21:15:48 -0800104 result[end - start] = '\0';
Andrew Duggan4e811252014-04-03 15:17:57 -0700105
106 return 1;
107}
108
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700109void interactive(RMIDevice * device, unsigned char *report)
Andrew Duggan4e811252014-04-03 15:17:57 -0700110{
Andrew Duggan4e811252014-04-03 15:17:57 -0700111 char token[256];
112 char * start;
113 char * end;
114 int rc;
115
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700116 for (;;) {
Andrew Duggan4e811252014-04-03 15:17:57 -0700117 fprintf(stdout, "\n");
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700118 print_cmd_usage();
119 char input[256];
Andrew Duggan4e811252014-04-03 15:17:57 -0700120
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700121 if (fgets(input, 256, stdin)) {
122 memset(token, 0, 256);
Andrew Duggan4e811252014-04-03 15:17:57 -0700123
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700124 if (input[0] == 's') {
125 start = input + 2;
126 find_token(start, token, &end);
127 int mode = strtol(token, NULL, 0);
128 if (mode >= 0 && mode <= 2) {
129 if (device->SetMode(mode)) {
130 fprintf(stderr, "Set RMI Mode to: %d\n", mode);
131 } else {
132 fprintf(stderr, "Set RMI Mode FAILED!\n");
133 continue;
134 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700135 }
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700136 } else if (input[0] == 'r') {
137 start = input + 2;
138 find_token(start, token, &end);
139 start = end + 1;
140 unsigned int addr = strtol(token, NULL, 0);
141 find_token(start, token, &end);
142 start = end + 1;
143 unsigned int len = strtol(token, NULL, 0);
144 fprintf(stdout, "Address = 0x%02x Length = %d\n", addr, len);
145
146 memset(report, 0, 256);
147 rc = device->Read(addr, report, len);
148 if (rc < 0)
149 fprintf(stderr, "Failed to read report: %d\n", rc);
150 print_buffer(report, len);
151 } else if (input[0] == 'w') {
152 int index = 0;
153 start = input + 2;
154 find_token(start, token, &end);
155 start = end + 1;
156 unsigned int addr = strtol(token, NULL, 0);
157 unsigned int len = 0;
158
159 memset(report, 0, 256);
160 while (find_token(start, token, &end)) {
161 start = end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800162 report[index++] = strtol(token, NULL, 0);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700163 ++len;
164 }
165
166 if (device->Write(addr, report, len) < 0) {
167 fprintf(stderr, "Failed to Write Report\n");
168 continue;
169 }
170 } else if (input[0] == 'a') {
171 unsigned int bytes = 256;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800172 device->GetAttentionReport(NULL,
173 RMI_INTERUPT_SOURCES_ALL_MASK,
174 report, &bytes);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700175 print_buffer(report, bytes);
176 } else if (input[0] == 'q') {
177 return;
178 } else {
179 print_cmd_usage();
Andrew Duggan4e811252014-04-03 15:17:57 -0700180 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700181 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700182 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700183}
184
185static void cleanup(int status)
186{
187 if (report_attn) {
188 report_attn = 0;
189 if (g_device)
190 g_device->Cancel();
191 } else {
192 exit(0);
193 }
194}
195
196int main(int argc, char ** argv)
197{
198 int rc;
199 struct sigaction sig_cleanup_action;
Andrew Duggan63078d52014-04-08 11:20:15 -0700200 int opt;
201 int index;
202 RMIDevice *device;
203 const char *protocol = "HID";
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700204 unsigned char report[256];
205 char token[256];
Andrew Duggan63078d52014-04-08 11:20:15 -0700206 static struct option long_options[] = {
207 {"help", 0, NULL, 'h'},
208 {"protocol", 1, NULL, 'p'},
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700209 {"interactive", 0, NULL, 'i'},
210 {"read", 1, NULL, 'r'},
211 {"write", 1, NULL, 'w'},
212 {"firmware-id", 0, NULL, 'f'},
213 {"props", 0, NULL, 'o'},
214 {"attention", 0, NULL, 'a'},
215 {"print-functions", 0, NULL, 'm'},
Andrew Duggan161c83c2015-05-06 18:06:03 -0700216 {"rebind-driver", 0, NULL, 'b'},
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700217 {"device-info", 0, NULL, 'd'},
Andrew Duggana0d74e02015-05-11 15:50:59 -0700218 {"reset-device", 0, NULL, 'e'},
Andrew Duggan63078d52014-04-08 11:20:15 -0700219 {0, 0, 0, 0},
220 };
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700221 enum rmihidtool_cmd cmd = RMIHIDTOOL_CMD_INTERACTIVE;
Andrew Duggan8b79f312014-05-16 13:26:09 -0700222 unsigned int addr = 0;
223 unsigned int len = 0;
224 char * data = NULL;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700225 char * start;
226 char * end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800227 int i = 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700228
229 memset(&sig_cleanup_action, 0, sizeof(struct sigaction));
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700230 sig_cleanup_action.sa_handler = cleanup;
231 sig_cleanup_action.sa_flags = SA_RESTART;
232 sigaction(SIGINT, &sig_cleanup_action, NULL);
Andrew Duggan4e811252014-04-03 15:17:57 -0700233
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700234 while ((opt = getopt_long(argc, argv, RMI4UPDATE_GETOPTS, long_options, &index)) != -1) {
235 switch (opt) {
236 case 'h':
237 print_help(argv[0]);
238 return 0;
239 case 'p':
240 protocol = optarg;
241 break;
242 case 'i':
243 cmd = RMIHIDTOOL_CMD_INTERACTIVE;
244 break;
245 case 'r':
246 cmd = RMIHIDTOOL_CMD_READ;
247 addr = strtol(optarg, NULL, 0);
248 len = strtol(argv[optind++], NULL, 0);
249 break;
250 case 'w':
251 cmd = RMIHIDTOOL_CMD_WRITE;
252 addr = strtol(optarg, NULL, 0);
253 data = argv[optind++];
254 break;
255 case 'f':
256 cmd = RMIHIDTOOL_CMD_FW_ID;
257 break;
258 case 'o':
259 cmd = RMIHIDTOOL_CMD_PROPS;
260 break;
261 case 'a':
262 cmd = RMIHIDTOOL_CMD_ATTN;
263 break;
264 case 'm':
265 cmd = RMIHIDTOOL_CMD_PRINT_FUNCTIONS;
266 break;
Andrew Duggan161c83c2015-05-06 18:06:03 -0700267 case 'b':
268 cmd = RMIHIDTOOL_CMD_REBIND_DRIVER;
269 break;
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700270 case 'd':
271 cmd = RMIHIDTOOL_CMD_PRINT_DEVICE_INFO;
272 break;
Andrew Duggana0d74e02015-05-11 15:50:59 -0700273 case 'e':
274 cmd = RMIHIDTOOL_CMD_RESET_DEVICE;
275 break;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700276 default:
277 print_help(argv[0]);
278 return 0;
279 break;
Andrew Duggan63078d52014-04-08 11:20:15 -0700280
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700281 }
282 }
Andrew Duggan63078d52014-04-08 11:20:15 -0700283
284 if (!strncasecmp("hid", protocol, 3)) {
285 device = new HIDDevice();
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700286 } else {
Andrew Duggan63078d52014-04-08 11:20:15 -0700287 fprintf(stderr, "Invalid Protocol: %s\n", protocol);
288 return -1;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700289 }
Andrew Duggan63078d52014-04-08 11:20:15 -0700290
Andrew Duggan4667eb72014-04-29 13:37:42 -0700291 if (optind >= argc) {
292 print_help(argv[0]);
293 return -1;
294 }
295
Andrew Duggan63078d52014-04-08 11:20:15 -0700296 rc = device->Open(argv[optind++]);
Andrew Duggan4e811252014-04-03 15:17:57 -0700297 if (rc) {
Andrew Duggan63078d52014-04-08 11:20:15 -0700298 fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno,
299 strerror(errno));
Andrew Duggan4e811252014-04-03 15:17:57 -0700300 return 1;
301 }
302
Andrew Duggan63078d52014-04-08 11:20:15 -0700303 g_device = device;
Andrew Duggan4e811252014-04-03 15:17:57 -0700304
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700305 switch (cmd) {
306 case RMIHIDTOOL_CMD_READ:
307 memset(report, 0, sizeof(report));
308 rc = device->Read(addr, report, len);
309 if (rc < 0)
310 fprintf(stderr, "Failed to read report: %d\n", rc);
Andrew Duggan4e811252014-04-03 15:17:57 -0700311
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700312 print_buffer(report, len);
313 break;
314 case RMIHIDTOOL_CMD_WRITE:
Andrew Duggan849fea32014-11-13 21:15:48 -0800315 i = 0;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700316 start = data;
317 memset(report, 0, sizeof(report));
318 while (find_token(start, token, &end)) {
319 start = end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800320 report[i++] = (unsigned char)strtol(token, NULL, 0);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700321 ++len;
322 }
323
324 if (device->Write(addr, report, len) < 0) {
325 fprintf(stderr, "Failed to Write Report\n");
326 return -1;
327 }
328 break;
329 case RMIHIDTOOL_CMD_FW_ID:
330 device->ScanPDT();
331 device->QueryBasicProperties();
332 fprintf(stdout, "firmware id: %lu\n", device->GetFirmwareID());
333 break;
334 case RMIHIDTOOL_CMD_PROPS:
335 device->ScanPDT();
336 device->QueryBasicProperties();
337 device->PrintProperties();
338 break;
339 case RMIHIDTOOL_CMD_ATTN:
340 report_attn = 1;
341 while(report_attn) {
342 unsigned int bytes = 256;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800343 rc = device->GetAttentionReport(NULL,
344 RMI_INTERUPT_SOURCES_ALL_MASK,
345 report, &bytes);
Andrew Dugganf6e278f2014-10-07 21:58:02 -0700346 if (rc > 0) {
347 print_buffer(report, bytes);
348 fprintf(stdout, "\n");
349 }
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700350 }
351 break;
352 case RMIHIDTOOL_CMD_PRINT_FUNCTIONS:
353 device->ScanPDT();
354 device->PrintFunctions();
355 break;
Andrew Duggan161c83c2015-05-06 18:06:03 -0700356 case RMIHIDTOOL_CMD_REBIND_DRIVER:
357 device->RebindDriver();
358 break;
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700359 case RMIHIDTOOL_CMD_PRINT_DEVICE_INFO:
360 device->PrintDeviceInfo();
361 break;
Andrew Duggana0d74e02015-05-11 15:50:59 -0700362 case RMIHIDTOOL_CMD_RESET_DEVICE:
363 device->ScanPDT();
364 device->Reset();
365 break;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700366 case RMIHIDTOOL_CMD_INTERACTIVE:
367 default:
368 interactive(device, report);
369 break;
Andrew Duggan4e811252014-04-03 15:17:57 -0700370 }
371
Andrew Duggan63078d52014-04-08 11:20:15 -0700372 device->Close();
Andrew Duggan4e811252014-04-03 15:17:57 -0700373
374 return 0;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700375}