+static int clear_instances(void)
+{
+ GSList *l;
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ struct drv_context *drvc;
+
+ if (!(drvc = gdi->priv))
+ return SR_OK;
+
+ /* Properly close and free all devices. */
+ for (l = drvc->instances; l; l = l->next) {
+ if (!(sdi = l->data)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("genericdmm: sdi was NULL, continuing.");
+ continue;
+ }
+ if (!(devc = sdi->priv)) {
+ /* Log error, but continue cleaning up the rest. */
+ sr_err("genericdmm: sdi->priv was NULL, continuing.");
+ continue;
+ }
+
+ if (devc->profile) {
+ switch (devc->profile->transport) {
+ case DMM_TRANSPORT_USBHID:
+ /* TODO */
+ sr_usb_dev_inst_free(devc->usb);
+ break;
+ case DMM_TRANSPORT_SERIAL:
+ if (devc->serial && devc->serial->fd != -1)
+ serial_close(devc->serial->fd);
+ sr_serial_dev_inst_free(devc->serial);
+ break;
+ }
+ }
+
+ sr_dev_inst_free(sdi);
+ }
+
+ g_slist_free(drvc->instances);
+ drvc->instances = NULL;
+
+ return SR_OK;
+}
+