From b107b904a6b856a62433c0baf3b160f22d9d8b19 Mon Sep 17 00:00:00 2001 From: Ian Lepore Date: Sun, 18 Feb 2018 02:01:41 +0000 Subject: [PATCH] Add a detach method so that this can be a kldunload-friendly module. --- sys/arm/freescale/imx/imx_i2c.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sys/arm/freescale/imx/imx_i2c.c b/sys/arm/freescale/imx/imx_i2c.c index 3b6d4aadb227..dfbe267dac19 100644 --- a/sys/arm/freescale/imx/imx_i2c.c +++ b/sys/arm/freescale/imx/imx_i2c.c @@ -154,6 +154,7 @@ struct i2c_softc { static phandle_t i2c_get_node(device_t, device_t); static int i2c_probe(device_t); static int i2c_attach(device_t); +static int i2c_detach(device_t); static int i2c_repeated_start(device_t, u_char, int); static int i2c_start(device_t, u_char, int); @@ -165,6 +166,7 @@ static int i2c_write(device_t, const char *, int, int *, int); static device_method_t i2c_methods[] = { DEVMETHOD(device_probe, i2c_probe), DEVMETHOD(device_attach, i2c_attach), + DEVMETHOD(device_detach, i2c_detach), /* OFW methods */ DEVMETHOD(ofw_bus_get_node, i2c_get_node), @@ -448,6 +450,28 @@ i2c_attach(device_t dev) return (0); } +static int +i2c_detach(device_t dev) +{ + struct i2c_softc *sc; + int error; + + sc = device_get_softc(dev); + + if ((error = bus_generic_detach(sc->dev)) != 0) { + device_printf(sc->dev, "cannot detach child devices\n"); + return (error); + } + + if (sc->iicbus != NULL) + device_delete_child(dev, sc->iicbus); + + if (sc->res != NULL) + bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res); + + return (0); +} + static int i2c_repeated_start(device_t dev, u_char slave, int timeout) {