summaryrefslogtreecommitdiffstats
path: root/plat/st/common/usb_dfu.c
blob: 8bb099425eaee1d87c32bfd911f1cc1c130d4477 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>
#include <string.h>

#include <common/debug.h>

#include <platform_def.h>
#include <usb_dfu.h>

/* Device states as defined in DFU spec */
#define STATE_APP_IDLE			0
#define STATE_APP_DETACH		1
#define STATE_DFU_IDLE			2
#define STATE_DFU_DNLOAD_SYNC		3
#define STATE_DFU_DNLOAD_BUSY		4
#define STATE_DFU_DNLOAD_IDLE		5
#define STATE_DFU_MANIFEST_SYNC		6
#define STATE_DFU_MANIFEST		7
#define STATE_DFU_MANIFEST_WAIT_RESET	8
#define STATE_DFU_UPLOAD_IDLE		9
#define STATE_DFU_ERROR			10

/* DFU errors */
#define DFU_ERROR_NONE			0x00
#define DFU_ERROR_TARGET		0x01
#define DFU_ERROR_FILE			0x02
#define DFU_ERROR_WRITE			0x03
#define DFU_ERROR_ERASE			0x04
#define DFU_ERROR_CHECK_ERASED		0x05
#define DFU_ERROR_PROG			0x06
#define DFU_ERROR_VERIFY		0x07
#define DFU_ERROR_ADDRESS		0x08
#define DFU_ERROR_NOTDONE		0x09
#define DFU_ERROR_FIRMWARE		0x0A
#define DFU_ERROR_VENDOR		0x0B
#define DFU_ERROR_USB			0x0C
#define DFU_ERROR_POR			0x0D
#define DFU_ERROR_UNKNOWN		0x0E
#define DFU_ERROR_STALLEDPKT		0x0F

/* DFU request */
#define DFU_DETACH			0
#define DFU_DNLOAD			1
#define DFU_UPLOAD			2
#define DFU_GETSTATUS			3
#define DFU_CLRSTATUS			4
#define DFU_GETSTATE			5
#define DFU_ABORT			6

static bool usb_dfu_detach_req;

/*
 * usb_dfu_init
 *         Initialize the DFU interface
 * pdev: device instance
 * cfgidx: Configuration index
 * return: status
 */
static uint8_t usb_dfu_init(struct usb_handle *pdev, uint8_t cfgidx)
{
	(void)pdev;
	(void)cfgidx;

	/* Nothing to do in this stage */
	return USBD_OK;
}

/*
 * usb_dfu_de_init
 *         De-Initialize the DFU layer
 * pdev: device instance
 * cfgidx: Configuration index
 * return: status
 */
static uint8_t usb_dfu_de_init(struct usb_handle *pdev, uint8_t cfgidx)
{
	(void)pdev;
	(void)cfgidx;

	/* Nothing to do in this stage */
	return USBD_OK;
}

/*
 * usb_dfu_data_in
 *         handle data IN Stage
 * pdev: device instance
 * epnum: endpoint index
 * return: status
 */
static uint8_t usb_dfu_data_in(struct usb_handle *pdev, uint8_t epnum)
{
	(void)pdev;
	(void)epnum;

	return USBD_OK;
}

/*
 * usb_dfu_ep0_rx_ready
 *         handle EP0 Rx Ready event
 * pdev: device
 * return: status
 */
static uint8_t usb_dfu_ep0_rx_ready(struct usb_handle *pdev)
{
	(void)pdev;

	return USBD_OK;
}

/*
 * usb_dfu_ep0_tx_ready
 *         handle EP0 TRx Ready event
 * pdev: device instance
 * return: status
 */
static uint8_t usb_dfu_ep0_tx_ready(struct usb_handle *pdev)
{
	(void)pdev;

	return USBD_OK;
}

/*
 * usb_dfu_sof
 *         handle SOF event
 * pdev: device instance
 * return: status
 */
static uint8_t usb_dfu_sof(struct usb_handle *pdev)
{
	(void)pdev;

	return USBD_OK;
}

