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 <osmocom/core/msgb.h>
31 # define SERCOMM_RX_MSG_SIZE 2048
33 # define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
37 static inline void sercomm_lock(unsigned long __attribute__((unused)) *flags) {}
38 static inline void sercomm_unlock(unsigned long __attribute__((unused)) *flags) {}
42 # define SERCOMM_RX_MSG_SIZE 256
44 # include <osmocom/core/linuxlist.h>
45 # include <asm/system.h>
47 static inline void sercomm_lock(unsigned long *flags)
49 local_firq_save(*flags);
52 static inline void sercomm_unlock(unsigned long *flags)
54 local_irq_restore(*flags);
57 # include <comm/sercomm.h>
76 struct llist_head dlci_queues[_SC_DLCI_MAX];
84 dlci_cb_t dlci_handler[_SC_DLCI_MAX];
93 void sercomm_init(void)
96 for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++)
97 INIT_LLIST_HEAD(&sercomm.tx.dlci_queues[i]);
99 sercomm.rx.msg = NULL;
100 sercomm.initialized = 1;
102 /* set up the echo dlci */
103 sercomm_register_rx_cb(SC_DLCI_ECHO, &sercomm_sendmsg);
106 int sercomm_initialized(void)
108 return sercomm.initialized;
111 /* user interface for transmitting messages for a given DLCI */
112 void sercomm_sendmsg(uint8_t dlci, struct msgb *msg)
117 /* prepend address + control octet */
118 hdr = msgb_push(msg, 2);
122 /* This functiion can be called from any context: FIQ, IRQ
123 * and supervisor context. Proper locking is important! */
124 sercomm_lock(&flags);
125 msgb_enqueue(&sercomm.tx.dlci_queues[dlci], msg);
126 sercomm_unlock(&flags);
129 /* tell UART that we have something to send */
130 uart_irq_enable(SERCOMM_UART_NR, UART_IRQ_TX_EMPTY, 1);
134 /* how deep is the Tx queue for a given DLCI */
135 unsigned int sercomm_tx_queue_depth(uint8_t dlci)
137 struct llist_head *le;
138 unsigned int num = 0;
140 llist_for_each(le, &sercomm.tx.dlci_queues[dlci]) {
147 /* fetch one octet of to-be-transmitted serial data */
148 int sercomm_drv_pull(uint8_t *ch)
150 /* we are always called from interrupt context in this function,
151 * which means that any data structures we use need to be for
152 * our exclusive access */
153 if (!sercomm.tx.msg) {
155 /* dequeue a new message from the queues */
156 for (i = 0; i < ARRAY_SIZE(sercomm.tx.dlci_queues); i++) {
157 sercomm.tx.msg = msgb_dequeue(&sercomm.tx.dlci_queues[i]);
161 if (sercomm.tx.msg) {
162 /* start of a new message, send start flag octet */
164 sercomm.tx.next_char = sercomm.tx.msg->data;
167 /* no more data avilable */
172 if (sercomm.tx.state == RX_ST_ESCAPE) {
173 /* we've already transmitted the ESCAPE octet,
174 * we now need to transmit the escaped data */
175 *ch = *sercomm.tx.next_char++;
176 sercomm.tx.state = RX_ST_DATA;
177 } else if (sercomm.tx.next_char >= sercomm.tx.msg->tail) {
178 /* last character has already been transmitted,
179 * send end-of-message octet */
181 /* we've reached the end of the message buffer */
182 msgb_free(sercomm.tx.msg);
183 sercomm.tx.msg = NULL;
184 sercomm.tx.next_char = NULL;
185 /* escaping for the two control octets */
186 } else if (*sercomm.tx.next_char == HDLC_FLAG ||
187 *sercomm.tx.next_char == HDLC_ESCAPE ||
188 *sercomm.tx.next_char == 0x00) {
189 /* send an escape octet */
191 /* invert bit 5 of the next octet to be sent */
192 *sercomm.tx.next_char ^= (1 << 5);
193 sercomm.tx.state = RX_ST_ESCAPE;
195 /* standard case, simply send next octet */
196 *ch = *sercomm.tx.next_char++;
201 /* register a handler for a given DLCI */
202 int sercomm_register_rx_cb(uint8_t dlci, dlci_cb_t cb)
204 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler))
207 if (sercomm.rx.dlci_handler[dlci])
210 sercomm.rx.dlci_handler[dlci] = cb;
214 /* dispatch an incoming message once it is completely received */
215 static void dispatch_rx_msg(uint8_t dlci, struct msgb *msg)
217 if (dlci >= ARRAY_SIZE(sercomm.rx.dlci_handler) ||
218 !sercomm.rx.dlci_handler[dlci]) {
222 sercomm.rx.dlci_handler[dlci](dlci, msg);
225 /* the driver has received one byte, pass it into sercomm layer */
226 int sercomm_drv_rx_char(uint8_t ch)
230 /* we are always called from interrupt context in this function,
231 * which means that any data structures we use need to be for
232 * our exclusive access */
234 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
236 if (msgb_tailroom(sercomm.rx.msg) == 0) {
237 //cons_puts("sercomm_drv_rx_char() overflow!\n");
238 msgb_free(sercomm.rx.msg);
239 sercomm.rx.msg = sercomm_alloc_msgb(SERCOMM_RX_MSG_SIZE);
240 sercomm.rx.state = RX_ST_WAIT_START;
244 switch (sercomm.rx.state) {
245 case RX_ST_WAIT_START:
248 sercomm.rx.state = RX_ST_ADDR;
251 sercomm.rx.dlci = ch;
252 sercomm.rx.state = RX_ST_CTRL;
255 sercomm.rx.ctrl = ch;
256 sercomm.rx.state = RX_ST_DATA;
259 if (ch == HDLC_ESCAPE) {
260 /* drop the escape octet, but change state */
261 sercomm.rx.state = RX_ST_ESCAPE;
263 } else if (ch == HDLC_FLAG) {
264 /* message is finished */
265 dispatch_rx_msg(sercomm.rx.dlci, sercomm.rx.msg);
266 /* allocate new buffer */
267 sercomm.rx.msg = NULL;
268 /* start all over again */
269 sercomm.rx.state = RX_ST_WAIT_START;
271 /* do not add the control char */
274 /* default case: store the octet */
275 ptr = msgb_put(sercomm.rx.msg, 1);
279 /* store bif-5-inverted octet in buffer */
281 ptr = msgb_put(sercomm.rx.msg, 1);
283 /* transition back to normal DATA state */
284 sercomm.rx.state = RX_ST_DATA;