From: notaz Date: Fri, 26 Feb 2016 23:19:40 +0000 (+0200) Subject: find device when no args are given, accept RM850i X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=465ea922e26f0fddfc1255dee6a59bea2e4a4560;p=corsairmi.git find device when no args are given, accept RM850i --- diff --git a/README.md b/README.md index 932d636..f09879b 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ There are basically no dependencies, so running `make` should suffice. Usage ----- -`./corsairmi /dev/hidrawN` +`./corsairmi [/dev/hidrawN]` diff --git a/corsairmi.c b/corsairmi.c index e09d80b..f45b421 100644 --- a/corsairmi.c +++ b/corsairmi.c @@ -63,6 +63,11 @@ #include #include +static const uint16_t products[] = { + 0x1c0b, /* RM750i */ + 0x1c0c, /* RM850i */ +}; + static void dump(const uint8_t *buf, size_t size) { size_t i, j; @@ -171,43 +176,75 @@ static void print_std_reg(int fd, uint8_t reg, const char *fmt, ...) printf("%5.1f\n", mkv(val)); } -int main(int argc, char *argv[]) +static int try_open_device(const char *name, int report_errors) { - const char *device = "/dev/hidraw0"; struct hidraw_devinfo info; + int found = 0; + int i, ret, fd; + + fd = open(name, O_RDWR); + if (fd == -1) { + if (report_errors) { + fprintf(stderr, "open %s: ", name); + perror(NULL); + } + return -1; + } + + memset(&info, 0, sizeof(info)); + ret = ioctl(fd, HIDIOCGRAWINFO, &info); + if (ret != 0) { + perror("HIDIOCGRAWINFO"); + goto out; + } + + if (info.vendor != 0x1b1c) + goto out; + + for (i = 0; i < sizeof(products) / sizeof(products[0]); i++) { + if (info.product == products[i]) { + found = 1; + break; + } + } + +out: + if (!found) { + if (report_errors) + fprintf(stderr, "unexpected device: %04hx:%04hx\n", + info.vendor, info.product); + close(fd); + fd = -1; + } + return fd; +} + +int main(int argc, char *argv[]) +{ char name[63]; uint32_t v32; uint8_t osel; - int ret, fd; + int i, fd; if (argc > 1) { - if (argv[1][0] == '-') { + if (argv[1][0] == '-' || argc != 2) { fprintf(stderr, "usage:\n"); - fprintf(stderr, "%s /dev/hidrawN\n", argv[0]); + fprintf(stderr, "%s [/dev/hidrawN]\n", argv[0]); return 1; } - device = argv[1]; - } - - fd = open(device, O_RDWR); - if (fd == -1) { - fprintf(stderr, "open %s: ", device); - perror(NULL); - return 1; + fd = try_open_device(argv[1], 1); } - - memset(&info, 0, sizeof(info)); - ret = ioctl(fd, HIDIOCGRAWINFO, &info); - if (ret != 0) { - perror("HIDIOCGRAWINFO"); - return 1; + else { + for (i = 0; i < 16; i++) { + snprintf(name, sizeof(name), "/dev/hidraw%d", i); + fd = try_open_device(name, 0); + if (fd != -1) + break; + } } - if (info.vendor != 0x1b1c && info.product != 0x1c0b) { - fprintf(stderr, "unexpected device: %04hx:%04hx\n", - info.vendor, info.product); + if (fd == -1) return 1; - } name[sizeof(name) - 1] = 0; send_recv_cmd(fd, 0xfe, 0x03, 0x00, name, sizeof(name) - 1);