aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-litecell15/calib_file.c
blob: a9e86293a4313aa8f1355ce16af30f9d452f1c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* NuRAN Wireless Litecell 1.5 BTS L1 calibration file routines*/

/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
 * Copyright (C) 2016 by Harald Welte <laforge@gnumonks.org>
 * 
 * Based on sysmoBTS:
 *     (C) 2012 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>

#include <osmocom/core/utils.h>

#include <osmo-bts/gsm_data.h>
#include <osmo-bts/logging.h>

#include <nrw/litecell15/litecell15.h>
#include <nrw/litecell15/gsml1const.h>

#include "l1_if.h"
#include "lc15bts.h"
#include "utils.h"

/**
 *  * Maximum calibration data chunk size
 *   */
#define MAX_CALIB_TBL_SIZE  65536
#define CALIB_HDR_V1  0x01

struct calib_file_desc {
	const char *fname;
	int rx;
	int trx;
        int rxpath;
};

static const struct calib_file_desc calib_files[] = {
	{
		.fname = "calib_rx0a.conf",
		.rx = 1,
		.trx = 0,
		.rxpath = 0,
	}, {
		.fname = "calib_rx0b.conf",
		.rx = 1,
		.trx = 0,
		.rxpath = 1,
	}, {
		.fname = "calib_rx1a.conf",
		.rx = 1,
		.trx = 1,
		.rxpath = 0,
	}, {
		.fname = "calib_rx1b.conf",
		.rx = 1,
		.trx = 1,
		.rxpath = 1,
	}, {
		.fname = "calib_tx0.conf",
		.rx = 0,
		.trx = 0,
	}, {
		.fname = "calib_tx1.conf",
		.rx = 0,
		.trx = 1,
	},
};

struct calTbl_t
{
    union
    {
        struct
        {
            uint8_t u8Version;                // Header version (1)
            uint8_t u8Parity;                 // Parity byte (xor)
            uint8_t u8Type;                   // Table type (0:TX Downlink, 1:RX-A Uplink, 2:RX-B Uplink)
            uint8_t u8Band;                   // GSM Band (0:GSM-850, 1:EGSM-900, 2:DCS-1800, 3:PCS-1900)
            uint32_t u32Len;                  // Table length in bytes including the header
            struct
            {
                uint32_t u32DescOfst;         // Description section offset
                uint32_t u32DateOfst;         // Date section offset
                uint32_t u32StationOfst;      // Calibration test station section offset
                uint32_t u32FpgaFwVerOfst;    // Calibration FPGA firmware version section offset
                uint32_t u32DspFwVerOfst;     // Calibration DSP firmware section offset
                uint32_t u32DataOfst;         // Calibration data section offset
            } toc;
        } v1;
    } hdr;

    uint8_t u8RawData[MAX_CALIB_TBL_SIZE - 32];
};


static int calib_file_send(struct lc15l1_hdl *fl1h,
			   const struct calib_file_desc *desc);
static int calib_verify(struct lc15l1_hdl *fl1h,
			const struct calib_file_desc *desc);

/* determine next calibration file index based on supported bands */
static int get_next_calib_file_idx(struct lc15l1_hdl *fl1h, int last_idx)
{
	struct phy_link *plink = fl1h->phy_inst->phy_link;
        int i;

        for (i = last_idx+1; i < ARRAY_SIZE(calib_files); i++) {
                if (calib_files[i].trx == plink->num)
                        return i;
        }
        return -1;
}

static int calib_file_open(struct lc15l1_hdl *fl1h,
                           const struct calib_file_desc *desc)
{
	struct calib_send_state *st = &fl1h->st;
	char *calib_path = fl1h->phy_inst->u.lc15.calib_path;
        char fname[PATH_MAX];

	if (st->fp) {
		LOGP(DL1C, LOGL_NOTICE, "L1 calibration file was left opened !!\n");
		fclose(st->fp);
		st->fp = NULL;
	}

        fname[0] = '\0';
        snprintf(fname, sizeof(fname)-1, "%s/%s", calib_path, desc->fname);
        fname[sizeof(fname)-1] = '\0';

        st->fp = fopen(fname, "rb");
        if (!st->fp) {
                LOGP(DL1C, LOGL_ERROR,
                        "Failed to open '%s' for calibration data.\n", fname);
                return -1;
        }
	return 0;
}

static int calib_file_close(struct lc15l1_hdl *fl1h)
{
	struct calib_send_state *st = &fl1h->st;

	if (st->fp) {
		fclose(st->fp);
		st->fp = NULL;
	}
	return 0;
}

/* iteratively download the calibration data into the L1 */

static int calib_send_compl_cb(struct gsm_bts_trx *trx, struct msgb *l1_msg,
			       void *data);

