usb: properly clean up usb gadget 01/240101/1 accepted/tizen/unified/20200804.142348 submit/tizen/20200804.050420
authorSeung-Woo Kim <sw0312.kim@samsung.com>
Mon, 3 Aug 2020 08:09:37 +0000 (17:09 +0900)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Mon, 3 Aug 2020 09:02:36 +0000 (18:02 +0900)
If error occurs during flashing, usb gadget is not cleaned up and
the second tfm session does not work for usb gadget. Properly clean
up usb gadget including usbg_disable_gadget() and usbg_rm_gadget().

Change-Id: I702deaa009642e3ead45d40abb20c3bf821f1fb7
Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
packaging/initrd-flash.spec
src/usb.c

index c991b35..05fdf38 100644 (file)
@@ -1,6 +1,6 @@
 Name:       initrd-flash
 Summary:    Advanced flash-manager, package for building ramdisk-recovery.img
-Version:    0.0.2
+Version:    0.0.3
 Release:    0
 Group:      System/Utilities
 License:    Apache-2.0
index 606a2a7..9705b90 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -50,6 +50,7 @@ enum {
 
 struct usb_context {
        usbg_state *usbg;
+       usbg_gadget *gadget;
        pthread_t ep0_thread;
        int eps[TFMFFS_EP_MAX];
 
@@ -408,24 +409,24 @@ static int usb_connect(struct tfm_interface *intf)
        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) {
@@ -444,6 +445,7 @@ static int usb_connect(struct tfm_interface *intf)
                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];
@@ -458,6 +460,9 @@ err_ffs:
 err_umount:
        umount_ffs();
 
+err_rmgadget:
+       usbg_rm_gadget(g, USBG_RM_RECURSE);
+
 err_cleanup:
        usbg_cleanup(s);
 
@@ -477,9 +482,15 @@ static int usb_disconnect(struct tfm_interface *intf)
                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;
 }