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