blob: 4fd4e61d0bd384d966d02ce87dff1edb77529160 [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");
Harald Weltefd44a5f2011-08-21 00:48:54 +0200109 int ret;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200110
111 if (!msg)
112 return -ENOMEM;
113
114 ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16);
115 if (ret == -1)
116 handle_dahdi_exception(e1i_ts);
117 else if (ret < 0) {
118 perror("read ");
119 }
120 msgb_put(msg, ret - 2);
121 if (ret <= 3) {
122 perror("read ");
123 }
124
Harald Weltefd44a5f2011-08-21 00:48:54 +0200125 return e1inp_rx_ts_lapd(e1i_ts, msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200126}
127
128static int ts_want_write(struct e1inp_ts *e1i_ts)
129{
130 /* We never include the DAHDI B-Channel FD into the
131 * writeset, since it doesn't support poll() based
132 * write flow control */
133 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
134 fprintf(stderr, "Trying to write TRAU ts\n");
135 return 0;
136 }
137
138 e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
139
140 return 0;
141}
142
143static void timeout_ts1_write(void *data)
144{
145 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
146
147 /* trigger write of ts1, due to tx delay timer */
148 ts_want_write(e1i_ts);
149}
150
151static void dahdi_write_msg(uint8_t *data, int len, void *cbdata)
152{
153 struct osmo_fd *bfd = cbdata;
154 struct e1inp_line *line = bfd->data;
155 unsigned int ts_nr = bfd->priv_nr;
156 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
157 int ret;
158
159 ret = write(bfd->fd, data, len + 2);
160 if (ret == -1)
161 handle_dahdi_exception(e1i_ts);
162 else if (ret < 0)
Harald Weltecc2241b2011-07-19 16:06:06 +0200163 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200164}
165
166static int handle_ts1_write(struct osmo_fd *bfd)
167{
168 struct e1inp_line *line = bfd->data;
169 unsigned int ts_nr = bfd->priv_nr;
170 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
171 struct e1inp_sign_link *sign_link;
172 struct msgb *msg;
173
174 bfd->when &= ~BSC_FD_WRITE;
175
176 /* get the next msg for this timeslot */
177 msg = e1inp_tx_ts(e1i_ts, &sign_link);
178 if (!msg) {
179 /* no message after tx delay timer */
180 return 0;
181 }
182
Harald Weltecc2241b2011-07-19 16:06:06 +0200183 DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Weltefd44a5f2011-08-21 00:48:54 +0200184 lapd_transmit(e1i_ts->lapd, sign_link->tei,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200185 sign_link->sapi, msg->data, msg->len);
186 msgb_free(msg);
187
188 /* set tx delay timer for next event */
189 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
190 e1i_ts->sign.tx_timer.data = e1i_ts;
191 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000);
192
193 return 0;
194}
195
196
197static int invertbits = 1;
198
199static uint8_t flip_table[256];
200
201static void init_flip_bits(void)
202{
203 int i,k;
204
205 for (i = 0 ; i < 256 ; i++) {
206 uint8_t sample = 0 ;
207 for (k = 0; k<8; k++) {
208 if ( i & 1 << k ) sample |= 0x80 >> k;
209 }
210 flip_table[i] = sample;
211 }
212}
213
214static uint8_t * flip_buf_bits ( uint8_t * buf , int len)
215{
216 int i;
217 uint8_t * start = buf;
218
219 for (i = 0 ; i < len; i++) {
220 buf[i] = flip_table[(uint8_t)buf[i]];
221 }
222
223 return start;
224}
225
226#define D_BCHAN_TX_GRAN 160
227/* write to a B channel TS */
228static int handle_tsX_write(struct osmo_fd *bfd)
229{
230 struct e1inp_line *line = bfd->data;
231 unsigned int ts_nr = bfd->priv_nr;
232 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
233 uint8_t tx_buf[D_BCHAN_TX_GRAN];
234 struct subch_mux *mx = &e1i_ts->trau.mux;
235 int ret;
236
237 ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN);
238
239 if (ret != D_BCHAN_TX_GRAN) {
240 fprintf(stderr, "Huh, got ret of %d\n", ret);
241 if (ret < 0)
242 return ret;
243 }
244
Harald Weltecc2241b2011-07-19 16:06:06 +0200245 DEBUGP(DLMIB, "BCHAN TX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200246 osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN));
247
248 if (invertbits) {
249 flip_buf_bits(tx_buf, ret);
250 }
251
252 ret = write(bfd->fd, tx_buf, ret);
253 if (ret < D_BCHAN_TX_GRAN)
254 fprintf(stderr, "send returns %d instead of %d\n", ret,
255 D_BCHAN_TX_GRAN);
256
257 return ret;
258}
259
260#define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN)
261/* FIXME: read from a B channel TS */
262static int handle_tsX_read(struct osmo_fd *bfd)
263{
264 struct e1inp_line *line = bfd->data;
265 unsigned int ts_nr = bfd->priv_nr;
266 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
267 struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx");
268 int ret;
269
270 if (!msg)
271 return -ENOMEM;
272
273 ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE);
274 if (ret < 0 || ret != D_TSX_ALLOC_SIZE) {
275 fprintf(stderr, "read error %d %s\n", ret, strerror(errno));
276 return ret;
277 }
278
279 if (invertbits) {
280 flip_buf_bits(msg->data, ret);
281 }
282
283 msgb_put(msg, ret);
284
285 msg->l2h = msg->data;
Harald Weltecc2241b2011-07-19 16:06:06 +0200286 DEBUGP(DLMIB, "BCHAN RX: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200287 osmo_hexdump(msgb_l2(msg), ret));
288 ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
289 /* physical layer indicates that data has been sent,
290 * we thus can send some more data */
291 ret = handle_tsX_write(bfd);
292 msgb_free(msg);
293
294 return ret;
295}
296
297/* callback from select.c in case one of the fd's can be read/written */
298static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what)
299{
300 struct e1inp_line *line = bfd->data;
301 unsigned int ts_nr = bfd->priv_nr;
302 unsigned int idx = ts_nr-1;
303 struct e1inp_ts *e1i_ts = &line->ts[idx];
304 int rc = 0;
305
306 switch (e1i_ts->type) {
307 case E1INP_TS_TYPE_SIGN:
308 if (what & BSC_FD_EXCEPT)
309 handle_dahdi_exception(e1i_ts);
310 if (what & BSC_FD_READ)
311 rc = handle_ts1_read(bfd);
312 if (what & BSC_FD_WRITE)
313 rc = handle_ts1_write(bfd);
314 break;
315 case E1INP_TS_TYPE_TRAU:
316 if (what & BSC_FD_EXCEPT)
317 handle_dahdi_exception(e1i_ts);
318 if (what & BSC_FD_READ)
319 rc = handle_tsX_read(bfd);
320 if (what & BSC_FD_WRITE)
321 rc = handle_tsX_write(bfd);
322 /* We never include the DAHDI B-Channel FD into the
323 * writeset, since it doesn't support poll() based
324 * write flow control */
325 break;
326 default:
327 fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type);
328 break;
329 }
330
331 return rc;
332}
333
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200334static int dahdi_e1_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200335
336struct e1inp_driver dahdi_driver = {
337 .name = "dahdi",
338 .want_write = ts_want_write,
339 .line_update = &dahdi_e1_line_update,
340};
341
342void dahdi_set_bufinfo(int fd, int as_sigchan)
343{
344 struct dahdi_bufferinfo bi;
345 int x = 0;
346
347 if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
348 fprintf(stderr, "Error getting bufinfo\n");
349 exit(-1);
350 }
351
352 if (as_sigchan) {
353 bi.numbufs = 4;
354 bi.bufsize = 512;
355 } else {
356 bi.numbufs = 8;
357 bi.bufsize = D_BCHAN_TX_GRAN;
358 bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
359 }
360
361 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
362 fprintf(stderr, "Error setting bufinfo\n");
363 exit(-1);
364 }
365
366 if (!as_sigchan) {
367 if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
368 fprintf(stderr, "Error setting bufinfo\n");
369 exit(-1);
370 }
371 } else {
372 int one = 1;
373 ioctl(fd, DAHDI_HDLCFCSMODE, &one);
374 /* we cannot reliably check for the ioctl return value here
375 * as this command will fail if the slot _already_ was a
376 * signalling slot before :( */
377 }
378}
379
380static int dahdi_e1_setup(struct e1inp_line *line)
381{
382 int ts, ret;
383
384 /* TS0 is CRC4, don't need any fd for it */
385 for (ts = 1; ts < NUM_E1_TS; ts++) {
386 unsigned int idx = ts-1;
387 char openstr[128];
388 struct e1inp_ts *e1i_ts = &line->ts[idx];
389 struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200390 int dev_nr;
391
392 /* DAHDI device names/numbers just keep incrementing
393 * even over multiple boards. So TS1 of the second
394 * board will be 32 */
395 dev_nr = line->num * (NUM_E1_TS-1) + ts;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200396
397 bfd->data = line;
398 bfd->priv_nr = ts;
399 bfd->cb = dahdi_fd_cb;
Harald Weltef3ca61c2011-08-09 11:21:23 +0200400 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200401
402 switch (e1i_ts->type) {
403 case E1INP_TS_TYPE_NONE:
404 continue;
405 break;
406 case E1INP_TS_TYPE_SIGN:
407 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
408 if (bfd->fd == -1) {
409 fprintf(stderr, "%s could not open %s %s\n",
410 __func__, openstr, strerror(errno));
411 exit(-1);
412 }
413 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
414 dahdi_set_bufinfo(bfd->fd, 1);
Harald Weltefd44a5f2011-08-21 00:48:54 +0200415 e1i_ts->lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200416 break;
417 case E1INP_TS_TYPE_TRAU:
418 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
419 if (bfd->fd == -1) {
420 fprintf(stderr, "%s could not open %s %s\n",
421 __func__, openstr, strerror(errno));
422 exit(-1);
423 }
424 dahdi_set_bufinfo(bfd->fd, 0);
425 /* We never include the DAHDI B-Channel FD into the
426 * writeset, since it doesn't support poll() based
427 * write flow control */
428 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
429 break;
430 }
431
432 if (bfd->fd < 0) {
433 fprintf(stderr, "%s could not open %s %s\n",
434 __func__, openstr, strerror(errno));
435 return bfd->fd;
436 }
437
438 ret = osmo_fd_register(bfd);
439 if (ret < 0) {
440 fprintf(stderr, "could not register FD: %s\n",
441 strerror(ret));
442 return ret;
443 }
444 }
445
446 return 0;
447}
448
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200449static int dahdi_e1_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200450{
451 if (line->driver != &dahdi_driver)
452 return -EINVAL;
453
454 return dahdi_e1_setup(line);
455}
456
457int e1inp_dahdi_init(void)
458{
459 init_flip_bits();
460
461 /* register the driver with the core */
462 return e1inp_driver_register(&dahdi_driver);
463}