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

📄 i2c-dev.c

📁 s3c2410上面的I2C驱动程序,直接insmod就可以用了
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    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>    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> *//* The devfs code is contributed by Philipp Matthias Hahn    <pmhahn@titan.lahn.de> *//* $Id: i2c-dev.c,v 1.2 2004/02/06 13:19:45 laputa Exp $ */#include <linux/config.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/slab.h>#include <linux/version.h>#if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0)#include <linux/smp_lock.h>#endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */#ifdef CONFIG_DEVFS_FS#include <linux/devfs_fs_kernel.h>#endif/* If you want debugging uncomment: *//* #define DEBUG */#include <linux/init.h>#include <asm/uaccess.h>#include <linux/i2c.h>#include <linux/i2c-dev.h>//laputa debug msg 030901//#define LAPUTA_DEBUG_MSG	1#include "dbg_msg.h"/* struct file_operations changed too often in the 2.1 series for nice code */#if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)static loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin);#endif//laputa unsigned char for iic data format 030903//static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,  // -- remove static ssize_t i2cdev_read (struct file *file, unsigned char *buf, size_t count,  // +- modify                             loff_t *offset);//laputa unsigned char for iic data format 030903 //static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count, //--remove static ssize_t i2cdev_write (struct file *file, const unsigned char *buf, size_t count,  //+-modify                             loff_t *offset);static int i2cdev_ioctl (struct inode *inode, struct file *file,                          unsigned int cmd, unsigned long arg);static int i2cdev_open (struct inode *inode, struct file *file);static int i2cdev_release (struct inode *inode, struct file *file);static int i2cdev_attach_adapter(struct i2c_adapter *adap);static int i2cdev_detach_client(struct i2c_client *client);static int i2cdev_command(struct i2c_client *client, unsigned int cmd,                           void *arg);static int __init i2c_dev_init(void);static void i2cdev_cleanup(void);static struct file_operations i2cdev_fops = {#if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0)	owner:		THIS_MODULE,#endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */#if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)	llseek:		i2cdev_lseek,#else	llseek:		no_llseek,#endif	read:		i2cdev_read,	write:		i2cdev_write,	ioctl:		i2cdev_ioctl,	open:		i2cdev_open,	release:	i2cdev_release,};#define I2CDEV_ADAPS_MAX I2C_ADAP_MAXstatic struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX];#ifdef CONFIG_DEVFS_FSstatic devfs_handle_t devfs_i2c[I2CDEV_ADAPS_MAX];static devfs_handle_t devfs_handle = NULL;#endifstatic struct i2c_driver i2cdev_driver = {	name:		"i2c-dev dummy driver",	id:		I2C_DRIVERID_I2CDEV,	flags:		I2C_DF_DUMMY,	attach_adapter:	i2cdev_attach_adapter,	detach_client:	i2cdev_detach_client,	command:	i2cdev_command,/*	inc_use:	NULL,	dec_use:	NULL, */};static struct i2c_client i2cdev_client_template = {	name:		"I2C /dev entry",	id:		1,	flags:		0,	addr:		-1,/*	adapter:	NULL, */	driver:		&i2cdev_driver,/*	data:		NULL */};static int i2cdev_initialized;#if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,9)/* Note that the lseek function is called llseek in 2.1 kernels. But things   are complicated enough as is. */loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin){#ifdef DEBUG	struct inode *inode = file->f_dentry->d_inode;	printk("i2c-dev.o: i2c-%d lseek to %ld bytes relative to %d.\n",	       MINOR(inode->i_rdev),(long) offset,origin);#endif /* DEBUG */	return -ESPIPE;}#endif//laputa must be unsgined char type for the iic data 030903//static ssize_t i2cdev_read (struct file *file, char *buf, size_t count,static ssize_t i2cdev_read (struct file *file, unsigned char *buf, size_t count,                            loff_t *offset){	//char *tmp;     	// laputa -- remove	unsigned char *tmp; // laputa +- modify 030903	int ret;#ifdef DEBUG	struct inode *inode = file->f_dentry->d_inode;#endif /* DEBUG */	struct i2c_client *client = (struct i2c_client *)file->private_data;	/* copy user space data to kernel space. */	tmp = kmalloc(count,GFP_KERNEL);	if (tmp==NULL)		return -ENOMEM;#ifdef DEBUG	printk("i2c-dev.o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev),	       count);#endif	DbgMsg(LAPUTA_DEBUG_MSG,printk("--> reading %d bytes.\n",count));		ret = i2c_master_recv(client,tmp,count);	if (ret >= 0)		ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;	kfree(tmp);	return ret;}//laputa must be unsgined char type for the iic data 030903//static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count,static ssize_t i2cdev_write (struct file *file, const unsigned char *buf, size_t count,                             loff_t *offset){	int ret;	// char *tmp; 			//--remove 	unsigned char *tmp;     // +- modify 030903 	struct i2c_client *client = (struct i2c_client *)file->private_data;#ifdef DEBUG	struct inode *inode = file->f_dentry->d_inode;#endif /* DEBUG */	//laputa check for buf chip addres setting & transfer data 030901	DbgMsg(LAPUTA_DEBUG_MSG,printk("==>i2c dev  write buf [%s] addr[%x] \n",buf,client->addr));	/* copy user space data to kernel space. */	tmp = kmalloc(count,GFP_KERNEL);	if (tmp==NULL)		return -ENOMEM;	if (copy_from_user(tmp,buf,count)) {		kfree(tmp);		return -EFAULT;	}#ifdef DEBUG	printk("i2c-dev.o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev),	       count);#endif	ret = i2c_master_send(client,tmp,count);	kfree(tmp);	return ret;}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;	int i,datasize,res;	unsigned long funcs;#ifdef DEBUG	printk("i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n", 	       MINOR(inode->i_rdev),cmd, arg);#endif /* DEBUG */	//laputa check for argument at ioctl 030830	DbgMsg(LAPUTA_DEBUG_MSG,printk("client->name:%s:arg=%x\n",client->name,arg));	switch ( cmd ) {	case I2C_SLAVE:	case I2C_SLAVE_FORCE:		if ((arg > 0x3ff) || 		    (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))			return -EINVAL;				if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))			return -EBUSY;		client->addr = arg;	//laputa for to checked a address setting value 030830	DbgMsg(LAPUTA_DEBUG_MSG,printk("==>I2C_SLAVE_FORCE ADD [%x]\n",client->addr));		return 0;	case I2C_TENBIT:		if (arg)			client->flags |= I2C_M_TEN;		else			client->flags &= ~I2C_M_TEN;		return 0;	case I2C_FUNCS:		funcs = i2c_get_functionality(client->adapter);		return (copy_to_user((unsigned long *)arg,&funcs,		                     sizeof(unsigned long)))?-EFAULT:0;    case I2C_RDWR:		if (copy_from_user(&rdwr_arg, 				   (struct i2c_rdwr_ioctl_data *)arg, 				   sizeof(rdwr_arg)))			return -EFAULT;		//laputa test only return value checked 030830		DbgMsg(LAPUTA_DEBUG_MSG,printk("+++> receive data %s\n",rdwr_arg.msgs->buf));		rdwr_pa = (struct i2c_msg *)			kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), 			GFP_KERNEL);		if (rdwr_pa == NULL) return -ENOMEM;		res = 0;		for( i=0; i<rdwr_arg.nmsgs; i++ )		{		    	if(copy_from_user(&(rdwr_pa[i]),					&(rdwr_arg.msgs[i]),					sizeof(rdwr_pa[i])))			{			        res = -EFAULT;				break;			}			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,				rdwr_arg.msgs[i].buf,

⌨️ 快捷键说明

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