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