aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-litecell15/misc/lc15bts_bid.c
blob: 9284b62ea6051b5484adba522efb80539705f8e1 (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
/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
 * 
 * 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 <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>

#include "lc15bts_bid.h"

#define BOARD_REV_SYSFS		"/var/lc15/platform/revision"
#define BOARD_OPT_SYSFS		"/var/lc15/platform/option"

static const int option_type_mask[_NUM_OPTION_TYPES] = {
        [LC15BTS_OPTION_OCXO]		= 0x07,
	[LC15BTS_OPTION_FPGA]		= 0x03,
	[LC15BTS_OPTION_PA]		= 0x01,
	[LC15BTS_OPTION_BAND]		= 0x03,
	[LC15BTS_OPTION_TX_ISO_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_DUP_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_PB_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_DIV]		= 0x01,
	[LC15BTS_OPTION_RX1A]		= 0x01,
	[LC15BTS_OPTION_RX1B]		= 0x01,
	[LC15BTS_OPTION_RX2A]		= 0x01,
	[LC15BTS_OPTION_RX2B]		= 0x01,
	[LC15BTS_OPTION_DDR_32B]	= 0x01,
	[LC15BTS_OPTION_DDR_ECC]	= 0x01,
	[LC15BTS_OPTION_LOG_DET]	= 0x01,
	[LC15BTS_OPTION_DUAL_LOG_DET]	= 0x01,
};

static const int option_type_shift[_NUM_OPTION_TYPES] = {
        [LC15BTS_OPTION_OCXO]		= 0,
	[LC15BTS_OPTION_FPGA]		= 3,
	[LC15BTS_OPTION_PA]		= 5,
	[LC15BTS_OPTION_BAND]		= 6,
	[LC15BTS_OPTION_TX_ISO_BYP]	= 8,
	[LC15BTS_OPTION_RX_DUP_BYP]	= 9,
	[LC15BTS_OPTION_RX_PB_BYP]	= 10,
	[LC15BTS_OPTION_RX_DIV]		= 11,
	[LC15BTS_OPTION_RX1A]		= 12,
	[LC15BTS_OPTION_RX1B]		= 13,
	[LC15BTS_OPTION_RX2A]		= 14,
	[LC15BTS_OPTION_RX2B]		= 15,
	[LC15BTS_OPTION_DDR_32B]	= 16,
	[LC15BTS_OPTION_DDR_ECC]	= 17,
	[LC15BTS_OPTION_LOG_DET]	= 18,
	[LC15BTS_OPTION_DUAL_LOG_DET]	= 19,
};


static int board_rev = -1;
static int board_option = -1;

static inline bool read_board(const char *src, const char *spec, void *dst)
{
	FILE *fp = fopen(src, "r");
        if (!fp) {
		fprintf(stderr, "Failed to open %s due to '%s' error\n", src, strerror(errno));
		return false;
	}

        if (fscanf(fp, spec, dst) != 1) {
                fclose(fp);
		fprintf(stderr, "Failed to read %s due to '%s' error\n", src, strerror(errno));
		return false;
        }
        fclose(fp);
	return true;
}

int lc15bts_rev_get(void) 
{
	char rev;

	if (board_rev != -1) {
		return board_rev;
	}

	if (!read_board(BOARD_REV_SYSFS, "%c", &rev))
		return -1;

	board_rev = rev;
	return board_rev;
}

int lc15bts_model_get(void)
{
        int opt;

        if (board_option != -1)
		return board_option;

	if (!read_board(BOARD_OPT_SYSFS, "%X", &opt))
		return -1;
	
	board_option = opt;
        return board_option;
}

int lc15bts_option_get(enum lc15bts_option_type type)
{
	int rc;
	int option;

	if (type >= _NUM_OPTION_TYPES) {
		return -EINVAL;
	}

	if (board_option == -1) {
		rc = lc15bts_model_get();
		if (rc < 0) return rc;
	}

	option = (board_option >> option_type_shift[type])
		& option_type_mask[type];

	return option;	
}

const char* get_hwversion_desc()
{
        int rev;
        int model;
        size_t len;
        static char model_name[64] = {0, };
        len = snprintf(model_name, sizeof(model_name), "NuRAN Litecell 1.5 BTS");

        rev = lc15bts_rev_get();
        if (rev >= 0) {
                len += snprintf(model_name + len, sizeof(model_name) - len,
                                " Rev %c", (char)rev);
        }

        model = lc15bts_model_get();
        if (model >= 0) {
                snprintf(model_name + len, sizeof(model_name) - len,
                         "%s (%05X)", model_name, model);
        }
        return model_name;
}