/* send a chunk of calibration tabledata for a single specified file */
static int calib_file_send_next_chunk(struct lc15l1_hdl *fl1h)
{
	struct calib_send_state *st = &fl1h->st;
	Litecell15_Prim_t *prim;
	struct msgb *msg;
	size_t n;

	msg = sysp_msgb_alloc();
	prim = msgb_sysprim(msg);

	prim->id = Litecell15_PrimId_SetCalibTblReq;
	prim->u.setCalibTblReq.offset = (uint32_t)ftell(st->fp);
	n = fread(prim->u.setCalibTblReq.u8Data, 1, 
			sizeof(prim->u.setCalibTblReq.u8Data), st->fp);
	prim->u.setCalibTblReq.length = n;


	if (n == 0) {
		/* The table data has been completely sent and acknowledged */
                LOGP(DL1C, LOGL_NOTICE, "L1 calibration table %s loaded\n",
                        calib_files[st->last_file_idx].fname);

                calib_file_close(fl1h);

                msgb_free(msg);

                /* Send the next one if any */
                st->last_file_idx = get_next_calib_file_idx(fl1h, st->last_file_idx);
                if (st->last_file_idx >= 0) {
                        return calib_file_send(fl1h,
                                       &calib_files[st->last_file_idx]);
                }

                LOGP(DL1C, LOGL_INFO, "L1 calibration table loading complete!\n");
                return 0;
	}

	return l1if_req_compl(fl1h, msg, calib_send_compl_cb, NULL);
}

/* send the calibration table for a single specified file */
static int calib_file_send(struct lc15l1_hdl *fl1h,
			   const struct calib_file_desc *desc)
{
	struct calib_send_state *st = &fl1h->st;
	int rc;

	rc = calib_file_open(fl1h, desc);
	if (rc < 0) {
		/* still, we'd like to continue trying to load
                 * calibration for all other bands */
                st->last_file_idx = get_next_calib_file_idx(fl1h, st->last_file_idx);
                if (st->last_file_idx >= 0)
                        return calib_file_send(fl1h,
                                               &calib_files[st->last_file_idx]);

		LOGP(DL1C, LOGL_INFO, "L1 calibration table loading complete!\n");
                return 0;
	}

	rc = calib_verify(fl1h, desc);
	if ( rc < 0 ) {
		LOGP(DL1C, LOGL_ERROR, "Verify L1 calibration table %s -> failed (%d)\n", desc->fname, rc);
		st->last_file_idx = get_next_calib_file_idx(fl1h, st->last_file_idx);

		if (st->last_file_idx >= 0)
			return calib_file_send(fl1h,
				&calib_files[st->last_file_idx]);
		return 0;

	}

	LOGP(DL1C, LOGL_INFO, "Verify L1 calibration table %s -> done\n", desc->fname);

	return calib_file_send_next_chunk(fl1h);
}

/* completion callback after every SetCalibTbl is confirmed */
static int calib_send_compl_cb(struct gsm_bts_trx *trx, struct msgb *l1_msg,
				void *data)
{
	struct lc15l1_hdl *fl1h = trx_lc15l1_hdl(trx);
	struct calib_send_state *st = &fl1h->st;
	Litecell15_Prim_t *prim = msgb_sysprim(l1_msg);

	if (prim->u.setCalibTblCnf.status != GsmL1_Status_Success) {
		LOGP(DL1C, LOGL_ERROR, "L1 rejected calibration table\n");

		msgb_free(l1_msg);

		calib_file_close(fl1h);

		/* Skip this one and try the next one */
		st->last_file_idx = get_next_calib_file_idx(fl1h, st->last_file_idx);
	        if (st->last_file_idx >= 0) {
        	        return calib_file_send(fl1h, 
					&calib_files[st->last_file_idx]);
		}

		LOGP(DL1C, LOGL_INFO, "L1 calibration table loading complete!\n");
		return 0;
	}

	msgb_free(l1_msg);

	/* Keep sending the calibration file data */
	return calib_file_send_next_chunk(fl1h);
}

int calib_load(struct lc15l1_hdl *fl1h)
{
	int rc;
	struct calib_send_state *st = &fl1h->st;
	char *calib_path = fl1h->phy_inst->u.lc15.calib_path;

        if (!calib_path) {
                LOGP(DL1C, LOGL_ERROR, "Calibration file path not specified\n");
                return -1;
        }

	rc = get_next_calib_file_idx(fl1h, -1);
	if (rc < 0) {
		return -1;
	}
	st->last_file_idx = rc;

	return calib_file_send(fl1h, &calib_files[st->last_file_idx]);
}


