blob: 761d496113bb251b0634d5162408c646399c53b7 [file] [log] [blame]
Thomas Heijligen140c1262021-09-27 15:12:26 +02001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2017 Urja Rannikko <urjaman@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <sys/ioctl.h>
18#include <fcntl.h>
19#include <asm-generic/termbits.h>
20#include <asm-generic/ioctls.h>
21
22#include "custom_baud.h"
23
24/*
25 * This include hell above is why this is in a separate source file. See eg.
26 * https://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/
27 * https://stackoverflow.com/questions/12646324/how-to-set-a-custom-baud-rate-on-linux
28 * https://github.com/jbkim/Linux-custom-baud-rate
29 * for more info.
30 */
31
Peter Stugeb078ec62022-12-11 04:02:10 +010032int set_custom_baudrate(int fd, unsigned int baud, const enum custom_baud_stage stage, void *tio_wanted)
Thomas Heijligen140c1262021-09-27 15:12:26 +020033{
34 struct termios2 tio;
Peter Stugeb078ec62022-12-11 04:02:10 +010035
36 if (stage != BEFORE_FLAGS)
37 return 0;
38
Thomas Heijligen140c1262021-09-27 15:12:26 +020039 if (ioctl(fd, TCGETS2, &tio)) {
40 return -1;
41 }
42 tio.c_cflag &= ~CBAUD;
43 tio.c_cflag |= BOTHER;
44 tio.c_ispeed = baud;
45 tio.c_ospeed = baud;
46 return ioctl(fd, TCSETS2, &tio);
47}
48
49int use_custom_baud(unsigned int baud, const struct baudentry *baudtable)
50{
51 int i;
52 for (i = 0; baudtable[i].baud; i++) {
53 if (baudtable[i].baud == baud)
54 return 0;
55
56 if (baudtable[i].baud > baud)
57 return 1;
58 }
59 return 1;
60}