* add trau_mux implementation to relay from one incoming TRAU
  channel to another one (simple voice call switching)
* add a way more generic E1 input layer, abstracting out the misdn
  low-level interface. This also adds infrastructure for multiple TRX
  in one BTS, as well as multiple BTS on one E1 link
* add a E1 subchannel multiplexer for sending multiple 16kbit sub-channels
  one one 64kBps E1 channel
* add TRAU IDLE frame generation
* terminate bsc_hack in case there is a E1 / mISDN init error
* introduce 'e1_config.c' file with static configuration of our
  E1 setup (which TRX/BTS is configured for which TEI/SAPI/E1). This should
  later become a config file rather than a compiled C file.

WARNING: all this compiles but is not tested yet.  Expect fix-up committs over
the next hours or so

diff --git a/src/subchan_demux.c b/src/subchan_demux.c
index da1b090..78ba2da 100644
--- a/src/subchan_demux.c
+++ b/src/subchan_demux.c
@@ -1,4 +1,4 @@
-/* A E1 sub-channel demultiplexer with TRAU frame sync */
+/* A E1 sub-channel (de)multiplexer with TRAU frame sync */
 
 /* (C) 2009 by Harald Welte <laforge@gnumonks.org>
  * All Rights Reserved
@@ -26,8 +26,9 @@
 #include <errno.h>
 
 #include <openbsc/subchan_demux.h>
+#include <openbsc/trau_frame.h>
 
-static inline void append_bit(struct subch *sch, u_int8_t bit)
+static inline void append_bit(struct demux_subch *sch, u_int8_t bit)
 {
 	sch->out_bitbuf[sch->out_idx++] = bit;
 }
@@ -37,7 +38,7 @@
 
 /* check if we have just completed the 16 bit zero sync header,
  * in accordance with GSM TS 08.60 Chapter 4.8.1 */
-static int sync_hdr_complete(struct subch *sch)
+static int sync_hdr_complete(struct demux_subch *sch)
 {
 	int rc;
 	int bits_at_end = 0;
@@ -62,7 +63,7 @@
 }
 
 /* resynchronize to current location */
-static void resync_to_here(struct subch *sch)
+static void resync_to_here(struct demux_subch *sch)
 {
 #if 0
 	u_int8_t tmp[TRAU_FRAME_BITS];
@@ -98,7 +99,7 @@
 
 	dmx->chan_activ = 0;
 	for (i = 0; i < NR_SUBCH; i++) {
-		struct subch *sch = &dmx->subch[i];
+		struct demux_subch *sch = &dmx->subch[i];
 		sch->out_idx = 0;
 		memset(sch->out_bitbuf, 0xff, sizeof(sch->out_bitbuf));
 	}
@@ -119,7 +120,7 @@
 		u_int8_t inbyte = data[i];
 
 		for (c = 0; c < NR_SUBCH; c++) {
-			struct subch *sch = &dmx->subch[c];
+			struct demux_subch *sch = &dmx->subch[c];
 			u_int8_t bit;
 
 			/* ignore inactive subchannels */
@@ -177,3 +178,134 @@
 	dmx->chan_activ &= ~(1 << subch);
 	return 0;
 }
+
+/* MULTIPLEXER */
+
+static int alloc_add_idle_frame(struct subch_mux *mx, int sch_nr)
+{
+	/* FIXME: allocate and initialize with idle pattern */
+	return subchan_mux_enqueue(mx, sch_nr, trau_idle_frame(),
+				   TRAU_FRAME_BITS);
+}
+
+/* return the requested number of bits from the specified subchannel */
+static int get_subch_bits(struct subch_mux *mx, int subch,
+			  u_int8_t *bits, int num_requested)
+{
+	struct mux_subch *sch = &mx->subch[subch];
+	int num_bits = 0;
+
+	while (num_bits < num_requested) {
+		struct subch_txq_entry *txe;
+		int num_bits_left;
+		int num_bits_thistime;
+
+		/* make sure we have a valid entry at top of tx queue.
+		 * if not, add an idle frame */
+		if (llist_empty(&sch->tx_queue))	
+			alloc_add_idle_frame(mx, subch);
+	
+		if (llist_empty(&sch->tx_queue))
+			return -EIO;
+
+		txe = llist_entry(&sch->tx_queue, struct subch_txq_entry, list);
+		num_bits_left = txe->bit_len - txe->next_bit;
+
+		if (num_bits_left < num_requested)
+			num_bits_thistime = num_bits_left;
+		else
+			num_bits_thistime = num_requested;
+
+		/* pull the bits from the txe */
+		memcpy(bits + num_bits, txe->bits + txe->next_bit, num_requested);
+		txe->next_bit += num_bits_thistime;
+
+		/* free the tx_queue entry if it is fully consumed */
+		if (txe->next_bit >= txe->bit_len) {
+			llist_del(&txe->list);
+			free(txe);
+		}
+
+		/* increment global number of bits dequeued */
+		num_bits += num_bits_thistime;
+	}
+
+	return num_requested;
+}
+
+/* compact an array of 8 single-bit bytes into one byte of 8 bits */
+static u_int8_t compact_bits(u_int8_t *bits)
+{
+	u_int8_t ret = 0;
+	int i;
+
+	for (i = 0; i < 8; i++)
+		ret |= (bits[i] ? 1 : 0) << i;
+
+	return ret;
+}
+
+/* obtain a single output byte from the subchannel muxer */
+static int mux_output_byte(struct subch_mux *mx, u_int8_t *byte)
+{
+	u_int8_t bits[8];
+	int rc;
+
+	/* combine two bits of every subchan */
+	rc = get_subch_bits(mx, 0, &bits[0], 2);
+	rc = get_subch_bits(mx, 1, &bits[2], 2);
+	rc = get_subch_bits(mx, 2, &bits[4], 2);
+	rc = get_subch_bits(mx, 3, &bits[6], 2);
+
+	if (!rc)
+		*byte = compact_bits(bits);
+
+	return rc;
+}
+
+/* Request the output of some muxed bytes from the subchan muxer */
+int subchan_mux_out(struct subch_mux *mx, u_int8_t *data, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		int rc;
+		rc = mux_output_byte(mx, &data[i]);
+		if (rc < 0)
+			break;
+	}
+	return i;
+}
+
+/* enqueue some data into the tx_queue of a given subchannel */
+int subchan_mux_enqueue(struct subch_mux *mx, int s_nr, const u_int8_t *data,
+			int len)
+{
+	struct mux_subch *sch = &mx->subch[s_nr];
+	struct subch_txq_entry *tqe = malloc(sizeof(*tqe) + len);
+
+	if (!tqe)
+		return -ENOMEM;
+
+	memset(tqe, 0, sizeof(*tqe));
+	tqe->bit_len = len;
+	memcpy(tqe->bits, data, len);
+
+	llist_add(&tqe->list, &sch->tx_queue);
+
+	return 0;
+}
+
+/* initialize one subchannel muxer instance */
+int subchan_mux_init(struct subch_mux *mx)
+{
+	int i;
+
+	memset(mx, 0, sizeof(*mx));
+	for (i = 0; i < NR_SUBCH; i++) {
+		struct mux_subch *sch = &mx->subch[i];
+		INIT_LLIST_HEAD(&sch->tx_queue);
+	}
+
+	return 0;
+}