blob: db72783058d0eb4a37e491b9ecd69aea78ac1fa0 [file] [log] [blame]
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +01001/* OpenBSC Abis input driver for mISDNuser */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <mISDNif.h>
36
37//#define AF_COMPATIBILITY_FUNC
38//#include <compat_af_isdn.h>
39#ifndef AF_ISDN
40#define AF_ISDN 34
41#define PF_ISDN AF_ISDN
42#endif
43
44#include <openbsc/select.h>
45#include <openbsc/msgb.h>
46#include <openbsc/debug.h>
47#include <openbsc/gsm_data.h>
48#include <openbsc/abis_nm.h>
49#include <openbsc/abis_rsl.h>
50#include <openbsc/subchan_demux.h>
51#include <openbsc/e1_input.h>
52#include <openbsc/talloc.h>
53
54#define TS1_ALLOC_SIZE 300
55
56struct prim_name {
57 unsigned int prim;
58 const char *name;
59};
60
61const struct prim_name prim_names[] = {
62 { PH_CONTROL_IND, "PH_CONTROL_IND" },
63 { PH_DATA_IND, "PH_DATA_IND" },
64 { PH_DATA_CNF, "PH_DATA_CNF" },
65 { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" },
66 { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" },
67 { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" },
68 { DL_RELEASE_IND, "DL_RELEASE_IND" },
69 { DL_RELEASE_CNF, "DL_RELEASE_CNF" },
70 { DL_DATA_IND, "DL_DATA_IND" },
71 { DL_UNITDATA_IND, "DL_UNITDATA_IND" },
72 { DL_INFORMATION_IND, "DL_INFORMATION_IND" },
73 { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" },
74 { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" },
75};
76
77const char *get_prim_name(unsigned int prim)
78{
79 int i;
80
81 for (i = 0; i < ARRAY_SIZE(prim_names); i++) {
82 if (prim_names[i].prim == prim)
83 return prim_names[i].name;
84 }
85
86 return "UNKNOWN";
87}
88
89static int handle_ts1_read(struct bsc_fd *bfd)
90{
91 struct e1inp_line *line = bfd->data;
92 unsigned int ts_nr = bfd->priv_nr;
93 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
94 struct e1inp_sign_link *link;
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +010095 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +010096 struct sockaddr_mISDN l2addr;
97 struct mISDNhead *hh;
98 socklen_t alen;
99 int ret;
100
101 if (!msg)
102 return -ENOMEM;
103
104 hh = (struct mISDNhead *) msg->data;
105
106 alen = sizeof(l2addr);
107 ret = recvfrom(bfd->fd, msg->data, 300, 0,
108 (struct sockaddr *) &l2addr, &alen);
109 if (ret < 0) {
110 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
111 return ret;
112 }
113
114 if (alen != sizeof(l2addr)) {
115 fprintf(stderr, "%s error len\n", __func__);
116 return -EINVAL;
117 }
118
119 msgb_put(msg, ret);
120
121 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
122 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
123
124 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n",
125 ret, hh->prim, hh->id, get_prim_name(hh->prim));
126
127 switch (hh->prim) {
128 case DL_INFORMATION_IND:
129 /* mISDN tells us which channel number is allocated for this
130 * tuple of (SAPI, TEI). */
131 DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n",
132 l2addr.channel, l2addr.sapi, l2addr.tei);
133 link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi);
134 if (!link) {
135 DEBUGPC(DMI, "mISDN message for unknown sign_link\n");
136 msgb_free(msg);
137 return -EINVAL;
138 }
139 /* save the channel number in the driver private struct */
140 link->driver.misdn.channel = l2addr.channel;
141 break;
142 case DL_ESTABLISH_IND:
143 DEBUGP(DMI, "DL_ESTABLISH_IND: channel(%d) sapi(%d) tei(%d)\n",
144 l2addr.channel, l2addr.sapi, l2addr.tei);
145 ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi);
146 break;
147 case DL_RELEASE_IND:
148 DEBUGP(DMI, "DL_RELEASE_IND: channel(%d) sapi(%d) tei(%d)\n",
149 l2addr.channel, l2addr.sapi, l2addr.tei);
150 ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi);
151 break;
152 case DL_DATA_IND:
153 case DL_UNITDATA_IND:
154 msg->l2h = msg->data + MISDN_HEADER_LEN;
155 DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
156 ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
157 break;
158 case PH_ACTIVATE_IND:
159 DEBUGP(DMI, "PH_ACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
160 l2addr.channel, l2addr.sapi, l2addr.tei);
161 break;
162 case PH_DEACTIVATE_IND:
163 DEBUGP(DMI, "PH_DEACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n",
164 l2addr.channel, l2addr.sapi, l2addr.tei);
165 break;
166 default:
167 break;
168 }
169 return ret;
170}
171
172static int ts_want_write(struct e1inp_ts *e1i_ts)
173{
174 /* We never include the mISDN B-Channel FD into the
175 * writeset, since it doesn't support poll() based
176 * write flow control */
177 if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
178 return 0;
179
180 e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
181
182 return 0;
183}
184
185static void timeout_ts1_write(void *data)
186{
187 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
188
189 /* trigger write of ts1, due to tx delay timer */
190 ts_want_write(e1i_ts);
191}
192
193static int handle_ts1_write(struct bsc_fd *bfd)
194{
195 struct e1inp_line *line = bfd->data;
196 unsigned int ts_nr = bfd->priv_nr;
197 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
198 struct e1inp_sign_link *sign_link;
199 struct sockaddr_mISDN sa;
200 struct msgb *msg;
201 struct mISDNhead *hh;
202 u_int8_t *l2_data;
203 int ret;
204
205 bfd->when &= ~BSC_FD_WRITE;
206
207 /* get the next msg for this timeslot */
208 msg = e1inp_tx_ts(e1i_ts, &sign_link);
209 if (!msg) {
210 /* no message after tx delay timer */
211 return 0;
212 }
213
214 l2_data = msg->data;
215
216 /* prepend the mISDNhead */
217 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
218 hh->prim = DL_DATA_REQ;
219
220 DEBUGP(DMI, "TX TEI(%d) SAPI(%d): %s\n", sign_link->tei,
221 sign_link->sapi, hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
222
223 /* construct the sockaddr */
224 sa.family = AF_ISDN;
225 sa.sapi = sign_link->sapi;
226 sa.dev = sign_link->tei;
227 sa.channel = sign_link->driver.misdn.channel;
228
229 ret = sendto(bfd->fd, msg->data, msg->len, 0,
230 (struct sockaddr *)&sa, sizeof(sa));
231 if (ret < 0)
232 fprintf(stderr, "%s sendto failed %d\n", __func__, ret);
233 msgb_free(msg);
234
235 /* set tx delay timer for next event */
236 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
237 e1i_ts->sign.tx_timer.data = e1i_ts;
238 bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000);
239
240 return ret;
241}
242
243#define BCHAN_TX_GRAN 160
244/* write to a B channel TS */
245static int handle_tsX_write(struct bsc_fd *bfd)
246{
247 struct e1inp_line *line = bfd->data;
248 unsigned int ts_nr = bfd->priv_nr;
249 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
250 struct mISDNhead *hh;
251 u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)];
252 struct subch_mux *mx = &e1i_ts->trau.mux;
253 int ret;
254
255 hh = (struct mISDNhead *) tx_buf;
256 hh->prim = PH_DATA_REQ;
257
258 subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
259
260 DEBUGP(DMIB, "BCHAN TX: %s\n",
261 hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
262
263 ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
264 if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
265 DEBUGP(DMIB, "send returns %d instead of %lu\n", ret,
266 sizeof(*hh) + BCHAN_TX_GRAN);
267
268 return ret;
269}
270
271#define TSX_ALLOC_SIZE 4096
272/* FIXME: read from a B channel TS */
273static int handle_tsX_read(struct bsc_fd *bfd)
274{
275 struct e1inp_line *line = bfd->data;
276 unsigned int ts_nr = bfd->priv_nr;
277 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
278 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE, "DAHDI TSx");
279 struct mISDNhead *hh;
280 int ret;
281
282 if (!msg)
283 return -ENOMEM;
284
285 hh = (struct mISDNhead *) msg->data;
286
287 ret = read(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
288 if (ret < 0) {
289 fprintf(stderr, "read error %s\n", strerror(errno));
290 return ret;
291 }
292
293 msgb_put(msg, ret - 2);
294
295 if (hh->prim != PH_CONTROL_IND)
296 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n",
297 ret, hh->prim, hh->id, get_prim_name(hh->prim));
298
299 switch (hh->prim) {
300 case PH_DATA_IND:
301 msg->l2h = msg->data + MISDN_HEADER_LEN;
302 DEBUGP(DMIB, "BCHAN RX: %s\n",
303 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
304 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
305 break;
306 case PH_ACTIVATE_IND:
307 case PH_DATA_CNF:
308 /* physical layer indicates that data has been sent,
309 * we thus can send some more data */
310 ret = handle_tsX_write(bfd);
311 default:
312 break;
313 }
314 /* FIXME: why do we free signalling msgs in the caller, and trau not? */
315 msgb_free(msg);
316
317 return ret;
318}
319
320/* callback from select.c in case one of the fd's can be read/written */
321static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what)
322{
323 struct e1inp_line *line = bfd->data;
324 unsigned int ts_nr = bfd->priv_nr;
325 unsigned int idx = ts_nr-1;
326 struct e1inp_ts *e1i_ts = &line->ts[idx];
327 int rc = 0;
328
329 switch (e1i_ts->type) {
330 case E1INP_TS_TYPE_SIGN:
331 if (what & BSC_FD_READ)
332 rc = handle_ts1_read(bfd);
333 if (what & BSC_FD_WRITE)
334 rc = handle_ts1_write(bfd);
335 break;
336 case E1INP_TS_TYPE_TRAU:
337 if (what & BSC_FD_READ)
338 rc = handle_tsX_read(bfd);
339 /* We never include the mISDN B-Channel FD into the
340 * writeset, since it doesn't support poll() based
341 * write flow control */
342 break;
343 default:
344 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
345 break;
346 }
347
348 return rc;
349}
350
351#if 0
352static int activate_bchan(struct e1inp_line *line, int ts, int act)
353{
354 struct mISDNhead hh;
355 int ret;
356 unsigned int idx = ts-1;
357 struct e1inp_ts *e1i_ts = &line->ts[idx];
358 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
359
360 fprintf(stdout, "activate bchan\n");
361 if (act)
362 hh.prim = PH_ACTIVATE_REQ;
363 else
364 hh.prim = PH_DEACTIVATE_REQ;
365
366 hh.id = MISDN_ID_ANY;
367 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
368 if (ret < 0) {
369 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
370 strerror(errno));
371 }
372
373 return ret;
374}
375#endif
376
377struct e1inp_driver dahdi_driver = {
378 .name = "DAHDI",
379 .want_write = ts_want_write,
380};
381
382static int mi_e1_setup(struct e1inp_line *line, int release_l2)
383{
384 int ts, ret;
385
386 /* TS0 is CRC4, don't need any fd for it */
387 for (ts = 1; ts < NUM_E1_TS; ts++) {
388 unsigned int idx = ts-1;
389 char openstr[128];
390 struct e1inp_ts *e1i_ts = &line->ts[idx];
391 struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100392
393 bfd->data = line;
394 bfd->priv_nr = ts;
395 bfd->cb = dahdi_fd_cb;
396 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
397
398 switch (e1i_ts->type) {
399 case E1INP_TS_TYPE_NONE:
400 continue;
401 break;
402 case E1INP_TS_TYPE_SIGN:
403 bfd->fd = open(openstr, O_RDWR);
404 bfd->when = BSC_FD_READ;
405 break;
406 case E1INP_TS_TYPE_TRAU:
407 bfd->fd = open(openstr, O_RDWR);
408 /* We never include the mISDN B-Channel FD into the
409 * writeset, since it doesn't support poll() based
410 * write flow control */
411 bfd->when = BSC_FD_READ;
412 break;
413 }
414
415 if (bfd->fd < 0) {
416 fprintf(stderr, "%s could not open %s %s\n",
417 __func__, openstr, strerror(errno));
418 return bfd->fd;
419 }
420
421 ret = bsc_register_fd(bfd);
422 if (ret < 0) {
423 fprintf(stderr, "could not register FD: %s\n",
424 strerror(ret));
425 return ret;
426 }
427 }
428
429 return 0;
430}
431
432int mi_e1_line_update(struct e1inp_line *line)
433{
434 struct mISDN_devinfo devinfo;
435 int sk, ret, cnt;
436
437 if (!line->driver) {
438 /* this must be the first update */
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100439 line->driver = &dahdi_driver;
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100440 } else {
441 /* this is a subsequent update */
442 /* FIXME: first close all sockets */
443 fprintf(stderr, "incremental line updates not supported yet\n");
444 return 0;
445 }
446
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100447 if (line->driver != &dahdi_driver)
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100448 return -EINVAL;
449
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100450#if 0
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100451 /* open the ISDN card device */
452 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
453 if (sk < 0) {
454 fprintf(stderr, "%s could not open socket %s\n",
455 __func__, strerror(errno));
456 return sk;
457 }
458
459 ret = ioctl(sk, IMGETCOUNT, &cnt);
460 if (ret) {
461 fprintf(stderr, "%s error getting interf count: %s\n",
462 __func__, strerror(errno));
463 close(sk);
464 return -ENODEV;
465 }
466 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
467 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
468#if 1
469 devinfo.id = line->num;
470 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
471 if (ret < 0) {
472 fprintf(stdout, "error getting info for device %d: %s\n",
473 line->num, strerror(errno));
474 return -ENODEV;
475 }
476 fprintf(stdout, " id: %d\n", devinfo.id);
477 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
478 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
479 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
480 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
481 fprintf(stdout, " name: %s\n", devinfo.name);
482#endif
483
484 if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) {
485 fprintf(stderr, "error: card is not of type E1 (NT-mode)\n");
486 return -EINVAL;
487 }
Matthew Fredricksonbc6649e2010-02-16 22:01:36 +0100488#endif
Matthew Fredricksonb5ddc182010-02-16 21:55:50 +0100489
490 ret = mi_e1_setup(line, 1);
491 if (ret)
492 return ret;
493
494 return 0;
495}
496
497static __attribute__((constructor)) void on_dso_load_sms(void)
498{
499 /* register the driver with the core */
500 e1inp_driver_register(&dahdi_driver);
501}