/*
 * usb_dfu_iso_in_incomplete
 *         handle data ISO IN Incomplete event
 * pdev: device instance
 * epnum: endpoint index
 * return: status
 */
static uint8_t usb_dfu_iso_in_incomplete(struct usb_handle *pdev, uint8_t epnum)
{
	(void)pdev;
	(void)epnum;

	return USBD_OK;
}

/*
 * usb_dfu_iso_out_incomplete
 *         handle data ISO OUT Incomplete event
 * pdev: device instance
 * epnum: endpoint index
 * return: status
 */
static uint8_t usb_dfu_iso_out_incomplete(struct usb_handle *pdev,
					  uint8_t epnum)
{
	(void)pdev;
	(void)epnum;

	return USBD_OK;
}

/*
 * usb_dfu_data_out
 *         handle data OUT Stage
 * pdev: device instance
 * epnum: endpoint index
 * return: status
 */
static uint8_t usb_dfu_data_out(struct usb_handle *pdev, uint8_t epnum)
{
	(void)pdev;
	(void)epnum;

	return USBD_OK;
}

/*
 * usb_dfu_detach
 *         Handles the DFU DETACH request.
 * pdev: device instance
 * req: pointer to the request structure.
 */
static void usb_dfu_detach(struct usb_handle *pdev, struct usb_setup_req *req)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	INFO("Receive DFU Detach\n");

	if ((hdfu->dev_state == STATE_DFU_IDLE) ||
	    (hdfu->dev_state == STATE_DFU_DNLOAD_SYNC) ||
	    (hdfu->dev_state == STATE_DFU_DNLOAD_IDLE) ||
	    (hdfu->dev_state == STATE_DFU_MANIFEST_SYNC) ||
	    (hdfu->dev_state == STATE_DFU_UPLOAD_IDLE)) {
		/* Update the state machine */
		hdfu->dev_state = STATE_DFU_IDLE;
		hdfu->dev_status = DFU_ERROR_NONE;
	}

	usb_dfu_detach_req = true;
}

/*
 * usb_dfu_download
 *         Handles the DFU DNLOAD request.
 * pdev: device instance
 * req: pointer to the request structure
 */
static void usb_dfu_download(struct usb_handle *pdev, struct usb_setup_req *req)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
	uintptr_t data_ptr;
	uint32_t length;
	int ret;

	/* Data setup request */
	if (req->length > 0) {
		/* Unsupported state */
		if ((hdfu->dev_state != STATE_DFU_IDLE) &&
		    (hdfu->dev_state != STATE_DFU_DNLOAD_IDLE)) {
			/* Call the error management function (command will be nacked) */
			usb_core_ctl_error(pdev);
			return;
		}

		/* Get the data address */
		length = req->length;
		ret = hdfu->callback->download(hdfu->alt_setting, &data_ptr,
					       &length, pdev->user_data);
		if (ret == 0U) {
			/* Update the state machine */
			hdfu->dev_state = STATE_DFU_DNLOAD_SYNC;
			/* Start the transfer */
			usb_core_receive_ep0(pdev, (uint8_t *)data_ptr, length);
		} else {
			usb_core_ctl_error(pdev);
		}
	} else {
		/* End of DNLOAD operation*/
		if (hdfu->dev_state != STATE_DFU_DNLOAD_IDLE) {
			/* Call the error management function (command will be nacked) */
			usb_core_ctl_error(pdev);
			return;
		}
		/* End of DNLOAD operation*/
		hdfu->dev_state = STATE_DFU_MANIFEST_SYNC;
		ret = hdfu->callback->manifestation(hdfu->alt_setting, pdev->user_data);
		if (ret == 0U) {
			hdfu->dev_state = STATE_DFU_MANIFEST_SYNC;
		} else {
			usb_core_ctl_error(pdev);
		}
	}
}

/*
 * usb_dfu_upload
 *         Handles the DFU UPLOAD request.
 * pdev: instance
 * req: pointer to the request structure
 */
