--- x/drivers/comedi/drivers.c +++ y/drivers/comedi/drivers.c @@ -961,7 +961,7 @@ EXPORT_SYMBOL_GPL(comedi_legacy_detach); int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) { struct comedi_driver *driv; - int ret; + int ret, detach; lockdep_assert_held(&dev->mutex); if (dev->attached) @@ -1004,11 +1004,15 @@ int comedi_device_attach(struct comedi_d dev->driver = driv; dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr : dev->driver->driver_name; + detach = 0; ret = driv->attach(dev, it); - if (ret >= 0) + if (ret >= 0) { + detach = 1; ret = comedi_device_postconfig(dev); + } if (ret < 0) { - comedi_device_detach(dev); + if (detach) + comedi_device_detach(dev); module_put(driv->module); } /* On success, the driver module count has been incremented. */