From: David Woodhouse <dwmw@amazon.co.uk>
Date: Fri, 21 Jan 2022 10:46:25 +0000 (+0000)
Subject: Fix handling of ENOENT on opening hiddev
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;p=users%2Fdwmw2%2Fpidgin-headset.git

Fix handling of ENOENT on opening hiddev

The original Jabra code returned a boolean from doListDev(), but I made it
return the fd. Which means returning zero for ENOENT probably isn't a good
idea any more. Make it return -1 for that case too.
---

diff --git a/jabra.c b/jabra.c
index 42c7db9..80f7359 100644
--- a/jabra.c
+++ b/jabra.c
@@ -246,36 +246,32 @@ static int doListDev(char *path) {
   char name[128];
   int version;
 
-  if ((fd = open(path, O_RDONLY)) != -1) {
-    if (ioctl(fd, HIDIOCGDEVINFO, &devinfo) == -1) {
-      perror("ioctl HIDIOCGDEVINFO");
-      close(fd);
-      return -1;
-    }
-    if (ioctl(fd, HIDIOCGNAME(sizeof(name)), name) == -1) {
-      perror("ioctl HIDIOCGNAME");
-      close(fd);
-      return -1;
-    }
-    if (ioctl(fd, HIDIOCGVERSION, &version) == -1) {
-      perror("ioctl HIDIOCGVERSION");
-      close(fd);
-      return -1;
-    }
-    if (devinfo.vendor != JABRA_VID &&
-	devinfo.vendor != PLANTRONICS_VID) {
-	    close(fd);
-	    return -1;
-    }
+  fd = open(path, O_RDONLY);
+  if (fd == -1)
     return fd;
-  }
 
-  if (errno == ENOENT) {
-    return 0;
+  if (ioctl(fd, HIDIOCGDEVINFO, &devinfo) == -1) {
+    perror("ioctl HIDIOCGDEVINFO");
+    close(fd);
+    return -1;
+  }
+  if (ioctl(fd, HIDIOCGNAME(sizeof(name)), name) == -1) {
+    perror("ioctl HIDIOCGNAME");
+    close(fd);
+    return -1;
+  }
+  if (ioctl(fd, HIDIOCGVERSION, &version) == -1) {
+    perror("ioctl HIDIOCGVERSION");
+    close(fd);
+    return -1;
+  }
+  if (devinfo.vendor != JABRA_VID &&
+      devinfo.vendor != PLANTRONICS_VID) {
+    close(fd);
+    return -1;
   }
 
-  perror("ioctl HIDIOCGVERSION");
-  return -1;
+  return fd;
 }
 
 static void writeUsage(int fd, unsigned report_type, unsigned page, unsigned code, __s32 value) {