insmod错误

发布于 2022-10-15 07:24:25 字数 3594 浏览 23 评论 0

本帖最后由 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

请大家指教。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

后来的我们 2022-10-22 07:24:25

有人帮忙吗

你怎么敢 2022-10-22 07:24:25

写个简单的调试一下吧,一个helloworld写这么多

迷路的信 2022-10-22 07:24:25

建议先手动创建设备节点,OK后再自动创建,step by step。
建议用动态的方式分配dev_t
建议注意一些常用的内核编程习惯

﹉夏雨初晴づ 2022-10-22 07:24:25

回复 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);

复制代码

梦里寻她 2022-10-22 07:24:25

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);

独木成林 2022-10-22 07:24:25

实不相瞒,是你的device_create缺少一个参数。
device_create( my_class, NULL, devno, “hello0", "heiman%d", 0);

美煞众生 2022-10-22 07:24:25

重启电脑就可以解决了。哈哈,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文