blob: bdfc8eac26f87ac381e679cc2754e09b349acd48 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm/gprs_rlc.c
2 * helper functions for (E)GPRS RLC according to 3GPP TS 44.060.
Harald Weltee08da972017-11-13 01:00:26 +09003 *
4 * (C) 2016 by Thomas Thou
5 * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
6 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
7 * All Rights Reserved.
8 *
9 * SPDX-License-Identifier: GPL-2.0+
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020010 */
11
Tom Tsou9a5bbf32016-07-14 15:13:45 -070012#include <errno.h>
13#include <string.h>
14
Harald Welte35b26322017-07-31 19:36:52 +020015#include <osmocom/core/utils.h>
Tom Tsou9a5bbf32016-07-14 15:13:45 -070016#include <osmocom/gprs/gprs_rlc.h>
Maxc8cf8202017-05-22 16:07:04 +020017#include <osmocom/coding/gsm0503_coding.h>
Tom Tsou9a5bbf32016-07-14 15:13:45 -070018#include <osmocom/gprs/protocol/gsm_04_60.h>
19
20#define EGPRS_CPS_TYPE1_TBL_SZ 29
21#define EGPRS_CPS_TYPE2_TBL_SZ 8
22#define EGPRS_CPS_TYPE3_TBL_SZ 16
23
24/* 3GPP TS 44.060 10.4.8a.1.1 "Header type 1" */
25static const struct egprs_cps egprs_cps_table_type1[EGPRS_CPS_TYPE1_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020026 { .bits = 0, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
27 { .bits = 1, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
28 { .bits = 2, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
29 { .bits = 3, .mcs = EGPRS_NUM_MCS, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } }, /* reserved for future use */
30 { .bits = 4, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
31 { .bits = 5, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
32 { .bits = 6, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
33 { .bits = 7, .mcs = EGPRS_NUM_MCS, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } }, /* reserved for future use */
34 { .bits = 8, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
35 { .bits = 9, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
36 { .bits = 10, .mcs = EGPRS_MCS9, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
37 { .bits = 11, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
38 { .bits = 12, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
39 { .bits = 13, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
40 { .bits = 14, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
41 { .bits = 15, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
42 { .bits = 16, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
43 { .bits = 17, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
44 { .bits = 18, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
45 { .bits = 19, .mcs = EGPRS_MCS8, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
46 { .bits = 20, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P1 } },
47 { .bits = 21, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P2 } },
48 { .bits = 22, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P1, EGPRS_CPS_P3 } },
49 { .bits = 23, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P1 } },
50 { .bits = 24, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P2 } },
51 { .bits = 25, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P2, EGPRS_CPS_P3 } },
52 { .bits = 26, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P1 } },
53 { .bits = 27, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P2 } },
54 { .bits = 28, .mcs = EGPRS_MCS7, .p = { EGPRS_CPS_P3, EGPRS_CPS_P3 } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070055};
56
57/*
58 * 3GPP TS 44.060 10.4.8a.2.1
59 * "Header type 2 in EGPRS TBF or uplink EGPRS2-A TBF"
60 */
61static const struct egprs_cps egprs_cps_table_type2[EGPRS_CPS_TYPE2_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020062 { .bits = 0, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
63 { .bits = 1, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
64 { .bits = 2, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
65 { .bits = 3, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
66 { .bits = 4, .mcs = EGPRS_MCS5, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
67 { .bits = 5, .mcs = EGPRS_MCS5, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
68 { .bits = 6, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
69 { .bits = 7, .mcs = EGPRS_MCS6, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070070};
71
72/* 3GPP TS 44.060 10.4.8a.3 "Header type 3" */
73static const struct egprs_cps egprs_cps_table_type3[EGPRS_CPS_TYPE3_TBL_SZ] = {
Maxc8cf8202017-05-22 16:07:04 +020074 { .bits = 0, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
75 { .bits = 1, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
76 { .bits = 2, .mcs = EGPRS_MCS4, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
77 { .bits = 3, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
78 { .bits = 4, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
79 { .bits = 5, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
80 { .bits = 6, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
81 { .bits = 7, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
82 { .bits = 8, .mcs = EGPRS_MCS3, .p = { EGPRS_CPS_P3, EGPRS_CPS_NONE } },
83 { .bits = 9, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
84 { .bits = 10, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
85 { .bits = 11, .mcs = EGPRS_MCS1, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
86 { .bits = 12, .mcs = EGPRS_MCS1, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
87 { .bits = 13, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P1, EGPRS_CPS_NONE } },
88 { .bits = 14, .mcs = EGPRS_MCS2, .p = { EGPRS_CPS_P2, EGPRS_CPS_NONE } },
89 { .bits = 15, .mcs = EGPRS_MCS0, .p = { EGPRS_CPS_NONE, EGPRS_CPS_NONE } },
Tom Tsou9a5bbf32016-07-14 15:13:45 -070090};
91
92int egprs_get_cps(struct egprs_cps *cps, uint8_t type, uint8_t bits)
93{
94 const struct egprs_cps *table_cps;
95
96 switch (type) {
97 case EGPRS_HDR_TYPE1:
98 if (bits >= EGPRS_CPS_TYPE1_TBL_SZ)
99 return -EINVAL;
100 table_cps = &egprs_cps_table_type1[bits];
101 break;
102 case EGPRS_HDR_TYPE2:
103 if (bits >= EGPRS_CPS_TYPE2_TBL_SZ)
104 return -EINVAL;
105 table_cps = &egprs_cps_table_type2[bits];
106 break;
107 case EGPRS_HDR_TYPE3:
108 if (bits >= EGPRS_CPS_TYPE3_TBL_SZ)
109 return -EINVAL;
110 table_cps = &egprs_cps_table_type3[bits];
111 break;
112 default:
113 return -EINVAL;
114 }
115
116 memcpy(cps, table_cps, sizeof *cps);
117
118 return 0;
119}
Harald Welte35b26322017-07-31 19:36:52 +0200120
121struct gprs_cs_desc {
122 struct {
123 uint8_t bytes;
124 uint8_t bits;
125 } uplink, downlink;
126};
127
128const struct gprs_cs_desc gprs_cs_desc[_NUM_OSMO_GPRS_CS] = {
129 [OSMO_GPRS_CS1] = { {23, 0}, {23, 0} },
130 [OSMO_GPRS_CS2] = { {33, 7}, {33, 7} },
131 [OSMO_GPRS_CS3] = { {39, 3}, {39, 3} },
132 [OSMO_GPRS_CS4] = { {53, 7}, {53, 7} },
133
134 [OSMO_GPRS_MCS1] = { {26, 1}, {26, 1} },
135 [OSMO_GPRS_MCS2] = { {32, 1}, {32, 1} },
136 [OSMO_GPRS_MCS3] = { {41, 1}, {41, 1} },
137 [OSMO_GPRS_MCS4] = { {48, 1}, {48, 1} },
138
139 [OSMO_GPRS_MCS5] = { {60, 7}, {59, 6} },
140 [OSMO_GPRS_MCS6] = { {78, 7}, {77, 6} },
141 [OSMO_GPRS_MCS7] = { {118, 2}, {117, 4} },
142 [OSMO_GPRS_MCS8] = { {142, 2}, {141, 4} },
143 [OSMO_GPRS_MCS9] = { {154, 2}, {153, 4} },
144};
145
146/*! Return size of (E)GPRS uplink block for given coding scheme in bits */
147int osmo_gprs_ul_block_size_bits(enum osmo_gprs_cs cs)
148{
149 if (cs >= ARRAY_SIZE(gprs_cs_desc))
150 return -EINVAL;
151 return gprs_cs_desc[cs].uplink.bytes * 8 + gprs_cs_desc[cs].uplink.bits;
152}
153
154/*! Return size of (E)GPRS downlink block for given coding scheme in bits */
155int osmo_gprs_dl_block_size_bits(enum osmo_gprs_cs cs)
156{
157 if (cs >= ARRAY_SIZE(gprs_cs_desc))
158 return -EINVAL;
159 return gprs_cs_desc[cs].downlink.bytes * 8 + gprs_cs_desc[cs].downlink.bits;
160}
161
162/*! Return size of (E)GPRS uplink block for given coding scheme in bytes */
163int osmo_gprs_ul_block_size_bytes(enum osmo_gprs_cs cs)
164{
165 int rc;
166 if (cs >= ARRAY_SIZE(gprs_cs_desc))
167 return -EINVAL;
168 rc = gprs_cs_desc[cs].uplink.bytes;
169 if (gprs_cs_desc[cs].uplink.bits)
170 rc++;
171 return rc;
172}
173
174/*! Return size of (E)GPRS downlink block for given coding scheme in bytes */
175int osmo_gprs_dl_block_size_bytes(enum osmo_gprs_cs cs)
176{
177 int rc;
178 if (cs >= ARRAY_SIZE(gprs_cs_desc))
179 return -EINVAL;
180 rc = gprs_cs_desc[cs].downlink.bytes;
181 if (gprs_cs_desc[cs].downlink.bits)
182 rc++;
183 return rc;
184}
185
186/*! Return coding scheme for given (E)GPRS uplink block size */
187enum osmo_gprs_cs osmo_gprs_ul_cs_by_block_bytes(uint8_t block_size)
188{
189 unsigned int i;
190
191 for (i = 0; i < ARRAY_SIZE(gprs_cs_desc); i++) {
192 if (block_size == osmo_gprs_ul_block_size_bytes(i))
193 return i;
194 }
195 return OSMO_GPRS_CS_NONE;
196}
197
198/*! Return coding scheme for given (E)GPRS downlink block size */
199enum osmo_gprs_cs osmo_gprs_dl_cs_by_block_bytes(uint8_t block_size)
200{
201 unsigned int i;
202
203 for (i = 0; i < ARRAY_SIZE(gprs_cs_desc); i++) {
204 if (block_size == osmo_gprs_dl_block_size_bytes(i))
205 return i;
206 }
207 return OSMO_GPRS_CS_NONE;
208}