:)
[platform/kernel/u-boot.git] / cmd / thordown.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * cmd_thordown.c -- USB TIZEN "THOR" Downloader gadget
4  *
5  * Copyright (C) 2013 Lukasz Majewski <l.majewski@samsung.com>
6  * All rights reserved.
7  */
8
9 #include <common.h>
10 #include <command.h>
11 #include <thor.h>
12 #include <dfu.h>
13 #include <g_dnl.h>
14 #include <usb.h>
15 #include <libtizen.h>
16
17 int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
18 {
19         char *usb_controller;
20         char *interface;
21         char *devstring;
22         int ret;
23
24         puts("TIZEN \"THOR\" Downloader\n");
25
26         switch (argc) {
27         case 1:
28                 usb_controller = strdup(env_get("dfu_usb_con"));
29                 interface = strdup(env_get("dfu_interface"));
30                 devstring = strdup(env_get("dfu_device"));
31
32                 if (!usb_controller || !interface || !devstring) {
33                         puts("DFU: default device environment is not set.\n");
34                         ret = CMD_RET_USAGE;
35                         goto bad_args;
36                 }
37                 break;
38         case 4:
39                 usb_controller = argv[1];
40                 interface = argv[2];
41                 devstring = argv[3];
42                 break;
43         default:
44                 return CMD_RET_USAGE;
45         }
46
47         ret = dfu_init_env_entities(interface, devstring);
48         if (ret)
49                 goto done;
50
51         int controller_index = simple_strtoul(usb_controller, NULL, 0);
52         ret = usb_gadget_initialize(controller_index);
53         if (ret) {
54                 pr_err("USB init failed: %d\n", ret);
55                 ret = CMD_RET_FAILURE;
56                 goto exit;
57         }
58
59         ret = g_dnl_register("usb_dnl_thor");
60         if (ret) {
61                 pr_err("g_dnl_register failed %d\n", ret);
62                 ret = CMD_RET_FAILURE;
63                 goto exit;
64         }
65
66         ret = thor_init();
67         if (ret) {
68                 if (ret == -EINTR) {
69                         printk("THOR DOWNLOAD stopped\n");
70                         ret = CMD_RET_SUCCESS;
71                 } else {
72                         printk("THOR DOWNLOAD failed: %d\n", ret);
73                         ret = CMD_RET_FAILURE;
74                 }
75                 goto exit;
76         }
77
78         do {
79                 ret = thor_handle();
80                 if (ret == THOR_DFU_REINIT_NEEDED) {
81                         dfu_free_entities();
82                         ret = dfu_init_env_entities(interface, devstring);
83                 }
84                 if (ret) {
85                         pr_err("THOR failed: %d\n", ret);
86                         ret = CMD_RET_FAILURE;
87                         goto exit;
88                 }
89         } while (ret == 0);
90 exit:
91         g_dnl_unregister();
92         usb_gadget_release(controller_index);
93 done:
94         dfu_free_entities();
95
96 #ifdef CONFIG_TIZEN
97 #ifdef CONFIG_LCD       /* TODO : Need to enable LCD */
98         if (ret != CMD_RET_SUCCESS)
99                 draw_thor_fail_screen();
100         else
101                 lcd_clear();
102 #endif
103         if (ret != CMD_RET_SUCCESS)
104                 thor_status_notify(THOR_NOTIFY_DOWNLOAD_FAILED, NULL);
105 #endif
106 bad_args:
107         if (argc == 1) {
108                 free(usb_controller);
109                 free(interface);
110                 free(devstring);
111         }
112         return ret;
113 }
114
115 __weak void thor_status_notify(enum thor_notify_type type,
116                         struct thor_notify_data *data)
117 {
118 }
119
120 U_BOOT_CMD(thordown, CONFIG_SYS_MAXARGS, 1, do_thor_down,
121            "TIZEN \"THOR\" downloader",
122            "<USB_controller> <interface> <dev>\n"
123            "  - device software upgrade via LTHOR TIZEN download\n"
124            "    program via <USB_controller> on device <dev>,\n"
125            "    attached to interface <interface>\n"
126 );