---
name: "device_attach"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/device_attach"
---

# device\_attach(9)

manage device's connection to a device driver

## NAME

**device\_attach**, **device\_detach**, **device\_probe**, **device\_probe\_and\_attach** – manage device's connection to a device driver

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/bus.h\>**

*int*\

**device\_attach**(*device\_t dev*);

*int*\

**device\_detach**(*device\_t dev*);

*int*\

**device\_probe**(*device\_t dev*);

*int*\

**device\_probe\_and\_attach**(*device\_t dev*);

## DESCRIPTION

These functions manage the relationship between a device and device drivers.

**device\_probe**() invokes the [DEVICE\_PROBE(9)](/DEVICE_PROBE/9.md) method of each suitable driver and to find the driver with the best match for *dev*. If a matching driver is found, *dev* is set to the DS\_ALIVE state and zero is returned. If *dev* is already attached to a device driver or has been disabled via [device\_disable(9)](/device_disable/9.md), then it will not be probed and -1 is returned.

**device\_attach**() fully attaches a device driver to *dev*. This function prints a description of the device and invokes the [DEVICE\_ATTACH(9)](/DEVICE_ATTACH/9.md) method. If the [DEVICE\_ATTACH(9)](/DEVICE_ATTACH/9.md) method succeeds, *dev* is set to the DS\_ATTACHED state and zero is returned. If the [DEVICE\_ATTACH(9)](/DEVICE_ATTACH/9.md) method fails, [BUS\_CHILD\_DETACHED(9)](/BUS_CHILD_DETACHED/9.md) is called and an error value is returned.

If the device name and unit are disabled by a hint, **device\_attach**() disables the device, demotes it to the DS\_NOTPRESENT state, and returns ENXIO. The device retains its device name and unit and can be re-enabled via [devctl(8)](/devctl/8.md).

**device\_probe\_and\_attach**() is a wrapper function around **device\_probe**() and **device\_attach**() that fully initialises a device. If *dev* is already attached or disabled, **device\_probe\_and\_attach**() leaves the device unchanged and returns zero. Otherwise, **device\_probe**() is used to identify a device driver for *dev* and **device\_attach**() finalizes attaching the driver to *dev*. Device drivers should generally use this function to initialize a device rather than direct calls to **device\_probe**() and **device\_attach**().

**device\_detach**() detaches the device driver from *dev*. This function invokes the [DEVICE\_DETACH(9)](/DEVICE_DETACH/9.md) method to tear down device driver state for *dev*. If the method fails, its error value is returned and *dev* remains attached. If the method succeeds, otherwise, [BUS\_CHILD\_DETACHED(9)](/BUS_CHILD_DETACHED/9.md) is called, the device is set to the DS\_NOTPRESENT state, and zero is returned. If a device is busy, **device\_detach**() fails with EBUSY and leaving *dev* unchanged.

## RETURN VALUES

Zero is returned on success, otherwise an appropriate error is returned. In addition, **device\_probe**() returns -1 if *dev* is disabled or already attached.

## SEE ALSO

[devctl(8)](/devctl/8.md), [BUS\_CHILD\_DETACHED(9)](/BUS_CHILD_DETACHED/9.md), [device(9)](/device/9.md), [DEVICE\_ATTACH(9)](/DEVICE_ATTACH/9.md), [DEVICE\_DETACH(9)](/DEVICE_DETACH/9.md), [DEVICE\_PROBE(9)](/DEVICE_PROBE/9.md), [driver(9)](/driver/9.md)

## AUTHORS

This manual page was written by Doug Rabson.
