blob: 5b45715618aa8f99a8f768d5555a90dcbc3bfda8 [file] [log] [blame]
Matthew Fredricksonab27bdd2010-02-16 22:07:04 +01001/* OpenBSC Abis input driver for DAHDI */
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +01002
Harald Welte6aef70d2011-02-04 21:33:14 +01003/* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org>
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +01004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Matthew Fredricksonab27bdd2010-02-16 22:07:04 +01005 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +01006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -050036#include <dahdi/user.h>
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010037
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010038#include <openbsc/select.h>
39#include <openbsc/msgb.h>
40#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <openbsc/abis_nm.h>
43#include <openbsc/abis_rsl.h>
44#include <openbsc/subchan_demux.h>
45#include <openbsc/e1_input.h>
46#include <openbsc/talloc.h>
47
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010048#include "lapd.h"
49
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010050#define TS1_ALLOC_SIZE 300
51
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010052static int handle_ts1_read(struct bsc_fd *bfd)
53{
54 struct e1inp_line *line = bfd->data;
55 unsigned int ts_nr = bfd->priv_nr;
56 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
57 struct e1inp_sign_link *link;
Matthew Fredrickson124c80e2010-02-16 22:01:36 +010058 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Welte6aef70d2011-02-04 21:33:14 +010059 lapd_mph_type prim;
60 unsigned int sapi, tei;
61 int ilen, ret;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010062
63 if (!msg)
64 return -ENOMEM;
65
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -050066 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010067 if (ret < 0) {
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010068 perror("read ");
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010069 }
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -050070 msgb_put(msg, ret - 2);
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010071 if (ret <= 3) {
72 perror("read ");
73 }
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010074
Harald Welte6aef70d2011-02-04 21:33:14 +010075 sapi = msg->data[0] >> 2;
76 tei = msg->data[1] >> 1;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -050077
Harald Welte6aef70d2011-02-04 21:33:14 +010078 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +010079
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010080 uint8_t *idata = lapd_receive(msg->data, msg->len, &ilen, &prim, bfd);
81
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010082 msgb_pull(msg, 2);
83
Harald Welte6aef70d2011-02-04 21:33:14 +010084 DEBUGP(DMI, "prim %08x\n", prim);
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010085
Harald Welte6aef70d2011-02-04 21:33:14 +010086 switch (prim) {
87 case 0:
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010088 break;
Harald Welte6aef70d2011-02-04 21:33:14 +010089 case LAPD_MPH_ACTIVATE_IND:
90 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
91 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, tei, sapi);
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010092 break;
Harald Welte6aef70d2011-02-04 21:33:14 +010093 case LAPD_MPH_DEACTIVATE_IND:
94 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
95 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, tei, sapi);
Matthew Fredrickson73448bf2010-02-17 19:25:39 +010096 break;
Harald Welte6aef70d2011-02-04 21:33:14 +010097 case LAPD_DL_DATA_IND:
98 case LAPD_DL_UNITDATA_IND:
99 if (prim == DL_DATA_IND)
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500100 msg->l2h = msg->data + 2;
101 else
102 msg->l2h = msg->data + 1;
103 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret));
Harald Welte6aef70d2011-02-04 21:33:14 +0100104 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100105 break;
106 default:
Harald Welte6aef70d2011-02-04 21:33:14 +0100107 printf("ERROR: unknown prim\n");
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100108 break;
109 }
Harald Welte6aef70d2011-02-04 21:33:14 +0100110
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500111 DEBUGP(DMI, "Returned ok\n");
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100112 return ret;
113}
114
115static int ts_want_write(struct e1inp_ts *e1i_ts)
116{
Harald Welte6aef70d2011-02-04 21:33:14 +0100117 /* We never include the DAHDI B-Channel FD into the
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100118 * writeset, since it doesn't support poll() based
119 * write flow control */
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500120 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
121 fprintf(stderr, "Trying to write TRAU ts\n");
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100122 return 0;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500123 }
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100124
125 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
126
127 return 0;
128}
129
130static void timeout_ts1_write(void *data)
131{
132 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
133
134 /* trigger write of ts1, due to tx delay timer */
135 ts_want_write(e1i_ts);
136}
137
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500138static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
139{
140 struct bsc_fd *bfd = cbdata;
141 int ret;
142
143 ret = write(bfd->fd, data, len + 2);
144
145 if (ret < 0)
146 fprintf(stderr, "%s write failed %d\n", __func__, ret);
147}
148
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100149static int handle_ts1_write(struct bsc_fd *bfd)
150{
151 struct e1inp_line *line = bfd->data;
152 unsigned int ts_nr = bfd->priv_nr;
153 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
154 struct e1inp_sign_link *sign_link;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100155 struct msgb *msg;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100156
157 bfd->when &= ~BSC_FD_WRITE;
158
159 /* get the next msg for this timeslot */
160 msg = e1inp_tx_ts(e1i_ts, &sign_link);
161 if (!msg) {
162 /* no message after tx delay timer */
163 return 0;
164 }
165
Matthew Fredrickson73448bf2010-02-17 19:25:39 +0100166 lapd_transmit(sign_link->tei, msg->data, msg->len, bfd);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100167 msgb_free(msg);
168
169 /* set tx delay timer for next event */
170 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
171 e1i_ts->sign.tx_timer.data = e1i_ts;
172 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000);
173
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500174 return 0;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100175}
176
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500177
178static int invertbits = 1;
179
180static u_int8_t flip_table[256];
181
182static void init_flip_bits(void)
183{
184 int i,k;
185
186 for (i = 0 ; i < 256 ; i++) {
187 u_int8_t sample = 0 ;
188 for (k = 0; k<8; k++) {
189 if ( i & 1 << k ) sample |= 0x80 >> k;
190 }
191 flip_table[i] = sample;
192 }
193}
194
195static u_int8_t * flip_buf_bits ( u_int8_t * buf , int len)
196{
197 int i;
creslin28752b0e022010-03-26 12:57:31 -0500198 u_int8_t * start = buf;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500199
200 for (i = 0 ; i < len; i++) {
201 buf[i] = flip_table[(u_int8_t)buf[i]];
202 }
203
204 return start;
205}
206
creslin28752b0e022010-03-26 12:57:31 -0500207#define D_BCHAN_TX_GRAN 160
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100208/* write to a B channel TS */
209static int handle_tsX_write(struct bsc_fd *bfd)
210{
211 struct e1inp_line *line = bfd->data;
212 unsigned int ts_nr = bfd->priv_nr;
213 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500214 u_int8_t tx_buf[D_BCHAN_TX_GRAN];
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100215 struct subch_mux *mx = &e1i_ts->trau.mux;
216 int ret;
217
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500218 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100219
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500220 if (ret != D_BCHAN_TX_GRAN) {
221 fprintf(stderr, "Huh, got ret of %d\n", ret);
222 if (ret < 0)
223 return ret;
224 }
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100225
226 DEBUGP(DMIB, "BCHAN TX: %s\n",
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500227 hexdump(tx_buf, D_BCHAN_TX_GRAN));
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100228
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500229 if (invertbits) {
230 flip_buf_bits(tx_buf, ret);
231 }
232
233 ret = write(bfd->fd, tx_buf, ret);
234 if (ret < D_BCHAN_TX_GRAN)
235 fprintf(stderr, "send returns %d instead of %lu\n", ret,
236 D_BCHAN_TX_GRAN);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100237
238 return ret;
239}
240
creslin28752b0e022010-03-26 12:57:31 -0500241#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100242/* FIXME: read from a B channel TS */
243static int handle_tsX_read(struct bsc_fd *bfd)
244{
245 struct e1inp_line *line = bfd->data;
246 unsigned int ts_nr = bfd->priv_nr;
247 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500248 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100249 int ret;
250
251 if (!msg)
252 return -ENOMEM;
253
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500254 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
255 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
256 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100257 return ret;
258 }
259
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500260 if (invertbits) {
261 flip_buf_bits(msg->data, ret);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100262 }
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500263
264 msgb_put(msg, ret);
265
266 msg->l2h = msg->data;
267 DEBUGP(DMIB, "BCHAN RX: %s\n",
268 hexdump(msgb_l2(msg), ret));
269 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
270 /* physical layer indicates that data has been sent,
271 * we thus can send some more data */
creslin28752b0e022010-03-26 12:57:31 -0500272 ret = handle_tsX_write(bfd);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100273 msgb_free(msg);
274
275 return ret;
276}
277
278/* callback from select.c in case one of the fd's can be read/written */
279static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what)
280{
281 struct e1inp_line *line = bfd->data;
282 unsigned int ts_nr = bfd->priv_nr;
283 unsigned int idx = ts_nr-1;
284 struct e1inp_ts *e1i_ts = &line->ts[idx];
285 int rc = 0;
286
287 switch (e1i_ts->type) {
288 case E1INP_TS_TYPE_SIGN:
289 if (what & BSC_FD_READ)
290 rc = handle_ts1_read(bfd);
291 if (what & BSC_FD_WRITE)
292 rc = handle_ts1_write(bfd);
293 break;
294 case E1INP_TS_TYPE_TRAU:
295 if (what & BSC_FD_READ)
296 rc = handle_tsX_read(bfd);
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500297 if (what & BSC_FD_WRITE)
298 rc = handle_tsX_write(bfd);
Harald Welte6aef70d2011-02-04 21:33:14 +0100299 /* We never include the DAHDI B-Channel FD into the
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100300 * writeset, since it doesn't support poll() based
301 * write flow control */
302 break;
303 default:
304 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
305 break;
306 }
307
308 return rc;
309}
310
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100311struct e1inp_driver dahdi_driver = {
312 .name = "DAHDI",
313 .want_write = ts_want_write,
314};
315
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500316void dahdi_set_bufinfo(int fd, int as_sigchan)
317{
318 struct dahdi_bufferinfo bi;
319 int x = 0;
320
321 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
322 fprintf(stderr, "Error getting bufinfo\n");
323 exit(-1);
324 }
325
326 if (as_sigchan) {
327 bi.numbufs = 4;
328 bi.bufsize = 512;
329 } else {
creslin28752b0e022010-03-26 12:57:31 -0500330 bi.numbufs = 8;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500331 bi.bufsize = D_BCHAN_TX_GRAN;
creslin28752b0e022010-03-26 12:57:31 -0500332 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500333 }
334
335 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
336 fprintf(stderr, "Error setting bufinfo\n");
337 exit(-1);
338 }
339
340 if (!as_sigchan) {
341 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
342 fprintf(stderr, "Error setting bufinfo\n");
343 exit(-1);
344 }
345 }
346
347}
348
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100349static int mi_e1_setup(struct e1inp_line *line, int release_l2)
350{
351 int ts, ret;
352
353 /* TS0 is CRC4, don't need any fd for it */
354 for (ts = 1; ts < NUM_E1_TS; ts++) {
355 unsigned int idx = ts-1;
356 char openstr[128];
357 struct e1inp_ts *e1i_ts = &line->ts[idx];
358 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100359
360 bfd->data = line;
361 bfd->priv_nr = ts;
362 bfd->cb = dahdi_fd_cb;
363 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
364
365 switch (e1i_ts->type) {
366 case E1INP_TS_TYPE_NONE:
367 continue;
368 break;
369 case E1INP_TS_TYPE_SIGN:
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500370 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
371 if (bfd->fd == -1) {
372 fprintf(stderr, "%s could not open %s %s\n",
373 __func__, openstr, strerror(errno));
374 exit(-1);
375 }
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100376 bfd->when = BSC_FD_READ;
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500377 dahdi_set_bufinfo(bfd->fd, 1);
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100378 break;
379 case E1INP_TS_TYPE_TRAU:
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500380 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
381 if (bfd->fd == -1) {
382 fprintf(stderr, "%s could not open %s %s\n",
383 __func__, openstr, strerror(errno));
384 exit(-1);
385 }
386 dahdi_set_bufinfo(bfd->fd, 0);
Harald Welte6aef70d2011-02-04 21:33:14 +0100387 /* We never include the DAHDI B-Channel FD into the
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100388 * writeset, since it doesn't support poll() based
389 * write flow control */
creslin28752b0e022010-03-26 12:57:31 -0500390 bfd->when = BSC_FD_READ;// | BSC_FD_WRITE;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100391 break;
392 }
393
394 if (bfd->fd < 0) {
395 fprintf(stderr, "%s could not open %s %s\n",
396 __func__, openstr, strerror(errno));
397 return bfd->fd;
398 }
399
400 ret = bsc_register_fd(bfd);
401 if (ret < 0) {
402 fprintf(stderr, "could not register FD: %s\n",
403 strerror(ret));
404 return ret;
405 }
406 }
407
408 return 0;
409}
410
411int mi_e1_line_update(struct e1inp_line *line)
412{
Matthew Fredrickson73448bf2010-02-17 19:25:39 +0100413 int ret;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100414
415 if (!line->driver) {
416 /* this must be the first update */
Matthew Fredrickson124c80e2010-02-16 22:01:36 +0100417 line->driver = &dahdi_driver;
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100418 } else {
419 /* this is a subsequent update */
420 /* FIXME: first close all sockets */
421 fprintf(stderr, "incremental line updates not supported yet\n");
422 return 0;
423 }
424
Matthew Fredrickson124c80e2010-02-16 22:01:36 +0100425 if (line->driver != &dahdi_driver)
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100426 return -EINVAL;
427
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500428 init_flip_bits();
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100429
430 ret = mi_e1_setup(line, 1);
431 if (ret)
432 return ret;
433
Matthew Fredrickson8f58bbe2010-03-15 12:22:37 -0500434 lapd_transmit_cb = dahdi_write_msg;
435
Matthew Fredrickson7a9f9852010-02-16 21:55:50 +0100436 return 0;
437}
438
439static __attribute__((constructor)) void on_dso_load_sms(void)
440{
441 /* register the driver with the core */
442 e1inp_driver_register(&dahdi_driver);
443}