Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
Andrew Jeffery | 89a2878 | 2022-09-29 11:46:31 +0930 | [diff] [blame] | 4 | #include <errno.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 5 | #include <stdbool.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 8 | |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 9 | #include "crc-16-ccitt.h" |
| 10 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 11 | #ifdef HAVE_CONFIG_H |
| 12 | #include "config.h" |
| 13 | #endif |
| 14 | |
| 15 | #ifdef MCTP_HAVE_FILEIO |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 16 | #include <fcntl.h> |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 17 | #include <poll.h> |
| 18 | #include <unistd.h> |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 19 | #else |
| 20 | static const size_t write(int fd, void *buf, size_t len) |
| 21 | { |
| 22 | return -1; |
| 23 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #define pr_fmt(x) "serial: " x |
| 27 | |
Andrew Jeffery | 89a2878 | 2022-09-29 11:46:31 +0930 | [diff] [blame] | 28 | /* |
| 29 | * @fn: A function that will copy data from the buffer at src into the dst object |
| 30 | * @dst: An opaque object to pass as state to fn |
| 31 | * @src: A pointer to the buffer of data to copy to dst |
| 32 | * @len: The length of the data pointed to by src |
| 33 | * @return: 0 on succes, negative error code on failure |
| 34 | * |
| 35 | * Pre-condition: fn returns a write count or a negative error code |
| 36 | * Post-condition: All bytes written or an error has occurred |
| 37 | */ |
| 38 | #define mctp_write_all(fn, dst, src, len) \ |
| 39 | ({ \ |
| 40 | typeof(src) __src = src; \ |
| 41 | ssize_t wrote; \ |
| 42 | while (len) { \ |
| 43 | wrote = fn(dst, __src, len); \ |
| 44 | if (wrote < 0) \ |
| 45 | break; \ |
| 46 | __src += wrote; \ |
| 47 | len -= wrote; \ |
| 48 | } \ |
| 49 | len ? wrote : 0; \ |
| 50 | }) |
| 51 | |
| 52 | static ssize_t mctp_serial_write(int fildes, const void *buf, size_t nbyte) |
| 53 | { |
| 54 | ssize_t wrote; |
| 55 | |
| 56 | return ((wrote = write(fildes, buf, nbyte)) < 0) ? -errno : wrote; |
| 57 | } |
Andrew Jeffery | ad772b9 | 2020-01-10 16:04:18 +1030 | [diff] [blame] | 58 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 59 | #include "libmctp.h" |
| 60 | #include "libmctp-alloc.h" |
| 61 | #include "libmctp-log.h" |
| 62 | #include "libmctp-serial.h" |
Przemyslaw Czarnowski | ff25d7e | 2020-03-26 11:39:37 +0100 | [diff] [blame] | 63 | #include "container_of.h" |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 64 | |
| 65 | struct mctp_binding_serial { |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 66 | struct mctp_binding binding; |
| 67 | int fd; |
| 68 | unsigned long bus_id; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 69 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 70 | mctp_serial_tx_fn tx_fn; |
| 71 | void *tx_fn_data; |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 72 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 73 | /* receive buffer and state */ |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 74 | uint8_t rxbuf[1024]; |
| 75 | struct mctp_pktbuf *rx_pkt; |
| 76 | uint8_t rx_exp_len; |
| 77 | uint16_t rx_fcs; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 78 | uint16_t rx_fcs_calc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 79 | enum { |
| 80 | STATE_WAIT_SYNC_START, |
| 81 | STATE_WAIT_REVISION, |
| 82 | STATE_WAIT_LEN, |
| 83 | STATE_DATA, |
| 84 | STATE_DATA_ESCAPED, |
| 85 | STATE_WAIT_FCS1, |
| 86 | STATE_WAIT_FCS2, |
| 87 | STATE_WAIT_SYNC_END, |
| 88 | } rx_state; |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 89 | |
| 90 | /* temporary transmit buffer */ |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 91 | uint8_t txbuf[256]; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 92 | }; |
| 93 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 94 | #define binding_to_serial(b) \ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 95 | container_of(b, struct mctp_binding_serial, binding) |
| 96 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 97 | #define MCTP_SERIAL_REVISION 0x01 |
| 98 | #define MCTP_SERIAL_FRAMING_FLAG 0x7e |
| 99 | #define MCTP_SERIAL_ESCAPE 0x7d |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 100 | |
| 101 | struct mctp_serial_header { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 102 | uint8_t flag; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 103 | uint8_t revision; |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 104 | uint8_t len; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct mctp_serial_trailer { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 108 | uint8_t fcs_msb; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 109 | uint8_t fcs_lsb; |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 110 | uint8_t flag; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 111 | }; |
| 112 | |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 113 | static size_t mctp_serial_pkt_escape(struct mctp_pktbuf *pkt, uint8_t *buf) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 114 | { |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 115 | uint8_t total_len; |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 116 | uint8_t *p; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 117 | int i, j; |
| 118 | |
| 119 | total_len = pkt->end - pkt->mctp_hdr_off; |
| 120 | |
| 121 | p = (void *)mctp_pktbuf_hdr(pkt); |
| 122 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 123 | for (i = 0, j = 0; i < total_len; i++, j++) { |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 124 | uint8_t c = p[i]; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 125 | if (c == 0x7e || c == 0x7d) { |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 126 | if (buf) |
| 127 | buf[j] = 0x7d; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 128 | j++; |
| 129 | c ^= 0x20; |
| 130 | } |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 131 | if (buf) |
| 132 | buf[j] = c; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 133 | } |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 134 | |
| 135 | return j; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int mctp_binding_serial_tx(struct mctp_binding *b, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 139 | struct mctp_pktbuf *pkt) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 140 | { |
| 141 | struct mctp_binding_serial *serial = binding_to_serial(b); |
| 142 | struct mctp_serial_header *hdr; |
| 143 | struct mctp_serial_trailer *tlr; |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 144 | uint8_t *buf; |
| 145 | size_t len; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 146 | uint16_t fcs; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 147 | |
| 148 | /* the length field in the header excludes serial framing |
| 149 | * and escape sequences */ |
| 150 | len = mctp_pktbuf_size(pkt); |
| 151 | |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 152 | hdr = (void *)serial->txbuf; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 153 | hdr->flag = MCTP_SERIAL_FRAMING_FLAG; |
| 154 | hdr->revision = MCTP_SERIAL_REVISION; |
| 155 | hdr->len = len; |
| 156 | |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 157 | // Calculate fcs |
| 158 | fcs = crc_16_ccitt(FCS_INIT_16, (const uint8_t *)hdr + 1, 2); |
| 159 | fcs = crc_16_ccitt(fcs, (const uint8_t *)mctp_pktbuf_hdr(pkt), len); |
| 160 | |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 161 | buf = (void *)(hdr + 1); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 162 | |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 163 | len = mctp_serial_pkt_escape(pkt, NULL); |
| 164 | if (len + sizeof(*hdr) + sizeof(*tlr) > sizeof(serial->txbuf)) |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 165 | return -EMSGSIZE; |
Jeremy Kerr | c67605b | 2019-02-07 21:52:00 +0800 | [diff] [blame] | 166 | |
| 167 | mctp_serial_pkt_escape(pkt, buf); |
| 168 | |
| 169 | buf += len; |
| 170 | |
| 171 | tlr = (void *)buf; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 172 | tlr->flag = MCTP_SERIAL_FRAMING_FLAG; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 173 | tlr->fcs_msb = fcs >> 8; |
| 174 | tlr->fcs_lsb = fcs & 0xff; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 175 | |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 176 | len += sizeof(*hdr) + sizeof(*tlr); |
| 177 | |
Andrew Jeffery | ad772b9 | 2020-01-10 16:04:18 +1030 | [diff] [blame] | 178 | if (!serial->tx_fn) |
Andrew Jeffery | 89a2878 | 2022-09-29 11:46:31 +0930 | [diff] [blame] | 179 | return mctp_write_all(mctp_serial_write, serial->fd, |
| 180 | &serial->txbuf[0], len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 181 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 182 | return mctp_write_all(serial->tx_fn, serial->tx_fn_data, |
| 183 | &serial->txbuf[0], len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void mctp_serial_finish_packet(struct mctp_binding_serial *serial, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 187 | bool valid) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 188 | { |
| 189 | struct mctp_pktbuf *pkt = serial->rx_pkt; |
| 190 | assert(pkt); |
| 191 | |
| 192 | if (valid) |
Jeremy Kerr | 0a00dca | 2019-03-01 08:01:35 +0800 | [diff] [blame] | 193 | mctp_bus_rx(&serial->binding, pkt); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 194 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 195 | serial->rx_pkt = NULL; |
| 196 | } |
| 197 | |
| 198 | static void mctp_serial_start_packet(struct mctp_binding_serial *serial, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 199 | uint8_t len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 200 | { |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 201 | serial->rx_pkt = mctp_pktbuf_alloc(&serial->binding, len); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 204 | static void mctp_rx_consume_one(struct mctp_binding_serial *serial, uint8_t c) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 205 | { |
| 206 | struct mctp_pktbuf *pkt = serial->rx_pkt; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 207 | bool valid = false; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 208 | |
| 209 | mctp_prdebug("state: %d, char 0x%02x", serial->rx_state, c); |
| 210 | |
| 211 | assert(!pkt == (serial->rx_state == STATE_WAIT_SYNC_START || |
| 212 | serial->rx_state == STATE_WAIT_REVISION || |
| 213 | serial->rx_state == STATE_WAIT_LEN)); |
| 214 | |
| 215 | switch (serial->rx_state) { |
| 216 | case STATE_WAIT_SYNC_START: |
| 217 | if (c != MCTP_SERIAL_FRAMING_FLAG) { |
| 218 | mctp_prdebug("lost sync, dropping packet"); |
| 219 | if (pkt) |
| 220 | mctp_serial_finish_packet(serial, false); |
| 221 | } else { |
| 222 | serial->rx_state = STATE_WAIT_REVISION; |
| 223 | } |
| 224 | break; |
| 225 | |
| 226 | case STATE_WAIT_REVISION: |
| 227 | if (c == MCTP_SERIAL_REVISION) { |
| 228 | serial->rx_state = STATE_WAIT_LEN; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 229 | serial->rx_fcs_calc = crc_16_ccitt_byte(FCS_INIT_16, c); |
Nikhil Namjoshi | 1fe5899 | 2024-01-09 04:00:12 +0000 | [diff] [blame] | 230 | } else if (c == MCTP_SERIAL_FRAMING_FLAG) { |
| 231 | /* Handle the case where there are bytes dropped in request, |
| 232 | * and the state machine is out of sync. The failed request's |
| 233 | * trailing footer i.e. 0x7e would be interpreted as next |
| 234 | * request's framing footer. So if we are in STATE_WAIT_REVISION |
| 235 | * and receive 0x7e byte, then contine to stay in |
| 236 | * STATE_WAIT_REVISION |
| 237 | */ |
| 238 | mctp_prdebug( |
| 239 | "Received serial framing flag 0x%02x while waiting" |
| 240 | " for serial revision 0x%02x.", |
| 241 | c, MCTP_SERIAL_REVISION); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 242 | } else { |
| 243 | mctp_prdebug("invalid revision 0x%02x", c); |
| 244 | serial->rx_state = STATE_WAIT_SYNC_START; |
| 245 | } |
| 246 | break; |
| 247 | case STATE_WAIT_LEN: |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 248 | if (c > serial->binding.pkt_size || |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 249 | c < sizeof(struct mctp_hdr)) { |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 250 | mctp_prdebug("invalid size %d", c); |
| 251 | serial->rx_state = STATE_WAIT_SYNC_START; |
| 252 | } else { |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 253 | mctp_serial_start_packet(serial, 0); |
| 254 | pkt = serial->rx_pkt; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 255 | serial->rx_exp_len = c; |
| 256 | serial->rx_state = STATE_DATA; |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 257 | serial->rx_fcs_calc = |
| 258 | crc_16_ccitt_byte(serial->rx_fcs_calc, c); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 259 | } |
| 260 | break; |
| 261 | |
| 262 | case STATE_DATA: |
| 263 | if (c == MCTP_SERIAL_ESCAPE) { |
| 264 | serial->rx_state = STATE_DATA_ESCAPED; |
| 265 | } else { |
| 266 | mctp_pktbuf_push(pkt, &c, 1); |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 267 | serial->rx_fcs_calc = |
| 268 | crc_16_ccitt_byte(serial->rx_fcs_calc, c); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 269 | if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len) |
| 270 | serial->rx_state = STATE_WAIT_FCS1; |
| 271 | } |
| 272 | break; |
| 273 | |
| 274 | case STATE_DATA_ESCAPED: |
| 275 | c ^= 0x20; |
| 276 | mctp_pktbuf_push(pkt, &c, 1); |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 277 | serial->rx_fcs_calc = crc_16_ccitt_byte(serial->rx_fcs_calc, c); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 278 | if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len) |
| 279 | serial->rx_state = STATE_WAIT_FCS1; |
| 280 | else |
| 281 | serial->rx_state = STATE_DATA; |
| 282 | break; |
| 283 | |
| 284 | case STATE_WAIT_FCS1: |
| 285 | serial->rx_fcs = c << 8; |
| 286 | serial->rx_state = STATE_WAIT_FCS2; |
| 287 | break; |
| 288 | case STATE_WAIT_FCS2: |
| 289 | serial->rx_fcs |= c; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 290 | serial->rx_state = STATE_WAIT_SYNC_END; |
| 291 | break; |
| 292 | |
| 293 | case STATE_WAIT_SYNC_END: |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 294 | if (serial->rx_fcs == serial->rx_fcs_calc) { |
| 295 | if (c == MCTP_SERIAL_FRAMING_FLAG) { |
| 296 | valid = true; |
| 297 | } else { |
| 298 | valid = false; |
| 299 | mctp_prdebug("missing end frame marker"); |
| 300 | } |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 301 | } else { |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 302 | valid = false; |
| 303 | mctp_prdebug("invalid fcs : 0x%04x, expect 0x%04x", |
| 304 | serial->rx_fcs, serial->rx_fcs_calc); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 305 | } |
John Chung | b3de343 | 2024-04-19 11:47:01 +0800 | [diff] [blame] | 306 | |
| 307 | mctp_serial_finish_packet(serial, valid); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 308 | serial->rx_state = STATE_WAIT_SYNC_START; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | mctp_prdebug(" -> state: %d", serial->rx_state); |
| 313 | } |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 314 | static void mctp_rx_consume(struct mctp_binding_serial *serial, const void *buf, |
| 315 | size_t len) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 316 | { |
| 317 | size_t i; |
| 318 | |
| 319 | for (i = 0; i < len; i++) |
| 320 | mctp_rx_consume_one(serial, *(uint8_t *)(buf + i)); |
| 321 | } |
| 322 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 323 | #ifdef MCTP_HAVE_FILEIO |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 324 | int mctp_serial_read(struct mctp_binding_serial *serial) |
| 325 | { |
| 326 | ssize_t len; |
| 327 | |
| 328 | len = read(serial->fd, serial->rxbuf, sizeof(serial->rxbuf)); |
| 329 | if (len == 0) |
| 330 | return -1; |
| 331 | |
| 332 | if (len < 0) { |
| 333 | mctp_prerr("can't read from serial device: %m"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | mctp_rx_consume(serial, serial->rxbuf, len); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 342 | int mctp_serial_init_pollfd(struct mctp_binding_serial *serial, |
| 343 | struct pollfd *pollfd) |
| 344 | { |
| 345 | pollfd->fd = serial->fd; |
| 346 | pollfd->events = POLLIN; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 351 | int mctp_serial_open_path(struct mctp_binding_serial *serial, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 352 | const char *device) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 353 | { |
| 354 | serial->fd = open(device, O_RDWR); |
| 355 | if (serial->fd < 0) |
| 356 | mctp_prerr("can't open device %s: %m", device); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | void mctp_serial_open_fd(struct mctp_binding_serial *serial, int fd) |
| 362 | { |
| 363 | serial->fd = fd; |
| 364 | } |
| 365 | #endif |
| 366 | |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 367 | void mctp_serial_set_tx_fn(struct mctp_binding_serial *serial, |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 368 | mctp_serial_tx_fn fn, void *data) |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 369 | { |
| 370 | serial->tx_fn = fn; |
| 371 | serial->tx_fn_data = data; |
| 372 | } |
| 373 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 374 | int mctp_serial_rx(struct mctp_binding_serial *serial, const void *buf, |
| 375 | size_t len) |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 376 | { |
| 377 | mctp_rx_consume(serial, buf, len); |
Andrew Jeffery | 00e6770 | 2020-01-24 12:25:50 +1030 | [diff] [blame] | 378 | return 0; |
Jeremy Kerr | 597b369 | 2019-09-03 16:24:48 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 381 | static int mctp_serial_core_start(struct mctp_binding *binding) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 382 | { |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 383 | mctp_binding_set_tx_enabled(binding, true); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | struct mctp_binding *mctp_binding_serial_core(struct mctp_binding_serial *b) |
| 388 | { |
| 389 | return &b->binding; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | struct mctp_binding_serial *mctp_serial_init(void) |
| 393 | { |
| 394 | struct mctp_binding_serial *serial; |
| 395 | |
| 396 | serial = __mctp_alloc(sizeof(*serial)); |
Jeremy Kerr | 0bead57 | 2019-09-02 16:45:33 +0800 | [diff] [blame] | 397 | memset(serial, 0, sizeof(*serial)); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 398 | serial->fd = -1; |
| 399 | serial->rx_state = STATE_WAIT_SYNC_START; |
| 400 | serial->rx_pkt = NULL; |
| 401 | serial->binding.name = "serial"; |
| 402 | serial->binding.version = 1; |
Andrew Jeffery | 73c268e | 2020-01-30 10:16:09 +1030 | [diff] [blame] | 403 | serial->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU); |
Andrew Jeffery | 39da3d0 | 2021-03-12 16:51:26 +1030 | [diff] [blame] | 404 | serial->binding.pkt_header = 0; |
Konstantin Aladyshev | 2a2a0f6 | 2023-08-25 23:40:02 +0300 | [diff] [blame] | 405 | serial->binding.pkt_trailer = 0; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 406 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 407 | serial->binding.start = mctp_serial_core_start; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 408 | serial->binding.tx = mctp_binding_serial_tx; |
| 409 | |
| 410 | return serial; |
| 411 | } |
| 412 | |
Andrew Jeffery | f8b4749 | 2020-03-10 23:48:59 +1030 | [diff] [blame] | 413 | void mctp_serial_destroy(struct mctp_binding_serial *serial) |
| 414 | { |
| 415 | __mctp_free(serial); |
| 416 | } |