Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for osmo-e1d */ |
| 2 | |
| 3 | /* (C) 2019 by Sylvain Munaut <tnt@246tNt.com> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 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 | |
| 25 | #include "config.h" |
Harald Welte | ac290ee | 2020-08-02 10:43:42 +0200 | [diff] [blame] | 26 | #include "internal.h" |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 27 | |
| 28 | #ifdef HAVE_E1D |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include <osmocom/core/bits.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | |
| 37 | #include <osmocom/vty/vty.h> |
| 38 | |
| 39 | #include <osmocom/abis/subchan_demux.h> |
| 40 | #include <osmocom/abis/e1_input.h> |
| 41 | #include <osmocom/abis/lapd.h> |
| 42 | |
| 43 | #include <osmocom/e1d/proto.h> |
| 44 | #include <osmocom/e1d/proto_clnt.h> |
| 45 | |
| 46 | |
| 47 | #define TS_SIGN_ALLOC_SIZE 300 |
| 48 | |
| 49 | struct osmo_e1dp_client *g_e1d; |
| 50 | |
Harald Welte | f776669 | 2020-06-30 19:04:07 +0200 | [diff] [blame] | 51 | static int invertbits = 1; |
| 52 | |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 53 | /* pre-declaration */ |
| 54 | extern struct e1inp_driver e1d_driver; |
| 55 | static int e1d_want_write(struct e1inp_ts *e1i_ts); |
| 56 | |
| 57 | |
| 58 | static int |
| 59 | handle_ts_sign_read(struct osmo_fd *bfd) |
| 60 | { |
| 61 | struct e1inp_line *line = bfd->data; |
| 62 | unsigned int ts_nr = bfd->priv_nr; |
| 63 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 64 | struct msgb *msg = msgb_alloc(TS_SIGN_ALLOC_SIZE, "E1D Signaling TS"); |
| 65 | int ret; |
| 66 | |
| 67 | if (!msg) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | ret = read(bfd->fd, msg->data, TS_SIGN_ALLOC_SIZE - 16); |
| 71 | if (ret < 0) { |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 72 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | msgb_put(msg, ret); |
| 77 | if (ret <= 1) { |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 78 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno)); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | return e1inp_rx_ts_lapd(e1i_ts, msg); |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | timeout_ts_sign_write(void *data) |
| 87 | { |
| 88 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 89 | |
| 90 | /* trigger write of ts1, due to tx delay timer */ |
| 91 | e1d_want_write(e1i_ts); |
| 92 | } |
| 93 | |
| 94 | static int |
| 95 | handle_ts_sign_write(struct osmo_fd *bfd) |
| 96 | { |
| 97 | struct e1inp_line *line = bfd->data; |
| 98 | unsigned int ts_nr = bfd->priv_nr; |
| 99 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 100 | struct e1inp_sign_link *sign_link; |
| 101 | struct msgb *msg; |
| 102 | |
| 103 | bfd->when &= ~BSC_FD_WRITE; |
| 104 | |
| 105 | /* get the next msg for this timeslot */ |
| 106 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 107 | if (!msg) { |
| 108 | /* no message after tx delay timer */ |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
| 113 | lapd_transmit(e1i_ts->lapd, sign_link->tei, |
| 114 | sign_link->sapi, msg); |
| 115 | |
| 116 | /* set tx delay timer for next event */ |
| 117 | osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts_sign_write, e1i_ts); |
| 118 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Harald Welte | f776669 | 2020-06-30 19:04:07 +0200 | [diff] [blame] | 123 | #define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN) |
| 124 | |
| 125 | static int |
| 126 | handle_ts_trau_write(struct osmo_fd *bfd) |
| 127 | { |
| 128 | struct e1inp_line *line = bfd->data; |
| 129 | unsigned int ts_nr = bfd->priv_nr; |
| 130 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 131 | uint8_t tx_buf[D_BCHAN_TX_GRAN]; |
| 132 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 133 | int ret; |
| 134 | |
| 135 | ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN); |
| 136 | |
| 137 | if (ret != D_BCHAN_TX_GRAN) { |
| 138 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Huh, got ret of %d\n", ret); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN TX: %s\n", osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN)); |
| 144 | |
| 145 | if (invertbits) |
| 146 | osmo_revbytebits_buf(tx_buf, ret); |
| 147 | |
| 148 | ret = write(bfd->fd, tx_buf, ret); |
| 149 | if (ret < D_BCHAN_TX_GRAN) |
| 150 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", |
| 151 | ret, D_BCHAN_TX_GRAN); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static int |
| 158 | handle_ts_trau_read(struct osmo_fd *bfd) |
| 159 | { |
| 160 | struct e1inp_line *line = bfd->data; |
| 161 | unsigned int ts_nr = bfd->priv_nr; |
| 162 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 163 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "E1D Rx TSx"); |
| 164 | int ret; |
| 165 | |
| 166 | if (!msg) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 170 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
| 171 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno)); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | if (invertbits) |
| 176 | osmo_revbytebits_buf(msg->data, ret); |
| 177 | |
| 178 | msgb_put(msg, ret); |
| 179 | |
| 180 | msg->l2h = msg->data; |
| 181 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret)); |
| 182 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 183 | /* physical layer indicates that data has been sent, |
| 184 | * we thus can send some more data */ |
| 185 | ret = handle_ts_trau_write(bfd); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Harald Welte | ca71296 | 2020-06-30 19:12:42 +0200 | [diff] [blame] | 190 | /* write to a raw channel TS */ |
| 191 | static int handle_ts_raw_write(struct osmo_fd *bfd) |
| 192 | { |
| 193 | struct e1inp_line *line = bfd->data; |
| 194 | unsigned int ts_nr = bfd->priv_nr; |
| 195 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 196 | struct msgb *msg; |
| 197 | int ret; |
| 198 | |
| 199 | /* get the next msg for this timeslot */ |
| 200 | msg = e1inp_tx_ts(e1i_ts, NULL); |
| 201 | if (!msg) |
| 202 | return 0; |
| 203 | |
| 204 | if (msg->len != D_BCHAN_TX_GRAN) { |
| 205 | /* This might lead to a transmit underrun, as we call tx |
| 206 | * from the rx path, as there's no select/poll on dahdi |
| 207 | * */ |
| 208 | LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unexpected msg->len = %u, " |
| 209 | "expected %u\n", msg->len, D_BCHAN_TX_GRAN); |
| 210 | } |
| 211 | |
| 212 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
| 213 | |
| 214 | if (0/*invertbits*/) |
| 215 | osmo_revbytebits_buf(msg->data, msg->len); |
| 216 | |
| 217 | ret = write(bfd->fd, msg->data, msg->len); |
| 218 | if (ret < msg->len) |
| 219 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", ret, msg->len); |
| 220 | msgb_free(msg); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | static int handle_ts_raw_read(struct osmo_fd *bfd) |
| 226 | { |
| 227 | struct e1inp_line *line = bfd->data; |
| 228 | unsigned int ts_nr = bfd->priv_nr; |
| 229 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 230 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "E1D Raw TS"); |
| 231 | int ret; |
| 232 | |
| 233 | if (!msg) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 237 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
| 238 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "read error %d %s\n", ret, strerror(errno)); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | if (0/*invertbits*/) |
| 243 | osmo_revbytebits_buf(msg->data, ret); |
| 244 | |
| 245 | msgb_put(msg, ret); |
| 246 | |
| 247 | msg->l2h = msg->data; |
| 248 | LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), ret)); |
| 249 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 250 | /* physical layer indicates that data has been sent, |
| 251 | * we thus can send some more data */ |
| 252 | ret = handle_ts_raw_write(bfd); |
| 253 | |
| 254 | return ret; |
| 255 | } |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 256 | |
| 257 | static void |
| 258 | e1d_write_msg(struct msgb *msg, void *cbdata) |
| 259 | { |
| 260 | struct osmo_fd *bfd = cbdata; |
| 261 | struct e1inp_line *line = bfd->data; |
| 262 | unsigned int ts_nr = bfd->priv_nr; |
| 263 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 264 | int ret; |
| 265 | |
| 266 | ret = write(bfd->fd, msg->data, msg->len); |
| 267 | msgb_free(msg); |
| 268 | if (ret < 0) |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 269 | LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int |
| 273 | e1d_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 274 | { |
| 275 | struct e1inp_line *line = bfd->data; |
| 276 | unsigned int ts_nr = bfd->priv_nr; |
| 277 | unsigned int idx = ts_nr-1; |
| 278 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 279 | int ret = 0; |
| 280 | |
| 281 | switch (e1i_ts->type) { |
| 282 | case E1INP_TS_TYPE_SIGN: |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 283 | if (what & BSC_FD_READ) |
| 284 | ret = handle_ts_sign_read(bfd); |
| 285 | if (what & BSC_FD_WRITE) |
| 286 | ret = handle_ts_sign_write(bfd); |
| 287 | break; |
Harald Welte | f776669 | 2020-06-30 19:04:07 +0200 | [diff] [blame] | 288 | case E1INP_TS_TYPE_TRAU: |
| 289 | if (what & BSC_FD_READ) |
| 290 | ret = handle_ts_trau_read(bfd); |
| 291 | if (what & BSC_FD_WRITE) |
| 292 | ret = handle_ts_trau_write(bfd); |
| 293 | break; |
Harald Welte | ca71296 | 2020-06-30 19:12:42 +0200 | [diff] [blame] | 294 | case E1INP_TS_TYPE_RAW: |
| 295 | if (what & BSC_FD_READ) |
| 296 | ret = handle_ts_raw_read(bfd); |
| 297 | if (what & BSC_FD_WRITE) |
| 298 | ret = handle_ts_raw_write(bfd); |
| 299 | break; |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 300 | default: |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 301 | LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown/unsupported E1 TS type %u\n", e1i_ts->type); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static int |
| 310 | e1d_want_write(struct e1inp_ts *e1i_ts) |
| 311 | { |
| 312 | /* We never include the DAHDI B-Channel FD into the writeset */ |
| 313 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) { |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 314 | LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n"); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int |
| 324 | e1d_line_update(struct e1inp_line *line) |
| 325 | { |
| 326 | int ts; |
| 327 | int ret; |
| 328 | |
Harald Welte | ccf84ea | 2020-01-12 12:31:45 +0100 | [diff] [blame] | 329 | /* we use higher 4 bits for interface, lower 4 bits for line, |
| 330 | * resulting in max. 16 interfaces with 16 lines each */ |
| 331 | uint8_t e1d_intf = (line->port_nr >> 4) & 0xF; |
| 332 | uint8_t e1d_line = line->port_nr & 0xF; |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 333 | struct osmo_e1dp_ts_info *ts_info; |
| 334 | int num_ts_info; |
Harald Welte | ccf84ea | 2020-01-12 12:31:45 +0100 | [diff] [blame] | 335 | |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 336 | if (line->driver != &e1d_driver) |
| 337 | return -EINVAL; |
| 338 | |
Harald Welte | accd677 | 2020-01-12 13:36:56 +0100 | [diff] [blame] | 339 | if (!g_e1d) { |
| 340 | /* Connect to daemon */ |
| 341 | g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl"); |
| 342 | if (!g_e1d) { |
| 343 | LOGPIL(line, DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n"); |
| 344 | return -EPIPE; |
| 345 | } |
| 346 | } |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 347 | |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 348 | LOGPIL(line, DLINP, LOGL_NOTICE, "Line update %d %d=E1D(%d:%d) %d\n", line->num, line->port_nr, |
Harald Welte | ccf84ea | 2020-01-12 12:31:45 +0100 | [diff] [blame] | 349 | e1d_intf, e1d_line, line->num_ts); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 350 | |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 351 | ret = osmo_e1dp_client_ts_query(g_e1d, &ts_info, &num_ts_info, e1d_intf, e1d_line, E1DP_INVALID); |
| 352 | if (ret < 0) { |
| 353 | LOGPIL(line, DLINP, LOGL_ERROR, "Cannot query E1D for timeslot information: %d\n", ret); |
| 354 | return -EIO; |
| 355 | } |
| 356 | |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 357 | for (ts=1; ts<line->num_ts; ts++) |
| 358 | { |
| 359 | unsigned int idx = ts-1; |
| 360 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 361 | struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd; |
| 362 | |
| 363 | /* unregister FD if it was already registered */ |
| 364 | if (bfd->list.next && bfd->list.next != LLIST_POISON1) |
| 365 | osmo_fd_unregister(bfd); |
| 366 | |
| 367 | bfd->data = line; |
| 368 | bfd->priv_nr = ts; |
| 369 | bfd->cb = e1d_fd_cb; |
| 370 | |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 371 | if (e1i_ts->type != E1INP_TS_TYPE_NONE && ts >= num_ts_info) { |
| 372 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Timeslot configured, but not existant " |
| 373 | "on E1D side; skipping\n"); |
| 374 | continue; |
| 375 | } |
| 376 | |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 377 | switch (e1i_ts->type) { |
| 378 | case E1INP_TS_TYPE_NONE: |
| 379 | /* close/release LAPD instance, if any */ |
| 380 | if (e1i_ts->lapd) { |
| 381 | lapd_instance_free(e1i_ts->lapd); |
| 382 | e1i_ts->lapd = NULL; |
| 383 | } |
| 384 | if (bfd->fd) { |
| 385 | close(bfd->fd); |
| 386 | bfd->fd = 0; |
| 387 | } |
| 388 | continue; |
| 389 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 390 | if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) { |
| 391 | close(bfd->fd); |
| 392 | bfd->fd = 0; |
| 393 | } |
Harald Welte | ccf84ea | 2020-01-12 12:31:45 +0100 | [diff] [blame] | 394 | if (bfd->fd <= 0) { |
| 395 | bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts, |
Harald Welte | ac290ee | 2020-08-02 10:43:42 +0200 | [diff] [blame] | 396 | E1DP_TSMODE_HDLCFCS, D_BCHAN_TX_GRAN); |
Harald Welte | ccf84ea | 2020-01-12 12:31:45 +0100 | [diff] [blame] | 397 | } |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 398 | if (bfd->fd < 0) { |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 399 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts); |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 400 | talloc_free(ts_info); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 401 | return -EIO; |
| 402 | } |
Harald Welte | 6f674de | 2020-01-12 12:18:26 +0100 | [diff] [blame] | 403 | bfd->when = BSC_FD_READ; |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 404 | |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 405 | if (!e1i_ts->lapd) { |
| 406 | char name[32]; |
Harald Welte | 36166d0 | 2020-06-30 18:10:31 +0200 | [diff] [blame] | 407 | e1inp_ts_name(name, sizeof(name), e1i_ts); |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 408 | e1i_ts->lapd = lapd_instance_alloc2(1, |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 409 | e1d_write_msg, bfd, e1inp_dlsap_up, |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 410 | e1i_ts, &lapd_profile_abis, name); |
| 411 | } |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 412 | break; |
| 413 | case E1INP_TS_TYPE_HDLC: |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 414 | /* close/release LAPD instance, if any */ |
| 415 | if (e1i_ts->lapd) { |
| 416 | lapd_instance_free(e1i_ts->lapd); |
| 417 | e1i_ts->lapd = NULL; |
| 418 | } |
| 419 | /* close, if old timeslot mode doesn't match new config */ |
| 420 | if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_HDLCFCS) { |
| 421 | close(bfd->fd); |
| 422 | bfd->fd = 0; |
| 423 | } |
| 424 | if (bfd->fd <= 0) { |
| 425 | bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts, |
Harald Welte | ac290ee | 2020-08-02 10:43:42 +0200 | [diff] [blame] | 426 | E1DP_TSMODE_HDLCFCS, D_BCHAN_TX_GRAN); |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 427 | } |
| 428 | if (bfd->fd < 0) { |
| 429 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts); |
| 430 | talloc_free(ts_info); |
| 431 | return -EIO; |
| 432 | } |
| 433 | bfd->when = BSC_FD_READ; |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 434 | break; |
| 435 | case E1INP_TS_TYPE_TRAU: |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 436 | case E1INP_TS_TYPE_RAW: |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 437 | /* close/release LAPD instance, if any */ |
| 438 | if (e1i_ts->lapd) { |
| 439 | lapd_instance_free(e1i_ts->lapd); |
| 440 | e1i_ts->lapd = NULL; |
| 441 | } |
| 442 | /* close, if old timeslot mode doesn't match new config */ |
| 443 | if (bfd->fd > 0 && ts_info[ts].cfg.mode != E1DP_TSMODE_RAW) { |
| 444 | close(bfd->fd); |
| 445 | bfd->fd = 0; |
| 446 | } |
| 447 | if (bfd->fd <= 0) { |
| 448 | bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts, |
Harald Welte | ac290ee | 2020-08-02 10:43:42 +0200 | [diff] [blame] | 449 | E1DP_TSMODE_RAW, D_BCHAN_TX_GRAN); |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 450 | } |
| 451 | if (bfd->fd < 0) { |
| 452 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts); |
| 453 | talloc_free(ts_info); |
| 454 | return -EIO; |
| 455 | } |
| 456 | bfd->when = BSC_FD_READ; |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 457 | break; |
| 458 | }; |
| 459 | |
| 460 | ret = osmo_fd_register(bfd); |
| 461 | if (ret < 0) { |
Harald Welte | 28898d1 | 2020-01-12 13:34:07 +0100 | [diff] [blame] | 462 | LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret)); |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 463 | talloc_free(ts_info); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 464 | return ret; |
| 465 | } |
| 466 | } |
| 467 | |
Harald Welte | eed3916 | 2020-01-12 13:46:34 +0100 | [diff] [blame] | 468 | talloc_free(ts_info); |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 472 | struct e1inp_driver e1d_driver = { |
| 473 | .name = "e1d", |
| 474 | .want_write = e1d_want_write, |
| 475 | .line_update = e1d_line_update, |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | int |
| 479 | e1inp_e1d_init(void) |
| 480 | { |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 481 | /* register the driver with the core */ |
| 482 | return e1inp_driver_register(&e1d_driver); |
| 483 | } |
| 484 | |
| 485 | #endif /* HAVE_E1D */ |