平台驱动程序模板无法正常工作 [英] A platform driver template not working as it should

查看:104
本文介绍了平台驱动程序模板无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的平台驱动程序,但是我不知道为什么它不起作用.这是我尝试构建的代码. Insmod很干净,但是与rmmod一起存在一些隐秘消息.
以及我应该添加什么细节,以便该编辑器满足.........

I am trying to create a simple platform driver but i don't know why its not working. Here is the code that i am trying to build. Insmod is clean but there are some cryptic messages along with rmmod.
and what details i should add so this editor satisfies.........

    #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?

这是rmmod日志表单终端

This is the rmmod log form terminal

 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

我检查了/sys/modules并且这些模块仍然在rmmod之后,因此我每次都要更改名称.我需要知道 1)如果我正在做一些明显的错误 2)有没有办法了解这个错误日志 3)我的内核版本是否与此矿有关:3.2.0-29 任何提示都会有所帮助.

I checked /sys/modules and these modules are still after rmmod ,there so i have to change name everytime.I need to know 1) If I am doing some obvious error 2) Is there a way to understand this error logs 3) Is my kernel version have to do with this mine: 3.2.0-29 Any hint will be helpful..

推荐答案

代码中有两次更正.

1)在my_init()中,您应该在此处添加return函数,以避免在成功添加设备的情况下调用platform_device_put().

1) In my_init() you should add return function here to avoid calling platform_device_put() in case of success of device adding.

返回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;

2)在my_cleanup()中始终先注销所有设备,然后再注销驱动程序...

2) in my_cleanup() always unregistered all devices first and then unregistered your driver...

这篇关于平台驱动程序模板无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