blob: 1b366e1d91cb386554be1c12f3d45debff4e9ae4 [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
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020025#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/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <dahdi/user.h>
36
37#include <osmocom/core/select.h>
38#include <osmocom/core/msgb.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020039#include <osmocom/core/logging.h>
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +020040#include <osmocom/core/signal.h>
Harald Welte95e5dec2011-08-16 14:41:32 +020041#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +020042#include <osmocom/abis/subchan_demux.h>
43#include <osmocom/abis/e1_input.h>
Harald Welte71d87b22011-07-18 14:49:56 +020044#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020045
Pablo Neira Ayuso355ce692011-07-05 14:53:37 +020046#include <osmocom/abis/lapd.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020047
48#define TS1_ALLOC_SIZE 300
49
50/* Corresponds to dahdi/user.h, only PRI related events */
51static const struct value_string dahdi_evt_names[] = {
52 { DAHDI_EVENT_NONE, "NONE" },
53 { DAHDI_EVENT_ALARM, "ALARM" },
54 { DAHDI_EVENT_NOALARM, "NOALARM" },
55 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
56 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
57 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
58 { DAHDI_EVENT_REMOVED, "REMOVED" },
59 { 0, NULL }
60};
61
62static void handle_dahdi_exception(struct e1inp_ts *ts)
63{
64 int rc, evt;
Harald Welte95e5dec2011-08-16 14:41:32 +020065 struct e1inp_line *line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020066 struct input_signal_data isd;
67
68 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
69 if (rc < 0)
70 return;
71
Harald Weltecc2241b2011-07-19 16:06:06 +020072 LOGP(DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020073 ts->line->num, ts->line->name, ts->num,
74 get_value_string(dahdi_evt_names, evt));
75
76 isd.line = ts->line;
77
78 switch (evt) {
79 case DAHDI_EVENT_ALARM:
80 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +020081 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +020082 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020083 break;
84 case DAHDI_EVENT_NOALARM:
85 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +020086 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020087 break;
Harald Welte95e5dec2011-08-16 14:41:32 +020088 case DAHDI_EVENT_ABORT:
89 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
90 break;
91 case DAHDI_EVENT_OVERRUN:
92 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
93 break;
94 case DAHDI_EVENT_BADFCS:
95 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
96 break;
97 case DAHDI_EVENT_REMOVED:
98 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
99 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200100 }
101}
102
103static int handle_ts1_read(struct osmo_fd *bfd)
104{
105 struct e1inp_line *line = bfd->data;
106 unsigned int ts_nr = bfd->priv_nr;
107 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
108 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
109 lapd_mph_type prim;
110 unsigned int sapi, tei;
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200111 int ilen, ret, error = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200112 uint8_t *idata;
113
114 if (!msg)
115 return -ENOMEM;
116
117 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
118 if (ret == -1)
119 handle_dahdi_exception(e1i_ts);
120 else if (ret < 0) {
121 perror("read ");
122 }
123 msgb_put(msg, ret - 2);
124 if (ret <= 3) {
125 perror("read ");
126 }
127
128 sapi = msg->data[0] >> 2;
129 tei = msg->data[1] >> 1;
130
Harald Weltecc2241b2011-07-19 16:06:06 +0200131 DEBUGP(DLMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200132
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200133 idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim, &error);
134 if (!idata) {
135 switch(error) {
136 case LAPD_ERR_UNKNOWN_TEI:
137 /* We don't know about this TEI, probably the BSC
138 * lost local states (it crashed or it was stopped),
139 * notify the driver to see if it can do anything to
140 * recover the existing signalling links with the BTS.
141 */
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200142 e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi);
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200143 return -EIO;
144 }
145 if (prim == 0)
146 return -EIO;
147 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200148
149 msgb_pull(msg, 2);
150
Harald Weltecc2241b2011-07-19 16:06:06 +0200151 DEBUGP(DLMI, "prim %08x\n", prim);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200152
153 switch (prim) {
154 case 0:
155 break;
156 case LAPD_MPH_ACTIVATE_IND:
Harald Weltecc2241b2011-07-19 16:06:06 +0200157 DEBUGP(DLMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200158 ret = e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200159 break;
160 case LAPD_MPH_DEACTIVATE_IND:
Harald Weltecc2241b2011-07-19 16:06:06 +0200161 DEBUGP(DLMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200162 ret = e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200163 break;
164 case LAPD_DL_DATA_IND:
165 case LAPD_DL_UNITDATA_IND:
166 if (prim == LAPD_DL_DATA_IND)
167 msg->l2h = msg->data + 2;
168 else
169 msg->l2h = msg->data + 1;
Harald Weltecc2241b2011-07-19 16:06:06 +0200170 DEBUGP(DLMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200171 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
172 break;
173 default:
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +0200174 printf("ERROR: unknown prim\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200175 break;
176 }
177
Harald Weltecc2241b2011-07-19 16:06:06 +0200178 DEBUGP(DLMI, "Returned ok\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200179 return ret;
180}
181
182static int ts_want_write(struct e1inp_ts *e1i_ts)
183{
184 /* We never include the DAHDI B-Channel FD into the
185 * writeset, since it doesn't support poll() based
186 * write flow control */
187 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
188 fprintf(stderr, "Trying to write TRAU ts\n");
189 return 0;
190 }
191
192 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
193
194 return 0;
195}
196
197static void timeout_ts1_write(void *data)
198{
199 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
200
201 /* trigger write of ts1, due to tx delay timer */
202 ts_want_write(e1i_ts);
203}
204
205static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
206{
207 struct osmo_fd *bfd = cbdata;
208 struct e1inp_line *line = bfd->data;
209 unsigned int ts_nr = bfd->priv_nr;
210 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
211 int ret;
212
213 ret = write(bfd->fd, data, len + 2);
214 if (ret == -1)
215 handle_dahdi_exception(e1i_ts);
216 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200217 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200218}
219
220static int handle_ts1_write(struct osmo_fd *bfd)
221{
222 struct e1inp_line *line = bfd->data;
223 unsigned int ts_nr = bfd->priv_nr;
224 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
225 struct e1inp_sign_link *sign_link;
226 struct msgb *msg;
227
228 bfd->when &= ~BSC_FD_WRITE;
229
230 /* get the next msg for this timeslot */
231 msg = e1inp_tx_ts(e1i_ts, &sign_link);
232 if (!msg) {
233 /* no message after tx delay timer */
234 return 0;
235 }
236
Harald Weltecc2241b2011-07-19 16:06:06 +0200237 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200238 lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei,
239 sign_link->sapi, msg->data, msg->len);
240 msgb_free(msg);
241
242 /* set tx delay timer for next event */
243 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
244 e1i_ts->sign.tx_timer.data = e1i_ts;
245 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
246
247 return 0;
248}
249
250
251static int invertbits = 1;
252
253static uint8_t flip_table[256];
254
255static void init_flip_bits(void)
256{
257 int i,k;
258
259 for (i = 0 ; i < 256 ; i++) {
260 uint8_t sample = 0 ;
261 for (k = 0; k<8; k++) {
262 if ( i & 1 << k ) sample |= 0x80 >> k;
263 }
264 flip_table[i] = sample;
265 }
266}
267
268static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
269{
270 int i;
271 uint8_t * start = buf;
272
273 for (i = 0 ; i < len; i++) {
274 buf[i] = flip_table[(uint8_t)buf[i]];
275 }
276
277 return start;
278}
279
280#define D_BCHAN_TX_GRAN 160
281/* write to a B channel TS */
282static int handle_tsX_write(struct osmo_fd *bfd)
283{
284 struct e1inp_line *line = bfd->data;
285 unsigned int ts_nr = bfd->priv_nr;
286 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
287 uint8_t tx_buf[D_BCHAN_TX_GRAN];
288 struct subch_mux *mx = &e1i_ts->trau.mux;
289 int ret;
290
291 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
292
293 if (ret != D_BCHAN_TX_GRAN) {
294 fprintf(stderr, "Huh, got ret of %d\n", ret);
295 if (ret < 0)
296 return ret;
297 }
298
Harald Weltecc2241b2011-07-19 16:06:06 +0200299 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200300 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
301
302 if (invertbits) {
303 flip_buf_bits(tx_buf, ret);
304 }
305
306 ret = write(bfd->fd, tx_buf, ret);
307 if (ret < D_BCHAN_TX_GRAN)
308 fprintf(stderr, "send returns %d instead of %d\n", ret,
309 D_BCHAN_TX_GRAN);
310
311 return ret;
312}
313
314#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
315/* FIXME: read from a B channel TS */
316static int handle_tsX_read(struct osmo_fd *bfd)
317{
318 struct e1inp_line *line = bfd->data;
319 unsigned int ts_nr = bfd->priv_nr;
320 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
321 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
322 int ret;
323
324 if (!msg)
325 return -ENOMEM;
326
327 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
328 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
329 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
330 return ret;
331 }
332
333 if (invertbits) {
334 flip_buf_bits(msg->data, ret);
335 }
336
337 msgb_put(msg, ret);
338
339 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200340 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200341 osmo_hexdump(msgb_l2(msg), ret));
342 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
343 /* physical layer indicates that data has been sent,
344 * we thus can send some more data */
345 ret = handle_tsX_write(bfd);
346 msgb_free(msg);
347
348 return ret;
349}
350
351/* callback from select.c in case one of the fd's can be read/written */
352static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
353{
354 struct e1inp_line *line = bfd->data;
355 unsigned int ts_nr = bfd->priv_nr;
356 unsigned int idx = ts_nr-1;
357 struct e1inp_ts *e1i_ts = &line->ts[idx];
358 int rc = 0;
359
360 switch (e1i_ts->type) {
361 case E1INP_TS_TYPE_SIGN:
362 if (what & BSC_FD_EXCEPT)
363 handle_dahdi_exception(e1i_ts);
364 if (what & BSC_FD_READ)
365 rc = handle_ts1_read(bfd);
366 if (what & BSC_FD_WRITE)
367 rc = handle_ts1_write(bfd);
368 break;
369 case E1INP_TS_TYPE_TRAU:
370 if (what & BSC_FD_EXCEPT)
371 handle_dahdi_exception(e1i_ts);
372 if (what & BSC_FD_READ)
373 rc = handle_tsX_read(bfd);
374 if (what & BSC_FD_WRITE)
375 rc = handle_tsX_write(bfd);
376 /* We never include the DAHDI B-Channel FD into the
377 * writeset, since it doesn't support poll() based
378 * write flow control */
379 break;
380 default:
381 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
382 break;
383 }
384
385 return rc;
386}
387
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200388static int dahdi_e1_line_update(struct e1inp_line *line,
389 enum e1inp_line_role role, const char *addr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200390
391struct e1inp_driver dahdi_driver = {
392 .name = "dahdi",
393 .want_write = ts_want_write,
394 .line_update = &dahdi_e1_line_update,
395};
396
397void dahdi_set_bufinfo(int fd, int as_sigchan)
398{
399 struct dahdi_bufferinfo bi;
400 int x = 0;
401
402 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
403 fprintf(stderr, "Error getting bufinfo\n");
404 exit(-1);
405 }
406
407 if (as_sigchan) {
408 bi.numbufs = 4;
409 bi.bufsize = 512;
410 } else {
411 bi.numbufs = 8;
412 bi.bufsize = D_BCHAN_TX_GRAN;
413 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
414 }
415
416 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
417 fprintf(stderr, "Error setting bufinfo\n");
418 exit(-1);
419 }
420
421 if (!as_sigchan) {
422 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
423 fprintf(stderr, "Error setting bufinfo\n");
424 exit(-1);
425 }
426 } else {
427 int one = 1;
428 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
429 /* we cannot reliably check for the ioctl return value here
430 * as this command will fail if the slot _already_ was a
431 * signalling slot before :( */
432 }
433}
434
435static int dahdi_e1_setup(struct e1inp_line *line)
436{
437 int ts, ret;
438
439 /* TS0 is CRC4, don't need any fd for it */
440 for (ts = 1; ts < NUM_E1_TS; ts++) {
441 unsigned int idx = ts-1;
442 char openstr[128];
443 struct e1inp_ts *e1i_ts = &line->ts[idx];
444 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200445 int dev_nr;
446
447 /* DAHDI device names/numbers just keep incrementing
448 * even over multiple boards. So TS1 of the second
449 * board will be 32 */
450 dev_nr = line->num * (NUM_E1_TS-1) + ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200451
452 bfd->data = line;
453 bfd->priv_nr = ts;
454 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200455 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200456
457 switch (e1i_ts->type) {
458 case E1INP_TS_TYPE_NONE:
459 continue;
460 break;
461 case E1INP_TS_TYPE_SIGN:
462 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
463 if (bfd->fd == -1) {
464 fprintf(stderr, "%s could not open %s %s\n",
465 __func__, openstr, strerror(errno));
466 exit(-1);
467 }
468 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
469 dahdi_set_bufinfo(bfd->fd, 1);
470 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
471 break;
472 case E1INP_TS_TYPE_TRAU:
473 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
474 if (bfd->fd == -1) {
475 fprintf(stderr, "%s could not open %s %s\n",
476 __func__, openstr, strerror(errno));
477 exit(-1);
478 }
479 dahdi_set_bufinfo(bfd->fd, 0);
480 /* We never include the DAHDI B-Channel FD into the
481 * writeset, since it doesn't support poll() based
482 * write flow control */
483 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
484 break;
485 }
486
487 if (bfd->fd < 0) {
488 fprintf(stderr, "%s could not open %s %s\n",
489 __func__, openstr, strerror(errno));
490 return bfd->fd;
491 }
492
493 ret = osmo_fd_register(bfd);
494 if (ret < 0) {
495 fprintf(stderr, "could not register FD: %s\n",
496 strerror(ret));
497 return ret;
498 }
499 }
500
501 return 0;
502}
503
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200504static int dahdi_e1_line_update(struct e1inp_line *line,
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +0200505 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200506{
507 if (line->driver != &dahdi_driver)
508 return -EINVAL;
509
510 return dahdi_e1_setup(line);
511}
512
513int e1inp_dahdi_init(void)
514{
515 init_flip_bits();
516
517 /* register the driver with the core */
518 return e1inp_driver_register(&dahdi_driver);
519}