xref: /aosp_15_r20/external/flashrom/custom_baud_linux.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2017 Urja Rannikko <[email protected]>
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 
set_custom_baudrate(int fd,unsigned int baud,const enum custom_baud_stage stage,void * tio_wanted)32 int set_custom_baudrate(int fd, unsigned int baud, const enum custom_baud_stage stage, void *tio_wanted)
33 {
34 	struct termios2 tio;
35 
36 	if (stage != BEFORE_FLAGS)
37 		return 0;
38 
39 	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 
use_custom_baud(unsigned int baud,const struct baudentry * baudtable)49 int 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 }
61