Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for mISDNuser */ |
| 2 | |
| 3 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <mISDNif.h> |
| 36 | |
| 37 | //#define AF_COMPATIBILITY_FUNC |
| 38 | //#include <compat_af_isdn.h> |
Holger Hans Peter Freyther | b4752d0 | 2009-08-10 08:00:33 +0200 | [diff] [blame] | 39 | #ifndef AF_ISDN |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 40 | #define AF_ISDN 34 |
| 41 | #define PF_ISDN AF_ISDN |
Holger Hans Peter Freyther | b4752d0 | 2009-08-10 08:00:33 +0200 | [diff] [blame] | 42 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 43 | |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 44 | #include <osmocore/select.h> |
| 45 | #include <osmocore/msgb.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 46 | #include <openbsc/debug.h> |
| 47 | #include <openbsc/gsm_data.h> |
| 48 | #include <openbsc/abis_nm.h> |
| 49 | #include <openbsc/abis_rsl.h> |
| 50 | #include <openbsc/subchan_demux.h> |
| 51 | #include <openbsc/e1_input.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 52 | #include <osmocore/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 53 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 54 | #define TS1_ALLOC_SIZE 300 |
| 55 | |
| 56 | struct prim_name { |
| 57 | unsigned int prim; |
| 58 | const char *name; |
| 59 | }; |
| 60 | |
| 61 | const struct prim_name prim_names[] = { |
| 62 | { PH_CONTROL_IND, "PH_CONTROL_IND" }, |
| 63 | { PH_DATA_IND, "PH_DATA_IND" }, |
| 64 | { PH_DATA_CNF, "PH_DATA_CNF" }, |
| 65 | { PH_ACTIVATE_IND, "PH_ACTIVATE_IND" }, |
| 66 | { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" }, |
| 67 | { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" }, |
| 68 | { DL_RELEASE_IND, "DL_RELEASE_IND" }, |
| 69 | { DL_RELEASE_CNF, "DL_RELEASE_CNF" }, |
| 70 | { DL_DATA_IND, "DL_DATA_IND" }, |
| 71 | { DL_UNITDATA_IND, "DL_UNITDATA_IND" }, |
| 72 | { DL_INFORMATION_IND, "DL_INFORMATION_IND" }, |
| 73 | { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" }, |
| 74 | { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" }, |
| 75 | }; |
| 76 | |
| 77 | const char *get_prim_name(unsigned int prim) |
| 78 | { |
| 79 | int i; |
| 80 | |
| 81 | for (i = 0; i < ARRAY_SIZE(prim_names); i++) { |
| 82 | if (prim_names[i].prim == prim) |
| 83 | return prim_names[i].name; |
| 84 | } |
| 85 | |
| 86 | return "UNKNOWN"; |
| 87 | } |
| 88 | |
| 89 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 90 | { |
| 91 | struct e1inp_line *line = bfd->data; |
| 92 | unsigned int ts_nr = bfd->priv_nr; |
| 93 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 94 | struct e1inp_sign_link *link; |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 95 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "mISDN TS1"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 96 | struct sockaddr_mISDN l2addr; |
| 97 | struct mISDNhead *hh; |
| 98 | socklen_t alen; |
| 99 | int ret; |
| 100 | |
| 101 | if (!msg) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | hh = (struct mISDNhead *) msg->data; |
| 105 | |
| 106 | alen = sizeof(l2addr); |
| 107 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 108 | (struct sockaddr *) &l2addr, &alen); |
| 109 | if (ret < 0) { |
| 110 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | if (alen != sizeof(l2addr)) { |
| 115 | fprintf(stderr, "%s error len\n", __func__); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | msgb_put(msg, ret); |
| 120 | |
| 121 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 122 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 123 | |
| 124 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x): %s\n", |
| 125 | ret, hh->prim, hh->id, get_prim_name(hh->prim)); |
| 126 | |
| 127 | switch (hh->prim) { |
| 128 | case DL_INFORMATION_IND: |
| 129 | /* mISDN tells us which channel number is allocated for this |
| 130 | * tuple of (SAPI, TEI). */ |
| 131 | DEBUGP(DMI, "DL_INFORMATION_IND: use channel(%d) sapi(%d) tei(%d) for now\n", |
| 132 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 133 | link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi); |
| 134 | if (!link) { |
| 135 | DEBUGPC(DMI, "mISDN message for unknown sign_link\n"); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 136 | msgb_free(msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 137 | return -EINVAL; |
| 138 | } |
| 139 | /* save the channel number in the driver private struct */ |
| 140 | link->driver.misdn.channel = l2addr.channel; |
| 141 | break; |
| 142 | case DL_ESTABLISH_IND: |
| 143 | DEBUGP(DMI, "DL_ESTABLISH_IND: channel(%d) sapi(%d) tei(%d)\n", |
| 144 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 145 | ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi); |
| 146 | break; |
| 147 | case DL_RELEASE_IND: |
| 148 | DEBUGP(DMI, "DL_RELEASE_IND: channel(%d) sapi(%d) tei(%d)\n", |
| 149 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 150 | ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi); |
| 151 | break; |
| 152 | case DL_DATA_IND: |
Harald Welte | cde2085 | 2009-07-04 09:28:00 +0200 | [diff] [blame] | 153 | case DL_UNITDATA_IND: |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 154 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 155 | DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN)); |
| 156 | ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi); |
| 157 | break; |
| 158 | case PH_ACTIVATE_IND: |
| 159 | DEBUGP(DMI, "PH_ACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n", |
| 160 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 161 | break; |
| 162 | case PH_DEACTIVATE_IND: |
| 163 | DEBUGP(DMI, "PH_DEACTIVATE_IND: channel(%d) sapi(%d) tei(%d)\n", |
| 164 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 173 | { |
| 174 | /* We never include the mISDN B-Channel FD into the |
| 175 | * writeset, since it doesn't support poll() based |
| 176 | * write flow control */ |
| 177 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) |
| 178 | return 0; |
| 179 | |
| 180 | e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void timeout_ts1_write(void *data) |
| 186 | { |
| 187 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 188 | |
| 189 | /* trigger write of ts1, due to tx delay timer */ |
| 190 | ts_want_write(e1i_ts); |
| 191 | } |
| 192 | |
| 193 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 194 | { |
| 195 | struct e1inp_line *line = bfd->data; |
| 196 | unsigned int ts_nr = bfd->priv_nr; |
| 197 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 198 | struct e1inp_sign_link *sign_link; |
| 199 | struct sockaddr_mISDN sa; |
| 200 | struct msgb *msg; |
| 201 | struct mISDNhead *hh; |
| 202 | u_int8_t *l2_data; |
| 203 | int ret; |
| 204 | |
| 205 | bfd->when &= ~BSC_FD_WRITE; |
| 206 | |
| 207 | /* get the next msg for this timeslot */ |
| 208 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 209 | if (!msg) { |
| 210 | /* no message after tx delay timer */ |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | l2_data = msg->data; |
| 215 | |
| 216 | /* prepend the mISDNhead */ |
| 217 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 218 | hh->prim = DL_DATA_REQ; |
| 219 | |
| 220 | DEBUGP(DMI, "TX TEI(%d) SAPI(%d): %s\n", sign_link->tei, |
| 221 | sign_link->sapi, hexdump(l2_data, msg->len - MISDN_HEADER_LEN)); |
| 222 | |
| 223 | /* construct the sockaddr */ |
| 224 | sa.family = AF_ISDN; |
| 225 | sa.sapi = sign_link->sapi; |
| 226 | sa.dev = sign_link->tei; |
| 227 | sa.channel = sign_link->driver.misdn.channel; |
| 228 | |
| 229 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
| 230 | (struct sockaddr *)&sa, sizeof(sa)); |
| 231 | if (ret < 0) |
| 232 | fprintf(stderr, "%s sendto failed %d\n", __func__, ret); |
| 233 | msgb_free(msg); |
| 234 | |
| 235 | /* set tx delay timer for next event */ |
| 236 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 237 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 238 | bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | #define BCHAN_TX_GRAN 160 |
| 244 | /* write to a B channel TS */ |
| 245 | static int handle_tsX_write(struct bsc_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 mISDNhead *hh; |
| 251 | u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)]; |
| 252 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 253 | int ret; |
| 254 | |
| 255 | hh = (struct mISDNhead *) tx_buf; |
| 256 | hh->prim = PH_DATA_REQ; |
| 257 | |
| 258 | subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN); |
| 259 | |
| 260 | DEBUGP(DMIB, "BCHAN TX: %s\n", |
| 261 | hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN)); |
| 262 | |
| 263 | ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0); |
| 264 | if (ret < sizeof(*hh) + BCHAN_TX_GRAN) |
Holger Hans Peter Freyther | 29645dd | 2010-03-30 15:25:02 +0200 | [diff] [blame] | 265 | DEBUGP(DMIB, "send returns %d instead of %zu\n", ret, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 266 | sizeof(*hh) + BCHAN_TX_GRAN); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | #define TSX_ALLOC_SIZE 4096 |
| 272 | /* FIXME: read from a B channel TS */ |
| 273 | static int handle_tsX_read(struct bsc_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]; |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 278 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE, "mISDN TSx"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 279 | struct mISDNhead *hh; |
| 280 | int ret; |
| 281 | |
| 282 | if (!msg) |
| 283 | return -ENOMEM; |
| 284 | |
| 285 | hh = (struct mISDNhead *) msg->data; |
| 286 | |
| 287 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 288 | if (ret < 0) { |
| 289 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | msgb_put(msg, ret); |
| 294 | |
| 295 | if (hh->prim != PH_CONTROL_IND) |
| 296 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n", |
| 297 | ret, hh->prim, hh->id, get_prim_name(hh->prim)); |
| 298 | |
| 299 | switch (hh->prim) { |
| 300 | case PH_DATA_IND: |
| 301 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 302 | DEBUGP(DMIB, "BCHAN RX: %s\n", |
| 303 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN)); |
| 304 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 305 | break; |
| 306 | case PH_ACTIVATE_IND: |
| 307 | case PH_DATA_CNF: |
| 308 | /* physical layer indicates that data has been sent, |
| 309 | * we thus can send some more data */ |
| 310 | ret = handle_tsX_write(bfd); |
| 311 | default: |
| 312 | break; |
| 313 | } |
| 314 | /* FIXME: why do we free signalling msgs in the caller, and trau not? */ |
| 315 | msgb_free(msg); |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /* callback from select.c in case one of the fd's can be read/written */ |
| 321 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 322 | { |
| 323 | struct e1inp_line *line = bfd->data; |
| 324 | unsigned int ts_nr = bfd->priv_nr; |
| 325 | unsigned int idx = ts_nr-1; |
| 326 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 327 | int rc = 0; |
| 328 | |
| 329 | switch (e1i_ts->type) { |
| 330 | case E1INP_TS_TYPE_SIGN: |
| 331 | if (what & BSC_FD_READ) |
| 332 | rc = handle_ts1_read(bfd); |
| 333 | if (what & BSC_FD_WRITE) |
| 334 | rc = handle_ts1_write(bfd); |
| 335 | break; |
| 336 | case E1INP_TS_TYPE_TRAU: |
| 337 | if (what & BSC_FD_READ) |
| 338 | rc = handle_tsX_read(bfd); |
| 339 | /* We never include the mISDN B-Channel FD into the |
| 340 | * writeset, since it doesn't support poll() based |
| 341 | * write flow control */ |
| 342 | break; |
| 343 | default: |
| 344 | fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type); |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | return rc; |
| 349 | } |
| 350 | |
| 351 | static int activate_bchan(struct e1inp_line *line, int ts, int act) |
| 352 | { |
| 353 | struct mISDNhead hh; |
| 354 | int ret; |
| 355 | unsigned int idx = ts-1; |
| 356 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 357 | struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd; |
| 358 | |
| 359 | fprintf(stdout, "activate bchan\n"); |
| 360 | if (act) |
| 361 | hh.prim = PH_ACTIVATE_REQ; |
| 362 | else |
| 363 | hh.prim = PH_DEACTIVATE_REQ; |
| 364 | |
| 365 | hh.id = MISDN_ID_ANY; |
| 366 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 367 | if (ret < 0) { |
| 368 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 369 | strerror(errno)); |
| 370 | } |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | struct e1inp_driver misdn_driver = { |
| 376 | .name = "mISDNuser", |
| 377 | .want_write = ts_want_write, |
| 378 | }; |
| 379 | |
| 380 | static int mi_e1_setup(struct e1inp_line *line, int release_l2) |
| 381 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 382 | int ts, ret; |
| 383 | |
| 384 | /* TS0 is CRC4, don't need any fd for it */ |
| 385 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
| 386 | unsigned int idx = ts-1; |
| 387 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 388 | struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd; |
| 389 | struct sockaddr_mISDN addr; |
| 390 | |
| 391 | bfd->data = line; |
| 392 | bfd->priv_nr = ts; |
| 393 | bfd->cb = misdn_fd_cb; |
| 394 | |
| 395 | switch (e1i_ts->type) { |
| 396 | case E1INP_TS_TYPE_NONE: |
| 397 | continue; |
| 398 | break; |
| 399 | case E1INP_TS_TYPE_SIGN: |
| 400 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
| 401 | bfd->when = BSC_FD_READ; |
| 402 | break; |
| 403 | case E1INP_TS_TYPE_TRAU: |
| 404 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 405 | /* We never include the mISDN B-Channel FD into the |
| 406 | * writeset, since it doesn't support poll() based |
| 407 | * write flow control */ |
| 408 | bfd->when = BSC_FD_READ; |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | if (bfd->fd < 0) { |
| 413 | fprintf(stderr, "%s could not open socket %s\n", |
| 414 | __func__, strerror(errno)); |
| 415 | return bfd->fd; |
| 416 | } |
| 417 | |
| 418 | memset(&addr, 0, sizeof(addr)); |
| 419 | addr.family = AF_ISDN; |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 420 | addr.dev = line->num; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 421 | switch (e1i_ts->type) { |
| 422 | case E1INP_TS_TYPE_SIGN: |
| 423 | addr.channel = 0; |
| 424 | /* SAPI not supported yet in kernel */ |
| 425 | //addr.sapi = e1inp_ts->sign.sapi; |
| 426 | addr.sapi = 0; |
| 427 | addr.tei = GROUP_TEI; |
| 428 | break; |
| 429 | case E1INP_TS_TYPE_TRAU: |
| 430 | addr.channel = ts; |
| 431 | break; |
| 432 | default: |
| 433 | DEBUGP(DMI, "unsupported E1 TS type: %u\n", |
| 434 | e1i_ts->type); |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 439 | if (ret < 0) { |
| 440 | fprintf(stderr, "could not bind l2 socket %s\n", |
| 441 | strerror(errno)); |
| 442 | return -EIO; |
| 443 | } |
| 444 | |
| 445 | if (e1i_ts->type == E1INP_TS_TYPE_SIGN) { |
| 446 | ret = ioctl(bfd->fd, IMCLEAR_L2, &release_l2); |
| 447 | if (ret < 0) { |
| 448 | fprintf(stderr, "could not send IOCTL IMCLEAN_L2 %s\n", strerror(errno)); |
| 449 | return -EIO; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* FIXME: only activate B-Channels once we start to |
| 454 | * use them to conserve CPU power */ |
| 455 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) |
| 456 | activate_bchan(line, ts, 1); |
| 457 | |
| 458 | ret = bsc_register_fd(bfd); |
| 459 | if (ret < 0) { |
| 460 | fprintf(stderr, "could not register FD: %s\n", |
| 461 | strerror(ret)); |
| 462 | return ret; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 469 | int mi_e1_line_update(struct e1inp_line *line) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 470 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 471 | struct mISDN_devinfo devinfo; |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 472 | int sk, ret, cnt; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 473 | |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 474 | if (!line->driver) { |
| 475 | /* this must be the first update */ |
| 476 | line->driver = &misdn_driver; |
| 477 | } else { |
| 478 | /* this is a subsequent update */ |
| 479 | /* FIXME: first close all sockets */ |
| 480 | fprintf(stderr, "incremental line updates not supported yet\n"); |
| 481 | return 0; |
| 482 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 483 | |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 484 | if (line->driver != &misdn_driver) |
| 485 | return -EINVAL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 486 | |
| 487 | /* open the ISDN card device */ |
| 488 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
| 489 | if (sk < 0) { |
| 490 | fprintf(stderr, "%s could not open socket %s\n", |
| 491 | __func__, strerror(errno)); |
| 492 | return sk; |
| 493 | } |
| 494 | |
| 495 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 496 | if (ret) { |
| 497 | fprintf(stderr, "%s error getting interf count: %s\n", |
| 498 | __func__, strerror(errno)); |
| 499 | close(sk); |
| 500 | return -ENODEV; |
| 501 | } |
| 502 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 503 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 504 | #if 1 |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 505 | devinfo.id = line->num; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 506 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 507 | if (ret < 0) { |
| 508 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 509 | line->num, strerror(errno)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 510 | return -ENODEV; |
| 511 | } |
| 512 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 513 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 514 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 515 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 516 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 517 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 518 | #endif |
| 519 | |
| 520 | if (!(devinfo.Dprotocols & (1 << ISDN_P_NT_E1))) { |
| 521 | fprintf(stderr, "error: card is not of type E1 (NT-mode)\n"); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 525 | ret = mi_e1_setup(line, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 526 | if (ret) |
| 527 | return ret; |
| 528 | |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 529 | return 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 530 | } |
Harald Welte | 65c12b9 | 2009-08-04 14:11:49 +0200 | [diff] [blame] | 531 | |
| 532 | static __attribute__((constructor)) void on_dso_load_sms(void) |
| 533 | { |
| 534 | /* register the driver with the core */ |
| 535 | e1inp_driver_register(&misdn_driver); |
| 536 | } |