blob: 75331ccae1e4508043d42076e790f835ddf5bcdd [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
Harald Welte3bc78852011-08-24 08:32:38 +020025#include "../../config.h"
26
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
41#include <osmocom/core/select.h>
42#include <osmocom/core/msgb.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020043#include <osmocom/core/logging.h>
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +020044#include <osmocom/core/signal.h>
Harald Welte95e5dec2011-08-16 14:41:32 +020045#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020046#include <osmocom/abis/subchan_demux.h>
47#include <osmocom/abis/e1_input.h>
Harald Welte71d87b22011-07-18 14:49:56 +020048#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
Pablo Neira Ayuso355ce692011-07-05 14:53:37 +020050#include <osmocom/abis/lapd.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020051
52#define TS1_ALLOC_SIZE 300
53
Harald Weltec2889512011-09-13 23:49:04 +010054struct span_cfg {
55 struct llist_head list;
56
57 unsigned int span_nr;
58 unsigned int chan_base;
59 unsigned int chan_num;
60};
61
62static struct span_cfg *span_cfgs[DAHDI_MAX_SPANS];
63
64static int reread_span_cfgs(void)
65{
66 struct dahdi_spaninfo si;
67 unsigned int basechan = 1;
68 int i;
69 int fd;
70
71 if ((fd = open("/dev/dahdi/ctl", O_RDWR)) < 0) {
72 LOGP(DLMI, LOGL_ERROR, "Unable to open DAHDI ctl: %s\n",
73 strerror(errno));
74 return -EIO;
75 }
76
77 for (i = 1; i < DAHDI_MAX_SPANS; i++) {
78 struct span_cfg *scfg;
79
80 /* clear any old cached information */
81 if (span_cfgs[i]) {
82 talloc_free(span_cfgs[i]);
83 span_cfgs[i] = NULL;
84 }
85
86 memset(&si, 0, sizeof(si));
87 si.spanno = i;
88 if (ioctl(fd, DAHDI_SPANSTAT, &si))
89 continue;
90
91 /* create and link new span_cfg */
92 scfg = talloc_zero(NULL, struct span_cfg);
93 if (!scfg) {
94 close(fd);
95 return -ENOMEM;
96 }
97 scfg->span_nr = i;
98 scfg->chan_num = si.totalchans;
99 scfg->chan_base = basechan;
100 span_cfgs[i] = scfg;
101
102 basechan += si.totalchans;
103 }
104
105 close(fd);
106
107 return 0;
108}
109
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200110/* Corresponds to dahdi/user.h, only PRI related events */
111static const struct value_string dahdi_evt_names[] = {
112 { DAHDI_EVENT_NONE, "NONE" },
113 { DAHDI_EVENT_ALARM, "ALARM" },
114 { DAHDI_EVENT_NOALARM, "NOALARM" },
115 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
116 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
117 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
118 { DAHDI_EVENT_REMOVED, "REMOVED" },
119 { 0, NULL }
120};
121
122static void handle_dahdi_exception(struct e1inp_ts *ts)
123{
124 int rc, evt;
Harald Welte95e5dec2011-08-16 14:41:32 +0200125 struct e1inp_line *line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200126 struct input_signal_data isd;
127
128 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
129 if (rc < 0)
130 return;
131
Harald Weltecc2241b2011-07-19 16:06:06 +0200132 LOGP(DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200133 ts->line->num, ts->line->name, ts->num,
134 get_value_string(dahdi_evt_names, evt));
135
136 isd.line = ts->line;
137
138 switch (evt) {
139 case DAHDI_EVENT_ALARM:
140 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200141 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +0200142 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200143 break;
144 case DAHDI_EVENT_NOALARM:
145 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200146 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200147 break;
Harald Welte95e5dec2011-08-16 14:41:32 +0200148 case DAHDI_EVENT_ABORT:
149 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
150 break;
151 case DAHDI_EVENT_OVERRUN:
152 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
153 break;
154 case DAHDI_EVENT_BADFCS:
155 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
156 break;
157 case DAHDI_EVENT_REMOVED:
158 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
159 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200160 }
161}
162
163static int handle_ts1_read(struct osmo_fd *bfd)
164{
165 struct e1inp_line *line = bfd->data;
166 unsigned int ts_nr = bfd->priv_nr;
167 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
168 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200169 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200170
171 if (!msg)
172 return -ENOMEM;
173
174 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
175 if (ret == -1)
176 handle_dahdi_exception(e1i_ts);
177 else if (ret < 0) {
178 perror("read ");
179 }
180 msgb_put(msg, ret - 2);
181 if (ret <= 3) {
182 perror("read ");
183 }
184
Harald Weltefd44a5f2011-08-21 00:48:54 +0200185 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200186}
187
188static int ts_want_write(struct e1inp_ts *e1i_ts)
189{
190 /* We never include the DAHDI B-Channel FD into the
191 * writeset, since it doesn't support poll() based
192 * write flow control */
193 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
194 fprintf(stderr, "Trying to write TRAU ts\n");
195 return 0;
196 }
197
198 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
199
200 return 0;
201}
202
203static void timeout_ts1_write(void *data)
204{
205 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
206
207 /* trigger write of ts1, due to tx delay timer */
208 ts_want_write(e1i_ts);
209}
210
211static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
212{
213 struct osmo_fd *bfd = cbdata;
214 struct e1inp_line *line = bfd->data;
215 unsigned int ts_nr = bfd->priv_nr;
216 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
217 int ret;
218
219 ret = write(bfd->fd, data, len + 2);
220 if (ret == -1)
221 handle_dahdi_exception(e1i_ts);
222 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200223 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200224}
225
226static int handle_ts1_write(struct osmo_fd *bfd)
227{
228 struct e1inp_line *line = bfd->data;
229 unsigned int ts_nr = bfd->priv_nr;
230 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
231 struct e1inp_sign_link *sign_link;
232 struct msgb *msg;
233
234 bfd->when &= ~BSC_FD_WRITE;
235
236 /* get the next msg for this timeslot */
237 msg = e1inp_tx_ts(e1i_ts, &sign_link);
238 if (!msg) {
239 /* no message after tx delay timer */
240 return 0;
241 }
242
Harald Weltecc2241b2011-07-19 16:06:06 +0200243 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200244 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200245 sign_link->sapi, msg->data, msg->len);
246 msgb_free(msg);
247
248 /* set tx delay timer for next event */
249 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
250 e1i_ts->sign.tx_timer.data = e1i_ts;
251 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
252
253 return 0;
254}
255
256
257static int invertbits = 1;
258
259static uint8_t flip_table[256];
260
261static void init_flip_bits(void)
262{
263 int i,k;
264
265 for (i = 0 ; i < 256 ; i++) {
266 uint8_t sample = 0 ;
267 for (k = 0; k<8; k++) {
268 if ( i & 1 << k ) sample |= 0x80 >> k;
269 }
270 flip_table[i] = sample;
271 }
272}
273
274static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
275{
276 int i;
277 uint8_t * start = buf;
278
279 for (i = 0 ; i < len; i++) {
280 buf[i] = flip_table[(uint8_t)buf[i]];
281 }
282
283 return start;
284}
285
286#define D_BCHAN_TX_GRAN 160
287/* write to a B channel TS */
288static int handle_tsX_write(struct osmo_fd *bfd)
289{
290 struct e1inp_line *line = bfd->data;
291 unsigned int ts_nr = bfd->priv_nr;
292 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
293 uint8_t tx_buf[D_BCHAN_TX_GRAN];
294 struct subch_mux *mx = &e1i_ts->trau.mux;
295 int ret;
296
297 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
298
299 if (ret != D_BCHAN_TX_GRAN) {
300 fprintf(stderr, "Huh, got ret of %d\n", ret);
301 if (ret < 0)
302 return ret;
303 }
304
Harald Weltecc2241b2011-07-19 16:06:06 +0200305 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200306 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
307
308 if (invertbits) {
309 flip_buf_bits(tx_buf, ret);
310 }
311
312 ret = write(bfd->fd, tx_buf, ret);
313 if (ret < D_BCHAN_TX_GRAN)
314 fprintf(stderr, "send returns %d instead of %d\n", ret,
315 D_BCHAN_TX_GRAN);
316
317 return ret;
318}
319
320#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
321/* FIXME: read from a B channel TS */
322static int handle_tsX_read(struct osmo_fd *bfd)
323{
324 struct e1inp_line *line = bfd->data;
325 unsigned int ts_nr = bfd->priv_nr;
326 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
327 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
328 int ret;
329
330 if (!msg)
331 return -ENOMEM;
332
333 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
334 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
335 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
336 return ret;
337 }
338
339 if (invertbits) {
340 flip_buf_bits(msg->data, ret);
341 }
342
343 msgb_put(msg, ret);
344
345 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200346 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200347 osmo_hexdump(msgb_l2(msg), ret));
348 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
349 /* physical layer indicates that data has been sent,
350 * we thus can send some more data */
351 ret = handle_tsX_write(bfd);
352 msgb_free(msg);
353
354 return ret;
355}
356
357/* callback from select.c in case one of the fd's can be read/written */
358static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
359{
360 struct e1inp_line *line = bfd->data;
361 unsigned int ts_nr = bfd->priv_nr;
362 unsigned int idx = ts_nr-1;
363 struct e1inp_ts *e1i_ts = &line->ts[idx];
364 int rc = 0;
365
366 switch (e1i_ts->type) {
367 case E1INP_TS_TYPE_SIGN:
368 if (what & BSC_FD_EXCEPT)
369 handle_dahdi_exception(e1i_ts);
370 if (what & BSC_FD_READ)
371 rc = handle_ts1_read(bfd);
372 if (what & BSC_FD_WRITE)
373 rc = handle_ts1_write(bfd);
374 break;
375 case E1INP_TS_TYPE_TRAU:
376 if (what & BSC_FD_EXCEPT)
377 handle_dahdi_exception(e1i_ts);
378 if (what & BSC_FD_READ)
379 rc = handle_tsX_read(bfd);
380 if (what & BSC_FD_WRITE)
381 rc = handle_tsX_write(bfd);
382 /* We never include the DAHDI B-Channel FD into the
383 * writeset, since it doesn't support poll() based
384 * write flow control */
385 break;
386 default:
387 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
388 break;
389 }
390
391 return rc;
392}
393
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200394static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200395
396struct e1inp_driver dahdi_driver = {
397 .name = "dahdi",
398 .want_write = ts_want_write,
399 .line_update = &dahdi_e1_line_update,
400};
401
402void dahdi_set_bufinfo(int fd, int as_sigchan)
403{
404 struct dahdi_bufferinfo bi;
405 int x = 0;
406
407 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
408 fprintf(stderr, "Error getting bufinfo\n");
409 exit(-1);
410 }
411
412 if (as_sigchan) {
413 bi.numbufs = 4;
414 bi.bufsize = 512;
415 } else {
416 bi.numbufs = 8;
417 bi.bufsize = D_BCHAN_TX_GRAN;
418 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
419 }
420
421 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
422 fprintf(stderr, "Error setting bufinfo\n");
423 exit(-1);
424 }
425
426 if (!as_sigchan) {
427 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
428 fprintf(stderr, "Error setting bufinfo\n");
429 exit(-1);
430 }
431 } else {
432 int one = 1;
433 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
434 /* we cannot reliably check for the ioctl return value here
435 * as this command will fail if the slot _already_ was a
436 * signalling slot before :( */
437 }
438}
439
440static int dahdi_e1_setup(struct e1inp_line *line)
441{
Harald Weltec2889512011-09-13 23:49:04 +0100442 struct span_cfg *scfg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200443 int ts, ret;
444
Harald Weltec2889512011-09-13 23:49:04 +0100445 reread_span_cfgs();
446
447 scfg = span_cfgs[line->port_nr];
448 if (!scfg)
449 return -EIO;
450
451 line->num_ts = scfg->chan_num;
452
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200453 /* TS0 is CRC4, don't need any fd for it */
Harald Weltec2889512011-09-13 23:49:04 +0100454 for (ts = 1; ts <= scfg->chan_num; ts++) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200455 unsigned int idx = ts-1;
456 char openstr[128];
457 struct e1inp_ts *e1i_ts = &line->ts[idx];
458 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200459 int dev_nr;
460
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200461 /* unregister FD if it was already registered */
462 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
463 osmo_fd_unregister(bfd);
464
Harald Weltef3ca61c2011-08-09 11:21:23 +0200465 /* DAHDI device names/numbers just keep incrementing
466 * even over multiple boards. So TS1 of the second
467 * board will be 32 */
Harald Weltec2889512011-09-13 23:49:04 +0100468 dev_nr = scfg->chan_base + idx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200469
470 bfd->data = line;
471 bfd->priv_nr = ts;
472 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200473 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200474
475 switch (e1i_ts->type) {
476 case E1INP_TS_TYPE_NONE:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200477 /* close/release LAPD instance, if any */
478 if (e1i_ts->lapd) {
479 lapd_instance_free(e1i_ts->lapd);
480 e1i_ts->lapd = NULL;
481 }
482 if (bfd->fd) {
483 close(bfd->fd);
484 bfd->fd = 0;
485 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200486 continue;
487 break;
488 case E1INP_TS_TYPE_SIGN:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200489 if (!bfd->fd)
490 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200491 if (bfd->fd == -1) {
492 fprintf(stderr, "%s could not open %s %s\n",
493 __func__, openstr, strerror(errno));
494 exit(-1);
495 }
496 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
497 dahdi_set_bufinfo(bfd->fd, 1);
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200498 if (!e1i_ts->lapd)
499 e1i_ts->lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200500 break;
501 case E1INP_TS_TYPE_TRAU:
Harald Weltecfc9f1f2011-08-24 09:45:36 +0200502 /* close/release LAPD instance, if any */
503 if (e1i_ts->lapd) {
504 lapd_instance_free(e1i_ts->lapd);
505 e1i_ts->lapd = NULL;
506 }
507 if (!bfd->fd)
508 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200509 if (bfd->fd == -1) {
510 fprintf(stderr, "%s could not open %s %s\n",
511 __func__, openstr, strerror(errno));
512 exit(-1);
513 }
514 dahdi_set_bufinfo(bfd->fd, 0);
515 /* We never include the DAHDI B-Channel FD into the
516 * writeset, since it doesn't support poll() based
517 * write flow control */
518 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
519 break;
520 }
521
522 if (bfd->fd < 0) {
523 fprintf(stderr, "%s could not open %s %s\n",
524 __func__, openstr, strerror(errno));
525 return bfd->fd;
526 }
527
528 ret = osmo_fd_register(bfd);
529 if (ret < 0) {
530 fprintf(stderr, "could not register FD: %s\n",
531 strerror(ret));
532 return ret;
533 }
534 }
535
536 return 0;
537}
538
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200539static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200540{
541 if (line->driver != &dahdi_driver)
542 return -EINVAL;
543
544 return dahdi_e1_setup(line);
545}
546
547int e1inp_dahdi_init(void)
548{
549 init_flip_bits();
550
551 /* register the driver with the core */
552 return e1inp_driver_register(&dahdi_driver);
553}
Harald Welte3bc78852011-08-24 08:32:38 +0200554
555#endif /* HAVE_DAHDI_USER_H */