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