static int calib_verify(struct lc15l1_hdl *fl1h, const struct calib_file_desc *desc)
{
       int i, rc, sz;
       struct calib_send_state *st = &fl1h->st;
       struct phy_link *plink = fl1h->phy_inst->phy_link;
       char *rbuf;
       struct calTbl_t *calTbl;
       char calChkSum ;


       //calculate file size in bytes
       fseek(st->fp, 0L, SEEK_END);
       sz = ftell(st->fp);

       //rewind read poiner
       fseek(st->fp, 0L, SEEK_SET);

       //read file
       rbuf = (char *) malloc( sizeof(char) * sz );

       rc = fread(rbuf, 1, sizeof(char) * sz, st->fp);
       if ( rc != sz) {

               LOGP(DL1C, LOGL_ERROR, "%s reading error\n", desc->fname);
               free(rbuf);

               //close file
               rc = calib_file_close(fl1h);
               if (rc < 0 ) {
                       LOGP(DL1C, LOGL_ERROR, "%s can not close\n", desc->fname);
                       return rc;
               }

               return -2;
       }


       calTbl = (struct calTbl_t*) rbuf;
       //calcualte file checksum
       calChkSum = 0;
       while ( sz-- ) {
               calChkSum ^= rbuf[sz];
       }

       //validate Tx calibration parity
       if ( calChkSum ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid checksum %x.\n", desc->fname, calChkSum);
               return -4;
       }

       //validate Tx calibration header
       if ( calTbl->hdr.v1.u8Version != CALIB_HDR_V1 ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid header version %u.\n", desc->fname, calTbl->hdr.v1.u8Version);
               return -5;
       }

       //validate calibration description
       if ( calTbl->hdr.v1.toc.u32DescOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid calibration description  offset.\n", desc->fname);
               return -6;
       }

       //validate calibration date
       if ( calTbl->hdr.v1.toc.u32DateOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid calibration date offset.\n", desc->fname);
               return -7;
       }

       LOGP(DL1C, LOGL_INFO, "L1 calibration table %s created on %s\n",
               desc->fname,
               calTbl->u8RawData + calTbl->hdr.v1.toc.u32DateOfst);

       //validate calibration station
       if ( calTbl->hdr.v1.toc.u32StationOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid calibration station ID offset.\n", desc->fname);
               return -8;
       }

       //validate FPGA FW version
       if ( calTbl->hdr.v1.toc.u32FpgaFwVerOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid FPGA FW version offset.\n", desc->fname);
               return -9;
       }
       //validate DSP FW version
       if ( calTbl->hdr.v1.toc.u32DspFwVerOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid DSP FW version offset.\n", desc->fname);
               return -10;
       }

       //validate Tx calibration data offset
       if ( calTbl->hdr.v1.toc.u32DataOfst == 0xFFFFFFFF ) {
               LOGP(DL1C, LOGL_ERROR, "%s has invalid calibration data offset.\n", desc->fname);
               return -11;
       }

       if ( !desc->rx ) {

               //parse min/max Tx power
               fl1h->phy_inst->u.lc15.minTxPower = calTbl->u8RawData[calTbl->hdr.v1.toc.u32DataOfst + (5 << 2)];
               fl1h->phy_inst->u.lc15.maxTxPower = calTbl->u8RawData[calTbl->hdr.v1.toc.u32DataOfst + (6 << 2)];

               //override nominal Tx power of given TRX if needed
               if ( fl1h->phy_inst->trx->nominal_power > fl1h->phy_inst->u.lc15.maxTxPower) {
                       LOGP(DL1C, LOGL_INFO, "Set TRX %u nominal Tx power to %d dBm (%d)\n",
                                       plink->num,
                                       fl1h->phy_inst->u.lc15.maxTxPower,
                                       fl1h->phy_inst->trx->nominal_power);

                       fl1h->phy_inst->trx->nominal_power = fl1h->phy_inst->u.lc15.maxTxPower;
               }

               if ( fl1h->phy_inst->trx->nominal_power < fl1h->phy_inst->u.lc15.minTxPower) {
                       LOGP(DL1C, LOGL_INFO, "Set TRX %u nominal Tx power to %d dBm (%d)\n",
                                       plink->num,
                                       fl1h->phy_inst->u.lc15.minTxPower,
                                       fl1h->phy_inst->trx->nominal_power);

                       fl1h->phy_inst->trx->nominal_power = fl1h->phy_inst->u.lc15.minTxPower;
               }

               if ( fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm > to_mdB(fl1h->phy_inst->u.lc15.maxTxPower) ) {
                       LOGP(DL1C, LOGL_INFO, "Set TRX %u Tx power parameter to %d dBm (%d)\n",
                                       plink->num,
                                       to_mdB(fl1h->phy_inst->u.lc15.maxTxPower),
                                       fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm);

                       fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm = to_mdB(fl1h->phy_inst->u.lc15.maxTxPower);
               }

               if ( fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm < to_mdB(fl1h->phy_inst->u.lc15.minTxPower) ) {
                       LOGP(DL1C, LOGL_INFO, "Set TRX %u Tx power parameter to %d dBm (%d)\n",
                                       plink->num,
                                       to_mdB(fl1h->phy_inst->u.lc15.minTxPower),
                                       fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm);

                       fl1h->phy_inst->trx->power_params.trx_p_max_out_mdBm = to_mdB(fl1h->phy_inst->u.lc15.minTxPower);
               }

               LOGP(DL1C, LOGL_DEBUG, "%s: minTxPower=%d, maxTxPower=%d\n",
                               desc->fname,
                               fl1h->phy_inst->u.lc15.minTxPower,
                               fl1h->phy_inst->u.lc15.maxTxPower );
       }

       //rewind read poiner for subsequence tasks
       fseek(st->fp, 0L, SEEK_SET);
       free(rbuf);

       return 0;
}