Peter Stuge | b8ee2d6 | 2022-12-11 16:20:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2022 Peter Stuge <peter@stuge.se> |
| 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 <termios.h> |
| 18 | #include <sys/ioctl.h> |
| 19 | #include <IOKit/serial/ioss.h> |
| 20 | #include <errno.h> |
| 21 | |
| 22 | #include "custom_baud.h" |
| 23 | |
| 24 | int use_custom_baud(unsigned int baud, const struct baudentry *baudtable) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | if (baud > 230400) |
| 29 | return 1; |
| 30 | |
| 31 | for (i = 0; baudtable[i].baud; i++) { |
| 32 | if (baudtable[i].baud == baud) |
| 33 | return 0; |
| 34 | |
| 35 | if (baudtable[i].baud > baud) |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | int set_custom_baudrate(int fd, unsigned int baud, const enum custom_baud_stage stage, void *tio_wanted) |
| 43 | { |
| 44 | struct termios *wanted; |
| 45 | speed_t speed; |
| 46 | |
| 47 | switch (stage) { |
| 48 | case BEFORE_FLAGS: |
| 49 | break; |
| 50 | |
| 51 | case WITH_FLAGS: |
| 52 | wanted = tio_wanted; |
| 53 | return cfsetspeed(wanted, B19200); |
| 54 | |
| 55 | case AFTER_FLAGS: |
| 56 | speed = baud; |
| 57 | return ioctl(fd, IOSSIOSPEED, &speed); |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |