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