菜小白笔记——解决Mini2440 USB下载线驱动的问题——insmod: ERROR: could not insert module dnw_usb.ko: Invalid module format

以下是Mini2440 USB下载线驱动的下载地址:

链接:https://pan.baidu.com/s/1yTNUtQDI5dQhS4SOeNXiGg 
提取码:zmqh

 

因为刚学Linux,所以用友善之臂的Mini2440开发板作为入门,但是在安装Usb下载线驱动的时候,也就是sudo insmod dnw_usb.ko时,

出现了:insmod: ERROR: could not insert module dnw_usb.ko: Invalid module format 错误

对于我这个小白来说,太难了,搞了好几天,试了好多种方法都不行,但最终搞出来了!!!

经过这几天查资料,说是这个dnw_usb.ko文件是以前版本的内核编译的,跟现在的内核版本不同,所以要报错。

所以我下面用到的方法就是重新编译一下这个dnw_usb.ko文件:

1.在桌面创建一个dnw_usb.c文件

输入:touch dnw_usb.c

然后我在网上找到了dnw_usb驱动的c源码,将之复制到桌面的dnw_usb.c文件中保存:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
 
#define SECBULK_MAJOR    102
#define SECBULK_MINOR    0
#define DRIVER_NAME    "secbulk"
 
#define BULKOUT_BUFFER_SIZE    512
 
struct secbulk_dev
{
    struct usb_device *udev;
    struct mutex io_mutex;
    char*    bulkout_buffer;
    __u8    bulk_out_endpointAddr;
};
 
static struct usb_class_driver secbulk_class;
 
static struct usb_device_id secbulk_table[]= {
    { USB_DEVICE(0x04e8, 0x1234)},        //c
    { }
};
 
static struct usb_driver secbulk_driver;
static void secbulk_disconnect(struct usb_interface *interface)
{
    struct secbulk_dev *dev = NULL;
    printk(KERN_INFO "secbulk:secbulk disconnected!\n");
    dev = usb_get_intfdata(interface);
    if( NULL != dev )
        kfree(dev);
    usb_deregister_dev(interface, &secbulk_class);
    return;
}
 
static ssize_t secbulk_read(struct file *file, char __user *buf, size_t len, loff_t *loff)
{
    return -EPERM;
}
 
static ssize_t secbulk_write(struct file *file, const char __user *buf, size_t len, loff_t *loff)
{
    size_t to_write;
    struct secbulk_dev *dev = file->private_data;
    int ret;
    int actual_length;
    size_t total_writed;
 
    total_writed = 0;
    while(len > 0)
    {
        to_write = min(len, BULKOUT_BUFFER_SIZE);
 
        if(copy_from_user(dev->bulkout_buffer, buf+total_writed, to_write))    //得到用户空间数据
        {
            printk(KERN_ERR "secbulk:copy_from_user failed!\n");
            return -EFAULT;
        }
 
        ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),    //构造批量传输信息,传输给usbCore处理
                dev->bulkout_buffer,
                to_write,
                &actual_length,
                3*HZ);
        if(ret || actual_length!=to_write)
        {
            printk(KERN_ERR "secbulk:usb_bulk_msg failed!\n");
            return -EFAULT;
        }
        len -= to_write;
        total_writed += to_write;
    }
    return total_writed;
}
 
static int secbulk_open(struct inode *node, struct file *file)
{
    struct usb_interface *interface;
    struct secbulk_dev *dev;
 
    interface = usb_find_interface(&secbulk_driver, iminor(node));
    if(!interface)
        return -ENODEV;
 
    dev = usb_get_intfdata(interface);
    dev->bulkout_buffer = kzalloc(BULKOUT_BUFFER_SIZE, GFP_KERNEL);
    if(!(dev->bulkout_buffer))
        return -ENOMEM;
    if(!mutex_trylock(&dev->io_mutex))
        return -EBUSY;
    file->private_data = dev;
    return 0;
}
 
static int secbulk_release(struct inode *node, struct file *file)
{
    struct secbulk_dev *dev;
 
    dev = (struct secbulk_dev*)(file->private_data);
    kfree(dev->bulkout_buffer);
    mutex_unlock(&dev->io_mutex);
    return 0;
}
 
