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