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