This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / i2c / i2c-sensor-detect.c
1 /*
2     i2c-sensor-detect.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 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
27 #include <linux/sysctl.h>
28 #include <linux/init.h>
29 #include <linux/ioport.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-sensor.h>
32 #include <asm/uaccess.h>
33
34
35 /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
36 int i2c_detect(struct i2c_adapter *adapter,
37                struct i2c_address_data *address_data,
38                int (*found_proc) (struct i2c_adapter *, int, int))
39 {
40         int addr, i, found, j, err;
41         struct i2c_force_data *this_force;
42         int is_isa = i2c_is_isa_adapter(adapter);
43         int adapter_id =
44             is_isa ? ANY_I2C_ISA_BUS : i2c_adapter_id(adapter);
45
46         /* Forget it if we can't probe using SMBUS_QUICK */
47         if ((!is_isa) &&
48             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK))
49                 return -1;
50
51         for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
52                 if (!is_isa && i2c_check_addr(adapter, addr))
53                         continue;
54
55                 /* If it is in one of the force entries, we don't do any
56                    detection at all */
57                 found = 0;
58                 for (i = 0; !found && (this_force = address_data->forces + i, this_force->force); i++) {
59                         for (j = 0; !found && (this_force->force[j] != I2C_CLIENT_END); j += 2) {
60                                 if ( ((adapter_id == this_force->force[j]) ||
61                                       ((this_force->force[j] == ANY_I2C_BUS) && !is_isa)) &&
62                                       (addr == this_force->force[j + 1]) ) {
63                                         dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n", adapter_id, addr);
64                                         if ((err = found_proc(adapter, addr, this_force->kind)))
65                                                 return err;
66                                         found = 1;
67                                 }
68                         }
69                 }
70                 if (found)
71                         continue;
72
73                 /* If this address is in one of the ignores, we can forget about it
74                    right now */
75                 for (i = 0; !found && (address_data->ignore[i] != I2C_CLIENT_END); i += 2) {
76                         if ( ((adapter_id == address_data->ignore[i]) ||
77                               ((address_data->ignore[i] == ANY_I2C_BUS) &&
78                                !is_isa)) &&
79                               (addr == address_data->ignore[i + 1])) {
80                                 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, addr %04x\n", adapter_id, addr);
81                                 found = 1;
82                         }
83                 }
84                 for (i = 0; !found && (address_data->ignore_range[i] != I2C_CLIENT_END); i += 3) {
85                         if ( ((adapter_id == address_data->ignore_range[i]) ||
86                               ((address_data-> ignore_range[i] == ANY_I2C_BUS) & 
87                                !is_isa)) &&
88                              (addr >= address_data->ignore_range[i + 1]) &&
89                              (addr <= address_data->ignore_range[i + 2])) {
90                                 dev_dbg(&adapter->dev,  "found ignore_range parameter for adapter %d, addr %04x\n", adapter_id, addr);
91                                 found = 1;
92                         }
93                 }
94                 if (found)
95                         continue;
96
97                 /* Now, we will do a detection, but only if it is in the normal or 
98                    probe entries */
99                 if (is_isa) {
100                         for (i = 0; !found && (address_data->normal_isa[i] != I2C_CLIENT_ISA_END); i += 1) {
101                                 if (addr == address_data->normal_isa[i]) {
102                                         dev_dbg(&adapter->dev, "found normal isa entry for adapter %d, addr %04x\n", adapter_id, addr);
103                                         found = 1;
104                                 }
105                         }
106                         for (i = 0; !found && (address_data->normal_isa_range[i] != I2C_CLIENT_ISA_END); i += 3) {
107                                 if ((addr >= address_data->normal_isa_range[i]) &&
108                                     (addr <= address_data->normal_isa_range[i + 1]) &&
109                                     ((addr - address_data->normal_isa_range[i]) % address_data->normal_isa_range[i + 2] == 0)) {
110                                         dev_dbg(&adapter->dev, "found normal isa_range entry for adapter %d, addr %04x", adapter_id, addr);
111                                         found = 1;
112                                 }
113                         }
114                 } else {
115                         for (i = 0; !found && (address_data->normal_i2c[i] != I2C_CLIENT_END); i += 1) {
116                                 if (addr == address_data->normal_i2c[i]) {
117                                         found = 1;
118                                         dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, addr %02x", adapter_id, addr);
119                                 }
120                         }
121                         for (i = 0; !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END); i += 2) {
122                                 if ((addr >= address_data->normal_i2c_range[i]) &&
123                                     (addr <= address_data->normal_i2c_range[i + 1])) {
124                                         dev_dbg(&adapter->dev, "found normal i2c_range entry for adapter %d, addr %04x\n", adapter_id, addr);
125                                         found = 1;
126                                 }
127                         }
128                 }
129
130                 for (i = 0;
131                      !found && (address_data->probe[i] != I2C_CLIENT_END);
132                      i += 2) {
133                         if (((adapter_id == address_data->probe[i]) ||
134                              ((address_data->
135                                probe[i] == ANY_I2C_BUS) && !is_isa))
136                             && (addr == address_data->probe[i + 1])) {
137                                 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, addr %04x\n", adapter_id, addr);
138                                 found = 1;
139                         }
140                 }
141                 for (i = 0; !found && (address_data->probe_range[i] != I2C_CLIENT_END); i += 3) {
142                         if ( ((adapter_id == address_data->probe_range[i]) ||
143                               ((address_data->probe_range[i] == ANY_I2C_BUS) && !is_isa)) &&
144                              (addr >= address_data->probe_range[i + 1]) &&
145                              (addr <= address_data->probe_range[i + 2])) {
146                                 found = 1;
147                                 dev_dbg(&adapter->dev, "found probe_range parameter for adapter %d, addr %04x\n", adapter_id, addr);
148                         }
149                 }
150                 if (!found)
151                         continue;
152
153                 /* OK, so we really should examine this address. First check
154                    whether there is some client here at all! */
155                 if (is_isa ||
156                     (i2c_smbus_xfer (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
157                         if ((err = found_proc(adapter, addr, -1)))
158                                 return err;
159         }
160         return 0;
161 }
162
163 EXPORT_SYMBOL(i2c_detect);
164
165 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
166               "Rudolf Marek <r.marek@sh.cvut.cz>");
167
168 MODULE_DESCRIPTION("i2c-sensor driver");
169 MODULE_LICENSE("GPL");