blob: 3d337497bf169a503b10a2c505df142cc1a8a3f5 [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>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020043#include <talloc.h>
44
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
70 LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n",
71 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 */
79 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_ALARM, &isd);
80 break;
81 case DAHDI_EVENT_NOALARM:
82 /* alarm has gone, we should re-start the SABM requests */
83 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_NOALARM, &isd);
84 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
116 DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
117
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
124 DEBUGP(DMI, "prim %08x\n", prim);
125
126 switch (prim) {
127 case 0:
128 break;
129 case LAPD_MPH_ACTIVATE_IND:
130 DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
131 ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi);
132 break;
133 case LAPD_MPH_DEACTIVATE_IND:
134 DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei);
135 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;
143 DEBUGP(DMI, "RX: %s\n", osmo_hexdump(msgb_l2(msg), ret));
144 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
151 DEBUGP(DMI, "Returned ok\n");
152 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)
190 LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
191}
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
210 DEBUGP(DMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len));
211 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
272 DEBUGP(DMIB, "BCHAN TX: %s\n",
273 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;
313 DEBUGP(DMIB, "BCHAN RX: %s\n",
314 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;
418
419 bfd->data = line;
420 bfd->priv_nr = ts;
421 bfd->cb = dahdi_fd_cb;
422 snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts);
423
424 switch (e1i_ts->type) {
425 case E1INP_TS_TYPE_NONE:
426 continue;
427 break;
428 case E1INP_TS_TYPE_SIGN:
429 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
430 if (bfd->fd == -1) {
431 fprintf(stderr, "%s could not open %s %s\n",
432 __func__, openstr, strerror(errno));
433 exit(-1);
434 }
435 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;
436 dahdi_set_bufinfo(bfd->fd, 1);
437 e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd);
438 break;
439 case E1INP_TS_TYPE_TRAU:
440 bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
441 if (bfd->fd == -1) {
442 fprintf(stderr, "%s could not open %s %s\n",
443 __func__, openstr, strerror(errno));
444 exit(-1);
445 }
446 dahdi_set_bufinfo(bfd->fd, 0);
447 /* We never include the DAHDI B-Channel FD into the
448 * writeset, since it doesn't support poll() based
449 * write flow control */
450 bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE;
451 break;
452 }
453
454 if (bfd->fd < 0) {
455 fprintf(stderr, "%s could not open %s %s\n",
456 __func__, openstr, strerror(errno));
457 return bfd->fd;
458 }
459
460 ret = osmo_fd_register(bfd);
461 if (ret < 0) {
462 fprintf(stderr, "could not register FD: %s\n",
463 strerror(ret));
464 return ret;
465 }
466 }
467
468 return 0;
469}
470
Pablo Neira Ayusoc00ee732011-06-21 12:22:49 +0200471static int dahdi_e1_line_update(struct e1inp_line *line,
Pablo Neira Ayuso0b9ed9a2011-07-02 17:25:19 +0200472 enum e1inp_line_role role, const char *addr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200473{
474 if (line->driver != &dahdi_driver)
475 return -EINVAL;
476
477 return dahdi_e1_setup(line);
478}
479
480int e1inp_dahdi_init(void)
481{
482 init_flip_bits();
483
484 /* register the driver with the core */
485 return e1inp_driver_register(&dahdi_driver);
486}