static void usb_dfu_upload(struct usb_handle *pdev, struct usb_setup_req *req)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;
	uintptr_t data_ptr;
	uint32_t length;
	int ret;

	/* Data setup request */
	if (req->length == 0) {
		/* No Data setup request */
		hdfu->dev_state = STATE_DFU_IDLE;
		return;
	}

	/* Unsupported state */
	if ((hdfu->dev_state != STATE_DFU_IDLE) && (hdfu->dev_state != STATE_DFU_UPLOAD_IDLE)) {
		ERROR("UPLOAD : Unsupported State\n");
		/* Call the error management function (command will be nacked) */
		usb_core_ctl_error(pdev);
		return;
	}

	/* Update the data address */
	length = req->length;
	ret = hdfu->callback->upload(hdfu->alt_setting, &data_ptr, &length, pdev->user_data);
	if (ret == 0U) {
		/* Short frame */
		hdfu->dev_state = (req->length > length) ? STATE_DFU_IDLE : STATE_DFU_UPLOAD_IDLE;

		/* Start the transfer */
		usb_core_transmit_ep0(pdev, (uint8_t *)data_ptr, length);
	} else {
		ERROR("UPLOAD : bad block %i on alt %i\n", req->value, req->index);
		hdfu->dev_state = STATE_DFU_ERROR;
		hdfu->dev_status = DFU_ERROR_STALLEDPKT;

		/* Call the error management function (command will be nacked) */
		usb_core_ctl_error(pdev);
	}
}

/*
 * usb_dfu_get_status
 *         Handles the DFU GETSTATUS request.
 * pdev: instance
 */
static void usb_dfu_get_status(struct usb_handle *pdev)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	hdfu->status[0] = hdfu->dev_status;	/* bStatus */
	hdfu->status[1] = 0;			/* bwPollTimeout[3] */
	hdfu->status[2] = 0;
	hdfu->status[3] = 0;
	hdfu->status[4] = hdfu->dev_state;	/* bState */
	hdfu->status[5] = 0;			/* iString */

	/* next step */
	switch (hdfu->dev_state) {
	case STATE_DFU_DNLOAD_SYNC:
		hdfu->dev_state = STATE_DFU_DNLOAD_IDLE;
		break;
	case STATE_DFU_MANIFEST_SYNC:
		/* the device is 'ManifestationTolerant' */
		hdfu->status[4] = STATE_DFU_MANIFEST;
		hdfu->status[1] = 1U; /* bwPollTimeout = 1ms */
		hdfu->dev_state = STATE_DFU_IDLE;
		break;

	default:
		break;
	}

	/* Start the transfer */
	usb_core_transmit_ep0(pdev, (uint8_t *)&hdfu->status[0], sizeof(hdfu->status));
}

/*
 * usb_dfu_clear_status
 *         Handles the DFU CLRSTATUS request.
 * pdev: device instance
 */
static void usb_dfu_clear_status(struct usb_handle *pdev)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	if (hdfu->dev_state == STATE_DFU_ERROR) {
		hdfu->dev_state = STATE_DFU_IDLE;
		hdfu->dev_status = DFU_ERROR_NONE;
	} else {
		/* State Error */
		hdfu->dev_state = STATE_DFU_ERROR;
		hdfu->dev_status = DFU_ERROR_UNKNOWN;
	}
}

/*
 * usb_dfu_get_state
 *         Handles the DFU GETSTATE request.
 * pdev: device instance
 */
static void usb_dfu_get_state(struct usb_handle *pdev)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	/* Return the current state of the DFU interface */
	usb_core_transmit_ep0(pdev, &hdfu->dev_state, 1);
}

/*
 * usb_dfu_abort
 *         Handles the DFU ABORT request.
 * pdev: device instance
 */
static void usb_dfu_abort(struct usb_handle *pdev)
{
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	if ((hdfu->dev_state == STATE_DFU_IDLE) ||
	    (hdfu->dev_state == STATE_DFU_DNLOAD_SYNC) ||
	    (hdfu->dev_state == STATE_DFU_DNLOAD_IDLE) ||
	    (hdfu->dev_state == STATE_DFU_MANIFEST_SYNC) ||
	    (hdfu->dev_state == STATE_DFU_UPLOAD_IDLE)) {
		hdfu->dev_state = STATE_DFU_IDLE;
		hdfu->dev_status = DFU_ERROR_NONE;
	}
}

