summaryrefslogtreecommitdiff
path: root/usb/device/dfu/dfu_driver.c
blob: c9a9f035cec79cc19ddfa22670cf2df795d11387 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* USB Device Firmware Update Implementation for OpenPCD
 * (C) 2006-2011 by Harald Welte <hwelte@hmw-consulting.de>
 *
 * This ought to be compliant to the USB DFU Spec 1.0 as available from
 * http://www.usb.org/developers/devclass_docs/usbdfu10.pdf
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <unistd.h>

#include <board.h>
#include <core_cm3.h>

#include <utility/trace.h>

#include <usb/common/core/USBInterfaceDescriptor.h>
#include <usb/common/core/USBGenericDescriptor.h>
#include <usb/common/core/USBGetDescriptorRequest.h>
#include <usb/device/core/USBD.h>
#include <usb/common/dfu/usb_dfu.h>
#include <usb/device/dfu/dfu.h>

/* FIXME */
#define __dfudata
#define __dfufunc

/// Standard device driver instance.
static USBDDriver usbdDriver;
static unsigned char if_altsettings[1];

__dfudata struct dfudata g_dfu = {
  	.state = DFU_STATE_appIDLE,
	.past_manifest = 0,
	.total_bytes = 0,
};

static __dfufunc void handle_getstatus(void)
{
	/* has to be static as USBD_Write is async ? */
	static struct dfu_status dstat;

	dfu_drv_updstatus();

	/* send status response */
	dstat.bStatus = g_dfu.status;
	dstat.bState = g_dfu.state;
	dstat.iString = 0;
	/* FIXME: set dstat.bwPollTimeout */

	TRACE_DEBUG("handle_getstatus(%u, %u)\n\r", dstat.bStatus, dstat.bState);

	USBD_Write(0, (char *)&dstat, sizeof(dstat), NULL, 0);
}

static void __dfufunc handle_getstate(void)
{
	uint8_t u8 = g_dfu.state;

	TRACE_DEBUG("handle_getstate(%u)\n\r", g_dfu.state);

	USBD_Write(0, (char *)&u8, sizeof(u8), NULL, 0);
}

static void TerminateCtrlInWithNull(void *pArg,
                                    unsigned char status,
                                    unsigned int transferred,
                                    unsigned int remaining)
{
    USBD_Write(0, // Endpoint #0
               0, // No data buffer
               0, // No data buffer
               (TransferCallback) 0,
               (void *)  0);
}

static uint8_t dfu_buf[BOARD_DFU_PAGE_SIZE];

/* download of a single page has completed */
static void dnload_cb(void *arg, unsigned char status, unsigned int transferred,
		      unsigned int remaining)
{
	int rc;

	TRACE_DEBUG("COMPLETE\n\r");

	if (status != USBD_STATUS_SUCCESS) {
		TRACE_ERROR("USBD download callback status %d\n\r", status);
		USBD_Stall(0);
		return;
	}

	rc = USBDFU_handle_dnload(if_altsettings[0], g_dfu.total_bytes, dfu_buf, transferred);
	switch (rc) {
	case DFU_RET_ZLP:
		g_dfu.total_bytes += transferred;
		g_dfu.state = DFU_STATE_dfuDNLOAD_IDLE;
		TerminateCtrlInWithNull(0,0,0,0);
		break;
	case DFU_RET_STALL:
		g_dfu.state = DFU_STATE_dfuERROR;
		USBD_Stall(0);
		break;
	case DFU_RET_NOTHING:
		break;
	}

}

static int handle_dnload(uint16_t val, uint16_t len, int first)
{
	int rc;

	if (len > BOARD_DFU_PAGE_SIZE) {
		TRACE_ERROR("DFU length exceeds flash page size\n\r");
		g_dfu.state = DFU_STATE_dfuERROR;
		g_dfu.status = DFU_STATUS_errADDRESS;
		return DFU_RET_STALL;
	}

	if (len & 0x03) {
		TRACE_ERROR("DFU length not four-byte-aligned\n\r");
		g_dfu.state = DFU_STATE_dfuERROR;
		g_dfu.status = DFU_STATUS_errADDRESS;
		return DFU_RET_STALL;
	}

	if (first)
		g_dfu.total_bytes = 0;

	if (len == 0) {
		TRACE_DEBUG("zero-size write -> MANIFEST_SYNC\n\r");
		g_dfu.state = DFU_STATE_dfuMANIFEST_SYNC;
		return DFU_RET_ZLP;
	}

	/* else: actually read data */
	rc = USBD_Read(0, dfu_buf, len, &dnload_cb, 0);
	if (rc == USBD_STATUS_SUCCESS)
		return DFU_RET_NOTHING;
	else
		return DFU_RET_STALL;
}

