免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2101 | 回复: 4
打印 上一主题 下一主题

linux PCI驱动 无法访问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-21 09:38 |只看该作者 |倒序浏览
本帖最后由 wucongdonglai 于 2011-01-21 09:51 编辑

哪位大大有时间帮忙看看我写的无奈驱动啊!好不容易,总算是写下来了,然后运行加载,看上去都蛮正常的,然后在mknod /dev/adlink c 240 0
在/dev/下也产生了节点adlink,但是比如cat /dev/adlink,结果被告知:没有那设备或地址,这是怎么回事啊?还是我的源码有问题?哎,真不好搞啊!什么消息也没显示出来,怎么整啊?
#include <linux/pci.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
//#include <linux/config.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/compiler.h>
#include <linux/completion.h>
#include <linux/mii.h>
#include <linux/cdev.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wu Donglei");

#define ADLINK_VENDOR_ID  0x144a
#define ADLINK_DEVICE_ID  0x9221
#define MODULE_NAME "adlinkdr"
#define DEVICE_NAME "programe9221"

static struct pci_device_id adl_pci_tbl [] = {
{ADLINK_VENDOR_ID,ADLINK_DEVICE_ID,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  {0,}
};
MODULE_DEVICE_TABLE(pci,adl_pci_tbl);

#define MEM_SIZE 512 //the size of buffer memory
struct adlink_device
{
  struct pci_dev *p_dev;
  char *mem;     //define the buffer memmory whose size is MEM_SIZE
  struct cdev *pcdev;      //thr struct of character device
};

struct adlink_device adl_dev;
//file_operation's member function open
static int adl_open(struct inode *inode,struct file *pfile)
{
  struct adlink_device *padl;
  padl=container_of(inode->i_cdev,struct adlink_device,pcdev);
  pfile->private_data=padl;
  printk(KERN_ALERT "adlink file_operation's open is called!\n");
  return 0;
}

//file_operation's member function read
ssize_t adl_read(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 read is called!\n");
  if (p>MEM_SIZE)
    return count ? - ENXIO:0;
  if(count>MEM_SIZE-p)
    count=MEM_SIZE-p;
  if(copy_to_user(buf,(void*)(dev->mem+p),count))
    ret=-EFAULT;
  else
  {
    *ppos+=count;
    ret=count;}
  printk(KERN_ALERT "read %d bytes form %ld\n",count,p);
  return ret;
}

//file_operation's member function 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");
  if (p>MEM_SIZE)
    return count ? - ENXIO:0;
  if(count>MEM_SIZE-p)
    count=MEM_SIZE-p;
  if(copy_from_user(dev->mem+p,buf,count))
    ret=-EFAULT;
  else
  {
    *ppos+=count;
    ret=count;}
  printk(KERN_ALERT "write %d bytes form %ld\n",count,p);
  return ret;
}

//file_operation's member function close
static int adl_release(struct inode *inode,struct file *filp)
{
  printk(KERN_ALERT "adlink file_operation's ioctl is called!\n");
  filp->private_data=0;
  return 0;
}

static struct file_operations adl_fops={
  .owner= THIS_MODULE,
  .read=  adl_read,
  .write= adl_write,
  .open= adl_open,
  .release= adl_release,
};
static dev_t devno=240; //character device's major number
//define and register the character device
int adl_char_init(void)
{
  int result=0;  //used for checking the result of called functionsi
  printk(KERN_ALERT "it is adl_char_init \n");
  //struct cdev ncdev;
  if(devno)
    result=register_chrdev_region(MKDEV(devno,0),1,"adlink");
  else
  {
    result=alloc_chrdev_region(&devno,0,1,"adlink");
    devno=MAJOR(devno);
  }
  printk(KERN_ALERT"result is %d !\n",result);
  if (result<0)
    return result;
  adl_dev.mem=(char *)vmalloc(MEM_SIZE);
  adl_dev.pcdev=cdev_alloc();
  printk(KERN_ALERT "the adl_dev.pcdev is %lx\n",(long)adl_dev.pcdev);
  (*adl_dev.pcdev).owner= THIS_MODULE;
  (*adl_dev.pcdev).ops= &adl_fops;
  if(cdev_add(adl_dev.pcdev,devno,1))
    printk(KERN_NOTICE"Error in add cdev !\n");
  else printk(KERN_NOTICE"cdev is added!\n");
  printk(KERN_ALERT "the adl_dev.pcdev is %lx\n",(long)adl_dev.pcdev);
  return 0;
}

//driver's member function prode
static int __devinit adl_probe(struct pci_dev *pdev,const struct pci_device_id *pci_id)
{
  if(pci_enable_device(pdev))
  {
    return -EIO;
  };
  pci_set_master(pdev);
  adl_dev.p_dev=pdev;
  if(adl_char_init())
  {
    printk(KERN_ALERT "%s has no failed! \n",pci_name(pdev));
    return -EIO;
  }
  return 0;
}


//driver's member function remove
static void __devexit adl_remove(void)
{
  printk(KERN_ALERT "the adl_dev.pcdev is %lx\n",(long)adl_dev.pcdev);
  cdev_del(adl_dev.pcdev);  //remove the character device
  printk(KERN_ALERT "the adl_dev.pcdev is %lx\n",(long)adl_dev.pcdev);
  vfree(adl_dev.mem);
  kfree(adl_dev.pcdev);
  unregister_chrdev_region(MKDEV(devno,0),1);
  printk(KERN_ALERT "adlink modules is removed!\n");
}


//the detail of the driver module
static struct pci_driver adl_pci_driver= {
  .name = MODULE_NAME,
  .id_table = adl_pci_tbl,
  .probe = adl_probe,
  .remove = adl_remove,
};
//register adlink driver module
static int __init adl_init_module(void)
{
  printk(KERN_ALERT "adlink modules is inited!\n");
  return pci_register_driver(&adl_pci_driver);
}

//remove adlink driver module
static void __exit adl_exit_module(void)
{
  printk(KERN_ALERT "the adl_dev.pcdev is %lx\n",(long)adl_dev.pcdev);
  pci_unregister_driver(&adl_pci_driver);
}


module_init(adl_init_module);
module_exit(adl_exit_module);

论坛徽章:
0
2 [报告]
发表于 2011-01-21 11:47 |只看该作者
这个和你的代码没有关系,是不是你搞错了,应该在板子上mknod

论坛徽章:
0
3 [报告]
发表于 2011-01-21 11:52 |只看该作者
回复 2# 0vk0


    应该没有,我没有到具体的开发板上,我的就是在我的电脑上插了一个凌华的采集卡,然后做它的驱动

论坛徽章:
0
4 [报告]
发表于 2011-01-21 14:21 |只看该作者
回复  0vk0


    应该没有,我没有到具体的开发板上,我的就是在我的电脑上插了一个凌华的采集卡,然后 ...
wucongdonglai 发表于 2011-01-21 11:52



    试试重新mount 一下它

论坛徽章:
0
5 [报告]
发表于 2011-01-21 16:38 |只看该作者
解决了,是我在设备号上面有问题,哈哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP