insmod错误
本帖最后由 createwindow 于 2011-05-12 09:30 编辑
以下为代码
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- MODULE_LICENSE ("GPL");
- int hello_major = 120;
- int hello_minor = 0;
- int number_of_devices = 1;
- struct cdev cdev;
- static dev_t dev;
- struct file_operations hello_fops = {
- .owner = THIS_MODULE
- };
- static void char_reg_setup_cdev (void)
- {
- int error;
- cdev_init (&cdev, &hello_fops);
- cdev.owner = THIS_MODULE;
- cdev.ops = &hello_fops;
- error = cdev_add (&cdev, dev, 1);
- if (error)
- printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);
- }
- struct class *my_class;
- static int __init hello_2_init (void)
- {
- int result;
- dev = MKDEV(hello_minor, hello_minor);
- result = register_chrdev_region (dev, number_of_devices, "hello");
- if (result<0) {
- printk (KERN_WARNING "hello: can't get major number %d\n", hello_major);
- return result;
- }
- char_reg_setup_cdev ();
- /* create your own class under /sysfs */
- my_class = class_create(THIS_MODULE, "my_class");
- if(IS_ERR(my_class))
- {
- printk("Err: failed in creating class.\n");
- return -1;
- }
- /* register your own device in sysfs, and this will cause udev to create corresponding device node */
- device_create( my_class, NULL, dev, "hello%d", 0 );
- printk (KERN_INFO "Registered character driver\n");
- return 0;
- }
- static void __exit hello_2_exit (void)
- {
- cdev_del (&cdev);
- device_destroy(my_class, dev);
- class_destroy(my_class);
- unregister_chrdev_region (dev, number_of_devices);
- printk (KERN_INFO "char driver cleaned up\n");
- }
- module_init (hello_2_init);
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有人帮忙吗
写个简单的调试一下吧,一个helloworld写这么多
建议先手动创建设备节点,OK后再自动创建,step by step。
建议用动态的方式分配dev_t
建议注意一些常用的内核编程习惯
回复 3# dreamice
试过简单的了,下面是改过的版本。问题依旧。但是删除device_create()后,结果是正常的。非常奇怪
复制代码
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);
实不相瞒,是你的device_create缺少一个参数。
device_create( my_class, NULL, devno, “hello0", "heiman%d", 0);
重启电脑就可以解决了。哈哈,