当前位置: 技术问答>linux和unix
pci驱动的读写函数
来源: 互联网 发布时间:2016-11-06
本文导语: 又要麻烦各位大哥了!小弟那写的pci采集卡的驱动函数,要求也不高,实现基本的IO访问即可! 设备结构体如下: struct adlink_device { unsigned long mem_len; //the lence of the memery region void __iomem *ioaddr; //the base star...
又要麻烦各位大哥了!小弟那写的pci采集卡的驱动函数,要求也不高,实现基本的IO访问即可!
设备结构体如下:
struct adlink_device
{
unsigned long mem_len; //the lence of the memery region
void __iomem *ioaddr; //the base start address of IO memery
//我这里采用内存映射的方式
unsigned long mem_flag; //the flags of the IO port and IO memer
char mem[MEM_SIZE]; //define the buffer memmory whose size is MEM_SIZE
struct pci_dev *pci_dev;
struct cdev cdev; //thr struct of character device
};
一开始是先尝试通过字符设备来把数据读写到 char mem[MEM_SIZE]; ,这步没有问题!整个驱动程序都很正常!
然后我打算往PCI寄存器的里面读写,就是 void __iomem *ioaddr读写!发现有2个问题:
1:往寄存器里写还好说,要是读的话,事先并不知道是char型还是int或者double什么的,而 *ioaddr类型也是viod的,该怎么进行read呢?
2:我就自己瞎尝试着 void __iomem *ioaddr写入!把原型的字符设备的write函数改为如下:
ssize_t adl_write(struct file *filp,char __user *buf,size_t size,loff_t *ppos)
{
unsigned long p=*ppos;
unsigned int count=size;
int ret=0;
struct adlink_device *dev=filp->private_data;
printk(KERN_ALERT "adlink file_operation's write is called!n");
char *mem=(char *)dev->ioaddr;
if (p>dev->mem_len)
{
printk(KERN_ALERT "write %d bytes form %ldn",count,p);
return count ? - ENXIO:0;
}
if(count>dev->mem_len-p)
count=dev->mem_len-p;
if(copy_from_user(mem+p,buf,count))
ret=-EFAULT;
else
{
*ppos+=count;
ret=count;}
printk(KERN_ALERT "write %d bytes form %ldn",count,p);
return ret;
}
编译加载,并建立/dev/adlink 节点之后,我往该节点写入数据:比如echo “hello" > /dev/adlink
在输出:adlink file_operation's write is called后,不断输出:
write 0 bytes form 0
这是为什么呢?为什么不写入呢?麻烦各位大大帮忙分析分析啊
设备结构体如下:
struct adlink_device
{
unsigned long mem_len; //the lence of the memery region
void __iomem *ioaddr; //the base start address of IO memery
//我这里采用内存映射的方式
unsigned long mem_flag; //the flags of the IO port and IO memer
char mem[MEM_SIZE]; //define the buffer memmory whose size is MEM_SIZE
struct pci_dev *pci_dev;
struct cdev cdev; //thr struct of character device
};
一开始是先尝试通过字符设备来把数据读写到 char mem[MEM_SIZE]; ,这步没有问题!整个驱动程序都很正常!
然后我打算往PCI寄存器的里面读写,就是 void __iomem *ioaddr读写!发现有2个问题:
1:往寄存器里写还好说,要是读的话,事先并不知道是char型还是int或者double什么的,而 *ioaddr类型也是viod的,该怎么进行read呢?
2:我就自己瞎尝试着 void __iomem *ioaddr写入!把原型的字符设备的write函数改为如下:
ssize_t adl_write(struct file *filp,char __user *buf,size_t size,loff_t *ppos)
{
unsigned long p=*ppos;
unsigned int count=size;
int ret=0;
struct adlink_device *dev=filp->private_data;
printk(KERN_ALERT "adlink file_operation's write is called!n");
char *mem=(char *)dev->ioaddr;
if (p>dev->mem_len)
{
printk(KERN_ALERT "write %d bytes form %ldn",count,p);
return count ? - ENXIO:0;
}
if(count>dev->mem_len-p)
count=dev->mem_len-p;
if(copy_from_user(mem+p,buf,count))
ret=-EFAULT;
else
{
*ppos+=count;
ret=count;}
printk(KERN_ALERT "write %d bytes form %ldn",count,p);
return ret;
}
编译加载,并建立/dev/adlink 节点之后,我往该节点写入数据:比如echo “hello" > /dev/adlink
在输出:adlink file_operation's write is called后,不断输出:
write 0 bytes form 0
这是为什么呢?为什么不写入呢?麻烦各位大大帮忙分析分析啊
|
if (p>dev->mem_len)//这一句有很明显的问题!