Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [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 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "platform.h" |
| 22 | #include "custom_baud.h" |
| 23 | |
| 24 | #if IS_LINUX |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <asm-generic/termbits.h> |
| 28 | #include <asm-generic/ioctls.h> |
| 29 | |
| 30 | /* |
| 31 | * This include hell above is why this is in a separate source file. See eg. |
| 32 | * https://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/ |
| 33 | * https://stackoverflow.com/questions/12646324/how-to-set-a-custom-baud-rate-on-linux |
| 34 | * https://github.com/jbkim/Linux-custom-baud-rate |
| 35 | * for more info. |
| 36 | */ |
| 37 | |
| 38 | int set_custom_baudrate(int fd, unsigned int baud) |
| 39 | { |
| 40 | struct termios2 tio; |
| 41 | if (ioctl(fd, TCGETS2, &tio)) { |
| 42 | return -1; |
| 43 | } |
| 44 | tio.c_cflag &= ~CBAUD; |
| 45 | tio.c_cflag |= BOTHER; |
| 46 | tio.c_ispeed = baud; |
| 47 | tio.c_ospeed = baud; |
| 48 | return ioctl(fd, TCSETS2, &tio); |
| 49 | } |
| 50 | |
| 51 | int use_custom_baud(unsigned int baud, const struct baudentry *baudtable) |
| 52 | { |
| 53 | int i; |
| 54 | for (i = 0; baudtable[i].baud; i++) { |
| 55 | if (baudtable[i].baud == baud) |
| 56 | return 0; |
| 57 | |
| 58 | if (baudtable[i].baud > baud) |
| 59 | return 1; |
| 60 | } |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | #else |
| 65 | #include <errno.h> |
| 66 | |
| 67 | /* Stub, should not get called. */ |
| 68 | int set_custom_baudrate(int fd, unsigned int baud) |
| 69 | { |
| 70 | errno = ENOSYS; /* Hoping "Function not supported" will make you look here. */ |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | int use_custom_baud(unsigned int baud, const struct baudentry *baudtable) |
| 75 | { |
| 76 | return 0; |
| 77 | } |
| 78 | #endif |