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