blob: d33db911111a08511b1c82b2b6b20dd4af75f583 [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
23#include <stdint.h>
24#include <stdio.h>
25#include <errno.h>
26
27#include <osmocom/core/msgb.h>
Harald Welte1a97e2c2017-05-01 00:19:38 +020028#include <osmocom/core/sercomm.h>
Harald Weltec68af6a2017-04-30 21:21:52 +020029
30#ifdef HOST_BUILD
31
Harald Weltef6adcd72017-05-01 00:19:13 +020032# define DEFAULT_RX_MSG_SIZE 2048
Harald Weltec68af6a2017-04-30 21:21:52 +020033# ifndef ARRAY_SIZE
34# define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
35# endif
Harald Weltec68af6a2017-04-30 21:21:52 +020036
37static inline void sercomm_lock(unsigned long __attribute__((unused)) *flags) {}
38static inline void sercomm_unlock(unsigned long __attribute__((unused)) *flags) {}
39
40#else
41
Harald Weltef6adcd72017-05-01 00:19:13 +020042# define DEFAULT_RX_MSG_SIZE 256
Harald Weltec68af6a2017-04-30 21:21:52 +020043# include <debug.h>
44# include <osmocom/core/linuxlist.h>
45# include <asm/system.h>
46
47static inline void sercomm_lock(unsigned long *flags)
48{
49 local_firq_save(*flags);
50}
51
52static inline void sercomm_unlock(unsigned long *flags)
53{
54 local_irq_restore(*flags);
55}
56
Harald Weltec68af6a2017-04-30 21:21:52 +020057# include <uart.h>
58
59#endif
60
Harald Welte8a4eb832017-05-02 21:41:36 +020061#define HDLC_FLAG 0x7E
62#define HDLC_ESCAPE 0x7D
63
64#define HDLC_C_UI 0x03
65#define HDLC_C_P_BIT (1 << 4)
66#define HDLC_C_F_BIT (1 << 4)
67
Harald Weltec68af6a2017-04-30 21:21:52 +020068enum rx_state {
69 RX_ST_WAIT_START,
70 RX_ST_ADDR,
71 RX_ST_CTRL,
72 RX_ST_DATA,
73 RX_ST_ESCAPE,
74};
75
Harald Welte13588362017-04-30 23:57:55 +020076void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020077{
78 unsigned int i;
Harald Weltecc95f4b2017-04-30 21:39:33 +020079 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
80 INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]);
Harald Weltec68af6a2017-04-30 21:21:52 +020081
Harald Weltecc95f4b2017-04-30 21:39:33 +020082 sercomm->rx.msg = NULL;
Harald Weltef6adcd72017-05-01 00:19:13 +020083 if (!sercomm->rx.msg_size)
84 sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE;
Harald Weltecc95f4b2017-04-30 21:39:33 +020085 sercomm->initialized = 1;
Harald Weltec68af6a2017-04-30 21:21:52 +020086
87 /* set up the echo dlci */
Harald Welte13588362017-04-30 23:57:55 +020088 osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg);
Harald Weltec68af6a2017-04-30 21:21:52 +020089}
90
Harald Welte13588362017-04-30 23:57:55 +020091int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020092{
Harald Weltecc95f4b2017-04-30 21:39:33 +020093 return sercomm->initialized;
Harald Weltec68af6a2017-04-30 21:21:52 +020094}
95
96/* user interface for transmitting messages for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +020097void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +020098{
99 unsigned long flags;
100 uint8_t *hdr;
101
102 /* prepend address + control octet */
103 hdr = msgb_push(msg, 2);
104 hdr[0] = dlci;
105 hdr[1] = HDLC_C_UI;
106
107 /* This functiion can be called from any context: FIQ, IRQ
108 * and supervisor context. Proper locking is important! */
109 sercomm_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200110 msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200111 sercomm_unlock(&flags);
112
113#ifndef HOST_BUILD
114 /* tell UART that we have something to send */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200115 uart_irq_enable(sercomm->uart_id, UART_IRQ_TX_EMPTY, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200116#endif
117}
118
119/* how deep is the Tx queue for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +0200120unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci)
Harald Weltec68af6a2017-04-30 21:21:52 +0200121{
122 struct llist_head *le;
123 unsigned int num = 0;
124
Harald Weltecc95f4b2017-04-30 21:39:33 +0200125 llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200126 num++;
127 }
128
129 return num;
130}
131
132#ifndef HOST_BUILD
133/* wait until everything has been transmitted, then grab the lock and
134 * change the baud rate as requested */
Harald Welte13588362017-04-30 23:57:55 +0200135void osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, enum uart_baudrate bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +0200136{
137 unsigned int i, count;
138 unsigned long flags;
139
140 while (1) {
141 /* count the number of pending messages */
142 count = 0;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200143 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
Harald Weltec68af6a2017-04-30 21:21:52 +0200144 count += sercomm_tx_queue_depth(i);
145 /* if we still have any in the queue, restart */
146 if (count == 0)
147 break;
148 }
149
150 while (1) {
151 /* no messages in the queue, grab the lock to ensure it
152 * stays that way */
153 sercomm_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200154 if (!sercomm->tx.msg && !sercomm->tx.next_char) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200155 /* change speed */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200156 uart_baudrate(sercomm->uart_id, bdrt);
Harald Weltec68af6a2017-04-30 21:21:52 +0200157 sercomm_unlock(&flags);
158 break;
159 }
160 sercomm_unlock(&flags);
161 }
162}
163#endif
164
Harald Welteea3d3ba2017-05-02 21:24:48 +0200165/*! \brief fetch one octet of to-be-transmitted serial data
166 * \param[in] sercomm Sercomm Instance from which to fetch pending data
167 * \param[out] ch pointer to caller-allocaed output memory
168 * \returns 1 in case of succss; 0 if no data available; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200169int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200170{
171 unsigned long flags;
172
173 /* we may be called from interrupt context, but we stiff need to lock
174 * because sercomm could be accessed from a FIQ context ... */
175
176 sercomm_lock(&flags);
177
Harald Weltecc95f4b2017-04-30 21:39:33 +0200178 if (!sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200179 unsigned int i;
180 /* dequeue a new message from the queues */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200181 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) {
182 sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]);
183 if (sercomm->tx.msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200184 break;
185 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200186 if (sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200187 /* start of a new message, send start flag octet */
188 *ch = HDLC_FLAG;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200189 sercomm->tx.next_char = sercomm->tx.msg->data;
Harald Weltec68af6a2017-04-30 21:21:52 +0200190 sercomm_unlock(&flags);
191 return 1;
192 } else {
193 /* no more data avilable */
194 sercomm_unlock(&flags);
195 return 0;
196 }
197 }
198
Harald Weltecc95f4b2017-04-30 21:39:33 +0200199 if (sercomm->tx.state == RX_ST_ESCAPE) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200200 /* we've already transmitted the ESCAPE octet,
201 * we now need to transmit the escaped data */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200202 *ch = *sercomm->tx.next_char++;
203 sercomm->tx.state = RX_ST_DATA;
204 } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200205 /* last character has already been transmitted,
206 * send end-of-message octet */
207 *ch = HDLC_FLAG;
208 /* we've reached the end of the message buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200209 msgb_free(sercomm->tx.msg);
210 sercomm->tx.msg = NULL;
211 sercomm->tx.next_char = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200212 /* escaping for the two control octets */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200213 } else if (*sercomm->tx.next_char == HDLC_FLAG ||
214 *sercomm->tx.next_char == HDLC_ESCAPE ||
215 *sercomm->tx.next_char == 0x00) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200216 /* send an escape octet */
217 *ch = HDLC_ESCAPE;
218 /* invert bit 5 of the next octet to be sent */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200219 *sercomm->tx.next_char ^= (1 << 5);
220 sercomm->tx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200221 } else {
222 /* standard case, simply send next octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200223 *ch = *sercomm->tx.next_char++;
Harald Weltec68af6a2017-04-30 21:21:52 +0200224 }
225
226 sercomm_unlock(&flags);
227 return 1;
228}
229
Harald Welteea3d3ba2017-05-02 21:24:48 +0200230/*! \brief Register a handler for a given DLCI
231 * \param sercomm Sercomm Instance in which caller wishes to register
232 * \param[in] dlci Data Ling Connection Identifier to register
233 * \param[in] cb Callback function for \a dlci
234 * \returns 0 on success; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200235int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb)
Harald Weltec68af6a2017-04-30 21:21:52 +0200236{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200237 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler))
Harald Weltec68af6a2017-04-30 21:21:52 +0200238 return -EINVAL;
239
Harald Weltecc95f4b2017-04-30 21:39:33 +0200240 if (sercomm->rx.dlci_handler[dlci])
Harald Weltec68af6a2017-04-30 21:21:52 +0200241 return -EBUSY;
242
Harald Weltecc95f4b2017-04-30 21:39:33 +0200243 sercomm->rx.dlci_handler[dlci] = cb;
Harald Weltec68af6a2017-04-30 21:21:52 +0200244 return 0;
245}
246
247/* dispatch an incoming message once it is completely received */
Harald Welte13588362017-04-30 23:57:55 +0200248static void dispatch_rx_msg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200249{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200250 if (dlci >= ARRAY_SIZE(sercomm->rx.dlci_handler) ||
251 !sercomm->rx.dlci_handler[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200252 msgb_free(msg);
253 return;
254 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200255 sercomm->rx.dlci_handler[dlci](sercomm, dlci, msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200256}
257
Harald Welteea3d3ba2017-05-02 21:24:48 +0200258/*! \brief the driver has received one byte, pass it into sercomm layer
259 * \param[in] sercomm Sercomm Instance for which a byte was received
260 * \param[in] ch byte that was received from line for said instance
261 * \returns 1 on success; 0 on unrecognized char; negative on error */
Harald Welte13588362017-04-30 23:57:55 +0200262int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200263{
264 uint8_t *ptr;
265
266 /* we are always called from interrupt context in this function,
267 * which means that any data structures we use need to be for
268 * our exclusive access */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200269 if (!sercomm->rx.msg)
Harald Weltef6adcd72017-05-01 00:19:13 +0200270 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltec68af6a2017-04-30 21:21:52 +0200271
Harald Weltecc95f4b2017-04-30 21:39:33 +0200272 if (msgb_tailroom(sercomm->rx.msg) == 0) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200273 //cons_puts("sercomm_drv_rx_char() overflow!\n");
Harald Weltecc95f4b2017-04-30 21:39:33 +0200274 msgb_free(sercomm->rx.msg);
Harald Weltef6adcd72017-05-01 00:19:13 +0200275 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200276 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200277 return 0;
278 }
279
Harald Weltecc95f4b2017-04-30 21:39:33 +0200280 switch (sercomm->rx.state) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200281 case RX_ST_WAIT_START:
282 if (ch != HDLC_FLAG)
283 break;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200284 sercomm->rx.state = RX_ST_ADDR;
Harald Weltec68af6a2017-04-30 21:21:52 +0200285 break;
286 case RX_ST_ADDR:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200287 sercomm->rx.dlci = ch;
288 sercomm->rx.state = RX_ST_CTRL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200289 break;
290 case RX_ST_CTRL:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200291 sercomm->rx.ctrl = ch;
292 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200293 break;
294 case RX_ST_DATA:
295 if (ch == HDLC_ESCAPE) {
296 /* drop the escape octet, but change state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200297 sercomm->rx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200298 break;
299 } else if (ch == HDLC_FLAG) {
300 /* message is finished */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200301 dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200302 /* allocate new buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200303 sercomm->rx.msg = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200304 /* start all over again */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200305 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200306
307 /* do not add the control char */
308 break;
309 }
310 /* default case: store the octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200311 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200312 *ptr = ch;
313 break;
314 case RX_ST_ESCAPE:
315 /* store bif-5-inverted octet in buffer */
316 ch ^= (1 << 5);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200317 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200318 *ptr = ch;
319 /* transition back to normal DATA state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200320 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200321 break;
322 }
323
324 return 1;
325}