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