]> git.itanic.dy.fi Git - log-plotter/blob - baud.c
Rename debug.[ch] to trace.[ch]
[log-plotter] / baud.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <linux/termios.h>
7 #include <unistd.h>
8
9 #include "trace.h"
10
11 /*
12  * HACK: declare the ioctl function by hand...
13  *
14  * We cannot include <sys/ioctl.h> as it will eventually include
15  * <termios.h>, which will conflict with <linux/temios.h>. As we
16  * really need the latter one AND the ioctl, just declare it by hand
17  * here so that we get to use it...
18  */
19 int ioctl(int d, int request, ...);
20
21 /**
22  * Open a serial device and adjust the baud rate to give (arbitrary)
23  * baud rate. The actual baud rate is stored back to the @baud
24  * variable. This might differ from the requested one as the
25  * underlying hardware may not support all possible baud
26  * rates.
27  *
28  * Returns the file descriptor that was opened.
29  */
30 int open_at_baud(const char *device, int *baud)
31 {
32         struct termios2 tios;
33         int fd;
34
35         fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
36
37         if (fd == -1) {
38                 pr_err("error opening %s: %m\n", device);
39                 return fd;
40         }
41
42         if (ioctl(fd, TCGETS2, &tios)) {
43                 pr_err("TCGETS2: %m\n");
44                 goto err;
45         }
46
47         tios.c_cflag &= ~CBAUD;
48         tios.c_cflag |= BOTHER;
49         tios.c_ispeed = *baud;
50         tios.c_ospeed = *baud;
51
52         if (ioctl(fd, TCSETS2, &tios)) {
53                 pr_err("TCSETS2: %m");
54                 goto err;
55         }
56
57         if (ioctl(fd, TCGETS2, &tios)) {
58                 pr_err("TCGETS2: %m");
59                 goto err;
60         }
61
62         *baud = tios.c_ospeed;
63
64         return fd;
65 err:
66         close(fd);
67
68         return -1;
69 }