blob: 2fc99f58e3cf79722d5b44e436732987b69b69b5 [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;
Harald Welte174a51a2011-02-13 14:26:54 +010072 struct input_signal_data isd;
Harald Welte44f04da2011-02-13 14:19:26 +010073
74 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
75 if (rc < 0)
76 return;
77
78 LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
79 ts->line->num, ts->line->name, ts->num,
80 get_value_string(dahdi_evt_names, evt));
81
Harald Welte174a51a2011-02-13 14:26:54 +010082 isd.line = ts->line;
83
Harald Welte44f04da2011-02-13 14:19:26 +010084 switch (evt) {
85 case DAHDI_EVENT_ALARM:
Harald Welte174a51a2011-02-13 14:26:54 +010086 /* we should notify the code that the line is gone */
87 dispatch_signal(SS_INPUT, S_INP_LINE_ALARM, &isd);
Harald Welte44f04da2011-02-13 14:19:26 +010088 break;
89 case DAHDI_EVENT_NOALARM:
Harald Welte174a51a2011-02-13 14:26:54 +010090 /* alarm has gone, we should re-start the SABM requests */
91 dispatch_signal(SS_INPUT, S_INP_LINE_NOALARM, &isd);
Harald Welte44f04da2011-02-13 14:19:26 +010092 break;
93 }
94}
95
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010096static int handle_ts1_read(struct bsc_fd *bfd)
97{
98 struct e1inp_line *line = bfd->data;
99 unsigned int ts_nr = bfd->priv_nr;
100 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100101 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Welteba0db5b2011-02-04 21:33:14 +0100102 lapd_mph_type prim;
103 unsigned int sapi, tei;
104 int ilen, ret;
Harald Welted38f1052011-02-05 19:13:00 +0100105 uint8_t *idata;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100106
107 if (!msg)
108 return -ENOMEM;
109
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500110 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
Harald Welte44f04da2011-02-13 14:19:26 +0100111 if (ret == -1)
112 handle_dahdi_exception(e1i_ts);
113 else if (ret < 0) {
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100114 perror("read ");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100115 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500116 msgb_put(msg, ret - 2);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100117 if (ret <= 3) {
118 perror("read ");
119 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100120
Harald Welteba0db5b2011-02-04 21:33:14 +0100121 sapi = msg->data[0] >> 2;
122 tei = msg->data[1] >> 1;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500123
Harald Welteba0db5b2011-02-04 21:33:14 +0100124 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100125
Harald Welted38f1052011-02-05 19:13:00 +0100126 idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim);
Harald Welte1458ec62011-02-05 19:50:44 +0100127 if (!idata && prim == 0)
Harald Welted38f1052011-02-05 19:13:00 +0100128 return -EIO;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100129
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100130 msgb_pull(msg, 2);
131
Harald Welteba0db5b2011-02-04 21:33:14 +0100132 DEBUGP(DMI, "prim %08x\n", prim);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100133
Harald Welteba0db5b2011-02-04 21:33:14 +0100134 switch (prim) {
135 case 0:
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100136 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100137 case LAPD_MPH_ACTIVATE_IND:
138 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Harald Weltef338a032011-01-14 15:55:42 +0100139 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100140 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100141 case LAPD_MPH_DEACTIVATE_IND:
142 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Harald Weltef338a032011-01-14 15:55:42 +0100143 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, tei, sapi);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100144 break;
Harald Welteba0db5b2011-02-04 21:33:14 +0100145 case LAPD_DL_DATA_IND:
146 case LAPD_DL_UNITDATA_IND:
Harald Welte1dd68c32011-02-05 13:58:46 +0100147 if (prim == LAPD_DL_DATA_IND)
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500148 msg->l2h = msg->data + 2;
149 else
150 msg->l2h = msg->data + 1;
151 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret));
Harald Welteba0db5b2011-02-04 21:33:14 +0100152 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100153 break;
154 default:
Harald Welteba0db5b2011-02-04 21:33:14 +0100155 printf("ERROR: unknown prim\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100156 break;
157 }
Harald Welteba0db5b2011-02-04 21:33:14 +0100158
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500159 DEBUGP(DMI, "Returned ok\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100160 return ret;
161}
162
163static int ts_want_write(struct e1inp_ts *e1i_ts)
164{
Harald Welteba0db5b2011-02-04 21:33:14 +0100165 /* We never include the DAHDI B-Channel FD into the
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100166 * writeset, since it doesn't support poll() based
Harald Welte62d46032011-02-05 20:25:22 +0100167 * write flow control */
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500168 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
169 fprintf(stderr, "Trying to write TRAU ts\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100170 return 0;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500171 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100172
Harald Welted38f1052011-02-05 19:13:00 +0100173 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100174
175 return 0;
176}
177
178static void timeout_ts1_write(void *data)
179{
180 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
181
182 /* trigger write of ts1, due to tx delay timer */
183 ts_want_write(e1i_ts);
184}
185
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500186static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
187{
188 struct bsc_fd *bfd = cbdata;
Harald Welte44f04da2011-02-13 14:19:26 +0100189 struct e1inp_line *line = bfd->data;
190 unsigned int ts_nr = bfd->priv_nr;
191 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500192 int ret;
193
194 ret = write(bfd->fd, data, len + 2);
Harald Welte44f04da2011-02-13 14:19:26 +0100195 if (ret == -1)
196 handle_dahdi_exception(e1i_ts);
197 else if (ret < 0)
198 LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500199}
200
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100201static int handle_ts1_write(struct bsc_fd *bfd)
202{
203 struct e1inp_line *line = bfd->data;
204 unsigned int ts_nr = bfd->priv_nr;
205 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
206 struct e1inp_sign_link *sign_link;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100207 struct msgb *msg;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100208
209 bfd->when &= ~BSC_FD_WRITE;
210
211 /* get the next msg for this timeslot */
212 msg = e1inp_tx_ts(e1i_ts, &sign_link);
213 if (!msg) {
214 /* no message after tx delay timer */
215 return 0;
216 }
217
Harald Weltea4898a82011-02-12 13:09:12 +0100218 DEBUGP(DMI, "TX: %s\n", hexdump(msg->data, msg->len));
Harald Welte4ee2eaf2011-02-05 20:20:50 +0100219 lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei,
220 sign_link->sapi, msg->data, msg->len);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100221 msgb_free(msg);
222
223 /* set tx delay timer for next event */
224 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
225 e1i_ts->sign.tx_timer.data = e1i_ts;
226 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000);
227
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500228 return 0;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100229}
230
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500231
232static int invertbits = 1;
233
234static u_int8_t flip_table[256];
235
236static void init_flip_bits(void)
237{
238 int i,k;
239
240 for (i = 0 ; i < 256 ; i++) {
241 u_int8_t sample = 0 ;
242 for (k = 0; k<8; k++) {
243 if ( i & 1 << k ) sample |= 0x80 >> k;
244 }
245 flip_table[i] = sample;
246 }
247}
248
249static u_int8_t * flip_buf_bits ( u_int8_t * buf , int len)
250{
251 int i;
creslin287cd8b86f2010-03-26 12:57:31 -0500252 u_int8_t * start = buf;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500253
254 for (i = 0 ; i < len; i++) {
255 buf[i] = flip_table[(u_int8_t)buf[i]];
256 }
257
258 return start;
259}
260
creslin287cd8b86f2010-03-26 12:57:31 -0500261#define D_BCHAN_TX_GRAN 160
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100262/* write to a B channel TS */
263static int handle_tsX_write(struct bsc_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];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500268 u_int8_t tx_buf[D_BCHAN_TX_GRAN];
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100269 struct subch_mux *mx = &e1i_ts->trau.mux;
270 int ret;
271
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500272 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100273
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500274 if (ret != D_BCHAN_TX_GRAN) {
275 fprintf(stderr, "Huh, got ret of %d\n", ret);
276 if (ret < 0)
277 return ret;
278 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100279
280 DEBUGP(DMIB, "BCHAN TX: %s\n",
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500281 hexdump(tx_buf, D_BCHAN_TX_GRAN));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100282
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500283 if (invertbits) {
284 flip_buf_bits(tx_buf, ret);
285 }
286
287 ret = write(bfd->fd, tx_buf, ret);
288 if (ret < D_BCHAN_TX_GRAN)
Harald Welte1dd68c32011-02-05 13:58:46 +0100289 fprintf(stderr, "send returns %d instead of %d\n", ret,
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500290 D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100291
292 return ret;
293}
294
creslin287cd8b86f2010-03-26 12:57:31 -0500295#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100296/* FIXME: read from a B channel TS */
297static int handle_tsX_read(struct bsc_fd *bfd)
298{
299 struct e1inp_line *line = bfd->data;
300 unsigned int ts_nr = bfd->priv_nr;
301 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500302 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100303 int ret;
304
305 if (!msg)
306 return -ENOMEM;
307
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500308 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
309 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
310 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100311 return ret;
312 }
313
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500314 if (invertbits) {
315 flip_buf_bits(msg->data, ret);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100316 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500317
318 msgb_put(msg, ret);
319
320 msg->l2h = msg->data;
321 DEBUGP(DMIB, "BCHAN RX: %s\n",
322 hexdump(msgb_l2(msg), ret));
323 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
324 /* physical layer indicates that data has been sent,
325 * we thus can send some more data */
creslin287cd8b86f2010-03-26 12:57:31 -0500326 ret = handle_tsX_write(bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100327 msgb_free(msg);
328
329 return ret;
330}
331
332/* callback from select.c in case one of the fd's can be read/written */
333static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what)
334{
335 struct e1inp_line *line = bfd->data;
336 unsigned int ts_nr = bfd->priv_nr;
337 unsigned int idx = ts_nr-1;
338 struct e1inp_ts *e1i_ts = &line->ts[idx];
339 int rc = 0;
340
341 switch (e1i_ts->type) {
342 case E1INP_TS_TYPE_SIGN:
Harald Welte00ee4b72011-02-13 15:41:25 +0100343 if (what & BSC_FD_EXCEPT)
344 handle_dahdi_exception(e1i_ts);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100345 if (what & BSC_FD_READ)
346 rc = handle_ts1_read(bfd);
347 if (what & BSC_FD_WRITE)
348 rc = handle_ts1_write(bfd);
349 break;
350 case E1INP_TS_TYPE_TRAU:
Harald Welte00ee4b72011-02-13 15:41:25 +0100351 if (what & BSC_FD_EXCEPT)
352 handle_dahdi_exception(e1i_ts);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100353 if (what & BSC_FD_READ)
354 rc = handle_tsX_read(bfd);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500355 if (what & BSC_FD_WRITE)
356 rc = handle_tsX_write(bfd);
Harald Welteba0db5b2011-02-04 21:33:14 +0100357 /* We never include the DAHDI B-Channel FD into the
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100358 * writeset, since it doesn't support poll() based
Harald Welte62d46032011-02-05 20:25:22 +0100359 * write flow control */
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100360 break;
361 default:
362 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
363 break;
364 }
365
366 return rc;
367}
368
Harald Welte1dd68c32011-02-05 13:58:46 +0100369static int dahdi_e1_line_update(struct e1inp_line *line);
370
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100371struct e1inp_driver dahdi_driver = {
Harald Welteebec58e2011-02-14 16:31:53 +0100372 .name = "dahdi",
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100373 .want_write = ts_want_write,
Harald Welte1dd68c32011-02-05 13:58:46 +0100374 .line_update = &dahdi_e1_line_update,
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100375};
376
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500377void dahdi_set_bufinfo(int fd, int as_sigchan)
378{
379 struct dahdi_bufferinfo bi;
380 int x = 0;
381
382 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
383 fprintf(stderr, "Error getting bufinfo\n");
384 exit(-1);
385 }
386
387 if (as_sigchan) {
388 bi.numbufs = 4;
389 bi.bufsize = 512;
390 } else {
creslin287cd8b86f2010-03-26 12:57:31 -0500391 bi.numbufs = 8;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500392 bi.bufsize = D_BCHAN_TX_GRAN;
creslin287cd8b86f2010-03-26 12:57:31 -0500393 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500394 }
395
396 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
397 fprintf(stderr, "Error setting bufinfo\n");
398 exit(-1);
399 }
400
401 if (!as_sigchan) {
402 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
403 fprintf(stderr, "Error setting bufinfo\n");
404 exit(-1);
405 }
Harald Weltee9252052011-02-14 16:43:35 +0100406 } else {
407 int one = 1;
408 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
409 /* we cannot reliably check for the ioctl return value here
410 * as this command will fail if the slot _already_ was a
411 * signalling slot before :( */
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500412 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500413}
414
Harald Welted38f1052011-02-05 19:13:00 +0100415static int dahdi_e1_setup(struct e1inp_line *line)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100416{
417 int ts, ret;
418
419 /* TS0 is CRC4, don't need any fd for it */
420 for (ts = 1; ts < NUM_E1_TS; ts++) {
421 unsigned int idx = ts-1;
422 char openstr[128];
423 struct e1inp_ts *e1i_ts = &line->ts[idx];
Harald Welted38f1052011-02-05 19:13:00 +0100424 struct bsc_fd *bfd = &e1i_ts->driver.dahdi.fd;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100425
426 bfd->data = line;
427 bfd->priv_nr = ts;
428 bfd->cb = dahdi_fd_cb;
429 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
430
431 switch (e1i_ts->type) {
432 case E1INP_TS_TYPE_NONE:
433 continue;
434 break;
435 case E1INP_TS_TYPE_SIGN:
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 }
Harald Welte00ee4b72011-02-13 15:41:25 +0100442 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500443 dahdi_set_bufinfo(bfd->fd, 1);
Harald Welte1a00d822011-02-11 18:34:51 +0100444 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100445 break;
446 case E1INP_TS_TYPE_TRAU:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500447 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
448 if (bfd->fd == -1) {
449 fprintf(stderr, "%s could not open %s %s\n",
450 __func__, openstr, strerror(errno));
451 exit(-1);
452 }
453 dahdi_set_bufinfo(bfd->fd, 0);
Harald Welteba0db5b2011-02-04 21:33:14 +0100454 /* We never include the DAHDI B-Channel FD into the
Harald Welte62d46032011-02-05 20:25:22 +0100455 * writeset, since it doesn't support poll() based
456 * write flow control */
Harald Welte00ee4b72011-02-13 15:41:25 +0100457 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100458 break;
459 }
460
461 if (bfd->fd < 0) {
462 fprintf(stderr, "%s could not open %s %s\n",
463 __func__, openstr, strerror(errno));
464 return bfd->fd;
465 }
466
467 ret = bsc_register_fd(bfd);
468 if (ret < 0) {
469 fprintf(stderr, "could not register FD: %s\n",
470 strerror(ret));
471 return ret;
472 }
473 }
474
475 return 0;
476}
477
Harald Welte1dd68c32011-02-05 13:58:46 +0100478static int dahdi_e1_line_update(struct e1inp_line *line)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100479{
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100480 if (line->driver != &dahdi_driver)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100481 return -EINVAL;
482
Harald Welted38f1052011-02-05 19:13:00 +0100483 return dahdi_e1_setup(line);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100484}
485
Harald Welte1dd68c32011-02-05 13:58:46 +0100486int e1inp_dahdi_init(void)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100487{
Harald Welted38f1052011-02-05 19:13:00 +0100488 init_flip_bits();
489
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100490 /* register the driver with the core */
Harald Welte50d369e2011-02-05 15:30:48 +0100491 return e1inp_driver_register(&dahdi_driver);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100492}
Harald Welteca17ef82011-02-05 15:13:27 +0100493
494#endif /* HAVE_DAHDI_USER_H */