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