FreeBSD/sys/kern/subr_bus.c
/**
 * @brief Attach a device driver to a device
 *
 * This function is a wrapper around the DEVICE_ATTACH() driver
 * method. In addition to calling DEVICE_ATTACH(), it initialises the
 * device's sysctl tree, optionally prints a description of the device
 * and queues a notification event for user-based device management
 * services.
 *
 * Normally this function is only called internally from
 * device_probe_and_attach().
 *
 * @param dev		the device to initialise
 *
 * @retval 0		success
 * @retval ENXIO	no driver was found
 * @retval ENOMEM	memory allocation failure
 * @retval non-zero	some other unix error code
 */
@brief Attach a device driver to a device
This function is a wrapper around the DEVICE_ATTACH() driver
method. In addition to calling DEVICE_ATTACH(), it initialises the
device's sysctl tree, optionally prints a description of the device
and queues a notification event for user-based device management
services.

Normally this function is only called internally from
device_probe_and_attach().

@param dev           the device to initialise

@retval 0            success
@retval ENXIO        no driver was found
@retval ENOMEM       memory allocation failure
@retval non-zero     some other unix error code
@brief Attach a device driver to a device This function is a wrapper around the DEVICE_ATTACH () driver method . In addition to calling DEVICE_ATTACH () it initialises the device ' s sysctl tree optionally prints a description of the device and queues a notification event for user - based device management services . Normally this function is only called internally from device_probe_and_attach () . @param dev the device to initialise @retval 0 success @retval ENXIO no driver was found @retval ENOMEM memory allocation failure @retval non - zero some other unix error code
int
device_attach(device_t dev)
{
	uint64_t attachtime;
	int error;
	if (resource_disabled(dev->driver->name, dev->unit)) {
		device_disable(dev);
		if (bootverbose)
			 device_printf(dev, "disabled via hints entry\n");
		return (ENXIO);
	}
	device_sysctl_init(dev);
	if (!device_is_quiet(dev))
		device_print_child(dev->parent, dev);
	attachtime = get_cyclecount();
	dev->state = DS_ATTACHING;
	if ((error = DEVICE_ATTACH(dev)) != 0) {
		printf("device_attach: %s%d attach returned %d\n",
		    dev->driver->name, dev->unit, error);
		if (!(dev->flags & DF_FIXEDCLASS))
			devclass_delete_device(dev->devclass, dev);
		(void)device_set_driver(dev, NULL);
		device_sysctl_fini(dev);
		KASSERT(dev->busy == 0, ("attach failed but busy"));
		dev->state = DS_NOTPRESENT;
		return (error);
	}
	attachtime = get_cyclecount() - attachtime;
#define	RANDOM_PROBE_BIT_GUESS	4
	if (bootverbose)
		printf("random: harvesting attach, %zu bytes (%d bits) from %s%d\n",
		    sizeof(attachtime), RANDOM_PROBE_BIT_GUESS,
		    dev->driver->name, dev->unit);
	random_harvest_direct(&attachtime, sizeof(attachtime),
	    RANDOM_PROBE_BIT_GUESS, RANDOM_ATTACH);
	device_sysctl_update(dev);
	if (dev->busy)
		dev->state = DS_BUSY;
	else
		dev->state = DS_ATTACHED;
	dev->flags &= ~DF_DONENOMATCH;
	devadded(dev);
	return (0);
}
int device_attach ( device_t dev ) { uint64_t attachtime ; int error ; if ( resource_disabled ( dev -> driver -> name , dev -> unit ) ) { device_disable ( dev ) ; if ( bootverbose ) device_printf ( dev , " \n " ) ; return ( ENXIO ) ; } device_sysctl_init ( dev ) ; if ( ! device_is_quiet ( dev ) ) device_print_child ( dev -> parent , dev ) ; attachtime = get_cyclecount ( ) ; dev -> state = DS_ATTACHING ; if ( ( error = DEVICE_ATTACH ( dev ) ) != 0 ) { printf ( " \n " , dev -> driver -> name , dev -> unit , error ) ; if ( ! ( dev -> flags & DF_FIXEDCLASS ) ) devclass_delete_device ( dev -> devclass , dev ) ; ( void ) device_set_driver ( dev , NULL ) ; device_sysctl_fini ( dev ) ; KASSERT ( dev -> busy == 0 , ( " " ) ) ; dev -> state = DS_NOTPRESENT ; return ( error ) ; } attachtime = get_cyclecount ( ) - attachtime ; #define RANDOM_PROBE_BIT_GUESS 	4 if ( bootverbose ) printf ( " \n " , sizeof ( attachtime ) , RANDOM_PROBE_BIT_GUESS , dev -> driver -> name , dev -> unit ) ; random_harvest_direct ( & attachtime , sizeof ( attachtime ) , RANDOM_PROBE_BIT_GUESS , RANDOM_ATTACH ) ; device_sysctl_update ( dev ) ; if ( dev -> busy ) dev -> state = DS_BUSY ; else dev -> state = DS_ATTACHED ; dev -> flags &= ~ DF_DONENOMATCH ; devadded ( dev ) ; return ( 0 ) ; }
@brief Attach a device driver to a device
 This function is a wrapper around the DEVICE_ATTACH() driver
 method.
@brief Attach a device driver to a device This function is a wrapper around the DEVICE_ATTACH () driver method .
/*
	 * 4 bits per device is a reasonable value for desktop and server
	 * hardware with good get_cyclecount() implementations, but WILL
	 * need to be adjusted on other platforms.
	 */