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