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