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