aboutsummaryrefslogtreecommitdiffstats
path: root/usb/class/dfu/usb_protocol_dfu.h
blob: 2e7323fb32c0d488b042bd726eeefedb1a415857 (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
/**
 * \file
 *
 * \brief USB Device Firmware Upgrade (DFU) protocol definitions
 *
 * Copyright (c) 2018 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef _USB_PROTOCOL_DFU_H_
#define _USB_PROTOCOL_DFU_H_

#include "usb_includes.h"

/*
 * \ingroup usb_protocol_group
 * \defgroup dfu_protocol_group Device Firmware Upgrade Definitions
 * \implements USB Device Firmware Upgrade Specification, Revision 1.1
 * @{
 */

/**
 * \name USB DFU Subclass IDs
 */
//@{
#define USB_DFU_CLASS 0xFE //!< Application Specific Class Code
//@}

//! \name USB DFU Subclass IDs
//@{
#define USB_DFU_SUBCLASS 0x01 //!< Device Firmware Upgrade Code
//@}

//! \name USB DFU Protocol IDs
//@{
#define USB_DFU_PROTOCOL_RUNTIME 0x01 //!< Runtime protocol
#define USB_DFU_PROTOCOL_DFU 0x02 //!< DFU mode protocol
//@}

//! \name USB DFU Attributes bits mask
//@{
#define USB_DFU_ATTRIBUTES_CAN_DOWNLOAD 0x01
#define USB_DFU_ATTRIBUTES_CAN_UPLOAD 0x02
#define USB_DFU_ATTRIBUTES_MANIFEST_TOLERANT 0x04
#define USB_DFU_ATTRIBUTES_WILL_DETACH 0x08
//@}

//! \name USB DFU Request IDs
//@{
#define USB_REQ_DFU_DETACH 0x00
#define USB_REQ_DFU_DNLOAD 0x01
#define USB_REQ_DFU_UPLOAD 0x02
#define USB_REQ_DFU_GETSTATUS 0x03
#define USB_REQ_DFU_CLRSTATUS 0x04
#define USB_REQ_DFU_GETSTATE 0x05
#define USB_REQ_DFU_ABORT 0x06
//@}

/*
 * Need to pack structures tightly, or the compiler might insert padding
 * and violate the spec-mandated layout.
 */
COMPILER_PACK_SET(1)

//! \name USB DFU Descriptors
//@{

//! DFU Functional Descriptor
typedef struct usb_dfu_func_desc {
	uint8_t bFunctionLength;
	uint8_t bDescriptorType;
	uint8_t bmAttributes;
	le16_t  wDetachTimeOut;
	le16_t  wTransferSize;
	le16_t  bcdDFUVersion;
} usb_dfu_func_desc_t;

#define USB_DFU_FUNC_DESC_LEN 0x09
#define USB_DFU_FUNC_DESC_TYPE 0x21
#define USB_DFU_FUNC_DESC_BYTES(bmAttributes, wDetachTimeOut, wTransferSize, bcdDFUVersion) \
	USB_DFU_FUNC_DESC_LEN, /* bFunctionLength */ \
	USB_DFU_FUNC_DESC_TYPE, /* bDescriptorType */ \
	bmAttributes, \
	LE_BYTE0(wDetachTimeOut), LE_BYTE1(wDetachTimeOut), \
	LE_BYTE0(wTransferSize), LE_BYTE1(wTransferSize), \
	LE_BYTE0(bcdDFUVersion), LE_BYTE1(bcdDFUVersion)

COMPILER_PACK_RESET()

//! @}

//! USB DFU Request IDs
enum usb_dfu_req {
	USB_DFU_DETACH,
	USB_DFU_DNLOAD,
	USB_DFU_UPLOAD,
	USB_DFU_GETSTATUS,
	USB_DFU_CLRSTATUS,
	USB_DFU_GETSTATE,
	USB_DFU_ABORT,
};

//! USB DFU Device Status IDs
enum usb_dfu_status {
	USB_DFU_STATUS_OK,
	USB_DFU_STATUS_ERR_TARGET,
	USB_DFU_STATUS_ERR_FILE,
	USB_DFU_STATUS_ERR_WRITE,
	USB_DFU_STATUS_ERR_ERASE,
	USB_DFU_STATUS_ERR_CHECK_ERASED,
	USB_DFU_STATUS_ERR_PROG,
	USB_DFU_STATUS_ERR_VERIFY,
	USB_DFU_STATUS_ERR_ADDRESS,
	USB_DFU_STATUS_ERR_NOTDONE,
	USB_DFU_STATUS_ERR_FIRMWARE,
	USB_DFU_STATUS_ERR_VENDOR,
	USB_DFU_STATUS_ERR_USBR,
	USB_DFU_STATUS_ERR_POR,
	USB_DFU_STATUS_ERR_UNKNOWN,
	USB_DFU_STATUS_ERR_STALLEDPKT,
};

//! USB DFU Device State IDs
enum usb_dfu_state {
	USB_DFU_STATE_APP_IDLE,
	USB_DFU_STATE_APP_DETACH,
	USB_DFU_STATE_DFU_IDLE,
	USB_DFU_STATE_DFU_DNLOAD_SYNC,
	USB_DFU_STATE_DFU_DNBUSY,
	USB_DFU_STATE_DFU_DNLOAD_IDLE,
	USB_DFU_STATE_DFU_MANIFEST_SYNC,
	USB_DFU_STATE_DFU_MANIFEST,
	USB_DFU_STATE_DFU_MANIFEST_WAIT_RESET,
	USB_DFU_STATE_DFU_UPLOAD_IDLE,
	USB_DFU_STATE_DFU_ERROR,
};

#endif // _USB_PROTOCOL_DFU_H_