1 /*
2     i2c-proc.h - Part of the i2c package
3     was originally sensors.h - Part of lm_sensors, Linux kernel modules
4                                for hardware monitoring
5     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 
22 #ifndef SENSORS_SENSORS_H
23 #define SENSORS_SENSORS_H
24 
25 #ifdef __KERNEL__
26 
27 #include <linux/sysctl.h>
28 
29 /* The type of callback functions used in sensors_{proc,sysctl}_real */
30 typedef void (*i2c_real_callback) (struct i2c_client * client,
31 				       int operation, int ctl_name,
32 				       int *nrels_mag, long *results);
33 
34 /* Values for the operation field in the above function type */
35 #define SENSORS_PROC_REAL_INFO 1
36 #define SENSORS_PROC_REAL_READ 2
37 #define SENSORS_PROC_REAL_WRITE 3
38 
39 /* These funcion reads or writes a 'real' value (encoded by the combination
40    of an integer and a magnitude, the last is the power of ten the value
41    should be divided with) to a /proc/sys directory. To use these functions,
42    you must (before registering the ctl_table) set the extra2 field to the
43    client, and the extra1 field to a function of the form:
44       void func(struct i2c_client *client, int operation, int ctl_name,
45                 int *nrels_mag, long *results)
46    This last function can be called for three values of operation. If
47    operation equals SENSORS_PROC_REAL_INFO, the magnitude should be returned
48    in nrels_mag. If operation equals SENSORS_PROC_REAL_READ, values should
49    be read into results. nrels_mag should return the number of elements
50    read; the maximum number is put in it on entry. Finally, if operation
51    equals SENSORS_PROC_REAL_WRITE, the values in results should be
52    written to the chip. nrels_mag contains on entry the number of elements
53    found.
54    In all cases, client points to the client we wish to interact with,
55    and ctl_name is the SYSCTL id of the file we are accessing. */
56 extern int i2c_sysctl_real(ctl_table * table, int *name, int nlen,
57 			       void *oldval, size_t * oldlenp,
58 			       void *newval, size_t newlen,
59 			       void **context);
60 extern int i2c_proc_real(ctl_table * ctl, int write, struct file *filp,
61 			     void *buffer, size_t * lenp);
62 
63 
64 
65 /* These rather complex functions must be called when you want to add or
66    delete an entry in /proc/sys/dev/sensors/chips (not yet implemented). It
67    also creates a new directory within /proc/sys/dev/sensors/.
68    ctl_template should be a template of the newly created directory. It is
69    copied in memory. The extra2 field of each file is set to point to client.
70    If any driver wants subdirectories within the newly created directory,
71    these functions must be updated! */
72 extern int i2c_register_entry(struct i2c_client *client,
73 				  const char *prefix,
74 				  ctl_table * ctl_template,
75 				  struct module *controlling_mod);
76 
77 extern void i2c_deregister_entry(int id);
78 
79 
80 /* A structure containing detect information.
81    Force variables overrule all other variables; they force a detection on
82    that place. If a specific chip is given, the module blindly assumes this
83    chip type is present; if a general force (kind == 0) is given, the module
84    will still try to figure out what type of chip is present. This is useful
85    if for some reasons the detect for SMBus or ISA address space filled
86    fails.
87    probe: insmod parameter. Initialize this list with SENSORS_I2C_END values.
88      A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for
89      the ISA bus, -1 for any I2C bus), the second is the address.
90    kind: The kind of chip. 0 equals any chip.
91 */
92 struct i2c_force_data {
93 	unsigned short *force;
94 	unsigned short kind;
95 };
96 
97 /* A structure containing the detect information.
98    normal_i2c: filled in by the module writer. Terminated by SENSORS_I2C_END.
99      A list of I2C addresses which should normally be examined.
100    normal_i2c_range: filled in by the module writer. Terminated by
101      SENSORS_I2C_END
102      A list of pairs of I2C addresses, each pair being an inclusive range of
103      addresses which should normally be examined.
104    normal_isa: filled in by the module writer. Terminated by SENSORS_ISA_END.
105      A list of ISA addresses which should normally be examined.
106    normal_isa_range: filled in by the module writer. Terminated by
107      SENSORS_ISA_END
108      A list of triples. The first two elements are ISA addresses, being an
109      range of addresses which should normally be examined. The third is the
110      modulo parameter: only addresses which are 0 module this value relative
111      to the first address of the range are actually considered.
112    probe: insmod parameter. Initialize this list with SENSORS_I2C_END values.
113      A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for
114      the ISA bus, -1 for any I2C bus), the second is the address. These
115      addresses are also probed, as if they were in the 'normal' list.
116    probe_range: insmod parameter. Initialize this list with SENSORS_I2C_END
117      values.
118      A list of triples. The first value is a bus number (SENSORS_ISA_BUS for
119      the ISA bus, -1 for any I2C bus), the second and third are addresses.
120      These form an inclusive range of addresses that are also probed, as
121      if they were in the 'normal' list.
122    ignore: insmod parameter. Initialize this list with SENSORS_I2C_END values.
123      A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for
124      the ISA bus, -1 for any I2C bus), the second is the I2C address. These
125      addresses are never probed. This parameter overrules 'normal' and
126      'probe', but not the 'force' lists.
127    ignore_range: insmod parameter. Initialize this list with SENSORS_I2C_END
128       values.
129      A list of triples. The first value is a bus number (SENSORS_ISA_BUS for
130      the ISA bus, -1 for any I2C bus), the second and third are addresses.
131      These form an inclusive range of I2C addresses that are never probed.
132      This parameter overrules 'normal' and 'probe', but not the 'force' lists.
133    force_data: insmod parameters. A list, ending with an element of which
134      the force field is NULL.
135 */
136 struct i2c_address_data {
137 	unsigned short *normal_i2c;
138 	unsigned short *normal_i2c_range;
139 	unsigned int *normal_isa;
140 	unsigned int *normal_isa_range;
141 	unsigned short *probe;
142 	unsigned short *probe_range;
143 	unsigned short *ignore;
144 	unsigned short *ignore_range;
145 	struct i2c_force_data *forces;
146 };
147 
148 /* Internal numbers to terminate lists */
149 #define SENSORS_I2C_END 0xfffe
150 #define SENSORS_ISA_END 0xfffefffe
151 
152 /* The numbers to use to set an ISA or I2C bus address */
153 #define SENSORS_ISA_BUS 9191
154 #define SENSORS_ANY_I2C_BUS 0xffff
155 
156 /* The length of the option lists */
157 #define SENSORS_MAX_OPTS 48
158 
159 /* Default fill of many variables */
160 #define SENSORS_DEFAULTS {SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
161                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
162                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
163                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
164                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
165                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
166                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
167                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
168                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
169                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
170                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
171                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
172                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
173                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
174                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END, \
175                           SENSORS_I2C_END, SENSORS_I2C_END, SENSORS_I2C_END}
176 
177 /* This is ugly. We need to evaluate SENSORS_MAX_OPTS before it is
178    stringified */
179 #define SENSORS_MODPARM_AUX1(x) "1-" #x "h"
180 #define SENSORS_MODPARM_AUX(x) SENSORS_MODPARM_AUX1(x)
181 #define SENSORS_MODPARM SENSORS_MODPARM_AUX(SENSORS_MAX_OPTS)
182 
183 /* SENSORS_MODULE_PARM creates a module parameter, and puts it in the
184    module header */
185 #define SENSORS_MODULE_PARM(var,desc) \
186   static unsigned short var[SENSORS_MAX_OPTS] = SENSORS_DEFAULTS; \
187   MODULE_PARM(var,SENSORS_MODPARM); \
188   MODULE_PARM_DESC(var,desc)
189 
190 /* SENSORS_MODULE_PARM creates a 'force_*' module parameter, and puts it in
191    the module header */
192 #define SENSORS_MODULE_PARM_FORCE(name) \
193   SENSORS_MODULE_PARM(force_ ## name, \
194                       "List of adapter,address pairs which are unquestionably" \
195                       " assumed to contain a `" # name "' chip")
196 
197 
198 /* This defines several insmod variables, and the addr_data structure */
199 #define SENSORS_INSMOD \
200   SENSORS_MODULE_PARM(probe, \
201                       "List of adapter,address pairs to scan additionally"); \
202   SENSORS_MODULE_PARM(probe_range, \
203                       "List of adapter,start-addr,end-addr triples to scan " \
204                       "additionally"); \
205   SENSORS_MODULE_PARM(ignore, \
206                       "List of adapter,address pairs not to scan"); \
207   SENSORS_MODULE_PARM(ignore_range, \
208                       "List of adapter,start-addr,end-addr triples not to " \
209                       "scan"); \
210   static struct i2c_address_data addr_data = \
211                                        {normal_i2c, normal_i2c_range, \
212                                         normal_isa, normal_isa_range, \
213                                         probe, probe_range, \
214                                         ignore, ignore_range, \
215                                         forces}
216 
217 /* The following functions create an enum with the chip names as elements.
218    The first element of the enum is any_chip. These are the only macros
219    a module will want to use. */
220 
221 #define SENSORS_INSMOD_0 \
222   enum chips { any_chip }; \
223   SENSORS_MODULE_PARM(force, \
224                       "List of adapter,address pairs to boldly assume " \
225                       "to be present"); \
226   static struct i2c_force_data forces[] = {{force,any_chip},{NULL}}; \
227   SENSORS_INSMOD
228 
229 #define SENSORS_INSMOD_1(chip1) \
230   enum chips { any_chip, chip1 }; \
231   SENSORS_MODULE_PARM(force, \
232                       "List of adapter,address pairs to boldly assume " \
233                       "to be present"); \
234   SENSORS_MODULE_PARM_FORCE(chip1); \
235   static struct i2c_force_data forces[] = {{force,any_chip},\
236                                                  {force_ ## chip1,chip1}, \
237                                                  {NULL}}; \
238   SENSORS_INSMOD
239 
240 #define SENSORS_INSMOD_2(chip1,chip2) \
241   enum chips { any_chip, chip1, chip2 }; \
242   SENSORS_MODULE_PARM(force, \
243                       "List of adapter,address pairs to boldly assume " \
244                       "to be present"); \
245   SENSORS_MODULE_PARM_FORCE(chip1); \
246   SENSORS_MODULE_PARM_FORCE(chip2); \
247   static struct i2c_force_data forces[] = {{force,any_chip}, \
248                                                  {force_ ## chip1,chip1}, \
249                                                  {force_ ## chip2,chip2}, \
250                                                  {NULL}}; \
251   SENSORS_INSMOD
252 
253 #define SENSORS_INSMOD_3(chip1,chip2,chip3) \
254   enum chips { any_chip, chip1, chip2, chip3 }; \
255   SENSORS_MODULE_PARM(force, \
256                       "List of adapter,address pairs to boldly assume " \
257                       "to be present"); \
258   SENSORS_MODULE_PARM_FORCE(chip1); \
259   SENSORS_MODULE_PARM_FORCE(chip2); \
260   SENSORS_MODULE_PARM_FORCE(chip3); \
261   static struct i2c_force_data forces[] = {{force,any_chip}, \
262                                                  {force_ ## chip1,chip1}, \
263                                                  {force_ ## chip2,chip2}, \
264                                                  {force_ ## chip3,chip3}, \
265                                                  {NULL}}; \
266   SENSORS_INSMOD
267 
268 #define SENSORS_INSMOD_4(chip1,chip2,chip3,chip4) \
269   enum chips { any_chip, chip1, chip2, chip3, chip4 }; \
270   SENSORS_MODULE_PARM(force, \
271                       "List of adapter,address pairs to boldly assume " \
272                       "to be present"); \
273   SENSORS_MODULE_PARM_FORCE(chip1); \
274   SENSORS_MODULE_PARM_FORCE(chip2); \
275   SENSORS_MODULE_PARM_FORCE(chip3); \
276   SENSORS_MODULE_PARM_FORCE(chip4); \
277   static struct i2c_force_data forces[] = {{force,any_chip}, \
278                                                  {force_ ## chip1,chip1}, \
279                                                  {force_ ## chip2,chip2}, \
280                                                  {force_ ## chip3,chip3}, \
281                                                  {force_ ## chip4,chip4}, \
282                                                  {NULL}}; \
283   SENSORS_INSMOD
284 
285 #define SENSORS_INSMOD_5(chip1,chip2,chip3,chip4,chip5) \
286   enum chips { any_chip, chip1, chip2, chip3, chip4, chip5 }; \
287   SENSORS_MODULE_PARM(force, \
288                       "List of adapter,address pairs to boldly assume " \
289                       "to be present"); \
290   SENSORS_MODULE_PARM_FORCE(chip1); \
291   SENSORS_MODULE_PARM_FORCE(chip2); \
292   SENSORS_MODULE_PARM_FORCE(chip3); \
293   SENSORS_MODULE_PARM_FORCE(chip4); \
294   SENSORS_MODULE_PARM_FORCE(chip5); \
295   static struct i2c_force_data forces[] = {{force,any_chip}, \
296                                                  {force_ ## chip1,chip1}, \
297                                                  {force_ ## chip2,chip2}, \
298                                                  {force_ ## chip3,chip3}, \
299                                                  {force_ ## chip4,chip4}, \
300                                                  {force_ ## chip5,chip5}, \
301                                                  {NULL}}; \
302   SENSORS_INSMOD
303 
304 #define SENSORS_INSMOD_6(chip1,chip2,chip3,chip4,chip5,chip6) \
305   enum chips { any_chip, chip1, chip2, chip3, chip4, chip5, chip6 }; \
306   SENSORS_MODULE_PARM(force, \
307                       "List of adapter,address pairs to boldly assume " \
308                       "to be present"); \
309   SENSORS_MODULE_PARM_FORCE(chip1); \
310   SENSORS_MODULE_PARM_FORCE(chip2); \
311   SENSORS_MODULE_PARM_FORCE(chip3); \
312   SENSORS_MODULE_PARM_FORCE(chip4); \
313   SENSORS_MODULE_PARM_FORCE(chip5); \
314   SENSORS_MODULE_PARM_FORCE(chip6); \
315   static struct i2c_force_data forces[] = {{force,any_chip}, \
316                                                  {force_ ## chip1,chip1}, \
317                                                  {force_ ## chip2,chip2}, \
318                                                  {force_ ## chip3,chip3}, \
319                                                  {force_ ## chip4,chip4}, \
320                                                  {force_ ## chip5,chip5}, \
321                                                  {force_ ## chip6,chip6}, \
322                                                  {NULL}}; \
323   SENSORS_INSMOD
324 
325 #define SENSORS_INSMOD_7(chip1,chip2,chip3,chip4,chip5,chip6,chip7) \
326   enum chips { any_chip, chip1, chip2, chip3, chip4, chip5, chip6, chip7 }; \
327   SENSORS_MODULE_PARM(force, \
328                       "List of adapter,address pairs to boldly assume " \
329                       "to be present"); \
330   SENSORS_MODULE_PARM_FORCE(chip1); \
331   SENSORS_MODULE_PARM_FORCE(chip2); \
332   SENSORS_MODULE_PARM_FORCE(chip3); \
333   SENSORS_MODULE_PARM_FORCE(chip4); \
334   SENSORS_MODULE_PARM_FORCE(chip5); \
335   SENSORS_MODULE_PARM_FORCE(chip6); \
336   SENSORS_MODULE_PARM_FORCE(chip7); \
337   static struct i2c_force_data forces[] = {{force,any_chip}, \
338                                                  {force_ ## chip1,chip1}, \
339                                                  {force_ ## chip2,chip2}, \
340                                                  {force_ ## chip3,chip3}, \
341                                                  {force_ ## chip4,chip4}, \
342                                                  {force_ ## chip5,chip5}, \
343                                                  {force_ ## chip6,chip6}, \
344                                                  {force_ ## chip7,chip7}, \
345                                                  {NULL}}; \
346   SENSORS_INSMOD
347 
348 typedef int i2c_found_addr_proc(struct i2c_adapter *adapter,
349 				    int addr, unsigned short flags,
350 				    int kind);
351 
352 /* Detect function. It iterates over all possible addresses itself. For
353    SMBus addresses, it will only call found_proc if some client is connected
354    to the SMBus (unless a 'force' matched); for ISA detections, this is not
355    done. */
356 extern int i2c_detect(struct i2c_adapter *adapter,
357 			  struct i2c_address_data *address_data,
358 			  i2c_found_addr_proc * found_proc);
359 
360 
361 /* This macro is used to scale user-input to sensible values in almost all
362    chip drivers. */
SENSORS_LIMIT(long value,long low,long high)363 static inline int SENSORS_LIMIT(long value, long low, long high)
364 {
365 	if (value < low)
366 		return low;
367 	else if (value > high)
368 		return high;
369 	else
370 		return value;
371 }
372 
373 #endif				/* def __KERNEL__ */
374 
375 
376 /* The maximum length of the prefix */
377 #define SENSORS_PREFIX_MAX 20
378 
379 /* Sysctl IDs */
380 #ifdef DEV_HWMON
381 #define DEV_SENSORS DEV_HWMON
382 #else				/* ndef DEV_HWMOM */
383 #define DEV_SENSORS 2		/* The id of the lm_sensors directory within the
384 				   dev table */
385 #endif				/* def DEV_HWMON */
386 
387 #define SENSORS_CHIPS 1
388 struct i2c_chips_data {
389 	int sysctl_id;
390 	char name[SENSORS_PREFIX_MAX + 13];
391 };
392 
393 #endif				/* def SENSORS_SENSORS_H */
394 
395