blob: 6b70791c34d03791fd8ed0880f8e37eb0a5fa67e [file] [log] [blame]
Matthew Fredricksond105e202010-02-16 22:07:04 +01001/* OpenBSC Abis input driver for DAHDI */
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +01002
Harald Welteba0db5b2011-02-04 21:33:14 +01003/* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +01004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Matthew Fredricksond105e202010-02-16 22:07:04 +01005 * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +01006 *
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 Welteca17ef82011-02-05 15:13:27 +010025#include "../../bscconfig.h"
26
27#ifdef HAVE_DAHDI_USER_H
28
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010029#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/types.h>
37#include <sys/socket.h>
38#include <sys/ioctl.h>
39#include <arpa/inet.h>
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -050040#include <dahdi/user.h>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010041
Harald Welte1dd68c32011-02-05 13:58:46 +010042#include <osmocore/select.h>
43#include <osmocore/msgb.h>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010044#include <openbsc/debug.h>
45#include <openbsc/gsm_data.h>
46#include <openbsc/abis_nm.h>
47#include <openbsc/abis_rsl.h>
48#include <openbsc/subchan_demux.h>
49#include <openbsc/e1_input.h>
Harald Weltef338a032011-01-14 15:55:42 +010050#include <openbsc/signal.h>
Harald Welte1dd68c32011-02-05 13:58:46 +010051#include <osmocore/talloc.h>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010052
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +010053#include "lapd.h"
54
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010055#define TS1_ALLOC_SIZE 300
56
Harald Welte44f04da2011-02-13 14:19:26 +010057/* Corresponds to dahdi/user.h, only PRI related events */
58static const struct value_string dahdi_evt_names[] = {
59 { DAHDI_EVENT_NONE, "NONE" },
60 { DAHDI_EVENT_ALARM, "ALARM" },
61 { DAHDI_EVENT_NOALARM, "NOALARM" },
62 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
63 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
64 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
65 { DAHDI_EVENT_REMOVED, "REMOVED" },
66 { 0, NULL }
67};
68
69static void handle_dahdi_exception(struct e1inp_ts *ts)
70{
71 int rc, evt;
72
73 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
74 if (rc < 0)
75 return;
76
77 LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
78 ts->line->num, ts->line->name, ts->num,
79 get_value_string(dahdi_evt_names, evt));
80
81 switch (evt) {
82 case DAHDI_EVENT_ALARM:
83 /* FIXME: we should notify the code that the line is gone */
84 break;
85 case DAHDI_EVENT_NOALARM:
86 /* FIXME: alarm has gone, we should re-start the SABM requests */
87 break;
88 }
89}
90
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010091static int handle_ts1_read(struct bsc_fd *bfd)
92{
93 struct e1inp_line *line = bfd->data;
94 unsigned int ts_nr = bfd->priv_nr;
95 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010096 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Welteba0db5b2011-02-04 21:33:14 +010097 lapd_mph_type prim;
98 unsigned int sapi, tei;
99 int ilen, ret;
Harald Welted38f1052011-02-05 19:13:00 +0100100 uint8_t *idata;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100101
102 if (!msg)
103 return -ENOMEM;
104
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500105 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
Harald Welte44f04da2011-02-13 14:19:26 +0100106 if (ret == -1)
107 handle_dahdi_exception(e1i_ts);
108 else if (ret < 0) {
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100109 perror("read ");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100110 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500111 msgb_put(msg, ret - 2);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100112 if (ret <= 3) {
113 perror("read ");
114 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100115
Harald Welteba0db5b2011-02-04 21:33:14 +0100116 sapi = msg->data[0] >> 2;
117 tei = msg->data[1] >> 1;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500118
Harald Welteba0db5b2011-02-04 21:33:14 +0100119 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100120
Harald Welted38f1052011-02-05 19:13:00 +0100121 idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim);
Harald Welte1458ec62011-02-05 19:50:44 +0100122 if (!idata && prim == 0)
Harald Welted38f1052011-02-05 19:13:00 +0100123 return -EIO;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100124
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100125 msgb_pull(msg, 2);
126
Harald Welteba0db5b2011-02-04 21:33:14 +0100127 DEBUGP(DMI, "prim %08x\n", prim);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100128
Harald Welteba0db5b2011-02-04 21:33:14 +0100129 switch (prim) {
130 case 0:
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100131 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100132 case LAPD_MPH_ACTIVATE_IND:
133 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Harald Weltef338a032011-01-14 15:55:42 +0100134 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100135 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100136 case LAPD_MPH_DEACTIVATE_IND:
137 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Harald Weltef338a032011-01-14 15:55:42 +0100138 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, tei, sapi);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100139 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100140 case LAPD_DL_DATA_IND:
141 case LAPD_DL_UNITDATA_IND:
Harald Welte1dd68c32011-02-05 13:58:46 +0100142 if (prim == LAPD_DL_DATA_IND)
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500143 msg->l2h = msg->data + 2;
144 else
145 msg->l2h = msg->data + 1;
146 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret));
Harald Welteba0db5b2011-02-04 21:33:14 +0100147 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100148 break;
149 default:
Harald Welteba0db5b2011-02-04 21:33:14 +0100150 printf("ERROR: unknown prim\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100151 break;
152 }
Harald Welteba0db5b2011-02-04 21:33:14 +0100153
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500154 DEBUGP(DMI, "Returned ok\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100155 return ret;
156}
157
158static int ts_want_write(struct e1inp_ts *e1i_ts)
159{
Harald Welteba0db5b2011-02-04 21:33:14 +0100160 /* We never include the DAHDI B-Channel FD into the
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100161 * writeset, since it doesn't support poll() based
Harald Welte62d46032011-02-05 20:25:22 +0100162 * write flow control */
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500163 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
164 fprintf(stderr, "Trying to write TRAU ts\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100165 return 0;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500166 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100167
Harald Welted38f1052011-02-05 19:13:00 +0100168 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100169
170 return 0;
171}
172
173static void timeout_ts1_write(void *data)
174{
175 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
176
177 /* trigger write of ts1, due to tx delay timer */
178 ts_want_write(e1i_ts);
179}
180
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500181static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
182{
183 struct bsc_fd *bfd = cbdata;
Harald Welte44f04da2011-02-13 14:19:26 +0100184 struct e1inp_line *line = bfd->data;
185 unsigned int ts_nr = bfd->priv_nr;
186 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500187 int ret;
188
189 ret = write(bfd->fd, data, len + 2);
Harald Welte44f04da2011-02-13 14:19:26 +0100190 if (ret == -1)
191 handle_dahdi_exception(e1i_ts);
192 else if (ret < 0)
193 LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500194}
195
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100196static int handle_ts1_write(struct bsc_fd *bfd)
197{
198 struct e1inp_line *line = bfd->data;
199 unsigned int ts_nr = bfd->priv_nr;
200 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
201 struct e1inp_sign_link *sign_link;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100202 struct msgb *msg;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100203
204 bfd->when &= ~BSC_FD_WRITE;
205
206 /* get the next msg for this timeslot */
207 msg = e1inp_tx_ts(e1i_ts, &sign_link);
208 if (!msg) {
209 /* no message after tx delay timer */
210 return 0;
211 }
212
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100213 lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei,
214 sign_link->sapi, msg->data, msg->len);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100215 msgb_free(msg);
216
217 /* set tx delay timer for next event */
218 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
219 e1i_ts->sign.tx_timer.data = e1i_ts;
220 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000);
221
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500222 return 0;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100223}
224
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500225
226static int invertbits = 1;
227
228static u_int8_t flip_table[256];
229
230static void init_flip_bits(void)
231{
232 int i,k;
233
234 for (i = 0 ; i < 256 ; i++) {
235 u_int8_t sample = 0 ;
236 for (k = 0; k<8; k++) {
237 if ( i & 1 << k ) sample |= 0x80 >> k;
238 }
239 flip_table[i] = sample;
240 }
241}
242
243static u_int8_t * flip_buf_bits ( u_int8_t * buf , int len)
244{
245 int i;
creslin287cd8b86f2010-03-26 12:57:31 -0500246 u_int8_t * start = buf;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500247
248 for (i = 0 ; i < len; i++) {
249 buf[i] = flip_table[(u_int8_t)buf[i]];
250 }
251
252 return start;
253}
254
creslin287cd8b86f2010-03-26 12:57:31 -0500255#define D_BCHAN_TX_GRAN 160
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100256/* write to a B channel TS */
257static int handle_tsX_write(struct bsc_fd *bfd)
258{
259 struct e1inp_line *line = bfd->data;
260 unsigned int ts_nr = bfd->priv_nr;
261 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500262 u_int8_t tx_buf[D_BCHAN_TX_GRAN];
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100263 struct subch_mux *mx = &e1i_ts->trau.mux;
264 int ret;
265
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500266 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100267
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500268 if (ret != D_BCHAN_TX_GRAN) {
269 fprintf(stderr, "Huh, got ret of %d\n", ret);
270 if (ret < 0)
271 return ret;
272 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100273
274 DEBUGP(DMIB, "BCHAN TX: %s\n",
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500275 hexdump(tx_buf, D_BCHAN_TX_GRAN));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100276
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500277 if (invertbits) {
278 flip_buf_bits(tx_buf, ret);
279 }
280
281 ret = write(bfd->fd, tx_buf, ret);
282 if (ret < D_BCHAN_TX_GRAN)
Harald Welte1dd68c32011-02-05 13:58:46 +0100283 fprintf(stderr, "send returns %d instead of %d\n", ret,
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500284 D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100285
286 return ret;
287}
288
creslin287cd8b86f2010-03-26 12:57:31 -0500289#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100290/* FIXME: read from a B channel TS */
291static int handle_tsX_read(struct bsc_fd *bfd)
292{
293 struct e1inp_line *line = bfd->data;
294 unsigned int ts_nr = bfd->priv_nr;
295 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500296 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100297 int ret;
298
299 if (!msg)
300 return -ENOMEM;
301
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500302 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
303 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
304 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100305 return ret;
306 }
307
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500308 if (invertbits) {
309 flip_buf_bits(msg->data, ret);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100310 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500311
312 msgb_put(msg, ret);
313
314 msg->l2h = msg->data;
315 DEBUGP(DMIB, "BCHAN RX: %s\n",
316 hexdump(msgb_l2(msg), ret));
317 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
318 /* physical layer indicates that data has been sent,
319 * we thus can send some more data */
creslin287cd8b86f2010-03-26 12:57:31 -0500320 ret = handle_tsX_write(bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100321 msgb_free(msg);
322
323 return ret;
324}
325
326/* callback from select.c in case one of the fd's can be read/written */
327static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what)
328{
329 struct e1inp_line *line = bfd->data;
330 unsigned int ts_nr = bfd->priv_nr;
331 unsigned int idx = ts_nr-1;
332 struct e1inp_ts *e1i_ts = &line->ts[idx];
333 int rc = 0;
334
335 switch (e1i_ts->type) {
336 case E1INP_TS_TYPE_SIGN:
Harald Welte00ee4b72011-02-13 15:41:25 +0100337 if (what & BSC_FD_EXCEPT)
338 handle_dahdi_exception(e1i_ts);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100339 if (what & BSC_FD_READ)
340 rc = handle_ts1_read(bfd);
341 if (what & BSC_FD_WRITE)
342 rc = handle_ts1_write(bfd);
343 break;
344 case E1INP_TS_TYPE_TRAU:
Harald Welte00ee4b72011-02-13 15:41:25 +0100345 if (what & BSC_FD_EXCEPT)
346 handle_dahdi_exception(e1i_ts);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100347 if (what & BSC_FD_READ)
348 rc = handle_tsX_read(bfd);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500349 if (what & BSC_FD_WRITE)
350 rc = handle_tsX_write(bfd);
Harald Welteba0db5b2011-02-04 21:33:14 +0100351 /* We never include the DAHDI B-Channel FD into the
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100352 * writeset, since it doesn't support poll() based
Harald Welte62d46032011-02-05 20:25:22 +0100353 * write flow control */
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100354 break;
355 default:
356 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
357 break;
358 }
359
360 return rc;
361}
362
Harald Welte1dd68c32011-02-05 13:58:46 +0100363static int dahdi_e1_line_update(struct e1inp_line *line);
364
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100365struct e1inp_driver dahdi_driver = {
366 .name = "DAHDI",
367 .want_write = ts_want_write,
Harald Welte1dd68c32011-02-05 13:58:46 +0100368 .line_update = &dahdi_e1_line_update,
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100369};
370
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500371void dahdi_set_bufinfo(int fd, int as_sigchan)
372{
373 struct dahdi_bufferinfo bi;
374 int x = 0;
375
376 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
377 fprintf(stderr, "Error getting bufinfo\n");
378 exit(-1);
379 }
380
381 if (as_sigchan) {
382 bi.numbufs = 4;
383 bi.bufsize = 512;
384 } else {
creslin287cd8b86f2010-03-26 12:57:31 -0500385 bi.numbufs = 8;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500386 bi.bufsize = D_BCHAN_TX_GRAN;
creslin287cd8b86f2010-03-26 12:57:31 -0500387 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500388 }
389
390 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
391 fprintf(stderr, "Error setting bufinfo\n");
392 exit(-1);
393 }
394
395 if (!as_sigchan) {
396 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
397 fprintf(stderr, "Error setting bufinfo\n");
398 exit(-1);
399 }
400 }
401
402}
403
Harald Welted38f1052011-02-05 19:13:00 +0100404static int dahdi_e1_setup(struct e1inp_line *line)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100405{
406 int ts, ret;
407
408 /* TS0 is CRC4, don't need any fd for it */
409 for (ts = 1; ts < NUM_E1_TS; ts++) {
410 unsigned int idx = ts-1;
411 char openstr[128];
412 struct e1inp_ts *e1i_ts = &line->ts[idx];
Harald Welted38f1052011-02-05 19:13:00 +0100413 struct bsc_fd *bfd = &e1i_ts->driver.dahdi.fd;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100414
415 bfd->data = line;
416 bfd->priv_nr = ts;
417 bfd->cb = dahdi_fd_cb;
418 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
419
420 switch (e1i_ts->type) {
421 case E1INP_TS_TYPE_NONE:
422 continue;
423 break;
424 case E1INP_TS_TYPE_SIGN:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500425 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
426 if (bfd->fd == -1) {
427 fprintf(stderr, "%s could not open %s %s\n",
428 __func__, openstr, strerror(errno));
429 exit(-1);
430 }
Harald Welte00ee4b72011-02-13 15:41:25 +0100431 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500432 dahdi_set_bufinfo(bfd->fd, 1);
Harald Welte1a00d822011-02-11 18:34:51 +0100433 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100434 break;
435 case E1INP_TS_TYPE_TRAU:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500436 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
437 if (bfd->fd == -1) {
438 fprintf(stderr, "%s could not open %s %s\n",
439 __func__, openstr, strerror(errno));
440 exit(-1);
441 }
442 dahdi_set_bufinfo(bfd->fd, 0);
Harald Welteba0db5b2011-02-04 21:33:14 +0100443 /* We never include the DAHDI B-Channel FD into the
Harald Welte62d46032011-02-05 20:25:22 +0100444 * writeset, since it doesn't support poll() based
445 * write flow control */
Harald Welte00ee4b72011-02-13 15:41:25 +0100446 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100447 break;
448 }
449
450 if (bfd->fd < 0) {
451 fprintf(stderr, "%s could not open %s %s\n",
452 __func__, openstr, strerror(errno));
453 return bfd->fd;
454 }
455
456 ret = bsc_register_fd(bfd);
457 if (ret < 0) {
458 fprintf(stderr, "could not register FD: %s\n",
459 strerror(ret));
460 return ret;
461 }
462 }
463
464 return 0;
465}
466
Harald Welte1dd68c32011-02-05 13:58:46 +0100467static int dahdi_e1_line_update(struct e1inp_line *line)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100468{
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100469 if (line->driver != &dahdi_driver)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100470 return -EINVAL;
471
Harald Welted38f1052011-02-05 19:13:00 +0100472 return dahdi_e1_setup(line);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100473}
474
Harald Welte1dd68c32011-02-05 13:58:46 +0100475int e1inp_dahdi_init(void)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100476{
Harald Welted38f1052011-02-05 19:13:00 +0100477 init_flip_bits();
478
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100479 /* register the driver with the core */
Harald Welte50d369e2011-02-05 15:30:48 +0100480 return e1inp_driver_register(&dahdi_driver);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100481}
Harald Welteca17ef82011-02-05 15:13:27 +0100482
483#endif /* HAVE_DAHDI_USER_H */