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