免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5493 | 回复: 7
打印 上一主题 下一主题

insmod错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-10 13:26 |只看该作者 |倒序浏览
本帖最后由 createwindow 于 2011-05-12 09:30 编辑

以下为代码
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>

  7. MODULE_LICENSE ("GPL");

  8. int hello_major = 120;
  9. int hello_minor = 0;
  10. int number_of_devices = 1;

  11. struct cdev cdev;
  12. static dev_t dev;

  13. struct file_operations hello_fops = {
  14.       .owner = THIS_MODULE
  15. };

  16. static void char_reg_setup_cdev (void)
  17. {
  18.    int error;
  19.    cdev_init (&cdev, &hello_fops);
  20.    cdev.owner = THIS_MODULE;
  21.    cdev.ops = &hello_fops;
  22.    error = cdev_add (&cdev, dev, 1);
  23.    if (error)
  24.        printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);

  25. }

  26. struct class *my_class;

  27. static int __init hello_2_init (void)
  28. {
  29.     int result;
  30.     dev = MKDEV(hello_minor, hello_minor);
  31.     result = register_chrdev_region (dev, number_of_devices, "hello");
  32.     if (result<0) {
  33.         printk (KERN_WARNING "hello: can't get major number %d\n", hello_major);
  34.         return result;
  35.      }

  36.     char_reg_setup_cdev ();

  37.     /* create your own class under /sysfs */
  38.     my_class = class_create(THIS_MODULE, "my_class");
  39.     if(IS_ERR(my_class))
  40.     {
  41.         printk("Err: failed in creating class.\n");
  42.         return -1;
  43.     }

  44.     /* register your own device in sysfs, and this will cause udev to create corresponding device node */
  45.     device_create( my_class, NULL, dev, "hello%d", 0 );

  46.     printk (KERN_INFO "Registered character driver\n");
  47.     return 0;
  48. }

  49. static void __exit hello_2_exit (void)
  50. {
  51.     cdev_del (&cdev);
  52.     device_destroy(my_class, dev);   
  53.     class_destroy(my_class);                              
  54.     unregister_chrdev_region (dev, number_of_devices);

  55.     printk (KERN_INFO "char driver cleaned up\n");
  56. }

  57. module_init (hello_2_init);
  58. module_exit (hello_2_exit);
复制代码
成功编译后,insmod hello.ko,只提示"killed".
若再次insmod hello.ko则会提示insmod error inserting 'hello.ko ':-1 file exists。运行lsmod |grep hello,显示
hello                   2849  1
如果rmmod hello.ko,则提示ERROR: Module hello is in use

内核版本为2.6.38.2

请大家指教。

论坛徽章:
0
2 [报告]
发表于 2011-05-12 07:51 |只看该作者
有人帮忙吗

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
3 [报告]
发表于 2011-05-12 12:55 |只看该作者
写个简单的调试一下吧,一个helloworld写这么多

论坛徽章:
0
4 [报告]
发表于 2011-05-12 18:45 |只看该作者
建议先手动创建设备节点,OK后再自动创建,step by step。
建议用动态的方式分配dev_t
建议注意一些常用的内核编程习惯

论坛徽章:
0
5 [报告]
发表于 2011-05-12 22:17 |只看该作者
回复 3# dreamice


试过简单的了,下面是改过的版本。问题依旧。但是删除device_create()后,结果是正常的。非常奇怪
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/slab.h>
  7. #include <linux/device.h>

  8. MODULE_LICENSE ("GPL");

  9. static struct cdev ctest;
  10. static dev_t devno;

  11. struct file_operations hello_fops = {
  12.       .owner = THIS_MODULE,
  13. };


  14. struct class *my_class;

  15. int __init heiman_init (void)
  16. {
  17.     int error;
  18.    
  19.     if (alloc_chrdev_region(&devno, 0, 1, "heiman") < 0) {
  20.         printk(KERN_DEBUG "CAN'T register a device\n");
  21.         return -1;
  22.     }

  23.     my_class = class_create(THIS_MODULE, "heiman");
  24.     cdev_init (&ctest, &hello_fops);
  25.     ctest.owner = THIS_MODULE;
  26.     error = cdev_add (&ctest, devno, 1);
  27.     device_create( my_class, NULL, devno, "heiman%d", 0);

  28.     printk (KERN_INFO "Registered character driver\n");
  29.     return 0;
  30. }

  31. static void __exit heiman_exit (void)
  32. {
  33.     unregister_chrdev_region(devno, 1);
  34.     device_destroy(my_class, devno);   
  35.     cdev_del(&ctest);
  36.     class_destroy(my_class);         

  37.     printk (KERN_INFO "char driver cleaned up\n");
  38.     return;
  39. }

  40. module_init(heiman_init);
  41. module_exit(heiman_exit);
复制代码

论坛徽章:
0
6 [报告]
发表于 2011-06-02 17:56 |只看该作者
device_create( my_class, NULL, dev, "hello%d", 0 );用错拉!
改成device_create( my_class, NULL, MKDEV(hello_major, 0), NULL ,"hello%d", 0);就可以了,你那个代码是从网上抄来的吧!


/**
* device_create - creates a device and registers it with sysfs
* @class: pointer to the struct class that this device should be registered to
* @parent: pointer to the parent struct device of this new device, if any
* @devt: the dev_t for the char device to be added
* @drvdata: the data to be added to the device for callbacks
* @fmt: string for the device's name
*
* This function can be used by char device classes.  A struct device
* will be created in sysfs, registered to the specified class.
*
* A "dev" file will be created, showing the dev_t for the device, if
* the dev_t is not 0,0.
* If a pointer to a parent struct device is passed in, the newly created
* struct device will be a child of that device in sysfs.
* The pointer to the struct device will be returned from the call.
* Any further sysfs files that might be required can be created using this
* pointer.
*
* Returns &struct device pointer on success, or ERR_PTR() on error.
*
* Note: the struct class passed to this function must have previously
* been created with a call to class_create().
*/
struct device *device_create(struct class *class, struct device *parent,
                             dev_t devt, void *drvdata, const char *fmt, ...)
{
        va_list vargs;
        struct device *dev;

        va_start(vargs, fmt);
        dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
        va_end(vargs);
        return dev;
}
EXPORT_SYMBOL_GPL(device_create);

论坛徽章:
0
7 [报告]
发表于 2011-06-02 22:47 |只看该作者
实不相瞒,是你的device_create缺少一个参数。
device_create( my_class, NULL, devno, “hello0", "heiman%d", 0);

论坛徽章:
0
8 [报告]
发表于 2011-06-07 19:21 |只看该作者
重启电脑就可以解决了。哈哈,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP