blob: cf25a4b79156b79fe090e792334e202e46accfb2 [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 Duggan2c24adb2015-05-06 18:18:06 -070037#define RMI4UPDATE_GETOPTS "hp:ir:w:foambd"
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 Duggane9a5cd02014-04-29 13:34:42 -070049};
Andrew Duggan63078d52014-04-08 11:20:15 -070050
Andrew Duggan4e811252014-04-03 15:17:57 -070051static int report_attn = 0;
Andrew Duggan63078d52014-04-08 11:20:15 -070052static RMIDevice * g_device = NULL;
53
Andrew Duggane9a5cd02014-04-29 13:34:42 -070054void print_help(const char *prog_name)
Andrew Duggan63078d52014-04-08 11:20:15 -070055{
Andrew Duggane9a5cd02014-04-29 13:34:42 -070056 fprintf(stdout, "Usage: %s [OPTIONS] DEVICEFILE\n", prog_name);
57 fprintf(stdout, "\t-h, --help\t\t\t\tPrint this message\n");
58 fprintf(stdout, "\t-p, --protocol [protocol]\t\tSet which transport prototocl to use.\n");
59 fprintf(stdout, "\t-i, --interactive\t\t\tRun in interactive mode.\n");
60 fprintf(stdout, "\t-r, --read [address] [length]\t\tRead registers starting at the address.\n");
61 fprintf(stdout, "\t-r, --write [address] [length] [data]\tWrite registers starting at the address.\n");
62 fprintf(stdout, "\t-f, --firmware-id\t\t\tPrint the firmware id\n");
63 fprintf(stdout, "\t-o, --props\t\t\t\tPrint device properties\n");
64 fprintf(stdout, "\t-a, --attention\t\t\t\tPrint attention reports until control + c\n");
65 fprintf(stdout, "\t-m, --print-functions\t\t\tPrint RMI4 functions for the device.\n");
Andrew Duggan161c83c2015-05-06 18:06:03 -070066 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 -070067 fprintf(stdout, "\t-d, --device-info\t\t\tPrint protocol specific information about the device.\n");
Andrew Duggan63078d52014-04-08 11:20:15 -070068}
Andrew Duggan4e811252014-04-03 15:17:57 -070069
Andrew Duggane9a5cd02014-04-29 13:34:42 -070070void print_cmd_usage()
Andrew Duggan4e811252014-04-03 15:17:57 -070071{
72 fprintf(stdout, "Commands:\n");
73 fprintf(stdout, "s [0,1,2]: Set RMIMode\n");
74 fprintf(stdout, "r address size: read size bytes from address\n");
75 fprintf(stdout, "w address { values }: write bytes to address\n");
76 fprintf(stdout, "a: Wait for attention\n");
77 fprintf(stdout, "q: quit\n");
78}
79
80int find_token(char * input, char * result, char ** endpp)
81{
82 int i = 0;
83 char * start = input;
84 char * end;
85
86 while (input[i] == ' ') {
87 ++start;
88 ++i;
89 }
90
91 while (input[i] != '\0') {
Andrew Duggan849fea32014-11-13 21:15:48 -080092 if (input[++i] == ' ')
Andrew Duggan4e811252014-04-03 15:17:57 -070093 break;
Andrew Duggan4e811252014-04-03 15:17:57 -070094 }
95 end = &input[i];
96
Andrew Duggane9a5cd02014-04-29 13:34:42 -070097 if (start == end)
Andrew Duggan4e811252014-04-03 15:17:57 -070098 return 0;
Andrew Duggan4e811252014-04-03 15:17:57 -070099
100 *endpp = end;
101 strncpy(result, start, end - start);
Andrew Duggan849fea32014-11-13 21:15:48 -0800102 result[end - start] = '\0';
Andrew Duggan4e811252014-04-03 15:17:57 -0700103
104 return 1;
105}
106
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700107void interactive(RMIDevice * device, unsigned char *report)
Andrew Duggan4e811252014-04-03 15:17:57 -0700108{
Andrew Duggan4e811252014-04-03 15:17:57 -0700109 char token[256];
110 char * start;
111 char * end;
112 int rc;
113
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700114 for (;;) {
Andrew Duggan4e811252014-04-03 15:17:57 -0700115 fprintf(stdout, "\n");
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700116 print_cmd_usage();
117 char input[256];
Andrew Duggan4e811252014-04-03 15:17:57 -0700118
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700119 if (fgets(input, 256, stdin)) {
120 memset(token, 0, 256);
Andrew Duggan4e811252014-04-03 15:17:57 -0700121
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700122 if (input[0] == 's') {
123 start = input + 2;
124 find_token(start, token, &end);
125 int mode = strtol(token, NULL, 0);
126 if (mode >= 0 && mode <= 2) {
127 if (device->SetMode(mode)) {
128 fprintf(stderr, "Set RMI Mode to: %d\n", mode);
129 } else {
130 fprintf(stderr, "Set RMI Mode FAILED!\n");
131 continue;
132 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700133 }
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700134 } else if (input[0] == 'r') {
135 start = input + 2;
136 find_token(start, token, &end);
137 start = end + 1;
138 unsigned int addr = strtol(token, NULL, 0);
139 find_token(start, token, &end);
140 start = end + 1;
141 unsigned int len = strtol(token, NULL, 0);
142 fprintf(stdout, "Address = 0x%02x Length = %d\n", addr, len);
143
144 memset(report, 0, 256);
145 rc = device->Read(addr, report, len);
146 if (rc < 0)
147 fprintf(stderr, "Failed to read report: %d\n", rc);
148 print_buffer(report, len);
149 } else if (input[0] == 'w') {
150 int index = 0;
151 start = input + 2;
152 find_token(start, token, &end);
153 start = end + 1;
154 unsigned int addr = strtol(token, NULL, 0);
155 unsigned int len = 0;
156
157 memset(report, 0, 256);
158 while (find_token(start, token, &end)) {
159 start = end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800160 report[index++] = strtol(token, NULL, 0);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700161 ++len;
162 }
163
164 if (device->Write(addr, report, len) < 0) {
165 fprintf(stderr, "Failed to Write Report\n");
166 continue;
167 }
168 } else if (input[0] == 'a') {
169 unsigned int bytes = 256;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800170 device->GetAttentionReport(NULL,
171 RMI_INTERUPT_SOURCES_ALL_MASK,
172 report, &bytes);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700173 print_buffer(report, bytes);
174 } else if (input[0] == 'q') {
175 return;
176 } else {
177 print_cmd_usage();
Andrew Duggan4e811252014-04-03 15:17:57 -0700178 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700179 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700180 }
Andrew Duggan4e811252014-04-03 15:17:57 -0700181}
182
183static void cleanup(int status)
184{
185 if (report_attn) {
186 report_attn = 0;
187 if (g_device)
188 g_device->Cancel();
189 } else {
190 exit(0);
191 }
192}
193
194int main(int argc, char ** argv)
195{
196 int rc;
197 struct sigaction sig_cleanup_action;
Andrew Duggan63078d52014-04-08 11:20:15 -0700198 int opt;
199 int index;
200 RMIDevice *device;
201 const char *protocol = "HID";
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700202 unsigned char report[256];
203 char token[256];
Andrew Duggan63078d52014-04-08 11:20:15 -0700204 static struct option long_options[] = {
205 {"help", 0, NULL, 'h'},
206 {"protocol", 1, NULL, 'p'},
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700207 {"interactive", 0, NULL, 'i'},
208 {"read", 1, NULL, 'r'},
209 {"write", 1, NULL, 'w'},
210 {"firmware-id", 0, NULL, 'f'},
211 {"props", 0, NULL, 'o'},
212 {"attention", 0, NULL, 'a'},
213 {"print-functions", 0, NULL, 'm'},
Andrew Duggan161c83c2015-05-06 18:06:03 -0700214 {"rebind-driver", 0, NULL, 'b'},
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700215 {"device-info", 0, NULL, 'd'},
Andrew Duggan63078d52014-04-08 11:20:15 -0700216 {0, 0, 0, 0},
217 };
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700218 enum rmihidtool_cmd cmd = RMIHIDTOOL_CMD_INTERACTIVE;
Andrew Duggan8b79f312014-05-16 13:26:09 -0700219 unsigned int addr = 0;
220 unsigned int len = 0;
221 char * data = NULL;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700222 char * start;
223 char * end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800224 int i = 0;
Andrew Duggan4e811252014-04-03 15:17:57 -0700225
226 memset(&sig_cleanup_action, 0, sizeof(struct sigaction));
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700227 sig_cleanup_action.sa_handler = cleanup;
228 sig_cleanup_action.sa_flags = SA_RESTART;
229 sigaction(SIGINT, &sig_cleanup_action, NULL);
Andrew Duggan4e811252014-04-03 15:17:57 -0700230
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700231 while ((opt = getopt_long(argc, argv, RMI4UPDATE_GETOPTS, long_options, &index)) != -1) {
232 switch (opt) {
233 case 'h':
234 print_help(argv[0]);
235 return 0;
236 case 'p':
237 protocol = optarg;
238 break;
239 case 'i':
240 cmd = RMIHIDTOOL_CMD_INTERACTIVE;
241 break;
242 case 'r':
243 cmd = RMIHIDTOOL_CMD_READ;
244 addr = strtol(optarg, NULL, 0);
245 len = strtol(argv[optind++], NULL, 0);
246 break;
247 case 'w':
248 cmd = RMIHIDTOOL_CMD_WRITE;
249 addr = strtol(optarg, NULL, 0);
250 data = argv[optind++];
251 break;
252 case 'f':
253 cmd = RMIHIDTOOL_CMD_FW_ID;
254 break;
255 case 'o':
256 cmd = RMIHIDTOOL_CMD_PROPS;
257 break;
258 case 'a':
259 cmd = RMIHIDTOOL_CMD_ATTN;
260 break;
261 case 'm':
262 cmd = RMIHIDTOOL_CMD_PRINT_FUNCTIONS;
263 break;
Andrew Duggan161c83c2015-05-06 18:06:03 -0700264 case 'b':
265 cmd = RMIHIDTOOL_CMD_REBIND_DRIVER;
266 break;
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700267 case 'd':
268 cmd = RMIHIDTOOL_CMD_PRINT_DEVICE_INFO;
269 break;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700270 default:
271 print_help(argv[0]);
272 return 0;
273 break;
Andrew Duggan63078d52014-04-08 11:20:15 -0700274
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700275 }
276 }
Andrew Duggan63078d52014-04-08 11:20:15 -0700277
278 if (!strncasecmp("hid", protocol, 3)) {
279 device = new HIDDevice();
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700280 } else {
Andrew Duggan63078d52014-04-08 11:20:15 -0700281 fprintf(stderr, "Invalid Protocol: %s\n", protocol);
282 return -1;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700283 }
Andrew Duggan63078d52014-04-08 11:20:15 -0700284
Andrew Duggan4667eb72014-04-29 13:37:42 -0700285 if (optind >= argc) {
286 print_help(argv[0]);
287 return -1;
288 }
289
Andrew Duggan63078d52014-04-08 11:20:15 -0700290 rc = device->Open(argv[optind++]);
Andrew Duggan4e811252014-04-03 15:17:57 -0700291 if (rc) {
Andrew Duggan63078d52014-04-08 11:20:15 -0700292 fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno,
293 strerror(errno));
Andrew Duggan4e811252014-04-03 15:17:57 -0700294 return 1;
295 }
296
Andrew Duggan63078d52014-04-08 11:20:15 -0700297 g_device = device;
Andrew Duggan4e811252014-04-03 15:17:57 -0700298
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700299 switch (cmd) {
300 case RMIHIDTOOL_CMD_READ:
301 memset(report, 0, sizeof(report));
302 rc = device->Read(addr, report, len);
303 if (rc < 0)
304 fprintf(stderr, "Failed to read report: %d\n", rc);
Andrew Duggan4e811252014-04-03 15:17:57 -0700305
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700306 print_buffer(report, len);
307 break;
308 case RMIHIDTOOL_CMD_WRITE:
Andrew Duggan849fea32014-11-13 21:15:48 -0800309 i = 0;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700310 start = data;
311 memset(report, 0, sizeof(report));
312 while (find_token(start, token, &end)) {
313 start = end;
Andrew Duggan849fea32014-11-13 21:15:48 -0800314 report[i++] = (unsigned char)strtol(token, NULL, 0);
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700315 ++len;
316 }
317
318 if (device->Write(addr, report, len) < 0) {
319 fprintf(stderr, "Failed to Write Report\n");
320 return -1;
321 }
322 break;
323 case RMIHIDTOOL_CMD_FW_ID:
324 device->ScanPDT();
325 device->QueryBasicProperties();
326 fprintf(stdout, "firmware id: %lu\n", device->GetFirmwareID());
327 break;
328 case RMIHIDTOOL_CMD_PROPS:
329 device->ScanPDT();
330 device->QueryBasicProperties();
331 device->PrintProperties();
332 break;
333 case RMIHIDTOOL_CMD_ATTN:
334 report_attn = 1;
335 while(report_attn) {
336 unsigned int bytes = 256;
Andrew Dugganf73fdc72014-11-09 11:02:22 -0800337 rc = device->GetAttentionReport(NULL,
338 RMI_INTERUPT_SOURCES_ALL_MASK,
339 report, &bytes);
Andrew Dugganf6e278f2014-10-07 21:58:02 -0700340 if (rc > 0) {
341 print_buffer(report, bytes);
342 fprintf(stdout, "\n");
343 }
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700344 }
345 break;
346 case RMIHIDTOOL_CMD_PRINT_FUNCTIONS:
347 device->ScanPDT();
348 device->PrintFunctions();
349 break;
Andrew Duggan161c83c2015-05-06 18:06:03 -0700350 case RMIHIDTOOL_CMD_REBIND_DRIVER:
351 device->RebindDriver();
352 break;
Andrew Duggan2c24adb2015-05-06 18:18:06 -0700353 case RMIHIDTOOL_CMD_PRINT_DEVICE_INFO:
354 device->PrintDeviceInfo();
355 break;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700356 case RMIHIDTOOL_CMD_INTERACTIVE:
357 default:
358 interactive(device, report);
359 break;
Andrew Duggan4e811252014-04-03 15:17:57 -0700360 }
361
Andrew Duggan63078d52014-04-08 11:20:15 -0700362 device->Close();
Andrew Duggan4e811252014-04-03 15:17:57 -0700363
364 return 0;
Andrew Duggane9a5cd02014-04-29 13:34:42 -0700365}