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