device_add(9)
π device_add(9) β the kernel API manual, but make it cute π
okay so this is device_add(9) from the Kernel API Manual, and bestie, it's giving "part two of a two-part saga" energy and I am obsessed. Let's get into it β¨
πΈ NAME πΈ
device_add β add device to device hierarchy. that's it, that's the vibe π
π¦ SYNOPSIS π¦
int device_add (struct device *dev );
yes she returns an int, yes she takes a struct device *, no we are not scared π«Ά
π ARGUMENTS π
devβ device. literally just "device." the original said it in three letters and honestly? iconic minimalism π
β¨ DESCRIPTION β¨
so this is part 2 of device_register, though it may be called separately β and I say may because it's only valid _iff_ device_initialize has been called separately first. read that again, bestie, because the italics are doing the heavy lifting π
what it actually does: it adds dev to the kobject hierarchy via kobject_add, adds it to the global and sibling lists for the device, and then adds it to the other relevant subsystems of the driver model. like a glow-up in three phases, no cap π₯
now the serious part, and I need you to lock in:
Do not call this routine ordevice_registermore than once for any device structure. The driver model core is not designed to work with devices that get unregistered and then spring back to life. (Among other things, it's very hard to guarantee that all references to the previous incarnation ofdevhave been dropped.)
so no resurrecting devices, bestie. that's not main character energy, that's undefined behaviour energy π instead: allocate and register a fresh new struct device. new device, new you, periodt π
π NOTE π
_Never_ directly free dev after calling this function β even if it returned an error! Always use put_device to give up your reference instead. this is the number one rule and I will not be negotiating π
ββοΈ
rule of thumb, and I'm quoting the energy here:
- if
device_addsucceeds, you should calldevice_delwhen you want to get rid of it π - if
device_addhas not succeeded, use onlyput_deviceto drop the reference count π«Ά
that's it. that's the whole flowchart. screenshot it, put it on your wall π
π SEE ALSO π
Kernel file ./drivers/base/core.c, plus the whole extended friend group:
__fwnode_link_add(9)__fwnode_link_del(9)__fwnode_link_cycle(9)fwnode_links_purge_suppliers(9)fwnode_links_purge_consumers(9)fwnode_links_purge(9)__fwnode_links_move_consumers(9)__fw_devlink_pickup_dangling_consumers(9)fw_devlink_refresh_fwnode(9)device_is_dependent(9)device_pm_move_to_tail(9)device_link_wait_removal(9)device_link_add(9)device_link_del(9)device_link_remove(9)device_links_check_suppliers(9)__device_links_queue_sync_state(9)device_links_flush_sync_list(9)device_links_force_bind(9)device_links_driver_bound(9)__device_links_no_driver(9)device_links_no_driver(9)device_links_driver_cleanup(9)device_links_busy(9)device_links_unbind_consumers(9)device_links_purge(9)wait_for_init_devices_probe(9)fwnode_is_ancestor_of(9)fwnode_get_next_parent_dev(9)__fw_devlink_relax_cycles(9)fw_devlink_create_devlink(9)__fw_devlink_link_to_consumers(9)__fw_devlink_link_to_suppliers(9)dev_driver_string(9)device_release(9)devm_device_add_group(9)devices_kset_move_before(9)devices_kset_move_after(9)devices_kset_move_last(9)device_create_file(9)device_remove_file(9)device_remove_file_self(9)device_create_bin_file(9)device_remove_bin_file(9)device_initialize(9)kobject_has_children(9)dev_set_name(9)device_register(9)get_device(9)put_device(9)device_del(9)device_unregister(9)device_get_devnode(9)device_for_each_child(9)device_for_each_child_reverse(9)device_for_each_child_reverse_from(9)device_find_child(9)device_offline(9)device_online(9)__root_device_register(9)root_device_unregister(9)device_create(9)device_create_with_groups(9)device_destroy(9)device_rename(9)device_move(9)device_change_owner(9)device_shutdown(9)dev_err_probe(9)dev_warn_probe(9)set_primary_fwnode(9)set_secondary_fwnode(9)device_remove_of_node(9)device_add_of_node(9)device_set_of_node_from_dev(9)get_dev_from_fwnode(9)
and that's the page, bestie π«Ά this lil drop came out of ./drivers/base, dated October 2026, and it's filed under device_add(9) β go forth, add your devices exactly once, and slay π
β¨π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms