?? i2c-dev.c
字號:
/* i2c-dev.c - i2c-bus driver, char device interface Copyright (C) 1995-97 Simon G. Vogl Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl> Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> 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.*//* Note that this is a complete rewrite of Simon Vogl's i2c-dev module. But I have used so much of his original code and ideas that it seems only fair to recognize him as co-author -- Frodo *//* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/list.h>#include <linux/i2c.h>#include <linux/i2c-dev.h>#include <asm/uaccess.h>static struct i2c_driver i2cdev_driver;/* * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a * slave (i2c_client) with which messages will be exchanged. It's coupled * with a character special file which is accessed by user mode drivers. * * The list of i2c_dev structures is parallel to the i2c_adapter lists * maintained by the driver model, and is updated using notifications * delivered to the i2cdev_driver. */struct i2c_dev { struct list_head list; struct i2c_adapter *adap; struct device *dev;};#define I2C_MINORS 256static LIST_HEAD(i2c_dev_list);static DEFINE_SPINLOCK(i2c_dev_list_lock);static struct i2c_dev *i2c_dev_get_by_minor(unsigned index){ struct i2c_dev *i2c_dev; spin_lock(&i2c_dev_list_lock); list_for_each_entry(i2c_dev, &i2c_dev_list, list) { if (i2c_dev->adap->nr == index) goto found; } i2c_dev = NULL;found: spin_unlock(&i2c_dev_list_lock); return i2c_dev;}static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap){ struct i2c_dev *i2c_dev; if (adap->nr >= I2C_MINORS) { printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n", adap->nr); return ERR_PTR(-ENODEV); } i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL); if (!i2c_dev) return ERR_PTR(-ENOMEM); i2c_dev->adap = adap; spin_lock(&i2c_dev_list_lock); list_add_tail(&i2c_dev->list, &i2c_dev_list); spin_unlock(&i2c_dev_list_lock); return i2c_dev;}static void return_i2c_dev(struct i2c_dev *i2c_dev){ spin_lock(&i2c_dev_list_lock); list_del(&i2c_dev->list); spin_unlock(&i2c_dev_list_lock); kfree(i2c_dev);}static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf){ struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt)); if (!i2c_dev) return -ENODEV; return sprintf(buf, "%s\n", i2c_dev->adap->name);}static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);/* ------------------------------------------------------------------------- *//* * After opening an instance of this character special file, a file * descriptor starts out associated only with an i2c_adapter (and bus). * * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg * traffic to any devices on the bus used by that adapter. That's because * the i2c_msg vectors embed all the addressing information they need, and * are submitted directly to an i2c_adapter. However, SMBus-only adapters * don't support that interface. * * To use read()/write() system calls on that file descriptor, or to use * SMBus interfaces (and work with SMBus-only hosts!), you must first issue * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous * (never registered) i2c_client so it holds the addressing information * needed by those system calls and by this SMBus interface. */static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count, loff_t *offset){ char *tmp; int ret; struct i2c_client *client = (struct i2c_client *)file->private_data; if (count > 8192) count = 8192; tmp = kmalloc(count,GFP_KERNEL); if (tmp==NULL) return -ENOMEM; pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n", iminor(file->f_path.dentry->d_inode), count); ret = i2c_master_recv(client,tmp,count); if (ret >= 0) ret = copy_to_user(buf,tmp,count)?-EFAULT:ret; kfree(tmp); return ret;}static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count, loff_t *offset){ int ret; char *tmp; struct i2c_client *client = (struct i2c_client *)file->private_data; if (count > 8192) count = 8192; tmp = kmalloc(count,GFP_KERNEL); if (tmp==NULL) return -ENOMEM; if (copy_from_user(tmp,buf,count)) { kfree(tmp); return -EFAULT; } pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n", iminor(file->f_path.dentry->d_inode), count); ret = i2c_master_send(client,tmp,count); kfree(tmp); return ret;}/* This address checking function differs from the one in i2c-core in that it considers an address with a registered device, but no bound driver, as NOT busy. */static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr){ struct list_head *item; struct i2c_client *client; int res = 0; mutex_lock(&adapter->clist_lock); list_for_each(item, &adapter->clients) { client = list_entry(item, struct i2c_client, list); if (client->addr == addr) { if (client->driver) res = -EBUSY; break; } } mutex_unlock(&adapter->clist_lock); return res;}static int i2cdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct i2c_client *client = (struct i2c_client *)file->private_data; struct i2c_rdwr_ioctl_data rdwr_arg; struct i2c_smbus_ioctl_data data_arg; union i2c_smbus_data temp; struct i2c_msg *rdwr_pa; u8 __user **data_ptrs; int i,datasize,res; unsigned long funcs; dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n", cmd, arg); switch ( cmd ) { case I2C_SLAVE: case I2C_SLAVE_FORCE: /* NOTE: devices set up to work with "new style" drivers * can't use I2C_SLAVE, even when the device node is not * bound to a driver. Only I2C_SLAVE_FORCE will work. * * Setting the PEC flag here won't affect kernel drivers, * which will be using the i2c_client node registered with * the driver model core. Likewise, when that client has * the PEC flag already set, the i2c-dev driver won't see * (or use) this setting. */ if ((arg > 0x3ff) || (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f)) return -EINVAL; if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg)) return -EBUSY; /* REVISIT: address could become busy later */ client->addr = arg; return 0; case I2C_TENBIT: if (arg) client->flags |= I2C_M_TEN; else client->flags &= ~I2C_M_TEN; return 0; case I2C_PEC: if (arg) client->flags |= I2C_CLIENT_PEC; else client->flags &= ~I2C_CLIENT_PEC; return 0; case I2C_FUNCS: funcs = i2c_get_functionality(client->adapter); return put_user(funcs, (unsigned long __user *)arg); case I2C_RDWR: if (copy_from_user(&rdwr_arg, (struct i2c_rdwr_ioctl_data __user *)arg, sizeof(rdwr_arg))) return -EFAULT; /* Put an arbitrary limit on the number of messages that can * be sent at once */ if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS) return -EINVAL; rdwr_pa = (struct i2c_msg *) kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), GFP_KERNEL); if (rdwr_pa == NULL) return -ENOMEM; if (copy_from_user(rdwr_pa, rdwr_arg.msgs, rdwr_arg.nmsgs * sizeof(struct i2c_msg))) { kfree(rdwr_pa); return -EFAULT; } data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL); if (data_ptrs == NULL) { kfree(rdwr_pa); return -ENOMEM; } res = 0; for( i=0; i<rdwr_arg.nmsgs; i++ ) { /* Limit the size of the message to a sane amount; * and don't let length change either. */ if ((rdwr_pa[i].len > 8192) || (rdwr_pa[i].flags & I2C_M_RECV_LEN)) { res = -EINVAL; break; } data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf; rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL); if(rdwr_pa[i].buf == NULL) { res = -ENOMEM; break; } if(copy_from_user(rdwr_pa[i].buf, data_ptrs[i], rdwr_pa[i].len)) { ++i; /* Needs to be kfreed too */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -