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; |
| 184 | fcntl(fd, F_SETFL, 0); |
| 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 | const struct baudentry *entry = round_baud(baud); |
| 191 | if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) { |
| 192 | msg_perr_strerror("Could not set serial baud rate: "); |
| 193 | return 1; |
| 194 | } |
| 195 | wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 196 | wanted.c_cflag |= (CS8 | CLOCAL | CREAD); |
| 197 | wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 198 | wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR); |
| 199 | wanted.c_oflag &= ~OPOST; |
| 200 | if (tcsetattr(fd, TCSANOW, &wanted) != 0) { |
| 201 | msg_perr_strerror("Could not change serial port configuration: "); |
| 202 | return 1; |
| 203 | } |
| 204 | if (tcgetattr(fd, &observed) != 0) { |
| 205 | msg_perr_strerror("Could not fetch new serial port configuration: "); |
| 206 | return 1; |
| 207 | } |
| 208 | if (observed.c_cflag != wanted.c_cflag || |
| 209 | observed.c_lflag != wanted.c_lflag || |
| 210 | observed.c_iflag != wanted.c_iflag || |
| 211 | observed.c_oflag != wanted.c_oflag || |
| 212 | cfgetispeed(&observed) != cfgetispeed(&wanted)) { |
| 213 | msg_perr("%s: Some requested options did not stick.\n", __func__); |
| 214 | return 1; |
| 215 | } |
| 216 | msg_pdbg("Baud rate is %d now.\n", entry->baud); |
| 217 | #endif |
| 218 | return 0; |
| 219 | } |
| 220 | |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 221 | fdtype sp_openserport(char *dev, unsigned int baud) |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 222 | { |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 223 | fdtype fd; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 224 | #ifdef _WIN32 |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 225 | char *dev2 = dev; |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 226 | if ((strlen(dev) > 3) && |
| 227 | (tolower((unsigned char)dev[0]) == 'c') && |
Carl-Daniel Hailfinger | 9e3a6c4 | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 228 | (tolower((unsigned char)dev[1]) == 'o') && |
| 229 | (tolower((unsigned char)dev[2]) == 'm')) { |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 230 | dev2 = malloc(strlen(dev) + 5); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 231 | if (!dev2) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 232 | msg_perr_strerror("Out of memory: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 233 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 234 | } |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 235 | strcpy(dev2, "\\\\.\\"); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 236 | strcpy(dev2 + 4, dev); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 237 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 238 | fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 239 | OPEN_EXISTING, 0, NULL); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 240 | if (dev2 != dev) |
| 241 | free(dev2); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 242 | if (fd == INVALID_HANDLE_VALUE) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 243 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 244 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 245 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 246 | if (serialport_config(fd, baud) != 0) { |
| 247 | CloseHandle(fd); |
| 248 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 249 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 250 | return fd; |
| 251 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 252 | fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 253 | if (fd < 0) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 254 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 255 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 256 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 257 | if (serialport_config(fd, baud) != 0) { |
| 258 | close(fd); |
| 259 | return SER_INV_FD; |
Stefan Tauner | f966cc4 | 2013-04-01 00:45:57 +0000 | [diff] [blame] | 260 | } |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 261 | return fd; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 262 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 265 | void sp_set_pin(enum SP_PIN pin, int val) { |
| 266 | #ifdef _WIN32 |
| 267 | DWORD ctl; |
| 268 | |
| 269 | if(pin == PIN_TXD) { |
| 270 | ctl = val ? SETBREAK: CLRBREAK; |
| 271 | } |
| 272 | else if(pin == PIN_DTR) { |
| 273 | ctl = val ? SETDTR: CLRDTR; |
| 274 | } |
| 275 | else { |
| 276 | ctl = val ? SETRTS: CLRRTS; |
| 277 | } |
| 278 | EscapeCommFunction(sp_fd, ctl); |
| 279 | #else |
| 280 | int ctl, s; |
| 281 | |
| 282 | if(pin == PIN_TXD) { |
| 283 | ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0); |
| 284 | } |
| 285 | else { |
| 286 | s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS; |
| 287 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 288 | |
| 289 | if (val) { |
| 290 | ctl |= s; |
| 291 | } |
| 292 | else { |
| 293 | ctl &= ~s; |
| 294 | } |
| 295 | ioctl(sp_fd, TIOCMSET, &ctl); |
| 296 | } |
| 297 | #endif |
| 298 | } |
| 299 | |
| 300 | int sp_get_pin(enum SP_PIN pin) { |
| 301 | int s; |
| 302 | #ifdef _WIN32 |
| 303 | DWORD ctl; |
| 304 | |
| 305 | s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON; |
| 306 | GetCommModemStatus(sp_fd, &ctl); |
| 307 | #else |
| 308 | int ctl; |
| 309 | s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR; |
| 310 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 311 | #endif |
| 312 | |
| 313 | return ((ctl & s) ? 1 : 0); |
| 314 | |
| 315 | } |
| 316 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 317 | void sp_flush_incoming(void) |
| 318 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 319 | #ifdef _WIN32 |
| 320 | PurgeComm(sp_fd, PURGE_RXCLEAR); |
| 321 | #else |
Niklas Söderlund | 7145a50 | 2012-09-07 07:07:07 +0000 | [diff] [blame] | 322 | /* FIXME: error handling */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 323 | tcflush(sp_fd, TCIFLUSH); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 324 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 325 | return; |
| 326 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 327 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 328 | int serialport_shutdown(void *data) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 329 | { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 330 | #ifdef _WIN32 |
| 331 | CloseHandle(sp_fd); |
| 332 | #else |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 333 | close(sp_fd); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 334 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int serialport_write(unsigned char *buf, unsigned int writecnt) |
| 339 | { |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 340 | #ifdef _WIN32 |
| 341 | DWORD tmp = 0; |
| 342 | #else |
| 343 | ssize_t tmp = 0; |
| 344 | #endif |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 345 | unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */ |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 346 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 347 | while (writecnt > 0) { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 348 | #ifdef _WIN32 |
| 349 | WriteFile(sp_fd, buf, writecnt, &tmp, NULL); |
| 350 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 351 | tmp = write(sp_fd, buf, writecnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 352 | #endif |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 353 | if (tmp == -1) { |
| 354 | msg_perr("Serial port write error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 355 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 356 | } |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 357 | if (!tmp) { |
| 358 | msg_pdbg2("Empty write\n"); |
| 359 | empty_writes--; |
Urja Rannikko | f0111d2 | 2013-10-19 23:35:28 +0000 | [diff] [blame^] | 360 | internal_delay(500); |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 361 | if (empty_writes == 0) { |
| 362 | msg_perr("Serial port is unresponsive!\n"); |
| 363 | return 1; |
| 364 | } |
| 365 | } |
| 366 | writecnt -= tmp; |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 367 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 374 | { |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 375 | #ifdef _WIN32 |
| 376 | DWORD tmp = 0; |
| 377 | #else |
| 378 | ssize_t tmp = 0; |
| 379 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 380 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 381 | while (readcnt > 0) { |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 382 | #ifdef _WIN32 |
| 383 | ReadFile(sp_fd, buf, readcnt, &tmp, NULL); |
| 384 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 385 | tmp = read(sp_fd, buf, readcnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 386 | #endif |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 387 | if (tmp == -1) { |
| 388 | msg_perr("Serial port read error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 389 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 390 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 391 | if (!tmp) |
Stefan Tauner | 79587f5 | 2013-04-01 00:45:51 +0000 | [diff] [blame] | 392 | msg_pdbg2("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 393 | readcnt -= tmp; |
| 394 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 399 | |
| 400 | /* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns |
| 401 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 402 | * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */ |
| 403 | int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read) |
| 404 | { |
| 405 | int ret = 1; |
| 406 | /* disable blocked i/o and declare platform-specific variables */ |
| 407 | #ifdef _WIN32 |
| 408 | DWORD rv; |
| 409 | COMMTIMEOUTS oldTimeout; |
| 410 | COMMTIMEOUTS newTimeout = { |
| 411 | .ReadIntervalTimeout = MAXDWORD, |
| 412 | .ReadTotalTimeoutMultiplier = 0, |
| 413 | .ReadTotalTimeoutConstant = 0, |
| 414 | .WriteTotalTimeoutMultiplier = 0, |
| 415 | .WriteTotalTimeoutConstant = 0 |
| 416 | }; |
| 417 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 418 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 419 | return -1; |
| 420 | } |
| 421 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
| 422 | #else |
| 423 | ssize_t rv; |
| 424 | const int flags = fcntl(sp_fd, F_GETFL); |
| 425 | if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| 426 | #endif |
| 427 | msg_perr_strerror("Could not set serial port timeout settings %s"); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | int i; |
| 432 | int rd_bytes = 0; |
| 433 | for (i = 0; i < timeout; i++) { |
| 434 | msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes); |
| 435 | #ifdef _WIN32 |
| 436 | ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL); |
| 437 | msg_pspew("read %lu bytes\n", rv); |
| 438 | #else |
| 439 | rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes); |
| 440 | msg_pspew("read %zd bytes\n", rv); |
| 441 | #endif |
| 442 | if ((rv == -1) && (errno != EAGAIN)) { |
| 443 | msg_perr_strerror("Serial port read error: "); |
| 444 | ret = -1; |
| 445 | break; |
| 446 | } |
| 447 | if (rv > 0) |
| 448 | rd_bytes += rv; |
| 449 | if (rd_bytes == readcnt) { |
| 450 | ret = 0; |
| 451 | break; |
| 452 | } |
| 453 | internal_delay(1000); /* 1ms units */ |
| 454 | } |
| 455 | if (really_read != NULL) |
| 456 | *really_read = rd_bytes; |
| 457 | |
| 458 | /* restore original blocking behavior */ |
| 459 | #ifdef _WIN32 |
| 460 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
| 461 | #else |
| 462 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 463 | #endif |
| 464 | msg_perr_strerror("Could not restore serial port timeout settings: %s\n"); |
| 465 | ret = -1; |
| 466 | } |
| 467 | return ret; |
| 468 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 469 | |
| 470 | /* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns |
| 471 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 472 | * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */ |
| 473 | int serialport_write_nonblock(unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote) |
| 474 | { |
| 475 | int ret = 1; |
| 476 | /* disable blocked i/o and declare platform-specific variables */ |
| 477 | #ifdef _WIN32 |
| 478 | DWORD rv; |
| 479 | COMMTIMEOUTS oldTimeout; |
| 480 | COMMTIMEOUTS newTimeout = { |
| 481 | .ReadIntervalTimeout = MAXDWORD, |
| 482 | .ReadTotalTimeoutMultiplier = 0, |
| 483 | .ReadTotalTimeoutConstant = 0, |
| 484 | .WriteTotalTimeoutMultiplier = 0, |
| 485 | .WriteTotalTimeoutConstant = 0 |
| 486 | }; |
| 487 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 488 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 489 | return -1; |
| 490 | } |
| 491 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
| 492 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 493 | return -1; |
| 494 | } |
| 495 | #else |
| 496 | ssize_t rv; |
| 497 | const int flags = fcntl(sp_fd, F_GETFL); |
| 498 | fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK); |
| 499 | #endif |
| 500 | |
| 501 | int i; |
| 502 | int wr_bytes = 0; |
| 503 | for (i = 0; i < timeout; i++) { |
| 504 | msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes); |
| 505 | #ifdef _WIN32 |
| 506 | WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL); |
| 507 | msg_pspew("wrote %lu bytes\n", rv); |
| 508 | #else |
| 509 | rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes); |
| 510 | msg_pspew("wrote %zd bytes\n", rv); |
| 511 | #endif |
| 512 | if ((rv == -1) && (errno != EAGAIN)) { |
| 513 | msg_perr_strerror("Serial port write error: "); |
| 514 | ret = -1; |
| 515 | break; |
| 516 | } |
| 517 | if (rv > 0) { |
| 518 | wr_bytes += rv; |
| 519 | if (wr_bytes == writecnt) { |
| 520 | msg_pspew("write successful\n"); |
| 521 | ret = 0; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | internal_delay(1000); /* 1ms units */ |
| 526 | } |
| 527 | if (really_wrote != NULL) |
| 528 | *really_wrote = wr_bytes; |
| 529 | |
| 530 | /* restore original blocking behavior */ |
| 531 | #ifdef _WIN32 |
| 532 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
| 533 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
| 534 | #else |
| 535 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 536 | #endif |
| 537 | return -1; |
| 538 | } |
| 539 | return ret; |
| 540 | } |