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