i2c: i2c stack can remove()
[linux-2.6/mini2440.git] / drivers / i2c / i2c-core.c
blob3aac1b5c7cbdf9d48886a95dbcbdb4f26d8d54b6
1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
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.
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.
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 /* ------------------------------------------------------------------------- */
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> */
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>
39 static LIST_HEAD(adapters);
40 static LIST_HEAD(drivers);
41 static DEFINE_MUTEX(core_lists);
42 static DEFINE_IDR(i2c_adapter_idr);
44 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
46 /* ------------------------------------------------------------------------- */
48 static int i2c_device_match(struct device *dev, struct device_driver *drv)
50 struct i2c_client *client = to_i2c_client(dev);
51 struct i2c_driver *driver = to_i2c_driver(drv);
53 /* make legacy i2c drivers bypass driver model probing entirely;
54 * such drivers scan each i2c adapter/bus themselves.
56 if (!is_newstyle_driver(driver))
57 return 0;
59 /* new style drivers use the same kind of driver matching policy
60 * as platform devices or SPI: compare device and driver IDs.
62 return strcmp(client->driver_name, drv->name) == 0;
65 #ifdef CONFIG_HOTPLUG
67 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
68 static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
69 char *buffer, int buffer_size)
71 struct i2c_client *client = to_i2c_client(dev);
72 int i = 0, length = 0;
74 /* by definition, legacy drivers can't hotplug */
75 if (dev->driver || !client->driver_name)
76 return 0;
78 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
79 "MODALIAS=%s", client->driver_name))
80 return -ENOMEM;
81 envp[i] = NULL;
82 dev_dbg(dev, "uevent\n");
83 return 0;
86 #else
87 #define i2c_device_uevent NULL
88 #endif /* CONFIG_HOTPLUG */
90 static int i2c_device_probe(struct device *dev)
92 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
95 if (!driver->probe)
96 return -ENODEV;
97 client->driver = driver;
98 dev_dbg(dev, "probe\n");
99 return driver->probe(client);
102 static int i2c_device_remove(struct device *dev)
104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver;
106 int status;
108 if (!dev->driver)
109 return 0;
111 driver = to_i2c_driver(dev->driver);
112 if (driver->remove) {
113 dev_dbg(dev, "remove\n");
114 status = driver->remove(client);
115 } else {
116 dev->driver = NULL;
117 status = 0;
119 if (status == 0)
120 client->driver = NULL;
121 return status;
124 static void i2c_device_shutdown(struct device *dev)
126 struct i2c_driver *driver;
128 if (!dev->driver)
129 return;
130 driver = to_i2c_driver(dev->driver);
131 if (driver->shutdown)
132 driver->shutdown(to_i2c_client(dev));
135 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
137 struct i2c_driver *driver;
139 if (!dev->driver)
140 return 0;
141 driver = to_i2c_driver(dev->driver);
142 if (!driver->suspend)
143 return 0;
144 return driver->suspend(to_i2c_client(dev), mesg);
147 static int i2c_device_resume(struct device * dev)
149 struct i2c_driver *driver;
151 if (!dev->driver)
152 return 0;
153 driver = to_i2c_driver(dev->driver);
154 if (!driver->resume)
155 return 0;
156 return driver->resume(to_i2c_client(dev));
159 static void i2c_client_release(struct device *dev)
161 struct i2c_client *client = to_i2c_client(dev);
162 complete(&client->released);
165 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
167 struct i2c_client *client = to_i2c_client(dev);
168 return sprintf(buf, "%s\n", client->name);
171 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
173 struct i2c_client *client = to_i2c_client(dev);
174 return client->driver_name
175 ? sprintf(buf, "%s\n", client->driver_name)
176 : 0;
179 static struct device_attribute i2c_dev_attrs[] = {
180 __ATTR(name, S_IRUGO, show_client_name, NULL),
181 /* modalias helps coldplug: modprobe $(cat .../modalias) */
182 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
183 { },
186 struct bus_type i2c_bus_type = {
187 .name = "i2c",
188 .dev_attrs = i2c_dev_attrs,
189 .match = i2c_device_match,
190 .uevent = i2c_device_uevent,
191 .probe = i2c_device_probe,
192 .remove = i2c_device_remove,
193 .shutdown = i2c_device_shutdown,
194 .suspend = i2c_device_suspend,
195 .resume = i2c_device_resume,
198 static void i2c_unregister_device(struct i2c_client *client)
200 struct i2c_adapter *adapter = client->adapter;
201 struct i2c_driver *driver = client->driver;
203 if (driver && !is_newstyle_driver(driver)) {
204 dev_err(&client->dev, "can't unregister devices "
205 "with legacy drivers\n");
206 WARN_ON(1);
207 return;
210 mutex_lock(&adapter->clist_lock);
211 list_del(&client->list);
212 mutex_unlock(&adapter->clist_lock);
214 device_unregister(&client->dev);
218 /* ------------------------------------------------------------------------- */
220 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
222 void i2c_adapter_dev_release(struct device *dev)
224 struct i2c_adapter *adap = to_i2c_adapter(dev);
225 complete(&adap->dev_released);
228 static ssize_t
229 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
231 struct i2c_adapter *adap = to_i2c_adapter(dev);
232 return sprintf(buf, "%s\n", adap->name);
235 static struct device_attribute i2c_adapter_attrs[] = {
236 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
237 { },
240 struct class i2c_adapter_class = {
241 .owner = THIS_MODULE,
242 .name = "i2c-adapter",
243 .dev_attrs = i2c_adapter_attrs,
247 /* -----
248 * i2c_add_adapter is called from within the algorithm layer,
249 * when a new hw adapter registers. A new device is register to be
250 * available for clients.
252 int i2c_add_adapter(struct i2c_adapter *adap)
254 int id, res = 0;
255 struct list_head *item;
256 struct i2c_driver *driver;
258 mutex_lock(&core_lists);
260 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
261 res = -ENOMEM;
262 goto out_unlock;
265 res = idr_get_new(&i2c_adapter_idr, adap, &id);
266 if (res < 0) {
267 if (res == -EAGAIN)
268 res = -ENOMEM;
269 goto out_unlock;
272 adap->nr = id & MAX_ID_MASK;
273 mutex_init(&adap->bus_lock);
274 mutex_init(&adap->clist_lock);
275 list_add_tail(&adap->list,&adapters);
276 INIT_LIST_HEAD(&adap->clients);
278 /* Add the adapter to the driver core.
279 * If the parent pointer is not set up,
280 * we add this adapter to the host bus.
282 if (adap->dev.parent == NULL) {
283 adap->dev.parent = &platform_bus;
284 pr_debug("I2C adapter driver [%s] forgot to specify "
285 "physical device\n", adap->name);
287 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
288 adap->dev.release = &i2c_adapter_dev_release;
289 adap->dev.class = &i2c_adapter_class;
290 res = device_register(&adap->dev);
291 if (res)
292 goto out_list;
294 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
296 /* let legacy drivers scan this bus for matching devices */
297 list_for_each(item,&drivers) {
298 driver = list_entry(item, struct i2c_driver, list);
299 if (driver->attach_adapter)
300 /* We ignore the return code; if it fails, too bad */
301 driver->attach_adapter(adap);
304 out_unlock:
305 mutex_unlock(&core_lists);
306 return res;
308 out_list:
309 list_del(&adap->list);
310 idr_remove(&i2c_adapter_idr, adap->nr);
311 goto out_unlock;
315 int i2c_del_adapter(struct i2c_adapter *adap)
317 struct list_head *item, *_n;
318 struct i2c_adapter *adap_from_list;
319 struct i2c_driver *driver;
320 struct i2c_client *client;
321 int res = 0;
323 mutex_lock(&core_lists);
325 /* First make sure that this adapter was ever added */
326 list_for_each_entry(adap_from_list, &adapters, list) {
327 if (adap_from_list == adap)
328 break;
330 if (adap_from_list != adap) {
331 pr_debug("i2c-core: attempting to delete unregistered "
332 "adapter [%s]\n", adap->name);
333 res = -EINVAL;
334 goto out_unlock;
337 list_for_each(item,&drivers) {
338 driver = list_entry(item, struct i2c_driver, list);
339 if (driver->detach_adapter)
340 if ((res = driver->detach_adapter(adap))) {
341 dev_err(&adap->dev, "detach_adapter failed "
342 "for driver [%s]\n",
343 driver->driver.name);
344 goto out_unlock;
348 /* detach any active clients. This must be done first, because
349 * it can fail; in which case we give up. */
350 list_for_each_safe(item, _n, &adap->clients) {
351 struct i2c_driver *driver;
353 client = list_entry(item, struct i2c_client, list);
354 driver = client->driver;
356 /* new style, follow standard driver model */
357 if (!driver || is_newstyle_driver(driver)) {
358 i2c_unregister_device(client);
359 continue;
362 /* legacy drivers create and remove clients themselves */
363 if ((res = driver->detach_client(client))) {
364 dev_err(&adap->dev, "detach_client failed for client "
365 "[%s] at address 0x%02x\n", client->name,
366 client->addr);
367 goto out_unlock;
371 /* clean up the sysfs representation */
372 init_completion(&adap->dev_released);
373 device_unregister(&adap->dev);
374 list_del(&adap->list);
376 /* wait for sysfs to drop all references */
377 wait_for_completion(&adap->dev_released);
379 /* free dynamically allocated bus id */
380 idr_remove(&i2c_adapter_idr, adap->nr);
382 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
384 out_unlock:
385 mutex_unlock(&core_lists);
386 return res;
390 /* ------------------------------------------------------------------------- */
393 * An i2c_driver is used with one or more i2c_client (device) nodes to access
394 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
395 * are two models for binding the driver to its device: "new style" drivers
396 * follow the standard Linux driver model and just respond to probe() calls
397 * issued if the driver core sees they match(); "legacy" drivers create device
398 * nodes themselves.
401 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
403 int res;
405 /* new style driver methods can't mix with legacy ones */
406 if (is_newstyle_driver(driver)) {
407 if (driver->attach_adapter || driver->detach_adapter
408 || driver->detach_client) {
409 printk(KERN_WARNING
410 "i2c-core: driver [%s] is confused\n",
411 driver->driver.name);
412 return -EINVAL;
416 /* add the driver to the list of i2c drivers in the driver core */
417 driver->driver.owner = owner;
418 driver->driver.bus = &i2c_bus_type;
420 res = driver_register(&driver->driver);
421 if (res)
422 return res;
424 mutex_lock(&core_lists);
426 list_add_tail(&driver->list,&drivers);
427 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
429 /* legacy drivers scan i2c busses directly */
430 if (driver->attach_adapter) {
431 struct i2c_adapter *adapter;
433 list_for_each_entry(adapter, &adapters, list) {
434 driver->attach_adapter(adapter);
438 mutex_unlock(&core_lists);
439 return 0;
441 EXPORT_SYMBOL(i2c_register_driver);
444 * i2c_del_driver - unregister I2C driver
445 * @driver: the driver being unregistered
447 int i2c_del_driver(struct i2c_driver *driver)
449 struct list_head *item1, *item2, *_n;
450 struct i2c_client *client;
451 struct i2c_adapter *adap;
453 int res = 0;
455 mutex_lock(&core_lists);
457 /* new-style driver? */
458 if (is_newstyle_driver(driver))
459 goto unregister;
461 /* Have a look at each adapter, if clients of this driver are still
462 * attached. If so, detach them to be able to kill the driver
463 * afterwards.
465 list_for_each(item1,&adapters) {
466 adap = list_entry(item1, struct i2c_adapter, list);
467 if (driver->detach_adapter) {
468 if ((res = driver->detach_adapter(adap))) {
469 dev_err(&adap->dev, "detach_adapter failed "
470 "for driver [%s]\n",
471 driver->driver.name);
472 goto out_unlock;
474 } else {
475 list_for_each_safe(item2, _n, &adap->clients) {
476 client = list_entry(item2, struct i2c_client, list);
477 if (client->driver != driver)
478 continue;
479 dev_dbg(&adap->dev, "detaching client [%s] "
480 "at 0x%02x\n", client->name,
481 client->addr);
482 if ((res = driver->detach_client(client))) {
483 dev_err(&adap->dev, "detach_client "
484 "failed for client [%s] at "
485 "0x%02x\n", client->name,
486 client->addr);
487 goto out_unlock;
493 unregister:
494 driver_unregister(&driver->driver);
495 list_del(&driver->list);
496 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
498 out_unlock:
499 mutex_unlock(&core_lists);
500 return 0;
503 /* ------------------------------------------------------------------------- */
505 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
507 struct list_head *item;
508 struct i2c_client *client;
510 list_for_each(item,&adapter->clients) {
511 client = list_entry(item, struct i2c_client, list);
512 if (client->addr == addr)
513 return -EBUSY;
515 return 0;
518 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
520 int rval;
522 mutex_lock(&adapter->clist_lock);
523 rval = __i2c_check_addr(adapter, addr);
524 mutex_unlock(&adapter->clist_lock);
526 return rval;
529 int i2c_attach_client(struct i2c_client *client)
531 struct i2c_adapter *adapter = client->adapter;
532 int res = 0;
534 mutex_lock(&adapter->clist_lock);
535 if (__i2c_check_addr(client->adapter, client->addr)) {
536 res = -EBUSY;
537 goto out_unlock;
539 list_add_tail(&client->list,&adapter->clients);
541 client->usage_count = 0;
543 client->dev.parent = &client->adapter->dev;
544 client->dev.driver = &client->driver->driver;
545 client->dev.bus = &i2c_bus_type;
546 client->dev.release = &i2c_client_release;
548 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
549 "%d-%04x", i2c_adapter_id(adapter), client->addr);
550 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
551 client->name, client->dev.bus_id);
552 res = device_register(&client->dev);
553 if (res)
554 goto out_list;
555 mutex_unlock(&adapter->clist_lock);
557 if (adapter->client_register) {
558 if (adapter->client_register(client)) {
559 dev_dbg(&adapter->dev, "client_register "
560 "failed for client [%s] at 0x%02x\n",
561 client->name, client->addr);
565 return 0;
567 out_list:
568 list_del(&client->list);
569 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
570 "(%d)\n", client->name, client->addr, res);
571 out_unlock:
572 mutex_unlock(&adapter->clist_lock);
573 return res;
577 int i2c_detach_client(struct i2c_client *client)
579 struct i2c_adapter *adapter = client->adapter;
580 int res = 0;
582 if (client->usage_count > 0) {
583 dev_warn(&client->dev, "Client [%s] still busy, "
584 "can't detach\n", client->name);
585 return -EBUSY;
588 if (adapter->client_unregister) {
589 res = adapter->client_unregister(client);
590 if (res) {
591 dev_err(&client->dev,
592 "client_unregister [%s] failed, "
593 "client not detached\n", client->name);
594 goto out;
598 mutex_lock(&adapter->clist_lock);
599 list_del(&client->list);
600 init_completion(&client->released);
601 device_unregister(&client->dev);
602 mutex_unlock(&adapter->clist_lock);
603 wait_for_completion(&client->released);
605 out:
606 return res;
609 static int i2c_inc_use_client(struct i2c_client *client)
612 if (!try_module_get(client->driver->driver.owner))
613 return -ENODEV;
614 if (!try_module_get(client->adapter->owner)) {
615 module_put(client->driver->driver.owner);
616 return -ENODEV;
619 return 0;
622 static void i2c_dec_use_client(struct i2c_client *client)
624 module_put(client->driver->driver.owner);
625 module_put(client->adapter->owner);
628 int i2c_use_client(struct i2c_client *client)
630 int ret;
632 ret = i2c_inc_use_client(client);
633 if (ret)
634 return ret;
636 client->usage_count++;
638 return 0;
641 int i2c_release_client(struct i2c_client *client)
643 if (!client->usage_count) {
644 pr_debug("i2c-core: %s used one too many times\n",
645 __FUNCTION__);
646 return -EPERM;
649 client->usage_count--;
650 i2c_dec_use_client(client);
652 return 0;
655 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
657 struct list_head *item;
658 struct i2c_client *client;
660 mutex_lock(&adap->clist_lock);
661 list_for_each(item,&adap->clients) {
662 client = list_entry(item, struct i2c_client, list);
663 if (!try_module_get(client->driver->driver.owner))
664 continue;
665 if (NULL != client->driver->command) {
666 mutex_unlock(&adap->clist_lock);
667 client->driver->command(client,cmd,arg);
668 mutex_lock(&adap->clist_lock);
670 module_put(client->driver->driver.owner);
672 mutex_unlock(&adap->clist_lock);
675 static int __init i2c_init(void)
677 int retval;
679 retval = bus_register(&i2c_bus_type);
680 if (retval)
681 return retval;
682 return class_register(&i2c_adapter_class);
685 static void __exit i2c_exit(void)
687 class_unregister(&i2c_adapter_class);
688 bus_unregister(&i2c_bus_type);
691 subsys_initcall(i2c_init);
692 module_exit(i2c_exit);
694 /* ----------------------------------------------------
695 * the functional interface to the i2c busses.
696 * ----------------------------------------------------
699 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
701 int ret;
703 if (adap->algo->master_xfer) {
704 #ifdef DEBUG
705 for (ret = 0; ret < num; ret++) {
706 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
707 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
708 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
709 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
711 #endif
713 mutex_lock_nested(&adap->bus_lock, adap->level);
714 ret = adap->algo->master_xfer(adap,msgs,num);
715 mutex_unlock(&adap->bus_lock);
717 return ret;
718 } else {
719 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
720 return -ENOSYS;
724 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
726 int ret;
727 struct i2c_adapter *adap=client->adapter;
728 struct i2c_msg msg;
730 msg.addr = client->addr;
731 msg.flags = client->flags & I2C_M_TEN;
732 msg.len = count;
733 msg.buf = (char *)buf;
735 ret = i2c_transfer(adap, &msg, 1);
737 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
738 transmitted, else error code. */
739 return (ret == 1) ? count : ret;
742 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
744 struct i2c_adapter *adap=client->adapter;
745 struct i2c_msg msg;
746 int ret;
748 msg.addr = client->addr;
749 msg.flags = client->flags & I2C_M_TEN;
750 msg.flags |= I2C_M_RD;
751 msg.len = count;
752 msg.buf = buf;
754 ret = i2c_transfer(adap, &msg, 1);
756 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
757 transmitted, else error code. */
758 return (ret == 1) ? count : ret;
762 int i2c_control(struct i2c_client *client,
763 unsigned int cmd, unsigned long arg)
765 int ret = 0;
766 struct i2c_adapter *adap = client->adapter;
768 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
769 switch (cmd) {
770 case I2C_RETRIES:
771 adap->retries = arg;
772 break;
773 case I2C_TIMEOUT:
774 adap->timeout = arg;
775 break;
776 default:
777 if (adap->algo->algo_control!=NULL)
778 ret = adap->algo->algo_control(adap,cmd,arg);
780 return ret;
783 /* ----------------------------------------------------
784 * the i2c address scanning function
785 * Will not work for 10-bit addresses!
786 * ----------------------------------------------------
788 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
789 int (*found_proc) (struct i2c_adapter *, int, int))
791 int err;
793 /* Make sure the address is valid */
794 if (addr < 0x03 || addr > 0x77) {
795 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
796 addr);
797 return -EINVAL;
800 /* Skip if already in use */
801 if (i2c_check_addr(adapter, addr))
802 return 0;
804 /* Make sure there is something at this address, unless forced */
805 if (kind < 0) {
806 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
807 I2C_SMBUS_QUICK, NULL) < 0)
808 return 0;
810 /* prevent 24RF08 corruption */
811 if ((addr & ~0x0f) == 0x50)
812 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
813 I2C_SMBUS_QUICK, NULL);
816 /* Finally call the custom detection function */
817 err = found_proc(adapter, addr, kind);
818 /* -ENODEV can be returned if there is a chip at the given address
819 but it isn't supported by this chip driver. We catch it here as
820 this isn't an error. */
821 if (err == -ENODEV)
822 err = 0;
824 if (err)
825 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
826 addr, err);
827 return err;
830 int i2c_probe(struct i2c_adapter *adapter,
831 struct i2c_client_address_data *address_data,
832 int (*found_proc) (struct i2c_adapter *, int, int))
834 int i, err;
835 int adap_id = i2c_adapter_id(adapter);
837 /* Force entries are done first, and are not affected by ignore
838 entries */
839 if (address_data->forces) {
840 unsigned short **forces = address_data->forces;
841 int kind;
843 for (kind = 0; forces[kind]; kind++) {
844 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
845 i += 2) {
846 if (forces[kind][i] == adap_id
847 || forces[kind][i] == ANY_I2C_BUS) {
848 dev_dbg(&adapter->dev, "found force "
849 "parameter for adapter %d, "
850 "addr 0x%02x, kind %d\n",
851 adap_id, forces[kind][i + 1],
852 kind);
853 err = i2c_probe_address(adapter,
854 forces[kind][i + 1],
855 kind, found_proc);
856 if (err)
857 return err;
863 /* Stop here if we can't use SMBUS_QUICK */
864 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
865 if (address_data->probe[0] == I2C_CLIENT_END
866 && address_data->normal_i2c[0] == I2C_CLIENT_END)
867 return 0;
869 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
870 "can't probe for chips\n");
871 return -1;
874 /* Probe entries are done second, and are not affected by ignore
875 entries either */
876 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
877 if (address_data->probe[i] == adap_id
878 || address_data->probe[i] == ANY_I2C_BUS) {
879 dev_dbg(&adapter->dev, "found probe parameter for "
880 "adapter %d, addr 0x%02x\n", adap_id,
881 address_data->probe[i + 1]);
882 err = i2c_probe_address(adapter,
883 address_data->probe[i + 1],
884 -1, found_proc);
885 if (err)
886 return err;
890 /* Normal entries are done last, unless shadowed by an ignore entry */
891 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
892 int j, ignore;
894 ignore = 0;
895 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
896 j += 2) {
897 if ((address_data->ignore[j] == adap_id ||
898 address_data->ignore[j] == ANY_I2C_BUS)
899 && address_data->ignore[j + 1]
900 == address_data->normal_i2c[i]) {
901 dev_dbg(&adapter->dev, "found ignore "
902 "parameter for adapter %d, "
903 "addr 0x%02x\n", adap_id,
904 address_data->ignore[j + 1]);
905 ignore = 1;
906 break;
909 if (ignore)
910 continue;
912 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
913 "addr 0x%02x\n", adap_id,
914 address_data->normal_i2c[i]);
915 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
916 -1, found_proc);
917 if (err)
918 return err;
921 return 0;
924 struct i2c_adapter* i2c_get_adapter(int id)
926 struct i2c_adapter *adapter;
928 mutex_lock(&core_lists);
929 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
930 if (adapter && !try_module_get(adapter->owner))
931 adapter = NULL;
933 mutex_unlock(&core_lists);
934 return adapter;
937 void i2c_put_adapter(struct i2c_adapter *adap)
939 module_put(adap->owner);
942 /* The SMBus parts */
944 #define POLY (0x1070U << 3)
945 static u8
946 crc8(u16 data)
948 int i;
950 for(i = 0; i < 8; i++) {
951 if (data & 0x8000)
952 data = data ^ POLY;
953 data = data << 1;
955 return (u8)(data >> 8);
958 /* Incremental CRC8 over count bytes in the array pointed to by p */
959 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
961 int i;
963 for(i = 0; i < count; i++)
964 crc = crc8((crc ^ p[i]) << 8);
965 return crc;
968 /* Assume a 7-bit address, which is reasonable for SMBus */
969 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
971 /* The address will be sent first */
972 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
973 pec = i2c_smbus_pec(pec, &addr, 1);
975 /* The data buffer follows */
976 return i2c_smbus_pec(pec, msg->buf, msg->len);
979 /* Used for write only transactions */
980 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
982 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
983 msg->len++;
986 /* Return <0 on CRC error
987 If there was a write before this read (most cases) we need to take the
988 partial CRC from the write part into account.
989 Note that this function does modify the message (we need to decrease the
990 message length to hide the CRC byte from the caller). */
991 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
993 u8 rpec = msg->buf[--msg->len];
994 cpec = i2c_smbus_msg_pec(cpec, msg);
996 if (rpec != cpec) {
997 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
998 rpec, cpec);
999 return -1;
1001 return 0;
1004 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1006 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1007 value,0,I2C_SMBUS_QUICK,NULL);
1010 s32 i2c_smbus_read_byte(struct i2c_client *client)
1012 union i2c_smbus_data data;
1013 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1014 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1015 return -1;
1016 else
1017 return data.byte;
1020 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1022 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1023 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1026 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1028 union i2c_smbus_data data;
1029 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1030 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1031 return -1;
1032 else
1033 return data.byte;
1036 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1038 union i2c_smbus_data data;
1039 data.byte = value;
1040 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1041 I2C_SMBUS_WRITE,command,
1042 I2C_SMBUS_BYTE_DATA,&data);
1045 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1047 union i2c_smbus_data data;
1048 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1049 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1050 return -1;
1051 else
1052 return data.word;
1055 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1057 union i2c_smbus_data data;
1058 data.word = value;
1059 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1060 I2C_SMBUS_WRITE,command,
1061 I2C_SMBUS_WORD_DATA,&data);
1064 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1065 u8 length, const u8 *values)
1067 union i2c_smbus_data data;
1069 if (length > I2C_SMBUS_BLOCK_MAX)
1070 length = I2C_SMBUS_BLOCK_MAX;
1071 data.block[0] = length;
1072 memcpy(&data.block[1], values, length);
1073 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1074 I2C_SMBUS_WRITE,command,
1075 I2C_SMBUS_BLOCK_DATA,&data);
1078 /* Returns the number of read bytes */
1079 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1081 union i2c_smbus_data data;
1083 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1084 I2C_SMBUS_READ,command,
1085 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1086 return -1;
1088 memcpy(values, &data.block[1], data.block[0]);
1089 return data.block[0];
1092 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1093 u8 length, const u8 *values)
1095 union i2c_smbus_data data;
1097 if (length > I2C_SMBUS_BLOCK_MAX)
1098 length = I2C_SMBUS_BLOCK_MAX;
1099 data.block[0] = length;
1100 memcpy(data.block + 1, values, length);
1101 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1102 I2C_SMBUS_WRITE, command,
1103 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1106 /* Simulate a SMBus command using the i2c protocol
1107 No checking of parameters is done! */
1108 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1109 unsigned short flags,
1110 char read_write, u8 command, int size,
1111 union i2c_smbus_data * data)
1113 /* So we need to generate a series of msgs. In the case of writing, we
1114 need to use only one message; when reading, we need two. We initialize
1115 most things with sane defaults, to keep the code below somewhat
1116 simpler. */
1117 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1118 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1119 int num = read_write == I2C_SMBUS_READ?2:1;
1120 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1121 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1123 int i;
1124 u8 partial_pec = 0;
1126 msgbuf0[0] = command;
1127 switch(size) {
1128 case I2C_SMBUS_QUICK:
1129 msg[0].len = 0;
1130 /* Special case: The read/write field is used as data */
1131 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1132 num = 1;
1133 break;
1134 case I2C_SMBUS_BYTE:
1135 if (read_write == I2C_SMBUS_READ) {
1136 /* Special case: only a read! */
1137 msg[0].flags = I2C_M_RD | flags;
1138 num = 1;
1140 break;
1141 case I2C_SMBUS_BYTE_DATA:
1142 if (read_write == I2C_SMBUS_READ)
1143 msg[1].len = 1;
1144 else {
1145 msg[0].len = 2;
1146 msgbuf0[1] = data->byte;
1148 break;
1149 case I2C_SMBUS_WORD_DATA:
1150 if (read_write == I2C_SMBUS_READ)
1151 msg[1].len = 2;
1152 else {
1153 msg[0].len=3;
1154 msgbuf0[1] = data->word & 0xff;
1155 msgbuf0[2] = data->word >> 8;
1157 break;
1158 case I2C_SMBUS_PROC_CALL:
1159 num = 2; /* Special case */
1160 read_write = I2C_SMBUS_READ;
1161 msg[0].len = 3;
1162 msg[1].len = 2;
1163 msgbuf0[1] = data->word & 0xff;
1164 msgbuf0[2] = data->word >> 8;
1165 break;
1166 case I2C_SMBUS_BLOCK_DATA:
1167 if (read_write == I2C_SMBUS_READ) {
1168 msg[1].flags |= I2C_M_RECV_LEN;
1169 msg[1].len = 1; /* block length will be added by
1170 the underlying bus driver */
1171 } else {
1172 msg[0].len = data->block[0] + 2;
1173 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1174 dev_err(&adapter->dev, "smbus_access called with "
1175 "invalid block write size (%d)\n",
1176 data->block[0]);
1177 return -1;
1179 for (i = 1; i < msg[0].len; i++)
1180 msgbuf0[i] = data->block[i-1];
1182 break;
1183 case I2C_SMBUS_BLOCK_PROC_CALL:
1184 num = 2; /* Another special case */
1185 read_write = I2C_SMBUS_READ;
1186 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1187 dev_err(&adapter->dev, "%s called with invalid "
1188 "block proc call size (%d)\n", __FUNCTION__,
1189 data->block[0]);
1190 return -1;
1192 msg[0].len = data->block[0] + 2;
1193 for (i = 1; i < msg[0].len; i++)
1194 msgbuf0[i] = data->block[i-1];
1195 msg[1].flags |= I2C_M_RECV_LEN;
1196 msg[1].len = 1; /* block length will be added by
1197 the underlying bus driver */
1198 break;
1199 case I2C_SMBUS_I2C_BLOCK_DATA:
1200 if (read_write == I2C_SMBUS_READ) {
1201 msg[1].len = I2C_SMBUS_BLOCK_MAX;
1202 } else {
1203 msg[0].len = data->block[0] + 1;
1204 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1205 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1206 "invalid block write size (%d)\n",
1207 data->block[0]);
1208 return -1;
1210 for (i = 1; i <= data->block[0]; i++)
1211 msgbuf0[i] = data->block[i];
1213 break;
1214 default:
1215 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1216 size);
1217 return -1;
1220 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1221 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1222 if (i) {
1223 /* Compute PEC if first message is a write */
1224 if (!(msg[0].flags & I2C_M_RD)) {
1225 if (num == 1) /* Write only */
1226 i2c_smbus_add_pec(&msg[0]);
1227 else /* Write followed by read */
1228 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1230 /* Ask for PEC if last message is a read */
1231 if (msg[num-1].flags & I2C_M_RD)
1232 msg[num-1].len++;
1235 if (i2c_transfer(adapter, msg, num) < 0)
1236 return -1;
1238 /* Check PEC if last message is a read */
1239 if (i && (msg[num-1].flags & I2C_M_RD)) {
1240 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1241 return -1;
1244 if (read_write == I2C_SMBUS_READ)
1245 switch(size) {
1246 case I2C_SMBUS_BYTE:
1247 data->byte = msgbuf0[0];
1248 break;
1249 case I2C_SMBUS_BYTE_DATA:
1250 data->byte = msgbuf1[0];
1251 break;
1252 case I2C_SMBUS_WORD_DATA:
1253 case I2C_SMBUS_PROC_CALL:
1254 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1255 break;
1256 case I2C_SMBUS_I2C_BLOCK_DATA:
1257 /* fixed at 32 for now */
1258 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1259 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1260 data->block[i+1] = msgbuf1[i];
1261 break;
1262 case I2C_SMBUS_BLOCK_DATA:
1263 case I2C_SMBUS_BLOCK_PROC_CALL:
1264 for (i = 0; i < msgbuf1[0] + 1; i++)
1265 data->block[i] = msgbuf1[i];
1266 break;
1268 return 0;
1272 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1273 char read_write, u8 command, int size,
1274 union i2c_smbus_data * data)
1276 s32 res;
1278 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1280 if (adapter->algo->smbus_xfer) {
1281 mutex_lock(&adapter->bus_lock);
1282 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1283 command,size,data);
1284 mutex_unlock(&adapter->bus_lock);
1285 } else
1286 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1287 command,size,data);
1289 return res;
1293 /* Next three are needed by i2c-isa */
1294 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1295 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1296 EXPORT_SYMBOL_GPL(i2c_bus_type);
1298 EXPORT_SYMBOL(i2c_add_adapter);
1299 EXPORT_SYMBOL(i2c_del_adapter);
1300 EXPORT_SYMBOL(i2c_del_driver);
1301 EXPORT_SYMBOL(i2c_attach_client);
1302 EXPORT_SYMBOL(i2c_detach_client);
1303 EXPORT_SYMBOL(i2c_use_client);
1304 EXPORT_SYMBOL(i2c_release_client);
1305 EXPORT_SYMBOL(i2c_clients_command);
1306 EXPORT_SYMBOL(i2c_check_addr);
1308 EXPORT_SYMBOL(i2c_master_send);
1309 EXPORT_SYMBOL(i2c_master_recv);
1310 EXPORT_SYMBOL(i2c_control);
1311 EXPORT_SYMBOL(i2c_transfer);
1312 EXPORT_SYMBOL(i2c_get_adapter);
1313 EXPORT_SYMBOL(i2c_put_adapter);
1314 EXPORT_SYMBOL(i2c_probe);
1316 EXPORT_SYMBOL(i2c_smbus_xfer);
1317 EXPORT_SYMBOL(i2c_smbus_write_quick);
1318 EXPORT_SYMBOL(i2c_smbus_read_byte);
1319 EXPORT_SYMBOL(i2c_smbus_write_byte);
1320 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1321 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1322 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1323 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1324 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1325 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1326 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1328 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1329 MODULE_DESCRIPTION("I2C-Bus main module");
1330 MODULE_LICENSE("GPL");