g_dnl.c 6.18 KB
Newer Older
1
2
3
4
5
6
/*
 * g_dnl.c -- USB Downloader Gadget
 *
 * Copyright (C) 2012 Samsung Electronics
 * Lukasz Majewski  <l.majewski@samsung.com>
 *
7
 * SPDX-License-Identifier:	GPL-2.0+
8
9
10
11
12
13
14
 */

#include <common.h>
#include <malloc.h>

#include <mmc.h>
#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
23
24
25
26
27
28
29
30
31
32
33
34

#include "gadget_chips.h"
#include "composite.c"

/*
 * One needs to define the following:
 * CONFIG_G_DNL_VENDOR_NUM
 * CONFIG_G_DNL_PRODUCT_NUM
 * CONFIG_G_DNL_MANUFACTURER
 * at e.g. ./include/configs/<board>.h
 */

#define STRING_MANUFACTURER 25
#define STRING_PRODUCT 2
35
/* Index of String Descriptor describing this configuration */
36
#define STRING_USBDOWN 2
37
38
39
/* Index of String serial */
#define STRING_SERIAL  3
#define MAX_STRING_SERIAL	32
40
41
/* Number of supported configurations */
#define CONFIGURATION_NUMBER 1
42
43
44
45

#define DRIVER_VERSION		"usb_dnl 2.0"

static const char product[] = "USB download gadget";
46
static char g_dnl_serial[MAX_STRING_SERIAL];
47
48
static const char manufacturer[] = CONFIG_G_DNL_MANUFACTURER;

49
50
51
52
53
54
55
void g_dnl_set_serialnumber(char *s)
{
	memset(g_dnl_serial, 0, MAX_STRING_SERIAL);
	if (strlen(s) < MAX_STRING_SERIAL)
		strncpy(g_dnl_serial, s, strlen(s));
}

56
57
58
59
60
61
62
63
64
65
66
static struct usb_device_descriptor device_desc = {
	.bLength = sizeof device_desc,
	.bDescriptorType = USB_DT_DEVICE,

	.bcdUSB = __constant_cpu_to_le16(0x0200),
	.bDeviceClass = USB_CLASS_COMM,
	.bDeviceSubClass = 0x02, /*0x02:CDC-modem , 0x00:CDC-serial*/

	.idVendor = __constant_cpu_to_le16(CONFIG_G_DNL_VENDOR_NUM),
	.idProduct = __constant_cpu_to_le16(CONFIG_G_DNL_PRODUCT_NUM),
	.iProduct = STRING_PRODUCT,
67
	.iSerialNumber = STRING_SERIAL,
68
69
70
	.bNumConfigurations = 1,
};

71
72
73
74
/*
 * static strings, in UTF-8
 * IDs for those strings are assigned dynamically at g_dnl_bind()
 */
75
static struct usb_string g_dnl_string_defs[] = {
76
77
	{.s = manufacturer},
	{.s = product},
78
	{.s = g_dnl_serial},
79
	{ }		/* end of list */
80
81
82
83
84
85
86
87
88
89
90
91
92
93
};

static struct usb_gadget_strings g_dnl_string_tab = {
	.language = 0x0409, /* en-us */
	.strings = g_dnl_string_defs,
};

static struct usb_gadget_strings *g_dnl_composite_strings[] = {
	&g_dnl_string_tab,
	NULL,
};

static int g_dnl_unbind(struct usb_composite_dev *cdev)
{
94
95
96
	struct usb_gadget *gadget = cdev->gadget;

	debug("%s: calling usb_gadget_disconnect for "
97
			"controller '%s'\n", __func__, gadget->name);
98
99
	usb_gadget_disconnect(gadget);

100
101
102
	return 0;
}

103
104
105
106
107
108
109
110
111
112
113
114
static inline struct g_dnl_bind_callback *g_dnl_bind_callback_first(void)
{
	return ll_entry_start(struct g_dnl_bind_callback,
				g_dnl_bind_callbacks);
}

static inline struct g_dnl_bind_callback *g_dnl_bind_callback_end(void)
{
	return ll_entry_end(struct g_dnl_bind_callback,
				g_dnl_bind_callbacks);
}

115
116
117
static int g_dnl_do_config(struct usb_configuration *c)
{
	const char *s = c->cdev->driver->name;
118
	struct g_dnl_bind_callback *callback = g_dnl_bind_callback_first();
119
120
121
122

	debug("%s: configuration: 0x%p composite dev: 0x%p\n",
	      __func__, c, c->cdev);

123
124
125
126
	for (; callback != g_dnl_bind_callback_end(); callback++)
		if (!strcmp(s, callback->usb_function_name))
			return callback->fptr(c);
	return -ENODEV;
127
128
129
130
}

static int g_dnl_config_register(struct usb_composite_dev *cdev)
{
131
132
	struct usb_configuration *config;
	const char *name = "usb_dnload";
133

134
135
136
	config = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*config));
	if (!config)
		return -ENOMEM;
137

138
139
140
141
142
143
144
145
146
	memset(config, 0, sizeof(*config));

	config->label = name;
	config->bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER;
	config->bConfigurationValue = CONFIGURATION_NUMBER;
	config->iConfiguration = STRING_USBDOWN;
	config->bind = g_dnl_do_config;

	return usb_add_config(cdev, config);
147
148
}

149
150
151
152
153
154
155
156
157
158
159
160
__weak
int board_usb_init(int index, enum usb_init_type init)
{
	return 0;
}

__weak
int board_usb_cleanup(int index, enum usb_init_type init)
{
	return 0;
}

161
__weak
162
int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
163
164
165
166
{
	return 0;
}

167
168
169
170
171
__weak int g_dnl_get_board_bcd_device_number(int gcnum)
{
	return gcnum;
}

172
173
174
175
176
__weak int g_dnl_board_usb_cable_connected(void)
{
	return -EOPNOTSUPP;
}

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static bool g_dnl_detach_request;

bool g_dnl_detach(void)
{
	return g_dnl_detach_request;
}

void g_dnl_trigger_detach(void)
{
	g_dnl_detach_request = true;
}

void g_dnl_clear_detach(void)
{
	g_dnl_detach_request = false;
}

194
195
196
197
198
199
200
201
202
203
204
205
static int g_dnl_get_bcd_device_number(struct usb_composite_dev *cdev)
{
	struct usb_gadget *gadget = cdev->gadget;
	int gcnum;

	gcnum = usb_gadget_controller_number(gadget);
	if (gcnum > 0)
		gcnum += 0x200;

	return g_dnl_get_board_bcd_device_number(gcnum);
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
static int g_dnl_bind(struct usb_composite_dev *cdev)
{
	struct usb_gadget *gadget = cdev->gadget;
	int id, ret;
	int gcnum;

	debug("%s: gadget: 0x%p cdev: 0x%p\n", __func__, gadget, cdev);

	id = usb_string_id(cdev);

	if (id < 0)
		return id;
	g_dnl_string_defs[0].id = id;
	device_desc.iManufacturer = id;

	id = usb_string_id(cdev);
	if (id < 0)
		return id;

	g_dnl_string_defs[1].id = id;
	device_desc.iProduct = id;

228
229
230
231
232
233
234
	id = usb_string_id(cdev);
	if (id < 0)
		return id;

	g_dnl_string_defs[2].id = id;
	device_desc.iSerialNumber = id;

235
	g_dnl_bind_fixup(&device_desc, cdev->driver->name);
236
237
238
239
	ret = g_dnl_config_register(cdev);
	if (ret)
		goto error;

240
	gcnum = g_dnl_get_bcd_device_number(cdev);
241
	if (gcnum >= 0)
242
		device_desc.bcdDevice = cpu_to_le16(gcnum);
243
244
	else {
		debug("%s: controller '%s' not recognized\n",
245
			__func__, gadget->name);
246
247
248
		device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
	}

249
	debug("%s: calling usb_gadget_connect for "
250
			"controller '%s'\n", __func__, gadget->name);
251
252
	usb_gadget_connect(gadget);

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	return 0;

 error:
	g_dnl_unbind(cdev);
	return -ENOMEM;
}

static struct usb_composite_driver g_dnl_driver = {
	.name = NULL,
	.dev = &device_desc,
	.strings = g_dnl_composite_strings,

	.bind = g_dnl_bind,
	.unbind = g_dnl_unbind,
};

269
270
271
272
273
274
/*
 * NOTICE:
 * Registering via USB function name won't be necessary after rewriting
 * g_dnl to support multiple USB functions.
 */
int g_dnl_register(const char *name)
275
{
276
	int ret;
277

278
	debug("%s: g_dnl_driver.name = %s\n", __func__, name);
279
280
	g_dnl_driver.name = name;

281
	ret = usb_composite_register(&g_dnl_driver);
282
283
284
285
286
287
288
289
290
291
292
	if (ret) {
		printf("%s: failed!, error: %d\n", __func__, ret);
		return ret;
	}
	return 0;
}

void g_dnl_unregister(void)
{
	usb_composite_unregister(&g_dnl_driver);
}