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