aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_desc.c
blob: faebc13fd8a381b1ee60662da4542d22267289f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* DFU related USB Descriptors */
/* (C) 2006-2017 Harald Welte <hwelte@hmw-consulting.de> */

#include <unistd.h>

#include "board.h"
#include "utils.h"

#include <usb/include/USBDescriptors.h>

#include <usb/include/USBDDriver.h>

#include <usb/common/dfu/usb_dfu.h>
#include <usb/device/dfu/dfu.h>

#include "usb_strings_generated.h"

enum {
	STR_MANUF	= 1,
	STR_PROD,
	STR_CONFIG,
	// strings for the first alternate interface (e.g. DFU)
	_STR_FIRST_ALT,
	// serial string
	STR_SERIAL	= (_STR_FIRST_ALT + BOARD_DFU_NUM_IF),
	// version string (on additional interface)
	VERSION_CONF_STR,
	VERSION_STR,
	// count
	STRING_DESC_CNT,
};

/* string used to replace one of both DFU flash partition atlsettings */
static const unsigned char usb_string_notavailable[] = {
	USBStringDescriptor_LENGTH(13),
	USBGenericDescriptor_STRING,
	USBStringDescriptor_UNICODE('n'),
	USBStringDescriptor_UNICODE('o'),
	USBStringDescriptor_UNICODE('t'),
	USBStringDescriptor_UNICODE(' '),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('v'),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('i'),
	USBStringDescriptor_UNICODE('l'),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('b'),
	USBStringDescriptor_UNICODE('l'),
	USBStringDescriptor_UNICODE('e'),
};

/* USB string for the serial (using 128-bit device ID) */
static unsigned char usb_string_serial[] = {
	USBStringDescriptor_LENGTH(32),
	USBGenericDescriptor_STRING,
	USBStringDescriptor_UNICODE('0'),
	USBStringDescriptor_UNICODE('0'),
	USBStringDescriptor_UNICODE('1'),
	USBStringDescriptor_UNICODE('1'),
	USBStringDescriptor_UNICODE('2'),
	USBStringDescriptor_UNICODE('2'),
	USBStringDescriptor_UNICODE('3'),
	USBStringDescriptor_UNICODE('3'),
	USBStringDescriptor_UNICODE('4'),
	USBStringDescriptor_UNICODE('4'),
	USBStringDescriptor_UNICODE('5'),
	USBStringDescriptor_UNICODE('5'),
	USBStringDescriptor_UNICODE('6'),
	USBStringDescriptor_UNICODE('6'),
	USBStringDescriptor_UNICODE('7'),
	USBStringDescriptor_UNICODE('7'),
	USBStringDescriptor_UNICODE('8'),
	USBStringDescriptor_UNICODE('8'),
	USBStringDescriptor_UNICODE('9'),
	USBStringDescriptor_UNICODE('9'),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('b'),
	USBStringDescriptor_UNICODE('b'),
	USBStringDescriptor_UNICODE('c'),
	USBStringDescriptor_UNICODE('c'),
	USBStringDescriptor_UNICODE('d'),
	USBStringDescriptor_UNICODE('d'),
	USBStringDescriptor_UNICODE('e'),
	USBStringDescriptor_UNICODE('e'),
	USBStringDescriptor_UNICODE('f'),
	USBStringDescriptor_UNICODE('f'),
};

/* USB string for the version */
static const unsigned char usb_string_version_conf[] = {
	USBStringDescriptor_LENGTH(16),
	USBGenericDescriptor_STRING,
	USBStringDescriptor_UNICODE('f'),
	USBStringDescriptor_UNICODE('i'),
	USBStringDescriptor_UNICODE('r'),
	USBStringDescriptor_UNICODE('m'),
	USBStringDescriptor_UNICODE('w'),
	USBStringDescriptor_UNICODE('a'),
	USBStringDescriptor_UNICODE('r'),
	USBStringDescriptor_UNICODE('e'),
	USBStringDescriptor_UNICODE(' '),
	USBStringDescriptor_UNICODE('v'),
	USBStringDescriptor_UNICODE('e'),
	USBStringDescriptor_UNICODE('r'),
	USBStringDescriptor_UNICODE('s'),
	USBStringDescriptor_UNICODE('i'),
	USBStringDescriptor_UNICODE('o'),
	USBStringDescriptor_UNICODE('n'),
};

static const char git_version[] = GIT_VERSION;
static unsigned char usb_string_version[2 + ARRAY_SIZE(git_version) * 2 - 2];
/** array of static (from usb_strings) and runtime (serial, version) USB strings */
static const unsigned char *usb_strings_extended[STRING_DESC_CNT];

static const USBDeviceDescriptor fsDevice = {
	.bLength = 		sizeof(USBDeviceDescriptor),
	.bDescriptorType = 	USBGenericDescriptor_DEVICE,
	.bcdUSB = 		USBDeviceDescriptor_USB2_00,
	.bDeviceClass =		0,
	.bDeviceSubClass = 	0,
	.bDeviceProtocol = 	0,
	.bMaxPacketSize0 = 	USBEndpointDescriptor_MAXCTRLSIZE_FS,
	.idVendor =		BOARD_USB_VENDOR_ID,
	.idProduct = 		BOARD_DFU_USB_PRODUCT_ID,
	.bcdDevice = 		BOARD_USB_RELEASE,
	.iManufacturer =	STR_MANUF,
	.iProduct =		STR_PROD,
	.iSerialNumber =	STR_SERIAL,
	.bNumConfigurations =	2, // DFU + version configurations
};

