ioctl和内核通信传值例子

来源:互联网 发布:unity3d怎么创建地面 编辑:程序博客网 时间:2024/06/07 01:45

应用层:

static int write_i2c_data(unsigned char *writedata, unsigned char *returndata)

{
    char nm[16];
int fd, i;
unsigned char data[10];
    sprintf(data,"%s",writedata);
snprintf(nm, 16, "/dev/%s", I2C_DEV_NAME);
if ((fd = open(nm, O_RDONLY)) < 0) {
   dprintf("open file error!");
   return 0;
}
ioctl(fd, RT2880_I2C_WRITE, &data);
close(fd);
for(i=0;i<10; i++)
{
        returndata[i]=data[i];
}
return 1;

}



内核设备文件:

struct file_operations i2cdrv_fops = {
ioctl:  i2cdrv_ioctl,
};


int i2cdrv_ioctl(struct inode *inode, struct file *filp, \
                     unsigned int cmd, unsigned long arg)
{

unsigned int address, size;
unsigned long value;
I2C_WRITE *i2c_write;
unsigned char *tx_data;
unsigned char rx_data[10];
    int status = 0;
    unsigned char flag=5;
    int i;
    
switch (cmd) {
case RT2880_I2C_DUMP:
//i2c_eeprom_dump();
TestProtection();
break;
case RT2880_I2C_READ:
value = 0; address = 0;
address = (unsigned int)arg;
gpio_i2c_read(0xC0>>1, tmp_sub_addr,rx_data,10);
break;
case RT2880_I2C_WRITE:
tx_data = (unsigned char *)arg;

size = gpio_i2c_write(0xC0>>1, tmp_sub_addr,tx_data,8);

mdelay(1);

size = gpio_i2c_read(0xC0>>1, tmp_sub_addr,rx_data,10);


status = copy_to_user((unsigned char *)arg, rx_data, 10);
break;

default :
printk("i2c_drv: command format error\n");
}


return 0;
}

0 0
原创粉丝点击