blob: dd0e5e2c20ebef0d2621ffa79e3cbc877bff4dc2 [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
81#define DFURT_IF_DESCRIPTOR_STRUCT(a, b)
82#define DFURT_IF_DESCRIPTOR
83
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 Welteadba0ce2017-02-28 01:02:24 +0100100extern struct dfudata *g_dfu;
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100101
102void set_usb_serial_str(const uint8_t *serial_usbstr);
103
104void DFURT_SwitchToDFU(void);
105
106/* call-backs from DFU USB function driver to the board/SOC */
107extern int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
108 uint8_t *data, unsigned int len);
109extern int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
110 uint8_t *data, unsigned int req_len);
111
112/* function to be called at end of EP0 handler during runtime */
113void USBDFU_Runtime_RequestHandler(const USBGenericRequest *request);
114
115/* function to be called at end of EP0 handler during DFU mode */
116void USBDFU_DFU_RequestHandler(const USBGenericRequest *request);
117
118/* initialization of USB DFU driver (in DFU mode */
119void USBDFU_Initialize(const USBDDriverDescriptors *pDescriptors);
120
121/* USBD tells us to switch from DFU mode to application mode */
122void USBDFU_SwitchToApp(void);
123
124/* Return values to be used by USBDFU_handle_{dn,up}load */
125#define DFU_RET_NOTHING 0
126#define DFU_RET_ZLP 1
127#define DFU_RET_STALL 2
128
129#endif