How to create a device in /dev automatically upon loading of the kernel module for a device driver?

后端 未结 6 1329
南旧
南旧 2020-12-06 01:26

I am attempting to develop Linux device drivers and as my first attempt I am trying to develop a char device driver that has the following file options,

st         


        
相关标签:
6条回答
  • 2020-12-06 01:52
    • Include the header file linux/device.h and linux/kdev_t.h

    • static struct class c_dev;

    • static struct dev_t dev;

    Add the below API 's inside __init fuction of the driver

    • cl = class_create(THIS_MODULE ,"x");

    where x - Name to be displayed inside /sys/class/ when driver is loaded.

    • Use device_create () kernel api with device_create(cl, NULL, dev, NULL, "d");

    where d - device file to be created under /dev.

    where dev is variable for the first device number that is initialized during the usage of alloc_chrdev_region API for dynamic allocation of major number for the driver

    For Further reference please go through the link http://opensourceforu.com/2011/04/character-device-files-creation-operations/

    0 讨论(0)
  • 2020-12-06 01:57

    I could see entries within /dev after creating node by running following command at console.

    sudo mknod -m 0666 /dev/msio c 22 0

    The user was not root, so I had to use sudo. My entry name was msio, a character device with major and minor number 22, 0 respectively.

    I will let you know if this can be achieved programatically.

    0 讨论(0)
  • 2020-12-06 01:57

    There are two ways to create the device file in /dev

    • Manually creation of a device file using mknod
      $ mknod -m <permission> <name> <device_type> <major> <minor>
      here
      name is the name of device driver,
      device_type is the type of the device (b-> block devices , c-> char devices),
      major & minor are the device numbers,
      permission is optional you can change it after creation, by using chmod.
    • Dynamic creation using .
    dev_t dev=0;
    static struct class *devicefileClass;
    deviceFileClass=class_create(THIS_MODULE,"device_name");
    device_create( deviceFileClass , NULL , dev ,"device_name")
    


    NOTE class_create , device_create function call should be in your __init function. and inlude <linux/device.h>,<linux/kdev_t>.

    0 讨论(0)
  • 2020-12-06 02:05

    CONFIG_DEVTMPFS is quite nice if you can use that with your distro. You can have the kernel automount that for you at boot (CONFIG_DEVTMPFS_MOUNT) or mount it manually (mount -t devtmpfs none /dev).

    0 讨论(0)
  • 2020-12-06 02:06

    First you have to use : sudo mknod /dev/devicename c 81 0

    It creates device file in /dev but you have to give it read/write permission. to do so,

    sudo chmod 777 /dev/devicename

    Done !!!

    0 讨论(0)
  • 2020-12-06 02:18

    You may have to create some udev rules to tell the system what device node(s) you need it to create.

    0 讨论(0)
提交回复
热议问题