linux的PCI驱动的一些莫名其妙的问题

12 views
Skip to first unread message

donglei wu

unread,
Jan 12, 2011, 3:49:12 AM1/12/11
to linux-...@zh-kernel.org
看了一阵子linux驱动的相关知识,今天打算自己写一个大体框架,然后在往里慢慢填充!结果问题一大堆,而且都莫名其妙!
我用的内核版本是2.6.15.5,系统的fedora 7,编译命令都相当于
make -C /usr/src/linux-2.6.15.5 M=(shell pwd) modules

我是照着linux设备驱动程序里的pci例子写的:源码如下:
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>

static struct pci_device_id ids[] = {
{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3), },
{ 0, }
};
MODULE_DEVICE_TABLE(pci, ids);
static unsigned char skel_get_revision(struct pci_dev *dev)
{
u8 revision;
pci_read_config_byte(dev, PCI_REVISION_ID, &revision);
return revision;
}
static int probe(struct pci_dev *dev, const struct pci_device_id *id)
{
/* Do probing type stuff here.
* Like calling request_region();
*/
pci_enable_device(dev);

if (skel_get_revision(dev) == 0x42)
return -ENODEV;

return 0;
}
static void remove(struct pci_dev *dev)
{
/* clean up any allocated resources and stuff here.
* like call release_region();
*/
}
static struct pci_driver pci_driver = {
.name = "pci_skel",
.id_table = ids,
.probe = probe,
.remove = remove,
};
static int __init pci_skel_init(void)
{
return pci_register_driver(&pci_driver);
}
static void __exit pci_skel_exit(void)
{
pci_unregister_driver(&pci_driver);
}
MODULE_LICENSE("GPL");
module_init(pci_skel_init);
module_exit(pci_skel_exit);

运行编译,一点问题都没有,没有任何错误和警告就完成了!
但是我的和它基本一样,错误很多:
错误1:
我的源码如下:
79 #define ADLINK_VENDOR_ID 0x1022;
80 #define ADLINK_DEVICE_ID 0x2000;
81
82
83 static struct pci_device_id adl_pci_tbl [] = {
84 {ADLINK_VENDOR_ID,ADLINK_DEVICE_ID,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
85 // {0x1022,0x2000,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
86 {0,}
87 };
88 MODULE_DEVICCE_TABLE(pci,adl_pci_tbl);
它就显示错误:
/home/gm/tmp/adl/adlink.c:85: 错误:expected ‘}’ before ‘;’ token
/home/gm/tmp/adl/adlink.c:87: 警告:标量初始化带花括号
/home/gm/tmp/adl/adlink.c:87: 警告:(在 ‘adl_pci_tbl[0].subvendor’ 的初始化附近)
/home/gm/tmp/adl/adlink.c:89: 警告:数据定义时没有类型或存储类
/home/gm/tmp/adl/adlink.c:89: 警告:在 ‘MODULE_DEVICCE_TABLE’ 的声明中,类型默认为 ‘int’
/home/gm/tmp/adl/adlink.c:89: 警告:函数声明中出现形参名却未指定类型
但是如果我把85行给注释掉,采用86行,那么前3个错误就没有了,不就是2个宏定义么,怎么就不行呢?想不通!
后面3个警告,我把例子程序的源码段改为
static struct pci_device_id ids[] = {
// { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
PCI_DEVICE_ID_INTEL_82801AA_3), },
{ 0x8086,0x262f,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
{ 0, }
};
然后编译,也照样通过啊,为什么我的就是不行呢?会有那后面3个警告呢?想不明白
错误2:源码如下:
131 static struct file_operations adl_fops={
132 owner: THIS_MODULE,
133 read: adl_read,
134 write: adl_write,
135 open: adl_open,
136 ioctl: adl_ioctl,
137 release: adl_release,
138 };
……
157 static struct pci_driver adl_pci_driver= {
158 .name = MODULE_NAME,
159 .id_table = adl_pci_tbl,
160 .probe = adl_probe,
161 .remove = adl_remove,
162 };

