root/lm-sensors/trunk/lib/data.c @ 3090

Revision 3090, 8.2 KB (checked in by mmh, 8 years ago)

(mmh)
This patch moves the chip and bus lists from lib/proc.c to lib/data.c.
The variables were already declared in lib/data.h. Motivation: when
support for kernel 2.4.x becomes optional, lib/proc.c may not even be
compiled into the library... but these variables are still needed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    data.c - Part of libsensors, a Linux library for reading sensor data.
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdlib.h>
21#include <string.h>
22
23#include "error.h"
24#include "data.h"
25#include "sensors.h"
26#include "../version.h"
27
28const char *libsensors_version = LM_VERSION;
29const char *libsensors_date = LM_DATE;
30
31sensors_chip *sensors_config_chips = NULL;
32int sensors_config_chips_count = 0;
33int sensors_config_chips_max = 0;
34
35sensors_bus *sensors_config_busses = NULL;
36int sensors_config_busses_count = 0;
37int sensors_config_busses_max = 0;
38
39sensors_proc_chips_entry *sensors_proc_chips = NULL;
40int sensors_proc_chips_count = 0;
41int sensors_proc_chips_max = 0;
42
43sensors_bus *sensors_proc_bus = NULL;
44int sensors_proc_bus_count = 0;
45int sensors_proc_bus_max = 0;
46
47static int sensors_substitute_chip(sensors_chip_name *name,int lineno);
48
49/* Wow, this must be one of the ugliest functions I have ever written.
50   The idea is that it parses a chip name. These are valid names:
51
52     lm78-i2c-10-5e             *-i2c-10-5e
53     lm78-i2c-10-*              *-i2c-10-*
54     lm78-i2c-*-5e              *-i2c-*-5e
55     lm78-i2c-*-*               *-i2c-*-*
56     lm78-isa-10dd              *-isa-10dd
57     lm78-isa-*                 *-isa-*
58     lm78-*                     *-*
59                                *
60   Here 'lm78' can be any prefix. To complicate matters, such a prefix
61   can also contain dashes (like lm78-j, for example!). 'i2c' and 'isa' are
62   literal strings, just like all dashes '-' and wildcards '*'. '10' can
63   be any decimal i2c bus number. '5e' can be any hexadecimal i2c device
64   address, and '10dd' any hexadecimal isa address.
65
66   If '*' is used in prefixes, together with dashes, ambigious parses are
67   introduced. In that case, the prefix is kept as small as possible.
68
69   The 'prefix' part in the result is freshly allocated. All old contents
70   of res is overwritten. res itself is not allocated. In case of an error
71   return (ie. != 0), res is undefined, but all allocations are undone.
72
73   Don't tell me there are bugs in here, because I'll start screaming :-)
74*/
75
76int sensors_parse_chip_name(const char *orig_name, sensors_chip_name *res)
77{
78  char *part2, *part3, *part4;
79  char *name = strdup(orig_name);
80  int i;
81
82  /* Play it safe */
83  res->busname = NULL;
84 
85  if (! name)
86    sensors_fatal_error("sensors_parse_chip_name","Allocating new name");
87  /* First split name in upto four pieces. */
88  if ((part4 = strrchr(name,'-')))
89    *part4++ = '\0';
90  if ((part3 = strrchr(name,'-')))
91    *part3++ = '\0';
92  if ((part2 = strrchr(name,'-'))) 
93    *part2++ = '\0';
94
95  /* No dashes found? */
96  if (! part4) {
97    if (!strcmp(name,"*")) {
98      res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
99      res->bus = SENSORS_CHIP_NAME_BUS_ANY;
100      res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
101      goto SUCCES;
102    } else 
103      goto ERROR;
104  }
105
106  /* At least one dash found. Now part4 is either '*', or an address */
107  if (!strcmp(part4,"*"))
108    res->addr = SENSORS_CHIP_NAME_ADDR_ANY;
109  else {
110    if ((strlen(part4) > 4) || (strlen(part4) == 0))
111      goto ERROR;
112    res->addr = 0;
113    for (i = 0; ; i++) { 
114      switch (part4[i]) {
115      case '0': case '1': case '2': case '3': case '4':
116      case '5': case '6': case '7': case '8': case '9':
117        res->addr = res->addr * 16 + part4[i] - '0';
118        break;
119      case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
120        res->addr = res->addr * 16 + part4[i] - 'a' + 10;
121        break;
122      case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
123        res->addr = res->addr * 16 + part4[i] - 'A' + 10;
124        break;
125      case 0:
126        goto DONE;
127      default:
128        goto ERROR;
129      }
130    }
131DONE:;
132  }
133
134  /* OK. So let's look at part3. It must either be the number of the
135     i2c bus (and then part2 *must* be "i2c"), or it must be "isa",
136     or, if part4 was "*", it belongs to 'prefix'. Or no second dash
137     was found at all, of course. */
138  if (! part3) {
139    if (res->addr == SENSORS_CHIP_NAME_ADDR_ANY) {
140      res->bus = SENSORS_CHIP_NAME_BUS_ANY;
141    } else
142      goto ERROR;
143  } else if (!strcmp(part3,"isa")) {
144    res->bus = SENSORS_CHIP_NAME_BUS_ISA;
145    if (part2)
146      *(part2-1) = '-';
147  } else if (part2 && !strcmp(part2,"i2c") && !strcmp(part3,"*"))
148    res->bus = SENSORS_CHIP_NAME_BUS_ANY_I2C;
149  else if (part2 && !strcmp(part2,"i2c")) {
150    if ((strlen(part3) > 3) || (strlen(part3) == 0))
151      goto ERROR;
152    res->bus = 0;
153    for (i = 0; ; i++) { 
154      switch (part3[i]) {
155      case '0': case '1': case '2': case '3': case '4':
156      case '5': case '6': case '7': case '8': case '9':
157        res->bus = res->bus * 10 + part3[i] - '0';
158        break;
159      case 0:
160        goto DONE2;
161      default:
162        goto ERROR;
163      }
164    }
165DONE2:;
166  } else if (res->addr == SENSORS_CHIP_NAME_ADDR_ANY) {
167    res->bus = SENSORS_CHIP_NAME_BUS_ANY;
168    if (part2)
169      *(part2-1) = '-';
170    *(part3-1) = '-';
171  } else if(part3 && part4) {
172    res->bus = SENSORS_CHIP_NAME_BUS_DUMMY;
173    if (! (res->busname = strdup(part3)))
174      sensors_fatal_error("sensors_parse_chip_name","Allocating new busname");
175  } else
176    goto ERROR;
177   
178  if (!strcmp(name,"*"))
179    res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
180  else if (! (res->prefix = strdup(name)))
181    sensors_fatal_error("sensors_parse_chip_name","Allocating new name");
182  goto SUCCES;
183
184SUCCES:
185  free(name);
186  return 0;
187
188ERROR:
189  free(name);
190  return -SENSORS_ERR_CHIP_NAME;
191}
192
193int sensors_parse_i2cbus_name(const char *name, int *res)
194{
195  int i;
196
197  if (! strcmp(name,"isa")) {
198    *res = SENSORS_CHIP_NAME_BUS_ISA;
199    return 0;
200  }
201  if (strncmp(name,"i2c-",4)) {
202    *res = SENSORS_CHIP_NAME_BUS_DUMMY;
203    return 0;
204  }
205  name += 4;
206  if ((strlen(name) > 3) || (strlen(name) == 0))
207    return -SENSORS_ERR_BUS_NAME;
208  *res = 0;
209  for (i = 0; ; i++) { 
210    switch (name[i]) {
211    case '0': case '1': case '2': case '3': case '4':
212    case '5': case '6': case '7': case '8': case '9':
213      *res = *res * 10 + name[i] - '0';
214      break;
215    case 0:
216      return 0;
217    default:
218      return -SENSORS_ERR_BUS_NAME;
219    }
220  }
221}
222
223
224int sensors_substitute_chip(sensors_chip_name *name,int lineno)
225{
226  int i,j;
227  for (i = 0; i < sensors_config_busses_count; i++)
228    if (sensors_config_busses[i].number == name->bus)
229      break;
230
231  if (i == sensors_config_busses_count) {
232    sensors_parse_error("Undeclared i2c bus referenced",lineno);
233    name->bus = sensors_proc_bus_count;
234    return -SENSORS_ERR_BUS_NAME;
235  }
236
237  /* We used to compare both the adapter and the algorithm names for
238     bus matching, but Linux 2.6 has no more names for algorithms, and
239     it was redundant anyway. So we now only rely on the adapter name. */
240  for (j = 0; j < sensors_proc_bus_count; j++) {
241    if (!strcmp(sensors_config_busses[i].adapter,
242                sensors_proc_bus[j].adapter)) {
243      name->bus = sensors_proc_bus[j].number;
244      return 0;
245    }
246  }
247
248  /* We did not find anything. sensors_proc_bus_count is not a valid
249     bus number, so it will never be matched. Good. */
250  name->bus = sensors_proc_bus_count;
251  return 0;
252}
253
254     
255int sensors_substitute_busses(void)
256{
257  int err,i,j,lineno;
258  sensors_chip_name_list *chips;
259  int res=0;
260 
261  for(i = 0; i < sensors_config_chips_count; i++) {
262    lineno = sensors_config_chips[i].lineno;
263    chips = &sensors_config_chips[i].chips;
264    for(j = 0; j < chips->fits_count; j++)
265      if ((chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ISA) &&
266          (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_DUMMY) &&
267          (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY) &&
268          (chips->fits[j].bus != SENSORS_CHIP_NAME_BUS_ANY_I2C))
269        if ((err = sensors_substitute_chip(chips->fits+j, lineno)))
270          res = err;
271  }
272  return res;
273}
Note: See TracBrowser for help on using the browser.