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