usb: ums: add ums exit feature by ctrl+c or by detach usb cable
authorPrzemyslaw Marczak <p.marczak@samsung.com>
Wed, 23 Oct 2013 12:30:46 +0000 (14:30 +0200)
committerChanho Park <chanho61.park@samsung.com>
Fri, 24 Jul 2015 07:29:57 +0000 (16:29 +0900)
This patch allows exiting from UMS mode to u-boot prompt
by detaching usb cable or by pressing ctrl+c.

Add new config: CONFIG_USB_CABLE_CHECK. If defined then board
file should provide function: usb_cable_connected() (include/usb.h)
that return 1 if cable is connected and 0 otherwise.

Changes v2:
- add a note to the README

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Cc: Marek Vasut <marex@denx.de>
README
common/cmd_usb_mass_storage.c
drivers/usb/gadget/f_mass_storage.c
include/usb.h

diff --git a/README b/README
index cb9be7cf2fd54fc11fdb7066bc1ed8877e4c2ac7..b26e282d6c8324e7b9ef544a5367267b5c3bf107 100644 (file)
--- a/README
+++ b/README
@@ -1225,6 +1225,13 @@ The following options need to be configured:
                        for your device
                        - CONFIG_USBD_PRODUCTID 0xFFFF
 
+               Some USB device drivers may need to check USB cable attachment.
+               In this case you can enable following config in BoardName.h:
+                       CONFIG_USB_CABLE_CHECK
+                       This enables function definition:
+                       - usb_cable_connected() in include/usb.h
+                       Implementation of this function is board-specific.
+
 - ULPI Layer Support:
                The ULPI (UTMI Low Pin (count) Interface) PHYs are supported via
                the generic ULPI layer. The generic layer accesses the ULPI PHY
index f360c2d2c91ec7e5a77a64b8fb4ab38ab2b8a979..f1bc73a8fb85750042494637131271edb789bb86 100644 (file)
@@ -18,6 +18,7 @@
  * MA 02111-1307 USA
  */
 
+#include <errno.h>
 #include <common.h>
 #include <command.h>
 #include <g_dnl.h>
@@ -55,16 +56,20 @@ int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
        g_dnl_register("ums");
 
        while (1) {
-               /* Handle control-c and timeouts */
-               if (ctrlc()) {
-                       error("The remote end did not respond in time.");
-                       goto exit;
-               }
-
                usb_gadget_handle_interrupts();
-               /* Check if USB cable has been detached */
-               if (fsg_main_thread(NULL) == EIO)
+
+               rc = fsg_main_thread(NULL);
+               if (rc) {
+                       /* Check I/O error */
+                       if (rc == -EIO)
+                               printf("\rCheck USB cable connection\n");
+
+                       /* Check CTRL+C */
+                       if (rc == -EPIPE)
+                               printf("\rCTRL+C - Operation aborted\n");
+
                        goto exit;
+               }
        }
 exit:
        g_dnl_unregister();
index b948128c0e2fc40a616851f00b2a70292102d82e..b642ead13a8995cb2c511a575ac7d5d91d3408c4 100644 (file)
 #include <config.h>
 #include <malloc.h>
 #include <common.h>
+#include <usb.h>
 
 #include <linux/err.h>
 #include <linux/usb/ch9.h>
@@ -706,6 +707,18 @@ static int sleep_thread(struct fsg_common *common)
                        k++;
                }
 
+               if (k == 10) {
+                       /* Handle CTRL+C */
+                       if (ctrlc())
+                               return -EPIPE;
+#ifdef CONFIG_USB_CABLE_CHECK
+                       /* Check cable connection */
+                       if (!usb_cable_connected())
+                               return -EIO;
+#endif
+                       k = 0;
+               }
+
                usb_gadget_handle_interrupts();
        }
        common->thread_wakeup_needed = 0;
@@ -2418,6 +2431,7 @@ static void handle_exception(struct fsg_common *common)
 
 int fsg_main_thread(void *common_)
 {
+       int ret;
        struct fsg_common       *common = the_fsg_common;
        /* The main loop */
        do {
@@ -2427,12 +2441,16 @@ int fsg_main_thread(void *common_)
                }
 
                if (!common->running) {
-                       sleep_thread(common);
+                       ret = sleep_thread(common);
+                       if (ret)
+                               return ret;
+
                        continue;
                }
 
-               if (get_next_command(common))
-                       continue;
+               ret = get_next_command(common);
+               if (ret)
+                       return ret;
 
                if (!exception_in_progress(common))
                        common->state = FSG_STATE_DATA_PHASE;
index 912342231e82e9196d77db9569624bb86088210c..bac24c906e73bb2a00705780211957de9a1474e8 100644 (file)
@@ -214,6 +214,16 @@ int board_usb_init(int index, enum usb_init_type init);
  */
 int board_usb_cleanup(int index, enum usb_init_type init);
 
+/*
+ * If CONFIG_USB_CABLE_CHECK is set then this function
+ * should be defined in board file.
+ *
+ * @return 1 if cable is connected and 0 otherwise.
+ */
+#ifdef CONFIG_USB_CABLE_CHECK
+int usb_cable_connected(void);
+#endif
+
 #ifdef CONFIG_USB_STORAGE
 
 #define USB_MAX_STOR_DEV 5