home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / I2C / i2c-core.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  39KB  |  1,425 lines

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