#include #include #include #include #include #include #include #include "debug.h" /* * HACK: declare the ioctl function by hand... * * We cannot include as it will eventually include * , which will conflict with . As we * really need the latter one AND the ioctl, just declare it by hand * here so that we get to use it... */ int ioctl(int d, int request, ...); /** * Open a serial device and adjust the baud rate to give (arbitrary) * baud rate. The actual baud rate is stored back to the @baud * variable. This might differ from the requested one as the * underlying hardware may not support all possible baud * rates. * * Returns the file descriptor that was opened. */ int open_at_baud(const char *device, int *baud) { struct termios2 tios; int fd; fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { pr_err("error opening %s: %m\n", device); return fd; } if (ioctl(fd, TCGETS2, &tios)) { pr_err("TCGETS2: %m\n"); goto err; } tios.c_cflag &= ~CBAUD; tios.c_cflag |= BOTHER; tios.c_ispeed = *baud; tios.c_ospeed = *baud; if (ioctl(fd, TCSETS2, &tios)) { pr_err("TCSETS2: %m"); goto err; } if (ioctl(fd, TCGETS2, &tios)) { pr_err("TCGETS2: %m"); goto err; } *baud = tios.c_ospeed; return fd; err: close(fd); return -1; }