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