| 1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
|---|
| 2 | /* ------------------------------------------------------------------------- */ |
|---|
| 3 | /* Copyright (C) 1995-99 Simon G. Vogl |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|---|
| 18 | /* ------------------------------------------------------------------------- */ |
|---|
| 19 | |
|---|
| 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
|---|
| 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
|---|
| 22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */ |
|---|
| 23 | |
|---|
| 24 | /* $Id$ */ |
|---|
| 25 | |
|---|
| 26 | #include <linux/module.h> |
|---|
| 27 | #include <linux/kernel.h> |
|---|
| 28 | #include <linux/errno.h> |
|---|
| 29 | #include <linux/slab.h> |
|---|
| 30 | #include <linux/proc_fs.h> |
|---|
| 31 | #include <linux/config.h> |
|---|
| 32 | #include "i2c.h" |
|---|
| 33 | |
|---|
| 34 | /* ----- compatibility stuff ----------------------------------------------- */ |
|---|
| 35 | |
|---|
| 36 | #include <linux/version.h> |
|---|
| 37 | #include <linux/init.h> |
|---|
| 38 | |
|---|
| 39 | #include <asm/uaccess.h> |
|---|
| 40 | |
|---|
| 41 | /* ----- global defines ---------------------------------------------------- */ |
|---|
| 42 | |
|---|
| 43 | /* exclusive access to the bus */ |
|---|
| 44 | #define I2C_LOCK(adap) down(&adap->lock) |
|---|
| 45 | #define I2C_UNLOCK(adap) up(&adap->lock) |
|---|
| 46 | |
|---|
| 47 | #define ADAP_LOCK() down(&adap_lock) |
|---|
| 48 | #define ADAP_UNLOCK() up(&adap_lock) |
|---|
| 49 | |
|---|
| 50 | #define DRV_LOCK() down(&driver_lock) |
|---|
| 51 | #define DRV_UNLOCK() up(&driver_lock) |
|---|
| 52 | |
|---|
| 53 | #define DEB(x) if (i2c_debug>=1) x; |
|---|
| 54 | #define DEB2(x) if (i2c_debug>=2) x; |
|---|
| 55 | |
|---|
| 56 | /* ----- global variables -------------------------------------------------- */ |
|---|
| 57 | |
|---|
| 58 | /**** lock for writing to global variables: the adapter & driver list */ |
|---|
| 59 | DECLARE_MUTEX(adap_lock); |
|---|
| 60 | DECLARE_MUTEX(driver_lock); |
|---|
| 61 | |
|---|
| 62 | /**** adapter list */ |
|---|
| 63 | static struct i2c_adapter *adapters[I2C_ADAP_MAX]; |
|---|
| 64 | static int adap_count; |
|---|
| 65 | |
|---|
| 66 | /**** drivers list */ |
|---|
| 67 | static struct i2c_driver *drivers[I2C_DRIVER_MAX]; |
|---|
| 68 | static int driver_count; |
|---|
| 69 | |
|---|
| 70 | /**** debug level */ |
|---|
| 71 | static int i2c_debug; |
|---|
| 72 | |
|---|
| 73 | /* --------------------------------------------------- |
|---|
| 74 | * /proc entry declarations |
|---|
| 75 | *---------------------------------------------------- |
|---|
| 76 | */ |
|---|
| 77 | |
|---|
| 78 | #ifdef CONFIG_PROC_FS |
|---|
| 79 | static ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count, |
|---|
| 80 | loff_t *ppos); |
|---|
| 81 | static int read_bus_i2c(char *buf, char **start, off_t offset, int len, |
|---|
| 82 | int *eof , void *private); |
|---|
| 83 | |
|---|
| 84 | static int i2cproc_register(struct i2c_adapter *adap, int bus); |
|---|
| 85 | static void i2cproc_remove(int bus); |
|---|
| 86 | /* To implement the dynamic /proc/bus/i2c-? files, we need our own |
|---|
| 87 | implementation of the read hook */ |
|---|
| 88 | static struct file_operations i2cproc_operations = { |
|---|
| 89 | .read = i2cproc_bus_read, |
|---|
| 90 | }; |
|---|
| 91 | #endif /* CONFIG_PROC_FS */ |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /* --------------------------------------------------- |
|---|
| 95 | * registering functions |
|---|
| 96 | * --------------------------------------------------- |
|---|
| 97 | */ |
|---|
| 98 | |
|---|
| 99 | /* ----- |
|---|
| 100 | * i2c_add_adapter is called from within the algorithm layer, |
|---|
| 101 | * when a new hw adapter registers. A new device is register to be |
|---|
| 102 | * available for clients. |
|---|
| 103 | */ |
|---|
| 104 | int i2c_add_adapter(struct i2c_adapter *adap) |
|---|
| 105 | { |
|---|
| 106 | int i,j,res; |
|---|
| 107 | |
|---|
| 108 | ADAP_LOCK(); |
|---|
| 109 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 110 | if (NULL == adapters[i]) |
|---|
| 111 | break; |
|---|
| 112 | if (I2C_ADAP_MAX == i) { |
|---|
| 113 | printk(KERN_WARNING |
|---|
| 114 | " i2c-core.o: register_adapter(%s) - enlarge I2C_ADAP_MAX.\n", |
|---|
| 115 | adap->name); |
|---|
| 116 | res = -ENOMEM; |
|---|
| 117 | goto ERROR0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | adapters[i] = adap; |
|---|
| 121 | adap_count++; |
|---|
| 122 | ADAP_UNLOCK(); |
|---|
| 123 | |
|---|
| 124 | /* init data types */ |
|---|
| 125 | init_MUTEX(&adap->lock); |
|---|
| 126 | |
|---|
| 127 | #ifdef CONFIG_PROC_FS |
|---|
| 128 | res = i2cproc_register(adap, i); |
|---|
| 129 | if (res<0) |
|---|
| 130 | goto ERROR1; |
|---|
| 131 | #endif /* def CONFIG_PROC_FS */ |
|---|
| 132 | |
|---|
| 133 | /* inform drivers of new adapters */ |
|---|
| 134 | DRV_LOCK(); |
|---|
| 135 | for (j=0;j<I2C_DRIVER_MAX;j++) |
|---|
| 136 | if (drivers[j]!=NULL && |
|---|
| 137 | (drivers[j]->flags&(I2C_DF_NOTIFY|I2C_DF_DUMMY))) |
|---|
| 138 | /* We ignore the return code; if it fails, too bad */ |
|---|
| 139 | drivers[j]->attach_adapter(adap); |
|---|
| 140 | DRV_UNLOCK(); |
|---|
| 141 | |
|---|
| 142 | DEB(printk(KERN_DEBUG "i2c-core.o: adapter %s registered as adapter %d.\n", |
|---|
| 143 | adap->name,i)); |
|---|
| 144 | |
|---|
| 145 | return 0; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | ERROR1: |
|---|
| 149 | ADAP_LOCK(); |
|---|
| 150 | adapters[i] = NULL; |
|---|
| 151 | adap_count--; |
|---|
| 152 | ERROR0: |
|---|
| 153 | ADAP_UNLOCK(); |
|---|
| 154 | return res; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | int i2c_del_adapter(struct i2c_adapter *adap) |
|---|
| 159 | { |
|---|
| 160 | int i,j,res; |
|---|
| 161 | |
|---|
| 162 | ADAP_LOCK(); |
|---|
| 163 | |
|---|
| 164 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 165 | if (adap == adapters[i]) |
|---|
| 166 | break; |
|---|
| 167 | if (I2C_ADAP_MAX == i) { |
|---|
| 168 | printk( KERN_WARNING "i2c-core.o: unregister_adapter adap [%s] not found.\n", |
|---|
| 169 | adap->name); |
|---|
| 170 | res = -ENODEV; |
|---|
| 171 | goto ERROR0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /* DUMMY drivers do not register their clients, so we have to |
|---|
| 175 | * use a trick here: we call driver->attach_adapter to |
|---|
| 176 | * *detach* it! Of course, each dummy driver should know about |
|---|
| 177 | * this or hell will break loose... |
|---|
| 178 | */ |
|---|
| 179 | DRV_LOCK(); |
|---|
| 180 | for (j = 0; j < I2C_DRIVER_MAX; j++) |
|---|
| 181 | if (drivers[j] && (drivers[j]->flags & I2C_DF_DUMMY)) |
|---|
| 182 | if ((res = drivers[j]->attach_adapter(adap))) { |
|---|
| 183 | printk(KERN_WARNING "i2c-core.o: can't detach adapter %s " |
|---|
| 184 | "while detaching driver %s: driver not " |
|---|
| 185 | "detached!",adap->name,drivers[j]->name); |
|---|
| 186 | goto ERROR1; |
|---|
| 187 | } |
|---|
| 188 | DRV_UNLOCK(); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | /* detach any active clients. This must be done first, because |
|---|
| 192 | * it can fail; in which case we give upp. */ |
|---|
| 193 | for (j=0;j<I2C_CLIENT_MAX;j++) { |
|---|
| 194 | struct i2c_client *client = adap->clients[j]; |
|---|
| 195 | if (client!=NULL) |
|---|
| 196 | /* detaching devices is unconditional of the set notify |
|---|
| 197 | * flag, as _all_ clients that reside on the adapter |
|---|
| 198 | * must be deleted, as this would cause invalid states. |
|---|
| 199 | */ |
|---|
| 200 | if ((res=client->driver->detach_client(client))) { |
|---|
| 201 | printk(KERN_ERR "i2c-core.o: adapter %s not " |
|---|
| 202 | "unregistered, because client at " |
|---|
| 203 | "address %02x can't be detached. ", |
|---|
| 204 | adap->name, client->addr); |
|---|
| 205 | goto ERROR0; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | #ifdef CONFIG_PROC_FS |
|---|
| 209 | i2cproc_remove(i); |
|---|
| 210 | #endif /* def CONFIG_PROC_FS */ |
|---|
| 211 | |
|---|
| 212 | adapters[i] = NULL; |
|---|
| 213 | adap_count--; |
|---|
| 214 | |
|---|
| 215 | ADAP_UNLOCK(); |
|---|
| 216 | DEB(printk(KERN_DEBUG "i2c-core.o: adapter unregistered: %s\n",adap->name)); |
|---|
| 217 | return 0; |
|---|
| 218 | |
|---|
| 219 | ERROR0: |
|---|
| 220 | ADAP_UNLOCK(); |
|---|
| 221 | return res; |
|---|
| 222 | ERROR1: |
|---|
| 223 | DRV_UNLOCK(); |
|---|
| 224 | return res; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | /* ----- |
|---|
| 229 | * What follows is the "upwards" interface: commands for talking to clients, |
|---|
| 230 | * which implement the functions to access the physical information of the |
|---|
| 231 | * chips. |
|---|
| 232 | */ |
|---|
| 233 | |
|---|
| 234 | int i2c_add_driver(struct i2c_driver *driver) |
|---|
| 235 | { |
|---|
| 236 | int i; |
|---|
| 237 | DRV_LOCK(); |
|---|
| 238 | for (i = 0; i < I2C_DRIVER_MAX; i++) |
|---|
| 239 | if (NULL == drivers[i]) |
|---|
| 240 | break; |
|---|
| 241 | if (I2C_DRIVER_MAX == i) { |
|---|
| 242 | printk(KERN_WARNING |
|---|
| 243 | " i2c-core.o: register_driver(%s) " |
|---|
| 244 | "- enlarge I2C_DRIVER_MAX.\n", |
|---|
| 245 | driver->name); |
|---|
| 246 | DRV_UNLOCK(); |
|---|
| 247 | return -ENOMEM; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | drivers[i] = driver; |
|---|
| 251 | driver_count++; |
|---|
| 252 | |
|---|
| 253 | DRV_UNLOCK(); /* driver was successfully added */ |
|---|
| 254 | |
|---|
| 255 | DEB(printk(KERN_DEBUG "i2c-core.o: driver %s registered.\n",driver->name)); |
|---|
| 256 | |
|---|
| 257 | ADAP_LOCK(); |
|---|
| 258 | |
|---|
| 259 | /* now look for instances of driver on our adapters |
|---|
| 260 | */ |
|---|
| 261 | if (driver->flags& (I2C_DF_NOTIFY|I2C_DF_DUMMY)) { |
|---|
| 262 | for (i=0;i<I2C_ADAP_MAX;i++) |
|---|
| 263 | if (adapters[i]!=NULL) |
|---|
| 264 | /* Ignore errors */ |
|---|
| 265 | driver->attach_adapter(adapters[i]); |
|---|
| 266 | } |
|---|
| 267 | ADAP_UNLOCK(); |
|---|
| 268 | return 0; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | int i2c_del_driver(struct i2c_driver *driver) |
|---|
| 272 | { |
|---|
| 273 | int i,j,k,res; |
|---|
| 274 | |
|---|
| 275 | DRV_LOCK(); |
|---|
| 276 | for (i = 0; i < I2C_DRIVER_MAX; i++) |
|---|
| 277 | if (driver == drivers[i]) |
|---|
| 278 | break; |
|---|
| 279 | if (I2C_DRIVER_MAX == i) { |
|---|
| 280 | printk(KERN_WARNING " i2c-core.o: unregister_driver: " |
|---|
| 281 | "[%s] not found\n", |
|---|
| 282 | driver->name); |
|---|
| 283 | DRV_UNLOCK(); |
|---|
| 284 | return -ENODEV; |
|---|
| 285 | } |
|---|
| 286 | /* Have a look at each adapter, if clients of this driver are still |
|---|
| 287 | * attached. If so, detach them to be able to kill the driver |
|---|
| 288 | * afterwards. |
|---|
| 289 | */ |
|---|
| 290 | DEB2(printk(KERN_DEBUG "i2c-core.o: unregister_driver - looking for clients.\n")); |
|---|
| 291 | /* removing clients does not depend on the notify flag, else |
|---|
| 292 | * invalid operation might (will!) result, when using stale client |
|---|
| 293 | * pointers. |
|---|
| 294 | */ |
|---|
| 295 | ADAP_LOCK(); /* should be moved inside the if statement... */ |
|---|
| 296 | for (k=0;k<I2C_ADAP_MAX;k++) { |
|---|
| 297 | struct i2c_adapter *adap = adapters[k]; |
|---|
| 298 | if (adap == NULL) /* skip empty entries. */ |
|---|
| 299 | continue; |
|---|
| 300 | DEB2(printk(KERN_DEBUG "i2c-core.o: examining adapter %s:\n", |
|---|
| 301 | adap->name)); |
|---|
| 302 | if (driver->flags & I2C_DF_DUMMY) { |
|---|
| 303 | /* DUMMY drivers do not register their clients, so we have to |
|---|
| 304 | * use a trick here: we call driver->attach_adapter to |
|---|
| 305 | * *detach* it! Of course, each dummy driver should know about |
|---|
| 306 | * this or hell will break loose... |
|---|
| 307 | */ |
|---|
| 308 | if ((res = driver->attach_adapter(adap))) { |
|---|
| 309 | printk(KERN_WARNING "i2c-core.o: while unregistering " |
|---|
| 310 | "dummy driver %s, adapter %s could " |
|---|
| 311 | "not be detached properly; driver " |
|---|
| 312 | "not unloaded!",driver->name, |
|---|
| 313 | adap->name); |
|---|
| 314 | ADAP_UNLOCK(); |
|---|
| 315 | return res; |
|---|
| 316 | } |
|---|
| 317 | } else { |
|---|
| 318 | for (j=0;j<I2C_CLIENT_MAX;j++) { |
|---|
| 319 | struct i2c_client *client = adap->clients[j]; |
|---|
| 320 | if (client != NULL && |
|---|
| 321 | client->driver == driver) { |
|---|
| 322 | DEB2(printk(KERN_DEBUG "i2c-core.o: " |
|---|
| 323 | "detaching client %s:\n", |
|---|
| 324 | client->name)); |
|---|
| 325 | if ((res = driver-> |
|---|
| 326 | detach_client(client))) |
|---|
| 327 | { |
|---|
| 328 | printk(KERN_ERR "i2c-core.o: while " |
|---|
| 329 | "unregistering driver " |
|---|
| 330 | "`%s', the client at " |
|---|
| 331 | "address %02x of adapter `%s' could not be " |
|---|
| 332 | "detached; driver not unloaded!", |
|---|
| 333 | driver->name, |
|---|
| 334 | client->addr, |
|---|
| 335 | adap->name); |
|---|
| 336 | ADAP_UNLOCK(); |
|---|
| 337 | return res; |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | ADAP_UNLOCK(); |
|---|
| 344 | drivers[i] = NULL; |
|---|
| 345 | driver_count--; |
|---|
| 346 | DRV_UNLOCK(); |
|---|
| 347 | |
|---|
| 348 | DEB(printk(KERN_DEBUG "i2c-core.o: driver unregistered: %s\n",driver->name)); |
|---|
| 349 | return 0; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | int i2c_check_addr (struct i2c_adapter *adapter, int addr) |
|---|
| 353 | { |
|---|
| 354 | int i; |
|---|
| 355 | for (i = 0; i < I2C_CLIENT_MAX ; i++) |
|---|
| 356 | if (adapter->clients[i] && (adapter->clients[i]->addr == addr)) |
|---|
| 357 | return -EBUSY; |
|---|
| 358 | return 0; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | int i2c_attach_client(struct i2c_client *client) |
|---|
| 362 | { |
|---|
| 363 | struct i2c_adapter *adapter = client->adapter; |
|---|
| 364 | int i; |
|---|
| 365 | |
|---|
| 366 | if (i2c_check_addr(client->adapter,client->addr)) |
|---|
| 367 | return -EBUSY; |
|---|
| 368 | |
|---|
| 369 | for (i = 0; i < I2C_CLIENT_MAX; i++) |
|---|
| 370 | if (NULL == adapter->clients[i]) |
|---|
| 371 | break; |
|---|
| 372 | if (I2C_CLIENT_MAX == i) { |
|---|
| 373 | printk(KERN_WARNING |
|---|
| 374 | " i2c-core.o: attach_client(%s) - enlarge I2C_CLIENT_MAX.\n", |
|---|
| 375 | client->name); |
|---|
| 376 | return -ENOMEM; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | adapter->clients[i] = client; |
|---|
| 380 | adapter->client_count++; |
|---|
| 381 | |
|---|
| 382 | if (adapter->client_register) |
|---|
| 383 | if (adapter->client_register(client)) |
|---|
| 384 | printk(KERN_DEBUG "i2c-core.o: warning: client_register seems " |
|---|
| 385 | "to have failed for client %02x at adapter %s\n", |
|---|
| 386 | client->addr,adapter->name); |
|---|
| 387 | DEB(printk(KERN_DEBUG "i2c-core.o: client [%s] registered to adapter [%s](pos. %d).\n", |
|---|
| 388 | client->name, adapter->name,i)); |
|---|
| 389 | |
|---|
| 390 | if(client->flags & I2C_CLIENT_ALLOW_USE) |
|---|
| 391 | client->usage_count = 0; |
|---|
| 392 | |
|---|
| 393 | return 0; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | int i2c_detach_client(struct i2c_client *client) |
|---|
| 398 | { |
|---|
| 399 | struct i2c_adapter *adapter = client->adapter; |
|---|
| 400 | int i,res; |
|---|
| 401 | |
|---|
| 402 | for (i = 0; i < I2C_CLIENT_MAX; i++) |
|---|
| 403 | if (client == adapter->clients[i]) |
|---|
| 404 | break; |
|---|
| 405 | if (I2C_CLIENT_MAX == i) { |
|---|
| 406 | printk(KERN_WARNING " i2c-core.o: unregister_client " |
|---|
| 407 | "[%s] not found\n", |
|---|
| 408 | client->name); |
|---|
| 409 | return -ENODEV; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | if( (client->flags & I2C_CLIENT_ALLOW_USE) && |
|---|
| 413 | (client->usage_count>0)) |
|---|
| 414 | return -EBUSY; |
|---|
| 415 | |
|---|
| 416 | if (adapter->client_unregister != NULL) |
|---|
| 417 | if ((res = adapter->client_unregister(client))) { |
|---|
| 418 | printk(KERN_ERR "i2c-core.o: client_unregister [%s] failed, " |
|---|
| 419 | "client not detached",client->name); |
|---|
| 420 | return res; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | adapter->clients[i] = NULL; |
|---|
| 424 | adapter->client_count--; |
|---|
| 425 | |
|---|
| 426 | DEB(printk(KERN_DEBUG "i2c-core.o: client [%s] unregistered.\n",client->name)); |
|---|
| 427 | return 0; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | static int i2c_inc_use_client(struct i2c_client *client) |
|---|
| 431 | { |
|---|
| 432 | |
|---|
| 433 | if (!try_module_get(client->driver->owner)) |
|---|
| 434 | return -ENODEV; |
|---|
| 435 | if (!try_module_get(client->adapter->owner)) { |
|---|
| 436 | module_put(client->driver->owner); |
|---|
| 437 | return -ENODEV; |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | return 0; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | static void i2c_dec_use_client(struct i2c_client *client) |
|---|
| 444 | { |
|---|
| 445 | module_put(client->driver->owner); |
|---|
| 446 | module_put(client->adapter->owner); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | struct i2c_client *i2c_get_client(int driver_id, int adapter_id, |
|---|
| 450 | struct i2c_client *prev) |
|---|
| 451 | { |
|---|
| 452 | int i,j; |
|---|
| 453 | |
|---|
| 454 | /* Will iterate through the list of clients in each adapter of adapters-list |
|---|
| 455 | in search for a client that matches the search criteria. driver_id or |
|---|
| 456 | adapter_id are ignored if set to 0. If both are ignored this returns |
|---|
| 457 | first client found. */ |
|---|
| 458 | |
|---|
| 459 | i = j = 0; |
|---|
| 460 | |
|---|
| 461 | /* set starting point */ |
|---|
| 462 | if(prev) |
|---|
| 463 | { |
|---|
| 464 | if(!(prev->adapter)) |
|---|
| 465 | return (struct i2c_client *) -EINVAL; |
|---|
| 466 | |
|---|
| 467 | for(j=0; j < I2C_ADAP_MAX; j++) |
|---|
| 468 | if(prev->adapter == adapters[j]) |
|---|
| 469 | break; |
|---|
| 470 | |
|---|
| 471 | /* invalid starting point? */ |
|---|
| 472 | if (I2C_ADAP_MAX == j) { |
|---|
| 473 | printk(KERN_WARNING " i2c-core.o: get_client adapter for client:[%s] not found\n", |
|---|
| 474 | prev->name); |
|---|
| 475 | return (struct i2c_client *) -ENODEV; |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | for(i=0; i < I2C_CLIENT_MAX; i++) |
|---|
| 479 | if(prev == adapters[j]->clients[i]) |
|---|
| 480 | break; |
|---|
| 481 | |
|---|
| 482 | /* invalid starting point? */ |
|---|
| 483 | if (I2C_CLIENT_MAX == i) { |
|---|
| 484 | printk(KERN_WARNING " i2c-core.o: get_client client:[%s] not found\n", |
|---|
| 485 | prev->name); |
|---|
| 486 | return (struct i2c_client *) -ENODEV; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | i++; /* start from one after prev */ |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | for(; j < I2C_ADAP_MAX; j++) |
|---|
| 493 | { |
|---|
| 494 | if(!adapters[j]) |
|---|
| 495 | continue; |
|---|
| 496 | |
|---|
| 497 | if(adapter_id && (adapters[j]->id != adapter_id)) |
|---|
| 498 | continue; |
|---|
| 499 | |
|---|
| 500 | for(; i < I2C_CLIENT_MAX; i++) |
|---|
| 501 | { |
|---|
| 502 | if(!adapters[j]->clients[i]) |
|---|
| 503 | continue; |
|---|
| 504 | |
|---|
| 505 | if(driver_id && (adapters[j]->clients[i]->driver->id != driver_id)) |
|---|
| 506 | continue; |
|---|
| 507 | if(adapters[j]->clients[i]->flags & I2C_CLIENT_ALLOW_USE) |
|---|
| 508 | return adapters[j]->clients[i]; |
|---|
| 509 | } |
|---|
| 510 | i = 0; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | return 0; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | int i2c_use_client(struct i2c_client *client) |
|---|
| 517 | { |
|---|
| 518 | if (!i2c_inc_use_client(client)) |
|---|
| 519 | return -ENODEV; |
|---|
| 520 | |
|---|
| 521 | if (client->flags & I2C_CLIENT_ALLOW_USE) { |
|---|
| 522 | if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE) |
|---|
| 523 | client->usage_count++; |
|---|
| 524 | else if (client->usage_count > 0) |
|---|
| 525 | goto busy; |
|---|
| 526 | else |
|---|
| 527 | client->usage_count++; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | return 0; |
|---|
| 531 | busy: |
|---|
| 532 | i2c_dec_use_client(client); |
|---|
| 533 | return -EBUSY; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | int i2c_release_client(struct i2c_client *client) |
|---|
| 537 | { |
|---|
| 538 | if(client->flags & I2C_CLIENT_ALLOW_USE) { |
|---|
| 539 | if(client->usage_count>0) |
|---|
| 540 | client->usage_count--; |
|---|
| 541 | else |
|---|
| 542 | { |
|---|
| 543 | printk(KERN_WARNING " i2c-core.o: dec_use_client used one too many times\n"); |
|---|
| 544 | return -EPERM; |
|---|
| 545 | } |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | i2c_dec_use_client(client); |
|---|
| 549 | |
|---|
| 550 | return 0; |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | /* ---------------------------------------------------- |
|---|
| 554 | * The /proc functions |
|---|
| 555 | * ---------------------------------------------------- |
|---|
| 556 | */ |
|---|
| 557 | |
|---|
| 558 | #ifdef CONFIG_PROC_FS |
|---|
| 559 | |
|---|
| 560 | /* This function generates the output for /proc/bus/i2c */ |
|---|
| 561 | int read_bus_i2c(char *buf, char **start, off_t offset, int len, int *eof, |
|---|
| 562 | void *private) |
|---|
| 563 | { |
|---|
| 564 | int i; |
|---|
| 565 | int nr = 0; |
|---|
| 566 | /* Note that it is safe to write a `little' beyond len. Yes, really. */ |
|---|
| 567 | for (i = 0; (i < I2C_ADAP_MAX) && (nr < len); i++) |
|---|
| 568 | if (adapters[i]) { |
|---|
| 569 | nr += sprintf(buf+nr, "i2c-%d\t", i); |
|---|
| 570 | if (adapters[i]->algo->smbus_xfer) { |
|---|
| 571 | if (adapters[i]->algo->master_xfer) |
|---|
| 572 | nr += sprintf(buf+nr,"smbus/i2c"); |
|---|
| 573 | else |
|---|
| 574 | nr += sprintf(buf+nr,"smbus "); |
|---|
| 575 | } else if (adapters[i]->algo->master_xfer) |
|---|
| 576 | nr += sprintf(buf+nr,"i2c "); |
|---|
| 577 | else |
|---|
| 578 | nr += sprintf(buf+nr,"dummy "); |
|---|
| 579 | nr += sprintf(buf+nr,"\t%-32s\t%-32s\n", |
|---|
| 580 | adapters[i]->name, |
|---|
| 581 | adapters[i]->algo->name); |
|---|
| 582 | } |
|---|
| 583 | return nr; |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | /* This function generates the output for /proc/bus/i2c-? */ |
|---|
| 587 | ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count, |
|---|
| 588 | loff_t *ppos) |
|---|
| 589 | { |
|---|
| 590 | struct inode * inode = file->f_dentry->d_inode; |
|---|
| 591 | char *kbuf; |
|---|
| 592 | struct i2c_client *client; |
|---|
| 593 | int i,j,k,order_nr,len=0; |
|---|
| 594 | size_t len_total; |
|---|
| 595 | int order[I2C_CLIENT_MAX]; |
|---|
| 596 | #define OUTPUT_LENGTH_PER_LINE 70 |
|---|
| 597 | |
|---|
| 598 | len_total = file->f_pos + count; |
|---|
| 599 | if (len_total > (I2C_CLIENT_MAX * OUTPUT_LENGTH_PER_LINE) ) |
|---|
| 600 | /* adjust to maximum file size */ |
|---|
| 601 | len_total = (I2C_CLIENT_MAX * OUTPUT_LENGTH_PER_LINE); |
|---|
| 602 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 603 | if (adapters[i]->inode == inode->i_ino) { |
|---|
| 604 | /* We need a bit of slack in the kernel buffer; this makes the |
|---|
| 605 | sprintf safe. */ |
|---|
| 606 | if (! (kbuf = kmalloc(len_total + |
|---|
| 607 | OUTPUT_LENGTH_PER_LINE, |
|---|
| 608 | GFP_KERNEL))) |
|---|
| 609 | return -ENOMEM; |
|---|
| 610 | /* Order will hold the indexes of the clients |
|---|
| 611 | sorted by address */ |
|---|
| 612 | order_nr=0; |
|---|
| 613 | for (j = 0; j < I2C_CLIENT_MAX; j++) { |
|---|
| 614 | if ((client = adapters[i]->clients[j]) && |
|---|
| 615 | (client->driver->id != I2C_DRIVERID_I2CDEV)) { |
|---|
| 616 | for(k = order_nr; |
|---|
| 617 | (k > 0) && |
|---|
| 618 | adapters[i]->clients[order[k-1]]-> |
|---|
| 619 | addr > client->addr; |
|---|
| 620 | k--) |
|---|
| 621 | order[k] = order[k-1]; |
|---|
| 622 | order[k] = j; |
|---|
| 623 | order_nr++; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | for (j = 0; (j < order_nr) && (len < len_total); j++) { |
|---|
| 629 | client = adapters[i]->clients[order[j]]; |
|---|
| 630 | len += sprintf(kbuf+len,"%02x\t%-32s\t%-32s\n", |
|---|
| 631 | client->addr, |
|---|
| 632 | client->name, |
|---|
| 633 | client->driver->name); |
|---|
| 634 | } |
|---|
| 635 | len = len - file->f_pos; |
|---|
| 636 | if (len > count) |
|---|
| 637 | len = count; |
|---|
| 638 | if (len < 0) |
|---|
| 639 | len = 0; |
|---|
| 640 | if (copy_to_user (buf,kbuf+file->f_pos, len)) { |
|---|
| 641 | kfree(kbuf); |
|---|
| 642 | return -EFAULT; |
|---|
| 643 | } |
|---|
| 644 | file->f_pos += len; |
|---|
| 645 | kfree(kbuf); |
|---|
| 646 | return len; |
|---|
| 647 | } |
|---|
| 648 | return -ENOENT; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | static int i2cproc_register(struct i2c_adapter *adap, int bus) |
|---|
| 653 | { |
|---|
| 654 | char name[8]; |
|---|
| 655 | struct proc_dir_entry *proc_entry; |
|---|
| 656 | |
|---|
| 657 | sprintf(name,"i2c-%d", bus); |
|---|
| 658 | proc_entry = create_proc_entry(name,0,proc_bus); |
|---|
| 659 | if (! proc_entry) { |
|---|
| 660 | printk(KERN_ERR "i2c-core.o: Could not create /proc/bus/%s\n", |
|---|
| 661 | name); |
|---|
| 662 | return -ENOENT; |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | proc_entry->proc_fops = &i2cproc_operations; |
|---|
| 666 | proc_entry->owner = THIS_MODULE; |
|---|
| 667 | adap->inode = proc_entry->low_ino; |
|---|
| 668 | return 0; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | static void i2cproc_remove(int bus) |
|---|
| 672 | { |
|---|
| 673 | char name[8]; |
|---|
| 674 | sprintf(name,"i2c-%d", bus); |
|---|
| 675 | remove_proc_entry(name, proc_bus); |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | static int __init i2cproc_init(void) |
|---|
| 679 | { |
|---|
| 680 | struct proc_dir_entry *proc_bus_i2c; |
|---|
| 681 | |
|---|
| 682 | proc_bus_i2c = create_proc_entry("i2c",0,proc_bus); |
|---|
| 683 | if (!proc_bus_i2c) { |
|---|
| 684 | printk(KERN_ERR "i2c-core.o: Could not create /proc/bus/i2c"); |
|---|
| 685 | return -ENOENT; |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | proc_bus_i2c->read_proc = &read_bus_i2c; |
|---|
| 689 | proc_bus_i2c->owner = THIS_MODULE; |
|---|
| 690 | return 0; |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | static void __exit i2cproc_cleanup(void) |
|---|
| 694 | { |
|---|
| 695 | remove_proc_entry("i2c",proc_bus); |
|---|
| 696 | } |
|---|
| 697 | #endif /* def CONFIG_PROC_FS */ |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | /* ---------------------------------------------------- |
|---|
| 701 | * the functional interface to the i2c busses. |
|---|
| 702 | * ---------------------------------------------------- |
|---|
| 703 | */ |
|---|
| 704 | |
|---|
| 705 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num) |
|---|
| 706 | { |
|---|
| 707 | int ret; |
|---|
| 708 | |
|---|
| 709 | if (adap->algo->master_xfer) { |
|---|
| 710 | DEB2(printk(KERN_DEBUG "i2c-core.o: master_xfer: %s with %d msgs.\n", |
|---|
| 711 | adap->name,num)); |
|---|
| 712 | |
|---|
| 713 | I2C_LOCK(adap); |
|---|
| 714 | ret = adap->algo->master_xfer(adap,msgs,num); |
|---|
| 715 | I2C_UNLOCK(adap); |
|---|
| 716 | |
|---|
| 717 | return ret; |
|---|
| 718 | } else { |
|---|
| 719 | printk(KERN_ERR "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 720 | adap->id); |
|---|
| 721 | return -ENOSYS; |
|---|
| 722 | } |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
|---|
| 726 | { |
|---|
| 727 | int ret; |
|---|
| 728 | struct i2c_adapter *adap=client->adapter; |
|---|
| 729 | struct i2c_msg msg; |
|---|
| 730 | |
|---|
| 731 | if (client->adapter->algo->master_xfer) { |
|---|
| 732 | msg.addr = client->addr; |
|---|
| 733 | msg.flags = client->flags & I2C_M_TEN; |
|---|
| 734 | msg.len = count; |
|---|
| 735 | (const char *)msg.buf = buf; |
|---|
| 736 | |
|---|
| 737 | DEB2(printk(KERN_DEBUG "i2c-core.o: master_send: writing %d bytes on %s.\n", |
|---|
| 738 | count,client->adapter->name)); |
|---|
| 739 | |
|---|
| 740 | I2C_LOCK(adap); |
|---|
| 741 | ret = adap->algo->master_xfer(adap,&msg,1); |
|---|
| 742 | I2C_UNLOCK(adap); |
|---|
| 743 | |
|---|
| 744 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
|---|
| 745 | * transmitted, else error code. |
|---|
| 746 | */ |
|---|
| 747 | return (ret == 1 )? count : ret; |
|---|
| 748 | } else { |
|---|
| 749 | printk(KERN_ERR "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 750 | client->adapter->id); |
|---|
| 751 | return -ENOSYS; |
|---|
| 752 | } |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
|---|
| 756 | { |
|---|
| 757 | struct i2c_adapter *adap=client->adapter; |
|---|
| 758 | struct i2c_msg msg; |
|---|
| 759 | int ret; |
|---|
| 760 | if (client->adapter->algo->master_xfer) { |
|---|
| 761 | msg.addr = client->addr; |
|---|
| 762 | msg.flags = client->flags & I2C_M_TEN; |
|---|
| 763 | msg.flags |= I2C_M_RD; |
|---|
| 764 | msg.len = count; |
|---|
| 765 | msg.buf = buf; |
|---|
| 766 | |
|---|
| 767 | DEB2(printk(KERN_DEBUG "i2c-core.o: master_recv: reading %d bytes on %s.\n", |
|---|
| 768 | count,client->adapter->name)); |
|---|
| 769 | |
|---|
| 770 | I2C_LOCK(adap); |
|---|
| 771 | ret = adap->algo->master_xfer(adap,&msg,1); |
|---|
| 772 | I2C_UNLOCK(adap); |
|---|
| 773 | |
|---|
| 774 | DEB2(printk(KERN_DEBUG "i2c-core.o: master_recv: return:%d (count:%d, addr:0x%02x)\n", |
|---|
| 775 | ret, count, client->addr)); |
|---|
| 776 | |
|---|
| 777 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
|---|
| 778 | * transmitted, else error code. |
|---|
| 779 | */ |
|---|
| 780 | return (ret == 1 )? count : ret; |
|---|
| 781 | } else { |
|---|
| 782 | printk(KERN_DEBUG "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 783 | client->adapter->id); |
|---|
| 784 | return -ENOSYS; |
|---|
| 785 | } |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | |
|---|
| 789 | int i2c_control(struct i2c_client *client, |
|---|
| 790 | unsigned int cmd, unsigned long arg) |
|---|
| 791 | { |
|---|
| 792 | int ret = 0; |
|---|
| 793 | struct i2c_adapter *adap = client->adapter; |
|---|
| 794 | |
|---|
| 795 | DEB2(printk(KERN_DEBUG "i2c-core.o: i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg)); |
|---|
| 796 | switch ( cmd ) { |
|---|
| 797 | case I2C_RETRIES: |
|---|
| 798 | adap->retries = arg; |
|---|
| 799 | break; |
|---|
| 800 | case I2C_TIMEOUT: |
|---|
| 801 | adap->timeout = arg; |
|---|
| 802 | break; |
|---|
| 803 | default: |
|---|
| 804 | if (adap->algo->algo_control!=NULL) |
|---|
| 805 | ret = adap->algo->algo_control(adap,cmd,arg); |
|---|
| 806 | } |
|---|
| 807 | return ret; |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | /* ---------------------------------------------------- |
|---|
| 811 | * the i2c address scanning function |
|---|
| 812 | * Will not work for 10-bit addresses! |
|---|
| 813 | * ---------------------------------------------------- |
|---|
| 814 | */ |
|---|
| 815 | int i2c_probe(struct i2c_adapter *adapter, |
|---|
| 816 | struct i2c_client_address_data *address_data, |
|---|
| 817 | i2c_client_found_addr_proc *found_proc) |
|---|
| 818 | { |
|---|
| 819 | int addr,i,found,err; |
|---|
| 820 | int adap_id = i2c_adapter_id(adapter); |
|---|
| 821 | |
|---|
| 822 | /* Forget it if we can't probe using SMBUS_QUICK */ |
|---|
| 823 | if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK)) |
|---|
| 824 | return -1; |
|---|
| 825 | |
|---|
| 826 | for (addr = 0x00; addr <= 0x7f; addr++) { |
|---|
| 827 | |
|---|
| 828 | /* Skip if already in use */ |
|---|
| 829 | if (i2c_check_addr(adapter,addr)) |
|---|
| 830 | continue; |
|---|
| 831 | |
|---|
| 832 | /* If it is in one of the force entries, we don't do any detection |
|---|
| 833 | at all */ |
|---|
| 834 | found = 0; |
|---|
| 835 | |
|---|
| 836 | for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 3) { |
|---|
| 837 | if (((adap_id == address_data->force[i]) || |
|---|
| 838 | (address_data->force[i] == ANY_I2C_BUS)) && |
|---|
| 839 | (addr == address_data->force[i+1])) { |
|---|
| 840 | DEB2(printk(KERN_DEBUG "i2c-core.o: found force parameter for adapter %d, addr %04x\n", |
|---|
| 841 | adap_id,addr)); |
|---|
| 842 | if ((err = found_proc(adapter,addr,0,0))) |
|---|
| 843 | return err; |
|---|
| 844 | found = 1; |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | if (found) |
|---|
| 848 | continue; |
|---|
| 849 | |
|---|
| 850 | /* If this address is in one of the ignores, we can forget about |
|---|
| 851 | it right now */ |
|---|
| 852 | for (i = 0; |
|---|
| 853 | !found && (address_data->ignore[i] != I2C_CLIENT_END); |
|---|
| 854 | i += 2) { |
|---|
| 855 | if (((adap_id == address_data->ignore[i]) || |
|---|
| 856 | ((address_data->ignore[i] == ANY_I2C_BUS))) && |
|---|
| 857 | (addr == address_data->ignore[i+1])) { |
|---|
| 858 | DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore parameter for adapter %d, " |
|---|
| 859 | "addr %04x\n", adap_id ,addr)); |
|---|
| 860 | found = 1; |
|---|
| 861 | } |
|---|
| 862 | } |
|---|
| 863 | for (i = 0; |
|---|
| 864 | !found && (address_data->ignore_range[i] != I2C_CLIENT_END); |
|---|
| 865 | i += 3) { |
|---|
| 866 | if (((adap_id == address_data->ignore_range[i]) || |
|---|
| 867 | ((address_data->ignore_range[i]==ANY_I2C_BUS))) && |
|---|
| 868 | (addr >= address_data->ignore_range[i+1]) && |
|---|
| 869 | (addr <= address_data->ignore_range[i+2])) { |
|---|
| 870 | DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore_range parameter for adapter %d, " |
|---|
| 871 | "addr %04x\n", adap_id,addr)); |
|---|
| 872 | found = 1; |
|---|
| 873 | } |
|---|
| 874 | } |
|---|
| 875 | if (found) |
|---|
| 876 | continue; |
|---|
| 877 | |
|---|
| 878 | /* Now, we will do a detection, but only if it is in the normal or |
|---|
| 879 | probe entries */ |
|---|
| 880 | for (i = 0; |
|---|
| 881 | !found && (address_data->normal_i2c[i] != I2C_CLIENT_END); |
|---|
| 882 | i += 1) { |
|---|
| 883 | if (addr == address_data->normal_i2c[i]) { |
|---|
| 884 | found = 1; |
|---|
| 885 | DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c entry for adapter %d, " |
|---|
| 886 | "addr %02x", adap_id,addr)); |
|---|
| 887 | } |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | for (i = 0; |
|---|
| 891 | !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END); |
|---|
| 892 | i += 2) { |
|---|
| 893 | if ((addr >= address_data->normal_i2c_range[i]) && |
|---|
| 894 | (addr <= address_data->normal_i2c_range[i+1])) { |
|---|
| 895 | found = 1; |
|---|
| 896 | DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c_range entry for adapter %d, " |
|---|
| 897 | "addr %04x\n", adap_id,addr)); |
|---|
| 898 | } |
|---|
| 899 | } |
|---|
| 900 | |
|---|
| 901 | for (i = 0; |
|---|
| 902 | !found && (address_data->probe[i] != I2C_CLIENT_END); |
|---|
| 903 | i += 2) { |
|---|
| 904 | if (((adap_id == address_data->probe[i]) || |
|---|
| 905 | ((address_data->probe[i] == ANY_I2C_BUS))) && |
|---|
| 906 | (addr == address_data->probe[i+1])) { |
|---|
| 907 | found = 1; |
|---|
| 908 | DEB2(printk(KERN_DEBUG "i2c-core.o: found probe parameter for adapter %d, " |
|---|
| 909 | "addr %04x\n", adap_id,addr)); |
|---|
| 910 | } |
|---|
| 911 | } |
|---|
| 912 | for (i = 0; |
|---|
| 913 | !found && (address_data->probe_range[i] != I2C_CLIENT_END); |
|---|
| 914 | i += 3) { |
|---|
| 915 | if (((adap_id == address_data->probe_range[i]) || |
|---|
| 916 | (address_data->probe_range[i] == ANY_I2C_BUS)) && |
|---|
| 917 | (addr >= address_data->probe_range[i+1]) && |
|---|
| 918 | (addr <= address_data->probe_range[i+2])) { |
|---|
| 919 | found = 1; |
|---|
| 920 | DEB2(printk(KERN_DEBUG "i2c-core.o: found probe_range parameter for adapter %d, " |
|---|
| 921 | "addr %04x\n", adap_id,addr)); |
|---|
| 922 | } |
|---|
| 923 | } |
|---|
| 924 | if (!found) |
|---|
| 925 | continue; |
|---|
| 926 | |
|---|
| 927 | /* OK, so we really should examine this address. First check |
|---|
| 928 | whether there is some client here at all! */ |
|---|
| 929 | if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0) |
|---|
| 930 | if ((err = found_proc(adapter,addr,0,-1))) |
|---|
| 931 | return err; |
|---|
| 932 | } |
|---|
| 933 | return 0; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | /* |
|---|
| 937 | * return id number for a specific adapter |
|---|
| 938 | */ |
|---|
| 939 | int i2c_adapter_id(struct i2c_adapter *adap) |
|---|
| 940 | { |
|---|
| 941 | int i; |
|---|
| 942 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 943 | if (adap == adapters[i]) |
|---|
| 944 | return i; |
|---|
| 945 | return -1; |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | /* The SMBus parts */ |
|---|
| 949 | |
|---|
| 950 | #define POLY (0x1070U << 3) |
|---|
| 951 | static u8 |
|---|
| 952 | crc8(u16 data) |
|---|
| 953 | { |
|---|
| 954 | int i; |
|---|
| 955 | |
|---|
| 956 | for(i = 0; i < 8; i++) { |
|---|
| 957 | if (data & 0x8000) |
|---|
| 958 | data = data ^ POLY; |
|---|
| 959 | data = data << 1; |
|---|
| 960 | } |
|---|
| 961 | return (u8)(data >> 8); |
|---|
| 962 | } |
|---|
| 963 | |
|---|
| 964 | /* CRC over count bytes in the first array plus the bytes in the rest |
|---|
| 965 | array if it is non-null. rest[0] is the (length of rest) - 1 |
|---|
| 966 | and is included. */ |
|---|
| 967 | u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest) |
|---|
| 968 | { |
|---|
| 969 | int i; |
|---|
| 970 | |
|---|
| 971 | for(i = 0; i < count; i++) |
|---|
| 972 | crc = crc8((crc ^ first[i]) << 8); |
|---|
| 973 | if(rest != NULL) |
|---|
| 974 | for(i = 0; i <= rest[0]; i++) |
|---|
| 975 | crc = crc8((crc ^ rest[i]) << 8); |
|---|
| 976 | return crc; |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | u8 i2c_smbus_pec(int count, u8 *first, u8 *rest) |
|---|
| 980 | { |
|---|
| 981 | return i2c_smbus_partial_pec(0, count, first, rest); |
|---|
| 982 | } |
|---|
| 983 | |
|---|
| 984 | /* Returns new "size" (transaction type) |
|---|
| 985 | Note that we convert byte to byte_data and byte_data to word_data |
|---|
| 986 | rather than invent new xxx_PEC transactions. */ |
|---|
| 987 | int i2c_smbus_add_pec(u16 addr, u8 command, int size, |
|---|
| 988 | union i2c_smbus_data *data) |
|---|
| 989 | { |
|---|
| 990 | u8 buf[3]; |
|---|
| 991 | |
|---|
| 992 | buf[0] = addr << 1; |
|---|
| 993 | buf[1] = command; |
|---|
| 994 | switch(size) { |
|---|
| 995 | case I2C_SMBUS_BYTE: |
|---|
| 996 | data->byte = i2c_smbus_pec(2, buf, NULL); |
|---|
| 997 | size = I2C_SMBUS_BYTE_DATA; |
|---|
| 998 | break; |
|---|
| 999 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1000 | buf[2] = data->byte; |
|---|
| 1001 | data->word = buf[2] || |
|---|
| 1002 | (i2c_smbus_pec(3, buf, NULL) << 8); |
|---|
| 1003 | size = I2C_SMBUS_WORD_DATA; |
|---|
| 1004 | break; |
|---|
| 1005 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1006 | /* unsupported */ |
|---|
| 1007 | break; |
|---|
| 1008 | case I2C_SMBUS_BLOCK_DATA: |
|---|
| 1009 | data->block[data->block[0] + 1] = |
|---|
| 1010 | i2c_smbus_pec(2, buf, data->block); |
|---|
| 1011 | size = I2C_SMBUS_BLOCK_DATA_PEC; |
|---|
| 1012 | break; |
|---|
| 1013 | } |
|---|
| 1014 | return size; |
|---|
| 1015 | } |
|---|
| 1016 | |
|---|
| 1017 | int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial, |
|---|
| 1018 | union i2c_smbus_data *data) |
|---|
| 1019 | { |
|---|
| 1020 | u8 buf[3], rpec, cpec; |
|---|
| 1021 | |
|---|
| 1022 | buf[1] = command; |
|---|
| 1023 | switch(size) { |
|---|
| 1024 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1025 | buf[0] = (addr << 1) | 1; |
|---|
| 1026 | cpec = i2c_smbus_pec(2, buf, NULL); |
|---|
| 1027 | rpec = data->byte; |
|---|
| 1028 | break; |
|---|
| 1029 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1030 | buf[0] = (addr << 1) | 1; |
|---|
| 1031 | buf[2] = data->word & 0xff; |
|---|
| 1032 | cpec = i2c_smbus_pec(3, buf, NULL); |
|---|
| 1033 | rpec = data->word >> 8; |
|---|
| 1034 | break; |
|---|
| 1035 | case I2C_SMBUS_WORD_DATA_PEC: |
|---|
| 1036 | /* unsupported */ |
|---|
| 1037 | cpec = rpec = 0; |
|---|
| 1038 | break; |
|---|
| 1039 | case I2C_SMBUS_PROC_CALL_PEC: |
|---|
| 1040 | /* unsupported */ |
|---|
| 1041 | cpec = rpec = 0; |
|---|
| 1042 | break; |
|---|
| 1043 | case I2C_SMBUS_BLOCK_DATA_PEC: |
|---|
| 1044 | buf[0] = (addr << 1); |
|---|
| 1045 | buf[2] = (addr << 1) | 1; |
|---|
| 1046 | cpec = i2c_smbus_pec(3, buf, data->block); |
|---|
| 1047 | rpec = data->block[data->block[0] + 1]; |
|---|
| 1048 | break; |
|---|
| 1049 | case I2C_SMBUS_BLOCK_PROC_CALL_PEC: |
|---|
| 1050 | buf[0] = (addr << 1) | 1; |
|---|
| 1051 | rpec = i2c_smbus_partial_pec(partial, 1, |
|---|
| 1052 | buf, data->block); |
|---|
| 1053 | cpec = data->block[data->block[0] + 1]; |
|---|
| 1054 | break; |
|---|
| 1055 | default: |
|---|
| 1056 | cpec = rpec = 0; |
|---|
| 1057 | break; |
|---|
| 1058 | } |
|---|
| 1059 | if(rpec != cpec) { |
|---|
| 1060 | DEB(printk(KERN_DEBUG "i2c-core.o: Bad PEC 0x%02x vs. 0x%02x\n", |
|---|
| 1061 | rpec, cpec)); |
|---|
| 1062 | return -1; |
|---|
| 1063 | } |
|---|
| 1064 | return 0; |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value) |
|---|
| 1068 | { |
|---|
| 1069 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1070 | value,0,I2C_SMBUS_QUICK,NULL); |
|---|
| 1071 | } |
|---|
| 1072 | |
|---|
| 1073 | extern s32 i2c_smbus_read_byte(struct i2c_client * client) |
|---|
| 1074 | { |
|---|
| 1075 | union i2c_smbus_data data; |
|---|
| 1076 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1077 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) |
|---|
| 1078 | return -1; |
|---|
| 1079 | else |
|---|
| 1080 | return 0x0FF & data.byte; |
|---|
| 1081 | } |
|---|
| 1082 | |
|---|
| 1083 | extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value) |
|---|
| 1084 | { |
|---|
| 1085 | union i2c_smbus_data data; /* only for PEC */ |
|---|
| 1086 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1087 | I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data); |
|---|
| 1088 | } |
|---|
| 1089 | |
|---|
| 1090 | extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command) |
|---|
| 1091 | { |
|---|
| 1092 | union i2c_smbus_data data; |
|---|
| 1093 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1094 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) |
|---|
| 1095 | return -1; |
|---|
| 1096 | else |
|---|
| 1097 | return 0x0FF & data.byte; |
|---|
| 1098 | } |
|---|
| 1099 | |
|---|
| 1100 | extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, u8 command, |
|---|
| 1101 | u8 value) |
|---|
| 1102 | { |
|---|
| 1103 | union i2c_smbus_data data; |
|---|
| 1104 | data.byte = value; |
|---|
| 1105 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1106 | I2C_SMBUS_WRITE,command, |
|---|
| 1107 | I2C_SMBUS_BYTE_DATA,&data); |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command) |
|---|
| 1111 | { |
|---|
| 1112 | union i2c_smbus_data data; |
|---|
| 1113 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1114 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) |
|---|
| 1115 | return -1; |
|---|
| 1116 | else |
|---|
| 1117 | return 0x0FFFF & data.word; |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | extern s32 i2c_smbus_write_word_data(struct i2c_client * client, |
|---|
| 1121 | u8 command, u16 value) |
|---|
| 1122 | { |
|---|
| 1123 | union i2c_smbus_data data; |
|---|
| 1124 | data.word = value; |
|---|
| 1125 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1126 | I2C_SMBUS_WRITE,command, |
|---|
| 1127 | I2C_SMBUS_WORD_DATA,&data); |
|---|
| 1128 | } |
|---|
| 1129 | |
|---|
| 1130 | extern s32 i2c_smbus_process_call(struct i2c_client * client, |
|---|
| 1131 | u8 command, u16 value) |
|---|
| 1132 | { |
|---|
| 1133 | union i2c_smbus_data data; |
|---|
| 1134 | data.word = value; |
|---|
| 1135 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1136 | I2C_SMBUS_WRITE,command, |
|---|
| 1137 | I2C_SMBUS_PROC_CALL, &data)) |
|---|
| 1138 | return -1; |
|---|
| 1139 | else |
|---|
| 1140 | return 0x0FFFF & data.word; |
|---|
| 1141 | } |
|---|
| 1142 | |
|---|
| 1143 | /* Returns the number of read bytes */ |
|---|
| 1144 | extern s32 i2c_smbus_read_block_data(struct i2c_client * client, |
|---|
| 1145 | u8 command, u8 *values) |
|---|
| 1146 | { |
|---|
| 1147 | union i2c_smbus_data data; |
|---|
| 1148 | int i; |
|---|
| 1149 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1150 | I2C_SMBUS_READ,command, |
|---|
| 1151 | I2C_SMBUS_BLOCK_DATA,&data)) |
|---|
| 1152 | return -1; |
|---|
| 1153 | else { |
|---|
| 1154 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 1155 | values[i-1] = data.block[i]; |
|---|
| 1156 | return data.block[0]; |
|---|
| 1157 | } |
|---|
| 1158 | } |
|---|
| 1159 | |
|---|
| 1160 | extern s32 i2c_smbus_write_block_data(struct i2c_client * client, |
|---|
| 1161 | u8 command, u8 length, u8 *values) |
|---|
| 1162 | { |
|---|
| 1163 | union i2c_smbus_data data; |
|---|
| 1164 | int i; |
|---|
| 1165 | if (length > I2C_SMBUS_BLOCK_MAX) |
|---|
| 1166 | length = I2C_SMBUS_BLOCK_MAX; |
|---|
| 1167 | for (i = 1; i <= length; i++) |
|---|
| 1168 | data.block[i] = values[i-1]; |
|---|
| 1169 | data.block[0] = length; |
|---|
| 1170 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1171 | I2C_SMBUS_WRITE,command, |
|---|
| 1172 | I2C_SMBUS_BLOCK_DATA,&data); |
|---|
| 1173 | } |
|---|
| 1174 | |
|---|
| 1175 | /* Returns the number of read bytes */ |
|---|
| 1176 | extern s32 i2c_smbus_block_process_call(struct i2c_client * client, |
|---|
| 1177 | u8 command, u8 length, u8 *values) |
|---|
| 1178 | { |
|---|
| 1179 | union i2c_smbus_data data; |
|---|
| 1180 | int i; |
|---|
| 1181 | if (length > I2C_SMBUS_BLOCK_MAX - 1) |
|---|
| 1182 | return -1; |
|---|
| 1183 | data.block[0] = length; |
|---|
| 1184 | for (i = 1; i <= length; i++) |
|---|
| 1185 | data.block[i] = values[i-1]; |
|---|
| 1186 | if(i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1187 | I2C_SMBUS_WRITE, command, |
|---|
| 1188 | I2C_SMBUS_BLOCK_PROC_CALL, &data)) |
|---|
| 1189 | return -1; |
|---|
| 1190 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 1191 | values[i-1] = data.block[i]; |
|---|
| 1192 | return data.block[0]; |
|---|
| 1193 | } |
|---|
| 1194 | |
|---|
| 1195 | /* Returns the number of read bytes */ |
|---|
| 1196 | extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client, |
|---|
| 1197 | u8 command, u8 *values) |
|---|
| 1198 | { |
|---|
| 1199 | union i2c_smbus_data data; |
|---|
| 1200 | int i; |
|---|
| 1201 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1202 | I2C_SMBUS_READ,command, |
|---|
| 1203 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
|---|
| 1204 | return -1; |
|---|
| 1205 | else { |
|---|
| 1206 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 1207 | values[i-1] = data.block[i]; |
|---|
| 1208 | return data.block[0]; |
|---|
| 1209 | } |
|---|
| 1210 | } |
|---|
| 1211 | |
|---|
| 1212 | extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client, |
|---|
| 1213 | u8 command, u8 length, u8 *values) |
|---|
| 1214 | { |
|---|
| 1215 | union i2c_smbus_data data; |
|---|
| 1216 | int i; |
|---|
| 1217 | if (length > I2C_SMBUS_I2C_BLOCK_MAX) |
|---|
| 1218 | length = I2C_SMBUS_I2C_BLOCK_MAX; |
|---|
| 1219 | for (i = 1; i <= length; i++) |
|---|
| 1220 | data.block[i] = values[i-1]; |
|---|
| 1221 | data.block[0] = length; |
|---|
| 1222 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 1223 | I2C_SMBUS_WRITE,command, |
|---|
| 1224 | I2C_SMBUS_I2C_BLOCK_DATA,&data); |
|---|
| 1225 | } |
|---|
| 1226 | |
|---|
| 1227 | /* Simulate a SMBus command using the i2c protocol |
|---|
| 1228 | No checking of parameters is done! */ |
|---|
| 1229 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
|---|
| 1230 | unsigned short flags, |
|---|
| 1231 | char read_write, u8 command, int size, |
|---|
| 1232 | union i2c_smbus_data * data) |
|---|
| 1233 | { |
|---|
| 1234 | /* So we need to generate a series of msgs. In the case of writing, we |
|---|
| 1235 | need to use only one message; when reading, we need two. We initialize |
|---|
| 1236 | most things with sane defaults, to keep the code below somewhat |
|---|
| 1237 | simpler. */ |
|---|
| 1238 | unsigned char msgbuf0[34]; |
|---|
| 1239 | unsigned char msgbuf1[34]; |
|---|
| 1240 | int num = read_write == I2C_SMBUS_READ?2:1; |
|---|
| 1241 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
|---|
| 1242 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
|---|
| 1243 | }; |
|---|
| 1244 | int i; |
|---|
| 1245 | |
|---|
| 1246 | msgbuf0[0] = command; |
|---|
| 1247 | switch(size) { |
|---|
| 1248 | case I2C_SMBUS_QUICK: |
|---|
| 1249 | msg[0].len = 0; |
|---|
| 1250 | /* Special case: The read/write field is used as data */ |
|---|
| 1251 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; |
|---|
| 1252 | num = 1; |
|---|
| 1253 | break; |
|---|
| 1254 | case I2C_SMBUS_BYTE: |
|---|
| 1255 | if (read_write == I2C_SMBUS_READ) { |
|---|
| 1256 | /* Special case: only a read! */ |
|---|
| 1257 | msg[0].flags = I2C_M_RD | flags; |
|---|
| 1258 | num = 1; |
|---|
| 1259 | } |
|---|
| 1260 | break; |
|---|
| 1261 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1262 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1263 | msg[1].len = 1; |
|---|
| 1264 | else { |
|---|
| 1265 | msg[0].len = 2; |
|---|
| 1266 | msgbuf0[1] = data->byte; |
|---|
| 1267 | } |
|---|
| 1268 | break; |
|---|
| 1269 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1270 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1271 | msg[1].len = 2; |
|---|
| 1272 | else { |
|---|
| 1273 | msg[0].len=3; |
|---|
| 1274 | msgbuf0[1] = data->word & 0xff; |
|---|
| 1275 | msgbuf0[2] = (data->word >> 8) & 0xff; |
|---|
| 1276 | } |
|---|
| 1277 | break; |
|---|
| 1278 | case I2C_SMBUS_PROC_CALL: |
|---|
| 1279 | num = 2; /* Special case */ |
|---|
| 1280 | read_write = I2C_SMBUS_READ; |
|---|
| 1281 | msg[0].len = 3; |
|---|
| 1282 | msg[1].len = 2; |
|---|
| 1283 | msgbuf0[1] = data->word & 0xff; |
|---|
| 1284 | msgbuf0[2] = (data->word >> 8) & 0xff; |
|---|
| 1285 | break; |
|---|
| 1286 | case I2C_SMBUS_BLOCK_DATA: |
|---|
| 1287 | case I2C_SMBUS_BLOCK_DATA_PEC: |
|---|
| 1288 | if (read_write == I2C_SMBUS_READ) { |
|---|
| 1289 | printk(KERN_ERR "i2c-core.o: Block read not supported " |
|---|
| 1290 | "under I2C emulation!\n"); |
|---|
| 1291 | return -1; |
|---|
| 1292 | } else { |
|---|
| 1293 | msg[0].len = data->block[0] + 2; |
|---|
| 1294 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
|---|
| 1295 | printk(KERN_ERR "i2c-core.o: smbus_access called with " |
|---|
| 1296 | "invalid block write size (%d)\n", |
|---|
| 1297 | data->block[0]); |
|---|
| 1298 | return -1; |
|---|
| 1299 | } |
|---|
| 1300 | if(size == I2C_SMBUS_BLOCK_DATA_PEC) |
|---|
| 1301 | (msg[0].len)++; |
|---|
| 1302 | for (i = 1; i <= msg[0].len; i++) |
|---|
| 1303 | msgbuf0[i] = data->block[i-1]; |
|---|
| 1304 | } |
|---|
| 1305 | break; |
|---|
| 1306 | case I2C_SMBUS_BLOCK_PROC_CALL: |
|---|
| 1307 | case I2C_SMBUS_BLOCK_PROC_CALL_PEC: |
|---|
| 1308 | printk(KERN_ERR "i2c-core.o: Block process call not supported " |
|---|
| 1309 | "under I2C emulation!\n"); |
|---|
| 1310 | return -1; |
|---|
| 1311 | case I2C_SMBUS_I2C_BLOCK_DATA: |
|---|
| 1312 | if (read_write == I2C_SMBUS_READ) { |
|---|
| 1313 | msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX; |
|---|
| 1314 | } else { |
|---|
| 1315 | msg[0].len = data->block[0] + 1; |
|---|
| 1316 | if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) { |
|---|
| 1317 | printk("i2c-core.o: i2c_smbus_xfer_emulated called with " |
|---|
| 1318 | "invalid block write size (%d)\n", |
|---|
| 1319 | data->block[0]); |
|---|
| 1320 | return -1; |
|---|
| 1321 | } |
|---|
| 1322 | for (i = 1; i <= data->block[0]; i++) |
|---|
| 1323 | msgbuf0[i] = data->block[i]; |
|---|
| 1324 | } |
|---|
| 1325 | break; |
|---|
| 1326 | default: |
|---|
| 1327 | printk(KERN_ERR "i2c-core.o: smbus_access called with invalid size (%d)\n", |
|---|
| 1328 | size); |
|---|
| 1329 | return -1; |
|---|
| 1330 | } |
|---|
| 1331 | |
|---|
| 1332 | if (i2c_transfer(adapter, msg, num) < 0) |
|---|
| 1333 | return -1; |
|---|
| 1334 | |
|---|
| 1335 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1336 | switch(size) { |
|---|
| 1337 | case I2C_SMBUS_BYTE: |
|---|
| 1338 | data->byte = msgbuf0[0]; |
|---|
| 1339 | break; |
|---|
| 1340 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1341 | data->byte = msgbuf1[0]; |
|---|
| 1342 | break; |
|---|
| 1343 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1344 | case I2C_SMBUS_PROC_CALL: |
|---|
| 1345 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
|---|
| 1346 | break; |
|---|
| 1347 | case I2C_SMBUS_I2C_BLOCK_DATA: |
|---|
| 1348 | /* fixed at 32 for now */ |
|---|
| 1349 | data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX; |
|---|
| 1350 | for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++) |
|---|
| 1351 | data->block[i+1] = msgbuf1[i]; |
|---|
| 1352 | break; |
|---|
| 1353 | } |
|---|
| 1354 | return 0; |
|---|
| 1355 | } |
|---|
| 1356 | |
|---|
| 1357 | |
|---|
| 1358 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, |
|---|
| 1359 | char read_write, u8 command, int size, |
|---|
| 1360 | union i2c_smbus_data * data) |
|---|
| 1361 | { |
|---|
| 1362 | s32 res; |
|---|
| 1363 | int swpec = 0; |
|---|
| 1364 | u8 partial = 0; |
|---|
| 1365 | |
|---|
| 1366 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
|---|
| 1367 | if((flags & I2C_CLIENT_PEC) && |
|---|
| 1368 | !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) { |
|---|
| 1369 | swpec = 1; |
|---|
| 1370 | if(read_write == I2C_SMBUS_READ && |
|---|
| 1371 | size == I2C_SMBUS_BLOCK_DATA) |
|---|
| 1372 | size = I2C_SMBUS_BLOCK_DATA_PEC; |
|---|
| 1373 | else if(size == I2C_SMBUS_PROC_CALL) |
|---|
| 1374 | size = I2C_SMBUS_PROC_CALL_PEC; |
|---|
| 1375 | else if(size == I2C_SMBUS_BLOCK_PROC_CALL) { |
|---|
| 1376 | i2c_smbus_add_pec(addr, command, |
|---|
| 1377 | I2C_SMBUS_BLOCK_DATA, data); |
|---|
| 1378 | partial = data->block[data->block[0] + 1]; |
|---|
| 1379 | size = I2C_SMBUS_BLOCK_PROC_CALL_PEC; |
|---|
| 1380 | } else if(read_write == I2C_SMBUS_WRITE && |
|---|
| 1381 | size != I2C_SMBUS_QUICK && |
|---|
| 1382 | size != I2C_SMBUS_I2C_BLOCK_DATA) |
|---|
| 1383 | size = i2c_smbus_add_pec(addr, command, size, data); |
|---|
| 1384 | } |
|---|
| 1385 | |
|---|
| 1386 | if (adapter->algo->smbus_xfer) { |
|---|
| 1387 | I2C_LOCK(adapter); |
|---|
| 1388 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
|---|
| 1389 | command,size,data); |
|---|
| 1390 | I2C_UNLOCK(adapter); |
|---|
| 1391 | } else |
|---|
| 1392 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
|---|
| 1393 | command,size,data); |
|---|
| 1394 | |
|---|
| 1395 | if(res >= 0 && swpec && |
|---|
| 1396 | size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA && |
|---|
| 1397 | (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC || |
|---|
| 1398 | size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) { |
|---|
| 1399 | if(i2c_smbus_check_pec(addr, command, size, partial, data)) |
|---|
| 1400 | return -1; |
|---|
| 1401 | } |
|---|
| 1402 | return res; |
|---|
| 1403 | } |
|---|
| 1404 | |
|---|
| 1405 | |
|---|
| 1406 | /* You should always define `functionality'; the 'else' is just for |
|---|
| 1407 | backward compatibility. */ |
|---|
| 1408 | u32 i2c_get_functionality (struct i2c_adapter *adap) |
|---|
| 1409 | { |
|---|
| 1410 | if (adap->algo->functionality) |
|---|
| 1411 | return adap->algo->functionality(adap); |
|---|
| 1412 | else |
|---|
| 1413 | return 0xffffffff; |
|---|
| 1414 | } |
|---|
| 1415 | |
|---|
| 1416 | int i2c_check_functionality (struct i2c_adapter *adap, u32 func) |
|---|
| 1417 | { |
|---|
| 1418 | u32 adap_func = i2c_get_functionality (adap); |
|---|
| 1419 | return (func & adap_func) == func; |
|---|
| 1420 | } |
|---|
| 1421 | |
|---|
| 1422 | |
|---|
| 1423 | static int __init i2c_init(void) |
|---|
| 1424 | { |
|---|
| 1425 | printk(KERN_INFO "i2c-core.o: i2c core module version %s (%s)\n", I2C_VERSION, I2C_DATE); |
|---|
| 1426 | memset(adapters,0,sizeof(adapters)); |
|---|
| 1427 | memset(drivers,0,sizeof(drivers)); |
|---|
| 1428 | adap_count=0; |
|---|
| 1429 | driver_count=0; |
|---|
| 1430 | |
|---|
| 1431 | init_MUTEX(&adap_lock); |
|---|
| 1432 | init_MUTEX(&driver_lock); |
|---|
| 1433 | |
|---|
| 1434 | #ifdef CONFIG_PROC_FS |
|---|
| 1435 | i2cproc_init(); |
|---|
| 1436 | #endif |
|---|
| 1437 | |
|---|
| 1438 | return 0; |
|---|
| 1439 | } |
|---|
| 1440 | |
|---|
| 1441 | static void __exit i2c_exit(void) |
|---|
| 1442 | { |
|---|
| 1443 | #ifdef CONFIG_PROC_FS |
|---|
| 1444 | i2cproc_cleanup(); |
|---|
| 1445 | #endif |
|---|
| 1446 | } |
|---|
| 1447 | |
|---|
| 1448 | EXPORT_SYMBOL(i2c_add_adapter); |
|---|
| 1449 | EXPORT_SYMBOL(i2c_del_adapter); |
|---|
| 1450 | EXPORT_SYMBOL(i2c_add_driver); |
|---|
| 1451 | EXPORT_SYMBOL(i2c_del_driver); |
|---|
| 1452 | EXPORT_SYMBOL(i2c_attach_client); |
|---|
| 1453 | EXPORT_SYMBOL(i2c_detach_client); |
|---|
| 1454 | EXPORT_SYMBOL(i2c_get_client); |
|---|
| 1455 | EXPORT_SYMBOL(i2c_use_client); |
|---|
| 1456 | EXPORT_SYMBOL(i2c_release_client); |
|---|
| 1457 | EXPORT_SYMBOL(i2c_check_addr); |
|---|
| 1458 | |
|---|
| 1459 | |
|---|
| 1460 | EXPORT_SYMBOL(i2c_master_send); |
|---|
| 1461 | EXPORT_SYMBOL(i2c_master_recv); |
|---|
| 1462 | EXPORT_SYMBOL(i2c_control); |
|---|
| 1463 | EXPORT_SYMBOL(i2c_transfer); |
|---|
| 1464 | EXPORT_SYMBOL(i2c_adapter_id); |
|---|
| 1465 | EXPORT_SYMBOL(i2c_probe); |
|---|
| 1466 | |
|---|
| 1467 | EXPORT_SYMBOL(i2c_smbus_xfer); |
|---|
| 1468 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
|---|
| 1469 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
|---|
| 1470 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
|---|
| 1471 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
|---|
| 1472 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
|---|
| 1473 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
|---|
| 1474 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
|---|
| 1475 | EXPORT_SYMBOL(i2c_smbus_process_call); |
|---|
| 1476 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
|---|
| 1477 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
|---|
| 1478 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
|---|
| 1479 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
|---|
| 1480 | |
|---|
| 1481 | EXPORT_SYMBOL(i2c_get_functionality); |
|---|
| 1482 | EXPORT_SYMBOL(i2c_check_functionality); |
|---|
| 1483 | |
|---|
| 1484 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
|---|
| 1485 | MODULE_DESCRIPTION("I2C-Bus main module"); |
|---|
| 1486 | MODULE_LICENSE("GPL"); |
|---|
| 1487 | |
|---|
| 1488 | MODULE_PARM(i2c_debug, "i"); |
|---|
| 1489 | MODULE_PARM_DESC(i2c_debug,"debug level"); |
|---|
| 1490 | |
|---|
| 1491 | module_init(i2c_init); |
|---|
| 1492 | module_exit(i2c_exit); |
|---|