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