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 | { |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame^] | 132 | tcflush(sp_fd, TCIFLUSH); |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 135 | |
| 136 | int serialport_shutdown(void) |
| 137 | { |
| 138 | close(sp_fd); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int serialport_write(unsigned char *buf, unsigned int writecnt) |
| 143 | { |
| 144 | int tmp = 0; |
| 145 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame^] | 146 | while (writecnt > 0) { |
| 147 | tmp = write(sp_fd, buf, writecnt); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 148 | if (tmp == -1) |
| 149 | return 1; |
| 150 | if (!tmp) |
| 151 | printf_debug("Empty write\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame^] | 152 | writecnt -= tmp; |
| 153 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 160 | { |
| 161 | int tmp = 0; |
| 162 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame^] | 163 | while (readcnt > 0) { |
| 164 | tmp = read(sp_fd, buf, readcnt); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 165 | if (tmp == -1) |
| 166 | return 1; |
| 167 | if (!tmp) |
| 168 | printf_debug("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame^] | 169 | readcnt -= tmp; |
| 170 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |