blob: 2639bf8a7b7f7ed542d181af11c1e24ccd994450 [file] [log] [blame]
Harald Weltecc95f4b2017-04-30 21:39:33 +02001/* (C) 2010,2017 by Harald Welte <laforge@gnumonks.org>
Harald Weltec68af6a2017-04-30 21:21:52 +02002 *
3 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Weltec68af6a2017-04-30 21:21:52 +02007 * 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
Harald Welte77117132017-05-15 17:33:02 +020023/*! \addtogroup sercomm
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * Serial communications layer, based on HDLC.
26 *
27 * \file sercomm.c */
Harald Welte77117132017-05-15 17:33:02 +020028
Harald Welte799bef52017-05-15 17:16:48 +020029#include "config.h"
30
Harald Weltec68af6a2017-04-30 21:21:52 +020031#include <stdint.h>
32#include <stdio.h>
33#include <errno.h>
34
35#include <osmocom/core/msgb.h>
Harald Weltedf8e89f2017-05-14 21:46:08 +020036#include <osmocom/core/utils.h>
Harald Welte1a97e2c2017-05-01 00:19:38 +020037#include <osmocom/core/sercomm.h>
Harald Weltedf8e89f2017-05-14 21:46:08 +020038#include <osmocom/core/linuxlist.h>
Harald Weltec68af6a2017-04-30 21:21:52 +020039
Harald Welte7af6d222017-05-14 21:48:41 +020040#ifndef EMBEDDED
Harald Weltef6adcd72017-05-01 00:19:13 +020041# define DEFAULT_RX_MSG_SIZE 2048
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! Protect against IRQ context */
Harald Welte799bef52017-05-15 17:16:48 +020043void sercomm_drv_lock(unsigned long __attribute__((unused)) *flags) {}
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044/*! Release protection against IRQ context */
Harald Welte799bef52017-05-15 17:16:48 +020045void sercomm_drv_unlock(unsigned long __attribute__((unused)) *flags) {}
Harald Weltec68af6a2017-04-30 21:21:52 +020046#else
Harald Weltef6adcd72017-05-01 00:19:13 +020047# define DEFAULT_RX_MSG_SIZE 256
Harald Welte799bef52017-05-15 17:16:48 +020048#endif /* EMBEDDED */
Harald Weltec68af6a2017-04-30 21:21:52 +020049
Harald Welte799bef52017-05-15 17:16:48 +020050/* weak symbols to be overridden by application */
51__attribute__((weak)) void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm) {};
52__attribute__((weak)) int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +020053{
Harald Welte799bef52017-05-15 17:16:48 +020054 return -1;
Harald Weltec68af6a2017-04-30 21:21:52 +020055}
56
Harald Welte8a4eb832017-05-02 21:41:36 +020057#define HDLC_FLAG 0x7E
58#define HDLC_ESCAPE 0x7D
59
60#define HDLC_C_UI 0x03
61#define HDLC_C_P_BIT (1 << 4)
62#define HDLC_C_F_BIT (1 << 4)
63
Harald Weltec68af6a2017-04-30 21:21:52 +020064enum rx_state {
65 RX_ST_WAIT_START,
66 RX_ST_ADDR,
67 RX_ST_CTRL,
68 RX_ST_DATA,
69 RX_ST_ESCAPE,
70};
71
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072/*! Initialize an Osmocom sercomm instance
Harald Welte77117132017-05-15 17:33:02 +020073 * \param sercomm Caller-allocated sercomm instance to be initialized
74 *
75 * This function initializes the sercomm instance, including the
76 * registration of the ECHO service at the ECHO DLCI
77 */
Harald Welte13588362017-04-30 23:57:55 +020078void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020079{
80 unsigned int i;
Harald Weltecc95f4b2017-04-30 21:39:33 +020081 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
82 INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]);
Harald Weltec68af6a2017-04-30 21:21:52 +020083
Harald Weltecc95f4b2017-04-30 21:39:33 +020084 sercomm->rx.msg = NULL;
Harald Weltef6adcd72017-05-01 00:19:13 +020085 if (!sercomm->rx.msg_size)
86 sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE;
Harald Weltecc95f4b2017-04-30 21:39:33 +020087 sercomm->initialized = 1;
Harald Weltec68af6a2017-04-30 21:21:52 +020088
89 /* set up the echo dlci */
Harald Welte13588362017-04-30 23:57:55 +020090 osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg);
Harald Weltec68af6a2017-04-30 21:21:52 +020091}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Determine if a given Osmocom sercomm instance has been initialized
Harald Welte77117132017-05-15 17:33:02 +020094 * \param[in] sercomm Osmocom sercomm instance to be checked
95 * \returns 1 in case \a sercomm was previously initialized; 0 otherwise */
Harald Welte13588362017-04-30 23:57:55 +020096int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020097{
Harald Weltecc95f4b2017-04-30 21:39:33 +020098 return sercomm->initialized;
Harald Weltec68af6a2017-04-30 21:21:52 +020099}
100
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200101/*! User interface for transmitting messages for a given DLCI
Harald Welte77117132017-05-15 17:33:02 +0200102 * \param[in] sercomm Osmocom sercomm instance through which to transmit
103 * \param[in] dlci DLCI through whcih to transmit \a msg
104 * \param[in] msg Message buffer to be transmitted via \a dlci on \a * sercomm
105 **/
Harald Welte13588362017-04-30 23:57:55 +0200106void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200107{
108 unsigned long flags;
109 uint8_t *hdr;
110
111 /* prepend address + control octet */
112 hdr = msgb_push(msg, 2);
113 hdr[0] = dlci;
114 hdr[1] = HDLC_C_UI;
115
116 /* This functiion can be called from any context: FIQ, IRQ
117 * and supervisor context. Proper locking is important! */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200118 sercomm_drv_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200119 msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg);
Harald Weltedf8e89f2017-05-14 21:46:08 +0200120 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200121
Harald Weltec68af6a2017-04-30 21:21:52 +0200122 /* tell UART that we have something to send */
Harald Welte799bef52017-05-15 17:16:48 +0200123 sercomm_drv_start_tx(sercomm);
Harald Weltec68af6a2017-04-30 21:21:52 +0200124}
125
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200126/*! How deep is the Tx queue for a given DLCI?
Harald Welte77117132017-05-15 17:33:02 +0200127 * \param[n] sercomm Osmocom sercomm instance on which to operate
128 * \param[in] dlci DLCI whose queue depthy is to be determined
129 * \returns number of elements in the per-DLCI transmit queue */
Harald Welte13588362017-04-30 23:57:55 +0200130unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci)
Harald Weltec68af6a2017-04-30 21:21:52 +0200131{
132 struct llist_head *le;
133 unsigned int num = 0;
134
Harald Weltecc95f4b2017-04-30 21:39:33 +0200135 llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200136 num++;
137 }
138
139 return num;
140}
141
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200142/*! wait until everything has been transmitted, then grab the lock and
Harald Welte77117132017-05-15 17:33:02 +0200143 * change the baud rate as requested
144 * \param[in] sercomm Osmocom sercomm instance
145 * \param[in] bdrt New UART Baud Rate
146 * \returns result of the operation as provided by sercomm_drv_baudrate_chg()
147 */
Harald Welte799bef52017-05-15 17:16:48 +0200148int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +0200149{
150 unsigned int i, count;
151 unsigned long flags;
152
153 while (1) {
154 /* count the number of pending messages */
155 count = 0;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200156 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
Harald Welte799bef52017-05-15 17:16:48 +0200157 count += osmo_sercomm_tx_queue_depth(sercomm, i);
Harald Weltec68af6a2017-04-30 21:21:52 +0200158 /* if we still have any in the queue, restart */
159 if (count == 0)
160 break;
161 }
162
163 while (1) {
164 /* no messages in the queue, grab the lock to ensure it
165 * stays that way */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200166 sercomm_drv_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200167 if (!sercomm->tx.msg && !sercomm->tx.next_char) {
Harald Welte799bef52017-05-15 17:16:48 +0200168 int rc;
Harald Weltec68af6a2017-04-30 21:21:52 +0200169 /* change speed */
Harald Welte799bef52017-05-15 17:16:48 +0200170 rc = sercomm_drv_baudrate_chg(sercomm, bdrt);
Harald Weltedf8e89f2017-05-14 21:46:08 +0200171 sercomm_drv_unlock(&flags);
Harald Welte799bef52017-05-15 17:16:48 +0200172 return rc;
173 } else
Harald Weltedf8e89f2017-05-14 21:46:08 +0200174 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200175 }
Harald Welte799bef52017-05-15 17:16:48 +0200176 return -1;
Harald Weltec68af6a2017-04-30 21:21:52 +0200177}
Harald Weltec68af6a2017-04-30 21:21:52 +0200178
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179/*! fetch one octet of to-be-transmitted serial data
Harald Welteea3d3ba2017-05-02 21:24:48 +0200180 * \param[in] sercomm Sercomm Instance from which to fetch pending data
181 * \param[out] ch pointer to caller-allocaed output memory
182 * \returns 1 in case of succss; 0 if no data available; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200183int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200184{
185 unsigned long flags;
186
187 /* we may be called from interrupt context, but we stiff need to lock
188 * because sercomm could be accessed from a FIQ context ... */
189
Harald Weltedf8e89f2017-05-14 21:46:08 +0200190 sercomm_drv_lock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200191
Harald Weltecc95f4b2017-04-30 21:39:33 +0200192 if (!sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200193 unsigned int i;
194 /* dequeue a new message from the queues */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200195 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) {
196 sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]);
197 if (sercomm->tx.msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200198 break;
199 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200200 if (sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200201 /* start of a new message, send start flag octet */
202 *ch = HDLC_FLAG;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200203 sercomm->tx.next_char = sercomm->tx.msg->data;
Harald Weltedf8e89f2017-05-14 21:46:08 +0200204 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200205 return 1;
206 } else {
207 /* no more data avilable */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200208 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200209 return 0;
210 }
211 }
212
Harald Weltecc95f4b2017-04-30 21:39:33 +0200213 if (sercomm->tx.state == RX_ST_ESCAPE) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200214 /* we've already transmitted the ESCAPE octet,
215 * we now need to transmit the escaped data */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200216 *ch = *sercomm->tx.next_char++;
217 sercomm->tx.state = RX_ST_DATA;
218 } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200219 /* last character has already been transmitted,
220 * send end-of-message octet */
221 *ch = HDLC_FLAG;
222 /* we've reached the end of the message buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200223 msgb_free(sercomm->tx.msg);
224 sercomm->tx.msg = NULL;
225 sercomm->tx.next_char = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200226 /* escaping for the two control octets */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200227 } else if (*sercomm->tx.next_char == HDLC_FLAG ||
228 *sercomm->tx.next_char == HDLC_ESCAPE ||
229 *sercomm->tx.next_char == 0x00) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200230 /* send an escape octet */
231 *ch = HDLC_ESCAPE;
232 /* invert bit 5 of the next octet to be sent */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200233 *sercomm->tx.next_char ^= (1 << 5);
234 sercomm->tx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200235 } else {
236 /* standard case, simply send next octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200237 *ch = *sercomm->tx.next_char++;
Harald Weltec68af6a2017-04-30 21:21:52 +0200238 }
239
Harald Weltedf8e89f2017-05-14 21:46:08 +0200240 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200241 return 1;
242}
243
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200244/*! Register a handler for a given DLCI
Harald Welteea3d3ba2017-05-02 21:24:48 +0200245 * \param sercomm Sercomm Instance in which caller wishes to register
246 * \param[in] dlci Data Ling Connection Identifier to register
247 * \param[in] cb Callback function for \a dlci
248 * \returns 0 on success; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200249int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb)
Harald Weltec68af6a2017-04-30 21:21:52 +0200250{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200251 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler))
Harald Weltec68af6a2017-04-30 21:21:52 +0200252 return -EINVAL;
253
Harald Weltecc95f4b2017-04-30 21:39:33 +0200254 if (sercomm->rx.dlci_handler[dlci])
Harald Weltec68af6a2017-04-30 21:21:52 +0200255 return -EBUSY;
256
Harald Weltecc95f4b2017-04-30 21:39:33 +0200257 sercomm->rx.dlci_handler[dlci] = cb;
Harald Weltec68af6a2017-04-30 21:21:52 +0200258 return 0;
259}
260
261/* dispatch an incoming message once it is completely received */
Harald Welte13588362017-04-30 23:57:55 +0200262static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200263{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200264 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) ||
265 !sercomm->rx.dlci_handler[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200266 msgb_free(msg);
267 return;
268 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200269 sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200270}
271
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200272/*! the driver has received one byte, pass it into sercomm layer
Harald Welteea3d3ba2017-05-02 21:24:48 +0200273 * \param[in] sercomm Sercomm Instance for which a byte was received
274 * \param[in] ch byte that was received from line for said instance
275 * \returns 1 on success; 0 on unrecognized char; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200276int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200277{
278 uint8_t *ptr;
279
280 /* we are always called from interrupt context in this function,
281 * which means that any data structures we use need to be for
282 * our exclusive access */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200283 if (!sercomm->rx.msg)
Harald Weltef6adcd72017-05-01 00:19:13 +0200284 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltec68af6a2017-04-30 21:21:52 +0200285
Harald Weltecc95f4b2017-04-30 21:39:33 +0200286 if (msgb_tailroom(sercomm->rx.msg) == 0) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200287 //cons_puts("sercomm_drv_rx_char() overflow!\n");
Harald Weltecc95f4b2017-04-30 21:39:33 +0200288 msgb_free(sercomm->rx.msg);
Harald Weltef6adcd72017-05-01 00:19:13 +0200289 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200290 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200291 return 0;
292 }
293
Harald Weltecc95f4b2017-04-30 21:39:33 +0200294 switch (sercomm->rx.state) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200295 case RX_ST_WAIT_START:
296 if (ch != HDLC_FLAG)
297 break;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200298 sercomm->rx.state = RX_ST_ADDR;
Harald Weltec68af6a2017-04-30 21:21:52 +0200299 break;
300 case RX_ST_ADDR:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200301 sercomm->rx.dlci = ch;
302 sercomm->rx.state = RX_ST_CTRL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200303 break;
304 case RX_ST_CTRL:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200305 sercomm->rx.ctrl = ch;
306 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200307 break;
308 case RX_ST_DATA:
309 if (ch == HDLC_ESCAPE) {
310 /* drop the escape octet, but change state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200311 sercomm->rx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200312 break;
313 } else if (ch == HDLC_FLAG) {
314 /* message is finished */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200315 dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200316 /* allocate new buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200317 sercomm->rx.msg = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200318 /* start all over again */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200319 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200320
321 /* do not add the control char */
322 break;
323 }
324 /* default case: store the octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200325 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200326 *ptr = ch;
327 break;
328 case RX_ST_ESCAPE:
329 /* store bif-5-inverted octet in buffer */
330 ch ^= (1 << 5);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200331 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200332 *ptr = ch;
333 /* transition back to normal DATA state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200334 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200335 break;
336 }
337
338 return 1;
339}
Harald Welte77117132017-05-15 17:33:02 +0200340
341/*! @} */