Linux - List of Registered Devices

List all devices bound to a driver

Yes, simply register functions with A when driver B loads and call same function whenever device list is required.
e.g

Driver A <<< register_func(func_ptr_list); export register_func
Driver B <<< Call register_func with function list.

Multiple driver talks to each other using similar function element. for example Look at module_int for cxgb4 and cxgb4i

getting list of devices inside another driver

The final solution I used was this:

static int custom_match_dev(struct device *dev, void *data)
{
// this function implements the comaparison logic. Return not zero if found.
const char *name = data;

return sysfs_streq(name, dev->of_node->name);
}

static struct device *find_dev( const char * name )
{
struct device *dev = bus_find_device( &platform_bus_type, NULL, name, custom_match_dev );

return dev;
}

The only thing to remember is that the name of the device in the device tree must be unique.

How can i get the list of all initialized device drivers in a running Linux kernel?

each device driver is attached to some bus, and each bus has a "drivers" directory in sysfs, so something like the following shell command would print each bus and its registered drivers

cd /sys/bus; for bus in $(ls); do echo $bus; ls -1 $bus/drivers; echo; done

sample output:

scsi
sd
sr

sdio

serio
atkbd
psmouse
serio_raw

How to access a dynamic character device from userspace?

As soon as a character device gets registered with a dynamic major number, the corresponding information appears in /proc/devices and thus can be retrieved by a user-space application/script in order to create an appropriate node.

For a better example you may refer to Linux Device Drivers book (3rd edition), for instance, a script to read /proc/devices is shown on this page.

Register to the DeviceManager of Linux

Based on your comments, what you really want is to track which network interfaces are operational at any given time.

The only true way to determine if a network interface is up is to test it - after all, the router on the other end may be down. You could send pings out periodically, for example.

However, if you just want to know if the media goes down (ie, the network cable is unplugged), take a look at these SO questions:

  • Linux carrier detection notification
  • Get notified about network interface change on Linux

If you just want to be notified of the actual hardware-level registration of interfaces (eg, when a USB NIC is plugged in), you can use udev events if your platform has udev; otherwise, I believe there's another netlink category for hardware addition/removal events.

device driver documentation for linux

The command cat /proc/devices shows the character and block major device numbers in use by drivers in the currently running Linux kernel, but provides no information about minor device numbers.

There is a list of pre-assigned (reserved) device numbers in the Linux kernel user's and administrator's guide: Linux allocated devices (4.x+ version). (The same list also appears in "Documentation/admin-guide/devices.txt" in the Linux kernel sources.) The list shows how minor device numbers are interpreted for each pre-assigned character and block major device number.

Some major device numbers are reserved for local or experimental use, or for dynamic assignment:

  60-63 char    LOCAL/EXPERIMENTAL USE

60-63 block LOCAL/EXPERIMENTAL USE
Allocated for local/experimental use. For devices not
assigned official numbers, these ranges should be
used in order to avoid conflicting with future assignments.
 120-127 char   LOCAL/EXPERIMENTAL USE

120-127 block LOCAL/EXPERIMENTAL USE
Allocated for local/experimental use. For devices not
assigned official numbers, these ranges should be
used in order to avoid conflicting with future assignments.
 234-254    char    RESERVED FOR DYNAMIC ASSIGNMENT
Character devices that request a dynamic allocation of major number will
take numbers starting from 254 and downward.

240-254 block LOCAL/EXPERIMENTAL USE
Allocated for local/experimental use. For devices not
assigned official numbers, these ranges should be
used in order to avoid conflicting with future assignments.
 384-511 char   RESERVED FOR DYNAMIC ASSIGNMENT
Character devices that request a dynamic allocation of major
number will take numbers starting from 511 and downward,
once the 234-254 range is full.

Character device drivers that call alloc_chrdev_region() to register a range of character device numbers will be assigned an unused major device number from the dynamic range. The same is true for character device drivers that call __register_chrdev() with the first argument (major) set to 0.

Some external ("out-of-tree") Linux kernel modules have a module parameter to allow their default major device number to be specified at module load time. That is useful for drivers that do not create their "/dev" entries dynamically, but want some flexibility for the system administrator to choose a major device number when creating device files manually with mknod.



Related Topics



Leave a reply



Submit