blob: 9b899ff7540928786c9211fb81e76c59737860c4 [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
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 * (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
25#include <stdio.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <time.h>
31#include <sys/fcntl.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36#include <mISDNif.h>
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -050037#include <dahdi/user.h>
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010038
39//#define AF_COMPATIBILITY_FUNC
40//#include <compat_af_isdn.h>
41#ifndef AF_ISDN
42#define AF_ISDN 34
43#define PF_ISDN AF_ISDN
44#endif
45
46#include <openbsc/select.h>
47#include <openbsc/msgb.h>
48#include <openbsc/debug.h>
49#include <openbsc/gsm_data.h>
50#include <openbsc/abis_nm.h>
51#include <openbsc/abis_rsl.h>
52#include <openbsc/subchan_demux.h>
53#include <openbsc/e1_input.h>
54#include <openbsc/talloc.h>
55
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +010056#include "lapd.h"
57
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010058#define TS1_ALLOC_SIZE 300
59
60struct prim_name {
61 unsigned int prim;
62 const char *name;
63};
64
65const struct prim_name prim_names[] = {
66 { PH_CONTROL_IND, "PH_CONTROL_IND" },
67 { PH_DATA_IND, "PH_DATA_IND" },
68 { PH_DATA_CNF, "PH_DATA_CNF" },
69 { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" },
70 { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" },
71 { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" },
72 { DL_RELEASE_IND, "DL_RELEASE_IND" },
73 { DL_RELEASE_CNF, "DL_RELEASE_CNF" },
74 { DL_DATA_IND, "DL_DATA_IND" },
75 { DL_UNITDATA_IND, "DL_UNITDATA_IND" },
76 { DL_INFORMATION_IND, "DL_INFORMATION_IND" },
77 { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" },
78 { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" },
79};
80
81const char *get_prim_name(unsigned int prim)
82{
83 int i;
84
85 for (i = 0; i < ARRAY_SIZE(prim_names); i++) {
86 if (prim_names[i].prim == prim)
87 return prim_names[i].name;
88 }
89
90 return "UNKNOWN";
91}
92
93static int handle_ts1_read(struct bsc_fd *bfd)
94{
95 struct e1inp_line *line = bfd->data;
96 unsigned int ts_nr = bfd->priv_nr;
97 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
98 struct e1inp_sign_link *link;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010099 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100100 struct sockaddr_mISDN l2addr;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100101 struct mISDNhead ah;
102 struct mISDNhead *hh = &ah;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100103 int ret;
104
105 if (!msg)
106 return -ENOMEM;
107
108 hh = (struct mISDNhead *) msg->data;
109
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500110 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100111 if (ret < 0) {
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100112 perror("read ");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100113 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500114 msgb_put(msg, ret - 2);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100115 if (ret <= 3) {
116 perror("read ");
117 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100118
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500119 l2addr.sapi = msg->data[0] >> 2;
120 l2addr.tei = msg->data[1] >> 1;
121
122 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)",
123 ret, l2addr.sapi, l2addr.tei);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100124
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100125 int ilen;
126 lapd_mph_type prim;
127 uint8_t *idata = lapd_receive(msg->data, msg->len, &ilen, &prim, bfd);
128
129 switch (prim) {
130 case 0: break;
131 case LAPD_MPH_ACTIVATE_IND: hh->prim = MPH_ACTIVATE_IND; break;
132 case LAPD_MPH_DEACTIVATE_IND: hh->prim = MPH_DEACTIVATE_IND; break;
133 case LAPD_DL_DATA_IND: hh->prim = DL_DATA_IND; break;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500134 case LAPD_DL_UNITDATA_IND: hh->prim = DL_UNITDATA_IND; break;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100135 default: printf("ERROR: unknown prim\n");
136 };
137
138 int pass_on = (prim != 0);
139
140 if (!pass_on) {
141 return 0;
142 };
143
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500144 //l2addr.sapi = msg->data[0] >> 2;
145 //l2addr.tei = msg->data[1] >> 1;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100146 msgb_pull(msg, 2);
147
148#if 0
149 switch (hh->prim) {
150 case DL_INFORMATION_IND:
151 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
152 struct sockaddr_mISDN *sa = NULL;
153 char *lstr = "UNKN";
154
155 switch (l2addr.tei) {
156 case TEI_OML:
157 sa = &e1h->omladdr;
158 lstr = "OML";
159 break;
160 case TEI_RSL:
161 sa = &e1h->l2addr;
162 lstr = "RSL";
163 break;
164 default:
165 break;
166 }
167 if (sa) {
168 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
169 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
170 memcpy(sa, &l2addr, sizeof(l2addr));
171 }
172 break;
173 case DL_ESTABLISH_IND:
174 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
175 break;
176 case DL_ESTABLISH_CNF:
177 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
178 break;
179 case DL_RELEASE_IND:
180 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
181 break;
182#if 0
183 case MPH_ACTIVATE_IND:
184 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
185 printf("tei %d, sapi %d\n", l2addr.tei, l2addr.sapi);
186 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
187 e1h->cb(EVT_E1_OML_UP, e1h->bts);
188 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
189 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
190 break;
191 case MPH_DEACTIVATE_IND:
192 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
193 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
194 e1h->cb(EVT_E1_OML_DN, e1h->bts);
195 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
196 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
197 break;
198#endif
199 case DL_DATA_IND:
200 DEBUGP(DMI, "got DL_DATA_IND\n");
201
202 ret = msg->len;
203 msg->l2h = msg->data + 2;// + MISDN_HEADER_LEN;
204
205#if 0
206 if (debug_mask & DMI) {
207 fprintf(stdout, "RX: ");
208 hexdump(msgb_l2(msg), ret - (msg->l2h - msg->data));// - MISDN_HEADER_LEN);
209 }
210#endif
211 switch (l2addr.tei) {
212 case TEI_OML:
213 ret = abis_nm_rcvmsg(msg);
214 break;
215 case TEI_RSL:
216 ret = abis_rsl_rcvmsg(msg);
217 break;
218 default:
219 fprintf(stderr, "DATA_IND for unknown TEI\n");
220 break;
221 }
222 break;
223 default:
224 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
225 break;
226 }
227#endif
228
229
230
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500231 DEBUGP(DMI, "hh->prim %08x\n", hh->prim);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100232#if 1
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100233 switch (hh->prim) {
234 case DL_INFORMATION_IND:
235 /* mISDN tells us which channel number is allocated for this
236 * tuple of (SAPI, TEI). */
237 DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n",
238 l2addr.channel, l2addr.sapi, l2addr.tei);
239 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
240 if (!link) {
241 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
242 msgb_free(msg);
243 return -EINVAL;
244 }
245 /* save the channel number in the driver private struct */
246 link->driver.misdn.channel = l2addr.channel;
247 break;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500248 case MPH_ACTIVATE_IND:
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100249 case DL_ESTABLISH_IND:
250 DEBUGP(DMI, "DL_ESTABLISH_IND: channel(%d) sapi(%d) tei(%d)\n",
251 l2addr.channel, l2addr.sapi, l2addr.tei);
252 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi);
253 break;
254 case DL_RELEASE_IND:
255 DEBUGP(DMI, "DL_RELEASE_IND: channel(%d) sapi(%d) tei(%d)\n",
256 l2addr.channel, l2addr.sapi, l2addr.tei);
257 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi);
258 break;
259 case DL_DATA_IND:
260 case DL_UNITDATA_IND:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500261 if (hh->prim == DL_DATA_IND)
262 msg->l2h = msg->data + 2;
263 else
264 msg->l2h = msg->data + 1;
265 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100266 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
267 break;
268 case PH_ACTIVATE_IND:
269 DEBUGP(DMI, "PH_ACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
270 l2addr.channel, l2addr.sapi, l2addr.tei);
271 break;
272 case PH_DEACTIVATE_IND:
273 DEBUGP(DMI, "PH_DEACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
274 l2addr.channel, l2addr.sapi, l2addr.tei);
275 break;
276 default:
277 break;
278 }
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100279#endif
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500280 DEBUGP(DMI, "Returned ok\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100281 return ret;
282}
283
284static int ts_want_write(struct e1inp_ts *e1i_ts)
285{
286 /* We never include the mISDN B-Channel FD into the
287 * writeset, since it doesn't support poll() based
288 * write flow control */
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500289 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
290 fprintf(stderr, "Trying to write TRAU ts\n");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100291 return 0;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500292 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100293
294 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
295
296 return 0;
297}
298
299static void timeout_ts1_write(void *data)
300{
301 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
302
303 /* trigger write of ts1, due to tx delay timer */
304 ts_want_write(e1i_ts);
305}
306
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500307static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
308{
309 struct bsc_fd *bfd = cbdata;
310 int ret;
311
312 ret = write(bfd->fd, data, len + 2);
313
314 if (ret < 0)
315 fprintf(stderr, "%s write failed %d\n", __func__, ret);
316}
317
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100318static int handle_ts1_write(struct bsc_fd *bfd)
319{
320 struct e1inp_line *line = bfd->data;
321 unsigned int ts_nr = bfd->priv_nr;
322 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
323 struct e1inp_sign_link *sign_link;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100324 struct msgb *msg;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100325 //u_int8_t *l2_data;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500326 //int ret;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100327 //int no_oml = 0;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100328
329 bfd->when &= ~BSC_FD_WRITE;
330
331 /* get the next msg for this timeslot */
332 msg = e1inp_tx_ts(e1i_ts, &sign_link);
333 if (!msg) {
334 /* no message after tx delay timer */
335 return 0;
336 }
337
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100338 lapd_transmit(sign_link->tei, msg->data, msg->len, bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100339
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500340 //ret = write(bfd->fd, msg->data, msg->len + 2);
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100341
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500342#if 0
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100343 if (ret < 0)
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100344 fprintf(stderr, "%s write failed %d\n", __func__, ret);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500345#endif
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100346 msgb_free(msg);
347
348 /* set tx delay timer for next event */
349 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
350 e1i_ts->sign.tx_timer.data = e1i_ts;
351 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000);
352
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500353 return 0;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100354}
355
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500356
357static int invertbits = 1;
358
359static u_int8_t flip_table[256];
360
361static void init_flip_bits(void)
362{
363 int i,k;
364
365 for (i = 0 ; i < 256 ; i++) {
366 u_int8_t sample = 0 ;
367 for (k = 0; k<8; k++) {
368 if ( i & 1 << k ) sample |= 0x80 >> k;
369 }
370 flip_table[i] = sample;
371 }
372}
373
374static u_int8_t * flip_buf_bits ( u_int8_t * buf , int len)
375{
376 int i;
377 char * start = buf;
378
379 for (i = 0 ; i < len; i++) {
380 buf[i] = flip_table[(u_int8_t)buf[i]];
381 }
382
383 return start;
384}
385
386#define D_BCHAN_TX_GRAN 1024
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100387/* write to a B channel TS */
388static int handle_tsX_write(struct bsc_fd *bfd)
389{
390 struct e1inp_line *line = bfd->data;
391 unsigned int ts_nr = bfd->priv_nr;
392 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500393 u_int8_t tx_buf[D_BCHAN_TX_GRAN];
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100394 struct subch_mux *mx = &e1i_ts->trau.mux;
395 int ret;
396
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500397 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100398
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500399 if (ret != D_BCHAN_TX_GRAN) {
400 fprintf(stderr, "Huh, got ret of %d\n", ret);
401 if (ret < 0)
402 return ret;
403 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100404
405 DEBUGP(DMIB, "BCHAN TX: %s\n",
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500406 hexdump(tx_buf, D_BCHAN_TX_GRAN));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100407
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500408 if (invertbits) {
409 flip_buf_bits(tx_buf, ret);
410 }
411
412 ret = write(bfd->fd, tx_buf, ret);
413 if (ret < D_BCHAN_TX_GRAN)
414 fprintf(stderr, "send returns %d instead of %lu\n", ret,
415 D_BCHAN_TX_GRAN);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100416
417 return ret;
418}
419
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500420#define D_TSX_ALLOC_SIZE 1024
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100421/* FIXME: read from a B channel TS */
422static int handle_tsX_read(struct bsc_fd *bfd)
423{
424 struct e1inp_line *line = bfd->data;
425 unsigned int ts_nr = bfd->priv_nr;
426 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500427 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100428 int ret;
429
430 if (!msg)
431 return -ENOMEM;
432
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500433 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
434 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
435 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100436 return ret;
437 }
438
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500439 if (invertbits) {
440 flip_buf_bits(msg->data, ret);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100441 }
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500442
443 msgb_put(msg, ret);
444
445 msg->l2h = msg->data;
446 DEBUGP(DMIB, "BCHAN RX: %s\n",
447 hexdump(msgb_l2(msg), ret));
448 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
449 /* physical layer indicates that data has been sent,
450 * we thus can send some more data */
451 //ret = handle_tsX_write(bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100452 msgb_free(msg);
453
454 return ret;
455}
456
457/* callback from select.c in case one of the fd's can be read/written */
458static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what)
459{
460 struct e1inp_line *line = bfd->data;
461 unsigned int ts_nr = bfd->priv_nr;
462 unsigned int idx = ts_nr-1;
463 struct e1inp_ts *e1i_ts = &line->ts[idx];
464 int rc = 0;
465
466 switch (e1i_ts->type) {
467 case E1INP_TS_TYPE_SIGN:
468 if (what & BSC_FD_READ)
469 rc = handle_ts1_read(bfd);
470 if (what & BSC_FD_WRITE)
471 rc = handle_ts1_write(bfd);
472 break;
473 case E1INP_TS_TYPE_TRAU:
474 if (what & BSC_FD_READ)
475 rc = handle_tsX_read(bfd);
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500476 if (what & BSC_FD_WRITE)
477 rc = handle_tsX_write(bfd);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100478 /* We never include the mISDN B-Channel FD into the
479 * writeset, since it doesn't support poll() based
480 * write flow control */
481 break;
482 default:
483 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
484 break;
485 }
486
487 return rc;
488}
489
490#if 0
491static int activate_bchan(struct e1inp_line *line, int ts, int act)
492{
493 struct mISDNhead hh;
494 int ret;
495 unsigned int idx = ts-1;
496 struct e1inp_ts *e1i_ts = &line->ts[idx];
497 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
498
499 fprintf(stdout, "activate bchan\n");
500 if (act)
501 hh.prim = PH_ACTIVATE_REQ;
502 else
503 hh.prim = PH_DEACTIVATE_REQ;
504
505 hh.id = MISDN_ID_ANY;
506 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
507 if (ret < 0) {
508 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
509 strerror(errno));
510 }
511
512 return ret;
513}
514#endif
515
516struct e1inp_driver dahdi_driver = {
517 .name = "DAHDI",
518 .want_write = ts_want_write,
519};
520
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500521void dahdi_set_bufinfo(int fd, int as_sigchan)
522{
523 struct dahdi_bufferinfo bi;
524 int x = 0;
525
526 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
527 fprintf(stderr, "Error getting bufinfo\n");
528 exit(-1);
529 }
530
531 if (as_sigchan) {
532 bi.numbufs = 4;
533 bi.bufsize = 512;
534 } else {
535 bi.numbufs = 4;
536 bi.bufsize = D_BCHAN_TX_GRAN;
537 //bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
538 }
539
540 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
541 fprintf(stderr, "Error setting bufinfo\n");
542 exit(-1);
543 }
544
545 if (!as_sigchan) {
546 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
547 fprintf(stderr, "Error setting bufinfo\n");
548 exit(-1);
549 }
550 }
551
552}
553
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100554static int mi_e1_setup(struct e1inp_line *line, int release_l2)
555{
556 int ts, ret;
557
558 /* TS0 is CRC4, don't need any fd for it */
559 for (ts = 1; ts < NUM_E1_TS; ts++) {
560 unsigned int idx = ts-1;
561 char openstr[128];
562 struct e1inp_ts *e1i_ts = &line->ts[idx];
563 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100564
565 bfd->data = line;
566 bfd->priv_nr = ts;
567 bfd->cb = dahdi_fd_cb;
568 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
569
570 switch (e1i_ts->type) {
571 case E1INP_TS_TYPE_NONE:
572 continue;
573 break;
574 case E1INP_TS_TYPE_SIGN:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500575 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
576 if (bfd->fd == -1) {
577 fprintf(stderr, "%s could not open %s %s\n",
578 __func__, openstr, strerror(errno));
579 exit(-1);
580 }
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100581 bfd->when = BSC_FD_READ;
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500582 dahdi_set_bufinfo(bfd->fd, 1);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100583 break;
584 case E1INP_TS_TYPE_TRAU:
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500585 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
586 if (bfd->fd == -1) {
587 fprintf(stderr, "%s could not open %s %s\n",
588 __func__, openstr, strerror(errno));
589 exit(-1);
590 }
591 dahdi_set_bufinfo(bfd->fd, 0);
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100592 /* We never include the mISDN B-Channel FD into the
593 * writeset, since it doesn't support poll() based
594 * write flow control */
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500595 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100596 break;
597 }
598
599 if (bfd->fd < 0) {
600 fprintf(stderr, "%s could not open %s %s\n",
601 __func__, openstr, strerror(errno));
602 return bfd->fd;
603 }
604
605 ret = bsc_register_fd(bfd);
606 if (ret < 0) {
607 fprintf(stderr, "could not register FD: %s\n",
608 strerror(ret));
609 return ret;
610 }
611 }
612
613 return 0;
614}
615
616int mi_e1_line_update(struct e1inp_line *line)
617{
618 struct mISDN_devinfo devinfo;
Matthew Fredricksonb1cb8eb2010-02-17 19:25:39 +0100619 //int sk, ret, cnt;
620 int ret;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100621
622 if (!line->driver) {
623 /* this must be the first update */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100624 line->driver = &dahdi_driver;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100625 } else {
626 /* this is a subsequent update */
627 /* FIXME: first close all sockets */
628 fprintf(stderr, "incremental line updates not supported yet\n");
629 return 0;
630 }
631
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100632 if (line->driver != &dahdi_driver)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100633 return -EINVAL;
634
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100635#if 0
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100636 /* open the ISDN card device */
637 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
638 if (sk < 0) {
639 fprintf(stderr, "%s could not open socket %s\n",
640 __func__, strerror(errno));
641 return sk;
642 }
643
644 ret = ioctl(sk, IMGETCOUNT, &cnt);
645 if (ret) {
646 fprintf(stderr, "%s error getting interf count: %s\n",
647 __func__, strerror(errno));
648 close(sk);
649 return -ENODEV;
650 }
651 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
652 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
653#if 1
654 devinfo.id = line->num;
655 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
656 if (ret < 0) {
657 fprintf(stdout, "error getting info for device %d: %s\n",
658 line->num, strerror(errno));
659 return -ENODEV;
660 }
661 fprintf(stdout, " id: %d\n", devinfo.id);
662 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
663 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
664 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
665 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
666 fprintf(stdout, " name: %s\n", devinfo.name);
667#endif
668
669 if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) {
670 fprintf(stderr, "error: card is not of type E1 (NT-mode)\n");
671 return -EINVAL;
672 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100673#endif
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500674 init_flip_bits();
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100675
676 ret = mi_e1_setup(line, 1);
677 if (ret)
678 return ret;
679
Matthew Fredricksoncc2bc352010-03-15 12:22:37 -0500680 lapd_transmit_cb = dahdi_write_msg;
681
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100682 return 0;
683}
684
685static __attribute__((constructor)) void on_dso_load_sms(void)
686{
687 /* register the driver with the core */
688 e1inp_driver_register(&dahdi_driver);
689}