aboutsummaryrefslogtreecommitdiff
path: root/upgrade/dfu.c
blob: 2ab16ed0b79ea8ef9cf1067108b176baf4a24e56 (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
/*
 * This file is part of the Black Magic Debug project.
 *
 * Copyright (C) 2011  Black Sphere Technologies Ltd.
 * Written by Gareth McMullin <gareth@blacksphere.co.nz>
 *
 * 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/>.
 */

#include <usb.h>

#include "dfu.h"

/* DFU Requests: Refer to Table 3.2 */
#define DFU_DETACH	0x00
#define DFU_DNLOAD	0x01
#define DFU_UPLOAD	0x02
#define DFU_GETSTATUS	0x03
#define DFU_CLRSTATUS	0x04
#define DFU_GETSTATE	0x05
#define DFU_ABORT	0x06

#define USB_DEFAULT_TIMEOUT 1000
#define DFU_DETACH_TIMEOUT 1000

int dfu_detach(usb_dev_handle *dev, uint16_t iface, uint16_t wTimeout)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_DETACH, wTimeout, iface, NULL, 0, 
			USB_DEFAULT_TIMEOUT);
}

int dfu_dnload(usb_dev_handle *dev, uint16_t iface, 
		 uint16_t wBlockNum, void *data, uint16_t size)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_DNLOAD, wBlockNum, iface, data, size, 
			USB_DEFAULT_TIMEOUT);
}

int dfu_upload(usb_dev_handle *dev, uint16_t iface, 
		 uint16_t wBlockNum, void *data, uint16_t size)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_DNLOAD, wBlockNum, iface, data, size, 
			USB_DEFAULT_TIMEOUT);
}

int dfu_getstatus(usb_dev_handle *dev, uint16_t iface, dfu_status *status)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_GETSTATUS, 0, iface, (void*)status, sizeof(dfu_status), 
			USB_DEFAULT_TIMEOUT);
}

int dfu_clrstatus(usb_dev_handle *dev, uint16_t iface)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_CLRSTATUS, 0, iface, NULL, 0, USB_DEFAULT_TIMEOUT);
}

int dfu_getstate(usb_dev_handle *dev, uint16_t iface)
{
	int i;
	uint8_t state;
	do {
		i = usb_control_msg(dev, 
			USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_GETSTATE, 0, iface, &state, 1, USB_DEFAULT_TIMEOUT);
	} while(i == 0);

	if(i > 0) 
		return state;
	else
		return i;
}

int dfu_abort(usb_dev_handle *dev, uint16_t iface)
{
	return usb_control_msg(dev, 
			USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
			DFU_ABORT, 0, iface, NULL, 0, USB_DEFAULT_TIMEOUT);
}


int dfu_makeidle(usb_dev_handle *dev, uint16_t iface)
{
	int i;
	dfu_status status;

	for(i = 0; i < 3; i++) {
		if(dfu_getstatus(dev, iface, &status) < 0) {
			dfu_clrstatus(dev, iface);
			continue;
		}

		i--;
		
		switch(status.bState) {
		    case STATE_DFU_IDLE:
			return 0;

		    case STATE_DFU_DOWNLOAD_SYNC:
		    case STATE_DFU_DOWNLOAD_IDLE:
		    case STATE_DFU_MANIFEST_SYNC:
		    case STATE_DFU_UPLOAD_IDLE:
		    case STATE_DFU_DOWNLOAD_BUSY:
		    case STATE_DFU_MANIFEST:
			dfu_abort(dev, iface);
			continue;

		    case STATE_DFU_ERROR:
			dfu_clrstatus(dev, iface);
			continue;

		    case STATE_APP_IDLE:
			dfu_detach(dev, iface, DFU_DETACH_TIMEOUT);
			continue;

		    case STATE_APP_DETACH:
		    case STATE_DFU_MANIFEST_WAIT_RESET:
			usb_reset(dev);
			return -1;

		    default:
			return -1;
		}

	}

	return -1;
}