1 /* Serial communications layer, based on HDLC */
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
27 #include <osmocore/msgb.h>
30 #define SERCOMM_RX_MSG_SIZE 2048
32 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
35 #define local_irq_save(x)
36 #define local_fiq_disable()
37 #define local_irq_restore(x)
40 #define SERCOMM_RX_MSG_SIZE 256
42 #include <osmocore/linuxlist.h>
43 #include <asm/system.h>
45 #include <comm/sercomm.h>
46 #include <calypso/uart.h>
63 struct llist_head dlci_queues[_SC_DLCI_MAX];
71 dlci_cb_t dlci_handler[_SC_DLCI_MAX];
80 void sercomm_init(void)
83 for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++)
84 INIT_LLIST_HEAD(&sercomm.tx.dlci_queues[i]);
86 sercomm.rx.msg = NULL;
87 sercomm.initialized = 1;
90 int sercomm_initialized(void)
92 return sercomm.initialized;
95 /* user interface for transmitting messages for a given DLCI */
96 void sercomm_sendmsg(uint8_t dlci, struct msgb *msg)
101 /* prepend address + control octet */
102 hdr = msgb_push(msg, 2);
106 /* This functiion can be called from any context: FIQ, IRQ
107 * and supervisor context. Proper locking is important! */
108 local_irq_save(flags);
110 msgb_enqueue(&sercomm.tx.dlci_queues[dlci], msg);
111 local_irq_restore(flags);
114 /* tell UART that we have something to send */
115 uart_irq_enable(SERCOMM_UART_NR, UART_IRQ_TX_EMPTY, 1);
119 /* how deep is the Tx queue for a given DLCI */
120 unsigned int sercomm_tx_queue_depth(uint8_t dlci)
122 struct llist_head *le;
123 unsigned int num = 0;
125 llist_for_each(le, &sercomm.tx.dlci_queues[dlci]) {
132 /* fetch one octet of to-be-transmitted serial data */
133 int sercomm_drv_pull(uint8_t *ch)
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) {
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]);
146 if (sercomm.tx.msg) {
147 /* start of a new message, send start flag octet */
149 sercomm.tx.next_char = sercomm.tx.msg->data;
152 /* no more data avilable */
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 */
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 */
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;
180 /* standard case, simply send next octet */
181 *ch = *sercomm.tx.next_char++;
186 /* register a handler for a given DLCI */
187 int sercomm_register_rx_cb(uint8_t dlci, dlci_cb_t cb)
189 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler))
192 if (sercomm.rx.dlci_handler[dlci])
195 sercomm.rx.dlci_handler[dlci] = cb;
199 /* dispatch an incomnig message once it is completely received */
200 static void dispatch_rx_msg(uint8_t dlci, struct msgb *msg)
202 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler) ||
203 !sercomm.rx.dlci_handler[dlci]) {
207 sercomm.rx.dlci_handler[dlci](dlci, msg);
210 /* the driver has received one byte, pass it into sercomm layer */
211 int sercomm_drv_rx_char(uint8_t ch)
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 */
219 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
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;
229 switch (sercomm.rx.state) {
230 case RX_ST_WAIT_START:
233 sercomm.rx.state = RX_ST_ADDR;
236 sercomm.rx.dlci = ch;
237 sercomm.rx.state = RX_ST_CTRL;
240 sercomm.rx.ctrl = ch;
241 sercomm.rx.state = RX_ST_DATA;
244 if (ch == HDLC_ESCAPE) {
245 /* drop the escape octet, but change state */
246 sercomm.rx.state = RX_ST_ESCAPE;
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;
256 /* do not add the control char */
259 /* default case: store the octet */
260 ptr = msgb_put(sercomm.rx.msg, 1);
264 /* store bif-5-inverted octet in buffer */
266 ptr = msgb_put(sercomm.rx.msg, 1);
268 /* transition back to nromal DATA state */
269 sercomm.rx.state = RX_ST_DATA;