Andrew Duggan | 052556f | 2014-04-16 11:32:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 18 | #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 Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 27 | #include <getopt.h> |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 28 | |
| 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 Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 37 | #define RMI4UPDATE_GETOPTS "hp:ir:w:foam" |
| 38 | |
| 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, |
| 47 | }; |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 48 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 49 | static int report_attn = 0; |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 50 | static RMIDevice * g_device = NULL; |
| 51 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 52 | void print_help(const char *prog_name) |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 53 | { |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 54 | fprintf(stdout, "Usage: %s [OPTIONS] DEVICEFILE\n", prog_name); |
| 55 | fprintf(stdout, "\t-h, --help\t\t\t\tPrint this message\n"); |
| 56 | fprintf(stdout, "\t-p, --protocol [protocol]\t\tSet which transport prototocl to use.\n"); |
| 57 | fprintf(stdout, "\t-i, --interactive\t\t\tRun in interactive mode.\n"); |
| 58 | fprintf(stdout, "\t-r, --read [address] [length]\t\tRead registers starting at the address.\n"); |
| 59 | fprintf(stdout, "\t-r, --write [address] [length] [data]\tWrite registers starting at the address.\n"); |
| 60 | fprintf(stdout, "\t-f, --firmware-id\t\t\tPrint the firmware id\n"); |
| 61 | fprintf(stdout, "\t-o, --props\t\t\t\tPrint device properties\n"); |
| 62 | fprintf(stdout, "\t-a, --attention\t\t\t\tPrint attention reports until control + c\n"); |
| 63 | fprintf(stdout, "\t-m, --print-functions\t\t\tPrint RMI4 functions for the device.\n"); |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 64 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 65 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 66 | void print_cmd_usage() |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 67 | { |
| 68 | fprintf(stdout, "Commands:\n"); |
| 69 | fprintf(stdout, "s [0,1,2]: Set RMIMode\n"); |
| 70 | fprintf(stdout, "r address size: read size bytes from address\n"); |
| 71 | fprintf(stdout, "w address { values }: write bytes to address\n"); |
| 72 | fprintf(stdout, "a: Wait for attention\n"); |
| 73 | fprintf(stdout, "q: quit\n"); |
| 74 | } |
| 75 | |
| 76 | int find_token(char * input, char * result, char ** endpp) |
| 77 | { |
| 78 | int i = 0; |
| 79 | char * start = input; |
| 80 | char * end; |
| 81 | |
| 82 | while (input[i] == ' ') { |
| 83 | ++start; |
| 84 | ++i; |
| 85 | } |
| 86 | |
| 87 | while (input[i] != '\0') { |
| 88 | ++i; |
| 89 | if (input[i] == ' ') { |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | end = &input[i]; |
| 94 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 95 | if (start == end) |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 96 | return 0; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 97 | |
| 98 | *endpp = end; |
| 99 | strncpy(result, start, end - start); |
| 100 | result[end - start + 1] = '\0'; |
| 101 | |
| 102 | return 1; |
| 103 | } |
| 104 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 105 | |
| 106 | |
| 107 | void interactive(RMIDevice * device, unsigned char *report) |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 108 | { |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 109 | char token[256]; |
| 110 | char * start; |
| 111 | char * end; |
| 112 | int rc; |
| 113 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 114 | for (;;) { |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 115 | fprintf(stdout, "\n"); |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 116 | print_cmd_usage(); |
| 117 | char input[256]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 118 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 119 | if (fgets(input, 256, stdin)) { |
| 120 | memset(token, 0, 256); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 121 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 122 | 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 Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 133 | } |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 134 | } 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; |
| 160 | report[index] = strtol(token, NULL, 0); |
| 161 | ++index; |
| 162 | ++len; |
| 163 | } |
| 164 | |
| 165 | if (device->Write(addr, report, len) < 0) { |
| 166 | fprintf(stderr, "Failed to Write Report\n"); |
| 167 | continue; |
| 168 | } |
| 169 | } else if (input[0] == 'a') { |
| 170 | unsigned int bytes = 256; |
| 171 | device->GetAttentionReport(NULL, NULL, report, &bytes); |
| 172 | print_buffer(report, bytes); |
| 173 | } else if (input[0] == 'q') { |
| 174 | return; |
| 175 | } else { |
| 176 | print_cmd_usage(); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 177 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 178 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 179 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void cleanup(int status) |
| 183 | { |
| 184 | if (report_attn) { |
| 185 | report_attn = 0; |
| 186 | if (g_device) |
| 187 | g_device->Cancel(); |
| 188 | } else { |
| 189 | exit(0); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int main(int argc, char ** argv) |
| 194 | { |
| 195 | int rc; |
| 196 | struct sigaction sig_cleanup_action; |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 197 | int opt; |
| 198 | int index; |
| 199 | RMIDevice *device; |
| 200 | const char *protocol = "HID"; |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 201 | unsigned char report[256]; |
| 202 | char token[256]; |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 203 | static struct option long_options[] = { |
| 204 | {"help", 0, NULL, 'h'}, |
| 205 | {"protocol", 1, NULL, 'p'}, |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 206 | {"interactive", 0, NULL, 'i'}, |
| 207 | {"read", 1, NULL, 'r'}, |
| 208 | {"write", 1, NULL, 'w'}, |
| 209 | {"firmware-id", 0, NULL, 'f'}, |
| 210 | {"props", 0, NULL, 'o'}, |
| 211 | {"attention", 0, NULL, 'a'}, |
| 212 | {"print-functions", 0, NULL, 'm'}, |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 213 | {0, 0, 0, 0}, |
| 214 | }; |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 215 | enum rmihidtool_cmd cmd = RMIHIDTOOL_CMD_INTERACTIVE; |
| 216 | unsigned int addr; |
| 217 | unsigned int len; |
| 218 | char * data; |
| 219 | char * start; |
| 220 | char * end; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 221 | |
| 222 | memset(&sig_cleanup_action, 0, sizeof(struct sigaction)); |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 223 | sig_cleanup_action.sa_handler = cleanup; |
| 224 | sig_cleanup_action.sa_flags = SA_RESTART; |
| 225 | sigaction(SIGINT, &sig_cleanup_action, NULL); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 226 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 227 | while ((opt = getopt_long(argc, argv, RMI4UPDATE_GETOPTS, long_options, &index)) != -1) { |
| 228 | switch (opt) { |
| 229 | case 'h': |
| 230 | print_help(argv[0]); |
| 231 | return 0; |
| 232 | case 'p': |
| 233 | protocol = optarg; |
| 234 | break; |
| 235 | case 'i': |
| 236 | cmd = RMIHIDTOOL_CMD_INTERACTIVE; |
| 237 | break; |
| 238 | case 'r': |
| 239 | cmd = RMIHIDTOOL_CMD_READ; |
| 240 | addr = strtol(optarg, NULL, 0); |
| 241 | len = strtol(argv[optind++], NULL, 0); |
| 242 | break; |
| 243 | case 'w': |
| 244 | cmd = RMIHIDTOOL_CMD_WRITE; |
| 245 | addr = strtol(optarg, NULL, 0); |
| 246 | data = argv[optind++]; |
| 247 | break; |
| 248 | case 'f': |
| 249 | cmd = RMIHIDTOOL_CMD_FW_ID; |
| 250 | break; |
| 251 | case 'o': |
| 252 | cmd = RMIHIDTOOL_CMD_PROPS; |
| 253 | break; |
| 254 | case 'a': |
| 255 | cmd = RMIHIDTOOL_CMD_ATTN; |
| 256 | break; |
| 257 | case 'm': |
| 258 | cmd = RMIHIDTOOL_CMD_PRINT_FUNCTIONS; |
| 259 | break; |
| 260 | default: |
| 261 | print_help(argv[0]); |
| 262 | return 0; |
| 263 | break; |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 264 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 265 | } |
| 266 | } |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 267 | |
| 268 | if (!strncasecmp("hid", protocol, 3)) { |
| 269 | device = new HIDDevice(); |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 270 | } else { |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 271 | fprintf(stderr, "Invalid Protocol: %s\n", protocol); |
| 272 | return -1; |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 273 | } |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 274 | |
| 275 | rc = device->Open(argv[optind++]); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 276 | if (rc) { |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 277 | fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno, |
| 278 | strerror(errno)); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 279 | return 1; |
| 280 | } |
| 281 | |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 282 | g_device = device; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 283 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 284 | switch (cmd) { |
| 285 | case RMIHIDTOOL_CMD_READ: |
| 286 | memset(report, 0, sizeof(report)); |
| 287 | rc = device->Read(addr, report, len); |
| 288 | if (rc < 0) |
| 289 | fprintf(stderr, "Failed to read report: %d\n", rc); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 290 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 291 | print_buffer(report, len); |
| 292 | break; |
| 293 | case RMIHIDTOOL_CMD_WRITE: |
| 294 | start = data; |
| 295 | memset(report, 0, sizeof(report)); |
| 296 | while (find_token(start, token, &end)) { |
| 297 | start = end; |
| 298 | report[index++] = (unsigned char)strtol(token, NULL, 0); |
| 299 | ++len; |
| 300 | } |
| 301 | |
| 302 | if (device->Write(addr, report, len) < 0) { |
| 303 | fprintf(stderr, "Failed to Write Report\n"); |
| 304 | return -1; |
| 305 | } |
| 306 | break; |
| 307 | case RMIHIDTOOL_CMD_FW_ID: |
| 308 | device->ScanPDT(); |
| 309 | device->QueryBasicProperties(); |
| 310 | fprintf(stdout, "firmware id: %lu\n", device->GetFirmwareID()); |
| 311 | break; |
| 312 | case RMIHIDTOOL_CMD_PROPS: |
| 313 | device->ScanPDT(); |
| 314 | device->QueryBasicProperties(); |
| 315 | device->PrintProperties(); |
| 316 | break; |
| 317 | case RMIHIDTOOL_CMD_ATTN: |
| 318 | report_attn = 1; |
| 319 | while(report_attn) { |
| 320 | unsigned int bytes = 256; |
| 321 | device->GetAttentionReport(NULL, NULL, report, &bytes); |
| 322 | print_buffer(report, bytes); |
| 323 | fprintf(stdout, "\n"); |
| 324 | } |
| 325 | break; |
| 326 | case RMIHIDTOOL_CMD_PRINT_FUNCTIONS: |
| 327 | device->ScanPDT(); |
| 328 | device->PrintFunctions(); |
| 329 | break; |
| 330 | case RMIHIDTOOL_CMD_INTERACTIVE: |
| 331 | default: |
| 332 | interactive(device, report); |
| 333 | break; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Andrew Duggan | 63078d5 | 2014-04-08 11:20:15 -0700 | [diff] [blame] | 336 | device->Close(); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 337 | |
| 338 | return 0; |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame^] | 339 | } |