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

Revision 3469, 34.5 KB (checked in by simon, 13 years ago)

(simon) Merged Juha Valkama's code: get a client based on driver and adapter

id, and be able to lock/release it -> usage_count. See his mail for
a detailed description.

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