下面这个注册方法是自动创建设备节点的字符设备驱动的方法 #define DEVICE_NAME "fpga_dma" static int dev_major = 0; static struct class *fpga_class; static struct cdev fpgaDevice; static int dev_init(void) { int result; int err; dev_t dev = MKDEV(dev_major, 0); if (dev_major) result = register_chrdev_region(dev, 1, DEVICE_NAME); else { result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME); dev_major = MAJOR(dev); } if (result < 0) { printk("unable to get major %d\n", dev_major); return result; } printk("get major is %d\n", dev_major); if (dev_major == 0) dev_major = result; cdev_init(&fpgaDevice, &fpga_dma_fops); fpgaDevice.owner = THIS_MODULE; fpgaDevice.ops = &fpga_dma_fops; err = cdev_add(&fpgaDevice, dev, 1); if (err) printk("error %d add fpga ", err); fpga_class = class_create(THIS_MODULE, DEVICE_NAME); if (IS_ERR(fpga_class)) { printk("Err:failed in creating class.\n"); return -1; } class_device_create(fpga_class, NULL, MKDEV(dev_major, 0), NULL, "%s", DEVICE_NAME); printk("MPC8377core FPGA_GPIO_driver installed OK\n"); return 0; } //这里面又两个函数 fpga_class = class_create(THIS_MODULE, DEVICE_NAME); 和class_device_create(fpga_class, NULL, MKDEV(dev_major, 0), NULL, "%s", DEVICE_NAME); 是注册时自动创建节点的关键函数,具体函数意义,可以网上搜索 /**********************************************************/ static struct file_operations fpga_dma_fops = { owner: THIS_MODULE, //ioctl: fpga_dma_ioctl, //open: fpga_dma_open, //release: fpga_dma_close, }; static void dev_clean(void) { cdev_del(&fpgaDevice); class_device_destroy(fpga_class, MKDEV(dev_major, 0)); class_destroy(fpga_class); unregister_chrdev_region(MKDEV(dev_major, 0), 2); }