blob: 2a44602304ac52ed913fba425330743012fe3eac [file] [log] [blame]
Harald Welte7ed6f3b2017-02-26 17:00:43 +01001#ifndef _USB_DEV_DFU_H
2#define _USB_DEV_DFU_H
3
4#include <stdint.h>
5#include <board.h>
6#include <usb/include/USBDescriptors.h>
7#include <usb/include/USBDDriver.h>
8
9#if 0
10/* This is valid for CCID */
11#define CONFIG_DFU_NUM_APP_IF 1
12#define CONFIG_DFU_NUM_APP_STR 4
13#else
14/* This is valid for CDC-Serial */
15#define CONFIG_DFU_NUM_APP_IF 2
16#define CONFIG_DFU_NUM_APP_STR 2
17#endif
18
19struct USBStringDescriptor {
20 USBGenericDescriptor hdr;
21 unsigned short wData[];
22} __attribute__((packed));
23
24
25#ifdef BOARD_USB_DFU
26
27#include <usb/common/dfu/usb_dfu.h>
28
29/* for board-specific config */
30#include <board.h>
31
32struct dfu_desc {
33 USBConfigurationDescriptor ucfg;
34 USBInterfaceDescriptor uif[BOARD_DFU_NUM_IF];
35 struct usb_dfu_func_descriptor func_dfu;
36} __attribute__ ((packed));
37
38/* USB DFU functional descriptor */
39#define DFU_FUNC_DESC { \
40 .bLength = USB_DT_DFU_SIZE, \
41 .bDescriptorType = USB_DT_DFU, \
42 .bmAttributes = USB_DFU_CAN_UPLOAD | USB_DFU_CAN_DOWNLOAD, \
43 .wDetachTimeOut = 0xff00, \
44 .wTransferSize = BOARD_DFU_PAGE_SIZE, \
45 .bcdDFUVersion = 0x0100, \
46}
47
48/* Number of DFU interface during runtime mode */
49#define DFURT_NUM_IF 1
50
51/* to be used by the runtime as part of its USB descriptor structure
52 * declaration */
53#define DFURT_IF_DESCRIPTOR_STRUCT \
54 USBInterfaceDescriptor dfu_rt; \
55 struct usb_dfu_func_descriptor func_dfu;
56
57/* to be used by the runtime as part of its USB Dsecriptor structure
58 * definition */
59#define DFURT_IF_DESCRIPTOR(dfuIF, dfuSTR) \
60 .dfu_rt = { \
61 .bLength = sizeof(USBInterfaceDescriptor), \
62 .bDescriptorType = USBGenericDescriptor_INTERFACE, \
63 .bInterfaceNumber = dfuIF, \
64 .bAlternateSetting = 0, \
65 .bNumEndpoints = 0, \
66 .bInterfaceClass = 0xFE, \
67 .bInterfaceSubClass = 0x01, \
68 .bInterfaceProtocol = 0x01, \
69 .iInterface = dfuSTR, \
70 }, \
71 .func_dfu = DFU_FUNC_DESC \
72
73/* provided by dfu_desc.c */
74extern const struct dfu_desc dfu_cfg_descriptor;
75extern const USBDDriverDescriptors dfu_descriptors;
76
77#else /* BOARD_USB_DFU */
78
79/* no DFU bootloader is being used */
80#define DFURT_NUM_IF 0
Harald Weltef2315412017-11-28 19:16:10 +010081#define DFURT_IF_DESCRIPTOR_STRUCT
82#define DFURT_IF_DESCRIPTOR(a, b)
Harald Welte7ed6f3b2017-02-26 17:00:43 +010083
84#endif /* BOARD_USB_DFU */
85
86/* magic value we use during boot to detect if we should start in DFU
87 * mode or runtime mode */
88#define USB_DFU_MAGIC 0xDFDFDFDF
Harald Welte7ed6f3b2017-02-26 17:00:43 +010089
90/* The API between the core DFU handler and the board/soc specific code */
91
92struct dfudata {
Harald Welted1e96342017-03-03 00:34:17 +010093 uint32_t magic;
Harald Welte7ed6f3b2017-02-26 17:00:43 +010094 uint8_t status;
95 uint32_t state;
96 int past_manifest;
97 unsigned int total_bytes;
98};
99
Harald Welte91fc4022017-03-03 01:05:22 +0100100/* RAM address for this magic value above */
101extern struct dfudata _g_dfu;
Harald Welteadba0ce2017-02-28 01:02:24 +0100102extern struct dfudata *g_dfu;
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100103
104void set_usb_serial_str(const uint8_t *serial_usbstr);
105
106void DFURT_SwitchToDFU(void);
107
108/* call-backs from DFU USB function driver to the board/SOC */
109extern int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
110 uint8_t *data, unsigned int len);
111extern int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
112 uint8_t *data, unsigned int req_len);
Harald Welte14051002017-03-04 19:17:27 +0100113extern int USBDFU_OverrideEnterDFU(void);
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100114
115/* function to be called at end of EP0 handler during runtime */
116void USBDFU_Runtime_RequestHandler(const USBGenericRequest *request);
117
118/* function to be called at end of EP0 handler during DFU mode */
119void USBDFU_DFU_RequestHandler(const USBGenericRequest *request);
120
121/* initialization of USB DFU driver (in DFU mode */
122void USBDFU_Initialize(const USBDDriverDescriptors *pDescriptors);
123
124/* USBD tells us to switch from DFU mode to application mode */
125void USBDFU_SwitchToApp(void);
126
127/* Return values to be used by USBDFU_handle_{dn,up}load */
128#define DFU_RET_NOTHING 0
129#define DFU_RET_ZLP 1
130#define DFU_RET_STALL 2
131
132#endif