blob: ba8ce02b65de44a9c1491be61f3fcfd60541b5a1 [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#ifdef HAVE_DAHDI_USER_H
26
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <errno.h>
31#include <string.h>
32#include <time.h>
33#include <sys/fcntl.h>
34#include <sys/socket.h>
35#include <sys/ioctl.h>
36#include <arpa/inet.h>
37#include <dahdi/user.h>
38
39#include <osmocom/core/select.h>
40#include <osmocom/core/msgb.h>
41#include <openbsc/debug.h>
42#include <openbsc/gsm_data.h>
43#include <openbsc/abis_nm.h>
44#include <openbsc/abis_rsl.h>
45#include <openbsc/subchan_demux.h>
46#include <openbsc/e1_input.h>
47#include <openbsc/signal.h>
48#include <talloc.h>
49
50#include "lapd.h"
51
52#define TS1_ALLOC_SIZE 300
53
54/* Corresponds to dahdi/user.h, only PRI related events */
55static const struct value_string dahdi_evt_names[] = {
56 { DAHDI_EVENT_NONE, "NONE" },
57 { DAHDI_EVENT_ALARM, "ALARM" },
58 { DAHDI_EVENT_NOALARM, "NOALARM" },
59 { DAHDI_EVENT_ABORT, "HDLC ABORT" },
60 { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" },
61 { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" },
62 { DAHDI_EVENT_REMOVED, "REMOVED" },
63 { 0, NULL }
64};
65
66static void handle_dahdi_exception(struct e1inp_ts *ts)
67{
68 int rc, evt;
69 struct input_signal_data isd;
70
71 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
72 if (rc < 0)
73 return;
74
75 LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
76 ts->line->num, ts->line->name, ts->num,
77 get_value_string(dahdi_evt_names, evt));
78
79 isd.line = ts->line;
80
81 switch (evt) {
82 case DAHDI_EVENT_ALARM:
83 /* we should notify the code that the line is gone */
84 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_ALARM, &isd);
85 break;
86 case DAHDI_EVENT_NOALARM:
87 /* alarm has gone, we should re-start the SABM requests */
88 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_NOALARM, &isd);
89 break;
90 }
91}
92
93static int handle_ts1_read(struct osmo_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 msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
99 lapd_mph_type prim;
100 unsigned int sapi, tei;
101 int ilen, ret;
102 uint8_t *idata;
103
104 if (!msg)
105 return -ENOMEM;
106
107 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
108 if (ret == -1)
109 handle_dahdi_exception(e1i_ts);
110 else if (ret < 0) {
111 perror("read ");
112 }
113 msgb_put(msg, ret - 2);
114 if (ret <= 3) {
115 perror("read ");
116 }
117
118 sapi = msg->data[0] >> 2;
119 tei = msg->data[1] >> 1;
120
121 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
122
123 idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim);
124 if (!idata && prim == 0)
125 return -EIO;
126
127 msgb_pull(msg, 2);
128
129 DEBUGP(DMI, "prim %08x\n", prim);
130
131 switch (prim) {
132 case 0:
133 break;
134 case LAPD_MPH_ACTIVATE_IND:
135 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
136 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi);
137 break;
138 case LAPD_MPH_DEACTIVATE_IND:
139 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
140 ret = e1inp_event(e1i_ts, S_INP_TEI_DN, tei, sapi);
141 break;
142 case LAPD_DL_DATA_IND:
143 case LAPD_DL_UNITDATA_IND:
144 if (prim == LAPD_DL_DATA_IND)
145 msg->l2h = msg->data + 2;
146 else
147 msg->l2h = msg->data + 1;
148 DEBUGP(DMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
149 ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi);
150 break;
151 default:
152 printf("ERROR: unknown prim\n");
153 break;
154 }
155
156 DEBUGP(DMI, "Returned ok\n");
157 return ret;
158}
159
160static int ts_want_write(struct e1inp_ts *e1i_ts)
161{
162 /* We never include the DAHDI B-Channel FD into the
163 * writeset, since it doesn't support poll() based
164 * write flow control */
165 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
166 fprintf(stderr, "Trying to write TRAU ts\n");
167 return 0;
168 }
169
170 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
171
172 return 0;
173}
174
175static void timeout_ts1_write(void *data)
176{
177 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
178
179 /* trigger write of ts1, due to tx delay timer */
180 ts_want_write(e1i_ts);
181}
182
183static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
184{
185 struct osmo_fd *bfd = cbdata;
186 struct e1inp_line *line = bfd->data;
187 unsigned int ts_nr = bfd->priv_nr;
188 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
189 int ret;
190
191 ret = write(bfd->fd, data, len + 2);
192 if (ret == -1)
193 handle_dahdi_exception(e1i_ts);
194 else if (ret < 0)
195 LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
196}
197
198static int handle_ts1_write(struct osmo_fd *bfd)
199{
200 struct e1inp_line *line = bfd->data;
201 unsigned int ts_nr = bfd->priv_nr;
202 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
203 struct e1inp_sign_link *sign_link;
204 struct msgb *msg;
205
206 bfd->when &= ~BSC_FD_WRITE;
207
208 /* get the next msg for this timeslot */
209 msg = e1inp_tx_ts(e1i_ts, &sign_link);
210 if (!msg) {
211 /* no message after tx delay timer */
212 return 0;
213 }
214
215 DEBUGP(DMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
216 lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei,
217 sign_link->sapi, msg->data, msg->len);
218 msgb_free(msg);
219
220 /* set tx delay timer for next event */
221 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
222 e1i_ts->sign.tx_timer.data = e1i_ts;
223 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
224
225 return 0;
226}
227
228
229static int invertbits = 1;
230
231static uint8_t flip_table[256];
232
233static void init_flip_bits(void)
234{
235 int i,k;
236
237 for (i = 0 ; i < 256 ; i++) {
238 uint8_t sample = 0 ;
239 for (k = 0; k<8; k++) {
240 if ( i & 1 << k ) sample |= 0x80 >> k;
241 }
242 flip_table[i] = sample;
243 }
244}
245
246static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
247{
248 int i;
249 uint8_t * start = buf;
250
251 for (i = 0 ; i < len; i++) {
252 buf[i] = flip_table[(uint8_t)buf[i]];
253 }
254
255 return start;
256}
257
258#define D_BCHAN_TX_GRAN 160
259/* write to a B channel TS */
260static int handle_tsX_write(struct osmo_fd *bfd)
261{
262 struct e1inp_line *line = bfd->data;
263 unsigned int ts_nr = bfd->priv_nr;
264 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
265 uint8_t tx_buf[D_BCHAN_TX_GRAN];
266 struct subch_mux *mx = &e1i_ts->trau.mux;
267 int ret;
268
269 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
270
271 if (ret != D_BCHAN_TX_GRAN) {
272 fprintf(stderr, "Huh, got ret of %d\n", ret);
273 if (ret < 0)
274 return ret;
275 }
276
277 DEBUGP(DMIB, "BCHAN TX: %s\n",
278 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
279
280 if (invertbits) {
281 flip_buf_bits(tx_buf, ret);
282 }
283
284 ret = write(bfd->fd, tx_buf, ret);
285 if (ret < D_BCHAN_TX_GRAN)
286 fprintf(stderr, "send returns %d instead of %d\n", ret,
287 D_BCHAN_TX_GRAN);
288
289 return ret;
290}
291
292#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
293/* FIXME: read from a B channel TS */
294static int handle_tsX_read(struct osmo_fd *bfd)
295{
296 struct e1inp_line *line = bfd->data;
297 unsigned int ts_nr = bfd->priv_nr;
298 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
299 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
300 int ret;
301
302 if (!msg)
303 return -ENOMEM;
304
305 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
306 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
307 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
308 return ret;
309 }
310
311 if (invertbits) {
312 flip_buf_bits(msg->data, ret);
313 }
314
315 msgb_put(msg, ret);
316
317 msg->l2h = msg->data;
318 DEBUGP(DMIB, "BCHAN RX: %s\n",
319 osmo_hexdump(msgb_l2(msg), ret));
320 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
321 /* physical layer indicates that data has been sent,
322 * we thus can send some more data */
323 ret = handle_tsX_write(bfd);
324 msgb_free(msg);
325
326 return ret;
327}
328
329/* callback from select.c in case one of the fd's can be read/written */
330static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
331{
332 struct e1inp_line *line = bfd->data;
333 unsigned int ts_nr = bfd->priv_nr;
334 unsigned int idx = ts_nr-1;
335 struct e1inp_ts *e1i_ts = &line->ts[idx];
336 int rc = 0;
337
338 switch (e1i_ts->type) {
339 case E1INP_TS_TYPE_SIGN:
340 if (what & BSC_FD_EXCEPT)
341 handle_dahdi_exception(e1i_ts);
342 if (what & BSC_FD_READ)
343 rc = handle_ts1_read(bfd);
344 if (what & BSC_FD_WRITE)
345 rc = handle_ts1_write(bfd);
346 break;
347 case E1INP_TS_TYPE_TRAU:
348 if (what & BSC_FD_EXCEPT)
349 handle_dahdi_exception(e1i_ts);
350 if (what & BSC_FD_READ)
351 rc = handle_tsX_read(bfd);
352 if (what & BSC_FD_WRITE)
353 rc = handle_tsX_write(bfd);
354 /* We never include the DAHDI B-Channel FD into the
355 * writeset, since it doesn't support poll() based
356 * write flow control */
357 break;
358 default:
359 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
360 break;
361 }
362
363 return rc;
364}
365
366static int
367dahdi_e1_line_update(struct e1inp_line *line, enum e1inp_line_role role);
368
369struct e1inp_driver dahdi_driver = {
370 .name = "dahdi",
371 .want_write = ts_want_write,
372 .line_update = &dahdi_e1_line_update,
373};
374
375void dahdi_set_bufinfo(int fd, int as_sigchan)
376{
377 struct dahdi_bufferinfo bi;
378 int x = 0;
379
380 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
381 fprintf(stderr, "Error getting bufinfo\n");
382 exit(-1);
383 }
384
385 if (as_sigchan) {
386 bi.numbufs = 4;
387 bi.bufsize = 512;
388 } else {
389 bi.numbufs = 8;
390 bi.bufsize = D_BCHAN_TX_GRAN;
391 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
392 }
393
394 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
395 fprintf(stderr, "Error setting bufinfo\n");
396 exit(-1);
397 }
398
399 if (!as_sigchan) {
400 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
401 fprintf(stderr, "Error setting bufinfo\n");
402 exit(-1);
403 }
404 } else {
405 int one = 1;
406 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
407 /* we cannot reliably check for the ioctl return value here
408 * as this command will fail if the slot _already_ was a
409 * signalling slot before :( */
410 }
411}
412
413static int dahdi_e1_setup(struct e1inp_line *line)
414{
415 int ts, ret;
416
417 /* TS0 is CRC4, don't need any fd for it */
418 for (ts = 1; ts < NUM_E1_TS; ts++) {
419 unsigned int idx = ts-1;
420 char openstr[128];
421 struct e1inp_ts *e1i_ts = &line->ts[idx];
422 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
423
424 bfd->data = line;
425 bfd->priv_nr = ts;
426 bfd->cb = dahdi_fd_cb;
427 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
428
429 switch (e1i_ts->type) {
430 case E1INP_TS_TYPE_NONE:
431 continue;
432 break;
433 case E1INP_TS_TYPE_SIGN:
434 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
435 if (bfd->fd == -1) {
436 fprintf(stderr, "%s could not open %s %s\n",
437 __func__, openstr, strerror(errno));
438 exit(-1);
439 }
440 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
441 dahdi_set_bufinfo(bfd->fd, 1);
442 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
443 break;
444 case E1INP_TS_TYPE_TRAU:
445 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
446 if (bfd->fd == -1) {
447 fprintf(stderr, "%s could not open %s %s\n",
448 __func__, openstr, strerror(errno));
449 exit(-1);
450 }
451 dahdi_set_bufinfo(bfd->fd, 0);
452 /* We never include the DAHDI B-Channel FD into the
453 * writeset, since it doesn't support poll() based
454 * write flow control */
455 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
456 break;
457 }
458
459 if (bfd->fd < 0) {
460 fprintf(stderr, "%s could not open %s %s\n",
461 __func__, openstr, strerror(errno));
462 return bfd->fd;
463 }
464
465 ret = osmo_fd_register(bfd);
466 if (ret < 0) {
467 fprintf(stderr, "could not register FD: %s\n",
468 strerror(ret));
469 return ret;
470 }
471 }
472
473 return 0;
474}
475
476static int
477dahdi_e1_line_update(struct e1inp_line *line, enum e1inp_line_role role)
478{
479 if (line->driver != &dahdi_driver)
480 return -EINVAL;
481
482 return dahdi_e1_setup(line);
483}
484
485int e1inp_dahdi_init(void)
486{
487 init_flip_bits();
488
489 /* register the driver with the core */
490 return e1inp_driver_register(&dahdi_driver);
491}
492
493#endif /* HAVE_DAHDI_USER_H */