root/i2c/trunk/kernel/i2c-core.c @ 4014

Revision 4014, 39.1 KB (checked in by khali, 8 years ago)

Backport SMBus PEC support rewrite from Linux 2.6:

This is my rewrite of the SMBus PEC support. The original
implementation was known to have bugs (credits go to Hideki Iwamoto
for reporting many of them recently), and was incomplete due to a
conceptual limitation.

The rewrite affects only software PEC. Hardware PEC needs very little
code and is mostly untouched.

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