static struct file_operations secbulk_fops = {
    .owner     =    THIS_MODULE,
    .read     =    secbulk_read,
    .write    =    secbulk_write,
    .open   =    secbulk_open,
    .release=    secbulk_release,
};
 
static struct usb_class_driver secbulk_class = {
    .name =     "secbulk%d",
    .fops =        &secbulk_fops,
    .minor_base=    100,
};
 
static int secbulk_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
    int ret;
    struct secbulk_dev *dev;
    struct usb_host_interface *iface_desc;
    struct usb_endpoint_descriptor *endpoint;
    int i;
 
    printk(KERN_INFO "secbulk:secbulk probing...\n");
 
    dev = kzalloc(sizeof(*dev), GFP_KERNEL);        //分配一个指针空间,只是为了索引内核的空间
    if(!dev)
    {
        ret = -ENOMEM;
        goto error;
    }
 
    iface_desc = interface->cur_altsetting;
    for(i=0; i < iface_desc->desc.bNumEndpoints; i++) //从USBCore传入的借口中得到得到输出端点
    {
        endpoint = &(iface_desc->endpoint[i].desc);
        if(!dev->bulk_out_endpointAddr
        && usb_endpoint_is_bulk_out(endpoint))
        {
            printk(KERN_INFO "secbulk:bulk out endpoint found!\n");
            dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
            break;
        }
    }
 
    if(!(dev->bulk_out_endpointAddr))
    {
        ret = -EBUSY;
        goto error;
    }
 
    ret = usb_register_dev(interface, &secbulk_class);    //将usb驱动注册成字符设备驱动提供给用户程序
    if(ret)
    {
        printk(KERN_ERR "secbulk: usb_register_dev failed!\n");
        return ret;
    }
    dev->udev = usb_get_dev(interface_to_usbdev(interface));
 
    usb_set_intfdata(interface, dev);
 
    mutex_init(&dev->io_mutex);
    return 0;
error:
    if(!dev)
        kfree(dev);
    return ret;
}
 
static struct usb_driver secbulk_driver= {
    .name=        "secbulk",
    .probe=        secbulk_probe,
    .disconnect=    secbulk_disconnect,
    .id_table=    secbulk_table,
    .supports_autosuspend=0,
};
static int __init secbulk_init(void)
{
    int result;
    printk(KERN_INFO "secbulk:secbulk loaded\n");
    result = usb_register(&secbulk_driver);        //1 注册 secbulk_driver, 此处我们就注册了usb_driver,当插入USB后,
                                                //会调用add_dev,然后将ID号与驱动支持的id进行对比,如果对比通过,就会调用probe
    if(result)
    {    printk(KERN_ERR "secbulk:usb_register failed: %d", result);
        return result;
    }
    return 0;
}
 
static void __exit secbulk_exit(void)
{
    usb_deregister(&secbulk_driver);
    printk(KERN_INFO "secbulk:secbulk unloaded\n");
}
 
module_init(secbulk_init);
module_exit(secbulk_exit);
MODULE_LICENSE("GPL");

2.然后接着在桌面上创建一个Makefile:

输入:touch Makefile

然后将如下代码复制进去:

注意:下面的4.15.0-112要你自己在终端输入uname -r 查询,下面以我的版本号为例

obj-m:=dnw_usb.o
KERNELDIR:= /usr/src/linux-headers-4.15.0-112-generic
PWD :=$(shell pwd)

all:
        make -C $(KERNELDIR) M=$(PWD) modules

clean:
        make -C $(KERNELDIR) M=$(PWD) clean

注意:all和clean下面是TAB键

3.然后在桌面下,

输入:make

出现这个就表示重现编译成功了

4.然后再sudo insmod dnw_usb.ko就不会报错了!

哭了...

5.输入lsmod验证

其中出现的dnw_usb,就是我们刚刚安装的,说明安装成功了

 

posted @ 2020-07-27 17:58  菜小白king  阅读(809)  评论(1编辑  收藏  举报