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