Lines Matching full:serial
26 #define pr_fmt(x) "serial: " x
33 #include "libmctp-serial.h"
149 struct mctp_binding_serial *serial = binding_to_serial(b); in mctp_binding_serial_tx() local
156 /* the length field in the header excludes serial framing in mctp_binding_serial_tx()
160 hdr = (void *)serial->txbuf; in mctp_binding_serial_tx()
172 if (len + sizeof(*hdr) + sizeof(*tlr) > sizeof(serial->txbuf)) in mctp_binding_serial_tx()
186 if (!serial->tx_fn) in mctp_binding_serial_tx()
187 return mctp_write_all(mctp_serial_write, &serial->fd, in mctp_binding_serial_tx()
188 &serial->txbuf[0], len); in mctp_binding_serial_tx()
190 return mctp_write_all(serial->tx_fn, serial->tx_fn_data, in mctp_binding_serial_tx()
191 &serial->txbuf[0], len); in mctp_binding_serial_tx()
194 static void mctp_serial_finish_packet(struct mctp_binding_serial *serial, in mctp_serial_finish_packet() argument
197 struct mctp_pktbuf *pkt = serial->rx_pkt; in mctp_serial_finish_packet()
201 mctp_bus_rx(&serial->binding, pkt); in mctp_serial_finish_packet()
203 serial->rx_pkt = NULL; in mctp_serial_finish_packet()
206 static void mctp_serial_start_packet(struct mctp_binding_serial *serial) in mctp_serial_start_packet() argument
208 serial->rx_pkt = mctp_pktbuf_init(&serial->binding, serial->rx_storage); in mctp_serial_start_packet()
211 static void mctp_rx_consume_one(struct mctp_binding_serial *serial, uint8_t c) in mctp_rx_consume_one() argument
213 struct mctp_pktbuf *pkt = serial->rx_pkt; in mctp_rx_consume_one()
216 mctp_prdebug("state: %d, char 0x%02x", serial->rx_state, c); in mctp_rx_consume_one()
218 assert(!pkt == (serial->rx_state == STATE_WAIT_SYNC_START || in mctp_rx_consume_one()
219 serial->rx_state == STATE_WAIT_REVISION || in mctp_rx_consume_one()
220 serial->rx_state == STATE_WAIT_LEN)); in mctp_rx_consume_one()
222 switch (serial->rx_state) { in mctp_rx_consume_one()
227 mctp_serial_finish_packet(serial, false); in mctp_rx_consume_one()
229 serial->rx_state = STATE_WAIT_REVISION; in mctp_rx_consume_one()
235 serial->rx_state = STATE_WAIT_LEN; in mctp_rx_consume_one()
236 serial->rx_fcs_calc = crc_16_ccitt_byte(FCS_INIT_16, c); in mctp_rx_consume_one()
246 "Received serial framing flag 0x%02x while waiting" in mctp_rx_consume_one()
247 " for serial revision 0x%02x.", in mctp_rx_consume_one()
251 serial->rx_state = STATE_WAIT_SYNC_START; in mctp_rx_consume_one()
255 if (c > serial->binding.pkt_size || in mctp_rx_consume_one()
258 serial->rx_state = STATE_WAIT_SYNC_START; in mctp_rx_consume_one()
260 mctp_serial_start_packet(serial); in mctp_rx_consume_one()
261 pkt = serial->rx_pkt; in mctp_rx_consume_one()
262 serial->rx_exp_len = c; in mctp_rx_consume_one()
263 serial->rx_state = STATE_DATA; in mctp_rx_consume_one()
264 serial->rx_fcs_calc = in mctp_rx_consume_one()
265 crc_16_ccitt_byte(serial->rx_fcs_calc, c); in mctp_rx_consume_one()
271 serial->rx_state = STATE_DATA_ESCAPED; in mctp_rx_consume_one()
274 serial->rx_fcs_calc = in mctp_rx_consume_one()
275 crc_16_ccitt_byte(serial->rx_fcs_calc, c); in mctp_rx_consume_one()
276 if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len) in mctp_rx_consume_one()
277 serial->rx_state = STATE_WAIT_FCS1; in mctp_rx_consume_one()
284 serial->rx_fcs_calc = crc_16_ccitt_byte(serial->rx_fcs_calc, c); in mctp_rx_consume_one()
285 if (pkt->end - pkt->mctp_hdr_off == serial->rx_exp_len) in mctp_rx_consume_one()
286 serial->rx_state = STATE_WAIT_FCS1; in mctp_rx_consume_one()
288 serial->rx_state = STATE_DATA; in mctp_rx_consume_one()
292 serial->rx_fcs = c << 8; in mctp_rx_consume_one()
293 serial->rx_state = STATE_WAIT_FCS2; in mctp_rx_consume_one()
296 serial->rx_fcs |= c; in mctp_rx_consume_one()
297 serial->rx_state = STATE_WAIT_SYNC_END; in mctp_rx_consume_one()
301 if (serial->rx_fcs == serial->rx_fcs_calc) { in mctp_rx_consume_one()
311 serial->rx_fcs, serial->rx_fcs_calc); in mctp_rx_consume_one()
314 mctp_serial_finish_packet(serial, valid); in mctp_rx_consume_one()
315 serial->rx_state = STATE_WAIT_SYNC_START; in mctp_rx_consume_one()
319 mctp_prdebug(" -> state: %d", serial->rx_state); in mctp_rx_consume_one()
321 static void mctp_rx_consume(struct mctp_binding_serial *serial, const void *buf, in mctp_rx_consume() argument
327 mctp_rx_consume_one(serial, ((const uint8_t *)buf)[i]); in mctp_rx_consume()
331 int mctp_serial_read(struct mctp_binding_serial *serial) in mctp_serial_read() argument
335 len = read(serial->fd, serial->rxbuf, sizeof(serial->rxbuf)); in mctp_serial_read()
340 mctp_prerr("can't read from serial device: %s", in mctp_serial_read()
345 mctp_rx_consume(serial, serial->rxbuf, len); in mctp_serial_read()
350 int mctp_serial_init_pollfd(struct mctp_binding_serial *serial, in mctp_serial_init_pollfd() argument
353 pollfd->fd = serial->fd; in mctp_serial_init_pollfd()
359 int mctp_serial_open_path(struct mctp_binding_serial *serial, in mctp_serial_open_path() argument
362 serial->fd = open(device, O_RDWR); in mctp_serial_open_path()
363 if (serial->fd < 0) in mctp_serial_open_path()
369 void mctp_serial_open_fd(struct mctp_binding_serial *serial, int fd) in mctp_serial_open_fd() argument
371 serial->fd = fd; in mctp_serial_open_fd()
375 void mctp_serial_set_tx_fn(struct mctp_binding_serial *serial, in mctp_serial_set_tx_fn() argument
378 serial->tx_fn = fn; in mctp_serial_set_tx_fn()
379 serial->tx_fn_data = data; in mctp_serial_set_tx_fn()
382 int mctp_serial_rx(struct mctp_binding_serial *serial, const void *buf, in mctp_serial_rx() argument
385 mctp_rx_consume(serial, buf, len); in mctp_serial_rx()
402 struct mctp_binding_serial *serial; in mctp_serial_init() local
404 serial = __mctp_alloc(sizeof(*serial)); in mctp_serial_init()
405 memset(serial, 0, sizeof(*serial)); in mctp_serial_init()
406 serial->fd = -1; in mctp_serial_init()
407 serial->rx_state = STATE_WAIT_SYNC_START; in mctp_serial_init()
408 serial->rx_pkt = NULL; in mctp_serial_init()
409 serial->binding.name = "serial"; in mctp_serial_init()
410 serial->binding.version = 1; in mctp_serial_init()
411 serial->binding.pkt_size = MCTP_PACKET_SIZE(SERIAL_BTU); in mctp_serial_init()
412 serial->binding.pkt_header = 0; in mctp_serial_init()
413 serial->binding.pkt_trailer = 0; in mctp_serial_init()
414 serial->binding.tx_storage = serial->tx_storage; in mctp_serial_init()
416 serial->binding.start = mctp_serial_core_start; in mctp_serial_init()
417 serial->binding.tx = mctp_binding_serial_tx; in mctp_serial_init()
419 return serial; in mctp_serial_init()
422 void mctp_serial_destroy(struct mctp_binding_serial *serial) in mctp_serial_destroy() argument
424 __mctp_free(serial); in mctp_serial_destroy()