blob: 8d3e060a38349179e8eadb7559ce4a46d8a38906 [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
Harald Welte3bc78852011-08-24 08:32:38 +020025#include "../../config.h"
26
27#ifdef HAVE_DAHDI_USER_H
28
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 Weltecc2241b2011-07-19 16:06:06 +0200137 LOGP(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;
142
143 switch (evt) {
144 case DAHDI_EVENT_ALARM:
145 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200146 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +0200147 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200148 break;
149 case DAHDI_EVENT_NOALARM:
150 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200151 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200152 break;
Harald Welte95e5dec2011-08-16 14:41:32 +0200153 case DAHDI_EVENT_ABORT:
154 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
155 break;
156 case DAHDI_EVENT_OVERRUN:
157 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
158 break;
159 case DAHDI_EVENT_BADFCS:
160 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
161 break;
162 case DAHDI_EVENT_REMOVED:
163 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
164 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200165 }
166}
167
168static int handle_ts1_read(struct osmo_fd *bfd)
169{
170 struct e1inp_line *line = bfd->data;
171 unsigned int ts_nr = bfd->priv_nr;
172 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
173 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200174 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200175
176 if (!msg)
177 return -ENOMEM;
178
179 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
180 if (ret == -1)
181 handle_dahdi_exception(e1i_ts);
182 else if (ret < 0) {
183 perror("read ");
184 }
185 msgb_put(msg, ret - 2);
186 if (ret <= 3) {
187 perror("read ");
188 }
189
Harald Weltefd44a5f2011-08-21 00:48:54 +0200190 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200191}
192
193static int ts_want_write(struct e1inp_ts *e1i_ts)
194{
195 /* We never include the DAHDI B-Channel FD into the
196 * writeset, since it doesn't support poll() based
197 * write flow control */
198 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100199 LOGP(DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200200 return 0;
201 }
202
203 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
204
205 return 0;
206}
207
208static void timeout_ts1_write(void *data)
209{
210 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
211
212 /* trigger write of ts1, due to tx delay timer */
213 ts_want_write(e1i_ts);
214}
215
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200216static void dahdi_write_msg(struct msgb *msg, void *cbdata)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200217{
218 struct osmo_fd *bfd = cbdata;
219 struct e1inp_line *line = bfd->data;
220 unsigned int ts_nr = bfd->priv_nr;
221 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
222 int ret;
223
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200224 ret = write(bfd->fd, msg->data, msg->len + 2);
225 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200226 if (ret == -1)
227 handle_dahdi_exception(e1i_ts);
228 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200229 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200230}
231
232static int handle_ts1_write(struct osmo_fd *bfd)
233{
234 struct e1inp_line *line = bfd->data;
235 unsigned int ts_nr = bfd->priv_nr;
236 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
237 struct e1inp_sign_link *sign_link;
238 struct msgb *msg;
239
240 bfd->when &= ~BSC_FD_WRITE;
241
242 /* get the next msg for this timeslot */
243 msg = e1inp_tx_ts(e1i_ts, &sign_link);
244 if (!msg) {
245 /* no message after tx delay timer */
246 return 0;
247 }
248
Harald Weltecc2241b2011-07-19 16:06:06 +0200249 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200250 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200251 sign_link->sapi, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200252
253 /* set tx delay timer for next event */
254 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
255 e1i_ts->sign.tx_timer.data = e1i_ts;
256 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
257
258 return 0;
259}
260
261
262static int invertbits = 1;
263
264static uint8_t flip_table[256];
265
266static void init_flip_bits(void)
267{
268 int i,k;
269
270 for (i = 0 ; i < 256 ; i++) {
271 uint8_t sample = 0 ;
272 for (k = 0; k<8; k++) {
273 if ( i & 1 << k ) sample |= 0x80 >> k;
274 }
275 flip_table[i] = sample;
276 }
277}
278
279static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
280{
281 int i;
282 uint8_t * start = buf;
283
284 for (i = 0 ; i < len; i++) {
285 buf[i] = flip_table[(uint8_t)buf[i]];
286 }
287
288 return start;
289}
290
291#define D_BCHAN_TX_GRAN 160
292/* write to a B channel TS */
293static int handle_tsX_write(struct osmo_fd *bfd)
294{
295 struct e1inp_line *line = bfd->data;
296 unsigned int ts_nr = bfd->priv_nr;
297 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
298 uint8_t tx_buf[D_BCHAN_TX_GRAN];
299 struct subch_mux *mx = &e1i_ts->trau.mux;
300 int ret;
301
302 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
303
304 if (ret != D_BCHAN_TX_GRAN) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100305 LOGP(DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200306 if (ret < 0)
307 return ret;
308 }
309
Harald Weltecc2241b2011-07-19 16:06:06 +0200310 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200311 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
312
313 if (invertbits) {
314 flip_buf_bits(tx_buf, ret);
315 }
316
317 ret = write(bfd->fd, tx_buf, ret);
318 if (ret < D_BCHAN_TX_GRAN)
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100319 LOGP(DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
320 ret, D_BCHAN_TX_GRAN);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200321
322 return ret;
323}
324
325#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
326/* FIXME: read from a B channel TS */
327static int handle_tsX_read(struct osmo_fd *bfd)
328{
329 struct e1inp_line *line = bfd->data;
330 unsigned int ts_nr = bfd->priv_nr;
331 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
332 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
333 int ret;
334
335 if (!msg)
336 return -ENOMEM;
337
338 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
339 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100340 LOGP(DLINP, LOGL_DEBUG, "read error %d %s\n",
341 ret, strerror(errno));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342 return ret;
343 }
344
345 if (invertbits) {
346 flip_buf_bits(msg->data, ret);
347 }
348
349 msgb_put(msg, ret);
350
351 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200352 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200353 osmo_hexdump(msgb_l2(msg), ret));
354 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
355 /* physical layer indicates that data has been sent,
356 * we thus can send some more data */
357 ret = handle_tsX_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200358
359 return ret;
360}
361
362/* callback from select.c in case one of the fd's can be read/written */
363static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
364{
365 struct e1inp_line *line = bfd->data;
366 unsigned int ts_nr = bfd->priv_nr;
367 unsigned int idx = ts_nr-1;
368 struct e1inp_ts *e1i_ts = &line->ts[idx];
369 int rc = 0;
370
371 switch (e1i_ts->type) {
372 case E1INP_TS_TYPE_SIGN:
373 if (what & BSC_FD_EXCEPT)
374 handle_dahdi_exception(e1i_ts);
375 if (what & BSC_FD_READ)
376 rc = handle_ts1_read(bfd);
377 if (what & BSC_FD_WRITE)
378 rc = handle_ts1_write(bfd);
379 break;
380 case E1INP_TS_TYPE_TRAU:
381 if (what & BSC_FD_EXCEPT)
382 handle_dahdi_exception(e1i_ts);
383 if (what & BSC_FD_READ)
384 rc = handle_tsX_read(bfd);
385 if (what & BSC_FD_WRITE)
386 rc = handle_tsX_write(bfd);
387 /* We never include the DAHDI B-Channel FD into the
388 * writeset, since it doesn't support poll() based
389 * write flow control */
390 break;
391 default:
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100392 LOGP(DLINP, LOGL_NOTICE,
393 "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200394 break;
395 }
396
397 return rc;
398}
399
Harald Weltefe05cf52011-09-26 23:18:41 +0200400static void dahdi_vty_show(struct vty *vty, struct e1inp_line *line)
401{
402 struct span_cfg *scfg;
403
404 if (line->port_nr > ARRAY_SIZE(span_cfgs))
405 return;
406
407 scfg = span_cfgs[line->port_nr];
408 if (!scfg) {
409 vty_out(vty, "DAHDI Span %u non-existant%s",
410 line->port_nr+1, VTY_NEWLINE);
411 return;
412 }
413
414 vty_out(vty, "DAHDI Span #%u, Base Nr %u, Timeslots: %u%s",
415 line->port_nr+1, scfg->chan_base, scfg->chan_num,
416 VTY_NEWLINE);
417}
418
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200419static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420
421struct e1inp_driver dahdi_driver = {
422 .name = "dahdi",
423 .want_write = ts_want_write,
424 .line_update = &dahdi_e1_line_update,
Harald Weltefe05cf52011-09-26 23:18:41 +0200425 .vty_show = &dahdi_vty_show,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200426};
427
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100428int dahdi_set_bufinfo(int fd, int as_sigchan)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200429{
430 struct dahdi_bufferinfo bi;
431 int x = 0;
432
433 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100434 LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100435 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200436 }
437
438 if (as_sigchan) {
439 bi.numbufs = 4;
440 bi.bufsize = 512;
441 } else {
442 bi.numbufs = 8;
443 bi.bufsize = D_BCHAN_TX_GRAN;
444 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
445 }
446
447 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100448 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100449 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200450 }
451
452 if (!as_sigchan) {
453 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100454 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100455 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200456 }
457 } else {
458 int one = 1;
459 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
460 /* we cannot reliably check for the ioctl return value here
461 * as this command will fail if the slot _already_ was a
462 * signalling slot before :( */
463 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100464 return 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200465}
466
467static int dahdi_e1_setup(struct e1inp_line *line)
468{
Harald Weltec2889512011-09-13 23:49:04 +0100469 struct span_cfg *scfg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200470 int ts, ret;
471
Harald Weltec2889512011-09-13 23:49:04 +0100472 reread_span_cfgs();
473
474 scfg = span_cfgs[line->port_nr];
Harald Welte0cf55142011-09-26 23:06:18 +0200475 if (!scfg) {
476 LOGP(DLMI, LOGL_ERROR, "Line %u(%s): DAHDI Port %u (Span %u) "
477 "doesn't exist\n", line->num, line->name, line->port_nr,
478 line->port_nr+1);
Harald Weltec2889512011-09-13 23:49:04 +0100479 return -EIO;
Harald Welte0cf55142011-09-26 23:06:18 +0200480 }
Harald Weltec2889512011-09-13 23:49:04 +0100481
482 line->num_ts = scfg->chan_num;
483
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200484 /* TS0 is CRC4, don't need any fd for it */
Harald Weltec2889512011-09-13 23:49:04 +0100485 for (ts = 1; ts <= scfg->chan_num; ts++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200486 unsigned int idx = ts-1;
487 char openstr[128];
488 struct e1inp_ts *e1i_ts = &line->ts[idx];
489 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200490 int dev_nr;
491
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200492 /* unregister FD if it was already registered */
493 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
494 osmo_fd_unregister(bfd);
495
Harald Weltef3ca61c2011-08-09 11:21:23 +0200496 /* DAHDI device names/numbers just keep incrementing
497 * even over multiple boards. So TS1 of the second
498 * board will be 32 */
Harald Weltec2889512011-09-13 23:49:04 +0100499 dev_nr = scfg->chan_base + idx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200500
501 bfd->data = line;
502 bfd->priv_nr = ts;
503 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200504 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200505
506 switch (e1i_ts->type) {
507 case E1INP_TS_TYPE_NONE:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200508 /* close/release LAPD instance, if any */
509 if (e1i_ts->lapd) {
510 lapd_instance_free(e1i_ts->lapd);
511 e1i_ts->lapd = NULL;
512 }
513 if (bfd->fd) {
514 close(bfd->fd);
515 bfd->fd = 0;
516 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200517 continue;
518 break;
519 case E1INP_TS_TYPE_SIGN:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200520 if (!bfd->fd)
521 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200522 if (bfd->fd == -1) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100523 LOGP(DLINP, LOGL_ERROR,
524 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200525 __func__, openstr, strerror(errno));
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100526 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200527 }
528 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100529 ret = dahdi_set_bufinfo(bfd->fd, 1);
530 if (ret < 0)
531 return ret;
532
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200533 if (!e1i_ts->lapd)
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200534 e1i_ts->lapd = lapd_instance_alloc(1,
535 dahdi_write_msg, bfd, e1inp_dlsap_up,
Andreas Eversberg3744b872011-09-27 12:12:36 +0200536 e1i_ts, &lapd_profile_abis);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 break;
538 case E1INP_TS_TYPE_TRAU:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200539 /* close/release LAPD instance, if any */
540 if (e1i_ts->lapd) {
541 lapd_instance_free(e1i_ts->lapd);
542 e1i_ts->lapd = NULL;
543 }
544 if (!bfd->fd)
545 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200546 if (bfd->fd == -1) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100547 LOGP(DLINP, LOGL_ERROR,
548 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200549 __func__, openstr, strerror(errno));
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100550 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200551 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100552 ret = dahdi_set_bufinfo(bfd->fd, 0);
553 if (ret < 0)
554 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200555 /* We never include the DAHDI B-Channel FD into the
556 * writeset, since it doesn't support poll() based
557 * write flow control */
558 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
559 break;
560 }
561
562 if (bfd->fd < 0) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100563 LOGP(DLINP, LOGL_ERROR,
564 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200565 __func__, openstr, strerror(errno));
566 return bfd->fd;
567 }
568
569 ret = osmo_fd_register(bfd);
570 if (ret < 0) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100571 LOGP(DLINP, LOGL_ERROR,
572 "could not register FD: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573 strerror(ret));
574 return ret;
575 }
576 }
577
578 return 0;
579}
580
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200581static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200582{
583 if (line->driver != &dahdi_driver)
584 return -EINVAL;
585
586 return dahdi_e1_setup(line);
587}
588
589int e1inp_dahdi_init(void)
590{
591 init_flip_bits();
592
593 /* register the driver with the core */
594 return e1inp_driver_register(&dahdi_driver);
595}
Harald Welte3bc78852011-08-24 08:32:38 +0200596
597#endif /* HAVE_DAHDI_USER_H */