blob: ab069082540e16d33d577f2e721e942c789fb001 [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
Holger Hans Peter Freytherd9d1b5c2014-09-25 18:47:12 +020025#include "config.h"
Harald Welte3bc78852011-08-24 08:32:38 +020026
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;
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) {
184 perror("read ");
185 }
186 msgb_put(msg, ret - 2);
187 if (ret <= 3) {
188 perror("read ");
189 }
190
Harald Weltefd44a5f2011-08-21 00:48:54 +0200191 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200192}
193
194static int ts_want_write(struct e1inp_ts *e1i_ts)
195{
196 /* We never include the DAHDI B-Channel FD into the
197 * writeset, since it doesn't support poll() based
198 * write flow control */
199 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100200 LOGP(DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200201 return 0;
202 }
203
204 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
205
206 return 0;
207}
208
209static void timeout_ts1_write(void *data)
210{
211 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
212
213 /* trigger write of ts1, due to tx delay timer */
214 ts_want_write(e1i_ts);
215}
216
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200217static void dahdi_write_msg(struct msgb *msg, void *cbdata)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200218{
219 struct osmo_fd *bfd = cbdata;
220 struct e1inp_line *line = bfd->data;
221 unsigned int ts_nr = bfd->priv_nr;
222 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
223 int ret;
224
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200225 ret = write(bfd->fd, msg->data, msg->len + 2);
226 msgb_free(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200227 if (ret == -1)
228 handle_dahdi_exception(e1i_ts);
229 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200230 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200231}
232
233static int handle_ts1_write(struct osmo_fd *bfd)
234{
235 struct e1inp_line *line = bfd->data;
236 unsigned int ts_nr = bfd->priv_nr;
237 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
238 struct e1inp_sign_link *sign_link;
239 struct msgb *msg;
240
241 bfd->when &= ~BSC_FD_WRITE;
242
243 /* get the next msg for this timeslot */
244 msg = e1inp_tx_ts(e1i_ts, &sign_link);
245 if (!msg) {
246 /* no message after tx delay timer */
247 return 0;
248 }
249
Harald Weltecc2241b2011-07-19 16:06:06 +0200250 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200251 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200252 sign_link->sapi, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200253
254 /* set tx delay timer for next event */
255 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
256 e1i_ts->sign.tx_timer.data = e1i_ts;
257 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
258
259 return 0;
260}
261
262
263static int invertbits = 1;
264
265static uint8_t flip_table[256];
266
267static void init_flip_bits(void)
268{
269 int i,k;
270
271 for (i = 0 ; i < 256 ; i++) {
272 uint8_t sample = 0 ;
273 for (k = 0; k<8; k++) {
274 if ( i & 1 << k ) sample |= 0x80 >> k;
275 }
276 flip_table[i] = sample;
277 }
278}
279
280static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
281{
282 int i;
283 uint8_t * start = buf;
284
285 for (i = 0 ; i < len; i++) {
286 buf[i] = flip_table[(uint8_t)buf[i]];
287 }
288
289 return start;
290}
291
292#define D_BCHAN_TX_GRAN 160
293/* write to a B channel TS */
294static int handle_tsX_write(struct osmo_fd *bfd)
295{
296 struct e1inp_line *line = bfd->data;
297 unsigned int ts_nr = bfd->priv_nr;
298 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
299 uint8_t tx_buf[D_BCHAN_TX_GRAN];
300 struct subch_mux *mx = &e1i_ts->trau.mux;
301 int ret;
302
303 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
304
305 if (ret != D_BCHAN_TX_GRAN) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100306 LOGP(DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200307 if (ret < 0)
308 return ret;
309 }
310
Harald Weltecc2241b2011-07-19 16:06:06 +0200311 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200312 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
313
314 if (invertbits) {
315 flip_buf_bits(tx_buf, ret);
316 }
317
318 ret = write(bfd->fd, tx_buf, ret);
319 if (ret < D_BCHAN_TX_GRAN)
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100320 LOGP(DLINP, LOGL_DEBUG, "send returns %d instead of %d\n",
321 ret, D_BCHAN_TX_GRAN);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200322
323 return ret;
324}
325
326#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
327/* FIXME: read from a B channel TS */
328static int handle_tsX_read(struct osmo_fd *bfd)
329{
330 struct e1inp_line *line = bfd->data;
331 unsigned int ts_nr = bfd->priv_nr;
332 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
333 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
334 int ret;
335
336 if (!msg)
337 return -ENOMEM;
338
339 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
340 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100341 LOGP(DLINP, LOGL_DEBUG, "read error %d %s\n",
342 ret, strerror(errno));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200343 return ret;
344 }
345
346 if (invertbits) {
347 flip_buf_bits(msg->data, ret);
348 }
349
350 msgb_put(msg, ret);
351
352 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200353 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354 osmo_hexdump(msgb_l2(msg), ret));
355 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
356 /* physical layer indicates that data has been sent,
357 * we thus can send some more data */
358 ret = handle_tsX_write(bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200359
360 return ret;
361}
362
363/* callback from select.c in case one of the fd's can be read/written */
364static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
365{
366 struct e1inp_line *line = bfd->data;
367 unsigned int ts_nr = bfd->priv_nr;
368 unsigned int idx = ts_nr-1;
369 struct e1inp_ts *e1i_ts = &line->ts[idx];
370 int rc = 0;
371
372 switch (e1i_ts->type) {
373 case E1INP_TS_TYPE_SIGN:
374 if (what & BSC_FD_EXCEPT)
375 handle_dahdi_exception(e1i_ts);
376 if (what & BSC_FD_READ)
377 rc = handle_ts1_read(bfd);
378 if (what & BSC_FD_WRITE)
379 rc = handle_ts1_write(bfd);
380 break;
381 case E1INP_TS_TYPE_TRAU:
382 if (what & BSC_FD_EXCEPT)
383 handle_dahdi_exception(e1i_ts);
384 if (what & BSC_FD_READ)
385 rc = handle_tsX_read(bfd);
386 if (what & BSC_FD_WRITE)
387 rc = handle_tsX_write(bfd);
388 /* We never include the DAHDI B-Channel FD into the
389 * writeset, since it doesn't support poll() based
390 * write flow control */
391 break;
392 default:
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100393 LOGP(DLINP, LOGL_NOTICE,
394 "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200395 break;
396 }
397
398 return rc;
399}
400
Harald Weltefe05cf52011-09-26 23:18:41 +0200401static void dahdi_vty_show(struct vty *vty, struct e1inp_line *line)
402{
403 struct span_cfg *scfg;
404
Holger Hans Peter Freytherf69b8122013-07-06 18:54:58 +0200405 if (line->port_nr >= ARRAY_SIZE(span_cfgs))
Harald Weltefe05cf52011-09-26 23:18:41 +0200406 return;
407
408 scfg = span_cfgs[line->port_nr];
409 if (!scfg) {
410 vty_out(vty, "DAHDI Span %u non-existant%s",
411 line->port_nr+1, VTY_NEWLINE);
412 return;
413 }
414
415 vty_out(vty, "DAHDI Span #%u, Base Nr %u, Timeslots: %u%s",
416 line->port_nr+1, scfg->chan_base, scfg->chan_num,
417 VTY_NEWLINE);
418}
419
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200420static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200421
422struct e1inp_driver dahdi_driver = {
423 .name = "dahdi",
424 .want_write = ts_want_write,
425 .line_update = &dahdi_e1_line_update,
Harald Weltefe05cf52011-09-26 23:18:41 +0200426 .vty_show = &dahdi_vty_show,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200427};
428
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100429int dahdi_set_bufinfo(int fd, int as_sigchan)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200430{
431 struct dahdi_bufferinfo bi;
432 int x = 0;
433
434 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100435 LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100436 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200437 }
438
439 if (as_sigchan) {
440 bi.numbufs = 4;
441 bi.bufsize = 512;
442 } else {
443 bi.numbufs = 8;
444 bi.bufsize = D_BCHAN_TX_GRAN;
445 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
446 }
447
448 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100449 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100450 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200451 }
452
453 if (!as_sigchan) {
454 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100455 LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n");
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100456 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200457 }
458 } else {
459 int one = 1;
460 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
461 /* we cannot reliably check for the ioctl return value here
462 * as this command will fail if the slot _already_ was a
463 * signalling slot before :( */
464 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100465 return 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200466}
467
468static int dahdi_e1_setup(struct e1inp_line *line)
469{
Harald Weltec2889512011-09-13 23:49:04 +0100470 struct span_cfg *scfg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200471 int ts, ret;
472
Harald Weltec2889512011-09-13 23:49:04 +0100473 reread_span_cfgs();
474
475 scfg = span_cfgs[line->port_nr];
Harald Welte0cf55142011-09-26 23:06:18 +0200476 if (!scfg) {
477 LOGP(DLMI, LOGL_ERROR, "Line %u(%s): DAHDI Port %u (Span %u) "
478 "doesn't exist\n", line->num, line->name, line->port_nr,
479 line->port_nr+1);
Harald Weltec2889512011-09-13 23:49:04 +0100480 return -EIO;
Harald Welte0cf55142011-09-26 23:06:18 +0200481 }
Harald Weltec2889512011-09-13 23:49:04 +0100482
483 line->num_ts = scfg->chan_num;
484
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485 /* TS0 is CRC4, don't need any fd for it */
Harald Weltec2889512011-09-13 23:49:04 +0100486 for (ts = 1; ts <= scfg->chan_num; ts++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200487 unsigned int idx = ts-1;
488 char openstr[128];
489 struct e1inp_ts *e1i_ts = &line->ts[idx];
490 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200491 int dev_nr;
492
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200493 /* unregister FD if it was already registered */
494 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
495 osmo_fd_unregister(bfd);
496
Harald Weltef3ca61c2011-08-09 11:21:23 +0200497 /* DAHDI device names/numbers just keep incrementing
498 * even over multiple boards. So TS1 of the second
499 * board will be 32 */
Harald Weltec2889512011-09-13 23:49:04 +0100500 dev_nr = scfg->chan_base + idx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200501
502 bfd->data = line;
503 bfd->priv_nr = ts;
504 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200505 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506
507 switch (e1i_ts->type) {
508 case E1INP_TS_TYPE_NONE:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200509 /* close/release LAPD instance, if any */
510 if (e1i_ts->lapd) {
511 lapd_instance_free(e1i_ts->lapd);
512 e1i_ts->lapd = NULL;
513 }
514 if (bfd->fd) {
515 close(bfd->fd);
516 bfd->fd = 0;
517 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200518 continue;
519 break;
520 case E1INP_TS_TYPE_SIGN:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200521 if (!bfd->fd)
522 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523 if (bfd->fd == -1) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100524 LOGP(DLINP, LOGL_ERROR,
525 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526 __func__, openstr, strerror(errno));
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100527 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200528 }
529 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100530 ret = dahdi_set_bufinfo(bfd->fd, 1);
531 if (ret < 0)
532 return ret;
533
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200534 if (!e1i_ts->lapd)
Andreas Eversberga7ff0012011-09-26 11:29:30 +0200535 e1i_ts->lapd = lapd_instance_alloc(1,
536 dahdi_write_msg, bfd, e1inp_dlsap_up,
Andreas Eversberg3744b872011-09-27 12:12:36 +0200537 e1i_ts, &lapd_profile_abis);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538 break;
539 case E1INP_TS_TYPE_TRAU:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200540 /* close/release LAPD instance, if any */
541 if (e1i_ts->lapd) {
542 lapd_instance_free(e1i_ts->lapd);
543 e1i_ts->lapd = NULL;
544 }
545 if (!bfd->fd)
546 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200547 if (bfd->fd == -1) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100548 LOGP(DLINP, LOGL_ERROR,
549 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200550 __func__, openstr, strerror(errno));
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100551 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200552 }
Pablo Neira Ayuso7ed92582012-03-12 18:38:45 +0100553 ret = dahdi_set_bufinfo(bfd->fd, 0);
554 if (ret < 0)
555 return -EIO;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200556 /* We never include the DAHDI B-Channel FD into the
557 * writeset, since it doesn't support poll() based
558 * write flow control */
559 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
560 break;
561 }
562
563 if (bfd->fd < 0) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100564 LOGP(DLINP, LOGL_ERROR,
565 "%s could not open %s %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200566 __func__, openstr, strerror(errno));
567 return bfd->fd;
568 }
569
570 ret = osmo_fd_register(bfd);
571 if (ret < 0) {
Pablo Neira Ayuso5c67fb52012-03-12 18:34:12 +0100572 LOGP(DLINP, LOGL_ERROR,
573 "could not register FD: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200574 strerror(ret));
575 return ret;
576 }
577 }
578
579 return 0;
580}
581
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200582static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200583{
584 if (line->driver != &dahdi_driver)
585 return -EINVAL;
586
587 return dahdi_e1_setup(line);
588}
589
590int e1inp_dahdi_init(void)
591{
592 init_flip_bits();
593
594 /* register the driver with the core */
595 return e1inp_driver_register(&dahdi_driver);
596}
Harald Welte3bc78852011-08-24 08:32:38 +0200597
598#endif /* HAVE_DAHDI_USER_H */