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 <linux/printk.h>
16
17int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
18{
19	char *interface, *devstring;
20	int controller_index;
21	struct udevice *udc;
22	int ret;
23
24	if (argc < 4)
25		return CMD_RET_USAGE;
26
27	puts("TIZEN \"THOR\" Downloader\n");
28
29	interface = argv[2];
30	devstring = argv[3];
31
32	ret = dfu_init_env_entities(interface, devstring);
33	if (ret)
34		goto done;
35
36	controller_index = simple_strtoul(argv[1], NULL, 0);
37	ret = udc_device_get_by_index(controller_index, &udc);
38	if (ret) {
39		pr_err("USB init failed: %d\n", ret);
40		ret = CMD_RET_FAILURE;
41		goto exit;
42	}
43
44	ret = g_dnl_register("usb_dnl_thor");
45	if (ret) {
46		pr_err("g_dnl_register failed %d\n", ret);
47		ret = CMD_RET_FAILURE;
48		goto exit;
49	}
50
51	ret = thor_init(udc);
52	if (ret) {
53		pr_err("THOR DOWNLOAD failed: %d\n", ret);
54		ret = CMD_RET_FAILURE;
55		goto exit;
56	}
57
58	do {
59		ret = thor_handle(udc);
60		if (ret == THOR_DFU_REINIT_NEEDED) {
61			dfu_free_entities();
62			ret = dfu_init_env_entities(interface, devstring);
63		}
64		if (ret) {
65			pr_err("THOR failed: %d\n", ret);
66			ret = CMD_RET_FAILURE;
67			goto exit;
68		}
69	} while (ret == 0);
70exit:
71	g_dnl_unregister();
72	udc_device_put(udc);
73done:
74	dfu_free_entities();
75
76	return ret;
77}
78
79U_BOOT_CMD(thordown, CONFIG_SYS_MAXARGS, 1, do_thor_down,
80	   "TIZEN \"THOR\" downloader",
81	   "<USB_controller> <interface> <dev>\n"
82	   "  - device software upgrade via LTHOR TIZEN download\n"
83	   "    program via <USB_controller> on device <dev>,\n"
84	   "	attached to interface <interface>\n"
85);
86