+/* Properly close and free all devices. */
+static int clear_instances(void)
+{
+ struct sr_dev_inst *sdi;
+ struct drv_context *drvc;
+ struct dev_context *devc;
+ GSList *l;
+
+ drvc = hdi->priv;
+ for (l = drvc->instances; l; l = l->next) {
+ if (!(sdi = l->data)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
+ continue;
+ }
+ if (!(devc = sdi->priv)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
+ continue;
+ }
+ dso_close(sdi);
+ sr_usb_dev_inst_free(devc->usb);
+ g_free(devc->triggersource);
+
+ sr_dev_inst_free(sdi);
+ }
+
+ g_slist_free(drvc->instances);
+ drvc->instances = NULL;
+
+ return SR_OK;
+}
+