Merge commit '163d0ea85b99a2c581b1f861bf9445a9a14bfc6f'
[osmocom-bb.git] / src / target / firmware / comm / sercomm.c
1 /* Serial communications layer, based on HDLC */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
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 <osmocore/msgb.h>
28
29 #ifdef HOST_BUILD
30 #define SERCOMM_RX_MSG_SIZE     2048
31 #ifndef ARRAY_SIZE
32 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
33 #endif
34 #include <sercomm.h>
35 #define local_irq_save(x)
36 #define local_fiq_disable()
37 #define local_irq_restore(x)
38
39 #else
40 #define SERCOMM_RX_MSG_SIZE     256
41 #include <debug.h>
42 #include <osmocore/linuxlist.h>
43 #include <asm/system.h>
44
45 #include <comm/sercomm.h>
46 #include <calypso/uart.h>
47 #endif
48
49
50 enum rx_state {
51         RX_ST_WAIT_START,
52         RX_ST_ADDR,
53         RX_ST_CTRL,
54         RX_ST_DATA,
55         RX_ST_ESCAPE,
56 };
57
58 static struct {
59         int initialized;
60
61         /* transmit side */
62         struct {
63                 struct llist_head dlci_queues[_SC_DLCI_MAX];
64                 struct msgb *msg;
65                 enum rx_state state;
66                 uint8_t *next_char;
67         } tx;
68
69         /* receive side */
70         struct {
71                 dlci_cb_t dlci_handler[_SC_DLCI_MAX];
72                 struct msgb *msg;
73                 enum rx_state state;
74                 uint8_t dlci;
75                 uint8_t ctrl;
76         } rx;
77         
78 } sercomm;
79
80 void sercomm_init(void)
81 {
82         unsigned int i;
83         for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++)
84                 INIT_LLIST_HEAD(&sercomm.tx.dlci_queues[i]);
85
86         sercomm.rx.msg = NULL;
87         sercomm.initialized = 1;
88 }
89
90 int sercomm_initialized(void)
91 {
92         return sercomm.initialized;
93 }
94
95 /* user interface for transmitting messages for a given DLCI */
96 void sercomm_sendmsg(uint8_t dlci, struct msgb *msg)
97 {
98         unsigned long flags;
99         uint8_t *hdr;
100
101         /* prepend address + control octet */
102         hdr = msgb_push(msg, 2);
103         hdr[0] = dlci;
104         hdr[1] = HDLC_C_UI;
105
106         /* This functiion can be called from any context: FIQ, IRQ
107          * and supervisor context.  Proper locking is important! */
108         local_irq_save(flags);
109         local_fiq_disable();
110         msgb_enqueue(&sercomm.tx.dlci_queues[dlci], msg);
111         local_irq_restore(flags);
112
113 #ifndef HOST_BUILD
114         /* tell UART that we have something to send */
115         uart_irq_enable(SERCOMM_UART_NR, UART_IRQ_TX_EMPTY, 1);
116 #endif
117 }
118
119 /* how deep is the Tx queue for a given DLCI */
120 unsigned int sercomm_tx_queue_depth(uint8_t dlci)
121 {
122         struct llist_head *le;
123         unsigned int num = 0;
124
125         llist_for_each(le, &sercomm.tx.dlci_queues[dlci]) {
126                 num++;
127         }
128
129         return num;
130 }
131
132 /* fetch one octet of to-be-transmitted serial data */
133 int sercomm_drv_pull(uint8_t *ch)
134 {
135         /* we are always called from interrupt context in this function,
136          * which means that any data structures we use need to be for
137          * our exclusive access */
138         if (!sercomm.tx.msg) {
139                 unsigned int i;
140                 /* dequeue a new message from the queues */
141                 for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++) {
142                         sercomm.tx.msg = msgb_dequeue(&sercomm.tx.dlci_queues[i]);
143                         if (sercomm.tx.msg)
144                                 break;
145                 }
146                 if (sercomm.tx.msg) {
147                         /* start of a new message, send start flag octet */
148                         *ch = HDLC_FLAG;
149                         sercomm.tx.next_char = sercomm.tx.msg->data;
150                         return 1;
151                 } else {
152                         /* no more data avilable */
153                         return 0;
154                 }
155         }
156
157         if (sercomm.tx.state == RX_ST_ESCAPE) {
158                 /* we've already transmitted the ESCAPE octet,
159                  * we now need to trnsmit the escaped data */
160                 *ch = *sercomm.tx.next_char++;
161                 sercomm.tx.state = RX_ST_DATA;
162         } else if (sercomm.tx.next_char >= sercomm.tx.msg->tail) {
163                 /* last character has already been transmitted,
164                  * send end-of-message octet */
165                 *ch = HDLC_FLAG;
166                 /* we've reached the end of the message buffer */
167                 msgb_free(sercomm.tx.msg);
168                 sercomm.tx.msg = NULL;
169                 sercomm.tx.next_char = NULL;
170         /* escaping for the two control octets */
171         } else if (*sercomm.tx.next_char == HDLC_FLAG ||
172                    *sercomm.tx.next_char == HDLC_ESCAPE ||
173                    *sercomm.tx.next_char == 0x00) {
174                 /* send an escape octet */
175                 *ch = HDLC_ESCAPE;
176                 /* invert bit 5 of the next octet to be sent */
177                 *sercomm.tx.next_char ^= (1 << 5);
178                 sercomm.tx.state = RX_ST_ESCAPE;
179         } else {
180                 /* standard case, simply send next octet */
181                 *ch = *sercomm.tx.next_char++;
182         }
183         return 1;
184 }
185
186 /* register a handler for a given DLCI */
187 int sercomm_register_rx_cb(uint8_t dlci, dlci_cb_t cb)
188 {
189         if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler))
190                 return -EINVAL;
191
192         if (sercomm.rx.dlci_handler[dlci])
193                 return -EBUSY;
194
195         sercomm.rx.dlci_handler[dlci] = cb;
196         return 0;
197 }
198
199 /* dispatch an incomnig message once it is completely received */
200 static void dispatch_rx_msg(uint8_t dlci, struct msgb *msg)
201 {
202         if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler) ||
203             !sercomm.rx.dlci_handler[dlci]) {
204                 msgb_free(msg);
205                 return;
206         }
207         sercomm.rx.dlci_handler[dlci](dlci, msg);
208 }
209
210 /* the driver has received one byte, pass it into sercomm layer */
211 int sercomm_drv_rx_char(uint8_t ch)
212 {
213         uint8_t *ptr;
214
215         /* we are always called from interrupt context in this function,
216          * which means that any data structures we use need to be for
217          * our exclusive access */
218         if (!sercomm.rx.msg)
219                 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
220
221         if (msgb_tailroom(sercomm.rx.msg) == 0) {
222                 //cons_puts("sercomm_drv_rx_char() overflow!\n");
223                 msgb_free(sercomm.rx.msg);
224                 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
225                 sercomm.rx.state = RX_ST_WAIT_START;
226                 return 0;
227         }
228
229         switch (sercomm.rx.state) {
230         case RX_ST_WAIT_START:
231                 if (ch != HDLC_FLAG)
232                         break;
233                 sercomm.rx.state = RX_ST_ADDR;
234                 break;
235         case RX_ST_ADDR:
236                 sercomm.rx.dlci = ch;
237                 sercomm.rx.state = RX_ST_CTRL;
238                 break;
239         case RX_ST_CTRL:
240                 sercomm.rx.ctrl = ch;
241                 sercomm.rx.state = RX_ST_DATA;
242                 break;
243         case RX_ST_DATA:
244                 if (ch == HDLC_ESCAPE) {
245                         /* drop the escape octet, but change state */
246                         sercomm.rx.state = RX_ST_ESCAPE;
247                         break;
248                 } else if (ch == HDLC_FLAG) {
249                         /* message is finished */
250                         dispatch_rx_msg(sercomm.rx.dlci, sercomm.rx.msg);
251                         /* allocate new buffer */
252                         sercomm.rx.msg = NULL;
253                         /* start all over again */
254                         sercomm.rx.state = RX_ST_WAIT_START;
255
256                         /* do not add the control char */
257                         break;
258                 }
259                 /* default case: store the octet */
260                 ptr = msgb_put(sercomm.rx.msg, 1);
261                 *ptr = ch;
262                 break;
263         case RX_ST_ESCAPE:
264                 /* store bif-5-inverted octet in buffer */
265                 ch ^= (1 << 5);
266                 ptr = msgb_put(sercomm.rx.msg, 1);
267                 *ptr = ch;
268                 /* transition back to nromal DATA state */
269                 sercomm.rx.state = RX_ST_DATA;
270                 break;
271         }
272
273         return 1;
274 }