Enable a PCI device with managed pcim_enable_device
pcim_enable_device() enables a PCI device and registers the disable for you, so a failing probe() or a remove() leaves the device off without extra code.
A PCI driver has to call pci_enable_device() before it touches the card and pci_disable_device() when it is done, on every error path. pcim_enable_device() is the managed version: the disable is registered with the device's resources and runs on unbind. The sample is for Linux 6.12.
static int demo_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
int ret;
ret = pcim_enable_device(pdev);
if (ret)
return dev_err_probe(&pdev->dev, ret, "cannot enable device\n");
pci_set_master(pdev);
return 0;
}demo 0000:03:00.0: enabling device (0000 -> 0002)The log line comes from the PCI core and shows the command register changing, here turning on memory decoding, bit 1. It is the sign that the enable succeeded, and the numbers vary with the device.
The managed call removes the matching pci_disable_device() from remove() and from each goto on the error path, which is where most leaks come from. The managed enable covers only the enable and disable pair: BAR mapping and region requests have their own helpers, so do not assume they are cleaned up by it. See the PCI driver guide.