FreeBSD/sys/kern/subr_bus.c
/**
 * @internal
 * @brief Add a device to a devclass
 *
 * A unit number is allocated for the device (using the device's
 * preferred unit number if any) and the device is registered in the
 * devclass. This allows the device to be looked up by its unit
 * number, e.g. by decoding a dev_t minor number.
 *
 * @param dc		the devclass to add to
 * @param dev		the device to add
 *
 * @retval 0		success
 * @retval EEXIST	the requested unit number is already allocated
 * @retval ENOMEM	memory allocation failure
 */
@internal
@brief Add a device to a devclass

A unit number is allocated for the device (using the device's
preferred unit number if any) and the device is registered in the
devclass. This allows the device to be looked up by its unit
number, e.g. by decoding a dev_t minor number.

@param dc            the devclass to add to
@param dev           the device to add

@retval 0            success
@retval EEXIST       the requested unit number is already allocated
@retval ENOMEM       memory allocation failure
@internal @brief Add a device to a devclass A unit number is allocated for the device ( using the device ' s preferred unit number if any ) and the device is registered in the devclass . This allows the device to be looked up by its unit number e . g . by decoding a dev_t minor number . @param dc the devclass to add to @param dev the device to add @retval 0 success @retval EEXIST the requested unit number is already allocated @retval ENOMEM memory allocation failure
static int
devclass_add_device(devclass_t dc, device_t dev)
{
	int buflen, error;
	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
	buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
	if (buflen < 0)
		return (ENOMEM);
	dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
	if (!dev->nameunit)
		return (ENOMEM);
	if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
		free(dev->nameunit, M_BUS);
		dev->nameunit = NULL;
		return (error);
	}
	dc->devices[dev->unit] = dev;
	dev->devclass = dc;
	snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
	return (0);
}
static int devclass_add_device ( devclass_t dc , device_t dev ) { int buflen , error ; PDEBUG ( ( " " , DEVICENAME ( dev ) , DEVCLANAME ( dc ) ) ) ; buflen = snprintf ( NULL , 0 , " " , dc -> name , INT_MAX ) ; if ( buflen < 0 ) return ( ENOMEM ) ; dev -> nameunit = malloc ( buflen , M_BUS , M_NOWAIT | M_ZERO ) ; if ( ! dev -> nameunit ) return ( ENOMEM ) ; if ( ( error = devclass_alloc_unit ( dc , dev , & dev -> unit ) ) != 0 ) { free ( dev -> nameunit , M_BUS ) ; dev -> nameunit = NULL ; return ( error ) ; } dc -> devices [ dev -> unit ] = dev ; dev -> devclass = dc ; snprintf ( dev -> nameunit , buflen , " " , dc -> name , dev -> unit ) ; return ( 0 ) ; }
@internal
 @brief Add a device to a devclass
@internal @brief Add a device to a devclass