serial: Support custom baud rates on linux

The function to do this is contained in custom_baud.c because
of broken include stuff.

Change-Id: I2a20f9182cb85e7bce5d6654a2caf20e6202b195
Signed-off-by: Urja Rannikko <urjaman@gmail.com>
Reviewed-on: https://review.coreboot.org/20224
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/Makefile b/Makefile
index 5573127..52f6c98 100644
--- a/Makefile
+++ b/Makefile
@@ -921,7 +921,7 @@
 endif
 
 ifneq ($(NEED_SERIAL), )
-LIB_OBJS += serial.o
+LIB_OBJS += serial.o custom_baud.o
 endif
 
 ifneq ($(NEED_POSIX_SOCKETS), )
diff --git a/custom_baud.c b/custom_baud.c
new file mode 100644
index 0000000..0caca80
--- /dev/null
+++ b/custom_baud.c
@@ -0,0 +1,78 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2017 Urja Rannikko <urjaman@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include "platform.h"
+#include "custom_baud.h"
+
+#if IS_LINUX
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <asm-generic/termbits.h>
+#include <asm-generic/ioctls.h>
+
+/*
+ * This include hell above is why this is in a separate source file. See eg.
+ * https://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/
+ * https://stackoverflow.com/questions/12646324/how-to-set-a-custom-baud-rate-on-linux
+ * https://github.com/jbkim/Linux-custom-baud-rate
+ * for more info.
+ */
+
+int set_custom_baudrate(int fd, unsigned int baud)
+{
+	struct termios2 tio;
+	if (ioctl(fd, TCGETS2, &tio)) {
+		return -1;
+	}
+	tio.c_cflag &= ~CBAUD;
+	tio.c_cflag |= BOTHER;
+	tio.c_ispeed = baud;
+	tio.c_ospeed = baud;
+	return ioctl(fd, TCSETS2, &tio);
+}
+
+int use_custom_baud(unsigned int baud, const struct baudentry *baudtable)
+{
+	int i;
+	for (i = 0; baudtable[i].baud; i++) {
+		if (baudtable[i].baud == baud)
+			return 0;
+
+		if (baudtable[i].baud > baud)
+			return 1;
+	}
+	return 1;
+}
+
+#else
+#include <errno.h>
+
+/* Stub, should not get called. */
+int set_custom_baudrate(int fd, unsigned int baud)
+{
+	errno = ENOSYS; /* Hoping "Function not supported" will make you look here. */
+	return -1;
+}
+
+int use_custom_baud(unsigned int baud, const struct baudentry *baudtable)
+{
+	return 0;
+}
+#endif
diff --git a/custom_baud.h b/custom_baud.h
new file mode 100644
index 0000000..ae286f5
--- /dev/null
+++ b/custom_baud.h
@@ -0,0 +1,35 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2017 Urja Rannikko <urjaman@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef __CUSTOM_BAUD_H__
+#define __CUSTOM_BAUD_H__ 1
+
+struct baudentry {
+	int flag;
+	unsigned int baud;
+};
+
+int set_custom_baudrate(int fd, unsigned int baud);
+
+/* Returns 1 if non-exact rate would be used, and setting a custom rate is supported.
+   The baudtable must be in ascending order and terminated with a 0-baud entry. */
+int use_custom_baud(unsigned int baud, const struct baudentry *baudtable);
+
+#endif
diff --git a/serial.c b/serial.c
index a64a51d..608464b 100644
--- a/serial.c
+++ b/serial.c
@@ -40,6 +40,7 @@
 #endif
 #include "flash.h"
 #include "programmer.h"
+#include "custom_baud.h"
 
 fdtype sp_fd = SER_INV_FD;
 
@@ -49,18 +50,14 @@
  * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
  * with numerical user input.
  *
- * On Linux there is a non-standard way to use arbitrary baud rates that flashrom does not support (yet), cf.
- * http://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/
+ * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no
+ * matching standard rate, see custom_baud.c
  *
  * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
  * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
  */
 #if !IS_WINDOWS
-struct baudentry {
-	int flag;
-	unsigned int baud;
-};
 #define BAUDENTRY(baud) { B##baud, baud },
 
 static const struct baudentry sp_baudtable[] = {
@@ -195,10 +192,25 @@
 	}
 	wanted = observed;
 	if (baud >= 0) {
-		const struct baudentry *entry = round_baud(baud);
-		if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
-			msg_perr_strerror("Could not set serial baud rate: ");
-			return 1;
+		if (use_custom_baud(baud, sp_baudtable)) {
+			if (set_custom_baudrate(fd, baud)) {
+				msg_perr_strerror("Could not set custom baudrate: ");
+				return 1;
+			}
+			/* We want whatever the termios looks like now, so the rest of the
+			   setup doesnt mess up the custom rate. */
+			if (tcgetattr(fd, &wanted) != 0) {
+				/* This should pretty much never happen (see above), but.. */
+				msg_perr_strerror("Could not fetch serial port configuration: ");
+				return 1;
+			}
+			msg_pdbg("Using custom baud rate.\n");
+		} else {
+			const struct baudentry *entry = round_baud(baud);
+			if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
+				msg_perr_strerror("Could not set serial baud rate: ");
+				return 1;
+			}
 		}
 	}
 	wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);