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