Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for DAHDI */ |
| 2 | |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 3 | /* (C) 2008-2019 by Harald Welte <laforge@gnumonks.org> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 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 | * |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
Holger Hans Peter Freyther | d9d1b5c | 2014-09-25 18:47:12 +0200 | [diff] [blame] | 27 | #include "config.h" |
Harald Welte | ac290ee | 2020-08-02 10:43:42 +0200 | [diff] [blame] | 28 | #include "internal.h" |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <unistd.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | #include <time.h> |
| 36 | #include <sys/fcntl.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <arpa/inet.h> |
| 40 | #include <dahdi/user.h> |
| 41 | |
Harald Welte | fe05cf5 | 2011-09-26 23:18:41 +0200 | [diff] [blame] | 42 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 43 | #include <osmocom/core/select.h> |
| 44 | #include <osmocom/core/msgb.h> |
Pablo Neira Ayuso | 0b9ed9a | 2011-07-02 17:25:19 +0200 | [diff] [blame] | 45 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 46 | #include <osmocom/core/signal.h> |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 47 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | fe05cf5 | 2011-09-26 23:18:41 +0200 | [diff] [blame] | 48 | |
| 49 | #include <osmocom/vty/vty.h> |
| 50 | |
Pablo Neira Ayuso | 0b9ed9a | 2011-07-02 17:25:19 +0200 | [diff] [blame] | 51 | #include <osmocom/abis/subchan_demux.h> |
| 52 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 53 | |
Pablo Neira Ayuso | 355ce69 | 2011-07-05 14:53:37 +0200 | [diff] [blame] | 54 | #include <osmocom/abis/lapd.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 55 | |
| 56 | #define TS1_ALLOC_SIZE 300 |
| 57 | |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 58 | struct span_cfg { |
| 59 | struct llist_head list; |
| 60 | |
| 61 | unsigned int span_nr; |
| 62 | unsigned int chan_base; |
| 63 | unsigned int chan_num; |
| 64 | }; |
| 65 | |
| 66 | static struct span_cfg *span_cfgs[DAHDI_MAX_SPANS]; |
| 67 | |
| 68 | static int reread_span_cfgs(void) |
| 69 | { |
| 70 | struct dahdi_spaninfo si; |
| 71 | unsigned int basechan = 1; |
Harald Welte | 494f290 | 2011-09-26 23:06:49 +0200 | [diff] [blame] | 72 | int span_nr; |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 73 | int fd; |
| 74 | |
| 75 | if ((fd = open("/dev/dahdi/ctl", O_RDWR)) < 0) { |
| 76 | LOGP(DLMI, LOGL_ERROR, "Unable to open DAHDI ctl: %s\n", |
| 77 | strerror(errno)); |
| 78 | return -EIO; |
| 79 | } |
| 80 | |
Harald Welte | e2bd685 | 2011-09-26 23:03:12 +0200 | [diff] [blame] | 81 | for (span_nr = 1; span_nr < DAHDI_MAX_SPANS; span_nr++) { |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 82 | struct span_cfg *scfg; |
Harald Welte | e2bd685 | 2011-09-26 23:03:12 +0200 | [diff] [blame] | 83 | /* our array index starts at 0, but DAHDI span at 1 */ |
| 84 | int i = span_nr - 1; |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 85 | |
| 86 | /* clear any old cached information */ |
| 87 | if (span_cfgs[i]) { |
| 88 | talloc_free(span_cfgs[i]); |
| 89 | span_cfgs[i] = NULL; |
| 90 | } |
| 91 | |
| 92 | memset(&si, 0, sizeof(si)); |
Harald Welte | e2bd685 | 2011-09-26 23:03:12 +0200 | [diff] [blame] | 93 | si.spanno = span_nr; |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 94 | if (ioctl(fd, DAHDI_SPANSTAT, &si)) |
| 95 | continue; |
| 96 | |
| 97 | /* create and link new span_cfg */ |
| 98 | scfg = talloc_zero(NULL, struct span_cfg); |
| 99 | if (!scfg) { |
| 100 | close(fd); |
| 101 | return -ENOMEM; |
| 102 | } |
Harald Welte | e2bd685 | 2011-09-26 23:03:12 +0200 | [diff] [blame] | 103 | scfg->span_nr = span_nr; |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 104 | scfg->chan_num = si.totalchans; |
| 105 | scfg->chan_base = basechan; |
| 106 | span_cfgs[i] = scfg; |
| 107 | |
| 108 | basechan += si.totalchans; |
| 109 | } |
| 110 | |
| 111 | close(fd); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 116 | /* Corresponds to dahdi/user.h, only PRI related events */ |
| 117 | static const struct value_string dahdi_evt_names[] = { |
| 118 | { DAHDI_EVENT_NONE, "NONE" }, |
| 119 | { DAHDI_EVENT_ALARM, "ALARM" }, |
| 120 | { DAHDI_EVENT_NOALARM, "NOALARM" }, |
| 121 | { DAHDI_EVENT_ABORT, "HDLC ABORT" }, |
| 122 | { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" }, |
| 123 | { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" }, |
| 124 | { DAHDI_EVENT_REMOVED, "REMOVED" }, |
| 125 | { 0, NULL } |
| 126 | }; |
| 127 | |
| 128 | static void handle_dahdi_exception(struct e1inp_ts *ts) |
| 129 | { |
| 130 | int rc, evt; |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 131 | struct e1inp_line *line = ts->line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 132 | struct input_signal_data isd; |
| 133 | |
| 134 | rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt); |
| 135 | if (rc < 0) |
| 136 | return; |
| 137 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 138 | LOGPITS(ts, DLMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 139 | ts->line->num, ts->line->name, ts->num, |
| 140 | get_value_string(dahdi_evt_names, evt)); |
| 141 | |
| 142 | isd.line = ts->line; |
Harald Welte | f35d889 | 2016-10-16 15:33:52 +0200 | [diff] [blame] | 143 | isd.ts_nr = ts->num; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 144 | |
| 145 | switch (evt) { |
| 146 | case DAHDI_EVENT_ALARM: |
| 147 | /* we should notify the code that the line is gone */ |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 148 | osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd); |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 149 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 150 | break; |
| 151 | case DAHDI_EVENT_NOALARM: |
| 152 | /* alarm has gone, we should re-start the SABM requests */ |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 153 | osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_NOALARM, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 154 | break; |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 155 | case DAHDI_EVENT_ABORT: |
| 156 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]); |
| 157 | break; |
| 158 | case DAHDI_EVENT_OVERRUN: |
| 159 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]); |
| 160 | break; |
| 161 | case DAHDI_EVENT_BADFCS: |
| 162 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]); |
| 163 | break; |
| 164 | case DAHDI_EVENT_REMOVED: |
| 165 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]); |
| 166 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | static int handle_ts1_read(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 msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1"); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 176 | int ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 177 | |
| 178 | if (!msg) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16); |
| 182 | if (ret == -1) |
| 183 | handle_dahdi_exception(e1i_ts); |
| 184 | else if (ret < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 185 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Harald Welte | 510ae99 | 2020-01-12 12:17:33 +0100 | [diff] [blame] | 186 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 187 | } |
| 188 | msgb_put(msg, ret - 2); |
| 189 | if (ret <= 3) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 190 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Harald Welte | 510ae99 | 2020-01-12 12:17:33 +0100 | [diff] [blame] | 191 | return ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 194 | return e1inp_rx_ts_lapd(e1i_ts, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 198 | { |
| 199 | /* We never include the DAHDI B-Channel FD into the |
| 200 | * writeset, since it doesn't support poll() based |
| 201 | * write flow control */ |
| 202 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 203 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void timeout_ts1_write(void *data) |
| 213 | { |
| 214 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 215 | |
| 216 | /* trigger write of ts1, due to tx delay timer */ |
| 217 | ts_want_write(e1i_ts); |
| 218 | } |
| 219 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 220 | static void dahdi_write_msg(struct msgb *msg, void *cbdata) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 221 | { |
| 222 | struct osmo_fd *bfd = cbdata; |
| 223 | struct e1inp_line *line = bfd->data; |
| 224 | unsigned int ts_nr = bfd->priv_nr; |
| 225 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 226 | int ret; |
| 227 | |
Harald Welte | 96034fc | 2020-07-03 19:07:00 +0200 | [diff] [blame] | 228 | if (msgb_tailroom(msg) >= 2) { |
| 229 | /* two bytes of space for the FCS added by DAHDI in the kernel */ |
| 230 | msgb_put(msg, 2); |
| 231 | ret = write(bfd->fd, msg->data, msg->len); |
| 232 | } else { |
| 233 | /* work-around for code that sends us messages with no tailroom (OS#4644) */ |
| 234 | uint8_t buf[msg->len + 2]; |
| 235 | memcpy(buf, msg->data, msg->len); |
| 236 | ret = write(bfd->fd, buf, sizeof(buf)); |
| 237 | } |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 238 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 239 | if (ret == -1) |
| 240 | handle_dahdi_exception(e1i_ts); |
| 241 | else if (ret < 0) |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 242 | LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int handle_ts1_write(struct osmo_fd *bfd) |
| 246 | { |
| 247 | struct e1inp_line *line = bfd->data; |
| 248 | unsigned int ts_nr = bfd->priv_nr; |
| 249 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 250 | struct e1inp_sign_link *sign_link; |
| 251 | struct msgb *msg; |
| 252 | |
| 253 | bfd->when &= ~BSC_FD_WRITE; |
| 254 | |
| 255 | /* get the next msg for this timeslot */ |
| 256 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 257 | if (!msg) { |
| 258 | /* no message after tx delay timer */ |
| 259 | return 0; |
| 260 | } |
| 261 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 262 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 263 | lapd_transmit(e1i_ts->lapd, sign_link->tei, |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 264 | sign_link->sapi, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 265 | |
| 266 | /* set tx delay timer for next event */ |
Pablo Neira Ayuso | b26f2fd | 2017-06-07 18:32:13 +0200 | [diff] [blame] | 267 | osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts1_write, e1i_ts); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 268 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 273 | static void handle_hdlc_write(struct osmo_fd *bfd) |
| 274 | { |
| 275 | struct e1inp_line *line = bfd->data; |
| 276 | unsigned int ts_nr = bfd->priv_nr; |
| 277 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 278 | struct msgb *msg; |
| 279 | int ret; |
| 280 | |
| 281 | /* get the next msg for this timeslot */ |
| 282 | msg = e1inp_tx_ts(e1i_ts, NULL); |
| 283 | if (!msg) |
| 284 | return; |
| 285 | |
Harald Welte | 96034fc | 2020-07-03 19:07:00 +0200 | [diff] [blame] | 286 | if (msgb_tailroom(msg) >= 2) { |
| 287 | /* two bytes of space for the FCS added by DAHDI in the kernel */ |
| 288 | msgb_put(msg, 2); |
| 289 | ret = write(bfd->fd, msg->data, msg->len); |
| 290 | } else { |
| 291 | /* work-around for code that sends us messages with no tailroom (OS#4644) */ |
| 292 | uint8_t buf[msg->len + 2]; |
| 293 | memcpy(buf, msg->data, msg->len); |
| 294 | ret = write(bfd->fd, buf, sizeof(buf)); |
| 295 | } |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 296 | msgb_free(msg); |
| 297 | if (ret == -1) |
| 298 | handle_dahdi_exception(e1i_ts); |
| 299 | else if (ret < 0) |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 300 | LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int handle_hdlc_read(struct osmo_fd *bfd) |
| 304 | { |
| 305 | struct e1inp_line *line = bfd->data; |
| 306 | unsigned int ts_nr = bfd->priv_nr; |
| 307 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 308 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI HDLC Rx"); |
| 309 | int ret; |
| 310 | |
| 311 | if (!msg) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16); |
| 315 | if (ret == -1) |
| 316 | handle_dahdi_exception(e1i_ts); |
| 317 | else if (ret < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 318 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Harald Welte | 510ae99 | 2020-01-12 12:17:33 +0100 | [diff] [blame] | 319 | return ret; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 320 | } |
| 321 | msgb_put(msg, ret - 2); |
| 322 | if (ret <= 3) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 323 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Harald Welte | 510ae99 | 2020-01-12 12:17:33 +0100 | [diff] [blame] | 324 | return ret; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 328 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 329 | |
| 330 | static int invertbits = 1; |
| 331 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 332 | /* write to a B channel TS */ |
| 333 | static int handle_tsX_write(struct osmo_fd *bfd) |
| 334 | { |
| 335 | struct e1inp_line *line = bfd->data; |
| 336 | unsigned int ts_nr = bfd->priv_nr; |
| 337 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 338 | uint8_t tx_buf[D_BCHAN_TX_GRAN]; |
| 339 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 340 | int ret; |
| 341 | |
| 342 | ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN); |
| 343 | |
| 344 | if (ret != D_BCHAN_TX_GRAN) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 345 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 346 | if (ret < 0) |
| 347 | return ret; |
| 348 | } |
| 349 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 350 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN TX: %s\n", osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN)); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 351 | |
Harald Welte | 182957b | 2020-08-02 10:33:29 +0200 | [diff] [blame] | 352 | if (invertbits) |
| 353 | osmo_revbytebits_buf(tx_buf, ret); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 354 | |
| 355 | ret = write(bfd->fd, tx_buf, ret); |
| 356 | if (ret < D_BCHAN_TX_GRAN) |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 357 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", |
Pablo Neira Ayuso | 5c67fb5 | 2012-03-12 18:34:12 +0100 | [diff] [blame] | 358 | ret, D_BCHAN_TX_GRAN); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | #define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN) |
| 364 | /* FIXME: read from a B channel TS */ |
| 365 | static int handle_tsX_read(struct osmo_fd *bfd) |
| 366 | { |
| 367 | struct e1inp_line *line = bfd->data; |
| 368 | unsigned int ts_nr = bfd->priv_nr; |
| 369 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 370 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx"); |
| 371 | int ret; |
| 372 | |
| 373 | if (!msg) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 377 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 378 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno)); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 379 | return ret; |
| 380 | } |
| 381 | |
Harald Welte | 182957b | 2020-08-02 10:33:29 +0200 | [diff] [blame] | 382 | if (invertbits) |
| 383 | osmo_revbytebits_buf(msg->data, ret); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 384 | |
| 385 | msgb_put(msg, ret); |
| 386 | |
| 387 | msg->l2h = msg->data; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 388 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret)); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 389 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 390 | /* physical layer indicates that data has been sent, |
| 391 | * we thus can send some more data */ |
| 392 | ret = handle_tsX_write(bfd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 393 | |
| 394 | return ret; |
| 395 | } |
| 396 | |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 397 | /* write to a raw channel TS */ |
| 398 | static int handle_ts_raw_write(struct osmo_fd *bfd) |
| 399 | { |
| 400 | struct e1inp_line *line = bfd->data; |
| 401 | unsigned int ts_nr = bfd->priv_nr; |
| 402 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 403 | struct msgb *msg; |
| 404 | int ret; |
| 405 | |
| 406 | /* get the next msg for this timeslot */ |
| 407 | msg = e1inp_tx_ts(e1i_ts, NULL); |
| 408 | if (!msg) |
| 409 | return 0; |
| 410 | |
| 411 | if (msg->len != D_BCHAN_TX_GRAN) { |
| 412 | /* This might lead to a transmit underrun, as we call tx |
| 413 | * from the rx path, as there's no select/poll on dahdi |
| 414 | * */ |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 415 | LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unexpected msg->len = %u, " |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 416 | "expected %u\n", msg->len, D_BCHAN_TX_GRAN); |
| 417 | } |
| 418 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 419 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 420 | |
Harald Welte | 182957b | 2020-08-02 10:33:29 +0200 | [diff] [blame] | 421 | if (0/*invertbits*/) |
| 422 | osmo_revbytebits_buf(msg->data, msg->len); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 423 | |
| 424 | ret = write(bfd->fd, msg->data, msg->len); |
| 425 | if (ret < msg->len) |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 426 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", ret, msg->len); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 427 | msgb_free(msg); |
| 428 | |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | static int handle_ts_raw_read(struct osmo_fd *bfd) |
| 433 | { |
| 434 | struct e1inp_line *line = bfd->data; |
| 435 | unsigned int ts_nr = bfd->priv_nr; |
| 436 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 437 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI Raw TS"); |
| 438 | int ret; |
| 439 | |
| 440 | if (!msg) |
| 441 | return -ENOMEM; |
| 442 | |
| 443 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 444 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 445 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno)); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 446 | return ret; |
| 447 | } |
| 448 | |
Harald Welte | 182957b | 2020-08-02 10:33:29 +0200 | [diff] [blame] | 449 | if (0/*invertbits*/) |
| 450 | osmo_revbytebits_buf(msg->data, ret); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 451 | |
| 452 | msgb_put(msg, ret); |
| 453 | |
| 454 | msg->l2h = msg->data; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 455 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret)); |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 456 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 457 | /* physical layer indicates that data has been sent, |
| 458 | * we thus can send some more data */ |
| 459 | ret = handle_ts_raw_write(bfd); |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 464 | /* callback from select.c in case one of the fd's can be read/written */ |
| 465 | static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 466 | { |
| 467 | struct e1inp_line *line = bfd->data; |
| 468 | unsigned int ts_nr = bfd->priv_nr; |
| 469 | unsigned int idx = ts_nr-1; |
| 470 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 471 | int rc = 0; |
| 472 | |
| 473 | switch (e1i_ts->type) { |
| 474 | case E1INP_TS_TYPE_SIGN: |
| 475 | if (what & BSC_FD_EXCEPT) |
| 476 | handle_dahdi_exception(e1i_ts); |
| 477 | if (what & BSC_FD_READ) |
| 478 | rc = handle_ts1_read(bfd); |
| 479 | if (what & BSC_FD_WRITE) |
| 480 | rc = handle_ts1_write(bfd); |
| 481 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 482 | case E1INP_TS_TYPE_HDLC: |
| 483 | if (what & BSC_FD_EXCEPT) |
| 484 | handle_dahdi_exception(e1i_ts); |
| 485 | if (what & BSC_FD_READ) |
| 486 | rc = handle_hdlc_read(bfd); |
| 487 | if (what & BSC_FD_WRITE) |
| 488 | handle_hdlc_write(bfd); |
| 489 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 490 | case E1INP_TS_TYPE_TRAU: |
| 491 | if (what & BSC_FD_EXCEPT) |
| 492 | handle_dahdi_exception(e1i_ts); |
| 493 | if (what & BSC_FD_READ) |
| 494 | rc = handle_tsX_read(bfd); |
| 495 | if (what & BSC_FD_WRITE) |
| 496 | rc = handle_tsX_write(bfd); |
| 497 | /* We never include the DAHDI B-Channel FD into the |
| 498 | * writeset, since it doesn't support poll() based |
| 499 | * write flow control */ |
| 500 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 501 | case E1INP_TS_TYPE_RAW: |
| 502 | if (what & BSC_FD_EXCEPT) |
| 503 | handle_dahdi_exception(e1i_ts); |
| 504 | if (what & BSC_FD_READ) |
| 505 | rc = handle_ts_raw_read(bfd); |
| 506 | if (what & BSC_FD_WRITE) |
| 507 | rc = handle_ts_raw_write(bfd); |
| 508 | /* We never include the DAHDI B-Channel FD into the |
| 509 | * writeset, since it doesn't support poll() based |
| 510 | * write flow control */ |
| 511 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 512 | default: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 513 | LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown E1 TS type %u\n", e1i_ts->type); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | |
| 517 | return rc; |
| 518 | } |
| 519 | |
Harald Welte | fe05cf5 | 2011-09-26 23:18:41 +0200 | [diff] [blame] | 520 | static void dahdi_vty_show(struct vty *vty, struct e1inp_line *line) |
| 521 | { |
| 522 | struct span_cfg *scfg; |
| 523 | |
Holger Hans Peter Freyther | f69b812 | 2013-07-06 18:54:58 +0200 | [diff] [blame] | 524 | if (line->port_nr >= ARRAY_SIZE(span_cfgs)) |
Harald Welte | fe05cf5 | 2011-09-26 23:18:41 +0200 | [diff] [blame] | 525 | return; |
| 526 | |
| 527 | scfg = span_cfgs[line->port_nr]; |
| 528 | if (!scfg) { |
| 529 | vty_out(vty, "DAHDI Span %u non-existant%s", |
| 530 | line->port_nr+1, VTY_NEWLINE); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | vty_out(vty, "DAHDI Span #%u, Base Nr %u, Timeslots: %u%s", |
| 535 | line->port_nr+1, scfg->chan_base, scfg->chan_num, |
| 536 | VTY_NEWLINE); |
| 537 | } |
| 538 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 539 | static int dahdi_e1_line_update(struct e1inp_line *line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 540 | |
| 541 | struct e1inp_driver dahdi_driver = { |
| 542 | .name = "dahdi", |
| 543 | .want_write = ts_want_write, |
| 544 | .line_update = &dahdi_e1_line_update, |
Harald Welte | fe05cf5 | 2011-09-26 23:18:41 +0200 | [diff] [blame] | 545 | .vty_show = &dahdi_vty_show, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 546 | }; |
| 547 | |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 548 | int dahdi_set_bufinfo(int fd, int as_sigchan) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 549 | { |
| 550 | struct dahdi_bufferinfo bi; |
| 551 | int x = 0; |
| 552 | |
| 553 | if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) { |
Pablo Neira Ayuso | 5c67fb5 | 2012-03-12 18:34:12 +0100 | [diff] [blame] | 554 | LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n"); |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 555 | return -EIO; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (as_sigchan) { |
| 559 | bi.numbufs = 4; |
| 560 | bi.bufsize = 512; |
| 561 | } else { |
| 562 | bi.numbufs = 8; |
| 563 | bi.bufsize = D_BCHAN_TX_GRAN; |
| 564 | bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL; |
| 565 | } |
| 566 | |
| 567 | if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) { |
Pablo Neira Ayuso | 5c67fb5 | 2012-03-12 18:34:12 +0100 | [diff] [blame] | 568 | LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n"); |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 569 | return -EIO; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | if (!as_sigchan) { |
| 573 | if (ioctl(fd, DAHDI_AUDIOMODE, &x)) { |
Pablo Neira Ayuso | 5c67fb5 | 2012-03-12 18:34:12 +0100 | [diff] [blame] | 574 | LOGP(DLINP, LOGL_ERROR, "Error setting bufinfo\n"); |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 575 | return -EIO; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 576 | } |
| 577 | } else { |
| 578 | int one = 1; |
| 579 | ioctl(fd, DAHDI_HDLCFCSMODE, &one); |
| 580 | /* we cannot reliably check for the ioctl return value here |
| 581 | * as this command will fail if the slot _already_ was a |
| 582 | * signalling slot before :( */ |
| 583 | } |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 584 | return 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 587 | static int dahdi_open_slot(int dahdi_chan_nr) |
| 588 | { |
| 589 | int rc, fd; |
| 590 | #ifndef DAHDI_SPECIFY |
| 591 | char openstr[128]; |
| 592 | snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr); |
| 593 | #else |
| 594 | const char *openstr = "/dev/dahdi/channel"; |
| 595 | #endif |
| 596 | rc = open(openstr, O_RDWR | O_NONBLOCK); |
| 597 | if (rc < 0) { |
| 598 | LOGP(DLINP, LOGL_ERROR, "DAHDI: could not open %s %s\n", openstr, strerror(errno)); |
| 599 | return -EIO; |
| 600 | } |
| 601 | fd = rc; |
| 602 | #ifdef DAHDI_SPECIFY |
| 603 | rc = ioctl(fd, DAHDI_SPECIFY, &dahdi_chan_nr); |
| 604 | if (rc < 0) { |
| 605 | close(fd); |
| 606 | LOGP(DLINP, LOGL_ERROR, "DAHDI: could not DAHDI_SPECIFY %d: %s\n", |
| 607 | dahdi_chan_nr, strerror(errno)); |
| 608 | return -EIO; |
| 609 | } |
| 610 | #endif |
| 611 | return fd; |
| 612 | } |
| 613 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 614 | static int dahdi_e1_setup(struct e1inp_line *line) |
| 615 | { |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 616 | struct span_cfg *scfg; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 617 | int ts, ret; |
| 618 | |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 619 | reread_span_cfgs(); |
| 620 | |
| 621 | scfg = span_cfgs[line->port_nr]; |
Harald Welte | 0cf5514 | 2011-09-26 23:06:18 +0200 | [diff] [blame] | 622 | if (!scfg) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 623 | LOGPIL(line, DLMI, LOGL_ERROR, "Line %u(%s): DAHDI Port %u (Span %u) doesn't exist\n", |
| 624 | line->num, line->name, line->port_nr, line->port_nr+1); |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 625 | return -EIO; |
Harald Welte | 0cf5514 | 2011-09-26 23:06:18 +0200 | [diff] [blame] | 626 | } |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 627 | |
| 628 | line->num_ts = scfg->chan_num; |
| 629 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 630 | /* TS0 is CRC4, don't need any fd for it */ |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 631 | for (ts = 1; ts <= scfg->chan_num; ts++) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 632 | unsigned int idx = ts-1; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 633 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 634 | struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd; |
Harald Welte | f3ca61c | 2011-08-09 11:21:23 +0200 | [diff] [blame] | 635 | int dev_nr; |
| 636 | |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 637 | /* unregister FD if it was already registered */ |
| 638 | if (bfd->list.next && bfd->list.next != LLIST_POISON1) |
| 639 | osmo_fd_unregister(bfd); |
| 640 | |
Harald Welte | f3ca61c | 2011-08-09 11:21:23 +0200 | [diff] [blame] | 641 | /* DAHDI device names/numbers just keep incrementing |
| 642 | * even over multiple boards. So TS1 of the second |
| 643 | * board will be 32 */ |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 644 | dev_nr = scfg->chan_base + idx; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 645 | |
| 646 | bfd->data = line; |
| 647 | bfd->priv_nr = ts; |
| 648 | bfd->cb = dahdi_fd_cb; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 649 | |
| 650 | switch (e1i_ts->type) { |
| 651 | case E1INP_TS_TYPE_NONE: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 652 | /* close/release LAPD instance, if any */ |
| 653 | if (e1i_ts->lapd) { |
| 654 | lapd_instance_free(e1i_ts->lapd); |
| 655 | e1i_ts->lapd = NULL; |
| 656 | } |
| 657 | if (bfd->fd) { |
| 658 | close(bfd->fd); |
| 659 | bfd->fd = 0; |
| 660 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 661 | continue; |
| 662 | break; |
| 663 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 664 | if (!bfd->fd) |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 665 | bfd->fd = dahdi_open_slot(dev_nr); |
| 666 | if (bfd->fd < 0) |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 667 | return -EIO; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 668 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 669 | ret = dahdi_set_bufinfo(bfd->fd, 1); |
| 670 | if (ret < 0) |
| 671 | return ret; |
| 672 | |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 673 | if (!e1i_ts->lapd) { |
| 674 | char name[32]; |
| 675 | e1inp_ts_name(name, sizeof(name), e1i_ts); |
| 676 | e1i_ts->lapd = lapd_instance_alloc2(1, |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 677 | dahdi_write_msg, bfd, e1inp_dlsap_up, |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 678 | e1i_ts, &lapd_profile_abis, name); |
| 679 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 680 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 681 | case E1INP_TS_TYPE_HDLC: |
| 682 | if (!bfd->fd) |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 683 | bfd->fd = dahdi_open_slot(dev_nr); |
| 684 | if (bfd->fd < 0) |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 685 | return -EIO; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 686 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
| 687 | ret = dahdi_set_bufinfo(bfd->fd, 1); |
| 688 | if (ret < 0) |
| 689 | return ret; |
| 690 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 691 | case E1INP_TS_TYPE_TRAU: |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 692 | case E1INP_TS_TYPE_RAW: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 693 | /* close/release LAPD instance, if any */ |
| 694 | if (e1i_ts->lapd) { |
| 695 | lapd_instance_free(e1i_ts->lapd); |
| 696 | e1i_ts->lapd = NULL; |
| 697 | } |
| 698 | if (!bfd->fd) |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 699 | bfd->fd = dahdi_open_slot(dev_nr); |
| 700 | if (bfd->fd < 0) |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 701 | return -EIO; |
Pablo Neira Ayuso | 7ed9258 | 2012-03-12 18:38:45 +0100 | [diff] [blame] | 702 | ret = dahdi_set_bufinfo(bfd->fd, 0); |
| 703 | if (ret < 0) |
| 704 | return -EIO; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 705 | /* We never include the DAHDI B-Channel FD into the |
| 706 | * writeset, since it doesn't support poll() based |
| 707 | * write flow control */ |
| 708 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE; |
| 709 | break; |
| 710 | } |
| 711 | |
Harald Welte | 7c1c8cc | 2019-11-06 21:50:20 +0100 | [diff] [blame] | 712 | if (bfd->fd < 0) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 713 | return bfd->fd; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 714 | |
| 715 | ret = osmo_fd_register(bfd); |
| 716 | if (ret < 0) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 717 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret)); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 718 | return ret; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 725 | static int dahdi_e1_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 726 | { |
| 727 | if (line->driver != &dahdi_driver) |
| 728 | return -EINVAL; |
| 729 | |
| 730 | return dahdi_e1_setup(line); |
| 731 | } |
| 732 | |
| 733 | int e1inp_dahdi_init(void) |
| 734 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 735 | /* register the driver with the core */ |
| 736 | return e1inp_driver_register(&dahdi_driver); |
| 737 | } |