Thomas Heijligen | 140c126 | 2021-09-27 15:12:26 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | int set_custom_baudrate(int fd, unsigned int baud) |
| 33 | { |
| 34 | struct termios2 tio; |
| 35 | if (ioctl(fd, TCGETS2, &tio)) { |
| 36 | return -1; |
| 37 | } |
| 38 | tio.c_cflag &= ~CBAUD; |
| 39 | tio.c_cflag |= BOTHER; |
| 40 | tio.c_ispeed = baud; |
| 41 | tio.c_ospeed = baud; |
| 42 | return ioctl(fd, TCSETS2, &tio); |
| 43 | } |
| 44 | |
| 45 | int use_custom_baud(unsigned int baud, const struct baudentry *baudtable) |
| 46 | { |
| 47 | int i; |
| 48 | for (i = 0; baudtable[i].baud; i++) { |
| 49 | if (baudtable[i].baud == baud) |
| 50 | return 0; |
| 51 | |
| 52 | if (baudtable[i].baud > baud) |
| 53 | return 1; |
| 54 | } |
| 55 | return 1; |
| 56 | } |