/* Alternate Interface Descriptor, we use one per partition/memory type */
#define DFU_IF(ALT)								\
	{									\
		.bLength =		sizeof(USBInterfaceDescriptor),		\
		.bDescriptorType = 	USBGenericDescriptor_INTERFACE,		\
		.bInterfaceNumber =	0,					\
		.bAlternateSetting =	ALT,					\
		.bNumEndpoints =	0,					\
		.bInterfaceClass =	0xfe,					\
		.bInterfaceSubClass =	1,					\
		.iInterface =		(_STR_FIRST_ALT + ALT),			\
		.bInterfaceProtocol =	2,					\
	}

/* overall descriptor for the DFU configuration, including all
 * descriptors for alternate interfaces */
const struct dfu_desc dfu_cfg_descriptor = {
	.ucfg = {
		.bLength =	   sizeof(USBConfigurationDescriptor),
		.bDescriptorType = USBGenericDescriptor_CONFIGURATION,
		.wTotalLength =	   sizeof(struct dfu_desc),
		.bNumInterfaces =  1,
		.bConfigurationValue = 1,
		.iConfiguration =  STR_CONFIG,
		.bmAttributes =	   BOARD_USB_BMATTRIBUTES,
		.bMaxPower =	   100,
	},
	.uif[0] = DFU_IF(0),
#if BOARD_DFU_NUM_IF > 1
	.uif[1] = DFU_IF(1),
#endif
#if BOARD_DFU_NUM_IF > 2
	.uif[2] = DFU_IF(2),
#endif
#if BOARD_DFU_NUM_IF > 3
	.uif[3] = DFU_IF(3),
#endif
#if BOARD_DFU_NUM_IF > 4
	.uif[4] = DFU_IF(4),
#endif
	.func_dfu = DFU_FUNC_DESC
};

void set_usb_serial_str(void)
{
	unsigned int i;

	// put device ID into USB serial number description
	unsigned int device_id[4];
	EEFC_ReadUniqueID(device_id);
	char device_id_string[32 + 1];
	snprintf(device_id_string, ARRAY_SIZE(device_id_string), "%08x%08x%08x%08x",
		device_id[0], device_id[1], device_id[2], device_id[3]);
	for (i = 0; i < ARRAY_SIZE(device_id_string) - 1; i++) {
		usb_string_serial[2 + 2 * i] = device_id_string[i];
	}

	// put version into USB string
	usb_string_version[0] = USBStringDescriptor_LENGTH(ARRAY_SIZE(git_version) - 1);
	usb_string_version[1] = USBGenericDescriptor_STRING;
	for (i = 0; i < ARRAY_SIZE(git_version) - 1; i++) {
		usb_string_version[2 + i * 2 + 0] = git_version[i];
		usb_string_version[2 + i * 2 + 1] = 0;
	}

	// fill extended USB strings
	for (i = 0; i < ARRAY_SIZE(usb_strings) && i < ARRAY_SIZE(usb_strings_extended); i++) {
		usb_strings_extended[i] = usb_strings[i];
	}
#if defined(ENVIRONMENT_dfu)
	usb_strings_extended[_STR_FIRST_ALT + 1] = usb_string_notavailable;
#elif defined(ENVIRONMENT_flash)
	usb_strings_extended[_STR_FIRST_ALT + 2] = usb_string_notavailable;
#endif
	usb_strings_extended[STR_SERIAL] = usb_string_serial;
	usb_strings_extended[VERSION_CONF_STR] = usb_string_version_conf;
	usb_strings_extended[VERSION_STR] = usb_string_version;
}

/* USB descriptor just to show the version */
typedef struct _SIMTraceDriverConfigurationDescriptorVersion {
	/** Standard configuration descriptor. */
	USBConfigurationDescriptor configuration;
	USBInterfaceDescriptor version;
} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorVersion;

static const SIMTraceDriverConfigurationDescriptorVersion
				configurationDescriptorVersion = {
	/* Standard configuration descriptor for the interface descriptor*/
	.configuration = {
		.bLength 		= sizeof(USBConfigurationDescriptor),
		.bDescriptorType	= USBGenericDescriptor_CONFIGURATION,
		.wTotalLength		= sizeof(SIMTraceDriverConfigurationDescriptorVersion),
		.bNumInterfaces		= 1,
		.bConfigurationValue	= 2,
		.iConfiguration		= VERSION_CONF_STR,
		.bmAttributes		= USBD_BMATTRIBUTES,
		.bMaxPower		= USBConfigurationDescriptor_POWER(100),
	},
	/* Interface standard descriptor just holding the version information */
	.version = {
		.bLength = sizeof(USBInterfaceDescriptor),
		.bDescriptorType 	= USBGenericDescriptor_INTERFACE,
		.bInterfaceNumber	= 0,
		.bAlternateSetting	= 0,
		.bNumEndpoints		= 0,
		.bInterfaceClass	= USB_CLASS_PROPRIETARY,
		.bInterfaceSubClass	= 0xff,
		.bInterfaceProtocol	= 0,
		.iInterface		= VERSION_STR,
	},
};

static const USBConfigurationDescriptor *conf_desc_arr[] = {
	&dfu_cfg_descriptor.ucfg,
	&configurationDescriptorVersion.configuration,
};

const USBDDriverDescriptors dfu_descriptors = {
	.pFsDevice = &fsDevice,
	.pFsConfiguration = (const USBConfigurationDescriptor **)&conf_desc_arr,
	/* DFU only supports FS for now */
	.pFsQualifier = NULL,
	.pFsOtherSpeed = NULL,
	.pHsDevice = NULL,
	.pHsConfiguration = NULL,
	.pHsQualifier = NULL,
	.pHsOtherSpeed = NULL,
	.pStrings = usb_strings_extended,
	.numStrings = ARRAY_SIZE(usb_strings_extended),
};