/* upload of a single page has completed */
static void upload_cb(void *arg, unsigned char status, unsigned int transferred,
		      unsigned int remaining)
{
	int rc;

	TRACE_DEBUG("COMPLETE\n\r");

	if (status != USBD_STATUS_SUCCESS) {
		TRACE_ERROR("USBD upload callback status %d\n\r", status);
		USBD_Stall(0);
		return;
	}

	g_dfu.total_bytes += transferred;
}

static int handle_upload(uint16_t val, uint16_t len, int first)
{
	int rc;

	if (first)
		g_dfu.total_bytes = 0;

	if (len > BOARD_DFU_PAGE_SIZE) {
		TRACE_ERROR("DFU length exceeds flash page size\n\r");
		g_dfu.state = DFU_STATE_dfuERROR;
		g_dfu.status = DFU_STATUS_errADDRESS;
		return DFU_RET_STALL;
	}

	rc = USBDFU_handle_upload(if_altsettings[0], g_dfu.total_bytes, dfu_buf, len);
	if (rc < 0) {
		TRACE_ERROR("application handle_upload() returned %d\n\r", rc);
		return DFU_RET_STALL;
	}

	if (USBD_Write(0, dfu_buf, rc, &upload_cb, 0) == USBD_STATUS_SUCCESS)
		return rc;

	return DFU_RET_STALL;
}

