blob: a4dbd03f8b5bc7bd83091ceab2422a5977560db8 [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
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;
Harald Welte95e5dec2011-08-16 14:41:32 +020069 struct e1inp_line *line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020070 struct input_signal_data isd;
71
72 rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt);
73 if (rc < 0)
74 return;
75
Harald Weltecc2241b2011-07-19 16:06:06 +020076 LOGP(DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020077 ts->line->num, ts->line->name, ts->num,
78 get_value_string(dahdi_evt_names, evt));
79
80 isd.line = ts->line;
81
82 switch (evt) {
83 case DAHDI_EVENT_ALARM:
84 /* we should notify the code that the line is gone */
Pablo Neira Ayusode668912011-08-16 17:26:23 +020085 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd);
Harald Welte95e5dec2011-08-16 14:41:32 +020086 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020087 break;
88 case DAHDI_EVENT_NOALARM:
89 /* alarm has gone, we should re-start the SABM requests */
Pablo Neira Ayusode668912011-08-16 17:26:23 +020090 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020091 break;
Harald Welte95e5dec2011-08-16 14:41:32 +020092 case DAHDI_EVENT_ABORT:
93 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]);
94 break;
95 case DAHDI_EVENT_OVERRUN:
96 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]);
97 break;
98 case DAHDI_EVENT_BADFCS:
99 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]);
100 break;
101 case DAHDI_EVENT_REMOVED:
102 rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]);
103 break;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200104 }
105}
106
107static int handle_ts1_read(struct osmo_fd *bfd)
108{
109 struct e1inp_line *line = bfd->data;
110 unsigned int ts_nr = bfd->priv_nr;
111 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
112 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200113 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200114
115 if (!msg)
116 return -ENOMEM;
117
118 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
119 if (ret == -1)
120 handle_dahdi_exception(e1i_ts);
121 else if (ret < 0) {
122 perror("read ");
123 }
124 msgb_put(msg, ret - 2);
125 if (ret <= 3) {
126 perror("read ");
127 }
128
Harald Weltefd44a5f2011-08-21 00:48:54 +0200129 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200130}
131
132static int ts_want_write(struct e1inp_ts *e1i_ts)
133{
134 /* We never include the DAHDI B-Channel FD into the
135 * writeset, since it doesn't support poll() based
136 * write flow control */
137 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
138 fprintf(stderr, "Trying to write TRAU ts\n");
139 return 0;
140 }
141
142 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
143
144 return 0;
145}
146
147static void timeout_ts1_write(void *data)
148{
149 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
150
151 /* trigger write of ts1, due to tx delay timer */
152 ts_want_write(e1i_ts);
153}
154
155static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
156{
157 struct osmo_fd *bfd = cbdata;
158 struct e1inp_line *line = bfd->data;
159 unsigned int ts_nr = bfd->priv_nr;
160 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
161 int ret;
162
163 ret = write(bfd->fd, data, len + 2);
164 if (ret == -1)
165 handle_dahdi_exception(e1i_ts);
166 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200167 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200168}
169
170static int handle_ts1_write(struct osmo_fd *bfd)
171{
172 struct e1inp_line *line = bfd->data;
173 unsigned int ts_nr = bfd->priv_nr;
174 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
175 struct e1inp_sign_link *sign_link;
176 struct msgb *msg;
177
178 bfd->when &= ~BSC_FD_WRITE;
179
180 /* get the next msg for this timeslot */
181 msg = e1inp_tx_ts(e1i_ts, &sign_link);
182 if (!msg) {
183 /* no message after tx delay timer */
184 return 0;
185 }
186
Harald Weltecc2241b2011-07-19 16:06:06 +0200187 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200188 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200189 sign_link->sapi, msg->data, msg->len);
190 msgb_free(msg);
191
192 /* set tx delay timer for next event */
193 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
194 e1i_ts->sign.tx_timer.data = e1i_ts;
195 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
196
197 return 0;
198}
199
200
201static int invertbits = 1;
202
203static uint8_t flip_table[256];
204
205static void init_flip_bits(void)
206{
207 int i,k;
208
209 for (i = 0 ; i < 256 ; i++) {
210 uint8_t sample = 0 ;
211 for (k = 0; k<8; k++) {
212 if ( i & 1 << k ) sample |= 0x80 >> k;
213 }
214 flip_table[i] = sample;
215 }
216}
217
218static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
219{
220 int i;
221 uint8_t * start = buf;
222
223 for (i = 0 ; i < len; i++) {
224 buf[i] = flip_table[(uint8_t)buf[i]];
225 }
226
227 return start;
228}
229
230#define D_BCHAN_TX_GRAN 160
231/* write to a B channel TS */
232static int handle_tsX_write(struct osmo_fd *bfd)
233{
234 struct e1inp_line *line = bfd->data;
235 unsigned int ts_nr = bfd->priv_nr;
236 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
237 uint8_t tx_buf[D_BCHAN_TX_GRAN];
238 struct subch_mux *mx = &e1i_ts->trau.mux;
239 int ret;
240
241 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
242
243 if (ret != D_BCHAN_TX_GRAN) {
244 fprintf(stderr, "Huh, got ret of %d\n", ret);
245 if (ret < 0)
246 return ret;
247 }
248
Harald Weltecc2241b2011-07-19 16:06:06 +0200249 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200250 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
251
252 if (invertbits) {
253 flip_buf_bits(tx_buf, ret);
254 }
255
256 ret = write(bfd->fd, tx_buf, ret);
257 if (ret < D_BCHAN_TX_GRAN)
258 fprintf(stderr, "send returns %d instead of %d\n", ret,
259 D_BCHAN_TX_GRAN);
260
261 return ret;
262}
263
264#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
265/* FIXME: read from a B channel TS */
266static int handle_tsX_read(struct osmo_fd *bfd)
267{
268 struct e1inp_line *line = bfd->data;
269 unsigned int ts_nr = bfd->priv_nr;
270 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
271 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
272 int ret;
273
274 if (!msg)
275 return -ENOMEM;
276
277 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
278 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
279 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
280 return ret;
281 }
282
283 if (invertbits) {
284 flip_buf_bits(msg->data, ret);
285 }
286
287 msgb_put(msg, ret);
288
289 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200290 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200291 osmo_hexdump(msgb_l2(msg), ret));
292 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
293 /* physical layer indicates that data has been sent,
294 * we thus can send some more data */
295 ret = handle_tsX_write(bfd);
296 msgb_free(msg);
297
298 return ret;
299}
300
301/* callback from select.c in case one of the fd's can be read/written */
302static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
303{
304 struct e1inp_line *line = bfd->data;
305 unsigned int ts_nr = bfd->priv_nr;
306 unsigned int idx = ts_nr-1;
307 struct e1inp_ts *e1i_ts = &line->ts[idx];
308 int rc = 0;
309
310 switch (e1i_ts->type) {
311 case E1INP_TS_TYPE_SIGN:
312 if (what & BSC_FD_EXCEPT)
313 handle_dahdi_exception(e1i_ts);
314 if (what & BSC_FD_READ)
315 rc = handle_ts1_read(bfd);
316 if (what & BSC_FD_WRITE)
317 rc = handle_ts1_write(bfd);
318 break;
319 case E1INP_TS_TYPE_TRAU:
320 if (what & BSC_FD_EXCEPT)
321 handle_dahdi_exception(e1i_ts);
322 if (what & BSC_FD_READ)
323 rc = handle_tsX_read(bfd);
324 if (what & BSC_FD_WRITE)
325 rc = handle_tsX_write(bfd);
326 /* We never include the DAHDI B-Channel FD into the
327 * writeset, since it doesn't support poll() based
328 * write flow control */
329 break;
330 default:
331 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
332 break;
333 }
334
335 return rc;
336}
337
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200338static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200339
340struct e1inp_driver dahdi_driver = {
341 .name = "dahdi",
342 .want_write = ts_want_write,
343 .line_update = &dahdi_e1_line_update,
344};
345
346void dahdi_set_bufinfo(int fd, int as_sigchan)
347{
348 struct dahdi_bufferinfo bi;
349 int x = 0;
350
351 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
352 fprintf(stderr, "Error getting bufinfo\n");
353 exit(-1);
354 }
355
356 if (as_sigchan) {
357 bi.numbufs = 4;
358 bi.bufsize = 512;
359 } else {
360 bi.numbufs = 8;
361 bi.bufsize = D_BCHAN_TX_GRAN;
362 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
363 }
364
365 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
366 fprintf(stderr, "Error setting bufinfo\n");
367 exit(-1);
368 }
369
370 if (!as_sigchan) {
371 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
372 fprintf(stderr, "Error setting bufinfo\n");
373 exit(-1);
374 }
375 } else {
376 int one = 1;
377 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
378 /* we cannot reliably check for the ioctl return value here
379 * as this command will fail if the slot _already_ was a
380 * signalling slot before :( */
381 }
382}
383
384static int dahdi_e1_setup(struct e1inp_line *line)
385{
386 int ts, ret;
387
388 /* TS0 is CRC4, don't need any fd for it */
389 for (ts = 1; ts < NUM_E1_TS; ts++) {
390 unsigned int idx = ts-1;
391 char openstr[128];
392 struct e1inp_ts *e1i_ts = &line->ts[idx];
393 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200394 int dev_nr;
395
396 /* DAHDI device names/numbers just keep incrementing
397 * even over multiple boards. So TS1 of the second
398 * board will be 32 */
399 dev_nr = line->num * (NUM_E1_TS-1) + ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200400
401 bfd->data = line;
402 bfd->priv_nr = ts;
403 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200404 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200405
406 switch (e1i_ts->type) {
407 case E1INP_TS_TYPE_NONE:
408 continue;
409 break;
410 case E1INP_TS_TYPE_SIGN:
411 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
412 if (bfd->fd == -1) {
413 fprintf(stderr, "%s could not open %s %s\n",
414 __func__, openstr, strerror(errno));
415 exit(-1);
416 }
417 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
418 dahdi_set_bufinfo(bfd->fd, 1);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200419 e1i_ts->lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200420 break;
421 case E1INP_TS_TYPE_TRAU:
422 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
423 if (bfd->fd == -1) {
424 fprintf(stderr, "%s could not open %s %s\n",
425 __func__, openstr, strerror(errno));
426 exit(-1);
427 }
428 dahdi_set_bufinfo(bfd->fd, 0);
429 /* We never include the DAHDI B-Channel FD into the
430 * writeset, since it doesn't support poll() based
431 * write flow control */
432 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
433 break;
434 }
435
436 if (bfd->fd < 0) {
437 fprintf(stderr, "%s could not open %s %s\n",
438 __func__, openstr, strerror(errno));
439 return bfd->fd;
440 }
441
442 ret = osmo_fd_register(bfd);
443 if (ret < 0) {
444 fprintf(stderr, "could not register FD: %s\n",
445 strerror(ret));
446 return ret;
447 }
448 }
449
450 return 0;
451}
452
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200453static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200454{
455 if (line->driver != &dahdi_driver)
456 return -EINVAL;
457
458 return dahdi_e1_setup(line);
459}
460
461int e1inp_dahdi_init(void)
462{
463 init_flip_bits();
464
465 /* register the driver with the core */
466 return e1inp_driver_register(&dahdi_driver);
467}
Harald Welte3bc78852011-08-24 08:32:38 +0200468
469#endif /* HAVE_DAHDI_USER_H */