blob: 6da1a028c9cc48e2e7a208a1fcab9dba05aade21 [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
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020029#include <stdio.h>
30#include <unistd.h>
31#include <stdlib.h>
32#include <errno.h>
33#include <string.h>
34#include <time.h>
35#include <sys/fcntl.h>
36#include <sys/socket.h>
37#include <sys/ioctl.h>
38#include <arpa/inet.h>
39#include <dahdi/user.h>
40
Harald Weltefe05cf52011-09-26 23:18:41 +020041#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020042#include <osmocom/core/select.h>
43#include <osmocom/core/msgb.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020044#include <osmocom/core/logging.h>
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +020045#include <osmocom/core/signal.h>
Harald Welte95e5dec2011-08-16 14:41:32 +020046#include <osmocom/core/rate_ctr.h>
Harald Weltefe05cf52011-09-26 23:18:41 +020047
48#include <osmocom/vty/vty.h>
49
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020050#include <osmocom/abis/subchan_demux.h>
51#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052
Pablo Neira Ayuso355ce692011-07-05 14:53:37 +020053#include <osmocom/abis/lapd.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020054
55#define TS1_ALLOC_SIZE 300
56
Harald Weltec2889512011-09-13 23:49:04 +010057struct span_cfg {
58 struct llist_head list;
59
60 unsigned int span_nr;
61 unsigned int chan_base;
62 unsigned int chan_num;
63};
64
65static struct span_cfg *span_cfgs[DAHDI_MAX_SPANS];
66
67static int reread_span_cfgs(void)
68{
69 struct dahdi_spaninfo si;
70 unsigned int basechan = 1;
Harald Welte494f2902011-09-26 23:06:49 +020071 int span_nr;
Harald Weltec2889512011-09-13 23:49:04 +010072 int fd;
73
74 if ((fd = open("/dev/dahdi/ctl", O_RDWR)) < 0) {
75 LOGP(DLMI, LOGL_ERROR, "Unable to open DAHDI ctl: %s\n",
76 strerror(errno));
77 return -EIO;
78 }
79
Harald Weltee2bd6852011-09-26 23:03:12 +020080 for (span_nr = 1; span_nr < DAHDI_MAX_SPANS; span_nr++) {
Harald Weltec2889512011-09-13 23:49:04 +010081 struct span_cfg *scfg;
Harald Weltee2bd6852011-09-26 23:03:12 +020082 /* our array index starts at 0, but DAHDI span at 1 */
83 int i = span_nr - 1;
Harald Weltec2889512011-09-13 23:49:04 +010084
85 /* clear any old cached information */
86 if (span_cfgs[i]) {
87 talloc_free(span_cfgs[i]);
88 span_cfgs[i] = NULL;
89 }
90
91 memset(&si, 0, sizeof(si));
Harald Weltee2bd6852011-09-26 23:03:12 +020092 si.spanno = span_nr;
Harald Weltec2889512011-09-13 23:49:04 +010093 if (ioctl(fd, DAHDI_SPANSTAT, &si))
94 continue;
95
96 /* create and link new span_cfg */
97 scfg = talloc_zero(NULL, struct span_cfg);
98 if (!scfg) {
99 close(fd);
100 return -ENOMEM;
101 }
Harald Weltee2bd6852011-09-26 23:03:12 +0200102 scfg->span_nr = span_nr;
Harald Weltec2889512011-09-13 23:49:04 +0100103 scfg->chan_num = si.totalchans;
104 scfg->chan_base = basechan;
105 span_cfgs[i] = scfg;
106
107 basechan += si.totalchans;
108 }
109
110 close(fd);
111
112 return 0;
113}
114
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200115/* Corresponds to dahdi/user.h, only PRI related events */
116static const struct value_string dahdi_evt_names[] = {
117 { DAHDI_EVENT_NONE, "NONE" },
118 { DAHDI_EVENT_ALARM, "ALARM" },
119 { DAHDI_EVENT_NOALARM, "NOALARM" },
120 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
121 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
122 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
123 { DAHDI_EVENT_REMOVED, "REMOVED" },
124 { 0, NULL }
125};
126
127static void handle_dahdi_exception(struct e1inp_ts *ts)
128{
129 int rc, evt;
Harald Welte95e5dec2011-08-16 14:41:32 +0200130 struct e1inp_line *line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200131 struct input_signal_data isd;
132
133 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
134 if (rc < 0)
135 return;
136
Harald Welteb5af0992020-01-12 12:59:52 +0100137 LOGPITS(ts, DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200138 ts->line->num, ts->line->name, ts->num,
139 get_value_string(dahdi_evt_names, evt));
140
141 isd.line = ts->line;
Harald Weltef35d8892016-10-16 15:33:52 +0200142 isd.ts_nr = ts->num;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200143
144 switch (evt) {
145 case DAHDI_EVENT_ALARM:
146 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200147 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +0200148 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200149 break;
150 case DAHDI_EVENT_NOALARM:
151 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200152 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200153 break;
Harald Welte95e5dec2011-08-16 14:41:32 +0200154 case DAHDI_EVENT_ABORT:
155 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
156 break;
157 case DAHDI_EVENT_OVERRUN:
158 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
159 break;
160 case DAHDI_EVENT_BADFCS:
161 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
162 break;
163 case DAHDI_EVENT_REMOVED:
164 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
165 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200166 }
167}
168
169static int handle_ts1_read(struct osmo_fd *bfd)
170{
171 struct e1inp_line *line = bfd->data;
172 unsigned int ts_nr = bfd->priv_nr;
173 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
174 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200175 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200176
177 if (!msg)
178 return -ENOMEM;
179
180 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
181 if (ret == -1)
182 handle_dahdi_exception(e1i_ts);
183 else if (ret < 0) {
Harald Welteb5af0992020-01-12 12:59:52 +0100184 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Harald Welte510ae992020-01-12 12:17:33 +0100185 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200186 }
187 msgb_put(msg, ret - 2);
188 if (ret <= 3) {
Harald Welteb5af0992020-01-12 12:59:52 +0100189 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Harald Welte510ae992020-01-12 12:17:33 +0100190 return ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200191 }
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) {
Harald Welteb5af0992020-01-12 12:59:52 +0100202 LOGPITS(e1i_ts, 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 Welteb5af0992020-01-12 12:59:52 +0100232 LOGPITS(e1i_ts, 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 Welteb5af0992020-01-12 12:59:52 +0100252 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "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)
Harald Welteb5af0992020-01-12 12:59:52 +0100281 LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Harald Welte7a228eb2016-07-28 11:09:31 +0200282}
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) {
Harald Welteb5af0992020-01-12 12:59:52 +0100299 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Harald Welte510ae992020-01-12 12:17:33 +0100300 return ret;
Harald Welte7a228eb2016-07-28 11:09:31 +0200301 }
302 msgb_put(msg, ret - 2);
303 if (ret <= 3) {
Harald Welteb5af0992020-01-12 12:59:52 +0100304 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Harald Welte510ae992020-01-12 12:17:33 +0100305 return ret;
Harald Welte7a228eb2016-07-28 11:09:31 +0200306 }
307
308 return e1inp_rx_ts(e1i_ts, msg, 0, 0);
309}
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200310
311static int invertbits = 1;
312
313static uint8_t flip_table[256];
314
315static void init_flip_bits(void)
316{
317 int i,k;
318
319 for (i = 0 ; i < 256 ; i++) {
320 uint8_t sample = 0 ;
321 for (k = 0; k<8; k++) {
322 if ( i & 1 << k ) sample |= 0x80 >> k;
323 }
324 flip_table[i] = sample;
325 }
326}
327
328static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
329{
330 int i;
331 uint8_t * start = buf;
332
333 for (i = 0 ; i < len; i++) {
334 buf[i] = flip_table[(uint8_t)buf[i]];
335 }
336
337 return start;
338}
339
340#define D_BCHAN_TX_GRAN 160
341/* write to a B channel TS */
342static int handle_tsX_write(struct osmo_fd *bfd)
343{
344 struct e1inp_line *line = bfd->data;
345 unsigned int ts_nr = bfd->priv_nr;
346 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
347 uint8_t tx_buf[D_BCHAN_TX_GRAN];
348 struct subch_mux *mx = &e1i_ts->trau.mux;
349 int ret;
350
351 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
352
353 if (ret != D_BCHAN_TX_GRAN) {
Harald Welteb5af0992020-01-12 12:59:52 +0100354 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200355 if (ret < 0)
356 return ret;
357 }
358
Harald Welteb5af0992020-01-12 12:59:52 +0100359 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN TX: %s\n", osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200360
361 if (invertbits) {
362 flip_buf_bits(tx_buf, ret);
363 }
364
365 ret = write(bfd->fd, tx_buf, ret);
366 if (ret < D_BCHAN_TX_GRAN)
Harald Welteb5af0992020-01-12 12:59:52 +0100367 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100368 ret, D_BCHAN_TX_GRAN);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369
370 return ret;
371}
372
373#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
374/* FIXME: read from a B channel TS */
375static int handle_tsX_read(struct osmo_fd *bfd)
376{
377 struct e1inp_line *line = bfd->data;
378 unsigned int ts_nr = bfd->priv_nr;
379 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
380 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
381 int ret;
382
383 if (!msg)
384 return -ENOMEM;
385
386 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
387 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
Harald Welteb5af0992020-01-12 12:59:52 +0100388 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", 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 Welteb5af0992020-01-12 12:59:52 +0100399 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
401 /* physical layer indicates that data has been sent,
402 * we thus can send some more data */
403 ret = handle_tsX_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200404
405 return ret;
406}
407
Harald Weltea0108e72016-07-27 21:44:50 +0200408/* write to a raw channel TS */
409static int handle_ts_raw_write(struct osmo_fd *bfd)
410{
411 struct e1inp_line *line = bfd->data;
412 unsigned int ts_nr = bfd->priv_nr;
413 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
414 struct msgb *msg;
415 int ret;
416
417 /* get the next msg for this timeslot */
418 msg = e1inp_tx_ts(e1i_ts, NULL);
419 if (!msg)
420 return 0;
421
422 if (msg->len != D_BCHAN_TX_GRAN) {
423 /* This might lead to a transmit underrun, as we call tx
424 * from the rx path, as there's no select/poll on dahdi
425 * */
Harald Welteb5af0992020-01-12 12:59:52 +0100426 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unexpected msg->len = %u, "
Harald Weltea0108e72016-07-27 21:44:50 +0200427 "expected %u\n", msg->len, D_BCHAN_TX_GRAN);
428 }
429
Harald Welteb5af0992020-01-12 12:59:52 +0100430 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltea0108e72016-07-27 21:44:50 +0200431
432 if (0/*invertbits*/) {
433 flip_buf_bits(msg->data, msg->len);
434 }
435
436 ret = write(bfd->fd, msg->data, msg->len);
437 if (ret < msg->len)
Harald Welteb5af0992020-01-12 12:59:52 +0100438 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", ret, msg->len);
Harald Weltea0108e72016-07-27 21:44:50 +0200439 msgb_free(msg);
440
441 return ret;
442}
443
444static int handle_ts_raw_read(struct osmo_fd *bfd)
445{
446 struct e1inp_line *line = bfd->data;
447 unsigned int ts_nr = bfd->priv_nr;
448 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
449 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI Raw TS");
450 int ret;
451
452 if (!msg)
453 return -ENOMEM;
454
455 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
456 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
Harald Welteb5af0992020-01-12 12:59:52 +0100457 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno));
Harald Weltea0108e72016-07-27 21:44:50 +0200458 return ret;
459 }
460
461 if (0/*invertbits*/) {
462 flip_buf_bits(msg->data, ret);
463 }
464
465 msgb_put(msg, ret);
466
467 msg->l2h = msg->data;
Harald Welteb5af0992020-01-12 12:59:52 +0100468 LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
Harald Weltea0108e72016-07-27 21:44:50 +0200469 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
470 /* physical layer indicates that data has been sent,
471 * we thus can send some more data */
472 ret = handle_ts_raw_write(bfd);
473
474 return ret;
475}
476
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200477/* callback from select.c in case one of the fd's can be read/written */
478static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
479{
480 struct e1inp_line *line = bfd->data;
481 unsigned int ts_nr = bfd->priv_nr;
482 unsigned int idx = ts_nr-1;
483 struct e1inp_ts *e1i_ts = &line->ts[idx];
484 int rc = 0;
485
486 switch (e1i_ts->type) {
487 case E1INP_TS_TYPE_SIGN:
488 if (what & BSC_FD_EXCEPT)
489 handle_dahdi_exception(e1i_ts);
490 if (what & BSC_FD_READ)
491 rc = handle_ts1_read(bfd);
492 if (what & BSC_FD_WRITE)
493 rc = handle_ts1_write(bfd);
494 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200495 case E1INP_TS_TYPE_HDLC:
496 if (what & BSC_FD_EXCEPT)
497 handle_dahdi_exception(e1i_ts);
498 if (what & BSC_FD_READ)
499 rc = handle_hdlc_read(bfd);
500 if (what & BSC_FD_WRITE)
501 handle_hdlc_write(bfd);
502 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503 case E1INP_TS_TYPE_TRAU:
504 if (what & BSC_FD_EXCEPT)
505 handle_dahdi_exception(e1i_ts);
506 if (what & BSC_FD_READ)
507 rc = handle_tsX_read(bfd);
508 if (what & BSC_FD_WRITE)
509 rc = handle_tsX_write(bfd);
510 /* We never include the DAHDI B-Channel FD into the
511 * writeset, since it doesn't support poll() based
512 * write flow control */
513 break;
Harald Weltea0108e72016-07-27 21:44:50 +0200514 case E1INP_TS_TYPE_RAW:
515 if (what & BSC_FD_EXCEPT)
516 handle_dahdi_exception(e1i_ts);
517 if (what & BSC_FD_READ)
518 rc = handle_ts_raw_read(bfd);
519 if (what & BSC_FD_WRITE)
520 rc = handle_ts_raw_write(bfd);
521 /* We never include the DAHDI B-Channel FD into the
522 * writeset, since it doesn't support poll() based
523 * write flow control */
524 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525 default:
Harald Welteb5af0992020-01-12 12:59:52 +0100526 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527 break;
528 }
529
530 return rc;
531}
532
Harald Weltefe05cf52011-09-26 23:18:41 +0200533static void dahdi_vty_show(struct vty *vty, struct e1inp_line *line)
534{
535 struct span_cfg *scfg;
536
Holger Hans Peter Freytherf69b8122013-07-06 18:54:58 +0200537 if (line->port_nr >= ARRAY_SIZE(span_cfgs))
Harald Weltefe05cf52011-09-26 23:18:41 +0200538 return;
539
540 scfg = span_cfgs[line->port_nr];
541 if (!scfg) {
542 vty_out(vty, "DAHDI Span %u non-existant%s",
543 line->port_nr+1, VTY_NEWLINE);
544 return;
545 }
546
547 vty_out(vty, "DAHDI Span #%u, Base Nr %u, Timeslots: %u%s",
548 line->port_nr+1, scfg->chan_base, scfg->chan_num,
549 VTY_NEWLINE);
550}
551
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200552static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200553
554struct e1inp_driver dahdi_driver = {
555 .name = "dahdi",
556 .want_write = ts_want_write,
557 .line_update = &dahdi_e1_line_update,
Harald Weltefe05cf52011-09-26 23:18:41 +0200558 .vty_show = &dahdi_vty_show,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200559};
560
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100561int dahdi_set_bufinfo(int fd, int as_sigchan)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200562{
563 struct dahdi_bufferinfo bi;
564 int x = 0;
565
566 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100567 LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100568 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200569 }
570
571 if (as_sigchan) {
572 bi.numbufs = 4;
573 bi.bufsize = 512;
574 } else {
575 bi.numbufs = 8;
576 bi.bufsize = D_BCHAN_TX_GRAN;
577 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
578 }
579
580 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100581 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100582 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200583 }
584
585 if (!as_sigchan) {
586 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
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 } else {
591 int one = 1;
592 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
593 /* we cannot reliably check for the ioctl return value here
594 * as this command will fail if the slot _already_ was a
595 * signalling slot before :( */
596 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100597 return 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200598}
599
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100600static int dahdi_open_slot(int dahdi_chan_nr)
601{
602 int rc, fd;
603#ifndef DAHDI_SPECIFY
604 char openstr[128];
605 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
606#else
607 const char *openstr = "/dev/dahdi/channel";
608#endif
609 rc = open(openstr, O_RDWR | O_NONBLOCK);
610 if (rc < 0) {
611 LOGP(DLINP, LOGL_ERROR, "DAHDI: could not open %s %s\n", openstr, strerror(errno));
612 return -EIO;
613 }
614 fd = rc;
615#ifdef DAHDI_SPECIFY
616 rc = ioctl(fd, DAHDI_SPECIFY, &dahdi_chan_nr);
617 if (rc < 0) {
618 close(fd);
619 LOGP(DLINP, LOGL_ERROR, "DAHDI: could not DAHDI_SPECIFY %d: %s\n",
620 dahdi_chan_nr, strerror(errno));
621 return -EIO;
622 }
623#endif
624 return fd;
625}
626
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200627static int dahdi_e1_setup(struct e1inp_line *line)
628{
Harald Weltec2889512011-09-13 23:49:04 +0100629 struct span_cfg *scfg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200630 int ts, ret;
631
Harald Weltec2889512011-09-13 23:49:04 +0100632 reread_span_cfgs();
633
634 scfg = span_cfgs[line->port_nr];
Harald Welte0cf55142011-09-26 23:06:18 +0200635 if (!scfg) {
Harald Welteb5af0992020-01-12 12:59:52 +0100636 LOGPIL(line, DLMI, LOGL_ERROR, "Line %u(%s): DAHDI Port %u (Span %u) doesn't exist\n",
637 line->num, line->name, line->port_nr, line->port_nr+1);
Harald Weltec2889512011-09-13 23:49:04 +0100638 return -EIO;
Harald Welte0cf55142011-09-26 23:06:18 +0200639 }
Harald Weltec2889512011-09-13 23:49:04 +0100640
641 line->num_ts = scfg->chan_num;
642
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200643 /* TS0 is CRC4, don't need any fd for it */
Harald Weltec2889512011-09-13 23:49:04 +0100644 for (ts = 1; ts <= scfg->chan_num; ts++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200645 unsigned int idx = ts-1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200646 struct e1inp_ts *e1i_ts = &line->ts[idx];
647 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200648 int dev_nr;
649
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200650 /* unregister FD if it was already registered */
651 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
652 osmo_fd_unregister(bfd);
653
Harald Weltef3ca61c2011-08-09 11:21:23 +0200654 /* DAHDI device names/numbers just keep incrementing
655 * even over multiple boards. So TS1 of the second
656 * board will be 32 */
Harald Weltec2889512011-09-13 23:49:04 +0100657 dev_nr = scfg->chan_base + idx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200658
659 bfd->data = line;
660 bfd->priv_nr = ts;
661 bfd->cb = dahdi_fd_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200662
663 switch (e1i_ts->type) {
664 case E1INP_TS_TYPE_NONE:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200665 /* close/release LAPD instance, if any */
666 if (e1i_ts->lapd) {
667 lapd_instance_free(e1i_ts->lapd);
668 e1i_ts->lapd = NULL;
669 }
670 if (bfd->fd) {
671 close(bfd->fd);
672 bfd->fd = 0;
673 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200674 continue;
675 break;
676 case E1INP_TS_TYPE_SIGN:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200677 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100678 bfd->fd = dahdi_open_slot(dev_nr);
679 if (bfd->fd < 0)
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100680 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200681 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100682 ret = dahdi_set_bufinfo(bfd->fd, 1);
683 if (ret < 0)
684 return ret;
685
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200686 if (!e1i_ts->lapd)
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200687 e1i_ts->lapd = lapd_instance_alloc(1,
688 dahdi_write_msg, bfd, e1inp_dlsap_up,
Andreas Eversberg3744b872011-09-27 12:12:36 +0200689 e1i_ts, &lapd_profile_abis);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200690 break;
Harald Welte7a228eb2016-07-28 11:09:31 +0200691 case E1INP_TS_TYPE_HDLC:
692 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100693 bfd->fd = dahdi_open_slot(dev_nr);
694 if (bfd->fd < 0)
Harald Welte7a228eb2016-07-28 11:09:31 +0200695 return -EIO;
Harald Welte7a228eb2016-07-28 11:09:31 +0200696 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
697 ret = dahdi_set_bufinfo(bfd->fd, 1);
698 if (ret < 0)
699 return ret;
700 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200701 case E1INP_TS_TYPE_TRAU:
Harald Weltea0108e72016-07-27 21:44:50 +0200702 case E1INP_TS_TYPE_RAW:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200703 /* close/release LAPD instance, if any */
704 if (e1i_ts->lapd) {
705 lapd_instance_free(e1i_ts->lapd);
706 e1i_ts->lapd = NULL;
707 }
708 if (!bfd->fd)
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100709 bfd->fd = dahdi_open_slot(dev_nr);
710 if (bfd->fd < 0)
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100711 return -EIO;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100712 ret = dahdi_set_bufinfo(bfd->fd, 0);
713 if (ret < 0)
714 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200715 /* We never include the DAHDI B-Channel FD into the
716 * writeset, since it doesn't support poll() based
717 * write flow control */
718 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
719 break;
720 }
721
Harald Welte7c1c8cc2019-11-06 21:50:20 +0100722 if (bfd->fd < 0)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200723 return bfd->fd;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200724
725 ret = osmo_fd_register(bfd);
726 if (ret < 0) {
Harald Welteb5af0992020-01-12 12:59:52 +0100727 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200728 return ret;
729 }
730 }
731
732 return 0;
733}
734
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200735static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200736{
737 if (line->driver != &dahdi_driver)
738 return -EINVAL;
739
740 return dahdi_e1_setup(line);
741}
742
743int e1inp_dahdi_init(void)
744{
745 init_flip_bits();
746
747 /* register the driver with the core */
748 return e1inp_driver_register(&dahdi_driver);
749}