Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

How to create a device node from the init_module code of a Linux kernel module?

I am writing a module for the linux kernel and I want to create some device nodes in the init function

int init_module(void)
{
    Major = register_chrdev(0, DEVICE_NAME, &fops);
 // Now I want to create device nodes with the returned major number
}

I also want the kernel to assign a minor number for my first node, and then I will assign the other nodes' minor numbers by myself.

How can I do this in the code. I dont want to create devices from the shell using mknod

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To have more control over the device numbers and the device creation you could do the following steps (instead of register_chrdev()):

  1. Call alloc_chrdev_region() to get a major number and a range of minor numbers to work with.
  2. Create device class for your devices with class_create().
  3. For each device, call cdev_init() and cdev_add() to add the character device to the system.
  4. For each device, call device_create(). As a result, among other things, Udev will create device nodes for your devices. No need for mknod or the like. device_create() also allows you to control the names of the devices.

There are probably many examples of this on the Net, one of them is here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...