Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 1 | #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 | |
| 19 | struct 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 | |
| 32 | struct 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 */ |
| 74 | extern const struct dfu_desc dfu_cfg_descriptor; |
| 75 | extern 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 Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 89 | |
| 90 | /* The API between the core DFU handler and the board/soc specific code */ |
| 91 | |
| 92 | struct dfudata { |
Harald Welte | d1e9634 | 2017-03-03 00:34:17 +0100 | [diff] [blame] | 93 | uint32_t magic; |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 94 | uint8_t status; |
| 95 | uint32_t state; |
| 96 | int past_manifest; |
| 97 | unsigned int total_bytes; |
| 98 | }; |
| 99 | |
Harald Welte | 91fc402 | 2017-03-03 01:05:22 +0100 | [diff] [blame^] | 100 | /* RAM address for this magic value above */ |
| 101 | extern struct dfudata _g_dfu; |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 102 | extern struct dfudata *g_dfu; |
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 103 | |
| 104 | void set_usb_serial_str(const uint8_t *serial_usbstr); |
| 105 | |
| 106 | void DFURT_SwitchToDFU(void); |
| 107 | |
| 108 | /* call-backs from DFU USB function driver to the board/SOC */ |
| 109 | extern int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, |
| 110 | uint8_t *data, unsigned int len); |
| 111 | extern int USBDFU_handle_upload(uint8_t altif, unsigned int offset, |
| 112 | uint8_t *data, unsigned int req_len); |
| 113 | |
| 114 | /* function to be called at end of EP0 handler during runtime */ |
| 115 | void USBDFU_Runtime_RequestHandler(const USBGenericRequest *request); |
| 116 | |
| 117 | /* function to be called at end of EP0 handler during DFU mode */ |
| 118 | void USBDFU_DFU_RequestHandler(const USBGenericRequest *request); |
| 119 | |
| 120 | /* initialization of USB DFU driver (in DFU mode */ |
| 121 | void USBDFU_Initialize(const USBDDriverDescriptors *pDescriptors); |
| 122 | |
| 123 | /* USBD tells us to switch from DFU mode to application mode */ |
| 124 | void USBDFU_SwitchToApp(void); |
| 125 | |
| 126 | /* Return values to be used by USBDFU_handle_{dn,up}load */ |
| 127 | #define DFU_RET_NOTHING 0 |
| 128 | #define DFU_RET_ZLP 1 |
| 129 | #define DFU_RET_STALL 2 |
| 130 | |
| 131 | #endif |