xref: /rk3399_rockchip-uboot/drivers/usb/gadget/g_dnl.c (revision d6eae7b0b172b3ef31a3f816ce946857f382ac4e)
1 /*
2  * g_dnl.c -- USB Downloader Gadget
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * Lukasz Majewski  <l.majewski@samsung.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <malloc.h>
12 
13 #include <mmc.h>
14 #include <part.h>
15 
16 #include <g_dnl.h>
17 #include <usb_mass_storage.h>
18 #include <dfu.h>
19 
20 #include "gadget_chips.h"
21 #include "composite.c"
22 
23 /*
24  * One needs to define the following:
25  * CONFIG_G_DNL_VENDOR_NUM
26  * CONFIG_G_DNL_PRODUCT_NUM
27  * CONFIG_G_DNL_MANUFACTURER
28  * at e.g. ./include/configs/<board>.h
29  */
30 
31 #define STRING_MANUFACTURER 25
32 #define STRING_PRODUCT 2
33 /* Index of String Descriptor describing this configuration */
34 #define STRING_USBDOWN 2
35 /* Number of supported configurations */
36 #define CONFIGURATION_NUMBER 1
37 
38 #define DRIVER_VERSION		"usb_dnl 2.0"
39 
40 static const char shortname[] = "usb_dnl_";
41 static const char product[] = "USB download gadget";
42 static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;
43 
44 static struct usb_device_descriptor device_desc = {
45 	.bLength = sizeof device_desc,
46 	.bDescriptorType = USB_DT_DEVICE,
47 
48 	.bcdUSB = __constant_cpu_to_le16(0x0200),
49 	.bDeviceClass = USB_CLASS_COMM,
50 	.bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/
51 
52 	.idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
53 	.idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
54 	.iProduct = STRING_PRODUCT,
55 	.bNumConfigurations = 1,
56 };
57 
58 /*
59  * static strings, in UTF-8
60  * IDs for those strings are assigned dynamically at g_dnl_bind()
61  */
62 static struct usb_string g_dnl_string_defs[] = {
63 	{.s = manufacturer},
64 	{.s = product},
65 	{ }		/* end of list */
66 };
67 
68 static struct usb_gadget_strings g_dnl_string_tab = {
69 	.language = 0x0409, /* en-us */
70 	.strings = g_dnl_string_defs,
71 };
72 
73 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
74 	&g_dnl_string_tab,
75 	NULL,
76 };
77 
78 static int g_dnl_unbind(struct usb_composite_dev *cdev)
79 {
80 	struct usb_gadget *gadget = cdev->gadget;
81 
82 	free(cdev->config);
83 	cdev->config = NULL;
84 	debug("%s: calling usb_gadget_disconnect for "
85 			"controller '%s'\n", shortname, gadget->name);
86 	usb_gadget_disconnect(gadget);
87 
88 	return 0;
89 }
90 
91 static int g_dnl_do_config(struct usb_configuration *c)
92 {
93 	const char *s = c->cdev->driver->name;
94 	int ret = -1;
95 
96 	debug("%s: configuration: 0x%p composite dev: 0x%p\n",
97 	      __func__, c, c->cdev);
98 
99 	printf("GADGET DRIVER: %s\n", s);
100 	if (!strcmp(s, "usb_dnl_dfu"))
101 		ret = dfu_add(c);
102 	else if (!strcmp(s, "usb_dnl_ums"))
103 		ret = fsg_add(c);
104 
105 	return ret;
106 }
107 
108 static int g_dnl_config_register(struct usb_composite_dev *cdev)
109 {
110 	struct usb_configuration *config;
111 	const char *name = "usb_dnload";
112 
113 	config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
114 	if (!config)
115 		return -ENOMEM;
116 
117 	memset(config, 0, sizeof(*config));
118 
119 	config->label = name;
120 	config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
121 	config->bConfigurationValue = CONFIGURATION_NUMBER;
122 	config->iConfiguration = STRING_USBDOWN;
123 	config->bind = g_dnl_do_config;
124 
125 	return usb_add_config(cdev, config);
126 }
127 
128 __weak
129 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
130 {
131 	return 0;
132 }
133 
134 static int g_dnl_bind(struct usb_composite_dev *cdev)
135 {
136 	struct usb_gadget *gadget = cdev->gadget;
137 	int id, ret;
138 	int gcnum;
139 
140 	debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
141 
142 	id = usb_string_id(cdev);
143 
144 	if (id < 0)
145 		return id;
146 	g_dnl_string_defs[0].id = id;
147 	device_desc.iManufacturer = id;
148 
149 	id = usb_string_id(cdev);
150 	if (id < 0)
151 		return id;
152 
153 	g_dnl_string_defs[1].id = id;
154 	device_desc.iProduct = id;
155 
156 	g_dnl_bind_fixup(&device_desc, cdev->driver->name);
157 	ret = g_dnl_config_register(cdev);
158 	if (ret)
159 		goto error;
160 
161 	gcnum = usb_gadget_controller_number(gadget);
162 
163 	debug("gcnum: %d\n", gcnum);
164 	if (gcnum >= 0)
165 		device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
166 	else {
167 		debug("%s: controller '%s' not recognized\n",
168 			shortname, gadget->name);
169 		device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
170 	}
171 
172 	debug("%s: calling usb_gadget_connect for "
173 			"controller '%s'\n", shortname, gadget->name);
174 	usb_gadget_connect(gadget);
175 
176 	return 0;
177 
178  error:
179 	g_dnl_unbind(cdev);
180 	return -ENOMEM;
181 }
182 
183 static struct usb_composite_driver g_dnl_driver = {
184 	.name = NULL,
185 	.dev = &device_desc,
186 	.strings = g_dnl_composite_strings,
187 
188 	.bind = g_dnl_bind,
189 	.unbind = g_dnl_unbind,
190 };
191 
192 int g_dnl_register(const char *type)
193 {
194 	/* We only allow "dfu" atm, so 3 should be enough */
195 	static char name[sizeof(shortname) + 3];
196 	int ret;
197 
198 	if (!strcmp(type, "dfu")) {
199 		strcpy(name, shortname);
200 		strcat(name, type);
201 	} else if (!strcmp(type, "ums")) {
202 		strcpy(name, shortname);
203 		strcat(name, type);
204 	} else {
205 		printf("%s: unknown command: %s\n", __func__, type);
206 		return -EINVAL;
207 	}
208 
209 	g_dnl_driver.name = name;
210 
211 	debug("%s: g_dnl_driver.name: %s\n", __func__, g_dnl_driver.name);
212 	ret = usb_composite_register(&g_dnl_driver);
213 
214 	if (ret) {
215 		printf("%s: failed!, error: %d\n", __func__, ret);
216 		return ret;
217 	}
218 
219 	return 0;
220 }
221 
222 void g_dnl_unregister(void)
223 {
224 	usb_composite_unregister(&g_dnl_driver);
225 }
226