blob: 2f693ef464ce647d1e535ca6b5e584e9b46d9616 [file] [log] [blame]
Harald Weltec68af6a2017-04-30 21:21:52 +02001/* Serial communications layer, based on HDLC */
2
Harald Weltecc95f4b2017-04-30 21:39:33 +02003/* (C) 2010,2017 by Harald Welte <laforge@gnumonks.org>
Harald Weltec68af6a2017-04-30 21:21:52 +02004 *
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
Harald Welte77117132017-05-15 17:33:02 +020023/*! \addtogroup sercomm
24 * @{
25 */
26
27/*! \file sercomm.c
28 */
29
Harald Welte799bef52017-05-15 17:16:48 +020030#include "config.h"
31
Harald Weltec68af6a2017-04-30 21:21:52 +020032#include <stdint.h>
33#include <stdio.h>
34#include <errno.h>
35
36#include <osmocom/core/msgb.h>
Harald Weltedf8e89f2017-05-14 21:46:08 +020037#include <osmocom/core/utils.h>
Harald Welte1a97e2c2017-05-01 00:19:38 +020038#include <osmocom/core/sercomm.h>
Harald Weltedf8e89f2017-05-14 21:46:08 +020039#include <osmocom/core/linuxlist.h>
Harald Weltec68af6a2017-04-30 21:21:52 +020040
Harald Welte7af6d222017-05-14 21:48:41 +020041#ifndef EMBEDDED
Harald Weltef6adcd72017-05-01 00:19:13 +020042# define DEFAULT_RX_MSG_SIZE 2048
Harald Welte77117132017-05-15 17:33:02 +020043/*! \brief Protect against IRQ context */
Harald Welte799bef52017-05-15 17:16:48 +020044void sercomm_drv_lock(unsigned long __attribute__((unused)) *flags) {}
Harald Welte77117132017-05-15 17:33:02 +020045/*! \brief Release protection against IRQ context */
Harald Welte799bef52017-05-15 17:16:48 +020046void sercomm_drv_unlock(unsigned long __attribute__((unused)) *flags) {}
Harald Weltec68af6a2017-04-30 21:21:52 +020047#else
Harald Weltef6adcd72017-05-01 00:19:13 +020048# define DEFAULT_RX_MSG_SIZE 256
Harald Welte799bef52017-05-15 17:16:48 +020049#endif /* EMBEDDED */
Harald Weltec68af6a2017-04-30 21:21:52 +020050
Harald Welte799bef52017-05-15 17:16:48 +020051/* weak symbols to be overridden by application */
52__attribute__((weak)) void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm) {};
53__attribute__((weak)) int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +020054{
Harald Welte799bef52017-05-15 17:16:48 +020055 return -1;
Harald Weltec68af6a2017-04-30 21:21:52 +020056}
57
Harald Welte8a4eb832017-05-02 21:41:36 +020058#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 Weltec68af6a2017-04-30 21:21:52 +020065enum 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 Welte77117132017-05-15 17:33:02 +020073/*! \brief Initialize an Osmocom sercomm instance
74 * \param sercomm Caller-allocated sercomm instance to be initialized
75 *
76 * This function initializes the sercomm instance, including the
77 * registration of the ECHO service at the ECHO DLCI
78 */
Harald Welte13588362017-04-30 23:57:55 +020079void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020080{
81 unsigned int i;
Harald Weltecc95f4b2017-04-30 21:39:33 +020082 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
83 INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]);
Harald Weltec68af6a2017-04-30 21:21:52 +020084
Harald Weltecc95f4b2017-04-30 21:39:33 +020085 sercomm->rx.msg = NULL;
Harald Weltef6adcd72017-05-01 00:19:13 +020086 if (!sercomm->rx.msg_size)
87 sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE;
Harald Weltecc95f4b2017-04-30 21:39:33 +020088 sercomm->initialized = 1;
Harald Weltec68af6a2017-04-30 21:21:52 +020089
90 /* set up the echo dlci */
Harald Welte13588362017-04-30 23:57:55 +020091 osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg);
Harald Weltec68af6a2017-04-30 21:21:52 +020092}
93
Harald Welte77117132017-05-15 17:33:02 +020094/*! \brief Determine if a given Osmocom sercomm instance has been initialized
95 * \param[in] sercomm Osmocom sercomm instance to be checked
96 * \returns 1 in case \a sercomm was previously initialized; 0 otherwise */
Harald Welte13588362017-04-30 23:57:55 +020097int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020098{
Harald Weltecc95f4b2017-04-30 21:39:33 +020099 return sercomm->initialized;
Harald Weltec68af6a2017-04-30 21:21:52 +0200100}
101
Harald Welte77117132017-05-15 17:33:02 +0200102/*! \brief User interface for transmitting messages for a given DLCI
103 * \param[in] sercomm Osmocom sercomm instance through which to transmit
104 * \param[in] dlci DLCI through whcih to transmit \a msg
105 * \param[in] msg Message buffer to be transmitted via \a dlci on \a * sercomm
106 **/
Harald Welte13588362017-04-30 23:57:55 +0200107void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200108{
109 unsigned long flags;
110 uint8_t *hdr;
111
112 /* prepend address + control octet */
113 hdr = msgb_push(msg, 2);
114 hdr[0] = dlci;
115 hdr[1] = HDLC_C_UI;
116
117 /* This functiion can be called from any context: FIQ, IRQ
118 * and supervisor context. Proper locking is important! */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200119 sercomm_drv_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200120 msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg);
Harald Weltedf8e89f2017-05-14 21:46:08 +0200121 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200122
Harald Weltec68af6a2017-04-30 21:21:52 +0200123 /* tell UART that we have something to send */
Harald Welte799bef52017-05-15 17:16:48 +0200124 sercomm_drv_start_tx(sercomm);
Harald Weltec68af6a2017-04-30 21:21:52 +0200125}
126
Harald Welte77117132017-05-15 17:33:02 +0200127/*! \brief How deep is the Tx queue for a given DLCI?
128 * \param[n] sercomm Osmocom sercomm instance on which to operate
129 * \param[in] dlci DLCI whose queue depthy is to be determined
130 * \returns number of elements in the per-DLCI transmit queue */
Harald Welte13588362017-04-30 23:57:55 +0200131unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci)
Harald Weltec68af6a2017-04-30 21:21:52 +0200132{
133 struct llist_head *le;
134 unsigned int num = 0;
135
Harald Weltecc95f4b2017-04-30 21:39:33 +0200136 llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200137 num++;
138 }
139
140 return num;
141}
142
Harald Welte77117132017-05-15 17:33:02 +0200143/*! \brief wait until everything has been transmitted, then grab the lock and
144 * change the baud rate as requested
145 * \param[in] sercomm Osmocom sercomm instance
146 * \param[in] bdrt New UART Baud Rate
147 * \returns result of the operation as provided by sercomm_drv_baudrate_chg()
148 */
Harald Welte799bef52017-05-15 17:16:48 +0200149int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +0200150{
151 unsigned int i, count;
152 unsigned long flags;
153
154 while (1) {
155 /* count the number of pending messages */
156 count = 0;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200157 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
Harald Welte799bef52017-05-15 17:16:48 +0200158 count += osmo_sercomm_tx_queue_depth(sercomm, i);
Harald Weltec68af6a2017-04-30 21:21:52 +0200159 /* if we still have any in the queue, restart */
160 if (count == 0)
161 break;
162 }
163
164 while (1) {
165 /* no messages in the queue, grab the lock to ensure it
166 * stays that way */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200167 sercomm_drv_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200168 if (!sercomm->tx.msg && !sercomm->tx.next_char) {
Harald Welte799bef52017-05-15 17:16:48 +0200169 int rc;
Harald Weltec68af6a2017-04-30 21:21:52 +0200170 /* change speed */
Harald Welte799bef52017-05-15 17:16:48 +0200171 rc = sercomm_drv_baudrate_chg(sercomm, bdrt);
Harald Weltedf8e89f2017-05-14 21:46:08 +0200172 sercomm_drv_unlock(&flags);
Harald Welte799bef52017-05-15 17:16:48 +0200173 return rc;
174 } else
Harald Weltedf8e89f2017-05-14 21:46:08 +0200175 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200176 }
Harald Welte799bef52017-05-15 17:16:48 +0200177 return -1;
Harald Weltec68af6a2017-04-30 21:21:52 +0200178}
Harald Weltec68af6a2017-04-30 21:21:52 +0200179
Harald Welteea3d3ba2017-05-02 21:24:48 +0200180/*! \brief fetch one octet of to-be-transmitted serial data
181 * \param[in] sercomm Sercomm Instance from which to fetch pending data
182 * \param[out] ch pointer to caller-allocaed output memory
183 * \returns 1 in case of succss; 0 if no data available; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200184int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200185{
186 unsigned long flags;
187
188 /* we may be called from interrupt context, but we stiff need to lock
189 * because sercomm could be accessed from a FIQ context ... */
190
Harald Weltedf8e89f2017-05-14 21:46:08 +0200191 sercomm_drv_lock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200192
Harald Weltecc95f4b2017-04-30 21:39:33 +0200193 if (!sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200194 unsigned int i;
195 /* dequeue a new message from the queues */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200196 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) {
197 sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]);
198 if (sercomm->tx.msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200199 break;
200 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200201 if (sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200202 /* start of a new message, send start flag octet */
203 *ch = HDLC_FLAG;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200204 sercomm->tx.next_char = sercomm->tx.msg->data;
Harald Weltedf8e89f2017-05-14 21:46:08 +0200205 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200206 return 1;
207 } else {
208 /* no more data avilable */
Harald Weltedf8e89f2017-05-14 21:46:08 +0200209 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200210 return 0;
211 }
212 }
213
Harald Weltecc95f4b2017-04-30 21:39:33 +0200214 if (sercomm->tx.state == RX_ST_ESCAPE) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200215 /* we've already transmitted the ESCAPE octet,
216 * we now need to transmit the escaped data */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200217 *ch = *sercomm->tx.next_char++;
218 sercomm->tx.state = RX_ST_DATA;
219 } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200220 /* last character has already been transmitted,
221 * send end-of-message octet */
222 *ch = HDLC_FLAG;
223 /* we've reached the end of the message buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200224 msgb_free(sercomm->tx.msg);
225 sercomm->tx.msg = NULL;
226 sercomm->tx.next_char = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200227 /* escaping for the two control octets */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200228 } else if (*sercomm->tx.next_char == HDLC_FLAG ||
229 *sercomm->tx.next_char == HDLC_ESCAPE ||
230 *sercomm->tx.next_char == 0x00) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200231 /* send an escape octet */
232 *ch = HDLC_ESCAPE;
233 /* invert bit 5 of the next octet to be sent */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200234 *sercomm->tx.next_char ^= (1 << 5);
235 sercomm->tx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200236 } else {
237 /* standard case, simply send next octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200238 *ch = *sercomm->tx.next_char++;
Harald Weltec68af6a2017-04-30 21:21:52 +0200239 }
240
Harald Weltedf8e89f2017-05-14 21:46:08 +0200241 sercomm_drv_unlock(&flags);
Harald Weltec68af6a2017-04-30 21:21:52 +0200242 return 1;
243}
244
Harald Welteea3d3ba2017-05-02 21:24:48 +0200245/*! \brief Register a handler for a given DLCI
246 * \param sercomm Sercomm Instance in which caller wishes to register
247 * \param[in] dlci Data Ling Connection Identifier to register
248 * \param[in] cb Callback function for \a dlci
249 * \returns 0 on success; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200250int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb)
Harald Weltec68af6a2017-04-30 21:21:52 +0200251{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200252 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler))
Harald Weltec68af6a2017-04-30 21:21:52 +0200253 return -EINVAL;
254
Harald Weltecc95f4b2017-04-30 21:39:33 +0200255 if (sercomm->rx.dlci_handler[dlci])
Harald Weltec68af6a2017-04-30 21:21:52 +0200256 return -EBUSY;
257
Harald Weltecc95f4b2017-04-30 21:39:33 +0200258 sercomm->rx.dlci_handler[dlci] = cb;
Harald Weltec68af6a2017-04-30 21:21:52 +0200259 return 0;
260}
261
262/* dispatch an incoming message once it is completely received */
Harald Welte13588362017-04-30 23:57:55 +0200263static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200264{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200265 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) ||
266 !sercomm->rx.dlci_handler[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200267 msgb_free(msg);
268 return;
269 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200270 sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200271}
272
Harald Welteea3d3ba2017-05-02 21:24:48 +0200273/*! \brief the driver has received one byte, pass it into sercomm layer
274 * \param[in] sercomm Sercomm Instance for which a byte was received
275 * \param[in] ch byte that was received from line for said instance
276 * \returns 1 on success; 0 on unrecognized char; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200277int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200278{
279 uint8_t *ptr;
280
281 /* we are always called from interrupt context in this function,
282 * which means that any data structures we use need to be for
283 * our exclusive access */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200284 if (!sercomm->rx.msg)
Harald Weltef6adcd72017-05-01 00:19:13 +0200285 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltec68af6a2017-04-30 21:21:52 +0200286
Harald Weltecc95f4b2017-04-30 21:39:33 +0200287 if (msgb_tailroom(sercomm->rx.msg) == 0) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200288 //cons_puts("sercomm_drv_rx_char() overflow!\n");
Harald Weltecc95f4b2017-04-30 21:39:33 +0200289 msgb_free(sercomm->rx.msg);
Harald Weltef6adcd72017-05-01 00:19:13 +0200290 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200291 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200292 return 0;
293 }
294
Harald Weltecc95f4b2017-04-30 21:39:33 +0200295 switch (sercomm->rx.state) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200296 case RX_ST_WAIT_START:
297 if (ch != HDLC_FLAG)
298 break;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200299 sercomm->rx.state = RX_ST_ADDR;
Harald Weltec68af6a2017-04-30 21:21:52 +0200300 break;
301 case RX_ST_ADDR:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200302 sercomm->rx.dlci = ch;
303 sercomm->rx.state = RX_ST_CTRL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200304 break;
305 case RX_ST_CTRL:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200306 sercomm->rx.ctrl = ch;
307 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200308 break;
309 case RX_ST_DATA:
310 if (ch == HDLC_ESCAPE) {
311 /* drop the escape octet, but change state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200312 sercomm->rx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200313 break;
314 } else if (ch == HDLC_FLAG) {
315 /* message is finished */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200316 dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200317 /* allocate new buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200318 sercomm->rx.msg = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200319 /* start all over again */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200320 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200321
322 /* do not add the control char */
323 break;
324 }
325 /* default case: store the octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200326 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200327 *ptr = ch;
328 break;
329 case RX_ST_ESCAPE:
330 /* store bif-5-inverted octet in buffer */
331 ch ^= (1 << 5);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200332 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200333 *ptr = ch;
334 /* transition back to normal DATA state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200335 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200336 break;
337 }
338
339 return 1;
340}
Harald Welte77117132017-05-15 17:33:02 +0200341
342/*! @} */