I want to add a char device to /devices in my Linux directory via C code. Since I’m creating fictitious drivers that should only exist when I insmod my_module.ko, I want my module to create a device for me. Below is the part of my code which should add the device, but I only initialize my cdev struct and tell the kernel about it.
int start_mod(void){
//Because we are dealing with a fictitious device, I want
//the driver to create my two devices with arbitrarly
//assigned major numbers.
alloc_chrdev_region(&dev_num, FIRST_MINOR, COUNT, DEVICE_NAME); // This assigns my device name
// as well as asign Major # my driver uses
cdev_init(&(my_dev->my_cdev), &fops);// This initializes my cdev struct that the kernel uses to keep track of my device
my_dev->my_cdev.owner = THIS_MODULE;
my_dev->my_cdev.ops = &fops;// fops is my file operations struct
int err = cdev_add(&(my_dev->my_cdev), dev_num, COUNT);// this in theory should give a pointer to the kernel
// to my cdev struct that I have setup to exist in my other structure.
// Now I need to officially add my device to /devices folder.
return 0;
}
I’m not to sure what I need to do to officially add the char device to the kernel.
What you do is you use some of the newer registration functions in the kernel like
class_createanddevice_create. This will allowudevto create your device.Are you saying you wrote a driver without looking at any other drivers?
Because there is no shortage of examples about how to register a character device.
Look in
Those aforementioned functions are GPL-only, by the way, which has implications if you want to redistribute the code.