Kahn
Kahn

Reputation: 113

How A Device Link To Driver In Linux

Device file link to driver by major and minor number. So how drivers link to devices??? When we plug-in a device, how can kernel know it work with which module had loaded

Upvotes: 2

Views: 2865

Answers (3)

Alan Curry
Alan Curry

Reputation: 14711

The answers about vendor and device IDs and hotplug and udev are correct, but they haven't explained what major and minor numbers are. Just so all the keywords are covered for future searchers, I'll do that here.

Major and minor device numbers, as seen in ls -l /dev, don't contribute to the identification of a driver to be loaded on detection of a device. They are for the kernel to know what device you're accessing when you open the device file.

When a driver is loaded, if it supports any special files in /dev, it will call register_chrdev (or some similar function), notifying the kernel of the major number it wants to use. Some drivers dynamically choose a major number by asking the kernel to pick an unused one. The registration procedure puts the driver and its major number into a table where they can be found later.

Permanently allocated device numbers are registered with a central authority and the registrations are published in Documentation/admin-guide/devices.*.

Files in /dev have major numbers matching their driver, and minor numbers identifying a particular device or function. These files are created either statically by MAKEDEV or dynamically by the hotplug/udev system. The low-level "create device file" operation is called mknod.

When a user program opens a device file, the major and minor numbers are used to look up the driver in the table maintained by register_chrdev, and the driver's methods are called to service the user's requests.

Upvotes: 2

Bill Lynch
Bill Lynch

Reputation: 81916

For PCI Devices, they expose a vendor and device id in a consistent location. Kernel modules can present to the pci subsystem a list of these that they support.

For example: http://lxr.free-electrons.com/source/drivers/uio/uio_aec.c#L48

Upvotes: 1

The hotplug program may be automagically started by the kernel when it detects new plugged devices. Now udev also contributes to that.

Upvotes: 0

Related Questions