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