Skip to content

Driver Core APIs

Overview

The Linux Driver Core provides generic APIs for registering:

  • Buses
  • Devices
  • Drivers

and coordinating device-driver matching.

These APIs form the foundation of:

  • Platform Bus
  • I2C
  • SPI
  • USB
  • PCI

subsystems.


bus_register()

Prototype

int bus_register(struct bus_type *bus);

Description

Registers a bus with the Linux Driver Core.

After registration:

/sys/bus/<bus-name>

is created.

The bus becomes available for:

  • Device registration
  • Driver registration
  • Matching

Parameters

Parameter Description
bus Bus object to register

Return Value

Value Meaning
0 Success
< 0 Failure

Example

ret = bus_register(&demo_bus_type);
if (ret)
    return ret;

  • bus_unregister()

bus_unregister()

Prototype

void bus_unregister(struct bus_type *bus);

Description

Removes a previously registered bus.

All associated devices and drivers should be removed first.


Example

bus_unregister(&demo_bus_type);

device_register()

Prototype

int device_register(struct device *dev);

Description

Registers a device with the Driver Core.

The device becomes visible under:

/sys/devices

and under the corresponding bus.

The Driver Core automatically attempts matching against existing drivers.


Parameters

Parameter Description
dev Device object

Return Value

Value Meaning
0 Success
< 0 Failure

Example

ret = device_register(&demo_device);

Important Notes

A device must provide a valid release callback.

Example:

static void demo_device_release(struct device *dev)
{
}

Failure to provide a release callback will trigger a kernel warning.


  • device_unregister()

device_unregister()

Prototype

void device_unregister(struct device *dev);

Description

Removes a previously registered device.

The Driver Core automatically:

unbind
remove()
release()

when necessary.


Example

device_unregister(&demo_device);

driver_register()

Prototype

int driver_register(struct device_driver *drv);

Description

Registers a driver with the Driver Core.

The driver becomes visible under:

/sys/bus/<bus>/drivers

The Driver Core automatically attempts matching against existing devices.


Parameters

Parameter Description
drv Driver object

Return Value

Value Meaning
0 Success
< 0 Failure

Example

ret = driver_register(&demo_driver);

Important Notes

Successful registration does not guarantee successful probe.

Example:

driver_register()
match()
probe()
failure

In this case:

Driver Registered
Device Not Bound

  • driver_unregister()

driver_unregister()

Prototype

void driver_unregister(struct device_driver *drv);

Description

Removes a registered driver.

If devices are currently bound:

remove()
unbind

is performed automatically.


Example

driver_unregister(&demo_driver);

Device-Driver Lifecycle

bus_register()

device_register()

driver_register()

match()

probe()

binding

Removal:

driver_unregister()

remove()

unbind()

Common Pitfalls

Missing release callback

Incorrect:

static struct device demo_device = {
    .init_name = "demo_device",
};

Correct:

static struct device demo_device = {
    .init_name = "demo_device",
    .release = demo_device_release,
};

Assuming driver_register() implies probe success

Incorrect assumption:

driver_register() success
probe success

Actual behavior:

driver_register() success
match()
probe()
success or failure

Related Topics

  • Linux Driver Model
  • Platform Bus Internals
  • Driver Core
  • Device Tree Integration

Related Labs

  • Day71 - Linux Driver Model Fundamentals