/home/gm/tmp/adl/adlink.c:133: 警告:从不兼容的指针类型初始化
/home/gm/tmp/adl/adlink.c:134: 警告:从不兼容的指针类型初始化
/home/gm/tmp/adl/adlink.c:136: 警告:从不兼容的指针类型初始化
/home/gm/tmp/adl/adlink.c:137: 警告:从不兼容的指针类型初始化
/home/gm/tmp/adl/adlink.c:161: 警告:从不兼容的指针类型初始化
我故意采用2种方式定义,但是好像都有这个警告,但是不明白为什么有的就有警告,有的就没有警告!纳闷啊!但是例子程序就好好的啊!搞不明白,都是在我同一台电脑上啊!哪位大大帮忙看看啊!
_______________________________________________
Linux 内核开发中文邮件列表
Linux-...@zh-kernel.org
http://zh-kernel.org/mailman/listinfo/linux-kernel
Linux 内核开发中文社区: http://zh-kernel.org

Li Jie

unread,
Jan 12, 2011, 4:04:02 AM1/12/11
to donglei wu, linux-...@zh-kernel.org
2011/1/12 donglei wu <wucong...@gmail.com>:

> 79  #define ADLINK_VENDOR_ID  0x1022;
> 80  #define ADLINK_DEVICE_ID  0x2000;

去掉后面的分号


> 错误2:源码如下:
> 131  static struct file_operations adl_fops={
> 132    owner: THIS_MODULE,
> 133    read:  adl_read,
> 134    write: adl_write,
> 135    open: adl_open,
> 136    ioctl: adl_ioctl,
> 137    release: adl_release,
> 138  };
> ……
> 157  static struct pci_driver adl_pci_driver= {
> 158    .name = MODULE_NAME,
> 159    .id_table = adl_pci_tbl,
> 160    .probe = adl_probe,
> 161    .remove = adl_remove,
> 162  };
>
> /home/gm/tmp/adl/adlink.c:133: 警告:从不兼容的指针类型初始化
> /home/gm/tmp/adl/adlink.c:134: 警告:从不兼容的指针类型初始化
> /home/gm/tmp/adl/adlink.c:136: 警告:从不兼容的指针类型初始化
> /home/gm/tmp/adl/adlink.c:137: 警告:从不兼容的指针类型初始化
> /home/gm/tmp/adl/adlink.c:161: 警告:从不兼容的指针类型初始化

你要贴出你的函数声明来.

--
Regards
Li Jie

donglei wu

unread,
Jan 12, 2011, 7:44:12 PM1/12/11
to Li Jie, linux-...@zh-kernel.org
汗,居然是多了两分号,丢人丢大发了,典型的灯下黑!我的头文件列表,基本上能加的都加了,好像比较有用的是前5个:
#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/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.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>

那我这警告是怎么回事呢?


/home/gm/tmp/adl/adlink.c:89: 警告:数据定义时没有类型或存储类
/home/gm/tmp/adl/adlink.c:89: 警告:在 ‘MODULE_DEVICCE_TABLE’ 的声明中,类型默认为 ‘int’
/home/gm/tmp/adl/adlink.c:89: 警告:函数声明中出现形参名却未指定类型

对照源码也没有错误啊:
89 MODULE_DEVICCE_TABLE(pci,adl_pci_tbl);
另外,为什么会出现 警告:从不兼容的指针类型初始化!搞不定啊!

Rain MaoMao

unread,
Jan 13, 2011, 3:12:51 AM1/13/11
to donglei wu, linux-...@zh-kernel.org
没事,不丢人。这个错误每个c程序员都犯过。这个错误应该说是c语言本身的设计缺陷。
类似的缺陷还有一些,比如:
if (a=NULL)
{
}
else
{
a->x 就死翘翘了

E=MaC^2s

unread,
Jan 13, 2011, 5:10:37 AM1/13/11
to Rain MaoMao, donglei wu, linux-...@zh-kernel.org
NULL==a
好习惯是避免低级错误的方法。
Reply all
Reply to author
Forward
0 new messages