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> |
| 25 | #include "flash.h" |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <errno.h> |
| 32 | #include <inttypes.h> |
| 33 | #include <termios.h> |
| 34 | |
| 35 | int sp_fd; |
| 36 | |
| 37 | void __attribute__((noreturn)) sp_die(char *msg) |
| 38 | { |
| 39 | perror(msg); |
| 40 | exit(1); |
| 41 | } |
| 42 | |
| 43 | struct baudentry { |
| 44 | int flag; |
| 45 | unsigned int baud; |
| 46 | }; |
| 47 | |
| 48 | /* I'd like if the C preprocessor could have directives in macros */ |
| 49 | #define BAUDENTRY(baud) { B##baud, baud }, |
| 50 | static const struct baudentry sp_baudtable[] = { |
| 51 | BAUDENTRY(9600) |
| 52 | BAUDENTRY(19200) |
| 53 | BAUDENTRY(38400) |
| 54 | BAUDENTRY(57600) |
| 55 | BAUDENTRY(115200) |
| 56 | #ifdef B230400 |
| 57 | BAUDENTRY(230400) |
| 58 | #endif |
| 59 | #ifdef B460800 |
| 60 | BAUDENTRY(460800) |
| 61 | #endif |
| 62 | #ifdef B500000 |
| 63 | BAUDENTRY(500000) |
| 64 | #endif |
| 65 | #ifdef B576000 |
| 66 | BAUDENTRY(576000) |
| 67 | #endif |
| 68 | #ifdef B921600 |
| 69 | BAUDENTRY(921600) |
| 70 | #endif |
| 71 | #ifdef B1000000 |
| 72 | BAUDENTRY(1000000) |
| 73 | #endif |
| 74 | #ifdef B1152000 |
| 75 | BAUDENTRY(1152000) |
| 76 | #endif |
| 77 | #ifdef B1500000 |
| 78 | BAUDENTRY(1500000) |
| 79 | #endif |
| 80 | #ifdef B2000000 |
| 81 | BAUDENTRY(2000000) |
| 82 | #endif |
| 83 | #ifdef B2500000 |
| 84 | BAUDENTRY(2500000) |
| 85 | #endif |
| 86 | #ifdef B3000000 |
| 87 | BAUDENTRY(3000000) |
| 88 | #endif |
| 89 | #ifdef B3500000 |
| 90 | BAUDENTRY(3500000) |
| 91 | #endif |
| 92 | #ifdef B4000000 |
| 93 | BAUDENTRY(4000000) |
| 94 | #endif |
| 95 | {0, 0} /* Terminator */ |
| 96 | }; |
| 97 | |
| 98 | int sp_openserport(char *dev, unsigned int baud) |
| 99 | { |
| 100 | struct termios options; |
| 101 | int fd, i; |
| 102 | fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); |
| 103 | if (fd < 0) |
| 104 | sp_die("Error: cannot open serial port"); |
| 105 | fcntl(fd, F_SETFL, 0); |
| 106 | tcgetattr(fd, &options); |
| 107 | for (i = 0;; i++) { |
| 108 | if (sp_baudtable[i].baud == 0) { |
| 109 | close(fd); |
| 110 | fprintf(stderr, |
| 111 | "Error: cannot configure for baudrate %d\n", |
| 112 | baud); |
| 113 | exit(1); |
| 114 | } |
| 115 | if (sp_baudtable[i].baud == baud) { |
| 116 | cfsetispeed(&options, sp_baudtable[i].flag); |
| 117 | cfsetospeed(&options, sp_baudtable[i].flag); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 122 | options.c_cflag |= (CS8 | CLOCAL | CREAD); |
| 123 | options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 124 | options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR); |
| 125 | options.c_oflag &= ~OPOST; |
| 126 | tcsetattr(fd, TCSANOW, &options); |
| 127 | return fd; |
| 128 | } |
| 129 | |
| 130 | void sp_flush_incoming(void) |
| 131 | { |
| 132 | int i; |
| 133 | for (i=0;i<100;i++) { /* In case the device doesnt do EAGAIN, just read 0 */ |
| 134 | unsigned char flush[16]; |
| 135 | ssize_t rv; |
| 136 | rv = read(sp_fd, flush, sizeof(flush)); |
| 137 | if ((rv == -1) && (errno == EAGAIN)) |
| 138 | break; |
| 139 | if (rv == -1) |
| 140 | sp_die("flush read"); |
| 141 | } |
| 142 | return; |
| 143 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame^] | 144 | |
| 145 | int serialport_shutdown(void) |
| 146 | { |
| 147 | close(sp_fd); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int serialport_write(unsigned char *buf, unsigned int writecnt) |
| 152 | { |
| 153 | int tmp = 0; |
| 154 | |
| 155 | while (tmp != writecnt) { |
| 156 | tmp = write(sp_fd, buf + tmp, writecnt - tmp); |
| 157 | if (tmp == -1) |
| 158 | return 1; |
| 159 | if (!tmp) |
| 160 | printf_debug("Empty write\n"); |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 167 | { |
| 168 | int tmp = 0; |
| 169 | |
| 170 | while (tmp != readcnt) { |
| 171 | tmp = read(sp_fd, buf + tmp, readcnt - tmp); |
| 172 | if (tmp == -1) |
| 173 | return 1; |
| 174 | if (!tmp) |
| 175 | printf_debug("Empty read\n"); |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int serialport_discard_read(void) |
| 182 | { |
| 183 | int flags; |
| 184 | |
| 185 | printf_debug("%s\n", __func__); |
| 186 | flags = fcntl(sp_fd, F_GETFL); |
| 187 | flags |= O_NONBLOCK; |
| 188 | fcntl(sp_fd, F_SETFL, flags); |
| 189 | sp_flush_incoming(); |
| 190 | flags &= ~O_NONBLOCK; |
| 191 | fcntl(sp_fd, F_SETFL, flags); |
| 192 | |
| 193 | return 0; |
| 194 | } |