Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 平台驱动程序模板无法正常工作_C_Linux_Debugging_Linux Device Driver - Fatal编程技术网

C 平台驱动程序模板无法正常工作

C 平台驱动程序模板无法正常工作,c,linux,debugging,linux-device-driver,C,Linux,Debugging,Linux Device Driver,我试图创建一个简单的平台驱动程序,但我不知道为什么它不工作。下面是我试图构建的代码。 Insmod是干净的,但是rmmod中有一些隐藏的消息。 我应该添加哪些细节,以使这位编辑满意 #include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/platform_device.h> MODU

我试图创建一个简单的平台驱动程序,但我不知道为什么它不工作。下面是我试图构建的代码。 Insmod是干净的,但是rmmod中有一些隐藏的消息。
我应该添加哪些细节,以使这位编辑满意

    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/kernel.h>
    #include <linux/platform_device.h>
    MODULE_LICENSE("GPL");
    static struct platform_device *dev;

    static int  my_probe(struct platform_device *pdev)
    {
        printk("probe called, means device and driver are now associated\n");
        return 0;
    }

    static struct platform_driver my_driver = {
        .driver = {
            .name = "m_driver",
        },
        .probe = my_probe,
    };
    static int __init my_init(void)
    { 
        int ret;    
        /*register a platform driver*/
        ret = platform_driver_register(&my_driver); 
        if(ret == 0 )
            printk("<1>" "platform driver registered\n");

        dev = platform_device_alloc("m_driver", -1);
        if (!dev)
            return -ENOMEM;

        ret = platform_device_add(dev);
        if (ret != 0) {
            printk("<1>" "platform driver could not be added\n");   
            goto undo_malloc;
        }
        printk("<1>" "platform driver installed\n");
    undo_malloc:
        platform_device_put(dev);
        return ret;
    }
    module_init(my_init);

    static void __exit my_cleanup(void) 
    {
        /* Unregister driver */
        platform_driver_unregister(&my_driver);
        platform_device_unregister(dev);
        printk("<1>" "over\n"); 
        return;
    } 
    module_exit(my_cleanup);

Few part of the code is commented just to keep things minimal.After insmod ,in rmmod the dmesg is filled with messages as rmmod tainted and call trace and addresses .What i am doing wrong?
#包括
#包括
#包括
#包括
模块许可证(“GPL”);
静态结构平台\u设备*dev;
静态int my_探测器(结构平台设备*pdev)
{
printk(“已调用探测,表示设备和驱动程序现在已关联\n”);
返回0;
}
静态结构平台\u驱动程序我的\u驱动程序={
.司机={
.name=“m_驱动程序”,
},
.probe=我的探针,
};
静态int\uu init my\u init(void)
{ 
int ret;
/*注册平台驱动程序*/
ret=平台驱动程序寄存器(&my驱动程序);
如果(ret==0)
printk(“已注册的平台驱动程序”);
dev=平台、设备、所有(“m\U驱动程序”、-1);
如果(!dev)
return-ENOMEM;
ret=平台设备添加(开发);
如果(ret!=0){
printk(“无法添加平台驱动程序”);
goto undo_malloc;
}
printk(“已安装平台驱动程序”);
撤消\u malloc:
平台设备输出(dev);
返回ret;
}
模块_init(我的_init);
静态无效\u退出我的\u清理(无效)
{
/*注销驱动程序*/
平台\u驱动程序\u注销(&my\u驱动程序);
平台设备注销(dev);
printk(““”over\n”);
返回;
} 
模块退出(我的清理);
代码中很少有部分被注释只是为了让事情最小化。在insmod之后,在rmmod中,dmesg中充满了rmmod污染的消息以及调用跟踪和地址。我做错了什么?
这是rmmod日志表单终端

 WARNING: at /build/buildd/linux-3.2.0/fs/sysfs/inode.c:324 sysfs_hash_and_remove+0x92/0xa0()
