blob: f96be83f213a80eb144da7b72365821d4d5194dd [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>
28
29#ifdef HOST_BUILD
30
Harald Weltef6adcd72017-05-01 00:19:13 +020031# define DEFAULT_RX_MSG_SIZE 2048
Harald Weltec68af6a2017-04-30 21:21:52 +020032# ifndef ARRAY_SIZE
33# define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
34# endif
35# include <sercomm.h>
36
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
57# include <comm/sercomm.h>
58# include <uart.h>
59
60#endif
61
Harald Weltec68af6a2017-04-30 21:21:52 +020062enum 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
Harald Weltec68af6a2017-04-30 21:21:52 +020070
71#ifndef HOST_BUILD
Harald Welte13588362017-04-30 23:57:55 +020072void osmo_sercomm_bind_uart(struct osmo_sercomm_inst *sercomm, int uart)
Harald Weltec68af6a2017-04-30 21:21:52 +020073{
Harald Weltecc95f4b2017-04-30 21:39:33 +020074 sercomm->uart_id = uart;
Harald Weltec68af6a2017-04-30 21:21:52 +020075}
76
Harald Welte13588362017-04-30 23:57:55 +020077int osmo_sercomm_get_uart(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020078{
Harald Weltecc95f4b2017-04-30 21:39:33 +020079 return sercomm->uart_id;
Harald Weltec68af6a2017-04-30 21:21:52 +020080}
81#endif
82
Harald Welte13588362017-04-30 23:57:55 +020083void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020084{
85 unsigned int i;
Harald Weltecc95f4b2017-04-30 21:39:33 +020086 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
87 INIT_LLIST_HEAD(&sercomm->tx.dlci_queues[i]);
Harald Weltec68af6a2017-04-30 21:21:52 +020088
Harald Weltecc95f4b2017-04-30 21:39:33 +020089 sercomm->rx.msg = NULL;
Harald Weltef6adcd72017-05-01 00:19:13 +020090 if (!sercomm->rx.msg_size)
91 sercomm->rx.msg_size = DEFAULT_RX_MSG_SIZE;
Harald Weltecc95f4b2017-04-30 21:39:33 +020092 sercomm->initialized = 1;
Harald Weltec68af6a2017-04-30 21:21:52 +020093
94 /* set up the echo dlci */
Harald Welte13588362017-04-30 23:57:55 +020095 osmo_sercomm_register_rx_cb(sercomm, SC_DLCI_ECHO, &osmo_sercomm_sendmsg);
Harald Weltec68af6a2017-04-30 21:21:52 +020096}
97
Harald Welte13588362017-04-30 23:57:55 +020098int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm)
Harald Weltec68af6a2017-04-30 21:21:52 +020099{
Harald Weltecc95f4b2017-04-30 21:39:33 +0200100 return sercomm->initialized;
Harald Weltec68af6a2017-04-30 21:21:52 +0200101}
102
103/* user interface for transmitting messages for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +0200104void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200105{
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! */
116 sercomm_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200117 msgb_enqueue(&sercomm->tx.dlci_queues[dlci], msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200118 sercomm_unlock(&flags);
119
120#ifndef HOST_BUILD
121 /* tell UART that we have something to send */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200122 uart_irq_enable(sercomm->uart_id, UART_IRQ_TX_EMPTY, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200123#endif
124}
125
126/* how deep is the Tx queue for a given DLCI */
Harald Welte13588362017-04-30 23:57:55 +0200127unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci)
Harald Weltec68af6a2017-04-30 21:21:52 +0200128{
129 struct llist_head *le;
130 unsigned int num = 0;
131
Harald Weltecc95f4b2017-04-30 21:39:33 +0200132 llist_for_each(le, &sercomm->tx.dlci_queues[dlci]) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200133 num++;
134 }
135
136 return num;
137}
138
139#ifndef HOST_BUILD
140/* wait until everything has been transmitted, then grab the lock and
141 * change the baud rate as requested */
Harald Welte13588362017-04-30 23:57:55 +0200142void osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, enum uart_baudrate bdrt)
Harald Weltec68af6a2017-04-30 21:21:52 +0200143{
144 unsigned int i, count;
145 unsigned long flags;
146
147 while (1) {
148 /* count the number of pending messages */
149 count = 0;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200150 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++)
Harald Weltec68af6a2017-04-30 21:21:52 +0200151 count += sercomm_tx_queue_depth(i);
152 /* if we still have any in the queue, restart */
153 if (count == 0)
154 break;
155 }
156
157 while (1) {
158 /* no messages in the queue, grab the lock to ensure it
159 * stays that way */
160 sercomm_lock(&flags);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200161 if (!sercomm->tx.msg && !sercomm->tx.next_char) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200162 /* change speed */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200163 uart_baudrate(sercomm->uart_id, bdrt);
Harald Weltec68af6a2017-04-30 21:21:52 +0200164 sercomm_unlock(&flags);
165 break;
166 }
167 sercomm_unlock(&flags);
168 }
169}
170#endif
171
172/* fetch one octet of to-be-transmitted serial data */
Harald Welte13588362017-04-30 23:57:55 +0200173int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200174{
175 unsigned long flags;
176
177 /* we may be called from interrupt context, but we stiff need to lock
178 * because sercomm could be accessed from a FIQ context ... */
179
180 sercomm_lock(&flags);
181
Harald Weltecc95f4b2017-04-30 21:39:33 +0200182 if (!sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200183 unsigned int i;
184 /* dequeue a new message from the queues */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200185 for (i = 0; i < ARRAY_SIZE(sercomm->tx.dlci_queues); i++) {
186 sercomm->tx.msg = msgb_dequeue(&sercomm->tx.dlci_queues[i]);
187 if (sercomm->tx.msg)
Harald Weltec68af6a2017-04-30 21:21:52 +0200188 break;
189 }
Harald Weltecc95f4b2017-04-30 21:39:33 +0200190 if (sercomm->tx.msg) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200191 /* start of a new message, send start flag octet */
192 *ch = HDLC_FLAG;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200193 sercomm->tx.next_char = sercomm->tx.msg->data;
Harald Weltec68af6a2017-04-30 21:21:52 +0200194 sercomm_unlock(&flags);
195 return 1;
196 } else {
197 /* no more data avilable */
198 sercomm_unlock(&flags);
199 return 0;
200 }
201 }
202
Harald Weltecc95f4b2017-04-30 21:39:33 +0200203 if (sercomm->tx.state == RX_ST_ESCAPE) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200204 /* we've already transmitted the ESCAPE octet,
205 * we now need to transmit the escaped data */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200206 *ch = *sercomm->tx.next_char++;
207 sercomm->tx.state = RX_ST_DATA;
208 } else if (sercomm->tx.next_char >= sercomm->tx.msg->tail) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200209 /* last character has already been transmitted,
210 * send end-of-message octet */
211 *ch = HDLC_FLAG;
212 /* we've reached the end of the message buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200213 msgb_free(sercomm->tx.msg);
214 sercomm->tx.msg = NULL;
215 sercomm->tx.next_char = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200216 /* escaping for the two control octets */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200217 } else if (*sercomm->tx.next_char == HDLC_FLAG ||
218 *sercomm->tx.next_char == HDLC_ESCAPE ||
219 *sercomm->tx.next_char == 0x00) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200220 /* send an escape octet */
221 *ch = HDLC_ESCAPE;
222 /* invert bit 5 of the next octet to be sent */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200223 *sercomm->tx.next_char ^= (1 << 5);
224 sercomm->tx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200225 } else {
226 /* standard case, simply send next octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200227 *ch = *sercomm->tx.next_char++;
Harald Weltec68af6a2017-04-30 21:21:52 +0200228 }
229
230 sercomm_unlock(&flags);
231 return 1;
232}
233
234/* register a handler for a given DLCI */
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
258/* the driver has received one byte, pass it into sercomm layer */
Harald Welte13588362017-04-30 23:57:55 +0200259int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch)
Harald Weltec68af6a2017-04-30 21:21:52 +0200260{
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 Weltecc95f4b2017-04-30 21:39:33 +0200266 if (!sercomm->rx.msg)
Harald Weltef6adcd72017-05-01 00:19:13 +0200267 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltec68af6a2017-04-30 21:21:52 +0200268
Harald Weltecc95f4b2017-04-30 21:39:33 +0200269 if (msgb_tailroom(sercomm->rx.msg) == 0) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200270 //cons_puts("sercomm_drv_rx_char() overflow!\n");
Harald Weltecc95f4b2017-04-30 21:39:33 +0200271 msgb_free(sercomm->rx.msg);
Harald Weltef6adcd72017-05-01 00:19:13 +0200272 sercomm->rx.msg = osmo_sercomm_alloc_msgb(sercomm->rx.msg_size);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200273 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200274 return 0;
275 }
276
Harald Weltecc95f4b2017-04-30 21:39:33 +0200277 switch (sercomm->rx.state) {
Harald Weltec68af6a2017-04-30 21:21:52 +0200278 case RX_ST_WAIT_START:
279 if (ch != HDLC_FLAG)
280 break;
Harald Weltecc95f4b2017-04-30 21:39:33 +0200281 sercomm->rx.state = RX_ST_ADDR;
Harald Weltec68af6a2017-04-30 21:21:52 +0200282 break;
283 case RX_ST_ADDR:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200284 sercomm->rx.dlci = ch;
285 sercomm->rx.state = RX_ST_CTRL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200286 break;
287 case RX_ST_CTRL:
Harald Weltecc95f4b2017-04-30 21:39:33 +0200288 sercomm->rx.ctrl = ch;
289 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200290 break;
291 case RX_ST_DATA:
292 if (ch == HDLC_ESCAPE) {
293 /* drop the escape octet, but change state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200294 sercomm->rx.state = RX_ST_ESCAPE;
Harald Weltec68af6a2017-04-30 21:21:52 +0200295 break;
296 } else if (ch == HDLC_FLAG) {
297 /* message is finished */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200298 dispatch_rx_msg(sercomm, sercomm->rx.dlci, sercomm->rx.msg);
Harald Weltec68af6a2017-04-30 21:21:52 +0200299 /* allocate new buffer */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200300 sercomm->rx.msg = NULL;
Harald Weltec68af6a2017-04-30 21:21:52 +0200301 /* start all over again */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200302 sercomm->rx.state = RX_ST_WAIT_START;
Harald Weltec68af6a2017-04-30 21:21:52 +0200303
304 /* do not add the control char */
305 break;
306 }
307 /* default case: store the octet */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200308 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200309 *ptr = ch;
310 break;
311 case RX_ST_ESCAPE:
312 /* store bif-5-inverted octet in buffer */
313 ch ^= (1 << 5);
Harald Weltecc95f4b2017-04-30 21:39:33 +0200314 ptr = msgb_put(sercomm->rx.msg, 1);
Harald Weltec68af6a2017-04-30 21:21:52 +0200315 *ptr = ch;
316 /* transition back to normal DATA state */
Harald Weltecc95f4b2017-04-30 21:39:33 +0200317 sercomm->rx.state = RX_ST_DATA;
Harald Weltec68af6a2017-04-30 21:21:52 +0200318 break;
319 }
320
321 return 1;
322}