diff --git a/sys/dev/mmc/host/dwmmc.c b/sys/dev/mmc/host/dwmmc.c --- a/sys/dev/mmc/host/dwmmc.c +++ b/sys/dev/mmc/host/dwmmc.c @@ -477,33 +477,36 @@ #ifdef MMCCAM mmc_cam_sim_discover(&sc->mmc_sim); #else + device_t child; + bool present; + DWMMC_LOCK(sc); + child = sc->child; + present = READ4(sc, SDMMC_CDETECT) || + (sc->mmc_helper.props & MMC_PROP_BROKEN_CD) != 0; + DWMMC_UNLOCK(sc); - if (READ4(sc, SDMMC_CDETECT) == 0 || - (sc->mmc_helper.props & MMC_PROP_BROKEN_CD)) { - if (sc->child == NULL) { - if (bootverbose) - device_printf(sc->dev, "Card inserted\n"); - - sc->child = device_add_child(sc->dev, "mmc", DEVICE_UNIT_ANY); - DWMMC_UNLOCK(sc); - if (sc->child) { - device_set_ivars(sc->child, sc); - (void)device_probe_and_attach(sc->child); - } - } else - DWMMC_UNLOCK(sc); - } else { - /* Card isn't present, detach if necessary */ - if (sc->child != NULL) { - if (bootverbose) - device_printf(sc->dev, "Card removed\n"); - - DWMMC_UNLOCK(sc); - device_delete_child(sc->dev, sc->child); - sc->child = NULL; - } else - DWMMC_UNLOCK(sc); + if (present && child == NULL) { + if (bootverbose) + device_printf(sc->dev, "Card inserted\n"); + + child = device_add_child(sc->dev, "mmc", DEVICE_UNIT_ANY); + if (child != NULL) { + device_set_ivars(child, sc); + (void)device_probe_and_attach(child); + } + DWMMC_LOCK(sc); + sc->child = child; + DWMMC_UNLOCK(sc); + } else if (!present && child != NULL) { + /* Card isn't present, detach. */ + if (bootverbose) + device_printf(sc->dev, "Card removed\n"); + + device_delete_child(sc->dev, child); + DWMMC_LOCK(sc); + sc->child = NULL; + DWMMC_UNLOCK(sc); } #endif /* MMCCAM */ }