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

Revision 3991, 41.2 KB (checked in by khali, 8 years ago)

Five log messages lack their trailing new line.

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