device_watcher_init()
The device_watcher_init()
function is the initialization function for the driver. It is responsible for registering the PCI driver with the kernel when the module is loaded.
Function Signature
static int __init device_watcher_init(void);
static: The function is only visible within this file.
__init: Indicates that this function is used only during initialization and can be discarded after the driver is loaded.
Returns: An integer, typically
0
on success or a negative error code on failure.
Purpose
The device_watcher_init()
function registers the PCI driver with the kernel using the pci_register_driver()
function. This allows the kernel to associate the driver with the PCI devices it supports.
Implementation
static int __init device_watcher_init(void) {
return pci_register_driver(&pci_watcher_driver);
}
The function calls
pci_register_driver()
and passes thepci_watcher_driver
structure as an argument.The
pci_watcher_driver
structure contains the driver’s name, the device ID table (pci_watcher_ids
), and the probe and remove functions.The return value of
pci_register_driver()
is returned directly. If successful, it returns0
; otherwise, it returns an error code.
Error Handling
The function does not explicitly handle errors. If
pci_register_driver()
fails, the error is propagated to the caller (typically the kernel’s module loading mechanism).
Usage
This function is called when the driver module is loaded using the
insmod
ormodprobe
command.It is paired with the
device_watcher_exit()
function, which is called when the module is unloaded.
Example Usage
When the driver is loaded, the kernel calls device_watcher_init()
, which registers the driver with the PCI subsystem. If successful, the driver is now ready to handle PCI devices that match the IDs in the pci_watcher_ids
table.
Summary
The device_watcher_init()
function is a critical part of the driver’s initialization process. It registers the driver with the kernel’s PCI subsystem, enabling the driver to manage the supported PCI devices. The function is simple and relies on the pci_register_driver()
function to perform the actual registration.