问题
I have a linux driver that registers a char device for every compatible device-node in the Flattened-Device-Tree(FDT).
On initialization, the driver allocates a major device number and a range of minor device numbers.
when i look at /proc/devices, this major device number is listed.
Now, when I define 2 device-nodes, compatible with that driver, in the FDT, the driver's platform-probe-function get's called twice, and 2 char devices are registered under the same major device number, but with ascending minor device numbers.
Since I can lookup the major number, and I do know how many devices I have and that the minor numbers start at 0, I can just type mknod -m 666 /dev/mydevice1 c 246 0 and mknod -m 666 /dev/mydevice2 c 246 1 to create device nodes for the two char devices.
But I am wondering if there is a way I can lookup all the devices known to the system with their major and minor device numbers, so I do not have to know the minor number beforehand, in order to be able to create device nodes for those devices?
回答1:
It sounds like you're asking how to have the system automatically create your device nodes for you, rather than having to use the mknod command.
Try these steps:
1) A bit of initialization
#include <linux/devices.h>
#define DEVNAME "device_name" /* you will need this a few times,
so make it a macro */
static dev_t my_devt; /* make this static so we can access it
across function calls */
static struct class* my_class; /* pointer to device class */
2) In your __init routine after cdev_add(), initialize the class and register the device with sysfs:
my_class = class_create(THIS_MODULE, DEVNAME);
if(IS_ERR(device_create(my_class, NULL, my_devt, NULL, DEVNAME))){
printk(KERN_ERR "Node creation for %s failed.", DEVNAME);
/* clean up after failed initialization */
}
3) Add the corresponding clean up function calls to __exit():
device_destroy(my_class, my_devt);
class_destroy(my_class);
This should result in a node being created in /dev for a single device. Your question specifically asks about multiple nodes, so you can create an array of nodes instead, and duplicate the device_create() and device_destroy() function calls for each of the individual nodes you have.
I know that your original question was asking for a way to enumerate all devices known to the system, but you also said that your driver is already registering a char device for every compatible device in the FDT. If you add this code to your existing driver, you can take advantage of the dev_t information you already have during char device registration to create the nodes and eliminate the need to enumerate or list anything later.
For more details, check out this tutorial: Character Device Files. The kernel.org docs for device_create() may also be helpful to you.
回答2:
Use this command:
ls -l /sys/dev/char/
来源:https://stackoverflow.com/questions/23810295/linux-list-of-registered-devices