Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for DAHDI */ |
| 2 | |
| 3 | /* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 25 | #include "../../config.h" |
| 26 | |
| 27 | #ifdef HAVE_DAHDI_USER_H |
| 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 | |
| 41 | #include <osmocom/core/select.h> |
| 42 | #include <osmocom/core/msgb.h> |
Pablo Neira Ayuso | 0b9ed9a | 2011-07-02 17:25:19 +0200 | [diff] [blame] | 43 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 44 | #include <osmocom/core/signal.h> |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 45 | #include <osmocom/core/rate_ctr.h> |
Pablo Neira Ayuso | 0b9ed9a | 2011-07-02 17:25:19 +0200 | [diff] [blame] | 46 | #include <osmocom/abis/subchan_demux.h> |
| 47 | #include <osmocom/abis/e1_input.h> |
Harald Welte | 71d87b2 | 2011-07-18 14:49:56 +0200 | [diff] [blame] | 48 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 49 | |
Pablo Neira Ayuso | 355ce69 | 2011-07-05 14:53:37 +0200 | [diff] [blame] | 50 | #include <osmocom/abis/lapd.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 51 | |
| 52 | #define TS1_ALLOC_SIZE 300 |
| 53 | |
| 54 | /* Corresponds to dahdi/user.h, only PRI related events */ |
| 55 | static const struct value_string dahdi_evt_names[] = { |
| 56 | { DAHDI_EVENT_NONE, "NONE" }, |
| 57 | { DAHDI_EVENT_ALARM, "ALARM" }, |
| 58 | { DAHDI_EVENT_NOALARM, "NOALARM" }, |
| 59 | { DAHDI_EVENT_ABORT, "HDLC ABORT" }, |
| 60 | { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" }, |
| 61 | { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" }, |
| 62 | { DAHDI_EVENT_REMOVED, "REMOVED" }, |
| 63 | { 0, NULL } |
| 64 | }; |
| 65 | |
| 66 | static void handle_dahdi_exception(struct e1inp_ts *ts) |
| 67 | { |
| 68 | int rc, evt; |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 69 | struct e1inp_line *line = ts->line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 70 | struct input_signal_data isd; |
| 71 | |
| 72 | rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt); |
| 73 | if (rc < 0) |
| 74 | return; |
| 75 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 76 | LOGP(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] | 77 | ts->line->num, ts->line->name, ts->num, |
| 78 | get_value_string(dahdi_evt_names, evt)); |
| 79 | |
| 80 | isd.line = ts->line; |
| 81 | |
| 82 | switch (evt) { |
| 83 | case DAHDI_EVENT_ALARM: |
| 84 | /* we should notify the code that the line is gone */ |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 85 | osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_ALARM, &isd); |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 86 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_ALARM]); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 87 | break; |
| 88 | case DAHDI_EVENT_NOALARM: |
| 89 | /* alarm has gone, we should re-start the SABM requests */ |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 90 | 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] | 91 | break; |
Harald Welte | 95e5dec | 2011-08-16 14:41:32 +0200 | [diff] [blame] | 92 | case DAHDI_EVENT_ABORT: |
| 93 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_ABORT]); |
| 94 | break; |
| 95 | case DAHDI_EVENT_OVERRUN: |
| 96 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_OVERR]); |
| 97 | break; |
| 98 | case DAHDI_EVENT_BADFCS: |
| 99 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_HDLC_BADFCS]); |
| 100 | break; |
| 101 | case DAHDI_EVENT_REMOVED: |
| 102 | rate_ctr_inc(&line->rate_ctr->ctr[E1I_CTR_REMOVED]); |
| 103 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | static int handle_ts1_read(struct osmo_fd *bfd) |
| 108 | { |
| 109 | struct e1inp_line *line = bfd->data; |
| 110 | unsigned int ts_nr = bfd->priv_nr; |
| 111 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 112 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1"); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 113 | int ret; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 114 | |
| 115 | if (!msg) |
| 116 | return -ENOMEM; |
| 117 | |
| 118 | ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16); |
| 119 | if (ret == -1) |
| 120 | handle_dahdi_exception(e1i_ts); |
| 121 | else if (ret < 0) { |
| 122 | perror("read "); |
| 123 | } |
| 124 | msgb_put(msg, ret - 2); |
| 125 | if (ret <= 3) { |
| 126 | perror("read "); |
| 127 | } |
| 128 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 129 | return e1inp_rx_ts_lapd(e1i_ts, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 133 | { |
| 134 | /* We never include the DAHDI B-Channel FD into the |
| 135 | * writeset, since it doesn't support poll() based |
| 136 | * write flow control */ |
| 137 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) { |
| 138 | fprintf(stderr, "Trying to write TRAU ts\n"); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static void timeout_ts1_write(void *data) |
| 148 | { |
| 149 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 150 | |
| 151 | /* trigger write of ts1, due to tx delay timer */ |
| 152 | ts_want_write(e1i_ts); |
| 153 | } |
| 154 | |
| 155 | static void dahdi_write_msg(uint8_t *data, int len, void *cbdata) |
| 156 | { |
| 157 | struct osmo_fd *bfd = cbdata; |
| 158 | struct e1inp_line *line = bfd->data; |
| 159 | unsigned int ts_nr = bfd->priv_nr; |
| 160 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 161 | int ret; |
| 162 | |
| 163 | ret = write(bfd->fd, data, len + 2); |
| 164 | if (ret == -1) |
| 165 | handle_dahdi_exception(e1i_ts); |
| 166 | else if (ret < 0) |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 167 | LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int handle_ts1_write(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 e1inp_sign_link *sign_link; |
| 176 | struct msgb *msg; |
| 177 | |
| 178 | bfd->when &= ~BSC_FD_WRITE; |
| 179 | |
| 180 | /* get the next msg for this timeslot */ |
| 181 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 182 | if (!msg) { |
| 183 | /* no message after tx delay timer */ |
| 184 | return 0; |
| 185 | } |
| 186 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 187 | DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 188 | lapd_transmit(e1i_ts->lapd, sign_link->tei, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 189 | sign_link->sapi, msg->data, msg->len); |
| 190 | msgb_free(msg); |
| 191 | |
| 192 | /* set tx delay timer for next event */ |
| 193 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 194 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 195 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static int invertbits = 1; |
| 202 | |
| 203 | static uint8_t flip_table[256]; |
| 204 | |
| 205 | static void init_flip_bits(void) |
| 206 | { |
| 207 | int i,k; |
| 208 | |
| 209 | for (i = 0 ; i < 256 ; i++) { |
| 210 | uint8_t sample = 0 ; |
| 211 | for (k = 0; k<8; k++) { |
| 212 | if ( i & 1 << k ) sample |= 0x80 >> k; |
| 213 | } |
| 214 | flip_table[i] = sample; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static uint8_t * flip_buf_bits ( uint8_t * buf , int len) |
| 219 | { |
| 220 | int i; |
| 221 | uint8_t * start = buf; |
| 222 | |
| 223 | for (i = 0 ; i < len; i++) { |
| 224 | buf[i] = flip_table[(uint8_t)buf[i]]; |
| 225 | } |
| 226 | |
| 227 | return start; |
| 228 | } |
| 229 | |
| 230 | #define D_BCHAN_TX_GRAN 160 |
| 231 | /* write to a B channel TS */ |
| 232 | static int handle_tsX_write(struct osmo_fd *bfd) |
| 233 | { |
| 234 | struct e1inp_line *line = bfd->data; |
| 235 | unsigned int ts_nr = bfd->priv_nr; |
| 236 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 237 | uint8_t tx_buf[D_BCHAN_TX_GRAN]; |
| 238 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 239 | int ret; |
| 240 | |
| 241 | ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN); |
| 242 | |
| 243 | if (ret != D_BCHAN_TX_GRAN) { |
| 244 | fprintf(stderr, "Huh, got ret of %d\n", ret); |
| 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | } |
| 248 | |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 249 | DEBUGP(DLMIB, "BCHAN TX: %s\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 250 | osmo_hexdump(tx_buf, D_BCHAN_TX_GRAN)); |
| 251 | |
| 252 | if (invertbits) { |
| 253 | flip_buf_bits(tx_buf, ret); |
| 254 | } |
| 255 | |
| 256 | ret = write(bfd->fd, tx_buf, ret); |
| 257 | if (ret < D_BCHAN_TX_GRAN) |
| 258 | fprintf(stderr, "send returns %d instead of %d\n", ret, |
| 259 | D_BCHAN_TX_GRAN); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | #define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN) |
| 265 | /* FIXME: read from a B channel TS */ |
| 266 | static int handle_tsX_read(struct osmo_fd *bfd) |
| 267 | { |
| 268 | struct e1inp_line *line = bfd->data; |
| 269 | unsigned int ts_nr = bfd->priv_nr; |
| 270 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 271 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx"); |
| 272 | int ret; |
| 273 | |
| 274 | if (!msg) |
| 275 | return -ENOMEM; |
| 276 | |
| 277 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 278 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
| 279 | fprintf(stderr, "read error %d %s\n", ret, strerror(errno)); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | if (invertbits) { |
| 284 | flip_buf_bits(msg->data, ret); |
| 285 | } |
| 286 | |
| 287 | msgb_put(msg, ret); |
| 288 | |
| 289 | msg->l2h = msg->data; |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 290 | DEBUGP(DLMIB, "BCHAN RX: %s\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 291 | osmo_hexdump(msgb_l2(msg), ret)); |
| 292 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 293 | /* physical layer indicates that data has been sent, |
| 294 | * we thus can send some more data */ |
| 295 | ret = handle_tsX_write(bfd); |
| 296 | msgb_free(msg); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | /* callback from select.c in case one of the fd's can be read/written */ |
| 302 | static int dahdi_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 303 | { |
| 304 | struct e1inp_line *line = bfd->data; |
| 305 | unsigned int ts_nr = bfd->priv_nr; |
| 306 | unsigned int idx = ts_nr-1; |
| 307 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 308 | int rc = 0; |
| 309 | |
| 310 | switch (e1i_ts->type) { |
| 311 | case E1INP_TS_TYPE_SIGN: |
| 312 | if (what & BSC_FD_EXCEPT) |
| 313 | handle_dahdi_exception(e1i_ts); |
| 314 | if (what & BSC_FD_READ) |
| 315 | rc = handle_ts1_read(bfd); |
| 316 | if (what & BSC_FD_WRITE) |
| 317 | rc = handle_ts1_write(bfd); |
| 318 | break; |
| 319 | case E1INP_TS_TYPE_TRAU: |
| 320 | if (what & BSC_FD_EXCEPT) |
| 321 | handle_dahdi_exception(e1i_ts); |
| 322 | if (what & BSC_FD_READ) |
| 323 | rc = handle_tsX_read(bfd); |
| 324 | if (what & BSC_FD_WRITE) |
| 325 | rc = handle_tsX_write(bfd); |
| 326 | /* We never include the DAHDI B-Channel FD into the |
| 327 | * writeset, since it doesn't support poll() based |
| 328 | * write flow control */ |
| 329 | break; |
| 330 | default: |
| 331 | fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | return rc; |
| 336 | } |
| 337 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 338 | static int dahdi_e1_line_update(struct e1inp_line *line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 339 | |
| 340 | struct e1inp_driver dahdi_driver = { |
| 341 | .name = "dahdi", |
| 342 | .want_write = ts_want_write, |
| 343 | .line_update = &dahdi_e1_line_update, |
| 344 | }; |
| 345 | |
| 346 | void dahdi_set_bufinfo(int fd, int as_sigchan) |
| 347 | { |
| 348 | struct dahdi_bufferinfo bi; |
| 349 | int x = 0; |
| 350 | |
| 351 | if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) { |
| 352 | fprintf(stderr, "Error getting bufinfo\n"); |
| 353 | exit(-1); |
| 354 | } |
| 355 | |
| 356 | if (as_sigchan) { |
| 357 | bi.numbufs = 4; |
| 358 | bi.bufsize = 512; |
| 359 | } else { |
| 360 | bi.numbufs = 8; |
| 361 | bi.bufsize = D_BCHAN_TX_GRAN; |
| 362 | bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL; |
| 363 | } |
| 364 | |
| 365 | if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) { |
| 366 | fprintf(stderr, "Error setting bufinfo\n"); |
| 367 | exit(-1); |
| 368 | } |
| 369 | |
| 370 | if (!as_sigchan) { |
| 371 | if (ioctl(fd, DAHDI_AUDIOMODE, &x)) { |
| 372 | fprintf(stderr, "Error setting bufinfo\n"); |
| 373 | exit(-1); |
| 374 | } |
| 375 | } else { |
| 376 | int one = 1; |
| 377 | ioctl(fd, DAHDI_HDLCFCSMODE, &one); |
| 378 | /* we cannot reliably check for the ioctl return value here |
| 379 | * as this command will fail if the slot _already_ was a |
| 380 | * signalling slot before :( */ |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static int dahdi_e1_setup(struct e1inp_line *line) |
| 385 | { |
| 386 | int ts, ret; |
| 387 | |
| 388 | /* TS0 is CRC4, don't need any fd for it */ |
| 389 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
| 390 | unsigned int idx = ts-1; |
| 391 | char openstr[128]; |
| 392 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 393 | struct osmo_fd *bfd = &e1i_ts->driver.dahdi.fd; |
Harald Welte | f3ca61c | 2011-08-09 11:21:23 +0200 | [diff] [blame] | 394 | int dev_nr; |
| 395 | |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 396 | /* unregister FD if it was already registered */ |
| 397 | if (bfd->list.next && bfd->list.next != LLIST_POISON1) |
| 398 | osmo_fd_unregister(bfd); |
| 399 | |
Harald Welte | f3ca61c | 2011-08-09 11:21:23 +0200 | [diff] [blame] | 400 | /* DAHDI device names/numbers just keep incrementing |
| 401 | * even over multiple boards. So TS1 of the second |
| 402 | * board will be 32 */ |
| 403 | dev_nr = line->num * (NUM_E1_TS-1) + ts; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 404 | |
| 405 | bfd->data = line; |
| 406 | bfd->priv_nr = ts; |
| 407 | bfd->cb = dahdi_fd_cb; |
Harald Welte | f3ca61c | 2011-08-09 11:21:23 +0200 | [diff] [blame] | 408 | snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", dev_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 409 | |
| 410 | switch (e1i_ts->type) { |
| 411 | case E1INP_TS_TYPE_NONE: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 412 | /* close/release LAPD instance, if any */ |
| 413 | if (e1i_ts->lapd) { |
| 414 | lapd_instance_free(e1i_ts->lapd); |
| 415 | e1i_ts->lapd = NULL; |
| 416 | } |
| 417 | if (bfd->fd) { |
| 418 | close(bfd->fd); |
| 419 | bfd->fd = 0; |
| 420 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 421 | continue; |
| 422 | break; |
| 423 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 424 | if (!bfd->fd) |
| 425 | bfd->fd = open(openstr, O_RDWR | O_NONBLOCK); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 426 | if (bfd->fd == -1) { |
| 427 | fprintf(stderr, "%s could not open %s %s\n", |
| 428 | __func__, openstr, strerror(errno)); |
| 429 | exit(-1); |
| 430 | } |
| 431 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
| 432 | dahdi_set_bufinfo(bfd->fd, 1); |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [diff] [blame] | 433 | if (!e1i_ts->lapd) |
| 434 | e1i_ts->lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 435 | break; |
| 436 | case E1INP_TS_TYPE_TRAU: |
Harald Welte | cfc9f1f | 2011-08-24 09:45:36 +0200 | [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 | if (!bfd->fd) |
| 443 | bfd->fd = open(openstr, O_RDWR | O_NONBLOCK); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 444 | if (bfd->fd == -1) { |
| 445 | fprintf(stderr, "%s could not open %s %s\n", |
| 446 | __func__, openstr, strerror(errno)); |
| 447 | exit(-1); |
| 448 | } |
| 449 | dahdi_set_bufinfo(bfd->fd, 0); |
| 450 | /* We never include the DAHDI B-Channel FD into the |
| 451 | * writeset, since it doesn't support poll() based |
| 452 | * write flow control */ |
| 453 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | if (bfd->fd < 0) { |
| 458 | fprintf(stderr, "%s could not open %s %s\n", |
| 459 | __func__, openstr, strerror(errno)); |
| 460 | return bfd->fd; |
| 461 | } |
| 462 | |
| 463 | ret = osmo_fd_register(bfd); |
| 464 | if (ret < 0) { |
| 465 | fprintf(stderr, "could not register FD: %s\n", |
| 466 | strerror(ret)); |
| 467 | return ret; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 474 | static int dahdi_e1_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 475 | { |
| 476 | if (line->driver != &dahdi_driver) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | return dahdi_e1_setup(line); |
| 480 | } |
| 481 | |
| 482 | int e1inp_dahdi_init(void) |
| 483 | { |
| 484 | init_flip_bits(); |
| 485 | |
| 486 | /* register the driver with the core */ |
| 487 | return e1inp_driver_register(&dahdi_driver); |
| 488 | } |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 489 | |
| 490 | #endif /* HAVE_DAHDI_USER_H */ |