| 1 | /* ------------------------------------------------------------------------- */ |
|---|
| 2 | /* i2c-core.c - a device driver for the iic-bus interface */ |
|---|
| 3 | /* ------------------------------------------------------------------------- */ |
|---|
| 4 | /* Copyright (C) 1995-99 Simon G. Vogl |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | This program is distributed in the hope that it will be useful, |
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | GNU General Public License for more details. |
|---|
| 15 | |
|---|
| 16 | You should have received a copy of the GNU General Public License |
|---|
| 17 | along with this program; if not, write to the Free Software |
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|---|
| 19 | /* ------------------------------------------------------------------------- */ |
|---|
| 20 | #define RCSID "$Id$" |
|---|
| 21 | /* ------------------------------------------------------------------------- */ |
|---|
| 22 | |
|---|
| 23 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
|---|
| 24 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> */ |
|---|
| 25 | |
|---|
| 26 | #include <linux/module.h> |
|---|
| 27 | #include <linux/kernel.h> |
|---|
| 28 | #include <linux/errno.h> |
|---|
| 29 | #include <linux/malloc.h> |
|---|
| 30 | #include <linux/proc_fs.h> |
|---|
| 31 | #if LINUX_VERSION_CODE >= 0x020135 |
|---|
| 32 | #include <linux/init.h> |
|---|
| 33 | #else |
|---|
| 34 | #define __init |
|---|
| 35 | #endif |
|---|
| 36 | #include <linux/version.h> |
|---|
| 37 | #ifndef KERNEL_VERSION |
|---|
| 38 | #define KERNEL_VERSION(a,b,c) (((a) << 16) | ((b) << 8) | (c)) |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,4)) |
|---|
| 42 | #define copy_from_user memcpy_fromfs |
|---|
| 43 | #define copy_to_user memcpy_tofs |
|---|
| 44 | #else |
|---|
| 45 | #include <asm/uaccess.h> |
|---|
| 46 | #endif |
|---|
| 47 | |
|---|
| 48 | #include "i2c.h" |
|---|
| 49 | |
|---|
| 50 | /* ----- compatibility stuff ----------------------------------------------- */ |
|---|
| 51 | #if LINUX_VERSION_CODE < 0x020301 |
|---|
| 52 | #define init_MUTEX(s) do { *(s) = MUTEX; } while(0) |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | /* ----- global defines ---------------------------------------------------- */ |
|---|
| 56 | |
|---|
| 57 | /* exclusive access to the bus */ |
|---|
| 58 | #define I2C_LOCK(adap) down(&adap->lock) |
|---|
| 59 | #define I2C_UNLOCK(adap) up(&adap->lock) |
|---|
| 60 | |
|---|
| 61 | #define DEB(x) if (i2c_debug>=1) x; |
|---|
| 62 | #define DEB2(x) if (i2c_debug>=2) x; |
|---|
| 63 | |
|---|
| 64 | /* ----- global variables -------------------------------------------------- */ |
|---|
| 65 | |
|---|
| 66 | /**** algorithm list */ |
|---|
| 67 | static struct i2c_algorithm *algorithms[I2C_ALGO_MAX]; |
|---|
| 68 | static int algo_count; |
|---|
| 69 | |
|---|
| 70 | /**** adapter list */ |
|---|
| 71 | static struct i2c_adapter *adapters[I2C_ADAP_MAX]; |
|---|
| 72 | static int adap_count; |
|---|
| 73 | |
|---|
| 74 | /**** drivers list */ |
|---|
| 75 | static struct i2c_driver *drivers[I2C_DRIVER_MAX]; |
|---|
| 76 | static int driver_count; |
|---|
| 77 | |
|---|
| 78 | /**** debug level */ |
|---|
| 79 | static int i2c_debug=1; |
|---|
| 80 | |
|---|
| 81 | /* --------------------------------------------------- |
|---|
| 82 | * /proc entries |
|---|
| 83 | *---------------------------------------------------- |
|---|
| 84 | */ |
|---|
| 85 | |
|---|
| 86 | static int i2cproc_init(void); |
|---|
| 87 | static int i2cproc_cleanup(void); |
|---|
| 88 | |
|---|
| 89 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) |
|---|
| 90 | static void monitor_bus_i2c(struct inode *inode, int fill); |
|---|
| 91 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) */ |
|---|
| 92 | |
|---|
| 93 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 94 | |
|---|
| 95 | static ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count, |
|---|
| 96 | loff_t *ppos); |
|---|
| 97 | static int read_bus_i2c(char *buf, char **start, off_t offset, int len, |
|---|
| 98 | int *eof , void *private); |
|---|
| 99 | |
|---|
| 100 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 101 | |
|---|
| 102 | static int i2cproc_bus_read(struct inode * inode, struct file * file, |
|---|
| 103 | char * buf, int count); |
|---|
| 104 | static int read_bus_i2c(char *buf, char **start, off_t offset, int len, |
|---|
| 105 | int unused); |
|---|
| 106 | |
|---|
| 107 | static struct proc_dir_entry proc_bus_dir = |
|---|
| 108 | { |
|---|
| 109 | /* low_ino */ 0, /* Set by proc_register_dynamic */ |
|---|
| 110 | /* namelen */ 3, |
|---|
| 111 | /* name */ "bus", |
|---|
| 112 | /* mode */ S_IRUGO | S_IXUGO | S_IFDIR, |
|---|
| 113 | /* nlink */ 2, /* Corrected by proc_register[_dynamic] */ |
|---|
| 114 | /* uid */ 0, |
|---|
| 115 | /* gid */ 0, |
|---|
| 116 | /* size */ 0, |
|---|
| 117 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,36)) |
|---|
| 118 | /* ops */ &proc_dir_inode_operations, |
|---|
| 119 | #endif |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | static struct proc_dir_entry proc_bus_i2c_dir = |
|---|
| 123 | { |
|---|
| 124 | /* low_ino */ 0, /* Set by proc_register_dynamic */ |
|---|
| 125 | /* namelen */ 3, |
|---|
| 126 | /* name */ "i2c", |
|---|
| 127 | /* mode */ S_IRUGO | S_IFREG, |
|---|
| 128 | /* nlink */ 1, |
|---|
| 129 | /* uid */ 0, |
|---|
| 130 | /* gid */ 0, |
|---|
| 131 | /* size */ 0, |
|---|
| 132 | /* ops */ NULL, |
|---|
| 133 | /* get_info */ &read_bus_i2c |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 137 | |
|---|
| 138 | /* To implement the dynamic /proc/bus/i2c-? files, we need our own |
|---|
| 139 | implementation of the read hook */ |
|---|
| 140 | static struct file_operations i2cproc_operations = { |
|---|
| 141 | NULL, |
|---|
| 142 | i2cproc_bus_read, |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | static struct inode_operations i2cproc_inode_operations = { |
|---|
| 146 | &i2cproc_operations |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | static int i2cproc_initialized = 0; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /* --------------------------------------------------- |
|---|
| 153 | * registering functions |
|---|
| 154 | * --------------------------------------------------- |
|---|
| 155 | */ |
|---|
| 156 | |
|---|
| 157 | /* ----- |
|---|
| 158 | * Algorithms - used to access groups of similar hw adapters or |
|---|
| 159 | * specific interfaces like the PCF8584 et al. |
|---|
| 160 | */ |
|---|
| 161 | int i2c_add_algorithm(struct i2c_algorithm *algo) |
|---|
| 162 | { |
|---|
| 163 | int i; |
|---|
| 164 | |
|---|
| 165 | for (i = 0; i < I2C_ALGO_MAX; i++) |
|---|
| 166 | if (NULL == algorithms[i]) |
|---|
| 167 | break; |
|---|
| 168 | if (I2C_ALGO_MAX == i) { |
|---|
| 169 | printk(KERN_WARNING |
|---|
| 170 | " i2c: register_algorithm(%s) - enlarge I2C_ALGO_MAX.\n", |
|---|
| 171 | algo->name); |
|---|
| 172 | return -ENOMEM; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | algorithms[i] = algo; |
|---|
| 176 | algo_count++; |
|---|
| 177 | |
|---|
| 178 | DEB(printk("i2c: algorithm %s registered.\n",algo->name)); |
|---|
| 179 | return 0; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | int i2c_del_algorithm(struct i2c_algorithm *algo) |
|---|
| 184 | { |
|---|
| 185 | int i; |
|---|
| 186 | |
|---|
| 187 | for (i = 0; i < I2C_ALGO_MAX; i++) |
|---|
| 188 | if (algo == algorithms[i]) |
|---|
| 189 | break; |
|---|
| 190 | if (I2C_ALGO_MAX == i) { |
|---|
| 191 | printk(KERN_WARNING |
|---|
| 192 | " i2c: unregister_algorithm: [%s] not found.\n", |
|---|
| 193 | algo->name); |
|---|
| 194 | return -ENODEV; |
|---|
| 195 | } |
|---|
| 196 | algorithms[i] = NULL; |
|---|
| 197 | algo_count--; |
|---|
| 198 | |
|---|
| 199 | DEB(printk("i2c: algorithm unregistered: %s\n",algo->name)); |
|---|
| 200 | return 0; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | /* ----- |
|---|
| 205 | * i2c_add_adapter is called from within the algorithm layer, |
|---|
| 206 | * when a new hw adapter registers. A new device is register to be |
|---|
| 207 | * available for clients. |
|---|
| 208 | */ |
|---|
| 209 | int i2c_add_adapter(struct i2c_adapter *adap) |
|---|
| 210 | { |
|---|
| 211 | int i,j; |
|---|
| 212 | char name[8]; |
|---|
| 213 | struct proc_dir_entry *proc_entry; |
|---|
| 214 | |
|---|
| 215 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 216 | if (NULL == adapters[i]) |
|---|
| 217 | break; |
|---|
| 218 | if (I2C_ADAP_MAX == i) { |
|---|
| 219 | printk(KERN_WARNING |
|---|
| 220 | " i2c: register_adapter(%s) - enlarge I2C_ADAP_MAX.\n", |
|---|
| 221 | adap->name); |
|---|
| 222 | return -ENOMEM; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | adapters[i] = adap; |
|---|
| 227 | adap_count++; |
|---|
| 228 | |
|---|
| 229 | /* init data types */ |
|---|
| 230 | init_MUTEX(&adap->lock); |
|---|
| 231 | |
|---|
| 232 | /* inform drivers of new adapters */ |
|---|
| 233 | for (j=0;j<I2C_DRIVER_MAX;j++) |
|---|
| 234 | if (drivers[j]!=NULL && drivers[j]->flags&I2C_DF_NOTIFY) |
|---|
| 235 | drivers[j]->attach_adapter(adap); |
|---|
| 236 | |
|---|
| 237 | DEB(printk("i2c: adapter %s registered.\n",adap->name)); |
|---|
| 238 | sprintf(name,"i2c-%d", i); |
|---|
| 239 | |
|---|
| 240 | if (i2cproc_initialized) { |
|---|
| 241 | |
|---|
| 242 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 243 | proc_entry = create_proc_entry(name,0,proc_bus); |
|---|
| 244 | if (! proc_entry) { |
|---|
| 245 | printk("i2c-core: Could not create /proc/bus/%s\n", |
|---|
| 246 | name); |
|---|
| 247 | return -ENOENT; |
|---|
| 248 | } |
|---|
| 249 | proc_entry->ops = &i2cproc_inode_operations; |
|---|
| 250 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) |
|---|
| 251 | proc_entry->fill_inode = &monitor_bus_i2c; |
|---|
| 252 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) */ |
|---|
| 253 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 254 | if (!(proc_entry = kmalloc(sizeof(struct proc_dir_entry)+ |
|---|
| 255 | strlen(name)+1, GFP_KERNEL))) { |
|---|
| 256 | printk("i2c-core: Out of memory!\n"); |
|---|
| 257 | return -ENOMEM; |
|---|
| 258 | } |
|---|
| 259 | memset(proc_entry,0,sizeof(struct proc_dir_entry)); |
|---|
| 260 | proc_entry->namelen = strlen(name); |
|---|
| 261 | proc_entry->name = (char *) (proc_entry + 1); |
|---|
| 262 | proc_entry->mode = S_IRUGO | S_IFREG; |
|---|
| 263 | proc_entry->nlink = 1; |
|---|
| 264 | proc_entry->ops = &i2cproc_inode_operations; |
|---|
| 265 | |
|---|
| 266 | /* Nasty stuff to keep GCC satisfied */ |
|---|
| 267 | { |
|---|
| 268 | char *procname; |
|---|
| 269 | (const char *) procname = proc_entry->name; |
|---|
| 270 | strcpy (procname,name); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | if ((res = proc_register_dynamic(&proc_bus_dir, proc_entry))) { |
|---|
| 274 | printk("i2c-core: Could not create %s.\n",name); |
|---|
| 275 | kfree(proc_entry); |
|---|
| 276 | return res; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | adapters[i]->proc_entry = proc_entry; |
|---|
| 280 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 281 | |
|---|
| 282 | adapters[i]->inode = proc_entry->low_ino; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | return 0; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | int i2c_del_adapter(struct i2c_adapter *adap) |
|---|
| 289 | { |
|---|
| 290 | int i,j; |
|---|
| 291 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 292 | char name[8]; |
|---|
| 293 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 294 | |
|---|
| 295 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 296 | if (adap == adapters[i]) |
|---|
| 297 | break; |
|---|
| 298 | if (I2C_ADAP_MAX == i) { |
|---|
| 299 | printk( " i2c: unregister_adapter adap [%s] not found.\n", |
|---|
| 300 | adap->name); |
|---|
| 301 | return -ENODEV; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | if (i2cproc_initialized) { |
|---|
| 305 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 306 | sprintf(name,"i2c-%d", i); |
|---|
| 307 | remove_proc_entry(name,proc_bus); |
|---|
| 308 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 309 | if ((res = proc_unregister(&proc_bus_dir, |
|---|
| 310 | adapters[i]->proc_entry->low_ino))) { |
|---|
| 311 | printk("i2c-core: Deregistration of /proc " |
|---|
| 312 | "entry failed\n" |
|---|
| 313 | return res; |
|---|
| 314 | } |
|---|
| 315 | kfree(adapters[i]->proc_entry); |
|---|
| 316 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | /* detach any active clients */ |
|---|
| 320 | for (j=0;j<I2C_CLIENT_MAX;j++) { |
|---|
| 321 | struct i2c_client *client = adap->clients[j]; |
|---|
| 322 | if ( (client!=NULL) |
|---|
| 323 | /* && (client->driver->flags & I2C_DF_NOTIFY) */ ) |
|---|
| 324 | /* detaching devices is unconditional of the set notify |
|---|
| 325 | * flag, as _all_ clients that reside on the adapter |
|---|
| 326 | * must be deleted, as this would cause invalid states. |
|---|
| 327 | */ |
|---|
| 328 | client->driver->detach_client(client); |
|---|
| 329 | /* i2c_detach_client(client); --- frodo */ |
|---|
| 330 | } |
|---|
| 331 | /* all done, now unregister */ |
|---|
| 332 | adapters[i] = NULL; |
|---|
| 333 | adap_count--; |
|---|
| 334 | |
|---|
| 335 | DEB(printk("i2c: adapter unregistered: %s\n",adap->name)); |
|---|
| 336 | return 0; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | /* ----- |
|---|
| 340 | * What follows is the "upwards" interface: commands for talking to clients, |
|---|
| 341 | * which implement the functions to access the physical information of the |
|---|
| 342 | * chips. |
|---|
| 343 | */ |
|---|
| 344 | |
|---|
| 345 | int i2c_add_driver(struct i2c_driver *driver) |
|---|
| 346 | { |
|---|
| 347 | int i; |
|---|
| 348 | |
|---|
| 349 | for (i = 0; i < I2C_DRIVER_MAX; i++) |
|---|
| 350 | if (NULL == drivers[i]) |
|---|
| 351 | break; |
|---|
| 352 | if (I2C_DRIVER_MAX == i) { |
|---|
| 353 | printk(KERN_WARNING |
|---|
| 354 | " i2c: register_driver(%s) - enlarge I2C_DRIVER_MAX.\n", |
|---|
| 355 | driver->name); |
|---|
| 356 | return -ENOMEM; |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | drivers[i] = driver; |
|---|
| 360 | driver_count++; |
|---|
| 361 | |
|---|
| 362 | DEB(printk("i2c: driver %s registered.\n",driver->name)); |
|---|
| 363 | |
|---|
| 364 | /* now look for instances of driver on our adapters |
|---|
| 365 | */ |
|---|
| 366 | if ( driver->flags&I2C_DF_NOTIFY ) |
|---|
| 367 | for (i=0;i<I2C_ADAP_MAX;i++) |
|---|
| 368 | if (adapters[i]!=NULL) |
|---|
| 369 | driver->attach_adapter(adapters[i]); |
|---|
| 370 | |
|---|
| 371 | return 0; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | int i2c_del_driver(struct i2c_driver *driver) |
|---|
| 375 | { |
|---|
| 376 | int i,j,k; |
|---|
| 377 | |
|---|
| 378 | for (i = 0; i < I2C_DRIVER_MAX; i++) |
|---|
| 379 | if (driver == drivers[i]) |
|---|
| 380 | break; |
|---|
| 381 | if (I2C_DRIVER_MAX == i) { |
|---|
| 382 | printk(KERN_WARNING " i2c: unregister_driver: [%s] not found\n", |
|---|
| 383 | driver->name); |
|---|
| 384 | return -ENODEV; |
|---|
| 385 | } |
|---|
| 386 | /* Have a look at each adapter, if clients of this driver are still |
|---|
| 387 | * attached. If so, detach them to be able to kill the driver afterwards. |
|---|
| 388 | */ |
|---|
| 389 | DEB2(printk("i2c: unregister_driver - looking for clients.\n")); |
|---|
| 390 | /* removing clients does not depend on the notify flag, else |
|---|
| 391 | * invalid operation might (will!) result, when using stale client |
|---|
| 392 | * pointers. |
|---|
| 393 | */ |
|---|
| 394 | for (k=0;k<I2C_ADAP_MAX;k++) { |
|---|
| 395 | struct i2c_adapter *adap = adapters[k]; |
|---|
| 396 | if (adap == NULL) /* skip empty entries. */ |
|---|
| 397 | continue; |
|---|
| 398 | DEB2(printk("i2c: examining adapter %s:\n",adap->name)); |
|---|
| 399 | for (j=0;j<I2C_CLIENT_MAX;j++) { |
|---|
| 400 | struct i2c_client *client = adap->clients[j]; |
|---|
| 401 | if (client != NULL && client->driver == driver) { |
|---|
| 402 | DEB2(printk("i2c: detaching client %s:\n", |
|---|
| 403 | client->name)); |
|---|
| 404 | /*i2c_detach_client(client);*/ |
|---|
| 405 | driver->detach_client(client); |
|---|
| 406 | } |
|---|
| 407 | } |
|---|
| 408 | } |
|---|
| 409 | drivers[i] = NULL; |
|---|
| 410 | driver_count--; |
|---|
| 411 | |
|---|
| 412 | DEB(printk("i2c: driver unregistered: %s\n",driver->name)); |
|---|
| 413 | return 0; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | int i2c_attach_client(struct i2c_client *client) |
|---|
| 418 | { |
|---|
| 419 | struct i2c_adapter *adapter = client->adapter; |
|---|
| 420 | struct i2c_algorithm *algo = adapter->algo; |
|---|
| 421 | int i; |
|---|
| 422 | |
|---|
| 423 | for (i = 0; i < I2C_CLIENT_MAX; i++) |
|---|
| 424 | if (NULL == adapter->clients[i]) |
|---|
| 425 | break; |
|---|
| 426 | if (I2C_CLIENT_MAX == i) { |
|---|
| 427 | printk(KERN_WARNING |
|---|
| 428 | " i2c: attach_client(%s) - enlarge I2C_CLIENT_MAX.\n", |
|---|
| 429 | client->name); |
|---|
| 430 | return -ENOMEM; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | adapter->clients[i] = client; |
|---|
| 434 | adapter->client_count++; |
|---|
| 435 | if (algo->client_register != NULL) |
|---|
| 436 | algo->client_register(client); |
|---|
| 437 | DEB(printk("i2c: client [%s] registered to adapter [%s](pos. %d).\n", |
|---|
| 438 | client->name, adapter->name,i)); |
|---|
| 439 | return 0; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | int i2c_detach_client(struct i2c_client *client) |
|---|
| 444 | { |
|---|
| 445 | struct i2c_adapter *adapter = client->adapter; |
|---|
| 446 | struct i2c_algorithm *algo = adapter->algo; |
|---|
| 447 | int i; |
|---|
| 448 | |
|---|
| 449 | for (i = 0; i < I2C_CLIENT_MAX; i++) |
|---|
| 450 | if (client == adapter->clients[i]) |
|---|
| 451 | break; |
|---|
| 452 | if (I2C_CLIENT_MAX == i) { |
|---|
| 453 | printk(KERN_WARNING " i2c: unregister_client [%s] not found\n", |
|---|
| 454 | client->name); |
|---|
| 455 | return -ENODEV; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | if (algo->client_unregister != NULL) |
|---|
| 459 | algo->client_unregister(client); |
|---|
| 460 | /* client->driver->detach_client(client);*/ |
|---|
| 461 | adapter->clients[i] = NULL; |
|---|
| 462 | adapter->client_count--; |
|---|
| 463 | DEB(printk("i2c: client [%s] unregistered.\n",client->name)); |
|---|
| 464 | return 0; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | void i2c_inc_use_client(struct i2c_client *client) |
|---|
| 468 | { |
|---|
| 469 | |
|---|
| 470 | if (client->driver->inc_use != NULL) |
|---|
| 471 | client->driver->inc_use(client); |
|---|
| 472 | |
|---|
| 473 | if (client->adapter->inc_use != NULL) |
|---|
| 474 | client->adapter->inc_use(client->adapter); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | void i2c_dec_use_client(struct i2c_client *client) |
|---|
| 478 | { |
|---|
| 479 | |
|---|
| 480 | if (client->driver->dec_use != NULL) |
|---|
| 481 | client->driver->dec_use(client); |
|---|
| 482 | |
|---|
| 483 | if (client->adapter->dec_use != NULL) |
|---|
| 484 | client->adapter->dec_use(client->adapter); |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | /* ---------------------------------------------------- |
|---|
| 488 | * The /proc functions |
|---|
| 489 | * ---------------------------------------------------- |
|---|
| 490 | */ |
|---|
| 491 | |
|---|
| 492 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) |
|---|
| 493 | /* Monitor access to /proc/bus/i2c*; make unloading i2c-proc impossible |
|---|
| 494 | if some process still uses it or some file in it */ |
|---|
| 495 | void monitor_bus_i2c(struct inode *inode, int fill) |
|---|
| 496 | { |
|---|
| 497 | if (fill) |
|---|
| 498 | MOD_INC_USE_COUNT; |
|---|
| 499 | else |
|---|
| 500 | MOD_DEC_USE_COUNT; |
|---|
| 501 | } |
|---|
| 502 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) */ |
|---|
| 503 | |
|---|
| 504 | /* This function generates the output for /proc/bus/i2c */ |
|---|
| 505 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 506 | int read_bus_i2c(char *buf, char **start, off_t offset, int len, int *eof, |
|---|
| 507 | void *private) |
|---|
| 508 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 509 | int read_bus_i2c(char *buf, char **start, off_t offset, int len, int unused) |
|---|
| 510 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 511 | { |
|---|
| 512 | int i; |
|---|
| 513 | int nr = 0; |
|---|
| 514 | /* Note that it is safe to write a `little' beyond len. Yes, really. */ |
|---|
| 515 | for (i = 0; (i < I2C_ADAP_MAX) && (nr < len); i++) |
|---|
| 516 | if (adapters[i]) { |
|---|
| 517 | nr += sprintf(buf+nr, "i2c-%d\t", i); |
|---|
| 518 | if (adapters[i]->algo->smbus_xfer) { |
|---|
| 519 | if (adapters[i]->algo->master_xfer) |
|---|
| 520 | nr += sprintf(buf+nr,"smbus/i2c"); |
|---|
| 521 | else |
|---|
| 522 | nr += sprintf(buf+nr,"smbus "); |
|---|
| 523 | } else if (adapters[i]->algo->master_xfer) |
|---|
| 524 | nr += sprintf(buf+nr,"i2c "); |
|---|
| 525 | else |
|---|
| 526 | nr += sprintf(buf+nr,"dummy "); |
|---|
| 527 | nr += sprintf(buf+nr,"\t%-32s\t%-32s\n", |
|---|
| 528 | adapters[i]->name, |
|---|
| 529 | adapters[i]->algo->name); |
|---|
| 530 | } |
|---|
| 531 | return nr; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | /* This function generates the output for /proc/bus/i2c-? */ |
|---|
| 535 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 536 | ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count, |
|---|
| 537 | loff_t *ppos) |
|---|
| 538 | { |
|---|
| 539 | struct inode * inode = file->f_dentry->d_inode; |
|---|
| 540 | #else (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) |
|---|
| 541 | int i2cproc_bus_read(struct inode * inode, struct file * file,char * buf, |
|---|
| 542 | int count) |
|---|
| 543 | { |
|---|
| 544 | #endif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 545 | char *kbuf; |
|---|
| 546 | struct i2c_client *client; |
|---|
| 547 | int i,j,len=0; |
|---|
| 548 | |
|---|
| 549 | if (count < 0) |
|---|
| 550 | return -EINVAL; |
|---|
| 551 | if (count > 4000) |
|---|
| 552 | count = 4000; |
|---|
| 553 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 554 | if (adapters[i]->inode == inode->i_ino) { |
|---|
| 555 | /* We need a bit of slack in the kernel buffer; this makes the |
|---|
| 556 | sprintf safe. */ |
|---|
| 557 | if (! (kbuf = kmalloc(count + 80,GFP_KERNEL))) |
|---|
| 558 | return -ENOMEM; |
|---|
| 559 | for (j = 0; j < I2C_CLIENT_MAX; j++) |
|---|
| 560 | if ((client = adapters[i]->clients[j])) |
|---|
| 561 | /* Filter out dummy clients */ |
|---|
| 562 | if (client->driver->id != I2C_DRIVERID_I2CDEV) |
|---|
| 563 | len += sprintf(kbuf+len,"%x\t%-32s\t%-32s\n", |
|---|
| 564 | client->addr, |
|---|
| 565 | client->name,client->driver->name); |
|---|
| 566 | if (file->f_pos+len > count) |
|---|
| 567 | len = count - file->f_pos; |
|---|
| 568 | len = len - file->f_pos; |
|---|
| 569 | if (len < 0) |
|---|
| 570 | len = 0; |
|---|
| 571 | copy_to_user (buf,kbuf+file->f_pos,len); |
|---|
| 572 | file->f_pos += len; |
|---|
| 573 | kfree(kbuf); |
|---|
| 574 | return len; |
|---|
| 575 | } |
|---|
| 576 | return -ENOENT; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | int i2cproc_init(void) |
|---|
| 580 | { |
|---|
| 581 | |
|---|
| 582 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 583 | struct proc_dir_entry *proc_bus_i2c; |
|---|
| 584 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 585 | |
|---|
| 586 | i2cproc_initialized = 0; |
|---|
| 587 | |
|---|
| 588 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 589 | if (! proc_bus) { |
|---|
| 590 | printk("i2c-core: /proc/bus/ does not exist"); |
|---|
| 591 | i2cproc_cleanup(); |
|---|
| 592 | return -ENOENT; |
|---|
| 593 | } |
|---|
| 594 | proc_bus_i2c = create_proc_entry("i2c",0,proc_bus); |
|---|
| 595 | if (!proc_bus_i2c) { |
|---|
| 596 | printk("i2c-core: Could not create /proc/bus/i2c"); |
|---|
| 597 | i2cproc_cleanup(); |
|---|
| 598 | return -ENOENT; |
|---|
| 599 | } |
|---|
| 600 | proc_bus_i2c->read_proc = &read_bus_i2c; |
|---|
| 601 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) |
|---|
| 602 | proc_bus_i2c->fill_inode = &monitor_bus_i2c; |
|---|
| 603 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 604 | i2cproc_initialized += 2; |
|---|
| 605 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 606 | /* In Linux 2.0.x, there is no /proc/bus! But I hope no other module |
|---|
| 607 | introduced it, or we are fucked. And 2.0.35 and earlier does not |
|---|
| 608 | export proc_dir_inode_operations, so we grab it from proc_net, |
|---|
| 609 | which also uses it. Not nice. */ |
|---|
| 610 | proc_bus_dir.ops = proc_net.ops; |
|---|
| 611 | if ((res = proc_register_dynamic(&proc_root, &proc_bus_dir))) { |
|---|
| 612 | printk("i2c-core: Could not create /proc/bus/"); |
|---|
| 613 | i2cproc_cleanup(); |
|---|
| 614 | return res; |
|---|
| 615 | } |
|---|
| 616 | i2cproc_initialized ++; |
|---|
| 617 | if ((res = proc_register_dynamic(&proc_bus_dir, &proc_bus_i2c_dir))) { |
|---|
| 618 | printk("i2c-core: Could not create /proc/bus/i2c\n"); |
|---|
| 619 | i2cproc_cleanup(); |
|---|
| 620 | return res; |
|---|
| 621 | } |
|---|
| 622 | i2cproc_initialized ++; |
|---|
| 623 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 624 | return 0; |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | int i2cproc_cleanup(void) |
|---|
| 628 | { |
|---|
| 629 | |
|---|
| 630 | if (i2cproc_initialized >= 1) { |
|---|
| 631 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) |
|---|
| 632 | remove_proc_entry("i2c",proc_bus); |
|---|
| 633 | i2cproc_initialized -= 2; |
|---|
| 634 | #else /* (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,29)) */ |
|---|
| 635 | if (i2cproc_initialized >= 2) { |
|---|
| 636 | if ((res = proc_unregister(&proc_bus_dir, |
|---|
| 637 | proc_bus_i2c_dir.low_ino))) { |
|---|
| 638 | printk("i2c-core: could not delete " |
|---|
| 639 | "/proc/bus/i2c, module not removed."); |
|---|
| 640 | return res; |
|---|
| 641 | } |
|---|
| 642 | i2cproc_initialized --; |
|---|
| 643 | } |
|---|
| 644 | if ((res = proc_unregister(&proc_root,proc_bus_dir.low_ino))) { |
|---|
| 645 | printk("i2c-core: could not delete /proc/bus/, " |
|---|
| 646 | "module not removed."); |
|---|
| 647 | return res; |
|---|
| 648 | } |
|---|
| 649 | i2cproc_initialized --; |
|---|
| 650 | #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,29)) */ |
|---|
| 651 | } |
|---|
| 652 | return 0; |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | /* ---------------------------------------------------- |
|---|
| 657 | * the functional interface to the i2c busses. |
|---|
| 658 | * ---------------------------------------------------- |
|---|
| 659 | */ |
|---|
| 660 | |
|---|
| 661 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num) |
|---|
| 662 | { |
|---|
| 663 | int ret; |
|---|
| 664 | |
|---|
| 665 | if (adap->algo->master_xfer) { |
|---|
| 666 | DEB(printk("master_xfer: %s with %d msgs.\n",adap->name,num)); |
|---|
| 667 | |
|---|
| 668 | I2C_LOCK(adap); |
|---|
| 669 | ret = adap->algo->master_xfer(adap,msgs,num); |
|---|
| 670 | I2C_UNLOCK(adap); |
|---|
| 671 | |
|---|
| 672 | return ret; |
|---|
| 673 | } else { |
|---|
| 674 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 675 | adap->id); |
|---|
| 676 | return 0; |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
|---|
| 681 | { |
|---|
| 682 | int ret; |
|---|
| 683 | struct i2c_adapter *adap=client->adapter; |
|---|
| 684 | struct i2c_msg msg; |
|---|
| 685 | |
|---|
| 686 | if (client->adapter->algo->master_xfer) { |
|---|
| 687 | msg.addr = client->addr; |
|---|
| 688 | msg.flags = client->flags & ( I2C_M_TEN|I2C_M_TENMASK ); |
|---|
| 689 | msg.len = count; |
|---|
| 690 | (const char *)msg.buf = buf; |
|---|
| 691 | |
|---|
| 692 | DEB(printk("master_send: writing %d bytes on %s.\n", |
|---|
| 693 | count,client->adapter->name)); |
|---|
| 694 | |
|---|
| 695 | I2C_LOCK(adap); |
|---|
| 696 | ret = adap->algo->master_xfer(adap,&msg,1); |
|---|
| 697 | I2C_UNLOCK(adap); |
|---|
| 698 | |
|---|
| 699 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
|---|
| 700 | * transmitted, else error code. |
|---|
| 701 | */ |
|---|
| 702 | return (ret == 1 )? count : ret; |
|---|
| 703 | } else { |
|---|
| 704 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 705 | client->adapter->id); |
|---|
| 706 | return 0; |
|---|
| 707 | } |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | int i2c_master_send_subadress(struct i2c_client *client,const char *buf ,int count, int subadress) |
|---|
| 711 | { |
|---|
| 712 | char* c; |
|---|
| 713 | |
|---|
| 714 | int ret; |
|---|
| 715 | struct i2c_adapter *adap=client->adapter; |
|---|
| 716 | struct i2c_msg msg; |
|---|
| 717 | |
|---|
| 718 | if (client->adapter->algo->master_xfer) { |
|---|
| 719 | c = (char*)kmalloc(sizeof(char)*(count+1),GFP_KERNEL); |
|---|
| 720 | if (0 == c) |
|---|
| 721 | return -ENOMEM; |
|---|
| 722 | |
|---|
| 723 | msg.addr = client->addr; |
|---|
| 724 | msg.flags = client->flags & ( I2C_M_TEN|I2C_M_TENMASK ); |
|---|
| 725 | msg.len = count+1; |
|---|
| 726 | (const char *)msg.buf = c; |
|---|
| 727 | |
|---|
| 728 | c[0] = subadress; |
|---|
| 729 | memcpy(&c[1], buf, sizeof(char)*count); |
|---|
| 730 | |
|---|
| 731 | DEB(printk("master_send_subadress: writing %d bytes on %s. (sa:0x%02x)\n", |
|---|
| 732 | count,client->adapter->name,subadress)); |
|---|
| 733 | |
|---|
| 734 | I2C_LOCK(adap); |
|---|
| 735 | ret = adap->algo->master_xfer(adap,&msg,1); |
|---|
| 736 | I2C_UNLOCK(adap); |
|---|
| 737 | |
|---|
| 738 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
|---|
| 739 | * transmitted, else error code. |
|---|
| 740 | */ |
|---|
| 741 | |
|---|
| 742 | kfree(c); |
|---|
| 743 | |
|---|
| 744 | return (ret == 1 )? count : ret; |
|---|
| 745 | } else { |
|---|
| 746 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 747 | client->adapter->id); |
|---|
| 748 | return 0; |
|---|
| 749 | } |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | int i2c_master_recv_subadress(struct i2c_client *client,const char *buf ,int count, int subadress) |
|---|
| 754 | { |
|---|
| 755 | int ret; |
|---|
| 756 | struct i2c_adapter *adap=client->adapter; |
|---|
| 757 | struct i2c_msg msg[2]; |
|---|
| 758 | |
|---|
| 759 | if (client->adapter->algo->master_xfer) { |
|---|
| 760 | msg[0].addr = client->addr; |
|---|
| 761 | msg[0].flags = client->flags & ( I2C_M_TEN|I2C_M_TENMASK ); |
|---|
| 762 | msg[0].len = 1; |
|---|
| 763 | (const char*)msg[0].buf = (const char*)&subadress; |
|---|
| 764 | |
|---|
| 765 | msg[1].addr = client->addr; |
|---|
| 766 | msg[1].flags = client->flags & ( I2C_M_TEN|I2C_M_TENMASK ); |
|---|
| 767 | msg[1].flags |= I2C_M_RD; |
|---|
| 768 | msg[1].len = count; |
|---|
| 769 | (const char*)msg[1].buf = (const char*)buf; |
|---|
| 770 | |
|---|
| 771 | DEB(printk("master_recv_subadress: reading %d bytes from subadress %d on %s.\n", |
|---|
| 772 | count,subadress,client->adapter->name)); |
|---|
| 773 | |
|---|
| 774 | I2C_LOCK(adap); |
|---|
| 775 | ret = adap->algo->master_xfer(adap,msg,2); |
|---|
| 776 | I2C_UNLOCK(adap); |
|---|
| 777 | |
|---|
| 778 | /* if everything went ok (i.e. 1 msg transmitted), |
|---|
| 779 | return 0, else return error code. |
|---|
| 780 | */ |
|---|
| 781 | return (ret == 1 ) ? 0 : ret; |
|---|
| 782 | } else { |
|---|
| 783 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 784 | client->adapter->id); |
|---|
| 785 | return 0; |
|---|
| 786 | } |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
|---|
| 792 | { |
|---|
| 793 | struct i2c_adapter *adap=client->adapter; |
|---|
| 794 | struct i2c_msg msg; |
|---|
| 795 | int ret; |
|---|
| 796 | if (client->adapter->algo->master_xfer) { |
|---|
| 797 | msg.addr = client->addr; |
|---|
| 798 | msg.flags = client->flags & ( I2C_M_TEN|I2C_M_TENMASK ); |
|---|
| 799 | msg.flags |= I2C_M_RD; |
|---|
| 800 | msg.len = count; |
|---|
| 801 | msg.buf = buf; |
|---|
| 802 | |
|---|
| 803 | DEB(printk("master_recv: reading %d bytes on %s.\n", |
|---|
| 804 | count,client->adapter->name)); |
|---|
| 805 | |
|---|
| 806 | I2C_LOCK(adap); |
|---|
| 807 | ret = adap->algo->master_xfer(adap,&msg,1); |
|---|
| 808 | I2C_UNLOCK(adap); |
|---|
| 809 | |
|---|
| 810 | DEB(printk("master_recv: return:%d (count:%d, addr:0x%02x)\n", |
|---|
| 811 | ret, count, client->addr)); |
|---|
| 812 | |
|---|
| 813 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
|---|
| 814 | * transmitted, else error code. |
|---|
| 815 | */ |
|---|
| 816 | return (ret == 1 )? count : ret; |
|---|
| 817 | } else { |
|---|
| 818 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 819 | client->adapter->id); |
|---|
| 820 | return 0; |
|---|
| 821 | } |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | int i2c_control(struct i2c_client *client, |
|---|
| 826 | unsigned int cmd, unsigned long arg) |
|---|
| 827 | { |
|---|
| 828 | int ret = 0; |
|---|
| 829 | struct i2c_adapter *adap = client->adapter; |
|---|
| 830 | |
|---|
| 831 | DEB2(printk("i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg)); |
|---|
| 832 | switch ( cmd ) { |
|---|
| 833 | case I2C_RETRIES: |
|---|
| 834 | adap->retries = arg; |
|---|
| 835 | break; |
|---|
| 836 | case I2C_TIMEOUT: |
|---|
| 837 | adap->timeout = arg; |
|---|
| 838 | break; |
|---|
| 839 | default: |
|---|
| 840 | if (adap->algo->algo_control!=NULL) |
|---|
| 841 | ret = adap->algo->algo_control(adap,cmd,arg); |
|---|
| 842 | } |
|---|
| 843 | return ret; |
|---|
| 844 | } |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | int i2c_probe(struct i2c_client *client, int low_addr, int hi_addr) |
|---|
| 848 | { |
|---|
| 849 | int i; |
|---|
| 850 | struct i2c_msg msg; |
|---|
| 851 | if (client->adapter->algo->master_xfer) { |
|---|
| 852 | msg.flags=client->flags & (I2C_M_TENMASK | I2C_M_TEN ); |
|---|
| 853 | msg.buf = NULL; |
|---|
| 854 | msg.len = 0; |
|---|
| 855 | I2C_LOCK(client->adapter); |
|---|
| 856 | for (i = low_addr; i <= hi_addr; i++) { |
|---|
| 857 | msg.addr=i; |
|---|
| 858 | /* TODO: implement a control statement in the algo layer |
|---|
| 859 | * that does address lookup only. |
|---|
| 860 | */ |
|---|
| 861 | if (1 == client->adapter-> |
|---|
| 862 | algo->master_xfer(client->adapter,&msg,1)) |
|---|
| 863 | break; |
|---|
| 864 | } |
|---|
| 865 | I2C_UNLOCK(client->adapter); |
|---|
| 866 | return (i <= hi_addr) ? (client->addr=i) : -1; |
|---|
| 867 | } else { |
|---|
| 868 | printk("I2C adapter %04x: I2C level transfers not supported\n", |
|---|
| 869 | client->adapter->id); |
|---|
| 870 | return 0; |
|---|
| 871 | } |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | /* +++ frodo |
|---|
| 875 | * return id number for a specific adapter |
|---|
| 876 | */ |
|---|
| 877 | int i2c_adapter_id(struct i2c_adapter *adap) |
|---|
| 878 | { |
|---|
| 879 | int i; |
|---|
| 880 | for (i = 0; i < I2C_ADAP_MAX; i++) |
|---|
| 881 | if (adap == adapters[i]) |
|---|
| 882 | return i; |
|---|
| 883 | return -1; |
|---|
| 884 | } |
|---|
| 885 | |
|---|
| 886 | /* The SMBus parts */ |
|---|
| 887 | |
|---|
| 888 | extern s32 i2c_smbus_write_quick(struct i2c_adapter * adapter, u8 addr, |
|---|
| 889 | u8 value) |
|---|
| 890 | { |
|---|
| 891 | return i2c_smbus_xfer(adapter,addr,value,0,I2C_SMBUS_QUICK,NULL); |
|---|
| 892 | } |
|---|
| 893 | |
|---|
| 894 | extern s32 i2c_smbus_read_byte(struct i2c_adapter * adapter,u8 addr) |
|---|
| 895 | { |
|---|
| 896 | union i2c_smbus_data data; |
|---|
| 897 | if (i2c_smbus_xfer(adapter,addr,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, |
|---|
| 898 | &data)) |
|---|
| 899 | return -1; |
|---|
| 900 | else |
|---|
| 901 | return 0x0FF & data.byte; |
|---|
| 902 | } |
|---|
| 903 | |
|---|
| 904 | extern s32 i2c_smbus_write_byte(struct i2c_adapter * adapter, u8 addr, |
|---|
| 905 | u8 value) |
|---|
| 906 | { |
|---|
| 907 | return i2c_smbus_xfer(adapter,addr,I2C_SMBUS_WRITE,value, |
|---|
| 908 | I2C_SMBUS_BYTE,NULL); |
|---|
| 909 | } |
|---|
| 910 | |
|---|
| 911 | extern s32 i2c_smbus_read_byte_data(struct i2c_adapter * adapter, |
|---|
| 912 | u8 addr, u8 command) |
|---|
| 913 | { |
|---|
| 914 | union i2c_smbus_data data; |
|---|
| 915 | if (i2c_smbus_xfer(adapter,addr,I2C_SMBUS_READ,command, |
|---|
| 916 | I2C_SMBUS_BYTE_DATA,&data)) |
|---|
| 917 | return -1; |
|---|
| 918 | else |
|---|
| 919 | return 0x0FF & data.byte; |
|---|
| 920 | } |
|---|
| 921 | |
|---|
| 922 | extern s32 i2c_smbus_write_byte_data(struct i2c_adapter * adapter, |
|---|
| 923 | u8 addr, u8 command, u8 value) |
|---|
| 924 | { |
|---|
| 925 | union i2c_smbus_data data; |
|---|
| 926 | data.byte = value; |
|---|
| 927 | return i2c_smbus_xfer(adapter,addr,I2C_SMBUS_WRITE,command, |
|---|
| 928 | I2C_SMBUS_BYTE_DATA,&data); |
|---|
| 929 | } |
|---|
| 930 | |
|---|
| 931 | extern s32 i2c_smbus_read_word_data(struct i2c_adapter * adapter, |
|---|
| 932 | u8 addr, u8 command) |
|---|
| 933 | { |
|---|
| 934 | union i2c_smbus_data data; |
|---|
| 935 | if (i2c_smbus_xfer(adapter,addr,I2C_SMBUS_READ,command, |
|---|
| 936 | I2C_SMBUS_WORD_DATA, &data)) |
|---|
| 937 | return -1; |
|---|
| 938 | else |
|---|
| 939 | return 0x0FFFF & data.word; |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | extern s32 i2c_smbus_write_word_data(struct i2c_adapter * adapter, |
|---|
| 943 | u8 addr, u8 command, u16 value) |
|---|
| 944 | { |
|---|
| 945 | union i2c_smbus_data data; |
|---|
| 946 | data.word = value; |
|---|
| 947 | return i2c_smbus_xfer(adapter,addr,I2C_SMBUS_WRITE,command, |
|---|
| 948 | I2C_SMBUS_WORD_DATA,&data); |
|---|
| 949 | } |
|---|
| 950 | |
|---|
| 951 | extern s32 i2c_smbus_process_call(struct i2c_adapter * adapter, |
|---|
| 952 | u8 addr, u8 command, u16 value) |
|---|
| 953 | { |
|---|
| 954 | union i2c_smbus_data data; |
|---|
| 955 | data.word = value; |
|---|
| 956 | if (i2c_smbus_xfer(adapter,addr,I2C_SMBUS_WRITE,command, |
|---|
| 957 | I2C_SMBUS_PROC_CALL, &data)) |
|---|
| 958 | return -1; |
|---|
| 959 | else |
|---|
| 960 | return 0x0FFFF & data.word; |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | /* Returns the number of read bytes */ |
|---|
| 964 | extern s32 i2c_smbus_read_block_data(struct i2c_adapter * adapter, |
|---|
| 965 | u8 addr, u8 command, u8 *values) |
|---|
| 966 | { |
|---|
| 967 | union i2c_smbus_data data; |
|---|
| 968 | int i; |
|---|
| 969 | if (i2c_smbus_xfer(adapter,addr,I2C_SMBUS_READ,command, |
|---|
| 970 | I2C_SMBUS_BLOCK_DATA,&data)) |
|---|
| 971 | return -1; |
|---|
| 972 | else { |
|---|
| 973 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 974 | values[i-1] = data.block[i]; |
|---|
| 975 | return data.block[0]; |
|---|
| 976 | } |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | extern s32 i2c_smbus_write_block_data(struct i2c_adapter * adapter, |
|---|
| 980 | u8 addr, u8 command, u8 length, |
|---|
| 981 | u8 *values) |
|---|
| 982 | { |
|---|
| 983 | union i2c_smbus_data data; |
|---|
| 984 | int i; |
|---|
| 985 | if (length > 32) |
|---|
| 986 | length = 32; |
|---|
| 987 | for (i = 1; i <= length; i++) |
|---|
| 988 | data.block[i] = values[i-1]; |
|---|
| 989 | data.block[0] = length; |
|---|
| 990 | return i2c_smbus_xfer(adapter,addr,I2C_SMBUS_WRITE,command, |
|---|
| 991 | I2C_SMBUS_BLOCK_DATA,&data); |
|---|
| 992 | } |
|---|
| 993 | |
|---|
| 994 | /* Simulate a SMBus command using the i2c protocol |
|---|
| 995 | No checking of parameters is done! */ |
|---|
| 996 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u8 addr, |
|---|
| 997 | char read_write, u8 command, int size, |
|---|
| 998 | union i2c_smbus_data * data) |
|---|
| 999 | { |
|---|
| 1000 | /* So we need to generate a series of msgs. In the case of writing, we |
|---|
| 1001 | need to use only one message; when reading, we need two. We initialize |
|---|
| 1002 | most things with sane defaults, to keep the code below somewhat |
|---|
| 1003 | simpler. */ |
|---|
| 1004 | unsigned char msgbuf0[33]; |
|---|
| 1005 | unsigned char msgbuf1[33]; |
|---|
| 1006 | int num = read_write == I2C_SMBUS_READ?2:1; |
|---|
| 1007 | struct i2c_msg msg[2] = { { addr, 0, 1, msgbuf0 }, |
|---|
| 1008 | { addr, I2C_M_RD, 0, msgbuf1 } |
|---|
| 1009 | }; |
|---|
| 1010 | int i; |
|---|
| 1011 | |
|---|
| 1012 | msgbuf0[0] = command; |
|---|
| 1013 | switch(size) { |
|---|
| 1014 | case I2C_SMBUS_QUICK: |
|---|
| 1015 | msg[0].len = 0; |
|---|
| 1016 | num = 1; /* Special case: The read/write field is used |
|---|
| 1017 | as data */ |
|---|
| 1018 | break; |
|---|
| 1019 | case I2C_SMBUS_BYTE: |
|---|
| 1020 | if (read_write == I2C_SMBUS_READ) { |
|---|
| 1021 | /* Special case: only a read! */ |
|---|
| 1022 | msg[0].flags = I2C_M_RD; |
|---|
| 1023 | num = 1; |
|---|
| 1024 | } |
|---|
| 1025 | break; |
|---|
| 1026 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1027 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1028 | msg[1].len = 1; |
|---|
| 1029 | else { |
|---|
| 1030 | msg[0].len = 2; |
|---|
| 1031 | msgbuf0[1] = data->byte; |
|---|
| 1032 | } |
|---|
| 1033 | break; |
|---|
| 1034 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1035 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1036 | msg[1].len = 2; |
|---|
| 1037 | else { |
|---|
| 1038 | msg[0].len=3; |
|---|
| 1039 | msgbuf0[1] = data->word & 0xff; |
|---|
| 1040 | msgbuf0[2] = (data->word >> 8) & 0xff; |
|---|
| 1041 | } |
|---|
| 1042 | break; |
|---|
| 1043 | case I2C_SMBUS_PROC_CALL: |
|---|
| 1044 | num = 2; /* Special case */ |
|---|
| 1045 | msg[0].len = 3; |
|---|
| 1046 | msg[1].len = 2; |
|---|
| 1047 | msgbuf0[1] = data->word & 0xff; |
|---|
| 1048 | msgbuf0[2] = (data->word >> 8) & 0xff; |
|---|
| 1049 | break; |
|---|
| 1050 | case I2C_SMBUS_BLOCK_DATA: |
|---|
| 1051 | if (read_write == I2C_SMBUS_READ) { |
|---|
| 1052 | printk("smbus.o: Block read not supported under " |
|---|
| 1053 | "I2C emulation!\n"); |
|---|
| 1054 | return -1; |
|---|
| 1055 | } else { |
|---|
| 1056 | msg[1].len = data->block[0] + 1; |
|---|
| 1057 | if (msg[1].len > 32) { |
|---|
| 1058 | printk("smbus.o: smbus_access called with " |
|---|
| 1059 | "invalid block write size (%d)\n", |
|---|
| 1060 | msg[1].len); |
|---|
| 1061 | return -1; |
|---|
| 1062 | } |
|---|
| 1063 | for (i = 1; i <= msg[1].len; i++) |
|---|
| 1064 | msgbuf0[i] = data->block[i]; |
|---|
| 1065 | } |
|---|
| 1066 | break; |
|---|
| 1067 | default: |
|---|
| 1068 | printk("smbus.o: smbus_access called with invalid size (%d)\n", |
|---|
| 1069 | size); |
|---|
| 1070 | return -1; |
|---|
| 1071 | } |
|---|
| 1072 | |
|---|
| 1073 | if (i2c_transfer(adapter, msg, num) < 0) |
|---|
| 1074 | return -1; |
|---|
| 1075 | |
|---|
| 1076 | if (read_write == I2C_SMBUS_READ) |
|---|
| 1077 | switch(size) { |
|---|
| 1078 | case I2C_SMBUS_BYTE: |
|---|
| 1079 | data->byte = msgbuf0[0]; |
|---|
| 1080 | break; |
|---|
| 1081 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 1082 | data->byte = msgbuf1[0]; |
|---|
| 1083 | break; |
|---|
| 1084 | case I2C_SMBUS_WORD_DATA: |
|---|
| 1085 | case I2C_SMBUS_PROC_CALL: |
|---|
| 1086 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
|---|
| 1087 | break; |
|---|
| 1088 | } |
|---|
| 1089 | return 0; |
|---|
| 1090 | } |
|---|
| 1091 | |
|---|
| 1092 | |
|---|
| 1093 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u8 addr, char read_write, |
|---|
| 1094 | u8 command, int size, union i2c_smbus_data * data) |
|---|
| 1095 | { |
|---|
| 1096 | s32 res; |
|---|
| 1097 | if (adapter->algo->smbus_xfer) { |
|---|
| 1098 | down(&adapter->lock); |
|---|
| 1099 | res = adapter->algo->smbus_xfer(adapter,addr,read_write, |
|---|
| 1100 | command,size,data); |
|---|
| 1101 | up(&adapter->lock); |
|---|
| 1102 | } else |
|---|
| 1103 | res = i2c_smbus_xfer_emulated(adapter,addr,read_write, |
|---|
| 1104 | command,size,data); |
|---|
| 1105 | return res; |
|---|
| 1106 | } |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | int __init i2c_init(void) |
|---|
| 1110 | { |
|---|
| 1111 | /* clear algorithms */ |
|---|
| 1112 | memset(algorithms,0,sizeof(algorithms)); |
|---|
| 1113 | memset(adapters,0,sizeof(adapters)); |
|---|
| 1114 | memset(drivers,0,sizeof(drivers)); |
|---|
| 1115 | algo_count=0; |
|---|
| 1116 | adap_count=0; |
|---|
| 1117 | driver_count=0; |
|---|
| 1118 | |
|---|
| 1119 | i2cproc_init(); |
|---|
| 1120 | |
|---|
| 1121 | printk(KERN_INFO "i2c module initialized.\n"); |
|---|
| 1122 | return 0; |
|---|
| 1123 | } |
|---|
| 1124 | |
|---|
| 1125 | #ifndef MODULE |
|---|
| 1126 | #ifdef CONFIG_I2C_CHARDEV |
|---|
| 1127 | extern int i2cdev_init(void); |
|---|
| 1128 | #endif |
|---|
| 1129 | #ifdef CONFIG_I2C_ALGOBIT |
|---|
| 1130 | extern int algo_bit_init(void); |
|---|
| 1131 | #endif |
|---|
| 1132 | #ifdef CONFIG_I2C_BITLP |
|---|
| 1133 | extern int bitlp_init(void); |
|---|
| 1134 | #endif |
|---|
| 1135 | #ifdef CONFIG_I2C_BITELV |
|---|
| 1136 | extern int bitelv_init(void); |
|---|
| 1137 | #endif |
|---|
| 1138 | #ifdef CONFIG_I2C_BITVELLE |
|---|
| 1139 | extern int bitvelle_init(void); |
|---|
| 1140 | #endif |
|---|
| 1141 | #ifdef CONFIG_I2C_BITVIA |
|---|
| 1142 | extern int bitvia_init(void); |
|---|
| 1143 | #endif |
|---|
| 1144 | |
|---|
| 1145 | #ifdef CONFIG_I2C_ALGOPCF |
|---|
| 1146 | extern int algo_pcf_init(void); |
|---|
| 1147 | #endif |
|---|
| 1148 | #ifdef CONFIG_I2C_PCFISA |
|---|
| 1149 | extern int pcfisa_init(void); |
|---|
| 1150 | #endif |
|---|
| 1151 | |
|---|
| 1152 | |
|---|
| 1153 | int simons_i2c_init(void) |
|---|
| 1154 | { |
|---|
| 1155 | /* --------------------- global ----- */ |
|---|
| 1156 | i2c_init(); |
|---|
| 1157 | |
|---|
| 1158 | #ifdef CONFIG_I2C_CHARDEV |
|---|
| 1159 | i2cdev_init(); |
|---|
| 1160 | #endif |
|---|
| 1161 | /* --------------------- bit -------- */ |
|---|
| 1162 | #ifdef CONFIG_I2C_ALGOBIT |
|---|
| 1163 | algo_bit_init(); |
|---|
| 1164 | #endif |
|---|
| 1165 | #ifdef CONFIG_I2C_BITLP |
|---|
| 1166 | bitlp_init(); |
|---|
| 1167 | #endif |
|---|
| 1168 | #ifdef CONFIG_I2C_BITELV |
|---|
| 1169 | bitelv_init(); |
|---|
| 1170 | #endif |
|---|
| 1171 | #ifdef CONFIG_I2C_BITVELLE |
|---|
| 1172 | bitvelle_init(); |
|---|
| 1173 | #endif |
|---|
| 1174 | #ifdef CONFIG_I2C_BITVIA |
|---|
| 1175 | bitvia_init(); |
|---|
| 1176 | #endif |
|---|
| 1177 | |
|---|
| 1178 | /* --------------------- pcf -------- */ |
|---|
| 1179 | #ifdef CONFIG_I2C_ALGOPCF |
|---|
| 1180 | algo_pcf_init(); |
|---|
| 1181 | #endif |
|---|
| 1182 | #ifdef CONFIG_I2C_PCFISA |
|---|
| 1183 | pcfisa_init(); |
|---|
| 1184 | #endif |
|---|
| 1185 | return 0; |
|---|
| 1186 | } |
|---|
| 1187 | |
|---|
| 1188 | #endif |
|---|
| 1189 | |
|---|
| 1190 | |
|---|
| 1191 | #ifdef MODULE |
|---|
| 1192 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
|---|
| 1193 | MODULE_DESCRIPTION("I2C-Bus main module"); |
|---|
| 1194 | MODULE_PARM(i2c_debug, "i"); |
|---|
| 1195 | MODULE_PARM_DESC(i2c_debug,"debug level"); |
|---|
| 1196 | |
|---|
| 1197 | EXPORT_SYMBOL(i2c_add_algorithm); |
|---|
| 1198 | EXPORT_SYMBOL(i2c_del_algorithm); |
|---|
| 1199 | EXPORT_SYMBOL(i2c_add_adapter); |
|---|
| 1200 | EXPORT_SYMBOL(i2c_del_adapter); |
|---|
| 1201 | EXPORT_SYMBOL(i2c_add_driver); |
|---|
| 1202 | EXPORT_SYMBOL(i2c_del_driver); |
|---|
| 1203 | EXPORT_SYMBOL(i2c_attach_client); |
|---|
| 1204 | EXPORT_SYMBOL(i2c_detach_client); |
|---|
| 1205 | EXPORT_SYMBOL(i2c_inc_use_client); |
|---|
| 1206 | EXPORT_SYMBOL(i2c_dec_use_client); |
|---|
| 1207 | |
|---|
| 1208 | |
|---|
| 1209 | EXPORT_SYMBOL(i2c_master_send); |
|---|
| 1210 | EXPORT_SYMBOL(i2c_master_recv); |
|---|
| 1211 | EXPORT_SYMBOL(i2c_master_send_subadress); |
|---|
| 1212 | EXPORT_SYMBOL(i2c_master_recv_subadress); |
|---|
| 1213 | EXPORT_SYMBOL(i2c_control); |
|---|
| 1214 | EXPORT_SYMBOL(i2c_transfer); |
|---|
| 1215 | EXPORT_SYMBOL(i2c_adapter_id); |
|---|
| 1216 | |
|---|
| 1217 | EXPORT_SYMBOL(i2c_smbus_xfer); |
|---|
| 1218 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
|---|
| 1219 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
|---|
| 1220 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
|---|
| 1221 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
|---|
| 1222 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
|---|
| 1223 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
|---|
| 1224 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
|---|
| 1225 | EXPORT_SYMBOL(i2c_smbus_process_call); |
|---|
| 1226 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
|---|
| 1227 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
|---|
| 1228 | |
|---|
| 1229 | int init_module(void) |
|---|
| 1230 | { |
|---|
| 1231 | return i2c_init(); |
|---|
| 1232 | } |
|---|
| 1233 | |
|---|
| 1234 | void cleanup_module(void) |
|---|
| 1235 | { |
|---|
| 1236 | i2cproc_cleanup(); |
|---|
| 1237 | } |
|---|
| 1238 | #endif |
|---|