- /* allocate any major number with only one minor */
- r = alloc_chrdev_region(&dev, 0, 1, "helloc");
- if (r)
- goto out_region;
- r = -ENOMEM;
- /* print the major number allocated so we can create our device node */
- printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev));
- /* allocate the character device struct */
- cdev = cdev_alloc();
- if (!cdev)
- goto out_alloc;
- /* set the module owner and the file operations of our chardev */
- cdev->owner = THIS_MODULE;
- cdev->ops = &helloc_fops;
- /* register the chardev to the system */
- r = cdev_add(cdev, dev, 1);
- if (r)
- goto out_add;
- return 0;
-out_add:
- /* release memory allocated to the cdev device */
- kfree(cdev);
-out_alloc:
- /* release the device number allocated */
- unregister_chrdev_region(dev, 1);
-out_region: