blob: 22f38d80f170e3a5cea7a48bc40e260923469ceb [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis interface to E1 */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "internal.h"
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <time.h>
30#include <sys/fcntl.h>
31#include <sys/socket.h>
32#include <sys/ioctl.h>
33#include <arpa/inet.h>
34#include <mISDNif.h>
35
36//#define AF_COMPATIBILITY_FUNC
37//#include <compat_af_isdn.h>
38#ifndef AF_ISDN
39#define AF_ISDN 34
40#define PF_ISDN AF_ISDN
41#endif
42
Harald Weltef2737fc2011-08-16 14:30:10 +020043#include <osmocom/core/linuxlist.h>
44#include <osmocom/core/talloc.h>
45#include <osmocom/core/rate_ctr.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020046#include <osmocom/core/logging.h>
47#include <osmocom/core/signal.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020048#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020049
50#define NUM_E1_TS 32
51
52static void *tall_e1inp_ctx;
53
54/* list of all E1 drivers */
55LLIST_HEAD(e1inp_driver_list);
56
57/* list of all E1 lines */
58LLIST_HEAD(e1inp_line_list);
59
60static void *tall_sigl_ctx;
61
Harald Weltef2737fc2011-08-16 14:30:10 +020062static const struct rate_ctr_desc e1inp_ctr_d[] = {
63 [E1I_CTR_HDLC_ABORT] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020064 "hdlc.abort", "HDLC abort"
Harald Weltef2737fc2011-08-16 14:30:10 +020065 },
66 [E1I_CTR_HDLC_BADFCS] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020067 "hdlc.bad_fcs", "HLDC Bad FCS"
Harald Weltef2737fc2011-08-16 14:30:10 +020068 },
69 [E1I_CTR_HDLC_OVERR] = {
70 "hdlc.overrun", "HDLC Overrun"
71 },
72 [E1I_CTR_ALARM] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020073 "alarm", "Alarm"
Harald Weltef2737fc2011-08-16 14:30:10 +020074 },
75 [E1I_CTR_REMOVED] = {
Harald Weltedd0c2ef2011-08-11 12:54:07 +020076 "removed", "Line removed"
Harald Weltef2737fc2011-08-16 14:30:10 +020077 },
78};
79
80static const struct rate_ctr_group_desc e1inp_ctr_g_d = {
81 .group_name_prefix = "e1inp",
82 .group_description = "E1 Input subsystem",
83 .num_ctr = ARRAY_SIZE(e1inp_ctr_d),
84 .ctr_desc = e1inp_ctr_d,
85};
86
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020087/*
88 * pcap writing of the misdn load
89 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
90 */
91#define DLT_LINUX_LAPD 177
92#define PCAP_INPUT 0
93#define PCAP_OUTPUT 1
94
95struct pcap_hdr {
96 uint32_t magic_number;
97 uint16_t version_major;
98 uint16_t version_minor;
99 int32_t thiszone;
100 uint32_t sigfigs;
101 uint32_t snaplen;
102 uint32_t network;
103} __attribute__((packed));
104
105struct pcaprec_hdr {
106 uint32_t ts_sec;
107 uint32_t ts_usec;
108 uint32_t incl_len;
109 uint32_t orig_len;
110} __attribute__((packed));
111
112struct fake_linux_lapd_header {
113 uint16_t pkttype;
114 uint16_t hatype;
115 uint16_t halen;
116 uint64_t addr;
117 int16_t protocol;
118} __attribute__((packed));
119
120struct lapd_header {
121 uint8_t ea1 : 1;
122 uint8_t cr : 1;
123 uint8_t sapi : 6;
124 uint8_t ea2 : 1;
125 uint8_t tei : 7;
126 uint8_t control_foo; /* fake UM's ... */
127} __attribute__((packed));
128
129osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
130osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
131osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
132osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
133osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
134
135
136static int pcap_fd = -1;
137
138void e1_set_pcap_fd(int fd)
139{
140 int ret;
141 struct pcap_hdr header = {
142 .magic_number = 0xa1b2c3d4,
143 .version_major = 2,
144 .version_minor = 4,
145 .thiszone = 0,
146 .sigfigs = 0,
147 .snaplen = 65535,
148 .network = DLT_LINUX_LAPD,
149 };
150
151 pcap_fd = fd;
152 ret = write(pcap_fd, &header, sizeof(header));
153}
154
155/* This currently only works for the D-Channel */
156static void write_pcap_packet(int direction, int sapi, int tei,
157 struct msgb *msg) {
158 if (pcap_fd < 0)
159 return;
160
161 int ret;
162 time_t cur_time;
163 struct tm *tm;
164
165 struct fake_linux_lapd_header header = {
166 .pkttype = 4,
167 .hatype = 0,
168 .halen = 0,
169 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
170 .protocol = ntohs(48),
171 };
172
173 struct lapd_header lapd_header = {
174 .ea1 = 0,
175 .cr = direction == PCAP_OUTPUT ? 1 : 0,
176 .sapi = sapi & 0x3F,
177 .ea2 = 1,
178 .tei = tei & 0x7F,
179 .control_foo = 0x03 /* UI */,
180 };
181
182 struct pcaprec_hdr payload_header = {
183 .ts_sec = 0,
184 .ts_usec = 0,
185 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
186 + sizeof(struct lapd_header),
187 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
188 + sizeof(struct lapd_header),
189 };
190
191
192 cur_time = time(NULL);
193 tm = localtime(&cur_time);
194 payload_header.ts_sec = mktime(tm);
195
196 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
197 ret = write(pcap_fd, &header, sizeof(header));
198 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
199 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
200}
201
202static const char *sign_types[] = {
203 [E1INP_SIGN_NONE] = "None",
204 [E1INP_SIGN_OML] = "OML",
205 [E1INP_SIGN_RSL] = "RSL",
206};
207const char *e1inp_signtype_name(enum e1inp_sign_type tp)
208{
209 if (tp >= ARRAY_SIZE(sign_types))
210 return "undefined";
211 return sign_types[tp];
212}
213
214static const char *ts_types[] = {
215 [E1INP_TS_TYPE_NONE] = "None",
216 [E1INP_TS_TYPE_SIGN] = "Signalling",
217 [E1INP_TS_TYPE_TRAU] = "TRAU",
218};
219
220const char *e1inp_tstype_name(enum e1inp_ts_type tp)
221{
222 if (tp >= ARRAY_SIZE(ts_types))
223 return "undefined";
224 return ts_types[tp];
225}
226
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200227int abis_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200228{
229 struct e1inp_sign_link *sign_link = msg->dst;
230 struct e1inp_driver *e1inp_driver;
231 struct e1inp_ts *e1i_ts;
232;
233 msg->l2h = msg->data;
234
235 /* don't know how to route this message. */
236 if (sign_link == NULL) {
Harald Welte40b0e8c2011-07-21 16:57:34 +0200237 LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200238 osmo_hexdump(msg->data, msg->len));
239 talloc_free(msg);
240 return -EINVAL;
241 }
242 e1i_ts = sign_link->ts;
243 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
244 /* notify the driver we have something to write */
245 e1inp_driver = sign_link->ts->line->driver;
246 e1inp_driver->want_write(e1i_ts);
247 }
248 msgb_enqueue(&sign_link->tx_list, msg);
249
250 /* dump it */
251 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
252
253 return 0;
254}
255
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200256int abis_rsl_sendmsg(struct msgb *msg)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200257{
Pablo Neira Ayuso96e72632011-06-26 19:08:05 +0200258 return abis_sendmsg(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200259}
260
261/* Timeslot */
262int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line,
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200263 int (*trau_rcv_cb)(struct subch_demux *dmx, int ch,
264 uint8_t *data, int len, void *_priv))
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200265{
266 if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line)
267 return 0;
268
269 ts->type = E1INP_TS_TYPE_TRAU;
270 ts->line = line;
271
272 subchan_mux_init(&ts->trau.mux);
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200273 ts->trau.demux.out_cb = trau_rcv_cb;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200274 ts->trau.demux.data = ts;
275 subch_demux_init(&ts->trau.demux);
276 return 0;
277}
278
279int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line)
280{
281 if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line)
282 return 0;
283
284 ts->type = E1INP_TS_TYPE_SIGN;
285 ts->line = line;
286
287 if (line && line->driver)
288 ts->sign.delay = line->driver->default_delay;
289 else
290 ts->sign.delay = 100000;
291 INIT_LLIST_HEAD(&ts->sign.sign_links);
292 return 0;
293}
294
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200295struct e1inp_line *e1inp_line_find(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200296{
297 struct e1inp_line *e1i_line;
298
299 /* iterate over global list of e1 lines */
300 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
301 if (e1i_line->num == e1_nr)
302 return e1i_line;
303 }
304 return NULL;
305}
306
307struct e1inp_line *
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200308e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200309{
310 struct e1inp_driver *driver;
311 struct e1inp_line *line;
312 int i;
313
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200314 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200315 if (line) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200316 LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200317 e1_nr);
318 return NULL;
319 }
320
321 driver = e1inp_driver_find(driver_name);
322 if (!driver) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200323 LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200324 driver_name);
325 return NULL;
326 }
327
328 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
329 if (!line)
330 return NULL;
331
332 line->driver = driver;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200333 line->num = e1_nr;
Harald Weltef2737fc2011-08-16 14:30:10 +0200334
335 line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num);
336
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200337 for (i = 0; i < NUM_E1_TS; i++) {
338 line->ts[i].num = i+1;
339 line->ts[i].line = line;
340 }
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200341 line->refcnt++;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200342 llist_add_tail(&line->list, &e1inp_line_list);
343
344 return line;
345}
346
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200347struct e1inp_line *
348e1inp_line_clone(void *ctx, struct e1inp_line *line)
349{
350 struct e1inp_line *clone;
351
352 /* clone virtual E1 line for this new OML link. */
353 clone = talloc_zero(ctx, struct e1inp_line);
354 if (clone == NULL)
355 return NULL;
356
357 memcpy(clone, line, sizeof(struct e1inp_line));
358 clone->refcnt = 1;
359 return clone;
360}
361
362void e1inp_line_get(struct e1inp_line *line)
363{
364 line->refcnt++;
365}
366
367void e1inp_line_put(struct e1inp_line *line)
368{
369 line->refcnt--;
370 if (line->refcnt == 0)
371 talloc_free(line);
372}
373
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200374void
375e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops)
376{
377 line->ops = ops;
378}
379
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200380#if 0
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200381struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200382{
383 struct e1inp_line *line;
384 int i;
385
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200386 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200387 if (line)
388 return line;
389
390 line = talloc_zero(tall_e1inp_ctx, struct e1inp_line);
391 if (!line)
392 return NULL;
393
394 line->num = e1_nr;
395 for (i = 0; i < NUM_E1_TS; i++) {
396 line->ts[i].num = i+1;
397 line->ts[i].line = line;
398 }
399 llist_add_tail(&line->list, &e1inp_line_list);
400
401 return line;
402}
403#endif
404
405static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
406{
407 struct e1inp_line *e1i_line;
408
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200409 e1i_line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200410 if (!e1i_line)
411 return NULL;
412
413 return &e1i_line->ts[ts_nr-1];
414}
415
416struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
417{
418 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
419
420 if (!e1i_ts)
421 return NULL;
422
423 return &e1i_ts->trau.mux;
424}
425
426/* Signalling Link */
427
428struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
429 uint8_t tei, uint8_t sapi)
430{
431 struct e1inp_sign_link *link;
432
433 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
434 if (link->sapi == sapi && link->tei == tei)
435 return link;
436 }
437
438 return NULL;
439}
440
441/* create a new signalling link in a E1 timeslot */
442
443struct e1inp_sign_link *
444e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
445 struct gsm_bts_trx *trx, uint8_t tei,
446 uint8_t sapi)
447{
448 struct e1inp_sign_link *link;
449
450 if (ts->type != E1INP_TS_TYPE_SIGN)
451 return NULL;
452
453 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
454 if (!link)
455 return NULL;
456
457 link->ts = ts;
458 link->type = type;
459 INIT_LLIST_HEAD(&link->tx_list);
460 link->trx = trx;
461 link->tei = tei;
462 link->sapi = sapi;
463
464 llist_add_tail(&link->list, &ts->sign.sign_links);
465
466 return link;
467}
468
469void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
470{
471 struct msgb *msg;
472
473 llist_del(&link->list);
474 while (!llist_empty(&link->tx_list)) {
475 msg = msgb_dequeue(&link->tx_list);
476 msgb_free(msg);
477 }
478
479 if (link->ts->type == E1INP_TS_TYPE_SIGN)
480 osmo_timer_del(&link->ts->sign.tx_timer);
481
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200482 if (link->ts->line->driver->close)
Pablo Neira Ayusoadd3ec82011-07-05 14:45:46 +0200483 link->ts->line->driver->close(link);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200484
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200485 talloc_free(link);
486}
487
488/* XXX */
489/* the E1 driver tells us he has received something on a TS */
490int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
491 uint8_t tei, uint8_t sapi)
492{
493 struct e1inp_sign_link *link;
494 int ret = 0;
495
496 switch (ts->type) {
497 case E1INP_TS_TYPE_SIGN:
498 /* consult the list of signalling links */
499 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
500 link = e1inp_lookup_sign_link(ts, tei, sapi);
501 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200502 LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for "
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200503 "tei %d, sapi %d\n", tei, sapi);
504 return -EINVAL;
505 }
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200506 if (!ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200507 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200508 "no action set for signalling messages.\n");
509 return -ENOENT;
510 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200511 msg->dst = link;
512 ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200513 break;
514 case E1INP_TS_TYPE_TRAU:
Pablo Neira Ayuso211d2ca2011-06-07 17:15:10 +0200515 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200516 break;
517 default:
518 ret = -EINVAL;
Harald Weltecc2241b2011-07-19 16:06:06 +0200519 LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200520 break;
521 }
522
523 return ret;
524}
525
526#define TSX_ALLOC_SIZE 4096
527
528/* called by driver if it wants to transmit on a given TS */
529struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
530 struct e1inp_sign_link **sign_link)
531{
532 struct e1inp_sign_link *link;
533 struct msgb *msg = NULL;
534 int len;
535
536 switch (e1i_ts->type) {
537 case E1INP_TS_TYPE_SIGN:
538 /* FIXME: implement this round robin */
539 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
540 msg = msgb_dequeue(&link->tx_list);
541 if (msg) {
542 if (sign_link)
543 *sign_link = link;
544 break;
545 }
546 }
547 break;
548 case E1INP_TS_TYPE_TRAU:
549 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
550 if (!msg)
551 return NULL;
552 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
553 msgb_put(msg, 40);
554 break;
555 default:
Harald Weltecc2241b2011-07-19 16:06:06 +0200556 LOGP(DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200557 return NULL;
558 }
559 return msg;
560}
561
562/* called by driver in case some kind of link state event */
563int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
564{
565 struct e1inp_sign_link *link;
566 struct input_signal_data isd;
567
568 link = e1inp_lookup_sign_link(ts, tei, sapi);
569 if (!link)
570 return -EINVAL;
571
Pablo Neira Ayuso31fe5f22011-08-09 23:15:38 +0200572 isd.line = ts->line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200573 isd.link_type = link->type;
574 isd.trx = link->trx;
575 isd.tei = tei;
576 isd.sapi = sapi;
577
578 /* report further upwards */
Harald Weltecc2241b2011-07-19 16:06:06 +0200579 osmo_signal_dispatch(SS_L_INPUT, evt, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200580 return 0;
581}
582
583/* register a driver with the E1 core */
584int e1inp_driver_register(struct e1inp_driver *drv)
585{
586 llist_add_tail(&drv->list, &e1inp_driver_list);
587 return 0;
588}
589
590struct e1inp_driver *e1inp_driver_find(const char *name)
591{
592 struct e1inp_driver *drv;
593
594 llist_for_each_entry(drv, &e1inp_driver_list, list) {
595 if (!strcasecmp(name, drv->name))
596 return drv;
597 }
598 return NULL;
599}
600
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200601int e1inp_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200602{
603 struct input_signal_data isd;
604 int rc;
605
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200606 e1inp_line_get(line);
607
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200608 /* This line has been already initialized, skip this. */
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +0200609 if (line->refcnt > 2)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200610 return 0;
611
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200612 if (line->driver && line->ops && line->driver->line_update) {
613 rc = line->driver->line_update(line, line->ops->role,
614 line->ops->addr);
615 } else
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200616 rc = 0;
617
618 /* Send a signal to anyone who is interested in new lines being
619 * configured */
620 memset(&isd, 0, sizeof(isd));
621 isd.line = line;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200622 osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200623
624 return rc;
625}
626
Pablo Neira Ayusoa20762a2011-07-02 19:01:58 +0200627static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200628 void *handler_data, void *signal_data)
629{
Harald Weltecc2241b2011-07-19 16:06:06 +0200630 if (subsys != SS_L_GLOBAL ||
631 signal != S_L_GLOBAL_SHUTDOWN)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200632 return 0;
633
634 if (pcap_fd) {
635 close(pcap_fd);
636 pcap_fd = -1;
637 }
638
639 return 0;
640}
641
642void e1inp_misdn_init(void);
643void e1inp_dahdi_init(void);
644void e1inp_ipaccess_init(void);
645void e1inp_hsl_init(void);
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200646void e1inp_rs232_init(void);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200647
648void e1inp_init(void)
649{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200650 tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200651 tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1,
652 "e1inp_sign_link");
Harald Weltecc2241b2011-07-19 16:06:06 +0200653 osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200654
655 e1inp_misdn_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200656 e1inp_dahdi_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200657 e1inp_ipaccess_init();
658 e1inp_hsl_init();
Pablo Neira Ayuso7e0d0062011-08-19 11:36:15 +0200659 e1inp_rs232_init();
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200660}