blob: bf73f97b26bb1c39185b81ad070f4e91939cc3bb [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for DAHDI */
2
3/* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
6 *
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
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020025#ifdef HAVE_DAHDI_USER_H
26
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <errno.h>
31#include <string.h>
32#include <time.h>
33#include <sys/fcntl.h>
34#include <sys/socket.h>
35#include <sys/ioctl.h>
36#include <arpa/inet.h>
37#include <dahdi/user.h>
38
39#include <osmocom/core/select.h>
40#include <osmocom/core/msgb.h>
41#include <openbsc/debug.h>
42#include <openbsc/gsm_data.h>
43#include <openbsc/abis_nm.h>
44#include <openbsc/abis_rsl.h>
45#include <openbsc/subchan_demux.h>
46#include <openbsc/e1_input.h>
47#include <openbsc/signal.h>
48#include <talloc.h>
49
50#include "lapd.h"
51
52#define TS1_ALLOC_SIZE 300
53
54/* Corresponds to dahdi/user.h, only PRI related events */
55static const struct value_string dahdi_evt_names[] = {
56 { DAHDI_EVENT_NONE, "NONE" },
57 { DAHDI_EVENT_ALARM, "ALARM" },
58 { DAHDI_EVENT_NOALARM, "NOALARM" },
59 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
60 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
61 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
62 { DAHDI_EVENT_REMOVED, "REMOVED" },
63 { 0, NULL }
64};
65
66static void handle_dahdi_exception(struct e1inp_ts *ts)
67{
68 int rc, evt;
69 struct input_signal_data isd;
70
71 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
72 if (rc < 0)
73 return;
74
75 LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
76 ts->line->num, ts->line->name, ts->num,
77 get_value_string(dahdi_evt_names, evt));
78
79 isd.line = ts->line;
80
81 switch (evt) {
82 case DAHDI_EVENT_ALARM:
83 /* we should notify the code that the line is gone */
84 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_ALARM, &isd);
85 break;
86 case DAHDI_EVENT_NOALARM:
87 /* alarm has gone, we should re-start the SABM requests */
88 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_NOALARM, &isd);
89 break;
90 }
91}
92
93static int handle_ts1_read(struct osmo_fd *bfd)
94{
95 struct e1inp_line *line = bfd->data;
96 unsigned int ts_nr = bfd->priv_nr;
97 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
98 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
99 lapd_mph_type prim;
100 unsigned int sapi, tei;
101 int ilen, ret;
102 uint8_t *idata;
103
104 if (!msg)
105 return -ENOMEM;
106
107 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
108 if (ret == -1)
109 handle_dahdi_exception(e1i_ts);
110 else if (ret < 0) {
111 perror("read ");
112 }
113 msgb_put(msg, ret - 2);
114 if (ret <= 3) {
115 perror("read ");
116 }
117
118 sapi = msg->data[0] >> 2;
119 tei = msg->data[1] >> 1;
120
121 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
122
123 idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim);
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200124 if (!idata && prim == 0) {
125 if (line->ops.error)
126 line->ops.error(NULL, -EBADMSG);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200127 return -EIO;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200128 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200129
130 msgb_pull(msg, 2);
131
132 DEBUGP(DMI, "prim %08x\n", prim);
133
134 switch (prim) {
135 case 0:
136 break;
137 case LAPD_MPH_ACTIVATE_IND:
138 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
139 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi);
140 break;
141 case LAPD_MPH_DEACTIVATE_IND:
142 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
143 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, tei, sapi);
144 break;
145 case LAPD_DL_DATA_IND:
146 case LAPD_DL_UNITDATA_IND:
147 if (prim == LAPD_DL_DATA_IND)
148 msg->l2h = msg->data + 2;
149 else
150 msg->l2h = msg->data + 1;
151 DEBUGP(DMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
152 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
153 break;
154 default:
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200155 if (line->ops.error)
156 line->ops.error(NULL, -EBADMSG);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200157 break;
158 }
159
160 DEBUGP(DMI, "Returned ok\n");
161 return ret;
162}
163
164static int ts_want_write(struct e1inp_ts *e1i_ts)
165{
166 /* We never include the DAHDI B-Channel FD into the
167 * writeset, since it doesn't support poll() based
168 * write flow control */
169 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
170 fprintf(stderr, "Trying to write TRAU ts\n");
171 return 0;
172 }
173
174 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
175
176 return 0;
177}
178
179static void timeout_ts1_write(void *data)
180{
181 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
182
183 /* trigger write of ts1, due to tx delay timer */
184 ts_want_write(e1i_ts);
185}
186
187static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
188{
189 struct osmo_fd *bfd = cbdata;
190 struct e1inp_line *line = bfd->data;
191 unsigned int ts_nr = bfd->priv_nr;
192 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
193 int ret;
194
195 ret = write(bfd->fd, data, len + 2);
196 if (ret == -1)
197 handle_dahdi_exception(e1i_ts);
198 else if (ret < 0)
199 LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
200}
201
202static int handle_ts1_write(struct osmo_fd *bfd)
203{
204 struct e1inp_line *line = bfd->data;
205 unsigned int ts_nr = bfd->priv_nr;
206 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
207 struct e1inp_sign_link *sign_link;
208 struct msgb *msg;
209
210 bfd->when &= ~BSC_FD_WRITE;
211
212 /* get the next msg for this timeslot */
213 msg = e1inp_tx_ts(e1i_ts, &sign_link);
214 if (!msg) {
215 /* no message after tx delay timer */
216 return 0;
217 }
218
219 DEBUGP(DMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
220 lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei,
221 sign_link->sapi, msg->data, msg->len);
222 msgb_free(msg);
223
224 /* set tx delay timer for next event */
225 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
226 e1i_ts->sign.tx_timer.data = e1i_ts;
227 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
228
229 return 0;
230}
231
232
233static int invertbits = 1;
234
235static uint8_t flip_table[256];
236
237static void init_flip_bits(void)
238{
239 int i,k;
240
241 for (i = 0 ; i < 256 ; i++) {
242 uint8_t sample = 0 ;
243 for (k = 0; k<8; k++) {
244 if ( i & 1 << k ) sample |= 0x80 >> k;
245 }
246 flip_table[i] = sample;
247 }
248}
249
250static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
251{
252 int i;
253 uint8_t * start = buf;
254
255 for (i = 0 ; i < len; i++) {
256 buf[i] = flip_table[(uint8_t)buf[i]];
257 }
258
259 return start;
260}
261
262#define D_BCHAN_TX_GRAN 160
263/* write to a B channel TS */
264static int handle_tsX_write(struct osmo_fd *bfd)
265{
266 struct e1inp_line *line = bfd->data;
267 unsigned int ts_nr = bfd->priv_nr;
268 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
269 uint8_t tx_buf[D_BCHAN_TX_GRAN];
270 struct subch_mux *mx = &e1i_ts->trau.mux;
271 int ret;
272
273 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
274
275 if (ret != D_BCHAN_TX_GRAN) {
276 fprintf(stderr, "Huh, got ret of %d\n", ret);
277 if (ret < 0)
278 return ret;
279 }
280
281 DEBUGP(DMIB, "BCHAN TX: %s\n",
282 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
283
284 if (invertbits) {
285 flip_buf_bits(tx_buf, ret);
286 }
287
288 ret = write(bfd->fd, tx_buf, ret);
289 if (ret < D_BCHAN_TX_GRAN)
290 fprintf(stderr, "send returns %d instead of %d\n", ret,
291 D_BCHAN_TX_GRAN);
292
293 return ret;
294}
295
296#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
297/* FIXME: read from a B channel TS */
298static int handle_tsX_read(struct osmo_fd *bfd)
299{
300 struct e1inp_line *line = bfd->data;
301 unsigned int ts_nr = bfd->priv_nr;
302 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
303 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
304 int ret;
305
306 if (!msg)
307 return -ENOMEM;
308
309 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
310 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
311 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
312 return ret;
313 }
314
315 if (invertbits) {
316 flip_buf_bits(msg->data, ret);
317 }
318
319 msgb_put(msg, ret);
320
321 msg->l2h = msg->data;
322 DEBUGP(DMIB, "BCHAN RX: %s\n",
323 osmo_hexdump(msgb_l2(msg), ret));
324 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
325 /* physical layer indicates that data has been sent,
326 * we thus can send some more data */
327 ret = handle_tsX_write(bfd);
328 msgb_free(msg);
329
330 return ret;
331}
332
333/* callback from select.c in case one of the fd's can be read/written */
334static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
335{
336 struct e1inp_line *line = bfd->data;
337 unsigned int ts_nr = bfd->priv_nr;
338 unsigned int idx = ts_nr-1;
339 struct e1inp_ts *e1i_ts = &line->ts[idx];
340 int rc = 0;
341
342 switch (e1i_ts->type) {
343 case E1INP_TS_TYPE_SIGN:
344 if (what & BSC_FD_EXCEPT)
345 handle_dahdi_exception(e1i_ts);
346 if (what & BSC_FD_READ)
347 rc = handle_ts1_read(bfd);
348 if (what & BSC_FD_WRITE)
349 rc = handle_ts1_write(bfd);
350 break;
351 case E1INP_TS_TYPE_TRAU:
352 if (what & BSC_FD_EXCEPT)
353 handle_dahdi_exception(e1i_ts);
354 if (what & BSC_FD_READ)
355 rc = handle_tsX_read(bfd);
356 if (what & BSC_FD_WRITE)
357 rc = handle_tsX_write(bfd);
358 /* We never include the DAHDI B-Channel FD into the
359 * writeset, since it doesn't support poll() based
360 * write flow control */
361 break;
362 default:
363 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
364 break;
365 }
366
367 return rc;
368}
369
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200370static int dahdi_e1_line_update(struct e1inp_line *line,
371 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200372
373struct e1inp_driver dahdi_driver = {
374 .name = "dahdi",
375 .want_write = ts_want_write,
376 .line_update = &dahdi_e1_line_update,
377};
378
379void dahdi_set_bufinfo(int fd, int as_sigchan)
380{
381 struct dahdi_bufferinfo bi;
382 int x = 0;
383
384 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
385 fprintf(stderr, "Error getting bufinfo\n");
386 exit(-1);
387 }
388
389 if (as_sigchan) {
390 bi.numbufs = 4;
391 bi.bufsize = 512;
392 } else {
393 bi.numbufs = 8;
394 bi.bufsize = D_BCHAN_TX_GRAN;
395 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
396 }
397
398 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
399 fprintf(stderr, "Error setting bufinfo\n");
400 exit(-1);
401 }
402
403 if (!as_sigchan) {
404 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
405 fprintf(stderr, "Error setting bufinfo\n");
406 exit(-1);
407 }
408 } else {
409 int one = 1;
410 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
411 /* we cannot reliably check for the ioctl return value here
412 * as this command will fail if the slot _already_ was a
413 * signalling slot before :( */
414 }
415}
416
417static int dahdi_e1_setup(struct e1inp_line *line)
418{
419 int ts, ret;
420
421 /* TS0 is CRC4, don't need any fd for it */
422 for (ts = 1; ts < NUM_E1_TS; ts++) {
423 unsigned int idx = ts-1;
424 char openstr[128];
425 struct e1inp_ts *e1i_ts = &line->ts[idx];
426 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
427
428 bfd->data = line;
429 bfd->priv_nr = ts;
430 bfd->cb = dahdi_fd_cb;
431 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
432
433 switch (e1i_ts->type) {
434 case E1INP_TS_TYPE_NONE:
435 continue;
436 break;
437 case E1INP_TS_TYPE_SIGN:
438 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
439 if (bfd->fd == -1) {
440 fprintf(stderr, "%s could not open %s %s\n",
441 __func__, openstr, strerror(errno));
442 exit(-1);
443 }
444 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
445 dahdi_set_bufinfo(bfd->fd, 1);
446 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
447 break;
448 case E1INP_TS_TYPE_TRAU:
449 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
450 if (bfd->fd == -1) {
451 fprintf(stderr, "%s could not open %s %s\n",
452 __func__, openstr, strerror(errno));
453 exit(-1);
454 }
455 dahdi_set_bufinfo(bfd->fd, 0);
456 /* We never include the DAHDI B-Channel FD into the
457 * writeset, since it doesn't support poll() based
458 * write flow control */
459 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
460 break;
461 }
462
463 if (bfd->fd < 0) {
464 fprintf(stderr, "%s could not open %s %s\n",
465 __func__, openstr, strerror(errno));
466 return bfd->fd;
467 }
468
469 ret = osmo_fd_register(bfd);
470 if (ret < 0) {
471 fprintf(stderr, "could not register FD: %s\n",
472 strerror(ret));
473 return ret;
474 }
475 }
476
477 return 0;
478}
479
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200480static int dahdi_e1_line_update(struct e1inp_line *line,
481 enum e1inp_line_role role, void *data)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200482{
483 if (line->driver != &dahdi_driver)
484 return -EINVAL;
485
486 return dahdi_e1_setup(line);
487}
488
489int e1inp_dahdi_init(void)
490{
491 init_flip_bits();
492
493 /* register the driver with the core */
494 return e1inp_driver_register(&dahdi_driver);
495}
496
497#endif /* HAVE_DAHDI_USER_H */