Get rid of allocating first timeslot at tfi_alloc

This simpliefies the allocation process.

tfi_alloc is responsible to allocate a TFI, not a time slot.

The first time slot available depends on multislot class and on other
ongoing TBF (concurrent TBFs), so it is part of the allocation
algorithm to select it.
diff --git a/src/gprs_bssgp_pcu.cpp b/src/gprs_bssgp_pcu.cpp
index 414e96b..6ea9920 100644
--- a/src/gprs_bssgp_pcu.cpp
+++ b/src/gprs_bssgp_pcu.cpp
@@ -192,7 +192,8 @@
 				tbf->ms_class = ms_class;
 		}
 	} else {
-		uint8_t trx, ts, use_trx, first_ts, ta, ss;
+		uint8_t trx, ta, ss;
+		int8_t use_trx;
 		struct gprs_rlcmac_tbf *old_tbf;
 
 		/* check for uplink data, so we copy our informations */
@@ -200,27 +201,25 @@
 		if (tbf && tbf->dir.ul.contention_resolution_done
 		 && !tbf->dir.ul.final_ack_sent) {
 			use_trx = tbf->trx;
-			first_ts = tbf->first_ts;
 			ta = tbf->ta;
 			ss = 0;
 			old_tbf = tbf;
 		} else {
 			use_trx = -1;
-			first_ts = -1;
 			ta = 0; /* FIXME: initial TA */
 			ss = 1; /* PCH assignment only allows one timeslot */
 			old_tbf = NULL;
 		}
 
 		// Create new TBF (any TRX)
-		tfi = tfi_alloc(GPRS_RLCMAC_DL_TBF, &trx, &ts, use_trx, first_ts);
+		tfi = tfi_alloc(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
 		if (tfi < 0) {
 			LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
 			/* FIXME: send reject */
 			return -EBUSY;
 		}
 		/* set number of downlink slots according to multislot class */
-		tbf = tbf_alloc(tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ts, ms_class,
+		tbf = tbf_alloc(tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ms_class,
 			ss);
 		if (!tbf) {
 			LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
diff --git a/src/gprs_rlcmac.cpp b/src/gprs_rlcmac.cpp
index ee76e82..ecc39fe 100644
--- a/src/gprs_rlcmac.cpp
+++ b/src/gprs_rlcmac.cpp
@@ -175,9 +175,8 @@
 
 /* FIXME: spread ressources over multiple TRX. Also add option to use same
  * TRX in case of existing TBF for TLLI in the other direction. */
-/* search for free TFI and return TFI, TRX and first TS */
-int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, uint8_t *_ts,
-	int8_t use_trx, int8_t first_ts)
+/* search for free TFI and return TFI, TRX */
+int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, int8_t use_trx)
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
 	struct gprs_rlcmac_pdch *pdch;
@@ -190,12 +189,10 @@
 		trx_from = 0;
 		trx_to = 7;
 	}
-	if (first_ts < 0 || first_ts >= 8)
-		first_ts = 0;
 
 	/* on TRX find first enabled TS */
 	for (trx = trx_from; trx <= trx_to; trx++) {
-		for (ts = first_ts; ts < 8; ts++) {
+		for (ts = 0; ts < 8; ts++) {
 			pdch = &bts->trx[trx].pdch[ts];
 			if (!pdch->enable)
 				continue;
@@ -224,7 +221,6 @@
 	if (tfi < 32) {
 		LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
 		*_trx = trx;
-		*_ts = ts;
 		return tfi;
 	}
 	LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available.\n");
@@ -324,7 +320,7 @@
 
 struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_tbf *old_tbf,
 	enum gprs_rlcmac_tbf_direction dir, uint8_t tfi, uint8_t trx,
-	uint8_t first_ts, uint8_t ms_class, uint8_t single_slot)
+	uint8_t ms_class, uint8_t single_slot)
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
 	struct gprs_rlcmac_tbf *tbf;
@@ -353,7 +349,7 @@
 		"MS_CLASS=%d\n", (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
 		tfi, trx, ms_class);
 
-	if (trx >= 8 || first_ts >= 8 || tfi >= 32)
+	if (trx >= 8 || tfi >= 32)
 		return NULL;
 
 	tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_tbf);
@@ -367,7 +363,6 @@
 	tbf->tfi = tfi;
 	tbf->trx = trx;
 	tbf->arfcn = bts->trx[trx].arfcn;
-	tbf->first_ts = first_ts;
 	tbf->ms_class = ms_class;
 	tbf->ws = 64;
 	tbf->sns = 128;
@@ -414,17 +409,24 @@
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
 	struct gprs_rlcmac_pdch *pdch;
-	uint8_t ts = tbf->first_ts;
+	uint8_t ts;
 	int8_t usf; /* must be signed */
 
 	LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
 		"%d\n", tbf->ms_class);
 
-	pdch = &bts->trx[tbf->trx].pdch[ts];
-	if (!pdch->enable) {
-		LOGP(DRLCMAC, LOGL_ERROR, "TS=%d not enabled.", ts);
-			return -EIO;
+	for (ts = 0; ts < 8; ts++) {
+		pdch = &bts->trx[tbf->trx].pdch[ts];
+		if (!pdch->enable) {
+			LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
+				"not enabled\n", ts);
+			continue;
+		}
+		break;
 	}
+	if (ts == 8)
+		return -EINVAL;
+
 	tbf->tsc = pdch->tsc;
 	if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
 		/* if USF available */
@@ -447,7 +449,7 @@
 		tbf->pdch[ts] = pdch;
 	}
 	/* the only one TS is the common TS */
-	tbf->first_common_ts = ts;
+	tbf->first_ts = tbf->first_common_ts = ts;
 
 	return 0;
 }
