📄 i2c-core.c
字号:
/* i2c-core.c - a device driver for the iic-bus interface *//* ------------------------------------------------------------------------- *//* Copyright (C) 1995-99 Simon G. Vogl This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* ------------------------------------------------------------------------- *//* With some changes from Ky鰏ti M鋖kki <kmalkki@cc.hut.fi>. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and Jean Delvare <khali@linux-fr.org> */#include <linux/module.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/i2c.h>#include <linux/init.h>#include <linux/idr.h>#include <linux/seq_file.h>#include <linux/platform_device.h>#include <linux/mutex.h>#include <asm/uaccess.h>static LIST_HEAD(adapters);static LIST_HEAD(drivers);static DEFINE_MUTEX(core_lists);static DEFINE_IDR(i2c_adapter_idr);/* match always succeeds, as we want the probe() to tell if we really accept this match */static int i2c_device_match(struct device *dev, struct device_driver *drv){ return 1;}static int i2c_bus_suspend(struct device * dev, pm_message_t state){ int rc = 0; if (dev->driver && dev->driver->suspend) rc = dev->driver->suspend(dev, state); return rc;}static int i2c_bus_resume(struct device * dev){ int rc = 0; if (dev->driver && dev->driver->resume) rc = dev->driver->resume(dev); return rc;}static int i2c_device_probe(struct device *dev){ return -ENODEV;}static int i2c_device_remove(struct device *dev){ return 0;}struct bus_type i2c_bus_type = { .name = "i2c", .match = i2c_device_match, .probe = i2c_device_probe, .remove = i2c_device_remove, .suspend = i2c_bus_suspend, .resume = i2c_bus_resume,};void i2c_adapter_dev_release(struct device *dev){ struct i2c_adapter *adap = dev_to_i2c_adapter(dev); complete(&adap->dev_released);}struct device_driver i2c_adapter_driver = { .owner = THIS_MODULE, .name = "i2c_adapter", .bus = &i2c_bus_type,};static void i2c_adapter_class_dev_release(struct class_device *dev){ struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev); complete(&adap->class_dev_released);}struct class i2c_adapter_class = { .owner = THIS_MODULE, .name = "i2c-adapter", .release = &i2c_adapter_class_dev_release,};static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf){ struct i2c_adapter *adap = dev_to_i2c_adapter(dev); return sprintf(buf, "%s\n", adap->name);}static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);static void i2c_client_release(struct device *dev){ struct i2c_client *client = to_i2c_client(dev); complete(&client->released);}static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf){ struct i2c_client *client = to_i2c_client(dev); return sprintf(buf, "%s\n", client->name);}/* * We can't use the DEVICE_ATTR() macro here as we want the same filename for a * different type of a device. So beware if the DEVICE_ATTR() macro ever * changes, this definition will also have to change. */static struct device_attribute dev_attr_client_name = { .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE }, .show = &show_client_name,};/* --------------------------------------------------- * registering functions * --------------------------------------------------- *//* ----- * i2c_add_adapter is called from within the algorithm layer, * when a new hw adapter registers. A new device is register to be * available for clients. */int i2c_add_adapter(struct i2c_adapter *adap){ int id, res = 0; struct list_head *item; struct i2c_driver *driver; mutex_lock(&core_lists); if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) { res = -ENOMEM; goto out_unlock; } res = idr_get_new(&i2c_adapter_idr, adap, &id); if (res < 0) { if (res == -EAGAIN) res = -ENOMEM; goto out_unlock; } adap->nr = id & MAX_ID_MASK; mutex_init(&adap->bus_lock); mutex_init(&adap->clist_lock); list_add_tail(&adap->list,&adapters); INIT_LIST_HEAD(&adap->clients); /* Add the adapter to the driver core. * If the parent pointer is not set up, * we add this adapter to the host bus. */ if (adap->dev.parent == NULL) adap->dev.parent = &platform_bus; sprintf(adap->dev.bus_id, "i2c-%d", adap->nr); adap->dev.driver = &i2c_adapter_driver; adap->dev.release = &i2c_adapter_dev_release; res = device_register(&adap->dev); if (res) goto out_list; res = device_create_file(&adap->dev, &dev_attr_name); if (res) goto out_unregister; /* Add this adapter to the i2c_adapter class */ memset(&adap->class_dev, 0x00, sizeof(struct class_device)); adap->class_dev.dev = &adap->dev; adap->class_dev.class = &i2c_adapter_class; strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE); res = class_device_register(&adap->class_dev); if (res) goto out_remove_name; dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); /* inform drivers of new adapters */ list_for_each(item,&drivers) { driver = list_entry(item, struct i2c_driver, list); if (driver->attach_adapter) /* We ignore the return code; if it fails, too bad */ driver->attach_adapter(adap); }out_unlock: mutex_unlock(&core_lists); return res;out_remove_name: device_remove_file(&adap->dev, &dev_attr_name);out_unregister: init_completion(&adap->dev_released); /* Needed? */ device_unregister(&adap->dev); wait_for_completion(&adap->dev_released);out_list: list_del(&adap->list); idr_remove(&i2c_adapter_idr, adap->nr); goto out_unlock;}int i2c_del_adapter(struct i2c_adapter *adap){ struct list_head *item, *_n; struct i2c_adapter *adap_from_list; struct i2c_driver *driver; struct i2c_client *client; int res = 0; mutex_lock(&core_lists); /* First make sure that this adapter was ever added */ list_for_each_entry(adap_from_list, &adapters, list) { if (adap_from_list == adap) break; } if (adap_from_list != adap) { pr_debug("i2c-core: attempting to delete unregistered " "adapter [%s]\n", adap->name); res = -EINVAL; goto out_unlock; } list_for_each(item,&drivers) { driver = list_entry(item, struct i2c_driver, list); if (driver->detach_adapter) if ((res = driver->detach_adapter(adap))) { dev_err(&adap->dev, "detach_adapter failed " "for driver [%s]\n", driver->driver.name); goto out_unlock; } } /* detach any active clients. This must be done first, because * it can fail; in which case we give up. */ list_for_each_safe(item, _n, &adap->clients) { client = list_entry(item, struct i2c_client, list); if ((res=client->driver->detach_client(client))) { dev_err(&adap->dev, "detach_client failed for client " "[%s] at address 0x%02x\n", client->name, client->addr); goto out_unlock; } } /* clean up the sysfs representation */ init_completion(&adap->dev_released); init_completion(&adap->class_dev_released); class_device_unregister(&adap->class_dev); device_remove_file(&adap->dev, &dev_attr_name); device_unregister(&adap->dev); list_del(&adap->list); /* wait for sysfs to drop all references */ wait_for_completion(&adap->dev_released); wait_for_completion(&adap->class_dev_released); /* free dynamically allocated bus id */ idr_remove(&i2c_adapter_idr, adap->nr); dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); out_unlock: mutex_unlock(&core_lists); return res;}/* ----- * What follows is the "upwards" interface: commands for talking to clients, * which implement the functions to access the physical information of the * chips. */int i2c_register_driver(struct module *owner, struct i2c_driver *driver){ struct list_head *item; struct i2c_adapter *adapter; int res; /* add the driver to the list of i2c drivers in the driver core */ driver->driver.owner = owner; driver->driver.bus = &i2c_bus_type; res = driver_register(&driver->driver); if (res) return res; mutex_lock(&core_lists); list_add_tail(&driver->list,&drivers); pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); /* now look for instances of driver on our adapters */ if (driver->attach_adapter) { list_for_each(item,&adapters) { adapter = list_entry(item, struct i2c_adapter, list); driver->attach_adapter(adapter); } } mutex_unlock(&core_lists); return 0;}EXPORT_SYMBOL(i2c_register_driver);int i2c_del_driver(struct i2c_driver *driver){ struct list_head *item1, *item2, *_n; struct i2c_client *client; struct i2c_adapter *adap; int res = 0; mutex_lock(&core_lists); /* Have a look at each adapter, if clients of this driver are still * attached. If so, detach them to be able to kill the driver * afterwards. */ list_for_each(item1,&adapters) { adap = list_entry(item1, struct i2c_adapter, list); if (driver->detach_adapter) { if ((res = driver->detach_adapter(adap))) { dev_err(&adap->dev, "detach_adapter failed " "for driver [%s]\n", driver->driver.name); goto out_unlock; } } else { list_for_each_safe(item2, _n, &adap->clients) { client = list_entry(item2, struct i2c_client, list); if (client->driver != driver) continue; dev_dbg(&adap->dev, "detaching client [%s] " "at 0x%02x\n", client->name, client->addr); if ((res = driver->detach_client(client))) { dev_err(&adap->dev, "detach_client " "failed for client [%s] at " "0x%02x\n", client->name, client->addr); goto out_unlock; } } } } driver_unregister(&driver->driver); list_del(&driver->list); pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); out_unlock: mutex_unlock(&core_lists); return 0;}static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr){ struct list_head *item; struct i2c_client *client; list_for_each(item,&adapter->clients) { client = list_entry(item, struct i2c_client, list); if (client->addr == addr) return -EBUSY; } return 0;}int i2c_check_addr(struct i2c_adapter *adapter, int addr){ int rval;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -