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