struct usb_context {
usbg_state *usbg;
+ usbg_gadget *gadget;
pthread_t ep0_thread;
int eps[TFMFFS_EP_MAX];
ret = usbg_create_function(g, USBG_F_FFS, TFMFFS_DEV, NULL, &f);
if (ret < 0) {
fprintf(stderr, "Failed to create USB function\n");
- goto err_cleanup;
+ goto err_rmgadget;
}
ret = usbg_create_config(g, 1, NULL, NULL, &c_strs, &c);
if (ret < 0) {
fprintf(stderr, "Failed to create USB config\n");
- goto err_cleanup;
+ goto err_rmgadget;
}
ret = usbg_add_config_function(c, TFMFFS_BIND_LINK, f);
if (ret < 0) {
fprintf(stderr, "Failed to bind USB function\n");
- goto err_cleanup;
+ goto err_rmgadget;
}
if (mount_ffs() < 0) {
fprintf(stderr, "Failed to mount functionfs\n");
- goto err_cleanup;
+ goto err_rmgadget;
}
if (usb_setup_ffs_daemon(ctx) < 0) {
pthread_cond_wait(&ctx->ready, &ctx->mutex);
pthread_mutex_unlock(&ctx->mutex);
+ ctx->gadget = g;
ctx->usbg = s;
intf->txd = ctx->eps[TFMFFS_EP_BULK_IN];
err_umount:
umount_ffs();
+err_rmgadget:
+ usbg_rm_gadget(g, USBG_RM_RECURSE);
+
err_cleanup:
usbg_cleanup(s);
close(ep[i]);
usb_cleanup_ffs_daemon(ctx);
+ if (ctx->gadget)
+ usbg_disable_gadget(ctx->gadget);
umount_ffs();
+ if (ctx->gadget)
+ usbg_rm_gadget(ctx->gadget, USBG_RM_RECURSE);
if (ctx->usbg)
usbg_cleanup(ctx->usbg);
+ free(ctx);
+ intf->priv = NULL;
return 0;
}