subr_bus.c (9dbf5b0e6876d8c93890754bcc9c748339de79c0) subr_bus.c (22ea1ec051c15659944bf3922c33ec4025d010ea)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997,1998,2003 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 1046 unchanged lines hidden (view full) ---

1055 if (dc->devices[i])
1056 count++;
1057 return (count);
1058}
1059
1060/**
1061 * @brief Get the maximum unit number used in a devclass
1062 *
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997,1998,2003 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 1046 unchanged lines hidden (view full) ---

1055 if (dc->devices[i])
1056 count++;
1057 return (count);
1058}
1059
1060/**
1061 * @brief Get the maximum unit number used in a devclass
1062 *
1063 * Note that this is one greater than the highest currently-allocated
1064 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1065 * that not even the devclass has been allocated yet.
1063 * Note that this is one greater than the highest currently-allocated unit. If
1064 * @p dc is NULL, @c -1 is returned to indicate that not even the devclass has
1065 * been allocated yet.
1066 *
1067 * @param dc the devclass to examine
1068 */
1069int
1070devclass_get_maxunit(devclass_t dc)
1071{
1072 if (dc == NULL)
1073 return (-1);

--- 56 unchanged lines hidden (view full) ---

1130{
1131 return (dc->sysctl_tree);
1132}
1133
1134/**
1135 * @internal
1136 * @brief Allocate a unit number
1137 *
1066 *
1067 * @param dc the devclass to examine
1068 */
1069int
1070devclass_get_maxunit(devclass_t dc)
1071{
1072 if (dc == NULL)
1073 return (-1);

--- 56 unchanged lines hidden (view full) ---

1130{
1131 return (dc->sysctl_tree);
1132}
1133
1134/**
1135 * @internal
1136 * @brief Allocate a unit number
1137 *
1138 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1138 * On entry, @p *unitp is the desired unit number (or @c DEVICE_UNIT_ANY if any
1139 * will do). The allocated unit number is returned in @p *unitp.
1139 * will do). The allocated unit number is returned in @p *unitp.
1140
1140 *
1141 * @param dc the devclass to allocate from
1142 * @param unitp points at the location for the allocated unit
1143 * number
1144 *
1145 * @retval 0 success
1146 * @retval EEXIST the requested unit number is already allocated
1147 * @retval ENOMEM memory allocation failure
1148 */
1149static int
1150devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1151{
1152 const char *s;
1153 int unit = *unitp;
1154
1155 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1156
1157 /* Ask the parent bus if it wants to wire this device. */
1141 * @param dc the devclass to allocate from
1142 * @param unitp points at the location for the allocated unit
1143 * number
1144 *
1145 * @retval 0 success
1146 * @retval EEXIST the requested unit number is already allocated
1147 * @retval ENOMEM memory allocation failure
1148 */
1149static int
1150devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1151{
1152 const char *s;
1153 int unit = *unitp;
1154
1155 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1156
1157 /* Ask the parent bus if it wants to wire this device. */
1158 if (unit == -1)
1158 if (unit == DEVICE_UNIT_ANY)
1159 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1160 &unit);
1161
1162 /* If we were given a wired unit number, check for existing device */
1159 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1160 &unit);
1161
1162 /* If we were given a wired unit number, check for existing device */
1163 /* XXX imp XXX */
1164 if (unit != -1) {
1163 if (unit != DEVICE_UNIT_ANY) {
1165 if (unit >= 0 && unit < dc->maxunit &&
1166 dc->devices[unit] != NULL) {
1167 if (bootverbose)
1168 printf("%s: %s%d already exists; skipping it\n",
1169 dc->name, dc->name, *unitp);
1170 return (EEXIST);
1171 }
1172 } else {

--- 104 unchanged lines hidden (view full) ---

1277 return (0);
1278
1279 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1280
1281 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1282 panic("devclass_delete_device: inconsistent device class");
1283 dc->devices[dev->unit] = NULL;
1284 if (dev->flags & DF_WILDCARD)
1164 if (unit >= 0 && unit < dc->maxunit &&
1165 dc->devices[unit] != NULL) {
1166 if (bootverbose)
1167 printf("%s: %s%d already exists; skipping it\n",
1168 dc->name, dc->name, *unitp);
1169 return (EEXIST);
1170 }
1171 } else {

--- 104 unchanged lines hidden (view full) ---

1276 return (0);
1277
1278 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1279
1280 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1281 panic("devclass_delete_device: inconsistent device class");
1282 dc->devices[dev->unit] = NULL;
1283 if (dev->flags & DF_WILDCARD)
1285 dev->unit = -1;
1284 dev->unit = DEVICE_UNIT_ANY;
1286 dev->devclass = NULL;
1287 free(dev->nameunit, M_BUS);
1288 dev->nameunit = NULL;
1289
1290 return (0);
1291}
1292
1293/**
1294 * @internal
1295 * @brief Make a new device and add it as a child of @p parent
1296 *
1297 * @param parent the parent of the new device
1298 * @param name the devclass name of the new device or @c NULL
1299 * to leave the devclass unspecified
1285 dev->devclass = NULL;
1286 free(dev->nameunit, M_BUS);
1287 dev->nameunit = NULL;
1288
1289 return (0);
1290}
1291
1292/**
1293 * @internal
1294 * @brief Make a new device and add it as a child of @p parent
1295 *
1296 * @param parent the parent of the new device
1297 * @param name the devclass name of the new device or @c NULL
1298 * to leave the devclass unspecified
1300 * @parem unit the unit number of the new device of @c -1 to
1301 * leave the unit number unspecified
1299 * @parem unit the unit number of the new device of @c DEVICE_UNIT_ANY
1300 * to leave the unit number unspecified
1302 *
1303 * @returns the new device
1304 */
1305static device_t
1306make_device(device_t parent, const char *name, int unit)
1307{
1308 device_t dev;
1309 devclass_t dc;

--- 22 unchanged lines hidden (view full) ---

1332 dev->devclass = NULL;
1333 dev->unit = unit;
1334 dev->nameunit = NULL;
1335 dev->desc = NULL;
1336 dev->busy = 0;
1337 dev->devflags = 0;
1338 dev->flags = DF_ENABLED;
1339 dev->order = 0;
1301 *
1302 * @returns the new device
1303 */
1304static device_t
1305make_device(device_t parent, const char *name, int unit)
1306{
1307 device_t dev;
1308 devclass_t dc;

--- 22 unchanged lines hidden (view full) ---

1331 dev->devclass = NULL;
1332 dev->unit = unit;
1333 dev->nameunit = NULL;
1334 dev->desc = NULL;
1335 dev->busy = 0;
1336 dev->devflags = 0;
1337 dev->flags = DF_ENABLED;
1338 dev->order = 0;
1340 if (unit == -1)
1339 if (unit == DEVICE_UNIT_ANY)
1341 dev->flags |= DF_WILDCARD;
1342 if (name) {
1343 dev->flags |= DF_FIXEDCLASS;
1344 if (devclass_add_device(dc, dev)) {
1345 kobj_delete((kobj_t) dev, M_BUS);
1346 return (NULL);
1347 }
1348 }

--- 33 unchanged lines hidden (view full) ---

1382 * This creates a new device and adds it as a child of an existing
1383 * parent device. The new device will be added after the last existing
1384 * child with order zero.
1385 *
1386 * @param dev the device which will be the parent of the
1387 * new child device
1388 * @param name devclass name for new device or @c NULL if not
1389 * specified
1340 dev->flags |= DF_WILDCARD;
1341 if (name) {
1342 dev->flags |= DF_FIXEDCLASS;
1343 if (devclass_add_device(dc, dev)) {
1344 kobj_delete((kobj_t) dev, M_BUS);
1345 return (NULL);
1346 }
1347 }

--- 33 unchanged lines hidden (view full) ---

1381 * This creates a new device and adds it as a child of an existing
1382 * parent device. The new device will be added after the last existing
1383 * child with order zero.
1384 *
1385 * @param dev the device which will be the parent of the
1386 * new child device
1387 * @param name devclass name for new device or @c NULL if not
1388 * specified
1390 * @param unit unit number for new device or @c -1 if not
1389 * @param unit unit number for new device or @c DEVICE_UNIT_ANY if not
1391 * specified
1392 *
1393 * @returns the new device
1394 */
1395device_t
1396device_add_child(device_t dev, const char *name, int unit)
1397{
1398 return (device_add_child_ordered(dev, 0, name, unit));

--- 9 unchanged lines hidden (view full) ---

1408 * @param dev the device which will be the parent of the
1409 * new child device
1410 * @param order a value which is used to partially sort the
1411 * children of @p dev - devices created using
1412 * lower values of @p order appear first in @p
1413 * dev's list of children
1414 * @param name devclass name for new device or @c NULL if not
1415 * specified
1390 * specified
1391 *
1392 * @returns the new device
1393 */
1394device_t
1395device_add_child(device_t dev, const char *name, int unit)
1396{
1397 return (device_add_child_ordered(dev, 0, name, unit));

--- 9 unchanged lines hidden (view full) ---

1407 * @param dev the device which will be the parent of the
1408 * new child device
1409 * @param order a value which is used to partially sort the
1410 * children of @p dev - devices created using
1411 * lower values of @p order appear first in @p
1412 * dev's list of children
1413 * @param name devclass name for new device or @c NULL if not
1414 * specified
1416 * @param unit unit number for new device or @c -1 if not
1415 * @param unit unit number for new device or @c DEVICE_UNIT_ANY if not
1417 * specified
1418 *
1419 * @returns the new device
1420 */
1421device_t
1422device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1423{
1424 device_t child;
1425 device_t place;
1426
1427 PDEBUG(("%s at %s with order %u as unit %d",
1428 name, DEVICENAME(dev), order, unit));
1416 * specified
1417 *
1418 * @returns the new device
1419 */
1420device_t
1421device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1422{
1423 device_t child;
1424 device_t place;
1425
1426 PDEBUG(("%s at %s with order %u as unit %d",
1427 name, DEVICENAME(dev), order, unit));
1429 KASSERT(name != NULL || unit == -1,
1428 KASSERT(name != NULL || unit == DEVICE_UNIT_ANY,
1430 ("child device with wildcard name and specific unit number"));
1431
1432 child = make_device(dev, name, unit);
1433 if (child == NULL)
1434 return (child);
1435 child->order = order;
1436
1437 TAILQ_FOREACH(place, &dev->children, link) {

--- 98 unchanged lines hidden (view full) ---

1536
1537/**
1538 * @brief Find a device given a unit number
1539 *
1540 * This is similar to devclass_get_devices() but only searches for
1541 * devices which have @p dev as a parent.
1542 *
1543 * @param dev the parent device to search
1429 ("child device with wildcard name and specific unit number"));
1430
1431 child = make_device(dev, name, unit);
1432 if (child == NULL)
1433 return (child);
1434 child->order = order;
1435
1436 TAILQ_FOREACH(place, &dev->children, link) {

--- 98 unchanged lines hidden (view full) ---

1535
1536/**
1537 * @brief Find a device given a unit number
1538 *
1539 * This is similar to devclass_get_devices() but only searches for
1540 * devices which have @p dev as a parent.
1541 *
1542 * @param dev the parent device to search
1544 * @param unit the unit number to search for. If the unit is -1,
1545 * return the first child of @p dev which has name
1546 * @p classname (that is, the one with the lowest unit.)
1543 * @param unit the unit number to search for. If the unit is
1544 * @c DEVICE_UNIT_ANY, return the first child of @p dev
1545 * which has name @p classname (that is, the one with the
1546 * lowest unit.)
1547 *
1548 * @returns the device with the given unit number or @c
1549 * NULL if there is no such device
1550 */
1551device_t
1552device_find_child(device_t dev, const char *classname, int unit)
1553{
1554 devclass_t dc;
1555 device_t child;
1556
1557 dc = devclass_find(classname);
1558 if (!dc)
1559 return (NULL);
1560
1547 *
1548 * @returns the device with the given unit number or @c
1549 * NULL if there is no such device
1550 */
1551device_t
1552device_find_child(device_t dev, const char *classname, int unit)
1553{
1554 devclass_t dc;
1555 device_t child;
1556
1557 dc = devclass_find(classname);
1558 if (!dc)
1559 return (NULL);
1560
1561 if (unit != -1) {
1561 if (unit != DEVICE_UNIT_ANY) {
1562 child = devclass_get_device(dc, unit);
1563 if (child && child->parent == dev)
1564 return (child);
1565 } else {
1566 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1567 child = devclass_get_device(dc, unit);
1568 if (child && child->parent == dev)
1569 return (child);

--- 4213 unchanged lines hidden (view full) ---

5783 if (error)
5784 break;
5785 }
5786
5787 /* Clear any previously-fixed device class and unit. */
5788 if (dev->flags & DF_FIXEDCLASS)
5789 devclass_delete_device(dev->devclass, dev);
5790 dev->flags |= DF_WILDCARD;
1562 child = devclass_get_device(dc, unit);
1563 if (child && child->parent == dev)
1564 return (child);
1565 } else {
1566 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1567 child = devclass_get_device(dc, unit);
1568 if (child && child->parent == dev)
1569 return (child);

--- 4213 unchanged lines hidden (view full) ---

5783 if (error)
5784 break;
5785 }
5786
5787 /* Clear any previously-fixed device class and unit. */
5788 if (dev->flags & DF_FIXEDCLASS)
5789 devclass_delete_device(dev->devclass, dev);
5790 dev->flags |= DF_WILDCARD;
5791 dev->unit = -1;
5791 dev->unit = DEVICE_UNIT_ANY;
5792
5793 /* Force the new device class. */
5794 error = devclass_add_device(dc, dev);
5795 if (error)
5796 break;
5797 dev->flags |= DF_FIXEDCLASS;
5798 error = device_probe_and_attach(dev);
5799 break;

--- 298 unchanged lines hidden ---
5792
5793 /* Force the new device class. */
5794 error = devclass_add_device(dc, dev);
5795 if (error)
5796 break;
5797 dev->flags |= DF_FIXEDCLASS;
5798 error = device_probe_and_attach(dev);
5799 break;

--- 298 unchanged lines hidden ---