#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h> //kmalloc
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <asm/io.h>
#include <asm/uaccess.h>
struct fm24_drv_dev{
int major;
struct class *cls;
struct device *dev;
struct i2c_client *client;
};
struct fm24_drv_dev *fm24_dev;
int fm24_read_bytes(struct i2c_client *client, char *buf, int len)
{
int ret;
struct i2c_msg msg;
msg.addr= client->addr;
msg.flags |=1;
msg.len=len;
msg.buf=buf;
ret = i2c_transfer(client->adapter, &msgs,1);
return (ret == 1)? len : ret;
}
int fm24_write_bytes(struct i2c_client *client, char *buf, int len)
{
int ret;
struct i2c_msg msg;
msg.addr= client->addr;
msg.flags |=0;
msg.len=len;
msg.buf=buf;
ret = i2c_transfer(client->adapter, &msgs,1);
return (ret == 1)? len : ret;
}
int fm24_open(struct inode *inode, struct file *filp)
{
printk("---------%s---------\n", __FUNCTION__);
return 0;
}
ssize_t fm24_write (struct file *filp, const char __user *buf, size_t count, loff_t *loff)
{
printk("---------%s---------\n", __FUNCTION__);
int ret;
char *tmp_buf;
tmp_buf = kzalloc(count,GFP_KERNEL);
if(tmp_buf == NULL)
{
printf("kzalloc error");
return -ENOMEM;
}
ret = copy_from_user(tmp_buf,buf,count);
if(ret)
{
printf("copy_to_user error");
ret = -EFAULT;
goto err_0;
}
ret= fm24_write_bytes(fm24_dev->client,tmp_buf,count);
if(ret < 0)
{
printf("kzalloc error");
ret = -EAGAIN;
goto err_0;
}
return count;
err_0:
kfree(tmp_buf);
return ret;
}
ssize_t fm24_read (struct file *filp, char __user *buf, size_t count, loff_t *loff)
{
printk("---------%s---------\n", __FUNCTION__);
int ret;
char *tmp_buf;
tmp_buf = kzalloc(count,GFP_KERNEL);
if(tmp_buf == NULL)
{
printf("kzalloc error");
return -ENOMEM;
}
ret= fm24_read_bytes(fm24_dev->client,tmp_buf,count);
if(ret < 0)
{
printf("kzalloc error");
ret = -EAGAIN;
goto err_0;
}
ret = copy_to_user(buf,tmp_buf,count);
if(ret)
{
printf("copy_to_user error");
ret = -EFAULT;
goto err_0;
}
return count;
err_0:
kfree(tmp_buf);
return ret;
}
int fm24_close(struct inode *inode, struct file *filp)
{
printk("---------%s---------\n", __FUNCTION__);
return 0;
}
struct file_operations fm24_fops ={
.open= fm24_open,
.write= fm24_write,
.read= fm24_read,
release= fm24_close,
};
int fm24_drv_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
printk("------------%s-----------\n",__FUNCTION__);
int ret;
fm24_dev = kzalloc(sizeof(struct fm24_drv_dev),GFP_KERNEL);
if(fm24_dev == NULL)
{
printk(KERN_ERR "kzalloc error");
return ENOMEM;
}
fm24_dev->major = register_chrdev(0, "fm24_dev", &fm24_fops);
if(fm24_dev->major < 0)
{
printk(KERN_ERR "register_chrdev error");
ret = -EFAULT;
goto err_0;
}
fm24_dev->cls = class_create(THIS_MODULE, "fm24");
if(fm24_dev->cls == NULL)
{
printk(KERN_ERR "register_chrdev error");
ret = -EFAULT;
goto err_1;
}
MKDEV(ma,mi)
fm24_dev->dev = device_create(fm24_dev->cls,NULL,MKDEV(fm24_dev->major,0),NULL,"fm24%d",0);
if(fm24_dev->dev == NULL)
{
printk(KERN_ERR "device_create error");
ret = -EFAULT;
goto err_2;
}
return 0;
err_2:
class_destroy(fm24_dev->cls);
err_1:
unregister_chrdev(fm24_dev->major,"fm24_dev");
err_0:
kfree(fm24_dev);
return ret;
}
int fm24_drv_remove(struct i2c_client *)
{
printk("------------%s-----------\n",__FUNCTION__);
device_destroy(fm24_dev->cls,MKDEV(fm24_dev->major,0));
class_destroy(fm24_dev->cls);
unregister_chrdev(fm24_dev->major,"fm24_dev");
kfree(fm24_dev);
}
struct i2c_device_id fm24_drv_device_id[] = {
{"FM24CL04", 0x1234},
{"FM24CL08", 0x1235},
};
struct i2c_driver fm24_drv={
.probe= fm24_drv_probe,
.remove= fm24_drv_remove,
.driver= {
.name = "FM24CL04"
},
.id_table= fm24_drv_device_id,
};
static int __init fm24_drv_init(void)
{
printk("-----------%s-----------\n", __FUNCTION__);
return i2c_add_driver(&fm24_drv);
}
static void __exit fm24_drv_exit(void)
{
printk("-----------%s-----------\n", __FUNCTION__);
i2c_del_driver(&fm24_drv);
}
module_init(fm24_drv_init);
module_exit(fm24_drv_exit);
MODULE_LICENSE("GPL");
测试的时候总会出现空指针错误,然后就崩了。问题出在fm24_write_bytes函数上,但是看不错原因,求搭救!!
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h> //kmalloc
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <asm/io.h>
#include <asm/uaccess.h>
struct fm24_drv_dev{
int major;
struct class *cls;
struct device *dev;
struct i2c_client *client;
};
struct fm24_drv_dev *fm24_dev;
int fm24_read_bytes(struct i2c_client *client, char *buf, int len)
{
int ret;
struct i2c_msg msg;
msg.addr= client->addr;
msg.flags |=1;
msg.len=len;
msg.buf=buf;
ret = i2c_transfer(client->adapter, &msgs,1);
return (ret == 1)? len : ret;
}
int fm24_write_bytes(struct i2c_client *client, char *buf, int len)
{
int ret;
struct i2c_msg msg;
msg.addr= client->addr;
msg.flags |=0;
msg.len=len;
msg.buf=buf;
ret = i2c_transfer(client->adapter, &msgs,1);
return (ret == 1)? len : ret;
}
int fm24_open(struct inode *inode, struct file *filp)
{
printk("---------%s---------\n", __FUNCTION__);
return 0;
}
ssize_t fm24_write (struct file *filp, const char __user *buf, size_t count, loff_t *loff)
{
printk("---------%s---------\n", __FUNCTION__);
int ret;
char *tmp_buf;
tmp_buf = kzalloc(count,GFP_KERNEL);
if(tmp_buf == NULL)
{
printf("kzalloc error");
return -ENOMEM;
}
ret = copy_from_user(tmp_buf,buf,count);
if(ret)
{
printf("copy_to_user error");
ret = -EFAULT;
goto err_0;
}
ret= fm24_write_bytes(fm24_dev->client,tmp_buf,count);
if(ret < 0)
{
printf("kzalloc error");
ret = -EAGAIN;
goto err_0;
}
return count;
err_0:
kfree(tmp_buf);
return ret;
}
ssize_t fm24_read (struct file *filp, char __user *buf, size_t count, loff_t *loff)
{
printk("---------%s---------\n", __FUNCTION__);
int ret;
char *tmp_buf;
tmp_buf = kzalloc(count,GFP_KERNEL);
if(tmp_buf == NULL)
{
printf("kzalloc error");
return -ENOMEM;
}
ret= fm24_read_bytes(fm24_dev->client,tmp_buf,count);
if(ret < 0)
{
printf("kzalloc error");
ret = -EAGAIN;
goto err_0;
}
ret = copy_to_user(buf,tmp_buf,count);
if(ret)
{
printf("copy_to_user error");
ret = -EFAULT;
goto err_0;
}
return count;
err_0:
kfree(tmp_buf);
return ret;
}
int fm24_close(struct inode *inode, struct file *filp)
{
printk("---------%s---------\n", __FUNCTION__);
return 0;
}
struct file_operations fm24_fops ={
.open= fm24_open,
.write= fm24_write,
.read= fm24_read,
release= fm24_close,
};
int fm24_drv_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
printk("------------%s-----------\n",__FUNCTION__);
int ret;
fm24_dev = kzalloc(sizeof(struct fm24_drv_dev),GFP_KERNEL);
if(fm24_dev == NULL)
{
printk(KERN_ERR "kzalloc error");
return ENOMEM;
}
fm24_dev->major = register_chrdev(0, "fm24_dev", &fm24_fops);
if(fm24_dev->major < 0)
{
printk(KERN_ERR "register_chrdev error");
ret = -EFAULT;
goto err_0;
}
fm24_dev->cls = class_create(THIS_MODULE, "fm24");
if(fm24_dev->cls == NULL)
{
printk(KERN_ERR "register_chrdev error");
ret = -EFAULT;
goto err_1;
}
MKDEV(ma,mi)
fm24_dev->dev = device_create(fm24_dev->cls,NULL,MKDEV(fm24_dev->major,0),NULL,"fm24%d",0);
if(fm24_dev->dev == NULL)
{
printk(KERN_ERR "device_create error");
ret = -EFAULT;
goto err_2;
}
return 0;
err_2:
class_destroy(fm24_dev->cls);
err_1:
unregister_chrdev(fm24_dev->major,"fm24_dev");
err_0:
kfree(fm24_dev);
return ret;
}
int fm24_drv_remove(struct i2c_client *)
{
printk("------------%s-----------\n",__FUNCTION__);
device_destroy(fm24_dev->cls,MKDEV(fm24_dev->major,0));
class_destroy(fm24_dev->cls);
unregister_chrdev(fm24_dev->major,"fm24_dev");
kfree(fm24_dev);
}
struct i2c_device_id fm24_drv_device_id[] = {
{"FM24CL04", 0x1234},
{"FM24CL08", 0x1235},
};
struct i2c_driver fm24_drv={
.probe= fm24_drv_probe,
.remove= fm24_drv_remove,
.driver= {
.name = "FM24CL04"
},
.id_table= fm24_drv_device_id,
};
static int __init fm24_drv_init(void)
{
printk("-----------%s-----------\n", __FUNCTION__);
return i2c_add_driver(&fm24_drv);
}
static void __exit fm24_drv_exit(void)
{
printk("-----------%s-----------\n", __FUNCTION__);
i2c_del_driver(&fm24_drv);
}
module_init(fm24_drv_init);
module_exit(fm24_drv_exit);
MODULE_LICENSE("GPL");
测试的时候总会出现空指针错误,然后就崩了。问题出在fm24_write_bytes函数上,但是看不错原因,求搭救!!