xref: /openbmc/u-boot/drivers/usb/gadget/g_dnl.c (revision 1d6edcbf)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * g_dnl.c -- USB Downloader Gadget
4  *
5  * Copyright (C) 2012 Samsung Electronics
6  * Lukasz Majewski  <l.majewski@samsung.com>
7  */
8 
9 #include <common.h>
10 #include <malloc.h>
11 
12 #include <mmc.h>
13 #include <part.h>
14 #include <usb.h>
15 
16 #include <g_dnl.h>
17 #include <usb_mass_storage.h>
18 #include <dfu.h>
19 #include <thor.h>
20 
21 #include <env_callback.h>
22 
23 #include "gadget_chips.h"
24 #include "composite.c"
25 
26 /*
27  * One needs to define the following:
28  * CONFIG_USB_GADGET_VENDOR_NUM
29  * CONFIG_USB_GADGET_PRODUCT_NUM
30  * CONFIG_USB_GADGET_MANUFACTURER
31  * at e.g. ./configs/<board>_defconfig
32  */
33 
34 #define STRING_MANUFACTURER 25
35 #define STRING_PRODUCT 2
36 /* Index of String Descriptor describing this configuration */
37 #define STRING_USBDOWN 2
38 /* Index of String serial */
39 #define STRING_SERIAL  3
40 #define MAX_STRING_SERIAL	256
41 /* Number of supported configurations */
42 #define CONFIGURATION_NUMBER 1
43 
44 #define DRIVER_VERSION		"usb_dnl 2.0"
45 
46 static const char product[] = "USB download gadget";
47 static char g_dnl_serial[MAX_STRING_SERIAL];
48 static const char manufacturer[] = CONFIG_USB_GADGET_MANUFACTURER;
49 
50 void g_dnl_set_serialnumber(char *s)
51 {
52 	memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
53 	strncpy(g_dnl_serial, s, MAX_STRING_SERIAL - 1);
54 }
55 
56 static struct usb_device_descriptor device_desc = {
57 	.bLength = sizeof device_desc,
58 	.bDescriptorType = USB_DT_DEVICE,
59 
60 	.bcdUSB = __constant_cpu_to_le16(0x0200),
61 	.bDeviceClass = USB_CLASS_PER_INTERFACE,
62 	.bDeviceSubClass = 0, /*0x02:CDC-modem , 0x00:CDC-serial*/
63 
64 	.idVendor = __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM),
65 	.idProduct = __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM),
66 	/* .iProduct = DYNAMIC */
67 	/* .iSerialNumber = DYNAMIC */
68 	.bNumConfigurations = 1,
69 };
70 
71 /*
72  * static strings, in UTF-8
73  * IDs for those strings are assigned dynamically at g_dnl_bind()
74  */
75 static struct usb_string g_dnl_string_defs[] = {
76 	{.s = manufacturer},
77 	{.s = product},
78 	{.s = g_dnl_serial},
79 	{ }		/* end of list */
80 };
81 
82 static struct usb_gadget_strings g_dnl_string_tab = {
83 	.language = 0x0409, /* en-us */
84 	.strings = g_dnl_string_defs,
85 };
86 
87 static struct usb_gadget_strings *g_dnl_composite_strings[] = {
88 	&g_dnl_string_tab,
89 	NULL,
90 };
91 
92 static int g_dnl_unbind(struct usb_composite_dev *cdev)
93 {
94 	struct usb_gadget *gadget = cdev->gadget;
95 
96 	debug("%s: calling usb_gadget_disconnect for "
97 			"controller '%s'\n", __func__, gadget->name);
98 	usb_gadget_disconnect(gadget);
99 
100 	return 0;
101 }
102 
103 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
104 {
105 	return ll_entry_start(struct g_dnl_bind_callback,
106 				g_dnl_bind_callbacks);
107 }
108 
109 static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
110 {
111 	return ll_entry_end(struct g_dnl_bind_callback,
112 				g_dnl_bind_callbacks);
113 }
114 
115 static int g_dnl_do_config(struct usb_configuration *c)
116 {
117 	const char *s = c->cdev->driver->name;
118 	struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
119 
120 	debug("%s: configuration: 0x%p composite dev: 0x%p\n",
121 	      __func__, c, c->cdev);
122 
123 	for (; callback != g_dnl_bind_callback_end(); callback++)
124 		if (!strcmp(s, callback->usb_function_name))
125 			return callback->fptr(c);
126 	return -ENODEV;
127 }
128 
129 static int g_dnl_config_register(struct usb_composite_dev *cdev)
130 {
131 	struct usb_configuration *config;
132 	const char *name = "usb_dnload";
133 
134 	config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
135 	if (!config)
136 		return -ENOMEM;
137 
138 	memset(config, 0, sizeof(*config));
139 
140 	config->label = name;
141 	config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
142 	config->bConfigurationValue = CONFIGURATION_NUMBER;
143 	config->iConfiguration = STRING_USBDOWN;
144 	config->bind = g_dnl_do_config;
145 
146 	return usb_add_config(cdev, config);
147 }
148 
149 __weak
150 int board_usb_init(int index, enum usb_init_type init)
151 {
152 	return 0;
153 }
154 
155 __weak
156 int board_usb_cleanup(int index, enum usb_init_type init)
157 {
158 	return 0;
159 }
160 
161 __weak
162 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
163 {
164 	return 0;
165 }
166 
167 __weak int g_dnl_get_board_bcd_device_number(int gcnum)
168 {
169 	return gcnum;
170 }
171 
172 __weak int g_dnl_board_usb_cable_connected(void)
173 {
174 	return -EOPNOTSUPP;
175 }
176 
177 static bool g_dnl_detach_request;
178 
179 bool g_dnl_detach(void)
180 {
181 	return g_dnl_detach_request;
182 }
183 
184 void g_dnl_trigger_detach(void)
185 {
186 	g_dnl_detach_request = true;
187 }
188 
189 void g_dnl_clear_detach(void)
190 {
191 	g_dnl_detach_request = false;
192 }
193 
194 static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
195 {
196 	struct usb_gadget *gadget = cdev->gadget;
197 	int gcnum;
198 
199 	gcnum = usb_gadget_controller_number(gadget);
200 	if (gcnum > 0)
201 		gcnum += 0x200;
202 
203 	return g_dnl_get_board_bcd_device_number(gcnum);
204 }
205 
206 /**
207  * Update internal serial number variable when the "serial#" env var changes.
208  *
209  * Handle all cases, even when flags == H_PROGRAMMATIC or op == env_op_delete.
210  */
211 static int on_serialno(const char *name, const char *value, enum env_op op,
212 		int flags)
213 {
214 	g_dnl_set_serialnumber((char *)value);
215 	return 0;
216 }
217 U_BOOT_ENV_CALLBACK(serialno, on_serialno);
218 
219 static int g_dnl_bind(struct usb_composite_dev *cdev)
220 {
221 	struct usb_gadget *gadget = cdev->gadget;
222 	int id, ret;
223 	int gcnum;
224 
225 	debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);
226 
227 	id = usb_string_id(cdev);
228 
229 	if (id < 0)
230 		return id;
231 	g_dnl_string_defs[0].id = id;
232 	device_desc.iManufacturer = id;
233 
234 	id = usb_string_id(cdev);
235 	if (id < 0)
236 		return id;
237 
238 	g_dnl_string_defs[1].id = id;
239 	device_desc.iProduct = id;
240 
241 	g_dnl_bind_fixup(&device_desc, cdev->driver->name);
242 
243 	if (strlen(g_dnl_serial)) {
244 		id = usb_string_id(cdev);
245 		if (id < 0)
246 			return id;
247 
248 		g_dnl_string_defs[2].id = id;
249 		device_desc.iSerialNumber = id;
250 	}
251 
252 	ret = g_dnl_config_register(cdev);
253 	if (ret)
254 		goto error;
255 
256 	gcnum = g_dnl_get_bcd_device_number(cdev);
257 	if (gcnum >= 0)
258 		device_desc.bcdDevice = cpu_to_le16(gcnum);
259 	else {
260 		debug("%s: controller '%s' not recognized\n",
261 			__func__, gadget->name);
262 		device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
263 	}
264 
265 	debug("%s: calling usb_gadget_connect for "
266 			"controller '%s'\n", __func__, gadget->name);
267 	usb_gadget_connect(gadget);
268 
269 	return 0;
270 
271  error:
272 	g_dnl_unbind(cdev);
273 	return -ENOMEM;
274 }
275 
276 static struct usb_composite_driver g_dnl_driver = {
277 	.name = NULL,
278 	.dev = &device_desc,
279 	.strings = g_dnl_composite_strings,
280 
281 	.bind = g_dnl_bind,
282 	.unbind = g_dnl_unbind,
283 };
284 
285 /*
286  * NOTICE:
287  * Registering via USB function name won't be necessary after rewriting
288  * g_dnl to support multiple USB functions.
289  */
290 int g_dnl_register(const char *name)
291 {
292 	int ret;
293 
294 	debug("%s: g_dnl_driver.name = %s\n", __func__, name);
295 	g_dnl_driver.name = name;
296 
297 	ret = usb_composite_register(&g_dnl_driver);
298 	if (ret) {
299 		printf("%s: failed!, error: %d\n", __func__, ret);
300 		return ret;
301 	}
302 	return 0;
303 }
304 
305 void g_dnl_unregister(void)
306 {
307 	usb_composite_unregister(&g_dnl_driver);
308 }
309