@@ -467,7 +469,7 @@
 	uint8_t Rx, Tx, Sum;	/* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
 	uint8_t Tta, Ttb, Tra, Trb, Tt, Tr;	/* Minimum Number of Slots */
 	uint8_t Type; /* Type of Mobile */
-	uint8_t rx_win_min, rx_win_max;
+	uint8_t rx_win_min = 0, rx_win_max = 7;
 	uint8_t tx_win_min, tx_win_max, tx_range;
 	uint8_t rx_window = 0, tx_window = 0;
 	const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
@@ -622,7 +624,7 @@
 		int j;
 
 		/* calculate mask of colliding slots */
-		for (ts = old_tbf->first_ts; ts < 8; ts++) {
+		for (ts = 0; ts < 8; ts++) {
 			if (old_tbf->pdch[ts]) {
 				ul_usage |= (1 << ts);
 				/* mark bits from TS-t .. TS+r */
diff --git a/src/gprs_rlcmac.h b/src/gprs_rlcmac.h
index 1d3fad9..a25eeb3 100644
--- a/src/gprs_rlcmac.h
+++ b/src/gprs_rlcmac.h
@@ -282,12 +282,12 @@
 
 struct gprs_rlcmac_sba *sba_find(uint8_t trx, uint8_t ts, uint32_t fn);
 
-int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, uint8_t *_ts,
-	int8_t use_trx, int8_t first_ts);
+int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx,
+	int8_t use_trx);
 
 struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_tbf *old_tbf,
 	enum gprs_rlcmac_tbf_direction dir, uint8_t tfi, uint8_t trx,
-	uint8_t first_ts, uint8_t ms_class, uint8_t single_slot);
+	uint8_t ms_class, uint8_t single_slot);
 
 struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi, uint8_t trx,
         enum gprs_rlcmac_tbf_direction dir);
diff --git a/src/gprs_rlcmac_data.cpp b/src/gprs_rlcmac_data.cpp
index bbfdabd..77d5a8a 100644
--- a/src/gprs_rlcmac_data.cpp
+++ b/src/gprs_rlcmac_data.cpp
@@ -221,24 +221,23 @@
 	return 0;
 }
 
-static struct gprs_rlcmac_tbf *alloc_ul_tbf(int8_t use_trx, int8_t first_ts,
-	uint8_t ms_class, uint32_t tlli, uint8_t ta,
-	struct gprs_rlcmac_tbf *dl_tbf)
+static struct gprs_rlcmac_tbf *alloc_ul_tbf(int8_t use_trx, uint8_t ms_class,
+	uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf)
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
-	uint8_t trx, ts;
+	uint8_t trx;
 	struct gprs_rlcmac_tbf *tbf;
 	uint8_t tfi;
 
 	/* create new TBF, use sme TRX as DL TBF */
-	tfi = tfi_alloc(GPRS_RLCMAC_UL_TBF, &trx, &ts, use_trx, first_ts);
+	tfi = tfi_alloc(GPRS_RLCMAC_UL_TBF, &trx, use_trx);
 	if (tfi < 0) {
 		LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
 		/* FIXME: send reject */
 		return NULL;
 	}
 	/* use multislot class of downlink TBF */
-	tbf = tbf_alloc(dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx, ts, ms_class, 0);
+	tbf = tbf_alloc(dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx, ms_class, 0);
 	if (!tbf) {
 		LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
 		/* FIXME: send reject */
@@ -401,7 +400,7 @@
 		if (ul_control_block->u.Packet_Downlink_Ack_Nack.Exist_Channel_Request_Description) {
 			LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF in ack "
 				"message, so we provide one:\n");
-			alloc_ul_tbf(tbf->trx, tbf->first_ts, tbf->ms_class, tbf->tlli, tbf->ta, tbf);
+			alloc_ul_tbf(tbf->trx, tbf->ms_class, tbf->tlli, tbf->ta, tbf);
 			/* schedule uplink assignment */
 			tbf->ul_ass_state = GPRS_RLCMAC_UL_ASS_SEND_ASS;
 		}
@@ -436,7 +435,7 @@
 					ms_class = get_ms_class_by_capability(&ul_control_block->u.Packet_Resource_Request.MS_Radio_Access_capability);
 				if (!ms_class)
 					LOGP(DRLCMAC, LOGL_NOTICE, "MS does not give us a class.\n");
-				tbf = alloc_ul_tbf(trx, ts, ms_class, tlli, 0, NULL);
+				tbf = alloc_ul_tbf(trx, ms_class, tlli, 0, NULL);
 #warning FIXME TA!!!
 				if (!tbf)
 					break;
@@ -1095,7 +1094,7 @@
 {
 	struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
 	struct gprs_rlcmac_tbf *tbf;
-	uint8_t trx, ts;
+	uint8_t trx, ts = 0;
 	int8_t tfi; /* must be signed */
 	uint8_t sb = 0;
 	uint32_t sb_fn = 0;
@@ -1128,14 +1127,14 @@
 			"(AGCH)\n");
 	} else {
 		// Create new TBF
-		tfi = tfi_alloc(GPRS_RLCMAC_UL_TBF, &trx, &ts, -1, -1);
+		tfi = tfi_alloc(GPRS_RLCMAC_UL_TBF, &trx, -1);
 		if (tfi < 0) {
 			LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
 			/* FIXME: send reject */
 			return -EBUSY;
 		}
 		/* set class to 0, since we don't know the multislot class yet */
-		tbf = tbf_alloc(NULL, GPRS_RLCMAC_UL_TBF, tfi, trx, ts, 0, 1);
+		tbf = tbf_alloc(NULL, GPRS_RLCMAC_UL_TBF, tfi, trx, 0, 1);
 		if (!tbf) {
 			LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
 			/* FIXME: send reject */