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

Revision 3878, 40.9 KB (checked in by mds, 9 years ago)

tweaks to emulation patch.

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