* @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 *
| 1697 | * @retval ENOMEM memory allocation failure |
| 1698 | */ |
| 1699 | static int |
| 1700 | devclass_add_device(devclass_t dc, device_t dev) |
| 1701 | { |
| 1702 | int buflen, error; |
| 1703 | |
| 1704 | PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); |
| 1705 | |
| 1706 | buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); |
| 1707 | if (buflen < 0) |
| 1708 | return (ENOMEM); |
| 1709 | dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); |
| 1710 | if (!dev->nameunit) |
| 1711 | return (ENOMEM); |
| 1712 | |
| 1713 | if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { |
| 1714 | free(dev->nameunit, M_BUS); |
| 1715 | dev->nameunit = NULL; |
| 1716 | return (error); |
| 1717 | } |
| 1718 | dc->devices[dev->unit] = dev; |
| 1719 | dev->devclass = dc; |
| 1720 | snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); |
| 1721 | |
| 1722 | return (0); |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * @internal |
no test coverage detected