egprs: Add CPS tables from TS 04.60

Includes EGPRS coding and puncturing scheme (CPS) tables from 3GPP
TS 04.60. Currently osmo-bts-trx is the only user of CPS table
values, but this may change with gprsdecode and other utilities.

Change-Id: I09fe6514a0e2e51bb3206f8387633f7e0255345f
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index 0f43f48..a2f2524 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -15,7 +15,7 @@
 
 libgsmint_la_SOURCES =  a5.c rxlev_stat.c tlv_parser.c comp128.c comp128v23.c \
 			gsm_utils.c rsl.c gsm48.c gsm48_ie.c gsm0808.c sysinfo.c \
-			gprs_cipher_core.c gsm0480.c abis_nm.c gsm0502.c \
+			gprs_cipher_core.c gprs_rlc.c gsm0480.c abis_nm.c gsm0502.c \
 			gsm0411_utils.c gsm0411_smc.c gsm0411_smr.c \
 			lapd_core.c lapdm.c kasumi.c gsm_04_08_gprs.c \
 			conv_cs2_gen.c conv_cs3_gen.c conv_xcch_gen.c \
diff --git a/src/gsm/gprs_rlc.c b/src/gsm/gprs_rlc.c
new file mode 100644
index 0000000..d6ccbbe
--- /dev/null
+++ b/src/gsm/gprs_rlc.c
@@ -0,0 +1,106 @@
+#include <errno.h>
+#include <string.h>
+
+#include <osmocom/gprs/gprs_rlc.h>
+#include <osmocom/gprs/protocol/gsm_04_60.h>
+
+#define EGPRS_CPS_TYPE1_TBL_SZ		29
+#define EGPRS_CPS_TYPE2_TBL_SZ		8
+#define EGPRS_CPS_TYPE3_TBL_SZ		16
+
+/* 3GPP TS 44.060 10.4.8a.1.1 "Header type 1" */
+static const struct egprs_cps egprs_cps_table_type1[EGPRS_CPS_TYPE1_TBL_SZ] = {
+	{ .bits =  0, .mcs = 9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
+	{ .bits =  1, .mcs = 9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
+	{ .bits =  2, .mcs = 9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
+	{ .bits =  3, .mcs = 0, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } },
+	{ .bits =  4, .mcs = 9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
+	{ .bits =  5, .mcs = 9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
+	{ .bits =  6, .mcs = 9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
+	{ .bits =  7, .mcs = 0, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } },
+	{ .bits =  8, .mcs = 9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
+	{ .bits =  9, .mcs = 9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
+	{ .bits = 10, .mcs = 9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
+	{ .bits = 11, .mcs = 8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
+	{ .bits = 12, .mcs = 8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
+	{ .bits = 13, .mcs = 8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
+	{ .bits = 14, .mcs = 8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
+	{ .bits = 15, .mcs = 8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
+	{ .bits = 16, .mcs = 8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
+	{ .bits = 17, .mcs = 8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
+	{ .bits = 18, .mcs = 8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
+	{ .bits = 19, .mcs = 8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
+	{ .bits = 20, .mcs = 7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
+	{ .bits = 21, .mcs = 7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
+	{ .bits = 22, .mcs = 7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
+	{ .bits = 23, .mcs = 7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
+	{ .bits = 24, .mcs = 7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
+	{ .bits = 25, .mcs = 7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
+	{ .bits = 26, .mcs = 7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
+	{ .bits = 27, .mcs = 7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
+	{ .bits = 28, .mcs = 7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
+};
+
+/*
+ * 3GPP TS 44.060 10.4.8a.2.1
+ * "Header type 2 in EGPRS TBF or uplink EGPRS2-A TBF"
+ */
+static const struct egprs_cps egprs_cps_table_type2[EGPRS_CPS_TYPE2_TBL_SZ] = {
+	{ .bits =  0, .mcs = 6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  1, .mcs = 6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  2, .mcs = 6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  3, .mcs = 6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  4, .mcs = 5, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  5, .mcs = 5, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  6, .mcs = 6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  7, .mcs = 6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+};
+
+/* 3GPP TS 44.060 10.4.8a.3 "Header type 3" */
+static const struct egprs_cps egprs_cps_table_type3[EGPRS_CPS_TYPE3_TBL_SZ] = {
+	{ .bits =  0, .mcs = 4, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  1, .mcs = 4, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  2, .mcs = 4, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
+	{ .bits =  3, .mcs = 3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  4, .mcs = 3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  5, .mcs = 3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
+	{ .bits =  6, .mcs = 3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits =  7, .mcs = 3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits =  8, .mcs = 3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
+	{ .bits =  9, .mcs = 2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits = 10, .mcs = 2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits = 11, .mcs = 1, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits = 12, .mcs = 1, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits = 13, .mcs = 2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
+	{ .bits = 14, .mcs = 2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
+	{ .bits = 15, .mcs = 0, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } },
+};
+
+int egprs_get_cps(struct egprs_cps *cps, uint8_t type, uint8_t bits)
+{
+	const struct egprs_cps *table_cps;
+
+	switch (type) {
+	case EGPRS_HDR_TYPE1:
+		if (bits >= EGPRS_CPS_TYPE1_TBL_SZ)
+			return -EINVAL;
+		table_cps = &egprs_cps_table_type1[bits];
+		break;
+	case EGPRS_HDR_TYPE2:
+		if (bits >= EGPRS_CPS_TYPE2_TBL_SZ)
+			return -EINVAL;
+		table_cps = &egprs_cps_table_type2[bits];
+		break;
+	case EGPRS_HDR_TYPE3:
+		if (bits >= EGPRS_CPS_TYPE3_TBL_SZ)
+			return -EINVAL;
+		table_cps = &egprs_cps_table_type3[bits];
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	memcpy(cps, table_cps, sizeof *cps);
+
+	return 0;
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 9667ea0..f530b6f 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -48,6 +48,8 @@
 gprs_ms_net_cap_gea_supported;
 gprs_msgt_gmm_names;
 
+egprs_get_cps;
+
 gsm48_gmm_cause_names;
 gsm48_gsm_cause_names;
 gprs_att_t_strs;