i2c-dev模块--read/write支持DMA传输

打开内核配置项CONFIG_I2C_CHARDEV,加载i2c-dev模块。

设备节点:/dev/i2c-x。

用户空间通过设备节点访问挂载到i2c适配器上的任意i2c设备。.


i2c-dev模块的read/write函数不支持block传输,一次最多传输8字节数据。

修改read/write函数支持DMA传输,以实现block传输。DMA传输 BUFFER 大小的限制是255字节。

代码修改如下:i2c-dev.c

#include<linux/dma-mapping.h>

#define MAX_BUFFER_SIZE 255

static ssize_t i2cdev_write_dma(struct file *file,const char __user *buf, size_t count, loff_t *offset)

{

int ret;

struct i2c_client *client = file->private_data;

char *pI2cDMAWriteBuf = NULL;

unsigned int PhyAddrForDMABuf = 0;

if (count > MAX_BUFFER_SIZE )

count  = MAX_BUFFER_SIZE ;


pI2cDMAWriteBuf  = (char *)dma_alloc_coherent(NULL,  MAX_BUFFER_SIZE ,&PhyAddrForDMABuf , GFP_KERNEL);

if (pI2cDMAWriteBuf   == NULL)

{

return -ENOMEM;

}


if (copyt_from_user(pI2cDMAWriteBuf  , buf, count))

{

// error

dma_free_coherent(NULL, MAX_BUFFER_SIZE ,  pI2cDMAWriteBuf  , PhyAddrForDMABuf );

pI2cDMAWriteBuf  = NULL;

PhyAddrForDMABuf  = 0;

return -EFAULT;

}


client->addr &= I2C_MASK_FLAG;

client->ext_flag |= I2C_DMA_FLAG;

client->ext_flag |= I2C_A_FILTER_MSG;

ret = i2c_master_sent(client, (unsigned char *)PhyAddrForDMABuf, count);

dma_free_coherent(NULL, MAX_BUFFER_SIZE ,  pI2cDMAWriteBuf  , PhyAddrForDMABuf );

pI2cDMAWriteBuf  = NULL;

PhyAddrForDMABuf  = 0;

return ret;

}


static ssize_t i2cdev_write_dma(struct file *file,const char __user *buf, size_t count, loff_t *offset)

{

int ret;

struct i2c_client *client = file->private_data;

char *pI2cDMAReadBuf = NULL;

unsigned int PhyAddrForDMABuf = 0;

if (count > MAX_BUFFER_SIZE )

count  = MAX_BUFFER_SIZE ;


pI2cDMAReadBuf = (char *)dma_alloc_coherent(NULL,  MAX_BUFFER_SIZE , &PhyAddrForDMABuf, GFP_KERNEL);

if (pI2cDMAReadBuf == NULL)

{

return -ENOMEM;

}

client->addr &= I2C_MASK_FLAG;

client->ext_flag |= I2C_DMA_FLAG;

client->ext_flag |= I2C_A_FILTER_MSG;

ret = i2c_master_recv(client, (unsigned char *)PhyAddrForDMABuf, count);
if (ret >= 0)
{
if(copy_to_user(buf, pI2cDMAReadBuf, ret))
{
   ret = -EFAULT;
}
}

dma_free_coherent(NULL, MAX_BUFFER_SIZE ,  pI2cDMAReadBuf, PhyAddrForDMABuf );

pI2cDMAWriteBuf  = NULL;

PhyAddrForDMABuf  = 0;

return ret;

}


static const struct file_operations i2cdev_fops = {
...
.read= i2cdev_read_dma,
.write = i2cdev_write_dma,

};


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值