Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 1 | /*! \file i460_mux.c |
| 2 | * ITU-T I.460 sub-channel multiplexer + demultiplexer */ |
| 3 | /* |
| 4 | * (C) 2020 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include <osmocom/core/bits.h> |
| 22 | #include <osmocom/core/utils.h> |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/gsm/i460_mux.h> |
| 25 | |
| 26 | /* count the number of sub-channels in this I460 slot */ |
| 27 | static int osmo_i460_subchan_count(struct osmo_i460_timeslot *ts) |
| 28 | { |
| 29 | int i, num_used = 0; |
| 30 | |
| 31 | for (i = 0; i < ARRAY_SIZE(ts->schan); i++) { |
| 32 | if (ts->schan[i].rate != OSMO_I460_RATE_NONE) |
| 33 | num_used++; |
| 34 | } |
| 35 | |
| 36 | return num_used; |
| 37 | } |
| 38 | |
| 39 | /* does this channel have no sub-streams (single 64k subchannel)? */ |
| 40 | static bool osmo_i460_has_single_64k_schan(struct osmo_i460_timeslot *ts) |
| 41 | { |
| 42 | if (osmo_i460_subchan_count(ts) != 1) |
| 43 | return false; |
| 44 | |
| 45 | if (ts->schan[0].rate != OSMO_I460_RATE_64k) |
| 46 | return false; |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /*********************************************************************** |
| 52 | * Demultiplexer |
| 53 | ***********************************************************************/ |
| 54 | |
| 55 | /* append a single bit to a sub-channel */ |
| 56 | static void demux_subchan_append_bit(struct osmo_i460_subchan *schan, uint8_t bit) |
| 57 | { |
| 58 | struct osmo_i460_subchan_demux *demux = &schan->demux; |
| 59 | |
| 60 | OSMO_ASSERT(demux->out_bitbuf); |
| 61 | OSMO_ASSERT(demux->out_idx < demux->out_bitbuf_size); |
| 62 | |
| 63 | demux->out_bitbuf[demux->out_idx++] = bit ? 1 : 0; |
| 64 | |
| 65 | if (demux->out_idx >= demux->out_bitbuf_size) { |
| 66 | if (demux->out_cb_bits) |
Harald Welte | b3b474d | 2020-08-02 11:54:56 +0200 | [diff] [blame] | 67 | demux->out_cb_bits(schan, demux->user_data, demux->out_bitbuf, demux->out_idx); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 68 | else { |
| 69 | /* pack bits into bytes */ |
| 70 | OSMO_ASSERT((demux->out_idx % 8) == 0); |
| 71 | unsigned int num_bytes = demux->out_idx / 8; |
| 72 | uint8_t bytes[num_bytes]; |
| 73 | osmo_ubit2pbit(bytes, demux->out_bitbuf, demux->out_idx); |
Harald Welte | b3b474d | 2020-08-02 11:54:56 +0200 | [diff] [blame] | 74 | demux->out_cb_bytes(schan, demux->user_data, bytes, num_bytes); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 75 | } |
| 76 | demux->out_idx = 0; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* extract those bits relevant to this schan of each byte in 'data' */ |
| 81 | static void demux_subchan_extract_bits(struct osmo_i460_subchan *schan, const uint8_t *data, size_t data_len) |
| 82 | { |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < data_len; i++) { |
| 86 | uint8_t inbyte = data[i]; |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 87 | /* I.460 defines sub-channel 0 is using bit positions 1+2 (the two |
| 88 | * most significant bits, hence we extract msb-first */ |
| 89 | uint8_t inbits = inbyte << schan->bit_offset; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 90 | |
| 91 | /* extract the bits relevant to the given schan */ |
| 92 | switch (schan->rate) { |
| 93 | case OSMO_I460_RATE_8k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 94 | demux_subchan_append_bit(schan, inbits & 0x80); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 95 | break; |
| 96 | case OSMO_I460_RATE_16k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 97 | demux_subchan_append_bit(schan, inbits & 0x80); |
| 98 | demux_subchan_append_bit(schan, inbits & 0x40); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 99 | break; |
| 100 | case OSMO_I460_RATE_32k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 101 | demux_subchan_append_bit(schan, inbits & 0x80); |
| 102 | demux_subchan_append_bit(schan, inbits & 0x40); |
| 103 | demux_subchan_append_bit(schan, inbits & 0x20); |
| 104 | demux_subchan_append_bit(schan, inbits & 0x10); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 105 | break; |
| 106 | case OSMO_I460_RATE_64k: |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 107 | demux_subchan_append_bit(schan, inbits & 0x80); |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 108 | demux_subchan_append_bit(schan, inbits & 0x40); |
| 109 | demux_subchan_append_bit(schan, inbits & 0x20); |
| 110 | demux_subchan_append_bit(schan, inbits & 0x10); |
| 111 | demux_subchan_append_bit(schan, inbits & 0x08); |
| 112 | demux_subchan_append_bit(schan, inbits & 0x04); |
| 113 | demux_subchan_append_bit(schan, inbits & 0x02); |
| 114 | demux_subchan_append_bit(schan, inbits & 0x01); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 115 | break; |
| 116 | default: |
| 117 | OSMO_ASSERT(0); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /*! Data from E1 timeslot into de-multiplexer |
| 123 | * \param[in] ts timeslot state |
| 124 | * \param[in] data input data bytes as received from E1/T1 |
| 125 | * \param[in] data_len length of data in bytes */ |
| 126 | void osmo_i460_demux_in(struct osmo_i460_timeslot *ts, const uint8_t *data, size_t data_len) |
| 127 | { |
| 128 | struct osmo_i460_subchan *schan; |
| 129 | struct osmo_i460_subchan_demux *demux; |
| 130 | int i; |
| 131 | |
| 132 | /* fast path if entire 64k slot is used */ |
| 133 | if (osmo_i460_has_single_64k_schan(ts)) { |
| 134 | schan = &ts->schan[0]; |
| 135 | demux = &schan->demux; |
| 136 | if (demux->out_cb_bytes) |
Harald Welte | b3b474d | 2020-08-02 11:54:56 +0200 | [diff] [blame] | 137 | demux->out_cb_bytes(schan, demux->user_data, data, data_len); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 138 | else { |
| 139 | ubit_t bits[data_len*8]; |
| 140 | osmo_pbit2ubit(bits, data, data_len*8); |
Harald Welte | b3b474d | 2020-08-02 11:54:56 +0200 | [diff] [blame] | 141 | demux->out_cb_bits(schan, demux->user_data, bits, data_len*8); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 142 | } |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | /* Slow path iterating over all lchans */ |
| 147 | for (i = 0; i < ARRAY_SIZE(ts->schan); i++) { |
| 148 | schan = &ts->schan[i]; |
| 149 | if (schan->rate == OSMO_I460_RATE_NONE) |
| 150 | continue; |
| 151 | demux_subchan_extract_bits(schan, data, data_len); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /*********************************************************************** |
| 157 | * Multiplexer |
| 158 | ***********************************************************************/ |
| 159 | |
| 160 | /*! enqueue a to-be-transmitted message buffer containing unpacked bits */ |
| 161 | void osmo_i460_mux_enqueue(struct osmo_i460_subchan *schan, struct msgb *msg) |
| 162 | { |
| 163 | OSMO_ASSERT(msgb_length(msg) > 0); |
| 164 | msgb_enqueue(&schan->mux.tx_queue, msg); |
| 165 | } |
| 166 | |
| 167 | /* mux: pull the next bit out of the given sub-channel */ |
| 168 | static ubit_t mux_schan_provide_bit(struct osmo_i460_subchan *schan) |
| 169 | { |
| 170 | struct osmo_i460_subchan_mux *mux = &schan->mux; |
| 171 | struct msgb *msg; |
| 172 | ubit_t bit; |
| 173 | |
| 174 | /* if we don't have anything to transmit, return '1' bits */ |
Philipp Maier | b5518a8 | 2020-07-31 19:04:00 +0200 | [diff] [blame] | 175 | if (llist_empty(&mux->tx_queue)) { |
| 176 | /* User code now has a last chance to put something into the queue. */ |
| 177 | if (mux->in_cb_queue_empty) |
Harald Welte | b3b474d | 2020-08-02 11:54:56 +0200 | [diff] [blame] | 178 | mux->in_cb_queue_empty(schan, mux->user_data); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 179 | |
Philipp Maier | b5518a8 | 2020-07-31 19:04:00 +0200 | [diff] [blame] | 180 | /* If the queue is still empty, return idle bits */ |
| 181 | if (llist_empty(&mux->tx_queue)) |
| 182 | return 0x01; |
| 183 | } |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 184 | msg = llist_entry(mux->tx_queue.next, struct msgb, list); |
| 185 | bit = msgb_pull_u8(msg); |
| 186 | |
| 187 | /* free msgb if we have pulled the last bit */ |
| 188 | if (msgb_length(msg) <= 0) { |
| 189 | llist_del(&msg->list); |
| 190 | talloc_free(msg); |
| 191 | } |
| 192 | |
| 193 | return bit; |
| 194 | } |
| 195 | |
| 196 | /*! provide one byte with the subchan-specific bits of given sub-channel. |
| 197 | * \param[in] schan sub-channel that is to provide bits |
| 198 | * \parma[out] mask bitmask of those bits filled in |
| 199 | * \returns bits of given sub-channel */ |
| 200 | static uint8_t mux_subchan_provide_bits(struct osmo_i460_subchan *schan, uint8_t *mask) |
| 201 | { |
| 202 | uint8_t outbits = 0; |
| 203 | uint8_t outmask; |
| 204 | |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 205 | /* I.460 defines sub-channel 0 is using bit positions 1+2 (the two |
| 206 | * most significant bits, hence we provide msb-first */ |
| 207 | |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 208 | switch (schan->rate) { |
| 209 | case OSMO_I460_RATE_8k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 210 | outbits = mux_schan_provide_bit(schan) << 7; |
| 211 | outmask = 0x80; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 212 | break; |
| 213 | case OSMO_I460_RATE_16k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 214 | outbits |= mux_schan_provide_bit(schan) << 7; |
| 215 | outbits |= mux_schan_provide_bit(schan) << 6; |
| 216 | outmask = 0xC0; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 217 | break; |
| 218 | case OSMO_I460_RATE_32k: |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 219 | outbits |= mux_schan_provide_bit(schan) << 7; |
| 220 | outbits |= mux_schan_provide_bit(schan) << 6; |
| 221 | outbits |= mux_schan_provide_bit(schan) << 5; |
| 222 | outbits |= mux_schan_provide_bit(schan) << 4; |
| 223 | outmask = 0xF0; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 224 | break; |
| 225 | case OSMO_I460_RATE_64k: |
| 226 | outbits |= mux_schan_provide_bit(schan) << 7; |
| 227 | outbits |= mux_schan_provide_bit(schan) << 6; |
| 228 | outbits |= mux_schan_provide_bit(schan) << 5; |
| 229 | outbits |= mux_schan_provide_bit(schan) << 4; |
| 230 | outbits |= mux_schan_provide_bit(schan) << 3; |
| 231 | outbits |= mux_schan_provide_bit(schan) << 2; |
| 232 | outbits |= mux_schan_provide_bit(schan) << 1; |
| 233 | outbits |= mux_schan_provide_bit(schan) << 0; |
| 234 | outmask = 0xFF; |
| 235 | break; |
| 236 | default: |
| 237 | OSMO_ASSERT(0); |
| 238 | } |
Harald Welte | 4496498 | 2020-08-02 21:54:30 +0200 | [diff] [blame] | 239 | *mask = outmask >> schan->bit_offset; |
| 240 | return outbits >> schan->bit_offset; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* provide one byte of multiplexed I.460 bits */ |
| 244 | static uint8_t mux_timeslot_provide_bits(struct osmo_i460_timeslot *ts) |
| 245 | { |
| 246 | int i, count = 0; |
| 247 | uint8_t ret = 0xff; /* unused bits must be '1' as per I.460 */ |
| 248 | |
| 249 | for (i = 0; i < ARRAY_SIZE(ts->schan); i++) { |
| 250 | struct osmo_i460_subchan *schan = &ts->schan[i]; |
| 251 | uint8_t bits, mask; |
| 252 | |
| 253 | if (schan->rate == OSMO_I460_RATE_NONE) |
| 254 | continue; |
| 255 | count++; |
| 256 | bits = mux_subchan_provide_bits(schan, &mask); |
| 257 | ret &= ~mask; |
| 258 | ret |= bits; |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /*! Data from E1 timeslot into de-multiplexer |
| 266 | * \param[in] ts timeslot state |
| 267 | * \param[out] out caller-provided buffer where to store generated output bytes |
| 268 | * \param[in] out_len number of bytes to be stored at out |
| 269 | */ |
| 270 | int osmo_i460_mux_out(struct osmo_i460_timeslot *ts, uint8_t *out, size_t out_len) |
| 271 | { |
| 272 | int i; |
| 273 | |
| 274 | /* fast path if entire 64k slot is used */ |
| 275 | //if (osmo_i460_has_single_64k_schan(ts)) { } |
| 276 | |
| 277 | for (i = 0; i < out_len; i++) |
| 278 | out[i] = mux_timeslot_provide_bits(ts); |
| 279 | |
| 280 | return out_len; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /*********************************************************************** |
| 285 | * Initialization / Control |
| 286 | ***********************************************************************/ |
| 287 | |
| 288 | |
| 289 | static int alloc_bitbuf(void *ctx, struct osmo_i460_subchan *schan, size_t num_bits) |
| 290 | { |
| 291 | struct osmo_i460_subchan_demux *demux = &schan->demux; |
| 292 | |
| 293 | talloc_free(demux->out_bitbuf); |
| 294 | demux->out_bitbuf = talloc_zero_size(ctx, num_bits); |
| 295 | if (!demux->out_bitbuf) |
| 296 | return -ENOMEM; |
| 297 | demux->out_bitbuf_size = num_bits; |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static int find_unused_subchan_idx(const struct osmo_i460_timeslot *ts) |
| 304 | { |
| 305 | int i; |
| 306 | |
| 307 | for (i = 0; i < ARRAY_SIZE(ts->schan); i++) { |
| 308 | const struct osmo_i460_subchan *schan = &ts->schan[i]; |
| 309 | if (schan->rate == OSMO_I460_RATE_NONE) |
| 310 | return i; |
| 311 | } |
| 312 | return -1; |
| 313 | } |
| 314 | |
Philipp Maier | 6509d20 | 2020-07-24 21:52:56 +0200 | [diff] [blame] | 315 | /* reset subchannel struct into a defined state */ |
| 316 | static void subchan_reset(struct osmo_i460_subchan *schan, bool first_time) |
| 317 | { |
| 318 | /* Before we zero out the subchannel struct, we must be sure that the |
| 319 | * tx_queue is cleared and all dynamically allocated memory is freed. |
| 320 | * However, on an uninitalized subchannel struct we can not be sure |
| 321 | * that the pointers are valid. If the subchannel is reset the first |
| 322 | * time the caller must set first_time to true. */ |
| 323 | if (!first_time) { |
| 324 | if (schan->demux.out_bitbuf) |
| 325 | talloc_free(schan->demux.out_bitbuf); |
| 326 | msgb_queue_free(&schan->mux.tx_queue); |
| 327 | } |
| 328 | |
| 329 | /* Reset subchannel to a defined state */ |
| 330 | memset(schan, 0, sizeof(*schan)); |
| 331 | schan->rate = OSMO_I460_RATE_NONE; |
| 332 | INIT_LLIST_HEAD(&schan->mux.tx_queue); |
| 333 | } |
| 334 | |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 335 | /*! initialize an I.460 timeslot */ |
| 336 | void osmo_i460_ts_init(struct osmo_i460_timeslot *ts) |
| 337 | { |
| 338 | int i; |
| 339 | |
| 340 | for (i = 0; i < ARRAY_SIZE(ts->schan); i++) { |
| 341 | struct osmo_i460_subchan *schan = &ts->schan[i]; |
Harald Welte | eb8240d | 2020-08-02 11:54:15 +0200 | [diff] [blame] | 342 | schan->ts = ts; |
Philipp Maier | 6509d20 | 2020-07-24 21:52:56 +0200 | [diff] [blame] | 343 | subchan_reset(schan, true); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | /*! add a new sub-channel to the given timeslot |
| 348 | * \param[in] ctx talloc context from where to allocate the internal buffer |
| 349 | * \param[in] ts timeslot to which to add a sub-channel |
| 350 | * \param[in] chd description of the sub-channel to be added |
| 351 | * \return pointer to sub-channel on success, NULL on error */ |
| 352 | struct osmo_i460_subchan * |
| 353 | osmo_i460_subchan_add(void *ctx, struct osmo_i460_timeslot *ts, const struct osmo_i460_schan_desc *chd) |
| 354 | { |
| 355 | struct osmo_i460_subchan *schan; |
| 356 | int idx, rc; |
| 357 | |
| 358 | idx = find_unused_subchan_idx(ts); |
| 359 | if (idx < 0) |
| 360 | return NULL; |
| 361 | |
| 362 | schan = &ts->schan[idx]; |
| 363 | |
| 364 | schan->rate = chd->rate; |
| 365 | schan->bit_offset = chd->bit_offset; |
| 366 | |
| 367 | schan->demux.out_cb_bits = chd->demux.out_cb_bits; |
| 368 | schan->demux.out_cb_bytes = chd->demux.out_cb_bytes; |
| 369 | schan->demux.user_data = chd->demux.user_data; |
Philipp Maier | b5518a8 | 2020-07-31 19:04:00 +0200 | [diff] [blame] | 370 | schan->mux.in_cb_queue_empty = chd->mux.in_cb_queue_empty; |
| 371 | schan->mux.user_data = chd->mux.user_data; |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 372 | rc = alloc_bitbuf(ctx, schan, chd->demux.num_bits); |
| 373 | if (rc < 0) { |
Philipp Maier | 6509d20 | 2020-07-24 21:52:56 +0200 | [diff] [blame] | 374 | subchan_reset(schan, false); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | /* return number of schan in use */ |
| 379 | return schan; |
| 380 | } |
| 381 | |
| 382 | /* remove a su-channel from the multiplex */ |
| 383 | void osmo_i460_subchan_del(struct osmo_i460_subchan *schan) |
| 384 | { |
Philipp Maier | 6509d20 | 2020-07-24 21:52:56 +0200 | [diff] [blame] | 385 | subchan_reset(schan, false); |
Harald Welte | b795f03 | 2020-05-14 11:42:53 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /*! @} */ |