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