- 论坛徽章:
- 0
|
各位大神,我insmod 驱动后 rmmod,再insmod报错了,发现sys/devices/virtual下生成的设备还在,下面是我的probe 和remove代码,各位大神帮我看下
probe的部分代码:
devno = MKDEV(MCU_LCD_MAJOR, MCU_LCD_MINOR);
703
704 lcd_dev = kzalloc(sizeof(struct slcd_dev), GFP_KERNEL);
705
706 if (NULL == lcd_dev) {
707
708 printk("%s kzalloc failed!\n",__func__);
709
710 return -ENOMEM;
711
712 }
713
714
715 if (MCU_LCD_MAJOR)
716 {
717 err= register_chrdev_region(lcd_dev->chrdev, 2, "lcddev");
718 }
719 else
720 {
721 err = alloc_chrdev_region(&lcd_dev->chrdev, 0, 2, "lcddev");
722 MCU_LCD_MAJOR = MAJOR(devno);
723 }
724
725 if (err)
726 {
727 printk("%s alloc_chrdev_region failed!\n",__func__);
728 goto alloc_chrdev_err;
729 }
730
731 printk("MAJOR IS %d\n", MCU_LCD_MAJOR);
732 lcd_dev->data = kzalloc(CHAR_DEV_DATA_SIZE, GFP_KERNEL);
733
734 if(NULL == lcd_dev->data)
735 {
736 printk("%s alloc_chrdev_region failed!\n", __func__);
737 goto alloc_dev_data_err;
738 }
739
740 cdev_init(&(lcd_dev->cdev), &lcd_fops);
741
742 lcd_dev->cdev.owner = THIS_MODULE;
743
744
745 err = cdev_add(&(lcd_dev->cdev), lcd_dev->chrdev, 1);
746
747
748 if (err) {
749
750 printk("%s cdev_add failed!\n",__func__);
751
752 goto cdev_add_err;
753
754 }
printk (KERN_INFO "Character driver Registered\n");
757
758
759
760
761 lcd_class = class_create(THIS_MODULE, "lcd_char_class"); //类名为hello_char_class
762
763 if(IS_ERR(lcd_class))
764
765 {
766
767 err = PTR_ERR(lcd_class);
768
769 printk("%s class_create failed!\n", __func__);
770
771 goto class_err;
772
773 }
774
775
776
777
778 lcd_dev->dev = device_create(lcd_class, NULL, lcd_dev->chrdev, NULL, "lcddev %d",0); //设备名为memdev
779
780
781 if (IS_ERR(lcd_dev->dev)) {
782
783 err = PTR_ERR(lcd_dev->dev);
784
785 printk("%s device_create failed!\n",__func__);
786
787 goto device_err;
788
789 }
下面是rmmove 的部分代码
cdev_del(&(lcd_dev->cdev));
978 unregister_chrdev_region(lcd_dev->chrdev,1);
979
980 device_destroy(lcd_class, devno); //delete device node under /dev//必须先删除设备,再删除class类
981
982 class_destroy(lcd_class); //delete class created by us
983
984 kfree(lcd_dev->data);
985 kfree(lcd_dev);
|
|