⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c-core.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/* 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>                */#include <linux/config.h>#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 <asm/uaccess.h>static LIST_HEAD(adapters);static LIST_HEAD(drivers);static DECLARE_MUTEX(core_lists);static DEFINE_IDR(i2c_adapter_idr);int i2c_device_probe(struct device *dev){	return -ENODEV;}int i2c_device_remove(struct device *dev){	return 0;}static void i2c_adapter_dev_release(struct device *dev){	struct i2c_adapter *adap = dev_to_i2c_adapter(dev);	complete(&adap->dev_released);}static struct device_driver i2c_adapter_driver = {	.name =	"i2c_adapter",	.bus = &i2c_bus_type,	.probe = i2c_device_probe,	.remove = i2c_device_remove,};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);}static struct class i2c_adapter_class = {	.name =		"i2c-adapter",	.release =	&i2c_adapter_class_dev_release,};static ssize_t show_adapter_name(struct device *dev, 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, 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;	down(&core_lists);	if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {		res = -ENOMEM;		goto out_unlock;	}	res = idr_get_new(&i2c_adapter_idr, NULL, &id);	if (res < 0) {		if (res == -EAGAIN)			res = -ENOMEM;		goto out_unlock;	}	adap->nr =  id & MAX_ID_MASK;	init_MUTEX(&adap->bus_lock);	init_MUTEX(&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;	device_register(&adap->dev);	device_create_file(&adap->dev, &dev_attr_name);	/* 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);	class_device_register(&adap->class_dev);	/* inform drivers of new adapters */	list_for_each(item,&drivers) {		driver = list_entry(item, struct i2c_driver, list);		if (driver->flags & I2C_DF_NOTIFY)			/* We ignore the return code; if it fails, too bad */			driver->attach_adapter(adap);	}	dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);out_unlock:	up(&core_lists);	return res;}int i2c_del_adapter(struct i2c_adapter *adap){	struct list_head  *item, *_n;	struct i2c_driver *driver;	struct i2c_client *client;	int res = 0;	down(&core_lists);	list_for_each(item,&drivers) {		driver = list_entry(item, struct i2c_driver, list);		if (driver->detach_adapter)			if ((res = driver->detach_adapter(adap))) {				dev_warn(&adap->dev, "can't detach adapter "					 "while detaching driver %s: driver not "					 "detached!", driver->name);				goto out_unlock;			}	}	/* detach any active clients. This must be done first, because	 * it can fail; in which case we give upp. */	list_for_each_safe(item, _n, &adap->clients) {		client = list_entry(item, struct i2c_client, list);		/* detaching devices is unconditional of the set notify		 * flag, as _all_ clients that reside on the adapter		 * must be deleted, as this would cause invalid states.		 */		if ((res=client->driver->detach_client(client))) {			dev_err(&adap->dev, "adapter not "				"unregistered, because client at "				"address %02x can't be detached. ",				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 unregistered\n"); out_unlock:	up(&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_add_driver(struct i2c_driver *driver){	struct list_head   *item;	struct i2c_adapter *adapter;	int res = 0;	down(&core_lists);	/* add the driver to the list of i2c drivers in the driver core */	driver->driver.name = driver->name;	driver->driver.bus = &i2c_bus_type;	driver->driver.probe = i2c_device_probe;	driver->driver.remove = i2c_device_remove;	res = driver_register(&driver->driver);	if (res)		goto out_unlock;		list_add_tail(&driver->list,&drivers);	pr_debug("i2c-core: driver %s registered.\n", driver->name);	/* now look for instances of driver on our adapters */	if (driver->flags & I2C_DF_NOTIFY) {		list_for_each(item,&adapters) {			adapter = list_entry(item, struct i2c_adapter, list);			driver->attach_adapter(adapter);		}	} out_unlock:	up(&core_lists);	return res;}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;	down(&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.	 */	pr_debug("i2c-core: unregister_driver - looking for clients.\n");	/* removing clients does not depend on the notify flag, else 	 * invalid operation might (will!) result, when using stale client	 * pointers.	 */	list_for_each(item1,&adapters) {		adap = list_entry(item1, struct i2c_adapter, list);		dev_dbg(&adap->dev, "examining adapter\n");		if (driver->detach_adapter) {			if ((res = driver->detach_adapter(adap))) {				dev_warn(&adap->dev, "while unregistering "				       "dummy driver %s, adapter could "				       "not be detached properly; driver "				       "not unloaded!",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;				pr_debug("i2c-core.o: detaching client %s:\n", client->name);				if ((res = driver->detach_client(client))) {					dev_err(&adap->dev, "while "						"unregistering driver "						"`%s', the client at "						"address %02x of "						"adapter could not "						"be detached; driver "						"not unloaded!",						driver->name,						client->addr);					goto out_unlock;				}			}		}	}	driver_unregister(&driver->driver);	list_del(&driver->list);	pr_debug("i2c-core: driver unregistered: %s\n", driver->name); out_unlock:	up(&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;	down(&adapter->clist_lock);	rval = __i2c_check_addr(adapter, addr);	up(&adapter->clist_lock);	return rval;}int i2c_attach_client(struct i2c_client *client){	struct i2c_adapter *adapter = client->adapter;	down(&adapter->clist_lock);	if (__i2c_check_addr(client->adapter, client->addr)) {		up(&adapter->clist_lock);		return -EBUSY;	}	list_add_tail(&client->list,&adapter->clients);	up(&adapter->clist_lock);		if (adapter->client_register)  {		if (adapter->client_register(client))  {			dev_warn(&adapter->dev, "warning: client_register "				"seems to have failed for client %02x\n",				client->addr);		}	}	dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",		client->name);	if (client->flags & I2C_CLIENT_ALLOW_USE)		client->usage_count = 0;	client->dev.parent = &client->adapter->dev;	client->dev.driver = &client->driver->driver;	client->dev.bus = &i2c_bus_type;	client->dev.release = &i2c_client_release;		snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),		"%d-%04x", i2c_adapter_id(adapter), client->addr);	pr_debug("registering %s\n", client->dev.bus_id);	device_register(&client->dev);	device_create_file(&client->dev, &dev_attr_client_name);		return 0;}int i2c_detach_client(struct i2c_client *client){	struct i2c_adapter *adapter = client->adapter;	int res = 0;		if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))		return -EBUSY;	if (adapter->client_unregister)  {		res = adapter->client_unregister(client);		if (res) {			dev_err(&client->dev,			       "client_unregister [%s] failed, "			       "client not detached", client->name);			goto out;		}	}	down(&adapter->clist_lock);	list_del(&client->list);	init_completion(&client->released);	device_remove_file(&client->dev, &dev_attr_client_name);	device_unregister(&client->dev);	up(&adapter->clist_lock);	wait_for_completion(&client->released); out:	return res;}static int i2c_inc_use_client(struct i2c_client *client){	if (!try_module_get(client->driver->owner))		return -ENODEV;	if (!try_module_get(client->adapter->owner)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -