/*设备驱动模块加载函数*/ int globalfifo_init(void) { int ret; dev_t devno = MKDEV(globalfifo_major, 0); /* 申请设备号*/ if (globalfifo_major) ret = register_chrdev_region(devno, 1, "globalfifo"); else { /* 动态申请设备号 */ ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo"); globalfifo_major = MAJOR(devno); } if (ret < 0) return ret; /* 动态申请设备结构体的内存*/ globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL); if (!globalfifo_devp) { ret = - ENOMEM; goto fail_malloc; } memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev)); globalfifo_setup_cdev(globalfifo_devp, 0); init_MUTEX(&globalfifo_devp->sem); /*初始化信号量*/ init_waitqueue_head(&globalfifo_devp->r_wait); /*初始化读等待队列头*/ init_waitqueue_head(&globalfifo_devp->w_wait); /*初始化写等待队列头*/ return 0; fail_malloc: unregister_chrdev_region(devno, 1); return ret; }
int globalfifo_init(void) { int result; dev_t devno = MKDEV(globalfifo_major,0); if(globalfifo_major) result = register_chrdev_region(devno,1,"globalfifo"); else{ result = alloc_chrdev_region(&devno,0,1,"globalfifo"); globalfifo_major = MAJOR(devno); } if(result < 0) return result; devp = (struct globalfifo_dev*)kmalloc(sizeof(struct globalfifo_dev),GFP_KERNEL); if(!devp){ result = -ENOMEM; goto fail_malloc; } memset(devp,0,sizeof(struct globalfifo_dev)); globalfifo_setup_cdev(devp,0); //init_MUTEX(devp->sem);//初始化信号量 sema_init(&devp->sem,1); init_waitqueue_head(&devp->r_wait);//初始化读等待队列头 init_waitqueue_head(&devp->w_wait);//初始化写等待队列头 printk(KERN_ALERT "dev major is %d , usage:sudo mknod /dev/globalfifo c major minor",globalfifo_major); return 0; fail_malloc: unregister_chrdev_region(devno,1); return result; }
static int __init globalfifo_init(void) { int ret; dev_t devno = MKDEV(globalfifo_major, 0); if (globalfifo_major) ret = register_chrdev_region(devno, 1, "globalfifo"); else { ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo"); globalfifo_major = MAJOR(devno); } if (ret < 0) return ret; globalfifo_devp = kzalloc(sizeof(struct globalfifo_dev), GFP_KERNEL); if (!globalfifo_devp) { ret = -ENOMEM; goto fail_malloc; } globalfifo_setup_cdev(globalfifo_devp, 0); mutex_init(&globalfifo_devp->mutex); init_waitqueue_head(&globalfifo_devp->r_wait); init_waitqueue_head(&globalfifo_devp->w_wait); printk("globalfifo init ok\n"); return 0; fail_malloc: unregister_chrdev_region(devno, 1); return ret; }
//globalfifo设备驱动模块加载函数 int globalfifo_init(void) { int ret; dev_t devno = MKDEV(globalfifo_major, 0); //申请设备号 if (globalfifo_major) ret = register_chrdev_region(devno, 1, "globalfifo"); else { //动态申请设备号 ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo"); globalfifo_major = MAJOR(devno); } if (ret < 0) return ret; //动态申请设备结构体的内存 globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL); if (!globalfifo_devp) { //申请失败 ret = - ENOMEM; goto fail_malloc; } memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev)); globalfifo_setup_cdev(globalfifo_devp, 0); sema_init(&globalfifo_devp->sem, 1); //初始化信号量 #ifndef init_MUTEX sema_init(&globalfifo_devp->sem, 1); #else init_MUTEX(&globalfifo_devp->sem); #endif init_waitqueue_head(&globalfifo_devp->r_wait); //初始化读等待队列头 init_waitqueue_head(&globalfifo_devp->w_wait); //初始化写等待队列头 return 0; fail_malloc: unregister_chrdev_region(devno, 1); return ret; }
// globalfifo init function int globalfifo_init(void) { int result; dev_t devno = MKDEV(globalfifo_major, 0); if (globalfifo_major) result = register_chrdev_region(devno, 1, "globalfifo"); else { result = alloc_chrdev_region(&devno, 0, 1, "globalfifo"); globalfifo_major = MAJOR(devno); } if (result < 0) return result; globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL); if (!globalfifo_devp) { result = -ENOMEM; goto fail_malloc; } memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev)); globalfifo_setup_cdev(globalfifo_devp, 0); // init the MUTEX // Linux version > 2.6.32后,init_MUTEX被废除了 // init_MUTEX(&globalfifo_devp->sem); sema_init(&globalfifo_devp->sem, 1); // init the wait_queue_head init_waitqueue_head(&globalfifo_devp->r_wait); init_waitqueue_head(&globalfifo_devp->w_wait); return 0; fail_malloc: unregister_chrdev_region(devno, 1); return result; }