/* * Module housekeeping. */ static int simple_init(void) { int result; dev_t dev = MKDEV(simple_major, 0); /* Figure out our device number. */ if (simple_major) result = register_chrdev_region(dev, 2, "simple"); else { result = alloc_chrdev_region(&dev, 0, 2, "simple"); simple_major = MAJOR(dev); } if (result < 0) { printk(KERN_WARNING "simple: unable to get major %d\n", simple_major); return result; } if (simple_major == 0) simple_major = result; /* Now set up two cdevs. */ simple_setup_cdev(SimpleDevs, 0, &simple_remap_ops); simple_setup_cdev(SimpleDevs + 1, 1, &simple_nopage_ops); return 0; }
static int simple_init(void) { int result = 0; static dev_t dev_no; printk(KERN_INFO "%s()\n", __func__); result = kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL); if (result) { printk(KERN_ERR "error kfifo_alloc\n"); goto kfifo_alloc_err; } printk(KERN_INFO "%s() : kfifo size = %lu\n", __func__, FIFO_SIZE); result = alloc_chrdev_region(&dev_no, 0, 2, "simple"); if (result < 0) { printk(KERN_WARNING "simple: unable to get major %d\n", simple_major); result = -EPERM; goto alloc_chedev_region_fail; } simple_major = MAJOR(dev_no); simple_minor = MINOR(dev_no); printk(KERN_INFO "%s() : Simple driver for 2 devices\n", __func__); printk(KERN_INFO "Major=%d,Minor=%d\n", simple_major, simple_minor); printk(KERN_INFO "MajoR=%d,Minor=%d\n", simple_major, (simple_minor+1)); cl = class_create(THIS_MODULE, "chardrv"); if (cl == NULL) { result = -EPERM; goto class_create_fail; } /* Now set up two cdevs. */ result = simple_setup_cdev(simple_cdev, 0, &prod_ops, "prod"); if (result < 0) { if (result == -DEVADDERR) goto dev_add_err_0; else goto dev_create_err_0; } result = simple_setup_cdev(simple_cdev + 1, 1, &cons_ops, "cons"); if (result < 0) { if (result == -DEVADDERR) goto dev_add_err_1; else goto dev_create_err_1; } sema_init(&prod_sem, 1); sema_init(&cons_sem, 1); init_waitqueue_head(&prod_que); init_waitqueue_head(&cons_que); goto init_success; dev_add_err_1: cdev_del(simple_cdev + 1); dev_create_err_1: dev_add_err_0: cdev_del(simple_cdev); dev_create_err_0: class_create_fail: unregister_chrdev_region(MKDEV(simple_major, simple_minor), 2); alloc_chedev_region_fail: kfifo_alloc_err: result = -1; init_success: return result; }