Matthew Fredrickson | ab27bdd | 2010-02-16 22:07:04 +0100 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for DAHDI */ |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 2 | |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 3 | /* (C) 2008-2011 by Harald Welte <laforge@gnumonks.org> |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Matthew Fredrickson | ab27bdd | 2010-02-16 22:07:04 +0100 | [diff] [blame] | 5 | * (C) 2010 by Digium and Matthew Fredrickson <creslin@digium.com> |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 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 | c7841cc | 2011-02-05 15:13:27 +0100 | [diff] [blame] | 25 | #include "../../bscconfig.h" |
| 26 | |
| 27 | #ifdef HAVE_DAHDI_USER_H |
| 28 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [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/types.h> |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <arpa/inet.h> |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 40 | #include <dahdi/user.h> |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 41 | |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 42 | #include <osmocore/select.h> |
| 43 | #include <osmocore/msgb.h> |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 44 | #include <openbsc/debug.h> |
| 45 | #include <openbsc/gsm_data.h> |
| 46 | #include <openbsc/abis_nm.h> |
| 47 | #include <openbsc/abis_rsl.h> |
| 48 | #include <openbsc/subchan_demux.h> |
| 49 | #include <openbsc/e1_input.h> |
Harald Welte | 4c826f7 | 2011-01-14 15:55:42 +0100 | [diff] [blame] | 50 | #include <openbsc/signal.h> |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 51 | #include <osmocore/talloc.h> |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 52 | |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 53 | #include "lapd.h" |
| 54 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 55 | #define TS1_ALLOC_SIZE 300 |
| 56 | |
Harald Welte | a6ef1b2 | 2011-02-13 14:19:26 +0100 | [diff] [blame] | 57 | /* Corresponds to dahdi/user.h, only PRI related events */ |
| 58 | static const struct value_string dahdi_evt_names[] = { |
| 59 | { DAHDI_EVENT_NONE, "NONE" }, |
| 60 | { DAHDI_EVENT_ALARM, "ALARM" }, |
| 61 | { DAHDI_EVENT_NOALARM, "NOALARM" }, |
| 62 | { DAHDI_EVENT_ABORT, "HDLC ABORT" }, |
| 63 | { DAHDI_EVENT_OVERRUN, "HDLC OVERRUN" }, |
| 64 | { DAHDI_EVENT_BADFCS, "HDLC BAD FCS" }, |
| 65 | { DAHDI_EVENT_REMOVED, "REMOVED" }, |
| 66 | { 0, NULL } |
| 67 | }; |
| 68 | |
| 69 | static void handle_dahdi_exception(struct e1inp_ts *ts) |
| 70 | { |
| 71 | int rc, evt; |
| 72 | |
| 73 | rc = ioctl(ts->driver.dahdi.fd.fd, DAHDI_GETEVENT, &evt); |
| 74 | if (rc < 0) |
| 75 | return; |
| 76 | |
| 77 | LOGP(DMI, LOGL_NOTICE, "Line %u(%s) / TS %u DAHDI EVENT %s\n", |
| 78 | ts->line->num, ts->line->name, ts->num, |
| 79 | get_value_string(dahdi_evt_names, evt)); |
| 80 | |
| 81 | switch (evt) { |
| 82 | case DAHDI_EVENT_ALARM: |
| 83 | /* FIXME: we should notify the code that the line is gone */ |
| 84 | break; |
| 85 | case DAHDI_EVENT_NOALARM: |
| 86 | /* FIXME: alarm has gone, we should re-start the SABM requests */ |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 91 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 92 | { |
| 93 | struct e1inp_line *line = bfd->data; |
| 94 | unsigned int ts_nr = bfd->priv_nr; |
| 95 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 96 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE, "DAHDI TS1"); |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 97 | lapd_mph_type prim; |
| 98 | unsigned int sapi, tei; |
| 99 | int ilen, ret; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 100 | uint8_t *idata; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 101 | |
| 102 | if (!msg) |
| 103 | return -ENOMEM; |
| 104 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 105 | ret = read(bfd->fd, msg->data, TS1_ALLOC_SIZE - 16); |
Harald Welte | a6ef1b2 | 2011-02-13 14:19:26 +0100 | [diff] [blame] | 106 | if (ret == -1) |
| 107 | handle_dahdi_exception(e1i_ts); |
| 108 | else if (ret < 0) { |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 109 | perror("read "); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 110 | } |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 111 | msgb_put(msg, ret - 2); |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 112 | if (ret <= 3) { |
| 113 | perror("read "); |
| 114 | } |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 115 | |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 116 | sapi = msg->data[0] >> 2; |
| 117 | tei = msg->data[1] >> 1; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 118 | |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 119 | DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 120 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 121 | idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim); |
Harald Welte | c08f3b1 | 2011-02-05 19:50:44 +0100 | [diff] [blame] | 122 | if (!idata && prim == 0) |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 123 | return -EIO; |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 124 | |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 125 | msgb_pull(msg, 2); |
| 126 | |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 127 | DEBUGP(DMI, "prim %08x\n", prim); |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 128 | |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 129 | switch (prim) { |
| 130 | case 0: |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 131 | break; |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 132 | case LAPD_MPH_ACTIVATE_IND: |
| 133 | DEBUGP(DMI, "MPH_ACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei); |
Harald Welte | 4c826f7 | 2011-01-14 15:55:42 +0100 | [diff] [blame] | 134 | ret = e1inp_event(e1i_ts, S_INP_TEI_UP, tei, sapi); |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 135 | break; |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 136 | case LAPD_MPH_DEACTIVATE_IND: |
| 137 | DEBUGP(DMI, "MPH_DEACTIVATE_IND: sapi(%d) tei(%d)\n", sapi, tei); |
Harald Welte | 4c826f7 | 2011-01-14 15:55:42 +0100 | [diff] [blame] | 138 | ret = e1inp_event(e1i_ts, S_INP_TEI_DN, tei, sapi); |
Matthew Fredrickson | 73448bf | 2010-02-17 19:25:39 +0100 | [diff] [blame] | 139 | break; |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 140 | case LAPD_DL_DATA_IND: |
| 141 | case LAPD_DL_UNITDATA_IND: |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 142 | if (prim == LAPD_DL_DATA_IND) |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 143 | msg->l2h = msg->data + 2; |
| 144 | else |
| 145 | msg->l2h = msg->data + 1; |
| 146 | DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret)); |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 147 | ret = e1inp_rx_ts(e1i_ts, msg, tei, sapi); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 148 | break; |
| 149 | default: |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 150 | printf("ERROR: unknown prim\n"); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 151 | break; |
| 152 | } |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 153 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 154 | DEBUGP(DMI, "Returned ok\n"); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 159 | { |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 160 | /* We never include the DAHDI B-Channel FD into the |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 161 | * writeset, since it doesn't support poll() based |
Harald Welte | 075b562 | 2011-02-05 20:25:22 +0100 | [diff] [blame] | 162 | * write flow control */ |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 163 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) { |
| 164 | fprintf(stderr, "Trying to write TRAU ts\n"); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 165 | return 0; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 166 | } |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 167 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 168 | e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static void timeout_ts1_write(void *data) |
| 174 | { |
| 175 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 176 | |
| 177 | /* trigger write of ts1, due to tx delay timer */ |
| 178 | ts_want_write(e1i_ts); |
| 179 | } |
| 180 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 181 | static void dahdi_write_msg(uint8_t *data, int len, void *cbdata) |
| 182 | { |
| 183 | struct bsc_fd *bfd = cbdata; |
Harald Welte | a6ef1b2 | 2011-02-13 14:19:26 +0100 | [diff] [blame] | 184 | struct e1inp_line *line = bfd->data; |
| 185 | unsigned int ts_nr = bfd->priv_nr; |
| 186 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 187 | int ret; |
| 188 | |
| 189 | ret = write(bfd->fd, data, len + 2); |
Harald Welte | a6ef1b2 | 2011-02-13 14:19:26 +0100 | [diff] [blame] | 190 | if (ret == -1) |
| 191 | handle_dahdi_exception(e1i_ts); |
| 192 | else if (ret < 0) |
| 193 | LOGP(DMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 196 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 197 | { |
| 198 | struct e1inp_line *line = bfd->data; |
| 199 | unsigned int ts_nr = bfd->priv_nr; |
| 200 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 201 | struct e1inp_sign_link *sign_link; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 202 | struct msgb *msg; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 203 | |
| 204 | bfd->when &= ~BSC_FD_WRITE; |
| 205 | |
| 206 | /* get the next msg for this timeslot */ |
| 207 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 208 | if (!msg) { |
| 209 | /* no message after tx delay timer */ |
| 210 | return 0; |
| 211 | } |
| 212 | |
Harald Welte | 59c51e7 | 2011-02-12 13:09:12 +0100 | [diff] [blame^] | 213 | DEBUGP(DMI, "TX: %s\n", hexdump(msg->data, msg->len)); |
Harald Welte | cbaa38a | 2011-02-05 20:20:50 +0100 | [diff] [blame] | 214 | lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei, |
| 215 | sign_link->sapi, msg->data, msg->len); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 216 | msgb_free(msg); |
| 217 | |
| 218 | /* set tx delay timer for next event */ |
| 219 | e1i_ts->sign.tx_timer.cb = timeout_ts1_write; |
| 220 | e1i_ts->sign.tx_timer.data = e1i_ts; |
| 221 | bsc_schedule_timer(&e1i_ts->sign.tx_timer, 0, 50000); |
| 222 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 223 | return 0; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 226 | |
| 227 | static int invertbits = 1; |
| 228 | |
| 229 | static u_int8_t flip_table[256]; |
| 230 | |
| 231 | static void init_flip_bits(void) |
| 232 | { |
| 233 | int i,k; |
| 234 | |
| 235 | for (i = 0 ; i < 256 ; i++) { |
| 236 | u_int8_t sample = 0 ; |
| 237 | for (k = 0; k<8; k++) { |
| 238 | if ( i & 1 << k ) sample |= 0x80 >> k; |
| 239 | } |
| 240 | flip_table[i] = sample; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static u_int8_t * flip_buf_bits ( u_int8_t * buf , int len) |
| 245 | { |
| 246 | int i; |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 247 | u_int8_t * start = buf; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 248 | |
| 249 | for (i = 0 ; i < len; i++) { |
| 250 | buf[i] = flip_table[(u_int8_t)buf[i]]; |
| 251 | } |
| 252 | |
| 253 | return start; |
| 254 | } |
| 255 | |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 256 | #define D_BCHAN_TX_GRAN 160 |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 257 | /* write to a B channel TS */ |
| 258 | static int handle_tsX_write(struct bsc_fd *bfd) |
| 259 | { |
| 260 | struct e1inp_line *line = bfd->data; |
| 261 | unsigned int ts_nr = bfd->priv_nr; |
| 262 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 263 | u_int8_t tx_buf[D_BCHAN_TX_GRAN]; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 264 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 265 | int ret; |
| 266 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 267 | ret = subchan_mux_out(mx, tx_buf, D_BCHAN_TX_GRAN); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 268 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 269 | if (ret != D_BCHAN_TX_GRAN) { |
| 270 | fprintf(stderr, "Huh, got ret of %d\n", ret); |
| 271 | if (ret < 0) |
| 272 | return ret; |
| 273 | } |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 274 | |
| 275 | DEBUGP(DMIB, "BCHAN TX: %s\n", |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 276 | hexdump(tx_buf, D_BCHAN_TX_GRAN)); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 277 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 278 | if (invertbits) { |
| 279 | flip_buf_bits(tx_buf, ret); |
| 280 | } |
| 281 | |
| 282 | ret = write(bfd->fd, tx_buf, ret); |
| 283 | if (ret < D_BCHAN_TX_GRAN) |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 284 | fprintf(stderr, "send returns %d instead of %d\n", ret, |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 285 | D_BCHAN_TX_GRAN); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 290 | #define D_TSX_ALLOC_SIZE (D_BCHAN_TX_GRAN) |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 291 | /* FIXME: read from a B channel TS */ |
| 292 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 293 | { |
| 294 | struct e1inp_line *line = bfd->data; |
| 295 | unsigned int ts_nr = bfd->priv_nr; |
| 296 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 297 | struct msgb *msg = msgb_alloc(D_TSX_ALLOC_SIZE, "DAHDI TSx"); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 298 | int ret; |
| 299 | |
| 300 | if (!msg) |
| 301 | return -ENOMEM; |
| 302 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 303 | ret = read(bfd->fd, msg->data, D_TSX_ALLOC_SIZE); |
| 304 | if (ret < 0 || ret != D_TSX_ALLOC_SIZE) { |
| 305 | fprintf(stderr, "read error %d %s\n", ret, strerror(errno)); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 306 | return ret; |
| 307 | } |
| 308 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 309 | if (invertbits) { |
| 310 | flip_buf_bits(msg->data, ret); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 311 | } |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 312 | |
| 313 | msgb_put(msg, ret); |
| 314 | |
| 315 | msg->l2h = msg->data; |
| 316 | DEBUGP(DMIB, "BCHAN RX: %s\n", |
| 317 | hexdump(msgb_l2(msg), ret)); |
| 318 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 319 | /* physical layer indicates that data has been sent, |
| 320 | * we thus can send some more data */ |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 321 | ret = handle_tsX_write(bfd); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 322 | msgb_free(msg); |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | /* callback from select.c in case one of the fd's can be read/written */ |
| 328 | static int dahdi_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 329 | { |
| 330 | struct e1inp_line *line = bfd->data; |
| 331 | unsigned int ts_nr = bfd->priv_nr; |
| 332 | unsigned int idx = ts_nr-1; |
| 333 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 334 | int rc = 0; |
| 335 | |
| 336 | switch (e1i_ts->type) { |
| 337 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | fccc25c | 2011-02-13 15:41:25 +0100 | [diff] [blame] | 338 | if (what & BSC_FD_EXCEPT) |
| 339 | handle_dahdi_exception(e1i_ts); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 340 | if (what & BSC_FD_READ) |
| 341 | rc = handle_ts1_read(bfd); |
| 342 | if (what & BSC_FD_WRITE) |
| 343 | rc = handle_ts1_write(bfd); |
| 344 | break; |
| 345 | case E1INP_TS_TYPE_TRAU: |
Harald Welte | fccc25c | 2011-02-13 15:41:25 +0100 | [diff] [blame] | 346 | if (what & BSC_FD_EXCEPT) |
| 347 | handle_dahdi_exception(e1i_ts); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 348 | if (what & BSC_FD_READ) |
| 349 | rc = handle_tsX_read(bfd); |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 350 | if (what & BSC_FD_WRITE) |
| 351 | rc = handle_tsX_write(bfd); |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 352 | /* We never include the DAHDI B-Channel FD into the |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 353 | * writeset, since it doesn't support poll() based |
Harald Welte | 075b562 | 2011-02-05 20:25:22 +0100 | [diff] [blame] | 354 | * write flow control */ |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 355 | break; |
| 356 | default: |
| 357 | fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type); |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | return rc; |
| 362 | } |
| 363 | |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 364 | static int dahdi_e1_line_update(struct e1inp_line *line); |
| 365 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 366 | struct e1inp_driver dahdi_driver = { |
| 367 | .name = "DAHDI", |
| 368 | .want_write = ts_want_write, |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 369 | .line_update = &dahdi_e1_line_update, |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 370 | }; |
| 371 | |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 372 | void dahdi_set_bufinfo(int fd, int as_sigchan) |
| 373 | { |
| 374 | struct dahdi_bufferinfo bi; |
| 375 | int x = 0; |
| 376 | |
| 377 | if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) { |
| 378 | fprintf(stderr, "Error getting bufinfo\n"); |
| 379 | exit(-1); |
| 380 | } |
| 381 | |
| 382 | if (as_sigchan) { |
| 383 | bi.numbufs = 4; |
| 384 | bi.bufsize = 512; |
| 385 | } else { |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 386 | bi.numbufs = 8; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 387 | bi.bufsize = D_BCHAN_TX_GRAN; |
creslin287 | 52b0e02 | 2010-03-26 12:57:31 -0500 | [diff] [blame] | 388 | bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) { |
| 392 | fprintf(stderr, "Error setting bufinfo\n"); |
| 393 | exit(-1); |
| 394 | } |
| 395 | |
| 396 | if (!as_sigchan) { |
| 397 | if (ioctl(fd, DAHDI_AUDIOMODE, &x)) { |
| 398 | fprintf(stderr, "Error setting bufinfo\n"); |
| 399 | exit(-1); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | } |
| 404 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 405 | static int dahdi_e1_setup(struct e1inp_line *line) |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 406 | { |
| 407 | int ts, ret; |
| 408 | |
| 409 | /* TS0 is CRC4, don't need any fd for it */ |
| 410 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
| 411 | unsigned int idx = ts-1; |
| 412 | char openstr[128]; |
| 413 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 414 | struct bsc_fd *bfd = &e1i_ts->driver.dahdi.fd; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 415 | |
| 416 | bfd->data = line; |
| 417 | bfd->priv_nr = ts; |
| 418 | bfd->cb = dahdi_fd_cb; |
| 419 | snprintf(openstr, sizeof(openstr), "/dev/dahdi/%d", ts); |
| 420 | |
| 421 | switch (e1i_ts->type) { |
| 422 | case E1INP_TS_TYPE_NONE: |
| 423 | continue; |
| 424 | break; |
| 425 | case E1INP_TS_TYPE_SIGN: |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 426 | bfd->fd = open(openstr, O_RDWR | O_NONBLOCK); |
| 427 | if (bfd->fd == -1) { |
| 428 | fprintf(stderr, "%s could not open %s %s\n", |
| 429 | __func__, openstr, strerror(errno)); |
| 430 | exit(-1); |
| 431 | } |
Harald Welte | fccc25c | 2011-02-13 15:41:25 +0100 | [diff] [blame] | 432 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 433 | dahdi_set_bufinfo(bfd->fd, 1); |
Harald Welte | 0a47400 | 2011-02-11 18:34:51 +0100 | [diff] [blame] | 434 | e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(1, dahdi_write_msg, bfd); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 435 | break; |
| 436 | case E1INP_TS_TYPE_TRAU: |
Matthew Fredrickson | 8f58bbe | 2010-03-15 12:22:37 -0500 | [diff] [blame] | 437 | bfd->fd = open(openstr, O_RDWR | O_NONBLOCK); |
| 438 | if (bfd->fd == -1) { |
| 439 | fprintf(stderr, "%s could not open %s %s\n", |
| 440 | __func__, openstr, strerror(errno)); |
| 441 | exit(-1); |
| 442 | } |
| 443 | dahdi_set_bufinfo(bfd->fd, 0); |
Harald Welte | 6aef70d | 2011-02-04 21:33:14 +0100 | [diff] [blame] | 444 | /* We never include the DAHDI B-Channel FD into the |
Harald Welte | 075b562 | 2011-02-05 20:25:22 +0100 | [diff] [blame] | 445 | * writeset, since it doesn't support poll() based |
| 446 | * write flow control */ |
Harald Welte | fccc25c | 2011-02-13 15:41:25 +0100 | [diff] [blame] | 447 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT;// | BSC_FD_WRITE; |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | |
| 451 | if (bfd->fd < 0) { |
| 452 | fprintf(stderr, "%s could not open %s %s\n", |
| 453 | __func__, openstr, strerror(errno)); |
| 454 | return bfd->fd; |
| 455 | } |
| 456 | |
| 457 | ret = bsc_register_fd(bfd); |
| 458 | if (ret < 0) { |
| 459 | fprintf(stderr, "could not register FD: %s\n", |
| 460 | strerror(ret)); |
| 461 | return ret; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 468 | static int dahdi_e1_line_update(struct e1inp_line *line) |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 469 | { |
Matthew Fredrickson | 124c80e | 2010-02-16 22:01:36 +0100 | [diff] [blame] | 470 | if (line->driver != &dahdi_driver) |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 471 | return -EINVAL; |
| 472 | |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 473 | return dahdi_e1_setup(line); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Harald Welte | 890fe4a | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 476 | int e1inp_dahdi_init(void) |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 477 | { |
Harald Welte | 306a206 | 2011-02-05 19:13:00 +0100 | [diff] [blame] | 478 | init_flip_bits(); |
| 479 | |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 480 | /* register the driver with the core */ |
Harald Welte | 348ffa8 | 2011-02-05 15:30:48 +0100 | [diff] [blame] | 481 | return e1inp_driver_register(&dahdi_driver); |
Matthew Fredrickson | 7a9f985 | 2010-02-16 21:55:50 +0100 | [diff] [blame] | 482 | } |
Harald Welte | c7841cc | 2011-02-05 15:13:27 +0100 | [diff] [blame] | 483 | |
| 484 | #endif /* HAVE_DAHDI_USER_H */ |