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