find device when no args are given, accept RM850i
authornotaz <notasas@gmail.com>
Fri, 26 Feb 2016 23:19:40 +0000 (01:19 +0200)
committernotaz <notasas@gmail.com>
Fri, 26 Feb 2016 23:19:40 +0000 (01:19 +0200)
README.md
corsairmi.c

index 932d636..f09879b 100644 (file)
--- 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]`
index e09d80b..f45b421 100644 (file)
 #include <unistd.h>
 #include <linux/hidraw.h>
 
+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);