/* this function gets daisy-chained into processing EP0 requests */
void USBDFU_DFU_RequestHandler(const USBGenericRequest *request)
{
	uint8_t req = USBGenericRequest_GetRequest(request);
	uint16_t len = USBGenericRequest_GetLength(request);
	uint16_t val = USBGenericRequest_GetValue(request);
	int rc, ret;

	TRACE_DEBUG("type=0x%x, recipient=0x%x val=0x%x len=%u\n\r",
			USBGenericRequest_GetType(request),
			USBGenericRequest_GetRecipient(request),
			val, len);

	/* check for GET_DESCRIPTOR on DFU */
	if (USBGenericRequest_GetType(request) == USBGenericRequest_STANDARD &&
	    USBGenericRequest_GetRecipient(request) == USBGenericRequest_DEVICE &&
	    USBGenericRequest_GetRequest(request) == USBGenericRequest_GETDESCRIPTOR &&
	    USBGetDescriptorRequest_GetDescriptorType(request) == USB_DT_DFU) {
		uint16_t length = sizeof(struct usb_dfu_func_descriptor);
		const USBDeviceDescriptor *pDevice;
		int terminateWithNull;

		if (USBD_IsHighSpeed())
			pDevice = usbdDriver.pDescriptors->pHsDevice;
		else
			pDevice = usbdDriver.pDescriptors->pFsDevice;

		terminateWithNull = ((length % pDevice->bMaxPacketSize0) == 0);
		USBD_Write(0, &dfu_cfg_descriptor.func_dfu, length,
			   terminateWithNull ? TerminateCtrlInWithNull : 0, 0);
		return;
	}

	/* forward all non-DFU specific messages to core handler*/
	if (USBGenericRequest_GetType(request) != USBGenericRequest_CLASS ||
	    USBGenericRequest_GetRecipient(request) != USBGenericRequest_INTERFACE) {
		TRACE_DEBUG("std_ho_usbd ");
		USBDDriver_RequestHandler(&usbdDriver, request);
	}

	switch (g_dfu.state) {
	case DFU_STATE_appIDLE:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		case USB_REQ_DFU_DETACH:
			TRACE_DEBUG("\r\n====dfu_detach\n\r");
			g_dfu.state = DFU_STATE_appDETACH;
			ret = DFU_RET_ZLP;
			goto out;
			break;
		default:
			ret = DFU_RET_STALL;
		}
		break;
	case DFU_STATE_appDETACH:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_appIDLE;
			ret = DFU_RET_STALL;
			goto out;
			break;
		}
		/* FIXME: implement timer to return to appIDLE */
		break;
	case DFU_STATE_dfuIDLE:
		switch (req) {
		case USB_REQ_DFU_DNLOAD:
			if (len == 0) {
				g_dfu.state = DFU_STATE_dfuERROR;
				ret = DFU_RET_STALL;
				goto out;
			}
			g_dfu.state = DFU_STATE_dfuDNLOAD_SYNC;
			ret = handle_dnload(val, len, 1);
			break;
		case USB_REQ_DFU_UPLOAD:
			g_dfu.state = DFU_STATE_dfuUPLOAD_IDLE;
			handle_upload(val, len, 1);
			break;
		case USB_REQ_DFU_ABORT:
			/* no zlp? */
			ret = DFU_RET_ZLP;
			break;
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			goto out;
			break;
		}
		break;
	case DFU_STATE_dfuDNLOAD_SYNC:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			/* FIXME: state transition depending on block completeness */
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			goto out;
		}
		break;
	case DFU_STATE_dfuDNBUSY:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			/* FIXME: only accept getstatus if bwPollTimeout
			 * has elapsed */
			handle_getstatus();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			goto out;
		}
		break;
	case DFU_STATE_dfuDNLOAD_IDLE:
		switch (req) {
		case USB_REQ_DFU_DNLOAD:
			g_dfu.state = DFU_STATE_dfuDNLOAD_SYNC;
			ret = handle_dnload(val, len, 0);
			break;
		case USB_REQ_DFU_ABORT:
			g_dfu.state = DFU_STATE_dfuIDLE;
			ret = DFU_RET_ZLP;
			break;
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			break;
		}
		break;
	case DFU_STATE_dfuMANIFEST_SYNC:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			break;
		}
		break;
	case DFU_STATE_dfuMANIFEST:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			/* we don't want to change to WAIT_RST, as it
			 * would mean that we can not support another
			 * DFU transaction before doing the actual
			 * reset.  Instead, we switch to idle and note
			 * that we've already been through MANIFST in
			 * the global variable 'past_manifest'.
			 */
			//g_dfu.state = DFU_STATE_dfuMANIFEST_WAIT_RST;
			g_dfu.state = DFU_STATE_dfuIDLE;
			g_dfu.past_manifest = 1;
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			break;
		}
		break;
	case DFU_STATE_dfuMANIFEST_WAIT_RST:
		/* we should never go here */
		break;
	case DFU_STATE_dfuUPLOAD_IDLE:
		switch (req) {
		case USB_REQ_DFU_UPLOAD:
			/* state transition if less data then requested */
			rc = handle_upload(val, len, 0);
			if (rc >= 0 && rc < len)
				g_dfu.state = DFU_STATE_dfuIDLE;
			break;
		case USB_REQ_DFU_ABORT:
			g_dfu.state = DFU_STATE_dfuIDLE;
			/* no zlp? */
			ret = DFU_RET_ZLP;
			break;
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			break;
		}
		break;
	case DFU_STATE_dfuERROR:
		switch (req) {
		case USB_REQ_DFU_GETSTATUS:
			handle_getstatus();
			break;
		case USB_REQ_DFU_GETSTATE:
			handle_getstate();
			break;
		case USB_REQ_DFU_CLRSTATUS:
			g_dfu.state = DFU_STATE_dfuIDLE;
			g_dfu.status = DFU_STATUS_OK;
			/* no zlp? */
			ret = DFU_RET_ZLP;
			break;
		default:
			g_dfu.state = DFU_STATE_dfuERROR;
			ret = DFU_RET_STALL;
			break;
		}
		break;
	}

out:
	switch (ret) {
	case DFU_RET_NOTHING:
		break;
	case DFU_RET_ZLP:
		USBD_Write(0, 0, 0, 0, 0);
		break;
	case DFU_RET_STALL:
		USBD_Stall(0);
		break;
	}
}

void USBDFU_Initialize(const USBDDriverDescriptors *pDescriptors)
{
	/* We already start in DFU idle mode */
	g_dfu.state = DFU_STATE_dfuIDLE;

	USBDDriver_Initialize(&usbdDriver, pDescriptors, if_altsettings);

	USBD_Init();
}

void USBDFU_SwitchToApp(void)
{
	/* make sure the MAGIC is not set to enter DFU again */
	*(unsigned int *)USB_DFU_MAGIC_ADDR = 0;

	/* disconnect from USB to ensure re-enumeration */
	USBD_Disconnect();

	/* disable any interrupts during transition */
	__disable_irq();

	/* Tell the hybrid to execute FTL JUMP! */
	//BootIntoApp();
	RSTC_ProcessorReset();
}
personal git repositories of Harald Welte. Your mileage may vary