Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 1 | /* (C) 2010,2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 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 | * |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 19 | /*! \addtogroup sercomm |
| 20 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 21 | * Serial communications layer, based on HDLC. |
| 22 | * |
| 23 | * \file sercomm.c */ |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 24 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 25 | #include "config.h" |
| 26 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include <osmocom/core/msgb.h> |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
Harald Welte | 1a97e2c | 2017-05-01 00:19:38 +0200 | [diff] [blame] | 33 | #include <osmocom/core/sercomm.h> |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 34 | #include <osmocom/core/linuxlist.h> |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 35 | |
Harald Welte | 7af6d22 | 2017-05-14 21:48:41 +0200 | [diff] [blame] | 36 | #ifndef EMBEDDED |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 37 | # define DEFAULT_RX_MSG_SIZE 2048 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | /*! Protect against IRQ context */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 39 | void sercomm_drv_lock(unsigned long __attribute__((unused)) *flags) {} |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | /*! Release protection against IRQ context */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 41 | void sercomm_drv_unlock(unsigned long __attribute__((unused)) *flags) {} |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 42 | #else |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 43 | # define DEFAULT_RX_MSG_SIZE 256 |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 44 | #endif /* EMBEDDED */ |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 45 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 46 | /* weak symbols to be overridden by application */ |
| 47 | __attribute__((weak)) void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm) {}; |
| 48 | __attribute__((weak)) int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 49 | { |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 50 | return -1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Harald Welte | 8a4eb83 | 2017-05-02 21:41:36 +0200 | [diff] [blame] | 53 | #define HDLC_FLAG 0x7E |
| 54 | #define HDLC_ESCAPE 0x7D |
| 55 | |
| 56 | #define HDLC_C_UI 0x03 |
| 57 | #define HDLC_C_P_BIT (1 << 4) |
| 58 | #define HDLC_C_F_BIT (1 << 4) |
| 59 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 60 | enum rx_state { |
| 61 | RX_ST_WAIT_START, |
| 62 | RX_ST_ADDR, |
| 63 | RX_ST_CTRL, |
| 64 | RX_ST_DATA, |
| 65 | RX_ST_ESCAPE, |
| 66 | }; |
| 67 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! Initialize an Osmocom sercomm instance |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 69 | * \param sercomm Caller-allocated sercomm instance to be initialized |
| 70 | * |
| 71 | * This function initializes the sercomm instance, including the |
| 72 | * registration of the ECHO service at the ECHO DLCI |
| 73 | */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 74 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 75 | { |
| 76 | unsigned int i; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 77 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
| 78 | INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 79 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 80 | sercomm->rx.msg = NULL; |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 81 | if (!sercomm->rx.msg_size) |
| 82 | sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 83 | sercomm->initialized = 1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 84 | |
| 85 | /* set up the echo dlci */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 86 | osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 89 | /*! Determine if a given Osmocom sercomm instance has been initialized |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 90 | * \param[in] sercomm Osmocom sercomm instance to be checked |
| 91 | * \returns 1 in case \a sercomm was previously initialized; 0 otherwise */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 92 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 93 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 94 | return sercomm->initialized; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 97 | /*! User interface for transmitting messages for a given DLCI |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 98 | * \param[in] sercomm Osmocom sercomm instance through which to transmit |
| 99 | * \param[in] dlci DLCI through whcih to transmit \a msg |
| 100 | * \param[in] msg Message buffer to be transmitted via \a dlci on \a * sercomm |
| 101 | **/ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 102 | void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 103 | { |
| 104 | unsigned long flags; |
| 105 | uint8_t *hdr; |
| 106 | |
| 107 | /* prepend address + control octet */ |
| 108 | hdr = msgb_push(msg, 2); |
| 109 | hdr[0] = dlci; |
| 110 | hdr[1] = HDLC_C_UI; |
| 111 | |
| 112 | /* This functiion can be called from any context: FIQ, IRQ |
| 113 | * and supervisor context. Proper locking is important! */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 114 | sercomm_drv_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 115 | msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg); |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 116 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 117 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 118 | /* tell UART that we have something to send */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 119 | sercomm_drv_start_tx(sercomm); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! How deep is the Tx queue for a given DLCI? |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 123 | * \param[n] sercomm Osmocom sercomm instance on which to operate |
| 124 | * \param[in] dlci DLCI whose queue depthy is to be determined |
| 125 | * \returns number of elements in the per-DLCI transmit queue */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 126 | unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 127 | { |
| 128 | struct llist_head *le; |
| 129 | unsigned int num = 0; |
| 130 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 131 | llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 132 | num++; |
| 133 | } |
| 134 | |
| 135 | return num; |
| 136 | } |
| 137 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 138 | /*! wait until everything has been transmitted, then grab the lock and |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 139 | * change the baud rate as requested |
| 140 | * \param[in] sercomm Osmocom sercomm instance |
| 141 | * \param[in] bdrt New UART Baud Rate |
| 142 | * \returns result of the operation as provided by sercomm_drv_baudrate_chg() |
| 143 | */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 144 | int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 145 | { |
| 146 | unsigned int i, count; |
| 147 | unsigned long flags; |
| 148 | |
| 149 | while (1) { |
| 150 | /* count the number of pending messages */ |
| 151 | count = 0; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 152 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 153 | count += osmo_sercomm_tx_queue_depth(sercomm, i); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 154 | /* if we still have any in the queue, restart */ |
| 155 | if (count == 0) |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | while (1) { |
| 160 | /* no messages in the queue, grab the lock to ensure it |
| 161 | * stays that way */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 162 | sercomm_drv_lock(&flags); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 163 | if (!sercomm->tx.msg && !sercomm->tx.next_char) { |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 164 | int rc; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 165 | /* change speed */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 166 | rc = sercomm_drv_baudrate_chg(sercomm, bdrt); |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 167 | sercomm_drv_unlock(&flags); |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 168 | return rc; |
| 169 | } else |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 170 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 171 | } |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 172 | return -1; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 173 | } |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 174 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 175 | /*! fetch one octet of to-be-transmitted serial data |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 176 | * \param[in] sercomm Sercomm Instance from which to fetch pending data |
| 177 | * \param[out] ch pointer to caller-allocaed output memory |
| 178 | * \returns 1 in case of succss; 0 if no data available; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 179 | int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 180 | { |
| 181 | unsigned long flags; |
| 182 | |
| 183 | /* we may be called from interrupt context, but we stiff need to lock |
| 184 | * because sercomm could be accessed from a FIQ context ... */ |
| 185 | |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 186 | sercomm_drv_lock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 187 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 188 | if (!sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 189 | unsigned int i; |
| 190 | /* dequeue a new message from the queues */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 191 | for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) { |
| 192 | sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]); |
| 193 | if (sercomm->tx.msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 194 | break; |
| 195 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 196 | if (sercomm->tx.msg) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 197 | /* start of a new message, send start flag octet */ |
| 198 | *ch = HDLC_FLAG; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 199 | sercomm->tx.next_char = sercomm->tx.msg->data; |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 200 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 201 | return 1; |
| 202 | } else { |
| 203 | /* no more data avilable */ |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 204 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | } |
| 208 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 209 | if (sercomm->tx.state == RX_ST_ESCAPE) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 210 | /* we've already transmitted the ESCAPE octet, |
| 211 | * we now need to transmit the escaped data */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 212 | *ch = *sercomm->tx.next_char++; |
| 213 | sercomm->tx.state = RX_ST_DATA; |
| 214 | } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 215 | /* last character has already been transmitted, |
| 216 | * send end-of-message octet */ |
| 217 | *ch = HDLC_FLAG; |
| 218 | /* we've reached the end of the message buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 219 | msgb_free(sercomm->tx.msg); |
| 220 | sercomm->tx.msg = NULL; |
| 221 | sercomm->tx.next_char = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 222 | /* escaping for the two control octets */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 223 | } else if (*sercomm->tx.next_char == HDLC_FLAG || |
| 224 | *sercomm->tx.next_char == HDLC_ESCAPE || |
| 225 | *sercomm->tx.next_char == 0x00) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 226 | /* send an escape octet */ |
| 227 | *ch = HDLC_ESCAPE; |
| 228 | /* invert bit 5 of the next octet to be sent */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 229 | *sercomm->tx.next_char ^= (1 << 5); |
| 230 | sercomm->tx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 231 | } else { |
| 232 | /* standard case, simply send next octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 233 | *ch = *sercomm->tx.next_char++; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Harald Welte | df8e89f | 2017-05-14 21:46:08 +0200 | [diff] [blame] | 236 | sercomm_drv_unlock(&flags); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 237 | return 1; |
| 238 | } |
| 239 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 240 | /*! Register a handler for a given DLCI |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 241 | * \param sercomm Sercomm Instance in which caller wishes to register |
| 242 | * \param[in] dlci Data Ling Connection Identifier to register |
| 243 | * \param[in] cb Callback function for \a dlci |
| 244 | * \returns 0 on success; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 245 | int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 246 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 247 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler)) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 248 | return -EINVAL; |
| 249 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 250 | if (sercomm->rx.dlci_handler[dlci]) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 251 | return -EBUSY; |
| 252 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 253 | sercomm->rx.dlci_handler[dlci] = cb; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* dispatch an incoming message once it is completely received */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 258 | static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 259 | { |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 260 | if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) || |
| 261 | !sercomm->rx.dlci_handler[dlci]) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 262 | msgb_free(msg); |
| 263 | return; |
| 264 | } |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 265 | sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 268 | /*! the driver has received one byte, pass it into sercomm layer |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 269 | * \param[in] sercomm Sercomm Instance for which a byte was received |
| 270 | * \param[in] ch byte that was received from line for said instance |
| 271 | * \returns 1 on success; 0 on unrecognized char; negative on error */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 272 | int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 273 | { |
| 274 | uint8_t *ptr; |
| 275 | |
| 276 | /* we are always called from interrupt context in this function, |
| 277 | * which means that any data structures we use need to be for |
| 278 | * our exclusive access */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 279 | if (!sercomm->rx.msg) |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 280 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 281 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 282 | if (msgb_tailroom(sercomm->rx.msg) == 0) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 283 | //cons_puts("sercomm_drv_rx_char() overflow!\n"); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 284 | msgb_free(sercomm->rx.msg); |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 285 | sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 286 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 290 | switch (sercomm->rx.state) { |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 291 | case RX_ST_WAIT_START: |
| 292 | if (ch != HDLC_FLAG) |
| 293 | break; |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 294 | sercomm->rx.state = RX_ST_ADDR; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 295 | break; |
| 296 | case RX_ST_ADDR: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 297 | sercomm->rx.dlci = ch; |
| 298 | sercomm->rx.state = RX_ST_CTRL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 299 | break; |
| 300 | case RX_ST_CTRL: |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 301 | sercomm->rx.ctrl = ch; |
| 302 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 303 | break; |
| 304 | case RX_ST_DATA: |
| 305 | if (ch == HDLC_ESCAPE) { |
| 306 | /* drop the escape octet, but change state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 307 | sercomm->rx.state = RX_ST_ESCAPE; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 308 | break; |
| 309 | } else if (ch == HDLC_FLAG) { |
| 310 | /* message is finished */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 311 | dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 312 | /* allocate new buffer */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 313 | sercomm->rx.msg = NULL; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 314 | /* start all over again */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 315 | sercomm->rx.state = RX_ST_WAIT_START; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 316 | |
| 317 | /* do not add the control char */ |
| 318 | break; |
| 319 | } |
| 320 | /* default case: store the octet */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 321 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 322 | *ptr = ch; |
| 323 | break; |
| 324 | case RX_ST_ESCAPE: |
| 325 | /* store bif-5-inverted octet in buffer */ |
| 326 | ch ^= (1 << 5); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 327 | ptr = msgb_put(sercomm->rx.msg, 1); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 328 | *ptr = ch; |
| 329 | /* transition back to normal DATA state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 330 | sercomm->rx.state = RX_ST_DATA; |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
| 334 | return 1; |
| 335 | } |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 336 | |
| 337 | /*! @} */ |