i2c-dev模組--read/write支援DMA傳輸_kernel-i2c子系統

來源:互聯網
上載者:User

開啟核心配置項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,
};


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.