Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com> |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 22 | #include "platform.h" |
| 23 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <ctype.h> |
| 29 | #include <fcntl.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <errno.h> |
| 32 | #include <inttypes.h> |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 33 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 34 | #include <conio.h> |
| 35 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 36 | #include <termios.h> |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/ioctl.h> |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 40 | #endif |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 41 | #include "flash.h" |
| 42 | #include "programmer.h" |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 43 | |
Stefan Tauner | e33c40e | 2013-04-13 00:29:30 +0000 | [diff] [blame] | 44 | fdtype sp_fd = SER_INV_FD; |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 45 | |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 46 | /* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to |
| 47 | * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3) |
| 48 | * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h |
| 49 | * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal |
| 50 | * with numerical user input. |
| 51 | * |
| 52 | * On Linux there is a non-standard way to use arbitrary baud rates that flashrom does not support (yet), cf. |
| 53 | * http://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/ |
| 54 | * |
| 55 | * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for |
| 56 | * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see |
| 57 | * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx |
| 58 | */ |
| 59 | #if !IS_WINDOWS |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 60 | struct baudentry { |
| 61 | int flag; |
| 62 | unsigned int baud; |
| 63 | }; |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 64 | #define BAUDENTRY(baud) { B##baud, baud }, |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 65 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 66 | static const struct baudentry sp_baudtable[] = { |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 67 | BAUDENTRY(9600) /* unconditional default */ |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 68 | #ifdef B19200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 69 | BAUDENTRY(19200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 70 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 71 | #ifdef B38400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 72 | BAUDENTRY(38400) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 73 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 74 | #ifdef B57600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 75 | BAUDENTRY(57600) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 76 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 77 | #ifdef B115200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 78 | BAUDENTRY(115200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 79 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 80 | #ifdef B230400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 81 | BAUDENTRY(230400) |
| 82 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 83 | #ifdef B460800 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 84 | BAUDENTRY(460800) |
| 85 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 86 | #ifdef B500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 87 | BAUDENTRY(500000) |
| 88 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 89 | #ifdef B576000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 90 | BAUDENTRY(576000) |
| 91 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 92 | #ifdef B921600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 93 | BAUDENTRY(921600) |
| 94 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 95 | #ifdef B1000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 96 | BAUDENTRY(1000000) |
| 97 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 98 | #ifdef B1152000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 99 | BAUDENTRY(1152000) |
| 100 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 101 | #ifdef B1500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 102 | BAUDENTRY(1500000) |
| 103 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 104 | #ifdef B2000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 105 | BAUDENTRY(2000000) |
| 106 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 107 | #ifdef B2500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 108 | BAUDENTRY(2500000) |
| 109 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 110 | #ifdef B3000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 111 | BAUDENTRY(3000000) |
| 112 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 113 | #ifdef B3500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 114 | BAUDENTRY(3500000) |
| 115 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 116 | #ifdef B4000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 117 | BAUDENTRY(4000000) |
| 118 | #endif |
| 119 | {0, 0} /* Terminator */ |
| 120 | }; |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 121 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 122 | static const struct baudentry *round_baud(unsigned int baud) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */ |
| 126 | for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) { |
| 127 | if (sp_baudtable[i].baud == baud) |
| 128 | return &sp_baudtable[i]; |
| 129 | |
| 130 | if (sp_baudtable[i].baud < baud) { |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 131 | msg_pwarn("Warning: given baudrate %d rounded down to %d.\n", |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 132 | baud, sp_baudtable[i].baud); |
| 133 | return &sp_baudtable[i]; |
| 134 | } |
| 135 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 136 | msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud); |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 137 | return &sp_baudtable[0]; |
| 138 | } |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 139 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 140 | |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 141 | /* Uses msg_perr to print the last system error. |
| 142 | * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via |
| 143 | * strerror() or FormatMessage() and ending with a linebreak. */ |
| 144 | static void msg_perr_strerror(const char *msg) |
| 145 | { |
| 146 | msg_perr("Error: %s", msg); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 147 | #if IS_WINDOWS |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 148 | char *lpMsgBuf; |
| 149 | DWORD nErr = GetLastError(); |
| 150 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr, |
| 151 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); |
| 152 | msg_perr(lpMsgBuf); |
| 153 | /* At least some formatted messages contain a line break at the end. Make sure to always print one */ |
| 154 | if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n') |
| 155 | msg_perr("\n"); |
| 156 | LocalFree(lpMsgBuf); |
| 157 | #else |
| 158 | msg_perr("%s\n", strerror(errno)); |
| 159 | #endif |
| 160 | } |
| 161 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 162 | int serialport_config(fdtype fd, int baud) |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 163 | { |
| 164 | if (fd == SER_INV_FD) { |
| 165 | msg_perr("%s: File descriptor is invalid.\n", __func__); |
| 166 | return 1; |
| 167 | } |
| 168 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 169 | #if IS_WINDOWS |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 170 | DCB dcb; |
| 171 | if (!GetCommState(fd, &dcb)) { |
| 172 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 173 | return 1; |
| 174 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 175 | if (baud >= 0) { |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 176 | dcb.BaudRate = baud; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 177 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 178 | dcb.ByteSize = 8; |
| 179 | dcb.Parity = NOPARITY; |
| 180 | dcb.StopBits = ONESTOPBIT; |
| 181 | if (!SetCommState(fd, &dcb)) { |
| 182 | msg_perr_strerror("Could not change serial port configuration: "); |
| 183 | return 1; |
| 184 | } |
| 185 | if (!GetCommState(fd, &dcb)) { |
| 186 | msg_perr_strerror("Could not fetch new serial port configuration: "); |
| 187 | return 1; |
| 188 | } |
| 189 | msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate); |
| 190 | #else |
| 191 | struct termios wanted, observed; |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 192 | if (tcgetattr(fd, &observed) != 0) { |
| 193 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 194 | return 1; |
| 195 | } |
| 196 | wanted = observed; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 197 | if (baud >= 0) { |
| 198 | const struct baudentry *entry = round_baud(baud); |
| 199 | if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) { |
| 200 | msg_perr_strerror("Could not set serial baud rate: "); |
| 201 | return 1; |
| 202 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 203 | } |
| 204 | wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 205 | wanted.c_cflag |= (CS8 | CLOCAL | CREAD); |
| 206 | wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 207 | wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR); |
| 208 | wanted.c_oflag &= ~OPOST; |
| 209 | if (tcsetattr(fd, TCSANOW, &wanted) != 0) { |
| 210 | msg_perr_strerror("Could not change serial port configuration: "); |
| 211 | return 1; |
| 212 | } |
| 213 | if (tcgetattr(fd, &observed) != 0) { |
| 214 | msg_perr_strerror("Could not fetch new serial port configuration: "); |
| 215 | return 1; |
| 216 | } |
| 217 | if (observed.c_cflag != wanted.c_cflag || |
| 218 | observed.c_lflag != wanted.c_lflag || |
| 219 | observed.c_iflag != wanted.c_iflag || |
Stefan Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 220 | observed.c_oflag != wanted.c_oflag) { |
| 221 | msg_pwarn("Some requested serial options did not stick, continuing anyway.\n"); |
| 222 | msg_pdbg(" observed wanted\n" |
| 223 | "c_cflag: 0x%08lX 0x%08lX\n" |
| 224 | "c_lflag: 0x%08lX 0x%08lX\n" |
| 225 | "c_iflag: 0x%08lX 0x%08lX\n" |
| 226 | "c_oflag: 0x%08lX 0x%08lX\n", |
| 227 | (long)observed.c_cflag, (long)wanted.c_cflag, |
| 228 | (long)observed.c_lflag, (long)wanted.c_lflag, |
| 229 | (long)observed.c_iflag, (long)wanted.c_iflag, |
| 230 | (long)observed.c_oflag, (long)wanted.c_oflag |
| 231 | ); |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 232 | } |
Stefan Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 233 | if (cfgetispeed(&observed) != cfgetispeed(&wanted) || |
| 234 | cfgetospeed(&observed) != cfgetospeed(&wanted)) { |
| 235 | msg_pwarn("Could not set baud rates exactly.\n"); |
| 236 | msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n", |
| 237 | (long)cfgetispeed(&observed), (long)cfgetospeed(&observed)); |
| 238 | } |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 239 | // FIXME: display actual baud rate - at least if none was specified by the user. |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 240 | #endif |
| 241 | return 0; |
| 242 | } |
| 243 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 244 | fdtype sp_openserport(char *dev, int baud) |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 245 | { |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 246 | fdtype fd; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 247 | #if IS_WINDOWS |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 248 | char *dev2 = dev; |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 249 | if ((strlen(dev) > 3) && |
| 250 | (tolower((unsigned char)dev[0]) == 'c') && |
Carl-Daniel Hailfinger | 9e3a6c4 | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 251 | (tolower((unsigned char)dev[1]) == 'o') && |
| 252 | (tolower((unsigned char)dev[2]) == 'm')) { |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 253 | dev2 = malloc(strlen(dev) + 5); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 254 | if (!dev2) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 255 | msg_perr_strerror("Out of memory: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 256 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 257 | } |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 258 | strcpy(dev2, "\\\\.\\"); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 259 | strcpy(dev2 + 4, dev); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 260 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 261 | fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 262 | OPEN_EXISTING, 0, NULL); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 263 | if (dev2 != dev) |
| 264 | free(dev2); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 265 | if (fd == INVALID_HANDLE_VALUE) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 266 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 267 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 268 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 269 | if (serialport_config(fd, baud) != 0) { |
| 270 | CloseHandle(fd); |
| 271 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 272 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 273 | return fd; |
| 274 | #else |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 275 | fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 276 | if (fd < 0) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 277 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 278 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 279 | } |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 280 | |
| 281 | /* Ensure that we use blocking I/O */ |
| 282 | const int flags = fcntl(fd, F_GETFL); |
| 283 | if (flags == -1) { |
| 284 | msg_perr_strerror("Could not get serial port mode: "); |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 285 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 286 | } |
| 287 | if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
| 288 | msg_perr_strerror("Could not set serial port mode to blocking: "); |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 289 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 292 | if (serialport_config(fd, baud) != 0) { |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 293 | goto err; |
Stefan Tauner | f966cc4 | 2013-04-01 00:45:57 +0000 | [diff] [blame] | 294 | } |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 295 | return fd; |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 296 | err: |
| 297 | close(fd); |
| 298 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 299 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 302 | void sp_set_pin(enum SP_PIN pin, int val) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 303 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 304 | DWORD ctl; |
| 305 | |
| 306 | if(pin == PIN_TXD) { |
| 307 | ctl = val ? SETBREAK: CLRBREAK; |
| 308 | } |
| 309 | else if(pin == PIN_DTR) { |
| 310 | ctl = val ? SETDTR: CLRDTR; |
| 311 | } |
| 312 | else { |
| 313 | ctl = val ? SETRTS: CLRRTS; |
| 314 | } |
| 315 | EscapeCommFunction(sp_fd, ctl); |
| 316 | #else |
| 317 | int ctl, s; |
| 318 | |
| 319 | if(pin == PIN_TXD) { |
| 320 | ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0); |
| 321 | } |
| 322 | else { |
| 323 | s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS; |
| 324 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 325 | |
| 326 | if (val) { |
| 327 | ctl |= s; |
| 328 | } |
| 329 | else { |
| 330 | ctl &= ~s; |
| 331 | } |
| 332 | ioctl(sp_fd, TIOCMSET, &ctl); |
| 333 | } |
| 334 | #endif |
| 335 | } |
| 336 | |
| 337 | int sp_get_pin(enum SP_PIN pin) { |
| 338 | int s; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 339 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 340 | DWORD ctl; |
| 341 | |
| 342 | s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON; |
| 343 | GetCommModemStatus(sp_fd, &ctl); |
| 344 | #else |
| 345 | int ctl; |
| 346 | s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR; |
| 347 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 348 | #endif |
| 349 | |
| 350 | return ((ctl & s) ? 1 : 0); |
| 351 | |
| 352 | } |
| 353 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 354 | void sp_flush_incoming(void) |
| 355 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 356 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 357 | PurgeComm(sp_fd, PURGE_RXCLEAR); |
| 358 | #else |
Niklas Söderlund | 7145a50 | 2012-09-07 07:07:07 +0000 | [diff] [blame] | 359 | /* FIXME: error handling */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 360 | tcflush(sp_fd, TCIFLUSH); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 361 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 364 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 365 | int serialport_shutdown(void *data) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 366 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 367 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 368 | CloseHandle(sp_fd); |
| 369 | #else |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 370 | close(sp_fd); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 371 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 375 | int serialport_write(const unsigned char *buf, unsigned int writecnt) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 376 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 377 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 378 | DWORD tmp = 0; |
| 379 | #else |
| 380 | ssize_t tmp = 0; |
| 381 | #endif |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 382 | unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */ |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 383 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 384 | while (writecnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 385 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 386 | WriteFile(sp_fd, buf, writecnt, &tmp, NULL); |
| 387 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 388 | tmp = write(sp_fd, buf, writecnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 389 | #endif |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 390 | if (tmp == -1) { |
| 391 | msg_perr("Serial port write error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 392 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 393 | } |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 394 | if (!tmp) { |
| 395 | msg_pdbg2("Empty write\n"); |
| 396 | empty_writes--; |
Urja Rannikko | f0111d2 | 2013-10-19 23:35:28 +0000 | [diff] [blame] | 397 | internal_delay(500); |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 398 | if (empty_writes == 0) { |
| 399 | msg_perr("Serial port is unresponsive!\n"); |
| 400 | return 1; |
| 401 | } |
| 402 | } |
| 403 | writecnt -= tmp; |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 404 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 411 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 412 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 413 | DWORD tmp = 0; |
| 414 | #else |
| 415 | ssize_t tmp = 0; |
| 416 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 417 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 418 | while (readcnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 419 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 420 | ReadFile(sp_fd, buf, readcnt, &tmp, NULL); |
| 421 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 422 | tmp = read(sp_fd, buf, readcnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 423 | #endif |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 424 | if (tmp == -1) { |
| 425 | msg_perr("Serial port read error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 426 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 427 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 428 | if (!tmp) |
Stefan Tauner | 79587f5 | 2013-04-01 00:45:51 +0000 | [diff] [blame] | 429 | msg_pdbg2("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 430 | readcnt -= tmp; |
| 431 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 436 | |
| 437 | /* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns |
| 438 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 439 | * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */ |
| 440 | int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read) |
| 441 | { |
| 442 | int ret = 1; |
| 443 | /* disable blocked i/o and declare platform-specific variables */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 444 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 445 | DWORD rv; |
| 446 | COMMTIMEOUTS oldTimeout; |
| 447 | COMMTIMEOUTS newTimeout = { |
| 448 | .ReadIntervalTimeout = MAXDWORD, |
| 449 | .ReadTotalTimeoutMultiplier = 0, |
| 450 | .ReadTotalTimeoutConstant = 0, |
| 451 | .WriteTotalTimeoutMultiplier = 0, |
| 452 | .WriteTotalTimeoutConstant = 0 |
| 453 | }; |
| 454 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 455 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 456 | return -1; |
| 457 | } |
| 458 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 459 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 460 | return -1; |
| 461 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 462 | #else |
| 463 | ssize_t rv; |
| 464 | const int flags = fcntl(sp_fd, F_GETFL); |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 465 | if (flags == -1) { |
| 466 | msg_perr_strerror("Could not get serial port mode: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 467 | return -1; |
| 468 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 469 | if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| 470 | msg_perr_strerror("Could not set serial port mode to non-blocking: "); |
| 471 | return -1; |
| 472 | } |
| 473 | #endif |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 474 | |
| 475 | int i; |
| 476 | int rd_bytes = 0; |
| 477 | for (i = 0; i < timeout; i++) { |
| 478 | msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 479 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 480 | ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL); |
| 481 | msg_pspew("read %lu bytes\n", rv); |
| 482 | #else |
| 483 | rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes); |
| 484 | msg_pspew("read %zd bytes\n", rv); |
| 485 | #endif |
| 486 | if ((rv == -1) && (errno != EAGAIN)) { |
| 487 | msg_perr_strerror("Serial port read error: "); |
| 488 | ret = -1; |
| 489 | break; |
| 490 | } |
| 491 | if (rv > 0) |
| 492 | rd_bytes += rv; |
| 493 | if (rd_bytes == readcnt) { |
| 494 | ret = 0; |
| 495 | break; |
| 496 | } |
| 497 | internal_delay(1000); /* 1ms units */ |
| 498 | } |
| 499 | if (really_read != NULL) |
| 500 | *really_read = rd_bytes; |
| 501 | |
| 502 | /* restore original blocking behavior */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 503 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 504 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 505 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 506 | ret = -1; |
| 507 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 508 | #else |
| 509 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 510 | msg_perr_strerror("Could not restore serial port mode to blocking: "); |
| 511 | ret = -1; |
| 512 | } |
| 513 | #endif |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 514 | return ret; |
| 515 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 516 | |
| 517 | /* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns |
| 518 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 519 | * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 520 | int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote) |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 521 | { |
| 522 | int ret = 1; |
| 523 | /* disable blocked i/o and declare platform-specific variables */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 524 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 525 | DWORD rv; |
| 526 | COMMTIMEOUTS oldTimeout; |
| 527 | COMMTIMEOUTS newTimeout = { |
| 528 | .ReadIntervalTimeout = MAXDWORD, |
| 529 | .ReadTotalTimeoutMultiplier = 0, |
| 530 | .ReadTotalTimeoutConstant = 0, |
| 531 | .WriteTotalTimeoutMultiplier = 0, |
| 532 | .WriteTotalTimeoutConstant = 0 |
| 533 | }; |
| 534 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 535 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 536 | return -1; |
| 537 | } |
| 538 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
| 539 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 540 | return -1; |
| 541 | } |
| 542 | #else |
| 543 | ssize_t rv; |
| 544 | const int flags = fcntl(sp_fd, F_GETFL); |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 545 | if (flags == -1) { |
| 546 | msg_perr_strerror("Could not get serial port mode: "); |
| 547 | return -1; |
| 548 | } |
| 549 | if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| 550 | msg_perr_strerror("Could not set serial port mode to non-blocking: "); |
| 551 | return -1; |
| 552 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 553 | #endif |
| 554 | |
| 555 | int i; |
| 556 | int wr_bytes = 0; |
| 557 | for (i = 0; i < timeout; i++) { |
| 558 | msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 559 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 560 | WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL); |
| 561 | msg_pspew("wrote %lu bytes\n", rv); |
| 562 | #else |
| 563 | rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes); |
| 564 | msg_pspew("wrote %zd bytes\n", rv); |
| 565 | #endif |
| 566 | if ((rv == -1) && (errno != EAGAIN)) { |
| 567 | msg_perr_strerror("Serial port write error: "); |
| 568 | ret = -1; |
| 569 | break; |
| 570 | } |
| 571 | if (rv > 0) { |
| 572 | wr_bytes += rv; |
| 573 | if (wr_bytes == writecnt) { |
| 574 | msg_pspew("write successful\n"); |
| 575 | ret = 0; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | internal_delay(1000); /* 1ms units */ |
| 580 | } |
| 581 | if (really_wrote != NULL) |
| 582 | *really_wrote = wr_bytes; |
| 583 | |
| 584 | /* restore original blocking behavior */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 585 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 586 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
| 587 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 588 | return -1; |
| 589 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 590 | #else |
| 591 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 592 | msg_perr_strerror("Could not restore serial port blocking behavior: "); |
| 593 | return -1; |
| 594 | } |
| 595 | #endif |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 596 | return ret; |
| 597 | } |