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

Revision 3453, 32.1 KB (checked in by frodo, 13 years ago)

Sync with kernel 2.3.48

Use proc_entry->proc_fops instead of proc_entry->ops for kernels >= 2.3.48

  • 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        return 0;
383}
384
385
386int i2c_detach_client(struct i2c_client *client)
387{
388        struct i2c_adapter *adapter = client->adapter;
389        int i;
390
391        for (i = 0; i < I2C_CLIENT_MAX; i++)
392                if (client == adapter->clients[i])
393                        break;
394        if (I2C_CLIENT_MAX == i) {
395                printk(KERN_WARNING " i2c-core.o: unregister_client [%s] not found\n",
396                        client->name);
397                return -ENODEV;
398        }
399
400        if (adapter->client_unregister != NULL) 
401                adapter->client_unregister(client);
402        /*      client->driver->detach_client(client);*/
403
404        adapter->clients[i] = NULL;
405        adapter->client_count--;
406        i2c_dummy_client(client);
407
408        DEB(printk("i2c-core.o: client [%s] unregistered.\n",client->name));
409        return 0;
410}
411
412void i2c_inc_use_client(struct i2c_client *client)
413{
414
415        if (client->driver->inc_use != NULL)
416                client->driver->inc_use(client);
417
418        if (client->adapter->inc_use != NULL)
419                client->adapter->inc_use(client->adapter);
420}
421
422void i2c_dec_use_client(struct i2c_client *client)
423{
424
425        if (client->driver->dec_use != NULL)
426                client->driver->dec_use(client);
427
428        if (client->adapter->dec_use != NULL)
429                client->adapter->dec_use(client->adapter);
430}
431
432/* ----------------------------------------------------
433 * The /proc functions
434 * ----------------------------------------------------
435 */
436
437#ifdef CONFIG_PROC_FS
438
439#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,27))
440/* Monitor access to /proc/bus/i2c*; make unloading i2c-proc impossible
441   if some process still uses it or some file in it */
442void monitor_bus_i2c(struct inode *inode, int fill)
443{
444        if (fill)
445                MOD_INC_USE_COUNT;
446        else
447                MOD_DEC_USE_COUNT;
448}
449#endif /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,37)) */
450
451/* This function generates the output for /proc/bus/i2c */
452int read_bus_i2c(char *buf, char **start, off_t offset, int len, int *eof, 
453                 void *private)
454{
455        int i;
456        int nr = 0;
457        /* Note that it is safe to write a `little' beyond len. Yes, really. */
458        for (i = 0; (i < I2C_ADAP_MAX) && (nr < len); i++)
459                if (adapters[i]) {
460                        nr += sprintf(buf+nr, "i2c-%d\t", i);
461                        if (adapters[i]->algo->smbus_xfer) {
462                                if (adapters[i]->algo->master_xfer)
463                                        nr += sprintf(buf+nr,"smbus/i2c");
464                                else
465                                        nr += sprintf(buf+nr,"smbus    ");
466                        } else if (adapters[i]->algo->master_xfer)
467                                nr += sprintf(buf+nr,"i2c       ");
468                        else
469                                nr += sprintf(buf+nr,"dummy     ");
470                        nr += sprintf(buf+nr,"\t%-32s\t%-32s\n",
471                                      adapters[i]->name,
472                                      adapters[i]->algo->name);
473                }
474        return nr;
475}
476
477/* This function generates the output for /proc/bus/i2c-? */
478ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count, 
479                         loff_t *ppos)
480{
481        struct inode * inode = file->f_dentry->d_inode;
482        char *kbuf;
483        struct i2c_client *client;
484        int i,j,k,order_nr,len=0,len_total;
485        int order[I2C_CLIENT_MAX];
486
487        if (count < 0)
488                return -EINVAL; 
489        len_total = file->f_pos + count;
490        /* Too bad if this gets longer (unlikely) */
491        if (len_total > 4000)
492                len_total = 4000;
493        for (i = 0; i < I2C_ADAP_MAX; i++)
494                if (adapters[i]->inode == inode->i_ino) {
495                /* We need a bit of slack in the kernel buffer; this makes the
496                   sprintf safe. */
497                        if (! (kbuf = kmalloc(count + 80,GFP_KERNEL)))
498                                return -ENOMEM;
499                        /* Order will hold the indexes of the clients
500                           sorted by address */
501                        order_nr=0;
502                        for (j = 0; j < I2C_CLIENT_MAX; j++) {
503                                if ((client = adapters[i]->clients[j]) && 
504                                    (client->driver->id != I2C_DRIVERID_I2CDEV))  {
505                                        for(k = order_nr; 
506                                            (k > 0) && 
507                                            adapters[i]->clients[order[k-1]]->
508                                                     addr > client->addr; 
509                                            k--)
510                                                order[k] = order[k-1];
511                                        order[k] = j;
512                                        order_nr++;
513                                }
514                        }
515
516
517                        for (j = 0; (j < order_nr) && (len < len_total); j++) {
518                                client = adapters[i]->clients[order[j]];
519                                len += sprintf(kbuf+len,"%02x\t%-32s\t%-32s\n",
520                                              client->addr,
521                                              client->name,
522                                              client->driver->name);
523                        }
524                        len = len - file->f_pos;
525                        if (len > count)
526                                len = count;
527                        if (len < 0) 
528                                len = 0;
529                        if (copy_to_user (buf,kbuf+file->f_pos, len)) {
530                                kfree(kbuf);
531                                return -EFAULT;
532                        }
533                        file->f_pos += len;
534                        kfree(kbuf);
535                        return len;
536                }
537        return -ENOENT;
538}
539
540int i2cproc_init(void)
541{
542
543        struct proc_dir_entry *proc_bus_i2c;
544
545        i2cproc_initialized = 0;
546
547        if (! proc_bus) {
548                printk("i2c-core.o: /proc/bus/ does not exist");
549                i2cproc_cleanup();
550                return -ENOENT;
551        } 
552        proc_bus_i2c = create_proc_entry("i2c",0,proc_bus);
553        if (!proc_bus_i2c) {
554                printk("i2c-core.o: Could not create /proc/bus/i2c");
555                i2cproc_cleanup();
556                return -ENOENT;
557        }
558        proc_bus_i2c->read_proc = &read_bus_i2c;
559#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27))
560        proc_bus_i2c->owner = THIS_MODULE;
561#else
562        proc_bus_i2c->fill_inode = &monitor_bus_i2c;
563#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27)) */
564        i2cproc_initialized += 2;
565        return 0;
566}
567
568int i2cproc_cleanup(void)
569{
570
571        if (i2cproc_initialized >= 1) {
572                remove_proc_entry("i2c",proc_bus);
573                i2cproc_initialized -= 2;
574        }
575        return 0;
576}
577
578
579#endif /* def CONFIG_PROC_FS */
580
581/* ---------------------------------------------------
582 * dummy driver notification
583 * ---------------------------------------------------
584 */
585
586static void i2c_dummy_adapter(struct i2c_adapter *adap)
587{
588        int i; 
589        for (i=0; i<I2C_DRIVER_MAX; i++)
590                if (drivers[i] && (drivers[i]->flags & I2C_DF_DUMMY))
591                    drivers[i]->attach_adapter(adap);
592}
593
594static void i2c_dummy_client(struct i2c_client *client)
595{
596        int i;
597        for (i=0; i<I2C_DRIVER_MAX; i++)
598                if (drivers[i] && (drivers[i]->flags & I2C_DF_DUMMY))
599                    drivers[i]->detach_client(client);
600}
601
602
603/* ----------------------------------------------------
604 * the functional interface to the i2c busses.
605 * ----------------------------------------------------
606 */
607
608int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num)
609{
610        int ret;
611
612        if (adap->algo->master_xfer) {
613                DEB2(printk("i2c-core.o: master_xfer: %s with %d msgs.\n",
614                            adap->name,num));
615
616                I2C_LOCK(adap);
617                ret = adap->algo->master_xfer(adap,msgs,num);
618                I2C_UNLOCK(adap);
619
620                return ret;
621        } else {
622                printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
623                       adap->id);
624                return -ENOSYS;
625        }
626}
627
628int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
629{
630        int ret;
631        struct i2c_adapter *adap=client->adapter;
632        struct i2c_msg msg;
633
634        if (client->adapter->algo->master_xfer) {
635                msg.addr   = client->addr;
636                msg.flags = client->flags & I2C_M_TEN;
637                msg.len = count;
638                (const char *)msg.buf = buf;
639       
640                DEB2(printk("i2c-core.o: master_send: writing %d bytes on %s.\n",
641                        count,client->adapter->name));
642       
643                I2C_LOCK(adap);
644                ret = adap->algo->master_xfer(adap,&msg,1);
645                I2C_UNLOCK(adap);
646
647                /* if everything went ok (i.e. 1 msg transmitted), return #bytes
648                 * transmitted, else error code.
649                 */
650                return (ret == 1 )? count : ret;
651        } else {
652                printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
653                       client->adapter->id);
654                return -ENOSYS;
655        }
656}
657
658int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
659{
660        struct i2c_adapter *adap=client->adapter;
661        struct i2c_msg msg;
662        int ret;
663        if (client->adapter->algo->master_xfer) {
664                msg.addr   = client->addr;
665                msg.flags = client->flags & I2C_M_TEN;
666                msg.flags |= I2C_M_RD;
667                msg.len = count;
668                msg.buf = buf;
669
670                DEB2(printk("i2c-core.o: master_recv: reading %d bytes on %s.\n",
671                        count,client->adapter->name));
672       
673                I2C_LOCK(adap);
674                ret = adap->algo->master_xfer(adap,&msg,1);
675                I2C_UNLOCK(adap);
676       
677                DEB2(printk("i2c-core.o: master_recv: return:%d (count:%d, addr:0x%02x)\n",
678                        ret, count, client->addr));
679       
680                /* if everything went ok (i.e. 1 msg transmitted), return #bytes
681                * transmitted, else error code.
682                */
683                return (ret == 1 )? count : ret;
684        } else {
685                printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
686                       client->adapter->id);
687                return -ENOSYS;
688        }
689}
690
691
692int i2c_control(struct i2c_client *client,
693        unsigned int cmd, unsigned long arg)
694{
695        int ret = 0;
696        struct i2c_adapter *adap = client->adapter;
697
698        DEB2(printk("i2c-core.o: i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg));
699        switch ( cmd ) {
700                case I2C_RETRIES:
701                        adap->retries = arg;
702                        break;
703                case I2C_TIMEOUT:
704                        adap->timeout = arg;
705                        break;
706                default:
707                        if (adap->algo->algo_control!=NULL)
708                                ret = adap->algo->algo_control(adap,cmd,arg);
709        }
710        return ret;
711}
712
713/* ----------------------------------------------------
714 * the i2c address scanning function
715 * Will not work for 10-bit addresses!
716 * ----------------------------------------------------
717 */
718int i2c_probe(struct i2c_adapter *adapter,
719                   struct i2c_client_address_data *address_data,
720                   i2c_client_found_addr_proc *found_proc)
721{
722        int addr,i,found,err;
723        int adap_id = i2c_adapter_id(adapter);
724
725        /* Forget it if we can't probe using SMBUS_QUICK */
726        if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
727                return -1;
728
729        for (addr = 0x00; addr <= 0x7f; addr++) {
730
731                /* Skip if already in use */
732                if (i2c_check_addr(adapter,addr))
733                        continue;
734
735                /* If it is in one of the force entries, we don't do any detection
736                   at all */
737                found = 0;
738
739                for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 3) {
740                        if (((adap_id == address_data->force[i]) || 
741                             (address_data->force[i] == ANY_I2C_BUS)) &&
742                             (addr == address_data->force[i+1])) {
743                                DEB2(printk("i2c-core.o: found force parameter for adapter %d, addr %04x\n",
744                                            adap_id,addr));
745                                if ((err = found_proc(adapter,addr,0,0)))
746                                        return err;
747                                found = 1;
748                        }
749                }
750                if (found) 
751                        continue;
752
753                /* If this address is in one of the ignores, we can forget about
754                   it right now */
755                for (i = 0;
756                     !found && (address_data->ignore[i] != I2C_CLIENT_END);
757                     i += 2) {
758                        if (((adap_id == address_data->ignore[i]) || 
759                            ((address_data->ignore[i] == ANY_I2C_BUS))) &&
760                            (addr == address_data->ignore[i+1])) {
761                                DEB2(printk("i2c-core.o: found ignore parameter for adapter %d, "
762                                     "addr %04x\n", adap_id ,addr));
763                                found = 1;
764                        }
765                }
766                for (i = 0;
767                     !found && (address_data->ignore_range[i] != I2C_CLIENT_END);
768                     i += 3) {
769                        if (((adap_id == address_data->ignore_range[i]) ||
770                            ((address_data->ignore_range[i]==ANY_I2C_BUS))) &&
771                            (addr >= address_data->ignore_range[i+1]) &&
772                            (addr <= address_data->ignore_range[i+2])) {
773                                DEB2(printk("i2c-core.o: found ignore_range parameter for adapter %d, "
774                                            "addr %04x\n", adap_id,addr));
775                                found = 1;
776                        }
777                }
778                if (found) 
779                        continue;
780
781                /* Now, we will do a detection, but only if it is in the normal or
782                   probe entries */ 
783                for (i = 0;
784                     !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
785                     i += 1) {
786                        if (addr == address_data->normal_i2c[i]) {
787                                found = 1;
788                                DEB2(printk("i2c-core.o: found normal i2c entry for adapter %d, "
789                                            "addr %02x", adap_id,addr));
790                        }
791                }
792
793                for (i = 0;
794                     !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END);
795                     i += 2) {
796                        if ((addr >= address_data->normal_i2c_range[i]) &&
797                            (addr <= address_data->normal_i2c_range[i+1])) {
798                                found = 1;
799                                DEB2(printk("i2c-core.o: found normal i2c_range entry for adapter %d, "
800                                            "addr %04x\n", adap_id,addr));
801                        }
802                }
803
804                for (i = 0;
805                     !found && (address_data->probe[i] != I2C_CLIENT_END);
806                     i += 2) {
807                        if (((adap_id == address_data->probe[i]) ||
808                            ((address_data->probe[i] == ANY_I2C_BUS))) &&
809                            (addr == address_data->probe[i+1])) {
810                                found = 1;
811                                DEB2(printk("i2c-core.o: found probe parameter for adapter %d, "
812                                            "addr %04x\n", adap_id,addr));
813                        }
814                }
815                for (i = 0;
816                     !found && (address_data->probe_range[i] != I2C_CLIENT_END);
817                     i += 3) {
818                        if (((adap_id == address_data->probe_range[i]) ||
819                           (address_data->probe_range[i] == ANY_I2C_BUS)) &&
820                           (addr >= address_data->probe_range[i+1]) &&
821                           (addr <= address_data->probe_range[i+2])) {
822                                found = 1;
823                                DEB2(printk("i2c-core.o: found probe_range parameter for adapter %d, "
824                                            "addr %04x\n", adap_id,addr));
825                        }
826                }
827                if (!found) 
828                        continue;
829
830                /* OK, so we really should examine this address. First check
831                   whether there is some client here at all! */
832                if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
833                        if ((err = found_proc(adapter,addr,0,-1)))
834                                return err;
835        }
836        return 0;
837}
838
839/*
840 * return id number for a specific adapter
841 */
842int i2c_adapter_id(struct i2c_adapter *adap)
843{
844        int i;
845        for (i = 0; i < I2C_ADAP_MAX; i++)
846                if (adap == adapters[i])
847                        return i;
848        return -1;
849}
850
851/* The SMBus parts */
852
853extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value)
854{
855        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
856                              value,0,I2C_SMBUS_QUICK,NULL);
857}
858
859extern s32 i2c_smbus_read_byte(struct i2c_client * client)
860{
861        union i2c_smbus_data data;
862        if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
863                           I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
864                return -1;
865        else
866                return 0x0FF & data.byte;
867}
868
869extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value)
870{
871        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
872                              I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,NULL);
873}
874
875extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command)
876{
877        union i2c_smbus_data data;
878        if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
879                           I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
880                return -1;
881        else
882                return 0x0FF & data.byte;
883}
884
885extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, u8 command,
886                                     u8 value)
887{
888        union i2c_smbus_data data;
889        data.byte = value;
890        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
891                              I2C_SMBUS_WRITE,command,
892                              I2C_SMBUS_BYTE_DATA,&data);
893}
894
895extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command)
896{
897        union i2c_smbus_data data;
898        if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
899                           I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
900                return -1;
901        else
902                return 0x0FFFF & data.word;
903}
904
905extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
906                                     u8 command, u16 value)
907{
908        union i2c_smbus_data data;
909        data.word = value;
910        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
911                              I2C_SMBUS_WRITE,command,
912                              I2C_SMBUS_WORD_DATA,&data);
913}
914
915extern s32 i2c_smbus_process_call(struct i2c_client * client,
916                                  u8 command, u16 value)
917{
918        union i2c_smbus_data data;
919        data.word = value;
920        if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
921                           I2C_SMBUS_WRITE,command,
922                           I2C_SMBUS_PROC_CALL, &data))
923                return -1;
924        else
925                return 0x0FFFF & data.word;
926}
927
928/* Returns the number of read bytes */
929extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
930                                     u8 command, u8 *values)
931{
932        union i2c_smbus_data data;
933        int i;
934        if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
935                           I2C_SMBUS_READ,command,
936                           I2C_SMBUS_BLOCK_DATA,&data))
937                return -1;
938        else {
939                for (i = 1; i <= data.block[0]; i++)
940                        values[i-1] = data.block[i];
941                return data.block[0];
942        }
943}
944
945extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
946                                      u8 command, u8 length, u8 *values)
947{
948        union i2c_smbus_data data;
949        int i;
950        if (length > 32)
951                length = 32;
952        for (i = 1; i <= length; i++)
953                data.block[i] = values[i-1];
954        data.block[0] = length;
955        return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
956                              I2C_SMBUS_WRITE,command,
957                              I2C_SMBUS_BLOCK_DATA,&data);
958}
959
960/* Simulate a SMBus command using the i2c protocol
961   No checking of parameters is done!  */
962static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, 
963                                   unsigned short flags,
964                                   char read_write, u8 command, int size, 
965                                   union i2c_smbus_data * data)
966{
967        /* So we need to generate a series of msgs. In the case of writing, we
968          need to use only one message; when reading, we need two. We initialize
969          most things with sane defaults, to keep the code below somewhat
970          simpler. */
971        unsigned char msgbuf0[33];
972        unsigned char msgbuf1[33];
973        int num = read_write == I2C_SMBUS_READ?2:1;
974        struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, 
975                                  { addr, flags | I2C_M_RD, 0, msgbuf1 }
976                                };
977        int i;
978
979        msgbuf0[0] = command;
980        switch(size) {
981        case I2C_SMBUS_QUICK:
982                msg[0].len = 0;
983                /* Special case: The read/write field is used as data */
984                msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
985                num = 1;
986                break;
987        case I2C_SMBUS_BYTE:
988                if (read_write == I2C_SMBUS_READ) {
989                        /* Special case: only a read! */
990                        msg[0].flags = I2C_M_RD | flags;
991                        num = 1;
992                }
993                break;
994        case I2C_SMBUS_BYTE_DATA:
995                if (read_write == I2C_SMBUS_READ)
996                        msg[1].len = 1;
997                else {
998                        msg[0].len = 2;
999                        msgbuf0[1] = data->byte;
1000                }
1001                break;
1002        case I2C_SMBUS_WORD_DATA:
1003                if (read_write == I2C_SMBUS_READ)
1004                        msg[1].len = 2;
1005                else {
1006                        msg[0].len=3;
1007                        msgbuf0[1] = data->word & 0xff;
1008                        msgbuf0[2] = (data->word >> 8) & 0xff;
1009                }
1010                break;
1011        case I2C_SMBUS_PROC_CALL:
1012                num = 2; /* Special case */
1013                msg[0].len = 3;
1014                msg[1].len = 2;
1015                msgbuf0[1] = data->word & 0xff;
1016                msgbuf0[2] = (data->word >> 8) & 0xff;
1017                break;
1018        case I2C_SMBUS_BLOCK_DATA:
1019                if (read_write == I2C_SMBUS_READ) {
1020                        printk("i2c-core.o: Block read not supported under "
1021                               "I2C emulation!\n");
1022                return -1;
1023                } else {
1024                        msg[1].len = data->block[0] + 1;
1025                        if (msg[1].len > 32) {
1026                                printk("i2c-core.o: smbus_access called with "
1027                                       "invalid block write size (%d)\n",
1028                                       msg[1].len);
1029                                return -1;
1030                        }
1031                        for (i = 1; i <= msg[1].len; i++)
1032                                msgbuf0[i] = data->block[i];
1033                }
1034                break;
1035        default:
1036                printk("i2c-core.o: smbus_access called with invalid size (%d)\n",
1037                       size);
1038                return -1;
1039        }
1040
1041        if (i2c_transfer(adapter, msg, num) < 0)
1042                return -1;
1043
1044        if (read_write == I2C_SMBUS_READ)
1045                switch(size) {
1046                        case I2C_SMBUS_BYTE:
1047                                data->byte = msgbuf0[0];
1048                                break;
1049                        case I2C_SMBUS_BYTE_DATA:
1050                                data->byte = msgbuf1[0];
1051                                break;
1052                        case I2C_SMBUS_WORD_DATA:
1053                        case I2C_SMBUS_PROC_CALL:
1054                                data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1055                                break;
1056                }
1057        return 0;
1058}
1059
1060
1061s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1062                   char read_write, u8 command, int size, 
1063                   union i2c_smbus_data * data)
1064{
1065        s32 res;
1066        flags = flags & I2C_M_TEN;
1067        if (adapter->algo->smbus_xfer) {
1068                I2C_LOCK(adapter);
1069                res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1070                                                command,size,data);
1071                I2C_UNLOCK(adapter);
1072        } else
1073                res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1074                                              command,size,data);
1075        return res;
1076}
1077
1078
1079/* You should always define `functionality'; the 'else' is just for
1080   backward compatibility. */ 
1081u32 i2c_get_functionality (struct i2c_adapter *adap)
1082{
1083        if (adap->algo->functionality)
1084                return adap->algo->functionality(adap);
1085        else
1086                return 0xffffffff;
1087}
1088
1089int i2c_check_functionality (struct i2c_adapter *adap, u32 func)
1090{
1091        u32 adap_func = i2c_get_functionality (adap);
1092        return (func & adap_func) == func;
1093}
1094
1095
1096static int __init i2c_init(void)
1097{
1098        printk("i2c-core.o: i2c core module\n");
1099        memset(adapters,0,sizeof(adapters));
1100        memset(drivers,0,sizeof(drivers));
1101        adap_count=0;
1102        driver_count=0;
1103
1104        init_MUTEX(&adap_lock);
1105        init_MUTEX(&driver_lock);
1106       
1107        i2cproc_init();
1108       
1109        return 0;
1110}
1111
1112#ifndef MODULE
1113#ifdef CONFIG_I2C_CHARDEV
1114        extern int i2c_dev_init(void);
1115#endif
1116#ifdef CONFIG_I2C_ALGOBIT
1117        extern int algo_bit_init(void);
1118#endif
1119#ifdef CONFIG_I2C_BITLP
1120        extern int bitlp_init(void);
1121#endif
1122#ifdef CONFIG_I2C_BITELV
1123        extern int bitelv_init(void);
1124#endif
1125#ifdef CONFIG_I2C_BITVELLE
1126        extern int bitvelle_init(void);
1127#endif
1128#ifdef CONFIG_I2C_BITVIA
1129        extern int bitvia_init(void);
1130#endif
1131
1132#ifdef CONFIG_I2C_ALGOPCF
1133        extern int algo_pcf_init(void); 
1134#endif
1135#ifdef CONFIG_I2C_PCFISA
1136        extern int pcfisa_init(void);
1137#endif
1138
1139/* This is needed for automatic patch generation: sensors code starts here */
1140/* This is needed for automatic patch generation: sensors code ends here   */
1141
1142int __init i2c_init_all(void)
1143{
1144        /* --------------------- global ----- */
1145        i2c_init();
1146
1147#ifdef CONFIG_I2C_CHARDEV
1148        i2c_dev_init();
1149#endif
1150        /* --------------------- bit -------- */
1151#ifdef CONFIG_I2C_ALGOBIT
1152        i2c_algo_bit_init();
1153#endif
1154#ifdef CONFIG_I2C_PHILIPSPAR
1155        i2c_bitlp_init();
1156#endif
1157#ifdef CONFIG_I2C_ELV
1158        i2c_bitelv_init();
1159#endif
1160#ifdef CONFIG_I2C_VELLEMAN
1161        i2c_bitvelle_init();
1162#endif
1163
1164        /* --------------------- pcf -------- */
1165#ifdef CONFIG_I2C_ALGOPCF
1166        i2c_algo_pcf_init();   
1167#endif
1168#ifdef CONFIG_I2C_ELEKTOR
1169        i2c_pcfisa_init();
1170#endif
1171/* This is needed for automatic patch generation: sensors code starts here */
1172/* This is needed for automatic patch generation: sensors code ends here */
1173
1174        return 0;
1175}
1176
1177#endif
1178
1179
1180
1181EXPORT_SYMBOL(i2c_add_adapter);
1182EXPORT_SYMBOL(i2c_del_adapter);
1183EXPORT_SYMBOL(i2c_add_driver);
1184EXPORT_SYMBOL(i2c_del_driver);
1185EXPORT_SYMBOL(i2c_attach_client);
1186EXPORT_SYMBOL(i2c_detach_client);
1187EXPORT_SYMBOL(i2c_inc_use_client);
1188EXPORT_SYMBOL(i2c_dec_use_client);
1189EXPORT_SYMBOL(i2c_check_addr);
1190
1191
1192EXPORT_SYMBOL(i2c_master_send);
1193EXPORT_SYMBOL(i2c_master_recv);
1194EXPORT_SYMBOL(i2c_control);
1195EXPORT_SYMBOL(i2c_transfer);
1196EXPORT_SYMBOL(i2c_adapter_id);
1197EXPORT_SYMBOL(i2c_probe);
1198
1199EXPORT_SYMBOL(i2c_smbus_xfer);
1200EXPORT_SYMBOL(i2c_smbus_write_quick);
1201EXPORT_SYMBOL(i2c_smbus_read_byte);
1202EXPORT_SYMBOL(i2c_smbus_write_byte);
1203EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1204EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1205EXPORT_SYMBOL(i2c_smbus_read_word_data);
1206EXPORT_SYMBOL(i2c_smbus_write_word_data);
1207EXPORT_SYMBOL(i2c_smbus_process_call);
1208EXPORT_SYMBOL(i2c_smbus_read_block_data);
1209EXPORT_SYMBOL(i2c_smbus_write_block_data);
1210
1211EXPORT_SYMBOL(i2c_get_functionality);
1212EXPORT_SYMBOL(i2c_check_functionality);
1213
1214#ifdef MODULE
1215MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1216MODULE_DESCRIPTION("I2C-Bus main module");
1217MODULE_PARM(i2c_debug, "i");
1218MODULE_PARM_DESC(i2c_debug,"debug level");
1219
1220int init_module(void) 
1221{
1222        return i2c_init();
1223}
1224
1225void cleanup_module(void) 
1226{
1227        i2cproc_cleanup();
1228}
1229#endif
Note: See TracBrowser for help on using the browser.