blob: 6594eeab31821199f77cf878fd5f9a0fe9507a65 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for DAHDI */
2
Harald Welte7c1c8cc2019-11-06 21:50:20 +01003/* (C) 2008-2019 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02004 * (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 *
Harald Welte323d39d2017-11-13 01:09:21 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
Holger Hans Peter Freytherd9d1b5c2014-09-25 18:47:12 +020027#include "config.h"
Harald Welte3bc78852011-08-24 08:32:38 +020028
29#ifdef HAVE_DAHDI_USER_H
30
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020031#include <stdio.h>
32#include <unistd.h>
33#include <stdlib.h>
34#include <errno.h>
35#include <string.h>
36#include <time.h>
37#include <sys/fcntl.h>
38#include <sys/socket.h>
39#include <sys/ioctl.h>
40#include <arpa/inet.h>
41#include <dahdi/user.h>
42
Harald Weltefe05cf52011-09-26 23:18:41 +020043#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020044#include <osmocom/core/select.h>
45#include <osmocom/core/msgb.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020046#include <osmocom/core/logging.h>
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +020047#include <osmocom/core/signal.h>
Harald Welte95e5dec2011-08-16 14:41:32 +020048#include <osmocom/core/rate_ctr.h>
Harald Weltefe05cf52011-09-26 23:18:41 +020049
50#include <osmocom/vty/vty.h>
51
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020052#include <osmocom/abis/subchan_demux.h>
53#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054
Pablo Neira Ayuso355ce692011-07-05 14:53:37 +020055#include <osmocom/abis/lapd.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020056
57#define TS1_ALLOC_SIZE 300
58
Harald Weltec2889512011-09-13 23:49:04 +010059struct span_cfg {
60 struct llist_head list;
61
62 unsigned int span_nr;
63 unsigned int chan_base;
64 unsigned int chan_num;
65};
66
67static struct span_cfg *span_cfgs[DAHDI_MAX_SPANS];
68
69static int reread_span_cfgs(void)
70{
71 struct dahdi_spaninfo si;
72 unsigned int basechan = 1;
Harald Welte494f2902011-09-26 23:06:49 +020073 int span_nr;
Harald Weltec2889512011-09-13 23:49:04 +010074 int fd;
75
76 if ((fd = open("/dev/dahdi/ctl", O_RDWR)) < 0) {
77 LOGP(DLMI, LOGL_ERROR, "Unable to open DAHDI ctl: %s\n",
78 strerror(errno));
79 return -EIO;
80 }
81
Harald Weltee2bd6852011-09-26 23:03:12 +020082 for (span_nr = 1; span_nr < DAHDI_MAX_SPANS; span_nr++) {
Harald Weltec2889512011-09-13 23:49:04 +010083 struct span_cfg *scfg;
Harald Weltee2bd6852011-09-26 23:03:12 +020084 /* our array index starts at 0, but DAHDI span at 1 */
85 int i = span_nr - 1;
Harald Weltec2889512011-09-13 23:49:04 +010086
87 /* clear any old cached information */
88 if (span_cfgs[i]) {
89 talloc_free(span_cfgs[i]);
90 span_cfgs[i] = NULL;
91 }
92
93 memset(&si, 0, sizeof(si));
Harald Weltee2bd6852011-09-26 23:03:12 +020094 si.spanno = span_nr;
Harald Weltec2889512011-09-13 23:49:04 +010095 if (ioctl(fd, DAHDI_SPANSTAT, &si))
96 continue;
97
98 /* create and link new span_cfg */
99 scfg = talloc_zero(NULL, struct span_cfg);
100 if (!scfg) {
101 close(fd);
102 return -ENOMEM;
103 }
Harald Weltee2bd6852011-09-26 23:03:12 +0200104 scfg->span_nr = span_nr;
Harald Weltec2889512011-09-13 23:49:04 +0100105 scfg->chan_num = si.totalchans;
106 scfg->chan_base = basechan;
107 span_cfgs[i] = scfg;
108
109 basechan += si.totalchans;
110 }
111
112 close(fd);
113
114 return 0;
115}
116
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200117/* Corresponds to dahdi/user.h, only PRI related events */
118static const struct value_string dahdi_evt_names[] = {
119 { DAHDI_EVENT_NONE, "NONE" },
120 { DAHDI_EVENT_ALARM, "ALARM" },
121 { DAHDI_EVENT_NOALARM, "NOALARM" },
122 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
123 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
124 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
125 { DAHDI_EVENT_REMOVED, "REMOVED" },
126 { 0, NULL }
127};
128
129static void handle_dahdi_exception(struct e1inp_ts *ts)
130{
131 int rc, evt;
Harald Welte95e5dec2011-08-16 14:41:32 +0200132 struct e1inp_line *line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200133 struct input_signal_data isd;
134
135 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
136 if (rc < 0)
137 return;
138
Harald Weltecc2241b2011-07-19 16:06:06 +0200139 LOGP(DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200140 ts->line->num, ts->line->name, ts->num,
141 get_value_string(dahdi_evt_names, evt));
142
143 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200144 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200145
146 switch (evt) {
147 case DAHDI_EVENT_ALARM:
148 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200149 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +0200150 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200151 break;
152 case DAHDI_EVENT_NOALARM:
153 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200154 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200155 break;
Harald Welte95e5dec2011-08-16 14:41:32 +0200156 case DAHDI_EVENT_ABORT:
157 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
158 break;
159 case DAHDI_EVENT_OVERRUN:
160 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
161 break;
162 case DAHDI_EVENT_BADFCS:
163 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
164 break;
165 case DAHDI_EVENT_REMOVED:
166 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
167 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200168 }
169}
170
171static int handle_ts1_read(struct osmo_fd *bfd)
172{
173 struct e1inp_line *line = bfd->data;
174 unsigned int ts_nr = bfd->priv_nr;
175 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
176 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200177 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200178
179 if (!msg)
180 return -ENOMEM;
181
182 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
183 if (ret == -1)
184 handle_dahdi_exception(e1i_ts);
185 else if (ret < 0) {
186 perror("read ");
187 }
188 msgb_put(msg, ret - 2);
189 if (ret <= 3) {
190 perror("read ");
191 }
192
Harald Weltefd44a5f2011-08-21 00:48:54 +0200193 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200194}
195
196static int ts_want_write(struct e1inp_ts *e1i_ts)
197{
198 /* We never include the DAHDI B-Channel FD into the
199 * writeset, since it doesn't support poll() based
200 * write flow control */
201 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100202 LOGP(DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200203 return 0;
204 }
205
206 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
207
208 return 0;
209}
210
211static void timeout_ts1_write(void *data)
212{
213 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
214
215 /* trigger write of ts1, due to tx delay timer */
216 ts_want_write(e1i_ts);
217}
218
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200219static void dahdi_write_msg(struct msgb *msg, void *cbdata)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200220{
221 struct osmo_fd *bfd = cbdata;
222 struct e1inp_line *line = bfd->data;
223 unsigned int ts_nr = bfd->priv_nr;
224 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
225 int ret;
226
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200227 ret = write(bfd->fd, msg->data, msg->len + 2);
228 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200229 if (ret == -1)
230 handle_dahdi_exception(e1i_ts);
231 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200232 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200233}
234
235static int handle_ts1_write(struct osmo_fd *bfd)
236{
237 struct e1inp_line *line = bfd->data;
238 unsigned int ts_nr = bfd->priv_nr;
239 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
240 struct e1inp_sign_link *sign_link;
241 struct msgb *msg;
242
243 bfd->when &= ~BSC_FD_WRITE;
244
245 /* get the next msg for this timeslot */
246 msg = e1inp_tx_ts(e1i_ts, &sign_link);
247 if (!msg) {
248 /* no message after tx delay timer */
249 return 0;
250 }
251
Harald Weltecc2241b2011-07-19 16:06:06 +0200252 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200253 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200254 sign_link->sapi, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200255
256 /* set tx delay timer for next event */
Pablo Neira Ayusob26f2fd2017-06-07 18:32:13 +0200257 osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200258 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
259
260 return 0;
261}
262
Harald Welte7a228eb2016-07-28 11:09:31 +0200263static void handle_hdlc_write(struct osmo_fd *bfd)
264{
265 struct e1inp_line *line = bfd->data;
266 unsigned int ts_nr = bfd->priv_nr;
267 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
268 struct msgb *msg;
269 int ret;
270
271 /* get the next msg for this timeslot */
272 msg = e1inp_tx_ts(e1i_ts, NULL);
273 if (!msg)
274 return;
275
276 ret = write(bfd->fd, msg->data, msg->len + 2);
277 msgb_free(msg);
278 if (ret == -1)
279 handle_dahdi_exception(e1i_ts);
280 else if (ret < 0)
281 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
282}
283
284static int handle_hdlc_read(struct osmo_fd *bfd)
285{
286 struct e1inp_line *line = bfd->data;
287 unsigned int ts_nr = bfd->priv_nr;
288 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
289 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI HDLC Rx");
290 int ret;
291
292 if (!msg)
293 return -ENOMEM;
294
295 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
296 if (ret == -1)
297 handle_dahdi_exception(e1i_ts);
298 else if (ret < 0) {
299 perror("read ");
300 }
301 msgb_put(msg, ret - 2);
302 if (ret <= 3) {
303 perror("read ");
304 }
305
306 return e1inp_rx_ts(e1i_ts, msg, 0, 0);
307}
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200308
309static int invertbits = 1;
310
311static uint8_t flip_table[256];
312
313static void init_flip_bits(void)
314{
315 int i,k;
316
317 for (i = 0 ; i < 256 ; i++) {
318 uint8_t sample = 0 ;
319 for (k = 0; k<8; k++) {
320 if ( i & 1 << k ) sample |= 0x80 >> k;
321 }
322 flip_table[i] = sample;
323 }
324}
325
326static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
327{
328 int i;
329 uint8_t * start = buf;
330
331 for (i = 0 ; i < len; i++) {
332 buf[i] = flip_table[(uint8_t)buf[i]];
333 }
334
335 return start;
336}
337
338#define D_BCHAN_TX_GRAN 160
339/* write to a B channel TS */
340static int handle_tsX_write(struct osmo_fd *bfd)
341{
342 struct e1inp_line *line = bfd->data;
343 unsigned int ts_nr = bfd->priv_nr;
344 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
345 uint8_t tx_buf[D_BCHAN_TX_GRAN];
346 struct subch_mux *mx = &e1i_ts->trau.mux;
347 int ret;
348
349 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
350
351 if (ret != D_BCHAN_TX_GRAN) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100352 LOGP(DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200353 if (ret < 0)
354 return ret;
355 }
356
Harald Weltecc2241b2011-07-19 16:06:06 +0200357 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200358 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
359
360 if (invertbits) {
361 flip_buf_bits(tx_buf, ret);
362 }
363
364 ret = write(bfd->fd, tx_buf, ret);
365 if (ret < D_BCHAN_TX_GRAN)
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100366 LOGP(DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
367 ret, D_BCHAN_TX_GRAN);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200368
369 return ret;
370}
371
372#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
373/* FIXME: read from a B channel TS */
374static int handle_tsX_read(struct osmo_fd *bfd)
375{
376 struct e1inp_line *line = bfd->data;
377 unsigned int ts_nr = bfd->priv_nr;
378 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
379 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
380 int ret;
381
382 if (!msg)
383 return -ENOMEM;
384
385 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
386 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100387 LOGP(DLINP, LOGL_DEBUG, "read error %d %s\n",
388 ret, strerror(errno));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200389 return ret;
390 }
391
392 if (invertbits) {
393 flip_buf_bits(msg->data, ret);
394 }
395
396 msgb_put(msg, ret);
397
398 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200399 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400 osmo_hexdump(msgb_l2(msg), ret));
401 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
402 /* physical layer indicates that data has been sent,
403 * we thus can send some more data */
404 ret = handle_tsX_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405
406 return ret;
407}
408
Harald Weltea0108e72016-07-27 21:44:50 +0200409/* write to a raw channel TS */
410static int handle_ts_raw_write(struct osmo_fd *bfd)
411{
412 struct e1inp_line *line = bfd->data;
413 unsigned int ts_nr = bfd->priv_nr;
414 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
415 struct msgb *msg;
416 int ret;
417
418 /* get the next msg for this timeslot */
419 msg = e1inp_tx_ts(e1i_ts, NULL);
420 if (!msg)
421 return 0;
422
423 if (msg->len != D_BCHAN_TX_GRAN) {
424 /* This might lead to a transmit underrun, as we call tx
425 * from the rx path, as there's no select/poll on dahdi
426 * */
427 LOGP(DLINP, LOGL_NOTICE, "unexpected msg->len = %u, "
428 "expected %u\n", msg->len, D_BCHAN_TX_GRAN);
429 }
430
431 DEBUGP(DLMIB, "RAW CHAN TX: %s\n",
432 osmo_hexdump(msg->data, msg->len));
433
434 if (0/*invertbits*/) {
435 flip_buf_bits(msg->data, msg->len);
436 }
437
438 ret = write(bfd->fd, msg->data, msg->len);
439 if (ret < msg->len)
440 LOGP(DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
441 ret, msg->len);
442 msgb_free(msg);
443
444 return ret;
445}
446
447static int handle_ts_raw_read(struct osmo_fd *bfd)
448{
449 struct e1inp_line *line = bfd->data;
450 unsigned int ts_nr = bfd->priv_nr;
451 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
452 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI Raw TS");
453 int ret;
454
455 if (!msg)
456 return -ENOMEM;
457
458 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
459 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
460 LOGP(DLINP, LOGL_DEBUG, "read error %d %s\n",
461 ret, strerror(errno));
462 return ret;
463 }
464
465 if (0/*invertbits*/) {
466 flip_buf_bits(msg->data, ret);
467 }
468
469 msgb_put(msg, ret);
470
471 msg->l2h = msg->data;
472 DEBUGP(DLMIB, "RAW CHAN RX: %s\n",
473 osmo_hexdump(msgb_l2(msg), ret));
474 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
475 /* physical layer indicates that data has been sent,
476 * we thus can send some more data */
477 ret = handle_ts_raw_write(bfd);
478
479 return ret;
480}
481
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200482/* callback from select.c in case one of the fd's can be read/written */
483static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
484{
485 struct e1inp_line *line = bfd->data;
486 unsigned int ts_nr = bfd->priv_nr;
487 unsigned int idx = ts_nr-1;
488 struct e1inp_ts *e1i_ts = &line->ts[idx];
489 int rc = 0;
490
491 switch (e1i_ts->type) {
492 case E1INP_TS_TYPE_SIGN:
493 if (what & BSC_FD_EXCEPT)
494 handle_dahdi_exception(e1i_ts);
495 if (what & BSC_FD_READ)
496 rc = handle_ts1_read(bfd);
497 if (what & BSC_FD_WRITE)
498 rc = handle_ts1_write(bfd);
499 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200500 case E1INP_TS_TYPE_HDLC:
501 if (what & BSC_FD_EXCEPT)
502 handle_dahdi_exception(e1i_ts);
503 if (what & BSC_FD_READ)
504 rc = handle_hdlc_read(bfd);
505 if (what & BSC_FD_WRITE)
506 handle_hdlc_write(bfd);
507 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200508 case E1INP_TS_TYPE_TRAU:
509 if (what & BSC_FD_EXCEPT)
510 handle_dahdi_exception(e1i_ts);
511 if (what & BSC_FD_READ)
512 rc = handle_tsX_read(bfd);
513 if (what & BSC_FD_WRITE)
514 rc = handle_tsX_write(bfd);
515 /* We never include the DAHDI B-Channel FD into the
516 * writeset, since it doesn't support poll() based
517 * write flow control */
518 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200519 case E1INP_TS_TYPE_RAW:
520 if (what & BSC_FD_EXCEPT)
521 handle_dahdi_exception(e1i_ts);
522 if (what & BSC_FD_READ)
523 rc = handle_ts_raw_read(bfd);
524 if (what & BSC_FD_WRITE)
525 rc = handle_ts_raw_write(bfd);
526 /* We never include the DAHDI B-Channel FD into the
527 * writeset, since it doesn't support poll() based
528 * write flow control */
529 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 default:
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100531 LOGP(DLINP, LOGL_NOTICE,
532 "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200533 break;
534 }
535
536 return rc;
537}
538
Harald Weltefe05cf52011-09-26 23:18:41 +0200539static void dahdi_vty_show(struct vty *vty, struct e1inp_line *line)
540{
541 struct span_cfg *scfg;
542
Holger Hans Peter Freytherf69b8122013-07-06 18:54:58 +0200543 if (line->port_nr >= ARRAY_SIZE(span_cfgs))
Harald Weltefe05cf52011-09-26 23:18:41 +0200544 return;
545
546 scfg = span_cfgs[line->port_nr];
547 if (!scfg) {
548 vty_out(vty, "DAHDI Span %u non-existant%s",
549 line->port_nr+1, VTY_NEWLINE);
550 return;
551 }
552
553 vty_out(vty, "DAHDI Span #%u, Base Nr %u, Timeslots: %u%s",
554 line->port_nr+1, scfg->chan_base, scfg->chan_num,
555 VTY_NEWLINE);
556}
557
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200558static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200559
560struct e1inp_driver dahdi_driver = {
561 .name = "dahdi",
562 .want_write = ts_want_write,
563 .line_update = &dahdi_e1_line_update,
Harald Weltefe05cf52011-09-26 23:18:41 +0200564 .vty_show = &dahdi_vty_show,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200565};
566
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100567int dahdi_set_bufinfo(int fd, int as_sigchan)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200568{
569 struct dahdi_bufferinfo bi;
570 int x = 0;
571
572 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100573 LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100574 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200575 }
576
577 if (as_sigchan) {
578 bi.numbufs = 4;
579 bi.bufsize = 512;
580 } else {
581 bi.numbufs = 8;
582 bi.bufsize = D_BCHAN_TX_GRAN;
583 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
584 }
585
586 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100587 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100588 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200589 }
590
591 if (!as_sigchan) {
592 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100593 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100594 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200595 }
596 } else {
597 int one = 1;
598 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
599 /* we cannot reliably check for the ioctl return value here
600 * as this command will fail if the slot _already_ was a
601 * signalling slot before :( */
602 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100603 return 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200604}
605
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100606static int dahdi_open_slot(int dahdi_chan_nr)
607{
608 int rc, fd;
609#ifndef DAHDI_SPECIFY
610 char openstr[128];
611 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
612#else
613 const char *openstr = "/dev/dahdi/channel";
614#endif
615 rc = open(openstr, O_RDWR | O_NONBLOCK);
616 if (rc < 0) {
617 LOGP(DLINP, LOGL_ERROR, "DAHDI: could not open %s %s\n", openstr, strerror(errno));
618 return -EIO;
619 }
620 fd = rc;
621#ifdef DAHDI_SPECIFY
622 rc = ioctl(fd, DAHDI_SPECIFY, &dahdi_chan_nr);
623 if (rc < 0) {
624 close(fd);
625 LOGP(DLINP, LOGL_ERROR, "DAHDI: could not DAHDI_SPECIFY %d: %s\n",
626 dahdi_chan_nr, strerror(errno));
627 return -EIO;
628 }
629#endif
630 return fd;
631}
632
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200633static int dahdi_e1_setup(struct e1inp_line *line)
634{
Harald Weltec2889512011-09-13 23:49:04 +0100635 struct span_cfg *scfg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200636 int ts, ret;
637
Harald Weltec2889512011-09-13 23:49:04 +0100638 reread_span_cfgs();
639
640 scfg = span_cfgs[line->port_nr];
Harald Welte0cf55142011-09-26 23:06:18 +0200641 if (!scfg) {
642 LOGP(DLMI, LOGL_ERROR, "Line %u(%s): DAHDI Port %u (Span %u) "
643 "doesn't exist\n", line->num, line->name, line->port_nr,
644 line->port_nr+1);
Harald Weltec2889512011-09-13 23:49:04 +0100645 return -EIO;
Harald Welte0cf55142011-09-26 23:06:18 +0200646 }
Harald Weltec2889512011-09-13 23:49:04 +0100647
648 line->num_ts = scfg->chan_num;
649
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200650 /* TS0 is CRC4, don't need any fd for it */
Harald Weltec2889512011-09-13 23:49:04 +0100651 for (ts = 1; ts <= scfg->chan_num; ts++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200652 unsigned int idx = ts-1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200653 struct e1inp_ts *e1i_ts = &line->ts[idx];
654 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200655 int dev_nr;
656
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200657 /* unregister FD if it was already registered */
658 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
659 osmo_fd_unregister(bfd);
660
Harald Weltef3ca61c2011-08-09 11:21:23 +0200661 /* DAHDI device names/numbers just keep incrementing
662 * even over multiple boards. So TS1 of the second
663 * board will be 32 */
Harald Weltec2889512011-09-13 23:49:04 +0100664 dev_nr = scfg->chan_base + idx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200665
666 bfd->data = line;
667 bfd->priv_nr = ts;
668 bfd->cb = dahdi_fd_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200669
670 switch (e1i_ts->type) {
671 case E1INP_TS_TYPE_NONE:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200672 /* close/release LAPD instance, if any */
673 if (e1i_ts->lapd) {
674 lapd_instance_free(e1i_ts->lapd);
675 e1i_ts->lapd = NULL;
676 }
677 if (bfd->fd) {
678 close(bfd->fd);
679 bfd->fd = 0;
680 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200681 continue;
682 break;
683 case E1INP_TS_TYPE_SIGN:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200684 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100685 bfd->fd = dahdi_open_slot(dev_nr);
686 if (bfd->fd < 0)
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100687 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200688 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100689 ret = dahdi_set_bufinfo(bfd->fd, 1);
690 if (ret < 0)
691 return ret;
692
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200693 if (!e1i_ts->lapd)
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200694 e1i_ts->lapd = lapd_instance_alloc(1,
695 dahdi_write_msg, bfd, e1inp_dlsap_up,
Andreas Eversberg3744b872011-09-27 12:12:36 +0200696 e1i_ts, &lapd_profile_abis);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200697 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200698 case E1INP_TS_TYPE_HDLC:
699 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100700 bfd->fd = dahdi_open_slot(dev_nr);
701 if (bfd->fd < 0)
Harald Welte7a228eb2016-07-28 11:09:31 +0200702 return -EIO;
Harald Welte7a228eb2016-07-28 11:09:31 +0200703 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
704 ret = dahdi_set_bufinfo(bfd->fd, 1);
705 if (ret < 0)
706 return ret;
707 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200708 case E1INP_TS_TYPE_TRAU:
Harald Weltea0108e72016-07-27 21:44:50 +0200709 case E1INP_TS_TYPE_RAW:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200710 /* close/release LAPD instance, if any */
711 if (e1i_ts->lapd) {
712 lapd_instance_free(e1i_ts->lapd);
713 e1i_ts->lapd = NULL;
714 }
715 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100716 bfd->fd = dahdi_open_slot(dev_nr);
717 if (bfd->fd < 0)
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100718 return -EIO;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100719 ret = dahdi_set_bufinfo(bfd->fd, 0);
720 if (ret < 0)
721 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200722 /* We never include the DAHDI B-Channel FD into the
723 * writeset, since it doesn't support poll() based
724 * write flow control */
725 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
726 break;
727 }
728
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100729 if (bfd->fd < 0)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200730 return bfd->fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200731
732 ret = osmo_fd_register(bfd);
733 if (ret < 0) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100734 LOGP(DLINP, LOGL_ERROR,
735 "could not register FD: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200736 strerror(ret));
737 return ret;
738 }
739 }
740
741 return 0;
742}
743
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200744static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200745{
746 if (line->driver != &dahdi_driver)
747 return -EINVAL;
748
749 return dahdi_e1_setup(line);
750}
751
752int e1inp_dahdi_init(void)
753{
754 init_flip_bits();
755
756 /* register the driver with the core */
757 return e1inp_driver_register(&dahdi_driver);
758}
Harald Welte3bc78852011-08-24 08:32:38 +0200759
760#endif /* HAVE_DAHDI_USER_H */