1 : Register Device : Registering the new device means provide major no. to it .The major number is provided by kernel. There are basically two way of registering the new device to the system
a.)Statically Registered Device Driver : when we know the major no then we can register device statically. example of this method is --> register_chrdev(int major , char *name , struct filr_operations *ops). To allocate major no dynamically , we can pass major number as 0 . this function will allocate major number dynamically and returned allocated major no. The prototype of register_chrdev() is in <linux/fs.h>
b.)Dynamically Registered Device driver : The kernel gives highest available major number between 1 to 255 . example of this method is ---> alloc_chrdev_region(dev_t *dev,int firstMinor ,int nod , char *DriverName). returns 0 on success .
c.)Unregister the driver : For deallocating the allocated major no we can use unregister_chrdev(int major , char *driverName).it basically remove the driver entry from device driver driver table .it should be done in cleanup function.
2.)Get Memory for Scull Device : The memory allocate in the kernel space by using kmalloc(size_t size ,gfp_t flags).The first argument is size ,which is no of bytes to allocate.The second argument is GFP flag (get free page flag). kmalloc is a symbol imported from kernel , used to allocate memory in kernel space.The requested space will be allocated in RAM only and this memory will be treated as Device memory.and the prototype of kmalloc is in <linux/slab.h> . we can free this allocated memory by using kfree(Dev *dev).
3.)Set Memory to NULL :We can Set allocated Memory to null using memset.
4.)Initialize Device : cdev_init is used to initialize the cdev structure . cdev structure define the device parameter and give you direct access to device. the struct cdev is the kernel's internal structure that represents the char devices.the prototype of cdev_init(struct cdev * ,const struct file_operations *) in the <linux/cdev.h>
5.)Add Device :After initialized the driver we use cdev_add to add or to give direct access to cdev from device table . cdev_add(struct cdev *dev ,dev_t num ,int nod). if the function returns negative number, then it means your device has not added to to the system ,so check the return value of the function.
6.) Get Device Status : we can get device status by following commands :
It looks like you're new here. If you want to get involved, click one of these buttons!