1 /*
2     i2c-proc.c - Part of lm_sensors, Linux kernel modules for hardware
3                 monitoring
4     Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl> and
5     Mark D. Studebaker <mdsxyz123@yahoo.com>
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 /*
23     This driver puts entries in /proc/sys/dev/sensors for each I2C device
24 */
25 
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <linux/sysctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-proc.h>
36 #include <asm/uaccess.h>
37 
38 #ifndef THIS_MODULE
39 #define THIS_MODULE NULL
40 #endif
41 
42 static int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
43 			       long *results, int magnitude);
44 static int i2c_write_reals(int nrels, char *buffer, size_t *bufsize,
45 			       long *results, int magnitude);
46 static int i2c_proc_chips(ctl_table * ctl, int write,
47 			      struct file *filp, void *buffer,
48 			      size_t * lenp);
49 static int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
50 				void *oldval, size_t * oldlenp,
51 				void *newval, size_t newlen,
52 				void **context);
53 
54 int __init sensors_init(void);
55 
56 #define SENSORS_ENTRY_MAX 20
57 static struct ctl_table_header *i2c_entries[SENSORS_ENTRY_MAX];
58 
59 static struct i2c_client *i2c_clients[SENSORS_ENTRY_MAX];
60 static unsigned short i2c_inodes[SENSORS_ENTRY_MAX];
61 
62 static ctl_table sysctl_table[] = {
63 	{CTL_DEV, "dev", NULL, 0, 0555},
64 	{0},
65 	{DEV_SENSORS, "sensors", NULL, 0, 0555},
66 	{0},
67 	{0, NULL, NULL, 0, 0555},
68 	{0}
69 };
70 
71 static ctl_table i2c_proc_dev_sensors[] = {
72 	{SENSORS_CHIPS, "chips", NULL, 0, 0644, NULL, &i2c_proc_chips,
73 	 &i2c_sysctl_chips},
74 	{0}
75 };
76 
77 static ctl_table i2c_proc_dev[] = {
78 	{DEV_SENSORS, "sensors", NULL, 0, 0555, i2c_proc_dev_sensors},
79 	{0},
80 };
81 
82 
83 static ctl_table i2c_proc[] = {
84 	{CTL_DEV, "dev", NULL, 0, 0555, i2c_proc_dev},
85 	{0}
86 };
87 
88 
89 static struct ctl_table_header *i2c_proc_header;
90 static int i2c_initialized;
91 
92 /* This returns a nice name for a new directory; for example lm78-isa-0310
93    (for a LM78 chip on the ISA bus at port 0x310), or lm75-i2c-3-4e (for
94    a LM75 chip on the third i2c bus at address 0x4e).
95    name is allocated first. */
i2c_create_name(char ** name,const char * prefix,struct i2c_adapter * adapter,int addr)96 int i2c_create_name(char **name, const char *prefix,
97 			struct i2c_adapter *adapter, int addr)
98 {
99 	char name_buffer[50];
100 	int id;
101 	if (i2c_is_isa_adapter(adapter))
102 		sprintf(name_buffer, "%s-isa-%04x", prefix, addr);
103 	else {
104 		if ((id = i2c_adapter_id(adapter)) < 0)
105 			return -ENOENT;
106 		sprintf(name_buffer, "%s-i2c-%d-%02x", prefix, id, addr);
107 	}
108 	*name = kmalloc(strlen(name_buffer) + 1, GFP_KERNEL);
109 	if (!*name) {
110 		printk (KERN_WARNING "i2c_create_name: not enough memory\n");
111 		return -ENOMEM;
112 	}
113 	strcpy(*name, name_buffer);
114 	return 0;
115 }
116 
117 /* This rather complex function must be called when you want to add an entry
118    to /proc/sys/dev/sensors/chips. It also creates a new directory within
119    /proc/sys/dev/sensors/.
120    ctl_template should be a template of the newly created directory. It is
121    copied in memory. The extra2 field of each file is set to point to client.
122    If any driver wants subdirectories within the newly created directory,
123    this function must be updated!
124    controlling_mod is the controlling module. It should usually be
125    THIS_MODULE when calling. */
i2c_register_entry(struct i2c_client * client,const char * prefix,ctl_table * ctl_template,struct module * controlling_mod)126 int i2c_register_entry(struct i2c_client *client, const char *prefix,
127 			   ctl_table *ctl_template,
128 			   struct module *controlling_mod)
129 {
130 	int i, res, len, id;
131 	ctl_table *new_table;
132 	char *name;
133 	struct ctl_table_header *new_header;
134 
135 	if ((res = i2c_create_name(&name, prefix, client->adapter,
136 				       client->addr))) return res;
137 
138 	for (id = 0; id < SENSORS_ENTRY_MAX; id++)
139 		if (!i2c_entries[id]) {
140 			break;
141 		}
142 	if (id == SENSORS_ENTRY_MAX) {
143 		kfree(name);
144 		return -ENOMEM;
145 	}
146 	id += 256;
147 
148 	len = 0;
149 	while (ctl_template[len].ctl_name)
150 		len++;
151 	len += 7;
152 	if (!(new_table = kmalloc(sizeof(ctl_table) * len, GFP_KERNEL))) {
153 		kfree(name);
154 		return -ENOMEM;
155 	}
156 
157 	memcpy(new_table, sysctl_table, 6 * sizeof(ctl_table));
158 	new_table[0].child = &new_table[2];
159 	new_table[2].child = &new_table[4];
160 	new_table[4].child = &new_table[6];
161 	new_table[4].procname = name;
162 	new_table[4].ctl_name = id;
163 	memcpy(new_table + 6, ctl_template, (len - 6) * sizeof(ctl_table));
164 	for (i = 6; i < len; i++)
165 		new_table[i].extra2 = client;
166 
167 	if (!(new_header = register_sysctl_table(new_table, 0))) {
168 		kfree(new_table);
169 		kfree(name);
170 		return -ENOMEM;
171 	}
172 
173 	i2c_entries[id - 256] = new_header;
174 
175 	i2c_clients[id - 256] = client;
176 #ifdef DEBUG
177 	if (!new_header || !new_header->ctl_table ||
178 	    !new_header->ctl_table->child ||
179 	    !new_header->ctl_table->child->child ||
180 	    !new_header->ctl_table->child->child->de) {
181 		printk
182 		    ("i2c-proc.o: NULL pointer when trying to install fill_inode fix!\n");
183 		return id;
184 	}
185 #endif				/* DEBUG */
186 	i2c_inodes[id - 256] =
187 	    new_header->ctl_table->child->child->de->low_ino;
188 	new_header->ctl_table->child->child->de->owner = controlling_mod;
189 
190 	return id;
191 }
192 
i2c_deregister_entry(int id)193 void i2c_deregister_entry(int id)
194 {
195 	ctl_table *table;
196 	char *temp;
197 	id -= 256;
198 	if (i2c_entries[id]) {
199 		table = i2c_entries[id]->ctl_table;
200 		unregister_sysctl_table(i2c_entries[id]);
201 		/* 2-step kfree needed to keep gcc happy about const points */
202 		temp = (char *) table[4].procname;
203 		kfree(temp);
204 		kfree(table);
205 		i2c_entries[id] = NULL;
206 		i2c_clients[id] = NULL;
207 	}
208 }
209 
210 /* Monitor access for /proc/sys/dev/sensors; make unloading i2c-proc.o
211    impossible if some process still uses it or some file in it */
i2c_fill_inode(struct inode * inode,int fill)212 void i2c_fill_inode(struct inode *inode, int fill)
213 {
214 	if (fill)
215 		MOD_INC_USE_COUNT;
216 	else
217 		MOD_DEC_USE_COUNT;
218 }
219 
220 /* Monitor access for /proc/sys/dev/sensors/ directories; make unloading
221    the corresponding module impossible if some process still uses it or
222    some file in it */
i2c_dir_fill_inode(struct inode * inode,int fill)223 void i2c_dir_fill_inode(struct inode *inode, int fill)
224 {
225 	int i;
226 	struct i2c_client *client;
227 
228 #ifdef DEBUG
229 	if (!inode) {
230 		printk("i2c-proc.o: Warning: inode NULL in fill_inode()\n");
231 		return;
232 	}
233 #endif				/* def DEBUG */
234 
235 	for (i = 0; i < SENSORS_ENTRY_MAX; i++)
236 		if (i2c_clients[i]
237 		    && (i2c_inodes[i] == inode->i_ino)) break;
238 #ifdef DEBUG
239 	if (i == SENSORS_ENTRY_MAX) {
240 		printk
241 		    ("i2c-proc.o: Warning: inode (%ld) not found in fill_inode()\n",
242 		     inode->i_ino);
243 		return;
244 	}
245 #endif				/* def DEBUG */
246 	client = i2c_clients[i];
247 	if (fill)
248 		client->driver->inc_use(client);
249 	else
250 		client->driver->dec_use(client);
251 }
252 
i2c_proc_chips(ctl_table * ctl,int write,struct file * filp,void * buffer,size_t * lenp)253 int i2c_proc_chips(ctl_table * ctl, int write, struct file *filp,
254 		       void *buffer, size_t * lenp)
255 {
256 	char BUF[SENSORS_PREFIX_MAX + 30];
257 	int buflen, curbufsize, i;
258 	struct ctl_table *client_tbl;
259 
260 	if (write)
261 		return 0;
262 
263 	/* If buffer is size 0, or we try to read when not at the start, we
264 	   return nothing. Note that I think writing when not at the start
265 	   does not work either, but anyway, this is straight from the kernel
266 	   sources. */
267 	if (!*lenp || (filp->f_pos && !write)) {
268 		*lenp = 0;
269 		return 0;
270 	}
271 	curbufsize = 0;
272 	for (i = 0; i < SENSORS_ENTRY_MAX; i++)
273 		if (i2c_entries[i]) {
274 			client_tbl =
275 			    i2c_entries[i]->ctl_table->child->child;
276 			buflen =
277 			    sprintf(BUF, "%d\t%s\n", client_tbl->ctl_name,
278 				    client_tbl->procname);
279 			if (buflen + curbufsize > *lenp)
280 				buflen = *lenp - curbufsize;
281 			if(copy_to_user(buffer, BUF, buflen))
282 				return -EFAULT;
283 			curbufsize += buflen;
284 			buffer = (char *) buffer + buflen;
285 		}
286 	*lenp = curbufsize;
287 	filp->f_pos += curbufsize;
288 	return 0;
289 }
290 
i2c_sysctl_chips(ctl_table * table,int * name,int nlen,void * oldval,size_t * oldlenp,void * newval,size_t newlen,void ** context)291 int i2c_sysctl_chips(ctl_table * table, int *name, int nlen,
292 			 void *oldval, size_t * oldlenp, void *newval,
293 			 size_t newlen, void **context)
294 {
295 	struct i2c_chips_data data;
296 	int i, oldlen, nrels, maxels,ret=0;
297 	struct ctl_table *client_tbl;
298 
299 	if (oldval && oldlenp && !((ret = get_user(oldlen, oldlenp))) &&
300 	    oldlen) {
301 		maxels = oldlen / sizeof(struct i2c_chips_data);
302 		nrels = 0;
303 		for (i = 0; (i < SENSORS_ENTRY_MAX) && (nrels < maxels);
304 		     i++)
305 			if (i2c_entries[i]) {
306 				client_tbl =
307 				    i2c_entries[i]->ctl_table->child->
308 				    child;
309 				data.sysctl_id = client_tbl->ctl_name;
310 				strcpy(data.name, client_tbl->procname);
311 				if(copy_to_user(oldval, &data,
312 					     sizeof(struct
313 						    i2c_chips_data)))
314 					return -EFAULT;
315 				oldval = (char *) oldval +
316 				    sizeof(struct i2c_chips_data);
317 				nrels++;
318 			}
319 		oldlen = nrels * sizeof(struct i2c_chips_data);
320 		if(put_user(oldlen, oldlenp))
321 			return -EFAULT;
322 	}
323 	return ret;
324 }
325 
326 
327 /* This funcion reads or writes a 'real' value (encoded by the combination
328    of an integer and a magnitude, the last is the power of ten the value
329    should be divided with) to a /proc/sys directory. To use this function,
330    you must (before registering the ctl_table) set the extra2 field to the
331    client, and the extra1 field to a function of the form:
332       void func(struct i2c_client *client, int operation, int ctl_name,
333                 int *nrels_mag, long *results)
334    This function can be called for three values of operation. If operation
335    equals SENSORS_PROC_REAL_INFO, the magnitude should be returned in
336    nrels_mag. If operation equals SENSORS_PROC_REAL_READ, values should
337    be read into results. nrels_mag should return the number of elements
338    read; the maximum number is put in it on entry. Finally, if operation
339    equals SENSORS_PROC_REAL_WRITE, the values in results should be
340    written to the chip. nrels_mag contains on entry the number of elements
341    found.
342    In all cases, client points to the client we wish to interact with,
343    and ctl_name is the SYSCTL id of the file we are accessing. */
i2c_proc_real(ctl_table * ctl,int write,struct file * filp,void * buffer,size_t * lenp)344 int i2c_proc_real(ctl_table * ctl, int write, struct file *filp,
345 		      void *buffer, size_t * lenp)
346 {
347 #define MAX_RESULTS 32
348 	int mag, nrels = MAX_RESULTS;
349 	long results[MAX_RESULTS];
350 	i2c_real_callback callback = ctl->extra1;
351 	struct i2c_client *client = ctl->extra2;
352 	int res;
353 
354 	/* If buffer is size 0, or we try to read when not at the start, we
355 	   return nothing. Note that I think writing when not at the start
356 	   does not work either, but anyway, this is straight from the kernel
357 	   sources. */
358 	if (!*lenp || (filp->f_pos && !write)) {
359 		*lenp = 0;
360 		return 0;
361 	}
362 
363 	/* Get the magnitude */
364 	callback(client, SENSORS_PROC_REAL_INFO, ctl->ctl_name, &mag,
365 		 NULL);
366 
367 	if (write) {
368 		/* Read the complete input into results, converting to longs */
369 		res = i2c_parse_reals(&nrels, buffer, *lenp, results, mag);
370 		if (res)
371 			return res;
372 
373 		if (!nrels)
374 			return 0;
375 
376 		/* Now feed this information back to the client */
377 		callback(client, SENSORS_PROC_REAL_WRITE, ctl->ctl_name,
378 			 &nrels, results);
379 
380 		filp->f_pos += *lenp;
381 		return 0;
382 	} else {		/* read */
383 		/* Get the information from the client into results */
384 		callback(client, SENSORS_PROC_REAL_READ, ctl->ctl_name,
385 			 &nrels, results);
386 
387 		/* And write them to buffer, converting to reals */
388 		res = i2c_write_reals(nrels, buffer, lenp, results, mag);
389 		if (res)
390 			return res;
391 		filp->f_pos += *lenp;
392 		return 0;
393 	}
394 }
395 
396 /* This function is equivalent to i2c_proc_real, only it interacts with
397    the sysctl(2) syscall, and returns no reals, but integers */
i2c_sysctl_real(ctl_table * table,int * name,int nlen,void * oldval,size_t * oldlenp,void * newval,size_t newlen,void ** context)398 int i2c_sysctl_real(ctl_table * table, int *name, int nlen,
399 			void *oldval, size_t * oldlenp, void *newval,
400 			size_t newlen, void **context)
401 {
402 	long results[MAX_RESULTS];
403 	int oldlen, nrels = MAX_RESULTS,ret=0;
404 	i2c_real_callback callback = table->extra1;
405 	struct i2c_client *client = table->extra2;
406 
407 	/* Check if we need to output the old values */
408 	if (oldval && oldlenp && !((ret=get_user(oldlen, oldlenp))) && oldlen) {
409 		callback(client, SENSORS_PROC_REAL_READ, table->ctl_name,
410 			 &nrels, results);
411 
412 		/* Note the rounding factor! */
413 		if (nrels * sizeof(long) < oldlen)
414 			oldlen = nrels * sizeof(long);
415 		oldlen = (oldlen / sizeof(long)) * sizeof(long);
416 		if(copy_to_user(oldval, results, oldlen))
417 			return -EFAULT;
418 		if(put_user(oldlen, oldlenp))
419 			return -EFAULT;
420 	}
421 
422 	if (newval && newlen) {
423 		/* Note the rounding factor! */
424 		newlen -= newlen % sizeof(long);
425 		nrels = newlen / sizeof(long);
426 		if(copy_from_user(results, newval, newlen))
427 			return -EFAULT;
428 
429 		/* Get the new values back to the client */
430 		callback(client, SENSORS_PROC_REAL_WRITE, table->ctl_name,
431 			 &nrels, results);
432 	}
433 	return ret;
434 }
435 
436 
437 /* nrels contains initially the maximum number of elements which can be
438    put in results, and finally the number of elements actually put there.
439    A magnitude of 1 will multiply everything with 10; etc.
440    buffer, bufsize is the character buffer we read from and its length.
441    results will finally contain the parsed integers.
442 
443    Buffer should contain several reals, separated by whitespace. A real
444    has the following syntax:
445      [ Minus ] Digit* [ Dot Digit* ]
446    (everything between [] is optional; * means zero or more).
447    When the next character is unparsable, everything is skipped until the
448    next whitespace.
449 
450    WARNING! This is tricky code. I have tested it, but there may still be
451             hidden bugs in it, even leading to crashes and things!
452 */
i2c_parse_reals(int * nrels,char * buffer,int bufsize,long * results,int magnitude)453 static int i2c_parse_reals(int *nrels, char *buffer, int bufsize,
454 			 long *results, int magnitude)
455 {
456 	int maxels, min, mag;
457 	long res,ret=0;
458 	char nextchar = 0;
459 
460 	maxels = *nrels;
461 	*nrels = 0;
462 
463 	while (bufsize && (*nrels < maxels)) {
464 
465 		/* Skip spaces at the start */
466 		while (bufsize &&
467 		       !((ret=get_user(nextchar, buffer))) &&
468 		       isspace((int) nextchar)) {
469 			bufsize--;
470 			buffer++;
471 		}
472 
473 		if (ret)
474 			return -EFAULT;
475 		/* Well, we may be done now */
476 		if (!bufsize)
477 			return 0;
478 
479 		/* New defaults for our result */
480 		min = 0;
481 		res = 0;
482 		mag = magnitude;
483 
484 		/* Check for a minus */
485 		if (!((ret=get_user(nextchar, buffer)))
486 		    && (nextchar == '-')) {
487 			min = 1;
488 			bufsize--;
489 			buffer++;
490 		}
491 		if (ret)
492 			return -EFAULT;
493 
494 		/* Digits before a decimal dot */
495 		while (bufsize &&
496 		       !((ret=get_user(nextchar, buffer))) &&
497 		       isdigit((int) nextchar)) {
498 			res = res * 10 + nextchar - '0';
499 			bufsize--;
500 			buffer++;
501 		}
502 		if (ret)
503 			return -EFAULT;
504 
505 		/* If mag < 0, we must actually divide here! */
506 		while (mag < 0) {
507 			res = res / 10;
508 			mag++;
509 		}
510 
511 		if (bufsize && (nextchar == '.')) {
512 			/* Skip the dot */
513 			bufsize--;
514 			buffer++;
515 
516 			/* Read digits while they are significant */
517 			while (bufsize && (mag > 0) &&
518 			       !((ret=get_user(nextchar, buffer))) &&
519 			       isdigit((int) nextchar)) {
520 				res = res * 10 + nextchar - '0';
521 				mag--;
522 				bufsize--;
523 				buffer++;
524 			}
525 			if (ret)
526 				return -EFAULT;
527 		}
528 		/* If we are out of data, but mag > 0, we need to scale here */
529 		while (mag > 0) {
530 			res = res * 10;
531 			mag--;
532 		}
533 
534 		/* Skip everything until we hit whitespace */
535 		while (bufsize &&
536 		       !((ret=get_user(nextchar, buffer))) &&
537 		       !isspace((int) nextchar)) {
538 			bufsize--;
539 			buffer++;
540 		}
541 		if (ret)
542 			return -EFAULT;
543 
544 		/* Put res in results */
545 		results[*nrels] = (min ? -1 : 1) * res;
546 		(*nrels)++;
547 	}
548 
549 	/* Well, there may be more in the buffer, but we need no more data.
550 	   Ignore anything that is left. */
551 	return 0;
552 }
553 
i2c_write_reals(int nrels,char * buffer,size_t * bufsize,long * results,int magnitude)554 static int i2c_write_reals(int nrels, char *buffer, size_t *bufsize,
555 			 long *results, int magnitude)
556 {
557 #define BUFLEN 20
558 	char BUF[BUFLEN + 1];	/* An individual representation should fit! */
559 	char printfstr[10];
560 	int nr = 0;
561 	int buflen, mag, times;
562 	int curbufsize = 0;
563 
564 	while ((nr < nrels) && (curbufsize < *bufsize)) {
565 		mag = magnitude;
566 
567 		if (nr != 0) {
568 			if(put_user(' ', buffer))
569 				return -EFAULT;
570 			curbufsize++;
571 			buffer++;
572 		}
573 
574 		/* Fill BUF with the representation of the next string */
575 		if (mag <= 0) {
576 			buflen = sprintf(BUF, "%ld", results[nr]);
577 			if (buflen < 0) {	/* Oops, a sprintf error! */
578 				*bufsize = 0;
579 				return -EINVAL;
580 			}
581 			while ((mag < 0) && (buflen < BUFLEN)) {
582 				BUF[buflen++] = '0';
583 				mag++;
584 			}
585 			BUF[buflen] = 0;
586 		} else {
587 			times = 1;
588 			for (times = 1; mag-- > 0; times *= 10);
589 			if (results[nr] < 0) {
590 				BUF[0] = '-';
591 				buflen = 1;
592 			} else
593 				buflen = 0;
594 			strcpy(printfstr, "%ld.%0Xld");
595 			printfstr[6] = magnitude + '0';
596 			buflen +=
597 			    sprintf(BUF + buflen, printfstr,
598 				    abs(results[nr]) / times,
599 				    abs(results[nr]) % times);
600 			if (buflen < 0) {	/* Oops, a sprintf error! */
601 				*bufsize = 0;
602 				return -EINVAL;
603 			}
604 		}
605 
606 		/* Now copy it to the user-space buffer */
607 		if (buflen + curbufsize > *bufsize)
608 			buflen = *bufsize - curbufsize;
609 		if(copy_to_user(buffer, BUF, buflen))
610 			return -EFAULT;
611 		curbufsize += buflen;
612 		buffer += buflen;
613 
614 		nr++;
615 	}
616 	if (curbufsize < *bufsize) {
617 		if(put_user('\n', buffer))
618 			return -EFAULT;
619 		curbufsize++;
620 	}
621 	*bufsize = curbufsize;
622 	return 0;
623 }
624 
625 
626 /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
i2c_detect(struct i2c_adapter * adapter,struct i2c_address_data * address_data,i2c_found_addr_proc * found_proc)627 int i2c_detect(struct i2c_adapter *adapter,
628 		   struct i2c_address_data *address_data,
629 		   i2c_found_addr_proc * found_proc)
630 {
631 	int addr, i, found, j, err;
632 	struct i2c_force_data *this_force;
633 	int is_isa = i2c_is_isa_adapter(adapter);
634 	int adapter_id =
635 	    is_isa ? SENSORS_ISA_BUS : i2c_adapter_id(adapter);
636 
637 	/* Forget it if we can't probe using SMBUS_QUICK */
638 	if ((!is_isa)
639 	    && !i2c_check_functionality(adapter,
640 					I2C_FUNC_SMBUS_QUICK)) return -1;
641 
642 	for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
643 		if ((is_isa && check_region(addr, 1)) ||
644 		    (!is_isa && i2c_check_addr(adapter, addr)))
645 			continue;
646 
647 		/* If it is in one of the force entries, we don't do any
648 		   detection at all */
649 		found = 0;
650 		for (i = 0;
651 		     !found
652 		     && (this_force =
653 			 address_data->forces + i, this_force->force); i++) {
654 			for (j = 0;
655 			     !found
656 			     && (this_force->force[j] != SENSORS_I2C_END);
657 			     j += 2) {
658 				if (
659 				    ((adapter_id == this_force->force[j])
660 				     ||
661 				     ((this_force->
662 				       force[j] == SENSORS_ANY_I2C_BUS)
663 				      && !is_isa))
664 				    && (addr == this_force->force[j + 1])) {
665 #ifdef DEBUG
666 					printk
667 					    (KERN_DEBUG "i2c-proc.o: found force parameter for adapter %d, addr %04x\n",
668 					     adapter_id, addr);
669 #endif
670 					if (
671 					    (err =
672 					     found_proc(adapter, addr, 0,
673 							this_force->
674 							kind))) return err;
675 					found = 1;
676 				}
677 			}
678 		}
679 		if (found)
680 			continue;
681 
682 		/* If this address is in one of the ignores, we can forget about it
683 		   right now */
684 		for (i = 0;
685 		     !found
686 		     && (address_data->ignore[i] != SENSORS_I2C_END);
687 		     i += 2) {
688 			if (
689 			    ((adapter_id == address_data->ignore[i])
690 			     ||
691 			     ((address_data->
692 			       ignore[i] == SENSORS_ANY_I2C_BUS)
693 			      && !is_isa))
694 			    && (addr == address_data->ignore[i + 1])) {
695 #ifdef DEBUG
696 				printk
697 				    (KERN_DEBUG "i2c-proc.o: found ignore parameter for adapter %d, "
698 				     "addr %04x\n", adapter_id, addr);
699 #endif
700 				found = 1;
701 			}
702 		}
703 		for (i = 0;
704 		     !found
705 		     && (address_data->ignore_range[i] != SENSORS_I2C_END);
706 		     i += 3) {
707 			if (
708 			    ((adapter_id == address_data->ignore_range[i])
709 			     ||
710 			     ((address_data->
711 			       ignore_range[i] ==
712 			       SENSORS_ANY_I2C_BUS) && !is_isa))
713 			    && (addr >= address_data->ignore_range[i + 1])
714 			    && (addr <= address_data->ignore_range[i + 2])) {
715 #ifdef DEBUG
716 				printk
717 				    (KERN_DEBUG "i2c-proc.o: found ignore_range parameter for adapter %d, "
718 				     "addr %04x\n", adapter_id, addr);
719 #endif
720 				found = 1;
721 			}
722 		}
723 		if (found)
724 			continue;
725 
726 		/* Now, we will do a detection, but only if it is in the normal or
727 		   probe entries */
728 		if (is_isa) {
729 			for (i = 0;
730 			     !found
731 			     && (address_data->normal_isa[i] !=
732 				 SENSORS_ISA_END); i += 1) {
733 				if (addr == address_data->normal_isa[i]) {
734 #ifdef DEBUG
735 					printk
736 					    (KERN_DEBUG "i2c-proc.o: found normal isa entry for adapter %d, "
737 					     "addr %04x\n", adapter_id,
738 					     addr);
739 #endif
740 					found = 1;
741 				}
742 			}
743 			for (i = 0;
744 			     !found
745 			     && (address_data->normal_isa_range[i] !=
746 				 SENSORS_ISA_END); i += 3) {
747 				if ((addr >=
748 				     address_data->normal_isa_range[i])
749 				    && (addr <=
750 					address_data->normal_isa_range[i + 1])
751 				    &&
752 				    ((addr -
753 				      address_data->normal_isa_range[i]) %
754 				     address_data->normal_isa_range[i + 2] ==
755 				     0)) {
756 #ifdef DEBUG
757 					printk
758 					    (KERN_DEBUG "i2c-proc.o: found normal isa_range entry for adapter %d, "
759 					     "addr %04x\n", adapter_id, addr);
760 #endif
761 					found = 1;
762 				}
763 			}
764 		} else {
765 			for (i = 0;
766 			     !found && (address_data->normal_i2c[i] !=
767 				 SENSORS_I2C_END); i += 1) {
768 				if (addr == address_data->normal_i2c[i]) {
769 					found = 1;
770 #ifdef DEBUG
771 					printk
772 					    (KERN_DEBUG "i2c-proc.o: found normal i2c entry for adapter %d, "
773 					     "addr %02x\n", adapter_id, addr);
774 #endif
775 				}
776 			}
777 			for (i = 0;
778 			     !found
779 			     && (address_data->normal_i2c_range[i] !=
780 				 SENSORS_I2C_END); i += 2) {
781 				if ((addr >=
782 				     address_data->normal_i2c_range[i])
783 				    && (addr <=
784 					address_data->normal_i2c_range[i + 1]))
785 				{
786 #ifdef DEBUG
787 					printk
788 					    (KERN_DEBUG "i2c-proc.o: found normal i2c_range entry for adapter %d, "
789 					     "addr %04x\n", adapter_id, addr);
790 #endif
791 					found = 1;
792 				}
793 			}
794 		}
795 
796 		for (i = 0;
797 		     !found && (address_data->probe[i] != SENSORS_I2C_END);
798 		     i += 2) {
799 			if (((adapter_id == address_data->probe[i]) ||
800 			     ((address_data->
801 			       probe[i] == SENSORS_ANY_I2C_BUS) && !is_isa))
802 			    && (addr == address_data->probe[i + 1])) {
803 #ifdef DEBUG
804 				printk
805 				    (KERN_DEBUG "i2c-proc.o: found probe parameter for adapter %d, "
806 				     "addr %04x\n", adapter_id, addr);
807 #endif
808 				found = 1;
809 			}
810 		}
811 		for (i = 0; !found &&
812 		           (address_data->probe_range[i] != SENSORS_I2C_END);
813 		     i += 3) {
814 			if (
815 			    ((adapter_id == address_data->probe_range[i])
816 			     ||
817 			     ((address_data->probe_range[i] ==
818 			       SENSORS_ANY_I2C_BUS) && !is_isa))
819 			    && (addr >= address_data->probe_range[i + 1])
820 			    && (addr <= address_data->probe_range[i + 2])) {
821 				found = 1;
822 #ifdef DEBUG
823 				printk
824 				    (KERN_DEBUG "i2c-proc.o: found probe_range parameter for adapter %d, "
825 				     "addr %04x\n", adapter_id, addr);
826 #endif
827 			}
828 		}
829 		if (!found)
830 			continue;
831 
832 		/* OK, so we really should examine this address. First check
833 		   whether there is some client here at all! */
834 		if (is_isa ||
835 		    (i2c_smbus_xfer
836 		     (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
837 			if ((err = found_proc(adapter, addr, 0, -1)))
838 				return err;
839 	}
840 	return 0;
841 }
842 
sensors_init(void)843 int __init sensors_init(void)
844 {
845 	printk(KERN_INFO "i2c-proc.o version %s (%s)\n", I2C_VERSION, I2C_DATE);
846 	i2c_initialized = 0;
847 	if (!
848 	    (i2c_proc_header =
849 	     register_sysctl_table(i2c_proc, 0))) return -ENOMEM;
850 	i2c_proc_header->ctl_table->child->de->owner = THIS_MODULE;
851 	i2c_initialized++;
852 	return 0;
853 }
854 
855 EXPORT_SYMBOL(i2c_deregister_entry);
856 EXPORT_SYMBOL(i2c_detect);
857 EXPORT_SYMBOL(i2c_proc_real);
858 EXPORT_SYMBOL(i2c_register_entry);
859 EXPORT_SYMBOL(i2c_sysctl_real);
860 
861 #ifdef MODULE
862 
863 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
864 MODULE_DESCRIPTION("i2c-proc driver");
865 MODULE_LICENSE("GPL");
866 
i2c_cleanup(void)867 int i2c_cleanup(void)
868 {
869 	if (i2c_initialized >= 1) {
870 		unregister_sysctl_table(i2c_proc_header);
871 		i2c_initialized--;
872 	}
873 	return 0;
874 }
875 
init_module(void)876 int init_module(void)
877 {
878 	return sensors_init();
879 }
880 
cleanup_module(void)881 int cleanup_module(void)
882 {
883 	return i2c_cleanup();
884 }
885 #endif				/* MODULE */
886