Register_chrdev, apiregister_chrdev
Register_chrdev for kernel device driver APIs
Register_chrdev is used to register a character device. The source code is analyzed as follows: static inline int register_chrdev (unsigned int major, const char * name, const struct file_operations * fops) {# After setting the smallest character device number and the largest device number, call the following function to register return _ register_chrdev (major, 0,256, name, fops);} int _ register_chrdev (unsigned int major, unsigned int baseminor, unsigned int count, const char * name, const struct file_operations * fops) {struct char_device_struct * cd; struct cdev * cdev; int err =- ENOMEM; # apply for a structure cd = _ register_chrdev_region (major, baseminor, count, name); if (IS_ERR (cd) return PTR_ERR (cd ); # apply for a character device cdev = cdev_alloc (); if (! Cdev) goto out2; # assign a value to the character device: cdev-> owner = fops-> owner; cdev-> ops = fops; # Set the namekobject_set_name (& cdev-> kobj, "% s", name); # Add the character device err = cdev_add (cdev, MKDEV (cd-> major, baseminor), count); if (err) goto out; # point to the newly applied character device cd-> cdev = cdev; # normally, major is not zero, so the whole function returns zero as the success result return major? 0: cd-> major; out: kobject_put (& cdev-> kobj); out2: kfree (_ unregister_chrdev_region (cd-> major, baseminor, count )); return err ;}