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