Linux Kernel Driver Program:5_Manually_Creating_Device_File

Linux Kernel Driver Program:

5_Manually_Creating_Device_File

neelkanth_surekha#cat hello_world_module.c 

/**********************************************************************
 * After inserting the below kernel module, manually create a device 
 * File using below command:
 *
 * sudo mknod -m 666 /dev/etx_device c 246 0
 ***********************************************************************/


#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>

dev_t dev = 0;

static int __init hello_world_init(void)
{
    /*Allocating Major number*/
    if((alloc_chrdev_region(&dev, 0, 1, "Neelkanth")) <0){
        printk(KERN_INFO "Cannot allocate major number for device\n");
        return -1;
    }
    printk(KERN_INFO "Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
    printk(KERN_INFO "Kernel Module Inserted Successfully...\n");
    return 0;
}

void __exit hello_world_exit(void)
{
    unregister_chrdev_region(dev, 1);
    printk(KERN_INFO "Kernel Module Removed Successfully...\n");
}

module_init(hello_world_init);
module_exit(hello_world_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Neelkanth <www.neelkanth.13@gmail.com>");
MODULE_DESCRIPTION("A simple hello world driver");
MODULE_VERSION("1.1");

neelkanth_surekha#cat Makefile 

obj-m += hello_world_module.o

KDIR = /lib/modules/$(shell uname -r)/build

all:
make -C $(KDIR)  M=$(shell pwd) modules

clean:
make -C $(KDIR)  M=$(shell pwd) clean