0

我正在为自定义设备开发此模块,实际上,这是一个连接到 ISA 总线的 4*8 位 io 端口,地址为 0x0120 - 0x0123。该驱动程序基于 Alessandro Rubini 和 Jonathan Corbet 的“scull”。我的操作系统是 Ubuntu 10.04,内核是 2.6.32-74 通用的,我使用内置的面向控制台的编译器 gcc。使用“insmod”插入已编译模块时,出现错误“错误地址”并且模块未加载。我尝试使用“printk”对其进行调试,发现我的模块成功获取了一系列 io 端口、主要和次要编号,然后,当尝试执行“Reset_Port”功能时,它会生成错误“错误地址”并退出.

谁能告诉我,我做错了什么?

这是我的模块的 __exit 和 __init 函数

void __exit ET3201_exit(void)
{
        int i;
        dev_t devno = MKDEV(ET3201_major, ET3201_minor);

        /* Get rid of our char dev entries */
        if (ET3201_devices) {
                for (i = 0; i < ET3201_nr_devs; i++) {
                        ET3201_trim(ET3201_devices + i);
                        cdev_del(&ET3201_devices[i].cdev);
                }
                kfree(ET3201_devices);
        }

#ifdef ET3201_DEBUG /* use proc only if debugging */
        ET3201_remove_proc();
#endif

        /* cleanup_module is never called if registering failed */
        unregister_chrdev_region(devno, ET3201_nr_devs);
    if ( ! port ) release_region(BaseIO, 8);
    printk(KERN_INFO "Goodbye, cruel world - ET3201 is unloaded\n");

        /* and call the cleanup functions for friend devices */
        /*ET3201_access_cleanup();*/
}
/*----------------------------------------------------------------------------*/
/* Set up the char_dev structure for this device. */
static void ET3201_setup_cdev(struct ET3201_dev *dev, int index)
{
        int err, devno = MKDEV(ET3201_major, ET3201_minor + index);

        cdev_init(&dev->cdev, &ET3201_fops);
        dev->cdev.owner = THIS_MODULE;
        dev->cdev.ops = &ET3201_fops;
    dev->CAMAC_Module_Number = CAMAC_Nmod;
    dev->CAMAC_Command_Adress = CAMAC_Adcom;
    dev->Driver_Number = ET3201_minor + index;
        err = cdev_add (&dev->cdev, devno, 1);
        /* Fail gracefully if need be */
        if (err)
                printk(KERN_NOTICE "Error %d adding ET3201%d", err, index);
}
/*----------------------------------------------------------------------------*/
int __init ET3201_init(void)
{
    int result = 0;
    int i;
    dev_t dev = 0;
    BaseIO = Base;  
    /* Get a range of minor numbers to work with, asking for a dynamic
    major unless directed otherwise at load time. */
    if (ET3201_major) {
           dev = MKDEV(ET3201_major, ET3201_minor);
       result = register_chrdev_region(dev, ET3201_nr_devs, "ET3201");
    } else {
           result = alloc_chrdev_region(&dev, ET3201_minor, ET3201_nr_devs, "ET3201");
       ET3201_major = MAJOR(dev);
    }
    if (result < 0) {
       printk(KERN_WARNING "ET3201: can't get major %d\n", ET3201_major);
       return result;
    }     
    port = request_region(BaseIO, 8, "ET3201");
    if ( port == NULL ) {
                        printk(KERN_WARNING "ET3201 cannot reserve i-o ports %lu \n", BaseIO);
                        return -ENODEV; 
            goto fail;
                }
         /*
         * allocate the devices -- we can't have them static, as the number
         * can be specified at load time
         */
        ET3201_devices = kmalloc(ET3201_nr_devs * sizeof(struct ET3201_dev), GFP_KERNEL);
        if (! ET3201_devices) {
                result = -ENOMEM;
        printk(KERN_ALERT "ET3201: can't get memory \n");
                goto fail; /* Fail gracefully if need be */
        }
        memset(ET3201_devices, 0, ET3201_nr_devs * sizeof(struct ET3201_dev));

        /* Initialize each device. */
        for (i = 0; i < ET3201_nr_devs; i++) {
                ET3201_devices[i].quantum = ET3201_quantum;
                ET3201_devices[i].qset = ET3201_qset;
                init_MUTEX(&ET3201_devices[i].sem);
                ET3201_setup_cdev(&ET3201_devices[i], i);
        }

        /* At this point call the init function for any friend device */
        dev = MKDEV(ET3201_major, ET3201_minor + ET3201_nr_devs);
        /*dev += ET3201_access_init(dev);*/

    printk(KERN_INFO "ET3201 is initialized with major %d\n", ET3201_major);
    if ( port != NULL ){
        printk(KERN_INFO "ET3201 is trying to reset %d devices\n", ET3201_nr_devs);
        result = Reset_Port();
    }
        if ( result != 0 ) {
        printk(KERN_ALERT "ET3201: device cannot reset with result %d\n", result);
                result = -EFAULT;
                goto fail;  
        }
#ifdef ET3201_DEBUG /* only when debugging */
        ET3201_create_proc();
#endif

        return 0; /* succeed */

  fail:
        ET3201_exit();
    return result;
}
/*----------------------------------------------------------------------------*/
module_init(ET3201_init);
module_exit(ET3201_exit);
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(ET3201_minor);

接下来是 Reset_Port()

static int Reset_Port(void)
{
int result = -EIO;
int count;
if (port == NULL) goto fail;
for ( count = 0; count < ET3201_nr_devs; count++ )
{
outb(0x00, ports[count]);
}
wmb();          /*write memory barrier*/
LastOp = E_Reset;
result = 0;     /* success  */
fail:   
return result;
}
EXPORT_SYMBOL(Reset_Port);

现在,在修复“int Reset_Port(void)”之后,我遇到了另一个问题——
“警告:modpost:发现 1 个部分不匹配。” 调试后,我发现这是从“module_init()”调用“ET3201_exit()”的结果——当我评论这个调用时,警告消失了。令人惊讶的是,在受人尊敬的作者的“scull”驱动程序中做出了完全相同的调用 - 它有效。问题:什么会导致此代码中的内核不匹配?

4

1 回答 1

1

是的!该错误已修复 - 在声明“int Reset_Port(void)”后,模块已成功插入和移除。我认为(但这是错误的)不能将所有可以从“ module_init() ”中调用的函数声明为静态的。

于 2015-10-26T19:17:09.543 回答