diff --git a/sys/dev/rtsx/rtsx.c b/sys/dev/rtsx/rtsx.c --- a/sys/dev/rtsx/rtsx.c +++ b/sys/dev/rtsx/rtsx.c @@ -686,40 +686,46 @@ rtsx_card_task(void *arg, int pending __unused) { struct rtsx_softc *sc = arg; + device_t child; + bool present; - if (rtsx_is_card_present(sc)) { - sc->rtsx_flags |= RTSX_F_CARD_PRESENT; + RTSX_LOCK(sc); + child = sc->rtsx_mmc_dev; + present = rtsx_is_card_present(sc); + RTSX_UNLOCK(sc); + + if (present && child == NULL) { /* Card is present, attach if necessary. */ - if (sc->rtsx_mmc_dev == NULL) { - if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) - device_printf(sc->rtsx_dev, "Card inserted\n"); + if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) + device_printf(sc->rtsx_dev, "Card inserted\n"); - sc->rtsx_read_count = sc->rtsx_write_count = 0; - RTSX_LOCK(sc); - sc->rtsx_mmc_dev = device_add_child(sc->rtsx_dev, "mmc", DEVICE_UNIT_ANY); - RTSX_UNLOCK(sc); - if (sc->rtsx_mmc_dev == NULL) { - device_printf(sc->rtsx_dev, "Adding MMC bus failed\n"); - } else { - device_set_ivars(sc->rtsx_mmc_dev, sc); - device_probe_and_attach(sc->rtsx_mmc_dev); - } + sc->rtsx_read_count = sc->rtsx_write_count = 0; + child = device_add_child(sc->rtsx_dev, "mmc", DEVICE_UNIT_ANY); + if (child == NULL) { + device_printf(sc->rtsx_dev, "Adding MMC bus failed\n"); + } else { + device_set_ivars(child, sc); + device_probe_and_attach(child); } - } else { + + RTSX_LOCK(sc); + sc->rtsx_mmc_dev = child; + sc->rtsx_flags |= RTSX_F_CARD_PRESENT; + RTSX_UNLOCK(sc); + } else if (!present && child != NULL) { + if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) + device_printf(sc->rtsx_dev, "Card removed\n"); + if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) + device_printf(sc->rtsx_dev, "Read count: %jx, write count: %jx\n", + (uintmax_t)sc->rtsx_read_count, (uintmax_t)sc->rtsx_write_count); + + RTSX_LOCK(sc); sc->rtsx_flags &= ~RTSX_F_CARD_PRESENT; - /* Card isn't present, detach if necessary. */ - if (sc->rtsx_mmc_dev != NULL) { - if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) - device_printf(sc->rtsx_dev, "Card removed\n"); + sc->rtsx_mmc_dev = NULL; + RTSX_UNLOCK(sc); - if (sc->rtsx_debug_mask & RTSX_DEBUG_BASIC) - device_printf(sc->rtsx_dev, "Read count: %jx, write count: %jx\n", - (uintmax_t)sc->rtsx_read_count, - (uintmax_t)sc->rtsx_write_count); - if (device_delete_child(sc->rtsx_dev, sc->rtsx_mmc_dev)) - device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n"); - sc->rtsx_mmc_dev = NULL; - } + if (device_delete_child(sc->rtsx_dev, child)) + device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n"); } } #endif /* MMCCAM */