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