/*
 * usb_dfu_setup
 *         Handle the DFU specific requests
 * pdev: instance
 * req: usb requests
 * return: status
 */
static uint8_t usb_dfu_setup(struct usb_handle *pdev, struct usb_setup_req *req)
{
	uint8_t *pbuf = NULL;
	uint16_t len = 0U;
	uint8_t ret = USBD_OK;
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	switch (req->bm_request & USB_REQ_TYPE_MASK) {
	case USB_REQ_TYPE_CLASS:
		switch (req->b_request) {
		case DFU_DNLOAD:
			usb_dfu_download(pdev, req);
			break;

		case DFU_UPLOAD:
			usb_dfu_upload(pdev, req);
			break;

		case DFU_GETSTATUS:
			usb_dfu_get_status(pdev);
			break;

		case DFU_CLRSTATUS:
			usb_dfu_clear_status(pdev);
			break;

		case DFU_GETSTATE:
			usb_dfu_get_state(pdev);
			break;

		case DFU_ABORT:
			usb_dfu_abort(pdev);
			break;

		case DFU_DETACH:
			usb_dfu_detach(pdev, req);
			break;

		default:
			ERROR("unknown request %x on alternate %i\n",
			      req->b_request, hdfu->alt_setting);
			usb_core_ctl_error(pdev);
			ret = USBD_FAIL;
			break;
		}
		break;
	case USB_REQ_TYPE_STANDARD:
		switch (req->b_request) {
		case USB_REQ_GET_DESCRIPTOR:
			if (HIBYTE(req->value) == DFU_DESCRIPTOR_TYPE) {
				pbuf = pdev->desc->get_config_desc(&len);
				/* DFU descriptor at the end of the USB */
				pbuf += len - 9U;
				len = 9U;
				len = MIN(len, req->length);
			}

			/* Start the transfer */
			usb_core_transmit_ep0(pdev, pbuf, len);

			break;

		case USB_REQ_GET_INTERFACE:
			/* Start the transfer */
			usb_core_transmit_ep0(pdev, (uint8_t *)&hdfu->alt_setting, 1U);
			break;

		case USB_REQ_SET_INTERFACE:
			hdfu->alt_setting = LOBYTE(req->value);
			break;

		default:
			usb_core_ctl_error(pdev);
			ret = USBD_FAIL;
			break;
		}
	default:
		break;
	}

	return ret;
}

static const struct usb_class usb_dfu = {
	.init = usb_dfu_init,
	.de_init = usb_dfu_de_init,
	.setup = usb_dfu_setup,
	.ep0_tx_sent = usb_dfu_ep0_tx_ready,
	.ep0_rx_ready = usb_dfu_ep0_rx_ready,
	.data_in = usb_dfu_data_in,
	.data_out = usb_dfu_data_out,
	.sof = usb_dfu_sof,
	.iso_in_incomplete = usb_dfu_iso_in_incomplete,
	.iso_out_incomplete = usb_dfu_iso_out_incomplete,
};

void usb_dfu_register(struct usb_handle *pdev, struct usb_dfu_handle *phandle)
{
	pdev->class = (struct usb_class *)&usb_dfu;
	pdev->class_data = phandle;

	phandle->dev_state = STATE_DFU_IDLE;
	phandle->dev_status = DFU_ERROR_NONE;
}

int usb_dfu_loop(struct usb_handle *pdev, const struct usb_dfu_media *pmedia)
{
	uint32_t it_count;
	enum usb_status ret;
	struct usb_dfu_handle *hdfu = (struct usb_dfu_handle *)pdev->class_data;

	hdfu->callback = pmedia;
	usb_dfu_detach_req = false;
	/* Continue to handle USB core IT to assure complete data transmission */
	it_count = 100U;

	/* DFU infinite loop until DETACH_REQ */
	while (it_count != 0U) {
		ret = usb_core_handle_it(pdev);
		if (ret != USBD_OK) {
			return -EIO;
		}

		/* Detach request received */
		if (usb_dfu_detach_req) {
			it_count--;
		}
	}

	return 0;
}