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