-
Notifications
You must be signed in to change notification settings - Fork 270
/
dfu.desc.c
258 lines (224 loc) · 6.45 KB
/
dfu.desc.c
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
// Originally Generated from MCHCK Toolkit
/* Copyright (c) Jacob Alexander 2014-2020 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Local Includes -----
#include "dfu.desc.h"
// ----- Generated Includes -----
#include <buildvars.h>
// ----- Macros -----
#if defined(_sam_)
#undef LSB
#undef MSB
#endif
#define LSB(n) ((n) & 255)
#define MSB(n) (((n) >> 8) & 255)
// ----- Structs -----
static const struct usb_config_1 usb_config_1 = {
.config = {
.bLength = sizeof(struct usb_desc_config_t),
.bDescriptorType = USB_DESC_CONFIG,
.wTotalLength = sizeof(struct usb_config_1),
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 5,
.one = 1,
.bMaxPower = 50
},
#if !defined(_sam_)
.usb_function_0 = {
.iface = {
.bLength = sizeof(struct usb_desc_iface_t),
.bDescriptorType = USB_DESC_IFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 0,
.bInterfaceClass = USB_DEV_CLASS_APP,
.bInterfaceSubClass = USB_DEV_SUBCLASS_APP_DFU,
.bInterfaceProtocol = USB_DEV_PROTO_DFU_DFU,
.iInterface = 4
},
.dfu = {
.bLength = sizeof(struct dfu_desc_functional),
.bDescriptorType = {
.id = 0x1,
.type_type = USB_DESC_TYPE_CLASS
},
.will_detach = 1,
.manifestation_tolerant = 0,
.can_upload = 0,
.can_upload = 1,
.can_download = 1,
.wDetachTimeOut = 0,
.wTransferSize = USB_DFU_TRANSFER_SIZE,
.bcdDFUVersion = { .maj = 1, .min = 1 }
}
},
#endif
};
static const struct usbd_config usbd_config_1 = {
.init = init_usb_bootloader,
.suspend = NULL,
.resume = NULL,
.desc = &usb_config_1.config,
.function = {&dfu_function},
};
static const struct usb_desc_dev_t dfu_device_dev_desc = {
.bLength = sizeof(struct usb_desc_dev_t),
.bDescriptorType = USB_DESC_DEV,
.bcdUSB = { .maj = 2 },
.bDeviceClass = USB_DEV_CLASS_SEE_IFACE,
.bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE,
.bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE,
.bMaxPacketSize0 = EP0_BUFSIZE,
.idVendor = VENDOR_ID,
.idProduct = PRODUCT_ID,
.bcdDevice = { .maj = MSB( BCD_VERSION ), .min = LSB( BCD_VERSION ) },
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};
// Secondary USB descriptor when alt device detected
static const struct usb_desc_dev_t alt_dfu_device_dev_desc = {
.bLength = sizeof(struct usb_desc_dev_t),
.bDescriptorType = USB_DESC_DEV,
.bcdUSB = { .maj = 2 },
.bDeviceClass = USB_DEV_CLASS_SEE_IFACE,
.bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE,
.bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE,
.bMaxPacketSize0 = EP0_BUFSIZE,
.idVendor = VENDOR_ID,
.idProduct = ALT_PRODUCT_ID,
.bcdDevice = { .maj = MSB( BCD_VERSION ), .min = LSB( BCD_VERSION ) },
.iManufacturer = 1,
.iProduct = 6,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};
// Enables Microsoft specefic setup requests with bmRequestType set to bMS_VendorCode
// LanguageID must be 0, not english (0x0409)
#if defined(_sam_)
static struct usb_desc_string_t msft_os_str_desc = {
.bLength = 18,
.bDescriptorType = USB_DESC_STRING,
.bString = {
'M', 'S', 'F', 'T', '1', '0', '0', // qwSignature
MS_VENDOR_CODE, // bMS_VendorCode & bPad
}
};
#endif
// Microsoft Compatible ID Feature Descriptor ---
// Requests the given driver if available
struct usb_desc_msft_compat_t msft_extended_compat_desc = {
.dwLength = sizeof(struct usb_desc_msft_compat_t),
.bcdVersion = 0x01,
.wIndex = USB_CTRL_REQ_MSFT_COMPAT_ID,
.bCount = 1,
.bFirstInterfaceNumber = 0,
.compatibleID = STR_WCID_DRIVER
};
struct usb_desc_string_t * const dfu_device_str_desc[] = {
USB_DESC_STRING_LANG_ENUS,
USB_DESC_STRING(STR_MANUFACTURER),
USB_DESC_STRING(STR_PRODUCT),
#if DFU_EXTRA_BLE_SWD_SUPPORT == 1
USB_DESC_STRING(STR_SERIAL_BLE),
#else
USB_DESC_STRING(STR_SERIAL),
#endif
USB_DESC_STRING(STR_ALTNAME),
USB_DESC_STRING(STR_CONFIG_NAME),
USB_DESC_STRING(STR_ALT_PRODUCT),
#if DFU_EXTRA_BLE_SWD_SUPPORT == 1
USB_DESC_STRING(STR_ALTNAME2),
#endif
NULL
};
const struct usbd_device dfu_device = {
.dev_desc = &dfu_device_dev_desc,
.string_descs = dfu_device_str_desc,
.configs = {
&usbd_config_1,
NULL
}
};
const struct usbd_device alt_dfu_device = {
.dev_desc = &alt_dfu_device_dev_desc,
.string_descs = dfu_device_str_desc,
.configs = {
&usbd_config_1,
NULL
}
};
// Initialize DFU USB descriptor
void dfu_usb_init(bool alt_device)
{
if (alt_device) {
// Set alternate device descriptor
usb_init(&alt_dfu_device);
} else {
// Set default device descriptor
usb_init(&dfu_device);
}
}
// Poll USB for changes in DFU status
void dfu_usb_poll()
{
usb_poll();
}
#if defined(_sam_)
#include <common/services/usb/usb_protocol.h>
#include <common/services/usb/udc/udd.h>
/*! \brief Example of extra USB string management
* This feature is available for single or composite device
* which want implement additional USB string than
* Manufacture, Product and serial number ID.
*
* return true, if the string ID requested is know and managed by this functions
*/
bool main_extra_string()
{
struct usb_desc_string_t *desc = NULL;
uint8_t inreq = udd_g_ctrlreq.req.wValue & 0xff;
// Microsoft looks for a special string at index 0xEE
// If found it will be compared against known OS compatabilily strings
// Once matched additional Microsoft-specific setup requests may be sent
if (inreq == 0xEE) {
desc = &msft_os_str_desc;
goto send_str_desc;
}
// Lookup request and send descriptor
for ( uint8_t req = 0; dfu_device_str_desc[req] != NULL; req++ )
{
// Request matches
if ( inreq == req )
{
desc = dfu_device_str_desc[req];
goto send_str_desc;
}
}
// No string found
return false;
send_str_desc:
udd_g_ctrlreq.payload = (uint8_t *)desc;
udd_g_ctrlreq.payload_size = desc->bLength;
// if the string is larger than request length, then cut it
if (udd_g_ctrlreq.payload_size > udd_g_ctrlreq.req.wLength) {
udd_g_ctrlreq.payload_size = udd_g_ctrlreq.req.wLength;
}
return true;
}
#endif