DEVICE_ADD_CHILD(9)
π DEVICE_ADD_CHILD π
okay bestie, so this is a Kernel Developer's Manual page β section 9, the driver-writing corner of the universe β and it's all about how to make a brand-new baby device and adopt it into an existing device's family tree π³β¨. The functions are device_add_child and device_add_child_ordered, and yes they're literally sisters, one of them just has opinions about birth order.
β¨ SYNOPSIS β¨
first, the vibes check β you gotta include these headers or nothing works, no cap:
#include <sys/param.h>
#include <sys/bus.h>
then the main character herself:
device_t
device_add_child(device_t dev, const char *name, int unit);
and her slightly more organized sister:
device_t
device_add_child_ordered(device_t dev, int order, const char *name,
int unit);
πΈ DESCRIPTION πΈ
So what we're doing here is creating a new child device of dev π«Ά. The name and unit args are literally just telling the system what this device is called and which unit number it gets.
- if the
nameis unknown? bestie, passNULLπ - if the
unitis unknown? passDEVICE_UNIT_ANYand the system will pick the next available unit number for you, slay π
The name of the device is what tells the system which drivers might be a good match for it. So if you specify a name, only drivers with that name get probed. If you give no name, all the drivers for the owning bus get probed. Either way β and this is the iconic part β only the name of the device is stored, so you can safely unload/load a driver bound to that name π₯. It's giving commit-phobia in the best way.
This whole setup lets buses that can uniquely identify device instances (thinking of you, PCI π) let each driver check each device instance for a match. But for buses that rely on supplied probe hints, where only one driver even has a chance of probing the device, you should specify the driver name as the device name. Different strokes, iykyk.
π about unit numbers π
Normally the system picks unit numbers automatically and you should just hand over DEVICE_UNIT_ANY. That's the default vibe, that's the move β¨. But when you specifically want a unit number β like wiring a particular piece of hardware to a pre-configured unit β then pass that unit. Small caveat: if the unit number you asked for is already allocated, a new unit gets allocated instead, and a diagnostic message gets printed π. So it's not a hard guarantee, it's a request, okay?
π¦ about ordering π¦
Some buses need their devices probed in a specific order. ISA is the classic example β some devices are sensitive to failed probe attempts from unrelated drivers, so they gotta go first, periodt. That's what the order argument of device_add_child_ordered() is for: it specifies a partial ordering. The new device gets added before any existing device with a greater order π.
And if you just use plain device_add_child()? Then the new child gets added as if its order was zero. Simple as that π.
π the care-and-feeding rules π
when you're adding a device inside aDEVICE_IDENTIFY(9)routine, usedevice_find_child(9)first to make sure the device hasn't already been added to the tree. Because the device name anddevclass_tget associated at probe time (not child-addition time), previous instances of the driver β say, in a module that was later unloaded β may have already added the instance π.
Bus driver authors, listen up π’: you gotta be careful when adding children while loading and unloading, or you'll end up with duplicate children devices. Nobody wants that, fr fr.
also, when you're adding a child to another device node β like in an identify routine β useBUS_ADD_CHILD(9)instead ofdevice_add_child().BUS_ADD_CHILD(9)callsdevice_add_child()and adds the proper bus-specific data to the new child. Note thatdevice_add_child()does not callBUS_ADD_CHILD(9)β the love is one-directional π.
β RETURN VALUES β
- the new device, if it worked π
NULL, otherwise π
π SEE ALSO π
go read these next, bestie, they're all connected:
π AUTHORS π
This manual page was written by Doug Rabson. Iconic of him, honestly π«Ά.
and that's the whole page, no crumbs left π this was the Debian build, dated February 11, 2018, and the manual name is DEVICE_ADD_CHILD(9) β section 9 forever, periodt π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1998 Doug Rabson
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms