If the device was't properly closed/reset after a crash, tcgetattr() will
fail. Ignore that error and apply the settings we want nonetheless, in most
cases the device will just come back normally.

And to do so, merge setting the baud rate together with the rest so we only
have one call. Otherwise we can't apply the baud rate settings without
potentially submitting garbage to the driver.

Signed-off-by: Peter Hutterer <peter.hutte...@who-t.net>
---
 tools/isdv4-serial-debugger.c | 49 +++++++++++++++----------------------------
 1 file changed, 17 insertions(+), 32 deletions(-)

diff --git a/tools/isdv4-serial-debugger.c b/tools/isdv4-serial-debugger.c
index 8a64f43..47c6896 100644
--- a/tools/isdv4-serial-debugger.c
+++ b/tools/isdv4-serial-debugger.c
@@ -68,17 +68,16 @@ static void version(void)
                             PACKAGE_VERSION_PATCHLEVEL);
 }
 
-static int open_device(char *path)
+int open_device(const char *path)
 {
-       int fd, rc;
+       int fd;
        struct serial_struct ser;
-       struct termios t;
 
        TRACE("Opening device '%s'.\n", path);
        fd = open(path, O_RDWR);
 
        if (fd < 1)
-               perror("Failed to open device file.");
+               perror("Failed to open device file");
 
        if (ioctl(fd, TIOCGSERIAL, &ser) == -1)
        {
@@ -88,14 +87,16 @@ static int open_device(char *path)
                goto out;
        }
 
-       rc = tcgetattr(fd, &t);
-       if (rc == -1)
-       {
-               perror("Failed to get serial attributes.");
-               close(fd);
-               fd = -1;
-               goto out;
-       }
+out:
+        return fd;
+}
+
+int set_serial_attr(int fd, unsigned int baud)
+{
+       struct termios t;
+
+       if (tcgetattr(fd, &t) == -1)
+                memset(&t, 0, sizeof(t));
 
        /* defaults from xf86OpenSerial */
        t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
@@ -110,21 +111,9 @@ static int open_device(char *path)
        t.c_cflag &= ~(CSIZE); /* databits 8 */
        t.c_cflag |= (CS8); /* databits 8 */
        t.c_cflag &= ~(PARENB); /* parity none */
-       t.c_cc[VMIN] = 1;       /* vmin 1 */
+       t.c_cc[VMIN] = 1;       /* vmin 1 */
        t.c_cc[VTIME] = 10;     /* vtime 10 */
-       t.c_iflag |= IXOFF;     /* flow controll xoff */
-
-
-       tcsetattr(fd, TCSANOW, &t);
-
-out:
-       return fd;
-}
-
-static int set_baud_rate(int fd, int baud)
-{
-       struct termios t;
-       int rc;
+       t.c_iflag |= IXOFF;     /* flow controll xoff */
 
        TRACE("Baud rate is %d\n", baud);
 
@@ -137,15 +126,11 @@ static int set_baud_rate(int fd, int baud)
                        return -1;
        }
 
-       rc = tcgetattr(fd, &t);
-
-       if (rc)
-               return rc;
-
        cfsetispeed(&t, baud);
        cfsetospeed(&t, baud);
 
        return tcsetattr(fd, TCSANOW, &t);
+
 }
 
 static int write_to_tablet(int fd, char *command)
@@ -567,7 +552,7 @@ int main (int argc, char **argv)
        if (fd < 0)
                return 1;
 
-       rc = set_baud_rate(fd, baudrate);
+       rc = set_serial_attr(fd, baudrate);
        if (rc < 0)
                return 1;
 
-- 
1.8.5.3


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Linuxwacom-devel mailing list
Linuxwacom-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to