aboutsummaryrefslogtreecommitdiff
path: root/daemon/libsensors/data.c
blob: cac9c8dc6eb1467d9f7a27c5598d537b1e879157 (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
/*
    data.c - Part of libsensors, a Linux library for reading sensor data.
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
    Copyright (C) 2007, 2009  Jean Delvare <khali@linux-fr.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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 Lesser General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

/*** This file modified by ARM on Jan 23, 2013 to move version.h to the current directory. ***/

/* this define needed for strndup() */
#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>

#include "access.h"
#include "error.h"
#include "data.h"
#include "sensors.h"
#include "version.h"

const char *libsensors_version = LM_VERSION;

char **sensors_config_files = NULL;
int sensors_config_files_count = 0;
int sensors_config_files_max = 0;

sensors_chip *sensors_config_chips = NULL;
int sensors_config_chips_count = 0;
int sensors_config_chips_subst = 0;
int sensors_config_chips_max = 0;

sensors_bus *sensors_config_busses = NULL;
int sensors_config_busses_count = 0;
int sensors_config_busses_max = 0;

sensors_chip_features *sensors_proc_chips = NULL;
int sensors_proc_chips_count = 0;
int sensors_proc_chips_max = 0;

sensors_bus *sensors_proc_bus = NULL;
int sensors_proc_bus_count = 0;
int sensors_proc_bus_max = 0;

void sensors_free_chip_name(sensors_chip_name *chip)
{
	free(chip->prefix);
}

/*
   Parse a chip name to the internal representation. These are valid names:

     lm78-i2c-10-5e		*-i2c-10-5e
     lm78-i2c-10-*		*-i2c-10-*
     lm78-i2c-*-5e		*-i2c-*-5e
     lm78-i2c-*-*		*-i2c-*-*
     lm78-isa-10dd		*-isa-10dd
     lm78-isa-*			*-isa-*
     lm78-*			*-*

   Here 'lm78' can be any prefix. 'i2c' and 'isa' are
   literal strings, just like all dashes '-' and wildcards '*'. '10' can
   be any decimal i2c bus number. '5e' can be any hexadecimal i2c device
   address, and '10dd' any hexadecimal isa address.

   The 'prefix' part in the result is freshly allocated. All old contents
   of res is overwritten. res itself is not allocated. In case of an error
   return (ie. != 0), res is undefined, but all allocations are undone.
*/

int sensors_parse_chip_name(const char *name, sensors_chip_name *res)
{
	char *dash;

	/* First, the prefix. It's either "*" or a real chip name. */
	if (!strncmp(name, "*-", 2)) {
		res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
		name += 2;
	} else {
		if (!(dash = strchr(name, '-')))
			return -SENSORS_ERR_CHIP_NAME;
		res->prefix = strndup(name, dash - name);
		if (!res->prefix)
			sensors_fatal_error(__func__,
					    "Allocating name prefix");
		name = dash + 1;
	}

	/* Then we have either a sole "*" (all chips with this name) or a bus
	   type and an address. */
	if (!strcmp(name, "*")) {
		res->bus.type = SENSORS_BUS_TYPE_ANY;
		res->bus.nr = SENSORS_BUS_NR_ANY;
		res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
		return 0;
	}

	if (!(dash = strchr(name, '-')))
		goto ERROR;
	if (!strncmp(name, "i2c", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_I2C;
	else if (!strncmp(name, "isa", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_ISA;
	else if (!strncmp(name, "pci", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_PCI;
	else if (!strncmp(name, "spi", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_SPI;
	else if (!strncmp(name, "virtual", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_VIRTUAL;
	else if (!strncmp(name, "acpi", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_ACPI;
	else if (!strncmp(name, "hid", dash - name))
		res->bus.type = SENSORS_BUS_TYPE_HID;
	else
		goto ERROR;
	name = dash + 1;

	/* Some bus types (i2c, spi) have an additional bus number.
	   For these, the next part is either a "*" (any bus of that type)
	   or a decimal number. */
	switch (res->bus.type) {
	case SENSORS_BUS_TYPE_I2C:
	case SENSORS_BUS_TYPE_SPI:
	case SENSORS_BUS_TYPE_HID:
		if (!strncmp(name, "*-", 2)) {
			res->bus.nr = SENSORS_BUS_NR_ANY;
			name += 2;
			break;
		}

		res->bus.nr = strtoul(name, &dash, 10);
		if (*name == '\0' || *dash != '-' || res->bus.nr < 0)
			goto ERROR;
		name = dash + 1;
		break;
	default:
		res->bus.nr = SENSORS_BUS_NR_ANY;
	}

	/* Last part is the chip address, or "*" for any address. */
	if (!strcmp(name, "*")) {
		res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
	} else {
		res->addr = strtoul(name, &dash, 16);
		if (*name == '\0' || *dash != '\0' || res->addr < 0)
			goto ERROR;
	}

	return 0;

ERROR:
	free(res->prefix);
	return -SENSORS_ERR_CHIP_NAME;
}

int sensors_snprintf_chip_name(char *str, size_t size,
			       const sensors_chip_name *chip)
{
	if (sensors_chip_name_has_wildcards(chip))
		return -SENSORS_ERR_WILDCARDS;

	switch (chip->bus.type) {
	case SENSORS_BUS_TYPE_ISA:
		return snprintf(str, size, "%s-isa-%04x", chip->prefix,
				chip->addr);
	case SENSORS_BUS_TYPE_PCI:
		return snprintf(str, size, "%s-pci-%04x", chip->prefix,
				chip->addr);
	case SENSORS_BUS_TYPE_I2C:
		return snprintf(str, size, "%s-i2c-%hd-%02x", chip->prefix,
				chip->bus.nr, chip->addr);
	case SENSORS_BUS_TYPE_SPI:
		return snprintf(str, size, "%s-spi-%hd-%x", chip->prefix,
				chip->bus.nr, chip->addr);
	case SENSORS_BUS_TYPE_VIRTUAL:
		return snprintf(str, size, "%s-virtual-%x", chip->prefix,
				chip->addr);
	case SENSORS_BUS_TYPE_ACPI:
		return snprintf(str, size, "%s-acpi-%x", chip->prefix,
				chip->addr);
	case SENSORS_BUS_TYPE_HID:
		return snprintf(str, size, "%s-hid-%hd-%x", chip->prefix,
				chip->bus.nr, chip->addr);
	}

	return -SENSORS_ERR_CHIP_NAME;
}

int sensors_parse_bus_id(const char *name, sensors_bus_id *bus)
{
	char *endptr;

	if (strncmp(name, "i2c-", 4)) {
		return -SENSORS_ERR_BUS_NAME;
	}
	name += 4;
	bus->type = SENSORS_BUS_TYPE_I2C;
	bus->nr = strtoul(name, &endptr, 10);
	if (*name == '\0' || *endptr != '\0' || bus->nr < 0)
		return -SENSORS_ERR_BUS_NAME;
	return 0;
}

static int sensors_substitute_chip(sensors_chip_name *name,
				   const char *filename, int lineno)
{
	int i, j;
	for (i = 0; i < sensors_config_busses_count; i++)
		if (sensors_config_busses[i].bus.type == name->bus.type &&
		    sensors_config_busses[i].bus.nr == name->bus.nr)
			break;

	if (i == sensors_config_busses_count) {
		sensors_parse_error_wfn("Undeclared bus id referenced",
					filename, lineno);
		name->bus.nr = SENSORS_BUS_NR_IGNORE;
		return -SENSORS_ERR_BUS_NAME;
	}

	/* Compare the adapter names */
	for (j = 0; j < sensors_proc_bus_count; j++) {
		if (!strcmp(sensors_config_busses[i].adapter,
			    sensors_proc_bus[j].adapter)) {
			name->bus.nr = sensors_proc_bus[j].bus.nr;
			return 0;
		}
	}

	/* We did not find a matching bus name, simply ignore this chip
	   config entry. */
	name->bus.nr = SENSORS_BUS_NR_IGNORE;
	return 0;
}

/* Bus substitution is on a per-configuration file basis, so we keep
   memory (in sensors_config_chips_subst) of which chip entries have been
   already substituted. */
int sensors_substitute_busses(void)
{
	int err, i, j, lineno;
	sensors_chip_name_list *chips;
	const char *filename;
	int res = 0;

	for (i = sensors_config_chips_subst;
	     i < sensors_config_chips_count; i++) {
		filename = sensors_config_chips[i].line.filename;
		lineno = sensors_config_chips[i].line.lineno;
		chips = &sensors_config_chips[i].chips;
		for (j = 0; j < chips->fits_count; j++) {
			/* We can only substitute if a specific bus number
			   is given. */
			if (chips->fits[j].bus.nr == SENSORS_BUS_NR_ANY)
				continue;

			err = sensors_substitute_chip(&chips->fits[j],
						      filename, lineno);
			if (err)
				res = err;
		}
	}
	sensors_config_chips_subst = sensors_config_chips_count;
	return res;
}