Merge master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6] / drivers / i2c / i2c-core.c
1 /* i2c-core.c - a device driver for the iic-bus interface                    */
2 /* ------------------------------------------------------------------------- */
3 /*   Copyright (C) 1995-99 Simon G. Vogl
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
18 /* ------------------------------------------------------------------------- */
19
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21    All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22    SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23    Jean Delvare <khali@linux-fr.org> */
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <asm/uaccess.h>
35
36
37 static LIST_HEAD(adapters);
38 static LIST_HEAD(drivers);
39 static DECLARE_MUTEX(core_lists);
40 static DEFINE_IDR(i2c_adapter_idr);
41
42 /* match always succeeds, as we want the probe() to tell if we really accept this match */
43 static int i2c_device_match(struct device *dev, struct device_driver *drv)
44 {
45         return 1;
46 }
47
48 static int i2c_bus_suspend(struct device * dev, pm_message_t state)
49 {
50         int rc = 0;
51
52         if (dev->driver && dev->driver->suspend)
53                 rc = dev->driver->suspend(dev, state);
54         return rc;
55 }
56
57 static int i2c_bus_resume(struct device * dev)
58 {
59         int rc = 0;
60         
61         if (dev->driver && dev->driver->resume)
62                 rc = dev->driver->resume(dev);
63         return rc;
64 }
65
66 struct bus_type i2c_bus_type = {
67         .name =         "i2c",
68         .match =        i2c_device_match,
69         .suspend =      i2c_bus_suspend,
70         .resume =       i2c_bus_resume,
71 };
72
73 static int i2c_device_probe(struct device *dev)
74 {
75         return -ENODEV;
76 }
77
78 static int i2c_device_remove(struct device *dev)
79 {
80         return 0;
81 }
82
83 void i2c_adapter_dev_release(struct device *dev)
84 {
85         struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
86         complete(&adap->dev_released);
87 }
88
89 struct device_driver i2c_adapter_driver = {
90         .owner = THIS_MODULE,
91         .name = "i2c_adapter",
92         .bus = &i2c_bus_type,
93         .probe = i2c_device_probe,
94         .remove = i2c_device_remove,
95 };
96
97 static void i2c_adapter_class_dev_release(struct class_device *dev)
98 {
99         struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
100         complete(&adap->class_dev_released);
101 }
102
103 struct class i2c_adapter_class = {
104         .owner =        THIS_MODULE,
105         .name =         "i2c-adapter",
106         .release =      &i2c_adapter_class_dev_release,
107 };
108
109 static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
110 {
111         struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
112         return sprintf(buf, "%s\n", adap->name);
113 }
114 static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
115
116
117 static void i2c_client_release(struct device *dev)
118 {
119         struct i2c_client *client = to_i2c_client(dev);
120         complete(&client->released);
121 }
122
123 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
124 {
125         struct i2c_client *client = to_i2c_client(dev);
126         return sprintf(buf, "%s\n", client->name);
127 }
128
129 /* 
130  * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
131  * different type of a device.  So beware if the DEVICE_ATTR() macro ever
132  * changes, this definition will also have to change.
133  */
134 static struct device_attribute dev_attr_client_name = {
135         .attr   = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
136         .show   = &show_client_name,
137 };
138
139
140 /* ---------------------------------------------------
141  * registering functions 
142  * --------------------------------------------------- 
143  */
144
145 /* -----
146  * i2c_add_adapter is called from within the algorithm layer,
147  * when a new hw adapter registers. A new device is register to be
148  * available for clients.
149  */
150 int i2c_add_adapter(struct i2c_adapter *adap)
151 {
152         int id, res = 0;
153         struct list_head   *item;
154         struct i2c_driver  *driver;
155
156         down(&core_lists);
157
158         if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
159                 res = -ENOMEM;
160                 goto out_unlock;
161         }
162
163         res = idr_get_new(&i2c_adapter_idr, adap, &id);
164         if (res < 0) {
165                 if (res == -EAGAIN)
166                         res = -ENOMEM;
167                 goto out_unlock;
168         }
169
170         adap->nr =  id & MAX_ID_MASK;
171         init_MUTEX(&adap->bus_lock);
172         init_MUTEX(&adap->clist_lock);
173         list_add_tail(&adap->list,&adapters);
174         INIT_LIST_HEAD(&adap->clients);
175
176         /* Add the adapter to the driver core.
177          * If the parent pointer is not set up,
178          * we add this adapter to the host bus.
179          */
180         if (adap->dev.parent == NULL)
181                 adap->dev.parent = &platform_bus;
182         sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
183         adap->dev.driver = &i2c_adapter_driver;
184         adap->dev.release = &i2c_adapter_dev_release;
185         device_register(&adap->dev);
186         device_create_file(&adap->dev, &dev_attr_name);
187
188         /* Add this adapter to the i2c_adapter class */
189         memset(&adap->class_dev, 0x00, sizeof(struct class_device));
190         adap->class_dev.dev = &adap->dev;
191         adap->class_dev.class = &i2c_adapter_class;
192         strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
193         class_device_register(&adap->class_dev);
194
195         dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
196
197         /* inform drivers of new adapters */
198         list_for_each(item,&drivers) {
199                 driver = list_entry(item, struct i2c_driver, list);
200                 if (driver->attach_adapter)
201                         /* We ignore the return code; if it fails, too bad */
202                         driver->attach_adapter(adap);
203         }
204
205 out_unlock:
206         up(&core_lists);
207         return res;
208 }
209
210
211 int i2c_del_adapter(struct i2c_adapter *adap)
212 {
213         struct list_head  *item, *_n;
214         struct i2c_adapter *adap_from_list;
215         struct i2c_driver *driver;
216         struct i2c_client *client;
217         int res = 0;
218
219         down(&core_lists);
220
221         /* First make sure that this adapter was ever added */
222         list_for_each_entry(adap_from_list, &adapters, list) {
223                 if (adap_from_list == adap)
224                         break;
225         }
226         if (adap_from_list != adap) {
227                 pr_debug("i2c-core: attempting to delete unregistered "
228                          "adapter [%s]\n", adap->name);
229                 res = -EINVAL;
230                 goto out_unlock;
231         }
232
233         list_for_each(item,&drivers) {
234                 driver = list_entry(item, struct i2c_driver, list);
235                 if (driver->detach_adapter)
236                         if ((res = driver->detach_adapter(adap))) {
237                                 dev_err(&adap->dev, "detach_adapter failed "
238                                         "for driver [%s]\n",
239                                         driver->driver.name);
240                                 goto out_unlock;
241                         }
242         }
243
244         /* detach any active clients. This must be done first, because
245          * it can fail; in which case we give up. */
246         list_for_each_safe(item, _n, &adap->clients) {
247                 client = list_entry(item, struct i2c_client, list);
248
249                 if ((res=client->driver->detach_client(client))) {
250                         dev_err(&adap->dev, "detach_client failed for client "
251                                 "[%s] at address 0x%02x\n", client->name,
252                                 client->addr);
253                         goto out_unlock;
254                 }
255         }
256
257         /* clean up the sysfs representation */
258         init_completion(&adap->dev_released);
259         init_completion(&adap->class_dev_released);
260         class_device_unregister(&adap->class_dev);
261         device_remove_file(&adap->dev, &dev_attr_name);
262         device_unregister(&adap->dev);
263         list_del(&adap->list);
264
265         /* wait for sysfs to drop all references */
266         wait_for_completion(&adap->dev_released);
267         wait_for_completion(&adap->class_dev_released);
268
269         /* free dynamically allocated bus id */
270         idr_remove(&i2c_adapter_idr, adap->nr);
271
272         dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
273
274  out_unlock:
275         up(&core_lists);
276         return res;
277 }
278
279
280 /* -----
281  * What follows is the "upwards" interface: commands for talking to clients,
282  * which implement the functions to access the physical information of the
283  * chips.
284  */
285
286 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
287 {
288         struct list_head   *item;
289         struct i2c_adapter *adapter;
290         int res = 0;
291
292         down(&core_lists);
293
294         /* add the driver to the list of i2c drivers in the driver core */
295         driver->driver.owner = owner;
296         driver->driver.bus = &i2c_bus_type;
297         driver->driver.probe = i2c_device_probe;
298         driver->driver.remove = i2c_device_remove;
299
300         res = driver_register(&driver->driver);
301         if (res)
302                 goto out_unlock;
303         
304         list_add_tail(&driver->list,&drivers);
305         pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
306
307         /* now look for instances of driver on our adapters */
308         if (driver->attach_adapter) {
309                 list_for_each(item,&adapters) {
310                         adapter = list_entry(item, struct i2c_adapter, list);
311                         driver->attach_adapter(adapter);
312                 }
313         }
314
315  out_unlock:
316         up(&core_lists);
317         return res;
318 }
319 EXPORT_SYMBOL(i2c_register_driver);
320
321 int i2c_del_driver(struct i2c_driver *driver)
322 {
323         struct list_head   *item1, *item2, *_n;
324         struct i2c_client  *client;
325         struct i2c_adapter *adap;
326         
327         int res = 0;
328
329         down(&core_lists);
330
331         /* Have a look at each adapter, if clients of this driver are still
332          * attached. If so, detach them to be able to kill the driver 
333          * afterwards.
334          */
335         list_for_each(item1,&adapters) {
336                 adap = list_entry(item1, struct i2c_adapter, list);
337                 if (driver->detach_adapter) {
338                         if ((res = driver->detach_adapter(adap))) {
339                                 dev_err(&adap->dev, "detach_adapter failed "
340                                         "for driver [%s]\n",
341                                         driver->driver.name);
342                                 goto out_unlock;
343                         }
344                 } else {
345                         list_for_each_safe(item2, _n, &adap->clients) {
346                                 client = list_entry(item2, struct i2c_client, list);
347                                 if (client->driver != driver)
348                                         continue;
349                                 dev_dbg(&adap->dev, "detaching client [%s] "
350                                         "at 0x%02x\n", client->name,
351                                         client->addr);
352                                 if ((res = driver->detach_client(client))) {
353                                         dev_err(&adap->dev, "detach_client "
354                                                 "failed for client [%s] at "
355                                                 "0x%02x\n", client->name,
356                                                 client->addr);
357                                         goto out_unlock;
358                                 }
359                         }
360                 }
361         }
362
363         driver_unregister(&driver->driver);
364         list_del(&driver->list);
365         pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
366
367  out_unlock:
368         up(&core_lists);
369         return 0;
370 }
371
372 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
373 {
374         struct list_head   *item;
375         struct i2c_client  *client;
376
377         list_for_each(item,&adapter->clients) {
378                 client = list_entry(item, struct i2c_client, list);
379                 if (client->addr == addr)
380                         return -EBUSY;
381         }
382         return 0;
383 }
384
385 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
386 {
387         int rval;
388
389         down(&adapter->clist_lock);
390         rval = __i2c_check_addr(adapter, addr);
391         up(&adapter->clist_lock);
392
393         return rval;
394 }
395
396 int i2c_attach_client(struct i2c_client *client)
397 {
398         struct i2c_adapter *adapter = client->adapter;
399
400         down(&adapter->clist_lock);
401         if (__i2c_check_addr(client->adapter, client->addr)) {
402                 up(&adapter->clist_lock);
403                 return -EBUSY;
404         }
405         list_add_tail(&client->list,&adapter->clients);
406         up(&adapter->clist_lock);
407         
408         if (adapter->client_register)  {
409                 if (adapter->client_register(client))  {
410                         dev_dbg(&adapter->dev, "client_register "
411                                 "failed for client [%s] at 0x%02x\n",
412                                 client->name, client->addr);
413                 }
414         }
415
416         client->usage_count = 0;
417
418         client->dev.parent = &client->adapter->dev;
419         client->dev.driver = &client->driver->driver;
420         client->dev.bus = &i2c_bus_type;
421         client->dev.release = &i2c_client_release;
422         
423         snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
424                 "%d-%04x", i2c_adapter_id(adapter), client->addr);
425         dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
426                 client->name, client->dev.bus_id);
427         device_register(&client->dev);
428         device_create_file(&client->dev, &dev_attr_client_name);
429         
430         return 0;
431 }
432
433
434 int i2c_detach_client(struct i2c_client *client)
435 {
436         struct i2c_adapter *adapter = client->adapter;
437         int res = 0;
438         
439         if (client->usage_count > 0) {
440                 dev_warn(&client->dev, "Client [%s] still busy, "
441                          "can't detach\n", client->name);
442                 return -EBUSY;
443         }
444
445         if (adapter->client_unregister)  {
446                 res = adapter->client_unregister(client);
447                 if (res) {
448                         dev_err(&client->dev,
449                                 "client_unregister [%s] failed, "
450                                 "client not detached\n", client->name);
451                         goto out;
452                 }
453         }
454
455         down(&adapter->clist_lock);
456         list_del(&client->list);
457         init_completion(&client->released);
458         device_remove_file(&client->dev, &dev_attr_client_name);
459         device_unregister(&client->dev);
460         up(&adapter->clist_lock);
461         wait_for_completion(&client->released);
462
463  out:
464         return res;
465 }
466
467 static int i2c_inc_use_client(struct i2c_client *client)
468 {
469
470         if (!try_module_get(client->driver->driver.owner))
471                 return -ENODEV;
472         if (!try_module_get(client->adapter->owner)) {
473                 module_put(client->driver->driver.owner);
474                 return -ENODEV;
475         }
476
477         return 0;
478 }
479
480 static void i2c_dec_use_client(struct i2c_client *client)
481 {
482         module_put(client->driver->driver.owner);
483         module_put(client->adapter->owner);
484 }
485
486 int i2c_use_client(struct i2c_client *client)
487 {
488         int ret;
489
490         ret = i2c_inc_use_client(client);
491         if (ret)
492                 return ret;
493
494         client->usage_count++;
495
496         return 0;
497 }
498
499 int i2c_release_client(struct i2c_client *client)
500 {
501         if (!client->usage_count) {
502                 pr_debug("i2c-core: %s used one too many times\n",
503                          __FUNCTION__);
504                 return -EPERM;
505         }
506         
507         client->usage_count--;
508         i2c_dec_use_client(client);
509         
510         return 0;
511 }
512
513 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
514 {
515         struct list_head  *item;
516         struct i2c_client *client;
517
518         down(&adap->clist_lock);
519         list_for_each(item,&adap->clients) {
520                 client = list_entry(item, struct i2c_client, list);
521                 if (!try_module_get(client->driver->driver.owner))
522                         continue;
523                 if (NULL != client->driver->command) {
524                         up(&adap->clist_lock);
525                         client->driver->command(client,cmd,arg);
526                         down(&adap->clist_lock);
527                 }
528                 module_put(client->driver->driver.owner);
529        }
530        up(&adap->clist_lock);
531 }
532
533 static int __init i2c_init(void)
534 {
535         int retval;
536
537         retval = bus_register(&i2c_bus_type);
538         if (retval)
539                 return retval;
540         retval = driver_register(&i2c_adapter_driver);
541         if (retval)
542                 return retval;
543         return class_register(&i2c_adapter_class);
544 }
545
546 static void __exit i2c_exit(void)
547 {
548         class_unregister(&i2c_adapter_class);
549         driver_unregister(&i2c_adapter_driver);
550         bus_unregister(&i2c_bus_type);
551 }
552
553 subsys_initcall(i2c_init);
554 module_exit(i2c_exit);
555
556 /* ----------------------------------------------------
557  * the functional interface to the i2c busses.
558  * ----------------------------------------------------
559  */
560
561 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
562 {
563         int ret;
564
565         if (adap->algo->master_xfer) {
566 #ifdef DEBUG
567                 for (ret = 0; ret < num; ret++) {
568                         dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
569                                 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
570                                 'R' : 'W', msgs[ret].addr, msgs[ret].len);
571                 }
572 #endif
573
574                 down(&adap->bus_lock);
575                 ret = adap->algo->master_xfer(adap,msgs,num);
576                 up(&adap->bus_lock);
577
578                 return ret;
579         } else {
580                 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
581                 return -ENOSYS;
582         }
583 }
584
585 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
586 {
587         int ret;
588         struct i2c_adapter *adap=client->adapter;
589         struct i2c_msg msg;
590
591         msg.addr = client->addr;
592         msg.flags = client->flags & I2C_M_TEN;
593         msg.len = count;
594         msg.buf = (char *)buf;
595         
596         ret = i2c_transfer(adap, &msg, 1);
597
598         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
599            transmitted, else error code. */
600         return (ret == 1) ? count : ret;
601 }
602
603 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
604 {
605         struct i2c_adapter *adap=client->adapter;
606         struct i2c_msg msg;
607         int ret;
608
609         msg.addr = client->addr;
610         msg.flags = client->flags & I2C_M_TEN;
611         msg.flags |= I2C_M_RD;
612         msg.len = count;
613         msg.buf = buf;
614
615         ret = i2c_transfer(adap, &msg, 1);
616
617         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
618            transmitted, else error code. */
619         return (ret == 1) ? count : ret;
620 }
621
622
623 int i2c_control(struct i2c_client *client,
624         unsigned int cmd, unsigned long arg)
625 {
626         int ret = 0;
627         struct i2c_adapter *adap = client->adapter;
628
629         dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
630         switch (cmd) {
631                 case I2C_RETRIES:
632                         adap->retries = arg;
633                         break;
634                 case I2C_TIMEOUT:
635                         adap->timeout = arg;
636                         break;
637                 default:
638                         if (adap->algo->algo_control!=NULL)
639                                 ret = adap->algo->algo_control(adap,cmd,arg);
640         }
641         return ret;
642 }
643
644 /* ----------------------------------------------------
645  * the i2c address scanning function
646  * Will not work for 10-bit addresses!
647  * ----------------------------------------------------
648  */
649 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
650                              int (*found_proc) (struct i2c_adapter *, int, int))
651 {
652         int err;
653
654         /* Make sure the address is valid */
655         if (addr < 0x03 || addr > 0x77) {
656                 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
657                          addr);
658                 return -EINVAL;
659         }
660
661         /* Skip if already in use */
662         if (i2c_check_addr(adapter, addr))
663                 return 0;
664
665         /* Make sure there is something at this address, unless forced */
666         if (kind < 0) {
667                 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
668                                    I2C_SMBUS_QUICK, NULL) < 0)
669                         return 0;
670
671                 /* prevent 24RF08 corruption */
672                 if ((addr & ~0x0f) == 0x50)
673                         i2c_smbus_xfer(adapter, addr, 0, 0, 0,
674                                        I2C_SMBUS_QUICK, NULL);
675         }
676
677         /* Finally call the custom detection function */
678         err = found_proc(adapter, addr, kind);
679
680         /* -ENODEV can be returned if there is a chip at the given address
681            but it isn't supported by this chip driver. We catch it here as
682            this isn't an error. */
683         return (err == -ENODEV) ? 0 : err;
684 }
685
686 int i2c_probe(struct i2c_adapter *adapter,
687               struct i2c_client_address_data *address_data,
688               int (*found_proc) (struct i2c_adapter *, int, int))
689 {
690         int i, err;
691         int adap_id = i2c_adapter_id(adapter);
692
693         /* Force entries are done first, and are not affected by ignore
694            entries */
695         if (address_data->forces) {
696                 unsigned short **forces = address_data->forces;
697                 int kind;
698
699                 for (kind = 0; forces[kind]; kind++) {
700                         for (i = 0; forces[kind][i] != I2C_CLIENT_END;
701                              i += 2) {
702                                 if (forces[kind][i] == adap_id
703                                  || forces[kind][i] == ANY_I2C_BUS) {
704                                         dev_dbg(&adapter->dev, "found force "
705                                                 "parameter for adapter %d, "
706                                                 "addr 0x%02x, kind %d\n",
707                                                 adap_id, forces[kind][i + 1],
708                                                 kind);
709                                         err = i2c_probe_address(adapter,
710                                                 forces[kind][i + 1],
711                                                 kind, found_proc);
712                                         if (err)
713                                                 return err;
714                                 }
715                         }
716                 }
717         }
718
719         /* Stop here if we can't use SMBUS_QUICK */
720         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
721                 if (address_data->probe[0] == I2C_CLIENT_END
722                  && address_data->normal_i2c[0] == I2C_CLIENT_END)
723                         return 0;
724
725                 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
726                          "can't probe for chips\n");
727                 return -1;
728         }
729
730         /* Probe entries are done second, and are not affected by ignore
731            entries either */
732         for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
733                 if (address_data->probe[i] == adap_id
734                  || address_data->probe[i] == ANY_I2C_BUS) {
735                         dev_dbg(&adapter->dev, "found probe parameter for "
736                                 "adapter %d, addr 0x%02x\n", adap_id,
737                                 address_data->probe[i + 1]);
738                         err = i2c_probe_address(adapter,
739                                                 address_data->probe[i + 1],
740                                                 -1, found_proc);
741                         if (err)
742                                 return err;
743                 }
744         }
745
746         /* Normal entries are done last, unless shadowed by an ignore entry */
747         for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
748                 int j, ignore;
749
750                 ignore = 0;
751                 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
752                      j += 2) {
753                         if ((address_data->ignore[j] == adap_id ||
754                              address_data->ignore[j] == ANY_I2C_BUS)
755                          && address_data->ignore[j + 1]
756                             == address_data->normal_i2c[i]) {
757                                 dev_dbg(&adapter->dev, "found ignore "
758                                         "parameter for adapter %d, "
759                                         "addr 0x%02x\n", adap_id,
760                                         address_data->ignore[j + 1]);
761                         }
762                         ignore = 1;
763                         break;
764                 }
765                 if (ignore)
766                         continue;
767
768                 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
769                         "addr 0x%02x\n", adap_id,
770                         address_data->normal_i2c[i]);
771                 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
772                                         -1, found_proc);
773                 if (err)
774                         return err;
775         }
776
777         return 0;
778 }
779
780 struct i2c_adapter* i2c_get_adapter(int id)
781 {
782         struct i2c_adapter *adapter;
783         
784         down(&core_lists);
785         adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
786         if (adapter && !try_module_get(adapter->owner))
787                 adapter = NULL;
788
789         up(&core_lists);
790         return adapter;
791 }
792
793 void i2c_put_adapter(struct i2c_adapter *adap)
794 {
795         module_put(adap->owner);
796 }
797
798 /* The SMBus parts */
799
800 #define POLY    (0x1070U << 3) 
801 static u8
802 crc8(u16 data)
803 {
804         int i;
805   
806         for(i = 0; i < 8; i++) {
807                 if (data & 0x8000) 
808                         data = data ^ POLY;
809                 data = data << 1;
810         }
811         return (u8)(data >> 8);
812 }
813
814 /* Incremental CRC8 over count bytes in the array pointed to by p */
815 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
816 {
817         int i;
818
819         for(i = 0; i < count; i++)
820                 crc = crc8((crc ^ p[i]) << 8);
821         return crc;
822 }
823
824 /* Assume a 7-bit address, which is reasonable for SMBus */
825 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
826 {
827         /* The address will be sent first */
828         u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
829         pec = i2c_smbus_pec(pec, &addr, 1);
830
831         /* The data buffer follows */
832         return i2c_smbus_pec(pec, msg->buf, msg->len);
833 }
834
835 /* Used for write only transactions */
836 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
837 {
838         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
839         msg->len++;
840 }
841
842 /* Return <0 on CRC error
843    If there was a write before this read (most cases) we need to take the
844    partial CRC from the write part into account.
845    Note that this function does modify the message (we need to decrease the
846    message length to hide the CRC byte from the caller). */
847 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
848 {
849         u8 rpec = msg->buf[--msg->len];
850         cpec = i2c_smbus_msg_pec(cpec, msg);
851
852         if (rpec != cpec) {
853                 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
854                         rpec, cpec);
855                 return -1;
856         }
857         return 0;       
858 }
859
860 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
861 {
862         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
863                               value,0,I2C_SMBUS_QUICK,NULL);
864 }
865
866 s32 i2c_smbus_read_byte(struct i2c_client *client)
867 {
868         union i2c_smbus_data data;
869         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
870                            I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
871                 return -1;
872         else
873                 return 0x0FF & data.byte;
874 }
875
876 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
877 {
878         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
879                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
880 }
881
882 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
883 {
884         union i2c_smbus_data data;
885         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
886                            I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
887                 return -1;
888         else
889                 return 0x0FF & data.byte;
890 }
891
892 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
893 {
894         union i2c_smbus_data data;
895         data.byte = value;
896         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
897                               I2C_SMBUS_WRITE,command,
898                               I2C_SMBUS_BYTE_DATA,&data);
899 }
900
901 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
902 {
903         union i2c_smbus_data data;
904         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
905                            I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
906                 return -1;
907         else
908                 return 0x0FFFF & data.word;
909 }
910
911 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
912 {
913         union i2c_smbus_data data;
914         data.word = value;
915         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
916                               I2C_SMBUS_WRITE,command,
917                               I2C_SMBUS_WORD_DATA,&data);
918 }
919
920 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
921                                u8 length, u8 *values)
922 {
923         union i2c_smbus_data data;
924         int i;
925         if (length > I2C_SMBUS_BLOCK_MAX)
926                 length = I2C_SMBUS_BLOCK_MAX;
927         for (i = 1; i <= length; i++)
928                 data.block[i] = values[i-1];
929         data.block[0] = length;
930         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
931                               I2C_SMBUS_WRITE,command,
932                               I2C_SMBUS_BLOCK_DATA,&data);
933 }
934
935 /* Returns the number of read bytes */
936 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
937 {
938         union i2c_smbus_data data;
939         int i;
940         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
941                               I2C_SMBUS_READ,command,
942                               I2C_SMBUS_I2C_BLOCK_DATA,&data))
943                 return -1;
944         else {
945                 for (i = 1; i <= data.block[0]; i++)
946                         values[i-1] = data.block[i];
947                 return data.block[0];
948         }
949 }
950
951 /* Simulate a SMBus command using the i2c protocol 
952    No checking of parameters is done!  */
953 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, 
954                                    unsigned short flags,
955                                    char read_write, u8 command, int size, 
956                                    union i2c_smbus_data * data)
957 {
958         /* So we need to generate a series of msgs. In the case of writing, we
959           need to use only one message; when reading, we need two. We initialize
960           most things with sane defaults, to keep the code below somewhat
961           simpler. */
962         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
963         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
964         int num = read_write == I2C_SMBUS_READ?2:1;
965         struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, 
966                                   { addr, flags | I2C_M_RD, 0, msgbuf1 }
967                                 };
968         int i;
969         u8 partial_pec = 0;
970
971         msgbuf0[0] = command;
972         switch(size) {
973         case I2C_SMBUS_QUICK:
974                 msg[0].len = 0;
975                 /* Special case: The read/write field is used as data */
976                 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
977                 num = 1;
978                 break;
979         case I2C_SMBUS_BYTE:
980                 if (read_write == I2C_SMBUS_READ) {
981                         /* Special case: only a read! */
982                         msg[0].flags = I2C_M_RD | flags;
983                         num = 1;
984                 }
985                 break;
986         case I2C_SMBUS_BYTE_DATA:
987                 if (read_write == I2C_SMBUS_READ)
988                         msg[1].len = 1;
989                 else {
990                         msg[0].len = 2;
991                         msgbuf0[1] = data->byte;
992                 }
993                 break;
994         case I2C_SMBUS_WORD_DATA:
995                 if (read_write == I2C_SMBUS_READ)
996                         msg[1].len = 2;
997                 else {
998                         msg[0].len=3;
999                         msgbuf0[1] = data->word & 0xff;
1000                         msgbuf0[2] = (data->word >> 8) & 0xff;
1001                 }
1002                 break;
1003         case I2C_SMBUS_PROC_CALL:
1004                 num = 2; /* Special case */
1005                 read_write = I2C_SMBUS_READ;
1006                 msg[0].len = 3;
1007                 msg[1].len = 2;
1008                 msgbuf0[1] = data->word & 0xff;
1009                 msgbuf0[2] = (data->word >> 8) & 0xff;
1010                 break;
1011         case I2C_SMBUS_BLOCK_DATA:
1012                 if (read_write == I2C_SMBUS_READ) {
1013                         dev_err(&adapter->dev, "Block read not supported "
1014                                "under I2C emulation!\n");
1015                         return -1;
1016                 } else {
1017                         msg[0].len = data->block[0] + 2;
1018                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1019                                 dev_err(&adapter->dev, "smbus_access called with "
1020                                        "invalid block write size (%d)\n",
1021                                        data->block[0]);
1022                                 return -1;
1023                         }
1024                         for (i = 1; i < msg[0].len; i++)
1025                                 msgbuf0[i] = data->block[i-1];
1026                 }
1027                 break;
1028         case I2C_SMBUS_BLOCK_PROC_CALL:
1029                 dev_dbg(&adapter->dev, "Block process call not supported "
1030                        "under I2C emulation!\n");
1031                 return -1;
1032         case I2C_SMBUS_I2C_BLOCK_DATA:
1033                 if (read_write == I2C_SMBUS_READ) {
1034                         msg[1].len = I2C_SMBUS_BLOCK_MAX;
1035                 } else {
1036                         msg[0].len = data->block[0] + 1;
1037                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1038                                 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1039                                        "invalid block write size (%d)\n",
1040                                        data->block[0]);
1041                                 return -1;
1042                         }
1043                         for (i = 1; i <= data->block[0]; i++)
1044                                 msgbuf0[i] = data->block[i];
1045                 }
1046                 break;
1047         default:
1048                 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1049                        size);
1050                 return -1;
1051         }
1052
1053         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1054                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
1055         if (i) {
1056                 /* Compute PEC if first message is a write */
1057                 if (!(msg[0].flags & I2C_M_RD)) {
1058                         if (num == 1) /* Write only */
1059                                 i2c_smbus_add_pec(&msg[0]);
1060                         else /* Write followed by read */
1061                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1062                 }
1063                 /* Ask for PEC if last message is a read */
1064                 if (msg[num-1].flags & I2C_M_RD)
1065                         msg[num-1].len++;
1066         }
1067
1068         if (i2c_transfer(adapter, msg, num) < 0)
1069                 return -1;
1070
1071         /* Check PEC if last message is a read */
1072         if (i && (msg[num-1].flags & I2C_M_RD)) {
1073                 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1074                         return -1;
1075         }
1076
1077         if (read_write == I2C_SMBUS_READ)
1078                 switch(size) {
1079                         case I2C_SMBUS_BYTE:
1080                                 data->byte = msgbuf0[0];
1081                                 break;
1082                         case I2C_SMBUS_BYTE_DATA:
1083                                 data->byte = msgbuf1[0];
1084                                 break;
1085                         case I2C_SMBUS_WORD_DATA: 
1086                         case I2C_SMBUS_PROC_CALL:
1087                                 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1088                                 break;
1089                         case I2C_SMBUS_I2C_BLOCK_DATA:
1090                                 /* fixed at 32 for now */
1091                                 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1092                                 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1093                                         data->block[i+1] = msgbuf1[i];
1094                                 break;
1095                 }
1096         return 0;
1097 }
1098
1099
1100 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1101                    char read_write, u8 command, int size, 
1102                    union i2c_smbus_data * data)
1103 {
1104         s32 res;
1105
1106         flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1107
1108         if (adapter->algo->smbus_xfer) {
1109                 down(&adapter->bus_lock);
1110                 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1111                                                 command,size,data);
1112                 up(&adapter->bus_lock);
1113         } else
1114                 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1115                                               command,size,data);
1116
1117         return res;
1118 }
1119
1120
1121 /* Next four are needed by i2c-isa */
1122 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1123 EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1124 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1125 EXPORT_SYMBOL_GPL(i2c_bus_type);
1126
1127 EXPORT_SYMBOL(i2c_add_adapter);
1128 EXPORT_SYMBOL(i2c_del_adapter);
1129 EXPORT_SYMBOL(i2c_del_driver);
1130 EXPORT_SYMBOL(i2c_attach_client);
1131 EXPORT_SYMBOL(i2c_detach_client);
1132 EXPORT_SYMBOL(i2c_use_client);
1133 EXPORT_SYMBOL(i2c_release_client);
1134 EXPORT_SYMBOL(i2c_clients_command);
1135 EXPORT_SYMBOL(i2c_check_addr);
1136
1137 EXPORT_SYMBOL(i2c_master_send);
1138 EXPORT_SYMBOL(i2c_master_recv);
1139 EXPORT_SYMBOL(i2c_control);
1140 EXPORT_SYMBOL(i2c_transfer);
1141 EXPORT_SYMBOL(i2c_get_adapter);
1142 EXPORT_SYMBOL(i2c_put_adapter);
1143 EXPORT_SYMBOL(i2c_probe);
1144
1145 EXPORT_SYMBOL(i2c_smbus_xfer);
1146 EXPORT_SYMBOL(i2c_smbus_write_quick);
1147 EXPORT_SYMBOL(i2c_smbus_read_byte);
1148 EXPORT_SYMBOL(i2c_smbus_write_byte);
1149 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1150 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1151 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1152 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1153 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1154 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1155
1156 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1157 MODULE_DESCRIPTION("I2C-Bus main module");
1158 MODULE_LICENSE("GPL");