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;
89 /* set up the echo dlci */
90 sercomm_register_rx_cb(SC_DLCI_ECHO, &sercomm_sendmsg);
93 int sercomm_initialized(void)
95 return sercomm.initialized;
98 /* user interface for transmitting messages for a given DLCI */
99 void sercomm_sendmsg(uint8_t dlci, struct msgb *msg)
104 /* prepend address + control octet */
105 hdr = msgb_push(msg, 2);
109 /* This functiion can be called from any context: FIQ, IRQ
110 * and supervisor context. Proper locking is important! */
111 local_irq_save(flags);
113 msgb_enqueue(&sercomm.tx.dlci_queues[dlci], msg);
114 local_irq_restore(flags);
117 /* tell UART that we have something to send */
118 uart_irq_enable(SERCOMM_UART_NR, UART_IRQ_TX_EMPTY, 1);
122 /* how deep is the Tx queue for a given DLCI */
123 unsigned int sercomm_tx_queue_depth(uint8_t dlci)
125 struct llist_head *le;
126 unsigned int num = 0;
128 llist_for_each(le, &sercomm.tx.dlci_queues[dlci]) {
135 /* fetch one octet of to-be-transmitted serial data */
136 int sercomm_drv_pull(uint8_t *ch)
138 /* we are always called from interrupt context in this function,
139 * which means that any data structures we use need to be for
140 * our exclusive access */
141 if (!sercomm.tx.msg) {
143 /* dequeue a new message from the queues */
144 for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++) {
145 sercomm.tx.msg = msgb_dequeue(&sercomm.tx.dlci_queues[i]);
149 if (sercomm.tx.msg) {
150 /* start of a new message, send start flag octet */
152 sercomm.tx.next_char = sercomm.tx.msg->data;
155 /* no more data avilable */
160 if (sercomm.tx.state == RX_ST_ESCAPE) {
161 /* we've already transmitted the ESCAPE octet,
162 * we now need to trnsmit the escaped data */
163 *ch = *sercomm.tx.next_char++;
164 sercomm.tx.state = RX_ST_DATA;
165 } else if (sercomm.tx.next_char >= sercomm.tx.msg->tail) {
166 /* last character has already been transmitted,
167 * send end-of-message octet */
169 /* we've reached the end of the message buffer */
170 msgb_free(sercomm.tx.msg);
171 sercomm.tx.msg = NULL;
172 sercomm.tx.next_char = NULL;
173 /* escaping for the two control octets */
174 } else if (*sercomm.tx.next_char == HDLC_FLAG ||
175 *sercomm.tx.next_char == HDLC_ESCAPE ||
176 *sercomm.tx.next_char == 0x00) {
177 /* send an escape octet */
179 /* invert bit 5 of the next octet to be sent */
180 *sercomm.tx.next_char ^= (1 << 5);
181 sercomm.tx.state = RX_ST_ESCAPE;
183 /* standard case, simply send next octet */
184 *ch = *sercomm.tx.next_char++;
189 /* register a handler for a given DLCI */
190 int sercomm_register_rx_cb(uint8_t dlci, dlci_cb_t cb)
192 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler))
195 if (sercomm.rx.dlci_handler[dlci])
198 sercomm.rx.dlci_handler[dlci] = cb;
202 /* dispatch an incomnig message once it is completely received */
203 static void dispatch_rx_msg(uint8_t dlci, struct msgb *msg)
205 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler) ||
206 !sercomm.rx.dlci_handler[dlci]) {
210 sercomm.rx.dlci_handler[dlci](dlci, msg);
213 /* the driver has received one byte, pass it into sercomm layer */
214 int sercomm_drv_rx_char(uint8_t ch)
218 /* we are always called from interrupt context in this function,
219 * which means that any data structures we use need to be for
220 * our exclusive access */
222 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
224 if (msgb_tailroom(sercomm.rx.msg) == 0) {
225 //cons_puts("sercomm_drv_rx_char() overflow!\n");
226 msgb_free(sercomm.rx.msg);
227 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
228 sercomm.rx.state = RX_ST_WAIT_START;
232 switch (sercomm.rx.state) {
233 case RX_ST_WAIT_START:
236 sercomm.rx.state = RX_ST_ADDR;
239 sercomm.rx.dlci = ch;
240 sercomm.rx.state = RX_ST_CTRL;
243 sercomm.rx.ctrl = ch;
244 sercomm.rx.state = RX_ST_DATA;
247 if (ch == HDLC_ESCAPE) {
248 /* drop the escape octet, but change state */
249 sercomm.rx.state = RX_ST_ESCAPE;
251 } else if (ch == HDLC_FLAG) {
252 /* message is finished */
253 dispatch_rx_msg(sercomm.rx.dlci, sercomm.rx.msg);
254 /* allocate new buffer */
255 sercomm.rx.msg = NULL;
256 /* start all over again */
257 sercomm.rx.state = RX_ST_WAIT_START;
259 /* do not add the control char */
262 /* default case: store the octet */
263 ptr = msgb_put(sercomm.rx.msg, 1);
267 /* store bif-5-inverted octet in buffer */
269 ptr = msgb_put(sercomm.rx.msg, 1);
271 /* transition back to nromal DATA state */
272 sercomm.rx.state = RX_ST_DATA;