[ 3106.861968] Hardware name: VMware Virtual Platform
[ 3106.861972] sysfs: can not remove 'driver', no directory
[ 3106.861976] Modules linked in: uiyn(O-) test1(O-) isofs vmwgfx ttm drm bnep rfcomm bluetooth snd_ens1371 gameport snd_ac97_codec ac97_bus snd_pcm ppdev snd_seq_midi snd_rawmidi snd_seq_midi_event snd_seq vmw_balloon psmouse snd_timer snd_seq_device serio_raw uvcvideo videodev snd soundcore snd_page_alloc i2c_piix4 parport_pc mac_hid shpchp lp parport pcnet32 mptspi mptscsih mptbase floppy [last unloaded: uiyn]
[ 3106.862043] Pid: 2600, comm: rmmod Tainted: G      D W  O 3.2.0-29-generic-pae #46-Ubuntu
[ 3106.862047] Call Trace:
[ 3106.862058]  [<c105a182>] warn_slowpath_common+0x72/0xa0
[ 3106.862069]  [<c11a59f2>] ? sysfs_hash_and_remove+0x92/0xa0
[ 3106.862077]  [<c11a59f2>] ? sysfs_hash_and_remove+0x92/0xa0
[ 3106.862084]  [<c105a253>] warn_slowpath_fmt+0x33/0x40
[ 3106.862090]  [<c11a59f2>] sysfs_hash_and_remove+0x92/0xa0
[ 3106.862099]  [<c11a7b50>] sysfs_remove_link+0x20/0x30
[ 3106.862112]  [<c13842da>] driver_sysfs_remove+0x2a/0x30
[ 3106.862119]  [<c1384304>] __device_release_driver+0x24/0xb0
[ 3106.862126]  [<c13843b4>] device_release_driver+0x24/0x40
[ 3106.862133]  [<c1383f2a>] bus_remove_device+0x5a/0x80
[ 3106.862140]  [<c1381c47>] device_del+0xe7/0x150
[ 3106.862147]  [<c13860a8>] platform_device_del+0x18/0x70
[ 3106.862153]  [<c13863b0>] platform_device_unregister+0x10/0x20
[ 3106.862163]  [<e0a6e02a>] my_cleanup+0xe/0xfe4 [uiyn]
[ 3106.862170]  [<e0a6e01c>] ? my_probe+0x1c/0x1c [uiyn]
[ 3106.862179]  [<c1094be5>] sys_delete_module+0x135/0x230
[ 3106.862189]  [<c111b63a>] ? do_munmap+0x16a/0x200
[ 3106.862197]  [<c15ac7df>] sysenter_do_call+0x12/0x28
[ 3106.862208] ---[ end trace 8a59e3572e4fd8eb ]---
[ 3106.863592] over
警告:at/build/buildd/linux-3.2.0/fs/sysfs/inode.c:324 sysfs\u hash\u and\u remove+0x92/0xa0()
[3106.861968]硬件名称:VMware虚拟平台
[3106.861972]系统:无法删除“驱动程序”,没有目录
[3106.861976]链接到的模块:uiyn(O-)test1(O-)isofs vmwgfx ttm drm bnep rfcomm蓝牙snd_ens1371游戏端口snd_ac97_编解码器ac97_总线snd_pcm ppdev snd_seq midi snd_rawmidi snd_seq midi_事件snd_seq vmw_气球psmouse snd_定时器snd_seq设备序列原始uvcvideo视频开发snd soundcore snd页面ALLOCC i2c piix4 PARU pc mac SHP SPIMPT端口32软盘ETH[上次卸载:uiyn]
[3106.862043]Pid:2600,通信:rmmod污染:G D W O 3.2.0-29-generic-pae#46 Ubuntu
[3106.862047]呼叫跟踪:
[3106.862058][]警告慢路径公共+0x72/0xa0
[3106.862069][]?系统哈希和删除+0x92/0xa0
[3106.862077][]?系统哈希和删除+0x92/0xa0
[3106.862084][]警告慢路径fmt+0x33/0x40
[3106.862090][]系统哈希和删除+0x92/0xa0
[3106.862099][]系统删除链接+0x20/0x30
[3106.862112][]驱动程序系统删除+0x2a/0x30
[3106.862119][]设备释放驱动程序+0x24/0xb0
[3106.862126][]设备释放驱动程序+0x24/0x40
[3106.862133][]总线移除设备+0x5a/0x80
[3106.862140][]设备删除+0xe7/0x150
[3106.862147][]平台设备数据集+0x18/0x70
[3106.862153][]平台设备注销+0x10/0x20
[3106.862163][]我的清除+0xe/0xfe4[uiyn]
[3106.862170][]?我的探头+0x1c/0x1c[uiyn]
[3106.862179][]系统删除模块+0x135/0x230
[3106.862189][]?do_munmap+0x16a/0x200
[3106.862197][]系统输入\u do\u调用+0x12/0x28
[3106.862208]--[end trace 8a59e3572e4fd8eb]---
[3106.863592]完毕
我检查了/sys/modules,这些模块仍然在rmmod之后,所以每次我都必须更改名称。我需要知道 1) 如果我犯了一些明显的错误 2) 是否有办法了解此错误日志 3) 我的内核版本和我的3.2.0-29有关吗
任何提示都会很有帮助。

如果在删除模块(rmmod)时出现错误,则您的_exit()中一定有问题。根据您的_init(),您首先注册驱动程序,然后添加设备。因此,在注销驱动程序之前必须先注销设备。”

platform_device_unregister(dev);
platform_driver_unregister(&my_driver);

代码中的两个更正

1) 在my_init()中,您应该在此处添加返回函数,以避免在设备添加成功的情况下调用平台_device_put()

返回ret;//如果设备添加成功,则返回。

    ret = platform_device_add(dev);
    if (ret != 0) {
        printk("<1>" "platform driver could not be added\n");   
        goto undo_malloc;
    }
    printk("<1>" "platform driver installed\n");

    return ret; // if device is added succesfully then return.

undo_malloc:
    platform_device_put(dev);
    return ret;
ret=platform\u device\u add(dev);
如果(ret!=0){
printk(“无法添加平台驱动程序”);
goto undo_malloc;
}
printk(“已安装平台驱动程序”);
return ret;//如果设备添加成功,则返回。
撤消\u malloc:
平台设备输出(dev);
返回ret;
2) 在my_cleanup()中,始终先注销所有设备,然后注销驱动程序